mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-11 12:10:05 +00:00
Save State during Init Genesis State (#2193)
* added spans and save state during init state * fix other svcs * tests * Delete saving hist state
This commit is contained in:
parent
1ff3d7ea66
commit
83e5492e55
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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")
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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{
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user