Skip to content

Commit

Permalink
Fix: Tiebreaker for chains with equal weight
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Apr 25, 2024
1 parent e0c5e67 commit 3e2e5f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
15 changes: 15 additions & 0 deletions pkg/protocol/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ 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 Down Expand Up @@ -86,6 +89,7 @@ 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 @@ -230,6 +234,8 @@ func (c *Chain) initDerivedProperties() (shutdown func()) {
parentChain.deriveChildChains(c),

c.deriveShouldEvict(forkingPoint, parentChain),

c.deriveIsSolid(forkingPoint, parentChain),
)
}),
)
Expand Down Expand Up @@ -264,6 +270,15 @@ 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 Down
22 changes: 12 additions & 10 deletions pkg/protocol/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,21 @@ func (c *Chains) initChainSwitching() (shutdown func()) {

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

if evictionEvent := c.protocol.EvictionEvent(targetSlot); !evictionEvent.WasTriggered() {
c.HeaviestClaimedCandidate.registerCommitment(targetSlot, latestCommitment, evictionEvent)
if evictionEvent := c.protocol.EvictionEvent(targetSlot); !evictionEvent.WasTriggered() {
c.HeaviestClaimedCandidate.registerCommitment(targetSlot, latestCommitment, evictionEvent)

latestCommitment.IsAttested.OnTrigger(func() {
c.HeaviestAttestedCandidate.registerCommitment(targetSlot, latestCommitment, evictionEvent)
})
latestCommitment.IsAttested.OnTrigger(func() {
c.HeaviestAttestedCandidate.registerCommitment(targetSlot, latestCommitment, evictionEvent)
})

latestCommitment.IsVerified.OnTrigger(func() {
c.HeaviestVerifiedCandidate.registerCommitment(targetSlot, latestCommitment, evictionEvent)
})
}
latestCommitment.IsVerified.OnTrigger(func() {
c.HeaviestVerifiedCandidate.registerCommitment(targetSlot, latestCommitment, evictionEvent)
})
}
})
})
}

Expand Down

0 comments on commit 3e2e5f3

Please sign in to comment.