From 984221be4b34a26c789f631248480697c7263265 Mon Sep 17 00:00:00 2001 From: Alexander Sporn Date: Tue, 14 May 2024 21:15:12 +0200 Subject: [PATCH] Fixed pool rewards not getting exported when creating a snapshot with targetEpoch 0 --- .../sybilprotectionv1/performance/snapshot.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/snapshot.go b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/snapshot.go index 5113989a5..29639d3b3 100644 --- a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/snapshot.go +++ b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/snapshot.go @@ -293,9 +293,9 @@ func (t *Tracker) exportPoolRewards(writer io.WriteSeeker, targetEpoch iotago.Ep if err := stream.WriteCollection(writer, serializer.SeriLengthPrefixTypeAsUint32, func() (int, error) { var epochCount int - // Here underflow will not happen because we will stop iterating for epoch 0, because 0 is not greater than zero. - // Use safemath here anyway to avoid hard to trace problems stemming from an accidental underflow. - for epoch := targetEpoch; epoch > earliestRewardEpoch; epoch = lo.PanicOnErr(safemath.SafeSub(epoch, 1)) { + // Start at the targest epoch and go back in time until earliestRewardEpoch or epoch 0 (included) + epoch := targetEpoch + for { rewardsMap, err := t.rewardsMap(epoch) if err != nil { return 0, ierrors.Wrapf(err, "unable to get rewards tree for epoch %d", epoch) @@ -339,6 +339,12 @@ func (t *Tracker) exportPoolRewards(writer io.WriteSeeker, targetEpoch iotago.Ep } epochCount++ + + if epoch <= earliestRewardEpoch { + // Every reward before earliestRewardEpoch is already exported, so stop here + break + } + epoch = lo.PanicOnErr(safemath.SafeSub(epoch, 1)) } return epochCount, nil