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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-07 03:18:00 -07:00
|
|
|
if !seen[addr] {
|
|
|
|
signer, err := sdk.AccAddressFromBech32(addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signers = append(signers, signer)
|
|
|
|
seen[addr] = 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 {
|
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
|
|
|
return UnpackInterfaces(unpacker, m.Messages)
|
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
|
|
|
}
|
|
|
|
|
2020-09-10 11:26:47 -07:00
|
|
|
// RegisterInterfaces registers the sdk.Tx interface.
|
|
|
|
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
|
|
|
|
registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.Tx)(nil))
|
|
|
|
registry.RegisterImplementations((*sdk.Tx)(nil), &Tx{})
|
|
|
|
}
|
2021-07-07 03:18:00 -07:00
|
|
|
|
|
|
|
// ValidateMsg calls the `sdk.Msg.ValidateBasic()`
|
|
|
|
// also validates all the signers are valid bech32 addresses.
|
|
|
|
func ValidateMsg(msg sdk.Msg) error {
|
|
|
|
err := msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
signers := msg.GetSigners()
|
|
|
|
for _, signer := range signers {
|
|
|
|
_, err = sdk.AccAddressFromBech32(signer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|