mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 21:08:10 -05:00
* Ran gopls modernize to fix everything go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Override rules_go provided dependency for golang.org/x/tools to v0.38.0. To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches. * Fix buildtag violations and ignore buildtag violations in external * Introduce modernize analyzer package. * Add modernize "any" analyzer. * Fix violations of any analyzer * Add modernize "appendclipped" analyzer. * Fix violations of appendclipped * Add modernize "bloop" analyzer. * Add modernize "fmtappendf" analyzer. * Add modernize "forvar" analyzer. * Add modernize "mapsloop" analyzer. * Add modernize "minmax" analyzer. * Fix violations of minmax analyzer * Add modernize "omitzero" analyzer. * Add modernize "rangeint" analyzer. * Fix violations of rangeint. * Add modernize "reflecttypefor" analyzer. * Fix violations of reflecttypefor analyzer. * Add modernize "slicescontains" analyzer. * Add modernize "slicessort" analyzer. * Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686. * Add modernize "stringscutprefix" analyzer. * Add modernize "stringsbuilder" analyzer. * Fix violations of stringsbuilder analyzer. * Add modernize "stringsseq" analyzer. * Add modernize "testingcontext" analyzer. * Add modernize "waitgroup" analyzer. * Changelog fragment * gofmt * gazelle * Add modernize "newexpr" analyzer. * Disable newexpr until go1.26 * Add more details in WORKSPACE on how to update the override * @nalepae feedback on min() * gofmt * Fix violations of forvar
327 lines
11 KiB
Go
327 lines
11 KiB
Go
package helpers_test
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
|
|
"github.com/OffchainLabs/prysm/v7/beacon-chain/core/helpers"
|
|
"github.com/OffchainLabs/prysm/v7/beacon-chain/core/time"
|
|
state_native "github.com/OffchainLabs/prysm/v7/beacon-chain/state/state-native"
|
|
"github.com/OffchainLabs/prysm/v7/config/params"
|
|
"github.com/OffchainLabs/prysm/v7/consensus-types/primitives"
|
|
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
|
|
"github.com/OffchainLabs/prysm/v7/testing/assert"
|
|
"github.com/OffchainLabs/prysm/v7/testing/require"
|
|
)
|
|
|
|
func TestTotalBalance_OK(t *testing.T) {
|
|
helpers.ClearCache()
|
|
|
|
state, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{Validators: []*ethpb.Validator{
|
|
{EffectiveBalance: 27 * 1e9}, {EffectiveBalance: 28 * 1e9},
|
|
{EffectiveBalance: 32 * 1e9}, {EffectiveBalance: 40 * 1e9},
|
|
}})
|
|
require.NoError(t, err)
|
|
|
|
balance := helpers.TotalBalance(state, []primitives.ValidatorIndex{0, 1, 2, 3})
|
|
wanted := state.Validators()[0].EffectiveBalance + state.Validators()[1].EffectiveBalance +
|
|
state.Validators()[2].EffectiveBalance + state.Validators()[3].EffectiveBalance
|
|
assert.Equal(t, wanted, balance, "Incorrect TotalBalance")
|
|
}
|
|
|
|
func TestTotalBalance_ReturnsEffectiveBalanceIncrement(t *testing.T) {
|
|
helpers.ClearCache()
|
|
|
|
state, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{Validators: []*ethpb.Validator{}})
|
|
require.NoError(t, err)
|
|
|
|
balance := helpers.TotalBalance(state, []primitives.ValidatorIndex{})
|
|
wanted := params.BeaconConfig().EffectiveBalanceIncrement
|
|
assert.Equal(t, wanted, balance, "Incorrect TotalBalance")
|
|
}
|
|
|
|
func TestGetBalance_OK(t *testing.T) {
|
|
tests := []struct {
|
|
i uint64
|
|
b []uint64
|
|
}{
|
|
{i: 0, b: []uint64{27 * 1e9, 28 * 1e9, 32 * 1e9}},
|
|
{i: 1, b: []uint64{27 * 1e9, 28 * 1e9, 32 * 1e9}},
|
|
{i: 2, b: []uint64{27 * 1e9, 28 * 1e9, 32 * 1e9}},
|
|
{i: 0, b: []uint64{0, 0, 0}},
|
|
{i: 2, b: []uint64{0, 0, 0}},
|
|
}
|
|
for _, test := range tests {
|
|
helpers.ClearCache()
|
|
|
|
state, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{Balances: test.b})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, test.b[test.i], state.Balances()[test.i], "Incorrect Validator balance")
|
|
}
|
|
}
|
|
|
|
func TestTotalActiveBalance(t *testing.T) {
|
|
tests := []struct {
|
|
vCount int
|
|
}{
|
|
{1},
|
|
{10},
|
|
{10000},
|
|
}
|
|
for _, test := range tests {
|
|
helpers.ClearCache()
|
|
|
|
validators := make([]*ethpb.Validator, 0)
|
|
for i := 0; i < test.vCount; i++ {
|
|
validators = append(validators, ðpb.Validator{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, ExitEpoch: 1})
|
|
}
|
|
state, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{Validators: validators})
|
|
require.NoError(t, err)
|
|
bal, err := helpers.TotalActiveBalance(state)
|
|
require.NoError(t, err)
|
|
require.Equal(t, uint64(test.vCount)*params.BeaconConfig().MaxEffectiveBalance, bal)
|
|
}
|
|
}
|
|
|
|
func TestTotalActiveBal_ReturnMin(t *testing.T) {
|
|
tests := []struct {
|
|
vCount int
|
|
}{
|
|
{1},
|
|
{10},
|
|
{10000},
|
|
}
|
|
for _, test := range tests {
|
|
helpers.ClearCache()
|
|
|
|
validators := make([]*ethpb.Validator, 0)
|
|
for i := 0; i < test.vCount; i++ {
|
|
validators = append(validators, ðpb.Validator{EffectiveBalance: 1, ExitEpoch: 1})
|
|
}
|
|
state, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{Validators: validators})
|
|
require.NoError(t, err)
|
|
bal, err := helpers.TotalActiveBalance(state)
|
|
require.NoError(t, err)
|
|
require.Equal(t, params.BeaconConfig().EffectiveBalanceIncrement, bal)
|
|
}
|
|
}
|
|
|
|
func TestTotalActiveBalance_WithCache(t *testing.T) {
|
|
tests := []struct {
|
|
vCount int
|
|
wantCount int
|
|
}{
|
|
{vCount: 1, wantCount: 1},
|
|
{vCount: 10, wantCount: 10},
|
|
{vCount: 10000, wantCount: 10000},
|
|
}
|
|
for _, test := range tests {
|
|
helpers.ClearCache()
|
|
|
|
validators := make([]*ethpb.Validator, 0)
|
|
for i := 0; i < test.vCount; i++ {
|
|
validators = append(validators, ðpb.Validator{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, ExitEpoch: 1})
|
|
}
|
|
state, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{Validators: validators})
|
|
require.NoError(t, err)
|
|
bal, err := helpers.TotalActiveBalance(state)
|
|
require.NoError(t, err)
|
|
require.Equal(t, uint64(test.wantCount)*params.BeaconConfig().MaxEffectiveBalance, bal)
|
|
}
|
|
}
|
|
|
|
func TestIncreaseBalance_OK(t *testing.T) {
|
|
tests := []struct {
|
|
i primitives.ValidatorIndex
|
|
b []uint64
|
|
nb uint64
|
|
eb uint64
|
|
}{
|
|
{i: 0, b: []uint64{27 * 1e9, 28 * 1e9, 32 * 1e9}, nb: 1, eb: 27*1e9 + 1},
|
|
{i: 1, b: []uint64{27 * 1e9, 28 * 1e9, 32 * 1e9}, nb: 0, eb: 28 * 1e9},
|
|
{i: 2, b: []uint64{27 * 1e9, 28 * 1e9, 32 * 1e9}, nb: 33 * 1e9, eb: 65 * 1e9},
|
|
}
|
|
for _, test := range tests {
|
|
helpers.ClearCache()
|
|
|
|
state, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{
|
|
Validators: []*ethpb.Validator{
|
|
{EffectiveBalance: 4}, {EffectiveBalance: 4}, {EffectiveBalance: 4}},
|
|
Balances: test.b,
|
|
})
|
|
require.NoError(t, err)
|
|
require.NoError(t, helpers.IncreaseBalance(state, test.i, test.nb))
|
|
assert.Equal(t, test.eb, state.Balances()[test.i], "Incorrect Validator balance")
|
|
}
|
|
}
|
|
|
|
func TestDecreaseBalance_OK(t *testing.T) {
|
|
tests := []struct {
|
|
i primitives.ValidatorIndex
|
|
b []uint64
|
|
nb uint64
|
|
eb uint64
|
|
}{
|
|
{i: 0, b: []uint64{2, 28 * 1e9, 32 * 1e9}, nb: 1, eb: 1},
|
|
{i: 1, b: []uint64{27 * 1e9, 28 * 1e9, 32 * 1e9}, nb: 0, eb: 28 * 1e9},
|
|
{i: 2, b: []uint64{27 * 1e9, 28 * 1e9, 1}, nb: 2, eb: 0},
|
|
{i: 3, b: []uint64{27 * 1e9, 28 * 1e9, 1, 28 * 1e9}, nb: 28 * 1e9, eb: 0},
|
|
}
|
|
for _, test := range tests {
|
|
helpers.ClearCache()
|
|
|
|
state, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{
|
|
Validators: []*ethpb.Validator{
|
|
{EffectiveBalance: 4}, {EffectiveBalance: 4}, {EffectiveBalance: 4}, {EffectiveBalance: 3}},
|
|
Balances: test.b,
|
|
})
|
|
require.NoError(t, err)
|
|
require.NoError(t, helpers.DecreaseBalance(state, test.i, test.nb))
|
|
assert.Equal(t, test.eb, state.Balances()[test.i], "Incorrect Validator balance")
|
|
}
|
|
}
|
|
|
|
func TestFinalityDelay(t *testing.T) {
|
|
helpers.ClearCache()
|
|
|
|
base := buildState(params.BeaconConfig().SlotsPerEpoch*10, 1)
|
|
base.FinalizedCheckpoint = ðpb.Checkpoint{Epoch: 3}
|
|
beaconState, err := state_native.InitializeFromProtoPhase0(base)
|
|
require.NoError(t, err)
|
|
prevEpoch := primitives.Epoch(0)
|
|
finalizedEpoch := primitives.Epoch(0)
|
|
// Set values for each test case
|
|
setVal := func() {
|
|
prevEpoch = time.PrevEpoch(beaconState)
|
|
finalizedEpoch = beaconState.FinalizedCheckpointEpoch()
|
|
}
|
|
setVal()
|
|
d := helpers.FinalityDelay(prevEpoch, finalizedEpoch)
|
|
w := time.PrevEpoch(beaconState) - beaconState.FinalizedCheckpointEpoch()
|
|
assert.Equal(t, w, d, "Did not get wanted finality delay")
|
|
|
|
require.NoError(t, beaconState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 4}))
|
|
setVal()
|
|
d = helpers.FinalityDelay(prevEpoch, finalizedEpoch)
|
|
w = time.PrevEpoch(beaconState) - beaconState.FinalizedCheckpointEpoch()
|
|
assert.Equal(t, w, d, "Did not get wanted finality delay")
|
|
|
|
require.NoError(t, beaconState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 5}))
|
|
setVal()
|
|
d = helpers.FinalityDelay(prevEpoch, finalizedEpoch)
|
|
w = time.PrevEpoch(beaconState) - beaconState.FinalizedCheckpointEpoch()
|
|
assert.Equal(t, w, d, "Did not get wanted finality delay")
|
|
}
|
|
|
|
func TestIsInInactivityLeak(t *testing.T) {
|
|
helpers.ClearCache()
|
|
|
|
base := buildState(params.BeaconConfig().SlotsPerEpoch*10, 1)
|
|
base.FinalizedCheckpoint = ðpb.Checkpoint{Epoch: 3}
|
|
beaconState, err := state_native.InitializeFromProtoPhase0(base)
|
|
require.NoError(t, err)
|
|
prevEpoch := primitives.Epoch(0)
|
|
finalizedEpoch := primitives.Epoch(0)
|
|
// Set values for each test case
|
|
setVal := func() {
|
|
prevEpoch = time.PrevEpoch(beaconState)
|
|
finalizedEpoch = beaconState.FinalizedCheckpointEpoch()
|
|
}
|
|
setVal()
|
|
assert.Equal(t, true, helpers.IsInInactivityLeak(prevEpoch, finalizedEpoch), "Wanted inactivity leak true")
|
|
require.NoError(t, beaconState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 4}))
|
|
setVal()
|
|
assert.Equal(t, true, helpers.IsInInactivityLeak(prevEpoch, finalizedEpoch), "Wanted inactivity leak true")
|
|
require.NoError(t, beaconState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 5}))
|
|
setVal()
|
|
assert.Equal(t, false, helpers.IsInInactivityLeak(prevEpoch, finalizedEpoch), "Wanted inactivity leak false")
|
|
}
|
|
|
|
func buildState(slot primitives.Slot, validatorCount uint64) *ethpb.BeaconState {
|
|
validators := make([]*ethpb.Validator, validatorCount)
|
|
for i := range validators {
|
|
validators[i] = ðpb.Validator{
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
|
|
}
|
|
}
|
|
validatorBalances := make([]uint64, len(validators))
|
|
for i := range validatorBalances {
|
|
validatorBalances[i] = params.BeaconConfig().MaxEffectiveBalance
|
|
}
|
|
latestActiveIndexRoots := make(
|
|
[][]byte,
|
|
params.BeaconConfig().EpochsPerHistoricalVector,
|
|
)
|
|
for i := range latestActiveIndexRoots {
|
|
latestActiveIndexRoots[i] = params.BeaconConfig().ZeroHash[:]
|
|
}
|
|
latestRandaoMixes := make(
|
|
[][]byte,
|
|
params.BeaconConfig().EpochsPerHistoricalVector,
|
|
)
|
|
for i := range latestRandaoMixes {
|
|
latestRandaoMixes[i] = params.BeaconConfig().ZeroHash[:]
|
|
}
|
|
return ðpb.BeaconState{
|
|
Slot: slot,
|
|
Balances: validatorBalances,
|
|
Validators: validators,
|
|
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
|
|
Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector),
|
|
BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerEpoch*10),
|
|
FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
PreviousJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
}
|
|
}
|
|
|
|
func TestIncreaseBadBalance_NotOK(t *testing.T) {
|
|
tests := []struct {
|
|
i primitives.ValidatorIndex
|
|
b []uint64
|
|
nb uint64
|
|
}{
|
|
{i: 0, b: []uint64{math.MaxUint64, math.MaxUint64, math.MaxUint64}, nb: 1},
|
|
{i: 2, b: []uint64{math.MaxUint64, math.MaxUint64, math.MaxUint64}, nb: 33 * 1e9},
|
|
}
|
|
for _, test := range tests {
|
|
helpers.ClearCache()
|
|
|
|
state, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{
|
|
Validators: []*ethpb.Validator{
|
|
{EffectiveBalance: 4}, {EffectiveBalance: 4}, {EffectiveBalance: 4}},
|
|
Balances: test.b,
|
|
})
|
|
require.NoError(t, err)
|
|
require.ErrorContains(t, "addition overflows", helpers.IncreaseBalance(state, test.i, test.nb))
|
|
}
|
|
}
|
|
|
|
func TestUpdateTotalActiveBalanceCache(t *testing.T) {
|
|
helpers.ClearCache()
|
|
|
|
// Create a test state with some validators
|
|
validators := []*ethpb.Validator{
|
|
{EffectiveBalance: 32 * 1e9, ExitEpoch: params.BeaconConfig().FarFutureEpoch, ActivationEpoch: 0},
|
|
{EffectiveBalance: 32 * 1e9, ExitEpoch: params.BeaconConfig().FarFutureEpoch, ActivationEpoch: 0},
|
|
{EffectiveBalance: 31 * 1e9, ExitEpoch: params.BeaconConfig().FarFutureEpoch, ActivationEpoch: 0},
|
|
}
|
|
state, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{
|
|
Validators: validators,
|
|
Slot: 0,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
// Test updating cache with a specific total
|
|
testTotal := uint64(95 * 1e9) // 32 + 32 + 31 = 95
|
|
err = helpers.UpdateTotalActiveBalanceCache(state, testTotal)
|
|
require.NoError(t, err)
|
|
|
|
// Verify the cache was updated by retrieving the total active balance
|
|
// which should now return the cached value
|
|
cachedTotal, err := helpers.TotalActiveBalance(state)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, testTotal, cachedTotal, "Cache should return the updated total")
|
|
}
|