prysm-pulse/beacon-chain/core/blocks/block_test.go

124 lines
2.7 KiB
Go
Raw Normal View History

package blocks
2018-12-01 22:09:12 +00:00
import (
"bytes"
"reflect"
"testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
2018-12-01 22:09:12 +00:00
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestGenesisBlock(t *testing.T) {
stateHash := []byte{0}
2018-12-01 22:09:12 +00:00
b1 := NewGenesisBlock(stateHash)
if b1.GetParentRootHash32() == nil {
2018-12-01 22:09:12 +00:00
t.Error("genesis block missing ParentHash field")
}
if !reflect.DeepEqual(b1.GetBody().GetAttestations(), []*pb.Attestation{}) {
2018-12-01 22:09:12 +00:00
t.Errorf("genesis block should have 0 attestations")
}
if !bytes.Equal(b1.GetRandaoRevealHash32(), params.BeaconConfig().ZeroHash[:]) {
t.Error("genesis block missing RandaoRevealHash32 field")
2018-12-01 22:09:12 +00:00
}
if !bytes.Equal(b1.GetStateRootHash32(), stateHash) {
t.Error("genesis block StateRootHash32 isn't initialized correctly")
2018-12-01 22:09:12 +00:00
}
rd := []byte{}
if IsRandaoValid(b1.GetRandaoRevealHash32(), rd) {
2018-12-01 22:09:12 +00:00
t.Error("RANDAO should be empty")
}
}
func TestBlockRootAtSlot_OK(t *testing.T) {
if params.BeaconConfig().EpochLength != 64 {
t.Errorf("EpochLength should be 64 for these tests to pass")
}
var blockRoots [][]byte
for i := uint64(0); i < params.BeaconConfig().EpochLength*2; i++ {
blockRoots = append(blockRoots, []byte{byte(i)})
}
state := &pb.BeaconState{
LatestBlockRootHash32S: blockRoots,
}
tests := []struct {
slot uint64
stateSlot uint64
expectedRoot []byte
}{
{
slot: 0,
stateSlot: 1,
expectedRoot: []byte{0},
},
{
slot: 2,
stateSlot: 5,
expectedRoot: []byte{2},
},
{
slot: 64,
stateSlot: 128,
expectedRoot: []byte{64},
}, {
slot: 2999,
stateSlot: 3000,
expectedRoot: []byte{127},
}, {
slot: 2873,
stateSlot: 3000,
expectedRoot: []byte{1},
},
}
for _, tt := range tests {
state.Slot = tt.stateSlot
result, err := BlockRoot(state, tt.slot)
if err != nil {
t.Errorf("Failed to get block root at slot %d: %v", tt.slot, err)
}
if !bytes.Equal(result, tt.expectedRoot) {
t.Errorf(
"Result block root was an unexpected value. Wanted %d, got %d",
tt.expectedRoot,
result,
)
}
}
}
func TestBlockRootAtSlot_OutOfBounds(t *testing.T) {
if params.BeaconConfig().EpochLength != 64 {
t.Errorf("EpochLength should be 64 for these tests to pass")
}
state := &pb.BeaconState{}
tests := []struct {
slot uint64
expectedErr string
}{
{
slot: 1000,
expectedErr: "slot 1000 out of bounds: 0 <= slot < 0",
},
{
slot: 129,
expectedErr: "slot 129 out of bounds: 0 <= slot < 0",
},
}
for _, tt := range tests {
_, err := BlockRoot(state, tt.slot)
if err != nil && err.Error() != tt.expectedErr {
t.Errorf("Expected error \"%s\" got \"%v\"", tt.expectedErr, err)
}
}
}