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: gasWanted & gasUsed are always 0 #51

Merged
merged 2 commits into from
Jan 22, 2021
Merged
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
28 changes: 17 additions & 11 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,13 @@ func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context

func (app *BaseApp) checkTx(txBytes []byte, tx sdk.Tx, recheck bool) (gInfo sdk.GasInfo, err error) {
ctx := app.getCheckContextForTx(txBytes, recheck)
gasCtx := &ctx

defer func() {
if r := recover(); r != nil {
err = recoverToError(r, ctx.GasMeter())
err = app.recoverToError(r, ctx.GasMeter())
}
gInfo = sdk.GasInfo{GasWanted: ctx.GasMeter().Limit(), GasUsed: ctx.GasMeter().GasConsumed()}
gInfo = sdk.GasInfo{GasWanted: gasCtx.GasMeter().Limit(), GasUsed: gasCtx.GasMeter().GasConsumed()}
}()

msgs := tx.GetMsgs()
Expand All @@ -535,14 +536,18 @@ func (app *BaseApp) checkTx(txBytes []byte, tx sdk.Tx, recheck bool) (gInfo sdk.
accKeys := app.accountLock.Lock(ctx, tx)
defer app.accountLock.Unlock(accKeys)

_, err = app.anteTx(ctx, txBytes, tx, false)
var anteCtx sdk.Context
anteCtx, err = app.anteTx(ctx, txBytes, tx, false)
if !anteCtx.IsZero() {
gasCtx = &anteCtx
}

return gInfo, err
}

func (app *BaseApp) anteTx(ctx sdk.Context, txBytes []byte, tx sdk.Tx, simulate bool) (*sdk.Context, error) {
func (app *BaseApp) anteTx(ctx sdk.Context, txBytes []byte, tx sdk.Tx, simulate bool) (sdk.Context, error) {
if app.anteHandler == nil {
return nil, nil
return ctx, nil
}

// Cache wrap context before AnteHandler call in case it aborts.
Expand All @@ -557,11 +562,11 @@ func (app *BaseApp) anteTx(ctx sdk.Context, txBytes []byte, tx sdk.Tx, simulate
newCtx, err := app.anteHandler(anteCtx, tx, simulate)

if err != nil {
return &newCtx, err
return newCtx, err
}

msCache.Write()
return &newCtx, err
return newCtx, err
}

// runTx processes a transaction within a given execution mode, encoded transaction
Expand All @@ -588,7 +593,7 @@ func (app *BaseApp) runTx(txBytes []byte, tx sdk.Tx, simulate bool) (gInfo sdk.G

defer func() {
if r := recover(); r != nil {
err = recoverToError(r, ctx.GasMeter())
err = app.recoverToError(r, ctx.GasMeter())
result = nil
}

Expand Down Expand Up @@ -617,9 +622,9 @@ func (app *BaseApp) runTx(txBytes []byte, tx sdk.Tx, simulate bool) (gInfo sdk.G
return sdk.GasInfo{}, nil, err
}

var newCtx *sdk.Context
var newCtx sdk.Context
newCtx, err = app.anteTx(ctx, txBytes, tx, simulate)
if newCtx != nil && !newCtx.IsZero() {
if !newCtx.IsZero() {
// At this point, newCtx.MultiStore() is cache-wrapped, or something else
// replaced by the AnteHandler. We want the original multistore, not one
// which was cache-wrapped for the AnteHandler.
Expand Down Expand Up @@ -694,7 +699,7 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg) (*sdk.Result, error
}, nil
}

func recoverToError(r interface{}, gasMeter sdk.GasMeter) error {
func (app *BaseApp) recoverToError(r interface{}, gasMeter sdk.GasMeter) error {
switch rType := r.(type) {
// TODO: Use ErrOutOfGas instead of ErrorOutOfGas which would allow us
// to keep the stracktrace.
Expand All @@ -707,6 +712,7 @@ func recoverToError(r interface{}, gasMeter sdk.GasMeter) error {
)

default:
app.logger.Error("recoverToError", "r", r, "stack", string(debug.Stack()))
return sdkerrors.Wrap(
sdkerrors.ErrPanic, fmt.Sprintf(
"recovered: %v\nstack:\n%v", r, string(debug.Stack()),
Expand Down