From 32a710a5bd6caa26a2baedc08a97939d5da211fc Mon Sep 17 00:00:00 2001 From: nisdas Date: Mon, 11 Nov 2019 22:11:57 +0800 Subject: [PATCH 1/2] copy block --- .../blockchain/forkchoice/process_block.go | 1 - beacon-chain/blockchain/receive_block.go | 14 ++++++++------ proto/beacon/db/attestation_container.pb.go | 5 +++-- .../beacon/db/finalized_block_root_container.pb.go | 3 ++- proto/beacon/p2p/v1/messages.pb.go | 5 +++-- proto/beacon/p2p/v1/types.pb.go | 5 +++-- proto/beacon/rpc/v1/services.pb.go | 5 +++-- proto/beacon/rpc/v1_gateway/services.pb.go | 3 ++- proto/eth/v1alpha1/archive.pb.go | 5 +++-- proto/eth/v1alpha1/attestation.pb.go | 5 +++-- proto/eth/v1alpha1/beacon_block.pb.go | 5 +++-- proto/eth/v1alpha1/beacon_chain.pb.go | 5 +++-- proto/eth/v1alpha1/node.pb.go | 5 +++-- proto/eth/v1alpha1/validator.pb.go | 5 +++-- proto/sharding/p2p/v1/messages.pb.go | 3 ++- 15 files changed, 44 insertions(+), 30 deletions(-) diff --git a/beacon-chain/blockchain/forkchoice/process_block.go b/beacon-chain/blockchain/forkchoice/process_block.go index 8fa9b2967861..ae10cf49a9c8 100644 --- a/beacon-chain/blockchain/forkchoice/process_block.go +++ b/beacon-chain/blockchain/forkchoice/process_block.go @@ -73,7 +73,6 @@ func (s *Store) OnBlock(ctx context.Context, b *ethpb.BeaconBlock) error { "slot": b.Slot, "root": fmt.Sprintf("0x%s...", hex.EncodeToString(root[:])[:8]), }).Info("Executing state transition on block") - postState, err := state.ExecuteStateTransition(ctx, preState, b) if err != nil { return errors.Wrap(err, "could not execute state transition") diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index 3fdfa12bfa79..b06ad52ca5cc 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -5,6 +5,7 @@ import ( "context" "encoding/hex" + "github.com/gogo/protobuf/proto" "github.com/pkg/errors" "github.com/prysmaticlabs/go-ssz" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" @@ -61,14 +62,15 @@ func (s *Service) ReceiveBlock(ctx context.Context, block *ethpb.BeaconBlock) er func (s *Service) ReceiveBlockNoPubsub(ctx context.Context, block *ethpb.BeaconBlock) error { ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveBlockNoPubsub") defer span.End() + blockCopy := proto.Clone(block).(*ethpb.BeaconBlock) // Apply state transition on the new block. - if err := s.forkChoiceStore.OnBlock(ctx, block); err != nil { + if err := s.forkChoiceStore.OnBlock(ctx, blockCopy); err != nil { err := errors.Wrap(err, "could not process block from fork choice service") traceutil.AnnotateError(span, err) return err } - root, err := ssz.SigningRoot(block) + root, err := ssz.SigningRoot(blockCopy) if err != nil { return errors.Wrap(err, "could not get signing root on received block") } @@ -91,18 +93,18 @@ func (s *Service) ReceiveBlockNoPubsub(ctx context.Context, block *ethpb.BeaconB } // Remove block's contained deposits, attestations, and other operations from persistent storage. - if err := s.cleanupBlockOperations(ctx, block); err != nil { + if err := s.cleanupBlockOperations(ctx, blockCopy); err != nil { return errors.Wrap(err, "could not clean up block deposits, attestations, and other operations") } // Reports on block and fork choice metrics. - s.reportSlotMetrics(block.Slot) + s.reportSlotMetrics(blockCopy.Slot) // Log if block is a competing block. - isCompetingBlock(root[:], block.Slot, headRoot, headBlk.Slot) + isCompetingBlock(root[:], blockCopy.Slot, headRoot, headBlk.Slot) // Log state transition data. - logStateTransitionData(block, root[:]) + logStateTransitionData(blockCopy, root[:]) processedBlkNoPubsub.Inc() diff --git a/proto/beacon/db/attestation_container.pb.go b/proto/beacon/db/attestation_container.pb.go index e07adc093af7..6689465ba04d 100755 --- a/proto/beacon/db/attestation_container.pb.go +++ b/proto/beacon/db/attestation_container.pb.go @@ -5,12 +5,13 @@ package db import ( fmt "fmt" + io "io" + math "math" + _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" github_com_prysmaticlabs_go_bitfield "github.com/prysmaticlabs/go-bitfield" v1alpha1 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" - io "io" - math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/beacon/db/finalized_block_root_container.pb.go b/proto/beacon/db/finalized_block_root_container.pb.go index a7eae5c80789..ec2d6ffa74b4 100755 --- a/proto/beacon/db/finalized_block_root_container.pb.go +++ b/proto/beacon/db/finalized_block_root_container.pb.go @@ -5,9 +5,10 @@ package db import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/beacon/p2p/v1/messages.pb.go b/proto/beacon/p2p/v1/messages.pb.go index c49daf0c207c..429de7fc2a97 100755 --- a/proto/beacon/p2p/v1/messages.pb.go +++ b/proto/beacon/p2p/v1/messages.pb.go @@ -5,10 +5,11 @@ package ethereum_beacon_p2p_v1 import ( fmt "fmt" - _ "github.com/gogo/protobuf/gogoproto" - proto "github.com/gogo/protobuf/proto" io "io" math "math" + + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/beacon/p2p/v1/types.pb.go b/proto/beacon/p2p/v1/types.pb.go index 3ff88f8599ac..74ff5e6ce4a8 100755 --- a/proto/beacon/p2p/v1/types.pb.go +++ b/proto/beacon/p2p/v1/types.pb.go @@ -5,12 +5,13 @@ package ethereum_beacon_p2p_v1 import ( fmt "fmt" + io "io" + math "math" + _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" github_com_prysmaticlabs_go_bitfield "github.com/prysmaticlabs/go-bitfield" v1alpha1 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" - io "io" - math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/beacon/rpc/v1/services.pb.go b/proto/beacon/rpc/v1/services.pb.go index bcae8f7cec38..42c223a99c73 100755 --- a/proto/beacon/rpc/v1/services.pb.go +++ b/proto/beacon/rpc/v1/services.pb.go @@ -7,13 +7,14 @@ import ( context "context" encoding_binary "encoding/binary" fmt "fmt" + io "io" + math "math" + proto "github.com/gogo/protobuf/proto" types "github.com/gogo/protobuf/types" v1alpha1 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" - io "io" - math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/beacon/rpc/v1_gateway/services.pb.go b/proto/beacon/rpc/v1_gateway/services.pb.go index c708d734c2bc..92e1c285cd45 100755 --- a/proto/beacon/rpc/v1_gateway/services.pb.go +++ b/proto/beacon/rpc/v1_gateway/services.pb.go @@ -6,12 +6,13 @@ package ethereum_beacon_rpc_v1 import ( context "context" fmt "fmt" + math "math" + proto "github.com/golang/protobuf/proto" empty "github.com/golang/protobuf/ptypes/empty" v1alpha1 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" - math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/archive.pb.go b/proto/eth/v1alpha1/archive.pb.go index f943f90f0719..be11ffceb673 100755 --- a/proto/eth/v1alpha1/archive.pb.go +++ b/proto/eth/v1alpha1/archive.pb.go @@ -5,10 +5,11 @@ package eth import ( fmt "fmt" - _ "github.com/gogo/protobuf/gogoproto" - proto "github.com/gogo/protobuf/proto" io "io" math "math" + + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/attestation.pb.go b/proto/eth/v1alpha1/attestation.pb.go index b68d1b3d02d9..008971640b79 100755 --- a/proto/eth/v1alpha1/attestation.pb.go +++ b/proto/eth/v1alpha1/attestation.pb.go @@ -5,11 +5,12 @@ package eth import ( fmt "fmt" + io "io" + math "math" + _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" github_com_prysmaticlabs_go_bitfield "github.com/prysmaticlabs/go-bitfield" - io "io" - math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/beacon_block.pb.go b/proto/eth/v1alpha1/beacon_block.pb.go index f9eb15a7834f..552ea64b0fd1 100755 --- a/proto/eth/v1alpha1/beacon_block.pb.go +++ b/proto/eth/v1alpha1/beacon_block.pb.go @@ -5,10 +5,11 @@ package eth import ( fmt "fmt" - _ "github.com/gogo/protobuf/gogoproto" - proto "github.com/gogo/protobuf/proto" io "io" math "math" + + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/beacon_chain.pb.go b/proto/eth/v1alpha1/beacon_chain.pb.go index 2374817142c3..9319c481b72b 100755 --- a/proto/eth/v1alpha1/beacon_chain.pb.go +++ b/proto/eth/v1alpha1/beacon_chain.pb.go @@ -6,13 +6,14 @@ package eth import ( context "context" fmt "fmt" + io "io" + math "math" + _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" types "github.com/gogo/protobuf/types" _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" - io "io" - math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/node.pb.go b/proto/eth/v1alpha1/node.pb.go index 2ba95ac9b419..647805e30f3d 100755 --- a/proto/eth/v1alpha1/node.pb.go +++ b/proto/eth/v1alpha1/node.pb.go @@ -6,12 +6,13 @@ package eth import ( context "context" fmt "fmt" + io "io" + math "math" + proto "github.com/gogo/protobuf/proto" types "github.com/gogo/protobuf/types" _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" - io "io" - math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/eth/v1alpha1/validator.pb.go b/proto/eth/v1alpha1/validator.pb.go index d4f83587b8da..4fb145895295 100755 --- a/proto/eth/v1alpha1/validator.pb.go +++ b/proto/eth/v1alpha1/validator.pb.go @@ -7,13 +7,14 @@ import ( context "context" encoding_binary "encoding/binary" fmt "fmt" + io "io" + math "math" + _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" types "github.com/gogo/protobuf/types" _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" - io "io" - math "math" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/sharding/p2p/v1/messages.pb.go b/proto/sharding/p2p/v1/messages.pb.go index ba268c5f4bac..61228b93def4 100644 --- a/proto/sharding/p2p/v1/messages.pb.go +++ b/proto/sharding/p2p/v1/messages.pb.go @@ -5,9 +5,10 @@ package ethereum_sharding_p2p_v1 import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. From e1305d23f4c4b38482b2c9b676e955a503bda7e7 Mon Sep 17 00:00:00 2001 From: nisdas Date: Mon, 11 Nov 2019 22:30:13 +0800 Subject: [PATCH 2/2] clone for other service methods too --- beacon-chain/blockchain/receive_block.go | 36 +++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index b06ad52ca5cc..3af34bac8948 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -120,34 +120,35 @@ func (s *Service) ReceiveBlockNoPubsub(ctx context.Context, block *ethpb.BeaconB func (s *Service) ReceiveBlockNoPubsubForkchoice(ctx context.Context, block *ethpb.BeaconBlock) error { ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveBlockNoForkchoice") defer span.End() + blockCopy := proto.Clone(block).(*ethpb.BeaconBlock) // Apply state transition on the incoming newly received block. - if err := s.forkChoiceStore.OnBlock(ctx, block); err != nil { + if err := s.forkChoiceStore.OnBlock(ctx, blockCopy); err != nil { err := errors.Wrap(err, "could not process block from fork choice service") traceutil.AnnotateError(span, err) return err } - root, err := ssz.SigningRoot(block) + root, err := ssz.SigningRoot(blockCopy) if err != nil { return errors.Wrap(err, "could not get signing root on received block") } if !bytes.Equal(root[:], s.HeadRoot()) { - if err := s.saveHead(ctx, block, root); err != nil { + if err := s.saveHead(ctx, blockCopy, root); err != nil { return errors.Wrap(err, "could not save head") } } // Remove block's contained deposits, attestations, and other operations from persistent storage. - if err := s.cleanupBlockOperations(ctx, block); err != nil { + if err := s.cleanupBlockOperations(ctx, blockCopy); err != nil { return errors.Wrap(err, "could not clean up block deposits, attestations, and other operations") } // Reports on block and fork choice metrics. - s.reportSlotMetrics(block.Slot) + s.reportSlotMetrics(blockCopy.Slot) // Log state transition data. - logStateTransitionData(block, root[:]) + logStateTransitionData(blockCopy, root[:]) // We write the latest saved head root to a feed for consumption by other services. s.headUpdatedFeed.Send(root) @@ -161,32 +162,33 @@ func (s *Service) ReceiveBlockNoPubsubForkchoice(ctx context.Context, block *eth func (s *Service) ReceiveBlockNoVerify(ctx context.Context, block *ethpb.BeaconBlock) error { ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveBlockNoVerify") defer span.End() + blockCopy := proto.Clone(block).(*ethpb.BeaconBlock) - // Apply state transition on the incoming newly received block without verifying its BLS contents. - if err := s.forkChoiceStore.OnBlockNoVerifyStateTransition(ctx, block); err != nil { - return errors.Wrap(err, "could not process block from fork choice service") + // Apply state transition on the incoming newly received blockCopy without verifying its BLS contents. + if err := s.forkChoiceStore.OnBlockNoVerifyStateTransition(ctx, blockCopy); err != nil { + return errors.Wrap(err, "could not process blockCopy from fork choice service") } - root, err := ssz.SigningRoot(block) + root, err := ssz.SigningRoot(blockCopy) if err != nil { - return errors.Wrap(err, "could not get signing root on received block") + return errors.Wrap(err, "could not get signing root on received blockCopy") } if !bytes.Equal(root[:], s.HeadRoot()) { - if err := s.saveHead(ctx, block, root); err != nil { + if err := s.saveHead(ctx, blockCopy, root); err != nil { err := errors.Wrap(err, "could not save head") traceutil.AnnotateError(span, err) return err } } - // Reports on block and fork choice metrics. - s.reportSlotMetrics(block.Slot) + // Reports on blockCopy and fork choice metrics. + s.reportSlotMetrics(blockCopy.Slot) // Log state transition data. log.WithFields(logrus.Fields{ - "slot": block.Slot, - "attestations": len(block.Body.Attestations), - "deposits": len(block.Body.Deposits), + "slot": blockCopy.Slot, + "attestations": len(blockCopy.Body.Attestations), + "deposits": len(blockCopy.Body.Deposits), }).Debug("Finished applying state transition") // We write the latest saved head root to a feed for consumption by other services.