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

[Merged by Bors] - find topmost applied layer and prune allready applied results #5118

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion tortoise/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,10 @@ func (t *Tortoise) Mode() Mode {
// resetPending compares stored opinion with computed opinion and sets
// pending layer to the layer above equal layer.
// this method is meant to be used only in recovery from disk codepath.
func (t *Tortoise) resetPending(lid types.LayerID, opinion types.Hash32) {
func (t *Tortoise) resetPending(lid types.LayerID, opinion types.Hash32) bool {
if t.trtl.layer(lid).opinion == opinion {
t.trtl.pending = lid + 1
return true
}
return false
}
2 changes: 1 addition & 1 deletion tortoise/model/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (c *core) OnMessage(m Messenger, event Message) {
m.Send(MessageBallot{Ballot: ballot})
case MessageLayerEnd:
if ev.LayerID.After(types.GetEffectiveGenesis()) {
tortoise.RecoverLayer(context.Background(), c.tortoise, c.cdb, c.beacons, ev.LayerID, ev.LayerID, ev.LayerID)
tortoise.RecoverLayer(context.Background(), c.tortoise, c.cdb, c.beacons, ev.LayerID, ev.LayerID, ev.LayerID, ev.LayerID)
m.Notify(EventVerified{ID: c.id, Verified: c.tortoise.LatestComplete(), Layer: ev.LayerID})
}

Expand Down
25 changes: 16 additions & 9 deletions tortoise/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@
}
}
}
for lid := types.GetEffectiveGenesis().Add(1); !lid.After(last); lid = lid.Add(1) {
start := types.GetEffectiveGenesis().Add(1)
for lid := start; !lid.After(last); lid = lid.Add(1) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if err := RecoverLayer(ctx, trtl, db, beacon, lid, last, min(last, latest)); err != nil {
if err := RecoverLayer(ctx, trtl, db, beacon, start, lid, last, min(last, latest)); err != nil {
return nil, fmt.Errorf("failed to load tortoise state at layer %d: %w", lid, err)
}
}
Expand All @@ -83,7 +84,7 @@
return nil
}

func RecoverLayer(ctx context.Context, trtl *Tortoise, db *datastore.CachedDB, beacon system.BeaconGetter, lid, last, current types.LayerID) error {
func RecoverLayer(ctx context.Context, trtl *Tortoise, db *datastore.CachedDB, beacon system.BeaconGetter, start, lid, last, current types.LayerID) error {
if lid.FirstInEpoch() {
if err := recoverEpoch(lid.GetEpoch(), trtl, db, beacon); err != nil {
return err
Expand Down Expand Up @@ -135,13 +136,19 @@
}
if lid <= current && (lid%types.LayerID(trtl.cfg.WindowSize) == 0 || lid == last) {
trtl.TallyVotes(ctx, lid)

opinion, err := layers.GetAggregatedHash(db, lid-1)
if err == nil {
trtl.resetPending(lid-1, opinion)
} else if !errors.Is(err, sql.ErrNotFound) {
return fmt.Errorf("check opinion %w", err)
// find topmost layer that was already applied and reset pending
// so that result for that layer is not returned
for prev := lid - 1; prev >= start; prev-- {
opinion, err := layers.GetAggregatedHash(db, prev)
if err == nil {
if trtl.resetPending(prev, opinion) {
return nil
}
} else if !errors.Is(err, sql.ErrNotFound) {
return fmt.Errorf("check opinion %w", err)
}

Check warning on line 149 in tortoise/recover.go

View check run for this annotation

Codecov / codecov/patch

tortoise/recover.go#L147-L149

Added lines #L147 - L149 were not covered by tests
}

}
return nil
}
2 changes: 1 addition & 1 deletion tortoise/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (a *recoveryAdapter) TallyVotes(ctx context.Context, current types.LayerID)
a.prev = genesis
}
for lid := a.prev; lid <= current; lid++ {
require.NoError(a, RecoverLayer(ctx, a.Tortoise, a.db, a.beacon, lid, current, current))
require.NoError(a, RecoverLayer(ctx, a.Tortoise, a.db, a.beacon, lid, lid, current, current))
a.prev = lid
}
}
Expand Down