diff --git a/beacon-chain/blockchain/block_processing.go b/beacon-chain/blockchain/block_processing.go index 8d5d5cd63..510877839 100644 --- a/beacon-chain/blockchain/block_processing.go +++ b/beacon-chain/blockchain/block_processing.go @@ -228,7 +228,7 @@ func (c *ChainService) runStateTransition( ).Info("Block transition successfully processed") // Save Historical States. - if err := c.beaconDB.SaveHistoricalState(beaconState); err != nil { + if err := c.beaconDB.SaveHistoricalState(ctx, beaconState); err != nil { return nil, fmt.Errorf("could not save historical state: %v", err) } } diff --git a/beacon-chain/blockchain/block_processing_test.go b/beacon-chain/blockchain/block_processing_test.go index d86356fd9..310e7c6f6 100644 --- a/beacon-chain/blockchain/block_processing_test.go +++ b/beacon-chain/blockchain/block_processing_test.go @@ -29,7 +29,7 @@ func TestReceiveBlock_FaultyPOWChain(t *testing.T) { chainService := setupBeaconChain(t, db, nil) unixTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 100) - if err := db.InitializeState(unixTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), unixTime, deposits, &pb.Eth1Data{}); err != nil { t.Fatalf("Could not initialize beacon state to disk: %v", err) } @@ -189,7 +189,7 @@ func TestReceiveBlock_RemovesPendingDeposits(t *testing.T) { } depositRoot := depositTrie.Root() beaconState.LatestEth1Data.DepositRootHash32 = depositRoot[:] - if err := db.SaveHistoricalState(beaconState); err != nil { + if err := db.SaveHistoricalState(context.Background(), beaconState); err != nil { t.Fatal(err) } @@ -231,7 +231,7 @@ func TestReceiveBlock_RemovesPendingDeposits(t *testing.T) { if err := chainService.beaconDB.SaveState(ctx, beaconState); err != nil { t.Fatal(err) } - if err := db.SaveHistoricalState(beaconState); err != nil { + if err := db.SaveHistoricalState(context.Background(), beaconState); err != nil { t.Fatal(err) } computedState, err := chainService.ReceiveBlock(context.Background(), block) @@ -420,7 +420,7 @@ func TestIsBlockReadyForProcessing_ValidBlock(t *testing.T) { chainService := setupBeaconChain(t, db, nil) unixTime := uint64(time.Now().Unix()) deposits, privKeys := setupInitialDeposits(t, 100) - if err := db.InitializeState(unixTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), unixTime, deposits, &pb.Eth1Data{}); err != nil { t.Fatalf("Could not initialize beacon state to disk: %v", err) } beaconState, err := db.HeadState(ctx) diff --git a/beacon-chain/blockchain/fork_choice_test.go b/beacon-chain/blockchain/fork_choice_test.go index 8c47262a3..9f0c65050 100644 --- a/beacon-chain/blockchain/fork_choice_test.go +++ b/beacon-chain/blockchain/fork_choice_test.go @@ -96,7 +96,7 @@ func TestApplyForkChoice_SetsCanonicalHead(t *testing.T) { } unixTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 100) - if err := db.InitializeState(unixTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), unixTime, deposits, &pb.Eth1Data{}); err != nil { t.Fatalf("Could not initialize beacon state to disk: %v", err) } diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 612a2648f..d2c30af54 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -129,7 +129,7 @@ func (c *ChainService) initializeBeaconChain(genesisTime time.Time, deposits []* log.Info("ChainStart time reached, starting the beacon chain!") c.genesisTime = genesisTime unixTime := uint64(genesisTime.Unix()) - if err := c.beaconDB.InitializeState(unixTime, deposits, eth1data); err != nil { + if err := c.beaconDB.InitializeState(c.ctx, unixTime, deposits, eth1data); err != nil { return nil, fmt.Errorf("could not initialize beacon state to disk: %v", err) } beaconState, err := c.beaconDB.HeadState(c.ctx) diff --git a/beacon-chain/blockchain/service_test.go b/beacon-chain/blockchain/service_test.go index c1573bb22..d3616fb2e 100644 --- a/beacon-chain/blockchain/service_test.go +++ b/beacon-chain/blockchain/service_test.go @@ -309,7 +309,7 @@ func TestChainStartStop_Initialized(t *testing.T) { unixTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 100) - if err := db.InitializeState(unixTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), unixTime, deposits, &pb.Eth1Data{}); err != nil { t.Fatalf("Could not initialize beacon state to disk: %v", err) } beaconState, err := db.HeadState(ctx) diff --git a/beacon-chain/blockchain/stategenerator/state_generator_test.go b/beacon-chain/blockchain/stategenerator/state_generator_test.go index e0f80cfa8..bd4dd3ef7 100644 --- a/beacon-chain/blockchain/stategenerator/state_generator_test.go +++ b/beacon-chain/blockchain/stategenerator/state_generator_test.go @@ -176,7 +176,7 @@ func TestGenerateState_NilLatestFinalizedBlock(t *testing.T) { if err := beaconDB.SaveFinalizedState(beaconState); err != nil { t.Fatalf("Unable to save finalized state") } - if err := beaconDB.SaveHistoricalState(beaconState); err != nil { + if err := beaconDB.SaveHistoricalState(context.Background(), beaconState); err != nil { t.Fatalf("Unable to save finalized state") } diff --git a/beacon-chain/db/block_test.go b/beacon-chain/db/block_test.go index 147b0ee5d..7eadf0d75 100644 --- a/beacon-chain/db/block_test.go +++ b/beacon-chain/db/block_test.go @@ -92,7 +92,7 @@ func TestUpdateChainHead_NoBlock(t *testing.T) { genesisTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 10) - err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}) + err := db.InitializeState(context.Background(), genesisTime, deposits, &pb.Eth1Data{}) if err != nil { t.Fatalf("failed to initialize state: %v", err) } @@ -114,7 +114,7 @@ func TestUpdateChainHead_OK(t *testing.T) { genesisTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 10) - err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}) + err := db.InitializeState(context.Background(), genesisTime, deposits, &pb.Eth1Data{}) if err != nil { t.Fatalf("failed to initialize state: %v", err) } @@ -181,7 +181,7 @@ func TestChainProgress_OK(t *testing.T) { genesisTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 10) - err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}) + err := db.InitializeState(context.Background(), genesisTime, deposits, &pb.Eth1Data{}) if err != nil { t.Fatalf("failed to initialize state: %v", err) } diff --git a/beacon-chain/db/state.go b/beacon-chain/db/state.go index b20599514..faf621415 100644 --- a/beacon-chain/db/state.go +++ b/beacon-chain/db/state.go @@ -26,7 +26,10 @@ var ( // InitializeState creates an initial genesis state for the beacon // node using a set of genesis validators. -func (db *BeaconDB) InitializeState(genesisTime uint64, deposits []*pb.Deposit, eth1Data *pb.Eth1Data) error { +func (db *BeaconDB) InitializeState(ctx context.Context, genesisTime uint64, deposits []*pb.Deposit, eth1Data *pb.Eth1Data) error { + ctx, span := trace.StartSpan(ctx, "BeaconDB.InitializeState") + defer span.End() + beaconState, err := genesis.BeaconState(deposits, genesisTime, eth1Data) if err != nil { return err @@ -44,7 +47,7 @@ func (db *BeaconDB) InitializeState(genesisTime uint64, deposits []*pb.Deposit, db.currentState = beaconState - if err := db.SaveHistoricalState(beaconState); err != nil { + if err := db.SaveState(ctx, beaconState); err != nil { return err } @@ -86,7 +89,7 @@ func (db *BeaconDB) InitializeState(genesisTime uint64, deposits []*pb.Deposit, // HeadState fetches the canonical beacon chain's head state from the DB. func (db *BeaconDB) HeadState(ctx context.Context) (*pb.BeaconState, error) { - ctx, span := trace.StartSpan(ctx, "BeaconDB.State") + ctx, span := trace.StartSpan(ctx, "BeaconDB.HeadState") defer span.End() ctx, lockSpan := trace.StartSpan(ctx, "BeaconDB.stateLock.Lock") @@ -141,7 +144,7 @@ func (db *BeaconDB) SaveState(ctx context.Context, beaconState *pb.BeaconState) db.currentState = currentState cloneSpan.End() - if err := db.SaveHistoricalState(beaconState); err != nil { + if err := db.SaveHistoricalState(ctx, beaconState); err != nil { return err } @@ -199,7 +202,9 @@ func (db *BeaconDB) SaveFinalizedState(beaconState *pb.BeaconState) error { } // SaveHistoricalState saves the last finalized state in the db. -func (db *BeaconDB) SaveHistoricalState(beaconState *pb.BeaconState) error { +func (db *BeaconDB) SaveHistoricalState(ctx context.Context, beaconState *pb.BeaconState) error { + ctx, span := trace.StartSpan(ctx, "beacon-chain.db.SaveHistoricalState") + defer span.End() slotBinary := encodeSlotNumber(beaconState.Slot) stateHash, err := hashutil.HashProto(beaconState) diff --git a/beacon-chain/db/state_test.go b/beacon-chain/db/state_test.go index 29e547ac7..5ac1f598e 100644 --- a/beacon-chain/db/state_test.go +++ b/beacon-chain/db/state_test.go @@ -51,7 +51,7 @@ func TestInitializeState_OK(t *testing.T) { genesisTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 10) - if err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), genesisTime, deposits, &pb.Eth1Data{}); err != nil { t.Fatalf("Failed to initialize state: %v", err) } b, err := db.ChainHead() @@ -94,7 +94,7 @@ func TestFinalizeState_OK(t *testing.T) { genesisTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 10) - if err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), genesisTime, deposits, &pb.Eth1Data{}); err != nil { t.Fatalf("Failed to initialize state: %v", err) } @@ -124,7 +124,7 @@ func BenchmarkState_ReadingFromCache(b *testing.B) { genesisTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(b, 10) - if err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), genesisTime, deposits, &pb.Eth1Data{}); err != nil { b.Fatalf("Failed to initialize state: %v", err) } @@ -263,7 +263,7 @@ func TestHistoricalState_CanSaveRetrieve(t *testing.T) { if err := db.SaveFinalizedState(tt.state); err != nil { t.Fatalf("could not save finalized state: %v", err) } - if err := db.SaveHistoricalState(tt.state); err != nil { + if err := db.SaveHistoricalState(context.Background(), tt.state); err != nil { t.Fatalf("could not save historical state: %v", err) } @@ -343,10 +343,10 @@ func TestHistoricalState_Pruning(t *testing.T) { } for _, tt := range tests { - if err := db.SaveHistoricalState(tt.histState1); err != nil { + if err := db.SaveHistoricalState(context.Background(), tt.histState1); err != nil { t.Fatalf("could not save historical state: %v", err) } - if err := db.SaveHistoricalState(tt.histState2); err != nil { + if err := db.SaveHistoricalState(context.Background(), tt.histState2); err != nil { t.Fatalf("could not save historical state: %v", err) } @@ -356,7 +356,7 @@ func TestHistoricalState_Pruning(t *testing.T) { } // Save a dummy genesis state so that db doesnt return an error. - if err := db.SaveHistoricalState(&pb.BeaconState{Slot: slotGen(0), FinalizedEpoch: 1}); err != nil { + if err := db.SaveHistoricalState(context.Background(), &pb.BeaconState{Slot: slotGen(0), FinalizedEpoch: 1}); err != nil { t.Fatalf("could not save historical state: %v", err) } diff --git a/beacon-chain/rpc/attester_server_test.go b/beacon-chain/rpc/attester_server_test.go index 11e08462d..7413177bd 100644 --- a/beacon-chain/rpc/attester_server_test.go +++ b/beacon-chain/rpc/attester_server_test.go @@ -85,7 +85,7 @@ func TestAttestationDataAtSlot_JustifiedBlockFailure(t *testing.T) { if err := attesterServer.beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { t.Fatalf("Could not update chain head in test db: %v", err) } - if err := attesterServer.beaconDB.SaveHistoricalState(finalizedState); err != nil { + if err := attesterServer.beaconDB.SaveHistoricalState(context.Background(), finalizedState); err != nil { t.Fatalf("Could not save historical state in test db: %v", err) } epochBoundaryBlock := &pbp2p.BeaconBlock{ diff --git a/beacon-chain/rpc/proposer_server.go b/beacon-chain/rpc/proposer_server.go index bb760aa7a..65889fb70 100644 --- a/beacon-chain/rpc/proposer_server.go +++ b/beacon-chain/rpc/proposer_server.go @@ -65,7 +65,7 @@ func (ps *ProposerServer) ProposeBlock(ctx context.Context, blk *pbp2p.BeaconBlo } log.WithField("headRoot", fmt.Sprintf("0x%x", h)).Info("Chain head block and state updated") - if err := ps.beaconDB.SaveHistoricalState(beaconState); err != nil { + if err := ps.beaconDB.SaveHistoricalState(ctx, beaconState); err != nil { log.Errorf("Could not save new historical state: %v", err) } return &pb.ProposeResponse{BlockRootHash32: h[:]}, nil diff --git a/beacon-chain/sync/initial-sync/service_test.go b/beacon-chain/sync/initial-sync/service_test.go index 2309cb0aa..2929e950c 100644 --- a/beacon-chain/sync/initial-sync/service_test.go +++ b/beacon-chain/sync/initial-sync/service_test.go @@ -93,7 +93,7 @@ func setUpGenesisStateAndBlock(beaconDB *db.BeaconDB, t *testing.T) { ctx := context.Background() genesisTime := time.Now() unixTime := uint64(genesisTime.Unix()) - if err := beaconDB.InitializeState(unixTime, []*pb.Deposit{}, nil); err != nil { + if err := beaconDB.InitializeState(context.Background(), unixTime, []*pb.Deposit{}, nil); err != nil { t.Fatalf("could not initialize beacon state to disk: %v", err) } beaconState, err := beaconDB.HeadState(ctx) diff --git a/beacon-chain/sync/initial-sync/sync_state.go b/beacon-chain/sync/initial-sync/sync_state.go index cb81a393c..53c4bf78f 100644 --- a/beacon-chain/sync/initial-sync/sync_state.go +++ b/beacon-chain/sync/initial-sync/sync_state.go @@ -22,7 +22,7 @@ func (s *InitialSync) processState(msg p2p.Message) { return } - if err := s.db.SaveHistoricalState(finalizedState); err != nil { + if err := s.db.SaveHistoricalState(ctx, finalizedState); err != nil { log.Errorf("Could not save new historical state: %v", err) return } diff --git a/beacon-chain/sync/regular_sync_test.go b/beacon-chain/sync/regular_sync_test.go index 793e0b1bf..238f7029c 100644 --- a/beacon-chain/sync/regular_sync_test.go +++ b/beacon-chain/sync/regular_sync_test.go @@ -167,7 +167,7 @@ func TestProcessBlock_OK(t *testing.T) { } genesisTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 10) - if err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), genesisTime, deposits, &pb.Eth1Data{}); err != nil { t.Fatalf("Failed to initialize state: %v", err) } @@ -242,7 +242,7 @@ func TestProcessBlock_MultipleBlocksProcessedOK(t *testing.T) { } genesisTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 10) - if err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), genesisTime, deposits, &pb.Eth1Data{}); err != nil { t.Fatal(err) } @@ -654,7 +654,7 @@ func TestHandleStateReq_NOState(t *testing.T) { genesisTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 10) - if err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), genesisTime, deposits, &pb.Eth1Data{}); err != nil { t.Fatalf("Failed to initialize state: %v", err) } @@ -684,7 +684,7 @@ func TestHandleStateReq_OK(t *testing.T) { genesisTime := time.Now() unixTime := uint64(genesisTime.Unix()) - if err := db.InitializeState(unixTime, []*pb.Deposit{}, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), unixTime, []*pb.Deposit{}, &pb.Eth1Data{}); err != nil { t.Fatalf("could not initialize beacon state to disk: %v", err) } beaconState, err := db.HeadState(ctx) diff --git a/beacon-chain/sync/service_test.go b/beacon-chain/sync/service_test.go index 41202e755..631ed9b87 100644 --- a/beacon-chain/sync/service_test.go +++ b/beacon-chain/sync/service_test.go @@ -72,7 +72,7 @@ func setupTestSyncService(t *testing.T, synced bool) (*Service, *db.BeaconDB) { unixTime := uint64(time.Now().Unix()) deposits, _ := setupInitialDeposits(t, 10) - if err := db.InitializeState(unixTime, deposits, &pb.Eth1Data{}); err != nil { + if err := db.InitializeState(context.Background(), unixTime, deposits, &pb.Eth1Data{}); err != nil { t.Fatalf("Failed to initialize state: %v", err) }