Skip to content

Commit

Permalink
add check and test (#7853)
Browse files Browse the repository at this point in the history
Co-authored-by: terence tsao <terence@prysmaticlabs.com>
Co-authored-by: Radosław Kapka <rkapka@wp.pl>
  • Loading branch information
3 people committed Nov 19, 2020
1 parent 095c4d5 commit e6ecda5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions beacon-chain/state/stategen/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ func (s *State) loadStateByRoot(ctx context.Context, blockRoot [32]byte) (*state
return nil, errUnknownBoundaryState
}

// Return state early if we are retrieving it from our finalized state cache.
if startState.Slot() == targetSlot {
return startState, nil
}

blks, err := s.LoadBlocks(ctx, startState.Slot()+1, targetSlot, bytesutil.ToBytes32(summary.Root))
if err != nil {
return nil, errors.Wrap(err, "could not load blocks for hot state using root")
Expand Down
27 changes: 27 additions & 0 deletions beacon-chain/state/stategen/getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,33 @@ func TestLoadeStateByRoot_Cached(t *testing.T) {
}
}

func TestLoadeStateByRoot_FinalizedState(t *testing.T) {
ctx := context.Background()
db, _ := testDB.SetupDB(t)
service := New(db, cache.NewStateSummaryCache())

beaconState, _ := testutil.DeterministicGenesisState(t, 32)
genesisStateRoot, err := beaconState.HashTreeRoot(ctx)
require.NoError(t, err)
genesis := blocks.NewGenesisBlock(genesisStateRoot[:])
assert.NoError(t, db.SaveBlock(ctx, genesis))
gRoot, err := genesis.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 0, Root: gRoot[:]}))

service.finalizedInfo.state = beaconState
service.finalizedInfo.slot = beaconState.Slot()
service.finalizedInfo.root = gRoot

// This tests where hot state was already cached.
loadedState, err := service.loadStateByRoot(ctx, gRoot)
require.NoError(t, err)

if !proto.Equal(loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
t.Error("Did not correctly retrieve finalized state")
}
}

func TestLoadeStateByRoot_EpochBoundaryStateCanProcess(t *testing.T) {
ctx := context.Background()
db, ssc := testDB.SetupDB(t)
Expand Down

0 comments on commit e6ecda5

Please sign in to comment.