Skip to content

Commit

Permalink
Remove Deprecated Validator Protobuf (#2727)
Browse files Browse the repository at this point in the history
* Remove deprecated validator protos

* Fix to comments
  • Loading branch information
0xKiwi authored and nisdas committed May 31, 2019
1 parent 085962a commit 2fb1f3d
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 354 deletions.
24 changes: 22 additions & 2 deletions beacon-chain/chaintest/backend/simulated_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,40 @@ func (sb *SimulatedBackend) compareTestCase(testCase *StateTestCase) error {
)
}
for _, slashed := range testCase.Results.SlashedValidators {
if sb.state.ValidatorRegistry[slashed].SlashedEpoch == params.BeaconConfig().FarFutureEpoch {
if !sb.state.ValidatorRegistry[slashed].Slashed {
return fmt.Errorf(
"expected validator at index %d to have been slashed",
slashed,
)
}
if sb.state.ValidatorRegistry[slashed].ExitEpoch != params.BeaconConfig().FarFutureEpoch {
return fmt.Errorf(
"expected validator at index %d to have exited",
slashed,
)
}
if sb.state.ValidatorRegistry[slashed].WithdrawableEpoch != params.BeaconConfig().FarFutureEpoch {
return fmt.Errorf(
"expected validator at index %d withdrawable epoch to have been changed, received: %d",
slashed,
sb.state.ValidatorRegistry[slashed].WithdrawableEpoch,
)
}
}
for _, exited := range testCase.Results.ExitedValidators {
if sb.state.ValidatorRegistry[exited].StatusFlags != pb.Validator_INITIATED_EXIT {
if sb.state.ValidatorRegistry[exited].ExitEpoch != params.BeaconConfig().FarFutureEpoch {
return fmt.Errorf(
"expected validator at index %d to have exited",
exited,
)
}
if sb.state.ValidatorRegistry[exited].WithdrawableEpoch != params.BeaconConfig().FarFutureEpoch {
return fmt.Errorf(
"expected validator at index %d withdrawable epoch to have been changed, received: %d",
exited,
sb.state.ValidatorRegistry[exited].WithdrawableEpoch,
)
}
}
return nil
}
Expand Down
1 change: 0 additions & 1 deletion beacon-chain/core/blocks/block_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ func TestProcessProposerSlashings_AppliesCorrectStatus(t *testing.T) {
EffectiveBalance: params.BeaconConfig().MaxDepositAmount,
Slashed: false,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
SlashedEpoch: 1,
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
ActivationEpoch: 0,
}
Expand Down
12 changes: 7 additions & 5 deletions beacon-chain/core/epoch/epoch_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,15 +1214,16 @@ func TestProcessRegistryUpdates_ActivationCompletes(t *testing.T) {
func TestProcessRegistryUpdates_CanExits(t *testing.T) {
epoch := uint64(5)
exitEpoch := helpers.DelayedActivationExitEpoch(epoch)
minWithdrawalDelay := params.BeaconConfig().MinValidatorWithdrawalDelay
state := &pb.BeaconState{
Slot: epoch * params.BeaconConfig().SlotsPerEpoch,
ValidatorRegistry: []*pb.Validator{
{
ExitEpoch: exitEpoch,
StatusFlags: pb.Validator_INITIATED_EXIT},
ExitEpoch: exitEpoch,
WithdrawableEpoch: exitEpoch + minWithdrawalDelay},
{
ExitEpoch: exitEpoch,
StatusFlags: pb.Validator_INITIATED_EXIT},
ExitEpoch: exitEpoch,
WithdrawableEpoch: exitEpoch + minWithdrawalDelay},
},
Balances: []uint64{
params.BeaconConfig().MaxDepositAmount,
Expand All @@ -1235,7 +1236,8 @@ func TestProcessRegistryUpdates_CanExits(t *testing.T) {
t.Errorf("could not update registry %d, wanted exit slot %d got %d",
i,
exitEpoch,
validator.ExitEpoch)
validator.ExitEpoch,
)
}
}
}
Expand Down
23 changes: 0 additions & 23 deletions beacon-chain/core/validators/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func ProcessDeposit(
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
Slashed: false,
StatusFlags: 0,
WithdrawalCredentials: withdrawalCredentials,
EffectiveBalance: amount,
}
Expand Down Expand Up @@ -357,25 +356,3 @@ func maxBalanceChurn(totalBalance uint64) uint64 {
}
return params.BeaconConfig().MaxDepositAmount
}

