<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #10484 This PR makes the following big changes: ### 1. Change the tx.Handler interface ```diff - CheckTx(ctx context.Context, tx sdk.Tx, req abci.RequestCheckTx) (abci.ResponseCheckTx, error) + CheckTx(ctx context.Context, req tx.Request, checkReq tx.RequestCheckTx) (tx.Response, tx.ResponseCheckTx, error) // same for Deliver and Simulate ``` where: ```go type Response struct { GasWanted uint64 GasUsed uint64 // MsgResponses is an array containing each Msg service handler's response // type, packed in an Any. This will get proto-serialized into the `Data` field // in the ABCI Check/DeliverTx responses. MsgResponses []*codectypes.Any Log string Events []abci.Event } ``` ### 2. Change what gets passed into the ABCI Check/DeliverTx `Data` field Before, we were passing the concatenation of MsgResponse bytes into the `Data`. Now we are passing the proto-serialiazation of this struct: ```proto message TxMsgData { repeated google.protobuf.Any msg_responses = 2; } ``` <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
98 lines
3.0 KiB
Go
98 lines
3.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/cosmos/cosmos-sdk/types/tx"
|
|
)
|
|
|
|
// GasTx defines a Tx with a GetGas() method which is needed to use gasTxHandler.
|
|
type GasTx interface {
|
|
sdk.Tx
|
|
GetGas() uint64
|
|
}
|
|
|
|
type gasTxHandler struct {
|
|
next tx.Handler
|
|
}
|
|
|
|
// GasTxMiddleware defines a simple middleware that sets a new GasMeter on
|
|
// the sdk.Context, and sets the GasInfo on the result. It reads the tx.GetGas()
|
|
// by default, or sets to infinity in simulate mode.
|
|
func GasTxMiddleware(txh tx.Handler) tx.Handler {
|
|
return gasTxHandler{next: txh}
|
|
}
|
|
|
|
var _ tx.Handler = gasTxHandler{}
|
|
|
|
// CheckTx implements tx.Handler.CheckTx.
|
|
func (txh gasTxHandler) CheckTx(ctx context.Context, req tx.Request, checkReq tx.RequestCheckTx) (tx.Response, tx.ResponseCheckTx, error) {
|
|
sdkCtx, err := gasContext(sdk.UnwrapSDKContext(ctx), req.Tx, false)
|
|
if err != nil {
|
|
return tx.Response{}, tx.ResponseCheckTx{}, err
|
|
}
|
|
|
|
res, resCheckTx, err := txh.next.CheckTx(sdk.WrapSDKContext(sdkCtx), req, checkReq)
|
|
|
|
return populateGas(res, sdkCtx), resCheckTx, err
|
|
}
|
|
|
|
// DeliverTx implements tx.Handler.DeliverTx.
|
|
func (txh gasTxHandler) DeliverTx(ctx context.Context, req tx.Request) (tx.Response, error) {
|
|
sdkCtx, err := gasContext(sdk.UnwrapSDKContext(ctx), req.Tx, false)
|
|
if err != nil {
|
|
return tx.Response{}, err
|
|
}
|
|
|
|
res, err := txh.next.DeliverTx(sdk.WrapSDKContext(sdkCtx), req)
|
|
|
|
return populateGas(res, sdkCtx), err
|
|
}
|
|
|
|
// SimulateTx implements tx.Handler.SimulateTx method.
|
|
func (txh gasTxHandler) SimulateTx(ctx context.Context, req tx.Request) (tx.Response, error) {
|
|
sdkCtx, err := gasContext(sdk.UnwrapSDKContext(ctx), req.Tx, true)
|
|
if err != nil {
|
|
return tx.Response{}, err
|
|
}
|
|
|
|
res, err := txh.next.SimulateTx(sdk.WrapSDKContext(sdkCtx), req)
|
|
|
|
return populateGas(res, sdkCtx), err
|
|
}
|
|
|
|
// populateGas returns a new tx.Response with gas fields populated.
|
|
func populateGas(res tx.Response, sdkCtx sdk.Context) tx.Response {
|
|
res.GasWanted = sdkCtx.GasMeter().Limit()
|
|
res.GasUsed = sdkCtx.GasMeter().GasConsumed()
|
|
|
|
return res
|
|
}
|
|
|
|
// gasContext returns a new context with a gas meter set from a given context.
|
|
func gasContext(ctx sdk.Context, tx sdk.Tx, isSimulate bool) (sdk.Context, error) {
|
|
// all transactions must implement GasTx
|
|
gasTx, ok := tx.(GasTx)
|
|
if !ok {
|
|
// Set a gas meter with limit 0 as to prevent an infinite gas meter attack
|
|
// during runTx.
|
|
newCtx := setGasMeter(ctx, 0, isSimulate)
|
|
return newCtx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be GasTx")
|
|
}
|
|
|
|
return setGasMeter(ctx, gasTx.GetGas(), isSimulate), nil
|
|
}
|
|
|
|
// setGasMeter returns a new context with a gas meter set from a given context.
|
|
func setGasMeter(ctx sdk.Context, gasLimit uint64, simulate bool) sdk.Context {
|
|
// In various cases such as simulation and during the genesis block, we do not
|
|
// meter any gas utilization.
|
|
if simulate || ctx.BlockHeight() == 0 {
|
|
return ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
|
}
|
|
|
|
return ctx.WithGasMeter(sdk.NewGasMeter(gasLimit))
|
|
}
|