From a9a5973b983c8293148cc59403221cfab4112081 Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Thu, 12 Dec 2019 16:27:22 -0600 Subject: [PATCH] Add Getter for Genesis Block (#4271) * test passing * kafka --- beacon-chain/db/iface/interface.go | 2 ++ beacon-chain/db/kafka/passthrough.go | 6 ++++++ beacon-chain/db/kv/blocks.go | 18 ++++++++++++++++++ beacon-chain/db/kv/blocks_test.go | 27 +++++++++++++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/beacon-chain/db/iface/interface.go b/beacon-chain/db/iface/interface.go index 43467ffd4..e90da7bfd 100644 --- a/beacon-chain/db/iface/interface.go +++ b/beacon-chain/db/iface/interface.go @@ -8,6 +8,7 @@ import ( "github.com/ethereum/go-ethereum/common" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ) @@ -38,6 +39,7 @@ type Database interface { SaveBlock(ctx context.Context, block *eth.BeaconBlock) error SaveBlocks(ctx context.Context, blocks []*eth.BeaconBlock) error SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) error + GenesisBlock(ctx context.Context) (*ethpb.BeaconBlock, error) SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool // Validator related methods. diff --git a/beacon-chain/db/kafka/passthrough.go b/beacon-chain/db/kafka/passthrough.go index e4a68be4d..0ab5e8a56 100644 --- a/beacon-chain/db/kafka/passthrough.go +++ b/beacon-chain/db/kafka/passthrough.go @@ -6,6 +6,7 @@ import ( "github.com/ethereum/go-ethereum/common" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ) @@ -199,6 +200,11 @@ func (e Exporter) SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) err return e.db.SaveHeadBlockRoot(ctx, blockRoot) } +// GenesisBlock -- passthrough. +func (e Exporter) GenesisBlock(ctx context.Context) (*ethpb.BeaconBlock, error) { + return e.db.GenesisBlock(ctx) +} + // SaveGenesisBlockRoot -- passthrough. func (e Exporter) SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error { return e.db.SaveGenesisBlockRoot(ctx, blockRoot) diff --git a/beacon-chain/db/kv/blocks.go b/beacon-chain/db/kv/blocks.go index 8cf5fa567..707f77782 100644 --- a/beacon-chain/db/kv/blocks.go +++ b/beacon-chain/db/kv/blocks.go @@ -318,6 +318,24 @@ func (k *Store) SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) error }) } +// GenesisBlock retrieves the genesis block of the beacon chain. +func (k *Store) GenesisBlock(ctx context.Context) (*ethpb.BeaconBlock, error) { + ctx, span := trace.StartSpan(ctx, "BeaconDB.GenesisBlock") + defer span.End() + var block *ethpb.BeaconBlock + err := k.db.View(func(tx *bolt.Tx) error { + bkt := tx.Bucket(blocksBucket) + root := bkt.Get(genesisBlockRootKey) + enc := bkt.Get(root) + if enc == nil { + return nil + } + block = ðpb.BeaconBlock{} + return decode(enc, block) + }) + return block, err +} + // SaveGenesisBlockRoot to the db. func (k *Store) SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error { ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveGenesisBlockRoot") diff --git a/beacon-chain/db/kv/blocks_test.go b/beacon-chain/db/kv/blocks_test.go index 2c909c2b8..52074be95 100644 --- a/beacon-chain/db/kv/blocks_test.go +++ b/beacon-chain/db/kv/blocks_test.go @@ -139,6 +139,33 @@ func TestStore_BlocksBatchDelete(t *testing.T) { } } +func TestStore_GenesisBlock(t *testing.T) { + db := setupDB(t) + defer teardownDB(t, db) + ctx := context.Background() + genesisBlock := ðpb.BeaconBlock{ + Slot: 0, + ParentRoot: []byte{1, 2, 3}, + } + blockRoot, err := ssz.SigningRoot(genesisBlock) + if err != nil { + t.Fatal(err) + } + if err := db.SaveGenesisBlockRoot(ctx, blockRoot); err != nil { + t.Fatal(err) + } + if err := db.SaveBlock(ctx, genesisBlock); err != nil { + t.Fatal(err) + } + retrievedBlock, err := db.GenesisBlock(ctx) + if err != nil { + t.Fatal(err) + } + if !proto.Equal(genesisBlock, retrievedBlock) { + t.Errorf("Wanted %v, received %v", genesisBlock, retrievedBlock) + } +} + func TestStore_BlocksCRUD_NoCache(t *testing.T) { db := setupDB(t) defer teardownDB(t, db)