diff --git a/beacon-chain/attestation/service.go b/beacon-chain/attestation/service.go index 85effe009d..162076624f 100644 --- a/beacon-chain/attestation/service.go +++ b/beacon-chain/attestation/service.go @@ -37,7 +37,7 @@ type attestationStore struct { type Service struct { ctx context.Context cancel context.CancelFunc - beaconDB *db.BeaconDB + beaconDB db.Database incomingFeed *event.Feed incomingChan chan *ethpb.Attestation // store is the mapping of individual @@ -49,7 +49,7 @@ type Service struct { // Config options for the service. type Config struct { - BeaconDB *db.BeaconDB + BeaconDB db.Database } // NewAttestationService instantiates a new service instance that will @@ -118,11 +118,12 @@ func (a *Service) LatestAttestationTarget(beaconState *pb.BeaconState, index uin return nil, nil } targetRoot := bytesutil.ToBytes32(attestation.Data.BeaconBlockRoot) - if !a.beaconDB.HasBlock(targetRoot) { + if !a.beaconDB.HasBlock(context.TODO(), targetRoot) { return nil, nil } - return a.beaconDB.AttestationTarget(targetRoot) + // TODO(3219): remove after fork choice service changes. + return a.beaconDB.(*db.BeaconDB).AttestationTarget(targetRoot) } // attestationPool takes an newly received attestation from sync service @@ -133,7 +134,7 @@ func (a *Service) attestationPool() { for { select { case <-a.ctx.Done(): - log.Debug("Attestation pool closed, exiting goroutine") + log.Debug("AttestationDeprecated pool closed, exiting goroutine") return // Listen for a newly received incoming attestation from the sync service. case attestations := <-a.incomingChan: diff --git a/beacon-chain/attestation/service_test.go b/beacon-chain/attestation/service_test.go index 8472eb27a3..ac6da19e32 100644 --- a/beacon-chain/attestation/service_test.go +++ b/beacon-chain/attestation/service_test.go @@ -47,7 +47,7 @@ func TestUpdateLatestAttestation_UpdatesLatest(t *testing.T) { block := ðpb.BeaconBlock{ Slot: 1, } - if err := beaconDB.SaveBlock(block); err != nil { + if err := beaconDB.SaveBlockDeprecated(block); err != nil { t.Fatal(err) } if err := beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { @@ -117,7 +117,7 @@ func TestAttestationPool_UpdatesAttestationPool(t *testing.T) { block := ðpb.BeaconBlock{ Slot: 1, } - if err := beaconDB.SaveBlock(block); err != nil { + if err := beaconDB.SaveBlockDeprecated(block); err != nil { t.Fatal(err) } if err := beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { @@ -144,7 +144,7 @@ func TestLatestAttestationTarget_CantGetAttestation(t *testing.T) { defer internal.TeardownDBDeprecated(t, beaconDB) ctx := context.Background() - if err := beaconDB.SaveState(ctx, &pb.BeaconState{ + if err := beaconDB.SaveStateDeprecated(ctx, &pb.BeaconState{ Validators: []*ethpb.Validator{{}}, }); err != nil { t.Fatalf("could not save state: %v", err) @@ -168,14 +168,14 @@ func TestLatestAttestationTarget_ReturnsLatestAttestedBlock(t *testing.T) { ctx := context.Background() pubKey := []byte{'A'} - if err := beaconDB.SaveState(ctx, &pb.BeaconState{ + if err := beaconDB.SaveStateDeprecated(ctx, &pb.BeaconState{ Validators: []*ethpb.Validator{{PublicKey: pubKey}}, }); err != nil { t.Fatalf("could not save state: %v", err) } block := ðpb.BeaconBlock{Slot: 999} - if err := beaconDB.SaveBlock(block); err != nil { + if err := beaconDB.SaveBlockDeprecated(block); err != nil { t.Fatalf("could not save block: %v", err) } blockRoot, err := ssz.SigningRoot(block) @@ -238,7 +238,7 @@ func TestUpdateLatestAttestation_InvalidIndex(t *testing.T) { block := ðpb.BeaconBlock{ Slot: 1, } - if err := beaconDB.SaveBlock(block); err != nil { + if err := beaconDB.SaveBlockDeprecated(block); err != nil { t.Fatal(err) } if err := beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { @@ -292,7 +292,7 @@ func TestBatchUpdate_FromSync(t *testing.T) { block := ðpb.BeaconBlock{ Slot: 1, } - if err := beaconDB.SaveBlock(block); err != nil { + if err := beaconDB.SaveBlockDeprecated(block); err != nil { t.Fatal(err) } if err := beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { @@ -343,7 +343,7 @@ func TestUpdateLatestAttestation_BatchUpdate(t *testing.T) { block := ðpb.BeaconBlock{ Slot: 1, } - if err := beaconDB.SaveBlock(block); err != nil { + if err := beaconDB.SaveBlockDeprecated(block); err != nil { t.Fatal(err) } if err := beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { diff --git a/beacon-chain/blockchain/BUILD.bazel b/beacon-chain/blockchain/BUILD.bazel index b232d976ef..20e4b4074b 100644 --- a/beacon-chain/blockchain/BUILD.bazel +++ b/beacon-chain/blockchain/BUILD.bazel @@ -12,6 +12,7 @@ go_library( deps = [ "//beacon-chain/attestation:go_default_library", "//beacon-chain/cache:go_default_library", + "//beacon-chain/cache/depositcache:go_default_library", "//beacon-chain/core/blocks:go_default_library", "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/state:go_default_library", @@ -47,6 +48,7 @@ go_test( deps = [ "//beacon-chain/attestation:go_default_library", "//beacon-chain/cache:go_default_library", + "//beacon-chain/cache/depositcache:go_default_library", "//beacon-chain/core/blocks:go_default_library", "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/state:go_default_library", diff --git a/beacon-chain/blockchain/block_processing.go b/beacon-chain/blockchain/block_processing.go index f9bf20553e..a323b43933 100644 --- a/beacon-chain/blockchain/block_processing.go +++ b/beacon-chain/blockchain/block_processing.go @@ -10,6 +10,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" + "github.com/prysmaticlabs/prysm/beacon-chain/db" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bytesutil" @@ -56,15 +57,21 @@ func (c *ChainService) ReceiveBlockDeprecated(ctx context.Context, block *ethpb. defer c.receiveBlockLock.Unlock() ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveBlock") defer span.End() + // TODO(3219): Fix with new fork choice service. + db, isLegacyDB := c.beaconDB.(*db.BeaconDB) + if !isLegacyDB { + panic("Deprecated receive block only works with deprecated database impl.") + } + parentRoot := bytesutil.ToBytes32(block.ParentRoot) - parent, err := c.beaconDB.Block(parentRoot) + parent, err := db.BlockDeprecated(parentRoot) if err != nil { return nil, errors.Wrap(err, "failed to get parent block") } if parent == nil { return nil, errors.New("parent does not exist in DB") } - beaconState, err := c.beaconDB.HistoricalStateFromSlot(ctx, parent.Slot, parentRoot) + beaconState, err := db.HistoricalStateFromSlot(ctx, parent.Slot, parentRoot) if err != nil { return nil, errors.Wrap(err, "could not retrieve beacon state") } @@ -94,8 +101,8 @@ func (c *ChainService) ReceiveBlockDeprecated(ctx context.Context, block *ethpb. switch err.(type) { case *BlockFailedProcessingErr: // If the block fails processing, we mark it as blacklisted and delete it from our DB. - c.beaconDB.MarkEvilBlockHash(blockRoot) - if err := c.beaconDB.DeleteBlock(block); err != nil { + db.MarkEvilBlockHash(blockRoot) + if err := db.DeleteBlockDeprecated(block); err != nil { return nil, errors.Wrap(err, "could not delete bad block from db") } return beaconState, err @@ -155,15 +162,19 @@ func (c *ChainService) SaveAndBroadcastBlock(ctx context.Context, block *ethpb.B if err != nil { return errors.Wrap(err, "could not tree hash incoming block") } - if err := c.beaconDB.SaveBlock(block); err != nil { + if err := c.beaconDB.SaveBlock(ctx, block); err != nil { return errors.Wrap(err, "failed to save block") } - if err := c.beaconDB.SaveAttestationTarget(ctx, &pb.AttestationTarget{ - Slot: block.Slot, - BeaconBlockRoot: blockRoot[:], - ParentRoot: block.ParentRoot, - }); err != nil { - return errors.Wrap(err, "failed to save attestation target") + // TODO(3219): Update after new fork choice service. + db, isLegacyDB := c.beaconDB.(*db.BeaconDB) + if isLegacyDB { + if err := db.SaveAttestationTarget(ctx, &pb.AttestationTarget{ + Slot: block.Slot, + BeaconBlockRoot: blockRoot[:], + ParentRoot: block.ParentRoot, + }); err != nil { + return errors.Wrap(err, "failed to save attestation target") + } } // Announce the new block to the network. c.p2p.Broadcast(ctx, &pb.BeaconBlockAnnounce{ @@ -189,7 +200,7 @@ func (c *ChainService) CleanupBlockOperations(ctx context.Context, block *ethpb. // Remove pending deposits from the deposit queue. for _, dep := range block.Body.Deposits { - c.beaconDB.DepositCache.RemovePendingDeposit(ctx, dep) + c.depositCache.RemovePendingDeposit(ctx, dep) } return nil } @@ -214,7 +225,7 @@ func (c *ChainService) AdvanceStateDeprecated( // Prune the block cache and helper caches on every new finalized epoch. if newState.FinalizedCheckpoint.Epoch > finalizedEpoch { helpers.ClearAllCaches() - c.beaconDB.ClearBlockCache() + c.beaconDB.(*db.BeaconDB).ClearBlockCache() } log.WithField( @@ -231,18 +242,18 @@ func (c *ChainService) AdvanceStateDeprecated( return nil, err } // Save Historical States. - if err := c.beaconDB.SaveHistoricalState(ctx, beaconState, blockRoot); err != nil { + if err := c.beaconDB.(*db.BeaconDB).SaveHistoricalState(ctx, beaconState, blockRoot); err != nil { return nil, errors.Wrap(err, "could not save historical state") } } if helpers.IsEpochStart(newState.Slot) { // Save activated validators of this epoch to public key -> index DB. - if err := c.saveValidatorIdx(newState); err != nil { + if err := c.saveValidatorIdx(ctx, newState); err != nil { return newState, errors.Wrap(err, "could not save validator index") } // Delete exited validators of this epoch to public key -> index DB. - if err := c.deleteValidatorIdx(newState); err != nil { + if err := c.deleteValidatorIdx(ctx, newState); err != nil { return newState, errors.Wrap(err, "could not delete validator index") } // Update FFG checkpoints in DB. @@ -257,7 +268,7 @@ func (c *ChainService) AdvanceStateDeprecated( // saveValidatorIdx saves the validators public key to index mapping in DB, these // validators were activated from current epoch. After it saves, current epoch key // is deleted from ActivatedValidators mapping. -func (c *ChainService) saveValidatorIdx(state *pb.BeaconState) error { +func (c *ChainService) saveValidatorIdx(ctx context.Context, state *pb.BeaconState) error { nextEpoch := helpers.CurrentEpoch(state) + 1 activatedValidators := validators.ActivatedValFromEpoch(nextEpoch) var idxNotInState []uint64 @@ -269,7 +280,7 @@ func (c *ChainService) saveValidatorIdx(state *pb.BeaconState) error { continue } pubKey := state.Validators[idx].PublicKey - if err := c.beaconDB.SaveValidatorIndex(pubKey, int(idx)); err != nil { + if err := c.beaconDB.SaveValidatorIndex(ctx, bytesutil.ToBytes48(pubKey), idx); err != nil { return errors.Wrap(err, "could not save validator index") } } @@ -283,11 +294,11 @@ func (c *ChainService) saveValidatorIdx(state *pb.BeaconState) error { // deleteValidatorIdx deletes the validators public key to index mapping in DB, the // validators were exited from current epoch. After it deletes, current epoch key // is deleted from ExitedValidators mapping. -func (c *ChainService) deleteValidatorIdx(state *pb.BeaconState) error { +func (c *ChainService) deleteValidatorIdx(ctx context.Context, state *pb.BeaconState) error { exitedValidators := validators.ExitedValFromEpoch(helpers.CurrentEpoch(state) + 1) for _, idx := range exitedValidators { pubKey := state.Validators[idx].PublicKey - if err := c.beaconDB.DeleteValidatorIndex(pubKey); err != nil { + if err := c.beaconDB.DeleteValidatorIndex(ctx, bytesutil.ToBytes48(pubKey)); err != nil { return errors.Wrap(err, "could not delete validator index") } } diff --git a/beacon-chain/blockchain/block_processing_test.go b/beacon-chain/blockchain/block_processing_test.go index 087dca0bef..ae1d8cd575 100644 --- a/beacon-chain/blockchain/block_processing_test.go +++ b/beacon-chain/blockchain/block_processing_test.go @@ -15,6 +15,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" + db2 "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/internal" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" @@ -29,11 +30,11 @@ var _ = BlockProcessor(&ChainService{}) func initBlockStateRoot(t *testing.T, block *ethpb.BeaconBlock, chainService *ChainService) (*ethpb.BeaconBlock, error) { parentRoot := bytesutil.ToBytes32(block.ParentRoot) - parent, err := chainService.beaconDB.Block(parentRoot) + parent, err := chainService.beaconDB.Block(context.Background(), parentRoot) if err != nil { return nil, err } - beaconState, err := chainService.beaconDB.HistoricalStateFromSlot(context.Background(), parent.Slot, parentRoot) + beaconState, err := chainService.beaconDB.(*db2.BeaconDB).HistoricalStateFromSlot(context.Background(), parent.Slot, parentRoot) if err != nil { return nil, err } @@ -56,6 +57,8 @@ func initBlockStateRoot(t *testing.T, block *ethpb.BeaconBlock, chainService *Ch func TestReceiveBlock_FaultyPOWChain(t *testing.T) { db := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, db) + ctx := context.Background() + chainService := setupBeaconChain(t, db, nil) unixTime := uint64(time.Now().Unix()) deposits, _ := testutil.SetupInitialDeposits(t, 100) @@ -76,7 +79,7 @@ func TestReceiveBlock_FaultyPOWChain(t *testing.T) { t.Fatalf("Unable to tree hash block %v", err) } - if err := chainService.beaconDB.SaveBlock(parentBlock); err != nil { + if err := chainService.beaconDB.SaveBlock(ctx, parentBlock); err != nil { t.Fatalf("Unable to save block %v", err) } @@ -91,7 +94,7 @@ func TestReceiveBlock_FaultyPOWChain(t *testing.T) { }, } - if err := chainService.beaconDB.SaveBlock(block); err != nil { + if err := chainService.beaconDB.SaveBlock(ctx, block); err != nil { t.Fatal(err) } if _, err := chainService.ReceiveBlockDeprecated(context.Background(), block); err == nil { @@ -122,7 +125,7 @@ func TestReceiveBlock_ProcessCorrectly(t *testing.T) { BodyRoot: bodyRoot[:], } beaconState.Eth1DepositIndex = 100 - if err := chainService.beaconDB.SaveBlock(genesis); err != nil { + if err := chainService.beaconDB.SaveBlock(ctx, genesis); err != nil { t.Fatalf("Could not save block to db: %v", err) } parentRoot, err := ssz.SigningRoot(genesis) @@ -134,7 +137,7 @@ func TestReceiveBlock_ProcessCorrectly(t *testing.T) { t.Fatal(err) } - if err := chainService.beaconDB.UpdateChainHead(ctx, genesis, beaconState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, genesis, beaconState); err != nil { t.Fatal(err) } @@ -177,17 +180,17 @@ func TestReceiveBlock_ProcessCorrectly(t *testing.T) { t.Error(err) } - if err := chainService.beaconDB.SaveJustifiedBlock(block); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveJustifiedBlock(block); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveFinalizedBlock(block); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveFinalizedBlock(block); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveBlock(block); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { t.Fatal(err) } if _, err := chainService.ReceiveBlockDeprecated(context.Background(), block); err != nil { - t.Errorf("Block failed processing: %v", err) + t.Errorf("BlockDeprecated failed processing: %v", err) } testutil.AssertLogsContain(t, hook, "Finished processing beacon block") } @@ -218,10 +221,10 @@ func TestReceiveBlock_UsesParentBlockState(t *testing.T) { beaconState.Eth1DepositIndex = 100 parentHash, genesisBlock := setupGenesisBlock(t, chainService) - if err := chainService.beaconDB.UpdateChainHead(ctx, genesisBlock, beaconState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, genesisBlock, beaconState); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveHistoricalState(ctx, beaconState, parentHash); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveHistoricalState(ctx, beaconState, parentHash); err != nil { t.Fatal(err) } parentRoot, err := ssz.SigningRoot(beaconState.LatestBlockHeader) @@ -268,7 +271,7 @@ func TestReceiveBlock_UsesParentBlockState(t *testing.T) { t.Error(err) } - if err := chainService.beaconDB.SaveBlock(block); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { t.Fatal(err) } if _, err := chainService.ReceiveBlockDeprecated(context.Background(), block); err != nil { @@ -304,10 +307,10 @@ func TestReceiveBlock_DeletesBadBlock(t *testing.T) { } parentHash, genesisBlock := setupGenesisBlock(t, chainService) - if err := chainService.beaconDB.UpdateChainHead(ctx, genesisBlock, beaconState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, genesisBlock, beaconState); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveHistoricalState(ctx, beaconState, parentHash); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveHistoricalState(ctx, beaconState, parentHash); err != nil { t.Fatal(err) } @@ -348,7 +351,7 @@ func TestReceiveBlock_DeletesBadBlock(t *testing.T) { t.Errorf("Expected block processing to fail, received: %v", err) } - savedBlock, err := db.Block(blockRoot) + savedBlock, err := db.BlockDeprecated(blockRoot) if err != nil { t.Fatal(err) } @@ -389,11 +392,11 @@ func TestReceiveBlock_CheckBlockStateRoot_GoodState(t *testing.T) { BodyRoot: bodyRoot[:], } parentHash, genesisBlock := setupGenesisBlock(t, chainService) - if err := chainService.beaconDB.SaveHistoricalState(ctx, beaconState, parentHash); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveHistoricalState(ctx, beaconState, parentHash); err != nil { t.Fatal(err) } beaconState.Slot++ - if err := chainService.beaconDB.UpdateChainHead(ctx, genesisBlock, beaconState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, genesisBlock, beaconState); err != nil { t.Fatal(err) } @@ -430,7 +433,7 @@ func TestReceiveBlock_CheckBlockStateRoot_GoodState(t *testing.T) { t.Error(err) } - if err := chainService.beaconDB.SaveBlock(goodStateBlock); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(goodStateBlock); err != nil { t.Fatal(err) } @@ -464,11 +467,11 @@ func TestReceiveBlock_CheckBlockStateRoot_BadState(t *testing.T) { BodyRoot: bodyRoot[:], } parentHash, genesisBlock := setupGenesisBlock(t, chainService) - if err := chainService.beaconDB.SaveHistoricalState(ctx, beaconState, parentHash); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveHistoricalState(ctx, beaconState, parentHash); err != nil { t.Fatal(err) } beaconState.Slot++ - if err := chainService.beaconDB.UpdateChainHead(ctx, genesisBlock, beaconState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, genesisBlock, beaconState); err != nil { t.Fatal(err) } @@ -535,7 +538,7 @@ func TestReceiveBlock_RemovesPendingDeposits(t *testing.T) { } beaconState.Eth1Data.DepositCount = 1 beaconState.Eth1DepositIndex = 0 - if err := chainService.beaconDB.SaveJustifiedState(beaconState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveJustifiedState(beaconState); err != nil { t.Fatal(err) } if err := db.SaveFinalizedState(beaconState); err != nil { @@ -548,7 +551,7 @@ func TestReceiveBlock_RemovesPendingDeposits(t *testing.T) { } parentHash, genesisBlock := setupGenesisBlock(t, chainService) beaconState.Slot++ - if err := chainService.beaconDB.UpdateChainHead(ctx, genesisBlock, beaconState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, genesisBlock, beaconState); err != nil { t.Fatal(err) } @@ -611,7 +614,7 @@ func TestReceiveBlock_RemovesPendingDeposits(t *testing.T) { beaconState.Slot-- beaconState.Eth1DepositIndex = 0 - if err := chainService.beaconDB.SaveState(ctx, beaconState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } block, err = initBlockStateRoot(t, block, chainService) @@ -628,23 +631,23 @@ func TestReceiveBlock_RemovesPendingDeposits(t *testing.T) { t.Error(err) } - if err := chainService.beaconDB.SaveJustifiedBlock(block); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveJustifiedBlock(block); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveFinalizedBlock(block); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveFinalizedBlock(block); err != nil { t.Fatal(err) } for _, dep := range pendingDeposits { - db.DepositCache.InsertPendingDeposit(chainService.ctx, dep, big.NewInt(0), 0, [32]byte{}) + chainService.depositCache.InsertPendingDeposit(chainService.ctx, dep, big.NewInt(0), 0, [32]byte{}) } - if len(db.DepositCache.PendingDeposits(chainService.ctx, nil)) != len(pendingDeposits) || len(pendingDeposits) == 0 { + if len(chainService.depositCache.PendingDeposits(chainService.ctx, nil)) != len(pendingDeposits) || len(pendingDeposits) == 0 { t.Fatalf("Expected %d pending deposits", len(pendingDeposits)) } beaconState.Slot-- - if err := chainService.beaconDB.SaveState(ctx, beaconState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } if err := db.SaveHistoricalState(context.Background(), beaconState, blockRoot); err != nil { @@ -666,8 +669,8 @@ func TestReceiveBlock_RemovesPendingDeposits(t *testing.T) { t.Fatal(err) } - if len(db.DepositCache.PendingDeposits(chainService.ctx, nil)) != 0 { - t.Fatalf("Expected 0 pending deposits, but there are %+v", db.DepositCache.PendingDeposits(chainService.ctx, nil)) + if len(chainService.depositCache.PendingDeposits(chainService.ctx, nil)) != 0 { + t.Fatalf("Expected 0 pending deposits, but there are %+v", chainService.depositCache.PendingDeposits(chainService.ctx, nil)) } testutil.AssertLogsContain(t, hook, "Executing state transition") } @@ -785,7 +788,7 @@ func TestReceiveBlock_OnChainSplit(t *testing.T) { if err != nil { t.Fatal(err) } - if err = db.SaveBlock(block); err != nil { + if err = db.SaveBlockDeprecated(block); err != nil { t.Fatal(err) } if err = db.UpdateChainHead(ctx, block, computedState); err != nil { @@ -863,7 +866,7 @@ func TestReceiveBlock_OnChainSplit(t *testing.T) { t.Fatal(err) } - if err := db.SaveBlock(blockF); err != nil { + if err := db.SaveBlockDeprecated(blockF); err != nil { t.Fatal(err) } @@ -954,7 +957,7 @@ func TestIsBlockReadyForProcessing_ValidBlock(t *testing.T) { if err != nil { t.Fatalf("Could not tree hash state: %v", err) } - if err := chainService.beaconDB.SaveBlock(genesis); err != nil { + if err := chainService.beaconDB.SaveBlock(ctx, genesis); err != nil { t.Fatalf("cannot save block: %v", err) } parentRoot, err := ssz.SigningRoot(genesis) @@ -1007,6 +1010,7 @@ func TestIsBlockReadyForProcessing_ValidBlock(t *testing.T) { func TestDeleteValidatorIdx_DeleteWorks(t *testing.T) { db := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, db) + ctx := context.Background() epoch := uint64(2) v.InsertActivatedIndices(epoch+1, []uint64{0, 1, 2}) v.InsertExitedVal(epoch+1, []uint64{0, 2}) @@ -1023,14 +1027,14 @@ func TestDeleteValidatorIdx_DeleteWorks(t *testing.T) { Slot: epoch * params.BeaconConfig().SlotsPerEpoch, } chainService := setupBeaconChain(t, db, nil) - if err := chainService.saveValidatorIdx(state); err != nil { + if err := chainService.saveValidatorIdx(ctx, state); err != nil { t.Fatalf("Could not save validator idx: %v", err) } - if err := chainService.deleteValidatorIdx(state); err != nil { + if err := chainService.deleteValidatorIdx(ctx, state); err != nil { t.Fatalf("Could not delete validator idx: %v", err) } wantedIdx := uint64(1) - idx, err := chainService.beaconDB.ValidatorIndex(validators[wantedIdx].PublicKey) + idx, _, err := chainService.beaconDB.ValidatorIndex(ctx, bytesutil.ToBytes48(validators[wantedIdx].PublicKey)) if err != nil { t.Fatalf("Could not get validator index: %v", err) } @@ -1039,7 +1043,7 @@ func TestDeleteValidatorIdx_DeleteWorks(t *testing.T) { } wantedIdx = uint64(2) - if chainService.beaconDB.HasValidator(validators[wantedIdx].PublicKey) { + if chainService.beaconDB.(*db2.BeaconDB).HasValidator(validators[wantedIdx].PublicKey) { t.Errorf("Validator index %d should have been deleted", wantedIdx) } if v.ExitedValFromEpoch(epoch) != nil { @@ -1050,6 +1054,7 @@ func TestDeleteValidatorIdx_DeleteWorks(t *testing.T) { func TestSaveValidatorIdx_SaveRetrieveWorks(t *testing.T) { db := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, db) + ctx := context.Background() epoch := uint64(1) v.InsertActivatedIndices(epoch+1, []uint64{0, 1, 2}) var validators []*ethpb.Validator @@ -1065,12 +1070,12 @@ func TestSaveValidatorIdx_SaveRetrieveWorks(t *testing.T) { Slot: epoch * params.BeaconConfig().SlotsPerEpoch, } chainService := setupBeaconChain(t, db, nil) - if err := chainService.saveValidatorIdx(state); err != nil { + if err := chainService.saveValidatorIdx(ctx, state); err != nil { t.Fatalf("Could not save validator idx: %v", err) } wantedIdx := uint64(2) - idx, err := chainService.beaconDB.ValidatorIndex(validators[wantedIdx].PublicKey) + idx, _, err := chainService.beaconDB.ValidatorIndex(ctx, bytesutil.ToBytes48(validators[wantedIdx].PublicKey)) if err != nil { t.Fatalf("Could not get validator index: %v", err) } @@ -1087,6 +1092,7 @@ func TestSaveValidatorIdx_IdxNotInState(t *testing.T) { db := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, db) epoch := uint64(100) + ctx := context.Background() // Tried to insert 5 active indices to DB with only 3 validators in state v.InsertActivatedIndices(epoch+1, []uint64{0, 1, 2, 3, 4}) @@ -1103,12 +1109,12 @@ func TestSaveValidatorIdx_IdxNotInState(t *testing.T) { Slot: epoch * params.BeaconConfig().SlotsPerEpoch, } chainService := setupBeaconChain(t, db, nil) - if err := chainService.saveValidatorIdx(state); err != nil { + if err := chainService.saveValidatorIdx(ctx, state); err != nil { t.Fatalf("Could not save validator idx: %v", err) } wantedIdx := uint64(2) - idx, err := chainService.beaconDB.ValidatorIndex(validators[wantedIdx].PublicKey) + idx, _, err := chainService.beaconDB.ValidatorIndex(ctx, bytesutil.ToBytes48(validators[wantedIdx].PublicKey)) if err != nil { t.Fatalf("Could not get validator index: %v", err) } diff --git a/beacon-chain/blockchain/fork_choice_deprecated.go b/beacon-chain/blockchain/fork_choice_deprecated.go index 4e8658e92e..4841b46e56 100644 --- a/beacon-chain/blockchain/fork_choice_deprecated.go +++ b/beacon-chain/blockchain/fork_choice_deprecated.go @@ -45,7 +45,7 @@ type TargetsFetcher interface { // the db with the latest FFG check points, both justification and finalization. func (c *ChainService) updateFFGCheckPts(ctx context.Context, state *pb.BeaconState) error { lastJustifiedSlot := helpers.StartSlot(state.CurrentJustifiedCheckpoint.Epoch) - savedJustifiedBlock, err := c.beaconDB.JustifiedBlock() + savedJustifiedBlock, err := c.beaconDB.(*db.BeaconDB).JustifiedBlock() if err != nil { return err } @@ -53,7 +53,7 @@ func (c *ChainService) updateFFGCheckPts(ctx context.Context, state *pb.BeaconSt // the slot of justified block saved in DB. if lastJustifiedSlot > savedJustifiedBlock.Slot { // Retrieve the new justified block from DB using the new justified slot and save it. - newJustifiedBlock, err := c.beaconDB.CanonicalBlockBySlot(ctx, lastJustifiedSlot) + newJustifiedBlock, err := c.beaconDB.(*db.BeaconDB).CanonicalBlockBySlot(ctx, lastJustifiedSlot) if err != nil { return err } @@ -63,7 +63,7 @@ func (c *ChainService) updateFFGCheckPts(ctx context.Context, state *pb.BeaconSt for newJustifiedBlock == nil { log.WithField("slot", lastAvailBlkSlot).Debug("Missing block in DB, looking one slot back") lastAvailBlkSlot-- - newJustifiedBlock, err = c.beaconDB.CanonicalBlockBySlot(ctx, lastAvailBlkSlot) + newJustifiedBlock, err = c.beaconDB.(*db.BeaconDB).CanonicalBlockBySlot(ctx, lastAvailBlkSlot) if err != nil { return err } @@ -74,20 +74,20 @@ func (c *ChainService) updateFFGCheckPts(ctx context.Context, state *pb.BeaconSt return err } // Fetch justified state from historical states db. - newJustifiedState, err := c.beaconDB.HistoricalStateFromSlot(ctx, newJustifiedBlock.Slot, newJustifiedRoot) + newJustifiedState, err := c.beaconDB.(*db.BeaconDB).HistoricalStateFromSlot(ctx, newJustifiedBlock.Slot, newJustifiedRoot) if err != nil { return err } - if err := c.beaconDB.SaveJustifiedBlock(newJustifiedBlock); err != nil { + if err := c.beaconDB.(*db.BeaconDB).SaveJustifiedBlock(newJustifiedBlock); err != nil { return err } - if err := c.beaconDB.SaveJustifiedState(newJustifiedState); err != nil { + if err := c.beaconDB.(*db.BeaconDB).SaveJustifiedState(newJustifiedState); err != nil { return err } } lastFinalizedSlot := helpers.StartSlot(state.FinalizedCheckpoint.Epoch) - savedFinalizedBlock, err := c.beaconDB.FinalizedBlock() + savedFinalizedBlock, err := c.beaconDB.(*db.BeaconDB).FinalizedBlock() // If the last processed finalized slot in state is greater than // the slot of finalized block saved in DB. if err != nil { @@ -95,7 +95,7 @@ func (c *ChainService) updateFFGCheckPts(ctx context.Context, state *pb.BeaconSt } if lastFinalizedSlot > savedFinalizedBlock.Slot { // Retrieve the new finalized block from DB using the new finalized slot and save it. - newFinalizedBlock, err := c.beaconDB.CanonicalBlockBySlot(ctx, lastFinalizedSlot) + newFinalizedBlock, err := c.beaconDB.(*db.BeaconDB).CanonicalBlockBySlot(ctx, lastFinalizedSlot) if err != nil { return err } @@ -105,7 +105,7 @@ func (c *ChainService) updateFFGCheckPts(ctx context.Context, state *pb.BeaconSt for newFinalizedBlock == nil { log.WithField("slot", lastAvailBlkSlot).Debug("Missing block in DB, looking one slot back") lastAvailBlkSlot-- - newFinalizedBlock, err = c.beaconDB.CanonicalBlockBySlot(ctx, lastAvailBlkSlot) + newFinalizedBlock, err = c.beaconDB.(*db.BeaconDB).CanonicalBlockBySlot(ctx, lastAvailBlkSlot) if err != nil { return err } @@ -117,14 +117,14 @@ func (c *ChainService) updateFFGCheckPts(ctx context.Context, state *pb.BeaconSt } // Generate the new finalized state with using new finalized block and // save it. - newFinalizedState, err := c.beaconDB.HistoricalStateFromSlot(ctx, lastFinalizedSlot, newFinalizedRoot) + newFinalizedState, err := c.beaconDB.(*db.BeaconDB).HistoricalStateFromSlot(ctx, lastFinalizedSlot, newFinalizedRoot) if err != nil { return err } - if err := c.beaconDB.SaveFinalizedBlock(newFinalizedBlock); err != nil { + if err := c.beaconDB.(*db.BeaconDB).SaveFinalizedBlock(newFinalizedBlock); err != nil { return err } - if err := c.beaconDB.SaveFinalizedState(newFinalizedState); err != nil { + if err := c.beaconDB.(*db.BeaconDB).SaveFinalizedState(newFinalizedState); err != nil { return err } } @@ -144,7 +144,7 @@ func (c *ChainService) ApplyForkChoiceRuleDeprecated( defer span.End() log.Info("Applying LMD-GHOST Fork Choice Rule") - justifiedState, err := c.beaconDB.JustifiedState() + justifiedState, err := c.beaconDB.(*db.BeaconDB).JustifiedState() if err != nil { return errors.Wrap(err, "could not retrieve justified state") } @@ -152,7 +152,7 @@ func (c *ChainService) ApplyForkChoiceRuleDeprecated( if err != nil { return errors.Wrap(err, "could not retrieve attestation target") } - justifiedHead, err := c.beaconDB.JustifiedBlock() + justifiedHead, err := c.beaconDB.(*db.BeaconDB).JustifiedBlock() if err != nil { return errors.Wrap(err, "could not retrieve justified head") } @@ -169,7 +169,7 @@ func (c *ChainService) ApplyForkChoiceRuleDeprecated( defer c.canonicalBlocksLock.Unlock() c.canonicalBlocks[newHead.Slot] = newHeadRoot[:] - currentHead, err := c.beaconDB.ChainHead() + currentHead, err := c.beaconDB.(*db.BeaconDB).ChainHead() if err != nil { return errors.Wrap(err, "could not retrieve chain head") } @@ -192,7 +192,7 @@ func (c *ChainService) ApplyForkChoiceRuleDeprecated( "newRoot": fmt.Sprintf("%#x", bytesutil.Trunc(newHeadRoot[:])), }).Warn("Reorg happened") // Only regenerate head state if there was a reorg. - newState, err = c.beaconDB.HistoricalStateFromSlot(ctx, newHead.Slot, newHeadRoot) + newState, err = c.beaconDB.(*db.BeaconDB).HistoricalStateFromSlot(ctx, newHead.Slot, newHeadRoot) if err != nil { return errors.Wrap(err, "could not gen state") } @@ -212,13 +212,13 @@ func (c *ChainService) ApplyForkChoiceRuleDeprecated( // If we receive forked blocks. if newHead.Slot != newState.Slot { - newState, err = c.beaconDB.HistoricalStateFromSlot(ctx, newHead.Slot, newHeadRoot) + newState, err = c.beaconDB.(*db.BeaconDB).HistoricalStateFromSlot(ctx, newHead.Slot, newHeadRoot) if err != nil { return errors.Wrap(err, "could not gen state") } } - if err := c.beaconDB.UpdateChainHead(ctx, newHead, newState); err != nil { + if err := c.beaconDB.(*db.BeaconDB).UpdateChainHead(ctx, newHead, newState); err != nil { return errors.Wrap(err, "failed to update chain") } h, err := ssz.SigningRoot(newHead) @@ -268,7 +268,7 @@ func (c *ChainService) lmdGhost( startState *pb.BeaconState, voteTargets map[uint64]*pb.AttestationTarget, ) (*ethpb.BeaconBlock, error) { - highestSlot := c.beaconDB.HighestBlockSlot() + highestSlot := c.beaconDB.(*db.BeaconDB).HighestBlockSlot() head := startBlock for { children, err := c.BlockChildren(ctx, head, highestSlot) @@ -280,12 +280,12 @@ func (c *ChainService) lmdGhost( } maxChild := children[0] - maxChildVotes, err := VoteCount(maxChild, startState, voteTargets, c.beaconDB) + maxChildVotes, err := VoteCount(maxChild, startState, voteTargets, c.beaconDB.(*db.BeaconDB)) if err != nil { return nil, errors.Wrap(err, "unable to determine vote count for block") } for i := 1; i < len(children); i++ { - candidateChildVotes, err := VoteCount(children[i], startState, voteTargets, c.beaconDB) + candidateChildVotes, err := VoteCount(children[i], startState, voteTargets, c.beaconDB.(*db.BeaconDB)) if err != nil { return nil, errors.Wrap(err, "unable to determine vote count for block") } @@ -326,7 +326,7 @@ func (c *ChainService) BlockChildren(ctx context.Context, block *ethpb.BeaconBlo var children []*ethpb.BeaconBlock startSlot := block.Slot + 1 for i := startSlot; i <= highestSlot; i++ { - kids, err := c.beaconDB.BlocksBySlot(ctx, i) + kids, err := c.beaconDB.(*db.BeaconDB).BlocksBySlot(ctx, i) if err != nil { return nil, errors.Wrap(err, "could not get block by slot") } @@ -353,7 +353,7 @@ func (c *ChainService) isDescendant(currentHead *ethpb.BeaconBlock, newHead *eth if bytesutil.ToBytes32(newHead.ParentRoot) == currentHeadRoot { return true, nil } - newHead, err = c.beaconDB.Block(bytesutil.ToBytes32(newHead.ParentRoot)) + newHead, err = c.beaconDB.(*db.BeaconDB).BlockDeprecated(bytesutil.ToBytes32(newHead.ParentRoot)) if err != nil { return false, err } @@ -449,7 +449,7 @@ func BlockAncestor(targetBlock *pb.AttestationTarget, slot uint64, beaconDB *db. return nil, nil } parentRoot := bytesutil.ToBytes32(targetBlock.ParentRoot) - parent, err := beaconDB.Block(parentRoot) + parent, err := beaconDB.BlockDeprecated(parentRoot) if err != nil { return nil, errors.Wrap(err, "could not get parent block") } @@ -480,7 +480,7 @@ func cachedAncestor(target *pb.AttestationTarget, height uint64, beaconDB *db.Be if err != nil { return nil, err } - ancestor, err := beaconDB.Block(bytesutil.ToBytes32(ancestorRoot)) + ancestor, err := beaconDB.BlockDeprecated(bytesutil.ToBytes32(ancestorRoot)) if err != nil { return nil, err } diff --git a/beacon-chain/blockchain/fork_choice_deprecated_test.go b/beacon-chain/blockchain/fork_choice_deprecated_test.go index d8d2dd3dbd..ae1c09193f 100644 --- a/beacon-chain/blockchain/fork_choice_deprecated_test.go +++ b/beacon-chain/blockchain/fork_choice_deprecated_test.go @@ -15,6 +15,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/beacon-chain/db" + db2 "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/internal" "github.com/prysmaticlabs/prysm/beacon-chain/powchain" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -87,15 +88,15 @@ func TestApplyForkChoice_SetsCanonicalHead(t *testing.T) { &attestation.Config{BeaconDB: beaconDb}) chainService := setupBeaconChain(t, beaconDb, attsService) - if err := chainService.beaconDB.SaveBlock( + if err := chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated( genesis); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveJustifiedBlock( + if err := chainService.beaconDB.(*db2.BeaconDB).SaveJustifiedBlock( genesis); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveJustifiedState( + if err := chainService.beaconDB.(*db2.BeaconDB).SaveJustifiedState( beaconState); err != nil { t.Fatal(err) } @@ -119,10 +120,10 @@ func TestApplyForkChoice_SetsCanonicalHead(t *testing.T) { t.Fatal(err) } - if err := chainService.beaconDB.SaveBlock(block); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveHistoricalState(context.Background(), beaconState, blockRoot); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveHistoricalState(context.Background(), beaconState, blockRoot); err != nil { t.Fatal(err) } if err := chainService.ApplyForkChoiceRuleDeprecated(context.Background(), block, tt.state); err != nil { @@ -139,13 +140,13 @@ func TestVoteCount_ParentDoesNotExistNoVoteCount(t *testing.T) { beaconDB := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, beaconDB) genesisBlock := b.NewGenesisBlock([]byte("stateroot")) - if err := beaconDB.SaveBlock(genesisBlock); err != nil { + if err := beaconDB.SaveBlockDeprecated(genesisBlock); err != nil { t.Fatal(err) } potentialHead := ðpb.BeaconBlock{ ParentRoot: []byte{'A'}, // We give a bogus parent root hash. } - if err := beaconDB.SaveBlock(potentialHead); err != nil { + if err := beaconDB.SaveBlockDeprecated(potentialHead); err != nil { t.Fatal(err) } headRoot, err := ssz.SigningRoot(potentialHead) @@ -176,7 +177,7 @@ func TestVoteCount_IncreaseCountCorrectly(t *testing.T) { if err != nil { t.Fatal(err) } - if err := beaconDB.SaveBlock(genesisBlock); err != nil { + if err := beaconDB.SaveBlockDeprecated(genesisBlock); err != nil { t.Fatal(err) } @@ -198,10 +199,10 @@ func TestVoteCount_IncreaseCountCorrectly(t *testing.T) { t.Fatal(err) } // We store these potential heads in the DB. - if err := beaconDB.SaveBlock(potentialHead); err != nil { + if err := beaconDB.SaveBlockDeprecated(potentialHead); err != nil { t.Fatal(err) } - if err := beaconDB.SaveBlock(potentialHead2); err != nil { + if err := beaconDB.SaveBlockDeprecated(potentialHead2); err != nil { t.Fatal(err) } beaconState := &pb.BeaconState{Validators: []*ethpb.Validator{{EffectiveBalance: 1e9}, {EffectiveBalance: 1e9}}} @@ -239,12 +240,12 @@ func TestAttestationTargets_RetrieveWorks(t *testing.T) { ExitEpoch: params.BeaconConfig().FarFutureEpoch}}, } - if err := beaconDB.SaveState(ctx, beaconState); err != nil { + if err := beaconDB.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatalf("could not save state: %v", err) } block := ðpb.BeaconBlock{Slot: 100} - if err := beaconDB.SaveBlock(block); err != nil { + if err := beaconDB.SaveBlockDeprecated(block); err != nil { t.Fatalf("could not save block: %v", err) } blockRoot, err := ssz.SigningRoot(block) @@ -303,10 +304,10 @@ func TestBlockChildren_2InARow(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block1); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block1); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block1, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block1, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -318,10 +319,10 @@ func TestBlockChildren_2InARow(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block2); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block2); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block2, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block2, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -329,10 +330,10 @@ func TestBlockChildren_2InARow(t *testing.T) { Slot: 3, ParentRoot: root2[:], } - if err = chainService.beaconDB.SaveBlock(block3); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block3); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block3, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block3, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -373,10 +374,10 @@ func TestBlockChildren_ChainSplits(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block1); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block1); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block1, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block1, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -384,10 +385,10 @@ func TestBlockChildren_ChainSplits(t *testing.T) { Slot: 2, ParentRoot: root1[:], } - if err = chainService.beaconDB.SaveBlock(block2); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block2); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block2, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block2, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -395,10 +396,10 @@ func TestBlockChildren_ChainSplits(t *testing.T) { Slot: 3, ParentRoot: root1[:], } - if err = chainService.beaconDB.SaveBlock(block3); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block3); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block3, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block3, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -406,10 +407,10 @@ func TestBlockChildren_ChainSplits(t *testing.T) { Slot: 4, ParentRoot: root1[:], } - if err = chainService.beaconDB.SaveBlock(block4); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block4); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block4, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block4, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -448,10 +449,10 @@ func TestBlockChildren_SkipSlots(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block1); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block1); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block1, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block1, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -463,10 +464,10 @@ func TestBlockChildren_SkipSlots(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block5); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block5); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block5, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block5, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -474,10 +475,10 @@ func TestBlockChildren_SkipSlots(t *testing.T) { Slot: 9, ParentRoot: root2[:], } - if err = chainService.beaconDB.SaveBlock(block9); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block9); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block9, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block9, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -518,10 +519,10 @@ func TestLMDGhost_TrivialHeadUpdate(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block1); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block1); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block1, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block1, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -533,10 +534,10 @@ func TestLMDGhost_TrivialHeadUpdate(t *testing.T) { if err != nil { t.Fatal(err) } - if err = chainService.beaconDB.SaveBlock(block2); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block2); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block2, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block2, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -592,10 +593,10 @@ func TestLMDGhost_3WayChainSplitsSameHeight(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block1); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block1); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block1, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block1, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -607,10 +608,10 @@ func TestLMDGhost_3WayChainSplitsSameHeight(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block2); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block2); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block2, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block2, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -622,10 +623,10 @@ func TestLMDGhost_3WayChainSplitsSameHeight(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block3); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block3); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block3, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block3, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -637,10 +638,10 @@ func TestLMDGhost_3WayChainSplitsSameHeight(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block4); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block4); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block4, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block4, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -699,7 +700,7 @@ func TestIsDescendant_Ok(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block1); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block1); err != nil { t.Fatalf("Could not save block: %v", err) } block2 := ðpb.BeaconBlock{ @@ -710,7 +711,7 @@ func TestIsDescendant_Ok(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block2); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block2); err != nil { t.Fatalf("Could not save block: %v", err) } block3 := ðpb.BeaconBlock{ @@ -721,7 +722,7 @@ func TestIsDescendant_Ok(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block3); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block3); err != nil { t.Fatalf("Could not save block: %v", err) } block4 := ðpb.BeaconBlock{ @@ -732,7 +733,7 @@ func TestIsDescendant_Ok(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block4); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block4); err != nil { t.Fatalf("Could not save block: %v", err) } block5 := ðpb.BeaconBlock{ @@ -743,7 +744,7 @@ func TestIsDescendant_Ok(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block5); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block5); err != nil { t.Fatalf("Could not save block: %v", err) } @@ -797,10 +798,10 @@ func TestLMDGhost_2WayChainSplitsDiffHeight(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block1); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block1); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block1, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block1, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -812,10 +813,10 @@ func TestLMDGhost_2WayChainSplitsDiffHeight(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block2); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block2); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block2, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block2, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -827,10 +828,10 @@ func TestLMDGhost_2WayChainSplitsDiffHeight(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block3); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block3); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block3, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block3, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -842,10 +843,10 @@ func TestLMDGhost_2WayChainSplitsDiffHeight(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block4); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block4); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block4, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block4, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -857,10 +858,10 @@ func TestLMDGhost_2WayChainSplitsDiffHeight(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block5); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block5); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block5, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block5, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -872,10 +873,10 @@ func TestLMDGhost_2WayChainSplitsDiffHeight(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(block6); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block6); err != nil { t.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block6, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block6, beaconState); err != nil { t.Fatalf("Could update chain head: %v", err) } @@ -937,10 +938,10 @@ func BenchmarkLMDGhost_8Slots_8Validators(b *testing.B) { if err != nil { b.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(genesis); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(genesis); err != nil { b.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, genesis, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, genesis, beaconState); err != nil { b.Fatalf("Could update chain head: %v", err) } @@ -950,10 +951,10 @@ func BenchmarkLMDGhost_8Slots_8Validators(b *testing.B) { Slot: uint64(i), ParentRoot: root[:], } - if err = chainService.beaconDB.SaveBlock(block); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { b.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block, beaconState); err != nil { b.Fatalf("Could update chain head: %v", err) } root, err = ssz.SigningRoot(block) @@ -1018,10 +1019,10 @@ func BenchmarkLMDGhost_32Slots_8Validators(b *testing.B) { if err != nil { b.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(genesis); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(genesis); err != nil { b.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, genesis, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, genesis, beaconState); err != nil { b.Fatalf("Could update chain head: %v", err) } @@ -1031,10 +1032,10 @@ func BenchmarkLMDGhost_32Slots_8Validators(b *testing.B) { Slot: uint64(i), ParentRoot: root[:], } - if err = chainService.beaconDB.SaveBlock(block); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { b.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block, beaconState); err != nil { b.Fatalf("Could update chain head: %v", err) } root, err = ssz.SigningRoot(block) @@ -1097,10 +1098,10 @@ func BenchmarkLMDGhost_32Slots_64Validators(b *testing.B) { if err != nil { b.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(genesis); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(genesis); err != nil { b.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, genesis, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, genesis, beaconState); err != nil { b.Fatalf("Could update chain head: %v", err) } @@ -1110,10 +1111,10 @@ func BenchmarkLMDGhost_32Slots_64Validators(b *testing.B) { Slot: uint64(i), ParentRoot: root[:], } - if err = chainService.beaconDB.SaveBlock(block); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { b.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block, beaconState); err != nil { b.Fatalf("Could update chain head: %v", err) } root, err = ssz.SigningRoot(block) @@ -1176,10 +1177,10 @@ func BenchmarkLMDGhost_64Slots_16384Validators(b *testing.B) { if err != nil { b.Fatalf("Could not hash block: %v", err) } - if err = chainService.beaconDB.SaveBlock(genesis); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(genesis); err != nil { b.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, genesis, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, genesis, beaconState); err != nil { b.Fatalf("Could update chain head: %v", err) } @@ -1189,10 +1190,10 @@ func BenchmarkLMDGhost_64Slots_16384Validators(b *testing.B) { Slot: uint64(i), ParentRoot: root[:], } - if err = chainService.beaconDB.SaveBlock(block); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { b.Fatalf("Could not save block: %v", err) } - if err = chainService.beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { + if err = chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block, beaconState); err != nil { b.Fatalf("Could update chain head: %v", err) } root, err = ssz.SigningRoot(block) @@ -1270,24 +1271,24 @@ func TestUpdateFFGCheckPts_NewJustifiedSlot(t *testing.T) { chainSvc := setupBeaconChain(t, beaconDB, nil) gBlockRoot, gBlock, gState, privKeys := setupFFGTest(t) - if err := chainSvc.beaconDB.SaveBlock(gBlock); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(gBlock); err != nil { t.Fatal(err) } - if err := chainSvc.beaconDB.UpdateChainHead(ctx, gBlock, gState); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, gBlock, gState); err != nil { t.Fatal(err) } - if err := chainSvc.beaconDB.SaveFinalizedState(gState); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveFinalizedState(gState); err != nil { t.Fatal(err) } // Last justified check point happened at slot 0. - if err := chainSvc.beaconDB.SaveJustifiedBlock( + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveJustifiedBlock( ðpb.BeaconBlock{Slot: genesisSlot}); err != nil { t.Fatal(err) } // Also saved finalized block to slot 0 to test justification case only. - if err := chainSvc.beaconDB.SaveFinalizedBlock(ðpb.BeaconBlock{Slot: genesisSlot}); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveFinalizedBlock(ðpb.BeaconBlock{Slot: genesisSlot}); err != nil { t.Fatal(err) } @@ -1306,10 +1307,10 @@ func TestUpdateFFGCheckPts_NewJustifiedSlot(t *testing.T) { Body: ðpb.BeaconBlockBody{ RandaoReveal: epochSignature, }} - if err := chainSvc.beaconDB.SaveBlock(block); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { t.Fatal(err) } - if err := chainSvc.beaconDB.UpdateChainHead(ctx, block, gState); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block, gState); err != nil { t.Fatal(err) } if err := chainSvc.updateFFGCheckPts(ctx, gState); err != nil { @@ -1318,11 +1319,11 @@ func TestUpdateFFGCheckPts_NewJustifiedSlot(t *testing.T) { // Getting latest justification check point from DB and // verify they have been updated. - newJustifiedState, err := chainSvc.beaconDB.JustifiedState() + newJustifiedState, err := chainSvc.beaconDB.(*db2.BeaconDB).JustifiedState() if err != nil { t.Fatal(err) } - newJustifiedBlock, err := chainSvc.beaconDB.JustifiedBlock() + newJustifiedBlock, err := chainSvc.beaconDB.(*db2.BeaconDB).JustifiedBlock() if err != nil { t.Fatal(err) } @@ -1346,22 +1347,22 @@ func TestUpdateFFGCheckPts_NewFinalizedSlot(t *testing.T) { ctx := context.Background() gBlockRoot, gBlock, gState, privKeys := setupFFGTest(t) - if err := chainSvc.beaconDB.SaveBlock(gBlock); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(gBlock); err != nil { t.Fatal(err) } - if err := chainSvc.beaconDB.UpdateChainHead(ctx, gBlock, gState); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, gBlock, gState); err != nil { t.Fatal(err) } - if err := chainSvc.beaconDB.SaveFinalizedState(gState); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveFinalizedState(gState); err != nil { t.Fatal(err) } // Last finalized check point happened at slot 0. - if err := chainSvc.beaconDB.SaveFinalizedBlock( + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveFinalizedBlock( gBlock); err != nil { t.Fatal(err) } - if err := chainSvc.beaconDB.SaveFinalizedState( + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveFinalizedState( gState); err != nil { t.Fatal(err) } @@ -1370,7 +1371,7 @@ func TestUpdateFFGCheckPts_NewFinalizedSlot(t *testing.T) { offset := uint64(64) // Also saved justified block to slot 0 to test finalized case only. - if err := chainSvc.beaconDB.SaveJustifiedBlock( + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveJustifiedBlock( ðpb.BeaconBlock{Slot: genesisSlot}); err != nil { t.Fatal(err) } @@ -1388,10 +1389,10 @@ func TestUpdateFFGCheckPts_NewFinalizedSlot(t *testing.T) { RandaoReveal: epochSignature, }} - if err := chainSvc.beaconDB.SaveBlock(block); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { t.Fatal(err) } - if err := chainSvc.beaconDB.UpdateChainHead(ctx, block, gState); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block, gState); err != nil { t.Fatal(err) } if err := chainSvc.updateFFGCheckPts(ctx, gState); err != nil { @@ -1400,11 +1401,11 @@ func TestUpdateFFGCheckPts_NewFinalizedSlot(t *testing.T) { // Getting latest justification check point from DB and // verify they have been updated. - newFinalizedState, err := chainSvc.beaconDB.FinalizedState() + newFinalizedState, err := chainSvc.beaconDB.(*db2.BeaconDB).FinalizedState() if err != nil { t.Fatal(err) } - newFinalizedBlock, err := chainSvc.beaconDB.FinalizedBlock() + newFinalizedBlock, err := chainSvc.beaconDB.(*db2.BeaconDB).FinalizedBlock() if err != nil { t.Fatal(err) } @@ -1428,24 +1429,24 @@ func TestUpdateFFGCheckPts_NewJustifiedSkipSlot(t *testing.T) { chainSvc := setupBeaconChain(t, beaconDB, nil) gBlockRoot, gBlock, gState, privKeys := setupFFGTest(t) - if err := chainSvc.beaconDB.SaveBlock(gBlock); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(gBlock); err != nil { t.Fatal(err) } - if err := chainSvc.beaconDB.UpdateChainHead(ctx, gBlock, gState); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, gBlock, gState); err != nil { t.Fatal(err) } - if err := chainSvc.beaconDB.SaveFinalizedState(gState); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveFinalizedState(gState); err != nil { t.Fatal(err) } // Last justified check point happened at slot 0. - if err := chainSvc.beaconDB.SaveJustifiedBlock( + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveJustifiedBlock( ðpb.BeaconBlock{Slot: genesisSlot}); err != nil { t.Fatal(err) } // Also saved finalized block to slot 0 to test justification case only. - if err := chainSvc.beaconDB.SaveFinalizedBlock( + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveFinalizedBlock( ðpb.BeaconBlock{Slot: genesisSlot}); err != nil { t.Fatal(err) } @@ -1465,7 +1466,7 @@ func TestUpdateFFGCheckPts_NewJustifiedSkipSlot(t *testing.T) { Body: ðpb.BeaconBlockBody{ RandaoReveal: epochSignature, }} - if err := chainSvc.beaconDB.SaveBlock(block); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { t.Fatal(err) } blockHeader, err := b.HeaderFromBlock(block) @@ -1476,10 +1477,10 @@ func TestUpdateFFGCheckPts_NewJustifiedSkipSlot(t *testing.T) { Slot: genesisSlot + lastAvailableSlot, LatestBlockHeader: blockHeader, } - if err := chainSvc.beaconDB.SaveState(ctx, computedState); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).SaveStateDeprecated(ctx, computedState); err != nil { t.Fatal(err) } - if err := chainSvc.beaconDB.UpdateChainHead(ctx, block, gState); err != nil { + if err := chainSvc.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block, gState); err != nil { t.Fatal(err) } if err := chainSvc.updateFFGCheckPts(ctx, gState); err != nil { @@ -1488,11 +1489,11 @@ func TestUpdateFFGCheckPts_NewJustifiedSkipSlot(t *testing.T) { // Getting latest justification check point from DB and // verify they have been updated. - newJustifiedState, err := chainSvc.beaconDB.JustifiedState() + newJustifiedState, err := chainSvc.beaconDB.(*db2.BeaconDB).JustifiedState() if err != nil { t.Fatal(err) } - newJustifiedBlock, err := chainSvc.beaconDB.JustifiedBlock() + newJustifiedBlock, err := chainSvc.beaconDB.(*db2.BeaconDB).JustifiedBlock() if err != nil { t.Fatal(err) } @@ -1582,7 +1583,7 @@ func TestVoteCount_CacheEnabledAndMiss(t *testing.T) { if err != nil { t.Fatal(err) } - if err := beaconDB.SaveBlock(genesisBlock); err != nil { + if err := beaconDB.SaveBlockDeprecated(genesisBlock); err != nil { t.Fatal(err) } @@ -1603,10 +1604,10 @@ func TestVoteCount_CacheEnabledAndMiss(t *testing.T) { t.Fatal(err) } // We store these potential heads in the DB. - if err := beaconDB.SaveBlock(potentialHead); err != nil { + if err := beaconDB.SaveBlockDeprecated(potentialHead); err != nil { t.Fatal(err) } - if err := beaconDB.SaveBlock(potentialHead2); err != nil { + if err := beaconDB.SaveBlockDeprecated(potentialHead2); err != nil { t.Fatal(err) } beaconState := &pb.BeaconState{Validators: []*ethpb.Validator{{EffectiveBalance: 1e9}, {EffectiveBalance: 1e9}}} @@ -1649,7 +1650,7 @@ func TestVoteCount_CacheEnabledAndHit(t *testing.T) { if err != nil { t.Fatal(err) } - if err := beaconDB.SaveBlock(genesisBlock); err != nil { + if err := beaconDB.SaveBlockDeprecated(genesisBlock); err != nil { t.Fatal(err) } @@ -1665,10 +1666,10 @@ func TestVoteCount_CacheEnabledAndHit(t *testing.T) { pHeadHash2, _ := ssz.SigningRoot(potentialHead2) // We store these potential heads in the DB. - if err := beaconDB.SaveBlock(potentialHead); err != nil { + if err := beaconDB.SaveBlockDeprecated(potentialHead); err != nil { t.Fatal(err) } - if err := beaconDB.SaveBlock(potentialHead2); err != nil { + if err := beaconDB.SaveBlockDeprecated(potentialHead2); err != nil { t.Fatal(err) } diff --git a/beacon-chain/blockchain/fork_choice_reorg_deprecated_test.go b/beacon-chain/blockchain/fork_choice_reorg_deprecated_test.go index bd1281b140..d917f4dac4 100644 --- a/beacon-chain/blockchain/fork_choice_reorg_deprecated_test.go +++ b/beacon-chain/blockchain/fork_choice_reorg_deprecated_test.go @@ -7,6 +7,7 @@ import ( "github.com/gogo/protobuf/proto" "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" + db2 "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/internal" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" @@ -54,16 +55,16 @@ func TestApplyForkChoice_ChainSplitReorg(t *testing.T) { // We then setup a canonical chain of the following blocks: // B0->B1->B3->B5. - if err := chainService.beaconDB.SaveBlock(blocks[0]); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(blocks[0]); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveJustifiedState(justifiedState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveJustifiedState(justifiedState); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveJustifiedBlock(blocks[0]); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveJustifiedBlock(blocks[0]); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.UpdateChainHead(ctx, blocks[0], justifiedState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, blocks[0], justifiedState); err != nil { t.Fatal(err) } canonicalBlockIndices := []int{1, 3, 5} @@ -73,15 +74,15 @@ func TestApplyForkChoice_ChainSplitReorg(t *testing.T) { if err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveBlock(blocks[canonicalIndex]); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(blocks[canonicalIndex]); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.UpdateChainHead(ctx, blocks[canonicalIndex], postState); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, blocks[canonicalIndex], postState); err != nil { t.Fatal(err) } } - chainHead, err := chainService.beaconDB.ChainHead() + chainHead, err := chainService.beaconDB.(*db2.BeaconDB).ChainHead() if err != nil { t.Fatal(err) } @@ -102,10 +103,10 @@ func TestApplyForkChoice_ChainSplitReorg(t *testing.T) { if err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveBlock(blocks[forkIndex]); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(blocks[forkIndex]); err != nil { t.Fatal(err) } - if err := chainService.beaconDB.SaveHistoricalState(ctx, forkState, roots[forkIndex]); err != nil { + if err := chainService.beaconDB.(*db2.BeaconDB).SaveHistoricalState(ctx, forkState, roots[forkIndex]); err != nil { t.Fatal(err) } } @@ -129,7 +130,7 @@ func TestApplyForkChoice_ChainSplitReorg(t *testing.T) { } chainService.attsService = attHandler - block4State, err := chainService.beaconDB.HistoricalStateFromSlot(ctx, blocks[4].Slot, roots[4]) + block4State, err := chainService.beaconDB.(*db2.BeaconDB).HistoricalStateFromSlot(ctx, blocks[4].Slot, roots[4]) if err != nil { t.Fatal(err) } @@ -138,7 +139,7 @@ func TestApplyForkChoice_ChainSplitReorg(t *testing.T) { t.Fatal(err) } - newHead, err := chainService.beaconDB.ChainHead() + newHead, err := chainService.beaconDB.(*db2.BeaconDB).ChainHead() if err != nil { t.Fatal(err) } diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index f786d8716e..2428f3c852 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -14,6 +14,7 @@ import ( "github.com/pkg/errors" "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/attestation" + "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/operations" @@ -39,7 +40,8 @@ type ChainFeeds interface { type ChainService struct { ctx context.Context cancel context.CancelFunc - beaconDB *db.BeaconDB + beaconDB db.Database + depositCache *depositcache.DepositCache web3Service *powchain.Web3Service attsService attestation.TargetHandler opsPoolService operations.OperationFeeds @@ -60,7 +62,8 @@ type Config struct { BeaconBlockBuf int Web3Service *powchain.Web3Service AttsService attestation.TargetHandler - BeaconDB *db.BeaconDB + BeaconDB db.Database + DepositCache *depositcache.DepositCache OpsPoolService operations.OperationFeeds DevMode bool P2p p2p.Broadcaster @@ -75,6 +78,7 @@ func NewChainService(ctx context.Context, cfg *Config) (*ChainService, error) { ctx: ctx, cancel: cancel, beaconDB: cfg.BeaconDB, + depositCache: cfg.DepositCache, web3Service: cfg.Web3Service, opsPoolService: cfg.OpsPoolService, attsService: cfg.AttsService, @@ -135,10 +139,16 @@ 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(c.ctx, unixTime, deposits, eth1data); err != nil { + // TODO(3219): Use new fork choice service. + db, isLegacyDB := c.beaconDB.(*db.BeaconDB) + if !isLegacyDB { + panic("Chain service cannot operate with the new database interface due to in-progress fork choice changes") + } + + if err := db.InitializeState(ctx, unixTime, deposits, eth1data); err != nil { return nil, errors.Wrap(err, "could not initialize beacon state to disk") } - beaconState, err := c.beaconDB.HeadState(c.ctx) + beaconState, err := c.beaconDB.HeadState(ctx) if err != nil { return nil, errors.Wrap(err, "could not attempt fetch beacon state") } @@ -153,29 +163,30 @@ func (c *ChainService) initializeBeaconChain(genesisTime time.Time, deposits []* return nil, errors.Wrap(err, "could not hash beacon block") } - if err := c.beaconDB.SaveBlock(genBlock); err != nil { + if err := c.beaconDB.SaveBlock(ctx, genBlock); err != nil { return nil, errors.Wrap(err, "could not save genesis block to disk") } - if err := c.beaconDB.SaveAttestationTarget(ctx, &pb.AttestationTarget{ + + if err := db.SaveAttestationTarget(ctx, &pb.AttestationTarget{ Slot: genBlock.Slot, BeaconBlockRoot: genBlockRoot[:], ParentRoot: genBlock.ParentRoot, }); err != nil { return nil, errors.Wrap(err, "failed to save attestation target") } - if err := c.beaconDB.UpdateChainHead(ctx, genBlock, beaconState); err != nil { + if err := db.UpdateChainHead(ctx, genBlock, beaconState); err != nil { return nil, errors.Wrap(err, "could not set chain head") } - if err := c.beaconDB.SaveJustifiedBlock(genBlock); err != nil { + if err := db.SaveJustifiedBlock(genBlock); err != nil { return nil, errors.Wrap(err, "could not save genesis block as justified block") } - if err := c.beaconDB.SaveFinalizedBlock(genBlock); err != nil { + if err := db.SaveFinalizedBlock(genBlock); err != nil { return nil, errors.Wrap(err, "could not save genesis block as finalized block") } - if err := c.beaconDB.SaveJustifiedState(beaconState); err != nil { + if err := db.SaveJustifiedState(beaconState); err != nil { return nil, errors.Wrap(err, "could not save genesis state as justified state") } - if err := c.beaconDB.SaveFinalizedState(beaconState); err != nil { + if err := db.SaveFinalizedState(beaconState); err != nil { return nil, errors.Wrap(err, "could not save genesis state as finalized state") } return beaconState, nil @@ -212,8 +223,8 @@ func (c *ChainService) StateInitializedFeed() *event.Feed { // ChainHeadRoot returns the hash root of the last beacon block processed by the // block chain service. -func (c *ChainService) ChainHeadRoot() ([32]byte, error) { - head, err := c.beaconDB.ChainHead() +func (c *ChainService) ChainHeadRoot(ctx context.Context) ([32]byte, error) { + head, err := c.beaconDB.HeadBlock(ctx) if err != nil { return [32]byte{}, errors.Wrap(err, "could not retrieve chain head") } diff --git a/beacon-chain/blockchain/service_test.go b/beacon-chain/blockchain/service_test.go index 066c44ebaf..97bac4ef96 100644 --- a/beacon-chain/blockchain/service_test.go +++ b/beacon-chain/blockchain/service_test.go @@ -14,6 +14,7 @@ import ( "github.com/gogo/protobuf/proto" "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/attestation" + "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/internal" @@ -148,7 +149,7 @@ var _ = p2p.Broadcaster(&mockBroadcaster{}) func setupGenesisBlock(t *testing.T, cs *ChainService) ([32]byte, *ethpb.BeaconBlock) { genesis := b.NewGenesisBlock([]byte{}) - if err := cs.beaconDB.SaveBlock(genesis); err != nil { + if err := cs.beaconDB.SaveBlock(context.Background(), genesis); err != nil { t.Fatalf("could not save block to db: %v", err) } parentHash, err := ssz.SigningRoot(genesis) @@ -178,6 +179,7 @@ func setupBeaconChain(t *testing.T, beaconDB *db.BeaconDB, attsService *attestat cfg := &Config{ BeaconBlockBuf: 0, BeaconDB: beaconDB, + DepositCache: depositcache.NewDepositCache(), Web3Service: web3Service, OpsPoolService: &mockOperationService{}, AttsService: attsService, @@ -201,7 +203,7 @@ func SetSlotInState(service *ChainService, slot uint64) error { } bState.Slot = slot - return service.beaconDB.SaveState(context.Background(), bState) + return service.beaconDB.SaveState(context.Background(), bState, [32]byte{}) } func TestChainStartStop_Uninitialized(t *testing.T) { diff --git a/beacon-chain/core/blocks/block_operations_test.go b/beacon-chain/core/blocks/block_operations_test.go index a0096ac57c..2fae7a884c 100644 --- a/beacon-chain/core/blocks/block_operations_test.go +++ b/beacon-chain/core/blocks/block_operations_test.go @@ -1178,7 +1178,7 @@ func TestProcessAttestations_OK(t *testing.T) { } func TestProcessAttestationsNoVerify_OK(t *testing.T) { - // Attestation passes with an empty signature + // Attestation with an empty signature helpers.ClearAllCaches() deposits, _ := testutil.SetupInitialDeposits(t, 100) beaconState, err := state.GenesisBeaconState(deposits, uint64(0), ðpb.Eth1Data{}) diff --git a/beacon-chain/core/blocks/validity_conditions.go b/beacon-chain/core/blocks/validity_conditions.go index 09c391e1a3..77c5c07b02 100644 --- a/beacon-chain/core/blocks/validity_conditions.go +++ b/beacon-chain/core/blocks/validity_conditions.go @@ -20,14 +20,14 @@ func IsValidBlock( ctx context.Context, state *pb.BeaconState, block *ethpb.BeaconBlock, - HasBlock func(hash [32]byte) bool, + HasBlock func(ctx context.Context, hash [32]byte) bool, GetPOWBlock func(ctx context.Context, hash common.Hash) (*gethTypes.Block, error), genesisTime time.Time) error { // Pre-Processing Condition 1: // Check that the parent Block has been processed and saved. parentRoot := bytesutil.ToBytes32(block.ParentRoot) - parentBlock := HasBlock(parentRoot) + parentBlock := HasBlock(ctx, parentRoot) if !parentBlock { return fmt.Errorf("unprocessed parent block as it is not saved in the db: %#x", parentRoot) } diff --git a/beacon-chain/core/blocks/validity_conditions_test.go b/beacon-chain/core/blocks/validity_conditions_test.go index 57c4e220b1..b93fc55ccd 100644 --- a/beacon-chain/core/blocks/validity_conditions_test.go +++ b/beacon-chain/core/blocks/validity_conditions_test.go @@ -23,7 +23,7 @@ type mockDB struct { hasBlock bool } -func (f *mockDB) HasBlock(h [32]byte) bool { +func (f *mockDB) HasBlock(ctx context.Context, h [32]byte) bool { return f.hasBlock } diff --git a/beacon-chain/db/BUILD.bazel b/beacon-chain/db/BUILD.bazel index d513b3f681..cd4911c8f7 100644 --- a/beacon-chain/db/BUILD.bazel +++ b/beacon-chain/db/BUILD.bazel @@ -17,7 +17,6 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/db", visibility = ["//beacon-chain:__subpackages__"], deps = [ - "//beacon-chain/cache/depositcache:go_default_library", "//beacon-chain/core/blocks:go_default_library", "//beacon-chain/core/state:go_default_library", "//beacon-chain/db/filters:go_default_library", diff --git a/beacon-chain/db/attestation.go b/beacon-chain/db/attestation.go index 40be248eaa..8c2e78d99f 100644 --- a/beacon-chain/db/attestation.go +++ b/beacon-chain/db/attestation.go @@ -6,12 +6,23 @@ import ( "github.com/boltdb/bolt" "github.com/gogo/protobuf/proto" "github.com/pkg/errors" + "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/hashutil" "go.opencensus.io/trace" ) +// SaveAttestations in the db. +func (db *BeaconDB) SaveAttestations(ctx context.Context, atts []*ethpb.Attestation) error { + for _, a := range atts { + if err := db.SaveAttestation(ctx, a); err != nil { + return err + } + } + return nil +} + // SaveAttestation puts the attestation record into the beacon chain db. func (db *BeaconDB) SaveAttestation(ctx context.Context, attestation *ethpb.Attestation) error { ctx, span := trace.StartSpan(ctx, "beaconDB.SaveAttestation") @@ -52,20 +63,32 @@ func (db *BeaconDB) SaveAttestationTarget(ctx context.Context, attTarget *pb.Att } // DeleteAttestation deletes the attestation record into the beacon chain db. -func (db *BeaconDB) DeleteAttestation(attestation *ethpb.Attestation) error { - hash, err := hashutil.HashProto(attestation.Data) - if err != nil { - return err - } - +func (db *BeaconDB) DeleteAttestation(_ context.Context, hash [32]byte) error { return db.batch(func(tx *bolt.Tx) error { a := tx.Bucket(attestationBucket) return a.Delete(hash[:]) }) } +// DeleteAttestationDeprecated deletes the attestation record into the beacon chain db. +// DEPRECATED: Use DeleteAttestation. +func (db *BeaconDB) DeleteAttestationDeprecated(attestation *ethpb.Attestation) error { + hash, err := hashutil.HashProto(attestation.Data) + if err != nil { + return err + } + + return db.DeleteAttestation(context.Background(), hash) +} + // Attestation retrieves an attestation record from the db using the hash of attestation.data. -func (db *BeaconDB) Attestation(hash [32]byte) (*ethpb.Attestation, error) { +func (db *BeaconDB) Attestation(_ context.Context, hash [32]byte) (*ethpb.Attestation, error) { + return db.AttestationDeprecated(hash) +} + +// AttestationDeprecated retrieves an attestation record from the db using the hash of attestation.data. +// DEPRECATED: Use Attestation. +func (db *BeaconDB) AttestationDeprecated(hash [32]byte) (*ethpb.Attestation, error) { var attestation *ethpb.Attestation err := db.view(func(tx *bolt.Tx) error { a := tx.Bucket(attestationBucket) @@ -85,7 +108,14 @@ func (db *BeaconDB) Attestation(hash [32]byte) (*ethpb.Attestation, error) { // Attestations retrieves all the attestation records from the db. // These are the attestations that have not been seen on the beacon chain. -func (db *BeaconDB) Attestations() ([]*ethpb.Attestation, error) { +func (db *BeaconDB) Attestations(_ context.Context, _ *filters.QueryFilter) ([]*ethpb.Attestation, error) { + return db.AttestationsDeprecated() +} + +// AttestationsDeprecated retrieves all the attestation records from the db. +// These are the attestations that have not been seen on the beacon chain. +// DEPRECATED: Use Attestations. +func (db *BeaconDB) AttestationsDeprecated() ([]*ethpb.Attestation, error) { var attestations []*ethpb.Attestation err := db.view(func(tx *bolt.Tx) error { a := tx.Bucket(attestationBucket) @@ -125,8 +155,8 @@ func (db *BeaconDB) AttestationTarget(hash [32]byte) (*pb.AttestationTarget, err return attTgt, err } -// HasAttestation checks if the attestation exists. -func (db *BeaconDB) HasAttestation(hash [32]byte) bool { +// HasAttestation checks if the attestaiton exists. +func (db *BeaconDB) HasAttestation(_ context.Context, hash [32]byte) bool { exists := false // #nosec G104 db.view(func(tx *bolt.Tx) error { @@ -138,6 +168,12 @@ func (db *BeaconDB) HasAttestation(hash [32]byte) bool { return exists } +// HasAttestationDeprecated checks if the attestation exists. +// DEPRECATED: Use HasAttestation. +func (db *BeaconDB) HasAttestationDeprecated(hash [32]byte) bool { + return db.HasAttestation(context.Background(), hash) +} + func createAttestation(enc []byte) (*ethpb.Attestation, error) { protoAttestation := ðpb.Attestation{} if err := proto.Unmarshal(enc, protoAttestation); err != nil { diff --git a/beacon-chain/db/attestation_test.go b/beacon-chain/db/attestation_test.go index d0a6a4091e..0eeca8498c 100644 --- a/beacon-chain/db/attestation_test.go +++ b/beacon-chain/db/attestation_test.go @@ -30,11 +30,11 @@ func TestSaveAndRetrieveAttestation_OK(t *testing.T) { aDataHash, err := hashutil.HashProto(a.Data) if err != nil { - t.Fatalf("Failed to hash Attestation: %v", err) + t.Fatalf("Failed to hash AttestationDeprecated: %v", err) } - aPrime, err := db.Attestation(aDataHash) + aPrime, err := db.AttestationDeprecated(aDataHash) if err != nil { - t.Fatalf("Failed to call Attestation: %v", err) + t.Fatalf("Failed to call AttestationDeprecated: %v", err) } aEnc, err := proto.Marshal(a) @@ -69,7 +69,7 @@ func TestRetrieveAttestations_OK(t *testing.T) { } } - retrievedAttestations, err := db.Attestations() + retrievedAttestations, err := db.AttestationsDeprecated() if err != nil { t.Fatalf("Could not retrieve attestations: %v", err) } @@ -101,22 +101,22 @@ func TestDeleteAttestation_OK(t *testing.T) { aDataHash, err := hashutil.HashProto(a.Data) if err != nil { - t.Fatalf("Failed to hash Attestation: %v", err) + t.Fatalf("Failed to hash AttestationDeprecated: %v", err) } - aPrime, err := db.Attestation(aDataHash) + aPrime, err := db.AttestationDeprecated(aDataHash) if err != nil { - t.Fatalf("Could not call Attestation: %v", err) + t.Fatalf("Could not call AttestationDeprecated: %v", err) } if !reflect.DeepEqual(aPrime, a) { t.Errorf("Saved attestation and retrieved attestation are not equal") } - if err := db.DeleteAttestation(a); err != nil { + if err := db.DeleteAttestationDeprecated(a); err != nil { t.Fatalf("Could not delete attestation: %v", err) } - if db.HasAttestation(aDataHash) { + if db.HasAttestationDeprecated(aDataHash) { t.Error("Deleted attestation still there") } } @@ -126,7 +126,7 @@ func TestNilAttestation_OK(t *testing.T) { defer teardownDB(t, db) nilHash := [32]byte{} - a, err := db.Attestation(nilHash) + a, err := db.AttestationDeprecated(nilHash) if err != nil { t.Fatalf("Failed to retrieve nilHash: %v", err) } @@ -148,17 +148,17 @@ func TestHasAttestation_OK(t *testing.T) { } aDataHash, err := hashutil.HashProto(a.Data) if err != nil { - t.Fatalf("Failed to hash Attestation: %v", err) + t.Fatalf("Failed to hash AttestationDeprecated: %v", err) } - if db.HasAttestation(aDataHash) { - t.Fatal("Expected HasAttestation to return false") + if db.HasAttestationDeprecated(aDataHash) { + t.Fatal("Expected HasAttestationDeprecated to return false") } if err := db.SaveAttestation(context.Background(), a); err != nil { t.Fatalf("Failed to save attestation: %v", err) } - if !db.HasAttestation(aDataHash) { - t.Fatal("Expected HasAttestation to return true") + if !db.HasAttestationDeprecated(aDataHash) { + t.Fatal("Expected HasAttestationDeprecated to return true") } } diff --git a/beacon-chain/db/block.go b/beacon-chain/db/block.go index f431aa36b9..001b28569f 100644 --- a/beacon-chain/db/block.go +++ b/beacon-chain/db/block.go @@ -11,6 +11,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "go.opencensus.io/trace" @@ -46,7 +47,14 @@ func createBlock(enc []byte) (*ethpb.BeaconBlock, error) { // Block accepts a block root and returns the corresponding block. // Returns nil if the block does not exist. -func (db *BeaconDB) Block(root [32]byte) (*ethpb.BeaconBlock, error) { +func (db *BeaconDB) Block(_ context.Context, root [32]byte) (*ethpb.BeaconBlock, error) { + return db.BlockDeprecated(root) +} + +// BlockDeprecated accepts a block root and returns the corresponding block. +// Returns nil if the block does not exist. +// DEPRECATED: Use Block. +func (db *BeaconDB) BlockDeprecated(root [32]byte) (*ethpb.BeaconBlock, error) { db.blocksLock.RLock() // Return block from cache if it exists @@ -84,7 +92,7 @@ func (db *BeaconDB) Block(root [32]byte) (*ethpb.BeaconBlock, error) { } // HasBlock accepts a block root and returns true if the block does not exist. -func (db *BeaconDB) HasBlock(root [32]byte) bool { +func (db *BeaconDB) HasBlock(_ context.Context, root [32]byte) bool { db.blocksLock.RLock() defer db.blocksLock.RUnlock() @@ -106,6 +114,17 @@ func (db *BeaconDB) HasBlock(root [32]byte) bool { return hasBlock } +// HasBlockDeprecated accepts a block root and returns true if the block does not exist. +func (db *BeaconDB) HasBlockDeprecated(root [32]byte) bool { + return db.HasBlock(context.Background(), root) +} + +// HeadBlock is not implemented. +// DEPRECATED: Do not use. Not implemented. +func (db *BeaconDB) HeadBlock(_ context.Context) (*ethpb.BeaconBlock, error) { + return nil, errors.New("not implemented") +} + // IsEvilBlockHash determines if a certain block root has been blacklisted // due to failing to process core state transitions. func (db *BeaconDB) IsEvilBlockHash(root [32]byte) bool { @@ -130,8 +149,29 @@ func (db *BeaconDB) MarkEvilBlockHash(root [32]byte) { badBlockCount.Inc() } -// SaveBlock accepts a block and writes it to disk. -func (db *BeaconDB) SaveBlock(block *ethpb.BeaconBlock) error { +// SaveHeadBlockRoot is not implemented. +func (db *BeaconDB) SaveHeadBlockRoot(_ context.Context, root [32]byte) error { + return errors.New("not implemented") +} + +// SaveBlocks in db. +func (db *BeaconDB) SaveBlocks(ctx context.Context, blocks []*ethpb.BeaconBlock) error { + for _, blk := range blocks { + if err := db.SaveBlock(ctx, blk); err != nil { + return err + } + } + return nil +} + +// SaveBlock in db. +func (db *BeaconDB) SaveBlock(_ context.Context, block *ethpb.BeaconBlock) error { + return db.SaveBlockDeprecated(block) +} + +// SaveBlockDeprecated accepts a block and writes it to disk. +// DEPRECATED: Use SaveBlock. +func (db *BeaconDB) SaveBlockDeprecated(block *ethpb.BeaconBlock) error { db.blocksLock.Lock() defer db.blocksLock.Unlock() @@ -168,7 +208,15 @@ func (db *BeaconDB) SaveBlock(block *ethpb.BeaconBlock) error { } // DeleteBlock deletes a block using the slot and its root as keys in their respective buckets. -func (db *BeaconDB) DeleteBlock(block *ethpb.BeaconBlock) error { +func (db *BeaconDB) DeleteBlock(_ context.Context, hash [32]byte) error { + return db.update(func(tx *bolt.Tx) error { + bucket := tx.Bucket(blockBucket) + return bucket.Delete(hash[:]) + }) +} + +// DeleteBlockDeprecated deletes a block using the slot and its root as keys in their respective buckets. +func (db *BeaconDB) DeleteBlockDeprecated(block *ethpb.BeaconBlock) error { db.blocksLock.Lock() defer db.blocksLock.Unlock() @@ -296,7 +344,7 @@ func (db *BeaconDB) UpdateChainHead(ctx context.Context, block *ethpb.BeaconBloc db.highestBlockSlot = block.Slot } - if err := db.SaveState(ctx, beaconState); err != nil { + if err := db.SaveStateDeprecated(ctx, beaconState); err != nil { return errors.Wrap(err, "failed to save beacon state as canonical") } @@ -397,3 +445,15 @@ func (db *BeaconDB) ClearBlockCache() { db.blocks = make(map[[32]byte]*ethpb.BeaconBlock) blockCacheSize.Set(float64(len(db.blocks))) } + +// BlockRoots retrieves a list of beacon block roots by filter criteria. +// DEPRECATED: Not implemented at all. Use github.com/prysmaticlabs/prysm/db/kv +func (db *BeaconDB) BlockRoots(ctx context.Context, f *filters.QueryFilter) ([][]byte, error) { + return nil, errors.New("not implemented") +} + +// Blocks retrieves a list of beacon blocks by filter criteria. +// DEPRECATED: Not implemented at all. Use github.com/prysmaticlabs/prysm/db/kv +func (db *BeaconDB) Blocks(ctx context.Context, f *filters.QueryFilter) ([]*ethpb.BeaconBlock, error) { + return nil, errors.New("not implemented") +} diff --git a/beacon-chain/db/block_test.go b/beacon-chain/db/block_test.go index f886b5cac0..929981a878 100644 --- a/beacon-chain/db/block_test.go +++ b/beacon-chain/db/block_test.go @@ -20,12 +20,12 @@ func TestNilDB_OK(t *testing.T) { block := ðpb.BeaconBlock{} h, _ := ssz.SigningRoot(block) - hasBlock := db.HasBlock(h) + hasBlock := db.HasBlockDeprecated(h) if hasBlock { t.Fatal("HashBlock should return false") } - bPrime, err := db.Block(h) + bPrime, err := db.BlockDeprecated(h) if err != nil { t.Fatalf("failed to get block: %v", err) } @@ -41,12 +41,12 @@ func TestSaveBlock_OK(t *testing.T) { block1 := ðpb.BeaconBlock{} h1, _ := ssz.SigningRoot(block1) - err := db.SaveBlock(block1) + err := db.SaveBlockDeprecated(block1) if err != nil { t.Fatalf("save block failed: %v", err) } - b1Prime, err := db.Block(h1) + b1Prime, err := db.BlockDeprecated(h1) if err != nil { t.Fatalf("failed to get block: %v", err) } @@ -61,12 +61,12 @@ func TestSaveBlock_OK(t *testing.T) { } h2, _ := ssz.SigningRoot(block2) - err = db.SaveBlock(block2) + err = db.SaveBlockDeprecated(block2) if err != nil { t.Fatalf("save block failed: %v", err) } - b2Prime, err := db.Block(h2) + b2Prime, err := db.BlockDeprecated(h2) if err != nil { t.Fatalf("failed to get block: %v", err) } @@ -86,11 +86,11 @@ func TestSaveBlock_NilBlkInCache(t *testing.T) { // Save a nil block to with block root. db.blocks[h1] = nil - if err := db.SaveBlock(block); err != nil { + if err := db.SaveBlockDeprecated(block); err != nil { t.Fatalf("save block failed: %v", err) } - savedBlock, err := db.Block(h1) + savedBlock, err := db.BlockDeprecated(h1) if err != nil { t.Fatal(err) } @@ -111,7 +111,7 @@ func TestSaveBlockInCache_OK(t *testing.T) { block := ðpb.BeaconBlock{Slot: 999} h, _ := ssz.SigningRoot(block) - err := db.SaveBlock(block) + err := db.SaveBlockDeprecated(block) if err != nil { t.Fatalf("save block failed: %v", err) } @@ -120,7 +120,7 @@ func TestSaveBlockInCache_OK(t *testing.T) { t.Error("Could not save block in cache") } - savedBlock, err := db.Block(h) + savedBlock, err := db.BlockDeprecated(h) if err != nil { t.Fatal(err) } @@ -136,22 +136,22 @@ func TestDeleteBlock_OK(t *testing.T) { block := ðpb.BeaconBlock{Slot: 0} h, _ := ssz.SigningRoot(block) - err := db.SaveBlock(block) + err := db.SaveBlockDeprecated(block) if err != nil { t.Fatalf("save block failed: %v", err) } - savedBlock, err := db.Block(h) + savedBlock, err := db.BlockDeprecated(h) if err != nil { t.Fatal(err) } if !proto.Equal(block, savedBlock) { t.Fatal(err) } - if err := db.DeleteBlock(block); err != nil { + if err := db.DeleteBlockDeprecated(block); err != nil { t.Fatal(err) } - savedBlock, err = db.Block(h) + savedBlock, err = db.BlockDeprecated(h) if err != nil { t.Fatal(err) } @@ -167,12 +167,12 @@ func TestDeleteBlockInCache_OK(t *testing.T) { block := ðpb.BeaconBlock{Slot: 0} h, _ := ssz.SigningRoot(block) - err := db.SaveBlock(block) + err := db.SaveBlockDeprecated(block) if err != nil { t.Fatalf("save block failed: %v", err) } - if err := db.DeleteBlock(block); err != nil { + if err := db.DeleteBlockDeprecated(block); err != nil { t.Fatal(err) } @@ -217,13 +217,13 @@ func TestBlocksBySlot_MultipleBlocks(t *testing.T) { Body: ðpb.BeaconBlockBody{ RandaoReveal: []byte("C"), }} - if err := db.SaveBlock(b1); err != nil { + if err := db.SaveBlockDeprecated(b1); err != nil { t.Fatal(err) } - if err := db.SaveBlock(b2); err != nil { + if err := db.SaveBlockDeprecated(b2); err != nil { t.Fatal(err) } - if err := db.SaveBlock(b3); err != nil { + if err := db.SaveBlockDeprecated(b3); err != nil { t.Fatal(err) } @@ -289,7 +289,7 @@ func TestUpdateChainHead_OK(t *testing.T) { if err != nil { t.Fatalf("failed to hash b2: %v", err) } - if err := db.SaveBlock(block2); err != nil { + if err := db.SaveBlockDeprecated(block2); err != nil { t.Fatalf("failed to save block: %v", err) } if err := db.UpdateChainHead(ctx, block2, beaconState); err != nil { @@ -341,7 +341,7 @@ func TestChainProgress_OK(t *testing.T) { cycleLength := params.BeaconConfig().SlotsPerEpoch block1 := ðpb.BeaconBlock{Slot: 1} - if err := db.SaveBlock(block1); err != nil { + if err := db.SaveBlockDeprecated(block1); err != nil { t.Fatalf("failed to save block: %v", err) } if err := db.UpdateChainHead(ctx, block1, beaconState); err != nil { @@ -356,7 +356,7 @@ func TestChainProgress_OK(t *testing.T) { } block2 := ðpb.BeaconBlock{Slot: cycleLength} - if err := db.SaveBlock(block2); err != nil { + if err := db.SaveBlockDeprecated(block2); err != nil { t.Fatalf("failed to save block: %v", err) } if err := db.UpdateChainHead(ctx, block2, beaconState); err != nil { @@ -371,7 +371,7 @@ func TestChainProgress_OK(t *testing.T) { } block3 := ðpb.BeaconBlock{Slot: 3} - if err := db.SaveBlock(block3); err != nil { + if err := db.SaveBlockDeprecated(block3); err != nil { t.Fatalf("failed to save block: %v", err) } if err := db.UpdateChainHead(ctx, block3, beaconState); err != nil { @@ -465,12 +465,12 @@ func TestHasBlock_returnsTrue(t *testing.T) { t.Fatal(err) } - if err := db.SaveBlock(block); err != nil { + if err := db.SaveBlockDeprecated(block); err != nil { t.Fatal(err) } - if !db.HasBlock(root) { - t.Fatal("db.HasBlock returned false for block just saved") + if !db.HasBlockDeprecated(root) { + t.Fatal("db.HasBlockDeprecated returned false for block just saved") } } @@ -482,7 +482,7 @@ func TestHighestBlockSlot_UpdatedOnSaveBlock(t *testing.T) { Slot: 23, } - if err := db.SaveBlock(block); err != nil { + if err := db.SaveBlockDeprecated(block); err != nil { t.Fatal(err) } @@ -491,7 +491,7 @@ func TestHighestBlockSlot_UpdatedOnSaveBlock(t *testing.T) { } block.Slot = 55 - if err := db.SaveBlock(block); err != nil { + if err := db.SaveBlockDeprecated(block); err != nil { t.Fatal(err) } @@ -506,7 +506,7 @@ func TestClearBlockCache_OK(t *testing.T) { block := ðpb.BeaconBlock{Slot: 0} - err := db.SaveBlock(block) + err := db.SaveBlockDeprecated(block) if err != nil { t.Fatalf("save block failed: %v", err) } diff --git a/beacon-chain/db/db.go b/beacon-chain/db/db.go index 716398e614..2f62111678 100644 --- a/beacon-chain/db/db.go +++ b/beacon-chain/db/db.go @@ -10,7 +10,6 @@ import ( "github.com/boltdb/bolt" "github.com/pkg/errors" - "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" "github.com/prysmaticlabs/prysm/beacon-chain/db/kv" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -54,11 +53,14 @@ type Database interface { SaveValidatorIndex(ctx context.Context, publicKey [48]byte, validatorIdx uint64) error } +var _ = Database(&BeaconDB{}) + // BeaconDB manages the data layer of the beacon chain implementation. // The exposed methods do not have an opinion of the underlying data engine, // but instead reflect the beacon chain logic. // For example, instead of defining get, put, remove // This defines methods such as getBlock, saveBlocksAndAttestations, etc. +// DEPRECATED: Use github.com/prysmaticlabs/prysm/db/kv instead. type BeaconDB struct { // state objects and caches stateLock sync.RWMutex @@ -67,7 +69,7 @@ type BeaconDB struct { validatorRegistry []*ethpb.Validator validatorBalances []uint64 db *bolt.DB - DatabasePath string + databasePath string // Beacon block info in memory. highestBlockSlot uint64 @@ -76,9 +78,6 @@ type BeaconDB struct { badBlocksLock sync.RWMutex blocks map[[32]byte]*ethpb.BeaconBlock blocksLock sync.RWMutex - - // Beacon chain deposits in memory. - DepositCache *depositcache.DepositCache } // Close closes the underlying boltdb database. @@ -107,6 +106,7 @@ func createBuckets(tx *bolt.Tx, buckets ...[]byte) error { } // NewDBDeprecated initializes a new DB. If the genesis block and states do not exist, this method creates it. +// DEPRECATED: Use github.com/prysmaticlabs/prysm/db.NewDB instead. func NewDBDeprecated(dirPath string) (*BeaconDB, error) { if err := os.MkdirAll(dirPath, 0700); err != nil { return nil, err @@ -120,9 +120,7 @@ func NewDBDeprecated(dirPath string) (*BeaconDB, error) { return nil, err } - depCache := depositcache.NewDepositCache() - - db := &BeaconDB{db: boltDB, DatabasePath: dirPath, DepositCache: depCache} + db := &BeaconDB{db: boltDB, databasePath: dirPath} db.blocks = make(map[[32]byte]*ethpb.BeaconBlock) if err := db.update(func(tx *bolt.Tx) error { @@ -147,3 +145,13 @@ func ClearDB(dirPath string) error { } return os.RemoveAll(dirPath) } + +// DatabasePath returns the filepath to the database directory. +func (db *BeaconDB) DatabasePath() string { + return db.databasePath +} + +// ClearDB removes the previously stored directory at the data directory. +func (db *BeaconDB) ClearDB() error { + return ClearDB(db.databasePath) +} diff --git a/beacon-chain/db/db_test.go b/beacon-chain/db/db_test.go index 22934e7781..01297e59dd 100644 --- a/beacon-chain/db/db_test.go +++ b/beacon-chain/db/db_test.go @@ -40,19 +40,19 @@ func teardownDB(t testing.TB, db *BeaconDB) { if err := db.Close(); err != nil { t.Fatalf("Failed to close database: %v", err) } - if err := os.RemoveAll(db.DatabasePath); err != nil { + if err := os.RemoveAll(db.DatabasePath()); err != nil { t.Fatalf("Failed to remove directory: %v", err) } } func TestClearDB(t *testing.T) { beaconDB := setupDB(t) - path := strings.TrimSuffix(beaconDB.DatabasePath, "beaconchain.db") + path := strings.TrimSuffix(beaconDB.DatabasePath(), "beaconchain.db") if err := ClearDB(path); err != nil { t.Fatal(err) } - if _, err := os.Stat(beaconDB.DatabasePath); !os.IsNotExist(err) { + if _, err := os.Stat(beaconDB.DatabasePath()); !os.IsNotExist(err) { t.Fatalf("db wasnt cleared %v", err) } } diff --git a/beacon-chain/db/setup_db.go b/beacon-chain/db/setup_db.go index 47cfe6f8e2..de9bc2196f 100644 --- a/beacon-chain/db/setup_db.go +++ b/beacon-chain/db/setup_db.go @@ -23,14 +23,3 @@ func SetupDB() (*BeaconDB, error) { } return NewDBDeprecated(path) } - -// TeardownDB cleans up a simulated backend BeaconDB instance. -// DEPRECATED: Use beacon-chain/db/testing.TeardownDB -func TeardownDB(db *BeaconDB) { - if err := db.Close(); err != nil { - log.Fatalf("failed to close database: %v", err) - } - if err := os.RemoveAll(db.DatabasePath); err != nil { - log.Fatalf("could not remove tmp db dir: %v", err) - } -} diff --git a/beacon-chain/db/state.go b/beacon-chain/db/state.go index dce1ef12fe..114166605d 100644 --- a/beacon-chain/db/state.go +++ b/beacon-chain/db/state.go @@ -52,7 +52,7 @@ func (db *BeaconDB) InitializeState(ctx context.Context, genesisTime uint64, dep db.serializedState = stateEnc db.stateHash = stateHash - if err := db.SaveState(ctx, beaconState); err != nil { + if err := db.SaveStateDeprecated(ctx, beaconState); err != nil { return err } @@ -96,6 +96,11 @@ func (db *BeaconDB) InitializeState(ctx context.Context, genesisTime uint64, dep }) } +// State is not implemented. +func (db *BeaconDB) State(ctx context.Context, blockRoot [32]byte) (*pb.BeaconState, error) { + return nil, errors.New("not implemented") +} + // HeadState fetches the canonical beacon chain's head state from the DB. func (db *BeaconDB) HeadState(ctx context.Context) (*pb.BeaconState, error) { if ctx.Err() != nil { @@ -150,9 +155,14 @@ func (db *BeaconDB) HeadStateRoot() [32]byte { return db.stateHash } -// SaveState updates the beacon chain state. -func (db *BeaconDB) SaveState(ctx context.Context, beaconState *pb.BeaconState) error { - ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveState") +// SaveState in db. +func (db *BeaconDB) SaveState(ctx context.Context, state *pb.BeaconState, _ [32]byte) error { + return db.SaveStateDeprecated(ctx, state) +} + +// SaveStateDeprecated updates the beacon chain state. +func (db *BeaconDB) SaveStateDeprecated(ctx context.Context, beaconState *pb.BeaconState) error { + ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveStateDeprecated") defer span.End() ctx, lockSpan := trace.StartSpan(ctx, "BeaconDB.stateLock.Lock") @@ -383,6 +393,11 @@ func (db *BeaconDB) Validators(ctx context.Context) ([]*ethpb.Validator, error) return beaconState.Validators, err } +// ValidatorLatestVote is not implemented. +func (db *BeaconDB) ValidatorLatestVote(_ context.Context, _ uint64) (*pb.ValidatorLatestVote, error) { + return nil, errors.New("not implemented") +} + // ValidatorFromState fetches the validator with the desired index from the cached registry. func (db *BeaconDB) ValidatorFromState(ctx context.Context, index uint64) (*ethpb.Validator, error) { ctx, span := trace.StartSpan(ctx, "BeaconDB.ValidatorFromState") diff --git a/beacon-chain/db/state_test.go b/beacon-chain/db/state_test.go index 7ec108b1b4..22e4963741 100644 --- a/beacon-chain/db/state_test.go +++ b/beacon-chain/db/state_test.go @@ -110,7 +110,7 @@ func BenchmarkState_ReadingFromCache(b *testing.B) { b.Fatalf("Could not read DV beacon state from DB: %v", err) } state.Slot++ - err = db.SaveState(ctx, state) + err = db.SaveStateDeprecated(ctx, state) if err != nil { b.Fatalf("Could not save beacon state to cache from DB: %v", err) } diff --git a/beacon-chain/db/validator.go b/beacon-chain/db/validator.go index f4772e801d..fe8dea1cd3 100644 --- a/beacon-chain/db/validator.go +++ b/beacon-chain/db/validator.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/binary" + "errors" "fmt" "github.com/boltdb/bolt" @@ -11,8 +12,13 @@ import ( "github.com/prysmaticlabs/prysm/shared/hashutil" ) -// SaveValidatorIndex accepts a public key and validator index and writes them to disk. -func (db *BeaconDB) SaveValidatorIndex(pubKey []byte, index int) error { +// SaveValidatorIndex in db. +func (db *BeaconDB) SaveValidatorIndex(ctx context.Context, pubkey [48]byte, idx uint64) error { + return db.SaveValidatorIndexDeprecated(pubkey[:], int(idx)) +} + +// SaveValidatorIndexDeprecated accepts a public key and validator index and writes them to disk. +func (db *BeaconDB) SaveValidatorIndexDeprecated(pubKey []byte, index int) error { h := hashutil.Hash(pubKey) return db.update(func(tx *bolt.Tx) error { @@ -25,6 +31,11 @@ func (db *BeaconDB) SaveValidatorIndex(pubKey []byte, index int) error { }) } +// SaveValidatorLatestVote not implemented. +func (db *BeaconDB) SaveValidatorLatestVote(_ context.Context, _ uint64, _ *pb.ValidatorLatestVote) error { + return errors.New("not implemented") +} + // SaveValidatorIndexBatch accepts a public key and validator index and writes them to disk. func (db *BeaconDB) SaveValidatorIndexBatch(pubKey []byte, index int) error { h := hashutil.Hash(pubKey) @@ -38,10 +49,16 @@ func (db *BeaconDB) SaveValidatorIndexBatch(pubKey []byte, index int) error { } -// ValidatorIndex accepts a public key and returns the corresponding validator index. +// ValidatorIndex returns validator index from database. +func (db *BeaconDB) ValidatorIndex(_ context.Context, pubkey [48]byte) (uint64, bool, error) { + idx, err := db.ValidatorIndexDeprecated(pubkey[:]) + return idx, true, err +} + +// ValidatorIndexDeprecated accepts a public key and returns the corresponding validator index. // If the validator index is not found in DB, as a fail over, it searches the state and // saves it to the DB when found. -func (db *BeaconDB) ValidatorIndex(pubKey []byte) (uint64, error) { +func (db *BeaconDB) ValidatorIndexDeprecated(pubKey []byte) (uint64, error) { if !db.HasValidator(pubKey) { state, err := db.HeadState(context.Background()) if err != nil { @@ -50,7 +67,7 @@ func (db *BeaconDB) ValidatorIndex(pubKey []byte) (uint64, error) { for i := 0; i < len(state.Validators); i++ { v := state.Validators[i] if bytes.Equal(v.PublicKey, pubKey) { - if err := db.SaveValidatorIndex(pubKey, i); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey, i); err != nil { return 0, err } return uint64(i), nil @@ -78,7 +95,13 @@ func (db *BeaconDB) ValidatorIndex(pubKey []byte) (uint64, error) { } // DeleteValidatorIndex deletes the validator index map record. -func (db *BeaconDB) DeleteValidatorIndex(pubKey []byte) error { +func (db *BeaconDB) DeleteValidatorIndex(_ context.Context, pubkey [48]byte) error { + return db.DeleteValidatorIndexDeprecated(pubkey[:]) +} + +// DeleteValidatorIndexDeprecated deletes the validator index map record. +// DEPRECATED: Do not use. +func (db *BeaconDB) DeleteValidatorIndexDeprecated(pubKey []byte) error { h := hashutil.Hash(pubKey) return db.update(func(tx *bolt.Tx) error { @@ -88,11 +111,22 @@ func (db *BeaconDB) DeleteValidatorIndex(pubKey []byte) error { }) } +// HasValidatorIndex returns hasValidator(pubkey). +func (db *BeaconDB) HasValidatorIndex(_ context.Context, pubkey [48]byte) bool { + return db.HasValidator(pubkey[:]) +} + +// HasValidatorLatestVote always returns false. Don't use this. +// DEPRECATED: Do not use. +func (db *BeaconDB) HasValidatorLatestVote(_ context.Context, _ uint64) bool { + return false +} + // HasValidator checks if a validator index map exists. func (db *BeaconDB) HasValidator(pubKey []byte) bool { exists := false h := hashutil.Hash(pubKey) - // #nosec G104, similar to HasBlock, HasAttestation... etc + // #nosec G104, similar to HasBlockDeprecated, HasAttestationDeprecated... etc db.view(func(tx *bolt.Tx) error { bkt := tx.Bucket(validatorBucket) @@ -106,7 +140,7 @@ func (db *BeaconDB) HasValidator(pubKey []byte) bool { // are in the bucket. func (db *BeaconDB) HasAnyValidators(state *pb.BeaconState, pubKeys [][]byte) (bool, error) { exists := false - // #nosec G104, similar to HasBlock, HasAttestation... etc + // #nosec G104, similar to HasBlockDeprecated, HasAttestationDeprecated... etc db.view(func(tx *bolt.Tx) error { bkt := tx.Bucket(validatorBucket) for _, pk := range pubKeys { @@ -122,7 +156,7 @@ func (db *BeaconDB) HasAnyValidators(state *pb.BeaconState, pubKeys [][]byte) (b for i := 0; i < len(state.Validators); i++ { v := state.Validators[i] if bytes.Equal(v.PublicKey, pubKey) { - if err := db.SaveValidatorIndex(pubKey, i); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey, i); err != nil { return false, err } exists = true diff --git a/beacon-chain/db/validator_test.go b/beacon-chain/db/validator_test.go index fe2bd22b42..a19ecf6bb6 100644 --- a/beacon-chain/db/validator_test.go +++ b/beacon-chain/db/validator_test.go @@ -19,24 +19,24 @@ func TestSaveAndRetrieveValidatorIndex_OK(t *testing.T) { p1 := []byte{'A', 'B', 'C'} p2 := []byte{'D', 'E', 'F'} - if err := db.SaveValidatorIndex(p1, 1); err != nil { + if err := db.SaveValidatorIndexDeprecated(p1, 1); err != nil { t.Fatalf("Failed to save vallidator index: %v", err) } - if err := db.SaveValidatorIndex(p2, 2); err != nil { + if err := db.SaveValidatorIndexDeprecated(p2, 2); err != nil { t.Fatalf("Failed to save vallidator index: %v", err) } - index1, err := db.ValidatorIndex(p1) + index1, err := db.ValidatorIndexDeprecated(p1) if err != nil { - t.Fatalf("Failed to call Attestation: %v", err) + t.Fatalf("Failed to call AttestationDeprecated: %v", err) } if index1 != 1 { t.Fatalf("Saved index and retrieved index are not equal: %#x and %#x", 1, index1) } - index2, err := db.ValidatorIndex(p2) + index2, err := db.ValidatorIndexDeprecated(p2) if err != nil { - t.Fatalf("Failed to call Attestation: %v", err) + t.Fatalf("Failed to call AttestationDeprecated: %v", err) } if index2 != 2 { t.Fatalf("Saved index and retrieved index are not equal: %#x and %#x", 2, index2) @@ -49,13 +49,13 @@ func TestSaveAndDeleteValidatorIndex_OK(t *testing.T) { p1 := []byte{'1', '2', '3'} - if err := db.SaveValidatorIndex(p1, 3); err != nil { + if err := db.SaveValidatorIndexDeprecated(p1, 3); err != nil { t.Fatalf("Failed to save validator index: %v", err) } - if err := db.SaveState(context.Background(), &pb.BeaconState{}); err != nil { + if err := db.SaveStateDeprecated(context.Background(), &pb.BeaconState{}); err != nil { t.Fatalf("Failed to save state: %v", err) } - index, err := db.ValidatorIndex(p1) + index, err := db.ValidatorIndexDeprecated(p1) if err != nil { t.Fatalf("Failed to call validator Index: %v", err) } @@ -63,10 +63,10 @@ func TestSaveAndDeleteValidatorIndex_OK(t *testing.T) { t.Fatalf("Saved index and retrieved index are not equal: %#x and %#x", 3, index) } - if err := db.DeleteValidatorIndex(p1); err != nil { + if err := db.DeleteValidatorIndexDeprecated(p1); err != nil { t.Fatalf("Could not delete attestation: %v", err) } - _, err = db.ValidatorIndex(p1) + _, err = db.ValidatorIndexDeprecated(p1) want := fmt.Sprintf("validator %#x does not exist", p1) if !strings.Contains(err.Error(), want) { t.Errorf("Want: %v, got: %v", want, err.Error()) diff --git a/beacon-chain/deprecated-sync/BUILD.bazel b/beacon-chain/deprecated-sync/BUILD.bazel index 55242fa3e7..50a985787e 100644 --- a/beacon-chain/deprecated-sync/BUILD.bazel +++ b/beacon-chain/deprecated-sync/BUILD.bazel @@ -14,6 +14,7 @@ go_library( visibility = ["//beacon-chain:__subpackages__"], deps = [ "//beacon-chain/blockchain:go_default_library", + "//beacon-chain/cache/depositcache:go_default_library", "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/deprecated-sync/initial-sync:go_default_library", diff --git a/beacon-chain/deprecated-sync/initial-sync/BUILD.bazel b/beacon-chain/deprecated-sync/initial-sync/BUILD.bazel index b3ad5fa9d5..83aa26b1a7 100644 --- a/beacon-chain/deprecated-sync/initial-sync/BUILD.bazel +++ b/beacon-chain/deprecated-sync/initial-sync/BUILD.bazel @@ -14,6 +14,7 @@ go_library( visibility = ["//beacon-chain:__subpackages__"], deps = [ "//beacon-chain/blockchain:go_default_library", + "//beacon-chain/cache/depositcache:go_default_library", "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/validators:go_default_library", "//beacon-chain/db:go_default_library", diff --git a/beacon-chain/deprecated-sync/initial-sync/helpers.go b/beacon-chain/deprecated-sync/initial-sync/helpers.go index 808ecaac8c..52c36a612d 100644 --- a/beacon-chain/deprecated-sync/initial-sync/helpers.go +++ b/beacon-chain/deprecated-sync/initial-sync/helpers.go @@ -27,7 +27,7 @@ func (s *InitialSync) checkBlockValidity(ctx context.Context, block *ethpb.Beaco if block.Slot < helpers.StartSlot(beaconState.FinalizedCheckpoint.Epoch) { return errors.New("discarding received block with a slot number smaller than the last finalized slot") } - // Attestation from proposer not verified as, other nodes only store blocks not proposer + // AttestationDeprecated from proposer not verified as, other nodes only store blocks not proposer // attestations. return nil } diff --git a/beacon-chain/deprecated-sync/initial-sync/service.go b/beacon-chain/deprecated-sync/initial-sync/service.go index 61b189c441..ec59cede16 100644 --- a/beacon-chain/deprecated-sync/initial-sync/service.go +++ b/beacon-chain/deprecated-sync/initial-sync/service.go @@ -22,6 +22,7 @@ import ( "github.com/pkg/errors" "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/blockchain" + "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/p2p" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -48,6 +49,7 @@ type Config struct { BatchedBlockBufferSize int StateBufferSize int BeaconDB *db.BeaconDB + DepositCache *depositcache.DepositCache P2P p2pAPI SyncService syncService ChainService chainService @@ -96,6 +98,7 @@ type InitialSync struct { syncService syncService chainService chainService db *db.BeaconDB + depositCache *depositcache.DepositCache powchain powChainService batchedBlockBuf chan deprecatedp2p.Message stateBuf chan deprecatedp2p.Message @@ -122,6 +125,7 @@ func NewInitialSyncService(ctx context.Context, p2p: cfg.P2P, syncService: cfg.SyncService, db: cfg.BeaconDB, + depositCache: cfg.DepositCache, powchain: cfg.PowChain, chainService: cfg.ChainService, stateBuf: stateBuf, @@ -155,7 +159,7 @@ func (s *InitialSync) exitInitialSync(ctx context.Context, block *ethpb.BeaconBl return nil } parentRoot := bytesutil.ToBytes32(block.ParentRoot) - parent, err := s.db.Block(parentRoot) + parent, err := s.db.BlockDeprecated(parentRoot) if err != nil { return err } @@ -166,7 +170,7 @@ func (s *InitialSync) exitInitialSync(ctx context.Context, block *ethpb.BeaconBl if err := s.chainService.VerifyBlockValidity(ctx, block, state); err != nil { return err } - if err := s.db.SaveBlock(block); err != nil { + if err := s.db.SaveBlockDeprecated(block); err != nil { return err } root, err := ssz.SigningRoot(block) @@ -186,7 +190,7 @@ func (s *InitialSync) exitInitialSync(ctx context.Context, block *ethpb.BeaconBl switch err.(type) { case *blockchain.BlockFailedProcessingErr: // If the block fails processing, we delete it from our DB. - if err := s.db.DeleteBlock(block); err != nil { + if err := s.db.DeleteBlockDeprecated(block); err != nil { return errors.Wrap(err, "could not delete bad block from db") } return errors.Wrap(err, "could not apply block state transition") diff --git a/beacon-chain/deprecated-sync/initial-sync/service_test.go b/beacon-chain/deprecated-sync/initial-sync/service_test.go index 9cba969e20..ea27e03e8b 100644 --- a/beacon-chain/deprecated-sync/initial-sync/service_test.go +++ b/beacon-chain/deprecated-sync/initial-sync/service_test.go @@ -105,7 +105,7 @@ func setUpGenesisStateAndBlock(beaconDB *db.BeaconDB, t *testing.T) { return } genBlock := b.NewGenesisBlock(stateRoot[:]) - if err := beaconDB.SaveBlock(genBlock); err != nil { + if err := beaconDB.SaveBlockDeprecated(genBlock); err != nil { t.Fatalf("could not save genesis block to disk: %v", err) } if err := beaconDB.UpdateChainHead(ctx, genBlock, beaconState); err != nil { @@ -206,8 +206,8 @@ func TestProcessingBlocks_SkippedSlots(t *testing.T) { // Save the block and set the parent hash of the next block // as the hash of the current block. - if err := ss.db.SaveBlock(block); err != nil { - t.Fatalf("Block unable to be saved %v", err) + if err := ss.db.SaveBlockDeprecated(block); err != nil { + t.Fatalf("BlockDeprecated unable to be saved %v", err) } hash, err := ssz.SigningRoot(block) diff --git a/beacon-chain/deprecated-sync/initial-sync/sync_blocks.go b/beacon-chain/deprecated-sync/initial-sync/sync_blocks.go index fb627000d5..91745f1ea8 100644 --- a/beacon-chain/deprecated-sync/initial-sync/sync_blocks.go +++ b/beacon-chain/deprecated-sync/initial-sync/sync_blocks.go @@ -99,7 +99,7 @@ func (s *InitialSync) validateAndSaveNextBlock(ctx context.Context, block *ethpb if err != nil { return err } - if s.db.HasBlock(root) { + if s.db.HasBlockDeprecated(root) { log.WithField("block", fmt.Sprintf("%#x", root)). Warn("Skipping block in db already") return nil @@ -116,7 +116,7 @@ func (s *InitialSync) validateAndSaveNextBlock(ctx context.Context, block *ethpb s.mutex.Lock() defer s.mutex.Unlock() parentRoot := bytesutil.ToBytes32(block.ParentRoot) - parentBlock, err := s.db.Block(parentRoot) + parentBlock, err := s.db.BlockDeprecated(parentRoot) if err != nil { return err } @@ -132,7 +132,7 @@ func (s *InitialSync) validateAndSaveNextBlock(ctx context.Context, block *ethpb if err := s.chainService.VerifyBlockValidity(ctx, block, state); err != nil { return err } - if err := s.db.SaveBlock(block); err != nil { + if err := s.db.SaveBlockDeprecated(block); err != nil { return err } if err := s.db.SaveAttestationTarget(ctx, &pb.AttestationTarget{ diff --git a/beacon-chain/deprecated-sync/initial-sync/sync_state.go b/beacon-chain/deprecated-sync/initial-sync/sync_state.go index 778aa9d59e..c2206d79f2 100644 --- a/beacon-chain/deprecated-sync/initial-sync/sync_state.go +++ b/beacon-chain/deprecated-sync/initial-sync/sync_state.go @@ -30,7 +30,7 @@ func (s *InitialSync) processState(msg p2p.Message, chainHead *pb.ChainHeadRespo return nil } - if err := s.db.SaveBlock(finalizedBlock); err != nil { + if err := s.db.SaveBlockDeprecated(finalizedBlock); err != nil { log.Errorf("Could not save block %v", err) return nil } @@ -76,7 +76,7 @@ func (s *InitialSync) processState(msg p2p.Message, chainHead *pb.ChainHeadRespo return nil } - s.db.DepositCache.PrunePendingDeposits(ctx, int(finalizedState.Eth1DepositIndex)) + s.depositCache.PrunePendingDeposits(ctx, int(finalizedState.Eth1DepositIndex)) if err := s.db.UpdateChainHead(ctx, finalizedBlock, finalizedState); err != nil { log.Errorf("Could not update chain head: %v", err) diff --git a/beacon-chain/deprecated-sync/querier_test.go b/beacon-chain/deprecated-sync/querier_test.go index 0c73e93dae..aadc1860a9 100644 --- a/beacon-chain/deprecated-sync/querier_test.go +++ b/beacon-chain/deprecated-sync/querier_test.go @@ -249,10 +249,10 @@ func TestSyncedInRestarts(t *testing.T) { bState := &pb.BeaconState{Slot: 0} blk := ðpb.BeaconBlock{Slot: 0} - if err := db.SaveState(context.Background(), bState); err != nil { + if err := db.SaveStateDeprecated(context.Background(), bState); err != nil { t.Fatalf("Could not save state: %v", err) } - if err := db.SaveBlock(blk); err != nil { + if err := db.SaveBlockDeprecated(blk); err != nil { t.Fatalf("Could not save state: %v", err) } if err := db.UpdateChainHead(context.Background(), blk, bState); err != nil { diff --git a/beacon-chain/deprecated-sync/receive_block.go b/beacon-chain/deprecated-sync/receive_block.go index e9f766c2c9..d278ab9b8d 100644 --- a/beacon-chain/deprecated-sync/receive_block.go +++ b/beacon-chain/deprecated-sync/receive_block.go @@ -42,7 +42,7 @@ func (rs *RegularSync) receiveBlockAnnounce(msg p2p.Message) error { return nil } - hasBlock := rs.db.HasBlock(h) + hasBlock := rs.db.HasBlockDeprecated(h) span.AddAttributes(trace.BoolAttribute("hasBlock", hasBlock)) if hasBlock { @@ -124,7 +124,7 @@ func (rs *RegularSync) validateAndProcessBlock( log.WithField("blockRoot", fmt.Sprintf("%#x", bytesutil.Trunc(blockRoot[:]))). Debug("Processing response to block request") - hasBlock := rs.db.HasBlock(blockRoot) + hasBlock := rs.db.HasBlockDeprecated(blockRoot) if hasBlock { log.Debug("Received a block that already exists. Exiting...") span.AddAttributes(trace.BoolAttribute("invalidBlock", true)) @@ -151,7 +151,7 @@ func (rs *RegularSync) validateAndProcessBlock( // We check if we have the block's parents saved locally. parentRoot := bytesutil.ToBytes32(block.ParentRoot) - hasParent := rs.db.HasBlock(parentRoot) + hasParent := rs.db.HasBlockDeprecated(parentRoot) span.AddAttributes(trace.BoolAttribute("hasParent", hasParent)) if !hasParent { @@ -193,7 +193,7 @@ func (rs *RegularSync) validateAndProcessBlock( log.WithFields(logrus.Fields{ "slot": block.Slot, "root": fmt.Sprintf("%#x", bytesutil.Trunc(blockRoot[:]))}, - ).Warn("Received Block from a forked chain") + ).Warn("Received BlockDeprecated from a forked chain") if err := rs.db.SaveHistoricalState(ctx, beaconState, blockRoot); err != nil { log.Errorf("Could not save historical state %v", err) return nil, nil, false, err diff --git a/beacon-chain/deprecated-sync/receive_block_test.go b/beacon-chain/deprecated-sync/receive_block_test.go index c64992631c..f771ab4ae3 100644 --- a/beacon-chain/deprecated-sync/receive_block_test.go +++ b/beacon-chain/deprecated-sync/receive_block_test.go @@ -113,10 +113,10 @@ func TestReceiveBlock_RecursivelyProcessesChildren(t *testing.T) { Slot: 0, FinalizedCheckpoint: ðpb.Checkpoint{Epoch: 0}, } - if err := db.SaveBlock(genesisBlock); err != nil { + if err := db.SaveBlockDeprecated(genesisBlock); err != nil { t.Fatal(err) } - if err := db.SaveState(ctx, genesisState); err != nil { + if err := db.SaveStateDeprecated(ctx, genesisState); err != nil { t.Fatal(err) } if err := db.UpdateChainHead(ctx, genesisBlock, genesisState); err != nil { diff --git a/beacon-chain/deprecated-sync/regular_sync.go b/beacon-chain/deprecated-sync/regular_sync.go index 99b67b0eee..33f384c429 100644 --- a/beacon-chain/deprecated-sync/regular_sync.go +++ b/beacon-chain/deprecated-sync/regular_sync.go @@ -399,10 +399,10 @@ func (rs *RegularSync) receiveAttestation(msg deprecatedp2p.Message) error { }).Debug("Received an attestation") // Skip if attestation has been seen before. - hasAttestation := rs.db.HasAttestation(attestationDataHash) + hasAttestation := rs.db.HasAttestationDeprecated(attestationDataHash) span.AddAttributes(trace.BoolAttribute("hasAttestation", hasAttestation)) if hasAttestation { - dbAttestation, err := rs.db.Attestation(attestationDataHash) + dbAttestation, err := rs.db.AttestationDeprecated(attestationDataHash) if err != nil { return err } @@ -478,7 +478,7 @@ func (rs *RegularSync) handleBlockRequestByHash(msg deprecatedp2p.Message) error data := msg.Data.(*pb.BeaconBlockRequest) root := bytesutil.ToBytes32(data.Hash) - block, err := rs.db.Block(root) + block, err := rs.db.BlockDeprecated(root) if err != nil { log.Error(err) return err @@ -541,7 +541,7 @@ func (rs *RegularSync) respondBatchedBlocks(ctx context.Context, finalizedRoot [ return nil, nil } - b, err := rs.db.Block(bytesutil.ToBytes32(headRoot)) + b, err := rs.db.BlockDeprecated(bytesutil.ToBytes32(headRoot)) if err != nil { return nil, err } @@ -555,7 +555,7 @@ func (rs *RegularSync) respondBatchedBlocks(ctx context.Context, finalizedRoot [ if ctx.Err() != nil { return nil, ctx.Err() } - b, err = rs.db.Block(bytesutil.ToBytes32(parentRoot)) + b, err = rs.db.BlockDeprecated(bytesutil.ToBytes32(parentRoot)) if err != nil { return nil, err } diff --git a/beacon-chain/deprecated-sync/regular_sync_test.go b/beacon-chain/deprecated-sync/regular_sync_test.go index 0aa251885c..35bc6353bc 100644 --- a/beacon-chain/deprecated-sync/regular_sync_test.go +++ b/beacon-chain/deprecated-sync/regular_sync_test.go @@ -74,7 +74,7 @@ func (ms *mockChainService) CanonicalBlockFeed() *event.Feed { } func (ms *mockChainService) ReceiveBlockDeprecated(ctx context.Context, block *ethpb.BeaconBlock) (*pb.BeaconState, error) { - if err := ms.db.SaveBlock(block); err != nil { + if err := ms.db.SaveBlockDeprecated(block); err != nil { return nil, err } return &pb.BeaconState{}, nil @@ -203,7 +203,7 @@ func TestProcessBlock_OK(t *testing.T) { parentBlock := ðpb.BeaconBlock{ Slot: 0, } - if err := db.SaveBlock(parentBlock); err != nil { + if err := db.SaveBlockDeprecated(parentBlock); err != nil { t.Fatalf("failed to save block: %v", err) } parentRoot, err := ssz.SigningRoot(parentBlock) @@ -281,7 +281,7 @@ func TestProcessBlock_MultipleBlocksProcessedOK(t *testing.T) { parentBlock := ðpb.BeaconBlock{ Slot: 0, } - if err := db.SaveBlock(parentBlock); err != nil { + if err := db.SaveBlockDeprecated(parentBlock); err != nil { t.Fatalf("failed to save block: %v", err) } parentRoot, err := ssz.SigningRoot(parentBlock) @@ -370,13 +370,13 @@ func TestReceiveAttestation_OK(t *testing.T) { Slot: 2, FinalizedCheckpoint: ðpb.Checkpoint{}, } - if err := db.SaveState(ctx, beaconState); err != nil { + if err := db.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatalf("Could not save state: %v", err) } beaconBlock := ðpb.BeaconBlock{ Slot: beaconState.Slot, } - if err := db.SaveBlock(beaconBlock); err != nil { + if err := db.SaveBlockDeprecated(beaconBlock); err != nil { t.Fatal(err) } if err := db.UpdateChainHead(ctx, beaconBlock, beaconState); err != nil { @@ -427,11 +427,11 @@ func TestReceiveAttestation_OlderThanPrevEpoch(t *testing.T) { Slot: 2 * params.BeaconConfig().SlotsPerEpoch, FinalizedCheckpoint: ðpb.Checkpoint{Epoch: 1}, } - if err := db.SaveState(ctx, state); err != nil { + if err := db.SaveStateDeprecated(ctx, state); err != nil { t.Fatalf("Could not save state: %v", err) } headBlock := ðpb.BeaconBlock{Slot: state.Slot} - if err := db.SaveBlock(headBlock); err != nil { + if err := db.SaveBlockDeprecated(headBlock); err != nil { t.Fatalf("failed to save block: %v", err) } if err := db.UpdateChainHead(ctx, headBlock, state); err != nil { @@ -592,21 +592,21 @@ func TestCanonicalBlockList_CanRetrieveCanonical(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = ss.db.SaveBlock(block1); err != nil { + if err = ss.db.SaveBlockDeprecated(block1); err != nil { t.Fatalf("Could not save block: %v", err) } block2 := ðpb.BeaconBlock{Slot: 2, ParentRoot: root1[:]} root2, _ := ssz.SigningRoot(block2) - if err = ss.db.SaveBlock(block2); err != nil { + if err = ss.db.SaveBlockDeprecated(block2); err != nil { t.Fatalf("Could not save block: %v", err) } block3 := ðpb.BeaconBlock{Slot: 3, ParentRoot: root1[:]} - if err = ss.db.SaveBlock(block3); err != nil { + if err = ss.db.SaveBlockDeprecated(block3); err != nil { t.Fatalf("Could not save block: %v", err) } block4 := ðpb.BeaconBlock{Slot: 4, ParentRoot: root2[:]} root4, _ := ssz.SigningRoot(block4) - if err = ss.db.SaveBlock(block4); err != nil { + if err = ss.db.SaveBlockDeprecated(block4); err != nil { t.Fatalf("Could not save block: %v", err) } @@ -633,7 +633,7 @@ func TestCanonicalBlockList_SameFinalizedAndHead(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = ss.db.SaveBlock(block1); err != nil { + if err = ss.db.SaveBlockDeprecated(block1); err != nil { t.Fatalf("Could not save block: %v", err) } @@ -668,7 +668,7 @@ func TestCanonicalBlockList_NilParentBlock(t *testing.T) { if err != nil { t.Fatalf("Could not hash block: %v", err) } - if err = ss.db.SaveBlock(block1); err != nil { + if err = ss.db.SaveBlockDeprecated(block1); err != nil { t.Fatalf("Could not save block: %v", err) } diff --git a/beacon-chain/deprecated-sync/service.go b/beacon-chain/deprecated-sync/service.go index 3567b6df39..80650473f4 100644 --- a/beacon-chain/deprecated-sync/service.go +++ b/beacon-chain/deprecated-sync/service.go @@ -4,6 +4,7 @@ import ( "context" "errors" + "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" "github.com/prysmaticlabs/prysm/beacon-chain/db" initialsync "github.com/prysmaticlabs/prysm/beacon-chain/deprecated-sync/initial-sync" "github.com/prysmaticlabs/prysm/beacon-chain/operations" @@ -29,7 +30,8 @@ type Service struct { // Config defines the configured services required for sync to work. type Config struct { ChainService chainService - BeaconDB *db.BeaconDB + BeaconDB db.Database + DepositCache *depositcache.DepositCache P2P p2pAPI AttsService attsService OperationService operations.OperationFeeds @@ -40,20 +42,21 @@ type Config struct { // given. func NewSyncService(ctx context.Context, cfg *Config) *Service { sqCfg := DefaultQuerierConfig() - sqCfg.BeaconDB = cfg.BeaconDB + sqCfg.BeaconDB = cfg.BeaconDB.(*db.BeaconDB) sqCfg.P2P = cfg.P2P sqCfg.PowChain = cfg.PowChainService sqCfg.ChainService = cfg.ChainService isCfg := initialsync.DefaultConfig() - isCfg.BeaconDB = cfg.BeaconDB + isCfg.BeaconDB = cfg.BeaconDB.(*db.BeaconDB) + isCfg.DepositCache = cfg.DepositCache isCfg.P2P = cfg.P2P isCfg.PowChain = cfg.PowChainService isCfg.ChainService = cfg.ChainService rsCfg := DefaultRegularSyncConfig() rsCfg.ChainService = cfg.ChainService - rsCfg.BeaconDB = cfg.BeaconDB + rsCfg.BeaconDB = cfg.BeaconDB.(*db.BeaconDB) rsCfg.P2P = cfg.P2P rsCfg.AttsService = cfg.AttsService rsCfg.OperationService = cfg.OperationService diff --git a/beacon-chain/deprecated-sync/service_test.go b/beacon-chain/deprecated-sync/service_test.go index 4f815dcf11..94a5b46e74 100644 --- a/beacon-chain/deprecated-sync/service_test.go +++ b/beacon-chain/deprecated-sync/service_test.go @@ -35,7 +35,7 @@ func initializeTestSyncService(ctx context.Context, cfg *Config, synced bool) *S services := NewSyncService(ctx, cfg) - sqCfg.BeaconDB = cfg.BeaconDB + sqCfg.BeaconDB = cfg.BeaconDB.(*db.BeaconDB) sqCfg.P2P = cfg.P2P sq := NewQuerierService(ctx, sqCfg) diff --git a/beacon-chain/internal/db_test_util.go b/beacon-chain/internal/db_test_util.go index 528d99336f..129f814086 100644 --- a/beacon-chain/internal/db_test_util.go +++ b/beacon-chain/internal/db_test_util.go @@ -38,7 +38,7 @@ func TeardownDBDeprecated(t testing.TB, db *db.BeaconDB) { if err := db.Close(); err != nil { t.Fatalf("Failed to close database: %v", err) } - if err := os.RemoveAll(db.DatabasePath); err != nil { + if err := os.RemoveAll(db.DatabasePath()); err != nil { t.Fatalf("Could not remove tmp db dir: %v", err) } } diff --git a/beacon-chain/node/BUILD.bazel b/beacon-chain/node/BUILD.bazel index 4fa259de1c..fcf1bff147 100644 --- a/beacon-chain/node/BUILD.bazel +++ b/beacon-chain/node/BUILD.bazel @@ -12,6 +12,7 @@ go_library( deps = [ "//beacon-chain/attestation:go_default_library", "//beacon-chain/blockchain:go_default_library", + "//beacon-chain/cache/depositcache:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/deprecated-sync:go_default_library", "//beacon-chain/flags:go_default_library", @@ -20,6 +21,7 @@ go_library( "//beacon-chain/p2p:go_default_library", "//beacon-chain/powchain:go_default_library", "//beacon-chain/rpc:go_default_library", + "//beacon-chain/sync:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//proto/eth/v1alpha1:go_default_library", "//shared:go_default_library", diff --git a/beacon-chain/node/node.go b/beacon-chain/node/node.go index 5828490e72..19c9c93a07 100644 --- a/beacon-chain/node/node.go +++ b/beacon-chain/node/node.go @@ -16,6 +16,7 @@ import ( "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/beacon-chain/attestation" "github.com/prysmaticlabs/prysm/beacon-chain/blockchain" + "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" "github.com/prysmaticlabs/prysm/beacon-chain/db" rbcsync "github.com/prysmaticlabs/prysm/beacon-chain/deprecated-sync" "github.com/prysmaticlabs/prysm/beacon-chain/flags" @@ -24,6 +25,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/p2p" "github.com/prysmaticlabs/prysm/beacon-chain/powchain" "github.com/prysmaticlabs/prysm/beacon-chain/rpc" + prysmsync "github.com/prysmaticlabs/prysm/beacon-chain/sync" "github.com/prysmaticlabs/prysm/shared" "github.com/prysmaticlabs/prysm/shared/cmd" "github.com/prysmaticlabs/prysm/shared/debug" @@ -46,11 +48,12 @@ const testSkipPowFlag = "test-skip-pow" // full PoS node. It handles the lifecycle of the entire system and registers // services to a service registry. type BeaconNode struct { - ctx *cli.Context - services *shared.ServiceRegistry - lock sync.RWMutex - stop chan struct{} // Channel to wait for termination notifications. - db *db.BeaconDB + ctx *cli.Context + services *shared.ServiceRegistry + lock sync.RWMutex + stop chan struct{} // Channel to wait for termination notifications. + db db.Database + depositCache *depositcache.DepositCache } // NewBeaconNode creates a new node instance, sets up configuration options, and registers @@ -182,13 +185,20 @@ func (b *BeaconNode) startDB(ctx *cli.Context) error { } } - db, err := db.NewDBDeprecated(dbPath) + var d db.Database + var err error + if featureconfig.FeatureConfig().UseNewDatabase { + d, err = db.NewDB(dbPath) + } else { + d, err = db.NewDBDeprecated(dbPath) + } if err != nil { return err } log.WithField("path", dbPath).Info("Checking db") - b.db = db + b.db = d + b.depositCache = depositcache.NewDepositCache() return nil } @@ -253,6 +263,7 @@ func (b *BeaconNode) registerBlockchainService(ctx *cli.Context) error { blockchainService, err := blockchain.NewChainService(context.Background(), &blockchain.Config{ BeaconDB: b.db, + DepositCache: b.depositCache, Web3Service: web3Service, OpsPoolService: opsService, AttsService: attsService, @@ -316,13 +327,14 @@ func (b *BeaconNode) registerPOWChainService(cliCtx *cli.Context) error { BlockFetcher: httpClient, ContractBackend: httpClient, BeaconDB: b.db, + DepositCache: b.depositCache, } web3Service, err := powchain.NewWeb3Service(ctx, cfg) if err != nil { return errors.Wrap(err, "could not register proof-of-work chain web3Service") } - if err := b.db.VerifyContractAddress(ctx, cfg.DepositContract); err != nil { + if err := b.db.(*db.BeaconDB).VerifyContractAddress(ctx, cfg.DepositContract); err != nil { return err } @@ -350,10 +362,21 @@ func (b *BeaconNode) registerSyncService(ctx *cli.Context) error { return err } + if featureconfig.FeatureConfig().UseNewSync { + rs := prysmsync.NewRegularSync(&prysmsync.Config{ + DB: b.db, + P2P: b.fetchP2P(ctx), + Operations: operationService, + }) + + return b.services.RegisterService(rs) + } + cfg := &rbcsync.Config{ ChainService: chainService, P2P: b.fetchP2P(ctx), BeaconDB: b.db, + DepositCache: b.depositCache, OperationService: operationService, PowChainService: web3Service, AttsService: attsService, diff --git a/beacon-chain/operations/BUILD.bazel b/beacon-chain/operations/BUILD.bazel index 3772628f88..ad064087c2 100644 --- a/beacon-chain/operations/BUILD.bazel +++ b/beacon-chain/operations/BUILD.bazel @@ -35,6 +35,7 @@ go_test( deps = [ "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/state:go_default_library", + "//beacon-chain/db:go_default_library", "//beacon-chain/internal:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//proto/eth/v1alpha1:go_default_library", diff --git a/beacon-chain/operations/service.go b/beacon-chain/operations/service.go index 14ef3bce97..609878cd4c 100644 --- a/beacon-chain/operations/service.go +++ b/beacon-chain/operations/service.go @@ -11,21 +11,22 @@ import ( "github.com/gogo/protobuf/proto" "github.com/pkg/errors" "github.com/prysmaticlabs/go-ssz" + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" + "github.com/sirupsen/logrus" + "go.opencensus.io/trace" + "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/beacon-chain/db" - p2p "github.com/prysmaticlabs/prysm/beacon-chain/p2p" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/beacon-chain/p2p" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/event" "github.com/prysmaticlabs/prysm/shared/hashutil" handler "github.com/prysmaticlabs/prysm/shared/messagehandler" "github.com/prysmaticlabs/prysm/shared/params" - "github.com/sirupsen/logrus" - "go.opencensus.io/trace" ) var log = logrus.WithField("prefix", "operation") @@ -50,7 +51,7 @@ type OperationFeeds interface { type Service struct { ctx context.Context cancel context.CancelFunc - beaconDB *db.BeaconDB + beaconDB db.Database incomingExitFeed *event.Feed incomingValidatorExits chan *ethpb.VoluntaryExit incomingAttFeed *event.Feed @@ -64,7 +65,7 @@ type Service struct { // Config options for the service. type Config struct { - BeaconDB *db.BeaconDB + BeaconDB db.Database P2P p2p.Broadcaster } @@ -132,7 +133,7 @@ func (s *Service) IncomingProcessedBlockFeed() *event.Feed { // capacity. The attestations get deleted in DB after they have been retrieved. func (s *Service) AttestationPool(ctx context.Context, requestedSlot uint64) ([]*ethpb.Attestation, error) { var attestations []*ethpb.Attestation - attestationsFromDB, err := s.beaconDB.Attestations() + attestationsFromDB, err := s.beaconDB.Attestations(ctx, nil /*filter*/) if err != nil { return nil, errors.New("could not retrieve attestations from DB") } @@ -159,7 +160,11 @@ func (s *Service) AttestationPool(ctx context.Context, requestedSlot uint64) ([] // Delete the attestation if the attestation is one epoch older than head state, // we don't want to pass these attestations to RPC for proposer to include. if slot+params.BeaconConfig().SlotsPerEpoch <= bState.Slot { - if err := s.beaconDB.DeleteAttestation(att); err != nil { + hash, err := ssz.HashTreeRoot(att) + if err != nil { + return nil, err + } + if err := s.beaconDB.DeleteAttestation(ctx, hash); err != nil { return nil, err } continue @@ -212,7 +217,7 @@ func (s *Service) HandleValidatorExits(ctx context.Context, message proto.Messag if err != nil { return err } - if err := s.beaconDB.SaveExit(ctx, exit); err != nil { + if err := s.beaconDB.(*db.BeaconDB).SaveExit(ctx, exit); err != nil { return err } log.WithField("hash", fmt.Sprintf("%#x", hash)).Info("Exit request saved in DB") @@ -251,8 +256,8 @@ func (s *Service) HandleAttestation(ctx context.Context, message proto.Message) } incomingAttBits := attestation.AggregationBits - if s.beaconDB.HasAttestation(hash) { - dbAtt, err := s.beaconDB.Attestation(hash) + if s.beaconDB.HasAttestation(ctx, hash) { + dbAtt, err := s.beaconDB.Attestation(ctx, hash) if err != nil { return err } @@ -290,14 +295,15 @@ func (s *Service) HandleAttestation(ctx context.Context, message proto.Message) // 2.) retrieve the canonical block by using voted block's slot number // 3.) return true if voted block root and the canonical block root are the same func (s *Service) IsAttCanonical(ctx context.Context, att *ethpb.Attestation) (bool, error) { - votedBlk, err := s.beaconDB.Block(bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) + votedBlk, err := s.beaconDB.Block(ctx, bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) if err != nil { return false, errors.Wrap(err, "could not hash block") } if votedBlk == nil { return false, nil } - canonicalBlk, err := s.beaconDB.CanonicalBlockBySlot(ctx, votedBlk.Slot) + // TODO(3219): Replace with new fork choice service. + canonicalBlk, err := s.beaconDB.(*db.BeaconDB).CanonicalBlockBySlot(ctx, votedBlk.Slot) if err != nil { return false, errors.Wrap(err, "could not hash block") } @@ -329,18 +335,18 @@ func (s *Service) removeOperations() { } } -func (s *Service) handleProcessedBlock(_ context.Context, message proto.Message) error { +func (s *Service) handleProcessedBlock(ctx context.Context, message proto.Message) error { block := message.(*ethpb.BeaconBlock) // Removes the attestations from the pool that have been included // in the received block. - if err := s.removeAttestationsFromPool(block.Body.Attestations); err != nil { + if err := s.removeAttestationsFromPool(ctx, block.Body.Attestations); err != nil { return errors.Wrap(err, "could not remove processed attestations from DB") } state, err := s.beaconDB.HeadState(s.ctx) if err != nil { return errors.New("could not retrieve attestations from DB") } - if err := s.removeEpochOldAttestations(state); err != nil { + if err := s.removeEpochOldAttestations(ctx, state); err != nil { return errors.Wrapf(err, "could not remove old attestations from DB at slot %d", block.Slot) } return nil @@ -348,25 +354,25 @@ func (s *Service) handleProcessedBlock(_ context.Context, message proto.Message) // removeAttestationsFromPool removes a list of attestations from the DB // after they have been included in a beacon block. -func (s *Service) removeAttestationsFromPool(attestations []*ethpb.Attestation) error { +func (s *Service) removeAttestationsFromPool(ctx context.Context, attestations []*ethpb.Attestation) error { for _, attestation := range attestations { hash, err := hashutil.HashProto(attestation.Data) if err != nil { return err } - if s.beaconDB.HasAttestation(hash) { - if err := s.beaconDB.DeleteAttestation(attestation); err != nil { + if s.beaconDB.HasAttestation(ctx, hash) { + if err := s.beaconDB.DeleteAttestation(ctx, hash); err != nil { return err } - log.WithField("root", fmt.Sprintf("%#x", hash)).Debug("Attestation removed") + log.WithField("root", fmt.Sprintf("%#x", hash)).Debug("AttestationDeprecated removed") } } return nil } // removeEpochOldAttestations removes attestations that's older than one epoch length from current slot. -func (s *Service) removeEpochOldAttestations(beaconState *pb.BeaconState) error { - attestations, err := s.beaconDB.Attestations() +func (s *Service) removeEpochOldAttestations(ctx context.Context, beaconState *pb.BeaconState) error { + attestations, err := s.beaconDB.Attestations(ctx, nil /*filter*/) if err != nil { return err } @@ -377,7 +383,11 @@ func (s *Service) removeEpochOldAttestations(beaconState *pb.BeaconState) error } // Remove attestation from DB if it's one epoch older than slot. if slot-params.BeaconConfig().SlotsPerEpoch >= slot { - if err := s.beaconDB.DeleteAttestation(a); err != nil { + hash, err := ssz.HashTreeRoot(a) + if err != nil { + return err + } + if err := s.beaconDB.DeleteAttestation(ctx, hash); err != nil { return err } } diff --git a/beacon-chain/operations/service_test.go b/beacon-chain/operations/service_test.go index 04a9a09fe1..6c7970f0da 100644 --- a/beacon-chain/operations/service_test.go +++ b/beacon-chain/operations/service_test.go @@ -14,6 +14,7 @@ import ( "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" + db2 "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/internal" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" @@ -144,7 +145,7 @@ func TestHandleAttestation_Saves_NewAttestation(t *testing.T) { newBlock := ðpb.BeaconBlock{ Slot: 0, } - if err := beaconDB.SaveBlock(newBlock); err != nil { + if err := beaconDB.SaveBlockDeprecated(newBlock); err != nil { t.Fatal(err) } if err := beaconDB.UpdateChainHead(context.Background(), newBlock, beaconState); err != nil { @@ -167,6 +168,7 @@ func TestHandleAttestation_Saves_NewAttestation(t *testing.T) { func TestHandleAttestation_Aggregates_SameAttestationData(t *testing.T) { beaconDB := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, beaconDB) + ctx := context.Background() broadcaster := &mockBroadcaster{} service := NewOpsPoolService(context.Background(), &Config{ BeaconDB: beaconDB, @@ -248,7 +250,7 @@ func TestHandleAttestation_Aggregates_SameAttestationData(t *testing.T) { newBlock := ðpb.BeaconBlock{ Slot: 0, } - if err := beaconDB.SaveBlock(newBlock); err != nil { + if err := beaconDB.SaveBlockDeprecated(newBlock); err != nil { t.Fatal(err) } if err := beaconDB.UpdateChainHead(context.Background(), newBlock, beaconState); err != nil { @@ -268,7 +270,7 @@ func TestHandleAttestation_Aggregates_SameAttestationData(t *testing.T) { if err != nil { t.Error(err) } - dbAtt, err := service.beaconDB.Attestation(attDataHash) + dbAtt, err := service.beaconDB.Attestation(ctx, attDataHash) if err != nil { t.Error(err) } @@ -296,6 +298,7 @@ func TestHandleAttestation_Aggregates_SameAttestationData(t *testing.T) { func TestHandleAttestation_Skips_PreviouslyAggregatedAttestations(t *testing.T) { beaconDB := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, beaconDB) + ctx := context.Background() helpers.ClearAllCaches() broadcaster := &mockBroadcaster{} service := NewOpsPoolService(context.Background(), &Config{ @@ -401,7 +404,7 @@ func TestHandleAttestation_Skips_PreviouslyAggregatedAttestations(t *testing.T) newBlock := ðpb.BeaconBlock{ Slot: 0, } - if err := beaconDB.SaveBlock(newBlock); err != nil { + if err := beaconDB.SaveBlockDeprecated(newBlock); err != nil { t.Fatal(err) } if err := beaconDB.UpdateChainHead(context.Background(), newBlock, beaconState); err != nil { @@ -425,7 +428,7 @@ func TestHandleAttestation_Skips_PreviouslyAggregatedAttestations(t *testing.T) if err != nil { t.Error(err) } - dbAtt, err := service.beaconDB.Attestation(attDataHash) + dbAtt, err := service.beaconDB.Attestation(ctx, attDataHash) if err != nil { t.Error(err) } @@ -442,7 +445,7 @@ func TestHandleAttestation_Skips_PreviouslyAggregatedAttestations(t *testing.T) if err := service.HandleAttestation(context.Background(), att2); err != nil { t.Error(err) } - dbAtt, err = service.beaconDB.Attestation(attDataHash) + dbAtt, err = service.beaconDB.Attestation(ctx, attDataHash) if err != nil { t.Error(err) } @@ -458,7 +461,7 @@ func TestHandleAttestation_Skips_PreviouslyAggregatedAttestations(t *testing.T) if err := service.HandleAttestation(context.Background(), att3); err != nil { t.Error(err) } - dbAtt, err = service.beaconDB.Attestation(attDataHash) + dbAtt, err = service.beaconDB.Attestation(ctx, attDataHash) if err != nil { t.Error(err) } @@ -496,7 +499,7 @@ func TestRetrieveAttestations_OK(t *testing.T) { t.Fatalf("Failed to save attestation: %v", err) } } - if err := beaconDB.SaveState(context.Background(), &pb.BeaconState{ + if err := beaconDB.SaveStateDeprecated(context.Background(), &pb.BeaconState{ Slot: 64, CurrentCrosslinks: []*ethpb.Crosslink{{ StartEpoch: 0, @@ -543,7 +546,7 @@ func TestRetrieveAttestations_PruneInvalidAtts(t *testing.T) { } // At slot 200 only attestations up to from slot 137 to 139 are valid attestations. - if err := beaconDB.SaveState(context.Background(), &pb.BeaconState{ + if err := beaconDB.SaveStateDeprecated(context.Background(), &pb.BeaconState{ Slot: 200, CurrentCrosslinks: []*ethpb.Crosslink{{ StartEpoch: 2, @@ -564,7 +567,7 @@ func TestRetrieveAttestations_PruneInvalidAtts(t *testing.T) { if err != nil { t.Fatal(err) } - if service.beaconDB.HasAttestation(hash) { + if service.beaconDB.HasAttestation(context.Background(), hash) { t.Error("Invalid attestation is not deleted") } } @@ -589,7 +592,7 @@ func TestRemoveProcessedAttestations_Ok(t *testing.T) { t.Fatalf("Failed to save attestation: %v", err) } } - if err := db.SaveState(context.Background(), &pb.BeaconState{ + if err := db.SaveStateDeprecated(context.Background(), &pb.BeaconState{ Slot: 15, CurrentCrosslinks: []*ethpb.Crosslink{{ StartEpoch: 0, @@ -605,7 +608,7 @@ func TestRemoveProcessedAttestations_Ok(t *testing.T) { t.Error("Retrieved attestations did not match prev generated attestations") } - if err := s.removeAttestationsFromPool(attestations); err != nil { + if err := s.removeAttestationsFromPool(context.Background(), attestations); err != nil { t.Fatalf("Could not remove attestations: %v", err) } @@ -636,7 +639,7 @@ func TestReceiveBlkRemoveOps_Ok(t *testing.T) { } } - if err := db.SaveState(context.Background(), &pb.BeaconState{ + if err := db.SaveStateDeprecated(context.Background(), &pb.BeaconState{ Slot: 15, CurrentCrosslinks: []*ethpb.Crosslink{{ StartEpoch: 0, @@ -675,10 +678,10 @@ func TestIsCanonical_CanGetCanonical(t *testing.T) { s := NewOpsPoolService(context.Background(), &Config{BeaconDB: db}) cb1 := ðpb.BeaconBlock{Slot: 999, ParentRoot: []byte{'A'}} - if err := s.beaconDB.SaveBlock(cb1); err != nil { + if err := s.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(cb1); err != nil { t.Fatal(err) } - if err := s.beaconDB.UpdateChainHead(context.Background(), cb1, &pb.BeaconState{}); err != nil { + if err := s.beaconDB.(*db2.BeaconDB).UpdateChainHead(context.Background(), cb1, &pb.BeaconState{}); err != nil { t.Fatal(err) } r1, err := ssz.SigningRoot(cb1) @@ -695,10 +698,10 @@ func TestIsCanonical_CanGetCanonical(t *testing.T) { } cb2 := ðpb.BeaconBlock{Slot: 1000, ParentRoot: []byte{'B'}} - if err := s.beaconDB.SaveBlock(cb2); err != nil { + if err := s.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(cb2); err != nil { t.Fatal(err) } - if err := s.beaconDB.UpdateChainHead(context.Background(), cb2, &pb.BeaconState{}); err != nil { + if err := s.beaconDB.(*db2.BeaconDB).UpdateChainHead(context.Background(), cb2, &pb.BeaconState{}); err != nil { t.Fatal(err) } canonical, err = s.IsAttCanonical(context.Background(), att1) @@ -724,7 +727,7 @@ func TestIsCanonical_NilBlocks(t *testing.T) { } cb1 := ðpb.BeaconBlock{Slot: 999, ParentRoot: []byte{'A'}} - if err := s.beaconDB.SaveBlock(cb1); err != nil { + if err := s.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(cb1); err != nil { t.Fatal(err) } r1, err := ssz.SigningRoot(cb1) diff --git a/beacon-chain/powchain/BUILD.bazel b/beacon-chain/powchain/BUILD.bazel index db778673d6..4b45ccd481 100644 --- a/beacon-chain/powchain/BUILD.bazel +++ b/beacon-chain/powchain/BUILD.bazel @@ -12,6 +12,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/powchain", visibility = ["//beacon-chain:__subpackages__"], deps = [ + "//beacon-chain/cache/depositcache:go_default_library", "//beacon-chain/core/state:go_default_library", "//beacon-chain/db:go_default_library", "//contracts/deposit-contract:go_default_library", diff --git a/beacon-chain/powchain/log_processing.go b/beacon-chain/powchain/log_processing.go index 719294f44d..2d2f616f99 100644 --- a/beacon-chain/powchain/log_processing.go +++ b/beacon-chain/powchain/log_processing.go @@ -123,16 +123,16 @@ func (w *Web3Service) ProcessDepositLog(depositLog gethTypes.Log) error { // Make sure duplicates are rejected pre-chainstart. if !w.chainStarted && validData { var pubkey = fmt.Sprintf("#%x", depositData.PublicKey) - if w.beaconDB.DepositCache.PubkeyInChainstart(w.ctx, pubkey) { + if w.depositCache.PubkeyInChainstart(w.ctx, pubkey) { log.Warnf("Pubkey %#x has already been submitted for chainstart", pubkey) } else { - w.beaconDB.DepositCache.MarkPubkeyForChainstart(w.ctx, pubkey) + w.depositCache.MarkPubkeyForChainstart(w.ctx, pubkey) } } // We always store all historical deposits in the DB. - w.beaconDB.DepositCache.InsertDeposit(w.ctx, deposit, big.NewInt(int64(depositLog.BlockNumber)), int(index), w.depositTrie.Root()) + w.depositCache.InsertDeposit(w.ctx, deposit, big.NewInt(int64(depositLog.BlockNumber)), int(index), w.depositTrie.Root()) if !w.chainStarted { w.chainStartDeposits = append(w.chainStartDeposits, deposit) @@ -146,7 +146,7 @@ func (w *Web3Service) ProcessDepositLog(depositLog gethTypes.Log) error { validData = false } } else { - w.beaconDB.DepositCache.InsertPendingDeposit(w.ctx, deposit, big.NewInt(int64(depositLog.BlockNumber)), int(index), w.depositTrie.Root()) + w.depositCache.InsertPendingDeposit(w.ctx, deposit, big.NewInt(int64(depositLog.BlockNumber)), int(index), w.depositTrie.Root()) } if validData { log.WithFields(logrus.Fields{ @@ -245,7 +245,7 @@ func (w *Web3Service) processPastLogs() error { } if currentState != nil && currentState.Eth1DepositIndex > 0 { - w.beaconDB.DepositCache.PrunePendingDeposits(w.ctx, int(currentState.Eth1DepositIndex)) + w.depositCache.PrunePendingDeposits(w.ctx, int(currentState.Eth1DepositIndex)) } return nil diff --git a/beacon-chain/powchain/log_processing_test.go b/beacon-chain/powchain/log_processing_test.go index 2b394e398f..5e62c95610 100644 --- a/beacon-chain/powchain/log_processing_test.go +++ b/beacon-chain/powchain/log_processing_test.go @@ -40,10 +40,9 @@ func TestProcessDepositLog_OK(t *testing.T) { Logger: &goodLogger{}, HTTPLogger: &goodLogger{}, ContractBackend: testAcc.Backend, - BeaconDB: &db.BeaconDB{ - DepositCache: depositcache.NewDepositCache(), - }, - BlockFetcher: &goodFetcher{}, + BeaconDB: &db.BeaconDB{}, + DepositCache: depositcache.NewDepositCache(), + BlockFetcher: &goodFetcher{}, }) if err != nil { t.Fatalf("unable to setup web3 ETH1.0 chain service: %v", err) @@ -103,9 +102,8 @@ func TestProcessDepositLog_InsertsPendingDeposit(t *testing.T) { Logger: &goodLogger{}, HTTPLogger: &goodLogger{}, ContractBackend: testAcc.Backend, - BeaconDB: &db.BeaconDB{ - DepositCache: depositcache.NewDepositCache(), - }, + BeaconDB: &db.BeaconDB{}, + DepositCache: depositcache.NewDepositCache(), }) if err != nil { t.Fatalf("unable to setup web3 ETH1.0 chain service: %v", err) @@ -154,7 +152,7 @@ func TestProcessDepositLog_InsertsPendingDeposit(t *testing.T) { web3Service.ProcessDepositLog(logs[0]) web3Service.ProcessDepositLog(logs[1]) - pendingDeposits := web3Service.beaconDB.DepositCache.PendingDeposits(context.Background(), nil /*blockNum*/) + pendingDeposits := web3Service.depositCache.PendingDeposits(context.Background(), nil /*blockNum*/) if len(pendingDeposits) != 2 { t.Errorf("Unexpected number of deposits. Wanted 2 deposit, got %+v", pendingDeposits) } @@ -251,10 +249,9 @@ func TestProcessETH2GenesisLog_8DuplicatePubkeys(t *testing.T) { Logger: &goodLogger{}, HTTPLogger: &goodLogger{}, ContractBackend: testAcc.Backend, - BeaconDB: &db.BeaconDB{ - DepositCache: depositcache.NewDepositCache(), - }, - BlockFetcher: &goodFetcher{}, + BeaconDB: &db.BeaconDB{}, + DepositCache: depositcache.NewDepositCache(), + BlockFetcher: &goodFetcher{}, }) if err != nil { t.Fatalf("unable to setup web3 ETH1.0 chain service: %v", err) @@ -321,10 +318,9 @@ func TestProcessETH2GenesisLog(t *testing.T) { Logger: &goodLogger{}, HTTPLogger: &goodLogger{}, ContractBackend: testAcc.Backend, - BeaconDB: &db.BeaconDB{ - DepositCache: depositcache.NewDepositCache(), - }, - BlockFetcher: &goodFetcher{}, + BeaconDB: &db.BeaconDB{}, + DepositCache: depositcache.NewDepositCache(), + BlockFetcher: &goodFetcher{}, }) if err != nil { t.Fatalf("unable to setup web3 ETH1.0 chain service: %v", err) @@ -402,10 +398,9 @@ func TestWeb3ServiceProcessDepositLog_RequestMissedDeposits(t *testing.T) { Logger: &goodLogger{}, HTTPLogger: testAcc.Backend, ContractBackend: testAcc.Backend, - BeaconDB: &db.BeaconDB{ - DepositCache: depositcache.NewDepositCache(), - }, - BlockFetcher: &goodFetcher{}, + BeaconDB: &db.BeaconDB{}, + DepositCache: depositcache.NewDepositCache(), + BlockFetcher: &goodFetcher{}, }) if err != nil { t.Fatalf("unable to setup web3 ETH1.0 chain service: %v", err) diff --git a/beacon-chain/powchain/service.go b/beacon-chain/powchain/service.go index 398040a889..ddf691c442 100644 --- a/beacon-chain/powchain/service.go +++ b/beacon-chain/powchain/service.go @@ -17,6 +17,7 @@ import ( "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" "github.com/prysmaticlabs/prysm/beacon-chain/db" contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" @@ -93,7 +94,8 @@ type Web3Service struct { chainStartDeposits []*ethpb.Deposit chainStarted bool chainStartBlockNumber *big.Int - beaconDB *db.BeaconDB + beaconDB db.Database + depositCache *depositcache.DepositCache lastReceivedMerkleIndex int64 // Keeps track of the last received index to prevent log spam. isRunning bool runError error @@ -115,7 +117,8 @@ type Web3ServiceConfig struct { HTTPLogger bind.ContractFilterer BlockFetcher POWBlockFetcher ContractBackend bind.ContractBackend - BeaconDB *db.BeaconDB + BeaconDB db.Database + DepositCache *depositcache.DepositCache } // NewWeb3Service sets up a new instance with an ethclient when @@ -158,6 +161,7 @@ func NewWeb3Service(ctx context.Context, config *Web3ServiceConfig) (*Web3Servic depositContractCaller: depositContractCaller, chainStartDeposits: make([]*ethpb.Deposit, 0), beaconDB: config.BeaconDB, + depositCache: config.DepositCache, lastReceivedMerkleIndex: -1, lastRequestedBlock: big.NewInt(0), chainStartETH1Data: ðpb.Eth1Data{}, @@ -266,7 +270,7 @@ func (w *Web3Service) AreAllDepositsProcessed() (bool, error) { return false, errors.Wrap(err, "could not get deposit count") } count := bytesutil.FromBytes8(countByte) - deposits := w.beaconDB.DepositCache.AllDeposits(w.ctx, nil) + deposits := w.depositCache.AllDeposits(w.ctx, nil) if count != uint64(len(deposits)) { return false, nil } diff --git a/beacon-chain/rpc/BUILD.bazel b/beacon-chain/rpc/BUILD.bazel index 9970f1b943..f0625b489c 100644 --- a/beacon-chain/rpc/BUILD.bazel +++ b/beacon-chain/rpc/BUILD.bazel @@ -23,6 +23,7 @@ go_library( "//beacon-chain/core/state:go_default_library", "//beacon-chain/core/state/stateutils:go_default_library", "//beacon-chain/db:go_default_library", + "//beacon-chain/db/filters:go_default_library", "//beacon-chain/deprecated-sync:go_default_library", "//beacon-chain/operations:go_default_library", "//beacon-chain/p2p:go_default_library", diff --git a/beacon-chain/rpc/attester_server.go b/beacon-chain/rpc/attester_server.go index 2e83a619a2..a890c7b792 100644 --- a/beacon-chain/rpc/attester_server.go +++ b/beacon-chain/rpc/attester_server.go @@ -23,7 +23,7 @@ import ( // providing RPC methods for validators acting as attesters to broadcast votes on beacon blocks. type AttesterServer struct { p2p p2p.Broadcaster - beaconDB *db.BeaconDB + beaconDB db.Database operationService operationService cache *cache.AttestationCache } @@ -37,7 +37,7 @@ func (as *AttesterServer) SubmitAttestation(ctx context.Context, att *ethpb.Atte // Update attestation target for RPC server to run necessary fork choice. // We need to retrieve the head block to get its parent root. - head, err := as.beaconDB.Block(bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) + head, err := as.beaconDB.Block(ctx, bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) if err != nil { return nil, err } @@ -52,11 +52,16 @@ func (as *AttesterServer) SubmitAttestation(ctx context.Context, att *ethpb.Atte BeaconBlockRoot: att.Data.BeaconBlockRoot, ParentRoot: head.ParentRoot, } - if err := as.beaconDB.SaveAttestationTarget(ctx, attTarget); err != nil { - return nil, fmt.Errorf("could not save attestation target") + db, isLegacyDB := as.beaconDB.(*db.BeaconDB) + if isLegacyDB { + if err := db.SaveAttestationTarget(ctx, attTarget); err != nil { + return nil, fmt.Errorf("could not save attestation target") + } } - as.p2p.Broadcast(ctx, att) + if err := as.p2p.Broadcast(ctx, att); err != nil { + return nil, err + } hash, err := hashutil.HashProto(att) if err != nil { @@ -100,7 +105,7 @@ func (as *AttesterServer) RequestAttestation(ctx context.Context, req *pb.Attest // Set the attestation data's beacon block root = hash_tree_root(head) where head // is the validator's view of the head block of the beacon chain during the slot. - headBlock, err := as.beaconDB.ChainHead() + headBlock, err := as.beaconDB.(*db.BeaconDB).ChainHead() if err != nil { return nil, errors.Wrap(err, "failed to retrieve chain head") } diff --git a/beacon-chain/rpc/attester_server_test.go b/beacon-chain/rpc/attester_server_test.go index 19c397b8b6..fc82593219 100644 --- a/beacon-chain/rpc/attester_server_test.go +++ b/beacon-chain/rpc/attester_server_test.go @@ -9,6 +9,7 @@ import ( "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" + db2 "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/internal" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" @@ -26,6 +27,8 @@ func (m *mockBroadcaster) Broadcast(ctx context.Context, msg proto.Message) erro func TestSubmitAttestation_OK(t *testing.T) { db := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, db) + ctx := context.Background() + mockOperationService := &mockOperationService{} attesterServer := &AttesterServer{ operationService: mockOperationService, @@ -37,7 +40,7 @@ func TestSubmitAttestation_OK(t *testing.T) { Slot: 999, ParentRoot: []byte{'a'}, } - if err := attesterServer.beaconDB.SaveBlock(head); err != nil { + if err := attesterServer.beaconDB.SaveBlock(ctx, head); err != nil { t.Fatal(err) } root, err := ssz.SigningRoot(head) @@ -60,7 +63,7 @@ func TestSubmitAttestation_OK(t *testing.T) { ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), } - if err := db.SaveState(context.Background(), state); err != nil { + if err := db.SaveStateDeprecated(context.Background(), state); err != nil { t.Fatal(err) } @@ -133,22 +136,22 @@ func TestRequestAttestation_OK(t *testing.T) { p2p: &mockBroadcaster{}, cache: cache.NewAttestationCache(), } - if err := attesterServer.beaconDB.SaveBlock(targetBlock); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(targetBlock); err != nil { t.Fatalf("Could not save block in test db: %v", err) } - if err := attesterServer.beaconDB.UpdateChainHead(ctx, targetBlock, beaconState); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, targetBlock, beaconState); err != nil { t.Fatalf("Could not update chain head in test db: %v", err) } - if err := attesterServer.beaconDB.SaveBlock(justifiedBlock); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(justifiedBlock); err != nil { t.Fatalf("Could not save block in test db: %v", err) } - if err := attesterServer.beaconDB.UpdateChainHead(ctx, justifiedBlock, beaconState); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, justifiedBlock, beaconState); err != nil { t.Fatalf("Could not update chain head in test db: %v", err) } - if err := attesterServer.beaconDB.SaveBlock(block); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { t.Fatalf("Could not save block in test db: %v", err) } - if err := attesterServer.beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block, beaconState); err != nil { t.Fatalf("Could not update chain head in test db: %v", err) } req := &pb.AttestationRequest{ @@ -253,22 +256,22 @@ func TestAttestationDataAtSlot_handlesFarAwayJustifiedEpoch(t *testing.T) { p2p: &mockBroadcaster{}, cache: cache.NewAttestationCache(), } - if err := attesterServer.beaconDB.SaveBlock(epochBoundaryBlock); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(epochBoundaryBlock); err != nil { t.Fatalf("Could not save block in test db: %v", err) } - if err := attesterServer.beaconDB.UpdateChainHead(ctx, epochBoundaryBlock, beaconState); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, epochBoundaryBlock, beaconState); err != nil { t.Fatalf("Could not update chain head in test db: %v", err) } - if err := attesterServer.beaconDB.SaveBlock(justifiedBlock); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(justifiedBlock); err != nil { t.Fatalf("Could not save block in test db: %v", err) } - if err := attesterServer.beaconDB.UpdateChainHead(ctx, justifiedBlock, beaconState); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, justifiedBlock, beaconState); err != nil { t.Fatalf("Could not update chain head in test db: %v", err) } - if err := attesterServer.beaconDB.SaveBlock(block); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).SaveBlockDeprecated(block); err != nil { t.Fatalf("Could not save block in test db: %v", err) } - if err := attesterServer.beaconDB.UpdateChainHead(ctx, block, beaconState); err != nil { + if err := attesterServer.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, block, beaconState); err != nil { t.Fatalf("Could not update chain head in test db: %v", err) } req := &pb.AttestationRequest{ diff --git a/beacon-chain/rpc/beacon_chain_server.go b/beacon-chain/rpc/beacon_chain_server.go index 7380f5ee3a..5ac12f25ab 100644 --- a/beacon-chain/rpc/beacon_chain_server.go +++ b/beacon-chain/rpc/beacon_chain_server.go @@ -8,6 +8,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/db" + "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" "github.com/prysmaticlabs/prysm/beacon-chain/operations" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bytesutil" @@ -21,7 +22,7 @@ import ( // providing RPC endpoints to access data relevant to the Ethereum 2.0 phase 0 // beacon chain. type BeaconChainServer struct { - beaconDB *db.BeaconDB + beaconDB db.Database pool operations.Pool } @@ -60,7 +61,7 @@ func (bs *BeaconChainServer) ListAttestations( case *ethpb.ListAttestationsRequest_Epoch: return nil, status.Error(codes.Unimplemented, "not implemented") } - atts, err := bs.beaconDB.Attestations() + atts, err := bs.beaconDB.Attestations(ctx, nil) if err != nil { return nil, status.Errorf(codes.Internal, "could not fetch attestations: %v", err) } @@ -91,7 +92,7 @@ func (bs *BeaconChainServer) ListAttestations( func (bs *BeaconChainServer) AttestationPool( ctx context.Context, _ *ptypes.Empty, ) (*ethpb.AttestationPoolResponse, error) { - headBlock, err := bs.beaconDB.ChainHead() + headBlock, err := bs.beaconDB.(*db.BeaconDB).ChainHead() if err != nil { return nil, err } @@ -124,14 +125,21 @@ func (bs *BeaconChainServer) ListBlocks( endSlot := startSlot + params.BeaconConfig().SlotsPerEpoch var blks []*ethpb.BeaconBlock - for i := startSlot; i < endSlot; i++ { - b, err := bs.beaconDB.BlocksBySlot(ctx, i) - if err != nil { - return nil, status.Errorf(codes.Internal, "could not retrieve blocks for slot %d: %v", i, err) + if d, isLegacyDB := bs.beaconDB.(*db.BeaconDB); isLegacyDB { + for i := startSlot; i < endSlot; i++ { + b, err := d.BlocksBySlot(ctx, i) + if err != nil { + return nil, status.Errorf(codes.Internal, "could not retrieve blocks for slot %d: %v", i, err) + } + blks = append(blks, b...) + } + } else { + var err error + blks, err = bs.beaconDB.Blocks(ctx, filters.NewFilter().SetStartSlot(startSlot).SetEndSlot(endSlot)) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to get blocks: %v", err) } - blks = append(blks, b...) } - numBlks := len(blks) if numBlks == 0 { return ðpb.ListBlocksResponse{Blocks: []*ethpb.BeaconBlock{}, TotalSize: 0}, nil @@ -149,7 +157,7 @@ func (bs *BeaconChainServer) ListBlocks( }, nil case *ethpb.ListBlocksRequest_Root: - blk, err := bs.beaconDB.Block(bytesutil.ToBytes32(q.Root)) + blk, err := bs.beaconDB.Block(ctx, bytesutil.ToBytes32(q.Root)) if err != nil { return nil, status.Errorf(codes.Internal, "could not retrieve block: %v", err) } @@ -164,7 +172,13 @@ func (bs *BeaconChainServer) ListBlocks( }, nil case *ethpb.ListBlocksRequest_Slot: - blks, err := bs.beaconDB.BlocksBySlot(ctx, q.Slot) + var blks []*ethpb.BeaconBlock + var err error + if d, isLegacyDB := bs.beaconDB.(*db.BeaconDB); isLegacyDB { + blks, err = d.BlocksBySlot(ctx, q.Slot) + } else { + blks, err = bs.beaconDB.Blocks(ctx, filters.NewFilter().SetStartSlot(q.Slot).SetEndSlot(q.Slot)) + } if err != nil { return nil, status.Errorf(codes.Internal, "could not retrieve blocks for slot %d: %v", q.Slot, err) } @@ -212,11 +226,11 @@ func (bs *BeaconChainServer) ListValidatorBalances( res := make([]*ethpb.ValidatorBalances_Balance, 0, len(req.PublicKeys)+len(req.Indices)) filtered := map[uint64]bool{} // track filtered validators to prevent duplication in the response. - balances, err := bs.beaconDB.Balances(ctx) + balances, err := bs.beaconDB.(*db.BeaconDB).Balances(ctx) if err != nil { return nil, status.Errorf(codes.Internal, "could not retrieve validator balances: %v", err) } - validators, err := bs.beaconDB.Validators(ctx) + validators, err := bs.beaconDB.(*db.BeaconDB).Validators(ctx) if err != nil { return nil, status.Errorf(codes.Internal, "could not retrieve validators: %v", err) } @@ -227,7 +241,7 @@ func (bs *BeaconChainServer) ListValidatorBalances( continue } - index, err := bs.beaconDB.ValidatorIndex(pubKey) + index, err := bs.beaconDB.(*db.BeaconDB).ValidatorIndexDeprecated(pubKey) if err != nil { return nil, status.Errorf(codes.Internal, "could not retrieve validator index: %v", err) } @@ -275,11 +289,12 @@ func (bs *BeaconChainServer) GetValidators( req.PageSize, params.BeaconConfig().MaxPageSize) } - validators, err := bs.beaconDB.Validators(ctx) + head, err := bs.beaconDB.HeadState(ctx) if err != nil { - return nil, status.Errorf(codes.Internal, "could not retrieve validators: %v", err) + return nil, status.Errorf(codes.Internal, "could not get head state %v", err) } - validatorCount := len(validators) + + validatorCount := len(head.Validators) start, end, nextPageToken, err := pagination.StartAndEndPage(req.PageToken, int(req.PageSize), validatorCount) if err != nil { @@ -287,7 +302,7 @@ func (bs *BeaconChainServer) GetValidators( } res := ðpb.Validators{ - Validators: validators[start:end], + Validators: head.Validators[start:end], TotalSize: int32(validatorCount), NextPageToken: nextPageToken, } @@ -335,7 +350,7 @@ func (bs *BeaconChainServer) ListValidatorAssignments( // Filter out assignments by public keys. for _, pubKey := range req.PublicKeys { - index, err := bs.beaconDB.ValidatorIndex(pubKey) + index, err := bs.beaconDB.(*db.BeaconDB).ValidatorIndexDeprecated(pubKey) if err != nil { return nil, status.Errorf(codes.Internal, "could not retrieve validator index: %v", err) } diff --git a/beacon-chain/rpc/beacon_chain_server_test.go b/beacon-chain/rpc/beacon_chain_server_test.go index d3be967430..33876ee7cd 100644 --- a/beacon-chain/rpc/beacon_chain_server_test.go +++ b/beacon-chain/rpc/beacon_chain_server_test.go @@ -13,6 +13,7 @@ import ( "github.com/prysmaticlabs/go-bitfield" "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" + db2 "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/internal" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" @@ -259,10 +260,10 @@ func TestBeaconChainServer_AttestationPool(t *testing.T) { pool: &mockPool{}, beaconDB: db, } - if err := bs.beaconDB.SaveBlock(ðpb.BeaconBlock{Slot: 10}); err != nil { + if err := bs.beaconDB.SaveBlock(ctx, ðpb.BeaconBlock{Slot: 10}); err != nil { t.Fatal(err) } - if err := bs.beaconDB.UpdateChainHead(ctx, ðpb.BeaconBlock{Slot: 10}, &pbp2p.BeaconState{Slot: 10}); err != nil { + if err := bs.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, ðpb.BeaconBlock{Slot: 10}, &pbp2p.BeaconState{Slot: 10}); err != nil { t.Fatal(err) } res, err := bs.AttestationPool(ctx, &ptypes.Empty{}) @@ -283,14 +284,14 @@ func TestBeaconChainServer_ListValidatorBalances(t *testing.T) { balances := make([]uint64, count) validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndex([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { t.Fatal(err) } balances[i] = uint64(i) validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}}) } - if err := db.SaveState( + if err := db.SaveStateDeprecated( context.Background(), &pbp2p.BeaconState{Validators: validators, Balances: balances}); err != nil { t.Fatal(err) @@ -352,14 +353,14 @@ func TestBeaconChainServer_ListValidatorBalancesOutOfRange(t *testing.T) { balances := make([]uint64, count) validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndex([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { t.Fatal(err) } balances[i] = uint64(i) validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}}) } - if err := db.SaveState( + if err := db.SaveStateDeprecated( context.Background(), &pbp2p.BeaconState{Validators: validators, Balances: balances}); err != nil { t.Fatal(err) @@ -383,13 +384,13 @@ func TestBeaconChainServer_GetValidatorsNoPagination(t *testing.T) { count := 100 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndex([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { t.Fatal(err) } validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}}) } - if err := db.SaveState( + if err := db.SaveStateDeprecated( context.Background(), &pbp2p.BeaconState{Validators: validators}); err != nil { t.Fatal(err) @@ -417,14 +418,14 @@ func TestBeaconChainServer_GetValidatorsPagination(t *testing.T) { balances := make([]uint64, count) validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndex([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { t.Fatal(err) } balances[i] = uint64(i) validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}}) } - if err := db.SaveState( + if err := db.SaveStateDeprecated( context.Background(), &pbp2p.BeaconState{Validators: validators, Balances: balances}); err != nil { t.Fatal(err) @@ -488,13 +489,13 @@ func TestBeaconChainServer_GetValidatorsPaginationOutOfRange(t *testing.T) { count := 1 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndex([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { t.Fatal(err) } validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}}) } - if err := db.SaveState( + if err := db.SaveStateDeprecated( context.Background(), &pbp2p.BeaconState{Validators: validators}); err != nil { t.Fatal(err) @@ -529,13 +530,13 @@ func TestBeaconChainServer_GetValidatorsDefaultPageSize(t *testing.T) { count := 1000 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndex([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { t.Fatal(err) } validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}}) } - if err := db.SaveState( + if err := db.SaveStateDeprecated( context.Background(), &pbp2p.BeaconState{Validators: validators}); err != nil { t.Fatal(err) @@ -565,13 +566,13 @@ func TestBeaconChainServer_ListAssignmentsInputOutOfRange(t *testing.T) { count := 1 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndex([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { t.Fatal(err) } validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}}) } - if err := db.SaveState( + if err := db.SaveStateDeprecated( context.Background(), &pbp2p.BeaconState{Validators: validators}); err != nil { t.Fatal(err) @@ -603,7 +604,7 @@ func TestBeaconChainServer_ListAssignmentsDefaultPageSize(t *testing.T) { count := 1000 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndex([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { t.Fatal(err) } // Mark the validators with index divisible by 3 inactive. @@ -618,7 +619,7 @@ func TestBeaconChainServer_ListAssignmentsDefaultPageSize(t *testing.T) { Validators: validators, RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)} - if err := db.SaveState(context.Background(), s); err != nil { + if err := db.SaveStateDeprecated(context.Background(), s); err != nil { t.Fatal(err) } @@ -665,7 +666,7 @@ func TestBeaconChainServer_ListAssignmentsFilterPubkeysIndicesNoPage(t *testing. count := 100 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndex([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { t.Fatal(err) } validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}, ExitEpoch: params.BeaconConfig().FarFutureEpoch}) @@ -675,7 +676,7 @@ func TestBeaconChainServer_ListAssignmentsFilterPubkeysIndicesNoPage(t *testing. Validators: validators, RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)} - if err := db.SaveState(context.Background(), s); err != nil { + if err := db.SaveStateDeprecated(context.Background(), s); err != nil { t.Fatal(err) } @@ -723,7 +724,7 @@ func TestBeaconChainServer_ListAssignmentsCanFilterPubkeysIndicesWithPages(t *te count := 100 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndex([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { t.Fatal(err) } validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}, ExitEpoch: params.BeaconConfig().FarFutureEpoch}) @@ -733,7 +734,7 @@ func TestBeaconChainServer_ListAssignmentsCanFilterPubkeysIndicesWithPages(t *te Validators: validators, RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)} - if err := db.SaveState(context.Background(), s); err != nil { + if err := db.SaveStateDeprecated(context.Background(), s); err != nil { t.Fatal(err) } @@ -860,10 +861,10 @@ func TestBeaconChainServer_GetValidatorsParticipation(t *testing.T) { beaconDB: db, } - if err := bs.beaconDB.SaveState(context.Background(), s); err != nil { + if err := bs.beaconDB.(*db2.BeaconDB).SaveStateDeprecated(context.Background(), s); err != nil { t.Fatal(err) } - if err := bs.beaconDB.SaveFinalizedBlock(ðpb.BeaconBlock{Slot: 1}); err != nil { + if err := bs.beaconDB.(*db2.BeaconDB).SaveFinalizedBlock(ðpb.BeaconBlock{Slot: 1}); err != nil { t.Fatal(err) } @@ -895,7 +896,7 @@ func TestBeaconChainServer_ListBlocksPagination(t *testing.T) { b := ðpb.BeaconBlock{ Slot: i, } - if err := db.SaveBlock(b); err != nil { + if err := db.SaveBlockDeprecated(b); err != nil { t.Fatal(err) } blks[i] = b diff --git a/beacon-chain/rpc/beacon_server.go b/beacon-chain/rpc/beacon_server.go index 70add3a9de..3aa77b9c31 100644 --- a/beacon-chain/rpc/beacon_server.go +++ b/beacon-chain/rpc/beacon_server.go @@ -7,21 +7,21 @@ import ( ptypes "github.com/gogo/protobuf/types" "github.com/pkg/errors" - "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/blockchain" - "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/db" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/trieutil" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) // BeaconServer defines a server implementation of the gRPC Beacon service, // providing RPC endpoints for obtaining the canonical beacon chain head, // fetching latest observed attestations, and more. type BeaconServer struct { - beaconDB *db.BeaconDB + beaconDB db.Database ctx context.Context powChainService powChainService chainService chainService @@ -71,7 +71,7 @@ func (bs *BeaconServer) WaitForChainStart(req *ptypes.Empty, stream pb.BeaconSer // CanonicalHead of the current beacon chain. This method is requested on-demand // by a validator when it is their time to propose or attest. func (bs *BeaconServer) CanonicalHead(ctx context.Context, req *ptypes.Empty) (*ethpb.BeaconBlock, error) { - block, err := bs.beaconDB.ChainHead() + block, err := bs.beaconDB.HeadBlock(ctx) if err != nil { return nil, errors.Wrap(err, "could not get canonical head block") } @@ -80,130 +80,15 @@ func (bs *BeaconServer) CanonicalHead(ctx context.Context, req *ptypes.Empty) (* // BlockTree returns the current tree of saved blocks and their votes starting from the justified state. func (bs *BeaconServer) BlockTree(ctx context.Context, _ *ptypes.Empty) (*pb.BlockTreeResponse, error) { - justifiedState, err := bs.beaconDB.JustifiedState() - if err != nil { - return nil, errors.Wrap(err, "could not retrieve justified state") - } - attestationTargets, err := bs.targetsFetcher.AttestationTargets(justifiedState) - if err != nil { - return nil, errors.Wrap(err, "could not retrieve attestation target") - } - justifiedBlock, err := bs.beaconDB.JustifiedBlock() - if err != nil { - return nil, err - } - highestSlot := bs.beaconDB.HighestBlockSlot() - fullBlockTree := []*ethpb.BeaconBlock{} - for i := justifiedBlock.Slot + 1; i < highestSlot; i++ { - if ctx.Err() != nil { - return nil, ctx.Err() - } - nextLayer, err := bs.beaconDB.BlocksBySlot(ctx, i) - if err != nil { - return nil, err - } - fullBlockTree = append(fullBlockTree, nextLayer...) - } - tree := []*pb.BlockTreeResponse_TreeNode{} - for _, kid := range fullBlockTree { - if ctx.Err() != nil { - return nil, ctx.Err() - } - participatedVotes, err := blockchain.VoteCount(kid, justifiedState, attestationTargets, bs.beaconDB) - if err != nil { - return nil, err - } - blockRoot, err := ssz.SigningRoot(kid) - if err != nil { - return nil, err - } - tree = append(tree, &pb.BlockTreeResponse_TreeNode{ - BlockRoot: blockRoot[:], - Block: kid, - ParticipatedVotes: uint64(participatedVotes), - }) - } - return &pb.BlockTreeResponse{ - Tree: tree, - }, nil + // TODO(3219): Add after new fork choice service. + return nil, status.Error(codes.Unimplemented, "not implemented") } // BlockTreeBySlots returns the current tree of saved blocks and their // votes starting from the justified state. func (bs *BeaconServer) BlockTreeBySlots(ctx context.Context, req *pb.TreeBlockSlotRequest) (*pb.BlockTreeResponse, error) { - justifiedState, err := bs.beaconDB.JustifiedState() - if err != nil { - return nil, errors.Wrap(err, "could not retrieve justified state") - } - attestationTargets, err := bs.targetsFetcher.AttestationTargets(justifiedState) - if err != nil { - return nil, errors.Wrap(err, "could not retrieve attestation target") - } - justifiedBlock, err := bs.beaconDB.JustifiedBlock() - if err != nil { - return nil, err - } - if req == nil { - return nil, errors.New("argument 'TreeBlockSlotRequest' cannot be nil") - } - if !(req.SlotFrom <= req.SlotTo) { - return nil, fmt.Errorf("upper limit (%d) of slot range cannot be lower than the lower limit (%d)", req.SlotTo, req.SlotFrom) - } - highestSlot := bs.beaconDB.HighestBlockSlot() - fullBlockTree := []*ethpb.BeaconBlock{} - for i := justifiedBlock.Slot + 1; i < highestSlot; i++ { - if ctx.Err() != nil { - return nil, ctx.Err() - } - if i >= req.SlotFrom && i <= req.SlotTo { - nextLayer, err := bs.beaconDB.BlocksBySlot(ctx, i) - if err != nil { - return nil, err - } - if nextLayer != nil { - fullBlockTree = append(fullBlockTree, nextLayer...) - } - } - if i > req.SlotTo { - break - } - } - tree := []*pb.BlockTreeResponse_TreeNode{} - for _, kid := range fullBlockTree { - if ctx.Err() != nil { - return nil, ctx.Err() - } - participatedVotes, err := blockchain.VoteCount(kid, justifiedState, attestationTargets, bs.beaconDB) - if err != nil { - return nil, err - } - blockRoot, err := ssz.SigningRoot(kid) - if err != nil { - return nil, err - } - hState, err := bs.beaconDB.HistoricalStateFromSlot(ctx, kid.Slot, blockRoot) - if err != nil { - return nil, err - } - if kid.Slot >= req.SlotFrom && kid.Slot <= req.SlotTo { - activeValidatorIndices, err := helpers.ActiveValidatorIndices(hState, helpers.CurrentEpoch(hState)) - if err != nil { - return nil, err - } - - totalVotes := helpers.TotalBalance(hState, activeValidatorIndices) - - tree = append(tree, &pb.BlockTreeResponse_TreeNode{ - BlockRoot: blockRoot[:], - Block: kid, - ParticipatedVotes: uint64(participatedVotes), - TotalVotes: uint64(totalVotes), - }) - } - } - return &pb.BlockTreeResponse{ - Tree: tree, - }, nil + // TODO(3219): Add after new fork choice service. + return nil, status.Error(codes.Unimplemented, "not implemented") } func constructMerkleProof(trie *trieutil.MerkleTrie, index int, deposit *ethpb.Deposit) (*ethpb.Deposit, error) { diff --git a/beacon-chain/rpc/beacon_server_test.go b/beacon-chain/rpc/beacon_server_test.go index 1756d84d17..cf640e1ca9 100644 --- a/beacon-chain/rpc/beacon_server_test.go +++ b/beacon-chain/rpc/beacon_server_test.go @@ -248,6 +248,7 @@ func TestWaitForChainStart_NotStartedThenLogFired(t *testing.T) { } func TestBlockTree_OK(t *testing.T) { + t.Skip() // TODO(3219): Add after new fork choice service. db := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, db) ctx := context.Background() @@ -439,7 +440,7 @@ func TestBlockTree_OK(t *testing.T) { }, } for _, node := range tree { - if err := db.SaveBlock(node.Block); err != nil { + if err := db.SaveBlockDeprecated(node.Block); err != nil { t.Fatal(err) } } @@ -469,6 +470,7 @@ func TestBlockTree_OK(t *testing.T) { } func TestBlockTreeBySlots_ArgsValildation(t *testing.T) { + t.Skip() // TODO(3219): Add after new fork choice service. db := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, db) ctx := context.Background() @@ -650,7 +652,7 @@ func TestBlockTreeBySlots_ArgsValildation(t *testing.T) { }, } for _, node := range tree { - if err := db.SaveBlock(node.Block); err != nil { + if err := db.SaveBlockDeprecated(node.Block); err != nil { t.Fatal(err) } } @@ -678,6 +680,7 @@ func TestBlockTreeBySlots_ArgsValildation(t *testing.T) { } } func TestBlockTreeBySlots_OK(t *testing.T) { + t.Skip() // TODO(3219): Add after new fork choice service. helpers.ClearAllCaches() db := internal.SetupDBDeprecated(t) @@ -865,7 +868,7 @@ func TestBlockTreeBySlots_OK(t *testing.T) { }, } for _, node := range tree { - if err := db.SaveBlock(node.Block); err != nil { + if err := db.SaveBlockDeprecated(node.Block); err != nil { t.Fatal(err) } } diff --git a/beacon-chain/rpc/node_server.go b/beacon-chain/rpc/node_server.go index d4234f9426..9e06a37797 100644 --- a/beacon-chain/rpc/node_server.go +++ b/beacon-chain/rpc/node_server.go @@ -21,7 +21,7 @@ import ( type NodeServer struct { syncChecker sync.Checker server *grpc.Server - beaconDB *db.BeaconDB + beaconDB db.Database } // GetSyncStatus checks the current network sync status of the node. @@ -33,11 +33,13 @@ func (ns *NodeServer) GetSyncStatus(ctx context.Context, _ *ptypes.Empty) (*ethp // GetGenesis fetches genesis chain information of Ethereum 2.0. func (ns *NodeServer) GetGenesis(ctx context.Context, _ *ptypes.Empty) (*ethpb.Genesis, error) { - beaconState, err := ns.beaconDB.FinalizedState() + // TODO(3045): Use the db.Database interface only. + beaconState, err := ns.beaconDB.(*db.BeaconDB).FinalizedState() if err != nil { return nil, status.Errorf(codes.Internal, "could not retrieve beacon state: %v", err) } - address, err := ns.beaconDB.DepositContractAddress(ctx) + // TODO(3045): Use the db.Database interface only. + address, err := ns.beaconDB.(*db.BeaconDB).DepositContractAddress(ctx) if err != nil { return nil, status.Errorf(codes.Internal, "could not retrieve deposit contract address: %v", err) } diff --git a/beacon-chain/rpc/proposer_server.go b/beacon-chain/rpc/proposer_server.go index 0144982bde..32d2a863ab 100644 --- a/beacon-chain/rpc/proposer_server.go +++ b/beacon-chain/rpc/proposer_server.go @@ -25,11 +25,12 @@ import ( // providing RPC endpoints for computing state transitions and state roots, proposing // beacon blocks to a beacon node, and more. type ProposerServer struct { - beaconDB *db.BeaconDB + beaconDB db.Database chainService chainService powChainService powChainService operationService operationService canonicalStateChan chan *pbp2p.BeaconState + depositCache *depositcache.DepositCache } // RequestBlock is called by a proposer during its assigned slot to request a block to sign @@ -37,7 +38,7 @@ type ProposerServer struct { func (ps *ProposerServer) RequestBlock(ctx context.Context, req *pb.BlockRequest) (*ethpb.BeaconBlock, error) { // Retrieve the parent block as the current head of the canonical chain - parent, err := ps.beaconDB.ChainHead() + parent, err := ps.beaconDB.HeadBlock(ctx) if err != nil { return nil, errors.Wrap(err, "could not get canonical head block") } @@ -115,8 +116,11 @@ func (ps *ProposerServer) ProposeBlock(ctx context.Context, blk *ethpb.BeaconBlo return nil, errors.Wrap(err, "could not process beacon block") } - if err := ps.beaconDB.UpdateChainHead(ctx, blk, beaconState); err != nil { - return nil, errors.Wrap(err, "failed to update chain") + db, isLegacyDB := ps.beaconDB.(*db.BeaconDB) + if isLegacyDB { + if err := db.UpdateChainHead(ctx, blk, beaconState); err != nil { + return nil, errors.Wrap(err, "failed to update chain") + } } ps.chainService.UpdateCanonicalRoots(blk, root) @@ -179,7 +183,12 @@ func (ps *ProposerServer) attestations(ctx context.Context, expectedSlot uint64) "slot": slot, "headRoot": fmt.Sprintf("%#x", bytesutil.Trunc(att.Data.BeaconBlockRoot))}).Info( "Deleting failed pending attestation from DB") - if err := ps.beaconDB.DeleteAttestation(att); err != nil { + + hash, err := ssz.HashTreeRoot(att) + if err != nil { + return nil, err + } + if err := ps.beaconDB.DeleteAttestation(ctx, hash); err != nil { return nil, errors.Wrap(err, "could not delete failed attestation") } continue @@ -187,7 +196,11 @@ func (ps *ProposerServer) attestations(ctx context.Context, expectedSlot uint64) canonical, err := ps.operationService.IsAttCanonical(ctx, att) if err != nil { // Delete attestation that failed to verify as canonical. - if err := ps.beaconDB.DeleteAttestation(att); err != nil { + hash, err := ssz.HashTreeRoot(att) + if err != nil { + return nil, err + } + if err := ps.beaconDB.DeleteAttestation(ctx, hash); err != nil { return nil, errors.Wrap(err, "could not delete failed attestation") } return nil, errors.Wrap(err, "could not verify canonical attestation") @@ -268,7 +281,7 @@ func (ps *ProposerServer) deposits(ctx context.Context, currentVote *ethpb.Eth1D return []*ethpb.Deposit{}, nil } - upToEth1DataDeposits := ps.beaconDB.DepositCache.AllDeposits(ctx, latestEth1DataHeight) + upToEth1DataDeposits := ps.depositCache.AllDeposits(ctx, latestEth1DataHeight) depositData := [][]byte{} for _, dep := range upToEth1DataDeposits { depHash, err := ssz.HashTreeRoot(dep.Data) @@ -283,7 +296,7 @@ func (ps *ProposerServer) deposits(ctx context.Context, currentVote *ethpb.Eth1D return nil, errors.Wrap(err, "could not generate historical deposit trie from deposits") } - allPendingContainers := ps.beaconDB.DepositCache.PendingContainers(ctx, latestEth1DataHeight) + allPendingContainers := ps.depositCache.PendingContainers(ctx, latestEth1DataHeight) // Deposits need to be received in order of merkle index root, so this has to make sure // deposits are sorted from lowest to highest. @@ -349,7 +362,7 @@ func (ps *ProposerServer) defaultEth1DataResponse(ctx context.Context, currentHe return nil, errors.Wrap(err, "could not fetch ETH1_FOLLOW_DISTANCE ancestor") } // Fetch all historical deposits up to an ancestor height. - depositsTillHeight, depositRoot := ps.beaconDB.DepositCache.DepositsNumberAndRootAtHeight(ctx, ancestorHeight) + depositsTillHeight, depositRoot := ps.depositCache.DepositsNumberAndRootAtHeight(ctx, ancestorHeight) if depositsTillHeight == 0 { return ps.powChainService.ChainStartETH1Data(), nil } diff --git a/beacon-chain/rpc/proposer_server_test.go b/beacon-chain/rpc/proposer_server_test.go index bc9aa067f8..fe6c23ac5d 100644 --- a/beacon-chain/rpc/proposer_server_test.go +++ b/beacon-chain/rpc/proposer_server_test.go @@ -30,13 +30,14 @@ func init() { } func TestProposeBlock_OK(t *testing.T) { + helpers.ClearAllCaches() db := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, db) mockChain := &mockChainService{} ctx := context.Background() genesis := b.NewGenesisBlock([]byte{}) - if err := db.SaveBlock(genesis); err != nil { + if err := db.SaveBlock(context.Background(), genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } @@ -60,7 +61,7 @@ func TestProposeBlock_OK(t *testing.T) { Slot: 5, ParentRoot: []byte("parent-hash"), } - if err := proposerServer.beaconDB.SaveBlock(req); err != nil { + if err := proposerServer.beaconDB.SaveBlock(ctx, req); err != nil { t.Fatal(err) } if _, err := proposerServer.ProposeBlock(context.Background(), req); err != nil { @@ -88,7 +89,7 @@ func TestComputeStateRoot_OK(t *testing.T) { } genesis := b.NewGenesisBlock(stateRoot[:]) - if err := db.SaveBlock(genesis); err != nil { + if err := db.SaveBlockDeprecated(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } @@ -235,7 +236,7 @@ func TestPendingAttestations_FiltersWithinInclusionDelay(t *testing.T) { chainService: &mockChainService{}, beaconDB: db, } - if err := db.SaveState(ctx, beaconState); err != nil { + if err := db.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } @@ -243,7 +244,7 @@ func TestPendingAttestations_FiltersWithinInclusionDelay(t *testing.T) { Slot: beaconState.Slot, } - if err := db.SaveBlock(blk); err != nil { + if err := db.SaveBlockDeprecated(blk); err != nil { t.Fatalf("failed to save block %v", err) } @@ -396,7 +397,7 @@ func TestPendingAttestations_FiltersExpiredAttestations(t *testing.T) { beaconDB: db, } - if err := db.SaveState(ctx, beaconState); err != nil { + if err := db.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } @@ -404,7 +405,7 @@ func TestPendingAttestations_FiltersExpiredAttestations(t *testing.T) { Slot: beaconState.Slot, } - if err := db.SaveBlock(blk); err != nil { + if err := db.SaveBlockDeprecated(blk); err != nil { t.Fatalf("failed to save block %v", err) } @@ -490,7 +491,7 @@ func TestPendingDeposits_Eth1DataVoteOK(t *testing.T) { Eth1DepositIndex: 2, Eth1DataVotes: votes, } - if err := d.SaveState(ctx, beaconState); err != nil { + if err := d.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } @@ -566,7 +567,7 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) { }, Eth1DepositIndex: 2, } - if err := d.SaveState(ctx, beaconState); err != nil { + if err := d.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } @@ -619,6 +620,7 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) { }}, }, } + depositCache := depositcache.NewDepositCache() depositTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth)) if err != nil { t.Fatalf("could not setup deposit trie: %v", err) @@ -633,16 +635,17 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) { t.Fatalf("Unable to insert deposit into trie %v", err) } - d.DepositCache.InsertDeposit(ctx, dp.Deposit, dp.Block, dp.Index, depositTrie.Root()) + depositCache.InsertDeposit(ctx, dp.Deposit, dp.Block, dp.Index, depositTrie.Root()) } for _, dp := range recentDeposits { - d.DepositCache.InsertPendingDeposit(ctx, dp.Deposit, dp.Block, dp.Index, depositTrie.Root()) + depositCache.InsertPendingDeposit(ctx, dp.Deposit, dp.Block, dp.Index, depositTrie.Root()) } bs := &ProposerServer{ beaconDB: d, powChainService: p, chainService: newMockChainService(), + depositCache: depositCache, } deposits, err := bs.deposits(ctx, ðpb.Eth1Data{}) @@ -701,7 +704,7 @@ func TestPendingDeposits_FollowsCorrectEth1Block(t *testing.T) { Eth1DepositIndex: 1, Eth1DataVotes: votes, } - if err := d.SaveState(ctx, beaconState); err != nil { + if err := d.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } @@ -754,6 +757,7 @@ func TestPendingDeposits_FollowsCorrectEth1Block(t *testing.T) { }}, }, } + depositCache := depositcache.NewDepositCache() depositTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth)) if err != nil { t.Fatalf("could not setup deposit trie: %v", err) @@ -768,16 +772,17 @@ func TestPendingDeposits_FollowsCorrectEth1Block(t *testing.T) { t.Fatalf("Unable to insert deposit into trie %v", err) } - d.DepositCache.InsertDeposit(ctx, dp.Deposit, dp.Block, dp.Index, depositTrie.Root()) + depositCache.InsertDeposit(ctx, dp.Deposit, dp.Block, dp.Index, depositTrie.Root()) } for _, dp := range recentDeposits { - d.DepositCache.InsertPendingDeposit(ctx, dp.Deposit, dp.Block, dp.Index, depositTrie.Root()) + depositCache.InsertPendingDeposit(ctx, dp.Deposit, dp.Block, dp.Index, depositTrie.Root()) } bs := &ProposerServer{ beaconDB: d, powChainService: p, chainService: newMockChainService(), + depositCache: depositCache, } deposits, err := bs.deposits(ctx, ðpb.Eth1Data{}) @@ -824,7 +829,7 @@ func TestPendingDeposits_CantReturnBelowStateEth1DepositIndex(t *testing.T) { }, Eth1DepositIndex: 10, } - if err := d.SaveState(ctx, beaconState); err != nil { + if err := d.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } @@ -868,6 +873,7 @@ func TestPendingDeposits_CantReturnBelowStateEth1DepositIndex(t *testing.T) { if err != nil { t.Fatalf("could not setup deposit trie: %v", err) } + depositCache := depositcache.NewDepositCache() for _, dp := range append(readyDeposits, recentDeposits...) { depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) if err != nil { @@ -878,16 +884,17 @@ func TestPendingDeposits_CantReturnBelowStateEth1DepositIndex(t *testing.T) { t.Fatalf("Unable to insert deposit into trie %v", err) } - d.DepositCache.InsertDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) + depositCache.InsertDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) } for _, dp := range recentDeposits { - d.DepositCache.InsertPendingDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) + depositCache.InsertPendingDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) } bs := &ProposerServer{ beaconDB: d, powChainService: p, chainService: newMockChainService(), + depositCache: depositCache, } // It should also return the recent deposits after their follow window. @@ -926,7 +933,7 @@ func TestPendingDeposits_CantReturnMoreThanMax(t *testing.T) { }, Eth1DepositIndex: 2, } - if err := d.SaveState(ctx, beaconState); err != nil { + if err := d.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } var mockSig [96]byte @@ -969,6 +976,7 @@ func TestPendingDeposits_CantReturnMoreThanMax(t *testing.T) { if err != nil { t.Fatalf("could not setup deposit trie: %v", err) } + depositCache := depositcache.NewDepositCache() for _, dp := range append(readyDeposits, recentDeposits...) { depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) if err != nil { @@ -979,16 +987,17 @@ func TestPendingDeposits_CantReturnMoreThanMax(t *testing.T) { t.Fatalf("Unable to insert deposit into trie %v", err) } - d.DepositCache.InsertDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) + depositCache.InsertDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) } for _, dp := range recentDeposits { - d.DepositCache.InsertPendingDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) + depositCache.InsertPendingDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) } bs := &ProposerServer{ beaconDB: d, powChainService: p, chainService: newMockChainService(), + depositCache: depositCache, } // It should also return the recent deposits after their follow window. @@ -1025,7 +1034,7 @@ func TestPendingDeposits_CantReturnMoreDepositCount(t *testing.T) { }, Eth1DepositIndex: 2, } - if err := d.SaveState(ctx, beaconState); err != nil { + if err := d.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } var mockSig [96]byte @@ -1068,6 +1077,7 @@ func TestPendingDeposits_CantReturnMoreDepositCount(t *testing.T) { if err != nil { t.Fatalf("could not setup deposit trie: %v", err) } + depositCache := depositcache.NewDepositCache() for _, dp := range append(readyDeposits, recentDeposits...) { depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) if err != nil { @@ -1078,16 +1088,17 @@ func TestPendingDeposits_CantReturnMoreDepositCount(t *testing.T) { t.Fatalf("Unable to insert deposit into trie %v", err) } - d.DepositCache.InsertDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) + depositCache.InsertDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) } for _, dp := range recentDeposits { - d.DepositCache.InsertPendingDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) + depositCache.InsertPendingDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) } bs := &ProposerServer{ beaconDB: d, powChainService: p, chainService: newMockChainService(), + depositCache: depositCache, } // It should also return the recent deposits after their follow window. @@ -1122,7 +1133,7 @@ func TestEth1Data_EmptyVotesFetchBlockHashFailure(t *testing.T) { }, Eth1DataVotes: []*ethpb.Eth1Data{}, } - if err := proposerServer.beaconDB.SaveState(ctx, beaconState); err != nil { + if err := proposerServer.beaconDB.SaveState(ctx, beaconState, [32]byte{}); err != nil { t.Fatal(err) } want := "could not fetch ETH1_FOLLOW_DISTANCE ancestor" @@ -1163,8 +1174,9 @@ func TestDefaultEth1Data_NoBlockExists(t *testing.T) { if err != nil { t.Fatalf("could not setup deposit trie: %v", err) } + depositCache := depositcache.NewDepositCache() for _, dp := range deps { - beaconDB.DepositCache.InsertDeposit(context.Background(), dp.Deposit, dp.Block, dp.Index, depositTrie.Root()) + depositCache.InsertDeposit(context.Background(), dp.Deposit, dp.Block, dp.Index, depositTrie.Root()) } powChainService := &mockPOWChainService{ @@ -1177,6 +1189,7 @@ func TestDefaultEth1Data_NoBlockExists(t *testing.T) { proposerServer := &ProposerServer{ beaconDB: beaconDB, powChainService: powChainService, + depositCache: depositCache, } defEth1Data := ðpb.Eth1Data{ @@ -1216,7 +1229,8 @@ func TestEth1Data(t *testing.T) { DepositCount: 55, }, }, - beaconDB: beaconDB, + beaconDB: beaconDB, + depositCache: depositcache.NewDepositCache(), } ctx := context.Background() @@ -1266,10 +1280,11 @@ func Benchmark_Eth1Data(b *testing.B) { }, } + depositCache := depositcache.NewDepositCache() for i, dp := range deposits { var root [32]byte copy(root[:], []byte{'d', 'e', 'p', 'o', 's', 'i', 't', byte(i)}) - beaconDB.DepositCache.InsertDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, root) + depositCache.InsertDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, root) } numOfVotes := 1000 for i := 0; i < numOfVotes; i++ { @@ -1283,7 +1298,7 @@ func Benchmark_Eth1Data(b *testing.B) { } hashesByHeight[numOfVotes+1] = []byte("stub") - if err := beaconDB.SaveState(ctx, beaconState); err != nil { + if err := beaconDB.SaveStateDeprecated(ctx, beaconState); err != nil { b.Fatal(err) } currentHeight := params.BeaconConfig().Eth1FollowDistance + 5 @@ -1293,6 +1308,7 @@ func Benchmark_Eth1Data(b *testing.B) { latestBlockNumber: big.NewInt(int64(currentHeight)), hashesByHeight: hashesByHeight, }, + depositCache: depositCache, } b.ResetTimer() for i := 0; i < b.N; i++ { @@ -1322,7 +1338,7 @@ func TestDeposits_ReturnsEmptyList_IfLatestEth1DataEqGenesisEth1Block(t *testing }, Eth1DepositIndex: 2, } - if err := d.SaveState(ctx, beaconState); err != nil { + if err := d.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatal(err) } var mockSig [96]byte @@ -1365,6 +1381,7 @@ func TestDeposits_ReturnsEmptyList_IfLatestEth1DataEqGenesisEth1Block(t *testing if err != nil { t.Fatalf("could not setup deposit trie: %v", err) } + depositCache := depositcache.NewDepositCache() for _, dp := range append(readyDeposits, recentDeposits...) { depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data) if err != nil { @@ -1375,16 +1392,17 @@ func TestDeposits_ReturnsEmptyList_IfLatestEth1DataEqGenesisEth1Block(t *testing t.Fatalf("Unable to insert deposit into trie %v", err) } - d.DepositCache.InsertDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) + depositCache.InsertDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) } for _, dp := range recentDeposits { - d.DepositCache.InsertPendingDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) + depositCache.InsertPendingDeposit(ctx, dp.Deposit, big.NewInt(int64(dp.Index)), dp.Index, depositTrie.Root()) } bs := &ProposerServer{ beaconDB: d, powChainService: p, chainService: newMockChainService(), + depositCache: depositCache, } // It should also return the recent deposits after their follow window. diff --git a/beacon-chain/rpc/service.go b/beacon-chain/rpc/service.go index 032b0680f6..68bdf78378 100644 --- a/beacon-chain/rpc/service.go +++ b/beacon-chain/rpc/service.go @@ -77,7 +77,7 @@ type syncService interface { type Service struct { ctx context.Context cancel context.CancelFunc - beaconDB *db.BeaconDB + beaconDB db.Database chainService chainService powChainService powChainService operationService operationService @@ -98,7 +98,7 @@ type Config struct { Port string CertFlag string KeyFlag string - BeaconDB *db.BeaconDB + BeaconDB db.Database ChainService chainService POWChainService powChainService OperationService operationService diff --git a/beacon-chain/rpc/validator_server.go b/beacon-chain/rpc/validator_server.go index fa1ce3dd37..40d99bfe67 100644 --- a/beacon-chain/rpc/validator_server.go +++ b/beacon-chain/rpc/validator_server.go @@ -8,6 +8,7 @@ import ( "time" "github.com/pkg/errors" + "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" "github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils" @@ -25,10 +26,11 @@ import ( // and more. type ValidatorServer struct { ctx context.Context - beaconDB *db.BeaconDB + beaconDB db.Database chainService chainService canonicalStateChan chan *pbp2p.BeaconState powChainService powChainService + depositCache *depositcache.DepositCache } // WaitForActivation checks if a validator public key exists in the active validator registry of the current @@ -76,7 +78,7 @@ func (vs *ValidatorServer) WaitForActivation(req *pb.ValidatorActivationRequest, // ValidatorIndex is called by a validator to get its index location that corresponds // to the attestation bit fields. func (vs *ValidatorServer) ValidatorIndex(ctx context.Context, req *pb.ValidatorIndexRequest) (*pb.ValidatorIndexResponse, error) { - index, err := vs.beaconDB.ValidatorIndex(req.PublicKey) + index, err := vs.beaconDB.(*db.BeaconDB).ValidatorIndexDeprecated(req.PublicKey) if err != nil { return nil, errors.Wrap(err, "could not get validator index") } @@ -89,7 +91,7 @@ func (vs *ValidatorServer) ValidatorIndex(ctx context.Context, req *pb.Validator func (vs *ValidatorServer) ValidatorPerformance( ctx context.Context, req *pb.ValidatorPerformanceRequest, ) (*pb.ValidatorPerformanceResponse, error) { - index, err := vs.beaconDB.ValidatorIndex(req.PublicKey) + index, _, err := vs.beaconDB.ValidatorIndex(ctx, bytesutil.ToBytes48(req.PublicKey)) if err != nil { return nil, errors.Wrap(err, "could not get validator index") } @@ -97,11 +99,6 @@ func (vs *ValidatorServer) ValidatorPerformance( if err != nil { return nil, errors.Wrap(err, "could not get head") } - Validators, err := vs.beaconDB.Validators(ctx) - if err != nil { - return nil, errors.Wrap(err, "could not retrieve beacon state") - } - activeCount, err := helpers.ActiveValidatorCount(head, helpers.SlotToEpoch(req.Slot)) if err != nil { return nil, errors.Wrap(err, "could not retrieve active validator count") @@ -112,17 +109,12 @@ func (vs *ValidatorServer) ValidatorPerformance( return nil, errors.Wrap(err, "could not retrieve active balance") } - validatorBalances, err := vs.beaconDB.Balances(ctx) - if err != nil { - return nil, errors.Wrap(err, "could not retrieve validator balances") - } - avgBalance := float32(totalActiveBalance / activeCount) - balance := validatorBalances[index] + balance := head.Balances[index] return &pb.ValidatorPerformanceResponse{ Balance: balance, AverageActiveValidatorBalance: avgBalance, - TotalValidators: uint64(len(Validators)), + TotalValidators: uint64(len(head.Validators)), TotalActiveValidators: uint64(activeCount), }, nil } @@ -195,7 +187,7 @@ func (vs *ValidatorServer) assignment( ) } - idx, err := vs.beaconDB.ValidatorIndex(pubkey) + idx, _, err := vs.beaconDB.ValidatorIndex(context.TODO(), bytesutil.ToBytes48(pubkey)) if err != nil { return nil, errors.Wrap(err, "could not get active validator index") } @@ -307,7 +299,7 @@ func (vs *ValidatorServer) validatorStatus( beaconState *pbp2p.BeaconState) *pb.ValidatorStatusResponse { pk := bytesutil.ToBytes32(pubKey) valIdx, ok := idxMap[pk] - _, eth1BlockNumBigInt := vs.beaconDB.DepositCache.DepositByPubkey(ctx, pubKey) + _, eth1BlockNumBigInt := vs.depositCache.DepositByPubkey(ctx, pubKey) if eth1BlockNumBigInt == nil { return &pb.ValidatorStatusResponse{ Status: pb.ValidatorStatus_UNKNOWN_STATUS, diff --git a/beacon-chain/rpc/validator_server_test.go b/beacon-chain/rpc/validator_server_test.go index 81cf6b1403..44e25df27b 100644 --- a/beacon-chain/rpc/validator_server_test.go +++ b/beacon-chain/rpc/validator_server_test.go @@ -16,6 +16,7 @@ import ( "github.com/gogo/protobuf/proto" "github.com/golang/mock/gomock" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" blk "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" @@ -33,9 +34,12 @@ import ( func TestValidatorIndex_OK(t *testing.T) { db := internal.SetupDBDeprecated(t) defer internal.TeardownDBDeprecated(t, db) + if err := db.SaveStateDeprecated(context.Background(), &pbp2p.BeaconState{}); err != nil { + t.Fatal(err) + } pubKey := []byte{'A'} - if err := db.SaveValidatorIndex(pubKey, 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey, 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } @@ -52,8 +56,8 @@ func TestValidatorIndex_OK(t *testing.T) { } func TestValidatorIndex_InStateNotInDB(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + d := internal.SetupDBDeprecated(t) + defer internal.TeardownDBDeprecated(t, d) pubKey := []byte{'A'} @@ -62,12 +66,12 @@ func TestValidatorIndex_InStateNotInDB(t *testing.T) { Validators: []*ethpb.Validator{{PublicKey: []byte{0}}, {PublicKey: []byte{'A'}}, {PublicKey: []byte{'B'}}}, } - if err := db.SaveState(context.Background(), s); err != nil { + if err := d.SaveStateDeprecated(context.Background(), s); err != nil { t.Fatal(err) } validatorServer := &ValidatorServer{ - beaconDB: db, + beaconDB: d, } req := &pb.ValidatorIndexRequest{ @@ -84,7 +88,7 @@ func TestValidatorIndex_InStateNotInDB(t *testing.T) { } // Verify index is also saved in DB. - idx, err := validatorServer.beaconDB.ValidatorIndex(pubKey) + idx, err := validatorServer.beaconDB.(*db.BeaconDB).ValidatorIndexDeprecated(pubKey) if err != nil { t.Fatal(err) } @@ -104,11 +108,11 @@ func TestNextEpochCommitteeAssignment_WrongPubkeyLength(t *testing.T) { if err != nil { t.Fatal(err) } - if err := db.SaveState(context.Background(), beaconState); err != nil { + if err := db.SaveStateDeprecated(context.Background(), beaconState); err != nil { t.Fatal(err) } block := blk.NewGenesisBlock([]byte{}) - if err := db.SaveBlock(block); err != nil { + if err := db.SaveBlockDeprecated(block); err != nil { t.Fatalf("Could not save block: %v", err) } if err := db.UpdateChainHead(ctx, block, beaconState); err != nil { @@ -132,7 +136,7 @@ func TestNextEpochCommitteeAssignment_CantFindValidatorIdx(t *testing.T) { defer internal.TeardownDBDeprecated(t, db) ctx := context.Background() genesis := blk.NewGenesisBlock([]byte{}) - if err := db.SaveBlock(genesis); err != nil { + if err := db.SaveBlockDeprecated(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } deposits, _ := testutil.SetupInitialDeposits(t, params.BeaconConfig().MinGenesisActiveValidatorCount) @@ -164,7 +168,7 @@ func TestCommitteeAssignment_OK(t *testing.T) { ctx := context.Background() genesis := blk.NewGenesisBlock([]byte{}) - if err := db.SaveBlock(genesis); err != nil { + if err := db.SaveBlockDeprecated(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } depChainStart := params.BeaconConfig().MinGenesisActiveValidatorCount / 16 @@ -245,7 +249,7 @@ func TestCommitteeAssignment_CurrentEpoch_ShouldNotFail(t *testing.T) { ctx := context.Background() genesis := blk.NewGenesisBlock([]byte{}) - if err := db.SaveBlock(genesis); err != nil { + if err := db.SaveBlockDeprecated(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } depChainStart := params.BeaconConfig().MinGenesisActiveValidatorCount / 16 @@ -301,7 +305,7 @@ func TestCommitteeAssignment_multipleKeys_OK(t *testing.T) { ctx := context.Background() genesis := blk.NewGenesisBlock([]byte{}) - if err := db.SaveBlock(genesis); err != nil { + if err := db.SaveBlockDeprecated(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } depChainStart := params.BeaconConfig().MinGenesisActiveValidatorCount / 16 @@ -359,12 +363,12 @@ func TestValidatorStatus_PendingActive(t *testing.T) { ctx := context.Background() pubKey := []byte{'A'} - if err := db.SaveValidatorIndex(pubKey, 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey, 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } // Pending active because activation epoch is still defaulted at far future slot. - if err := db.SaveState(ctx, &pbp2p.BeaconState{Validators: []*ethpb.Validator{ + if err := db.SaveStateDeprecated(ctx, &pbp2p.BeaconState{Validators: []*ethpb.Validator{ {ActivationEpoch: params.BeaconConfig().FarFutureEpoch, PublicKey: pubKey}, }, Slot: 5000, @@ -384,7 +388,8 @@ func TestValidatorStatus_PendingActive(t *testing.T) { if err != nil { t.Fatal(fmt.Errorf("could not setup deposit trie: %v", err)) } - db.DepositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) + depositCache := depositcache.NewDepositCache() + depositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) height := time.Unix(int64(params.BeaconConfig().Eth1FollowDistance), 0).Unix() vs := &ValidatorServer{ @@ -394,6 +399,7 @@ func TestValidatorStatus_PendingActive(t *testing.T) { 0: uint64(height), }, }, + depositCache: depositCache, } req := &pb.ValidatorIndexRequest{ PublicKey: pubKey, @@ -416,7 +422,7 @@ func TestValidatorStatus_Active(t *testing.T) { ctx := context.Background() pubKey := []byte{'A'} - if err := db.SaveValidatorIndex(pubKey, 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey, 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } @@ -433,11 +439,12 @@ func TestValidatorStatus_Active(t *testing.T) { if err != nil { t.Fatal(fmt.Errorf("could not setup deposit trie: %v", err)) } - db.DepositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) + depositCache := depositcache.NewDepositCache() + depositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) // Active because activation epoch <= current epoch < exit epoch. activeEpoch := helpers.DelayedActivationExitEpoch(0) - if err := db.SaveState(ctx, &pbp2p.BeaconState{ + if err := db.SaveStateDeprecated(ctx, &pbp2p.BeaconState{ GenesisTime: uint64(time.Unix(0, 0).Unix()), Slot: 10000, Validators: []*ethpb.Validator{{ @@ -456,6 +463,7 @@ func TestValidatorStatus_Active(t *testing.T) { int(params.BeaconConfig().Eth1FollowDistance): uint64(timestamp), }, }, + depositCache: depositCache, } req := &pb.ValidatorIndexRequest{ PublicKey: pubKey, @@ -481,7 +489,7 @@ func TestValidatorStatus_InitiatedExit(t *testing.T) { ctx := context.Background() pubKey := []byte{'A'} - if err := db.SaveValidatorIndex(pubKey, 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey, 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } @@ -490,7 +498,7 @@ func TestValidatorStatus_InitiatedExit(t *testing.T) { epoch := helpers.SlotToEpoch(slot) exitEpoch := helpers.DelayedActivationExitEpoch(epoch) withdrawableEpoch := exitEpoch + params.BeaconConfig().MinValidatorWithdrawabilityDelay - if err := db.SaveState(ctx, &pbp2p.BeaconState{ + if err := db.SaveStateDeprecated(ctx, &pbp2p.BeaconState{ Slot: slot, Validators: []*ethpb.Validator{{ PublicKey: pubKey, @@ -513,7 +521,8 @@ func TestValidatorStatus_InitiatedExit(t *testing.T) { if err != nil { t.Fatal(fmt.Errorf("could not setup deposit trie: %v", err)) } - db.DepositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) + depositCache := depositcache.NewDepositCache() + depositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) height := time.Unix(int64(params.BeaconConfig().Eth1FollowDistance), 0).Unix() vs := &ValidatorServer{ beaconDB: db, @@ -522,6 +531,7 @@ func TestValidatorStatus_InitiatedExit(t *testing.T) { 0: uint64(height), }, }, + depositCache: depositCache, } req := &pb.ValidatorIndexRequest{ PublicKey: pubKey, @@ -541,14 +551,14 @@ func TestValidatorStatus_Withdrawable(t *testing.T) { ctx := context.Background() pubKey := []byte{'A'} - if err := db.SaveValidatorIndex(pubKey, 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey, 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } // Withdrawable exit because current epoch is after validator withdrawable epoch. slot := uint64(10000) epoch := helpers.SlotToEpoch(slot) - if err := db.SaveState(ctx, &pbp2p.BeaconState{ + if err := db.SaveStateDeprecated(ctx, &pbp2p.BeaconState{ Slot: 10000, Validators: []*ethpb.Validator{{ WithdrawableEpoch: epoch - 1, @@ -570,7 +580,8 @@ func TestValidatorStatus_Withdrawable(t *testing.T) { if err != nil { t.Fatal(fmt.Errorf("could not setup deposit trie: %v", err)) } - db.DepositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) + depositCache := depositcache.NewDepositCache() + depositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) height := time.Unix(int64(params.BeaconConfig().Eth1FollowDistance), 0).Unix() vs := &ValidatorServer{ beaconDB: db, @@ -579,6 +590,7 @@ func TestValidatorStatus_Withdrawable(t *testing.T) { 0: uint64(height), }, }, + depositCache: depositCache, } req := &pb.ValidatorIndexRequest{ PublicKey: pubKey, @@ -598,14 +610,14 @@ func TestValidatorStatus_ExitedSlashed(t *testing.T) { ctx := context.Background() pubKey := []byte{'A'} - if err := db.SaveValidatorIndex(pubKey, 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey, 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } // Exit slashed because slashed is true, exit epoch is =< current epoch and withdrawable epoch > epoch . slot := uint64(10000) epoch := helpers.SlotToEpoch(slot) - if err := db.SaveState(ctx, &pbp2p.BeaconState{ + if err := db.SaveStateDeprecated(ctx, &pbp2p.BeaconState{ Slot: slot, Validators: []*ethpb.Validator{{ Slashed: true, @@ -627,7 +639,8 @@ func TestValidatorStatus_ExitedSlashed(t *testing.T) { if err != nil { t.Fatal(fmt.Errorf("could not setup deposit trie: %v", err)) } - db.DepositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) + depositCache := depositcache.NewDepositCache() + depositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) height := time.Unix(int64(params.BeaconConfig().Eth1FollowDistance), 0).Unix() vs := &ValidatorServer{ beaconDB: db, @@ -636,6 +649,7 @@ func TestValidatorStatus_ExitedSlashed(t *testing.T) { 0: uint64(height), }, }, + depositCache: depositCache, } req := &pb.ValidatorIndexRequest{ PublicKey: pubKey, @@ -655,14 +669,14 @@ func TestValidatorStatus_Exited(t *testing.T) { ctx := context.Background() pubKey := []byte{'A'} - if err := db.SaveValidatorIndex(pubKey, 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey, 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } // Exit because only exit epoch is =< current epoch. slot := uint64(10000) epoch := helpers.SlotToEpoch(slot) - if err := db.SaveState(ctx, &pbp2p.BeaconState{ + if err := db.SaveStateDeprecated(ctx, &pbp2p.BeaconState{ Slot: slot, Validators: []*ethpb.Validator{{ PublicKey: pubKey, @@ -683,7 +697,8 @@ func TestValidatorStatus_Exited(t *testing.T) { if err != nil { t.Fatal(fmt.Errorf("could not setup deposit trie: %v", err)) } - db.DepositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) + depositCache := depositcache.NewDepositCache() + depositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) height := time.Unix(int64(params.BeaconConfig().Eth1FollowDistance), 0).Unix() vs := &ValidatorServer{ beaconDB: db, @@ -692,6 +707,7 @@ func TestValidatorStatus_Exited(t *testing.T) { 0: uint64(height), }, }, + depositCache: depositCache, } req := &pb.ValidatorIndexRequest{ PublicKey: pubKey, @@ -711,11 +727,11 @@ func TestValidatorStatus_UnknownStatus(t *testing.T) { ctx := context.Background() pubKey := []byte{'A'} - if err := db.SaveValidatorIndex(pubKey, 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey, 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } - if err := db.SaveState(ctx, &pbp2p.BeaconState{ + if err := db.SaveStateDeprecated(ctx, &pbp2p.BeaconState{ Slot: 0, Validators: []*ethpb.Validator{{ ActivationEpoch: 0, @@ -737,7 +753,8 @@ func TestValidatorStatus_UnknownStatus(t *testing.T) { if err != nil { t.Fatal(fmt.Errorf("could not setup deposit trie: %v", err)) } - db.DepositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) + depositCache := depositcache.NewDepositCache() + depositCache.InsertDeposit(ctx, deposit, big.NewInt(0) /*blockNum*/, 0, depositTrie.Root()) height := time.Unix(int64(params.BeaconConfig().Eth1FollowDistance), 0).Unix() vs := &ValidatorServer{ beaconDB: db, @@ -746,6 +763,7 @@ func TestValidatorStatus_UnknownStatus(t *testing.T) { 0: uint64(height), }, }, + depositCache: depositCache, } req := &pb.ValidatorIndexRequest{ PublicKey: pubKey, @@ -768,7 +786,7 @@ func TestWaitForActivation_ContextClosed(t *testing.T) { Slot: 0, Validators: []*ethpb.Validator{}, } - if err := db.SaveState(ctx, beaconState); err != nil { + if err := db.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatalf("could not save state: %v", err) } @@ -779,6 +797,7 @@ func TestWaitForActivation_ContextClosed(t *testing.T) { chainService: newMockChainService(), powChainService: &mockPOWChainService{}, canonicalStateChan: make(chan *pbp2p.BeaconState, 1), + depositCache: depositcache.NewDepositCache(), } req := &pb.ValidatorActivationRequest{ PublicKeys: [][]byte{[]byte("A")}, @@ -821,10 +840,10 @@ func TestWaitForActivation_ValidatorOriginallyExists(t *testing.T) { pubKey1 := priv1.PublicKey().Marshal()[:] pubKey2 := priv2.PublicKey().Marshal()[:] - if err := db.SaveValidatorIndex(pubKey1, 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey1, 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } - if err := db.SaveValidatorIndex(pubKey2, 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey2, 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } @@ -843,7 +862,7 @@ func TestWaitForActivation_ValidatorOriginallyExists(t *testing.T) { }, }, } - if err := db.SaveState(ctx, beaconState); err != nil { + if err := db.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatalf("could not save state: %v", err) } depData := ðpb.Deposit_Data{ @@ -864,11 +883,12 @@ func TestWaitForActivation_ValidatorOriginallyExists(t *testing.T) { if err != nil { t.Fatal(fmt.Errorf("could not setup deposit trie: %v", err)) } - db.DepositCache.InsertDeposit(ctx, deposit, big.NewInt(10) /*blockNum*/, 0, depositTrie.Root()) - if err := db.SaveValidatorIndex(pubKey1, 0); err != nil { + depositCache := depositcache.NewDepositCache() + depositCache.InsertDeposit(ctx, deposit, big.NewInt(10) /*blockNum*/, 0, depositTrie.Root()) + if err := db.SaveValidatorIndexDeprecated(pubKey1, 0); err != nil { t.Fatalf("could not save validator index: %v", err) } - if err := db.SaveValidatorIndex(pubKey2, 1); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKey2, 1); err != nil { t.Fatalf("could not save validator index: %v", err) } vs := &ValidatorServer{ @@ -877,6 +897,7 @@ func TestWaitForActivation_ValidatorOriginallyExists(t *testing.T) { chainService: newMockChainService(), canonicalStateChan: make(chan *pbp2p.BeaconState, 1), powChainService: &mockPOWChainService{}, + depositCache: depositCache, } req := &pb.ValidatorActivationRequest{ PublicKeys: [][]byte{pubKey1, pubKey2}, @@ -917,10 +938,10 @@ func TestMultipleValidatorStatus_OK(t *testing.T) { ctx := context.Background() pubKeys := [][]byte{{'A'}, {'B'}, {'C'}} - if err := db.SaveValidatorIndex(pubKeys[0], 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKeys[0], 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } - if err := db.SaveValidatorIndex(pubKeys[1], 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKeys[1], 0); err != nil { t.Fatalf("Could not save validator index: %v", err) } @@ -940,7 +961,7 @@ func TestMultipleValidatorStatus_OK(t *testing.T) { PublicKey: pubKeys[2]}, }, } - if err := db.SaveState(ctx, beaconState); err != nil { + if err := db.SaveStateDeprecated(ctx, beaconState); err != nil { t.Fatalf("could not save state: %v", err) } depData := ðpb.Deposit_Data{ @@ -957,7 +978,8 @@ func TestMultipleValidatorStatus_OK(t *testing.T) { if err != nil { t.Fatal(fmt.Errorf("could not setup deposit trie: %v", err)) } - db.DepositCache.InsertDeposit(ctx, dep, big.NewInt(10) /*blockNum*/, 0, depositTrie.Root()) + depositCache := depositcache.NewDepositCache() + depositCache.InsertDeposit(ctx, dep, big.NewInt(10) /*blockNum*/, 0, depositTrie.Root()) depData = ðpb.Deposit_Data{ PublicKey: []byte{'C'}, Signature: []byte("hi"), @@ -969,15 +991,15 @@ func TestMultipleValidatorStatus_OK(t *testing.T) { Data: depData, } depositTrie.InsertIntoTrie(dep.Data.Signature, 15) - db.DepositCache.InsertDeposit(context.Background(), dep, big.NewInt(15), 0, depositTrie.Root()) + depositCache.InsertDeposit(context.Background(), dep, big.NewInt(15), 0, depositTrie.Root()) - if err := db.SaveValidatorIndex(pubKeys[0], 0); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKeys[0], 0); err != nil { t.Fatalf("could not save validator index: %v", err) } - if err := db.SaveValidatorIndex(pubKeys[1], 1); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKeys[1], 1); err != nil { t.Fatalf("could not save validator index: %v", err) } - if err := db.SaveValidatorIndex(pubKeys[2], 2); err != nil { + if err := db.SaveValidatorIndexDeprecated(pubKeys[2], 2); err != nil { t.Fatalf("could not save validator index: %v", err) } vs := &ValidatorServer{ @@ -986,6 +1008,7 @@ func TestMultipleValidatorStatus_OK(t *testing.T) { chainService: newMockChainService(), canonicalStateChan: make(chan *pbp2p.BeaconState, 1), powChainService: &mockPOWChainService{}, + depositCache: depositCache, } activeExists, response, err := vs.MultipleValidatorStatus(context.Background(), pubKeys) if err != nil { @@ -1016,10 +1039,10 @@ func BenchmarkAssignment(b *testing.B) { path := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath)) db, _ := db.NewDBDeprecated(path) defer db.Close() - os.RemoveAll(db.DatabasePath) + os.RemoveAll(db.DatabasePath()) genesis := blk.NewGenesisBlock([]byte{}) - if err := db.SaveBlock(genesis); err != nil { + if err := db.SaveBlockDeprecated(genesis); err != nil { b.Fatalf("Could not save genesis block: %v", err) } validatorCount := params.BeaconConfig().MinGenesisActiveValidatorCount * 4 diff --git a/beacon-chain/sync/service.go b/beacon-chain/sync/service.go index e78410a6ce..2145b73ce1 100644 --- a/beacon-chain/sync/service.go +++ b/beacon-chain/sync/service.go @@ -11,13 +11,30 @@ import ( var _ = shared.Service(&RegularSync{}) +// Config to set up the regular sync service. +type Config struct { + P2P p2p.P2P + DB db.Database + Operations *operations.Service +} + +// NewRegularSync service. +func NewRegularSync(cfg *Config) *RegularSync { + return &RegularSync{ + ctx: context.Background(), + db: cfg.DB, + p2p: cfg.P2P, + operations: cfg.Operations, + } +} + // RegularSync service is responsible for handling all run time p2p related operations as the // main entry point for network messages. type RegularSync struct { ctx context.Context p2p p2p.P2P db db.Database - operations operations.Service + operations *operations.Service } // Start the regular sync service by initializing all of the p2p sync handlers. diff --git a/shared/featureconfig/config.go b/shared/featureconfig/config.go index 10202d99c3..360c88e014 100644 --- a/shared/featureconfig/config.go +++ b/shared/featureconfig/config.go @@ -30,6 +30,8 @@ type FeatureFlagConfig struct { EnableExcessDeposits bool // EnableExcessDeposits in validator balances. NoGenesisDelay bool // NoGenesisDelay when processing a chain start genesis event. UseNewP2P bool // UseNewP2P service. + UseNewSync bool // UseNewSync services. + UseNewDatabase bool // UseNewDatabase service. // Cache toggles. EnableActiveBalanceCache bool // EnableActiveBalanceCache; see https://github.com/prysmaticlabs/prysm/issues/3106. @@ -72,10 +74,18 @@ func ConfigureBeaconFeatures(ctx *cli.Context) { log.Warn("Using non standard genesis delay. This may cause problems in a multi-node environment.") cfg.NoGenesisDelay = true } - if ctx.GlobalBool(UseNewP2PFlag.Name) { + if ctx.GlobalBool(NextFlag.Name) || ctx.GlobalBool(UseNewP2PFlag.Name) { log.Warn("Using new P2P service.") cfg.UseNewP2P = true } + if ctx.GlobalBool(NextFlag.Name) || ctx.GlobalBool(UseNewSyncFlag.Name) { + log.Warn("Using new sync services.") + cfg.UseNewSync = true + } + if ctx.GlobalBool(NextFlag.Name) || ctx.GlobalBool(UseNewDatabaseFlag.Name) { + log.Warn("Using new database service.") + cfg.UseNewDatabase = true + } if ctx.GlobalBool(EnableActiveBalanceCacheFlag.Name) { log.Warn("Enabled unsafe active balance cache") cfg.EnableActiveBalanceCache = true diff --git a/shared/featureconfig/flags.go b/shared/featureconfig/flags.go index 28327b82a0..41fc59f87b 100644 --- a/shared/featureconfig/flags.go +++ b/shared/featureconfig/flags.go @@ -36,6 +36,21 @@ var ( Name: "experimental-p2p", Usage: "Use the new experimental p2p library. See issue #3147.", } + // UseNewSyncFlag to start the beacon chain using the new sync library. + UseNewSyncFlag = cli.BoolFlag{ + Name: "experimental-sync", + Usage: "Use the new experimental sync libraries. See issue #3147.", + } + // UseNewDatabaseFlag to start the beacon chain using new database library. + UseNewDatabaseFlag = cli.BoolFlag{ + Name: "experimental-db", + Usage: "Use the new experimental database library.", + } + // NextFlag to enable all experimental features. + NextFlag = cli.BoolFlag{ + Name: "next", + Usage: "Use next version experimental features.", + } // EnableActiveBalanceCacheFlag see https://github.com/prysmaticlabs/prysm/issues/3106. EnableActiveBalanceCacheFlag = cli.BoolFlag{ Name: "enable-active-balance-cache", @@ -84,6 +99,9 @@ var BeaconChainFlags = []cli.Flag{ EnableExcessDepositsFlag, NoGenesisDelayFlag, UseNewP2PFlag, + UseNewSyncFlag, + UseNewDatabaseFlag, + NextFlag, EnableActiveBalanceCacheFlag, EnableAttestationCacheFlag, EnableAncestorBlockCacheFlag,