2020-07-06 22:27:42 +00:00
|
|
|
package blocks_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-16 07:45:34 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2020-07-06 22:27:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2021-09-27 16:19:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/signing"
|
2021-09-30 19:00:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/time"
|
2021-07-19 18:26:39 +00:00
|
|
|
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/bls"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
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,
|
|
|
|
},
|
|
|
|
}
|
2021-07-29 21:45:17 +00:00
|
|
|
state, err := v1.InitializeFromProto(ðpb.BeaconState{
|
2020-07-06 22:27:42 +00:00
|
|
|
Validators: registry,
|
|
|
|
Slot: 10,
|
|
|
|
})
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2021-09-23 18:53:46 +00:00
|
|
|
b := util.NewBeaconBlock()
|
2020-09-01 01:29:27 +00:00
|
|
|
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"
|
2021-04-15 13:58:54 +00:00
|
|
|
_, err = blocks.ProcessVoluntaryExits(context.Background(), state, b.Block.Body.VoluntaryExits)
|
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,
|
|
|
|
},
|
|
|
|
}
|
2021-07-29 21:45:17 +00:00
|
|
|
state, err := v1.InitializeFromProto(ðpb.BeaconState{
|
2020-09-10 14:49:13 +00:00
|
|
|
Validators: registry,
|
|
|
|
Slot: 0,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-09-23 18:53:46 +00:00
|
|
|
b := util.NewBeaconBlock()
|
2020-09-10 14:49:13 +00:00
|
|
|
b.Block = ðpb.BeaconBlock{
|
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
VoluntaryExits: exits,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-11 11:55:33 +00:00
|
|
|
want := "validator with index 0 has already submitted an exit, which will take place at epoch: 10"
|
2021-04-15 13:58:54 +00:00
|
|
|
_, err = blocks.ProcessVoluntaryExits(context.Background(), state, b.Block.Body.VoluntaryExits)
|
2020-09-10 14:49:13 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
2021-07-29 21:45:17 +00:00
|
|
|
state, err := v1.InitializeFromProto(ðpb.BeaconState{
|
2020-07-06 22:27:42 +00:00
|
|
|
Validators: registry,
|
2021-07-29 21:45:17 +00:00
|
|
|
Fork: ðpb.Fork{
|
2020-07-06 22:27:42 +00:00
|
|
|
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)
|
2021-02-16 07:45:34 +00:00
|
|
|
err = state.SetSlot(state.Slot() + params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().ShardCommitteePeriod)))
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:27:42 +00:00
|
|
|
|
2020-10-30 19:06:33 +00:00
|
|
|
priv, err := bls.RandKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-06 22:27:42 +00:00
|
|
|
val, err := state.ValidatorAtIndex(0)
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-23 16:14:34 +00:00
|
|
|
val.PublicKey = priv.PublicKey().Marshal()
|
2020-08-08 19:06:04 +00:00
|
|
|
require.NoError(t, state.UpdateValidatorAtIndex(0, val))
|
2021-09-30 19:00:14 +00:00
|
|
|
exits[0].Signature, err = signing.ComputeDomainAndSign(state, time.CurrentEpoch(state), exits[0].Exit, params.BeaconConfig().DomainVoluntaryExit, priv)
|
2020-07-30 19:11:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
b := util.NewBeaconBlock()
|
2020-09-01 01:29:27 +00:00
|
|
|
b.Block = ðpb.BeaconBlock{
|
2020-07-06 22:27:42 +00:00
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
VoluntaryExits: exits,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:58:54 +00:00
|
|
|
newState, err := blocks.ProcessVoluntaryExits(context.Background(), state, b.Block.Body.VoluntaryExits)
|
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()
|
2021-02-09 10:05:22 +00:00
|
|
|
if newRegistry[0].ExitEpoch != helpers.ActivationExitEpoch(types.Epoch(state.Slot()/params.BeaconConfig().SlotsPerEpoch)) {
|
2020-07-06 22:27:42 +00:00
|
|
|
t.Errorf("Expected validator exit epoch to be %d, got %d",
|
2021-02-09 10:05:22 +00:00
|
|
|
helpers.ActivationExitEpoch(types.Epoch(state.Slot()/params.BeaconConfig().SlotsPerEpoch)), newRegistry[0].ExitEpoch)
|
2020-07-06 22:27:42 +00:00
|
|
|
}
|
|
|
|
}
|