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

Commit 53d8886

Browse files
Merge pull request #530 from iotaledger/addRewardsTests
includes a test comparing rewards to float results
2 parents e0e827a + 05841dd commit 53d8886

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,14 @@ func (t *TestSuite) ApplyEpochActions(epoch iotago.EpochIndex, actions map[strin
155155
for alias, action := range actions {
156156
epochPerformanceFactor := action.SlotPerformance * action.ActiveSlotsCount >> t.api.ProtocolParameters().SlotsPerEpochExponent()
157157
poolRewards := t.calculatePoolReward(epoch, totalValidatorsStake, totalStake, action.PoolStake, action.ValidatorStake, epochPerformanceFactor)
158+
poolRewardsFloat := t.calculatePoolRewardFloat(epoch, totalValidatorsStake, totalStake, action.PoolStake, action.ValidatorStake, epochPerformanceFactor)
158159
t.poolRewards[epoch][alias] = &model.PoolRewards{
159160
PoolStake: action.PoolStake,
160161
PoolRewards: iotago.Mana(poolRewards),
161162
FixedCost: action.FixedCost,
162163
}
164+
165+
require.InEpsilon(t.T, poolRewards, poolRewardsFloat, 0.01)
163166
}
164167
}
165168

@@ -284,14 +287,29 @@ func (t *TestSuite) calculatePoolReward(epoch iotago.EpochIndex, totalValidators
284287
params := t.api.ProtocolParameters()
285288
targetReward, err := params.RewardsParameters().TargetReward(epoch, t.api)
286289
require.NoError(t.T, err)
287-
288290
poolCoefficient := t.calculatePoolCoefficient(poolStake, totalStake, validatorStake, totalValidatorsStake)
289291
scaledPoolReward := poolCoefficient * uint64(targetReward) * performanceFactor
290292
poolRewardNoFixedCost := scaledPoolReward / uint64(params.ValidationBlocksPerSlot()) >> (params.RewardsParameters().PoolCoefficientExponent + 1)
291293

292294
return poolRewardNoFixedCost
293295
}
294296

297+
func (t *TestSuite) calculatePoolRewardFloat(epoch iotago.EpochIndex, totalValidatorsStake, totalStake, poolStake, validatorStake iotago.BaseToken, performanceFactor uint64) float64 {
298+
params := t.api.ProtocolParameters()
299+
totalValidatorsStakeFloat := float64(totalValidatorsStake)
300+
totalStakeFloat := float64(totalStake)
301+
poolStakeFloat := float64(poolStake)
302+
validatorStakeFloat := float64(validatorStake)
303+
performanceFactorFloat := float64(performanceFactor) / float64(params.ValidationBlocksPerSlot())
304+
305+
targetReward, _ := params.RewardsParameters().TargetReward(epoch, t.api)
306+
targetRewardFloat := float64(targetReward)
307+
308+
poolCoefficientFloat := (poolStakeFloat/totalStakeFloat + validatorStakeFloat/totalValidatorsStakeFloat) / 2.0
309+
PoolRewardFloat := poolCoefficientFloat * targetRewardFloat * performanceFactorFloat
310+
return PoolRewardFloat
311+
}
312+
295313
func (t *TestSuite) calculatePoolCoefficient(poolStake, totalStake, validatorStake, totalValidatorStake iotago.BaseToken) uint64 {
296314
poolCoeffExponent := t.api.ProtocolParameters().RewardsParameters().PoolCoefficientExponent
297315
poolCoeff := (poolStake<<poolCoeffExponent)/totalStake +

0 commit comments

Comments
 (0)