mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
d077483577
* v3 import renamings * tidy * fmt * rev * Update beacon-chain/core/epoch/precompute/reward_penalty_test.go * Update beacon-chain/core/helpers/validators_test.go * Update beacon-chain/db/alias.go * Update beacon-chain/db/alias.go * Update beacon-chain/db/alias.go * Update beacon-chain/db/iface/BUILD.bazel * Update beacon-chain/db/kv/kv.go * Update beacon-chain/db/kv/state.go * Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go * Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go * Update beacon-chain/sync/initial-sync/service.go * fix deps * fix bad replacements * fix bad replacements * change back * gohashtree version * fix deps Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Potuz <potuz@prysmaticlabs.com>
136 lines
3.2 KiB
Go
136 lines
3.2 KiB
Go
package testing
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
|
|
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
|
)
|
|
|
|
type getStateWithLatestBlockHeader func(*ethpb.BeaconBlockHeader) (state.BeaconState, error)
|
|
|
|
func VerifyBeaconStateLatestBlockHeader(
|
|
t *testing.T,
|
|
factory getState,
|
|
factoryLBH getStateWithLatestBlockHeader,
|
|
) {
|
|
s, err := factory()
|
|
require.NoError(t, err)
|
|
got := s.LatestBlockHeader()
|
|
require.DeepEqual(t, (*ethpb.BeaconBlockHeader)(nil), got)
|
|
|
|
want := ðpb.BeaconBlockHeader{Slot: 100}
|
|
s, err = factoryLBH(want)
|
|
require.NoError(t, err)
|
|
got = s.LatestBlockHeader()
|
|
require.DeepEqual(t, want, got)
|
|
|
|
// Test copy does not mutate.
|
|
got.Slot = 101
|
|
require.DeepNotEqual(t, want, got)
|
|
}
|
|
|
|
type getStateWithLBlockRoots func([][]byte) (state.BeaconState, error)
|
|
|
|
func VerifyBeaconStateBlockRoots(
|
|
t *testing.T,
|
|
factory getState,
|
|
factoryBR getStateWithLBlockRoots,
|
|
) {
|
|
s, err := factory()
|
|
require.NoError(t, err)
|
|
got := s.BlockRoots()
|
|
require.DeepEqual(t, ([][]byte)(nil), got)
|
|
|
|
want := [][]byte{{'a'}}
|
|
s, err = factoryBR(want)
|
|
require.NoError(t, err)
|
|
got = s.BlockRoots()
|
|
require.DeepEqual(t, want, got)
|
|
|
|
// Test copy does not mutate.
|
|
got[0][0] = 'b'
|
|
require.DeepNotEqual(t, want, got)
|
|
}
|
|
|
|
func VerifyBeaconStateBlockRootsNative(
|
|
t *testing.T,
|
|
factory getState,
|
|
factoryBR getStateWithLBlockRoots,
|
|
) {
|
|
s, err := factory()
|
|
require.NoError(t, err)
|
|
got := s.BlockRoots()
|
|
want := make([][]byte, fieldparams.BlockRootsLength)
|
|
for i := range want {
|
|
want[i] = make([]byte, 32)
|
|
}
|
|
require.DeepEqual(t, want, got)
|
|
|
|
want = make([][]byte, fieldparams.BlockRootsLength)
|
|
for i := range want {
|
|
if i == 0 {
|
|
want[i] = bytesutil.PadTo([]byte{'a'}, 32)
|
|
} else {
|
|
want[i] = make([]byte, 32)
|
|
}
|
|
|
|
}
|
|
s, err = factoryBR(want)
|
|
require.NoError(t, err)
|
|
got = s.BlockRoots()
|
|
require.DeepEqual(t, want, got)
|
|
|
|
// Test copy does not mutate.
|
|
got[0][0] = 'b'
|
|
require.DeepNotEqual(t, want, got)
|
|
}
|
|
|
|
func VerifyBeaconStateBlockRootAtIndex(
|
|
t *testing.T,
|
|
factory getState,
|
|
factoryBR getStateWithLBlockRoots,
|
|
) {
|
|
s, err := factory()
|
|
require.NoError(t, err)
|
|
got, err := s.BlockRootAtIndex(0)
|
|
require.NoError(t, err)
|
|
require.DeepEqual(t, ([]byte)(nil), got)
|
|
|
|
r := [][]byte{{'a'}}
|
|
s, err = factoryBR(r)
|
|
require.NoError(t, err)
|
|
got, err = s.BlockRootAtIndex(0)
|
|
require.NoError(t, err)
|
|
want := bytesutil.PadTo([]byte{'a'}, fieldparams.RootLength)
|
|
require.DeepSSZEqual(t, want, got)
|
|
}
|
|
|
|
func VerifyBeaconStateBlockRootAtIndexNative(
|
|
t *testing.T,
|
|
factory getState,
|
|
factoryBR getStateWithLBlockRoots,
|
|
) {
|
|
s, err := factory()
|
|
require.NoError(t, err)
|
|
got, err := s.BlockRootAtIndex(0)
|
|
require.NoError(t, err)
|
|
require.DeepEqual(t, bytesutil.PadTo([]byte{}, 32), got)
|
|
|
|
r := [fieldparams.BlockRootsLength][32]byte{{'a'}}
|
|
bRoots := make([][]byte, len(r))
|
|
for i, root := range r {
|
|
tmp := root
|
|
bRoots[i] = tmp[:]
|
|
}
|
|
s, err = factoryBR(bRoots)
|
|
require.NoError(t, err)
|
|
got, err = s.BlockRootAtIndex(0)
|
|
require.NoError(t, err)
|
|
want := bytesutil.PadTo([]byte{'a'}, 32)
|
|
require.DeepSSZEqual(t, want, got)
|
|
}
|