refactor!: deprecate sdk.Msg.GetSigners (#15284)

Co-authored-by: Matt Kocubinski <mkocubinski@gmail.com>
This commit is contained in:
Aaron Craelius
2023-05-25 18:35:09 +00:00
committed by GitHub
co-authored by Matt Kocubinski
parent b6613f9163
commit 82659a7477
60 changed files with 927 additions and 298 deletions
+8 -2
View File
@@ -8,8 +8,10 @@ import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
protov2 "google.golang.org/protobuf/proto"
"cosmossdk.io/log"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/mempool"
@@ -52,7 +54,7 @@ type testTx struct {
strAddress string
}
func (tx testTx) GetSigners() []sdk.AccAddress { panic("not implemented") }
func (tx testTx) GetSigners() ([][]byte, error) { panic("not implemented") }
func (tx testTx) GetPubKeys() ([]cryptotypes.PubKey, error) { panic("not implemented") }
@@ -74,6 +76,8 @@ var (
func (tx testTx) GetMsgs() []sdk.Msg { return nil }
func (tx testTx) GetMsgsV2() ([]protov2.Message, error) { return nil, nil }
func (tx testTx) ValidateBasic() error { return nil }
func (tx testTx) String() string {
@@ -88,9 +92,11 @@ func (sigErrTx) Size() int64 { return 0 }
func (sigErrTx) GetMsgs() []sdk.Msg { return nil }
func (sigErrTx) GetMsgsV2() ([]protov2.Message, error) { return nil, nil }
func (sigErrTx) ValidateBasic() error { return nil }
func (sigErrTx) GetSigners() []sdk.AccAddress { return nil }
func (sigErrTx) GetSigners() ([][]byte, error) { return nil, nil }
func (sigErrTx) GetPubKeys() ([]cryptotypes.PubKey, error) { return nil, nil }
+3 -2
View File
@@ -3,6 +3,7 @@ package testutil
import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/types/module"
@@ -22,7 +23,7 @@ type TestEncodingConfig struct {
func MakeTestEncodingConfig(modules ...module.AppModuleBasic) TestEncodingConfig {
aminoCodec := codec.NewLegacyAmino()
interfaceRegistry := types.NewInterfaceRegistry()
interfaceRegistry := testutil.CodecOptions{}.NewInterfaceRegistry()
codec := codec.NewProtoCodec(interfaceRegistry)
encCfg := TestEncodingConfig{
@@ -43,7 +44,7 @@ func MakeTestEncodingConfig(modules ...module.AppModuleBasic) TestEncodingConfig
}
func MakeTestTxConfig() client.TxConfig {
interfaceRegistry := types.NewInterfaceRegistry()
interfaceRegistry := testutil.CodecOptions{}.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
return tx.NewTxConfig(cdc, tx.DefaultSignModes)
}
+4 -3
View File
@@ -10,6 +10,7 @@ import (
"github.com/cosmos/gogoproto/proto"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
)
@@ -182,15 +183,15 @@ func (s SearchTxsResult) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (r TxResponse) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if r.Tx != nil {
var tx Tx
var tx HasMsgs
return unpacker.UnpackAny(r.Tx, &tx)
}
return nil
}
// GetTx unpacks the Tx from within a TxResponse and returns it
func (r TxResponse) GetTx() Tx {
if tx, ok := r.Tx.GetCachedValue().(Tx); ok {
func (r TxResponse) GetTx() HasMsgs {
if tx, ok := r.Tx.GetCachedValue().(HasMsgs); ok {
return tx
}
return nil
+43 -25
View File
@@ -4,7 +4,9 @@ import (
"fmt"
errorsmod "cosmossdk.io/errors"
protov2 "google.golang.org/protobuf/proto"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -17,7 +19,6 @@ const MaxGasWanted = uint64((1 << 63) - 1)
// Interface implementation checks.
var (
_, _, _, _ codectypes.UnpackInterfacesMessage = &Tx{}, &TxBody{}, &AuthInfo{}, &SignerInfo{}
_ sdk.Tx = &Tx{}
)
// GetMsgs implements the GetMsgs method on sdk.Tx.
@@ -89,41 +90,49 @@ func (t *Tx) ValidateBasic() error {
return sdkerrors.ErrNoSignatures
}
if len(sigs) != len(t.GetSigners()) {
return errorsmod.Wrapf(
sdkerrors.ErrUnauthorized,
"wrong number of signers; expected %d, got %d", len(t.GetSigners()), len(sigs),
)
}
return nil
}
// GetSigners retrieves all the signers of a tx.
// This includes all unique signers of the messages (in order),
// as well as the FeePayer (if specified and not already included).
func (t *Tx) GetSigners() []sdk.AccAddress {
var signers []sdk.AccAddress
func (t *Tx) GetSigners(cdc codec.Codec) ([][]byte, []protov2.Message, error) {
var signers [][]byte
seen := map[string]bool{}
for _, msg := range t.GetMsgs() {
for _, addr := range msg.GetSigners() {
if !seen[addr.String()] {
signers = append(signers, addr)
seen[addr.String()] = true
var msgsv2 []protov2.Message
for _, msg := range t.Body.Messages {
xs, msgv2, err := cdc.GetMsgAnySigners(msg)
if err != nil {
return nil, nil, err
}
msgsv2 = append(msgsv2, msgv2)
for _, signer := range xs {
if !seen[string(signer)] {
signers = append(signers, signer)
seen[string(signer)] = true
}
}
}
// ensure any specified fee payer is included in the required signers (at the end)
feePayer := t.AuthInfo.Fee.Payer
if feePayer != "" && !seen[feePayer] {
payerAddr := sdk.MustAccAddressFromBech32(feePayer)
signers = append(signers, payerAddr)
seen[feePayer] = true
var feePayerAddr []byte
if feePayer != "" {
var err error
feePayerAddr, err = cdc.InterfaceRegistry().SigningContext().AddressCodec().StringToBytes(feePayer)
if err != nil {
return nil, nil, err
}
}
if feePayerAddr != nil && !seen[string(feePayerAddr)] {
signers = append(signers, feePayerAddr)
seen[string(feePayerAddr)] = true
}
return signers
return signers, msgsv2, nil
}
func (t *Tx) GetGas() uint64 {
@@ -134,13 +143,22 @@ func (t *Tx) GetFee() sdk.Coins {
return t.AuthInfo.Fee.Amount
}
func (t *Tx) FeePayer() sdk.AccAddress {
func (t *Tx) FeePayer(cdc codec.Codec) []byte {
feePayer := t.AuthInfo.Fee.Payer
if feePayer != "" {
return sdk.MustAccAddressFromBech32(feePayer)
feePayerAddr, err := cdc.InterfaceRegistry().SigningContext().AddressCodec().StringToBytes(feePayer)
if err != nil {
panic(err)
}
return feePayerAddr
}
// use first signer as default if no payer specified
return t.GetSigners()[0]
signers, _, err := t.GetSigners(cdc)
if err != nil {
panic(err)
}
return signers[0]
}
func (t *Tx) FeeGranter() sdk.AccAddress {
@@ -205,8 +223,8 @@ func (m *SignerInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterInterface(msgResponseInterfaceProtoName, (*MsgResponse)(nil))
registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.Tx)(nil))
registry.RegisterImplementations((*sdk.Tx)(nil), &Tx{})
registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.HasMsgs)(nil))
registry.RegisterImplementations((*sdk.HasMsgs)(nil), &Tx{})
registry.RegisterInterface("cosmos.tx.v1beta1.TxExtensionOptionI", (*TxExtensionOptionI)(nil))
}
+25 -10
View File
@@ -6,15 +6,20 @@ import (
strings "strings"
"github.com/cosmos/gogoproto/proto"
protov2 "google.golang.org/protobuf/proto"
"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)
type (
// Msg defines the interface a transaction message must fulfill.
Msg interface {
proto.Message
// Msg defines the interface a transaction message needed to fulfill.
Msg = proto.Message
// LegacyMsg defines the interface a transaction message needed to fulfill up through
// v0.47.
LegacyMsg interface {
Msg
// GetSigners returns the addrs of signers that must sign.
// CONTRACT: All signatures must be present to be valid.
@@ -36,21 +41,27 @@ type (
GetSignature() []byte
}
// Tx defines the interface a transaction must fulfill.
Tx interface {
HasValidateBasic
// HasMsgs defines an interface a transaction must fulfill.
HasMsgs interface {
// GetMsgs gets the all the transaction's messages.
GetMsgs() []Msg
}
// Tx defines an interface a transaction must fulfill.
Tx interface {
HasMsgs
// GetMsgsV2 gets the transaction's messages as google.golang.org/protobuf/proto.Message's.
GetMsgsV2() ([]protov2.Message, error)
}
// FeeTx defines the interface to be implemented by Tx to use the FeeDecorators
FeeTx interface {
Tx
GetGas() uint64
GetFee() Coins
FeePayer() AccAddress
FeeGranter() AccAddress
FeePayer() []byte
FeeGranter() string
}
// TxWithMemo must have GetMemo() method to use ValidateMemoDecorator
@@ -84,7 +95,11 @@ type TxDecoder func(txBytes []byte) (Tx, error)
type TxEncoder func(tx Tx) ([]byte, error)
// MsgTypeURL returns the TypeURL of a `sdk.Msg`.
func MsgTypeURL(msg Msg) string {
func MsgTypeURL(msg proto.Message) string {
if m, ok := msg.(protov2.Message); ok {
return "/" + string(m.ProtoReflect().Descriptor().FullName())
}
return "/" + proto.MessageName(msg)
}
+2
View File
@@ -4,6 +4,7 @@ import (
"testing"
"github.com/stretchr/testify/suite"
"google.golang.org/protobuf/types/known/anypb"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
@@ -31,6 +32,7 @@ func (s *testMsgSuite) TestMsg() {
func (s *testMsgSuite) TestMsgTypeURL() {
s.Require().Equal("/testpb.TestMsg", sdk.MsgTypeURL(new(testdata.TestMsg)))
s.Require().Equal("/google.protobuf.Any", sdk.MsgTypeURL(&anypb.Any{}))
}
func (s *testMsgSuite) TestGetMsgFromTypeURL() {