prysm-pulse/beacon-chain/core/altair/reward_test.go
terence tsao 5c36d59672
Reward: altair base reward (#9307)
* Add altair base reward funcs

* Update BUILD.bazel

* Better tests

* Update BUILD.bazel
2021-07-30 19:17:20 +00:00

194 lines
4.7 KiB
Go

package altair_test
import (
"math"
"testing"
types "github.com/prysmaticlabs/eth2-types"
altair "github.com/prysmaticlabs/prysm/beacon-chain/core/altair"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func Test_BaseReward(t *testing.T) {
genState := func(valCount uint64) state.ReadOnlyBeaconState {
s, _ := testutil.DeterministicGenesisStateAltair(t, valCount)
return s
}
tests := []struct {
name string
valIdx types.ValidatorIndex
st state.ReadOnlyBeaconState
want uint64
errString string
}{
{
name: "unknown validator",
valIdx: 2,
st: genState(1),
want: 0,
errString: "index 2 out of range",
},
{
name: "active balance is 32eth",
valIdx: 0,
st: genState(1),
want: 11448672,
errString: "",
},
{
name: "active balance is 32eth * target committee size",
valIdx: 0,
st: genState(params.BeaconConfig().TargetCommitteeSize),
want: 1011904,
errString: "",
},
{
name: "active balance is 32eth * max validator per committee size",
valIdx: 0,
st: genState(params.BeaconConfig().MaxValidatorsPerCommittee),
want: 252960,
errString: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := altair.BaseReward(tt.st, tt.valIdx)
if (err != nil) && (tt.errString != "") {
require.ErrorContains(t, tt.errString, err)
return
}
require.Equal(t, tt.want, got)
})
}
}
func Test_BaseRewardWithTotalBalance(t *testing.T) {
s, _ := testutil.DeterministicGenesisStateAltair(t, 1)
tests := []struct {
name string
valIdx types.ValidatorIndex
activeBalance uint64
want uint64
errString string
}{
{
name: "active balance is 0",
valIdx: 0,
activeBalance: 0,
want: 0,
errString: "active balance can't be 0",
},
{
name: "unknown validator",
valIdx: 2,
activeBalance: 1,
want: 0,
errString: "index 2 out of range",
},
{
name: "active balance is 1",
valIdx: 0,
activeBalance: 1,
want: 2048000000000,
errString: "",
},
{
name: "active balance is 1eth",
valIdx: 0,
activeBalance: params.BeaconConfig().EffectiveBalanceIncrement,
want: 64765024,
errString: "",
},
{
name: "active balance is 32eth",
valIdx: 0,
activeBalance: params.BeaconConfig().MaxEffectiveBalance,
want: 11448672,
errString: "",
},
{
name: "active balance is 32eth * 1m validators",
valIdx: 0,
activeBalance: params.BeaconConfig().MaxEffectiveBalance * 1e9,
want: 544,
errString: "",
},
{
name: "active balance is max uint64",
valIdx: 0,
activeBalance: math.MaxUint64,
want: 448,
errString: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := altair.BaseRewardWithTotalBalance(s, tt.valIdx, tt.activeBalance)
if (err != nil) && (tt.errString != "") {
require.ErrorContains(t, tt.errString, err)
return
}
require.Equal(t, tt.want, got)
})
}
}
func Test_BaseRewardPerIncrement(t *testing.T) {
tests := []struct {
name string
activeBalance uint64
want uint64
errString string
}{
{
name: "active balance is 0",
activeBalance: 0,
want: 0,
errString: "active balance can't be 0",
},
{
name: "active balance is 1",
activeBalance: 1,
want: 64000000000,
errString: "",
},
{
name: "active balance is 1eth",
activeBalance: params.BeaconConfig().EffectiveBalanceIncrement,
want: 2023907,
errString: "",
},
{
name: "active balance is 32eth",
activeBalance: params.BeaconConfig().MaxEffectiveBalance,
want: 357771,
errString: "",
},
{
name: "active balance is 32eth * 1m validators",
activeBalance: params.BeaconConfig().MaxEffectiveBalance * 1e9,
want: 17,
errString: "",
},
{
name: "active balance is max uint64",
activeBalance: math.MaxUint64,
want: 14,
errString: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := altair.BaseRewardPerIncrement(tt.activeBalance)
if (err != nil) && (tt.errString != "") {
require.ErrorContains(t, tt.errString, err)
return
}
require.Equal(t, tt.want, got)
})
}
}