Files
prysm/beacon-chain/core/helpers/validators_test.go
Preston Van Loon 78a25f99c3 Update fastssz (#6760)
* Update fastssz
* Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz
* fmt
* gaz
* Merge refs/heads/master into update-fssz
* goimports
* Merge refs/heads/master into update-fssz
* Merge refs/heads/master into update-fssz
* Merge refs/heads/master into update-fssz
* Merge refs/heads/master into update-fssz
* Merge refs/heads/master into update-fssz
* Fix
* fix ethereumapis
* fix again
* kafka
* fix gen file
* fix compute signing root
* gofmt
* checkpoint progress
* progress
* checkpoint
* progress
* Fix build
* checkpoint
* helpers
* Another test fixed
* gaz
* another test fix
* gofmt
* some fixes
* Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz
* fix one test
* Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz
* fill empty checkpoint roots
* more padding
* more padding
* Fix //beacon-chain/rpc/debug:go_default_test
* fix //beacon-chain/core/state:go_default_test
* fix //beacon-chain/core/state:go_default_test
* fix some htr errors
* fix //slasher/rpc:go_default_test
* Progress on //beacon-chain/core/blocks:go_default_test
* Progress on //beacon-chain/core/blocks:go_default_test
* Progress on //beacon-chain/core/blocks:go_default_test
* fix //slasher/db/kv:go_default_test
* progress
* fix //beacon-chain/sync/initial-sync:go_raceon_test
* gofmt and gaz
* fix one more test, taking a break
* Fix //beacon-chain/core/blocks:go_default_test
* Complete beacon-chain/powchain
* Do most of beacon-chain/rpc/beacon/
* Do most of beacon-chain/blockchain
* fix //beacon-chain/operations/attestations/kv:go_default_test
* Fix //beacon-chain/cache/depositcache:go_default_test
* Fix //slasher/detection:go_default_test
* Progress
* fix //beacon-chain/rpc/validator:go_default_test
* gofmt
* fix //validator/client:go_default_test
* fix
* fix //beacon-chain/blockchain:go_raceoff_test
* fix //beacon-chain/rpc/beacon:go_default_test
* fix 1 of 4 shards in //beacon-chain/sync:go_default_test
* Fix //beacon-chain/sync:go_default_test and gofmt
* prevent panic
* fix //beacon-chain/state/stategen:go_default_test
* fix
* Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz
* fix most tests
* Self review, go mod tidy, run regen scripts
* fix slasher
* Update ethereumapis
* disable spawn strategy override
* Merge refs/heads/master into update-fssz
* Merge refs/heads/master into update-fssz
* Remove extra line in imports
* Remove extra line in imports
* Gofmt
* PR feedback from @nisdas
2020-08-24 01:46:17 +00:00

749 lines
21 KiB
Go

package helpers
import (
"testing"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestIsActiveValidator_OK(t *testing.T) {
tests := []struct {
a uint64
b bool
}{
{a: 0, b: false},
{a: 10, b: true},
{a: 100, b: false},
{a: 1000, b: false},
{a: 64, b: true},
}
for _, test := range tests {
validator := &ethpb.Validator{ActivationEpoch: 10, ExitEpoch: 100}
assert.Equal(t, test.b, IsActiveValidator(validator, test.a), "IsActiveValidator(%d)", test.a)
}
}
func TestIsActiveValidatorUsingTrie_OK(t *testing.T) {
tests := []struct {
a uint64
b bool
}{
{a: 0, b: false},
{a: 10, b: true},
{a: 100, b: false},
{a: 1000, b: false},
{a: 64, b: true},
}
val := &ethpb.Validator{ActivationEpoch: 10, ExitEpoch: 100}
beaconState, err := beaconstate.InitializeFromProto(&pb.BeaconState{Validators: []*ethpb.Validator{val}})
require.NoError(t, err)
for _, test := range tests {
readOnlyVal, err := beaconState.ValidatorAtIndexReadOnly(0)
require.NoError(t, err)
assert.Equal(t, test.b, IsActiveValidatorUsingTrie(readOnlyVal, test.a), "IsActiveValidatorUsingTrie(%d)", test.a)
}
}
func TestIsSlashableValidator_OK(t *testing.T) {
tests := []struct {
name string
validator *ethpb.Validator
epoch uint64
slashable bool
}{
{
name: "Unset withdrawable, slashable",
validator: &ethpb.Validator{
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
},
epoch: 0,
slashable: true,
},
{
name: "before withdrawable, slashable",
validator: &ethpb.Validator{
WithdrawableEpoch: 5,
},
epoch: 3,
slashable: true,
},
{
name: "inactive, not slashable",
validator: &ethpb.Validator{
ActivationEpoch: 5,
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
},
epoch: 2,
slashable: false,
},
{
name: "after withdrawable, not slashable",
validator: &ethpb.Validator{
WithdrawableEpoch: 3,
},
epoch: 3,
slashable: false,
},
{
name: "slashed and withdrawable, not slashable",
validator: &ethpb.Validator{
Slashed: true,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawableEpoch: 1,
},
epoch: 2,
slashable: false,
},
{
name: "slashed, not slashable",
validator: &ethpb.Validator{
Slashed: true,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
},
epoch: 2,
slashable: false,
},
{
name: "inactive and slashed, not slashable",
validator: &ethpb.Validator{
Slashed: true,
ActivationEpoch: 4,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
},
epoch: 2,
slashable: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
slashableValidator := IsSlashableValidator(test.validator.ActivationEpoch,
test.validator.WithdrawableEpoch, test.validator.Slashed, test.epoch)
assert.Equal(t, test.slashable, slashableValidator, "Expected active validator slashable to be %t", test.slashable)
})
}
}
func TestIsSlashableValidatorUsingTrie_OK(t *testing.T) {
tests := []struct {
name string
validator *ethpb.Validator
epoch uint64
slashable bool
}{
{
name: "Unset withdrawable, slashable",
validator: &ethpb.Validator{
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
},
epoch: 0,
slashable: true,
},
{
name: "before withdrawable, slashable",
validator: &ethpb.Validator{
WithdrawableEpoch: 5,
},
epoch: 3,
slashable: true,
},
{
name: "inactive, not slashable",
validator: &ethpb.Validator{
ActivationEpoch: 5,
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
},
epoch: 2,
slashable: false,
},
{
name: "after withdrawable, not slashable",
validator: &ethpb.Validator{
WithdrawableEpoch: 3,
},
epoch: 3,
slashable: false,
},
{
name: "slashed and withdrawable, not slashable",
validator: &ethpb.Validator{
Slashed: true,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawableEpoch: 1,
},
epoch: 2,
slashable: false,
},
{
name: "slashed, not slashable",
validator: &ethpb.Validator{
Slashed: true,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
},
epoch: 2,
slashable: false,
},
{
name: "inactive and slashed, not slashable",
validator: &ethpb.Validator{
Slashed: true,
ActivationEpoch: 4,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
},
epoch: 2,
slashable: false,
},
}
for _, test := range tests {
beaconState, err := beaconstate.InitializeFromProto(&pb.BeaconState{Validators: []*ethpb.Validator{test.validator}})
require.NoError(t, err)
readOnlyVal, err := beaconState.ValidatorAtIndexReadOnly(0)
require.NoError(t, err)
t.Run(test.name, func(t *testing.T) {
slashableValidator := IsSlashableValidatorUsingTrie(readOnlyVal, test.epoch)
assert.Equal(t, test.slashable, slashableValidator, "Expected active validator slashable to be %t", test.slashable)
})
}
}
func TestBeaconProposerIndex_OK(t *testing.T) {
params.SetupTestConfigCleanup(t)
ClearCache()
c := params.BeaconConfig()
c.MinGenesisActiveValidatorCount = 16384
params.OverrideBeaconConfig(c)
validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount/8)
for i := 0; i < len(validators); i++ {
validators[i] = &ethpb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
state, err := beaconstate.InitializeFromProto(&pb.BeaconState{
Validators: validators,
Slot: 0,
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
})
require.NoError(t, err)
tests := []struct {
slot uint64
index uint64
}{
{
slot: 1,
index: 2039,
},
{
slot: 5,
index: 1895,
},
{
slot: 19,
index: 1947,
},
{
slot: 30,
index: 369,
},
{
slot: 43,
index: 464,
},
}
for _, tt := range tests {
ClearCache()
require.NoError(t, state.SetSlot(tt.slot))
result, err := BeaconProposerIndex(state)
require.NoError(t, err, "Failed to get shard and committees at slot")
assert.Equal(t, tt.index, result, "Result index was an unexpected value")
}
}
func TestComputeProposerIndex_Compatibility(t *testing.T) {
validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount)
for i := 0; i < len(validators); i++ {
validators[i] = &ethpb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
state, err := beaconstate.InitializeFromProto(&pb.BeaconState{
Validators: validators,
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
})
require.NoError(t, err)
indices, err := ActiveValidatorIndices(state, 0)
require.NoError(t, err)
var proposerIndices []uint64
seed, err := Seed(state, 0, params.BeaconConfig().DomainBeaconProposer)
require.NoError(t, err)
for i := uint64(0); i < params.BeaconConfig().SlotsPerEpoch; i++ {
seedWithSlot := append(seed[:], bytesutil.Bytes8(i)...)
seedWithSlotHash := hashutil.Hash(seedWithSlot)
index, err := ComputeProposerIndex(state, indices, seedWithSlotHash)
require.NoError(t, err)
proposerIndices = append(proposerIndices, index)
}
var wantedProposerIndices []uint64
seed, err = Seed(state, 0, params.BeaconConfig().DomainBeaconProposer)
require.NoError(t, err)
for i := uint64(0); i < params.BeaconConfig().SlotsPerEpoch; i++ {
seedWithSlot := append(seed[:], bytesutil.Bytes8(i)...)
seedWithSlotHash := hashutil.Hash(seedWithSlot)
index, err := ComputeProposerIndexWithValidators(state.Validators(), indices, seedWithSlotHash)
require.NoError(t, err)
wantedProposerIndices = append(wantedProposerIndices, index)
}
assert.DeepEqual(t, wantedProposerIndices, proposerIndices, "Wanted proposer indices from ComputeProposerIndexWithValidators does not match")
}
func TestDelayedActivationExitEpoch_OK(t *testing.T) {
epoch := uint64(9999)
wanted := epoch + 1 + params.BeaconConfig().MaxSeedLookahead
assert.Equal(t, wanted, ActivationExitEpoch(epoch))
}
func TestActiveValidatorCount_Genesis(t *testing.T) {
c := 1000
validators := make([]*ethpb.Validator, c)
for i := 0; i < len(validators); i++ {
validators[i] = &ethpb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
beaconState, err := beaconstate.InitializeFromProto(&pb.BeaconState{
Slot: 0,
Validators: validators,
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
})
require.NoError(t, err)
// Preset cache to a bad count.
seed, err := Seed(beaconState, 0, params.BeaconConfig().DomainBeaconAttester)
require.NoError(t, err)
require.NoError(t, committeeCache.AddCommitteeShuffledList(&cache.Committees{Seed: seed, ShuffledIndices: []uint64{1, 2, 3}}))
validatorCount, err := ActiveValidatorCount(beaconState, CurrentEpoch(beaconState))
require.NoError(t, err)
assert.Equal(t, uint64(c), validatorCount, "Did not get the correct validator count")
}
func TestChurnLimit_OK(t *testing.T) {
tests := []struct {
validatorCount int
wantedChurn uint64
}{
{validatorCount: 1000, wantedChurn: 4},
{validatorCount: 100000, wantedChurn: 4},
{validatorCount: 1000000, wantedChurn: 15 /* validatorCount/churnLimitQuotient */},
{validatorCount: 2000000, wantedChurn: 30 /* validatorCount/churnLimitQuotient */},
}
for _, test := range tests {
ClearCache()
validators := make([]*ethpb.Validator, test.validatorCount)
for i := 0; i < len(validators); i++ {
validators[i] = &ethpb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
beaconState, err := beaconstate.InitializeFromProto(&pb.BeaconState{
Slot: 1,
Validators: validators,
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
})
require.NoError(t, err)
validatorCount, err := ActiveValidatorCount(beaconState, CurrentEpoch(beaconState))
require.NoError(t, err)
resultChurn, err := ValidatorChurnLimit(validatorCount)
require.NoError(t, err)
assert.Equal(t, test.wantedChurn, resultChurn, "ValidatorChurnLimit(%d)", test.validatorCount)
}
}
func TestDomain_OK(t *testing.T) {
state := &pb.BeaconState{
Fork: &pb.Fork{
Epoch: 3,
PreviousVersion: []byte{0, 0, 0, 2},
CurrentVersion: []byte{0, 0, 0, 3},
},
}
tests := []struct {
epoch uint64
domainType [4]byte
result []byte
}{
{epoch: 1, domainType: bytesutil.ToBytes4(bytesutil.Bytes4(4)), result: bytesutil.ToBytes(947067381421703172, 32)},
{epoch: 2, domainType: bytesutil.ToBytes4(bytesutil.Bytes4(4)), result: bytesutil.ToBytes(947067381421703172, 32)},
{epoch: 2, domainType: bytesutil.ToBytes4(bytesutil.Bytes4(5)), result: bytesutil.ToBytes(947067381421703173, 32)},
{epoch: 3, domainType: bytesutil.ToBytes4(bytesutil.Bytes4(4)), result: bytesutil.ToBytes(9369798235163459588, 32)},
{epoch: 3, domainType: bytesutil.ToBytes4(bytesutil.Bytes4(5)), result: bytesutil.ToBytes(9369798235163459589, 32)},
}
for _, tt := range tests {
domain, err := Domain(state.Fork, tt.epoch, tt.domainType, nil)
require.NoError(t, err)
assert.DeepEqual(t, tt.result[:8], domain[:8], "Unexpected domain version")
}
}
// Test basic functionality of ActiveValidatorIndices without caching. This test will need to be
// rewritten when releasing some cache flag.
func TestActiveValidatorIndices(t *testing.T) {
farFutureEpoch := params.BeaconConfig().FarFutureEpoch
type args struct {
state *pb.BeaconState
epoch uint64
}
tests := []struct {
name string
args args
want []uint64
wantErr bool
}{
{
name: "all_active_epoch_10",
args: args{
state: &pb.BeaconState{
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
Validators: []*ethpb.Validator{
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
},
},
epoch: 10,
},
want: []uint64{0, 1, 2},
},
{
name: "some_active_epoch_10",
args: args{
state: &pb.BeaconState{
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
Validators: []*ethpb.Validator{
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
{
ActivationEpoch: 0,
ExitEpoch: 1,
},
},
},
epoch: 10,
},
want: []uint64{0, 1},
},
{
name: "some_active_with_recent_new_epoch_10",
args: args{
state: &pb.BeaconState{
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
Validators: []*ethpb.Validator{
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
{
ActivationEpoch: 0,
ExitEpoch: 1,
},
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
},
},
epoch: 10,
},
want: []uint64{0, 1, 3},
},
{
name: "some_active_with_recent_new_epoch_10",
args: args{
state: &pb.BeaconState{
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
Validators: []*ethpb.Validator{
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
{
ActivationEpoch: 0,
ExitEpoch: 1,
},
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
},
},
epoch: 10,
},
want: []uint64{0, 1, 3},
},
{
name: "some_active_with_recent_new_epoch_10",
args: args{
state: &pb.BeaconState{
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
Validators: []*ethpb.Validator{
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
{
ActivationEpoch: 0,
ExitEpoch: 1,
},
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
},
},
epoch: 10,
},
want: []uint64{0, 2, 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, err := beaconstate.InitializeFromProto(tt.args.state)
require.NoError(t, err)
got, err := ActiveValidatorIndices(s, tt.args.epoch)
if (err != nil) != tt.wantErr {
t.Errorf("ActiveValidatorIndices() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.DeepEqual(t, tt.want, got, "ActiveValidatorIndices()")
ClearCache()
})
}
}
func TestComputeProposerIndex(t *testing.T) {
seed := bytesutil.ToBytes32([]byte("seed"))
type args struct {
validators []*ethpb.Validator
indices []uint64
seed [32]byte
}
tests := []struct {
name string
args args
want uint64
wantErr bool
}{
{
name: "all_active_indices",
args: args{
validators: []*ethpb.Validator{
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
},
indices: []uint64{0, 1, 2, 3, 4},
seed: seed,
},
want: 2,
},
{ // Regression test for https://github.com/prysmaticlabs/prysm/issues/4259.
name: "1_active_index",
args: args{
validators: []*ethpb.Validator{
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
},
indices: []uint64{3},
seed: seed,
},
want: 3,
},
{
name: "empty_active_indices",
args: args{
validators: []*ethpb.Validator{
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
},
indices: []uint64{},
seed: seed,
},
wantErr: true,
},
{
name: "active_indices_out_of_range",
args: args{
validators: []*ethpb.Validator{
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
},
indices: []uint64{100},
seed: seed,
},
wantErr: true,
},
{
name: "second_half_active",
args: args{
validators: []*ethpb.Validator{
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
},
indices: []uint64{5, 6, 7, 8, 9},
seed: seed,
},
want: 7,
},
{
name: "nil_validator",
args: args{
validators: []*ethpb.Validator{
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
nil, // Should never happen, but would cause a panic when it does happen.
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
},
indices: []uint64{0, 1, 2, 3, 4},
seed: seed,
},
want: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bState := &pb.BeaconState{Validators: tt.args.validators}
stTrie, err := beaconstate.InitializeFromProtoUnsafe(bState)
if err != nil {
t.Error(err)
return
}
got, err := ComputeProposerIndex(stTrie, tt.args.indices, tt.args.seed)
if (err != nil) != tt.wantErr {
t.Errorf("ComputeProposerIndex() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.want, got, "ComputeProposerIndex()")
})
}
}
func TestIsEligibleForActivationQueue(t *testing.T) {
tests := []struct {
name string
validator *ethpb.Validator
want bool
}{
{"Eligible",
&ethpb.Validator{ActivationEligibilityEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
true},
{"Incorrect activation eligibility epoch",
&ethpb.Validator{ActivationEligibilityEpoch: 1, EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
false},
{"Not enough balance",
&ethpb.Validator{ActivationEligibilityEpoch: params.BeaconConfig().FarFutureEpoch, EffectiveBalance: 1},
false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, IsEligibleForActivationQueue(tt.validator), "IsEligibleForActivationQueue()")
})
}
}
func TestIsIsEligibleForActivation(t *testing.T) {
tests := []struct {
name string
validator *ethpb.Validator
state *pb.BeaconState
want bool
}{
{"Eligible",
&ethpb.Validator{ActivationEligibilityEpoch: 1, ActivationEpoch: params.BeaconConfig().FarFutureEpoch},
&pb.BeaconState{FinalizedCheckpoint: &ethpb.Checkpoint{Epoch: 2}},
true},
{"Not yet finalized",
&ethpb.Validator{ActivationEligibilityEpoch: 1, ActivationEpoch: params.BeaconConfig().FarFutureEpoch},
&pb.BeaconState{FinalizedCheckpoint: &ethpb.Checkpoint{Root: make([]byte, 32)}},
false},
{"Incorrect activation epoch",
&ethpb.Validator{ActivationEligibilityEpoch: 1},
&pb.BeaconState{FinalizedCheckpoint: &ethpb.Checkpoint{Epoch: 2}},
false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, err := beaconstate.InitializeFromProto(tt.state)
require.NoError(t, err)
assert.Equal(t, tt.want, IsEligibleForActivation(s, tt.validator), "IsEligibleForActivation()")
})
}
}