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

Recover from action handling panic #4513

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions action/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var (
ErrGasTipOverFeeCap = errors.New("tip cap is greater than fee cap")
ErrMissRequiredField = errors.New("missing required field")
ErrValueVeryHigh = errors.New("value is very high")
ErrPanic = errors.New("panic")
ErrPanicButReverted = errors.New("panic but reverted")
)

// LoadErrorDescription loads corresponding description related to the error
Expand Down
29 changes: 27 additions & 2 deletions state/factory/workingset.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func withActionCtx(ctx context.Context, selp *action.SealedEnvelope) (context.Co
func (ws *workingSet) runAction(
ctx context.Context,
selp *action.SealedEnvelope,
) (*action.Receipt, error) {
) (receipt *action.Receipt, err error) {
actCtx := protocol.MustGetActionCtx(ctx)
if protocol.MustGetBlockCtx(ctx).GasLimit < actCtx.IntrinsicGas {
return nil, action.ErrGasLimit
Expand Down Expand Up @@ -207,6 +207,24 @@ func (ws *workingSet) runAction(
return nil, errors.Wrapf(err, "Failed to get hash")
}
defer ws.ResetSnapshots()
sn := ws.Snapshot()
defer func() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

could these two defer functions been merged which will make it easier to read and maintain

if err != nil {
if e := ws.Revert(sn); e != nil {
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if the Revert() will be a hard fork here. We may need run fullsync

log.T(ctx).Error("Failed to revert snapshot", zap.Error(e))
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's also log err

return
}
if errors.Is(err, action.ErrPanic) {
err = errors.Wrap(action.ErrPanicButReverted, err.Error())
}
}
}()
defer func() {
if r := recover(); r != nil {
receipt = nil
err = errors.Wrapf(action.ErrPanic, "panic and reverted when running action: %v", r)
}
}()
if err := ws.freshAccountConversion(ctx, &actCtx); err != nil {
return nil, err
}
Expand Down Expand Up @@ -736,6 +754,8 @@ func (ws *workingSet) pickAndRunActions(
actionIterator.PopAccount()
continue
}
actCtx := protocol.MustGetActionCtx(actionCtx)
l := log.L().With(log.Hex("actHash", actCtx.ActionHash[:]), zap.Uint64("height", ws.height))
receipt, err := ws.runAction(actionCtx, nextAction)
switch errors.Cause(err) {
case nil:
Expand All @@ -744,7 +764,12 @@ func (ws *workingSet) pickAndRunActions(
actionIterator.PopAccount()
continue
case action.ErrChainID, errUnfoldTxContainer, errDeployerNotWhitelisted:
log.L().Debug("runAction() failed", zap.Uint64("height", ws.height), zap.Error(err))
l.Debug("runAction() failed", zap.Error(err))
ap.DeleteAction(caller)
actionIterator.PopAccount()
continue
case action.ErrPanicButReverted:
Copy link
Member Author

Choose a reason for hiding this comment

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

if panic but reverted, continue when mint and terminate when validate block

l.Warn("runAction() panic but reverted", zap.Error(err))
ap.DeleteAction(caller)
actionIterator.PopAccount()
continue
Expand Down
Loading