Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit dfc9a02

Browse files
committed
Feat: use protocol parameters
1 parent 26f6f6a commit dfc9a02

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

pkg/protocol/chains.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,4 @@ func increasing[T cmp.Ordered](currentValue T, newValue T) T {
171171
return max(currentValue, newValue)
172172
}
173173

174-
const (
175-
chainSwitchingThreshold iotago.SlotIndex = 3
176-
177-
chainSwitchingMeasurementOffset iotago.SlotIndex = 1
178-
)
174+
const chainSwitchingMeasurementOffset iotago.SlotIndex = 1

pkg/protocol/heaviest_chain_candidate.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ func newHeaviestChainCandidate(chains *Chains, weightVariable func(element *Comm
2727
}
2828

2929
func (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

Comments
 (0)