Skip to content

Commit

Permalink
Tidy up validator state logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Dec 4, 2023
1 parent cddc7a4 commit 9d4133d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 20 deletions.
30 changes: 11 additions & 19 deletions api/v1/validatorstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,42 +161,34 @@ func ValidatorToState(validator *phase0.Validator,
return ValidatorStateUnknown
}

if validator.ActivationEpoch > currentEpoch {
switch {
case validator.ActivationEpoch > currentEpoch:
// Pending.
if validator.ActivationEligibilityEpoch == farFutureEpoch {
return ValidatorStatePendingInitialized
}

return ValidatorStatePendingQueued
}

if validator.ActivationEpoch <= currentEpoch && currentEpoch < validator.ExitEpoch {
// Active.
if validator.ExitEpoch == farFutureEpoch {
return ValidatorStateActiveOngoing
}
case validator.ExitEpoch == farFutureEpoch:
// Active ongoing.
return ValidatorStateActiveOngoing
case validator.ExitEpoch > currentEpoch:
// Active exiting.
if validator.Slashed {
return ValidatorStateActiveSlashed
}

return ValidatorStateActiveExiting
}

if validator.ExitEpoch <= currentEpoch && currentEpoch < validator.WithdrawableEpoch {
case validator.WithdrawableEpoch > currentEpoch:
// Exited.
if validator.Slashed {
return ValidatorStateExitedSlashed
}

return ValidatorStateExitedUnslashed
}

// Withdrawable.
if balance != nil && *balance == 0 {
// We have a definite balance of 0.
case balance != nil && *balance == 0:
return ValidatorStateWithdrawalDone
default:
return ValidatorStateWithdrawalPossible
}

// No balance information, or balance > 0.
return ValidatorStateWithdrawalPossible
}
64 changes: 63 additions & 1 deletion api/v1/validatorstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ func TestValidatorToState(t *testing.T) {
},
state: api.ValidatorStatePendingQueued,
},
{
name: "ActiveOngoingNext",
validator: &phase0.Validator{
ActivationEligibilityEpoch: currentEpoch - 50,
ActivationEpoch: currentEpoch + 1,
ExitEpoch: farFutureEpoch,
WithdrawableEpoch: farFutureEpoch,
},
state: api.ValidatorStatePendingQueued,
},
{
name: "ActiveOngoingThis",
validator: &phase0.Validator{
ActivationEligibilityEpoch: currentEpoch - 50,
ActivationEpoch: currentEpoch,
ExitEpoch: farFutureEpoch,
WithdrawableEpoch: farFutureEpoch,
},
state: api.ValidatorStateActiveOngoing,
},
{
name: "ActiveOngoing",
validator: &phase0.Validator{
Expand Down Expand Up @@ -215,6 +235,26 @@ func TestValidatorToState(t *testing.T) {
},
state: api.ValidatorStateActiveSlashed,
},
{
name: "ExitedNext",
validator: &phase0.Validator{
ActivationEligibilityEpoch: currentEpoch - 50,
ActivationEpoch: currentEpoch - 40,
ExitEpoch: currentEpoch + 1,
WithdrawableEpoch: farFutureEpoch,
},
state: api.ValidatorStateActiveExiting,
},
{
name: "ExitedThis",
validator: &phase0.Validator{
ActivationEligibilityEpoch: currentEpoch - 50,
ActivationEpoch: currentEpoch - 40,
ExitEpoch: currentEpoch,
WithdrawableEpoch: farFutureEpoch,
},
state: api.ValidatorStateExitedUnslashed,
},
{
name: "ExitedUnslashed",
validator: &phase0.Validator{
Expand All @@ -226,7 +266,29 @@ func TestValidatorToState(t *testing.T) {
state: api.ValidatorStateExitedUnslashed,
},
{
name: "ExitedUnslashed",
name: "ExitedSlashedNext",
validator: &phase0.Validator{
ActivationEligibilityEpoch: currentEpoch - 50,
ActivationEpoch: currentEpoch - 40,
ExitEpoch: currentEpoch + 1,
WithdrawableEpoch: farFutureEpoch,
Slashed: true,
},
state: api.ValidatorStateActiveSlashed,
},
{
name: "ExitedSlashedThis",
validator: &phase0.Validator{
ActivationEligibilityEpoch: currentEpoch - 50,
ActivationEpoch: currentEpoch - 40,
ExitEpoch: currentEpoch,
WithdrawableEpoch: farFutureEpoch,
Slashed: true,
},
state: api.ValidatorStateExitedSlashed,
},
{
name: "ExitedSlashed",
validator: &phase0.Validator{
ActivationEligibilityEpoch: currentEpoch - 50,
ActivationEpoch: currentEpoch - 40,
Expand Down

0 comments on commit 9d4133d

Please sign in to comment.