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

Commit 737aa28

Browse files
authored
Merge pull request #894 from iotaledger/fix/increase_rew_precision
use Safe64MulDiv for increased precision
2 parents 96b3a2a + bc3b8f6 commit 737aa28

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

pkg/protocol/sybilprotection/sybilprotectionv1/performance/rewards.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,9 @@ func (t *Tracker) DelegatorReward(validatorID iotago.AccountID, delegatedAmount
185185
return 0, 0, 0, ierrors.Wrapf(err, "failed to multiply profitMarginComplement and poolReward for unDecayedEpochRewards due to overflow for epoch %d and validator accountID %s", epoch, validatorID)
186186
}
187187

188-
result, err = safemath.SafeDiv(result>>profitMarginExponent, uint64(rewardsForAccountInEpoch.PoolStake))
188+
undecayedEpochRewards, err := safemath.Safe64MulDiv(result>>profitMarginExponent, uint64(delegatedAmount), uint64(rewardsForAccountInEpoch.PoolStake))
189189
if err != nil {
190-
return 0, 0, 0, ierrors.Wrapf(err, "failed to divide by PoolStake for unDecayedEpochRewards due to overflow for epoch %d and validator accountID %s", epoch, validatorID)
191-
}
192-
193-
undecayedEpochRewards, err := safemath.SafeMul(result, uint64(delegatedAmount))
194-
if err != nil {
195-
return 0, 0, 0, ierrors.Wrapf(err, "failed to multiply by delegatedAmmount for unDecayedEpochRewards due to overflow for epoch %d and validator accountID %s", epoch, validatorID)
190+
return 0, 0, 0, ierrors.Wrapf(err, "failed to calculate unDecayedEpochRewards due to overflow for epoch %d and validator accountID %s", epoch, validatorID)
196191
}
197192

198193
decayProvider := t.apiProvider.APIForEpoch(epoch).ManaDecayProvider()

pkg/protocol/sybilprotection/sybilprotectionv1/performance/testsuite_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"github.com/stretchr/testify/require"
99

10+
"github.com/iotaledger/hive.go/core/safemath"
11+
1012
"github.com/iotaledger/hive.go/kvstore"
1113
"github.com/iotaledger/hive.go/kvstore/mapdb"
1214
"github.com/iotaledger/hive.go/lo"
@@ -272,7 +274,10 @@ func (t *TestSuite) delegatorReward(epoch iotago.EpochIndex, profitMargin, poolR
272274
if poolRewardWithFixedCost >= fixedCost {
273275
poolRewards = poolRewardWithFixedCost - fixedCost
274276
}
275-
unDecayedEpochRewards := (((profitMarginComplement * poolRewards) >> profitMarginExponent) / poolStake) * delegatedAmount
277+
278+
result := (profitMarginComplement * poolRewards) >> profitMarginExponent
279+
unDecayedEpochRewards, err := safemath.Safe64MulDiv(result, delegatedAmount, poolStake)
280+
require.NoError(t.T, err)
276281

277282
decayProvider := t.api.ManaDecayProvider()
278283
decayedEpochRewards, err := decayProvider.DecayManaByEpochs(iotago.Mana(unDecayedEpochRewards), epoch, epoch)

0 commit comments

Comments
 (0)