Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 29 additions & 61 deletions beacon-chain/state/state-native/v1/getters_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,43 @@ package v1
import (
"testing"

fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/testing/require"
)

func TestBeaconState_LatestBlockHeader(t *testing.T) {
s, err := InitializeFromProto(&ethpb.BeaconState{})
require.NoError(t, err)
got := s.LatestBlockHeader()
require.DeepEqual(t, (*ethpb.BeaconBlockHeader)(nil), got)

want := &ethpb.BeaconBlockHeader{Slot: 100}
s, err = InitializeFromProto(&ethpb.BeaconState{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)
testtmpl.VerifyBeaconStateLatestBlockHeader(
t,
func() (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconState{})
},
func(BH *ethpb.BeaconBlockHeader) (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconState{LatestBlockHeader: BH})
},
)
}

func TestBeaconState_BlockRoots(t *testing.T) {
s, err := InitializeFromProto(&ethpb.BeaconState{})
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 = InitializeFromProto(&ethpb.BeaconState{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)
testtmpl.VerifyBeaconStateBlockRootsNative(
t,
func() (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconState{})
},
func(BR [][]byte) (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconState{BlockRoots: BR})
},
)
}

func TestBeaconState_BlockRootAtIndex(t *testing.T) {
s, err := InitializeFromProto(&ethpb.BeaconState{})
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 = InitializeFromProto(&ethpb.BeaconState{BlockRoots: 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)
testtmpl.VerifyBeaconStateBlockRootAtIndexNative(
t,
func() (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconState{})
},
func(BR [][]byte) (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconState{BlockRoots: BR})
},
)
}
8 changes: 4 additions & 4 deletions beacon-chain/state/state-native/v1/getters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

func TestBeaconState_SlotDataRace(t *testing.T) {
testtmpl.VerifyBeaconState_SlotDataRace(t, func() (state.BeaconState, error) {
testtmpl.VerifyBeaconStateSlotDataRace(t, func() (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconState{Slot: 1})
})
}

func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) {
testtmpl.VerifyBeaconState_MatchCurrentJustifiedCheckptNative(
testtmpl.VerifyBeaconStateMatchCurrentJustifiedCheckptNative(
t,
func(cp *ethpb.Checkpoint) (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconState{CurrentJustifiedCheckpoint: cp})
Expand All @@ -24,7 +24,7 @@ func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) {
}

func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) {
testtmpl.VerifyBeaconState_MatchPreviousJustifiedCheckptNative(
testtmpl.VerifyBeaconStateMatchPreviousJustifiedCheckptNative(
t,
func(cp *ethpb.Checkpoint) (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconState{PreviousJustifiedCheckpoint: cp})
Expand All @@ -33,7 +33,7 @@ func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) {
}

func TestBeaconState_ValidatorByPubkey(t *testing.T) {
testtmpl.VerifyBeaconState_ValidatorByPubkey(t, func() (state.BeaconState, error) {
testtmpl.VerifyBeaconStateValidatorByPubkey(t, func() (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconState{})
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) {
testtmpl.VerifyBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t, func() (state.BeaconState, error) {
testtmpl.VerifyBeaconStateValidatorAtIndexReadOnlyHandlesNilSlice(t, func() (state.BeaconState, error) {
return v1.InitializeFromProtoUnsafe(&ethpb.BeaconState{
Validators: nil,
})
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/state/state-native/v2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ go_test(
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/stateutil:go_default_library",
"//beacon-chain/state/testing:go_default_library",
"//beacon-chain/state/types:go_default_library",
"//beacon-chain/state/v2:go_default_library",
"//config/features:go_default_library",
"//config/fieldparams:go_default_library",
Expand Down
89 changes: 29 additions & 60 deletions beacon-chain/state/state-native/v2/getters_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,43 @@ package v2
import (
"testing"

fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/testing/require"
)

func TestBeaconState_LatestBlockHeader(t *testing.T) {
s, err := InitializeFromProto(&ethpb.BeaconStateAltair{})
require.NoError(t, err)
got := s.LatestBlockHeader()
require.DeepEqual(t, (*ethpb.BeaconBlockHeader)(nil), got)

want := &ethpb.BeaconBlockHeader{Slot: 100}
s, err = InitializeFromProto(&ethpb.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)
testtmpl.VerifyBeaconStateLatestBlockHeader(
t,
func() (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconStateAltair{})
},
func(BH *ethpb.BeaconBlockHeader) (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconStateAltair{LatestBlockHeader: BH})
},
)
}

func TestBeaconState_BlockRoots(t *testing.T) {
s, err := InitializeFromProto(&ethpb.BeaconStateAltair{})
require.NoError(t, err)
got := s.BlockRoots()
want := make([][]byte, fieldparams.BlockRootsLength)
for i := range want {
want[i] = make([]byte, fieldparams.RootLength)
}
require.DeepEqual(t, want, got)

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)
}
}
s, err = InitializeFromProto(&ethpb.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)
testtmpl.VerifyBeaconStateBlockRootsNative(
t,
func() (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconStateAltair{})
},
func(BR [][]byte) (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconStateAltair{BlockRoots: BR})
},
)
}

func TestBeaconState_BlockRootAtIndex(t *testing.T) {
s, err := InitializeFromProto(&ethpb.BeaconStateAltair{})
require.NoError(t, err)
got, err := s.BlockRootAtIndex(0)
require.NoError(t, err)
require.DeepEqual(t, bytesutil.PadTo([]byte{}, fieldparams.RootLength), got)

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(&ethpb.BeaconStateAltair{BlockRoots: bRoots})
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)
testtmpl.VerifyBeaconStateBlockRootAtIndexNative(
t,
func() (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconStateAltair{})
},
func(BR [][]byte) (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconStateAltair{BlockRoots: BR})
},
)
}
8 changes: 4 additions & 4 deletions beacon-chain/state/state-native/v2/getters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

func TestBeaconState_SlotDataRace(t *testing.T) {
testtmpl.VerifyBeaconState_SlotDataRace(t, func() (state.BeaconState, error) {
testtmpl.VerifyBeaconStateSlotDataRace(t, func() (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconStateAltair{Slot: 1})
})
}

func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) {
testtmpl.VerifyBeaconState_MatchCurrentJustifiedCheckptNative(
testtmpl.VerifyBeaconStateMatchCurrentJustifiedCheckptNative(
t,
func(cp *ethpb.Checkpoint) (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconStateAltair{CurrentJustifiedCheckpoint: cp})
Expand All @@ -24,7 +24,7 @@ func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) {
}

func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) {
testtmpl.VerifyBeaconState_MatchPreviousJustifiedCheckptNative(
testtmpl.VerifyBeaconStateMatchPreviousJustifiedCheckptNative(
t,
func(cp *ethpb.Checkpoint) (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconStateAltair{PreviousJustifiedCheckpoint: cp})
Expand All @@ -33,7 +33,7 @@ func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) {
}

func TestBeaconState_ValidatorByPubkey(t *testing.T) {
testtmpl.VerifyBeaconState_ValidatorByPubkey(t, func() (state.BeaconState, error) {
testtmpl.VerifyBeaconStateValidatorByPubkey(t, func() (state.BeaconState, error) {
return InitializeFromProto(&ethpb.BeaconStateAltair{})
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) {
testtmpl.VerifyBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t, func() (state.BeaconState, error) {
testtmpl.VerifyBeaconStateValidatorAtIndexReadOnlyHandlesNilSlice(t, func() (state.BeaconState, error) {
return v2.InitializeFromProtoUnsafe(&ethpb.BeaconStateAltair{
Validators: nil,
})
Expand Down
Loading