Skip to content

Commit

Permalink
Merge pull request #894 from iotaledger/fix/increase_rew_precision
Browse files Browse the repository at this point in the history
use Safe64MulDiv for increased precision
  • Loading branch information
muXxer authored Mar 28, 2024
2 parents 96b3a2a + bc3b8f6 commit 737aa28
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,9 @@ func (t *Tracker) DelegatorReward(validatorID iotago.AccountID, delegatedAmount
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)
}

result, err = safemath.SafeDiv(result>>profitMarginExponent, uint64(rewardsForAccountInEpoch.PoolStake))
undecayedEpochRewards, err := safemath.Safe64MulDiv(result>>profitMarginExponent, uint64(delegatedAmount), uint64(rewardsForAccountInEpoch.PoolStake))
if err != nil {
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)
}

undecayedEpochRewards, err := safemath.SafeMul(result, uint64(delegatedAmount))
if err != nil {
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)
return 0, 0, 0, ierrors.Wrapf(err, "failed to calculate unDecayedEpochRewards due to overflow for epoch %d and validator accountID %s", epoch, validatorID)
}

decayProvider := t.apiProvider.APIForEpoch(epoch).ManaDecayProvider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/stretchr/testify/require"

"github.com/iotaledger/hive.go/core/safemath"

"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/kvstore/mapdb"
"github.com/iotaledger/hive.go/lo"
Expand Down Expand Up @@ -272,7 +274,10 @@ func (t *TestSuite) delegatorReward(epoch iotago.EpochIndex, profitMargin, poolR
if poolRewardWithFixedCost >= fixedCost {
poolRewards = poolRewardWithFixedCost - fixedCost
}
unDecayedEpochRewards := (((profitMarginComplement * poolRewards) >> profitMarginExponent) / poolStake) * delegatedAmount

result := (profitMarginComplement * poolRewards) >> profitMarginExponent
unDecayedEpochRewards, err := safemath.Safe64MulDiv(result, delegatedAmount, poolStake)
require.NoError(t.T, err)

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

0 comments on commit 737aa28

Please sign in to comment.