2020-07-06 10:03:45 -07:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-08-14 10:58:53 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec/legacy"
|
2020-07-06 10:03:45 -07:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2020-07-29 08:52:22 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2020-07-06 10:03:45 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-07-29 08:52:22 -07:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
2020-07-06 10:03:45 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTxBuilder(t *testing.T) {
|
2020-07-20 05:30:12 -07:00
|
|
|
_, pubkey, addr := testdata.KeyTestPubAddr()
|
2020-07-06 10:03:45 -07:00
|
|
|
|
2020-07-30 11:58:26 -07:00
|
|
|
marshaler := codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
|
feat: Add AuxTxBuilder (#10455)
<!--
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: #10443
For creating an intermediary/auxiliary tx (e.g. by the tipper in tipped transactions), using the existing `client.TxBuilder` is awkward. We propose a new client-side builder for this purpose.
API Usage (e.g. how the tipper would programmtically use this):
```go
// Note: there's no need to use clientCtx.TxConfig anymore!
bldr := clienttx.NewAuxTxBuilder()
err := bldr.SetMsgs(msgs...)
bldr.SetAddress("cosmos1...")
bldr.SetMemo(...)
bldr.SetTip(...)
bldr.SetPubKey(...)
err := bldr.SetSignMode(...) // DIRECT_AUX or AMINO, or else error
// ... other setters are available
// Get the bytes to sign.
signBz, err := bldr.GetSignBytes()
// Sign the bz using your favorite method.
sig, err := privKey.sign(signBz)
// Set the signature
bldr.SetSig(sig)
// Get the final auxSignerData to be sent to the fee payer
auxSignerData, err:= bldr.GetAuxSignerData()
```
auxSignerData is a protobuf message, whose JSON reprensentation looks like:
```json
{
"address": "cosmos1...",
"mode": "SIGN_MODE_{DIRECT_AUX,LEGACY_AMINO_JSON}",
"sign_doc": {
"body_bytes": "{base64 bytes}",
"public_key": {
"@type": "cosmos.sepc256k1.PubKey",
"key": "{base64 bytes}"
},
"chain_id": "...",
"account_number": 24,
"sequence": 42,
"tip": {
"amount": [{ "denom": "uregen", "amount": 1000 }],
"tipper": "cosmos1..."
}
},
"sig": "{base64 bytes}"
}
```
Then the fee payer would use the TxBuilder to construct the final TX, with a new helper method: `AddAuxSignerData`:
```go
// get auxSignerData from AuxTxBuilder
auxSignerData := ...
txBuilder := txConfig.NewTxBuilder()
err := txBuilder.AddAuxSignerData(auxSignerData)
// Set fee payer data
txBuilder.SetFee()
txBuilder.SetGasLimit()
txBuilder.SetFeePayer()
sigs, err := txBuilder.GetSignaturesV2()
auxSig := sigs[0] // the aux signer's signature
// Set all signer infos (1st round of calling SetSignatures)
txBuilder.SetSignatures(
auxSig, // The aux SignatureV2
signing.SignatureV2{...}, // The feePayer's SignatureV2
)
// Sign
signBz, err = encCfg.TxConfig.SignModeHandler().GetSignBytes(...)
feepayerSig, err := feepayerPriv.Sign(signBz)
// Set all signatures (2nd round of calling SetSignatures)
txBuilder.SetSignatures(
auxSig, // The aux SignatureV2
signing.SignatureV2{feepayerSig, ...}, // The feePayer's SignatureV2
)
```
---
### 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-11-11 03:25:13 -08:00
|
|
|
txBuilder := newBuilder(nil)
|
2020-07-06 10:03:45 -07:00
|
|
|
|
|
|
|
memo := "sometestmemo"
|
|
|
|
msgs := []sdk.Msg{testdata.NewTestMsg(addr)}
|
2020-08-21 07:20:47 -07:00
|
|
|
accSeq := uint64(2) // Arbitrary account sequence
|
2020-12-18 06:55:25 -08:00
|
|
|
any, err := codectypes.NewAnyWithValue(pubkey)
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-29 08:52:22 -07:00
|
|
|
var signerInfo []*txtypes.SignerInfo
|
|
|
|
signerInfo = append(signerInfo, &txtypes.SignerInfo{
|
2020-09-21 09:48:28 -07:00
|
|
|
PublicKey: any,
|
2020-07-29 08:52:22 -07:00
|
|
|
ModeInfo: &txtypes.ModeInfo{
|
|
|
|
Sum: &txtypes.ModeInfo_Single_{
|
|
|
|
Single: &txtypes.ModeInfo_Single{
|
2020-07-06 10:03:45 -07:00
|
|
|
Mode: signing.SignMode_SIGN_MODE_DIRECT,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-21 07:20:47 -07:00
|
|
|
Sequence: accSeq,
|
2020-07-06 10:03:45 -07:00
|
|
|
})
|
|
|
|
|
2021-03-22 02:30:27 -07:00
|
|
|
var sig signing.SignatureV2 = signing.SignatureV2{
|
2020-07-06 10:03:45 -07:00
|
|
|
PubKey: pubkey,
|
|
|
|
Data: &signing.SingleSignatureData{
|
|
|
|
SignMode: signing.SignMode_SIGN_MODE_DIRECT,
|
2021-04-29 03:46:22 -07:00
|
|
|
Signature: legacy.Cdc.MustMarshal(pubkey),
|
2020-07-06 10:03:45 -07:00
|
|
|
},
|
2020-08-21 07:20:47 -07:00
|
|
|
Sequence: accSeq,
|
2020-07-06 10:03:45 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 08:52:22 -07:00
|
|
|
fee := txtypes.Fee{Amount: sdk.NewCoins(sdk.NewInt64Coin("atom", 150)), GasLimit: 20000}
|
2020-07-06 10:03:45 -07:00
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
t.Log("verify that authInfo bytes encoded with DefaultTxEncoder and decoded with DefaultTxDecoder can be retrieved from getAuthInfoBytes")
|
2020-07-29 08:52:22 -07:00
|
|
|
authInfo := &txtypes.AuthInfo{
|
2020-07-06 10:03:45 -07:00
|
|
|
Fee: &fee,
|
|
|
|
SignerInfos: signerInfo,
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
authInfoBytes := marshaler.MustMarshal(authInfo)
|
2020-07-06 10:03:45 -07:00
|
|
|
|
|
|
|
require.NotEmpty(t, authInfoBytes)
|
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
t.Log("verify that body bytes encoded with DefaultTxEncoder and decoded with DefaultTxDecoder can be retrieved from getBodyBytes")
|
2020-07-06 10:03:45 -07:00
|
|
|
anys := make([]*codectypes.Any, len(msgs))
|
|
|
|
|
|
|
|
for i, msg := range msgs {
|
|
|
|
var err error
|
|
|
|
anys[i], err = codectypes.NewAnyWithValue(msg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 08:52:22 -07:00
|
|
|
txBody := &txtypes.TxBody{
|
2020-07-06 10:03:45 -07:00
|
|
|
Memo: memo,
|
|
|
|
Messages: anys,
|
|
|
|
}
|
2021-04-29 03:46:22 -07:00
|
|
|
bodyBytes := marshaler.MustMarshal(txBody)
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NotEmpty(t, bodyBytes)
|
2020-08-03 12:47:25 -07:00
|
|
|
require.Empty(t, txBuilder.getBodyBytes())
|
2020-07-06 10:03:45 -07:00
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
t.Log("verify that calling the SetMsgs, SetMemo results in the correct getBodyBytes")
|
|
|
|
require.NotEqual(t, bodyBytes, txBuilder.getBodyBytes())
|
2020-07-29 08:52:22 -07:00
|
|
|
err = txBuilder.SetMsgs(msgs...)
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-03 12:47:25 -07:00
|
|
|
require.NotEqual(t, bodyBytes, txBuilder.getBodyBytes())
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.SetMemo(memo)
|
2020-08-03 12:47:25 -07:00
|
|
|
require.Equal(t, bodyBytes, txBuilder.getBodyBytes())
|
2020-07-29 08:52:22 -07:00
|
|
|
require.Equal(t, len(msgs), len(txBuilder.GetMsgs()))
|
2021-03-09 23:16:04 -08:00
|
|
|
pks, err := txBuilder.GetPubKeys()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, pks)
|
2020-07-06 10:03:45 -07:00
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
t.Log("verify that updated AuthInfo results in the correct getAuthInfoBytes and GetPubKeys")
|
|
|
|
require.NotEqual(t, authInfoBytes, txBuilder.getAuthInfoBytes())
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.SetFeeAmount(fee.Amount)
|
2020-08-03 12:47:25 -07:00
|
|
|
require.NotEqual(t, authInfoBytes, txBuilder.getAuthInfoBytes())
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.SetGasLimit(fee.GasLimit)
|
2020-08-03 12:47:25 -07:00
|
|
|
require.NotEqual(t, authInfoBytes, txBuilder.getAuthInfoBytes())
|
2020-07-29 08:52:22 -07:00
|
|
|
err = txBuilder.SetSignatures(sig)
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// once fee, gas and signerInfos are all set, AuthInfo bytes should match
|
2020-08-03 12:47:25 -07:00
|
|
|
require.Equal(t, authInfoBytes, txBuilder.getAuthInfoBytes())
|
2020-07-06 10:03:45 -07:00
|
|
|
|
2020-07-29 08:52:22 -07:00
|
|
|
require.Equal(t, len(msgs), len(txBuilder.GetMsgs()))
|
2021-03-09 23:16:04 -08:00
|
|
|
pks, err = txBuilder.GetPubKeys()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(pks))
|
|
|
|
require.True(t, pubkey.Equals(pks[0]))
|
2020-07-27 08:41:20 -07:00
|
|
|
|
2020-09-21 09:48:28 -07:00
|
|
|
any, err = codectypes.NewAnyWithValue(testdata.NewTestMsg())
|
2020-08-18 06:44:56 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
txBuilder.SetExtensionOptions(any)
|
|
|
|
require.Equal(t, []*codectypes.Any{any}, txBuilder.GetExtensionOptions())
|
|
|
|
txBuilder.SetNonCriticalExtensionOptions(any)
|
|
|
|
require.Equal(t, []*codectypes.Any{any}, txBuilder.GetNonCriticalExtensionOptions())
|
|
|
|
|
2020-08-24 07:41:08 -07:00
|
|
|
txBuilder = &wrapper{}
|
2020-07-27 08:41:20 -07:00
|
|
|
require.NotPanics(t, func() {
|
2020-07-29 08:52:22 -07:00
|
|
|
_ = txBuilder.GetMsgs()
|
2020-07-27 08:41:20 -07:00
|
|
|
})
|
2020-07-06 10:03:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderValidateBasic(t *testing.T) {
|
|
|
|
// keys and addresses
|
2020-07-20 05:30:12 -07:00
|
|
|
_, pubKey1, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
_, pubKey2, addr2 := testdata.KeyTestPubAddr()
|
2020-07-06 10:03:45 -07:00
|
|
|
|
|
|
|
// msg and signatures
|
|
|
|
msg1 := testdata.NewTestMsg(addr1, addr2)
|
2020-07-20 05:30:12 -07:00
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
2020-07-06 10:03:45 -07:00
|
|
|
msgs := []sdk.Msg{msg1}
|
|
|
|
|
|
|
|
// require to fail validation upon invalid fee
|
2020-07-20 05:30:12 -07:00
|
|
|
badFeeAmount := testdata.NewTestFeeAmount()
|
|
|
|
badFeeAmount[0].Amount = sdk.NewInt(-5)
|
feat: Add AuxTxBuilder (#10455)
<!--
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: #10443
For creating an intermediary/auxiliary tx (e.g. by the tipper in tipped transactions), using the existing `client.TxBuilder` is awkward. We propose a new client-side builder for this purpose.
API Usage (e.g. how the tipper would programmtically use this):
```go
// Note: there's no need to use clientCtx.TxConfig anymore!
bldr := clienttx.NewAuxTxBuilder()
err := bldr.SetMsgs(msgs...)
bldr.SetAddress("cosmos1...")
bldr.SetMemo(...)
bldr.SetTip(...)
bldr.SetPubKey(...)
err := bldr.SetSignMode(...) // DIRECT_AUX or AMINO, or else error
// ... other setters are available
// Get the bytes to sign.
signBz, err := bldr.GetSignBytes()
// Sign the bz using your favorite method.
sig, err := privKey.sign(signBz)
// Set the signature
bldr.SetSig(sig)
// Get the final auxSignerData to be sent to the fee payer
auxSignerData, err:= bldr.GetAuxSignerData()
```
auxSignerData is a protobuf message, whose JSON reprensentation looks like:
```json
{
"address": "cosmos1...",
"mode": "SIGN_MODE_{DIRECT_AUX,LEGACY_AMINO_JSON}",
"sign_doc": {
"body_bytes": "{base64 bytes}",
"public_key": {
"@type": "cosmos.sepc256k1.PubKey",
"key": "{base64 bytes}"
},
"chain_id": "...",
"account_number": 24,
"sequence": 42,
"tip": {
"amount": [{ "denom": "uregen", "amount": 1000 }],
"tipper": "cosmos1..."
}
},
"sig": "{base64 bytes}"
}
```
Then the fee payer would use the TxBuilder to construct the final TX, with a new helper method: `AddAuxSignerData`:
```go
// get auxSignerData from AuxTxBuilder
auxSignerData := ...
txBuilder := txConfig.NewTxBuilder()
err := txBuilder.AddAuxSignerData(auxSignerData)
// Set fee payer data
txBuilder.SetFee()
txBuilder.SetGasLimit()
txBuilder.SetFeePayer()
sigs, err := txBuilder.GetSignaturesV2()
auxSig := sigs[0] // the aux signer's signature
// Set all signer infos (1st round of calling SetSignatures)
txBuilder.SetSignatures(
auxSig, // The aux SignatureV2
signing.SignatureV2{...}, // The feePayer's SignatureV2
)
// Sign
signBz, err = encCfg.TxConfig.SignModeHandler().GetSignBytes(...)
feepayerSig, err := feepayerPriv.Sign(signBz)
// Set all signatures (2nd round of calling SetSignatures)
txBuilder.SetSignatures(
auxSig, // The aux SignatureV2
signing.SignatureV2{feepayerSig, ...}, // The feePayer's SignatureV2
)
```
---
### 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-11-11 03:25:13 -08:00
|
|
|
txBuilder := newBuilder(nil)
|
2020-07-06 10:03:45 -07:00
|
|
|
|
|
|
|
var sig1, sig2 signing.SignatureV2
|
|
|
|
sig1 = signing.SignatureV2{
|
|
|
|
PubKey: pubKey1,
|
|
|
|
Data: &signing.SingleSignatureData{
|
|
|
|
SignMode: signing.SignMode_SIGN_MODE_DIRECT,
|
2021-04-29 03:46:22 -07:00
|
|
|
Signature: legacy.Cdc.MustMarshal(pubKey1),
|
2020-07-06 10:03:45 -07:00
|
|
|
},
|
2020-08-21 07:20:47 -07:00
|
|
|
Sequence: 0, // Arbitrary account sequence
|
2020-07-06 10:03:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
sig2 = signing.SignatureV2{
|
|
|
|
PubKey: pubKey2,
|
|
|
|
Data: &signing.SingleSignatureData{
|
|
|
|
SignMode: signing.SignMode_SIGN_MODE_DIRECT,
|
2021-04-29 03:46:22 -07:00
|
|
|
Signature: legacy.Cdc.MustMarshal(pubKey2),
|
2020-07-06 10:03:45 -07:00
|
|
|
},
|
2020-08-21 07:20:47 -07:00
|
|
|
Sequence: 0, // Arbitrary account sequence
|
2020-07-06 10:03:45 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 08:52:22 -07:00
|
|
|
err := txBuilder.SetMsgs(msgs...)
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.SetGasLimit(200000)
|
|
|
|
err = txBuilder.SetSignatures(sig1, sig2)
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.SetFeeAmount(badFeeAmount)
|
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
_, code, _ := sdkerrors.ABCIInfo(err, false)
|
|
|
|
require.Equal(t, sdkerrors.ErrInsufficientFee.ABCICode(), code)
|
|
|
|
|
|
|
|
// require to fail validation when no signatures exist
|
2020-07-29 08:52:22 -07:00
|
|
|
err = txBuilder.SetSignatures()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
_, code, _ = sdkerrors.ABCIInfo(err, false)
|
|
|
|
require.Equal(t, sdkerrors.ErrNoSignatures.ABCICode(), code)
|
|
|
|
|
|
|
|
// require to fail with nil values for tx, authinfo
|
2020-07-29 08:52:22 -07:00
|
|
|
err = txBuilder.SetMsgs(msgs...)
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
2020-07-29 08:52:22 -07:00
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// require to fail validation when signatures do not match expected signers
|
2020-07-29 08:52:22 -07:00
|
|
|
err = txBuilder.SetSignatures(sig1)
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-29 08:52:22 -07:00
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
_, code, _ = sdkerrors.ABCIInfo(err, false)
|
|
|
|
require.Equal(t, sdkerrors.ErrUnauthorized.ABCICode(), code)
|
|
|
|
|
|
|
|
require.Error(t, err)
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
err = txBuilder.SetSignatures(sig1, sig2)
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
2020-07-29 08:52:22 -07:00
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// gas limit too high
|
2020-09-10 11:26:47 -07:00
|
|
|
txBuilder.SetGasLimit(txtypes.MaxGasWanted + 1)
|
2020-07-29 08:52:22 -07:00
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.Error(t, err)
|
2020-09-10 11:26:47 -07:00
|
|
|
txBuilder.SetGasLimit(txtypes.MaxGasWanted - 1)
|
2020-07-29 08:52:22 -07:00
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// bad builder structs
|
|
|
|
|
|
|
|
// missing body
|
2020-07-29 08:52:22 -07:00
|
|
|
body := txBuilder.tx.Body
|
|
|
|
txBuilder.tx.Body = nil
|
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.Error(t, err)
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.tx.Body = body
|
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// missing fee
|
2020-07-29 08:52:22 -07:00
|
|
|
f := txBuilder.tx.AuthInfo.Fee
|
|
|
|
txBuilder.tx.AuthInfo.Fee = nil
|
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.Error(t, err)
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.tx.AuthInfo.Fee = f
|
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// missing AuthInfo
|
2020-07-29 08:52:22 -07:00
|
|
|
authInfo := txBuilder.tx.AuthInfo
|
|
|
|
txBuilder.tx.AuthInfo = nil
|
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.Error(t, err)
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.tx.AuthInfo = authInfo
|
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// missing tx
|
2020-07-29 08:52:22 -07:00
|
|
|
txBuilder.tx = nil
|
|
|
|
err = txBuilder.ValidateBasic()
|
2020-07-06 10:03:45 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2020-09-30 02:03:44 -07:00
|
|
|
|
|
|
|
func TestBuilderFeePayer(t *testing.T) {
|
|
|
|
// keys and addresses
|
|
|
|
_, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
_, _, addr2 := testdata.KeyTestPubAddr()
|
|
|
|
_, _, addr3 := testdata.KeyTestPubAddr()
|
|
|
|
|
|
|
|
// msg and signatures
|
|
|
|
msg1 := testdata.NewTestMsg(addr1, addr2)
|
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
|
|
msgs := []sdk.Msg{msg1}
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
txFeePayer sdk.AccAddress
|
|
|
|
expectedSigners []sdk.AccAddress
|
|
|
|
expectedPayer sdk.AccAddress
|
|
|
|
}{
|
|
|
|
"no fee payer specified": {
|
|
|
|
expectedSigners: []sdk.AccAddress{addr1, addr2},
|
|
|
|
expectedPayer: addr1,
|
|
|
|
},
|
|
|
|
"secondary signer set as fee payer": {
|
|
|
|
txFeePayer: addr2,
|
|
|
|
expectedSigners: []sdk.AccAddress{addr1, addr2},
|
|
|
|
expectedPayer: addr2,
|
|
|
|
},
|
|
|
|
"outside signer set as fee payer": {
|
|
|
|
txFeePayer: addr3,
|
|
|
|
expectedSigners: []sdk.AccAddress{addr1, addr2, addr3},
|
|
|
|
expectedPayer: addr3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
// setup basic tx
|
feat: Add AuxTxBuilder (#10455)
<!--
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: #10443
For creating an intermediary/auxiliary tx (e.g. by the tipper in tipped transactions), using the existing `client.TxBuilder` is awkward. We propose a new client-side builder for this purpose.
API Usage (e.g. how the tipper would programmtically use this):
```go
// Note: there's no need to use clientCtx.TxConfig anymore!
bldr := clienttx.NewAuxTxBuilder()
err := bldr.SetMsgs(msgs...)
bldr.SetAddress("cosmos1...")
bldr.SetMemo(...)
bldr.SetTip(...)
bldr.SetPubKey(...)
err := bldr.SetSignMode(...) // DIRECT_AUX or AMINO, or else error
// ... other setters are available
// Get the bytes to sign.
signBz, err := bldr.GetSignBytes()
// Sign the bz using your favorite method.
sig, err := privKey.sign(signBz)
// Set the signature
bldr.SetSig(sig)
// Get the final auxSignerData to be sent to the fee payer
auxSignerData, err:= bldr.GetAuxSignerData()
```
auxSignerData is a protobuf message, whose JSON reprensentation looks like:
```json
{
"address": "cosmos1...",
"mode": "SIGN_MODE_{DIRECT_AUX,LEGACY_AMINO_JSON}",
"sign_doc": {
"body_bytes": "{base64 bytes}",
"public_key": {
"@type": "cosmos.sepc256k1.PubKey",
"key": "{base64 bytes}"
},
"chain_id": "...",
"account_number": 24,
"sequence": 42,
"tip": {
"amount": [{ "denom": "uregen", "amount": 1000 }],
"tipper": "cosmos1..."
}
},
"sig": "{base64 bytes}"
}
```
Then the fee payer would use the TxBuilder to construct the final TX, with a new helper method: `AddAuxSignerData`:
```go
// get auxSignerData from AuxTxBuilder
auxSignerData := ...
txBuilder := txConfig.NewTxBuilder()
err := txBuilder.AddAuxSignerData(auxSignerData)
// Set fee payer data
txBuilder.SetFee()
txBuilder.SetGasLimit()
txBuilder.SetFeePayer()
sigs, err := txBuilder.GetSignaturesV2()
auxSig := sigs[0] // the aux signer's signature
// Set all signer infos (1st round of calling SetSignatures)
txBuilder.SetSignatures(
auxSig, // The aux SignatureV2
signing.SignatureV2{...}, // The feePayer's SignatureV2
)
// Sign
signBz, err = encCfg.TxConfig.SignModeHandler().GetSignBytes(...)
feepayerSig, err := feepayerPriv.Sign(signBz)
// Set all signatures (2nd round of calling SetSignatures)
txBuilder.SetSignatures(
auxSig, // The aux SignatureV2
signing.SignatureV2{feepayerSig, ...}, // The feePayer's SignatureV2
)
```
---
### 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-11-11 03:25:13 -08:00
|
|
|
txBuilder := newBuilder(nil)
|
2020-09-30 02:03:44 -07:00
|
|
|
err := txBuilder.SetMsgs(msgs...)
|
|
|
|
require.NoError(t, err)
|
|
|
|
txBuilder.SetGasLimit(200000)
|
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
|
|
|
|
// set fee payer
|
|
|
|
txBuilder.SetFeePayer(tc.txFeePayer)
|
|
|
|
// and check it updates fields properly
|
|
|
|
require.Equal(t, tc.expectedSigners, txBuilder.GetSigners())
|
|
|
|
require.Equal(t, tc.expectedPayer, txBuilder.FeePayer())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 14:56:09 -07:00
|
|
|
|
|
|
|
func TestBuilderFeeGranter(t *testing.T) {
|
|
|
|
// keys and addresses
|
|
|
|
_, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
|
|
|
|
// msg and signatures
|
|
|
|
msg1 := testdata.NewTestMsg(addr1, addr2)
|
|
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
|
|
msgs := []sdk.Msg{msg1}
|
|
|
|
|
feat: Add AuxTxBuilder (#10455)
<!--
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: #10443
For creating an intermediary/auxiliary tx (e.g. by the tipper in tipped transactions), using the existing `client.TxBuilder` is awkward. We propose a new client-side builder for this purpose.
API Usage (e.g. how the tipper would programmtically use this):
```go
// Note: there's no need to use clientCtx.TxConfig anymore!
bldr := clienttx.NewAuxTxBuilder()
err := bldr.SetMsgs(msgs...)
bldr.SetAddress("cosmos1...")
bldr.SetMemo(...)
bldr.SetTip(...)
bldr.SetPubKey(...)
err := bldr.SetSignMode(...) // DIRECT_AUX or AMINO, or else error
// ... other setters are available
// Get the bytes to sign.
signBz, err := bldr.GetSignBytes()
// Sign the bz using your favorite method.
sig, err := privKey.sign(signBz)
// Set the signature
bldr.SetSig(sig)
// Get the final auxSignerData to be sent to the fee payer
auxSignerData, err:= bldr.GetAuxSignerData()
```
auxSignerData is a protobuf message, whose JSON reprensentation looks like:
```json
{
"address": "cosmos1...",
"mode": "SIGN_MODE_{DIRECT_AUX,LEGACY_AMINO_JSON}",
"sign_doc": {
"body_bytes": "{base64 bytes}",
"public_key": {
"@type": "cosmos.sepc256k1.PubKey",
"key": "{base64 bytes}"
},
"chain_id": "...",
"account_number": 24,
"sequence": 42,
"tip": {
"amount": [{ "denom": "uregen", "amount": 1000 }],
"tipper": "cosmos1..."
}
},
"sig": "{base64 bytes}"
}
```
Then the fee payer would use the TxBuilder to construct the final TX, with a new helper method: `AddAuxSignerData`:
```go
// get auxSignerData from AuxTxBuilder
auxSignerData := ...
txBuilder := txConfig.NewTxBuilder()
err := txBuilder.AddAuxSignerData(auxSignerData)
// Set fee payer data
txBuilder.SetFee()
txBuilder.SetGasLimit()
txBuilder.SetFeePayer()
sigs, err := txBuilder.GetSignaturesV2()
auxSig := sigs[0] // the aux signer's signature
// Set all signer infos (1st round of calling SetSignatures)
txBuilder.SetSignatures(
auxSig, // The aux SignatureV2
signing.SignatureV2{...}, // The feePayer's SignatureV2
)
// Sign
signBz, err = encCfg.TxConfig.SignModeHandler().GetSignBytes(...)
feepayerSig, err := feepayerPriv.Sign(signBz)
// Set all signatures (2nd round of calling SetSignatures)
txBuilder.SetSignatures(
auxSig, // The aux SignatureV2
signing.SignatureV2{feepayerSig, ...}, // The feePayer's SignatureV2
)
```
---
### 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-11-11 03:25:13 -08:00
|
|
|
txBuilder := newBuilder(nil)
|
2020-10-05 14:56:09 -07:00
|
|
|
err := txBuilder.SetMsgs(msgs...)
|
|
|
|
require.NoError(t, err)
|
|
|
|
txBuilder.SetGasLimit(200000)
|
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
|
|
|
|
require.Empty(t, txBuilder.GetTx().FeeGranter())
|
|
|
|
|
|
|
|
// set fee granter
|
|
|
|
txBuilder.SetFeeGranter(addr1)
|
|
|
|
require.Equal(t, addr1, txBuilder.GetTx().FeeGranter())
|
|
|
|
}
|