Skip to content

Commit

Permalink
Added recover to standard antehandler
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSripal committed Jul 13, 2018
1 parent cb9c7fe commit f6d5c85
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions x/auth/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,32 @@ func NewAnteHandler(am AccountMapper, fck FeeCollectionKeeper) sdk.AnteHandler {

return func(
ctx sdk.Context, tx sdk.Tx,
) (_ sdk.Context, _ sdk.Result, abort bool) {
) (_ sdk.Context, res sdk.Result, abort bool) {

// This AnteHandler requires Txs to be StdTxs
stdTx, ok := tx.(StdTx)
if !ok {
return ctx, sdk.ErrInternal("tx must be StdTx").Result(), true
}

// AnteHandlers must have their own defer/recover in order
// for the BaseApp to know how much gas was used!
// This is because the GasMeter is created in the AnteHandler,
// but if it panics the context won't be set properly in runTx's recover ...
defer func() {
if r := recover(); r != nil {
switch rType := r.(type) {
case sdk.ErrorOutOfGas:
log := fmt.Sprintf("out of gas in location: %v", rType.Descriptor)
res = sdk.ErrOutOfGas(log).Result()
res.GasWanted = stdTx.Fee.Gas
res.GasUsed = ctx.GasMeter().GasConsumed()
default:
panic(r)
}
}
}()

err := validateBasic(stdTx)
if err != nil {
return ctx, err.Result(), true
Expand Down Expand Up @@ -90,7 +108,7 @@ func NewAnteHandler(am AccountMapper, fck FeeCollectionKeeper) sdk.AnteHandler {

// TODO: tx tags (?)

return ctx, sdk.Result{}, false // continue...
return ctx, sdk.Result{GasWanted: stdTx.Fee.Gas}, false // continue...
}
}

Expand Down

0 comments on commit f6d5c85

Please sign in to comment.