2020-07-06 10:03:45 -07:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
2020-10-15 06:07:59 -07:00
|
|
|
"fmt"
|
2020-09-10 11:26:47 -07:00
|
|
|
|
2020-07-06 10:03:45 -07:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2020-11-09 08:01:43 -08:00
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2020-07-06 10:03:45 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-09-10 11:26:47 -07:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-07-06 10:03:45 -07:00
|
|
|
)
|
|
|
|
|
2020-09-10 11:26:47 -07:00
|
|
|
// MaxGasWanted defines the max gas allowed.
|
|
|
|
const MaxGasWanted = uint64((1 << 63) - 1)
|
|
|
|
|
2020-09-21 09:48:28 -07:00
|
|
|
// Interface implementation checks.
|
|
|
|
var _, _, _, _ codectypes.UnpackInterfacesMessage = &Tx{}, &TxBody{}, &AuthInfo{}, &SignerInfo{}
|
2020-09-10 11:26:47 -07:00
|
|
|
var _ sdk.Tx = &Tx{}
|
|
|
|
|
|
|
|
// GetMsgs implements the GetMsgs method on sdk.Tx.
|
|
|
|
func (t *Tx) GetMsgs() []sdk.Msg {
|
|
|
|
if t == nil || t.Body == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
anys := t.Body.Messages
|
feat: Add group module proto definitions and basic types (#9631)
<!--
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
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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-07-09 00:36:16 -07:00
|
|
|
res, err := GetMsgs(anys, "transaction")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-09-10 11:26:47 -07:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateBasic implements the ValidateBasic method on sdk.Tx.
|
|
|
|
func (t *Tx) ValidateBasic() error {
|
|
|
|
if t == nil {
|
|
|
|
return fmt.Errorf("bad Tx")
|
|
|
|
}
|
|
|
|
|
|
|
|
body := t.Body
|
|
|
|
if body == nil {
|
|
|
|
return fmt.Errorf("missing TxBody")
|
|
|
|
}
|
|
|
|
|
|
|
|
authInfo := t.AuthInfo
|
|
|
|
if authInfo == nil {
|
|
|
|
return fmt.Errorf("missing AuthInfo")
|
|
|
|
}
|
|
|
|
|
|
|
|
fee := authInfo.Fee
|
|
|
|
if fee == nil {
|
|
|
|
return fmt.Errorf("missing fee")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fee.GasLimit > MaxGasWanted {
|
|
|
|
return sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrInvalidRequest,
|
|
|
|
"invalid gas supplied; %d > %d", fee.GasLimit, MaxGasWanted,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-12 02:11:14 -07:00
|
|
|
if fee.Amount.IsAnyNil() {
|
|
|
|
return sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrInsufficientFee,
|
|
|
|
"invalid fee provided: null",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:26:47 -07:00
|
|
|
if fee.Amount.IsAnyNegative() {
|
|
|
|
return sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrInsufficientFee,
|
|
|
|
"invalid fee provided: %s", fee.Amount,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-30 02:03:44 -07:00
|
|
|
if fee.Payer != "" {
|
|
|
|
_, err := sdk.AccAddressFromBech32(fee.Payer)
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid fee payer address (%s)", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:26:47 -07:00
|
|
|
sigs := t.Signatures
|
|
|
|
|
|
|
|
if len(sigs) == 0 {
|
|
|
|
return sdkerrors.ErrNoSignatures
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sigs) != len(t.GetSigners()) {
|
|
|
|
return sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrUnauthorized,
|
2020-11-05 17:25:38 -08:00
|
|
|
"wrong number of signers; expected %d, got %d", len(t.GetSigners()), len(sigs),
|
2020-09-10 11:26:47 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSigners retrieves all the signers of a tx.
|
2020-09-30 02:03:44 -07:00
|
|
|
// This includes all unique signers of the messages (in order),
|
|
|
|
// as well as the FeePayer (if specified and not already included).
|
2020-09-10 11:26:47 -07:00
|
|
|
func (t *Tx) GetSigners() []sdk.AccAddress {
|
|
|
|
var signers []sdk.AccAddress
|
|
|
|
seen := map[string]bool{}
|
|
|
|
|
|
|
|
for _, msg := range t.GetMsgs() {
|
|
|
|
for _, addr := range msg.GetSigners() {
|
2021-08-13 08:34:00 -07:00
|
|
|
if !seen[addr.String()] {
|
|
|
|
signers = append(signers, addr)
|
|
|
|
seen[addr.String()] = true
|
2020-09-10 11:26:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 02:03:44 -07:00
|
|
|
// ensure any specified fee payer is included in the required signers (at the end)
|
|
|
|
feePayer := t.AuthInfo.Fee.Payer
|
|
|
|
if feePayer != "" && !seen[feePayer] {
|
|
|
|
payerAddr, err := sdk.AccAddressFromBech32(feePayer)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
signers = append(signers, payerAddr)
|
|
|
|
seen[feePayer] = true
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:26:47 -07:00
|
|
|
return signers
|
|
|
|
}
|
2020-07-06 10:03:45 -07:00
|
|
|
|
2021-01-21 01:33:02 -08:00
|
|
|
func (t *Tx) GetGas() uint64 {
|
|
|
|
return t.AuthInfo.Fee.GasLimit
|
|
|
|
}
|
|
|
|
func (t *Tx) GetFee() sdk.Coins {
|
|
|
|
return t.AuthInfo.Fee.Amount
|
|
|
|
}
|
|
|
|
func (t *Tx) FeePayer() sdk.AccAddress {
|
|
|
|
feePayer := t.AuthInfo.Fee.Payer
|
|
|
|
if feePayer != "" {
|
|
|
|
payerAddr, err := sdk.AccAddressFromBech32(feePayer)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return payerAddr
|
|
|
|
}
|
|
|
|
// use first signer as default if no payer specified
|
|
|
|
return t.GetSigners()[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tx) FeeGranter() sdk.AccAddress {
|
|
|
|
feePayer := t.AuthInfo.Fee.Granter
|
|
|
|
if feePayer != "" {
|
|
|
|
granterAddr, err := sdk.AccAddressFromBech32(feePayer)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return granterAddr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-06 10:03:45 -07:00
|
|
|
// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
|
2020-09-10 11:26:47 -07:00
|
|
|
func (t *Tx) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
|
|
|
if t.Body != nil {
|
2020-09-21 09:48:28 -07:00
|
|
|
if err := t.Body.UnpackInterfaces(unpacker); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.AuthInfo != nil {
|
|
|
|
return t.AuthInfo.UnpackInterfaces(unpacker)
|
2020-07-06 10:03:45 -07:00
|
|
|
}
|
2020-09-21 09:48:28 -07:00
|
|
|
|
2020-07-06 10:03:45 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
|
|
|
|
func (m *TxBody) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
2022-03-29 07:46:56 -07:00
|
|
|
if err := UnpackInterfaces(unpacker, m.Messages); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := unpackTxExtensionOptionsI(unpacker, m.ExtensionOptions); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := unpackTxExtensionOptionsI(unpacker, m.NonCriticalExtensionOptions); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-07-06 10:03:45 -07:00
|
|
|
}
|
2020-09-10 11:26:47 -07:00
|
|
|
|
2020-09-21 09:48:28 -07:00
|
|
|
// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
|
|
|
|
func (m *AuthInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
|
|
|
for _, signerInfo := range m.SignerInfos {
|
|
|
|
err := signerInfo.UnpackInterfaces(unpacker)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method
|
|
|
|
func (m *SignerInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
2020-11-09 08:01:43 -08:00
|
|
|
return unpacker.UnpackAny(m.PublicKey, new(cryptotypes.PubKey))
|
2020-09-21 09:48:28 -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
|
|
|
// RegisterInterfaces registers the sdk.Tx and MsgResponse interfaces.
|
|
|
|
// Note: the registration of sdk.Msg is done in sdk.RegisterInterfaces, but it
|
|
|
|
// could be moved inside this function.
|
2020-09-10 11:26:47 -07:00
|
|
|
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
|
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
|
|
|
registry.RegisterInterface(msgResponseInterfaceProtoName, (*MsgResponse)(nil))
|
|
|
|
|
2020-09-10 11:26:47 -07:00
|
|
|
registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.Tx)(nil))
|
|
|
|
registry.RegisterImplementations((*sdk.Tx)(nil), &Tx{})
|
2022-03-29 07:46:56 -07:00
|
|
|
|
|
|
|
registry.RegisterInterface("cosmos.tx.v1beta1.TxExtensionOptionI", (*TxExtensionOptionI)(nil))
|
2020-09-10 11:26:47 -07:00
|
|
|
}
|