Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy Block When Receiving it From Sync #3966

Merged
merged 2 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion beacon-chain/blockchain/forkchoice/process_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
50 changes: 27 additions & 23 deletions beacon-chain/blockchain/receive_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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()

Expand All @@ -118,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)
Expand All @@ -159,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.
Expand Down
5 changes: 3 additions & 2 deletions proto/beacon/db/attestation_container.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion proto/beacon/db/finalized_block_root_container.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/beacon/p2p/v1/messages.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/beacon/p2p/v1/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/beacon/rpc/v1/services.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion proto/beacon/rpc/v1_gateway/services.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/eth/v1alpha1/archive.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/eth/v1alpha1/attestation.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/eth/v1alpha1/beacon_block.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/eth/v1alpha1/beacon_chain.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/eth/v1alpha1/node.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/eth/v1alpha1/validator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion proto/sharding/p2p/v1/messages.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.