2020-07-06 22:27:42 +00:00
|
|
|
package blocks_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-09-01 01:29:27 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-08-08 19:06:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2020-07-30 19:11:22 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-07-06 22:27:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProcessVoluntaryExits_ValidatorNotActive(t *testing.T) {
|
|
|
|
exits := []*ethpb.SignedVoluntaryExit{
|
|
|
|
{
|
|
|
|
Exit: ðpb.VoluntaryExit{
|
|
|
|
ValidatorIndex: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
registry := []*ethpb.Validator{
|
|
|
|
{
|
|
|
|
ExitEpoch: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
state, err := stateTrie.InitializeFromProto(&pb.BeaconState{
|
|
|
|
Validators: registry,
|
|
|
|
})
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-01 01:29:27 +00:00
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block = ðpb.BeaconBlock{
|
2020-07-06 22:27:42 +00:00
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
VoluntaryExits: exits,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
want := "non-active validator cannot exit"
|
2020-09-01 01:29:27 +00:00
|
|
|
_, err = blocks.ProcessVoluntaryExits(context.Background(), state, b)
|
2020-08-08 19:06:04 +00:00
|
|
|
assert.ErrorContains(t, want, err)
|
2020-08-13 14:37:38 +00:00
|
|
|
|
|
|
|
// Check conformance of no verify method.
|
2020-09-01 01:29:27 +00:00
|
|
|
_, err = blocks.ProcessVoluntaryExitsNoVerifySignature(state, b.Block.Body)
|
2020-08-13 14:37:38 +00:00
|
|
|
assert.ErrorContains(t, want, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessVoluntaryExits_InvalidExitEpoch(t *testing.T) {
|
|
|
|
exits := []*ethpb.SignedVoluntaryExit{
|
|
|
|
{
|
|
|
|
Exit: ðpb.VoluntaryExit{
|
|
|
|
Epoch: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
registry := []*ethpb.Validator{
|
|
|
|
{
|
|
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
state, err := stateTrie.InitializeFromProto(&pb.BeaconState{
|
|
|
|
Validators: registry,
|
|
|
|
Slot: 0,
|
|
|
|
})
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-01 01:29:27 +00:00
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block = ðpb.BeaconBlock{
|
2020-07-06 22:27:42 +00:00
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
VoluntaryExits: exits,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
want := "expected current epoch >= exit epoch"
|
2020-09-01 01:29:27 +00:00
|
|
|
_, err = blocks.ProcessVoluntaryExits(context.Background(), state, b)
|
2020-08-08 19:06:04 +00:00
|
|
|
assert.ErrorContains(t, want, err)
|
2020-08-13 14:37:38 +00:00
|
|
|
|
|
|
|
// Check conformance of no verify method.
|
2020-09-01 01:29:27 +00:00
|
|
|
_, err = blocks.ProcessVoluntaryExitsNoVerifySignature(state, b.Block.Body)
|
2020-08-13 14:37:38 +00:00
|
|
|
assert.ErrorContains(t, want, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessVoluntaryExits_NotActiveLongEnoughToExit(t *testing.T) {
|
|
|
|
exits := []*ethpb.SignedVoluntaryExit{
|
|
|
|
{
|
|
|
|
Exit: ðpb.VoluntaryExit{
|
|
|
|
ValidatorIndex: 0,
|
|
|
|
Epoch: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
registry := []*ethpb.Validator{
|
|
|
|
{
|
|
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
state, err := stateTrie.InitializeFromProto(&pb.BeaconState{
|
|
|
|
Validators: registry,
|
|
|
|
Slot: 10,
|
|
|
|
})
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-01 01:29:27 +00:00
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block = ðpb.BeaconBlock{
|
2020-07-06 22:27:42 +00:00
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
VoluntaryExits: exits,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
want := "validator has not been active long enough to exit"
|
2020-09-01 01:29:27 +00:00
|
|
|
_, err = blocks.ProcessVoluntaryExits(context.Background(), state, b)
|
2020-08-08 19:06:04 +00:00
|
|
|
assert.ErrorContains(t, want, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 14:49:13 +00:00
|
|
|
func TestProcessVoluntaryExits_ExitAlreadySubmitted(t *testing.T) {
|
|
|
|
exits := []*ethpb.SignedVoluntaryExit{
|
|
|
|
{
|
|
|
|
Exit: ðpb.VoluntaryExit{
|
|
|
|
Epoch: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
registry := []*ethpb.Validator{
|
|
|
|
{
|
|
|
|
ExitEpoch: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
state, err := stateTrie.InitializeFromProto(&pb.BeaconState{
|
|
|
|
Validators: registry,
|
|
|
|
Slot: 0,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block = ðpb.BeaconBlock{
|
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
VoluntaryExits: exits,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
want := "validator has already submitted an exit, which will take place at epoch: 10"
|
|
|
|
_, err = blocks.ProcessVoluntaryExits(context.Background(), state, b)
|
|
|
|
assert.ErrorContains(t, want, err)
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:27:42 +00:00
|
|
|
func TestProcessVoluntaryExits_AppliesCorrectStatus(t *testing.T) {
|
|
|
|
exits := []*ethpb.SignedVoluntaryExit{
|
|
|
|
{
|
|
|
|
Exit: ðpb.VoluntaryExit{
|
|
|
|
ValidatorIndex: 0,
|
|
|
|
Epoch: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
registry := []*ethpb.Validator{
|
|
|
|
{
|
|
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
|
|
ActivationEpoch: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
state, err := stateTrie.InitializeFromProto(&pb.BeaconState{
|
|
|
|
Validators: registry,
|
|
|
|
Fork: &pb.Fork{
|
|
|
|
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
},
|
|
|
|
Slot: params.BeaconConfig().SlotsPerEpoch * 5,
|
|
|
|
})
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
err = state.SetSlot(state.Slot() + (params.BeaconConfig().ShardCommitteePeriod * params.BeaconConfig().SlotsPerEpoch))
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
|
|
|
|
priv := bls.RandKey()
|
|
|
|
val, err := state.ValidatorAtIndex(0)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
val.PublicKey = priv.PublicKey().Marshal()[:]
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, state.UpdateValidatorAtIndex(0, val))
|
2020-07-30 19:11:22 +00:00
|
|
|
exits[0].Signature, err = helpers.ComputeDomainAndSign(state, helpers.CurrentEpoch(state), exits[0].Exit, params.BeaconConfig().DomainVoluntaryExit, priv)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-01 01:29:27 +00:00
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block = ðpb.BeaconBlock{
|
2020-07-06 22:27:42 +00:00
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
VoluntaryExits: exits,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-13 14:37:38 +00:00
|
|
|
stateCopy := state.Copy()
|
2020-09-01 01:29:27 +00:00
|
|
|
newState, err := blocks.ProcessVoluntaryExits(context.Background(), state, b)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err, "Could not process exits")
|
2020-07-06 22:27:42 +00:00
|
|
|
newRegistry := newState.Validators()
|
|
|
|
if newRegistry[0].ExitEpoch != helpers.ActivationExitEpoch(state.Slot()/params.BeaconConfig().SlotsPerEpoch) {
|
|
|
|
t.Errorf("Expected validator exit epoch to be %d, got %d",
|
|
|
|
helpers.ActivationExitEpoch(state.Slot()/params.BeaconConfig().SlotsPerEpoch), newRegistry[0].ExitEpoch)
|
|
|
|
}
|
2020-08-13 14:37:38 +00:00
|
|
|
|
|
|
|
// Check conformance with NoVerify Exit Method.
|
2020-09-01 01:29:27 +00:00
|
|
|
newState, err = blocks.ProcessVoluntaryExitsNoVerifySignature(stateCopy, b.Block.Body)
|
2020-08-13 14:37:38 +00:00
|
|
|
require.NoError(t, err, "Could not process exits")
|
|
|
|
newRegistry = newState.Validators()
|
|
|
|
if newRegistry[0].ExitEpoch != helpers.ActivationExitEpoch(stateCopy.Slot()/params.BeaconConfig().SlotsPerEpoch) {
|
|
|
|
t.Errorf("Expected validator exit epoch to be %d, got %d",
|
|
|
|
helpers.ActivationExitEpoch(stateCopy.Slot()/params.BeaconConfig().SlotsPerEpoch), newRegistry[0].ExitEpoch)
|
|
|
|
}
|
2020-07-06 22:27:42 +00:00
|
|
|
}
|