From b7a878d011dae999a185a44253462ec00cfea324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kapka?= Date: Wed, 19 Oct 2022 09:37:45 -0500 Subject: [PATCH] Resolve remaining native state tasks (#11561) * remove ToProto and ToProtoUnsafe wrappers * TestAppendBeyondIndicesLimit * change type of genesisValidatorsRoot * fuzz tests * check type assertion --- beacon-chain/blockchain/chain_info_test.go | 2 +- beacon-chain/blockchain/head_test.go | 4 +- beacon-chain/blockchain/metrics.go | 2 +- .../blockchain/process_attestation_test.go | 2 +- beacon-chain/blockchain/service_test.go | 6 +- beacon-chain/cache/checkpoint_state_test.go | 8 +- beacon-chain/cache/skip_slot_cache_test.go | 2 +- .../core/transition/benchmarks_test.go | 4 +- .../core/transition/skip_slot_cache_test.go | 6 +- beacon-chain/core/transition/state_test.go | 4 +- .../db/kv/migration_state_validators_test.go | 10 +- beacon-chain/db/kv/state.go | 14 +- beacon-chain/db/kv/state_test.go | 34 +- beacon-chain/execution/log_processing.go | 2 +- beacon-chain/execution/service.go | 2 +- beacon-chain/p2p/encoder/ssz_test.go | 2 +- beacon-chain/state/interfaces.go | 4 +- beacon-chain/state/state-native/BUILD.bazel | 3 + .../state-native/beacon_state_mainnet.go | 2 +- .../state-native/beacon_state_minimal.go | 2 +- .../state-native/custom-types/BUILD.bazel | 2 - .../state/state-native/custom-types/byte32.go | 56 - .../state-native/custom-types/byte32_test.go | 70 - .../state/state-native/getters_state.go | 11 - .../state/state-native/hasher_test.go | 8 +- .../state-native/setters_attestation_test.go | 52 + .../state/state-native/state_fuzz_test.go | 294 +++ .../state/state-native/state_trie_test.go | 28 +- beacon-chain/state/state-native/types_test.go | 6 +- .../epoch_boundary_state_cache_test.go | 4 +- beacon-chain/state/stategen/getter_test.go | 16 +- .../state/stategen/hot_state_cache_test.go | 2 +- beacon-chain/state/stategen/migrate_test.go | 2 +- beacon-chain/state/stategen/service_test.go | 2 +- proto/prysm/v1alpha1/BUILD.bazel | 26 +- proto/prysm/v1alpha1/generated.ssz.go | 1680 ++++++++++++++++- runtime/interop/generate_genesis_state.go | 2 +- .../shared/altair/epoch_processing/helpers.go | 4 +- .../shared/altair/finality/finality.go | 2 +- .../spectest/shared/altair/fork/transition.go | 2 +- .../shared/altair/fork/upgrade_to_altair.go | 2 +- .../shared/altair/operations/block_header.go | 4 +- .../shared/altair/operations/helpers.go | 4 +- .../shared/altair/sanity/block_processing.go | 4 +- .../shared/altair/sanity/slot_processing.go | 2 +- .../bellatrix/epoch_processing/helpers.go | 4 +- .../shared/bellatrix/finality/finality.go | 2 +- .../shared/bellatrix/fork/transition.go | 2 +- .../bellatrix/fork/upgrade_to_bellatrix.go | 2 +- .../bellatrix/operations/block_header.go | 4 +- .../shared/bellatrix/operations/helpers.go | 4 +- .../bellatrix/sanity/block_processing.go | 4 +- .../bellatrix/sanity/slot_processing.go | 2 +- .../shared/phase0/epoch_processing/helpers.go | 2 +- .../spectest/shared/phase0/finality/runner.go | 4 +- .../shared/phase0/operations/block_header.go | 4 +- .../shared/phase0/operations/helpers.go | 4 +- .../shared/phase0/sanity/block_processing.go | 4 +- .../shared/phase0/sanity/slot_processing.go | 2 +- testing/util/BUILD.bazel | 1 + testing/util/attestation.go | 6 +- testing/util/bellatrix_state.go | 4 +- testing/util/capella_state.go | 245 +++ testing/util/state_test.go | 8 +- tools/pcli/main.go | 4 +- 65 files changed, 2426 insertions(+), 286 deletions(-) delete mode 100644 beacon-chain/state/state-native/custom-types/byte32.go delete mode 100644 beacon-chain/state/state-native/custom-types/byte32_test.go create mode 100644 beacon-chain/state/state-native/state_fuzz_test.go create mode 100644 testing/util/capella_state.go diff --git a/beacon-chain/blockchain/chain_info_test.go b/beacon-chain/blockchain/chain_info_test.go index 4c68d5fefd..447731d671 100644 --- a/beacon-chain/blockchain/chain_info_test.go +++ b/beacon-chain/blockchain/chain_info_test.go @@ -198,7 +198,7 @@ func TestHeadState_CanRetrieve(t *testing.T) { c.head = &head{state: s} headState, err := c.HeadState(context.Background()) require.NoError(t, err) - assert.DeepEqual(t, headState.InnerStateUnsafe(), s.InnerStateUnsafe(), "Incorrect head state received") + assert.DeepEqual(t, headState.ToProtoUnsafe(), s.ToProtoUnsafe(), "Incorrect head state received") } func TestGenesisTime_CanRetrieve(t *testing.T) { diff --git a/beacon-chain/blockchain/head_test.go b/beacon-chain/blockchain/head_test.go index 08da22fde0..3f60cc2507 100644 --- a/beacon-chain/blockchain/head_test.go +++ b/beacon-chain/blockchain/head_test.go @@ -89,7 +89,7 @@ func TestSaveHead_Different(t *testing.T) { pb, err := headBlock.Proto() require.NoError(t, err) assert.DeepEqual(t, newHeadSignedBlock, pb, "Head did not change") - assert.DeepSSZEqual(t, headState.CloneInnerState(), service.headState(ctx).CloneInnerState(), "Head did not change") + assert.DeepSSZEqual(t, headState.ToProto(), service.headState(ctx).ToProto(), "Head did not change") } func TestSaveHead_Different_Reorg(t *testing.T) { @@ -147,7 +147,7 @@ func TestSaveHead_Different_Reorg(t *testing.T) { pb, err := headBlock.Proto() require.NoError(t, err) assert.DeepEqual(t, newHeadSignedBlock, pb, "Head did not change") - assert.DeepSSZEqual(t, headState.CloneInnerState(), service.headState(ctx).CloneInnerState(), "Head did not change") + assert.DeepSSZEqual(t, headState.ToProto(), service.headState(ctx).ToProto(), "Head did not change") require.LogsContain(t, hook, "Chain reorg occurred") require.LogsContain(t, hook, "distance=1") require.LogsContain(t, hook, "depth=1") diff --git a/beacon-chain/blockchain/metrics.go b/beacon-chain/blockchain/metrics.go index 59a4a92dd7..83b462458f 100644 --- a/beacon-chain/blockchain/metrics.go +++ b/beacon-chain/blockchain/metrics.go @@ -338,7 +338,7 @@ func reportEpochMetrics(ctx context.Context, postState, headState state.BeaconSt return err } default: - return errors.Errorf("invalid state type provided: %T", headState.InnerStateUnsafe()) + return errors.Errorf("invalid state type provided: %T", headState.ToProtoUnsafe()) } prevEpochActiveBalances.Set(float64(b.ActivePrevEpoch)) prevEpochSourceBalances.Set(float64(b.PrevEpochAttested)) diff --git a/beacon-chain/blockchain/process_attestation_test.go b/beacon-chain/blockchain/process_attestation_test.go index 12839bfed3..849be0058d 100644 --- a/beacon-chain/blockchain/process_attestation_test.go +++ b/beacon-chain/blockchain/process_attestation_test.go @@ -256,7 +256,7 @@ func TestStore_UpdateCheckpointState(t *testing.T) { cached, err = service.checkpointStateCache.StateByCheckpoint(newCheckpoint) require.NoError(t, err) - require.DeepSSZEqual(t, returned.InnerStateUnsafe(), cached.InnerStateUnsafe()) + require.DeepSSZEqual(t, returned.ToProtoUnsafe(), cached.ToProtoUnsafe()) } func TestAttEpoch_MatchPrevEpoch(t *testing.T) { diff --git a/beacon-chain/blockchain/service_test.go b/beacon-chain/blockchain/service_test.go index b4df415410..22503a203f 100644 --- a/beacon-chain/blockchain/service_test.go +++ b/beacon-chain/blockchain/service_test.go @@ -83,7 +83,7 @@ func setupBeaconChain(t *testing.T, beaconDB db.Database) *Service { srv.Stop() }) bState, _ := util.DeterministicGenesisState(t, 10) - pbState, err := state_native.ProtobufBeaconStatePhase0(bState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStatePhase0(bState.ToProtoUnsafe()) require.NoError(t, err) mockTrie, err := trie.NewTrie(0) require.NoError(t, err) @@ -325,7 +325,7 @@ func TestChainService_InitializeChainInfo(t *testing.T) { assert.DeepEqual(t, headBlock, pb, "Head block incorrect") s, err := c.HeadState(ctx) require.NoError(t, err) - assert.DeepSSZEqual(t, headState.InnerStateUnsafe(), s.InnerStateUnsafe(), "Head state incorrect") + assert.DeepSSZEqual(t, headState.ToProtoUnsafe(), s.ToProtoUnsafe(), "Head state incorrect") assert.Equal(t, c.HeadSlot(), headBlock.Block.Slot, "Head slot incorrect") r, err := c.HeadRoot(context.Background()) require.NoError(t, err) @@ -380,7 +380,7 @@ func TestChainService_InitializeChainInfo_SetHeadAtGenesis(t *testing.T) { require.NoError(t, c.StartFromSavedState(headState)) s, err := c.HeadState(ctx) require.NoError(t, err) - assert.DeepSSZEqual(t, headState.InnerStateUnsafe(), s.InnerStateUnsafe(), "Head state incorrect") + assert.DeepSSZEqual(t, headState.ToProtoUnsafe(), s.ToProtoUnsafe(), "Head state incorrect") assert.Equal(t, genesisRoot, c.originBlockRoot, "Genesis block root incorrect") pb, err := c.head.block.Proto() require.NoError(t, err) diff --git a/beacon-chain/cache/checkpoint_state_test.go b/beacon-chain/cache/checkpoint_state_test.go index dbdf920a5c..3245dd5229 100644 --- a/beacon-chain/cache/checkpoint_state_test.go +++ b/beacon-chain/cache/checkpoint_state_test.go @@ -33,9 +33,9 @@ func TestCheckpointStateCache_StateByCheckpoint(t *testing.T) { s, err = cache.StateByCheckpoint(cp1) require.NoError(t, err) - pbState1, err := state_native.ProtobufBeaconStatePhase0(s.InnerStateUnsafe()) + pbState1, err := state_native.ProtobufBeaconStatePhase0(s.ToProtoUnsafe()) require.NoError(t, err) - pbstate, err := state_native.ProtobufBeaconStatePhase0(st.InnerStateUnsafe()) + pbstate, err := state_native.ProtobufBeaconStatePhase0(st.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState1, pbstate) { t.Error("incorrectly cached state") @@ -50,11 +50,11 @@ func TestCheckpointStateCache_StateByCheckpoint(t *testing.T) { s, err = cache.StateByCheckpoint(cp2) require.NoError(t, err) - assert.DeepEqual(t, st2.CloneInnerState(), s.CloneInnerState(), "incorrectly cached state") + assert.DeepEqual(t, st2.ToProto(), s.ToProto(), "incorrectly cached state") s, err = cache.StateByCheckpoint(cp1) require.NoError(t, err) - assert.DeepEqual(t, st.CloneInnerState(), s.CloneInnerState(), "incorrectly cached state") + assert.DeepEqual(t, st.ToProto(), s.ToProto(), "incorrectly cached state") } func TestCheckpointStateCache_MaxSize(t *testing.T) { diff --git a/beacon-chain/cache/skip_slot_cache_test.go b/beacon-chain/cache/skip_slot_cache_test.go index 3d7bacfd3d..412853435f 100644 --- a/beacon-chain/cache/skip_slot_cache_test.go +++ b/beacon-chain/cache/skip_slot_cache_test.go @@ -33,5 +33,5 @@ func TestSkipSlotCache_RoundTrip(t *testing.T) { res, err := c.Get(ctx, r) require.NoError(t, err) - assert.DeepEqual(t, res.CloneInnerState(), s.CloneInnerState(), "Expected equal protos to return from cache") + assert.DeepEqual(t, res.ToProto(), s.ToProto(), "Expected equal protos to return from cache") } diff --git a/beacon-chain/core/transition/benchmarks_test.go b/beacon-chain/core/transition/benchmarks_test.go index 7b66ce310e..a69e00d476 100644 --- a/beacon-chain/core/transition/benchmarks_test.go +++ b/beacon-chain/core/transition/benchmarks_test.go @@ -124,7 +124,7 @@ func BenchmarkHashTreeRootState_FullState(b *testing.B) { func BenchmarkMarshalState_FullState(b *testing.B) { beaconState, err := benchmark.PreGenstateFullEpochs() require.NoError(b, err) - natState, err := state_native.ProtobufBeaconStatePhase0(beaconState.InnerStateUnsafe()) + natState, err := state_native.ProtobufBeaconStatePhase0(beaconState.ToProtoUnsafe()) require.NoError(b, err) b.Run("Proto_Marshal", func(b *testing.B) { b.ResetTimer() @@ -148,7 +148,7 @@ func BenchmarkMarshalState_FullState(b *testing.B) { func BenchmarkUnmarshalState_FullState(b *testing.B) { beaconState, err := benchmark.PreGenstateFullEpochs() require.NoError(b, err) - natState, err := state_native.ProtobufBeaconStatePhase0(beaconState.InnerStateUnsafe()) + natState, err := state_native.ProtobufBeaconStatePhase0(beaconState.ToProtoUnsafe()) require.NoError(b, err) protoObject, err := proto.Marshal(natState) require.NoError(b, err) diff --git a/beacon-chain/core/transition/skip_slot_cache_test.go b/beacon-chain/core/transition/skip_slot_cache_test.go index 92066792f4..73a7b7e37f 100644 --- a/beacon-chain/core/transition/skip_slot_cache_test.go +++ b/beacon-chain/core/transition/skip_slot_cache_test.go @@ -20,7 +20,7 @@ func TestSkipSlotCache_OK(t *testing.T) { transition.SkipSlotCache.Enable() defer transition.SkipSlotCache.Disable() bState, privs := util.DeterministicGenesisState(t, params.MinimalSpecConfig().MinGenesisActiveValidatorCount) - pbState, err := state_native.ProtobufBeaconStatePhase0(bState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStatePhase0(bState.ToProto()) require.NoError(t, err) originalState, err := state_native.InitializeFromProtoPhase0(pbState) require.NoError(t, err) @@ -42,12 +42,12 @@ func TestSkipSlotCache_OK(t *testing.T) { bState, err = transition.ExecuteStateTransition(context.Background(), bState, wsb) require.NoError(t, err, "Could not process state transition") - assert.DeepEqual(t, originalState.CloneInnerState(), bState.CloneInnerState(), "Skipped slots cache leads to different states") + assert.DeepEqual(t, originalState.ToProto(), bState.ToProto(), "Skipped slots cache leads to different states") } func TestSkipSlotCache_ConcurrentMixup(t *testing.T) { bState, privs := util.DeterministicGenesisState(t, params.MinimalSpecConfig().MinGenesisActiveValidatorCount) - pbState, err := state_native.ProtobufBeaconStatePhase0(bState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStatePhase0(bState.ToProto()) require.NoError(t, err) originalState, err := state_native.InitializeFromProtoPhase0(pbState) require.NoError(t, err) diff --git a/beacon-chain/core/transition/state_test.go b/beacon-chain/core/transition/state_test.go index 8df3594f66..96707cb34b 100644 --- a/beacon-chain/core/transition/state_test.go +++ b/beacon-chain/core/transition/state_test.go @@ -98,9 +98,9 @@ func TestGenesisState_HashEquality(t *testing.T) { state, err := transition.GenesisBeaconState(context.Background(), deposits, 0, ðpb.Eth1Data{BlockHash: make([]byte, 32)}) require.NoError(t, err) - pbState1, err := state_native.ProtobufBeaconStatePhase0(state1.CloneInnerState()) + pbState1, err := state_native.ProtobufBeaconStatePhase0(state1.ToProto()) require.NoError(t, err) - pbstate, err := state_native.ProtobufBeaconStatePhase0(state.CloneInnerState()) + pbstate, err := state_native.ProtobufBeaconStatePhase0(state.ToProto()) require.NoError(t, err) root1, err1 := hash.HashProto(pbState1) diff --git a/beacon-chain/db/kv/migration_state_validators_test.go b/beacon-chain/db/kv/migration_state_validators_test.go index 99c2fdbae2..87b90c3016 100644 --- a/beacon-chain/db/kv/migration_state_validators_test.go +++ b/beacon-chain/db/kv/migration_state_validators_test.go @@ -92,7 +92,7 @@ func Test_migrateStateValidators(t *testing.T) { assert.NoError(t, hashErr) individualHashes = append(individualHashes, hash[:]) } - pbState, err := state_native.ProtobufBeaconStatePhase0(st.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStatePhase0(st.ToProtoUnsafe()) assert.NoError(t, err) validatorsFoundCount := 0 for _, val := range pbState.Validators { @@ -138,7 +138,7 @@ func Test_migrateStateValidators(t *testing.T) { blockRoot := [32]byte{'A'} rcvdState, err := dbStore.State(context.Background(), blockRoot) assert.NoError(t, err) - require.DeepSSZEqual(t, rcvdState.InnerStateUnsafe(), state.InnerStateUnsafe(), "saved state with validators and retrieved state are not matching") + require.DeepSSZEqual(t, rcvdState.ToProtoUnsafe(), state.ToProtoUnsafe(), "saved state with validators and retrieved state are not matching") // find hashes of the validators that are set as part of the state var hashes []byte @@ -151,7 +151,7 @@ func Test_migrateStateValidators(t *testing.T) { } // check if all the validators that were in the state, are stored properly in the validator bucket - pbState, err := state_native.ProtobufBeaconStatePhase0(rcvdState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStatePhase0(rcvdState.ToProtoUnsafe()) assert.NoError(t, err) validatorsFoundCount := 0 for _, val := range pbState.Validators { @@ -241,7 +241,7 @@ func Test_migrateAltairStateValidators(t *testing.T) { blockRoot := [32]byte{'A'} rcvdState, err := dbStore.State(context.Background(), blockRoot) assert.NoError(t, err) - require.DeepSSZEqual(t, rcvdState.InnerStateUnsafe(), state.InnerStateUnsafe(), "saved state with validators and retrieved state are not matching") + require.DeepSSZEqual(t, rcvdState.ToProtoUnsafe(), state.ToProtoUnsafe(), "saved state with validators and retrieved state are not matching") // find hashes of the validators that are set as part of the state var hashes []byte @@ -254,7 +254,7 @@ func Test_migrateAltairStateValidators(t *testing.T) { } // check if all the validators that were in the state, are stored properly in the validator bucket - pbState, err := state_native.ProtobufBeaconStateAltair(rcvdState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStateAltair(rcvdState.ToProtoUnsafe()) assert.NoError(t, err) validatorsFoundCount := 0 for _, val := range pbState.Validators { diff --git a/beacon-chain/db/kv/state.go b/beacon-chain/db/kv/state.go index 2c0490dc78..b4138b5d18 100644 --- a/beacon-chain/db/kv/state.go +++ b/beacon-chain/db/kv/state.go @@ -189,7 +189,7 @@ func getValidators(states []state.ReadOnlyBeaconState) ([][]byte, map[string]*et validatorsEntries := make(map[string]*ethpb.Validator) // It's a map to make sure that you store only new validator entries. validatorKeys := make([][]byte, len(states)) // For every state, this stores a compressed list of validator keys. for i, st := range states { - pb, ok := st.InnerStateUnsafe().(withValidators) + pb, ok := st.ToProtoUnsafe().(withValidators) if !ok { return nil, nil, errors.New("could not cast state to interface with GetValidators()") } @@ -228,7 +228,7 @@ func (s *Store) saveStatesEfficientInternal(ctx context.Context, tx *bolt.Tx, bl // validator entries.To bring the gap closer, we empty the validators // just before Put() and repopulate that state with original validators. // look at issue https://github.com/prysmaticlabs/prysm/issues/9262. - switch rawType := states[i].InnerStateUnsafe().(type) { + switch rawType := states[i].ToProtoUnsafe().(type) { case *ethpb.BeaconState: pbState, err := statenative.ProtobufBeaconStatePhase0(rawType) if err != nil { @@ -534,15 +534,15 @@ func (s *Store) unmarshalState(_ context.Context, enc []byte, validatorEntries [ // marshal versioned state from struct type down to bytes. func marshalState(ctx context.Context, st state.ReadOnlyBeaconState) ([]byte, error) { - switch st.InnerStateUnsafe().(type) { + switch st.ToProtoUnsafe().(type) { case *ethpb.BeaconState: - rState, ok := st.InnerStateUnsafe().(*ethpb.BeaconState) + rState, ok := st.ToProtoUnsafe().(*ethpb.BeaconState) if !ok { return nil, errors.New("non valid inner state") } return encode(ctx, rState) case *ethpb.BeaconStateAltair: - rState, ok := st.InnerStateUnsafe().(*ethpb.BeaconStateAltair) + rState, ok := st.ToProtoUnsafe().(*ethpb.BeaconStateAltair) if !ok { return nil, errors.New("non valid inner state") } @@ -555,7 +555,7 @@ func marshalState(ctx context.Context, st state.ReadOnlyBeaconState) ([]byte, er } return snappy.Encode(nil, append(altairKey, rawObj...)), nil case *ethpb.BeaconStateBellatrix: - rState, ok := st.InnerStateUnsafe().(*ethpb.BeaconStateBellatrix) + rState, ok := st.ToProtoUnsafe().(*ethpb.BeaconStateBellatrix) if !ok { return nil, errors.New("non valid inner state") } @@ -568,7 +568,7 @@ func marshalState(ctx context.Context, st state.ReadOnlyBeaconState) ([]byte, er } return snappy.Encode(nil, append(bellatrixKey, rawObj...)), nil case *ethpb.BeaconStateCapella: - rState, ok := st.InnerStateUnsafe().(*ethpb.BeaconStateCapella) + rState, ok := st.ToProtoUnsafe().(*ethpb.BeaconStateCapella) if !ok { return nil, errors.New("non valid inner state") } diff --git a/beacon-chain/db/kv/state_test.go b/beacon-chain/db/kv/state_test.go index 8233fc5cdb..d9eb146f1c 100644 --- a/beacon-chain/db/kv/state_test.go +++ b/beacon-chain/db/kv/state_test.go @@ -44,7 +44,7 @@ func TestState_CanSaveRetrieve(t *testing.T) { savedS, err := db.State(context.Background(), r) require.NoError(t, err) - require.DeepSSZEqual(t, st.InnerStateUnsafe(), savedS.InnerStateUnsafe(), "saved state and retrieved state are not matching") + require.DeepSSZEqual(t, st.ToProtoUnsafe(), savedS.ToProtoUnsafe(), "saved state and retrieved state are not matching") savedS, err = db.State(context.Background(), [32]byte{'B'}) require.NoError(t, err) @@ -77,7 +77,7 @@ func TestState_CanSaveRetrieveValidatorEntries(t *testing.T) { savedS, err := db.State(context.Background(), r) require.NoError(t, err) - require.DeepSSZEqual(t, st.InnerStateUnsafe(), savedS.InnerStateUnsafe(), "saved state with validators and retrieved state are not matching") + require.DeepSSZEqual(t, st.ToProtoUnsafe(), savedS.ToProtoUnsafe(), "saved state with validators and retrieved state are not matching") // check if the index of the second state is still present. err = db.db.Update(func(tx *bolt.Tx) error { @@ -129,7 +129,7 @@ func TestStateAltair_CanSaveRetrieveValidatorEntries(t *testing.T) { savedS, err := db.State(context.Background(), r) require.NoError(t, err) - require.DeepSSZEqual(t, st.InnerStateUnsafe(), savedS.InnerStateUnsafe(), "saved state with validators and retrieved state are not matching") + require.DeepSSZEqual(t, st.ToProtoUnsafe(), savedS.ToProtoUnsafe(), "saved state with validators and retrieved state are not matching") // check if the index of the second state is still present. err = db.db.Update(func(tx *bolt.Tx) error { @@ -239,7 +239,7 @@ func TestState_CanSaveRetrieveValidatorEntriesWithoutCache(t *testing.T) { savedS, err := db.State(context.Background(), r) require.NoError(t, err) - require.DeepSSZEqual(t, st.InnerStateUnsafe(), savedS.InnerStateUnsafe(), "saved state with validators and retrieved state are not matching") + require.DeepSSZEqual(t, st.ToProtoUnsafe(), savedS.ToProtoUnsafe(), "saved state with validators and retrieved state are not matching") // check if the index of the second state is still present. err = db.db.Update(func(tx *bolt.Tx) error { @@ -360,7 +360,7 @@ func TestGenesisState_CanSaveRetrieve(t *testing.T) { savedGenesisS, err := db.GenesisState(context.Background()) require.NoError(t, err) - assert.DeepSSZEqual(t, st.InnerStateUnsafe(), savedGenesisS.InnerStateUnsafe(), "Did not retrieve saved state") + assert.DeepSSZEqual(t, st.ToProtoUnsafe(), savedGenesisS.ToProtoUnsafe(), "Did not retrieve saved state") require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), [32]byte{'C'})) } @@ -481,7 +481,7 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { st, err := util.NewBeaconState() require.NoError(t, err) require.NoError(t, st.SetSlot(1)) - s0 := st.InnerStateUnsafe() + s0 := st.ToProtoUnsafe() require.NoError(t, db.SaveState(context.Background(), st, r)) b.Block.Slot = 100 @@ -493,7 +493,7 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { st, err = util.NewBeaconState() require.NoError(t, err) require.NoError(t, st.SetSlot(100)) - s1 := st.InnerStateUnsafe() + s1 := st.ToProtoUnsafe() require.NoError(t, db.SaveState(context.Background(), st, r1)) b.Block.Slot = 1000 @@ -505,21 +505,21 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { st, err = util.NewBeaconState() require.NoError(t, err) require.NoError(t, st.SetSlot(1000)) - s2 := st.InnerStateUnsafe() + s2 := st.ToProtoUnsafe() require.NoError(t, db.SaveState(context.Background(), st, r2)) highest, err := db.HighestSlotStatesBelow(context.Background(), 2) require.NoError(t, err) - assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), s0) + assert.DeepSSZEqual(t, highest[0].ToProtoUnsafe(), s0) highest, err = db.HighestSlotStatesBelow(context.Background(), 101) require.NoError(t, err) - assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), s1) + assert.DeepSSZEqual(t, highest[0].ToProtoUnsafe(), s1) highest, err = db.HighestSlotStatesBelow(context.Background(), 1001) require.NoError(t, err) - assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), s2) + assert.DeepSSZEqual(t, highest[0].ToProtoUnsafe(), s2) } func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) { @@ -546,14 +546,14 @@ func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) { highest, err := db.HighestSlotStatesBelow(context.Background(), 2) require.NoError(t, err) - assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), st.InnerStateUnsafe()) + assert.DeepSSZEqual(t, highest[0].ToProtoUnsafe(), st.ToProtoUnsafe()) highest, err = db.HighestSlotStatesBelow(context.Background(), 1) require.NoError(t, err) - assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe()) + assert.DeepSSZEqual(t, highest[0].ToProtoUnsafe(), genesisState.ToProtoUnsafe()) highest, err = db.HighestSlotStatesBelow(context.Background(), 0) require.NoError(t, err) - assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe()) + assert.DeepSSZEqual(t, highest[0].ToProtoUnsafe(), genesisState.ToProtoUnsafe()) } func TestStore_CleanUpDirtyStates_AboveThreshold(t *testing.T) { @@ -680,7 +680,7 @@ func TestAltairState_CanSaveRetrieve(t *testing.T) { savedS, err := db.State(context.Background(), r) require.NoError(t, err) - require.DeepSSZEqual(t, st.InnerStateUnsafe(), savedS.InnerStateUnsafe()) + require.DeepSSZEqual(t, st.ToProtoUnsafe(), savedS.ToProtoUnsafe()) savedS, err = db.State(context.Background(), [32]byte{'B'}) require.NoError(t, err) @@ -831,7 +831,7 @@ func TestStateBellatrix_CanSaveRetrieveValidatorEntries(t *testing.T) { savedS, err := db.State(context.Background(), r) require.NoError(t, err) - require.DeepSSZEqual(t, st.InnerStateUnsafe(), savedS.InnerStateUnsafe(), "saved state with validators and retrieved state are not matching") + require.DeepSSZEqual(t, st.ToProtoUnsafe(), savedS.ToProtoUnsafe(), "saved state with validators and retrieved state are not matching") // check if the index of the second state is still present. err = db.db.Update(func(tx *bolt.Tx) error { @@ -874,7 +874,7 @@ func TestBellatrixState_CanSaveRetrieve(t *testing.T) { savedS, err := db.State(context.Background(), r) require.NoError(t, err) - require.DeepSSZEqual(t, st.InnerStateUnsafe(), savedS.InnerStateUnsafe()) + require.DeepSSZEqual(t, st.ToProtoUnsafe(), savedS.ToProtoUnsafe()) savedS, err = db.State(context.Background(), [32]byte{'B'}) require.NoError(t, err) diff --git a/beacon-chain/execution/log_processing.go b/beacon-chain/execution/log_processing.go index 111a5ac148..98d5544966 100644 --- a/beacon-chain/execution/log_processing.go +++ b/beacon-chain/execution/log_processing.go @@ -550,7 +550,7 @@ func (s *Service) processChainStartIfReady(ctx context.Context, blockHash [32]by // savePowchainData saves all powchain related metadata to disk. func (s *Service) savePowchainData(ctx context.Context) error { - pbState, err := statenative.ProtobufBeaconStatePhase0(s.preGenesisState.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStatePhase0(s.preGenesisState.ToProtoUnsafe()) if err != nil { return err } diff --git a/beacon-chain/execution/service.go b/beacon-chain/execution/service.go index 1f1df303ba..0a3fe3459e 100644 --- a/beacon-chain/execution/service.go +++ b/beacon-chain/execution/service.go @@ -791,7 +791,7 @@ func (s *Service) ensureValidPowchainData(ctx context.Context) error { return errors.Wrap(err, "unable to retrieve eth1 data") } if eth1Data == nil || !eth1Data.ChainstartData.Chainstarted || !validateDepositContainers(eth1Data.DepositContainers) { - pbState, err := native.ProtobufBeaconStatePhase0(s.preGenesisState.InnerStateUnsafe()) + pbState, err := native.ProtobufBeaconStatePhase0(s.preGenesisState.ToProtoUnsafe()) if err != nil { return err } diff --git a/beacon-chain/p2p/encoder/ssz_test.go b/beacon-chain/p2p/encoder/ssz_test.go index d9f4780a46..8a191f1f0b 100644 --- a/beacon-chain/p2p/encoder/ssz_test.go +++ b/beacon-chain/p2p/encoder/ssz_test.go @@ -115,7 +115,7 @@ func TestSszNetworkEncoder_DecodeWithMultipleFrames(t *testing.T) { maxChunkSize := uint64(1 << 22) encoder.MaxChunkSize = maxChunkSize params.OverrideBeaconNetworkConfig(c) - _, err := e.EncodeWithMaxLength(buf, st.InnerStateUnsafe().(*ethpb.BeaconState)) + _, err := e.EncodeWithMaxLength(buf, st.ToProtoUnsafe().(*ethpb.BeaconState)) require.NoError(t, err) // Max snappy block size if buf.Len() <= 76490 { diff --git a/beacon-chain/state/interfaces.go b/beacon-chain/state/interfaces.go index d0390f6c25..6397684ecb 100644 --- a/beacon-chain/state/interfaces.go +++ b/beacon-chain/state/interfaces.go @@ -49,8 +49,8 @@ type ReadOnlyBeaconState interface { ReadOnlyBalances ReadOnlyCheckpoint ReadOnlyAttestations - InnerStateUnsafe() interface{} - CloneInnerState() interface{} + ToProtoUnsafe() interface{} + ToProto() interface{} GenesisTime() uint64 GenesisValidatorsRoot() []byte Slot() types.Slot diff --git a/beacon-chain/state/state-native/BUILD.bazel b/beacon-chain/state/state-native/BUILD.bazel index 9b7b6be577..3bc1691405 100644 --- a/beacon-chain/state/state-native/BUILD.bazel +++ b/beacon-chain/state/state-native/BUILD.bazel @@ -86,6 +86,7 @@ go_test( "references_test.go", "setters_attestation_test.go", "setters_withdrawal_test.go", + "state_fuzz_test.go", "state_test.go", "state_trie_test.go", "types_test.go", @@ -95,6 +96,7 @@ go_test( }), embed = [":go_default_library"], deps = [ + "//beacon-chain/core/transition:go_default_library", "//beacon-chain/state:go_default_library", "//beacon-chain/state/state-native/types:go_default_library", "//beacon-chain/state/stateutil:go_default_library", @@ -104,6 +106,7 @@ go_test( "//consensus-types/blocks:go_default_library", "//consensus-types/primitives:go_default_library", "//container/trie:go_default_library", + "//crypto/rand:go_default_library", "//encoding/bytesutil:go_default_library", "//proto/engine/v1:go_default_library", "//proto/prysm/v1alpha1:go_default_library", diff --git a/beacon-chain/state/state-native/beacon_state_mainnet.go b/beacon-chain/state/state-native/beacon_state_mainnet.go index ac20a2ab35..722bcae8c2 100644 --- a/beacon-chain/state/state-native/beacon_state_mainnet.go +++ b/beacon-chain/state/state-native/beacon_state_mainnet.go @@ -20,7 +20,7 @@ import ( type BeaconState struct { version int genesisTime uint64 `ssz-gen:"true"` - genesisValidatorsRoot customtypes.Byte32 `ssz-gen:"true" ssz-size:"32"` + genesisValidatorsRoot [32]byte `ssz-gen:"true" ssz-size:"32"` slot eth2types.Slot `ssz-gen:"true"` fork *ethpb.Fork `ssz-gen:"true"` latestBlockHeader *ethpb.BeaconBlockHeader `ssz-gen:"true"` diff --git a/beacon-chain/state/state-native/beacon_state_minimal.go b/beacon-chain/state/state-native/beacon_state_minimal.go index a5de92ea9f..0fd83f7055 100644 --- a/beacon-chain/state/state-native/beacon_state_minimal.go +++ b/beacon-chain/state/state-native/beacon_state_minimal.go @@ -20,7 +20,7 @@ import ( type BeaconState struct { version int genesisTime uint64 `ssz-gen:"true"` - genesisValidatorsRoot customtypes.Byte32 `ssz-gen:"true" ssz-size:"32"` + genesisValidatorsRoot [32]byte `ssz-gen:"true" ssz-size:"32"` slot eth2types.Slot `ssz-gen:"true"` fork *ethpb.Fork `ssz-gen:"true"` latestBlockHeader *ethpb.BeaconBlockHeader `ssz-gen:"true"` diff --git a/beacon-chain/state/state-native/custom-types/BUILD.bazel b/beacon-chain/state/state-native/custom-types/BUILD.bazel index d6580c117b..c2fdb8ed93 100644 --- a/beacon-chain/state/state-native/custom-types/BUILD.bazel +++ b/beacon-chain/state/state-native/custom-types/BUILD.bazel @@ -4,7 +4,6 @@ go_library( name = "go_default_library", srcs = [ "block_roots.go", - "byte32.go", "historical_roots.go", "randao_mixes.go", "state_roots.go", @@ -21,7 +20,6 @@ go_test( name = "go_default_test", srcs = [ "block_roots_test.go", - "byte32_test.go", "historical_roots_test.go", "randao_mixes_test.go", "state_roots_test.go", diff --git a/beacon-chain/state/state-native/custom-types/byte32.go b/beacon-chain/state/state-native/custom-types/byte32.go deleted file mode 100644 index 9ba119f6d6..0000000000 --- a/beacon-chain/state/state-native/custom-types/byte32.go +++ /dev/null @@ -1,56 +0,0 @@ -package customtypes - -import ( - "fmt" - - fssz "github.com/prysmaticlabs/fastssz" -) - -var _ fssz.HashRoot = (Byte32)([32]byte{}) -var _ fssz.Marshaler = (*Byte32)(nil) -var _ fssz.Unmarshaler = (*Byte32)(nil) - -// Byte32 represents a 32 bytes Byte32 object in Ethereum beacon chain consensus. -type Byte32 [32]byte - -// HashTreeRoot returns calculated hash root. -func (e Byte32) HashTreeRoot() ([32]byte, error) { - return fssz.HashWithDefaultHasher(e) -} - -// HashTreeRootWith hashes a Byte32 object with a Hasher from the default HasherPool. -func (e Byte32) HashTreeRootWith(hh *fssz.Hasher) error { - hh.PutBytes(e[:]) - return nil -} - -// UnmarshalSSZ deserializes the provided bytes buffer into the Byte32 object. -func (e *Byte32) UnmarshalSSZ(buf []byte) error { - if len(buf) != e.SizeSSZ() { - return fmt.Errorf("expected buffer of length %d received %d", e.SizeSSZ(), len(buf)) - } - - var b Byte32 - copy(b[:], buf) - *e = b - return nil -} - -// MarshalSSZTo marshals Byte32 with the provided byte slice. -func (e *Byte32) MarshalSSZTo(dst []byte) ([]byte, error) { - marshalled, err := e.MarshalSSZ() - if err != nil { - return nil, err - } - return append(dst, marshalled...), nil -} - -// MarshalSSZ marshals Byte32 into a serialized object. -func (e *Byte32) MarshalSSZ() ([]byte, error) { - return e[:], nil -} - -// SizeSSZ returns the size of the serialized object. -func (_ *Byte32) SizeSSZ() int { - return 32 -} diff --git a/beacon-chain/state/state-native/custom-types/byte32_test.go b/beacon-chain/state/state-native/custom-types/byte32_test.go deleted file mode 100644 index a92a3048ef..0000000000 --- a/beacon-chain/state/state-native/custom-types/byte32_test.go +++ /dev/null @@ -1,70 +0,0 @@ -package customtypes - -import ( - "reflect" - "testing" -) - -func TestByte32_Casting(t *testing.T) { - var b [32]byte - d := Byte32(b) - if !reflect.DeepEqual([32]byte(d), b) { - t.Errorf("Unequal: %v = %v", d, b) - } -} - -func TestByte32_UnmarshalSSZ(t *testing.T) { - t.Run("Ok", func(t *testing.T) { - d := Byte32{} - var b = [32]byte{'f', 'o', 'o'} - err := d.UnmarshalSSZ(b[:]) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - if !reflect.DeepEqual(b, [32]byte(d)) { - t.Errorf("Unequal: %v = %v", b, [32]byte(d)) - } - }) - - t.Run("Wrong slice length", func(t *testing.T) { - d := Byte32{} - var b = [16]byte{'f', 'o', 'o'} - err := d.UnmarshalSSZ(b[:]) - if err == nil { - t.Error("Expected error") - } - }) -} - -func TestByte32_MarshalSSZTo(t *testing.T) { - d := Byte32{'f', 'o', 'o'} - dst := []byte("bar") - b, err := d.MarshalSSZTo(dst) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - rest := [29]byte{} - expected := append([]byte("barfoo"), rest[:]...) - if !reflect.DeepEqual(expected, b) { - t.Errorf("Unequal: %v = %v", expected, b) - } -} - -func TestByte32_MarshalSSZ(t *testing.T) { - d := Byte32{'f', 'o', 'o'} - b, err := d.MarshalSSZ() - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - actual := [32]byte(d) - if !reflect.DeepEqual(b, actual[:]) { - t.Errorf("Unequal: %v = %v", b, [32]byte(d)) - } -} - -func TestByte32_SizeSSZ(t *testing.T) { - d := Byte32{} - if d.SizeSSZ() != 32 { - t.Errorf("Wrong SSZ size. Expected %v vs actual %v", 32, d.SizeSSZ()) - } -} diff --git a/beacon-chain/state/state-native/getters_state.go b/beacon-chain/state/state-native/getters_state.go index fa7de16538..177e727e93 100644 --- a/beacon-chain/state/state-native/getters_state.go +++ b/beacon-chain/state/state-native/getters_state.go @@ -338,14 +338,3 @@ func ProtobufBeaconStateCapella(s interface{}) (*ethpb.BeaconStateCapella, error } return pbState, nil } - -// InnerStateUnsafe returns the pointer value of the underlying -// beacon state proto object, bypassing immutability. Use with care. -func (b *BeaconState) InnerStateUnsafe() interface{} { - return b.ToProtoUnsafe() -} - -// CloneInnerState the beacon state into a protobuf for usage. -func (b *BeaconState) CloneInnerState() interface{} { - return b.ToProto() -} diff --git a/beacon-chain/state/state-native/hasher_test.go b/beacon-chain/state/state-native/hasher_test.go index 354af1add1..8d724aa5c8 100644 --- a/beacon-chain/state/state-native/hasher_test.go +++ b/beacon-chain/state/state-native/hasher_test.go @@ -45,7 +45,7 @@ func TestComputeFieldRootsWithHasher_Phase0(t *testing.T) { nativeState, ok := beaconState.(*statenative.BeaconState) require.Equal(t, true, ok) - protoState, ok := nativeState.InnerStateUnsafe().(*ethpb.BeaconState) + protoState, ok := nativeState.ToProtoUnsafe().(*ethpb.BeaconState) require.Equal(t, true, ok) initState, err := statenative.InitializeFromProtoPhase0(protoState) @@ -112,7 +112,7 @@ func TestComputeFieldRootsWithHasher_Altair(t *testing.T) { nativeState, ok := beaconState.(*statenative.BeaconState) require.Equal(t, true, ok) - protoState, ok := nativeState.InnerStateUnsafe().(*ethpb.BeaconStateAltair) + protoState, ok := nativeState.ToProtoUnsafe().(*ethpb.BeaconStateAltair) require.Equal(t, true, ok) initState, err := statenative.InitializeFromProtoAltair(protoState) require.NoError(t, err) @@ -185,7 +185,7 @@ func TestComputeFieldRootsWithHasher_Bellatrix(t *testing.T) { nativeState, ok := beaconState.(*statenative.BeaconState) require.Equal(t, true, ok) - protoState, ok := nativeState.InnerStateUnsafe().(*ethpb.BeaconStateBellatrix) + protoState, ok := nativeState.ToProtoUnsafe().(*ethpb.BeaconStateBellatrix) require.Equal(t, true, ok) initState, err := statenative.InitializeFromProtoBellatrix(protoState) require.NoError(t, err) @@ -262,7 +262,7 @@ func TestComputeFieldRootsWithHasher_Capella(t *testing.T) { nativeState, ok := beaconState.(*statenative.BeaconState) require.Equal(t, true, ok) - protoState, ok := nativeState.InnerStateUnsafe().(*ethpb.BeaconStateCapella) + protoState, ok := nativeState.ToProtoUnsafe().(*ethpb.BeaconStateCapella) require.Equal(t, true, ok) initState, err := statenative.InitializeFromProtoCapella(protoState) require.NoError(t, err) diff --git a/beacon-chain/state/state-native/setters_attestation_test.go b/beacon-chain/state/state-native/setters_attestation_test.go index ead3297411..ce3b493827 100644 --- a/beacon-chain/state/state-native/setters_attestation_test.go +++ b/beacon-chain/state/state-native/setters_attestation_test.go @@ -1,10 +1,14 @@ package state_native import ( + "context" "testing" + nativetypes "github.com/prysmaticlabs/prysm/v3/beacon-chain/state/state-native/types" + "github.com/prysmaticlabs/prysm/v3/config/params" types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives" ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" + "github.com/prysmaticlabs/prysm/v3/testing/assert" "github.com/prysmaticlabs/prysm/v3/testing/require" ) @@ -22,3 +26,51 @@ func TestBeaconState_RotateAttestations(t *testing.T) { require.Equal(t, 0, len(s.currentEpochAttestationsVal())) require.Equal(t, types.Slot(456), s.previousEpochAttestationsVal()[0].Data.Slot) } + +func TestAppendBeyondIndicesLimit(t *testing.T) { + zeroHash := params.BeaconConfig().ZeroHash + mockblockRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot) + for i := 0; i < len(mockblockRoots); i++ { + mockblockRoots[i] = zeroHash[:] + } + + mockstateRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot) + for i := 0; i < len(mockstateRoots); i++ { + mockstateRoots[i] = zeroHash[:] + } + mockrandaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector) + for i := 0; i < len(mockrandaoMixes); i++ { + mockrandaoMixes[i] = zeroHash[:] + } + st, err := InitializeFromProtoPhase0(ðpb.BeaconState{ + Slot: 1, + CurrentEpochAttestations: []*ethpb.PendingAttestation{{Data: ðpb.AttestationData{Slot: 456}}}, + PreviousEpochAttestations: []*ethpb.PendingAttestation{{Data: ðpb.AttestationData{Slot: 123}}}, + Validators: []*ethpb.Validator{}, + Eth1Data: ðpb.Eth1Data{}, + BlockRoots: mockblockRoots, + StateRoots: mockstateRoots, + RandaoMixes: mockrandaoMixes, + }) + require.NoError(t, err) + _, err = st.HashTreeRoot(context.Background()) + require.NoError(t, err) + s, ok := st.(*BeaconState) + require.Equal(t, true, ok) + for i := nativetypes.FieldIndex(0); i < nativetypes.FieldIndex(params.BeaconConfig().BeaconStateFieldCount); i++ { + s.dirtyFields[i] = true + } + _, err = st.HashTreeRoot(context.Background()) + require.NoError(t, err) + for i := 0; i < 10; i++ { + assert.NoError(t, st.AppendValidator(ðpb.Validator{})) + } + assert.Equal(t, false, s.rebuildTrie[nativetypes.Validators]) + assert.NotEqual(t, len(s.dirtyIndices[nativetypes.Validators]), 0) + + for i := 0; i < indicesLimit; i++ { + assert.NoError(t, st.AppendValidator(ðpb.Validator{})) + } + assert.Equal(t, true, s.rebuildTrie[nativetypes.Validators]) + assert.Equal(t, len(s.dirtyIndices[nativetypes.Validators]), 0) +} diff --git a/beacon-chain/state/state-native/state_fuzz_test.go b/beacon-chain/state/state-native/state_fuzz_test.go new file mode 100644 index 0000000000..cf36943df9 --- /dev/null +++ b/beacon-chain/state/state-native/state_fuzz_test.go @@ -0,0 +1,294 @@ +package state_native_test + +import ( + "context" + "testing" + + coreState "github.com/prysmaticlabs/prysm/v3/beacon-chain/core/transition" + native "github.com/prysmaticlabs/prysm/v3/beacon-chain/state/state-native" + types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives" + "github.com/prysmaticlabs/prysm/v3/crypto/rand" + "github.com/prysmaticlabs/prysm/v3/encoding/bytesutil" + ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" + "github.com/prysmaticlabs/prysm/v3/testing/assert" + "github.com/prysmaticlabs/prysm/v3/testing/util" +) + +func FuzzPhase0StateHashTreeRoot(f *testing.F) { + gState, _ := util.DeterministicGenesisState(f, 100) + output, err := gState.MarshalSSZ() + assert.NoError(f, err) + randPool := make([]byte, 100) + _, err = rand.NewDeterministicGenerator().Read(randPool) + assert.NoError(f, err) + f.Add(randPool, uint64(10)) + f.Fuzz(func(t *testing.T, diffBuffer []byte, slotsToTransition uint64) { + stateSSZ := bytesutil.SafeCopyBytes(output) + for i := 0; i < len(diffBuffer); i += 9 { + if i+8 >= len(diffBuffer) { + return + } + num := bytesutil.BytesToUint64BigEndian(diffBuffer[i : i+8]) + num %= uint64(len(diffBuffer)) + // Perform a XOR on the byte of the selected index. + stateSSZ[num] ^= diffBuffer[i+8] + } + pbState := ðpb.BeaconState{} + err := pbState.UnmarshalSSZ(stateSSZ) + if err != nil { + return + } + nativeState, err := native.InitializeFromProtoPhase0(pbState) + assert.NoError(t, err) + + slotsToTransition %= 100 + stateObj, err := native.InitializeFromProtoUnsafePhase0(pbState) + assert.NoError(t, err) + for stateObj.Slot() < types.Slot(slotsToTransition) { + stateObj, err = coreState.ProcessSlots(context.Background(), stateObj, stateObj.Slot()+1) + assert.NoError(t, err) + stateObj.Copy() + + nativeState, err = coreState.ProcessSlots(context.Background(), nativeState, nativeState.Slot()+1) + assert.NoError(t, err) + nativeState.Copy() + } + assert.NoError(t, err) + // Perform a cold HTR calculation by initializing a new state. + innerState, ok := stateObj.ToProtoUnsafe().(*ethpb.BeaconState) + assert.Equal(t, true, ok, "inner state is a not a beacon state proto") + newState, err := native.InitializeFromProtoUnsafePhase0(innerState) + assert.NoError(t, err) + + newRt, newErr := newState.HashTreeRoot(context.Background()) + rt, err := stateObj.HashTreeRoot(context.Background()) + nativeRt, nativeErr := nativeState.HashTreeRoot(context.Background()) + + assert.Equal(t, newErr != nil, err != nil) + assert.Equal(t, newErr != nil, nativeErr != nil) + if err == nil { + assert.Equal(t, rt, newRt) + assert.Equal(t, rt, nativeRt) + } + + newSSZ, newErr := newState.MarshalSSZ() + stateObjSSZ, err := stateObj.MarshalSSZ() + nativeSSZ, nativeErr := nativeState.MarshalSSZ() + assert.Equal(t, newErr != nil, err != nil) + assert.Equal(t, newErr != nil, nativeErr != nil) + if err == nil { + assert.DeepEqual(t, newSSZ, stateObjSSZ) + assert.DeepEqual(t, newSSZ, nativeSSZ) + } + }) +} + +func FuzzAltairStateHashTreeRoot(f *testing.F) { + gState, _ := util.DeterministicGenesisStateAltair(f, 100) + output, err := gState.MarshalSSZ() + assert.NoError(f, err) + randPool := make([]byte, 100) + _, err = rand.NewDeterministicGenerator().Read(randPool) + assert.NoError(f, err) + f.Add(randPool, uint64(10)) + f.Fuzz(func(t *testing.T, diffBuffer []byte, slotsToTransition uint64) { + stateSSZ := bytesutil.SafeCopyBytes(output) + for i := 0; i < len(diffBuffer); i += 9 { + if i+8 >= len(diffBuffer) { + return + } + num := bytesutil.BytesToUint64BigEndian(diffBuffer[i : i+8]) + num %= uint64(len(diffBuffer)) + // Perform a XOR on the byte of the selected index. + stateSSZ[num] ^= diffBuffer[i+8] + } + pbState := ðpb.BeaconStateAltair{} + err := pbState.UnmarshalSSZ(stateSSZ) + if err != nil { + return + } + nativeState, err := native.InitializeFromProtoAltair(pbState) + if err != nil { + return + } + + slotsToTransition %= 100 + stateObj, err := native.InitializeFromProtoUnsafeAltair(pbState) + assert.NoError(t, err) + for stateObj.Slot() < types.Slot(slotsToTransition) { + stateObj, err = coreState.ProcessSlots(context.Background(), stateObj, stateObj.Slot()+1) + assert.NoError(t, err) + stateObj.Copy() + + nativeState, err = coreState.ProcessSlots(context.Background(), nativeState, nativeState.Slot()+1) + assert.NoError(t, err) + nativeState.Copy() + } + assert.NoError(t, err) + // Perform a cold HTR calculation by initializing a new state. + innerState, ok := stateObj.ToProtoUnsafe().(*ethpb.BeaconStateAltair) + assert.Equal(t, true, ok, "inner state is a not a beacon state altair proto") + newState, err := native.InitializeFromProtoUnsafeAltair(innerState) + assert.NoError(t, err) + + newRt, newErr := newState.HashTreeRoot(context.Background()) + rt, err := stateObj.HashTreeRoot(context.Background()) + nativeRt, nativeErr := nativeState.HashTreeRoot(context.Background()) + assert.Equal(t, newErr != nil, err != nil) + assert.Equal(t, newErr != nil, nativeErr != nil) + if err == nil { + assert.Equal(t, rt, newRt) + assert.Equal(t, rt, nativeRt) + } + + newSSZ, newErr := newState.MarshalSSZ() + stateObjSSZ, err := stateObj.MarshalSSZ() + nativeSSZ, nativeErr := nativeState.MarshalSSZ() + assert.Equal(t, newErr != nil, err != nil) + assert.Equal(t, newErr != nil, nativeErr != nil) + if err == nil { + assert.DeepEqual(t, newSSZ, stateObjSSZ) + assert.DeepEqual(t, newSSZ, nativeSSZ) + } + }) +} + +func FuzzBellatrixStateHashTreeRoot(f *testing.F) { + gState, _ := util.DeterministicGenesisStateBellatrix(f, 100) + output, err := gState.MarshalSSZ() + assert.NoError(f, err) + randPool := make([]byte, 100) + _, err = rand.NewDeterministicGenerator().Read(randPool) + assert.NoError(f, err) + f.Add(randPool, uint64(10)) + f.Fuzz(func(t *testing.T, diffBuffer []byte, slotsToTransition uint64) { + stateSSZ := bytesutil.SafeCopyBytes(output) + for i := 0; i < len(diffBuffer); i += 9 { + if i+8 >= len(diffBuffer) { + return + } + num := bytesutil.BytesToUint64BigEndian(diffBuffer[i : i+8]) + num %= uint64(len(diffBuffer)) + // Perform a XOR on the byte of the selected index. + stateSSZ[num] ^= diffBuffer[i+8] + } + pbState := ðpb.BeaconStateBellatrix{} + err := pbState.UnmarshalSSZ(stateSSZ) + if err != nil { + return + } + nativeState, err := native.InitializeFromProtoBellatrix(pbState) + if err != nil { + return + } + + slotsToTransition %= 100 + stateObj, err := native.InitializeFromProtoUnsafeBellatrix(pbState) + assert.NoError(t, err) + for stateObj.Slot() < types.Slot(slotsToTransition) { + stateObj, err = coreState.ProcessSlots(context.Background(), stateObj, stateObj.Slot()+1) + assert.NoError(t, err) + stateObj.Copy() + + nativeState, err = coreState.ProcessSlots(context.Background(), nativeState, nativeState.Slot()+1) + assert.NoError(t, err) + nativeState.Copy() + } + assert.NoError(t, err) + // Perform a cold HTR calculation by initializing a new state. + innerState, ok := stateObj.ToProtoUnsafe().(*ethpb.BeaconStateBellatrix) + assert.Equal(t, true, ok, "inner state is a not a beacon state bellatrix proto") + newState, err := native.InitializeFromProtoUnsafeBellatrix(innerState) + assert.NoError(t, err) + + newRt, newErr := newState.HashTreeRoot(context.Background()) + rt, err := stateObj.HashTreeRoot(context.Background()) + nativeRt, nativeErr := nativeState.HashTreeRoot(context.Background()) + assert.Equal(t, newErr != nil, err != nil) + assert.Equal(t, newErr != nil, nativeErr != nil) + if err == nil { + assert.Equal(t, rt, newRt) + assert.Equal(t, rt, nativeRt) + } + + newSSZ, newErr := newState.MarshalSSZ() + stateObjSSZ, err := stateObj.MarshalSSZ() + nativeSSZ, nativeErr := nativeState.MarshalSSZ() + assert.Equal(t, newErr != nil, err != nil) + assert.Equal(t, newErr != nil, nativeErr != nil) + if err == nil { + assert.DeepEqual(t, newSSZ, stateObjSSZ) + assert.DeepEqual(t, newSSZ, nativeSSZ) + } + }) +} + +func FuzzCapellaStateHashTreeRoot(f *testing.F) { + gState, _ := util.DeterministicGenesisStateCapella(f, 100) + output, err := gState.MarshalSSZ() + assert.NoError(f, err) + randPool := make([]byte, 100) + _, err = rand.NewDeterministicGenerator().Read(randPool) + assert.NoError(f, err) + f.Add(randPool, uint64(10)) + f.Fuzz(func(t *testing.T, diffBuffer []byte, slotsToTransition uint64) { + stateSSZ := bytesutil.SafeCopyBytes(output) + for i := 0; i < len(diffBuffer); i += 9 { + if i+8 >= len(diffBuffer) { + return + } + num := bytesutil.BytesToUint64BigEndian(diffBuffer[i : i+8]) + num %= uint64(len(diffBuffer)) + // Perform a XOR on the byte of the selected index. + stateSSZ[num] ^= diffBuffer[i+8] + } + pbState := ðpb.BeaconStateCapella{} + err := pbState.UnmarshalSSZ(stateSSZ) + if err != nil { + return + } + nativeState, err := native.InitializeFromProtoCapella(pbState) + if err != nil { + return + } + + slotsToTransition %= 100 + stateObj, err := native.InitializeFromProtoUnsafeCapella(pbState) + assert.NoError(t, err) + for stateObj.Slot() < types.Slot(slotsToTransition) { + stateObj, err = coreState.ProcessSlots(context.Background(), stateObj, stateObj.Slot()+1) + assert.NoError(t, err) + stateObj.Copy() + + nativeState, err = coreState.ProcessSlots(context.Background(), nativeState, nativeState.Slot()+1) + assert.NoError(t, err) + nativeState.Copy() + } + assert.NoError(t, err) + // Perform a cold HTR calculation by initializing a new state. + innerState, ok := stateObj.ToProtoUnsafe().(*ethpb.BeaconStateCapella) + assert.Equal(t, true, ok, "inner state is a not a beacon state bellatrix proto") + newState, err := native.InitializeFromProtoUnsafeCapella(innerState) + assert.NoError(t, err) + + newRt, newErr := newState.HashTreeRoot(context.Background()) + rt, err := stateObj.HashTreeRoot(context.Background()) + nativeRt, nativeErr := nativeState.HashTreeRoot(context.Background()) + assert.Equal(t, newErr != nil, err != nil) + assert.Equal(t, newErr != nil, nativeErr != nil) + if err == nil { + assert.Equal(t, rt, newRt) + assert.Equal(t, rt, nativeRt) + } + + newSSZ, newErr := newState.MarshalSSZ() + stateObjSSZ, err := stateObj.MarshalSSZ() + nativeSSZ, nativeErr := nativeState.MarshalSSZ() + assert.Equal(t, newErr != nil, err != nil) + assert.Equal(t, newErr != nil, nativeErr != nil) + if err == nil { + assert.DeepEqual(t, newSSZ, stateObjSSZ) + assert.DeepEqual(t, newSSZ, nativeSSZ) + } + }) +} diff --git a/beacon-chain/state/state-native/state_trie_test.go b/beacon-chain/state/state-native/state_trie_test.go index e6a19a4e06..6e18d1c066 100644 --- a/beacon-chain/state/state-native/state_trie_test.go +++ b/beacon-chain/state/state-native/state_trie_test.go @@ -17,7 +17,7 @@ import ( func TestInitializeFromProto_Phase0(t *testing.T) { testState, _ := util.DeterministicGenesisState(t, 64) - pbState, err := statenative.ProtobufBeaconStatePhase0(testState.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStatePhase0(testState.ToProtoUnsafe()) require.NoError(t, err) type test struct { name string @@ -168,7 +168,7 @@ func TestInitializeFromProto_Capella(t *testing.T) { func TestInitializeFromProtoUnsafe_Phase0(t *testing.T) { testState, _ := util.DeterministicGenesisState(t, 64) - pbState, err := statenative.ProtobufBeaconStatePhase0(testState.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStatePhase0(testState.ToProtoUnsafe()) require.NoError(t, err) type test struct { name string @@ -350,7 +350,7 @@ func TestBeaconState_HashTreeRoot(t *testing.T) { if err == nil && tt.error != "" { t.Errorf("Expected error, expected %v, recevied %v", tt.error, err) } - pbState, err := statenative.ProtobufBeaconStatePhase0(testState.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStatePhase0(testState.ToProtoUnsafe()) require.NoError(t, err) genericHTR, err := pbState.HashTreeRoot() if err == nil && tt.error != "" { @@ -368,7 +368,7 @@ func TestBeaconState_HashTreeRoot(t *testing.T) { func BenchmarkBeaconState(b *testing.B) { testState, _ := util.DeterministicGenesisState(b, 16000) - pbState, err := statenative.ProtobufBeaconStatePhase0(testState.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStatePhase0(testState.ToProtoUnsafe()) require.NoError(b, err) b.Run("Vectorized SHA256", func(b *testing.B) { @@ -437,7 +437,7 @@ func TestBeaconState_HashTreeRoot_FieldTrie(t *testing.T) { if err == nil && tt.error != "" { t.Errorf("Expected error, expected %v, recevied %v", tt.error, err) } - pbState, err := statenative.ProtobufBeaconStatePhase0(testState.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStatePhase0(testState.ToProtoUnsafe()) require.NoError(t, err) genericHTR, err := pbState.HashTreeRoot() if err == nil && tt.error != "" { @@ -468,7 +468,7 @@ func TestBeaconState_AppendValidator_DoesntMutateCopy(t *testing.T) { func TestBeaconState_ValidatorMutation_Phase0(t *testing.T) { testState, _ := util.DeterministicGenesisState(t, 400) - pbState, err := statenative.ProtobufBeaconStatePhase0(testState.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStatePhase0(testState.ToProtoUnsafe()) require.NoError(t, err) testState, err = statenative.InitializeFromProtoPhase0(pbState) require.NoError(t, err) @@ -497,7 +497,7 @@ func TestBeaconState_ValidatorMutation_Phase0(t *testing.T) { rt, err := testState.HashTreeRoot(context.Background()) require.NoError(t, err) - pbState, err = statenative.ProtobufBeaconStatePhase0(testState.InnerStateUnsafe()) + pbState, err = statenative.ProtobufBeaconStatePhase0(testState.ToProtoUnsafe()) require.NoError(t, err) copiedTestState, err := statenative.InitializeFromProtoPhase0(pbState) @@ -521,7 +521,7 @@ func TestBeaconState_ValidatorMutation_Phase0(t *testing.T) { rt, err = newState1.HashTreeRoot(context.Background()) require.NoError(t, err) - pbState, err = statenative.ProtobufBeaconStatePhase0(newState1.InnerStateUnsafe()) + pbState, err = statenative.ProtobufBeaconStatePhase0(newState1.ToProtoUnsafe()) require.NoError(t, err) copiedTestState, err = statenative.InitializeFromProtoPhase0(pbState) @@ -535,7 +535,7 @@ func TestBeaconState_ValidatorMutation_Phase0(t *testing.T) { func TestBeaconState_ValidatorMutation_Altair(t *testing.T) { testState, _ := util.DeterministicGenesisStateAltair(t, 400) - pbState, err := statenative.ProtobufBeaconStateAltair(testState.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStateAltair(testState.ToProtoUnsafe()) require.NoError(t, err) testState, err = statenative.InitializeFromProtoAltair(pbState) require.NoError(t, err) @@ -564,7 +564,7 @@ func TestBeaconState_ValidatorMutation_Altair(t *testing.T) { rt, err := testState.HashTreeRoot(context.Background()) require.NoError(t, err) - pbState, err = statenative.ProtobufBeaconStateAltair(testState.InnerStateUnsafe()) + pbState, err = statenative.ProtobufBeaconStateAltair(testState.ToProtoUnsafe()) require.NoError(t, err) copiedTestState, err := statenative.InitializeFromProtoAltair(pbState) @@ -588,7 +588,7 @@ func TestBeaconState_ValidatorMutation_Altair(t *testing.T) { rt, err = newState1.HashTreeRoot(context.Background()) require.NoError(t, err) - pbState, err = statenative.ProtobufBeaconStateAltair(newState1.InnerStateUnsafe()) + pbState, err = statenative.ProtobufBeaconStateAltair(newState1.ToProtoUnsafe()) require.NoError(t, err) copiedTestState, err = statenative.InitializeFromProtoAltair(pbState) @@ -602,7 +602,7 @@ func TestBeaconState_ValidatorMutation_Altair(t *testing.T) { func TestBeaconState_ValidatorMutation_Bellatrix(t *testing.T) { testState, _ := util.DeterministicGenesisStateBellatrix(t, 400) - pbState, err := statenative.ProtobufBeaconStateBellatrix(testState.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStateBellatrix(testState.ToProtoUnsafe()) require.NoError(t, err) testState, err = statenative.InitializeFromProtoBellatrix(pbState) require.NoError(t, err) @@ -631,7 +631,7 @@ func TestBeaconState_ValidatorMutation_Bellatrix(t *testing.T) { rt, err := testState.HashTreeRoot(context.Background()) require.NoError(t, err) - pbState, err = statenative.ProtobufBeaconStateBellatrix(testState.InnerStateUnsafe()) + pbState, err = statenative.ProtobufBeaconStateBellatrix(testState.ToProtoUnsafe()) require.NoError(t, err) copiedTestState, err := statenative.InitializeFromProtoBellatrix(pbState) @@ -655,7 +655,7 @@ func TestBeaconState_ValidatorMutation_Bellatrix(t *testing.T) { rt, err = newState1.HashTreeRoot(context.Background()) require.NoError(t, err) - pbState, err = statenative.ProtobufBeaconStateBellatrix(newState1.InnerStateUnsafe()) + pbState, err = statenative.ProtobufBeaconStateBellatrix(newState1.ToProtoUnsafe()) require.NoError(t, err) copiedTestState, err = statenative.InitializeFromProtoBellatrix(pbState) diff --git a/beacon-chain/state/state-native/types_test.go b/beacon-chain/state/state-native/types_test.go index 40bbefde7a..ab3969bc28 100644 --- a/beacon-chain/state/state-native/types_test.go +++ b/beacon-chain/state/state-native/types_test.go @@ -26,7 +26,7 @@ func TestBeaconState_ProtoBeaconStateCompatibility(t *testing.T) { require.NoError(t, err) cloned, ok := proto.Clone(genesis).(*ethpb.BeaconState) assert.Equal(t, true, ok, "Object is not of type *ethpb.BeaconState") - custom := customState.CloneInnerState() + custom := customState.ToProto() assert.DeepSSZEqual(t, cloned, custom) r1, err := customState.HashTreeRoot(ctx) @@ -141,7 +141,7 @@ func BenchmarkStateClone_Manual(b *testing.B) { require.NoError(b, err) b.StartTimer() for i := 0; i < b.N; i++ { - _ = st.CloneInnerState() + _ = st.ToProto() } } @@ -225,7 +225,7 @@ func TestForkManualCopy_OK(t *testing.T) { } require.NoError(t, a.SetFork(wantedFork)) - pbState, err := statenative.ProtobufBeaconStatePhase0(a.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStatePhase0(a.ToProtoUnsafe()) require.NoError(t, err) require.DeepEqual(t, pbState.Fork, wantedFork) } diff --git a/beacon-chain/state/stategen/epoch_boundary_state_cache_test.go b/beacon-chain/state/stategen/epoch_boundary_state_cache_test.go index 904d32820f..89af2c09be 100644 --- a/beacon-chain/state/stategen/epoch_boundary_state_cache_test.go +++ b/beacon-chain/state/stategen/epoch_boundary_state_cache_test.go @@ -35,7 +35,7 @@ func TestEpochBoundaryStateCache_CanSaveAndDelete(t *testing.T) { got, exists, err = e.getByBlockRoot([32]byte{'a'}) require.NoError(t, err) assert.Equal(t, true, exists, "Should exist") - assert.DeepSSZEqual(t, s.InnerStateUnsafe(), got.state.InnerStateUnsafe(), "Should have the same state") + assert.DeepSSZEqual(t, s.ToProtoUnsafe(), got.state.ToProtoUnsafe(), "Should have the same state") got, exists, err = e.getBySlot(2) require.NoError(t, err) @@ -45,7 +45,7 @@ func TestEpochBoundaryStateCache_CanSaveAndDelete(t *testing.T) { got, exists, err = e.getBySlot(1) require.NoError(t, err) assert.Equal(t, true, exists, "Should exist") - assert.DeepSSZEqual(t, s.InnerStateUnsafe(), got.state.InnerStateUnsafe(), "Should have the same state") + assert.DeepSSZEqual(t, s.ToProtoUnsafe(), got.state.ToProtoUnsafe(), "Should have the same state") require.NoError(t, e.delete(r)) got, exists, err = e.getByBlockRoot([32]byte{'b'}) diff --git a/beacon-chain/state/stategen/getter_test.go b/beacon-chain/state/stategen/getter_test.go index dc72a77b23..74a933d5d1 100644 --- a/beacon-chain/state/stategen/getter_test.go +++ b/beacon-chain/state/stategen/getter_test.go @@ -30,7 +30,7 @@ func TestStateByRoot_GenesisState(t *testing.T) { require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, bRoot)) loadedState, err := service.StateByRoot(ctx, params.BeaconConfig().ZeroHash) // Zero hash is genesis state root. require.NoError(t, err) - require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) + require.DeepSSZEqual(t, loadedState.ToProtoUnsafe(), beaconState.ToProtoUnsafe()) } func TestStateByRoot_ColdState(t *testing.T) { @@ -53,7 +53,7 @@ func TestStateByRoot_ColdState(t *testing.T) { require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, bRoot)) loadedState, err := service.StateByRoot(ctx, bRoot) require.NoError(t, err) - require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) + require.DeepSSZEqual(t, loadedState.ToProtoUnsafe(), beaconState.ToProtoUnsafe()) bal, err := service.BalancesByRoot(ctx, bRoot) require.NoError(t, err) @@ -75,7 +75,7 @@ func TestStateByRootIfCachedNoCopy_HotState(t *testing.T) { service.hotStateCache.put(r, beaconState) loadedState := service.StateByRootIfCachedNoCopy(r) - require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) + require.DeepSSZEqual(t, loadedState.ToProtoUnsafe(), beaconState.ToProtoUnsafe()) } func TestStateByRootIfCachedNoCopy_ColdState(t *testing.T) { @@ -157,7 +157,7 @@ func TestStateByRoot_HotStateCached(t *testing.T) { loadedState, err := service.StateByRoot(ctx, r) require.NoError(t, err) - require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) + require.DeepSSZEqual(t, loadedState.ToProtoUnsafe(), beaconState.ToProtoUnsafe()) } func TestDeleteStateFromCaches(t *testing.T) { @@ -203,7 +203,7 @@ func TestStateByRoot_StateByRootInitialSync(t *testing.T) { require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, bRoot)) loadedState, err := service.StateByRootInitialSync(ctx, params.BeaconConfig().ZeroHash) // Zero hash is genesis state root. require.NoError(t, err) - require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) + require.DeepSSZEqual(t, loadedState.ToProtoUnsafe(), beaconState.ToProtoUnsafe()) } func TestStateByRootInitialSync_UseEpochStateCache(t *testing.T) { @@ -237,7 +237,7 @@ func TestStateByRootInitialSync_UseCache(t *testing.T) { loadedState, err := service.StateByRootInitialSync(ctx, r) require.NoError(t, err) - require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) + require.DeepSSZEqual(t, loadedState.ToProtoUnsafe(), beaconState.ToProtoUnsafe()) if service.hotStateCache.has(r) { t.Error("Hot state cache was not invalidated") } @@ -279,7 +279,7 @@ func TestLoadeStateByRoot_Cached(t *testing.T) { // This tests where hot state was already cached. loadedState, err := service.loadStateByRoot(ctx, r) require.NoError(t, err) - require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) + require.DeepSSZEqual(t, loadedState.ToProtoUnsafe(), beaconState.ToProtoUnsafe()) } func TestLoadeStateByRoot_FinalizedState(t *testing.T) { @@ -303,7 +303,7 @@ func TestLoadeStateByRoot_FinalizedState(t *testing.T) { // This tests where hot state was already cached. loadedState, err := service.loadStateByRoot(ctx, gRoot) require.NoError(t, err) - require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) + require.DeepSSZEqual(t, loadedState.ToProtoUnsafe(), beaconState.ToProtoUnsafe()) } func TestLoadeStateByRoot_EpochBoundaryStateCanProcess(t *testing.T) { diff --git a/beacon-chain/state/stategen/hot_state_cache_test.go b/beacon-chain/state/stategen/hot_state_cache_test.go index 2d010acba3..54fdd7ca89 100644 --- a/beacon-chain/state/stategen/hot_state_cache_test.go +++ b/beacon-chain/state/stategen/hot_state_cache_test.go @@ -27,7 +27,7 @@ func TestHotStateCache_RoundTrip(t *testing.T) { res := c.get(root) assert.NotNil(t, s) - assert.DeepEqual(t, res.CloneInnerState(), s.CloneInnerState(), "Expected equal protos to return from cache") + assert.DeepEqual(t, res.ToProto(), s.ToProto(), "Expected equal protos to return from cache") c.delete(root) assert.Equal(t, false, c.has(root), "Cache not supposed to have the object") diff --git a/beacon-chain/state/stategen/migrate_test.go b/beacon-chain/state/stategen/migrate_test.go index 25e4146c24..fbabf7dc1d 100644 --- a/beacon-chain/state/stategen/migrate_test.go +++ b/beacon-chain/state/stategen/migrate_test.go @@ -53,7 +53,7 @@ func TestMigrateToCold_HappyPath(t *testing.T) { gotState, err := service.beaconDB.State(ctx, fRoot) require.NoError(t, err) - assert.DeepSSZEqual(t, beaconState.InnerStateUnsafe(), gotState.InnerStateUnsafe(), "Did not save state") + assert.DeepSSZEqual(t, beaconState.ToProtoUnsafe(), gotState.ToProtoUnsafe(), "Did not save state") gotRoot := service.beaconDB.ArchivedPointRoot(ctx, stateSlot/service.slotsPerArchivedPoint) assert.Equal(t, fRoot, gotRoot, "Did not save archived root") lastIndex, err := service.beaconDB.LastArchivedSlot(ctx) diff --git a/beacon-chain/state/stategen/service_test.go b/beacon-chain/state/stategen/service_test.go index 5c7d3b7436..9777ad7786 100644 --- a/beacon-chain/state/stategen/service_test.go +++ b/beacon-chain/state/stategen/service_test.go @@ -30,7 +30,7 @@ func TestResume(t *testing.T) { resumeState, err := service.Resume(ctx, beaconState) require.NoError(t, err) - require.DeepSSZEqual(t, beaconState.InnerStateUnsafe(), resumeState.InnerStateUnsafe()) + require.DeepSSZEqual(t, beaconState.ToProtoUnsafe(), resumeState.ToProtoUnsafe()) assert.Equal(t, params.BeaconConfig().SlotsPerEpoch, service.finalizedInfo.slot, "Did not get watned slot") assert.Equal(t, service.finalizedInfo.root, root, "Did not get wanted root") assert.NotNil(t, service.finalizedState(), "Wanted a non nil finalized state") diff --git a/proto/prysm/v1alpha1/BUILD.bazel b/proto/prysm/v1alpha1/BUILD.bazel index be51b2d283..eb68b5099a 100644 --- a/proto/prysm/v1alpha1/BUILD.bazel +++ b/proto/prysm/v1alpha1/BUILD.bazel @@ -35,8 +35,8 @@ proto_library( ], visibility = ["//visibility:public"], deps = [ - "//proto/eth/ext:proto", "//proto/engine/v1:proto", + "//proto/eth/ext:proto", "@com_github_grpc_ecosystem_grpc_gateway_v2//protoc-gen-openapiv2/options:options_proto", "@com_google_protobuf//:any_proto", "@com_google_protobuf//:descriptor_proto", @@ -63,6 +63,15 @@ ssz_gen_marshal( "BeaconBlockBellatrix", "BeaconBlockBodyBellatrix", "SignedBeaconBlockBellatrix", + "SignedBlindedBeaconBlockBellatrix", + "BlindedBeaconBlockBellatrix", + "BlindedBeaconBlockBodyBellatrix", + "BeaconBlockCapella", + "BeaconBlockBodyCapella", + "SignedBeaconBlockCapella", + "SignedBlindedBeaconBlockCapella", + "BlindedBeaconBlockCapella", + "BlindedBeaconBlockBodyCapella", "SyncAggregate", "SyncCommitteeMessage", "SyncCommitteeContribution", @@ -105,16 +114,13 @@ ssz_gen_marshal( "SyncCommittee", "SyncAggregatorSelectionData", "PowBlock", - "SignedBlindedBeaconBlockBellatrix", - "BlindedBeaconBlockBellatrix", - "BlindedBeaconBlockBodyBellatrix", "SignedValidatorRegistrationV1", "ValidatorRegistrationV1", - "Withdrawal", + "Withdrawal", "BLSToExecutionChange", "SignedBLSToExecutionChange", - "BuilderBid" - ], + "BuilderBid", + ], ) go_proto_library( @@ -126,11 +132,11 @@ go_proto_library( proto = ":proto", visibility = ["//visibility:public"], deps = [ - "//proto/eth/ext:go_default_library", + "//consensus-types/primitives:go_default_library", "//proto/engine/v1:go_default_library", + "//proto/eth/ext:go_default_library", "@com_github_golang_protobuf//proto:go_default_library", "@com_github_grpc_ecosystem_grpc_gateway_v2//protoc-gen-openapiv2/options:options_go_proto", - "//consensus-types/primitives:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", "@go_googleapis//google/api:annotations_go_proto", "@io_bazel_rules_go//proto/wkt:descriptor_go_proto", @@ -152,8 +158,8 @@ go_proto_library( protos = [":proto"], visibility = ["//visibility:private"], deps = [ - "//proto/eth/ext:go_default_library", "//proto/engine/v1:go_default_library", + "//proto/eth/ext:go_default_library", "@com_github_grpc_ecosystem_grpc_gateway_v2//protoc-gen-openapiv2/options:options_go_proto", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", "@go_googleapis//google/api:annotations_go_proto", diff --git a/proto/prysm/v1alpha1/generated.ssz.go b/proto/prysm/v1alpha1/generated.ssz.go index b80f287433..f12c096d51 100644 --- a/proto/prysm/v1alpha1/generated.ssz.go +++ b/proto/prysm/v1alpha1/generated.ssz.go @@ -1,5 +1,5 @@ // Code generated by fastssz. DO NOT EDIT. -// Hash: 41d28533fab8e97615f7ba793437a8b7e0b364505ee55808e497ea4de8232d96 +// Hash: 1c6f768f80da4249df93fa7adac146024bcebec0eeca716c007888075e03b2b3 package eth import ( @@ -4613,6 +4613,1684 @@ func (b *BlindedBeaconBlockBodyBellatrix) HashTreeRootWith(hh *ssz.Hasher) (err return } +// MarshalSSZ ssz marshals the SignedBeaconBlockCapella object +func (s *SignedBeaconBlockCapella) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(s) +} + +// MarshalSSZTo ssz marshals the SignedBeaconBlockCapella object to a target array +func (s *SignedBeaconBlockCapella) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(100) + + // Offset (0) 'Block' + dst = ssz.WriteOffset(dst, offset) + if s.Block == nil { + s.Block = new(BeaconBlockCapella) + } + offset += s.Block.SizeSSZ() + + // Field (1) 'Signature' + if size := len(s.Signature); size != 96 { + err = ssz.ErrBytesLengthFn("--.Signature", size, 96) + return + } + dst = append(dst, s.Signature...) + + // Field (0) 'Block' + if dst, err = s.Block.MarshalSSZTo(dst); err != nil { + return + } + + return +} + +// UnmarshalSSZ ssz unmarshals the SignedBeaconBlockCapella object +func (s *SignedBeaconBlockCapella) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 100 { + return ssz.ErrSize + } + + tail := buf + var o0 uint64 + + // Offset (0) 'Block' + if o0 = ssz.ReadOffset(buf[0:4]); o0 > size { + return ssz.ErrOffset + } + + if o0 < 100 { + return ssz.ErrInvalidVariableOffset + } + + // Field (1) 'Signature' + if cap(s.Signature) == 0 { + s.Signature = make([]byte, 0, len(buf[4:100])) + } + s.Signature = append(s.Signature, buf[4:100]...) + + // Field (0) 'Block' + { + buf = tail[o0:] + if s.Block == nil { + s.Block = new(BeaconBlockCapella) + } + if err = s.Block.UnmarshalSSZ(buf); err != nil { + return err + } + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the SignedBeaconBlockCapella object +func (s *SignedBeaconBlockCapella) SizeSSZ() (size int) { + size = 100 + + // Field (0) 'Block' + if s.Block == nil { + s.Block = new(BeaconBlockCapella) + } + size += s.Block.SizeSSZ() + + return +} + +// HashTreeRoot ssz hashes the SignedBeaconBlockCapella object +func (s *SignedBeaconBlockCapella) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(s) +} + +// HashTreeRootWith ssz hashes the SignedBeaconBlockCapella object with a hasher +func (s *SignedBeaconBlockCapella) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'Block' + if err = s.Block.HashTreeRootWith(hh); err != nil { + return + } + + // Field (1) 'Signature' + if size := len(s.Signature); size != 96 { + err = ssz.ErrBytesLengthFn("--.Signature", size, 96) + return + } + hh.PutBytes(s.Signature) + + if ssz.EnableVectorizedHTR { + hh.MerkleizeVectorizedHTR(indx) + } else { + hh.Merkleize(indx) + } + return +} + +// MarshalSSZ ssz marshals the BeaconBlockCapella object +func (b *BeaconBlockCapella) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(b) +} + +// MarshalSSZTo ssz marshals the BeaconBlockCapella object to a target array +func (b *BeaconBlockCapella) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(84) + + // Field (0) 'Slot' + dst = ssz.MarshalUint64(dst, uint64(b.Slot)) + + // Field (1) 'ProposerIndex' + dst = ssz.MarshalUint64(dst, uint64(b.ProposerIndex)) + + // Field (2) 'ParentRoot' + if size := len(b.ParentRoot); size != 32 { + err = ssz.ErrBytesLengthFn("--.ParentRoot", size, 32) + return + } + dst = append(dst, b.ParentRoot...) + + // Field (3) 'StateRoot' + if size := len(b.StateRoot); size != 32 { + err = ssz.ErrBytesLengthFn("--.StateRoot", size, 32) + return + } + dst = append(dst, b.StateRoot...) + + // Offset (4) 'Body' + dst = ssz.WriteOffset(dst, offset) + if b.Body == nil { + b.Body = new(BeaconBlockBodyCapella) + } + offset += b.Body.SizeSSZ() + + // Field (4) 'Body' + if dst, err = b.Body.MarshalSSZTo(dst); err != nil { + return + } + + return +} + +// UnmarshalSSZ ssz unmarshals the BeaconBlockCapella object +func (b *BeaconBlockCapella) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 84 { + return ssz.ErrSize + } + + tail := buf + var o4 uint64 + + // Field (0) 'Slot' + b.Slot = github_com_prysmaticlabs_prysm_v3_consensus_types_primitives.Slot(ssz.UnmarshallUint64(buf[0:8])) + + // Field (1) 'ProposerIndex' + b.ProposerIndex = github_com_prysmaticlabs_prysm_v3_consensus_types_primitives.ValidatorIndex(ssz.UnmarshallUint64(buf[8:16])) + + // Field (2) 'ParentRoot' + if cap(b.ParentRoot) == 0 { + b.ParentRoot = make([]byte, 0, len(buf[16:48])) + } + b.ParentRoot = append(b.ParentRoot, buf[16:48]...) + + // Field (3) 'StateRoot' + if cap(b.StateRoot) == 0 { + b.StateRoot = make([]byte, 0, len(buf[48:80])) + } + b.StateRoot = append(b.StateRoot, buf[48:80]...) + + // Offset (4) 'Body' + if o4 = ssz.ReadOffset(buf[80:84]); o4 > size { + return ssz.ErrOffset + } + + if o4 < 84 { + return ssz.ErrInvalidVariableOffset + } + + // Field (4) 'Body' + { + buf = tail[o4:] + if b.Body == nil { + b.Body = new(BeaconBlockBodyCapella) + } + if err = b.Body.UnmarshalSSZ(buf); err != nil { + return err + } + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the BeaconBlockCapella object +func (b *BeaconBlockCapella) SizeSSZ() (size int) { + size = 84 + + // Field (4) 'Body' + if b.Body == nil { + b.Body = new(BeaconBlockBodyCapella) + } + size += b.Body.SizeSSZ() + + return +} + +// HashTreeRoot ssz hashes the BeaconBlockCapella object +func (b *BeaconBlockCapella) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(b) +} + +// HashTreeRootWith ssz hashes the BeaconBlockCapella object with a hasher +func (b *BeaconBlockCapella) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'Slot' + hh.PutUint64(uint64(b.Slot)) + + // Field (1) 'ProposerIndex' + hh.PutUint64(uint64(b.ProposerIndex)) + + // Field (2) 'ParentRoot' + if size := len(b.ParentRoot); size != 32 { + err = ssz.ErrBytesLengthFn("--.ParentRoot", size, 32) + return + } + hh.PutBytes(b.ParentRoot) + + // Field (3) 'StateRoot' + if size := len(b.StateRoot); size != 32 { + err = ssz.ErrBytesLengthFn("--.StateRoot", size, 32) + return + } + hh.PutBytes(b.StateRoot) + + // Field (4) 'Body' + if err = b.Body.HashTreeRootWith(hh); err != nil { + return + } + + if ssz.EnableVectorizedHTR { + hh.MerkleizeVectorizedHTR(indx) + } else { + hh.Merkleize(indx) + } + return +} + +// MarshalSSZ ssz marshals the BeaconBlockBodyCapella object +func (b *BeaconBlockBodyCapella) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(b) +} + +// MarshalSSZTo ssz marshals the BeaconBlockBodyCapella object to a target array +func (b *BeaconBlockBodyCapella) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(388) + + // Field (0) 'RandaoReveal' + if size := len(b.RandaoReveal); size != 96 { + err = ssz.ErrBytesLengthFn("--.RandaoReveal", size, 96) + return + } + dst = append(dst, b.RandaoReveal...) + + // Field (1) 'Eth1Data' + if b.Eth1Data == nil { + b.Eth1Data = new(Eth1Data) + } + if dst, err = b.Eth1Data.MarshalSSZTo(dst); err != nil { + return + } + + // Field (2) 'Graffiti' + if size := len(b.Graffiti); size != 32 { + err = ssz.ErrBytesLengthFn("--.Graffiti", size, 32) + return + } + dst = append(dst, b.Graffiti...) + + // Offset (3) 'ProposerSlashings' + dst = ssz.WriteOffset(dst, offset) + offset += len(b.ProposerSlashings) * 416 + + // Offset (4) 'AttesterSlashings' + dst = ssz.WriteOffset(dst, offset) + for ii := 0; ii < len(b.AttesterSlashings); ii++ { + offset += 4 + offset += b.AttesterSlashings[ii].SizeSSZ() + } + + // Offset (5) 'Attestations' + dst = ssz.WriteOffset(dst, offset) + for ii := 0; ii < len(b.Attestations); ii++ { + offset += 4 + offset += b.Attestations[ii].SizeSSZ() + } + + // Offset (6) 'Deposits' + dst = ssz.WriteOffset(dst, offset) + offset += len(b.Deposits) * 1240 + + // Offset (7) 'VoluntaryExits' + dst = ssz.WriteOffset(dst, offset) + offset += len(b.VoluntaryExits) * 112 + + // Field (8) 'SyncAggregate' + if b.SyncAggregate == nil { + b.SyncAggregate = new(SyncAggregate) + } + if dst, err = b.SyncAggregate.MarshalSSZTo(dst); err != nil { + return + } + + // Offset (9) 'ExecutionPayload' + dst = ssz.WriteOffset(dst, offset) + if b.ExecutionPayload == nil { + b.ExecutionPayload = new(v1.ExecutionPayloadCapella) + } + offset += b.ExecutionPayload.SizeSSZ() + + // Offset (10) 'BlsToExecutionChanges' + dst = ssz.WriteOffset(dst, offset) + offset += len(b.BlsToExecutionChanges) * 172 + + // Field (3) 'ProposerSlashings' + if size := len(b.ProposerSlashings); size > 16 { + err = ssz.ErrListTooBigFn("--.ProposerSlashings", size, 16) + return + } + for ii := 0; ii < len(b.ProposerSlashings); ii++ { + if dst, err = b.ProposerSlashings[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + // Field (4) 'AttesterSlashings' + if size := len(b.AttesterSlashings); size > 2 { + err = ssz.ErrListTooBigFn("--.AttesterSlashings", size, 2) + return + } + { + offset = 4 * len(b.AttesterSlashings) + for ii := 0; ii < len(b.AttesterSlashings); ii++ { + dst = ssz.WriteOffset(dst, offset) + offset += b.AttesterSlashings[ii].SizeSSZ() + } + } + for ii := 0; ii < len(b.AttesterSlashings); ii++ { + if dst, err = b.AttesterSlashings[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + // Field (5) 'Attestations' + if size := len(b.Attestations); size > 128 { + err = ssz.ErrListTooBigFn("--.Attestations", size, 128) + return + } + { + offset = 4 * len(b.Attestations) + for ii := 0; ii < len(b.Attestations); ii++ { + dst = ssz.WriteOffset(dst, offset) + offset += b.Attestations[ii].SizeSSZ() + } + } + for ii := 0; ii < len(b.Attestations); ii++ { + if dst, err = b.Attestations[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + // Field (6) 'Deposits' + if size := len(b.Deposits); size > 16 { + err = ssz.ErrListTooBigFn("--.Deposits", size, 16) + return + } + for ii := 0; ii < len(b.Deposits); ii++ { + if dst, err = b.Deposits[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + // Field (7) 'VoluntaryExits' + if size := len(b.VoluntaryExits); size > 16 { + err = ssz.ErrListTooBigFn("--.VoluntaryExits", size, 16) + return + } + for ii := 0; ii < len(b.VoluntaryExits); ii++ { + if dst, err = b.VoluntaryExits[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + // Field (9) 'ExecutionPayload' + if dst, err = b.ExecutionPayload.MarshalSSZTo(dst); err != nil { + return + } + + // Field (10) 'BlsToExecutionChanges' + if size := len(b.BlsToExecutionChanges); size > 16 { + err = ssz.ErrListTooBigFn("--.BlsToExecutionChanges", size, 16) + return + } + for ii := 0; ii < len(b.BlsToExecutionChanges); ii++ { + if dst, err = b.BlsToExecutionChanges[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + return +} + +// UnmarshalSSZ ssz unmarshals the BeaconBlockBodyCapella object +func (b *BeaconBlockBodyCapella) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 388 { + return ssz.ErrSize + } + + tail := buf + var o3, o4, o5, o6, o7, o9, o10 uint64 + + // Field (0) 'RandaoReveal' + if cap(b.RandaoReveal) == 0 { + b.RandaoReveal = make([]byte, 0, len(buf[0:96])) + } + b.RandaoReveal = append(b.RandaoReveal, buf[0:96]...) + + // Field (1) 'Eth1Data' + if b.Eth1Data == nil { + b.Eth1Data = new(Eth1Data) + } + if err = b.Eth1Data.UnmarshalSSZ(buf[96:168]); err != nil { + return err + } + + // Field (2) 'Graffiti' + if cap(b.Graffiti) == 0 { + b.Graffiti = make([]byte, 0, len(buf[168:200])) + } + b.Graffiti = append(b.Graffiti, buf[168:200]...) + + // Offset (3) 'ProposerSlashings' + if o3 = ssz.ReadOffset(buf[200:204]); o3 > size { + return ssz.ErrOffset + } + + if o3 < 388 { + return ssz.ErrInvalidVariableOffset + } + + // Offset (4) 'AttesterSlashings' + if o4 = ssz.ReadOffset(buf[204:208]); o4 > size || o3 > o4 { + return ssz.ErrOffset + } + + // Offset (5) 'Attestations' + if o5 = ssz.ReadOffset(buf[208:212]); o5 > size || o4 > o5 { + return ssz.ErrOffset + } + + // Offset (6) 'Deposits' + if o6 = ssz.ReadOffset(buf[212:216]); o6 > size || o5 > o6 { + return ssz.ErrOffset + } + + // Offset (7) 'VoluntaryExits' + if o7 = ssz.ReadOffset(buf[216:220]); o7 > size || o6 > o7 { + return ssz.ErrOffset + } + + // Field (8) 'SyncAggregate' + if b.SyncAggregate == nil { + b.SyncAggregate = new(SyncAggregate) + } + if err = b.SyncAggregate.UnmarshalSSZ(buf[220:380]); err != nil { + return err + } + + // Offset (9) 'ExecutionPayload' + if o9 = ssz.ReadOffset(buf[380:384]); o9 > size || o7 > o9 { + return ssz.ErrOffset + } + + // Offset (10) 'BlsToExecutionChanges' + if o10 = ssz.ReadOffset(buf[384:388]); o10 > size || o9 > o10 { + return ssz.ErrOffset + } + + // Field (3) 'ProposerSlashings' + { + buf = tail[o3:o4] + num, err := ssz.DivideInt2(len(buf), 416, 16) + if err != nil { + return err + } + b.ProposerSlashings = make([]*ProposerSlashing, num) + for ii := 0; ii < num; ii++ { + if b.ProposerSlashings[ii] == nil { + b.ProposerSlashings[ii] = new(ProposerSlashing) + } + if err = b.ProposerSlashings[ii].UnmarshalSSZ(buf[ii*416 : (ii+1)*416]); err != nil { + return err + } + } + } + + // Field (4) 'AttesterSlashings' + { + buf = tail[o4:o5] + num, err := ssz.DecodeDynamicLength(buf, 2) + if err != nil { + return err + } + b.AttesterSlashings = make([]*AttesterSlashing, num) + err = ssz.UnmarshalDynamic(buf, num, func(indx int, buf []byte) (err error) { + if b.AttesterSlashings[indx] == nil { + b.AttesterSlashings[indx] = new(AttesterSlashing) + } + if err = b.AttesterSlashings[indx].UnmarshalSSZ(buf); err != nil { + return err + } + return nil + }) + if err != nil { + return err + } + } + + // Field (5) 'Attestations' + { + buf = tail[o5:o6] + num, err := ssz.DecodeDynamicLength(buf, 128) + if err != nil { + return err + } + b.Attestations = make([]*Attestation, num) + err = ssz.UnmarshalDynamic(buf, num, func(indx int, buf []byte) (err error) { + if b.Attestations[indx] == nil { + b.Attestations[indx] = new(Attestation) + } + if err = b.Attestations[indx].UnmarshalSSZ(buf); err != nil { + return err + } + return nil + }) + if err != nil { + return err + } + } + + // Field (6) 'Deposits' + { + buf = tail[o6:o7] + num, err := ssz.DivideInt2(len(buf), 1240, 16) + if err != nil { + return err + } + b.Deposits = make([]*Deposit, num) + for ii := 0; ii < num; ii++ { + if b.Deposits[ii] == nil { + b.Deposits[ii] = new(Deposit) + } + if err = b.Deposits[ii].UnmarshalSSZ(buf[ii*1240 : (ii+1)*1240]); err != nil { + return err + } + } + } + + // Field (7) 'VoluntaryExits' + { + buf = tail[o7:o9] + num, err := ssz.DivideInt2(len(buf), 112, 16) + if err != nil { + return err + } + b.VoluntaryExits = make([]*SignedVoluntaryExit, num) + for ii := 0; ii < num; ii++ { + if b.VoluntaryExits[ii] == nil { + b.VoluntaryExits[ii] = new(SignedVoluntaryExit) + } + if err = b.VoluntaryExits[ii].UnmarshalSSZ(buf[ii*112 : (ii+1)*112]); err != nil { + return err + } + } + } + + // Field (9) 'ExecutionPayload' + { + buf = tail[o9:o10] + if b.ExecutionPayload == nil { + b.ExecutionPayload = new(v1.ExecutionPayloadCapella) + } + if err = b.ExecutionPayload.UnmarshalSSZ(buf); err != nil { + return err + } + } + + // Field (10) 'BlsToExecutionChanges' + { + buf = tail[o10:] + num, err := ssz.DivideInt2(len(buf), 172, 16) + if err != nil { + return err + } + b.BlsToExecutionChanges = make([]*SignedBLSToExecutionChange, num) + for ii := 0; ii < num; ii++ { + if b.BlsToExecutionChanges[ii] == nil { + b.BlsToExecutionChanges[ii] = new(SignedBLSToExecutionChange) + } + if err = b.BlsToExecutionChanges[ii].UnmarshalSSZ(buf[ii*172 : (ii+1)*172]); err != nil { + return err + } + } + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the BeaconBlockBodyCapella object +func (b *BeaconBlockBodyCapella) SizeSSZ() (size int) { + size = 388 + + // Field (3) 'ProposerSlashings' + size += len(b.ProposerSlashings) * 416 + + // Field (4) 'AttesterSlashings' + for ii := 0; ii < len(b.AttesterSlashings); ii++ { + size += 4 + size += b.AttesterSlashings[ii].SizeSSZ() + } + + // Field (5) 'Attestations' + for ii := 0; ii < len(b.Attestations); ii++ { + size += 4 + size += b.Attestations[ii].SizeSSZ() + } + + // Field (6) 'Deposits' + size += len(b.Deposits) * 1240 + + // Field (7) 'VoluntaryExits' + size += len(b.VoluntaryExits) * 112 + + // Field (9) 'ExecutionPayload' + if b.ExecutionPayload == nil { + b.ExecutionPayload = new(v1.ExecutionPayloadCapella) + } + size += b.ExecutionPayload.SizeSSZ() + + // Field (10) 'BlsToExecutionChanges' + size += len(b.BlsToExecutionChanges) * 172 + + return +} + +// HashTreeRoot ssz hashes the BeaconBlockBodyCapella object +func (b *BeaconBlockBodyCapella) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(b) +} + +// HashTreeRootWith ssz hashes the BeaconBlockBodyCapella object with a hasher +func (b *BeaconBlockBodyCapella) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'RandaoReveal' + if size := len(b.RandaoReveal); size != 96 { + err = ssz.ErrBytesLengthFn("--.RandaoReveal", size, 96) + return + } + hh.PutBytes(b.RandaoReveal) + + // Field (1) 'Eth1Data' + if err = b.Eth1Data.HashTreeRootWith(hh); err != nil { + return + } + + // Field (2) 'Graffiti' + if size := len(b.Graffiti); size != 32 { + err = ssz.ErrBytesLengthFn("--.Graffiti", size, 32) + return + } + hh.PutBytes(b.Graffiti) + + // Field (3) 'ProposerSlashings' + { + subIndx := hh.Index() + num := uint64(len(b.ProposerSlashings)) + if num > 16 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.ProposerSlashings { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 16) + } else { + hh.MerkleizeWithMixin(subIndx, num, 16) + } + } + + // Field (4) 'AttesterSlashings' + { + subIndx := hh.Index() + num := uint64(len(b.AttesterSlashings)) + if num > 2 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.AttesterSlashings { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 2) + } else { + hh.MerkleizeWithMixin(subIndx, num, 2) + } + } + + // Field (5) 'Attestations' + { + subIndx := hh.Index() + num := uint64(len(b.Attestations)) + if num > 128 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.Attestations { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 128) + } else { + hh.MerkleizeWithMixin(subIndx, num, 128) + } + } + + // Field (6) 'Deposits' + { + subIndx := hh.Index() + num := uint64(len(b.Deposits)) + if num > 16 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.Deposits { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 16) + } else { + hh.MerkleizeWithMixin(subIndx, num, 16) + } + } + + // Field (7) 'VoluntaryExits' + { + subIndx := hh.Index() + num := uint64(len(b.VoluntaryExits)) + if num > 16 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.VoluntaryExits { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 16) + } else { + hh.MerkleizeWithMixin(subIndx, num, 16) + } + } + + // Field (8) 'SyncAggregate' + if err = b.SyncAggregate.HashTreeRootWith(hh); err != nil { + return + } + + // Field (9) 'ExecutionPayload' + if err = b.ExecutionPayload.HashTreeRootWith(hh); err != nil { + return + } + + // Field (10) 'BlsToExecutionChanges' + { + subIndx := hh.Index() + num := uint64(len(b.BlsToExecutionChanges)) + if num > 16 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.BlsToExecutionChanges { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 16) + } else { + hh.MerkleizeWithMixin(subIndx, num, 16) + } + } + + if ssz.EnableVectorizedHTR { + hh.MerkleizeVectorizedHTR(indx) + } else { + hh.Merkleize(indx) + } + return +} + +// MarshalSSZ ssz marshals the SignedBlindedBeaconBlockCapella object +func (s *SignedBlindedBeaconBlockCapella) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(s) +} + +// MarshalSSZTo ssz marshals the SignedBlindedBeaconBlockCapella object to a target array +func (s *SignedBlindedBeaconBlockCapella) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(100) + + // Offset (0) 'Block' + dst = ssz.WriteOffset(dst, offset) + if s.Block == nil { + s.Block = new(BlindedBeaconBlockCapella) + } + offset += s.Block.SizeSSZ() + + // Field (1) 'Signature' + if size := len(s.Signature); size != 96 { + err = ssz.ErrBytesLengthFn("--.Signature", size, 96) + return + } + dst = append(dst, s.Signature...) + + // Field (0) 'Block' + if dst, err = s.Block.MarshalSSZTo(dst); err != nil { + return + } + + return +} + +// UnmarshalSSZ ssz unmarshals the SignedBlindedBeaconBlockCapella object +func (s *SignedBlindedBeaconBlockCapella) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 100 { + return ssz.ErrSize + } + + tail := buf + var o0 uint64 + + // Offset (0) 'Block' + if o0 = ssz.ReadOffset(buf[0:4]); o0 > size { + return ssz.ErrOffset + } + + if o0 < 100 { + return ssz.ErrInvalidVariableOffset + } + + // Field (1) 'Signature' + if cap(s.Signature) == 0 { + s.Signature = make([]byte, 0, len(buf[4:100])) + } + s.Signature = append(s.Signature, buf[4:100]...) + + // Field (0) 'Block' + { + buf = tail[o0:] + if s.Block == nil { + s.Block = new(BlindedBeaconBlockCapella) + } + if err = s.Block.UnmarshalSSZ(buf); err != nil { + return err + } + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the SignedBlindedBeaconBlockCapella object +func (s *SignedBlindedBeaconBlockCapella) SizeSSZ() (size int) { + size = 100 + + // Field (0) 'Block' + if s.Block == nil { + s.Block = new(BlindedBeaconBlockCapella) + } + size += s.Block.SizeSSZ() + + return +} + +// HashTreeRoot ssz hashes the SignedBlindedBeaconBlockCapella object +func (s *SignedBlindedBeaconBlockCapella) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(s) +} + +// HashTreeRootWith ssz hashes the SignedBlindedBeaconBlockCapella object with a hasher +func (s *SignedBlindedBeaconBlockCapella) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'Block' + if err = s.Block.HashTreeRootWith(hh); err != nil { + return + } + + // Field (1) 'Signature' + if size := len(s.Signature); size != 96 { + err = ssz.ErrBytesLengthFn("--.Signature", size, 96) + return + } + hh.PutBytes(s.Signature) + + if ssz.EnableVectorizedHTR { + hh.MerkleizeVectorizedHTR(indx) + } else { + hh.Merkleize(indx) + } + return +} + +// MarshalSSZ ssz marshals the BlindedBeaconBlockCapella object +func (b *BlindedBeaconBlockCapella) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(b) +} + +// MarshalSSZTo ssz marshals the BlindedBeaconBlockCapella object to a target array +func (b *BlindedBeaconBlockCapella) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(84) + + // Field (0) 'Slot' + dst = ssz.MarshalUint64(dst, uint64(b.Slot)) + + // Field (1) 'ProposerIndex' + dst = ssz.MarshalUint64(dst, uint64(b.ProposerIndex)) + + // Field (2) 'ParentRoot' + if size := len(b.ParentRoot); size != 32 { + err = ssz.ErrBytesLengthFn("--.ParentRoot", size, 32) + return + } + dst = append(dst, b.ParentRoot...) + + // Field (3) 'StateRoot' + if size := len(b.StateRoot); size != 32 { + err = ssz.ErrBytesLengthFn("--.StateRoot", size, 32) + return + } + dst = append(dst, b.StateRoot...) + + // Offset (4) 'Body' + dst = ssz.WriteOffset(dst, offset) + if b.Body == nil { + b.Body = new(BlindedBeaconBlockBodyCapella) + } + offset += b.Body.SizeSSZ() + + // Field (4) 'Body' + if dst, err = b.Body.MarshalSSZTo(dst); err != nil { + return + } + + return +} + +// UnmarshalSSZ ssz unmarshals the BlindedBeaconBlockCapella object +func (b *BlindedBeaconBlockCapella) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 84 { + return ssz.ErrSize + } + + tail := buf + var o4 uint64 + + // Field (0) 'Slot' + b.Slot = github_com_prysmaticlabs_prysm_v3_consensus_types_primitives.Slot(ssz.UnmarshallUint64(buf[0:8])) + + // Field (1) 'ProposerIndex' + b.ProposerIndex = github_com_prysmaticlabs_prysm_v3_consensus_types_primitives.ValidatorIndex(ssz.UnmarshallUint64(buf[8:16])) + + // Field (2) 'ParentRoot' + if cap(b.ParentRoot) == 0 { + b.ParentRoot = make([]byte, 0, len(buf[16:48])) + } + b.ParentRoot = append(b.ParentRoot, buf[16:48]...) + + // Field (3) 'StateRoot' + if cap(b.StateRoot) == 0 { + b.StateRoot = make([]byte, 0, len(buf[48:80])) + } + b.StateRoot = append(b.StateRoot, buf[48:80]...) + + // Offset (4) 'Body' + if o4 = ssz.ReadOffset(buf[80:84]); o4 > size { + return ssz.ErrOffset + } + + if o4 < 84 { + return ssz.ErrInvalidVariableOffset + } + + // Field (4) 'Body' + { + buf = tail[o4:] + if b.Body == nil { + b.Body = new(BlindedBeaconBlockBodyCapella) + } + if err = b.Body.UnmarshalSSZ(buf); err != nil { + return err + } + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the BlindedBeaconBlockCapella object +func (b *BlindedBeaconBlockCapella) SizeSSZ() (size int) { + size = 84 + + // Field (4) 'Body' + if b.Body == nil { + b.Body = new(BlindedBeaconBlockBodyCapella) + } + size += b.Body.SizeSSZ() + + return +} + +// HashTreeRoot ssz hashes the BlindedBeaconBlockCapella object +func (b *BlindedBeaconBlockCapella) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(b) +} + +// HashTreeRootWith ssz hashes the BlindedBeaconBlockCapella object with a hasher +func (b *BlindedBeaconBlockCapella) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'Slot' + hh.PutUint64(uint64(b.Slot)) + + // Field (1) 'ProposerIndex' + hh.PutUint64(uint64(b.ProposerIndex)) + + // Field (2) 'ParentRoot' + if size := len(b.ParentRoot); size != 32 { + err = ssz.ErrBytesLengthFn("--.ParentRoot", size, 32) + return + } + hh.PutBytes(b.ParentRoot) + + // Field (3) 'StateRoot' + if size := len(b.StateRoot); size != 32 { + err = ssz.ErrBytesLengthFn("--.StateRoot", size, 32) + return + } + hh.PutBytes(b.StateRoot) + + // Field (4) 'Body' + if err = b.Body.HashTreeRootWith(hh); err != nil { + return + } + + if ssz.EnableVectorizedHTR { + hh.MerkleizeVectorizedHTR(indx) + } else { + hh.Merkleize(indx) + } + return +} + +// MarshalSSZ ssz marshals the BlindedBeaconBlockBodyCapella object +func (b *BlindedBeaconBlockBodyCapella) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(b) +} + +// MarshalSSZTo ssz marshals the BlindedBeaconBlockBodyCapella object to a target array +func (b *BlindedBeaconBlockBodyCapella) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(388) + + // Field (0) 'RandaoReveal' + if size := len(b.RandaoReveal); size != 96 { + err = ssz.ErrBytesLengthFn("--.RandaoReveal", size, 96) + return + } + dst = append(dst, b.RandaoReveal...) + + // Field (1) 'Eth1Data' + if b.Eth1Data == nil { + b.Eth1Data = new(Eth1Data) + } + if dst, err = b.Eth1Data.MarshalSSZTo(dst); err != nil { + return + } + + // Field (2) 'Graffiti' + if size := len(b.Graffiti); size != 32 { + err = ssz.ErrBytesLengthFn("--.Graffiti", size, 32) + return + } + dst = append(dst, b.Graffiti...) + + // Offset (3) 'ProposerSlashings' + dst = ssz.WriteOffset(dst, offset) + offset += len(b.ProposerSlashings) * 416 + + // Offset (4) 'AttesterSlashings' + dst = ssz.WriteOffset(dst, offset) + for ii := 0; ii < len(b.AttesterSlashings); ii++ { + offset += 4 + offset += b.AttesterSlashings[ii].SizeSSZ() + } + + // Offset (5) 'Attestations' + dst = ssz.WriteOffset(dst, offset) + for ii := 0; ii < len(b.Attestations); ii++ { + offset += 4 + offset += b.Attestations[ii].SizeSSZ() + } + + // Offset (6) 'Deposits' + dst = ssz.WriteOffset(dst, offset) + offset += len(b.Deposits) * 1240 + + // Offset (7) 'VoluntaryExits' + dst = ssz.WriteOffset(dst, offset) + offset += len(b.VoluntaryExits) * 112 + + // Field (8) 'SyncAggregate' + if b.SyncAggregate == nil { + b.SyncAggregate = new(SyncAggregate) + } + if dst, err = b.SyncAggregate.MarshalSSZTo(dst); err != nil { + return + } + + // Offset (9) 'ExecutionPayloadHeader' + dst = ssz.WriteOffset(dst, offset) + if b.ExecutionPayloadHeader == nil { + b.ExecutionPayloadHeader = new(v1.ExecutionPayloadHeaderCapella) + } + offset += b.ExecutionPayloadHeader.SizeSSZ() + + // Offset (10) 'BlsToExecutionChanges' + dst = ssz.WriteOffset(dst, offset) + offset += len(b.BlsToExecutionChanges) * 172 + + // Field (3) 'ProposerSlashings' + if size := len(b.ProposerSlashings); size > 16 { + err = ssz.ErrListTooBigFn("--.ProposerSlashings", size, 16) + return + } + for ii := 0; ii < len(b.ProposerSlashings); ii++ { + if dst, err = b.ProposerSlashings[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + // Field (4) 'AttesterSlashings' + if size := len(b.AttesterSlashings); size > 2 { + err = ssz.ErrListTooBigFn("--.AttesterSlashings", size, 2) + return + } + { + offset = 4 * len(b.AttesterSlashings) + for ii := 0; ii < len(b.AttesterSlashings); ii++ { + dst = ssz.WriteOffset(dst, offset) + offset += b.AttesterSlashings[ii].SizeSSZ() + } + } + for ii := 0; ii < len(b.AttesterSlashings); ii++ { + if dst, err = b.AttesterSlashings[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + // Field (5) 'Attestations' + if size := len(b.Attestations); size > 128 { + err = ssz.ErrListTooBigFn("--.Attestations", size, 128) + return + } + { + offset = 4 * len(b.Attestations) + for ii := 0; ii < len(b.Attestations); ii++ { + dst = ssz.WriteOffset(dst, offset) + offset += b.Attestations[ii].SizeSSZ() + } + } + for ii := 0; ii < len(b.Attestations); ii++ { + if dst, err = b.Attestations[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + // Field (6) 'Deposits' + if size := len(b.Deposits); size > 16 { + err = ssz.ErrListTooBigFn("--.Deposits", size, 16) + return + } + for ii := 0; ii < len(b.Deposits); ii++ { + if dst, err = b.Deposits[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + // Field (7) 'VoluntaryExits' + if size := len(b.VoluntaryExits); size > 16 { + err = ssz.ErrListTooBigFn("--.VoluntaryExits", size, 16) + return + } + for ii := 0; ii < len(b.VoluntaryExits); ii++ { + if dst, err = b.VoluntaryExits[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + // Field (9) 'ExecutionPayloadHeader' + if dst, err = b.ExecutionPayloadHeader.MarshalSSZTo(dst); err != nil { + return + } + + // Field (10) 'BlsToExecutionChanges' + if size := len(b.BlsToExecutionChanges); size > 16 { + err = ssz.ErrListTooBigFn("--.BlsToExecutionChanges", size, 16) + return + } + for ii := 0; ii < len(b.BlsToExecutionChanges); ii++ { + if dst, err = b.BlsToExecutionChanges[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + return +} + +// UnmarshalSSZ ssz unmarshals the BlindedBeaconBlockBodyCapella object +func (b *BlindedBeaconBlockBodyCapella) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 388 { + return ssz.ErrSize + } + + tail := buf + var o3, o4, o5, o6, o7, o9, o10 uint64 + + // Field (0) 'RandaoReveal' + if cap(b.RandaoReveal) == 0 { + b.RandaoReveal = make([]byte, 0, len(buf[0:96])) + } + b.RandaoReveal = append(b.RandaoReveal, buf[0:96]...) + + // Field (1) 'Eth1Data' + if b.Eth1Data == nil { + b.Eth1Data = new(Eth1Data) + } + if err = b.Eth1Data.UnmarshalSSZ(buf[96:168]); err != nil { + return err + } + + // Field (2) 'Graffiti' + if cap(b.Graffiti) == 0 { + b.Graffiti = make([]byte, 0, len(buf[168:200])) + } + b.Graffiti = append(b.Graffiti, buf[168:200]...) + + // Offset (3) 'ProposerSlashings' + if o3 = ssz.ReadOffset(buf[200:204]); o3 > size { + return ssz.ErrOffset + } + + if o3 < 388 { + return ssz.ErrInvalidVariableOffset + } + + // Offset (4) 'AttesterSlashings' + if o4 = ssz.ReadOffset(buf[204:208]); o4 > size || o3 > o4 { + return ssz.ErrOffset + } + + // Offset (5) 'Attestations' + if o5 = ssz.ReadOffset(buf[208:212]); o5 > size || o4 > o5 { + return ssz.ErrOffset + } + + // Offset (6) 'Deposits' + if o6 = ssz.ReadOffset(buf[212:216]); o6 > size || o5 > o6 { + return ssz.ErrOffset + } + + // Offset (7) 'VoluntaryExits' + if o7 = ssz.ReadOffset(buf[216:220]); o7 > size || o6 > o7 { + return ssz.ErrOffset + } + + // Field (8) 'SyncAggregate' + if b.SyncAggregate == nil { + b.SyncAggregate = new(SyncAggregate) + } + if err = b.SyncAggregate.UnmarshalSSZ(buf[220:380]); err != nil { + return err + } + + // Offset (9) 'ExecutionPayloadHeader' + if o9 = ssz.ReadOffset(buf[380:384]); o9 > size || o7 > o9 { + return ssz.ErrOffset + } + + // Offset (10) 'BlsToExecutionChanges' + if o10 = ssz.ReadOffset(buf[384:388]); o10 > size || o9 > o10 { + return ssz.ErrOffset + } + + // Field (3) 'ProposerSlashings' + { + buf = tail[o3:o4] + num, err := ssz.DivideInt2(len(buf), 416, 16) + if err != nil { + return err + } + b.ProposerSlashings = make([]*ProposerSlashing, num) + for ii := 0; ii < num; ii++ { + if b.ProposerSlashings[ii] == nil { + b.ProposerSlashings[ii] = new(ProposerSlashing) + } + if err = b.ProposerSlashings[ii].UnmarshalSSZ(buf[ii*416 : (ii+1)*416]); err != nil { + return err + } + } + } + + // Field (4) 'AttesterSlashings' + { + buf = tail[o4:o5] + num, err := ssz.DecodeDynamicLength(buf, 2) + if err != nil { + return err + } + b.AttesterSlashings = make([]*AttesterSlashing, num) + err = ssz.UnmarshalDynamic(buf, num, func(indx int, buf []byte) (err error) { + if b.AttesterSlashings[indx] == nil { + b.AttesterSlashings[indx] = new(AttesterSlashing) + } + if err = b.AttesterSlashings[indx].UnmarshalSSZ(buf); err != nil { + return err + } + return nil + }) + if err != nil { + return err + } + } + + // Field (5) 'Attestations' + { + buf = tail[o5:o6] + num, err := ssz.DecodeDynamicLength(buf, 128) + if err != nil { + return err + } + b.Attestations = make([]*Attestation, num) + err = ssz.UnmarshalDynamic(buf, num, func(indx int, buf []byte) (err error) { + if b.Attestations[indx] == nil { + b.Attestations[indx] = new(Attestation) + } + if err = b.Attestations[indx].UnmarshalSSZ(buf); err != nil { + return err + } + return nil + }) + if err != nil { + return err + } + } + + // Field (6) 'Deposits' + { + buf = tail[o6:o7] + num, err := ssz.DivideInt2(len(buf), 1240, 16) + if err != nil { + return err + } + b.Deposits = make([]*Deposit, num) + for ii := 0; ii < num; ii++ { + if b.Deposits[ii] == nil { + b.Deposits[ii] = new(Deposit) + } + if err = b.Deposits[ii].UnmarshalSSZ(buf[ii*1240 : (ii+1)*1240]); err != nil { + return err + } + } + } + + // Field (7) 'VoluntaryExits' + { + buf = tail[o7:o9] + num, err := ssz.DivideInt2(len(buf), 112, 16) + if err != nil { + return err + } + b.VoluntaryExits = make([]*SignedVoluntaryExit, num) + for ii := 0; ii < num; ii++ { + if b.VoluntaryExits[ii] == nil { + b.VoluntaryExits[ii] = new(SignedVoluntaryExit) + } + if err = b.VoluntaryExits[ii].UnmarshalSSZ(buf[ii*112 : (ii+1)*112]); err != nil { + return err + } + } + } + + // Field (9) 'ExecutionPayloadHeader' + { + buf = tail[o9:o10] + if b.ExecutionPayloadHeader == nil { + b.ExecutionPayloadHeader = new(v1.ExecutionPayloadHeaderCapella) + } + if err = b.ExecutionPayloadHeader.UnmarshalSSZ(buf); err != nil { + return err + } + } + + // Field (10) 'BlsToExecutionChanges' + { + buf = tail[o10:] + num, err := ssz.DivideInt2(len(buf), 172, 16) + if err != nil { + return err + } + b.BlsToExecutionChanges = make([]*SignedBLSToExecutionChange, num) + for ii := 0; ii < num; ii++ { + if b.BlsToExecutionChanges[ii] == nil { + b.BlsToExecutionChanges[ii] = new(SignedBLSToExecutionChange) + } + if err = b.BlsToExecutionChanges[ii].UnmarshalSSZ(buf[ii*172 : (ii+1)*172]); err != nil { + return err + } + } + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the BlindedBeaconBlockBodyCapella object +func (b *BlindedBeaconBlockBodyCapella) SizeSSZ() (size int) { + size = 388 + + // Field (3) 'ProposerSlashings' + size += len(b.ProposerSlashings) * 416 + + // Field (4) 'AttesterSlashings' + for ii := 0; ii < len(b.AttesterSlashings); ii++ { + size += 4 + size += b.AttesterSlashings[ii].SizeSSZ() + } + + // Field (5) 'Attestations' + for ii := 0; ii < len(b.Attestations); ii++ { + size += 4 + size += b.Attestations[ii].SizeSSZ() + } + + // Field (6) 'Deposits' + size += len(b.Deposits) * 1240 + + // Field (7) 'VoluntaryExits' + size += len(b.VoluntaryExits) * 112 + + // Field (9) 'ExecutionPayloadHeader' + if b.ExecutionPayloadHeader == nil { + b.ExecutionPayloadHeader = new(v1.ExecutionPayloadHeaderCapella) + } + size += b.ExecutionPayloadHeader.SizeSSZ() + + // Field (10) 'BlsToExecutionChanges' + size += len(b.BlsToExecutionChanges) * 172 + + return +} + +// HashTreeRoot ssz hashes the BlindedBeaconBlockBodyCapella object +func (b *BlindedBeaconBlockBodyCapella) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(b) +} + +// HashTreeRootWith ssz hashes the BlindedBeaconBlockBodyCapella object with a hasher +func (b *BlindedBeaconBlockBodyCapella) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'RandaoReveal' + if size := len(b.RandaoReveal); size != 96 { + err = ssz.ErrBytesLengthFn("--.RandaoReveal", size, 96) + return + } + hh.PutBytes(b.RandaoReveal) + + // Field (1) 'Eth1Data' + if err = b.Eth1Data.HashTreeRootWith(hh); err != nil { + return + } + + // Field (2) 'Graffiti' + if size := len(b.Graffiti); size != 32 { + err = ssz.ErrBytesLengthFn("--.Graffiti", size, 32) + return + } + hh.PutBytes(b.Graffiti) + + // Field (3) 'ProposerSlashings' + { + subIndx := hh.Index() + num := uint64(len(b.ProposerSlashings)) + if num > 16 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.ProposerSlashings { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 16) + } else { + hh.MerkleizeWithMixin(subIndx, num, 16) + } + } + + // Field (4) 'AttesterSlashings' + { + subIndx := hh.Index() + num := uint64(len(b.AttesterSlashings)) + if num > 2 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.AttesterSlashings { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 2) + } else { + hh.MerkleizeWithMixin(subIndx, num, 2) + } + } + + // Field (5) 'Attestations' + { + subIndx := hh.Index() + num := uint64(len(b.Attestations)) + if num > 128 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.Attestations { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 128) + } else { + hh.MerkleizeWithMixin(subIndx, num, 128) + } + } + + // Field (6) 'Deposits' + { + subIndx := hh.Index() + num := uint64(len(b.Deposits)) + if num > 16 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.Deposits { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 16) + } else { + hh.MerkleizeWithMixin(subIndx, num, 16) + } + } + + // Field (7) 'VoluntaryExits' + { + subIndx := hh.Index() + num := uint64(len(b.VoluntaryExits)) + if num > 16 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.VoluntaryExits { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 16) + } else { + hh.MerkleizeWithMixin(subIndx, num, 16) + } + } + + // Field (8) 'SyncAggregate' + if err = b.SyncAggregate.HashTreeRootWith(hh); err != nil { + return + } + + // Field (9) 'ExecutionPayloadHeader' + if err = b.ExecutionPayloadHeader.HashTreeRootWith(hh); err != nil { + return + } + + // Field (10) 'BlsToExecutionChanges' + { + subIndx := hh.Index() + num := uint64(len(b.BlsToExecutionChanges)) + if num > 16 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range b.BlsToExecutionChanges { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + if ssz.EnableVectorizedHTR { + hh.MerkleizeWithMixinVectorizedHTR(subIndx, num, 16) + } else { + hh.MerkleizeWithMixin(subIndx, num, 16) + } + } + + if ssz.EnableVectorizedHTR { + hh.MerkleizeVectorizedHTR(indx) + } else { + hh.Merkleize(indx) + } + return +} + // MarshalSSZ ssz marshals the ValidatorRegistrationV1 object func (v *ValidatorRegistrationV1) MarshalSSZ() ([]byte, error) { return ssz.MarshalSSZ(v) diff --git a/runtime/interop/generate_genesis_state.go b/runtime/interop/generate_genesis_state.go index ce2d72e3a6..fd1a6e9593 100644 --- a/runtime/interop/generate_genesis_state.go +++ b/runtime/interop/generate_genesis_state.go @@ -68,7 +68,7 @@ func GenerateGenesisStateFromDepositData( return nil, nil, errors.Wrap(err, "could not generate genesis state") } - pbState, err := statenative.ProtobufBeaconStatePhase0(beaconState.InnerStateUnsafe()) + pbState, err := statenative.ProtobufBeaconStatePhase0(beaconState.ToProtoUnsafe()) if err != nil { return nil, nil, err } diff --git a/testing/spectest/shared/altair/epoch_processing/helpers.go b/testing/spectest/shared/altair/epoch_processing/helpers.go index 7dd6fb7fd0..7ac65e0acb 100644 --- a/testing/spectest/shared/altair/epoch_processing/helpers.go +++ b/testing/spectest/shared/altair/epoch_processing/helpers.go @@ -59,10 +59,10 @@ func RunEpochOperationTest( t.Fatalf("Failed to unmarshal: %v", err) } - pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.InnerStateUnsafe(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProtoUnsafe(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/altair/finality/finality.go b/testing/spectest/shared/altair/finality/finality.go index ddc00bbe0d..00a9991df7 100644 --- a/testing/spectest/shared/altair/finality/finality.go +++ b/testing/spectest/shared/altair/finality/finality.go @@ -73,7 +73,7 @@ func RunFinalityTest(t *testing.T, config string) { require.NoError(t, err, "Failed to decompress") postBeaconState := ðpb.BeaconStateAltair{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { t.Fatal("Post state does not match expected") diff --git a/testing/spectest/shared/altair/fork/transition.go b/testing/spectest/shared/altair/fork/transition.go index f12d40440f..b6e1bf9312 100644 --- a/testing/spectest/shared/altair/fork/transition.go +++ b/testing/spectest/shared/altair/fork/transition.go @@ -122,7 +122,7 @@ func RunForkTransitionTest(t *testing.T, config string) { postBeaconState := ðpb.BeaconStateAltair{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.ToProto()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { t.Fatal("Post state does not match expected") diff --git a/testing/spectest/shared/altair/fork/upgrade_to_altair.go b/testing/spectest/shared/altair/fork/upgrade_to_altair.go index a54335fded..24402c7664 100644 --- a/testing/spectest/shared/altair/fork/upgrade_to_altair.go +++ b/testing/spectest/shared/altair/fork/upgrade_to_altair.go @@ -39,7 +39,7 @@ func RunUpgradeToAltair(t *testing.T, config string) { require.NoError(t, err) postState, err := altair.UpgradeToAltair(context.Background(), preState) require.NoError(t, err) - postStateFromFunction, err := state_native.ProtobufBeaconStateAltair(postState.InnerStateUnsafe()) + postStateFromFunction, err := state_native.ProtobufBeaconStateAltair(postState.ToProtoUnsafe()) require.NoError(t, err) postStateFile, err := util.BazelFileBytes(path.Join(folderPath, "post.ssz_snappy")) diff --git a/testing/spectest/shared/altair/operations/block_header.go b/testing/spectest/shared/altair/operations/block_header.go index 49c6359e15..024ec16fa2 100644 --- a/testing/spectest/shared/altair/operations/block_header.go +++ b/testing/spectest/shared/altair/operations/block_header.go @@ -63,10 +63,10 @@ func RunBlockHeaderTest(t *testing.T, config string) { postBeaconState := ðpb.BeaconStateAltair{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.ToProto()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.CloneInnerState(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProto(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/altair/operations/helpers.go b/testing/spectest/shared/altair/operations/helpers.go index 5ffe15d849..c4ccdbca6b 100644 --- a/testing/spectest/shared/altair/operations/helpers.go +++ b/testing/spectest/shared/altair/operations/helpers.go @@ -69,10 +69,10 @@ func RunBlockOperationTest( if err := postBeaconState.UnmarshalSSZ(postBeaconStateSSZ); err != nil { t.Fatalf("Failed to unmarshal: %v", err) } - pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.InnerStateUnsafe(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProtoUnsafe(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/altair/sanity/block_processing.go b/testing/spectest/shared/altair/sanity/block_processing.go index 3207dfe43c..84b5558f92 100644 --- a/testing/spectest/shared/altair/sanity/block_processing.go +++ b/testing/spectest/shared/altair/sanity/block_processing.go @@ -92,10 +92,10 @@ func RunBlockProcessingTest(t *testing.T, config, folderPath string) { postBeaconState := ðpb.BeaconStateAltair{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStateAltair(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.InnerStateUnsafe(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProtoUnsafe(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/altair/sanity/slot_processing.go b/testing/spectest/shared/altair/sanity/slot_processing.go index 30968db471..363f675556 100644 --- a/testing/spectest/shared/altair/sanity/slot_processing.go +++ b/testing/spectest/shared/altair/sanity/slot_processing.go @@ -52,7 +52,7 @@ func RunSlotProcessingTests(t *testing.T, config string) { postState, err := transition.ProcessSlots(context.Background(), beaconState, beaconState.Slot().Add(slotsCount)) require.NoError(t, err) - pbState, err := state_native.ProtobufBeaconStateAltair(postState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStateAltair(postState.ToProto()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { diff, _ := messagediff.PrettyDiff(beaconState, postBeaconState) diff --git a/testing/spectest/shared/bellatrix/epoch_processing/helpers.go b/testing/spectest/shared/bellatrix/epoch_processing/helpers.go index 0708a0d80d..5d5dbfe4b4 100644 --- a/testing/spectest/shared/bellatrix/epoch_processing/helpers.go +++ b/testing/spectest/shared/bellatrix/epoch_processing/helpers.go @@ -59,10 +59,10 @@ func RunEpochOperationTest( t.Fatalf("Failed to unmarshal: %v", err) } - pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.InnerStateUnsafe(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProtoUnsafe(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/bellatrix/finality/finality.go b/testing/spectest/shared/bellatrix/finality/finality.go index ef7465aa08..78ce2e3ff3 100644 --- a/testing/spectest/shared/bellatrix/finality/finality.go +++ b/testing/spectest/shared/bellatrix/finality/finality.go @@ -73,7 +73,7 @@ func RunFinalityTest(t *testing.T, config string) { require.NoError(t, err, "Failed to decompress") postBeaconState := ðpb.BeaconStateBellatrix{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { t.Fatal("Post state does not match expected") diff --git a/testing/spectest/shared/bellatrix/fork/transition.go b/testing/spectest/shared/bellatrix/fork/transition.go index 8e50d85930..9d06dc86fd 100644 --- a/testing/spectest/shared/bellatrix/fork/transition.go +++ b/testing/spectest/shared/bellatrix/fork/transition.go @@ -117,7 +117,7 @@ func RunForkTransitionTest(t *testing.T, config string) { postBeaconState := ðpb.BeaconStateBellatrix{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.ToProto()) require.NoError(t, err) require.DeepSSZEqual(t, pbState, postBeaconState) }) diff --git a/testing/spectest/shared/bellatrix/fork/upgrade_to_bellatrix.go b/testing/spectest/shared/bellatrix/fork/upgrade_to_bellatrix.go index 050bc3c10f..d6418fbd30 100644 --- a/testing/spectest/shared/bellatrix/fork/upgrade_to_bellatrix.go +++ b/testing/spectest/shared/bellatrix/fork/upgrade_to_bellatrix.go @@ -38,7 +38,7 @@ func RunUpgradeToBellatrix(t *testing.T, config string) { require.NoError(t, err) postState, err := execution.UpgradeToBellatrix(preState) require.NoError(t, err) - postStateFromFunction, err := state_native.ProtobufBeaconStateBellatrix(postState.InnerStateUnsafe()) + postStateFromFunction, err := state_native.ProtobufBeaconStateBellatrix(postState.ToProtoUnsafe()) require.NoError(t, err) postStateFile, err := util.BazelFileBytes(path.Join(folderPath, "post.ssz_snappy")) diff --git a/testing/spectest/shared/bellatrix/operations/block_header.go b/testing/spectest/shared/bellatrix/operations/block_header.go index b508355923..8fb8c0ecd0 100644 --- a/testing/spectest/shared/bellatrix/operations/block_header.go +++ b/testing/spectest/shared/bellatrix/operations/block_header.go @@ -63,10 +63,10 @@ func RunBlockHeaderTest(t *testing.T, config string) { postBeaconState := ðpb.BeaconStateBellatrix{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.ToProto()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.CloneInnerState(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProto(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/bellatrix/operations/helpers.go b/testing/spectest/shared/bellatrix/operations/helpers.go index 40663fdcba..c72144eb24 100644 --- a/testing/spectest/shared/bellatrix/operations/helpers.go +++ b/testing/spectest/shared/bellatrix/operations/helpers.go @@ -69,10 +69,10 @@ func RunBlockOperationTest( if err := postBeaconState.UnmarshalSSZ(postBeaconStateSSZ); err != nil { t.Fatalf("Failed to unmarshal: %v", err) } - pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.InnerStateUnsafe(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProtoUnsafe(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/bellatrix/sanity/block_processing.go b/testing/spectest/shared/bellatrix/sanity/block_processing.go index cdd01c9227..7329b06c89 100644 --- a/testing/spectest/shared/bellatrix/sanity/block_processing.go +++ b/testing/spectest/shared/bellatrix/sanity/block_processing.go @@ -92,10 +92,10 @@ func RunBlockProcessingTest(t *testing.T, config, folderPath string) { postBeaconState := ðpb.BeaconStateBellatrix{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStateBellatrix(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.InnerStateUnsafe(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProtoUnsafe(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/bellatrix/sanity/slot_processing.go b/testing/spectest/shared/bellatrix/sanity/slot_processing.go index 4328dbe218..4009dbee8d 100644 --- a/testing/spectest/shared/bellatrix/sanity/slot_processing.go +++ b/testing/spectest/shared/bellatrix/sanity/slot_processing.go @@ -52,7 +52,7 @@ func RunSlotProcessingTests(t *testing.T, config string) { postState, err := transition.ProcessSlots(context.Background(), beaconState, beaconState.Slot().Add(slotsCount)) require.NoError(t, err) - pbState, err := state_native.ProtobufBeaconStateBellatrix(postState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStateBellatrix(postState.ToProto()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { diff, _ := messagediff.PrettyDiff(beaconState, postBeaconState) diff --git a/testing/spectest/shared/phase0/epoch_processing/helpers.go b/testing/spectest/shared/phase0/epoch_processing/helpers.go index 4b7535372b..f5d352de45 100644 --- a/testing/spectest/shared/phase0/epoch_processing/helpers.go +++ b/testing/spectest/shared/phase0/epoch_processing/helpers.go @@ -58,7 +58,7 @@ func RunEpochOperationTest( t.Fatalf("Failed to unmarshal: %v", err) } - pbState, err := state_native.ProtobufBeaconStatePhase0(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStatePhase0(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { t.Fatal("Post state does not match expected") diff --git a/testing/spectest/shared/phase0/finality/runner.go b/testing/spectest/shared/phase0/finality/runner.go index d2b7760fa4..42f38d8e89 100644 --- a/testing/spectest/shared/phase0/finality/runner.go +++ b/testing/spectest/shared/phase0/finality/runner.go @@ -74,10 +74,10 @@ func RunFinalityTest(t *testing.T, config string) { require.NoError(t, err, "Failed to decompress") postBeaconState := ðpb.BeaconState{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStatePhase0(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStatePhase0(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.InnerStateUnsafe(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProtoUnsafe(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/phase0/operations/block_header.go b/testing/spectest/shared/phase0/operations/block_header.go index 655bca65a5..1c31929eaa 100644 --- a/testing/spectest/shared/phase0/operations/block_header.go +++ b/testing/spectest/shared/phase0/operations/block_header.go @@ -64,10 +64,10 @@ func RunBlockHeaderTest(t *testing.T, config string) { postBeaconState := ðpb.BeaconState{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStatePhase0(beaconState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStatePhase0(beaconState.ToProto()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.CloneInnerState(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProto(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/phase0/operations/helpers.go b/testing/spectest/shared/phase0/operations/helpers.go index 987fe8950f..0227b4eac4 100644 --- a/testing/spectest/shared/phase0/operations/helpers.go +++ b/testing/spectest/shared/phase0/operations/helpers.go @@ -69,10 +69,10 @@ func RunBlockOperationTest( if err := postBeaconState.UnmarshalSSZ(postBeaconStateSSZ); err != nil { t.Fatalf("Failed to unmarshal: %v", err) } - pbState, err := state_native.ProtobufBeaconStatePhase0(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStatePhase0(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.InnerStateUnsafe(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProtoUnsafe(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/phase0/sanity/block_processing.go b/testing/spectest/shared/phase0/sanity/block_processing.go index c8de409476..bdca588799 100644 --- a/testing/spectest/shared/phase0/sanity/block_processing.go +++ b/testing/spectest/shared/phase0/sanity/block_processing.go @@ -92,10 +92,10 @@ func RunBlockProcessingTest(t *testing.T, config, folderPath string) { postBeaconState := ðpb.BeaconState{} require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal") - pbState, err := state_native.ProtobufBeaconStatePhase0(beaconState.InnerStateUnsafe()) + pbState, err := state_native.ProtobufBeaconStatePhase0(beaconState.ToProtoUnsafe()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { - diff, _ := messagediff.PrettyDiff(beaconState.InnerStateUnsafe(), postBeaconState) + diff, _ := messagediff.PrettyDiff(beaconState.ToProtoUnsafe(), postBeaconState) t.Log(diff) t.Fatal("Post state does not match expected") } diff --git a/testing/spectest/shared/phase0/sanity/slot_processing.go b/testing/spectest/shared/phase0/sanity/slot_processing.go index 6e71567da1..61bd0c667b 100644 --- a/testing/spectest/shared/phase0/sanity/slot_processing.go +++ b/testing/spectest/shared/phase0/sanity/slot_processing.go @@ -52,7 +52,7 @@ func RunSlotProcessingTests(t *testing.T, config string) { postState, err := transition.ProcessSlots(context.Background(), beaconState, beaconState.Slot().Add(slotsCount)) require.NoError(t, err) - pbState, err := state_native.ProtobufBeaconStatePhase0(postState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStatePhase0(postState.ToProto()) require.NoError(t, err) if !proto.Equal(pbState, postBeaconState) { diff, _ := messagediff.PrettyDiff(beaconState, postBeaconState) diff --git a/testing/util/BUILD.bazel b/testing/util/BUILD.bazel index bddcb275bd..474c93aea0 100644 --- a/testing/util/BUILD.bazel +++ b/testing/util/BUILD.bazel @@ -10,6 +10,7 @@ go_library( "bellatrix.go", "bellatrix_state.go", "block.go", + "capella_state.go", "deposits.go", "helpers.go", "merge.go", diff --git a/testing/util/attestation.go b/testing/util/attestation.go index ef88c3645c..e2ee8d6e9f 100644 --- a/testing/util/attestation.go +++ b/testing/util/attestation.go @@ -69,7 +69,7 @@ func GenerateAttestations( var headState state.BeaconState switch bState.Version() { case version.Phase0: - pbState, err := state_native.ProtobufBeaconStatePhase0(bState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStatePhase0(bState.ToProto()) if err != nil { return nil, err } @@ -79,7 +79,7 @@ func GenerateAttestations( } headState = genState case version.Altair: - pbState, err := state_native.ProtobufBeaconStateAltair(bState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStateAltair(bState.ToProto()) if err != nil { return nil, err } @@ -89,7 +89,7 @@ func GenerateAttestations( } headState = genState case version.Bellatrix: - pbState, err := state_native.ProtobufBeaconStateBellatrix(bState.CloneInnerState()) + pbState, err := state_native.ProtobufBeaconStateBellatrix(bState.ToProto()) if err != nil { return nil, err } diff --git a/testing/util/bellatrix_state.go b/testing/util/bellatrix_state.go index 1a7adf0b12..dd5a35f6fe 100644 --- a/testing/util/bellatrix_state.go +++ b/testing/util/bellatrix_state.go @@ -62,8 +62,8 @@ func emptyGenesisStateBellatrix() (state.BeaconState, error) { // Misc fields. Slot: 0, Fork: ðpb.Fork{ - PreviousVersion: params.BeaconConfig().GenesisForkVersion, - CurrentVersion: params.BeaconConfig().AltairForkVersion, + PreviousVersion: params.BeaconConfig().AltairForkVersion, + CurrentVersion: params.BeaconConfig().BellatrixForkVersion, Epoch: 0, }, // Validator registry fields. diff --git a/testing/util/capella_state.go b/testing/util/capella_state.go new file mode 100644 index 0000000000..22c89fd74a --- /dev/null +++ b/testing/util/capella_state.go @@ -0,0 +1,245 @@ +package util + +import ( + "context" + "testing" + + "github.com/pkg/errors" + "github.com/prysmaticlabs/prysm/v3/beacon-chain/core/helpers" + "github.com/prysmaticlabs/prysm/v3/beacon-chain/state" + state_native "github.com/prysmaticlabs/prysm/v3/beacon-chain/state/state-native" + "github.com/prysmaticlabs/prysm/v3/beacon-chain/state/stateutil" + fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams" + "github.com/prysmaticlabs/prysm/v3/config/params" + "github.com/prysmaticlabs/prysm/v3/crypto/bls" + "github.com/prysmaticlabs/prysm/v3/encoding/bytesutil" + enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1" + ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" +) + +// DeterministicGenesisStateCapella returns a genesis state in Capella format made using the deterministic deposits. +func DeterministicGenesisStateCapella(t testing.TB, numValidators uint64) (state.BeaconState, []bls.SecretKey) { + deposits, privKeys, err := DeterministicDepositsAndKeys(numValidators) + if err != nil { + t.Fatal(errors.Wrapf(err, "failed to get %d deposits", numValidators)) + } + eth1Data, err := DeterministicEth1Data(len(deposits)) + if err != nil { + t.Fatal(errors.Wrapf(err, "failed to get eth1data for %d deposits", numValidators)) + } + beaconState, err := genesisBeaconStateCapella(context.Background(), deposits, uint64(0), eth1Data) + if err != nil { + t.Fatal(errors.Wrapf(err, "failed to get genesis beacon state of %d validators", numValidators)) + } + resetCache() + return beaconState, privKeys +} + +// genesisBeaconStateCapella returns the genesis beacon state. +func genesisBeaconStateCapella(ctx context.Context, deposits []*ethpb.Deposit, genesisTime uint64, eth1Data *ethpb.Eth1Data) (state.BeaconState, error) { + st, err := emptyGenesisStateCapella() + if err != nil { + return nil, err + } + + // Process initial deposits. + st, err = helpers.UpdateGenesisEth1Data(st, deposits, eth1Data) + if err != nil { + return nil, err + } + + st, err = processPreGenesisDeposits(ctx, st, deposits) + if err != nil { + return nil, errors.Wrap(err, "could not process validator deposits") + } + + return buildGenesisBeaconStateCapella(genesisTime, st, st.Eth1Data()) +} + +// emptyGenesisStateCapella returns an empty genesis state in Capella format. +func emptyGenesisStateCapella() (state.BeaconState, error) { + st := ðpb.BeaconStateCapella{ + // Misc fields. + Slot: 0, + Fork: ðpb.Fork{ + PreviousVersion: params.BeaconConfig().BellatrixForkVersion, + CurrentVersion: params.BeaconConfig().CapellaForkVersion, + Epoch: 0, + }, + // Validator registry fields. + Validators: []*ethpb.Validator{}, + Balances: []uint64{}, + InactivityScores: []uint64{}, + + JustificationBits: []byte{0}, + HistoricalRoots: [][]byte{}, + CurrentEpochParticipation: []byte{}, + PreviousEpochParticipation: []byte{}, + + // Eth1 data. + Eth1Data: ðpb.Eth1Data{}, + Eth1DataVotes: []*ethpb.Eth1Data{}, + Eth1DepositIndex: 0, + + LatestExecutionPayloadHeader: &enginev1.ExecutionPayloadHeaderCapella{}, + } + return state_native.InitializeFromProtoCapella(st) +} + +func buildGenesisBeaconStateCapella(genesisTime uint64, preState state.BeaconState, eth1Data *ethpb.Eth1Data) (state.BeaconState, error) { + if eth1Data == nil { + return nil, errors.New("no eth1data provided for genesis state") + } + + randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector) + for i := 0; i < len(randaoMixes); i++ { + h := make([]byte, 32) + copy(h, eth1Data.BlockHash) + randaoMixes[i] = h + } + + zeroHash := params.BeaconConfig().ZeroHash[:] + + activeIndexRoots := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector) + for i := 0; i < len(activeIndexRoots); i++ { + activeIndexRoots[i] = zeroHash + } + + blockRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot) + for i := 0; i < len(blockRoots); i++ { + blockRoots[i] = zeroHash + } + + stateRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot) + for i := 0; i < len(stateRoots); i++ { + stateRoots[i] = zeroHash + } + + slashings := make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector) + + genesisValidatorsRoot, err := stateutil.ValidatorRegistryRoot(preState.Validators()) + if err != nil { + return nil, errors.Wrapf(err, "could not hash tree root genesis validators %v", err) + } + + prevEpochParticipation, err := preState.PreviousEpochParticipation() + if err != nil { + return nil, err + } + currEpochParticipation, err := preState.CurrentEpochParticipation() + if err != nil { + return nil, err + } + scores, err := preState.InactivityScores() + if err != nil { + return nil, err + } + st := ðpb.BeaconStateCapella{ + // Misc fields. + Slot: 0, + GenesisTime: genesisTime, + GenesisValidatorsRoot: genesisValidatorsRoot[:], + + Fork: ðpb.Fork{ + PreviousVersion: params.BeaconConfig().GenesisForkVersion, + CurrentVersion: params.BeaconConfig().GenesisForkVersion, + Epoch: 0, + }, + + // Validator registry fields. + Validators: preState.Validators(), + Balances: preState.Balances(), + PreviousEpochParticipation: prevEpochParticipation, + CurrentEpochParticipation: currEpochParticipation, + InactivityScores: scores, + + // Randomness and committees. + RandaoMixes: randaoMixes, + + // Finality. + PreviousJustifiedCheckpoint: ðpb.Checkpoint{ + Epoch: 0, + Root: params.BeaconConfig().ZeroHash[:], + }, + CurrentJustifiedCheckpoint: ðpb.Checkpoint{ + Epoch: 0, + Root: params.BeaconConfig().ZeroHash[:], + }, + JustificationBits: []byte{0}, + FinalizedCheckpoint: ðpb.Checkpoint{ + Epoch: 0, + Root: params.BeaconConfig().ZeroHash[:], + }, + + HistoricalRoots: [][]byte{}, + BlockRoots: blockRoots, + StateRoots: stateRoots, + Slashings: slashings, + + // Eth1 data. + Eth1Data: eth1Data, + Eth1DataVotes: []*ethpb.Eth1Data{}, + Eth1DepositIndex: preState.Eth1DepositIndex(), + } + + var scBits [fieldparams.SyncAggregateSyncCommitteeBytesLength]byte + bodyRoot, err := (ðpb.BeaconBlockBodyCapella{ + RandaoReveal: make([]byte, 96), + Eth1Data: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + BlockHash: make([]byte, 32), + }, + Graffiti: make([]byte, 32), + SyncAggregate: ðpb.SyncAggregate{ + SyncCommitteeBits: scBits[:], + SyncCommitteeSignature: make([]byte, 96), + }, + ExecutionPayload: &enginev1.ExecutionPayloadCapella{ + ParentHash: make([]byte, 32), + FeeRecipient: make([]byte, 20), + StateRoot: make([]byte, 32), + ReceiptsRoot: make([]byte, 32), + LogsBloom: make([]byte, 256), + PrevRandao: make([]byte, 32), + BaseFeePerGas: make([]byte, 32), + BlockHash: make([]byte, 32), + }, + }).HashTreeRoot() + if err != nil { + return nil, errors.Wrap(err, "could not hash tree root empty block body") + } + + st.LatestBlockHeader = ðpb.BeaconBlockHeader{ + ParentRoot: zeroHash, + StateRoot: zeroHash, + BodyRoot: bodyRoot[:], + } + + var pubKeys [][]byte + for i := uint64(0); i < params.BeaconConfig().SyncCommitteeSize; i++ { + pubKeys = append(pubKeys, bytesutil.PadTo([]byte{}, params.BeaconConfig().BLSPubkeyLength)) + } + st.CurrentSyncCommittee = ðpb.SyncCommittee{ + Pubkeys: pubKeys, + AggregatePubkey: bytesutil.PadTo([]byte{}, params.BeaconConfig().BLSPubkeyLength), + } + st.NextSyncCommittee = ðpb.SyncCommittee{ + Pubkeys: bytesutil.SafeCopy2dBytes(pubKeys), + AggregatePubkey: bytesutil.PadTo([]byte{}, params.BeaconConfig().BLSPubkeyLength), + } + + st.LatestExecutionPayloadHeader = &enginev1.ExecutionPayloadHeaderCapella{ + ParentHash: make([]byte, 32), + FeeRecipient: make([]byte, 20), + StateRoot: make([]byte, 32), + ReceiptsRoot: make([]byte, 32), + LogsBloom: make([]byte, 256), + PrevRandao: make([]byte, 32), + BaseFeePerGas: make([]byte, 32), + BlockHash: make([]byte, 32), + TransactionsRoot: make([]byte, 32), + WithdrawalsRoot: make([]byte, 32), + } + + return state_native.InitializeFromProtoCapella(st) +} diff --git a/testing/util/state_test.go b/testing/util/state_test.go index 066991f0fe..97a891eae6 100644 --- a/testing/util/state_test.go +++ b/testing/util/state_test.go @@ -16,7 +16,7 @@ func TestNewBeaconState(t *testing.T) { require.NoError(t, err) got := ðpb.BeaconState{} require.NoError(t, got.UnmarshalSSZ(b)) - if !reflect.DeepEqual(st.InnerStateUnsafe(), got) { + if !reflect.DeepEqual(st.ToProtoUnsafe(), got) { t.Fatal("State did not match after round trip marshal") } } @@ -28,7 +28,7 @@ func TestNewBeaconStateAltair(t *testing.T) { require.NoError(t, err) got := ðpb.BeaconStateAltair{} require.NoError(t, got.UnmarshalSSZ(b)) - if !reflect.DeepEqual(st.InnerStateUnsafe(), got) { + if !reflect.DeepEqual(st.ToProtoUnsafe(), got) { t.Fatal("State did not match after round trip marshal") } } @@ -40,7 +40,7 @@ func TestNewBeaconStateBellatrix(t *testing.T) { require.NoError(t, err) got := ðpb.BeaconStateBellatrix{} require.NoError(t, got.UnmarshalSSZ(b)) - if !reflect.DeepEqual(st.InnerStateUnsafe(), got) { + if !reflect.DeepEqual(st.ToProtoUnsafe(), got) { t.Fatal("State did not match after round trip marshal") } } @@ -52,7 +52,7 @@ func TestNewBeaconStateCapella(t *testing.T) { require.NoError(t, err) got := ðpb.BeaconStateCapella{} require.NoError(t, got.UnmarshalSSZ(b)) - if !reflect.DeepEqual(st.InnerStateUnsafe(), got) { + if !reflect.DeepEqual(st.ToProtoUnsafe(), got) { t.Fatal("State did not match after round trip marshal") } } diff --git a/tools/pcli/main.go b/tools/pcli/main.go index 00928aad8e..bb5abe1ca9 100644 --- a/tools/pcli/main.go +++ b/tools/pcli/main.go @@ -197,8 +197,8 @@ func main() { if err := dataFetcher(expectedPostStatePath, expectedState); err != nil { log.Fatal(err) } - if !equality.DeepEqual(expectedState, postState.InnerStateUnsafe()) { - diff, _ := messagediff.PrettyDiff(expectedState, postState.InnerStateUnsafe()) + if !equality.DeepEqual(expectedState, postState.ToProtoUnsafe()) { + diff, _ := messagediff.PrettyDiff(expectedState, postState.ToProtoUnsafe()) log.Errorf("Derived state differs from provided post state: %s", diff) } }