From b4e8c2bb99fa5366f7322fda1361dadfb41c2112 Mon Sep 17 00:00:00 2001 From: nisdas Date: Fri, 21 May 2021 13:23:42 +0800 Subject: [PATCH 01/44] commit initial work --- beacon-chain/blockchain/process_block.go | 8 +- .../blockchain/process_block_helpers.go | 24 +++-- beacon-chain/interfaces/BUILD.bazel | 15 +++ beacon-chain/interfaces/block_interface.go | 32 ++++++ beacon-chain/interfaces/block_wrapper.go | 102 ++++++++++++++++++ 5 files changed, 167 insertions(+), 14 deletions(-) create mode 100644 beacon-chain/interfaces/BUILD.bazel create mode 100644 beacon-chain/interfaces/block_interface.go create mode 100644 beacon-chain/interfaces/block_wrapper.go diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 7912cb62d40c..7a3080cf924d 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -5,6 +5,8 @@ import ( "fmt" "time" + "github.com/prysmaticlabs/prysm/beacon-chain/interfaces" + "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" @@ -81,14 +83,14 @@ var initialSyncBlockCacheSize = uint64(2 * params.BeaconConfig().SlotsPerEpoch) // ancestor_at_finalized_slot = get_ancestor(store, store.justified_checkpoint.root, finalized_slot) // if ancestor_at_finalized_slot != store.finalized_checkpoint.root: // store.justified_checkpoint = state.current_justified_checkpoint -func (s *Service) onBlock(ctx context.Context, signed *ethpb.SignedBeaconBlock, blockRoot [32]byte) error { +func (s *Service) onBlock(ctx context.Context, signed interfaces.SignedBeaconBlock, blockRoot [32]byte) error { ctx, span := trace.StartSpan(ctx, "blockChain.onBlock") defer span.End() - if signed == nil || signed.Block == nil { + if signed.IsNil() || signed.Block().IsNil() { return errors.New("nil block") } - b := signed.Block + b := signed.Block() preState, err := s.getBlockPreState(ctx, b) if err != nil { diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index d8bbe86cb213..6dc72518cd2f 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -5,6 +5,8 @@ import ( "context" "fmt" + "github.com/prysmaticlabs/prysm/beacon-chain/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -26,7 +28,7 @@ func (s *Service) CurrentSlot() types.Slot { // getBlockPreState returns the pre state of an incoming block. It uses the parent root of the block // to retrieve the state in DB. It verifies the pre state's validity and the incoming block // is in the correct time window. -func (s *Service) getBlockPreState(ctx context.Context, b *ethpb.BeaconBlock) (iface.BeaconState, error) { +func (s *Service) getBlockPreState(ctx context.Context, b interfaces.BeaconBlock) (iface.BeaconState, error) { ctx, span := trace.StartSpan(ctx, "blockChain.getBlockPreState") defer span.End() @@ -35,16 +37,16 @@ func (s *Service) getBlockPreState(ctx context.Context, b *ethpb.BeaconBlock) (i return nil, err } - preState, err := s.cfg.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(b.ParentRoot)) + preState, err := s.cfg.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(b.ParentRoot())) if err != nil { - return nil, errors.Wrapf(err, "could not get pre state for slot %d", b.Slot) + return nil, errors.Wrapf(err, "could not get pre state for slot %d", b.Slot()) } if preState == nil { - return nil, errors.Wrapf(err, "nil pre state for slot %d", b.Slot) + return nil, errors.Wrapf(err, "nil pre state for slot %d", b.Slot()) } // Verify block slot time is not from the future. - if err := helpers.VerifySlotTime(preState.GenesisTime(), b.Slot, params.BeaconNetworkConfig().MaximumGossipClockDisparity); err != nil { + if err := helpers.VerifySlotTime(preState.GenesisTime(), b.Slot(), params.BeaconNetworkConfig().MaximumGossipClockDisparity); err != nil { return nil, err } @@ -57,11 +59,11 @@ func (s *Service) getBlockPreState(ctx context.Context, b *ethpb.BeaconBlock) (i } // verifyBlkPreState validates input block has a valid pre-state. -func (s *Service) verifyBlkPreState(ctx context.Context, b *ethpb.BeaconBlock) error { +func (s *Service) verifyBlkPreState(ctx context.Context, b interfaces.BeaconBlock) error { ctx, span := trace.StartSpan(ctx, "blockChain.verifyBlkPreState") defer span.End() - parentRoot := bytesutil.ToBytes32(b.ParentRoot) + parentRoot := bytesutil.ToBytes32(b.ParentRoot()) // Loosen the check to HasBlock because state summary gets saved in batches // during initial syncing. There's no risk given a state summary object is just a // a subset of the block object. @@ -69,7 +71,7 @@ func (s *Service) verifyBlkPreState(ctx context.Context, b *ethpb.BeaconBlock) e return errors.New("could not reconstruct parent state") } - if err := s.VerifyBlkDescendant(ctx, bytesutil.ToBytes32(b.ParentRoot)); err != nil { + if err := s.VerifyBlkDescendant(ctx, bytesutil.ToBytes32(b.ParentRoot())); err != nil { return err } @@ -120,13 +122,13 @@ func (s *Service) VerifyBlkDescendant(ctx context.Context, root [32]byte) error // verifyBlkFinalizedSlot validates input block is not less than or equal // to current finalized slot. -func (s *Service) verifyBlkFinalizedSlot(b *ethpb.BeaconBlock) error { +func (s *Service) verifyBlkFinalizedSlot(b interfaces.BeaconBlock) error { finalizedSlot, err := helpers.StartSlot(s.finalizedCheckpt.Epoch) if err != nil { return err } - if finalizedSlot >= b.Slot { - return fmt.Errorf("block is equal or earlier than finalized block, slot %d < slot %d", b.Slot, finalizedSlot) + if finalizedSlot >= b.Slot() { + return fmt.Errorf("block is equal or earlier than finalized block, slot %d < slot %d", b.Slot(), finalizedSlot) } return nil } diff --git a/beacon-chain/interfaces/BUILD.bazel b/beacon-chain/interfaces/BUILD.bazel new file mode 100644 index 000000000000..9412f2b0c221 --- /dev/null +++ b/beacon-chain/interfaces/BUILD.bazel @@ -0,0 +1,15 @@ +load("@prysm//tools/go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = [ + "block_interface.go", + "block_wrapper.go", + ], + importpath = "github.com/prysmaticlabs/prysm/beacon-chain/interfaces", + visibility = ["//visibility:public"], + deps = [ + "@com_github_prysmaticlabs_eth2_types//:go_default_library", + "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", + ], +) diff --git a/beacon-chain/interfaces/block_interface.go b/beacon-chain/interfaces/block_interface.go new file mode 100644 index 000000000000..dd054d636f0d --- /dev/null +++ b/beacon-chain/interfaces/block_interface.go @@ -0,0 +1,32 @@ +package interfaces + +import ( + types "github.com/prysmaticlabs/eth2-types" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" +) + +type SignedBeaconBlock interface { + Block() BeaconBlock + Signature() []byte + IsNil() bool +} + +type BeaconBlock interface { + Slot() types.Slot + ProposerIndex() types.ValidatorIndex + ParentRoot() []byte + StateRoot() []byte + Body() BeaconBlockBody + IsNil() bool +} +type BeaconBlockBody interface { + RandaoReveal() []byte + Eth1Data() *ethpb.Eth1Data + Graffiti() []byte + ProposerSlashings() []*ethpb.ProposerSlashing + AttesterSlashings() []*ethpb.AttesterSlashing + Attestations() []*ethpb.Attestation + Deposits() []*ethpb.Deposit + VoluntaryExits() []*ethpb.SignedVoluntaryExit + IsNil() bool +} diff --git a/beacon-chain/interfaces/block_wrapper.go b/beacon-chain/interfaces/block_wrapper.go new file mode 100644 index 000000000000..8da33799a944 --- /dev/null +++ b/beacon-chain/interfaces/block_wrapper.go @@ -0,0 +1,102 @@ +package interfaces + +import ( + types "github.com/prysmaticlabs/eth2-types" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" +) + +type WrappedSignedBeaconBlock struct { + b *ethpb.SignedBeaconBlock +} + +func NewWrappedSignedBeaconBlock(b *ethpb.SignedBeaconBlock) WrappedSignedBeaconBlock { + return WrappedSignedBeaconBlock{b: b} +} + +func (w WrappedSignedBeaconBlock) Signature() []byte { + return w.b.Signature +} + +func (w WrappedSignedBeaconBlock) Block() BeaconBlock { + return NewWrappedBeaconBlock(w.b.Block) +} + +func (w WrappedSignedBeaconBlock) IsNil() bool { + return w.b == nil +} + +type WrappedBeaconBlock struct { + b *ethpb.BeaconBlock +} + +func NewWrappedBeaconBlock(b *ethpb.BeaconBlock) WrappedBeaconBlock { + return WrappedBeaconBlock{b: b} +} + +func (w WrappedBeaconBlock) Slot() types.Slot { + return w.b.Slot +} + +func (w WrappedBeaconBlock) ProposerIndex() types.ValidatorIndex { + return w.b.ProposerIndex +} + +func (w WrappedBeaconBlock) ParentRoot() []byte { + return w.b.ParentRoot +} + +func (w WrappedBeaconBlock) StateRoot() []byte { + return w.b.StateRoot +} + +func (w WrappedBeaconBlock) Body() BeaconBlockBody { + return NewWrappedBeaconBlockBody(w.b.Body) +} + +func (w WrappedBeaconBlock) IsNil() bool { + return w.b == nil +} + +type WrappedBeaconBlockBody struct { + b *ethpb.BeaconBlockBody +} + +func NewWrappedBeaconBlockBody(b *ethpb.BeaconBlockBody) WrappedBeaconBlockBody { + return WrappedBeaconBlockBody{b: b} +} + +func (w WrappedBeaconBlockBody) RandaoReveal() []byte { + return w.b.RandaoReveal +} + +func (w WrappedBeaconBlockBody) Eth1Data() *ethpb.Eth1Data { + return w.b.Eth1Data +} + +func (w WrappedBeaconBlockBody) Graffiti() []byte { + return w.b.Graffiti +} + +func (w WrappedBeaconBlockBody) ProposerSlashings() []*ethpb.ProposerSlashing { + return w.b.ProposerSlashings +} + +func (w WrappedBeaconBlockBody) AttesterSlashings() []*ethpb.AttesterSlashing { + return w.b.AttesterSlashings +} + +func (w WrappedBeaconBlockBody) Attestations() []*ethpb.Attestation { + return w.b.Attestations +} + +func (w WrappedBeaconBlockBody) Deposits() []*ethpb.Deposit { + return w.b.Deposits +} + +func (w WrappedBeaconBlockBody) VoluntaryExits() []*ethpb.SignedVoluntaryExit { + return w.b.VoluntaryExits +} + +func (w WrappedBeaconBlockBody) IsNil() bool { + return w.b == nil +} From ee2e4af4b29f5c28870afc9d593966a9a87617e5 Mon Sep 17 00:00:00 2001 From: nisdas Date: Fri, 21 May 2021 16:23:05 +0800 Subject: [PATCH 02/44] checkpoint current work --- beacon-chain/blockchain/BUILD.bazel | 3 +- beacon-chain/blockchain/chain_info.go | 9 +-- beacon-chain/blockchain/head.go | 39 +++++++------ .../blockchain/init_sync_process_block.go | 12 ++-- beacon-chain/blockchain/metrics.go | 8 ++- .../blockchain/process_attestation.go | 5 +- beacon-chain/blockchain/process_block.go | 35 ++++++------ .../blockchain/process_block_helpers.go | 47 ++++++++-------- beacon-chain/blockchain/receive_block.go | 7 ++- beacon-chain/blockchain/service.go | 33 ++++++----- beacon-chain/cache/BUILD.bazel | 2 +- beacon-chain/cache/attestation_data.go | 5 +- beacon-chain/core/blocks/BUILD.bazel | 3 +- beacon-chain/core/blocks/attestation.go | 12 ++-- beacon-chain/core/blocks/eth1_data.go | 5 +- beacon-chain/core/blocks/eth1_data_test.go | 4 +- beacon-chain/core/blocks/header.go | 10 ++-- beacon-chain/core/blocks/randao.go | 11 ++-- beacon-chain/core/epoch/BUILD.bazel | 2 +- beacon-chain/core/epoch/epoch_processing.go | 7 ++- beacon-chain/core/feed/state/events.go | 5 +- beacon-chain/core/helpers/block.go | 11 ++-- .../core/state/interop/write_block_to_disk.go | 7 ++- beacon-chain/core/state/transition.go | 55 ++++++++++--------- .../core/state/transition_no_verify_sig.go | 55 ++++++++++--------- beacon-chain/db/iface/BUILD.bazel | 1 + beacon-chain/db/iface/interface.go | 11 ++-- .../operations/attestations/BUILD.bazel | 2 +- .../operations/attestations/kv/BUILD.bazel | 2 +- .../operations/attestations/kv/aggregated.go | 5 +- .../operations/attestations/kv/block.go | 4 +- .../operations/attestations/kv/forkchoice.go | 6 +- .../attestations/kv/unaggregated.go | 7 ++- .../attestations/prepare_forkchoice.go | 5 +- beacon-chain/rpc/validator/BUILD.bazel | 2 +- beacon-chain/rpc/validator/attester.go | 5 +- .../validator/proposer_utils_bench_test.go | 5 +- beacon-chain/state/stateV0/BUILD.bazel | 3 +- beacon-chain/state/stateV0/getters_eth1.go | 5 +- beacon-chain/state/stateV0/getters_state.go | 6 +- .../state/stateV0/getters_validator.go | 6 +- beacon-chain/state/stateV0/references_test.go | 6 +- beacon-chain/state/stateV0/setters_block.go | 4 +- beacon-chain/sync/BUILD.bazel | 1 + .../sync/pending_blocks_queue_test.go | 9 +-- shared/aggregation/attestations/BUILD.bazel | 2 +- .../aggregation/attestations/attestations.go | 6 +- .../attestations/attestations_bench_test.go | 5 +- shared/aggregation/attestations/maxcover.go | 6 +- shared/blockutil/BUILD.bazel | 7 ++- .../stateV0 => shared/blockutil}/cloners.go | 2 +- shared/depositutil/BUILD.bazel | 2 +- shared/depositutil/deposit.go | 4 +- .../interfaces/BUILD.bazel | 0 .../interfaces/block_interface.go | 3 + .../interfaces/block_wrapper.go | 13 +++++ slasher/rpc/BUILD.bazel | 2 +- slasher/rpc/server_test.go | 6 +- 58 files changed, 304 insertions(+), 241 deletions(-) rename {beacon-chain/state/stateV0 => shared/blockutil}/cloners.go (99%) rename {beacon-chain => shared}/interfaces/BUILD.bazel (100%) rename {beacon-chain => shared}/interfaces/block_interface.go (87%) rename {beacon-chain => shared}/interfaces/block_wrapper.go (85%) diff --git a/beacon-chain/blockchain/BUILD.bazel b/beacon-chain/blockchain/BUILD.bazel index 06aba828a2c5..38fb5bbbbee2 100644 --- a/beacon-chain/blockchain/BUILD.bazel +++ b/beacon-chain/blockchain/BUILD.bazel @@ -36,17 +36,18 @@ go_library( "//beacon-chain/db/filters:go_default_library", "//beacon-chain/forkchoice:go_default_library", "//beacon-chain/forkchoice/protoarray:go_default_library", + "//beacon-chain/interfaces:go_default_library", "//beacon-chain/operations/attestations:go_default_library", "//beacon-chain/operations/slashings:go_default_library", "//beacon-chain/operations/voluntaryexits:go_default_library", "//beacon-chain/p2p:go_default_library", "//beacon-chain/powchain:go_default_library", "//beacon-chain/state/interface:go_default_library", - "//beacon-chain/state/stateV0:go_default_library", "//beacon-chain/state/stategen:go_default_library", "//cmd/beacon-chain/flags:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/attestationutil:go_default_library", + "//shared/blockutil:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", "//shared/featureconfig:go_default_library", diff --git a/beacon-chain/blockchain/chain_info.go b/beacon-chain/blockchain/chain_info.go index f51618f6f44a..3e9585bf628d 100644 --- a/beacon-chain/blockchain/chain_info.go +++ b/beacon-chain/blockchain/chain_info.go @@ -4,12 +4,13 @@ import ( "context" "time" + "github.com/prysmaticlabs/prysm/shared/blockutil" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" @@ -76,7 +77,7 @@ func (s *Service) FinalizedCheckpt() *ethpb.Checkpoint { return ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]} } - return stateV0.CopyCheckpoint(s.finalizedCheckpt) + return blockutil.CopyCheckpoint(s.finalizedCheckpt) } // CurrentJustifiedCheckpt returns the current justified checkpoint from head state. @@ -85,7 +86,7 @@ func (s *Service) CurrentJustifiedCheckpt() *ethpb.Checkpoint { return ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]} } - return stateV0.CopyCheckpoint(s.justifiedCheckpt) + return blockutil.CopyCheckpoint(s.justifiedCheckpt) } // PreviousJustifiedCheckpt returns the previous justified checkpoint from head state. @@ -94,7 +95,7 @@ func (s *Service) PreviousJustifiedCheckpt() *ethpb.Checkpoint { return ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]} } - return stateV0.CopyCheckpoint(s.prevJustifiedCheckpt) + return blockutil.CopyCheckpoint(s.prevJustifiedCheckpt) } // HeadSlot returns the slot of the head of the chain. diff --git a/beacon-chain/blockchain/head.go b/beacon-chain/blockchain/head.go index f50687d60dc3..4a9828ce0d79 100644 --- a/beacon-chain/blockchain/head.go +++ b/beacon-chain/blockchain/head.go @@ -7,14 +7,13 @@ import ( "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "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" "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/sirupsen/logrus" "go.opencensus.io/trace" @@ -22,10 +21,10 @@ import ( // This defines the current chain service's view of head. type head struct { - slot types.Slot // current head slot. - root [32]byte // current head root. - block *ethpb.SignedBeaconBlock // current head block. - state iface.BeaconState // current head state. + slot types.Slot // current head slot. + root [32]byte // current head root. + block interfaces.SignedBeaconBlock // current head block. + state iface.BeaconState // current head state. } // Determined the head from the fork choice service and saves its new data @@ -104,7 +103,7 @@ func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error { if err != nil { return err } - if newHeadBlock == nil || newHeadBlock.Block == nil { + if newHeadBlock.IsNil() || newHeadBlock.Block().IsNil() { return errors.New("cannot save nil head block") } @@ -119,15 +118,15 @@ func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error { // A chain re-org occurred, so we fire an event notifying the rest of the services. headSlot := s.HeadSlot() - if bytesutil.ToBytes32(newHeadBlock.Block.ParentRoot) != bytesutil.ToBytes32(r) { + if bytesutil.ToBytes32(newHeadBlock.Block().ParentRoot()) != bytesutil.ToBytes32(r) { log.WithFields(logrus.Fields{ - "newSlot": fmt.Sprintf("%d", newHeadBlock.Block.Slot), + "newSlot": fmt.Sprintf("%d", newHeadBlock.Block().Slot()), "oldSlot": fmt.Sprintf("%d", headSlot), }).Debug("Chain reorg occurred") s.cfg.StateNotifier.StateFeed().Send(&feed.Event{ Type: statefeed.Reorg, Data: &statefeed.ReorgData{ - NewSlot: newHeadBlock.Block.Slot, + NewSlot: newHeadBlock.Block().Slot(), OldSlot: headSlot, }, }) @@ -149,7 +148,7 @@ func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error { // This gets called to update canonical root mapping. It does not save head block // root in DB. With the inception of initial-sync-cache-state flag, it uses finalized // check point as anchors to resume sync therefore head is no longer needed to be saved on per slot basis. -func (s *Service) saveHeadNoDB(ctx context.Context, b *ethpb.SignedBeaconBlock, r [32]byte, hs iface.BeaconState) error { +func (s *Service) saveHeadNoDB(ctx context.Context, b interfaces.SignedBeaconBlock, r [32]byte, hs iface.BeaconState) error { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return err } @@ -161,20 +160,20 @@ func (s *Service) saveHeadNoDB(ctx context.Context, b *ethpb.SignedBeaconBlock, return nil } - s.setHeadInitialSync(r, stateV0.CopySignedBeaconBlock(b), hs) + s.setHeadInitialSync(r, b.Copy(), hs) return nil } // This sets head view object which is used to track the head slot, root, block and state. -func (s *Service) setHead(root [32]byte, block *ethpb.SignedBeaconBlock, state iface.BeaconState) { +func (s *Service) setHead(root [32]byte, block interfaces.SignedBeaconBlock, state iface.BeaconState) { s.headLock.Lock() defer s.headLock.Unlock() // This does a full copy of the block and state. s.head = &head{ - slot: block.Block.Slot, + slot: block.Block().Slot(), root: root, - block: stateV0.CopySignedBeaconBlock(block), + block: block.Copy(), state: state.Copy(), } } @@ -182,15 +181,15 @@ func (s *Service) setHead(root [32]byte, block *ethpb.SignedBeaconBlock, state i // This sets head view object which is used to track the head slot, root, block and state. The method // assumes that state being passed into the method will not be modified by any other alternate // caller which holds the state's reference. -func (s *Service) setHeadInitialSync(root [32]byte, block *ethpb.SignedBeaconBlock, state iface.BeaconState) { +func (s *Service) setHeadInitialSync(root [32]byte, block interfaces.SignedBeaconBlock, state iface.BeaconState) { s.headLock.Lock() defer s.headLock.Unlock() // This does a full copy of the block only. s.head = &head{ - slot: block.Block.Slot, + slot: block.Block().Slot(), root: root, - block: stateV0.CopySignedBeaconBlock(block), + block: block.Copy(), state: state, } } @@ -215,8 +214,8 @@ func (s *Service) headRoot() [32]byte { // This returns the head block. // It does a full copy on head block for immutability. // This is a lock free version. -func (s *Service) headBlock() *ethpb.SignedBeaconBlock { - return stateV0.CopySignedBeaconBlock(s.head.block) +func (s *Service) headBlock() interfaces.SignedBeaconBlock { + return s.head.block.Copy() } // This returns the head state. diff --git a/beacon-chain/blockchain/init_sync_process_block.go b/beacon-chain/blockchain/init_sync_process_block.go index 85efc57a73f7..7963a7a9bd92 100644 --- a/beacon-chain/blockchain/init_sync_process_block.go +++ b/beacon-chain/blockchain/init_sync_process_block.go @@ -1,11 +1,11 @@ package blockchain import ( - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/shared/interfaces" ) // This saves a beacon block to the initial sync blocks cache. -func (s *Service) saveInitSyncBlock(r [32]byte, b *ethpb.SignedBeaconBlock) { +func (s *Service) saveInitSyncBlock(r [32]byte, b interfaces.SignedBeaconBlock) { s.initSyncBlocksLock.Lock() defer s.initSyncBlocksLock.Unlock() s.initSyncBlocks[r] = b @@ -22,7 +22,7 @@ func (s *Service) hasInitSyncBlock(r [32]byte) bool { // This retrieves a beacon block from the initial sync blocks cache using the root of // the block. -func (s *Service) getInitSyncBlock(r [32]byte) *ethpb.SignedBeaconBlock { +func (s *Service) getInitSyncBlock(r [32]byte) interfaces.SignedBeaconBlock { s.initSyncBlocksLock.RLock() defer s.initSyncBlocksLock.RUnlock() b := s.initSyncBlocks[r] @@ -31,11 +31,11 @@ func (s *Service) getInitSyncBlock(r [32]byte) *ethpb.SignedBeaconBlock { // This retrieves all the beacon blocks from the initial sync blocks cache, the returned // blocks are unordered. -func (s *Service) getInitSyncBlocks() []*ethpb.SignedBeaconBlock { +func (s *Service) getInitSyncBlocks() []interfaces.SignedBeaconBlock { s.initSyncBlocksLock.RLock() defer s.initSyncBlocksLock.RUnlock() - blks := make([]*ethpb.SignedBeaconBlock, 0, len(s.initSyncBlocks)) + blks := make([]interfaces.SignedBeaconBlock, 0, len(s.initSyncBlocks)) for _, b := range s.initSyncBlocks { blks = append(blks, b) } @@ -46,5 +46,5 @@ func (s *Service) getInitSyncBlocks() []*ethpb.SignedBeaconBlock { func (s *Service) clearInitSyncBlocks() { s.initSyncBlocksLock.Lock() defer s.initSyncBlocksLock.Unlock() - s.initSyncBlocks = make(map[[32]byte]*ethpb.SignedBeaconBlock) + s.initSyncBlocks = make(map[[32]byte]interfaces.SignedBeaconBlock) } diff --git a/beacon-chain/blockchain/metrics.go b/beacon-chain/blockchain/metrics.go index bb8ad1efedbb..39ac094f1e8f 100644 --- a/beacon-chain/blockchain/metrics.go +++ b/beacon-chain/blockchain/metrics.go @@ -3,6 +3,8 @@ package blockchain import ( "context" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" types "github.com/prysmaticlabs/eth2-types" @@ -227,8 +229,8 @@ func reportEpochMetrics(ctx context.Context, postState, headState iface.BeaconSt return nil } -func reportAttestationInclusion(blk *ethpb.BeaconBlock) { - for _, att := range blk.Body.Attestations { - attestationInclusionDelay.Observe(float64(blk.Slot - att.Data.Slot)) +func reportAttestationInclusion(blk interfaces.BeaconBlock) { + for _, att := range blk.Body().Attestations() { + attestationInclusionDelay.Observe(float64(blk.Slot() - att.Data.Slot)) } } diff --git a/beacon-chain/blockchain/process_attestation.go b/beacon-chain/blockchain/process_attestation.go index ed5ce3c1862e..331a77c198e6 100644 --- a/beacon-chain/blockchain/process_attestation.go +++ b/beacon-chain/blockchain/process_attestation.go @@ -3,10 +3,11 @@ package blockchain import ( "context" + "github.com/prysmaticlabs/prysm/shared/blockutil" + "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" @@ -46,7 +47,7 @@ func (s *Service) onAttestation(ctx context.Context, a *ethpb.Attestation) error if err := helpers.ValidateSlotTargetEpoch(a.Data); err != nil { return err } - tgt := stateV0.CopyCheckpoint(a.Data.Target) + tgt := blockutil.CopyCheckpoint(a.Data.Target) // Note that target root check is ignored here because it was performed in sync's validation pipeline: // validate_aggregate_proof.go and validate_beacon_attestation.go diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 7a3080cf924d..512ee8795445 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -5,8 +5,6 @@ import ( "fmt" "time" - "github.com/prysmaticlabs/prysm/beacon-chain/interfaces" - "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" @@ -19,6 +17,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "go.opencensus.io/trace" ) @@ -145,7 +144,7 @@ func (s *Service) onBlock(ctx context.Context, signed interfaces.SignedBeaconBlo s.cfg.StateNotifier.StateFeed().Send(&feed.Event{ Type: statefeed.BlockProcessed, Data: &statefeed.BlockProcessedData{ - Slot: signed.Block.Slot, + Slot: signed.Block().Slot(), BlockRoot: blockRoot, SignedBlock: signed, Verified: true, @@ -184,7 +183,7 @@ func (s *Service) onBlock(ctx context.Context, signed interfaces.SignedBeaconBlo return s.handleEpochBoundary(ctx, postState) } -func (s *Service) onBlockBatch(ctx context.Context, blks []*ethpb.SignedBeaconBlock, +func (s *Service) onBlockBatch(ctx context.Context, blks []interfaces.SignedBeaconBlock, blockRoots [][32]byte) ([]*ethpb.Checkpoint, []*ethpb.Checkpoint, error) { ctx, span := trace.StartSpan(ctx, "blockChain.onBlockBatch") defer span.End() @@ -192,16 +191,16 @@ func (s *Service) onBlockBatch(ctx context.Context, blks []*ethpb.SignedBeaconBl if len(blks) == 0 || len(blockRoots) == 0 { return nil, nil, errors.New("no blocks provided") } - if blks[0] == nil || blks[0].Block == nil { + if blks[0].IsNil() || blks[0].Block().IsNil() { return nil, nil, errors.New("nil block") } - b := blks[0].Block + b := blks[0].Block() // Retrieve incoming block's pre state. if err := s.verifyBlkPreState(ctx, b); err != nil { return nil, nil, err } - preState, err := s.cfg.StateGen.StateByRootInitialSync(ctx, bytesutil.ToBytes32(b.ParentRoot)) + preState, err := s.cfg.StateGen.StateByRootInitialSync(ctx, bytesutil.ToBytes32(b.ParentRoot())) if err != nil { return nil, nil, err } @@ -260,16 +259,16 @@ func (s *Service) onBlockBatch(ctx context.Context, blks []*ethpb.SignedBeaconBl // handles a block after the block's batch has been verified, where we can save blocks // their state summaries and split them off to relative hot/cold storage. -func (s *Service) handleBlockAfterBatchVerify(ctx context.Context, signed *ethpb.SignedBeaconBlock, +func (s *Service) handleBlockAfterBatchVerify(ctx context.Context, signed interfaces.SignedBeaconBlock, blockRoot [32]byte, fCheckpoint, jCheckpoint *ethpb.Checkpoint) error { - b := signed.Block + b := signed.Block() s.saveInitSyncBlock(blockRoot, signed) if err := s.insertBlockToForkChoiceStore(ctx, b, blockRoot, fCheckpoint, jCheckpoint); err != nil { return err } if err := s.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{ - Slot: signed.Block.Slot, + Slot: signed.Block().Slot(), Root: blockRoot[:], }); err != nil { return err @@ -338,7 +337,7 @@ func (s *Service) handleEpochBoundary(ctx context.Context, postState iface.Beaco // This feeds in the block and block's attestations to fork choice store. It's allows fork choice store // to gain information on the most current chain. -func (s *Service) insertBlockAndAttestationsToForkChoiceStore(ctx context.Context, blk *ethpb.BeaconBlock, root [32]byte, +func (s *Service) insertBlockAndAttestationsToForkChoiceStore(ctx context.Context, blk interfaces.BeaconBlock, root [32]byte, st iface.BeaconState) error { fCheckpoint := st.FinalizedCheckpoint() jCheckpoint := st.CurrentJustifiedCheckpoint() @@ -346,7 +345,7 @@ func (s *Service) insertBlockAndAttestationsToForkChoiceStore(ctx context.Contex return err } // Feed in block's attestations to fork choice store. - for _, a := range blk.Body.Attestations { + for _, a := range blk.Body().Attestations() { committee, err := helpers.BeaconCommitteeFromState(st, a.Data.Slot, a.Data.CommitteeIndex) if err != nil { return err @@ -360,14 +359,14 @@ func (s *Service) insertBlockAndAttestationsToForkChoiceStore(ctx context.Contex return nil } -func (s *Service) insertBlockToForkChoiceStore(ctx context.Context, blk *ethpb.BeaconBlock, +func (s *Service) insertBlockToForkChoiceStore(ctx context.Context, blk interfaces.BeaconBlock, root [32]byte, fCheckpoint, jCheckpoint *ethpb.Checkpoint) error { if err := s.fillInForkChoiceMissingBlocks(ctx, blk, fCheckpoint, jCheckpoint); err != nil { return err } // Feed in block to fork choice store. if err := s.cfg.ForkChoiceStore.ProcessBlock(ctx, - blk.Slot, root, bytesutil.ToBytes32(blk.ParentRoot), bytesutil.ToBytes32(blk.Body.Graffiti), + blk.Slot(), root, bytesutil.ToBytes32(blk.ParentRoot()), bytesutil.ToBytes32(blk.Body().Graffiti()), jCheckpoint.Epoch, fCheckpoint.Epoch); err != nil { return errors.Wrap(err, "could not process block for proto array fork choice") @@ -377,19 +376,19 @@ func (s *Service) insertBlockToForkChoiceStore(ctx context.Context, blk *ethpb.B // This saves post state info to DB or cache. This also saves post state info to fork choice store. // Post state info consists of processed block and state. Do not call this method unless the block and state are verified. -func (s *Service) savePostStateInfo(ctx context.Context, r [32]byte, b *ethpb.SignedBeaconBlock, st iface.BeaconState, initSync bool) error { +func (s *Service) savePostStateInfo(ctx context.Context, r [32]byte, b interfaces.SignedBeaconBlock, st iface.BeaconState, initSync bool) error { ctx, span := trace.StartSpan(ctx, "blockChain.savePostStateInfo") defer span.End() if initSync { s.saveInitSyncBlock(r, b) } else if err := s.cfg.BeaconDB.SaveBlock(ctx, b); err != nil { - return errors.Wrapf(err, "could not save block from slot %d", b.Block.Slot) + return errors.Wrapf(err, "could not save block from slot %d", b.Block().Slot()) } if err := s.cfg.StateGen.SaveState(ctx, r, st); err != nil { return errors.Wrap(err, "could not save state") } - if err := s.insertBlockAndAttestationsToForkChoiceStore(ctx, b.Block, r, st); err != nil { - return errors.Wrapf(err, "could not insert block %d to fork choice store", b.Block.Slot) + if err := s.insertBlockAndAttestationsToForkChoiceStore(ctx, b.Block(), r, st); err != nil { + return errors.Wrapf(err, "could not insert block %d to fork choice store", b.Block().Slot()) } return nil } diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index 6dc72518cd2f..9a37dde69ad4 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -5,8 +5,6 @@ import ( "context" "fmt" - "github.com/prysmaticlabs/prysm/beacon-chain/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -15,6 +13,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/traceutil" "go.opencensus.io/trace" @@ -98,11 +97,11 @@ func (s *Service) VerifyBlkDescendant(ctx context.Context, root [32]byte) error if err != nil { return err } - if finalizedBlkSigned == nil || finalizedBlkSigned.Block == nil { + if finalizedBlkSigned.IsNil() || finalizedBlkSigned.Block().IsNil() { return errors.New("nil finalized block") } - finalizedBlk := finalizedBlkSigned.Block - bFinalizedRoot, err := s.ancestor(ctx, root[:], finalizedBlk.Slot) + finalizedBlk := finalizedBlkSigned.Block() + bFinalizedRoot, err := s.ancestor(ctx, root[:], finalizedBlk.Slot()) if err != nil { return errors.Wrap(err, "could not get finalized block root") } @@ -141,7 +140,7 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified if helpers.SlotsSinceEpochStarts(s.CurrentSlot()) < params.BeaconConfig().SafeSlotsToUpdateJustified { return true, nil } - var newJustifiedBlockSigned *ethpb.SignedBeaconBlock + var newJustifiedBlockSigned interfaces.SignedBeaconBlock justifiedRoot := s.ensureRootNotZeros(bytesutil.ToBytes32(newJustifiedCheckpt.Root)) var err error if s.hasInitSyncBlock(justifiedRoot) { @@ -152,19 +151,19 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified return false, err } } - if newJustifiedBlockSigned == nil || newJustifiedBlockSigned.Block == nil { + if newJustifiedBlockSigned.IsNil() || newJustifiedBlockSigned.Block().IsNil() { return false, errors.New("nil new justified block") } - newJustifiedBlock := newJustifiedBlockSigned.Block + newJustifiedBlock := newJustifiedBlockSigned.Block() jSlot, err := helpers.StartSlot(s.justifiedCheckpt.Epoch) if err != nil { return false, err } - if newJustifiedBlock.Slot <= jSlot { + if newJustifiedBlock.Slot() <= jSlot { return false, nil } - var justifiedBlockSigned *ethpb.SignedBeaconBlock + var justifiedBlockSigned interfaces.SignedBeaconBlock cachedJustifiedRoot := s.ensureRootNotZeros(bytesutil.ToBytes32(s.justifiedCheckpt.Root)) if s.hasInitSyncBlock(cachedJustifiedRoot) { justifiedBlockSigned = s.getInitSyncBlock(cachedJustifiedRoot) @@ -178,8 +177,8 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified if justifiedBlockSigned == nil || justifiedBlockSigned.Block == nil { return false, errors.New("nil justified block") } - justifiedBlock := justifiedBlockSigned.Block - b, err := s.ancestor(ctx, justifiedRoot[:], justifiedBlock.Slot) + justifiedBlock := justifiedBlockSigned.Block() + b, err := s.ancestor(ctx, justifiedRoot[:], justifiedBlock.Slot()) if err != nil { return false, err } @@ -309,15 +308,15 @@ func (s *Service) ancestorByDB(ctx context.Context, r [32]byte, slot types.Slot) signed = s.getInitSyncBlock(r) } - if signed == nil || signed.Block == nil { + if signed.IsNil() || signed.Block().IsNil() { return nil, errors.New("nil block") } - b := signed.Block - if b.Slot == slot || b.Slot < slot { + b := signed.Block() + if b.Slot() == slot || b.Slot() < slot { return r[:], nil } - return s.ancestorByDB(ctx, bytesutil.ToBytes32(b.ParentRoot), slot) + return s.ancestorByDB(ctx, bytesutil.ToBytes32(b.ParentRoot()), slot) } // This updates justified check point in store, if the new justified is later than stored justified or @@ -365,13 +364,13 @@ func (s *Service) finalizedImpliesNewJustified(ctx context.Context, state iface. // This retrieves missing blocks from DB (ie. the blocks that couldn't be received over sync) and inserts them to fork choice store. // This is useful for block tree visualizer and additional vote accounting. -func (s *Service) fillInForkChoiceMissingBlocks(ctx context.Context, blk *ethpb.BeaconBlock, +func (s *Service) fillInForkChoiceMissingBlocks(ctx context.Context, blk interfaces.BeaconBlock, fCheckpoint, jCheckpoint *ethpb.Checkpoint) error { - pendingNodes := make([]*ethpb.BeaconBlock, 0) + pendingNodes := make([]interfaces.BeaconBlock, 0) pendingRoots := make([][32]byte, 0) - parentRoot := bytesutil.ToBytes32(blk.ParentRoot) - slot := blk.Slot + parentRoot := bytesutil.ToBytes32(blk.ParentRoot()) + slot := blk.Slot() // Fork choice only matters from last finalized slot. fSlot, err := helpers.StartSlot(s.finalizedCheckpt.Epoch) if err != nil { @@ -385,11 +384,11 @@ func (s *Service) fillInForkChoiceMissingBlocks(ctx context.Context, blk *ethpb. return err } - pendingNodes = append(pendingNodes, b.Block) + pendingNodes = append(pendingNodes, b.Block()) copiedRoot := parentRoot pendingRoots = append(pendingRoots, copiedRoot) - parentRoot = bytesutil.ToBytes32(b.Block.ParentRoot) - slot = b.Block.Slot + parentRoot = bytesutil.ToBytes32(b.Block().ParentRoot()) + slot = b.Block().Slot() higherThanFinalized = slot > fSlot } @@ -399,7 +398,7 @@ func (s *Service) fillInForkChoiceMissingBlocks(ctx context.Context, blk *ethpb. b := pendingNodes[i] r := pendingRoots[i] if err := s.cfg.ForkChoiceStore.ProcessBlock(ctx, - b.Slot, r, bytesutil.ToBytes32(b.ParentRoot), bytesutil.ToBytes32(b.Body.Graffiti), + b.Slot(), r, bytesutil.ToBytes32(b.ParentRoot()), bytesutil.ToBytes32(b.Body().Graffiti()), jCheckpoint.Epoch, fCheckpoint.Epoch); err != nil { return errors.Wrap(err, "could not process block for proto array fork choice") diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index 9156d11863ce..9faafdfebc16 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -3,13 +3,14 @@ package blockchain import ( "context" + "github.com/prysmaticlabs/prysm/shared/blockutil" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "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" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/timeutils" "github.com/prysmaticlabs/prysm/shared/traceutil" @@ -35,7 +36,7 @@ func (s *Service) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlo ctx, span := trace.StartSpan(ctx, "blockChain.ReceiveBlock") defer span.End() receivedTime := timeutils.Now() - blockCopy := stateV0.CopySignedBeaconBlock(block) + blockCopy := blockutil.CopySignedBeaconBlock(block) // Apply state transition on the new block. if err := s.onBlock(ctx, blockCopy, blockRoot); err != nil { @@ -100,7 +101,7 @@ func (s *Service) ReceiveBlockBatch(ctx context.Context, blocks []*ethpb.SignedB } for i, b := range blocks { - blockCopy := stateV0.CopySignedBeaconBlock(b) + blockCopy := blockutil.CopySignedBeaconBlock(b) if err = s.handleBlockAfterBatchVerify(ctx, blockCopy, blkRoots[i], fCheckpoints[i], jCheckpoints[i]); err != nil { traceutil.AnnotateError(span, err) return err diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 001eb6013ff4..17b761ff89da 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -10,6 +10,10 @@ import ( "sync" "time" + "github.com/prysmaticlabs/prysm/shared/blockutil" + + "github.com/prysmaticlabs/prysm/beacon-chain/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -28,7 +32,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/p2p" "github.com/prysmaticlabs/prysm/beacon-chain/powchain" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" "github.com/prysmaticlabs/prysm/shared/bytesutil" @@ -60,7 +63,7 @@ type Service struct { nextEpochBoundarySlot types.Slot boundaryRoots [][32]byte checkpointStateCache *cache.CheckpointStateCache - initSyncBlocks map[[32]byte]*ethpb.SignedBeaconBlock + initSyncBlocks map[[32]byte]interfaces.SignedBeaconBlock initSyncBlocksLock sync.RWMutex justifiedBalances []uint64 justifiedBalancesLock sync.RWMutex @@ -95,7 +98,7 @@ func NewService(ctx context.Context, cfg *Config) (*Service, error) { cancel: cancel, boundaryRoots: [][32]byte{}, checkpointStateCache: cache.NewCheckpointStateCache(), - initSyncBlocks: make(map[[32]byte]*ethpb.SignedBeaconBlock), + initSyncBlocks: make(map[[32]byte]interfaces.SignedBeaconBlock), justifiedBalances: make([]uint64, 0), }, nil } @@ -164,14 +167,14 @@ func (s *Service) Start() { } // Resume fork choice. - s.justifiedCheckpt = stateV0.CopyCheckpoint(justifiedCheckpoint) + s.justifiedCheckpt = blockutil.CopyCheckpoint(justifiedCheckpoint) if err := s.cacheJustifiedStateBalances(s.ctx, s.ensureRootNotZeros(bytesutil.ToBytes32(s.justifiedCheckpt.Root))); err != nil { log.Fatalf("Could not cache justified state balances: %v", err) } - s.prevJustifiedCheckpt = stateV0.CopyCheckpoint(justifiedCheckpoint) - s.bestJustifiedCheckpt = stateV0.CopyCheckpoint(justifiedCheckpoint) - s.finalizedCheckpt = stateV0.CopyCheckpoint(finalizedCheckpoint) - s.prevFinalizedCheckpt = stateV0.CopyCheckpoint(finalizedCheckpoint) + s.prevJustifiedCheckpt = blockutil.CopyCheckpoint(justifiedCheckpoint) + s.bestJustifiedCheckpt = blockutil.CopyCheckpoint(justifiedCheckpoint) + s.finalizedCheckpt = blockutil.CopyCheckpoint(finalizedCheckpoint) + s.prevFinalizedCheckpt = blockutil.CopyCheckpoint(finalizedCheckpoint) s.resumeForkChoice(justifiedCheckpoint, finalizedCheckpoint) ss, err := helpers.StartSlot(s.finalizedCheckpt.Epoch) @@ -351,14 +354,14 @@ func (s *Service) saveGenesisData(ctx context.Context, genesisState iface.Beacon // Finalized checkpoint at genesis is a zero hash. genesisCheckpoint := genesisState.FinalizedCheckpoint() - s.justifiedCheckpt = stateV0.CopyCheckpoint(genesisCheckpoint) + s.justifiedCheckpt = blockutil.CopyCheckpoint(genesisCheckpoint) if err := s.cacheJustifiedStateBalances(ctx, genesisBlkRoot); err != nil { return err } - s.prevJustifiedCheckpt = stateV0.CopyCheckpoint(genesisCheckpoint) - s.bestJustifiedCheckpt = stateV0.CopyCheckpoint(genesisCheckpoint) - s.finalizedCheckpt = stateV0.CopyCheckpoint(genesisCheckpoint) - s.prevFinalizedCheckpt = stateV0.CopyCheckpoint(genesisCheckpoint) + s.prevJustifiedCheckpt = blockutil.CopyCheckpoint(genesisCheckpoint) + s.bestJustifiedCheckpt = blockutil.CopyCheckpoint(genesisCheckpoint) + s.finalizedCheckpt = blockutil.CopyCheckpoint(genesisCheckpoint) + s.prevFinalizedCheckpt = blockutil.CopyCheckpoint(genesisCheckpoint) if err := s.cfg.ForkChoiceStore.ProcessBlock(ctx, genesisBlk.Block.Slot, @@ -411,7 +414,7 @@ func (s *Service) initializeChainInfo(ctx context.Context) error { if err != nil { return errors.Wrap(err, "could not retrieve head block") } - headEpoch := helpers.SlotToEpoch(headBlock.Block.Slot) + headEpoch := helpers.SlotToEpoch(headBlock.Block().Slot()) var epochsSinceFinality types.Epoch if headEpoch > finalized.Epoch { epochsSinceFinality = headEpoch - finalized.Epoch @@ -419,7 +422,7 @@ func (s *Service) initializeChainInfo(ctx context.Context) error { // Head sync when node is far enough beyond known finalized epoch, // this becomes really useful during long period of non-finality. if epochsSinceFinality >= headSyncMinEpochsAfterCheckpoint { - headRoot, err := headBlock.Block.HashTreeRoot() + headRoot, err := headBlock.Block().HashTreeRoot() if err != nil { return errors.Wrap(err, "could not hash head block") } diff --git a/beacon-chain/cache/BUILD.bazel b/beacon-chain/cache/BUILD.bazel index 54af4fd4060a..e1965858ba94 100644 --- a/beacon-chain/cache/BUILD.bazel +++ b/beacon-chain/cache/BUILD.bazel @@ -32,7 +32,7 @@ go_library( ], deps = [ "//beacon-chain/state/interface:go_default_library", - "//beacon-chain/state/stateV0:go_default_library", + "//shared/blockutil:go_default_library", "//shared/hashutil:go_default_library", "//shared/params:go_default_library", "//shared/sliceutil:go_default_library", diff --git a/beacon-chain/cache/attestation_data.go b/beacon-chain/cache/attestation_data.go index 039df2e9a390..849bc3b1dd0a 100644 --- a/beacon-chain/cache/attestation_data.go +++ b/beacon-chain/cache/attestation_data.go @@ -8,10 +8,11 @@ import ( "sync" "time" + "github.com/prysmaticlabs/prysm/shared/blockutil" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "k8s.io/client-go/tools/cache" ) @@ -98,7 +99,7 @@ func (c *AttestationCache) Get(ctx context.Context, req *ethpb.AttestationDataRe if exists && item != nil && item.(*attestationReqResWrapper).res != nil { attestationCacheHit.Inc() - return stateV0.CopyAttestationData(item.(*attestationReqResWrapper).res), nil + return blockutil.CopyAttestationData(item.(*attestationReqResWrapper).res), nil } attestationCacheMiss.Inc() return nil, nil diff --git a/beacon-chain/core/blocks/BUILD.bazel b/beacon-chain/core/blocks/BUILD.bazel index 7ffb969140ec..12674b25cd55 100644 --- a/beacon-chain/core/blocks/BUILD.bazel +++ b/beacon-chain/core/blocks/BUILD.bazel @@ -28,9 +28,9 @@ go_library( "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/validators:go_default_library", "//beacon-chain/state/interface:go_default_library", - "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/attestationutil:go_default_library", + "//shared/blockutil:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", "//shared/depositutil:go_default_library", @@ -80,6 +80,7 @@ go_test( "//shared/aggregation:go_default_library", "//shared/aggregation/attestations:go_default_library", "//shared/attestationutil:go_default_library", + "//shared/blockutil:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", "//shared/params:go_default_library", diff --git a/beacon-chain/core/blocks/attestation.go b/beacon-chain/core/blocks/attestation.go index 34ba477c9158..ba9f4e78aad3 100644 --- a/beacon-chain/core/blocks/attestation.go +++ b/beacon-chain/core/blocks/attestation.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -21,14 +23,14 @@ import ( func ProcessAttestations( ctx context.Context, beaconState iface.BeaconState, - b *ethpb.SignedBeaconBlock, + b interfaces.SignedBeaconBlock, ) (iface.BeaconState, error) { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return nil, err } var err error - for idx, attestation := range b.Block.Body.Attestations { + for idx, attestation := range b.Block().Body().Attestations() { beaconState, err = ProcessAttestation(ctx, beaconState, attestation) if err != nil { return nil, errors.Wrapf(err, "could not verify attestation at index %d in block", idx) @@ -83,14 +85,14 @@ func ProcessAttestation( func ProcessAttestationsNoVerifySignature( ctx context.Context, beaconState iface.BeaconState, - b *ethpb.SignedBeaconBlock, + b interfaces.SignedBeaconBlock, ) (iface.BeaconState, error) { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return nil, err } - body := b.Block.Body + body := b.Block().Body() var err error - for idx, attestation := range body.Attestations { + for idx, attestation := range body.Attestations() { beaconState, err = ProcessAttestationNoVerifySignature(ctx, beaconState, attestation) if err != nil { return nil, errors.Wrapf(err, "could not verify attestation at index %d in block", idx) diff --git a/beacon-chain/core/blocks/eth1_data.go b/beacon-chain/core/blocks/eth1_data.go index 09dcc1357049..4bfd5e78de3c 100644 --- a/beacon-chain/core/blocks/eth1_data.go +++ b/beacon-chain/core/blocks/eth1_data.go @@ -5,9 +5,10 @@ import ( "context" "errors" + "github.com/prysmaticlabs/prysm/shared/blockutil" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "github.com/prysmaticlabs/prysm/shared/params" ) @@ -58,7 +59,7 @@ func AreEth1DataEqual(a, b *ethpb.Eth1Data) bool { // votes to see if they match the eth1data. func Eth1DataHasEnoughSupport(beaconState iface.ReadOnlyBeaconState, data *ethpb.Eth1Data) (bool, error) { voteCount := uint64(0) - data = stateV0.CopyETH1Data(data) + data = blockutil.CopyETH1Data(data) for _, vote := range beaconState.Eth1DataVotes() { if AreEth1DataEqual(vote, data) { diff --git a/beacon-chain/core/blocks/eth1_data_test.go b/beacon-chain/core/blocks/eth1_data_test.go index fefe0d7766c8..2a2facba2d04 100644 --- a/beacon-chain/core/blocks/eth1_data_test.go +++ b/beacon-chain/core/blocks/eth1_data_test.go @@ -5,6 +5,8 @@ import ( "fmt" "testing" + "github.com/prysmaticlabs/prysm/shared/blockutil" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" @@ -188,7 +190,7 @@ func TestProcessEth1Data_SetsCorrectly(t *testing.T) { if len(newETH1DataVotes) <= 1 { t.Error("Expected new ETH1 data votes to have length > 1") } - if !proto.Equal(beaconState.Eth1Data(), stateV0.CopyETH1Data(b.Block.Body.Eth1Data)) { + if !proto.Equal(beaconState.Eth1Data(), blockutil.CopyETH1Data(b.Block.Body.Eth1Data)) { t.Errorf( "Expected latest eth1 data to have been set to %v, received %v", b.Block.Body.Eth1Data, diff --git a/beacon-chain/core/blocks/header.go b/beacon-chain/core/blocks/header.go index 1d94707eff08..6b89b9507e42 100644 --- a/beacon-chain/core/blocks/header.go +++ b/beacon-chain/core/blocks/header.go @@ -5,6 +5,8 @@ import ( "context" "fmt" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" @@ -40,22 +42,22 @@ import ( func ProcessBlockHeader( _ context.Context, beaconState iface.BeaconState, - block *ethpb.SignedBeaconBlock, + block interfaces.SignedBeaconBlock, ) (iface.BeaconState, error) { if err := helpers.VerifyNilBeaconBlock(block); err != nil { return nil, err } - bodyRoot, err := block.Block.Body.HashTreeRoot() + bodyRoot, err := block.Block().Body().HashTreeRoot() if err != nil { return nil, err } - beaconState, err = ProcessBlockHeaderNoVerify(beaconState, block.Block.Slot, block.Block.ProposerIndex, block.Block.ParentRoot, bodyRoot[:]) + beaconState, err = ProcessBlockHeaderNoVerify(beaconState, block.Block().Slot(), block.Block().ProposerIndex(), block.Block().ParentRoot(), bodyRoot[:]) if err != nil { return nil, err } // Verify proposer signature. - if err := VerifyBlockSignature(beaconState, block.Block.ProposerIndex, block.Signature, block.Block.HashTreeRoot); err != nil { + if err := VerifyBlockSignature(beaconState, block.Block().ProposerIndex(), block.Signature(), block.Block().HashTreeRoot); err != nil { return nil, err } diff --git a/beacon-chain/core/blocks/randao.go b/beacon-chain/core/blocks/randao.go index 7ce53dc0b7e2..b2ff50858303 100644 --- a/beacon-chain/core/blocks/randao.go +++ b/beacon-chain/core/blocks/randao.go @@ -3,8 +3,9 @@ package blocks import ( "context" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/hashutil" @@ -28,21 +29,21 @@ import ( func ProcessRandao( _ context.Context, beaconState iface.BeaconState, - b *ethpb.SignedBeaconBlock, + b interfaces.SignedBeaconBlock, ) (iface.BeaconState, error) { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return nil, err } - body := b.Block.Body + body := b.Block().Body() buf, proposerPub, domain, err := randaoSigningData(beaconState) if err != nil { return nil, err } - if err := verifySignature(buf, proposerPub, body.RandaoReveal, domain); err != nil { + if err := verifySignature(buf, proposerPub, body.RandaoReveal(), domain); err != nil { return nil, errors.Wrap(err, "could not verify block randao") } - beaconState, err = ProcessRandaoNoVerify(beaconState, body.RandaoReveal) + beaconState, err = ProcessRandaoNoVerify(beaconState, body.RandaoReveal()) if err != nil { return nil, errors.Wrap(err, "could not process randao") } diff --git a/beacon-chain/core/epoch/BUILD.bazel b/beacon-chain/core/epoch/BUILD.bazel index a54db900a1fb..a0c72f9bddba 100644 --- a/beacon-chain/core/epoch/BUILD.bazel +++ b/beacon-chain/core/epoch/BUILD.bazel @@ -13,9 +13,9 @@ go_library( "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/validators:go_default_library", "//beacon-chain/state/interface:go_default_library", - "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/attestationutil:go_default_library", + "//shared/blockutil:go_default_library", "//shared/featureconfig:go_default_library", "//shared/mathutil:go_default_library", "//shared/params:go_default_library", diff --git a/beacon-chain/core/epoch/epoch_processing.go b/beacon-chain/core/epoch/epoch_processing.go index d16bc6163cf4..c9ca6ceb63f7 100644 --- a/beacon-chain/core/epoch/epoch_processing.go +++ b/beacon-chain/core/epoch/epoch_processing.go @@ -8,13 +8,14 @@ import ( "fmt" "sort" + "github.com/prysmaticlabs/prysm/shared/blockutil" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" @@ -258,7 +259,7 @@ func ProcessEffectiveBalanceUpdates(state iface.BeaconState) (iface.BeaconState, balance := bals[idx] if balance+downwardThreshold < val.EffectiveBalance || val.EffectiveBalance+upwardThreshold < balance { - newVal := stateV0.CopyValidator(val) + newVal := blockutil.CopyValidator(val) newVal.EffectiveBalance = maxEffBalance if newVal.EffectiveBalance > balance-balance%effBalanceInc { newVal.EffectiveBalance = balance - balance%effBalanceInc @@ -284,7 +285,7 @@ func ProcessEffectiveBalanceUpdates(state iface.BeaconState) (iface.BeaconState, effectiveBal = balance - balance%effBalanceInc } if effectiveBal != val.EffectiveBalance { - newVal := stateV0.CopyValidator(val) + newVal := blockutil.CopyValidator(val) newVal.EffectiveBalance = effectiveBal return true, newVal, nil } diff --git a/beacon-chain/core/feed/state/events.go b/beacon-chain/core/feed/state/events.go index ac3214b7334a..ab9a97980e61 100644 --- a/beacon-chain/core/feed/state/events.go +++ b/beacon-chain/core/feed/state/events.go @@ -6,8 +6,9 @@ package state import ( "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" ) const ( @@ -31,7 +32,7 @@ type BlockProcessedData struct { // BlockRoot of the processed block. BlockRoot [32]byte // SignedBlock is the physical processed block. - SignedBlock *ethpb.SignedBeaconBlock + SignedBlock interfaces.SignedBeaconBlock // Verified is true if the block's BLS contents have been verified. Verified bool } diff --git a/beacon-chain/core/helpers/block.go b/beacon-chain/core/helpers/block.go index f137c85c7357..4dff27f8030d 100644 --- a/beacon-chain/core/helpers/block.go +++ b/beacon-chain/core/helpers/block.go @@ -3,9 +3,10 @@ package helpers import ( "math" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/params" ) @@ -13,14 +14,14 @@ import ( // VerifyNilBeaconBlock checks if any composite field of input signed beacon block is nil. // Access to these nil fields will result in run time panic, // it is recommended to run these checks as first line of defense. -func VerifyNilBeaconBlock(b *ethpb.SignedBeaconBlock) error { - if b == nil { +func VerifyNilBeaconBlock(b interfaces.SignedBeaconBlock) error { + if b.IsNil() { return errors.New("signed beacon block can't be nil") } - if b.Block == nil { + if b.Block().IsNil() { return errors.New("beacon block can't be nil") } - if b.Block.Body == nil { + if b.Block().Body().IsNil() { return errors.New("beacon block body can't be nil") } return nil diff --git a/beacon-chain/core/state/interop/write_block_to_disk.go b/beacon-chain/core/state/interop/write_block_to_disk.go index 63a194652bec..aac9c6590088 100644 --- a/beacon-chain/core/state/interop/write_block_to_disk.go +++ b/beacon-chain/core/state/interop/write_block_to_disk.go @@ -5,18 +5,19 @@ import ( "os" "path" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/fileutil" ) // WriteBlockToDisk as a block ssz. Writes to temp directory. Debug! -func WriteBlockToDisk(block *ethpb.SignedBeaconBlock, failed bool) { +func WriteBlockToDisk(block interfaces.SignedBeaconBlock, failed bool) { if !featureconfig.Get().WriteSSZStateTransitions { return } - filename := fmt.Sprintf("beacon_block_%d.ssz", block.Block.Slot) + filename := fmt.Sprintf("beacon_block_%d.ssz", block.Block().Slot()) if failed { filename = "failed_" + filename } diff --git a/beacon-chain/core/state/transition.go b/beacon-chain/core/state/transition.go index d6b7be3e7706..e2656510b047 100644 --- a/beacon-chain/core/state/transition.go +++ b/beacon-chain/core/state/transition.go @@ -8,9 +8,10 @@ import ( "context" "fmt" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/cache" b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" e "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch" @@ -25,25 +26,25 @@ import ( ) // processFunc is a function that processes a block with a given state. State is mutated. -type processFunc func(context.Context, iface.BeaconState, *ethpb.SignedBeaconBlock) (iface.BeaconState, error) +type processFunc func(context.Context, iface.BeaconState, interfaces.SignedBeaconBlock) (iface.BeaconState, error) -var processDepositsFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { - return b.ProcessDeposits(ctx, s, blk.Block.Body.Deposits) +var processDepositsFunc = func(ctx context.Context, s iface.BeaconState, blk interfaces.SignedBeaconBlock) (iface.BeaconState, error) { + return b.ProcessDeposits(ctx, s, blk.Block().Body().Deposits()) } -var processProposerSlashingFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { - return b.ProcessProposerSlashings(ctx, s, blk.Block.Body.ProposerSlashings, v.SlashValidator) +var processProposerSlashingFunc = func(ctx context.Context, s iface.BeaconState, blk interfaces.SignedBeaconBlock) (iface.BeaconState, error) { + return b.ProcessProposerSlashings(ctx, s, blk.Block().Body().ProposerSlashings(), v.SlashValidator) } -var processAttesterSlashingFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { - return b.ProcessAttesterSlashings(ctx, s, blk.Block.Body.AttesterSlashings, v.SlashValidator) +var processAttesterSlashingFunc = func(ctx context.Context, s iface.BeaconState, blk interfaces.SignedBeaconBlock) (iface.BeaconState, error) { + return b.ProcessAttesterSlashings(ctx, s, blk.Block().Body().AttesterSlashings(), v.SlashValidator) } -var processEth1DataFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { - return b.ProcessEth1DataInBlock(ctx, s, blk.Block.Body.Eth1Data) +var processEth1DataFunc = func(ctx context.Context, s iface.BeaconState, blk interfaces.SignedBeaconBlock) (iface.BeaconState, error) { + return b.ProcessEth1DataInBlock(ctx, s, blk.Block().Body().Eth1Data()) } -var processExitFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { - return b.ProcessVoluntaryExits(ctx, s, blk.Block.Body.VoluntaryExits) +var processExitFunc = func(ctx context.Context, s iface.BeaconState, blk interfaces.SignedBeaconBlock) (iface.BeaconState, error) { + return b.ProcessVoluntaryExits(ctx, s, blk.Block().Body().VoluntaryExits()) } // This defines the processing block routine as outlined in eth2 spec: @@ -81,12 +82,12 @@ var processingPipeline = []processFunc{ func ExecuteStateTransition( ctx context.Context, state iface.BeaconState, - signed *ethpb.SignedBeaconBlock, + signed interfaces.SignedBeaconBlock, ) (iface.BeaconState, error) { if ctx.Err() != nil { return nil, ctx.Err() } - if signed == nil || signed.Block == nil { + if signed.IsNil() || signed.Block().IsNil() { return nil, errors.New("nil block") } @@ -308,7 +309,7 @@ func ProcessSlots(ctx context.Context, state iface.BeaconState, slot types.Slot) func ProcessBlock( ctx context.Context, state iface.BeaconState, - signed *ethpb.SignedBeaconBlock, + signed interfaces.SignedBeaconBlock, ) (iface.BeaconState, error) { ctx, span := trace.StartSpan(ctx, "core.state.ProcessBlock") defer span.End() @@ -329,40 +330,40 @@ func ProcessBlock( } // VerifyOperationLengths verifies that block operation lengths are valid. -func VerifyOperationLengths(_ context.Context, state iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { +func VerifyOperationLengths(_ context.Context, state iface.BeaconState, b interfaces.SignedBeaconBlock) (iface.BeaconState, error) { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return nil, err } - body := b.Block.Body + body := b.Block().Body() - if uint64(len(body.ProposerSlashings)) > params.BeaconConfig().MaxProposerSlashings { + if uint64(len(body.ProposerSlashings())) > params.BeaconConfig().MaxProposerSlashings { return nil, fmt.Errorf( "number of proposer slashings (%d) in block body exceeds allowed threshold of %d", - len(body.ProposerSlashings), + len(body.ProposerSlashings()), params.BeaconConfig().MaxProposerSlashings, ) } - if uint64(len(body.AttesterSlashings)) > params.BeaconConfig().MaxAttesterSlashings { + if uint64(len(body.AttesterSlashings())) > params.BeaconConfig().MaxAttesterSlashings { return nil, fmt.Errorf( "number of attester slashings (%d) in block body exceeds allowed threshold of %d", - len(body.AttesterSlashings), + len(body.AttesterSlashings()), params.BeaconConfig().MaxAttesterSlashings, ) } - if uint64(len(body.Attestations)) > params.BeaconConfig().MaxAttestations { + if uint64(len(body.Attestations())) > params.BeaconConfig().MaxAttestations { return nil, fmt.Errorf( "number of attestations (%d) in block body exceeds allowed threshold of %d", - len(body.Attestations), + len(body.Attestations()), params.BeaconConfig().MaxAttestations, ) } - if uint64(len(body.VoluntaryExits)) > params.BeaconConfig().MaxVoluntaryExits { + if uint64(len(body.VoluntaryExits())) > params.BeaconConfig().MaxVoluntaryExits { return nil, fmt.Errorf( "number of voluntary exits (%d) in block body exceeds allowed threshold of %d", - len(body.VoluntaryExits), + len(body.VoluntaryExits()), params.BeaconConfig().MaxVoluntaryExits, ) } @@ -375,9 +376,9 @@ func VerifyOperationLengths(_ context.Context, state iface.BeaconState, b *ethpb } maxDeposits := mathutil.Min(params.BeaconConfig().MaxDeposits, eth1Data.DepositCount-state.Eth1DepositIndex()) // Verify outstanding deposits are processed up to max number of deposits - if uint64(len(body.Deposits)) != maxDeposits { + if uint64(len(body.Deposits())) != maxDeposits { return nil, fmt.Errorf("incorrect outstanding deposits in block body, wanted: %d, got: %d", - maxDeposits, len(body.Deposits)) + maxDeposits, len(body.Deposits())) } return state, nil diff --git a/beacon-chain/core/state/transition_no_verify_sig.go b/beacon-chain/core/state/transition_no_verify_sig.go index bbec55d2edb0..257fbb58dba5 100644 --- a/beacon-chain/core/state/transition_no_verify_sig.go +++ b/beacon-chain/core/state/transition_no_verify_sig.go @@ -5,8 +5,9 @@ import ( "context" "fmt" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state/interop" @@ -42,12 +43,12 @@ import ( func ExecuteStateTransitionNoVerifyAnySig( ctx context.Context, state iface.BeaconState, - signed *ethpb.SignedBeaconBlock, + signed interfaces.SignedBeaconBlock, ) (*bls.SignatureSet, iface.BeaconState, error) { if ctx.Err() != nil { return nil, nil, ctx.Err() } - if signed == nil || signed.Block == nil { + if signed.IsNil() || signed.Block().IsNil() { return nil, nil, errors.New("nil block") } @@ -59,12 +60,12 @@ func ExecuteStateTransitionNoVerifyAnySig( interop.WriteStateToDisk(state) if featureconfig.Get().EnableNextSlotStateCache { - state, err = ProcessSlotsUsingNextSlotCache(ctx, state, signed.Block.ParentRoot, signed.Block.Slot) + state, err = ProcessSlotsUsingNextSlotCache(ctx, state, signed.Block().ParentRoot(), signed.Block().Slot()) if err != nil { return nil, nil, errors.Wrap(err, "could not process slots") } } else { - state, err = ProcessSlots(ctx, state, signed.Block.Slot) + state, err = ProcessSlots(ctx, state, signed.Block().Slot()) if err != nil { return nil, nil, errors.Wrap(err, "could not process slot") } @@ -81,9 +82,9 @@ func ExecuteStateTransitionNoVerifyAnySig( if err != nil { return nil, nil, err } - if !bytes.Equal(postStateRoot[:], signed.Block.StateRoot) { + if !bytes.Equal(postStateRoot[:], signed.Block().StateRoot()) { return nil, nil, fmt.Errorf("could not validate state root, wanted: %#x, received: %#x", - postStateRoot[:], signed.Block.StateRoot) + postStateRoot[:], signed.Block().StateRoot()) } return set, state, nil @@ -113,7 +114,7 @@ func ExecuteStateTransitionNoVerifyAnySig( func CalculateStateRoot( ctx context.Context, state iface.BeaconState, - signed *ethpb.SignedBeaconBlock, + signed interfaces.SignedBeaconBlock, ) ([32]byte, error) { ctx, span := trace.StartSpan(ctx, "core.state.CalculateStateRoot") defer span.End() @@ -124,7 +125,7 @@ func CalculateStateRoot( if state == nil { return [32]byte{}, errors.New("nil state") } - if signed == nil || signed.Block == nil { + if signed.IsNil() || signed.Block().IsNil() { return [32]byte{}, errors.New("nil block") } @@ -134,12 +135,12 @@ func CalculateStateRoot( // Execute per slots transition. var err error if featureconfig.Get().EnableNextSlotStateCache { - state, err = ProcessSlotsUsingNextSlotCache(ctx, state, signed.Block.ParentRoot, signed.Block.Slot) + state, err = ProcessSlotsUsingNextSlotCache(ctx, state, signed.Block().ParentRoot(), signed.Block().Slot()) if err != nil { return [32]byte{}, errors.Wrap(err, "could not process slots") } } else { - state, err = ProcessSlots(ctx, state, signed.Block.Slot) + state, err = ProcessSlots(ctx, state, signed.Block().Slot()) if err != nil { return [32]byte{}, errors.Wrap(err, "could not process slot") } @@ -169,7 +170,7 @@ func CalculateStateRoot( func ProcessBlockNoVerifyAnySig( ctx context.Context, state iface.BeaconState, - signed *ethpb.SignedBeaconBlock, + signed interfaces.SignedBeaconBlock, ) (*bls.SignatureSet, iface.BeaconState, error) { ctx, span := trace.StartSpan(ctx, "core.state.ProcessBlockNoVerifyAnySig") defer span.End() @@ -177,23 +178,23 @@ func ProcessBlockNoVerifyAnySig( return nil, nil, err } - blk := signed.Block + blk := signed.Block() state, err := ProcessBlockForStateRoot(ctx, state, signed) if err != nil { return nil, nil, err } - bSet, err := b.BlockSignatureSet(state, blk.ProposerIndex, signed.Signature, blk.HashTreeRoot) + bSet, err := b.BlockSignatureSet(state, blk.ProposerIndex(), signed.Signature(), blk.HashTreeRoot) if err != nil { traceutil.AnnotateError(span, err) return nil, nil, errors.Wrap(err, "could not retrieve block signature set") } - rSet, err := b.RandaoSignatureSet(state, signed.Block.Body.RandaoReveal) + rSet, err := b.RandaoSignatureSet(state, signed.Block().Body().RandaoReveal()) if err != nil { traceutil.AnnotateError(span, err) return nil, nil, errors.Wrap(err, "could not retrieve randao signature set") } - aSet, err := b.AttestationSignatureSet(ctx, state, signed.Block.Body.Attestations) + aSet, err := b.AttestationSignatureSet(ctx, state, signed.Block().Body().Attestations()) if err != nil { return nil, nil, errors.Wrap(err, "could not retrieve attestation signature set") } @@ -229,7 +230,7 @@ func ProcessBlockNoVerifyAnySig( func ProcessOperationsNoVerifyAttsSigs( ctx context.Context, state iface.BeaconState, - signedBeaconBlock *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { + signedBeaconBlock interfaces.SignedBeaconBlock) (iface.BeaconState, error) { ctx, span := trace.StartSpan(ctx, "core.state.ProcessOperationsNoVerifyAttsSigs") defer span.End() if err := helpers.VerifyNilBeaconBlock(signedBeaconBlock); err != nil { @@ -240,11 +241,11 @@ func ProcessOperationsNoVerifyAttsSigs( return nil, errors.Wrap(err, "could not verify operation lengths") } - state, err := b.ProcessProposerSlashings(ctx, state, signedBeaconBlock.Block.Body.ProposerSlashings, v.SlashValidator) + state, err := b.ProcessProposerSlashings(ctx, state, signedBeaconBlock.Block().Body().ProposerSlashings(), v.SlashValidator) if err != nil { return nil, errors.Wrap(err, "could not process block proposer slashings") } - state, err = b.ProcessAttesterSlashings(ctx, state, signedBeaconBlock.Block.Body.AttesterSlashings, v.SlashValidator) + state, err = b.ProcessAttesterSlashings(ctx, state, signedBeaconBlock.Block().Body().AttesterSlashings(), v.SlashValidator) if err != nil { return nil, errors.Wrap(err, "could not process block attester slashings") } @@ -252,11 +253,11 @@ func ProcessOperationsNoVerifyAttsSigs( if err != nil { return nil, errors.Wrap(err, "could not process block attestations") } - state, err = b.ProcessDeposits(ctx, state, signedBeaconBlock.Block.Body.Deposits) + state, err = b.ProcessDeposits(ctx, state, signedBeaconBlock.Block().Body().Deposits()) if err != nil { return nil, errors.Wrap(err, "could not process block validator deposits") } - state, err = b.ProcessVoluntaryExits(ctx, state, signedBeaconBlock.Block.Body.VoluntaryExits) + state, err = b.ProcessVoluntaryExits(ctx, state, signedBeaconBlock.Block().Body().VoluntaryExits()) if err != nil { return nil, errors.Wrap(err, "could not process validator exits") } @@ -269,7 +270,7 @@ func ProcessOperationsNoVerifyAttsSigs( func ProcessBlockForStateRoot( ctx context.Context, state iface.BeaconState, - signed *ethpb.SignedBeaconBlock, + signed interfaces.SignedBeaconBlock, ) (iface.BeaconState, error) { ctx, span := trace.StartSpan(ctx, "core.state.ProcessBlockForStateRoot") defer span.End() @@ -277,25 +278,25 @@ func ProcessBlockForStateRoot( return nil, err } - blk := signed.Block - body := blk.Body + blk := signed.Block() + body := blk.Body() bodyRoot, err := body.HashTreeRoot() if err != nil { return nil, err } - state, err = b.ProcessBlockHeaderNoVerify(state, blk.Slot, blk.ProposerIndex, blk.ParentRoot, bodyRoot[:]) + state, err = b.ProcessBlockHeaderNoVerify(state, blk.Slot(), blk.ProposerIndex(), blk.ParentRoot(), bodyRoot[:]) if err != nil { traceutil.AnnotateError(span, err) return nil, errors.Wrap(err, "could not process block header") } - state, err = b.ProcessRandaoNoVerify(state, signed.Block.Body.RandaoReveal) + state, err = b.ProcessRandaoNoVerify(state, signed.Block().Body().RandaoReveal()) if err != nil { traceutil.AnnotateError(span, err) return nil, errors.Wrap(err, "could not verify and process randao") } - state, err = b.ProcessEth1DataInBlock(ctx, state, signed.Block.Body.Eth1Data) + state, err = b.ProcessEth1DataInBlock(ctx, state, signed.Block().Body().Eth1Data()) if err != nil { traceutil.AnnotateError(span, err) return nil, errors.Wrap(err, "could not process eth1 data") diff --git a/beacon-chain/db/iface/BUILD.bazel b/beacon-chain/db/iface/BUILD.bazel index 2e4ec55d6cce..3fb69543e91e 100644 --- a/beacon-chain/db/iface/BUILD.bazel +++ b/beacon-chain/db/iface/BUILD.bazel @@ -11,6 +11,7 @@ go_library( visibility = ["//beacon-chain/db:__subpackages__"], deps = [ "//beacon-chain/db/filters:go_default_library", + "//beacon-chain/interfaces:go_default_library", "//beacon-chain/slasher/types:go_default_library", "//beacon-chain/state/interface:go_default_library", "//proto/beacon/db:go_default_library", diff --git a/beacon-chain/db/iface/interface.go b/beacon-chain/db/iface/interface.go index 22ed0392d278..cbf7d89f1342 100644 --- a/beacon-chain/db/iface/interface.go +++ b/beacon-chain/db/iface/interface.go @@ -16,13 +16,14 @@ import ( "github.com/prysmaticlabs/prysm/proto/beacon/db" ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/backuputil" + "github.com/prysmaticlabs/prysm/shared/interfaces" ) // ReadOnlyDatabase defines a struct which only has read access to database methods. type ReadOnlyDatabase interface { // Block related methods. - Block(ctx context.Context, blockRoot [32]byte) (*eth.SignedBeaconBlock, error) - Blocks(ctx context.Context, f *filters.QueryFilter) ([]*eth.SignedBeaconBlock, [][32]byte, error) + Block(ctx context.Context, blockRoot [32]byte) (interfaces.SignedBeaconBlock, error) + Blocks(ctx context.Context, f *filters.QueryFilter) ([]interfaces.SignedBeaconBlock, [][32]byte, error) BlockRoots(ctx context.Context, f *filters.QueryFilter) ([][32]byte, error) BlocksBySlot(ctx context.Context, slot types.Slot) (bool, []*eth.SignedBeaconBlock, error) BlockRootsBySlot(ctx context.Context, slot types.Slot) (bool, [][32]byte, error) @@ -64,8 +65,8 @@ type NoHeadAccessDatabase interface { ReadOnlyDatabase // Block related methods. - SaveBlock(ctx context.Context, block *eth.SignedBeaconBlock) error - SaveBlocks(ctx context.Context, blocks []*eth.SignedBeaconBlock) error + SaveBlock(ctx context.Context, block interfaces.SignedBeaconBlock) error + SaveBlocks(ctx context.Context, blocks []interfaces.SignedBeaconBlock) error SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error // State related methods. SaveState(ctx context.Context, state iface.ReadOnlyBeaconState, blockRoot [32]byte) error @@ -97,7 +98,7 @@ type HeadAccessDatabase interface { NoHeadAccessDatabase // Block related methods. - HeadBlock(ctx context.Context) (*eth.SignedBeaconBlock, error) + HeadBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) error // Genesis operations. diff --git a/beacon-chain/operations/attestations/BUILD.bazel b/beacon-chain/operations/attestations/BUILD.bazel index 580c3f946a57..0014436cbc7a 100644 --- a/beacon-chain/operations/attestations/BUILD.bazel +++ b/beacon-chain/operations/attestations/BUILD.bazel @@ -19,8 +19,8 @@ go_library( ], deps = [ "//beacon-chain/operations/attestations/kv:go_default_library", - "//beacon-chain/state/stateV0:go_default_library", "//shared/aggregation/attestations:go_default_library", + "//shared/blockutil:go_default_library", "//shared/hashutil:go_default_library", "//shared/params:go_default_library", "//shared/slotutil:go_default_library", diff --git a/beacon-chain/operations/attestations/kv/BUILD.bazel b/beacon-chain/operations/attestations/kv/BUILD.bazel index 1fb94a8c9ba9..af1cc89a9faf 100644 --- a/beacon-chain/operations/attestations/kv/BUILD.bazel +++ b/beacon-chain/operations/attestations/kv/BUILD.bazel @@ -15,8 +15,8 @@ go_library( visibility = ["//beacon-chain:__subpackages__"], deps = [ "//beacon-chain/core/helpers:go_default_library", - "//beacon-chain/state/stateV0:go_default_library", "//shared/aggregation/attestations:go_default_library", + "//shared/blockutil:go_default_library", "//shared/hashutil:go_default_library", "//shared/params:go_default_library", "@com_github_patrickmn_go_cache//:go_default_library", diff --git a/beacon-chain/operations/attestations/kv/aggregated.go b/beacon-chain/operations/attestations/kv/aggregated.go index 46e4c6f295d0..cacbd29431a8 100644 --- a/beacon-chain/operations/attestations/kv/aggregated.go +++ b/beacon-chain/operations/attestations/kv/aggregated.go @@ -3,11 +3,12 @@ package kv import ( "context" + "github.com/prysmaticlabs/prysm/shared/blockutil" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" attaggregation "github.com/prysmaticlabs/prysm/shared/aggregation/attestations" log "github.com/sirupsen/logrus" "go.opencensus.io/trace" @@ -120,7 +121,7 @@ func (c *AttCaches) SaveAggregatedAttestation(att *ethpb.Attestation) error { if err != nil { return errors.Wrap(err, "could not tree hash attestation") } - copiedAtt := stateV0.CopyAttestation(att) + copiedAtt := blockutil.CopyAttestation(att) c.aggregatedAttLock.Lock() defer c.aggregatedAttLock.Unlock() atts, ok := c.aggregatedAtt[r] diff --git a/beacon-chain/operations/attestations/kv/block.go b/beacon-chain/operations/attestations/kv/block.go index ec220c922912..a6e5c6c0d029 100644 --- a/beacon-chain/operations/attestations/kv/block.go +++ b/beacon-chain/operations/attestations/kv/block.go @@ -3,7 +3,7 @@ package kv import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" + "github.com/prysmaticlabs/prysm/shared/blockutil" ) // SaveBlockAttestation saves an block attestation in cache. @@ -30,7 +30,7 @@ func (c *AttCaches) SaveBlockAttestation(att *ethpb.Attestation) error { } } - c.blockAtt[r] = append(atts, stateV0.CopyAttestation(att)) + c.blockAtt[r] = append(atts, blockutil.CopyAttestation(att)) return nil } diff --git a/beacon-chain/operations/attestations/kv/forkchoice.go b/beacon-chain/operations/attestations/kv/forkchoice.go index 5314947cd6cb..598bea6bb400 100644 --- a/beacon-chain/operations/attestations/kv/forkchoice.go +++ b/beacon-chain/operations/attestations/kv/forkchoice.go @@ -3,7 +3,7 @@ package kv import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" + "github.com/prysmaticlabs/prysm/shared/blockutil" ) // SaveForkchoiceAttestation saves an forkchoice attestation in cache. @@ -16,7 +16,7 @@ func (c *AttCaches) SaveForkchoiceAttestation(att *ethpb.Attestation) error { return errors.Wrap(err, "could not tree hash attestation") } - att = stateV0.CopyAttestation(att) + att = blockutil.CopyAttestation(att) c.forkchoiceAttLock.Lock() defer c.forkchoiceAttLock.Unlock() c.forkchoiceAtt[r] = att @@ -42,7 +42,7 @@ func (c *AttCaches) ForkchoiceAttestations() []*ethpb.Attestation { atts := make([]*ethpb.Attestation, 0, len(c.forkchoiceAtt)) for _, att := range c.forkchoiceAtt { - atts = append(atts, stateV0.CopyAttestation(att) /* Copied */) + atts = append(atts, blockutil.CopyAttestation(att) /* Copied */) } return atts diff --git a/beacon-chain/operations/attestations/kv/unaggregated.go b/beacon-chain/operations/attestations/kv/unaggregated.go index 5185f111ac76..c9cbb7e64f48 100644 --- a/beacon-chain/operations/attestations/kv/unaggregated.go +++ b/beacon-chain/operations/attestations/kv/unaggregated.go @@ -3,11 +3,12 @@ package kv import ( "context" + "github.com/prysmaticlabs/prysm/shared/blockutil" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "go.opencensus.io/trace" ) @@ -32,7 +33,7 @@ func (c *AttCaches) SaveUnaggregatedAttestation(att *ethpb.Attestation) error { if err != nil { return errors.Wrap(err, "could not tree hash attestation") } - att = stateV0.CopyAttestation(att) // Copied. + att = blockutil.CopyAttestation(att) // Copied. c.unAggregateAttLock.Lock() defer c.unAggregateAttLock.Unlock() c.unAggregatedAtt[r] = att @@ -63,7 +64,7 @@ func (c *AttCaches) UnaggregatedAttestations() ([]*ethpb.Attestation, error) { return nil, err } if !seen { - atts = append(atts, stateV0.CopyAttestation(att) /* Copied */) + atts = append(atts, blockutil.CopyAttestation(att) /* Copied */) } } return atts, nil diff --git a/beacon-chain/operations/attestations/prepare_forkchoice.go b/beacon-chain/operations/attestations/prepare_forkchoice.go index 86365d0d7aef..9182b4d43dba 100644 --- a/beacon-chain/operations/attestations/prepare_forkchoice.go +++ b/beacon-chain/operations/attestations/prepare_forkchoice.go @@ -6,9 +6,10 @@ import ( "errors" "time" + "github.com/prysmaticlabs/prysm/shared/blockutil" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" attaggregation "github.com/prysmaticlabs/prysm/shared/aggregation/attestations" "github.com/prysmaticlabs/prysm/shared/hashutil" "github.com/prysmaticlabs/prysm/shared/slotutil" @@ -88,7 +89,7 @@ func (s *Service) batchForkChoiceAtts(ctx context.Context) error { func (s *Service) aggregateAndSaveForkChoiceAtts(atts []*ethpb.Attestation) error { clonedAtts := make([]*ethpb.Attestation, len(atts)) for i, a := range atts { - clonedAtts[i] = stateV0.CopyAttestation(a) + clonedAtts[i] = blockutil.CopyAttestation(a) } aggregatedAtts, err := attaggregation.Aggregate(clonedAtts) if err != nil { diff --git a/beacon-chain/rpc/validator/BUILD.bazel b/beacon-chain/rpc/validator/BUILD.bazel index 52d63a5987f4..ce7231a87099 100644 --- a/beacon-chain/rpc/validator/BUILD.bazel +++ b/beacon-chain/rpc/validator/BUILD.bazel @@ -35,13 +35,13 @@ go_library( "//beacon-chain/p2p:go_default_library", "//beacon-chain/powchain:go_default_library", "//beacon-chain/state/interface:go_default_library", - "//beacon-chain/state/stateV0:go_default_library", "//beacon-chain/state/stategen:go_default_library", "//beacon-chain/sync:go_default_library", "//proto/beacon/db:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/aggregation:go_default_library", "//shared/aggregation/attestations:go_default_library", + "//shared/blockutil:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", "//shared/depositutil:go_default_library", diff --git a/beacon-chain/rpc/validator/attester.go b/beacon-chain/rpc/validator/attester.go index b53b1c4c24a5..e6dd520fde24 100644 --- a/beacon-chain/rpc/validator/attester.go +++ b/beacon-chain/rpc/validator/attester.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" + "github.com/prysmaticlabs/prysm/shared/blockutil" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/cache" @@ -12,7 +14,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/operation" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" @@ -182,7 +183,7 @@ func (vs *Server) ProposeAttestation(ctx context.Context, att *ethpb.Attestation go func() { ctx = trace.NewContext(context.Background(), trace.FromContext(ctx)) - attCopy := stateV0.CopyAttestation(att) + attCopy := blockutil.CopyAttestation(att) if err := vs.AttPool.SaveUnaggregatedAttestation(attCopy); err != nil { log.WithError(err).Error("Could not handle attestation in operations service") return diff --git a/beacon-chain/rpc/validator/proposer_utils_bench_test.go b/beacon-chain/rpc/validator/proposer_utils_bench_test.go index 86f391446aab..28acb93f412c 100644 --- a/beacon-chain/rpc/validator/proposer_utils_bench_test.go +++ b/beacon-chain/rpc/validator/proposer_utils_bench_test.go @@ -4,9 +4,10 @@ import ( "fmt" "testing" + "github.com/prysmaticlabs/prysm/shared/blockutil" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" aggtesting "github.com/prysmaticlabs/prysm/shared/aggregation/testing" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/params" @@ -48,7 +49,7 @@ func BenchmarkProposerAtts_sortByProfitability(b *testing.B) { runner := func(atts []*ethpb.Attestation) { attsCopy := make(proposerAtts, len(atts)) for i, att := range atts { - attsCopy[i] = stateV0.CopyAttestation(att) + attsCopy[i] = blockutil.CopyAttestation(att) } attsCopy.sortByProfitability() } diff --git a/beacon-chain/state/stateV0/BUILD.bazel b/beacon-chain/state/stateV0/BUILD.bazel index dc2dcdc0cc93..24aab76ab88f 100644 --- a/beacon-chain/state/stateV0/BUILD.bazel +++ b/beacon-chain/state/stateV0/BUILD.bazel @@ -4,7 +4,6 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test") go_library( name = "go_default_library", srcs = [ - "cloners.go", "doc.go", "field_roots.go", "field_trie.go", @@ -48,6 +47,7 @@ go_library( "//beacon-chain/state/interface:go_default_library", "//beacon-chain/state/stateutil:go_default_library", "//proto/beacon/p2p/v1:go_default_library", + "//shared/blockutil:go_default_library", "//shared/bytesutil:go_default_library", "//shared/featureconfig:go_default_library", "//shared/hashutil:go_default_library", @@ -85,6 +85,7 @@ go_test( "//beacon-chain/state/interface:go_default_library", "//beacon-chain/state/stateutil:go_default_library", "//proto/beacon/p2p/v1:go_default_library", + "//shared/blockutil:go_default_library", "//shared/bytesutil:go_default_library", "//shared/interop:go_default_library", "//shared/params:go_default_library", diff --git a/beacon-chain/state/stateV0/getters_eth1.go b/beacon-chain/state/stateV0/getters_eth1.go index e056de0b0884..a0e8c9c33475 100644 --- a/beacon-chain/state/stateV0/getters_eth1.go +++ b/beacon-chain/state/stateV0/getters_eth1.go @@ -4,6 +4,7 @@ import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/htrutils" ) @@ -33,7 +34,7 @@ func (b *BeaconState) eth1Data() *ethpb.Eth1Data { return nil } - return CopyETH1Data(b.state.Eth1Data) + return blockutil.CopyETH1Data(b.state.Eth1Data) } // Eth1DataVotes corresponds to votes from eth2 on the canonical proof-of-work chain @@ -65,7 +66,7 @@ func (b *BeaconState) eth1DataVotes() []*ethpb.Eth1Data { res := make([]*ethpb.Eth1Data, len(b.state.Eth1DataVotes)) for i := 0; i < len(res); i++ { - res[i] = CopyETH1Data(b.state.Eth1DataVotes[i]) + res[i] = blockutil.CopyETH1Data(b.state.Eth1DataVotes[i]) } return res } diff --git a/beacon-chain/state/stateV0/getters_state.go b/beacon-chain/state/stateV0/getters_state.go index 0e9d89f843a7..9c3d868fb58f 100644 --- a/beacon-chain/state/stateV0/getters_state.go +++ b/beacon-chain/state/stateV0/getters_state.go @@ -3,6 +3,8 @@ package stateV0 import ( "fmt" + "github.com/prysmaticlabs/prysm/shared/blockutil" + "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -158,7 +160,7 @@ func (b *BeaconState) safeCopyPendingAttestationSlice(input []*pbp2p.PendingAtte res := make([]*pbp2p.PendingAttestation, len(input)) for i := 0; i < len(res); i++ { - res[i] = CopyPendingAttestation(input[i]) + res[i] = blockutil.CopyPendingAttestation(input[i]) } return res } @@ -168,5 +170,5 @@ func (b *BeaconState) safeCopyCheckpoint(input *ethpb.Checkpoint) *ethpb.Checkpo return nil } - return CopyCheckpoint(input) + return blockutil.CopyCheckpoint(input) } diff --git a/beacon-chain/state/stateV0/getters_validator.go b/beacon-chain/state/stateV0/getters_validator.go index 4540887fdc98..4f5a05cf2c84 100644 --- a/beacon-chain/state/stateV0/getters_validator.go +++ b/beacon-chain/state/stateV0/getters_validator.go @@ -5,6 +5,8 @@ import ( "encoding/binary" "fmt" + "github.com/prysmaticlabs/prysm/shared/blockutil" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -48,7 +50,7 @@ func (b *BeaconState) validators() []*ethpb.Validator { if val == nil { continue } - res[i] = CopyValidator(val) + res[i] = blockutil.CopyValidator(val) } return res } @@ -92,7 +94,7 @@ func (b *BeaconState) ValidatorAtIndex(idx types.ValidatorIndex) (*ethpb.Validat defer b.lock.RUnlock() val := b.state.Validators[idx] - return CopyValidator(val), nil + return blockutil.CopyValidator(val), nil } // ValidatorAtIndexReadOnly is the validator at the provided index. This method diff --git a/beacon-chain/state/stateV0/references_test.go b/beacon-chain/state/stateV0/references_test.go index c3b6fe87c7c2..c1ef445fd7ea 100644 --- a/beacon-chain/state/stateV0/references_test.go +++ b/beacon-chain/state/stateV0/references_test.go @@ -6,6 +6,8 @@ import ( "runtime/debug" "testing" + "github.com/prysmaticlabs/prysm/shared/blockutil" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" @@ -252,7 +254,7 @@ func TestStateReferenceCopy_NoUnexpectedAttestationsMutation(t *testing.T) { copy(atts, state.CurrentEpochAttestations) state.CurrentEpochAttestations = atts for i := range state.GetCurrentEpochAttestations() { - att := CopyPendingAttestation(state.CurrentEpochAttestations[i]) + att := blockutil.CopyPendingAttestation(state.CurrentEpochAttestations[i]) att.AggregationBits = bitfield.NewBitlist(3) state.CurrentEpochAttestations[i] = att } @@ -261,7 +263,7 @@ func TestStateReferenceCopy_NoUnexpectedAttestationsMutation(t *testing.T) { copy(atts, state.PreviousEpochAttestations) state.PreviousEpochAttestations = atts for i := range state.GetPreviousEpochAttestations() { - att := CopyPendingAttestation(state.PreviousEpochAttestations[i]) + att := blockutil.CopyPendingAttestation(state.PreviousEpochAttestations[i]) att.AggregationBits = bitfield.NewBitlist(3) state.PreviousEpochAttestations[i] = att } diff --git a/beacon-chain/state/stateV0/setters_block.go b/beacon-chain/state/stateV0/setters_block.go index 73da62c60835..7ef0ceda94c6 100644 --- a/beacon-chain/state/stateV0/setters_block.go +++ b/beacon-chain/state/stateV0/setters_block.go @@ -3,6 +3,8 @@ package stateV0 import ( "fmt" + "github.com/prysmaticlabs/prysm/shared/blockutil" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" ) @@ -15,7 +17,7 @@ func (b *BeaconState) SetLatestBlockHeader(val *ethpb.BeaconBlockHeader) error { b.lock.Lock() defer b.lock.Unlock() - b.state.LatestBlockHeader = CopyBeaconBlockHeader(val) + b.state.LatestBlockHeader = blockutil.CopyBeaconBlockHeader(val) b.markFieldAsDirty(latestBlockHeader) return nil } diff --git a/beacon-chain/sync/BUILD.bazel b/beacon-chain/sync/BUILD.bazel index f1e2c9645f67..f7c1ecec11da 100644 --- a/beacon-chain/sync/BUILD.bazel +++ b/beacon-chain/sync/BUILD.bazel @@ -161,6 +161,7 @@ go_test( "//proto/beacon/p2p/v1:go_default_library", "//shared/abool:go_default_library", "//shared/attestationutil:go_default_library", + "//shared/blockutil:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", "//shared/params:go_default_library", diff --git a/beacon-chain/sync/pending_blocks_queue_test.go b/beacon-chain/sync/pending_blocks_queue_test.go index 2a307c662cc3..37efd626e511 100644 --- a/beacon-chain/sync/pending_blocks_queue_test.go +++ b/beacon-chain/sync/pending_blocks_queue_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/prysmaticlabs/prysm/shared/blockutil" + "github.com/ethereum/go-ethereum/p2p/enr" "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/protocol" @@ -18,7 +20,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers" p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/rand" @@ -435,15 +436,15 @@ func TestService_AddPeningBlockToQueueOverMax(t *testing.T) { } b := testutil.NewBeaconBlock() - b1 := stateV0.CopySignedBeaconBlock(b) + b1 := blockutil.CopySignedBeaconBlock(b) b1.Block.StateRoot = []byte{'a'} - b2 := stateV0.CopySignedBeaconBlock(b) + b2 := blockutil.CopySignedBeaconBlock(b) b2.Block.StateRoot = []byte{'b'} require.NoError(t, r.insertBlockToPendingQueue(0, b, [32]byte{})) require.NoError(t, r.insertBlockToPendingQueue(0, b1, [32]byte{1})) require.NoError(t, r.insertBlockToPendingQueue(0, b2, [32]byte{2})) - b3 := stateV0.CopySignedBeaconBlock(b) + b3 := blockutil.CopySignedBeaconBlock(b) b3.Block.StateRoot = []byte{'c'} require.NoError(t, r.insertBlockToPendingQueue(0, b2, [32]byte{3})) require.Equal(t, maxBlocksPerSlot, len(r.pendingBlocksInCache(0))) diff --git a/shared/aggregation/attestations/BUILD.bazel b/shared/aggregation/attestations/BUILD.bazel index 555f40be526f..56b1bfb05f29 100644 --- a/shared/aggregation/attestations/BUILD.bazel +++ b/shared/aggregation/attestations/BUILD.bazel @@ -11,8 +11,8 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/shared/aggregation/attestations", visibility = ["//visibility:public"], deps = [ - "//beacon-chain/state/stateV0:go_default_library", "//shared/aggregation:go_default_library", + "//shared/blockutil:go_default_library", "//shared/bls:go_default_library", "//shared/featureconfig:go_default_library", "@com_github_pkg_errors//:go_default_library", diff --git a/shared/aggregation/attestations/attestations.go b/shared/aggregation/attestations/attestations.go index e76031af2457..2687e4974c9a 100644 --- a/shared/aggregation/attestations/attestations.go +++ b/shared/aggregation/attestations/attestations.go @@ -3,8 +3,8 @@ package attestations import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "github.com/prysmaticlabs/prysm/shared/aggregation" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/sirupsen/logrus" @@ -73,8 +73,8 @@ func AggregatePair(a1, a2 *ethpb.Attestation) (*ethpb.Attestation, error) { return nil, aggregation.ErrBitsOverlap } - baseAtt := stateV0.CopyAttestation(a1) - newAtt := stateV0.CopyAttestation(a2) + baseAtt := blockutil.CopyAttestation(a1) + newAtt := blockutil.CopyAttestation(a2) if newAtt.AggregationBits.Count() > baseAtt.AggregationBits.Count() { baseAtt, newAtt = newAtt, baseAtt } diff --git a/shared/aggregation/attestations/attestations_bench_test.go b/shared/aggregation/attestations/attestations_bench_test.go index bcae92228096..1f2ab880d96b 100644 --- a/shared/aggregation/attestations/attestations_bench_test.go +++ b/shared/aggregation/attestations/attestations_bench_test.go @@ -4,9 +4,10 @@ import ( "fmt" "testing" + "github.com/prysmaticlabs/prysm/shared/blockutil" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" aggtesting "github.com/prysmaticlabs/prysm/shared/aggregation/testing" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bls/common" @@ -56,7 +57,7 @@ func BenchmarkAggregateAttestations_Aggregate(b *testing.B) { runner := func(atts []*ethpb.Attestation) { attsCopy := make([]*ethpb.Attestation, len(atts)) for i, att := range atts { - attsCopy[i] = stateV0.CopyAttestation(att) + attsCopy[i] = blockutil.CopyAttestation(att) } _, err := Aggregate(attsCopy) require.NoError(b, err) diff --git a/shared/aggregation/attestations/maxcover.go b/shared/aggregation/attestations/maxcover.go index af9d137a100e..19bb19ea93b9 100644 --- a/shared/aggregation/attestations/maxcover.go +++ b/shared/aggregation/attestations/maxcover.go @@ -6,8 +6,8 @@ import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "github.com/prysmaticlabs/prysm/shared/aggregation" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/bls" ) @@ -176,7 +176,7 @@ func (al attList) aggregate(coverage bitfield.Bitlist) (*ethpb.Attestation, erro } return ðpb.Attestation{ AggregationBits: coverage, - Data: stateV0.CopyAttestationData(al[0].Data), + Data: blockutil.CopyAttestationData(al[0].Data), Signature: aggregateSignatures(signs).Marshal(), }, nil } @@ -208,7 +208,7 @@ func aggregateAttestations(atts []*ethpb.Attestation, keys []int, coverage *bitf } signs = append(signs, sig) if i == 0 { - data = stateV0.CopyAttestationData(atts[idx].Data) + data = blockutil.CopyAttestationData(atts[idx].Data) targetIdx = idx } } diff --git a/shared/blockutil/BUILD.bazel b/shared/blockutil/BUILD.bazel index e27e6724226a..c779864ef62e 100644 --- a/shared/blockutil/BUILD.bazel +++ b/shared/blockutil/BUILD.bazel @@ -3,10 +3,15 @@ load("@prysm//tools/go:def.bzl", "go_library") go_library( name = "go_default_library", - srcs = ["block_utils.go"], + srcs = [ + "block_utils.go", + "cloners.go", + ], importpath = "github.com/prysmaticlabs/prysm/shared/blockutil", visibility = ["//visibility:public"], deps = [ + "//proto/beacon/p2p/v1:go_default_library", + "//shared/bytesutil:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", ], diff --git a/beacon-chain/state/stateV0/cloners.go b/shared/blockutil/cloners.go similarity index 99% rename from beacon-chain/state/stateV0/cloners.go rename to shared/blockutil/cloners.go index 7da44c565c2d..c6a7756a1db3 100644 --- a/beacon-chain/state/stateV0/cloners.go +++ b/shared/blockutil/cloners.go @@ -1,4 +1,4 @@ -package stateV0 +package blockutil import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" diff --git a/shared/depositutil/BUILD.bazel b/shared/depositutil/BUILD.bazel index 58f3827d6057..98226dee71e3 100644 --- a/shared/depositutil/BUILD.bazel +++ b/shared/depositutil/BUILD.bazel @@ -8,8 +8,8 @@ go_library( visibility = ["//visibility:public"], deps = [ "//beacon-chain/core/helpers:go_default_library", - "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", + "//shared/blockutil:go_default_library", "//shared/bls:go_default_library", "//shared/featureconfig:go_default_library", "//shared/hashutil:go_default_library", diff --git a/shared/depositutil/deposit.go b/shared/depositutil/deposit.go index 7eea25c5a0d3..e86063c69eb5 100644 --- a/shared/depositutil/deposit.go +++ b/shared/depositutil/deposit.go @@ -6,8 +6,8 @@ import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/hashutil" @@ -84,7 +84,7 @@ func VerifyDepositSignature(dd *ethpb.Deposit_Data, domain []byte) error { if featureconfig.Get().SkipBLSVerify { return nil } - ddCopy := stateV0.CopyDepositData(dd) + ddCopy := blockutil.CopyDepositData(dd) publicKey, err := bls.PublicKeyFromBytes(ddCopy.PublicKey) if err != nil { return errors.Wrap(err, "could not convert bytes to public key") diff --git a/beacon-chain/interfaces/BUILD.bazel b/shared/interfaces/BUILD.bazel similarity index 100% rename from beacon-chain/interfaces/BUILD.bazel rename to shared/interfaces/BUILD.bazel diff --git a/beacon-chain/interfaces/block_interface.go b/shared/interfaces/block_interface.go similarity index 87% rename from beacon-chain/interfaces/block_interface.go rename to shared/interfaces/block_interface.go index dd054d636f0d..bd0314160da3 100644 --- a/beacon-chain/interfaces/block_interface.go +++ b/shared/interfaces/block_interface.go @@ -9,6 +9,7 @@ type SignedBeaconBlock interface { Block() BeaconBlock Signature() []byte IsNil() bool + Copy() WrappedSignedBeaconBlock } type BeaconBlock interface { @@ -18,6 +19,7 @@ type BeaconBlock interface { StateRoot() []byte Body() BeaconBlockBody IsNil() bool + HashTreeRoot() ([32]byte, error) } type BeaconBlockBody interface { RandaoReveal() []byte @@ -29,4 +31,5 @@ type BeaconBlockBody interface { Deposits() []*ethpb.Deposit VoluntaryExits() []*ethpb.SignedVoluntaryExit IsNil() bool + HashTreeRoot() ([32]byte, error) } diff --git a/beacon-chain/interfaces/block_wrapper.go b/shared/interfaces/block_wrapper.go similarity index 85% rename from beacon-chain/interfaces/block_wrapper.go rename to shared/interfaces/block_wrapper.go index 8da33799a944..7247d8a2f937 100644 --- a/beacon-chain/interfaces/block_wrapper.go +++ b/shared/interfaces/block_wrapper.go @@ -3,6 +3,7 @@ package interfaces import ( types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/shared/blockutil" ) type WrappedSignedBeaconBlock struct { @@ -25,6 +26,10 @@ func (w WrappedSignedBeaconBlock) IsNil() bool { return w.b == nil } +func (w WrappedSignedBeaconBlock) Copy() WrappedSignedBeaconBlock { + return NewWrappedSignedBeaconBlock(blockutil.CopySignedBeaconBlock(w.b)) +} + type WrappedBeaconBlock struct { b *ethpb.BeaconBlock } @@ -57,6 +62,10 @@ func (w WrappedBeaconBlock) IsNil() bool { return w.b == nil } +func (w WrappedBeaconBlock) HashTreeRoot() ([32]byte, error) { + return w.b.HashTreeRoot() +} + type WrappedBeaconBlockBody struct { b *ethpb.BeaconBlockBody } @@ -100,3 +109,7 @@ func (w WrappedBeaconBlockBody) VoluntaryExits() []*ethpb.SignedVoluntaryExit { func (w WrappedBeaconBlockBody) IsNil() bool { return w.b == nil } + +func (w WrappedBeaconBlockBody) HashTreeRoot() ([32]byte, error) { + return w.b.HashTreeRoot() +} diff --git a/slasher/rpc/BUILD.bazel b/slasher/rpc/BUILD.bazel index a04802afda94..9e7531c32f74 100644 --- a/slasher/rpc/BUILD.bazel +++ b/slasher/rpc/BUILD.bazel @@ -50,7 +50,7 @@ go_test( deps = [ "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/p2p/types:go_default_library", - "//beacon-chain/state/stateV0:go_default_library", + "//shared/blockutil:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", "//shared/mock:go_default_library", diff --git a/slasher/rpc/server_test.go b/slasher/rpc/server_test.go index df6d639ba986..62fa0fee2aad 100644 --- a/slasher/rpc/server_test.go +++ b/slasher/rpc/server_test.go @@ -11,7 +11,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/mock" @@ -81,7 +81,7 @@ func TestServer_IsSlashableAttestation(t *testing.T) { for i := types.Slot(0); i < 100; i++ { go func(j types.Slot) { defer wg.Done() - iatt := stateV0.CopyIndexedAttestation(savedAttestation) + iatt := blockutil.CopyIndexedAttestation(savedAttestation) iatt.Data.Slot += j root, err := helpers.ComputeSigningRoot(iatt.Data, domain) require.NoError(t, err) @@ -233,7 +233,7 @@ func TestServer_IsSlashableBlock(t *testing.T) { for i := uint64(0); i < 100; i++ { go func(j uint64) { defer wg.Done() - sbbh := stateV0.CopySignedBeaconBlockHeader(savedBlock) + sbbh := blockutil.CopySignedBeaconBlockHeader(savedBlock) sbbh.Header.BodyRoot = bytesutil.PadTo([]byte(fmt.Sprintf("%d", j)), 32) bhr, err := sbbh.Header.HashTreeRoot() assert.NoError(t, err) From 4d82c1a59f96cc286adc19bf9e9cb489fa3a91a0 Mon Sep 17 00:00:00 2001 From: nisdas Date: Fri, 21 May 2021 16:32:21 +0800 Subject: [PATCH 03/44] gaz --- beacon-chain/blockchain/BUILD.bazel | 2 +- beacon-chain/blockchain/head.go | 2 +- beacon-chain/blockchain/service.go | 22 ++++++++++----------- beacon-chain/core/blocks/BUILD.bazel | 1 + beacon-chain/core/feed/state/BUILD.bazel | 2 +- beacon-chain/core/helpers/BUILD.bazel | 1 + beacon-chain/core/state/BUILD.bazel | 1 + beacon-chain/core/state/interop/BUILD.bazel | 2 +- beacon-chain/db/iface/BUILD.bazel | 2 +- beacon-chain/db/iface/interface.go | 2 +- shared/interfaces/BUILD.bazel | 3 ++- shared/interfaces/block_interface.go | 2 ++ shared/interfaces/block_wrapper.go | 8 ++++++++ 13 files changed, 31 insertions(+), 19 deletions(-) diff --git a/beacon-chain/blockchain/BUILD.bazel b/beacon-chain/blockchain/BUILD.bazel index 38fb5bbbbee2..5db74a110373 100644 --- a/beacon-chain/blockchain/BUILD.bazel +++ b/beacon-chain/blockchain/BUILD.bazel @@ -36,7 +36,6 @@ go_library( "//beacon-chain/db/filters:go_default_library", "//beacon-chain/forkchoice:go_default_library", "//beacon-chain/forkchoice/protoarray:go_default_library", - "//beacon-chain/interfaces:go_default_library", "//beacon-chain/operations/attestations:go_default_library", "//beacon-chain/operations/slashings:go_default_library", "//beacon-chain/operations/voluntaryexits:go_default_library", @@ -51,6 +50,7 @@ go_library( "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", "//shared/featureconfig:go_default_library", + "//shared/interfaces:go_default_library", "//shared/mputil:go_default_library", "//shared/params:go_default_library", "//shared/slotutil:go_default_library", diff --git a/beacon-chain/blockchain/head.go b/beacon-chain/blockchain/head.go index 4a9828ce0d79..d9935fc31cd1 100644 --- a/beacon-chain/blockchain/head.go +++ b/beacon-chain/blockchain/head.go @@ -63,7 +63,7 @@ func (s *Service) updateHead(ctx context.Context, balances []uint64) error { return err } s.cfg.ForkChoiceStore = protoarray.New(j.Epoch, f.Epoch, bytesutil.ToBytes32(f.Root)) - if err := s.insertBlockToForkChoiceStore(ctx, jb.Block, headStartRoot, f, j); err != nil { + if err := s.insertBlockToForkChoiceStore(ctx, jb.Block(), headStartRoot, f, j); err != nil { return err } } diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 17b761ff89da..50de2b2906f5 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -10,10 +10,6 @@ import ( "sync" "time" - "github.com/prysmaticlabs/prysm/shared/blockutil" - - "github.com/prysmaticlabs/prysm/beacon-chain/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -34,7 +30,9 @@ import ( iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/slotutil" "github.com/sirupsen/logrus" @@ -123,7 +121,7 @@ func (s *Service) Start() { log.Fatalf("Could not fetch finalized cp: %v", err) } if genesisBlock != nil { - r, err = genesisBlock.Block.HashTreeRoot() + r, err = genesisBlock.Block().HashTreeRoot() if err != nil { log.Fatalf("Could not tree hash genesis block: %v", err) } @@ -181,11 +179,11 @@ func (s *Service) Start() { if err != nil { log.Fatalf("Could not get start slot of finalized epoch: %v", err) } - h := s.headBlock().Block - if h.Slot > ss { + h := s.headBlock().Block() + if h.Slot() > ss { log.WithFields(logrus.Fields{ "startSlot": ss, - "endSlot": h.Slot, + "endSlot": h.Slot(), }).Info("Loading blocks to fork choice store, this may take a while.") if err := s.fillInForkChoiceMissingBlocks(s.ctx, h, s.finalizedCheckpt, s.justifiedCheckpt); err != nil { log.Fatalf("Could not fill in fork choice store missing blocks: %v", err) @@ -343,7 +341,7 @@ func (s *Service) saveGenesisData(ctx context.Context, genesisState iface.Beacon if err != nil || genesisBlk == nil { return fmt.Errorf("could not load genesis block: %v", err) } - genesisBlkRoot, err := genesisBlk.Block.HashTreeRoot() + genesisBlkRoot, err := genesisBlk.Block().HashTreeRoot() if err != nil { return errors.Wrap(err, "could not get genesis block root") } @@ -364,7 +362,7 @@ func (s *Service) saveGenesisData(ctx context.Context, genesisState iface.Beacon s.prevFinalizedCheckpt = blockutil.CopyCheckpoint(genesisCheckpoint) if err := s.cfg.ForkChoiceStore.ProcessBlock(ctx, - genesisBlk.Block.Slot, + genesisBlk.Block().Slot(), genesisBlkRoot, params.BeaconConfig().ZeroHash, [32]byte{}, @@ -386,7 +384,7 @@ func (s *Service) initializeChainInfo(ctx context.Context) error { if genesisBlock == nil { return errors.New("no genesis block in db") } - genesisBlkRoot, err := genesisBlock.Block.HashTreeRoot() + genesisBlkRoot, err := genesisBlock.Block().HashTreeRoot() if err != nil { return errors.Wrap(err, "could not get signing root of genesis block") } @@ -431,7 +429,7 @@ func (s *Service) initializeChainInfo(ctx context.Context) error { return errors.Wrap(err, "could not get finalized state from db") } log.Infof("Regenerating state from the last checkpoint at slot %d to current head slot of %d."+ - "This process may take a while, please wait.", finalizedState.Slot(), headBlock.Block.Slot) + "This process may take a while, please wait.", finalizedState.Slot(), headBlock.Block().Slot()) headState, err := s.cfg.StateGen.StateByRoot(ctx, headRoot) if err != nil { return errors.Wrap(err, "could not retrieve head state") diff --git a/beacon-chain/core/blocks/BUILD.bazel b/beacon-chain/core/blocks/BUILD.bazel index 12674b25cd55..6b51a0bd9451 100644 --- a/beacon-chain/core/blocks/BUILD.bazel +++ b/beacon-chain/core/blocks/BUILD.bazel @@ -35,6 +35,7 @@ go_library( "//shared/bytesutil:go_default_library", "//shared/depositutil:go_default_library", "//shared/hashutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/mathutil:go_default_library", "//shared/params:go_default_library", "//shared/slashutil:go_default_library", diff --git a/beacon-chain/core/feed/state/BUILD.bazel b/beacon-chain/core/feed/state/BUILD.bazel index d86c08284210..3eb15b66f6af 100644 --- a/beacon-chain/core/feed/state/BUILD.bazel +++ b/beacon-chain/core/feed/state/BUILD.bazel @@ -10,7 +10,7 @@ go_library( visibility = ["//beacon-chain:__subpackages__"], deps = [ "//shared/event:go_default_library", + "//shared/interfaces:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", - "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", ], ) diff --git a/beacon-chain/core/helpers/BUILD.bazel b/beacon-chain/core/helpers/BUILD.bazel index 45ba92a96bad..34a3abe9f901 100644 --- a/beacon-chain/core/helpers/BUILD.bazel +++ b/beacon-chain/core/helpers/BUILD.bazel @@ -40,6 +40,7 @@ go_library( "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", "//shared/hashutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/mathutil:go_default_library", "//shared/params:go_default_library", "//shared/sliceutil:go_default_library", diff --git a/beacon-chain/core/state/BUILD.bazel b/beacon-chain/core/state/BUILD.bazel index f7f82bde7c76..d33d0787a362 100644 --- a/beacon-chain/core/state/BUILD.bazel +++ b/beacon-chain/core/state/BUILD.bazel @@ -40,6 +40,7 @@ go_library( "//shared/bytesutil:go_default_library", "//shared/featureconfig:go_default_library", "//shared/hashutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/mathutil:go_default_library", "//shared/params:go_default_library", "//shared/traceutil:go_default_library", diff --git a/beacon-chain/core/state/interop/BUILD.bazel b/beacon-chain/core/state/interop/BUILD.bazel index 065b5386758a..5ed7a481c343 100644 --- a/beacon-chain/core/state/interop/BUILD.bazel +++ b/beacon-chain/core/state/interop/BUILD.bazel @@ -16,7 +16,7 @@ go_library( "//beacon-chain/state/interface:go_default_library", "//shared/featureconfig:go_default_library", "//shared/fileutil:go_default_library", - "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", + "//shared/interfaces:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", ], ) diff --git a/beacon-chain/db/iface/BUILD.bazel b/beacon-chain/db/iface/BUILD.bazel index 3fb69543e91e..72fcc9ff9404 100644 --- a/beacon-chain/db/iface/BUILD.bazel +++ b/beacon-chain/db/iface/BUILD.bazel @@ -11,12 +11,12 @@ go_library( visibility = ["//beacon-chain/db:__subpackages__"], deps = [ "//beacon-chain/db/filters:go_default_library", - "//beacon-chain/interfaces:go_default_library", "//beacon-chain/slasher/types:go_default_library", "//beacon-chain/state/interface:go_default_library", "//proto/beacon/db:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/backuputil:go_default_library", + "//shared/interfaces:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", diff --git a/beacon-chain/db/iface/interface.go b/beacon-chain/db/iface/interface.go index cbf7d89f1342..25729d3eea7f 100644 --- a/beacon-chain/db/iface/interface.go +++ b/beacon-chain/db/iface/interface.go @@ -28,7 +28,7 @@ type ReadOnlyDatabase interface { BlocksBySlot(ctx context.Context, slot types.Slot) (bool, []*eth.SignedBeaconBlock, error) BlockRootsBySlot(ctx context.Context, slot types.Slot) (bool, [][32]byte, error) HasBlock(ctx context.Context, blockRoot [32]byte) bool - GenesisBlock(ctx context.Context) (*eth.SignedBeaconBlock, error) + GenesisBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool FinalizedChildBlock(ctx context.Context, blockRoot [32]byte) (*eth.SignedBeaconBlock, error) HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([]*eth.SignedBeaconBlock, error) diff --git a/shared/interfaces/BUILD.bazel b/shared/interfaces/BUILD.bazel index 9412f2b0c221..5a321b840b25 100644 --- a/shared/interfaces/BUILD.bazel +++ b/shared/interfaces/BUILD.bazel @@ -6,9 +6,10 @@ go_library( "block_interface.go", "block_wrapper.go", ], - importpath = "github.com/prysmaticlabs/prysm/beacon-chain/interfaces", + importpath = "github.com/prysmaticlabs/prysm/shared/interfaces", visibility = ["//visibility:public"], deps = [ + "//shared/blockutil:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", ], diff --git a/shared/interfaces/block_interface.go b/shared/interfaces/block_interface.go index bd0314160da3..c08e0f745b12 100644 --- a/shared/interfaces/block_interface.go +++ b/shared/interfaces/block_interface.go @@ -10,6 +10,7 @@ type SignedBeaconBlock interface { Signature() []byte IsNil() bool Copy() WrappedSignedBeaconBlock + MarshalSSZ() ([]byte, error) } type BeaconBlock interface { @@ -20,6 +21,7 @@ type BeaconBlock interface { Body() BeaconBlockBody IsNil() bool HashTreeRoot() ([32]byte, error) + MarshalSSZ() ([]byte, error) } type BeaconBlockBody interface { RandaoReveal() []byte diff --git a/shared/interfaces/block_wrapper.go b/shared/interfaces/block_wrapper.go index 7247d8a2f937..33d65d2d1611 100644 --- a/shared/interfaces/block_wrapper.go +++ b/shared/interfaces/block_wrapper.go @@ -30,6 +30,10 @@ func (w WrappedSignedBeaconBlock) Copy() WrappedSignedBeaconBlock { return NewWrappedSignedBeaconBlock(blockutil.CopySignedBeaconBlock(w.b)) } +func (w WrappedSignedBeaconBlock) MarshalSSZ() ([]byte, error) { + return w.b.MarshalSSZ() +} + type WrappedBeaconBlock struct { b *ethpb.BeaconBlock } @@ -66,6 +70,10 @@ func (w WrappedBeaconBlock) HashTreeRoot() ([32]byte, error) { return w.b.HashTreeRoot() } +func (w WrappedBeaconBlock) MarshalSSZ() ([]byte, error) { + return w.b.MarshalSSZ() +} + type WrappedBeaconBlockBody struct { b *ethpb.BeaconBlockBody } From 6872b204b92b85d6f35d30ab9db77eb8bf0e4632 Mon Sep 17 00:00:00 2001 From: nisdas Date: Fri, 21 May 2021 17:06:58 +0800 Subject: [PATCH 04/44] checkpoint --- beacon-chain/blockchain/chain_info.go | 8 +-- beacon-chain/blockchain/log.go | 33 +++++----- .../blockchain/process_attestation_helpers.go | 4 +- beacon-chain/blockchain/process_block.go | 2 +- .../blockchain/process_block_helpers.go | 4 +- beacon-chain/blockchain/receive_block.go | 34 +++++------ beacon-chain/db/iface/interface.go | 6 +- beacon-chain/db/kv/BUILD.bazel | 1 + beacon-chain/db/kv/backup.go | 2 +- beacon-chain/db/kv/blocks.go | 60 ++++++++++--------- beacon-chain/db/kv/finalized_block_roots.go | 18 +++--- beacon-chain/db/kv/genesis.go | 4 +- beacon-chain/db/kv/state.go | 4 +- beacon-chain/rpc/debug/block.go | 4 +- beacon-chain/state/stategen/BUILD.bazel | 1 + beacon-chain/state/stategen/getter.go | 4 +- beacon-chain/state/stategen/migrate.go | 4 +- beacon-chain/state/stategen/replay.go | 33 +++++----- shared/interfaces/BUILD.bazel | 1 + shared/interfaces/block_interface.go | 4 ++ shared/interfaces/block_wrapper.go | 13 ++++ 21 files changed, 137 insertions(+), 107 deletions(-) diff --git a/beacon-chain/blockchain/chain_info.go b/beacon-chain/blockchain/chain_info.go index 3e9585bf628d..5a9079987bb3 100644 --- a/beacon-chain/blockchain/chain_info.go +++ b/beacon-chain/blockchain/chain_info.go @@ -4,15 +4,15 @@ import ( "context" "time" - "github.com/prysmaticlabs/prysm/shared/blockutil" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "go.opencensus.io/trace" ) @@ -128,7 +128,7 @@ func (s *Service) HeadRoot(ctx context.Context) ([]byte, error) { return params.BeaconConfig().ZeroHash[:], nil } - r, err := b.Block.HashTreeRoot() + r, err := b.Block().HashTreeRoot() if err != nil { return nil, err } @@ -139,7 +139,7 @@ func (s *Service) HeadRoot(ctx context.Context) ([]byte, error) { // HeadBlock returns the head block of the chain. // If the head is nil from service struct, // it will attempt to get the head block from DB. -func (s *Service) HeadBlock(ctx context.Context) (*ethpb.SignedBeaconBlock, error) { +func (s *Service) HeadBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) { s.headLock.RLock() defer s.headLock.RUnlock() diff --git a/beacon-chain/blockchain/log.go b/beacon-chain/blockchain/log.go index 5af2f5acf331..fd103d425af7 100644 --- a/beacon-chain/blockchain/log.go +++ b/beacon-chain/blockchain/log.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/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/timeutils" "github.com/sirupsen/logrus" @@ -15,36 +16,36 @@ import ( var log = logrus.WithField("prefix", "blockchain") // logs state transition related data every slot. -func logStateTransitionData(b *ethpb.BeaconBlock) { +func logStateTransitionData(b interfaces.BeaconBlock) { log := log.WithField("slot", b.Slot) - if len(b.Body.Attestations) > 0 { - log = log.WithField("attestations", len(b.Body.Attestations)) + if len(b.Body().Attestations()) > 0 { + log = log.WithField("attestations", len(b.Body().Attestations())) } - if len(b.Body.Deposits) > 0 { - log = log.WithField("deposits", len(b.Body.Deposits)) + if len(b.Body().Deposits()) > 0 { + log = log.WithField("deposits", len(b.Body().Deposits())) } - if len(b.Body.AttesterSlashings) > 0 { - log = log.WithField("attesterSlashings", len(b.Body.AttesterSlashings)) + if len(b.Body().AttesterSlashings()) > 0 { + log = log.WithField("attesterSlashings", len(b.Body().AttesterSlashings())) } - if len(b.Body.ProposerSlashings) > 0 { - log = log.WithField("proposerSlashings", len(b.Body.ProposerSlashings)) + if len(b.Body().ProposerSlashings()) > 0 { + log = log.WithField("proposerSlashings", len(b.Body().ProposerSlashings())) } - if len(b.Body.VoluntaryExits) > 0 { - log = log.WithField("voluntaryExits", len(b.Body.VoluntaryExits)) + if len(b.Body().VoluntaryExits()) > 0 { + log = log.WithField("voluntaryExits", len(b.Body().VoluntaryExits())) } log.Info("Finished applying state transition") } -func logBlockSyncStatus(block *ethpb.BeaconBlock, blockRoot [32]byte, finalized *ethpb.Checkpoint, receivedTime time.Time, genesisTime uint64) error { - startTime, err := helpers.SlotToTime(genesisTime, block.Slot) +func logBlockSyncStatus(block interfaces.BeaconBlock, blockRoot [32]byte, finalized *ethpb.Checkpoint, receivedTime time.Time, genesisTime uint64) error { + startTime, err := helpers.SlotToTime(genesisTime, block.Slot()) if err != nil { return err } log.WithFields(logrus.Fields{ - "slot": block.Slot, - "slotInEpoch": block.Slot % params.BeaconConfig().SlotsPerEpoch, + "slot": block.Slot(), + "slotInEpoch": block.Slot() % params.BeaconConfig().SlotsPerEpoch, "block": fmt.Sprintf("0x%s...", hex.EncodeToString(blockRoot[:])[:8]), - "epoch": helpers.SlotToEpoch(block.Slot), + "epoch": helpers.SlotToEpoch(block.Slot()), "finalizedEpoch": finalized.Epoch, "finalizedRoot": fmt.Sprintf("0x%s...", hex.EncodeToString(finalized.Root)[:8]), }).Info("Synced new block") diff --git a/beacon-chain/blockchain/process_attestation_helpers.go b/beacon-chain/blockchain/process_attestation_helpers.go index d00212cc65dd..0105d698b6fa 100644 --- a/beacon-chain/blockchain/process_attestation_helpers.go +++ b/beacon-chain/blockchain/process_attestation_helpers.go @@ -105,8 +105,8 @@ func (s *Service) verifyBeaconBlock(ctx context.Context, data *ethpb.Attestation if err := helpers.VerifyNilBeaconBlock(b); err != nil { return err } - if b.Block.Slot > data.Slot { - return fmt.Errorf("could not process attestation for future block, block.Slot=%d > attestation.Data.Slot=%d", b.Block.Slot, data.Slot) + if b.Block().Slot() > data.Slot { + return fmt.Errorf("could not process attestation for future block, block.Slot=%d > attestation.Data.Slot=%d", b.Block().Slot(), data.Slot) } return nil } diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 512ee8795445..07649d2a15e0 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -205,7 +205,7 @@ func (s *Service) onBlockBatch(ctx context.Context, blks []interfaces.SignedBeac return nil, nil, err } if preState == nil { - return nil, nil, fmt.Errorf("nil pre state for slot %d", b.Slot) + return nil, nil, fmt.Errorf("nil pre state for slot %d", b.Slot()) } jCheckpoints := make([]*ethpb.Checkpoint, len(blks)) diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index 9a37dde69ad4..265e85c3b6cb 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -111,7 +111,7 @@ func (s *Service) VerifyBlkDescendant(ctx context.Context, root [32]byte) error if !bytes.Equal(bFinalizedRoot, fRoot[:]) { err := fmt.Errorf("block %#x is not a descendent of the current finalized block slot %d, %#x != %#x", - bytesutil.Trunc(root[:]), finalizedBlk.Slot, bytesutil.Trunc(bFinalizedRoot), + bytesutil.Trunc(root[:]), finalizedBlk.Slot(), bytesutil.Trunc(bFinalizedRoot), bytesutil.Trunc(fRoot[:])) traceutil.AnnotateError(span, err) return err @@ -174,7 +174,7 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified } } - if justifiedBlockSigned == nil || justifiedBlockSigned.Block == nil { + if justifiedBlockSigned.IsNil() || justifiedBlockSigned.Block().IsNil() { return false, errors.New("nil justified block") } justifiedBlock := justifiedBlockSigned.Block() diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index 9faafdfebc16..5d5f2802a79c 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -3,7 +3,7 @@ package blockchain import ( "context" - "github.com/prysmaticlabs/prysm/shared/blockutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" @@ -32,11 +32,11 @@ type BlockReceiver interface { // 1. Validate block, apply state transition and update check points // 2. Apply fork choice to the processed block // 3. Save latest head info -func (s *Service) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlock, blockRoot [32]byte) error { +func (s *Service) ReceiveBlock(ctx context.Context, block interfaces.SignedBeaconBlock, blockRoot [32]byte) error { ctx, span := trace.StartSpan(ctx, "blockChain.ReceiveBlock") defer span.End() receivedTime := timeutils.Now() - blockCopy := blockutil.CopySignedBeaconBlock(block) + blockCopy := block.Copy() // Apply state transition on the new block. if err := s.onBlock(ctx, blockCopy, blockRoot); err != nil { @@ -54,7 +54,7 @@ func (s *Service) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlo s.cfg.StateNotifier.StateFeed().Send(&feed.Event{ Type: statefeed.BlockProcessed, Data: &statefeed.BlockProcessedData{ - Slot: blockCopy.Block.Slot, + Slot: blockCopy.Block().Slot(), BlockRoot: blockRoot, SignedBlock: blockCopy, Verified: true, @@ -63,7 +63,7 @@ func (s *Service) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlo } // Handle post block operations such as attestations and exits. - if err := s.handlePostBlockOperations(blockCopy.Block); err != nil { + if err := s.handlePostBlockOperations(blockCopy.Block()); err != nil { return err } @@ -73,14 +73,14 @@ func (s *Service) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlo } // Reports on block and fork choice metrics. - reportSlotMetrics(blockCopy.Block.Slot, s.HeadSlot(), s.CurrentSlot(), s.finalizedCheckpt) + reportSlotMetrics(blockCopy.Block().Slot(), s.HeadSlot(), s.CurrentSlot(), s.finalizedCheckpt) // Log block sync status. - if err := logBlockSyncStatus(blockCopy.Block, blockRoot, s.finalizedCheckpt, receivedTime, uint64(s.genesisTime.Unix())); err != nil { + if err := logBlockSyncStatus(blockCopy.Block(), blockRoot, s.finalizedCheckpt, receivedTime, uint64(s.genesisTime.Unix())); err != nil { return err } // Log state transition data. - logStateTransitionData(blockCopy.Block) + logStateTransitionData(blockCopy.Block()) return nil } @@ -88,7 +88,7 @@ func (s *Service) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlo // ReceiveBlockBatch processes the whole block batch at once, assuming the block batch is linear ,transitioning // the state, performing batch verification of all collected signatures and then performing the appropriate // actions for a block post-transition. -func (s *Service) ReceiveBlockBatch(ctx context.Context, blocks []*ethpb.SignedBeaconBlock, blkRoots [][32]byte) error { +func (s *Service) ReceiveBlockBatch(ctx context.Context, blocks []interfaces.SignedBeaconBlock, blkRoots [][32]byte) error { ctx, span := trace.StartSpan(ctx, "blockChain.ReceiveBlockBatch") defer span.End() @@ -101,7 +101,7 @@ func (s *Service) ReceiveBlockBatch(ctx context.Context, blocks []*ethpb.SignedB } for i, b := range blocks { - blockCopy := blockutil.CopySignedBeaconBlock(b) + blockCopy := b.Copy() if err = s.handleBlockAfterBatchVerify(ctx, blockCopy, blkRoots[i], fCheckpoints[i], jCheckpoints[i]); err != nil { traceutil.AnnotateError(span, err) return err @@ -110,7 +110,7 @@ func (s *Service) ReceiveBlockBatch(ctx context.Context, blocks []*ethpb.SignedB s.cfg.StateNotifier.StateFeed().Send(&feed.Event{ Type: statefeed.BlockProcessed, Data: &statefeed.BlockProcessedData{ - Slot: blockCopy.Block.Slot, + Slot: blockCopy.Block().Slot(), BlockRoot: blkRoots[i], SignedBlock: blockCopy, Verified: true, @@ -118,7 +118,7 @@ func (s *Service) ReceiveBlockBatch(ctx context.Context, blocks []*ethpb.SignedB }) // Reports on blockCopy and fork choice metrics. - reportSlotMetrics(blockCopy.Block.Slot, s.HeadSlot(), s.CurrentSlot(), s.finalizedCheckpt) + reportSlotMetrics(blockCopy.Block().Slot(), s.HeadSlot(), s.CurrentSlot(), s.finalizedCheckpt) } if err := s.VerifyWeakSubjectivityRoot(s.ctx); err != nil { @@ -136,24 +136,24 @@ func (s *Service) HasInitSyncBlock(root [32]byte) bool { return s.hasInitSyncBlock(root) } -func (s *Service) handlePostBlockOperations(b *ethpb.BeaconBlock) error { +func (s *Service) handlePostBlockOperations(b interfaces.BeaconBlock) error { // Delete the processed block attestations from attestation pool. - if err := s.deletePoolAtts(b.Body.Attestations); err != nil { + if err := s.deletePoolAtts(b.Body().Attestations()); err != nil { return err } // Add block attestations to the fork choice pool to compute head. - if err := s.cfg.AttPool.SaveBlockAttestations(b.Body.Attestations); err != nil { + if err := s.cfg.AttPool.SaveBlockAttestations(b.Body().Attestations()); err != nil { log.Errorf("Could not save block attestations for fork choice: %v", err) return nil } // Mark block exits as seen so we don't include same ones in future blocks. - for _, e := range b.Body.VoluntaryExits { + for _, e := range b.Body().VoluntaryExits() { s.cfg.ExitPool.MarkIncluded(e) } // Mark attester slashings as seen so we don't include same ones in future blocks. - for _, as := range b.Body.AttesterSlashings { + for _, as := range b.Body().AttesterSlashings() { s.cfg.SlashingPool.MarkIncludedAttesterSlashing(as) } return nil diff --git a/beacon-chain/db/iface/interface.go b/beacon-chain/db/iface/interface.go index 25729d3eea7f..e2a7284283f5 100644 --- a/beacon-chain/db/iface/interface.go +++ b/beacon-chain/db/iface/interface.go @@ -25,13 +25,13 @@ type ReadOnlyDatabase interface { Block(ctx context.Context, blockRoot [32]byte) (interfaces.SignedBeaconBlock, error) Blocks(ctx context.Context, f *filters.QueryFilter) ([]interfaces.SignedBeaconBlock, [][32]byte, error) BlockRoots(ctx context.Context, f *filters.QueryFilter) ([][32]byte, error) - BlocksBySlot(ctx context.Context, slot types.Slot) (bool, []*eth.SignedBeaconBlock, error) + BlocksBySlot(ctx context.Context, slot types.Slot) (bool, []interfaces.SignedBeaconBlock, error) BlockRootsBySlot(ctx context.Context, slot types.Slot) (bool, [][32]byte, error) HasBlock(ctx context.Context, blockRoot [32]byte) bool GenesisBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool - FinalizedChildBlock(ctx context.Context, blockRoot [32]byte) (*eth.SignedBeaconBlock, error) - HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([]*eth.SignedBeaconBlock, error) + FinalizedChildBlock(ctx context.Context, blockRoot [32]byte) (interfaces.SignedBeaconBlock, error) + HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([]interfaces.SignedBeaconBlock, error) // State related methods. State(ctx context.Context, blockRoot [32]byte) (iface.BeaconState, error) GenesisState(ctx context.Context) (iface.BeaconState, error) diff --git a/beacon-chain/db/kv/BUILD.bazel b/beacon-chain/db/kv/BUILD.bazel index ca91da5aa506..ae459be7f623 100644 --- a/beacon-chain/db/kv/BUILD.bazel +++ b/beacon-chain/db/kv/BUILD.bazel @@ -44,6 +44,7 @@ go_library( "//proto/beacon/p2p/v1:go_default_library", "//shared/bytesutil:go_default_library", "//shared/fileutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/sliceutil:go_default_library", "//shared/traceutil:go_default_library", diff --git a/beacon-chain/db/kv/backup.go b/beacon-chain/db/kv/backup.go index 9e4aae901d3d..00112b2f1564 100644 --- a/beacon-chain/db/kv/backup.go +++ b/beacon-chain/db/kv/backup.go @@ -41,7 +41,7 @@ func (s *Store) Backup(ctx context.Context, outputDir string) error { if err := fileutil.MkdirAll(backupsDir); err != nil { return err } - backupPath := path.Join(backupsDir, fmt.Sprintf("prysm_beacondb_at_slot_%07d.backup", head.Block.Slot)) + backupPath := path.Join(backupsDir, fmt.Sprintf("prysm_beacondb_at_slot_%07d.backup", head.Block().Slot())) log.WithField("backup", backupPath).Info("Writing backup database.") copyDB, err := bolt.Open( diff --git a/beacon-chain/db/kv/blocks.go b/beacon-chain/db/kv/blocks.go index 1f5f388f16fa..36c690bc947b 100644 --- a/beacon-chain/db/kv/blocks.go +++ b/beacon-chain/db/kv/blocks.go @@ -5,6 +5,8 @@ import ( "context" "fmt" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -21,12 +23,12 @@ import ( var errInvalidSlotRange = errors.New("invalid end slot and start slot provided") // Block retrieval by root. -func (s *Store) Block(ctx context.Context, blockRoot [32]byte) (*ethpb.SignedBeaconBlock, error) { +func (s *Store) Block(ctx context.Context, blockRoot [32]byte) (interfaces.SignedBeaconBlock, error) { ctx, span := trace.StartSpan(ctx, "BeaconDB.Block") defer span.End() // Return block from cache if it exists. if v, ok := s.blockCache.Get(string(blockRoot[:])); v != nil && ok { - return v.(*ethpb.SignedBeaconBlock), nil + return v.(interfaces.SignedBeaconBlock), nil } var block *ethpb.SignedBeaconBlock err := s.db.View(func(tx *bolt.Tx) error { @@ -38,11 +40,11 @@ func (s *Store) Block(ctx context.Context, blockRoot [32]byte) (*ethpb.SignedBea block = ðpb.SignedBeaconBlock{} return decode(ctx, enc, block) }) - return block, err + return interfaces.NewWrappedSignedBeaconBlock(block), err } // HeadBlock returns the latest canonical block in eth2. -func (s *Store) HeadBlock(ctx context.Context) (*ethpb.SignedBeaconBlock, error) { +func (s *Store) HeadBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) { ctx, span := trace.StartSpan(ctx, "BeaconDB.HeadBlock") defer span.End() var headBlock *ethpb.SignedBeaconBlock @@ -59,14 +61,14 @@ func (s *Store) HeadBlock(ctx context.Context) (*ethpb.SignedBeaconBlock, error) headBlock = ðpb.SignedBeaconBlock{} return decode(ctx, enc, headBlock) }) - return headBlock, err + return interfaces.NewWrappedSignedBeaconBlock(headBlock), err } // Blocks retrieves a list of beacon blocks and its respective roots by filter criteria. -func (s *Store) Blocks(ctx context.Context, f *filters.QueryFilter) ([]*ethpb.SignedBeaconBlock, [][32]byte, error) { +func (s *Store) Blocks(ctx context.Context, f *filters.QueryFilter) ([]interfaces.SignedBeaconBlock, [][32]byte, error) { ctx, span := trace.StartSpan(ctx, "BeaconDB.Blocks") defer span.End() - blocks := make([]*ethpb.SignedBeaconBlock, 0) + blocks := make([]interfaces.SignedBeaconBlock, 0) blockRoots := make([][32]byte, 0) err := s.db.View(func(tx *bolt.Tx) error { @@ -83,7 +85,7 @@ func (s *Store) Blocks(ctx context.Context, f *filters.QueryFilter) ([]*ethpb.Si if err := decode(ctx, encoded, block); err != nil { return err } - blocks = append(blocks, block) + blocks = append(blocks, interfaces.NewWrappedSignedBeaconBlock(block)) blockRoots = append(blockRoots, bytesutil.ToBytes32(keys[i])) } return nil @@ -136,10 +138,10 @@ func (s *Store) HasBlock(ctx context.Context, blockRoot [32]byte) bool { } // BlocksBySlot retrieves a list of beacon blocks and its respective roots by slot. -func (s *Store) BlocksBySlot(ctx context.Context, slot types.Slot) (bool, []*ethpb.SignedBeaconBlock, error) { +func (s *Store) BlocksBySlot(ctx context.Context, slot types.Slot) (bool, []interfaces.SignedBeaconBlock, error) { ctx, span := trace.StartSpan(ctx, "BeaconDB.BlocksBySlot") defer span.End() - blocks := make([]*ethpb.SignedBeaconBlock, 0) + blocks := make([]interfaces.SignedBeaconBlock, 0) err := s.db.View(func(tx *bolt.Tx) error { bkt := tx.Bucket(blocksBucket) @@ -155,7 +157,7 @@ func (s *Store) BlocksBySlot(ctx context.Context, slot types.Slot) (bool, []*eth if err := decode(ctx, encoded, block); err != nil { return err } - blocks = append(blocks, block) + blocks = append(blocks, interfaces.NewWrappedSignedBeaconBlock(block)) } return nil }) @@ -198,7 +200,7 @@ func (s *Store) deleteBlock(ctx context.Context, blockRoot [32]byte) error { if err := decode(ctx, enc, block); err != nil { return err } - indicesByBucket := createBlockIndicesFromBlock(ctx, block.Block) + indicesByBucket := createBlockIndicesFromBlock(ctx, interfaces.NewWrappedBeaconBlock(block.Block)) if err := deleteValueForIndices(ctx, indicesByBucket, blockRoot[:], tx); err != nil { return errors.Wrap(err, "could not delete root for DB indices") } @@ -223,7 +225,7 @@ func (s *Store) deleteBlocks(ctx context.Context, blockRoots [][32]byte) error { if err := decode(ctx, enc, block); err != nil { return err } - indicesByBucket := createBlockIndicesFromBlock(ctx, block.Block) + indicesByBucket := createBlockIndicesFromBlock(ctx, interfaces.NewWrappedBeaconBlock(block.Block)) if err := deleteValueForIndices(ctx, indicesByBucket, blockRoot[:], tx); err != nil { return errors.Wrap(err, "could not delete root for DB indices") } @@ -237,10 +239,10 @@ func (s *Store) deleteBlocks(ctx context.Context, blockRoots [][32]byte) error { } // SaveBlock to the db. -func (s *Store) SaveBlock(ctx context.Context, signed *ethpb.SignedBeaconBlock) error { +func (s *Store) SaveBlock(ctx context.Context, signed interfaces.SignedBeaconBlock) error { ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveBlock") defer span.End() - blockRoot, err := signed.Block.HashTreeRoot() + blockRoot, err := signed.Block().HashTreeRoot() if err != nil { return err } @@ -248,18 +250,18 @@ func (s *Store) SaveBlock(ctx context.Context, signed *ethpb.SignedBeaconBlock) return nil } - return s.SaveBlocks(ctx, []*ethpb.SignedBeaconBlock{signed}) + return s.SaveBlocks(ctx, []interfaces.SignedBeaconBlock{signed}) } // SaveBlocks via bulk updates to the db. -func (s *Store) SaveBlocks(ctx context.Context, blocks []*ethpb.SignedBeaconBlock) error { +func (s *Store) SaveBlocks(ctx context.Context, blocks []interfaces.SignedBeaconBlock) error { ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveBlocks") defer span.End() return s.db.Update(func(tx *bolt.Tx) error { bkt := tx.Bucket(blocksBucket) for _, block := range blocks { - blockRoot, err := block.Block.HashTreeRoot() + blockRoot, err := block.Block().HashTreeRoot() if err != nil { return err } @@ -267,11 +269,11 @@ func (s *Store) SaveBlocks(ctx context.Context, blocks []*ethpb.SignedBeaconBloc if existingBlock := bkt.Get(blockRoot[:]); existingBlock != nil { continue } - enc, err := encode(ctx, block) + enc, err := encode(ctx, block.Proto()) if err != nil { return err } - indicesByBucket := createBlockIndicesFromBlock(ctx, block.Block) + indicesByBucket := createBlockIndicesFromBlock(ctx, block.Block()) if err := updateValueForIndices(ctx, indicesByBucket, blockRoot[:], tx); err != nil { return errors.Wrap(err, "could not update DB indices") } @@ -302,7 +304,7 @@ func (s *Store) SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) error } // GenesisBlock retrieves the genesis block of the beacon chain. -func (s *Store) GenesisBlock(ctx context.Context) (*ethpb.SignedBeaconBlock, error) { +func (s *Store) GenesisBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) { ctx, span := trace.StartSpan(ctx, "BeaconDB.GenesisBlock") defer span.End() var block *ethpb.SignedBeaconBlock @@ -316,7 +318,7 @@ func (s *Store) GenesisBlock(ctx context.Context) (*ethpb.SignedBeaconBlock, err block = ðpb.SignedBeaconBlock{} return decode(ctx, enc, block) }) - return block, err + return interfaces.NewWrappedSignedBeaconBlock(block), err } // SaveGenesisBlockRoot to the db. @@ -330,7 +332,7 @@ func (s *Store) SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) er } // HighestSlotBlocksBelow returns the block with the highest slot below the input slot from the db. -func (s *Store) HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([]*ethpb.SignedBeaconBlock, error) { +func (s *Store) HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([]interfaces.SignedBeaconBlock, error) { ctx, span := trace.StartSpan(ctx, "BeaconDB.HighestSlotBlocksBelow") defer span.End() @@ -357,7 +359,7 @@ func (s *Store) HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([] return nil, err } - var blk *ethpb.SignedBeaconBlock + var blk interfaces.SignedBeaconBlock var err error if best != nil { blk, err = s.Block(ctx, bytesutil.ToBytes32(best)) @@ -372,7 +374,7 @@ func (s *Store) HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([] } } - return []*ethpb.SignedBeaconBlock{blk}, nil + return []interfaces.SignedBeaconBlock{blk}, nil } // blockRootsByFilter retrieves the block roots given the filter criteria. @@ -524,7 +526,7 @@ func blockRootsBySlot(ctx context.Context, tx *bolt.Tx, slot types.Slot) ([][]by // createBlockIndicesFromBlock takes in a beacon block and returns // a map of bolt DB index buckets corresponding to each particular key for indices for // data, such as (shard indices bucket -> shard 5). -func createBlockIndicesFromBlock(ctx context.Context, block *ethpb.BeaconBlock) map[string][]byte { +func createBlockIndicesFromBlock(ctx context.Context, block interfaces.BeaconBlock) map[string][]byte { ctx, span := trace.StartSpan(ctx, "BeaconDB.createBlockIndicesFromBlock") defer span.End() indicesByBucket := make(map[string][]byte) @@ -534,11 +536,11 @@ func createBlockIndicesFromBlock(ctx context.Context, block *ethpb.BeaconBlock) blockSlotIndicesBucket, } indices := [][]byte{ - bytesutil.SlotToBytesBigEndian(block.Slot), + bytesutil.SlotToBytesBigEndian(block.Slot()), } - if block.ParentRoot != nil && len(block.ParentRoot) > 0 { + if block.ParentRoot() != nil && len(block.ParentRoot()) > 0 { buckets = append(buckets, blockParentRootIndicesBucket) - indices = append(indices, block.ParentRoot) + indices = append(indices, block.ParentRoot()) } for i := 0; i < len(buckets); i++ { indicesByBucket[string(buckets[i])] = indices[i] diff --git a/beacon-chain/db/kv/finalized_block_roots.go b/beacon-chain/db/kv/finalized_block_roots.go index 47ec1d627eef..690cd995a6c8 100644 --- a/beacon-chain/db/kv/finalized_block_roots.go +++ b/beacon-chain/db/kv/finalized_block_roots.go @@ -5,6 +5,8 @@ import ( "context" "fmt" + "github.com/prysmaticlabs/prysm/shared/interfaces" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db" @@ -83,15 +85,15 @@ func (s *Store) updateFinalizedBlockRoots(ctx context.Context, tx *bolt.Tx, chec traceutil.AnnotateError(span, err) return err } - if signedBlock == nil || signedBlock.Block == nil { + if signedBlock.IsNil() || signedBlock.Block().IsNil() { err := fmt.Errorf("missing block in database: block root=%#x", root) traceutil.AnnotateError(span, err) return err } - block := signedBlock.Block + block := signedBlock.Block() container := &dbpb.FinalizedBlockRootContainer{ - ParentRoot: block.ParentRoot, + ParentRoot: block.ParentRoot(), ChildRoot: previousRoot, } @@ -106,7 +108,7 @@ func (s *Store) updateFinalizedBlockRoots(ctx context.Context, tx *bolt.Tx, chec } // Found parent, loop exit condition. - if parentBytes := bkt.Get(block.ParentRoot); parentBytes != nil { + if parentBytes := bkt.Get(block.ParentRoot()); parentBytes != nil { parent := &dbpb.FinalizedBlockRootContainer{} if err := decode(ctx, parentBytes, parent); err != nil { traceutil.AnnotateError(span, err) @@ -118,14 +120,14 @@ func (s *Store) updateFinalizedBlockRoots(ctx context.Context, tx *bolt.Tx, chec traceutil.AnnotateError(span, err) return err } - if err := bkt.Put(block.ParentRoot, enc); err != nil { + if err := bkt.Put(block.ParentRoot(), enc); err != nil { traceutil.AnnotateError(span, err) return err } break } previousRoot = root - root = block.ParentRoot + root = block.ParentRoot() } // Upsert blocks from the current finalized epoch. @@ -182,7 +184,7 @@ func (s *Store) IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool { // FinalizedChildBlock returns the child block of a provided finalized block. If // no finalized block or its respective child block exists we return with a nil // block. -func (s *Store) FinalizedChildBlock(ctx context.Context, blockRoot [32]byte) (*ethpb.SignedBeaconBlock, error) { +func (s *Store) FinalizedChildBlock(ctx context.Context, blockRoot [32]byte) (interfaces.SignedBeaconBlock, error) { ctx, span := trace.StartSpan(ctx, "BeaconDB.FinalizedChildBlock") defer span.End() @@ -208,5 +210,5 @@ func (s *Store) FinalizedChildBlock(ctx context.Context, blockRoot [32]byte) (*e return decode(ctx, enc, blk) }) traceutil.AnnotateError(span, err) - return blk, err + return interfaces.NewWrappedSignedBeaconBlock(blk), err } diff --git a/beacon-chain/db/kv/genesis.go b/beacon-chain/db/kv/genesis.go index d876f0d736ef..c35870daddb1 100644 --- a/beacon-chain/db/kv/genesis.go +++ b/beacon-chain/db/kv/genesis.go @@ -7,6 +7,8 @@ import ( "io" "io/ioutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" dbIface "github.com/prysmaticlabs/prysm/beacon-chain/db/iface" @@ -27,7 +29,7 @@ func (s *Store) SaveGenesisData(ctx context.Context, genesisState iface.BeaconSt if err != nil { return errors.Wrap(err, "could not get genesis block root") } - if err := s.SaveBlock(ctx, genesisBlk); err != nil { + if err := s.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesisBlk)); err != nil { return errors.Wrap(err, "could not save genesis block") } if err := s.SaveState(ctx, genesisState, genesisBlkRoot); err != nil { diff --git a/beacon-chain/db/kv/state.go b/beacon-chain/db/kv/state.go index c9f63437a504..6987dc79b82a 100644 --- a/beacon-chain/db/kv/state.go +++ b/beacon-chain/db/kv/state.go @@ -4,6 +4,8 @@ import ( "bytes" "context" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -244,7 +246,7 @@ func slotByBlockRoot(ctx context.Context, tx *bolt.Tx, blockRoot []byte) (types. if err != nil { return 0, err } - if err := helpers.VerifyNilBeaconBlock(b); err != nil { + if err := helpers.VerifyNilBeaconBlock(interfaces.NewWrappedSignedBeaconBlock(b)); err != nil { return 0, err } return b.Block.Slot, nil diff --git a/beacon-chain/rpc/debug/block.go b/beacon-chain/rpc/debug/block.go index 13466a03d6e2..b87954feed00 100644 --- a/beacon-chain/rpc/debug/block.go +++ b/beacon-chain/rpc/debug/block.go @@ -64,7 +64,7 @@ func (ds *Server) GetInclusionSlot(ctx context.Context, req *pbrpc.InclusionSlot inclusionSlot := types.Slot(1<<64 - 1) targetStates := make(map[[32]byte]iface.ReadOnlyBeaconState) for _, blk := range blks { - for _, a := range blk.Block.Body.Attestations { + for _, a := range blk.Block().Body().Attestations() { tr := bytesutil.ToBytes32(a.Data.Target.Root) s, ok := targetStates[tr] if !ok { @@ -84,7 +84,7 @@ func (ds *Server) GetInclusionSlot(ctx context.Context, req *pbrpc.InclusionSlot } for _, i := range indices { if req.Id == i && req.Slot == a.Data.Slot { - inclusionSlot = blk.Block.Slot + inclusionSlot = blk.Block().Slot() break } } diff --git a/beacon-chain/state/stategen/BUILD.bazel b/beacon-chain/state/stategen/BUILD.bazel index dafb57affd26..5a7b75ef43ac 100644 --- a/beacon-chain/state/stategen/BUILD.bazel +++ b/beacon-chain/state/stategen/BUILD.bazel @@ -29,6 +29,7 @@ go_library( "//beacon-chain/state/interface:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "@com_github_hashicorp_golang_lru//:go_default_library", "@com_github_pkg_errors//:go_default_library", diff --git a/beacon-chain/state/stategen/getter.go b/beacon-chain/state/stategen/getter.go index 31c0c40c2434..f834f25eff7e 100644 --- a/beacon-chain/state/stategen/getter.go +++ b/beacon-chain/state/stategen/getter.go @@ -133,7 +133,7 @@ func (s *State) RecoverStateSummary(ctx context.Context, blockRoot [32]byte) (*p if err != nil { return nil, err } - summary := &pb.StateSummary{Slot: b.Block.Slot, Root: blockRoot[:]} + summary := &pb.StateSummary{Slot: b.Block().Slot(), Root: blockRoot[:]} if err := s.beaconDB.SaveStateSummary(ctx, summary); err != nil { return nil, err } @@ -257,7 +257,7 @@ func (s *State) lastAncestorState(ctx context.Context, root [32]byte) (iface.Bea return nil, ctx.Err() } // Is the state a genesis state. - parentRoot := bytesutil.ToBytes32(b.Block.ParentRoot) + parentRoot := bytesutil.ToBytes32(b.Block().ParentRoot()) if parentRoot == params.BeaconConfig().ZeroHash { return s.beaconDB.GenesisState(ctx) } diff --git a/beacon-chain/state/stategen/migrate.go b/beacon-chain/state/stategen/migrate.go index 095a60920670..baf37b6df39e 100644 --- a/beacon-chain/state/stategen/migrate.go +++ b/beacon-chain/state/stategen/migrate.go @@ -26,7 +26,7 @@ func (s *State) MigrateToCold(ctx context.Context, fRoot [32]byte) error { if err != nil { return err } - fSlot := fBlock.Block.Slot + fSlot := fBlock.Block().Slot() if oldFSlot > fSlot { return nil } @@ -64,7 +64,7 @@ func (s *State) MigrateToCold(ctx context.Context, fRoot [32]byte) error { if len(blks) != 1 { return errUnknownBlock } - missingRoot, err := blks[0].Block.HashTreeRoot() + missingRoot, err := blks[0].Block().HashTreeRoot() if err != nil { return err } diff --git a/beacon-chain/state/stategen/replay.go b/beacon-chain/state/stategen/replay.go index 0b150d1da6cd..2e14e93bd33d 100644 --- a/beacon-chain/state/stategen/replay.go +++ b/beacon-chain/state/stategen/replay.go @@ -4,9 +4,10 @@ import ( "context" "fmt" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" transition "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" @@ -18,7 +19,7 @@ import ( func (s *State) ReplayBlocks( ctx context.Context, state iface.BeaconState, - signed []*ethpb.SignedBeaconBlock, + signed []interfaces.SignedBeaconBlock, targetSlot types.Slot, ) (iface.BeaconState, error) { ctx, span := trace.StartSpan(ctx, "stateGen.ReplayBlocks") @@ -35,7 +36,7 @@ func (s *State) ReplayBlocks( break } // A node shouldn't process the block if the block slot is lower than the state slot. - if state.Slot() >= signed[i].Block.Slot { + if state.Slot() >= signed[i].Block().Slot() { continue } state, err = executeStateTransitionStateGen(ctx, state, signed[i]) @@ -58,7 +59,7 @@ func (s *State) ReplayBlocks( // LoadBlocks loads the blocks between start slot and end slot by recursively fetching from end block root. // The Blocks are returned in slot-descending order. -func (s *State) LoadBlocks(ctx context.Context, startSlot, endSlot types.Slot, endBlockRoot [32]byte) ([]*ethpb.SignedBeaconBlock, error) { +func (s *State) LoadBlocks(ctx context.Context, startSlot, endSlot types.Slot, endBlockRoot [32]byte) ([]interfaces.SignedBeaconBlock, error) { // Nothing to load for invalid range. if endSlot < startSlot { return nil, fmt.Errorf("start slot %d >= end slot %d", startSlot, endSlot) @@ -81,7 +82,7 @@ func (s *State) LoadBlocks(ctx context.Context, startSlot, endSlot types.Slot, e // The last retrieved block root has to match input end block root. // Covers the edge case if there's multiple blocks on the same end slot, // the end root may not be the last index in `blockRoots`. - for length >= 3 && blocks[length-1].Block.Slot == blocks[length-2].Block.Slot && blockRoots[length-1] != endBlockRoot { + for length >= 3 && blocks[length-1].Block().Slot() == blocks[length-2].Block().Slot() && blockRoots[length-1] != endBlockRoot { if ctx.Err() != nil { return nil, ctx.Err() } @@ -96,14 +97,14 @@ func (s *State) LoadBlocks(ctx context.Context, startSlot, endSlot types.Slot, e return nil, errors.New("end block roots don't match") } - filteredBlocks := []*ethpb.SignedBeaconBlock{blocks[length-1]} + filteredBlocks := []interfaces.SignedBeaconBlock{blocks[length-1]} // Starting from second to last index because the last block is already in the filtered block list. for i := length - 2; i >= 0; i-- { if ctx.Err() != nil { return nil, ctx.Err() } b := filteredBlocks[len(filteredBlocks)-1] - if bytesutil.ToBytes32(b.Block.ParentRoot) != blockRoots[i] { + if bytesutil.ToBytes32(b.Block().ParentRoot()) != blockRoots[i] { continue } filteredBlocks = append(filteredBlocks, blocks[i]) @@ -119,12 +120,12 @@ func (s *State) LoadBlocks(ctx context.Context, startSlot, endSlot types.Slot, e func executeStateTransitionStateGen( ctx context.Context, state iface.BeaconState, - signed *ethpb.SignedBeaconBlock, + signed interfaces.SignedBeaconBlock, ) (iface.BeaconState, error) { if ctx.Err() != nil { return nil, ctx.Err() } - if signed == nil || signed.Block == nil { + if signed.IsNil() || signed.Block().IsNil() { return nil, errUnknownBlock } @@ -134,7 +135,7 @@ func executeStateTransitionStateGen( // Execute per slots transition. // Given this is for state gen, a node uses the version process slots without skip slots cache. - state, err = processSlotsStateGen(ctx, state, signed.Block.Slot) + state, err = processSlotsStateGen(ctx, state, signed.Block().Slot()) if err != nil { return nil, errors.Wrap(err, "could not process slot") } @@ -214,15 +215,15 @@ func (s *State) lastSavedBlock(ctx context.Context, slot types.Slot) ([32]byte, if len(lastSaved) != 1 { return [32]byte{}, 0, fmt.Errorf("highest saved block does not equal to 1, it equals to %d", len(lastSaved)) } - if lastSaved[0] == nil || lastSaved[0].Block == nil { + if lastSaved[0].IsNil() || lastSaved[0].Block().IsNil() { return [32]byte{}, 0, nil } - r, err := lastSaved[0].Block.HashTreeRoot() + r, err := lastSaved[0].Block().HashTreeRoot() if err != nil { return [32]byte{}, 0, err } - return r, lastSaved[0].Block.Slot, nil + return r, lastSaved[0].Block().Slot(), nil } // This finds the last saved state in DB from searching backwards from input slot, @@ -259,12 +260,12 @@ func (s *State) genesisRoot(ctx context.Context) ([32]byte, error) { if err != nil { return [32]byte{}, err } - return b.Block.HashTreeRoot() + return b.Block().HashTreeRoot() } // Given the start slot and the end slot, this returns the finalized beacon blocks in between. // Since hot states don't have finalized blocks, this should ONLY be used for replaying cold state. -func (s *State) loadFinalizedBlocks(ctx context.Context, startSlot, endSlot types.Slot) ([]*ethpb.SignedBeaconBlock, error) { +func (s *State) loadFinalizedBlocks(ctx context.Context, startSlot, endSlot types.Slot) ([]interfaces.SignedBeaconBlock, error) { f := filters.NewFilter().SetStartSlot(startSlot).SetEndSlot(endSlot) bs, bRoots, err := s.beaconDB.Blocks(ctx, f) if err != nil { @@ -273,7 +274,7 @@ func (s *State) loadFinalizedBlocks(ctx context.Context, startSlot, endSlot type if len(bs) != len(bRoots) { return nil, errors.New("length of blocks and roots don't match") } - fbs := make([]*ethpb.SignedBeaconBlock, 0, len(bs)) + fbs := make([]interfaces.SignedBeaconBlock, 0, len(bs)) for i := len(bs) - 1; i >= 0; i-- { if s.beaconDB.IsFinalizedBlock(ctx, bRoots[i]) { fbs = append(fbs, bs[i]) diff --git a/shared/interfaces/BUILD.bazel b/shared/interfaces/BUILD.bazel index 5a321b840b25..babf13bd9b6b 100644 --- a/shared/interfaces/BUILD.bazel +++ b/shared/interfaces/BUILD.bazel @@ -12,5 +12,6 @@ go_library( "//shared/blockutil:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", + "@org_golang_google_protobuf//proto:go_default_library", ], ) diff --git a/shared/interfaces/block_interface.go b/shared/interfaces/block_interface.go index c08e0f745b12..d0c4bb793629 100644 --- a/shared/interfaces/block_interface.go +++ b/shared/interfaces/block_interface.go @@ -3,6 +3,7 @@ package interfaces import ( types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "google.golang.org/protobuf/proto" ) type SignedBeaconBlock interface { @@ -11,6 +12,7 @@ type SignedBeaconBlock interface { IsNil() bool Copy() WrappedSignedBeaconBlock MarshalSSZ() ([]byte, error) + Proto() proto.Message } type BeaconBlock interface { @@ -22,6 +24,7 @@ type BeaconBlock interface { IsNil() bool HashTreeRoot() ([32]byte, error) MarshalSSZ() ([]byte, error) + Proto() proto.Message } type BeaconBlockBody interface { RandaoReveal() []byte @@ -34,4 +37,5 @@ type BeaconBlockBody interface { VoluntaryExits() []*ethpb.SignedVoluntaryExit IsNil() bool HashTreeRoot() ([32]byte, error) + Proto() proto.Message } diff --git a/shared/interfaces/block_wrapper.go b/shared/interfaces/block_wrapper.go index 33d65d2d1611..83bb2d62f917 100644 --- a/shared/interfaces/block_wrapper.go +++ b/shared/interfaces/block_wrapper.go @@ -4,6 +4,7 @@ import ( types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/blockutil" + "google.golang.org/protobuf/proto" ) type WrappedSignedBeaconBlock struct { @@ -34,6 +35,10 @@ func (w WrappedSignedBeaconBlock) MarshalSSZ() ([]byte, error) { return w.b.MarshalSSZ() } +func (w WrappedSignedBeaconBlock) Proto() proto.Message { + return w.b +} + type WrappedBeaconBlock struct { b *ethpb.BeaconBlock } @@ -74,6 +79,10 @@ func (w WrappedBeaconBlock) MarshalSSZ() ([]byte, error) { return w.b.MarshalSSZ() } +func (w WrappedBeaconBlock) Proto() proto.Message { + return w.b +} + type WrappedBeaconBlockBody struct { b *ethpb.BeaconBlockBody } @@ -121,3 +130,7 @@ func (w WrappedBeaconBlockBody) IsNil() bool { func (w WrappedBeaconBlockBody) HashTreeRoot() ([32]byte, error) { return w.b.HashTreeRoot() } + +func (w WrappedBeaconBlockBody) Proto() proto.Message { + return w.b +} From 2fd5e66d9eb4dcea1dfb0917fbbc18ad3e8a326b Mon Sep 17 00:00:00 2001 From: nisdas Date: Fri, 21 May 2021 18:07:30 +0800 Subject: [PATCH 05/44] req/resp changes --- beacon-chain/blockchain/chain_info.go | 2 +- beacon-chain/blockchain/receive_block.go | 5 +- beacon-chain/core/feed/block/BUILD.bazel | 2 +- beacon-chain/core/feed/block/events.go | 6 +- beacon-chain/rpc/beaconv1/BUILD.bazel | 1 + beacon-chain/rpc/beaconv1/blocks.go | 47 ++++++++---- beacon-chain/rpc/beaconv1/state.go | 12 +-- beacon-chain/rpc/validator/BUILD.bazel | 1 + beacon-chain/rpc/validator/proposer.go | 17 +++-- beacon-chain/sync/BUILD.bazel | 1 + beacon-chain/sync/initial-sync/BUILD.bazel | 1 + .../sync/initial-sync/blocks_fetcher.go | 13 ++-- .../sync/initial-sync/blocks_fetcher_utils.go | 21 +++--- beacon-chain/sync/initial-sync/fsm.go | 7 +- beacon-chain/sync/pending_blocks_queue.go | 53 +++++++------- .../sync/rpc_beacon_blocks_by_range.go | 21 +++--- .../sync/rpc_beacon_blocks_by_root.go | 9 ++- beacon-chain/sync/rpc_chunked_response.go | 10 ++- beacon-chain/sync/rpc_send_request.go | 25 ++++--- beacon-chain/sync/rpc_status.go | 6 +- beacon-chain/sync/subscriber_beacon_blocks.go | 13 ++-- beacon-chain/sync/utils.go | 12 +-- beacon-chain/sync/validate_beacon_blocks.go | 73 ++++++++++--------- shared/interfaces/block_interface.go | 1 + shared/interfaces/block_wrapper.go | 4 + 25 files changed, 203 insertions(+), 160 deletions(-) diff --git a/beacon-chain/blockchain/chain_info.go b/beacon-chain/blockchain/chain_info.go index 5a9079987bb3..66e94e4d8f3e 100644 --- a/beacon-chain/blockchain/chain_info.go +++ b/beacon-chain/blockchain/chain_info.go @@ -42,7 +42,7 @@ type GenesisFetcher interface { type HeadFetcher interface { HeadSlot() types.Slot HeadRoot(ctx context.Context) ([]byte, error) - HeadBlock(ctx context.Context) (*ethpb.SignedBeaconBlock, error) + HeadBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) HeadState(ctx context.Context) (iface.BeaconState, error) HeadValidatorsIndices(ctx context.Context, epoch types.Epoch) ([]types.ValidatorIndex, error) HeadSeed(ctx context.Context, epoch types.Epoch) ([32]byte, error) diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index 5d5f2802a79c..60b6f2728b11 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -7,7 +7,6 @@ import ( "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "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" @@ -22,8 +21,8 @@ var epochsSinceFinalitySaveHotStateDB = types.Epoch(100) // BlockReceiver interface defines the methods of chain service receive and processing new blocks. type BlockReceiver interface { - ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlock, blockRoot [32]byte) error - ReceiveBlockBatch(ctx context.Context, blocks []*ethpb.SignedBeaconBlock, blkRoots [][32]byte) error + ReceiveBlock(ctx context.Context, block interfaces.SignedBeaconBlock, blockRoot [32]byte) error + ReceiveBlockBatch(ctx context.Context, blocks []interfaces.SignedBeaconBlock, blkRoots [][32]byte) error HasInitSyncBlock(root [32]byte) bool } diff --git a/beacon-chain/core/feed/block/BUILD.bazel b/beacon-chain/core/feed/block/BUILD.bazel index cd96c3280a04..a021e5f63fdd 100644 --- a/beacon-chain/core/feed/block/BUILD.bazel +++ b/beacon-chain/core/feed/block/BUILD.bazel @@ -10,6 +10,6 @@ go_library( visibility = ["//beacon-chain:__subpackages__"], deps = [ "//shared/event:go_default_library", - "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", + "//shared/interfaces:go_default_library", ], ) diff --git a/beacon-chain/core/feed/block/events.go b/beacon-chain/core/feed/block/events.go index d77615b55087..7ff8e16e9e2d 100644 --- a/beacon-chain/core/feed/block/events.go +++ b/beacon-chain/core/feed/block/events.go @@ -2,9 +2,7 @@ // during the runtime of a beacon node. package block -import ( - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" -) +import "github.com/prysmaticlabs/prysm/shared/interfaces" const ( // ReceivedBlock is sent after a block has been received by the beacon node via p2p or RPC. @@ -13,5 +11,5 @@ const ( // ReceivedBlockData is the data sent with ReceivedBlock events. type ReceivedBlockData struct { - SignedBlock *ethpb.SignedBeaconBlock + SignedBlock interfaces.SignedBeaconBlock } diff --git a/beacon-chain/rpc/beaconv1/BUILD.bazel b/beacon-chain/rpc/beaconv1/BUILD.bazel index 77a8db481921..bcee8b557ff1 100644 --- a/beacon-chain/rpc/beaconv1/BUILD.bazel +++ b/beacon-chain/rpc/beaconv1/BUILD.bazel @@ -32,6 +32,7 @@ go_library( "//proto/migration:go_default_library", "//shared/bytesutil:go_default_library", "//shared/featureconfig:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", "@com_github_pkg_errors//:go_default_library", diff --git a/beacon-chain/rpc/beaconv1/blocks.go b/beacon-chain/rpc/beaconv1/blocks.go index 1c2ed04732f1..66a18e8551a4 100644 --- a/beacon-chain/rpc/beaconv1/blocks.go +++ b/beacon-chain/rpc/beaconv1/blocks.go @@ -5,10 +5,11 @@ import ( "fmt" "strconv" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1" - ethpb_alpha "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" blockfeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/block" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" @@ -25,13 +26,17 @@ func (bs *Server) GetBlockHeader(ctx context.Context, req *ethpb.BlockRequest) ( ctx, span := trace.StartSpan(ctx, "beaconv1.GetBlockHeader") defer span.End() - blk, err := bs.blockFromBlockID(ctx, req.BlockId) + rBlk, err := bs.blockFromBlockID(ctx, req.BlockId) if err != nil { return nil, status.Errorf(codes.Internal, "Could not get block from block ID: %v", err) } - if blk == nil { + if rBlk.IsNil() { return nil, status.Errorf(codes.NotFound, "Could not find requested block header") } + blk, err := rBlk.RawPhase0Block() + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) + } v1BlockHdr, err := migration.V1Alpha1BlockToV1BlockHeader(blk) if err != nil { @@ -69,7 +74,7 @@ func (bs *Server) ListBlockHeaders(ctx context.Context, req *ethpb.BlockHeadersR defer span.End() var err error - var blks []*ethpb_alpha.SignedBeaconBlock + var blks []interfaces.SignedBeaconBlock var blkRoots [][32]byte if len(req.ParentRoot) == 32 { blks, blkRoots, err = bs.BeaconDB.Blocks(ctx, filters.NewFilter().SetParentRoot(req.ParentRoot)) @@ -95,7 +100,11 @@ func (bs *Server) ListBlockHeaders(ctx context.Context, req *ethpb.BlockHeadersR } blkHdrs := make([]*ethpb.BlockHeaderContainer, len(blks)) - for i, blk := range blks { + for i, bl := range blks { + blk, err := bl.RawPhase0Block() + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) + } blkHdr, err := migration.V1Alpha1BlockToV1BlockHeader(blk) if err != nil { return nil, status.Errorf(codes.Internal, "Could not get block header from block: %v", err) @@ -131,10 +140,11 @@ func (bs *Server) SubmitBlock(ctx context.Context, req *ethpb.BeaconBlockContain defer span.End() blk := req.Message - v1alpha1Block, err := migration.V1ToV1Alpha1Block(ðpb.SignedBeaconBlock{Block: blk, Signature: req.Signature}) + rBlock, err := migration.V1ToV1Alpha1Block(ðpb.SignedBeaconBlock{Block: blk, Signature: req.Signature}) if err != nil { return nil, status.Errorf(codes.Internal, "Could not convert block to v1") } + v1alpha1Block := interfaces.NewWrappedSignedBeaconBlock(rBlock) root, err := blk.HashTreeRoot() if err != nil { @@ -152,7 +162,7 @@ func (bs *Server) SubmitBlock(ctx context.Context, req *ethpb.BeaconBlockContain }() // Broadcast the new block to the network. - if err := bs.Broadcaster.Broadcast(ctx, v1alpha1Block); err != nil { + if err := bs.Broadcaster.Broadcast(ctx, v1alpha1Block.Proto()); err != nil { return nil, status.Errorf(codes.Internal, "Could not broadcast block: %v", err) } @@ -168,13 +178,17 @@ func (bs *Server) GetBlock(ctx context.Context, req *ethpb.BlockRequest) (*ethpb ctx, span := trace.StartSpan(ctx, "beaconv1.GetBlock") defer span.End() - blk, err := bs.blockFromBlockID(ctx, req.BlockId) + rBlk, err := bs.blockFromBlockID(ctx, req.BlockId) if err != nil { return nil, status.Errorf(codes.Internal, "Could not get block from block ID: %v", err) } - if blk == nil { + if rBlk.IsNil() { return nil, status.Errorf(codes.NotFound, "Could not find requested block") } + blk, err := rBlk.RawPhase0Block() + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) + } v1Block, err := migration.V1Alpha1ToV1Block(blk) if err != nil { @@ -216,7 +230,7 @@ func (bs *Server) GetBlockRoot(ctx context.Context, req *ethpb.BlockRequest) (*e if blk == nil { return nil, status.Error(codes.NotFound, "Could not find genesis block") } - blkRoot, err := blk.Block.HashTreeRoot() + blkRoot, err := blk.Block().HashTreeRoot() if err != nil { return nil, status.Error(codes.Internal, "Could not hash genesis block") } @@ -274,14 +288,19 @@ func (bs *Server) ListBlockAttestations(ctx context.Context, req *ethpb.BlockReq ctx, span := trace.StartSpan(ctx, "beaconv1.ListBlockAttestations") defer span.End() - blk, err := bs.blockFromBlockID(ctx, req.BlockId) + rBlk, err := bs.blockFromBlockID(ctx, req.BlockId) if err != nil { return nil, status.Errorf(codes.Internal, "Could not get block from block ID: %v", err) } - if blk == nil { + if rBlk.IsNil() { return nil, status.Errorf(codes.NotFound, "Could not find requested block") } + blk, err := rBlk.RawPhase0Block() + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) + } + v1Block, err := migration.V1Alpha1ToV1Block(blk) if err != nil { return nil, status.Errorf(codes.Internal, "Could not convert block to v1") @@ -291,9 +310,9 @@ func (bs *Server) ListBlockAttestations(ctx context.Context, req *ethpb.BlockReq }, nil } -func (bs *Server) blockFromBlockID(ctx context.Context, blockId []byte) (*ethpb_alpha.SignedBeaconBlock, error) { +func (bs *Server) blockFromBlockID(ctx context.Context, blockId []byte) (interfaces.SignedBeaconBlock, error) { var err error - var blk *ethpb_alpha.SignedBeaconBlock + var blk interfaces.SignedBeaconBlock switch string(blockId) { case "head": blk, err = bs.ChainInfoFetcher.HeadBlock(ctx) diff --git a/beacon-chain/rpc/beaconv1/state.go b/beacon-chain/rpc/beaconv1/state.go index bd867465f211..b52ade025bb1 100644 --- a/beacon-chain/rpc/beaconv1/state.go +++ b/beacon-chain/rpc/beaconv1/state.go @@ -161,7 +161,7 @@ func (bs *Server) headStateRoot(ctx context.Context) ([]byte, error) { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return nil, err } - return b.Block.StateRoot, nil + return b.Block().StateRoot(), nil } func (bs *Server) genesisStateRoot(ctx context.Context) ([]byte, error) { @@ -172,7 +172,7 @@ func (bs *Server) genesisStateRoot(ctx context.Context) ([]byte, error) { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return nil, err } - return b.Block.StateRoot, nil + return b.Block().StateRoot(), nil } func (bs *Server) finalizedStateRoot(ctx context.Context) ([]byte, error) { @@ -187,7 +187,7 @@ func (bs *Server) finalizedStateRoot(ctx context.Context) ([]byte, error) { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return nil, err } - return b.Block.StateRoot, nil + return b.Block().StateRoot(), nil } func (bs *Server) justifiedStateRoot(ctx context.Context) ([]byte, error) { @@ -202,7 +202,7 @@ func (bs *Server) justifiedStateRoot(ctx context.Context) ([]byte, error) { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return nil, err } - return b.Block.StateRoot, nil + return b.Block().StateRoot(), nil } func (bs *Server) stateRootByHex(ctx context.Context, stateId []byte) ([]byte, error) { @@ -235,10 +235,10 @@ func (bs *Server) stateRootBySlot(ctx context.Context, slot types.Slot) ([]byte, if len(blks) != 1 { return nil, errors.New("multiple blocks exist in same slot") } - if blks[0] == nil || blks[0].Block == nil { + if blks[0].IsNil() || blks[0].Block().IsNil() { return nil, errors.New("nil block") } - return blks[0].Block.StateRoot, nil + return blks[0].Block().StateRoot(), nil } func checkpoint(sourceCheckpoint *eth.Checkpoint) *ethpb.Checkpoint { diff --git a/beacon-chain/rpc/validator/BUILD.bazel b/beacon-chain/rpc/validator/BUILD.bazel index ce7231a87099..6e0040e0b37f 100644 --- a/beacon-chain/rpc/validator/BUILD.bazel +++ b/beacon-chain/rpc/validator/BUILD.bazel @@ -47,6 +47,7 @@ go_library( "//shared/depositutil:go_default_library", "//shared/featureconfig:go_default_library", "//shared/hashutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/rand:go_default_library", "//shared/slotutil:go_default_library", diff --git a/beacon-chain/rpc/validator/proposer.go b/beacon-chain/rpc/validator/proposer.go index 1a1f7b9bc693..0c3f8a5b704b 100644 --- a/beacon-chain/rpc/validator/proposer.go +++ b/beacon-chain/rpc/validator/proposer.go @@ -9,6 +9,8 @@ import ( "reflect" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + fastssz "github.com/ferranbt/fastssz" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" @@ -129,9 +131,9 @@ func (vs *Server) GetBlock(ctx context.Context, req *ethpb.BlockRequest) (*ethpb } // Compute state root with the newly constructed block. - stateRoot, err = vs.computeStateRoot(ctx, ðpb.SignedBeaconBlock{Block: blk, Signature: make([]byte, 96)}) + stateRoot, err = vs.computeStateRoot(ctx, interfaces.NewWrappedSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: blk, Signature: make([]byte, 96)})) if err != nil { - interop.WriteBlockToDisk(ðpb.SignedBeaconBlock{Block: blk}, true /*failed*/) + interop.WriteBlockToDisk(interfaces.NewWrappedSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: blk}), true /*failed*/) return nil, status.Errorf(codes.Internal, "Could not compute state root: %v", err) } blk.StateRoot = stateRoot @@ -141,8 +143,9 @@ func (vs *Server) GetBlock(ctx context.Context, req *ethpb.BlockRequest) (*ethpb // ProposeBlock is called by a proposer during its assigned slot to create a block in an attempt // to get it processed by the beacon node as the canonical head. -func (vs *Server) ProposeBlock(ctx context.Context, blk *ethpb.SignedBeaconBlock) (*ethpb.ProposeResponse, error) { - root, err := blk.Block.HashTreeRoot() +func (vs *Server) ProposeBlock(ctx context.Context, rBlk *ethpb.SignedBeaconBlock) (*ethpb.ProposeResponse, error) { + blk := interfaces.NewWrappedSignedBeaconBlock(rBlk) + root, err := blk.Block().HashTreeRoot() if err != nil { return nil, status.Errorf(codes.Internal, "Could not tree hash block: %v", err) } @@ -158,7 +161,7 @@ func (vs *Server) ProposeBlock(ctx context.Context, blk *ethpb.SignedBeaconBlock }() // Broadcast the new block to the network. - if err := vs.P2P.Broadcast(ctx, blk); err != nil { + if err := vs.P2P.Broadcast(ctx, blk.Proto()); err != nil { return nil, status.Errorf(codes.Internal, "Could not broadcast block: %v", err) } log.WithFields(logrus.Fields{ @@ -364,8 +367,8 @@ func (vs *Server) randomETH1DataVote(ctx context.Context) (*ethpb.Eth1Data, erro // computeStateRoot computes the state root after a block has been processed through a state transition and // returns it to the validator client. -func (vs *Server) computeStateRoot(ctx context.Context, block *ethpb.SignedBeaconBlock) ([]byte, error) { - beaconState, err := vs.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(block.Block.ParentRoot)) +func (vs *Server) computeStateRoot(ctx context.Context, block interfaces.SignedBeaconBlock) ([]byte, error) { + beaconState, err := vs.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(block.Block().ParentRoot())) if err != nil { return nil, errors.Wrap(err, "could not retrieve beacon state") } diff --git a/beacon-chain/sync/BUILD.bazel b/beacon-chain/sync/BUILD.bazel index f7c1ecec11da..7bda614edeb7 100644 --- a/beacon-chain/sync/BUILD.bazel +++ b/beacon-chain/sync/BUILD.bazel @@ -71,6 +71,7 @@ go_library( "//shared/abool:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/messagehandler:go_default_library", "//shared/mputil:go_default_library", "//shared/p2putils:go_default_library", diff --git a/beacon-chain/sync/initial-sync/BUILD.bazel b/beacon-chain/sync/initial-sync/BUILD.bazel index 7456f7561a8f..f2160d17144d 100644 --- a/beacon-chain/sync/initial-sync/BUILD.bazel +++ b/beacon-chain/sync/initial-sync/BUILD.bazel @@ -33,6 +33,7 @@ go_library( "//shared:go_default_library", "//shared/abool:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/mathutil:go_default_library", "//shared/params:go_default_library", "//shared/rand:go_default_library", diff --git a/beacon-chain/sync/initial-sync/blocks_fetcher.go b/beacon-chain/sync/initial-sync/blocks_fetcher.go index 65e2b553166f..cdd665efad1e 100644 --- a/beacon-chain/sync/initial-sync/blocks_fetcher.go +++ b/beacon-chain/sync/initial-sync/blocks_fetcher.go @@ -6,11 +6,12 @@ import ( "sync" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/kevinms/leakybucket-go" "github.com/libp2p/go-libp2p-core/peer" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" - eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/p2p" p2pTypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" @@ -103,7 +104,7 @@ type fetchRequestResponse struct { pid peer.ID start types.Slot count uint64 - blocks []*eth.SignedBeaconBlock + blocks []interfaces.SignedBeaconBlock err error } @@ -244,7 +245,7 @@ func (f *blocksFetcher) handleRequest(ctx context.Context, start types.Slot, cou response := &fetchRequestResponse{ start: start, count: count, - blocks: []*eth.SignedBeaconBlock{}, + blocks: []interfaces.SignedBeaconBlock{}, err: nil, } @@ -278,7 +279,7 @@ func (f *blocksFetcher) fetchBlocksFromPeer( ctx context.Context, start types.Slot, count uint64, peers []peer.ID, -) ([]*eth.SignedBeaconBlock, peer.ID, error) { +) ([]interfaces.SignedBeaconBlock, peer.ID, error) { ctx, span := trace.StartSpan(ctx, "initialsync.fetchBlocksFromPeer") defer span.End() @@ -302,7 +303,7 @@ func (f *blocksFetcher) requestBlocks( ctx context.Context, req *p2ppb.BeaconBlocksByRangeRequest, pid peer.ID, -) ([]*eth.SignedBeaconBlock, error) { +) ([]interfaces.SignedBeaconBlock, error) { if ctx.Err() != nil { return nil, ctx.Err() } @@ -331,7 +332,7 @@ func (f *blocksFetcher) requestBlocksByRoot( ctx context.Context, req *p2pTypes.BeaconBlockByRootsReq, pid peer.ID, -) ([]*eth.SignedBeaconBlock, error) { +) ([]interfaces.SignedBeaconBlock, error) { if ctx.Err() != nil { return nil, ctx.Err() } diff --git a/beacon-chain/sync/initial-sync/blocks_fetcher_utils.go b/beacon-chain/sync/initial-sync/blocks_fetcher_utils.go index d80781ace323..ee09aff01a76 100644 --- a/beacon-chain/sync/initial-sync/blocks_fetcher_utils.go +++ b/beacon-chain/sync/initial-sync/blocks_fetcher_utils.go @@ -5,10 +5,11 @@ import ( "fmt" "sort" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/libp2p/go-libp2p-core/peer" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" - eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" p2pTypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" @@ -24,7 +25,7 @@ import ( // either in DB or initial sync cache. type forkData struct { peer peer.ID - blocks []*eth.SignedBeaconBlock + blocks []interfaces.SignedBeaconBlock } // nonSkippedSlotAfter checks slots after the given one in an attempt to find a non-empty future slot. @@ -78,8 +79,8 @@ func (f *blocksFetcher) nonSkippedSlotAfterWithPeersTarget( } if len(blocks) > 0 { for _, block := range blocks { - if block.Block.Slot > slot { - return block.Block.Slot, nil + if block.Block().Slot() > slot { + return block.Block().Slot(), nil } } } @@ -233,11 +234,11 @@ func (f *blocksFetcher) findForkWithPeer(ctx context.Context, pid peer.ID, slot // Traverse blocks, and if we've got one that doesn't have parent in DB, backtrack on it. for i, block := range blocks { - parentRoot := bytesutil.ToBytes32(block.Block.ParentRoot) + parentRoot := bytesutil.ToBytes32(block.Block().ParentRoot()) if !f.db.HasBlock(ctx, parentRoot) && !f.chain.HasInitSyncBlock(parentRoot) { log.WithFields(logrus.Fields{ "peer": pid, - "slot": block.Block.Slot, + "slot": block.Block().Slot(), "root": fmt.Sprintf("%#x", parentRoot), }).Debug("Block with unknown parent root has been found") // Backtrack only if the first block is diverging, @@ -257,14 +258,14 @@ func (f *blocksFetcher) findForkWithPeer(ctx context.Context, pid peer.ID, slot } // findAncestor tries to figure out common ancestor slot that connects a given root to known block. -func (f *blocksFetcher) findAncestor(ctx context.Context, pid peer.ID, block *eth.SignedBeaconBlock) (*forkData, error) { - outBlocks := []*eth.SignedBeaconBlock{block} +func (f *blocksFetcher) findAncestor(ctx context.Context, pid peer.ID, block interfaces.SignedBeaconBlock) (*forkData, error) { + outBlocks := []interfaces.SignedBeaconBlock{block} for i := uint64(0); i < backtrackingMaxHops; i++ { - parentRoot := bytesutil.ToBytes32(outBlocks[len(outBlocks)-1].Block.ParentRoot) + parentRoot := bytesutil.ToBytes32(outBlocks[len(outBlocks)-1].Block().ParentRoot()) if f.db.HasBlock(ctx, parentRoot) || f.chain.HasInitSyncBlock(parentRoot) { // Common ancestor found, forward blocks back to processor. sort.Slice(outBlocks, func(i, j int) bool { - return outBlocks[i].Block.Slot < outBlocks[j].Block.Slot + return outBlocks[i].Block().Slot() < outBlocks[j].Block().Slot() }) return &forkData{ peer: pid, diff --git a/beacon-chain/sync/initial-sync/fsm.go b/beacon-chain/sync/initial-sync/fsm.go index 7df75064208a..51902c5db988 100644 --- a/beacon-chain/sync/initial-sync/fsm.go +++ b/beacon-chain/sync/initial-sync/fsm.go @@ -6,9 +6,10 @@ import ( "sort" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/libp2p/go-libp2p-core/peer" types "github.com/prysmaticlabs/eth2-types" - eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/shared/timeutils" ) @@ -46,7 +47,7 @@ type stateMachine struct { start types.Slot state stateID pid peer.ID - blocks []*eth.SignedBeaconBlock + blocks []interfaces.SignedBeaconBlock updated time.Time } @@ -78,7 +79,7 @@ func (smm *stateMachineManager) addStateMachine(startSlot types.Slot) *stateMach smm: smm, start: startSlot, state: stateNew, - blocks: []*eth.SignedBeaconBlock{}, + blocks: []interfaces.SignedBeaconBlock{}, updated: timeutils.Now(), } smm.recalculateMachineAttribs() diff --git a/beacon-chain/sync/pending_blocks_queue.go b/beacon-chain/sync/pending_blocks_queue.go index 3c24aecc9d00..e16350864da3 100644 --- a/beacon-chain/sync/pending_blocks_queue.go +++ b/beacon-chain/sync/pending_blocks_queue.go @@ -7,9 +7,10 @@ import ( "sync" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" "github.com/prysmaticlabs/prysm/shared/bytesutil" @@ -77,22 +78,22 @@ func (s *Service) processPendingBlocks(ctx context.Context) error { // Loop through the pending queue and mark the potential parent blocks as seen. for _, b := range bs { - if b == nil || b.Block == nil { + if b.IsNil() || b.Block().IsNil() { span.End() continue } s.pendingQueueLock.RLock() - inPendingQueue := s.seenPendingBlocks[bytesutil.ToBytes32(b.Block.ParentRoot)] + inPendingQueue := s.seenPendingBlocks[bytesutil.ToBytes32(b.Block().ParentRoot())] s.pendingQueueLock.RUnlock() - blkRoot, err := b.Block.HashTreeRoot() + blkRoot, err := b.Block().HashTreeRoot() if err != nil { traceutil.AnnotateError(span, err) span.End() return err } - parentIsBad := s.hasBadBlock(bytesutil.ToBytes32(b.Block.ParentRoot)) + parentIsBad := s.hasBadBlock(bytesutil.ToBytes32(b.Block().ParentRoot())) blockIsBad := s.hasBadBlock(blkRoot) // Check if parent is a bad block. if parentIsBad || blockIsBad { @@ -111,17 +112,17 @@ func (s *Service) processPendingBlocks(ctx context.Context) error { continue } - inDB := s.cfg.DB.HasBlock(ctx, bytesutil.ToBytes32(b.Block.ParentRoot)) + inDB := s.cfg.DB.HasBlock(ctx, bytesutil.ToBytes32(b.Block().ParentRoot())) hasPeer := len(pids) != 0 // Only request for missing parent block if it's not in DB, not in pending cache // and has peer in the peer list. if !inPendingQueue && !inDB && hasPeer { log.WithFields(logrus.Fields{ - "currentSlot": b.Block.Slot, - "parentRoot": hex.EncodeToString(bytesutil.Trunc(b.Block.ParentRoot)), + "currentSlot": b.Block().Slot(), + "parentRoot": hex.EncodeToString(bytesutil.Trunc(b.Block().ParentRoot())), }).Debug("Requesting parent block") - parentRoots = append(parentRoots, bytesutil.ToBytes32(b.Block.ParentRoot)) + parentRoots = append(parentRoots, bytesutil.ToBytes32(b.Block().ParentRoot())) span.End() continue @@ -133,7 +134,7 @@ func (s *Service) processPendingBlocks(ctx context.Context) error { } if err := s.validateBeaconBlock(ctx, b, blkRoot); err != nil { - log.Debugf("Could not validate block from slot %d: %v", b.Block.Slot, err) + log.Debugf("Could not validate block from slot %d: %v", b.Block().Slot(), err) s.setBadBlock(ctx, blkRoot) traceutil.AnnotateError(span, err) // In the next iteration of the queue, this block will be removed from @@ -143,7 +144,7 @@ func (s *Service) processPendingBlocks(ctx context.Context) error { } if err := s.cfg.Chain.ReceiveBlock(ctx, b, blkRoot); err != nil { - log.Debugf("Could not process block from slot %d: %v", b.Block.Slot, err) + log.Debugf("Could not process block from slot %d: %v", b.Block().Slot(), err) s.setBadBlock(ctx, blkRoot) traceutil.AnnotateError(span, err) // In the next iteration of the queue, this block will be removed from @@ -152,10 +153,10 @@ func (s *Service) processPendingBlocks(ctx context.Context) error { continue } - s.setSeenBlockIndexSlot(b.Block.Slot, b.Block.ProposerIndex) + s.setSeenBlockIndexSlot(b.Block().Slot(), b.Block().ProposerIndex()) // Broadcasting the block again once a node is able to process it. - if err := s.cfg.P2P.Broadcast(ctx, b); err != nil { + if err := s.cfg.P2P.Broadcast(ctx, b.Proto()); err != nil { log.WithError(err).Debug("Could not broadcast block") } @@ -257,8 +258,8 @@ func (s *Service) validatePendingSlots() error { for _, b := range blks { epoch := helpers.SlotToEpoch(slot) // remove all descendant blocks of old blocks - if oldBlockRoots[bytesutil.ToBytes32(b.Block.ParentRoot)] { - root, err := b.Block.HashTreeRoot() + if oldBlockRoots[bytesutil.ToBytes32(b.Block().ParentRoot())] { + root, err := b.Block().HashTreeRoot() if err != nil { return err } @@ -270,7 +271,7 @@ func (s *Service) validatePendingSlots() error { } // don't process old blocks if finalizedEpoch > 0 && epoch <= finalizedEpoch { - blkRoot, err := b.Block.HashTreeRoot() + blkRoot, err := b.Block().HashTreeRoot() if err != nil { return err } @@ -293,7 +294,7 @@ func (s *Service) clearPendingSlots() { // Delete block from the list from the pending queue using the slot as key. // Note: this helper is not thread safe. -func (s *Service) deleteBlockFromPendingQueue(slot types.Slot, b *ethpb.SignedBeaconBlock, r [32]byte) error { +func (s *Service) deleteBlockFromPendingQueue(slot types.Slot, b interfaces.SignedBeaconBlock, r [32]byte) error { mutexasserts.AssertRWMutexLocked(&s.pendingQueueLock) blks := s.pendingBlocksInCache(slot) @@ -301,7 +302,7 @@ func (s *Service) deleteBlockFromPendingQueue(slot types.Slot, b *ethpb.SignedBe return nil } - newBlks := make([]*ethpb.SignedBeaconBlock, 0, len(blks)) + newBlks := make([]interfaces.SignedBeaconBlock, 0, len(blks)) for _, blk := range blks { if sszutil.DeepEqual(blk, b) { continue @@ -324,7 +325,7 @@ func (s *Service) deleteBlockFromPendingQueue(slot types.Slot, b *ethpb.SignedBe // Insert block to the list in the pending queue using the slot as key. // Note: this helper is not thread safe. -func (s *Service) insertBlockToPendingQueue(slot types.Slot, b *ethpb.SignedBeaconBlock, r [32]byte) error { +func (s *Service) insertBlockToPendingQueue(slot types.Slot, b interfaces.SignedBeaconBlock, r [32]byte) error { mutexasserts.AssertRWMutexLocked(&s.pendingQueueLock) if s.seenPendingBlocks[r] { @@ -340,33 +341,33 @@ func (s *Service) insertBlockToPendingQueue(slot types.Slot, b *ethpb.SignedBeac } // This returns signed beacon blocks given input key from slotToPendingBlocks. -func (s *Service) pendingBlocksInCache(slot types.Slot) []*ethpb.SignedBeaconBlock { +func (s *Service) pendingBlocksInCache(slot types.Slot) []interfaces.SignedBeaconBlock { k := slotToCacheKey(slot) value, ok := s.slotToPendingBlocks.Get(k) if !ok { - return []*ethpb.SignedBeaconBlock{} + return []interfaces.SignedBeaconBlock{} } - blks, ok := value.([]*ethpb.SignedBeaconBlock) + blks, ok := value.([]interfaces.SignedBeaconBlock) if !ok { - return []*ethpb.SignedBeaconBlock{} + return []interfaces.SignedBeaconBlock{} } return blks } // This adds input signed beacon block to slotToPendingBlocks cache. -func (s *Service) addPendingBlockToCache(b *ethpb.SignedBeaconBlock) error { +func (s *Service) addPendingBlockToCache(b interfaces.SignedBeaconBlock) error { if err := helpers.VerifyNilBeaconBlock(b); err != nil { return err } - blks := s.pendingBlocksInCache(b.Block.Slot) + blks := s.pendingBlocksInCache(b.Block().Slot()) if len(blks) >= maxBlocksPerSlot { return nil } blks = append(blks, b) - k := slotToCacheKey(b.Block.Slot) + k := slotToCacheKey(b.Block().Slot()) s.slotToPendingBlocks.Set(k, blks, pendingBlockExpTime) return nil } diff --git a/beacon-chain/sync/rpc_beacon_blocks_by_range.go b/beacon-chain/sync/rpc_beacon_blocks_by_range.go index f343e36dc908..2e8d9af3ae8d 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_range.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_range.go @@ -4,10 +4,11 @@ import ( "context" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + libp2pcore "github.com/libp2p/go-libp2p-core" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" @@ -139,7 +140,7 @@ func (s *Service) writeBlockRangeToStream(ctx context.Context, startSlot, endSlo traceutil.AnnotateError(span, err) return err } - blks = append([]*ethpb.SignedBeaconBlock{genBlock}, blks...) + blks = append([]interfaces.SignedBeaconBlock{genBlock}, blks...) roots = append([][32]byte{genRoot}, roots...) } // Filter and sort our retrieved blocks, so that @@ -159,7 +160,7 @@ func (s *Service) writeBlockRangeToStream(ctx context.Context, startSlot, endSlo return err } for _, b := range blks { - if b == nil || b.Block == nil { + if b.IsNil() || b.Block().IsNil() { continue } if chunkErr := s.chunkWriter(stream, b); chunkErr != nil { @@ -207,22 +208,22 @@ func (s *Service) validateRangeRequest(r *pb.BeaconBlocksByRangeRequest) error { // filters all the provided blocks to ensure they are canonical // and are strictly linear. -func (s *Service) filterBlocks(ctx context.Context, blks []*ethpb.SignedBeaconBlock, roots [][32]byte, prevRoot *[32]byte, - step uint64, startSlot types.Slot) ([]*ethpb.SignedBeaconBlock, error) { +func (s *Service) filterBlocks(ctx context.Context, blks []interfaces.SignedBeaconBlock, roots [][32]byte, prevRoot *[32]byte, + step uint64, startSlot types.Slot) ([]interfaces.SignedBeaconBlock, error) { if len(blks) != len(roots) { return nil, errors.New("input blks and roots are diff lengths") } - newBlks := make([]*ethpb.SignedBeaconBlock, 0, len(blks)) + newBlks := make([]interfaces.SignedBeaconBlock, 0, len(blks)) for i, b := range blks { isCanonical, err := s.cfg.Chain.IsCanonical(ctx, roots[i]) if err != nil { return nil, err } parentValid := *prevRoot != [32]byte{} - isLinear := *prevRoot == bytesutil.ToBytes32(b.Block.ParentRoot) + isLinear := *prevRoot == bytesutil.ToBytes32(b.Block().ParentRoot()) isSingular := step == 1 - slotDiff, err := b.Block.Slot.SafeSubSlot(startSlot) + slotDiff, err := b.Block().Slot().SafeSubSlot(startSlot) if err != nil { return nil, err } @@ -250,12 +251,12 @@ func (s *Service) writeErrorResponseToStream(responseCode byte, reason string, s writeErrorResponseToStream(responseCode, reason, stream, s.cfg.P2P) } -func (s *Service) retrieveGenesisBlock(ctx context.Context) (*ethpb.SignedBeaconBlock, [32]byte, error) { +func (s *Service) retrieveGenesisBlock(ctx context.Context) (interfaces.SignedBeaconBlock, [32]byte, error) { genBlock, err := s.cfg.DB.GenesisBlock(ctx) if err != nil { return nil, [32]byte{}, err } - genRoot, err := genBlock.Block.HashTreeRoot() + genRoot, err := genBlock.Block().HashTreeRoot() if err != nil { return nil, [32]byte{}, err } diff --git a/beacon-chain/sync/rpc_beacon_blocks_by_root.go b/beacon-chain/sync/rpc_beacon_blocks_by_root.go index b7af04049efa..269c00a7d340 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_root.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_root.go @@ -3,10 +3,11 @@ package sync import ( "context" + "github.com/prysmaticlabs/prysm/shared/interfaces" + libp2pcore "github.com/libp2p/go-libp2p-core" "github.com/libp2p/go-libp2p-core/peer" "github.com/pkg/errors" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" "github.com/prysmaticlabs/prysm/shared/params" ) @@ -17,13 +18,13 @@ func (s *Service) sendRecentBeaconBlocksRequest(ctx context.Context, blockRoots ctx, cancel := context.WithTimeout(ctx, respTimeout) defer cancel() - _, err := SendBeaconBlocksByRootRequest(ctx, s.cfg.Chain, s.cfg.P2P, id, blockRoots, func(blk *ethpb.SignedBeaconBlock) error { - blkRoot, err := blk.Block.HashTreeRoot() + _, err := SendBeaconBlocksByRootRequest(ctx, s.cfg.Chain, s.cfg.P2P, id, blockRoots, func(blk interfaces.SignedBeaconBlock) error { + blkRoot, err := blk.Block().HashTreeRoot() if err != nil { return err } s.pendingQueueLock.Lock() - if err := s.insertBlockToPendingQueue(blk.Block.Slot, blk, blkRoot); err != nil { + if err := s.insertBlockToPendingQueue(blk.Block().Slot(), blk, blkRoot); err != nil { return err } s.pendingQueueLock.Unlock() diff --git a/beacon-chain/sync/rpc_chunked_response.go b/beacon-chain/sync/rpc_chunked_response.go index 92d517b1304d..2a0467d2faea 100644 --- a/beacon-chain/sync/rpc_chunked_response.go +++ b/beacon-chain/sync/rpc_chunked_response.go @@ -3,6 +3,8 @@ package sync import ( "errors" + "github.com/prysmaticlabs/prysm/shared/interfaces" + libp2pcore "github.com/libp2p/go-libp2p-core" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/blockchain" @@ -33,7 +35,7 @@ func WriteChunk(stream libp2pcore.Stream, chain blockchain.ChainInfoFetcher, enc // ReadChunkedBlock handles each response chunk that is sent by the // peer and converts it into a beacon block. -func ReadChunkedBlock(stream libp2pcore.Stream, chain blockchain.ChainInfoFetcher, p2p p2p.P2P, isFirstChunk bool) (*eth.SignedBeaconBlock, error) { +func ReadChunkedBlock(stream libp2pcore.Stream, chain blockchain.ChainInfoFetcher, p2p p2p.P2P, isFirstChunk bool) (interfaces.SignedBeaconBlock, error) { // Handle deadlines differently for first chunk if isFirstChunk { return readFirstChunkedBlock(stream, chain, p2p) @@ -42,12 +44,12 @@ func ReadChunkedBlock(stream libp2pcore.Stream, chain blockchain.ChainInfoFetche if err := readResponseChunk(stream, chain, p2p, blk); err != nil { return nil, err } - return blk, nil + return interfaces.NewWrappedSignedBeaconBlock(blk), nil } // readFirstChunkedBlock reads the first chunked block and applies the appropriate deadlines to // it. -func readFirstChunkedBlock(stream libp2pcore.Stream, chain blockchain.ChainInfoFetcher, p2p p2p.P2P) (*eth.SignedBeaconBlock, error) { +func readFirstChunkedBlock(stream libp2pcore.Stream, chain blockchain.ChainInfoFetcher, p2p p2p.P2P) (interfaces.SignedBeaconBlock, error) { blk := ð.SignedBeaconBlock{} code, errMsg, err := ReadStatusCode(stream, p2p.Encoding()) if err != nil { @@ -62,7 +64,7 @@ func readFirstChunkedBlock(stream libp2pcore.Stream, chain blockchain.ChainInfoF return nil, err } err = p2p.Encoding().DecodeWithMaxLength(stream, blk) - return blk, err + return interfaces.NewWrappedSignedBeaconBlock(blk), err } // readResponseChunk reads the response from the stream and decodes it into the diff --git a/beacon-chain/sync/rpc_send_request.go b/beacon-chain/sync/rpc_send_request.go index 4b66b46f3019..49837f8d3fcb 100644 --- a/beacon-chain/sync/rpc_send_request.go +++ b/beacon-chain/sync/rpc_send_request.go @@ -4,10 +4,11 @@ import ( "context" "io" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/libp2p/go-libp2p-core/peer" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/blockchain" "github.com/prysmaticlabs/prysm/beacon-chain/p2p" p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" @@ -20,13 +21,13 @@ var ErrInvalidFetchedData = errors.New("invalid data returned from peer") // BeaconBlockProcessor defines a block processing function, which allows to start utilizing // blocks even before all blocks are ready. -type BeaconBlockProcessor func(block *ethpb.SignedBeaconBlock) error +type BeaconBlockProcessor func(block interfaces.SignedBeaconBlock) error // SendBeaconBlocksByRangeRequest sends BeaconBlocksByRange and returns fetched blocks, if any. func SendBeaconBlocksByRangeRequest( ctx context.Context, chain blockchain.ChainInfoFetcher, p2pProvider p2p.P2P, pid peer.ID, req *pb.BeaconBlocksByRangeRequest, blockProcessor BeaconBlockProcessor, -) ([]*ethpb.SignedBeaconBlock, error) { +) ([]interfaces.SignedBeaconBlock, error) { stream, err := p2pProvider.Send(ctx, req, p2p.RPCBlocksByRangeTopicV1, pid) if err != nil { return nil, err @@ -34,8 +35,8 @@ func SendBeaconBlocksByRangeRequest( defer closeStream(stream, log) // Augment block processing function, if non-nil block processor is provided. - blocks := make([]*ethpb.SignedBeaconBlock, 0, req.Count) - process := func(blk *ethpb.SignedBeaconBlock) error { + blocks := make([]interfaces.SignedBeaconBlock, 0, req.Count) + process := func(blk interfaces.SignedBeaconBlock) error { blocks = append(blocks, blk) if blockProcessor != nil { return blockProcessor(blk) @@ -58,21 +59,21 @@ func SendBeaconBlocksByRangeRequest( return nil, ErrInvalidFetchedData } // Returned blocks MUST be in the slot range [start_slot, start_slot + count * step). - if blk.Block.Slot < req.StartSlot || blk.Block.Slot >= req.StartSlot.Add(req.Count*req.Step) { + if blk.Block().Slot() < req.StartSlot || blk.Block().Slot() >= req.StartSlot.Add(req.Count*req.Step) { return nil, ErrInvalidFetchedData } // Returned blocks, where they exist, MUST be sent in a consecutive order. // Consecutive blocks MUST have values in `step` increments (slots may be skipped in between). isSlotOutOfOrder := false - if prevSlot >= blk.Block.Slot { + if prevSlot >= blk.Block().Slot() { isSlotOutOfOrder = true - } else if req.Step != 0 && blk.Block.Slot.SubSlot(prevSlot).Mod(req.Step) != 0 { + } else if req.Step != 0 && blk.Block().Slot().SubSlot(prevSlot).Mod(req.Step) != 0 { isSlotOutOfOrder = true } if !isFirstChunk && isSlotOutOfOrder { return nil, ErrInvalidFetchedData } - prevSlot = blk.Block.Slot + prevSlot = blk.Block().Slot() if err := process(blk); err != nil { return nil, err } @@ -84,7 +85,7 @@ func SendBeaconBlocksByRangeRequest( func SendBeaconBlocksByRootRequest( ctx context.Context, chain blockchain.ChainInfoFetcher, p2pProvider p2p.P2P, pid peer.ID, req *p2ptypes.BeaconBlockByRootsReq, blockProcessor BeaconBlockProcessor, -) ([]*ethpb.SignedBeaconBlock, error) { +) ([]interfaces.SignedBeaconBlock, error) { stream, err := p2pProvider.Send(ctx, req, p2p.RPCBlocksByRootTopicV1, pid) if err != nil { return nil, err @@ -92,8 +93,8 @@ func SendBeaconBlocksByRootRequest( defer closeStream(stream, log) // Augment block processing function, if non-nil block processor is provided. - blocks := make([]*ethpb.SignedBeaconBlock, 0, len(*req)) - process := func(block *ethpb.SignedBeaconBlock) error { + blocks := make([]interfaces.SignedBeaconBlock, 0, len(*req)) + process := func(block interfaces.SignedBeaconBlock) error { blocks = append(blocks, block) if blockProcessor != nil { return blockProcessor(block) diff --git a/beacon-chain/sync/rpc_status.go b/beacon-chain/sync/rpc_status.go index 9fadad871492..8e320d5d0a45 100644 --- a/beacon-chain/sync/rpc_status.go +++ b/beacon-chain/sync/rpc_status.go @@ -318,7 +318,7 @@ func (s *Service) validateStatusMessage(ctx context.Context, msg *pb.Status) err if blk == nil { return p2ptypes.ErrGeneric } - if helpers.SlotToEpoch(blk.Block.Slot) == msg.FinalizedEpoch { + if helpers.SlotToEpoch(blk.Block().Slot()) == msg.FinalizedEpoch { return nil } @@ -326,7 +326,7 @@ func (s *Service) validateStatusMessage(ctx context.Context, msg *pb.Status) err if err != nil { return p2ptypes.ErrGeneric } - if startSlot > blk.Block.Slot { + if startSlot > blk.Block().Slot() { childBlock, err := s.cfg.DB.FinalizedChildBlock(ctx, bytesutil.ToBytes32(msg.FinalizedRoot)) if err != nil { return p2ptypes.ErrGeneric @@ -338,7 +338,7 @@ func (s *Service) validateStatusMessage(ctx context.Context, msg *pb.Status) err } // If child finalized block also has a smaller or // equal slot number we return an error. - if startSlot >= childBlock.Block.Slot { + if startSlot >= childBlock.Block().Slot() { return p2ptypes.ErrInvalidEpoch } return nil diff --git a/beacon-chain/sync/subscriber_beacon_blocks.go b/beacon-chain/sync/subscriber_beacon_blocks.go index 32922c7a749f..b84879109cf0 100644 --- a/beacon-chain/sync/subscriber_beacon_blocks.go +++ b/beacon-chain/sync/subscriber_beacon_blocks.go @@ -4,6 +4,8 @@ import ( "context" "errors" + "github.com/prysmaticlabs/prysm/shared/interfaces" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state/interop" @@ -11,18 +13,19 @@ import ( ) func (s *Service) beaconBlockSubscriber(ctx context.Context, msg proto.Message) error { - signed, ok := msg.(*ethpb.SignedBeaconBlock) + rBlock, ok := msg.(*ethpb.SignedBeaconBlock) if !ok { return errors.New("message is not type *ethpb.SignedBeaconBlock") } + signed := interfaces.NewWrappedSignedBeaconBlock(rBlock) - if signed == nil || signed.Block == nil { + if signed.IsNil() || signed.Block().IsNil() { return errors.New("nil block") } - s.setSeenBlockIndexSlot(signed.Block.Slot, signed.Block.ProposerIndex) + s.setSeenBlockIndexSlot(signed.Block().Slot(), signed.Block().ProposerIndex()) - block := signed.Block + block := signed.Block() root, err := block.HashTreeRoot() if err != nil { @@ -36,7 +39,7 @@ func (s *Service) beaconBlockSubscriber(ctx context.Context, msg proto.Message) } // Delete attestations from the block in the pool to avoid inclusion in future block. - if err := s.deleteAttsInPool(block.Body.Attestations); err != nil { + if err := s.deleteAttsInPool(block.Body().Attestations()); err != nil { log.Debugf("Could not delete attestations in pool: %v", err) return nil } diff --git a/beacon-chain/sync/utils.go b/beacon-chain/sync/utils.go index 5c7b3aba67c0..3d9a96ac7d09 100644 --- a/beacon-chain/sync/utils.go +++ b/beacon-chain/sync/utils.go @@ -4,20 +4,20 @@ import ( "errors" "sort" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/shared/interfaces" ) // A type to represent beacon blocks and roots which have methods // which satisfy the Interface in `Sort` so that this type can // be sorted in ascending order. type sortedObj struct { - blks []*ethpb.SignedBeaconBlock + blks []interfaces.SignedBeaconBlock roots [][32]byte } // Less reports whether the element with index i must sort before the element with index j. func (s sortedObj) Less(i, j int) bool { - return s.blks[i].Block.Slot < s.blks[j].Block.Slot + return s.blks[i].Block().Slot() < s.blks[j].Block().Slot() } // Swap swaps the elements with indexes i and j. @@ -32,14 +32,14 @@ func (s sortedObj) Len() int { } // removes duplicates from provided blocks and roots. -func (s *Service) dedupBlocksAndRoots(blks []*ethpb.SignedBeaconBlock, roots [][32]byte) ([]*ethpb.SignedBeaconBlock, [][32]byte, error) { +func (s *Service) dedupBlocksAndRoots(blks []interfaces.SignedBeaconBlock, roots [][32]byte) ([]interfaces.SignedBeaconBlock, [][32]byte, error) { if len(blks) != len(roots) { return nil, nil, errors.New("input blks and roots are diff lengths") } // Remove duplicate blocks received rootMap := make(map[[32]byte]bool, len(blks)) - newBlks := make([]*ethpb.SignedBeaconBlock, 0, len(blks)) + newBlks := make([]interfaces.SignedBeaconBlock, 0, len(blks)) newRoots := make([][32]byte, 0, len(roots)) for i, r := range roots { if rootMap[r] { @@ -67,7 +67,7 @@ func (s *Service) dedupRoots(roots [][32]byte) [][32]byte { // sort the provided blocks and roots in ascending order. This method assumes that the size of // block slice and root slice is equal. -func (s *Service) sortBlocksAndRoots(blks []*ethpb.SignedBeaconBlock, roots [][32]byte) ([]*ethpb.SignedBeaconBlock, [][32]byte) { +func (s *Service) sortBlocksAndRoots(blks []interfaces.SignedBeaconBlock, roots [][32]byte) ([]interfaces.SignedBeaconBlock, [][32]byte) { obj := sortedObj{ blks: blks, roots: roots, diff --git a/beacon-chain/sync/validate_beacon_blocks.go b/beacon-chain/sync/validate_beacon_blocks.go index ceaebcd220fc..2d8d53d3be59 100644 --- a/beacon-chain/sync/validate_beacon_blocks.go +++ b/beacon-chain/sync/validate_beacon_blocks.go @@ -6,6 +6,8 @@ import ( "fmt" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/libp2p/go-libp2p-core/peer" pubsub "github.com/libp2p/go-libp2p-pubsub" types "github.com/prysmaticlabs/eth2-types" @@ -52,13 +54,14 @@ func (s *Service) validateBeaconBlockPubSub(ctx context.Context, pid peer.ID, ms s.validateBlockLock.Lock() defer s.validateBlockLock.Unlock() - blk, ok := m.(*ethpb.SignedBeaconBlock) + rblk, ok := m.(*ethpb.SignedBeaconBlock) if !ok { log.WithError(errors.New("msg is not ethpb.SignedBeaconBlock")).Debug("Rejected block") return pubsub.ValidationReject } + blk := interfaces.NewWrappedSignedBeaconBlock(rblk) - if blk.Block == nil { + if blk.Block().IsNil() { log.WithError(errors.New("block.Block is nil")).Debug("Rejected block") return pubsub.ValidationReject } @@ -73,23 +76,23 @@ func (s *Service) validateBeaconBlockPubSub(ctx context.Context, pid peer.ID, ms }) // Verify the block is the first block received for the proposer for the slot. - if s.hasSeenBlockIndexSlot(blk.Block.Slot, blk.Block.ProposerIndex) { + if s.hasSeenBlockIndexSlot(blk.Block().Slot(), blk.Block().ProposerIndex()) { return pubsub.ValidationIgnore } - blockRoot, err := blk.Block.HashTreeRoot() + blockRoot, err := blk.Block().HashTreeRoot() if err != nil { - log.WithError(err).WithField("blockSlot", blk.Block.Slot).Debug("Ignored block") + log.WithError(err).WithField("blockSlot", blk.Block().Slot()).Debug("Ignored block") return pubsub.ValidationIgnore } if s.cfg.DB.HasBlock(ctx, blockRoot) { return pubsub.ValidationIgnore } // Check if parent is a bad block and then reject the block. - if s.hasBadBlock(bytesutil.ToBytes32(blk.Block.ParentRoot)) { + if s.hasBadBlock(bytesutil.ToBytes32(blk.Block().ParentRoot())) { s.setBadBlock(ctx, blockRoot) - e := fmt.Errorf("received block with root %#x that has an invalid parent %#x", blockRoot, blk.Block.ParentRoot) - log.WithError(e).WithField("blockSlot", blk.Block.Slot).Debug("Rejected block") + e := fmt.Errorf("received block with root %#x that has an invalid parent %#x", blockRoot, blk.Block().ParentRoot()) + log.WithError(e).WithField("blockSlot", blk.Block().Slot()).Debug("Rejected block") return pubsub.ValidationReject } @@ -100,85 +103,85 @@ func (s *Service) validateBeaconBlockPubSub(ctx context.Context, pid peer.ID, ms } s.pendingQueueLock.RUnlock() - if err := helpers.VerifySlotTime(uint64(s.cfg.Chain.GenesisTime().Unix()), blk.Block.Slot, params.BeaconNetworkConfig().MaximumGossipClockDisparity); err != nil { - log.WithError(err).WithField("blockSlot", blk.Block.Slot).Debug("Ignored block") + if err := helpers.VerifySlotTime(uint64(s.cfg.Chain.GenesisTime().Unix()), blk.Block().Slot(), params.BeaconNetworkConfig().MaximumGossipClockDisparity); err != nil { + log.WithError(err).WithField("blockSlot", blk.Block().Slot()).Debug("Ignored block") return pubsub.ValidationIgnore } // Add metrics for block arrival time subtracts slot start time. genesisTime := uint64(s.cfg.Chain.GenesisTime().Unix()) - if err := captureArrivalTimeMetric(genesisTime, blk.Block.Slot); err != nil { - log.WithError(err).WithField("blockSlot", blk.Block.Slot).Debug("Ignored block") + if err := captureArrivalTimeMetric(genesisTime, blk.Block().Slot()); err != nil { + log.WithError(err).WithField("blockSlot", blk.Block().Slot()).Debug("Ignored block") return pubsub.ValidationIgnore } startSlot, err := helpers.StartSlot(s.cfg.Chain.FinalizedCheckpt().Epoch) if err != nil { - log.WithError(err).WithField("blockSlot", blk.Block.Slot).Debug("Ignored block") + log.WithError(err).WithField("blockSlot", blk.Block().Slot()).Debug("Ignored block") return pubsub.ValidationIgnore } - if startSlot >= blk.Block.Slot { - e := fmt.Errorf("finalized slot %d greater or equal to block slot %d", startSlot, blk.Block.Slot) - log.WithError(e).WithField("blockSlot", blk.Block.Slot).Debug("Ignored block") + if startSlot >= blk.Block().Slot() { + e := fmt.Errorf("finalized slot %d greater or equal to block slot %d", startSlot, blk.Block().Slot()) + log.WithError(e).WithField("blockSlot", blk.Block().Slot()).Debug("Ignored block") return pubsub.ValidationIgnore } // Handle block when the parent is unknown. - if !s.cfg.DB.HasBlock(ctx, bytesutil.ToBytes32(blk.Block.ParentRoot)) { + if !s.cfg.DB.HasBlock(ctx, bytesutil.ToBytes32(blk.Block().ParentRoot())) { s.pendingQueueLock.Lock() - if err := s.insertBlockToPendingQueue(blk.Block.Slot, blk, blockRoot); err != nil { + if err := s.insertBlockToPendingQueue(blk.Block().Slot(), blk, blockRoot); err != nil { s.pendingQueueLock.Unlock() - log.WithError(err).WithField("blockSlot", blk.Block.Slot).Debug("Ignored block") + log.WithError(err).WithField("blockSlot", blk.Block().Slot()).Debug("Ignored block") return pubsub.ValidationIgnore } s.pendingQueueLock.Unlock() - log.WithError(errors.New("unknown parent")).WithField("blockSlot", blk.Block.Slot).Debug("Ignored block") + log.WithError(errors.New("unknown parent")).WithField("blockSlot", blk.Block().Slot()).Debug("Ignored block") return pubsub.ValidationIgnore } if err := s.validateBeaconBlock(ctx, blk, blockRoot); err != nil { - log.WithError(err).WithField("blockSlot", blk.Block.Slot).Warn("Rejected block") + log.WithError(err).WithField("blockSlot", blk.Block().Slot()).Warn("Rejected block") return pubsub.ValidationReject } // Record attribute of valid block. - span.AddAttributes(trace.Int64Attribute("slotInEpoch", int64(blk.Block.Slot%params.BeaconConfig().SlotsPerEpoch))) + span.AddAttributes(trace.Int64Attribute("slotInEpoch", int64(blk.Block().Slot()%params.BeaconConfig().SlotsPerEpoch))) msg.ValidatorData = blk // Used in downstream subscriber // Log the arrival time of the accepted block - startTime, err := helpers.SlotToTime(genesisTime, blk.Block.Slot) + startTime, err := helpers.SlotToTime(genesisTime, blk.Block().Slot()) if err != nil { - log.WithError(err).WithField("blockSlot", blk.Block.Slot).Debug("Couldn't get slot start time") + log.WithError(err).WithField("blockSlot", blk.Block().Slot()).Debug("Couldn't get slot start time") return pubsub.ValidationIgnore } log.WithFields(logrus.Fields{ - "blockSlot": blk.Block.Slot, + "blockSlot": blk.Block().Slot(), "sinceSlotStartTime": receivedTime.Sub(startTime), }).Debug("Received block") return pubsub.ValidationAccept } -func (s *Service) validateBeaconBlock(ctx context.Context, blk *ethpb.SignedBeaconBlock, blockRoot [32]byte) error { +func (s *Service) validateBeaconBlock(ctx context.Context, blk interfaces.SignedBeaconBlock, blockRoot [32]byte) error { ctx, span := trace.StartSpan(ctx, "sync.validateBeaconBlock") defer span.End() - if err := s.cfg.Chain.VerifyBlkDescendant(ctx, bytesutil.ToBytes32(blk.Block.ParentRoot)); err != nil { + if err := s.cfg.Chain.VerifyBlkDescendant(ctx, bytesutil.ToBytes32(blk.Block().ParentRoot())); err != nil { s.setBadBlock(ctx, blockRoot) return err } - hasStateSummaryDB := s.cfg.DB.HasStateSummary(ctx, bytesutil.ToBytes32(blk.Block.ParentRoot)) + hasStateSummaryDB := s.cfg.DB.HasStateSummary(ctx, bytesutil.ToBytes32(blk.Block().ParentRoot())) if !hasStateSummaryDB { - _, err := s.cfg.StateGen.RecoverStateSummary(ctx, bytesutil.ToBytes32(blk.Block.ParentRoot)) + _, err := s.cfg.StateGen.RecoverStateSummary(ctx, bytesutil.ToBytes32(blk.Block().ParentRoot())) if err != nil { return err } } - parentState, err := s.cfg.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(blk.Block.ParentRoot)) + parentState, err := s.cfg.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(blk.Block().ParentRoot())) if err != nil { return err } - if err := blocks.VerifyBlockSignature(parentState, blk.Block.ProposerIndex, blk.Signature, blk.Block.HashTreeRoot); err != nil { + if err := blocks.VerifyBlockSignature(parentState, blk.Block().ProposerIndex(), blk.Signature(), blk.Block().HashTreeRoot); err != nil { s.setBadBlock(ctx, blockRoot) return err } @@ -189,16 +192,16 @@ func (s *Service) validateBeaconBlock(ctx context.Context, blk *ethpb.SignedBeac // Seed for Next Epoch => Derived From Randao Mix at the end of the Previous Epoch. // Which is why we simply set the slot over here. nextEpoch := helpers.NextEpoch(parentState) - expectedEpoch := helpers.SlotToEpoch(blk.Block.Slot) + expectedEpoch := helpers.SlotToEpoch(blk.Block().Slot()) if expectedEpoch <= nextEpoch { - err = parentState.SetSlot(blk.Block.Slot) + err = parentState.SetSlot(blk.Block().Slot()) if err != nil { return err } } else { // In the event the block is more than an epoch ahead from its // parent state, we have to advance the state forward. - parentState, err = state.ProcessSlots(ctx, parentState, blk.Block.Slot) + parentState, err = state.ProcessSlots(ctx, parentState, blk.Block().Slot()) if err != nil { return err } @@ -207,7 +210,7 @@ func (s *Service) validateBeaconBlock(ctx context.Context, blk *ethpb.SignedBeac if err != nil { return err } - if blk.Block.ProposerIndex != idx { + if blk.Block().ProposerIndex() != idx { s.setBadBlock(ctx, blockRoot) return errors.New("incorrect proposer index") } diff --git a/shared/interfaces/block_interface.go b/shared/interfaces/block_interface.go index d0c4bb793629..334bd5ba8b91 100644 --- a/shared/interfaces/block_interface.go +++ b/shared/interfaces/block_interface.go @@ -13,6 +13,7 @@ type SignedBeaconBlock interface { Copy() WrappedSignedBeaconBlock MarshalSSZ() ([]byte, error) Proto() proto.Message + RawPhase0Block() (*ethpb.SignedBeaconBlock, error) } type BeaconBlock interface { diff --git a/shared/interfaces/block_wrapper.go b/shared/interfaces/block_wrapper.go index 83bb2d62f917..1a3e4bc3d023 100644 --- a/shared/interfaces/block_wrapper.go +++ b/shared/interfaces/block_wrapper.go @@ -39,6 +39,10 @@ func (w WrappedSignedBeaconBlock) Proto() proto.Message { return w.b } +func (w WrappedSignedBeaconBlock) RawPhase0Block() (*ethpb.SignedBeaconBlock, error) { + return w.b, nil +} + type WrappedBeaconBlock struct { b *ethpb.BeaconBlock } From e0b54c716faa74f583a16a3b6c5eb95a38088b57 Mon Sep 17 00:00:00 2001 From: nisdas Date: Fri, 21 May 2021 18:15:27 +0800 Subject: [PATCH 06/44] initial-sync --- beacon-chain/rpc/beacon/BUILD.bazel | 1 + beacon-chain/rpc/beacon/attestations.go | 9 ++-- .../sync/initial-sync/blocks_queue.go | 4 +- .../sync/initial-sync/blocks_queue_utils.go | 8 +-- beacon-chain/sync/initial-sync/round_robin.go | 52 +++++++++---------- 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/beacon-chain/rpc/beacon/BUILD.bazel b/beacon-chain/rpc/beacon/BUILD.bazel index a079afe3a6b6..c1e3d0699ceb 100644 --- a/beacon-chain/rpc/beacon/BUILD.bazel +++ b/beacon-chain/rpc/beacon/BUILD.bazel @@ -45,6 +45,7 @@ go_library( "//shared/cmd:go_default_library", "//shared/event:go_default_library", "//shared/featureconfig:go_default_library", + "//shared/interfaces:go_default_library", "//shared/pagination:go_default_library", "//shared/params:go_default_library", "//shared/sliceutil:go_default_library", diff --git a/beacon-chain/rpc/beacon/attestations.go b/beacon-chain/rpc/beacon/attestations.go index 03955302aec8..b1c44eb1385b 100644 --- a/beacon-chain/rpc/beacon/attestations.go +++ b/beacon-chain/rpc/beacon/attestations.go @@ -15,6 +15,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/cmd" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/pagination" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/slotutil" @@ -62,7 +63,7 @@ func (bs *Server) ListAttestations( return nil, status.Errorf(codes.InvalidArgument, "Requested page size %d can not be greater than max size %d", req.PageSize, cmd.Get().MaxRPCPageSize) } - var blocks []*ethpb.SignedBeaconBlock + var blocks []interfaces.SignedBeaconBlock var err error switch q := req.QueryFilter.(type) { case *ethpb.ListAttestationsRequest_GenesisEpoch: @@ -80,7 +81,7 @@ func (bs *Server) ListAttestations( } atts := make([]*ethpb.Attestation, 0, params.BeaconConfig().MaxAttestations*uint64(len(blocks))) for _, block := range blocks { - atts = append(atts, block.Block.Body.Attestations...) + atts = append(atts, block.Block().Body().Attestations()...) } // We sort attestations according to the Sortable interface. sort.Sort(sortableAttestations(atts)) @@ -116,7 +117,7 @@ func (bs *Server) ListAttestations( func (bs *Server) ListIndexedAttestations( ctx context.Context, req *ethpb.ListIndexedAttestationsRequest, ) (*ethpb.ListIndexedAttestationsResponse, error) { - var blocks []*ethpb.SignedBeaconBlock + var blocks []interfaces.SignedBeaconBlock var err error switch q := req.QueryFilter.(type) { case *ethpb.ListIndexedAttestationsRequest_GenesisEpoch: @@ -135,7 +136,7 @@ func (bs *Server) ListIndexedAttestations( attsArray := make([]*ethpb.Attestation, 0, params.BeaconConfig().MaxAttestations*uint64(len(blocks))) for _, block := range blocks { - attsArray = append(attsArray, block.Block.Body.Attestations...) + attsArray = append(attsArray, block.Block().Body().Attestations()...) } // We sort attestations according to the Sortable interface. sort.Sort(sortableAttestations(attsArray)) diff --git a/beacon-chain/sync/initial-sync/blocks_queue.go b/beacon-chain/sync/initial-sync/blocks_queue.go index 962e646dcf68..757565f31753 100644 --- a/beacon-chain/sync/initial-sync/blocks_queue.go +++ b/beacon-chain/sync/initial-sync/blocks_queue.go @@ -7,11 +7,11 @@ import ( "github.com/libp2p/go-libp2p-core/peer" types "github.com/prysmaticlabs/eth2-types" - eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/p2p" beaconsync "github.com/prysmaticlabs/prysm/beacon-chain/sync" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/sirupsen/logrus" ) @@ -89,7 +89,7 @@ type blocksQueue struct { // blocksQueueFetchedData is a data container that is returned from a queue on each step. type blocksQueueFetchedData struct { pid peer.ID - blocks []*eth.SignedBeaconBlock + blocks []interfaces.SignedBeaconBlock } // newBlocksQueue creates initialized priority queue. diff --git a/beacon-chain/sync/initial-sync/blocks_queue_utils.go b/beacon-chain/sync/initial-sync/blocks_queue_utils.go index b12b02b82d82..c9805eec25e4 100644 --- a/beacon-chain/sync/initial-sync/blocks_queue_utils.go +++ b/beacon-chain/sync/initial-sync/blocks_queue_utils.go @@ -17,8 +17,8 @@ func (q *blocksQueue) resetFromFork(ctx context.Context, fork *forkData) error { if len(fork.blocks) == 0 { return errors.New("no blocks to reset from") } - firstBlock := fork.blocks[0].Block - if firstBlock == nil { + firstBlock := fork.blocks[0].Block() + if firstBlock.IsNil() { return errors.New("invalid first block in fork data") } @@ -26,13 +26,13 @@ func (q *blocksQueue) resetFromFork(ctx context.Context, fork *forkData) error { if err := q.smm.removeAllStateMachines(); err != nil { return err } - fsm := q.smm.addStateMachine(firstBlock.Slot) + fsm := q.smm.addStateMachine(firstBlock.Slot()) fsm.pid = fork.peer fsm.blocks = fork.blocks fsm.state = stateDataParsed // The rest of machines are in skipped state. - startSlot := firstBlock.Slot.Add(uint64(len(fork.blocks))) + startSlot := firstBlock.Slot().Add(uint64(len(fork.blocks))) for i := startSlot; i < startSlot.Add(blocksPerRequest*(lookaheadSteps-1)); i += types.Slot(blocksPerRequest) { fsm := q.smm.addStateMachine(i) fsm.state = stateSkipped diff --git a/beacon-chain/sync/initial-sync/round_robin.go b/beacon-chain/sync/initial-sync/round_robin.go index 09b6b88dba0d..11e3450bf05d 100644 --- a/beacon-chain/sync/initial-sync/round_robin.go +++ b/beacon-chain/sync/initial-sync/round_robin.go @@ -11,10 +11,10 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "github.com/paulbellamy/ratecounter" types "github.com/prysmaticlabs/eth2-types" - eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/sirupsen/logrus" ) @@ -24,10 +24,10 @@ const ( ) // blockReceiverFn defines block receiving function. -type blockReceiverFn func(ctx context.Context, block *eth.SignedBeaconBlock, blockRoot [32]byte) error +type blockReceiverFn func(ctx context.Context, block interfaces.SignedBeaconBlock, blockRoot [32]byte) error // batchBlockReceiverFn defines batch receiving function. -type batchBlockReceiverFn func(ctx context.Context, blks []*eth.SignedBeaconBlock, roots [][32]byte) error +type batchBlockReceiverFn func(ctx context.Context, blks []interfaces.SignedBeaconBlock, roots [][32]byte) error // Round Robin sync looks at the latest peer statuses and syncs up to the highest known epoch. // @@ -178,14 +178,14 @@ func (s *Service) highestFinalizedEpoch() types.Epoch { } // logSyncStatus and increment block processing counter. -func (s *Service) logSyncStatus(genesis time.Time, blk *eth.BeaconBlock, blkRoot [32]byte) { +func (s *Service) logSyncStatus(genesis time.Time, blk interfaces.BeaconBlock, blkRoot [32]byte) { s.counter.Incr(1) rate := float64(s.counter.Rate()) / counterSeconds if rate == 0 { rate = 1 } - if helpers.IsEpochStart(blk.Slot) { - timeRemaining := time.Duration(float64(helpers.SlotsSince(genesis)-blk.Slot)/rate) * time.Second + if helpers.IsEpochStart(blk.Slot()) { + timeRemaining := time.Duration(float64(helpers.SlotsSince(genesis)-blk.Slot())/rate) * time.Second log.WithFields(logrus.Fields{ "peers": len(s.cfg.P2P.Peers().Connected()), "blocksPerSecond": fmt.Sprintf("%.1f", rate), @@ -198,21 +198,21 @@ func (s *Service) logSyncStatus(genesis time.Time, blk *eth.BeaconBlock, blkRoot } // logBatchSyncStatus and increments the block processing counter. -func (s *Service) logBatchSyncStatus(genesis time.Time, blks []*eth.SignedBeaconBlock, blkRoot [32]byte) { +func (s *Service) logBatchSyncStatus(genesis time.Time, blks []interfaces.SignedBeaconBlock, blkRoot [32]byte) { s.counter.Incr(int64(len(blks))) rate := float64(s.counter.Rate()) / counterSeconds if rate == 0 { rate = 1 } firstBlk := blks[0] - timeRemaining := time.Duration(float64(helpers.SlotsSince(genesis)-firstBlk.Block.Slot)/rate) * time.Second + timeRemaining := time.Duration(float64(helpers.SlotsSince(genesis)-firstBlk.Block().Slot())/rate) * time.Second log.WithFields(logrus.Fields{ "peers": len(s.cfg.P2P.Peers().Connected()), "blocksPerSecond": fmt.Sprintf("%.1f", rate), }).Infof( "Processing block batch of size %d starting from %s %d/%d - estimated time remaining %s", len(blks), fmt.Sprintf("0x%s...", hex.EncodeToString(blkRoot[:])[:8]), - firstBlk.Block.Slot, helpers.SlotsSince(genesis), timeRemaining, + firstBlk.Block().Slot, helpers.SlotsSince(genesis), timeRemaining, ) } @@ -220,61 +220,61 @@ func (s *Service) logBatchSyncStatus(genesis time.Time, blks []*eth.SignedBeacon func (s *Service) processBlock( ctx context.Context, genesis time.Time, - blk *eth.SignedBeaconBlock, + blk interfaces.SignedBeaconBlock, blockReceiver blockReceiverFn, ) error { - blkRoot, err := blk.Block.HashTreeRoot() + blkRoot, err := blk.Block().HashTreeRoot() if err != nil { return err } if s.isProcessedBlock(ctx, blk, blkRoot) { - return fmt.Errorf("slot: %d , root %#x: %w", blk.Block.Slot, blkRoot, errBlockAlreadyProcessed) + return fmt.Errorf("slot: %d , root %#x: %w", blk.Block().Slot(), blkRoot, errBlockAlreadyProcessed) } - s.logSyncStatus(genesis, blk.Block, blkRoot) - parentRoot := bytesutil.ToBytes32(blk.Block.ParentRoot) + s.logSyncStatus(genesis, blk.Block(), blkRoot) + parentRoot := bytesutil.ToBytes32(blk.Block().ParentRoot()) if !s.cfg.DB.HasBlock(ctx, parentRoot) && !s.cfg.Chain.HasInitSyncBlock(parentRoot) { - return fmt.Errorf("%w: %#x", errParentDoesNotExist, blk.Block.ParentRoot) + return fmt.Errorf("%w: %#x", errParentDoesNotExist, blk.Block().ParentRoot()) } return blockReceiver(ctx, blk, blkRoot) } func (s *Service) processBatchedBlocks(ctx context.Context, genesis time.Time, - blks []*eth.SignedBeaconBlock, bFunc batchBlockReceiverFn) error { + blks []interfaces.SignedBeaconBlock, bFunc batchBlockReceiverFn) error { if len(blks) == 0 { return errors.New("0 blocks provided into method") } firstBlock := blks[0] - blkRoot, err := firstBlock.Block.HashTreeRoot() + blkRoot, err := firstBlock.Block().HashTreeRoot() if err != nil { return err } headSlot := s.cfg.Chain.HeadSlot() - for headSlot >= firstBlock.Block.Slot && s.isProcessedBlock(ctx, firstBlock, blkRoot) { + for headSlot >= firstBlock.Block().Slot() && s.isProcessedBlock(ctx, firstBlock, blkRoot) { if len(blks) == 1 { return errors.New("no good blocks in batch") } blks = blks[1:] firstBlock = blks[0] - blkRoot, err = firstBlock.Block.HashTreeRoot() + blkRoot, err = firstBlock.Block().HashTreeRoot() if err != nil { return err } } s.logBatchSyncStatus(genesis, blks, blkRoot) - parentRoot := bytesutil.ToBytes32(firstBlock.Block.ParentRoot) + parentRoot := bytesutil.ToBytes32(firstBlock.Block().ParentRoot()) if !s.cfg.DB.HasBlock(ctx, parentRoot) && !s.cfg.Chain.HasInitSyncBlock(parentRoot) { - return fmt.Errorf("%w: %#x", errParentDoesNotExist, firstBlock.Block.ParentRoot) + return fmt.Errorf("%w: %#x", errParentDoesNotExist, firstBlock.Block().ParentRoot()) } blockRoots := make([][32]byte, len(blks)) blockRoots[0] = blkRoot for i := 1; i < len(blks); i++ { b := blks[i] - if !bytes.Equal(b.Block.ParentRoot, blockRoots[i-1][:]) { + if !bytes.Equal(b.Block().ParentRoot(), blockRoots[i-1][:]) { return fmt.Errorf("expected linear block list with parent root of %#x but received %#x", - blockRoots[i-1][:], b.Block.ParentRoot) + blockRoots[i-1][:], b.Block().ParentRoot()) } - blkRoot, err := b.Block.HashTreeRoot() + blkRoot, err := b.Block().HashTreeRoot() if err != nil { return err } @@ -299,12 +299,12 @@ func (s *Service) updatePeerScorerStats(pid peer.ID, startSlot types.Slot) { } // isProcessedBlock checks DB and local cache for presence of a given block, to avoid duplicates. -func (s *Service) isProcessedBlock(ctx context.Context, blk *eth.SignedBeaconBlock, blkRoot [32]byte) bool { +func (s *Service) isProcessedBlock(ctx context.Context, blk interfaces.SignedBeaconBlock, blkRoot [32]byte) bool { finalizedSlot, err := helpers.StartSlot(s.cfg.Chain.FinalizedCheckpt().Epoch) if err != nil { return false } - if blk.Block.Slot <= finalizedSlot || (s.cfg.DB.HasBlock(ctx, blkRoot) || s.cfg.Chain.HasInitSyncBlock(blkRoot)) { + if blk.Block().Slot() <= finalizedSlot || (s.cfg.DB.HasBlock(ctx, blkRoot) || s.cfg.Chain.HasInitSyncBlock(blkRoot)) { return true } return false From 1ff779a32a7b6fb8fad184eec81db991c9792606 Mon Sep 17 00:00:00 2001 From: nisdas Date: Fri, 21 May 2021 18:37:28 +0800 Subject: [PATCH 07/44] finally works --- beacon-chain/rpc/beacon/blocks.go | 63 +++++++++++++------ beacon-chain/rpc/validator/server.go | 6 +- beacon-chain/state/stategen/service.go | 7 ++- beacon-chain/sync/initial-sync/round_robin.go | 4 +- 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/beacon-chain/rpc/beacon/blocks.go b/beacon-chain/rpc/beacon/blocks.go index 5b4cf944a390..613da8f47ff7 100644 --- a/beacon-chain/rpc/beacon/blocks.go +++ b/beacon-chain/rpc/beacon/blocks.go @@ -59,7 +59,7 @@ func (bs *Server) ListBlocks( returnedBlks := blks[start:end] containers := make([]*ethpb.BeaconBlockContainer, len(returnedBlks)) for i, b := range returnedBlks { - root, err := b.Block.HashTreeRoot() + root, err := b.Block().HashTreeRoot() if err != nil { return nil, err } @@ -67,8 +67,12 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err) } + phBlk, err := b.RawPhase0Block() + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) + } containers[i] = ðpb.BeaconBlockContainer{ - Block: b, + Block: phBlk, BlockRoot: root[:], Canonical: canonical, } @@ -91,7 +95,7 @@ func (bs *Server) ListBlocks( NextPageToken: strconv.Itoa(0), }, nil } - root, err := blk.Block.HashTreeRoot() + root, err := blk.Block().HashTreeRoot() if err != nil { return nil, err } @@ -99,10 +103,13 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err) } - + phBlk, err := blk.RawPhase0Block() + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) + } return ðpb.ListBlocksResponse{ BlockContainers: []*ethpb.BeaconBlockContainer{{ - Block: blk, + Block: phBlk, BlockRoot: root[:], Canonical: canonical}, }, @@ -132,7 +139,7 @@ func (bs *Server) ListBlocks( returnedBlks := blks[start:end] containers := make([]*ethpb.BeaconBlockContainer, len(returnedBlks)) for i, b := range returnedBlks { - root, err := b.Block.HashTreeRoot() + root, err := b.Block().HashTreeRoot() if err != nil { return nil, err } @@ -140,8 +147,12 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err) } + phBlk, err := b.RawPhase0Block() + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) + } containers[i] = ðpb.BeaconBlockContainer{ - Block: b, + Block: phBlk, BlockRoot: root[:], Canonical: canonical, } @@ -160,13 +171,17 @@ func (bs *Server) ListBlocks( if genBlk == nil { return nil, status.Error(codes.Internal, "Could not find genesis block") } - root, err := genBlk.Block.HashTreeRoot() + root, err := genBlk.Block().HashTreeRoot() if err != nil { return nil, err } + phBlk, err := genBlk.RawPhase0Block() + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) + } containers := []*ethpb.BeaconBlockContainer{ { - Block: genBlk, + Block: phBlk, BlockRoot: root[:], Canonical: true, }, @@ -211,7 +226,12 @@ func (bs *Server) StreamBlocks(req *ethpb.StreamBlocksRequest, stream ethpb.Beac if !ok || data == nil { continue } - if err := stream.Send(data.SignedBlock); err != nil { + phBlk, err := data.SignedBlock.RawPhase0Block() + if err != nil { + log.Error(err) + continue + } + if err := stream.Send(phBlk); err != nil { return status.Errorf(codes.Unavailable, "Could not send over stream: %v", err) } } @@ -228,15 +248,20 @@ func (bs *Server) StreamBlocks(req *ethpb.StreamBlocksRequest, stream ethpb.Beac } headState, err := bs.HeadFetcher.HeadState(bs.Ctx) if err != nil { - log.WithError(err).WithField("blockSlot", data.SignedBlock.Block.Slot).Error("Could not get head state") + log.WithError(err).WithField("blockSlot", data.SignedBlock.Block().Slot()).Error("Could not get head state") continue } signed := data.SignedBlock - if err := blocks.VerifyBlockSignature(headState, signed.Block.ProposerIndex, signed.Signature, signed.Block.HashTreeRoot); err != nil { - log.WithError(err).WithField("blockSlot", data.SignedBlock.Block.Slot).Error("Could not verify block signature") + if err := blocks.VerifyBlockSignature(headState, signed.Block().ProposerIndex(), signed.Signature(), signed.Block().HashTreeRoot); err != nil { + log.WithError(err).WithField("blockSlot", data.SignedBlock.Block().Slot()).Error("Could not verify block signature") + continue + } + phBlk, err := signed.RawPhase0Block() + if err != nil { + log.Error(err) continue } - if err := stream.Send(data.SignedBlock); err != nil { + if err := stream.Send(phBlk); err != nil { return status.Errorf(codes.Unavailable, "Could not send over stream: %v", err) } } @@ -284,10 +309,10 @@ func (bs *Server) chainHeadRetrieval(ctx context.Context) (*ethpb.ChainHead, err if err != nil { return nil, status.Error(codes.Internal, "Could not get head block") } - if headBlock == nil || headBlock.Block == nil { + if headBlock.IsNil() || headBlock.Block().IsNil() { return nil, status.Error(codes.Internal, "Head block of chain was nil") } - headBlockRoot, err := headBlock.Block.HashTreeRoot() + headBlockRoot, err := headBlock.Block().HashTreeRoot() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get head block root: %v", err) } @@ -297,7 +322,7 @@ func (bs *Server) chainHeadRetrieval(ctx context.Context) (*ethpb.ChainHead, err } // Retrieve genesis block in the event we have genesis checkpoints. genBlock, err := bs.BeaconDB.GenesisBlock(ctx) - if err != nil || genBlock == nil || genBlock.Block == nil { + if err != nil || genBlock.IsNil() || genBlock.Block().IsNil() { return nil, status.Error(codes.Internal, "Could not get genesis block") } @@ -347,8 +372,8 @@ func (bs *Server) chainHeadRetrieval(ctx context.Context) (*ethpb.ChainHead, err return nil, err } return ðpb.ChainHead{ - HeadSlot: headBlock.Block.Slot, - HeadEpoch: helpers.SlotToEpoch(headBlock.Block.Slot), + HeadSlot: headBlock.Block().Slot(), + HeadEpoch: helpers.SlotToEpoch(headBlock.Block().Slot()), HeadBlockRoot: headBlockRoot[:], FinalizedSlot: fSlot, FinalizedEpoch: finalizedCheckpoint.Epoch, diff --git a/beacon-chain/rpc/validator/server.go b/beacon-chain/rpc/validator/server.go index 14f02dead564..5a32ebdb5b61 100644 --- a/beacon-chain/rpc/validator/server.go +++ b/beacon-chain/rpc/validator/server.go @@ -142,7 +142,11 @@ func (vs *Server) CanonicalHead(ctx context.Context, _ *emptypb.Empty) (*ethpb.S if err != nil { return nil, status.Errorf(codes.Internal, "Could not get head block: %v", err) } - return headBlk, nil + b, err := headBlk.RawPhase0Block() + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not get head block: %v", err) + } + return b, nil } // WaitForChainStart queries the logs of the Deposit Contract in order to verify the beacon chain diff --git a/beacon-chain/state/stategen/service.go b/beacon-chain/state/stategen/service.go index 7491a04e18ed..3575e02b7e87 100644 --- a/beacon-chain/state/stategen/service.go +++ b/beacon-chain/state/stategen/service.go @@ -8,8 +8,9 @@ import ( "errors" "sync" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" - eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -26,8 +27,8 @@ type StateManager interface { Resume(ctx context.Context) (iface.BeaconState, error) SaveFinalizedState(fSlot types.Slot, fRoot [32]byte, fState iface.BeaconState) MigrateToCold(ctx context.Context, fRoot [32]byte) error - ReplayBlocks(ctx context.Context, state iface.BeaconState, signed []*eth.SignedBeaconBlock, targetSlot types.Slot) (iface.BeaconState, error) - LoadBlocks(ctx context.Context, startSlot, endSlot types.Slot, endBlockRoot [32]byte) ([]*eth.SignedBeaconBlock, error) + ReplayBlocks(ctx context.Context, state iface.BeaconState, signed []interfaces.SignedBeaconBlock, targetSlot types.Slot) (iface.BeaconState, error) + LoadBlocks(ctx context.Context, startSlot, endSlot types.Slot, endBlockRoot [32]byte) ([]interfaces.SignedBeaconBlock, error) HasState(ctx context.Context, blockRoot [32]byte) (bool, error) HasStateInCache(ctx context.Context, blockRoot [32]byte) (bool, error) StateByRoot(ctx context.Context, blockRoot [32]byte) (iface.BeaconState, error) diff --git a/beacon-chain/sync/initial-sync/round_robin.go b/beacon-chain/sync/initial-sync/round_robin.go index 11e3450bf05d..59391a99b2bb 100644 --- a/beacon-chain/sync/initial-sync/round_robin.go +++ b/beacon-chain/sync/initial-sync/round_robin.go @@ -192,7 +192,7 @@ func (s *Service) logSyncStatus(genesis time.Time, blk interfaces.BeaconBlock, b }).Infof( "Processing block %s %d/%d - estimated time remaining %s", fmt.Sprintf("0x%s...", hex.EncodeToString(blkRoot[:])[:8]), - blk.Slot, helpers.SlotsSince(genesis), timeRemaining, + blk.Slot(), helpers.SlotsSince(genesis), timeRemaining, ) } } @@ -212,7 +212,7 @@ func (s *Service) logBatchSyncStatus(genesis time.Time, blks []interfaces.Signed }).Infof( "Processing block batch of size %d starting from %s %d/%d - estimated time remaining %s", len(blks), fmt.Sprintf("0x%s...", hex.EncodeToString(blkRoot[:])[:8]), - firstBlk.Block().Slot, helpers.SlotsSince(genesis), timeRemaining, + firstBlk.Block().Slot(), helpers.SlotsSince(genesis), timeRemaining, ) } From b32d16e589bd5f75b3d0404a1e18ac64cfde4e9f Mon Sep 17 00:00:00 2001 From: nisdas Date: Fri, 21 May 2021 20:07:11 +0800 Subject: [PATCH 08/44] fix error --- beacon-chain/sync/validate_beacon_blocks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/sync/validate_beacon_blocks.go b/beacon-chain/sync/validate_beacon_blocks.go index 2d8d53d3be59..f8940288d42c 100644 --- a/beacon-chain/sync/validate_beacon_blocks.go +++ b/beacon-chain/sync/validate_beacon_blocks.go @@ -145,7 +145,7 @@ func (s *Service) validateBeaconBlockPubSub(ctx context.Context, pid peer.ID, ms } // Record attribute of valid block. span.AddAttributes(trace.Int64Attribute("slotInEpoch", int64(blk.Block().Slot()%params.BeaconConfig().SlotsPerEpoch))) - msg.ValidatorData = blk // Used in downstream subscriber + msg.ValidatorData = rblk // Used in downstream subscriber // Log the arrival time of the accepted block startTime, err := helpers.SlotToTime(genesisTime, blk.Block().Slot()) From 96204c7feeb9f5c8369d3d1b3fa7183f1eb0cdf7 Mon Sep 17 00:00:00 2001 From: nisdas Date: Fri, 21 May 2021 20:11:50 +0800 Subject: [PATCH 09/44] fix bugs --- beacon-chain/blockchain/process_attestation_helpers.go | 2 +- beacon-chain/sync/rpc_beacon_blocks_by_range.go | 2 +- beacon-chain/sync/rpc_beacon_blocks_by_root.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/beacon-chain/blockchain/process_attestation_helpers.go b/beacon-chain/blockchain/process_attestation_helpers.go index 0105d698b6fa..3174b808b432 100644 --- a/beacon-chain/blockchain/process_attestation_helpers.go +++ b/beacon-chain/blockchain/process_attestation_helpers.go @@ -99,7 +99,7 @@ func (s *Service) verifyBeaconBlock(ctx context.Context, data *ethpb.Attestation } // If the block does not exist in db, check again if block exists in initial sync block cache. // This could happen as the node first syncs to head. - if b == nil && s.hasInitSyncBlock(r) { + if b.IsNil() && s.hasInitSyncBlock(r) { b = s.getInitSyncBlock(r) } if err := helpers.VerifyNilBeaconBlock(b); err != nil { diff --git a/beacon-chain/sync/rpc_beacon_blocks_by_range.go b/beacon-chain/sync/rpc_beacon_blocks_by_range.go index 2e8d9af3ae8d..deb24622bfa9 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_range.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_range.go @@ -163,7 +163,7 @@ func (s *Service) writeBlockRangeToStream(ctx context.Context, startSlot, endSlo if b.IsNil() || b.Block().IsNil() { continue } - if chunkErr := s.chunkWriter(stream, b); chunkErr != nil { + if chunkErr := s.chunkWriter(stream, b.Proto()); chunkErr != nil { log.WithError(chunkErr).Debug("Could not send a chunked response") s.writeErrorResponseToStream(responseCodeServerError, p2ptypes.ErrGeneric.Error(), stream) traceutil.AnnotateError(span, chunkErr) diff --git a/beacon-chain/sync/rpc_beacon_blocks_by_root.go b/beacon-chain/sync/rpc_beacon_blocks_by_root.go index 269c00a7d340..73bd6270c41b 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_root.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_root.go @@ -72,7 +72,7 @@ func (s *Service) beaconBlocksRootRPCHandler(ctx context.Context, msg interface{ if blk == nil { continue } - if err := s.chunkWriter(stream, blk); err != nil { + if err := s.chunkWriter(stream, blk.Proto()); err != nil { return err } } From 7b8aecc1678125bb920c075ac5c49f372fe3feda Mon Sep 17 00:00:00 2001 From: nisdas Date: Sat, 22 May 2021 08:15:43 +0800 Subject: [PATCH 10/44] fix issue --- beacon-chain/sync/rpc_beacon_blocks_by_root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/sync/rpc_beacon_blocks_by_root.go b/beacon-chain/sync/rpc_beacon_blocks_by_root.go index 73bd6270c41b..da8a3e1328cc 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_root.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_root.go @@ -69,7 +69,7 @@ func (s *Service) beaconBlocksRootRPCHandler(ctx context.Context, msg interface{ s.writeErrorResponseToStream(responseCodeServerError, types.ErrGeneric.Error(), stream) return err } - if blk == nil { + if blk.IsNil() { continue } if err := s.chunkWriter(stream, blk.Proto()); err != nil { From 588774b01bf047440fa8ee39f890f4997ddabef4 Mon Sep 17 00:00:00 2001 From: nisdas Date: Sat, 22 May 2021 12:26:41 +0800 Subject: [PATCH 11/44] fix issues --- beacon-chain/blockchain/chain_info.go | 2 +- beacon-chain/blockchain/service.go | 4 ++-- beacon-chain/core/state/transition_fuzz_test.go | 15 ++++++++------- beacon-chain/db/kv/backup.go | 2 +- beacon-chain/db/kv/blocks.go | 2 +- beacon-chain/db/kv/genesis.go | 2 +- beacon-chain/rpc/beacon/blocks.go | 4 ++-- beacon-chain/rpc/beaconv1/blocks.go | 6 +++--- beacon-chain/rpc/debug/block.go | 2 +- beacon-chain/state/stategen/getter.go | 4 ++-- beacon-chain/sync/initial-sync/BUILD.bazel | 1 - beacon-chain/sync/rpc_status.go | 4 ++-- shared/testutil/BUILD.bazel | 1 + shared/testutil/helpers.go | 3 ++- 14 files changed, 27 insertions(+), 25 deletions(-) diff --git a/beacon-chain/blockchain/chain_info.go b/beacon-chain/blockchain/chain_info.go index 66e94e4d8f3e..e377b4cdcd3d 100644 --- a/beacon-chain/blockchain/chain_info.go +++ b/beacon-chain/blockchain/chain_info.go @@ -124,7 +124,7 @@ func (s *Service) HeadRoot(ctx context.Context) ([]byte, error) { if err != nil { return nil, err } - if b == nil { + if b.IsNil() { return params.BeaconConfig().ZeroHash[:], nil } diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 50de2b2906f5..742edfbde0bd 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -120,7 +120,7 @@ func (s *Service) Start() { if err != nil { log.Fatalf("Could not fetch finalized cp: %v", err) } - if genesisBlock != nil { + if !genesisBlock.IsNil() { r, err = genesisBlock.Block().HashTreeRoot() if err != nil { log.Fatalf("Could not tree hash genesis block: %v", err) @@ -381,7 +381,7 @@ func (s *Service) initializeChainInfo(ctx context.Context) error { if err != nil { return errors.Wrap(err, "could not get genesis block from db") } - if genesisBlock == nil { + if genesisBlock.IsNil() { return errors.New("no genesis block in db") } genesisBlkRoot, err := genesisBlock.Block().HashTreeRoot() diff --git a/beacon-chain/core/state/transition_fuzz_test.go b/beacon-chain/core/state/transition_fuzz_test.go index b9e2afc6b612..2392dfe2a879 100644 --- a/beacon-chain/core/state/transition_fuzz_test.go +++ b/beacon-chain/core/state/transition_fuzz_test.go @@ -8,6 +8,7 @@ import ( types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" + "github.com/prysmaticlabs/prysm/shared/interfaces" ) func TestFuzzExecuteStateTransition_1000(t *testing.T) { @@ -21,7 +22,7 @@ func TestFuzzExecuteStateTransition_1000(t *testing.T) { for i := 0; i < 1000; i++ { fuzzer.Fuzz(state) fuzzer.Fuzz(sb) - s, err := ExecuteStateTransition(ctx, state, sb) + s, err := ExecuteStateTransition(ctx, state, interfaces.NewWrappedSignedBeaconBlock(sb)) if err != nil && s != nil { t.Fatalf("state should be nil on err. found: %v on error: %v for state: %v and signed block: %v", s, err, state, sb) } @@ -39,7 +40,7 @@ func TestFuzzCalculateStateRoot_1000(t *testing.T) { for i := 0; i < 1000; i++ { fuzzer.Fuzz(state) fuzzer.Fuzz(sb) - stateRoot, err := CalculateStateRoot(ctx, state, sb) + stateRoot, err := CalculateStateRoot(ctx, state, interfaces.NewWrappedSignedBeaconBlock(sb)) if err != nil && stateRoot != [32]byte{} { t.Fatalf("state root should be empty on err. found: %v on error: %v for signed block: %v", stateRoot, err, sb) } @@ -91,7 +92,7 @@ func TestFuzzProcessBlock_1000(t *testing.T) { for i := 0; i < 1000; i++ { fuzzer.Fuzz(state) fuzzer.Fuzz(sb) - s, err := ProcessBlock(ctx, state, sb) + s, err := ProcessBlock(ctx, state, interfaces.NewWrappedSignedBeaconBlock(sb)) if err != nil && s != nil { t.Fatalf("state should be nil on err. found: %v on error: %v for signed block: %v", s, err, sb) } @@ -109,7 +110,7 @@ func TestFuzzProcessOperations_1000(t *testing.T) { for i := 0; i < 1000; i++ { fuzzer.Fuzz(state) fuzzer.Fuzz(bb) - s, err := ProcessBlock(ctx, state, bb) + s, err := ProcessBlock(ctx, state, interfaces.NewWrappedSignedBeaconBlock(bb)) if err != nil && s != nil { t.Fatalf("state should be nil on err. found: %v on error: %v for block body: %v", s, err, bb) } @@ -127,7 +128,7 @@ func TestFuzzprocessOperationsNoVerify_1000(t *testing.T) { for i := 0; i < 1000; i++ { fuzzer.Fuzz(state) fuzzer.Fuzz(bb) - s, err := ProcessOperationsNoVerifyAttsSigs(ctx, state, bb) + s, err := ProcessOperationsNoVerifyAttsSigs(ctx, state, interfaces.NewWrappedSignedBeaconBlock(bb)) if err != nil && s != nil { t.Fatalf("state should be nil on err. found: %v on error: %v for block body: %v", s, err, bb) } @@ -144,7 +145,7 @@ func TestFuzzverifyOperationLengths_10000(t *testing.T) { for i := 0; i < 10000; i++ { fuzzer.Fuzz(state) fuzzer.Fuzz(bb) - _, err := VerifyOperationLengths(context.Background(), state, bb) + _, err := VerifyOperationLengths(context.Background(), state, interfaces.NewWrappedSignedBeaconBlock(bb)) _ = err } } @@ -188,7 +189,7 @@ func TestFuzzProcessBlockForStateRoot_1000(t *testing.T) { for i := 0; i < 1000; i++ { fuzzer.Fuzz(state) fuzzer.Fuzz(sb) - s, err := ProcessBlockForStateRoot(ctx, state, sb) + s, err := ProcessBlockForStateRoot(ctx, state, interfaces.NewWrappedSignedBeaconBlock(sb)) if err != nil && s != nil { t.Fatalf("state should be nil on err. found: %v on error: %v for signed block: %v", s, err, sb) } diff --git a/beacon-chain/db/kv/backup.go b/beacon-chain/db/kv/backup.go index 00112b2f1564..974948509bab 100644 --- a/beacon-chain/db/kv/backup.go +++ b/beacon-chain/db/kv/backup.go @@ -34,7 +34,7 @@ func (s *Store) Backup(ctx context.Context, outputDir string) error { if err != nil { return err } - if head == nil { + if head.IsNil() { return errors.New("no head block") } // Ensure the backups directory exists. diff --git a/beacon-chain/db/kv/blocks.go b/beacon-chain/db/kv/blocks.go index 36c690bc947b..3a9fde5a4a9e 100644 --- a/beacon-chain/db/kv/blocks.go +++ b/beacon-chain/db/kv/blocks.go @@ -367,7 +367,7 @@ func (s *Store) HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([] return nil, err } } - if blk == nil { + if blk.IsNil() { blk, err = s.GenesisBlock(ctx) if err != nil { return nil, err diff --git a/beacon-chain/db/kv/genesis.go b/beacon-chain/db/kv/genesis.go index c35870daddb1..df8430c83099 100644 --- a/beacon-chain/db/kv/genesis.go +++ b/beacon-chain/db/kv/genesis.go @@ -103,7 +103,7 @@ func (s *Store) EnsureEmbeddedGenesis(ctx context.Context) error { if err != nil { return err } - if gb != nil { + if !gb.IsNil() { return nil } gs, err := s.GenesisState(ctx) diff --git a/beacon-chain/rpc/beacon/blocks.go b/beacon-chain/rpc/beacon/blocks.go index 613da8f47ff7..368211741ca7 100644 --- a/beacon-chain/rpc/beacon/blocks.go +++ b/beacon-chain/rpc/beacon/blocks.go @@ -88,7 +88,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not retrieve block: %v", err) } - if blk == nil { + if blk.IsNil() { return ðpb.ListBlocksResponse{ BlockContainers: make([]*ethpb.BeaconBlockContainer, 0), TotalSize: 0, @@ -168,7 +168,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not retrieve blocks for genesis slot: %v", err) } - if genBlk == nil { + if genBlk.IsNil() { return nil, status.Error(codes.Internal, "Could not find genesis block") } root, err := genBlk.Block().HashTreeRoot() diff --git a/beacon-chain/rpc/beaconv1/blocks.go b/beacon-chain/rpc/beaconv1/blocks.go index 66a18e8551a4..fb0380e948d0 100644 --- a/beacon-chain/rpc/beaconv1/blocks.go +++ b/beacon-chain/rpc/beaconv1/blocks.go @@ -95,7 +95,7 @@ func (bs *Server) ListBlockHeaders(ctx context.Context, req *ethpb.BlockHeadersR return nil, status.Errorf(codes.Internal, "Could not retrieve block roots for slot %d: %v", req.Slot, err) } } - if blks == nil { + if len(blks) == 0 { return nil, status.Error(codes.NotFound, "Could not find requested blocks") } @@ -227,7 +227,7 @@ func (bs *Server) GetBlockRoot(ctx context.Context, req *ethpb.BlockRequest) (*e if err != nil { return nil, status.Errorf(codes.Internal, "Could not retrieve blocks for genesis slot: %v", err) } - if blk == nil { + if blk.IsNil() { return nil, status.Error(codes.NotFound, "Could not find genesis block") } blkRoot, err := blk.Block().HashTreeRoot() @@ -241,7 +241,7 @@ func (bs *Server) GetBlockRoot(ctx context.Context, req *ethpb.BlockRequest) (*e if err != nil { return nil, status.Errorf(codes.Internal, "Could not retrieve block for block root %#x: %v", req.BlockId, err) } - if block == nil { + if block.IsNil() { return nil, status.Error(codes.NotFound, "Could not find any blocks with given root") } diff --git a/beacon-chain/rpc/debug/block.go b/beacon-chain/rpc/debug/block.go index b87954feed00..e2fb646eaab6 100644 --- a/beacon-chain/rpc/debug/block.go +++ b/beacon-chain/rpc/debug/block.go @@ -26,7 +26,7 @@ func (ds *Server) GetBlock( if err != nil { return nil, status.Errorf(codes.Internal, "Could not retrieve block by root: %v", err) } - if signedBlock == nil { + if signedBlock.IsNil() { return &pbrpc.SSZResponse{Encoded: make([]byte, 0)}, nil } encoded, err := signedBlock.MarshalSSZ() diff --git a/beacon-chain/state/stategen/getter.go b/beacon-chain/state/stategen/getter.go index f834f25eff7e..66ee2392f459 100644 --- a/beacon-chain/state/stategen/getter.go +++ b/beacon-chain/state/stategen/getter.go @@ -248,7 +248,7 @@ func (s *State) lastAncestorState(ctx context.Context, root [32]byte) (iface.Bea if err != nil { return nil, err } - if b == nil { + if b.IsNil() { return nil, errUnknownBlock } @@ -289,7 +289,7 @@ func (s *State) lastAncestorState(ctx context.Context, root [32]byte) (iface.Bea if err != nil { return nil, err } - if b == nil { + if b.IsNil() { return nil, errUnknownBlock } } diff --git a/beacon-chain/sync/initial-sync/BUILD.bazel b/beacon-chain/sync/initial-sync/BUILD.bazel index f2160d17144d..8cf04c05c9d2 100644 --- a/beacon-chain/sync/initial-sync/BUILD.bazel +++ b/beacon-chain/sync/initial-sync/BUILD.bazel @@ -43,7 +43,6 @@ go_library( "@com_github_paulbellamy_ratecounter//:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", - "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@io_opencensus_go//trace:go_default_library", ], diff --git a/beacon-chain/sync/rpc_status.go b/beacon-chain/sync/rpc_status.go index 8e320d5d0a45..b5cea63db64c 100644 --- a/beacon-chain/sync/rpc_status.go +++ b/beacon-chain/sync/rpc_status.go @@ -315,7 +315,7 @@ func (s *Service) validateStatusMessage(ctx context.Context, msg *pb.Status) err if err != nil { return p2ptypes.ErrGeneric } - if blk == nil { + if blk.IsNil() { return p2ptypes.ErrGeneric } if helpers.SlotToEpoch(blk.Block().Slot()) == msg.FinalizedEpoch { @@ -333,7 +333,7 @@ func (s *Service) validateStatusMessage(ctx context.Context, msg *pb.Status) err } // Is a valid finalized block if no // other child blocks exist yet. - if childBlock == nil { + if childBlock.IsNil() { return nil } // If child finalized block also has a smaller or diff --git a/shared/testutil/BUILD.bazel b/shared/testutil/BUILD.bazel index 21d6c3373950..da361160b40e 100644 --- a/shared/testutil/BUILD.bazel +++ b/shared/testutil/BUILD.bazel @@ -24,6 +24,7 @@ go_library( "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", "//shared/hashutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/interop:go_default_library", "//shared/params:go_default_library", "//shared/rand:go_default_library", diff --git a/shared/testutil/helpers.go b/shared/testutil/helpers.go index 3f7c108c6b28..0d7557f6b8f3 100644 --- a/shared/testutil/helpers.go +++ b/shared/testutil/helpers.go @@ -12,6 +12,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/state" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/bls" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/rand" ) @@ -38,7 +39,7 @@ func BlockSignature( privKeys []bls.SecretKey, ) (bls.Signature, error) { var err error - s, err := state.CalculateStateRoot(context.Background(), bState, ðpb.SignedBeaconBlock{Block: block}) + s, err := state.CalculateStateRoot(context.Background(), bState, interfaces.NewWrappedSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: block})) if err != nil { return nil, err } From ee33eea97b11ec6f3e915ce1a08c390be659b40c Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 25 May 2021 00:51:40 +0800 Subject: [PATCH 12/44] fix refs --- beacon-chain/blockchain/service.go | 4 ++-- beacon-chain/core/helpers/weak_subjectivity.go | 2 +- beacon-chain/core/state/transition.go | 4 ++-- beacon-chain/powchain/log_processing.go | 2 +- beacon-chain/sync/initial-sync/service.go | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index de8d8d12e760..df2323f3bcfd 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -338,7 +338,7 @@ func (s *Service) saveGenesisData(ctx context.Context, genesisState iface.Beacon return errors.Wrap(err, "could not save genesis data") } genesisBlk, err := s.cfg.BeaconDB.GenesisBlock(ctx) - if err != nil || genesisBlk == nil { + if err != nil || genesisBlk == nil || genesisBlk.IsNil() { return fmt.Errorf("could not load genesis block: %v", err) } genesisBlkRoot, err := genesisBlk.Block().HashTreeRoot() @@ -448,7 +448,7 @@ func (s *Service) initializeChainInfo(ctx context.Context) error { return errors.Wrap(err, "could not get finalized block from db") } - if finalizedState == nil || finalizedBlock == nil { + if finalizedState == nil || finalizedState.IsNil() || finalizedBlock == nil || finalizedBlock.IsNil() { return errors.New("finalized state and block can't be nil") } s.setHead(finalizedRoot, finalizedBlock, finalizedState) diff --git a/beacon-chain/core/helpers/weak_subjectivity.go b/beacon-chain/core/helpers/weak_subjectivity.go index 2c25268520a1..3150c9771d1f 100644 --- a/beacon-chain/core/helpers/weak_subjectivity.go +++ b/beacon-chain/core/helpers/weak_subjectivity.go @@ -121,7 +121,7 @@ func ComputeWeakSubjectivityPeriod(st iface.ReadOnlyBeaconState) (types.Epoch, e func IsWithinWeakSubjectivityPeriod( currentEpoch types.Epoch, wsState iface.ReadOnlyBeaconState, wsCheckpoint *eth.WeakSubjectivityCheckpoint) (bool, error) { // Make sure that incoming objects are not nil. - if wsState == nil || wsState.LatestBlockHeader() == nil || wsCheckpoint == nil { + if wsState == nil || wsState.IsNil() || wsState.LatestBlockHeader() == nil || wsCheckpoint == nil { return false, errors.New("invalid weak subjectivity state or checkpoint") } diff --git a/beacon-chain/core/state/transition.go b/beacon-chain/core/state/transition.go index 1be4378a8a67..5c245b904705 100644 --- a/beacon-chain/core/state/transition.go +++ b/beacon-chain/core/state/transition.go @@ -233,7 +233,7 @@ func ProcessSlots(ctx context.Context, state iface.BeaconState, slot types.Slot) return nil, err } - if cachedState != nil && cachedState.Slot() < slot { + if cachedState != nil && !cachedState.IsNil() && cachedState.Slot() < slot { highestSlot = cachedState.Slot() state = cachedState } @@ -242,7 +242,7 @@ func ProcessSlots(ctx context.Context, state iface.BeaconState, slot types.Slot) if err != nil { return nil, err } - if cachedState != nil && cachedState.Slot() < slot { + if cachedState != nil && !cachedState.IsNil() && cachedState.Slot() < slot { highestSlot = cachedState.Slot() state = cachedState } diff --git a/beacon-chain/powchain/log_processing.go b/beacon-chain/powchain/log_processing.go index 5eb2ad616015..b1b9538d2b98 100644 --- a/beacon-chain/powchain/log_processing.go +++ b/beacon-chain/powchain/log_processing.go @@ -374,7 +374,7 @@ func (s *Service) processPastLogs(ctx context.Context) error { if err != nil { return err } - if fState != nil && fState.Eth1DepositIndex() > 0 { + if fState != nil && !fState.IsNil() && fState.Eth1DepositIndex() > 0 { s.cfg.DepositCache.PrunePendingDeposits(ctx, int64(fState.Eth1DepositIndex())) } return nil diff --git a/beacon-chain/sync/initial-sync/service.go b/beacon-chain/sync/initial-sync/service.go index 1d5b24efcad4..81fd3c47d352 100644 --- a/beacon-chain/sync/initial-sync/service.go +++ b/beacon-chain/sync/initial-sync/service.go @@ -140,7 +140,7 @@ func (s *Service) Initialized() bool { // behind the current network head. func (s *Service) Resync() error { headState, err := s.cfg.Chain.HeadState(s.ctx) - if err != nil || headState == nil { + if err != nil || headState == nil || headState.IsNil() { return errors.Errorf("could not retrieve head state: %v", err) } From 4e60370065738d877a8f298cf2837ce6439bab6d Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 25 May 2021 16:14:05 +0800 Subject: [PATCH 13/44] tests --- beacon-chain/blockchain/testing/BUILD.bazel | 1 + beacon-chain/blockchain/testing/mock.go | 44 ++++++++++--------- beacon-chain/core/blocks/attestation_test.go | 19 ++++---- .../core/blocks/block_operations_fuzz_test.go | 10 +++-- beacon-chain/core/blocks/header_test.go | 14 +++--- beacon-chain/core/blocks/randao_test.go | 6 ++- beacon-chain/core/state/BUILD.bazel | 1 + beacon-chain/core/state/benchmarks_test.go | 8 ++-- .../core/state/skip_slot_cache_test.go | 12 ++--- .../state/transition_no_verify_sig_test.go | 12 ++--- beacon-chain/core/state/transition_test.go | 30 +++++++------ beacon-chain/rpc/beaconv1/blocks_test.go | 42 +++++++++--------- beacon-chain/rpc/statefetcher/fetcher_test.go | 4 +- beacon-chain/state/stategen/getter_test.go | 44 ++++++++++--------- beacon-chain/state/stategen/mock.go | 7 +-- .../sync/pending_attestations_queue_test.go | 12 ++--- 16 files changed, 147 insertions(+), 119 deletions(-) diff --git a/beacon-chain/blockchain/testing/BUILD.bazel b/beacon-chain/blockchain/testing/BUILD.bazel index 133540be6ad6..447de4d846cd 100644 --- a/beacon-chain/blockchain/testing/BUILD.bazel +++ b/beacon-chain/blockchain/testing/BUILD.bazel @@ -23,6 +23,7 @@ go_library( "//proto/beacon/p2p/v1:go_default_library", "//shared/bytesutil:go_default_library", "//shared/event:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", diff --git a/beacon-chain/blockchain/testing/mock.go b/beacon-chain/blockchain/testing/mock.go index 89dda6deac5b..c26b1d9b4078 100644 --- a/beacon-chain/blockchain/testing/mock.go +++ b/beacon-chain/blockchain/testing/mock.go @@ -8,6 +8,8 @@ import ( "sync" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -32,11 +34,11 @@ import ( type ChainService struct { State iface.BeaconState Root []byte - Block *ethpb.SignedBeaconBlock + Block interfaces.SignedBeaconBlock FinalizedCheckPoint *ethpb.Checkpoint CurrentJustifiedCheckPoint *ethpb.Checkpoint PreviousJustifiedCheckPoint *ethpb.Checkpoint - BlocksReceived []*ethpb.SignedBeaconBlock + BlocksReceived []interfaces.SignedBeaconBlock Balance *precompute.Balance Genesis time.Time ValidatorsRoot [32]byte @@ -149,18 +151,18 @@ func (mon *MockOperationNotifier) OperationFeed() *event.Feed { } // ReceiveBlockInitialSync mocks ReceiveBlockInitialSync method in chain service. -func (s *ChainService) ReceiveBlockInitialSync(ctx context.Context, block *ethpb.SignedBeaconBlock, _ [32]byte) error { +func (s *ChainService) ReceiveBlockInitialSync(ctx context.Context, block interfaces.SignedBeaconBlock, _ [32]byte) error { if s.State == nil { s.State = &stateV0.BeaconState{} } - if !bytes.Equal(s.Root, block.Block.ParentRoot) { - return errors.Errorf("wanted %#x but got %#x", s.Root, block.Block.ParentRoot) + if !bytes.Equal(s.Root, block.Block().ParentRoot()) { + return errors.Errorf("wanted %#x but got %#x", s.Root, block.Block().ParentRoot()) } - if err := s.State.SetSlot(block.Block.Slot); err != nil { + if err := s.State.SetSlot(block.Block().Slot()); err != nil { return err } s.BlocksReceived = append(s.BlocksReceived, block) - signingRoot, err := block.Block.HashTreeRoot() + signingRoot, err := block.Block().HashTreeRoot() if err != nil { return err } @@ -168,7 +170,7 @@ func (s *ChainService) ReceiveBlockInitialSync(ctx context.Context, block *ethpb if err := s.DB.SaveBlock(ctx, block); err != nil { return err } - logrus.Infof("Saved block with root: %#x at slot %d", signingRoot, block.Block.Slot) + logrus.Infof("Saved block with root: %#x at slot %d", signingRoot, block.Block().Slot()) } s.Root = signingRoot[:] s.Block = block @@ -176,19 +178,19 @@ func (s *ChainService) ReceiveBlockInitialSync(ctx context.Context, block *ethpb } // ReceiveBlockBatch processes blocks in batches from initial-sync. -func (s *ChainService) ReceiveBlockBatch(ctx context.Context, blks []*ethpb.SignedBeaconBlock, _ [][32]byte) error { +func (s *ChainService) ReceiveBlockBatch(ctx context.Context, blks []interfaces.SignedBeaconBlock, _ [][32]byte) error { if s.State == nil { s.State = &stateV0.BeaconState{} } for _, block := range blks { - if !bytes.Equal(s.Root, block.Block.ParentRoot) { - return errors.Errorf("wanted %#x but got %#x", s.Root, block.Block.ParentRoot) + if !bytes.Equal(s.Root, block.Block().ParentRoot()) { + return errors.Errorf("wanted %#x but got %#x", s.Root, block.Block().ParentRoot()) } - if err := s.State.SetSlot(block.Block.Slot); err != nil { + if err := s.State.SetSlot(block.Block().Slot()); err != nil { return err } s.BlocksReceived = append(s.BlocksReceived, block) - signingRoot, err := block.Block.HashTreeRoot() + signingRoot, err := block.Block().HashTreeRoot() if err != nil { return err } @@ -196,7 +198,7 @@ func (s *ChainService) ReceiveBlockBatch(ctx context.Context, blks []*ethpb.Sign if err := s.DB.SaveBlock(ctx, block); err != nil { return err } - logrus.Infof("Saved block with root: %#x at slot %d", signingRoot, block.Block.Slot) + logrus.Infof("Saved block with root: %#x at slot %d", signingRoot, block.Block().Slot()) } s.Root = signingRoot[:] s.Block = block @@ -205,18 +207,18 @@ func (s *ChainService) ReceiveBlockBatch(ctx context.Context, blks []*ethpb.Sign } // ReceiveBlock mocks ReceiveBlock method in chain service. -func (s *ChainService) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlock, _ [32]byte) error { +func (s *ChainService) ReceiveBlock(ctx context.Context, block interfaces.SignedBeaconBlock, _ [32]byte) error { if s.State == nil { s.State = &stateV0.BeaconState{} } - if !bytes.Equal(s.Root, block.Block.ParentRoot) { - return errors.Errorf("wanted %#x but got %#x", s.Root, block.Block.ParentRoot) + if !bytes.Equal(s.Root, block.Block().ParentRoot()) { + return errors.Errorf("wanted %#x but got %#x", s.Root, block.Block().ParentRoot()) } - if err := s.State.SetSlot(block.Block.Slot); err != nil { + if err := s.State.SetSlot(block.Block().Slot()); err != nil { return err } s.BlocksReceived = append(s.BlocksReceived, block) - signingRoot, err := block.Block.HashTreeRoot() + signingRoot, err := block.Block().HashTreeRoot() if err != nil { return err } @@ -224,7 +226,7 @@ func (s *ChainService) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeac if err := s.DB.SaveBlock(ctx, block); err != nil { return err } - logrus.Infof("Saved block with root: %#x at slot %d", signingRoot, block.Block.Slot) + logrus.Infof("Saved block with root: %#x at slot %d", signingRoot, block.Block().Slot()) } s.Root = signingRoot[:] s.Block = block @@ -248,7 +250,7 @@ func (s *ChainService) HeadRoot(_ context.Context) ([]byte, error) { } // HeadBlock mocks HeadBlock method in chain service. -func (s *ChainService) HeadBlock(context.Context) (*ethpb.SignedBeaconBlock, error) { +func (s *ChainService) HeadBlock(context.Context) (interfaces.SignedBeaconBlock, error) { return s.Block, nil } diff --git a/beacon-chain/core/blocks/attestation_test.go b/beacon-chain/core/blocks/attestation_test.go index 03ac35bee16a..cb078c3a76c8 100644 --- a/beacon-chain/core/blocks/attestation_test.go +++ b/beacon-chain/core/blocks/attestation_test.go @@ -3,6 +3,7 @@ package blocks_test import ( "context" "fmt" + "github.com/prysmaticlabs/prysm/shared/interfaces" "testing" types "github.com/prysmaticlabs/eth2-types" @@ -46,7 +47,7 @@ func TestProcessAttestations_InclusionDelayFailure(t *testing.T) { params.BeaconConfig().MinAttestationInclusionDelay, beaconState.Slot(), ) - _, err := blocks.ProcessAttestations(context.Background(), beaconState, b) + _, err := blocks.ProcessAttestations(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) } @@ -76,7 +77,7 @@ func TestProcessAttestations_NeitherCurrentNorPrevEpoch(t *testing.T) { helpers.PrevEpoch(beaconState), helpers.CurrentEpoch(beaconState), ) - _, err = blocks.ProcessAttestations(context.Background(), beaconState, b) + _, err = blocks.ProcessAttestations(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) } @@ -104,11 +105,11 @@ func TestProcessAttestations_CurrentEpochFFGDataMismatches(t *testing.T) { require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{})) want := "source check point not equal to current justified checkpoint" - _, err := blocks.ProcessAttestations(context.Background(), beaconState, b) + _, err := blocks.ProcessAttestations(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) b.Block.Body.Attestations[0].Data.Source.Epoch = helpers.CurrentEpoch(beaconState) b.Block.Body.Attestations[0].Data.Source.Root = []byte{} - _, err = blocks.ProcessAttestations(context.Background(), beaconState, b) + _, err = blocks.ProcessAttestations(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) } @@ -142,12 +143,12 @@ func TestProcessAttestations_PrevEpochFFGDataMismatches(t *testing.T) { require.NoError(t, beaconState.AppendPreviousEpochAttestations(&pb.PendingAttestation{})) want := "source check point not equal to previous justified checkpoint" - _, err = blocks.ProcessAttestations(context.Background(), beaconState, b) + _, err = blocks.ProcessAttestations(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) b.Block.Body.Attestations[0].Data.Source.Epoch = helpers.PrevEpoch(beaconState) b.Block.Body.Attestations[0].Data.Target.Epoch = helpers.PrevEpoch(beaconState) b.Block.Body.Attestations[0].Data.Source.Root = []byte{} - _, err = blocks.ProcessAttestations(context.Background(), beaconState, b) + _, err = blocks.ProcessAttestations(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) } @@ -178,7 +179,7 @@ func TestProcessAttestations_InvalidAggregationBitsLength(t *testing.T) { require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{})) expected := "failed to verify aggregation bitfield: wanted participants bitfield length 3, got: 4" - _, err = blocks.ProcessAttestations(context.Background(), beaconState, b) + _, err = blocks.ProcessAttestations(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, expected, err) } @@ -221,7 +222,7 @@ func TestProcessAttestations_OK(t *testing.T) { err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay) require.NoError(t, err) - _, err = blocks.ProcessAttestations(context.Background(), beaconState, block) + _, err = blocks.ProcessAttestations(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) assert.NoError(t, err) } @@ -352,7 +353,7 @@ func TestProcessAggregatedAttestation_NoOverlappingBits(t *testing.T) { err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay) require.NoError(t, err) - _, err = blocks.ProcessAttestations(context.Background(), beaconState, block) + _, err = blocks.ProcessAttestations(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) assert.NoError(t, err) } diff --git a/beacon-chain/core/blocks/block_operations_fuzz_test.go b/beacon-chain/core/blocks/block_operations_fuzz_test.go index ca56dd698958..dc7114631a63 100644 --- a/beacon-chain/core/blocks/block_operations_fuzz_test.go +++ b/beacon-chain/core/blocks/block_operations_fuzz_test.go @@ -4,6 +4,8 @@ import ( "context" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + fuzz "github.com/google/gofuzz" types "github.com/prysmaticlabs/eth2-types" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -41,7 +43,7 @@ func TestFuzzProcessBlockHeader_10000(t *testing.T) { s, err := stateV0.InitializeFromProtoUnsafe(state) require.NoError(t, err) - _, err = ProcessBlockHeader(context.Background(), s, block) + _, err = ProcessBlockHeader(context.Background(), s, interfaces.NewWrappedSignedBeaconBlock(block)) _ = err } } @@ -139,7 +141,7 @@ func TestFuzzProcessRandao_10000(t *testing.T) { fuzzer.Fuzz(b) s, err := stateV0.InitializeFromProtoUnsafe(state) require.NoError(t, err) - r, err := ProcessRandao(context.Background(), s, b) + r, err := ProcessRandao(context.Background(), s, interfaces.NewWrappedSignedBeaconBlock(b)) if err != nil && r != nil { t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, b) } @@ -258,7 +260,7 @@ func TestFuzzProcessAttestations_10000(t *testing.T) { fuzzer.Fuzz(b) s, err := stateV0.InitializeFromProtoUnsafe(state) require.NoError(t, err) - r, err := ProcessAttestations(ctx, s, b) + r, err := ProcessAttestations(ctx, s, interfaces.NewWrappedSignedBeaconBlock(b)) if err != nil && r != nil { t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, b) } @@ -275,7 +277,7 @@ func TestFuzzProcessAttestationsNoVerify_10000(t *testing.T) { fuzzer.Fuzz(b) s, err := stateV0.InitializeFromProtoUnsafe(state) require.NoError(t, err) - r, err := ProcessAttestationsNoVerifySignature(ctx, s, b) + r, err := ProcessAttestationsNoVerifySignature(ctx, s, interfaces.NewWrappedSignedBeaconBlock(b)) if err != nil && r != nil { t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, b) } diff --git a/beacon-chain/core/blocks/header_test.go b/beacon-chain/core/blocks/header_test.go index 82887a9796b0..95a454107fe9 100644 --- a/beacon-chain/core/blocks/header_test.go +++ b/beacon-chain/core/blocks/header_test.go @@ -5,6 +5,8 @@ import ( "io/ioutil" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" @@ -65,7 +67,7 @@ func TestProcessBlockHeader_ImproperBlockSlot(t *testing.T) { err = state.UpdateValidatorAtIndex(proposerIdx, validators[proposerIdx]) require.NoError(t, err) - _, err = blocks.ProcessBlockHeader(context.Background(), state, block) + _, err = blocks.ProcessBlockHeader(context.Background(), state, interfaces.NewWrappedSignedBeaconBlock(block)) assert.ErrorContains(t, "block.Slot 10 must be greater than state.LatestBlockHeader.Slot 10", err) } @@ -91,7 +93,7 @@ func TestProcessBlockHeader_WrongProposerSig(t *testing.T) { block.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, block.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx+1]) require.NoError(t, err) - _, err = blocks.ProcessBlockHeader(context.Background(), beaconState, block) + _, err = blocks.ProcessBlockHeader(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) want := "signature did not verify" assert.ErrorContains(t, want, err) } @@ -133,7 +135,7 @@ func TestProcessBlockHeader_DifferentSlots(t *testing.T) { Signature: blockSig, }) - _, err = blocks.ProcessBlockHeader(context.Background(), state, block) + _, err = blocks.ProcessBlockHeader(context.Background(), state, interfaces.NewWrappedSignedBeaconBlock(block)) want := "is different than block slot" assert.ErrorContains(t, want, err) } @@ -172,7 +174,7 @@ func TestProcessBlockHeader_PreviousBlockRootNotSignedRoot(t *testing.T) { block.Block.ParentRoot = bytesutil.PadTo([]byte{'A'}, 32) block.Signature = blockSig - _, err = blocks.ProcessBlockHeader(context.Background(), state, block) + _, err = blocks.ProcessBlockHeader(context.Background(), state, interfaces.NewWrappedSignedBeaconBlock(block)) want := "does not match" assert.ErrorContains(t, want, err) } @@ -214,7 +216,7 @@ func TestProcessBlockHeader_SlashedProposer(t *testing.T) { block.Block.ParentRoot = parentRoot[:] block.Signature = blockSig - _, err = blocks.ProcessBlockHeader(context.Background(), state, block) + _, err = blocks.ProcessBlockHeader(context.Background(), state, interfaces.NewWrappedSignedBeaconBlock(block)) want := "was previously slashed" assert.ErrorContains(t, want, err) } @@ -263,7 +265,7 @@ func TestProcessBlockHeader_OK(t *testing.T) { err = state.UpdateValidatorAtIndex(proposerIdx, validators[proposerIdx]) require.NoError(t, err) - newState, err := blocks.ProcessBlockHeader(context.Background(), state, block) + newState, err := blocks.ProcessBlockHeader(context.Background(), state, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err, "Failed to process block header got") var zeroHash [32]byte nsh := newState.LatestBlockHeader() diff --git a/beacon-chain/core/blocks/randao_test.go b/beacon-chain/core/blocks/randao_test.go index bdef490fc467..5a1010995b3b 100644 --- a/beacon-chain/core/blocks/randao_test.go +++ b/beacon-chain/core/blocks/randao_test.go @@ -5,6 +5,8 @@ import ( "encoding/binary" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" @@ -38,7 +40,7 @@ func TestProcessRandao_IncorrectProposerFailsVerification(t *testing.T) { } want := "block randao: signature did not verify" - _, err = blocks.ProcessRandao(context.Background(), beaconState, b) + _, err = blocks.ProcessRandao(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) } @@ -59,7 +61,7 @@ func TestProcessRandao_SignatureVerifiesAndUpdatesLatestStateMixes(t *testing.T) newState, err := blocks.ProcessRandao( context.Background(), beaconState, - b, + interfaces.NewWrappedSignedBeaconBlock(b), ) require.NoError(t, err, "Unexpected error processing block randao") currentEpoch := helpers.CurrentEpoch(beaconState) diff --git a/beacon-chain/core/state/BUILD.bazel b/beacon-chain/core/state/BUILD.bazel index d33d0787a362..177ff29b631c 100644 --- a/beacon-chain/core/state/BUILD.bazel +++ b/beacon-chain/core/state/BUILD.bazel @@ -84,6 +84,7 @@ go_test( "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", "//shared/hashutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", diff --git a/beacon-chain/core/state/benchmarks_test.go b/beacon-chain/core/state/benchmarks_test.go index e6d6189d195e..878997ebf873 100644 --- a/beacon-chain/core/state/benchmarks_test.go +++ b/beacon-chain/core/state/benchmarks_test.go @@ -4,6 +4,8 @@ import ( "context" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" coreState "github.com/prysmaticlabs/prysm/beacon-chain/core/state" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" @@ -27,7 +29,7 @@ func BenchmarkExecuteStateTransition_FullBlock(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - _, err := coreState.ExecuteStateTransition(context.Background(), cleanStates[i], block) + _, err := coreState.ExecuteStateTransition(context.Background(), cleanStates[i], interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(b, err) } } @@ -48,12 +50,12 @@ func BenchmarkExecuteStateTransition_WithCache(b *testing.B) { require.NoError(b, helpers.UpdateCommitteeCache(beaconState, helpers.CurrentEpoch(beaconState))) require.NoError(b, beaconState.SetSlot(currentSlot)) // Run the state transition once to populate the cache. - _, err = coreState.ExecuteStateTransition(context.Background(), beaconState, block) + _, err = coreState.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(b, err, "Failed to process block, benchmarks will fail") b.ResetTimer() for i := 0; i < b.N; i++ { - _, err := coreState.ExecuteStateTransition(context.Background(), cleanStates[i], block) + _, err := coreState.ExecuteStateTransition(context.Background(), cleanStates[i], interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(b, err, "Failed to process block, benchmarks will fail") } } diff --git a/beacon-chain/core/state/skip_slot_cache_test.go b/beacon-chain/core/state/skip_slot_cache_test.go index f6407c76c24e..f8248ace1aeb 100644 --- a/beacon-chain/core/state/skip_slot_cache_test.go +++ b/beacon-chain/core/state/skip_slot_cache_test.go @@ -5,6 +5,8 @@ import ( "sync" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/prysmaticlabs/prysm/beacon-chain/core/state" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" @@ -30,11 +32,11 @@ func TestSkipSlotCache_OK(t *testing.T) { // with the state blk, err := testutil.GenerateFullBlock(bState, privs, blkCfg, originalState.Slot()+10) require.NoError(t, err) - executedState, err := state.ExecuteStateTransition(context.Background(), originalState, blk) + executedState, err := state.ExecuteStateTransition(context.Background(), originalState, interfaces.NewWrappedSignedBeaconBlock(blk)) require.NoError(t, err, "Could not run state transition") originalState, ok := executedState.(*stateV0.BeaconState) require.Equal(t, true, ok) - bState, err = state.ExecuteStateTransition(context.Background(), bState, blk) + bState, err = state.ExecuteStateTransition(context.Background(), bState, interfaces.NewWrappedSignedBeaconBlock(blk)) require.NoError(t, err, "Could not process state transition") assert.DeepEqual(t, originalState.CloneInnerState(), bState.CloneInnerState(), "Skipped slots cache leads to different states") @@ -56,7 +58,7 @@ func TestSkipSlotCache_ConcurrentMixup(t *testing.T) { // with the state blk, err := testutil.GenerateFullBlock(bState, privs, blkCfg, originalState.Slot()+10) require.NoError(t, err) - executedState, err := state.ExecuteStateTransition(context.Background(), originalState, blk) + executedState, err := state.ExecuteStateTransition(context.Background(), originalState, interfaces.NewWrappedSignedBeaconBlock(blk)) require.NoError(t, err, "Could not run state transition") originalState, ok := executedState.(*stateV0.BeaconState) require.Equal(t, true, ok) @@ -70,7 +72,7 @@ func TestSkipSlotCache_ConcurrentMixup(t *testing.T) { signature, err := testutil.BlockSignature(originalState, blk.Block, privs) require.NoError(t, err) blk.Signature = signature.Marshal() - state1, err = state.ExecuteStateTransition(context.Background(), originalState.Copy(), blk) + state1, err = state.ExecuteStateTransition(context.Background(), originalState.Copy(), interfaces.NewWrappedSignedBeaconBlock(blk)) require.NoError(t, err, "Could not run state transition") } @@ -81,7 +83,7 @@ func TestSkipSlotCache_ConcurrentMixup(t *testing.T) { signature, err := testutil.BlockSignature(originalState, blk.Block, privs) require.NoError(t, err) blk.Signature = signature.Marshal() - state2, err = state.ExecuteStateTransition(context.Background(), originalState.Copy(), blk) + state2, err = state.ExecuteStateTransition(context.Background(), originalState.Copy(), interfaces.NewWrappedSignedBeaconBlock(blk)) require.NoError(t, err, "Could not run state transition") } diff --git a/beacon-chain/core/state/transition_no_verify_sig_test.go b/beacon-chain/core/state/transition_no_verify_sig_test.go index 8e6409b928ff..fd92370da22f 100644 --- a/beacon-chain/core/state/transition_no_verify_sig_test.go +++ b/beacon-chain/core/state/transition_no_verify_sig_test.go @@ -4,6 +4,8 @@ import ( "context" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" @@ -50,7 +52,7 @@ func TestExecuteStateTransitionNoVerify_FullProcess(t *testing.T) { block.Block.Body.RandaoReveal = randaoReveal block.Block.Body.Eth1Data = eth1Data - stateRoot, err := state.CalculateStateRoot(context.Background(), beaconState, block) + stateRoot, err := state.CalculateStateRoot(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) block.Block.StateRoot = stateRoot[:] @@ -59,7 +61,7 @@ func TestExecuteStateTransitionNoVerify_FullProcess(t *testing.T) { require.NoError(t, err) block.Signature = sig.Marshal() - set, _, err := state.ExecuteStateTransitionNoVerifyAnySig(context.Background(), beaconState, block) + set, _, err := state.ExecuteStateTransitionNoVerifyAnySig(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) assert.NoError(t, err) verified, err := set.Verify() assert.NoError(t, err) @@ -102,7 +104,7 @@ func TestExecuteStateTransitionNoVerifySignature_CouldNotVerifyStateRoot(t *test block.Block.Body.RandaoReveal = randaoReveal block.Block.Body.Eth1Data = eth1Data - stateRoot, err := state.CalculateStateRoot(context.Background(), beaconState, block) + stateRoot, err := state.CalculateStateRoot(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) block.Block.StateRoot = stateRoot[:] @@ -112,13 +114,13 @@ func TestExecuteStateTransitionNoVerifySignature_CouldNotVerifyStateRoot(t *test block.Signature = sig.Marshal() block.Block.StateRoot = bytesutil.PadTo([]byte{'a'}, 32) - _, _, err = state.ExecuteStateTransitionNoVerifyAnySig(context.Background(), beaconState, block) + _, _, err = state.ExecuteStateTransitionNoVerifyAnySig(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.ErrorContains(t, "could not validate state root", err) } func TestProcessBlockNoVerify_PassesProcessingConditions(t *testing.T) { beaconState, block, _, _, _ := createFullBlockWithOperations(t) - set, _, err := state.ProcessBlockNoVerifyAnySig(context.Background(), beaconState, block) + set, _, err := state.ProcessBlockNoVerifyAnySig(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) // Test Signature set verifies. verified, err := set.Verify() diff --git a/beacon-chain/core/state/transition_test.go b/beacon-chain/core/state/transition_test.go index 829cb711389b..9c7628c35b9a 100644 --- a/beacon-chain/core/state/transition_test.go +++ b/beacon-chain/core/state/transition_test.go @@ -6,6 +6,8 @@ import ( "fmt" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" @@ -43,7 +45,7 @@ func TestExecuteStateTransition_IncorrectSlot(t *testing.T) { }, } want := "expected state.slot" - _, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + _, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) assert.ErrorContains(t, want, err) } @@ -86,7 +88,7 @@ func TestExecuteStateTransition_FullProcess(t *testing.T) { block.Block.Body.RandaoReveal = randaoReveal block.Block.Body.Eth1Data = eth1Data - stateRoot, err := state.CalculateStateRoot(context.Background(), beaconState, block) + stateRoot, err := state.CalculateStateRoot(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) block.Block.StateRoot = stateRoot[:] @@ -95,7 +97,7 @@ func TestExecuteStateTransition_FullProcess(t *testing.T) { require.NoError(t, err) block.Signature = sig.Marshal() - beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) assert.Equal(t, params.BeaconConfig().SlotsPerEpoch, beaconState.Slot(), "Unexpected Slot number") @@ -134,7 +136,7 @@ func TestProcessBlock_IncorrectProposerSlashing(t *testing.T) { beaconState, err = state.ProcessSlots(context.Background(), beaconState, 1) require.NoError(t, err) want := "could not verify proposer slashing" - _, err = state.ProcessBlock(context.Background(), beaconState, block) + _, err = state.ProcessBlock(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) assert.ErrorContains(t, want, err) } @@ -161,7 +163,7 @@ func TestProcessBlock_IncorrectProcessBlockAttestations(t *testing.T) { require.NoError(t, err) want := "could not verify attestation" - _, err = state.ProcessBlock(context.Background(), beaconState, block) + _, err = state.ProcessBlock(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) assert.ErrorContains(t, want, err) } @@ -242,7 +244,7 @@ func TestProcessBlock_IncorrectProcessExits(t *testing.T) { cp.Root = []byte("hello-world") require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(cp)) require.NoError(t, beaconState.AppendCurrentEpochAttestations(&pb.PendingAttestation{})) - _, err = state.VerifyOperationLengths(context.Background(), beaconState, block) + _, err = state.VerifyOperationLengths(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) wanted := "number of voluntary exits (17) in block body exceeds allowed threshold of 16" assert.ErrorContains(t, wanted, err) } @@ -423,7 +425,7 @@ func TestProcessBlock_PassesProcessingConditions(t *testing.T) { beaconState, block, _, proposerSlashings, exits := createFullBlockWithOperations(t) exit := exits[0] - beaconState, err := state.ProcessBlock(context.Background(), beaconState, block) + beaconState, err := state.ProcessBlock(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err, "Expected block to pass processing conditions") v, err := beaconState.ValidatorAtIndex(proposerSlashings[0].Header_1.Header.ProposerIndex) @@ -615,7 +617,7 @@ func BenchmarkProcessBlk_65536Validators_FullBlock(b *testing.B) { b.ResetTimer() for n := 0; n < b.N; n++ { - _, err := state.ProcessBlock(context.Background(), s, blk) + _, err := state.ProcessBlock(context.Background(), s, interfaces.NewWrappedSignedBeaconBlock(blk)) require.NoError(b, err) // Reset state fields to process block again v := s.Validators() @@ -699,7 +701,7 @@ func TestProcessBlk_AttsBasedOnValidatorCount(t *testing.T) { params.OverrideBeaconConfig(config) require.NoError(t, s.SetSlot(s.Slot()+1)) - _, err = state.ProcessBlock(context.Background(), s, blk) + _, err = state.ProcessBlock(context.Background(), s, interfaces.NewWrappedSignedBeaconBlock(blk)) require.NoError(t, err) } @@ -746,7 +748,7 @@ func TestProcessBlock_OverMaxProposerSlashings(t *testing.T) { } want := fmt.Sprintf("number of proposer slashings (%d) in block body exceeds allowed threshold of %d", len(b.Block.Body.ProposerSlashings), params.BeaconConfig().MaxProposerSlashings) - _, err := state.VerifyOperationLengths(context.Background(), &stateV0.BeaconState{}, b) + _, err := state.VerifyOperationLengths(context.Background(), &stateV0.BeaconState{}, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) } @@ -761,7 +763,7 @@ func TestProcessBlock_OverMaxAttesterSlashings(t *testing.T) { } want := fmt.Sprintf("number of attester slashings (%d) in block body exceeds allowed threshold of %d", len(b.Block.Body.AttesterSlashings), params.BeaconConfig().MaxAttesterSlashings) - _, err := state.VerifyOperationLengths(context.Background(), &stateV0.BeaconState{}, b) + _, err := state.VerifyOperationLengths(context.Background(), &stateV0.BeaconState{}, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) } @@ -775,7 +777,7 @@ func TestProcessBlock_OverMaxAttestations(t *testing.T) { } want := fmt.Sprintf("number of attestations (%d) in block body exceeds allowed threshold of %d", len(b.Block.Body.Attestations), params.BeaconConfig().MaxAttestations) - _, err := state.VerifyOperationLengths(context.Background(), &stateV0.BeaconState{}, b) + _, err := state.VerifyOperationLengths(context.Background(), &stateV0.BeaconState{}, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) } @@ -790,7 +792,7 @@ func TestProcessBlock_OverMaxVoluntaryExits(t *testing.T) { } want := fmt.Sprintf("number of voluntary exits (%d) in block body exceeds allowed threshold of %d", len(b.Block.Body.VoluntaryExits), maxExits) - _, err := state.VerifyOperationLengths(context.Background(), &stateV0.BeaconState{}, b) + _, err := state.VerifyOperationLengths(context.Background(), &stateV0.BeaconState{}, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) } @@ -810,7 +812,7 @@ func TestProcessBlock_IncorrectDeposits(t *testing.T) { } want := fmt.Sprintf("incorrect outstanding deposits in block body, wanted: %d, got: %d", s.Eth1Data().DepositCount-s.Eth1DepositIndex(), len(b.Block.Body.Deposits)) - _, err = state.VerifyOperationLengths(context.Background(), s, b) + _, err = state.VerifyOperationLengths(context.Background(), s, interfaces.NewWrappedSignedBeaconBlock(b)) assert.ErrorContains(t, want, err) } diff --git a/beacon-chain/rpc/beaconv1/blocks_test.go b/beacon-chain/rpc/beaconv1/blocks_test.go index ef810c62496f..e265d38512ff 100644 --- a/beacon-chain/rpc/beaconv1/blocks_test.go +++ b/beacon-chain/rpc/beaconv1/blocks_test.go @@ -5,6 +5,8 @@ import ( "reflect" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1" ethpb_alpha "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -27,11 +29,11 @@ func fillDBTestBlocks(ctx context.Context, t *testing.T, beaconDB db.Database) ( genBlk.Block.ParentRoot = parentRoot[:] root, err := genBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, genBlk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genBlk))) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, root)) count := types.Slot(100) - blks := make([]*ethpb_alpha.SignedBeaconBlock, count) + blks := make([]interfaces.SignedBeaconBlock, count) blkContainers := make([]*ethpb_alpha.BeaconBlockContainer, count) for i := types.Slot(0); i < count; i++ { b := testutil.NewBeaconBlock() @@ -46,7 +48,7 @@ func fillDBTestBlocks(ctx context.Context, t *testing.T, beaconDB db.Database) ( b.Block.Body.Attestations = []*ethpb_alpha.Attestation{att1, att2} root, err := b.Block.HashTreeRoot() require.NoError(t, err) - blks[i] = b + blks[i] = interfaces.NewWrappedSignedBeaconBlock(b) blkContainers[i] = ðpb_alpha.BeaconBlockContainer{Block: b, BlockRoot: root[:]} } require.NoError(t, beaconDB.SaveBlocks(ctx, blks)) @@ -72,17 +74,17 @@ func TestServer_GetBlockHeader(t *testing.T) { b2 := testutil.NewBeaconBlock() b2.Block.Slot = 30 b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) - require.NoError(t, beaconDB.SaveBlock(ctx, b2)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b2))) b3 := testutil.NewBeaconBlock() b3.Block.Slot = 30 b3.Block.ParentRoot = bytesutil.PadTo([]byte{4}, 32) - require.NoError(t, beaconDB.SaveBlock(ctx, b3)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b3))) bs := &Server{ BeaconDB: beaconDB, ChainInfoFetcher: &mock.ChainService{ DB: beaconDB, - Block: headBlock.Block, + Block: interfaces.NewWrappedSignedBeaconBlock(headBlock.Block), Root: headBlock.BlockRoot, FinalizedCheckPoint: ðpb_alpha.Checkpoint{Root: blkContainers[64].BlockRoot}, }, @@ -167,7 +169,7 @@ func TestServer_ListBlockHeaders(t *testing.T) { BeaconDB: beaconDB, ChainInfoFetcher: &mock.ChainService{ DB: beaconDB, - Block: headBlock.Block, + Block: interfaces.NewWrappedSignedBeaconBlock(headBlock.Block), Root: headBlock.BlockRoot, FinalizedCheckPoint: ðpb_alpha.Checkpoint{Root: blkContainers[64].BlockRoot}, }, @@ -176,19 +178,19 @@ func TestServer_ListBlockHeaders(t *testing.T) { b2 := testutil.NewBeaconBlock() b2.Block.Slot = 30 b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) - require.NoError(t, beaconDB.SaveBlock(ctx, b2)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b2))) b3 := testutil.NewBeaconBlock() b3.Block.Slot = 30 b3.Block.ParentRoot = bytesutil.PadTo([]byte{4}, 32) - require.NoError(t, beaconDB.SaveBlock(ctx, b3)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b3))) b4 := testutil.NewBeaconBlock() b4.Block.Slot = 31 b4.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) - require.NoError(t, beaconDB.SaveBlock(ctx, b4)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b4))) b5 := testutil.NewBeaconBlock() b5.Block.Slot = 28 b5.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) - require.NoError(t, beaconDB.SaveBlock(ctx, b5)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b5))) tests := []struct { name string @@ -245,7 +247,7 @@ func TestServer_ProposeBlock_OK(t *testing.T) { params.OverrideBeaconConfig(params.MainnetConfig()) genesis := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(context.Background(), genesis), "Could not save genesis block") + require.NoError(t, beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genesis)), "Could not save genesis block") numDeposits := uint64(64) beaconState, _ := testutil.DeterministicGenesisState(t, numDeposits) @@ -268,7 +270,7 @@ func TestServer_ProposeBlock_OK(t *testing.T) { req.Block.ParentRoot = bsRoot[:] v1Block, err := migration.V1Alpha1ToV1Block(req) require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, req)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(req))) blockReq := ðpb.BeaconBlockContainer{ Message: v1Block.Block, Signature: v1Block.Signature, @@ -287,17 +289,17 @@ func TestServer_GetBlock(t *testing.T) { b2 := testutil.NewBeaconBlock() b2.Block.Slot = 30 b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) - require.NoError(t, beaconDB.SaveBlock(ctx, b2)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b2))) b3 := testutil.NewBeaconBlock() b3.Block.Slot = 30 b3.Block.ParentRoot = bytesutil.PadTo([]byte{4}, 32) - require.NoError(t, beaconDB.SaveBlock(ctx, b3)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b3))) bs := &Server{ BeaconDB: beaconDB, ChainInfoFetcher: &mock.ChainService{ DB: beaconDB, - Block: headBlock.Block, + Block: interfaces.NewWrappedSignedBeaconBlock(headBlock.Block), Root: headBlock.BlockRoot, FinalizedCheckPoint: ðpb_alpha.Checkpoint{Root: blkContainers[64].BlockRoot}, }, @@ -399,17 +401,17 @@ func TestServer_GetBlockRoot(t *testing.T) { b2 := testutil.NewBeaconBlock() b2.Block.Slot = 30 b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) - require.NoError(t, beaconDB.SaveBlock(ctx, b2)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b2))) b3 := testutil.NewBeaconBlock() b3.Block.Slot = 30 b3.Block.ParentRoot = bytesutil.PadTo([]byte{4}, 32) - require.NoError(t, beaconDB.SaveBlock(ctx, b3)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b3))) bs := &Server{ BeaconDB: beaconDB, ChainInfoFetcher: &mock.ChainService{ DB: beaconDB, - Block: headBlock.Block, + Block: interfaces.NewWrappedSignedBeaconBlock(headBlock.Block), Root: headBlock.BlockRoot, FinalizedCheckPoint: ðpb_alpha.Checkpoint{Root: blkContainers[64].BlockRoot}, }, @@ -500,7 +502,7 @@ func TestServer_ListBlockAttestations(t *testing.T) { BeaconDB: beaconDB, ChainInfoFetcher: &mock.ChainService{ DB: beaconDB, - Block: headBlock.Block, + Block: interfaces.NewWrappedSignedBeaconBlock(headBlock.Block), Root: headBlock.BlockRoot, FinalizedCheckPoint: ðpb_alpha.Checkpoint{Root: blkContainers[64].BlockRoot}, }, diff --git a/beacon-chain/rpc/statefetcher/fetcher_test.go b/beacon-chain/rpc/statefetcher/fetcher_test.go index e55599445205..1fc5af470824 100644 --- a/beacon-chain/rpc/statefetcher/fetcher_test.go +++ b/beacon-chain/rpc/statefetcher/fetcher_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/ethereum/go-ethereum/common/hexutil" types "github.com/prysmaticlabs/eth2-types" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -55,7 +57,7 @@ func TestGetStateRoot(t *testing.T) { db := testDB.SetupDB(t) b := testutil.NewBeaconBlock() b.Block.StateRoot = bytesutil.PadTo([]byte("foo"), 32) - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) diff --git a/beacon-chain/state/stategen/getter_test.go b/beacon-chain/state/stategen/getter_test.go index b6e4a50931f6..1be6754d97ce 100644 --- a/beacon-chain/state/stategen/getter_test.go +++ b/beacon-chain/state/stategen/getter_test.go @@ -4,6 +4,8 @@ import ( "context" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" @@ -26,13 +28,13 @@ func TestStateByRoot_ColdState(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = 1 - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) bRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) beaconState, _ := testutil.DeterministicGenesisState(t, 32) require.NoError(t, beaconState.SetSlot(1)) require.NoError(t, service.beaconDB.SaveState(ctx, beaconState, bRoot)) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, bRoot)) loadedState, err := service.StateByRoot(ctx, bRoot) require.NoError(t, err) @@ -73,7 +75,7 @@ func TestStateByRoot_HotStateUsingEpochBoundaryCacheWithReplay(t *testing.T) { targetBlock.Block.Slot = 11 targetBlock.Block.ParentRoot = blkRoot[:] targetBlock.Block.ProposerIndex = 8 - require.NoError(t, service.beaconDB.SaveBlock(ctx, targetBlock)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(targetBlock))) targetRoot, err := targetBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: targetSlot, Root: targetRoot[:]})) @@ -151,7 +153,7 @@ func TestStateByRootInitialSync_CanProcessUpTo(t *testing.T) { targetBlk.Block.ParentRoot = blkRoot[:] targetRoot, err := targetBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, service.beaconDB.SaveBlock(ctx, targetBlk)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(targetBlk))) require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: targetSlot, Root: targetRoot[:]})) loadedState, err := service.StateByRootInitialSync(ctx, targetRoot) @@ -171,7 +173,7 @@ func TestStateBySlot_ColdState(t *testing.T) { genesisStateRoot, err := beaconState.HashTreeRoot(ctx) require.NoError(t, err) genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - assert.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) gRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) assert.NoError(t, beaconDB.SaveState(ctx, beaconState, gRoot)) @@ -179,7 +181,7 @@ func TestStateBySlot_ColdState(t *testing.T) { b, err := testutil.GenerateFullBlock(beaconState, pks, testutil.DefaultBlockGenConfig(), 1) require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) bRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveState(ctx, beaconState, bRoot)) @@ -204,7 +206,7 @@ func TestStateBySlot_HotStateDB(t *testing.T) { genesisStateRoot, err := beaconState.HashTreeRoot(ctx) require.NoError(t, err) genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - assert.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) gRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) assert.NoError(t, beaconDB.SaveState(ctx, beaconState, gRoot)) @@ -240,7 +242,7 @@ func TestLoadeStateByRoot_FinalizedState(t *testing.T) { genesisStateRoot, err := beaconState.HashTreeRoot(ctx) require.NoError(t, err) genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - assert.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) gRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 0, Root: gRoot[:]})) @@ -270,7 +272,7 @@ func TestLoadeStateByRoot_EpochBoundaryStateCanProcess(t *testing.T) { blk.Block.Slot = 11 blk.Block.ProposerIndex = 8 blk.Block.ParentRoot = gBlkRoot[:] - require.NoError(t, service.beaconDB.SaveBlock(ctx, blk)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) blkRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 10, Root: blkRoot[:]})) @@ -296,7 +298,7 @@ func TestLoadeStateByRoot_FromDBBoundaryCase(t *testing.T) { blk.Block.Slot = 11 blk.Block.ProposerIndex = 8 blk.Block.ParentRoot = gBlkRoot[:] - require.NoError(t, service.beaconDB.SaveBlock(ctx, blk)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) blkRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 10, Root: blkRoot[:]})) @@ -313,7 +315,7 @@ func TestLoadeStateBySlot_CanAdvanceSlotUsingDB(t *testing.T) { service := New(beaconDB) beaconState, _ := testutil.DeterministicGenesisState(t, 32) b := testutil.NewBeaconBlock() - require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, gRoot)) @@ -335,14 +337,14 @@ func TestLoadeStateBySlot_CanReplayBlock(t *testing.T) { stateRoot, err := genesis.HashTreeRoot(ctx) require.NoError(t, err) genesisBlk := blocks.NewGenesisBlock(stateRoot[:]) - require.NoError(t, beaconDB.SaveBlock(ctx, genesisBlk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesisBlk))) genesisBlkRoot, err := genesisBlk.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, genesisBlkRoot)) b1, err := testutil.GenerateFullBlock(genesis, keys, testutil.DefaultBlockGenConfig(), 1) assert.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, b1)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b1))) r1, err := b1.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 1, Root: r1[:]})) @@ -382,10 +384,10 @@ func TestLastAncestorState_CanGetUsingDB(t *testing.T) { require.NoError(t, err) require.NoError(t, b1State.SetSlot(1)) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b0)) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b1)) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b2)) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b3)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b0))) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b1))) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b2))) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b3))) require.NoError(t, service.beaconDB.SaveState(ctx, b1State, r1)) lastState, err := service.lastAncestorState(ctx, r3) @@ -422,10 +424,10 @@ func TestLastAncestorState_CanGetUsingCache(t *testing.T) { require.NoError(t, err) require.NoError(t, b1State.SetSlot(1)) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b0)) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b1)) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b2)) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b3)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b0))) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b1))) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b2))) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b3))) service.hotStateCache.put(r1, b1State) lastState, err := service.lastAncestorState(ctx, r3) diff --git a/beacon-chain/state/stategen/mock.go b/beacon-chain/state/stategen/mock.go index e9922580047d..a8e0e2645c79 100644 --- a/beacon-chain/state/stategen/mock.go +++ b/beacon-chain/state/stategen/mock.go @@ -3,8 +3,9 @@ package stategen import ( "context" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" - eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ) @@ -42,7 +43,7 @@ func (m *MockStateManager) MigrateToCold(ctx context.Context, fRoot [32]byte) er func (m *MockStateManager) ReplayBlocks( ctx context.Context, state iface.BeaconState, - signed []*eth.SignedBeaconBlock, + signed []interfaces.SignedBeaconBlock, targetSlot types.Slot, ) (iface.BeaconState, error) { panic("implement me") @@ -53,7 +54,7 @@ func (m *MockStateManager) LoadBlocks( ctx context.Context, startSlot, endSlot types.Slot, endBlockRoot [32]byte, -) ([]*eth.SignedBeaconBlock, error) { +) ([]interfaces.SignedBeaconBlock, error) { panic("implement me") } diff --git a/beacon-chain/sync/pending_attestations_queue_test.go b/beacon-chain/sync/pending_attestations_queue_test.go index 2a5c971ac83e..bf59a0ae08ec 100644 --- a/beacon-chain/sync/pending_attestations_queue_test.go +++ b/beacon-chain/sync/pending_attestations_queue_test.go @@ -5,6 +5,8 @@ import ( "testing" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/ethereum/go-ethereum/p2p/enr" lru "github.com/hashicorp/golang-lru" "github.com/libp2p/go-libp2p-core/network" @@ -62,7 +64,7 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) { beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) sb := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(context.Background(), sb)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(sb))) root, err := sb.Block.HashTreeRoot() require.NoError(t, err) @@ -126,7 +128,7 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) { sb = testutil.NewBeaconBlock() r32, err := sb.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), sb)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(sb))) s, err := testutil.NewBeaconState() require.NoError(t, err) require.NoError(t, r.cfg.DB.SaveState(context.Background(), s, r32)) @@ -171,7 +173,7 @@ func TestProcessPendingAtts_NoBroadcastWithBadSignature(t *testing.T) { b := testutil.NewBeaconBlock() r32, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) require.NoError(t, r.cfg.DB.SaveState(context.Background(), s, r32)) r.blkRootToPendingAtts[r32] = []*ethpb.SignedAggregateAttestationAndProof{{Message: a, Signature: make([]byte, 96)}} @@ -255,7 +257,7 @@ func TestProcessPendingAtts_HasBlockSaveAggregatedAtt(t *testing.T) { beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) sb := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(context.Background(), sb)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(sb))) root, err := sb.Block.HashTreeRoot() require.NoError(t, err) @@ -323,7 +325,7 @@ func TestProcessPendingAtts_HasBlockSaveAggregatedAtt(t *testing.T) { sb = testutil.NewBeaconBlock() r32, err := sb.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), sb)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(sb))) s, err := testutil.NewBeaconState() require.NoError(t, err) require.NoError(t, r.cfg.DB.SaveState(context.Background(), s, r32)) From f89097787ed0efc5653e3b0dea03385c4611908c Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 25 May 2021 16:31:10 +0800 Subject: [PATCH 14/44] more text fixes --- beacon-chain/core/blocks/BUILD.bazel | 1 + beacon-chain/db/BUILD.bazel | 1 + beacon-chain/db/restore_test.go | 6 +- beacon-chain/rpc/beacon/BUILD.bazel | 1 + beacon-chain/rpc/beacon/assignments_test.go | 4 +- beacon-chain/rpc/beacon/attestations_test.go | 148 ++++++++++--------- beacon-chain/rpc/beaconv1/BUILD.bazel | 1 + beacon-chain/rpc/statefetcher/BUILD.bazel | 1 + beacon-chain/state/stategen/BUILD.bazel | 2 +- beacon-chain/state/stategen/migrate_test.go | 14 +- beacon-chain/state/stategen/replay_test.go | 48 +++--- beacon-chain/sync/BUILD.bazel | 1 + 12 files changed, 124 insertions(+), 104 deletions(-) diff --git a/beacon-chain/core/blocks/BUILD.bazel b/beacon-chain/core/blocks/BUILD.bazel index 6b51a0bd9451..e9ece5717d02 100644 --- a/beacon-chain/core/blocks/BUILD.bazel +++ b/beacon-chain/core/blocks/BUILD.bazel @@ -84,6 +84,7 @@ go_test( "//shared/blockutil:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", diff --git a/beacon-chain/db/BUILD.bazel b/beacon-chain/db/BUILD.bazel index 859f4da8851a..9c5d4d4403ad 100644 --- a/beacon-chain/db/BUILD.bazel +++ b/beacon-chain/db/BUILD.bazel @@ -60,6 +60,7 @@ go_test( deps = [ "//beacon-chain/db/kv:go_default_library", "//shared/cmd:go_default_library", + "//shared/interfaces:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", "//shared/testutil/require:go_default_library", diff --git a/beacon-chain/db/restore_test.go b/beacon-chain/db/restore_test.go index 6850edc0c6a6..f052dccdaca8 100644 --- a/beacon-chain/db/restore_test.go +++ b/beacon-chain/db/restore_test.go @@ -8,6 +8,8 @@ import ( "path" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/db/kv" "github.com/prysmaticlabs/prysm/shared/cmd" @@ -26,7 +28,7 @@ func TestRestore(t *testing.T) { require.NoError(t, err) head := testutil.NewBeaconBlock() head.Block.Slot = 5000 - require.NoError(t, backupDb.SaveBlock(ctx, head)) + require.NoError(t, backupDb.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(head))) root, err := head.Block.HashTreeRoot() require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -63,7 +65,7 @@ func TestRestore(t *testing.T) { require.NoError(t, err) headBlock, err := restoredDb.HeadBlock(ctx) require.NoError(t, err) - assert.Equal(t, types.Slot(5000), headBlock.Block.Slot, "Restored database has incorrect data") + assert.Equal(t, types.Slot(5000), headBlock.Block().Slot(), "Restored database has incorrect data") assert.LogsContain(t, logHook, "Restore completed successfully") } diff --git a/beacon-chain/rpc/beacon/BUILD.bazel b/beacon-chain/rpc/beacon/BUILD.bazel index c1e3d0699ceb..1343914b4909 100644 --- a/beacon-chain/rpc/beacon/BUILD.bazel +++ b/beacon-chain/rpc/beacon/BUILD.bazel @@ -102,6 +102,7 @@ go_test( "//shared/bytesutil:go_default_library", "//shared/cmd:go_default_library", "//shared/featureconfig:go_default_library", + "//shared/interfaces:go_default_library", "//shared/mock:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", diff --git a/beacon-chain/rpc/beacon/assignments_test.go b/beacon-chain/rpc/beacon/assignments_test.go index acc5537f4750..a24380cde391 100644 --- a/beacon-chain/rpc/beacon/assignments_test.go +++ b/beacon-chain/rpc/beacon/assignments_test.go @@ -7,6 +7,8 @@ import ( "strconv" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" @@ -50,7 +52,7 @@ func TestServer_ListAssignments_NoResults(t *testing.T) { require.NoError(t, err) b := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot)) diff --git a/beacon-chain/rpc/beacon/attestations_test.go b/beacon-chain/rpc/beacon/attestations_test.go index 7246c4aaf5bc..ee09fc98e4ba 100644 --- a/beacon-chain/rpc/beacon/attestations_test.go +++ b/beacon-chain/rpc/beacon/attestations_test.go @@ -3,6 +3,7 @@ package beacon import ( "context" "fmt" + "github.com/prysmaticlabs/prysm/shared/interfaces" "sort" "strconv" "testing" @@ -91,7 +92,7 @@ func TestServer_ListAttestations_Genesis(t *testing.T) { signedBlock.Block.Body.Attestations = []*ethpb.Attestation{att} root, err := signedBlock.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, signedBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(signedBlock))) require.NoError(t, db.SaveGenesisBlockRoot(ctx, root)) wanted := ðpb.ListAttestationsResponse{ Attestations: []*ethpb.Attestation{att}, @@ -128,7 +129,7 @@ func TestServer_ListAttestations_NoPagination(t *testing.T) { AggregationBits: bitfield.Bitlist{0b11}, }, } - require.NoError(t, db.SaveBlock(ctx, blockExample)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blockExample))) atts = append(atts, blockExample.Block.Body.Attestations...) } @@ -155,82 +156,87 @@ func TestServer_ListAttestations_FiltersCorrectly(t *testing.T) { targetRoot := [32]byte{7, 8, 9} targetEpoch := types.Epoch(7) - blocks := []*ethpb.SignedBeaconBlock{ - testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: 4, - Body: ðpb.BeaconBlockBody{ - Attestations: []*ethpb.Attestation{ - { - Data: ðpb.AttestationData{ - BeaconBlockRoot: someRoot[:], - Source: ðpb.Checkpoint{ - Root: sourceRoot[:], - Epoch: sourceEpoch, - }, - Target: ðpb.Checkpoint{ - Root: targetRoot[:], - Epoch: targetEpoch, + blocks := []interfaces.SignedBeaconBlock{ + interfaces.NewWrappedSignedBeaconBlock( + testutil.HydrateSignedBeaconBlock( + ðpb.SignedBeaconBlock{ + Block: ðpb.BeaconBlock{ + Slot: 4, + Body: ðpb.BeaconBlockBody{ + Attestations: []*ethpb.Attestation{ + { + Data: ðpb.AttestationData{ + BeaconBlockRoot: someRoot[:], + Source: ðpb.Checkpoint{ + Root: sourceRoot[:], + Epoch: sourceEpoch, + }, + Target: ðpb.Checkpoint{ + Root: targetRoot[:], + Epoch: targetEpoch, + }, + Slot: 3, + }, + AggregationBits: bitfield.Bitlist{0b11}, + Signature: bytesutil.PadTo([]byte("sig"), 96), }, - Slot: 3, }, - AggregationBits: bitfield.Bitlist{0b11}, - Signature: bytesutil.PadTo([]byte("sig"), 96), }, }, - }, - }, - }), - testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: 5 + params.BeaconConfig().SlotsPerEpoch, - Body: ðpb.BeaconBlockBody{ - Attestations: []*ethpb.Attestation{ - { - Data: ðpb.AttestationData{ - BeaconBlockRoot: someRoot[:], - Source: ðpb.Checkpoint{ - Root: sourceRoot[:], - Epoch: sourceEpoch, + })), + interfaces.NewWrappedSignedBeaconBlock( + testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{ + Block: ðpb.BeaconBlock{ + Slot: 5 + params.BeaconConfig().SlotsPerEpoch, + Body: ðpb.BeaconBlockBody{ + Attestations: []*ethpb.Attestation{ + { + Data: ðpb.AttestationData{ + BeaconBlockRoot: someRoot[:], + Source: ðpb.Checkpoint{ + Root: sourceRoot[:], + Epoch: sourceEpoch, + }, + Target: ðpb.Checkpoint{ + Root: targetRoot[:], + Epoch: targetEpoch, + }, + Slot: 4 + params.BeaconConfig().SlotsPerEpoch, }, - Target: ðpb.Checkpoint{ - Root: targetRoot[:], - Epoch: targetEpoch, - }, - Slot: 4 + params.BeaconConfig().SlotsPerEpoch, + AggregationBits: bitfield.Bitlist{0b11}, + Signature: bytesutil.PadTo([]byte("sig"), 96), }, - AggregationBits: bitfield.Bitlist{0b11}, - Signature: bytesutil.PadTo([]byte("sig"), 96), }, }, }, - }, - }), - testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: 5, - Body: ðpb.BeaconBlockBody{ - Attestations: []*ethpb.Attestation{ - { - Data: ðpb.AttestationData{ - BeaconBlockRoot: someRoot[:], - Source: ðpb.Checkpoint{ - Root: sourceRoot[:], - Epoch: sourceEpoch, - }, - Target: ðpb.Checkpoint{ - Root: targetRoot[:], - Epoch: targetEpoch, + })), + interfaces.NewWrappedSignedBeaconBlock( + testutil.HydrateSignedBeaconBlock( + ðpb.SignedBeaconBlock{ + Block: ðpb.BeaconBlock{ + Slot: 5, + Body: ðpb.BeaconBlockBody{ + Attestations: []*ethpb.Attestation{ + { + Data: ðpb.AttestationData{ + BeaconBlockRoot: someRoot[:], + Source: ðpb.Checkpoint{ + Root: sourceRoot[:], + Epoch: sourceEpoch, + }, + Target: ðpb.Checkpoint{ + Root: targetRoot[:], + Epoch: targetEpoch, + }, + Slot: 4, + }, + AggregationBits: bitfield.Bitlist{0b11}, + Signature: bytesutil.PadTo([]byte("sig"), 96), }, - Slot: 4, }, - AggregationBits: bitfield.Bitlist{0b11}, - Signature: bytesutil.PadTo([]byte("sig"), 96), }, }, - }, - }, - }), + })), } require.NoError(t, db.SaveBlocks(ctx, blocks)) @@ -270,7 +276,7 @@ func TestServer_ListAttestations_Pagination_CustomPageParameters(t *testing.T) { AggregationBits: bitfield.Bitlist{0b11}, }), } - require.NoError(t, db.SaveBlock(ctx, blockExample)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blockExample))) atts = append(atts, blockExample.Block.Body.Attestations...) } } @@ -379,7 +385,7 @@ func TestServer_ListAttestations_Pagination_OutOfRange(t *testing.T) { }, }, }) - require.NoError(t, db.SaveBlock(ctx, blockExample)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blockExample))) atts = append(atts, blockExample.Block.Body.Attestations...) } @@ -430,7 +436,7 @@ func TestServer_ListAttestations_Pagination_DefaultPageSize(t *testing.T) { AggregationBits: bitfield.Bitlist{0b11}, }, } - require.NoError(t, db.SaveBlock(ctx, blockExample)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blockExample))) atts = append(atts, blockExample.Block.Body.Attestations...) } @@ -519,7 +525,7 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { AggregationBits: bitfield.NewBitlist(128 / uint64(params.BeaconConfig().SlotsPerEpoch)), }, } - require.NoError(t, db.SaveBlock(ctx, blockExample)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blockExample))) if i%2 == 0 { atts = append(atts, blockExample.Block.Body.Attestations...) } else { @@ -624,7 +630,7 @@ func TestServer_ListIndexedAttestations_OldEpoch(t *testing.T) { }, }, } - require.NoError(t, db.SaveBlock(ctx, blockExample)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blockExample))) atts = append(atts, blockExample.Block.Body.Attestations...) } @@ -848,7 +854,7 @@ func TestServer_StreamIndexedAttestations_OK(t *testing.T) { numValidators := 64 headState, privKeys := testutil.DeterministicGenesisState(t, uint64(numValidators)) b := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot)) diff --git a/beacon-chain/rpc/beaconv1/BUILD.bazel b/beacon-chain/rpc/beaconv1/BUILD.bazel index bcee8b557ff1..bd5fc90e6091 100644 --- a/beacon-chain/rpc/beaconv1/BUILD.bazel +++ b/beacon-chain/rpc/beaconv1/BUILD.bazel @@ -76,6 +76,7 @@ go_test( "//proto/migration:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", diff --git a/beacon-chain/rpc/statefetcher/BUILD.bazel b/beacon-chain/rpc/statefetcher/BUILD.bazel index 2c0c7f9a9c14..9222f6dd7a73 100644 --- a/beacon-chain/rpc/statefetcher/BUILD.bazel +++ b/beacon-chain/rpc/statefetcher/BUILD.bazel @@ -27,6 +27,7 @@ go_test( "//beacon-chain/state/stategen:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", diff --git a/beacon-chain/state/stategen/BUILD.bazel b/beacon-chain/state/stategen/BUILD.bazel index 5a7b75ef43ac..929a98a37777 100644 --- a/beacon-chain/state/stategen/BUILD.bazel +++ b/beacon-chain/state/stategen/BUILD.bazel @@ -36,7 +36,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_eth2_types//:go_default_library", - "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1: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", @@ -64,6 +63,7 @@ go_test( "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", diff --git a/beacon-chain/state/stategen/migrate_test.go b/beacon-chain/state/stategen/migrate_test.go index 808f9ec84155..798e92e0d63b 100644 --- a/beacon-chain/state/stategen/migrate_test.go +++ b/beacon-chain/state/stategen/migrate_test.go @@ -4,6 +4,8 @@ import ( "context" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" @@ -23,7 +25,7 @@ func TestMigrateToCold_CanSaveFinalizedInfo(t *testing.T) { b.Block.Slot = 1 br, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) require.NoError(t, service.epochBoundaryStateCache.put(br, beaconState)) require.NoError(t, service.MigrateToCold(ctx, br)) @@ -45,7 +47,7 @@ func TestMigrateToCold_HappyPath(t *testing.T) { b.Block.Slot = 2 fRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) require.NoError(t, service.epochBoundaryStateCache.put(fRoot, beaconState)) require.NoError(t, service.MigrateToCold(ctx, fRoot)) @@ -72,7 +74,7 @@ func TestMigrateToCold_RegeneratePath(t *testing.T) { genesisStateRoot, err := beaconState.HashTreeRoot(ctx) require.NoError(t, err) genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - assert.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) gRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) assert.NoError(t, beaconDB.SaveState(ctx, beaconState, gRoot)) @@ -82,14 +84,14 @@ func TestMigrateToCold_RegeneratePath(t *testing.T) { require.NoError(t, err) r1, err := b1.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b1)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b1))) require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 1, Root: r1[:]})) b4, err := testutil.GenerateFullBlock(beaconState, pks, testutil.DefaultBlockGenConfig(), 4) require.NoError(t, err) r4, err := b4.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b4)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b4))) require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 4, Root: r4[:]})) service.finalizedInfo = &finalizedInfo{ slot: 0, @@ -125,7 +127,7 @@ func TestMigrateToCold_StateExistsInDB(t *testing.T) { b.Block.Slot = 2 fRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) require.NoError(t, service.epochBoundaryStateCache.put(fRoot, beaconState)) require.NoError(t, service.beaconDB.SaveState(ctx, beaconState, fRoot)) diff --git a/beacon-chain/state/stategen/replay_test.go b/beacon-chain/state/stategen/replay_test.go index ae27d86b0fdd..263780b89561 100644 --- a/beacon-chain/state/stategen/replay_test.go +++ b/beacon-chain/state/stategen/replay_test.go @@ -4,6 +4,8 @@ import ( "context" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" @@ -42,7 +44,7 @@ func TestReplayBlocks_AllSkipSlots(t *testing.T) { service := New(beaconDB) targetSlot := params.BeaconConfig().SlotsPerEpoch - 1 - newState, err := service.ReplayBlocks(context.Background(), beaconState, []*ethpb.SignedBeaconBlock{}, targetSlot) + newState, err := service.ReplayBlocks(context.Background(), beaconState, []interfaces.SignedBeaconBlock{}, targetSlot) require.NoError(t, err) assert.Equal(t, targetSlot, newState.Slot(), "Did not advance slots") } @@ -71,7 +73,7 @@ func TestReplayBlocks_SameSlot(t *testing.T) { service := New(beaconDB) targetSlot := beaconState.Slot() - newState, err := service.ReplayBlocks(context.Background(), beaconState, []*ethpb.SignedBeaconBlock{}, targetSlot) + newState, err := service.ReplayBlocks(context.Background(), beaconState, []interfaces.SignedBeaconBlock{}, targetSlot) require.NoError(t, err) assert.Equal(t, targetSlot, newState.Slot(), "Did not advance slots") } @@ -103,7 +105,7 @@ func TestReplayBlocks_LowerSlotBlock(t *testing.T) { targetSlot := beaconState.Slot() b := testutil.NewBeaconBlock() b.Block.Slot = beaconState.Slot() - 1 - newState, err := service.ReplayBlocks(context.Background(), beaconState, []*ethpb.SignedBeaconBlock{b}, targetSlot) + newState, err := service.ReplayBlocks(context.Background(), beaconState, []interfaces.SignedBeaconBlock{interfaces.NewWrappedSignedBeaconBlock(b)}, targetSlot) require.NoError(t, err) assert.Equal(t, targetSlot, newState.Slot(), "Did not advance slots") } @@ -131,7 +133,7 @@ func TestLoadBlocks_FirstBranch(t *testing.T) { } for i, block := range wanted { - if !proto.Equal(block, filteredBlocks[i]) { + if !proto.Equal(block, filteredBlocks[i].Proto()) { t.Error("Did not get wanted blocks") } } @@ -158,7 +160,7 @@ func TestLoadBlocks_SecondBranch(t *testing.T) { } for i, block := range wanted { - if !proto.Equal(block, filteredBlocks[i]) { + if !proto.Equal(block, filteredBlocks[i].Proto()) { t.Error("Did not get wanted blocks") } } @@ -187,7 +189,7 @@ func TestLoadBlocks_ThirdBranch(t *testing.T) { } for i, block := range wanted { - if !proto.Equal(block, filteredBlocks[i]) { + if !proto.Equal(block, filteredBlocks[i].Proto()) { t.Error("Did not get wanted blocks") } } @@ -214,7 +216,7 @@ func TestLoadBlocks_SameSlots(t *testing.T) { } for i, block := range wanted { - if !proto.Equal(block, filteredBlocks[i]) { + if !proto.Equal(block, filteredBlocks[i].Proto()) { t.Error("Did not get wanted blocks") } } @@ -240,7 +242,7 @@ func TestLoadBlocks_SameEndSlots(t *testing.T) { } for i, block := range wanted { - if !proto.Equal(block, filteredBlocks[i]) { + if !proto.Equal(block, filteredBlocks[i].Proto()) { t.Error("Did not get wanted blocks") } } @@ -265,7 +267,7 @@ func TestLoadBlocks_SameEndSlotsWith2blocks(t *testing.T) { } for i, block := range wanted { - if !proto.Equal(block, filteredBlocks[i]) { + if !proto.Equal(block, filteredBlocks[i].Proto()) { t.Error("Did not get wanted blocks") } } @@ -295,7 +297,7 @@ func TestLastSavedBlock_Genesis(t *testing.T) { gBlk := testutil.NewBeaconBlock() gRoot, err := gBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, s.beaconDB.SaveBlock(ctx, gBlk)) + require.NoError(t, s.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(gBlk))) require.NoError(t, s.beaconDB.SaveGenesisBlockRoot(ctx, gRoot)) savedRoot, savedSlot, err := s.lastSavedBlock(ctx, 0) @@ -314,13 +316,13 @@ func TestLastSavedBlock_CanGet(t *testing.T) { b1 := testutil.NewBeaconBlock() b1.Block.Slot = s.finalizedInfo.slot + 5 - require.NoError(t, s.beaconDB.SaveBlock(ctx, b1)) + require.NoError(t, s.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b1))) b2 := testutil.NewBeaconBlock() b2.Block.Slot = s.finalizedInfo.slot + 10 - require.NoError(t, s.beaconDB.SaveBlock(ctx, b2)) + require.NoError(t, s.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b2))) b3 := testutil.NewBeaconBlock() b3.Block.Slot = s.finalizedInfo.slot + 20 - require.NoError(t, s.beaconDB.SaveBlock(ctx, b3)) + require.NoError(t, s.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b3))) savedRoot, savedSlot, err := s.lastSavedBlock(ctx, s.finalizedInfo.slot+100) require.NoError(t, err) @@ -358,7 +360,7 @@ func TestLastSavedState_Genesis(t *testing.T) { require.NoError(t, err) gRoot, err := gBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, s.beaconDB.SaveBlock(ctx, gBlk)) + require.NoError(t, s.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(gBlk))) require.NoError(t, s.beaconDB.SaveGenesisBlockRoot(ctx, gRoot)) require.NoError(t, s.beaconDB.SaveState(ctx, gState, gRoot)) @@ -377,10 +379,10 @@ func TestLastSavedState_CanGet(t *testing.T) { b1 := testutil.NewBeaconBlock() b1.Block.Slot = s.finalizedInfo.slot + 5 - require.NoError(t, s.beaconDB.SaveBlock(ctx, b1)) + require.NoError(t, s.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b1))) b2 := testutil.NewBeaconBlock() b2.Block.Slot = s.finalizedInfo.slot + 10 - require.NoError(t, s.beaconDB.SaveBlock(ctx, b2)) + require.NoError(t, s.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b2))) b2Root, err := b2.Block.HashTreeRoot() require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -390,7 +392,7 @@ func TestLastSavedState_CanGet(t *testing.T) { require.NoError(t, s.beaconDB.SaveState(ctx, st, b2Root)) b3 := testutil.NewBeaconBlock() b3.Block.Slot = s.finalizedInfo.slot + 20 - require.NoError(t, s.beaconDB.SaveBlock(ctx, b3)) + require.NoError(t, s.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b3))) savedState, err := s.lastSavedState(ctx, s.finalizedInfo.slot+100) require.NoError(t, err) @@ -407,7 +409,7 @@ func TestLastSavedState_NoSavedBlockState(t *testing.T) { b1 := testutil.NewBeaconBlock() b1.Block.Slot = 127 - require.NoError(t, s.beaconDB.SaveBlock(ctx, b1)) + require.NoError(t, s.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b1))) _, err := s.lastSavedState(ctx, s.finalizedInfo.slot+1) assert.ErrorContains(t, errUnknownState.Error(), err) @@ -489,7 +491,7 @@ func tree1(t *testing.T, beaconDB db.Database, genesisRoot []byte) ([][32]byte, beaconBlock := testutil.NewBeaconBlock() beaconBlock.Block.Slot = b.Block.Slot beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) - if err := beaconDB.SaveBlock(context.Background(), beaconBlock); err != nil { + if err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(beaconBlock)); err != nil { return nil, nil, err } if err := beaconDB.SaveState(context.Background(), st.Copy(), bytesutil.ToBytes32(beaconBlock.Block.ParentRoot)); err != nil { @@ -569,7 +571,7 @@ func tree2(t *testing.T, beaconDB db.Database, genesisRoot []byte) ([][32]byte, 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 := beaconDB.SaveBlock(context.Background(), beaconBlock); err != nil { + if err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(beaconBlock)); err != nil { return nil, nil, err } if err := beaconDB.SaveState(context.Background(), st.Copy(), bytesutil.ToBytes32(beaconBlock.Block.ParentRoot)); err != nil { @@ -642,7 +644,7 @@ func tree3(t *testing.T, beaconDB db.Database, genesisRoot []byte) ([][32]byte, 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 := beaconDB.SaveBlock(context.Background(), beaconBlock); err != nil { + if err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(beaconBlock)); err != nil { return nil, nil, err } if err := beaconDB.SaveState(context.Background(), st.Copy(), bytesutil.ToBytes32(beaconBlock.Block.ParentRoot)); err != nil { @@ -709,7 +711,7 @@ func tree4(t *testing.T, beaconDB db.Database, genesisRoot []byte) ([][32]byte, 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 := beaconDB.SaveBlock(context.Background(), beaconBlock); err != nil { + if err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(beaconBlock)); err != nil { return nil, nil, err } if err := beaconDB.SaveState(context.Background(), st.Copy(), bytesutil.ToBytes32(beaconBlock.Block.ParentRoot)); err != nil { @@ -730,7 +732,7 @@ func TestLoadFinalizedBlocks(t *testing.T) { gBlock := testutil.NewBeaconBlock() gRoot, err := gBlock.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, gBlock)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(gBlock))) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, [32]byte{})) roots, _, err := tree1(t, beaconDB, gRoot[:]) require.NoError(t, err) diff --git a/beacon-chain/sync/BUILD.bazel b/beacon-chain/sync/BUILD.bazel index 7bda614edeb7..3076b0b2543f 100644 --- a/beacon-chain/sync/BUILD.bazel +++ b/beacon-chain/sync/BUILD.bazel @@ -165,6 +165,7 @@ go_test( "//shared/blockutil:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/rand:go_default_library", "//shared/sszutil:go_default_library", From 0d1250418f0ae7fec9cf4af9a2bb7e9d43a2d3ce Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 25 May 2021 16:46:16 +0800 Subject: [PATCH 15/44] more text fixes --- beacon-chain/rpc/beacon/blocks_test.go | 50 ++++++------- beacon-chain/rpc/validator/attester_test.go | 14 ++-- beacon-chain/rpc/validator/exit_test.go | 6 +- beacon-chain/rpc/validator/proposer_test.go | 15 ++-- beacon-chain/rpc/validator/server_test.go | 4 +- beacon-chain/rpc/validator/status_test.go | 14 ++-- beacon-chain/state/stategen/service_test.go | 4 +- beacon-chain/state/stategen/setter_test.go | 6 +- .../sync/pending_blocks_queue_test.go | 72 ++++++++++--------- 9 files changed, 101 insertions(+), 84 deletions(-) diff --git a/beacon-chain/rpc/beacon/blocks_test.go b/beacon-chain/rpc/beacon/blocks_test.go index 56441b688d18..34c10f85b61f 100644 --- a/beacon-chain/rpc/beacon/blocks_test.go +++ b/beacon-chain/rpc/beacon/blocks_test.go @@ -6,6 +6,8 @@ import ( "strconv" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/golang/mock/gomock" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -92,7 +94,7 @@ func TestServer_ListBlocks_Genesis(t *testing.T) { blk.Block.ParentRoot = parentRoot[:] root, err := blk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, blk)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, db.SaveGenesisBlockRoot(ctx, root)) wanted := ðpb.ListBlocksResponse{ BlockContainers: []*ethpb.BeaconBlockContainer{ @@ -129,16 +131,16 @@ func TestServer_ListBlocks_Genesis_MultiBlocks(t *testing.T) { blk.Block.ParentRoot = parentRoot[:] root, err := blk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, blk)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, db.SaveGenesisBlockRoot(ctx, root)) count := types.Slot(100) - blks := make([]*ethpb.SignedBeaconBlock, count) + blks := make([]interfaces.SignedBeaconBlock, count) for i := types.Slot(0); i < count; i++ { b := testutil.NewBeaconBlock() b.Block.Slot = i require.NoError(t, err) - blks[i] = b + blks[i] = interfaces.NewWrappedSignedBeaconBlock(b) } require.NoError(t, db.SaveBlocks(ctx, blks)) @@ -162,7 +164,7 @@ func TestServer_ListBlocks_Pagination(t *testing.T) { ctx := context.Background() count := types.Slot(100) - blks := make([]*ethpb.SignedBeaconBlock, count) + blks := make([]interfaces.SignedBeaconBlock, count) blkContainers := make([]*ethpb.BeaconBlockContainer, count) for i := types.Slot(0); i < count; i++ { b := testutil.NewBeaconBlock() @@ -170,7 +172,7 @@ func TestServer_ListBlocks_Pagination(t *testing.T) { root, err := b.Block.HashTreeRoot() require.NoError(t, err) chain.CanonicalRoots[root] = true - blks[i] = b + blks[i] = interfaces.NewWrappedSignedBeaconBlock(b) blkContainers[i] = ðpb.BeaconBlockContainer{Block: b, BlockRoot: root[:], Canonical: true} } require.NoError(t, db.SaveBlocks(ctx, blks)) @@ -179,14 +181,14 @@ func TestServer_ListBlocks_Pagination(t *testing.T) { orphanedBlk.Block.Slot = 300 orphanedBlkRoot, err := orphanedBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, orphanedBlk)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(orphanedBlk))) bs := &Server{ BeaconDB: db, CanonicalFetcher: chain, } - root6, err := blks[6].Block.HashTreeRoot() + root6, err := blks[6].Block().HashTreeRoot() require.NoError(t, err) tests := []struct { @@ -333,14 +335,14 @@ func TestServer_GetChainHead_NoFinalizedBlock(t *testing.T) { genBlock := testutil.NewBeaconBlock() genBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'G'}, 32) - require.NoError(t, db.SaveBlock(context.Background(), genBlock)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genBlock))) gRoot, err := genBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), gRoot)) bs := &Server{ BeaconDB: db, - HeadFetcher: &chainMock.ChainService{Block: genBlock, State: s}, + HeadFetcher: &chainMock.ChainService{Block: interfaces.NewWrappedSignedBeaconBlock(genBlock), State: s}, FinalizationFetcher: &chainMock.ChainService{ FinalizedCheckPoint: s.FinalizedCheckpoint(), CurrentJustifiedCheckPoint: s.CurrentJustifiedCheckpoint(), @@ -366,7 +368,7 @@ func TestServer_GetChainHead(t *testing.T) { db := dbTest.SetupDB(t) genBlock := testutil.NewBeaconBlock() genBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'G'}, 32) - require.NoError(t, db.SaveBlock(context.Background(), genBlock)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genBlock))) gRoot, err := genBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), gRoot)) @@ -374,21 +376,21 @@ func TestServer_GetChainHead(t *testing.T) { finalizedBlock := testutil.NewBeaconBlock() finalizedBlock.Block.Slot = 1 finalizedBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'A'}, 32) - require.NoError(t, db.SaveBlock(context.Background(), finalizedBlock)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(finalizedBlock))) fRoot, err := finalizedBlock.Block.HashTreeRoot() require.NoError(t, err) justifiedBlock := testutil.NewBeaconBlock() justifiedBlock.Block.Slot = 2 justifiedBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'B'}, 32) - require.NoError(t, db.SaveBlock(context.Background(), justifiedBlock)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(justifiedBlock))) jRoot, err := justifiedBlock.Block.HashTreeRoot() require.NoError(t, err) prevJustifiedBlock := testutil.NewBeaconBlock() prevJustifiedBlock.Block.Slot = 3 prevJustifiedBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'C'}, 32) - require.NoError(t, db.SaveBlock(context.Background(), prevJustifiedBlock)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(prevJustifiedBlock))) pjRoot, err := prevJustifiedBlock.Block.HashTreeRoot() require.NoError(t, err) @@ -406,7 +408,7 @@ func TestServer_GetChainHead(t *testing.T) { b.Block.Slot++ bs := &Server{ BeaconDB: db, - HeadFetcher: &chainMock.ChainService{Block: b, State: s}, + HeadFetcher: &chainMock.ChainService{Block: interfaces.NewWrappedSignedBeaconBlock(b), State: s}, FinalizationFetcher: &chainMock.ChainService{ FinalizedCheckPoint: s.FinalizedCheckpoint(), CurrentJustifiedCheckPoint: s.CurrentJustifiedCheckpoint(), @@ -456,7 +458,7 @@ func TestServer_StreamChainHead_OnHeadUpdated(t *testing.T) { params.UseMainnetConfig() genBlock := testutil.NewBeaconBlock() genBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'G'}, 32) - require.NoError(t, db.SaveBlock(context.Background(), genBlock)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genBlock))) gRoot, err := genBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), gRoot)) @@ -464,21 +466,21 @@ func TestServer_StreamChainHead_OnHeadUpdated(t *testing.T) { finalizedBlock := testutil.NewBeaconBlock() finalizedBlock.Block.Slot = 32 finalizedBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'A'}, 32) - require.NoError(t, db.SaveBlock(context.Background(), finalizedBlock)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(finalizedBlock))) fRoot, err := finalizedBlock.Block.HashTreeRoot() require.NoError(t, err) justifiedBlock := testutil.NewBeaconBlock() justifiedBlock.Block.Slot = 64 justifiedBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'B'}, 32) - require.NoError(t, db.SaveBlock(context.Background(), justifiedBlock)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(justifiedBlock))) jRoot, err := justifiedBlock.Block.HashTreeRoot() require.NoError(t, err) prevJustifiedBlock := testutil.NewBeaconBlock() prevJustifiedBlock.Block.Slot = 96 prevJustifiedBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'C'}, 32) - require.NoError(t, db.SaveBlock(context.Background(), prevJustifiedBlock)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(prevJustifiedBlock))) pjRoot, err := prevJustifiedBlock.Block.HashTreeRoot() require.NoError(t, err) @@ -501,7 +503,7 @@ func TestServer_StreamChainHead_OnHeadUpdated(t *testing.T) { ctx := context.Background() server := &Server{ Ctx: ctx, - HeadFetcher: &chainMock.ChainService{Block: b, State: s}, + HeadFetcher: &chainMock.ChainService{Block: interfaces.NewWrappedSignedBeaconBlock(b), State: s}, BeaconDB: db, StateNotifier: chainService.StateNotifier(), FinalizationFetcher: &chainMock.ChainService{ @@ -629,7 +631,7 @@ func TestServer_StreamBlocks_OnHeadUpdated(t *testing.T) { for sent := 0; sent == 0; { sent = server.BlockNotifier.BlockFeed().Send(&feed.Event{ Type: blockfeed.ReceivedBlock, - Data: &blockfeed.ReceivedBlockData{SignedBlock: b}, + Data: &blockfeed.ReceivedBlockData{SignedBlock: interfaces.NewWrappedSignedBeaconBlock(b)}, }) } <-exitRoutine @@ -643,7 +645,7 @@ func TestServer_StreamBlocksVerified_OnHeadUpdated(t *testing.T) { require.NoError(t, err) r, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) chainService := &chainMock.ChainService{State: beaconState} server := &Server{ Ctx: ctx, @@ -670,7 +672,7 @@ func TestServer_StreamBlocksVerified_OnHeadUpdated(t *testing.T) { for sent := 0; sent == 0; { sent = server.StateNotifier.StateFeed().Send(&feed.Event{ Type: statefeed.BlockProcessed, - Data: &statefeed.BlockProcessedData{Slot: b.Block.Slot, BlockRoot: r, SignedBlock: b}, + Data: &statefeed.BlockProcessedData{Slot: b.Block.Slot, BlockRoot: r, SignedBlock: interfaces.NewWrappedSignedBeaconBlock(b)}, }) } <-exitRoutine @@ -707,7 +709,7 @@ func TestServer_GetWeakSubjectivityCheckpoint(t *testing.T) { genesisBlock := testutil.NewBeaconBlock() genesisBlockRoot, err := genesisBlock.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, genesisBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesisBlock))) require.NoError(t, db.SaveState(ctx, beaconState, genesisBlockRoot)) require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesisBlockRoot)) diff --git a/beacon-chain/rpc/validator/attester_test.go b/beacon-chain/rpc/validator/attester_test.go index 75a903863bf8..f8a35ce51860 100644 --- a/beacon-chain/rpc/validator/attester_test.go +++ b/beacon-chain/rpc/validator/attester_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" @@ -44,7 +46,7 @@ func TestProposeAttestation_OK(t *testing.T) { head := testutil.NewBeaconBlock() head.Block.Slot = 999 head.Block.ParentRoot = bytesutil.PadTo([]byte{'a'}, 32) - require.NoError(t, db.SaveBlock(ctx, head)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(head))) root, err := head.Block.HashTreeRoot() require.NoError(t, err) @@ -150,7 +152,7 @@ func TestGetAttestationData_OK(t *testing.T) { StateNotifier: chainService.StateNotifier(), } require.NoError(t, db.SaveState(ctx, beaconState, blockRoot)) - require.NoError(t, db.SaveBlock(ctx, block)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block))) require.NoError(t, db.SaveHeadBlockRoot(ctx, blockRoot)) req := ðpb.AttestationDataRequest{ @@ -251,7 +253,7 @@ func TestAttestationDataAtSlot_HandlesFarAwayJustifiedEpoch(t *testing.T) { StateNotifier: chainService.StateNotifier(), } require.NoError(t, db.SaveState(ctx, beaconState, blockRoot)) - require.NoError(t, db.SaveBlock(ctx, block)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block))) require.NoError(t, db.SaveHeadBlockRoot(ctx, blockRoot)) req := ðpb.AttestationDataRequest{ @@ -370,7 +372,7 @@ func TestServer_GetAttestationData_HeadStateSlotGreaterThanRequestSlot(t *testin require.NoError(t, err, "Could not hash beacon block") blockRoot2, err := block2.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, block2)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block2))) justifiedRoot, err := justifiedBlock.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root for justified block") targetRoot, err := targetBlock.Block.HashTreeRoot() @@ -416,7 +418,7 @@ func TestServer_GetAttestationData_HeadStateSlotGreaterThanRequestSlot(t *testin StateGen: stategen.New(db), } require.NoError(t, db.SaveState(ctx, beaconState, blockRoot)) - require.NoError(t, db.SaveBlock(ctx, block)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block))) require.NoError(t, db.SaveHeadBlockRoot(ctx, blockRoot)) req := ðpb.AttestationDataRequest{ @@ -494,7 +496,7 @@ func TestGetAttestationData_SucceedsInFirstEpoch(t *testing.T) { StateNotifier: chainService.StateNotifier(), } require.NoError(t, db.SaveState(ctx, beaconState, blockRoot)) - require.NoError(t, db.SaveBlock(ctx, block)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block))) require.NoError(t, db.SaveHeadBlockRoot(ctx, blockRoot)) req := ðpb.AttestationDataRequest{ diff --git a/beacon-chain/rpc/validator/exit_test.go b/beacon-chain/rpc/validator/exit_test.go index 840203d2e3d0..5325ab2d544d 100644 --- a/beacon-chain/rpc/validator/exit_test.go +++ b/beacon-chain/rpc/validator/exit_test.go @@ -5,6 +5,8 @@ import ( "testing" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" @@ -34,7 +36,7 @@ func TestProposeExit_Notification(t *testing.T) { epoch := types.Epoch(2048) require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(epoch)))) block := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, block), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block)), "Could not save genesis block") genesisRoot, err := block.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") @@ -105,7 +107,7 @@ func TestProposeExit_NoPanic(t *testing.T) { epoch := types.Epoch(2048) require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(epoch)))) block := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, block), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block)), "Could not save genesis block") genesisRoot, err := block.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") diff --git a/beacon-chain/rpc/validator/proposer_test.go b/beacon-chain/rpc/validator/proposer_test.go index bb5d0db94848..87256eff5a83 100644 --- a/beacon-chain/rpc/validator/proposer_test.go +++ b/beacon-chain/rpc/validator/proposer_test.go @@ -2,6 +2,7 @@ package validator import ( "context" + "github.com/prysmaticlabs/prysm/shared/interfaces" "math/big" "testing" @@ -48,7 +49,7 @@ func TestProposer_GetBlock_OK(t *testing.T) { require.NoError(t, err, "Could not hash genesis state") genesis := b.NewGenesisBlock(stateRoot[:]) - require.NoError(t, db.SaveBlock(ctx, genesis), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis)), "Could not save genesis block") parentRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") @@ -130,7 +131,7 @@ func TestProposer_GetBlock_AddsUnaggregatedAtts(t *testing.T) { require.NoError(t, err, "Could not hash genesis state") genesis := b.NewGenesisBlock(stateRoot[:]) - require.NoError(t, db.SaveBlock(ctx, genesis), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis)), "Could not save genesis block") parentRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") @@ -212,7 +213,7 @@ func TestProposer_ProposeBlock_OK(t *testing.T) { params.OverrideBeaconConfig(params.MainnetConfig()) genesis := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(context.Background(), genesis), "Could not save genesis block") + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genesis)), "Could not save genesis block") numDeposits := uint64(64) beaconState, _ := testutil.DeterministicGenesisState(t, numDeposits) @@ -236,7 +237,7 @@ func TestProposer_ProposeBlock_OK(t *testing.T) { req := testutil.NewBeaconBlock() req.Block.Slot = 5 req.Block.ParentRoot = bsRoot[:] - require.NoError(t, db.SaveBlock(ctx, req)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(req))) _, err = proposerServer.ProposeBlock(context.Background(), req) assert.NoError(t, err, "Could not propose block correctly") } @@ -253,7 +254,7 @@ func TestProposer_ComputeStateRoot_OK(t *testing.T) { require.NoError(t, err, "Could not hash genesis state") genesis := b.NewGenesisBlock(stateRoot[:]) - require.NoError(t, db.SaveBlock(ctx, genesis), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis)), "Could not save genesis block") parentRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") @@ -282,7 +283,7 @@ func TestProposer_ComputeStateRoot_OK(t *testing.T) { req.Signature, err = helpers.ComputeDomainAndSign(beaconState, currentEpoch, req.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) require.NoError(t, err) - _, err = proposerServer.computeStateRoot(context.Background(), req) + _, err = proposerServer.computeStateRoot(context.Background(), interfaces.NewWrappedSignedBeaconBlock(req)) require.NoError(t, err) } @@ -1819,7 +1820,7 @@ func TestProposer_FilterAttestation(t *testing.T) { params.SetupTestConfigCleanup(t) params.OverrideBeaconConfig(params.MainnetConfig()) genesis := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(context.Background(), genesis), "Could not save genesis block") + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genesis)), "Could not save genesis block") numValidators := uint64(64) state, privKeys := testutil.DeterministicGenesisState(t, numValidators) diff --git a/beacon-chain/rpc/validator/server_test.go b/beacon-chain/rpc/validator/server_test.go index 48ccc83a471e..bf04d038e544 100644 --- a/beacon-chain/rpc/validator/server_test.go +++ b/beacon-chain/rpc/validator/server_test.go @@ -6,6 +6,8 @@ import ( "testing" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/golang/mock/gomock" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" @@ -64,7 +66,7 @@ func TestWaitForActivation_ContextClosed(t *testing.T) { }) require.NoError(t, err) block := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, block), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block)), "Could not save genesis block") genesisRoot, err := block.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") diff --git a/beacon-chain/rpc/validator/status_test.go b/beacon-chain/rpc/validator/status_test.go index 9817679858f4..d2e463680cc8 100644 --- a/beacon-chain/rpc/validator/status_test.go +++ b/beacon-chain/rpc/validator/status_test.go @@ -5,6 +5,8 @@ import ( "testing" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" @@ -172,7 +174,7 @@ func TestValidatorStatus_Pending(t *testing.T) { pubKey := pubKey(1) block := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, block), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block)), "Could not save genesis block") genesisRoot, err := block.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") // Pending active because activation epoch is still defaulted at far future slot. @@ -259,7 +261,7 @@ func TestValidatorStatus_Active(t *testing.T) { activeEpoch := helpers.ActivationExitEpoch(0) block := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, block), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block)), "Could not save genesis block") genesisRoot, err := block.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") @@ -316,7 +318,7 @@ func TestValidatorStatus_Exiting(t *testing.T) { exitEpoch := helpers.ActivationExitEpoch(epoch) withdrawableEpoch := exitEpoch + params.BeaconConfig().MinValidatorWithdrawabilityDelay block := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, block), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block)), "Could not save genesis block") genesisRoot, err := block.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") @@ -377,7 +379,7 @@ func TestValidatorStatus_Slashing(t *testing.T) { slot := types.Slot(10000) epoch := helpers.SlotToEpoch(slot) block := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, block), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block)), "Could not save genesis block") genesisRoot, err := block.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") @@ -437,7 +439,7 @@ func TestValidatorStatus_Exited(t *testing.T) { slot := types.Slot(10000) epoch := helpers.SlotToEpoch(slot) block := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, block), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block)), "Could not save genesis block") genesisRoot, err := block.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") params.SetupTestConfigCleanup(t) @@ -614,7 +616,7 @@ func TestValidatorStatus_CorrectActivationQueue(t *testing.T) { pbKey := pubKey(5) block := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, block), "Could not save genesis block") + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block)), "Could not save genesis block") genesisRoot, err := block.Block.HashTreeRoot() require.NoError(t, err, "Could not get signing root") currentSlot := types.Slot(5000) diff --git a/beacon-chain/state/stategen/service_test.go b/beacon-chain/state/stategen/service_test.go index 755b765d5118..f22e5e722ff7 100644 --- a/beacon-chain/state/stategen/service_test.go +++ b/beacon-chain/state/stategen/service_test.go @@ -4,6 +4,8 @@ import ( "context" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/shared/params" @@ -18,7 +20,7 @@ func TestResume(t *testing.T) { service := New(beaconDB) b := testutil.NewBeaconBlock() - require.NoError(t, service.beaconDB.SaveBlock(ctx, b)) + require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) root, err := b.Block.HashTreeRoot() require.NoError(t, err) beaconState, _ := testutil.DeterministicGenesisState(t, 32) diff --git a/beacon-chain/state/stategen/setter_test.go b/beacon-chain/state/stategen/setter_test.go index f60c60069c0c..81367c6e7c8f 100644 --- a/beacon-chain/state/stategen/setter_test.go +++ b/beacon-chain/state/stategen/setter_test.go @@ -4,6 +4,8 @@ import ( "context" "testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" + testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -122,7 +124,7 @@ func TestSaveState_NoSaveNotEpochBoundary(t *testing.T) { require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch-1)) r := [32]byte{'A'} b := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, gRoot)) @@ -182,7 +184,7 @@ func TestEnableSaveHotStateToDB_Disabled(t *testing.T) { service := New(beaconDB) service.saveHotStateDB.enabled = true b := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) service.saveHotStateDB.savedStateRoots = [][32]byte{r} diff --git a/beacon-chain/sync/pending_blocks_queue_test.go b/beacon-chain/sync/pending_blocks_queue_test.go index 37efd626e511..97ce3c0eb23d 100644 --- a/beacon-chain/sync/pending_blocks_queue_test.go +++ b/beacon-chain/sync/pending_blocks_queue_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/ethereum/go-ethereum/p2p/enr" @@ -54,13 +56,13 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks1(t *testing.T) { require.NoError(t, err) b0 := testutil.NewBeaconBlock() - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b0)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b0))) b0Root, err := b0.Block.HashTreeRoot() require.NoError(t, err) b3 := testutil.NewBeaconBlock() b3.Block.Slot = 3 b3.Block.ParentRoot = b0Root[:] - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b3)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b3))) // Incomplete block link b1 := testutil.NewBeaconBlock() b1.Block.Slot = 1 @@ -74,18 +76,18 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks1(t *testing.T) { require.NoError(t, err) // Add b2 to the cache - require.NoError(t, r.insertBlockToPendingQueue(b2.Block.Slot, b2, b2Root)) + require.NoError(t, r.insertBlockToPendingQueue(b2.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b2), b2Root)) require.NoError(t, r.processPendingBlocks(context.Background())) assert.Equal(t, 1, len(r.slotToPendingBlocks.Items()), "Incorrect size for slot to pending blocks cache") assert.Equal(t, 1, len(r.seenPendingBlocks), "Incorrect size for seen pending block") // Add b1 to the cache - require.NoError(t, r.insertBlockToPendingQueue(b1.Block.Slot, b1, b1Root)) - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b1)) + require.NoError(t, r.insertBlockToPendingQueue(b1.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b1), b1Root)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b1))) // Insert bad b1 in the cache to verify the good one doesn't get replaced. - require.NoError(t, r.insertBlockToPendingQueue(b1.Block.Slot, testutil.NewBeaconBlock(), [32]byte{})) + require.NoError(t, r.insertBlockToPendingQueue(b1.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock()), [32]byte{})) require.NoError(t, r.processPendingBlocks(context.Background())) // Marks a block as bad require.NoError(t, r.processPendingBlocks(context.Background())) // Bad block removed on second run @@ -117,7 +119,7 @@ func TestRegularSync_InsertDuplicateBlocks(t *testing.T) { b0 := testutil.NewBeaconBlock() b0r := [32]byte{'a'} - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b0)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b0))) b0Root, err := b0.Block.HashTreeRoot() require.NoError(t, err) b1 := testutil.NewBeaconBlock() @@ -125,18 +127,18 @@ func TestRegularSync_InsertDuplicateBlocks(t *testing.T) { b1.Block.ParentRoot = b0Root[:] b1r := [32]byte{'b'} - require.NoError(t, r.insertBlockToPendingQueue(b0.Block.Slot, b0, b0r)) + require.NoError(t, r.insertBlockToPendingQueue(b0.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b0), b0r)) require.Equal(t, 1, len(r.pendingBlocksInCache(b0.Block.Slot)), "Block was not added to map") - require.NoError(t, r.insertBlockToPendingQueue(b1.Block.Slot, b1, b1r)) + require.NoError(t, r.insertBlockToPendingQueue(b1.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b1), b1r)) require.Equal(t, 1, len(r.pendingBlocksInCache(b1.Block.Slot)), "Block was not added to map") // Add duplicate block which should not be saved. - require.NoError(t, r.insertBlockToPendingQueue(b0.Block.Slot, b0, b0r)) + require.NoError(t, r.insertBlockToPendingQueue(b0.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b0), b0r)) require.Equal(t, 1, len(r.pendingBlocksInCache(b0.Block.Slot)), "Block was added to map") // Add duplicate block which should not be saved. - require.NoError(t, r.insertBlockToPendingQueue(b1.Block.Slot, b1, b1r)) + require.NoError(t, r.insertBlockToPendingQueue(b1.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b1), b1r)) require.Equal(t, 1, len(r.pendingBlocksInCache(b1.Block.Slot)), "Block was added to map") } @@ -189,13 +191,13 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks_2Chains(t *testin p1.Peers().SetChainState(p2.PeerID(), &pb.Status{}) b0 := testutil.NewBeaconBlock() - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b0)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b0))) b0Root, err := b0.Block.HashTreeRoot() require.NoError(t, err) b1 := testutil.NewBeaconBlock() b1.Block.Slot = 1 b1.Block.ParentRoot = b0Root[:] - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b1)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b1))) b1Root, err := b1.Block.HashTreeRoot() require.NoError(t, err) @@ -221,8 +223,8 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks_2Chains(t *testin b4Root, err := b4.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, r.insertBlockToPendingQueue(b4.Block.Slot, b4, b4Root)) - require.NoError(t, r.insertBlockToPendingQueue(b5.Block.Slot, b5, b5Root)) + require.NoError(t, r.insertBlockToPendingQueue(b4.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b4), b4Root)) + require.NoError(t, r.insertBlockToPendingQueue(b5.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b5), b5Root)) require.NoError(t, r.processPendingBlocks(context.Background())) // Marks a block as bad require.NoError(t, r.processPendingBlocks(context.Background())) // Bad block removed on second run @@ -231,8 +233,8 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks_2Chains(t *testin assert.Equal(t, 2, len(r.seenPendingBlocks), "Incorrect size for seen pending block") // Add b3 to the cache - require.NoError(t, r.insertBlockToPendingQueue(b3.Block.Slot, b3, b3Root)) - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b3)) + require.NoError(t, r.insertBlockToPendingQueue(b3.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b3), b3Root)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b3))) require.NoError(t, r.processPendingBlocks(context.Background())) // Marks a block as bad require.NoError(t, r.processPendingBlocks(context.Background())) // Bad block removed on second run @@ -241,9 +243,9 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks_2Chains(t *testin assert.Equal(t, 3, len(r.seenPendingBlocks), "Incorrect size for seen pending block") // Add b2 to the cache - require.NoError(t, r.insertBlockToPendingQueue(b2.Block.Slot, b2, b2Root)) + require.NoError(t, r.insertBlockToPendingQueue(b2.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b2), b2Root)) - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b2)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b2))) require.NoError(t, r.processPendingBlocks(context.Background())) // Marks a block as bad require.NoError(t, r.processPendingBlocks(context.Background())) // Bad block removed on second run @@ -280,13 +282,13 @@ func TestRegularSyncBeaconBlockSubscriber_PruneOldPendingBlocks(t *testing.T) { p1.Peers().SetChainState(p1.PeerID(), &pb.Status{}) b0 := testutil.NewBeaconBlock() - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b0)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b0))) b0Root, err := b0.Block.HashTreeRoot() require.NoError(t, err) b1 := testutil.NewBeaconBlock() b1.Block.Slot = 1 b1.Block.ParentRoot = b0Root[:] - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b1)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b1))) b1Root, err := b1.Block.HashTreeRoot() require.NoError(t, err) @@ -312,10 +314,10 @@ func TestRegularSyncBeaconBlockSubscriber_PruneOldPendingBlocks(t *testing.T) { b4Root, err := b4.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, r.insertBlockToPendingQueue(b2.Block.Slot, b2, b2Root)) - require.NoError(t, r.insertBlockToPendingQueue(b3.Block.Slot, b3, b3Root)) - require.NoError(t, r.insertBlockToPendingQueue(b4.Block.Slot, b4, b4Root)) - require.NoError(t, r.insertBlockToPendingQueue(b5.Block.Slot, b5, b5Root)) + require.NoError(t, r.insertBlockToPendingQueue(b2.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b2), b2Root)) + require.NoError(t, r.insertBlockToPendingQueue(b3.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b3), b3Root)) + require.NoError(t, r.insertBlockToPendingQueue(b4.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b4), b4Root)) + require.NoError(t, r.insertBlockToPendingQueue(b5.Block.Slot, interfaces.NewWrappedSignedBeaconBlock(b5), b5Root)) require.NoError(t, r.processPendingBlocks(context.Background())) assert.Equal(t, 0, len(r.slotToPendingBlocks.Items()), "Incorrect size for slot to pending blocks cache") @@ -329,10 +331,10 @@ func TestService_sortedPendingSlots(t *testing.T) { } var lastSlot types.Slot = math.MaxUint64 - require.NoError(t, r.insertBlockToPendingQueue(lastSlot, testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: lastSlot}}), [32]byte{1})) - require.NoError(t, r.insertBlockToPendingQueue(lastSlot-3, testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: lastSlot - 3}}), [32]byte{2})) - require.NoError(t, r.insertBlockToPendingQueue(lastSlot-5, testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: lastSlot - 5}}), [32]byte{3})) - require.NoError(t, r.insertBlockToPendingQueue(lastSlot-2, testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: lastSlot - 2}}), [32]byte{4})) + require.NoError(t, r.insertBlockToPendingQueue(lastSlot, interfaces.NewWrappedSignedBeaconBlock(testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: lastSlot}})), [32]byte{1})) + require.NoError(t, r.insertBlockToPendingQueue(lastSlot-3, interfaces.NewWrappedSignedBeaconBlock(testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: lastSlot - 3}})), [32]byte{2})) + require.NoError(t, r.insertBlockToPendingQueue(lastSlot-5, interfaces.NewWrappedSignedBeaconBlock(testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: lastSlot - 5}})), [32]byte{3})) + require.NoError(t, r.insertBlockToPendingQueue(lastSlot-2, interfaces.NewWrappedSignedBeaconBlock(testutil.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: lastSlot - 2}})), [32]byte{4})) want := []types.Slot{lastSlot - 5, lastSlot - 3, lastSlot - 2, lastSlot} assert.DeepEqual(t, want, r.sortedPendingSlots(), "Unexpected pending slots list") @@ -367,13 +369,13 @@ func TestService_BatchRootRequest(t *testing.T) { p1.Peers().SetChainState(p2.PeerID(), &pb.Status{FinalizedEpoch: 2}) b0 := testutil.NewBeaconBlock() - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b0)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b0))) b0Root, err := b0.Block.HashTreeRoot() require.NoError(t, err) b1 := testutil.NewBeaconBlock() b1.Block.Slot = 1 b1.Block.ParentRoot = b0Root[:] - require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), b1)) + require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b1))) b1Root, err := b1.Block.HashTreeRoot() require.NoError(t, err) @@ -440,12 +442,12 @@ func TestService_AddPeningBlockToQueueOverMax(t *testing.T) { b1.Block.StateRoot = []byte{'a'} b2 := blockutil.CopySignedBeaconBlock(b) b2.Block.StateRoot = []byte{'b'} - require.NoError(t, r.insertBlockToPendingQueue(0, b, [32]byte{})) - require.NoError(t, r.insertBlockToPendingQueue(0, b1, [32]byte{1})) - require.NoError(t, r.insertBlockToPendingQueue(0, b2, [32]byte{2})) + require.NoError(t, r.insertBlockToPendingQueue(0, interfaces.NewWrappedSignedBeaconBlock(b), [32]byte{})) + require.NoError(t, r.insertBlockToPendingQueue(0, interfaces.NewWrappedSignedBeaconBlock(b1), [32]byte{1})) + require.NoError(t, r.insertBlockToPendingQueue(0, interfaces.NewWrappedSignedBeaconBlock(b2), [32]byte{2})) b3 := blockutil.CopySignedBeaconBlock(b) b3.Block.StateRoot = []byte{'c'} - require.NoError(t, r.insertBlockToPendingQueue(0, b2, [32]byte{3})) + require.NoError(t, r.insertBlockToPendingQueue(0, interfaces.NewWrappedSignedBeaconBlock(b2), [32]byte{3})) require.Equal(t, maxBlocksPerSlot, len(r.pendingBlocksInCache(0))) } From 237b785bba439c0fd2ae6d36beb56cc338e5a006 Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 25 May 2021 17:00:10 +0800 Subject: [PATCH 16/44] more text fixes --- beacon-chain/db/kv/backup_test.go | 7 +- beacon-chain/db/kv/blocks_test.go | 113 +++++++++++---------- beacon-chain/rpc/beacon/committees_test.go | 7 +- beacon-chain/rpc/beacon/validators_test.go | 29 +++--- beacon-chain/rpc/beaconv1/state_test.go | 23 +++-- beacon-chain/rpc/debug/BUILD.bazel | 1 + beacon-chain/rpc/debug/block_test.go | 5 +- beacon-chain/rpc/debug/state_test.go | 3 +- beacon-chain/rpc/validator/BUILD.bazel | 1 + 9 files changed, 102 insertions(+), 87 deletions(-) diff --git a/beacon-chain/db/kv/backup_test.go b/beacon-chain/db/kv/backup_test.go index fd8e8028346c..a19b922d9900 100644 --- a/beacon-chain/db/kv/backup_test.go +++ b/beacon-chain/db/kv/backup_test.go @@ -8,6 +8,7 @@ import ( "testing" types "github.com/prysmaticlabs/eth2-types" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) @@ -20,7 +21,7 @@ func TestStore_Backup(t *testing.T) { head := testutil.NewBeaconBlock() head.Block.Slot = 5000 - require.NoError(t, db.SaveBlock(ctx, head)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(head))) root, err := head.Block.HashTreeRoot() require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -60,7 +61,7 @@ func TestStore_BackupMultipleBuckets(t *testing.T) { for i := startSlot; i < 5200; i++ { head := testutil.NewBeaconBlock() head.Block.Slot = i - require.NoError(t, db.SaveBlock(ctx, head)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(head))) root, err := head.Block.HashTreeRoot() require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -97,7 +98,7 @@ func TestStore_BackupMultipleBuckets(t *testing.T) { nBlock, err := backedDB.Block(ctx, root) require.NoError(t, err) require.NotNil(t, nBlock) - require.Equal(t, nBlock.Block.Slot, i) + require.Equal(t, nBlock.Block().Slot(), i) nState, err := backedDB.State(ctx, root) require.NoError(t, err) require.NotNil(t, nState) diff --git a/beacon-chain/db/kv/blocks_test.go b/beacon-chain/db/kv/blocks_test.go index da48525c3400..1388faa19958 100644 --- a/beacon-chain/db/kv/blocks_test.go +++ b/beacon-chain/db/kv/blocks_test.go @@ -9,6 +9,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -25,7 +26,7 @@ func TestStore_SaveBlock_NoDuplicates(t *testing.T) { 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)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(prevBlock))) block := testutil.NewBeaconBlock() block.Block.Slot = slot @@ -33,7 +34,7 @@ func TestStore_SaveBlock_NoDuplicates(t *testing.T) { // Even with a full cache, saving new blocks should not cause // duplicated blocks in the DB. for i := 0; i < 100; i++ { - require.NoError(t, db.SaveBlock(ctx, block)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block))) } f := filters.NewFilter().SetStartSlot(slot).SetEndSlot(slot) retrieved, _, err := db.Blocks(ctx, f) @@ -56,11 +57,11 @@ func TestStore_BlocksCRUD(t *testing.T) { retrievedBlock, err := db.Block(ctx, blockRoot) require.NoError(t, err) assert.Equal(t, (*ethpb.SignedBeaconBlock)(nil), retrievedBlock, "Expected nil block") - require.NoError(t, db.SaveBlock(ctx, block)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block))) assert.Equal(t, true, db.HasBlock(ctx, blockRoot), "Expected block to exist in the db") retrievedBlock, err = db.Block(ctx, blockRoot) require.NoError(t, err) - assert.Equal(t, true, proto.Equal(block, retrievedBlock), "Wanted: %v, received: %v", block, retrievedBlock) + assert.Equal(t, true, proto.Equal(block, retrievedBlock.Proto()), "Wanted: %v, received: %v", block, retrievedBlock) require.NoError(t, db.deleteBlock(ctx, blockRoot)) assert.Equal(t, false, db.HasBlock(ctx, blockRoot), "Expected block to have been deleted from the db") } @@ -69,16 +70,16 @@ func TestStore_BlocksBatchDelete(t *testing.T) { db := setupDB(t) ctx := context.Background() numBlocks := 10 - totalBlocks := make([]*ethpb.SignedBeaconBlock, numBlocks) + totalBlocks := make([]interfaces.SignedBeaconBlock, numBlocks) blockRoots := make([][32]byte, 0) - oddBlocks := make([]*ethpb.SignedBeaconBlock, 0) + oddBlocks := make([]interfaces.SignedBeaconBlock, 0) for i := 0; i < len(totalBlocks); i++ { b := testutil.NewBeaconBlock() b.Block.Slot = types.Slot(i) b.Block.ParentRoot = bytesutil.PadTo([]byte("parent"), 32) - totalBlocks[i] = b + totalBlocks[i] = interfaces.NewWrappedSignedBeaconBlock(b) if i%2 == 0 { - r, err := totalBlocks[i].Block.HashTreeRoot() + r, err := totalBlocks[i].Block().HashTreeRoot() require.NoError(t, err) blockRoots = append(blockRoots, r) } else { @@ -95,10 +96,10 @@ func TestStore_BlocksBatchDelete(t *testing.T) { retrieved, _, err = db.Blocks(ctx, filters.NewFilter().SetParentRoot(bytesutil.PadTo([]byte("parent"), 32))) require.NoError(t, err) sort.Slice(retrieved, func(i, j int) bool { - return retrieved[i].Block.Slot < retrieved[j].Block.Slot + return retrieved[i].Block().Slot() < retrieved[j].Block().Slot() }) for i, block := range retrieved { - assert.Equal(t, true, proto.Equal(block, oddBlocks[i]), "Wanted: %v, received: %v", block, oddBlocks[i]) + assert.Equal(t, true, proto.Equal(block.Proto(), oddBlocks[i].Proto()), "Wanted: %v, received: %v", block, oddBlocks[i]) } } @@ -106,13 +107,13 @@ func TestStore_BlocksHandleZeroCase(t *testing.T) { db := setupDB(t) ctx := context.Background() numBlocks := 10 - totalBlocks := make([]*ethpb.SignedBeaconBlock, numBlocks) + totalBlocks := make([]interfaces.SignedBeaconBlock, numBlocks) for i := 0; i < len(totalBlocks); i++ { b := testutil.NewBeaconBlock() b.Block.Slot = types.Slot(i) b.Block.ParentRoot = bytesutil.PadTo([]byte("parent"), 32) - totalBlocks[i] = b - _, err := totalBlocks[i].Block.HashTreeRoot() + totalBlocks[i] = interfaces.NewWrappedSignedBeaconBlock(b) + _, err := totalBlocks[i].Block().HashTreeRoot() require.NoError(t, err) } require.NoError(t, db.SaveBlocks(ctx, totalBlocks)) @@ -126,14 +127,14 @@ func TestStore_BlocksHandleInvalidEndSlot(t *testing.T) { db := setupDB(t) ctx := context.Background() numBlocks := 10 - totalBlocks := make([]*ethpb.SignedBeaconBlock, numBlocks) + totalBlocks := make([]interfaces.SignedBeaconBlock, numBlocks) // Save blocks from slot 1 onwards. for i := 0; i < len(totalBlocks); i++ { b := testutil.NewBeaconBlock() b.Block.Slot = types.Slot(i) + 1 b.Block.ParentRoot = bytesutil.PadTo([]byte("parent"), 32) - totalBlocks[i] = b - _, err := totalBlocks[i].Block.HashTreeRoot() + totalBlocks[i] = interfaces.NewWrappedSignedBeaconBlock(b) + _, err := totalBlocks[i].Block().HashTreeRoot() require.NoError(t, err) } require.NoError(t, db.SaveBlocks(ctx, totalBlocks)) @@ -155,10 +156,10 @@ func TestStore_GenesisBlock(t *testing.T) { blockRoot, err := genesisBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(ctx, blockRoot)) - require.NoError(t, db.SaveBlock(ctx, genesisBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesisBlock))) retrievedBlock, err := db.GenesisBlock(ctx) require.NoError(t, err) - assert.Equal(t, true, proto.Equal(genesisBlock, retrievedBlock), "Wanted: %v, received: %v", genesisBlock, retrievedBlock) + assert.Equal(t, true, proto.Equal(genesisBlock, retrievedBlock.Proto()), "Wanted: %v, received: %v", genesisBlock, retrievedBlock) } func TestStore_BlocksCRUD_NoCache(t *testing.T) { @@ -172,12 +173,12 @@ func TestStore_BlocksCRUD_NoCache(t *testing.T) { retrievedBlock, err := db.Block(ctx, blockRoot) require.NoError(t, err) require.Equal(t, (*ethpb.SignedBeaconBlock)(nil), retrievedBlock, "Expected nil block") - require.NoError(t, db.SaveBlock(ctx, block)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block))) db.blockCache.Del(string(blockRoot[:])) assert.Equal(t, true, db.HasBlock(ctx, blockRoot), "Expected block to exist in the db") retrievedBlock, err = db.Block(ctx, blockRoot) require.NoError(t, err) - assert.Equal(t, true, proto.Equal(block, retrievedBlock), "Wanted: %v, received: %v", block, retrievedBlock) + assert.Equal(t, true, proto.Equal(block, retrievedBlock.Proto()), "Wanted: %v, received: %v", block, retrievedBlock) require.NoError(t, db.deleteBlock(ctx, blockRoot)) assert.Equal(t, false, db.HasBlock(ctx, blockRoot), "Expected block to have been deleted from the db") } @@ -199,7 +200,13 @@ func TestStore_Blocks_FiltersCorrectly(t *testing.T) { b8 := testutil.NewBeaconBlock() b8.Block.Slot = 8 b8.Block.ParentRoot = bytesutil.PadTo([]byte("parent4"), 32) - blocks := []*ethpb.SignedBeaconBlock{b4, b5, b6, b7, b8} + blocks := []interfaces.SignedBeaconBlock{ + interfaces.NewWrappedSignedBeaconBlock(b4), + interfaces.NewWrappedSignedBeaconBlock(b5), + interfaces.NewWrappedSignedBeaconBlock(b6), + interfaces.NewWrappedSignedBeaconBlock(b7), + interfaces.NewWrappedSignedBeaconBlock(b8), + } ctx := context.Background() require.NoError(t, db.SaveBlocks(ctx, blocks)) @@ -277,8 +284,8 @@ func TestStore_Blocks_VerifyBlockRoots(t *testing.T) { r2, err := b2.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, b1)) - require.NoError(t, db.SaveBlock(ctx, b2)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b1))) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b2))) filter := filters.NewFilter().SetStartSlot(b1.Block.Slot).SetEndSlot(b2.Block.Slot) roots, err := db.BlockRoots(ctx, filter) @@ -289,12 +296,12 @@ func TestStore_Blocks_VerifyBlockRoots(t *testing.T) { func TestStore_Blocks_Retrieve_SlotRange(t *testing.T) { db := setupDB(t) - totalBlocks := make([]*ethpb.SignedBeaconBlock, 500) + totalBlocks := make([]interfaces.SignedBeaconBlock, 500) for i := 0; i < 500; i++ { b := testutil.NewBeaconBlock() b.Block.Slot = types.Slot(i) b.Block.ParentRoot = bytesutil.PadTo([]byte("parent"), 32) - totalBlocks[i] = b + totalBlocks[i] = interfaces.NewWrappedSignedBeaconBlock(b) } ctx := context.Background() require.NoError(t, db.SaveBlocks(ctx, totalBlocks)) @@ -306,12 +313,12 @@ func TestStore_Blocks_Retrieve_SlotRange(t *testing.T) { func TestStore_Blocks_Retrieve_Epoch(t *testing.T) { db := setupDB(t) slots := params.BeaconConfig().SlotsPerEpoch.Mul(7) - totalBlocks := make([]*ethpb.SignedBeaconBlock, slots) + totalBlocks := make([]interfaces.SignedBeaconBlock, slots) for i := types.Slot(0); i < slots; i++ { b := testutil.NewBeaconBlock() b.Block.Slot = i b.Block.ParentRoot = bytesutil.PadTo([]byte("parent"), 32) - totalBlocks[i] = b + totalBlocks[i] = interfaces.NewWrappedSignedBeaconBlock(b) } ctx := context.Background() require.NoError(t, db.SaveBlocks(ctx, totalBlocks)) @@ -327,12 +334,12 @@ func TestStore_Blocks_Retrieve_Epoch(t *testing.T) { func TestStore_Blocks_Retrieve_SlotRangeWithStep(t *testing.T) { db := setupDB(t) - totalBlocks := make([]*ethpb.SignedBeaconBlock, 500) + totalBlocks := make([]interfaces.SignedBeaconBlock, 500) for i := 0; i < 500; i++ { b := testutil.NewBeaconBlock() b.Block.Slot = types.Slot(i) b.Block.ParentRoot = bytesutil.PadTo([]byte("parent"), 32) - totalBlocks[i] = b + totalBlocks[i] = interfaces.NewWrappedSignedBeaconBlock(b) } const step = 2 ctx := context.Background() @@ -341,7 +348,7 @@ func TestStore_Blocks_Retrieve_SlotRangeWithStep(t *testing.T) { require.NoError(t, err) assert.Equal(t, 150, len(retrieved)) for _, b := range retrieved { - assert.Equal(t, types.Slot(0), (b.Block.Slot-100)%step, "Unexpect block slot %d", b.Block.Slot) + assert.Equal(t, types.Slot(0), (b.Block().Slot()-100)%step, "Unexpect block slot %d", b.Block().Slot()) } } @@ -351,26 +358,26 @@ func TestStore_SaveBlock_CanGetHighestAt(t *testing.T) { block1 := testutil.NewBeaconBlock() block1.Block.Slot = 1 - require.NoError(t, db.SaveBlock(ctx, block1)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block1))) block2 := testutil.NewBeaconBlock() block2.Block.Slot = 10 - require.NoError(t, db.SaveBlock(ctx, block2)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block2))) block3 := testutil.NewBeaconBlock() block3.Block.Slot = 100 - require.NoError(t, db.SaveBlock(ctx, block3)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block3))) highestAt, err := db.HighestSlotBlocksBelow(ctx, 2) require.NoError(t, err) assert.Equal(t, false, len(highestAt) <= 0, "Got empty highest at slice") - assert.Equal(t, true, proto.Equal(block1, highestAt[0]), "Wanted: %v, received: %v", block1, highestAt[0]) + assert.Equal(t, true, proto.Equal(block1, highestAt[0].Proto()), "Wanted: %v, received: %v", block1, highestAt[0]) highestAt, err = db.HighestSlotBlocksBelow(ctx, 11) require.NoError(t, err) assert.Equal(t, false, len(highestAt) <= 0, "Got empty highest at slice") - assert.Equal(t, true, proto.Equal(block2, highestAt[0]), "Wanted: %v, received: %v", block2, highestAt[0]) + assert.Equal(t, true, proto.Equal(block2, highestAt[0].Proto()), "Wanted: %v, received: %v", block2, highestAt[0]) highestAt, err = db.HighestSlotBlocksBelow(ctx, 101) require.NoError(t, err) assert.Equal(t, false, len(highestAt) <= 0, "Got empty highest at slice") - assert.Equal(t, true, proto.Equal(block3, highestAt[0]), "Wanted: %v, received: %v", block3, highestAt[0]) + assert.Equal(t, true, proto.Equal(block3, highestAt[0].Proto()), "Wanted: %v, received: %v", block3, highestAt[0]) r3, err := block3.Block.HashTreeRoot() require.NoError(t, err) @@ -378,7 +385,7 @@ func TestStore_SaveBlock_CanGetHighestAt(t *testing.T) { highestAt, err = db.HighestSlotBlocksBelow(ctx, 101) require.NoError(t, err) - assert.Equal(t, true, proto.Equal(block2, highestAt[0]), "Wanted: %v, received: %v", block2, highestAt[0]) + assert.Equal(t, true, proto.Equal(block2, highestAt[0].Proto()), "Wanted: %v, received: %v", block2, highestAt[0]) } func TestStore_GenesisBlock_CanGetHighestAt(t *testing.T) { @@ -389,32 +396,32 @@ func TestStore_GenesisBlock_CanGetHighestAt(t *testing.T) { genesisRoot, err := genesisBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesisRoot)) - require.NoError(t, db.SaveBlock(ctx, genesisBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesisBlock))) block1 := testutil.NewBeaconBlock() block1.Block.Slot = 1 - require.NoError(t, db.SaveBlock(ctx, block1)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block1))) highestAt, err := db.HighestSlotBlocksBelow(ctx, 2) require.NoError(t, err) - assert.Equal(t, true, proto.Equal(block1, highestAt[0]), "Wanted: %v, received: %v", block1, highestAt[0]) + assert.Equal(t, true, proto.Equal(block1, highestAt[0].Proto()), "Wanted: %v, received: %v", block1, highestAt[0]) highestAt, err = db.HighestSlotBlocksBelow(ctx, 1) require.NoError(t, err) - assert.Equal(t, true, proto.Equal(genesisBlock, highestAt[0]), "Wanted: %v, received: %v", genesisBlock, highestAt[0]) + assert.Equal(t, true, proto.Equal(genesisBlock, highestAt[0].Proto()), "Wanted: %v, received: %v", genesisBlock, highestAt[0]) highestAt, err = db.HighestSlotBlocksBelow(ctx, 0) require.NoError(t, err) - assert.Equal(t, true, proto.Equal(genesisBlock, highestAt[0]), "Wanted: %v, received: %v", genesisBlock, highestAt[0]) + assert.Equal(t, true, proto.Equal(genesisBlock, highestAt[0].Proto()), "Wanted: %v, received: %v", genesisBlock, highestAt[0]) } func TestStore_SaveBlocks_HasCachedBlocks(t *testing.T) { db := setupDB(t) ctx := context.Background() - b := make([]*ethpb.SignedBeaconBlock, 500) + b := make([]interfaces.SignedBeaconBlock, 500) for i := 0; i < 500; i++ { blk := testutil.NewBeaconBlock() blk.Block.ParentRoot = bytesutil.PadTo([]byte("parent"), 32) blk.Block.Slot = types.Slot(i) - b[i] = blk + b[i] = interfaces.NewWrappedSignedBeaconBlock(blk) } require.NoError(t, db.SaveBlock(ctx, b[0])) @@ -430,12 +437,12 @@ func TestStore_SaveBlocks_HasRootsMatched(t *testing.T) { db := setupDB(t) ctx := context.Background() - b := make([]*ethpb.SignedBeaconBlock, 500) + b := make([]interfaces.SignedBeaconBlock, 500) for i := 0; i < 500; i++ { blk := testutil.NewBeaconBlock() blk.Block.ParentRoot = bytesutil.PadTo([]byte("parent"), 32) blk.Block.Slot = types.Slot(i) - b[i] = blk + b[i] = interfaces.NewWrappedSignedBeaconBlock(blk) } require.NoError(t, db.SaveBlocks(ctx, b)) @@ -446,7 +453,7 @@ func TestStore_SaveBlocks_HasRootsMatched(t *testing.T) { assert.Equal(t, 500, len(blks), "Did not get wanted blocks") for i, blk := range blks { - rt, err := blk.Block.HashTreeRoot() + rt, err := blk.Block().HashTreeRoot() require.NoError(t, err) assert.Equal(t, roots[i], rt, "mismatch of block roots") } @@ -458,15 +465,15 @@ func TestStore_BlocksBySlot_BlockRootsBySlot(t *testing.T) { b1 := testutil.NewBeaconBlock() b1.Block.Slot = 20 - require.NoError(t, db.SaveBlock(ctx, b1)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b1))) b2 := testutil.NewBeaconBlock() b2.Block.Slot = 100 b2.Block.ParentRoot = bytesutil.PadTo([]byte("parent1"), 32) - require.NoError(t, db.SaveBlock(ctx, b2)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b2))) b3 := testutil.NewBeaconBlock() b3.Block.Slot = 100 b3.Block.ParentRoot = bytesutil.PadTo([]byte("parent2"), 32) - require.NoError(t, db.SaveBlock(ctx, b3)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b3))) r1, err := b1.Block.HashTreeRoot() require.NoError(t, err) @@ -481,12 +488,12 @@ func TestStore_BlocksBySlot_BlockRootsBySlot(t *testing.T) { assert.Equal(t, false, hasBlocks, "Expected no blocks") hasBlocks, retrievedBlocks, err = db.BlocksBySlot(ctx, 20) require.NoError(t, err) - assert.Equal(t, true, proto.Equal(b1, retrievedBlocks[0]), "Wanted: %v, received: %v", b1, retrievedBlocks[0]) + assert.Equal(t, true, proto.Equal(b1, retrievedBlocks[0].Proto()), "Wanted: %v, received: %v", b1, retrievedBlocks[0]) assert.Equal(t, true, hasBlocks, "Expected to have blocks") hasBlocks, retrievedBlocks, err = db.BlocksBySlot(ctx, 100) require.NoError(t, err) - assert.Equal(t, true, proto.Equal(b2, retrievedBlocks[0]), "Wanted: %v, received: %v", b2, retrievedBlocks[0]) - assert.Equal(t, true, proto.Equal(b3, retrievedBlocks[1]), "Wanted: %v, received: %v", b3, retrievedBlocks[1]) + assert.Equal(t, true, proto.Equal(b2, retrievedBlocks[0].Proto()), "Wanted: %v, received: %v", b2, retrievedBlocks[0]) + assert.Equal(t, true, proto.Equal(b3, retrievedBlocks[1].Proto()), "Wanted: %v, received: %v", b3, retrievedBlocks[1]) assert.Equal(t, true, hasBlocks, "Expected to have blocks") hasBlockRoots, retrievedBlockRoots, err := db.BlockRootsBySlot(ctx, 1) diff --git a/beacon-chain/rpc/beacon/committees_test.go b/beacon-chain/rpc/beacon/committees_test.go index bc33abf18610..920bb1e18593 100644 --- a/beacon-chain/rpc/beacon/committees_test.go +++ b/beacon-chain/rpc/beacon/committees_test.go @@ -13,6 +13,7 @@ import ( iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -40,7 +41,7 @@ func TestServer_ListBeaconCommittees_CurrentEpoch(t *testing.T) { StateGen: stategen.New(db), } b := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot)) @@ -85,7 +86,7 @@ func TestServer_ListBeaconCommittees_PreviousEpoch(t *testing.T) { require.NoError(t, headState.SetSlot(params.BeaconConfig().SlotsPerEpoch)) b := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveState(ctx, headState, gRoot)) @@ -156,7 +157,7 @@ func TestRetrieveCommitteesForRoot(t *testing.T) { StateGen: stategen.New(db), } b := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot)) diff --git a/beacon-chain/rpc/beacon/validators_test.go b/beacon-chain/rpc/beacon/validators_test.go index 998e4afce698..96862fbc0b5a 100644 --- a/beacon-chain/rpc/beacon/validators_test.go +++ b/beacon-chain/rpc/beacon/validators_test.go @@ -24,6 +24,7 @@ import ( pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/cmd" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -105,7 +106,7 @@ func TestServer_ListValidatorBalances_NoResults(t *testing.T) { headState, err := testutil.NewBeaconState() require.NoError(t, err) b := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, gRoot)) @@ -157,7 +158,7 @@ func TestServer_ListValidatorBalances_DefaultResponse_NoArchive(t *testing.T) { require.NoError(t, st.SetValidators(validators)) require.NoError(t, st.SetBalances(balances)) b := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, gRoot)) @@ -522,7 +523,7 @@ func TestServer_ListValidators_OnlyActiveValidators(t *testing.T) { } b := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, gRoot)) @@ -590,7 +591,7 @@ func TestServer_ListValidators_InactiveInTheMiddle(t *testing.T) { } b := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, gRoot)) @@ -996,7 +997,7 @@ func TestServer_ListValidators_FromOldEpoch(t *testing.T) { require.NoError(t, st.SetSlot(20*params.BeaconConfig().SlotsPerEpoch)) require.NoError(t, st.SetValidators(validators)) b := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveState(ctx, st, gRoot)) @@ -1065,7 +1066,7 @@ func TestServer_ListValidators_ProcessHeadStateSlots(t *testing.T) { require.NoError(t, st.SetValidators(validators)) require.NoError(t, st.SetBalances(balances)) b := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveState(ctx, st, gRoot)) @@ -1219,7 +1220,7 @@ func TestServer_GetValidatorActiveSetChanges(t *testing.T) { require.NoError(t, err) } b := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) @@ -1521,7 +1522,7 @@ func TestServer_GetValidatorParticipation_CurrentAndPrevEpoch(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = 16 - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) bRoot, err := b.Block.HashTreeRoot() require.NoError(t, beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: bRoot[:]})) require.NoError(t, beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: params.BeaconConfig().ZeroHash[:]})) @@ -1599,7 +1600,7 @@ func TestServer_GetValidatorParticipation_OrphanedUntilGenesis(t *testing.T) { require.NoError(t, headState.AppendPreviousEpochAttestations(atts[0])) b := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) bRoot, err := b.Block.HashTreeRoot() require.NoError(t, beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: bRoot[:]})) require.NoError(t, beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: params.BeaconConfig().ZeroHash[:]})) @@ -1947,7 +1948,7 @@ func TestServer_GetIndividualVotes_ValidatorsDontExist(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = params.BeaconConfig().SlotsPerEpoch - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) gen := stategen.New(beaconDB) @@ -2043,7 +2044,7 @@ func TestServer_GetIndividualVotes_Working(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = params.BeaconConfig().SlotsPerEpoch - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) gen := stategen.New(beaconDB) @@ -2176,7 +2177,7 @@ func TestServer_isSlotCanonical(t *testing.T) { for i := 1; i < 100; i++ { b := testutil.NewBeaconBlock() b.Block.Slot = types.Slot(i) - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) br, err := b.Block.HashTreeRoot() require.NoError(t, err) if i%2 == 0 { @@ -2212,7 +2213,7 @@ func TestServer_isSlotCanonical_MultipleBlocks(t *testing.T) { for i := 1; i < 100; i++ { b := testutil.NewBeaconBlock() b.Block.Slot = types.Slot(i) - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) br, err := b.Block.HashTreeRoot() require.NoError(t, err) if i%2 == 0 { @@ -2221,7 +2222,7 @@ func TestServer_isSlotCanonical_MultipleBlocks(t *testing.T) { b = testutil.NewBeaconBlock() b.Block.Slot = types.Slot(i) b.Block.ProposerIndex = 100 - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) } roots = append(roots, br) } diff --git a/beacon-chain/rpc/beaconv1/state_test.go b/beacon-chain/rpc/beaconv1/state_test.go index 5cfbdc28aab8..29c7ba34b348 100644 --- a/beacon-chain/rpc/beaconv1/state_test.go +++ b/beacon-chain/rpc/beaconv1/state_test.go @@ -18,6 +18,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -86,7 +87,7 @@ func TestGetStateRoot(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.StateRoot = bytesutil.PadTo([]byte("head"), 32) s := Server{ - ChainInfoFetcher: &chainMock.ChainService{Block: b}, + ChainInfoFetcher: &chainMock.ChainService{Block: interfaces.NewWrappedSignedBeaconBlock(b)}, } resp, err := s.GetStateRoot(ctx, ðpb.StateRequest{ @@ -99,7 +100,7 @@ func TestGetStateRoot(t *testing.T) { t.Run("Genesis", func(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.StateRoot = bytesutil.PadTo([]byte("genesis"), 32) - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]})) @@ -119,12 +120,12 @@ func TestGetStateRoot(t *testing.T) { parent := testutil.NewBeaconBlock() parentR, err := parent.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, parent)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(parent))) require.NoError(t, db.SaveGenesisBlockRoot(ctx, parentR)) b := testutil.NewBeaconBlock() b.Block.ParentRoot = parentR[:] b.Block.StateRoot = bytesutil.PadTo([]byte("finalized"), 32) - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]})) @@ -144,12 +145,12 @@ func TestGetStateRoot(t *testing.T) { parent := testutil.NewBeaconBlock() parentR, err := parent.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, parent)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(parent))) require.NoError(t, db.SaveGenesisBlockRoot(ctx, parentR)) b := testutil.NewBeaconBlock() b.Block.ParentRoot = parentR[:] b.Block.StateRoot = bytesutil.PadTo([]byte("justified"), 32) - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]})) @@ -204,7 +205,7 @@ func TestGetStateRoot(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = 100 b.Block.StateRoot = bytesutil.PadTo([]byte("slot"), 32) - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) s := Server{ BeaconDB: db, GenesisTimeFetcher: &chainMock.ChainService{}, @@ -221,11 +222,11 @@ func TestGetStateRoot(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = 100 b.Block.StateRoot = bytesutil.PadTo([]byte("slot"), 32) - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) b = testutil.NewBeaconBlock() b.Block.Slot = 100 b.Block.StateRoot = bytesutil.PadTo([]byte("sLot"), 32) - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) s := Server{ BeaconDB: db, GenesisTimeFetcher: &chainMock.ChainService{}, @@ -299,7 +300,7 @@ func TestGetStateFork(t *testing.T) { db := testDB.SetupDB(t) b := testutil.NewBeaconBlock() b.Block.StateRoot = bytesutil.PadTo([]byte("genesis"), 32) - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]})) @@ -508,7 +509,7 @@ func TestGetFinalityCheckpoints(t *testing.T) { db := testDB.SetupDB(t) b := testutil.NewBeaconBlock() b.Block.StateRoot = bytesutil.PadTo([]byte("genesis"), 32) - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]})) diff --git a/beacon-chain/rpc/debug/BUILD.bazel b/beacon-chain/rpc/debug/BUILD.bazel index fc9b27086760..28c5abf1825b 100644 --- a/beacon-chain/rpc/debug/BUILD.bazel +++ b/beacon-chain/rpc/debug/BUILD.bazel @@ -54,6 +54,7 @@ go_test( "//beacon-chain/p2p/testing:go_default_library", "//beacon-chain/state/stategen:go_default_library", "//proto/beacon/rpc/v1:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", diff --git a/beacon-chain/rpc/debug/block_test.go b/beacon-chain/rpc/debug/block_test.go index cc9ea157a2d5..48b1144b6dfb 100644 --- a/beacon-chain/rpc/debug/block_test.go +++ b/beacon-chain/rpc/debug/block_test.go @@ -12,6 +12,7 @@ import ( dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -24,7 +25,7 @@ func TestServer_GetBlock(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = 100 - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) blockRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) bs := &Server{ @@ -76,7 +77,7 @@ func TestServer_GetAttestationInclusionSlot(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = 2 b.Block.Body.Attestations = []*ethpb.Attestation{a} - require.NoError(t, bs.BeaconDB.SaveBlock(ctx, b)) + require.NoError(t, bs.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) res, err := bs.GetInclusionSlot(ctx, &pbrpc.InclusionSlotRequest{Slot: 1, Id: uint64(c[0])}) require.NoError(t, err) require.Equal(t, b.Block.Slot, res.Slot) diff --git a/beacon-chain/rpc/debug/state_test.go b/beacon-chain/rpc/debug/state_test.go index ad40693daf45..67fcef8015f3 100644 --- a/beacon-chain/rpc/debug/state_test.go +++ b/beacon-chain/rpc/debug/state_test.go @@ -9,6 +9,7 @@ import ( dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -23,7 +24,7 @@ func TestServer_GetBeaconState(t *testing.T) { require.NoError(t, st.SetSlot(slot)) b := testutil.NewBeaconBlock() b.Block.Slot = slot - require.NoError(t, db.SaveBlock(ctx, b)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) gRoot, err := b.Block.HashTreeRoot() require.NoError(t, err) gen := stategen.New(db) diff --git a/beacon-chain/rpc/validator/BUILD.bazel b/beacon-chain/rpc/validator/BUILD.bazel index 6e0040e0b37f..6ab252a3bb61 100644 --- a/beacon-chain/rpc/validator/BUILD.bazel +++ b/beacon-chain/rpc/validator/BUILD.bazel @@ -111,6 +111,7 @@ go_test( "//shared/bytesutil:go_default_library", "//shared/event:go_default_library", "//shared/featureconfig:go_default_library", + "//shared/interfaces:go_default_library", "//shared/mock:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", From db29b9561bd05423728f056c567ee84567f19782 Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 25 May 2021 17:20:19 +0800 Subject: [PATCH 17/44] fix tests --- beacon-chain/db/kv/checkpoint_test.go | 3 ++- .../db/kv/finalized_block_roots_test.go | 23 +++++++++------- beacon-chain/db/kv/genesis_test.go | 4 +-- beacon-chain/db/kv/state_test.go | 25 ++++++++--------- .../sync/rpc_beacon_blocks_by_range_test.go | 21 ++++++++------- .../sync/rpc_beacon_blocks_by_root_test.go | 3 ++- beacon-chain/sync/rpc_send_request_test.go | 27 ++++++++++--------- beacon-chain/sync/rpc_status_test.go | 25 +++++++++-------- 8 files changed, 71 insertions(+), 60 deletions(-) diff --git a/beacon-chain/db/kv/checkpoint_test.go b/beacon-chain/db/kv/checkpoint_test.go index 6e87353e511c..751cfbf18ec5 100644 --- a/beacon-chain/db/kv/checkpoint_test.go +++ b/beacon-chain/db/kv/checkpoint_test.go @@ -6,6 +6,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -52,7 +53,7 @@ func TestStore_FinalizedCheckpoint_CanSaveRetrieve(t *testing.T) { } // a valid chain is required to save finalized checkpoint. - require.NoError(t, db.SaveBlock(ctx, blk)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) st, err := testutil.NewBeaconState() require.NoError(t, err) require.NoError(t, st.SetSlot(1)) diff --git a/beacon-chain/db/kv/finalized_block_roots_test.go b/beacon-chain/db/kv/finalized_block_roots_test.go index b6a2f04ba557..ed1921568d24 100644 --- a/beacon-chain/db/kv/finalized_block_roots_test.go +++ b/beacon-chain/db/kv/finalized_block_roots_test.go @@ -7,6 +7,7 @@ import ( types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -25,7 +26,7 @@ func TestStore_IsFinalizedBlock(t *testing.T) { blks := makeBlocks(t, 0, slotsPerEpoch*3, genesisBlockRoot) require.NoError(t, db.SaveBlocks(ctx, blks)) - root, err := blks[slotsPerEpoch].Block.HashTreeRoot() + root, err := blks[slotsPerEpoch].Block().HashTreeRoot() require.NoError(t, err) cp := ðpb.Checkpoint{ @@ -41,12 +42,12 @@ func TestStore_IsFinalizedBlock(t *testing.T) { // All blocks up to slotsPerEpoch*2 should be in the finalized index. for i := uint64(0); i < slotsPerEpoch*2; i++ { - root, err := blks[i].Block.HashTreeRoot() + root, err := blks[i].Block().HashTreeRoot() require.NoError(t, err) assert.Equal(t, true, db.IsFinalizedBlock(ctx, root), "Block at index %d was not considered finalized in the index", i) } for i := slotsPerEpoch * 3; i < uint64(len(blks)); i++ { - root, err := blks[i].Block.HashTreeRoot() + root, err := blks[i].Block().HashTreeRoot() require.NoError(t, err) assert.Equal(t, false, db.IsFinalizedBlock(ctx, root), "Block at index %d was considered finalized in the index, but should not have", i) } @@ -60,7 +61,7 @@ func TestStore_IsFinalizedBlockGenesis(t *testing.T) { blk.Block.Slot = 0 root, err := blk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(ctx, blk)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, db.SaveGenesisBlockRoot(ctx, root)) assert.Equal(t, true, db.IsFinalizedBlock(ctx, root), "Finalized genesis block doesn't exist in db") } @@ -141,7 +142,7 @@ func TestStore_IsFinalizedChildBlock(t *testing.T) { blks := makeBlocks(t, 0, slotsPerEpoch*3, genesisBlockRoot) require.NoError(t, db.SaveBlocks(ctx, blks)) - root, err := blks[slotsPerEpoch].Block.HashTreeRoot() + root, err := blks[slotsPerEpoch].Block().HashTreeRoot() require.NoError(t, err) cp := ðpb.Checkpoint{ @@ -157,7 +158,7 @@ func TestStore_IsFinalizedChildBlock(t *testing.T) { // All blocks up to slotsPerEpoch should have a finalized child block. for i := uint64(0); i < slotsPerEpoch; i++ { - root, err := blks[i].Block.HashTreeRoot() + root, err := blks[i].Block().HashTreeRoot() require.NoError(t, err) assert.Equal(t, true, db.IsFinalizedBlock(ctx, root), "Block at index %d was not considered finalized in the index", i) blk, err := db.FinalizedChildBlock(ctx, root) @@ -168,14 +169,15 @@ func TestStore_IsFinalizedChildBlock(t *testing.T) { } } -func sszRootOrDie(t *testing.T, block *ethpb.SignedBeaconBlock) []byte { - root, err := block.Block.HashTreeRoot() +func sszRootOrDie(t *testing.T, block interfaces.SignedBeaconBlock) []byte { + root, err := block.Block().HashTreeRoot() require.NoError(t, err) return root[:] } -func makeBlocks(t *testing.T, i, n uint64, previousRoot [32]byte) []*ethpb.SignedBeaconBlock { +func makeBlocks(t *testing.T, i, n uint64, previousRoot [32]byte) []interfaces.SignedBeaconBlock { blocks := make([]*ethpb.SignedBeaconBlock, n) + ifaceBlocks := make([]interfaces.SignedBeaconBlock, n) for j := i; j < n+i; j++ { parentRoot := make([]byte, 32) copy(parentRoot, previousRoot[:]) @@ -185,6 +187,7 @@ func makeBlocks(t *testing.T, i, n uint64, previousRoot [32]byte) []*ethpb.Signe var err error previousRoot, err = blocks[j-i].Block.HashTreeRoot() require.NoError(t, err) + ifaceBlocks[j-i] = interfaces.NewWrappedSignedBeaconBlock(blocks[j-i]) } - return blocks + return ifaceBlocks } diff --git a/beacon-chain/db/kv/genesis_test.go b/beacon-chain/db/kv/genesis_test.go index d36dfa9536f4..ba115c98ff36 100644 --- a/beacon-chain/db/kv/genesis_test.go +++ b/beacon-chain/db/kv/genesis_test.go @@ -31,7 +31,7 @@ func testGenesisDataSaved(t *testing.T, db iface.Database) { assert.NoError(t, err) assert.NotNil(t, gb) - gbHTR, err := gb.Block.HashTreeRoot() + gbHTR, err := gb.Block().HashTreeRoot() assert.NoError(t, err) gss, err := db.StateSummary(ctx, gbHTR) @@ -42,7 +42,7 @@ func testGenesisDataSaved(t *testing.T, db iface.Database) { assert.NoError(t, err) assert.NotNil(t, head) - headHTR, err := head.Block.HashTreeRoot() + headHTR, err := head.Block().HashTreeRoot() assert.NoError(t, err) assert.Equal(t, gbHTR, headHTR, "head block does not match genesis block") } diff --git a/beacon-chain/db/kv/state_test.go b/beacon-chain/db/kv/state_test.go index f43ea776b854..894fbd91a5ea 100644 --- a/beacon-chain/db/kv/state_test.go +++ b/beacon-chain/db/kv/state_test.go @@ -9,6 +9,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -64,14 +65,14 @@ func TestStore_StatesBatchDelete(t *testing.T) { db := setupDB(t) ctx := context.Background() numBlocks := 100 - totalBlocks := make([]*ethpb.SignedBeaconBlock, numBlocks) + totalBlocks := make([]interfaces.SignedBeaconBlock, numBlocks) blockRoots := make([][32]byte, 0) evenBlockRoots := make([][32]byte, 0) for i := 0; i < len(totalBlocks); i++ { b := testutil.NewBeaconBlock() b.Block.Slot = types.Slot(i) - totalBlocks[i] = b - r, err := totalBlocks[i].Block.HashTreeRoot() + totalBlocks[i] = interfaces.NewWrappedSignedBeaconBlock(b) + r, err := totalBlocks[i].Block().HashTreeRoot() require.NoError(t, err) st, err := testutil.NewBeaconState() require.NoError(t, err) @@ -121,7 +122,7 @@ func TestStore_DeleteFinalizedState(t *testing.T) { blk.Block.ParentRoot = genesis[:] blk.Block.Slot = 100 - require.NoError(t, db.SaveBlock(ctx, blk)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) finalizedBlockRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) @@ -146,7 +147,7 @@ func TestStore_DeleteHeadState(t *testing.T) { blk := testutil.NewBeaconBlock() blk.Block.ParentRoot = genesis[:] blk.Block.Slot = 100 - require.NoError(t, db.SaveBlock(ctx, blk)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) headBlockRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) @@ -166,7 +167,7 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { b.Block.Slot = 1 r, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) st, err := testutil.NewBeaconState() require.NoError(t, err) require.NoError(t, st.SetSlot(1)) @@ -176,7 +177,7 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { b.Block.Slot = 100 r1, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) st, err = testutil.NewBeaconState() require.NoError(t, err) require.NoError(t, st.SetSlot(100)) @@ -186,7 +187,7 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { b.Block.Slot = 1000 r2, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) st, err = testutil.NewBeaconState() require.NoError(t, err) require.NoError(t, st.SetSlot(1000)) @@ -220,7 +221,7 @@ func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) { b.Block.Slot = 1 r, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) st, err := testutil.NewBeaconState() require.NoError(t, err) @@ -257,7 +258,7 @@ func TestStore_CleanUpDirtyStates_AboveThreshold(t *testing.T) { b.Block.ParentRoot = prevRoot[:] r, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) bRoots = append(bRoots, r) prevRoot = r @@ -296,7 +297,7 @@ func TestStore_CleanUpDirtyStates_Finalized(t *testing.T) { b.Block.Slot = i r, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) st, err := testutil.NewBeaconState() require.NoError(t, err) @@ -324,7 +325,7 @@ func TestStore_CleanUpDirtyStates_DontDeleteNonFinalized(t *testing.T) { b.Block.Slot = i r, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) unfinalizedRoots = append(unfinalizedRoots, r) st, err := testutil.NewBeaconState() 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 ac05b1eb6ea0..6d77fdbaa996 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_range_test.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_range_test.go @@ -23,6 +23,7 @@ import ( "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -47,7 +48,7 @@ func TestRPCBeaconBlocksByRange_RPCHandlerReturnsBlocks(t *testing.T) { for i := req.StartSlot; i < req.StartSlot.Add(req.Step*req.Count); i += types.Slot(req.Step) { blk := testutil.NewBeaconBlock() blk.Block.Slot = i - require.NoError(t, d.SaveBlock(context.Background(), blk)) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk))) } // Start service with 160 as allowed blocks capacity (and almost zero capacity recovery). @@ -108,7 +109,7 @@ func TestRPCBeaconBlocksByRange_ReturnCorrectNumberBack(t *testing.T) { require.NoError(t, err) genRoot = rt } - require.NoError(t, d.SaveBlock(context.Background(), blk)) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk))) } require.NoError(t, d.SaveGenesisBlockRoot(context.Background(), genRoot)) @@ -172,7 +173,7 @@ func TestRPCBeaconBlocksByRange_RPCHandlerReturnsSortedBlocks(t *testing.T) { rt, err := blk.Block.HashTreeRoot() require.NoError(t, err) expectedRoots[j] = rt - require.NoError(t, d.SaveBlock(context.Background(), blk)) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk))) j-- } @@ -238,7 +239,7 @@ func TestRPCBeaconBlocksByRange_ReturnsGenesisBlock(t *testing.T) { if i == 0 { require.NoError(t, d.SaveGenesisBlockRoot(context.Background(), rt)) } - require.NoError(t, d.SaveBlock(context.Background(), blk)) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk))) prevRoot = rt } @@ -283,7 +284,7 @@ func TestRPCBeaconBlocksByRange_RPCHandlerRateLimitOverflow(t *testing.T) { if req.Step == 1 { block.Block.ParentRoot = parentRoot[:] } - require.NoError(t, d.SaveBlock(context.Background(), block)) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(block))) rt, err := block.Block.HashTreeRoot() require.NoError(t, err) parentRoot = rt @@ -542,7 +543,7 @@ func TestRPCBeaconBlocksByRange_EnforceResponseInvariants(t *testing.T) { block := testutil.NewBeaconBlock() block.Block.Slot = i block.Block.ParentRoot = parentRoot[:] - require.NoError(t, d.SaveBlock(context.Background(), block)) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(block))) rt, err := block.Block.HashTreeRoot() require.NoError(t, err) parentRoot = rt @@ -617,7 +618,7 @@ func TestRPCBeaconBlocksByRange_FilterBlocks(t *testing.T) { previousRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, d.SaveBlock(context.Background(), blk)) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, d.SaveGenesisBlockRoot(context.Background(), previousRoot)) blocks := make([]*ethpb.SignedBeaconBlock, req.Count) // Populate the database with blocks that would match the request. @@ -630,7 +631,7 @@ func TestRPCBeaconBlocksByRange_FilterBlocks(t *testing.T) { var err error previousRoot, err = blocks[j].Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, d.SaveBlock(context.Background(), blocks[j])) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blocks[j]))) j++ } stateSummaries := make([]*pb.StateSummary, len(blocks)) @@ -663,7 +664,7 @@ func TestRPCBeaconBlocksByRange_FilterBlocks(t *testing.T) { require.NoError(t, err) genRoot := previousRoot - require.NoError(t, d.SaveBlock(context.Background(), blk)) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, d.SaveGenesisBlockRoot(context.Background(), previousRoot)) blocks := make([]*ethpb.SignedBeaconBlock, req.Count) // Populate the database with blocks with non linear roots. @@ -680,7 +681,7 @@ func TestRPCBeaconBlocksByRange_FilterBlocks(t *testing.T) { var err error previousRoot, err = blocks[j].Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, d.SaveBlock(context.Background(), blocks[j])) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blocks[j]))) j++ } stateSummaries := make([]*pb.StateSummary, len(blocks)) 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 057dc23dcfe0..52bb6df43a25 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_root_test.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_root_test.go @@ -18,6 +18,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/p2p" p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" p2pTypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -38,7 +39,7 @@ func TestRecentBeaconBlocksRPCHandler_ReturnsBlocks(t *testing.T) { blk.Block.Slot = i root, err := blk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, d.SaveBlock(context.Background(), blk)) + require.NoError(t, d.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk))) blkRoots = append(blkRoots, root) } diff --git a/beacon-chain/sync/rpc_send_request_test.go b/beacon-chain/sync/rpc_send_request_test.go index 9cc0d4bf05e2..91155dc1a40d 100644 --- a/beacon-chain/sync/rpc_send_request_test.go +++ b/beacon-chain/sync/rpc_send_request_test.go @@ -15,6 +15,7 @@ import ( p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" p2pTypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -62,7 +63,7 @@ func TestSendRequest_SendBeaconBlocksByRangeRequest(t *testing.T) { for i := req.StartSlot; i < req.StartSlot.Add(req.Count*req.Step); i += types.Slot(req.Step) { if processor != nil { - if processorErr := processor(knownBlocks[i]); processorErr != nil { + if processorErr := processor(interfaces.NewWrappedSignedBeaconBlock(knownBlocks[i])); processorErr != nil { if errors.Is(processorErr, io.EOF) { // Close stream, w/o any errors written. return @@ -114,8 +115,8 @@ func TestSendRequest_SendBeaconBlocksByRangeRequest(t *testing.T) { Count: 128, Step: 1, } - blocksFromProcessor := make([]*eth.SignedBeaconBlock, 0) - blocks, err := SendBeaconBlocksByRangeRequest(ctx, nil, p1, p2.PeerID(), req, func(block *eth.SignedBeaconBlock) error { + blocksFromProcessor := make([]interfaces.SignedBeaconBlock, 0) + blocks, err := SendBeaconBlocksByRangeRequest(ctx, nil, p1, p2.PeerID(), req, func(block interfaces.SignedBeaconBlock) error { blocksFromProcessor = append(blocksFromProcessor, block) return nil }) @@ -137,7 +138,7 @@ func TestSendRequest_SendBeaconBlocksByRangeRequest(t *testing.T) { Step: 1, } errFromProcessor := errors.New("processor error") - _, err := SendBeaconBlocksByRangeRequest(ctx, nil, p1, p2.PeerID(), req, func(block *eth.SignedBeaconBlock) error { + _, err := SendBeaconBlocksByRangeRequest(ctx, nil, p1, p2.PeerID(), req, func(block interfaces.SignedBeaconBlock) error { return errFromProcessor }) assert.ErrorContains(t, errFromProcessor.Error(), err) @@ -166,7 +167,7 @@ func TestSendRequest_SendBeaconBlocksByRangeRequest(t *testing.T) { cfg.MaxRequestBlocks = maxRequestBlocks params.OverrideBeaconNetworkConfig(cfg) }() - blocks, err = SendBeaconBlocksByRangeRequest(ctx, nil, p1, p2.PeerID(), req, func(block *eth.SignedBeaconBlock) error { + blocks, err = SendBeaconBlocksByRangeRequest(ctx, nil, p1, p2.PeerID(), req, func(block interfaces.SignedBeaconBlock) error { // Since ssz checks the boundaries, and doesn't normally allow to send requests bigger than // the max request size, we are updating max request size dynamically. Even when updated dynamically, // no more than max request size of blocks is expected on return. @@ -184,7 +185,7 @@ func TestSendRequest_SendBeaconBlocksByRangeRequest(t *testing.T) { p1.Connect(p2) blocksProcessed := 0 expectedErr := errors.New("some error") - p2.SetStreamHandler(pcl, knownBlocksProvider(p2, func(block *eth.SignedBeaconBlock) error { + p2.SetStreamHandler(pcl, knownBlocksProvider(p2, func(block interfaces.SignedBeaconBlock) error { if blocksProcessed > 2 { return expectedErr } @@ -325,7 +326,7 @@ func TestSendRequest_SendBeaconBlocksByRootRequest(t *testing.T) { for _, root := range *req { if blk, ok := knownBlocks[root]; ok { if processor != nil { - if processorErr := processor(blk); processorErr != nil { + if processorErr := processor(interfaces.NewWrappedSignedBeaconBlock(blk)); processorErr != nil { if errors.Is(processorErr, io.EOF) { // Close stream, w/o any errors written. return @@ -367,8 +368,8 @@ func TestSendRequest_SendBeaconBlocksByRootRequest(t *testing.T) { // No error from block processor. req := &p2pTypes.BeaconBlockByRootsReq{knownRoots[0], knownRoots[1]} - blocksFromProcessor := make([]*eth.SignedBeaconBlock, 0) - blocks, err := SendBeaconBlocksByRootRequest(ctx, nil, p1, p2.PeerID(), req, func(block *eth.SignedBeaconBlock) error { + blocksFromProcessor := make([]interfaces.SignedBeaconBlock, 0) + blocks, err := SendBeaconBlocksByRootRequest(ctx, nil, p1, p2.PeerID(), req, func(block interfaces.SignedBeaconBlock) error { blocksFromProcessor = append(blocksFromProcessor, block) return nil }) @@ -386,7 +387,7 @@ func TestSendRequest_SendBeaconBlocksByRootRequest(t *testing.T) { // Send error from block processor. req := &p2pTypes.BeaconBlockByRootsReq{knownRoots[0], knownRoots[1]} errFromProcessor := errors.New("processor error") - _, err := SendBeaconBlocksByRootRequest(ctx, nil, p1, p2.PeerID(), req, func(block *eth.SignedBeaconBlock) error { + _, err := SendBeaconBlocksByRootRequest(ctx, nil, p1, p2.PeerID(), req, func(block interfaces.SignedBeaconBlock) error { return errFromProcessor }) assert.ErrorContains(t, errFromProcessor.Error(), err) @@ -411,7 +412,7 @@ func TestSendRequest_SendBeaconBlocksByRootRequest(t *testing.T) { cfg.MaxRequestBlocks = maxRequestBlocks params.OverrideBeaconNetworkConfig(cfg) }() - blocks, err = SendBeaconBlocksByRootRequest(ctx, nil, p1, p2.PeerID(), req, func(block *eth.SignedBeaconBlock) error { + blocks, err = SendBeaconBlocksByRootRequest(ctx, nil, p1, p2.PeerID(), req, func(block interfaces.SignedBeaconBlock) error { // Since ssz checks the boundaries, and doesn't normally allow to send requests bigger than // the max request size, we are updating max request size dynamically. Even when updated dynamically, // no more than max request size of blocks is expected on return. @@ -429,7 +430,7 @@ func TestSendRequest_SendBeaconBlocksByRootRequest(t *testing.T) { p1.Connect(p2) blocksProcessed := 0 expectedErr := errors.New("some error") - p2.SetStreamHandler(pcl, knownBlocksProvider(p2, func(block *eth.SignedBeaconBlock) error { + p2.SetStreamHandler(pcl, knownBlocksProvider(p2, func(block interfaces.SignedBeaconBlock) error { if blocksProcessed > 2 { return expectedErr } @@ -449,7 +450,7 @@ func TestSendRequest_SendBeaconBlocksByRootRequest(t *testing.T) { p1.Connect(p2) blocksProcessed := 0 expectedErr := io.EOF - p2.SetStreamHandler(pcl, knownBlocksProvider(p2, func(block *eth.SignedBeaconBlock) error { + p2.SetStreamHandler(pcl, knownBlocksProvider(p2, func(block interfaces.SignedBeaconBlock) error { if blocksProcessed > 2 { return expectedErr } diff --git a/beacon-chain/sync/rpc_status_test.go b/beacon-chain/sync/rpc_status_test.go index 7c14256e32b5..be87d9fa5cbd 100644 --- a/beacon-chain/sync/rpc_status_test.go +++ b/beacon-chain/sync/rpc_status_test.go @@ -23,6 +23,7 @@ import ( 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/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -175,7 +176,7 @@ func TestStatusRPCHandler_ReturnsHelloMessage(t *testing.T) { require.NoError(t, err) require.NoError(t, genesisState.SetSlot(111)) require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%uint64(params.BeaconConfig().SlotsPerHistoricalRoot), headRoot)) - require.NoError(t, db.SaveBlock(context.Background(), finalized)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(finalized))) require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), finalizedRoot)) finalizedCheckpt := ðpb.Checkpoint{ Epoch: 3, @@ -265,7 +266,7 @@ func TestHandshakeHandlers_Roundtrip(t *testing.T) { require.NoError(t, err) blk := testutil.NewBeaconBlock() blk.Block.Slot = 0 - require.NoError(t, db.SaveBlock(context.Background(), blk)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk))) finalizedRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), finalizedRoot)) @@ -477,7 +478,7 @@ func TestStatusRPCRequest_FinalizedBlockExists(t *testing.T) { require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%uint64(params.BeaconConfig().SlotsPerHistoricalRoot), headRoot)) blk := testutil.NewBeaconBlock() blk.Block.Slot = blkSlot - require.NoError(t, db.SaveBlock(context.Background(), blk)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), finalizedRoot)) finalizedCheckpt := ðpb.Checkpoint{ Epoch: 3, @@ -558,24 +559,24 @@ func TestStatusRPCRequest_FinalizedBlockSkippedSlots(t *testing.T) { genRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, db.SaveBlock(context.Background(), blk)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), genRoot)) blocksTillHead := makeBlocks(t, 1, 1000, genRoot) require.NoError(t, db.SaveBlocks(context.Background(), blocksTillHead)) stateSummaries := make([]*pb.StateSummary, len(blocksTillHead)) for i, b := range blocksTillHead { - bRoot, err := b.Block.HashTreeRoot() + bRoot, err := b.Block().HashTreeRoot() require.NoError(t, err) stateSummaries[i] = &pb.StateSummary{ - Slot: b.Block.Slot, + Slot: b.Block().Slot(), Root: bRoot[:], } } require.NoError(t, db.SaveStateSummaries(context.Background(), stateSummaries)) rootFetcher := func(slot types.Slot) [32]byte { - rt, err := blocksTillHead[slot-1].Block.HashTreeRoot() + rt, err := blocksTillHead[slot-1].Block().HashTreeRoot() require.NoError(t, err) return rt } @@ -634,11 +635,11 @@ func TestStatusRPCRequest_FinalizedBlockSkippedSlots(t *testing.T) { nState := bState.Copy() // Set up a head state with data we expect. head := blocksTillHead[len(blocksTillHead)-1] - headRoot, err := head.Block.HashTreeRoot() + headRoot, err := head.Block().HashTreeRoot() require.NoError(t, err) rHead := blocksTillHead[tt.remoteHeadSlot-1] - rHeadRoot, err := rHead.Block.HashTreeRoot() + rHeadRoot, err := rHead.Block().HashTreeRoot() require.NoError(t, err) require.NoError(t, nState.SetSlot(headSlot)) @@ -925,8 +926,9 @@ func TestShouldResync(t *testing.T) { } } -func makeBlocks(t *testing.T, i, n uint64, previousRoot [32]byte) []*ethpb.SignedBeaconBlock { +func makeBlocks(t *testing.T, i, n uint64, previousRoot [32]byte) []interfaces.SignedBeaconBlock { blocks := make([]*ethpb.SignedBeaconBlock, n) + ifaceBlocks := make([]interfaces.SignedBeaconBlock, n) for j := i; j < n+i; j++ { parentRoot := make([]byte, 32) copy(parentRoot, previousRoot[:]) @@ -936,6 +938,7 @@ func makeBlocks(t *testing.T, i, n uint64, previousRoot [32]byte) []*ethpb.Signe var err error previousRoot, err = blocks[j-i].Block.HashTreeRoot() require.NoError(t, err) + ifaceBlocks[j-i] = interfaces.NewWrappedSignedBeaconBlock(blocks[j-i]) } - return blocks + return ifaceBlocks } From 5e234a19ecccc8ba8efd22c3b53059c2ba97aab5 Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 25 May 2021 17:41:54 +0800 Subject: [PATCH 18/44] fix tests --- .../sync/initial-sync/blocks_fetcher_test.go | 35 +++++------ .../initial-sync/blocks_fetcher_utils_test.go | 27 +++++---- .../sync/initial-sync/blocks_queue_test.go | 59 ++++++++++--------- .../sync/initial-sync/initial_sync_test.go | 3 +- beacon-chain/sync/utils_test.go | 19 +++--- 5 files changed, 74 insertions(+), 69 deletions(-) diff --git a/beacon-chain/sync/initial-sync/blocks_fetcher_test.go b/beacon-chain/sync/initial-sync/blocks_fetcher_test.go index 0f0fc405280b..1babbddaeb1e 100644 --- a/beacon-chain/sync/initial-sync/blocks_fetcher_test.go +++ b/beacon-chain/sync/initial-sync/blocks_fetcher_test.go @@ -21,6 +21,7 @@ import ( beaconsync "github.com/prysmaticlabs/prysm/beacon-chain/sync" "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/sliceutil" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -266,7 +267,7 @@ func TestBlocksFetcher_RoundRobin(t *testing.T) { genesisRoot := cache.rootCache[0] cache.RUnlock() - err := beaconDB.SaveBlock(context.Background(), testutil.NewBeaconBlock()) + err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock())) require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -296,9 +297,9 @@ func TestBlocksFetcher_RoundRobin(t *testing.T) { fetcher.stop() }() - processFetchedBlocks := func() ([]*eth.SignedBeaconBlock, error) { + processFetchedBlocks := func() ([]interfaces.SignedBeaconBlock, error) { defer cancel() - var unionRespBlocks []*eth.SignedBeaconBlock + var unionRespBlocks []interfaces.SignedBeaconBlock for { select { @@ -338,12 +339,12 @@ func TestBlocksFetcher_RoundRobin(t *testing.T) { assert.NoError(t, err) sort.Slice(blocks, func(i, j int) bool { - return blocks[i].Block.Slot < blocks[j].Block.Slot + return blocks[i].Block().Slot() < blocks[j].Block().Slot() }) slots := make([]types.Slot, len(blocks)) for i, block := range blocks { - slots[i] = block.Block.Slot + slots[i] = block.Block().Slot() } log.WithFields(logrus.Fields{ @@ -357,7 +358,7 @@ func TestBlocksFetcher_RoundRobin(t *testing.T) { assert.Equal(t, len(tt.expectedBlockSlots), len(blocks), "Processes wrong number of blocks") var receivedBlockSlots []types.Slot for _, blk := range blocks { - receivedBlockSlots = append(receivedBlockSlots, blk.Block.Slot) + receivedBlockSlots = append(receivedBlockSlots, blk.Block().Slot()) } missing := sliceutil.NotSlot(sliceutil.IntersectionSlot(tt.expectedBlockSlots, receivedBlockSlots), tt.expectedBlockSlots) if len(missing) > 0 { @@ -443,7 +444,7 @@ func TestBlocksFetcher_handleRequest(t *testing.T) { } }() - var blocks []*eth.SignedBeaconBlock + var blocks []interfaces.SignedBeaconBlock select { case <-ctx.Done(): t.Error(ctx.Err()) @@ -460,7 +461,7 @@ func TestBlocksFetcher_handleRequest(t *testing.T) { var receivedBlockSlots []types.Slot for _, blk := range blocks { - receivedBlockSlots = append(receivedBlockSlots, blk.Block.Slot) + receivedBlockSlots = append(receivedBlockSlots, blk.Block().Slot()) } missing := sliceutil.NotSlot(sliceutil.IntersectionSlot(chainConfig.expectedBlockSlots, receivedBlockSlots), chainConfig.expectedBlockSlots) if len(missing) > 0 { @@ -594,7 +595,7 @@ func TestBlocksFetcher_requestBlocksFromPeerReturningInvalidBlocks(t *testing.T) req *p2ppb.BeaconBlocksByRangeRequest handlerGenFn func(req *p2ppb.BeaconBlocksByRangeRequest) func(stream network.Stream) wantedErr string - validate func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []*eth.SignedBeaconBlock) + validate func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []interfaces.SignedBeaconBlock) }{ { name: "no error", @@ -613,7 +614,7 @@ func TestBlocksFetcher_requestBlocksFromPeerReturningInvalidBlocks(t *testing.T) assert.NoError(t, stream.Close()) } }, - validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []*eth.SignedBeaconBlock) { + validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []interfaces.SignedBeaconBlock) { assert.Equal(t, req.Count, uint64(len(blocks))) }, }, @@ -634,7 +635,7 @@ func TestBlocksFetcher_requestBlocksFromPeerReturningInvalidBlocks(t *testing.T) assert.NoError(t, stream.Close()) } }, - validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []*eth.SignedBeaconBlock) { + validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []interfaces.SignedBeaconBlock) { assert.Equal(t, 0, len(blocks)) }, wantedErr: beaconsync.ErrInvalidFetchedData.Error(), @@ -658,7 +659,7 @@ func TestBlocksFetcher_requestBlocksFromPeerReturningInvalidBlocks(t *testing.T) assert.NoError(t, stream.Close()) } }, - validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []*eth.SignedBeaconBlock) { + validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []interfaces.SignedBeaconBlock) { assert.Equal(t, 0, len(blocks)) }, wantedErr: beaconsync.ErrInvalidFetchedData.Error(), @@ -682,7 +683,7 @@ func TestBlocksFetcher_requestBlocksFromPeerReturningInvalidBlocks(t *testing.T) assert.NoError(t, stream.Close()) } }, - validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []*eth.SignedBeaconBlock) { + validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []interfaces.SignedBeaconBlock) { assert.Equal(t, 0, len(blocks)) }, wantedErr: beaconsync.ErrInvalidFetchedData.Error(), @@ -714,7 +715,7 @@ func TestBlocksFetcher_requestBlocksFromPeerReturningInvalidBlocks(t *testing.T) } }, wantedErr: beaconsync.ErrInvalidFetchedData.Error(), - validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []*eth.SignedBeaconBlock) { + validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []interfaces.SignedBeaconBlock) { assert.Equal(t, 0, len(blocks)) }, }, @@ -745,7 +746,7 @@ func TestBlocksFetcher_requestBlocksFromPeerReturningInvalidBlocks(t *testing.T) } }, wantedErr: beaconsync.ErrInvalidFetchedData.Error(), - validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []*eth.SignedBeaconBlock) { + validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []interfaces.SignedBeaconBlock) { assert.Equal(t, 0, len(blocks)) }, }, @@ -768,7 +769,7 @@ func TestBlocksFetcher_requestBlocksFromPeerReturningInvalidBlocks(t *testing.T) assert.NoError(t, stream.Close()) } }, - validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []*eth.SignedBeaconBlock) { + validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []interfaces.SignedBeaconBlock) { assert.Equal(t, 2, len(blocks)) }, }, @@ -791,7 +792,7 @@ func TestBlocksFetcher_requestBlocksFromPeerReturningInvalidBlocks(t *testing.T) assert.NoError(t, stream.Close()) } }, - validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []*eth.SignedBeaconBlock) { + validate: func(req *p2ppb.BeaconBlocksByRangeRequest, blocks []interfaces.SignedBeaconBlock) { assert.Equal(t, 0, len(blocks)) }, wantedErr: beaconsync.ErrInvalidFetchedData.Error(), diff --git a/beacon-chain/sync/initial-sync/blocks_fetcher_utils_test.go b/beacon-chain/sync/initial-sync/blocks_fetcher_utils_test.go index 24a111aade7a..05e02e1a4fb1 100644 --- a/beacon-chain/sync/initial-sync/blocks_fetcher_utils_test.go +++ b/beacon-chain/sync/initial-sync/blocks_fetcher_utils_test.go @@ -20,6 +20,7 @@ import ( "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -163,7 +164,7 @@ func TestBlocksFetcher_findFork(t *testing.T) { finalizedEpoch := helpers.SlotToEpoch(finalizedSlot) genesisBlock := chain1[0] - require.NoError(t, beaconDB.SaveBlock(context.Background(), genesisBlock)) + require.NoError(t, beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genesisBlock))) genesisRoot, err := genesisBlock.Block.HashTreeRoot() require.NoError(t, err) @@ -210,7 +211,7 @@ func TestBlocksFetcher_findFork(t *testing.T) { require.NoError(t, err) for _, blk := range blocks { require.NoError(t, beaconDB.SaveBlock(ctx, blk)) - require.NoError(t, st.SetSlot(blk.Block.Slot)) + require.NoError(t, st.SetSlot(blk.Block().Slot())) } pidInd++ } @@ -257,7 +258,7 @@ func TestBlocksFetcher_findFork(t *testing.T) { require.Equal(t, curForkMoreBlocksPeer, fork.peer) // Save all chain1b blocks (so that they do not interfere with alternative fork) for _, blk := range chain1b { - require.NoError(t, beaconDB.SaveBlock(ctx, blk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, st.SetSlot(blk.Block.Slot)) } forkSlot := types.Slot(129) @@ -278,14 +279,14 @@ func TestBlocksFetcher_findFork(t *testing.T) { assert.Equal(t, 65, len(fork.blocks)) ind := forkSlot for _, blk := range fork.blocks { - require.Equal(t, blk.Block.Slot, chain2[ind].Block.Slot) + require.Equal(t, blk.Block().Slot(), chain2[ind].Block.Slot) ind++ } // Process returned blocks and then attempt to extend chain (ensuring that parent block exists). for _, blk := range fork.blocks { require.NoError(t, beaconDB.SaveBlock(ctx, blk)) - require.NoError(t, st.SetSlot(blk.Block.Slot)) + require.NoError(t, st.SetSlot(blk.Block().Slot())) } assert.Equal(t, forkSlot.Add(uint64(len(fork.blocks)-1)), mc.HeadSlot()) for i := forkSlot.Add(uint64(len(fork.blocks))); i < types.Slot(len(chain2)); i++ { @@ -294,7 +295,7 @@ func TestBlocksFetcher_findFork(t *testing.T) { // Only save is parent block exists. parentRoot := bytesutil.ToBytes32(blk.Block.ParentRoot) if beaconDB.HasBlock(ctx, parentRoot) || mc.HasInitSyncBlock(parentRoot) { - require.NoError(t, beaconDB.SaveBlock(ctx, blk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, st.SetSlot(blk.Block.Slot)) } } @@ -313,7 +314,7 @@ func TestBlocksFetcher_findForkWithPeer(t *testing.T) { knownBlocks := extendBlockSequence(t, []*eth.SignedBeaconBlock{}, 128) genesisBlock := knownBlocks[0] - require.NoError(t, beaconDB.SaveBlock(context.Background(), genesisBlock)) + require.NoError(t, beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genesisBlock))) genesisRoot, err := genesisBlock.Block.HashTreeRoot() require.NoError(t, err) @@ -338,7 +339,7 @@ func TestBlocksFetcher_findForkWithPeer(t *testing.T) { fetcher.rateLimiter = leakybucket.NewCollector(6400, 6400, false) for _, blk := range knownBlocks { - require.NoError(t, beaconDB.SaveBlock(ctx, blk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, st.SetSlot(blk.Block.Slot)) } @@ -387,7 +388,7 @@ func TestBlocksFetcher_findForkWithPeer(t *testing.T) { fork, err := fetcher.findForkWithPeer(ctx, p2, 64) require.NoError(t, err) require.Equal(t, 10, len(fork.blocks)) - assert.Equal(t, forkedSlot, fork.blocks[0].Block.Slot, "Expected slot %d to be ancestor", forkedSlot) + assert.Equal(t, forkedSlot, fork.blocks[0].Block().Slot(), "Expected slot %d to be ancestor", forkedSlot) }) t.Run("first block is diverging - no common ancestor", func(t *testing.T) { @@ -410,7 +411,7 @@ func TestBlocksFetcher_findForkWithPeer(t *testing.T) { fork, err := fetcher.findForkWithPeer(ctx, p2, 64) require.NoError(t, err) require.Equal(t, 64, len(fork.blocks)) - assert.Equal(t, types.Slot(33), fork.blocks[0].Block.Slot) + assert.Equal(t, types.Slot(33), fork.blocks[0].Block().Slot()) }) } @@ -423,7 +424,7 @@ func TestBlocksFetcher_findAncestor(t *testing.T) { finalizedEpoch := helpers.SlotToEpoch(finalizedSlot) genesisBlock := knownBlocks[0] - require.NoError(t, beaconDB.SaveBlock(context.Background(), genesisBlock)) + require.NoError(t, beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genesisBlock))) genesisRoot, err := genesisBlock.Block.HashTreeRoot() require.NoError(t, err) @@ -456,7 +457,7 @@ func TestBlocksFetcher_findAncestor(t *testing.T) { p2 := p2pt.NewTestP2P(t) p2p.Connect(p2) - _, err := fetcher.findAncestor(ctx, p2.PeerID(), knownBlocks[4]) + _, err := fetcher.findAncestor(ctx, p2.PeerID(), interfaces.NewWrappedSignedBeaconBlock(knownBlocks[4])) assert.ErrorContains(t, "protocol not supported", err) }) @@ -468,7 +469,7 @@ func TestBlocksFetcher_findAncestor(t *testing.T) { assert.NoError(t, stream.Close()) }) - fork, err := fetcher.findAncestor(ctx, p2.PeerID(), knownBlocks[4]) + fork, err := fetcher.findAncestor(ctx, p2.PeerID(), interfaces.NewWrappedSignedBeaconBlock(knownBlocks[4])) assert.ErrorContains(t, "no common ancestor found", err) assert.Equal(t, (*forkData)(nil), fork) }) diff --git a/beacon-chain/sync/initial-sync/blocks_queue_test.go b/beacon-chain/sync/initial-sync/blocks_queue_test.go index 1db029aa9c49..9be0449eea1a 100644 --- a/beacon-chain/sync/initial-sync/blocks_queue_test.go +++ b/beacon-chain/sync/initial-sync/blocks_queue_test.go @@ -18,6 +18,7 @@ import ( beaconsync "github.com/prysmaticlabs/prysm/beacon-chain/sync" "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/sliceutil" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -252,18 +253,18 @@ func TestBlocksQueue_Loop(t *testing.T) { highestExpectedSlot: tt.highestExpectedSlot, }) assert.NoError(t, queue.start()) - processBlock := func(block *eth.SignedBeaconBlock) error { - if !beaconDB.HasBlock(ctx, bytesutil.ToBytes32(block.Block.ParentRoot)) { - return fmt.Errorf("%w: %#x", errParentDoesNotExist, block.Block.ParentRoot) + processBlock := func(block interfaces.SignedBeaconBlock) error { + if !beaconDB.HasBlock(ctx, bytesutil.ToBytes32(block.Block().ParentRoot())) { + return fmt.Errorf("%w: %#x", errParentDoesNotExist, block.Block().ParentRoot()) } - root, err := block.Block.HashTreeRoot() + root, err := block.Block().HashTreeRoot() if err != nil { return err } return mc.ReceiveBlock(ctx, block, root) } - var blocks []*eth.SignedBeaconBlock + var blocks []interfaces.SignedBeaconBlock for data := range queue.fetchedData { for _, block := range data.blocks { if err := processBlock(block); err != nil { @@ -282,7 +283,7 @@ func TestBlocksQueue_Loop(t *testing.T) { assert.Equal(t, len(tt.expectedBlockSlots), len(blocks), "Processes wrong number of blocks") var receivedBlockSlots []types.Slot for _, blk := range blocks { - receivedBlockSlots = append(receivedBlockSlots, blk.Block.Slot) + receivedBlockSlots = append(receivedBlockSlots, blk.Block().Slot()) } missing := sliceutil.NotSlot(sliceutil.IntersectionSlot(tt.expectedBlockSlots, receivedBlockSlots), tt.expectedBlockSlots) if len(missing) > 0 { @@ -523,9 +524,9 @@ func TestBlocksQueue_onDataReceivedEvent(t *testing.T) { handlerFn := queue.onDataReceivedEvent(ctx) response := &fetchRequestResponse{ pid: "abc", - blocks: []*eth.SignedBeaconBlock{ - testutil.NewBeaconBlock(), - testutil.NewBeaconBlock(), + blocks: []interfaces.SignedBeaconBlock{ + interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock()), + interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock()), }, } fsm := &stateMachine{ @@ -619,8 +620,8 @@ func TestBlocksQueue_onReadyToSendEvent(t *testing.T) { queue.smm.addStateMachine(320) queue.smm.machines[256].state = stateDataParsed queue.smm.machines[256].pid = pidDataParsed - queue.smm.machines[256].blocks = []*eth.SignedBeaconBlock{ - testutil.NewBeaconBlock(), + queue.smm.machines[256].blocks = []interfaces.SignedBeaconBlock{ + interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock()), } handlerFn := queue.onReadyToSendEvent(ctx) @@ -649,8 +650,8 @@ func TestBlocksQueue_onReadyToSendEvent(t *testing.T) { queue.smm.addStateMachine(320) queue.smm.machines[320].state = stateDataParsed queue.smm.machines[320].pid = pidDataParsed - queue.smm.machines[320].blocks = []*eth.SignedBeaconBlock{ - testutil.NewBeaconBlock(), + queue.smm.machines[320].blocks = []interfaces.SignedBeaconBlock{ + interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock()), } handlerFn := queue.onReadyToSendEvent(ctx) @@ -676,8 +677,8 @@ func TestBlocksQueue_onReadyToSendEvent(t *testing.T) { queue.smm.addStateMachine(320) queue.smm.machines[320].state = stateDataParsed queue.smm.machines[320].pid = pidDataParsed - queue.smm.machines[320].blocks = []*eth.SignedBeaconBlock{ - testutil.NewBeaconBlock(), + queue.smm.machines[320].blocks = []interfaces.SignedBeaconBlock{ + interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock()), } handlerFn := queue.onReadyToSendEvent(ctx) @@ -1036,7 +1037,7 @@ func TestBlocksQueue_stuckInUnfavourableFork(t *testing.T) { finalizedEpoch := helpers.SlotToEpoch(finalizedSlot) genesisBlock := chain1[0] - require.NoError(t, beaconDB.SaveBlock(context.Background(), genesisBlock)) + require.NoError(t, beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genesisBlock))) genesisRoot, err := genesisBlock.Block.HashTreeRoot() require.NoError(t, err) @@ -1076,7 +1077,7 @@ func TestBlocksQueue_stuckInUnfavourableFork(t *testing.T) { parentRoot := bytesutil.ToBytes32(blk.Block.ParentRoot) // Save block only if parent root is already in database or cache. if beaconDB.HasBlock(ctx, parentRoot) || mc.HasInitSyncBlock(parentRoot) { - require.NoError(t, beaconDB.SaveBlock(ctx, blk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, st.SetSlot(blk.Block.Slot)) } } @@ -1199,14 +1200,14 @@ func TestBlocksQueue_stuckInUnfavourableFork(t *testing.T) { require.Equal(t, stateDataParsed, firstFSM.state) require.Equal(t, forkedPeer, firstFSM.pid) require.Equal(t, 64, len(firstFSM.blocks)) - require.Equal(t, forkedEpochStartSlot+1, firstFSM.blocks[0].Block.Slot) + require.Equal(t, forkedEpochStartSlot+1, firstFSM.blocks[0].Block().Slot()) // Assert that forked data from chain2 is available (within 64 fetched blocks). for i, blk := range chain2[forkedEpochStartSlot+1:] { if i >= len(firstFSM.blocks) { break } - rootFromFSM, err := firstFSM.blocks[i].Block.HashTreeRoot() + rootFromFSM, err := firstFSM.blocks[i].Block().HashTreeRoot() require.NoError(t, err) blkRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) @@ -1235,7 +1236,7 @@ func TestBlocksQueue_stuckWhenHeadIsSetToOrphanedBlock(t *testing.T) { finalizedEpoch := helpers.SlotToEpoch(finalizedSlot) genesisBlock := chain[0] - require.NoError(t, beaconDB.SaveBlock(context.Background(), genesisBlock)) + require.NoError(t, beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genesisBlock))) genesisRoot, err := genesisBlock.Block.HashTreeRoot() require.NoError(t, err) @@ -1256,7 +1257,7 @@ func TestBlocksQueue_stuckWhenHeadIsSetToOrphanedBlock(t *testing.T) { parentRoot := bytesutil.ToBytes32(blk.Block.ParentRoot) // Save block only if parent root is already in database or cache. if beaconDB.HasBlock(ctx, parentRoot) || mc.HasInitSyncBlock(parentRoot) { - require.NoError(t, beaconDB.SaveBlock(ctx, blk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, st.SetSlot(blk.Block.Slot)) } } @@ -1268,7 +1269,7 @@ func TestBlocksQueue_stuckWhenHeadIsSetToOrphanedBlock(t *testing.T) { orphanedBlock := testutil.NewBeaconBlock() orphanedBlock.Block.Slot = 85 orphanedBlock.Block.StateRoot = testutil.Random32Bytes(t) - require.NoError(t, beaconDB.SaveBlock(ctx, orphanedBlock)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(orphanedBlock))) require.NoError(t, st.SetSlot(orphanedBlock.Block.Slot)) require.Equal(t, types.Slot(85), mc.HeadSlot()) @@ -1297,12 +1298,12 @@ func TestBlocksQueue_stuckWhenHeadIsSetToOrphanedBlock(t *testing.T) { }) require.NoError(t, queue.start()) - isProcessedBlock := func(ctx context.Context, blk *eth.SignedBeaconBlock, blkRoot [32]byte) bool { + isProcessedBlock := func(ctx context.Context, blk interfaces.SignedBeaconBlock, blkRoot [32]byte) bool { finalizedSlot, err := helpers.StartSlot(mc.FinalizedCheckpt().Epoch) if err != nil { return false } - if blk.Block.Slot <= finalizedSlot || (beaconDB.HasBlock(ctx, blkRoot) || mc.HasInitSyncBlock(blkRoot)) { + if blk.Block().Slot() <= finalizedSlot || (beaconDB.HasBlock(ctx, blkRoot) || mc.HasInitSyncBlock(blkRoot)) { return true } return false @@ -1313,22 +1314,22 @@ func TestBlocksQueue_stuckWhenHeadIsSetToOrphanedBlock(t *testing.T) { t.Fatal("test takes too long to complete") case data := <-queue.fetchedData: for _, blk := range data.blocks { - blkRoot, err := blk.Block.HashTreeRoot() + blkRoot, err := blk.Block().HashTreeRoot() require.NoError(t, err) if isProcessedBlock(ctx, blk, blkRoot) { - log.Errorf("slot: %d , root %#x: %v", blk.Block.Slot, blkRoot, errBlockAlreadyProcessed) + log.Errorf("slot: %d , root %#x: %v", blk.Block().Slot(), blkRoot, errBlockAlreadyProcessed) continue } - parentRoot := bytesutil.ToBytes32(blk.Block.ParentRoot) + parentRoot := bytesutil.ToBytes32(blk.Block().ParentRoot()) if !beaconDB.HasBlock(ctx, parentRoot) && !mc.HasInitSyncBlock(parentRoot) { - log.Errorf("%v: %#x", errParentDoesNotExist, blk.Block.ParentRoot) + log.Errorf("%v: %#x", errParentDoesNotExist, blk.Block().ParentRoot()) continue } // Block is not already processed, and parent exists in database - process. require.NoError(t, beaconDB.SaveBlock(ctx, blk)) - require.NoError(t, st.SetSlot(blk.Block.Slot)) + require.NoError(t, st.SetSlot(blk.Block().Slot())) } } require.NoError(t, queue.stop()) diff --git a/beacon-chain/sync/initial-sync/initial_sync_test.go b/beacon-chain/sync/initial-sync/initial_sync_test.go index f8836c9e990b..1fa3c15705f7 100644 --- a/beacon-chain/sync/initial-sync/initial_sync_test.go +++ b/beacon-chain/sync/initial-sync/initial_sync_test.go @@ -26,6 +26,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/hashutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/sliceutil" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -82,7 +83,7 @@ func initializeTestServices(t *testing.T, slots []types.Slot, peers []*peerData) genesisRoot := cache.rootCache[0] cache.RUnlock() - err := beaconDB.SaveBlock(context.Background(), testutil.NewBeaconBlock()) + err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock())) require.NoError(t, err) st, err := testutil.NewBeaconState() diff --git a/beacon-chain/sync/utils_test.go b/beacon-chain/sync/utils_test.go index 1313734aa41b..6d5b15c3ea8e 100644 --- a/beacon-chain/sync/utils_test.go +++ b/beacon-chain/sync/utils_test.go @@ -7,13 +7,14 @@ import ( types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) func TestSortedObj_SortBlocksRoots(t *testing.T) { source := rand.NewSource(33) randGen := rand.New(source) - var blks []*ethpb.SignedBeaconBlock + var blks []interfaces.SignedBeaconBlock var roots [][32]byte randFunc := func() int64 { return randGen.Int63n(50) @@ -21,7 +22,7 @@ func TestSortedObj_SortBlocksRoots(t *testing.T) { for i := 0; i < 10; i++ { slot := types.Slot(randFunc()) - newBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: slot}} + newBlk := interfaces.NewWrappedSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: slot}}) blks = append(blks, newBlk) root := bytesutil.ToBytes32(bytesutil.Bytes32(uint64(slot))) roots = append(roots, root) @@ -33,20 +34,20 @@ func TestSortedObj_SortBlocksRoots(t *testing.T) { previousSlot := types.Slot(0) for i, b := range newBlks { - if b.Block.Slot < previousSlot { - t.Errorf("Block list is not sorted as %d is smaller than previousSlot %d", b.Block.Slot, previousSlot) + if b.Block().Slot() < previousSlot { + t.Errorf("Block list is not sorted as %d is smaller than previousSlot %d", b.Block().Slot(), previousSlot) } - if bytesutil.FromBytes8(newRoots[i][:]) != uint64(b.Block.Slot) { - t.Errorf("root doesn't match stored slot in block: wanted %d but got %d", b.Block.Slot, bytesutil.FromBytes8(newRoots[i][:])) + if bytesutil.FromBytes8(newRoots[i][:]) != uint64(b.Block().Slot()) { + t.Errorf("root doesn't match stored slot in block: wanted %d but got %d", b.Block().Slot(), bytesutil.FromBytes8(newRoots[i][:])) } - previousSlot = b.Block.Slot + previousSlot = b.Block().Slot() } } func TestSortedObj_NoDuplicates(t *testing.T) { source := rand.NewSource(33) randGen := rand.New(source) - var blks []*ethpb.SignedBeaconBlock + var blks []interfaces.SignedBeaconBlock var roots [][32]byte randFunc := func() int64 { return randGen.Int63n(50) @@ -56,7 +57,7 @@ func TestSortedObj_NoDuplicates(t *testing.T) { slot := types.Slot(randFunc()) newBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: slot}} // append twice - blks = append(blks, newBlk, newBlk) + blks = append(blks, interfaces.NewWrappedSignedBeaconBlock(newBlk), interfaces.NewWrappedSignedBeaconBlock(newBlk)) // append twice root := bytesutil.ToBytes32(bytesutil.Bytes32(uint64(slot))) From f05851488cdb65227261c6b61d792a4398d4c55e Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 25 May 2021 18:13:59 +0800 Subject: [PATCH 19/44] tests --- .../sync/initial-sync/round_robin_test.go | 51 ++++++++++--------- .../sync/initial-sync/service_test.go | 3 +- .../sync/validate_aggregate_proof_test.go | 11 ++-- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/beacon-chain/sync/initial-sync/round_robin_test.go b/beacon-chain/sync/initial-sync/round_robin_test.go index 13f598ec31c5..8ba9658fd1b4 100644 --- a/beacon-chain/sync/initial-sync/round_robin_test.go +++ b/beacon-chain/sync/initial-sync/round_robin_test.go @@ -12,6 +12,7 @@ import ( dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" p2pt "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" "github.com/prysmaticlabs/prysm/shared/abool" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/sliceutil" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -281,7 +282,7 @@ func TestService_roundRobinSync(t *testing.T) { genesisRoot := cache.rootCache[0] cache.RUnlock() - err := beaconDB.SaveBlock(context.Background(), testutil.NewBeaconBlock()) + err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock())) require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -307,7 +308,7 @@ func TestService_roundRobinSync(t *testing.T) { assert.Equal(t, true, len(tt.expectedBlockSlots) <= len(mc.BlocksReceived), "Processes wrong number of blocks") var receivedBlockSlots []types.Slot for _, blk := range mc.BlocksReceived { - receivedBlockSlots = append(receivedBlockSlots, blk.Block.Slot) + receivedBlockSlots = append(receivedBlockSlots, blk.Block().Slot()) } missing := sliceutil.NotSlot(sliceutil.IntersectionSlot(tt.expectedBlockSlots, receivedBlockSlots), tt.expectedBlockSlots) if len(missing) > 0 { @@ -322,7 +323,7 @@ func TestService_processBlock(t *testing.T) { genesisBlk := testutil.NewBeaconBlock() genesisBlkRoot, err := genesisBlk.Block.HashTreeRoot() require.NoError(t, err) - err = beaconDB.SaveBlock(context.Background(), genesisBlk) + err = beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genesisBlk)) require.NoError(t, err) st, err := testutil.NewBeaconState() require.NoError(t, err) @@ -353,23 +354,23 @@ func TestService_processBlock(t *testing.T) { blk2.Block.ParentRoot = blk1Root[:] // Process block normally. - err = s.processBlock(ctx, genesis, blk1, func( - ctx context.Context, block *eth.SignedBeaconBlock, blockRoot [32]byte) error { + err = s.processBlock(ctx, genesis, interfaces.NewWrappedSignedBeaconBlock(blk1), func( + ctx context.Context, block interfaces.SignedBeaconBlock, blockRoot [32]byte) error { assert.NoError(t, s.cfg.Chain.ReceiveBlock(ctx, block, blockRoot)) return nil }) assert.NoError(t, err) // Duplicate processing should trigger error. - err = s.processBlock(ctx, genesis, blk1, func( - ctx context.Context, block *eth.SignedBeaconBlock, blockRoot [32]byte) error { + err = s.processBlock(ctx, genesis, interfaces.NewWrappedSignedBeaconBlock(blk1), func( + ctx context.Context, block interfaces.SignedBeaconBlock, blockRoot [32]byte) error { return nil }) assert.ErrorContains(t, errBlockAlreadyProcessed.Error(), err) // Continue normal processing, should proceed w/o errors. - err = s.processBlock(ctx, genesis, blk2, func( - ctx context.Context, block *eth.SignedBeaconBlock, blockRoot [32]byte) error { + err = s.processBlock(ctx, genesis, interfaces.NewWrappedSignedBeaconBlock(blk2), func( + ctx context.Context, block interfaces.SignedBeaconBlock, blockRoot [32]byte) error { assert.NoError(t, s.cfg.Chain.ReceiveBlock(ctx, block, blockRoot)) return nil }) @@ -383,7 +384,7 @@ func TestService_processBlockBatch(t *testing.T) { genesisBlk := testutil.NewBeaconBlock() genesisBlkRoot, err := genesisBlk.Block.HashTreeRoot() require.NoError(t, err) - err = beaconDB.SaveBlock(context.Background(), genesisBlk) + err = beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(genesisBlk)) require.NoError(t, err) st, err := testutil.NewBeaconState() require.NoError(t, err) @@ -404,7 +405,7 @@ func TestService_processBlockBatch(t *testing.T) { genesis := makeGenesisTime(32) t.Run("process non-linear batch", func(t *testing.T) { - var batch []*eth.SignedBeaconBlock + var batch []interfaces.SignedBeaconBlock currBlockRoot := genesisBlkRoot for i := types.Slot(1); i < 10; i++ { parentRoot := currBlockRoot @@ -413,13 +414,13 @@ func TestService_processBlockBatch(t *testing.T) { blk1.Block.ParentRoot = parentRoot[:] blk1Root, err := blk1.Block.HashTreeRoot() require.NoError(t, err) - err = beaconDB.SaveBlock(context.Background(), blk1) + err = beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk1)) require.NoError(t, err) - batch = append(batch, blk1) + batch = append(batch, interfaces.NewWrappedSignedBeaconBlock(blk1)) currBlockRoot = blk1Root } - var batch2 []*eth.SignedBeaconBlock + var batch2 []interfaces.SignedBeaconBlock for i := types.Slot(10); i < 20; i++ { parentRoot := currBlockRoot blk1 := testutil.NewBeaconBlock() @@ -427,15 +428,15 @@ func TestService_processBlockBatch(t *testing.T) { blk1.Block.ParentRoot = parentRoot[:] blk1Root, err := blk1.Block.HashTreeRoot() require.NoError(t, err) - err = beaconDB.SaveBlock(context.Background(), blk1) + err = beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(blk1)) require.NoError(t, err) - batch2 = append(batch2, blk1) + batch2 = append(batch2, interfaces.NewWrappedSignedBeaconBlock(blk1)) currBlockRoot = blk1Root } // Process block normally. err = s.processBatchedBlocks(ctx, genesis, batch, func( - ctx context.Context, blks []*eth.SignedBeaconBlock, blockRoots [][32]byte) error { + ctx context.Context, blks []interfaces.SignedBeaconBlock, blockRoots [][32]byte) error { assert.NoError(t, s.cfg.Chain.ReceiveBlockBatch(ctx, blks, blockRoots)) return nil }) @@ -443,12 +444,12 @@ func TestService_processBlockBatch(t *testing.T) { // Duplicate processing should trigger error. err = s.processBatchedBlocks(ctx, genesis, batch, func( - ctx context.Context, blocks []*eth.SignedBeaconBlock, blockRoots [][32]byte) error { + ctx context.Context, blocks []interfaces.SignedBeaconBlock, blockRoots [][32]byte) error { return nil }) assert.ErrorContains(t, "no good blocks in batch", err) - var badBatch2 []*eth.SignedBeaconBlock + var badBatch2 []interfaces.SignedBeaconBlock for i, b := range batch2 { // create a non-linear batch if i%3 == 0 && i != 0 { @@ -459,7 +460,7 @@ func TestService_processBlockBatch(t *testing.T) { // Bad batch should fail because it is non linear err = s.processBatchedBlocks(ctx, genesis, badBatch2, func( - ctx context.Context, blks []*eth.SignedBeaconBlock, blockRoots [][32]byte) error { + ctx context.Context, blks []interfaces.SignedBeaconBlock, blockRoots [][32]byte) error { return nil }) expectedSubErr := "expected linear block list" @@ -467,7 +468,7 @@ func TestService_processBlockBatch(t *testing.T) { // Continue normal processing, should proceed w/o errors. err = s.processBatchedBlocks(ctx, genesis, batch2, func( - ctx context.Context, blks []*eth.SignedBeaconBlock, blockRoots [][32]byte) error { + ctx context.Context, blks []interfaces.SignedBeaconBlock, blockRoots [][32]byte) error { assert.NoError(t, s.cfg.Chain.ReceiveBlockBatch(ctx, blks, blockRoots)) return nil }) @@ -511,7 +512,7 @@ func TestService_blockProviderScoring(t *testing.T) { genesisRoot := cache.rootCache[0] cache.RUnlock() - err := beaconDB.SaveBlock(context.Background(), testutil.NewBeaconBlock()) + err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock())) require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -547,7 +548,7 @@ func TestService_blockProviderScoring(t *testing.T) { assert.Equal(t, true, len(expectedBlockSlots) <= len(mc.BlocksReceived), "Processes wrong number of blocks") var receivedBlockSlots []types.Slot for _, blk := range mc.BlocksReceived { - receivedBlockSlots = append(receivedBlockSlots, blk.Block.Slot) + receivedBlockSlots = append(receivedBlockSlots, blk.Block().Slot()) } missing := sliceutil.NotSlot(sliceutil.IntersectionSlot(expectedBlockSlots, receivedBlockSlots), expectedBlockSlots) if len(missing) > 0 { @@ -575,7 +576,7 @@ func TestService_syncToFinalizedEpoch(t *testing.T) { genesisRoot := cache.rootCache[0] cache.RUnlock() - err := beaconDB.SaveBlock(context.Background(), testutil.NewBeaconBlock()) + err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock())) require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -614,7 +615,7 @@ func TestService_syncToFinalizedEpoch(t *testing.T) { assert.Equal(t, true, len(expectedBlockSlots) <= len(mc.BlocksReceived), "Processes wrong number of blocks") var receivedBlockSlots []types.Slot for _, blk := range mc.BlocksReceived { - receivedBlockSlots = append(receivedBlockSlots, blk.Block.Slot) + receivedBlockSlots = append(receivedBlockSlots, blk.Block().Slot()) } missing := sliceutil.NotSlot(sliceutil.IntersectionSlot(expectedBlockSlots, receivedBlockSlots), expectedBlockSlots) if len(missing) > 0 { diff --git a/beacon-chain/sync/initial-sync/service_test.go b/beacon-chain/sync/initial-sync/service_test.go index d45a0ab66036..9c7de8d2d67a 100644 --- a/beacon-chain/sync/initial-sync/service_test.go +++ b/beacon-chain/sync/initial-sync/service_test.go @@ -18,6 +18,7 @@ import ( "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" "github.com/prysmaticlabs/prysm/shared/abool" "github.com/prysmaticlabs/prysm/shared/event" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -371,7 +372,7 @@ func TestService_Resync(t *testing.T) { }, p.Peers()) cache.initializeRootCache(makeSequence(1, 160), t) beaconDB := dbtest.SetupDB(t) - err := beaconDB.SaveBlock(context.Background(), testutil.NewBeaconBlock()) + err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock())) require.NoError(t, err) cache.RLock() genesisRoot := cache.rootCache[0] diff --git a/beacon-chain/sync/validate_aggregate_proof_test.go b/beacon-chain/sync/validate_aggregate_proof_test.go index 0d9cc8ec346c..9903fb134e11 100644 --- a/beacon-chain/sync/validate_aggregate_proof_test.go +++ b/beacon-chain/sync/validate_aggregate_proof_test.go @@ -23,6 +23,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -151,7 +152,7 @@ func TestValidateAggregateAndProof_NotWithinSlotRange(t *testing.T) { beaconState, _ := testutil.DeterministicGenesisState(t, validators) b := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) root, err := b.Block.HashTreeRoot() require.NoError(t, err) s, err := testutil.NewBeaconState() @@ -239,7 +240,7 @@ func TestValidateAggregateAndProof_ExistedInPool(t *testing.T) { beaconState, _ := testutil.DeterministicGenesisState(t, validators) b := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) root, err := b.Block.HashTreeRoot() require.NoError(t, err) @@ -307,7 +308,7 @@ func TestValidateAggregateAndProof_CanValidate(t *testing.T) { beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) b := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) root, err := b.Block.HashTreeRoot() require.NoError(t, err) s, err := testutil.NewBeaconState() @@ -400,7 +401,7 @@ func TestVerifyIndexInCommittee_SeenAggregatorEpoch(t *testing.T) { beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) b := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) root, err := b.Block.HashTreeRoot() require.NoError(t, err) s, err := testutil.NewBeaconState() @@ -603,7 +604,7 @@ func TestValidateAggregateAndProof_RejectWhenAttEpochDoesntEqualTargetEpoch(t *t beaconState, privKeys := testutil.DeterministicGenesisState(t, validators) b := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(context.Background(), b)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) root, err := b.Block.HashTreeRoot() require.NoError(t, err) s, err := testutil.NewBeaconState() From 78cd4010d2cc8388a7737e60e97080e4b7df5b7e Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 25 May 2021 23:25:41 +0800 Subject: [PATCH 20/44] finally fix builds --- beacon-chain/blockchain/BUILD.bazel | 2 + .../blockchain/chain_info_norace_test.go | 3 +- beacon-chain/blockchain/chain_info_test.go | 7 +- beacon-chain/blockchain/head_test.go | 7 +- beacon-chain/blockchain/info_test.go | 3 +- .../blockchain/process_attestation_test.go | 19 +++-- beacon-chain/blockchain/process_block_test.go | 85 ++++++++++--------- .../blockchain/receive_attestation_test.go | 9 +- beacon-chain/blockchain/receive_block_test.go | 19 +++-- beacon-chain/blockchain/service_test.go | 39 ++++----- .../weak_subjectivity_checks_test.go | 3 +- beacon-chain/db/kv/BUILD.bazel | 1 + beacon-chain/sync/initial-sync/BUILD.bazel | 1 + .../sync/validate_beacon_attestation_test.go | 8 +- .../sync/validate_beacon_blocks_test.go | 21 ++--- shared/testutil/block_test.go | 17 ++-- 16 files changed, 131 insertions(+), 113 deletions(-) diff --git a/beacon-chain/blockchain/BUILD.bazel b/beacon-chain/blockchain/BUILD.bazel index 5db74a110373..25373b1870d4 100644 --- a/beacon-chain/blockchain/BUILD.bazel +++ b/beacon-chain/blockchain/BUILD.bazel @@ -42,6 +42,7 @@ go_library( "//beacon-chain/p2p:go_default_library", "//beacon-chain/powchain:go_default_library", "//beacon-chain/state/interface:go_default_library", + "//beacon-chain/state/stateV0:go_default_library", "//beacon-chain/state/stategen:go_default_library", "//cmd/beacon-chain/flags:go_default_library", "//proto/beacon/p2p/v1:go_default_library", @@ -105,6 +106,7 @@ go_test( "//beacon-chain/db/testing:go_default_library", "//beacon-chain/p2p:go_default_library", "//beacon-chain/powchain:go_default_library", + "//beacon-chain/state/stateV0:go_default_library", "//beacon-chain/state/stateutil:go_default_library", "//proto/beacon/db:go_default_library", "//proto/beacon/p2p/v1:go_default_library", diff --git a/beacon-chain/blockchain/chain_info_norace_test.go b/beacon-chain/blockchain/chain_info_norace_test.go index 40f3df163285..137f9d47f840 100644 --- a/beacon-chain/blockchain/chain_info_norace_test.go +++ b/beacon-chain/blockchain/chain_info_norace_test.go @@ -7,6 +7,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) @@ -38,7 +39,7 @@ func TestHeadBlock_DataRace(t *testing.T) { beaconDB := testDB.SetupDB(t) s := &Service{ cfg: &Config{BeaconDB: beaconDB, StateGen: stategen.New(beaconDB)}, - head: &head{block: ðpb.SignedBeaconBlock{}}, + head: &head{block: interfaces.NewWrappedSignedBeaconBlock(ðpb.SignedBeaconBlock{})}, } go func() { require.NoError(t, s.saveHead(context.Background(), [32]byte{})) diff --git a/beacon-chain/blockchain/chain_info_test.go b/beacon-chain/blockchain/chain_info_test.go index bd639bdb6cf2..c6551ee4b8ea 100644 --- a/beacon-chain/blockchain/chain_info_test.go +++ b/beacon-chain/blockchain/chain_info_test.go @@ -13,6 +13,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -125,7 +126,7 @@ func TestHeadRoot_UseDB(t *testing.T) { b := testutil.NewBeaconBlock() br, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(context.Background(), b)) + require.NoError(t, beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) require.NoError(t, beaconDB.SaveStateSummary(context.Background(), &pb.StateSummary{Root: br[:]})) require.NoError(t, beaconDB.SaveHeadBlockRoot(context.Background(), br)) r, err := c.HeadRoot(context.Background()) @@ -139,7 +140,7 @@ func TestHeadBlock_CanRetrieve(t *testing.T) { s, err := stateV0.InitializeFromProto(&pb.BeaconState{}) require.NoError(t, err) c := &Service{} - c.head = &head{block: b, state: s} + c.head = &head{block: interfaces.NewWrappedSignedBeaconBlock(b), state: s} recevied, err := c.HeadBlock(context.Background()) require.NoError(t, err) @@ -221,7 +222,7 @@ func TestIsCanonical_Ok(t *testing.T) { blk.Block.Slot = 0 root, err := blk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, blk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, root)) can, err := c.IsCanonical(ctx, root) require.NoError(t, err) diff --git a/beacon-chain/blockchain/head_test.go b/beacon-chain/blockchain/head_test.go index d82530c81704..66f1b0e6c5e6 100644 --- a/beacon-chain/blockchain/head_test.go +++ b/beacon-chain/blockchain/head_test.go @@ -9,6 +9,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -39,7 +40,7 @@ func TestSaveHead_Different(t *testing.T) { newHeadSignedBlock.Block.Slot = 1 newHeadBlock := newHeadSignedBlock.Block - require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), newHeadSignedBlock)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(newHeadSignedBlock))) newRoot, err := newHeadBlock.HashTreeRoot() require.NoError(t, err) headState, err := testutil.NewBeaconState() @@ -73,7 +74,7 @@ func TestSaveHead_Different_Reorg(t *testing.T) { newHeadSignedBlock.Block.ParentRoot = reorgChainParent[:] newHeadBlock := newHeadSignedBlock.Block - require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), newHeadSignedBlock)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(newHeadSignedBlock))) newRoot, err := newHeadBlock.HashTreeRoot() require.NoError(t, err) headState, err := testutil.NewBeaconState() @@ -112,7 +113,7 @@ func TestUpdateHead_MissingJustifiedRoot(t *testing.T) { service := setupBeaconChain(t, beaconDB) b := testutil.NewBeaconBlock() - require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), b)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) diff --git a/beacon-chain/blockchain/info_test.go b/beacon-chain/blockchain/info_test.go index b969679b1c38..a932743e6113 100644 --- a/beacon-chain/blockchain/info_test.go +++ b/beacon-chain/blockchain/info_test.go @@ -9,6 +9,7 @@ import ( testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -37,7 +38,7 @@ func TestService_TreeHandler(t *testing.T) { require.NoError(t, err) require.NoError(t, s.cfg.ForkChoiceStore.ProcessBlock(ctx, 0, [32]byte{'a'}, [32]byte{'g'}, [32]byte{'c'}, 0, 0)) require.NoError(t, s.cfg.ForkChoiceStore.ProcessBlock(ctx, 1, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'c'}, 0, 0)) - s.setHead([32]byte{'a'}, testutil.NewBeaconBlock(), headState) + s.setHead([32]byte{'a'}, interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock()), headState) rr := httptest.NewRecorder() handler := http.HandlerFunc(s.TreeHandler) diff --git a/beacon-chain/blockchain/process_attestation_test.go b/beacon-chain/blockchain/process_attestation_test.go index ec4476c0d873..6cc1bac23587 100644 --- a/beacon-chain/blockchain/process_attestation_test.go +++ b/beacon-chain/blockchain/process_attestation_test.go @@ -13,6 +13,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -37,13 +38,13 @@ func TestStore_OnAttestation_ErrorConditions(t *testing.T) { BlkWithOutState := testutil.NewBeaconBlock() BlkWithOutState.Block.Slot = 0 - require.NoError(t, beaconDB.SaveBlock(ctx, BlkWithOutState)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(BlkWithOutState))) BlkWithOutStateRoot, err := BlkWithOutState.Block.HashTreeRoot() require.NoError(t, err) BlkWithStateBadAtt := testutil.NewBeaconBlock() BlkWithStateBadAtt.Block.Slot = 1 - require.NoError(t, beaconDB.SaveBlock(ctx, BlkWithStateBadAtt)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(BlkWithStateBadAtt))) BlkWithStateBadAttRoot, err := BlkWithStateBadAtt.Block.HashTreeRoot() require.NoError(t, err) @@ -54,7 +55,7 @@ func TestStore_OnAttestation_ErrorConditions(t *testing.T) { BlkWithValidState := testutil.NewBeaconBlock() BlkWithValidState.Block.Slot = 2 - require.NoError(t, beaconDB.SaveBlock(ctx, BlkWithValidState)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(BlkWithValidState))) BlkWithValidStateRoot, err := BlkWithValidState.Block.HashTreeRoot() require.NoError(t, err) @@ -321,7 +322,7 @@ func TestVerifyBeaconBlock_futureBlock(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = 2 - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) d := ðpb.AttestationData{Slot: 1, BeaconBlockRoot: r[:]} @@ -339,7 +340,7 @@ func TestVerifyBeaconBlock_OK(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = 2 - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) d := ðpb.AttestationData{Slot: 2, BeaconBlockRoot: r[:]} @@ -357,7 +358,7 @@ func TestVerifyFinalizedConsistency_InconsistentRoot(t *testing.T) { b32 := testutil.NewBeaconBlock() b32.Block.Slot = 32 - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b32)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b32))) r32, err := b32.Block.HashTreeRoot() require.NoError(t, err) @@ -366,7 +367,7 @@ func TestVerifyFinalizedConsistency_InconsistentRoot(t *testing.T) { b33 := testutil.NewBeaconBlock() b33.Block.Slot = 33 b33.Block.ParentRoot = r32[:] - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b33)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b33))) r33, err := b33.Block.HashTreeRoot() require.NoError(t, err) @@ -384,7 +385,7 @@ func TestVerifyFinalizedConsistency_OK(t *testing.T) { b32 := testutil.NewBeaconBlock() b32.Block.Slot = 32 - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b32)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b32))) r32, err := b32.Block.HashTreeRoot() require.NoError(t, err) @@ -393,7 +394,7 @@ func TestVerifyFinalizedConsistency_OK(t *testing.T) { b33 := testutil.NewBeaconBlock() b33.Block.Slot = 33 b33.Block.ParentRoot = r32[:] - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b33)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b33))) r33, err := b33.Block.HashTreeRoot() require.NoError(t, err) diff --git a/beacon-chain/blockchain/process_block_test.go b/beacon-chain/blockchain/process_block_test.go index ef1359ac4bcd..8b13ad202d84 100644 --- a/beacon-chain/blockchain/process_block_test.go +++ b/beacon-chain/blockchain/process_block_test.go @@ -23,6 +23,7 @@ import ( pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -43,7 +44,7 @@ func TestStore_OnBlock(t *testing.T) { require.NoError(t, err) genesisStateRoot := [32]byte{} genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - assert.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) validGenesisRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -54,7 +55,7 @@ func TestStore_OnBlock(t *testing.T) { random := testutil.NewBeaconBlock() random.Block.Slot = 1 random.Block.ParentRoot = validGenesisRoot[:] - assert.NoError(t, beaconDB.SaveBlock(ctx, random)) + assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(random))) randomParentRoot, err := random.Block.HashTreeRoot() assert.NoError(t, err) require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: st.Slot(), Root: randomParentRoot[:]})) @@ -120,7 +121,7 @@ func TestStore_OnBlock(t *testing.T) { root, err := tt.blk.Block.HashTreeRoot() assert.NoError(t, err) - err = service.onBlock(ctx, tt.blk, root) + err = service.onBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(tt.blk), root) assert.ErrorContains(t, tt.wantErrString, err) }) } @@ -139,38 +140,40 @@ func TestStore_OnBlockBatch(t *testing.T) { genesisStateRoot := [32]byte{} genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - assert.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) gRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) service.finalizedCheckpt = ðpb.Checkpoint{ Root: gRoot[:], } service.cfg.ForkChoiceStore = protoarray.New(0, 0, [32]byte{}) - service.saveInitSyncBlock(gRoot, genesis) + service.saveInitSyncBlock(gRoot, interfaces.NewWrappedSignedBeaconBlock(genesis)) st, keys := testutil.DeterministicGenesisState(t, 64) bState := st.Copy() - var blks []*ethpb.SignedBeaconBlock + var blks []interfaces.SignedBeaconBlock var blkRoots [][32]byte var firstState iface.BeaconState for i := 1; i < 10; i++ { b, err := testutil.GenerateFullBlock(bState, keys, testutil.DefaultBlockGenConfig(), types.Slot(i)) require.NoError(t, err) - bState, err = state.ExecuteStateTransition(ctx, bState, b) + bState, err = state.ExecuteStateTransition(ctx, bState, interfaces.NewWrappedSignedBeaconBlock(b)) require.NoError(t, err) if i == 1 { firstState = bState.Copy() } root, err := b.Block.HashTreeRoot() require.NoError(t, err) - service.saveInitSyncBlock(root, b) - blks = append(blks, b) + service.saveInitSyncBlock(root, interfaces.NewWrappedSignedBeaconBlock(b)) + blks = append(blks, interfaces.NewWrappedSignedBeaconBlock(b)) blkRoots = append(blkRoots, root) } - blks[0].Block.ParentRoot = gRoot[:] + rBlock, err := blks[0].RawPhase0Block() + assert.NoError(t, err) + rBlock.Block.ParentRoot = gRoot[:] require.NoError(t, beaconDB.SaveBlock(context.Background(), blks[0])) require.NoError(t, service.cfg.StateGen.SaveState(ctx, blkRoots[0], firstState)) _, _, err = service.onBlockBatch(ctx, blks[1:], blkRoots[1:]) @@ -200,8 +203,8 @@ func TestRemoveStateSinceLastFinalized_EmptyStartSlot(t *testing.T) { newJustifiedBlk.Block.ParentRoot = bytesutil.PadTo(lastJustifiedRoot[:], 32) newJustifiedRoot, err := newJustifiedBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, newJustifiedBlk)) - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, lastJustifiedBlk)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(newJustifiedBlk))) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(lastJustifiedBlk))) diff := params.BeaconConfig().SlotsPerEpoch.Sub(1).Mul(params.BeaconConfig().SecondsPerSlot) service.genesisTime = time.Unix(time.Now().Unix()-int64(diff), 0) @@ -228,8 +231,8 @@ func TestShouldUpdateJustified_ReturnFalse(t *testing.T) { newJustifiedBlk.Block.ParentRoot = bytesutil.PadTo(lastJustifiedRoot[:], 32) newJustifiedRoot, err := newJustifiedBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, newJustifiedBlk)) - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, lastJustifiedBlk)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(newJustifiedBlk))) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(lastJustifiedBlk))) diff := params.BeaconConfig().SlotsPerEpoch.Sub(1).Mul(params.BeaconConfig().SecondsPerSlot) service.genesisTime = time.Unix(time.Now().Unix()-int64(diff), 0) @@ -256,21 +259,21 @@ func TestCachedPreState_CanGetFromStateSummary(t *testing.T) { genesisStateRoot := [32]byte{} genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - assert.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) gRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) service.finalizedCheckpt = ðpb.Checkpoint{ Root: gRoot[:], } service.cfg.ForkChoiceStore = protoarray.New(0, 0, [32]byte{}) - service.saveInitSyncBlock(gRoot, genesis) + service.saveInitSyncBlock(gRoot, interfaces.NewWrappedSignedBeaconBlock(genesis)) b := testutil.NewBeaconBlock() b.Block.Slot = 1 b.Block.ParentRoot = gRoot[:] require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 1, Root: gRoot[:]})) require.NoError(t, service.cfg.StateGen.SaveState(ctx, gRoot, s)) - require.NoError(t, service.verifyBlkPreState(ctx, b.Block)) + require.NoError(t, service.verifyBlkPreState(ctx, interfaces.NewWrappedBeaconBlock(b.Block))) } func TestCachedPreState_CanGetFromDB(t *testing.T) { @@ -286,19 +289,19 @@ func TestCachedPreState_CanGetFromDB(t *testing.T) { genesisStateRoot := [32]byte{} genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - assert.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) gRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) service.finalizedCheckpt = ðpb.Checkpoint{ Root: gRoot[:], } service.cfg.ForkChoiceStore = protoarray.New(0, 0, [32]byte{}) - service.saveInitSyncBlock(gRoot, genesis) + service.saveInitSyncBlock(gRoot, interfaces.NewWrappedSignedBeaconBlock(genesis)) b := testutil.NewBeaconBlock() b.Block.Slot = 1 service.finalizedCheckpt = ðpb.Checkpoint{Root: gRoot[:]} - err = service.verifyBlkPreState(ctx, b.Block) + err = service.verifyBlkPreState(ctx, interfaces.NewWrappedBeaconBlock(b.Block)) wanted := "could not reconstruct parent state" assert.ErrorContains(t, wanted, err) @@ -307,7 +310,7 @@ func TestCachedPreState_CanGetFromDB(t *testing.T) { require.NoError(t, err) require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 1, Root: gRoot[:]})) require.NoError(t, service.cfg.StateGen.SaveState(ctx, gRoot, s)) - require.NoError(t, service.verifyBlkPreState(ctx, b.Block)) + require.NoError(t, service.verifyBlkPreState(ctx, interfaces.NewWrappedSignedBeaconBlock(b).Block())) } func TestUpdateJustified_CouldUpdateBest(t *testing.T) { @@ -319,7 +322,7 @@ func TestUpdateJustified_CouldUpdateBest(t *testing.T) { require.NoError(t, err) signedBlock := testutil.NewBeaconBlock() - require.NoError(t, beaconDB.SaveBlock(ctx, signedBlock)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(signedBlock))) r, err := signedBlock.Block.HashTreeRoot() require.NoError(t, err) service.justifiedCheckpt = ðpb.Checkpoint{Root: []byte{'A'}} @@ -355,7 +358,7 @@ func TestFillForkChoiceMissingBlocks_CanSave(t *testing.T) { genesisStateRoot := [32]byte{} genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - require.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) validGenesisRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -371,7 +374,7 @@ func TestFillForkChoiceMissingBlocks_CanSave(t *testing.T) { block.Block.ParentRoot = roots[8] err = service.fillInForkChoiceMissingBlocks( - context.Background(), block.Block, beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint()) + context.Background(), interfaces.NewWrappedSignedBeaconBlock(block).Block(), beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint()) require.NoError(t, err) // 5 nodes from the block tree 1. B0 - B3 - B4 - B6 - B8 @@ -393,7 +396,7 @@ func TestFillForkChoiceMissingBlocks_RootsMatch(t *testing.T) { genesisStateRoot := [32]byte{} genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - require.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) validGenesisRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) st, err := testutil.NewBeaconState() @@ -409,7 +412,7 @@ func TestFillForkChoiceMissingBlocks_RootsMatch(t *testing.T) { block.Block.ParentRoot = roots[8] err = service.fillInForkChoiceMissingBlocks( - context.Background(), block.Block, beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint()) + context.Background(), interfaces.NewWrappedSignedBeaconBlock(block).Block(), beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint()) require.NoError(t, err) // 5 nodes from the block tree 1. B0 - B3 - B4 - B6 - B8 @@ -435,7 +438,7 @@ func TestFillForkChoiceMissingBlocks_FilterFinalized(t *testing.T) { genesisStateRoot := [32]byte{} genesis := blocks.NewGenesisBlock(genesisStateRoot[:]) - assert.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) validGenesisRoot, err := genesis.Block.HashTreeRoot() assert.NoError(t, err) st, err := testutil.NewBeaconState() @@ -446,23 +449,23 @@ func TestFillForkChoiceMissingBlocks_FilterFinalized(t *testing.T) { // Define a tree branch, slot 63 <- 64 <- 65 b63 := testutil.NewBeaconBlock() b63.Block.Slot = 63 - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b63)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b63))) r63, err := b63.Block.HashTreeRoot() require.NoError(t, err) b64 := testutil.NewBeaconBlock() b64.Block.Slot = 64 b64.Block.ParentRoot = r63[:] - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b64)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b64))) r64, err := b64.Block.HashTreeRoot() require.NoError(t, err) b65 := testutil.NewBeaconBlock() b65.Block.Slot = 65 b65.Block.ParentRoot = r64[:] - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b65)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b65))) beaconState, _ := testutil.DeterministicGenesisState(t, 32) err = service.fillInForkChoiceMissingBlocks( - context.Background(), b65.Block, beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint()) + context.Background(), interfaces.NewWrappedSignedBeaconBlock(b65).Block(), beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint()) require.NoError(t, err) // There should be 2 nodes, block 65 and block 64. @@ -542,7 +545,7 @@ func blockTree1(t *testing.T, beaconDB db.Database, genesisRoot []byte) ([][]byt beaconBlock := testutil.NewBeaconBlock() beaconBlock.Block.Slot = b.Block.Slot beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) - if err := beaconDB.SaveBlock(context.Background(), beaconBlock); err != nil { + if err := beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(beaconBlock)); err != nil { return nil, err } if err := beaconDB.SaveState(context.Background(), st.Copy(), bytesutil.ToBytes32(beaconBlock.Block.ParentRoot)); err != nil { @@ -604,7 +607,7 @@ func TestAncestor_HandleSkipSlot(t *testing.T) { beaconBlock := testutil.NewBeaconBlock() beaconBlock.Block.Slot = b.Block.Slot beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) - require.NoError(t, beaconDB.SaveBlock(context.Background(), beaconBlock)) + require.NoError(t, beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(beaconBlock))) } // Slots 100 to 200 are skip slots. Requesting root at 150 will yield root at 100. The last physical block. @@ -686,7 +689,7 @@ func TestAncestor_CanUseDB(t *testing.T) { beaconBlock := testutil.NewBeaconBlock() beaconBlock.Block.Slot = b.Block.Slot beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) - require.NoError(t, beaconDB.SaveBlock(context.Background(), beaconBlock)) // Saves blocks to DB. + require.NoError(t, beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(beaconBlock))) // Saves blocks to DB. } require.NoError(t, service.cfg.ForkChoiceStore.ProcessBlock(context.Background(), 200, r200, r200, [32]byte{}, 0, 0)) @@ -778,7 +781,7 @@ func TestFinalizedImpliesNewJustified(t *testing.T) { beaconBlock := testutil.NewBeaconBlock() beaconBlock.Block.Slot = b.Block.Slot beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32) - require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), beaconBlock)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(beaconBlock))) } service.finalizedCheckpt = ðpb.Checkpoint{Root: []byte{'c'}, Epoch: 1} service.justifiedCheckpt.Root = r100[:] @@ -797,14 +800,14 @@ func TestVerifyBlkDescendant(t *testing.T) { b.Block.Slot = 1 r, err := b.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, b)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b))) b1 := testutil.NewBeaconBlock() b1.Block.Slot = 1 b1.Block.Body.Graffiti = bytesutil.PadTo([]byte{'a'}, 32) r1, err := b1.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, b1)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b1))) type args struct { parentRoot [32]byte @@ -871,7 +874,7 @@ func TestUpdateJustifiedInitSync(t *testing.T) { gBlk := testutil.NewBeaconBlock() gRoot, err := gBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, gBlk)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(gBlk))) require.NoError(t, service.cfg.BeaconDB.SaveGenesisBlockRoot(ctx, gRoot)) require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: gRoot[:]})) beaconState, _ := testutil.DeterministicGenesisState(t, 32) @@ -935,7 +938,7 @@ func TestOnBlock_CanFinalize(t *testing.T) { require.NoError(t, service.saveGenesisData(ctx, gs)) gBlk, err := service.cfg.BeaconDB.GenesisBlock(ctx) require.NoError(t, err) - gRoot, err := gBlk.Block.HashTreeRoot() + gRoot, err := gBlk.Block().HashTreeRoot() require.NoError(t, err) service.finalizedCheckpt = ðpb.Checkpoint{Root: gRoot[:]} @@ -945,7 +948,7 @@ func TestOnBlock_CanFinalize(t *testing.T) { require.NoError(t, err) r, err := blk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, service.onBlock(ctx, blk, r)) + require.NoError(t, service.onBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk), r)) testState, err = service.cfg.StateGen.StateByRoot(ctx, r) require.NoError(t, err) } @@ -971,7 +974,7 @@ func TestInsertFinalizedDeposits(t *testing.T) { require.NoError(t, service.saveGenesisData(ctx, gs)) gBlk, err := service.cfg.BeaconDB.GenesisBlock(ctx) require.NoError(t, err) - gRoot, err := gBlk.Block.HashTreeRoot() + gRoot, err := gBlk.Block().HashTreeRoot() require.NoError(t, err) service.finalizedCheckpt = ðpb.Checkpoint{Root: gRoot[:]} gs = gs.Copy() diff --git a/beacon-chain/blockchain/receive_attestation_test.go b/beacon-chain/blockchain/receive_attestation_test.go index 69859afb0fc5..cb1cbcad325c 100644 --- a/beacon-chain/blockchain/receive_attestation_test.go +++ b/beacon-chain/blockchain/receive_attestation_test.go @@ -14,6 +14,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" @@ -43,13 +44,13 @@ func TestVerifyLMDFFGConsistent_NotOK(t *testing.T) { b32 := testutil.NewBeaconBlock() b32.Block.Slot = 32 - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b32)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b32))) r32, err := b32.Block.HashTreeRoot() require.NoError(t, err) b33 := testutil.NewBeaconBlock() b33.Block.Slot = 33 b33.Block.ParentRoot = r32[:] - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b33)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b33))) r33, err := b33.Block.HashTreeRoot() require.NoError(t, err) @@ -71,13 +72,13 @@ func TestVerifyLMDFFGConsistent_OK(t *testing.T) { b32 := testutil.NewBeaconBlock() b32.Block.Slot = 32 - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b32)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b32))) r32, err := b32.Block.HashTreeRoot() require.NoError(t, err) b33 := testutil.NewBeaconBlock() b33.Block.Slot = 33 b33.Block.ParentRoot = r32[:] - require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, b33)) + require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b33))) r33, err := b33.Block.HashTreeRoot() require.NoError(t, err) diff --git a/beacon-chain/blockchain/receive_block_test.go b/beacon-chain/blockchain/receive_block_test.go index 132dee6d0775..0569a92be184 100644 --- a/beacon-chain/blockchain/receive_block_test.go +++ b/beacon-chain/blockchain/receive_block_test.go @@ -15,6 +15,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/operations/voluntaryexits" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -53,7 +54,7 @@ func TestService_ReceiveBlock(t *testing.T) { if hs := s.head.state.Slot(); hs != 2 { t.Errorf("Unexpected state slot. Got %d but wanted %d", hs, 2) } - if bs := s.head.block.Block.Slot; bs != 2 { + if bs := s.head.block.Block().Slot(); bs != 2 { t.Errorf("Unexpected head block slot. Got %d but wanted %d", bs, 2) } }, @@ -139,12 +140,12 @@ func TestService_ReceiveBlock(t *testing.T) { require.NoError(t, s.saveGenesisData(ctx, genesis)) gBlk, err := s.cfg.BeaconDB.GenesisBlock(ctx) require.NoError(t, err) - gRoot, err := gBlk.Block.HashTreeRoot() + gRoot, err := gBlk.Block().HashTreeRoot() require.NoError(t, err) s.finalizedCheckpt = ðpb.Checkpoint{Root: gRoot[:]} root, err := tt.args.block.Block.HashTreeRoot() require.NoError(t, err) - err = s.ReceiveBlock(ctx, tt.args.block, root) + err = s.ReceiveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(tt.args.block), root) if tt.wantedErr != "" { assert.ErrorContains(t, tt.wantedErr, err) } else { @@ -180,7 +181,7 @@ func TestService_ReceiveBlockUpdateHead(t *testing.T) { require.NoError(t, s.saveGenesisData(ctx, genesis)) gBlk, err := s.cfg.BeaconDB.GenesisBlock(ctx) require.NoError(t, err) - gRoot, err := gBlk.Block.HashTreeRoot() + gRoot, err := gBlk.Block().HashTreeRoot() require.NoError(t, err) s.finalizedCheckpt = ðpb.Checkpoint{Root: gRoot[:]} root, err := b.Block.HashTreeRoot() @@ -188,7 +189,7 @@ func TestService_ReceiveBlockUpdateHead(t *testing.T) { wg := sync.WaitGroup{} wg.Add(1) go func() { - require.NoError(t, s.ReceiveBlock(ctx, b, root)) + require.NoError(t, s.ReceiveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b), root)) wg.Done() }() wg.Wait() @@ -225,7 +226,7 @@ func TestService_ReceiveBlockBatch(t *testing.T) { }, check: func(t *testing.T, s *Service) { assert.Equal(t, types.Slot(2), s.head.state.Slot(), "Incorrect head state slot") - assert.Equal(t, types.Slot(2), s.head.block.Block.Slot, "Incorrect head block slot") + assert.Equal(t, types.Slot(2), s.head.block.Block().Slot(), "Incorrect head block slot") }, }, { @@ -263,12 +264,12 @@ func TestService_ReceiveBlockBatch(t *testing.T) { gBlk, err := s.cfg.BeaconDB.GenesisBlock(ctx) require.NoError(t, err) - gRoot, err := gBlk.Block.HashTreeRoot() + gRoot, err := gBlk.Block().HashTreeRoot() require.NoError(t, err) s.finalizedCheckpt = ðpb.Checkpoint{Root: gRoot[:]} root, err := tt.args.block.Block.HashTreeRoot() require.NoError(t, err) - blks := []*ethpb.SignedBeaconBlock{tt.args.block} + blks := []interfaces.SignedBeaconBlock{interfaces.NewWrappedSignedBeaconBlock(tt.args.block)} roots := [][32]byte{root} err = s.ReceiveBlockBatch(ctx, blks, roots) if tt.wantedErr != "" { @@ -288,7 +289,7 @@ func TestService_HasInitSyncBlock(t *testing.T) { if s.HasInitSyncBlock(r) { t.Error("Should not have block") } - s.saveInitSyncBlock(r, testutil.NewBeaconBlock()) + s.saveInitSyncBlock(r, interfaces.NewWrappedSignedBeaconBlock(testutil.NewBeaconBlock())) if !s.HasInitSyncBlock(r) { t.Error("Should have block") } diff --git a/beacon-chain/blockchain/service_test.go b/beacon-chain/blockchain/service_test.go index 236b185db452..f3ca23cde324 100644 --- a/beacon-chain/blockchain/service_test.go +++ b/beacon-chain/blockchain/service_test.go @@ -28,6 +28,7 @@ import ( pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/event" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -134,7 +135,7 @@ func TestChainStartStop_Initialized(t *testing.T) { genesisBlk := testutil.NewBeaconBlock() blkRoot, err := genesisBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, genesisBlk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesisBlk))) s, err := testutil.NewBeaconState() require.NoError(t, err) require.NoError(t, s.SetSlot(1)) @@ -164,7 +165,7 @@ func TestChainStartStop_GenesisZeroHashes(t *testing.T) { genesisBlk := testutil.NewBeaconBlock() blkRoot, err := genesisBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, genesisBlk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesisBlk))) s, err := testutil.NewBeaconState() require.NoError(t, err) require.NoError(t, beaconDB.SaveState(ctx, s, blkRoot)) @@ -233,7 +234,7 @@ func TestChainService_CorrectGenesisRoots(t *testing.T) { genesisBlk := testutil.NewBeaconBlock() blkRoot, err := genesisBlk.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, genesisBlk)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesisBlk))) s, err := testutil.NewBeaconState() require.NoError(t, err) require.NoError(t, s.SetSlot(0)) @@ -260,7 +261,7 @@ func TestChainService_InitializeChainInfo(t *testing.T) { genesisRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, genesisRoot)) - require.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) finalizedSlot := params.BeaconConfig().SlotsPerEpoch*2 + 1 headBlock := testutil.NewBeaconBlock() @@ -274,7 +275,7 @@ func TestChainService_InitializeChainInfo(t *testing.T) { require.NoError(t, err) require.NoError(t, beaconDB.SaveState(ctx, headState, headRoot)) require.NoError(t, beaconDB.SaveState(ctx, headState, genesisRoot)) - require.NoError(t, beaconDB.SaveBlock(ctx, headBlock)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(headBlock))) require.NoError(t, beaconDB.SaveFinalizedCheckpoint(ctx, ðpb.Checkpoint{Epoch: helpers.SlotToEpoch(finalizedSlot), Root: headRoot[:]})) c := &Service{cfg: &Config{BeaconDB: beaconDB, StateGen: stategen.New(beaconDB)}} require.NoError(t, c.initializeChainInfo(ctx)) @@ -301,7 +302,7 @@ func TestChainService_InitializeChainInfo_SetHeadAtGenesis(t *testing.T) { genesisRoot, err := genesis.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, genesisRoot)) - require.NoError(t, beaconDB.SaveBlock(ctx, genesis)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesis))) finalizedSlot := params.BeaconConfig().SlotsPerEpoch*2 + 1 headBlock := testutil.NewBeaconBlock() @@ -315,7 +316,7 @@ func TestChainService_InitializeChainInfo_SetHeadAtGenesis(t *testing.T) { require.NoError(t, err) require.NoError(t, beaconDB.SaveState(ctx, headState, headRoot)) require.NoError(t, beaconDB.SaveState(ctx, headState, genesisRoot)) - require.NoError(t, beaconDB.SaveBlock(ctx, headBlock)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(headBlock))) c := &Service{cfg: &Config{BeaconDB: beaconDB, StateGen: stategen.New(beaconDB)}} require.NoError(t, c.initializeChainInfo(ctx)) s, err := c.HeadState(ctx) @@ -343,14 +344,14 @@ func TestChainService_InitializeChainInfo_HeadSync(t *testing.T) { genesisRoot, err := genesisBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, genesisRoot)) - require.NoError(t, beaconDB.SaveBlock(ctx, genesisBlock)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(genesisBlock))) finalizedBlock := testutil.NewBeaconBlock() finalizedBlock.Block.Slot = finalizedSlot finalizedBlock.Block.ParentRoot = genesisRoot[:] finalizedRoot, err := finalizedBlock.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, finalizedBlock)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(finalizedBlock))) // Set head slot close to the finalization point, no head sync is triggered. headBlock := testutil.NewBeaconBlock() @@ -358,7 +359,7 @@ func TestChainService_InitializeChainInfo_HeadSync(t *testing.T) { headBlock.Block.ParentRoot = finalizedRoot[:] headRoot, err := headBlock.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, headBlock)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(headBlock))) headState, err := testutil.NewBeaconState() require.NoError(t, err) @@ -391,7 +392,7 @@ func TestChainService_InitializeChainInfo_HeadSync(t *testing.T) { headBlock.Block.ParentRoot = finalizedRoot[:] headRoot, err = headBlock.Block.HashTreeRoot() require.NoError(t, err) - require.NoError(t, beaconDB.SaveBlock(ctx, headBlock)) + require.NoError(t, beaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(headBlock))) require.NoError(t, beaconDB.SaveState(ctx, headState, headRoot)) require.NoError(t, beaconDB.SaveHeadBlockRoot(ctx, headRoot)) @@ -420,7 +421,7 @@ func TestChainService_SaveHeadNoDB(t *testing.T) { newState, err := testutil.NewBeaconState() require.NoError(t, err) require.NoError(t, s.cfg.StateGen.SaveState(ctx, r, newState)) - require.NoError(t, s.saveHeadNoDB(ctx, blk, r, newState)) + require.NoError(t, s.saveHeadNoDB(ctx, interfaces.NewWrappedSignedBeaconBlock(blk), r, newState)) newB, err := s.cfg.BeaconDB.HeadBlock(ctx) require.NoError(t, err) @@ -441,7 +442,7 @@ func TestHasBlock_ForkChoiceAndDB(t *testing.T) { require.NoError(t, err) beaconState, err := testutil.NewBeaconState() require.NoError(t, err) - require.NoError(t, s.insertBlockAndAttestationsToForkChoiceStore(ctx, block.Block, r, beaconState)) + require.NoError(t, s.insertBlockAndAttestationsToForkChoiceStore(ctx, interfaces.NewWrappedSignedBeaconBlock(block).Block(), r, beaconState)) assert.Equal(t, false, s.hasBlock(ctx, [32]byte{}), "Should not have block") assert.Equal(t, true, s.hasBlock(ctx, r), "Should have block") @@ -454,12 +455,12 @@ func TestServiceStop_SaveCachedBlocks(t *testing.T) { cfg: &Config{BeaconDB: beaconDB, StateGen: stategen.New(beaconDB)}, ctx: ctx, cancel: cancel, - initSyncBlocks: make(map[[32]byte]*ethpb.SignedBeaconBlock), + initSyncBlocks: make(map[[32]byte]interfaces.SignedBeaconBlock), } - b := testutil.NewBeaconBlock() - r, err := b.Block.HashTreeRoot() + block := testutil.NewBeaconBlock() + r, err := block.Block.HashTreeRoot() require.NoError(t, err) - s.saveInitSyncBlock(r, b) + s.saveInitSyncBlock(r, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, s.Stop()) require.Equal(t, true, s.cfg.BeaconDB.HasBlock(ctx, r)) } @@ -485,7 +486,7 @@ func BenchmarkHasBlockDB(b *testing.B) { cfg: &Config{BeaconDB: beaconDB}, } block := testutil.NewBeaconBlock() - require.NoError(b, s.cfg.BeaconDB.SaveBlock(ctx, block)) + require.NoError(b, s.cfg.BeaconDB.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block))) r, err := block.Block.HashTreeRoot() require.NoError(b, err) @@ -508,7 +509,7 @@ func BenchmarkHasBlockForkChoiceStore(b *testing.B) { bs := &pb.BeaconState{FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}} beaconState, err := stateV0.InitializeFromProto(bs) require.NoError(b, err) - require.NoError(b, s.insertBlockAndAttestationsToForkChoiceStore(ctx, block.Block, r, beaconState)) + require.NoError(b, s.insertBlockAndAttestationsToForkChoiceStore(ctx, interfaces.NewWrappedSignedBeaconBlock(block).Block(), r, beaconState)) b.ResetTimer() for i := 0; i < b.N; i++ { diff --git a/beacon-chain/blockchain/weak_subjectivity_checks_test.go b/beacon-chain/blockchain/weak_subjectivity_checks_test.go index 391c6bfa2a8b..bdab053247dd 100644 --- a/beacon-chain/blockchain/weak_subjectivity_checks_test.go +++ b/beacon-chain/blockchain/weak_subjectivity_checks_test.go @@ -8,6 +8,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) @@ -17,7 +18,7 @@ func TestService_VerifyWeakSubjectivityRoot(t *testing.T) { b := testutil.NewBeaconBlock() b.Block.Slot = 32 - require.NoError(t, beaconDB.SaveBlock(context.Background(), b)) + require.NoError(t, beaconDB.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(b))) r, err := b.Block.HashTreeRoot() require.NoError(t, err) tests := []struct { diff --git a/beacon-chain/db/kv/BUILD.bazel b/beacon-chain/db/kv/BUILD.bazel index ae459be7f623..05a67a3fac17 100644 --- a/beacon-chain/db/kv/BUILD.bazel +++ b/beacon-chain/db/kv/BUILD.bazel @@ -96,6 +96,7 @@ go_test( "//proto/beacon/p2p/v1:go_default_library", "//proto/testing:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/assert:go_default_library", diff --git a/beacon-chain/sync/initial-sync/BUILD.bazel b/beacon-chain/sync/initial-sync/BUILD.bazel index 8cf04c05c9d2..a4dbf8f9629b 100644 --- a/beacon-chain/sync/initial-sync/BUILD.bazel +++ b/beacon-chain/sync/initial-sync/BUILD.bazel @@ -127,6 +127,7 @@ go_test( "//shared/event:go_default_library", "//shared/featureconfig:go_default_library", "//shared/hashutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/sliceutil:go_default_library", "//shared/testutil:go_default_library", diff --git a/beacon-chain/sync/validate_beacon_attestation_test.go b/beacon-chain/sync/validate_beacon_attestation_test.go index 61ce30383095..fd3396d661c8 100644 --- a/beacon-chain/sync/validate_beacon_attestation_test.go +++ b/beacon-chain/sync/validate_beacon_attestation_test.go @@ -4,9 +4,6 @@ import ( "bytes" "context" "fmt" - "testing" - "time" - lru "github.com/hashicorp/golang-lru" pubsub "github.com/libp2p/go-libp2p-pubsub" pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb" @@ -18,9 +15,12 @@ import ( p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" + "testing" + "time" ) func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { @@ -58,7 +58,7 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { blk := testutil.NewBeaconBlock() blk.Block.Slot = 1 - require.NoError(t, db.SaveBlock(ctx, blk)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(blk))) validBlockRoot, err := blk.Block.HashTreeRoot() require.NoError(t, err) diff --git a/beacon-chain/sync/validate_beacon_blocks_test.go b/beacon-chain/sync/validate_beacon_blocks_test.go index 0ffe68b8c68c..dd96be625435 100644 --- a/beacon-chain/sync/validate_beacon_blocks_test.go +++ b/beacon-chain/sync/validate_beacon_blocks_test.go @@ -27,6 +27,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/abool" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" @@ -90,7 +91,7 @@ func TestValidateBeaconBlockPubSub_BlockAlreadyPresentInDB(t *testing.T) { msg := testutil.NewBeaconBlock() msg.Block.Slot = 100 msg.Block.ParentRoot = testutil.Random32Bytes(t) - require.NoError(t, db.SaveBlock(context.Background(), msg)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(msg))) c, err := lru.New(10) require.NoError(t, err) @@ -130,7 +131,7 @@ func TestValidateBeaconBlockPubSub_CanRecoverStateSummary(t *testing.T) { ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) parentBlock := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, parentBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(parentBlock))) bRoot, err := parentBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveState(ctx, beaconState, bRoot)) @@ -192,7 +193,7 @@ func TestValidateBeaconBlockPubSub_ValidProposerSignature(t *testing.T) { ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) parentBlock := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, parentBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(parentBlock))) bRoot, err := parentBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveState(ctx, beaconState, bRoot)) @@ -255,7 +256,7 @@ func TestValidateBeaconBlockPubSub_WithLookahead(t *testing.T) { ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) parentBlock := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, parentBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(parentBlock))) bRoot, err := parentBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveState(ctx, beaconState, bRoot)) @@ -320,7 +321,7 @@ func TestValidateBeaconBlockPubSub_AdvanceEpochsForState(t *testing.T) { ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) parentBlock := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, parentBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(parentBlock))) bRoot, err := parentBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveState(ctx, beaconState, bRoot)) @@ -522,7 +523,7 @@ func TestValidateBeaconBlockPubSub_SeenProposerSlot(t *testing.T) { ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) parentBlock := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, parentBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(parentBlock))) bRoot, err := parentBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveState(ctx, beaconState, bRoot)) @@ -583,7 +584,7 @@ func TestValidateBeaconBlockPubSub_FilterByFinalizedEpoch(t *testing.T) { p := p2ptest.NewTestP2P(t) parent := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(context.Background(), parent)) + require.NoError(t, db.SaveBlock(context.Background(), interfaces.NewWrappedSignedBeaconBlock(parent))) parentRoot, err := parent.Block.HashTreeRoot() require.NoError(t, err) chain := &mock.ChainService{Genesis: time.Unix(time.Now().Unix()-int64(params.BeaconConfig().SecondsPerSlot), 0), @@ -645,7 +646,7 @@ func TestValidateBeaconBlockPubSub_ParentNotFinalizedDescendant(t *testing.T) { ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) parentBlock := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, parentBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(parentBlock))) bRoot, err := parentBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveState(ctx, beaconState, bRoot)) @@ -708,7 +709,7 @@ func TestValidateBeaconBlockPubSub_InvalidParentBlock(t *testing.T) { ctx := context.Background() beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) parentBlock := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, parentBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(parentBlock))) bRoot, err := parentBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveState(ctx, beaconState, bRoot)) @@ -797,7 +798,7 @@ func TestValidateBeaconBlockPubSub_RejectEvilBlocksFromFuture(t *testing.T) { beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) parentBlock := testutil.NewBeaconBlock() - require.NoError(t, db.SaveBlock(ctx, parentBlock)) + require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(parentBlock))) bRoot, err := parentBlock.Block.HashTreeRoot() require.NoError(t, err) require.NoError(t, db.SaveState(ctx, beaconState, bRoot)) diff --git a/shared/testutil/block_test.go b/shared/testutil/block_test.go index 17a2f6b7c641..81fa705a3b71 100644 --- a/shared/testutil/block_test.go +++ b/shared/testutil/block_test.go @@ -11,6 +11,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) @@ -22,7 +23,7 @@ func TestGenerateFullBlock_PassesStateTransition(t *testing.T) { } block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot()) require.NoError(t, err) - _, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + _, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) } @@ -35,7 +36,7 @@ func TestGenerateFullBlock_ThousandValidators(t *testing.T) { } block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot()) require.NoError(t, err) - _, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + _, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) } @@ -53,7 +54,7 @@ func TestGenerateFullBlock_Passes4Epochs(t *testing.T) { helpers.ClearCache() block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot()) require.NoError(t, err) - beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) } @@ -78,7 +79,7 @@ func TestGenerateFullBlock_ValidProposerSlashings(t *testing.T) { } block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot()+1) require.NoError(t, err) - beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) slashableIndice := block.Block.Body.ProposerSlashings[0].Header_1.Header.ProposerIndex @@ -97,7 +98,7 @@ func TestGenerateFullBlock_ValidAttesterSlashings(t *testing.T) { } block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot()) require.NoError(t, err) - beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) slashableIndices := block.Block.Body.AttesterSlashings[0].Attestation_1.AttestingIndices @@ -117,7 +118,7 @@ func TestGenerateFullBlock_ValidAttestations(t *testing.T) { } block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot()) require.NoError(t, err) - beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) atts, err := beaconState.CurrentEpochAttestations() require.NoError(t, err) @@ -138,7 +139,7 @@ func TestGenerateFullBlock_ValidDeposits(t *testing.T) { } block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot()) require.NoError(t, err) - beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) depositedPubkey := block.Block.Body.Deposits[0].Data.PublicKey @@ -164,7 +165,7 @@ func TestGenerateFullBlock_ValidVoluntaryExits(t *testing.T) { } block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot()) require.NoError(t, err) - beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) exitedIndex := block.Block.Body.VoluntaryExits[0].Exit.ValidatorIndex From bd6dcce4a909810a3112c44e6f61962a8aca6b20 Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 25 May 2021 23:47:54 +0800 Subject: [PATCH 21/44] finally --- beacon-chain/blockchain/BUILD.bazel | 1 - shared/testutil/BUILD.bazel | 1 + spectest/shared/phase0/operations/BUILD.bazel | 1 + spectest/shared/phase0/operations/attester_slashing.go | 5 +++-- spectest/shared/phase0/operations/deposit.go | 5 +++-- spectest/shared/phase0/operations/helpers.go | 5 +++-- spectest/shared/phase0/operations/proposer_slashing.go | 5 +++-- spectest/shared/phase0/operations/voluntary_exit.go | 5 +++-- spectest/shared/phase0/sanity/BUILD.bazel | 1 + spectest/shared/phase0/sanity/block_processing.go | 3 ++- tools/benchmark-files-gen/BUILD.bazel | 1 + tools/benchmark-files-gen/main.go | 9 +++++---- tools/blocktree/main.go | 6 +++--- tools/pcli/BUILD.bazel | 1 + tools/pcli/main.go | 3 ++- 15 files changed, 32 insertions(+), 20 deletions(-) diff --git a/beacon-chain/blockchain/BUILD.bazel b/beacon-chain/blockchain/BUILD.bazel index 25373b1870d4..019bd0d68ac9 100644 --- a/beacon-chain/blockchain/BUILD.bazel +++ b/beacon-chain/blockchain/BUILD.bazel @@ -42,7 +42,6 @@ go_library( "//beacon-chain/p2p:go_default_library", "//beacon-chain/powchain:go_default_library", "//beacon-chain/state/interface:go_default_library", - "//beacon-chain/state/stateV0:go_default_library", "//beacon-chain/state/stategen:go_default_library", "//cmd/beacon-chain/flags:go_default_library", "//proto/beacon/p2p/v1:go_default_library", diff --git a/shared/testutil/BUILD.bazel b/shared/testutil/BUILD.bazel index da361160b40e..a3726d25b4f8 100644 --- a/shared/testutil/BUILD.bazel +++ b/shared/testutil/BUILD.bazel @@ -56,6 +56,7 @@ go_test( "//beacon-chain/core/state/stateutils:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/params:go_default_library", "//shared/testutil/assert:go_default_library", "//shared/testutil/require:go_default_library", diff --git a/spectest/shared/phase0/operations/BUILD.bazel b/spectest/shared/phase0/operations/BUILD.bazel index f8801fec5048..53e394663dbf 100644 --- a/spectest/shared/phase0/operations/BUILD.bazel +++ b/spectest/shared/phase0/operations/BUILD.bazel @@ -21,6 +21,7 @@ go_library( "//beacon-chain/state/interface:go_default_library", "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", + "//shared/interfaces:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/require:go_default_library", "//spectest/utils:go_default_library", diff --git a/spectest/shared/phase0/operations/attester_slashing.go b/spectest/shared/phase0/operations/attester_slashing.go index c13f76f8ee15..5c5382ce872b 100644 --- a/spectest/shared/phase0/operations/attester_slashing.go +++ b/spectest/shared/phase0/operations/attester_slashing.go @@ -10,6 +10,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" "github.com/prysmaticlabs/prysm/spectest/utils" @@ -30,8 +31,8 @@ func RunAttesterSlashingTest(t *testing.T, config string) { require.NoError(t, attSlashing.UnmarshalSSZ(attSlashingSSZ), "Failed to unmarshal") body := ðpb.BeaconBlockBody{AttesterSlashings: []*ethpb.AttesterSlashing{attSlashing}} - RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { - return blocks.ProcessAttesterSlashings(ctx, s, b.Block.Body.AttesterSlashings, v.SlashValidator) + RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b interfaces.SignedBeaconBlock) (iface.BeaconState, error) { + return blocks.ProcessAttesterSlashings(ctx, s, b.Block().Body().AttesterSlashings(), v.SlashValidator) }) }) } diff --git a/spectest/shared/phase0/operations/deposit.go b/spectest/shared/phase0/operations/deposit.go index 0f822716bbe3..baded9fbf858 100644 --- a/spectest/shared/phase0/operations/deposit.go +++ b/spectest/shared/phase0/operations/deposit.go @@ -9,6 +9,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" "github.com/prysmaticlabs/prysm/spectest/utils" @@ -29,8 +30,8 @@ func RunDepositTest(t *testing.T, config string) { require.NoError(t, deposit.UnmarshalSSZ(depositSSZ), "Failed to unmarshal") body := ðpb.BeaconBlockBody{Deposits: []*ethpb.Deposit{deposit}} - processDepositsFunc := func(ctx context.Context, s iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { - return blocks.ProcessDeposits(ctx, s, b.Block.Body.Deposits) + processDepositsFunc := func(ctx context.Context, s iface.BeaconState, b interfaces.SignedBeaconBlock) (iface.BeaconState, error) { + return blocks.ProcessDeposits(ctx, s, b.Block().Body().Deposits()) } RunBlockOperationTest(t, folderPath, body, processDepositsFunc) }) diff --git a/spectest/shared/phase0/operations/helpers.go b/spectest/shared/phase0/operations/helpers.go index 65d818c2d259..93457c6ae739 100644 --- a/spectest/shared/phase0/operations/helpers.go +++ b/spectest/shared/phase0/operations/helpers.go @@ -14,13 +14,14 @@ import ( iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" "google.golang.org/protobuf/proto" "gopkg.in/d4l3k/messagediff.v1" ) -type blockOperation func(context.Context, iface.BeaconState, *ethpb.SignedBeaconBlock) (iface.BeaconState, error) +type blockOperation func(context.Context, iface.BeaconState, interfaces.SignedBeaconBlock) (iface.BeaconState, error) // RunBlockOperationTest takes in the prestate and the beacon block body, processes it through the // passed in block operation function and checks the post state with the expected post state. @@ -53,7 +54,7 @@ func RunBlockOperationTest( helpers.ClearCache() b := testutil.NewBeaconBlock() b.Block.Body = body - beaconState, err := operationFn(context.Background(), preState, b) + beaconState, err := operationFn(context.Background(), preState, interfaces.NewWrappedSignedBeaconBlock(b)) if postSSZExists { require.NoError(t, err) diff --git a/spectest/shared/phase0/operations/proposer_slashing.go b/spectest/shared/phase0/operations/proposer_slashing.go index e1a8fdcb19b8..187cf505987e 100644 --- a/spectest/shared/phase0/operations/proposer_slashing.go +++ b/spectest/shared/phase0/operations/proposer_slashing.go @@ -10,6 +10,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" "github.com/prysmaticlabs/prysm/spectest/utils" @@ -30,8 +31,8 @@ func RunProposerSlashingTest(t *testing.T, config string) { require.NoError(t, proposerSlashing.UnmarshalSSZ(proposerSlashingSSZ), "Failed to unmarshal") body := ðpb.BeaconBlockBody{ProposerSlashings: []*ethpb.ProposerSlashing{proposerSlashing}} - RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { - return blocks.ProcessProposerSlashings(ctx, s, b.Block.Body.ProposerSlashings, v.SlashValidator) + RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b interfaces.SignedBeaconBlock) (iface.BeaconState, error) { + return blocks.ProcessProposerSlashings(ctx, s, b.Block().Body().ProposerSlashings(), v.SlashValidator) }) }) } diff --git a/spectest/shared/phase0/operations/voluntary_exit.go b/spectest/shared/phase0/operations/voluntary_exit.go index 8a16396532b1..d43449d4a7b1 100644 --- a/spectest/shared/phase0/operations/voluntary_exit.go +++ b/spectest/shared/phase0/operations/voluntary_exit.go @@ -9,6 +9,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" "github.com/prysmaticlabs/prysm/spectest/utils" @@ -29,8 +30,8 @@ func RunVoluntaryExitTest(t *testing.T, config string) { require.NoError(t, voluntaryExit.UnmarshalSSZ(exitSSZ), "Failed to unmarshal") body := ðpb.BeaconBlockBody{VoluntaryExits: []*ethpb.SignedVoluntaryExit{voluntaryExit}} - RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) { - return blocks.ProcessVoluntaryExits(ctx, s, b.Block.Body.VoluntaryExits) + RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b interfaces.SignedBeaconBlock) (iface.BeaconState, error) { + return blocks.ProcessVoluntaryExits(ctx, s, b.Block().Body().VoluntaryExits()) }) }) } diff --git a/spectest/shared/phase0/sanity/BUILD.bazel b/spectest/shared/phase0/sanity/BUILD.bazel index b5b121d3f6ee..b10e58fb8e2b 100644 --- a/spectest/shared/phase0/sanity/BUILD.bazel +++ b/spectest/shared/phase0/sanity/BUILD.bazel @@ -16,6 +16,7 @@ go_library( "//beacon-chain/state/interface:go_default_library", "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", + "//shared/interfaces:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/require:go_default_library", "//spectest/utils:go_default_library", diff --git a/spectest/shared/phase0/sanity/block_processing.go b/spectest/shared/phase0/sanity/block_processing.go index 68173efc4772..8372177e1158 100644 --- a/spectest/shared/phase0/sanity/block_processing.go +++ b/spectest/shared/phase0/sanity/block_processing.go @@ -16,6 +16,7 @@ import ( iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" "github.com/prysmaticlabs/prysm/spectest/utils" @@ -61,7 +62,7 @@ func RunBlockProcessingTest(t *testing.T, config string) { require.NoError(t, err, "Failed to decompress") block := ðpb.SignedBeaconBlock{} require.NoError(t, block.UnmarshalSSZ(blockSSZ), "Failed to unmarshal") - processedState, transitionError = state.ExecuteStateTransition(context.Background(), beaconState, block) + processedState, transitionError = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) if transitionError != nil { break } diff --git a/tools/benchmark-files-gen/BUILD.bazel b/tools/benchmark-files-gen/BUILD.bazel index 7324538eea9c..3021600cd02e 100644 --- a/tools/benchmark-files-gen/BUILD.bazel +++ b/tools/benchmark-files-gen/BUILD.bazel @@ -15,6 +15,7 @@ go_library( "//proto/beacon/p2p/v1:go_default_library", "//shared/benchutil:go_default_library", "//shared/fileutil:go_default_library", + "//shared/interfaces:go_default_library", "//shared/interop:go_default_library", "//shared/params:go_default_library", "//shared/testutil:go_default_library", diff --git a/tools/benchmark-files-gen/main.go b/tools/benchmark-files-gen/main.go index f8ad43544941..fd57879149c5 100644 --- a/tools/benchmark-files-gen/main.go +++ b/tools/benchmark-files-gen/main.go @@ -18,6 +18,7 @@ import ( pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/benchutil" "github.com/prysmaticlabs/prysm/shared/fileutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/interop" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -102,7 +103,7 @@ func generateMarshalledFullStateAndBlock() error { if err != nil { return err } - beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) if err != nil { return err } @@ -126,7 +127,7 @@ func generateMarshalledFullStateAndBlock() error { } block.Block.Body.Attestations = append(atts, block.Block.Body.Attestations...) - s, err := state.CalculateStateRoot(context.Background(), beaconState, block) + s, err := state.CalculateStateRoot(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) if err != nil { return errors.Wrap(err, "could not calculate state root") } @@ -157,7 +158,7 @@ func generateMarshalledFullStateAndBlock() error { } // Running a single state transition to make sure the generated files aren't broken. - _, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + _, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) if err != nil { return err } @@ -191,7 +192,7 @@ func generate2FullEpochState() error { if err != nil { return err } - beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) if err != nil { return err } diff --git a/tools/blocktree/main.go b/tools/blocktree/main.go index 8a192622cfa3..87481bfdeb46 100644 --- a/tools/blocktree/main.go +++ b/tools/blocktree/main.go @@ -67,7 +67,7 @@ func main() { if err != nil { panic(err) } - slot := b.Block.Slot + slot := b.Block().Slot() // If the state is not available, roll back for state == nil { slot-- @@ -83,11 +83,11 @@ func main() { // Construct label of each node. rStr := hex.EncodeToString(r[:2]) - label := "slot: " + strconv.Itoa(int(b.Block.Slot)) + "\n root: " + rStr + label := "slot: " + strconv.Itoa(int(b.Block().Slot())) + "\n root: " + rStr dotN := graph.Node(rStr).Box().Attr("label", label) n := &node{ - parentRoot: bytesutil.ToBytes32(b.Block.ParentRoot), + parentRoot: bytesutil.ToBytes32(b.Block().ParentRoot()), dothNode: &dotN, } m[r] = n diff --git a/tools/pcli/BUILD.bazel b/tools/pcli/BUILD.bazel index fac27d5ae766..9299c2d45da8 100644 --- a/tools/pcli/BUILD.bazel +++ b/tools/pcli/BUILD.bazel @@ -13,6 +13,7 @@ go_library( "//beacon-chain/core/state:go_default_library", "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", + "//shared/interfaces:go_default_library", "//shared/sszutil:go_default_library", "//shared/version:go_default_library", "@com_github_ferranbt_fastssz//:go_default_library", diff --git a/tools/pcli/main.go b/tools/pcli/main.go index 7a8dd11148a2..44518c48f340 100644 --- a/tools/pcli/main.go +++ b/tools/pcli/main.go @@ -15,6 +15,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/sszutil" "github.com/prysmaticlabs/prysm/shared/version" log "github.com/sirupsen/logrus" @@ -175,7 +176,7 @@ func main() { blkRoot, preStateRoot, ) - postState, err := state.ExecuteStateTransition(context.Background(), stateObj, block) + postState, err := state.ExecuteStateTransition(context.Background(), stateObj, interfaces.NewWrappedSignedBeaconBlock(block)) if err != nil { log.Fatal(err) } From cf97730b0d917b9f8e522d7bc35b0b5467741625 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 00:17:54 +0800 Subject: [PATCH 22/44] comments --- shared/interfaces/block_interface.go | 7 +++++ shared/interfaces/block_wrapper.go | 45 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/shared/interfaces/block_interface.go b/shared/interfaces/block_interface.go index 334bd5ba8b91..5622b5f51508 100644 --- a/shared/interfaces/block_interface.go +++ b/shared/interfaces/block_interface.go @@ -6,6 +6,8 @@ import ( "google.golang.org/protobuf/proto" ) +// SignedBeaconBlock is an interface describing the method set of +// a signed block. type SignedBeaconBlock interface { Block() BeaconBlock Signature() []byte @@ -16,6 +18,8 @@ type SignedBeaconBlock interface { RawPhase0Block() (*ethpb.SignedBeaconBlock, error) } +// BeaconBlock describes an interface which states the methods +// employed by an object that is a Beacon Block. type BeaconBlock interface { Slot() types.Slot ProposerIndex() types.ValidatorIndex @@ -27,6 +31,9 @@ type BeaconBlock interface { MarshalSSZ() ([]byte, error) Proto() proto.Message } + +// BeaconBlockBody describes the method set employed by an object +// that is a beacon block body. type BeaconBlockBody interface { RandaoReveal() []byte Eth1Data() *ethpb.Eth1Data diff --git a/shared/interfaces/block_wrapper.go b/shared/interfaces/block_wrapper.go index 1a3e4bc3d023..dd037853432c 100644 --- a/shared/interfaces/block_wrapper.go +++ b/shared/interfaces/block_wrapper.go @@ -7,134 +7,179 @@ import ( "google.golang.org/protobuf/proto" ) +// WrappedSignedBeaconBlock is a convenience wrapper around a phase 0 beacon block +// object. This wrapper allows us to conform to a common interface so that beacon +// blocks for future forks can also be applied across prysm without issues. type WrappedSignedBeaconBlock struct { b *ethpb.SignedBeaconBlock } +// NewWrappedSignedBeaconBlock is constructor which wraps a protobuf phase 0 object +// with the block wrapper. func NewWrappedSignedBeaconBlock(b *ethpb.SignedBeaconBlock) WrappedSignedBeaconBlock { return WrappedSignedBeaconBlock{b: b} } +// Signature returns the respective block signature. func (w WrappedSignedBeaconBlock) Signature() []byte { return w.b.Signature } +// Block returns the underlying beacon block object. func (w WrappedSignedBeaconBlock) Block() BeaconBlock { return NewWrappedBeaconBlock(w.b.Block) } +// IsNil checks if the underlying beacon block is +// nil. func (w WrappedSignedBeaconBlock) IsNil() bool { return w.b == nil } +// Copy performs a deep copy of the signed beacon block +// object. func (w WrappedSignedBeaconBlock) Copy() WrappedSignedBeaconBlock { return NewWrappedSignedBeaconBlock(blockutil.CopySignedBeaconBlock(w.b)) } +// MarshalSSZ marshals the signed beacon block to its relevant ssz +// form. func (w WrappedSignedBeaconBlock) MarshalSSZ() ([]byte, error) { return w.b.MarshalSSZ() } +// Proto returns the block in its underlying protobuf +// interface. func (w WrappedSignedBeaconBlock) Proto() proto.Message { return w.b } +// RawPhase0Block returns the underlying protobuf object. func (w WrappedSignedBeaconBlock) RawPhase0Block() (*ethpb.SignedBeaconBlock, error) { return w.b, nil } +// WrappedBeaconBlock is the wrapper for the actual block. type WrappedBeaconBlock struct { b *ethpb.BeaconBlock } +// NewWrappedBeaconBlock is constructor which wraps a protobuf phase 0 object +// with the block wrapper. func NewWrappedBeaconBlock(b *ethpb.BeaconBlock) WrappedBeaconBlock { return WrappedBeaconBlock{b: b} } +// Slot returns the respective slot of the block. func (w WrappedBeaconBlock) Slot() types.Slot { return w.b.Slot } +// ProposerIndex returns proposer index of the beacon block. func (w WrappedBeaconBlock) ProposerIndex() types.ValidatorIndex { return w.b.ProposerIndex } +// ParentRoot returns the parent root of beacon block. func (w WrappedBeaconBlock) ParentRoot() []byte { return w.b.ParentRoot } +// StateRoot returns the state root of the beacon block. func (w WrappedBeaconBlock) StateRoot() []byte { return w.b.StateRoot } +// Body returns the underlying block body. func (w WrappedBeaconBlock) Body() BeaconBlockBody { return NewWrappedBeaconBlockBody(w.b.Body) } +// IsNil checks if the beacon block is nil. func (w WrappedBeaconBlock) IsNil() bool { return w.b == nil } +// HashTreeRoot returns the ssz root of the block. func (w WrappedBeaconBlock) HashTreeRoot() ([32]byte, error) { return w.b.HashTreeRoot() } +// MarshalSSZ marshals the block into its respective +// ssz form. func (w WrappedBeaconBlock) MarshalSSZ() ([]byte, error) { return w.b.MarshalSSZ() } +// Proto returns the underlying block object in its +// proto form. func (w WrappedBeaconBlock) Proto() proto.Message { return w.b } +// WrappedBeaconBlockBody is a wrapper of a beacon block body. type WrappedBeaconBlockBody struct { b *ethpb.BeaconBlockBody } +// NewWrappedBeaconBlockBody is constructor which wraps a protobuf phase 0 object +// with the block wrapper. func NewWrappedBeaconBlockBody(b *ethpb.BeaconBlockBody) WrappedBeaconBlockBody { return WrappedBeaconBlockBody{b: b} } +// RandaoReveal returns the randao reveal from the block body. func (w WrappedBeaconBlockBody) RandaoReveal() []byte { return w.b.RandaoReveal } +// Eth1Data returns the eth1 data in the block. func (w WrappedBeaconBlockBody) Eth1Data() *ethpb.Eth1Data { return w.b.Eth1Data } +// Graffiti returns the graffiti in the block. func (w WrappedBeaconBlockBody) Graffiti() []byte { return w.b.Graffiti } +// ProposerSlashings returns the proposer slashings in the block. func (w WrappedBeaconBlockBody) ProposerSlashings() []*ethpb.ProposerSlashing { return w.b.ProposerSlashings } +// AttesterSlashings returns the attester slashings in the block. func (w WrappedBeaconBlockBody) AttesterSlashings() []*ethpb.AttesterSlashing { return w.b.AttesterSlashings } +// Attestations returns the stored attestations in the block. func (w WrappedBeaconBlockBody) Attestations() []*ethpb.Attestation { return w.b.Attestations } +// Deposits returns the stored deposits in the block. func (w WrappedBeaconBlockBody) Deposits() []*ethpb.Deposit { return w.b.Deposits } +// VoluntaryExits returns the voluntary exits in the block. func (w WrappedBeaconBlockBody) VoluntaryExits() []*ethpb.SignedVoluntaryExit { return w.b.VoluntaryExits } +// IsNIl checks if the block body is nil. func (w WrappedBeaconBlockBody) IsNil() bool { return w.b == nil } +// HashTreeRoot returns the ssz root of the block body. func (w WrappedBeaconBlockBody) HashTreeRoot() ([32]byte, error) { return w.b.HashTreeRoot() } +// Proto returns the underlying proto form of the block +// body. func (w WrappedBeaconBlockBody) Proto() proto.Message { return w.b } From c2919f464f6f55a2e864058ef74799c46106525a Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 00:27:06 +0800 Subject: [PATCH 23/44] fix fuzz --- fuzz/block_fuzz.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fuzz/block_fuzz.go b/fuzz/block_fuzz.go index 46c864e080da..0c29bf48b603 100644 --- a/fuzz/block_fuzz.go +++ b/fuzz/block_fuzz.go @@ -28,6 +28,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" "github.com/prysmaticlabs/prysm/beacon-chain/sync" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/rand" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -70,7 +71,7 @@ func setupDB() { panic(err) } b := testutil.NewBeaconBlock() - if err := db1.SaveBlock(ctx, b); err != nil { + if err := db1.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(b)); err != nil { panic(err) } br, err := b.HashTreeRoot() @@ -195,7 +196,7 @@ func BeaconFuzzBlock(b []byte) { _ = err } - if _, err := state.ProcessBlock(ctx, st, input.Block); err != nil { + if _, err := state.ProcessBlock(ctx, st, interfaces.NewWrappedSignedBeaconBlock(input.Block)); err != nil { _ = err } } From 6d9bee39ae1387f733783da71de01983e41c37b2 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 00:31:24 +0800 Subject: [PATCH 24/44] share common library --- fuzz/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/fuzz/BUILD.bazel b/fuzz/BUILD.bazel index 8cbe6f7e7579..c5de63f45f49 100644 --- a/fuzz/BUILD.bazel +++ b/fuzz/BUILD.bazel @@ -86,6 +86,7 @@ go_fuzz_test( "//beacon-chain/cache:go_default_library", "//beacon-chain/state/stategen:go_default_library", "//shared/rand:go_default_library", + "//shared/interfaces:go_default_library", ] + COMMON_DEPS, ) From 28cd19c5e045d1cc5632f1181ed3bfc9847f7204 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 11:28:03 +0800 Subject: [PATCH 25/44] fix --- beacon-chain/db/kafka/export_wrapper.go | 23 ++++++++++------------- beacon-chain/db/kafka/passthrough.go | 15 ++++++++------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/beacon-chain/db/kafka/export_wrapper.go b/beacon-chain/db/kafka/export_wrapper.go index f0dd2afbab9c..d19cfd53bd16 100644 --- a/beacon-chain/db/kafka/export_wrapper.go +++ b/beacon-chain/db/kafka/export_wrapper.go @@ -3,24 +3,22 @@ package kafka 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" "github.com/prysmaticlabs/prysm/beacon-chain/db/iface" "github.com/prysmaticlabs/prysm/shared/featureconfig" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/traceutil" "go.opencensus.io/trace" + jsonpb "google.golang.org/protobuf/encoding/protojson" "gopkg.in/confluentinc/confluent-kafka-go.v1/kafka" _ "gopkg.in/confluentinc/confluent-kafka-go.v1/kafka/librdkafka" // Required for c++ kafka library. "gopkg.in/errgo.v2/fmt/errors" ) var _ iface.Database = (*Exporter)(nil) -var marshaler = &jsonpb.Marshaler{} +var marshaler = jsonpb.MarshalOptions{} // Exporter wraps a database interface and exports certain objects to kafka topics. type Exporter struct { @@ -35,7 +33,6 @@ func Wrap(db iface.Database) (iface.Database, error) { log.Debug("Empty Kafka bootstrap servers list, database was not wrapped with Kafka exporter") return db, nil } - p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": featureconfig.Get().KafkaBootstrapServers}) if err != nil { return nil, err @@ -44,18 +41,18 @@ func Wrap(db iface.Database) (iface.Database, error) { return &Exporter{db: db, p: p}, nil } -func (e Exporter) publish(ctx context.Context, topic string, msg proto.Message) error { +func (e Exporter) publish(ctx context.Context, topic string, msg interfaces.SignedBeaconBlock) error { ctx, span := trace.StartSpan(ctx, "kafka.publish") defer span.End() - buf := bytes.NewBuffer(nil) - if err := marshaler.Marshal(buf, msg); err != nil { + var err error + var buf []byte + if buf, err = marshaler.Marshal(msg.Proto()); err != nil { traceutil.AnnotateError(span, err) return err } var key [32]byte - var err error if v, ok := msg.(fssz.HashRoot); ok { key, err = v.HashTreeRoot() } else { @@ -70,7 +67,7 @@ func (e Exporter) publish(ctx context.Context, topic string, msg proto.Message) TopicPartition: kafka.TopicPartition{ Topic: &topic, }, - Value: buf.Bytes(), + Value: buf, Key: key[:], }, nil); err != nil { traceutil.AnnotateError(span, err) @@ -86,7 +83,7 @@ func (e Exporter) Close() error { } // SaveBlock publishes to the kafka topic for beacon blocks. -func (e Exporter) SaveBlock(ctx context.Context, block *eth.SignedBeaconBlock) error { +func (e Exporter) SaveBlock(ctx context.Context, block interfaces.SignedBeaconBlock) error { go func() { if err := e.publish(ctx, "beacon_block", block); err != nil { log.WithError(err).Error("Failed to publish block") @@ -97,7 +94,7 @@ func (e Exporter) SaveBlock(ctx context.Context, block *eth.SignedBeaconBlock) e } // SaveBlocks publishes to the kafka topic for beacon blocks. -func (e Exporter) SaveBlocks(ctx context.Context, blocks []*eth.SignedBeaconBlock) error { +func (e Exporter) SaveBlocks(ctx context.Context, blocks []interfaces.SignedBeaconBlock) error { go func() { for _, block := range blocks { if err := e.publish(ctx, "beacon_block", block); err != nil { diff --git a/beacon-chain/db/kafka/passthrough.go b/beacon-chain/db/kafka/passthrough.go index 50b185b97323..a9150073eab5 100644 --- a/beacon-chain/db/kafka/passthrough.go +++ b/beacon-chain/db/kafka/passthrough.go @@ -11,6 +11,7 @@ import ( iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/proto/beacon/db" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" ) // DatabasePath -- passthrough. @@ -29,17 +30,17 @@ func (e Exporter) Backup(ctx context.Context, outputDir string) error { } // Block -- passthrough. -func (e Exporter) Block(ctx context.Context, blockRoot [32]byte) (*eth.SignedBeaconBlock, error) { +func (e Exporter) Block(ctx context.Context, blockRoot [32]byte) (interfaces.SignedBeaconBlock, error) { return e.db.Block(ctx, blockRoot) } // HeadBlock -- passthrough. -func (e Exporter) HeadBlock(ctx context.Context) (*eth.SignedBeaconBlock, error) { +func (e Exporter) HeadBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) { return e.db.HeadBlock(ctx) } // Blocks -- passthrough. -func (e Exporter) Blocks(ctx context.Context, f *filters.QueryFilter) ([]*eth.SignedBeaconBlock, [][32]byte, error) { +func (e Exporter) Blocks(ctx context.Context, f *filters.QueryFilter) ([]interfaces.SignedBeaconBlock, [][32]byte, error) { return e.db.Blocks(ctx, f) } @@ -49,7 +50,7 @@ func (e Exporter) BlockRoots(ctx context.Context, f *filters.QueryFilter) ([][32 } // BlocksBySlot -- passthrough. -func (e Exporter) BlocksBySlot(ctx context.Context, slot types.Slot) (bool, []*eth.SignedBeaconBlock, error) { +func (e Exporter) BlocksBySlot(ctx context.Context, slot types.Slot) (bool, []interfaces.SignedBeaconBlock, error) { return e.db.BlocksBySlot(ctx, slot) } @@ -129,7 +130,7 @@ func (e Exporter) SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) err } // GenesisBlock -- passthrough. -func (e Exporter) GenesisBlock(ctx context.Context) (*eth.SignedBeaconBlock, error) { +func (e Exporter) GenesisBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) { return e.db.GenesisBlock(ctx) } @@ -214,7 +215,7 @@ func (e Exporter) IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool } // FinalizedChildBlock -- passthrough. -func (e Exporter) FinalizedChildBlock(ctx context.Context, blockRoot [32]byte) (*eth.SignedBeaconBlock, error) { +func (e Exporter) FinalizedChildBlock(ctx context.Context, blockRoot [32]byte) (interfaces.SignedBeaconBlock, error) { return e.db.FinalizedChildBlock(ctx, blockRoot) } @@ -244,7 +245,7 @@ func (e Exporter) LastArchivedRoot(ctx context.Context) [32]byte { } // HighestSlotBlocksBelow -- passthrough -func (e Exporter) HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([]*eth.SignedBeaconBlock, error) { +func (e Exporter) HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([]interfaces.SignedBeaconBlock, error) { return e.db.HighestSlotBlocksBelow(ctx, slot) } From 27a91a13a502c12301735e601e9c8fe5f549bb4d Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 11:30:40 +0800 Subject: [PATCH 26/44] fix --- beacon-chain/db/kafka/BUILD.bazel | 4 ++-- spectest/shared/phase0/finality/BUILD.bazel | 1 + spectest/shared/phase0/finality/runner.go | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/beacon-chain/db/kafka/BUILD.bazel b/beacon-chain/db/kafka/BUILD.bazel index cc109db1f73e..b5447db3d70e 100644 --- a/beacon-chain/db/kafka/BUILD.bazel +++ b/beacon-chain/db/kafka/BUILD.bazel @@ -17,11 +17,10 @@ go_library( "//proto/beacon/db:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/featureconfig:go_default_library", + "//shared/interfaces: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_eth2_types//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", @@ -29,5 +28,6 @@ go_library( "@in_gopkg_confluentinc_confluent_kafka_go_v1//kafka/librdkafka:go_default_library", "@in_gopkg_errgo_v2//fmt/errors:go_default_library", "@io_opencensus_go//trace:go_default_library", + "@org_golang_google_protobuf//encoding/protojson:go_default_library", ], ) diff --git a/spectest/shared/phase0/finality/BUILD.bazel b/spectest/shared/phase0/finality/BUILD.bazel index a99e98ec8dd3..1342fd75f74f 100644 --- a/spectest/shared/phase0/finality/BUILD.bazel +++ b/spectest/shared/phase0/finality/BUILD.bazel @@ -12,6 +12,7 @@ go_library( "//beacon-chain/state/interface:go_default_library", "//beacon-chain/state/stateV0:go_default_library", "//proto/beacon/p2p/v1:go_default_library", + "//shared/interfaces:go_default_library", "//shared/testutil:go_default_library", "//shared/testutil/require:go_default_library", "//spectest/utils:go_default_library", diff --git a/spectest/shared/phase0/finality/runner.go b/spectest/shared/phase0/finality/runner.go index 1693d51845f4..78cfa45b9f84 100644 --- a/spectest/shared/phase0/finality/runner.go +++ b/spectest/shared/phase0/finality/runner.go @@ -12,6 +12,7 @@ import ( iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/require" "github.com/prysmaticlabs/prysm/spectest/utils" @@ -60,7 +61,7 @@ func RunFinalityTest(t *testing.T, config string) { require.NoError(t, err, "Failed to decompress") block := ðpb.SignedBeaconBlock{} require.NoError(t, block.UnmarshalSSZ(blockSSZ), "Failed to unmarshal") - processedState, err = state.ExecuteStateTransition(context.Background(), beaconState, block) + processedState, err = state.ExecuteStateTransition(context.Background(), beaconState, interfaces.NewWrappedSignedBeaconBlock(block)) require.NoError(t, err) beaconState, ok = processedState.(*stateV0.BeaconState) require.Equal(t, true, ok) From 0c6ad93d8ee4c743236bd46eac5e6c1560a38d44 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 13:43:55 +0800 Subject: [PATCH 27/44] add in more defensive nil checks --- beacon-chain/blockchain/chain_info.go | 2 +- beacon-chain/blockchain/head.go | 2 +- beacon-chain/blockchain/process_attestation_helpers.go | 2 +- beacon-chain/blockchain/process_block.go | 4 ++-- beacon-chain/blockchain/process_block_helpers.go | 8 ++++---- beacon-chain/blockchain/service.go | 4 ++-- beacon-chain/core/helpers/block.go | 2 +- beacon-chain/core/state/transition.go | 2 +- beacon-chain/core/state/transition_no_verify_sig.go | 4 ++-- beacon-chain/db/kv/backup.go | 2 +- beacon-chain/db/kv/blocks.go | 2 +- beacon-chain/db/kv/finalized_block_roots.go | 2 +- beacon-chain/db/kv/genesis.go | 2 +- beacon-chain/rpc/beacon/blocks.go | 6 +++--- beacon-chain/rpc/beaconv1/blocks.go | 10 +++++----- beacon-chain/rpc/beaconv1/state.go | 2 +- beacon-chain/rpc/debug/block.go | 2 +- beacon-chain/state/stategen/getter.go | 4 ++-- beacon-chain/state/stategen/replay.go | 4 ++-- beacon-chain/sync/initial-sync/blocks_queue_utils.go | 2 +- beacon-chain/sync/pending_blocks_queue.go | 2 +- beacon-chain/sync/rpc_beacon_blocks_by_range.go | 2 +- beacon-chain/sync/rpc_beacon_blocks_by_root.go | 2 +- beacon-chain/sync/rpc_status.go | 4 ++-- beacon-chain/sync/validate_beacon_blocks.go | 2 +- 25 files changed, 40 insertions(+), 40 deletions(-) diff --git a/beacon-chain/blockchain/chain_info.go b/beacon-chain/blockchain/chain_info.go index e377b4cdcd3d..8b82c4f2326b 100644 --- a/beacon-chain/blockchain/chain_info.go +++ b/beacon-chain/blockchain/chain_info.go @@ -124,7 +124,7 @@ func (s *Service) HeadRoot(ctx context.Context) ([]byte, error) { if err != nil { return nil, err } - if b.IsNil() { + if b == nil || b.IsNil() { return params.BeaconConfig().ZeroHash[:], nil } diff --git a/beacon-chain/blockchain/head.go b/beacon-chain/blockchain/head.go index 64d42e22313b..144a009724df 100644 --- a/beacon-chain/blockchain/head.go +++ b/beacon-chain/blockchain/head.go @@ -103,7 +103,7 @@ func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error { if err != nil { return err } - if newHeadBlock.IsNil() || newHeadBlock.Block().IsNil() { + if newHeadBlock == nil || newHeadBlock.IsNil() || newHeadBlock.Block().IsNil() { return errors.New("cannot save nil head block") } diff --git a/beacon-chain/blockchain/process_attestation_helpers.go b/beacon-chain/blockchain/process_attestation_helpers.go index f46e162d157a..acf3ac4cdd77 100644 --- a/beacon-chain/blockchain/process_attestation_helpers.go +++ b/beacon-chain/blockchain/process_attestation_helpers.go @@ -99,7 +99,7 @@ func (s *Service) verifyBeaconBlock(ctx context.Context, data *ethpb.Attestation } // If the block does not exist in db, check again if block exists in initial sync block cache. // This could happen as the node first syncs to head. - if b.IsNil() && s.hasInitSyncBlock(r) { + if (b == nil || b.IsNil()) && s.hasInitSyncBlock(r) { b = s.getInitSyncBlock(r) } if err := helpers.VerifyNilBeaconBlock(b); err != nil { diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 2580425f5fdd..307d9f0be2a0 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -86,7 +86,7 @@ func (s *Service) onBlock(ctx context.Context, signed interfaces.SignedBeaconBlo ctx, span := trace.StartSpan(ctx, "blockChain.onBlock") defer span.End() - if signed.IsNil() || signed.Block().IsNil() { + if signed == nil || signed.IsNil() || signed.Block().IsNil() { return errors.New("nil block") } b := signed.Block() @@ -191,7 +191,7 @@ func (s *Service) onBlockBatch(ctx context.Context, blks []interfaces.SignedBeac if len(blks) == 0 || len(blockRoots) == 0 { return nil, nil, errors.New("no blocks provided") } - if blks[0].IsNil() || blks[0].Block().IsNil() { + if blks[0] == nil || blks[0].IsNil() || blks[0].Block().IsNil() { return nil, nil, errors.New("nil block") } b := blks[0].Block() diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index defe0a42c0cd..7ecf0de1eec9 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -97,7 +97,7 @@ func (s *Service) VerifyBlkDescendant(ctx context.Context, root [32]byte) error if err != nil { return err } - if finalizedBlkSigned.IsNil() || finalizedBlkSigned.Block().IsNil() { + if finalizedBlkSigned == nil || finalizedBlkSigned.IsNil() || finalizedBlkSigned.Block().IsNil() { return errors.New("nil finalized block") } finalizedBlk := finalizedBlkSigned.Block() @@ -151,7 +151,7 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified return false, err } } - if newJustifiedBlockSigned.IsNil() || newJustifiedBlockSigned.Block().IsNil() { + if newJustifiedBlockSigned == nil || newJustifiedBlockSigned.IsNil() || newJustifiedBlockSigned.Block().IsNil() { return false, errors.New("nil new justified block") } @@ -174,7 +174,7 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified } } - if justifiedBlockSigned.IsNil() || justifiedBlockSigned.Block().IsNil() { + if justifiedBlockSigned == nil || justifiedBlockSigned.IsNil() || justifiedBlockSigned.Block().IsNil() { return false, errors.New("nil justified block") } justifiedBlock := justifiedBlockSigned.Block() @@ -308,7 +308,7 @@ func (s *Service) ancestorByDB(ctx context.Context, r [32]byte, slot types.Slot) signed = s.getInitSyncBlock(r) } - if signed.IsNil() || signed.Block().IsNil() { + if signed == nil || signed.IsNil() || signed.Block().IsNil() { return nil, errors.New("nil block") } b := signed.Block() diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index df2323f3bcfd..0c7790860aa3 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -120,7 +120,7 @@ func (s *Service) Start() { if err != nil { log.Fatalf("Could not fetch finalized cp: %v", err) } - if !genesisBlock.IsNil() { + if genesisBlock != nil && !genesisBlock.IsNil() { r, err = genesisBlock.Block().HashTreeRoot() if err != nil { log.Fatalf("Could not tree hash genesis block: %v", err) @@ -381,7 +381,7 @@ func (s *Service) initializeChainInfo(ctx context.Context) error { if err != nil { return errors.Wrap(err, "could not get genesis block from db") } - if genesisBlock.IsNil() { + if genesisBlock == nil || genesisBlock.IsNil() { return errors.New("no genesis block in db") } genesisBlkRoot, err := genesisBlock.Block().HashTreeRoot() diff --git a/beacon-chain/core/helpers/block.go b/beacon-chain/core/helpers/block.go index 4dff27f8030d..8441b3217cdc 100644 --- a/beacon-chain/core/helpers/block.go +++ b/beacon-chain/core/helpers/block.go @@ -15,7 +15,7 @@ import ( // Access to these nil fields will result in run time panic, // it is recommended to run these checks as first line of defense. func VerifyNilBeaconBlock(b interfaces.SignedBeaconBlock) error { - if b.IsNil() { + if b == nil || b.IsNil() { return errors.New("signed beacon block can't be nil") } if b.Block().IsNil() { diff --git a/beacon-chain/core/state/transition.go b/beacon-chain/core/state/transition.go index 5c245b904705..dbeeb1bd7f6f 100644 --- a/beacon-chain/core/state/transition.go +++ b/beacon-chain/core/state/transition.go @@ -87,7 +87,7 @@ func ExecuteStateTransition( if ctx.Err() != nil { return nil, ctx.Err() } - if signed.IsNil() || signed.Block().IsNil() { + if signed == nil || signed.IsNil() || signed.Block().IsNil() { return nil, errors.New("nil block") } diff --git a/beacon-chain/core/state/transition_no_verify_sig.go b/beacon-chain/core/state/transition_no_verify_sig.go index 7d06112cbfe0..f8b2b002a8a9 100644 --- a/beacon-chain/core/state/transition_no_verify_sig.go +++ b/beacon-chain/core/state/transition_no_verify_sig.go @@ -48,7 +48,7 @@ func ExecuteStateTransitionNoVerifyAnySig( if ctx.Err() != nil { return nil, nil, ctx.Err() } - if signed.IsNil() || signed.Block().IsNil() { + if signed == nil || signed.IsNil() || signed.Block().IsNil() { return nil, nil, errors.New("nil block") } @@ -125,7 +125,7 @@ func CalculateStateRoot( if state == nil || state.IsNil() { return [32]byte{}, errors.New("nil state") } - if signed.IsNil() || signed.Block().IsNil() { + if signed == nil || signed.IsNil() || signed.Block().IsNil() { return [32]byte{}, errors.New("nil block") } diff --git a/beacon-chain/db/kv/backup.go b/beacon-chain/db/kv/backup.go index 974948509bab..717ab50179eb 100644 --- a/beacon-chain/db/kv/backup.go +++ b/beacon-chain/db/kv/backup.go @@ -34,7 +34,7 @@ func (s *Store) Backup(ctx context.Context, outputDir string) error { if err != nil { return err } - if head.IsNil() { + if head == nil || head.IsNil() { return errors.New("no head block") } // Ensure the backups directory exists. diff --git a/beacon-chain/db/kv/blocks.go b/beacon-chain/db/kv/blocks.go index 3a9fde5a4a9e..4ec167272af0 100644 --- a/beacon-chain/db/kv/blocks.go +++ b/beacon-chain/db/kv/blocks.go @@ -367,7 +367,7 @@ func (s *Store) HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([] return nil, err } } - if blk.IsNil() { + if blk == nil || blk.IsNil() { blk, err = s.GenesisBlock(ctx) if err != nil { return nil, err diff --git a/beacon-chain/db/kv/finalized_block_roots.go b/beacon-chain/db/kv/finalized_block_roots.go index 690cd995a6c8..d31b0c2557d6 100644 --- a/beacon-chain/db/kv/finalized_block_roots.go +++ b/beacon-chain/db/kv/finalized_block_roots.go @@ -85,7 +85,7 @@ func (s *Store) updateFinalizedBlockRoots(ctx context.Context, tx *bolt.Tx, chec traceutil.AnnotateError(span, err) return err } - if signedBlock.IsNil() || signedBlock.Block().IsNil() { + if signedBlock == nil || signedBlock.IsNil() || signedBlock.Block().IsNil() { err := fmt.Errorf("missing block in database: block root=%#x", root) traceutil.AnnotateError(span, err) return err diff --git a/beacon-chain/db/kv/genesis.go b/beacon-chain/db/kv/genesis.go index 112b8f765e32..6dd70a6c0e8d 100644 --- a/beacon-chain/db/kv/genesis.go +++ b/beacon-chain/db/kv/genesis.go @@ -103,7 +103,7 @@ func (s *Store) EnsureEmbeddedGenesis(ctx context.Context) error { if err != nil { return err } - if !gb.IsNil() { + if gb != nil && !gb.IsNil() { return nil } gs, err := s.GenesisState(ctx) diff --git a/beacon-chain/rpc/beacon/blocks.go b/beacon-chain/rpc/beacon/blocks.go index 368211741ca7..873ae6ed8a17 100644 --- a/beacon-chain/rpc/beacon/blocks.go +++ b/beacon-chain/rpc/beacon/blocks.go @@ -88,7 +88,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not retrieve block: %v", err) } - if blk.IsNil() { + if blk == nil || blk.IsNil() { return ðpb.ListBlocksResponse{ BlockContainers: make([]*ethpb.BeaconBlockContainer, 0), TotalSize: 0, @@ -309,7 +309,7 @@ func (bs *Server) chainHeadRetrieval(ctx context.Context) (*ethpb.ChainHead, err if err != nil { return nil, status.Error(codes.Internal, "Could not get head block") } - if headBlock.IsNil() || headBlock.Block().IsNil() { + if headBlock == nil || headBlock.IsNil() || headBlock.Block().IsNil() { return nil, status.Error(codes.Internal, "Head block of chain was nil") } headBlockRoot, err := headBlock.Block().HashTreeRoot() @@ -322,7 +322,7 @@ func (bs *Server) chainHeadRetrieval(ctx context.Context) (*ethpb.ChainHead, err } // Retrieve genesis block in the event we have genesis checkpoints. genBlock, err := bs.BeaconDB.GenesisBlock(ctx) - if err != nil || genBlock.IsNil() || genBlock.Block().IsNil() { + if err != nil || genBlock == nil || genBlock.IsNil() || genBlock.Block().IsNil() { return nil, status.Error(codes.Internal, "Could not get genesis block") } diff --git a/beacon-chain/rpc/beaconv1/blocks.go b/beacon-chain/rpc/beaconv1/blocks.go index 4578b60a6671..da5d0c62d329 100644 --- a/beacon-chain/rpc/beaconv1/blocks.go +++ b/beacon-chain/rpc/beaconv1/blocks.go @@ -30,7 +30,7 @@ func (bs *Server) GetBlockHeader(ctx context.Context, req *ethpb.BlockRequest) ( if err != nil { return nil, status.Errorf(codes.Internal, "Could not get block from block ID: %v", err) } - if rBlk.IsNil() { + if rBlk == nil || rBlk.IsNil() { return nil, status.Errorf(codes.NotFound, "Could not find requested block header") } blk, err := rBlk.RawPhase0Block() @@ -182,7 +182,7 @@ func (bs *Server) GetBlock(ctx context.Context, req *ethpb.BlockRequest) (*ethpb if err != nil { return nil, status.Errorf(codes.Internal, "Could not get block from block ID: %v", err) } - if rBlk.IsNil() { + if rBlk == nil || rBlk.IsNil() { return nil, status.Errorf(codes.NotFound, "Could not find requested block") } blk, err := rBlk.RawPhase0Block() @@ -227,7 +227,7 @@ func (bs *Server) GetBlockRoot(ctx context.Context, req *ethpb.BlockRequest) (*e if err != nil { return nil, status.Errorf(codes.Internal, "Could not retrieve blocks for genesis slot: %v", err) } - if blk.IsNil() { + if blk == nil || blk.IsNil() { return nil, status.Error(codes.NotFound, "Could not find genesis block") } blkRoot, err := blk.Block().HashTreeRoot() @@ -241,7 +241,7 @@ func (bs *Server) GetBlockRoot(ctx context.Context, req *ethpb.BlockRequest) (*e if err != nil { return nil, status.Errorf(codes.Internal, "Could not retrieve block for block root %#x: %v", req.BlockId, err) } - if block.IsNil() { + if block == nil || block.IsNil() { return nil, status.Error(codes.NotFound, "Could not find any blocks with given root") } @@ -292,7 +292,7 @@ func (bs *Server) ListBlockAttestations(ctx context.Context, req *ethpb.BlockReq if err != nil { return nil, status.Errorf(codes.Internal, "Could not get block from block ID: %v", err) } - if rBlk.IsNil() { + if rBlk == nil || rBlk.IsNil() { return nil, status.Errorf(codes.NotFound, "Could not find requested block") } diff --git a/beacon-chain/rpc/beaconv1/state.go b/beacon-chain/rpc/beaconv1/state.go index 56aba49ba935..7f5c4d5268f1 100644 --- a/beacon-chain/rpc/beaconv1/state.go +++ b/beacon-chain/rpc/beaconv1/state.go @@ -235,7 +235,7 @@ func (bs *Server) stateRootBySlot(ctx context.Context, slot types.Slot) ([]byte, if len(blks) != 1 { return nil, errors.New("multiple blocks exist in same slot") } - if blks[0].IsNil() || blks[0].Block().IsNil() { + if blks[0] == nil || blks[0].IsNil() || blks[0].Block().IsNil() { return nil, errors.New("nil block") } return blks[0].Block().StateRoot(), nil diff --git a/beacon-chain/rpc/debug/block.go b/beacon-chain/rpc/debug/block.go index e2fb646eaab6..481ffd9db674 100644 --- a/beacon-chain/rpc/debug/block.go +++ b/beacon-chain/rpc/debug/block.go @@ -26,7 +26,7 @@ func (ds *Server) GetBlock( if err != nil { return nil, status.Errorf(codes.Internal, "Could not retrieve block by root: %v", err) } - if signedBlock.IsNil() { + if signedBlock == nil || signedBlock.IsNil() { return &pbrpc.SSZResponse{Encoded: make([]byte, 0)}, nil } encoded, err := signedBlock.MarshalSSZ() diff --git a/beacon-chain/state/stategen/getter.go b/beacon-chain/state/stategen/getter.go index b893e389261d..0dffff8764b9 100644 --- a/beacon-chain/state/stategen/getter.go +++ b/beacon-chain/state/stategen/getter.go @@ -248,7 +248,7 @@ func (s *State) lastAncestorState(ctx context.Context, root [32]byte) (iface.Bea if err != nil { return nil, err } - if b.IsNil() { + if b == nil || b.IsNil() { return nil, errUnknownBlock } @@ -289,7 +289,7 @@ func (s *State) lastAncestorState(ctx context.Context, root [32]byte) (iface.Bea if err != nil { return nil, err } - if b.IsNil() { + if b == nil || b.IsNil() { return nil, errUnknownBlock } } diff --git a/beacon-chain/state/stategen/replay.go b/beacon-chain/state/stategen/replay.go index 5950bc38f261..66a4c8c05aa6 100644 --- a/beacon-chain/state/stategen/replay.go +++ b/beacon-chain/state/stategen/replay.go @@ -125,7 +125,7 @@ func executeStateTransitionStateGen( if ctx.Err() != nil { return nil, ctx.Err() } - if signed.IsNil() || signed.Block().IsNil() { + if signed == nil || signed.IsNil() || signed.Block().IsNil() { return nil, errUnknownBlock } @@ -215,7 +215,7 @@ func (s *State) lastSavedBlock(ctx context.Context, slot types.Slot) ([32]byte, if len(lastSaved) != 1 { return [32]byte{}, 0, fmt.Errorf("highest saved block does not equal to 1, it equals to %d", len(lastSaved)) } - if lastSaved[0].IsNil() || lastSaved[0].Block().IsNil() { + if lastSaved[0] == nil || lastSaved[0].IsNil() || lastSaved[0].Block().IsNil() { return [32]byte{}, 0, nil } r, err := lastSaved[0].Block().HashTreeRoot() diff --git a/beacon-chain/sync/initial-sync/blocks_queue_utils.go b/beacon-chain/sync/initial-sync/blocks_queue_utils.go index c9805eec25e4..0a6aa0276e17 100644 --- a/beacon-chain/sync/initial-sync/blocks_queue_utils.go +++ b/beacon-chain/sync/initial-sync/blocks_queue_utils.go @@ -18,7 +18,7 @@ func (q *blocksQueue) resetFromFork(ctx context.Context, fork *forkData) error { return errors.New("no blocks to reset from") } firstBlock := fork.blocks[0].Block() - if firstBlock.IsNil() { + if firstBlock == nil || firstBlock.IsNil() { return errors.New("invalid first block in fork data") } diff --git a/beacon-chain/sync/pending_blocks_queue.go b/beacon-chain/sync/pending_blocks_queue.go index e16350864da3..b9acff0605c4 100644 --- a/beacon-chain/sync/pending_blocks_queue.go +++ b/beacon-chain/sync/pending_blocks_queue.go @@ -78,7 +78,7 @@ func (s *Service) processPendingBlocks(ctx context.Context) error { // Loop through the pending queue and mark the potential parent blocks as seen. for _, b := range bs { - if b.IsNil() || b.Block().IsNil() { + if b == nil || b.IsNil() || b.Block().IsNil() { span.End() continue } diff --git a/beacon-chain/sync/rpc_beacon_blocks_by_range.go b/beacon-chain/sync/rpc_beacon_blocks_by_range.go index deb24622bfa9..791939d18430 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_range.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_range.go @@ -160,7 +160,7 @@ func (s *Service) writeBlockRangeToStream(ctx context.Context, startSlot, endSlo return err } for _, b := range blks { - if b.IsNil() || b.Block().IsNil() { + if b == nil || b.IsNil() || b.Block().IsNil() { continue } if chunkErr := s.chunkWriter(stream, b.Proto()); chunkErr != nil { diff --git a/beacon-chain/sync/rpc_beacon_blocks_by_root.go b/beacon-chain/sync/rpc_beacon_blocks_by_root.go index da8a3e1328cc..f71bed12f4ca 100644 --- a/beacon-chain/sync/rpc_beacon_blocks_by_root.go +++ b/beacon-chain/sync/rpc_beacon_blocks_by_root.go @@ -69,7 +69,7 @@ func (s *Service) beaconBlocksRootRPCHandler(ctx context.Context, msg interface{ s.writeErrorResponseToStream(responseCodeServerError, types.ErrGeneric.Error(), stream) return err } - if blk.IsNil() { + if blk == nil || blk.IsNil() { continue } if err := s.chunkWriter(stream, blk.Proto()); err != nil { diff --git a/beacon-chain/sync/rpc_status.go b/beacon-chain/sync/rpc_status.go index b5cea63db64c..ff074280535a 100644 --- a/beacon-chain/sync/rpc_status.go +++ b/beacon-chain/sync/rpc_status.go @@ -315,7 +315,7 @@ func (s *Service) validateStatusMessage(ctx context.Context, msg *pb.Status) err if err != nil { return p2ptypes.ErrGeneric } - if blk.IsNil() { + if blk == nil || blk.IsNil() { return p2ptypes.ErrGeneric } if helpers.SlotToEpoch(blk.Block().Slot()) == msg.FinalizedEpoch { @@ -333,7 +333,7 @@ func (s *Service) validateStatusMessage(ctx context.Context, msg *pb.Status) err } // Is a valid finalized block if no // other child blocks exist yet. - if childBlock.IsNil() { + if childBlock == nil || childBlock.IsNil() { return nil } // If child finalized block also has a smaller or diff --git a/beacon-chain/sync/validate_beacon_blocks.go b/beacon-chain/sync/validate_beacon_blocks.go index f8940288d42c..33d11716d3ee 100644 --- a/beacon-chain/sync/validate_beacon_blocks.go +++ b/beacon-chain/sync/validate_beacon_blocks.go @@ -61,7 +61,7 @@ func (s *Service) validateBeaconBlockPubSub(ctx context.Context, pid peer.ID, ms } blk := interfaces.NewWrappedSignedBeaconBlock(rblk) - if blk.Block().IsNil() { + if blk.IsNil() || blk.Block().IsNil() { log.WithError(errors.New("block.Block is nil")).Debug("Rejected block") return pubsub.ValidationReject } From fc3e41b96515d7e632e8e805e29729aeae9dd6a7 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 13:44:24 +0800 Subject: [PATCH 28/44] add in more defensive nil checks --- beacon-chain/sync/validate_beacon_blocks.go | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon-chain/sync/validate_beacon_blocks.go b/beacon-chain/sync/validate_beacon_blocks.go index 33d11716d3ee..8f8e100741e6 100644 --- a/beacon-chain/sync/validate_beacon_blocks.go +++ b/beacon-chain/sync/validate_beacon_blocks.go @@ -10,6 +10,7 @@ import ( "github.com/libp2p/go-libp2p-core/peer" pubsub "github.com/libp2p/go-libp2p-pubsub" + types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" From 951192c2936b7a5c6ad3cd377da0ceba8ba46854 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 13:46:12 +0800 Subject: [PATCH 29/44] imports --- beacon-chain/sync/subscriber_beacon_blocks.go | 3 +-- beacon-chain/sync/validate_beacon_blocks.go | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/beacon-chain/sync/subscriber_beacon_blocks.go b/beacon-chain/sync/subscriber_beacon_blocks.go index b84879109cf0..d04a98a6a756 100644 --- a/beacon-chain/sync/subscriber_beacon_blocks.go +++ b/beacon-chain/sync/subscriber_beacon_blocks.go @@ -4,11 +4,10 @@ import ( "context" "errors" - "github.com/prysmaticlabs/prysm/shared/interfaces" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state/interop" + "github.com/prysmaticlabs/prysm/shared/interfaces" "google.golang.org/protobuf/proto" ) diff --git a/beacon-chain/sync/validate_beacon_blocks.go b/beacon-chain/sync/validate_beacon_blocks.go index 8f8e100741e6..fa09727e1f8b 100644 --- a/beacon-chain/sync/validate_beacon_blocks.go +++ b/beacon-chain/sync/validate_beacon_blocks.go @@ -6,11 +6,8 @@ import ( "fmt" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/libp2p/go-libp2p-core/peer" pubsub "github.com/libp2p/go-libp2p-pubsub" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" @@ -19,6 +16,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/timeutils" "github.com/prysmaticlabs/prysm/shared/traceutil" From 5a706f8676fecf8c75bf50f32b55c74b642e5073 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 26 May 2021 13:50:52 +0800 Subject: [PATCH 30/44] Apply suggestions from code review Co-authored-by: terence tsao --- beacon-chain/blockchain/metrics.go | 1 - beacon-chain/core/helpers/block.go | 1 - beacon-chain/core/state/transition.go | 1 - beacon-chain/core/state/transition_no_verify_sig.go | 1 - beacon-chain/db/kv/blocks.go | 1 - beacon-chain/db/kv/finalized_block_roots.go | 1 - beacon-chain/db/kv/genesis.go | 1 - beacon-chain/rpc/beaconv1/blocks.go | 1 - 8 files changed, 8 deletions(-) diff --git a/beacon-chain/blockchain/metrics.go b/beacon-chain/blockchain/metrics.go index 39ac094f1e8f..19c3e596505e 100644 --- a/beacon-chain/blockchain/metrics.go +++ b/beacon-chain/blockchain/metrics.go @@ -4,7 +4,6 @@ import ( "context" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" types "github.com/prysmaticlabs/eth2-types" diff --git a/beacon-chain/core/helpers/block.go b/beacon-chain/core/helpers/block.go index 8441b3217cdc..757e35a63e5e 100644 --- a/beacon-chain/core/helpers/block.go +++ b/beacon-chain/core/helpers/block.go @@ -4,7 +4,6 @@ import ( "math" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" diff --git a/beacon-chain/core/state/transition.go b/beacon-chain/core/state/transition.go index dbeeb1bd7f6f..17627f3d4b88 100644 --- a/beacon-chain/core/state/transition.go +++ b/beacon-chain/core/state/transition.go @@ -9,7 +9,6 @@ import ( "fmt" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/cache" diff --git a/beacon-chain/core/state/transition_no_verify_sig.go b/beacon-chain/core/state/transition_no_verify_sig.go index f8b2b002a8a9..303565865370 100644 --- a/beacon-chain/core/state/transition_no_verify_sig.go +++ b/beacon-chain/core/state/transition_no_verify_sig.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" diff --git a/beacon-chain/db/kv/blocks.go b/beacon-chain/db/kv/blocks.go index 4ec167272af0..d05edc25c9b0 100644 --- a/beacon-chain/db/kv/blocks.go +++ b/beacon-chain/db/kv/blocks.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" diff --git a/beacon-chain/db/kv/finalized_block_roots.go b/beacon-chain/db/kv/finalized_block_roots.go index d31b0c2557d6..3a59a4037062 100644 --- a/beacon-chain/db/kv/finalized_block_roots.go +++ b/beacon-chain/db/kv/finalized_block_roots.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/prysmaticlabs/prysm/shared/interfaces" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db" diff --git a/beacon-chain/db/kv/genesis.go b/beacon-chain/db/kv/genesis.go index 6dd70a6c0e8d..c88b4567fa82 100644 --- a/beacon-chain/db/kv/genesis.go +++ b/beacon-chain/db/kv/genesis.go @@ -8,7 +8,6 @@ import ( "io/ioutil" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" dbIface "github.com/prysmaticlabs/prysm/beacon-chain/db/iface" diff --git a/beacon-chain/rpc/beaconv1/blocks.go b/beacon-chain/rpc/beaconv1/blocks.go index da5d0c62d329..64116963725e 100644 --- a/beacon-chain/rpc/beaconv1/blocks.go +++ b/beacon-chain/rpc/beaconv1/blocks.go @@ -6,7 +6,6 @@ import ( "strconv" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1" From c550d6887e6b6956eabcb790931e1ee4fbc1c094 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 26 May 2021 13:57:07 +0800 Subject: [PATCH 31/44] Apply suggestions from code review Co-authored-by: terence tsao --- beacon-chain/blockchain/process_attestation.go | 1 - beacon-chain/blockchain/receive_block.go | 1 - beacon-chain/blockchain/testing/mock.go | 1 - beacon-chain/cache/attestation_data.go | 1 - beacon-chain/core/blocks/attestation.go | 1 - beacon-chain/core/blocks/block_operations_fuzz_test.go | 1 - beacon-chain/core/blocks/eth1_data.go | 1 - beacon-chain/core/blocks/eth1_data_test.go | 1 - beacon-chain/core/blocks/header.go | 1 - beacon-chain/core/blocks/header_test.go | 1 - beacon-chain/core/blocks/randao.go | 1 - beacon-chain/core/blocks/randao_test.go | 1 - beacon-chain/core/epoch/epoch_processing.go | 1 - beacon-chain/core/feed/state/events.go | 1 - beacon-chain/core/state/benchmarks_test.go | 1 - beacon-chain/core/state/interop/write_block_to_disk.go | 1 - beacon-chain/core/state/skip_slot_cache_test.go | 1 - 17 files changed, 17 deletions(-) diff --git a/beacon-chain/blockchain/process_attestation.go b/beacon-chain/blockchain/process_attestation.go index 331a77c198e6..1a8ab2882c29 100644 --- a/beacon-chain/blockchain/process_attestation.go +++ b/beacon-chain/blockchain/process_attestation.go @@ -4,7 +4,6 @@ import ( "context" "github.com/prysmaticlabs/prysm/shared/blockutil" - "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index 60b6f2728b11..0c1096b86cee 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -4,7 +4,6 @@ import ( "context" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" diff --git a/beacon-chain/blockchain/testing/mock.go b/beacon-chain/blockchain/testing/mock.go index c26b1d9b4078..0ae7ed3eaae0 100644 --- a/beacon-chain/blockchain/testing/mock.go +++ b/beacon-chain/blockchain/testing/mock.go @@ -9,7 +9,6 @@ import ( "time" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" diff --git a/beacon-chain/cache/attestation_data.go b/beacon-chain/cache/attestation_data.go index 849bc3b1dd0a..ef3f52f84e4b 100644 --- a/beacon-chain/cache/attestation_data.go +++ b/beacon-chain/cache/attestation_data.go @@ -9,7 +9,6 @@ import ( "time" "github.com/prysmaticlabs/prysm/shared/blockutil" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" diff --git a/beacon-chain/core/blocks/attestation.go b/beacon-chain/core/blocks/attestation.go index ba9f4e78aad3..17be8f7951e5 100644 --- a/beacon-chain/core/blocks/attestation.go +++ b/beacon-chain/core/blocks/attestation.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" diff --git a/beacon-chain/core/blocks/block_operations_fuzz_test.go b/beacon-chain/core/blocks/block_operations_fuzz_test.go index dc7114631a63..71a95d42bb55 100644 --- a/beacon-chain/core/blocks/block_operations_fuzz_test.go +++ b/beacon-chain/core/blocks/block_operations_fuzz_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/prysmaticlabs/prysm/shared/interfaces" - fuzz "github.com/google/gofuzz" types "github.com/prysmaticlabs/eth2-types" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" diff --git a/beacon-chain/core/blocks/eth1_data.go b/beacon-chain/core/blocks/eth1_data.go index 5b738d88442a..1bd9d2656863 100644 --- a/beacon-chain/core/blocks/eth1_data.go +++ b/beacon-chain/core/blocks/eth1_data.go @@ -6,7 +6,6 @@ import ( "errors" "github.com/prysmaticlabs/prysm/shared/blockutil" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/params" diff --git a/beacon-chain/core/blocks/eth1_data_test.go b/beacon-chain/core/blocks/eth1_data_test.go index 2a2facba2d04..77c0aba1d576 100644 --- a/beacon-chain/core/blocks/eth1_data_test.go +++ b/beacon-chain/core/blocks/eth1_data_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/prysmaticlabs/prysm/shared/blockutil" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" diff --git a/beacon-chain/core/blocks/header.go b/beacon-chain/core/blocks/header.go index 6b89b9507e42..ddfbd6157aff 100644 --- a/beacon-chain/core/blocks/header.go +++ b/beacon-chain/core/blocks/header.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" diff --git a/beacon-chain/core/blocks/header_test.go b/beacon-chain/core/blocks/header_test.go index 95a454107fe9..402d8dc1e396 100644 --- a/beacon-chain/core/blocks/header_test.go +++ b/beacon-chain/core/blocks/header_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/prysmaticlabs/prysm/shared/interfaces" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" diff --git a/beacon-chain/core/blocks/randao.go b/beacon-chain/core/blocks/randao.go index b2ff50858303..864e90828e0a 100644 --- a/beacon-chain/core/blocks/randao.go +++ b/beacon-chain/core/blocks/randao.go @@ -4,7 +4,6 @@ import ( "context" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" diff --git a/beacon-chain/core/blocks/randao_test.go b/beacon-chain/core/blocks/randao_test.go index 5a1010995b3b..2ce46e5d4412 100644 --- a/beacon-chain/core/blocks/randao_test.go +++ b/beacon-chain/core/blocks/randao_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" diff --git a/beacon-chain/core/epoch/epoch_processing.go b/beacon-chain/core/epoch/epoch_processing.go index c9ca6ceb63f7..293131a01eea 100644 --- a/beacon-chain/core/epoch/epoch_processing.go +++ b/beacon-chain/core/epoch/epoch_processing.go @@ -9,7 +9,6 @@ import ( "sort" "github.com/prysmaticlabs/prysm/shared/blockutil" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" diff --git a/beacon-chain/core/feed/state/events.go b/beacon-chain/core/feed/state/events.go index ab9a97980e61..826efb20864b 100644 --- a/beacon-chain/core/feed/state/events.go +++ b/beacon-chain/core/feed/state/events.go @@ -7,7 +7,6 @@ import ( "time" "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ) diff --git a/beacon-chain/core/state/benchmarks_test.go b/beacon-chain/core/state/benchmarks_test.go index 878997ebf873..33e8c1f0f53e 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/prysmaticlabs/prysm/shared/interfaces" - "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" coreState "github.com/prysmaticlabs/prysm/beacon-chain/core/state" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" diff --git a/beacon-chain/core/state/interop/write_block_to_disk.go b/beacon-chain/core/state/interop/write_block_to_disk.go index aac9c6590088..79e819166801 100644 --- a/beacon-chain/core/state/interop/write_block_to_disk.go +++ b/beacon-chain/core/state/interop/write_block_to_disk.go @@ -6,7 +6,6 @@ import ( "path" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/fileutil" ) diff --git a/beacon-chain/core/state/skip_slot_cache_test.go b/beacon-chain/core/state/skip_slot_cache_test.go index f8248ace1aeb..255fb08b496e 100644 --- a/beacon-chain/core/state/skip_slot_cache_test.go +++ b/beacon-chain/core/state/skip_slot_cache_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prysmaticlabs/prysm/beacon-chain/core/state" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" From 88b693db8979b7242f62dfe48c5a31842cd55dca Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 26 May 2021 14:00:27 +0800 Subject: [PATCH 32/44] Update shared/interfaces/block_interface.go Co-authored-by: terence tsao --- shared/interfaces/block_interface.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/interfaces/block_interface.go b/shared/interfaces/block_interface.go index 5622b5f51508..9fca6bc10ada 100644 --- a/shared/interfaces/block_interface.go +++ b/shared/interfaces/block_interface.go @@ -19,7 +19,7 @@ type SignedBeaconBlock interface { } // BeaconBlock describes an interface which states the methods -// employed by an object that is a Beacon Block. +// employed by an object that is a beacon block. type BeaconBlock interface { Slot() types.Slot ProposerIndex() types.ValidatorIndex From eec67ccf1a355bb5b0c531eccc8af5a353824bbe Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 26 May 2021 14:00:36 +0800 Subject: [PATCH 33/44] Update shared/interfaces/block_wrapper.go Co-authored-by: terence tsao --- shared/interfaces/block_wrapper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/interfaces/block_wrapper.go b/shared/interfaces/block_wrapper.go index dd037853432c..07690bc8ddbd 100644 --- a/shared/interfaces/block_wrapper.go +++ b/shared/interfaces/block_wrapper.go @@ -14,7 +14,7 @@ type WrappedSignedBeaconBlock struct { b *ethpb.SignedBeaconBlock } -// NewWrappedSignedBeaconBlock is constructor which wraps a protobuf phase 0 object +// NewWrappedSignedBeaconBlock is constructor which wraps a protobuf phase 0 block // with the block wrapper. func NewWrappedSignedBeaconBlock(b *ethpb.SignedBeaconBlock) WrappedSignedBeaconBlock { return WrappedSignedBeaconBlock{b: b} From cb6dd0b40921d8df1f4c2a0b13992af9c1a1bd15 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 26 May 2021 14:00:49 +0800 Subject: [PATCH 34/44] Update shared/interfaces/block_interface.go Co-authored-by: terence tsao --- shared/interfaces/block_interface.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/interfaces/block_interface.go b/shared/interfaces/block_interface.go index 9fca6bc10ada..a4bbb28ba95e 100644 --- a/shared/interfaces/block_interface.go +++ b/shared/interfaces/block_interface.go @@ -7,7 +7,7 @@ import ( ) // SignedBeaconBlock is an interface describing the method set of -// a signed block. +// a signed beacon block. type SignedBeaconBlock interface { Block() BeaconBlock Signature() []byte From 07ac83e1d044a43b8529f12175b61e4b6732a4a0 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 14:17:59 +0800 Subject: [PATCH 35/44] imports --- beacon-chain/blockchain/chain_info_norace_test.go | 3 +-- beacon-chain/blockchain/chain_info_test.go | 3 +-- beacon-chain/blockchain/info_test.go | 3 +-- beacon-chain/blockchain/metrics.go | 3 +-- beacon-chain/blockchain/receive_block.go | 3 +-- beacon-chain/blockchain/testing/mock.go | 3 +-- beacon-chain/core/blocks/attestation.go | 3 +-- beacon-chain/core/blocks/header_test.go | 3 +-- beacon-chain/core/blocks/randao.go | 3 +-- beacon-chain/core/blocks/randao_test.go | 3 +-- beacon-chain/core/feed/state/events.go | 3 +-- beacon-chain/core/state/benchmarks_test.go | 3 +-- beacon-chain/core/state/interop/write_block_to_disk.go | 3 +-- beacon-chain/core/state/skip_slot_cache_test.go | 3 +-- beacon-chain/core/state/transition.go | 3 +-- beacon-chain/core/state/transition_no_verify_sig.go | 3 +-- beacon-chain/core/state/transition_no_verify_sig_test.go | 3 +-- beacon-chain/core/state/transition_test.go | 3 +-- beacon-chain/db/kv/blocks.go | 3 +-- beacon-chain/db/kv/blocks_test.go | 3 +-- beacon-chain/db/kv/finalized_block_roots.go | 3 +-- beacon-chain/db/kv/genesis.go | 3 +-- beacon-chain/db/kv/state.go | 3 +-- beacon-chain/db/restore_test.go | 3 +-- beacon-chain/rpc/beacon/assignments_test.go | 3 +-- beacon-chain/rpc/beacon/attestations_test.go | 2 +- beacon-chain/rpc/beacon/blocks_test.go | 3 +-- beacon-chain/rpc/beaconv1/blocks.go | 3 +-- beacon-chain/rpc/beaconv1/blocks_test.go | 3 +-- beacon-chain/rpc/statefetcher/fetcher_test.go | 3 +-- beacon-chain/rpc/validator/attester_test.go | 3 +-- beacon-chain/rpc/validator/exit_test.go | 3 +-- beacon-chain/rpc/validator/proposer.go | 3 +-- beacon-chain/rpc/validator/proposer_test.go | 1 - beacon-chain/rpc/validator/server_test.go | 3 +-- beacon-chain/rpc/validator/status_test.go | 3 +-- beacon-chain/state/stategen/getter_test.go | 4 +--- beacon-chain/state/stategen/mock.go | 3 +-- beacon-chain/state/stategen/replay.go | 3 +-- beacon-chain/state/stategen/replay_test.go | 3 +-- beacon-chain/state/stategen/service.go | 3 +-- beacon-chain/state/stategen/service_test.go | 3 +-- beacon-chain/state/stategen/setter_test.go | 3 +-- beacon-chain/sync/initial-sync/blocks_fetcher.go | 3 +-- beacon-chain/sync/initial-sync/blocks_fetcher_utils.go | 3 +-- beacon-chain/sync/initial-sync/fsm.go | 3 +-- beacon-chain/sync/pending_blocks_queue.go | 3 +-- beacon-chain/sync/rpc_chunked_response.go | 3 +-- beacon-chain/sync/rpc_send_request.go | 3 +-- 49 files changed, 48 insertions(+), 97 deletions(-) diff --git a/beacon-chain/blockchain/chain_info_norace_test.go b/beacon-chain/blockchain/chain_info_norace_test.go index 137f9d47f840..13b064f7534b 100644 --- a/beacon-chain/blockchain/chain_info_norace_test.go +++ b/beacon-chain/blockchain/chain_info_norace_test.go @@ -7,8 +7,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prysmaticlabs/prysm/shared/testutil/require" + pcl := protocol.ID(p2p.RPCBlocksByRootTopicV1)"github.com/prysmaticlabs/prysm/shared/testutil/require" ) func TestHeadSlot_DataRace(t *testing.T) { diff --git a/beacon-chain/blockchain/chain_info_test.go b/beacon-chain/blockchain/chain_info_test.go index c6551ee4b8ea..e8e4b40d1102 100644 --- a/beacon-chain/blockchain/chain_info_test.go +++ b/beacon-chain/blockchain/chain_info_test.go @@ -13,8 +13,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prysmaticlabs/prysm/shared/params" + pcl := protocol.ID(p2p.RPCBlocksByRootTopicV1)"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" diff --git a/beacon-chain/blockchain/info_test.go b/beacon-chain/blockchain/info_test.go index a932743e6113..c99fa96caad2 100644 --- a/beacon-chain/blockchain/info_test.go +++ b/beacon-chain/blockchain/info_test.go @@ -9,8 +9,7 @@ import ( testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prysmaticlabs/prysm/shared/params" +pcl := protocol.ID(p2p.RPCBlocksByRootTopicV1)"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" diff --git a/beacon-chain/blockchain/metrics.go b/beacon-chain/blockchain/metrics.go index 39ac094f1e8f..1818f2abf9e1 100644 --- a/beacon-chain/blockchain/metrics.go +++ b/beacon-chain/blockchain/metrics.go @@ -3,8 +3,6 @@ package blockchain import ( "context" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" types "github.com/prysmaticlabs/eth2-types" @@ -12,6 +10,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" ) diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index 60b6f2728b11..0702ec7d604e 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -3,14 +3,13 @@ package blockchain import ( "context" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" "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" "github.com/prysmaticlabs/prysm/shared/featureconfig" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/timeutils" "github.com/prysmaticlabs/prysm/shared/traceutil" "go.opencensus.io/trace" diff --git a/beacon-chain/blockchain/testing/mock.go b/beacon-chain/blockchain/testing/mock.go index c26b1d9b4078..e135a85bc21e 100644 --- a/beacon-chain/blockchain/testing/mock.go +++ b/beacon-chain/blockchain/testing/mock.go @@ -8,8 +8,6 @@ import ( "sync" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -26,6 +24,7 @@ import ( pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/event" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/sirupsen/logrus" ) diff --git a/beacon-chain/core/blocks/attestation.go b/beacon-chain/core/blocks/attestation.go index ba9f4e78aad3..38af6e70cea4 100644 --- a/beacon-chain/core/blocks/attestation.go +++ b/beacon-chain/core/blocks/attestation.go @@ -4,8 +4,6 @@ import ( "context" "fmt" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -14,6 +12,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/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "go.opencensus.io/trace" ) diff --git a/beacon-chain/core/blocks/header_test.go b/beacon-chain/core/blocks/header_test.go index 95a454107fe9..b221641ebb47 100644 --- a/beacon-chain/core/blocks/header_test.go +++ b/beacon-chain/core/blocks/header_test.go @@ -5,14 +5,13 @@ import ( "io/ioutil" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/core/blocks/randao.go b/beacon-chain/core/blocks/randao.go index b2ff50858303..c6f04cceeaaa 100644 --- a/beacon-chain/core/blocks/randao.go +++ b/beacon-chain/core/blocks/randao.go @@ -3,12 +3,11 @@ package blocks import ( "context" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/hashutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" ) diff --git a/beacon-chain/core/blocks/randao_test.go b/beacon-chain/core/blocks/randao_test.go index 5a1010995b3b..81e1c0874a17 100644 --- a/beacon-chain/core/blocks/randao_test.go +++ b/beacon-chain/core/blocks/randao_test.go @@ -5,13 +5,12 @@ import ( "encoding/binary" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "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" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/core/feed/state/events.go b/beacon-chain/core/feed/state/events.go index ab9a97980e61..79eae66504a6 100644 --- a/beacon-chain/core/feed/state/events.go +++ b/beacon-chain/core/feed/state/events.go @@ -6,9 +6,8 @@ package state import ( "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" + "github.com/prysmaticlabs/prysm/shared/interfaces" ) const ( diff --git a/beacon-chain/core/state/benchmarks_test.go b/beacon-chain/core/state/benchmarks_test.go index 878997ebf873..78b15d4f7eb8 100644 --- a/beacon-chain/core/state/benchmarks_test.go +++ b/beacon-chain/core/state/benchmarks_test.go @@ -4,14 +4,13 @@ import ( "context" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" coreState "github.com/prysmaticlabs/prysm/beacon-chain/core/state" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/benchutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil/require" "google.golang.org/protobuf/proto" diff --git a/beacon-chain/core/state/interop/write_block_to_disk.go b/beacon-chain/core/state/interop/write_block_to_disk.go index aac9c6590088..6af26de759f2 100644 --- a/beacon-chain/core/state/interop/write_block_to_disk.go +++ b/beacon-chain/core/state/interop/write_block_to_disk.go @@ -5,10 +5,9 @@ import ( "os" "path" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/fileutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" ) // WriteBlockToDisk as a block ssz. Writes to temp directory. Debug! diff --git a/beacon-chain/core/state/skip_slot_cache_test.go b/beacon-chain/core/state/skip_slot_cache_test.go index f8248ace1aeb..e183f165e98b 100644 --- a/beacon-chain/core/state/skip_slot_cache_test.go +++ b/beacon-chain/core/state/skip_slot_cache_test.go @@ -5,11 +5,10 @@ import ( "sync" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prysmaticlabs/prysm/beacon-chain/core/state" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/core/state/transition.go b/beacon-chain/core/state/transition.go index dbeeb1bd7f6f..cc7af726b7d8 100644 --- a/beacon-chain/core/state/transition.go +++ b/beacon-chain/core/state/transition.go @@ -8,8 +8,6 @@ import ( "context" "fmt" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/cache" @@ -19,6 +17,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/mathutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/traceutil" diff --git a/beacon-chain/core/state/transition_no_verify_sig.go b/beacon-chain/core/state/transition_no_verify_sig.go index f8b2b002a8a9..08601ee9e88b 100644 --- a/beacon-chain/core/state/transition_no_verify_sig.go +++ b/beacon-chain/core/state/transition_no_verify_sig.go @@ -5,8 +5,6 @@ import ( "context" "fmt" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" @@ -15,6 +13,7 @@ import ( iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/featureconfig" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/traceutil" "go.opencensus.io/trace" ) diff --git a/beacon-chain/core/state/transition_no_verify_sig_test.go b/beacon-chain/core/state/transition_no_verify_sig_test.go index fd92370da22f..de2ef3113f91 100644 --- a/beacon-chain/core/state/transition_no_verify_sig_test.go +++ b/beacon-chain/core/state/transition_no_verify_sig_test.go @@ -4,12 +4,11 @@ import ( "context" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/core/state/transition_test.go b/beacon-chain/core/state/transition_test.go index 9c7628c35b9a..5a7662181270 100644 --- a/beacon-chain/core/state/transition_test.go +++ b/beacon-chain/core/state/transition_test.go @@ -6,8 +6,6 @@ import ( "fmt" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-bitfield" @@ -21,6 +19,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/hashutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/db/kv/blocks.go b/beacon-chain/db/kv/blocks.go index 4ec167272af0..45453c4a73b1 100644 --- a/beacon-chain/db/kv/blocks.go +++ b/beacon-chain/db/kv/blocks.go @@ -5,14 +5,13 @@ import ( "context" "fmt" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/sliceutil" bolt "go.etcd.io/bbolt" diff --git a/beacon-chain/db/kv/blocks_test.go b/beacon-chain/db/kv/blocks_test.go index 1388faa19958..5fbd85a43770 100644 --- a/beacon-chain/db/kv/blocks_test.go +++ b/beacon-chain/db/kv/blocks_test.go @@ -9,8 +9,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" "github.com/prysmaticlabs/prysm/shared/bytesutil" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/prysmaticlabs/prysm/shared/params" +pcl := protocol.ID(p2p.RPCBlocksByRootTopicV1)"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" diff --git a/beacon-chain/db/kv/finalized_block_roots.go b/beacon-chain/db/kv/finalized_block_roots.go index d31b0c2557d6..e4bdd7233498 100644 --- a/beacon-chain/db/kv/finalized_block_roots.go +++ b/beacon-chain/db/kv/finalized_block_roots.go @@ -5,12 +5,11 @@ import ( "context" "fmt" - "github.com/prysmaticlabs/prysm/shared/interfaces" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/traceutil" bolt "go.etcd.io/bbolt" "go.opencensus.io/trace" diff --git a/beacon-chain/db/kv/genesis.go b/beacon-chain/db/kv/genesis.go index 6dd70a6c0e8d..e52791152f9b 100644 --- a/beacon-chain/db/kv/genesis.go +++ b/beacon-chain/db/kv/genesis.go @@ -7,14 +7,13 @@ import ( "io" "io/ioutil" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" dbIface "github.com/prysmaticlabs/prysm/beacon-chain/db/iface" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" state "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" ) diff --git a/beacon-chain/db/kv/state.go b/beacon-chain/db/kv/state.go index feab89cad07e..e1d2de51e329 100644 --- a/beacon-chain/db/kv/state.go +++ b/beacon-chain/db/kv/state.go @@ -4,8 +4,6 @@ import ( "bytes" "context" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -15,6 +13,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/traceutil" bolt "go.etcd.io/bbolt" diff --git a/beacon-chain/db/restore_test.go b/beacon-chain/db/restore_test.go index f052dccdaca8..4bfdf52bbb7d 100644 --- a/beacon-chain/db/restore_test.go +++ b/beacon-chain/db/restore_test.go @@ -8,11 +8,10 @@ import ( "path" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/db/kv" "github.com/prysmaticlabs/prysm/shared/cmd" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" diff --git a/beacon-chain/rpc/beacon/assignments_test.go b/beacon-chain/rpc/beacon/assignments_test.go index a24380cde391..28ab7286c452 100644 --- a/beacon-chain/rpc/beacon/assignments_test.go +++ b/beacon-chain/rpc/beacon/assignments_test.go @@ -7,8 +7,6 @@ import ( "strconv" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" @@ -16,6 +14,7 @@ import ( dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" "github.com/prysmaticlabs/prysm/shared/cmd" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/rpc/beacon/attestations_test.go b/beacon-chain/rpc/beacon/attestations_test.go index ee09fc98e4ba..3b7ca3a73aba 100644 --- a/beacon-chain/rpc/beacon/attestations_test.go +++ b/beacon-chain/rpc/beacon/attestations_test.go @@ -3,7 +3,6 @@ package beacon import ( "context" "fmt" - "github.com/prysmaticlabs/prysm/shared/interfaces" "sort" "strconv" "testing" @@ -26,6 +25,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/cmd" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/mock" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" diff --git a/beacon-chain/rpc/beacon/blocks_test.go b/beacon-chain/rpc/beacon/blocks_test.go index 34c10f85b61f..966742af855f 100644 --- a/beacon-chain/rpc/beacon/blocks_test.go +++ b/beacon-chain/rpc/beacon/blocks_test.go @@ -6,8 +6,6 @@ import ( "strconv" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/golang/mock/gomock" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -22,6 +20,7 @@ import ( pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/cmd" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/mock" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" diff --git a/beacon-chain/rpc/beaconv1/blocks.go b/beacon-chain/rpc/beaconv1/blocks.go index da5d0c62d329..073dff941e79 100644 --- a/beacon-chain/rpc/beaconv1/blocks.go +++ b/beacon-chain/rpc/beaconv1/blocks.go @@ -5,8 +5,6 @@ import ( "fmt" "strconv" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1" @@ -15,6 +13,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" "github.com/prysmaticlabs/prysm/proto/migration" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "go.opencensus.io/trace" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" diff --git a/beacon-chain/rpc/beaconv1/blocks_test.go b/beacon-chain/rpc/beaconv1/blocks_test.go index d41b3fb64e9f..8a242d8a49d0 100644 --- a/beacon-chain/rpc/beaconv1/blocks_test.go +++ b/beacon-chain/rpc/beaconv1/blocks_test.go @@ -5,8 +5,6 @@ import ( "reflect" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1" ethpb_alpha "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -17,6 +15,7 @@ import ( p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/proto/migration" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/rpc/statefetcher/fetcher_test.go b/beacon-chain/rpc/statefetcher/fetcher_test.go index 1fc5af470824..18234946fa84 100644 --- a/beacon-chain/rpc/statefetcher/fetcher_test.go +++ b/beacon-chain/rpc/statefetcher/fetcher_test.go @@ -7,8 +7,6 @@ import ( "testing" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/ethereum/go-ethereum/common/hexutil" types "github.com/prysmaticlabs/eth2-types" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -17,6 +15,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/rpc/validator/attester_test.go b/beacon-chain/rpc/validator/attester_test.go index f8a35ce51860..5ff091a88e44 100644 --- a/beacon-chain/rpc/validator/attester_test.go +++ b/beacon-chain/rpc/validator/attester_test.go @@ -7,8 +7,6 @@ import ( "testing" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" @@ -23,6 +21,7 @@ import ( 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/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/rpc/validator/exit_test.go b/beacon-chain/rpc/validator/exit_test.go index 5325ab2d544d..524ca72d5624 100644 --- a/beacon-chain/rpc/validator/exit_test.go +++ b/beacon-chain/rpc/validator/exit_test.go @@ -5,8 +5,6 @@ import ( "testing" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" @@ -19,6 +17,7 @@ import ( mockp2p "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/rpc/validator/proposer.go b/beacon-chain/rpc/validator/proposer.go index 0c3f8a5b704b..7dd61b5d48d8 100644 --- a/beacon-chain/rpc/validator/proposer.go +++ b/beacon-chain/rpc/validator/proposer.go @@ -9,8 +9,6 @@ import ( "reflect" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - fastssz "github.com/ferranbt/fastssz" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" @@ -27,6 +25,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/hashutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/rand" "github.com/prysmaticlabs/prysm/shared/trieutil" diff --git a/beacon-chain/rpc/validator/proposer_test.go b/beacon-chain/rpc/validator/proposer_test.go index 87256eff5a83..8075ab3aadcd 100644 --- a/beacon-chain/rpc/validator/proposer_test.go +++ b/beacon-chain/rpc/validator/proposer_test.go @@ -2,7 +2,6 @@ package validator import ( "context" - "github.com/prysmaticlabs/prysm/shared/interfaces" "math/big" "testing" diff --git a/beacon-chain/rpc/validator/server_test.go b/beacon-chain/rpc/validator/server_test.go index bf04d038e544..d1201eec606c 100644 --- a/beacon-chain/rpc/validator/server_test.go +++ b/beacon-chain/rpc/validator/server_test.go @@ -6,8 +6,6 @@ import ( "testing" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/golang/mock/gomock" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" @@ -22,6 +20,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/event" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/mock" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" diff --git a/beacon-chain/rpc/validator/status_test.go b/beacon-chain/rpc/validator/status_test.go index d2e463680cc8..7f5862cc5ac1 100644 --- a/beacon-chain/rpc/validator/status_test.go +++ b/beacon-chain/rpc/validator/status_test.go @@ -5,8 +5,6 @@ import ( "testing" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" @@ -18,6 +16,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/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/state/stategen/getter_test.go b/beacon-chain/state/stategen/getter_test.go index 1be6754d97ce..80e03c25ae2f 100644 --- a/beacon-chain/state/stategen/getter_test.go +++ b/beacon-chain/state/stategen/getter_test.go @@ -4,14 +4,12 @@ import ( "context" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" - "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/state/stategen/mock.go b/beacon-chain/state/stategen/mock.go index a8e0e2645c79..134b5c8adafd 100644 --- a/beacon-chain/state/stategen/mock.go +++ b/beacon-chain/state/stategen/mock.go @@ -3,11 +3,10 @@ package stategen import ( "context" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" ) // MockStateManager is a fake implementation of StateManager. diff --git a/beacon-chain/state/stategen/replay.go b/beacon-chain/state/stategen/replay.go index 66a4c8c05aa6..a16363416673 100644 --- a/beacon-chain/state/stategen/replay.go +++ b/beacon-chain/state/stategen/replay.go @@ -4,14 +4,13 @@ import ( "context" "fmt" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" transition "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "go.opencensus.io/trace" ) diff --git a/beacon-chain/state/stategen/replay_test.go b/beacon-chain/state/stategen/replay_test.go index 263780b89561..5bac406971db 100644 --- a/beacon-chain/state/stategen/replay_test.go +++ b/beacon-chain/state/stategen/replay_test.go @@ -4,8 +4,6 @@ import ( "context" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" @@ -13,6 +11,7 @@ import ( testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/state/stategen/service.go b/beacon-chain/state/stategen/service.go index 76f9ebd6ad20..3dec30200bdf 100644 --- a/beacon-chain/state/stategen/service.go +++ b/beacon-chain/state/stategen/service.go @@ -8,13 +8,12 @@ import ( "errors" "sync" - "github.com/prysmaticlabs/prysm/shared/interfaces" - types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/db" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "go.opencensus.io/trace" ) diff --git a/beacon-chain/state/stategen/service_test.go b/beacon-chain/state/stategen/service_test.go index f22e5e722ff7..4f589f65dbc7 100644 --- a/beacon-chain/state/stategen/service_test.go +++ b/beacon-chain/state/stategen/service_test.go @@ -4,10 +4,9 @@ import ( "context" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/state/stategen/setter_test.go b/beacon-chain/state/stategen/setter_test.go index 81367c6e7c8f..39b0bc9651c7 100644 --- a/beacon-chain/state/stategen/setter_test.go +++ b/beacon-chain/state/stategen/setter_test.go @@ -4,9 +4,8 @@ import ( "context" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" - testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" diff --git a/beacon-chain/sync/initial-sync/blocks_fetcher.go b/beacon-chain/sync/initial-sync/blocks_fetcher.go index cdd665efad1e..939d66b4eb6a 100644 --- a/beacon-chain/sync/initial-sync/blocks_fetcher.go +++ b/beacon-chain/sync/initial-sync/blocks_fetcher.go @@ -6,8 +6,6 @@ import ( "sync" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/kevinms/leakybucket-go" "github.com/libp2p/go-libp2p-core/peer" "github.com/pkg/errors" @@ -18,6 +16,7 @@ import ( prysmsync "github.com/prysmaticlabs/prysm/beacon-chain/sync" "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/rand" "github.com/sirupsen/logrus" diff --git a/beacon-chain/sync/initial-sync/blocks_fetcher_utils.go b/beacon-chain/sync/initial-sync/blocks_fetcher_utils.go index ee09aff01a76..eb81d64f44e3 100644 --- a/beacon-chain/sync/initial-sync/blocks_fetcher_utils.go +++ b/beacon-chain/sync/initial-sync/blocks_fetcher_utils.go @@ -5,8 +5,6 @@ import ( "fmt" "sort" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/libp2p/go-libp2p-core/peer" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" @@ -15,6 +13,7 @@ import ( "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/sirupsen/logrus" "go.opencensus.io/trace" diff --git a/beacon-chain/sync/initial-sync/fsm.go b/beacon-chain/sync/initial-sync/fsm.go index 51902c5db988..691e4b8b15f9 100644 --- a/beacon-chain/sync/initial-sync/fsm.go +++ b/beacon-chain/sync/initial-sync/fsm.go @@ -6,11 +6,10 @@ import ( "sort" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/libp2p/go-libp2p-core/peer" types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/timeutils" ) diff --git a/beacon-chain/sync/pending_blocks_queue.go b/beacon-chain/sync/pending_blocks_queue.go index b9acff0605c4..c0172bea0fc9 100644 --- a/beacon-chain/sync/pending_blocks_queue.go +++ b/beacon-chain/sync/pending_blocks_queue.go @@ -7,13 +7,12 @@ import ( "sync" "time" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/rand" "github.com/prysmaticlabs/prysm/shared/runutil" diff --git a/beacon-chain/sync/rpc_chunked_response.go b/beacon-chain/sync/rpc_chunked_response.go index 2a0467d2faea..f820380a7b9d 100644 --- a/beacon-chain/sync/rpc_chunked_response.go +++ b/beacon-chain/sync/rpc_chunked_response.go @@ -3,13 +3,12 @@ package sync import ( "errors" - "github.com/prysmaticlabs/prysm/shared/interfaces" - libp2pcore "github.com/libp2p/go-libp2p-core" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/blockchain" "github.com/prysmaticlabs/prysm/beacon-chain/p2p" "github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder" + "github.com/prysmaticlabs/prysm/shared/interfaces" ) // chunkWriter writes the given message as a chunked response to the given network diff --git a/beacon-chain/sync/rpc_send_request.go b/beacon-chain/sync/rpc_send_request.go index 49837f8d3fcb..b1286ec777d5 100644 --- a/beacon-chain/sync/rpc_send_request.go +++ b/beacon-chain/sync/rpc_send_request.go @@ -4,8 +4,6 @@ import ( "context" "io" - "github.com/prysmaticlabs/prysm/shared/interfaces" - "github.com/libp2p/go-libp2p-core/peer" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" @@ -13,6 +11,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/p2p" p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" ) From 19fb5b02efa9dfdff26473b56f2fab1255dc2c1a Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 14:22:30 +0800 Subject: [PATCH 36/44] fix bad changes --- beacon-chain/blockchain/chain_info_norace_test.go | 3 ++- beacon-chain/blockchain/chain_info_test.go | 3 ++- beacon-chain/blockchain/info_test.go | 3 ++- beacon-chain/db/kv/blocks_test.go | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/beacon-chain/blockchain/chain_info_norace_test.go b/beacon-chain/blockchain/chain_info_norace_test.go index 13b064f7534b..137f9d47f840 100644 --- a/beacon-chain/blockchain/chain_info_norace_test.go +++ b/beacon-chain/blockchain/chain_info_norace_test.go @@ -7,7 +7,8 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" - pcl := protocol.ID(p2p.RPCBlocksByRootTopicV1)"github.com/prysmaticlabs/prysm/shared/testutil/require" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "github.com/prysmaticlabs/prysm/shared/testutil/require" ) func TestHeadSlot_DataRace(t *testing.T) { diff --git a/beacon-chain/blockchain/chain_info_test.go b/beacon-chain/blockchain/chain_info_test.go index e8e4b40d1102..c6551ee4b8ea 100644 --- a/beacon-chain/blockchain/chain_info_test.go +++ b/beacon-chain/blockchain/chain_info_test.go @@ -13,7 +13,8 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" - pcl := protocol.ID(p2p.RPCBlocksByRootTopicV1)"github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "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" diff --git a/beacon-chain/blockchain/info_test.go b/beacon-chain/blockchain/info_test.go index c99fa96caad2..a932743e6113 100644 --- a/beacon-chain/blockchain/info_test.go +++ b/beacon-chain/blockchain/info_test.go @@ -9,7 +9,8 @@ import ( testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" -pcl := protocol.ID(p2p.RPCBlocksByRootTopicV1)"github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "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" diff --git a/beacon-chain/db/kv/blocks_test.go b/beacon-chain/db/kv/blocks_test.go index 5fbd85a43770..1388faa19958 100644 --- a/beacon-chain/db/kv/blocks_test.go +++ b/beacon-chain/db/kv/blocks_test.go @@ -9,7 +9,8 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" "github.com/prysmaticlabs/prysm/shared/bytesutil" -pcl := protocol.ID(p2p.RPCBlocksByRootTopicV1)"github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/interfaces" + "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" From c26767722efc18c8f597d37a03230f8b27f9b9ed Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 14:44:49 +0800 Subject: [PATCH 37/44] fix --- beacon-chain/rpc/validator/proposer_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon-chain/rpc/validator/proposer_test.go b/beacon-chain/rpc/validator/proposer_test.go index 8075ab3aadcd..ca6d19e5eea1 100644 --- a/beacon-chain/rpc/validator/proposer_test.go +++ b/beacon-chain/rpc/validator/proposer_test.go @@ -27,6 +27,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" From eb0591736a710e42fbd37c97e093bb784b8f5772 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 14:54:38 +0800 Subject: [PATCH 38/44] terence's review --- beacon-chain/blockchain/process_block_test.go | 2 +- beacon-chain/rpc/beacon/blocks.go | 12 ++++++------ beacon-chain/rpc/beaconv1/blocks.go | 8 ++++---- beacon-chain/rpc/validator/server.go | 2 +- shared/interfaces/block_interface.go | 2 +- shared/interfaces/block_wrapper.go | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/beacon-chain/blockchain/process_block_test.go b/beacon-chain/blockchain/process_block_test.go index 8b13ad202d84..c17c4606d330 100644 --- a/beacon-chain/blockchain/process_block_test.go +++ b/beacon-chain/blockchain/process_block_test.go @@ -171,7 +171,7 @@ func TestStore_OnBlockBatch(t *testing.T) { blkRoots = append(blkRoots, root) } - rBlock, err := blks[0].RawPhase0Block() + rBlock, err := blks[0].ProtobufPhase0Block() assert.NoError(t, err) rBlock.Block.ParentRoot = gRoot[:] require.NoError(t, beaconDB.SaveBlock(context.Background(), blks[0])) diff --git a/beacon-chain/rpc/beacon/blocks.go b/beacon-chain/rpc/beacon/blocks.go index 873ae6ed8a17..5dafb77e6322 100644 --- a/beacon-chain/rpc/beacon/blocks.go +++ b/beacon-chain/rpc/beacon/blocks.go @@ -67,7 +67,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err) } - phBlk, err := b.RawPhase0Block() + phBlk, err := b.ProtobufPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) } @@ -103,7 +103,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err) } - phBlk, err := blk.RawPhase0Block() + phBlk, err := blk.ProtobufPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) } @@ -147,7 +147,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err) } - phBlk, err := b.RawPhase0Block() + phBlk, err := b.ProtobufPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) } @@ -175,7 +175,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, err } - phBlk, err := genBlk.RawPhase0Block() + phBlk, err := genBlk.ProtobufPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) } @@ -226,7 +226,7 @@ func (bs *Server) StreamBlocks(req *ethpb.StreamBlocksRequest, stream ethpb.Beac if !ok || data == nil { continue } - phBlk, err := data.SignedBlock.RawPhase0Block() + phBlk, err := data.SignedBlock.ProtobufPhase0Block() if err != nil { log.Error(err) continue @@ -256,7 +256,7 @@ func (bs *Server) StreamBlocks(req *ethpb.StreamBlocksRequest, stream ethpb.Beac log.WithError(err).WithField("blockSlot", data.SignedBlock.Block().Slot()).Error("Could not verify block signature") continue } - phBlk, err := signed.RawPhase0Block() + phBlk, err := signed.ProtobufPhase0Block() if err != nil { log.Error(err) continue diff --git a/beacon-chain/rpc/beaconv1/blocks.go b/beacon-chain/rpc/beaconv1/blocks.go index 073dff941e79..7bf536656e80 100644 --- a/beacon-chain/rpc/beaconv1/blocks.go +++ b/beacon-chain/rpc/beaconv1/blocks.go @@ -32,7 +32,7 @@ func (bs *Server) GetBlockHeader(ctx context.Context, req *ethpb.BlockRequest) ( if rBlk == nil || rBlk.IsNil() { return nil, status.Errorf(codes.NotFound, "Could not find requested block header") } - blk, err := rBlk.RawPhase0Block() + blk, err := rBlk.ProtobufPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) } @@ -100,7 +100,7 @@ func (bs *Server) ListBlockHeaders(ctx context.Context, req *ethpb.BlockHeadersR blkHdrs := make([]*ethpb.BlockHeaderContainer, len(blks)) for i, bl := range blks { - blk, err := bl.RawPhase0Block() + blk, err := bl.ProtobufPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) } @@ -184,7 +184,7 @@ func (bs *Server) GetBlock(ctx context.Context, req *ethpb.BlockRequest) (*ethpb if rBlk == nil || rBlk.IsNil() { return nil, status.Errorf(codes.NotFound, "Could not find requested block") } - blk, err := rBlk.RawPhase0Block() + blk, err := rBlk.ProtobufPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) } @@ -295,7 +295,7 @@ func (bs *Server) ListBlockAttestations(ctx context.Context, req *ethpb.BlockReq return nil, status.Errorf(codes.NotFound, "Could not find requested block") } - blk, err := rBlk.RawPhase0Block() + blk, err := rBlk.ProtobufPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) } diff --git a/beacon-chain/rpc/validator/server.go b/beacon-chain/rpc/validator/server.go index bbfb27f4c286..8b44823ae163 100644 --- a/beacon-chain/rpc/validator/server.go +++ b/beacon-chain/rpc/validator/server.go @@ -142,7 +142,7 @@ func (vs *Server) CanonicalHead(ctx context.Context, _ *emptypb.Empty) (*ethpb.S if err != nil { return nil, status.Errorf(codes.Internal, "Could not get head block: %v", err) } - b, err := headBlk.RawPhase0Block() + b, err := headBlk.ProtobufPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get head block: %v", err) } diff --git a/shared/interfaces/block_interface.go b/shared/interfaces/block_interface.go index a4bbb28ba95e..f3987c5a72a4 100644 --- a/shared/interfaces/block_interface.go +++ b/shared/interfaces/block_interface.go @@ -15,7 +15,7 @@ type SignedBeaconBlock interface { Copy() WrappedSignedBeaconBlock MarshalSSZ() ([]byte, error) Proto() proto.Message - RawPhase0Block() (*ethpb.SignedBeaconBlock, error) + ProtobufPhase0Block() (*ethpb.SignedBeaconBlock, error) } // BeaconBlock describes an interface which states the methods diff --git a/shared/interfaces/block_wrapper.go b/shared/interfaces/block_wrapper.go index 07690bc8ddbd..7120fb00a91a 100644 --- a/shared/interfaces/block_wrapper.go +++ b/shared/interfaces/block_wrapper.go @@ -54,8 +54,8 @@ func (w WrappedSignedBeaconBlock) Proto() proto.Message { return w.b } -// RawPhase0Block returns the underlying protobuf object. -func (w WrappedSignedBeaconBlock) RawPhase0Block() (*ethpb.SignedBeaconBlock, error) { +// ProtobufPhase0Block returns the underlying protobuf object. +func (w WrappedSignedBeaconBlock) ProtobufPhase0Block() (*ethpb.SignedBeaconBlock, error) { return w.b, nil } From 2792b5683b1f7be4487a1ea9e24e003993aa852e Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 14:59:28 +0800 Subject: [PATCH 39/44] terence's review --- beacon-chain/blockchain/process_block_test.go | 2 +- beacon-chain/rpc/beacon/blocks.go | 12 ++++++------ beacon-chain/rpc/beaconv1/blocks.go | 8 ++++---- beacon-chain/rpc/validator/server.go | 2 +- shared/interfaces/block_interface.go | 2 +- shared/interfaces/block_wrapper.go | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/beacon-chain/blockchain/process_block_test.go b/beacon-chain/blockchain/process_block_test.go index c17c4606d330..a530ecfe5c51 100644 --- a/beacon-chain/blockchain/process_block_test.go +++ b/beacon-chain/blockchain/process_block_test.go @@ -171,7 +171,7 @@ func TestStore_OnBlockBatch(t *testing.T) { blkRoots = append(blkRoots, root) } - rBlock, err := blks[0].ProtobufPhase0Block() + rBlock, err := blks[0].PbPhase0Block() assert.NoError(t, err) rBlock.Block.ParentRoot = gRoot[:] require.NoError(t, beaconDB.SaveBlock(context.Background(), blks[0])) diff --git a/beacon-chain/rpc/beacon/blocks.go b/beacon-chain/rpc/beacon/blocks.go index 5dafb77e6322..d256eec9d097 100644 --- a/beacon-chain/rpc/beacon/blocks.go +++ b/beacon-chain/rpc/beacon/blocks.go @@ -67,7 +67,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err) } - phBlk, err := b.ProtobufPhase0Block() + phBlk, err := b.PbPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) } @@ -103,7 +103,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err) } - phBlk, err := blk.ProtobufPhase0Block() + phBlk, err := blk.PbPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) } @@ -147,7 +147,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err) } - phBlk, err := b.ProtobufPhase0Block() + phBlk, err := b.PbPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) } @@ -175,7 +175,7 @@ func (bs *Server) ListBlocks( if err != nil { return nil, err } - phBlk, err := genBlk.ProtobufPhase0Block() + phBlk, err := genBlk.PbPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) } @@ -226,7 +226,7 @@ func (bs *Server) StreamBlocks(req *ethpb.StreamBlocksRequest, stream ethpb.Beac if !ok || data == nil { continue } - phBlk, err := data.SignedBlock.ProtobufPhase0Block() + phBlk, err := data.SignedBlock.PbPhase0Block() if err != nil { log.Error(err) continue @@ -256,7 +256,7 @@ func (bs *Server) StreamBlocks(req *ethpb.StreamBlocksRequest, stream ethpb.Beac log.WithError(err).WithField("blockSlot", data.SignedBlock.Block().Slot()).Error("Could not verify block signature") continue } - phBlk, err := signed.ProtobufPhase0Block() + phBlk, err := signed.PbPhase0Block() if err != nil { log.Error(err) continue diff --git a/beacon-chain/rpc/beaconv1/blocks.go b/beacon-chain/rpc/beaconv1/blocks.go index 7bf536656e80..0753a3dac246 100644 --- a/beacon-chain/rpc/beaconv1/blocks.go +++ b/beacon-chain/rpc/beaconv1/blocks.go @@ -32,7 +32,7 @@ func (bs *Server) GetBlockHeader(ctx context.Context, req *ethpb.BlockRequest) ( if rBlk == nil || rBlk.IsNil() { return nil, status.Errorf(codes.NotFound, "Could not find requested block header") } - blk, err := rBlk.ProtobufPhase0Block() + blk, err := rBlk.PbPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) } @@ -100,7 +100,7 @@ func (bs *Server) ListBlockHeaders(ctx context.Context, req *ethpb.BlockHeadersR blkHdrs := make([]*ethpb.BlockHeaderContainer, len(blks)) for i, bl := range blks { - blk, err := bl.ProtobufPhase0Block() + blk, err := bl.PbPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) } @@ -184,7 +184,7 @@ func (bs *Server) GetBlock(ctx context.Context, req *ethpb.BlockRequest) (*ethpb if rBlk == nil || rBlk.IsNil() { return nil, status.Errorf(codes.NotFound, "Could not find requested block") } - blk, err := rBlk.ProtobufPhase0Block() + blk, err := rBlk.PbPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) } @@ -295,7 +295,7 @@ func (bs *Server) ListBlockAttestations(ctx context.Context, req *ethpb.BlockReq return nil, status.Errorf(codes.NotFound, "Could not find requested block") } - blk, err := rBlk.ProtobufPhase0Block() + blk, err := rBlk.PbPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get raw block: %v", err) } diff --git a/beacon-chain/rpc/validator/server.go b/beacon-chain/rpc/validator/server.go index 8b44823ae163..963dada124fc 100644 --- a/beacon-chain/rpc/validator/server.go +++ b/beacon-chain/rpc/validator/server.go @@ -142,7 +142,7 @@ func (vs *Server) CanonicalHead(ctx context.Context, _ *emptypb.Empty) (*ethpb.S if err != nil { return nil, status.Errorf(codes.Internal, "Could not get head block: %v", err) } - b, err := headBlk.ProtobufPhase0Block() + b, err := headBlk.PbPhase0Block() if err != nil { return nil, status.Errorf(codes.Internal, "Could not get head block: %v", err) } diff --git a/shared/interfaces/block_interface.go b/shared/interfaces/block_interface.go index f3987c5a72a4..4686cdf66590 100644 --- a/shared/interfaces/block_interface.go +++ b/shared/interfaces/block_interface.go @@ -15,7 +15,7 @@ type SignedBeaconBlock interface { Copy() WrappedSignedBeaconBlock MarshalSSZ() ([]byte, error) Proto() proto.Message - ProtobufPhase0Block() (*ethpb.SignedBeaconBlock, error) + PbPhase0Block() (*ethpb.SignedBeaconBlock, error) } // BeaconBlock describes an interface which states the methods diff --git a/shared/interfaces/block_wrapper.go b/shared/interfaces/block_wrapper.go index 7120fb00a91a..46b3f4f36df7 100644 --- a/shared/interfaces/block_wrapper.go +++ b/shared/interfaces/block_wrapper.go @@ -54,8 +54,8 @@ func (w WrappedSignedBeaconBlock) Proto() proto.Message { return w.b } -// ProtobufPhase0Block returns the underlying protobuf object. -func (w WrappedSignedBeaconBlock) ProtobufPhase0Block() (*ethpb.SignedBeaconBlock, error) { +// PbPhase0Block returns the underlying protobuf object. +func (w WrappedSignedBeaconBlock) PbPhase0Block() (*ethpb.SignedBeaconBlock, error) { return w.b, nil } From a4a684f5a32914656742c3c78ccc0e062ea7db29 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 15:07:59 +0800 Subject: [PATCH 40/44] fmt --- beacon-chain/blockchain/process_attestation.go | 2 +- beacon-chain/cache/attestation_data.go | 2 +- beacon-chain/core/blocks/block_operations_fuzz_test.go | 2 +- beacon-chain/core/blocks/eth1_data.go | 2 +- beacon-chain/core/blocks/eth1_data_test.go | 2 +- beacon-chain/core/blocks/header.go | 2 +- beacon-chain/core/epoch/epoch_processing.go | 2 +- beacon-chain/core/helpers/block.go | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/beacon-chain/blockchain/process_attestation.go b/beacon-chain/blockchain/process_attestation.go index 1a8ab2882c29..8343c833978c 100644 --- a/beacon-chain/blockchain/process_attestation.go +++ b/beacon-chain/blockchain/process_attestation.go @@ -3,11 +3,11 @@ package blockchain import ( "context" - "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/shared/attestationutil" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/timeutils" diff --git a/beacon-chain/cache/attestation_data.go b/beacon-chain/cache/attestation_data.go index ef3f52f84e4b..85ddc210f801 100644 --- a/beacon-chain/cache/attestation_data.go +++ b/beacon-chain/cache/attestation_data.go @@ -8,10 +8,10 @@ import ( "sync" "time" - "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/shared/blockutil" "k8s.io/client-go/tools/cache" ) diff --git a/beacon-chain/core/blocks/block_operations_fuzz_test.go b/beacon-chain/core/blocks/block_operations_fuzz_test.go index 71a95d42bb55..0720c224955c 100644 --- a/beacon-chain/core/blocks/block_operations_fuzz_test.go +++ b/beacon-chain/core/blocks/block_operations_fuzz_test.go @@ -4,13 +4,13 @@ import ( "context" "testing" - "github.com/prysmaticlabs/prysm/shared/interfaces" fuzz "github.com/google/gofuzz" types "github.com/prysmaticlabs/eth2-types" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) diff --git a/beacon-chain/core/blocks/eth1_data.go b/beacon-chain/core/blocks/eth1_data.go index 1bd9d2656863..8894de414637 100644 --- a/beacon-chain/core/blocks/eth1_data.go +++ b/beacon-chain/core/blocks/eth1_data.go @@ -5,9 +5,9 @@ import ( "context" "errors" - "github.com/prysmaticlabs/prysm/shared/blockutil" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/params" ) diff --git a/beacon-chain/core/blocks/eth1_data_test.go b/beacon-chain/core/blocks/eth1_data_test.go index 77c0aba1d576..ca440ec1bc02 100644 --- a/beacon-chain/core/blocks/eth1_data_test.go +++ b/beacon-chain/core/blocks/eth1_data_test.go @@ -5,12 +5,12 @@ import ( "fmt" "testing" - "github.com/prysmaticlabs/prysm/shared/blockutil" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" diff --git a/beacon-chain/core/blocks/header.go b/beacon-chain/core/blocks/header.go index ddfbd6157aff..aa516b618bd6 100644 --- a/beacon-chain/core/blocks/header.go +++ b/beacon-chain/core/blocks/header.go @@ -5,11 +5,11 @@ import ( "context" "fmt" - "github.com/prysmaticlabs/prysm/shared/interfaces" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" ) diff --git a/beacon-chain/core/epoch/epoch_processing.go b/beacon-chain/core/epoch/epoch_processing.go index 293131a01eea..a35a2fca237b 100644 --- a/beacon-chain/core/epoch/epoch_processing.go +++ b/beacon-chain/core/epoch/epoch_processing.go @@ -8,7 +8,6 @@ import ( "fmt" "sort" - "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -17,6 +16,7 @@ import ( iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/attestationutil" + "github.com/prysmaticlabs/prysm/shared/blockutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/mathutil" "github.com/prysmaticlabs/prysm/shared/params" diff --git a/beacon-chain/core/helpers/block.go b/beacon-chain/core/helpers/block.go index 757e35a63e5e..da4594f1136f 100644 --- a/beacon-chain/core/helpers/block.go +++ b/beacon-chain/core/helpers/block.go @@ -3,10 +3,10 @@ package helpers import ( "math" - "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" + "github.com/prysmaticlabs/prysm/shared/interfaces" "github.com/prysmaticlabs/prysm/shared/params" ) From 8c54db1d2988033bfd7807f7a717f1fa22b849be Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 26 May 2021 16:01:41 +0800 Subject: [PATCH 41/44] Update beacon-chain/rpc/beacon/blocks.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: RadosÅ‚aw Kapka --- beacon-chain/rpc/beacon/blocks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/rpc/beacon/blocks.go b/beacon-chain/rpc/beacon/blocks.go index d256eec9d097..48019784aaa9 100644 --- a/beacon-chain/rpc/beacon/blocks.go +++ b/beacon-chain/rpc/beacon/blocks.go @@ -69,7 +69,7 @@ func (bs *Server) ListBlocks( } phBlk, err := b.PbPhase0Block() if err != nil { - return nil, status.Errorf(codes.Internal, "Could not determine if block is phase 0 block: %v", err) + return nil, status.Errorf(codes.Internal, "Could not get phase 0 block: %v", err) } containers[i] = ðpb.BeaconBlockContainer{ Block: phBlk, From 86a3b0e9722a14c03b67c3e73e0de6b2e67bdf15 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 16:17:07 +0800 Subject: [PATCH 42/44] fix tests --- beacon-chain/blockchain/chain_info_test.go | 2 +- beacon-chain/db/kv/blocks_test.go | 4 ++-- beacon-chain/db/kv/genesis_test.go | 2 +- beacon-chain/sync/initial-sync/blocks_queue_test.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/beacon-chain/blockchain/chain_info_test.go b/beacon-chain/blockchain/chain_info_test.go index c6551ee4b8ea..78daf804697b 100644 --- a/beacon-chain/blockchain/chain_info_test.go +++ b/beacon-chain/blockchain/chain_info_test.go @@ -144,7 +144,7 @@ func TestHeadBlock_CanRetrieve(t *testing.T) { recevied, err := c.HeadBlock(context.Background()) require.NoError(t, err) - assert.DeepEqual(t, b, recevied, "Incorrect head block received") + assert.DeepEqual(t, b, recevied.Proto(), "Incorrect head block received") } func TestHeadState_CanRetrieve(t *testing.T) { diff --git a/beacon-chain/db/kv/blocks_test.go b/beacon-chain/db/kv/blocks_test.go index 1388faa19958..2ed705847b26 100644 --- a/beacon-chain/db/kv/blocks_test.go +++ b/beacon-chain/db/kv/blocks_test.go @@ -56,7 +56,7 @@ func TestStore_BlocksCRUD(t *testing.T) { require.NoError(t, err) retrievedBlock, err := db.Block(ctx, blockRoot) require.NoError(t, err) - assert.Equal(t, (*ethpb.SignedBeaconBlock)(nil), retrievedBlock, "Expected nil block") + assert.DeepEqual(t, (*ethpb.SignedBeaconBlock)(nil), retrievedBlock.Proto(), "Expected nil block") require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block))) assert.Equal(t, true, db.HasBlock(ctx, blockRoot), "Expected block to exist in the db") retrievedBlock, err = db.Block(ctx, blockRoot) @@ -172,7 +172,7 @@ func TestStore_BlocksCRUD_NoCache(t *testing.T) { require.NoError(t, err) retrievedBlock, err := db.Block(ctx, blockRoot) require.NoError(t, err) - require.Equal(t, (*ethpb.SignedBeaconBlock)(nil), retrievedBlock, "Expected nil block") + require.DeepEqual(t, (*ethpb.SignedBeaconBlock)(nil), retrievedBlock.Proto(), "Expected nil block") require.NoError(t, db.SaveBlock(ctx, interfaces.NewWrappedSignedBeaconBlock(block))) db.blockCache.Del(string(blockRoot[:])) assert.Equal(t, true, db.HasBlock(ctx, blockRoot), "Expected block to exist in the db") diff --git a/beacon-chain/db/kv/genesis_test.go b/beacon-chain/db/kv/genesis_test.go index ba115c98ff36..a9d0c7a3c1ce 100644 --- a/beacon-chain/db/kv/genesis_test.go +++ b/beacon-chain/db/kv/genesis_test.go @@ -99,7 +99,7 @@ func TestEnsureEmbeddedGenesis(t *testing.T) { gb, err := db.GenesisBlock(ctx) assert.NoError(t, err) - if gb != nil { + if gb != nil && !gb.IsNil() { t.Fatal("Genesis block exists already") } diff --git a/beacon-chain/sync/initial-sync/blocks_queue_test.go b/beacon-chain/sync/initial-sync/blocks_queue_test.go index 9be0449eea1a..90fc6caca3bb 100644 --- a/beacon-chain/sync/initial-sync/blocks_queue_test.go +++ b/beacon-chain/sync/initial-sync/blocks_queue_test.go @@ -533,7 +533,7 @@ func TestBlocksQueue_onDataReceivedEvent(t *testing.T) { state: stateScheduled, } assert.Equal(t, peer.ID(""), fsm.pid) - assert.DeepSSZEqual(t, []*eth.SignedBeaconBlock(nil), fsm.blocks) + assert.DeepSSZEqual(t, []interfaces.SignedBeaconBlock(nil), fsm.blocks) updatedState, err := handlerFn(fsm, response) assert.NoError(t, err) assert.Equal(t, stateDataParsed, updatedState) From b38022320eaf963d286bbc72488570e0b60f447a Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 17:18:10 +0800 Subject: [PATCH 43/44] fix --- beacon-chain/blockchain/head_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/blockchain/head_test.go b/beacon-chain/blockchain/head_test.go index 66f1b0e6c5e6..b4803feed3c4 100644 --- a/beacon-chain/blockchain/head_test.go +++ b/beacon-chain/blockchain/head_test.go @@ -55,7 +55,7 @@ func TestSaveHead_Different(t *testing.T) { cachedRoot, err := service.HeadRoot(context.Background()) require.NoError(t, err) assert.DeepEqual(t, cachedRoot, newRoot[:], "Head did not change") - assert.DeepEqual(t, newHeadSignedBlock, service.headBlock(), "Head did not change") + assert.DeepEqual(t, newHeadSignedBlock, service.headBlock().Proto(), "Head did not change") assert.DeepSSZEqual(t, headState.CloneInnerState(), service.headState(ctx).CloneInnerState(), "Head did not change") } From 0798bfe29a702d7be91fe1d332bf48648b4b4ef7 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 26 May 2021 17:53:05 +0800 Subject: [PATCH 44/44] fix all tests --- beacon-chain/blockchain/head_test.go | 2 +- beacon-chain/blockchain/service_test.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/beacon-chain/blockchain/head_test.go b/beacon-chain/blockchain/head_test.go index b4803feed3c4..ae19ab4c04db 100644 --- a/beacon-chain/blockchain/head_test.go +++ b/beacon-chain/blockchain/head_test.go @@ -91,7 +91,7 @@ func TestSaveHead_Different_Reorg(t *testing.T) { if !bytes.Equal(cachedRoot, newRoot[:]) { t.Error("Head did not change") } - assert.DeepEqual(t, newHeadSignedBlock, service.headBlock(), "Head did not change") + assert.DeepEqual(t, newHeadSignedBlock, service.headBlock().Proto(), "Head did not change") assert.DeepSSZEqual(t, headState.CloneInnerState(), service.headState(ctx).CloneInnerState(), "Head did not change") require.LogsContain(t, hook, "Chain reorg occurred") } diff --git a/beacon-chain/blockchain/service_test.go b/beacon-chain/blockchain/service_test.go index f3ca23cde324..71947ca13702 100644 --- a/beacon-chain/blockchain/service_test.go +++ b/beacon-chain/blockchain/service_test.go @@ -281,7 +281,7 @@ func TestChainService_InitializeChainInfo(t *testing.T) { require.NoError(t, c.initializeChainInfo(ctx)) headBlk, err := c.HeadBlock(ctx) require.NoError(t, err) - assert.DeepEqual(t, headBlock, headBlk, "Head block incorrect") + assert.DeepEqual(t, headBlock, headBlk.Proto(), "Head block incorrect") s, err := c.HeadState(ctx) require.NoError(t, err) assert.DeepSSZEqual(t, headState.InnerStateUnsafe(), s.InnerStateUnsafe(), "Head state incorrect") @@ -323,7 +323,7 @@ func TestChainService_InitializeChainInfo_SetHeadAtGenesis(t *testing.T) { require.NoError(t, err) assert.DeepSSZEqual(t, headState.InnerStateUnsafe(), s.InnerStateUnsafe(), "Head state incorrect") assert.Equal(t, genesisRoot, c.genesisRoot, "Genesis block root incorrect") - assert.DeepEqual(t, genesis, c.head.block) + assert.DeepEqual(t, genesis, c.head.block.Proto()) } func TestChainService_InitializeChainInfo_HeadSync(t *testing.T) { @@ -382,7 +382,7 @@ func TestChainService_InitializeChainInfo_HeadSync(t *testing.T) { assert.DeepSSZEqual(t, headState.InnerStateUnsafe(), s.InnerStateUnsafe(), "Head state incorrect") assert.Equal(t, genesisRoot, c.genesisRoot, "Genesis block root incorrect") // Since head sync is not triggered, chain is initialized to the last finalization checkpoint. - assert.DeepEqual(t, finalizedBlock, c.head.block) + assert.DeepEqual(t, finalizedBlock, c.head.block.Proto()) assert.LogsContain(t, hook, "resetting head from the checkpoint ('--head-sync' flag is ignored)") assert.LogsDoNotContain(t, hook, "Regenerating state from the last checkpoint at slot") @@ -403,7 +403,7 @@ func TestChainService_InitializeChainInfo_HeadSync(t *testing.T) { assert.DeepSSZEqual(t, headState.InnerStateUnsafe(), s.InnerStateUnsafe(), "Head state incorrect") assert.Equal(t, genesisRoot, c.genesisRoot, "Genesis block root incorrect") // Head slot is far beyond the latest finalized checkpoint, head sync is triggered. - assert.DeepEqual(t, headBlock, c.head.block) + assert.DeepEqual(t, headBlock, c.head.block.Proto()) assert.LogsContain(t, hook, "Regenerating state from the last checkpoint at slot 225") assert.LogsDoNotContain(t, hook, "resetting head from the checkpoint ('--head-sync' flag is ignored)") }