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.
This commit is contained in:
Leo Lara 2022-01-18 15:19:20 +07:00 committed by GitHub
parent 33d1ae0792
commit 64c02c405b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 32 additions and 48 deletions

View File

@ -4,6 +4,7 @@ go_library(
name = "go_default_library",
srcs = [
"altair.go",
"error.go",
"phase0.go",
"prometheus.go",
],

View File

@ -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")
)

View File

@ -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)

View File

@ -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)
}

View File

@ -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",

View File

@ -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)

View File

@ -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)
}

View File

@ -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",

View File

@ -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)

View File

@ -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)
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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",

View File

@ -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)

View File

@ -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)
}

View File

@ -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",

View File

@ -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)

View File

@ -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)
}