2021-10-01 07:30:22 -07:00
|
|
|
package middleware_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *MWTestSuite) TestValidateBasic() {
|
|
|
|
ctx := s.SetupTest(true) // setup
|
|
|
|
txBuilder := s.clientCtx.TxConfig.NewTxBuilder()
|
|
|
|
|
2021-12-06 01:26:15 -08:00
|
|
|
txHandler := middleware.ComposeMiddlewares(noopTxHandler, middleware.ValidateBasicMiddleware)
|
2021-10-01 07:30:22 -07:00
|
|
|
|
|
|
|
// keys and addresses
|
|
|
|
priv1, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
|
|
|
|
// msg and signatures
|
|
|
|
msg := testdata.NewTestMsg(addr1)
|
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
|
|
gasLimit := testdata.NewTestGasLimit()
|
|
|
|
s.Require().NoError(txBuilder.SetMsgs(msg))
|
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
txBuilder.SetGasLimit(gasLimit)
|
|
|
|
|
|
|
|
privs, accNums, accSeqs := []cryptotypes.PrivKey{}, []uint64{}, []uint64{}
|
|
|
|
invalidTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
refactor: middleware refactor to change tx.Handler interface (#10527)
<!--
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)
2021-12-01 22:54:38 -08:00
|
|
|
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx})
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().NotNil(err, "Did not error on invalid tx")
|
|
|
|
|
|
|
|
privs, accNums, accSeqs = []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
|
|
|
|
validTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
refactor: middleware refactor to change tx.Handler interface (#10527)
<!--
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)
2021-12-01 22:54:38 -08:00
|
|
|
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: validTx})
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().Nil(err, "ValidateBasicMiddleware returned error on valid tx. err: %v", err)
|
|
|
|
|
|
|
|
// test middleware skips on recheck
|
|
|
|
ctx = ctx.WithIsReCheckTx(true)
|
|
|
|
|
|
|
|
// middleware should skip processing invalidTx on recheck and thus return nil-error
|
refactor: middleware refactor to change tx.Handler interface (#10527)
<!--
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)
2021-12-01 22:54:38 -08:00
|
|
|
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx})
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().Nil(err, "ValidateBasicMiddleware ran on ReCheck")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MWTestSuite) TestValidateMemo() {
|
|
|
|
ctx := s.SetupTest(true) // setup
|
|
|
|
txBuilder := s.clientCtx.TxConfig.NewTxBuilder()
|
2021-12-06 01:26:15 -08:00
|
|
|
txHandler := middleware.ComposeMiddlewares(noopTxHandler, middleware.ValidateMemoMiddleware(s.app.AccountKeeper))
|
2021-10-01 07:30:22 -07:00
|
|
|
|
|
|
|
// keys and addresses
|
|
|
|
priv1, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
|
|
|
|
// msg and signatures
|
|
|
|
msg := testdata.NewTestMsg(addr1)
|
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
|
|
gasLimit := testdata.NewTestGasLimit()
|
|
|
|
s.Require().NoError(txBuilder.SetMsgs(msg))
|
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
txBuilder.SetGasLimit(gasLimit)
|
|
|
|
|
|
|
|
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
|
|
|
|
txBuilder.SetMemo(strings.Repeat("01234567890", 500))
|
|
|
|
invalidTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// require that long memos get rejected
|
refactor: middleware refactor to change tx.Handler interface (#10527)
<!--
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)
2021-12-01 22:54:38 -08:00
|
|
|
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx})
|
2021-10-01 07:30:22 -07:00
|
|
|
|
|
|
|
s.Require().NotNil(err, "Did not error on tx with high memo")
|
|
|
|
|
|
|
|
txBuilder.SetMemo(strings.Repeat("01234567890", 10))
|
|
|
|
validTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// require small memos pass ValidateMemo middleware
|
refactor: middleware refactor to change tx.Handler interface (#10527)
<!--
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)
2021-12-01 22:54:38 -08:00
|
|
|
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: validTx})
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().Nil(err, "ValidateBasicMiddleware returned error on valid tx. err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MWTestSuite) TestConsumeGasForTxSize() {
|
|
|
|
ctx := s.SetupTest(true) // setup
|
|
|
|
txBuilder := s.clientCtx.TxConfig.NewTxBuilder()
|
|
|
|
|
2021-12-06 01:26:15 -08:00
|
|
|
txHandler := middleware.ComposeMiddlewares(noopTxHandler, middleware.ConsumeTxSizeGasMiddleware(s.app.AccountKeeper))
|
2021-10-01 07:30:22 -07:00
|
|
|
|
|
|
|
// keys and addresses
|
|
|
|
priv1, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
|
|
|
|
// msg and signatures
|
|
|
|
msg := testdata.NewTestMsg(addr1)
|
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
|
|
gasLimit := testdata.NewTestGasLimit()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
sigV2 signing.SignatureV2
|
|
|
|
}{
|
|
|
|
{"SingleSignatureData", signing.SignatureV2{PubKey: priv1.PubKey()}},
|
|
|
|
{"MultiSignatureData", signing.SignatureV2{PubKey: priv1.PubKey(), Data: multisig.NewMultisig(2)}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
txBuilder = s.clientCtx.TxConfig.NewTxBuilder()
|
|
|
|
s.Require().NoError(txBuilder.SetMsgs(msg))
|
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
txBuilder.SetGasLimit(gasLimit)
|
|
|
|
txBuilder.SetMemo(strings.Repeat("01234567890", 10))
|
|
|
|
|
|
|
|
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
|
|
|
|
testTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
txBytes, err := s.clientCtx.TxConfig.TxJSONEncoder()(testTx)
|
|
|
|
s.Require().Nil(err, "Cannot marshal tx: %v", err)
|
|
|
|
|
|
|
|
params := s.app.AccountKeeper.GetParams(ctx)
|
|
|
|
expectedGas := sdk.Gas(len(txBytes)) * params.TxSizeCostPerByte
|
|
|
|
|
|
|
|
// Set ctx with TxBytes manually
|
|
|
|
ctx = ctx.WithTxBytes(txBytes)
|
|
|
|
|
|
|
|
// track how much gas is necessary to retrieve parameters
|
|
|
|
beforeGas := ctx.GasMeter().GasConsumed()
|
|
|
|
s.app.AccountKeeper.GetParams(ctx)
|
|
|
|
afterGas := ctx.GasMeter().GasConsumed()
|
|
|
|
expectedGas += afterGas - beforeGas
|
|
|
|
|
|
|
|
beforeGas = ctx.GasMeter().GasConsumed()
|
refactor: middleware refactor to change tx.Handler interface (#10527)
<!--
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)
2021-12-01 22:54:38 -08:00
|
|
|
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx, TxBytes: txBytes})
|
2021-10-01 07:30:22 -07:00
|
|
|
|
|
|
|
s.Require().Nil(err, "ConsumeTxSizeGasMiddleware returned error: %v", err)
|
|
|
|
|
|
|
|
// require that middleware consumes expected amount of gas
|
|
|
|
consumedGas := ctx.GasMeter().GasConsumed() - beforeGas
|
|
|
|
s.Require().Equal(expectedGas, consumedGas, "Middleware did not consume the correct amount of gas")
|
|
|
|
|
|
|
|
// simulation must not underestimate gas of this middleware even with nil signatures
|
|
|
|
txBuilder, err := s.clientCtx.TxConfig.WrapTxBuilder(testTx)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(txBuilder.SetSignatures(tc.sigV2))
|
|
|
|
testTx = txBuilder.GetTx()
|
|
|
|
|
|
|
|
simTxBytes, err := s.clientCtx.TxConfig.TxJSONEncoder()(testTx)
|
|
|
|
s.Require().Nil(err, "Cannot marshal tx: %v", err)
|
|
|
|
// require that simulated tx is smaller than tx with signatures
|
|
|
|
s.Require().True(len(simTxBytes) < len(txBytes), "simulated tx still has signatures")
|
|
|
|
|
|
|
|
// Set s.ctx with smaller simulated TxBytes manually
|
|
|
|
ctx = ctx.WithTxBytes(simTxBytes)
|
|
|
|
|
|
|
|
beforeSimGas := ctx.GasMeter().GasConsumed()
|
|
|
|
|
|
|
|
// run txhandler in simulate mode
|
refactor: middleware refactor to change tx.Handler interface (#10527)
<!--
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)
2021-12-01 22:54:38 -08:00
|
|
|
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx, TxBytes: simTxBytes})
|
2021-10-01 07:30:22 -07:00
|
|
|
consumedSimGas := ctx.GasMeter().GasConsumed() - beforeSimGas
|
|
|
|
|
|
|
|
// require that txhandler passes and does not underestimate middleware cost
|
|
|
|
s.Require().Nil(err, "ConsumeTxSizeGasMiddleware returned error: %v", err)
|
|
|
|
s.Require().True(consumedSimGas >= expectedGas, "Simulate mode underestimates gas on Middleware. Simulated cost: %d, expected cost: %d", consumedSimGas, expectedGas)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MWTestSuite) TestTxHeightTimeoutMiddleware() {
|
|
|
|
ctx := s.SetupTest(true)
|
|
|
|
|
2021-12-06 01:26:15 -08:00
|
|
|
txHandler := middleware.ComposeMiddlewares(noopTxHandler, middleware.TxTimeoutHeightMiddleware)
|
2021-10-01 07:30:22 -07:00
|
|
|
|
|
|
|
// keys and addresses
|
|
|
|
priv1, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
|
|
|
|
// msg and signatures
|
|
|
|
msg := testdata.NewTestMsg(addr1)
|
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
|
|
gasLimit := testdata.NewTestGasLimit()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
timeout uint64
|
|
|
|
height int64
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{"default value", 0, 10, false},
|
|
|
|
{"no timeout (greater height)", 15, 10, false},
|
|
|
|
{"no timeout (same height)", 10, 10, false},
|
|
|
|
{"timeout (smaller height)", 9, 10, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
txBuilder := s.clientCtx.TxConfig.NewTxBuilder()
|
|
|
|
|
|
|
|
s.Require().NoError(txBuilder.SetMsgs(msg))
|
|
|
|
|
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
txBuilder.SetGasLimit(gasLimit)
|
|
|
|
txBuilder.SetMemo(strings.Repeat("01234567890", 10))
|
|
|
|
txBuilder.SetTimeoutHeight(tc.timeout)
|
|
|
|
|
|
|
|
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
|
|
|
|
testTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ctx := ctx.WithBlockHeight(tc.height)
|
refactor: middleware refactor to change tx.Handler interface (#10527)
<!--
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)
2021-12-01 22:54:38 -08:00
|
|
|
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx})
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().Equal(tc.expectErr, err != nil, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|