// eligibleToExit checks if a validator is eligible to exit whether it was
// slashed or not.
//
// Spec pseudocode definition:
// def eligible(index):
// validator = state.validator_registry[index]
// if validator.slashed_epoch <= current_epoch:
// slashed_withdrawal_epochs = LATEST_PENALIZED_EXIT_LENGTH // 2
// return current_epoch >= validator.slashed_epoch + slashd_withdrawal_epochs
// else:
// return current_epoch >= validator.exit_epoch + MIN_VALIDATOR_WITHDRAWAL_DELAY
func eligibleToExit(state *pb.BeaconState, idx uint64) bool {
currentEpoch := helpers.CurrentEpoch(state)
validator := state.ValidatorRegistry[idx]

if validator.SlashedEpoch <= currentEpoch {
slashedWithdrawalEpochs := params.BeaconConfig().LatestSlashedExitLength / 2
return currentEpoch >= validator.SlashedEpoch+slashedWithdrawalEpochs
}
return currentEpoch >= validator.ExitEpoch+params.BeaconConfig().MinValidatorWithdrawalDelay
}
23 changes: 0 additions & 23 deletions beacon-chain/core/validators/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,29 +457,6 @@ func TestExitValidator_AlreadyExited(t *testing.T) {
}
}

func TestEligibleToExit_OK(t *testing.T) {
state := &pb.BeaconState{
Slot: 1,
ValidatorRegistry: []*pb.Validator{
{ExitEpoch: params.BeaconConfig().ActivationExitDelay},
},
}
if eligibleToExit(state, 0) {
t.Error("eligible to exit should be true but got false")
}

state = &pb.BeaconState{
Slot: params.BeaconConfig().MinValidatorWithdrawalDelay,
ValidatorRegistry: []*pb.Validator{
{ExitEpoch: params.BeaconConfig().ActivationExitDelay,
SlashedEpoch: 1},
},
}
if eligibleToExit(state, 0) {
t.Error("eligible to exit should be true but got false")
}
}

