Skip to content

Commit

Permalink
Refactor: cleaned more code
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Oct 30, 2023
1 parent 1a994fa commit 77cb8cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 86 deletions.
62 changes: 17 additions & 45 deletions pkg/protocol/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type Chain struct {
AttestedWeight reactive.Variable[uint64]
VerifiedWeight reactive.Variable[uint64]
NetworkClockSlot reactive.Variable[iotago.SlotIndex]
SyncThreshold reactive.Variable[iotago.SlotIndex]
WarpSync reactive.Variable[bool]
WarpSyncThreshold reactive.Variable[iotago.SlotIndex]
OutOfSyncThreshold reactive.Variable[iotago.SlotIndex]
Expand All @@ -37,7 +36,6 @@ type Chain struct {
IsEvicted reactive.Event

commitments *shrinkingmap.ShrinkingMap[iotago.SlotIndex, *Commitment]
parentEngine reactive.Variable[*engine.Engine]
SpawnedEngine reactive.Variable[*engine.Engine]

log.Logger
Expand All @@ -63,7 +61,6 @@ func NewChain(protocol *Protocol) *Chain {
IsEvicted: reactive.NewEvent(),

commitments: shrinkingmap.New[iotago.SlotIndex, *Commitment](),
parentEngine: reactive.NewVariable[*engine.Engine](),
VerifyState: reactive.NewVariable[bool](),
SpawnedEngine: reactive.NewVariable[*engine.Engine](),
}
Expand Down Expand Up @@ -102,18 +99,6 @@ func NewChain(protocol *Protocol) *Chain {
}
})

c.SyncThreshold = reactive.NewDerivedVariable2[iotago.SlotIndex](func(forkingPoint, latestVerifiedCommitment *Commitment) iotago.SlotIndex {
if forkingPoint == nil {
return SyncWindow + 1
}

if latestVerifiedCommitment == nil {
return forkingPoint.Slot() + SyncWindow + 1
}

return latestVerifiedCommitment.Slot() + SyncWindow + 1
}, c.ForkingPoint, c.LatestVerifiedCommitment)

