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

fix: rollback tipset #6425

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkg/chainsync/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"sync/atomic"
"time"

"github.com/filecoin-project/venus/pkg/consensus"
"github.com/filecoin-project/venus/pkg/crypto"
"github.com/filecoin-project/venus/pkg/repo"
"github.com/filecoin-project/venus/pkg/statemanger"
"github.com/hashicorp/go-multierror"
"github.com/ipfs-force-community/metrics"

"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -216,6 +218,24 @@ func (syncer *Syncer) syncOne(ctx context.Context, parent, next *types.TipSet) e
}
err = wg.Wait()
if err != nil {
var rootNotMatch bool // nolint

if merr, isok := err.(*multierror.Error); isok {
for _, e := range merr.Errors {
if isRootNotMatch(e) {
rootNotMatch = true
break
}
}
} else {
rootNotMatch = isRootNotMatch(err) // nolint
}

if rootNotMatch { // nolint
// todo: should here rollback, and re-compute?
_ = syncer.stmgr.Rollback(ctx, parent, next)
}

return fmt.Errorf("validate mining failed %w", err)
}
}
Expand All @@ -241,6 +261,10 @@ func (syncer *Syncer) syncOne(ctx context.Context, parent, next *types.TipSet) e
return nil
}

func isRootNotMatch(err error) bool {
return errors.Is(err, consensus.ErrStateRootMismatch) || errors.Is(err, consensus.ErrReceiptRootMismatch)
}

// HandleNewTipSet validates and syncs the chain rooted at the provided tipset
// to a chain bsstore. Iff catchup is false then the syncer will set the head.
func (syncer *Syncer) HandleNewTipSet(ctx context.Context, target *syncTypes.Target) (err error) {
Expand Down
17 changes: 3 additions & 14 deletions pkg/statemanger/state_manger.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,28 +228,17 @@ func (s *Stmgr) TipsetState(ctx context.Context, ts *types.TipSet) (*tree.State,

// deprecated: this implementation needs more considerations
func (s *Stmgr) Rollback(ctx context.Context, pts, cts *types.TipSet) error {
log.Infof("rollback chain head from(%d) to a valid tipset", pts.Height())
redo:
log.Infof("rollback chain head from(%d)", pts.Height())
s.stLk.Lock()
defer s.stLk.Unlock()

if err := s.cs.DeleteTipSetMetadata(ctx, pts); err != nil {
s.stLk.Unlock()
return err
}
if err := s.cs.SetHead(ctx, pts); err != nil {
s.stLk.Unlock()
return err
}
s.stLk.Unlock()

if root, _, err := s.RunStateTransition(ctx, pts, nil, false); err != nil {
return err
} else if !root.Equals(cts.At(0).ParentStateRoot) {
cts = pts
if pts, err = s.cs.GetTipSet(ctx, cts.Parents()); err != nil {
return err
}
goto redo
}
return nil
}

Expand Down
Loading