Skip to content

Commit

Permalink
Add TaxGas into CheckTxResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
phamminh0811 committed Sep 21, 2024
1 parent 8172e9b commit baf765a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
5 changes: 3 additions & 2 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
panic(fmt.Sprintf("unknown RequestCheckTx type: %s", req.Type))
}

gInfo, result, anteEvents, priority, tx, err := app.runTx(mode, req.Tx)
gInfo, taxGas, result, anteEvents, priority, tx, err := app.runTx(mode, req.Tx)
if err != nil {
return sdkerrors.ResponseCheckTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, anteEvents, app.trace)
}
Expand All @@ -384,6 +384,7 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
Events: sdk.MarkEventsToIndex(result.Events, app.indexEvents),
Priority: priority,
IsOracleTx: isOracleTx(tx.GetMsgs()),
TaxGas: taxGas,
}
}

Expand Down Expand Up @@ -424,7 +425,7 @@ func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) (res abci.ResponseDeliv
telemetry.SetGauge(float32(gInfo.GasWanted), "tx", "gas", "wanted")
}()

gInfo, result, anteEvents, _, _, err := app.runTx(runTxModeDeliver, req.Tx)
gInfo, _, result, anteEvents, _, _, err := app.runTx(runTxModeDeliver, req.Tx)
if err != nil {
resultStr = "failed"
return sdkerrors.ResponseDeliverTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, sdk.MarkEventsToIndex(anteEvents, app.indexEvents), app.trace)
Expand Down
27 changes: 16 additions & 11 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context
// Note, gas execution info is always returned. A reference to a Result is
// returned if the tx does not run out of gas and if all the messages are valid
// and execute successfully. An error is returned otherwise.
func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, result *sdk.Result, anteEvents []abci.Event, priority int64, tx sdk.Tx, err error) {
func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, taxGas int64, result *sdk.Result, anteEvents []abci.Event, priority int64, tx sdk.Tx, err error) {
// NOTE: GasWanted should be returned by the AnteHandler. GasUsed is
// determined by the GasMeter. We need access to the context to get the gas
// meter, so we initialize upfront.
Expand All @@ -644,7 +644,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re

// only run the tx if there is block gas remaining
if mode == runTxModeDeliver && ctx.BlockGasMeter().IsOutOfGas() {
return gInfo, nil, nil, 0, nil, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx")
return gInfo, 0, nil, nil, 0, nil, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx")
}

defer func() {
Expand Down Expand Up @@ -683,12 +683,12 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re

tx, err = app.txDecoder(txBytes)
if err != nil {
return sdk.GasInfo{}, nil, nil, 0, nil, err
return sdk.GasInfo{}, 0, nil, nil, 0, nil, err
}

msgs := tx.GetMsgs()
if err := validateBasicTxMsgs(msgs); err != nil {
return sdk.GasInfo{}, nil, nil, 0, nil, err
return sdk.GasInfo{}, 0, nil, nil, 0, nil, err
}

if app.anteHandler != nil {
Expand Down Expand Up @@ -724,7 +724,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
gasWanted = ctx.GasMeter().Limit()

if err != nil {
return gInfo, nil, nil, 0, nil, err
return gInfo, 0, nil, nil, 0, nil, err
}

priority = ctx.Priority()
Expand All @@ -735,12 +735,12 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
if mode == runTxModeCheck {
err = app.mempool.Insert(ctx, tx)
if err != nil {
return gInfo, nil, anteEvents, priority, nil, err
return gInfo, 0, nil, anteEvents, priority, nil, err
}
} else if mode == runTxModeDeliver {
err = app.mempool.Remove(tx)
if err != nil && !errors.Is(err, mempool.ErrTxNotFound) {
return gInfo, nil, anteEvents, priority, nil,
return gInfo, 0, nil, anteEvents, priority, nil,
fmt.Errorf("failed to remove tx from mempool: %w", err)
}
}
Expand All @@ -766,7 +766,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re

newCtx, err := app.postHandler(postCtx, tx, mode == runTxModeSimulate || mode == runTxModeSimulateSpecial, err == nil)
if err != nil {
return gInfo, nil, anteEvents, priority, nil, err
return gInfo, 0, nil, anteEvents, priority, nil, err
}

result.Events = append(result.Events, newCtx.EventManager().ABCIEvents()...)
Expand All @@ -785,7 +785,12 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
}
}

return gInfo, result, anteEvents, priority, tx, err
taxGas = 0
if ctx.TaxGasMeter().GasConsumed().IsInt64() {
taxGas = ctx.TaxGasMeter().GasConsumed().Int64()
}

return gInfo, taxGas, result, anteEvents, priority, tx, err
}

// runMsgs iterates through a list of messages and executes them with the provided
Expand Down Expand Up @@ -891,7 +896,7 @@ func (app *BaseApp) PrepareProposalVerifyTx(tx sdk.Tx) ([]byte, error) {
return nil, err
}

