From 010e1038786f140aec754691bc9198a363dcae63 Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Sun, 3 Dec 2023 23:18:11 +0100 Subject: [PATCH] Refactor: reverted more changes --- pkg/protocol/chain.go | 51 ++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/pkg/protocol/chain.go b/pkg/protocol/chain.go index 08091dc1d..fc9ac9c01 100644 --- a/pkg/protocol/chain.go +++ b/pkg/protocol/chain.go @@ -209,21 +209,10 @@ func (c *Chain) initLogger() (shutdown func()) { // initDerivedProperties initializes the behavior of this chain by setting up the relations between its properties. func (c *Chain) initDerivedProperties() (shutdown func()) { return lo.Batch( - c.ClaimedWeight.DeriveValueFrom(reactive.NewDerivedVariable(func(_ uint64, latestCommitment *Commitment) uint64 { - return latestCommitment.cumulativeWeight() - }, c.LatestCommitment)), - - c.VerifiedWeight.DeriveValueFrom(reactive.NewDerivedVariable(func(_ uint64, latestProducedCommitment *Commitment) uint64 { - return latestProducedCommitment.cumulativeWeight() - }, c.LatestProducedCommitment)), - - c.WarpSyncMode.DeriveValueFrom(reactive.NewDerivedVariable3(func(enabled bool, latestFullyBookedSlot iotago.SlotIndex, latestSeenSlot iotago.SlotIndex, outOfSyncThreshold iotago.SlotIndex) bool { - return warpSyncModeEnabled(enabled, latestFullyBookedSlot, latestSeenSlot, outOfSyncThreshold) - }, c.LatestFullyBookedSlot, c.chains.LatestSeenSlot, c.OutOfSyncThreshold, c.WarpSyncMode.Get())), - - c.LatestAttestedCommitment.WithNonEmptyValue(func(latestAttestedCommitment *Commitment) (shutdown func()) { - return c.AttestedWeight.InheritFrom(latestAttestedCommitment.CumulativeAttestedWeight) - }), + c.deriveClaimedWeight(), + c.deriveVerifiedWeight(), + c.deriveLatestAttestedWeight(), + c.deriveWarpSyncMode(), c.ForkingPoint.WithValue(func(forkingPoint *Commitment) (shutdown func()) { return c.deriveParentChain(forkingPoint) @@ -241,6 +230,38 @@ 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) + }, 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() + }, c.LatestCommitment)) +} + +// deriveLatestAttestedWeight defines how a chain determines its attested weight (by inheriting the cumulative attested +// weight of the latest attested commitment). It uses inheritance instead of simply setting the value as the cumulative +// attested weight can change over time depending on the attestations that are received. +func (c *Chain) deriveLatestAttestedWeight() func() { + return c.LatestAttestedCommitment.WithNonEmptyValue(func(latestAttestedCommitment *Commitment) (shutdown func()) { + return c.AttestedWeight.InheritFrom(latestAttestedCommitment.CumulativeAttestedWeight) + }) +} + +// deriveVerifiedWeight defines how a chain determines its verified weight (by setting the cumulative weight of the +// latest produced commitment). +func (c *Chain) deriveVerifiedWeight() func() { + return c.VerifiedWeight.DeriveValueFrom(reactive.NewDerivedVariable(func(_ uint64, latestProducedCommitment *Commitment) uint64 { + return latestProducedCommitment.cumulativeWeight() + }, c.LatestProducedCommitment)) +} + // deriveChildChains defines how a chain determines its ChildChains (by adding each child to the set). func (c *Chain) deriveChildChains(child *Chain) func() { c.ChildChains.Add(child)