Skip to content

Commit

Permalink
Improved tests to check specification formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
tdahar committed Oct 27, 2022
1 parent a9b8770 commit e1c9fbf
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions pkg/fork_metrics/rewards_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fork_metrics

import (
"math"
"testing"

"github.com/attestantio/go-eth2-client/spec/altair"
Expand Down Expand Up @@ -72,18 +73,57 @@ func TestMaxAttestationReward(t *testing.T) {
state,
stateNext)

baseRewardPerInc := uint64(fork_state.EFFECTIVE_BALANCE_INCREMENT * fork_state.BASE_REWARD_FACTOR)
baseRewardPerInc = baseRewardPerInc / uint64(math.Sqrt(float64(rewardsObj.CurrentState.TotalActiveBalance)))
require.Equal(t,
rewardsObj.GetBaseRewardPerInc(rewardsObj.CurrentState.TotalActiveBalance),
uint64(254982))
uint64(baseRewardPerInc))

require.Equal(t,
rewardsObj.GetBaseReward(1, uint64(validator1.EffectiveBalance), rewardsObj.CurrentState.TotalActiveBalance),
uint64(254982*32))
rewardsObj.GetBaseReward(0, uint64(validator1.EffectiveBalance), rewardsObj.CurrentState.TotalActiveBalance),
uint64(baseRewardPerInc*32))

require.Equal(t,
rewardsObj.GetBaseReward(1, uint64(validator2.EffectiveBalance), rewardsObj.CurrentState.TotalActiveBalance),
uint64(baseRewardPerInc*31))

attReward := 0

// Source
reward := uint64(14) * baseRewardPerInc * 31 * uint64(rewardsObj.CurrentState.AttestingBalance[0]/1000000000)
reward = reward / (uint64(rewardsObj.CurrentState.TotalActiveBalance / 1000000000)) / 64
attReward += int(reward)

// Target
reward = uint64(26) * baseRewardPerInc * 31 * uint64(rewardsObj.CurrentState.AttestingBalance[1]/1000000000)
reward = reward / (uint64(rewardsObj.CurrentState.TotalActiveBalance / 1000000000)) / 64
attReward += int(reward)

// Head
reward = uint64(14) * baseRewardPerInc * 31 * uint64(rewardsObj.CurrentState.AttestingBalance[2]/1000000000)
reward = reward / (uint64(rewardsObj.CurrentState.TotalActiveBalance / 1000000000)) / 64
attReward += int(reward)

require.Equal(t,
rewardsObj.GetMaxAttestationReward(1),
uint64(2509346))
uint64(attReward))

attReward = 0

// Source
reward = uint64(14) * baseRewardPerInc * 32 * uint64(rewardsObj.CurrentState.AttestingBalance[0]/1000000000)
reward = reward / (uint64(rewardsObj.CurrentState.TotalActiveBalance / 1000000000)) / 64
attReward += int(reward)

// Target
reward = uint64(26) * baseRewardPerInc * 32 * uint64(rewardsObj.CurrentState.AttestingBalance[1]/1000000000)
reward = reward / (uint64(rewardsObj.CurrentState.TotalActiveBalance / 1000000000)) / 64
attReward += int(reward)

// Head
reward = uint64(14) * baseRewardPerInc * 32 * uint64(rewardsObj.CurrentState.AttestingBalance[2]/1000000000)
reward = reward / (uint64(rewardsObj.CurrentState.TotalActiveBalance / 1000000000)) / 64
attReward += int(reward)
require.Equal(t,
rewardsObj.GetMaxAttestationReward(0),
uint64(2590292))
Expand Down Expand Up @@ -137,18 +177,25 @@ func TestMaxSyncCommitteeReward(t *testing.T) {
fork_state.ForkStateContentBase{},
fork_state.ForkStateContentBase{})

baseRewardPerInc := uint64(fork_state.EFFECTIVE_BALANCE_INCREMENT * fork_state.BASE_REWARD_FACTOR)
baseRewardPerInc = baseRewardPerInc / uint64(math.Sqrt(float64(rewardsObj.NextState.TotalActiveBalance)))

participantReward := uint64(rewardsObj.NextState.TotalActiveBalance / 1000000000)
participantReward = participantReward * baseRewardPerInc
participantReward = participantReward * 2 / 64 / 32
participantReward = participantReward / 512
require.Equal(t,
rewardsObj.GetMaxSyncComReward(0),
uint64(30*32))
uint64(participantReward*32))

require.Equal(t,
rewardsObj.GetMaxSyncComReward(1),
uint64(0*32))
uint64(0)) // not in sync committee

rewardsObj.NextState.MissedBlocks = append(rewardsObj.NextState.MissedBlocks, 1)

require.Equal(t,
rewardsObj.GetMaxSyncComReward(0),
uint64(30*31))
uint64(participantReward*31)) // one missed block

}

0 comments on commit e1c9fbf

Please sign in to comment.