Fix Justified and Finalized Retrieval in Genesis (#6861)

* fix
* fix tests
* add test
This commit is contained in:
Nishant Das 2020-08-04 11:34:17 +08:00 committed by GitHub
parent 6af950f001
commit daedb7411c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 21 deletions

View File

@ -176,7 +176,7 @@ func (s *Service) Start() {
// Resume fork choice.
s.justifiedCheckpt = stateTrie.CopyCheckpoint(justifiedCheckpoint)
if err := s.cacheJustifiedStateBalances(s.ctx, bytesutil.ToBytes32(s.justifiedCheckpt.Root)); err != nil {
if err := s.cacheJustifiedStateBalances(s.ctx, s.ensureRootNotZeros(bytesutil.ToBytes32(s.justifiedCheckpt.Root))); err != nil {
log.Fatalf("Could not cache justified state balances: %v", err)
}
s.prevJustifiedCheckpt = stateTrie.CopyCheckpoint(justifiedCheckpoint)

View File

@ -198,6 +198,31 @@ func TestChainService_InitializeBeaconChain(t *testing.T) {
}
}
func TestChainService_CorrectGenesisRoots(t *testing.T) {
ctx := context.Background()
db, sc := testDB.SetupDB(t)
chainService := setupBeaconChain(t, db, sc)
genesisBlk := testutil.NewBeaconBlock()
blkRoot, err := stateutil.BlockRoot(genesisBlk.Block)
require.NoError(t, err)
require.NoError(t, db.SaveBlock(ctx, genesisBlk))
s := testutil.NewBeaconState()
require.NoError(t, s.SetSlot(0))
require.NoError(t, db.SaveState(ctx, s, blkRoot))
require.NoError(t, db.SaveHeadBlockRoot(ctx, blkRoot))
require.NoError(t, db.SaveGenesisBlockRoot(ctx, blkRoot))
// Test the start function.
chainService.Start()
require.DeepEqual(t, params.BeaconConfig().ZeroHash[:], chainService.finalizedCheckpt.Root, "Finalize Checkpoint root is incorrect")
require.DeepEqual(t, params.BeaconConfig().ZeroHash[:], chainService.justifiedCheckpt.Root, "Justified Checkpoint root is incorrect")
require.NoError(t, chainService.Stop(), "Unable to stop chain service")
}
func TestChainService_InitializeChainInfo(t *testing.T) {
db, sc := testDB.SetupDB(t)
ctx := context.Background()

View File

@ -6,6 +6,7 @@ import (
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
bolt "go.etcd.io/bbolt"
"go.opencensus.io/trace"
)
@ -21,9 +22,7 @@ func (kv *Store) JustifiedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, er
bkt := tx.Bucket(checkpointBucket)
enc := bkt.Get(justifiedCheckpointKey)
if enc == nil {
blockBucket := tx.Bucket(blocksBucket)
genesisRoot := blockBucket.Get(genesisBlockRootKey)
checkpoint = &ethpb.Checkpoint{Root: genesisRoot}
checkpoint = &ethpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
return nil
}
checkpoint = &ethpb.Checkpoint{}
@ -41,9 +40,7 @@ func (kv *Store) FinalizedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, er
bkt := tx.Bucket(checkpointBucket)
enc := bkt.Get(finalizedCheckpointKey)
if enc == nil {
blockBucket := tx.Bucket(blocksBucket)
genesisRoot := blockBucket.Get(genesisBlockRootKey)
checkpoint = &ethpb.Checkpoint{Root: genesisRoot}
checkpoint = &ethpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
return nil
}
checkpoint = &ethpb.Checkpoint{}

View File

@ -9,6 +9,7 @@ import (
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
@ -94,16 +95,11 @@ func TestStore_FinalizedCheckpoint_CanSaveRetrieve(t *testing.T) {
}
}
func TestStore_JustifiedCheckpoint_DefaultCantBeNil(t *testing.T) {
func TestStore_JustifiedCheckpoint_DefaultIsZeroHash(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
genesisRoot := [32]byte{'A'}
if err := db.SaveGenesisBlockRoot(ctx, genesisRoot); err != nil {
t.Fatal(err)
}
cp := &ethpb.Checkpoint{Root: genesisRoot[:]}
cp := &ethpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
retrieved, err := db.JustifiedCheckpoint(ctx)
if err != nil {
t.Fatal(err)
@ -113,16 +109,11 @@ func TestStore_JustifiedCheckpoint_DefaultCantBeNil(t *testing.T) {
}
}
func TestStore_FinalizedCheckpoint_DefaultCantBeNil(t *testing.T) {
func TestStore_FinalizedCheckpoint_DefaultIsZeroHash(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
genesisRoot := [32]byte{'B'}
if err := db.SaveGenesisBlockRoot(ctx, genesisRoot); err != nil {
t.Fatal(err)
}
cp := &ethpb.Checkpoint{Root: genesisRoot[:]}
cp := &ethpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
retrieved, err := db.FinalizedCheckpoint(ctx)
if err != nil {
t.Fatal(err)