_, _, _, _, _, err = app.runTx(runTxPrepareProposal, bz) //nolint:dogsled
_, _, _, _, _, _, err = app.runTx(runTxPrepareProposal, bz) //nolint:dogsled
if err != nil {
return nil, err
}
Expand All @@ -910,7 +915,7 @@ func (app *BaseApp) ProcessProposalVerifyTx(txBz []byte) (sdk.Tx, error) {
return nil, err
}

_, _, _, _, _, err = app.runTx(runTxProcessProposal, txBz) //nolint:dogsled
_, _, _, _, _, _, err = app.runTx(runTxProcessProposal, txBz) //nolint:dogsled
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions baseapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ func (app *BaseApp) SimCheck(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo, *
if err != nil {
return sdk.GasInfo{}, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err)
}
gasInfo, result, _, _, _, err := app.runTx(runTxModeCheck, bz)
gasInfo, _, result, _, _, _, err := app.runTx(runTxModeCheck, bz)
return gasInfo, result, err
}

// Simulate executes a tx in simulate mode to get result and gas info.
func (app *BaseApp) Simulate(txBytes []byte) (sdk.GasInfo, *sdk.Result, error) {
gasInfo, result, _, _, _, err := app.runTx(runTxModeSimulate, txBytes)
gasInfo, _, result, _, _, _, err := app.runTx(runTxModeSimulate, txBytes)
return gasInfo, result, err
}

func (app *BaseApp) SimulateSpecial(txBytes []byte) (sdk.GasInfo, *sdk.Result, error) {
gasInfo, result, _, _, _, err := app.runTx(runTxModeSimulateSpecial, txBytes)
gasInfo, _, result, _, _, _, err := app.runTx(runTxModeSimulateSpecial, txBytes)
return gasInfo, result, err
}

Expand All @@ -37,7 +37,7 @@ func (app *BaseApp) SimDeliver(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo,
if err != nil {
return sdk.GasInfo{}, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err)
}
gasInfo, result, _, _, _, err := app.runTx(runTxModeDeliver, bz)
gasInfo, _, result, _, _, _, err := app.runTx(runTxModeDeliver, bz)
return gasInfo, result, err
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ require (
replace (
// use cosmos fork of keyring
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
github.com/cometbft/cometbft => github.com/classic-terra/cometbft v0.37.4-terra1
github.com/cometbft/cometbft => github.com/classic-terra/cometbft v0.37.4-terra.2
// dgrijalva/jwt-go is deprecated and doesn't receive security updates.
// TODO: remove it: https://github.com/cosmos/cosmos-sdk/issues/13134
github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
github.com/classic-terra/cometbft v0.37.4-terra1 h1:eT5B2n5KKi5WVW+3ZNOVTmtfKKaZrXOLX9G80m9mhZo=
github.com/classic-terra/cometbft v0.37.4-terra1/go.mod h1:vFqj7Qe3uFFJvHZleTJPQDmJ/WscXHi4rKWqiCAaNZk=
github.com/classic-terra/cometbft v0.37.4-terra.2 h1:UhUx8zFHs5MlJ1+StdY8haNVYMdT/N6ZhT2MzI0Ti3I=
github.com/classic-terra/cometbft v0.37.4-terra.2/go.mod h1:vFqj7Qe3uFFJvHZleTJPQDmJ/WscXHi4rKWqiCAaNZk=
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand Down
8 changes: 3 additions & 5 deletions x/auth/ante/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,16 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
}

gas := gasTx.GetGas()
cacheTaxGasConsumed := uint64(0)
if !simulate && ctx.CacheTaxGasMeter().GasConsumed().IsInt64() {
cacheTaxGasConsumed := ctx.CacheTaxGasMeter().GasConsumed().Uint64()
if cacheTaxGasConsumed < gas && cacheTaxGasConsumed > 0 {
gas = gas - cacheTaxGasConsumed
}
cacheTaxGasConsumed = ctx.CacheTaxGasMeter().GasConsumed().Uint64()
}
newCtx = SetGasMeter(simulate, ctx, gas)

if cp := ctx.ConsensusParams(); cp != nil && cp.Block != nil {
// If there exists a maximum block gas limit, we must ensure that the tx
// does not exceed it.
if cp.Block.MaxGas > 0 && gas > uint64(cp.Block.MaxGas) {
if cp.Block.MaxGas > 0 && gas-cacheTaxGasConsumed > uint64(cp.Block.MaxGas) {
if simulate {
return newCtx, sdkerrors.Wrapf(sdkerrors.ErrInvalidGasLimit, "tx gas limit %d exceeds block max gas %d", gas, cp.Block.MaxGas)
}
Expand Down

0 comments on commit baf765a

Please sign in to comment.