Add Getter for Genesis Block (#4271)

* test passing

* kafka
This commit is contained in:
Raul Jordan 2019-12-12 16:27:22 -06:00 committed by GitHub
parent 570efe3d04
commit a9a5973b98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters" "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" 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 SaveBlock(ctx context.Context, block *eth.BeaconBlock) error
SaveBlocks(ctx context.Context, blocks []*eth.BeaconBlock) error SaveBlocks(ctx context.Context, blocks []*eth.BeaconBlock) error
SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) error SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) error
GenesisBlock(ctx context.Context) (*ethpb.BeaconBlock, error)
SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error
IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool
// Validator related methods. // Validator related methods.

View File

@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters" "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" 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) 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. // SaveGenesisBlockRoot -- passthrough.
func (e Exporter) SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error { func (e Exporter) SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error {
return e.db.SaveGenesisBlockRoot(ctx, blockRoot) return e.db.SaveGenesisBlockRoot(ctx, blockRoot)

View File

@ -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 = &ethpb.BeaconBlock{}
return decode(enc, block)
})
return block, err
}
// SaveGenesisBlockRoot to the db. // SaveGenesisBlockRoot to the db.
func (k *Store) SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error { func (k *Store) SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveGenesisBlockRoot") ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveGenesisBlockRoot")

View File

@ -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 := &ethpb.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) { func TestStore_BlocksCRUD_NoCache(t *testing.T) {
db := setupDB(t) db := setupDB(t)
defer teardownDB(t, db) defer teardownDB(t, db)