From 78a25f99c3d04a19e18a2cb93701c6cf90ab46ac Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Sun, 23 Aug 2020 18:46:17 -0700 Subject: [PATCH] Update fastssz (#6760) * Update fastssz * Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz * fmt * gaz * Merge refs/heads/master into update-fssz * goimports * Merge refs/heads/master into update-fssz * Merge refs/heads/master into update-fssz * Merge refs/heads/master into update-fssz * Merge refs/heads/master into update-fssz * Merge refs/heads/master into update-fssz * Fix * fix ethereumapis * fix again * kafka * fix gen file * fix compute signing root * gofmt * checkpoint progress * progress * checkpoint * progress * Fix build * checkpoint * helpers * Another test fixed * gaz * another test fix * gofmt * some fixes * Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz * fix one test * Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz * fill empty checkpoint roots * more padding * more padding * Fix //beacon-chain/rpc/debug:go_default_test * fix //beacon-chain/core/state:go_default_test * fix //beacon-chain/core/state:go_default_test * fix some htr errors * fix //slasher/rpc:go_default_test * Progress on //beacon-chain/core/blocks:go_default_test * Progress on //beacon-chain/core/blocks:go_default_test * Progress on //beacon-chain/core/blocks:go_default_test * fix //slasher/db/kv:go_default_test * progress * fix //beacon-chain/sync/initial-sync:go_raceon_test * gofmt and gaz * fix one more test, taking a break * Fix //beacon-chain/core/blocks:go_default_test * Complete beacon-chain/powchain * Do most of beacon-chain/rpc/beacon/ * Do most of beacon-chain/blockchain * fix //beacon-chain/operations/attestations/kv:go_default_test * Fix //beacon-chain/cache/depositcache:go_default_test * Fix //slasher/detection:go_default_test * Progress * fix //beacon-chain/rpc/validator:go_default_test * gofmt * fix //validator/client:go_default_test * fix * fix //beacon-chain/blockchain:go_raceoff_test * fix //beacon-chain/rpc/beacon:go_default_test * fix 1 of 4 shards in //beacon-chain/sync:go_default_test * Fix //beacon-chain/sync:go_default_test and gofmt * prevent panic * fix //beacon-chain/state/stategen:go_default_test * fix * Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz * fix most tests * Self review, go mod tidy, run regen scripts * fix slasher * Update ethereumapis * disable spawn strategy override * Merge refs/heads/master into update-fssz * Merge refs/heads/master into update-fssz * Remove extra line in imports * Remove extra line in imports * Gofmt * PR feedback from @nisdas --- .buildkite-bazelrc | 8 +- beacon-chain/blockchain/chain_info_test.go | 11 +- .../blockchain/checkpoint_info_cache_test.go | 2 +- beacon-chain/blockchain/head_test.go | 15 +- .../blockchain/process_attestation_test.go | 98 +- beacon-chain/blockchain/process_block_test.go | 180 +- .../blockchain/receive_attestation_test.go | 2 +- beacon-chain/blockchain/receive_block_test.go | 4 +- beacon-chain/blockchain/service_test.go | 19 +- beacon-chain/blockchain/testing/mock.go | 6 +- beacon-chain/cache/attestation_data_test.go | 2 +- beacon-chain/cache/checkpoint_state_test.go | 2 +- beacon-chain/cache/depositcache/BUILD.bazel | 2 - .../cache/depositcache/deposits_cache.go | 3 +- .../cache/depositcache/deposits_cache_test.go | 164 +- .../depositcache/pending_deposits_test.go | 32 +- beacon-chain/core/blocks/BUILD.bazel | 2 +- beacon-chain/core/blocks/attestation_test.go | 207 ++- .../core/blocks/attester_slashing_test.go | 45 +- .../core/blocks/block_regression_test.go | 12 +- beacon-chain/core/blocks/deposit.go | 4 +- beacon-chain/core/blocks/deposit_test.go | 18 +- beacon-chain/core/blocks/eth1_data_test.go | 9 +- beacon-chain/core/blocks/genesis.go | 21 +- beacon-chain/core/blocks/header_test.go | 190 +- .../core/blocks/proposer_slashing_test.go | 21 +- beacon-chain/core/blocks/randao_test.go | 3 +- beacon-chain/core/blocks/signature.go | 3 +- beacon-chain/core/epoch/BUILD.bazel | 2 +- beacon-chain/core/epoch/epoch_processing.go | 9 +- .../core/epoch/epoch_processing_test.go | 84 +- .../justification_finalization_test.go | 6 +- .../epoch/precompute/reward_penalty_test.go | 14 +- beacon-chain/core/feed/state/events.go | 2 +- beacon-chain/core/helpers/BUILD.bazel | 2 +- beacon-chain/core/helpers/committee_test.go | 12 +- beacon-chain/core/helpers/signing_root.go | 21 +- .../core/helpers/signing_root_test.go | 53 +- beacon-chain/core/helpers/validators_test.go | 2 +- beacon-chain/core/state/BUILD.bazel | 2 +- beacon-chain/core/state/benchmarks_test.go | 3 +- beacon-chain/core/state/state.go | 3 +- beacon-chain/core/state/transition_test.go | 187 +- beacon-chain/db/kafka/BUILD.bazel | 1 + beacon-chain/db/kafka/export_wrapper.go | 9 +- beacon-chain/db/kv/BUILD.bazel | 2 - beacon-chain/db/kv/backup_test.go | 4 +- beacon-chain/db/kv/blocks_test.go | 40 +- beacon-chain/db/kv/checkpoint_test.go | 9 +- .../db/kv/finalized_block_roots_test.go | 12 +- .../db/kv/migration_archived_index_test.go | 7 +- beacon-chain/db/kv/operations.go | 3 +- beacon-chain/db/kv/operations_test.go | 3 +- beacon-chain/db/kv/slashings.go | 5 +- beacon-chain/db/kv/slashings_test.go | 5 +- beacon-chain/db/kv/state_test.go | 29 +- .../operations/attestations/kv/BUILD.bazel | 1 + .../attestations/kv/aggregated_test.go | 190 +- .../operations/attestations/kv/block_test.go | 12 +- .../attestations/kv/forkchoice_test.go | 12 +- .../attestations/kv/seen_bits_test.go | 22 +- .../attestations/kv/unaggregated_test.go | 55 +- .../attestations/prepare_forkchoice_test.go | 70 +- .../attestations/prune_expired_test.go | 72 +- beacon-chain/p2p/BUILD.bazel | 1 + beacon-chain/p2p/discovery_test.go | 3 +- beacon-chain/p2p/subnets_test.go | 4 +- beacon-chain/powchain/BUILD.bazel | 2 - beacon-chain/powchain/deposit_test.go | 13 +- beacon-chain/powchain/log_processing.go | 3 +- beacon-chain/rpc/beacon/BUILD.bazel | 1 - beacon-chain/rpc/beacon/assignments_test.go | 58 +- beacon-chain/rpc/beacon/attestations_test.go | 223 +-- beacon-chain/rpc/beacon/blocks_test.go | 15 +- beacon-chain/rpc/beacon/committees_test.go | 6 +- beacon-chain/rpc/beacon/validators_test.go | 77 +- beacon-chain/rpc/debug/block_test.go | 24 +- beacon-chain/rpc/debug/state_test.go | 6 +- beacon-chain/rpc/validator/BUILD.bazel | 2 - beacon-chain/rpc/validator/aggregator_test.go | 16 +- .../rpc/validator/assignments_test.go | 5 +- beacon-chain/rpc/validator/attester_test.go | 118 +- beacon-chain/rpc/validator/proposer.go | 5 +- beacon-chain/rpc/validator/proposer_test.go | 281 ++- beacon-chain/rpc/validator/server_test.go | 11 +- beacon-chain/rpc/validator/status_test.go | 105 +- beacon-chain/state/state_trie_test.go | 5 +- beacon-chain/state/stategen/BUILD.bazel | 1 - beacon-chain/state/stategen/cold_test.go | 5 +- beacon-chain/state/stategen/getter_test.go | 26 +- beacon-chain/state/stategen/hot_test.go | 31 +- beacon-chain/state/stategen/migrate_test.go | 10 +- beacon-chain/state/stategen/replay_test.go | 243 ++- beacon-chain/state/stateutil/BUILD.bazel | 1 - .../state/stateutil/attestations_test.go | 5 +- beacon-chain/state/stateutil/blocks_test.go | 15 +- .../state/stateutil/state_root_test.go | 7 +- beacon-chain/sync/initial-sync/BUILD.bazel | 2 +- .../sync/initial-sync/blocks_fetcher_test.go | 7 +- .../sync/initial-sync/initial_sync_test.go | 19 +- .../sync/initial-sync/round_robin_test.go | 71 +- .../sync/pending_attestations_queue_test.go | 18 +- .../sync/pending_blocks_queue_test.go | 140 +- .../sync/rpc_beacon_blocks_by_range_test.go | 21 +- .../sync/rpc_beacon_blocks_by_root_test.go | 17 +- beacon-chain/sync/rpc_status_test.go | 65 +- beacon-chain/sync/service_test.go | 10 +- .../subscriber_beacon_aggregate_proof_test.go | 34 +- .../subscriber_beacon_attestation_test.go | 5 +- .../sync/subscriber_beacon_blocks_test.go | 28 +- beacon-chain/sync/subscriber_test.go | 4 +- .../sync/validate_aggregate_proof_test.go | 54 +- .../sync/validate_attester_slashing_test.go | 12 +- .../sync/validate_beacon_attestation_test.go | 29 +- .../sync/validate_beacon_blocks_test.go | 172 +- contracts/deposit-contract/BUILD.bazel | 1 - .../deposit-contract/deposit_tree_test.go | 5 +- deps.bzl | 12 +- fuzz/BUILD.bazel | 2 + fuzz/generated.ssz.go | 289 +++- fuzz/inputs.go | 12 +- fuzz/rpc_status_fuzz.go | 5 +- fuzz/testing/beacon_fuzz_states.go | 2 +- go.mod | 4 +- go.sum | 9 +- proto/beacon/p2p/v1/BUILD.bazel | 2 + proto/beacon/p2p/v1/generated.ssz.go | 1540 ++++++++++++----- proto/testing/ssz_regression_test.go | 2 +- proto/testing/ssz_static_test.go | 9 +- .../attestationutil/attestation_utils_test.go | 8 +- shared/bls/herumi/signature.go | 3 + shared/depositutil/deposit.go | 6 +- shared/depositutil/deposit_test.go | 2 +- shared/interop/generate_genesis_state.go | 4 +- shared/testutil/block.go | 28 +- shared/testutil/deposits.go | 10 +- shared/testutil/state_test.go | 10 + slasher/db/kv/attester_slashings_test.go | 53 +- slasher/db/kv/block_header_test.go | 33 +- slasher/db/kv/indexed_attestations_test.go | 204 +-- slasher/db/kv/proposer_slashings_test.go | 204 ++- slasher/detection/detect_test.go | 108 +- slasher/rpc/server_test.go | 29 +- tools/sendDepositTx/BUILD.bazel | 1 - tools/sendDepositTx/sendDeposits_test.go | 3 +- validator/client/BUILD.bazel | 1 + validator/client/aggregate_test.go | 32 +- validator/client/attest_test.go | 42 +- validator/client/propose_test.go | 83 +- validator/client/service.go | 10 +- validator/client/validator_test.go | 6 +- validator/slashing-protection/BUILD.bazel | 1 + .../slashing-protection/external_test.go | 13 +- 153 files changed, 4314 insertions(+), 2560 deletions(-) diff --git a/.buildkite-bazelrc b/.buildkite-bazelrc index 781afcded4..5c9039c52d 100644 --- a/.buildkite-bazelrc +++ b/.buildkite-bazelrc @@ -3,10 +3,10 @@ # # This config is loaded from https://github.com/bazelbuild/bazel-toolchains/blob/master/bazelrc/latest.bazelrc build:remote-cache --remote_timeout=3600 -build:remote-cache --spawn_strategy=standalone -build:remote-cache --strategy=Javac=standalone -build:remote-cache --strategy=Closure=standalone -build:remote-cache --strategy=Genrule=standalone +#build:remote-cache --spawn_strategy=standalone +#build:remote-cache --strategy=Javac=standalone +#build:remote-cache --strategy=Closure=standalone +#build:remote-cache --strategy=Genrule=standalone # Prysm specific remote-cache properties. #build:remote-cache --disk_cache= diff --git a/beacon-chain/blockchain/chain_info_test.go b/beacon-chain/blockchain/chain_info_test.go index 19f8cb22fe..e6edd36520 100644 --- a/beacon-chain/blockchain/chain_info_test.go +++ b/beacon-chain/blockchain/chain_info_test.go @@ -11,7 +11,9 @@ import ( testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) @@ -42,7 +44,7 @@ func TestHeadRoot_Nil(t *testing.T) { func TestFinalizedCheckpt_CanRetrieve(t *testing.T) { db, sc := testDB.SetupDB(t) - cp := ðpb.Checkpoint{Epoch: 5, Root: []byte("foo")} + cp := ðpb.Checkpoint{Epoch: 5, Root: bytesutil.PadTo([]byte("foo"), 32)} c := setupBeaconChain(t, db, sc) c.finalizedCheckpt = cp @@ -66,7 +68,7 @@ func TestFinalizedCheckpt_GenesisRootOk(t *testing.T) { func TestCurrentJustifiedCheckpt_CanRetrieve(t *testing.T) { db, sc := testDB.SetupDB(t) - cp := ðpb.Checkpoint{Epoch: 6, Root: []byte("foo")} + cp := ðpb.Checkpoint{Epoch: 6, Root: bytesutil.PadTo([]byte("foo"), 32)} c := setupBeaconChain(t, db, sc) c.justifiedCheckpt = cp @@ -90,7 +92,7 @@ func TestJustifiedCheckpt_GenesisRootOk(t *testing.T) { func TestPreviousJustifiedCheckpt_CanRetrieve(t *testing.T) { db, sc := testDB.SetupDB(t) - cp := ðpb.Checkpoint{Epoch: 7, Root: []byte("foo")} + cp := ðpb.Checkpoint{Epoch: 7, Root: bytesutil.PadTo([]byte("foo"), 32)} c := setupBeaconChain(t, db, sc) c.prevJustifiedCheckpt = cp @@ -128,7 +130,8 @@ func TestHeadRoot_CanRetrieve(t *testing.T) { } func TestHeadBlock_CanRetrieve(t *testing.T) { - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} + b := testutil.NewBeaconBlock() + b.Block.Slot = 1 s, err := state.InitializeFromProto(&pb.BeaconState{}) require.NoError(t, err) c := &Service{} diff --git a/beacon-chain/blockchain/checkpoint_info_cache_test.go b/beacon-chain/blockchain/checkpoint_info_cache_test.go index 19959be004..40e07e1438 100644 --- a/beacon-chain/blockchain/checkpoint_info_cache_test.go +++ b/beacon-chain/blockchain/checkpoint_info_cache_test.go @@ -34,7 +34,7 @@ func TestHotStateCache_RoundTrip(t *testing.T) { func TestHotStateCache_CanPrune(t *testing.T) { c := newCheckPointInfoCache() for i := 0; i < maxInfoSize+1; i++ { - cp := ðpb.Checkpoint{Epoch: uint64(i)} + cp := ðpb.Checkpoint{Epoch: uint64(i), Root: make([]byte, 32)} require.NoError(t, c.put(cp, &pb.CheckPtInfo{})) } require.Equal(t, len(c.cache.Keys()), maxInfoSize) diff --git a/beacon-chain/blockchain/head_test.go b/beacon-chain/blockchain/head_test.go index 145054c3d7..042f21cdb4 100644 --- a/beacon-chain/blockchain/head_test.go +++ b/beacon-chain/blockchain/head_test.go @@ -5,7 +5,6 @@ import ( "context" "testing" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -35,8 +34,9 @@ func TestSaveHead_Different(t *testing.T) { oldRoot := [32]byte{'A'} service.head = &head{slot: 0, root: oldRoot} - newHeadBlock := ðpb.BeaconBlock{Slot: 1} - newHeadSignedBlock := ðpb.SignedBeaconBlock{Block: newHeadBlock} + newHeadSignedBlock := testutil.NewBeaconBlock() + newHeadSignedBlock.Block.Slot = 1 + newHeadBlock := newHeadSignedBlock.Block require.NoError(t, service.beaconDB.SaveBlock(context.Background(), newHeadSignedBlock)) newRoot, err := stateutil.BlockRoot(newHeadBlock) @@ -68,11 +68,10 @@ func TestSaveHead_Different_Reorg(t *testing.T) { service.head = &head{slot: 0, root: oldRoot} reorgChainParent := [32]byte{'B'} - newHeadBlock := ðpb.BeaconBlock{ - Slot: 1, - ParentRoot: reorgChainParent[:], - } - newHeadSignedBlock := ðpb.SignedBeaconBlock{Block: newHeadBlock} + newHeadSignedBlock := testutil.NewBeaconBlock() + newHeadSignedBlock.Block.Slot = 1 + newHeadSignedBlock.Block.ParentRoot = reorgChainParent[:] + newHeadBlock := newHeadSignedBlock.Block require.NoError(t, service.beaconDB.SaveBlock(context.Background(), newHeadSignedBlock)) newRoot, err := stateutil.BlockRoot(newHeadBlock) diff --git a/beacon-chain/blockchain/process_attestation_test.go b/beacon-chain/blockchain/process_attestation_test.go index 9382aec85a..61d1ebb626 100644 --- a/beacon-chain/blockchain/process_attestation_test.go +++ b/beacon-chain/blockchain/process_attestation_test.go @@ -11,7 +11,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/state" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray" - stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -37,12 +36,14 @@ func TestStore_OnAttestation(t *testing.T) { _, err = blockTree1(db, []byte{'g'}) require.NoError(t, err) - BlkWithOutState := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 0}} + BlkWithOutState := testutil.NewBeaconBlock() + BlkWithOutState.Block.Slot = 0 require.NoError(t, db.SaveBlock(ctx, BlkWithOutState)) BlkWithOutStateRoot, err := stateutil.BlockRoot(BlkWithOutState.Block) require.NoError(t, err) - BlkWithStateBadAtt := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} + BlkWithStateBadAtt := testutil.NewBeaconBlock() + BlkWithStateBadAtt.Block.Slot = 1 require.NoError(t, db.SaveBlock(ctx, BlkWithStateBadAtt)) BlkWithStateBadAttRoot, err := stateutil.BlockRoot(BlkWithStateBadAtt.Block) require.NoError(t, err) @@ -51,7 +52,8 @@ func TestStore_OnAttestation(t *testing.T) { require.NoError(t, s.SetSlot(100*params.BeaconConfig().SlotsPerEpoch)) require.NoError(t, service.beaconDB.SaveState(ctx, s, BlkWithStateBadAttRoot)) - BlkWithValidState := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2}} + BlkWithValidState := testutil.NewBeaconBlock() + BlkWithValidState.Block.Slot = 2 require.NoError(t, db.SaveBlock(ctx, BlkWithValidState)) BlkWithValidStateRoot, err := stateutil.BlockRoot(BlkWithValidState.Block) @@ -69,28 +71,24 @@ func TestStore_OnAttestation(t *testing.T) { tests := []struct { name string a *ethpb.Attestation - s *pb.BeaconState wantErr bool wantErrString string }{ { name: "attestation's data slot not aligned with target vote", - a: ðpb.Attestation{Data: ðpb.AttestationData{Slot: params.BeaconConfig().SlotsPerEpoch, Target: ðpb.Checkpoint{}}}, - s: &pb.BeaconState{}, + a: ðpb.Attestation{Data: ðpb.AttestationData{Slot: params.BeaconConfig().SlotsPerEpoch, Target: ðpb.Checkpoint{Root: make([]byte, 32)}}}, wantErr: true, wantErrString: "data slot is not in the same epoch as target 1 != 0", }, { name: "attestation's target root not in db", - a: ðpb.Attestation{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{Root: []byte{'A'}}}}, - s: &pb.BeaconState{}, + a: ðpb.Attestation{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{'A'}, 32)}}}, wantErr: true, wantErrString: "target root does not exist in db", }, { name: "no pre state for attestations's target block", a: ðpb.Attestation{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{Root: BlkWithOutStateRoot[:]}}}, - s: &pb.BeaconState{}, wantErr: true, wantErrString: "could not get pre state for slot 0", }, @@ -98,28 +96,32 @@ func TestStore_OnAttestation(t *testing.T) { name: "process attestation doesn't match current epoch", a: ðpb.Attestation{Data: ðpb.AttestationData{Slot: 100 * params.BeaconConfig().SlotsPerEpoch, Target: ðpb.Checkpoint{Epoch: 100, Root: BlkWithStateBadAttRoot[:]}}}, - s: &pb.BeaconState{Slot: 100 * params.BeaconConfig().SlotsPerEpoch}, wantErr: true, wantErrString: "target epoch 100 does not match current epoch", }, { - name: "process nil field (a.Target) in attestation", + name: "process nil attestation", a: nil, - s: &pb.BeaconState{}, wantErr: true, wantErrString: "nil attestation", }, { name: "process nil field (a.Data) in attestation", a: ðpb.Attestation{}, - s: &pb.BeaconState{}, wantErr: true, wantErrString: "nil attestation.Data field", }, { - name: "process nil field (a.Target) in attestation", - a: ðpb.Attestation{Data: ðpb.AttestationData{}}, - s: &pb.BeaconState{}, + name: "process nil field (a.Target) in attestation", + a: ðpb.Attestation{ + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: nil, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, + AggregationBits: make([]byte, 1), + Signature: make([]byte, 96), + }, wantErr: true, wantErrString: "nil attestation.Data.Target field", }, @@ -150,22 +152,16 @@ func TestStore_SaveCheckpointState(t *testing.T) { service, err := NewService(ctx, cfg) require.NoError(t, err) - s, err := stateTrie.InitializeFromProto(&pb.BeaconState{ - Fork: &pb.Fork{ - Epoch: 0, - CurrentVersion: params.BeaconConfig().GenesisForkVersion, - PreviousVersion: params.BeaconConfig().GenesisForkVersion, - }, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - StateRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), - BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), - LatestBlockHeader: ðpb.BeaconBlockHeader{}, - JustificationBits: []byte{0}, - Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector), - FinalizedCheckpoint: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{'A'}, 32)}, - Validators: []*ethpb.Validator{{PublicKey: bytesutil.PadTo([]byte("foo"), 48)}}, - Balances: []uint64{0}, - }) + s := testutil.NewBeaconState() + err = s.SetFinalizedCheckpoint(ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{'A'}, 32)}) + require.NoError(t, err) + val := ðpb.Validator{ + PublicKey: bytesutil.PadTo([]byte("foo"), 48), + WithdrawalCredentials: bytesutil.PadTo([]byte("bar"), 32), + } + err = s.SetValidators([]*ethpb.Validator{val}) + require.NoError(t, err) + err = s.SetBalances([]uint64{0}) require.NoError(t, err) r := [32]byte{'g'} require.NoError(t, service.beaconDB.SaveState(ctx, s, r)) @@ -230,7 +226,7 @@ func TestStore_UpdateCheckpointState(t *testing.T) { epoch := uint64(1) baseState, _ := testutil.DeterministicGenesisState(t, 1) require.NoError(t, baseState.SetSlot(epoch*params.BeaconConfig().SlotsPerEpoch)) - checkpoint := ðpb.Checkpoint{Epoch: epoch} + checkpoint := ðpb.Checkpoint{Epoch: epoch, Root: bytesutil.PadTo([]byte("hi"), 32)} require.NoError(t, service.beaconDB.SaveState(ctx, baseState, bytesutil.ToBytes32(checkpoint.Root))) returned, err := service.getAttPreState(ctx, checkpoint) require.NoError(t, err) @@ -241,7 +237,7 @@ func TestStore_UpdateCheckpointState(t *testing.T) { assert.NotNil(t, cached, "State should have been cached") epoch = uint64(2) - newCheckpoint := ðpb.Checkpoint{Epoch: epoch} + newCheckpoint := ðpb.Checkpoint{Epoch: epoch, Root: bytesutil.PadTo([]byte("bye"), 32)} require.NoError(t, service.beaconDB.SaveState(ctx, baseState, bytesutil.ToBytes32(newCheckpoint.Root))) returned, err = service.getAttPreState(ctx, newCheckpoint) require.NoError(t, err) @@ -268,7 +264,7 @@ func TestAttEpoch_MatchPrevEpoch(t *testing.T) { ctx, 0, params.BeaconConfig().SlotsPerEpoch*params.BeaconConfig().SecondsPerSlot, - ðpb.Checkpoint{}); err != nil { + ðpb.Checkpoint{Root: make([]byte, 32)}); err != nil { t.Error(err) } } @@ -302,7 +298,7 @@ func TestAttEpoch_NotMatch(t *testing.T) { ctx, 0, 2*params.BeaconConfig().SlotsPerEpoch*params.BeaconConfig().SecondsPerSlot, - ðpb.Checkpoint{}); !strings.Contains(err.Error(), + ðpb.Checkpoint{Root: make([]byte, 32)}); !strings.Contains(err.Error(), "target epoch 0 does not match current epoch 2 or prev epoch 1") { t.Error("Did not receive wanted error") } @@ -316,8 +312,12 @@ func TestVerifyBeaconBlock_NoBlock(t *testing.T) { service, err := NewService(ctx, cfg) require.NoError(t, err) - d := ðpb.AttestationData{} - assert.ErrorContains(t, "beacon block does not exist", service.verifyBeaconBlock(ctx, d)) + d := ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + } + assert.ErrorContains(t, "beacon block 0x000000000000 does not exist", service.verifyBeaconBlock(ctx, d)) } func TestVerifyBeaconBlock_futureBlock(t *testing.T) { @@ -328,7 +328,8 @@ func TestVerifyBeaconBlock_futureBlock(t *testing.T) { service, err := NewService(ctx, cfg) require.NoError(t, err) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2}} + b := testutil.NewBeaconBlock() + b.Block.Slot = 2 require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) r, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -345,7 +346,8 @@ func TestVerifyBeaconBlock_OK(t *testing.T) { service, err := NewService(ctx, cfg) require.NoError(t, err) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2}} + b := testutil.NewBeaconBlock() + b.Block.Slot = 2 require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) r, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -362,11 +364,14 @@ func TestVerifyLMDFFGConsistent_NotOK(t *testing.T) { service, err := NewService(ctx, cfg) require.NoError(t, err) - b32 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 32}} + b32 := testutil.NewBeaconBlock() + b32.Block.Slot = 32 require.NoError(t, service.beaconDB.SaveBlock(ctx, b32)) r32, err := stateutil.BlockRoot(b32.Block) require.NoError(t, err) - b33 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 33, ParentRoot: r32[:]}} + b33 := testutil.NewBeaconBlock() + b33.Block.Slot = 33 + b33.Block.ParentRoot = r32[:] require.NoError(t, service.beaconDB.SaveBlock(ctx, b33)) r33, err := stateutil.BlockRoot(b33.Block) require.NoError(t, err) @@ -383,11 +388,14 @@ func TestVerifyLMDFFGConsistent_OK(t *testing.T) { service, err := NewService(ctx, cfg) require.NoError(t, err) - b32 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 32}} + b32 := testutil.NewBeaconBlock() + b32.Block.Slot = 32 require.NoError(t, service.beaconDB.SaveBlock(ctx, b32)) r32, err := stateutil.BlockRoot(b32.Block) require.NoError(t, err) - b33 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 33, ParentRoot: r32[:]}} + b33 := testutil.NewBeaconBlock() + b33.Block.Slot = 33 + b33.Block.ParentRoot = r32[:] require.NoError(t, service.beaconDB.SaveBlock(ctx, b33)) r33, err := stateutil.BlockRoot(b33.Block) require.NoError(t, err) diff --git a/beacon-chain/blockchain/process_block_test.go b/beacon-chain/blockchain/process_block_test.go index bda84de91d..6cbe1b2caf 100644 --- a/beacon-chain/blockchain/process_block_test.go +++ b/beacon-chain/blockchain/process_block_test.go @@ -6,8 +6,8 @@ import ( "testing" "time" + "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" @@ -48,7 +48,9 @@ func TestStore_OnBlock(t *testing.T) { require.NoError(t, service.beaconDB.SaveState(ctx, st.Copy(), validGenesisRoot)) roots, err := blockTree1(db, validGenesisRoot[:]) require.NoError(t, err) - random := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1, ParentRoot: validGenesisRoot[:]}} + random := testutil.NewBeaconBlock() + random.Block.Slot = 1 + random.Block.ParentRoot = validGenesisRoot[:] assert.NoError(t, db.SaveBlock(ctx, random)) randomParentRoot, err := stateutil.BlockRoot(random.Block) assert.NoError(t, err) @@ -60,32 +62,46 @@ func TestStore_OnBlock(t *testing.T) { tests := []struct { name string - blk *ethpb.BeaconBlock + blk *ethpb.SignedBeaconBlock s *stateTrie.BeaconState time uint64 wantErrString string }{ { name: "parent block root does not have a state", - blk: ðpb.BeaconBlock{}, + blk: testutil.NewBeaconBlock(), s: st.Copy(), wantErrString: "could not reconstruct parent state", }, { - name: "block is from the future", - blk: ðpb.BeaconBlock{ParentRoot: randomParentRoot[:], Slot: params.BeaconConfig().FarFutureEpoch}, + name: "block is from the future", + blk: func() *ethpb.SignedBeaconBlock { + b := testutil.NewBeaconBlock() + b.Block.ParentRoot = randomParentRoot[:] + b.Block.Slot = params.BeaconConfig().FarFutureEpoch + return b + }(), s: st.Copy(), wantErrString: "far distant future", }, { - name: "could not get finalized block", - blk: ðpb.BeaconBlock{ParentRoot: randomParentRoot[:]}, + name: "could not get finalized block", + blk: func() *ethpb.SignedBeaconBlock { + b := testutil.NewBeaconBlock() + b.Block.ParentRoot = randomParentRoot[:] + return b + }(), s: st.Copy(), wantErrString: "is not a descendent of the current finalized block", }, { - name: "same slot as finalized block", - blk: ðpb.BeaconBlock{Slot: 0, ParentRoot: randomParentRoot2}, + name: "same slot as finalized block", + blk: func() *ethpb.SignedBeaconBlock { + b := testutil.NewBeaconBlock() + b.Block.Slot = 0 + b.Block.ParentRoot = randomParentRoot2 + return b + }(), s: st.Copy(), wantErrString: "block is equal or earlier than finalized block, slot 0 < slot 0", }, @@ -99,9 +115,9 @@ func TestStore_OnBlock(t *testing.T) { service.prevFinalizedCheckpt = ðpb.Checkpoint{Root: validGenesisRoot[:]} service.finalizedCheckpt.Root = roots[0] - root, err := stateutil.BlockRoot(tt.blk) + root, err := stateutil.BlockRoot(tt.blk.Block) assert.NoError(t, err) - err = service.onBlock(ctx, ðpb.SignedBeaconBlock{Block: tt.blk}, root) + err = service.onBlock(ctx, tt.blk, root) assert.ErrorContains(t, tt.wantErrString, err) }) } @@ -159,7 +175,7 @@ func TestRemoveStateSinceLastFinalized_EmptyStartSlot(t *testing.T) { require.NoError(t, err) service.genesisTime = time.Now() - update, err := service.shouldUpdateCurrentJustified(ctx, ðpb.Checkpoint{}) + update, err := service.shouldUpdateCurrentJustified(ctx, ðpb.Checkpoint{Root: make([]byte, 32)}) require.NoError(t, err) assert.Equal(t, true, update, "Should be able to update justified") lastJustifiedBlk := testutil.NewBeaconBlock() @@ -228,10 +244,12 @@ func TestCachedPreState_CanGetFromStateSummary(t *testing.T) { s, err := stateTrie.InitializeFromProto(&pb.BeaconState{Slot: 1, GenesisValidatorsRoot: params.BeaconConfig().ZeroHash[:]}) require.NoError(t, err) r := [32]byte{'A'} - b := ðpb.BeaconBlock{Slot: 1, ParentRoot: r[:]} + b := testutil.NewBeaconBlock() + b.Block.Slot = 1 + b.Block.ParentRoot = r[:] require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 1, Root: r[:]})) require.NoError(t, service.stateGen.SaveState(ctx, r, s)) - require.NoError(t, service.verifyBlkPreState(ctx, b)) + require.NoError(t, service.verifyBlkPreState(ctx, b.Block)) } func TestCachedPreState_CanGetFromDB(t *testing.T) { @@ -249,10 +267,12 @@ func TestCachedPreState_CanGetFromDB(t *testing.T) { require.NoError(t, err) r := [32]byte{'A'} - b := ðpb.BeaconBlock{Slot: 1, ParentRoot: r[:]} + b := testutil.NewBeaconBlock() + b.Block.Slot = 1 + b.Block.ParentRoot = r[:] service.finalizedCheckpt = ðpb.Checkpoint{Root: r[:]} - err = service.verifyBlkPreState(ctx, b) + err = service.verifyBlkPreState(ctx, b.Block) wanted := "could not reconstruct parent state" assert.ErrorContains(t, wanted, err) @@ -260,7 +280,7 @@ func TestCachedPreState_CanGetFromDB(t *testing.T) { require.NoError(t, err) require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 1, Root: r[:]})) require.NoError(t, service.stateGen.SaveState(ctx, r, s)) - require.NoError(t, service.verifyBlkPreState(ctx, b)) + require.NoError(t, service.verifyBlkPreState(ctx, b.Block)) } func TestUpdateJustified_CouldUpdateBest(t *testing.T) { @@ -271,7 +291,7 @@ func TestUpdateJustified_CouldUpdateBest(t *testing.T) { service, err := NewService(ctx, cfg) require.NoError(t, err) - signedBlock := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + signedBlock := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, signedBlock)) r, err := stateutil.BlockRoot(signedBlock.Block) require.NoError(t, err) @@ -303,7 +323,7 @@ func TestFillForkChoiceMissingBlocks_CanSave(t *testing.T) { service, err := NewService(ctx, cfg) require.NoError(t, err) service.forkChoiceStore = protoarray.New(0, 0, [32]byte{'A'}) - service.finalizedCheckpt = ðpb.Checkpoint{} + service.finalizedCheckpt = ðpb.Checkpoint{Root: make([]byte, 32)} genesisStateRoot := [32]byte{} genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) @@ -317,10 +337,12 @@ func TestFillForkChoiceMissingBlocks_CanSave(t *testing.T) { require.NoError(t, err) beaconState, _ := testutil.DeterministicGenesisState(t, 32) - block := ðpb.BeaconBlock{Slot: 9, ParentRoot: roots[8], Body: ðpb.BeaconBlockBody{Graffiti: []byte{}}} + block := testutil.NewBeaconBlock() + block.Block.Slot = 9 + block.Block.ParentRoot = roots[8] err = service.fillInForkChoiceMissingBlocks( - context.Background(), block, beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint()) + context.Background(), block.Block, beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint()) require.NoError(t, err) // 5 nodes from the block tree 1. B0 - B3 - B4 - B6 - B8 @@ -351,15 +373,20 @@ func TestFillForkChoiceMissingBlocks_FilterFinalized(t *testing.T) { require.NoError(t, service.beaconDB.SaveState(ctx, st.Copy(), validGenesisRoot)) // Define a tree branch, slot 63 <- 64 <- 65 - b63 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 63, Body: ðpb.BeaconBlockBody{}}} + b63 := testutil.NewBeaconBlock() + b63.Block.Slot = 63 require.NoError(t, service.beaconDB.SaveBlock(ctx, b63)) r63, err := stateutil.BlockRoot(b63.Block) require.NoError(t, err) - b64 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 64, ParentRoot: r63[:], Body: ðpb.BeaconBlockBody{}}} + b64 := testutil.NewBeaconBlock() + b64.Block.Slot = 64 + b64.Block.ParentRoot = r63[:] require.NoError(t, service.beaconDB.SaveBlock(ctx, b64)) r64, err := stateutil.BlockRoot(b64.Block) require.NoError(t, err) - b65 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 65, ParentRoot: r64[:], Body: ðpb.BeaconBlockBody{}}} + b65 := testutil.NewBeaconBlock() + b65.Block.Slot = 65 + b65.Block.ParentRoot = r64[:] require.NoError(t, service.beaconDB.SaveBlock(ctx, b65)) beaconState, _ := testutil.DeterministicGenesisState(t, 32) @@ -380,58 +407,74 @@ func TestFillForkChoiceMissingBlocks_FilterFinalized(t *testing.T) { // \- B3 - B4 - B6 - B8 // (B1, and B3 are all from the same slots) func blockTree1(db db.Database, genesisRoot []byte) ([][]byte, error) { - b0 := ðpb.BeaconBlock{Slot: 0, ParentRoot: genesisRoot} - r0, err := ssz.HashTreeRoot(b0) + genesisRoot = bytesutil.PadTo(genesisRoot, 32) + b0 := testutil.NewBeaconBlock() + b0.Block.Slot = 0 + b0.Block.ParentRoot = genesisRoot + r0, err := b0.Block.HashTreeRoot() if err != nil { return nil, err } - b1 := ðpb.BeaconBlock{Slot: 1, ParentRoot: r0[:]} - r1, err := ssz.HashTreeRoot(b1) + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = r0[:] + r1, err := b1.Block.HashTreeRoot() if err != nil { return nil, err } - b3 := ðpb.BeaconBlock{Slot: 3, ParentRoot: r0[:]} - r3, err := ssz.HashTreeRoot(b3) + b3 := testutil.NewBeaconBlock() + b3.Block.Slot = 3 + b3.Block.ParentRoot = r0[:] + r3, err := b3.Block.HashTreeRoot() if err != nil { return nil, err } - b4 := ðpb.BeaconBlock{Slot: 4, ParentRoot: r3[:]} - r4, err := ssz.HashTreeRoot(b4) + b4 := testutil.NewBeaconBlock() + b4.Block.Slot = 4 + b4.Block.ParentRoot = r3[:] + r4, err := b4.Block.HashTreeRoot() if err != nil { return nil, err } - b5 := ðpb.BeaconBlock{Slot: 5, ParentRoot: r4[:]} - r5, err := ssz.HashTreeRoot(b5) + b5 := testutil.NewBeaconBlock() + b5.Block.Slot = 5 + b5.Block.ParentRoot = r4[:] + r5, err := b5.Block.HashTreeRoot() if err != nil { return nil, err } - b6 := ðpb.BeaconBlock{Slot: 6, ParentRoot: r4[:]} - r6, err := ssz.HashTreeRoot(b6) + b6 := testutil.NewBeaconBlock() + b6.Block.Slot = 6 + b6.Block.ParentRoot = r4[:] + r6, err := b6.Block.HashTreeRoot() if err != nil { return nil, err } - b7 := ðpb.BeaconBlock{Slot: 7, ParentRoot: r5[:]} - r7, err := ssz.HashTreeRoot(b7) + b7 := testutil.NewBeaconBlock() + b7.Block.Slot = 7 + b7.Block.ParentRoot = r5[:] + r7, err := b7.Block.HashTreeRoot() if err != nil { return nil, err } - b8 := ðpb.BeaconBlock{Slot: 8, ParentRoot: r6[:]} - r8, err := ssz.HashTreeRoot(b8) + b8 := testutil.NewBeaconBlock() + b8.Block.Slot = 8 + b8.Block.ParentRoot = r6[:] + r8, err := b8.Block.HashTreeRoot() if err != nil { return nil, err } st := testutil.NewBeaconState() - for _, b := range []*ethpb.BeaconBlock{b0, b1, b3, b4, b5, b6, b7, b8} { + for _, b := range []*ethpb.SignedBeaconBlock{b0, b1, b3, b4, b5, b6, b7, b8} { beaconBlock := testutil.NewBeaconBlock() - beaconBlock.Block.Slot = b.Slot - beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.ParentRoot, 32) - beaconBlock.Block.Body = ðpb.BeaconBlockBody{} + beaconBlock.Block.Slot = b.Block.Slot + beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) if err := db.SaveBlock(context.Background(), beaconBlock); err != nil { return nil, err } if err := db.SaveState(context.Background(), st.Copy(), bytesutil.ToBytes32(beaconBlock.Block.ParentRoot)); err != nil { - return nil, err + return nil, errors.Wrap(err, "could not save state") } } if err := db.SaveState(context.Background(), st.Copy(), r1); err != nil { @@ -461,20 +504,25 @@ func TestAncestor_HandleSkipSlot(t *testing.T) { service, err := NewService(ctx, cfg) require.NoError(t, err) - b1 := ðpb.BeaconBlock{Slot: 1, ParentRoot: []byte{'a'}} - r1, err := ssz.HashTreeRoot(b1) + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = bytesutil.PadTo([]byte{'a'}, 32) + r1, err := b1.Block.HashTreeRoot() require.NoError(t, err) - b100 := ðpb.BeaconBlock{Slot: 100, ParentRoot: r1[:]} - r100, err := ssz.HashTreeRoot(b100) + b100 := testutil.NewBeaconBlock() + b100.Block.Slot = 100 + b100.Block.ParentRoot = r1[:] + r100, err := b100.Block.HashTreeRoot() require.NoError(t, err) - b200 := ðpb.BeaconBlock{Slot: 200, ParentRoot: r100[:]} - r200, err := ssz.HashTreeRoot(b200) + b200 := testutil.NewBeaconBlock() + b200.Block.Slot = 200 + b200.Block.ParentRoot = r100[:] + r200, err := b200.Block.HashTreeRoot() require.NoError(t, err) - for _, b := range []*ethpb.BeaconBlock{b1, b100, b200} { + for _, b := range []*ethpb.SignedBeaconBlock{b1, b100, b200} { beaconBlock := testutil.NewBeaconBlock() - beaconBlock.Block.Slot = b.Slot - beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.ParentRoot, 32) - beaconBlock.Block.Body = ðpb.BeaconBlockBody{} + beaconBlock.Block.Slot = b.Block.Slot + beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) require.NoError(t, db.SaveBlock(context.Background(), beaconBlock)) } @@ -557,16 +605,20 @@ func TestFinalizedImpliesNewJustified(t *testing.T) { require.NoError(t, service.beaconDB.SaveState(ctx, genesisState, bytesutil.ToBytes32(test.want.Root))) if test.args.diffFinalizedCheckPoint { - b1 := ðpb.BeaconBlock{Slot: 1, ParentRoot: []byte{'a'}} - r1, err := ssz.HashTreeRoot(b1) + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = bytesutil.PadTo([]byte{'a'}, 32) + r1, err := b1.Block.HashTreeRoot() require.NoError(t, err) - b100 := ðpb.BeaconBlock{Slot: 100, ParentRoot: r1[:]} - r100, err := ssz.HashTreeRoot(b100) + b100 := testutil.NewBeaconBlock() + b100.Block.Slot = 100 + b100.Block.ParentRoot = r1[:] + r100, err := b100.Block.HashTreeRoot() require.NoError(t, err) - for _, b := range []*ethpb.BeaconBlock{b1, b100} { + for _, b := range []*ethpb.SignedBeaconBlock{b1, b100} { beaconBlock := testutil.NewBeaconBlock() - beaconBlock.Block.Slot = b.Slot - beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.ParentRoot, 32) + beaconBlock.Block.Slot = b.Block.Slot + beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) require.NoError(t, service.beaconDB.SaveBlock(context.Background(), beaconBlock)) } service.finalizedCheckpt = ðpb.Checkpoint{Root: []byte{'c'}, Epoch: 1} @@ -665,7 +717,7 @@ func TestUpdateJustifiedInitSync(t *testing.T) { service, err := NewService(ctx, cfg) require.NoError(t, err) - gBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + gBlk := testutil.NewBeaconBlock() gRoot, err := stateutil.BlockRoot(gBlk.Block) require.NoError(t, err) require.NoError(t, service.beaconDB.SaveBlock(ctx, gBlk)) diff --git a/beacon-chain/blockchain/receive_attestation_test.go b/beacon-chain/blockchain/receive_attestation_test.go index be277aee2d..449899fd97 100644 --- a/beacon-chain/blockchain/receive_attestation_test.go +++ b/beacon-chain/blockchain/receive_attestation_test.go @@ -17,6 +17,6 @@ func TestVerifyCheckpointEpoch_Ok(t *testing.T) { chainService := setupBeaconChain(t, db, sc) chainService.genesisTime = time.Now() - assert.Equal(t, true, chainService.verifyCheckpointEpoch(ðpb.Checkpoint{})) + assert.Equal(t, true, chainService.verifyCheckpointEpoch(ðpb.Checkpoint{Root: make([]byte, 32)})) assert.Equal(t, false, chainService.verifyCheckpointEpoch(ðpb.Checkpoint{Epoch: 1})) } diff --git a/beacon-chain/blockchain/receive_block_test.go b/beacon-chain/blockchain/receive_block_test.go index 86346ab713..4b252798dd 100644 --- a/beacon-chain/blockchain/receive_block_test.go +++ b/beacon-chain/blockchain/receive_block_test.go @@ -322,8 +322,8 @@ func TestService_ReceiveBlockBatch(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { db, stateSummaryCache := testDB.SetupDB(t) - genesisBlockRoot := bytesutil.ToBytes32(nil) - + genesisBlockRoot, err := genesis.HashTreeRoot(ctx) + require.NoError(t, err) cfg := &Config{ BeaconDB: db, ForkChoiceStore: protoarray.New( diff --git a/beacon-chain/blockchain/service_test.go b/beacon-chain/blockchain/service_test.go index aa55119c37..05970293a4 100644 --- a/beacon-chain/blockchain/service_test.go +++ b/beacon-chain/blockchain/service_test.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" @@ -179,11 +178,12 @@ func TestChainService_InitializeBeaconChain(t *testing.T) { err = genState.SetEth1Data(ðpb.Eth1Data{ DepositRoot: hashTreeRoot[:], DepositCount: uint64(len(deposits)), + BlockHash: make([]byte, 32), }) genState, err = b.ProcessPreGenesisDeposits(ctx, genState, deposits) require.NoError(t, err) - _, err = bc.initializeBeaconChain(ctx, time.Unix(0, 0), genState, ðpb.Eth1Data{DepositRoot: hashTreeRoot[:]}) + _, err = bc.initializeBeaconChain(ctx, time.Unix(0, 0), genState, ðpb.Eth1Data{DepositRoot: hashTreeRoot[:], BlockHash: make([]byte, 32)}) require.NoError(t, err) _, err = bc.HeadState(ctx) @@ -275,8 +275,9 @@ func TestChainService_SaveHeadNoDB(t *testing.T) { beaconDB: db, stateGen: stategen.New(db, sc), } - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} - r, err := ssz.HashTreeRoot(b) + b := testutil.NewBeaconBlock() + b.Block.Slot = 1 + r, err := b.HashTreeRoot() require.NoError(t, err) newState := testutil.NewBeaconState() require.NoError(t, s.stateGen.SaveState(ctx, r, newState)) @@ -294,13 +295,13 @@ func TestHasBlock_ForkChoiceAndDB(t *testing.T) { db, _ := testDB.SetupDB(t) s := &Service{ forkChoiceStore: protoarray.New(0, 0, [32]byte{}), - finalizedCheckpt: ðpb.Checkpoint{}, + finalizedCheckpt: ðpb.Checkpoint{Root: make([]byte, 32)}, beaconDB: db, } block := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}} r, err := stateutil.BlockRoot(block.Block) require.NoError(t, err) - bs := &pb.BeaconState{FinalizedCheckpoint: ðpb.Checkpoint{}, CurrentJustifiedCheckpoint: ðpb.Checkpoint{}} + bs := &pb.BeaconState{FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}} state, err := beaconstate.InitializeFromProto(bs) require.NoError(t, err) require.NoError(t, s.insertBlockAndAttestationsToForkChoiceStore(ctx, block.Block, r, state)) @@ -315,7 +316,7 @@ func BenchmarkHasBlockDB(b *testing.B) { s := &Service{ beaconDB: db, } - block := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + block := testutil.NewBeaconBlock() require.NoError(b, s.beaconDB.SaveBlock(ctx, block)) r, err := stateutil.BlockRoot(block.Block) require.NoError(b, err) @@ -331,13 +332,13 @@ func BenchmarkHasBlockForkChoiceStore(b *testing.B) { db, _ := testDB.SetupDB(b) s := &Service{ forkChoiceStore: protoarray.New(0, 0, [32]byte{}), - finalizedCheckpt: ðpb.Checkpoint{}, + finalizedCheckpt: ðpb.Checkpoint{Root: make([]byte, 32)}, beaconDB: db, } block := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}} r, err := stateutil.BlockRoot(block.Block) require.NoError(b, err) - bs := &pb.BeaconState{FinalizedCheckpoint: ðpb.Checkpoint{}, CurrentJustifiedCheckpoint: ðpb.Checkpoint{}} + bs := &pb.BeaconState{FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}} state, err := beaconstate.InitializeFromProto(bs) require.NoError(b, err) require.NoError(b, s.insertBlockAndAttestationsToForkChoiceStore(ctx, block.Block, r, state)) diff --git a/beacon-chain/blockchain/testing/mock.go b/beacon-chain/blockchain/testing/mock.go index e662defbb4..0a2c913bfb 100644 --- a/beacon-chain/blockchain/testing/mock.go +++ b/beacon-chain/blockchain/testing/mock.go @@ -233,8 +233,10 @@ func (ms *ChainService) HeadSlot() uint64 { // HeadRoot mocks HeadRoot method in chain service. func (ms *ChainService) HeadRoot(ctx context.Context) ([]byte, error) { - return ms.Root, nil - + if len(ms.Root) > 0 { + return ms.Root, nil + } + return make([]byte, 32), nil } // HeadBlock mocks HeadBlock method in chain service. diff --git a/beacon-chain/cache/attestation_data_test.go b/beacon-chain/cache/attestation_data_test.go index c82bce990f..b98a34d338 100644 --- a/beacon-chain/cache/attestation_data_test.go +++ b/beacon-chain/cache/attestation_data_test.go @@ -26,7 +26,7 @@ func TestAttestationCache_RoundTrip(t *testing.T) { assert.NoError(t, c.MarkInProgress(req)) res := ðpb.AttestationData{ - Target: ðpb.Checkpoint{Epoch: 5}, + Target: ðpb.Checkpoint{Epoch: 5, Root: make([]byte, 32)}, } assert.NoError(t, c.Put(ctx, req, res)) diff --git a/beacon-chain/cache/checkpoint_state_test.go b/beacon-chain/cache/checkpoint_state_test.go index ce98362de0..a40b5314c0 100644 --- a/beacon-chain/cache/checkpoint_state_test.go +++ b/beacon-chain/cache/checkpoint_state_test.go @@ -61,7 +61,7 @@ func TestCheckpointStateCache_MaxSize(t *testing.T) { for i := uint64(0); i < uint64(maxCheckpointStateSize+100); i++ { require.NoError(t, st.SetSlot(i)) - require.NoError(t, c.AddCheckpointState(ðpb.Checkpoint{Epoch: i}, st)) + require.NoError(t, c.AddCheckpointState(ðpb.Checkpoint{Epoch: i, Root: make([]byte, 32)}, st)) } assert.Equal(t, maxCheckpointStateSize, len(c.cache.Keys())) diff --git a/beacon-chain/cache/depositcache/BUILD.bazel b/beacon-chain/cache/depositcache/BUILD.bazel index 06aa97f282..3b4bfe740d 100644 --- a/beacon-chain/cache/depositcache/BUILD.bazel +++ b/beacon-chain/cache/depositcache/BUILD.bazel @@ -18,7 +18,6 @@ go_library( "@com_github_prometheus_client_golang//prometheus:go_default_library", "@com_github_prometheus_client_golang//prometheus/promauto:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@io_opencensus_go//trace:go_default_library", ], @@ -40,7 +39,6 @@ go_test( "//shared/trieutil:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//hooks/test:go_default_library", ], ) diff --git a/beacon-chain/cache/depositcache/deposits_cache.go b/beacon-chain/cache/depositcache/deposits_cache.go index 5eb18734f2..00d54e4cec 100644 --- a/beacon-chain/cache/depositcache/deposits_cache.go +++ b/beacon-chain/cache/depositcache/deposits_cache.go @@ -15,7 +15,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" @@ -128,7 +127,7 @@ func (dc *DepositCache) InsertFinalizedDeposits(ctx context.Context, eth1Deposit if d.Index > eth1DepositIndex { break } - depHash, err := ssz.HashTreeRoot(d.Deposit.Data) + depHash, err := d.Deposit.Data.HashTreeRoot() if err != nil { log.WithError(err).Error("Could not hash deposit data. Finalized deposit cache not updated.") return diff --git a/beacon-chain/cache/depositcache/deposits_cache_test.go b/beacon-chain/cache/depositcache/deposits_cache_test.go index 64a3638a39..5e4cc51e7e 100644 --- a/beacon-chain/cache/depositcache/deposits_cache_test.go +++ b/beacon-chain/cache/depositcache/deposits_cache_test.go @@ -8,7 +8,6 @@ import ( "testing" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" @@ -162,32 +161,74 @@ func TestDepositsNumberAndRootAtHeight_ReturnsAppropriateCountAndRoot(t *testing dc.deposits = []*dbpb.DepositContainer{ { Eth1BlockHeight: 10, - Deposit: ðpb.Deposit{}, + Deposit: ðpb.Deposit{ + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), + }, + }, }, { Eth1BlockHeight: 10, - Deposit: ðpb.Deposit{}, + Deposit: ðpb.Deposit{ + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), + }, + }, }, { Eth1BlockHeight: 10, - Deposit: ðpb.Deposit{}, + Deposit: ðpb.Deposit{ + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), + }, + }, }, { Eth1BlockHeight: 10, - Deposit: ðpb.Deposit{}, + Deposit: ðpb.Deposit{ + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), + }, + }, }, { Eth1BlockHeight: 11, - Deposit: ðpb.Deposit{}, - DepositRoot: []byte("root"), + Deposit: ðpb.Deposit{ + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), + }, + }, + DepositRoot: bytesutil.PadTo([]byte("root"), 32), }, { Eth1BlockHeight: 12, - Deposit: ðpb.Deposit{}, + Deposit: ðpb.Deposit{ + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), + }, + }, }, { Eth1BlockHeight: 12, - Deposit: ðpb.Deposit{}, + Deposit: ðpb.Deposit{ + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), + }, + }, }, } @@ -203,13 +244,25 @@ func TestDepositsNumberAndRootAtHeight_ReturnsEmptyTrieIfBlockHeightLessThanOlde dc.deposits = []*dbpb.DepositContainer{ { Eth1BlockHeight: 10, - Deposit: ðpb.Deposit{}, - DepositRoot: []byte("root"), + Deposit: ðpb.Deposit{ + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), + }, + }, + DepositRoot: bytesutil.PadTo([]byte("root"), 32), }, { Eth1BlockHeight: 11, - Deposit: ðpb.Deposit{}, - DepositRoot: []byte("root"), + Deposit: ðpb.Deposit{ + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), + }, + }, + DepositRoot: bytesutil.PadTo([]byte("root"), 32), }, } @@ -227,7 +280,9 @@ func TestDepositByPubkey_ReturnsFirstMatchingDeposit(t *testing.T) { Eth1BlockHeight: 9, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("pk0"), + PublicKey: bytesutil.PadTo([]byte("pk0"), 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, }, @@ -235,7 +290,9 @@ func TestDepositByPubkey_ReturnsFirstMatchingDeposit(t *testing.T) { Eth1BlockHeight: 10, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("pk1"), + PublicKey: bytesutil.PadTo([]byte("pk1"), 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, }, @@ -243,7 +300,9 @@ func TestDepositByPubkey_ReturnsFirstMatchingDeposit(t *testing.T) { Eth1BlockHeight: 11, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("pk1"), + PublicKey: bytesutil.PadTo([]byte("pk1"), 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, }, @@ -251,15 +310,18 @@ func TestDepositByPubkey_ReturnsFirstMatchingDeposit(t *testing.T) { Eth1BlockHeight: 12, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("pk2"), + PublicKey: bytesutil.PadTo([]byte("pk2"), 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, }, } - dep, blkNum := dc.DepositByPubkey(context.Background(), []byte("pk1")) + pk1 := bytesutil.PadTo([]byte("pk1"), 48) + dep, blkNum := dc.DepositByPubkey(context.Background(), pk1) - if !bytes.Equal(dep.Data.PublicKey, []byte("pk1")) { + if dep == nil || !bytes.Equal(dep.Data.PublicKey, pk1) { t.Error("Returned wrong deposit") } assert.Equal(t, 0, blkNum.Cmp(big.NewInt(10)), @@ -274,7 +336,9 @@ func TestFinalizedDeposits_DepositsCachedCorrectly(t *testing.T) { { Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{0}, + PublicKey: bytesutil.PadTo([]byte{0}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 0, @@ -282,7 +346,9 @@ func TestFinalizedDeposits_DepositsCachedCorrectly(t *testing.T) { { Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{1}, + PublicKey: bytesutil.PadTo([]byte{1}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 1, @@ -290,7 +356,9 @@ func TestFinalizedDeposits_DepositsCachedCorrectly(t *testing.T) { { Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{2}, + PublicKey: bytesutil.PadTo([]byte{2}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 2, @@ -299,7 +367,9 @@ func TestFinalizedDeposits_DepositsCachedCorrectly(t *testing.T) { dc.deposits = append(finalizedDeposits, &dbpb.DepositContainer{ Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{3}, + PublicKey: bytesutil.PadTo([]byte{3}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 3, @@ -313,7 +383,7 @@ func TestFinalizedDeposits_DepositsCachedCorrectly(t *testing.T) { var deps [][]byte for _, d := range finalizedDeposits { - hash, err := ssz.HashTreeRoot(d.Deposit.Data) + hash, err := d.Deposit.Data.HashTreeRoot() require.NoError(t, err, "Could not hash deposit data") deps = append(deps, hash[:]) } @@ -330,7 +400,9 @@ func TestFinalizedDeposits_UtilizesPreviouslyCachedDeposits(t *testing.T) { { Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{0}, + PublicKey: bytesutil.PadTo([]byte{0}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 0, @@ -338,7 +410,9 @@ func TestFinalizedDeposits_UtilizesPreviouslyCachedDeposits(t *testing.T) { { Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{1}, + PublicKey: bytesutil.PadTo([]byte{1}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 1, @@ -347,7 +421,9 @@ func TestFinalizedDeposits_UtilizesPreviouslyCachedDeposits(t *testing.T) { newFinalizedDeposit := dbpb.DepositContainer{ Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{2}, + PublicKey: bytesutil.PadTo([]byte{2}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 2, @@ -365,7 +441,7 @@ func TestFinalizedDeposits_UtilizesPreviouslyCachedDeposits(t *testing.T) { var deps [][]byte for _, d := range append(oldFinalizedDeposits, &newFinalizedDeposit) { - hash, err := ssz.HashTreeRoot(d.Deposit.Data) + hash, err := d.Deposit.Data.HashTreeRoot() require.NoError(t, err, "Could not hash deposit data") deps = append(deps, hash[:]) } @@ -393,7 +469,9 @@ func TestNonFinalizedDeposits_ReturnsAllNonFinalizedDeposits(t *testing.T) { Eth1BlockHeight: 10, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{0}, + PublicKey: bytesutil.PadTo([]byte{0}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 0, @@ -402,7 +480,9 @@ func TestNonFinalizedDeposits_ReturnsAllNonFinalizedDeposits(t *testing.T) { Eth1BlockHeight: 10, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{1}, + PublicKey: bytesutil.PadTo([]byte{1}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 1, @@ -413,7 +493,9 @@ func TestNonFinalizedDeposits_ReturnsAllNonFinalizedDeposits(t *testing.T) { Eth1BlockHeight: 10, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{2}, + PublicKey: bytesutil.PadTo([]byte{2}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 2, @@ -422,7 +504,9 @@ func TestNonFinalizedDeposits_ReturnsAllNonFinalizedDeposits(t *testing.T) { Eth1BlockHeight: 11, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{3}, + PublicKey: bytesutil.PadTo([]byte{3}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 3, @@ -442,7 +526,9 @@ func TestNonFinalizedDeposits_ReturnsNonFinalizedDepositsUpToBlockNumber(t *test Eth1BlockHeight: 10, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{0}, + PublicKey: bytesutil.PadTo([]byte{0}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 0, @@ -451,7 +537,9 @@ func TestNonFinalizedDeposits_ReturnsNonFinalizedDepositsUpToBlockNumber(t *test Eth1BlockHeight: 10, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{1}, + PublicKey: bytesutil.PadTo([]byte{1}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 1, @@ -462,7 +550,9 @@ func TestNonFinalizedDeposits_ReturnsNonFinalizedDepositsUpToBlockNumber(t *test Eth1BlockHeight: 10, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{2}, + PublicKey: bytesutil.PadTo([]byte{2}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 2, @@ -471,7 +561,9 @@ func TestNonFinalizedDeposits_ReturnsNonFinalizedDepositsUpToBlockNumber(t *test Eth1BlockHeight: 11, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{3}, + PublicKey: bytesutil.PadTo([]byte{3}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, }, Index: 3, diff --git a/beacon-chain/cache/depositcache/pending_deposits_test.go b/beacon-chain/cache/depositcache/pending_deposits_test.go index 9396c5304a..a0d2efabd7 100644 --- a/beacon-chain/cache/depositcache/pending_deposits_test.go +++ b/beacon-chain/cache/depositcache/pending_deposits_test.go @@ -29,14 +29,28 @@ func TestInsertPendingDeposit_ignoresNilDeposit(t *testing.T) { assert.Equal(t, 0, len(dc.pendingDeposits)) } +func makeDepositProof() [][]byte { + proof := make([][]byte, int(params.BeaconConfig().DepositContractTreeDepth)+1) + for i := range proof { + proof[i] = make([]byte, 32) + } + return proof +} + func TestRemovePendingDeposit_OK(t *testing.T) { db := DepositCache{} - proof1 := make([][]byte, int(params.BeaconConfig().DepositContractTreeDepth)+1) + proof1 := makeDepositProof() proof1[0] = bytesutil.PadTo([]byte{'A'}, 32) - proof2 := make([][]byte, int(params.BeaconConfig().DepositContractTreeDepth)+1) + proof2 := makeDepositProof() proof2[0] = bytesutil.PadTo([]byte{'A'}, 32) - depToRemove := ðpb.Deposit{Proof: proof1} - otherDep := ðpb.Deposit{Proof: proof2} + data := ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Amount: 0, + Signature: make([]byte, 96), + } + depToRemove := ðpb.Deposit{Proof: proof1, Data: data} + otherDep := ðpb.Deposit{Proof: proof2, Data: data} db.pendingDeposits = []*dbpb.DepositContainer{ {Deposit: depToRemove, Index: 1}, {Deposit: otherDep, Index: 5}, @@ -57,9 +71,15 @@ func TestRemovePendingDeposit_IgnoresNilDeposit(t *testing.T) { func TestPendingDeposit_RoundTrip(t *testing.T) { dc := DepositCache{} - proof := make([][]byte, int(params.BeaconConfig().DepositContractTreeDepth)+1) + proof := makeDepositProof() proof[0] = bytesutil.PadTo([]byte{'A'}, 32) - dep := ðpb.Deposit{Proof: proof} + data := ðpb.Deposit_Data{ + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + Amount: 0, + Signature: make([]byte, 96), + } + dep := ðpb.Deposit{Proof: proof, Data: data} dc.InsertPendingDeposit(context.Background(), dep, 111, 100, [32]byte{}) dc.RemovePendingDeposit(context.Background(), dep) assert.Equal(t, 0, len(dc.pendingDeposits), "Failed to insert & delete a pending deposit") diff --git a/beacon-chain/core/blocks/BUILD.bazel b/beacon-chain/core/blocks/BUILD.bazel index ab6c367c64..3741de51e9 100644 --- a/beacon-chain/core/blocks/BUILD.bazel +++ b/beacon-chain/core/blocks/BUILD.bazel @@ -63,6 +63,7 @@ go_test( "randao_test.go", ], embed = [":go_default_library"], + shard_count = 2, deps = [ "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/state:go_default_library", @@ -82,7 +83,6 @@ go_test( "@com_github_google_gofuzz//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", ], ) diff --git a/beacon-chain/core/blocks/attestation_test.go b/beacon-chain/core/blocks/attestation_test.go index 7eeee5d2f9..3819e55f2e 100644 --- a/beacon-chain/core/blocks/attestation_test.go +++ b/beacon-chain/core/blocks/attestation_test.go @@ -15,6 +15,7 @@ import ( attaggregation "github.com/prysmaticlabs/prysm/shared/aggregation/attestations" "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bls" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -25,7 +26,7 @@ func TestProcessAttestations_InclusionDelayFailure(t *testing.T) { attestations := []*ethpb.Attestation{ { Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{Epoch: 0}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, Slot: 5, }, }, @@ -81,8 +82,8 @@ func TestProcessAttestations_CurrentEpochFFGDataMismatches(t *testing.T) { attestations := []*ethpb.Attestation{ { Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{Epoch: 0}, - Source: ðpb.Checkpoint{Epoch: 1}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, AggregationBits: aggBits, }, @@ -127,8 +128,8 @@ func TestProcessAttestations_PrevEpochFFGDataMismatches(t *testing.T) { attestations := []*ethpb.Attestation{ { Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 1}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, Slot: params.BeaconConfig().SlotsPerEpoch, }, AggregationBits: aggBits, @@ -207,10 +208,12 @@ func TestProcessAttestations_OK(t *testing.T) { copy(mockRoot[:], "hello-world") att := ðpb.Attestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0, Root: mockRoot[:]}, - Target: ðpb.Checkpoint{Epoch: 0, Root: mockRoot[:]}, + Source: ðpb.Checkpoint{Epoch: 0, Root: mockRoot[:]}, + Target: ðpb.Checkpoint{Epoch: 0, Root: mockRoot[:]}, + BeaconBlockRoot: make([]byte, 32), }, AggregationBits: aggBits, + Signature: make([]byte, 96), } cfc := beaconState.CurrentJustifiedCheckpoint() @@ -232,23 +235,21 @@ func TestProcessAttestations_OK(t *testing.T) { } att.Signature = bls.AggregateSignatures(sigs).Marshal()[:] - block := ðpb.BeaconBlock{ - Body: ðpb.BeaconBlockBody{ - Attestations: []*ethpb.Attestation{att}, - }, - } + block := testutil.NewBeaconBlock() + block.Block.Body.Attestations = []*ethpb.Attestation{att} err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay) require.NoError(t, err) - _, err = blocks.ProcessAttestations(context.Background(), beaconState, block.Body) + _, err = blocks.ProcessAttestations(context.Background(), beaconState, block.Block.Body) assert.NoError(t, err) } func TestProcessAggregatedAttestation_OverlappingBits(t *testing.T) { beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) data := ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")}, - Target: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")}, + Source: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)}, + BeaconBlockRoot: make([]byte, 32), } aggBits1 := bitfield.NewBitlist(4) aggBits1.SetBitAt(0, true) @@ -260,7 +261,7 @@ func TestProcessAggregatedAttestation_OverlappingBits(t *testing.T) { } cfc := beaconState.CurrentJustifiedCheckpoint() - cfc.Root = []byte("hello-world") + cfc.Root = bytesutil.PadTo([]byte("hello-world"), 32) require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cfc)) require.NoError(t, beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{})) @@ -311,8 +312,9 @@ func TestProcessAggregatedAttestation_NoOverlappingBits(t *testing.T) { var mockRoot [32]byte copy(mockRoot[:], "hello-world") data := ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0, Root: mockRoot[:]}, - Target: ðpb.Checkpoint{Epoch: 0, Root: mockRoot[:]}, + Source: ðpb.Checkpoint{Epoch: 0, Root: mockRoot[:]}, + Target: ðpb.Checkpoint{Epoch: 0, Root: mockRoot[:]}, + BeaconBlockRoot: make([]byte, 32), } aggBits1 := bitfield.NewBitlist(9) aggBits1.SetBitAt(0, true) @@ -320,6 +322,7 @@ func TestProcessAggregatedAttestation_NoOverlappingBits(t *testing.T) { att1 := ðpb.Attestation{ Data: data, AggregationBits: aggBits1, + Signature: make([]byte, 32), } cfc := beaconState.CurrentJustifiedCheckpoint() @@ -347,6 +350,7 @@ func TestProcessAggregatedAttestation_NoOverlappingBits(t *testing.T) { att2 := ðpb.Attestation{ Data: data, AggregationBits: aggBits2, + Signature: make([]byte, 32), } committee, err = helpers.BeaconCommitteeFromState(beaconState, att2.Data.Slot, att2.Data.CommitteeIndex) @@ -365,16 +369,13 @@ func TestProcessAggregatedAttestation_NoOverlappingBits(t *testing.T) { aggregatedAtt, err := attaggregation.AggregatePair(att1, att2) require.NoError(t, err) - block := ðpb.BeaconBlock{ - Body: ðpb.BeaconBlockBody{ - Attestations: []*ethpb.Attestation{aggregatedAtt}, - }, - } + block := testutil.NewBeaconBlock() + block.Block.Body.Attestations = []*ethpb.Attestation{aggregatedAtt} err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay) require.NoError(t, err) - _, err = blocks.ProcessAttestations(context.Background(), beaconState, block.Body) + _, err = blocks.ProcessAttestations(context.Background(), beaconState, block.Block.Body) assert.NoError(t, err) } @@ -384,7 +385,7 @@ func TestProcessAttestationsNoVerify_IncorrectSlotTargetEpoch(t *testing.T) { att := ðpb.Attestation{ Data: ðpb.AttestationData{ Slot: params.BeaconConfig().SlotsPerEpoch, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, } wanted := fmt.Sprintf("data slot is not in the same epoch as target %d != %d", helpers.SlotToEpoch(att.Data.Slot), att.Data.Target.Epoch) @@ -404,7 +405,7 @@ func TestProcessAttestationsNoVerify_OK(t *testing.T) { att := ðpb.Attestation{ Data: ðpb.AttestationData{ Source: ðpb.Checkpoint{Epoch: 0, Root: mockRoot[:]}, - Target: ðpb.Checkpoint{Epoch: 0}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, }, AggregationBits: aggBits, } @@ -433,7 +434,7 @@ func TestProcessAttestationsNoVerify_BadAttIdx(t *testing.T) { Data: ðpb.AttestationData{ CommitteeIndex: 100, Source: ðpb.Checkpoint{Epoch: 0, Root: mockRoot[:]}, - Target: ðpb.Checkpoint{Epoch: 0}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, }, AggregationBits: aggBits, } @@ -486,8 +487,8 @@ func TestConvertToIndexed_OK(t *testing.T) { attestation := ðpb.Attestation{ Signature: sig[:], Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, }, } for _, tt := range tests { @@ -512,8 +513,9 @@ func TestVerifyIndexedAttestation_OK(t *testing.T) { require.NoError(t, err) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - PublicKey: keys[i].PublicKey().Marshal(), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + PublicKey: keys[i].PublicKey().Marshal(), + WithdrawalCredentials: make([]byte, 32), } } @@ -535,33 +537,57 @@ func TestVerifyIndexedAttestation_OK(t *testing.T) { Data: ðpb.AttestationData{ Target: ðpb.Checkpoint{ Epoch: 2, + Root: make([]byte, 32), }, + Source: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: []uint64{1}, + Signature: make([]byte, 96), }}, {attestation: ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ Target: ðpb.Checkpoint{ Epoch: 1, + Root: make([]byte, 32), }, + Source: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: []uint64{47, 99, 101}, + Signature: make([]byte, 96), }}, {attestation: ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ Target: ðpb.Checkpoint{ Epoch: 4, + Root: make([]byte, 32), }, + Source: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: []uint64{21, 72}, + Signature: make([]byte, 96), }}, {attestation: ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ Target: ðpb.Checkpoint{ Epoch: 7, + Root: make([]byte, 32), }, + Source: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: []uint64{100, 121, 122}, + Signature: make([]byte, 96), }}, } @@ -653,32 +679,28 @@ func TestVerifyAttestations_VerifiesMultipleAttestations(t *testing.T) { require.NoError(t, err) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - PublicKey: keys[i].PublicKey().Marshal(), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + PublicKey: keys[i].PublicKey().Marshal(), + WithdrawalCredentials: make([]byte, 32), } } - st, err := stateTrie.InitializeFromProto(&pb.BeaconState{ - Slot: 5, - Validators: validators, - Fork: &pb.Fork{ - Epoch: 0, - CurrentVersion: params.BeaconConfig().GenesisForkVersion, - PreviousVersion: params.BeaconConfig().GenesisForkVersion, - }, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) + st := testutil.NewBeaconState() + require.NoError(t, st.SetSlot(5)) + require.NoError(t, st.SetValidators(validators)) comm1, err := helpers.BeaconCommitteeFromState(st, 1 /*slot*/, 0 /*committeeIndex*/) require.NoError(t, err) att1 := ðpb.Attestation{ AggregationBits: bitfield.NewBitlist(uint64(len(comm1))), Data: ðpb.AttestationData{ - Slot: 1, - CommitteeIndex: 0, - Target: new(ethpb.Checkpoint), + Slot: 1, + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, - Signature: nil, + Signature: make([]byte, 96), } domain, err := helpers.Domain(st.Fork(), st.Fork().Epoch, params.BeaconConfig().DomainBeaconAttester, st.GenesisValidatorRoot()) require.NoError(t, err) @@ -696,11 +718,13 @@ func TestVerifyAttestations_VerifiesMultipleAttestations(t *testing.T) { att2 := ðpb.Attestation{ AggregationBits: bitfield.NewBitlist(uint64(len(comm2))), Data: ðpb.AttestationData{ - Slot: 1, - CommitteeIndex: 1, - Target: new(ethpb.Checkpoint), + Slot: 1, + CommitteeIndex: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, - Signature: nil, + Signature: make([]byte, 96), } root, err = helpers.ComputeSigningRoot(att2.Data, domain) require.NoError(t, err) @@ -723,32 +747,33 @@ func TestVerifyAttestations_HandlesPlannedFork(t *testing.T) { require.NoError(t, err) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - PublicKey: keys[i].PublicKey().Marshal(), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + PublicKey: keys[i].PublicKey().Marshal(), + WithdrawalCredentials: make([]byte, 32), } } - st, err := stateTrie.InitializeFromProto(&pb.BeaconState{ - Slot: 35, - Validators: validators, - Fork: &pb.Fork{ - Epoch: 1, - CurrentVersion: []byte{0, 1, 2, 3}, - PreviousVersion: params.BeaconConfig().GenesisForkVersion, - }, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) + st := testutil.NewBeaconState() + require.NoError(t, st.SetSlot(35)) + require.NoError(t, st.SetValidators(validators)) + require.NoError(t, st.SetFork(&pb.Fork{ + Epoch: 1, + CurrentVersion: []byte{0, 1, 2, 3}, + PreviousVersion: params.BeaconConfig().GenesisForkVersion, + })) comm1, err := helpers.BeaconCommitteeFromState(st, 1 /*slot*/, 0 /*committeeIndex*/) require.NoError(t, err) att1 := ðpb.Attestation{ AggregationBits: bitfield.NewBitlist(uint64(len(comm1))), Data: ðpb.AttestationData{ - Slot: 1, - CommitteeIndex: 0, - Target: new(ethpb.Checkpoint), + Slot: 1, + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, - Signature: nil, + Signature: make([]byte, 96), } prevDomain, err := helpers.Domain(st.Fork(), st.Fork().Epoch-1, params.BeaconConfig().DomainBeaconAttester, st.GenesisValidatorRoot()) require.NoError(t, err) @@ -766,11 +791,13 @@ func TestVerifyAttestations_HandlesPlannedFork(t *testing.T) { att2 := ðpb.Attestation{ AggregationBits: bitfield.NewBitlist(uint64(len(comm2))), Data: ðpb.AttestationData{ - Slot: 1*params.BeaconConfig().SlotsPerEpoch + 1, - CommitteeIndex: 1, - Target: new(ethpb.Checkpoint), + Slot: 1*params.BeaconConfig().SlotsPerEpoch + 1, + CommitteeIndex: 1, + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, - Signature: nil, + Signature: make([]byte, 96), } currDomain, err := helpers.Domain(st.Fork(), st.Fork().Epoch, params.BeaconConfig().DomainBeaconAttester, st.GenesisValidatorRoot()) root, err = helpers.ComputeSigningRoot(att2.Data, currDomain) @@ -793,32 +820,28 @@ func TestRetrieveAttestationSignatureSet_VerifiesMultipleAttestations(t *testing require.NoError(t, err) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - PublicKey: keys[i].PublicKey().Marshal(), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + PublicKey: keys[i].PublicKey().Marshal(), + WithdrawalCredentials: make([]byte, 32), } } - st, err := stateTrie.InitializeFromProto(&pb.BeaconState{ - Slot: 5, - Validators: validators, - Fork: &pb.Fork{ - Epoch: 0, - CurrentVersion: params.BeaconConfig().GenesisForkVersion, - PreviousVersion: params.BeaconConfig().GenesisForkVersion, - }, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) + st := testutil.NewBeaconState() + require.NoError(t, st.SetSlot(5)) + require.NoError(t, st.SetValidators(validators)) comm1, err := helpers.BeaconCommitteeFromState(st, 1 /*slot*/, 0 /*committeeIndex*/) require.NoError(t, err) att1 := ðpb.Attestation{ AggregationBits: bitfield.NewBitlist(uint64(len(comm1))), Data: ðpb.AttestationData{ - Slot: 1, - CommitteeIndex: 0, - Target: new(ethpb.Checkpoint), + Slot: 1, + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, - Signature: nil, + Signature: make([]byte, 96), } domain, err := helpers.Domain(st.Fork(), st.Fork().Epoch, params.BeaconConfig().DomainBeaconAttester, st.GenesisValidatorRoot()) require.NoError(t, err) @@ -836,11 +859,13 @@ func TestRetrieveAttestationSignatureSet_VerifiesMultipleAttestations(t *testing att2 := ðpb.Attestation{ AggregationBits: bitfield.NewBitlist(uint64(len(comm2))), Data: ðpb.AttestationData{ - Slot: 1, - CommitteeIndex: 1, - Target: new(ethpb.Checkpoint), + Slot: 1, + CommitteeIndex: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, - Signature: nil, + Signature: make([]byte, 96), } root, err = helpers.ComputeSigningRoot(att2.Data, domain) require.NoError(t, err) diff --git a/beacon-chain/core/blocks/attester_slashing_test.go b/beacon-chain/core/blocks/attester_slashing_test.go index 649d2e232d..10aa52a53f 100644 --- a/beacon-chain/core/blocks/attester_slashing_test.go +++ b/beacon-chain/core/blocks/attester_slashing_test.go @@ -11,6 +11,7 @@ import ( stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bls" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -19,12 +20,14 @@ import ( func TestSlashableAttestationData_CanSlash(t *testing.T) { att1 := ðpb.AttestationData{ - Target: ðpb.Checkpoint{Epoch: 1}, - Source: ðpb.Checkpoint{Root: []byte{'A'}}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{'A'}, 32)}, + BeaconBlockRoot: make([]byte, 32), } att2 := ðpb.AttestationData{ - Target: ðpb.Checkpoint{Epoch: 1}, - Source: ðpb.Checkpoint{Root: []byte{'B'}}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{'B'}, 32)}, + BeaconBlockRoot: make([]byte, 32), } assert.Equal(t, true, blocks.IsSlashableAttestationData(att1, att2), "Atts should have been slashable") att1.Target.Epoch = 4 @@ -38,15 +41,19 @@ func TestProcessAttesterSlashings_DataNotSlashable(t *testing.T) { { Attestation_1: ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), }, Attestation_2: ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 1}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), }, }, } @@ -82,17 +89,21 @@ func TestProcessAttesterSlashings_IndexedAttestationFailedToVerify(t *testing.T) { Attestation_1: ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 1}, - Target: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: make([]uint64, params.BeaconConfig().MaxValidatorsPerCommittee+1), + Signature: make([]byte, 96), }, Attestation_2: ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: make([]uint64, params.BeaconConfig().MaxValidatorsPerCommittee+1), + Signature: make([]byte, 96), }, }, } @@ -116,8 +127,9 @@ func TestProcessAttesterSlashings_AppliesCorrectStatus(t *testing.T) { att1 := ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 1}, - Target: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: []uint64{0, 1}, } @@ -132,8 +144,9 @@ func TestProcessAttesterSlashings_AppliesCorrectStatus(t *testing.T) { att2 := ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: []uint64{0, 1}, } diff --git a/beacon-chain/core/blocks/block_regression_test.go b/beacon-chain/core/blocks/block_regression_test.go index 544a0d5eb4..0941444dc4 100644 --- a/beacon-chain/core/blocks/block_regression_test.go +++ b/beacon-chain/core/blocks/block_regression_test.go @@ -39,10 +39,12 @@ func TestProcessAttesterSlashings_RegressionSlashableIndices(t *testing.T) { root1 := [32]byte{'d', 'o', 'u', 'b', 'l', 'e', '1'} att1 := ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 0, Root: root1[:]}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: root1[:]}, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: setA, + Signature: make([]byte, 96), } domain, err := helpers.Domain(beaconState.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, beaconState.GenesisValidatorRoot()) require.NoError(t, err) @@ -59,10 +61,12 @@ func TestProcessAttesterSlashings_RegressionSlashableIndices(t *testing.T) { root2 := [32]byte{'d', 'o', 'u', 'b', 'l', 'e', '2'} att2 := ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 0, Root: root2[:]}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: root2[:]}, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: setB, + Signature: make([]byte, 96), } signingRoot, err = helpers.ComputeSigningRoot(att2.Data, domain) assert.NoError(t, err, "Could not get signing root of beacon block header") diff --git a/beacon-chain/core/blocks/deposit.go b/beacon-chain/core/blocks/deposit.go index 42e36f90f0..c59304bcba 100644 --- a/beacon-chain/core/blocks/deposit.go +++ b/beacon-chain/core/blocks/deposit.go @@ -197,7 +197,7 @@ func verifyDeposit(beaconState *stateTrie.BeaconState, deposit *ethpb.Deposit) e } receiptRoot := eth1Data.DepositRoot - leaf, err := ssz.HashTreeRoot(deposit.Data) + leaf, err := deposit.Data.HashTreeRoot() if err != nil { return errors.Wrap(err, "could not tree hash deposit data") } @@ -253,7 +253,7 @@ func verifyDepositDataWithDomain(ctx context.Context, deps []*ethpb.Deposit, dom ObjectRoot: root[:], Domain: domain, } - ctrRoot, err := ssz.HashTreeRoot(signingData) + ctrRoot, err := signingData.HashTreeRoot() if err != nil { return errors.Wrap(err, "could not get container root") } diff --git a/beacon-chain/core/blocks/deposit_test.go b/beacon-chain/core/blocks/deposit_test.go index d62f096ea5..9c6230f64b 100644 --- a/beacon-chain/core/blocks/deposit_test.go +++ b/beacon-chain/core/blocks/deposit_test.go @@ -5,7 +5,6 @@ import ( "testing" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" @@ -58,11 +57,12 @@ func TestProcessDeposits_SameValidatorMultipleDepositsSameBlock(t *testing.T) { func TestProcessDeposits_MerkleBranchFailsVerification(t *testing.T) { deposit := ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{1, 2, 3}, - Signature: make([]byte, 96), + PublicKey: bytesutil.PadTo([]byte{1, 2, 3}, 48), + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, } - leaf, err := ssz.HashTreeRoot(deposit.Data) + leaf, err := deposit.Data.HashTreeRoot() require.NoError(t, err) // We then create a merkle branch for the test. @@ -132,15 +132,17 @@ func TestProcessDeposits_RepeatedDeposit_IncreasesValidatorBalance(t *testing.T) sk := bls.RandKey() deposit := ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: sk.PublicKey().Marshal(), - Amount: 1000, + PublicKey: sk.PublicKey().Marshal(), + Amount: 1000, + WithdrawalCredentials: make([]byte, 32), + Signature: make([]byte, 96), }, } - sr, err := helpers.ComputeSigningRoot(deposit.Data, bytesutil.ToBytes(3, 8)) + sr, err := helpers.ComputeSigningRoot(deposit.Data, bytesutil.ToBytes(3, 32)) require.NoError(t, err) sig := sk.Sign(sr[:]) deposit.Data.Signature = sig.Marshal() - leaf, err := ssz.HashTreeRoot(deposit.Data) + leaf, err := deposit.Data.HashTreeRoot() require.NoError(t, err) // We then create a merkle branch for the test. diff --git a/beacon-chain/core/blocks/eth1_data_test.go b/beacon-chain/core/blocks/eth1_data_test.go index 9755675002..1872a43251 100644 --- a/beacon-chain/core/blocks/eth1_data_test.go +++ b/beacon-chain/core/blocks/eth1_data_test.go @@ -9,6 +9,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -19,7 +20,7 @@ func FakeDeposits(n uint64) []*ethpb.Eth1Data { for i := uint64(0); i < n; i++ { deposits[i] = ðpb.Eth1Data{ DepositCount: 1, - DepositRoot: []byte("root"), + DepositRoot: bytesutil.PadTo([]byte("root"), 32), } } return deposits @@ -36,7 +37,7 @@ func TestEth1DataHasEnoughSupport(t *testing.T) { stateVotes: FakeDeposits(4 * params.BeaconConfig().SlotsPerEpoch), data: ðpb.Eth1Data{ DepositCount: 1, - DepositRoot: []byte("root"), + DepositRoot: bytesutil.PadTo([]byte("root"), 32), }, hasSupport: true, votingPeriodLength: 7, @@ -44,7 +45,7 @@ func TestEth1DataHasEnoughSupport(t *testing.T) { stateVotes: FakeDeposits(4 * params.BeaconConfig().SlotsPerEpoch), data: ðpb.Eth1Data{ DepositCount: 1, - DepositRoot: []byte("root"), + DepositRoot: bytesutil.PadTo([]byte("root"), 32), }, hasSupport: false, votingPeriodLength: 8, @@ -52,7 +53,7 @@ func TestEth1DataHasEnoughSupport(t *testing.T) { stateVotes: FakeDeposits(4 * params.BeaconConfig().SlotsPerEpoch), data: ðpb.Eth1Data{ DepositCount: 1, - DepositRoot: []byte("root"), + DepositRoot: bytesutil.PadTo([]byte("root"), 32), }, hasSupport: false, votingPeriodLength: 10, diff --git a/beacon-chain/core/blocks/genesis.go b/beacon-chain/core/blocks/genesis.go index 8b31afca2a..5a64be26af 100644 --- a/beacon-chain/core/blocks/genesis.go +++ b/beacon-chain/core/blocks/genesis.go @@ -10,13 +10,20 @@ import ( // NewGenesisBlock returns the canonical, genesis block for the beacon chain protocol. func NewGenesisBlock(stateRoot []byte) *ethpb.SignedBeaconBlock { zeroHash := params.BeaconConfig().ZeroHash[:] - genBlock := ðpb.BeaconBlock{ - ParentRoot: zeroHash, - StateRoot: stateRoot, - Body: ðpb.BeaconBlockBody{}, - } - return ðpb.SignedBeaconBlock{ - Block: genBlock, + block := ðpb.SignedBeaconBlock{ + Block: ðpb.BeaconBlock{ + ParentRoot: zeroHash, + StateRoot: stateRoot, + Body: ðpb.BeaconBlockBody{ + RandaoReveal: make([]byte, 96), + Eth1Data: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + BlockHash: make([]byte, 32), + }, + Graffiti: make([]byte, 32), + }, + }, Signature: params.BeaconConfig().EmptySignature[:], } + return block } diff --git a/beacon-chain/core/blocks/header_test.go b/beacon-chain/core/blocks/header_test.go index be3b6f5555..0668bd1de1 100644 --- a/beacon-chain/core/blocks/header_test.go +++ b/beacon-chain/core/blocks/header_test.go @@ -6,13 +6,13 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bls" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -28,22 +28,22 @@ func TestProcessBlockHeader_ImproperBlockSlot(t *testing.T) { validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - Slashed: true, + PublicKey: make([]byte, 32), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + Slashed: true, } } - state, err := stateTrie.InitializeFromProto(&pb.BeaconState{ - Validators: validators, - Slot: 10, - LatestBlockHeader: ðpb.BeaconBlockHeader{Slot: 10}, // Must be less than block.Slot - Fork: &pb.Fork{ - PreviousVersion: []byte{0, 0, 0, 0}, - CurrentVersion: []byte{0, 0, 0, 0}, - }, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) - require.NoError(t, err) + state := testutil.NewBeaconState() + require.NoError(t, state.SetSlot(10)) + require.NoError(t, state.SetValidators(validators)) + require.NoError(t, state.SetLatestBlockHeader(ðpb.BeaconBlockHeader{ + Slot: 10, // Must be less than block.Slot + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + })) latestBlockSignedRoot, err := stateutil.BlockHeaderRoot(state.LatestBlockHeader()) require.NoError(t, err) @@ -52,16 +52,11 @@ func TestProcessBlockHeader_ImproperBlockSlot(t *testing.T) { priv := bls.RandKey() pID, err := helpers.BeaconProposerIndex(state) require.NoError(t, err) - block := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: pID, - Slot: 10, - Body: ðpb.BeaconBlockBody{ - RandaoReveal: []byte{'A', 'B', 'C'}, - }, - ParentRoot: latestBlockSignedRoot[:], - }, - } + block := testutil.NewBeaconBlock() + block.Block.ProposerIndex = pID + block.Block.Slot = 10 + block.Block.Body.RandaoReveal = bytesutil.PadTo([]byte{'A', 'B', 'C'}, 96) + block.Block.ParentRoot = latestBlockSignedRoot[:] block.Signature, err = helpers.ComputeDomainAndSign(state, currentEpoch, block.Block, params.BeaconConfig().DomainBeaconProposer, priv) require.NoError(t, err) @@ -79,7 +74,12 @@ func TestProcessBlockHeader_ImproperBlockSlot(t *testing.T) { func TestProcessBlockHeader_WrongProposerSig(t *testing.T) { testutil.ResetCache() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) - require.NoError(t, beaconState.SetLatestBlockHeader(ðpb.BeaconBlockHeader{Slot: 9})) + require.NoError(t, beaconState.SetLatestBlockHeader(ðpb.BeaconBlockHeader{ + Slot: 9, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + })) require.NoError(t, beaconState.SetSlot(10)) lbhdr, err := stateutil.BlockHeaderRoot(beaconState.LatestBlockHeader()) @@ -88,16 +88,11 @@ func TestProcessBlockHeader_WrongProposerSig(t *testing.T) { proposerIdx, err := helpers.BeaconProposerIndex(beaconState) require.NoError(t, err) - block := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: proposerIdx, - Slot: 10, - Body: ðpb.BeaconBlockBody{ - RandaoReveal: []byte{'A', 'B', 'C'}, - }, - ParentRoot: lbhdr[:], - }, - } + block := testutil.NewBeaconBlock() + block.Block.ProposerIndex = proposerIdx + block.Block.Slot = 10 + block.Block.Body.RandaoReveal = bytesutil.PadTo([]byte{'A', 'B', 'C'}, 96) + block.Block.ParentRoot = lbhdr[:] block.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, block.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx+1]) require.NoError(t, err) @@ -110,24 +105,25 @@ func TestProcessBlockHeader_DifferentSlots(t *testing.T) { validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - Slashed: true, + PublicKey: make([]byte, 32), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + Slashed: true, } } - state, err := stateTrie.InitializeFromProto(&pb.BeaconState{ - Validators: validators, - Slot: 10, - LatestBlockHeader: ðpb.BeaconBlockHeader{Slot: 9}, - Fork: &pb.Fork{ - PreviousVersion: []byte{0, 0, 0, 0}, - CurrentVersion: []byte{0, 0, 0, 0}, - }, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) - require.NoError(t, err) + state := testutil.NewBeaconState() + require.NoError(t, state.SetValidators(validators)) + require.NoError(t, state.SetSlot(10)) + require.NoError(t, state.SetLatestBlockHeader(ðpb.BeaconBlockHeader{ + Slot: 9, + ProposerIndex: 0, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + })) - lbhsr, err := ssz.HashTreeRoot(state.LatestBlockHeader()) + lbhsr, err := state.LatestBlockHeader().HashTreeRoot() require.NoError(t, err) currentEpoch := helpers.CurrentEpoch(state) @@ -155,8 +151,10 @@ func TestProcessBlockHeader_PreviousBlockRootNotSignedRoot(t *testing.T) { validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - Slashed: true, + PublicKey: make([]byte, 32), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + Slashed: true, } } @@ -200,8 +198,10 @@ func TestProcessBlockHeader_SlashedProposer(t *testing.T) { validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - Slashed: true, + PublicKey: make([]byte, 32), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + Slashed: true, } } @@ -248,22 +248,23 @@ func TestProcessBlockHeader_OK(t *testing.T) { validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - Slashed: true, + PublicKey: make([]byte, 32), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + Slashed: true, } } - state, err := stateTrie.InitializeFromProto(&pb.BeaconState{ - Validators: validators, - Slot: 10, - LatestBlockHeader: ðpb.BeaconBlockHeader{Slot: 9}, - Fork: &pb.Fork{ - PreviousVersion: []byte{0, 0, 0, 0}, - CurrentVersion: []byte{0, 0, 0, 0}, - }, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) - require.NoError(t, err) + state := testutil.NewBeaconState() + require.NoError(t, state.SetValidators(validators)) + require.NoError(t, state.SetSlot(10)) + require.NoError(t, state.SetLatestBlockHeader(ðpb.BeaconBlockHeader{ + Slot: 9, + ProposerIndex: 0, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + })) latestBlockSignedRoot, err := stateutil.BlockHeaderRoot(state.LatestBlockHeader()) require.NoError(t, err) @@ -272,16 +273,11 @@ func TestProcessBlockHeader_OK(t *testing.T) { priv := bls.RandKey() pID, err := helpers.BeaconProposerIndex(state) require.NoError(t, err) - block := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: pID, - Slot: 10, - Body: ðpb.BeaconBlockBody{ - RandaoReveal: []byte{'A', 'B', 'C'}, - }, - ParentRoot: latestBlockSignedRoot[:], - }, - } + block := testutil.NewBeaconBlock() + block.Block.ProposerIndex = pID + block.Block.Slot = 10 + block.Block.Body.RandaoReveal = bytesutil.PadTo([]byte{'A', 'B', 'C'}, 96) + block.Block.ParentRoot = latestBlockSignedRoot[:] block.Signature, err = helpers.ComputeDomainAndSign(state, currentEpoch, block.Block, params.BeaconConfig().DomainBeaconProposer, priv) require.NoError(t, err) bodyRoot, err := stateutil.BlockBodyRoot(block.Block.Body) @@ -312,22 +308,23 @@ func TestBlockSignatureSet_OK(t *testing.T) { validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - Slashed: true, + PublicKey: make([]byte, 32), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + Slashed: true, } } - state, err := stateTrie.InitializeFromProto(&pb.BeaconState{ - Validators: validators, - Slot: 10, - LatestBlockHeader: ðpb.BeaconBlockHeader{Slot: 9}, - Fork: &pb.Fork{ - PreviousVersion: []byte{0, 0, 0, 0}, - CurrentVersion: []byte{0, 0, 0, 0}, - }, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) - require.NoError(t, err) + state := testutil.NewBeaconState() + require.NoError(t, state.SetValidators(validators)) + require.NoError(t, state.SetSlot(10)) + require.NoError(t, state.SetLatestBlockHeader(ðpb.BeaconBlockHeader{ + Slot: 9, + ProposerIndex: 0, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + })) latestBlockSignedRoot, err := stateutil.BlockHeaderRoot(state.LatestBlockHeader()) require.NoError(t, err) @@ -336,16 +333,11 @@ func TestBlockSignatureSet_OK(t *testing.T) { priv := bls.RandKey() pID, err := helpers.BeaconProposerIndex(state) require.NoError(t, err) - block := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: pID, - Slot: 10, - Body: ðpb.BeaconBlockBody{ - RandaoReveal: []byte{'A', 'B', 'C'}, - }, - ParentRoot: latestBlockSignedRoot[:], - }, - } + block := testutil.NewBeaconBlock() + block.Block.Slot = 10 + block.Block.ProposerIndex = pID + block.Block.Body.RandaoReveal = bytesutil.PadTo([]byte{'A', 'B', 'C'}, 96) + block.Block.ParentRoot = latestBlockSignedRoot[:] block.Signature, err = helpers.ComputeDomainAndSign(state, currentEpoch, block.Block, params.BeaconConfig().DomainBeaconProposer, priv) require.NoError(t, err) proposerIdx, err := helpers.BeaconProposerIndex(state) diff --git a/beacon-chain/core/blocks/proposer_slashing_test.go b/beacon-chain/core/blocks/proposer_slashing_test.go index c16a32337c..0a2b8ac00e 100644 --- a/beacon-chain/core/blocks/proposer_slashing_test.go +++ b/beacon-chain/core/blocks/proposer_slashing_test.go @@ -98,14 +98,14 @@ func TestProcessProposerSlashings_ValidatorNotSlashable(t *testing.T) { ProposerIndex: 0, Slot: 0, }, - Signature: []byte("A"), + Signature: bytesutil.PadTo([]byte("A"), 96), }, Header_2: ðpb.SignedBeaconBlockHeader{ Header: ðpb.BeaconBlockHeader{ ProposerIndex: 0, Slot: 0, }, - Signature: []byte("B"), + Signature: bytesutil.PadTo([]byte("B"), 96), }, }, } @@ -138,7 +138,9 @@ func TestProcessProposerSlashings_AppliesCorrectStatus(t *testing.T) { Header: ðpb.BeaconBlockHeader{ ProposerIndex: proposerIdx, Slot: 0, - StateRoot: []byte("A"), + ParentRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + StateRoot: bytesutil.PadTo([]byte("A"), 32), }, } var err error @@ -149,7 +151,9 @@ func TestProcessProposerSlashings_AppliesCorrectStatus(t *testing.T) { Header: ðpb.BeaconBlockHeader{ ProposerIndex: proposerIdx, Slot: 0, - StateRoot: []byte("B"), + ParentRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + StateRoot: bytesutil.PadTo([]byte("B"), 32), }, } header2.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, header2.Header, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) @@ -162,13 +166,10 @@ func TestProcessProposerSlashings_AppliesCorrectStatus(t *testing.T) { }, } - block := ðpb.BeaconBlock{ - Body: ðpb.BeaconBlockBody{ - ProposerSlashings: slashings, - }, - } + block := testutil.NewBeaconBlock() + block.Block.Body.ProposerSlashings = slashings - newState, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, block.Body) + newState, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, block.Block.Body) require.NoError(t, err) newStateVals := newState.Validators() diff --git a/beacon-chain/core/blocks/randao_test.go b/beacon-chain/core/blocks/randao_test.go index 50c684fb06..311388e22d 100644 --- a/beacon-chain/core/blocks/randao_test.go +++ b/beacon-chain/core/blocks/randao_test.go @@ -6,7 +6,6 @@ import ( "testing" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -26,7 +25,7 @@ func TestProcessRandao_IncorrectProposerFailsVerification(t *testing.T) { binary.LittleEndian.PutUint64(buf, epoch) domain, err := helpers.Domain(beaconState.Fork(), epoch, params.BeaconConfig().DomainRandao, beaconState.GenesisValidatorRoot()) require.NoError(t, err) - root, err := ssz.HashTreeRoot(&pb.SigningData{ObjectRoot: buf, Domain: domain}) + root, err := (&pb.SigningData{ObjectRoot: buf, Domain: domain}).HashTreeRoot() require.NoError(t, err) // We make the previous validator's index sign the message instead of the proposer. epochSignature := privKeys[proposerIdx-1].Sign(root[:]) diff --git a/beacon-chain/core/blocks/signature.go b/beacon-chain/core/blocks/signature.go index 8e48bbe9b5..db8a3d43b2 100644 --- a/beacon-chain/core/blocks/signature.go +++ b/beacon-chain/core/blocks/signature.go @@ -6,7 +6,6 @@ import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -29,7 +28,7 @@ func retrieveSignatureSet(signedData []byte, pub []byte, signature []byte, domai ObjectRoot: signedData, Domain: domain, } - root, err := ssz.HashTreeRoot(signingData) + root, err := signingData.HashTreeRoot() if err != nil { return nil, errors.Wrap(err, "could not hash container") } diff --git a/beacon-chain/core/epoch/BUILD.bazel b/beacon-chain/core/epoch/BUILD.bazel index e5f87bf768..3399946abe 100644 --- a/beacon-chain/core/epoch/BUILD.bazel +++ b/beacon-chain/core/epoch/BUILD.bazel @@ -16,7 +16,6 @@ go_library( "//shared/params:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", ], ) @@ -33,6 +32,7 @@ go_test( "//beacon-chain/state:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/params:go_default_library", + "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", "//shared/testutil/require:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", diff --git a/beacon-chain/core/epoch/epoch_processing.go b/beacon-chain/core/epoch/epoch_processing.go index 7207a5d315..4444406e13 100644 --- a/beacon-chain/core/epoch/epoch_processing.go +++ b/beacon-chain/core/epoch/epoch_processing.go @@ -10,7 +10,6 @@ import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" @@ -46,7 +45,7 @@ func (s sortableIndices) Less(i, j int) bool { // def get_attesting_balance(state: BeaconState, attestations: List[PendingAttestation]) -> Gwei: // return get_total_balance(state, get_unslashed_attesting_indices(state, attestations)) func AttestingBalance(state *stateTrie.BeaconState, atts []*pb.PendingAttestation) (uint64, error) { - indices, err := unslashedAttestingIndices(state, atts) + indices, err := UnslashedAttestingIndices(state, atts) if err != nil { return 0, errors.Wrap(err, "could not get attesting indices") } @@ -311,7 +310,7 @@ func ProcessFinalUpdates(state *stateTrie.BeaconState) (*stateTrie.BeaconState, BlockRoots: state.BlockRoots(), StateRoots: state.StateRoots(), } - batchRoot, err := ssz.HashTreeRoot(historicalBatch) + batchRoot, err := historicalBatch.HashTreeRoot() if err != nil { return nil, errors.Wrap(err, "could not hash historical batch") } @@ -330,7 +329,7 @@ func ProcessFinalUpdates(state *stateTrie.BeaconState) (*stateTrie.BeaconState, return state, nil } -// unslashedAttestingIndices returns all the attesting indices from a list of attestations, +// UnslashedAttestingIndices returns all the attesting indices from a list of attestations, // it sorts the indices and filters out the slashed ones. // // Spec pseudocode definition: @@ -340,7 +339,7 @@ func ProcessFinalUpdates(state *stateTrie.BeaconState) (*stateTrie.BeaconState, // for a in attestations: // output = output.union(get_attesting_indices(state, a.data, a.aggregation_bits)) // return set(filter(lambda index: not state.validators[index].slashed, output)) -func unslashedAttestingIndices(state *stateTrie.BeaconState, atts []*pb.PendingAttestation) ([]uint64, error) { +func UnslashedAttestingIndices(state *stateTrie.BeaconState, atts []*pb.PendingAttestation) ([]uint64, error) { var setIndices []uint64 seen := make(map[uint64]bool) diff --git a/beacon-chain/core/epoch/epoch_processing_test.go b/beacon-chain/core/epoch/epoch_processing_test.go index 7ec2fec1b2..003641c170 100644 --- a/beacon-chain/core/epoch/epoch_processing_test.go +++ b/beacon-chain/core/epoch/epoch_processing_test.go @@ -1,4 +1,4 @@ -package epoch +package epoch_test import ( "bytes" @@ -7,10 +7,12 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" + "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) @@ -20,8 +22,8 @@ func TestUnslashedAttestingIndices_CanSortAndFilter(t *testing.T) { atts := make([]*pb.PendingAttestation, 2) for i := 0; i < len(atts); i++ { atts[i] = &pb.PendingAttestation{ - Data: ðpb.AttestationData{Source: ðpb.Checkpoint{}, - Target: ðpb.Checkpoint{Epoch: 0}, + Data: ðpb.AttestationData{Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0xFF, 0xFF, 0xFF}, } @@ -42,7 +44,7 @@ func TestUnslashedAttestingIndices_CanSortAndFilter(t *testing.T) { state, err := state.InitializeFromProto(base) require.NoError(t, err) - indices, err := unslashedAttestingIndices(state, atts) + indices, err := epoch.UnslashedAttestingIndices(state, atts) require.NoError(t, err) for i := 0; i < len(indices)-1; i++ { if indices[i] >= indices[i+1] { @@ -55,7 +57,7 @@ func TestUnslashedAttestingIndices_CanSortAndFilter(t *testing.T) { validators = state.Validators() validators[slashedValidator].Slashed = true require.NoError(t, state.SetValidators(validators)) - indices, err = unslashedAttestingIndices(state, atts) + indices, err = epoch.UnslashedAttestingIndices(state, atts) require.NoError(t, err) for i := 0; i < len(indices); i++ { assert.NotEqual(t, slashedValidator, indices[i], "Slashed validator %d is not filtered", slashedValidator) @@ -67,7 +69,7 @@ func TestUnslashedAttestingIndices_DuplicatedAttestations(t *testing.T) { atts := make([]*pb.PendingAttestation, 5) for i := 0; i < len(atts); i++ { atts[i] = &pb.PendingAttestation{ - Data: ðpb.AttestationData{Source: ðpb.Checkpoint{}, + Data: ðpb.AttestationData{Source: ðpb.Checkpoint{Root: make([]byte, 32)}, Target: ðpb.Checkpoint{Epoch: 0}}, AggregationBits: bitfield.Bitlist{0xFF, 0xFF, 0xFF}, } @@ -88,7 +90,7 @@ func TestUnslashedAttestingIndices_DuplicatedAttestations(t *testing.T) { state, err := state.InitializeFromProto(base) require.NoError(t, err) - indices, err := unslashedAttestingIndices(state, atts) + indices, err := epoch.UnslashedAttestingIndices(state, atts) require.NoError(t, err) for i := 0; i < len(indices)-1; i++ { @@ -105,8 +107,8 @@ func TestAttestingBalance_CorrectBalance(t *testing.T) { for i := 0; i < len(atts); i++ { atts[i] = &pb.PendingAttestation{ Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{}, - Source: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, Slot: uint64(i), }, AggregationBits: bitfield.Bitlist{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, @@ -134,7 +136,7 @@ func TestAttestingBalance_CorrectBalance(t *testing.T) { state, err := state.InitializeFromProto(base) require.NoError(t, err) - balance, err := AttestingBalance(state, atts) + balance, err := epoch.AttestingBalance(state, atts) require.NoError(t, err) wanted := 256 * params.BeaconConfig().MaxEffectiveBalance assert.Equal(t, wanted, balance) @@ -159,9 +161,9 @@ func TestBaseReward_AccurateRewards(t *testing.T) { } state, err := state.InitializeFromProto(base) require.NoError(t, err) - c, err := BaseReward(state, 0) + c, err := epoch.BaseReward(state, 0) require.NoError(t, err) - assert.Equal(t, tt.c, c, "BaseReward(%d)", tt.a) + assert.Equal(t, tt.c, c, "epoch.BaseReward(%d)", tt.a) } } @@ -174,7 +176,7 @@ func TestProcessSlashings_NotSlashed(t *testing.T) { } s, err := state.InitializeFromProto(base) require.NoError(t, err) - newState, err := ProcessSlashings(s) + newState, err := epoch.ProcessSlashings(s) require.NoError(t, err) wanted := params.BeaconConfig().MaxEffectiveBalance assert.Equal(t, wanted, newState.Balances()[0], "Unexpected slashed balance") @@ -252,7 +254,7 @@ func TestProcessSlashings_SlashedLess(t *testing.T) { original := proto.Clone(tt.state) s, err := state.InitializeFromProto(tt.state) require.NoError(t, err) - newState, err := ProcessSlashings(s) + newState, err := epoch.ProcessSlashings(s) require.NoError(t, err) assert.Equal(t, tt.want, newState.Balances()[0], "ProcessSlashings({%v}) = newState; newState.Balances[0] = %d", original, newState.Balances()[0]) }) @@ -260,7 +262,7 @@ func TestProcessSlashings_SlashedLess(t *testing.T) { } func TestProcessFinalUpdates_CanProcess(t *testing.T) { - s := buildState(params.BeaconConfig().SlotsPerHistoricalRoot-1, params.BeaconConfig().SlotsPerEpoch) + s := buildState(t, params.BeaconConfig().SlotsPerHistoricalRoot-1, params.BeaconConfig().SlotsPerEpoch) ce := helpers.CurrentEpoch(s) ne := ce + 1 require.NoError(t, s.SetEth1DataVotes([]*ethpb.Eth1Data{})) @@ -275,7 +277,7 @@ func TestProcessFinalUpdates_CanProcess(t *testing.T) { mixes := s.RandaoMixes() mixes[ce] = []byte{'A'} require.NoError(t, s.SetRandaoMixes(mixes)) - newS, err := ProcessFinalUpdates(s) + newS, err := epoch.ProcessFinalUpdates(s) require.NoError(t, err) // Verify effective balance is correctly updated. @@ -306,11 +308,11 @@ func TestProcessRegistryUpdates_NoRotation(t *testing.T) { params.BeaconConfig().MaxEffectiveBalance, params.BeaconConfig().MaxEffectiveBalance, }, - FinalizedCheckpoint: ðpb.Checkpoint{}, + FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, } state, err := state.InitializeFromProto(base) require.NoError(t, err) - newState, err := ProcessRegistryUpdates(state) + newState, err := epoch.ProcessRegistryUpdates(state) require.NoError(t, err) for i, validator := range newState.Validators() { assert.Equal(t, params.BeaconConfig().MaxSeedLookahead, validator.ExitEpoch, "Could not update registry %d", i) @@ -320,7 +322,7 @@ func TestProcessRegistryUpdates_NoRotation(t *testing.T) { func TestProcessRegistryUpdates_EligibleToActivate(t *testing.T) { base := &pb.BeaconState{ Slot: 5 * params.BeaconConfig().SlotsPerEpoch, - FinalizedCheckpoint: ðpb.Checkpoint{Epoch: 6}, + FinalizedCheckpoint: ðpb.Checkpoint{Epoch: 6, Root: make([]byte, 32)}, } limit, err := helpers.ValidatorChurnLimit(0) require.NoError(t, err) @@ -333,7 +335,7 @@ func TestProcessRegistryUpdates_EligibleToActivate(t *testing.T) { } state, err := state.InitializeFromProto(base) currentEpoch := helpers.CurrentEpoch(state) - newState, err := ProcessRegistryUpdates(state) + newState, err := epoch.ProcessRegistryUpdates(state) require.NoError(t, err) for i, validator := range newState.Validators() { assert.Equal(t, currentEpoch+1, validator.ActivationEligibilityEpoch, "Could not update registry %d, unexpected activation eligibility epoch", i) @@ -357,11 +359,11 @@ func TestProcessRegistryUpdates_ActivationCompletes(t *testing.T) { {ExitEpoch: params.BeaconConfig().MaxSeedLookahead, ActivationEpoch: 5 + params.BeaconConfig().MaxSeedLookahead + 1}, }, - FinalizedCheckpoint: ðpb.Checkpoint{}, + FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, } state, err := state.InitializeFromProto(base) require.NoError(t, err) - newState, err := ProcessRegistryUpdates(state) + newState, err := epoch.ProcessRegistryUpdates(state) require.NoError(t, err) for i, validator := range newState.Validators() { assert.Equal(t, params.BeaconConfig().MaxSeedLookahead, validator.ExitEpoch, "Could not update registry %d, unexpected exit slot", i) @@ -381,11 +383,11 @@ func TestProcessRegistryUpdates_ValidatorsEjected(t *testing.T) { EffectiveBalance: params.BeaconConfig().EjectionBalance - 1, }, }, - FinalizedCheckpoint: ðpb.Checkpoint{}, + FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, } state, err := state.InitializeFromProto(base) require.NoError(t, err) - newState, err := ProcessRegistryUpdates(state) + newState, err := epoch.ProcessRegistryUpdates(state) require.NoError(t, err) for i, validator := range newState.Validators() { assert.Equal(t, params.BeaconConfig().MaxSeedLookahead+1, validator.ExitEpoch, "Could not update registry %d, unexpected exit slot", i) @@ -393,11 +395,11 @@ func TestProcessRegistryUpdates_ValidatorsEjected(t *testing.T) { } func TestProcessRegistryUpdates_CanExits(t *testing.T) { - epoch := uint64(5) - exitEpoch := helpers.ActivationExitEpoch(epoch) + e := uint64(5) + exitEpoch := helpers.ActivationExitEpoch(e) minWithdrawalDelay := params.BeaconConfig().MinValidatorWithdrawabilityDelay base := &pb.BeaconState{ - Slot: epoch * params.BeaconConfig().SlotsPerEpoch, + Slot: e * params.BeaconConfig().SlotsPerEpoch, Validators: []*ethpb.Validator{ { ExitEpoch: exitEpoch, @@ -406,18 +408,18 @@ func TestProcessRegistryUpdates_CanExits(t *testing.T) { ExitEpoch: exitEpoch, WithdrawableEpoch: exitEpoch + minWithdrawalDelay}, }, - FinalizedCheckpoint: ðpb.Checkpoint{}, + FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, } state, err := state.InitializeFromProto(base) require.NoError(t, err) - newState, err := ProcessRegistryUpdates(state) + newState, err := epoch.ProcessRegistryUpdates(state) require.NoError(t, err) for i, validator := range newState.Validators() { assert.Equal(t, exitEpoch, validator.ExitEpoch, "Could not update registry %d, unexpected exit slot", i) } } -func buildState(slot uint64, validatorCount uint64) *state.BeaconState { +func buildState(t testing.TB, slot uint64, validatorCount uint64) *state.BeaconState { validators := make([]*ethpb.Validator, validatorCount) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ @@ -443,19 +445,15 @@ func buildState(slot uint64, validatorCount uint64) *state.BeaconState { for i := 0; i < len(latestRandaoMixes); i++ { latestRandaoMixes[i] = params.BeaconConfig().ZeroHash[:] } - s, err := state.InitializeFromProto(&pb.BeaconState{ - Slot: slot, - Balances: validatorBalances, - Validators: validators, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector), - BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerEpoch*10), - FinalizedCheckpoint: ðpb.Checkpoint{}, - PreviousJustifiedCheckpoint: ðpb.Checkpoint{}, - CurrentJustifiedCheckpoint: ðpb.Checkpoint{}, - }) - if err != nil { - panic(err) + s := testutil.NewBeaconState() + if err := s.SetSlot(slot); err != nil { + t.Error(err) + } + if err := s.SetBalances(validatorBalances); err != nil { + t.Error(err) + } + if err := s.SetValidators(validators); err != nil { + t.Error(err) } return s } diff --git a/beacon-chain/core/epoch/precompute/justification_finalization_test.go b/beacon-chain/core/epoch/precompute/justification_finalization_test.go index c05cf1f986..8e02e323d6 100644 --- a/beacon-chain/core/epoch/precompute/justification_finalization_test.go +++ b/beacon-chain/core/epoch/precompute/justification_finalization_test.go @@ -30,7 +30,7 @@ func TestProcessJustificationAndFinalizationPreCompute_ConsecutiveEpochs(t *test Epoch: 0, Root: params.BeaconConfig().ZeroHash[:], }, - FinalizedCheckpoint: ðpb.Checkpoint{}, + FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, JustificationBits: bitfield.Bitvector4{0x0F}, // 0b1111 Validators: []*ethpb.Validator{{ExitEpoch: e}, {ExitEpoch: e}, {ExitEpoch: e}, {ExitEpoch: e}}, Balances: []uint64{a, a, a, a}, // validator total balance should be 128000000000 @@ -67,7 +67,7 @@ func TestProcessJustificationAndFinalizationPreCompute_JustifyCurrentEpoch(t *te Epoch: 0, Root: params.BeaconConfig().ZeroHash[:], }, - FinalizedCheckpoint: ðpb.Checkpoint{}, + FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, JustificationBits: bitfield.Bitvector4{0x03}, // 0b0011 Validators: []*ethpb.Validator{{ExitEpoch: e}, {ExitEpoch: e}, {ExitEpoch: e}, {ExitEpoch: e}}, Balances: []uint64{a, a, a, a}, // validator total balance should be 128000000000 @@ -107,7 +107,7 @@ func TestProcessJustificationAndFinalizationPreCompute_JustifyPrevEpoch(t *testi JustificationBits: bitfield.Bitvector4{0x03}, // 0b0011 Validators: []*ethpb.Validator{{ExitEpoch: e}, {ExitEpoch: e}, {ExitEpoch: e}, {ExitEpoch: e}}, Balances: []uint64{a, a, a, a}, // validator total balance should be 128000000000 - BlockRoots: blockRoots, FinalizedCheckpoint: ðpb.Checkpoint{}, + BlockRoots: blockRoots, FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, } state, err := beaconstate.InitializeFromProto(base) require.NoError(t, err) diff --git a/beacon-chain/core/epoch/precompute/reward_penalty_test.go b/beacon-chain/core/epoch/precompute/reward_penalty_test.go index 828932be6d..3f615d54e9 100644 --- a/beacon-chain/core/epoch/precompute/reward_penalty_test.go +++ b/beacon-chain/core/epoch/precompute/reward_penalty_test.go @@ -24,8 +24,8 @@ func TestProcessRewardsAndPenaltiesPrecompute(t *testing.T) { for i := 0; i < len(atts); i++ { atts[i] = &pb.PendingAttestation{ Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{}, - Source: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0xC0, 0xC0, 0xC0, 0xC0, 0x01}, InclusionDelay: 1, @@ -214,8 +214,8 @@ func TestProcessRewardsAndPenaltiesPrecompute_SlashedInactivePenalty(t *testing. for i := 0; i < len(atts); i++ { atts[i] = &pb.PendingAttestation{ Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{}, - Source: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0xC0, 0xC0, 0xC0, 0xC0, 0x01}, InclusionDelay: 1, @@ -287,9 +287,9 @@ func buildState(slot uint64, validatorCount uint64) *pb.BeaconState { RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector), BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerEpoch*10), - FinalizedCheckpoint: ðpb.Checkpoint{}, - PreviousJustifiedCheckpoint: ðpb.Checkpoint{}, - CurrentJustifiedCheckpoint: ðpb.Checkpoint{}, + FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, + PreviousJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, + CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, } } diff --git a/beacon-chain/core/feed/state/events.go b/beacon-chain/core/feed/state/events.go index 06021369f5..8a18b74f78 100644 --- a/beacon-chain/core/feed/state/events.go +++ b/beacon-chain/core/feed/state/events.go @@ -45,7 +45,7 @@ type SyncedData struct { type InitializedData struct { // StartTime is the time at which the chain started. StartTime time.Time - // GenesisValidatorsRoot represents ssz.HashTreeRoot(state.validators). + // GenesisValidatorsRoot represents state.validators.HashTreeRoot(). GenesisValidatorsRoot []byte } diff --git a/beacon-chain/core/helpers/BUILD.bazel b/beacon-chain/core/helpers/BUILD.bazel index de52ace98f..67812ec3ea 100644 --- a/beacon-chain/core/helpers/BUILD.bazel +++ b/beacon-chain/core/helpers/BUILD.bazel @@ -40,6 +40,7 @@ go_library( "//shared/params:go_default_library", "//shared/roughtime:go_default_library", "//shared/sliceutil:go_default_library", + "@com_github_ferranbt_fastssz//:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", @@ -66,7 +67,6 @@ go_test( deps = [ "//beacon-chain/cache:go_default_library", "//beacon-chain/state:go_default_library", - "//beacon-chain/state/stateutil:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", diff --git a/beacon-chain/core/helpers/committee_test.go b/beacon-chain/core/helpers/committee_test.go index 6b005e5d3c..211135e05b 100644 --- a/beacon-chain/core/helpers/committee_test.go +++ b/beacon-chain/core/helpers/committee_test.go @@ -263,7 +263,7 @@ func TestVerifyAttestationBitfieldLengths_OK(t *testing.T) { AggregationBits: bitfield.Bitlist{0x05}, Data: ðpb.AttestationData{ CommitteeIndex: 5, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, stateSlot: 5, @@ -274,7 +274,7 @@ func TestVerifyAttestationBitfieldLengths_OK(t *testing.T) { AggregationBits: bitfield.Bitlist{0x06}, Data: ðpb.AttestationData{ CommitteeIndex: 10, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, stateSlot: 10, @@ -284,7 +284,7 @@ func TestVerifyAttestationBitfieldLengths_OK(t *testing.T) { AggregationBits: bitfield.Bitlist{0x06}, Data: ðpb.AttestationData{ CommitteeIndex: 20, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, stateSlot: 20, @@ -294,7 +294,7 @@ func TestVerifyAttestationBitfieldLengths_OK(t *testing.T) { AggregationBits: bitfield.Bitlist{0x06}, Data: ðpb.AttestationData{ CommitteeIndex: 20, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, stateSlot: 20, @@ -304,7 +304,7 @@ func TestVerifyAttestationBitfieldLengths_OK(t *testing.T) { AggregationBits: bitfield.Bitlist{0xFF, 0xC0, 0x01}, Data: ðpb.AttestationData{ CommitteeIndex: 5, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, stateSlot: 5, @@ -315,7 +315,7 @@ func TestVerifyAttestationBitfieldLengths_OK(t *testing.T) { AggregationBits: bitfield.Bitlist{0xFF, 0x01}, Data: ðpb.AttestationData{ CommitteeIndex: 20, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, stateSlot: 20, diff --git a/beacon-chain/core/helpers/signing_root.go b/beacon-chain/core/helpers/signing_root.go index 655ee76fff..dbfdf49ef0 100644 --- a/beacon-chain/core/helpers/signing_root.go +++ b/beacon-chain/core/helpers/signing_root.go @@ -1,6 +1,7 @@ package helpers import ( + fssz "github.com/ferranbt/fastssz" "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" @@ -48,16 +49,14 @@ func ComputeDomainAndSign(state *state.BeaconState, epoch uint64, obj interface{ // domain=domain, // )) func ComputeSigningRoot(object interface{}, domain []byte) ([32]byte, error) { + if object == nil { + return [32]byte{}, errors.New("cannot compute signing root of nil") + } return signingData(func() ([32]byte, error) { - switch t := object.(type) { - case *ethpb.BeaconBlock: - return stateutil.BlockRoot(t) - case *ethpb.AttestationData: - return stateutil.AttestationDataRoot(t) - default: - // utilise generic ssz library - return ssz.HashTreeRoot(object) + if v, ok := object.(fssz.HashRoot); ok { + return v.HashTreeRoot() } + return ssz.HashTreeRoot(object) }, domain) } @@ -72,7 +71,7 @@ func signingData(rootFunc func() ([32]byte, error), domain []byte) ([32]byte, er ObjectRoot: objRoot[:], Domain: domain, } - return ssz.HashTreeRoot(container) + return container.HashTreeRoot() } // ComputeDomainVerifySigningRoot computes domain and verifies signing root of an object given the beacon state, validator index and signature. @@ -225,10 +224,10 @@ func domain(domainType [DomainByteLength]byte, forkDataRoot []byte) []byte { // genesis_validators_root=genesis_validators_root, // )) func computeForkDataRoot(version []byte, root []byte) ([32]byte, error) { - r, err := ssz.HashTreeRoot(&pb.ForkData{ + r, err := (&pb.ForkData{ CurrentVersion: version, GenesisValidatorsRoot: root, - }) + }).HashTreeRoot() if err != nil { return [32]byte{}, err } diff --git a/beacon-chain/core/helpers/signing_root_test.go b/beacon-chain/core/helpers/signing_root_test.go index 8b8fbe7db0..54e9c9f6f8 100644 --- a/beacon-chain/core/helpers/signing_root_test.go +++ b/beacon-chain/core/helpers/signing_root_test.go @@ -1,21 +1,21 @@ -package helpers +package helpers_test import ( "bytes" "testing" fuzz "github.com/google/gofuzz" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" + "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - "github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) func TestSigningRoot_ComputeOK(t *testing.T) { - emptyBlock := ðpb.BeaconBlock{} - _, err := ComputeSigningRoot(emptyBlock, []byte{'T', 'E', 'S', 'T'}) + emptyBlock := testutil.NewBeaconBlock() + _, err := helpers.ComputeSigningRoot(emptyBlock, bytesutil.PadTo([]byte{'T', 'E', 'S', 'T'}, 32)) assert.NoError(t, err, "Could not compute signing root of block") } @@ -25,38 +25,21 @@ func TestComputeDomain_OK(t *testing.T) { domainType [4]byte domain []byte }{ - {epoch: 1, domainType: [4]byte{4, 0, 0, 0}, domain: []byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, - {epoch: 2, domainType: [4]byte{4, 0, 0, 0}, domain: []byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, - {epoch: 2, domainType: [4]byte{5, 0, 0, 0}, domain: []byte{5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, - {epoch: 3, domainType: [4]byte{4, 0, 0, 0}, domain: []byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, - {epoch: 3, domainType: [4]byte{5, 0, 0, 0}, domain: []byte{5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {epoch: 1, domainType: [4]byte{4, 0, 0, 0}, domain: []byte{4, 0, 0, 0, 245, 165, 253, 66, 209, 106, 32, 48, 39, 152, 239, 110, 211, 9, 151, 155, 67, 0, 61, 35, 32, 217, 240, 232, 234, 152, 49, 169}}, + {epoch: 2, domainType: [4]byte{4, 0, 0, 0}, domain: []byte{4, 0, 0, 0, 245, 165, 253, 66, 209, 106, 32, 48, 39, 152, 239, 110, 211, 9, 151, 155, 67, 0, 61, 35, 32, 217, 240, 232, 234, 152, 49, 169}}, + {epoch: 2, domainType: [4]byte{5, 0, 0, 0}, domain: []byte{5, 0, 0, 0, 245, 165, 253, 66, 209, 106, 32, 48, 39, 152, 239, 110, 211, 9, 151, 155, 67, 0, 61, 35, 32, 217, 240, 232, 234, 152, 49, 169}}, + {epoch: 3, domainType: [4]byte{4, 0, 0, 0}, domain: []byte{4, 0, 0, 0, 245, 165, 253, 66, 209, 106, 32, 48, 39, 152, 239, 110, 211, 9, 151, 155, 67, 0, 61, 35, 32, 217, 240, 232, 234, 152, 49, 169}}, + {epoch: 3, domainType: [4]byte{5, 0, 0, 0}, domain: []byte{5, 0, 0, 0, 245, 165, 253, 66, 209, 106, 32, 48, 39, 152, 239, 110, 211, 9, 151, 155, 67, 0, 61, 35, 32, 217, 240, 232, 234, 152, 49, 169}}, } for _, tt := range tests { - if !bytes.Equal(domain(tt.domainType, params.BeaconConfig().ZeroHash[:]), tt.domain) { - t.Errorf("wanted domain version: %d, got: %d", tt.domain, domain(tt.domainType, params.BeaconConfig().ZeroHash[:])) + if got, err := helpers.ComputeDomain(tt.domainType, nil, nil); !bytes.Equal(got, tt.domain) { + t.Errorf("wanted domain version: %d, got: %d", tt.domain, got) + } else { + require.NoError(t, err) } } } -func TestSigningRoot_Compatibility(t *testing.T) { - parRoot := [32]byte{'A'} - stateRoot := [32]byte{'B'} - blk := ðpb.BeaconBlock{ - Slot: 20, - ProposerIndex: 20, - ParentRoot: parRoot[:], - StateRoot: stateRoot[:], - Body: ðpb.BeaconBlockBody{}, - } - root, err := ComputeSigningRoot(blk, params.BeaconConfig().DomainBeaconProposer[:]) - require.NoError(t, err) - newRoot, err := signingData(func() ([32]byte, error) { - return stateutil.BlockRoot(blk) - }, params.BeaconConfig().DomainBeaconProposer[:]) - require.NoError(t, err) - assert.Equal(t, root, newRoot, "Wanted root of %#x but got %#x", root, newRoot) -} - func TestComputeForkDigest_OK(t *testing.T) { tests := []struct { version []byte @@ -68,7 +51,7 @@ func TestComputeForkDigest_OK(t *testing.T) { {version: []byte{'b', 'w', 'r', 't'}, root: [32]byte{'r', 'd', 'c'}, result: [4]byte{0x83, 0x34, 0x38, 0x88}}, } for _, tt := range tests { - digest, err := ComputeForkDigest(tt.version, tt.root[:]) + digest, err := helpers.ComputeForkDigest(tt.version, tt.root[:]) require.NoError(t, err) assert.Equal(t, tt.result, digest, "Wanted domain version: %#x, got: %#x", digest, tt.result) } @@ -92,8 +75,8 @@ func TestFuzzverifySigningRoot_10000(t *testing.T) { fuzzer.Fuzz(&p) fuzzer.Fuzz(&s) fuzzer.Fuzz(&d) - err := VerifySigningRoot(state, pubkey[:], sig[:], domain[:]) - err = VerifySigningRoot(state, p, s, d) + err := helpers.VerifySigningRoot(state, pubkey[:], sig[:], domain[:]) + err = helpers.VerifySigningRoot(state, p, s, d) _ = err } } diff --git a/beacon-chain/core/helpers/validators_test.go b/beacon-chain/core/helpers/validators_test.go index 9210ff892c..eeb27659b5 100644 --- a/beacon-chain/core/helpers/validators_test.go +++ b/beacon-chain/core/helpers/validators_test.go @@ -731,7 +731,7 @@ func TestIsIsEligibleForActivation(t *testing.T) { true}, {"Not yet finalized", ðpb.Validator{ActivationEligibilityEpoch: 1, ActivationEpoch: params.BeaconConfig().FarFutureEpoch}, - &pb.BeaconState{FinalizedCheckpoint: ðpb.Checkpoint{}}, + &pb.BeaconState{FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}}, false}, {"Incorrect activation epoch", ðpb.Validator{ActivationEligibilityEpoch: 1}, diff --git a/beacon-chain/core/state/BUILD.bazel b/beacon-chain/core/state/BUILD.bazel index 371435ac40..d9e948eea6 100644 --- a/beacon-chain/core/state/BUILD.bazel +++ b/beacon-chain/core/state/BUILD.bazel @@ -38,7 +38,6 @@ go_library( "//shared/trieutil:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@io_opencensus_go//trace:go_default_library", ], @@ -70,6 +69,7 @@ go_test( "//shared/attestationutil:go_default_library", "//shared/benchutil:go_default_library", "//shared/bls:go_default_library", + "//shared/bytesutil:go_default_library", "//shared/hashutil:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", diff --git a/beacon-chain/core/state/benchmarks_test.go b/beacon-chain/core/state/benchmarks_test.go index 8a1c8502c6..45158f4d07 100644 --- a/beacon-chain/core/state/benchmarks_test.go +++ b/beacon-chain/core/state/benchmarks_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/gogo/protobuf/proto" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state" @@ -101,7 +100,7 @@ func BenchmarkHashTreeRoot_FullState(b *testing.B) { b.N = 50 b.ResetTimer() for i := 0; i < b.N; i++ { - if _, err := ssz.HashTreeRoot(beaconState); err != nil { + if _, err := beaconState.HashTreeRoot(context.Background()); err != nil { b.Fatal(err) } } diff --git a/beacon-chain/core/state/state.go b/beacon-chain/core/state/state.go index 80eb02e0d0..dcc61501e3 100644 --- a/beacon-chain/core/state/state.go +++ b/beacon-chain/core/state/state.go @@ -9,7 +9,6 @@ import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" @@ -70,7 +69,7 @@ func GenesisBeaconState(deposits []*ethpb.Deposit, genesisTime uint64, eth1Data if deposit == nil || deposit.Data == nil { return nil, fmt.Errorf("nil deposit or deposit with nil data cannot be processed: %v", deposit) } - hash, err := ssz.HashTreeRoot(deposit.Data) + hash, err := deposit.Data.HashTreeRoot() if err != nil { return nil, err } diff --git a/beacon-chain/core/state/transition_test.go b/beacon-chain/core/state/transition_test.go index 02cd446572..7014330f1d 100644 --- a/beacon-chain/core/state/transition_test.go +++ b/beacon-chain/core/state/transition_test.go @@ -9,7 +9,6 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" @@ -18,6 +17,7 @@ import ( pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bls" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/hashutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -52,7 +52,8 @@ func TestExecuteStateTransition_FullProcess(t *testing.T) { eth1Data := ðpb.Eth1Data{ DepositCount: 100, - DepositRoot: []byte{2}, + DepositRoot: bytesutil.PadTo([]byte{2}, 32), + BlockHash: make([]byte, 32), } require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch-1)) e := beaconState.Eth1Data() @@ -76,17 +77,12 @@ func TestExecuteStateTransition_FullProcess(t *testing.T) { require.NoError(t, nextSlotState.SetSlot(beaconState.Slot()+1)) proposerIdx, err := helpers.BeaconProposerIndex(nextSlotState) require.NoError(t, err) - block := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: proposerIdx, - Slot: beaconState.Slot() + 1, - ParentRoot: parentRoot[:], - Body: ðpb.BeaconBlockBody{ - RandaoReveal: randaoReveal, - Eth1Data: eth1Data, - }, - }, - } + block := testutil.NewBeaconBlock() + block.Block.ProposerIndex = proposerIdx + block.Block.Slot = beaconState.Slot() + 1 + block.Block.ParentRoot = parentRoot[:] + block.Block.Body.RandaoReveal = randaoReveal + block.Block.Body.Eth1Data = eth1Data stateRoot, err := state.CalculateStateRoot(context.Background(), beaconState, block) require.NoError(t, err) @@ -114,7 +110,8 @@ func TestExecuteStateTransitionNoVerify_FullProcess(t *testing.T) { eth1Data := ðpb.Eth1Data{ DepositCount: 100, - DepositRoot: []byte{2}, + DepositRoot: bytesutil.PadTo([]byte{2}, 32), + BlockHash: make([]byte, 32), } require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch-1)) e := beaconState.Eth1Data() @@ -135,17 +132,12 @@ func TestExecuteStateTransitionNoVerify_FullProcess(t *testing.T) { require.NoError(t, nextSlotState.SetSlot(beaconState.Slot()+1)) proposerIdx, err := helpers.BeaconProposerIndex(nextSlotState) require.NoError(t, err) - block := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: proposerIdx, - Slot: beaconState.Slot() + 1, - ParentRoot: parentRoot[:], - Body: ðpb.BeaconBlockBody{ - RandaoReveal: randaoReveal, - Eth1Data: eth1Data, - }, - }, - } + block := testutil.NewBeaconBlock() + block.Block.ProposerIndex = proposerIdx + block.Block.Slot = beaconState.Slot() + 1 + block.Block.ParentRoot = parentRoot[:] + block.Block.Body.RandaoReveal = randaoReveal + block.Block.Body.Eth1Data = eth1Data stateRoot, err := state.CalculateStateRoot(context.Background(), beaconState, block) require.NoError(t, err) @@ -169,8 +161,24 @@ func TestProcessBlock_IncorrectProposerSlashing(t *testing.T) { block, err := testutil.GenerateFullBlock(beaconState, privKeys, nil, 1) require.NoError(t, err) slashing := ðpb.ProposerSlashing{ - Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch}}, - Header_2: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch * 2}}, + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + Slot: params.BeaconConfig().SlotsPerEpoch, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + Slot: params.BeaconConfig().SlotsPerEpoch * 2, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, } block.Block.Body.ProposerSlashings = []*ethpb.ProposerSlashing{slashing} @@ -193,10 +201,12 @@ func TestProcessBlock_IncorrectProcessBlockAttestations(t *testing.T) { att := ðpb.Attestation{ Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{Epoch: 0}, - Source: ðpb.Checkpoint{Epoch: 0}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, AggregationBits: bitfield.NewBitlist(3), + Signature: bls.RandKey().Sign([]byte("foo")).Marshal(), } block, err := testutil.GenerateFullBlock(beaconState, privKeys, nil, 1) @@ -212,7 +222,7 @@ func TestProcessBlock_IncorrectProcessBlockAttestations(t *testing.T) { beaconState, err = state.ProcessSlots(context.Background(), beaconState, 1) require.NoError(t, err) - want := "could not process block attestations" + want := "could not verify attestations" _, err = state.ProcessBlock(context.Background(), beaconState, block) assert.ErrorContains(t, want, err) } @@ -226,15 +236,21 @@ func TestProcessBlock_IncorrectProcessExits(t *testing.T) { Header: ðpb.BeaconBlockHeader{ ProposerIndex: 3, Slot: 1, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), }, - Signature: []byte("A"), + Signature: bytesutil.PadTo([]byte("A"), 96), }, Header_2: ðpb.SignedBeaconBlockHeader{ Header: ðpb.BeaconBlockHeader{ ProposerIndex: 3, Slot: 1, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), }, - Signature: []byte("B"), + Signature: bytesutil.PadTo([]byte("B"), 96), }, }, } @@ -242,17 +258,19 @@ func TestProcessBlock_IncorrectProcessExits(t *testing.T) { { Attestation_1: ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, }, AttestingIndices: []uint64{0, 1}, + Signature: make([]byte, 96), }, Attestation_2: ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 1}, - Target: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, }, AttestingIndices: []uint64{0, 1}, + Signature: make([]byte, 96), }, }, } @@ -263,7 +281,7 @@ func TestProcessBlock_IncorrectProcessExits(t *testing.T) { require.NoError(t, beaconState.SetBlockRoots(blockRoots)) blockAtt := ðpb.Attestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, Target: ðpb.Checkpoint{Epoch: 0, Root: []byte("hello-world")}, }, AggregationBits: bitfield.Bitlist{0xC0, 0xC0, 0xC0, 0xC0, 0x01}, @@ -280,23 +298,24 @@ func TestProcessBlock_IncorrectProcessExits(t *testing.T) { Slot: genesisBlock.Block.Slot, ParentRoot: genesisBlock.Block.ParentRoot, BodyRoot: bodyRoot[:], + StateRoot: make([]byte, 32), }) require.NoError(t, err) - parentRoot, err := ssz.HashTreeRoot(beaconState.LatestBlockHeader()) + parentRoot, err := beaconState.LatestBlockHeader().HashTreeRoot() require.NoError(t, err) block := ðpb.SignedBeaconBlock{ Block: ðpb.BeaconBlock{ ParentRoot: parentRoot[:], Slot: 1, Body: ðpb.BeaconBlockBody{ - RandaoReveal: []byte{}, + RandaoReveal: make([]byte, 96), ProposerSlashings: proposerSlashings, AttesterSlashings: attesterSlashings, Attestations: attestations, VoluntaryExits: exits, Eth1Data: ðpb.Eth1Data{ - DepositRoot: []byte{2}, - BlockHash: []byte{3}, + DepositRoot: bytesutil.PadTo([]byte{2}, 32), + BlockHash: bytesutil.PadTo([]byte{3}, 32), }, }, }, @@ -343,7 +362,9 @@ func createFullBlockWithOperations(t *testing.T) (*beaconstate.BeaconState, Header: ðpb.BeaconBlockHeader{ ProposerIndex: proposerSlashIdx, Slot: 1, - StateRoot: []byte("A"), + StateRoot: bytesutil.PadTo([]byte("A"), 32), + ParentRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), }, } header1.Signature, err = helpers.ComputeDomainAndSign(beaconState, currentEpoch, header1.Header, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerSlashIdx]) @@ -353,7 +374,9 @@ func createFullBlockWithOperations(t *testing.T) (*beaconstate.BeaconState, Header: ðpb.BeaconBlockHeader{ ProposerIndex: proposerSlashIdx, Slot: 1, - StateRoot: []byte("B"), + StateRoot: bytesutil.PadTo([]byte("B"), 32), + ParentRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), }, } header2.Signature, err = helpers.ComputeDomainAndSign(beaconState, helpers.CurrentEpoch(beaconState), header2.Header, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerSlashIdx]) @@ -372,9 +395,12 @@ func createFullBlockWithOperations(t *testing.T) (*beaconstate.BeaconState, mockRoot2 := [32]byte{'A'} att1 := ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0, Root: mockRoot2[:]}, - Target: ðpb.Checkpoint{Epoch: 0}}, + Source: ðpb.Checkpoint{Epoch: 0, Root: mockRoot2[:]}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), + }, AttestingIndices: []uint64{0, 1}, + Signature: make([]byte, 96), } domain, err := helpers.Domain(beaconState.Fork(), currentEpoch, params.BeaconConfig().DomainBeaconAttester, beaconState.GenesisValidatorRoot()) require.NoError(t, err) @@ -388,9 +414,12 @@ func createFullBlockWithOperations(t *testing.T) (*beaconstate.BeaconState, mockRoot3 := [32]byte{'B'} att2 := ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0, Root: mockRoot3[:]}, - Target: ðpb.Checkpoint{Epoch: 0}}, + Source: ðpb.Checkpoint{Epoch: 0, Root: mockRoot3[:]}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), + }, AttestingIndices: []uint64{0, 1}, + Signature: make([]byte, 96), } hashTreeRoot, err = helpers.ComputeSigningRoot(att2.Data, domain) @@ -417,13 +446,15 @@ func createFullBlockWithOperations(t *testing.T) (*beaconstate.BeaconState, aggBits.SetBitAt(0, true) blockAtt := ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: beaconState.Slot(), - Target: ðpb.Checkpoint{Epoch: helpers.CurrentEpoch(beaconState)}, + Slot: beaconState.Slot(), + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Epoch: helpers.CurrentEpoch(beaconState), Root: make([]byte, 32)}, Source: ðpb.Checkpoint{ Epoch: 0, Root: mockRoot[:], }}, AggregationBits: aggBits, + Signature: make([]byte, 96), } committee, err := helpers.BeaconCommitteeFromState(beaconState, blockAtt.Data.Slot, blockAtt.Data.CommitteeIndex) @@ -467,14 +498,15 @@ func createFullBlockWithOperations(t *testing.T) (*beaconstate.BeaconState, Slot: beaconState.Slot() + 1, ProposerIndex: proposerIndex, Body: ðpb.BeaconBlockBody{ + Graffiti: make([]byte, 32), RandaoReveal: randaoReveal, ProposerSlashings: proposerSlashings, AttesterSlashings: attesterSlashings, Attestations: []*ethpb.Attestation{blockAtt}, VoluntaryExits: []*ethpb.SignedVoluntaryExit{exit}, Eth1Data: ðpb.Eth1Data{ - DepositRoot: []byte{2}, - BlockHash: []byte{3}, + DepositRoot: bytesutil.PadTo([]byte{2}, 32), + BlockHash: bytesutil.PadTo([]byte{3}, 32), }, }, }, @@ -522,7 +554,7 @@ func TestProcessBlockNoVerify_PassesProcessingConditions(t *testing.T) { func TestProcessEpochPrecompute_CanProcess(t *testing.T) { epoch := uint64(1) - atts := []*pb.PendingAttestation{{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{}}, InclusionDelay: 1}} + atts := []*pb.PendingAttestation{{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{Root: make([]byte, 32)}}, InclusionDelay: 1}} slashing := make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector) base := &pb.BeaconState{ Slot: epoch*params.BeaconConfig().SlotsPerEpoch + 1, @@ -530,9 +562,9 @@ func TestProcessEpochPrecompute_CanProcess(t *testing.T) { Slashings: slashing, RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), CurrentEpochAttestations: atts, - FinalizedCheckpoint: ðpb.Checkpoint{}, + FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, JustificationBits: bitfield.Bitvector4{0x00}, - CurrentJustifiedCheckpoint: ðpb.Checkpoint{}, + CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, Validators: []*ethpb.Validator{}, } s, err := beaconstate.InitializeFromProto(base) @@ -592,14 +624,14 @@ func BenchmarkProcessBlk_65536Validators_FullBlock(b *testing.B) { ProposerIndex: 1, Slot: 0, }, - Signature: []byte("A"), + Signature: bytesutil.PadTo([]byte("A"), 96), }, Header_2: ðpb.SignedBeaconBlockHeader{ Header: ðpb.BeaconBlockHeader{ ProposerIndex: 1, Slot: 0, }, - Signature: []byte("B"), + Signature: bytesutil.PadTo([]byte("B"), 96), }, }, } @@ -608,11 +640,19 @@ func BenchmarkProcessBlk_65536Validators_FullBlock(b *testing.B) { attesterSlashings := []*ethpb.AttesterSlashing{ { Attestation_1: ðpb.IndexedAttestation{ - Data: ðpb.AttestationData{}, + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, AttestingIndices: []uint64{2, 3}, }, Attestation_2: ðpb.IndexedAttestation{ - Data: ðpb.AttestationData{}, + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, AttestingIndices: []uint64{2, 3}, }, }, @@ -625,7 +665,7 @@ func BenchmarkProcessBlk_65536Validators_FullBlock(b *testing.B) { Amount: params.BeaconConfig().MaxEffectiveBalance, }, } - leaf, err := ssz.HashTreeRoot(deposit.Data) + leaf, err := deposit.Data.HashTreeRoot() require.NoError(b, err) depositTrie, err := trieutil.GenerateTrieFromItems([][]byte{leaf[:]}, int(params.BeaconConfig().DepositContractTreeDepth)) require.NoError(b, err, "Could not generate trie") @@ -648,7 +688,7 @@ func BenchmarkProcessBlk_65536Validators_FullBlock(b *testing.B) { ObjectRoot: buf, Domain: domain, } - root, err = ssz.HashTreeRoot(ctr) + root, err = ctr.HashTreeRoot() require.NoError(b, err) epochSignature := priv.Sign(root[:]) @@ -725,10 +765,13 @@ func TestProcessBlk_AttsBasedOnValidatorCount(t *testing.T) { for i := 0; i < len(atts); i++ { att := ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 1, - Source: ðpb.Checkpoint{Epoch: 0, Root: params.BeaconConfig().ZeroHash[:]}, - Target: ðpb.Checkpoint{Epoch: 0}}, + Slot: 1, + Source: ðpb.Checkpoint{Epoch: 0, Root: params.BeaconConfig().ZeroHash[:]}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), + }, AggregationBits: aggBits, + Signature: make([]byte, 96), } committee, err := helpers.BeaconCommitteeFromState(s, att.Data.Slot, att.Data.CommitteeIndex) @@ -764,18 +807,12 @@ func TestProcessBlk_AttsBasedOnValidatorCount(t *testing.T) { require.NoError(t, nextSlotState.SetSlot(s.Slot()+1)) proposerIdx, err := helpers.BeaconProposerIndex(nextSlotState) require.NoError(t, err) - blk := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: proposerIdx, - Slot: s.Slot() + 1, - ParentRoot: parentRoot[:], - Body: ðpb.BeaconBlockBody{ - Eth1Data: ðpb.Eth1Data{}, - RandaoReveal: epochSignature, - Attestations: atts, - }, - }, - } + blk := testutil.NewBeaconBlock() + blk.Block.ProposerIndex = proposerIdx + blk.Block.Slot = s.Slot() + 1 + blk.Block.ParentRoot = parentRoot[:] + blk.Block.Body.RandaoReveal = epochSignature + blk.Block.Body.Attestations = atts sig, err := testutil.BlockSignature(s, blk.Block, privKeys) require.NoError(t, err) blk.Signature = sig.Marshal() diff --git a/beacon-chain/db/kafka/BUILD.bazel b/beacon-chain/db/kafka/BUILD.bazel index 9244b3e3b3..2f7a18844d 100644 --- a/beacon-chain/db/kafka/BUILD.bazel +++ b/beacon-chain/db/kafka/BUILD.bazel @@ -18,6 +18,7 @@ go_library( "//shared/featureconfig:go_default_library", "//shared/traceutil:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", + "@com_github_ferranbt_fastssz//:go_default_library", "@com_github_golang_protobuf//jsonpb:go_default_library_gen", "@com_github_golang_protobuf//proto:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", diff --git a/beacon-chain/db/kafka/export_wrapper.go b/beacon-chain/db/kafka/export_wrapper.go index 6568221bb3..7c5be3c6fd 100644 --- a/beacon-chain/db/kafka/export_wrapper.go +++ b/beacon-chain/db/kafka/export_wrapper.go @@ -6,6 +6,7 @@ import ( "bytes" "context" + fssz "github.com/ferranbt/fastssz" "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/proto" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -55,7 +56,13 @@ func (e Exporter) publish(ctx context.Context, topic string, msg proto.Message) return err } - key, err := ssz.HashTreeRoot(msg) + var key [32]byte + var err error + if v, ok := msg.(fssz.HashRoot); ok { + key, err = v.HashTreeRoot() + } else { + key, err = ssz.HashTreeRoot(msg) + } if err != nil { traceutil.AnnotateError(span, err) return err diff --git a/beacon-chain/db/kv/BUILD.bazel b/beacon-chain/db/kv/BUILD.bazel index 8ffc6e2258..d9df859305 100644 --- a/beacon-chain/db/kv/BUILD.bazel +++ b/beacon-chain/db/kv/BUILD.bazel @@ -50,7 +50,6 @@ go_library( "@com_github_pkg_errors//:go_default_library", "@com_github_prometheus_client_golang//prometheus:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_prysmaticlabs_prombbolt//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@io_etcd_go_bbolt//:go_default_library", @@ -94,7 +93,6 @@ go_test( "@com_github_ethereum_go_ethereum//common:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@in_gopkg_d4l3k_messagediff_v1//:go_default_library", "@io_etcd_go_bbolt//:go_default_library", ], diff --git a/beacon-chain/db/kv/backup_test.go b/beacon-chain/db/kv/backup_test.go index 72abf79b96..1f8de73aa0 100644 --- a/beacon-chain/db/kv/backup_test.go +++ b/beacon-chain/db/kv/backup_test.go @@ -6,7 +6,6 @@ import ( "path" "testing" - eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -16,7 +15,8 @@ func TestStore_Backup(t *testing.T) { db := setupDB(t) ctx := context.Background() - head := ð.SignedBeaconBlock{Block: ð.BeaconBlock{Slot: 5000}} + head := testutil.NewBeaconBlock() + head.Block.Slot = 5000 require.NoError(t, db.SaveBlock(ctx, head)) root, err := stateutil.BlockRoot(head.Block) diff --git a/beacon-chain/db/kv/blocks_test.go b/beacon-chain/db/kv/blocks_test.go index 309219d3f5..36c338b0a7 100644 --- a/beacon-chain/db/kv/blocks_test.go +++ b/beacon-chain/db/kv/blocks_test.go @@ -22,19 +22,14 @@ func TestStore_SaveBlock_NoDuplicates(t *testing.T) { slot := uint64(20) ctx := context.Background() // First we save a previous block to ensure the cache max size is reached. - prevBlock := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: slot - 1, - ParentRoot: bytesutil.PadTo([]byte{1, 2, 3}, 32), - }, - } + prevBlock := testutil.NewBeaconBlock() + prevBlock.Block.Slot = slot - 1 + prevBlock.Block.ParentRoot = bytesutil.PadTo([]byte{1, 2, 3}, 32) require.NoError(t, db.SaveBlock(ctx, prevBlock)) - block := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: slot, - ParentRoot: bytesutil.PadTo([]byte{1, 2, 3}, 32), - }, - } + + block := testutil.NewBeaconBlock() + block.Block.Slot = slot + block.Block.ParentRoot = bytesutil.PadTo([]byte{1, 2, 3}, 32) // Even with a full cache, saving new blocks should not cause // duplicated blocks in the DB. for i := 0; i < 100; i++ { @@ -51,12 +46,11 @@ func TestStore_SaveBlock_NoDuplicates(t *testing.T) { func TestStore_BlocksCRUD(t *testing.T) { db := setupDB(t) ctx := context.Background() - block := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: 20, - ParentRoot: bytesutil.PadTo([]byte{1, 2, 3}, 32), - }, - } + + block := testutil.NewBeaconBlock() + block.Block.Slot = 20 + block.Block.ParentRoot = bytesutil.PadTo([]byte{1, 2, 3}, 32) + blockRoot, err := stateutil.BlockRoot(block.Block) require.NoError(t, err) retrievedBlock, err := db.Block(ctx, blockRoot) @@ -372,12 +366,10 @@ func TestStore_SaveBlocks_HasCachedBlocks(t *testing.T) { b := make([]*ethpb.SignedBeaconBlock, 500) for i := 0; i < 500; i++ { - b[i] = ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ParentRoot: bytesutil.PadTo([]byte("parent"), 32), - Slot: uint64(i), - }, - } + blk := testutil.NewBeaconBlock() + blk.Block.ParentRoot = bytesutil.PadTo([]byte("parent"), 32) + blk.Block.Slot = uint64(i) + b[i] = blk } require.NoError(t, db.SaveBlock(ctx, b[0])) diff --git a/beacon-chain/db/kv/checkpoint_test.go b/beacon-chain/db/kv/checkpoint_test.go index 7130dabf60..647a19d2a1 100644 --- a/beacon-chain/db/kv/checkpoint_test.go +++ b/beacon-chain/db/kv/checkpoint_test.go @@ -39,12 +39,9 @@ func TestStore_FinalizedCheckpoint_CanSaveRetrieve(t *testing.T) { genesis := bytesutil.ToBytes32([]byte{'G', 'E', 'N', 'E', 'S', 'I', 'S'}) require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesis)) - blk := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ParentRoot: genesis[:], - Slot: 40, - }, - } + blk := testutil.NewBeaconBlock() + blk.Block.ParentRoot = genesis[:] + blk.Block.Slot = 40 root, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) diff --git a/beacon-chain/db/kv/finalized_block_roots_test.go b/beacon-chain/db/kv/finalized_block_roots_test.go index 36f659f820..f9abb53a43 100644 --- a/beacon-chain/db/kv/finalized_block_roots_test.go +++ b/beacon-chain/db/kv/finalized_block_roots_test.go @@ -55,7 +55,8 @@ func TestStore_IsFinalizedBlockGenesis(t *testing.T) { db := setupDB(t) ctx := context.Background() - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 0}} + blk := testutil.NewBeaconBlock() + blk.Block.Slot = 0 root, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, db.SaveBlock(ctx, blk)) @@ -139,12 +140,9 @@ func makeBlocks(t *testing.T, i, n uint64, previousRoot [32]byte) []*ethpb.Signe for j := i; j < n+i; j++ { parentRoot := make([]byte, 32) copy(parentRoot, previousRoot[:]) - blocks[j-i] = ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: j + 1, - ParentRoot: parentRoot, - }, - } + blocks[j-i] = testutil.NewBeaconBlock() + blocks[j-i].Block.Slot = j + 1 + blocks[j-i].Block.ParentRoot = parentRoot var err error previousRoot, err = stateutil.BlockRoot(blocks[j-i].Block) require.NoError(t, err) diff --git a/beacon-chain/db/kv/migration_archived_index_test.go b/beacon-chain/db/kv/migration_archived_index_test.go index 5d3efd8892..44cab3e26b 100644 --- a/beacon-chain/db/kv/migration_archived_index_test.go +++ b/beacon-chain/db/kv/migration_archived_index_test.go @@ -6,8 +6,8 @@ import ( "fmt" "testing" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "go.etcd.io/bbolt" ) @@ -53,8 +53,9 @@ func Test_migrateArchivedIndex(t *testing.T) { if err := tx.Bucket(archivedRootBucket).Put(bytesutil.Uint64ToBytesLittleEndian(2048), []byte("foo")); err != nil { return err } - - b, err := encode(context.Background(), ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2048}}) + sb := testutil.NewBeaconBlock() + sb.Block.Slot = 2048 + b, err := encode(context.Background(), sb) if err != nil { return err } diff --git a/beacon-chain/db/kv/operations.go b/beacon-chain/db/kv/operations.go index feb354bab9..9d5575b9b5 100644 --- a/beacon-chain/db/kv/operations.go +++ b/beacon-chain/db/kv/operations.go @@ -4,7 +4,6 @@ import ( "context" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" bolt "go.etcd.io/bbolt" "go.opencensus.io/trace" ) @@ -42,7 +41,7 @@ func (kv *Store) HasVoluntaryExit(ctx context.Context, exitRoot [32]byte) bool { func (kv *Store) SaveVoluntaryExit(ctx context.Context, exit *ethpb.VoluntaryExit) error { ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveVoluntaryExit") defer span.End() - exitRoot, err := ssz.HashTreeRoot(exit) + exitRoot, err := exit.HashTreeRoot() if err != nil { return err } diff --git a/beacon-chain/db/kv/operations_test.go b/beacon-chain/db/kv/operations_test.go index 0657395c84..bfb52d62c8 100644 --- a/beacon-chain/db/kv/operations_test.go +++ b/beacon-chain/db/kv/operations_test.go @@ -6,7 +6,6 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) @@ -17,7 +16,7 @@ func TestStore_VoluntaryExits_CRUD(t *testing.T) { exit := ðpb.VoluntaryExit{ Epoch: 5, } - exitRoot, err := ssz.HashTreeRoot(exit) + exitRoot, err := exit.HashTreeRoot() require.NoError(t, err) retrieved, err := db.VoluntaryExit(ctx, exitRoot) require.NoError(t, err) diff --git a/beacon-chain/db/kv/slashings.go b/beacon-chain/db/kv/slashings.go index b97c263739..fec3d0cd4d 100644 --- a/beacon-chain/db/kv/slashings.go +++ b/beacon-chain/db/kv/slashings.go @@ -4,7 +4,6 @@ import ( "context" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" bolt "go.etcd.io/bbolt" "go.opencensus.io/trace" ) @@ -42,7 +41,7 @@ func (kv *Store) HasProposerSlashing(ctx context.Context, slashingRoot [32]byte) func (kv *Store) SaveProposerSlashing(ctx context.Context, slashing *ethpb.ProposerSlashing) error { ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveProposerSlashing") defer span.End() - slashingRoot, err := ssz.HashTreeRoot(slashing) + slashingRoot, err := slashing.HashTreeRoot() if err != nil { return err } @@ -111,7 +110,7 @@ func (kv *Store) HasAttesterSlashing(ctx context.Context, slashingRoot [32]byte) func (kv *Store) SaveAttesterSlashing(ctx context.Context, slashing *ethpb.AttesterSlashing) error { ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveAttesterSlashing") defer span.End() - slashingRoot, err := ssz.HashTreeRoot(slashing) + slashingRoot, err := slashing.HashTreeRoot() if err != nil { return err } diff --git a/beacon-chain/db/kv/slashings_test.go b/beacon-chain/db/kv/slashings_test.go index 22f212b62f..c465f9c1f6 100644 --- a/beacon-chain/db/kv/slashings_test.go +++ b/beacon-chain/db/kv/slashings_test.go @@ -6,7 +6,6 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) @@ -34,7 +33,7 @@ func TestStore_ProposerSlashing_CRUD(t *testing.T) { Signature: make([]byte, 96), }, } - slashingRoot, err := ssz.HashTreeRoot(prop) + slashingRoot, err := prop.HashTreeRoot() require.NoError(t, err) retrieved, err := db.ProposerSlashing(ctx, slashingRoot) require.NoError(t, err) @@ -83,7 +82,7 @@ func TestStore_AttesterSlashing_CRUD(t *testing.T) { Signature: make([]byte, 96), }, } - slashingRoot, err := ssz.HashTreeRoot(att) + slashingRoot, err := att.HashTreeRoot() require.NoError(t, err) retrieved, err := db.AttesterSlashing(ctx, slashingRoot) require.NoError(t, err) diff --git a/beacon-chain/db/kv/state_test.go b/beacon-chain/db/kv/state_test.go index b9d1528f9a..8b4d3589db 100644 --- a/beacon-chain/db/kv/state_test.go +++ b/beacon-chain/db/kv/state_test.go @@ -132,12 +132,10 @@ func TestStore_DeleteFinalizedState(t *testing.T) { genesis := bytesutil.ToBytes32([]byte{'G', 'E', 'N', 'E', 'S', 'I', 'S'}) require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesis)) - blk := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ParentRoot: genesis[:], - Slot: 100, - }, - } + blk := testutil.NewBeaconBlock() + blk.Block.ParentRoot = genesis[:] + blk.Block.Slot = 100 + require.NoError(t, db.SaveBlock(ctx, blk)) finalizedBlockRoot, err := stateutil.BlockRoot(blk.Block) @@ -159,12 +157,9 @@ func TestStore_DeleteHeadState(t *testing.T) { genesis := bytesutil.ToBytes32([]byte{'G', 'E', 'N', 'E', 'S', 'I', 'S'}) require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesis)) - blk := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ParentRoot: genesis[:], - Slot: 100, - }, - } + blk := testutil.NewBeaconBlock() + blk.Block.ParentRoot = genesis[:] + blk.Block.Slot = 100 require.NoError(t, db.SaveBlock(ctx, blk)) headBlockRoot, err := stateutil.BlockRoot(blk.Block) @@ -180,7 +175,8 @@ func TestStore_DeleteHeadState(t *testing.T) { func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { db := setupDB(t) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} + b := testutil.NewBeaconBlock() + b.Block.Slot = 1 r, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) require.NoError(t, db.SaveBlock(context.Background(), b)) @@ -189,7 +185,7 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { s0 := st.InnerStateUnsafe() require.NoError(t, db.SaveState(context.Background(), st, r)) - b = ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 100}} + b.Block.Slot = 100 r1, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) require.NoError(t, db.SaveBlock(context.Background(), b)) @@ -198,7 +194,7 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { s1 := st.InnerStateUnsafe() require.NoError(t, db.SaveState(context.Background(), st, r1)) - b = ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1000}} + b.Block.Slot = 1000 r2, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) require.NoError(t, db.SaveBlock(context.Background(), b)) @@ -229,7 +225,8 @@ func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) { require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), genesisRoot)) require.NoError(t, db.SaveState(context.Background(), genesisState, genesisRoot)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} + b := testutil.NewBeaconBlock() + b.Block.Slot = 1 r, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) require.NoError(t, db.SaveBlock(context.Background(), b)) diff --git a/beacon-chain/operations/attestations/kv/BUILD.bazel b/beacon-chain/operations/attestations/kv/BUILD.bazel index cb046bda7e..76131dfc5f 100644 --- a/beacon-chain/operations/attestations/kv/BUILD.bazel +++ b/beacon-chain/operations/attestations/kv/BUILD.bazel @@ -42,6 +42,7 @@ go_test( "//shared/bls:go_default_library", "//shared/testutil/assert:go_default_library", "//shared/testutil/require:go_default_library", + "@com_github_ferranbt_fastssz//:go_default_library", "@com_github_patrickmn_go_cache//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", diff --git a/beacon-chain/operations/attestations/kv/aggregated_test.go b/beacon-chain/operations/attestations/kv/aggregated_test.go index 3d8b8a8f29..4743a025d3 100644 --- a/beacon-chain/operations/attestations/kv/aggregated_test.go +++ b/beacon-chain/operations/attestations/kv/aggregated_test.go @@ -5,6 +5,7 @@ import ( "sort" "testing" + fssz "github.com/ferranbt/fastssz" c "github.com/patrickmn/go-cache" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" @@ -18,14 +19,14 @@ func TestKV_Aggregated_AggregateUnaggregatedAttestations(t *testing.T) { priv := bls.RandKey() sig1 := priv.Sign([]byte{'a'}) sig2 := priv.Sign([]byte{'b'}) - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1001}, Signature: sig1.Marshal()} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1010}, Signature: sig1.Marshal()} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1100}, Signature: sig1.Marshal()} - att4 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1001}, Signature: sig2.Marshal()} - att5 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1001}, Signature: sig1.Marshal()} - att6 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1010}, Signature: sig1.Marshal()} - att7 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1100}, Signature: sig1.Marshal()} - att8 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1001}, Signature: sig2.Marshal()} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1001}, Signature: sig1.Marshal()} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1010}, Signature: sig1.Marshal()} + att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1100}, Signature: sig1.Marshal()} + att4 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1001}, Signature: sig2.Marshal()} + att5 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1001}, Signature: sig1.Marshal()} + att6 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1010}, Signature: sig1.Marshal()} + att7 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1100}, Signature: sig1.Marshal()} + att8 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1001}, Signature: sig2.Marshal()} atts := []*ethpb.Attestation{att1, att2, att3, att4, att5, att6, att7, att8} if err := cache.SaveUnaggregatedAttestations(atts); err != nil { t.Fatal(err) @@ -60,7 +61,11 @@ func TestKV_Aggregated_SaveAggregatedAttestation(t *testing.T) { { name: "not aggregated", att: ðpb.Attestation{ - Data: ðpb.AttestationData{}, AggregationBits: bitfield.Bitlist{0b10100}}, + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, AggregationBits: bitfield.Bitlist{0b10100}}, wantErrString: "attestation is not aggregated", }, { @@ -71,15 +76,19 @@ func TestKV_Aggregated_SaveAggregatedAttestation(t *testing.T) { }, AggregationBits: bitfield.Bitlist{0b10111}, }, - wantErrString: "could not tree hash attestation: incorrect fixed bytes marshalling", + wantErrString: "could not tree hash attestation: " + fssz.ErrBytesLength.Error(), }, { name: "already seen", att: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 100, + Slot: 100, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b11101001}, + Signature: make([]byte, 96), }, count: 0, }, @@ -87,15 +96,22 @@ func TestKV_Aggregated_SaveAggregatedAttestation(t *testing.T) { name: "normal save", att: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1101}, + Signature: make([]byte, 96), }, count: 1, }, } r, err := hashFn(ðpb.AttestationData{ - Slot: 100, + Slot: 100, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }) require.NoError(t, err) @@ -136,9 +152,9 @@ func TestKV_Aggregated_SaveAggregatedAttestations(t *testing.T) { { name: "no duplicates", atts: []*ethpb.Attestation{ - {Data: ðpb.AttestationData{Slot: 1}, + {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}}, - {Data: ðpb.AttestationData{Slot: 1}, + {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}}, }, count: 1, @@ -169,9 +185,9 @@ func TestKV_Aggregated_SaveAggregatedAttestations(t *testing.T) { func TestKV_Aggregated_AggregatedAttestations(t *testing.T) { cache := NewAttCaches() - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} + att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} atts := []*ethpb.Attestation{att1, att2, att3} for _, att := range atts { @@ -193,7 +209,7 @@ func TestKV_Aggregated_DeleteAggregatedAttestation(t *testing.T) { if err := cache.DeleteAggregatedAttestation(nil); err != nil { t.Error(err) } - att := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b10101}} + att := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b10101}, Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}} if err := cache.DeleteAggregatedAttestation(att); err != nil { t.Error(err) } @@ -201,7 +217,7 @@ func TestKV_Aggregated_DeleteAggregatedAttestation(t *testing.T) { t.Run("non aggregated attestation", func(t *testing.T) { cache := NewAttCaches() - att := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1001}, Data: ðpb.AttestationData{Slot: 2}} + att := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1001}, Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}} err := cache.DeleteAggregatedAttestation(att) wantErr := "attestation is not aggregated" if err == nil || err.Error() != wantErr { @@ -214,12 +230,11 @@ func TestKV_Aggregated_DeleteAggregatedAttestation(t *testing.T) { att := ðpb.Attestation{ AggregationBits: bitfield.Bitlist{0b1111}, Data: ðpb.AttestationData{ - Slot: 2, - BeaconBlockRoot: []byte{0b0}, + Slot: 2, }, } err := cache.DeleteAggregatedAttestation(att) - wantErr := "could not tree hash attestation data: incorrect fixed bytes marshalling" + wantErr := "could not tree hash attestation data: " + fssz.ErrBytesLength.Error() if err == nil || err.Error() != wantErr { t.Errorf("Did not receive wanted error, want: %q, got: %v", wantErr, err) } @@ -227,7 +242,7 @@ func TestKV_Aggregated_DeleteAggregatedAttestation(t *testing.T) { t.Run("nonexistent attestation", func(t *testing.T) { cache := NewAttCaches() - att := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1111}, Data: ðpb.AttestationData{Slot: 2}} + att := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1111}, Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}} if err := cache.DeleteAggregatedAttestation(att); err != nil { t.Error(err) } @@ -235,10 +250,10 @@ func TestKV_Aggregated_DeleteAggregatedAttestation(t *testing.T) { t.Run("non-filtered deletion", func(t *testing.T) { cache := NewAttCaches() - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}} - att4 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b10101}} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} + att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} + att4 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b10101}} atts := []*ethpb.Attestation{att1, att2, att3, att4} if err := cache.SaveAggregatedAttestations(atts); err != nil { t.Fatal(err) @@ -258,10 +273,10 @@ func TestKV_Aggregated_DeleteAggregatedAttestation(t *testing.T) { t.Run("filtered deletion", func(t *testing.T) { cache := NewAttCaches() - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b110101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b110111}} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b110100}} - att4 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b110101}} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b110101}} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b110111}} + att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b110100}} + att4 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b110101}} atts := []*ethpb.Attestation{att1, att2, att3, att4} if err := cache.SaveAggregatedAttestations(atts); err != nil { t.Fatal(err) @@ -298,14 +313,23 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { { name: "nil attestation data", input: ðpb.Attestation{ - AggregationBits: bitfield.Bitlist{0b1111}}, + AggregationBits: bitfield.Bitlist{0b1111}, + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, + }, want: false, }, { name: "empty cache aggregated", input: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111}}, want: false, @@ -314,7 +338,10 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { name: "empty cache unaggregated", input: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1001}}, want: false, @@ -323,13 +350,19 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { name: "single attestation in cache with exact match", existing: []*ethpb.Attestation{{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111}}, }, input: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111}}, want: true, @@ -338,13 +371,19 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { name: "single attestation in cache with subset aggregation", existing: []*ethpb.Attestation{{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111}}, }, input: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1110}}, want: true, @@ -353,13 +392,19 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { name: "single attestation in cache with superset aggregation", existing: []*ethpb.Attestation{{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1110}}, }, input: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111}}, want: false, @@ -369,20 +414,29 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { existing: []*ethpb.Attestation{ { Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111000}, }, { Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1100111}, }, }, input: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1100000}}, want: true, @@ -392,20 +446,29 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { existing: []*ethpb.Attestation{ { Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111000}, }, { Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1100111}, }, }, input: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111111}}, want: false, @@ -415,20 +478,29 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { existing: []*ethpb.Attestation{ { Data: ðpb.AttestationData{ - Slot: 2, + Slot: 2, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111000}, }, { Data: ðpb.AttestationData{ - Slot: 3, + Slot: 3, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1100111}, }, }, input: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 1, + Slot: 1, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111111}}, want: false, @@ -438,14 +510,20 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { existing: []*ethpb.Attestation{ { Data: ðpb.AttestationData{ - Slot: 2, + Slot: 2, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111000}, }, }, input: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 2, + Slot: 2, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b1111}, }, @@ -460,6 +538,10 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { t.Error(err) } + if tt.input != nil && tt.input.Signature == nil { + tt.input.Signature = make([]byte, 96) + } + result, err := cache.HasAggregatedAttestation(tt.input) require.NoError(t, err) if result != tt.want { @@ -484,8 +566,8 @@ func TestKV_Aggregated_HasAggregatedAttestation(t *testing.T) { func TestKV_Aggregated_DuplicateAggregatedAttestations(t *testing.T) { cache := NewAttCaches() - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1111}} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1111}} atts := []*ethpb.Attestation{att1, att2} for _, att := range atts { diff --git a/beacon-chain/operations/attestations/kv/block_test.go b/beacon-chain/operations/attestations/kv/block_test.go index ff3a0bab9b..d5b0d28a0e 100644 --- a/beacon-chain/operations/attestations/kv/block_test.go +++ b/beacon-chain/operations/attestations/kv/block_test.go @@ -12,9 +12,9 @@ import ( func TestKV_BlockAttestation_CanSaveRetrieve(t *testing.T) { cache := NewAttCaches() - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} + att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} atts := []*ethpb.Attestation{att1, att2, att3} for _, att := range atts { @@ -35,9 +35,9 @@ func TestKV_BlockAttestation_CanSaveRetrieve(t *testing.T) { func TestKV_BlockAttestation_CanDelete(t *testing.T) { cache := NewAttCaches() - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} + att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}} atts := []*ethpb.Attestation{att1, att2, att3} for _, att := range atts { diff --git a/beacon-chain/operations/attestations/kv/forkchoice_test.go b/beacon-chain/operations/attestations/kv/forkchoice_test.go index 9096fba4f4..76ba60b446 100644 --- a/beacon-chain/operations/attestations/kv/forkchoice_test.go +++ b/beacon-chain/operations/attestations/kv/forkchoice_test.go @@ -12,9 +12,9 @@ import ( func TestKV_Forkchoice_CanSaveRetrieve(t *testing.T) { cache := NewAttCaches() - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}, Signature: make([]byte, 96)} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}, Signature: make([]byte, 96)} + att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}, Signature: make([]byte, 96)} atts := []*ethpb.Attestation{att1, att2, att3} for _, att := range atts { @@ -35,9 +35,9 @@ func TestKV_Forkchoice_CanSaveRetrieve(t *testing.T) { func TestKV_Forkchoice_CanDelete(t *testing.T) { cache := NewAttCaches() - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}, Signature: make([]byte, 96)} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}, Signature: make([]byte, 96)} + att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}, Signature: make([]byte, 96)} atts := []*ethpb.Attestation{att1, att2, att3} for _, att := range atts { diff --git a/beacon-chain/operations/attestations/kv/seen_bits_test.go b/beacon-chain/operations/attestations/kv/seen_bits_test.go index 6b8203c343..eaa45fbd34 100644 --- a/beacon-chain/operations/attestations/kv/seen_bits_test.go +++ b/beacon-chain/operations/attestations/kv/seen_bits_test.go @@ -10,21 +10,25 @@ import ( func TestAttCaches_hasSeenBit(t *testing.T) { c := NewAttCaches() - d := ðpb.AttestationData{} - seenA1 := ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b10000011}} - seenA2 := ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b11100000}} + d := ðpb.AttestationData{ + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), + } + seenA1 := ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b10000011}, Signature: make([]byte, 96)} + seenA2 := ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b11100000}, Signature: make([]byte, 96)} require.NoError(t, c.insertSeenBit(seenA1)) require.NoError(t, c.insertSeenBit(seenA2)) tests := []struct { att *ethpb.Attestation want bool }{ - {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b10000000}}, want: true}, - {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b10000001}}, want: true}, - {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b11100000}}, want: true}, - {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b10000011}}, want: true}, - {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b00001000}}, want: false}, - {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b11110111}}, want: false}, + {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b10000000}, Signature: make([]byte, 96)}, want: true}, + {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b10000001}, Signature: make([]byte, 96)}, want: true}, + {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b11100000}, Signature: make([]byte, 96)}, want: true}, + {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b10000011}, Signature: make([]byte, 96)}, want: true}, + {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b00001000}, Signature: make([]byte, 96)}, want: false}, + {att: ðpb.Attestation{Data: d, AggregationBits: bitfield.Bitlist{0b11110111}, Signature: make([]byte, 96)}, want: false}, } for _, tt := range tests { got, err := c.hasSeenBit(tt.att) diff --git a/beacon-chain/operations/attestations/kv/unaggregated_test.go b/beacon-chain/operations/attestations/kv/unaggregated_test.go index 0fb37f3a3a..43a1a2d08d 100644 --- a/beacon-chain/operations/attestations/kv/unaggregated_test.go +++ b/beacon-chain/operations/attestations/kv/unaggregated_test.go @@ -3,6 +3,7 @@ package kv import ( "testing" + fssz "github.com/ferranbt/fastssz" c "github.com/patrickmn/go-cache" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" @@ -23,7 +24,7 @@ func TestKV_Unaggregated_SaveUnaggregatedAttestation(t *testing.T) { }, { name: "already aggregated", - att: ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b10101}}, + att: ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b10101}, Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}}, wantErrString: "attestation is aggregated", }, { @@ -33,31 +34,33 @@ func TestKV_Unaggregated_SaveUnaggregatedAttestation(t *testing.T) { BeaconBlockRoot: []byte{0b0}, }, }, - wantErrString: "incorrect fixed bytes marshalling", + wantErrString: fssz.ErrBytesLength.Error(), }, { - name: "normal save", - att: ðpb.Attestation{ - Data: ðpb.AttestationData{ - Slot: 100, - }, - AggregationBits: bitfield.Bitlist{0b0001}, - }, + name: "normal save", + att: ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b0001}, Data: ðpb.AttestationData{BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}}, count: 1, }, { name: "already seen", att: ðpb.Attestation{ Data: ðpb.AttestationData{ - Slot: 100, + Slot: 100, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b10000001}, + Signature: make([]byte, 96), }, count: 0, }, } r, err := hashFn(ðpb.AttestationData{ - Slot: 100, + Slot: 100, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }) require.NoError(t, err) @@ -69,6 +72,10 @@ func TestKV_Unaggregated_SaveUnaggregatedAttestation(t *testing.T) { t.Errorf("Invalid start pool, atts: %d", len(cache.unAggregatedAtt)) } + if tt.att != nil && tt.att.Signature == nil { + tt.att.Signature = make([]byte, 96) + } + err := cache.SaveUnaggregatedAttestation(tt.att) if tt.wantErrString != "" && (err == nil || err.Error() != tt.wantErrString) { t.Errorf("Did not receive wanted error, want: %q, got: %v", tt.wantErrString, err) @@ -98,18 +105,18 @@ func TestKV_Unaggregated_SaveUnaggregatedAttestations(t *testing.T) { { name: "unaggregated only", atts: []*ethpb.Attestation{ - {Data: ðpb.AttestationData{Slot: 1}}, - {Data: ðpb.AttestationData{Slot: 2}}, - {Data: ðpb.AttestationData{Slot: 3}}, + {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, Signature: make([]byte, 96)}, + {Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, Signature: make([]byte, 96)}, + {Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, Signature: make([]byte, 96)}, }, count: 3, }, { name: "has aggregated", atts: []*ethpb.Attestation{ - {Data: ðpb.AttestationData{Slot: 1}}, - {AggregationBits: bitfield.Bitlist{0b1111}, Data: ðpb.AttestationData{Slot: 2}}, - {Data: ðpb.AttestationData{Slot: 3}}, + {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, Signature: make([]byte, 96)}, + {AggregationBits: bitfield.Bitlist{0b1111}, Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, Signature: make([]byte, 96)}, + {Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, Signature: make([]byte, 96)}, }, wantErrString: "attestation is aggregated", count: 1, @@ -150,7 +157,7 @@ func TestKV_Unaggregated_DeleteUnaggregatedAttestation(t *testing.T) { t.Run("aggregated attestation", func(t *testing.T) { cache := NewAttCaches() - att := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1111}, Data: ðpb.AttestationData{Slot: 2}} + att := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1111}, Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, Signature: make([]byte, 96)} err := cache.DeleteUnaggregatedAttestation(att) wantErr := "attestation is aggregated" if err == nil || err.Error() != wantErr { @@ -160,9 +167,9 @@ func TestKV_Unaggregated_DeleteUnaggregatedAttestation(t *testing.T) { t.Run("successful deletion", func(t *testing.T) { cache := NewAttCaches() - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b110}} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b110}} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b101}, Signature: make([]byte, 96)} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b110}, Signature: make([]byte, 96)} + att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b110}, Signature: make([]byte, 96)} atts := []*ethpb.Attestation{att1, att2, att3} if err := cache.SaveUnaggregatedAttestations(atts); err != nil { t.Fatal(err) @@ -181,9 +188,9 @@ func TestKV_Unaggregated_DeleteUnaggregatedAttestation(t *testing.T) { func TestKV_Unaggregated_UnaggregatedAttestationsBySlotIndex(t *testing.T) { cache := NewAttCaches() - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, CommitteeIndex: 1}, AggregationBits: bitfield.Bitlist{0b101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, CommitteeIndex: 2}, AggregationBits: bitfield.Bitlist{0b110}} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, CommitteeIndex: 1}, AggregationBits: bitfield.Bitlist{0b110}} + att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, CommitteeIndex: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b101}, Signature: make([]byte, 96)} + att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1, CommitteeIndex: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b110}, Signature: make([]byte, 96)} + att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2, CommitteeIndex: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b110}, Signature: make([]byte, 96)} atts := []*ethpb.Attestation{att1, att2, att3} for _, att := range atts { diff --git a/beacon-chain/operations/attestations/prepare_forkchoice_test.go b/beacon-chain/operations/attestations/prepare_forkchoice_test.go index c8050147d9..03e7ef2aba 100644 --- a/beacon-chain/operations/attestations/prepare_forkchoice_test.go +++ b/beacon-chain/operations/attestations/prepare_forkchoice_test.go @@ -2,6 +2,7 @@ package attestations import ( "context" + "fmt" "sort" "testing" @@ -262,21 +263,34 @@ func TestSeenAttestations_PresentInCache(t *testing.T) { s, err := NewService(context.Background(), &Config{Pool: NewPool()}) require.NoError(t, err) - att1 := ðpb.Attestation{Data: ðpb.AttestationData{}, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x13} /* 0b00010011 */} + ad1 := ðpb.AttestationData{ + Slot: 0, + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + } + att1 := ðpb.Attestation{Data: ad1, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x13} /* 0b00010011 */} got, err := s.seen(att1) require.NoError(t, err) if got { t.Error("Wanted false, got true") } - att2 := ðpb.Attestation{Data: ðpb.AttestationData{}, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x17} /* 0b00010111 */} + att2 := ðpb.Attestation{Data: ad1, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x17} /* 0b00010111 */} got, err = s.seen(att2) require.NoError(t, err) if got { t.Error("Wanted false, got true") } - att3 := ðpb.Attestation{Data: ðpb.AttestationData{}, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x17} /* 0b00010111 */} + att3 := ðpb.Attestation{Data: ad1, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x17} /* 0b00010111 */} got, err = s.seen(att3) require.NoError(t, err) if !got { @@ -285,6 +299,34 @@ func TestSeenAttestations_PresentInCache(t *testing.T) { } func TestService_seen(t *testing.T) { + ad1 := ðpb.AttestationData{ + Slot: 1, + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + } + + ad2 := ðpb.AttestationData{ + Slot: 2, + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + } + // Attestation are checked in order of this list. tests := []struct { att *ethpb.Attestation @@ -293,42 +335,42 @@ func TestService_seen(t *testing.T) { { att: ðpb.Attestation{ AggregationBits: bitfield.Bitlist{0b11011}, - Data: ðpb.AttestationData{Slot: 1}, + Data: ad1, }, want: false, }, { att: ðpb.Attestation{ AggregationBits: bitfield.Bitlist{0b11011}, - Data: ðpb.AttestationData{Slot: 1}, + Data: ad1, }, want: true, // Exact same attestation should return true }, { att: ðpb.Attestation{ AggregationBits: bitfield.Bitlist{0b10101}, - Data: ðpb.AttestationData{Slot: 1}, + Data: ad1, }, want: false, // Haven't seen the bit at index 2 yet. }, { att: ðpb.Attestation{ AggregationBits: bitfield.Bitlist{0b11111}, - Data: ðpb.AttestationData{Slot: 1}, + Data: ad1, }, want: true, // We've full committee at this point. }, { att: ðpb.Attestation{ AggregationBits: bitfield.Bitlist{0b11111}, - Data: ðpb.AttestationData{Slot: 2}, + Data: ad2, }, want: false, // Different root is different bitlist. }, { att: ðpb.Attestation{ AggregationBits: bitfield.Bitlist{0b11111001}, - Data: ðpb.AttestationData{Slot: 1}, + Data: ad1, }, want: false, // Sanity test that an attestation of different lengths does not panic. }, @@ -337,9 +379,11 @@ func TestService_seen(t *testing.T) { s, err := NewService(context.Background(), &Config{Pool: NewPool()}) require.NoError(t, err) - for _, tt := range tests { - got, err := s.seen(tt.att) - require.NoError(t, err) - assert.Equal(t, tt.want, got) + for i, tt := range tests { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + got, err := s.seen(tt.att) + require.NoError(t, err) + assert.Equal(t, tt.want, got) + }) } } diff --git a/beacon-chain/operations/attestations/prune_expired_test.go b/beacon-chain/operations/attestations/prune_expired_test.go index e11ac895e8..c70f411e31 100644 --- a/beacon-chain/operations/attestations/prune_expired_test.go +++ b/beacon-chain/operations/attestations/prune_expired_test.go @@ -24,9 +24,37 @@ func TestPruneExpired_Ticker(t *testing.T) { }) require.NoError(t, err) + ad1 := ðpb.AttestationData{ + Slot: 0, + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + } + + ad2 := ðpb.AttestationData{ + Slot: 1, + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + } + atts := []*ethpb.Attestation{ - {Data: ðpb.AttestationData{Slot: 0}, AggregationBits: bitfield.Bitlist{0b1000, 0b1}}, - {Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1000, 0b1}}, + {Data: ad1, AggregationBits: bitfield.Bitlist{0b1000, 0b1}, Signature: make([]byte, 96)}, + {Data: ad2, AggregationBits: bitfield.Bitlist{0b1000, 0b1}, Signature: make([]byte, 96)}, } if err := s.pool.SaveUnaggregatedAttestations(atts); err != nil { t.Fatal(err) @@ -35,8 +63,8 @@ func TestPruneExpired_Ticker(t *testing.T) { t.Fatalf("Unexpected number of attestations: %d", s.pool.UnaggregatedAttestationCount()) } atts = []*ethpb.Attestation{ - {Data: ðpb.AttestationData{Slot: 0}, AggregationBits: bitfield.Bitlist{0b1101, 0b1}}, - {Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101, 0b1}}, + {Data: ad1, AggregationBits: bitfield.Bitlist{0b1101, 0b1}, Signature: make([]byte, 96)}, + {Data: ad2, AggregationBits: bitfield.Bitlist{0b1101, 0b1}, Signature: make([]byte, 96)}, } if err := s.pool.SaveAggregatedAttestations(atts); err != nil { t.Fatal(err) @@ -87,10 +115,38 @@ func TestPruneExpired_PruneExpiredAtts(t *testing.T) { s, err := NewService(context.Background(), &Config{Pool: NewPool()}) require.NoError(t, err) - att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 0}, AggregationBits: bitfield.Bitlist{0b1101}} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 0}, AggregationBits: bitfield.Bitlist{0b1111}} - att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}} - att4 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1110}} + ad1 := ðpb.AttestationData{ + Slot: 0, + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + } + + ad2 := ðpb.AttestationData{ + Slot: 0, + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + } + + att1 := ðpb.Attestation{Data: ad1, AggregationBits: bitfield.Bitlist{0b1101}} + att2 := ðpb.Attestation{Data: ad1, AggregationBits: bitfield.Bitlist{0b1111}} + att3 := ðpb.Attestation{Data: ad2, AggregationBits: bitfield.Bitlist{0b1101}} + att4 := ðpb.Attestation{Data: ad2, AggregationBits: bitfield.Bitlist{0b1110}} atts := []*ethpb.Attestation{att1, att2, att3, att4} if err := s.pool.SaveAggregatedAttestations(atts); err != nil { t.Fatal(err) diff --git a/beacon-chain/p2p/BUILD.bazel b/beacon-chain/p2p/BUILD.bazel index fda1c5f51f..02946d9dd6 100644 --- a/beacon-chain/p2p/BUILD.bazel +++ b/beacon-chain/p2p/BUILD.bazel @@ -118,6 +118,7 @@ go_test( "//beacon-chain/p2p/testing:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//proto/testing:go_default_library", + "//shared/bytesutil:go_default_library", "//shared/featureconfig:go_default_library", "//shared/iputils:go_default_library", "//shared/p2putils:go_default_library", diff --git a/beacon-chain/p2p/discovery_test.go b/beacon-chain/p2p/discovery_test.go index 2105ec4f9c..7d8d4e2a84 100644 --- a/beacon-chain/p2p/discovery_test.go +++ b/beacon-chain/p2p/discovery_test.go @@ -17,6 +17,7 @@ import ( mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/iputils" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -217,7 +218,7 @@ func TestHostIsResolved(t *testing.T) { HostDNS: exampleHost, }, genesisTime: time.Now(), - genesisValidatorsRoot: []byte{'A'}, + genesisValidatorsRoot: bytesutil.PadTo([]byte{'A'}, 32), } ip, key := createAddrAndPrivKey(t) list, err := s.createListener(ip, key) diff --git a/beacon-chain/p2p/subnets_test.go b/beacon-chain/p2p/subnets_test.go index 6938feaca3..6b82f0e525 100644 --- a/beacon-chain/p2p/subnets_test.go +++ b/beacon-chain/p2p/subnets_test.go @@ -115,7 +115,9 @@ func TestStartDiscV5_DiscoverPeersWithSubnets(t *testing.T) { // Update ENR of a peer. testService := &Service{ dv5Listener: listeners[0], - metaData: &pb.MetaData{}, + metaData: &pb.MetaData{ + Attnets: bitfield.NewBitvector64(), + }, } cache.SubnetIDs.AddAttesterSubnetID(0, 10) testService.RefreshENR() diff --git a/beacon-chain/powchain/BUILD.bazel b/beacon-chain/powchain/BUILD.bazel index 9e6edb4ba9..b7c5b66bb9 100644 --- a/beacon-chain/powchain/BUILD.bazel +++ b/beacon-chain/powchain/BUILD.bazel @@ -42,7 +42,6 @@ go_library( "@com_github_prometheus_client_golang//prometheus:go_default_library", "@com_github_prometheus_client_golang//prometheus/promauto:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@io_k8s_client_go//tools/cache:go_default_library", "@io_opencensus_go//trace:go_default_library", @@ -85,7 +84,6 @@ go_test( "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", "@com_github_ethereum_go_ethereum//core/types:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@com_github_sirupsen_logrus//hooks/test:go_default_library", "@in_gopkg_d4l3k_messagediff_v1//:go_default_library", diff --git a/beacon-chain/powchain/deposit_test.go b/beacon-chain/powchain/deposit_test.go index b64bb4a464..0163bf8ad9 100644 --- a/beacon-chain/powchain/deposit_test.go +++ b/beacon-chain/powchain/deposit_test.go @@ -6,10 +6,10 @@ import ( "testing" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/shared/bls" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -81,9 +81,9 @@ func TestProcessDeposit_InvalidPublicKey(t *testing.T) { deposits, _, err := testutil.DeterministicDepositsAndKeys(1) require.NoError(t, err) - deposits[0].Data.PublicKey = []byte("junk") + deposits[0].Data.PublicKey = bytesutil.PadTo([]byte("junk"), 48) - leaf, err := ssz.HashTreeRoot(deposits[0].Data) + leaf, err := deposits[0].Data.HashTreeRoot() require.NoError(t, err, "Could not hash deposit") trie, err := trieutil.GenerateTrieFromItems([][]byte{leaf[:]}, int(params.BeaconConfig().DepositContractTreeDepth)) @@ -121,7 +121,7 @@ func TestProcessDeposit_InvalidSignature(t *testing.T) { copy(fakeSig[:], []byte{'F', 'A', 'K', 'E'}) deposits[0].Data.Signature = fakeSig[:] - leaf, err := ssz.HashTreeRoot(deposits[0].Data) + leaf, err := deposits[0].Data.HashTreeRoot() require.NoError(t, err, "Could not hash deposit") trie, err := trieutil.GenerateTrieFromItems([][]byte{leaf[:]}, int(params.BeaconConfig().DepositContractTreeDepth)) @@ -186,7 +186,8 @@ func TestProcessDeposit_IncompleteDeposit(t *testing.T) { deposit := ðpb.Deposit{ Data: ðpb.Deposit_Data{ Amount: params.BeaconConfig().EffectiveBalanceIncrement, // incomplete deposit - WithdrawalCredentials: []byte("testing"), + WithdrawalCredentials: bytesutil.PadTo([]byte("testing"), 32), + Signature: bytesutil.PadTo([]byte("test"), 96), }, } @@ -209,7 +210,7 @@ func TestProcessDeposit_IncompleteDeposit(t *testing.T) { } proof, err := trie.MerkleProof(0) require.NoError(t, err) - dataRoot, err := ssz.HashTreeRoot(deposit.Data) + dataRoot, err := deposit.Data.HashTreeRoot() require.NoError(t, err) deposit.Proof = proof diff --git a/beacon-chain/powchain/log_processing.go b/beacon-chain/powchain/log_processing.go index fe5d886bd7..ed440bfafd 100644 --- a/beacon-chain/powchain/log_processing.go +++ b/beacon-chain/powchain/log_processing.go @@ -13,7 +13,6 @@ import ( gethTypes "github.com/ethereum/go-ethereum/core/types" "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" @@ -135,7 +134,7 @@ func (s *Service) ProcessDepositLog(ctx context.Context, depositLog gethTypes.Lo WithdrawalCredentials: withdrawalCredentials, } - depositHash, err := ssz.HashTreeRoot(depositData) + depositHash, err := depositData.HashTreeRoot() if err != nil { return errors.Wrap(err, "Unable to determine hashed value of deposit") } diff --git a/beacon-chain/rpc/beacon/BUILD.bazel b/beacon-chain/rpc/beacon/BUILD.bazel index c7311127e0..57410f7a9e 100644 --- a/beacon-chain/rpc/beacon/BUILD.bazel +++ b/beacon-chain/rpc/beacon/BUILD.bazel @@ -111,7 +111,6 @@ go_test( "@com_github_golang_mock//gomock:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@in_gopkg_d4l3k_messagediff_v1//:go_default_library", ], ) diff --git a/beacon-chain/rpc/beacon/assignments_test.go b/beacon-chain/rpc/beacon/assignments_test.go index 9ba7e4bd94..f997faf405 100644 --- a/beacon-chain/rpc/beacon/assignments_test.go +++ b/beacon-chain/rpc/beacon/assignments_test.go @@ -10,7 +10,6 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" @@ -55,7 +54,7 @@ func TestServer_ListAssignments_NoResults(t *testing.T) { ctx := context.Background() st := testutil.NewBeaconState() - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -96,7 +95,7 @@ func TestServer_ListAssignments_Pagination_InputOutOfRange(t *testing.T) { headState, err := db.HeadState(ctx) require.NoError(t, err) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -141,29 +140,30 @@ func TestServer_ListAssignments_Pagination_DefaultPageSize_NoArchive(t *testing. validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { pubKey := make([]byte, params.BeaconConfig().BLSPubkeyLength) + withdrawalCred := make([]byte, 32) binary.LittleEndian.PutUint64(pubKey, uint64(i)) // Mark the validators with index divisible by 3 inactive. if i%3 == 0 { validators = append(validators, ðpb.Validator{ - PublicKey: pubKey, - ExitEpoch: 0, - ActivationEpoch: 0, - EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, + PublicKey: pubKey, + WithdrawalCredentials: withdrawalCred, + ExitEpoch: 0, + ActivationEpoch: 0, + EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, }) } else { validators = append(validators, ðpb.Validator{ - PublicKey: pubKey, - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, - ActivationEpoch: 0, + PublicKey: pubKey, + WithdrawalCredentials: withdrawalCred, + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, + ActivationEpoch: 0, }) } } - blk := ðpb.BeaconBlock{ - Slot: 0, - } - blockRoot, err := ssz.HashTreeRoot(blk) + blk := testutil.NewBeaconBlock().Block + blockRoot, err := blk.HashTreeRoot() require.NoError(t, err) s := testutil.NewBeaconState() @@ -221,16 +221,20 @@ func TestServer_ListAssignments_FilterPubkeysIndices_NoPagination(t *testing.T) ctx := context.Background() count := 100 validators := make([]*ethpb.Validator, 0, count) + withdrawCreds := make([]byte, 32) for i := 0; i < count; i++ { pubKey := make([]byte, params.BeaconConfig().BLSPubkeyLength) binary.LittleEndian.PutUint64(pubKey, uint64(i)) - validators = append(validators, ðpb.Validator{PublicKey: pubKey, ExitEpoch: params.BeaconConfig().FarFutureEpoch}) + val := ðpb.Validator{ + PublicKey: pubKey, + WithdrawalCredentials: withdrawCreds, + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + } + validators = append(validators, val) } - blk := ðpb.BeaconBlock{ - Slot: 0, - } - blockRoot, err := ssz.HashTreeRoot(blk) + blk := testutil.NewBeaconBlock().Block + blockRoot, err := blk.HashTreeRoot() require.NoError(t, err) s := testutil.NewBeaconState() require.NoError(t, s.SetValidators(validators)) @@ -285,16 +289,20 @@ func TestServer_ListAssignments_CanFilterPubkeysIndices_WithPagination(t *testin ctx := context.Background() count := 100 validators := make([]*ethpb.Validator, 0, count) + withdrawCred := make([]byte, 32) for i := 0; i < count; i++ { pubKey := make([]byte, params.BeaconConfig().BLSPubkeyLength) binary.LittleEndian.PutUint64(pubKey, uint64(i)) - validators = append(validators, ðpb.Validator{PublicKey: pubKey, ExitEpoch: params.BeaconConfig().FarFutureEpoch}) + val := ðpb.Validator{ + PublicKey: pubKey, + WithdrawalCredentials: withdrawCred, + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + } + validators = append(validators, val) } - blk := ðpb.BeaconBlock{ - Slot: 0, - } - blockRoot, err := ssz.HashTreeRoot(blk) + blk := testutil.NewBeaconBlock().Block + blockRoot, err := blk.HashTreeRoot() require.NoError(t, err) s := testutil.NewBeaconState() require.NoError(t, s.SetValidators(validators)) diff --git a/beacon-chain/rpc/beacon/attestations_test.go b/beacon-chain/rpc/beacon/attestations_test.go index 312397fb17..e301478f50 100644 --- a/beacon-chain/rpc/beacon/attestations_test.go +++ b/beacon-chain/rpc/beacon/attestations_test.go @@ -88,7 +88,8 @@ func TestServer_ListAttestations_Genesis(t *testing.T) { t.Fatal(err) } att := ðpb.Attestation{ - Signature: make([]byte, 96), + AggregationBits: bitfield.NewBitlist(0), + Signature: make([]byte, 96), Data: ðpb.AttestationData{ Slot: 2, CommitteeIndex: 1, @@ -99,17 +100,12 @@ func TestServer_ListAttestations_Genesis(t *testing.T) { } parentRoot := [32]byte{1, 2, 3} - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{ - Slot: 0, - ParentRoot: parentRoot[:], - Body: ðpb.BeaconBlockBody{ - Attestations: []*ethpb.Attestation{att}, - }, - }, - } - root, err := stateutil.BlockRoot(blk.Block) + signedBlock := testutil.NewBeaconBlock() + signedBlock.Block.ParentRoot = bytesutil.PadTo(parentRoot[:], 32) + signedBlock.Block.Body.Attestations = []*ethpb.Attestation{att} + root, err := stateutil.BlockRoot(signedBlock.Block) require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, blk)) + require.NoError(t, db.SaveBlock(ctx, signedBlock)) require.NoError(t, db.SaveGenesisBlockRoot(ctx, root)) wanted := ðpb.ListAttestationsResponse{ Attestations: []*ethpb.Attestation{att}, @@ -129,7 +125,7 @@ func TestServer_ListAttestations_Genesis(t *testing.T) { // Should throw an error if there is more than 1 block // for the genesis slot. - require.NoError(t, db.SaveBlock(ctx, blk)) + require.NoError(t, db.SaveBlock(ctx, signedBlock)) if _, err := bs.ListAttestations(ctx, ðpb.ListAttestationsRequest{ QueryFilter: ðpb.ListAttestationsRequest_GenesisEpoch{ GenesisEpoch: true, @@ -146,23 +142,17 @@ func TestServer_ListAttestations_NoPagination(t *testing.T) { count := uint64(8) atts := make([]*ethpb.Attestation, 0, count) for i := uint64(0); i < count; i++ { - blockExample := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: i, - Body: ðpb.BeaconBlockBody{ - Attestations: []*ethpb.Attestation{ - { - Signature: make([]byte, 96), - Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("root"), 32)}, - Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("root"), 32)}, - BeaconBlockRoot: bytesutil.PadTo([]byte("root"), 32), - Slot: i, - }, - AggregationBits: bitfield.Bitlist{0b11}, - }, - }, + blockExample := testutil.NewBeaconBlock() + blockExample.Block.Body.Attestations = []*ethpb.Attestation{ + { + Signature: make([]byte, 96), + Data: ðpb.AttestationData{ + Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("root"), 32)}, + Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("root"), 32)}, + BeaconBlockRoot: bytesutil.PadTo([]byte("root"), 32), + Slot: i, }, + AggregationBits: bitfield.Bitlist{0b11}, }, } require.NoError(t, db.SaveBlock(ctx, blockExample)) @@ -216,8 +206,14 @@ func TestServer_ListAttestations_FiltersCorrectly(t *testing.T) { Slot: 3, }, AggregationBits: bitfield.Bitlist{0b11}, + Signature: bytesutil.PadTo([]byte("sig"), 96), }, }, + Eth1Data: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + BlockHash: make([]byte, 32), + }, + Graffiti: make([]byte, 32), }, }, }, @@ -244,8 +240,14 @@ func TestServer_ListAttestations_FiltersCorrectly(t *testing.T) { Slot: 4 + params.BeaconConfig().SlotsPerEpoch, }, AggregationBits: bitfield.Bitlist{0b11}, + Signature: bytesutil.PadTo([]byte("sig"), 96), }, }, + Eth1Data: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + BlockHash: make([]byte, 32), + }, + Graffiti: make([]byte, 32), }, }, }, @@ -272,8 +274,14 @@ func TestServer_ListAttestations_FiltersCorrectly(t *testing.T) { Slot: 4, }, AggregationBits: bitfield.Bitlist{0b11}, + Signature: bytesutil.PadTo([]byte("sig"), 96), }, }, + Eth1Data: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + BlockHash: make([]byte, 32), + }, + Graffiti: make([]byte, 32), }, }, }, @@ -305,24 +313,19 @@ func TestServer_ListAttestations_Pagination_CustomPageParameters(t *testing.T) { atts := make([]*ethpb.Attestation, 0, count) for i := uint64(0); i < params.BeaconConfig().SlotsPerEpoch; i++ { for s := uint64(0); s < 4; s++ { - blockExample := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: i, - Body: ðpb.BeaconBlockBody{ - Attestations: []*ethpb.Attestation{ - { - Data: ðpb.AttestationData{ - CommitteeIndex: s, - Slot: i, - BeaconBlockRoot: make([]byte, 32), - Source: ðpb.Checkpoint{Root: make([]byte, 32)}, - Target: ðpb.Checkpoint{Root: make([]byte, 32)}, - }, - AggregationBits: bitfield.Bitlist{0b11}, - Signature: make([]byte, 96), - }, - }, + blockExample := testutil.NewBeaconBlock() + blockExample.Block.Slot = i + blockExample.Block.Body.Attestations = []*ethpb.Attestation{ + { + Data: ðpb.AttestationData{ + CommitteeIndex: s, + Slot: i, + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, + AggregationBits: bitfield.Bitlist{0b11}, + Signature: make([]byte, 96), }, } require.NoError(t, db.SaveBlock(ctx, blockExample)) @@ -424,17 +427,24 @@ func TestServer_ListAttestations_Pagination_OutOfRange(t *testing.T) { ParentRoot: make([]byte, 32), StateRoot: make([]byte, 32), Body: ðpb.BeaconBlockBody{ - Graffiti: make([]byte, 32), RandaoReveal: make([]byte, 96), Attestations: []*ethpb.Attestation{ { Data: ðpb.AttestationData{ BeaconBlockRoot: bytesutil.PadTo([]byte("root"), 32), Slot: i, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b11}, + Signature: make([]byte, 96), }, }, + Eth1Data: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + BlockHash: make([]byte, 32), + }, + Graffiti: make([]byte, 32), }, }, } @@ -476,26 +486,17 @@ func TestServer_ListAttestations_Pagination_DefaultPageSize(t *testing.T) { count := uint64(params.BeaconConfig().DefaultPageSize) atts := make([]*ethpb.Attestation, 0, count) for i := uint64(0); i < count; i++ { - blockExample := ðpb.SignedBeaconBlock{ - Signature: make([]byte, 96), - Block: ðpb.BeaconBlock{ - ParentRoot: make([]byte, 32), - StateRoot: make([]byte, 32), - Body: ðpb.BeaconBlockBody{ - RandaoReveal: make([]byte, 96), - Attestations: []*ethpb.Attestation{ - { - Data: ðpb.AttestationData{ - BeaconBlockRoot: bytesutil.PadTo([]byte("root"), 32), - Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("root"), 32)}, - Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("root"), 32)}, - Slot: i, - }, - Signature: bytesutil.PadTo([]byte("root"), 96), - AggregationBits: bitfield.Bitlist{0b11}, - }, - }, + blockExample := testutil.NewBeaconBlock() + blockExample.Block.Body.Attestations = []*ethpb.Attestation{ + { + Data: ðpb.AttestationData{ + BeaconBlockRoot: bytesutil.PadTo([]byte("root"), 32), + Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("root"), 32)}, + Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("root"), 32)}, + Slot: i, }, + Signature: bytesutil.PadTo([]byte("root"), 96), + AggregationBits: bitfield.Bitlist{0b11}, }, } require.NoError(t, db.SaveBlock(ctx, blockExample)) @@ -569,27 +570,22 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { } else { targetRoot = targetRoot2 } - blockExample := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Body: ðpb.BeaconBlockBody{ - Attestations: []*ethpb.Attestation{ - { - Signature: make([]byte, 96), - Data: ðpb.AttestationData{ - BeaconBlockRoot: make([]byte, 32), - Target: ðpb.Checkpoint{ - Root: targetRoot[:], - }, - Source: ðpb.Checkpoint{ - Root: make([]byte, 32), - }, - Slot: i, - CommitteeIndex: 0, - }, - AggregationBits: bitfield.Bitlist{0b11}, - }, + blockExample := testutil.NewBeaconBlock() + blockExample.Block.Body.Attestations = []*ethpb.Attestation{ + { + Signature: make([]byte, 96), + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{ + Root: targetRoot[:], }, + Source: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + Slot: i, + CommitteeIndex: 0, }, + AggregationBits: bitfield.Bitlist{0b11}, }, } require.NoError(t, db.SaveBlock(ctx, blockExample)) @@ -757,9 +753,36 @@ func TestServer_AttestationPool_Pagination_OutOfRange(t *testing.T) { } atts := []*ethpb.Attestation{ - {Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}}, - {Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}}, - {Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}}, + { + Data: ðpb.AttestationData{ + Slot: 1, + BeaconBlockRoot: bytesutil.PadTo([]byte{1}, 32), + Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{1}, 32)}, + Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{1}, 32)}, + }, + AggregationBits: bitfield.Bitlist{0b1101}, + Signature: bytesutil.PadTo([]byte{1}, 96), + }, + { + Data: ðpb.AttestationData{ + Slot: 2, + BeaconBlockRoot: bytesutil.PadTo([]byte{2}, 32), + Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{2}, 32)}, + Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{2}, 32)}, + }, + AggregationBits: bitfield.Bitlist{0b1101}, + Signature: bytesutil.PadTo([]byte{2}, 96), + }, + { + Data: ðpb.AttestationData{ + Slot: 3, + BeaconBlockRoot: bytesutil.PadTo([]byte{3}, 32), + Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{3}, 32)}, + Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{3}, 32)}, + }, + AggregationBits: bitfield.Bitlist{0b1101}, + Signature: bytesutil.PadTo([]byte{3}, 96), + }, } require.NoError(t, bs.AttestationsPool.SaveAggregatedAttestations(atts)) @@ -780,10 +803,9 @@ func TestServer_AttestationPool_Pagination_DefaultPageSize(t *testing.T) { atts := make([]*ethpb.Attestation, params.BeaconConfig().DefaultPageSize+1) for i := 0; i < len(atts); i++ { - atts[i] = ðpb.Attestation{ - Data: ðpb.AttestationData{Slot: uint64(i)}, - AggregationBits: bitfield.Bitlist{0b1101}, - } + att := testutil.NewAttestation() + att.Data.Slot = uint64(i) + atts[i] = att } require.NoError(t, bs.AttestationsPool.SaveAggregatedAttestations(atts)) @@ -803,10 +825,9 @@ func TestServer_AttestationPool_Pagination_CustomPageSize(t *testing.T) { numAtts := 100 atts := make([]*ethpb.Attestation, numAtts) for i := 0; i < len(atts); i++ { - atts[i] = ðpb.Attestation{ - Data: ðpb.AttestationData{Slot: uint64(i)}, - AggregationBits: bitfield.Bitlist{0b1101}, - } + att := testutil.NewAttestation() + att.Data.Slot = uint64(i) + atts[i] = att } require.NoError(t, bs.AttestationsPool.SaveAggregatedAttestations(atts)) tests := []struct { @@ -893,7 +914,7 @@ func TestServer_StreamIndexedAttestations_OK(t *testing.T) { numValidators := 64 headState, privKeys := testutil.DeterministicGenesisState(t, uint64(numValidators)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -936,6 +957,10 @@ func TestServer_StreamIndexedAttestations_OK(t *testing.T) { Data: ðpb.AttestationData{ BeaconBlockRoot: bytesutil.PadTo([]byte("root"), 32), Slot: i, + Source: ðpb.Checkpoint{ + Epoch: 0, + Root: gRoot[:], + }, Target: ðpb.Checkpoint{ Epoch: 0, Root: gRoot[:], @@ -1059,9 +1084,9 @@ func TestServer_StreamAttestations_OnSlotTick(t *testing.T) { } atts := []*ethpb.Attestation{ - {Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}}, - {Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}}, - {Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}}, + {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}}, + {Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}}, + {Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1101}}, } mockStream := mock.NewMockBeaconChain_StreamAttestationsServer(ctrl) diff --git a/beacon-chain/rpc/beacon/blocks_test.go b/beacon-chain/rpc/beacon/blocks_test.go index 73dfb8dc7b..416b9bea58 100644 --- a/beacon-chain/rpc/beacon/blocks_test.go +++ b/beacon-chain/rpc/beacon/blocks_test.go @@ -123,12 +123,8 @@ func TestServer_ListBlocks_Genesis_MultiBlocks(t *testing.T) { } // Should return the proper genesis block if it exists. parentRoot := [32]byte{1, 2, 3} - blk := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: 0, - ParentRoot: parentRoot[:], - }, - } + blk := testutil.NewBeaconBlock() + blk.Block.ParentRoot = parentRoot[:] root, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, db.SaveBlock(ctx, blk)) @@ -138,11 +134,8 @@ func TestServer_ListBlocks_Genesis_MultiBlocks(t *testing.T) { blks := make([]*ethpb.SignedBeaconBlock, count) blkContainers := make([]*ethpb.BeaconBlockContainer, count) for i := uint64(0); i < count; i++ { - b := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: i, - }, - } + b := testutil.NewBeaconBlock() + b.Block.Slot = i root, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) blks[i] = b diff --git a/beacon-chain/rpc/beacon/committees_test.go b/beacon-chain/rpc/beacon/committees_test.go index 8db4afc8d5..362be3a623 100644 --- a/beacon-chain/rpc/beacon/committees_test.go +++ b/beacon-chain/rpc/beacon/committees_test.go @@ -44,7 +44,7 @@ func TestServer_ListBeaconCommittees_CurrentEpoch(t *testing.T) { GenesisTimeFetcher: m, StateGen: stategen.New(db, sc), } - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -89,7 +89,7 @@ func TestServer_ListBeaconCommittees_PreviousEpoch(t *testing.T) { require.NoError(t, headState.SetRandaoMixes(mixes)) require.NoError(t, headState.SetSlot(params.BeaconConfig().SlotsPerEpoch*2)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -159,7 +159,7 @@ func TestRetrieveCommitteesForRoot(t *testing.T) { GenesisTimeFetcher: m, StateGen: stategen.New(db, sc), } - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) diff --git a/beacon-chain/rpc/beacon/validators_test.go b/beacon-chain/rpc/beacon/validators_test.go index d60709d402..2728b5bee1 100644 --- a/beacon-chain/rpc/beacon/validators_test.go +++ b/beacon-chain/rpc/beacon/validators_test.go @@ -14,7 +14,6 @@ import ( ptypes "github.com/gogo/protobuf/types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" - "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute" @@ -103,7 +102,7 @@ func TestServer_ListValidatorBalances_NoResults(t *testing.T) { } headState := testutil.NewBeaconState() - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -153,7 +152,7 @@ func TestServer_ListValidatorBalances_DefaultResponse_NoArchive(t *testing.T) { require.NoError(t, st.SetSlot(0)) require.NoError(t, st.SetValidators(validators)) require.NoError(t, st.SetBalances(balances)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -181,7 +180,7 @@ func TestServer_ListValidatorBalances_PaginationOutOfRange(t *testing.T) { ctx := context.Background() setupValidators(t, db, 3) st := testutil.NewBeaconState() - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -230,7 +229,7 @@ func TestServer_ListValidatorBalances_Pagination_Default(t *testing.T) { setupValidators(t, db, 100) headState, err := db.HeadState(context.Background()) require.NoError(t, err) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot)) @@ -315,7 +314,7 @@ func TestServer_ListValidatorBalances_Pagination_CustomPageSizes(t *testing.T) { setupValidators(t, db, count) headState, err := db.HeadState(context.Background()) require.NoError(t, err) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot)) @@ -386,7 +385,7 @@ func TestServer_ListValidatorBalances_OutOfRange(t *testing.T) { headState, err := db.HeadState(context.Background()) require.NoError(t, err) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot)) @@ -525,7 +524,7 @@ func TestServer_ListValidators_OnlyActiveValidators(t *testing.T) { StateGen: stategen.New(db, cache.NewStateSummaryCache()), } - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -896,7 +895,7 @@ func TestServer_ListValidators_FromOldEpoch(t *testing.T) { st := testutil.NewBeaconState() require.NoError(t, st.SetSlot(helpers.StartSlot(30))) require.NoError(t, st.SetValidators(validators)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -962,7 +961,7 @@ func TestServer_ListValidators_ProcessHeadStateSlots(t *testing.T) { require.NoError(t, st.SetSlot(headSlot)) require.NoError(t, st.SetValidators(validators)) require.NoError(t, st.SetBalances(balances)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -1124,7 +1123,7 @@ func TestServer_GetValidatorActiveSetChanges(t *testing.T) { }) require.NoError(t, err) } - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) @@ -1134,7 +1133,7 @@ func TestServer_GetValidatorActiveSetChanges(t *testing.T) { bs := &Server{ FinalizationFetcher: &mock.ChainService{ - FinalizedCheckPoint: ðpb.Checkpoint{Epoch: 0}, + FinalizedCheckPoint: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, }, GenesisTimeFetcher: &mock.ChainService{}, StateGen: stategen.New(db, sc), @@ -1234,19 +1233,19 @@ func TestServer_GetValidatorQueue_ExitedValidatorLeavesQueue(t *testing.T) { ActivationEpoch: 0, ExitEpoch: params.BeaconConfig().FarFutureEpoch, WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, - PublicKey: []byte("1"), + PublicKey: bytesutil.PadTo([]byte("1"), 48), }, { ActivationEpoch: 0, ExitEpoch: 4, WithdrawableEpoch: 6, - PublicKey: []byte("2"), + PublicKey: bytesutil.PadTo([]byte("2"), 48), }, } headState := testutil.NewBeaconState() require.NoError(t, headState.SetValidators(validators)) - require.NoError(t, headState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 0})) + require.NoError(t, headState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)})) bs := &Server{ HeadFetcher: &mock.ChainService{ State: headState, @@ -1257,7 +1256,7 @@ func TestServer_GetValidatorQueue_ExitedValidatorLeavesQueue(t *testing.T) { res, err := bs.GetValidatorQueue(context.Background(), &ptypes.Empty{}) require.NoError(t, err) wanted := [][]byte{ - []byte("2"), + bytesutil.PadTo([]byte("2"), 48), } activeValidatorCount, err := helpers.ActiveValidatorCount(headState, helpers.CurrentEpoch(headState)) require.NoError(t, err) @@ -1369,20 +1368,35 @@ func TestServer_GetValidatorParticipation_PrevEpoch(t *testing.T) { balances := make([]uint64, validatorCount) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, + PublicKey: bytesutil.ToBytes(uint64(i), 48), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, } balances[i] = params.BeaconConfig().MaxEffectiveBalance } - atts := []*pbp2p.PendingAttestation{{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{}}, InclusionDelay: 1}} + atts := []*pbp2p.PendingAttestation{{ + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + }, + InclusionDelay: 1, + AggregationBits: bitfield.NewBitlist(2), + }} headState := testutil.NewBeaconState() require.NoError(t, headState.SetSlot(params.BeaconConfig().SlotsPerEpoch)) require.NoError(t, headState.SetValidators(validators)) require.NoError(t, headState.SetBalances(balances)) require.NoError(t, headState.SetPreviousEpochAttestations(atts)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: params.BeaconConfig().SlotsPerEpoch}} + b := testutil.NewBeaconBlock() + b.Block.Slot = params.BeaconConfig().SlotsPerEpoch require.NoError(t, db.SaveBlock(ctx, b)) bRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -1412,7 +1426,8 @@ func TestServer_GetValidatorParticipation_DoesntExist(t *testing.T) { headState := testutil.NewBeaconState() require.NoError(t, headState.SetSlot(params.BeaconConfig().SlotsPerEpoch)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: params.BeaconConfig().SlotsPerEpoch}} + b := testutil.NewBeaconBlock() + b.Block.Slot = params.BeaconConfig().SlotsPerEpoch require.NoError(t, db.SaveBlock(ctx, b)) bRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -1456,8 +1471,8 @@ func TestGetValidatorPerformance_OK(t *testing.T) { for i := 0; i < len(atts); i++ { atts[i] = &pb.PendingAttestation{ Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{}, - Source: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0xC0, 0xC0, 0xC0, 0xC0, 0x01}, InclusionDelay: 1, @@ -1701,10 +1716,8 @@ func setupValidators(t testing.TB, db db.Database, count int) ([]*ethpb.Validato WithdrawalCredentials: make([]byte, 32), }) } - blk := ðpb.BeaconBlock{ - Slot: 0, - } - blockRoot, err := ssz.HashTreeRoot(blk) + blk := testutil.NewBeaconBlock().Block + blockRoot, err := blk.HashTreeRoot() require.NoError(t, err) s := testutil.NewBeaconState() require.NoError(t, s.SetValidators(validators)) @@ -1817,12 +1830,10 @@ func TestServer_GetIndividualVotes_Working(t *testing.T) { require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch)) bf := []byte{0xff} - att1 := ðpb.Attestation{Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{Epoch: 0}}, - AggregationBits: bf} - att2 := ðpb.Attestation{Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{Epoch: 0}}, - AggregationBits: bf} + att1 := testutil.NewAttestation() + att1.AggregationBits = bf + att2 := testutil.NewAttestation() + att2.AggregationBits = bf rt := [32]byte{'A'} att1.Data.Target.Root = rt[:] att1.Data.BeaconBlockRoot = rt[:] diff --git a/beacon-chain/rpc/debug/block_test.go b/beacon-chain/rpc/debug/block_test.go index e45ba50b53..a5c08ee407 100644 --- a/beacon-chain/rpc/debug/block_test.go +++ b/beacon-chain/rpc/debug/block_test.go @@ -22,9 +22,9 @@ import ( func TestServer_GetBlock(t *testing.T) { db, _ := dbTest.SetupDB(t) ctx := context.Background() - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{ - Slot: 100, - }} + + b := testutil.NewBeaconBlock() + b.Block.Slot = 100 require.NoError(t, db.SaveBlock(ctx, b)) blockRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -66,19 +66,17 @@ func TestServer_GetAttestationInclusionSlot(t *testing.T) { a := ðpb.Attestation{ Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{Root: tr[:]}, - Slot: 1, + Target: ðpb.Checkpoint{Root: tr[:]}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), + Slot: 1, }, AggregationBits: bitfield.Bitlist{0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, + Signature: make([]byte, 96), } - b := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: 2, - Body: ðpb.BeaconBlockBody{ - Attestations: []*ethpb.Attestation{a}, - }, - }, - } + b := testutil.NewBeaconBlock() + b.Block.Slot = 2 + b.Block.Body.Attestations = []*ethpb.Attestation{a} require.NoError(t, bs.BeaconDB.SaveBlock(ctx, b)) res, err := bs.GetInclusionSlot(ctx, &pbrpc.InclusionSlotRequest{Slot: 1, Id: c[0]}) require.NoError(t, err) diff --git a/beacon-chain/rpc/debug/state_test.go b/beacon-chain/rpc/debug/state_test.go index cd5f287ef6..b27dddcb45 100644 --- a/beacon-chain/rpc/debug/state_test.go +++ b/beacon-chain/rpc/debug/state_test.go @@ -4,7 +4,6 @@ import ( "context" "testing" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" @@ -25,9 +24,8 @@ func TestServer_GetBeaconState(t *testing.T) { st := testutil.NewBeaconState() slot := uint64(100) require.NoError(t, st.SetSlot(slot)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{ - Slot: slot, - }} + b := testutil.NewBeaconBlock() + b.Block.Slot = slot require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) diff --git a/beacon-chain/rpc/validator/BUILD.bazel b/beacon-chain/rpc/validator/BUILD.bazel index 46a0e2f4f9..bc4048231c 100644 --- a/beacon-chain/rpc/validator/BUILD.bazel +++ b/beacon-chain/rpc/validator/BUILD.bazel @@ -54,7 +54,6 @@ go_library( "@com_github_gogo_protobuf//types:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@io_opencensus_go//trace:go_default_library", "@org_golang_google_grpc//codes:go_default_library", @@ -117,7 +116,6 @@ go_test( "@com_github_golang_mock//gomock:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@com_github_sirupsen_logrus//hooks/test:go_default_library", ], diff --git a/beacon-chain/rpc/validator/aggregator_test.go b/beacon-chain/rpc/validator/aggregator_test.go index 4fd985c664..a45d0e58ad 100644 --- a/beacon-chain/rpc/validator/aggregator_test.go +++ b/beacon-chain/rpc/validator/aggregator_test.go @@ -218,11 +218,13 @@ func generateAtt(state *beaconstate.BeaconState, index uint64, privKeys []bls.Se aggBits.SetBitAt(index+1, true) att := ðpb.Attestation{ Data: ðpb.AttestationData{ - CommitteeIndex: 1, - Source: ðpb.Checkpoint{Epoch: 0, Root: params.BeaconConfig().ZeroHash[:]}, - Target: ðpb.Checkpoint{Epoch: 0}, + CommitteeIndex: 1, + Source: ðpb.Checkpoint{Epoch: 0, Root: params.BeaconConfig().ZeroHash[:]}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, AggregationBits: aggBits, + Signature: make([]byte, 96), } committee, err := helpers.BeaconCommitteeFromState(state, att.Data.Slot, att.Data.CommitteeIndex) if err != nil { @@ -256,11 +258,13 @@ func generateUnaggregatedAtt(state *beaconstate.BeaconState, index uint64, privK aggBits.SetBitAt(index, true) att := ðpb.Attestation{ Data: ðpb.AttestationData{ - CommitteeIndex: 1, - Source: ðpb.Checkpoint{Epoch: 0, Root: params.BeaconConfig().ZeroHash[:]}, - Target: ðpb.Checkpoint{Epoch: 0}, + CommitteeIndex: 1, + Source: ðpb.Checkpoint{Epoch: 0, Root: params.BeaconConfig().ZeroHash[:]}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, AggregationBits: aggBits, + Signature: make([]byte, 96), } committee, err := helpers.BeaconCommitteeFromState(state, att.Data.Slot, att.Data.CommitteeIndex) if err != nil { diff --git a/beacon-chain/rpc/validator/assignments_test.go b/beacon-chain/rpc/validator/assignments_test.go index 00dfcd300c..74af282940 100644 --- a/beacon-chain/rpc/validator/assignments_test.go +++ b/beacon-chain/rpc/validator/assignments_test.go @@ -10,7 +10,6 @@ import ( "github.com/golang/mock/gomock" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" @@ -266,7 +265,7 @@ func TestStreamDuties_OK(t *testing.T) { require.NoError(t, err) bs, err := state.GenesisBeaconState(deposits, 0, eth1Data) require.NoError(t, err, "Could not setup genesis bs") - genesisRoot, err := ssz.HashTreeRoot(genesis.Block) + genesisRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") pubKeys := make([][]byte, len(deposits)) @@ -325,7 +324,7 @@ func TestStreamDuties_OK_ChainReorg(t *testing.T) { require.NoError(t, err) bs, err := state.GenesisBeaconState(deposits, 0, eth1Data) require.NoError(t, err, "Could not setup genesis bs") - genesisRoot, err := ssz.HashTreeRoot(genesis.Block) + genesisRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") pubKeys := make([][]byte, len(deposits)) diff --git a/beacon-chain/rpc/validator/attester_test.go b/beacon-chain/rpc/validator/attester_test.go index a6ab4e0010..b7f9358921 100644 --- a/beacon-chain/rpc/validator/attester_test.go +++ b/beacon-chain/rpc/validator/attester_test.go @@ -9,7 +9,6 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" @@ -52,8 +51,10 @@ func TestProposeAttestation_OK(t *testing.T) { validators := make([]*ethpb.Validator, 64) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, + PublicKey: make([]byte, 48), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, } } @@ -69,8 +70,8 @@ func TestProposeAttestation_OK(t *testing.T) { Signature: sig.Marshal(), Data: ðpb.AttestationData{ BeaconBlockRoot: root[:], - Source: ðpb.Checkpoint{}, - Target: ðpb.Checkpoint{}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, }, } _, err = attesterServer.ProposeAttestation(context.Background(), req) @@ -91,9 +92,11 @@ func TestProposeAttestation_IncorrectSignature(t *testing.T) { req := ðpb.Attestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{}, - Target: ðpb.Checkpoint{}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), } wanted := "Incorrect attestation signature" _, err := attesterServer.ProposeAttestation(context.Background(), req) @@ -104,20 +107,17 @@ func TestGetAttestationData_OK(t *testing.T) { ctx := context.Background() db, _ := dbutil.SetupDB(t) - block := ðpb.BeaconBlock{ - Slot: 3*params.BeaconConfig().SlotsPerEpoch + 1, - } - targetBlock := ðpb.BeaconBlock{ - Slot: 1 * params.BeaconConfig().SlotsPerEpoch, - } - justifiedBlock := ðpb.BeaconBlock{ - Slot: 2 * params.BeaconConfig().SlotsPerEpoch, - } - blockRoot, err := stateutil.BlockRoot(block) + block := testutil.NewBeaconBlock() + block.Block.Slot = 3*params.BeaconConfig().SlotsPerEpoch + 1 + targetBlock := testutil.NewBeaconBlock() + targetBlock.Block.Slot = 1 * params.BeaconConfig().SlotsPerEpoch + justifiedBlock := testutil.NewBeaconBlock() + justifiedBlock.Block.Slot = 2 * params.BeaconConfig().SlotsPerEpoch + blockRoot, err := stateutil.BlockRoot(block.Block) require.NoError(t, err, "Could not hash beacon block") - justifiedRoot, err := stateutil.BlockRoot(justifiedBlock) + justifiedRoot, err := stateutil.BlockRoot(justifiedBlock.Block) require.NoError(t, err, "Could not get signing root for justified block") - targetRoot, err := stateutil.BlockRoot(targetBlock) + targetRoot, err := stateutil.BlockRoot(targetBlock.Block) require.NoError(t, err, "Could not get signing root for target block") slot := 3*params.BeaconConfig().SlotsPerEpoch + 1 beaconState := testutil.NewBeaconState() @@ -153,7 +153,7 @@ func TestGetAttestationData_OK(t *testing.T) { StateNotifier: chainService.StateNotifier(), } require.NoError(t, db.SaveState(ctx, beaconState, blockRoot)) - require.NoError(t, db.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block})) + require.NoError(t, db.SaveBlock(ctx, block)) require.NoError(t, db.SaveHeadBlockRoot(ctx, blockRoot)) req := ðpb.AttestationDataRequest{ @@ -206,20 +206,17 @@ func TestAttestationDataAtSlot_HandlesFarAwayJustifiedEpoch(t *testing.T) { cfg.HistoricalRootsLimit = 8192 params.OverrideBeaconConfig(cfg) - block := ðpb.BeaconBlock{ - Slot: 10000, - } - epochBoundaryBlock := ðpb.BeaconBlock{ - Slot: helpers.StartSlot(helpers.SlotToEpoch(10000)), - } - justifiedBlock := ðpb.BeaconBlock{ - Slot: helpers.StartSlot(helpers.SlotToEpoch(1500)) - 2, // Imagine two skip block - } - blockRoot, err := stateutil.BlockRoot(block) + block := testutil.NewBeaconBlock() + block.Block.Slot = 10000 + epochBoundaryBlock := testutil.NewBeaconBlock() + epochBoundaryBlock.Block.Slot = helpers.StartSlot(helpers.SlotToEpoch(10000)) + justifiedBlock := testutil.NewBeaconBlock() + justifiedBlock.Block.Slot = helpers.StartSlot(helpers.SlotToEpoch(1500)) - 2 // Imagine two skip block + blockRoot, err := stateutil.BlockRoot(block.Block) require.NoError(t, err, "Could not hash beacon block") - justifiedBlockRoot, err := stateutil.BlockRoot(justifiedBlock) + justifiedBlockRoot, err := stateutil.BlockRoot(justifiedBlock.Block) require.NoError(t, err, "Could not hash justified block") - epochBoundaryRoot, err := stateutil.BlockRoot(epochBoundaryBlock) + epochBoundaryRoot, err := stateutil.BlockRoot(epochBoundaryBlock.Block) require.NoError(t, err, "Could not hash justified block") slot := uint64(10000) @@ -251,7 +248,7 @@ func TestAttestationDataAtSlot_HandlesFarAwayJustifiedEpoch(t *testing.T) { StateNotifier: chainService.StateNotifier(), } require.NoError(t, db.SaveState(ctx, beaconState, blockRoot)) - require.NoError(t, db.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block})) + require.NoError(t, db.SaveBlock(ctx, block)) require.NoError(t, db.SaveHeadBlockRoot(ctx, blockRoot)) req := ðpb.AttestationDataRequest{ @@ -302,7 +299,7 @@ func TestAttestationDataSlot_handlesInProgressRequest(t *testing.T) { } res := ðpb.AttestationData{ - Target: ðpb.Checkpoint{Epoch: 55}, + Target: ðpb.Checkpoint{Epoch: 55, Root: make([]byte, 32)}, } require.NoError(t, server.AttestationCache.MarkInProgress(req)) @@ -355,24 +352,22 @@ func TestServer_GetAttestationData_HeadStateSlotGreaterThanRequestSlot(t *testin db, sc := dbutil.SetupDB(t) slot := 3*params.BeaconConfig().SlotsPerEpoch + 1 - block := ðpb.BeaconBlock{ - Slot: slot, - } - block2 := ðpb.BeaconBlock{Slot: slot - 1} - targetBlock := ðpb.BeaconBlock{ - Slot: 1 * params.BeaconConfig().SlotsPerEpoch, - } - justifiedBlock := ðpb.BeaconBlock{ - Slot: 2 * params.BeaconConfig().SlotsPerEpoch, - } - blockRoot, err := stateutil.BlockRoot(block) + block := testutil.NewBeaconBlock() + block.Block.Slot = slot + block2 := testutil.NewBeaconBlock() + block2.Block.Slot = slot - 1 + targetBlock := testutil.NewBeaconBlock() + targetBlock.Block.Slot = 1 * params.BeaconConfig().SlotsPerEpoch + justifiedBlock := testutil.NewBeaconBlock() + justifiedBlock.Block.Slot = 2 * params.BeaconConfig().SlotsPerEpoch + blockRoot, err := stateutil.BlockRoot(block.Block) require.NoError(t, err, "Could not hash beacon block") - blockRoot2, err := ssz.HashTreeRoot(block2) + blockRoot2, err := block2.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block2})) - justifiedRoot, err := stateutil.BlockRoot(justifiedBlock) + require.NoError(t, db.SaveBlock(ctx, block2)) + justifiedRoot, err := stateutil.BlockRoot(justifiedBlock.Block) require.NoError(t, err, "Could not get signing root for justified block") - targetRoot, err := stateutil.BlockRoot(targetBlock) + targetRoot, err := stateutil.BlockRoot(targetBlock.Block) require.NoError(t, err, "Could not get signing root for target block") beaconState := testutil.NewBeaconState() @@ -414,7 +409,7 @@ func TestServer_GetAttestationData_HeadStateSlotGreaterThanRequestSlot(t *testin StateGen: stategen.New(db, sc), } require.NoError(t, db.SaveState(ctx, beaconState, blockRoot)) - require.NoError(t, db.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block})) + require.NoError(t, db.SaveBlock(ctx, block)) require.NoError(t, db.SaveHeadBlockRoot(ctx, blockRoot)) req := ðpb.AttestationDataRequest{ @@ -447,20 +442,17 @@ func TestGetAttestationData_SucceedsInFirstEpoch(t *testing.T) { db, _ := dbutil.SetupDB(t) slot := uint64(5) - block := ðpb.BeaconBlock{ - Slot: slot, - } - targetBlock := ðpb.BeaconBlock{ - Slot: 0, - } - justifiedBlock := ðpb.BeaconBlock{ - Slot: 0, - } - blockRoot, err := stateutil.BlockRoot(block) + block := testutil.NewBeaconBlock() + block.Block.Slot = slot + targetBlock := testutil.NewBeaconBlock() + targetBlock.Block.Slot = 0 + justifiedBlock := testutil.NewBeaconBlock() + justifiedBlock.Block.Slot = 0 + blockRoot, err := stateutil.BlockRoot(block.Block) require.NoError(t, err, "Could not hash beacon block") - justifiedRoot, err := stateutil.BlockRoot(justifiedBlock) + justifiedRoot, err := stateutil.BlockRoot(justifiedBlock.Block) require.NoError(t, err, "Could not get signing root for justified block") - targetRoot, err := stateutil.BlockRoot(targetBlock) + targetRoot, err := stateutil.BlockRoot(targetBlock.Block) require.NoError(t, err, "Could not get signing root for target block") beaconState := testutil.NewBeaconState() @@ -493,7 +485,7 @@ func TestGetAttestationData_SucceedsInFirstEpoch(t *testing.T) { StateNotifier: chainService.StateNotifier(), } require.NoError(t, db.SaveState(ctx, beaconState, blockRoot)) - require.NoError(t, db.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block})) + require.NoError(t, db.SaveBlock(ctx, block)) require.NoError(t, db.SaveHeadBlockRoot(ctx, blockRoot)) req := ðpb.AttestationDataRequest{ diff --git a/beacon-chain/rpc/validator/proposer.go b/beacon-chain/rpc/validator/proposer.go index 1d34cceb9f..cf2ea1a7a5 100644 --- a/beacon-chain/rpc/validator/proposer.go +++ b/beacon-chain/rpc/validator/proposer.go @@ -12,7 +12,6 @@ import ( fastssz "github.com/ferranbt/fastssz" "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" @@ -541,7 +540,7 @@ func (vs *Server) depositTrie(ctx context.Context, canonicalEth1DataHeight *big. insertIndex := finalizedDeposits.MerkleTrieIndex + 1 for _, dep := range upToEth1DataDeposits { - depHash, err := ssz.HashTreeRoot(dep.Data) + depHash, err := dep.Data.HashTreeRoot() if err != nil { return nil, errors.Wrap(err, "could not hash deposit data") } @@ -555,7 +554,7 @@ func (vs *Server) depositTrie(ctx context.Context, canonicalEth1DataHeight *big. upToEth1DataDeposits := vs.DepositFetcher.AllDeposits(ctx, canonicalEth1DataHeight) depositData := [][]byte{} for _, dep := range upToEth1DataDeposits { - depHash, err := ssz.HashTreeRoot(dep.Data) + depHash, err := dep.Data.HashTreeRoot() if err != nil { return nil, errors.Wrap(err, "could not hash deposit data") } diff --git a/beacon-chain/rpc/validator/proposer_test.go b/beacon-chain/rpc/validator/proposer_test.go index 3389f61da4..9b52c3f9de 100644 --- a/beacon-chain/rpc/validator/proposer_test.go +++ b/beacon-chain/rpc/validator/proposer_test.go @@ -11,7 +11,6 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" - "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" @@ -277,19 +276,10 @@ func TestComputeStateRoot_OK(t *testing.T) { Eth1BlockFetcher: &mockPOW.POWChain{}, StateGen: stategen.New(db, sc), } - req := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: 21, - ParentRoot: parentRoot[:], - Slot: 1, - Body: ðpb.BeaconBlockBody{ - RandaoReveal: nil, - ProposerSlashings: nil, - AttesterSlashings: nil, - Eth1Data: ðpb.Eth1Data{}, - }, - }, - } + req := testutil.NewBeaconBlock() + req.Block.ProposerIndex = 21 + req.Block.ParentRoot = parentRoot[:] + req.Block.Slot = 1 require.NoError(t, beaconState.SetSlot(beaconState.Slot()+1)) randaoReveal, err := testutil.RandaoReveal(beaconState, 0, privKeys) require.NoError(t, err) @@ -334,22 +324,17 @@ func TestPendingDeposits_Eth1DataVoteOK(t *testing.T) { blockHash = make([]byte, 32) copy(blockHash, "0x0") - beaconState, err := beaconstate.InitializeFromProto(&pbp2p.BeaconState{ - Eth1Data: ðpb.Eth1Data{ - DepositRoot: make([]byte, 32), - BlockHash: blockHash, - DepositCount: 2, - }, - Eth1DepositIndex: 2, - Eth1DataVotes: votes, - }) - require.NoError(t, err) + beaconState := testutil.NewBeaconState() + require.NoError(t, beaconState.SetEth1DepositIndex(2)) + require.NoError(t, beaconState.SetEth1Data(ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + BlockHash: blockHash, + DepositCount: 2, + })) + require.NoError(t, beaconState.SetEth1DataVotes(votes)) - blk := ðpb.BeaconBlock{ - Body: ðpb.BeaconBlockBody{Eth1Data: ðpb.Eth1Data{}}, - } - - blkRoot, err := ssz.HashTreeRoot(blk) + blk := testutil.NewBeaconBlock() + blkRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) bs := &Server{ @@ -367,7 +352,7 @@ func TestPendingDeposits_Eth1DataVoteOK(t *testing.T) { assert.Equal(t, 0, eth1Height.Cmp(height)) - newState, err := b.ProcessEth1DataInBlock(beaconState, blk) + newState, err := b.ProcessEth1DataInBlock(beaconState, blk.Block) require.NoError(t, err) if proto.Equal(newState.Eth1Data(), vote) { @@ -375,15 +360,13 @@ func TestPendingDeposits_Eth1DataVoteOK(t *testing.T) { "have majority: Got %v", vote) } - blk = ðpb.BeaconBlock{ - Body: ðpb.BeaconBlockBody{Eth1Data: vote}, - } + blk.Block.Body.Eth1Data = vote _, eth1Height, err = bs.canonicalEth1Data(ctx, beaconState, vote) require.NoError(t, err) assert.Equal(t, 0, eth1Height.Cmp(newHeight)) - newState, err = b.ProcessEth1DataInBlock(beaconState, blk) + newState, err = b.ProcessEth1DataInBlock(beaconState, blk.Block) require.NoError(t, err) if !proto.Equal(newState.Eth1Data(), vote) { @@ -404,7 +387,8 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) { beaconState, err := beaconstate.InitializeFromProto(&pbp2p.BeaconState{ Eth1Data: ðpb.Eth1Data{ - BlockHash: []byte("0x0"), + BlockHash: bytesutil.PadTo([]byte("0x0"), 32), + DepositRoot: make([]byte, 32), }, Eth1DepositIndex: 2, }) @@ -420,7 +404,7 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) { Eth1BlockHeight: 2, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -430,7 +414,7 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) { Eth1BlockHeight: 8, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("b"), + PublicKey: bytesutil.PadTo([]byte("b"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -443,7 +427,7 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) { Eth1BlockHeight: 400, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("c"), + PublicKey: bytesutil.PadTo([]byte("c"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -453,7 +437,7 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) { Eth1BlockHeight: 600, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("d"), + PublicKey: bytesutil.PadTo([]byte("d"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -466,7 +450,7 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) { depositTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth)) require.NoError(t, err, "Could not setup deposit trie") for _, dp := range append(readyDeposits, recentDeposits...) { - depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) + depositHash, err := dp.Deposit.Data.HashTreeRoot() require.NoError(t, err, "Unable to determine hashed value of deposit") depositTrie.Insert(depositHash[:], int(dp.Index)) @@ -476,11 +460,10 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) { depositCache.InsertPendingDeposit(ctx, dp.Deposit, dp.Eth1BlockHeight, dp.Index, depositTrie.Root()) } - blk := ðpb.BeaconBlock{ - Slot: beaconState.Slot(), - } + blk := testutil.NewBeaconBlock() + blk.Block.Slot = beaconState.Slot() - blkRoot, err := ssz.HashTreeRoot(blk) + blkRoot, err := blk.HashTreeRoot() require.NoError(t, err) bs := &Server{ @@ -521,7 +504,8 @@ func TestPendingDeposits_FollowsCorrectEth1Block(t *testing.T) { var votes []*ethpb.Eth1Data vote := ðpb.Eth1Data{ - BlockHash: []byte("0x1"), + BlockHash: bytesutil.PadTo([]byte("0x1"), 32), + DepositRoot: make([]byte, 32), DepositCount: 7, } period := params.BeaconConfig().EpochsPerEth1VotingPeriod * params.BeaconConfig().SlotsPerEpoch @@ -532,17 +516,17 @@ func TestPendingDeposits_FollowsCorrectEth1Block(t *testing.T) { beaconState, err := beaconstate.InitializeFromProto(&pbp2p.BeaconState{ Eth1Data: ðpb.Eth1Data{ BlockHash: []byte("0x0"), + DepositRoot: make([]byte, 32), DepositCount: 5, }, Eth1DepositIndex: 1, Eth1DataVotes: votes, }) require.NoError(t, err) - blk := ðpb.BeaconBlock{ - Slot: beaconState.Slot(), - } + blk := testutil.NewBeaconBlock() + blk.Block.Slot = beaconState.Slot() - blkRoot, err := ssz.HashTreeRoot(blk) + blkRoot, err := blk.HashTreeRoot() require.NoError(t, err) var mockSig [96]byte @@ -555,7 +539,7 @@ func TestPendingDeposits_FollowsCorrectEth1Block(t *testing.T) { Eth1BlockHeight: 8, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -565,7 +549,7 @@ func TestPendingDeposits_FollowsCorrectEth1Block(t *testing.T) { Eth1BlockHeight: 14, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("b"), + PublicKey: bytesutil.PadTo([]byte("b"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -578,7 +562,7 @@ func TestPendingDeposits_FollowsCorrectEth1Block(t *testing.T) { Eth1BlockHeight: 5000, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("c"), + PublicKey: bytesutil.PadTo([]byte("c"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -588,7 +572,7 @@ func TestPendingDeposits_FollowsCorrectEth1Block(t *testing.T) { Eth1BlockHeight: 6000, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("d"), + PublicKey: bytesutil.PadTo([]byte("d"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -601,7 +585,7 @@ func TestPendingDeposits_FollowsCorrectEth1Block(t *testing.T) { depositTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth)) require.NoError(t, err, "Could not setup deposit trie") for _, dp := range append(readyDeposits, recentDeposits...) { - depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) + depositHash, err := dp.Deposit.Data.HashTreeRoot() require.NoError(t, err, "Unable to determine hashed value of deposit") depositTrie.Insert(depositHash[:], int(dp.Index)) @@ -644,18 +628,16 @@ func TestPendingDeposits_CantReturnBelowStateEth1DepositIndex(t *testing.T) { }, } - beaconState, err := beaconstate.InitializeFromProto(&pbp2p.BeaconState{ - Eth1Data: ðpb.Eth1Data{ - BlockHash: []byte("0x0"), - DepositCount: 100, - }, - Eth1DepositIndex: 10, - }) - require.NoError(t, err) - blk := ðpb.BeaconBlock{ - Slot: beaconState.Slot(), - } - blkRoot, err := ssz.HashTreeRoot(blk) + beaconState := testutil.NewBeaconState() + require.NoError(t, beaconState.SetEth1Data(ðpb.Eth1Data{ + BlockHash: bytesutil.PadTo([]byte("0x0"), 32), + DepositRoot: make([]byte, 32), + DepositCount: 100, + })) + require.NoError(t, beaconState.SetEth1DepositIndex(10)) + blk := testutil.NewBeaconBlock() + blk.Block.Slot = beaconState.Slot() + blkRoot, err := blk.HashTreeRoot() require.NoError(t, err) var mockSig [96]byte @@ -666,7 +648,7 @@ func TestPendingDeposits_CantReturnBelowStateEth1DepositIndex(t *testing.T) { Index: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -675,7 +657,7 @@ func TestPendingDeposits_CantReturnBelowStateEth1DepositIndex(t *testing.T) { Index: 1, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("b"), + PublicKey: bytesutil.PadTo([]byte("b"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -688,7 +670,7 @@ func TestPendingDeposits_CantReturnBelowStateEth1DepositIndex(t *testing.T) { Index: i, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{byte(i)}, + PublicKey: bytesutil.PadTo([]byte{byte(i)}, 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -701,7 +683,7 @@ func TestPendingDeposits_CantReturnBelowStateEth1DepositIndex(t *testing.T) { require.NoError(t, err) for _, dp := range append(readyDeposits, recentDeposits...) { - depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) + depositHash, err := dp.Deposit.Data.HashTreeRoot() require.NoError(t, err, "Unable to determine hashed value of deposit") depositTrie.Insert(depositHash[:], int(dp.Index)) @@ -743,16 +725,16 @@ func TestPendingDeposits_CantReturnMoreThanMax(t *testing.T) { beaconState, err := beaconstate.InitializeFromProto(&pbp2p.BeaconState{ Eth1Data: ðpb.Eth1Data{ - BlockHash: []byte("0x0"), + BlockHash: bytesutil.PadTo([]byte("0x0"), 32), + DepositRoot: make([]byte, 32), DepositCount: 100, }, Eth1DepositIndex: 2, }) require.NoError(t, err) - blk := ðpb.BeaconBlock{ - Slot: beaconState.Slot(), - } - blkRoot, err := ssz.HashTreeRoot(blk) + blk := testutil.NewBeaconBlock() + blk.Block.Slot = beaconState.Slot() + blkRoot, err := blk.HashTreeRoot() require.NoError(t, err) var mockSig [96]byte var mockCreds [32]byte @@ -762,7 +744,7 @@ func TestPendingDeposits_CantReturnMoreThanMax(t *testing.T) { Index: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -771,7 +753,7 @@ func TestPendingDeposits_CantReturnMoreThanMax(t *testing.T) { Index: 1, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("b"), + PublicKey: bytesutil.PadTo([]byte("b"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -784,7 +766,7 @@ func TestPendingDeposits_CantReturnMoreThanMax(t *testing.T) { Index: i, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{byte(i)}, + PublicKey: bytesutil.PadTo([]byte{byte(i)}, 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -797,7 +779,7 @@ func TestPendingDeposits_CantReturnMoreThanMax(t *testing.T) { require.NoError(t, err) for _, dp := range append(readyDeposits, recentDeposits...) { - depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) + depositHash, err := dp.Deposit.Data.HashTreeRoot() require.NoError(t, err, "Unable to determine hashed value of deposit") depositTrie.Insert(depositHash[:], int(dp.Index)) @@ -837,16 +819,16 @@ func TestPendingDeposits_CantReturnMoreThanDepositCount(t *testing.T) { beaconState, err := beaconstate.InitializeFromProto(&pbp2p.BeaconState{ Eth1Data: ðpb.Eth1Data{ - BlockHash: []byte("0x0"), + BlockHash: bytesutil.PadTo([]byte("0x0"), 32), + DepositRoot: make([]byte, 32), DepositCount: 5, }, Eth1DepositIndex: 2, }) require.NoError(t, err) - blk := ðpb.BeaconBlock{ - Slot: beaconState.Slot(), - } - blkRoot, err := ssz.HashTreeRoot(blk) + blk := testutil.NewBeaconBlock() + blk.Block.Slot = beaconState.Slot() + blkRoot, err := blk.HashTreeRoot() require.NoError(t, err) var mockSig [96]byte var mockCreds [32]byte @@ -856,7 +838,7 @@ func TestPendingDeposits_CantReturnMoreThanDepositCount(t *testing.T) { Index: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -865,7 +847,7 @@ func TestPendingDeposits_CantReturnMoreThanDepositCount(t *testing.T) { Index: 1, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("b"), + PublicKey: bytesutil.PadTo([]byte("b"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -878,7 +860,7 @@ func TestPendingDeposits_CantReturnMoreThanDepositCount(t *testing.T) { Index: i, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{byte(i)}, + PublicKey: bytesutil.PadTo([]byte{byte(i)}, 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -891,7 +873,7 @@ func TestPendingDeposits_CantReturnMoreThanDepositCount(t *testing.T) { require.NoError(t, err) for _, dp := range append(readyDeposits, recentDeposits...) { - depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) + depositHash, err := dp.Deposit.Data.HashTreeRoot() require.NoError(t, err, "Unable to determine hashed value of deposit") depositTrie.Insert(depositHash[:], int(dp.Index)) @@ -933,17 +915,17 @@ func TestDepositTrie_UtilizesCachedFinalizedDeposits(t *testing.T) { beaconState, err := beaconstate.InitializeFromProto(&pbp2p.BeaconState{ Eth1Data: ðpb.Eth1Data{ - BlockHash: []byte("0x0"), + BlockHash: bytesutil.PadTo([]byte("0x0"), 32), + DepositRoot: make([]byte, 32), DepositCount: 4, }, Eth1DepositIndex: 1, }) require.NoError(t, err) - blk := ðpb.BeaconBlock{ - Slot: beaconState.Slot(), - } + blk := testutil.NewBeaconBlock() + blk.Block.Slot = beaconState.Slot() - blkRoot, err := ssz.HashTreeRoot(blk) + blkRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) var mockSig [96]byte @@ -956,7 +938,7 @@ func TestDepositTrie_UtilizesCachedFinalizedDeposits(t *testing.T) { Eth1BlockHeight: 10, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -966,7 +948,7 @@ func TestDepositTrie_UtilizesCachedFinalizedDeposits(t *testing.T) { Eth1BlockHeight: 10, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("b"), + PublicKey: bytesutil.PadTo([]byte("b"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -979,7 +961,7 @@ func TestDepositTrie_UtilizesCachedFinalizedDeposits(t *testing.T) { Eth1BlockHeight: 11, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("c"), + PublicKey: bytesutil.PadTo([]byte("c"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -989,7 +971,7 @@ func TestDepositTrie_UtilizesCachedFinalizedDeposits(t *testing.T) { Eth1BlockHeight: 11, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("d"), + PublicKey: bytesutil.PadTo([]byte("d"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -1002,7 +984,7 @@ func TestDepositTrie_UtilizesCachedFinalizedDeposits(t *testing.T) { depositTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth)) require.NoError(t, err, "Could not setup deposit trie") for _, dp := range append(finalizedDeposits, recentDeposits...) { - depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) + depositHash, err := dp.Deposit.Data.HashTreeRoot() require.NoError(t, err, "Unable to determine hashed value of deposit") depositTrie.Insert(depositHash[:], int(dp.Index)) @@ -1063,7 +1045,7 @@ func TestDefaultEth1Data_NoBlockExists(t *testing.T) { Eth1BlockHeight: 8, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1073,7 +1055,7 @@ func TestDefaultEth1Data_NoBlockExists(t *testing.T) { Eth1BlockHeight: 14, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("b"), + PublicKey: bytesutil.PadTo([]byte("b"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1162,7 +1144,7 @@ func TestEth1Data_SmallerDepositCount(t *testing.T) { Eth1BlockHeight: 8, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1172,7 +1154,7 @@ func TestEth1Data_SmallerDepositCount(t *testing.T) { Eth1BlockHeight: 14, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("b"), + PublicKey: bytesutil.PadTo([]byte("b"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1277,7 +1259,7 @@ func TestEth1DataMajorityVote_ChooseHighestCount(t *testing.T) { Eth1BlockHeight: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1338,7 +1320,7 @@ func TestEth1DataMajorityVote_HighestCountBeforeRange_ChooseHighestCountWithinRa Eth1BlockHeight: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1398,7 +1380,7 @@ func TestEth1DataMajorityVote_HighestCountAfterRange_ChooseHighestCountWithinRan Eth1BlockHeight: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1458,7 +1440,7 @@ func TestEth1DataMajorityVote_HighestCountOnUnknownBlock_ChooseKnownBlockWithHig Eth1BlockHeight: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1520,7 +1502,7 @@ func TestEth1DataMajorityVote_NoVotesInRange_ChooseDefault(t *testing.T) { Eth1BlockHeight: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1580,7 +1562,7 @@ func TestEth1DataMajorityVote_NoVotes_ChooseDefault(t *testing.T) { Eth1BlockHeight: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1635,7 +1617,7 @@ func TestEth1DataMajorityVote_SameCount_ChooseMoreRecentBlock(t *testing.T) { Eth1BlockHeight: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: make([]byte, 96), WithdrawalCredentials: make([]byte, 32), }}, @@ -1702,9 +1684,14 @@ func TestFilterAttestation_OK(t *testing.T) { atts := make([]*ethpb.Attestation, 10) for i := 0; i < len(atts); i++ { - atts[i] = ðpb.Attestation{Data: ðpb.AttestationData{ - CommitteeIndex: uint64(i), - Target: ðpb.Checkpoint{}}, + atts[i] = ðpb.Attestation{ + Data: ðpb.AttestationData{ + CommitteeIndex: uint64(i), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), } } received, err := proposerServer.filterAttestationsForBlockInclusion(context.Background(), state, atts) @@ -1716,11 +1703,15 @@ func TestFilterAttestation_OK(t *testing.T) { for i := 0; i < len(atts); i++ { aggBits := bitfield.NewBitlist(2) aggBits.SetBitAt(0, true) - atts[i] = ðpb.Attestation{Data: ðpb.AttestationData{ - CommitteeIndex: uint64(i), - Target: ðpb.Checkpoint{}, - Source: ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}}, + atts[i] = ðpb.Attestation{ + Data: ðpb.AttestationData{ + CommitteeIndex: uint64(i), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}, + BeaconBlockRoot: make([]byte, 32), + }, AggregationBits: aggBits, + Signature: make([]byte, 96), } committee, err := helpers.BeaconCommitteeFromState(state, atts[i].Data.Slot, atts[i].Data.CommitteeIndex) assert.NoError(t, err) @@ -1765,7 +1756,7 @@ func Benchmark_Eth1Data(b *testing.B) { Index: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -1774,7 +1765,7 @@ func Benchmark_Eth1Data(b *testing.B) { Index: 1, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("b"), + PublicKey: bytesutil.PadTo([]byte("b"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -1802,10 +1793,9 @@ func Benchmark_Eth1Data(b *testing.B) { } hashesByHeight[numOfVotes+1] = []byte("stub") - blk := ðpb.BeaconBlock{ - Slot: beaconState.Slot(), - } - blkRoot, err := ssz.HashTreeRoot(blk) + blk := testutil.NewBeaconBlock() + blk.Block.Slot = beaconState.Slot() + blkRoot, err := blk.HashTreeRoot() require.NoError(b, err) currentHeight := params.BeaconConfig().Eth1FollowDistance + 5 @@ -1843,15 +1833,15 @@ func TestDeposits_ReturnsEmptyList_IfLatestEth1DataEqGenesisEth1Block(t *testing beaconState, err := beaconstate.InitializeFromProto(&pbp2p.BeaconState{ Eth1Data: ðpb.Eth1Data{ - BlockHash: []byte("0x0"), + BlockHash: bytesutil.PadTo([]byte("0x0"), 32), + DepositRoot: make([]byte, 32), }, Eth1DepositIndex: 2, }) require.NoError(t, err) - blk := ðpb.BeaconBlock{ - Slot: beaconState.Slot(), - } - blkRoot, err := ssz.HashTreeRoot(blk) + blk := testutil.NewBeaconBlock() + blk.Block.Slot = beaconState.Slot() + blkRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) var mockSig [96]byte @@ -1862,7 +1852,7 @@ func TestDeposits_ReturnsEmptyList_IfLatestEth1DataEqGenesisEth1Block(t *testing Index: 0, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("a"), + PublicKey: bytesutil.PadTo([]byte("a"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -1871,7 +1861,7 @@ func TestDeposits_ReturnsEmptyList_IfLatestEth1DataEqGenesisEth1Block(t *testing Index: 1, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte("b"), + PublicKey: bytesutil.PadTo([]byte("b"), 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -1884,7 +1874,7 @@ func TestDeposits_ReturnsEmptyList_IfLatestEth1DataEqGenesisEth1Block(t *testing Index: i, Deposit: ðpb.Deposit{ Data: ðpb.Deposit_Data{ - PublicKey: []byte{byte(i)}, + PublicKey: bytesutil.PadTo([]byte{byte(i)}, 48), Signature: mockSig[:], WithdrawalCredentials: mockCreds[:], }}, @@ -1897,7 +1887,7 @@ func TestDeposits_ReturnsEmptyList_IfLatestEth1DataEqGenesisEth1Block(t *testing require.NoError(t, err) for _, dp := range append(readyDeposits, recentDeposits...) { - depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) + depositHash, err := dp.Deposit.Data.HashTreeRoot() require.NoError(t, err, "Unable to determine hashed value of deposit") depositTrie.Insert(depositHash[:], int(dp.Index)) @@ -1930,9 +1920,8 @@ func TestDeleteAttsInPool_Aggregated(t *testing.T) { } sig := bls.RandKey().Sign([]byte("foo")).Marshal() - d := ðpb.AttestationData{} - aggregatedAtts := []*ethpb.Attestation{{Data: d, AggregationBits: bitfield.Bitlist{0b10101}, Signature: sig}, {Data: d, AggregationBits: bitfield.Bitlist{0b11010}, Signature: sig}} - unaggregatedAtts := []*ethpb.Attestation{{Data: d, AggregationBits: bitfield.Bitlist{0b1001}, Signature: sig}, {Data: d, AggregationBits: bitfield.Bitlist{0b0001}, Signature: sig}} + aggregatedAtts := []*ethpb.Attestation{{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b10101}, Signature: sig}, {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11010}, Signature: sig}} + unaggregatedAtts := []*ethpb.Attestation{{Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b1001}, Signature: sig}, {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b0001}, Signature: sig}} require.NoError(t, s.AttPool.SaveAggregatedAttestations(aggregatedAtts)) require.NoError(t, s.AttPool.SaveUnaggregatedAttestations(unaggregatedAtts)) @@ -1948,21 +1937,21 @@ func TestDeleteAttsInPool_Aggregated(t *testing.T) { func TestSortProfitableAtts(t *testing.T) { atts := []*ethpb.Attestation{ - {Data: ðpb.AttestationData{Slot: 4}, AggregationBits: bitfield.Bitlist{0b11100000}}, - {Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b11000000}}, - {Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b11100000}}, - {Data: ðpb.AttestationData{Slot: 4}, AggregationBits: bitfield.Bitlist{0b11110000}}, - {Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b11100000}}, - {Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b11000000}}, + {Data: ðpb.AttestationData{Slot: 4, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11100000}}, + {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11000000}}, + {Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11100000}}, + {Data: ðpb.AttestationData{Slot: 4, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11110000}}, + {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11100000}}, + {Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11000000}}, } sort.Sort(profitableAtts{atts: atts}) want := []*ethpb.Attestation{ - {Data: ðpb.AttestationData{Slot: 4}, AggregationBits: bitfield.Bitlist{0b11110000}}, - {Data: ðpb.AttestationData{Slot: 4}, AggregationBits: bitfield.Bitlist{0b11100000}}, - {Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b11000000}}, - {Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b11100000}}, - {Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b11100000}}, - {Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b11000000}}, + {Data: ðpb.AttestationData{Slot: 4, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11110000}}, + {Data: ðpb.AttestationData{Slot: 4, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11100000}}, + {Data: ðpb.AttestationData{Slot: 3, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11000000}}, + {Data: ðpb.AttestationData{Slot: 2, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11100000}}, + {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11100000}}, + {Data: ðpb.AttestationData{Slot: 1, BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{Root: make([]byte, 32)}, Source: ðpb.Checkpoint{Root: make([]byte, 32)}}, AggregationBits: bitfield.Bitlist{0b11000000}}, } require.DeepEqual(t, want, atts) } diff --git a/beacon-chain/rpc/validator/server_test.go b/beacon-chain/rpc/validator/server_test.go index 83af2ce8e6..c62f8da03e 100644 --- a/beacon-chain/rpc/validator/server_test.go +++ b/beacon-chain/rpc/validator/server_test.go @@ -20,6 +20,7 @@ import ( mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bls" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/event" "github.com/prysmaticlabs/prysm/shared/mock" "github.com/prysmaticlabs/prysm/shared/params" @@ -118,9 +119,10 @@ func TestWaitForActivation_ValidatorOriginallyExists(t *testing.T) { Slot: 4000, Validators: []*ethpb.Validator{ { - ActivationEpoch: 0, - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - PublicKey: pubKey1, + ActivationEpoch: 0, + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + PublicKey: pubKey1, + WithdrawalCredentials: make([]byte, 32), }, }, } @@ -129,7 +131,8 @@ func TestWaitForActivation_ValidatorOriginallyExists(t *testing.T) { require.NoError(t, err, "Could not get signing root") depData := ðpb.Deposit_Data{ PublicKey: pubKey1, - WithdrawalCredentials: []byte("hey"), + WithdrawalCredentials: bytesutil.PadTo([]byte("hey"), 32), + Signature: make([]byte, 96), } domain, err := helpers.ComputeDomain(params.BeaconConfig().DomainDeposit, nil, nil) require.NoError(t, err) diff --git a/beacon-chain/rpc/validator/status_test.go b/beacon-chain/rpc/validator/status_test.go index 84d9f17f45..58423c1190 100644 --- a/beacon-chain/rpc/validator/status_test.go +++ b/beacon-chain/rpc/validator/status_test.go @@ -17,6 +17,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -71,8 +72,8 @@ func TestValidatorStatus_Deposited(t *testing.T) { pubKey1 := pubKey(1) depData := ðpb.Deposit_Data{ PublicKey: pubKey1, - Signature: []byte("hi"), - WithdrawalCredentials: []byte("hey"), + Signature: bytesutil.PadTo([]byte("hi"), 96), + WithdrawalCredentials: bytesutil.PadTo([]byte("hey"), 32), } deposit := ðpb.Deposit{ Data: depData, @@ -129,10 +130,11 @@ func TestValidatorStatus_Pending(t *testing.T) { require.NoError(t, state.SetSlot(5000)) err = state.SetValidators([]*ethpb.Validator{ { - ActivationEpoch: params.BeaconConfig().FarFutureEpoch, - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, - PublicKey: pubKey, + ActivationEpoch: params.BeaconConfig().FarFutureEpoch, + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + PublicKey: pubKey, + WithdrawalCredentials: make([]byte, 32), }, }) require.NoError(t, err) @@ -141,8 +143,8 @@ func TestValidatorStatus_Pending(t *testing.T) { depData := ðpb.Deposit_Data{ PublicKey: pubKey, - Signature: []byte("hi"), - WithdrawalCredentials: []byte("hey"), + Signature: bytesutil.PadTo([]byte("hi"), 96), + WithdrawalCredentials: bytesutil.PadTo([]byte("hey"), 32), } deposit := ðpb.Deposit{ @@ -188,8 +190,8 @@ func TestValidatorStatus_Active(t *testing.T) { depData := ðpb.Deposit_Data{ PublicKey: pubKey, - Signature: []byte("hi"), - WithdrawalCredentials: []byte("hey"), + Signature: bytesutil.PadTo([]byte("hi"), 96), + WithdrawalCredentials: bytesutil.PadTo([]byte("hey"), 32), } deposit := ðpb.Deposit{ @@ -279,8 +281,8 @@ func TestValidatorStatus_Exiting(t *testing.T) { require.NoError(t, err) depData := ðpb.Deposit_Data{ PublicKey: pubKey, - Signature: []byte("hi"), - WithdrawalCredentials: []byte("hey"), + Signature: bytesutil.PadTo([]byte("hi"), 96), + WithdrawalCredentials: bytesutil.PadTo([]byte("hey"), 32), } deposit := ðpb.Deposit{ @@ -339,8 +341,8 @@ func TestValidatorStatus_Slashing(t *testing.T) { require.NoError(t, err) depData := ðpb.Deposit_Data{ PublicKey: pubKey, - Signature: []byte("hi"), - WithdrawalCredentials: []byte("hey"), + Signature: bytesutil.PadTo([]byte("hi"), 96), + WithdrawalCredentials: bytesutil.PadTo([]byte("hey"), 32), } deposit := ðpb.Deposit{ @@ -396,14 +398,15 @@ func TestValidatorStatus_Exited(t *testing.T) { state := testutil.NewBeaconState() require.NoError(t, state.SetSlot(slot)) err = state.SetValidators([]*ethpb.Validator{{ - PublicKey: pubKey, - WithdrawableEpoch: epoch + 1}, + PublicKey: pubKey, + WithdrawableEpoch: epoch + 1, + WithdrawalCredentials: make([]byte, 32)}, }) require.NoError(t, err) depData := ðpb.Deposit_Data{ PublicKey: pubKey, - Signature: []byte("hi"), - WithdrawalCredentials: []byte("hey"), + Signature: bytesutil.PadTo([]byte("hi"), 96), + WithdrawalCredentials: bytesutil.PadTo([]byte("hey"), 32), } deposit := ðpb.Deposit{ @@ -563,40 +566,46 @@ func TestValidatorStatus_CorrectActivationQueue(t *testing.T) { // Pending active because activation epoch is still defaulted at far future slot. validators := []*ethpb.Validator{ { - ActivationEpoch: 0, - PublicKey: pubKey(0), - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + ActivationEpoch: 0, + PublicKey: pubKey(0), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, }, { - ActivationEpoch: 0, - PublicKey: pubKey(1), - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + ActivationEpoch: 0, + PublicKey: pubKey(1), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, }, { - ActivationEpoch: 0, - PublicKey: pubKey(2), - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + ActivationEpoch: 0, + PublicKey: pubKey(2), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, }, { - ActivationEpoch: 0, - PublicKey: pubKey(3), - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + ActivationEpoch: 0, + PublicKey: pubKey(3), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, }, { - ActivationEpoch: currentSlot/params.BeaconConfig().SlotsPerEpoch + 1, - PublicKey: pbKey, - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + ActivationEpoch: currentSlot/params.BeaconConfig().SlotsPerEpoch + 1, + PublicKey: pbKey, + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, }, { - ActivationEpoch: currentSlot/params.BeaconConfig().SlotsPerEpoch + 4, - PublicKey: pubKey(5), - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + ActivationEpoch: currentSlot/params.BeaconConfig().SlotsPerEpoch + 4, + PublicKey: pubKey(5), + WithdrawalCredentials: make([]byte, 32), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, }, } state := testutil.NewBeaconState() @@ -613,8 +622,8 @@ func TestValidatorStatus_CorrectActivationQueue(t *testing.T) { for i := 0; i < 6; i++ { depData := ðpb.Deposit_Data{ PublicKey: pubKey(uint64(i)), - Signature: []byte("hi"), - WithdrawalCredentials: []byte("hey"), + Signature: bytesutil.PadTo([]byte("hi"), 96), + WithdrawalCredentials: bytesutil.PadTo([]byte("hey"), 32), } deposit := ðpb.Deposit{ @@ -655,8 +664,8 @@ func TestDepositBlockSlotAfterGenesisTime(t *testing.T) { depData := ðpb.Deposit_Data{ PublicKey: pubKey, - Signature: []byte("hi"), - WithdrawalCredentials: []byte("hey"), + Signature: bytesutil.PadTo([]byte("hi"), 96), + WithdrawalCredentials: bytesutil.PadTo([]byte("hey"), 32), } deposit := ðpb.Deposit{ @@ -718,8 +727,8 @@ func TestDepositBlockSlotBeforeGenesisTime(t *testing.T) { depData := ðpb.Deposit_Data{ PublicKey: pubKey, - Signature: []byte("hi"), - WithdrawalCredentials: []byte("hey"), + Signature: bytesutil.PadTo([]byte("hi"), 96), + WithdrawalCredentials: bytesutil.PadTo([]byte("hey"), 32), } deposit := ðpb.Deposit{ diff --git a/beacon-chain/state/state_trie_test.go b/beacon-chain/state/state_trie_test.go index 74a43e62be..30ed9a5e9a 100644 --- a/beacon-chain/state/state_trie_test.go +++ b/beacon-chain/state/state_trie_test.go @@ -6,7 +6,6 @@ import ( "testing" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/state" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" @@ -160,7 +159,7 @@ func TestBeaconState_HashTreeRoot(t *testing.T) { if err == nil && tt.error != "" { t.Errorf("Expected error, expected %v, recevied %v", tt.error, err) } - genericHTR, err := ssz.HashTreeRoot(testState.InnerStateUnsafe()) + genericHTR, err := testState.InnerStateUnsafe().HashTreeRoot() if err == nil && tt.error != "" { t.Errorf("Expected error, expected %v, recevied %v", tt.error, err) } @@ -231,7 +230,7 @@ func TestBeaconState_HashTreeRoot_FieldTrie(t *testing.T) { if err == nil && tt.error != "" { t.Errorf("Expected error, expected %v, recevied %v", tt.error, err) } - genericHTR, err := ssz.HashTreeRoot(testState.InnerStateUnsafe()) + genericHTR, err := testState.InnerStateUnsafe().HashTreeRoot() if err == nil && tt.error != "" { t.Errorf("Expected error, expected %v, recevied %v", tt.error, err) } diff --git a/beacon-chain/state/stategen/BUILD.bazel b/beacon-chain/state/stategen/BUILD.bazel index 1aa840a7bf..807148fe44 100644 --- a/beacon-chain/state/stategen/BUILD.bazel +++ b/beacon-chain/state/stategen/BUILD.bazel @@ -66,7 +66,6 @@ go_test( "//shared/testutil/require:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//hooks/test:go_default_library", ], ) diff --git a/beacon-chain/state/stategen/cold_test.go b/beacon-chain/state/stategen/cold_test.go index 1ef927ab74..a2313296bb 100644 --- a/beacon-chain/state/stategen/cold_test.go +++ b/beacon-chain/state/stategen/cold_test.go @@ -4,7 +4,6 @@ import ( "context" "testing" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/cache" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" @@ -58,7 +57,7 @@ func TestLoadStateByRoot_CanGet(t *testing.T) { service.slotsPerArchivedPoint = 1 beaconState, _ := testutil.DeterministicGenesisState(t, 32) - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + blk := testutil.NewBeaconBlock() blkRoot, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot)) @@ -84,7 +83,7 @@ func TestLoadColdStateBySlot_CanGet(t *testing.T) { service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + blk := testutil.NewBeaconBlock() blkRoot, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot)) diff --git a/beacon-chain/state/stategen/getter_test.go b/beacon-chain/state/stategen/getter_test.go index fb31dffa5b..14990a2445 100644 --- a/beacon-chain/state/stategen/getter_test.go +++ b/beacon-chain/state/stategen/getter_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/gogo/protobuf/proto" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/cache" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" @@ -24,7 +23,8 @@ func TestStateByRoot_ColdState(t *testing.T) { service.finalizedInfo.slot = 2 service.slotsPerArchivedPoint = 1 - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} + b := testutil.NewBeaconBlock() + b.Block.Slot = 1 require.NoError(t, db.SaveBlock(ctx, b)) bRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -55,7 +55,7 @@ func TestStateByRoot_HotStateUsingEpochBoundaryCacheNoReplay(t *testing.T) { beaconState, _ := testutil.DeterministicGenesisState(t, 32) require.NoError(t, beaconState.SetSlot(10)) - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + blk := testutil.NewBeaconBlock() blkRoot, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: blkRoot[:]})) @@ -72,12 +72,15 @@ func TestStateByRoot_HotStateUsingEpochBoundaryCacheWithReplay(t *testing.T) { service := New(db, ssc) beaconState, _ := testutil.DeterministicGenesisState(t, 32) - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + blk := testutil.NewBeaconBlock() blkRoot, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, service.epochBoundaryStateCache.put(blkRoot, beaconState)) targetSlot := uint64(10) - targetBlock := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 11, ParentRoot: blkRoot[:], ProposerIndex: 8}} + targetBlock := testutil.NewBeaconBlock() + targetBlock.Block.Slot = 11 + targetBlock.Block.ParentRoot = blkRoot[:] + targetBlock.Block.ProposerIndex = 8 require.NoError(t, service.beaconDB.SaveBlock(ctx, targetBlock)) targetRoot, err := stateutil.BlockRoot(targetBlock.Block) require.NoError(t, err) @@ -119,7 +122,7 @@ func TestStateByRootInitialSync_UseEpochStateCache(t *testing.T) { beaconState, _ := testutil.DeterministicGenesisState(t, 32) targetSlot := uint64(10) require.NoError(t, beaconState.SetSlot(targetSlot)) - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + blk := testutil.NewBeaconBlock() blkRoot, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, service.epochBoundaryStateCache.put(blkRoot, beaconState)) @@ -155,12 +158,14 @@ func TestStateByRootInitialSync_CanProcessUpTo(t *testing.T) { service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + blk := testutil.NewBeaconBlock() blkRoot, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, service.epochBoundaryStateCache.put(blkRoot, beaconState)) targetSlot := uint64(10) - targetBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 11, ParentRoot: blkRoot[:]}} + targetBlk := testutil.NewBeaconBlock() + targetBlk.Block.Slot = 11 + targetBlk.Block.ParentRoot = blkRoot[:] targetRoot, err := stateutil.BlockRoot(targetBlk.Block) require.NoError(t, err) require.NoError(t, service.beaconDB.SaveBlock(ctx, targetBlk)) @@ -186,7 +191,8 @@ func TestStateBySlot_ColdState(t *testing.T) { beaconState, _ := testutil.DeterministicGenesisState(t, 32) require.NoError(t, beaconState.SetSlot(1)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} + b := testutil.NewBeaconBlock() + b.Block.Slot = 1 require.NoError(t, db.SaveBlock(ctx, b)) bRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -214,7 +220,7 @@ func TestStateBySlot_HotStateDB(t *testing.T) { service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) bRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) diff --git a/beacon-chain/state/stategen/hot_test.go b/beacon-chain/state/stategen/hot_test.go index f538bfa495..2e2d65acbe 100644 --- a/beacon-chain/state/stategen/hot_test.go +++ b/beacon-chain/state/stategen/hot_test.go @@ -5,8 +5,6 @@ import ( "testing" "github.com/gogo/protobuf/proto" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/cache" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" @@ -66,7 +64,7 @@ func TestSaveHotState_NoSaveNotEpochBoundary(t *testing.T) { beaconState, _ := testutil.DeterministicGenesisState(t, 32) require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch-1)) r := [32]byte{'A'} - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -103,12 +101,15 @@ func TestLoadHoteStateByRoot_EpochBoundaryStateCanProcess(t *testing.T) { service := New(db, ssc) beaconState, _ := testutil.DeterministicGenesisState(t, 32) - gBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + gBlk := testutil.NewBeaconBlock() gBlkRoot, err := stateutil.BlockRoot(gBlk.Block) require.NoError(t, err) require.NoError(t, service.epochBoundaryStateCache.put(gBlkRoot, beaconState)) - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 11, ParentRoot: gBlkRoot[:], ProposerIndex: 8}} + blk := testutil.NewBeaconBlock() + blk.Block.Slot = 11 + blk.Block.ProposerIndex = 8 + blk.Block.ParentRoot = gBlkRoot[:] require.NoError(t, service.beaconDB.SaveBlock(ctx, blk)) blkRoot, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) @@ -131,7 +132,7 @@ func TestLoadHoteStateByRoot_FromDBBoundaryCase(t *testing.T) { service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + blk := testutil.NewBeaconBlock() blkRoot, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, service.epochBoundaryStateCache.put(blkRoot, beaconState)) @@ -155,7 +156,7 @@ func TestLoadHoteStateBySlot_CanAdvanceSlotUsingDB(t *testing.T) { db, _ := testDB.SetupDB(t) service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) gRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -175,22 +176,22 @@ func TestLastAncestorState_CanGetUsingDB(t *testing.T) { b0 := testutil.NewBeaconBlock() b0.Block.ParentRoot = bytesutil.PadTo([]byte{'a'}, 32) - r0, err := ssz.HashTreeRoot(b0.Block) + r0, err := b0.Block.HashTreeRoot() require.NoError(t, err) b1 := testutil.NewBeaconBlock() b1.Block.Slot = 1 b1.Block.ParentRoot = bytesutil.PadTo(r0[:], 32) - r1, err := ssz.HashTreeRoot(b1.Block) + r1, err := b1.Block.HashTreeRoot() require.NoError(t, err) b2 := testutil.NewBeaconBlock() b2.Block.Slot = 2 b2.Block.ParentRoot = bytesutil.PadTo(r1[:], 32) - r2, err := ssz.HashTreeRoot(b2.Block) + r2, err := b2.Block.HashTreeRoot() require.NoError(t, err) b3 := testutil.NewBeaconBlock() b3.Block.Slot = 3 b3.Block.ParentRoot = bytesutil.PadTo(r2[:], 32) - r3, err := ssz.HashTreeRoot(b3.Block) + r3, err := b3.Block.HashTreeRoot() require.NoError(t, err) b1State := testutil.NewBeaconState() @@ -214,22 +215,22 @@ func TestLastAncestorState_CanGetUsingHotCache(t *testing.T) { b0 := testutil.NewBeaconBlock() b0.Block.ParentRoot = bytesutil.PadTo([]byte{'a'}, 32) - r0, err := ssz.HashTreeRoot(b0.Block) + r0, err := b0.Block.HashTreeRoot() require.NoError(t, err) b1 := testutil.NewBeaconBlock() b1.Block.Slot = 1 b1.Block.ParentRoot = bytesutil.PadTo(r0[:], 32) - r1, err := ssz.HashTreeRoot(b1.Block) + r1, err := b1.Block.HashTreeRoot() require.NoError(t, err) b2 := testutil.NewBeaconBlock() b2.Block.Slot = 2 b2.Block.ParentRoot = bytesutil.PadTo(r1[:], 32) - r2, err := ssz.HashTreeRoot(b2.Block) + r2, err := b2.Block.HashTreeRoot() require.NoError(t, err) b3 := testutil.NewBeaconBlock() b3.Block.Slot = 3 b3.Block.ParentRoot = bytesutil.PadTo(r2[:], 32) - r3, err := ssz.HashTreeRoot(b3.Block) + r3, err := b3.Block.HashTreeRoot() require.NoError(t, err) b1State := testutil.NewBeaconState() diff --git a/beacon-chain/state/stategen/migrate_test.go b/beacon-chain/state/stategen/migrate_test.go index 5b2545ddb9..5bbd62bcfe 100644 --- a/beacon-chain/state/stategen/migrate_test.go +++ b/beacon-chain/state/stategen/migrate_test.go @@ -4,7 +4,6 @@ import ( "context" "testing" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/cache" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" @@ -20,7 +19,8 @@ func TestMigrateToCold_CanSaveFinalizedInfo(t *testing.T) { db, c := testDB.SetupDB(t) service := New(db, c) beaconState, _ := testutil.DeterministicGenesisState(t, 32) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} + b := testutil.NewBeaconBlock() + b.Block.Slot = 1 br, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) @@ -41,7 +41,8 @@ func TestMigrateToCold_HappyPath(t *testing.T) { beaconState, _ := testutil.DeterministicGenesisState(t, 32) stateSlot := uint64(1) require.NoError(t, beaconState.SetSlot(stateSlot)) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2}} + b := testutil.NewBeaconBlock() + b.Block.Slot = 2 fRoot, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) @@ -70,7 +71,8 @@ func TestMigrateToCold_RegeneratePath(t *testing.T) { beaconState, _ := testutil.DeterministicGenesisState(t, 32) stateSlot := uint64(1) require.NoError(t, beaconState.SetSlot(stateSlot)) - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2}} + blk := testutil.NewBeaconBlock() + blk.Block.Slot = 2 fRoot, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, service.beaconDB.SaveBlock(ctx, blk)) diff --git a/beacon-chain/state/stategen/replay_test.go b/beacon-chain/state/stategen/replay_test.go index f48a6b88ad..2220e1f1c6 100644 --- a/beacon-chain/state/stategen/replay_test.go +++ b/beacon-chain/state/stategen/replay_test.go @@ -6,7 +6,6 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/db" @@ -26,7 +25,7 @@ func TestComputeStateUpToSlot_GenesisState(t *testing.T) { service := New(db, cache.NewStateSummaryCache()) - gBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + gBlk := testutil.NewBeaconBlock() gRoot, err := stateutil.BlockRoot(gBlk.Block) require.NoError(t, err) require.NoError(t, service.beaconDB.SaveBlock(ctx, gBlk)) @@ -48,7 +47,7 @@ func TestComputeStateUpToSlot_CanProcessUpTo(t *testing.T) { service := New(db, cache.NewStateSummaryCache()) - gBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + gBlk := testutil.NewBeaconBlock() gRoot, err := stateutil.BlockRoot(gBlk.Block) require.NoError(t, err) require.NoError(t, service.beaconDB.SaveBlock(ctx, gBlk)) @@ -156,7 +155,7 @@ func TestLoadBlocks_SecondBranch(t *testing.T) { beaconDB: db, } - roots, savedBlocks, err := tree1(db, []byte{'A'}) + roots, savedBlocks, err := tree1(db, bytesutil.PadTo([]byte{'A'}, 32)) require.NoError(t, err) filteredBlocks, err := s.LoadBlocks(ctx, 0, 5, roots[5]) @@ -183,7 +182,7 @@ func TestLoadBlocks_ThirdBranch(t *testing.T) { beaconDB: db, } - roots, savedBlocks, err := tree1(db, []byte{'A'}) + roots, savedBlocks, err := tree1(db, bytesutil.PadTo([]byte{'A'}, 32)) require.NoError(t, err) filteredBlocks, err := s.LoadBlocks(ctx, 0, 7, roots[7]) @@ -212,7 +211,7 @@ func TestLoadBlocks_SameSlots(t *testing.T) { beaconDB: db, } - roots, savedBlocks, err := tree2(db, []byte{'A'}) + roots, savedBlocks, err := tree2(db, bytesutil.PadTo([]byte{'A'}, 32)) require.NoError(t, err) filteredBlocks, err := s.LoadBlocks(ctx, 0, 3, roots[6]) @@ -304,7 +303,7 @@ func TestLastSavedBlock_Genesis(t *testing.T) { finalizedInfo: &finalizedInfo{slot: 128}, } - gBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + gBlk := testutil.NewBeaconBlock() gRoot, err := stateutil.BlockRoot(gBlk.Block) require.NoError(t, err) require.NoError(t, s.beaconDB.SaveBlock(ctx, gBlk)) @@ -324,11 +323,14 @@ func TestLastSavedBlock_CanGet(t *testing.T) { finalizedInfo: &finalizedInfo{slot: 128}, } - b1 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: s.finalizedInfo.slot + 5}} + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = s.finalizedInfo.slot + 5 require.NoError(t, s.beaconDB.SaveBlock(ctx, b1)) - b2 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: s.finalizedInfo.slot + 10}} + b2 := testutil.NewBeaconBlock() + b2.Block.Slot = s.finalizedInfo.slot + 10 require.NoError(t, s.beaconDB.SaveBlock(ctx, b2)) - b3 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: s.finalizedInfo.slot + 20}} + b3 := testutil.NewBeaconBlock() + b3.Block.Slot = s.finalizedInfo.slot + 20 require.NoError(t, s.beaconDB.SaveBlock(ctx, b3)) savedRoot, savedSlot, err := s.lastSavedBlock(ctx, s.finalizedInfo.slot+100) @@ -362,7 +364,7 @@ func TestLastSavedState_Genesis(t *testing.T) { finalizedInfo: &finalizedInfo{slot: 128}, } - gBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + gBlk := testutil.NewBeaconBlock() gState := testutil.NewBeaconState() gRoot, err := stateutil.BlockRoot(gBlk.Block) require.NoError(t, err) @@ -383,9 +385,11 @@ func TestLastSavedState_CanGet(t *testing.T) { finalizedInfo: &finalizedInfo{slot: 128}, } - b1 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: s.finalizedInfo.slot + 5}} + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = s.finalizedInfo.slot + 5 require.NoError(t, s.beaconDB.SaveBlock(ctx, b1)) - b2 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: s.finalizedInfo.slot + 10}} + b2 := testutil.NewBeaconBlock() + b2.Block.Slot = s.finalizedInfo.slot + 10 require.NoError(t, s.beaconDB.SaveBlock(ctx, b2)) b2Root, err := stateutil.BlockRoot(b2.Block) require.NoError(t, err) @@ -393,7 +397,8 @@ func TestLastSavedState_CanGet(t *testing.T) { require.NoError(t, st.SetSlot(s.finalizedInfo.slot+10)) require.NoError(t, s.beaconDB.SaveState(ctx, st, b2Root)) - b3 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: s.finalizedInfo.slot + 20}} + b3 := testutil.NewBeaconBlock() + b3.Block.Slot = s.finalizedInfo.slot + 20 require.NoError(t, s.beaconDB.SaveBlock(ctx, b3)) savedState, err := s.lastSavedState(ctx, s.finalizedInfo.slot+100) @@ -411,7 +416,8 @@ func TestLastSavedState_NoSavedBlockState(t *testing.T) { finalizedInfo: &finalizedInfo{slot: 128}, } - b1 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 127}} + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 127 require.NoError(t, s.beaconDB.SaveBlock(ctx, b1)) _, err := s.lastSavedState(ctx, s.finalizedInfo.slot+1) @@ -465,58 +471,76 @@ func TestProcessStateUpToSlot_CanProcess(t *testing.T) { // \- B2 -- B4 -- B6 ----- B8 // \- B7 func tree1(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.SignedBeaconBlock, error) { - b0 := ðpb.BeaconBlock{Slot: 0, ParentRoot: genesisRoot} - r0, err := ssz.HashTreeRoot(b0) + b0 := testutil.NewBeaconBlock() + b0.Block.Slot = 0 + b0.Block.ParentRoot = genesisRoot + r0, err := b0.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b1 := ðpb.BeaconBlock{Slot: 1, ParentRoot: r0[:]} - r1, err := ssz.HashTreeRoot(b1) + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = r0[:] + r1, err := b1.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b2 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r1[:]} - r2, err := ssz.HashTreeRoot(b2) + b2 := testutil.NewBeaconBlock() + b2.Block.Slot = 2 + b2.Block.ParentRoot = r1[:] + r2, err := b2.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b3 := ðpb.BeaconBlock{Slot: 3, ParentRoot: r1[:]} - r3, err := ssz.HashTreeRoot(b3) + b3 := testutil.NewBeaconBlock() + b3.Block.Slot = 3 + b3.Block.ParentRoot = r1[:] + r3, err := b3.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b4 := ðpb.BeaconBlock{Slot: 4, ParentRoot: r2[:]} - r4, err := ssz.HashTreeRoot(b4) + b4 := testutil.NewBeaconBlock() + b4.Block.Slot = 4 + b4.Block.ParentRoot = r2[:] + r4, err := b4.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b5 := ðpb.BeaconBlock{Slot: 5, ParentRoot: r3[:]} - r5, err := ssz.HashTreeRoot(b5) + b5 := testutil.NewBeaconBlock() + b5.Block.Slot = 5 + b5.Block.ParentRoot = r3[:] + r5, err := b5.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b6 := ðpb.BeaconBlock{Slot: 6, ParentRoot: r4[:]} - r6, err := ssz.HashTreeRoot(b6) + b6 := testutil.NewBeaconBlock() + b6.Block.Slot = 6 + b6.Block.ParentRoot = r4[:] + r6, err := b6.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b7 := ðpb.BeaconBlock{Slot: 7, ParentRoot: r6[:]} - r7, err := ssz.HashTreeRoot(b7) + b7 := testutil.NewBeaconBlock() + b7.Block.Slot = 7 + b7.Block.ParentRoot = r6[:] + r7, err := b7.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b8 := ðpb.BeaconBlock{Slot: 8, ParentRoot: r6[:]} - r8, err := ssz.HashTreeRoot(b8) + b8 := testutil.NewBeaconBlock() + b8.Block.Slot = 8 + b8.Block.ParentRoot = r6[:] + r8, err := b8.Block.HashTreeRoot() if err != nil { return nil, nil, err } st := testutil.NewBeaconState() returnedBlocks := make([]*ethpb.SignedBeaconBlock, 0) - for _, b := range []*ethpb.BeaconBlock{b0, b1, b2, b3, b4, b5, b6, b7, b8} { + for _, b := range []*ethpb.SignedBeaconBlock{b0, b1, b2, b3, b4, b5, b6, b7, b8} { beaconBlock := testutil.NewBeaconBlock() - beaconBlock.Block.Slot = b.Slot - beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.ParentRoot, 32) + beaconBlock.Block.Slot = b.Block.Slot + beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) if err := db.SaveBlock(context.Background(), beaconBlock); err != nil { return nil, nil, err } @@ -535,49 +559,67 @@ func tree1(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.SignedBeaco // \- B2 // \- B2 -- B3 func tree2(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.SignedBeaconBlock, error) { - b0 := ðpb.BeaconBlock{Slot: 0, ParentRoot: genesisRoot} - r0, err := ssz.HashTreeRoot(b0) + b0 := testutil.NewBeaconBlock() + b0.Block.Slot = 0 + b0.Block.ParentRoot = genesisRoot + r0, err := b0.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b1 := ðpb.BeaconBlock{Slot: 1, ParentRoot: r0[:]} - r1, err := ssz.HashTreeRoot(b1) + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = r0[:] + r1, err := b1.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b21 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r1[:], StateRoot: []byte{'A'}} - r21, err := ssz.HashTreeRoot(b21) + b21 := testutil.NewBeaconBlock() + b21.Block.Slot = 2 + b21.Block.ParentRoot = r1[:] + b21.Block.StateRoot = bytesutil.PadTo([]byte{'A'}, 32) + r21, err := b21.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b22 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r1[:], StateRoot: []byte{'B'}} - r22, err := ssz.HashTreeRoot(b22) + b22 := testutil.NewBeaconBlock() + b22.Block.Slot = 2 + b22.Block.ParentRoot = r1[:] + b22.Block.StateRoot = bytesutil.PadTo([]byte{'B'}, 32) + r22, err := b22.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b23 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r1[:], StateRoot: []byte{'C'}} - r23, err := ssz.HashTreeRoot(b23) + b23 := testutil.NewBeaconBlock() + b23.Block.Slot = 2 + b23.Block.ParentRoot = r1[:] + b23.Block.StateRoot = bytesutil.PadTo([]byte{'C'}, 32) + r23, err := b23.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b24 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r1[:], StateRoot: []byte{'D'}} - r24, err := ssz.HashTreeRoot(b24) + b24 := testutil.NewBeaconBlock() + b24.Block.Slot = 2 + b24.Block.ParentRoot = r1[:] + b24.Block.StateRoot = bytesutil.PadTo([]byte{'D'}, 32) + r24, err := b24.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b3 := ðpb.BeaconBlock{Slot: 3, ParentRoot: r24[:]} - r3, err := ssz.HashTreeRoot(b3) + b3 := testutil.NewBeaconBlock() + b3.Block.Slot = 3 + b3.Block.ParentRoot = r24[:] + r3, err := b3.Block.HashTreeRoot() if err != nil { return nil, nil, err } st := testutil.NewBeaconState() returnedBlocks := make([]*ethpb.SignedBeaconBlock, 0) - for _, b := range []*ethpb.BeaconBlock{b0, b1, b21, b22, b23, b24, b3} { + for _, b := range []*ethpb.SignedBeaconBlock{b0, b1, b21, b22, b23, b24, b3} { beaconBlock := testutil.NewBeaconBlock() - beaconBlock.Block.Slot = b.Slot - beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.ParentRoot, 32) - beaconBlock.Block.StateRoot = bytesutil.PadTo(b.StateRoot, 32) + beaconBlock.Block.Slot = b.Block.Slot + beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) + beaconBlock.Block.StateRoot = bytesutil.PadTo(b.Block.StateRoot, 32) if err := db.SaveBlock(context.Background(), beaconBlock); err != nil { return nil, nil, err } @@ -596,44 +638,60 @@ func tree2(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.SignedBeaco // \- B2 // \- B2 func tree3(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.SignedBeaconBlock, error) { - b0 := ðpb.BeaconBlock{Slot: 0, ParentRoot: genesisRoot} - r0, err := ssz.HashTreeRoot(b0) + b0 := testutil.NewBeaconBlock() + b0.Block.Slot = 0 + b0.Block.ParentRoot = genesisRoot + r0, err := b0.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b1 := ðpb.BeaconBlock{Slot: 1, ParentRoot: r0[:]} - r1, err := ssz.HashTreeRoot(b1) + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = r0[:] + r1, err := b1.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b21 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r1[:], StateRoot: []byte{'A'}} - r21, err := ssz.HashTreeRoot(b21) + b21 := testutil.NewBeaconBlock() + b21.Block.Slot = 2 + b21.Block.ParentRoot = r1[:] + b21.Block.StateRoot = bytesutil.PadTo([]byte{'A'}, 32) + r21, err := b21.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b22 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r1[:], StateRoot: []byte{'B'}} - r22, err := ssz.HashTreeRoot(b22) + b22 := testutil.NewBeaconBlock() + b22.Block.Slot = 2 + b22.Block.ParentRoot = r1[:] + b22.Block.StateRoot = bytesutil.PadTo([]byte{'B'}, 32) + r22, err := b22.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b23 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r1[:], StateRoot: []byte{'C'}} - r23, err := ssz.HashTreeRoot(b23) + b23 := testutil.NewBeaconBlock() + b23.Block.Slot = 2 + b23.Block.ParentRoot = r1[:] + b23.Block.StateRoot = bytesutil.PadTo([]byte{'C'}, 32) + r23, err := b23.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b24 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r1[:], StateRoot: []byte{'D'}} - r24, err := ssz.HashTreeRoot(b24) + b24 := testutil.NewBeaconBlock() + b24.Block.Slot = 2 + b24.Block.ParentRoot = r1[:] + b24.Block.StateRoot = bytesutil.PadTo([]byte{'D'}, 32) + r24, err := b24.Block.HashTreeRoot() if err != nil { return nil, nil, err } st := testutil.NewBeaconState() returnedBlocks := make([]*ethpb.SignedBeaconBlock, 0) - for _, b := range []*ethpb.BeaconBlock{b0, b1, b21, b22, b23, b24} { + for _, b := range []*ethpb.SignedBeaconBlock{b0, b1, b21, b22, b23, b24} { beaconBlock := testutil.NewBeaconBlock() - beaconBlock.Block.Slot = b.Slot - beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.ParentRoot, 32) - beaconBlock.Block.StateRoot = bytesutil.PadTo(b.StateRoot, 32) + beaconBlock.Block.Slot = b.Block.Slot + beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) + beaconBlock.Block.StateRoot = bytesutil.PadTo(b.Block.StateRoot, 32) if err := db.SaveBlock(context.Background(), beaconBlock); err != nil { return nil, nil, err } @@ -653,39 +711,53 @@ func tree3(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.SignedBeaco // \- B2 // \- B2 func tree4(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.SignedBeaconBlock, error) { - b0 := ðpb.BeaconBlock{Slot: 0, ParentRoot: genesisRoot} - r0, err := ssz.HashTreeRoot(b0) + b0 := testutil.NewBeaconBlock() + b0.Block.Slot = 0 + b0.Block.ParentRoot = genesisRoot + r0, err := b0.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b21 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r0[:], StateRoot: []byte{'A'}} - r21, err := ssz.HashTreeRoot(b21) + b21 := testutil.NewBeaconBlock() + b21.Block.Slot = 2 + b21.Block.ParentRoot = r0[:] + b21.Block.StateRoot = bytesutil.PadTo([]byte{'A'}, 32) + r21, err := b21.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b22 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r0[:], StateRoot: []byte{'B'}} - r22, err := ssz.HashTreeRoot(b22) + b22 := testutil.NewBeaconBlock() + b22.Block.Slot = 2 + b22.Block.ParentRoot = r0[:] + b22.Block.StateRoot = bytesutil.PadTo([]byte{'B'}, 32) + r22, err := b22.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b23 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r0[:], StateRoot: []byte{'C'}} - r23, err := ssz.HashTreeRoot(b23) + b23 := testutil.NewBeaconBlock() + b23.Block.Slot = 2 + b23.Block.ParentRoot = r0[:] + b23.Block.StateRoot = bytesutil.PadTo([]byte{'C'}, 32) + r23, err := b23.Block.HashTreeRoot() if err != nil { return nil, nil, err } - b24 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r0[:], StateRoot: []byte{'D'}} - r24, err := ssz.HashTreeRoot(b24) + b24 := testutil.NewBeaconBlock() + b24.Block.Slot = 2 + b24.Block.ParentRoot = r0[:] + b24.Block.StateRoot = bytesutil.PadTo([]byte{'D'}, 32) + r24, err := b24.Block.HashTreeRoot() if err != nil { return nil, nil, err } st := testutil.NewBeaconState() returnedBlocks := make([]*ethpb.SignedBeaconBlock, 0) - for _, b := range []*ethpb.BeaconBlock{b0, b21, b22, b23, b24} { + for _, b := range []*ethpb.SignedBeaconBlock{b0, b21, b22, b23, b24} { beaconBlock := testutil.NewBeaconBlock() - beaconBlock.Block.Slot = b.Slot - beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.ParentRoot, 32) - beaconBlock.Block.StateRoot = bytesutil.PadTo(b.StateRoot, 32) + beaconBlock.Block.Slot = b.Block.Slot + beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) + beaconBlock.Block.StateRoot = bytesutil.PadTo(b.Block.StateRoot, 32) if err := db.SaveBlock(context.Background(), beaconBlock); err != nil { return nil, nil, err } @@ -704,10 +776,11 @@ func TestLoadFinalizedBlocks(t *testing.T) { s := &State{ beaconDB: db, } - gBlock := ðpb.SignedBeaconBlock{} + gBlock := testutil.NewBeaconBlock() gRoot, err := stateutil.BlockRoot(gBlock.Block) require.NoError(t, err) require.NoError(t, db.SaveBlock(ctx, gBlock)) + require.NoError(t, db.SaveGenesisBlockRoot(ctx, [32]byte{})) roots, _, err := tree1(db, gRoot[:]) require.NoError(t, err) diff --git a/beacon-chain/state/stateutil/BUILD.bazel b/beacon-chain/state/stateutil/BUILD.bazel index c07dd0e982..21d1abc5fa 100644 --- a/beacon-chain/state/stateutil/BUILD.bazel +++ b/beacon-chain/state/stateutil/BUILD.bazel @@ -61,7 +61,6 @@ go_test( "//shared/testutil/assert:go_default_library", "//shared/testutil/require:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", ], ) diff --git a/beacon-chain/state/stateutil/attestations_test.go b/beacon-chain/state/stateutil/attestations_test.go index 7fb2449724..ff5f4aadba 100644 --- a/beacon-chain/state/stateutil/attestations_test.go +++ b/beacon-chain/state/stateutil/attestations_test.go @@ -5,7 +5,6 @@ import ( "testing" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) @@ -24,7 +23,7 @@ func TestAttestationDataRoot_EqualGeneric(t *testing.T) { Epoch: 9, }, } - genericHtr, err := ssz.HashTreeRoot(attData) + genericHtr, err := attData.HashTreeRoot() require.NoError(t, err) dataHtr, err := AttestationDataRoot(attData) require.NoError(t, err) @@ -50,7 +49,7 @@ func BenchmarkAttestationDataRoot(b *testing.B) { } b.Run("generic", func(b *testing.B) { for i := 0; i < b.N; i++ { - _, err := ssz.HashTreeRoot(attData) + _, err := attData.HashTreeRoot() require.NoError(b, err) } }) diff --git a/beacon-chain/state/stateutil/blocks_test.go b/beacon-chain/state/stateutil/blocks_test.go index 34650762a3..0dad3b214c 100644 --- a/beacon-chain/state/stateutil/blocks_test.go +++ b/beacon-chain/state/stateutil/blocks_test.go @@ -4,7 +4,6 @@ import ( "testing" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -15,14 +14,14 @@ func TestBlockRoot(t *testing.T) { genState, keys := testutil.DeterministicGenesisState(t, 100) blk, err := testutil.GenerateFullBlock(genState, keys, testutil.DefaultBlockGenConfig(), 10) require.NoError(t, err) - expectedRoot, err := ssz.HashTreeRoot(blk.Block) + expectedRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) receivedRoot, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.Equal(t, expectedRoot, receivedRoot) blk, err = testutil.GenerateFullBlock(genState, keys, testutil.DefaultBlockGenConfig(), 100) require.NoError(t, err) - expectedRoot, err = ssz.HashTreeRoot(blk.Block) + expectedRoot, err = blk.Block.HashTreeRoot() require.NoError(t, err) receivedRoot, err = stateutil.BlockRoot(blk.Block) require.NoError(t, err) @@ -30,7 +29,15 @@ func TestBlockRoot(t *testing.T) { } func TestBlockBodyRoot_NilIsSameAsEmpty(t *testing.T) { - a, err := ssz.HashTreeRoot(ðpb.BeaconBlockBody{}) + a, err := (ðpb.BeaconBlockBody{ + RandaoReveal: make([]byte, 96), + Graffiti: make([]byte, 32), + Eth1Data: ðpb.Eth1Data{ + BlockHash: make([]byte, 32), + DepositCount: 0, + DepositRoot: make([]byte, 32), + }, + }).HashTreeRoot() require.NoError(t, err) b, err := stateutil.BlockBodyRoot(nil) require.NoError(t, err) diff --git a/beacon-chain/state/stateutil/state_root_test.go b/beacon-chain/state/stateutil/state_root_test.go index 563e9c254c..6cde79693d 100644 --- a/beacon-chain/state/stateutil/state_root_test.go +++ b/beacon-chain/state/stateutil/state_root_test.go @@ -7,7 +7,6 @@ import ( "testing" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/interop" "github.com/prysmaticlabs/prysm/shared/params" @@ -33,7 +32,7 @@ func BenchmarkHashTreeRoot_Generic_512(b *testing.B) { genesisState := setupGenesisState(b, 512) b.StartTimer() for i := 0; i < b.N; i++ { - _, err := ssz.HashTreeRoot(genesisState) + _, err := genesisState.HashTreeRoot() require.NoError(b, err) } } @@ -43,7 +42,7 @@ func BenchmarkHashTreeRoot_Generic_16384(b *testing.B) { genesisState := setupGenesisState(b, 16384) b.StartTimer() for i := 0; i < b.N; i++ { - _, err := ssz.HashTreeRoot(genesisState) + _, err := genesisState.HashTreeRoot() require.NoError(b, err) } } @@ -53,7 +52,7 @@ func BenchmarkHashTreeRoot_Generic_300000(b *testing.B) { genesisState := setupGenesisState(b, 300000) b.StartTimer() for i := 0; i < b.N; i++ { - _, err := ssz.HashTreeRoot(genesisState) + _, err := genesisState.HashTreeRoot() require.NoError(b, err) } } diff --git a/beacon-chain/sync/initial-sync/BUILD.bazel b/beacon-chain/sync/initial-sync/BUILD.bazel index bf85b947a7..b9d5e6d312 100644 --- a/beacon-chain/sync/initial-sync/BUILD.bazel +++ b/beacon-chain/sync/initial-sync/BUILD.bazel @@ -112,7 +112,6 @@ go_test( "//beacon-chain/p2p:go_default_library", "//beacon-chain/p2p/peers:go_default_library", "//beacon-chain/p2p/testing:go_default_library", - "//beacon-chain/state:go_default_library", "//beacon-chain/state/stateutil:go_default_library", "//beacon-chain/sync:go_default_library", "//proto/beacon/p2p/v1:go_default_library", @@ -122,6 +121,7 @@ go_test( "//shared/params:go_default_library", "//shared/roughtime:go_default_library", "//shared/sliceutil:go_default_library", + "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", "//shared/testutil/require:go_default_library", "@com_github_ethereum_go_ethereum//p2p/enr:go_default_library", diff --git a/beacon-chain/sync/initial-sync/blocks_fetcher_test.go b/beacon-chain/sync/initial-sync/blocks_fetcher_test.go index 066de81b09..2ed1f1adf4 100644 --- a/beacon-chain/sync/initial-sync/blocks_fetcher_test.go +++ b/beacon-chain/sync/initial-sync/blocks_fetcher_test.go @@ -21,11 +21,11 @@ import ( p2pm "github.com/prysmaticlabs/prysm/beacon-chain/p2p" "github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers" p2pt "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" - stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" "github.com/prysmaticlabs/prysm/shared/sliceutil" + "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" "github.com/sirupsen/logrus" @@ -256,11 +256,10 @@ func TestBlocksFetcher_RoundRobin(t *testing.T) { genesisRoot := cache.rootCache[0] cache.RUnlock() - err := beaconDB.SaveBlock(context.Background(), ð.SignedBeaconBlock{Block: ð.BeaconBlock{Slot: 0}}) + err := beaconDB.SaveBlock(context.Background(), testutil.NewBeaconBlock()) require.NoError(t, err) - st, err := stateTrie.InitializeFromProto(&p2ppb.BeaconState{}) - require.NoError(t, err) + st := testutil.NewBeaconState() mc := &mock.ChainService{ State: st, diff --git a/beacon-chain/sync/initial-sync/initial_sync_test.go b/beacon-chain/sync/initial-sync/initial_sync_test.go index 9420901975..8add624835 100644 --- a/beacon-chain/sync/initial-sync/initial_sync_test.go +++ b/beacon-chain/sync/initial-sync/initial_sync_test.go @@ -20,15 +20,16 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/flags" "github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers" p2pt "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" - stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" beaconsync "github.com/prysmaticlabs/prysm/beacon-chain/sync" p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/hashutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" "github.com/prysmaticlabs/prysm/shared/sliceutil" + "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" "github.com/sirupsen/logrus" @@ -82,11 +83,10 @@ func initializeTestServices(t *testing.T, blocks []uint64, peers []*peerData) (* genesisRoot := cache.rootCache[0] cache.RUnlock() - err := beaconDB.SaveBlock(context.Background(), ð.SignedBeaconBlock{Block: ð.BeaconBlock{Slot: 0}}) + err := beaconDB.SaveBlock(context.Background(), testutil.NewBeaconBlock()) require.NoError(t, err) - st, err := stateTrie.InitializeFromProto(&p2ppb.BeaconState{}) - require.NoError(t, err) + st := testutil.NewBeaconState() return &mock.ChainService{ State: st, @@ -202,12 +202,9 @@ func connectPeer(t *testing.T, host *p2pt.TestP2P, datum *peerData, peerStatus * cache.RLock() parentRoot := cache.rootCache[cache.parentSlotCache[slot]] cache.RUnlock() - blk := ð.SignedBeaconBlock{ - Block: ð.BeaconBlock{ - Slot: slot, - ParentRoot: parentRoot[:], - }, - } + blk := testutil.NewBeaconBlock() + blk.Block.Slot = slot + blk.Block.ParentRoot = parentRoot[:] // If forked peer, give a different parent root. if datum.forkedPeer { newRoot := hashutil.Hash(parentRoot[:]) @@ -236,7 +233,7 @@ func connectPeer(t *testing.T, host *p2pt.TestP2P, datum *peerData, peerStatus * ForkDigest: params.BeaconConfig().GenesisForkVersion, FinalizedRoot: []byte(fmt.Sprintf("finalized_root %d", datum.finalizedEpoch)), FinalizedEpoch: datum.finalizedEpoch, - HeadRoot: []byte("head_root"), + HeadRoot: bytesutil.PadTo([]byte("head_root"), 32), HeadSlot: datum.headSlot, }) diff --git a/beacon-chain/sync/initial-sync/round_robin_test.go b/beacon-chain/sync/initial-sync/round_robin_test.go index 4af5453e50..9d81c6931f 100644 --- a/beacon-chain/sync/initial-sync/round_robin_test.go +++ b/beacon-chain/sync/initial-sync/round_robin_test.go @@ -10,11 +10,10 @@ import ( dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/flags" p2pt "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" - stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" - p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/sliceutil" + "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) @@ -287,11 +286,10 @@ func TestService_roundRobinSync(t *testing.T) { genesisRoot := cache.rootCache[0] cache.RUnlock() - err := beaconDB.SaveBlock(context.Background(), ð.SignedBeaconBlock{Block: ð.BeaconBlock{Slot: 0}}) + err := beaconDB.SaveBlock(context.Background(), testutil.NewBeaconBlock()) require.NoError(t, err) - st, err := stateTrie.InitializeFromProto(&p2ppb.BeaconState{}) - require.NoError(t, err) + st := testutil.NewBeaconState() mc := &mock.ChainService{ State: st, Root: genesisRoot[:], @@ -326,15 +324,12 @@ func TestService_roundRobinSync(t *testing.T) { func TestService_processBlock(t *testing.T) { beaconDB, _ := dbtest.SetupDB(t) - genesisBlk := ð.BeaconBlock{ - Slot: 0, - } - genesisBlkRoot, err := stateutil.BlockRoot(genesisBlk) + genesisBlk := testutil.NewBeaconBlock() + genesisBlkRoot, err := stateutil.BlockRoot(genesisBlk.Block) require.NoError(t, err) - err = beaconDB.SaveBlock(context.Background(), ð.SignedBeaconBlock{Block: genesisBlk}) - require.NoError(t, err) - st, err := stateTrie.InitializeFromProto(&p2ppb.BeaconState{}) + err = beaconDB.SaveBlock(context.Background(), genesisBlk) require.NoError(t, err) + st := testutil.NewBeaconState() s := NewInitialSync(&Config{ P2P: p2pt.NewTestP2P(t), DB: beaconDB, @@ -351,20 +346,14 @@ func TestService_processBlock(t *testing.T) { genesis := makeGenesisTime(32) t.Run("process duplicate block", func(t *testing.T) { - blk1 := ð.SignedBeaconBlock{ - Block: ð.BeaconBlock{ - Slot: 1, - ParentRoot: genesisBlkRoot[:], - }, - } + blk1 := testutil.NewBeaconBlock() + blk1.Block.Slot = 1 + blk1.Block.ParentRoot = genesisBlkRoot[:] blk1Root, err := stateutil.BlockRoot(blk1.Block) require.NoError(t, err) - blk2 := ð.SignedBeaconBlock{ - Block: ð.BeaconBlock{ - Slot: 2, - ParentRoot: blk1Root[:], - }, - } + blk2 := testutil.NewBeaconBlock() + blk2.Block.Slot = 2 + blk2.Block.ParentRoot = blk1Root[:] // Process block normally. err = s.processBlock(ctx, genesis, blk1, func( @@ -394,15 +383,12 @@ func TestService_processBlock(t *testing.T) { func TestService_processBlockBatch(t *testing.T) { beaconDB, _ := dbtest.SetupDB(t) - genesisBlk := ð.BeaconBlock{ - Slot: 0, - } - genesisBlkRoot, err := stateutil.BlockRoot(genesisBlk) + genesisBlk := testutil.NewBeaconBlock() + genesisBlkRoot, err := stateutil.BlockRoot(genesisBlk.Block) require.NoError(t, err) - err = beaconDB.SaveBlock(context.Background(), ð.SignedBeaconBlock{Block: genesisBlk}) - require.NoError(t, err) - st, err := stateTrie.InitializeFromProto(&p2ppb.BeaconState{}) + err = beaconDB.SaveBlock(context.Background(), genesisBlk) require.NoError(t, err) + st := testutil.NewBeaconState() s := NewInitialSync(&Config{ P2P: p2pt.NewTestP2P(t), DB: beaconDB, @@ -423,12 +409,9 @@ func TestService_processBlockBatch(t *testing.T) { currBlockRoot := genesisBlkRoot for i := 1; i < 10; i++ { parentRoot := currBlockRoot - blk1 := ð.SignedBeaconBlock{ - Block: ð.BeaconBlock{ - Slot: uint64(i), - ParentRoot: parentRoot[:], - }, - } + blk1 := testutil.NewBeaconBlock() + blk1.Block.Slot = uint64(i) + blk1.Block.ParentRoot = parentRoot[:] blk1Root, err := stateutil.BlockRoot(blk1.Block) require.NoError(t, err) err = beaconDB.SaveBlock(context.Background(), blk1) @@ -440,12 +423,9 @@ func TestService_processBlockBatch(t *testing.T) { var batch2 []*eth.SignedBeaconBlock for i := 10; i < 20; i++ { parentRoot := currBlockRoot - blk1 := ð.SignedBeaconBlock{ - Block: ð.BeaconBlock{ - Slot: uint64(i), - ParentRoot: parentRoot[:], - }, - } + blk1 := testutil.NewBeaconBlock() + blk1.Block.Slot = uint64(i) + blk1.Block.ParentRoot = parentRoot[:] blk1Root, err := stateutil.BlockRoot(blk1.Block) require.NoError(t, err) err = beaconDB.SaveBlock(context.Background(), blk1) @@ -533,10 +513,10 @@ func TestService_blockProviderScoring(t *testing.T) { genesisRoot := cache.rootCache[0] cache.RUnlock() - err := beaconDB.SaveBlock(context.Background(), ð.SignedBeaconBlock{Block: ð.BeaconBlock{Slot: 0}}) + err := beaconDB.SaveBlock(context.Background(), testutil.NewBeaconBlock()) require.NoError(t, err) - st, err := stateTrie.InitializeFromProto(&p2ppb.BeaconState{}) + st := testutil.NewBeaconState() require.NoError(t, err) mc := &mock.ChainService{ State: st, @@ -544,6 +524,7 @@ func TestService_blockProviderScoring(t *testing.T) { DB: beaconDB, FinalizedCheckPoint: ð.Checkpoint{ Epoch: 0, + Root: make([]byte, 32), }, } // no-op mock s := &Service{ diff --git a/beacon-chain/sync/pending_attestations_queue_test.go b/beacon-chain/sync/pending_attestations_queue_test.go index b8e66fa7a6..d03d454153 100644 --- a/beacon-chain/sync/pending_attestations_queue_test.go +++ b/beacon-chain/sync/pending_attestations_queue_test.go @@ -49,7 +49,7 @@ func TestProcessPendingAtts_NoBlockRequestBlock(t *testing.T) { stateSummaryCache: cache.NewStateSummaryCache(), } - a := ðpb.AggregateAttestationAndProof{Aggregate: ðpb.Attestation{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{}}}} + a := ðpb.AggregateAttestationAndProof{Aggregate: ðpb.Attestation{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{Root: make([]byte, 32)}}}} r.blkRootToPendingAtts[[32]byte{'A'}] = []*ethpb.SignedAggregateAttestationAndProof{{Message: a}} require.NoError(t, r.processPendingAtts(context.Background())) require.LogsContain(t, hook, "Requesting block for pending attestation") @@ -76,16 +76,22 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) { Signature: bls.RandKey().Sign([]byte("foo")).Marshal(), AggregationBits: bitfield.Bitlist{0x02}, Data: ðpb.AttestationData{ - Target: ðpb.Checkpoint{}}}} + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), + }, + }, + SelectionProof: make([]byte, 96), + } - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() r32, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) s := testutil.NewBeaconState() require.NoError(t, r.db.SaveBlock(context.Background(), b)) require.NoError(t, r.db.SaveState(context.Background(), s, r32)) - r.blkRootToPendingAtts[r32] = []*ethpb.SignedAggregateAttestationAndProof{{Message: a}} + r.blkRootToPendingAtts[r32] = []*ethpb.SignedAggregateAttestationAndProof{{Message: a, Signature: make([]byte, 96)}} require.NoError(t, r.processPendingAtts(context.Background())) atts, err := r.attPool.UnaggregatedAttestations() @@ -104,7 +110,7 @@ func TestProcessPendingAtts_HasBlockSaveAggregatedAtt(t *testing.T) { testutil.ResetCache() beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) - sb := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + sb := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(context.Background(), sb)) root, err := stateutil.BlockRoot(sb.Block) require.NoError(t, err) @@ -163,7 +169,7 @@ func TestProcessPendingAtts_HasBlockSaveAggregatedAtt(t *testing.T) { stateSummaryCache: cache.NewStateSummaryCache(), } - sb = ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + sb = testutil.NewBeaconBlock() r32, err := stateutil.BlockRoot(sb.Block) require.NoError(t, err) require.NoError(t, r.db.SaveBlock(context.Background(), sb)) diff --git a/beacon-chain/sync/pending_blocks_queue_test.go b/beacon-chain/sync/pending_blocks_queue_test.go index 8745407504..a9c3c208d2 100644 --- a/beacon-chain/sync/pending_blocks_queue_test.go +++ b/beacon-chain/sync/pending_blocks_queue_test.go @@ -11,7 +11,6 @@ import ( "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/protocol" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers" @@ -46,17 +45,23 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks1(t *testing.T) { err := r.initCaches() require.NoError(t, err) - b0 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b0 := testutil.NewBeaconBlock() require.NoError(t, r.db.SaveBlock(context.Background(), b0)) b0Root, err := stateutil.BlockRoot(b0.Block) require.NoError(t, err) - b3 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 3, ParentRoot: b0Root[:]}} + b3 := testutil.NewBeaconBlock() + b3.Block.Slot = 3 + b3.Block.ParentRoot = b0Root[:] require.NoError(t, r.db.SaveBlock(context.Background(), b3)) // Incomplete block link - b1 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1, ParentRoot: b0Root[:]}} + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = b0Root[:] b1Root, err := stateutil.BlockRoot(b1.Block) require.NoError(t, err) - b2 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2, ParentRoot: b1Root[:]}} + b2 := testutil.NewBeaconBlock() + b2.Block.Slot = 2 + b2.Block.ParentRoot = b1Root[:] b2Root, err := stateutil.BlockRoot(b1.Block) require.NoError(t, err) @@ -89,6 +94,7 @@ func TestRegularSync_InsertDuplicateBlocks(t *testing.T) { chain: &mock.ChainService{ FinalizedCheckPoint: ðpb.Checkpoint{ Epoch: 0, + Root: make([]byte, 32), }, }, slotToPendingBlocks: make(map[uint64][]*ethpb.SignedBeaconBlock), @@ -97,11 +103,13 @@ func TestRegularSync_InsertDuplicateBlocks(t *testing.T) { err := r.initCaches() require.NoError(t, err) - b0 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b0 := testutil.NewBeaconBlock() b0r := [32]byte{'a'} require.NoError(t, r.db.SaveBlock(context.Background(), b0)) b0Root, err := stateutil.BlockRoot(b0.Block) - b1 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1, ParentRoot: b0Root[:]}} + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = b0Root[:] b1r := [32]byte{'b'} r.insertBlockToPendingQueue(b0.Block.Slot, b0, b0r) @@ -124,7 +132,7 @@ func TestRegularSync_InsertDuplicateBlocks(t *testing.T) { // b0 // \- b3 - b4 // Test b2 and b3 were missed, after receiving them we can process 2 chains. -func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks2(t *testing.T) { +func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks_2Chains(t *testing.T) { db, _ := dbtest.SetupDB(t) p1 := p2ptest.NewTestP2P(t) p2 := p2ptest.NewTestP2P(t) @@ -152,6 +160,7 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks2(t *testing.T) { chain: &mock.ChainService{ FinalizedCheckPoint: ðpb.Checkpoint{ Epoch: 0, + Root: make([]byte, 32), }, }, slotToPendingBlocks: make(map[uint64][]*ethpb.SignedBeaconBlock), @@ -163,47 +172,57 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks2(t *testing.T) { p1.Peers().SetConnectionState(p2.PeerID(), peers.PeerConnected) p1.Peers().SetChainState(p2.PeerID(), &pb.Status{}) - b0 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b0 := testutil.NewBeaconBlock() require.NoError(t, r.db.SaveBlock(context.Background(), b0)) b0Root, err := stateutil.BlockRoot(b0.Block) require.NoError(t, err) - b1 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1, ParentRoot: b0Root[:]}} + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = b0Root[:] require.NoError(t, r.db.SaveBlock(context.Background(), b1)) b1Root, err := stateutil.BlockRoot(b1.Block) require.NoError(t, err) // Incomplete block links - b2 := ðpb.BeaconBlock{Slot: 2, ParentRoot: b1Root[:]} - b2Root, err := ssz.HashTreeRoot(b2) + b2 := testutil.NewBeaconBlock() + b2.Block.Slot = 2 + b2.Block.ParentRoot = b1Root[:] + b2Root, err := b2.Block.HashTreeRoot() require.NoError(t, err) - b5 := ðpb.BeaconBlock{Slot: 5, ParentRoot: b2Root[:]} - b5Root, err := ssz.HashTreeRoot(b5) + b5 := testutil.NewBeaconBlock() + b5.Block.Slot = 5 + b5.Block.ParentRoot = b2Root[:] + b5Root, err := b5.Block.HashTreeRoot() require.NoError(t, err) - b3 := ðpb.BeaconBlock{Slot: 3, ParentRoot: b0Root[:]} - b3Root, err := ssz.HashTreeRoot(b3) + b3 := testutil.NewBeaconBlock() + b3.Block.Slot = 3 + b3.Block.ParentRoot = b0Root[:] + b3Root, err := b3.Block.HashTreeRoot() require.NoError(t, err) - b4 := ðpb.BeaconBlock{Slot: 4, ParentRoot: b3Root[:]} - b4Root, err := ssz.HashTreeRoot(b4) + b4 := testutil.NewBeaconBlock() + b4.Block.Slot = 4 + b4.Block.ParentRoot = b3Root[:] + b4Root, err := b4.Block.HashTreeRoot() require.NoError(t, err) - r.insertBlockToPendingQueue(b4.Slot, ðpb.SignedBeaconBlock{Block: b4}, b4Root) - r.insertBlockToPendingQueue(b5.Slot, ðpb.SignedBeaconBlock{Block: b5}, b5Root) + r.insertBlockToPendingQueue(b4.Block.Slot, b4, b4Root) + r.insertBlockToPendingQueue(b5.Block.Slot, b5, b5Root) require.NoError(t, r.processPendingBlocks(context.Background())) assert.Equal(t, 2, len(r.slotToPendingBlocks), "Incorrect size for slot to pending blocks cache") assert.Equal(t, 2, len(r.seenPendingBlocks), "Incorrect size for seen pending block") // Add b3 to the cache - r.insertBlockToPendingQueue(b3.Slot, ðpb.SignedBeaconBlock{Block: b3}, b3Root) - require.NoError(t, r.db.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: b3})) + r.insertBlockToPendingQueue(b3.Block.Slot, b3, b3Root) + require.NoError(t, r.db.SaveBlock(context.Background(), b3)) require.NoError(t, r.processPendingBlocks(context.Background())) assert.Equal(t, 1, len(r.slotToPendingBlocks), "Incorrect size for slot to pending blocks cache") assert.Equal(t, 3, len(r.seenPendingBlocks), "Incorrect size for seen pending block") // Add b2 to the cache - r.insertBlockToPendingQueue(b2.Slot, ðpb.SignedBeaconBlock{Block: b2}, b2Root) + r.insertBlockToPendingQueue(b2.Block.Slot, b2, b2Root) - require.NoError(t, r.db.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: b2})) + require.NoError(t, r.db.SaveBlock(context.Background(), b2)) require.NoError(t, r.processPendingBlocks(context.Background())) assert.Equal(t, 0, len(r.slotToPendingBlocks), "Incorrect size for slot to pending blocks cache") assert.Equal(t, 4, len(r.seenPendingBlocks), "Incorrect size for seen pending block") @@ -222,6 +241,7 @@ func TestRegularSyncBeaconBlockSubscriber_PruneOldPendingBlocks(t *testing.T) { chain: &mock.ChainService{ FinalizedCheckPoint: ðpb.Checkpoint{ Epoch: 1, + Root: make([]byte, 32), }, }, slotToPendingBlocks: make(map[uint64][]*ethpb.SignedBeaconBlock), @@ -233,33 +253,43 @@ func TestRegularSyncBeaconBlockSubscriber_PruneOldPendingBlocks(t *testing.T) { p1.Peers().SetConnectionState(p1.PeerID(), peers.PeerConnected) p1.Peers().SetChainState(p1.PeerID(), &pb.Status{}) - b0 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b0 := testutil.NewBeaconBlock() require.NoError(t, r.db.SaveBlock(context.Background(), b0)) b0Root, err := stateutil.BlockRoot(b0.Block) require.NoError(t, err) - b1 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1, ParentRoot: b0Root[:]}} + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = b0Root[:] require.NoError(t, r.db.SaveBlock(context.Background(), b1)) b1Root, err := stateutil.BlockRoot(b1.Block) require.NoError(t, err) // Incomplete block links - b2 := ðpb.BeaconBlock{Slot: 2, ParentRoot: b1Root[:]} - b2Root, err := ssz.HashTreeRoot(b2) + b2 := testutil.NewBeaconBlock() + b2.Block.Slot = 2 + b2.Block.ParentRoot = b1Root[:] + b2Root, err := b2.Block.HashTreeRoot() require.NoError(t, err) - b5 := ðpb.BeaconBlock{Slot: 5, ParentRoot: b2Root[:]} - b5Root, err := ssz.HashTreeRoot(b5) + b5 := testutil.NewBeaconBlock() + b5.Block.Slot = 5 + b5.Block.ParentRoot = b2Root[:] + b5Root, err := b5.Block.HashTreeRoot() require.NoError(t, err) - b3 := ðpb.BeaconBlock{Slot: 3, ParentRoot: b0Root[:]} - b3Root, err := ssz.HashTreeRoot(b3) + b3 := testutil.NewBeaconBlock() + b3.Block.Slot = 3 + b3.Block.ParentRoot = b0Root[:] + b3Root, err := b3.Block.HashTreeRoot() require.NoError(t, err) - b4 := ðpb.BeaconBlock{Slot: 4, ParentRoot: b3Root[:]} - b4Root, err := ssz.HashTreeRoot(b4) + b4 := testutil.NewBeaconBlock() + b4.Block.Slot = 4 + b4.Block.ParentRoot = b3Root[:] + b4Root, err := b4.Block.HashTreeRoot() require.NoError(t, err) - r.insertBlockToPendingQueue(b2.Slot, ðpb.SignedBeaconBlock{Block: b2}, b2Root) - r.insertBlockToPendingQueue(b3.Slot, ðpb.SignedBeaconBlock{Block: b3}, b3Root) - r.insertBlockToPendingQueue(b4.Slot, ðpb.SignedBeaconBlock{Block: b4}, b4Root) - r.insertBlockToPendingQueue(b5.Slot, ðpb.SignedBeaconBlock{Block: b5}, b5Root) + r.insertBlockToPendingQueue(b2.Block.Slot, b2, b2Root) + r.insertBlockToPendingQueue(b3.Block.Slot, b3, b3Root) + r.insertBlockToPendingQueue(b4.Block.Slot, b4, b4Root) + r.insertBlockToPendingQueue(b5.Block.Slot, b5, b5Root) require.NoError(t, r.processPendingBlocks(context.Background())) assert.Equal(t, 0, len(r.slotToPendingBlocks), "Incorrect size for slot to pending blocks cache") @@ -295,6 +325,7 @@ func TestService_BatchRootRequest(t *testing.T) { chain: &mock.ChainService{ FinalizedCheckPoint: ðpb.Checkpoint{ Epoch: 1, + Root: make([]byte, 32), }, }, slotToPendingBlocks: make(map[uint64][]*ethpb.SignedBeaconBlock), @@ -307,26 +338,36 @@ func TestService_BatchRootRequest(t *testing.T) { p1.Peers().SetConnectionState(p2.PeerID(), peers.PeerConnected) p1.Peers().SetChainState(p2.PeerID(), &pb.Status{FinalizedEpoch: 2}) - b0 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b0 := testutil.NewBeaconBlock() require.NoError(t, r.db.SaveBlock(context.Background(), b0)) b0Root, err := stateutil.BlockRoot(b0.Block) require.NoError(t, err) - b1 := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1, ParentRoot: b0Root[:]}} + b1 := testutil.NewBeaconBlock() + b1.Block.Slot = 1 + b1.Block.ParentRoot = b0Root[:] require.NoError(t, r.db.SaveBlock(context.Background(), b1)) b1Root, err := stateutil.BlockRoot(b1.Block) require.NoError(t, err) - b2 := ðpb.BeaconBlock{Slot: 2, ParentRoot: b1Root[:]} - b2Root, err := ssz.HashTreeRoot(b2) + b2 := testutil.NewBeaconBlock() + b2.Block.Slot = 2 + b2.Block.ParentRoot = b1Root[:] + b2Root, err := b2.Block.HashTreeRoot() require.NoError(t, err) - b5 := ðpb.BeaconBlock{Slot: 5, ParentRoot: b2Root[:]} - b5Root, err := ssz.HashTreeRoot(b5) + b5 := testutil.NewBeaconBlock() + b5.Block.Slot = 5 + b5.Block.ParentRoot = b2Root[:] + b5Root, err := b5.Block.HashTreeRoot() require.NoError(t, err) - b3 := ðpb.BeaconBlock{Slot: 3, ParentRoot: b0Root[:]} - b3Root, err := ssz.HashTreeRoot(b3) + b3 := testutil.NewBeaconBlock() + b3.Block.Slot = 3 + b3.Block.ParentRoot = b0Root[:] + b3Root, err := b3.Block.HashTreeRoot() require.NoError(t, err) - b4 := ðpb.BeaconBlock{Slot: 4, ParentRoot: b3Root[:]} - b4Root, err := ssz.HashTreeRoot(b4) + b4 := testutil.NewBeaconBlock() + b4.Block.Slot = 4 + b4.Block.ParentRoot = b3Root[:] + b4Root, err := b4.Block.HashTreeRoot() require.NoError(t, err) // Send in duplicated roots to also test deduplicaton. @@ -341,8 +382,7 @@ func TestService_BatchRootRequest(t *testing.T) { out := [][32]byte{} assert.NoError(t, p2.Encoding().DecodeWithMaxLength(stream, &out)) assert.DeepEqual(t, expectedRoots, out, "Did not receive expected message") - response := []*ethpb.SignedBeaconBlock{{Block: b2}, - {Block: b3}, {Block: b4}, {Block: b5}} + response := []*ethpb.SignedBeaconBlock{b2, b3, b4, b5} for _, blk := range response { _, err := stream.Write([]byte{responseCodeSuccess}) assert.NoError(t, err, "Failed to write to stream") diff --git a/beacon-chain/sync/rpc_beacon_blocks_by_range_test.go b/beacon-chain/sync/rpc_beacon_blocks_by_range_test.go index 7319c3d43d..cf7495e6c1 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_range_test.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_range_test.go @@ -37,7 +37,9 @@ func TestRPCBeaconBlocksByRange_RPCHandlerReturnsBlocks(t *testing.T) { // Populate the database with blocks that would match the request. for i := req.StartSlot; i < req.StartSlot+(req.Step*req.Count); i += req.Step { - require.NoError(t, d.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: i}})) + blk := testutil.NewBeaconBlock() + blk.Block.Slot = i + require.NoError(t, d.SaveBlock(context.Background(), blk)) } // Start service with 160 as allowed blocks capacity (and almost zero capacity recovery). @@ -51,7 +53,7 @@ func TestRPCBeaconBlocksByRange_RPCHandlerReturnsBlocks(t *testing.T) { defer wg.Done() for i := req.StartSlot; i < req.StartSlot+req.Count*req.Step; i += req.Step { expectSuccess(t, r, stream) - res := ðpb.SignedBeaconBlock{} + res := testutil.NewBeaconBlock() assert.NoError(t, r.p2p.Encoding().DecodeWithMaxLength(stream, res)) if (res.Block.Slot-req.StartSlot)%req.Step != 0 { t.Errorf("Received unexpected block slot %d", res.Block.Slot) @@ -92,7 +94,8 @@ func TestRPCBeaconBlocksByRange_RPCHandlerReturnsSortedBlocks(t *testing.T) { expectedRoots := make([][32]byte, req.Count) // Populate the database with blocks that would match the request. for i, j := endSlot, req.Count-1; i >= req.StartSlot; i -= req.Step { - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: i}} + blk := testutil.NewBeaconBlock() + blk.Block.Slot = i rt, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) expectedRoots[j] = rt @@ -152,13 +155,16 @@ func TestRPCBeaconBlocksByRange_ReturnsGenesisBlock(t *testing.T) { // Populate the database with blocks that would match the request. for i := req.StartSlot; i < req.StartSlot+(req.Step*req.Count); i++ { + blk := testutil.NewBeaconBlock() + blk.Block.Slot = i + // Save genesis block if i == 0 { - rt, err := stateutil.BlockRoot(ðpb.BeaconBlock{Slot: i}) + rt, err := stateutil.BlockRoot(blk.Block) require.NoError(t, err) require.NoError(t, d.SaveGenesisBlockRoot(context.Background(), rt)) } - require.NoError(t, d.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: i}})) + require.NoError(t, d.SaveBlock(context.Background(), blk)) } r := &Service{p2p: p1, db: d, chain: &chainMock.ChainService{}, rateLimiter: newRateLimiter(p1)} @@ -197,7 +203,8 @@ func TestRPCBeaconBlocksByRange_RPCHandlerRateLimitOverflow(t *testing.T) { saveBlocks := func(req *pb.BeaconBlocksByRangeRequest) { // Populate the database with blocks that would match the request. for i := req.StartSlot; i < req.StartSlot+(req.Step*req.Count); i += req.Step { - block := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: i}} + block := testutil.NewBeaconBlock() + block.Block.Slot = i require.NoError(t, d.SaveBlock(context.Background(), block)) } } @@ -213,7 +220,7 @@ func TestRPCBeaconBlocksByRange_RPCHandlerRateLimitOverflow(t *testing.T) { } for i := req.StartSlot; i < req.StartSlot+req.Count*req.Step; i += req.Step { expectSuccess(t, r, stream) - res := ðpb.SignedBeaconBlock{} + res := testutil.NewBeaconBlock() assert.NoError(t, r.p2p.Encoding().DecodeWithMaxLength(stream, res)) if (res.Block.Slot-req.StartSlot)%req.Step != 0 { t.Errorf("Received unexpected block slot %d", res.Block.Slot) diff --git a/beacon-chain/sync/rpc_beacon_blocks_by_root_test.go b/beacon-chain/sync/rpc_beacon_blocks_by_root_test.go index 1068fbc64a..427e6018d1 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_root_test.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_root_test.go @@ -36,12 +36,11 @@ func TestRecentBeaconBlocksRPCHandler_ReturnsBlocks(t *testing.T) { var blkRoots [][32]byte // Populate the database with blocks that would match the request. for i := 1; i < 11; i++ { - blk := ðpb.BeaconBlock{ - Slot: uint64(i), - } - root, err := ssz.HashTreeRoot(blk) + blk := testutil.NewBeaconBlock() + blk.Block.Slot = uint64(i) + root, err := blk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, d.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: blk})) + require.NoError(t, d.SaveBlock(context.Background(), blk)) blkRoots = append(blkRoots, root) } @@ -56,7 +55,7 @@ func TestRecentBeaconBlocksRPCHandler_ReturnsBlocks(t *testing.T) { defer wg.Done() for i := range blkRoots { expectSuccess(t, r, stream) - res := ðpb.SignedBeaconBlock{} + res := testutil.NewBeaconBlock() assert.NoError(t, r.p2p.Encoding().DecodeWithMaxLength(stream, &res)) if res.Block.Slot != uint64(i+1) { t.Errorf("Received unexpected block slot %d but wanted %d", res.Block.Slot, i+1) @@ -79,8 +78,10 @@ func TestRecentBeaconBlocks_RPCRequestSent(t *testing.T) { p2 := p2ptest.NewTestP2P(t) p1.DelaySend = true - blockA := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 111}} - blockB := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 40}} + blockA := testutil.NewBeaconBlock() + blockA.Block.Slot = 111 + blockB := testutil.NewBeaconBlock() + blockB.Block.Slot = 40 // Set up a head state with data we expect. blockARoot, err := stateutil.BlockRoot(blockA.Block) require.NoError(t, err) diff --git a/beacon-chain/sync/rpc_status_test.go b/beacon-chain/sync/rpc_status_test.go index 1867d9a8bf..24ada746ae 100644 --- a/beacon-chain/sync/rpc_status_test.go +++ b/beacon-chain/sync/rpc_status_test.go @@ -14,7 +14,6 @@ import ( "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/protocol" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" testingDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" @@ -23,6 +22,7 @@ import ( stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -50,6 +50,7 @@ func TestStatusRPCHandler_Disconnects_OnForkVersionMismatch(t *testing.T) { }, Genesis: time.Now(), ValidatorsRoot: [32]byte{'A'}, + Root: make([]byte, 32), }} pcl := protocol.ID("/testing") topic := string(pcl) @@ -83,7 +84,7 @@ func TestStatusRPCHandler_Disconnects_OnForkVersionMismatch(t *testing.T) { stream1, err := p1.BHost.NewStream(context.Background(), p2.BHost.ID(), pcl) require.NoError(t, err) - assert.NoError(t, r.statusRPCHandler(context.Background(), &pb.Status{ForkDigest: []byte("fake")}, stream1)) + assert.NoError(t, r.statusRPCHandler(context.Background(), &pb.Status{ForkDigest: bytesutil.PadTo([]byte("f"), 4), HeadRoot: make([]byte, 32), FinalizedRoot: make([]byte, 32)}, stream1)) if testutil.WaitTimeout(&wg, 1*time.Second) { t.Fatal("Did not receive stream within 1 sec") @@ -115,6 +116,7 @@ func TestStatusRPCHandler_ConnectsOnGenesis(t *testing.T) { }, Genesis: time.Now(), ValidatorsRoot: [32]byte{'A'}, + Root: make([]byte, 32), }} pcl := protocol.ID("/testing") topic := string(pcl) @@ -155,16 +157,20 @@ func TestStatusRPCHandler_ReturnsHelloMessage(t *testing.T) { db, _ := testingDB.SetupDB(t) // Set up a head state with data we expect. - headRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 111}) + head := testutil.NewBeaconBlock() + head.Block.Slot = 111 + headRoot, err := head.Block.HashTreeRoot() require.NoError(t, err) blkSlot := 3 * params.BeaconConfig().SlotsPerEpoch - finalizedRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: blkSlot}) + finalized := testutil.NewBeaconBlock() + finalized.Block.Slot = blkSlot + finalizedRoot, err := finalized.Block.HashTreeRoot() require.NoError(t, err) genesisState, err := state.GenesisBeaconState(nil, 0, ðpb.Eth1Data{}) require.NoError(t, err) require.NoError(t, genesisState.SetSlot(111)) require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%params.BeaconConfig().SlotsPerHistoricalRoot, headRoot)) - require.NoError(t, db.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: blkSlot}})) + require.NoError(t, db.SaveBlock(context.Background(), finalized)) require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), finalizedRoot)) finalizedCheckpt := ðpb.Checkpoint{ Epoch: 3, @@ -238,21 +244,22 @@ func TestHandshakeHandlers_Roundtrip(t *testing.T) { p1.LocalMetadata = &pb.MetaData{ SeqNumber: 2, - Attnets: []byte{'A', 'B'}, + Attnets: bytesutil.PadTo([]byte{'A', 'B'}, 8), } p2.LocalMetadata = &pb.MetaData{ SeqNumber: 2, - Attnets: []byte{'C', 'D'}, + Attnets: bytesutil.PadTo([]byte{'C', 'D'}, 8), } st, err := stateTrie.InitializeFromProto(&pb.BeaconState{ Slot: 5, }) require.NoError(t, err) - blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 0}} + blk := testutil.NewBeaconBlock() + blk.Block.Slot = 0 require.NoError(t, db.SaveBlock(context.Background(), blk)) - finalizedRoot, err := ssz.HashTreeRoot(blk.Block) + finalizedRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), finalizedRoot)) r := &Service{ @@ -266,6 +273,7 @@ func TestHandshakeHandlers_Roundtrip(t *testing.T) { }, Genesis: time.Now(), ValidatorsRoot: [32]byte{'A'}, + Root: make([]byte, 32), }, db: db, ctx: context.Background(), @@ -297,7 +305,7 @@ func TestHandshakeHandlers_Roundtrip(t *testing.T) { out := &pb.Status{} assert.NoError(t, r.p2p.Encoding().DecodeWithMaxLength(stream, out)) log.WithField("status", out).Warn("received status") - resp := &pb.Status{HeadSlot: 100, ForkDigest: p2.Digest[:], + resp := &pb.Status{HeadSlot: 100, HeadRoot: make([]byte, 32), ForkDigest: p2.Digest[:], FinalizedRoot: finalizedRoot[:], FinalizedEpoch: 0} _, err := stream.Write([]byte{responseCodeSuccess}) assert.NoError(t, err) @@ -367,9 +375,13 @@ func TestStatusRPCRequest_RequestSent(t *testing.T) { p2 := p2ptest.NewTestP2P(t) // Set up a head state with data we expect. - headRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 111}) + head := testutil.NewBeaconBlock() + head.Block.Slot = 111 + headRoot, err := head.Block.HashTreeRoot() require.NoError(t, err) - finalizedRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 40}) + finalized := testutil.NewBeaconBlock() + finalized.Block.Slot = 40 + finalizedRoot, err := finalized.Block.HashTreeRoot() require.NoError(t, err) genesisState, err := state.GenesisBeaconState(nil, 0, ðpb.Eth1Data{}) require.NoError(t, err) @@ -437,16 +449,22 @@ func TestStatusRPCRequest_FinalizedBlockExists(t *testing.T) { db, _ := testingDB.SetupDB(t) // Set up a head state with data we expect. - headRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 111}) + head := testutil.NewBeaconBlock() + head.Block.Slot = 111 + headRoot, err := head.Block.HashTreeRoot() require.NoError(t, err) blkSlot := 3 * params.BeaconConfig().SlotsPerEpoch - finalizedRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: blkSlot}) + finalized := testutil.NewBeaconBlock() + finalized.Block.Slot = blkSlot + finalizedRoot, err := finalized.Block.HashTreeRoot() require.NoError(t, err) - genesisState, err := state.GenesisBeaconState(nil, 0, ðpb.Eth1Data{}) + genesisState, err := state.GenesisBeaconState(nil, 0, ðpb.Eth1Data{DepositRoot: make([]byte, 32), BlockHash: make([]byte, 32)}) require.NoError(t, err) require.NoError(t, genesisState.SetSlot(111)) require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%params.BeaconConfig().SlotsPerHistoricalRoot, headRoot)) - require.NoError(t, db.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: blkSlot}})) + blk := testutil.NewBeaconBlock() + blk.Block.Slot = blkSlot + require.NoError(t, db.SaveBlock(context.Background(), blk)) require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), finalizedRoot)) finalizedCheckpt := ðpb.Checkpoint{ Epoch: 3, @@ -517,9 +535,12 @@ func TestStatusRPCRequest_BadPeerHandshake(t *testing.T) { p2 := p2ptest.NewTestP2P(t) // Set up a head state with data we expect. - headRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 111}) + head := testutil.NewBeaconBlock() + head.Block.Slot = 111 + headRoot, err := head.Block.HashTreeRoot() require.NoError(t, err) - finalizedRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 40}) + finalized := testutil.NewBeaconBlock() + finalizedRoot, err := finalized.Block.HashTreeRoot() require.NoError(t, err) genesisState, err := state.GenesisBeaconState(nil, 0, ðpb.Eth1Data{}) require.NoError(t, err) @@ -591,10 +612,14 @@ func TestStatusRPCRequest_BadPeerHandshake(t *testing.T) { func TestStatusRPC_ValidGenesisMessage(t *testing.T) { // Set up a head state with data we expect. - headRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 111}) + head := testutil.NewBeaconBlock() + head.Block.Slot = 111 + headRoot, err := head.Block.HashTreeRoot() require.NoError(t, err) blkSlot := 3 * params.BeaconConfig().SlotsPerEpoch - finalizedRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: blkSlot}) + finalized := testutil.NewBeaconBlock() + finalized.Block.Slot = blkSlot + finalizedRoot, err := finalized.Block.HashTreeRoot() require.NoError(t, err) genesisState, err := state.GenesisBeaconState(nil, 0, ðpb.Eth1Data{}) require.NoError(t, err) diff --git a/beacon-chain/sync/service_test.go b/beacon-chain/sync/service_test.go index 8fa3a30302..a82386ed06 100644 --- a/beacon-chain/sync/service_test.go +++ b/beacon-chain/sync/service_test.go @@ -5,7 +5,6 @@ import ( "testing" "time" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state" @@ -67,12 +66,9 @@ func TestSyncHandlers_WaitToSync(t *testing.T) { sk, err := bls.SecretKeyFromBytes(b32[:]) require.NoError(t, err) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ParentRoot: testutil.Random32Bytes(t), - }, - Signature: sk.Sign([]byte("data")).Marshal(), - } + msg := testutil.NewBeaconBlock() + msg.Block.ParentRoot = testutil.Random32Bytes(t) + msg.Signature = sk.Sign([]byte("data")).Marshal() p2p.ReceivePubSub(topic, msg) // wait for chainstart to be sent time.Sleep(400 * time.Millisecond) diff --git a/beacon-chain/sync/subscriber_beacon_aggregate_proof_test.go b/beacon-chain/sync/subscriber_beacon_aggregate_proof_test.go index a37728702a..d39d00c344 100644 --- a/beacon-chain/sync/subscriber_beacon_aggregate_proof_test.go +++ b/beacon-chain/sync/subscriber_beacon_aggregate_proof_test.go @@ -22,7 +22,20 @@ func TestBeaconAggregateProofSubscriber_CanSaveAggregatedAttestation(t *testing. attestationNotifier: (&mock.ChainService{}).OperationNotifier(), } - a := ðpb.SignedAggregateAttestationAndProof{Message: ðpb.AggregateAttestationAndProof{Aggregate: ðpb.Attestation{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{}}, AggregationBits: bitfield.Bitlist{0x07}}, AggregatorIndex: 100}} + a := ðpb.SignedAggregateAttestationAndProof{ + Message: ðpb.AggregateAttestationAndProof{ + Aggregate: ðpb.Attestation{ + Data: ðpb.AttestationData{ + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), + }, + AggregationBits: bitfield.Bitlist{0x07}, + }, + AggregatorIndex: 100, + }, + Signature: make([]byte, 96), + } require.NoError(t, r.beaconAggregateProofSubscriber(context.Background(), a)) assert.DeepEqual(t, []*ethpb.Attestation{a.Message.Aggregate}, r.attPool.AggregatedAttestations(), "Did not save aggregated attestation") } @@ -36,7 +49,24 @@ func TestBeaconAggregateProofSubscriber_CanSaveUnaggregatedAttestation(t *testin attestationNotifier: (&mock.ChainService{}).OperationNotifier(), } - a := ðpb.SignedAggregateAttestationAndProof{Message: ðpb.AggregateAttestationAndProof{Aggregate: ðpb.Attestation{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{}}, AggregationBits: bitfield.Bitlist{0x03}}, AggregatorIndex: 100}} + a := ðpb.SignedAggregateAttestationAndProof{ + Message: ðpb.AggregateAttestationAndProof{ + Aggregate: ðpb.Attestation{ + Data: ðpb.AttestationData{ + Target: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + Source: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + BeaconBlockRoot: make([]byte, 32), + }, + AggregationBits: bitfield.Bitlist{0x03}, + Signature: make([]byte, 96), + }, + AggregatorIndex: 100, + }, + } require.NoError(t, r.beaconAggregateProofSubscriber(context.Background(), a)) atts, err := r.attPool.UnaggregatedAttestations() diff --git a/beacon-chain/sync/subscriber_beacon_attestation_test.go b/beacon-chain/sync/subscriber_beacon_attestation_test.go index d31209b140..43baff4d39 100644 --- a/beacon-chain/sync/subscriber_beacon_attestation_test.go +++ b/beacon-chain/sync/subscriber_beacon_attestation_test.go @@ -25,7 +25,6 @@ import ( ) func TestService_committeeIndexBeaconAttestationSubscriber_ValidMessage(t *testing.T) { - p := p2ptest.NewTestP2P(t) resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{DisableDynamicCommitteeSubnets: true}) defer resetCfg() @@ -79,9 +78,11 @@ func TestService_committeeIndexBeaconAttestationSubscriber_ValidMessage(t *testi Data: ð.AttestationData{ Slot: 0, BeaconBlockRoot: root[:], - Target: ð.Checkpoint{}, + Target: ð.Checkpoint{Root: make([]byte, 32)}, + Source: ð.Checkpoint{Root: make([]byte, 32)}, }, AggregationBits: bitfield.Bitlist{0b0101}, + Signature: make([]byte, 96), } committee, err := helpers.BeaconCommitteeFromState(s, att.Data.Slot, att.Data.CommitteeIndex) require.NoError(t, err) diff --git a/beacon-chain/sync/subscriber_beacon_blocks_test.go b/beacon-chain/sync/subscriber_beacon_blocks_test.go index 4f039211a8..37abed9b22 100644 --- a/beacon-chain/sync/subscriber_beacon_blocks_test.go +++ b/beacon-chain/sync/subscriber_beacon_blocks_test.go @@ -19,11 +19,15 @@ func TestDeleteAttsInPool(t *testing.T) { r := &Service{ attPool: attestations.NewPool(), } - data := ðpb.AttestationData{} - att1 := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1101}, Data: data} - att2 := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1110}, Data: data} - att3 := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1011}, Data: data} - att4 := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1001}, Data: data} + data := ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + } + att1 := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1101}, Data: data, Signature: make([]byte, 96)} + att2 := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1110}, Data: data, Signature: make([]byte, 96)} + att3 := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1011}, Data: data, Signature: make([]byte, 96)} + att4 := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b1001}, Data: data, Signature: make([]byte, 96)} require.NoError(t, r.attPool.SaveAggregatedAttestation(att1)) require.NoError(t, r.attPool.SaveAggregatedAttestation(att2)) require.NoError(t, r.attPool.SaveAggregatedAttestation(att3)) @@ -41,12 +45,22 @@ func TestService_beaconBlockSubscriber(t *testing.T) { // Aggregated. { AggregationBits: bitfield.Bitlist{0b00011111}, - Data: ðpb.AttestationData{}, + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, + Signature: make([]byte, 96), }, // Unaggregated. { AggregationBits: bitfield.Bitlist{0b00010001}, - Data: ðpb.AttestationData{}, + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, + Signature: make([]byte, 96), }, } diff --git a/beacon-chain/sync/subscriber_test.go b/beacon-chain/sync/subscriber_test.go index c277a3ab0d..fcde5158c8 100644 --- a/beacon-chain/sync/subscriber_test.go +++ b/beacon-chain/sync/subscriber_test.go @@ -55,7 +55,7 @@ func TestSubscribe_ReceivesValidMessage(t *testing.T) { }) r.chainStarted = true - p2p.ReceivePubSub(topic, &pb.SignedVoluntaryExit{Exit: &pb.VoluntaryExit{Epoch: 55}}) + p2p.ReceivePubSub(topic, &pb.SignedVoluntaryExit{Exit: &pb.VoluntaryExit{Epoch: 55}, Signature: make([]byte, 96)}) if testutil.WaitTimeout(&wg, time.Second) { t.Fatal("Did not receive PubSub in 1 second") @@ -185,7 +185,7 @@ func TestSubscribe_HandlesPanic(t *testing.T) { panic("bad") }) r.chainStarted = true - p.ReceivePubSub(topic, &pb.SignedVoluntaryExit{Exit: &pb.VoluntaryExit{Epoch: 55}}) + p.ReceivePubSub(topic, &pb.SignedVoluntaryExit{Exit: &pb.VoluntaryExit{Epoch: 55}, Signature: make([]byte, 96)}) if testutil.WaitTimeout(&wg, time.Second) { t.Fatal("Did not receive PubSub in 1 second") diff --git a/beacon-chain/sync/validate_aggregate_proof_test.go b/beacon-chain/sync/validate_aggregate_proof_test.go index 69aef1f085..9e9e1ced41 100644 --- a/beacon-chain/sync/validate_aggregate_proof_test.go +++ b/beacon-chain/sync/validate_aggregate_proof_test.go @@ -86,7 +86,11 @@ func TestVerifySelection_NotAnAggregator(t *testing.T) { beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) sig := privKeys[0].Sign([]byte{'A'}) - data := ðpb.AttestationData{} + data := ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + } wanted := "validator is not an aggregator for slot" assert.ErrorContains(t, wanted, validateSelection(ctx, beaconState, data, 0, sig.Marshal())) @@ -98,7 +102,11 @@ func TestVerifySelection_BadSignature(t *testing.T) { beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) sig := privKeys[0].Sign([]byte{'A'}) - data := ðpb.AttestationData{} + data := ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + } wanted := "signature did not verify" assert.ErrorContains(t, wanted, validateSelection(ctx, beaconState, data, 0, sig.Marshal())) @@ -109,7 +117,11 @@ func TestVerifySelection_CanVerify(t *testing.T) { validators := uint64(256) beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) - data := ðpb.AttestationData{} + data := ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + } sig, err := helpers.ComputeDomainAndSign(beaconState, 0, data.Slot, params.BeaconConfig().DomainSelectionProof, privKeys[0]) require.NoError(t, err) require.NoError(t, validateSelection(ctx, beaconState, data, 0, sig)) @@ -121,9 +133,11 @@ func TestValidateAggregateAndProof_NoBlock(t *testing.T) { att := ðpb.Attestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)}, - Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)}, + Source: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), } aggregateAndProof := ðpb.AggregateAttestationAndProof{ @@ -131,7 +145,7 @@ func TestValidateAggregateAndProof_NoBlock(t *testing.T) { Aggregate: att, AggregatorIndex: 0, } - signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof} + signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof, Signature: make([]byte, 96)} c, err := lru.New(10) require.NoError(t, err) @@ -173,7 +187,7 @@ func TestValidateAggregateAndProof_NotWithinSlotRange(t *testing.T) { validators := uint64(256) beaconState, _ := testutil.DeterministicGenesisState(t, validators) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(context.Background(), b)) root, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -190,12 +204,14 @@ func TestValidateAggregateAndProof_NotWithinSlotRange(t *testing.T) { Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)}, }, AggregationBits: aggBits, + Signature: make([]byte, 96), } aggregateAndProof := ðpb.AggregateAttestationAndProof{ - Aggregate: att, + Aggregate: att, + SelectionProof: make([]byte, 96), } - signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof} + signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof, Signature: make([]byte, 96)} require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix()))) @@ -205,8 +221,10 @@ func TestValidateAggregateAndProof_NotWithinSlotRange(t *testing.T) { p2p: p, db: db, initialSync: &mockSync.Sync{IsSyncing: false}, - chain: &mock.ChainService{Genesis: time.Now(), - State: beaconState}, + chain: &mock.ChainService{ + Genesis: time.Now(), + State: beaconState, + }, attPool: attestations.NewPool(), seenAttestationCache: c, stateSummaryCache: cache.NewStateSummaryCache(), @@ -257,7 +275,7 @@ func TestValidateAggregateAndProof_ExistedInPool(t *testing.T) { validators := uint64(256) beaconState, _ := testutil.DeterministicGenesisState(t, validators) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(context.Background(), b)) root, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -272,12 +290,14 @@ func TestValidateAggregateAndProof_ExistedInPool(t *testing.T) { Target: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)}, }, AggregationBits: aggBits, + Signature: make([]byte, 96), } aggregateAndProof := ðpb.AggregateAttestationAndProof{ - Aggregate: att, + Aggregate: att, + SelectionProof: make([]byte, 96), } - signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof} + signedAggregateAndProof := ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof, Signature: make([]byte, 96)} require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix()))) c, err := lru.New(10) @@ -324,7 +344,7 @@ func TestValidateAggregateAndProofWithNewStateMgmt_CanValidate(t *testing.T) { validators := uint64(256) beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(context.Background(), b)) root, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -412,7 +432,7 @@ func TestVerifyIndexInCommittee_SeenAggregatorEpoch(t *testing.T) { validators := uint64(256) beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(context.Background(), b)) root, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) @@ -522,7 +542,7 @@ func TestValidateAggregateAndProof_BadBlock(t *testing.T) { validators := uint64(256) beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) - b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + b := testutil.NewBeaconBlock() root, err := stateutil.BlockRoot(b.Block) require.NoError(t, err) s := testutil.NewBeaconState() diff --git a/beacon-chain/sync/validate_attester_slashing_test.go b/beacon-chain/sync/validate_attester_slashing_test.go index 9ac34c6255..cb1b25c2ff 100644 --- a/beacon-chain/sync/validate_attester_slashing_test.go +++ b/beacon-chain/sync/validate_attester_slashing_test.go @@ -35,10 +35,12 @@ func setupValidAttesterSlashing(t *testing.T) (*ethpb.AttesterSlashing, *stateTr att1 := ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 1}, - Target: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: []uint64{0, 1}, + Signature: make([]byte, 96), } domain, err := helpers.Domain(state.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, state.GenesisValidatorRoot()) require.NoError(t, err) @@ -51,10 +53,12 @@ func setupValidAttesterSlashing(t *testing.T) (*ethpb.AttesterSlashing, *stateTr att2 := ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 0}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, AttestingIndices: []uint64{0, 1}, + Signature: make([]byte, 96), } hashTreeRoot, err = helpers.ComputeSigningRoot(att2.Data, domain) assert.NoError(t, err) diff --git a/beacon-chain/sync/validate_beacon_attestation_test.go b/beacon-chain/sync/validate_beacon_attestation_test.go index c973682b7b..4c8428ea6a 100644 --- a/beacon-chain/sync/validate_beacon_attestation_test.go +++ b/beacon-chain/sync/validate_beacon_attestation_test.go @@ -26,7 +26,6 @@ import ( ) func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { - ctx := context.Background() p := p2ptest.NewTestP2P(t) db, _ := dbtest.SetupDB(t) @@ -57,11 +56,8 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { digest, err := s.forkDigest() require.NoError(t, err) - blk := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: 1, - }, - } + blk := testutil.NewBeaconBlock() + blk.Block.Slot = 1 require.NoError(t, db.SaveBlock(ctx, blk)) validBlockRoot, err := stateutil.BlockRoot(blk.Block) @@ -92,6 +88,7 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { Epoch: 0, Root: validBlockRoot[:], }, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, topic: fmt.Sprintf("/eth2/%x/beacon_attestation_1", digest), @@ -106,7 +103,8 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { BeaconBlockRoot: validBlockRoot[:], CommitteeIndex: 0, Slot: 1, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, topic: fmt.Sprintf("/eth2/%x/beacon_attestation_1", digest), @@ -121,7 +119,8 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { BeaconBlockRoot: invalidRoot[:], CommitteeIndex: 0, Slot: 1, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, topic: fmt.Sprintf("/eth2/%x/beacon_attestation_1", digest), @@ -137,7 +136,8 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { BeaconBlockRoot: validBlockRoot[:], CommitteeIndex: 2, Slot: 1, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, topic: fmt.Sprintf("/eth2/%x/beacon_attestation_2", digest), @@ -152,7 +152,8 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { BeaconBlockRoot: validBlockRoot[:], CommitteeIndex: 1, Slot: 1, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, topic: fmt.Sprintf("/eth2/%x/beacon_attestation_1", digest), @@ -167,7 +168,8 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { BeaconBlockRoot: bytesutil.PadTo([]byte("missing"), 32), CommitteeIndex: 1, Slot: 1, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, topic: fmt.Sprintf("/eth2/%x/beacon_attestation_1", digest), @@ -182,7 +184,8 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { BeaconBlockRoot: validBlockRoot[:], CommitteeIndex: 1, Slot: 1, - Target: ðpb.Checkpoint{}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, }, topic: fmt.Sprintf("/eth2/%x/beacon_attestation_1", digest), @@ -207,6 +210,8 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { break } } + } else { + tt.msg.Signature = make([]byte, 96) } buf := new(bytes.Buffer) _, err := p.Encoding().EncodeGossip(buf, tt.msg) diff --git a/beacon-chain/sync/validate_beacon_blocks_test.go b/beacon-chain/sync/validate_beacon_blocks_test.go index 6b353e77be..bac12d2f66 100644 --- a/beacon-chain/sync/validate_beacon_blocks_test.go +++ b/beacon-chain/sync/validate_beacon_blocks_test.go @@ -40,13 +40,10 @@ import ( func TestValidateBeaconBlockPubSub_InvalidSignature(t *testing.T) { ctx := context.Background() db, _ := dbtest.SetupDB(t) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: 1, - ParentRoot: testutil.Random32Bytes(t), - }, - Signature: bytesutil.PadTo([]byte("fake"), 96), - } + msg := testutil.NewBeaconBlock() + msg.Block.Slot = 1 + msg.Block.ParentRoot = testutil.Random32Bytes(t) + msg.Signature = bytesutil.PadTo([]byte("fake"), 96) p := p2ptest.NewTestP2P(t) @@ -88,12 +85,9 @@ func TestValidateBeaconBlockPubSub_BlockAlreadyPresentInDB(t *testing.T) { ctx := context.Background() p := p2ptest.NewTestP2P(t) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: 100, - ParentRoot: testutil.Random32Bytes(t), - }, - } + msg := testutil.NewBeaconBlock() + msg.Block.Slot = 100 + msg.Block.ParentRoot = testutil.Random32Bytes(t) require.NoError(t, db.SaveBlock(context.Background(), msg)) c, err := lru.New(10) @@ -133,12 +127,7 @@ func TestValidateBeaconBlockPubSub_ValidProposerSignature(t *testing.T) { p := p2ptest.NewTestP2P(t) ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) - parentBlock := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: 0, - Slot: 0, - }, - } + parentBlock := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, parentBlock)) bRoot, err := stateutil.BlockRoot(parentBlock.Block) require.NoError(t, err) @@ -148,13 +137,10 @@ func TestValidateBeaconBlockPubSub_ValidProposerSignature(t *testing.T) { require.NoError(t, copied.SetSlot(1)) proposerIdx, err := helpers.BeaconProposerIndex(copied) require.NoError(t, err) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: proposerIdx, - Slot: 1, - ParentRoot: bRoot[:], - }, - } + msg := testutil.NewBeaconBlock() + msg.Block.ParentRoot = bRoot[:] + msg.Block.Slot = 1 + msg.Block.ProposerIndex = proposerIdx msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) require.NoError(t, err) @@ -167,7 +153,9 @@ func TestValidateBeaconBlockPubSub_ValidProposerSignature(t *testing.T) { State: beaconState, FinalizedCheckPoint: ðpb.Checkpoint{ Epoch: 0, - }} + Root: make([]byte, 32), + }, + } r := &Service{ db: db, p2p: p, @@ -202,12 +190,7 @@ func TestValidateBeaconBlockPubSub_AdvanceEpochsForState(t *testing.T) { p := p2ptest.NewTestP2P(t) ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) - parentBlock := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: 0, - Slot: 0, - }, - } + parentBlock := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, parentBlock)) bRoot, err := stateutil.BlockRoot(parentBlock.Block) require.NoError(t, err) @@ -220,13 +203,10 @@ func TestValidateBeaconBlockPubSub_AdvanceEpochsForState(t *testing.T) { require.NoError(t, err) proposerIdx, err := helpers.BeaconProposerIndex(copied) require.NoError(t, err) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: proposerIdx, - Slot: blkSlot, - ParentRoot: bRoot[:], - }, - } + msg := testutil.NewBeaconBlock() + msg.Block.ProposerIndex = proposerIdx + msg.Block.Slot = blkSlot + msg.Block.ParentRoot = bRoot[:] msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) require.NoError(t, err) @@ -277,12 +257,9 @@ func TestValidateBeaconBlockPubSub_Syncing(t *testing.T) { b32 := bytesutil.ToBytes32(b) sk, err := bls.SecretKeyFromBytes(b32[:]) require.NoError(t, err) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ParentRoot: testutil.Random32Bytes(t), - }, - Signature: sk.Sign([]byte("data")).Marshal(), - } + msg := testutil.NewBeaconBlock() + msg.Block.ParentRoot = testutil.Random32Bytes(t) + msg.Signature = sk.Sign([]byte("data")).Marshal() chainService := &mock.ChainService{ Genesis: time.Now(), FinalizedCheckPoint: ðpb.Checkpoint{ @@ -319,13 +296,10 @@ func TestValidateBeaconBlockPubSub_RejectBlocksFromFuture(t *testing.T) { b32 := bytesutil.ToBytes32(b) sk, err := bls.SecretKeyFromBytes(b32[:]) require.NoError(t, err) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ParentRoot: testutil.Random32Bytes(t), - Slot: 1000, - }, - Signature: sk.Sign([]byte("data")).Marshal(), - } + msg := testutil.NewBeaconBlock() + msg.Block.Slot = 1000 + msg.Block.ParentRoot = testutil.Random32Bytes(t) + msg.Signature = sk.Sign([]byte("data")).Marshal() c, err := lru.New(10) require.NoError(t, err) @@ -367,13 +341,10 @@ func TestValidateBeaconBlockPubSub_RejectBlocksFromThePast(t *testing.T) { ctx := context.Background() sk, err := bls.SecretKeyFromBytes(b32[:]) require.NoError(t, err) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ParentRoot: testutil.Random32Bytes(t), - Slot: 10, - }, - Signature: sk.Sign([]byte("data")).Marshal(), - } + msg := testutil.NewBeaconBlock() + msg.Block.ParentRoot = testutil.Random32Bytes(t) + msg.Block.Slot = 10 + msg.Signature = sk.Sign([]byte("data")).Marshal() genesisTime := time.Now() c, err := lru.New(10) @@ -384,7 +355,8 @@ func TestValidateBeaconBlockPubSub_RejectBlocksFromThePast(t *testing.T) { Genesis: time.Unix(genesisTime.Unix()-1000, 0), FinalizedCheckPoint: ðpb.Checkpoint{ Epoch: 1, - }} + }, + } r := &Service{ db: db, p2p: p, @@ -415,12 +387,7 @@ func TestValidateBeaconBlockPubSub_SeenProposerSlot(t *testing.T) { p := p2ptest.NewTestP2P(t) ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) - parentBlock := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: 0, - Slot: 0, - }, - } + parentBlock := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, parentBlock)) bRoot, err := stateutil.BlockRoot(parentBlock.Block) require.NoError(t, err) @@ -428,13 +395,10 @@ func TestValidateBeaconBlockPubSub_SeenProposerSlot(t *testing.T) { proposerIdx, err := helpers.BeaconProposerIndex(beaconState) require.NoError(t, err) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: proposerIdx, - Slot: 1, - ParentRoot: bRoot[:], - }, - } + msg := testutil.NewBeaconBlock() + msg.Block.Slot = 1 + msg.Block.ProposerIndex = proposerIdx + msg.Block.ParentRoot = bRoot[:] msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) require.NoError(t, err) @@ -446,7 +410,9 @@ func TestValidateBeaconBlockPubSub_SeenProposerSlot(t *testing.T) { State: beaconState, FinalizedCheckPoint: ðpb.Checkpoint{ Epoch: 0, - }} + Root: make([]byte, 32), + }, + } r := &Service{ db: db, p2p: p, @@ -482,7 +448,7 @@ func TestValidateBeaconBlockPubSub_FilterByFinalizedEpoch(t *testing.T) { db, _ := dbtest.SetupDB(t) p := p2ptest.NewTestP2P(t) - parent := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} + parent := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(context.Background(), parent)) parentRoot, err := stateutil.BlockRoot(parent.Block) require.NoError(t, err) @@ -505,9 +471,9 @@ func TestValidateBeaconBlockPubSub_FilterByFinalizedEpoch(t *testing.T) { initialSync: &mockSync.Sync{IsSyncing: false}, } - b := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{Slot: 1, ParentRoot: parentRoot[:], Body: ðpb.BeaconBlockBody{}}, - } + b := testutil.NewBeaconBlock() + b.Block.Slot = 1 + b.Block.ParentRoot = parentRoot[:] buf := new(bytes.Buffer) _, err = p.Encoding().EncodeGossip(buf, b) require.NoError(t, err) @@ -547,12 +513,7 @@ func TestValidateBeaconBlockPubSub_ParentNotFinalizedDescendant(t *testing.T) { p := p2ptest.NewTestP2P(t) ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) - parentBlock := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: 0, - Slot: 0, - }, - } + parentBlock := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, parentBlock)) bRoot, err := stateutil.BlockRoot(parentBlock.Block) require.NoError(t, err) @@ -562,13 +523,10 @@ func TestValidateBeaconBlockPubSub_ParentNotFinalizedDescendant(t *testing.T) { require.NoError(t, copied.SetSlot(1)) proposerIdx, err := helpers.BeaconProposerIndex(copied) require.NoError(t, err) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: proposerIdx, - Slot: 1, - ParentRoot: bRoot[:], - }, - } + msg := testutil.NewBeaconBlock() + msg.Block.Slot = 1 + msg.Block.ProposerIndex = proposerIdx + msg.Block.ParentRoot = bRoot[:] msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) require.NoError(t, err) @@ -581,6 +539,7 @@ func TestValidateBeaconBlockPubSub_ParentNotFinalizedDescendant(t *testing.T) { State: beaconState, FinalizedCheckPoint: ðpb.Checkpoint{ Epoch: 0, + Root: make([]byte, 32), }, VerifyBlkDescendantErr: errors.New("not part of finalized chain"), } @@ -617,12 +576,7 @@ func TestValidateBeaconBlockPubSub_InvalidParentBlock(t *testing.T) { p := p2ptest.NewTestP2P(t) ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) - parentBlock := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: 0, - Slot: 0, - }, - } + parentBlock := testutil.NewBeaconBlock() require.NoError(t, db.SaveBlock(ctx, parentBlock)) bRoot, err := stateutil.BlockRoot(parentBlock.Block) require.NoError(t, err) @@ -632,13 +586,10 @@ func TestValidateBeaconBlockPubSub_InvalidParentBlock(t *testing.T) { require.NoError(t, copied.SetSlot(1)) proposerIdx, err := helpers.BeaconProposerIndex(copied) require.NoError(t, err) - msg := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: proposerIdx, - Slot: 1, - ParentRoot: bRoot[:], - }, - } + msg := testutil.NewBeaconBlock() + msg.Block.ProposerIndex = proposerIdx + msg.Block.Slot = 1 + msg.Block.ParentRoot = bRoot[:] msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) require.NoError(t, err) @@ -688,13 +639,10 @@ func TestValidateBeaconBlockPubSub_InvalidParentBlock(t *testing.T) { proposerIdx, err = helpers.BeaconProposerIndex(copied) require.NoError(t, err) - msg = ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - ProposerIndex: proposerIdx, - Slot: 2, - ParentRoot: currBlockRoot[:], - }, - } + msg = testutil.NewBeaconBlock() + msg.Block.Slot = 2 + msg.Block.ProposerIndex = proposerIdx + msg.Block.ParentRoot = currBlockRoot[:] buf = new(bytes.Buffer) _, err = p.Encoding().EncodeGossip(buf, msg) diff --git a/contracts/deposit-contract/BUILD.bazel b/contracts/deposit-contract/BUILD.bazel index 5a0ab7e0d9..1d601c8f3a 100644 --- a/contracts/deposit-contract/BUILD.bazel +++ b/contracts/deposit-contract/BUILD.bazel @@ -39,6 +39,5 @@ go_test( "@com_github_ethereum_go_ethereum//:go_default_library", "@com_github_ethereum_go_ethereum//accounts/abi/bind:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", ], ) diff --git a/contracts/deposit-contract/deposit_tree_test.go b/contracts/deposit-contract/deposit_tree_test.go index e12bead607..bf108f5a17 100644 --- a/contracts/deposit-contract/deposit_tree_test.go +++ b/contracts/deposit-contract/deposit_tree_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/prysmaticlabs/go-ssz" depositcontract "github.com/prysmaticlabs/prysm/contracts/deposit-contract" "github.com/prysmaticlabs/prysm/shared/interop" "github.com/prysmaticlabs/prysm/shared/params" @@ -53,7 +52,7 @@ func TestDepositTrieRoot_OK(t *testing.T) { } testAcc.Backend.Commit() - item, err := ssz.HashTreeRoot(data) + item, err := data.HashTreeRoot() if err != nil { t.Fatal(err) } @@ -112,7 +111,7 @@ func TestDepositTrieRoot_Fail(t *testing.T) { copy(data.PublicKey, strconv.Itoa(i+10)) testAcc.Backend.Commit() - item, err := ssz.HashTreeRoot(data) + item, err := data.HashTreeRoot() if err != nil { t.Fatal(err) } diff --git a/deps.bzl b/deps.bzl index d5f19c44a4..4a382bc642 100644 --- a/deps.bzl +++ b/deps.bzl @@ -45,8 +45,8 @@ def prysm_deps(): name = "com_github_ferranbt_fastssz", importpath = "github.com/ferranbt/fastssz", nofuzz = True, - sum = "h1:maoKvILdMk6CSWHanFcUdxXIZGKD9YpWIaVbUQ/4kfg=", - version = "v0.0.0-20200514094935-99fccaf93472", + sum = "h1:Sbm5yhPObRc+RCZa9fAR/Px/j1qYwuqawF0X8dR2gAw=", + version = "v0.0.0-20200818222714-826c7ef45b30", ) go_repository( name = "com_github_prysmaticlabs_bazel_go_ethereum", @@ -1027,8 +1027,8 @@ def prysm_deps(): go_repository( name = "com_github_mitchellh_mapstructure", importpath = "github.com/mitchellh/mapstructure", - sum = "h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=", - version = "v1.1.2", + sum = "h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg=", + version = "v1.3.2", ) go_repository( name = "com_github_mmcloughlin_avo", @@ -2570,8 +2570,8 @@ def prysm_deps(): name = "com_github_prysmaticlabs_ethereumapis", build_file_generation = "off", importpath = "github.com/prysmaticlabs/ethereumapis", - sum = "h1:GjYix8Y4VpQhlsjA2ickr3HxjIns4bI36zOmC+lwaNw=", - version = "v0.0.0-20200709024211-e8095222f77b", + sum = "h1:0f++UXRfp4/Mrmlfj3UaCnYj2lPr6El0gWWTBb9MD2Y=", + version = "v0.0.0-20200812153649-a842fc47c2c3", ) go_repository( name = "com_github_prysmaticlabs_go_bitfield", diff --git a/fuzz/BUILD.bazel b/fuzz/BUILD.bazel index aec1df6e32..1e8f306321 100644 --- a/fuzz/BUILD.bazel +++ b/fuzz/BUILD.bazel @@ -188,6 +188,7 @@ go_fuzz_test( "//beacon-chain/sync/initial-sync/testing:go_default_library", "//beacon-chain/cache:go_default_library", "//proto/beacon/p2p/v1:go_default_library", + "//shared/bytesutil:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_libp2p_go_libp2p//:go_default_library", "@com_github_libp2p_go_libp2p_core//host:go_default_library", @@ -245,6 +246,7 @@ go_library( "//proto/beacon/p2p/v1:go_default_library", "//shared/featureconfig:go_default_library", "//shared/params:go_default_library", + "//shared/bytesutil:go_default_library", "@com_github_libp2p_go_libp2p//:go_default_library", "@com_github_libp2p_go_libp2p_core//host:go_default_library", "@com_github_libp2p_go_libp2p_core//network:go_default_library", diff --git a/fuzz/generated.ssz.go b/fuzz/generated.ssz.go index 2bebaa4664..40d269550f 100755 --- a/fuzz/generated.ssz.go +++ b/fuzz/generated.ssz.go @@ -2,36 +2,22 @@ package fuzz import ( - "fmt" - ssz "github.com/ferranbt/fastssz" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" ) -var ( - errDivideInt = fmt.Errorf("incorrect int divide") - errListTooBig = fmt.Errorf("incorrect list size, too big") - errMarshalDynamicBytes = fmt.Errorf("incorrect dynamic bytes marshalling") - errMarshalFixedBytes = fmt.Errorf("incorrect fixed bytes marshalling") - errMarshalList = fmt.Errorf("incorrect vector list") - errMarshalVector = fmt.Errorf("incorrect vector marshalling") - errOffset = fmt.Errorf("incorrect offset") - errSize = fmt.Errorf("incorrect size") -) - // MarshalSSZ ssz marshals the InputBlockHeader object func (i *InputBlockHeader) MarshalSSZ() ([]byte, error) { - buf := make([]byte, i.SizeSSZ()) - return i.MarshalSSZTo(buf[:0]) + return ssz.MarshalSSZ(i) } // MarshalSSZTo ssz marshals the InputBlockHeader object to a target array -func (i *InputBlockHeader) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error - offset := int(6) +func (i *InputBlockHeader) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(12) // Field (0) 'StateID' - dst = ssz.MarshalUint16(dst, i.StateID) + dst = ssz.MarshalUint64(dst, i.StateID) // Offset (1) 'Block' dst = ssz.WriteOffset(dst, offset) @@ -42,29 +28,29 @@ func (i *InputBlockHeader) MarshalSSZTo(dst []byte) ([]byte, error) { // Field (1) 'Block' if dst, err = i.Block.MarshalSSZTo(dst); err != nil { - return nil, err + return } - return dst, err + return } // UnmarshalSSZ ssz unmarshals the InputBlockHeader object func (i *InputBlockHeader) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) - if size < 6 { - return errSize + if size < 12 { + return ssz.ErrSize } tail := buf var o1 uint64 // Field (0) 'StateID' - i.StateID = ssz.UnmarshallUint16(buf[0:2]) + i.StateID = ssz.UnmarshallUint64(buf[0:8]) // Offset (1) 'Block' - if o1 = ssz.ReadOffset(buf[2:6]); o1 > size { - return errOffset + if o1 = ssz.ReadOffset(buf[8:12]); o1 > size { + return ssz.ErrOffset } // Field (1) 'Block' @@ -82,7 +68,7 @@ func (i *InputBlockHeader) UnmarshalSSZ(buf []byte) error { // SizeSSZ returns the ssz encoded size in bytes for the InputBlockHeader object func (i *InputBlockHeader) SizeSSZ() (size int) { - size = 6 + size = 12 // Field (1) 'Block' if i.Block == nil { @@ -93,19 +79,39 @@ func (i *InputBlockHeader) SizeSSZ() (size int) { return } +// HashTreeRoot ssz hashes the InputBlockHeader object +func (i *InputBlockHeader) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(i) +} + +// HashTreeRootWith ssz hashes the InputBlockHeader object with a hasher +func (i *InputBlockHeader) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'StateID' + hh.PutUint64(i.StateID) + + // Field (1) 'Block' + if err = i.Block.HashTreeRootWith(hh); err != nil { + return + } + + hh.Merkleize(indx) + return +} + // MarshalSSZ ssz marshals the InputAttesterSlashingWrapper object func (i *InputAttesterSlashingWrapper) MarshalSSZ() ([]byte, error) { - buf := make([]byte, i.SizeSSZ()) - return i.MarshalSSZTo(buf[:0]) + return ssz.MarshalSSZ(i) } // MarshalSSZTo ssz marshals the InputAttesterSlashingWrapper object to a target array -func (i *InputAttesterSlashingWrapper) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error - offset := int(6) +func (i *InputAttesterSlashingWrapper) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(12) // Field (0) 'StateID' - dst = ssz.MarshalUint16(dst, i.StateID) + dst = ssz.MarshalUint64(dst, i.StateID) // Offset (1) 'AttesterSlashing' dst = ssz.WriteOffset(dst, offset) @@ -116,29 +122,29 @@ func (i *InputAttesterSlashingWrapper) MarshalSSZTo(dst []byte) ([]byte, error) // Field (1) 'AttesterSlashing' if dst, err = i.AttesterSlashing.MarshalSSZTo(dst); err != nil { - return nil, err + return } - return dst, err + return } // UnmarshalSSZ ssz unmarshals the InputAttesterSlashingWrapper object func (i *InputAttesterSlashingWrapper) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) - if size < 6 { - return errSize + if size < 12 { + return ssz.ErrSize } tail := buf var o1 uint64 // Field (0) 'StateID' - i.StateID = ssz.UnmarshallUint16(buf[0:2]) + i.StateID = ssz.UnmarshallUint64(buf[0:8]) // Offset (1) 'AttesterSlashing' - if o1 = ssz.ReadOffset(buf[2:6]); o1 > size { - return errOffset + if o1 = ssz.ReadOffset(buf[8:12]); o1 > size { + return ssz.ErrOffset } // Field (1) 'AttesterSlashing' @@ -156,7 +162,7 @@ func (i *InputAttesterSlashingWrapper) UnmarshalSSZ(buf []byte) error { // SizeSSZ returns the ssz encoded size in bytes for the InputAttesterSlashingWrapper object func (i *InputAttesterSlashingWrapper) SizeSSZ() (size int) { - size = 6 + size = 12 // Field (1) 'AttesterSlashing' if i.AttesterSlashing == nil { @@ -167,19 +173,39 @@ func (i *InputAttesterSlashingWrapper) SizeSSZ() (size int) { return } +// HashTreeRoot ssz hashes the InputAttesterSlashingWrapper object +func (i *InputAttesterSlashingWrapper) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(i) +} + +// HashTreeRootWith ssz hashes the InputAttesterSlashingWrapper object with a hasher +func (i *InputAttesterSlashingWrapper) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'StateID' + hh.PutUint64(i.StateID) + + // Field (1) 'AttesterSlashing' + if err = i.AttesterSlashing.HashTreeRootWith(hh); err != nil { + return + } + + hh.Merkleize(indx) + return +} + // MarshalSSZ ssz marshals the InputAttestationWrapper object func (i *InputAttestationWrapper) MarshalSSZ() ([]byte, error) { - buf := make([]byte, i.SizeSSZ()) - return i.MarshalSSZTo(buf[:0]) + return ssz.MarshalSSZ(i) } // MarshalSSZTo ssz marshals the InputAttestationWrapper object to a target array -func (i *InputAttestationWrapper) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error - offset := int(6) +func (i *InputAttestationWrapper) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(12) // Field (0) 'StateID' - dst = ssz.MarshalUint16(dst, i.StateID) + dst = ssz.MarshalUint64(dst, i.StateID) // Offset (1) 'Attestation' dst = ssz.WriteOffset(dst, offset) @@ -190,29 +216,29 @@ func (i *InputAttestationWrapper) MarshalSSZTo(dst []byte) ([]byte, error) { // Field (1) 'Attestation' if dst, err = i.Attestation.MarshalSSZTo(dst); err != nil { - return nil, err + return } - return dst, err + return } // UnmarshalSSZ ssz unmarshals the InputAttestationWrapper object func (i *InputAttestationWrapper) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) - if size < 6 { - return errSize + if size < 12 { + return ssz.ErrSize } tail := buf var o1 uint64 // Field (0) 'StateID' - i.StateID = ssz.UnmarshallUint16(buf[0:2]) + i.StateID = ssz.UnmarshallUint64(buf[0:8]) // Offset (1) 'Attestation' - if o1 = ssz.ReadOffset(buf[2:6]); o1 > size { - return errOffset + if o1 = ssz.ReadOffset(buf[8:12]); o1 > size { + return ssz.ErrOffset } // Field (1) 'Attestation' @@ -230,7 +256,7 @@ func (i *InputAttestationWrapper) UnmarshalSSZ(buf []byte) error { // SizeSSZ returns the ssz encoded size in bytes for the InputAttestationWrapper object func (i *InputAttestationWrapper) SizeSSZ() (size int) { - size = 6 + size = 12 // Field (1) 'Attestation' if i.Attestation == nil { @@ -241,46 +267,66 @@ func (i *InputAttestationWrapper) SizeSSZ() (size int) { return } +// HashTreeRoot ssz hashes the InputAttestationWrapper object +func (i *InputAttestationWrapper) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(i) +} + +// HashTreeRootWith ssz hashes the InputAttestationWrapper object with a hasher +func (i *InputAttestationWrapper) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'StateID' + hh.PutUint64(i.StateID) + + // Field (1) 'Attestation' + if err = i.Attestation.HashTreeRootWith(hh); err != nil { + return + } + + hh.Merkleize(indx) + return +} + // MarshalSSZ ssz marshals the InputDepositWrapper object func (i *InputDepositWrapper) MarshalSSZ() ([]byte, error) { - buf := make([]byte, i.SizeSSZ()) - return i.MarshalSSZTo(buf[:0]) + return ssz.MarshalSSZ(i) } // MarshalSSZTo ssz marshals the InputDepositWrapper object to a target array -func (i *InputDepositWrapper) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error +func (i *InputDepositWrapper) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf // Field (0) 'StateID' - dst = ssz.MarshalUint16(dst, i.StateID) + dst = ssz.MarshalUint64(dst, i.StateID) // Field (1) 'Deposit' if i.Deposit == nil { i.Deposit = new(ethpb.Deposit) } if dst, err = i.Deposit.MarshalSSZTo(dst); err != nil { - return nil, err + return } - return dst, err + return } // UnmarshalSSZ ssz unmarshals the InputDepositWrapper object func (i *InputDepositWrapper) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) - if size != 1242 { - return errSize + if size != 1248 { + return ssz.ErrSize } // Field (0) 'StateID' - i.StateID = ssz.UnmarshallUint16(buf[0:2]) + i.StateID = ssz.UnmarshallUint64(buf[0:8]) // Field (1) 'Deposit' if i.Deposit == nil { i.Deposit = new(ethpb.Deposit) } - if err = i.Deposit.UnmarshalSSZ(buf[2:1242]); err != nil { + if err = i.Deposit.UnmarshalSSZ(buf[8:1248]); err != nil { return err } @@ -289,50 +335,70 @@ func (i *InputDepositWrapper) UnmarshalSSZ(buf []byte) error { // SizeSSZ returns the ssz encoded size in bytes for the InputDepositWrapper object func (i *InputDepositWrapper) SizeSSZ() (size int) { - size = 1242 + size = 1248 + return +} + +// HashTreeRoot ssz hashes the InputDepositWrapper object +func (i *InputDepositWrapper) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(i) +} + +// HashTreeRootWith ssz hashes the InputDepositWrapper object with a hasher +func (i *InputDepositWrapper) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'StateID' + hh.PutUint64(i.StateID) + + // Field (1) 'Deposit' + if err = i.Deposit.HashTreeRootWith(hh); err != nil { + return + } + + hh.Merkleize(indx) return } // MarshalSSZ ssz marshals the InputVoluntaryExitWrapper object func (i *InputVoluntaryExitWrapper) MarshalSSZ() ([]byte, error) { - buf := make([]byte, i.SizeSSZ()) - return i.MarshalSSZTo(buf[:0]) + return ssz.MarshalSSZ(i) } // MarshalSSZTo ssz marshals the InputVoluntaryExitWrapper object to a target array -func (i *InputVoluntaryExitWrapper) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error +func (i *InputVoluntaryExitWrapper) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf // Field (0) 'StateID' - dst = ssz.MarshalUint16(dst, i.StateID) + dst = ssz.MarshalUint64(dst, i.StateID) // Field (1) 'VoluntaryExit' if i.VoluntaryExit == nil { i.VoluntaryExit = new(ethpb.VoluntaryExit) } if dst, err = i.VoluntaryExit.MarshalSSZTo(dst); err != nil { - return nil, err + return } - return dst, err + return } // UnmarshalSSZ ssz unmarshals the InputVoluntaryExitWrapper object func (i *InputVoluntaryExitWrapper) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) - if size != 18 { - return errSize + if size != 24 { + return ssz.ErrSize } // Field (0) 'StateID' - i.StateID = ssz.UnmarshallUint16(buf[0:2]) + i.StateID = ssz.UnmarshallUint64(buf[0:8]) // Field (1) 'VoluntaryExit' if i.VoluntaryExit == nil { i.VoluntaryExit = new(ethpb.VoluntaryExit) } - if err = i.VoluntaryExit.UnmarshalSSZ(buf[2:18]); err != nil { + if err = i.VoluntaryExit.UnmarshalSSZ(buf[8:24]); err != nil { return err } @@ -341,50 +407,70 @@ func (i *InputVoluntaryExitWrapper) UnmarshalSSZ(buf []byte) error { // SizeSSZ returns the ssz encoded size in bytes for the InputVoluntaryExitWrapper object func (i *InputVoluntaryExitWrapper) SizeSSZ() (size int) { - size = 18 + size = 24 + return +} + +// HashTreeRoot ssz hashes the InputVoluntaryExitWrapper object +func (i *InputVoluntaryExitWrapper) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(i) +} + +// HashTreeRootWith ssz hashes the InputVoluntaryExitWrapper object with a hasher +func (i *InputVoluntaryExitWrapper) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'StateID' + hh.PutUint64(i.StateID) + + // Field (1) 'VoluntaryExit' + if err = i.VoluntaryExit.HashTreeRootWith(hh); err != nil { + return + } + + hh.Merkleize(indx) return } // MarshalSSZ ssz marshals the InputProposerSlashingWrapper object func (i *InputProposerSlashingWrapper) MarshalSSZ() ([]byte, error) { - buf := make([]byte, i.SizeSSZ()) - return i.MarshalSSZTo(buf[:0]) + return ssz.MarshalSSZ(i) } // MarshalSSZTo ssz marshals the InputProposerSlashingWrapper object to a target array -func (i *InputProposerSlashingWrapper) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error +func (i *InputProposerSlashingWrapper) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf // Field (0) 'StateID' - dst = ssz.MarshalUint16(dst, i.StateID) + dst = ssz.MarshalUint64(dst, i.StateID) // Field (1) 'ProposerSlashing' if i.ProposerSlashing == nil { i.ProposerSlashing = new(ethpb.ProposerSlashing) } if dst, err = i.ProposerSlashing.MarshalSSZTo(dst); err != nil { - return nil, err + return } - return dst, err + return } // UnmarshalSSZ ssz unmarshals the InputProposerSlashingWrapper object func (i *InputProposerSlashingWrapper) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) - if size != 418 { - return errSize + if size != 424 { + return ssz.ErrSize } // Field (0) 'StateID' - i.StateID = ssz.UnmarshallUint16(buf[0:2]) + i.StateID = ssz.UnmarshallUint64(buf[0:8]) // Field (1) 'ProposerSlashing' if i.ProposerSlashing == nil { i.ProposerSlashing = new(ethpb.ProposerSlashing) } - if err = i.ProposerSlashing.UnmarshalSSZ(buf[2:418]); err != nil { + if err = i.ProposerSlashing.UnmarshalSSZ(buf[8:424]); err != nil { return err } @@ -393,6 +479,27 @@ func (i *InputProposerSlashingWrapper) UnmarshalSSZ(buf []byte) error { // SizeSSZ returns the ssz encoded size in bytes for the InputProposerSlashingWrapper object func (i *InputProposerSlashingWrapper) SizeSSZ() (size int) { - size = 418 + size = 424 + return +} + +// HashTreeRoot ssz hashes the InputProposerSlashingWrapper object +func (i *InputProposerSlashingWrapper) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(i) +} + +// HashTreeRootWith ssz hashes the InputProposerSlashingWrapper object with a hasher +func (i *InputProposerSlashingWrapper) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'StateID' + hh.PutUint64(i.StateID) + + // Field (1) 'ProposerSlashing' + if err = i.ProposerSlashing.HashTreeRootWith(hh); err != nil { + return + } + + hh.Merkleize(indx) return } diff --git a/fuzz/inputs.go b/fuzz/inputs.go index 835a9c0e90..57160bb84d 100644 --- a/fuzz/inputs.go +++ b/fuzz/inputs.go @@ -6,36 +6,36 @@ import ( // InputBlockHeader for fuzz testing beacon block headers. type InputBlockHeader struct { - StateID uint16 + StateID uint64 Block *ethpb.BeaconBlock } // InputAttesterSlashingWrapper for fuzz testing attester slashing. type InputAttesterSlashingWrapper struct { - StateID uint16 + StateID uint64 AttesterSlashing *ethpb.AttesterSlashing } // InputAttestationWrapper for fuzz testing attestations. type InputAttestationWrapper struct { - StateID uint16 + StateID uint64 Attestation *ethpb.Attestation } // InputDepositWrapper for fuzz testing deposits. type InputDepositWrapper struct { - StateID uint16 + StateID uint64 Deposit *ethpb.Deposit } // InputVoluntaryExitWrapper for fuzz testing voluntary exits. type InputVoluntaryExitWrapper struct { - StateID uint16 + StateID uint64 VoluntaryExit *ethpb.VoluntaryExit } // InputProposerSlashingWrapper for fuzz testing proposer slashings. type InputProposerSlashingWrapper struct { - StateID uint16 + StateID uint64 ProposerSlashing *ethpb.ProposerSlashing } diff --git a/fuzz/rpc_status_fuzz.go b/fuzz/rpc_status_fuzz.go index e2d96f44e9..7be32979c0 100644 --- a/fuzz/rpc_status_fuzz.go +++ b/fuzz/rpc_status_fuzz.go @@ -15,6 +15,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/sync" mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/sirupsen/logrus" ) @@ -51,8 +52,8 @@ func init() { ExitPool: nil, SlashingPool: nil, Chain: &mock.ChainService{ - Root: []byte("root"), - FinalizedCheckPoint: ðpb.Checkpoint{Epoch: 4}, + Root: bytesutil.PadTo([]byte("root"), 32), + FinalizedCheckPoint: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, Fork: &pb.Fork{CurrentVersion: []byte("foo")}, }, StateNotifier: (&mock.ChainService{}).StateNotifier(), diff --git a/fuzz/testing/beacon_fuzz_states.go b/fuzz/testing/beacon_fuzz_states.go index 9335bb761c..4f1e85b520 100644 --- a/fuzz/testing/beacon_fuzz_states.go +++ b/fuzz/testing/beacon_fuzz_states.go @@ -13,7 +13,7 @@ const fileBase = "0-11-0/mainnet/beaconstate" const fileBaseENV = "BEACONSTATES_PATH" // GetBeaconFuzzState returns a beacon state by ID using the beacon-fuzz corpora. -func GetBeaconFuzzState(ID uint16) (*pb.BeaconState, error) { +func GetBeaconFuzzState(ID uint64) (*pb.BeaconState, error) { base := fileBase // Using an environment variable allows a host image to specify the path when only the binary // executable was uploaded (without the runfiles). i.e. fuzzit's platform. diff --git a/go.mod b/go.mod index b72464af25..7828de23b6 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/emicklei/dot v0.11.0 github.com/ethereum/go-ethereum v0.0.0-00010101000000-000000000000 github.com/fatih/color v1.9.0 // indirect - github.com/ferranbt/fastssz v0.0.0-20200514094935-99fccaf93472 + github.com/ferranbt/fastssz v0.0.0-20200818222714-826c7ef45b30 github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect github.com/ghodss/yaml v1.0.0 @@ -87,7 +87,7 @@ require ( github.com/prometheus/client_golang v1.7.1 github.com/prometheus/tsdb v0.10.0 // indirect github.com/protolambda/zssz v0.1.5 - github.com/prysmaticlabs/ethereumapis v0.0.0-20200709024211-e8095222f77b + github.com/prysmaticlabs/ethereumapis v0.0.0-20200812153649-a842fc47c2c3 github.com/prysmaticlabs/go-bitfield v0.0.0-20200618145306-2ae0807bef65 github.com/prysmaticlabs/go-ssz v0.0.0-20200612203617-6d5c9aa213ae github.com/prysmaticlabs/prombbolt v0.0.0-20200324184628-09789ef63796 diff --git a/go.sum b/go.sum index 49f4e42bea..8cfaf2d2c3 100644 --- a/go.sum +++ b/go.sum @@ -215,6 +215,9 @@ github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/ferranbt/fastssz v0.0.0-20200514094935-99fccaf93472 h1:maoKvILdMk6CSWHanFcUdxXIZGKD9YpWIaVbUQ/4kfg= github.com/ferranbt/fastssz v0.0.0-20200514094935-99fccaf93472/go.mod h1:LlFXPmgrgVYsuoFDwV8rDJ9tvt1pLQdjKvU1b5IRES0= +github.com/ferranbt/fastssz v0.0.0-20200728110133-0b6e349af87a/go.mod h1:DyEu2iuLBnb/T51BlsiO3yLYdJC6UbGMrIkqK1KmQxM= +github.com/ferranbt/fastssz v0.0.0-20200818222714-826c7ef45b30 h1:Sbm5yhPObRc+RCZa9fAR/Px/j1qYwuqawF0X8dR2gAw= +github.com/ferranbt/fastssz v0.0.0-20200818222714-826c7ef45b30/go.mod h1:DyEu2iuLBnb/T51BlsiO3yLYdJC6UbGMrIkqK1KmQxM= github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 h1:FtmdgXiUlNeRsoNMFlKLDt+S+6hbjVMEW6RGQ7aUf7c= github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= @@ -808,6 +811,8 @@ github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0 github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg= +github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -982,8 +987,8 @@ github.com/protolambda/zssz v0.1.5 h1:7fjJjissZIIaa2QcvmhS/pZISMX21zVITt49sW1oue github.com/protolambda/zssz v0.1.5/go.mod h1:a4iwOX5FE7/JkKA+J/PH0Mjo9oXftN6P8NZyL28gpag= github.com/prysmaticlabs/bazel-go-ethereum v0.0.0-20200626171358-a933315235ec h1:9JrPtwqCvV38DXYaHbB855KUIHYMwjJBE88lL8lMu8Q= github.com/prysmaticlabs/bazel-go-ethereum v0.0.0-20200626171358-a933315235ec/go.mod h1:oP8FC5+TbICUyftkTWs+8JryntjIJLJvWvApK3z2AYw= -github.com/prysmaticlabs/ethereumapis v0.0.0-20200709024211-e8095222f77b h1:GjYix8Y4VpQhlsjA2ickr3HxjIns4bI36zOmC+lwaNw= -github.com/prysmaticlabs/ethereumapis v0.0.0-20200709024211-e8095222f77b/go.mod h1:rs05kpTfWKl0KflsBWzBQFstoyPFMTWQTbxSAyGHe78= +github.com/prysmaticlabs/ethereumapis v0.0.0-20200812153649-a842fc47c2c3 h1:0f++UXRfp4/Mrmlfj3UaCnYj2lPr6El0gWWTBb9MD2Y= +github.com/prysmaticlabs/ethereumapis v0.0.0-20200812153649-a842fc47c2c3/go.mod h1:k7b2dxy6RppCG6kmOJkNOXzRpEoTdsPygc2aQhsUsZk= github.com/prysmaticlabs/go-bitfield v0.0.0-20191017011753-53b773adde52/go.mod h1:hCwmef+4qXWjv0jLDbQdWnL0Ol7cS7/lCSS26WR+u6s= github.com/prysmaticlabs/go-bitfield v0.0.0-20200322041314-62c2aee71669 h1:cX6YRZnZ9sgMqM5U14llxUiXVNJ3u07Res1IIjTOgtI= github.com/prysmaticlabs/go-bitfield v0.0.0-20200322041314-62c2aee71669/go.mod h1:hCwmef+4qXWjv0jLDbQdWnL0Ol7cS7/lCSS26WR+u6s= diff --git a/proto/beacon/p2p/v1/BUILD.bazel b/proto/beacon/p2p/v1/BUILD.bazel index 76a12c9405..2687b9354c 100644 --- a/proto/beacon/p2p/v1/BUILD.bazel +++ b/proto/beacon/p2p/v1/BUILD.bazel @@ -31,9 +31,11 @@ ssz_gen_marshal( "ENRForkID", "MetaData", "Fork", + "ForkData", "HistoricalBatch", "Status", "BeaconState", + "SigningData", ], ) diff --git a/proto/beacon/p2p/v1/generated.ssz.go b/proto/beacon/p2p/v1/generated.ssz.go index 5004f0e813..ccdbe07e37 100755 --- a/proto/beacon/p2p/v1/generated.ssz.go +++ b/proto/beacon/p2p/v1/generated.ssz.go @@ -2,41 +2,569 @@ package ethereum_beacon_p2p_v1 import ( - "fmt" - ssz "github.com/ferranbt/fastssz" v1alpha1 "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" ) -var ( - errDivideInt = fmt.Errorf("incorrect int divide") - errListTooBig = fmt.Errorf("incorrect list size, too big") - errMarshalDynamicBytes = fmt.Errorf("incorrect dynamic bytes marshalling") - errMarshalFixedBytes = fmt.Errorf("incorrect fixed bytes marshalling") - errMarshalList = fmt.Errorf("incorrect vector list") - errMarshalVector = fmt.Errorf("incorrect vector marshalling") - errOffset = fmt.Errorf("incorrect offset") - errSize = fmt.Errorf("incorrect size") -) +// MarshalSSZ ssz marshals the Status object +func (s *Status) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(s) +} + +// MarshalSSZTo ssz marshals the Status object to a target array +func (s *Status) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + + // Field (0) 'ForkDigest' + if len(s.ForkDigest) != 4 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, s.ForkDigest...) + + // Field (1) 'FinalizedRoot' + if len(s.FinalizedRoot) != 32 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, s.FinalizedRoot...) + + // Field (2) 'FinalizedEpoch' + dst = ssz.MarshalUint64(dst, s.FinalizedEpoch) + + // Field (3) 'HeadRoot' + if len(s.HeadRoot) != 32 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, s.HeadRoot...) + + // Field (4) 'HeadSlot' + dst = ssz.MarshalUint64(dst, s.HeadSlot) + + return +} + +// UnmarshalSSZ ssz unmarshals the Status object +func (s *Status) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size != 84 { + return ssz.ErrSize + } + + // Field (0) 'ForkDigest' + if cap(s.ForkDigest) == 0 { + s.ForkDigest = make([]byte, 0, len(buf[0:4])) + } + s.ForkDigest = append(s.ForkDigest, buf[0:4]...) + + // Field (1) 'FinalizedRoot' + if cap(s.FinalizedRoot) == 0 { + s.FinalizedRoot = make([]byte, 0, len(buf[4:36])) + } + s.FinalizedRoot = append(s.FinalizedRoot, buf[4:36]...) + + // Field (2) 'FinalizedEpoch' + s.FinalizedEpoch = ssz.UnmarshallUint64(buf[36:44]) + + // Field (3) 'HeadRoot' + if cap(s.HeadRoot) == 0 { + s.HeadRoot = make([]byte, 0, len(buf[44:76])) + } + s.HeadRoot = append(s.HeadRoot, buf[44:76]...) + + // Field (4) 'HeadSlot' + s.HeadSlot = ssz.UnmarshallUint64(buf[76:84]) + + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the Status object +func (s *Status) SizeSSZ() (size int) { + size = 84 + return +} + +// HashTreeRoot ssz hashes the Status object +func (s *Status) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(s) +} + +// HashTreeRootWith ssz hashes the Status object with a hasher +func (s *Status) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'ForkDigest' + if len(s.ForkDigest) != 4 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(s.ForkDigest) + + // Field (1) 'FinalizedRoot' + if len(s.FinalizedRoot) != 32 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(s.FinalizedRoot) + + // Field (2) 'FinalizedEpoch' + hh.PutUint64(s.FinalizedEpoch) + + // Field (3) 'HeadRoot' + if len(s.HeadRoot) != 32 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(s.HeadRoot) + + // Field (4) 'HeadSlot' + hh.PutUint64(s.HeadSlot) + + hh.Merkleize(indx) + return +} + +// MarshalSSZ ssz marshals the BeaconBlocksByRangeRequest object +func (b *BeaconBlocksByRangeRequest) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(b) +} + +// MarshalSSZTo ssz marshals the BeaconBlocksByRangeRequest object to a target array +func (b *BeaconBlocksByRangeRequest) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + + // Field (0) 'StartSlot' + dst = ssz.MarshalUint64(dst, b.StartSlot) + + // Field (1) 'Count' + dst = ssz.MarshalUint64(dst, b.Count) + + // Field (2) 'Step' + dst = ssz.MarshalUint64(dst, b.Step) + + return +} + +// UnmarshalSSZ ssz unmarshals the BeaconBlocksByRangeRequest object +func (b *BeaconBlocksByRangeRequest) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size != 24 { + return ssz.ErrSize + } + + // Field (0) 'StartSlot' + b.StartSlot = ssz.UnmarshallUint64(buf[0:8]) + + // Field (1) 'Count' + b.Count = ssz.UnmarshallUint64(buf[8:16]) + + // Field (2) 'Step' + b.Step = ssz.UnmarshallUint64(buf[16:24]) + + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the BeaconBlocksByRangeRequest object +func (b *BeaconBlocksByRangeRequest) SizeSSZ() (size int) { + size = 24 + return +} + +// HashTreeRoot ssz hashes the BeaconBlocksByRangeRequest object +func (b *BeaconBlocksByRangeRequest) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(b) +} + +// HashTreeRootWith ssz hashes the BeaconBlocksByRangeRequest object with a hasher +func (b *BeaconBlocksByRangeRequest) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'StartSlot' + hh.PutUint64(b.StartSlot) + + // Field (1) 'Count' + hh.PutUint64(b.Count) + + // Field (2) 'Step' + hh.PutUint64(b.Step) + + hh.Merkleize(indx) + return +} + +// MarshalSSZ ssz marshals the BeaconBlocksByRootRequest object +func (b *BeaconBlocksByRootRequest) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(b) +} + +// MarshalSSZTo ssz marshals the BeaconBlocksByRootRequest object to a target array +func (b *BeaconBlocksByRootRequest) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(4) + + // Offset (0) 'BlockRoots' + dst = ssz.WriteOffset(dst, offset) + offset += len(b.BlockRoots) * 32 + + // Field (0) 'BlockRoots' + if len(b.BlockRoots) > 1024 { + err = ssz.ErrListTooBig + return + } + for ii := 0; ii < len(b.BlockRoots); ii++ { + if len(b.BlockRoots[ii]) != 32 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, b.BlockRoots[ii]...) + } + + return +} + +// UnmarshalSSZ ssz unmarshals the BeaconBlocksByRootRequest object +func (b *BeaconBlocksByRootRequest) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 4 { + return ssz.ErrSize + } + + tail := buf + var o0 uint64 + + // Offset (0) 'BlockRoots' + if o0 = ssz.ReadOffset(buf[0:4]); o0 > size { + return ssz.ErrOffset + } + + // Field (0) 'BlockRoots' + { + buf = tail[o0:] + num, err := ssz.DivideInt2(len(buf), 32, 1024) + if err != nil { + return err + } + b.BlockRoots = make([][]byte, num) + for ii := 0; ii < num; ii++ { + if cap(b.BlockRoots[ii]) == 0 { + b.BlockRoots[ii] = make([]byte, 0, len(buf[ii*32:(ii+1)*32])) + } + b.BlockRoots[ii] = append(b.BlockRoots[ii], buf[ii*32:(ii+1)*32]...) + } + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the BeaconBlocksByRootRequest object +func (b *BeaconBlocksByRootRequest) SizeSSZ() (size int) { + size = 4 + + // Field (0) 'BlockRoots' + size += len(b.BlockRoots) * 32 + + return +} + +// HashTreeRoot ssz hashes the BeaconBlocksByRootRequest object +func (b *BeaconBlocksByRootRequest) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(b) +} + +// HashTreeRootWith ssz hashes the BeaconBlocksByRootRequest object with a hasher +func (b *BeaconBlocksByRootRequest) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'BlockRoots' + { + if len(b.BlockRoots) > 1024 { + err = ssz.ErrListTooBig + return + } + subIndx := hh.Index() + for _, i := range b.BlockRoots { + if len(i) != 32 { + err = ssz.ErrBytesLength + return + } + hh.Append(i) + } + numItems := uint64(len(b.BlockRoots)) + hh.MerkleizeWithMixin(subIndx, numItems, ssz.CalculateLimit(1024, numItems, 32)) + } + + hh.Merkleize(indx) + return +} + +// MarshalSSZ ssz marshals the ErrorResponse object +func (e *ErrorResponse) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(e) +} + +// MarshalSSZTo ssz marshals the ErrorResponse object to a target array +func (e *ErrorResponse) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(4) + + // Offset (0) 'Message' + dst = ssz.WriteOffset(dst, offset) + offset += len(e.Message) + + // Field (0) 'Message' + if len(e.Message) > 256 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, e.Message...) + + return +} + +// UnmarshalSSZ ssz unmarshals the ErrorResponse object +func (e *ErrorResponse) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 4 { + return ssz.ErrSize + } + + tail := buf + var o0 uint64 + + // Offset (0) 'Message' + if o0 = ssz.ReadOffset(buf[0:4]); o0 > size { + return ssz.ErrOffset + } + + // Field (0) 'Message' + { + buf = tail[o0:] + if len(buf) > 256 { + return ssz.ErrBytesLength + } + if cap(e.Message) == 0 { + e.Message = make([]byte, 0, len(buf)) + } + e.Message = append(e.Message, buf...) + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the ErrorResponse object +func (e *ErrorResponse) SizeSSZ() (size int) { + size = 4 + + // Field (0) 'Message' + size += len(e.Message) + + return +} + +// HashTreeRoot ssz hashes the ErrorResponse object +func (e *ErrorResponse) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(e) +} + +// HashTreeRootWith ssz hashes the ErrorResponse object with a hasher +func (e *ErrorResponse) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'Message' + if len(e.Message) > 256 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(e.Message) + + hh.Merkleize(indx) + return +} + +// MarshalSSZ ssz marshals the ENRForkID object +func (e *ENRForkID) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(e) +} + +// MarshalSSZTo ssz marshals the ENRForkID object to a target array +func (e *ENRForkID) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + + // Field (0) 'CurrentForkDigest' + if len(e.CurrentForkDigest) != 4 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, e.CurrentForkDigest...) + + // Field (1) 'NextForkVersion' + if len(e.NextForkVersion) != 4 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, e.NextForkVersion...) + + // Field (2) 'NextForkEpoch' + dst = ssz.MarshalUint64(dst, e.NextForkEpoch) + + return +} + +// UnmarshalSSZ ssz unmarshals the ENRForkID object +func (e *ENRForkID) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size != 16 { + return ssz.ErrSize + } + + // Field (0) 'CurrentForkDigest' + if cap(e.CurrentForkDigest) == 0 { + e.CurrentForkDigest = make([]byte, 0, len(buf[0:4])) + } + e.CurrentForkDigest = append(e.CurrentForkDigest, buf[0:4]...) + + // Field (1) 'NextForkVersion' + if cap(e.NextForkVersion) == 0 { + e.NextForkVersion = make([]byte, 0, len(buf[4:8])) + } + e.NextForkVersion = append(e.NextForkVersion, buf[4:8]...) + + // Field (2) 'NextForkEpoch' + e.NextForkEpoch = ssz.UnmarshallUint64(buf[8:16]) + + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the ENRForkID object +func (e *ENRForkID) SizeSSZ() (size int) { + size = 16 + return +} + +// HashTreeRoot ssz hashes the ENRForkID object +func (e *ENRForkID) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(e) +} + +// HashTreeRootWith ssz hashes the ENRForkID object with a hasher +func (e *ENRForkID) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'CurrentForkDigest' + if len(e.CurrentForkDigest) != 4 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(e.CurrentForkDigest) + + // Field (1) 'NextForkVersion' + if len(e.NextForkVersion) != 4 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(e.NextForkVersion) + + // Field (2) 'NextForkEpoch' + hh.PutUint64(e.NextForkEpoch) + + hh.Merkleize(indx) + return +} + +// MarshalSSZ ssz marshals the MetaData object +func (m *MetaData) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(m) +} + +// MarshalSSZTo ssz marshals the MetaData object to a target array +func (m *MetaData) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + + // Field (0) 'SeqNumber' + dst = ssz.MarshalUint64(dst, m.SeqNumber) + + // Field (1) 'Attnets' + if len(m.Attnets) != 8 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, m.Attnets...) + + return +} + +// UnmarshalSSZ ssz unmarshals the MetaData object +func (m *MetaData) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size != 16 { + return ssz.ErrSize + } + + // Field (0) 'SeqNumber' + m.SeqNumber = ssz.UnmarshallUint64(buf[0:8]) + + // Field (1) 'Attnets' + if cap(m.Attnets) == 0 { + m.Attnets = make([]byte, 0, len(buf[8:16])) + } + m.Attnets = append(m.Attnets, buf[8:16]...) + + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the MetaData object +func (m *MetaData) SizeSSZ() (size int) { + size = 16 + return +} + +// HashTreeRoot ssz hashes the MetaData object +func (m *MetaData) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(m) +} + +// HashTreeRootWith ssz hashes the MetaData object with a hasher +func (m *MetaData) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'SeqNumber' + hh.PutUint64(m.SeqNumber) + + // Field (1) 'Attnets' + if len(m.Attnets) != 8 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(m.Attnets) + + hh.Merkleize(indx) + return +} // MarshalSSZ ssz marshals the BeaconState object func (b *BeaconState) MarshalSSZ() ([]byte, error) { - buf := make([]byte, b.SizeSSZ()) - return b.MarshalSSZTo(buf[:0]) + return ssz.MarshalSSZ(b) } // MarshalSSZTo ssz marshals the BeaconState object to a target array -func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error +func (b *BeaconState) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf offset := int(2687377) // Field (0) 'GenesisTime' dst = ssz.MarshalUint64(dst, b.GenesisTime) // Field (1) 'GenesisValidatorsRoot' - if dst, err = ssz.MarshalFixedBytes(dst, b.GenesisValidatorsRoot, 32); err != nil { - return nil, errMarshalFixedBytes + if len(b.GenesisValidatorsRoot) != 32 { + err = ssz.ErrBytesLength + return } + dst = append(dst, b.GenesisValidatorsRoot...) // Field (2) 'Slot' dst = ssz.MarshalUint64(dst, b.Slot) @@ -46,7 +574,7 @@ func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { b.Fork = new(Fork) } if dst, err = b.Fork.MarshalSSZTo(dst); err != nil { - return nil, err + return } // Field (4) 'LatestBlockHeader' @@ -54,27 +582,33 @@ func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { b.LatestBlockHeader = new(v1alpha1.BeaconBlockHeader) } if dst, err = b.LatestBlockHeader.MarshalSSZTo(dst); err != nil { - return nil, err + return } // Field (5) 'BlockRoots' if len(b.BlockRoots) != 8192 { - return nil, errMarshalVector + err = ssz.ErrVectorLength + return } for ii := 0; ii < 8192; ii++ { - if dst, err = ssz.MarshalFixedBytes(dst, b.BlockRoots[ii], 32); err != nil { - return nil, errMarshalFixedBytes + if len(b.BlockRoots[ii]) != 32 { + err = ssz.ErrBytesLength + return } + dst = append(dst, b.BlockRoots[ii]...) } // Field (6) 'StateRoots' if len(b.StateRoots) != 8192 { - return nil, errMarshalVector + err = ssz.ErrVectorLength + return } for ii := 0; ii < 8192; ii++ { - if dst, err = ssz.MarshalFixedBytes(dst, b.StateRoots[ii], 32); err != nil { - return nil, errMarshalFixedBytes + if len(b.StateRoots[ii]) != 32 { + err = ssz.ErrBytesLength + return } + dst = append(dst, b.StateRoots[ii]...) } // Offset (7) 'HistoricalRoots' @@ -86,7 +620,7 @@ func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { b.Eth1Data = new(v1alpha1.Eth1Data) } if dst, err = b.Eth1Data.MarshalSSZTo(dst); err != nil { - return nil, err + return } // Offset (9) 'Eth1DataVotes' @@ -106,17 +640,21 @@ func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { // Field (13) 'RandaoMixes' if len(b.RandaoMixes) != 65536 { - return nil, errMarshalVector + err = ssz.ErrVectorLength + return } for ii := 0; ii < 65536; ii++ { - if dst, err = ssz.MarshalFixedBytes(dst, b.RandaoMixes[ii], 32); err != nil { - return nil, errMarshalFixedBytes + if len(b.RandaoMixes[ii]) != 32 { + err = ssz.ErrBytesLength + return } + dst = append(dst, b.RandaoMixes[ii]...) } // Field (14) 'Slashings' if len(b.Slashings) != 8192 { - return nil, errMarshalVector + err = ssz.ErrVectorLength + return } for ii := 0; ii < 8192; ii++ { dst = ssz.MarshalUint64(dst, b.Slashings[ii]) @@ -137,16 +675,18 @@ func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { } // Field (17) 'JustificationBits' - if dst, err = ssz.MarshalFixedBytes(dst, b.JustificationBits, 1); err != nil { - return nil, errMarshalFixedBytes + if len(b.JustificationBits) != 1 { + err = ssz.ErrBytesLength + return } + dst = append(dst, b.JustificationBits...) // Field (18) 'PreviousJustifiedCheckpoint' if b.PreviousJustifiedCheckpoint == nil { b.PreviousJustifiedCheckpoint = new(v1alpha1.Checkpoint) } if dst, err = b.PreviousJustifiedCheckpoint.MarshalSSZTo(dst); err != nil { - return nil, err + return } // Field (19) 'CurrentJustifiedCheckpoint' @@ -154,7 +694,7 @@ func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { b.CurrentJustifiedCheckpoint = new(v1alpha1.Checkpoint) } if dst, err = b.CurrentJustifiedCheckpoint.MarshalSSZTo(dst); err != nil { - return nil, err + return } // Field (20) 'FinalizedCheckpoint' @@ -162,42 +702,48 @@ func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { b.FinalizedCheckpoint = new(v1alpha1.Checkpoint) } if dst, err = b.FinalizedCheckpoint.MarshalSSZTo(dst); err != nil { - return nil, err + return } // Field (7) 'HistoricalRoots' if len(b.HistoricalRoots) > 16777216 { - return nil, errMarshalList + err = ssz.ErrListTooBig + return } for ii := 0; ii < len(b.HistoricalRoots); ii++ { - if dst, err = ssz.MarshalFixedBytes(dst, b.HistoricalRoots[ii], 32); err != nil { - return nil, errMarshalFixedBytes + if len(b.HistoricalRoots[ii]) != 32 { + err = ssz.ErrBytesLength + return } + dst = append(dst, b.HistoricalRoots[ii]...) } // Field (9) 'Eth1DataVotes' if len(b.Eth1DataVotes) > 1024 { - return nil, errMarshalList + err = ssz.ErrListTooBig + return } for ii := 0; ii < len(b.Eth1DataVotes); ii++ { if dst, err = b.Eth1DataVotes[ii].MarshalSSZTo(dst); err != nil { - return nil, err + return } } // Field (11) 'Validators' if len(b.Validators) > 1099511627776 { - return nil, errMarshalList + err = ssz.ErrListTooBig + return } for ii := 0; ii < len(b.Validators); ii++ { if dst, err = b.Validators[ii].MarshalSSZTo(dst); err != nil { - return nil, err + return } } // Field (12) 'Balances' if len(b.Balances) > 1099511627776 { - return nil, errMarshalList + err = ssz.ErrListTooBig + return } for ii := 0; ii < len(b.Balances); ii++ { dst = ssz.MarshalUint64(dst, b.Balances[ii]) @@ -205,7 +751,8 @@ func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { // Field (15) 'PreviousEpochAttestations' if len(b.PreviousEpochAttestations) > 4096 { - return nil, errMarshalList + err = ssz.ErrListTooBig + return } { offset = 4 * len(b.PreviousEpochAttestations) @@ -216,13 +763,14 @@ func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { } for ii := 0; ii < len(b.PreviousEpochAttestations); ii++ { if dst, err = b.PreviousEpochAttestations[ii].MarshalSSZTo(dst); err != nil { - return nil, err + return } } // Field (16) 'CurrentEpochAttestations' if len(b.CurrentEpochAttestations) > 4096 { - return nil, errMarshalList + err = ssz.ErrListTooBig + return } { offset = 4 * len(b.CurrentEpochAttestations) @@ -233,11 +781,11 @@ func (b *BeaconState) MarshalSSZTo(dst []byte) ([]byte, error) { } for ii := 0; ii < len(b.CurrentEpochAttestations); ii++ { if dst, err = b.CurrentEpochAttestations[ii].MarshalSSZTo(dst); err != nil { - return nil, err + return } } - return dst, err + return } // UnmarshalSSZ ssz unmarshals the BeaconState object @@ -245,7 +793,7 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) if size < 2687377 { - return errSize + return ssz.ErrSize } tail := buf @@ -255,6 +803,9 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { b.GenesisTime = ssz.UnmarshallUint64(buf[0:8]) // Field (1) 'GenesisValidatorsRoot' + if cap(b.GenesisValidatorsRoot) == 0 { + b.GenesisValidatorsRoot = make([]byte, 0, len(buf[8:40])) + } b.GenesisValidatorsRoot = append(b.GenesisValidatorsRoot, buf[8:40]...) // Field (2) 'Slot' @@ -279,18 +830,24 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { // Field (5) 'BlockRoots' b.BlockRoots = make([][]byte, 8192) for ii := 0; ii < 8192; ii++ { + if cap(b.BlockRoots[ii]) == 0 { + b.BlockRoots[ii] = make([]byte, 0, len(buf[176:262320][ii*32:(ii+1)*32])) + } b.BlockRoots[ii] = append(b.BlockRoots[ii], buf[176:262320][ii*32:(ii+1)*32]...) } // Field (6) 'StateRoots' b.StateRoots = make([][]byte, 8192) for ii := 0; ii < 8192; ii++ { + if cap(b.StateRoots[ii]) == 0 { + b.StateRoots[ii] = make([]byte, 0, len(buf[262320:524464][ii*32:(ii+1)*32])) + } b.StateRoots[ii] = append(b.StateRoots[ii], buf[262320:524464][ii*32:(ii+1)*32]...) } // Offset (7) 'HistoricalRoots' if o7 = ssz.ReadOffset(buf[524464:524468]); o7 > size { - return errOffset + return ssz.ErrOffset } // Field (8) 'Eth1Data' @@ -303,7 +860,7 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { // Offset (9) 'Eth1DataVotes' if o9 = ssz.ReadOffset(buf[524540:524544]); o9 > size || o7 > o9 { - return errOffset + return ssz.ErrOffset } // Field (10) 'Eth1DepositIndex' @@ -311,17 +868,20 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { // Offset (11) 'Validators' if o11 = ssz.ReadOffset(buf[524552:524556]); o11 > size || o9 > o11 { - return errOffset + return ssz.ErrOffset } // Offset (12) 'Balances' if o12 = ssz.ReadOffset(buf[524556:524560]); o12 > size || o11 > o12 { - return errOffset + return ssz.ErrOffset } // Field (13) 'RandaoMixes' b.RandaoMixes = make([][]byte, 65536) for ii := 0; ii < 65536; ii++ { + if cap(b.RandaoMixes[ii]) == 0 { + b.RandaoMixes[ii] = make([]byte, 0, len(buf[524560:2621712][ii*32:(ii+1)*32])) + } b.RandaoMixes[ii] = append(b.RandaoMixes[ii], buf[524560:2621712][ii*32:(ii+1)*32]...) } @@ -333,15 +893,18 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { // Offset (15) 'PreviousEpochAttestations' if o15 = ssz.ReadOffset(buf[2687248:2687252]); o15 > size || o12 > o15 { - return errOffset + return ssz.ErrOffset } // Offset (16) 'CurrentEpochAttestations' if o16 = ssz.ReadOffset(buf[2687252:2687256]); o16 > size || o15 > o16 { - return errOffset + return ssz.ErrOffset } // Field (17) 'JustificationBits' + if cap(b.JustificationBits) == 0 { + b.JustificationBits = make([]byte, 0, len(buf[2687256:2687257])) + } b.JustificationBits = append(b.JustificationBits, buf[2687256:2687257]...) // Field (18) 'PreviousJustifiedCheckpoint' @@ -371,15 +934,15 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { // Field (7) 'HistoricalRoots' { buf = tail[o7:o9] - num, ok := ssz.DivideInt(len(buf), 32) - if !ok { - return errDivideInt - } - if num > 16777216 { - return errListTooBig + num, err := ssz.DivideInt2(len(buf), 32, 16777216) + if err != nil { + return err } b.HistoricalRoots = make([][]byte, num) for ii := 0; ii < num; ii++ { + if cap(b.HistoricalRoots[ii]) == 0 { + b.HistoricalRoots[ii] = make([]byte, 0, len(buf[ii*32:(ii+1)*32])) + } b.HistoricalRoots[ii] = append(b.HistoricalRoots[ii], buf[ii*32:(ii+1)*32]...) } } @@ -387,12 +950,9 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { // Field (9) 'Eth1DataVotes' { buf = tail[o9:o11] - num, ok := ssz.DivideInt(len(buf), 72) - if !ok { - return errDivideInt - } - if num > 1024 { - return errListTooBig + num, err := ssz.DivideInt2(len(buf), 72, 1024) + if err != nil { + return err } b.Eth1DataVotes = make([]*v1alpha1.Eth1Data, num) for ii := 0; ii < num; ii++ { @@ -408,12 +968,9 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { // Field (11) 'Validators' { buf = tail[o11:o12] - num, ok := ssz.DivideInt(len(buf), 121) - if !ok { - return errDivideInt - } - if num > 1099511627776 { - return errListTooBig + num, err := ssz.DivideInt2(len(buf), 121, 1099511627776) + if err != nil { + return err } b.Validators = make([]*v1alpha1.Validator, num) for ii := 0; ii < num; ii++ { @@ -429,12 +986,9 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { // Field (12) 'Balances' { buf = tail[o12:o15] - num, ok := ssz.DivideInt(len(buf), 8) - if !ok { - return errDivideInt - } - if num > 1099511627776 { - return errListTooBig + num, err := ssz.DivideInt2(len(buf), 8, 1099511627776) + if err != nil { + return err } b.Balances = ssz.ExtendUint64(b.Balances, num) for ii := 0; ii < num; ii++ { @@ -519,30 +1073,260 @@ func (b *BeaconState) SizeSSZ() (size int) { return } +// HashTreeRoot ssz hashes the BeaconState object +func (b *BeaconState) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(b) +} + +// HashTreeRootWith ssz hashes the BeaconState object with a hasher +func (b *BeaconState) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'GenesisTime' + hh.PutUint64(b.GenesisTime) + + // Field (1) 'GenesisValidatorsRoot' + if len(b.GenesisValidatorsRoot) != 32 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(b.GenesisValidatorsRoot) + + // Field (2) 'Slot' + hh.PutUint64(b.Slot) + + // Field (3) 'Fork' + if err = b.Fork.HashTreeRootWith(hh); err != nil { + return + } + + // Field (4) 'LatestBlockHeader' + if err = b.LatestBlockHeader.HashTreeRootWith(hh); err != nil { + return + } + + // Field (5) 'BlockRoots' + { + if len(b.BlockRoots) != 8192 { + err = ssz.ErrVectorLength + return + } + subIndx := hh.Index() + for _, i := range b.BlockRoots { + if len(i) != 32 { + err = ssz.ErrBytesLength + return + } + hh.Append(i) + } + hh.Merkleize(subIndx) + } + + // Field (6) 'StateRoots' + { + if len(b.StateRoots) != 8192 { + err = ssz.ErrVectorLength + return + } + subIndx := hh.Index() + for _, i := range b.StateRoots { + if len(i) != 32 { + err = ssz.ErrBytesLength + return + } + hh.Append(i) + } + hh.Merkleize(subIndx) + } + + // Field (7) 'HistoricalRoots' + { + if len(b.HistoricalRoots) > 16777216 { + err = ssz.ErrListTooBig + return + } + subIndx := hh.Index() + for _, i := range b.HistoricalRoots { + if len(i) != 32 { + err = ssz.ErrBytesLength + return + } + hh.Append(i) + } + numItems := uint64(len(b.HistoricalRoots)) + hh.MerkleizeWithMixin(subIndx, numItems, ssz.CalculateLimit(16777216, numItems, 32)) + } + + // Field (8) 'Eth1Data' + if err = b.Eth1Data.HashTreeRootWith(hh); err != nil { + return + } + + // Field (9) 'Eth1DataVotes' + { + subIndx := hh.Index() + num := uint64(len(b.Eth1DataVotes)) + if num > 1024 { + err = ssz.ErrIncorrectListSize + return + } + for i := uint64(0); i < num; i++ { + if err = b.Eth1DataVotes[i].HashTreeRootWith(hh); err != nil { + return + } + } + hh.MerkleizeWithMixin(subIndx, num, 1024) + } + + // Field (10) 'Eth1DepositIndex' + hh.PutUint64(b.Eth1DepositIndex) + + // Field (11) 'Validators' + { + subIndx := hh.Index() + num := uint64(len(b.Validators)) + if num > 1099511627776 { + err = ssz.ErrIncorrectListSize + return + } + for i := uint64(0); i < num; i++ { + if err = b.Validators[i].HashTreeRootWith(hh); err != nil { + return + } + } + hh.MerkleizeWithMixin(subIndx, num, 1099511627776) + } + + // Field (12) 'Balances' + { + if len(b.Balances) > 1099511627776 { + err = ssz.ErrListTooBig + return + } + subIndx := hh.Index() + for _, i := range b.Balances { + hh.AppendUint64(i) + } + hh.FillUpTo32() + numItems := uint64(len(b.Balances)) + hh.MerkleizeWithMixin(subIndx, numItems, ssz.CalculateLimit(1099511627776, numItems, 8)) + } + + // Field (13) 'RandaoMixes' + { + if len(b.RandaoMixes) != 65536 { + err = ssz.ErrVectorLength + return + } + subIndx := hh.Index() + for _, i := range b.RandaoMixes { + if len(i) != 32 { + err = ssz.ErrBytesLength + return + } + hh.Append(i) + } + hh.Merkleize(subIndx) + } + + // Field (14) 'Slashings' + { + if len(b.Slashings) != 8192 { + err = ssz.ErrVectorLength + return + } + subIndx := hh.Index() + for _, i := range b.Slashings { + hh.AppendUint64(i) + } + hh.Merkleize(subIndx) + } + + // Field (15) 'PreviousEpochAttestations' + { + subIndx := hh.Index() + num := uint64(len(b.PreviousEpochAttestations)) + if num > 4096 { + err = ssz.ErrIncorrectListSize + return + } + for i := uint64(0); i < num; i++ { + if err = b.PreviousEpochAttestations[i].HashTreeRootWith(hh); err != nil { + return + } + } + hh.MerkleizeWithMixin(subIndx, num, 4096) + } + + // Field (16) 'CurrentEpochAttestations' + { + subIndx := hh.Index() + num := uint64(len(b.CurrentEpochAttestations)) + if num > 4096 { + err = ssz.ErrIncorrectListSize + return + } + for i := uint64(0); i < num; i++ { + if err = b.CurrentEpochAttestations[i].HashTreeRootWith(hh); err != nil { + return + } + } + hh.MerkleizeWithMixin(subIndx, num, 4096) + } + + // Field (17) 'JustificationBits' + if len(b.JustificationBits) != 1 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(b.JustificationBits) + + // Field (18) 'PreviousJustifiedCheckpoint' + if err = b.PreviousJustifiedCheckpoint.HashTreeRootWith(hh); err != nil { + return + } + + // Field (19) 'CurrentJustifiedCheckpoint' + if err = b.CurrentJustifiedCheckpoint.HashTreeRootWith(hh); err != nil { + return + } + + // Field (20) 'FinalizedCheckpoint' + if err = b.FinalizedCheckpoint.HashTreeRootWith(hh); err != nil { + return + } + + hh.Merkleize(indx) + return +} + // MarshalSSZ ssz marshals the Fork object func (f *Fork) MarshalSSZ() ([]byte, error) { - buf := make([]byte, f.SizeSSZ()) - return f.MarshalSSZTo(buf[:0]) + return ssz.MarshalSSZ(f) } // MarshalSSZTo ssz marshals the Fork object to a target array -func (f *Fork) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error +func (f *Fork) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf // Field (0) 'PreviousVersion' - if dst, err = ssz.MarshalFixedBytes(dst, f.PreviousVersion, 4); err != nil { - return nil, errMarshalFixedBytes + if len(f.PreviousVersion) != 4 { + err = ssz.ErrBytesLength + return } + dst = append(dst, f.PreviousVersion...) // Field (1) 'CurrentVersion' - if dst, err = ssz.MarshalFixedBytes(dst, f.CurrentVersion, 4); err != nil { - return nil, errMarshalFixedBytes + if len(f.CurrentVersion) != 4 { + err = ssz.ErrBytesLength + return } + dst = append(dst, f.CurrentVersion...) // Field (2) 'Epoch' dst = ssz.MarshalUint64(dst, f.Epoch) - return dst, err + return } // UnmarshalSSZ ssz unmarshals the Fork object @@ -550,13 +1334,19 @@ func (f *Fork) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) if size != 16 { - return errSize + return ssz.ErrSize } // Field (0) 'PreviousVersion' + if cap(f.PreviousVersion) == 0 { + f.PreviousVersion = make([]byte, 0, len(buf[0:4])) + } f.PreviousVersion = append(f.PreviousVersion, buf[0:4]...) // Field (1) 'CurrentVersion' + if cap(f.CurrentVersion) == 0 { + f.CurrentVersion = make([]byte, 0, len(buf[4:8])) + } f.CurrentVersion = append(f.CurrentVersion, buf[4:8]...) // Field (2) 'Epoch' @@ -571,15 +1361,44 @@ func (f *Fork) SizeSSZ() (size int) { return } +// HashTreeRoot ssz hashes the Fork object +func (f *Fork) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(f) +} + +// HashTreeRootWith ssz hashes the Fork object with a hasher +func (f *Fork) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'PreviousVersion' + if len(f.PreviousVersion) != 4 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(f.PreviousVersion) + + // Field (1) 'CurrentVersion' + if len(f.CurrentVersion) != 4 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(f.CurrentVersion) + + // Field (2) 'Epoch' + hh.PutUint64(f.Epoch) + + hh.Merkleize(indx) + return +} + // MarshalSSZ ssz marshals the PendingAttestation object func (p *PendingAttestation) MarshalSSZ() ([]byte, error) { - buf := make([]byte, p.SizeSSZ()) - return p.MarshalSSZTo(buf[:0]) + return ssz.MarshalSSZ(p) } // MarshalSSZTo ssz marshals the PendingAttestation object to a target array -func (p *PendingAttestation) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error +func (p *PendingAttestation) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf offset := int(148) // Offset (0) 'AggregationBits' @@ -591,7 +1410,7 @@ func (p *PendingAttestation) MarshalSSZTo(dst []byte) ([]byte, error) { p.Data = new(v1alpha1.AttestationData) } if dst, err = p.Data.MarshalSSZTo(dst); err != nil { - return nil, err + return } // Field (2) 'InclusionDelay' @@ -601,9 +1420,13 @@ func (p *PendingAttestation) MarshalSSZTo(dst []byte) ([]byte, error) { dst = ssz.MarshalUint64(dst, p.ProposerIndex) // Field (0) 'AggregationBits' + if len(p.AggregationBits) > 2048 { + err = ssz.ErrBytesLength + return + } dst = append(dst, p.AggregationBits...) - return dst, err + return } // UnmarshalSSZ ssz unmarshals the PendingAttestation object @@ -611,7 +1434,7 @@ func (p *PendingAttestation) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) if size < 148 { - return errSize + return ssz.ErrSize } tail := buf @@ -619,7 +1442,7 @@ func (p *PendingAttestation) UnmarshalSSZ(buf []byte) error { // Offset (0) 'AggregationBits' if o0 = ssz.ReadOffset(buf[0:4]); o0 > size { - return errOffset + return ssz.ErrOffset } // Field (1) 'Data' @@ -639,6 +1462,12 @@ func (p *PendingAttestation) UnmarshalSSZ(buf []byte) error { // Field (0) 'AggregationBits' { buf = tail[o0:] + if err = ssz.ValidateBitlist(buf, 2048); err != nil { + return err + } + if cap(p.AggregationBits) == 0 { + p.AggregationBits = make([]byte, 0, len(buf)) + } p.AggregationBits = append(p.AggregationBits, buf...) } return err @@ -654,37 +1483,69 @@ func (p *PendingAttestation) SizeSSZ() (size int) { return } +// HashTreeRoot ssz hashes the PendingAttestation object +func (p *PendingAttestation) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(p) +} + +// HashTreeRootWith ssz hashes the PendingAttestation object with a hasher +func (p *PendingAttestation) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'AggregationBits' + hh.PutBitlist(p.AggregationBits, 2048) + + // Field (1) 'Data' + if err = p.Data.HashTreeRootWith(hh); err != nil { + return + } + + // Field (2) 'InclusionDelay' + hh.PutUint64(p.InclusionDelay) + + // Field (3) 'ProposerIndex' + hh.PutUint64(p.ProposerIndex) + + hh.Merkleize(indx) + return +} + // MarshalSSZ ssz marshals the HistoricalBatch object func (h *HistoricalBatch) MarshalSSZ() ([]byte, error) { - buf := make([]byte, h.SizeSSZ()) - return h.MarshalSSZTo(buf[:0]) + return ssz.MarshalSSZ(h) } // MarshalSSZTo ssz marshals the HistoricalBatch object to a target array -func (h *HistoricalBatch) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error +func (h *HistoricalBatch) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf // Field (0) 'BlockRoots' if len(h.BlockRoots) != 8192 { - return nil, errMarshalVector + err = ssz.ErrVectorLength + return } for ii := 0; ii < 8192; ii++ { - if dst, err = ssz.MarshalFixedBytes(dst, h.BlockRoots[ii], 32); err != nil { - return nil, errMarshalFixedBytes + if len(h.BlockRoots[ii]) != 32 { + err = ssz.ErrBytesLength + return } + dst = append(dst, h.BlockRoots[ii]...) } // Field (1) 'StateRoots' if len(h.StateRoots) != 8192 { - return nil, errMarshalVector + err = ssz.ErrVectorLength + return } for ii := 0; ii < 8192; ii++ { - if dst, err = ssz.MarshalFixedBytes(dst, h.StateRoots[ii], 32); err != nil { - return nil, errMarshalFixedBytes + if len(h.StateRoots[ii]) != 32 { + err = ssz.ErrBytesLength + return } + dst = append(dst, h.StateRoots[ii]...) } - return dst, err + return } // UnmarshalSSZ ssz unmarshals the HistoricalBatch object @@ -692,18 +1553,24 @@ func (h *HistoricalBatch) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) if size != 524288 { - return errSize + return ssz.ErrSize } // Field (0) 'BlockRoots' h.BlockRoots = make([][]byte, 8192) for ii := 0; ii < 8192; ii++ { + if cap(h.BlockRoots[ii]) == 0 { + h.BlockRoots[ii] = make([]byte, 0, len(buf[0:262144][ii*32:(ii+1)*32])) + } h.BlockRoots[ii] = append(h.BlockRoots[ii], buf[0:262144][ii*32:(ii+1)*32]...) } // Field (1) 'StateRoots' h.StateRoots = make([][]byte, 8192) for ii := 0; ii < 8192; ii++ { + if cap(h.StateRoots[ii]) == 0 { + h.StateRoots[ii] = make([]byte, 0, len(buf[262144:524288][ii*32:(ii+1)*32])) + } h.StateRoots[ii] = append(h.StateRoots[ii], buf[262144:524288][ii*32:(ii+1)*32]...) } @@ -716,342 +1583,213 @@ func (h *HistoricalBatch) SizeSSZ() (size int) { return } -// MarshalSSZ ssz marshals the Status object -func (s *Status) MarshalSSZ() ([]byte, error) { - buf := make([]byte, s.SizeSSZ()) - return s.MarshalSSZTo(buf[:0]) +// HashTreeRoot ssz hashes the HistoricalBatch object +func (h *HistoricalBatch) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(h) } -// MarshalSSZTo ssz marshals the Status object to a target array -func (s *Status) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error - - // Field (0) 'ForkDigest' - if dst, err = ssz.MarshalFixedBytes(dst, s.ForkDigest, 4); err != nil { - return nil, errMarshalFixedBytes - } - - // Field (1) 'FinalizedRoot' - if dst, err = ssz.MarshalFixedBytes(dst, s.FinalizedRoot, 32); err != nil { - return nil, errMarshalFixedBytes - } - - // Field (2) 'FinalizedEpoch' - dst = ssz.MarshalUint64(dst, s.FinalizedEpoch) - - // Field (3) 'HeadRoot' - if dst, err = ssz.MarshalFixedBytes(dst, s.HeadRoot, 32); err != nil { - return nil, errMarshalFixedBytes - } - - // Field (4) 'HeadSlot' - dst = ssz.MarshalUint64(dst, s.HeadSlot) - - return dst, err -} - -// UnmarshalSSZ ssz unmarshals the Status object -func (s *Status) UnmarshalSSZ(buf []byte) error { - var err error - size := uint64(len(buf)) - if size != 84 { - return errSize - } - - // Field (0) 'ForkDigest' - s.ForkDigest = append(s.ForkDigest, buf[0:4]...) - - // Field (1) 'FinalizedRoot' - s.FinalizedRoot = append(s.FinalizedRoot, buf[4:36]...) - - // Field (2) 'FinalizedEpoch' - s.FinalizedEpoch = ssz.UnmarshallUint64(buf[36:44]) - - // Field (3) 'HeadRoot' - s.HeadRoot = append(s.HeadRoot, buf[44:76]...) - - // Field (4) 'HeadSlot' - s.HeadSlot = ssz.UnmarshallUint64(buf[76:84]) - - return err -} - -// SizeSSZ returns the ssz encoded size in bytes for the Status object -func (s *Status) SizeSSZ() (size int) { - size = 84 - return -} - -// MarshalSSZ ssz marshals the BeaconBlocksByRangeRequest object -func (b *BeaconBlocksByRangeRequest) MarshalSSZ() ([]byte, error) { - buf := make([]byte, b.SizeSSZ()) - return b.MarshalSSZTo(buf[:0]) -} - -// MarshalSSZTo ssz marshals the BeaconBlocksByRangeRequest object to a target array -func (b *BeaconBlocksByRangeRequest) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error - - // Field (0) 'StartSlot' - dst = ssz.MarshalUint64(dst, b.StartSlot) - - // Field (1) 'Count' - dst = ssz.MarshalUint64(dst, b.Count) - - // Field (2) 'Step' - dst = ssz.MarshalUint64(dst, b.Step) - - return dst, err -} - -// UnmarshalSSZ ssz unmarshals the BeaconBlocksByRangeRequest object -func (b *BeaconBlocksByRangeRequest) UnmarshalSSZ(buf []byte) error { - var err error - size := uint64(len(buf)) - if size != 24 { - return errSize - } - - // Field (0) 'StartSlot' - b.StartSlot = ssz.UnmarshallUint64(buf[0:8]) - - // Field (1) 'Count' - b.Count = ssz.UnmarshallUint64(buf[8:16]) - - // Field (2) 'Step' - b.Step = ssz.UnmarshallUint64(buf[16:24]) - - return err -} - -// SizeSSZ returns the ssz encoded size in bytes for the BeaconBlocksByRangeRequest object -func (b *BeaconBlocksByRangeRequest) SizeSSZ() (size int) { - size = 24 - return -} - -// MarshalSSZ ssz marshals the BeaconBlocksByRootRequest object -func (b *BeaconBlocksByRootRequest) MarshalSSZ() ([]byte, error) { - buf := make([]byte, b.SizeSSZ()) - return b.MarshalSSZTo(buf[:0]) -} - -// MarshalSSZTo ssz marshals the BeaconBlocksByRootRequest object to a target array -func (b *BeaconBlocksByRootRequest) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error - offset := int(4) - - // Offset (0) 'BlockRoots' - dst = ssz.WriteOffset(dst, offset) - offset += len(b.BlockRoots) * 32 - - // Field (0) 'BlockRoots' - if len(b.BlockRoots) > 1024 { - return nil, errMarshalList - } - for ii := 0; ii < len(b.BlockRoots); ii++ { - if dst, err = ssz.MarshalFixedBytes(dst, b.BlockRoots[ii], 32); err != nil { - return nil, errMarshalFixedBytes - } - } - - return dst, err -} - -// UnmarshalSSZ ssz unmarshals the BeaconBlocksByRootRequest object -func (b *BeaconBlocksByRootRequest) UnmarshalSSZ(buf []byte) error { - var err error - size := uint64(len(buf)) - if size < 4 { - return errSize - } - - tail := buf - var o0 uint64 - - // Offset (0) 'BlockRoots' - if o0 = ssz.ReadOffset(buf[0:4]); o0 > size { - return errOffset - } +// HashTreeRootWith ssz hashes the HistoricalBatch object with a hasher +func (h *HistoricalBatch) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() // Field (0) 'BlockRoots' { - buf = tail[o0:] - num, ok := ssz.DivideInt(len(buf), 32) - if !ok { - return errDivideInt + if len(h.BlockRoots) != 8192 { + err = ssz.ErrVectorLength + return } - if num > 1024 { - return errListTooBig + subIndx := hh.Index() + for _, i := range h.BlockRoots { + if len(i) != 32 { + err = ssz.ErrBytesLength + return + } + hh.Append(i) } - b.BlockRoots = make([][]byte, num) - for ii := 0; ii < num; ii++ { - b.BlockRoots[ii] = append(b.BlockRoots[ii], buf[ii*32:(ii+1)*32]...) - } - } - return err -} - -// SizeSSZ returns the ssz encoded size in bytes for the BeaconBlocksByRootRequest object -func (b *BeaconBlocksByRootRequest) SizeSSZ() (size int) { - size = 4 - - // Field (0) 'BlockRoots' - size += len(b.BlockRoots) * 32 - - return -} - -// MarshalSSZ ssz marshals the ErrorResponse object -func (e *ErrorResponse) MarshalSSZ() ([]byte, error) { - buf := make([]byte, e.SizeSSZ()) - return e.MarshalSSZTo(buf[:0]) -} - -// MarshalSSZTo ssz marshals the ErrorResponse object to a target array -func (e *ErrorResponse) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error - offset := int(4) - - // Offset (0) 'Message' - dst = ssz.WriteOffset(dst, offset) - offset += len(e.Message) - - // Field (0) 'Message' - if len(e.Message) > 256 { - return nil, errMarshalDynamicBytes - } - dst = append(dst, e.Message...) - - return dst, err -} - -// UnmarshalSSZ ssz unmarshals the ErrorResponse object -func (e *ErrorResponse) UnmarshalSSZ(buf []byte) error { - var err error - size := uint64(len(buf)) - if size < 4 { - return errSize + hh.Merkleize(subIndx) } - tail := buf - var o0 uint64 - - // Offset (0) 'Message' - if o0 = ssz.ReadOffset(buf[0:4]); o0 > size { - return errOffset - } - - // Field (0) 'Message' + // Field (1) 'StateRoots' { - buf = tail[o0:] - e.Message = append(e.Message, buf...) + if len(h.StateRoots) != 8192 { + err = ssz.ErrVectorLength + return + } + subIndx := hh.Index() + for _, i := range h.StateRoots { + if len(i) != 32 { + err = ssz.ErrBytesLength + return + } + hh.Append(i) + } + hh.Merkleize(subIndx) } - return err + + hh.Merkleize(indx) + return } -// SizeSSZ returns the ssz encoded size in bytes for the ErrorResponse object -func (e *ErrorResponse) SizeSSZ() (size int) { - size = 4 +// MarshalSSZ ssz marshals the SigningData object +func (s *SigningData) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(s) +} - // Field (0) 'Message' - size += len(e.Message) +// MarshalSSZTo ssz marshals the SigningData object to a target array +func (s *SigningData) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + + // Field (0) 'ObjectRoot' + if len(s.ObjectRoot) != 32 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, s.ObjectRoot...) + + // Field (1) 'Domain' + if len(s.Domain) != 32 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, s.Domain...) return } -// MarshalSSZ ssz marshals the ENRForkID object -func (e *ENRForkID) MarshalSSZ() ([]byte, error) { - buf := make([]byte, e.SizeSSZ()) - return e.MarshalSSZTo(buf[:0]) -} - -// MarshalSSZTo ssz marshals the ENRForkID object to a target array -func (e *ENRForkID) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error - - // Field (0) 'CurrentForkDigest' - if dst, err = ssz.MarshalFixedBytes(dst, e.CurrentForkDigest, 4); err != nil { - return nil, errMarshalFixedBytes - } - - // Field (1) 'NextForkVersion' - if dst, err = ssz.MarshalFixedBytes(dst, e.NextForkVersion, 4); err != nil { - return nil, errMarshalFixedBytes - } - - // Field (2) 'NextForkEpoch' - dst = ssz.MarshalUint64(dst, e.NextForkEpoch) - - return dst, err -} - -// UnmarshalSSZ ssz unmarshals the ENRForkID object -func (e *ENRForkID) UnmarshalSSZ(buf []byte) error { +// UnmarshalSSZ ssz unmarshals the SigningData object +func (s *SigningData) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) - if size != 16 { - return errSize + if size != 64 { + return ssz.ErrSize } - // Field (0) 'CurrentForkDigest' - e.CurrentForkDigest = append(e.CurrentForkDigest, buf[0:4]...) + // Field (0) 'ObjectRoot' + if cap(s.ObjectRoot) == 0 { + s.ObjectRoot = make([]byte, 0, len(buf[0:32])) + } + s.ObjectRoot = append(s.ObjectRoot, buf[0:32]...) - // Field (1) 'NextForkVersion' - e.NextForkVersion = append(e.NextForkVersion, buf[4:8]...) - - // Field (2) 'NextForkEpoch' - e.NextForkEpoch = ssz.UnmarshallUint64(buf[8:16]) + // Field (1) 'Domain' + if cap(s.Domain) == 0 { + s.Domain = make([]byte, 0, len(buf[32:64])) + } + s.Domain = append(s.Domain, buf[32:64]...) return err } -// SizeSSZ returns the ssz encoded size in bytes for the ENRForkID object -func (e *ENRForkID) SizeSSZ() (size int) { - size = 16 +// SizeSSZ returns the ssz encoded size in bytes for the SigningData object +func (s *SigningData) SizeSSZ() (size int) { + size = 64 return } -// MarshalSSZ ssz marshals the MetaData object -func (m *MetaData) MarshalSSZ() ([]byte, error) { - buf := make([]byte, m.SizeSSZ()) - return m.MarshalSSZTo(buf[:0]) +// HashTreeRoot ssz hashes the SigningData object +func (s *SigningData) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(s) } -// MarshalSSZTo ssz marshals the MetaData object to a target array -func (m *MetaData) MarshalSSZTo(dst []byte) ([]byte, error) { - var err error +// HashTreeRootWith ssz hashes the SigningData object with a hasher +func (s *SigningData) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() - // Field (0) 'SeqNumber' - dst = ssz.MarshalUint64(dst, m.SeqNumber) - - // Field (1) 'Attnets' - if dst, err = ssz.MarshalFixedBytes(dst, m.Attnets, 8); err != nil { - return nil, errMarshalFixedBytes + // Field (0) 'ObjectRoot' + if len(s.ObjectRoot) != 32 { + err = ssz.ErrBytesLength + return } + hh.PutBytes(s.ObjectRoot) - return dst, err + // Field (1) 'Domain' + if len(s.Domain) != 32 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(s.Domain) + + hh.Merkleize(indx) + return } -// UnmarshalSSZ ssz unmarshals the MetaData object -func (m *MetaData) UnmarshalSSZ(buf []byte) error { +// MarshalSSZ ssz marshals the ForkData object +func (f *ForkData) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(f) +} + +// MarshalSSZTo ssz marshals the ForkData object to a target array +func (f *ForkData) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + + // Field (0) 'CurrentVersion' + if len(f.CurrentVersion) != 4 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, f.CurrentVersion...) + + // Field (1) 'GenesisValidatorsRoot' + if len(f.GenesisValidatorsRoot) != 32 { + err = ssz.ErrBytesLength + return + } + dst = append(dst, f.GenesisValidatorsRoot...) + + return +} + +// UnmarshalSSZ ssz unmarshals the ForkData object +func (f *ForkData) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) - if size != 16 { - return errSize + if size != 36 { + return ssz.ErrSize } - // Field (0) 'SeqNumber' - m.SeqNumber = ssz.UnmarshallUint64(buf[0:8]) + // Field (0) 'CurrentVersion' + if cap(f.CurrentVersion) == 0 { + f.CurrentVersion = make([]byte, 0, len(buf[0:4])) + } + f.CurrentVersion = append(f.CurrentVersion, buf[0:4]...) - // Field (1) 'Attnets' - m.Attnets = append(m.Attnets, buf[8:16]...) + // Field (1) 'GenesisValidatorsRoot' + if cap(f.GenesisValidatorsRoot) == 0 { + f.GenesisValidatorsRoot = make([]byte, 0, len(buf[4:36])) + } + f.GenesisValidatorsRoot = append(f.GenesisValidatorsRoot, buf[4:36]...) return err } -// SizeSSZ returns the ssz encoded size in bytes for the MetaData object -func (m *MetaData) SizeSSZ() (size int) { - size = 16 +// SizeSSZ returns the ssz encoded size in bytes for the ForkData object +func (f *ForkData) SizeSSZ() (size int) { + size = 36 + return +} + +// HashTreeRoot ssz hashes the ForkData object +func (f *ForkData) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(f) +} + +// HashTreeRootWith ssz hashes the ForkData object with a hasher +func (f *ForkData) HashTreeRootWith(hh *ssz.Hasher) (err error) { + indx := hh.Index() + + // Field (0) 'CurrentVersion' + if len(f.CurrentVersion) != 4 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(f.CurrentVersion) + + // Field (1) 'GenesisValidatorsRoot' + if len(f.GenesisValidatorsRoot) != 32 { + err = ssz.ErrBytesLength + return + } + hh.PutBytes(f.GenesisValidatorsRoot) + + hh.Merkleize(indx) return } diff --git a/proto/testing/ssz_regression_test.go b/proto/testing/ssz_regression_test.go index 2da97c90c3..beea6dedb2 100644 --- a/proto/testing/ssz_regression_test.go +++ b/proto/testing/ssz_regression_test.go @@ -68,7 +68,7 @@ func TestBlockHeaderSigningRoot(t *testing.T) { for i, tt := range tests { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - root1, err := ssz.HashTreeRoot(tt.header1) + root1, err := tt.header1.HashTreeRoot() if err != nil { t.Error(err) } diff --git a/proto/testing/ssz_static_test.go b/proto/testing/ssz_static_test.go index ac16a16cf6..0609b1b3e3 100644 --- a/proto/testing/ssz_static_test.go +++ b/proto/testing/ssz_static_test.go @@ -85,7 +85,14 @@ func runSSZStaticTests(t *testing.T, config string) { if rootsYaml.SigningRoot == "" { return } - signingRoot, err := ssz.HashTreeRoot(object) + + var signingRoot [32]byte + if v, ok := object.(fssz.HashRoot); ok { + signingRoot, err = v.HashTreeRoot() + } else { + t.Fatal("object does not meet fssz.HashRoot") + } + if err != nil { t.Fatal(err) } diff --git a/shared/attestationutil/attestation_utils_test.go b/shared/attestationutil/attestation_utils_test.go index 01b7d6965e..482ee48ce5 100644 --- a/shared/attestationutil/attestation_utils_test.go +++ b/shared/attestationutil/attestation_utils_test.go @@ -60,7 +60,7 @@ func TestIsValidAttestationIndices(t *testing.T) { Data: ð.AttestationData{ Target: ð.Checkpoint{}, }, - Signature: nil, + Signature: make([]byte, 96), }, want: "expected non-empty", }, @@ -71,7 +71,7 @@ func TestIsValidAttestationIndices(t *testing.T) { Data: ð.AttestationData{ Target: ð.Checkpoint{}, }, - Signature: nil, + Signature: make([]byte, 96), }, want: "indices count exceeds", }, @@ -82,7 +82,7 @@ func TestIsValidAttestationIndices(t *testing.T) { Data: ð.AttestationData{ Target: ð.Checkpoint{}, }, - Signature: nil, + Signature: make([]byte, 96), }, want: "not uniquely sorted", }, @@ -93,7 +93,7 @@ func TestIsValidAttestationIndices(t *testing.T) { Data: ð.AttestationData{ Target: ð.Checkpoint{}, }, - Signature: nil, + Signature: make([]byte, 96), }, }, } diff --git a/shared/bls/herumi/signature.go b/shared/bls/herumi/signature.go index f03ce8e256..008e1450f6 100644 --- a/shared/bls/herumi/signature.go +++ b/shared/bls/herumi/signature.go @@ -186,6 +186,9 @@ func VerifyMultipleSignatures(sigs []iface.Signature, msgs [][32]byte, pubKeys [ multiKeys := make([]bls12.PublicKey, length) for i := 0; i < len(pubKeys); i++ { + if pubKeys[i] == nil { + return false, errors.New("nil public key") + } // Perform scalar multiplication for the corresponding g1 points. g1 := new(bls12.G1) bls12.G1Mul(g1, bls12.CastFromPublicKey(pubKeys[i].(*PublicKey).p), &randNums[i]) diff --git a/shared/depositutil/deposit.go b/shared/depositutil/deposit.go index 4506220ad7..9d647550a7 100644 --- a/shared/depositutil/deposit.go +++ b/shared/depositutil/deposit.go @@ -58,13 +58,13 @@ func DepositInput( if err != nil { return nil, [32]byte{}, err } - root, err := ssz.HashTreeRoot(&p2ppb.SigningData{ObjectRoot: sr[:], Domain: domain}) + root, err := (&p2ppb.SigningData{ObjectRoot: sr[:], Domain: domain}).HashTreeRoot() if err != nil { return nil, [32]byte{}, err } di.Signature = depositKey.Sign(root[:]).Marshal() - dr, err := ssz.HashTreeRoot(di) + dr, err := di.HashTreeRoot() if err != nil { return nil, [32]byte{}, err } @@ -107,7 +107,7 @@ func VerifyDepositSignature(dd *ethpb.Deposit_Data, domain []byte) error { ObjectRoot: root[:], Domain: domain, } - ctrRoot, err := ssz.HashTreeRoot(signingData) + ctrRoot, err := signingData.HashTreeRoot() if err != nil { return errors.Wrap(err, "could not get container root") } diff --git a/shared/depositutil/deposit_test.go b/shared/depositutil/deposit_test.go index 195ab9babc..320acf2c9e 100644 --- a/shared/depositutil/deposit_test.go +++ b/shared/depositutil/deposit_test.go @@ -34,7 +34,7 @@ func TestDepositInput_GeneratesPb(t *testing.T) { nil, /*genesisValidatorsRoot*/ ) require.NoError(t, err) - root, err := ssz.HashTreeRoot(&pb.SigningData{ObjectRoot: sr[:], Domain: domain[:]}) + root, err := (&pb.SigningData{ObjectRoot: sr[:], Domain: domain[:]}).HashTreeRoot() require.NoError(t, err) assert.Equal(t, true, sig.Verify(k1.PublicKey(), root[:])) } diff --git a/shared/interop/generate_genesis_state.go b/shared/interop/generate_genesis_state.go index cdc087cc40..135d675484 100644 --- a/shared/interop/generate_genesis_state.go +++ b/shared/interop/generate_genesis_state.go @@ -131,7 +131,7 @@ func depositDataFromKeys(privKeys []bls.SecretKey, pubKeys []bls.PublicKey) ([]* if err != nil { return nil, nil, errors.Wrapf(err, "could not create deposit data for key: %#x", privKeys[i].Marshal()) } - hash, err := ssz.HashTreeRoot(data) + hash, err := data.HashTreeRoot() if err != nil { return nil, nil, errors.Wrap(err, "could not hash tree root deposit data item") } @@ -156,7 +156,7 @@ func createDepositData(privKey bls.SecretKey, pubKey bls.PublicKey) (*ethpb.Depo if err != nil { return nil, err } - root, err := ssz.HashTreeRoot(&pb.SigningData{ObjectRoot: sr[:], Domain: domain}) + root, err := (&pb.SigningData{ObjectRoot: sr[:], Domain: domain}).HashTreeRoot() if err != nil { return nil, err } diff --git a/shared/testutil/block.go b/shared/testutil/block.go index 53ee0850d6..01e948e182 100644 --- a/shared/testutil/block.go +++ b/shared/testutil/block.go @@ -60,6 +60,23 @@ func NewBeaconBlock() *ethpb.SignedBeaconBlock { } } +// NewAttestation creates an attestation block with minimum marshalable fields. +func NewAttestation() *ethpb.Attestation { + return ðpb.Attestation{ + AggregationBits: bitfield.Bitlist{0b1101}, + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Root: make([]byte, 32), + }, + }, + Signature: make([]byte, 96), + } +} + // GenerateFullBlock generates a fully valid block with the requested parameters. // Use BlockGenConfig to declare the conditions you would like the block generated under. func GenerateFullBlock( @@ -167,6 +184,7 @@ func GenerateFullBlock( Attestations: atts, VoluntaryExits: exits, Deposits: newDeposits, + Graffiti: make([]byte, 32), }, } if err := bState.SetSlot(currentSlot); err != nil { @@ -253,8 +271,9 @@ func GenerateAttesterSlashingForValidator( att1 := ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Slot: bState.Slot(), - CommitteeIndex: 0, + Slot: bState.Slot(), + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{ Epoch: currentEpoch, Root: params.BeaconConfig().ZeroHash[:], @@ -274,8 +293,9 @@ func GenerateAttesterSlashingForValidator( att2 := ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ - Slot: bState.Slot(), - CommitteeIndex: 0, + Slot: bState.Slot(), + CommitteeIndex: 0, + BeaconBlockRoot: make([]byte, 32), Target: ðpb.Checkpoint{ Epoch: currentEpoch, Root: params.BeaconConfig().ZeroHash[:], diff --git a/shared/testutil/deposits.go b/shared/testutil/deposits.go index b8aaf75678..fc4c792d44 100644 --- a/shared/testutil/deposits.go +++ b/shared/testutil/deposits.go @@ -74,7 +74,7 @@ func DeterministicDepositsAndKeys(numDeposits uint64) ([]*ethpb.Deposit, []bls.S if err != nil { return nil, nil, errors.Wrap(err, "could not get signing root of deposit data") } - sigRoot, err := ssz.HashTreeRoot(&pb.SigningData{ObjectRoot: root[:], Domain: domain}) + sigRoot, err := (&pb.SigningData{ObjectRoot: root[:], Domain: domain}).HashTreeRoot() if err != nil { return nil, nil, err } @@ -85,7 +85,7 @@ func DeterministicDepositsAndKeys(numDeposits uint64) ([]*ethpb.Deposit, []bls.S } cachedDeposits = append(cachedDeposits, deposit) - hashedDeposit, err := ssz.HashTreeRoot(deposit.Data) + hashedDeposit, err := deposit.Data.HashTreeRoot() if err != nil { return nil, nil, errors.Wrap(err, "could not tree hash deposit data") } @@ -173,7 +173,7 @@ func DeterministicGenesisState(t testing.TB, numValidators uint64) (*stateTrie.B func DepositTrieFromDeposits(deposits []*ethpb.Deposit) (*trieutil.SparseMerkleTrie, [][32]byte, error) { encodedDeposits := make([][]byte, len(deposits)) for i := 0; i < len(encodedDeposits); i++ { - hashedDeposit, err := ssz.HashTreeRoot(deposits[i].Data) + hashedDeposit, err := deposits[i].Data.HashTreeRoot() if err != nil { return nil, [][32]byte{}, errors.Wrap(err, "could not tree hash deposit data") } @@ -246,7 +246,7 @@ func DeterministicDepositsAndKeysSameValidator(numDeposits uint64) ([]*ethpb.Dep if err != nil { return nil, nil, errors.Wrap(err, "could not get signing root of deposit data") } - sigRoot, err := ssz.HashTreeRoot(&pb.SigningData{ObjectRoot: root[:], Domain: domain}) + sigRoot, err := (&pb.SigningData{ObjectRoot: root[:], Domain: domain}).HashTreeRoot() if err != nil { return nil, nil, errors.Wrap(err, "could not get signing root of deposit data and domain") } @@ -258,7 +258,7 @@ func DeterministicDepositsAndKeysSameValidator(numDeposits uint64) ([]*ethpb.Dep } cachedDeposits = append(cachedDeposits, deposit) - hashedDeposit, err := ssz.HashTreeRoot(deposit.Data) + hashedDeposit, err := deposit.Data.HashTreeRoot() if err != nil { return nil, nil, errors.Wrap(err, "could not tree hash deposit data") } diff --git a/shared/testutil/state_test.go b/shared/testutil/state_test.go index 7aa4ed4b75..bf83d9784a 100644 --- a/shared/testutil/state_test.go +++ b/shared/testutil/state_test.go @@ -1,10 +1,12 @@ package testutil import ( + "context" "reflect" "testing" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/testutil/require" ) func TestNewBeaconState(t *testing.T) { @@ -21,3 +23,11 @@ func TestNewBeaconState(t *testing.T) { t.Fatal("State did not match after round trip marshal") } } + +func TestNewBeaconState_HashTreeRoot(t *testing.T) { + _, err := st.HashTreeRoot(context.Background()) + require.NoError(t, err) + state := NewBeaconState() + _, err = state.HashTreeRoot(context.Background()) + require.NoError(t, err) +} diff --git a/slasher/db/kv/attester_slashings_test.go b/slasher/db/kv/attester_slashings_test.go index f2d2eaf6b5..953e6ca147 100644 --- a/slasher/db/kv/attester_slashings_test.go +++ b/slasher/db/kv/attester_slashings_test.go @@ -19,7 +19,24 @@ func TestStore_AttesterSlashingNilBucket(t *testing.T) { db := setupDB(t, cli.NewContext(&app, set, nil)) ctx := context.Background() - as := ðpb.AttesterSlashing{Attestation_1: ðpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("hello"), 96)}} + as := ðpb.AttesterSlashing{ + Attestation_1: ðpb.IndexedAttestation{ + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, + Signature: bytesutil.PadTo([]byte("hello"), 96), + }, + Attestation_2: ðpb.IndexedAttestation{ + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, + Signature: bytesutil.PadTo([]byte("hello"), 96), + }, + } has, _, err := db.HasAttesterSlashing(ctx, as) require.NoError(t, err, "HasAttesterSlashing should not return error") require.Equal(t, false, has) @@ -37,10 +54,11 @@ func TestStore_SaveAttesterSlashing(t *testing.T) { ctx := context.Background() data := ðpb.AttestationData{ - Source: ðpb.Checkpoint{}, - Target: ðpb.Checkpoint{}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), } - att := ðpb.IndexedAttestation{Data: data} + att := ðpb.IndexedAttestation{Data: data, Signature: make([]byte, 96)} tests := []struct { ss types.SlashingStatus as *ethpb.AttesterSlashing @@ -76,9 +94,9 @@ func TestStore_SaveAttesterSlashings(t *testing.T) { db := setupDB(t, cli.NewContext(&app, set, nil)) ctx := context.Background() - ckpt := ðpb.Checkpoint{} - data := ðpb.AttestationData{Source: ckpt, Target: ckpt} - att := ðpb.IndexedAttestation{Data: data} + ckpt := ðpb.Checkpoint{Root: make([]byte, 32)} + data := ðpb.AttestationData{Source: ckpt, Target: ckpt, BeaconBlockRoot: make([]byte, 32)} + att := ðpb.IndexedAttestation{Data: data, Signature: make([]byte, 96)} as := []*ethpb.AttesterSlashing{ {Attestation_1: ðpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("1"), 96), Data: data}, Attestation_2: att}, {Attestation_1: ðpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("2"), 96), Data: data}, Attestation_2: att}, @@ -101,21 +119,36 @@ func TestStore_UpdateAttesterSlashingStatus(t *testing.T) { db := setupDB(t, cli.NewContext(&app, set, nil)) ctx := context.Background() + data := ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + } + tests := []struct { ss types.SlashingStatus as *ethpb.AttesterSlashing }{ { ss: types.Active, - as: ðpb.AttesterSlashing{Attestation_1: ðpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("hello"), 96)}}, + as: ðpb.AttesterSlashing{ + Attestation_1: ðpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello"), 96)}, + Attestation_2: ðpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello"), 96)}, + }, }, { ss: types.Active, - as: ðpb.AttesterSlashing{Attestation_1: ðpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("hello2"), 96)}}, + as: ðpb.AttesterSlashing{ + Attestation_1: ðpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello2"), 96)}, + Attestation_2: ðpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello2"), 96)}, + }, }, { ss: types.Active, - as: ðpb.AttesterSlashing{Attestation_1: ðpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("hello3"), 96)}}, + as: ðpb.AttesterSlashing{ + Attestation_1: ðpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello3"), 96)}, + Attestation_2: ðpb.IndexedAttestation{Data: data, Signature: bytesutil.PadTo([]byte("hello2"), 96)}, + }, }, } diff --git a/slasher/db/kv/block_header_test.go b/slasher/db/kv/block_header_test.go index f80ce4642a..3d0bc8b08a 100644 --- a/slasher/db/kv/block_header_test.go +++ b/slasher/db/kv/block_header_test.go @@ -7,6 +7,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -39,16 +40,16 @@ func TestSaveHistoryBlkHdr(t *testing.T) { bh *ethpb.SignedBeaconBlockHeader }{ { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in"), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in"), 96), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 2nd"), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 2nd"), 96), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 3rd"), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 3rd"), 96), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 3rd"), Header: ðpb.BeaconBlockHeader{Slot: 1, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 3rd"), 96), Header: ðpb.BeaconBlockHeader{Slot: 1, ProposerIndex: 0}}, }, } @@ -73,13 +74,13 @@ func TestDeleteHistoryBlkHdr(t *testing.T) { bh *ethpb.SignedBeaconBlockHeader }{ { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in"), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in"), 96), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 2nd"), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 2nd"), 96), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 3rd"), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 3rd"), 96), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}}, }, } for _, tt := range tests { @@ -111,16 +112,16 @@ func TestHasHistoryBlkHdr(t *testing.T) { bh *ethpb.SignedBeaconBlockHeader }{ { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in"), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in"), 96), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 2nd"), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 2nd"), 96), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 3rd"), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 3rd"), 96), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 4th"), Header: ðpb.BeaconBlockHeader{Slot: 1, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 4th"), 96), Header: ðpb.BeaconBlockHeader{Slot: 1, ProposerIndex: 0}}, }, } for _, tt := range tests { @@ -148,19 +149,19 @@ func TestPruneHistoryBlkHdr(t *testing.T) { bh *ethpb.SignedBeaconBlockHeader }{ { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in"), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in"), 96), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 2nd"), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 2nd"), 96), Header: ðpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 3rd"), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 3rd"), 96), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 4th"), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch*2 + 1, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 4th"), 96), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch*2 + 1, ProposerIndex: 0}}, }, { - bh: ðpb.SignedBeaconBlockHeader{Signature: []byte("let me in 5th"), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch*3 + 1, ProposerIndex: 0}}, + bh: ðpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 5th"), 96), Header: ðpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch*3 + 1, ProposerIndex: 0}}, }, } diff --git a/slasher/db/kv/indexed_attestations_test.go b/slasher/db/kv/indexed_attestations_test.go index a50528103c..5eadd371d6 100644 --- a/slasher/db/kv/indexed_attestations_test.go +++ b/slasher/db/kv/indexed_attestations_test.go @@ -22,8 +22,8 @@ func init() { idxAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2}, }, @@ -32,8 +32,8 @@ func init() { idxAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{1, 2}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, }, Signature: []byte{3, 4}, }, @@ -42,8 +42,8 @@ func init() { idxAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 1}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, }, Signature: []byte{5, 6}, }, @@ -52,8 +52,8 @@ func init() { idxAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 1}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, }, Signature: []byte{5, 6}, }, @@ -102,8 +102,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2}, }, @@ -114,8 +114,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2}, }, @@ -127,8 +127,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{1, 2, 3}, @@ -136,8 +136,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 2"), }, Signature: []byte{1, 2, 4}, @@ -145,8 +145,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 3"), }, Signature: []byte{1, 2, 5}, @@ -158,8 +158,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{1, 2, 3}, @@ -167,8 +167,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 2"), }, Signature: []byte{1, 2, 4}, @@ -176,8 +176,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 3"), }, Signature: []byte{1, 2, 5}, @@ -190,8 +190,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{1, 2, 3}, @@ -199,8 +199,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{1, 2, 4}, @@ -208,8 +208,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 3"), }, Signature: []byte{1, 2, 5}, @@ -217,8 +217,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 2"), }, Signature: []byte{1, 3, 1}, @@ -226,8 +226,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 2"), }, Signature: []byte{0, 2, 4}, @@ -235,8 +235,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{4}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 2"), }, Signature: []byte{1, 2, 9}, @@ -248,8 +248,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{1, 2, 4}, @@ -257,8 +257,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{4}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 2"), }, Signature: []byte{1, 2, 9}, @@ -271,8 +271,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{3, 5, 3}, @@ -280,8 +280,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{3, 5, 3}, @@ -289,8 +289,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 2"), }, Signature: []byte{1, 2, 4}, @@ -298,8 +298,8 @@ func TestIndexedAttestationsWithPrefix(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 3"), }, Signature: []byte{1, 2, 5}, @@ -344,8 +344,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2}, }, @@ -355,8 +355,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2}, }, @@ -368,8 +368,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{1, 2, 3}, @@ -377,8 +377,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 2"), }, Signature: []byte{1, 5, 4}, @@ -386,8 +386,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 3"), }, Signature: []byte{8, 2, 5}, @@ -398,8 +398,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{1, 2, 3}, @@ -407,8 +407,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 2"), }, Signature: []byte{1, 5, 4}, @@ -416,8 +416,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 3"), }, Signature: []byte{8, 2, 5}, @@ -430,8 +430,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{3, 5, 3}, @@ -439,8 +439,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there"), }, Signature: []byte{3, 5, 3}, @@ -448,8 +448,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 2"), }, Signature: []byte{1, 2, 4}, @@ -457,8 +457,8 @@ func TestIndexedAttestationsForTarget(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 5}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 5, Root: make([]byte, 32)}, BeaconBlockRoot: []byte("hi there 3"), }, Signature: []byte{1, 2, 5}, @@ -502,8 +502,8 @@ func TestDeleteIndexedAttestation(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2}, }, @@ -512,8 +512,8 @@ func TestDeleteIndexedAttestation(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2}, }, @@ -526,24 +526,24 @@ func TestDeleteIndexedAttestation(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2}, }, { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 3}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, }, Signature: []byte{2, 4}, }, { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, }, Signature: []byte{3, 5}, }, @@ -552,16 +552,16 @@ func TestDeleteIndexedAttestation(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2}, }, { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, }, Signature: []byte{3, 5}, }, @@ -574,24 +574,24 @@ func TestDeleteIndexedAttestation(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2, 2}, }, { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2, 3}, }, { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2, 4}, }, @@ -600,8 +600,8 @@ func TestDeleteIndexedAttestation(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2, 3}, }, @@ -614,24 +614,24 @@ func TestDeleteIndexedAttestation(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2, 2}, }, { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2, 3}, }, { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2, 4}, }, @@ -640,8 +640,8 @@ func TestDeleteIndexedAttestation(t *testing.T) { { AttestingIndices: []uint64{3}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, }, Signature: []byte{1, 2, 6}, }, diff --git a/slasher/db/kv/proposer_slashings_test.go b/slasher/db/kv/proposer_slashings_test.go index 8a51d04710..abcee5df4f 100644 --- a/slasher/db/kv/proposer_slashings_test.go +++ b/slasher/db/kv/proposer_slashings_test.go @@ -20,7 +20,26 @@ func TestStore_ProposerSlashingNilBucket(t *testing.T) { db := setupDB(t, cli.NewContext(&app, set, nil)) ctx := context.Background() - ps := ðpb.ProposerSlashing{Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 1}}} + ps := ðpb.ProposerSlashing{ + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + } has, _, err := db.HasProposerSlashing(ctx, ps) require.NoError(t, err) require.Equal(t, false, has) @@ -44,22 +63,70 @@ func TestStore_SaveProposerSlashing(t *testing.T) { { ss: types.Active, ps: ðpb.ProposerSlashing{ - Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 1}}, - Header_2: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 1}}, + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, }, }, { ss: types.Included, ps: ðpb.ProposerSlashing{ - Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 2}}, - Header_2: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 2}}, + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 2, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 2, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, }, }, { ss: types.Reverted, ps: ðpb.ProposerSlashing{ - Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 3}}, - Header_2: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 3}}, + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 3, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 3, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, }, }, } @@ -91,15 +158,72 @@ func TestStore_UpdateProposerSlashingStatus(t *testing.T) { }{ { ss: types.Active, - ps: ðpb.ProposerSlashing{Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 1}}}, + ps: ðpb.ProposerSlashing{ + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + }, }, { ss: types.Active, - ps: ðpb.ProposerSlashing{Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 2}}}, + ps: ðpb.ProposerSlashing{ + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 2, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 2, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + }, }, { ss: types.Active, - ps: ðpb.ProposerSlashing{Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 3}}}, + ps: ðpb.ProposerSlashing{ + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 3, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 3, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + }, }, } @@ -130,16 +254,64 @@ func TestStore_SaveProposerSlashings(t *testing.T) { ps := []*ethpb.ProposerSlashing{ { - Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 1}}, - Header_2: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 1}}, + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 1, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, }, { - Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 2}}, - Header_2: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 2}}, + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 2, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 2, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, }, { - Header_1: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 3}}, - Header_2: ðpb.SignedBeaconBlockHeader{Header: ðpb.BeaconBlockHeader{ProposerIndex: 3}}, + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 3, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, + Header_2: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 3, + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), + }, }, } err := db.SaveProposerSlashings(ctx, types.Active, ps) diff --git a/slasher/detection/detect_test.go b/slasher/detection/detect_test.go index 44322a3dcd..bd2b1e8c56 100644 --- a/slasher/detection/detect_test.go +++ b/slasher/detection/detect_test.go @@ -31,8 +31,9 @@ func TestDetect_detectAttesterSlashings_Surround(t *testing.T) { { AttestingIndices: []uint64{3}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 9}, - Target: ðpb.Checkpoint{Epoch: 13}, + Source: ðpb.Checkpoint{Epoch: 9, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 13, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 2}, 96), }, @@ -40,9 +41,11 @@ func TestDetect_detectAttesterSlashings_Surround(t *testing.T) { incomingAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{1, 3, 7}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 7}, - Target: ðpb.Checkpoint{Epoch: 14}, + Source: ðpb.Checkpoint{Epoch: 7, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 14, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), }, slashingsFound: 1, }, @@ -52,8 +55,9 @@ func TestDetect_detectAttesterSlashings_Surround(t *testing.T) { { AttestingIndices: []uint64{0, 2, 4, 8}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 6}, - Target: ðpb.Checkpoint{Epoch: 10}, + Source: ðpb.Checkpoint{Epoch: 6, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 10, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 2}, 96), }, @@ -61,9 +65,11 @@ func TestDetect_detectAttesterSlashings_Surround(t *testing.T) { incomingAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0, 4}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 7}, - Target: ðpb.Checkpoint{Epoch: 9}, + Source: ðpb.Checkpoint{Epoch: 7, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 9, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), }, slashingsFound: 1, }, @@ -73,16 +79,18 @@ func TestDetect_detectAttesterSlashings_Surround(t *testing.T) { { AttestingIndices: []uint64{0, 2}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 4}, - Target: ðpb.Checkpoint{Epoch: 5}, + Source: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 5, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 2}, 96), }, { AttestingIndices: []uint64{4, 8}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 3}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 3}, 96), }, @@ -90,9 +98,11 @@ func TestDetect_detectAttesterSlashings_Surround(t *testing.T) { incomingAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0, 4}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 2}, - Target: ðpb.Checkpoint{Epoch: 7}, + Source: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 7, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), }, slashingsFound: 2, }, @@ -102,16 +112,18 @@ func TestDetect_detectAttesterSlashings_Surround(t *testing.T) { { AttestingIndices: []uint64{0, 2}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 4}, - Target: ðpb.Checkpoint{Epoch: 10}, + Source: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 10, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 2}, 96), }, { AttestingIndices: []uint64{4, 8}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 5}, - Target: ðpb.Checkpoint{Epoch: 9}, + Source: ðpb.Checkpoint{Epoch: 5, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 9, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 3}, 96), }, @@ -119,9 +131,11 @@ func TestDetect_detectAttesterSlashings_Surround(t *testing.T) { incomingAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0, 4}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 7}, - Target: ðpb.Checkpoint{Epoch: 8}, + Source: ðpb.Checkpoint{Epoch: 7, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 8, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), }, slashingsFound: 2, }, @@ -131,8 +145,9 @@ func TestDetect_detectAttesterSlashings_Surround(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 1}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 2}, 96), }, @@ -140,9 +155,11 @@ func TestDetect_detectAttesterSlashings_Surround(t *testing.T) { incomingAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 1}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), }, slashingsFound: 0, }, @@ -197,8 +214,9 @@ func TestDetect_detectAttesterSlashings_Double(t *testing.T) { { AttestingIndices: []uint64{3}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 3}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 2}, 96), }, @@ -206,8 +224,9 @@ func TestDetect_detectAttesterSlashings_Double(t *testing.T) { incomingAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{1, 3, 7}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 2}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 2}, 96), }, @@ -219,16 +238,18 @@ func TestDetect_detectAttesterSlashings_Double(t *testing.T) { { AttestingIndices: []uint64{1}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 3}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 2}, 96), }, { AttestingIndices: []uint64{3}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 1}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 1, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 3}, 96), }, @@ -236,8 +257,9 @@ func TestDetect_detectAttesterSlashings_Double(t *testing.T) { incomingAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{1, 3, 7}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 2}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, Signature: bytesutil.PadTo([]byte{1, 4}, 96), }, @@ -249,8 +271,8 @@ func TestDetect_detectAttesterSlashings_Double(t *testing.T) { { AttestingIndices: []uint64{0, 2, 4, 8}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 2}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, BeaconBlockRoot: bytesutil.PadTo([]byte("good block root"), 32), }, Signature: bytesutil.PadTo([]byte{1, 2}, 96), @@ -259,10 +281,11 @@ func TestDetect_detectAttesterSlashings_Double(t *testing.T) { incomingAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0, 4}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 2}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, BeaconBlockRoot: bytesutil.PadTo([]byte("bad block root"), 32), }, + Signature: make([]byte, 96), }, slashingsFound: 1, }, @@ -272,8 +295,8 @@ func TestDetect_detectAttesterSlashings_Double(t *testing.T) { { AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, BeaconBlockRoot: bytesutil.PadTo([]byte("good block root"), 32), }, Signature: bytesutil.PadTo([]byte{1, 2}, 96), @@ -282,10 +305,11 @@ func TestDetect_detectAttesterSlashings_Double(t *testing.T) { incomingAtt: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 0}, - Target: ðpb.Checkpoint{Epoch: 2}, + Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, BeaconBlockRoot: bytesutil.PadTo([]byte("good block root"), 32), }, + Signature: make([]byte, 96), }, slashingsFound: 0, }, diff --git a/slasher/rpc/server_test.go b/slasher/rpc/server_test.go index a77c14d9eb..8e55c3dfba 100644 --- a/slasher/rpc/server_test.go +++ b/slasher/rpc/server_test.go @@ -43,15 +43,17 @@ func TestServer_IsSlashableAttestation(t *testing.T) { } wantedGenesis := ðpb.Genesis{ - GenesisValidatorsRoot: []byte("I am genesis"), + GenesisValidatorsRoot: bytesutil.PadTo([]byte("I am genesis"), 32), } savedAttestation := ðpb.IndexedAttestation{ AttestingIndices: []uint64{3}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 3}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), } cfg := &detection.Config{ @@ -122,22 +124,26 @@ func TestServer_IsSlashableAttestationNoUpdate(t *testing.T) { ).Return(wantedValidators1, nil) wantedGenesis := ðpb.Genesis{ - GenesisValidatorsRoot: []byte("I am genesis"), + GenesisValidatorsRoot: bytesutil.PadTo([]byte("I am genesis"), 32), } nClient.EXPECT().GetGenesis(gomock.Any(), gomock.Any()).Return(wantedGenesis, nil) savedAttestation := ðpb.IndexedAttestation{ AttestingIndices: []uint64{3}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 3}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 3, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), } incomingAtt := ðpb.IndexedAttestation{ AttestingIndices: []uint64{1, 3}, Data: ðpb.AttestationData{ - Source: ðpb.Checkpoint{Epoch: 2}, - Target: ðpb.Checkpoint{Epoch: 4}, + Source: ðpb.Checkpoint{Epoch: 2, Root: make([]byte, 32)}, + Target: ðpb.Checkpoint{Epoch: 4, Root: make([]byte, 32)}, + BeaconBlockRoot: make([]byte, 32), }, + Signature: make([]byte, 96), } cfg := &detection.Config{ SlasherDB: db, @@ -193,7 +199,7 @@ func TestServer_IsSlashableBlock(t *testing.T) { ).Return(wantedValidators, nil).AnyTimes() wantedGenesis := ðpb.Genesis{ - GenesisValidatorsRoot: []byte("I am genesis"), + GenesisValidatorsRoot: bytesutil.PadTo([]byte("I am genesis"), 32), } nClient.EXPECT().GetGenesis(gomock.Any(), gomock.Any()).Return(wantedGenesis, nil).AnyTimes() savedBlock := ðpb.SignedBeaconBlockHeader{ @@ -201,7 +207,10 @@ func TestServer_IsSlashableBlock(t *testing.T) { Slot: 1, ProposerIndex: 1, BodyRoot: bytesutil.PadTo([]byte("body root"), 32), + StateRoot: make([]byte, 32), + ParentRoot: make([]byte, 32), }, + Signature: make([]byte, 96), } cfg := &detection.Config{ @@ -266,7 +275,7 @@ func TestServer_IsSlashableBlockNoUpdate(t *testing.T) { ).Return(wantedValidators, nil) wantedGenesis := ðpb.Genesis{ - GenesisValidatorsRoot: []byte("I am genesis"), + GenesisValidatorsRoot: bytesutil.PadTo([]byte("I am genesis"), 32), } nClient.EXPECT().GetGenesis(gomock.Any(), gomock.Any()).Return(wantedGenesis, nil) savedBlock := ðpb.SignedBeaconBlockHeader{ diff --git a/tools/sendDepositTx/BUILD.bazel b/tools/sendDepositTx/BUILD.bazel index b1a75e80a1..69a16cad83 100644 --- a/tools/sendDepositTx/BUILD.bazel +++ b/tools/sendDepositTx/BUILD.bazel @@ -44,7 +44,6 @@ go_test( "@com_github_ethereum_go_ethereum//:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", ], ) diff --git a/tools/sendDepositTx/sendDeposits_test.go b/tools/sendDepositTx/sendDeposits_test.go index e22e2c8d1b..86c0488be7 100644 --- a/tools/sendDepositTx/sendDeposits_test.go +++ b/tools/sendDepositTx/sendDeposits_test.go @@ -12,7 +12,6 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-ssz" contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" "github.com/prysmaticlabs/prysm/shared/interop" "github.com/prysmaticlabs/prysm/shared/params" @@ -135,7 +134,7 @@ func TestEndtoEndDeposits(t *testing.T) { encodedDeposits := make([][]byte, numberOfValidators*numberOfDeposits) for i := 0; i < int(numberOfValidators); i++ { - hashedDeposit, err := ssz.HashTreeRoot(deposits[i].Data) + hashedDeposit, err := deposits[i].Data.HashTreeRoot() if err != nil { t.Fatalf("could not tree hash deposit data: %v", err) } diff --git a/validator/client/BUILD.bazel b/validator/client/BUILD.bazel index 6ce6873d91..906f179fe9 100644 --- a/validator/client/BUILD.bazel +++ b/validator/client/BUILD.bazel @@ -37,6 +37,7 @@ go_library( "//validator/keymanager/v2:go_default_library", "//validator/slashing-protection:go_default_library", "@com_github_dgraph_io_ristretto//:go_default_library", + "@com_github_ferranbt_fastssz//:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_gogo_protobuf//types:go_default_library", "@com_github_grpc_ecosystem_go_grpc_middleware//:go_default_library", diff --git a/validator/client/aggregate_test.go b/validator/client/aggregate_test.go index cc5841e072..610b9f188c 100644 --- a/validator/client/aggregate_test.go +++ b/validator/client/aggregate_test.go @@ -6,6 +6,7 @@ import ( "github.com/golang/mock/gomock" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/go-bitfield" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" @@ -40,7 +41,7 @@ func TestSubmitAggregateAndProof_Ok(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().SubmitAggregateSelectionProof( gomock.Any(), // ctx @@ -48,20 +49,28 @@ func TestSubmitAggregateAndProof_Ok(t *testing.T) { ).Return(ðpb.AggregateSelectionResponse{ AggregateAndProof: ðpb.AggregateAttestationAndProof{ AggregatorIndex: 0, - Aggregate: ðpb.Attestation{Data: ðpb.AttestationData{}}, - SelectionProof: nil, + Aggregate: ðpb.Attestation{ + Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, + Signature: make([]byte, 96), + AggregationBits: make([]byte, 1), + }, + SelectionProof: make([]byte, 96), }, }, nil) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().SubmitSignedAggregateSelectionProof( gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.SignedAggregateSubmitRequest{}), - ).Return(ðpb.SignedAggregateSubmitResponse{}, nil) + ).Return(ðpb.SignedAggregateSubmitResponse{AttestationDataRoot: make([]byte, 32)}, nil) validator.SubmitAggregateAndProof(context.Background(), 0, validatorPubKey) } @@ -88,12 +97,19 @@ func TestAggregateAndProofSignature_CanSignValidSignature(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx ðpb.DomainRequest{Epoch: 0, Domain: params.BeaconConfig().DomainAggregateAndProof[:]}, - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) agg := ðpb.AggregateAttestationAndProof{ AggregatorIndex: 0, - Aggregate: ðpb.Attestation{Data: ðpb.AttestationData{}}, - SelectionProof: nil, + Aggregate: ðpb.Attestation{ + AggregationBits: bitfield.NewBitlist(1), Data: ðpb.AttestationData{ + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, + }, + Signature: make([]byte, 96), + }, + SelectionProof: make([]byte, 96), } sig, err := validator.aggregateAndProofSig(context.Background(), validatorPubKey, agg) require.NoError(t, err) diff --git a/validator/client/attest_test.go b/validator/client/attest_test.go index cf7fdf4774..10fb51ce79 100644 --- a/validator/client/attest_test.go +++ b/validator/client/attest_test.go @@ -63,14 +63,14 @@ func TestAttestToBlockHead_SubmitAttestation_RequestFailure(t *testing.T) { gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.AttestationDataRequest{}), ).Return(ðpb.AttestationData{ - BeaconBlockRoot: []byte{}, - Target: ðpb.Checkpoint{}, - Source: ðpb.Checkpoint{}, + BeaconBlockRoot: make([]byte, 32), + Target: ðpb.Checkpoint{Root: make([]byte, 32)}, + Source: ðpb.Checkpoint{Root: make([]byte, 32)}, }, nil) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch2 - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeAttestation( gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.Attestation{}), @@ -115,7 +115,7 @@ func TestAttestToBlockHead_AttestsCorrectly(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{SignatureDomain: []byte{}}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) var generatedAttestation *ethpb.Attestation m.validatorClient.EXPECT().ProposeAttestation( @@ -136,9 +136,10 @@ func TestAttestToBlockHead_AttestsCorrectly(t *testing.T) { Source: ðpb.Checkpoint{Root: sourceRoot[:], Epoch: 3}, }, AggregationBits: aggregationBitfield, + Signature: make([]byte, 96), } - root, err := helpers.ComputeSigningRoot(expectedAttestation.Data, []byte{}) + root, err := helpers.ComputeSigningRoot(expectedAttestation.Data, make([]byte, 32)) require.NoError(t, err) sig, err := validator.keyManager.Sign(validatorPubKey, root) @@ -186,12 +187,12 @@ func TestAttestToBlockHead_BlocksDoubleAtt(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeAttestation( gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.Attestation{}), - ).Return(ðpb.AttestResponse{}, nil /* error */) + ).Return(ðpb.AttestResponse{AttestationDataRoot: make([]byte, 32)}, nil /* error */) validator.SubmitAttestation(context.Background(), 30, validatorPubKey) validator.SubmitAttestation(context.Background(), 30, validatorPubKey) @@ -233,7 +234,7 @@ func TestAttestToBlockHead_BlocksSurroundAtt(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeAttestation( gomock.Any(), // ctx @@ -280,7 +281,7 @@ func TestAttestToBlockHead_BlocksSurroundedAtt(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeAttestation( gomock.Any(), // ctx @@ -294,9 +295,9 @@ func TestAttestToBlockHead_BlocksSurroundedAtt(t *testing.T) { gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.AttestationDataRequest{}), ).Return(ðpb.AttestationData{ - BeaconBlockRoot: []byte("A"), - Target: ðpb.Checkpoint{Root: []byte("B"), Epoch: 2}, - Source: ðpb.Checkpoint{Root: []byte("C"), Epoch: 1}, + BeaconBlockRoot: bytesutil.PadTo([]byte("A"), 32), + Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("B"), 32), Epoch: 2}, + Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("C"), 32), Epoch: 1}, }, nil) validator.SubmitAttestation(context.Background(), 30, validatorPubKey) @@ -352,9 +353,9 @@ func TestAttestToBlockHead_DoesAttestAfterDelay(t *testing.T) { gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.AttestationDataRequest{}), ).Return(ðpb.AttestationData{ - BeaconBlockRoot: []byte("A"), - Target: ðpb.Checkpoint{Root: []byte("B")}, - Source: ðpb.Checkpoint{Root: []byte("C"), Epoch: 3}, + BeaconBlockRoot: bytesutil.PadTo([]byte("A"), 32), + Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("B"), 32)}, + Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("C"), 32), Epoch: 3}, }, nil).Do(func(arg0, arg1 interface{}) { wg.Done() }) @@ -362,7 +363,7 @@ func TestAttestToBlockHead_DoesAttestAfterDelay(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeAttestation( gomock.Any(), // ctx @@ -388,14 +389,15 @@ func TestAttestToBlockHead_CorrectBitfieldLength(t *testing.T) { gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.AttestationDataRequest{}), ).Return(ðpb.AttestationData{ - Target: ðpb.Checkpoint{Root: []byte("B")}, - Source: ðpb.Checkpoint{Root: []byte("C"), Epoch: 3}, + Target: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("B"), 32)}, + Source: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte("C"), 32), Epoch: 3}, + BeaconBlockRoot: make([]byte, 32), }, nil) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) var generatedAttestation *ethpb.Attestation m.validatorClient.EXPECT().ProposeAttestation( diff --git a/validator/client/propose_test.go b/validator/client/propose_test.go index 55c46c095a..7bd0e9a8aa 100644 --- a/validator/client/propose_test.go +++ b/validator/client/propose_test.go @@ -12,6 +12,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/mock" "github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" testing2 "github.com/prysmaticlabs/prysm/validator/db/testing" @@ -83,7 +84,7 @@ func TestProposeBlock_RequestBlockFailed(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().GetBlock( gomock.Any(), // ctx @@ -102,17 +103,17 @@ func TestProposeBlock_ProposeBlockFailed(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().GetBlock( gomock.Any(), // ctx gomock.Any(), - ).Return(ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}, nil /*err*/) + ).Return(testutil.NewBeaconBlock().Block, nil /*err*/) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeBlock( gomock.Any(), // ctx @@ -136,22 +137,22 @@ func TestProposeBlock_BlocksDoubleProposal(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Times(2).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Times(2).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().GetBlock( gomock.Any(), // ctx gomock.Any(), - ).Times(2).Return(ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}, nil /*err*/) + ).Times(2).Return(testutil.NewBeaconBlock().Block, nil /*err*/) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeBlock( gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.SignedBeaconBlock{}), - ).Return(ðpb.ProposeResponse{}, nil /*error*/) + ).Return(ðpb.ProposeResponse{BlockRoot: make([]byte, 32)}, nil /*error*/) slot := params.BeaconConfig().SlotsPerEpoch*5 + 2 validator.ProposeBlock(context.Background(), slot, validatorPubKey) @@ -174,22 +175,22 @@ func TestProposeBlock_BlocksDoubleProposal_After54KEpochs(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Times(2).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Times(2).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().GetBlock( gomock.Any(), // ctx gomock.Any(), - ).Times(2).Return(ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}, nil /*err*/) + ).Times(2).Return(testutil.NewBeaconBlock().Block, nil /*err*/) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeBlock( gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.SignedBeaconBlock{}), - ).Return(ðpb.ProposeResponse{}, nil /*error*/) + ).Return(ðpb.ProposeResponse{BlockRoot: make([]byte, 32)}, nil /*error*/) farFuture := (params.BeaconConfig().WeakSubjectivityPeriod + 9) * params.BeaconConfig().SlotsPerEpoch validator.ProposeBlock(context.Background(), farFuture, validatorPubKey) @@ -212,38 +213,36 @@ func TestProposeBlock_AllowsPastProposals(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Times(2).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Times(2).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) farAhead := (params.BeaconConfig().WeakSubjectivityPeriod + 9) * params.BeaconConfig().SlotsPerEpoch + blk := testutil.NewBeaconBlock() + blk.Block.Slot = farAhead m.validatorClient.EXPECT().GetBlock( gomock.Any(), // ctx gomock.Any(), - ).Return(ðpb.BeaconBlock{ - Slot: farAhead, - Body: ðpb.BeaconBlockBody{}, - }, nil /*err*/) + ).Return(blk.Block, nil /*err*/) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Times(2).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Times(2).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeBlock( gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.SignedBeaconBlock{}), - ).Times(2).Return(ðpb.ProposeResponse{}, nil /*error*/) + ).Times(2).Return(ðpb.ProposeResponse{BlockRoot: make([]byte, 32)}, nil /*error*/) validator.ProposeBlock(context.Background(), farAhead, validatorPubKey) require.LogsDoNotContain(t, hook, failedPreBlockSignLocalErr) past := (params.BeaconConfig().WeakSubjectivityPeriod - 400) * params.BeaconConfig().SlotsPerEpoch + blk2 := testutil.NewBeaconBlock() + blk2.Block.Slot = past m.validatorClient.EXPECT().GetBlock( gomock.Any(), // ctx gomock.Any(), - ).Return(ðpb.BeaconBlock{ - Slot: past, - Body: ðpb.BeaconBlockBody{}, - }, nil /*err*/) + ).Return(blk2.Block, nil /*err*/) validator.ProposeBlock(context.Background(), past, validatorPubKey) require.LogsDoNotContain(t, hook, failedPreBlockSignLocalErr) } @@ -261,38 +260,36 @@ func TestProposeBlock_AllowsSameEpoch(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Times(2).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Times(2).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) farAhead := (params.BeaconConfig().WeakSubjectivityPeriod + 9) * params.BeaconConfig().SlotsPerEpoch + blk := testutil.NewBeaconBlock() + blk.Block.Slot = farAhead m.validatorClient.EXPECT().GetBlock( gomock.Any(), // ctx gomock.Any(), - ).Return(ðpb.BeaconBlock{ - Slot: farAhead, - Body: ðpb.BeaconBlockBody{}, - }, nil /*err*/) + ).Return(blk.Block, nil /*err*/) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Times(2).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Times(2).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeBlock( gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.SignedBeaconBlock{}), - ).Times(2).Return(ðpb.ProposeResponse{}, nil /*error*/) + ).Times(2).Return(ðpb.ProposeResponse{BlockRoot: make([]byte, 32)}, nil /*error*/) pubKey := validatorPubKey validator.ProposeBlock(context.Background(), farAhead, pubKey) require.LogsDoNotContain(t, hook, failedPreBlockSignLocalErr) + blk2 := testutil.NewBeaconBlock() + blk2.Block.Slot = farAhead - 4 m.validatorClient.EXPECT().GetBlock( gomock.Any(), // ctx gomock.Any(), - ).Return(ðpb.BeaconBlock{ - Slot: farAhead - 4, - Body: ðpb.BeaconBlockBody{}, - }, nil /*err*/) + ).Return(blk2.Block, nil /*err*/) validator.ProposeBlock(context.Background(), farAhead-4, pubKey) require.LogsDoNotContain(t, hook, failedPreBlockSignLocalErr) @@ -305,22 +302,22 @@ func TestProposeBlock_BroadcastsBlock(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().GetBlock( gomock.Any(), // ctx gomock.Any(), - ).Return(ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}, nil /*err*/) + ).Return(testutil.NewBeaconBlock().Block, nil /*err*/) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().ProposeBlock( gomock.Any(), // ctx gomock.AssignableToTypeOf(ðpb.SignedBeaconBlock{}), - ).Return(ðpb.ProposeResponse{}, nil /*error*/) + ).Return(ðpb.ProposeResponse{BlockRoot: make([]byte, 32)}, nil /*error*/) validator.ProposeBlock(context.Background(), 1, validatorPubKey) } @@ -334,17 +331,19 @@ func TestProposeBlock_BroadcastsBlock_WithGraffiti(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) + blk := testutil.NewBeaconBlock() + blk.Block.Body.Graffiti = validator.graffiti m.validatorClient.EXPECT().GetBlock( gomock.Any(), // ctx gomock.Any(), - ).Return(ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{Graffiti: validator.graffiti}}, nil /*err*/) + ).Return(blk.Block, nil /*err*/) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), //epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) var sentBlock *ethpb.SignedBeaconBlock @@ -353,7 +352,7 @@ func TestProposeBlock_BroadcastsBlock_WithGraffiti(t *testing.T) { gomock.AssignableToTypeOf(ðpb.SignedBeaconBlock{}), ).DoAndReturn(func(ctx context.Context, block *ethpb.SignedBeaconBlock) (*ethpb.ProposeResponse, error) { sentBlock = block - return ðpb.ProposeResponse{}, nil + return ðpb.ProposeResponse{BlockRoot: make([]byte, 32)}, nil }) validator.ProposeBlock(context.Background(), 1, validatorPubKey) diff --git a/validator/client/service.go b/validator/client/service.go index 5264c2f2b0..fbb1f36f99 100644 --- a/validator/client/service.go +++ b/validator/client/service.go @@ -6,6 +6,7 @@ import ( "time" "github.com/dgraph-io/ristretto" + fssz "github.com/ferranbt/fastssz" middleware "github.com/grpc-ecosystem/go-grpc-middleware" grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry" grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing" @@ -204,7 +205,14 @@ func (v *validator) signObject( } if protectingKeymanager, supported := v.keyManager.(keymanager.ProtectingKeyManager); supported { - root, err := ssz.HashTreeRoot(object) + var root [32]byte + var err error + if v, ok := object.(fssz.HashRoot); ok { + root, err = v.HashTreeRoot() + } else { + root, err = ssz.HashTreeRoot(object) + } + if err != nil { return nil, err } diff --git a/validator/client/validator_test.go b/validator/client/validator_test.go index 316c2287a6..163e9059da 100644 --- a/validator/client/validator_test.go +++ b/validator/client/validator_test.go @@ -741,12 +741,12 @@ func TestRolesAt_OK(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) roleMap, err := v.RolesAt(context.Background(), 1) require.NoError(t, err) @@ -794,7 +794,7 @@ func TestRolesAt_DoesNotAssignProposer_Slot0(t *testing.T) { m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx gomock.Any(), // epoch - ).Return(ðpb.DomainResponse{}, nil /*err*/) + ).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) roleMap, err := v.RolesAt(context.Background(), 0) require.NoError(t, err) diff --git a/validator/slashing-protection/BUILD.bazel b/validator/slashing-protection/BUILD.bazel index bd052c82f7..f99c3aff83 100644 --- a/validator/slashing-protection/BUILD.bazel +++ b/validator/slashing-protection/BUILD.bazel @@ -32,6 +32,7 @@ go_test( srcs = ["external_test.go"], embed = [":go_default_library"], deps = [ + "//shared/bytesutil:go_default_library", "//shared/testutil/assert:go_default_library", "//validator/testing:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", diff --git a/validator/slashing-protection/external_test.go b/validator/slashing-protection/external_test.go index 9c54b9b572..e45ebe04c2 100644 --- a/validator/slashing-protection/external_test.go +++ b/validator/slashing-protection/external_test.go @@ -5,6 +5,7 @@ import ( "testing" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" mockSlasher "github.com/prysmaticlabs/prysm/validator/testing" ) @@ -61,9 +62,9 @@ func TestService_CommitBlock(t *testing.T) { Header: ð.BeaconBlockHeader{ Slot: 0, ProposerIndex: 0, - ParentRoot: []byte("parent"), - StateRoot: []byte("state"), - BodyRoot: []byte("body"), + ParentRoot: bytesutil.PadTo([]byte("parent"), 32), + StateRoot: bytesutil.PadTo([]byte("state"), 32), + BodyRoot: bytesutil.PadTo([]byte("body"), 32), }, } assert.Equal(t, false, s.CommitBlock(context.Background(), blk), "Expected commit block to fail verification") @@ -76,9 +77,9 @@ func TestService_VerifyBlock(t *testing.T) { blk := ð.BeaconBlockHeader{ Slot: 0, ProposerIndex: 0, - ParentRoot: []byte("parent"), - StateRoot: []byte("state"), - BodyRoot: []byte("body"), + ParentRoot: bytesutil.PadTo([]byte("parent"), 32), + StateRoot: bytesutil.PadTo([]byte("state"), 32), + BodyRoot: bytesutil.PadTo([]byte("body"), 32), } assert.Equal(t, false, s.CheckBlockSafety(context.Background(), blk), "Expected verify block to fail verification") s = &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: false}}