From b3d8c14c013279aa685036cb39a2757c87852f4c Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Thu, 1 Aug 2024 09:35:34 +0100 Subject: [PATCH] Better handling of validator state when no balance is present. --- CHANGELOG.md | 3 +++ api/v1/validatorstate.go | 2 +- api/v1/validatorstate_test.go | 17 ++++++++++++++++- http/http.go | 2 +- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c446b282..51bb6fde3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +0.21.10: + - better validator state when balance not supplied + 0.21.9: - enable custom timeouts for POSTs diff --git a/api/v1/validatorstate.go b/api/v1/validatorstate.go index 857bc14b8..bee32e30a 100644 --- a/api/v1/validatorstate.go +++ b/api/v1/validatorstate.go @@ -197,7 +197,7 @@ func ValidatorToState(validator *phase0.Validator, } return ValidatorStateExitedUnslashed - case balance != nil && *balance == 0: + case (balance != nil && *balance == 0) || (balance == nil && validator.EffectiveBalance == 0): return ValidatorStateWithdrawalDone default: return ValidatorStateWithdrawalPossible diff --git a/api/v1/validatorstate_test.go b/api/v1/validatorstate_test.go index 22153139f..714f3a4fa 100644 --- a/api/v1/validatorstate_test.go +++ b/api/v1/validatorstate_test.go @@ -306,6 +306,7 @@ func TestValidatorToState(t *testing.T) { ActivationEpoch: currentEpoch - 40, ExitEpoch: currentEpoch - 30, WithdrawableEpoch: currentEpoch - 20, + EffectiveBalance: 1, }, state: api.ValidatorStateWithdrawalPossible, }, @@ -316,6 +317,7 @@ func TestValidatorToState(t *testing.T) { ActivationEpoch: currentEpoch - 40, ExitEpoch: currentEpoch - 30, WithdrawableEpoch: currentEpoch - 20, + EffectiveBalance: 1, Slashed: true, }, state: api.ValidatorStateWithdrawalPossible, @@ -327,6 +329,7 @@ func TestValidatorToState(t *testing.T) { ActivationEpoch: currentEpoch - 40, ExitEpoch: currentEpoch - 30, WithdrawableEpoch: currentEpoch - 20, + EffectiveBalance: 1, }, balance: gweiPtr(5), state: api.ValidatorStateWithdrawalPossible, @@ -338,10 +341,22 @@ func TestValidatorToState(t *testing.T) { ActivationEpoch: currentEpoch - 40, ExitEpoch: currentEpoch - 30, WithdrawableEpoch: currentEpoch - 20, + EffectiveBalance: 0, }, balance: gweiPtr(0), state: api.ValidatorStateWithdrawalDone, }, + { + name: "WithdrawalDoneNoBalance", + validator: &phase0.Validator{ + ActivationEligibilityEpoch: currentEpoch - 50, + ActivationEpoch: currentEpoch - 40, + ExitEpoch: currentEpoch - 30, + WithdrawableEpoch: currentEpoch - 20, + EffectiveBalance: 0, + }, + state: api.ValidatorStateWithdrawalDone, + }, } for _, test := range tests { @@ -436,7 +451,7 @@ func TestMarshalJSON(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { require.NotPanics(t, func() { - res, err := json.Marshal(&test.state) //test.state.MarshalJSON() + res, err := json.Marshal(&test.state) test.errFunc(t, err) require.Equal(t, test.expected, res) }) diff --git a/http/http.go b/http/http.go index 634c9e137..109fb801c 100644 --- a/http/http.go +++ b/http/http.go @@ -34,7 +34,7 @@ import ( ) // defaultUserAgent is sent with requests if no other user agent has been supplied. -const defaultUserAgent = "go-eth2-client/0.21.9" +const defaultUserAgent = "go-eth2-client/0.21.10" // post sends an HTTP post request and returns the body. func (s *Service) post(ctx context.Context, endpoint string, body io.Reader) (io.Reader, error) {