diff --git a/beacon-chain/blockchain/block_processing.go b/beacon-chain/blockchain/block_processing.go index c713189735..be50ec77b8 100644 --- a/beacon-chain/blockchain/block_processing.go +++ b/beacon-chain/blockchain/block_processing.go @@ -26,7 +26,7 @@ type BlockReceiver interface { // BlockProcessor defines a common interface for methods useful for directly applying state transitions // to beacon blocks and generating a new beacon state from the Ethereum 2.0 core primitives. type BlockProcessor interface { - VerifyBlockValidity(block *pb.BeaconBlock, beaconState *pb.BeaconState) error + VerifyBlockValidity(ctx context.Context, block *pb.BeaconBlock, beaconState *pb.BeaconState) error ApplyBlockStateTransition(ctx context.Context, block *pb.BeaconBlock, beaconState *pb.BeaconState) (*pb.BeaconState, error) CleanupBlockOperations(ctx context.Context, block *pb.BeaconBlock) error } @@ -47,7 +47,7 @@ func (c *ChainService) ReceiveBlock(ctx context.Context, block *pb.BeaconBlock) } // We first verify the block's basic validity conditions. - if err := c.VerifyBlockValidity(block, beaconState); err != nil { + if err := c.VerifyBlockValidity(ctx, block, beaconState); err != nil { return beaconState, fmt.Errorf("block with slot %d is not ready for processing: %v", block.Slot, err) } @@ -113,7 +113,7 @@ func (c *ChainService) ApplyBlockStateTransition( // Check for skipped slots. numSkippedSlots := 0 for beaconState.Slot < block.Slot-1 { - beaconState, err = c.runStateTransition(headRoot, nil, beaconState) + beaconState, err = c.runStateTransition(ctx, headRoot, nil, beaconState) if err != nil { return beaconState, fmt.Errorf("could not execute state transition without block %v", err) } @@ -123,7 +123,7 @@ func (c *ChainService) ApplyBlockStateTransition( log.Warnf("Processed %d skipped slots", numSkippedSlots) } - beaconState, err = c.runStateTransition(headRoot, block, beaconState) + beaconState, err = c.runStateTransition(ctx, headRoot, block, beaconState) if err != nil { return beaconState, fmt.Errorf("could not execute state transition with block %v", err) } @@ -136,13 +136,17 @@ func (c *ChainService) ApplyBlockStateTransition( // The node has processed its state up to slot, block.slot - 1. // The Ethereum 1.0 block pointed to by the state.processed_pow_receipt_root has been processed and accepted. // The node's local clock time is greater than or equal to state.genesis_time + block.slot * SECONDS_PER_SLOT. -func (c *ChainService) VerifyBlockValidity(block *pb.BeaconBlock, beaconState *pb.BeaconState) error { +func (c *ChainService) VerifyBlockValidity( + ctx context.Context, + block *pb.BeaconBlock, + beaconState *pb.BeaconState, +) error { if block.Slot == params.BeaconConfig().GenesisSlot { return fmt.Errorf("cannot process a genesis block: received block with slot %d", block.Slot-params.BeaconConfig().GenesisSlot) } powBlockFetcher := c.web3Service.Client().BlockByHash - if err := b.IsValidBlock(c.ctx, beaconState, block, + if err := b.IsValidBlock(ctx, beaconState, block, c.beaconDB.HasBlock, powBlockFetcher, c.genesisTime); err != nil { return fmt.Errorf("block does not fulfill pre-processing conditions %v", err) } @@ -180,7 +184,7 @@ func (c *ChainService) CleanupBlockOperations(ctx context.Context, block *pb.Bea // Update attestation store with latest attestation target. for _, att := range block.Body.Attestations { - if err := c.attsService.UpdateLatestAttestation(c.ctx, att); err != nil { + if err := c.attsService.UpdateLatestAttestation(ctx, att); err != nil { return fmt.Errorf("failed to update latest attestation for store: %v", err) } } @@ -196,10 +200,13 @@ func (c *ChainService) CleanupBlockOperations(ctx context.Context, block *pb.Bea // updates important checkpoints and local persistent data during epoch transitions. It serves as a wrapper // around the more low-level, core state transition function primitive. func (c *ChainService) runStateTransition( - headRoot [32]byte, block *pb.BeaconBlock, beaconState *pb.BeaconState, + ctx context.Context, + headRoot [32]byte, + block *pb.BeaconBlock, + beaconState *pb.BeaconState, ) (*pb.BeaconState, error) { newState, err := state.ExecuteStateTransition( - c.ctx, + ctx, beaconState, block, headRoot, @@ -231,7 +238,7 @@ func (c *ChainService) runStateTransition( return newState, fmt.Errorf("could not delete validator index: %v", err) } // Update FFG checkpoints in DB. - if err := c.updateFFGCheckPts(newState); err != nil { + if err := c.updateFFGCheckPts(ctx, newState); err != nil { return newState, fmt.Errorf("could not update FFG checkpts: %v", err) } // Save Historical States. diff --git a/beacon-chain/blockchain/block_processing_test.go b/beacon-chain/blockchain/block_processing_test.go index f865fd4453..fe0c96b328 100644 --- a/beacon-chain/blockchain/block_processing_test.go +++ b/beacon-chain/blockchain/block_processing_test.go @@ -262,7 +262,7 @@ func TestIsBlockReadyForProcessing_ValidBlock(t *testing.T) { ParentRootHash32: []byte{'a'}, } - if err := chainService.VerifyBlockValidity(block, beaconState); err == nil { + if err := chainService.VerifyBlockValidity(ctx, block, beaconState); err == nil { t.Fatal("block processing succeeded despite block having no parent saved") } @@ -312,7 +312,7 @@ func TestIsBlockReadyForProcessing_ValidBlock(t *testing.T) { }, } - if err := chainService.VerifyBlockValidity(block2, beaconState); err != nil { + if err := chainService.VerifyBlockValidity(ctx, block2, beaconState); err != nil { t.Fatalf("block processing failed despite being a valid block: %v", err) } } diff --git a/beacon-chain/blockchain/fork_choice.go b/beacon-chain/blockchain/fork_choice.go index 263d312aab..34d00c7d7b 100644 --- a/beacon-chain/blockchain/fork_choice.go +++ b/beacon-chain/blockchain/fork_choice.go @@ -32,7 +32,7 @@ type ForkChoice interface { // updateFFGCheckPts checks whether the existing FFG check points saved in DB // are not older than the ones just processed in state. If it's older, we update // the db with the latest FFG check points, both justification and finalization. -func (c *ChainService) updateFFGCheckPts(state *pb.BeaconState) error { +func (c *ChainService) updateFFGCheckPts(ctx context.Context, state *pb.BeaconState) error { lastJustifiedSlot := helpers.StartSlot(state.JustifiedEpoch) savedJustifiedBlock, err := c.beaconDB.JustifiedBlock() if err != nil { @@ -59,7 +59,7 @@ func (c *ChainService) updateFFGCheckPts(state *pb.BeaconState) error { } } // Generate the new justified state with using new justified block and save it. - newJustifiedState, err := stategenerator.GenerateStateFromBlock(c.ctx, c.beaconDB, lastJustifiedSlot) + newJustifiedState, err := stategenerator.GenerateStateFromBlock(ctx, c.beaconDB, lastJustifiedSlot) if err != nil { return err } @@ -99,7 +99,7 @@ func (c *ChainService) updateFFGCheckPts(state *pb.BeaconState) error { } // Generate the new finalized state with using new finalized block and save it. - newFinalizedState, err := stategenerator.GenerateStateFromBlock(c.ctx, c.beaconDB, lastFinalizedSlot) + newFinalizedState, err := stategenerator.GenerateStateFromBlock(ctx, c.beaconDB, lastFinalizedSlot) if err != nil { return err } @@ -130,7 +130,7 @@ func (c *ChainService) ApplyForkChoiceRule( if err != nil { return fmt.Errorf("could not retrieve justified state: %v", err) } - attestationTargets, err := c.attestationTargets(justifiedState) + attestationTargets, err := c.attestationTargets(ctx, justifiedState) if err != nil { return fmt.Errorf("could not retrieve attestation target: %v", err) } @@ -148,7 +148,7 @@ func (c *ChainService) ApplyForkChoiceRule( block.Slot-params.BeaconConfig().GenesisSlot, head.Slot-params.BeaconConfig().GenesisSlot) // Only regenerate head state if there was a reorg. - newState, err = stategenerator.GenerateStateFromBlock(c.ctx, c.beaconDB, head.Slot) + newState, err = stategenerator.GenerateStateFromBlock(ctx, c.beaconDB, head.Slot) if err != nil { return fmt.Errorf("could not gen state: %v", err) } @@ -276,11 +276,11 @@ func (c *ChainService) blockChildren(block *pb.BeaconBlock, highestSlot uint64) // attestationTargets retrieves the list of attestation targets since last finalized epoch, // each attestation target consists of validator index and its attestation target (i.e. the block // which the validator attested to) -func (c *ChainService) attestationTargets(state *pb.BeaconState) (map[uint64]*pb.BeaconBlock, error) { +func (c *ChainService) attestationTargets(ctx context.Context, state *pb.BeaconState) (map[uint64]*pb.BeaconBlock, error) { indices := helpers.ActiveValidatorIndices(state.ValidatorRegistry, helpers.CurrentEpoch(state)) attestationTargets := make(map[uint64]*pb.BeaconBlock) for i, index := range indices { - block, err := c.attsService.LatestAttestationTarget(c.ctx, index) + block, err := c.attsService.LatestAttestationTarget(ctx, index) if err != nil { return nil, fmt.Errorf("could not retrieve attestation target: %v", err) } diff --git a/beacon-chain/blockchain/fork_choice_test.go b/beacon-chain/blockchain/fork_choice_test.go index f8e8d51e3e..8e91444b64 100644 --- a/beacon-chain/blockchain/fork_choice_test.go +++ b/beacon-chain/blockchain/fork_choice_test.go @@ -227,7 +227,7 @@ func TestAttestationTargets_RetrieveWorks(t *testing.T) { attsService.InsertAttestationIntoStore(pubKey48, att) chainService := setupBeaconChain(t, beaconDB, attsService) - attestationTargets, err := chainService.attestationTargets(state) + attestationTargets, err := chainService.attestationTargets(ctx, state) if err != nil { t.Fatalf("Could not get attestation targets: %v", err) } @@ -1078,7 +1078,7 @@ func TestUpdateFFGCheckPts_NewJustifiedSlot(t *testing.T) { if err := chainSvc.beaconDB.UpdateChainHead(ctx, block, gState); err != nil { t.Fatal(err) } - if err := chainSvc.updateFFGCheckPts(gState); err != nil { + if err := chainSvc.updateFFGCheckPts(ctx, gState); err != nil { t.Fatal(err) } @@ -1161,7 +1161,7 @@ func TestUpdateFFGCheckPts_NewFinalizedSlot(t *testing.T) { if err := chainSvc.beaconDB.UpdateChainHead(ctx, block, gState); err != nil { t.Fatal(err) } - if err := chainSvc.updateFFGCheckPts(gState); err != nil { + if err := chainSvc.updateFFGCheckPts(ctx, gState); err != nil { t.Fatal(err) } @@ -1239,7 +1239,7 @@ func TestUpdateFFGCheckPts_NewJustifiedSkipSlot(t *testing.T) { if err := chainSvc.beaconDB.UpdateChainHead(ctx, block, gState); err != nil { t.Fatal(err) } - if err := chainSvc.updateFFGCheckPts(gState); err != nil { + if err := chainSvc.updateFFGCheckPts(ctx, gState); err != nil { t.Fatal(err) } diff --git a/beacon-chain/sync/initial-sync/service_test.go b/beacon-chain/sync/initial-sync/service_test.go index f2272fe8ea..ee87fd2ec5 100644 --- a/beacon-chain/sync/initial-sync/service_test.go +++ b/beacon-chain/sync/initial-sync/service_test.go @@ -74,7 +74,9 @@ func (ms *mockChainService) ApplyBlockStateTransition( } func (ms *mockChainService) VerifyBlockValidity( - block *pb.BeaconBlock, beaconState *pb.BeaconState, + ctx context.Context, + block *pb.BeaconBlock, + beaconState *pb.BeaconState, ) error { return nil } diff --git a/beacon-chain/sync/initial-sync/sync_blocks.go b/beacon-chain/sync/initial-sync/sync_blocks.go index ec32c502f7..34c3edd0e3 100644 --- a/beacon-chain/sync/initial-sync/sync_blocks.go +++ b/beacon-chain/sync/initial-sync/sync_blocks.go @@ -155,7 +155,7 @@ func (s *InitialSync) validateAndSaveNextBlock(ctx context.Context, block *pb.Be if err != nil { return err } - if err := s.chainService.VerifyBlockValidity(block, state); err != nil { + if err := s.chainService.VerifyBlockValidity(ctx, block, state); err != nil { return err } if err := s.db.SaveBlock(block); err != nil { diff --git a/beacon-chain/sync/regular_sync_test.go b/beacon-chain/sync/regular_sync_test.go index 8b6336f686..3bf642412a 100644 --- a/beacon-chain/sync/regular_sync_test.go +++ b/beacon-chain/sync/regular_sync_test.go @@ -76,7 +76,7 @@ func (ms *mockChainService) ApplyBlockStateTransition(ctx context.Context, block return &pb.BeaconState{}, nil } -func (ms *mockChainService) VerifyBlockValidity(block *pb.BeaconBlock, beaconState *pb.BeaconState) error { +func (ms *mockChainService) VerifyBlockValidity(ctx context.Context, block *pb.BeaconBlock, beaconState *pb.BeaconState) error { return nil }