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

feat(basket): implement sdk.Msg and LegacyMsg for MsgCreate #745

Merged
merged 14 commits into from
Feb 10, 2022
Merged
46 changes: 46 additions & 0 deletions x/ecocredit/basket/msg_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package basket

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"

"github.com/regen-network/regen-ledger/x/ecocredit"
)

var (
_ legacytx.LegacyMsg = &MsgCreate{}
)
technicallyty marked this conversation as resolved.
Show resolved Hide resolved

const nameMaxLen = 32

// ValidateBasic does a stateless sanity check on the provided data.
func (m MsgCreate) ValidateBasic() error {
technicallyty marked this conversation as resolved.
Show resolved Hide resolved
if _, err := sdk.AccAddressFromBech32(m.Curator); err != nil {
return sdkerrors.ErrInvalidAddress.Wrap("malformed curator address " + err.Error())
}
if m.Name == "" || len(m.Name) > nameMaxLen {
return sdkerrors.ErrInvalidRequest.Wrap("name must not be empty and must not be longer than 100 characters")
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
}
if m.Exponent > 32 {
return sdkerrors.ErrInvalidRequest.Wrap("exponent must not be bigger than 32")
}
return nil
}

// GetSigners returns the expected signers for MsgCreate.
func (m MsgCreate) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(m.Curator)
return []sdk.AccAddress{addr}
}

// GetSignBytes Implements LegacyMsg.
func (m MsgCreate) GetSignBytes() []byte {
return sdk.MustSortJSON(ecocredit.ModuleCdc.MustMarshalJSON(&m))
}

// Route Implements LegacyMsg.
func (m MsgCreate) Route() string { return sdk.MsgTypeURL(&m) }

// Type Implements LegacyMsg.
func (m MsgCreate) Type() string { return sdk.MsgTypeURL(&m) }
59 changes: 59 additions & 0 deletions x/ecocredit/basket/msg_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package basket

import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/stretchr/testify/require"
"github.com/thanhpk/randstr"
)

func errorMatches(t *testing.T, err error, expect string) {
if expect == "" {
require.NoError(t, err)
} else {
require.Error(t, err)
require.Contains(t, err.Error(), expect)
}
}

func TestMsgCreateValidateBasic(t *testing.T) {
_, _, addr1 := testdata.KeyTestPubAddr()
a := addr1.String()

tcs := []struct {
msg MsgCreate
err string
}{
{MsgCreate{Curator: "wrong"}, "malformed curator address"},
{MsgCreate{Curator: a, Name: ""}, "name must not be empty"},
{MsgCreate{Curator: a, Name: randstr.String(nameMaxLen + 1)}, "name must not"},
{MsgCreate{Curator: a, Name: randstr.String(nameMaxLen), Exponent: 33}, "exponent must"},

{MsgCreate{Curator: a, Name: randstr.String(nameMaxLen), Exponent: 0}, ""},
{MsgCreate{Curator: a, Name: randstr.String(1), Exponent: 32}, ""},
}

for i, tc := range tcs {
t.Run(fmt.Sprint("test-", i), func(t *testing.T) {
err := tc.msg.ValidateBasic()
errorMatches(t, err, tc.err)
})
}
}

func TestMsgCreateGetSigners(t *testing.T) {
_, _, addr1 := testdata.KeyTestPubAddr()
m := MsgCreate{Curator: addr1.String(), Name: "name", Exponent: 2}
require.Equal(t, []sdk.AccAddress{addr1}, m.GetSigners())
}

func TestMsgCreateSignBytes(t *testing.T) {
_, _, addr1 := testdata.KeyTestPubAddr()
m := MsgCreate{Curator: addr1.String(), Name: "name", Exponent: 2}
bz := m.GetSignBytes()
require.NotEmpty(t, bz)
}
1 change: 1 addition & 0 deletions x/ecocredit/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ require (
github.com/tendermint/btcd v0.1.1 // indirect
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/thanhpk/randstr v1.0.4 // indirect
github.com/zondax/hid v0.9.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
Expand Down
2 changes: 2 additions & 0 deletions x/ecocredit/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,8 @@ github.com/tendermint/tm-db v0.6.4/go.mod h1:dptYhIpJ2M5kUuenLr+Yyf3zQOv1SgBZcl8
github.com/tendermint/tm-db v0.6.6 h1:EzhaOfR0bdKyATqcd5PNeyeq8r+V4bRPHBfyFdD9kGM=
github.com/tendermint/tm-db v0.6.6/go.mod h1:wP8d49A85B7/erz/r4YbKssKw6ylsO/hKtFk7E1aWZI=
github.com/tetafro/godot v1.4.9/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8=
github.com/thanhpk/randstr v1.0.4 h1:IN78qu/bR+My+gHCvMEXhR/i5oriVHcTB/BJJIRTsNo=
github.com/thanhpk/randstr v1.0.4/go.mod h1:M/H2P1eNLZzlDwAzpkkkUvoyNNMbzRGhESZuEQk3r0U=
github.com/tidwall/gjson v1.6.7/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
Expand Down