func TestMaxBalanceChurn_OK(t *testing.T) {
maxDepositAmount := params.BeaconConfig().MaxDepositAmount
tests := []struct {
Expand Down
12 changes: 9 additions & 3 deletions beacon-chain/db/state_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ func reportStateMetrics(state *pb.BeaconState) {
strconv.Itoa(i), //Validator index
).Set(float64(v.ExitEpoch))
// Track individual Validator's slashed epochs
validatorSlashedGauge.WithLabelValues(
strconv.Itoa(i), //Validator index
).Set(float64(v.SlashedEpoch))
if v.Slashed {
validatorSlashedGauge.WithLabelValues(
strconv.Itoa(i), //Validator index
).Set(float64(v.WithdrawableEpoch - params.BeaconConfig().LatestSlashedExitLength))
} else {
validatorSlashedGauge.WithLabelValues(
strconv.Itoa(i), //Validator index
).Set(float64(params.BeaconConfig().FarFutureEpoch))
}
// Total number of active validators
if v.ActivationEpoch <= currentEpoch && currentEpoch < v.ExitEpoch {
active++
Expand Down
31 changes: 14 additions & 17 deletions beacon-chain/rpc/validator_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (vs *ValidatorServer) assignment(
if err != nil {
return nil, err
}
status := vs.lookupValidatorStatusFlag(idx, beaconState)
status := vs.lookupValidatorStatus(idx, beaconState)
return &pb.CommitteeAssignmentResponse_CommitteeAssignment{
Committee: committee,
Shard: shard,
Expand Down Expand Up @@ -337,6 +337,7 @@ func (vs *ValidatorServer) validatorStatus(
}

currEpoch := helpers.CurrentEpoch(beaconState)
activationEpoch := params.BeaconConfig().FarFutureEpoch
var validatorInState *pbp2p.Validator
var validatorIndex uint64
for idx, val := range beaconState.ValidatorRegistry {
Expand All @@ -346,12 +347,7 @@ func (vs *ValidatorServer) validatorStatus(

if bytes.Equal(val.Pubkey, pubKey) {
if helpers.IsActiveValidator(val, currEpoch) {
return &pb.ValidatorStatusResponse{
Status: pb.ValidatorStatus_ACTIVE,
ActivationEpoch: val.ActivationEpoch,
Eth1DepositBlockNumber: eth1BlockNumBigInt.Uint64(),
DepositInclusionSlot: depositBlockSlot,
}
activationEpoch = val.ActivationEpoch
}
validatorInState = val
validatorIndex = uint64(idx)
Expand All @@ -373,33 +369,34 @@ func (vs *ValidatorServer) validatorStatus(
positionInQueue = validatorIndex - lastActivatedValidatorIdx
}

status := vs.lookupValidatorStatusFlag(uint64(valIdx), beaconState)
status := vs.lookupValidatorStatus(uint64(valIdx), beaconState)
return &pb.ValidatorStatusResponse{
Status: status,
Eth1DepositBlockNumber: eth1BlockNumBigInt.Uint64(),
PositionInActivationQueue: positionInQueue,
DepositInclusionSlot: depositBlockSlot,
ActivationEpoch: params.BeaconConfig().FarFutureEpoch,
ActivationEpoch: activationEpoch,
}
}

func (vs *ValidatorServer) lookupValidatorStatusFlag(validatorIdx uint64, beaconState *pbp2p.BeaconState) pb.ValidatorStatus {
func (vs *ValidatorServer) lookupValidatorStatus(validatorIdx uint64, beaconState *pbp2p.BeaconState) pb.ValidatorStatus {
var status pb.ValidatorStatus
v := beaconState.ValidatorRegistry[validatorIdx]
epoch := helpers.CurrentEpoch(beaconState)
farFutureEpoch := params.BeaconConfig().FarFutureEpoch

if v.ActivationEpoch > epoch {
if epoch < v.ActivationEpoch {
status = pb.ValidatorStatus_PENDING_ACTIVE
} else if epoch >= v.ActivationEpoch && epoch < v.ExitEpoch {
} else if v.ExitEpoch == farFutureEpoch {
status = pb.ValidatorStatus_ACTIVE
} else if v.StatusFlags == pbp2p.Validator_INITIATED_EXIT {
status = pb.ValidatorStatus_INITIATED_EXIT
} else if v.StatusFlags == pbp2p.Validator_WITHDRAWABLE {
} else if epoch >= v.WithdrawableEpoch {
status = pb.ValidatorStatus_WITHDRAWABLE
} else if epoch >= v.ExitEpoch && epoch >= v.SlashedEpoch {
} else if v.Slashed && epoch >= v.ExitEpoch {
status = pb.ValidatorStatus_EXITED_SLASHED
} else if epoch >= v.ExitEpoch {
status = pb.ValidatorStatus_EXITED
} else if v.ExitEpoch != farFutureEpoch {
status = pb.ValidatorStatus_INITIATED_EXIT
} else {
status = pb.ValidatorStatus_UNKNOWN_STATUS
}
Expand Down Expand Up @@ -438,7 +435,7 @@ func (vs *ValidatorServer) addNonActivePublicKeysAssignmentStatus(
for _, pk := range pubkeys {
hexPk := bytesutil.ToBytes32(pk)
if valIdx, ok := validatorMap[hexPk]; !ok || !helpers.IsActiveValidator(beaconState.ValidatorRegistry[validatorMap[hexPk]], currentEpoch) {
status := vs.lookupValidatorStatusFlag(uint64(valIdx), beaconState) //nolint:gosec
status := vs.lookupValidatorStatus(uint64(valIdx), beaconState) //nolint:gosec
if !ok {
status = pb.ValidatorStatus_UNKNOWN_STATUS
}
Expand Down
61 changes: 40 additions & 21 deletions beacon-chain/rpc/validator_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,18 @@ func TestValidatorStatus_InitiatedExit(t *testing.T) {
t.Fatalf("Could not save validator index: %v", err)
}

// Initiated exit because validator status flag = Validator_INITIATED_EXIT.
// Initiated exit because validator exit epoch and withdrawable epoch are not FAR_FUTURE_EPOCH
slot := uint64(10000)
epoch := helpers.SlotToEpoch(slot)
exitEpoch := helpers.DelayedActivationExitEpoch(epoch)
withdrawableEpoch := exitEpoch + params.BeaconConfig().MinValidatorWithdrawalDelay
if err := db.SaveState(ctx, &pbp2p.BeaconState{
Slot: 10000,
Slot: slot,
ValidatorRegistry: []*pbp2p.Validator{{
StatusFlags: pbp2p.Validator_INITIATED_EXIT,
Pubkey: pubKey},
Pubkey: pubKey,
ActivationEpoch: 0,
ExitEpoch: exitEpoch,
WithdrawableEpoch: withdrawableEpoch},
}}); err != nil {
t.Fatalf("could not save state: %v", err)
}
Expand Down Expand Up @@ -478,12 +484,15 @@ func TestValidatorStatus_Withdrawable(t *testing.T) {
t.Fatalf("Could not save validator index: %v", err)
}

// Withdrawable exit because validator status flag = Validator_WITHDRAWABLE.
// Withdrawable exit because current epoch is after validator withdrawable epoch.
slot := uint64(10000)
epoch := helpers.SlotToEpoch(slot)
if err := db.SaveState(ctx, &pbp2p.BeaconState{
Slot: 10000,
ValidatorRegistry: []*pbp2p.Validator{{
StatusFlags: pbp2p.Validator_WITHDRAWABLE,
Pubkey: pubKey},
WithdrawableEpoch: epoch - 1,
ExitEpoch: epoch - 2,
Pubkey: pubKey},
}}); err != nil {
t.Fatalf("could not save state: %v", err)
}
Expand Down Expand Up @@ -528,11 +537,15 @@ func TestValidatorStatus_ExitedSlashed(t *testing.T) {
t.Fatalf("Could not save validator index: %v", err)
}

// Exit slashed because exit epoch and slashed epoch are =< current epoch.
// Exit slashed because slashed is true, exit epoch is =< current epoch and withdrawable epoch > epoch .
slot := uint64(10000)
epoch := helpers.SlotToEpoch(slot)
if err := db.SaveState(ctx, &pbp2p.BeaconState{
Slot: 10000,
Slot: slot,
ValidatorRegistry: []*pbp2p.Validator{{
Pubkey: pubKey},
Slashed: true,
Pubkey: pubKey,
WithdrawableEpoch: epoch + 1},
}}); err != nil {
t.Fatalf("could not save state: %v", err)
}
Expand Down Expand Up @@ -578,11 +591,13 @@ func TestValidatorStatus_Exited(t *testing.T) {
}

// Exit because only exit epoch is =< current epoch.
slot := uint64(10000)
epoch := helpers.SlotToEpoch(slot)
if err := db.SaveState(ctx, &pbp2p.BeaconState{
Slot: 10000,
Slot: slot,
ValidatorRegistry: []*pbp2p.Validator{{
Pubkey: pubKey,
SlashedEpoch: params.BeaconConfig().FarFutureEpoch},
Pubkey: pubKey,
WithdrawableEpoch: epoch + 1},
}}); err != nil {
t.Fatalf("could not save state: %v", err)
}
Expand Down Expand Up @@ -725,14 +740,17 @@ func TestWaitForActivation_ValidatorOriginallyExists(t *testing.T) {

beaconState := &pbp2p.BeaconState{
Slot: 4000,
ValidatorRegistry: []*pbp2p.Validator{{
ActivationEpoch: 0,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
Pubkey: pubKeys[0]},
ValidatorRegistry: []*pbp2p.Validator{
{
ActivationEpoch: 0,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
Pubkey: pubKeys[1]},
Pubkey: pubKeys[0],
},
{
ActivationEpoch: 0,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
Pubkey: pubKeys[1],
},
},
}
if err := db.SaveState(ctx, beaconState); err != nil {
Expand Down Expand Up @@ -775,9 +793,10 @@ func TestWaitForActivation_ValidatorOriginallyExists(t *testing.T) {
Statuses: []*pb.ValidatorActivationResponse_Status{
{PublicKey: []byte{'A'},
Status: &pb.ValidatorStatusResponse{
Status: pb.ValidatorStatus_ACTIVE,
Eth1DepositBlockNumber: 10,
DepositInclusionSlot: 3413,
Status: pb.ValidatorStatus_ACTIVE,
Eth1DepositBlockNumber: 10,
DepositInclusionSlot: 3413,
PositionInActivationQueue: params.BeaconConfig().FarFutureEpoch,
},
},
{PublicKey: []byte{'B'},
Expand Down
Loading

0 comments on commit 2fb1f3d

Please sign in to comment.