@@ -27,7 +27,9 @@ func newHeaviestChainCandidate(chains *Chains, weightVariable func(element *Comm
2727}
2828
2929func (h * HeaviestChainCandidate ) measureAt (slot iotago.SlotIndex ) (teardown func ()) {
30- if slot < chainSwitchingThreshold {
30+ chainSwitchingThreshold := h .chains .protocol .APIForSlot (slot ).ProtocolParameters ().ChainSwitchingThreshold ()
31+
32+ if slot < iotago .SlotIndex (chainSwitchingThreshold ) {
3133 return
3234 }
3335
@@ -40,18 +42,18 @@ func (h *HeaviestChainCandidate) measureAt(slot iotago.SlotIndex) (teardown func
4042 var teardownFunctions []func ()
4143
4244 if heaviestChain := heaviestCommitment .Chain .Get (); heaviestChain != h .chains .Main .Get () {
43- slotsWithHeaviestChain := reactive.NewCounter [* Commitment ](func (commitment * Commitment ) bool {
45+ slotsWithSameChain := reactive.NewCounter [* Commitment ](func (commitment * Commitment ) bool {
4446 return commitment .Chain .Get () == heaviestChain
4547 })
4648
47- for i := iotago . SlotIndex (1 ); i < chainSwitchingThreshold ; i ++ {
48- if earlierCommitments , earlierCommitmentsExist := h .weightedCommitmentsBySlot .Get (slot - i ); earlierCommitmentsExist {
49- teardownFunctions = append (teardownFunctions , slotsWithHeaviestChain .Monitor (earlierCommitments .HeaviestElement ()))
49+ for i := uint8 (1 ); i < chainSwitchingThreshold ; i ++ {
50+ if earlierCommitments , earlierCommitmentsExist := h .weightedCommitmentsBySlot .Get (slot - iotago . SlotIndex ( i ) ); earlierCommitmentsExist {
51+ teardownFunctions = append (teardownFunctions , slotsWithSameChain .Monitor (earlierCommitments .HeaviestElement ()))
5052 }
5153 }
5254
53- teardownFunctions = append (teardownFunctions , slotsWithHeaviestChain .OnUpdate (func (_ int , slotsWithHeaviestChain int ) {
54- if iotago . SlotIndex ( slotsWithHeaviestChain ) >= chainSwitchingThreshold - 1 {
55+ teardownFunctions = append (teardownFunctions , slotsWithSameChain .OnUpdate (func (_ int , slotsWithSameChain int ) {
56+ if slotsWithSameChain >= int ( chainSwitchingThreshold ) - 1 {
5557 h .Set (heaviestChain )
5658 } else {
5759 h .Set (nil )
0 commit comments