Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Update GetSigners to return []string #9418

Merged
merged 20 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d7d37e9
update `GetSigners` to return []string
atheeshp May 28, 2021
f7f56d7
fix failing tests
atheeshp May 29, 2021
d62d5d5
Merge branch 'master' of github.com:cosmos/cosmos-sdk into update-get…
atheeshp May 29, 2021
f5fa9a2
fix tests
atheeshp May 29, 2021
cd28bd4
replace `ValidateBasic` with `ValidateMsg`
atheeshp Jun 1, 2021
74118f7
Merge branch 'master' of github.com:cosmos/cosmos-sdk into update-get…
atheeshp Jun 1, 2021
d60f84d
Merge branch 'master' into update-getsigners
atheeshp Jun 2, 2021
6859083
Merge branch 'master' into update-getsigners
atheeshp Jun 3, 2021
3dee427
Merge branch 'master' of github.com:cosmos/cosmos-sdk into update-get…
atheeshp Jun 28, 2021
964acf4
add change log
atheeshp Jun 28, 2021
cd4a867
Merge branch 'master' of github.com:cosmos/cosmos-sdk into update-get…
atheeshp Jun 28, 2021
87555ed
Merge branch 'master' into update-getsigners
atheeshp Jun 28, 2021
5a0a874
Merge branch 'master' of github.com:cosmos/cosmos-sdk into update-get…
atheeshp Jul 6, 2021
b572a65
add `ValidateMsg` in ante
atheeshp Jul 6, 2021
26623ee
Merge branch 'update-getsigners' of github.com:cosmos/cosmos-sdk into…
atheeshp Jul 6, 2021
71114b7
Merge branch 'master' of github.com:cosmos/cosmos-sdk into update-get…
atheeshp Jul 6, 2021
72e5bfc
review changes
atheeshp Jul 6, 2021
f926018
move `ValidateMsg` to `types/tx`
atheeshp Jul 6, 2021
52ec9b0
add test case
atheeshp Jul 7, 2021
72c93f5
Merge branch 'master' of github.com:cosmos/cosmos-sdk into update-get…
atheeshp Jul 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#9246](https://github.com/cosmos/cosmos-sdk/pull/9246) The `New` method for the network package now returns an error.
* (codec) [\#9521](https://github.com/cosmos/cosmos-sdk/pull/9521) Removed deprecated `clientCtx.JSONCodec` from `client.Context`.
* (codec) [\#9521](https://github.com/cosmos/cosmos-sdk/pull/9521) Rename `EncodingConfig.Marshaler` to `Codec`.
* [\#9418](https://github.com/cosmos/cosmos-sdk/pull/9418) `sdk.Msg`'s `GetSigners()` method updated to return `[]string`.
* [\#9594](https://github.com/cosmos/cosmos-sdk/pull/9594) `RESTHandlerFn` argument is removed from the `gov/NewProposalHandler`.
* [\#9594](https://github.com/cosmos/cosmos-sdk/pull/9594) `types/rest` package moved to `testutil/rest`.

Expand Down
3 changes: 2 additions & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

txvalidate "github.com/cosmos/cosmos-sdk/client/tx"
Copy link
Contributor

@amaury1093 amaury1093 Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

baseapp should not import anything from client, it creates messy code. Is there somewhere else we can put ValidateMsg?

Edit: Maybe types/tx?

Copy link
Contributor Author

@atheeshp atheeshp Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about types/utils.go ?

Copy link
Contributor

@amaury1093 amaury1093 Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we're moving away of putting anything in the types root package, it's too large already.

Edit: or put it in types/bech32, and call it bech32.ValidateMsgSigners

We can also create a new types/msg package. @robert-zaremba @aaronc @ryanchristo Do you have any opinions on this?

Copy link
Contributor Author

@atheeshp atheeshp Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or put it in types/bech32, and call it bech32.ValidateMsgSigners

I don't think it is fine to put it in types/bech32, since it is calling msg.ValidateBasic() inside. For now I kept it in the types/tx.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to avoid adding these as new public functions if possible and that means we don't need to think about packages as much if they're private. Since we are deprecating global bech32's soon that will avoid another API breaking change.

"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/snapshots"
"github.com/cosmos/cosmos-sdk/store"
Expand Down Expand Up @@ -508,7 +509,7 @@ func validateBasicTxMsgs(msgs []sdk.Msg) error {
}

for _, msg := range msgs {
err := msg.ValidateBasic()
err := txvalidate.ValidateMsg(msg)
if err != nil {
return err
}
Expand Down
30 changes: 15 additions & 15 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,10 @@ func (msg msgCounter) String() string { return "TODO" }
func (msg msgCounter) ProtoMessage() {}

// Implements Msg
func (msg msgCounter) Route() string { return routeMsgCounter }
func (msg msgCounter) Type() string { return "counter1" }
func (msg msgCounter) GetSignBytes() []byte { return nil }
func (msg msgCounter) GetSigners() []sdk.AccAddress { return nil }
func (msg msgCounter) Route() string { return routeMsgCounter }
func (msg msgCounter) Type() string { return "counter1" }
func (msg msgCounter) GetSignBytes() []byte { return nil }
func (msg msgCounter) GetSigners() []string { return nil }
func (msg msgCounter) ValidateBasic() error {
if msg.Counter >= 0 {
return nil
Expand Down Expand Up @@ -762,10 +762,10 @@ func (msg msgCounter2) String() string { return "TODO" }
func (msg msgCounter2) ProtoMessage() {}

// Implements Msg
func (msg msgCounter2) Route() string { return routeMsgCounter2 }
func (msg msgCounter2) Type() string { return "counter2" }
func (msg msgCounter2) GetSignBytes() []byte { return nil }
func (msg msgCounter2) GetSigners() []sdk.AccAddress { return nil }
func (msg msgCounter2) Route() string { return routeMsgCounter2 }
func (msg msgCounter2) Type() string { return "counter2" }
func (msg msgCounter2) GetSignBytes() []byte { return nil }
func (msg msgCounter2) GetSigners() []string { return nil }
func (msg msgCounter2) ValidateBasic() error {
if msg.Counter >= 0 {
return nil
Expand All @@ -779,13 +779,13 @@ type msgKeyValue struct {
Value []byte
}

func (msg msgKeyValue) Reset() {}
func (msg msgKeyValue) String() string { return "TODO" }
func (msg msgKeyValue) ProtoMessage() {}
func (msg msgKeyValue) Route() string { return routeMsgKeyValue }
func (msg msgKeyValue) Type() string { return "keyValue" }
func (msg msgKeyValue) GetSignBytes() []byte { return nil }
func (msg msgKeyValue) GetSigners() []sdk.AccAddress { return nil }
func (msg msgKeyValue) Reset() {}
func (msg msgKeyValue) String() string { return "TODO" }
func (msg msgKeyValue) ProtoMessage() {}
func (msg msgKeyValue) Route() string { return routeMsgKeyValue }
func (msg msgKeyValue) Type() string { return "keyValue" }
func (msg msgKeyValue) GetSignBytes() []byte { return nil }
func (msg msgKeyValue) GetSigners() []string { return nil }
func (msg msgKeyValue) ValidateBasic() error {
if msg.Key == nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "key cannot be nil")
Expand Down
19 changes: 18 additions & 1 deletion client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ import (
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
)

func ValidateMsg(msg sdk.Msg) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this? I thought all Msg's ValidateBasic already checks that the signers are correct bech32 addresses.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may not cover signer addresses in few cases, can you check this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a godoc for ValidateMsg?

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
}

// GenerateOrBroadcastTxCLI will either generate and print and unsigned transaction
// or sign it and broadcast it returning an error upon failure.
func GenerateOrBroadcastTxCLI(clientCtx client.Context, flagSet *pflag.FlagSet, msgs ...sdk.Msg) error {
Expand All @@ -35,7 +52,7 @@ func GenerateOrBroadcastTxWithFactory(clientCtx client.Context, txf Factory, msg
// Right now, we're factorizing that call inside this function.
// ref: https://github.com/cosmos/cosmos-sdk/pull/9236#discussion_r623803504
for _, msg := range msgs {
if err := msg.ValidateBasic(); err != nil {
if err := ValidateMsg(msg); err != nil {
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/mock/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (tx kvstoreTx) ValidateBasic() error {
return nil
}

func (tx kvstoreTx) GetSigners() []sdk.AccAddress {
func (tx kvstoreTx) GetSigners() []string {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion server/rosetta/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (c converter) Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, e
op := &rosettatypes.Operation{
Type: opName,
Status: &status,
Account: &rosettatypes.AccountIdentifier{Address: signer.String()},
Account: &rosettatypes.AccountIdentifier{Address: signer},
Metadata: meta,
}

Expand Down
18 changes: 4 additions & 14 deletions testutil/testdata/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,12 @@ func (msg *TestMsg) GetSignBytes() []byte {
}
return sdk.MustSortJSON(bz)
}
func (msg *TestMsg) GetSigners() []sdk.AccAddress {
addrs := make([]sdk.AccAddress, len(msg.Signers))
for i, in := range msg.Signers {
addr, err := sdk.AccAddressFromBech32(in)
if err != nil {
panic(err)
}

addrs[i] = addr
}

return addrs
func (msg *TestMsg) GetSigners() []string {
return msg.Signers
}
func (msg *TestMsg) ValidateBasic() error { return nil }

var _ sdk.Msg = &MsgCreateDog{}

func (msg *MsgCreateDog) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{} }
func (msg *MsgCreateDog) ValidateBasic() error { return nil }
func (msg *MsgCreateDog) GetSigners() []string { return []string{} }
func (msg *MsgCreateDog) ValidateBasic() error { return nil }
11 changes: 8 additions & 3 deletions types/tx/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ func (t *Tx) GetSigners() []sdk.AccAddress {

for _, msg := range t.GetMsgs() {
for _, addr := range msg.GetSigners() {
if !seen[addr.String()] {
signers = append(signers, addr)
seen[addr.String()] = true
if !seen[addr] {
signer, err := sdk.AccAddressFromBech32(addr)
if err != nil {
panic(err)
}

signers = append(signers, signer)
seen[addr] = true
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions types/tx_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type (
// doesn't require access to any other information.
ValidateBasic() error

// Signers returns the addrs of signers that must sign.
// Signers returns the bech32-encoded addrs of signers that must sign.
// CONTRACT: All signatures must be present to be valid.
// CONTRACT: Returns addrs in some deterministic order.
GetSigners() []AccAddress
GetSigners() []string
}

// Fee defines an interface for an application application-defined concrete
Expand Down
2 changes: 1 addition & 1 deletion types/tx_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (s *testMsgSuite) TestMsg() {

msg := testdata.NewTestMsg(accAddr)
s.Require().NotNil(msg)
s.Require().Equal([]sdk.AccAddress{accAddr}, msg.GetSigners())
s.Require().Equal([]string{accAddr.String()}, msg.GetSigners())
s.Require().Equal("TestMsg", msg.Route())
s.Require().Equal("Test message", msg.Type())
s.Require().Nil(msg.ValidateBasic())
Expand Down
3 changes: 2 additions & 1 deletion x/auth/ante/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
)

// ValidateBasicDecorator will call tx.ValidateBasic and return any non-nil error.
// ValidateBasicDecorator will call tx.ValidateBasic, ValidateMsg(for each msg inside tx)
// and return any non-nil error.
// If ValidateBasic passes, decorator calls next AnteHandler in chain. Note,
// ValidateBasicDecorator decorator will not get executed on ReCheckTx since it
// is not dependent on application state.
Expand Down
10 changes: 7 additions & 3 deletions x/auth/migrations/legacytx/stdtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,13 @@ func (tx StdTx) GetSigners() []sdk.AccAddress {

for _, msg := range tx.GetMsgs() {
for _, addr := range msg.GetSigners() {
if !seen[addr.String()] {
signers = append(signers, addr)
seen[addr.String()] = true
if !seen[addr] {
signer, err := sdk.AccAddressFromBech32(addr)
if err != nil {
panic(err)
}
signers = append(signers, signer)
seen[addr] = true
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions x/auth/vesting/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ func (msg MsgCreateVestingAccount) GetSignBytes() []byte {
}

// GetSigners returns the expected signers for a MsgCreateVestingAccount.
func (msg MsgCreateVestingAccount) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.FromAddress)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
func (msg MsgCreateVestingAccount) GetSigners() []string {
return []string{msg.FromAddress}
}
5 changes: 4 additions & 1 deletion x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []
if len(signers) != 1 {
return nil, sdkerrors.ErrInvalidRequest.Wrap("authorization can be given to msg with only one signer")
}
granter := signers[0]
granter, err := sdk.AccAddressFromBech32(signers[0])
if err != nil {
return nil, err
}
// if granter != grantee then check authorization.Accept, otherwise we implicitly accept.
if !granter.Equals(grantee) {
authorization, _ := k.GetCleanAuthorization(ctx, grantee, granter, sdk.MsgTypeURL(msg))
Expand Down
24 changes: 6 additions & 18 deletions x/authz/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ func NewMsgGrant(granter sdk.AccAddress, grantee sdk.AccAddress, a Authorization
}

// GetSigners implements Msg
func (msg MsgGrant) GetSigners() []sdk.AccAddress {
granter, err := sdk.AccAddressFromBech32(msg.Granter)
if err != nil {
panic(err)
}
return []sdk.AccAddress{granter}
func (msg MsgGrant) GetSigners() []string {
return []string{msg.Granter}
}

// ValidateBasic implements Msg
Expand Down Expand Up @@ -130,12 +126,8 @@ func NewMsgRevoke(granter sdk.AccAddress, grantee sdk.AccAddress, msgTypeURL str
}

// GetSigners implements Msg
func (msg MsgRevoke) GetSigners() []sdk.AccAddress {
granter, err := sdk.AccAddressFromBech32(msg.Granter)
if err != nil {
panic(err)
}
return []sdk.AccAddress{granter}
func (msg MsgRevoke) GetSigners() []string {
return []string{msg.Granter}
}

// ValidateBasic implements MsgRequest.ValidateBasic
Expand Down Expand Up @@ -209,12 +201,8 @@ func (msg MsgExec) GetMessages() ([]sdk.Msg, error) {
}

// GetSigners implements Msg
func (msg MsgExec) GetSigners() []sdk.AccAddress {
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
if err != nil {
panic(err)
}
return []sdk.AccAddress{grantee}
func (msg MsgExec) GetSigners() []string {
return []string{msg.Grantee}
}

// ValidateBasic implements Msg
Expand Down
15 changes: 5 additions & 10 deletions x/bank/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ func (msg MsgSend) GetSignBytes() []byte {
}

// GetSigners Implements Msg.
func (msg MsgSend) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.FromAddress)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
func (msg MsgSend) GetSigners() []string {
return []string{msg.FromAddress}
}

var _ sdk.Msg = &MsgMultiSend{}
Expand Down Expand Up @@ -96,11 +92,10 @@ func (msg MsgMultiSend) GetSignBytes() []byte {
}

// GetSigners Implements Msg.
func (msg MsgMultiSend) GetSigners() []sdk.AccAddress {
addrs := make([]sdk.AccAddress, len(msg.Inputs))
func (msg MsgMultiSend) GetSigners() []string {
addrs := make([]string, len(msg.Inputs))
for i, in := range msg.Inputs {
addr, _ := sdk.AccAddressFromBech32(in.Address)
addrs[i] = addr
addrs[i] = in.Address
}

return addrs
Expand Down
43 changes: 13 additions & 30 deletions x/bank/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ func TestMsgSendGetSignBytes(t *testing.T) {
require.Equal(t, expected, string(res))
}

func TestMsgSendGetSigners(t *testing.T) {
var msg = NewMsgSend(sdk.AccAddress([]byte("input111111111111111")), sdk.AccAddress{}, sdk.NewCoins())
res := msg.GetSigners()
// TODO: fix this !
require.Equal(t, fmt.Sprintf("%v", res), "[696E707574313131313131313131313131313131]")
}

func TestMsgMultiSendRoute(t *testing.T) {
// Construct a MsgSend
addr1 := sdk.AccAddress([]byte("input"))
Expand Down Expand Up @@ -238,32 +231,22 @@ func TestMsgMultiSendGetSignBytes(t *testing.T) {
}

func TestMsgMultiSendGetSigners(t *testing.T) {
var msg = MsgMultiSend{
Inputs: []Input{
NewInput(sdk.AccAddress([]byte("input111111111111111")), nil),
NewInput(sdk.AccAddress([]byte("input222222222222222")), nil),
NewInput(sdk.AccAddress([]byte("input333333333333333")), nil),
},
addrs := make([]string, 3)
inputs := make([]Input, 3)
for i, v := range []string{"input111111111111111", "input222222222222222", "input333333333333333"} {
addr := sdk.AccAddress([]byte(v))
inputs[i] = NewInput(addr, nil)
addrs[i] = addr.String()
}
var msg = NewMsgMultiSend(inputs, nil)

res := msg.GetSigners()
// TODO: fix this !
require.Equal(t, "[696E707574313131313131313131313131313131 696E707574323232323232323232323232323232 696E707574333333333333333333333333333333]", fmt.Sprintf("%v", res))
require.Equal(t, fmt.Sprintf("%v", addrs), fmt.Sprintf("%v", res))
}

func TestMsgSendSigners(t *testing.T) {
signers := []sdk.AccAddress{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}

someCoins := sdk.NewCoins(sdk.NewInt64Coin("atom", 123))
inputs := make([]Input, len(signers))
for i, signer := range signers {
inputs[i] = NewInput(signer, someCoins)
}
tx := NewMsgMultiSend(inputs, nil)

require.Equal(t, signers, tx.GetSigners())
func TestMsgSendGetSigners(t *testing.T) {
from := sdk.AccAddress([]byte("input111111111111111"))
msg := NewMsgSend(from, sdk.AccAddress{}, sdk.NewCoins())
res := msg.GetSigners()
require.Equal(t, fmt.Sprintf("%v", res), fmt.Sprintf("%v", []string{from.String()}))
}
5 changes: 2 additions & 3 deletions x/crisis/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ func (msg MsgVerifyInvariant) Route() string { return ModuleName }
func (msg MsgVerifyInvariant) Type() string { return "verify_invariant" }

// get the bytes for the message signer to sign on
func (msg MsgVerifyInvariant) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Sender)
return []sdk.AccAddress{sender}
func (msg MsgVerifyInvariant) GetSigners() []string {
return []string{msg.Sender}
}

// GetSignBytes gets the sign bytes for the msg MsgVerifyInvariant
Expand Down
Loading