State/v1: add atts and block roots getter tests (#9255)

* Add att and block getter tests

* Update BUILD.bazel

* Update getters_attestation_test.go

* Update beacon-chain/state/v1/getters_block_test.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* Fix mutation tests

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
This commit is contained in:
terence tsao 2021-07-22 15:31:14 -07:00 committed by GitHub
parent 5864795ca5
commit 526596a679
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 0 deletions

View File

@ -74,6 +74,8 @@ go_test(
name = "go_default_test",
srcs = [
"field_trie_test.go",
"getters_attestation_test.go",
"getters_block_test.go",
"getters_test.go",
"getters_validator_test.go",
"helpers_test.go",

View File

@ -0,0 +1,46 @@
package v1
import (
"testing"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestBeaconState_PreviousEpochAttestations(t *testing.T) {
s, err := InitializeFromProto(&pbp2p.BeaconState{})
require.NoError(t, err)
atts, err := s.PreviousEpochAttestations()
require.NoError(t, err)
require.DeepEqual(t, []*pbp2p.PendingAttestation(nil), atts)
want := []*pbp2p.PendingAttestation{{ProposerIndex: 100}}
s, err = InitializeFromProto(&pbp2p.BeaconState{PreviousEpochAttestations: want})
require.NoError(t, err)
got, err := s.PreviousEpochAttestations()
require.NoError(t, err)
require.DeepEqual(t, want, got)
// Test copy does not mutate.
got[0].ProposerIndex = 101
require.DeepNotEqual(t, want, got)
}
func TestBeaconState_CurrentEpochAttestations(t *testing.T) {
s, err := InitializeFromProto(&pbp2p.BeaconState{})
require.NoError(t, err)
atts, err := s.CurrentEpochAttestations()
require.NoError(t, err)
require.DeepEqual(t, []*pbp2p.PendingAttestation(nil), atts)
want := []*pbp2p.PendingAttestation{{ProposerIndex: 101}}
s, err = InitializeFromProto(&pbp2p.BeaconState{CurrentEpochAttestations: want})
require.NoError(t, err)
got, err := s.CurrentEpochAttestations()
require.NoError(t, err)
require.DeepEqual(t, want, got)
// Test copy does not mutate.
got[0].ProposerIndex = 102
require.DeepNotEqual(t, want, got)
}

View File

@ -0,0 +1,60 @@
package v1
import (
"testing"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
v1alpha1 "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestBeaconState_LatestBlockHeader(t *testing.T) {
s, err := InitializeFromProto(&pbp2p.BeaconState{})
require.NoError(t, err)
got := s.LatestBlockHeader()
require.DeepEqual(t, (*v1alpha1.BeaconBlockHeader)(nil), got)
want := &v1alpha1.BeaconBlockHeader{Slot: 100}
s, err = InitializeFromProto(&pbp2p.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)
}
func TestBeaconState_BlockRoots(t *testing.T) {
s, err := InitializeFromProto(&pbp2p.BeaconState{})
require.NoError(t, err)
got := s.BlockRoots()
require.DeepEqual(t, ([][]byte)(nil), got)
want := [][]byte{{'a'}}
s, err = InitializeFromProto(&pbp2p.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)
}
func TestBeaconState_BlockRootAtIndex(t *testing.T) {
s, err := InitializeFromProto(&pbp2p.BeaconState{})
require.NoError(t, err)
got, err := s.BlockRootAtIndex(0)
require.NoError(t, err)
require.DeepEqual(t, ([]byte)(nil), got)
r := [][]byte{{'a'}}
s, err = InitializeFromProto(&pbp2p.BeaconState{BlockRoots: r})
require.NoError(t, err)
got, err = s.BlockRootAtIndex(0)
require.NoError(t, err)
want := bytesutil.PadTo([]byte{'a'}, 32)
require.DeepSSZEqual(t, want, got)
}