c.ParentChain.OnUpdate(func(prevParent, newParent *Chain) {
if prevParent != nil {
prevParent.ChildChains.Delete(c)
Expand All @@ -128,31 +113,28 @@ func NewChain(protocol *Protocol) *Chain {
forkingPointContext(func() func() {
return forkingPoint.Parent.OnUpdate(func(_, parent *Commitment) {
forkingPointContext(func() func() {
return lo.Batch(
c.ParentChain.InheritFrom(parent.Chain),
c.parentEngine.InheritFrom(parent.Engine),
)
return c.ParentChain.InheritFrom(parent.Chain)
})
})
})
})

c.Logger = protocol.NewEntityLogger("Chain", c.IsEvicted, func(entityLogger log.Logger) {
entityLogger.LogDebug("created")

c.WarpSync.LogUpdates(entityLogger, log.LevelTrace, "WarpSync")
c.NetworkClockSlot.LogUpdates(entityLogger, log.LevelTrace, "NetworkClockSlot")
c.WarpSyncThreshold.LogUpdates(entityLogger, log.LevelTrace, "WarpSyncThreshold")
c.OutOfSyncThreshold.LogUpdates(entityLogger, log.LevelTrace, "OutOfSyncThreshold")
c.ForkingPoint.LogUpdates(entityLogger, log.LevelTrace, "ForkingPoint", (*Commitment).LogName)
c.ClaimedWeight.LogUpdates(entityLogger, log.LevelTrace, "ClaimedWeight")
c.AttestedWeight.LogUpdates(entityLogger, log.LevelTrace, "AttestedWeight")
c.VerifiedWeight.LogUpdates(entityLogger, log.LevelTrace, "VerifiedWeight")
c.LatestCommitment.LogUpdates(entityLogger, log.LevelTrace, "LatestCommitment", (*Commitment).LogName)
c.LatestVerifiedCommitment.LogUpdates(entityLogger, log.LevelDebug, "LatestVerifiedCommitment", (*Commitment).LogName)
c.VerifyAttestations.LogUpdates(entityLogger, log.LevelTrace, "VerifyAttestations")
c.VerifyState.LogUpdates(entityLogger, log.LevelDebug, "VerifyState")
c.SpawnedEngine.LogUpdates(entityLogger, log.LevelDebug, "SpawnedEngine", (*engine.Engine).LogName)
c.Logger = protocol.NewEntityLogger("Chain", c.IsEvicted, func(chainLogger log.Logger) {
chainLogger.LogDebug("created")

c.WarpSync.LogUpdates(chainLogger, log.LevelTrace, "WarpSync")
c.NetworkClockSlot.LogUpdates(chainLogger, log.LevelTrace, "NetworkClockSlot")
c.WarpSyncThreshold.LogUpdates(chainLogger, log.LevelTrace, "WarpSyncThreshold")
c.OutOfSyncThreshold.LogUpdates(chainLogger, log.LevelTrace, "OutOfSyncThreshold")
c.ForkingPoint.LogUpdates(chainLogger, log.LevelTrace, "ForkingPoint", (*Commitment).LogName)
c.ClaimedWeight.LogUpdates(chainLogger, log.LevelTrace, "ClaimedWeight")
c.AttestedWeight.LogUpdates(chainLogger, log.LevelTrace, "AttestedWeight")
c.VerifiedWeight.LogUpdates(chainLogger, log.LevelTrace, "VerifiedWeight")
c.LatestCommitment.LogUpdates(chainLogger, log.LevelTrace, "LatestCommitment", (*Commitment).LogName)
c.LatestVerifiedCommitment.LogUpdates(chainLogger, log.LevelDebug, "LatestVerifiedCommitment", (*Commitment).LogName)
c.VerifyAttestations.LogUpdates(chainLogger, log.LevelTrace, "VerifyAttestations")
c.VerifyState.LogUpdates(chainLogger, log.LevelDebug, "VerifyState")
c.SpawnedEngine.LogUpdates(chainLogger, log.LevelDebug, "SpawnedEngine", (*engine.Engine).LogName)
})

return c
Expand Down Expand Up @@ -283,16 +265,6 @@ func (c *Chain) DispatchBlock(block *model.Block, src peer.ID) (success bool) {
return success
}

func (c *Chain) InSyncRange(slot iotago.SlotIndex) bool {
if latestVerifiedCommitment := c.LatestVerifiedCommitment.Get(); latestVerifiedCommitment != nil {
return slot > latestVerifiedCommitment.Slot() && slot < c.SyncThreshold.Get()
}

forkingPoint := c.ForkingPoint.Get()

return forkingPoint != nil && (slot > forkingPoint.Slot()-1 && slot < c.SyncThreshold.Get())
}

func (c *Chain) registerCommitment(commitment *Commitment) (unregister func()) {
c.commitments.Set(commitment.Slot(), commitment)

Expand Down
41 changes: 0 additions & 41 deletions pkg/protocol/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/iotaledger/hive.go/log"
"github.com/iotaledger/iota-core/pkg/model"
"github.com/iotaledger/iota-core/pkg/protocol/engine"
iotago "github.com/iotaledger/iota.go/v4"
)

type Commitment struct {
Expand Down Expand Up @@ -37,8 +36,6 @@ type Commitment struct {
protocol *Protocol
isDirectlyAboveLatestAttestedCommitment reactive.Variable[bool]
isDirectlyAboveLatestVerifiedCommitment reactive.Variable[bool]
isBelowSyncThreshold reactive.Event
isBelowWarpSyncThreshold reactive.Event

log.Logger
}
Expand Down Expand Up @@ -70,8 +67,6 @@ func NewCommitment(commitment *model.Commitment, protocol *Protocol) *Commitment
protocol: protocol,
isDirectlyAboveLatestAttestedCommitment: reactive.NewVariable[bool](),
isDirectlyAboveLatestVerifiedCommitment: reactive.NewVariable[bool](),
isBelowSyncThreshold: reactive.NewEvent(),
isBelowWarpSyncThreshold: reactive.NewEvent(),
}

c.Parent.OnUpdateWithContext(func(_, parent *Commitment, unsubscribeOnUpdate func(subscriptionFactory func() (unsubscribe func()))) {
Expand Down Expand Up @@ -124,16 +119,6 @@ func NewCommitment(commitment *model.Commitment, protocol *Protocol) *Commitment
c.isDirectlyAboveLatestVerifiedCommitment.InheritFrom(reactive.NewDerivedVariable2(func(parentIsVerified, isVerified bool) bool {
return parentIsVerified && !isVerified
}, parent.IsVerified, c.IsVerified))

c.triggerEventIfBelowThreshold(
func(c *Commitment) reactive.Event { return c.isBelowSyncThreshold },
func(c *Chain) reactive.Variable[iotago.SlotIndex] { return c.SyncThreshold },
)

c.triggerEventIfBelowThreshold(
func(c *Commitment) reactive.Event { return c.isBelowWarpSyncThreshold },
func(c *Chain) reactive.Variable[iotago.SlotIndex] { return c.WarpSyncThreshold },
)
})

c.Chain.OnUpdateWithContext(func(_, chain *Chain, withinContext func(subscriptionFactory func() (unsubscribe func()))) {
Expand Down Expand Up @@ -166,8 +151,6 @@ func NewCommitment(commitment *model.Commitment, protocol *Protocol) *Commitment
c.IsSolid.Set(true)
c.IsAttested.Set(true)
c.IsVerified.Set(true)
c.isBelowWarpSyncThreshold.Set(true)
c.isBelowSyncThreshold.Set(true)
})

c.Logger = protocol.NewEntityLogger(fmt.Sprintf("Slot%d.", commitment.Slot()), c.IsEvicted, func(entityLogger log.Logger) {
Expand Down Expand Up @@ -250,27 +233,3 @@ func (c *Commitment) promote(targetChain *Chain) {
}
}
}

func (c *Commitment) triggerEventIfBelowThreshold(event func(*Commitment) reactive.Event, chainThreshold func(*Chain) reactive.Variable[iotago.SlotIndex]) {
c.Chain.OnUpdateWithContext(func(_, chain *Chain, withinContext func(subscriptionFactory func() (unsubscribe func()))) {
if chain == nil {
return
}

// only monitor the threshold after the parent event was triggered (minimize listeners to same threshold)
withinContext(func() (unsubscribe func()) {
return event(c.Parent.Get()).OnTrigger(func() {
if chain == nil {
c.LogError("chain is nil IN HERE")
}

// since events only trigger once, we unsubscribe from the threshold after the trigger condition is met
chainThreshold(chain).OnUpdateOnce(func(_, _ iotago.SlotIndex) {
event(c).Trigger()
}, func(_, slot iotago.SlotIndex) bool {
return c.Slot() < slot
})
})
})
})
}

0 comments on commit 77cb8cc

Please sign in to comment.