prysm-pulse/beacon-chain/core/helpers/randao_test.go
Raul Jordan 5dc5f1d1c8 Update Contract to Use Little Endian And Encoding Across Entire Repo (#1536)
* using little endian and tests for encoding dep inputs

* use decode value and timestamp method in state

* updated comments to match serialization format

* latest compiled contract, abi, bytecode, and bindings

* to little endian everywhere

* fix all tests except for contract tests

* include contract changes

* address broken build

* compile with vyper v8

* update readme

* fix pkg name

* add skip chainstart delay

* skip chainstart delay tests pass

* to little endian timestamp
2019-02-12 09:27:00 +05:30

150 lines
4.2 KiB
Go

package helpers
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
"testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestRandaoMix_Ok(t *testing.T) {
randaoMixes := make([][]byte, params.BeaconConfig().LatestRandaoMixesLength)
for i := 0; i < len(randaoMixes); i++ {
intInBytes := make([]byte, 32)
binary.LittleEndian.PutUint64(intInBytes, uint64(i))
randaoMixes[i] = intInBytes
}
state := &pb.BeaconState{LatestRandaoMixesHash32S: randaoMixes}
tests := []struct {
epoch uint64
randaoMix []byte
}{
{
epoch: 10,
randaoMix: randaoMixes[10],
},
{
epoch: 2344,
randaoMix: randaoMixes[2344],
},
{
epoch: 99999,
randaoMix: randaoMixes[99999%params.BeaconConfig().LatestRandaoMixesLength],
},
}
for _, test := range tests {
state.Slot = (test.epoch + 1) * params.BeaconConfig().EpochLength
mix, err := RandaoMix(state, test.epoch)
if err != nil {
t.Fatalf("Could not get randao mix: %v", err)
}
if !bytes.Equal(test.randaoMix, mix) {
t.Errorf("Incorrect randao mix. Wanted: %#x, got: %#x",
test.randaoMix, mix)
}
}
}
func TestRandaoMix_OutOfBound(t *testing.T) {
wanted := fmt.Sprintf(
"input randaoMix epoch %d out of bounds: %d <= epoch < %d",
100, 0, 0,
)
if _, err := RandaoMix(&pb.BeaconState{}, 100); !strings.Contains(err.Error(), wanted) {
t.Errorf("Expected: %s, received: %s", wanted, err.Error())
}
}
func TestActiveIndexRoot_Ok(t *testing.T) {
activeIndexRoots := make([][]byte, params.BeaconConfig().LatestIndexRootsLength)
for i := 0; i < len(activeIndexRoots); i++ {
intInBytes := make([]byte, 32)
binary.LittleEndian.PutUint64(intInBytes, uint64(i))
activeIndexRoots[i] = intInBytes
}
state := &pb.BeaconState{LatestIndexRootHash32S: activeIndexRoots}
tests := []struct {
epoch uint64
indexRoot []byte
}{
{
epoch: 34,
indexRoot: activeIndexRoots[34],
},
{
epoch: 3444,
indexRoot: activeIndexRoots[3444],
},
{
epoch: 999999,
indexRoot: activeIndexRoots[999999%params.BeaconConfig().LatestIndexRootsLength],
},
}
for _, test := range tests {
state.Slot = (test.epoch + 1) * params.BeaconConfig().EpochLength
indexRoot, err := ActiveIndexRoot(state, test.epoch)
if err != nil {
t.Fatalf("Could not get index root: %v", err)
}
if !bytes.Equal(test.indexRoot, indexRoot) {
t.Errorf("Incorrect index root. Wanted: %#x, got: %#x",
test.indexRoot, indexRoot)
}
}
}
func TestActiveIndexRoot_OutOfBound(t *testing.T) {
wanted := fmt.Sprintf(
"input indexRoot epoch %d out of bounds: %d <= epoch < %d",
100, 0, 0,
)
if _, err := ActiveIndexRoot(&pb.BeaconState{}, 100); !strings.Contains(err.Error(), wanted) {
t.Errorf("Expected: %s, received: %s", wanted, err.Error())
}
}
func TestGenerateSeed_OutOfBound(t *testing.T) {
wanted := fmt.Sprintf(
"input randaoMix epoch %d out of bounds: %d <= epoch < %d",
100-params.BeaconConfig().SeedLookahead, 0, 0,
)
if _, err := GenerateSeed(&pb.BeaconState{}, 100); !strings.Contains(err.Error(), wanted) {
t.Errorf("Expected: %s, received: %s", wanted, err.Error())
}
}
func TestGenerateSeed_Ok(t *testing.T) {
activeIndexRoots := make([][]byte, params.BeaconConfig().LatestIndexRootsLength)
for i := 0; i < len(activeIndexRoots); i++ {
intInBytes := make([]byte, 32)
binary.LittleEndian.PutUint64(intInBytes, uint64(i))
activeIndexRoots[i] = intInBytes
}
randaoMixes := make([][]byte, params.BeaconConfig().LatestRandaoMixesLength)
for i := 0; i < len(randaoMixes); i++ {
intInBytes := make([]byte, 32)
binary.LittleEndian.PutUint64(intInBytes, uint64(i))
randaoMixes[i] = intInBytes
}
slot := 10 * params.BeaconConfig().SeedLookahead * params.BeaconConfig().EpochLength
state := &pb.BeaconState{
LatestIndexRootHash32S: activeIndexRoots,
LatestRandaoMixesHash32S: randaoMixes,
Slot: slot}
got, err := GenerateSeed(state, 10)
if err != nil {
t.Fatalf("Could not generate seed: %v", err)
}
wanted := [32]byte{242, 35, 140, 234, 201, 60, 23, 152, 187, 111, 227, 129, 108, 254, 108, 63,
192, 111, 114, 225, 0, 159, 145, 106, 133, 217, 25, 25, 251, 58, 175, 168}
if got != wanted {
t.Errorf("Incorrect generated seeds. Got: %v, wanted: %v",
got, wanted)
}
}