From 64c02c405b1731155f1c658c5a0a0623e6a50320 Mon Sep 17 00:00:00 2001 From: Leo Lara Date: Tue, 18 Jan 2022 15:19:20 +0700 Subject: [PATCH] Move ErrNilValidatorsInState from one in each state version to a common one (#10074) To increase DRY and enable DRY in tests and other users of the Beacon Chain state package, an error that was duplicated unnecessarily in each version of the state is moved to the root Beacon Chain state package. --- beacon-chain/state/BUILD.bazel | 1 + beacon-chain/state/error.go | 9 +++++++++ beacon-chain/state/state-native/v1/getters_validator.go | 8 +------- .../state/state-native/v1/getters_validator_test.go | 3 ++- beacon-chain/state/state-native/v2/BUILD.bazel | 1 + beacon-chain/state/state-native/v2/getters_validator.go | 8 +------- .../state/state-native/v2/getters_validator_test.go | 3 ++- beacon-chain/state/state-native/v3/BUILD.bazel | 1 + beacon-chain/state/state-native/v3/getters_validator.go | 8 +------- .../state/state-native/v3/getters_validator_test.go | 3 ++- beacon-chain/state/v1/getters_validator.go | 8 +------- beacon-chain/state/v1/getters_validator_test.go | 3 ++- beacon-chain/state/v2/BUILD.bazel | 1 + beacon-chain/state/v2/getters_validator.go | 8 +------- beacon-chain/state/v2/getters_validator_test.go | 3 ++- beacon-chain/state/v3/BUILD.bazel | 1 + beacon-chain/state/v3/getters_validator.go | 8 +------- beacon-chain/state/v3/getters_validator_test.go | 3 ++- 18 files changed, 32 insertions(+), 48 deletions(-) create mode 100644 beacon-chain/state/error.go diff --git a/beacon-chain/state/BUILD.bazel b/beacon-chain/state/BUILD.bazel index e7b711b15..e303a754a 100644 --- a/beacon-chain/state/BUILD.bazel +++ b/beacon-chain/state/BUILD.bazel @@ -4,6 +4,7 @@ go_library( name = "go_default_library", srcs = [ "altair.go", + "error.go", "phase0.go", "prometheus.go", ], diff --git a/beacon-chain/state/error.go b/beacon-chain/state/error.go new file mode 100644 index 000000000..29dd69b38 --- /dev/null +++ b/beacon-chain/state/error.go @@ -0,0 +1,9 @@ +package state + +import "errors" + +var ( + // ErrNilValidatorsInState returns when accessing validators in the state while the state has a + // nil slice for the validators field. + ErrNilValidatorsInState = errors.New("state has nil validator slice") +) diff --git a/beacon-chain/state/state-native/v1/getters_validator.go b/beacon-chain/state/state-native/v1/getters_validator.go index 604e11219..746bd2030 100644 --- a/beacon-chain/state/state-native/v1/getters_validator.go +++ b/beacon-chain/state/state-native/v1/getters_validator.go @@ -17,12 +17,6 @@ type ValidatorIndexOutOfRangeError struct { message string } -var ( - // ErrNilValidatorsInState returns when accessing validators in the state while the state has a - // nil slice for the validators field. - ErrNilValidatorsInState = errors.New("state has nil validator slice") -) - // NewValidatorIndexOutOfRangeError creates a new error instance. func NewValidatorIndexOutOfRangeError(index types.ValidatorIndex) ValidatorIndexOutOfRangeError { return ValidatorIndexOutOfRangeError{ @@ -121,7 +115,7 @@ func (b *BeaconState) ValidatorAtIndexReadOnly(idx types.ValidatorIndex) (state. return nil, ErrNilInnerState } if b.state.Validators == nil { - return nil, ErrNilValidatorsInState + return nil, state.ErrNilValidatorsInState } if uint64(len(b.state.Validators)) <= uint64(idx) { e := NewValidatorIndexOutOfRangeError(idx) diff --git a/beacon-chain/state/state-native/v1/getters_validator_test.go b/beacon-chain/state/state-native/v1/getters_validator_test.go index 6e97f7874..88cb0614e 100644 --- a/beacon-chain/state/state-native/v1/getters_validator_test.go +++ b/beacon-chain/state/state-native/v1/getters_validator_test.go @@ -3,6 +3,7 @@ package v1_test import ( "testing" + "github.com/prysmaticlabs/prysm/beacon-chain/state" v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/v1" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/testing/assert" @@ -16,5 +17,5 @@ func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { require.NoError(t, err) _, err = st.ValidatorAtIndexReadOnly(0) - assert.Equal(t, v1.ErrNilValidatorsInState, err) + assert.Equal(t, state.ErrNilValidatorsInState, err) } diff --git a/beacon-chain/state/state-native/v2/BUILD.bazel b/beacon-chain/state/state-native/v2/BUILD.bazel index 7b3d1a681..fb8cb70fb 100644 --- a/beacon-chain/state/state-native/v2/BUILD.bazel +++ b/beacon-chain/state/state-native/v2/BUILD.bazel @@ -72,6 +72,7 @@ go_test( ], embed = [":go_default_library"], deps = [ + "//beacon-chain/state:go_default_library", "//beacon-chain/state/state-native/v1:go_default_library", "//beacon-chain/state/stateutil:go_default_library", "//beacon-chain/state/types:go_default_library", diff --git a/beacon-chain/state/state-native/v2/getters_validator.go b/beacon-chain/state/state-native/v2/getters_validator.go index 3137a0b9b..7e993f81a 100644 --- a/beacon-chain/state/state-native/v2/getters_validator.go +++ b/beacon-chain/state/state-native/v2/getters_validator.go @@ -18,12 +18,6 @@ type ValidatorIndexOutOfRangeError struct { message string } -var ( - // ErrNilValidatorsInState returns when accessing validators in the state while the state has a - // nil slice for the validators field. - ErrNilValidatorsInState = errors.New("state has nil validator slice") -) - // NewValidatorIndexOutOfRangeError creates a new error instance. func NewValidatorIndexOutOfRangeError(index types.ValidatorIndex) ValidatorIndexOutOfRangeError { return ValidatorIndexOutOfRangeError{ @@ -122,7 +116,7 @@ func (b *BeaconState) ValidatorAtIndexReadOnly(idx types.ValidatorIndex) (state. return nil, ErrNilInnerState } if b.state.Validators == nil { - return nil, ErrNilValidatorsInState + return nil, state.ErrNilValidatorsInState } if uint64(len(b.state.Validators)) <= uint64(idx) { e := NewValidatorIndexOutOfRangeError(idx) diff --git a/beacon-chain/state/state-native/v2/getters_validator_test.go b/beacon-chain/state/state-native/v2/getters_validator_test.go index 4dfe0095f..a8b85530b 100644 --- a/beacon-chain/state/state-native/v2/getters_validator_test.go +++ b/beacon-chain/state/state-native/v2/getters_validator_test.go @@ -3,6 +3,7 @@ package v2_test import ( "testing" + "github.com/prysmaticlabs/prysm/beacon-chain/state" v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/v1" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/testing/assert" @@ -16,5 +17,5 @@ func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { require.NoError(t, err) _, err = st.ValidatorAtIndexReadOnly(0) - assert.Equal(t, v1.ErrNilValidatorsInState, err) + assert.Equal(t, state.ErrNilValidatorsInState, err) } diff --git a/beacon-chain/state/state-native/v3/BUILD.bazel b/beacon-chain/state/state-native/v3/BUILD.bazel index 2a7efe182..1c8b01565 100644 --- a/beacon-chain/state/state-native/v3/BUILD.bazel +++ b/beacon-chain/state/state-native/v3/BUILD.bazel @@ -73,6 +73,7 @@ go_test( ], embed = [":go_default_library"], deps = [ + "//beacon-chain/state:go_default_library", "//beacon-chain/state/state-native/v1:go_default_library", "//beacon-chain/state/stateutil:go_default_library", "//beacon-chain/state/types:go_default_library", diff --git a/beacon-chain/state/state-native/v3/getters_validator.go b/beacon-chain/state/state-native/v3/getters_validator.go index 9a5c4d5fe..11c9d810f 100644 --- a/beacon-chain/state/state-native/v3/getters_validator.go +++ b/beacon-chain/state/state-native/v3/getters_validator.go @@ -18,12 +18,6 @@ type ValidatorIndexOutOfRangeError struct { message string } -var ( - // ErrNilValidatorsInState returns when accessing validators in the state while the state has a - // nil slice for the validators field. - ErrNilValidatorsInState = errors.New("state has nil validator slice") -) - // NewValidatorIndexOutOfRangeError creates a new error instance. func NewValidatorIndexOutOfRangeError(index types.ValidatorIndex) ValidatorIndexOutOfRangeError { return ValidatorIndexOutOfRangeError{ @@ -122,7 +116,7 @@ func (b *BeaconState) ValidatorAtIndexReadOnly(idx types.ValidatorIndex) (state. return nil, ErrNilInnerState } if b.state.Validators == nil { - return nil, ErrNilValidatorsInState + return nil, state.ErrNilValidatorsInState } if uint64(len(b.state.Validators)) <= uint64(idx) { e := NewValidatorIndexOutOfRangeError(idx) diff --git a/beacon-chain/state/state-native/v3/getters_validator_test.go b/beacon-chain/state/state-native/v3/getters_validator_test.go index a8851815d..258433afb 100644 --- a/beacon-chain/state/state-native/v3/getters_validator_test.go +++ b/beacon-chain/state/state-native/v3/getters_validator_test.go @@ -3,6 +3,7 @@ package v3_test import ( "testing" + "github.com/prysmaticlabs/prysm/beacon-chain/state" v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/v1" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/testing/assert" @@ -16,5 +17,5 @@ func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { require.NoError(t, err) _, err = st.ValidatorAtIndexReadOnly(0) - assert.Equal(t, v1.ErrNilValidatorsInState, err) + assert.Equal(t, state.ErrNilValidatorsInState, err) } diff --git a/beacon-chain/state/v1/getters_validator.go b/beacon-chain/state/v1/getters_validator.go index 604e11219..746bd2030 100644 --- a/beacon-chain/state/v1/getters_validator.go +++ b/beacon-chain/state/v1/getters_validator.go @@ -17,12 +17,6 @@ type ValidatorIndexOutOfRangeError struct { message string } -var ( - // ErrNilValidatorsInState returns when accessing validators in the state while the state has a - // nil slice for the validators field. - ErrNilValidatorsInState = errors.New("state has nil validator slice") -) - // NewValidatorIndexOutOfRangeError creates a new error instance. func NewValidatorIndexOutOfRangeError(index types.ValidatorIndex) ValidatorIndexOutOfRangeError { return ValidatorIndexOutOfRangeError{ @@ -121,7 +115,7 @@ func (b *BeaconState) ValidatorAtIndexReadOnly(idx types.ValidatorIndex) (state. return nil, ErrNilInnerState } if b.state.Validators == nil { - return nil, ErrNilValidatorsInState + return nil, state.ErrNilValidatorsInState } if uint64(len(b.state.Validators)) <= uint64(idx) { e := NewValidatorIndexOutOfRangeError(idx) diff --git a/beacon-chain/state/v1/getters_validator_test.go b/beacon-chain/state/v1/getters_validator_test.go index cb2834976..7e181c58d 100644 --- a/beacon-chain/state/v1/getters_validator_test.go +++ b/beacon-chain/state/v1/getters_validator_test.go @@ -3,6 +3,7 @@ package v1_test import ( "testing" + "github.com/prysmaticlabs/prysm/beacon-chain/state" v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/testing/assert" @@ -16,5 +17,5 @@ func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { require.NoError(t, err) _, err = st.ValidatorAtIndexReadOnly(0) - assert.Equal(t, v1.ErrNilValidatorsInState, err) + assert.Equal(t, state.ErrNilValidatorsInState, err) } diff --git a/beacon-chain/state/v2/BUILD.bazel b/beacon-chain/state/v2/BUILD.bazel index f047c0139..77939a1ef 100644 --- a/beacon-chain/state/v2/BUILD.bazel +++ b/beacon-chain/state/v2/BUILD.bazel @@ -72,6 +72,7 @@ go_test( ], embed = [":go_default_library"], deps = [ + "//beacon-chain/state:go_default_library", "//beacon-chain/state/stateutil:go_default_library", "//beacon-chain/state/types:go_default_library", "//beacon-chain/state/v1:go_default_library", diff --git a/beacon-chain/state/v2/getters_validator.go b/beacon-chain/state/v2/getters_validator.go index db49e3dcc..31255a33c 100644 --- a/beacon-chain/state/v2/getters_validator.go +++ b/beacon-chain/state/v2/getters_validator.go @@ -18,12 +18,6 @@ type ValidatorIndexOutOfRangeError struct { message string } -var ( - // ErrNilValidatorsInState returns when accessing validators in the state while the state has a - // nil slice for the validators field. - ErrNilValidatorsInState = errors.New("state has nil validator slice") -) - // NewValidatorIndexOutOfRangeError creates a new error instance. func NewValidatorIndexOutOfRangeError(index types.ValidatorIndex) ValidatorIndexOutOfRangeError { return ValidatorIndexOutOfRangeError{ @@ -122,7 +116,7 @@ func (b *BeaconState) ValidatorAtIndexReadOnly(idx types.ValidatorIndex) (state. return nil, ErrNilInnerState } if b.state.Validators == nil { - return nil, ErrNilValidatorsInState + return nil, state.ErrNilValidatorsInState } if uint64(len(b.state.Validators)) <= uint64(idx) { e := NewValidatorIndexOutOfRangeError(idx) diff --git a/beacon-chain/state/v2/getters_validator_test.go b/beacon-chain/state/v2/getters_validator_test.go index 454a1bbf6..8703979ee 100644 --- a/beacon-chain/state/v2/getters_validator_test.go +++ b/beacon-chain/state/v2/getters_validator_test.go @@ -3,6 +3,7 @@ package v2_test import ( "testing" + "github.com/prysmaticlabs/prysm/beacon-chain/state" v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/testing/assert" @@ -16,5 +17,5 @@ func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { require.NoError(t, err) _, err = st.ValidatorAtIndexReadOnly(0) - assert.Equal(t, v1.ErrNilValidatorsInState, err) + assert.Equal(t, state.ErrNilValidatorsInState, err) } diff --git a/beacon-chain/state/v3/BUILD.bazel b/beacon-chain/state/v3/BUILD.bazel index a45dd34b4..6fe4324f0 100644 --- a/beacon-chain/state/v3/BUILD.bazel +++ b/beacon-chain/state/v3/BUILD.bazel @@ -73,6 +73,7 @@ go_test( ], embed = [":go_default_library"], deps = [ + "//beacon-chain/state:go_default_library", "//beacon-chain/state/stateutil:go_default_library", "//beacon-chain/state/types:go_default_library", "//beacon-chain/state/v1:go_default_library", diff --git a/beacon-chain/state/v3/getters_validator.go b/beacon-chain/state/v3/getters_validator.go index a4689d00e..c8b9d8a61 100644 --- a/beacon-chain/state/v3/getters_validator.go +++ b/beacon-chain/state/v3/getters_validator.go @@ -18,12 +18,6 @@ type ValidatorIndexOutOfRangeError struct { message string } -var ( - // ErrNilValidatorsInState returns when accessing validators in the state while the state has a - // nil slice for the validators field. - ErrNilValidatorsInState = errors.New("state has nil validator slice") -) - // NewValidatorIndexOutOfRangeError creates a new error instance. func NewValidatorIndexOutOfRangeError(index types.ValidatorIndex) ValidatorIndexOutOfRangeError { return ValidatorIndexOutOfRangeError{ @@ -122,7 +116,7 @@ func (b *BeaconState) ValidatorAtIndexReadOnly(idx types.ValidatorIndex) (state. return nil, ErrNilInnerState } if b.state.Validators == nil { - return nil, ErrNilValidatorsInState + return nil, state.ErrNilValidatorsInState } if uint64(len(b.state.Validators)) <= uint64(idx) { e := NewValidatorIndexOutOfRangeError(idx) diff --git a/beacon-chain/state/v3/getters_validator_test.go b/beacon-chain/state/v3/getters_validator_test.go index 019f6dc8b..0306e23ba 100644 --- a/beacon-chain/state/v3/getters_validator_test.go +++ b/beacon-chain/state/v3/getters_validator_test.go @@ -3,6 +3,7 @@ package v3_test import ( "testing" + "github.com/prysmaticlabs/prysm/beacon-chain/state" v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/testing/assert" @@ -16,5 +17,5 @@ func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { require.NoError(t, err) _, err = st.ValidatorAtIndexReadOnly(0) - assert.Equal(t, v1.ErrNilValidatorsInState, err) + assert.Equal(t, state.ErrNilValidatorsInState, err) }