Skip to content

Commit

Permalink
Merge pull request #6425 from filecoin-project/fix/rollback
Browse files Browse the repository at this point in the history
fix: rollback tipset
  • Loading branch information
simlecode authored Oct 22, 2024
2 parents 1282e39 + 7b89946 commit 7552c55
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
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

0 comments on commit 7552c55

Please sign in to comment.