Skip to content

Commit

Permalink
Refactor: minimizing more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Dec 3, 2023
1 parent 010e103 commit a3a5c72
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
33 changes: 18 additions & 15 deletions pkg/protocol/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,26 @@ func (c *Chain) initDerivedProperties() (shutdown func()) {

// deriveWarpSyncMode defines how a chain determines whether it is in warp sync mode or not.
func (c *Chain) deriveWarpSyncMode() func() {
return c.WarpSyncMode.DeriveValueFrom(reactive.NewDerivedVariable3(func(enabled bool, latestFullyBookedSlot iotago.SlotIndex, latestSeenSlot iotago.SlotIndex, outOfSyncThreshold iotago.SlotIndex) bool {
return warpSyncModeEnabled(enabled, latestFullyBookedSlot, latestSeenSlot, outOfSyncThreshold)
return c.WarpSyncMode.DeriveValueFrom(reactive.NewDerivedVariable3(func(warpSyncMode bool, latestFullyBookedSlot iotago.SlotIndex, latestSeenSlot iotago.SlotIndex, outOfSyncThreshold iotago.SlotIndex) bool {
// if warp sync mode is enabled, keep it enabled until we have fully booked all slots
if warpSyncMode {
return latestFullyBookedSlot < latestSeenSlot
}

// if warp sync mode is disabled, enable it only if we fall below the out of sync threshold
return latestFullyBookedSlot < outOfSyncThreshold
}, c.LatestFullyBookedSlot, c.chains.LatestSeenSlot, c.OutOfSyncThreshold, c.WarpSyncMode.Get()))
}

// deriveClaimedWeight defines how a chain determines its claimed weight (by setting the cumulative weight of the
// latest commitment).
func (c *Chain) deriveClaimedWeight() func() {
return c.ClaimedWeight.DeriveValueFrom(reactive.NewDerivedVariable(func(_ uint64, latestCommitment *Commitment) uint64 {
return latestCommitment.cumulativeWeight()
if latestCommitment == nil {
return 0
}

return latestCommitment.CumulativeWeight()
}, c.LatestCommitment))
}

Expand All @@ -258,7 +268,11 @@ func (c *Chain) deriveLatestAttestedWeight() func() {
// latest produced commitment).
func (c *Chain) deriveVerifiedWeight() func() {
return c.VerifiedWeight.DeriveValueFrom(reactive.NewDerivedVariable(func(_ uint64, latestProducedCommitment *Commitment) uint64 {
return latestProducedCommitment.cumulativeWeight()
if latestProducedCommitment == nil {
return 0
}

return latestProducedCommitment.CumulativeWeight()
}, c.LatestProducedCommitment))
}

Expand Down Expand Up @@ -366,14 +380,3 @@ func outOfSyncThreshold(engineInstance *engine.Engine, latestSeenSlot iotago.Slo

return 0
}

// warpSyncModeEnabled determines whether warp sync mode should be enabled or not.
func warpSyncModeEnabled(enabled bool, latestFullyBookedSlot iotago.SlotIndex, latestSeenSlot iotago.SlotIndex, outOfSyncThreshold iotago.SlotIndex) bool {
// if warp sync mode is enabled, keep it enabled until we are no longer below the warp sync threshold
if enabled {
return latestFullyBookedSlot < latestSeenSlot
}

// if warp sync mode is disabled, enable it only if we fall below the out of sync threshold
return latestFullyBookedSlot < outOfSyncThreshold
}
9 changes: 0 additions & 9 deletions pkg/protocol/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,3 @@ func (c *Commitment) forceChain(targetChain *Chain) {
}
}
}

// cumulativeWeight returns the cumulative weight of this Commitment while gracefully handling nil receivers.
func (c *Commitment) cumulativeWeight() uint64 {
if c == nil {
return 0
}

return c.CumulativeWeight()
}

0 comments on commit a3a5c72

Please sign in to comment.