Skip to content

Commit

Permalink
Refactor: intermediary commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Sep 12, 2023
1 parent 971410a commit 101a1bf
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 70 deletions.
6 changes: 3 additions & 3 deletions components/debugapi/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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")
}

Expand Down
36 changes: 11 additions & 25 deletions pkg/protocol/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)

Expand Down
50 changes: 19 additions & 31 deletions pkg/protocol/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type Chains struct {
protocol *Protocol

mainChain reactive.Variable[*Chain]
MainChain reactive.Variable[*Chain]

heaviestClaimedCandidate reactive.Variable[*Chain]

Expand All @@ -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](),
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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] {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/protocol/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ 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)
})
})

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)

Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/protocol/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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) })
})
}
2 changes: 1 addition & 1 deletion pkg/testsuite/chainmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/testsuite/mock/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/testsuite/storage_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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())
}
Expand Down

0 comments on commit 101a1bf

Please sign in to comment.