mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
8c04ced1a6
* fixed epoch_processing * penalize->slash * exit -> voluntary_exit * SEED_LOOKAHEAD -> MIN_SEED_LOOKAHED * ENTRY_EXIT_DELAY -> ACTIVATION_EXIT_DELAY * `INCLUDER_REWARD_QUOTIENT` -> `ATTESTATION_INCLUSION_REWARD_QUOTIEN` * LatestIndexRoots -> LatestActiveIndexRoots * `MIN_VALIDATOR_WITHDRAWAL_EPOCHS` -> `MIN_VALIDATOR_WITHDRAWAL_DELAY` * MAX_WITHDRAWALS_PER_EPOCH -> MAX_EXIT_DEQUEUES_PER_EPOCH * ETH1_DATA_VOTING_PERIOD -> EPOCHS_PER_ETH1_VOTING_PERIOD * SLOT_DURATION -> SECONDS_PER_SLOT * EPOCH_LENGTH -> SLOTS_PER_EPOCH * SLOT_DURATION -> SECONDS_PER_SLOT take 2 * rest of the misc fixes for config name changes * remove tools/bootnode/.!74296!bootnode.go * `current_epoch_start_shard` -> `current_shuffling_start_shard`, `current_shuffling_epoch`, `current_shuffling_see` * go fmt * fixed comment * updated pseudocode comments * merged master
150 lines
4.3 KiB
Go
150 lines
4.3 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().SlotsPerEpoch
|
|
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().LatestActiveIndexRootsLength)
|
|
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().LatestActiveIndexRootsLength],
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
state.Slot = (test.epoch + 1) * params.BeaconConfig().SlotsPerEpoch
|
|
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().MinSeedLookahead, 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().LatestActiveIndexRootsLength)
|
|
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().MinSeedLookahead * params.BeaconConfig().SlotsPerEpoch
|
|
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)
|
|
}
|
|
}
|