refactor: Update GetSigners to return []string (#9418)

<!-- < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < ☺
v                               ✰  Thanks for creating a PR! ✰
v    Before smashing the submit button please review the checkboxes.
v    If a checkbox is n/a - please still include it but + a little note why
☺ > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >  -->

## Description

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review.
-->

closes: #9239 

---

Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

- [ ] Targeted PR against correct branch (see [CONTRIBUTING.md](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
- [ ] Code follows the [module structure standards](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules/structure.md).
- [ ] Wrote unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] Updated relevant documentation (`docs/`) or specification (`x/<module>/spec/`)
- [ ] Added relevant `godoc` [comments](https://blog.golang.org/godoc-documenting-go-code).
- [ ] Added a relevant changelog entry to the `Unreleased` section in `CHANGELOG.md`
- [ ] Re-reviewed `Files changed` in the Github PR explorer
- [ ] Review `Codecov Report` in the comment section below once CI passes
This commit is contained in:
atheeshp
2021-07-07 10:18:00 +00:00
committed by GitHub
parent bfa18c79be
commit a24a329bb5
30 changed files with 182 additions and 213 deletions
+27 -3
View File
@@ -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
}
}
}
@@ -202,3 +207,22 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.Tx)(nil))
registry.RegisterImplementations((*sdk.Tx)(nil), &Tx{})
}
// 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
}
+43
View File
@@ -0,0 +1,43 @@
package tx_test
import (
"testing"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/stretchr/testify/suite"
)
type testMsgSuite struct {
suite.Suite
}
func TestValidateMsg(t *testing.T) {
suite.Run(t, new(testMsgSuite))
}
func (s *testMsgSuite) TestMsg() {
cases := []struct {
signer []byte
expErr bool
}{
{
[]byte(""),
true,
},
{
[]byte("validAddress"),
false,
},
}
for _, c := range cases {
msg := testdata.NewTestMsg(c.signer)
err := tx.ValidateMsg(msg)
if c.expErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
}
}
}
+2 -2
View File
@@ -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
+1 -1
View File
@@ -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())