diff --git a/components/debugapi/commitment.go b/components/debugapi/commitment.go index 9e7bcee5b..2b875e152 100644 --- a/components/debugapi/commitment.go +++ b/components/debugapi/commitment.go @@ -13,7 +13,7 @@ import ( ) func chainManagerAllChainsDot() (string, error) { - rootCommitment := deps.Protocol.MainChain().ForkingPoint.Get() + rootCommitment := deps.Protocol.MainChain.Get().ForkingPoint.Get() g := graphviz.New() defer g.Close() @@ -32,7 +32,7 @@ func chainManagerAllChainsDot() (string, error) { } func chainManagerAllChainsRendered() ([]byte, error) { - rootCommitment := deps.Protocol.MainChain().ForkingPoint.Get() + rootCommitment := deps.Protocol.MainChain.Get().ForkingPoint.Get() g := graphviz.New() defer g.Close() @@ -75,7 +75,7 @@ func prepareCommitmentGraph(g *graphviz.Graphviz, rootCommitment *protocol.Commi return childErr } - if childCommitment.Chain.Get() == deps.Protocol.MainChain() { + if childCommitment.Chain.Get() == deps.Protocol.MainChain.Get() { child.SetColor("green") } diff --git a/pkg/protocol/chain.go b/pkg/protocol/chain.go index 644ae049b..f6c4b5ae4 100644 --- a/pkg/protocol/chain.go +++ b/pkg/protocol/chain.go @@ -18,25 +18,27 @@ type Chain struct { VerifiedWeight reactive.Variable[uint64] SyncThreshold reactive.Variable[iotago.SlotIndex] WarpSyncThreshold reactive.Variable[iotago.SlotIndex] - requestAttestations reactive.Variable[bool] - engine *chainEngine - isSolid reactive.Event - evicted reactive.Event - commitments *shrinkingmap.ShrinkingMap[iotago.SlotIndex, *Commitment] + RequestAttestations reactive.Variable[bool] + Engine *chainEngine + IsSolid reactive.Event + IsEvicted reactive.Event + + commitments *shrinkingmap.ShrinkingMap[iotago.SlotIndex, *Commitment] } func NewChain() *Chain { c := &Chain{ ForkingPoint: reactive.NewVariable[*Commitment](), - commitments: shrinkingmap.New[iotago.SlotIndex, *Commitment](), LatestCommitment: reactive.NewVariable[*Commitment](), LatestAttestedCommitment: reactive.NewVariable[*Commitment](), LatestVerifiedCommitment: reactive.NewVariable[*Commitment](), - requestAttestations: reactive.NewVariable[bool](), - evicted: reactive.NewEvent(), + RequestAttestations: reactive.NewVariable[bool](), + IsEvicted: reactive.NewEvent(), + + commitments: shrinkingmap.New[iotago.SlotIndex, *Commitment](), } - c.engine = newChainEngine(c) + c.Engine = newChainEngine(c) c.ClaimedWeight = reactive.NewDerivedVariable(cumulativeWeight, c.LatestCommitment) c.AttestedWeight = reactive.NewDerivedVariable(cumulativeWeight, c.LatestAttestedCommitment) @@ -83,18 +85,6 @@ func (c *Chain) Commitment(index iotago.SlotIndex) (commitment *Commitment, exis return nil, false } -func (c *Chain) RequestAttestations() reactive.Variable[bool] { - return c.requestAttestations -} - -func (c *Chain) Engine() *engine.Engine { - return c.engine.Get() -} - -func (c *Chain) EngineR() reactive.Variable[*engine.Engine] { - return c.engine -} - func (c *Chain) InSyncRange(index iotago.SlotIndex) bool { if latestVerifiedCommitment := c.LatestVerifiedCommitment.Get(); latestVerifiedCommitment != nil { return index > c.LatestVerifiedCommitment.Get().Index() && index < c.SyncThreshold.Get() @@ -103,10 +93,6 @@ func (c *Chain) InSyncRange(index iotago.SlotIndex) bool { return false } -func (c *Chain) Evicted() reactive.Event { - return c.evicted -} - func (c *Chain) registerCommitment(commitment *Commitment) { c.commitments.Set(commitment.Index(), commitment) diff --git a/pkg/protocol/chains.go b/pkg/protocol/chains.go index 103564140..0e8186abe 100644 --- a/pkg/protocol/chains.go +++ b/pkg/protocol/chains.go @@ -18,7 +18,7 @@ import ( type Chains struct { protocol *Protocol - mainChain reactive.Variable[*Chain] + MainChain reactive.Variable[*Chain] heaviestClaimedCandidate reactive.Variable[*Chain] @@ -41,7 +41,7 @@ func newChains(protocol *Protocol) *Chains { c := &Chains{ protocol: protocol, EvictionState: reactive.NewEvictionState[iotago.SlotIndex](), - mainChain: reactive.NewVariable[*Chain]().Init(NewChain()), + MainChain: reactive.NewVariable[*Chain]().Init(NewChain()), heaviestClaimedCandidate: reactive.NewVariable[*Chain](), heaviestAttestedCandidate: reactive.NewVariable[*Chain](), heaviestVerifiedCandidate: reactive.NewVariable[*Chain](), @@ -80,7 +80,7 @@ func newChains(protocol *Protocol) *Chains { c.publishEngineCommitments(chain) }) - c.chainCreated.Trigger(c.mainChain.Get()) + c.chainCreated.Trigger(c.MainChain.Get()) protocol.HookConstructed(func() { c.initMainChain() @@ -93,29 +93,29 @@ func newChains(protocol *Protocol) *Chains { } func (c *Chains) initMainChain() { - mainChain := c.mainChain.Get() - mainChain.engine.instantiate.Set(true) - mainChain.engine.OnUpdate(func(_, newEngine *engine.Engine) { + mainChain := c.MainChain.Get() + mainChain.Engine.instantiate.Set(true) + mainChain.Engine.OnUpdate(func(_, newEngine *engine.Engine) { c.protocol.Events.Engine.LinkTo(newEngine.Events) }) mainChain.ForkingPoint.Get().IsRoot.Trigger() } func (c *Chains) provideEngineIfRequested(chain *Chain) func() { - return chain.engine.instantiate.OnUpdate(func(_, instantiate bool) { + return chain.Engine.instantiate.OnUpdate(func(_, instantiate bool) { if !instantiate { - chain.engine.spawnedEngine.Set(nil) + chain.Engine.spawnedEngine.Set(nil) return } - if currentEngine := chain.engine.Get(); currentEngine == nil { + if currentEngine := chain.Engine.Get(); currentEngine == nil { mainEngine, err := c.engineManager.LoadActiveEngine(c.protocol.options.SnapshotPath) if err != nil { panic(fmt.Sprintf("could not load active engine: %s", err)) } - chain.engine.spawnedEngine.Set(mainEngine) + chain.Engine.spawnedEngine.Set(mainEngine) c.protocol.Network.HookStopped(mainEngine.Shutdown) } else { @@ -164,20 +164,8 @@ func (c *Chains) OnChainCreated(callback func(chain *Chain)) (unsubscribe func() return c.chainCreated.Hook(callback).Unhook } -func (c *Chains) MainChain() *Chain { - return c.mainChain.Get() -} - -func (c *Chains) MainChainR() reactive.Variable[*Chain] { - return c.mainChain -} - func (c *Chains) MainEngineInstance() *engine.Engine { - return c.mainChain.Get().Engine() -} - -func (c *Chains) MainEngineR() reactive.Variable[*engine.Engine] { - return c.mainChain.Get().EngineR() + return c.MainChain.Get().Engine.Get() } func (c *Chains) HeaviestClaimedCandidate() reactive.Variable[*Chain] { @@ -195,18 +183,18 @@ func (c *Chains) HeaviestVerifiedCandidate() reactive.Variable[*Chain] { func (c *Chains) initChainSwitching() { c.heaviestClaimedCandidate.OnUpdate(func(prevCandidate, newCandidate *Chain) { if prevCandidate != nil { - prevCandidate.requestAttestations.Set(false) + prevCandidate.RequestAttestations.Set(false) } - newCandidate.requestAttestations.Set(true) + newCandidate.RequestAttestations.Set(true) }) c.heaviestAttestedCandidate.OnUpdate(func(prevCandidate, newCandidate *Chain) { if prevCandidate != nil { - prevCandidate.engine.instantiate.Set(false) + prevCandidate.Engine.instantiate.Set(false) } - newCandidate.engine.instantiate.Set(true) + newCandidate.Engine.instantiate.Set(true) }) c.OnChainCreated(func(chain *Chain) { @@ -245,7 +233,7 @@ func (c *Chains) setupCommitment(commitment *Commitment, slotEvictedEvent reacti func (c *Chains) requestCommitment(commitmentID iotago.CommitmentID, requestFromPeers bool, optSuccessCallbacks ...func(metadata *Commitment)) (commitmentRequest *promise.Promise[*Commitment], err error) { slotEvicted := c.EvictionEvent(commitmentID.Index()) if slotEvicted.WasTriggered() && c.LastEvictedSlot().Get() != 0 { - forkingPoint := c.mainChain.Get().ForkingPoint.Get() + forkingPoint := c.MainChain.Get().ForkingPoint.Get() if forkingPoint == nil || commitmentID != forkingPoint.ID() { return nil, ErrorSlotEvicted @@ -283,7 +271,7 @@ func (c *Chains) requestCommitment(commitmentID iotago.CommitmentID, requestFrom } func (c *Chains) publishEngineCommitments(chain *Chain) { - chain.engine.OnUpdateWithContext(func(_, engine *engine.Engine, withinContext func(subscriptionFactory func() (unsubscribe func()))) { + chain.Engine.OnUpdateWithContext(func(_, engine *engine.Engine, withinContext func(subscriptionFactory func() (unsubscribe func()))) { if engine != nil { withinContext(func() (unsubscribe func()) { var ( @@ -335,12 +323,12 @@ func (c *Chains) publishEngineCommitments(chain *Chain) { func (c *Chains) trackHeaviestCandidate(candidateVariable reactive.Variable[*Chain], chainWeightVariable func(*Chain) reactive.Variable[uint64], candidate *Chain) { chainWeightVariable(candidate).OnUpdate(func(_, newChainWeight uint64) { - if newChainWeight <= c.mainChain.Get().VerifiedWeight.Get() { + if newChainWeight <= c.MainChain.Get().VerifiedWeight.Get() { return } candidateVariable.Compute(func(currentCandidate *Chain) *Chain { - if currentCandidate == nil || currentCandidate.evicted.WasTriggered() || newChainWeight > chainWeightVariable(currentCandidate).Get() { + if currentCandidate == nil || currentCandidate.IsEvicted.WasTriggered() || newChainWeight > chainWeightVariable(currentCandidate).Get() { return candidate } diff --git a/pkg/protocol/commitment.go b/pkg/protocol/commitment.go index fb16bd5ec..3da748ec9 100644 --- a/pkg/protocol/commitment.go +++ b/pkg/protocol/commitment.go @@ -104,7 +104,7 @@ func NewCommitment(commitment *model.Commitment) *Commitment { chain.registerCommitment(c) withinContext(func() (unsubscribe func()) { - return chain.engine.OnUpdate(func(_, chainEngine *engine.Engine) { + return chain.Engine.OnUpdate(func(_, chainEngine *engine.Engine) { c.Engine.Set(chainEngine) }) }) @@ -112,7 +112,7 @@ func NewCommitment(commitment *model.Commitment) *Commitment { withinContext(func() (unsubscribe func()) { requestAttestations := reactive.NewDerivedVariable2(func(requestAttestations, isDirectlyAboveLatestAttestedCommitment bool) bool { return requestAttestations && isDirectlyAboveLatestAttestedCommitment - }, chain.requestAttestations, c.isDirectlyAboveLatestAttestedCommitment) + }, chain.RequestAttestations, c.isDirectlyAboveLatestAttestedCommitment) c.RequestAttestations.InheritFrom(requestAttestations) @@ -158,7 +158,7 @@ func (c *Commitment) inheritChain(parent *Commitment) func(*Commitment, *Commitm case c: if spawnedChain != nil { - spawnedChain.evicted.Trigger() + spawnedChain.IsEvicted.Trigger() } unsubscribeFromParent = parent.Chain.OnUpdate(func(_, chain *Chain) { diff --git a/pkg/protocol/gossip.go b/pkg/protocol/gossip.go index 43e8cbce5..a12a67ac5 100644 --- a/pkg/protocol/gossip.go +++ b/pkg/protocol/gossip.go @@ -250,10 +250,10 @@ func (r *Gossip) OnAttestationsRequested(callback func(commitmentID iotago.Commi func (r *Gossip) startAttestationsRequester() { r.protocol.HookConstructed(func() { r.protocol.OnChainCreated(func(chain *Chain) { - chain.RequestAttestations().OnUpdate(func(_, requestAttestations bool) { + chain.RequestAttestations.OnUpdate(func(_, requestAttestations bool) { if requestAttestations { r.commitmentVerifiers.GetOrCreate(chain.ForkingPoint.Get().ID(), func() *CommitmentVerifier { - return NewCommitmentVerifier(chain.EngineR().Get(), chain.ForkingPoint.Get().Parent.Get().Commitment) + return NewCommitmentVerifier(chain.Engine.Get(), chain.ForkingPoint.Get().Parent.Get().Commitment) }) } else { r.commitmentVerifiers.Delete(chain.ForkingPoint.Get().ID()) @@ -292,11 +292,11 @@ func (r *Gossip) startBlockRequester() { engine.HookShutdown(unsubscribe) } - r.protocol.MainEngineR().OnUpdate(func(_, engine *engine.Engine) { + r.protocol.MainChain.Get().Engine.OnUpdate(func(_, engine *engine.Engine) { startBlockRequester(engine) }) r.protocol.OnChainCreated(func(chain *Chain) { - chain.EngineR().OnUpdate(func(_, engine *engine.Engine) { startBlockRequester(engine) }) + chain.Engine.OnUpdate(func(_, engine *engine.Engine) { startBlockRequester(engine) }) }) } diff --git a/pkg/testsuite/chainmanager.go b/pkg/testsuite/chainmanager.go index c7bb7306b..3d1d2fe3c 100644 --- a/pkg/testsuite/chainmanager.go +++ b/pkg/testsuite/chainmanager.go @@ -10,7 +10,7 @@ func (t *TestSuite) AssertChainManagerIsSolid(nodes ...*mock.Node) { for _, node := range nodes { t.Eventually(func() error { - chain := node.Protocol.MainChain() + chain := node.Protocol.MainChain.Get() if chain == nil { return ierrors.Errorf("AssertChainManagerIsSolid: %s: chain is nil", node.Name) } diff --git a/pkg/testsuite/mock/node.go b/pkg/testsuite/mock/node.go index 4502383a7..a5dc0dba4 100644 --- a/pkg/testsuite/mock/node.go +++ b/pkg/testsuite/mock/node.go @@ -207,7 +207,7 @@ func (n *Node) hookLogging(failOnBlockFiltered bool) { // n.attachEngineLogs(failOnBlockFiltered, engine) //}) - n.Protocol.MainEngineR().OnUpdate(func(_, engine *engine.Engine) { + n.Protocol.MainChain.Get().Engine.OnUpdate(func(_, engine *engine.Engine) { fmt.Printf("%s > MainEngineSwitched: %s, ChainID:%s Index:%s\n", n.Name, engine.Name(), engine.ChainID(), engine.ChainID().Index()) }) diff --git a/pkg/testsuite/storage_settings.go b/pkg/testsuite/storage_settings.go index 1bfff3c55..2ab65a670 100644 --- a/pkg/testsuite/storage_settings.go +++ b/pkg/testsuite/storage_settings.go @@ -67,7 +67,7 @@ func (t *TestSuite) AssertCommitmentSlotIndexExists(slot iotago.SlotIndex, nodes } // Make sure the commitment is also available in the ChainManager. - if node.Protocol.MainChain().LatestCommitment.Get().ID().Index() < slot { + if node.Protocol.MainChain.Get().LatestCommitment.Get().ID().Index() < slot { return ierrors.Errorf("AssertCommitmentSlotIndexExists: %s: commitment at index %v not found in ChainManager", node.Name, slot) } @@ -123,7 +123,7 @@ func (t *TestSuite) AssertChainID(expectedChainID iotago.CommitmentID, nodes ... for _, node := range nodes { t.Eventually(func() error { - actualChainID := node.Protocol.MainChain().ForkingPoint.Get().ID() + actualChainID := node.Protocol.MainChain.Get().ForkingPoint.Get().ID() if expectedChainID != actualChainID { return ierrors.Errorf("AssertChainID: %s: expected %s (index: %d), got %s (index: %d)", node.Name, expectedChainID, expectedChainID.Index(), actualChainID, actualChainID.Index()) }