2022-01-13 11:23:53 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBeaconState_LatestBlockHeader(t *testing.T) {
|
|
|
|
s, err := InitializeFromProto(ðpb.BeaconStateAltair{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
got := s.LatestBlockHeader()
|
|
|
|
require.DeepEqual(t, (*ethpb.BeaconBlockHeader)(nil), got)
|
|
|
|
|
|
|
|
want := ðpb.BeaconBlockHeader{Slot: 100}
|
|
|
|
s, err = InitializeFromProto(ðpb.BeaconStateAltair{LatestBlockHeader: 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeaconState_BlockRoots(t *testing.T) {
|
|
|
|
s, err := InitializeFromProto(ðpb.BeaconStateAltair{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
got := s.BlockRoots()
|
2022-01-27 08:42:32 +00:00
|
|
|
want := make([][]byte, fieldparams.BlockRootsLength)
|
|
|
|
for i := range want {
|
|
|
|
want[i] = make([]byte, fieldparams.RootLength)
|
|
|
|
}
|
|
|
|
require.DeepEqual(t, want, got)
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2022-01-27 08:42:32 +00:00
|
|
|
want = make([][]byte, fieldparams.BlockRootsLength)
|
|
|
|
for i := range want {
|
|
|
|
if i == 0 {
|
|
|
|
want[i] = bytesutil.PadTo([]byte{'a'}, fieldparams.RootLength)
|
|
|
|
} else {
|
|
|
|
want[i] = make([]byte, fieldparams.RootLength)
|
|
|
|
}
|
|
|
|
}
|
2022-01-13 11:23:53 +00:00
|
|
|
s, err = InitializeFromProto(ðpb.BeaconStateAltair{BlockRoots: 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 TestBeaconState_BlockRootAtIndex(t *testing.T) {
|
|
|
|
s, err := InitializeFromProto(ðpb.BeaconStateAltair{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
got, err := s.BlockRootAtIndex(0)
|
|
|
|
require.NoError(t, err)
|
2022-01-27 08:42:32 +00:00
|
|
|
require.DeepEqual(t, bytesutil.PadTo([]byte{}, fieldparams.RootLength), got)
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2022-01-27 08:42:32 +00:00
|
|
|
r := [fieldparams.BlockRootsLength][fieldparams.RootLength]byte{{'a'}}
|
|
|
|
bRoots := make([][]byte, len(r))
|
|
|
|
for i, root := range r {
|
|
|
|
tmp := root
|
|
|
|
bRoots[i] = tmp[:]
|
|
|
|
}
|
|
|
|
s, err = InitializeFromProto(ðpb.BeaconStateAltair{BlockRoots: bRoots})
|
2022-01-13 11:23:53 +00:00
|
|
|
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)
|
|
|
|
}
|