2018-12-20 22:00:38 +00:00
|
|
|
package blocks
|
2018-12-01 22:09:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2018-12-13 19:35:11 +00:00
|
|
|
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) {
|
2018-12-20 22:00:38 +00:00
|
|
|
stateHash := []byte{0}
|
2018-12-01 22:09:12 +00:00
|
|
|
b1 := NewGenesisBlock(stateHash)
|
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
if b1.GetParentRootHash32() == nil {
|
2018-12-01 22:09:12 +00:00
|
|
|
t.Error("genesis block missing ParentHash field")
|
|
|
|
}
|
|
|
|
|
2018-12-25 06:47:07 +00:00
|
|
|
if !reflect.DeepEqual(b1.GetBody().GetAttestations(), []*pb.Attestation{}) {
|
2018-12-01 22:09:12 +00:00
|
|
|
t.Errorf("genesis block should have 0 attestations")
|
|
|
|
}
|
|
|
|
|
2018-12-25 06:47:07 +00:00
|
|
|
if !bytes.Equal(b1.GetRandaoRevealHash32(), params.BeaconConfig().ZeroHash[:]) {
|
2018-12-03 16:10:47 +00:00
|
|
|
t.Error("genesis block missing RandaoRevealHash32 field")
|
2018-12-01 22:09:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
if !bytes.Equal(b1.GetStateRootHash32(), stateHash) {
|
2018-12-03 16:10:47 +00:00
|
|
|
t.Error("genesis block StateRootHash32 isn't initialized correctly")
|
2018-12-01 22:09:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
rd := []byte{}
|
|
|
|
if IsRandaoValid(b1.GetRandaoRevealHash32(), rd) {
|
2018-12-01 22:09:12 +00:00
|
|
|
t.Error("RANDAO should be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-12-13 19:35:11 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|