diff --git a/beacon-chain/state/state-native/v1/BUILD.bazel b/beacon-chain/state/state-native/v1/BUILD.bazel index 602aa0bf1d00..7bf4bfcb7ac9 100644 --- a/beacon-chain/state/state-native/v1/BUILD.bazel +++ b/beacon-chain/state/state-native/v1/BUILD.bazel @@ -97,6 +97,7 @@ go_test( deps = [ "//beacon-chain/state:go_default_library", "//beacon-chain/state/stateutil:go_default_library", + "//beacon-chain/state/testing:go_default_library", "//beacon-chain/state/types:go_default_library", "//config/features:go_default_library", "//config/fieldparams:go_default_library", diff --git a/beacon-chain/state/state-native/v1/getters_test.go b/beacon-chain/state/state-native/v1/getters_test.go index 014db4e1aaea..1844287aeff6 100644 --- a/beacon-chain/state/state-native/v1/getters_test.go +++ b/beacon-chain/state/state-native/v1/getters_test.go @@ -1,155 +1,39 @@ package v1 import ( - "sync" "testing" - types "github.com/prysmaticlabs/eth2-types" - fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" + "github.com/prysmaticlabs/prysm/beacon-chain/state" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_SlotDataRace(t *testing.T) { - headState, err := InitializeFromProto(ðpb.BeaconState{Slot: 1}) - require.NoError(t, err) - - wg := sync.WaitGroup{} - wg.Add(2) - go func() { - require.NoError(t, headState.SetSlot(0)) - wg.Done() - }() - go func() { - headState.Slot() - wg.Done() - }() - - wg.Wait() + testtmpl.VerifyBeaconState_SlotDataRace(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconState{Slot: 1}) + }) } func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) { - c1 := ðpb.Checkpoint{Epoch: 1} - c2 := ðpb.Checkpoint{Epoch: 2} - beaconState, err := InitializeFromProto(ðpb.BeaconState{CurrentJustifiedCheckpoint: c1}) - require.NoError(t, err) - require.Equal(t, true, beaconState.MatchCurrentJustifiedCheckpoint(c1)) - require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c2)) - require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c1)) - require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c2)) + testtmpl.VerifyBeaconState_MatchCurrentJustifiedCheckptNative( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconState{CurrentJustifiedCheckpoint: cp}) + }, + ) } func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) { - c1 := ðpb.Checkpoint{Epoch: 1} - c2 := ðpb.Checkpoint{Epoch: 2} - beaconState, err := InitializeFromProto(ðpb.BeaconState{PreviousJustifiedCheckpoint: c1}) - require.NoError(t, err) - require.NoError(t, err) - require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c1)) - require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c2)) - require.Equal(t, true, beaconState.MatchPreviousJustifiedCheckpoint(c1)) - require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c2)) + testtmpl.VerifyBeaconState_MatchPreviousJustifiedCheckptNative( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconState{PreviousJustifiedCheckpoint: cp}) + }, + ) } func TestBeaconState_ValidatorByPubkey(t *testing.T) { - keyCreator := func(input []byte) [fieldparams.BLSPubkeyLength]byte { - nKey := [fieldparams.BLSPubkeyLength]byte{} - copy(nKey[:1], input) - return nKey - } - - tests := []struct { - name string - modifyFunc func(b *BeaconState, k [fieldparams.BLSPubkeyLength]byte) - exists bool - expectedIdx types.ValidatorIndex - largestIdxInSet types.ValidatorIndex - }{ - { - name: "retrieve validator", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators from the start", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 2, - }, - { - name: "retrieve validator with multiple validators from the start with shared state", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - _ = b.Copy() - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators with shared state", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - - }, - exists: false, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators with shared state at boundary", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - - }, - exists: false, - expectedIdx: 0, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, err := InitializeFromProto(ðpb.BeaconState{}) - require.NoError(t, err) - nKey := keyCreator([]byte{'A'}) - tt.modifyFunc(s, nKey) - idx, ok := s.ValidatorIndexByPubkey(nKey) - assert.Equal(t, tt.exists, ok) - assert.Equal(t, tt.expectedIdx, idx) - }) - } + testtmpl.VerifyBeaconState_ValidatorByPubkey(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconState{}) + }) } 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 88cb0614ed9f..8976b935b4fc 100644 --- a/beacon-chain/state/state-native/v1/getters_validator_test.go +++ b/beacon-chain/state/state-native/v1/getters_validator_test.go @@ -5,17 +5,14 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state" v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/v1" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { - st, err := v1.InitializeFromProtoUnsafe(ðpb.BeaconState{ - Validators: nil, + testtmpl.VerifyBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t, func() (state.BeaconState, error) { + return v1.InitializeFromProtoUnsafe(ðpb.BeaconState{ + Validators: nil, + }) }) - require.NoError(t, err) - - _, err = st.ValidatorAtIndexReadOnly(0) - 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 3ac669bab9a2..777048e162a5 100644 --- a/beacon-chain/state/state-native/v2/BUILD.bazel +++ b/beacon-chain/state/state-native/v2/BUILD.bazel @@ -84,8 +84,8 @@ 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/testing:go_default_library", "//beacon-chain/state/v2:go_default_library", "//config/features:go_default_library", "//config/fieldparams:go_default_library", diff --git a/beacon-chain/state/state-native/v2/getters_test.go b/beacon-chain/state/state-native/v2/getters_test.go index 9bf7bf391f17..5830bc61a86a 100644 --- a/beacon-chain/state/state-native/v2/getters_test.go +++ b/beacon-chain/state/state-native/v2/getters_test.go @@ -1,132 +1,39 @@ package v2 import ( - "sync" "testing" - types "github.com/prysmaticlabs/eth2-types" - fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" + "github.com/prysmaticlabs/prysm/beacon-chain/state" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_SlotDataRace(t *testing.T) { - headState, err := InitializeFromProto(ðpb.BeaconStateAltair{Slot: 1}) - require.NoError(t, err) - - wg := sync.WaitGroup{} - wg.Add(2) - go func() { - require.NoError(t, headState.SetSlot(0)) - wg.Done() - }() - go func() { - headState.Slot() - wg.Done() - }() - - wg.Wait() + testtmpl.VerifyBeaconState_SlotDataRace(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateAltair{Slot: 1}) + }) } -func TestBeaconState_ValidatorByPubkey(t *testing.T) { - keyCreator := func(input []byte) [fieldparams.BLSPubkeyLength]byte { - nKey := [fieldparams.BLSPubkeyLength]byte{} - copy(nKey[:1], input) - return nKey - } - - tests := []struct { - name string - modifyFunc func(b *BeaconState, k [fieldparams.BLSPubkeyLength]byte) - exists bool - expectedIdx types.ValidatorIndex - largestIdxInSet types.ValidatorIndex - }{ - { - name: "retrieve validator", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators from the start", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 2, - }, - { - name: "retrieve validator with multiple validators from the start with shared state", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - _ = b.Copy() - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators with shared state", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - - }, - exists: false, - expectedIdx: 0, +func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) { + testtmpl.VerifyBeaconState_MatchCurrentJustifiedCheckptNative( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateAltair{CurrentJustifiedCheckpoint: cp}) }, - { - name: "retrieve validator with multiple validators with shared state at boundary", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + ) +} - }, - exists: false, - expectedIdx: 0, +func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) { + testtmpl.VerifyBeaconState_MatchPreviousJustifiedCheckptNative( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateAltair{PreviousJustifiedCheckpoint: cp}) }, - } + ) +} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, err := InitializeFromProto(ðpb.BeaconStateAltair{}) - require.NoError(t, err) - nKey := keyCreator([]byte{'A'}) - tt.modifyFunc(s, nKey) - idx, ok := s.ValidatorIndexByPubkey(nKey) - assert.Equal(t, tt.exists, ok) - assert.Equal(t, tt.expectedIdx, idx) - }) - } +func TestBeaconState_ValidatorByPubkey(t *testing.T) { + testtmpl.VerifyBeaconState_ValidatorByPubkey(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateAltair{}) + }) } 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 a8b85530beec..6842c38d21e2 100644 --- a/beacon-chain/state/state-native/v2/getters_validator_test.go +++ b/beacon-chain/state/state-native/v2/getters_validator_test.go @@ -4,18 +4,15 @@ import ( "testing" "github.com/prysmaticlabs/prysm/beacon-chain/state" - v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/v1" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" + v2 "github.com/prysmaticlabs/prysm/beacon-chain/state/v2" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { - st, err := v1.InitializeFromProtoUnsafe(ðpb.BeaconState{ - Validators: nil, + testtmpl.VerifyBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t, func() (state.BeaconState, error) { + return v2.InitializeFromProtoUnsafe(ðpb.BeaconStateAltair{ + Validators: nil, + }) }) - require.NoError(t, err) - - _, err = st.ValidatorAtIndexReadOnly(0) - 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 ed24955812d0..c0f53d583ba0 100644 --- a/beacon-chain/state/state-native/v3/BUILD.bazel +++ b/beacon-chain/state/state-native/v3/BUILD.bazel @@ -85,8 +85,8 @@ 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/testing:go_default_library", "//beacon-chain/state/v3:go_default_library", "//config/features:go_default_library", "//config/fieldparams:go_default_library", diff --git a/beacon-chain/state/state-native/v3/getters_test.go b/beacon-chain/state/state-native/v3/getters_test.go index a9d20f2bf308..eb9d6dd87800 100644 --- a/beacon-chain/state/state-native/v3/getters_test.go +++ b/beacon-chain/state/state-native/v3/getters_test.go @@ -1,132 +1,39 @@ package v3 import ( - "sync" "testing" - types "github.com/prysmaticlabs/eth2-types" - fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" + "github.com/prysmaticlabs/prysm/beacon-chain/state" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_SlotDataRace(t *testing.T) { - headState, err := InitializeFromProto(ðpb.BeaconStateBellatrix{Slot: 1}) - require.NoError(t, err) - - wg := sync.WaitGroup{} - wg.Add(2) - go func() { - require.NoError(t, headState.SetSlot(0)) - wg.Done() - }() - go func() { - headState.Slot() - wg.Done() - }() - - wg.Wait() + testtmpl.VerifyBeaconState_SlotDataRace(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateBellatrix{Slot: 1}) + }) } -func TestBeaconState_ValidatorByPubkey(t *testing.T) { - keyCreator := func(input []byte) [fieldparams.BLSPubkeyLength]byte { - nKey := [fieldparams.BLSPubkeyLength]byte{} - copy(nKey[:1], input) - return nKey - } - - tests := []struct { - name string - modifyFunc func(b *BeaconState, k [fieldparams.BLSPubkeyLength]byte) - exists bool - expectedIdx types.ValidatorIndex - largestIdxInSet types.ValidatorIndex - }{ - { - name: "retrieve validator", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators from the start", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 2, - }, - { - name: "retrieve validator with multiple validators from the start with shared state", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - _ = b.Copy() - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators with shared state", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - - }, - exists: false, - expectedIdx: 0, +func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) { + testtmpl.VerifyBeaconState_MatchCurrentJustifiedCheckptNative( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateBellatrix{CurrentJustifiedCheckpoint: cp}) }, - { - name: "retrieve validator with multiple validators with shared state at boundary", - modifyFunc: func(b *BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + ) +} - }, - exists: false, - expectedIdx: 0, +func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) { + testtmpl.VerifyBeaconState_MatchPreviousJustifiedCheckptNative( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateBellatrix{PreviousJustifiedCheckpoint: cp}) }, - } + ) +} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, err := InitializeFromProto(ðpb.BeaconStateBellatrix{}) - require.NoError(t, err) - nKey := keyCreator([]byte{'A'}) - tt.modifyFunc(s, nKey) - idx, ok := s.ValidatorIndexByPubkey(nKey) - assert.Equal(t, tt.exists, ok) - assert.Equal(t, tt.expectedIdx, idx) - }) - } +func TestBeaconState_ValidatorByPubkey(t *testing.T) { + testtmpl.VerifyBeaconState_ValidatorByPubkey(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateBellatrix{}) + }) } 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 258433afb678..138ee674e837 100644 --- a/beacon-chain/state/state-native/v3/getters_validator_test.go +++ b/beacon-chain/state/state-native/v3/getters_validator_test.go @@ -4,18 +4,15 @@ import ( "testing" "github.com/prysmaticlabs/prysm/beacon-chain/state" - v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/v1" + v3 "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/v3" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { - st, err := v1.InitializeFromProtoUnsafe(ðpb.BeaconState{ - Validators: nil, + testtmpl.VerifyBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t, func() (state.BeaconState, error) { + return v3.InitializeFromProtoUnsafe(ðpb.BeaconStateBellatrix{ + Validators: nil, + }) }) - require.NoError(t, err) - - _, err = st.ValidatorAtIndexReadOnly(0) - assert.Equal(t, state.ErrNilValidatorsInState, err) } diff --git a/beacon-chain/state/testing/BUILD.bazel b/beacon-chain/state/testing/BUILD.bazel new file mode 100644 index 000000000000..ccf8acfb631c --- /dev/null +++ b/beacon-chain/state/testing/BUILD.bazel @@ -0,0 +1,20 @@ +load("@prysm//tools/go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + testonly = True, + srcs = [ + "getters.go", + "getters_validator.go", + ], + importpath = "github.com/prysmaticlabs/prysm/beacon-chain/state/testing", + visibility = ["//beacon-chain/state:__subpackages__"], + deps = [ + "//beacon-chain/state:go_default_library", + "//config/fieldparams:go_default_library", + "//proto/prysm/v1alpha1:go_default_library", + "//testing/assert:go_default_library", + "//testing/require:go_default_library", + "@com_github_prysmaticlabs_eth2_types//:go_default_library", + ], +) diff --git a/beacon-chain/state/testing/getters.go b/beacon-chain/state/testing/getters.go new file mode 100644 index 000000000000..403adee797e0 --- /dev/null +++ b/beacon-chain/state/testing/getters.go @@ -0,0 +1,192 @@ +package testing + +import ( + "sync" + "testing" + + types "github.com/prysmaticlabs/eth2-types" + "github.com/prysmaticlabs/prysm/beacon-chain/state" + fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" + ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" + "github.com/prysmaticlabs/prysm/testing/assert" + "github.com/prysmaticlabs/prysm/testing/require" +) + +func VerifyBeaconState_SlotDataRace(t *testing.T, factory getState) { + headState, err := factory() + require.NoError(t, err) + + wg := sync.WaitGroup{} + wg.Add(2) + go func() { + require.NoError(t, headState.SetSlot(0)) + wg.Done() + }() + go func() { + headState.Slot() + wg.Done() + }() + + wg.Wait() +} + +type getStateWithCurrentJustifiedCheckpoint func(*ethpb.Checkpoint) (state.BeaconState, error) +type clearInternalState func(state.BeaconState) + +func VerifyBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T, factory getStateWithCurrentJustifiedCheckpoint, clear clearInternalState) { + c1 := ðpb.Checkpoint{Epoch: 1} + c2 := ðpb.Checkpoint{Epoch: 2} + beaconState, err := factory(c1) + require.NoError(t, err) + require.Equal(t, true, beaconState.MatchCurrentJustifiedCheckpoint(c1)) + require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c2)) + require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c1)) + require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c2)) + clear(beaconState) + require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c1)) +} + +func VerifyBeaconState_MatchCurrentJustifiedCheckptNative(t *testing.T, factory getStateWithCurrentJustifiedCheckpoint) { + c1 := ðpb.Checkpoint{Epoch: 1} + c2 := ðpb.Checkpoint{Epoch: 2} + beaconState, err := factory(c1) + require.NoError(t, err) + require.Equal(t, true, beaconState.MatchCurrentJustifiedCheckpoint(c1)) + require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c2)) + require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c1)) + require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c2)) +} + +func VerifyBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T, factory getStateWithCurrentJustifiedCheckpoint, clear clearInternalState) { + c1 := ðpb.Checkpoint{Epoch: 1} + c2 := ðpb.Checkpoint{Epoch: 2} + beaconState, err := factory(c1) + require.NoError(t, err) + require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c1)) + require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c2)) + require.Equal(t, true, beaconState.MatchPreviousJustifiedCheckpoint(c1)) + require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c2)) + clear(beaconState) + require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c1)) +} + +func VerifyBeaconState_MatchPreviousJustifiedCheckptNative(t *testing.T, factory getStateWithCurrentJustifiedCheckpoint) { + c1 := ðpb.Checkpoint{Epoch: 1} + c2 := ðpb.Checkpoint{Epoch: 2} + beaconState, err := factory(c1) + require.NoError(t, err) + require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c1)) + require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c2)) + require.Equal(t, true, beaconState.MatchPreviousJustifiedCheckpoint(c1)) + require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c2)) +} + +func VerifyBeaconState_MarshalSSZ_NilState(t *testing.T, factory getState, clear clearInternalState) { + s, err := factory() + require.NoError(t, err) + clear(s) + _, err = s.MarshalSSZ() + require.ErrorContains(t, "nil beacon state", err) +} + +func VerifyBeaconState_ValidatorByPubkey(t *testing.T, factory getState) { + keyCreator := func(input []byte) [fieldparams.BLSPubkeyLength]byte { + nKey := [fieldparams.BLSPubkeyLength]byte{} + copy(nKey[:1], input) + return nKey + } + + tests := []struct { + name string + modifyFunc func(b state.BeaconState, k [fieldparams.BLSPubkeyLength]byte) + exists bool + expectedIdx types.ValidatorIndex + largestIdxInSet types.ValidatorIndex + }{ + { + name: "retrieve validator", + modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + }, + exists: true, + expectedIdx: 0, + }, + { + name: "retrieve validator with multiple validators from the start", + modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { + key1 := keyCreator([]byte{'C'}) + key2 := keyCreator([]byte{'D'}) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) + }, + exists: true, + expectedIdx: 0, + }, + { + name: "retrieve validator with multiple validators", + modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { + key1 := keyCreator([]byte{'C'}) + key2 := keyCreator([]byte{'D'}) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + }, + exists: true, + expectedIdx: 2, + }, + { + name: "retrieve validator with multiple validators from the start with shared state", + modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { + key1 := keyCreator([]byte{'C'}) + key2 := keyCreator([]byte{'D'}) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + _ = b.Copy() + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) + }, + exists: true, + expectedIdx: 0, + }, + { + name: "retrieve validator with multiple validators with shared state", + modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { + key1 := keyCreator([]byte{'C'}) + key2 := keyCreator([]byte{'D'}) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) + n := b.Copy() + // Append to another state + assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + + }, + exists: false, + expectedIdx: 0, + }, + { + name: "retrieve validator with multiple validators with shared state at boundary", + modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { + key1 := keyCreator([]byte{'C'}) + assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) + n := b.Copy() + // Append to another state + assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + + }, + exists: false, + expectedIdx: 0, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s, err := factory() + require.NoError(t, err) + nKey := keyCreator([]byte{'A'}) + tt.modifyFunc(s, nKey) + idx, ok := s.ValidatorIndexByPubkey(nKey) + assert.Equal(t, tt.exists, ok) + assert.Equal(t, tt.expectedIdx, idx) + }) + } +} diff --git a/beacon-chain/state/testing/getters_validator.go b/beacon-chain/state/testing/getters_validator.go new file mode 100644 index 000000000000..7a4bc3f15eab --- /dev/null +++ b/beacon-chain/state/testing/getters_validator.go @@ -0,0 +1,19 @@ +package testing + +import ( + "testing" + + "github.com/prysmaticlabs/prysm/beacon-chain/state" + "github.com/prysmaticlabs/prysm/testing/assert" + "github.com/prysmaticlabs/prysm/testing/require" +) + +type getState func() (state.BeaconState, error) + +func VerifyBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T, factory getState) { + st, err := factory() + require.NoError(t, err) + + _, err = st.ValidatorAtIndexReadOnly(0) + assert.Equal(t, state.ErrNilValidatorsInState, err) +} diff --git a/beacon-chain/state/v1/BUILD.bazel b/beacon-chain/state/v1/BUILD.bazel index a1763a074235..3a9fa078fbb0 100644 --- a/beacon-chain/state/v1/BUILD.bazel +++ b/beacon-chain/state/v1/BUILD.bazel @@ -85,6 +85,7 @@ go_test( deps = [ "//beacon-chain/state:go_default_library", "//beacon-chain/state/stateutil:go_default_library", + "//beacon-chain/state/testing:go_default_library", "//beacon-chain/state/types:go_default_library", "//config/features:go_default_library", "//config/fieldparams:go_default_library", diff --git a/beacon-chain/state/v1/getters_test.go b/beacon-chain/state/v1/getters_test.go index 8d2c1dfb2a1f..24ead8f0b160 100644 --- a/beacon-chain/state/v1/getters_test.go +++ b/beacon-chain/state/v1/getters_test.go @@ -2,33 +2,19 @@ package v1 import ( "runtime/debug" - "sync" "testing" - types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/state" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_SlotDataRace(t *testing.T) { - headState, err := InitializeFromProto(ðpb.BeaconState{Slot: 1}) - require.NoError(t, err) - - wg := sync.WaitGroup{} - wg.Add(2) - go func() { - require.NoError(t, headState.SetSlot(0)) - wg.Done() - }() - go func() { - headState.Slot() - wg.Done() - }() - - wg.Wait() + testtmpl.VerifyBeaconState_SlotDataRace(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconState{Slot: 1}) + }) } func TestNilState_NoPanic(t *testing.T) { @@ -80,144 +66,55 @@ func TestNilState_NoPanic(t *testing.T) { } func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) { - c1 := ðpb.Checkpoint{Epoch: 1} - c2 := ðpb.Checkpoint{Epoch: 2} - beaconState, err := InitializeFromProto(ðpb.BeaconState{CurrentJustifiedCheckpoint: c1}) - require.NoError(t, err) - require.Equal(t, true, beaconState.MatchCurrentJustifiedCheckpoint(c1)) - require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c2)) - require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c1)) - require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c2)) - s, ok := beaconState.(*BeaconState) - require.Equal(t, true, ok) - s.state = nil - require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c1)) + testtmpl.VerifyBeaconState_MatchCurrentJustifiedCheckpt( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconState{CurrentJustifiedCheckpoint: cp}) + }, + func(i state.BeaconState) { + s, ok := i.(*BeaconState) + if !ok { + panic("error in type assertion in test template") + } + s.state = nil + }, + ) } func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) { - c1 := ðpb.Checkpoint{Epoch: 1} - c2 := ðpb.Checkpoint{Epoch: 2} - beaconState, err := InitializeFromProto(ðpb.BeaconState{PreviousJustifiedCheckpoint: c1}) - require.NoError(t, err) - require.NoError(t, err) - require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c1)) - require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c2)) - require.Equal(t, true, beaconState.MatchPreviousJustifiedCheckpoint(c1)) - require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c2)) - s, ok := beaconState.(*BeaconState) - require.Equal(t, true, ok) - s.state = nil - require.Equal(t, false, beaconState.MatchPreviousJustifiedCheckpoint(c1)) -} - -func TestBeaconState_MarshalSSZ_NilState(t *testing.T) { - beaconState, err := InitializeFromProto(ðpb.BeaconState{}) - require.NoError(t, err) - s, ok := beaconState.(*BeaconState) - require.Equal(t, true, ok) - s.state = nil - _, err = s.MarshalSSZ() - require.ErrorContains(t, "nil beacon state", err) -} - -func TestBeaconState_ValidatorByPubkey(t *testing.T) { - keyCreator := func(input []byte) [fieldparams.BLSPubkeyLength]byte { - nKey := [fieldparams.BLSPubkeyLength]byte{} - copy(nKey[:1], input) - return nKey - } - - tests := []struct { - name string - modifyFunc func(b state.BeaconState, k [fieldparams.BLSPubkeyLength]byte) - exists bool - expectedIdx types.ValidatorIndex - largestIdxInSet types.ValidatorIndex - }{ - { - name: "retrieve validator", - modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators from the start", - modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, + testtmpl.VerifyBeaconState_MatchPreviousJustifiedCheckpt( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconState{PreviousJustifiedCheckpoint: cp}) }, - { - name: "retrieve validator with multiple validators", - modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 2, + func(i state.BeaconState) { + s, ok := i.(*BeaconState) + if !ok { + panic("error in type assertion in test template") + } + s.state = nil }, - { - name: "retrieve validator with multiple validators from the start with shared state", - modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - _ = b.Copy() - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, - }, - { - name: "retrieve validator with multiple validators with shared state", - modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + ) +} - }, - exists: false, - expectedIdx: 0, +func TestBeaconState_MarshalSSZ_NilState(t *testing.T) { + testtmpl.VerifyBeaconState_MarshalSSZ_NilState( + t, + func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconState{}) }, - { - name: "retrieve validator with multiple validators with shared state at boundary", - modifyFunc: func(b state.BeaconState, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - - }, - exists: false, - expectedIdx: 0, + func(i state.BeaconState) { + s, ok := i.(*BeaconState) + if !ok { + panic("error in type assertion in test template") + } + s.state = nil }, - } + ) +} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, err := InitializeFromProto(ðpb.BeaconState{}) - require.NoError(t, err) - nKey := keyCreator([]byte{'A'}) - tt.modifyFunc(s, nKey) - idx, ok := s.ValidatorIndexByPubkey(nKey) - assert.Equal(t, tt.exists, ok) - assert.Equal(t, tt.expectedIdx, idx) - }) - } +func TestBeaconState_ValidatorByPubkey(t *testing.T) { + testtmpl.VerifyBeaconState_ValidatorByPubkey(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconState{}) + }) } diff --git a/beacon-chain/state/v1/getters_validator_test.go b/beacon-chain/state/v1/getters_validator_test.go index 7e181c58dd26..70dad3372048 100644 --- a/beacon-chain/state/v1/getters_validator_test.go +++ b/beacon-chain/state/v1/getters_validator_test.go @@ -4,18 +4,15 @@ import ( "testing" "github.com/prysmaticlabs/prysm/beacon-chain/state" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { - st, err := v1.InitializeFromProtoUnsafe(ðpb.BeaconState{ - Validators: nil, + testtmpl.VerifyBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t, func() (state.BeaconState, error) { + return v1.InitializeFromProtoUnsafe(ðpb.BeaconState{ + Validators: nil, + }) }) - require.NoError(t, err) - - _, err = st.ValidatorAtIndexReadOnly(0) - assert.Equal(t, state.ErrNilValidatorsInState, err) } diff --git a/beacon-chain/state/v2/BUILD.bazel b/beacon-chain/state/v2/BUILD.bazel index b05ac6311d59..eabec616107f 100644 --- a/beacon-chain/state/v2/BUILD.bazel +++ b/beacon-chain/state/v2/BUILD.bazel @@ -74,6 +74,7 @@ go_test( deps = [ "//beacon-chain/state:go_default_library", "//beacon-chain/state/stateutil:go_default_library", + "//beacon-chain/state/testing:go_default_library", "//beacon-chain/state/types:go_default_library", "//config/features:go_default_library", "//config/fieldparams:go_default_library", diff --git a/beacon-chain/state/v2/getters_test.go b/beacon-chain/state/v2/getters_test.go index 924c0e252858..8624381c48e8 100644 --- a/beacon-chain/state/v2/getters_test.go +++ b/beacon-chain/state/v2/getters_test.go @@ -2,33 +2,18 @@ package v2 import ( "runtime/debug" - "sync" "testing" - types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/state" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_SlotDataRace(t *testing.T) { - headState, err := InitializeFromProto(ðpb.BeaconStateAltair{Slot: 1}) - require.NoError(t, err) - - wg := sync.WaitGroup{} - wg.Add(2) - go func() { - require.NoError(t, headState.SetSlot(0)) - wg.Done() - }() - go func() { - headState.Slot() - wg.Done() - }() - - wg.Wait() + testtmpl.VerifyBeaconState_SlotDataRace(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateAltair{Slot: 1}) + }) } func TestNilState_NoPanic(t *testing.T) { @@ -89,104 +74,56 @@ func TestNilState_NoPanic(t *testing.T) { _ = err } -func TestBeaconState_ValidatorByPubkey(t *testing.T) { - keyCreator := func(input []byte) [fieldparams.BLSPubkeyLength]byte { - nKey := [fieldparams.BLSPubkeyLength]byte{} - copy(nKey[:1], input) - return nKey - } - - tests := []struct { - name string - modifyFunc func(b state.BeaconStateAltair, k [fieldparams.BLSPubkeyLength]byte) - exists bool - expectedIdx types.ValidatorIndex - largestIdxInSet types.ValidatorIndex - }{ - { - name: "retrieve validator", - modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) { - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 0, +func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) { + testtmpl.VerifyBeaconState_MatchCurrentJustifiedCheckpt( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateAltair{CurrentJustifiedCheckpoint: cp}) }, - { - name: "retrieve validator with multiple validators from the start", - modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, + func(i state.BeaconState) { + s, ok := i.(*BeaconState) + if !ok { + panic("error in type assertion in test template") + } + s.state = nil }, - { - name: "retrieve validator with multiple validators", - modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 2, + ) +} + +func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) { + testtmpl.VerifyBeaconState_MatchPreviousJustifiedCheckpt( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateAltair{PreviousJustifiedCheckpoint: cp}) }, - { - name: "retrieve validator with multiple validators from the start with shared state", - modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - _ = b.Copy() - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, + func(i state.BeaconState) { + s, ok := i.(*BeaconState) + if !ok { + panic("error in type assertion in test template") + } + s.state = nil }, - { - name: "retrieve validator with multiple validators with shared state", - modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + ) +} - }, - exists: false, - expectedIdx: 0, +func TestBeaconState_MarshalSSZ_NilState(t *testing.T) { + testtmpl.VerifyBeaconState_MarshalSSZ_NilState( + t, + func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateAltair{}) }, - { - name: "retrieve validator with multiple validators with shared state at boundary", - modifyFunc: func(b state.BeaconStateAltair, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - - }, - exists: false, - expectedIdx: 0, + func(i state.BeaconState) { + s, ok := i.(*BeaconState) + if !ok { + panic("error in type assertion in test template") + } + s.state = nil }, - } + ) +} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, err := InitializeFromProto(ðpb.BeaconStateAltair{}) - require.NoError(t, err) - nKey := keyCreator([]byte{'A'}) - tt.modifyFunc(s, nKey) - idx, ok := s.ValidatorIndexByPubkey(nKey) - assert.Equal(t, tt.exists, ok) - assert.Equal(t, tt.expectedIdx, idx) - }) - } +func TestBeaconState_ValidatorByPubkey(t *testing.T) { + testtmpl.VerifyBeaconState_ValidatorByPubkey(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateAltair{}) + }) } diff --git a/beacon-chain/state/v2/getters_validator_test.go b/beacon-chain/state/v2/getters_validator_test.go index ac01dbcefdb1..6842c38d21e2 100644 --- a/beacon-chain/state/v2/getters_validator_test.go +++ b/beacon-chain/state/v2/getters_validator_test.go @@ -4,18 +4,15 @@ import ( "testing" "github.com/prysmaticlabs/prysm/beacon-chain/state" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" v2 "github.com/prysmaticlabs/prysm/beacon-chain/state/v2" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { - st, err := v2.InitializeFromProtoUnsafe(ðpb.BeaconStateAltair{ - Validators: nil, + testtmpl.VerifyBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t, func() (state.BeaconState, error) { + return v2.InitializeFromProtoUnsafe(ðpb.BeaconStateAltair{ + Validators: nil, + }) }) - require.NoError(t, err) - - _, err = st.ValidatorAtIndexReadOnly(0) - assert.Equal(t, state.ErrNilValidatorsInState, err) } diff --git a/beacon-chain/state/v3/BUILD.bazel b/beacon-chain/state/v3/BUILD.bazel index 1ea9de8e9089..d1be540d98ae 100644 --- a/beacon-chain/state/v3/BUILD.bazel +++ b/beacon-chain/state/v3/BUILD.bazel @@ -75,6 +75,7 @@ go_test( deps = [ "//beacon-chain/state:go_default_library", "//beacon-chain/state/stateutil:go_default_library", + "//beacon-chain/state/testing:go_default_library", "//beacon-chain/state/types:go_default_library", "//config/features:go_default_library", "//config/fieldparams:go_default_library", diff --git a/beacon-chain/state/v3/getters_test.go b/beacon-chain/state/v3/getters_test.go index 997dd4ef5c87..583e33bbe6ea 100644 --- a/beacon-chain/state/v3/getters_test.go +++ b/beacon-chain/state/v3/getters_test.go @@ -2,33 +2,18 @@ package v3 import ( "runtime/debug" - "sync" "testing" - types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/state" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_SlotDataRace(t *testing.T) { - headState, err := InitializeFromProto(ðpb.BeaconStateBellatrix{Slot: 1}) - require.NoError(t, err) - - wg := sync.WaitGroup{} - wg.Add(2) - go func() { - require.NoError(t, headState.SetSlot(0)) - wg.Done() - }() - go func() { - headState.Slot() - wg.Done() - }() - - wg.Wait() + testtmpl.VerifyBeaconState_SlotDataRace(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateBellatrix{Slot: 1}) + }) } func TestNilState_NoPanic(t *testing.T) { @@ -89,104 +74,56 @@ func TestNilState_NoPanic(t *testing.T) { _ = err } -func TestBeaconState_ValidatorByPubkey(t *testing.T) { - keyCreator := func(input []byte) [fieldparams.BLSPubkeyLength]byte { - nKey := [fieldparams.BLSPubkeyLength]byte{} - copy(nKey[:1], input) - return nKey - } - - tests := []struct { - name string - modifyFunc func(b state.BeaconStateBellatrix, k [fieldparams.BLSPubkeyLength]byte) - exists bool - expectedIdx types.ValidatorIndex - largestIdxInSet types.ValidatorIndex - }{ - { - name: "retrieve validator", - modifyFunc: func(b state.BeaconStateBellatrix, key [fieldparams.BLSPubkeyLength]byte) { - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 0, +func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) { + testtmpl.VerifyBeaconState_MatchCurrentJustifiedCheckpt( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateBellatrix{CurrentJustifiedCheckpoint: cp}) }, - { - name: "retrieve validator with multiple validators from the start", - modifyFunc: func(b state.BeaconStateBellatrix, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, + func(i state.BeaconState) { + s, ok := i.(*BeaconState) + if !ok { + panic("error in type assertion in test template") + } + s.state = nil }, - { - name: "retrieve validator with multiple validators", - modifyFunc: func(b state.BeaconStateBellatrix, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - }, - exists: true, - expectedIdx: 2, + ) +} + +func TestBeaconState_MatchPreviousJustifiedCheckpt(t *testing.T) { + testtmpl.VerifyBeaconState_MatchPreviousJustifiedCheckpt( + t, + func(cp *ethpb.Checkpoint) (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateBellatrix{PreviousJustifiedCheckpoint: cp}) }, - { - name: "retrieve validator with multiple validators from the start with shared state", - modifyFunc: func(b state.BeaconStateBellatrix, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - _ = b.Copy() - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - }, - exists: true, - expectedIdx: 0, + func(i state.BeaconState) { + s, ok := i.(*BeaconState) + if !ok { + panic("error in type assertion in test template") + } + s.state = nil }, - { - name: "retrieve validator with multiple validators with shared state", - modifyFunc: func(b state.BeaconStateBellatrix, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - key2 := keyCreator([]byte{'D'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key2[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) + ) +} - }, - exists: false, - expectedIdx: 0, +func TestBeaconState_MarshalSSZ_NilState(t *testing.T) { + testtmpl.VerifyBeaconState_MarshalSSZ_NilState( + t, + func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateBellatrix{}) }, - { - name: "retrieve validator with multiple validators with shared state at boundary", - modifyFunc: func(b state.BeaconStateBellatrix, key [fieldparams.BLSPubkeyLength]byte) { - key1 := keyCreator([]byte{'C'}) - assert.NoError(t, b.AppendValidator(ðpb.Validator{PublicKey: key1[:]})) - n := b.Copy() - // Append to another state - assert.NoError(t, n.AppendValidator(ðpb.Validator{PublicKey: key[:]})) - - }, - exists: false, - expectedIdx: 0, + func(i state.BeaconState) { + s, ok := i.(*BeaconState) + if !ok { + panic("error in type assertion in test template") + } + s.state = nil }, - } + ) +} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s, err := InitializeFromProto(ðpb.BeaconStateBellatrix{}) - require.NoError(t, err) - nKey := keyCreator([]byte{'A'}) - tt.modifyFunc(s, nKey) - idx, ok := s.ValidatorIndexByPubkey(nKey) - assert.Equal(t, tt.exists, ok) - assert.Equal(t, tt.expectedIdx, idx) - }) - } +func TestBeaconState_ValidatorByPubkey(t *testing.T) { + testtmpl.VerifyBeaconState_ValidatorByPubkey(t, func() (state.BeaconState, error) { + return InitializeFromProto(ðpb.BeaconStateBellatrix{}) + }) } diff --git a/beacon-chain/state/v3/getters_validator_test.go b/beacon-chain/state/v3/getters_validator_test.go index b1975b12fb59..6bfb01095218 100644 --- a/beacon-chain/state/v3/getters_validator_test.go +++ b/beacon-chain/state/v3/getters_validator_test.go @@ -4,18 +4,15 @@ import ( "testing" "github.com/prysmaticlabs/prysm/beacon-chain/state" + testtmpl "github.com/prysmaticlabs/prysm/beacon-chain/state/testing" v3 "github.com/prysmaticlabs/prysm/beacon-chain/state/v3" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" ) func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t *testing.T) { - st, err := v3.InitializeFromProtoUnsafe(ðpb.BeaconStateBellatrix{ - Validators: nil, + testtmpl.VerifyBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice(t, func() (state.BeaconState, error) { + return v3.InitializeFromProtoUnsafe(ðpb.BeaconStateBellatrix{ + Validators: nil, + }) }) - require.NoError(t, err) - - _, err = st.ValidatorAtIndexReadOnly(0) - assert.Equal(t, state.ErrNilValidatorsInState, err) }