Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle missing archived index #5899

Merged
merged 9 commits into from
May 18, 2020
5 changes: 4 additions & 1 deletion beacon-chain/state/stategen/cold.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ func (s *State) loadColdStateBySlot(ctx context.Context, slot uint64) (*state.Be
return nil, err
}
if archivedState == nil {
archivedRoot := s.archivedRoot(ctx, slot)
archivedRoot, err := s.archivedRoot(ctx, slot)
if err != nil {
return nil, err
}
archivedState, err = s.recoverStateByRoot(ctx, archivedRoot)
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions beacon-chain/state/stategen/cold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func TestLoadColdStateByRoot_CanGet(t *testing.T) {
if err := service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot); err != nil {
t.Fatal(err)
}
if service.beaconDB.SaveBlock(ctx, blk) != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveState(ctx, beaconState, blkRoot); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -121,6 +124,9 @@ func TestLoadColdStateBySlot_CanGet(t *testing.T) {
if err := service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot); err != nil {
t.Fatal(err)
}
if service.beaconDB.SaveBlock(ctx, blk) != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveState(ctx, beaconState, blkRoot); err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 6 additions & 0 deletions beacon-chain/state/stategen/getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func TestStateByRoot_ColdState(t *testing.T) {
if err := service.beaconDB.SaveState(ctx, beaconState, bRoot); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveBlock(ctx, b); err != nil {
t.Fatal(err)
}
if err := service.beaconDB.SaveGenesisBlockRoot(ctx, bRoot); err != nil {
t.Fatal(err)
}
r := [32]byte{'a'}
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
Root: r[:],
Expand Down
26 changes: 22 additions & 4 deletions beacon-chain/state/stategen/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,36 @@ func (s *State) genesisRoot(ctx context.Context) ([32]byte, error) {
return stateutil.BlockRoot(b.Block)
}

// This retrieves the archived root in the DB.
func (s *State) archivedRoot(ctx context.Context, slot uint64) [32]byte {
// This retrieves the highest archived root that represents the input slot in the DB.
func (s *State) archivedRoot(ctx context.Context, slot uint64) ([32]byte, error) {
archivedIndex := uint64(0)
if slot/params.BeaconConfig().SlotsPerArchivedPoint > 1 {
archivedIndex = slot/params.BeaconConfig().SlotsPerArchivedPoint - 1
}
return s.beaconDB.ArchivedPointRoot(ctx, archivedIndex)
fmt.Println(archivedIndex)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this number increasing constantly, reached 130 and I turned off my slasher.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good. That means your slasher is doing work 👍 I removed the debug log

for archivedIndex > 0 {
if ctx.Err() != nil {
return [32]byte{}, ctx.Err()
}
if s.beaconDB.HasArchivedPoint(ctx, archivedIndex) {
return s.beaconDB.ArchivedPointRoot(ctx, archivedIndex), nil
}
archivedIndex--
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this? Can you add more to the godoc description about what this implementation is doing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

}

if archivedIndex == 0 {
return s.genesisRoot(ctx)
}

return [32]byte{}, errUnknownArchivedState
}

// This retrieves the archived state in the DB.
func (s *State) archivedState(ctx context.Context, slot uint64) (*state.BeaconState, error) {
archivedRoot := s.archivedRoot(ctx, slot)
archivedRoot, err := s.archivedRoot(ctx, slot)
if err != nil {
return nil, err
}
return s.beaconDB.State(ctx, archivedRoot)
}

Expand Down
62 changes: 56 additions & 6 deletions beacon-chain/state/stategen/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,35 +534,85 @@ func TestLastSavedState_NoSavedBlockState(t *testing.T) {
}
}

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

r := [32]byte{'a'}
if err := db.SaveArchivedPointRoot(ctx, r, 0); err != nil {
if err := db.SaveArchivedPointRoot(ctx, r, 1); err != nil {
t.Fatal(err)
}
got, err := service.archivedRoot(ctx, params.BeaconConfig().SlotsPerArchivedPoint*2)
if err != nil {
t.Fatal(err)
}
if r != got {
t.Error("Did not get wanted root")
}
}

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

r := [32]byte{'a'}
if err := db.SaveArchivedPointRoot(ctx, r, 10); err != nil {
t.Fatal(err)
}
r = [32]byte{'b'}
if err := db.SaveArchivedPointRoot(ctx, r, 11); err != nil {
t.Fatal(err)
}
got, err := service.archivedRoot(ctx, 100000)
if err != nil {
t.Fatal(err)
}
got := service.archivedRoot(ctx, params.BeaconConfig().SlotsPerArchivedPoint)
if r != got {
t.Error("Did not get wanted root")
}
}

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

gBlock := &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{}}
gRoot, err := stateutil.BlockRoot(gBlock.Block)
if err != nil {
t.Fatal(err)
}
if err := db.SaveBlock(ctx, gBlock); err != nil {
t.Fatal(err)
}
if err := db.SaveGenesisBlockRoot(ctx, gRoot); err != nil {
t.Fatal(err)
}
got, err := service.archivedRoot(ctx, 100000)
if err != nil {
t.Fatal(err)
}
if gRoot != got {
t.Error("Did not get wanted root")
}
}

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

r := [32]byte{'a'}
if err := db.SaveArchivedPointRoot(ctx, r, 0); err != nil {
if err := db.SaveArchivedPointRoot(ctx, r, 1); err != nil {
t.Fatal(err)
}
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
if err := db.SaveState(ctx, beaconState, r); err != nil {
t.Fatal(err)
}
got, err := service.archivedState(ctx, params.BeaconConfig().SlotsPerArchivedPoint)
got, err := service.archivedState(ctx, params.BeaconConfig().SlotsPerArchivedPoint*2)
if err != nil {
t.Fatal(err)
}
Expand Down