From 0570f4d02510059f4ecc7f2ccb8742a2cc574f75 Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Fri, 26 Apr 2024 03:30:29 +0200 Subject: [PATCH] Refactor: refactored flags --- pkg/protocol/chain.go | 32 +++++++++++++++++--------------- pkg/protocol/chains.go | 10 ++-------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/pkg/protocol/chain.go b/pkg/protocol/chain.go index d8190ec96..2acbb4a68 100644 --- a/pkg/protocol/chain.go +++ b/pkg/protocol/chain.go @@ -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] @@ -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 @@ -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](), @@ -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, @@ -234,10 +234,10 @@ func (c *Chain) initDerivedProperties() (shutdown func()) { parentChain.deriveChildChains(c), c.deriveShouldEvict(forkingPoint, parentChain), - - c.deriveIsSolid(forkingPoint, parentChain), ) }), + + c.deriveDivergencePointVerified(forkingPoint), ) }), ), @@ -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 { @@ -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()) { diff --git a/pkg/protocol/chains.go b/pkg/protocol/chains.go index a874c5d76..01ce3d449 100644 --- a/pkg/protocol/chains.go +++ b/pkg/protocol/chains.go @@ -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) { @@ -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() {