mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
* Electra: Beacon State * Electra: Beacon state fixes from PR 13919 * Add missing tests - part 1 * Split eip_7251_root.go into different files and reuse/share code with historical state summaries root. It's identical! * Add missing tests - part 2 * deposit receipts start index getters and setters (#13947) * adding in getters and setters for deposit receipts start index * adding tests * gaz * Add missing tests - part 3 of 3 Update the electra withdrawal example with a ssz state containing pending partial withdrawals * add tests for beacon-chain/state/state-native/getters_balance_deposits.go * Add electra field to testing/util/block.go execution payload * godoc commentary on public methods * Fix failing test * Add balances index out of bounds check and relevant tests. * Revert switch case electra * Instead of copying spectest data into testdata, use the spectest dependency * Deepsource fixes * Address @rkapka PR feedback * s/MaxPendingPartialsPerWithdrawalSweep/MaxPendingPartialsPerWithdrawalsSweep/ * Use multivalue slice compatible accessors for validator and balance in ActiveBalanceAtIndex * More @rkapka feedback. What a great reviewer! * More tests for branching logic in ExitEpochAndUpdateChurn * fix build --------- Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
148 lines
4.8 KiB
Go
148 lines
4.8 KiB
Go
package state_native_test
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
|
|
statenative "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native"
|
|
testtmpl "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/testing"
|
|
"github.com/prysmaticlabs/prysm/v5/config/params"
|
|
consensus_types "github.com/prysmaticlabs/prysm/v5/consensus-types"
|
|
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
|
"github.com/prysmaticlabs/prysm/v5/testing/util"
|
|
)
|
|
|
|
func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_Phase0(t *testing.T) {
|
|
testtmpl.VerifyBeaconStateValidatorAtIndexReadOnlyHandlesNilSlice(t, func() (state.BeaconState, error) {
|
|
return statenative.InitializeFromProtoUnsafePhase0(ðpb.BeaconState{
|
|
Validators: nil,
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_Altair(t *testing.T) {
|
|
testtmpl.VerifyBeaconStateValidatorAtIndexReadOnlyHandlesNilSlice(t, func() (state.BeaconState, error) {
|
|
return statenative.InitializeFromProtoUnsafeAltair(ðpb.BeaconStateAltair{
|
|
Validators: nil,
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_Bellatrix(t *testing.T) {
|
|
testtmpl.VerifyBeaconStateValidatorAtIndexReadOnlyHandlesNilSlice(t, func() (state.BeaconState, error) {
|
|
return statenative.InitializeFromProtoUnsafeBellatrix(ðpb.BeaconStateBellatrix{
|
|
Validators: nil,
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_Capella(t *testing.T) {
|
|
testtmpl.VerifyBeaconStateValidatorAtIndexReadOnlyHandlesNilSlice(t, func() (state.BeaconState, error) {
|
|
return statenative.InitializeFromProtoUnsafeCapella(ðpb.BeaconStateCapella{
|
|
Validators: nil,
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_Deneb(t *testing.T) {
|
|
testtmpl.VerifyBeaconStateValidatorAtIndexReadOnlyHandlesNilSlice(t, func() (state.BeaconState, error) {
|
|
return statenative.InitializeFromProtoUnsafeDeneb(ðpb.BeaconStateDeneb{
|
|
Validators: nil,
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestValidatorIndexes(t *testing.T) {
|
|
dState, _ := util.DeterministicGenesisState(t, 10)
|
|
byteValue := dState.PubkeyAtIndex(1)
|
|
t.Run("ValidatorIndexByPubkey", func(t *testing.T) {
|
|
require.Equal(t, hexutil.Encode(byteValue[:]), "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b")
|
|
})
|
|
t.Run("ValidatorAtIndexReadOnly", func(t *testing.T) {
|
|
readOnlyState, err := dState.ValidatorAtIndexReadOnly(1)
|
|
require.NoError(t, err)
|
|
readOnlyBytes := readOnlyState.PublicKey()
|
|
require.NotEmpty(t, readOnlyBytes)
|
|
require.Equal(t, hexutil.Encode(readOnlyBytes[:]), hexutil.Encode(byteValue[:]))
|
|
})
|
|
}
|
|
|
|
func TestActiveBalanceAtIndex(t *testing.T) {
|
|
// Test setup with a state with 4 validators.
|
|
// Validators 0 & 1 have compounding withdrawal credentials while validators 2 & 3 have BLS withdrawal credentials.
|
|
pb := ðpb.BeaconStateElectra{
|
|
Validators: []*ethpb.Validator{
|
|
{
|
|
WithdrawalCredentials: []byte{params.BeaconConfig().CompoundingWithdrawalPrefixByte},
|
|
},
|
|
{
|
|
WithdrawalCredentials: []byte{params.BeaconConfig().CompoundingWithdrawalPrefixByte},
|
|
},
|
|
{
|
|
WithdrawalCredentials: []byte{params.BeaconConfig().BLSWithdrawalPrefixByte},
|
|
},
|
|
{
|
|
WithdrawalCredentials: []byte{params.BeaconConfig().BLSWithdrawalPrefixByte},
|
|
},
|
|
},
|
|
Balances: []uint64{
|
|
55,
|
|
math.MaxUint64,
|
|
55,
|
|
math.MaxUint64,
|
|
},
|
|
}
|
|
state, err := statenative.InitializeFromProtoUnsafeElectra(pb)
|
|
require.NoError(t, err)
|
|
|
|
ab, err := state.ActiveBalanceAtIndex(0)
|
|
require.NoError(t, err)
|
|
require.Equal(t, uint64(55), ab)
|
|
|
|
ab, err = state.ActiveBalanceAtIndex(1)
|
|
require.NoError(t, err)
|
|
require.Equal(t, params.BeaconConfig().MaxEffectiveBalanceElectra, ab)
|
|
|
|
ab, err = state.ActiveBalanceAtIndex(2)
|
|
require.NoError(t, err)
|
|
require.Equal(t, uint64(55), ab)
|
|
|
|
ab, err = state.ActiveBalanceAtIndex(3)
|
|
require.NoError(t, err)
|
|
require.Equal(t, params.BeaconConfig().MinActivationBalance, ab)
|
|
|
|
// Accessing a validator index out of bounds should error.
|
|
_, err = state.ActiveBalanceAtIndex(4)
|
|
require.ErrorIs(t, err, consensus_types.ErrOutOfBounds)
|
|
|
|
// Accessing a validator wwhere balance slice is out of bounds for some reason.
|
|
require.NoError(t, state.SetBalances([]uint64{}))
|
|
_, err = state.ActiveBalanceAtIndex(0)
|
|
require.ErrorIs(t, err, consensus_types.ErrOutOfBounds)
|
|
}
|
|
|
|
func TestPendingBalanceToWithdraw(t *testing.T) {
|
|
pb := ðpb.BeaconStateElectra{
|
|
PendingPartialWithdrawals: []*ethpb.PendingPartialWithdrawal{
|
|
{
|
|
Amount: 100,
|
|
},
|
|
{
|
|
Amount: 200,
|
|
},
|
|
{
|
|
Amount: 300,
|
|
},
|
|
},
|
|
}
|
|
state, err := statenative.InitializeFromProtoUnsafeElectra(pb)
|
|
require.NoError(t, err)
|
|
|
|
ab, err := state.PendingBalanceToWithdraw(0)
|
|
require.NoError(t, err)
|
|
require.Equal(t, uint64(600), ab)
|
|
}
|