Skip to content

Commit

Permalink
Refactor: refactored flags
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Apr 26, 2024
1 parent 3e2e5f3 commit 0570f4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
32 changes: 17 additions & 15 deletions pkg/protocol/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Chain struct {
// ParentChain contains the chain that this chain forked from.
ParentChain reactive.Variable[*Chain]

// DivergencePointVerified contains a flag that indicates whether the divergence point of this chain is verified.
DivergencePointVerified reactive.Event

// ChildChains contains the set of all chains that forked from this chain.
ChildChains reactive.Set[*Chain]

Expand Down Expand Up @@ -54,9 +57,6 @@ type Chain struct {
// Engine contains the engine instance that is used to process blocks for this chain.
Engine reactive.Variable[*engine.Engine]

// IsSolid contains a flag that indicates whether this chain is solid (has a connection to the root).
IsSolid reactive.Event

// IsEvicted contains a flag that indicates whether this chain was evicted.
IsEvicted reactive.Event

Expand All @@ -78,6 +78,7 @@ func newChain(chains *Chains) *Chain {
c := &Chain{
ForkingPoint: reactive.NewVariable[*Commitment](),
ParentChain: reactive.NewVariable[*Chain](),
DivergencePointVerified: reactive.NewEvent(),
ChildChains: reactive.NewSet[*Chain](),
LatestCommitment: reactive.NewVariable[*Commitment](),
LatestAttestedCommitment: reactive.NewVariable[*Commitment](),
Expand All @@ -89,7 +90,6 @@ func newChain(chains *Chains) *Chain {
StartEngine: reactive.NewVariable[bool](),
Engine: reactive.NewVariable[*engine.Engine](),
IsEvicted: reactive.NewEvent(),
IsSolid: reactive.NewEvent(),
shouldEvict: reactive.NewEvent(),

chains: chains,
Expand Down Expand Up @@ -234,10 +234,10 @@ func (c *Chain) initDerivedProperties() (shutdown func()) {
parentChain.deriveChildChains(c),

c.deriveShouldEvict(forkingPoint, parentChain),

c.deriveIsSolid(forkingPoint, parentChain),
)
}),

c.deriveDivergencePointVerified(forkingPoint),
)
}),
),
Expand Down Expand Up @@ -270,15 +270,6 @@ func (c *Chain) deriveShouldEvict(forkingPoint *Commitment, parentChain *Chain)
return
}

// deriveIsSolid defines how a chain determines whether it is solid (has a connection to the root).
func (c *Chain) deriveIsSolid(forkingPoint *Commitment, parentChain *Chain) (shutdown func()) {
if parentChain != nil {
return c.IsSolid.InheritFrom(parentChain.IsSolid)
}

return c.IsSolid.InheritFrom(forkingPoint.IsRoot)
}

// deriveWarpSyncMode defines how a chain determines whether it is in warp sync mode or not.
func (c *Chain) deriveWarpSyncMode(engine *engine.Engine) func() {
return c.WarpSyncMode.DeriveValueFrom(reactive.NewDerivedVariable4(func(warpSyncMode bool, engineInitialized bool, latestSyncedSlot iotago.SlotIndex, latestSeenSlot iotago.SlotIndex, outOfSyncThreshold iotago.SlotIndex) bool {
Expand All @@ -305,6 +296,17 @@ func (c *Chain) deriveChildChains(child *Chain) (teardown func()) {
return
}

// deriveDivergencePointVerified defines how a chain determines whether its divergence point is verified.
func (c *Chain) deriveDivergencePointVerified(forkingPoint *Commitment) (shutdown func()) {
return lo.Batch(
c.DivergencePointVerified.InheritFrom(forkingPoint.IsRoot),

forkingPoint.Parent.WithNonEmptyValue(func(parentOfForkingPoint *Commitment) (teardown func()) {
return c.DivergencePointVerified.InheritFrom(parentOfForkingPoint.IsVerified)
}),
)
}

// deriveParentChain defines how a chain determines its parent chain from its forking point (it inherits the Chain from
// the parent commitment of the forking point or nil if either of them is still unknown).
func (c *Chain) deriveParentChain(forkingPoint *Commitment) (shutdown func()) {
Expand Down
10 changes: 2 additions & 8 deletions pkg/protocol/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,7 @@ func (c *Chains) initChainSwitching() (shutdown func()) {

return lo.BatchReverse(
c.HeaviestClaimedCandidate.WithNonEmptyValue(func(heaviestClaimedCandidate *Chain) (shutdown func()) {
return heaviestClaimedCandidate.ForkingPoint.WithNonEmptyValue(func(forkingPoint *Commitment) (teardown func()) {
return forkingPoint.Parent.WithNonEmptyValue(func(parentOfForkingPoint *Commitment) (teardown func()) {
return parentOfForkingPoint.IsVerified.WithNonEmptyValue(func(_ bool) (teardown func()) {
return heaviestClaimedCandidate.RequestAttestations.ToggleValue(true)
})
})
})
return heaviestClaimedCandidate.RequestAttestations.ToggleValue(true)
}),

c.HeaviestAttestedCandidate.OnUpdate(func(_ *Chain, heaviestAttestedCandidate *Chain) {
Expand All @@ -351,7 +345,7 @@ func (c *Chains) initChainSwitching() (shutdown func()) {

func (c *Chains) trackHeaviestCandidates(chain *Chain) (teardown func()) {
return chain.LatestCommitment.OnUpdate(func(_ *Commitment, latestCommitment *Commitment) {
chain.IsSolid.OnTrigger(func() {
chain.DivergencePointVerified.OnTrigger(func() {
targetSlot := latestCommitment.ID().Index()

if evictionEvent := c.protocol.EvictionEvent(targetSlot); !evictionEvent.WasTriggered() {
Expand Down

0 comments on commit 0570f4d

Please sign in to comment.