From 8f01b76366d0bfb17497f747fef47b96cb4bdbf4 Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Thu, 22 Aug 2019 15:28:53 -0500 Subject: [PATCH] Integrate DB Refactor Into Ethereum APIs Beacon Chain Server (#3245) * deprecate db * fix build * begin integrating new db * gaz * use more of the new db * newest implementation uses head state * remove more deprecated items * setup validators in state helper * fix up some tests with the new db * resolve broken build * gaz * begin ensuring tests pass * optional idx * list validator balances passing * default page size passing * only two failing * fixed most tests, found edge case * allow nil return and add proper tests * pass tests * fix head block root problem * working with the new db * every ethereumapis method now compliant with both dbs * pass in db into server --- beacon-chain/db/kv/attestations.go | 6 +- beacon-chain/db/kv/blocks.go | 2 +- beacon-chain/rpc/BUILD.bazel | 2 + beacon-chain/rpc/beacon_chain_server.go | 76 +++-- beacon-chain/rpc/beacon_chain_server_test.go | 311 +++++++++---------- 5 files changed, 209 insertions(+), 188 deletions(-) diff --git a/beacon-chain/db/kv/attestations.go b/beacon-chain/db/kv/attestations.go index 9c3158d135..9d2ae46f9d 100644 --- a/beacon-chain/db/kv/attestations.go +++ b/beacon-chain/db/kv/attestations.go @@ -175,16 +175,18 @@ func createAttestationIndicesFromData(attData *ethpb.AttestationData, tx *bolt.T indicesByBucket := make(map[string][]byte) buckets := [][]byte{ attestationShardIndicesBucket, - attestationParentRootIndicesBucket, attestationStartEpochIndicesBucket, attestationEndEpochIndicesBucket, } indices := [][]byte{ uint64ToBytes(attData.Crosslink.Shard), - attData.Crosslink.ParentRoot, uint64ToBytes(attData.Crosslink.StartEpoch), uint64ToBytes(attData.Crosslink.EndEpoch), } + if attData.Crosslink.ParentRoot != nil && len(attData.Crosslink.ParentRoot) > 0 { + buckets = append(buckets, attestationParentRootIndicesBucket) + indices = append(indices, attData.Crosslink.ParentRoot) + } for i := 0; i < len(buckets); i++ { indicesByBucket[string(buckets[i])] = indices[i] } diff --git a/beacon-chain/db/kv/blocks.go b/beacon-chain/db/kv/blocks.go index 599a3923c5..cc8febc8db 100644 --- a/beacon-chain/db/kv/blocks.go +++ b/beacon-chain/db/kv/blocks.go @@ -43,7 +43,7 @@ func (k *Store) HeadBlock(ctx context.Context) (*ethpb.BeaconBlock, error) { defer span.End() var headBlock *ethpb.BeaconBlock err := k.db.View(func(tx *bolt.Tx) error { - bkt := tx.Bucket(validatorsBucket) + bkt := tx.Bucket(blocksBucket) headRoot := bkt.Get(headBlockRootKey) if headRoot == nil { return nil diff --git a/beacon-chain/rpc/BUILD.bazel b/beacon-chain/rpc/BUILD.bazel index bc226be273..c3d5d00c10 100644 --- a/beacon-chain/rpc/BUILD.bazel +++ b/beacon-chain/rpc/BUILD.bazel @@ -23,6 +23,7 @@ go_library( "//beacon-chain/core/state/stateutils:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/filters:go_default_library", + "//beacon-chain/db/kv:go_default_library", "//beacon-chain/deprecated-blockchain:go_default_library", "//beacon-chain/operations:go_default_library", "//beacon-chain/p2p:go_default_library", @@ -75,6 +76,7 @@ go_test( "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/state:go_default_library", "//beacon-chain/db:go_default_library", + "//beacon-chain/db/testing:go_default_library", "//beacon-chain/internal:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//proto/beacon/rpc/v1:go_default_library", diff --git a/beacon-chain/rpc/beacon_chain_server.go b/beacon-chain/rpc/beacon_chain_server.go index 5ac12f25ab..84c1517af0 100644 --- a/beacon-chain/rpc/beacon_chain_server.go +++ b/beacon-chain/rpc/beacon_chain_server.go @@ -9,6 +9,7 @@ import ( "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/db/kv" "github.com/prysmaticlabs/prysm/beacon-chain/operations" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bytesutil" @@ -52,7 +53,6 @@ func (bs *BeaconChainServer) ListAttestations( return nil, status.Errorf(codes.InvalidArgument, "requested page size %d can not be greater than max size %d", req.PageSize, params.BeaconConfig().MaxPageSize) } - switch req.QueryFilter.(type) { case *ethpb.ListAttestationsRequest_BlockRoot: return nil, status.Error(codes.Unimplemented, "not implemented") @@ -92,9 +92,21 @@ func (bs *BeaconChainServer) ListAttestations( func (bs *BeaconChainServer) AttestationPool( ctx context.Context, _ *ptypes.Empty, ) (*ethpb.AttestationPoolResponse, error) { - headBlock, err := bs.beaconDB.(*db.BeaconDB).ChainHead() - if err != nil { - return nil, err + var headBlock *ethpb.BeaconBlock + var err error + if d, isLegacyDB := bs.beaconDB.(*db.BeaconDB); isLegacyDB { + headBlock, err = d.ChainHead() + if err != nil { + return nil, err + } + } else { + headBlock, err = bs.beaconDB.(*kv.Store).HeadBlock(ctx) + if err != nil { + return nil, err + } + } + if headBlock == nil { + return nil, status.Error(codes.Internal, "no head block found in db") } atts, err := bs.pool.AttestationPool(ctx, headBlock.Slot) if err != nil { @@ -121,8 +133,8 @@ func (bs *BeaconChainServer) ListBlocks( switch q := req.QueryFilter.(type) { case *ethpb.ListBlocksRequest_Epoch: - startSlot := helpers.StartSlot(q.Epoch) - endSlot := startSlot + params.BeaconConfig().SlotsPerEpoch + startSlot := q.Epoch * params.BeaconConfig().SlotsPerEpoch + endSlot := startSlot + params.BeaconConfig().SlotsPerEpoch - 1 var blks []*ethpb.BeaconBlock if d, isLegacyDB := bs.beaconDB.(*db.BeaconDB); isLegacyDB { @@ -142,7 +154,7 @@ func (bs *BeaconChainServer) ListBlocks( } numBlks := len(blks) if numBlks == 0 { - return ðpb.ListBlocksResponse{Blocks: []*ethpb.BeaconBlock{}, TotalSize: 0}, nil + return ðpb.ListBlocksResponse{Blocks: make([]*ethpb.BeaconBlock, 0), TotalSize: 0}, nil } start, end, nextPageToken, err := pagination.StartAndEndPage(req.PageToken, int(req.PageSize), numBlks) @@ -226,14 +238,12 @@ 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.(*db.BeaconDB).Balances(ctx) + headState, err := bs.beaconDB.HeadState(ctx) if err != nil { - return nil, status.Errorf(codes.Internal, "could not retrieve validator balances: %v", err) - } - validators, err := bs.beaconDB.(*db.BeaconDB).Validators(ctx) - if err != nil { - return nil, status.Errorf(codes.Internal, "could not retrieve validators: %v", err) + return nil, status.Errorf(codes.Internal, "could not retrieve head state: %v", err) } + balances := headState.Balances + validators := headState.Validators for _, pubKey := range req.PublicKeys { // Skip empty public key @@ -241,10 +251,23 @@ func (bs *BeaconChainServer) ListValidatorBalances( continue } - index, err := bs.beaconDB.(*db.BeaconDB).ValidatorIndexDeprecated(pubKey) - if err != nil { - return nil, status.Errorf(codes.Internal, "could not retrieve validator index: %v", err) + var index uint64 + var ok bool + if d, isLegacyDB := bs.beaconDB.(*db.BeaconDB); isLegacyDB { + index, err = d.ValidatorIndexDeprecated(pubKey) + if err != nil { + return nil, status.Errorf(codes.Internal, "could not retrieve validator index: %v", err) + } + } else { + index, ok, err = bs.beaconDB.ValidatorIndex(ctx, bytesutil.ToBytes48(pubKey)) + if err != nil { + return nil, status.Errorf(codes.Internal, "could not retrieve validator index: %v", err) + } + if !ok { + return nil, status.Errorf(codes.Internal, "could validator index for public key %#x not found", pubKey) + } } + filtered[index] = true if int(index) >= len(balances) { @@ -295,7 +318,6 @@ func (bs *BeaconChainServer) GetValidators( } validatorCount := len(head.Validators) - start, end, nextPageToken, err := pagination.StartAndEndPage(req.PageToken, int(req.PageSize), validatorCount) if err != nil { return nil, err @@ -350,10 +372,23 @@ func (bs *BeaconChainServer) ListValidatorAssignments( // Filter out assignments by public keys. for _, pubKey := range req.PublicKeys { - index, err := bs.beaconDB.(*db.BeaconDB).ValidatorIndexDeprecated(pubKey) - if err != nil { - return nil, status.Errorf(codes.Internal, "could not retrieve validator index: %v", err) + var index uint64 + var ok bool + if d, isLegacyDB := bs.beaconDB.(*db.BeaconDB); isLegacyDB { + index, err = d.ValidatorIndexDeprecated(pubKey) + if err != nil { + return nil, status.Errorf(codes.Internal, "could not retrieve validator index: %v", err) + } + } else { + index, ok, err = bs.beaconDB.ValidatorIndex(ctx, bytesutil.ToBytes48(pubKey)) + if err != nil { + return nil, status.Errorf(codes.Internal, "could not retrieve validator index: %v", err) + } + if !ok { + return nil, status.Errorf(codes.Internal, "could validator index for public key %#x not found", pubKey) + } } + filtered[index] = true if int(index) >= len(s.Validators) { @@ -455,7 +490,6 @@ func (bs *BeaconChainServer) ListValidatorAssignments( func (bs *BeaconChainServer) GetValidatorParticipation( ctx context.Context, req *ethpb.GetValidatorParticipationRequest, ) (*ethpb.ValidatorParticipation, error) { - s, err := bs.beaconDB.HeadState(ctx) if err != nil { return nil, status.Errorf(codes.Internal, "could not retrieve current state: %v", err) diff --git a/beacon-chain/rpc/beacon_chain_server_test.go b/beacon-chain/rpc/beacon_chain_server_test.go index 33876ee7cd..0e91a03705 100644 --- a/beacon-chain/rpc/beacon_chain_server_test.go +++ b/beacon-chain/rpc/beacon_chain_server_test.go @@ -13,8 +13,8 @@ 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" + "github.com/prysmaticlabs/prysm/beacon-chain/db" + testutil "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/params" @@ -38,8 +38,8 @@ func (m *mockPool) AttestationPool(ctx context.Context, expectedSlot uint64) ([] } func TestBeaconChainServer_ListAttestationsNoPagination(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) ctx := context.Background() count := uint64(10) @@ -73,8 +73,8 @@ func TestBeaconChainServer_ListAttestationsNoPagination(t *testing.T) { } func TestBeaconChainServer_ListAttestationsPagination(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) ctx := context.Background() count := uint64(100) @@ -171,8 +171,8 @@ func TestBeaconChainServer_ListAttestationsPagination(t *testing.T) { } func TestBeaconChainServer_ListAttestationsPaginationOutOfRange(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) ctx := context.Background() count := uint64(1) @@ -215,8 +215,8 @@ func TestBeaconChainServer_ListAttestationsExceedsMaxPageSize(t *testing.T) { } func TestBeaconChainServer_ListAttestationsDefaultPageSize(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) ctx := context.Background() count := uint64(params.BeaconConfig().DefaultPageSize) @@ -254,16 +254,26 @@ func TestBeaconChainServer_ListAttestationsDefaultPageSize(t *testing.T) { func TestBeaconChainServer_AttestationPool(t *testing.T) { ctx := context.Background() - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) bs := &BeaconChainServer{ pool: &mockPool{}, beaconDB: db, } - if err := bs.beaconDB.SaveBlock(ctx, ðpb.BeaconBlock{Slot: 10}); err != nil { + block := ðpb.BeaconBlock{ + Slot: 10, + } + blockRoot, err := ssz.SigningRoot(block) + if err != nil { t.Fatal(err) } - if err := bs.beaconDB.(*db2.BeaconDB).UpdateChainHead(ctx, ðpb.BeaconBlock{Slot: 10}, &pbp2p.BeaconState{Slot: 10}); err != nil { + if err := bs.beaconDB.SaveBlock(ctx, block); err != nil { + t.Fatal(err) + } + if err := bs.beaconDB.SaveHeadBlockRoot(ctx, blockRoot); err != nil { + t.Fatal(err) + } + if err := bs.beaconDB.SaveState(ctx, &pbp2p.BeaconState{Slot: 10}, blockRoot); err != nil { t.Fatal(err) } res, err := bs.AttestationPool(ctx, &ptypes.Empty{}) @@ -277,25 +287,10 @@ func TestBeaconChainServer_AttestationPool(t *testing.T) { } func TestBeaconChainServer_ListValidatorBalances(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) - count := 100 - balances := make([]uint64, count) - validators := make([]*ethpb.Validator, 0, count) - for i := 0; i < count; i++ { - 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.SaveStateDeprecated( - context.Background(), - &pbp2p.BeaconState{Validators: validators, Balances: balances}); err != nil { - t.Fatal(err) - } + setupValidators(t, db, 100) bs := &BeaconChainServer{ beaconDB: db, @@ -346,56 +341,26 @@ func TestBeaconChainServer_ListValidatorBalances(t *testing.T) { } func TestBeaconChainServer_ListValidatorBalancesOutOfRange(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) - - count := 1 - balances := make([]uint64, count) - validators := make([]*ethpb.Validator, 0, count) - for i := 0; i < count; i++ { - 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.SaveStateDeprecated( - context.Background(), - &pbp2p.BeaconState{Validators: validators, Balances: balances}); err != nil { - t.Fatal(err) - } + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) + _, balances := setupValidators(t, db, 1) bs := &BeaconChainServer{ beaconDB: db, } - req := ðpb.GetValidatorBalancesRequest{Indices: []uint64{uint64(count)}} - wanted := fmt.Sprintf("validator index %d >= balance list %d", count, len(balances)) + req := ðpb.GetValidatorBalancesRequest{Indices: []uint64{uint64(1)}} + wanted := fmt.Sprintf("validator index %d >= balance list %d", 1, len(balances)) if _, err := bs.ListValidatorBalances(context.Background(), req); !strings.Contains(err.Error(), wanted) { t.Errorf("Expected error %v, received %v", wanted, err) } } func TestBeaconChainServer_GetValidatorsNoPagination(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) - - count := 100 - validators := make([]*ethpb.Validator, 0, count) - for i := 0; i < count; i++ { - 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.SaveStateDeprecated( - context.Background(), - &pbp2p.BeaconState{Validators: validators}); err != nil { - t.Fatal(err) - } + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) + validators, _ := setupValidators(t, db, 100) bs := &BeaconChainServer{ beaconDB: db, } @@ -411,25 +376,11 @@ func TestBeaconChainServer_GetValidatorsNoPagination(t *testing.T) { } func TestBeaconChainServer_GetValidatorsPagination(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) count := 100 - balances := make([]uint64, count) - validators := make([]*ethpb.Validator, 0, count) - for i := 0; i < count; i++ { - 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.SaveStateDeprecated( - context.Background(), - &pbp2p.BeaconState{Validators: validators, Balances: balances}); err != nil { - t.Fatal(err) - } + setupValidators(t, db, count) bs := &BeaconChainServer{ beaconDB: db, @@ -483,24 +434,11 @@ func TestBeaconChainServer_GetValidatorsPagination(t *testing.T) { } func TestBeaconChainServer_GetValidatorsPaginationOutOfRange(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) count := 1 - validators := make([]*ethpb.Validator, 0, count) - for i := 0; i < count; i++ { - 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.SaveStateDeprecated( - context.Background(), - &pbp2p.BeaconState{Validators: validators}); err != nil { - t.Fatal(err) - } - + validators, _ := setupValidators(t, db, count) bs := &BeaconChainServer{ beaconDB: db, } @@ -524,24 +462,10 @@ func TestBeaconChainServer_GetValidatorsExceedsMaxPageSize(t *testing.T) { } func TestBeaconChainServer_GetValidatorsDefaultPageSize(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) - - count := 1000 - validators := make([]*ethpb.Validator, 0, count) - for i := 0; i < count; i++ { - 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.SaveStateDeprecated( - context.Background(), - &pbp2p.BeaconState{Validators: validators}); err != nil { - t.Fatal(err) - } + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) + validators, _ := setupValidators(t, db, 1000) bs := &BeaconChainServer{ beaconDB: db, } @@ -560,24 +484,10 @@ func TestBeaconChainServer_GetValidatorsDefaultPageSize(t *testing.T) { } func TestBeaconChainServer_ListAssignmentsInputOutOfRange(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) - - count := 1 - validators := make([]*ethpb.Validator, 0, count) - for i := 0; i < count; i++ { - 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.SaveStateDeprecated( - context.Background(), - &pbp2p.BeaconState{Validators: validators}); err != nil { - t.Fatal(err) - } + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) + setupValidators(t, db, 1) bs := &BeaconChainServer{beaconDB: db} wanted := fmt.Sprintf("page start %d >= list %d", 0, 0) @@ -598,13 +508,14 @@ func TestBeaconChainServer_ListAssignmentsExceedsMaxPageSize(t *testing.T) { } func TestBeaconChainServer_ListAssignmentsDefaultPageSize(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) + ctx := context.Background() count := 1000 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndex(ctx, [48]byte{byte(i)}, uint64(i)); err != nil { t.Fatal(err) } // Mark the validators with index divisible by 3 inactive. @@ -615,11 +526,22 @@ func TestBeaconChainServer_ListAssignmentsDefaultPageSize(t *testing.T) { } } + blk := ðpb.BeaconBlock{ + Slot: 0, + } + blockRoot, err := ssz.SigningRoot(blk) + if err != nil { + t.Fatal(err) + } + if err := db.SaveHeadBlockRoot(ctx, blockRoot); err != nil { + t.Fatal(err) + } + s := &pbp2p.BeaconState{ Validators: validators, RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)} - if err := db.SaveStateDeprecated(context.Background(), s); err != nil { + if err := db.SaveState(ctx, s, blockRoot); err != nil { t.Fatal(err) } @@ -660,23 +582,35 @@ func TestBeaconChainServer_ListAssignmentsDefaultPageSize(t *testing.T) { func TestBeaconChainServer_ListAssignmentsFilterPubkeysIndicesNoPage(t *testing.T) { helpers.ClearAllCaches() - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) + ctx := context.Background() count := 100 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndex(ctx, [48]byte{byte(i)}, uint64(i)); err != nil { t.Fatal(err) } validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}, ExitEpoch: params.BeaconConfig().FarFutureEpoch}) } + blk := ðpb.BeaconBlock{ + Slot: 0, + } + blockRoot, err := ssz.SigningRoot(blk) + if err != nil { + t.Fatal(err) + } + if err := db.SaveHeadBlockRoot(ctx, blockRoot); err != nil { + t.Fatal(err) + } + s := &pbp2p.BeaconState{ Validators: validators, RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)} - if err := db.SaveStateDeprecated(context.Background(), s); err != nil { + if err := db.SaveState(ctx, s, blockRoot); err != nil { t.Fatal(err) } @@ -718,23 +652,35 @@ func TestBeaconChainServer_ListAssignmentsFilterPubkeysIndicesNoPage(t *testing. func TestBeaconChainServer_ListAssignmentsCanFilterPubkeysIndicesWithPages(t *testing.T) { helpers.ClearAllCaches() - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) + ctx := context.Background() count := 100 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { - if err := db.SaveValidatorIndexDeprecated([]byte{byte(i)}, i); err != nil { + if err := db.SaveValidatorIndex(ctx, [48]byte{byte(i)}, uint64(i)); err != nil { t.Fatal(err) } validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}, ExitEpoch: params.BeaconConfig().FarFutureEpoch}) } + blk := ðpb.BeaconBlock{ + Slot: 0, + } + blockRoot, err := ssz.SigningRoot(blk) + if err != nil { + t.Fatal(err) + } + if err := db.SaveHeadBlockRoot(ctx, blockRoot); err != nil { + t.Fatal(err) + } + s := &pbp2p.BeaconState{ Validators: validators, RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), ActiveIndexRoots: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)} - if err := db.SaveStateDeprecated(context.Background(), s); err != nil { + if err := db.SaveState(ctx, s, blockRoot); err != nil { t.Fatal(err) } @@ -814,10 +760,10 @@ func TestBeaconChainServer_ListAssignmentsCanFilterPubkeysIndicesWithPages(t *te func TestBeaconChainServer_GetValidatorsParticipation(t *testing.T) { helpers.ClearAllCaches() + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) - + ctx := context.Background() epoch := uint64(1) attestedBalance := uint64(1) validatorCount := uint64(100) @@ -861,14 +807,21 @@ func TestBeaconChainServer_GetValidatorsParticipation(t *testing.T) { beaconDB: db, } - if err := bs.beaconDB.(*db2.BeaconDB).SaveStateDeprecated(context.Background(), s); err != nil { + block := ðpb.BeaconBlock{ + Slot: 1, + } + blockRoot, err := ssz.SigningRoot(block) + if err != nil { t.Fatal(err) } - if err := bs.beaconDB.(*db2.BeaconDB).SaveFinalizedBlock(ðpb.BeaconBlock{Slot: 1}); err != nil { + if err := bs.beaconDB.SaveHeadBlockRoot(ctx, blockRoot); err != nil { + t.Fatal(err) + } + if err := bs.beaconDB.SaveState(ctx, s, blockRoot); err != nil { t.Fatal(err) } - res, err := bs.GetValidatorParticipation(context.Background(), ðpb.GetValidatorParticipationRequest{Epoch: epoch}) + res, err := bs.GetValidatorParticipation(ctx, ðpb.GetValidatorParticipationRequest{Epoch: epoch}) if err != nil { t.Fatal(err) } @@ -886,8 +839,8 @@ func TestBeaconChainServer_GetValidatorsParticipation(t *testing.T) { } func TestBeaconChainServer_ListBlocksPagination(t *testing.T) { - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) ctx := context.Background() count := uint64(100) @@ -896,11 +849,11 @@ func TestBeaconChainServer_ListBlocksPagination(t *testing.T) { b := ðpb.BeaconBlock{ Slot: i, } - if err := db.SaveBlockDeprecated(b); err != nil { - t.Fatal(err) - } blks[i] = b } + if err := db.SaveBlocks(ctx, blks); err != nil { + t.Fatal(err) + } root6, err := ssz.SigningRoot(ðpb.BeaconBlock{Slot: 6}) if err != nil { @@ -974,15 +927,15 @@ func TestBeaconChainServer_ListBlocksPagination(t *testing.T) { t.Fatal(err) } if !proto.Equal(res, test.res) { - t.Error("Incorrect blocks response") + t.Errorf("Incorrect blocks response, wanted %d, received %d", len(test.res.Blocks), len(res.Blocks)) } } } func TestBeaconChainServer_ListBlocksErrors(t *testing.T) { + db := testutil.SetupDB(t) + defer testutil.TeardownDB(t, db) ctx := context.Background() - db := internal.SetupDBDeprecated(t) - defer internal.TeardownDBDeprecated(t, db) bs := &BeaconChainServer{beaconDB: db} exceedsMax := int32(params.BeaconConfig().MaxPageSize + 1) @@ -1009,7 +962,6 @@ func TestBeaconChainServer_ListBlocksErrors(t *testing.T) { } if res.TotalSize != 0 { t.Errorf("wanted total size 0, got size %d", res.TotalSize) - } req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Slot{}} @@ -1051,3 +1003,34 @@ func TestBeaconChainServer_ListBlocksErrors(t *testing.T) { } } + +func setupValidators(t *testing.T, db db.Database, count int) ([]*ethpb.Validator, []uint64) { + ctx := context.Background() + balances := make([]uint64, count) + validators := make([]*ethpb.Validator, 0, count) + for i := 0; i < count; i++ { + if err := db.SaveValidatorIndex(ctx, [48]byte{byte(i)}, uint64(i)); err != nil { + t.Fatal(err) + } + balances[i] = uint64(i) + validators = append(validators, ðpb.Validator{PublicKey: []byte{byte(i)}}) + } + blk := ðpb.BeaconBlock{ + Slot: 0, + } + blockRoot, err := ssz.SigningRoot(blk) + if err != nil { + t.Fatal(err) + } + if err := db.SaveHeadBlockRoot(ctx, blockRoot); err != nil { + t.Fatal(err) + } + if err := db.SaveState( + context.Background(), + &pbp2p.BeaconState{Validators: validators, Balances: balances}, + blockRoot, + ); err != nil { + t.Fatal(err) + } + return validators, balances +}