From 344de9baa4db1a83b8eb3804ae4bdc1d28fe2dfe Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 21 Jul 2022 11:28:15 -0500 Subject: [PATCH 1/3] add check to lockup validate basic --- x/lockup/types/msgs.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x/lockup/types/msgs.go b/x/lockup/types/msgs.go index fab3cac197f..a7c6d3555de 100644 --- a/x/lockup/types/msgs.go +++ b/x/lockup/types/msgs.go @@ -38,6 +38,10 @@ func (m MsgLockTokens) ValidateBasic() error { return fmt.Errorf("lockups can only have one denom per lock ID, got %v", m.Coins) } + if !m.Coins.IsAllPositive() { + return fmt.Errorf("cannot lock up a zero or negative amount") + } + return nil } From 1a9d0bcf8de96b1d6cc382ee0f72ec32928c2860 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 21 Jul 2022 15:10:38 -0500 Subject: [PATCH 2/3] add table driven test for ValidateBasic --- x/lockup/types/msgs.go | 6 +++ x/lockup/types/msgs_test.go | 104 ++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 x/lockup/types/msgs_test.go diff --git a/x/lockup/types/msgs.go b/x/lockup/types/msgs.go index a7c6d3555de..bbbce794244 100644 --- a/x/lockup/types/msgs.go +++ b/x/lockup/types/msgs.go @@ -5,6 +5,7 @@ import ( "time" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) // constants. @@ -29,6 +30,11 @@ func NewMsgLockTokens(owner sdk.AccAddress, duration time.Duration, coins sdk.Co func (m MsgLockTokens) Route() string { return RouterKey } func (m MsgLockTokens) Type() string { return TypeMsgLockTokens } func (m MsgLockTokens) ValidateBasic() error { + _, err := sdk.AccAddressFromBech32(m.Owner) + if err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid owner address (%s)", err) + } + if m.Duration <= 0 { return fmt.Errorf("duration should be positive: %d < 0", m.Duration) } diff --git a/x/lockup/types/msgs_test.go b/x/lockup/types/msgs_test.go new file mode 100644 index 00000000000..ab1542832e2 --- /dev/null +++ b/x/lockup/types/msgs_test.go @@ -0,0 +1,104 @@ +package types + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + sdk "github.com/cosmos/cosmos-sdk/types" + + appParams "github.com/osmosis-labs/osmosis/v10/app/params" +) + +func TestMsgLockTokens(t *testing.T) { + appParams.SetAddressPrefixes() + pk1 := ed25519.GenPrivKey().PubKey() + addr1 := sdk.AccAddress(pk1.Address()).String() + invalidAddr := sdk.AccAddress("invalid") + + createMsg := func(after func(msg MsgLockTokens) MsgLockTokens) MsgLockTokens { + properMsg := MsgLockTokens{ + Owner: addr1, + Duration: time.Hour, + Coins: sdk.NewCoins(sdk.NewCoin("test", sdk.NewInt(100))), + } + + return after(properMsg) + } + + msg := createMsg(func(msg MsgLockTokens) MsgLockTokens { + // Do nothing + return msg + }) + + require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Type(), "lock_tokens") + signers := msg.GetSigners() + require.Equal(t, len(signers), 1) + require.Equal(t, signers[0].String(), addr1) + + tests := []struct { + name string + msg MsgLockTokens + expectPass bool + }{ + { + name: "proper msg", + msg: createMsg(func(msg MsgLockTokens) MsgLockTokens { + // Do nothing + return msg + }), + expectPass: true, + }, + { + name: "invalid owner", + msg: createMsg(func(msg MsgLockTokens) MsgLockTokens { + msg.Owner = invalidAddr.String() + return msg + }), + expectPass: false, + }, + { + name: "invalid duration", + msg: createMsg(func(msg MsgLockTokens) MsgLockTokens { + msg.Duration = -1 + return msg + }), + expectPass: false, + }, + { + name: "invalid coin length", + msg: createMsg(func(msg MsgLockTokens) MsgLockTokens { + msg.Coins = sdk.NewCoins(sdk.NewCoin("test1", sdk.NewInt(100000)), sdk.NewCoin("test2", sdk.NewInt(100000))) + return msg + }), + expectPass: false, + }, + { + name: "zero token amount", + msg: createMsg(func(msg MsgLockTokens) MsgLockTokens { + msg.Coins = sdk.NewCoins(sdk.NewCoin("test1", sdk.NewInt(0))) + return msg + }), + expectPass: false, + }, + } + + for _, test := range tests { + if test.expectPass { + require.NoError(t, test.msg.ValidateBasic(), "test: %v", test.name) + } else { + require.Error(t, test.msg.ValidateBasic(), "test: %v", test.name) + } + } +} + +// NewMsgLockTokens + +// MsgBeginUnlockingAll + +// MsgBeginUnlocking + +// MsgExtendLockup From 72477ccb738fdba3c70c15420245980660e89e44 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 21 Jul 2022 15:13:36 -0500 Subject: [PATCH 3/3] Update msgs_test.go --- x/lockup/types/msgs_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/lockup/types/msgs_test.go b/x/lockup/types/msgs_test.go index ab1542832e2..8dd16cb3fd1 100644 --- a/x/lockup/types/msgs_test.go +++ b/x/lockup/types/msgs_test.go @@ -95,7 +95,7 @@ func TestMsgLockTokens(t *testing.T) { } } -// NewMsgLockTokens +// TODO: Complete table driven tests for the remaining messages // MsgBeginUnlockingAll