2021-10-01 07:30:22 -07:00
|
|
|
package middleware_test
|
2019-10-10 05:46:09 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-07-20 05:30:12 -07:00
|
|
|
|
2020-09-04 10:49:07 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2021-10-01 07:30:22 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec/legacy"
|
2020-09-25 01:41:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
2020-09-18 02:40:39 -07:00
|
|
|
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
|
2020-08-28 09:02:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
2021-03-04 13:31:42 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256r1"
|
2020-11-09 08:01:43 -08:00
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2020-06-04 03:38:24 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
|
2020-08-28 09:02:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2019-10-10 05:46:09 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-10-01 07:30:22 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx"
|
2020-08-28 09:02:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
2021-10-01 07:30:22 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/middleware"
|
2021-07-05 07:55:51 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
|
2019-10-10 05:46:09 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
2021-10-01 07:30:22 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2019-10-10 05:46:09 -07:00
|
|
|
)
|
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
func (s *MWTestSuite) TestSetPubKey() {
|
|
|
|
ctx := s.SetupTest(true) // setup
|
|
|
|
txBuilder := s.clientCtx.TxConfig.NewTxBuilder()
|
|
|
|
require := s.Require()
|
|
|
|
txHandler := middleware.ComposeMiddlewares(
|
2021-12-06 01:26:15 -08:00
|
|
|
noopTxHandler,
|
2021-10-01 07:30:22 -07:00
|
|
|
middleware.SetPubKeyMiddleware(s.app.AccountKeeper),
|
|
|
|
)
|
2019-10-10 05:46:09 -07:00
|
|
|
|
|
|
|
// keys and addresses
|
2020-07-20 05:30:12 -07:00
|
|
|
priv1, pub1, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
priv2, pub2, addr2 := testdata.KeyTestPubAddr()
|
2021-03-04 13:31:42 -08:00
|
|
|
priv3, pub3, addr3 := testdata.KeyTestPubAddrSecp256R1(require)
|
2019-10-10 05:46:09 -07:00
|
|
|
|
|
|
|
addrs := []sdk.AccAddress{addr1, addr2, addr3}
|
2020-11-09 08:01:43 -08:00
|
|
|
pubs := []cryptotypes.PubKey{pub1, pub2, pub3}
|
2019-10-10 05:46:09 -07:00
|
|
|
|
|
|
|
msgs := make([]sdk.Msg, len(addrs))
|
|
|
|
// set accounts and create msg for each address
|
|
|
|
for i, addr := range addrs {
|
2021-10-01 07:30:22 -07:00
|
|
|
acc := s.app.AccountKeeper.NewAccountWithAddress(ctx, addr)
|
2021-03-04 13:31:42 -08:00
|
|
|
require.NoError(acc.SetAccountNumber(uint64(i)))
|
2021-10-01 07:30:22 -07:00
|
|
|
s.app.AccountKeeper.SetAccount(ctx, acc)
|
2020-07-06 10:03:45 -07:00
|
|
|
msgs[i] = testdata.NewTestMsg(addr)
|
2019-10-10 05:46:09 -07:00
|
|
|
}
|
2021-10-01 07:30:22 -07:00
|
|
|
require.NoError(txBuilder.SetMsgs(msgs...))
|
|
|
|
txBuilder.SetFeeAmount(testdata.NewTestFeeAmount())
|
|
|
|
txBuilder.SetGasLimit(testdata.NewTestGasLimit())
|
2019-10-10 05:46:09 -07:00
|
|
|
|
2020-11-09 08:01:43 -08:00
|
|
|
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1, priv2, priv3}, []uint64{0, 1, 2}, []uint64{0, 0, 0}
|
2021-10-01 07:30:22 -07:00
|
|
|
testTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
|
2021-03-04 13:31:42 -08:00
|
|
|
require.NoError(err)
|
2019-10-10 05:46:09 -07:00
|
|
|
|
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})
|
2021-03-04 13:31:42 -08:00
|
|
|
require.NoError(err)
|
2019-10-10 05:46:09 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
// Require that all accounts have pubkey set after middleware runs
|
2019-10-10 05:46:09 -07:00
|
|
|
for i, addr := range addrs {
|
2021-10-01 07:30:22 -07:00
|
|
|
pk, err := s.app.AccountKeeper.GetPubKey(ctx, addr)
|
2021-03-04 13:31:42 -08:00
|
|
|
require.NoError(err, "Error on retrieving pubkey from account")
|
|
|
|
require.True(pubs[i].Equals(pk),
|
|
|
|
"Wrong Pubkey retrieved from AccountKeeper, idx=%d\nexpected=%s\n got=%s", i, pubs[i], pk)
|
2019-10-10 05:46:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
func (s *MWTestSuite) TestConsumeSignatureVerificationGas() {
|
2019-10-10 05:46:09 -07:00
|
|
|
params := types.DefaultParams()
|
|
|
|
msg := []byte{1, 2, 3, 4}
|
2021-01-07 13:50:52 -08:00
|
|
|
cdc := simapp.MakeTestEncodingConfig().Amino
|
2019-10-10 05:46:09 -07:00
|
|
|
|
2021-03-04 13:31:42 -08:00
|
|
|
p := types.DefaultParams()
|
|
|
|
skR1, _ := secp256r1.GenPrivKey()
|
2019-10-10 05:46:09 -07:00
|
|
|
pkSet1, sigSet1 := generatePubKeysAndSignatures(5, msg, false)
|
2020-09-18 02:40:39 -07:00
|
|
|
multisigKey1 := kmultisig.NewLegacyAminoPubKey(2, pkSet1)
|
2019-10-10 05:46:09 -07:00
|
|
|
multisignature1 := multisig.NewMultisig(len(pkSet1))
|
|
|
|
expectedCost1 := expectedGasCostByKeys(pkSet1)
|
|
|
|
for i := 0; i < len(pkSet1); i++ {
|
2020-09-22 07:35:18 -07:00
|
|
|
stdSig := legacytx.StdSignature{PubKey: pkSet1[i], Signature: sigSet1[i]}
|
|
|
|
sigV2, err := legacytx.StdSignatureToSignatureV2(cdc, stdSig)
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().NoError(err)
|
2020-06-12 11:13:20 -07:00
|
|
|
err = multisig.AddSignatureV2(multisignature1, sigV2, pkSet1)
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().NoError(err)
|
2019-10-10 05:46:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
meter sdk.GasMeter
|
2020-06-29 13:48:41 -07:00
|
|
|
sig signing.SignatureData
|
2020-11-09 08:01:43 -08:00
|
|
|
pubkey cryptotypes.PubKey
|
2019-10-10 05:46:09 -07:00
|
|
|
params types.Params
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
gasConsumed uint64
|
|
|
|
shouldErr bool
|
|
|
|
}{
|
2021-03-04 13:31:42 -08:00
|
|
|
{"PubKeyEd25519", args{sdk.NewInfiniteGasMeter(), nil, ed25519.GenPrivKey().PubKey(), params}, p.SigVerifyCostED25519, true},
|
|
|
|
{"PubKeySecp256k1", args{sdk.NewInfiniteGasMeter(), nil, secp256k1.GenPrivKey().PubKey(), params}, p.SigVerifyCostSecp256k1, false},
|
|
|
|
{"PubKeySecp256r1", args{sdk.NewInfiniteGasMeter(), nil, skR1.PubKey(), params}, p.SigVerifyCostSecp256r1(), false},
|
2020-06-29 13:48:41 -07:00
|
|
|
{"Multisig", args{sdk.NewInfiniteGasMeter(), multisignature1, multisigKey1, params}, expectedCost1, false},
|
2019-10-10 05:46:09 -07:00
|
|
|
{"unknown key", args{sdk.NewInfiniteGasMeter(), nil, nil, params}, 0, true},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2020-07-20 05:30:12 -07:00
|
|
|
sigV2 := signing.SignatureV2{
|
2020-08-21 07:20:47 -07:00
|
|
|
PubKey: tt.args.pubkey,
|
|
|
|
Data: tt.args.sig,
|
|
|
|
Sequence: 0, // Arbitrary account sequence
|
2020-07-20 05:30:12 -07:00
|
|
|
}
|
2021-10-01 07:30:22 -07:00
|
|
|
err := middleware.DefaultSigVerificationGasConsumer(tt.args.meter, sigV2, tt.args.params)
|
2020-07-20 05:30:12 -07:00
|
|
|
|
|
|
|
if tt.shouldErr {
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().NotNil(err)
|
2020-07-20 05:30:12 -07:00
|
|
|
} else {
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().Nil(err)
|
|
|
|
s.Require().Equal(tt.gasConsumed, tt.args.meter.GasConsumed(), fmt.Sprintf("%d != %d", tt.gasConsumed, tt.args.meter.GasConsumed()))
|
2020-07-20 05:30:12 -07:00
|
|
|
}
|
2019-10-10 05:46:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
func (s *MWTestSuite) TestSigVerification() {
|
|
|
|
ctx := s.SetupTest(true) // setup
|
2020-07-20 05:30:12 -07:00
|
|
|
|
2019-10-23 07:43:58 -07:00
|
|
|
// make block height non-zero to ensure account numbers part of signBytes
|
2021-10-01 07:30:22 -07:00
|
|
|
ctx = ctx.WithBlockHeight(1)
|
|
|
|
txHandler := middleware.ComposeMiddlewares(
|
2021-12-06 01:26:15 -08:00
|
|
|
noopTxHandler,
|
2021-10-01 07:30:22 -07:00
|
|
|
middleware.SetPubKeyMiddleware(s.app.AccountKeeper),
|
|
|
|
middleware.SigVerificationMiddleware(
|
|
|
|
s.app.AccountKeeper,
|
|
|
|
s.clientCtx.TxConfig.SignModeHandler(),
|
|
|
|
),
|
|
|
|
)
|
2019-10-10 05:46:09 -07:00
|
|
|
|
|
|
|
// keys and addresses
|
2020-07-20 05:30:12 -07:00
|
|
|
priv1, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
priv2, _, addr2 := testdata.KeyTestPubAddr()
|
|
|
|
priv3, _, addr3 := testdata.KeyTestPubAddr()
|
2019-10-10 05:46:09 -07:00
|
|
|
|
|
|
|
addrs := []sdk.AccAddress{addr1, addr2, addr3}
|
|
|
|
|
|
|
|
msgs := make([]sdk.Msg, len(addrs))
|
|
|
|
// set accounts and create msg for each address
|
|
|
|
for i, addr := range addrs {
|
2021-10-01 07:30:22 -07:00
|
|
|
acc := s.app.AccountKeeper.NewAccountWithAddress(ctx, addr)
|
|
|
|
s.Require().NoError(acc.SetAccountNumber(uint64(i)))
|
|
|
|
s.app.AccountKeeper.SetAccount(ctx, acc)
|
2020-09-04 10:49:07 -07:00
|
|
|
msgs[i] = testdata.NewTestMsg(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
|
|
gasLimit := testdata.NewTestGasLimit()
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
2020-11-09 08:01:43 -08:00
|
|
|
privs []cryptotypes.PrivKey
|
2020-09-04 10:49:07 -07:00
|
|
|
accNums []uint64
|
|
|
|
accSeqs []uint64
|
|
|
|
recheck bool
|
|
|
|
shouldErr bool
|
|
|
|
}
|
|
|
|
testCases := []testCase{
|
2020-11-09 08:01:43 -08:00
|
|
|
{"no signers", []cryptotypes.PrivKey{}, []uint64{}, []uint64{}, false, true},
|
|
|
|
{"not enough signers", []cryptotypes.PrivKey{priv1, priv2}, []uint64{0, 1}, []uint64{0, 0}, false, true},
|
|
|
|
{"wrong order signers", []cryptotypes.PrivKey{priv3, priv2, priv1}, []uint64{2, 1, 0}, []uint64{0, 0, 0}, false, true},
|
|
|
|
{"wrong accnums", []cryptotypes.PrivKey{priv1, priv2, priv3}, []uint64{7, 8, 9}, []uint64{0, 0, 0}, false, true},
|
|
|
|
{"wrong sequences", []cryptotypes.PrivKey{priv1, priv2, priv3}, []uint64{0, 1, 2}, []uint64{3, 4, 5}, false, true},
|
|
|
|
{"valid tx", []cryptotypes.PrivKey{priv1, priv2, priv3}, []uint64{0, 1, 2}, []uint64{0, 0, 0}, false, false},
|
|
|
|
{"no err on recheck", []cryptotypes.PrivKey{}, []uint64{}, []uint64{}, true, false},
|
2020-09-04 10:49:07 -07:00
|
|
|
}
|
|
|
|
for i, tc := range testCases {
|
2021-10-01 07:30:22 -07:00
|
|
|
ctx = ctx.WithIsReCheckTx(tc.recheck)
|
|
|
|
txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Create new txBuilder for each test
|
2020-09-04 10:49:07 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().NoError(txBuilder.SetMsgs(msgs...))
|
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
txBuilder.SetGasLimit(gasLimit)
|
2020-09-04 10:49:07 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
testTx, _, err := s.createTestTx(txBuilder, tc.privs, tc.accNums, tc.accSeqs, ctx.ChainID())
|
|
|
|
s.Require().NoError(err)
|
2020-09-04 10:49:07 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
if tc.recheck {
|
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.CheckTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}, tx.RequestCheckTx{Type: abci.CheckTxType_Recheck})
|
2021-10-01 07:30:22 -07:00
|
|
|
} else {
|
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.CheckTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}, tx.RequestCheckTx{})
|
2021-10-01 07:30:22 -07:00
|
|
|
}
|
2020-09-04 10:49:07 -07:00
|
|
|
if tc.shouldErr {
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().NotNil(err, "TestCase %d: %s did not error as expected", i, tc.name)
|
2020-09-04 10:49:07 -07:00
|
|
|
} else {
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().Nil(err, "TestCase %d: %s errored unexpectedly. Err: %v", i, tc.name, err)
|
2020-09-04 10:49:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is exactly like the one above, but we set the codec explicitly to
|
|
|
|
// Amino.
|
|
|
|
// Once https://github.com/cosmos/cosmos-sdk/issues/6190 is in, we can remove
|
|
|
|
// this, since it'll be handled by the test matrix.
|
|
|
|
// In the meantime, we want to make double-sure amino compatibility works.
|
|
|
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7229
|
2021-10-01 07:30:22 -07:00
|
|
|
func (s *MWTestSuite) TestSigVerification_ExplicitAmino() {
|
|
|
|
ctx := s.SetupTest(true)
|
|
|
|
ctx = ctx.WithBlockHeight(1)
|
2020-09-04 10:49:07 -07:00
|
|
|
|
|
|
|
// Set up TxConfig.
|
2021-10-01 07:30:22 -07:00
|
|
|
aminoCdc := legacy.Cdc
|
|
|
|
aminoCdc.RegisterInterface((*sdk.Msg)(nil), nil)
|
|
|
|
aminoCdc.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil)
|
|
|
|
|
2020-09-04 10:49:07 -07:00
|
|
|
// We're using TestMsg amino encoding in some tests, so register it here.
|
2020-09-22 07:35:18 -07:00
|
|
|
txConfig := legacytx.StdTxConfig{Cdc: aminoCdc}
|
2020-09-04 10:49:07 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
s.clientCtx = client.Context{}.
|
2020-09-04 10:49:07 -07:00
|
|
|
WithTxConfig(txConfig)
|
|
|
|
|
|
|
|
// make block height non-zero to ensure account numbers part of signBytes
|
2021-10-01 07:30:22 -07:00
|
|
|
ctx = ctx.WithBlockHeight(1)
|
2020-09-04 10:49:07 -07:00
|
|
|
|
|
|
|
// keys and addresses
|
|
|
|
priv1, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
priv2, _, addr2 := testdata.KeyTestPubAddr()
|
|
|
|
priv3, _, addr3 := testdata.KeyTestPubAddr()
|
|
|
|
|
|
|
|
addrs := []sdk.AccAddress{addr1, addr2, addr3}
|
|
|
|
|
|
|
|
msgs := make([]sdk.Msg, len(addrs))
|
|
|
|
// set accounts and create msg for each address
|
|
|
|
for i, addr := range addrs {
|
2021-10-01 07:30:22 -07:00
|
|
|
acc := s.app.AccountKeeper.NewAccountWithAddress(ctx, addr)
|
|
|
|
s.Require().NoError(acc.SetAccountNumber(uint64(i)))
|
|
|
|
s.app.AccountKeeper.SetAccount(ctx, acc)
|
2020-07-06 10:03:45 -07:00
|
|
|
msgs[i] = testdata.NewTestMsg(addr)
|
2019-10-10 05:46:09 -07:00
|
|
|
}
|
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
|
|
gasLimit := testdata.NewTestGasLimit()
|
2019-10-10 05:46:09 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
txHandler := middleware.ComposeMiddlewares(
|
2021-12-06 01:26:15 -08:00
|
|
|
noopTxHandler,
|
2021-10-01 07:30:22 -07:00
|
|
|
middleware.SetPubKeyMiddleware(s.app.AccountKeeper),
|
|
|
|
middleware.SigVerificationMiddleware(
|
|
|
|
s.app.AccountKeeper,
|
|
|
|
s.clientCtx.TxConfig.SignModeHandler(),
|
|
|
|
),
|
|
|
|
)
|
2019-10-10 05:46:09 -07:00
|
|
|
|
2019-10-23 07:43:58 -07:00
|
|
|
type testCase struct {
|
|
|
|
name string
|
2020-11-09 08:01:43 -08:00
|
|
|
privs []cryptotypes.PrivKey
|
2019-10-23 07:43:58 -07:00
|
|
|
accNums []uint64
|
2020-07-20 05:30:12 -07:00
|
|
|
accSeqs []uint64
|
2019-10-23 07:43:58 -07:00
|
|
|
recheck bool
|
|
|
|
shouldErr bool
|
|
|
|
}
|
2021-10-01 07:30:22 -07:00
|
|
|
|
2019-10-23 07:43:58 -07:00
|
|
|
testCases := []testCase{
|
2020-11-09 08:01:43 -08:00
|
|
|
{"no signers", []cryptotypes.PrivKey{}, []uint64{}, []uint64{}, false, true},
|
|
|
|
{"not enough signers", []cryptotypes.PrivKey{priv1, priv2}, []uint64{0, 1}, []uint64{0, 0}, false, true},
|
|
|
|
{"wrong order signers", []cryptotypes.PrivKey{priv3, priv2, priv1}, []uint64{2, 1, 0}, []uint64{0, 0, 0}, false, true},
|
|
|
|
{"wrong accnums", []cryptotypes.PrivKey{priv1, priv2, priv3}, []uint64{7, 8, 9}, []uint64{0, 0, 0}, false, true},
|
|
|
|
{"wrong sequences", []cryptotypes.PrivKey{priv1, priv2, priv3}, []uint64{0, 1, 2}, []uint64{3, 4, 5}, false, true},
|
|
|
|
{"valid tx", []cryptotypes.PrivKey{priv1, priv2, priv3}, []uint64{0, 1, 2}, []uint64{0, 0, 0}, false, false},
|
|
|
|
{"no err on recheck", []cryptotypes.PrivKey{}, []uint64{}, []uint64{}, true, false},
|
2019-10-23 07:43:58 -07:00
|
|
|
}
|
2021-10-01 07:30:22 -07:00
|
|
|
|
2019-10-23 07:43:58 -07:00
|
|
|
for i, tc := range testCases {
|
2021-10-01 07:30:22 -07:00
|
|
|
ctx = ctx.WithIsReCheckTx(tc.recheck)
|
|
|
|
txBuilder := s.clientCtx.TxConfig.NewTxBuilder() // Create new txBuilder for each test
|
2019-10-23 07:43:58 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().NoError(txBuilder.SetMsgs(msgs...))
|
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
txBuilder.SetGasLimit(gasLimit)
|
2019-10-23 07:43:58 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
testTx, _, err := s.createTestTx(txBuilder, tc.privs, tc.accNums, tc.accSeqs, ctx.ChainID())
|
|
|
|
s.Require().NoError(err)
|
2020-07-20 05:30:12 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
if tc.recheck {
|
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.CheckTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}, tx.RequestCheckTx{Type: abci.CheckTxType_Recheck})
|
2021-10-01 07:30:22 -07:00
|
|
|
} else {
|
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.CheckTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx}, tx.RequestCheckTx{})
|
2021-10-01 07:30:22 -07:00
|
|
|
}
|
2019-10-23 07:43:58 -07:00
|
|
|
if tc.shouldErr {
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().NotNil(err, "TestCase %d: %s did not error as expected", i, tc.name)
|
2019-10-23 07:43:58 -07:00
|
|
|
} else {
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().Nil(err, "TestCase %d: %s errored unexpectedly. Err: %v", i, tc.name, err)
|
2019-10-23 07:43:58 -07:00
|
|
|
}
|
|
|
|
}
|
2019-10-10 05:46:09 -07:00
|
|
|
}
|
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
func (s *MWTestSuite) TestSigIntegration() {
|
2019-10-10 05:46:09 -07:00
|
|
|
// generate private keys
|
2020-11-09 08:01:43 -08:00
|
|
|
privs := []cryptotypes.PrivKey{
|
2020-09-21 09:48:28 -07:00
|
|
|
secp256k1.GenPrivKey(),
|
|
|
|
secp256k1.GenPrivKey(),
|
|
|
|
secp256k1.GenPrivKey(),
|
|
|
|
}
|
2019-10-10 05:46:09 -07:00
|
|
|
|
|
|
|
params := types.DefaultParams()
|
|
|
|
initialSigCost := params.SigVerifyCostSecp256k1
|
2021-10-01 07:30:22 -07:00
|
|
|
initialCost, err := s.runSigMiddlewares(params, false, privs...)
|
|
|
|
s.Require().Nil(err)
|
2019-10-10 05:46:09 -07:00
|
|
|
|
|
|
|
params.SigVerifyCostSecp256k1 *= 2
|
2021-10-01 07:30:22 -07:00
|
|
|
doubleCost, err := s.runSigMiddlewares(params, false, privs...)
|
|
|
|
s.Require().Nil(err)
|
2019-10-10 05:46:09 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().Equal(initialSigCost*uint64(len(privs)), doubleCost-initialCost)
|
2019-10-10 05:46:09 -07:00
|
|
|
}
|
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
func (s *MWTestSuite) runSigMiddlewares(params types.Params, _ bool, privs ...cryptotypes.PrivKey) (sdk.Gas, error) {
|
|
|
|
ctx := s.SetupTest(true) // setup
|
|
|
|
txBuilder := s.clientCtx.TxConfig.NewTxBuilder()
|
2020-07-20 05:30:12 -07:00
|
|
|
|
2019-10-10 05:46:09 -07:00
|
|
|
// Make block-height non-zero to include accNum in SignBytes
|
2021-10-01 07:30:22 -07:00
|
|
|
ctx = ctx.WithBlockHeight(1)
|
|
|
|
s.app.AccountKeeper.SetParams(ctx, params)
|
2019-10-10 05:46:09 -07:00
|
|
|
|
|
|
|
msgs := make([]sdk.Msg, len(privs))
|
|
|
|
accNums := make([]uint64, len(privs))
|
2020-07-20 05:30:12 -07:00
|
|
|
accSeqs := make([]uint64, len(privs))
|
2019-10-10 05:46:09 -07:00
|
|
|
// set accounts and create msg for each address
|
|
|
|
for i, priv := range privs {
|
|
|
|
addr := sdk.AccAddress(priv.PubKey().Address())
|
2021-10-01 07:30:22 -07:00
|
|
|
acc := s.app.AccountKeeper.NewAccountWithAddress(ctx, addr)
|
|
|
|
s.Require().NoError(acc.SetAccountNumber(uint64(i)))
|
|
|
|
s.app.AccountKeeper.SetAccount(ctx, acc)
|
2020-07-06 10:03:45 -07:00
|
|
|
msgs[i] = testdata.NewTestMsg(addr)
|
2019-10-10 05:46:09 -07:00
|
|
|
accNums[i] = uint64(i)
|
2020-07-20 05:30:12 -07:00
|
|
|
accSeqs[i] = uint64(0)
|
2019-10-10 05:46:09 -07:00
|
|
|
}
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().NoError(txBuilder.SetMsgs(msgs...))
|
2019-10-10 05:46:09 -07:00
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
|
|
gasLimit := testdata.NewTestGasLimit()
|
2021-10-01 07:30:22 -07:00
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
txBuilder.SetGasLimit(gasLimit)
|
|
|
|
|
|
|
|
testTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
txHandler := middleware.ComposeMiddlewares(
|
2021-12-06 01:26:15 -08:00
|
|
|
noopTxHandler,
|
2021-10-01 07:30:22 -07:00
|
|
|
middleware.SetPubKeyMiddleware(s.app.AccountKeeper),
|
|
|
|
middleware.SigGasConsumeMiddleware(s.app.AccountKeeper, middleware.DefaultSigVerificationGasConsumer),
|
|
|
|
middleware.SigVerificationMiddleware(
|
|
|
|
s.app.AccountKeeper,
|
|
|
|
s.clientCtx.TxConfig.SignModeHandler(),
|
|
|
|
),
|
|
|
|
)
|
2019-10-10 05:46:09 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
// Determine gas consumption of txhandler with default params
|
|
|
|
before := 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})
|
2019-10-10 05:46:09 -07:00
|
|
|
after := ctx.GasMeter().GasConsumed()
|
|
|
|
|
|
|
|
return after - before, err
|
|
|
|
}
|
2020-04-07 11:32:37 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
func (s *MWTestSuite) TestIncrementSequenceMiddleware() {
|
|
|
|
ctx := s.SetupTest(true) // setup
|
|
|
|
txBuilder := s.clientCtx.TxConfig.NewTxBuilder()
|
2020-04-07 11:32:37 -07:00
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
priv, _, addr := testdata.KeyTestPubAddr()
|
2021-10-01 07:30:22 -07:00
|
|
|
acc := s.app.AccountKeeper.NewAccountWithAddress(ctx, addr)
|
|
|
|
s.Require().NoError(acc.SetAccountNumber(uint64(50)))
|
|
|
|
s.app.AccountKeeper.SetAccount(ctx, acc)
|
2020-04-07 11:32:37 -07:00
|
|
|
|
2020-07-06 10:03:45 -07:00
|
|
|
msgs := []sdk.Msg{testdata.NewTestMsg(addr)}
|
2021-10-01 07:30:22 -07:00
|
|
|
s.Require().NoError(txBuilder.SetMsgs(msgs...))
|
2020-11-09 08:01:43 -08:00
|
|
|
privs := []cryptotypes.PrivKey{priv}
|
2021-10-01 07:30:22 -07:00
|
|
|
accNums := []uint64{s.app.AccountKeeper.GetAccount(ctx, addr).GetAccountNumber()}
|
|
|
|
accSeqs := []uint64{s.app.AccountKeeper.GetAccount(ctx, addr).GetSequence()}
|
2020-07-20 05:30:12 -07:00
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
|
|
gasLimit := testdata.NewTestGasLimit()
|
2021-10-01 07:30:22 -07:00
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
txBuilder.SetGasLimit(gasLimit)
|
2020-07-20 05:30:12 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
testTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
|
|
|
|
s.Require().NoError(err)
|
2020-07-20 05:30:12 -07:00
|
|
|
|
2021-10-01 07:30:22 -07:00
|
|
|
txHandler := middleware.ComposeMiddlewares(
|
2021-12-06 01:26:15 -08:00
|
|
|
noopTxHandler,
|
2021-10-01 07:30:22 -07:00
|
|
|
middleware.IncrementSequenceMiddleware(s.app.AccountKeeper),
|
|
|
|
)
|
2020-04-07 11:32:37 -07:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
ctx sdk.Context
|
|
|
|
simulate bool
|
|
|
|
expectedSeq uint64
|
|
|
|
}{
|
2021-10-01 07:30:22 -07:00
|
|
|
{ctx.WithIsReCheckTx(true), false, 1},
|
|
|
|
{ctx.WithIsCheckTx(true).WithIsReCheckTx(false), false, 2},
|
|
|
|
{ctx.WithIsReCheckTx(true), false, 3},
|
|
|
|
{ctx.WithIsReCheckTx(true), false, 4},
|
|
|
|
{ctx.WithIsReCheckTx(true), true, 5},
|
2020-04-07 11:32:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2021-10-01 07:30:22 -07:00
|
|
|
var err error
|
|
|
|
if tc.simulate {
|
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(tc.ctx), tx.Request{Tx: testTx})
|
2021-10-01 07:30:22 -07:00
|
|
|
} else {
|
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(tc.ctx), tx.Request{Tx: testTx})
|
2021-10-01 07:30:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().NoError(err, "unexpected error; tc #%d, %v", i, tc)
|
|
|
|
s.Require().Equal(tc.expectedSeq, s.app.AccountKeeper.GetAccount(ctx, addr).GetSequence())
|
2020-04-07 11:32:37 -07:00
|
|
|
}
|
|
|
|
}
|