From 6672f36af4e3f8a42cea253aa08c93303f67a12c Mon Sep 17 00:00:00 2001 From: vuong Date: Wed, 20 Jul 2022 19:20:13 +0700 Subject: [PATCH 01/26] registerCodec for authz --- x/gamm/types/codec.go | 6 ++++++ x/incentives/types/codec.go | 6 ++++++ x/lockup/types/codec.go | 6 ++++++ x/superfluid/types/codec.go | 6 ++++++ 4 files changed, 24 insertions(+) diff --git a/x/gamm/types/codec.go b/x/gamm/types/codec.go index c0d961f54a6..d00a4182bc9 100644 --- a/x/gamm/types/codec.go +++ b/x/gamm/types/codec.go @@ -5,6 +5,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/msgservice" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers the necessary x/gamm interfaces and concrete types @@ -55,5 +56,10 @@ var ( func init() { RegisterLegacyAminoCodec(amino) + // Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be + // used to properly serialize MsgGrant and MsgExec instances + sdk.RegisterLegacyAminoCodec(amino) + RegisterLegacyAminoCodec(authzcodec.Amino) + amino.Seal() } diff --git a/x/incentives/types/codec.go b/x/incentives/types/codec.go index 07c7a66698d..7d948cdf770 100644 --- a/x/incentives/types/codec.go +++ b/x/incentives/types/codec.go @@ -5,6 +5,7 @@ import ( cdctypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/msgservice" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" ) var ( @@ -32,5 +33,10 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { func init() { RegisterCodec(amino) + // Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be + // used to properly serialize MsgGrant and MsgExec instances + sdk.RegisterLegacyAminoCodec(amino) + RegisterCodec(authzcodec.Amino) + amino.Seal() } diff --git a/x/lockup/types/codec.go b/x/lockup/types/codec.go index 5faea80aaaa..d15b1353f24 100644 --- a/x/lockup/types/codec.go +++ b/x/lockup/types/codec.go @@ -5,6 +5,7 @@ import ( cdctypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/msgservice" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" ) func RegisterCodec(cdc *codec.LegacyAmino) { @@ -30,5 +31,10 @@ var ( func init() { RegisterCodec(amino) + // Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be + // used to properly serialize MsgGrant and MsgExec instances + sdk.RegisterLegacyAminoCodec(amino) + RegisterCodec(authzcodec.Amino) + amino.Seal() } diff --git a/x/superfluid/types/codec.go b/x/superfluid/types/codec.go index d766d0057f5..2387c279b80 100644 --- a/x/superfluid/types/codec.go +++ b/x/superfluid/types/codec.go @@ -5,6 +5,7 @@ import ( cdctypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/msgservice" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" ) @@ -44,5 +45,10 @@ var ( func init() { RegisterCodec(amino) + sdk.RegisterLegacyAminoCodec(amino) + + // Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be + // used to properly serialize MsgGrant and MsgExec instances + RegisterCodec(authzcodec.Amino) amino.Seal() } From de7ef7ae388c3b0c1b17d41bb56e440d7381a1c7 Mon Sep 17 00:00:00 2001 From: vuong Date: Sat, 23 Jul 2022 05:30:14 +0700 Subject: [PATCH 02/26] gamm msg: test authz grant revoke exec --- x/gamm/types/msgs_test.go | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/x/gamm/types/msgs_test.go b/x/gamm/types/msgs_test.go index 3cfee6629dc..0fd5bbbeaf7 100644 --- a/x/gamm/types/msgs_test.go +++ b/x/gamm/types/msgs_test.go @@ -1,12 +1,17 @@ package types import ( + "fmt" "testing" + "time" "github.com/stretchr/testify/require" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" + "github.com/cosmos/cosmos-sdk/x/authz" appParams "github.com/osmosis-labs/osmosis/v10/app/params" ) @@ -867,3 +872,58 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { } } } + +// Test authz with ledger +func TestAuthzMsg(t *testing.T) { + appParams.SetAddressPrefixes() + pk1 := ed25519.GenPrivKey().PubKey() + addr1 := sdk.AccAddress(pk1.Address()).String() + + someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + + testCases := []struct { + expectedRevokeSignByteMsg string + expectedGrantSignByteMsg string + expectedExecStrSignByteMsg string + gammMsg sdk.Msg + }{ + { + expectedRevokeSignByteMsg: fmt.Sprintf(""), + expectedGrantSignByteMsg: fmt.Sprintf(""), + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/exit-swap-share-amount-in","value":{"pool_id":"1","sender":"%s","share_in_amount":"100","token_out_denom":"test","token_out_min_amount":"100"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + gammMsg: &MsgExitSwapShareAmountIn{ + Sender: addr1, + PoolId: 1, + TokenOutDenom: "test", + ShareInAmount: sdk.NewInt(100), + TokenOutMinAmount: sdk.NewInt(100), + }, + }, + } + for _, tc := range testCases { + // Authz: Grant Msg + typeURL := sdk.MsgTypeURL(tc.gammMsg) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} + require.Equal(t, + `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), + ) + + // Authz: Revoke Msg + msgRevoke := &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} + require.Equal(t, + `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}}],"sequence":"1","timeout_height":"1"}`, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), + ) + + // Authz: Exec Msg + msgAny, _ := cdctypes.NewAnyWithValue(tc.gammMsg) + msgExec := &authz.MsgExec{Grantee: "cosmos1def", Msgs: []*cdctypes.Any{msgAny}} + require.Equal(t, + tc.expectedExecStrSignByteMsg, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), + ) + } +} From b694cf8511baa7a07657e0e7549278bb7d62fec5 Mon Sep 17 00:00:00 2001 From: vuong Date: Sat, 23 Jul 2022 16:15:52 +0700 Subject: [PATCH 03/26] gamm msg: test authz grant revoke exec --- x/gamm/types/msgs_test.go | 104 +++++++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 7 deletions(-) diff --git a/x/gamm/types/msgs_test.go b/x/gamm/types/msgs_test.go index 0fd5bbbeaf7..31b915174f3 100644 --- a/x/gamm/types/msgs_test.go +++ b/x/gamm/types/msgs_test.go @@ -873,23 +873,23 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { } } -// Test authz with ledger +// Test authz func TestAuthzMsg(t *testing.T) { appParams.SetAddressPrefixes() pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() - + coin := sdk.NewCoin("stake", sdk.NewInt(1)) someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) testCases := []struct { - expectedRevokeSignByteMsg string expectedGrantSignByteMsg string + expectedRevokeSignByteMsg string expectedExecStrSignByteMsg string gammMsg sdk.Msg }{ { - expectedRevokeSignByteMsg: fmt.Sprintf(""), - expectedGrantSignByteMsg: fmt.Sprintf(""), + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}}],"sequence":"1","timeout_height":"1"}`, expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/exit-swap-share-amount-in","value":{"pool_id":"1","sender":"%s","share_in_amount":"100","token_out_denom":"test","token_out_min_amount":"100"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), gammMsg: &MsgExitSwapShareAmountIn{ Sender: addr1, @@ -899,6 +899,96 @@ func TestAuthzMsg(t *testing.T) { TokenOutMinAmount: sdk.NewInt(100), }, }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/exit-swap-extern-amount-out","value":{"pool_id":"1","sender":"%s","share_in_max_amount":"1","token_out":{"amount":"1","denom":"stake"}}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + gammMsg: &MsgExitSwapExternAmountOut{ + Sender: addr1, + PoolId: 1, + TokenOut: coin, + ShareInMaxAmount: sdk.NewInt(1), + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitPool"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitPool"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/exit-pool","value":{"pool_id":"1","sender":"%s","share_in_amount":"100","token_out_mins":[{"amount":"1","denom":"stake"}]}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + gammMsg: &MsgExitPool{ + Sender: addr1, + PoolId: 1, + ShareInAmount: sdk.NewInt(100), + TokenOutMins: sdk.NewCoins(coin), + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgJoinPool"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinPool"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/join-pool","value":{"pool_id":"1","sender":"%s","share_out_amount":"1","token_in_maxs":[{"amount":"1","denom":"stake"}]}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + gammMsg: &MsgJoinPool{ + Sender: addr1, + PoolId: 1, + ShareOutAmount: sdk.NewInt(1), + TokenInMaxs: sdk.NewCoins(coin), + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/join-swap-extern-amount-in","value":{"pool_id":"1","sender":"%s","share_out_min_amount":"1","token_in":{"amount":"1","denom":"stake"}}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + gammMsg: &MsgJoinSwapExternAmountIn{ + Sender: addr1, + PoolId: 1, + TokenIn: coin, + ShareOutMinAmount: sdk.NewInt(1), + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgJoinSwapShareAmountOut"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinSwapShareAmountOut"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/join-swap-share-amount-out","value":{"pool_id":"1","sender":"%s","share_out_amount":"1","token_in_denom":"denom","token_in_max_amount":"1"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + gammMsg: &MsgJoinSwapShareAmountOut{ + Sender: addr1, + PoolId: 1, + TokenInDenom: "denom", + ShareOutAmount: sdk.NewInt(1), + TokenInMaxAmount: sdk.NewInt(1), + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/swap-exact-amount-in","value":{"routes":[{"token_out_denom":"test"},{"pool_id":"1","token_out_denom":"test2"}],"sender":"%s","token_in":{"amount":"1","denom":"stake"},"token_out_min_amount":"1"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + gammMsg: &MsgSwapExactAmountIn{ + Sender: addr1, + Routes: []SwapAmountInRoute{{ + PoolId: 0, + TokenOutDenom: "test", + }, { + PoolId: 1, + TokenOutDenom: "test2", + }}, + TokenIn: coin, + TokenOutMinAmount: sdk.NewInt(1), + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgSwapExactAmountOut"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgSwapExactAmountOut"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/swap-exact-amount-out","value":{"routes":[{"token_in_denom":"test"},{"pool_id":"1","token_in_denom":"test2"}],"sender":"%s","token_in_max_amount":"1","token_out":{"amount":"1","denom":"stake"}}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + gammMsg: &MsgSwapExactAmountOut{ + Sender: addr1, + Routes: []SwapAmountOutRoute{{ + PoolId: 0, + TokenInDenom: "test", + }, { + PoolId: 1, + TokenInDenom: "test2", + }}, + TokenOut: coin, + TokenInMaxAmount: sdk.NewInt(1), + }, + }, } for _, tc := range testCases { // Authz: Grant Msg @@ -907,14 +997,14 @@ func TestAuthzMsg(t *testing.T) { require.NoError(t, err) msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} require.Equal(t, - `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + tc.expectedGrantSignByteMsg, string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), ) // Authz: Revoke Msg msgRevoke := &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} require.Equal(t, - `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}}],"sequence":"1","timeout_height":"1"}`, + tc.expectedRevokeSignByteMsg, string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), ) From 415cb39e9ebbfac6f09e5dcb215cf96bf9ffb5f0 Mon Sep 17 00:00:00 2001 From: vuong177 Date: Sun, 24 Jul 2022 19:43:08 +0700 Subject: [PATCH 04/26] test authz for incentives module --- x/incentives/types/msgs_test.go | 75 +++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/x/incentives/types/msgs_test.go b/x/incentives/types/msgs_test.go index cc906cd5ac5..c093fcaaa0b 100644 --- a/x/incentives/types/msgs_test.go +++ b/x/incentives/types/msgs_test.go @@ -1,15 +1,21 @@ package types import ( + "fmt" "testing" time "time" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + + appParams "github.com/osmosis-labs/osmosis/v10/app/params" lockuptypes "github.com/osmosis-labs/osmosis/v10/x/lockup/types" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" + "github.com/cosmos/cosmos-sdk/x/authz" ) // TestMsgCreatePool tests if valid/invalid create pool messages are properly validated/invalidated @@ -210,3 +216,72 @@ func TestMsgAddToGauge(t *testing.T) { } } } + +func TestAuthzMsg(t *testing.T) { + appParams.SetAddressPrefixes() + pk1 := ed25519.GenPrivKey().PubKey() + addr1 := sdk.AccAddress(pk1.Address()).String() + coin := sdk.NewCoin("stake", sdk.NewInt(1)) + someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + + testCases := []struct { + expectedGrantSignByteMsg string + expectedRevokeSignByteMsg string + expectedExecStrSignByteMsg string + incentivesMsg sdk.Msg + }{ + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.incentives.MsgAddToGauge"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.incentives.MsgAddToGauge"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/incentives/add-to-gauge","value":{"gauge_id":"1","owner":"%s","rewards":[{"amount":"1","denom":"stake"}]}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + incentivesMsg: &MsgAddToGauge{ + Owner: addr1, + GaugeId: 1, + Rewards: sdk.NewCoins(coin), + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.incentives.MsgCreateGauge"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.incentives.MsgCreateGauge"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/incentives/create-gauge","value":{"coins":[{"amount":"1","denom":"stake"}],"distribute_to":{"denom":"lptoken","duration":"1000000000","timestamp":"0001-01-01T00:00:00Z"},"num_epochs_paid_over":"1","owner":"%s","start_time":"0001-01-01T01:01:01.000000001Z"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + incentivesMsg: &MsgCreateGauge{ + IsPerpetual: false, + Owner: addr1, + DistributeTo: lockuptypes.QueryCondition{ + LockQueryType: lockuptypes.ByDuration, + Denom: "lptoken", + Duration: time.Second, + }, + Coins: sdk.NewCoins(coin), + StartTime: someDate, + NumEpochsPaidOver: 1, + }, + }, + } + for _, tc := range testCases { + // Authz: Grant Msg + typeURL := sdk.MsgTypeURL(tc.incentivesMsg) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} + require.Equal(t, + tc.expectedGrantSignByteMsg, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), + ) + + // Authz: Revoke Msg + msgRevoke := &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} + require.Equal(t, + tc.expectedRevokeSignByteMsg, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), + ) + + // Authz: Exec Msg + msgAny, _ := cdctypes.NewAnyWithValue(tc.incentivesMsg) + msgExec := &authz.MsgExec{Grantee: "cosmos1def", Msgs: []*cdctypes.Any{msgAny}} + require.Equal(t, + tc.expectedExecStrSignByteMsg, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), + ) + } +} From f73dd97bf6028a5baba37048700b1cab33ccbd33 Mon Sep 17 00:00:00 2001 From: vuong177 Date: Sun, 24 Jul 2022 21:17:45 +0700 Subject: [PATCH 05/26] test authz for superfluid module --- x/superfluid/types/msg_test.go | 106 +++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 x/superfluid/types/msg_test.go diff --git a/x/superfluid/types/msg_test.go b/x/superfluid/types/msg_test.go new file mode 100644 index 00000000000..193a48c1e4d --- /dev/null +++ b/x/superfluid/types/msg_test.go @@ -0,0 +1,106 @@ +package types + +import ( + fmt "fmt" + "testing" + "time" + + sdk "github.com/cosmos/cosmos-sdk/types" + + appParams "github.com/osmosis-labs/osmosis/v10/app/params" + + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" + "github.com/cosmos/cosmos-sdk/x/authz" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/ed25519" +) + +func TestAuthzMsg(t *testing.T) { + appParams.SetAddressPrefixes() + pk1 := ed25519.GenPrivKey().PubKey() + addr1 := sdk.AccAddress(pk1.Address()).String() + coin := sdk.NewCoin("stake", sdk.NewInt(1)) + someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + + testCases := []struct { + expectedGrantSignByteMsg string + expectedRevokeSignByteMsg string + expectedExecStrSignByteMsg string + msg sdk.Msg + }{ + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgLockAndSuperfluidDelegate"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.superfluid.MsgLockAndSuperfluidDelegate"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/lock-and-superfluid-delegate","value":{"coins":[{"amount":"1","denom":"stake"}],"sender":"%s","val_addr":"valoper1xyz"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + msg: &MsgLockAndSuperfluidDelegate{ + Sender: addr1, + Coins: sdk.NewCoins(coin), + ValAddr: "valoper1xyz", + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgSuperfluidDelegate"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.superfluid.MsgSuperfluidDelegate"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/superfluid-delegate","value":{"lock_id":"1","sender":"%s","val_addr":"valoper1xyz"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + msg: &MsgSuperfluidDelegate{ + Sender: addr1, + LockId: 1, + ValAddr: "valoper1xyz", + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgSuperfluidUnbondLock"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.superfluid.MsgSuperfluidUnbondLock"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/superfluid-unbond-lock","value":{"lock_id":"1","sender":"%s"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + msg: &MsgSuperfluidUnbondLock{ + Sender: addr1, + LockId: 1, + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgSuperfluidUndelegate"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.superfluid.MsgSuperfluidUndelegate"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/superfluid-undelegate","value":{"lock_id":"1","sender":"%s"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + msg: &MsgSuperfluidUndelegate{ + Sender: addr1, + LockId: 1, + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgUnPoolWhitelistedPool"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.superfluid.MsgUnPoolWhitelistedPool"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/unpool-whitelisted-pool","value":{"pool_id":"1","sender":"%s"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + msg: &MsgUnPoolWhitelistedPool{ + Sender: addr1, + PoolId: 1, + }, + }, + } + for _, tc := range testCases { + // Authz: Grant Msg + typeURL := sdk.MsgTypeURL(tc.msg) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} + require.Equal(t, + tc.expectedGrantSignByteMsg, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), + ) + + // Authz: Revoke Msg + msgRevoke := &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} + require.Equal(t, + tc.expectedRevokeSignByteMsg, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), + ) + + // Authz: Exec Msg + msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) + msgExec := &authz.MsgExec{Grantee: "cosmos1def", Msgs: []*cdctypes.Any{msgAny}} + require.Equal(t, + tc.expectedExecStrSignByteMsg, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), + ) + } +} From 00ea20806170683a9c4bc2c231f2db5a85b9f074 Mon Sep 17 00:00:00 2001 From: vuong177 Date: Sun, 24 Jul 2022 21:42:15 +0700 Subject: [PATCH 06/26] test authz for tokenfactory module --- x/tokenfactory/types/msgs_test.go | 96 +++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 x/tokenfactory/types/msgs_test.go diff --git a/x/tokenfactory/types/msgs_test.go b/x/tokenfactory/types/msgs_test.go new file mode 100644 index 00000000000..eb4a799a78f --- /dev/null +++ b/x/tokenfactory/types/msgs_test.go @@ -0,0 +1,96 @@ +package types + +import ( + fmt "fmt" + "testing" + "time" + + sdk "github.com/cosmos/cosmos-sdk/types" + + appParams "github.com/osmosis-labs/osmosis/v10/app/params" + + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" + "github.com/cosmos/cosmos-sdk/x/authz" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/ed25519" +) + +func TestAuthzMsg(t *testing.T) { + appParams.SetAddressPrefixes() + pk1 := ed25519.GenPrivKey().PubKey() + addr1 := sdk.AccAddress(pk1.Address()).String() + coin := sdk.NewCoin("denom", sdk.NewInt(1)) + someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + + testCases := []struct { + expectedGrantSignByteMsg string + expectedRevokeSignByteMsg string + expectedExecStrSignByteMsg string + msg sdk.Msg + }{ + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.tokenfactory.v1beta1.MsgCreateDenom"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgCreateDenom"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"sender":"%s","subdenom":"valoper1xyz"}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + msg: &MsgCreateDenom{ + Sender: addr1, + Subdenom: "valoper1xyz", + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.tokenfactory.v1beta1.MsgBurn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgBurn"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"amount":{"amount":"1","denom":"denom"},"sender":"%s"}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + msg: &MsgBurn{ + Sender: addr1, + Amount: coin, + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.tokenfactory.v1beta1.MsgMint"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgMint"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"amount":{"amount":"1","denom":"denom"},"sender":"%s"}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + msg: &MsgMint{ + Sender: addr1, + Amount: coin, + }, + }, + { + expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.tokenfactory.v1beta1.MsgChangeAdmin"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgChangeAdmin"}}],"sequence":"1","timeout_height":"1"}`, + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"denom":"denom","new_admin":"osmo1q8tq5qhrhw6t970egemuuwywhlhpnmdmts6xnu","sender":"%s"}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + msg: &MsgChangeAdmin{ + Sender: addr1, + Denom: "denom", + NewAdmin: "osmo1q8tq5qhrhw6t970egemuuwywhlhpnmdmts6xnu", + }, + }, + } + for _, tc := range testCases { + // Authz: Grant Msg + typeURL := sdk.MsgTypeURL(tc.msg) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} + require.Equal(t, + tc.expectedGrantSignByteMsg, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), + ) + + // Authz: Revoke Msg + msgRevoke := &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} + require.Equal(t, + tc.expectedRevokeSignByteMsg, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), + ) + + // Authz: Exec Msg + msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) + msgExec := &authz.MsgExec{Grantee: "cosmos1def", Msgs: []*cdctypes.Any{msgAny}} + require.Equal(t, + tc.expectedExecStrSignByteMsg, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), + ) + } +} From 89a04f9dbf332615c6bd69aadc78eed94be306e6 Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 29 Jul 2022 08:55:24 +0700 Subject: [PATCH 07/26] gamm test clean --- x/gamm/types/msgs_test.go | 782 +++++++++++++++++++++++++++++++++++--- 1 file changed, 733 insertions(+), 49 deletions(-) diff --git a/x/gamm/types/msgs_test.go b/x/gamm/types/msgs_test.go index 31b915174f3..fdabc1a4a3d 100644 --- a/x/gamm/types/msgs_test.go +++ b/x/gamm/types/msgs_test.go @@ -2,6 +2,7 @@ package types import ( "fmt" + "strings" "testing" "time" @@ -873,7 +874,7 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { } } -// Test authz +// Test authz serialize and de-serializes for gamm msg. func TestAuthzMsg(t *testing.T) { appParams.SetAddressPrefixes() pk1 := ed25519.GenPrivKey().PubKey() @@ -881,16 +882,101 @@ func TestAuthzMsg(t *testing.T) { coin := sdk.NewCoin("stake", sdk.NewInt(1)) someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + const ( + mockGranter string = "cosmos1abc" + mockGrantee string = "cosmos1xyz" + ) + testCases := []struct { + name string expectedGrantSignByteMsg string expectedRevokeSignByteMsg string expectedExecStrSignByteMsg string gammMsg sdk.Msg }{ { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/exit-swap-share-amount-in","value":{"pool_id":"1","sender":"%s","share_in_amount":"100","token_out_denom":"test","token_out_min_amount":"100"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgExitSwapExternAmountOut", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/gamm/exit-swap-share-amount-in", + "value":{ + "pool_id":"1", + "sender":"%s", + "share_in_amount":"100", + "token_out_denom":"test", + "token_out_min_amount":"100" + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), gammMsg: &MsgExitSwapShareAmountIn{ Sender: addr1, PoolId: 1, @@ -900,9 +986,90 @@ func TestAuthzMsg(t *testing.T) { }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/exit-swap-extern-amount-out","value":{"pool_id":"1","sender":"%s","share_in_max_amount":"1","token_out":{"amount":"1","denom":"stake"}}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: `MsgExitSwapExternAmountOut`, + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/gamm/exit-swap-extern-amount-out", + "value":{ + "pool_id":"1", + "sender":"%s", + "share_in_max_amount":"1", + "token_out":{ + "amount":"1", + "denom":"stake" + } + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), gammMsg: &MsgExitSwapExternAmountOut{ Sender: addr1, PoolId: 1, @@ -911,9 +1078,92 @@ func TestAuthzMsg(t *testing.T) { }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitPool"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitPool"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/exit-pool","value":{"pool_id":"1","sender":"%s","share_in_amount":"100","token_out_mins":[{"amount":"1","denom":"stake"}]}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgExitPool", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.gamm.v1beta1.MsgExitPool" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.gamm.v1beta1.MsgExitPool" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/gamm/exit-pool", + "value":{ + "pool_id":"1", + "sender":"%s", + "share_in_amount":"100", + "token_out_mins":[ + { + "amount":"1", + "denom":"stake" + } + ] + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), gammMsg: &MsgExitPool{ Sender: addr1, PoolId: 1, @@ -922,9 +1172,92 @@ func TestAuthzMsg(t *testing.T) { }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgJoinPool"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinPool"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/join-pool","value":{"pool_id":"1","sender":"%s","share_out_amount":"1","token_in_maxs":[{"amount":"1","denom":"stake"}]}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgJoinPool", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.gamm.v1beta1.MsgJoinPool" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinPool" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/gamm/join-pool", + "value":{ + "pool_id":"1", + "sender":"%s", + "share_out_amount":"1", + "token_in_maxs":[ + { + "amount":"1", + "denom":"stake" + } + ] + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), gammMsg: &MsgJoinPool{ Sender: addr1, PoolId: 1, @@ -933,9 +1266,90 @@ func TestAuthzMsg(t *testing.T) { }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/join-swap-extern-amount-in","value":{"pool_id":"1","sender":"%s","share_out_min_amount":"1","token_in":{"amount":"1","denom":"stake"}}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgJoinSwapExternAmountIn", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/gamm/join-swap-extern-amount-in", + "value":{ + "pool_id":"1", + "sender":"%s", + "share_out_min_amount":"1", + "token_in":{ + "amount":"1", + "denom":"stake" + } + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), gammMsg: &MsgJoinSwapExternAmountIn{ Sender: addr1, PoolId: 1, @@ -944,9 +1358,88 @@ func TestAuthzMsg(t *testing.T) { }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgJoinSwapShareAmountOut"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinSwapShareAmountOut"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/join-swap-share-amount-out","value":{"pool_id":"1","sender":"%s","share_out_amount":"1","token_in_denom":"denom","token_in_max_amount":"1"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgJoinSwapShareAmountOut", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.gamm.v1beta1.MsgJoinSwapShareAmountOut" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinSwapShareAmountOut" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/gamm/join-swap-share-amount-out", + "value":{ + "pool_id":"1", + "sender":"%s", + "share_out_amount":"1", + "token_in_denom":"denom", + "token_in_max_amount":"1" + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), gammMsg: &MsgJoinSwapShareAmountOut{ Sender: addr1, PoolId: 1, @@ -956,9 +1449,98 @@ func TestAuthzMsg(t *testing.T) { }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/swap-exact-amount-in","value":{"routes":[{"token_out_denom":"test"},{"pool_id":"1","token_out_denom":"test2"}],"sender":"%s","token_in":{"amount":"1","denom":"stake"},"token_out_min_amount":"1"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgSwapExactAmountIn", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/gamm/swap-exact-amount-in", + "value":{ + "routes":[ + { + "token_out_denom":"test" + }, + { + "pool_id":"1", + "token_out_denom":"test2" + } + ], + "sender":"%s", + "token_in":{ + "amount":"1", + "denom":"stake" + }, + "token_out_min_amount":"1" + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), gammMsg: &MsgSwapExactAmountIn{ Sender: addr1, Routes: []SwapAmountInRoute{{ @@ -973,9 +1555,98 @@ func TestAuthzMsg(t *testing.T) { }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgSwapExactAmountOut"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgSwapExactAmountOut"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/swap-exact-amount-out","value":{"routes":[{"token_in_denom":"test"},{"pool_id":"1","token_in_denom":"test2"}],"sender":"%s","token_in_max_amount":"1","token_out":{"amount":"1","denom":"stake"}}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgSwapExactAmountOut", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.gamm.v1beta1.MsgSwapExactAmountOut" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.gamm.v1beta1.MsgSwapExactAmountOut" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/gamm/swap-exact-amount-out", + "value":{ + "routes":[ + { + "token_in_denom":"test" + }, + { + "pool_id":"1", + "token_in_denom":"test2" + } + ], + "sender":"%s", + "token_in_max_amount":"1", + "token_out":{ + "amount":"1", + "denom":"stake" + } + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), gammMsg: &MsgSwapExactAmountOut{ Sender: addr1, Routes: []SwapAmountOutRoute{{ @@ -991,29 +1662,42 @@ func TestAuthzMsg(t *testing.T) { }, } for _, tc := range testCases { - // Authz: Grant Msg - typeURL := sdk.MsgTypeURL(tc.gammMsg) - grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) - require.NoError(t, err) - msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} - require.Equal(t, - tc.expectedGrantSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), - ) - - // Authz: Revoke Msg - msgRevoke := &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} - require.Equal(t, - tc.expectedRevokeSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), - ) - - // Authz: Exec Msg - msgAny, _ := cdctypes.NewAnyWithValue(tc.gammMsg) - msgExec := &authz.MsgExec{Grantee: "cosmos1def", Msgs: []*cdctypes.Any{msgAny}} - require.Equal(t, - tc.expectedExecStrSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), - ) + t.Run(tc.name, func(t *testing.T) { + // Authz: Grant Msg + typeURL := sdk.MsgTypeURL(tc.gammMsg) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + msgGrant := &authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} + + require.Equal(t, + formatJsonStr(tc.expectedGrantSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), + ) + + // Authz: Revoke Msg + msgRevoke := &authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + + require.Equal(t, + formatJsonStr(tc.expectedRevokeSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), + ) + + // Authz: Exec Msg + msgAny, _ := cdctypes.NewAnyWithValue(tc.gammMsg) + msgExec := &authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + + require.Equal(t, + formatJsonStr(tc.expectedExecStrSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), + ) + }) } } + +func formatJsonStr(jsonStrMsg string) string { + ans := strings.ReplaceAll(jsonStrMsg, "\n", "") + ans = strings.ReplaceAll(ans, "\t", "") + ans = strings.ReplaceAll(ans, " ", "") + + return ans +} From 791b5b54e67a758c3514b8191832fbe47e1d34f7 Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 29 Jul 2022 09:03:24 +0700 Subject: [PATCH 08/26] incentives test clean --- x/incentives/types/msgs_test.go | 259 ++++++++++++++++++++++++++++---- 1 file changed, 231 insertions(+), 28 deletions(-) diff --git a/x/incentives/types/msgs_test.go b/x/incentives/types/msgs_test.go index c093fcaaa0b..02d41b2631e 100644 --- a/x/incentives/types/msgs_test.go +++ b/x/incentives/types/msgs_test.go @@ -2,6 +2,7 @@ package types import ( "fmt" + "strings" "testing" time "time" @@ -217,6 +218,7 @@ func TestMsgAddToGauge(t *testing.T) { } } +// // Test authz serialize and de-serializes for incentives msg. func TestAuthzMsg(t *testing.T) { appParams.SetAddressPrefixes() pk1 := ed25519.GenPrivKey().PubKey() @@ -224,16 +226,110 @@ func TestAuthzMsg(t *testing.T) { coin := sdk.NewCoin("stake", sdk.NewInt(1)) someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + const ( + mockGranter string = "cosmos1abc" + mockGrantee string = "cosmos1xyz" + ) + testCases := []struct { + name string expectedGrantSignByteMsg string expectedRevokeSignByteMsg string expectedExecStrSignByteMsg string incentivesMsg sdk.Msg }{ { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.incentives.MsgAddToGauge"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.incentives.MsgAddToGauge"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/incentives/add-to-gauge","value":{"gauge_id":"1","owner":"%s","rewards":[{"amount":"1","denom":"stake"}]}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgAddToGauge", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.incentives.MsgAddToGauge" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.incentives.MsgAddToGauge" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/incentives/add-to-gauge", + "value":{ + "gauge_id":"1", + "owner":"%s", + "rewards":[ + { + "amount":"1", + "denom":"stake" + } + ] + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), incentivesMsg: &MsgAddToGauge{ Owner: addr1, GaugeId: 1, @@ -241,9 +337,103 @@ func TestAuthzMsg(t *testing.T) { }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.incentives.MsgCreateGauge"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.incentives.MsgCreateGauge"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/incentives/create-gauge","value":{"coins":[{"amount":"1","denom":"stake"}],"distribute_to":{"denom":"lptoken","duration":"1000000000","timestamp":"0001-01-01T00:00:00Z"},"num_epochs_paid_over":"1","owner":"%s","start_time":"0001-01-01T01:01:01.000000001Z"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgCreateGauge", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.incentives.MsgCreateGauge" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.incentives.MsgCreateGauge" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/incentives/create-gauge", + "value":{ + "coins":[ + { + "amount":"1", + "denom":"stake" + } + ], + "distribute_to":{ + "denom":"lptoken", + "duration":"1000000000", + "timestamp":"0001-01-01T00:00:00Z" + }, + "num_epochs_paid_over":"1", + "owner":"%s", + "start_time":"0001-01-01T01:01:01.000000001Z" + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), incentivesMsg: &MsgCreateGauge{ IsPerpetual: false, Owner: addr1, @@ -259,29 +449,42 @@ func TestAuthzMsg(t *testing.T) { }, } for _, tc := range testCases { - // Authz: Grant Msg - typeURL := sdk.MsgTypeURL(tc.incentivesMsg) - grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) - require.NoError(t, err) - msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} - require.Equal(t, - tc.expectedGrantSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), - ) + t.Run(tc.name, func(t *testing.T) { + // Authz: Grant Msg + typeURL := sdk.MsgTypeURL(tc.incentivesMsg) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + msgGrant := &authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - // Authz: Revoke Msg - msgRevoke := &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} - require.Equal(t, - tc.expectedRevokeSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), - ) + require.Equal(t, + formatJsonStr(tc.expectedGrantSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), + ) - // Authz: Exec Msg - msgAny, _ := cdctypes.NewAnyWithValue(tc.incentivesMsg) - msgExec := &authz.MsgExec{Grantee: "cosmos1def", Msgs: []*cdctypes.Any{msgAny}} - require.Equal(t, - tc.expectedExecStrSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), - ) + // Authz: Revoke Msg + msgRevoke := &authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + + require.Equal(t, + formatJsonStr(tc.expectedRevokeSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), + ) + + // Authz: Exec Msg + msgAny, _ := cdctypes.NewAnyWithValue(tc.incentivesMsg) + msgExec := &authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + + require.Equal(t, + formatJsonStr(tc.expectedExecStrSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), + ) + }) } } + +func formatJsonStr(jsonStrMsg string) string { + ans := strings.ReplaceAll(jsonStrMsg, "\n", "") + ans = strings.ReplaceAll(ans, "\t", "") + ans = strings.ReplaceAll(ans, " ", "") + + return ans +} From a00b4c5f412aa6837ef54293ecd33123e20a1370 Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 29 Jul 2022 09:17:40 +0700 Subject: [PATCH 09/26] superfluid test clean --- x/superfluid/types/msg_test.go | 512 ++++++++++++++++++++++++++++++--- 1 file changed, 475 insertions(+), 37 deletions(-) diff --git a/x/superfluid/types/msg_test.go b/x/superfluid/types/msg_test.go index 193a48c1e4d..044092c128a 100644 --- a/x/superfluid/types/msg_test.go +++ b/x/superfluid/types/msg_test.go @@ -2,6 +2,7 @@ package types import ( fmt "fmt" + "strings" "testing" "time" @@ -16,6 +17,7 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" ) +// // Test authz serialize and de-serializes for superfluid msg. func TestAuthzMsg(t *testing.T) { appParams.SetAddressPrefixes() pk1 := ed25519.GenPrivKey().PubKey() @@ -23,16 +25,110 @@ func TestAuthzMsg(t *testing.T) { coin := sdk.NewCoin("stake", sdk.NewInt(1)) someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + const ( + mockGranter string = "cosmos1abc" + mockGrantee string = "cosmos1xyz" + ) + testCases := []struct { + name string expectedGrantSignByteMsg string expectedRevokeSignByteMsg string expectedExecStrSignByteMsg string msg sdk.Msg }{ { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgLockAndSuperfluidDelegate"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.superfluid.MsgLockAndSuperfluidDelegate"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/lock-and-superfluid-delegate","value":{"coins":[{"amount":"1","denom":"stake"}],"sender":"%s","val_addr":"valoper1xyz"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgLockAndSuperfluidDelegate", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.superfluid.MsgLockAndSuperfluidDelegate" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.superfluid.MsgLockAndSuperfluidDelegate" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/lock-and-superfluid-delegate", + "value":{ + "coins":[ + { + "amount":"1", + "denom":"stake" + } + ], + "sender":"%s", + "val_addr":"valoper1xyz" + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), msg: &MsgLockAndSuperfluidDelegate{ Sender: addr1, Coins: sdk.NewCoins(coin), @@ -40,9 +136,92 @@ func TestAuthzMsg(t *testing.T) { }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgSuperfluidDelegate"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.superfluid.MsgSuperfluidDelegate"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/superfluid-delegate","value":{"lock_id":"1","sender":"%s","val_addr":"valoper1xyz"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgSuperfluidDelegate", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.superfluid.MsgSuperfluidDelegate" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.superfluid.MsgSuperfluidDelegate" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/superfluid-delegate", + "value":{ + "lock_id":"1", + "sender":"%s", + "val_addr":"valoper1xyz" + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), msg: &MsgSuperfluidDelegate{ Sender: addr1, LockId: 1, @@ -50,27 +229,273 @@ func TestAuthzMsg(t *testing.T) { }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgSuperfluidUnbondLock"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.superfluid.MsgSuperfluidUnbondLock"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/superfluid-unbond-lock","value":{"lock_id":"1","sender":"%s"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgSuperfluidUnbondLock", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.superfluid.MsgSuperfluidUnbondLock" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.superfluid.MsgSuperfluidUnbondLock" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/superfluid-unbond-lock", + "value":{ + "lock_id":"1", + "sender":"%s" + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), msg: &MsgSuperfluidUnbondLock{ Sender: addr1, LockId: 1, }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgSuperfluidUndelegate"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.superfluid.MsgSuperfluidUndelegate"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/superfluid-undelegate","value":{"lock_id":"1","sender":"%s"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgSuperfluidUndelegate", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.superfluid.MsgSuperfluidUndelegate" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.superfluid.MsgSuperfluidUndelegate" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/superfluid-undelegate", + "value":{ + "lock_id":"1", + "sender":"%s" + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), msg: &MsgSuperfluidUndelegate{ Sender: addr1, LockId: 1, }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.superfluid.MsgUnPoolWhitelistedPool"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.superfluid.MsgUnPoolWhitelistedPool"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/unpool-whitelisted-pool","value":{"pool_id":"1","sender":"%s"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgUnPoolWhitelistedPool", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.superfluid.MsgUnPoolWhitelistedPool" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.superfluid.MsgUnPoolWhitelistedPool" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/unpool-whitelisted-pool", + "value":{ + "pool_id":"1", + "sender":"%s" + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), msg: &MsgUnPoolWhitelistedPool{ Sender: addr1, PoolId: 1, @@ -78,29 +503,42 @@ func TestAuthzMsg(t *testing.T) { }, } for _, tc := range testCases { - // Authz: Grant Msg - typeURL := sdk.MsgTypeURL(tc.msg) - grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) - require.NoError(t, err) - msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} - require.Equal(t, - tc.expectedGrantSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), - ) + t.Run(tc.name, func(t *testing.T) { + // Authz: Grant Msg + typeURL := sdk.MsgTypeURL(tc.msg) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + msgGrant := &authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} + + require.Equal(t, + formatJsonStr(tc.expectedGrantSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), + ) + + // Authz: Revoke Msg + msgRevoke := &authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + + require.Equal(t, + formatJsonStr(tc.expectedRevokeSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), + ) - // Authz: Revoke Msg - msgRevoke := &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} - require.Equal(t, - tc.expectedRevokeSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), - ) + // Authz: Exec Msg + msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) + msgExec := &authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} - // Authz: Exec Msg - msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) - msgExec := &authz.MsgExec{Grantee: "cosmos1def", Msgs: []*cdctypes.Any{msgAny}} - require.Equal(t, - tc.expectedExecStrSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), - ) + require.Equal(t, + formatJsonStr(tc.expectedExecStrSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), + ) + }) } } + +func formatJsonStr(jsonStrMsg string) string { + ans := strings.ReplaceAll(jsonStrMsg, "\n", "") + ans = strings.ReplaceAll(ans, "\t", "") + ans = strings.ReplaceAll(ans, " ", "") + + return ans +} From 0f338975f5c63f30d47e74fc280d4f03d3b3defc Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 29 Jul 2022 09:28:23 +0700 Subject: [PATCH 10/26] tokenfactory test clean --- x/tokenfactory/types/msgs_test.go | 385 +++++++++++++++++++++++++++--- 1 file changed, 351 insertions(+), 34 deletions(-) diff --git a/x/tokenfactory/types/msgs_test.go b/x/tokenfactory/types/msgs_test.go index eb4a799a78f..779022fabee 100644 --- a/x/tokenfactory/types/msgs_test.go +++ b/x/tokenfactory/types/msgs_test.go @@ -2,6 +2,7 @@ package types import ( fmt "fmt" + "strings" "testing" "time" @@ -16,6 +17,7 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" ) +// // Test authz serialize and de-serializes for tokenfactory msg. func TestAuthzMsg(t *testing.T) { appParams.SetAddressPrefixes() pk1 := ed25519.GenPrivKey().PubKey() @@ -23,43 +25,345 @@ func TestAuthzMsg(t *testing.T) { coin := sdk.NewCoin("denom", sdk.NewInt(1)) someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + const ( + mockGranter string = "cosmos1abc" + mockGrantee string = "cosmos1xyz" + ) + testCases := []struct { + name string expectedGrantSignByteMsg string expectedRevokeSignByteMsg string expectedExecStrSignByteMsg string msg sdk.Msg }{ { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.tokenfactory.v1beta1.MsgCreateDenom"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgCreateDenom"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"sender":"%s","subdenom":"valoper1xyz"}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgCreateDenom", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.tokenfactory.v1beta1.MsgCreateDenom" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgCreateDenom" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "sender":"%s", + "subdenom":"valoper1xyz" + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), msg: &MsgCreateDenom{ Sender: addr1, Subdenom: "valoper1xyz", }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.tokenfactory.v1beta1.MsgBurn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgBurn"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"amount":{"amount":"1","denom":"denom"},"sender":"%s"}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgBurn", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.tokenfactory.v1beta1.MsgBurn" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgBurn" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "amount":{ + "amount":"1", + "denom":"denom" + }, + "sender":"%s" + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), msg: &MsgBurn{ Sender: addr1, Amount: coin, }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.tokenfactory.v1beta1.MsgMint"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgMint"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"amount":{"amount":"1","denom":"denom"},"sender":"%s"}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgMint", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.tokenfactory.v1beta1.MsgMint" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgMint" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "amount":{ + "amount":"1", + "denom":"denom" + }, + "sender":"%s" + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), msg: &MsgMint{ Sender: addr1, Amount: coin, }, }, { - expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.tokenfactory.v1beta1.MsgChangeAdmin"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, - expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgChangeAdmin"}}],"sequence":"1","timeout_height":"1"}`, - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"denom":"denom","new_admin":"osmo1q8tq5qhrhw6t970egemuuwywhlhpnmdmts6xnu","sender":"%s"}]}}],"sequence":"1","timeout_height":"1"}`, addr1), + name: "MsgChangeAdmin", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgGrant", + "value":{ + "grant":{ + "authorization":{ + "type":"cosmos-sdk/GenericAuthorization", + "value":{ + "msg":"/osmosis.tokenfactory.v1beta1.MsgChangeAdmin" + } + }, + "expiration":"0001-01-01T02:01:01.000000001Z" + }, + "grantee":"%s", + "granter":"%s" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgRevoke", + "value":{ + "grantee":"%s", + "granter":"%s", + "msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgChangeAdmin" + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"%s","msgs":[{"denom":"denom","new_admin":"osmo1q8tq5qhrhw6t970egemuuwywhlhpnmdmts6xnu","sender":"%s"}]}}],"sequence":"1","timeout_height":"1"}`, mockGrantee, addr1), msg: &MsgChangeAdmin{ Sender: addr1, Denom: "denom", @@ -68,29 +372,42 @@ func TestAuthzMsg(t *testing.T) { }, } for _, tc := range testCases { - // Authz: Grant Msg - typeURL := sdk.MsgTypeURL(tc.msg) - grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) - require.NoError(t, err) - msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} - require.Equal(t, - tc.expectedGrantSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), - ) + t.Run(tc.name, func(t *testing.T) { + // Authz: Grant Msg + typeURL := sdk.MsgTypeURL(tc.msg) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + msgGrant := &authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} + + require.Equal(t, + formatJsonStr(tc.expectedGrantSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), + ) + + // Authz: Revoke Msg + msgRevoke := &authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + + require.Equal(t, + formatJsonStr(tc.expectedRevokeSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), + ) - // Authz: Revoke Msg - msgRevoke := &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} - require.Equal(t, - tc.expectedRevokeSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), - ) + // Authz: Exec Msg + msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) + msgExec := &authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} - // Authz: Exec Msg - msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) - msgExec := &authz.MsgExec{Grantee: "cosmos1def", Msgs: []*cdctypes.Any{msgAny}} - require.Equal(t, - tc.expectedExecStrSignByteMsg, - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), - ) + require.Equal(t, + formatJsonStr(tc.expectedExecStrSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), + ) + }) } } + +func formatJsonStr(jsonStrMsg string) string { + ans := strings.ReplaceAll(jsonStrMsg, "\n", "") + ans = strings.ReplaceAll(ans, "\t", "") + ans = strings.ReplaceAll(ans, " ", "") + + return ans +} From 6506326b31381b5b5493224fca7458a0acc9923b Mon Sep 17 00:00:00 2001 From: vuong Date: Sat, 30 Jul 2022 21:27:19 +0700 Subject: [PATCH 11/26] tokenfactory --- x/tokenfactory/types/codec.go | 16 +++++++- x/tokenfactory/types/msgs_test.go | 65 ++++++++++++++++++++++++------- 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/x/tokenfactory/types/codec.go b/x/tokenfactory/types/codec.go index 7b1c1413b37..b086a51e17f 100644 --- a/x/tokenfactory/types/codec.go +++ b/x/tokenfactory/types/codec.go @@ -4,6 +4,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" // this line is used by starport scaffolding # 1 "github.com/cosmos/cosmos-sdk/types/msgservice" @@ -29,4 +30,17 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var ModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) +var ( + amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) +) + +func init() { + RegisterCodec(amino) + // Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be + // used to properly serialize MsgGrant and MsgExec instances + sdk.RegisterLegacyAminoCodec(amino) + RegisterCodec(authzcodec.Amino) + + amino.Seal() +} diff --git a/x/tokenfactory/types/msgs_test.go b/x/tokenfactory/types/msgs_test.go index 779022fabee..d57130be027 100644 --- a/x/tokenfactory/types/msgs_test.go +++ b/x/tokenfactory/types/msgs_test.go @@ -110,8 +110,11 @@ func TestAuthzMsg(t *testing.T) { "grantee":"%s", "msgs":[ { - "sender":"%s", - "subdenom":"valoper1xyz" + "type":"osmosis/tokenfactory/create-denom", + "value":{ + "sender":"%s", + "subdenom":"valoper1xyz" + } } ] } @@ -198,11 +201,14 @@ func TestAuthzMsg(t *testing.T) { "grantee":"%s", "msgs":[ { - "amount":{ - "amount":"1", - "denom":"denom" - }, - "sender":"%s" + "type":"osmosis/tokenfactory/burn", + "value":{ + "amount":{ + "amount":"1", + "denom":"denom" + }, + "sender":"%s" + } } ] } @@ -289,11 +295,14 @@ func TestAuthzMsg(t *testing.T) { "grantee":"%s", "msgs":[ { - "amount":{ - "amount":"1", - "denom":"denom" - }, - "sender":"%s" + "type":"osmosis/tokenfactory/mint", + "value":{ + "amount":{ + "amount":"1", + "denom":"denom" + }, + "sender":"%s" + } } ] } @@ -363,7 +372,37 @@ func TestAuthzMsg(t *testing.T) { "sequence":"1", "timeout_height":"1" }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"%s","msgs":[{"denom":"denom","new_admin":"osmo1q8tq5qhrhw6t970egemuuwywhlhpnmdmts6xnu","sender":"%s"}]}}],"sequence":"1","timeout_height":"1"}`, mockGrantee, addr1), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number":"1", + "chain_id":"foo", + "fee":{ + "amount":[ + + ], + "gas":"0" + }, + "memo":"memo", + "msgs":[ + { + "type":"cosmos-sdk/MsgExec", + "value":{ + "grantee":"%s", + "msgs":[ + { + "type":"osmosis/tokenfactory/change-admin", + "value":{ + "denom":"denom", + "new_admin":"osmo1q8tq5qhrhw6t970egemuuwywhlhpnmdmts6xnu", + "sender":"%s" + } + } + ] + } + } + ], + "sequence":"1", + "timeout_height":"1" + }`, mockGrantee, addr1), msg: &MsgChangeAdmin{ Sender: addr1, Denom: "denom", From f433f3bb3f5eb13947b60826e2f422471b5a5022 Mon Sep 17 00:00:00 2001 From: vuong Date: Sat, 30 Jul 2022 22:26:02 +0700 Subject: [PATCH 12/26] lockup test --- x/lockup/types/msg_test.go | 347 +++++++++++++++++++++++++++++++++++++ 1 file changed, 347 insertions(+) create mode 100644 x/lockup/types/msg_test.go diff --git a/x/lockup/types/msg_test.go b/x/lockup/types/msg_test.go new file mode 100644 index 00000000000..a3e94009daa --- /dev/null +++ b/x/lockup/types/msg_test.go @@ -0,0 +1,347 @@ +package types + +import ( + "fmt" + "strings" + "testing" + "time" + + sdk "github.com/cosmos/cosmos-sdk/types" + + appParams "github.com/osmosis-labs/osmosis/v10/app/params" + + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" + "github.com/cosmos/cosmos-sdk/x/authz" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/ed25519" +) + +// // Test authz serialize and de-serializes for lockup msg. +func TestAuthzMsg(t *testing.T) { + appParams.SetAddressPrefixes() + pk1 := ed25519.GenPrivKey().PubKey() + addr1 := sdk.AccAddress(pk1.Address()).String() + coin := sdk.NewCoin("denom", sdk.NewInt(1)) + someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + + const ( + mockGranter string = "cosmos1abc" + mockGrantee string = "cosmos1xyz" + ) + + testCases := []struct { + name string + expectedGrantSignByteMsg string + expectedRevokeSignByteMsg string + expectedExecStrSignByteMsg string + msg sdk.Msg + }{ + { + name: "MsgLockTokens", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number": "1", + "chain_id": "foo", + "fee": { + "amount": [], + "gas": "0" + }, + "memo": "memo", + "msgs": [ + { + "type": "cosmos-sdk/MsgGrant", + "value": { + "grant": { + "authorization": { + "type": "cosmos-sdk/GenericAuthorization", + "value": { + "msg": "/osmosis.lockup.MsgLockTokens" + } + }, + "expiration": "0001-01-01T02:01:01.000000001Z" + }, + "grantee": "%s", + "granter": "%s" + } + } + ], + "sequence": "1", + "timeout_height": "1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number": "1", + "chain_id": "foo", + "fee": { + "amount": [], + "gas": "0" + }, + "memo": "memo", + "msgs": [ + { + "type": "cosmos-sdk/MsgRevoke", + "value": { + "grantee": "%s", + "granter": "%s", + "msg_type_url": "/osmosis.lockup.MsgLockTokens" + } + } + ], + "sequence": "1", + "timeout_height": "1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number": "1", + "chain_id": "foo", + "fee": { + "amount": [], + "gas": "0" + }, + "memo": "memo", + "msgs": [ + { + "type": "cosmos-sdk/MsgExec", + "value": { + "grantee": "%s", + "msgs": [ + { + "type": "osmosis/lockup/lock-tokens", + "value": { + "coins": [ + { + "amount": "1", + "denom": "denom" + } + ], + "duration": "3600000000000", + "owner": "%s" + } + } + ] + } + } + ], + "sequence": "1", + "timeout_height": "1" + }`, mockGrantee, addr1), + msg: &MsgLockTokens{ + Owner: addr1, + Duration: time.Hour, + Coins: sdk.NewCoins(coin), + }, + }, + { + name: "MsgBeginUnlocking", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number": "1", + "chain_id": "foo", + "fee": { + "amount": [], + "gas": "0" + }, + "memo": "memo", + "msgs": [ + { + "type": "cosmos-sdk/MsgGrant", + "value": { + "grant": { + "authorization": { + "type": "cosmos-sdk/GenericAuthorization", + "value": { + "msg": "/osmosis.lockup.MsgBeginUnlocking" + } + }, + "expiration": "0001-01-01T02:01:01.000000001Z" + }, + "grantee": "%s", + "granter": "%s" + } + } + ], + "sequence": "1", + "timeout_height": "1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number": "1", + "chain_id": "foo", + "fee": { + "amount": [], + "gas": "0" + }, + "memo": "memo", + "msgs": [ + { + "type": "cosmos-sdk/MsgRevoke", + "value": { + "grantee": "%s", + "granter": "%s", + "msg_type_url": "/osmosis.lockup.MsgBeginUnlocking" + } + } + ], + "sequence": "1", + "timeout_height": "1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number": "1", + "chain_id": "foo", + "fee": { + "amount": [], + "gas": "0" + }, + "memo": "memo", + "msgs": [ + { + "type": "cosmos-sdk/MsgExec", + "value": { + "grantee": "%s", + "msgs": [ + { + "type": "osmosis/lockup/begin-unlock-period-lock", + "value": { + "ID": "1", + "coins": [ + { + "amount": "1", + "denom": "denom" + } + ], + "owner": "%s" + } + } + ] + } + } + ], + "sequence": "1", + "timeout_height": "1" + }`, mockGrantee, addr1), + msg: &MsgBeginUnlocking{ + Owner: addr1, + ID: 1, + Coins: sdk.NewCoins(coin), + }, + }, + { + name: "MsgBeginUnlockingAll", + expectedGrantSignByteMsg: fmt.Sprintf(`{ + "account_number": "1", + "chain_id": "foo", + "fee": { + "amount": [], + "gas": "0" + }, + "memo": "memo", + "msgs": [ + { + "type": "cosmos-sdk/MsgGrant", + "value": { + "grant": { + "authorization": { + "type": "cosmos-sdk/GenericAuthorization", + "value": { + "msg": "/osmosis.lockup.MsgBeginUnlockingAll" + } + }, + "expiration": "0001-01-01T02:01:01.000000001Z" + }, + "grantee": "%s", + "granter": "%s" + } + } + ], + "sequence": "1", + "timeout_height": "1" + }`, mockGrantee, mockGranter), + expectedRevokeSignByteMsg: fmt.Sprintf(`{ + "account_number": "1", + "chain_id": "foo", + "fee": { + "amount": [], + "gas": "0" + }, + "memo": "memo", + "msgs": [ + { + "type": "cosmos-sdk/MsgRevoke", + "value": { + "grantee": "%s", + "granter": "%s", + "msg_type_url": "/osmosis.lockup.MsgBeginUnlockingAll" + } + } + ], + "sequence": "1", + "timeout_height": "1" + }`, mockGrantee, mockGranter), + expectedExecStrSignByteMsg: fmt.Sprintf(`{ + "account_number": "1", + "chain_id": "foo", + "fee": { + "amount": [], + "gas": "0" + }, + "memo": "memo", + "msgs": [ + { + "type": "cosmos-sdk/MsgExec", + "value": { + "grantee": "%s", + "msgs": [ + { + "type": "osmosis/lockup/begin-unlock-tokens", + "value": { + "owner": "%s" + } + } + ] + } + } + ], + "sequence": "1", + "timeout_height": "1" + }`, mockGrantee, addr1), + msg: &MsgBeginUnlockingAll{ + Owner: addr1, + }, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // Authz: Grant Msg + typeURL := sdk.MsgTypeURL(tc.msg) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + msgGrant := &authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} + + require.Equal(t, + formatJsonStr(tc.expectedGrantSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), + ) + + // Authz: Revoke Msg + msgRevoke := &authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + + require.Equal(t, + formatJsonStr(tc.expectedRevokeSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), + ) + + // Authz: Exec Msg + msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) + msgExec := &authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + + require.Equal(t, + formatJsonStr(tc.expectedExecStrSignByteMsg), + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), + ) + }) + } +} + +func formatJsonStr(jsonStrMsg string) string { + ans := strings.ReplaceAll(jsonStrMsg, "\n", "") + ans = strings.ReplaceAll(ans, "\t", "") + ans = strings.ReplaceAll(ans, " ", "") + + return ans +} From 2760edf4c3d19ba1be91b1dd006a1fb4e525be5b Mon Sep 17 00:00:00 2001 From: vuong Date: Tue, 2 Aug 2022 09:36:43 +0700 Subject: [PATCH 13/26] refactor test for more maintainable --- x/gamm/types/msgs_test.go | 749 +----------------------------- x/incentives/types/msgs_test.go | 247 ++-------- x/lockup/types/msg_test.go | 305 +----------- x/superfluid/types/msg_test.go | 483 +------------------ x/tokenfactory/types/msgs_test.go | 397 +--------------- 5 files changed, 120 insertions(+), 2061 deletions(-) diff --git a/x/gamm/types/msgs_test.go b/x/gamm/types/msgs_test.go index fdabc1a4a3d..21202de43c0 100644 --- a/x/gamm/types/msgs_test.go +++ b/x/gamm/types/msgs_test.go @@ -1,18 +1,16 @@ package types import ( - "fmt" - "strings" + "encoding/json" "testing" "time" - "github.com/stretchr/testify/require" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" "github.com/cosmos/cosmos-sdk/x/authz" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" + "github.com/stretchr/testify/require" appParams "github.com/osmosis-labs/osmosis/v10/app/params" ) @@ -888,95 +886,11 @@ func TestAuthzMsg(t *testing.T) { ) testCases := []struct { - name string - expectedGrantSignByteMsg string - expectedRevokeSignByteMsg string - expectedExecStrSignByteMsg string - gammMsg sdk.Msg + name string + gammMsg sdk.Msg }{ { name: "MsgExitSwapExternAmountOut", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/gamm/exit-swap-share-amount-in", - "value":{ - "pool_id":"1", - "sender":"%s", - "share_in_amount":"100", - "token_out_denom":"test", - "token_out_min_amount":"100" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), gammMsg: &MsgExitSwapShareAmountIn{ Sender: addr1, PoolId: 1, @@ -987,89 +901,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: `MsgExitSwapExternAmountOut`, - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/gamm/exit-swap-extern-amount-out", - "value":{ - "pool_id":"1", - "sender":"%s", - "share_in_max_amount":"1", - "token_out":{ - "amount":"1", - "denom":"stake" - } - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), gammMsg: &MsgExitSwapExternAmountOut{ Sender: addr1, PoolId: 1, @@ -1079,91 +910,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgExitPool", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.gamm.v1beta1.MsgExitPool" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.gamm.v1beta1.MsgExitPool" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/gamm/exit-pool", - "value":{ - "pool_id":"1", - "sender":"%s", - "share_in_amount":"100", - "token_out_mins":[ - { - "amount":"1", - "denom":"stake" - } - ] - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), gammMsg: &MsgExitPool{ Sender: addr1, PoolId: 1, @@ -1173,91 +919,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgJoinPool", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.gamm.v1beta1.MsgJoinPool" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinPool" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/gamm/join-pool", - "value":{ - "pool_id":"1", - "sender":"%s", - "share_out_amount":"1", - "token_in_maxs":[ - { - "amount":"1", - "denom":"stake" - } - ] - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), gammMsg: &MsgJoinPool{ Sender: addr1, PoolId: 1, @@ -1267,89 +928,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgJoinSwapExternAmountIn", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/gamm/join-swap-extern-amount-in", - "value":{ - "pool_id":"1", - "sender":"%s", - "share_out_min_amount":"1", - "token_in":{ - "amount":"1", - "denom":"stake" - } - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), gammMsg: &MsgJoinSwapExternAmountIn{ Sender: addr1, PoolId: 1, @@ -1359,188 +937,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgJoinSwapShareAmountOut", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.gamm.v1beta1.MsgJoinSwapShareAmountOut" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinSwapShareAmountOut" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/gamm/join-swap-share-amount-out", - "value":{ - "pool_id":"1", - "sender":"%s", - "share_out_amount":"1", - "token_in_denom":"denom", - "token_in_max_amount":"1" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), - gammMsg: &MsgJoinSwapShareAmountOut{ - Sender: addr1, - PoolId: 1, - TokenInDenom: "denom", - ShareOutAmount: sdk.NewInt(1), - TokenInMaxAmount: sdk.NewInt(1), - }, - }, - { - name: "MsgSwapExactAmountIn", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/gamm/swap-exact-amount-in", - "value":{ - "routes":[ - { - "token_out_denom":"test" - }, - { - "pool_id":"1", - "token_out_denom":"test2" - } - ], - "sender":"%s", - "token_in":{ - "amount":"1", - "denom":"stake" - }, - "token_out_min_amount":"1" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), gammMsg: &MsgSwapExactAmountIn{ Sender: addr1, Routes: []SwapAmountInRoute{{ @@ -1556,97 +952,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgSwapExactAmountOut", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.gamm.v1beta1.MsgSwapExactAmountOut" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.gamm.v1beta1.MsgSwapExactAmountOut" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/gamm/swap-exact-amount-out", - "value":{ - "routes":[ - { - "token_in_denom":"test" - }, - { - "pool_id":"1", - "token_in_denom":"test2" - } - ], - "sender":"%s", - "token_in_max_amount":"1", - "token_out":{ - "amount":"1", - "denom":"stake" - } - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), gammMsg: &MsgSwapExactAmountOut{ Sender: addr1, Routes: []SwapAmountOutRoute{{ @@ -1663,41 +968,35 @@ func TestAuthzMsg(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { + var ( + mockMsgGrant authz.MsgGrant + mockMsgRevoke authz.MsgRevoke + mockMsgExec authz.MsgExec + ) + // Authz: Grant Msg typeURL := sdk.MsgTypeURL(tc.gammMsg) grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) require.NoError(t, err) - msgGrant := &authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - require.Equal(t, - formatJsonStr(tc.expectedGrantSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), - ) + msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} + msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) + require.NoError(t, err) // Authz: Revoke Msg - msgRevoke := &authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} - - require.Equal(t, - formatJsonStr(tc.expectedRevokeSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), - ) + msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) + require.NoError(t, err) // Authz: Exec Msg msgAny, _ := cdctypes.NewAnyWithValue(tc.gammMsg) - msgExec := &authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} - - require.Equal(t, - formatJsonStr(tc.expectedExecStrSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), - ) + msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) + err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) + require.NoError(t, err) + require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) }) } } - -func formatJsonStr(jsonStrMsg string) string { - ans := strings.ReplaceAll(jsonStrMsg, "\n", "") - ans = strings.ReplaceAll(ans, "\t", "") - ans = strings.ReplaceAll(ans, " ", "") - - return ans -} diff --git a/x/incentives/types/msgs_test.go b/x/incentives/types/msgs_test.go index 02d41b2631e..ea3ff69e16e 100644 --- a/x/incentives/types/msgs_test.go +++ b/x/incentives/types/msgs_test.go @@ -1,8 +1,7 @@ package types import ( - "fmt" - "strings" + "encoding/json" "testing" time "time" @@ -11,12 +10,12 @@ import ( cdctypes "github.com/cosmos/cosmos-sdk/codec/types" - appParams "github.com/osmosis-labs/osmosis/v10/app/params" - lockuptypes "github.com/osmosis-labs/osmosis/v10/x/lockup/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" "github.com/cosmos/cosmos-sdk/x/authz" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" + + appParams "github.com/osmosis-labs/osmosis/v10/app/params" + lockuptypes "github.com/osmosis-labs/osmosis/v10/x/lockup/types" ) // TestMsgCreatePool tests if valid/invalid create pool messages are properly validated/invalidated @@ -232,104 +231,11 @@ func TestAuthzMsg(t *testing.T) { ) testCases := []struct { - name string - expectedGrantSignByteMsg string - expectedRevokeSignByteMsg string - expectedExecStrSignByteMsg string - incentivesMsg sdk.Msg + name string + incentivesMsg sdk.Msg }{ { name: "MsgAddToGauge", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.incentives.MsgAddToGauge" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.incentives.MsgAddToGauge" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/incentives/add-to-gauge", - "value":{ - "gauge_id":"1", - "owner":"%s", - "rewards":[ - { - "amount":"1", - "denom":"stake" - } - ] - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), incentivesMsg: &MsgAddToGauge{ Owner: addr1, GaugeId: 1, @@ -338,102 +244,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgCreateGauge", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.incentives.MsgCreateGauge" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.incentives.MsgCreateGauge" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/incentives/create-gauge", - "value":{ - "coins":[ - { - "amount":"1", - "denom":"stake" - } - ], - "distribute_to":{ - "denom":"lptoken", - "duration":"1000000000", - "timestamp":"0001-01-01T00:00:00Z" - }, - "num_epochs_paid_over":"1", - "owner":"%s", - "start_time":"0001-01-01T01:01:01.000000001Z" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), incentivesMsg: &MsgCreateGauge{ IsPerpetual: false, Owner: addr1, @@ -450,41 +260,36 @@ func TestAuthzMsg(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { + var ( + mockMsgGrant authz.MsgGrant + mockMsgRevoke authz.MsgRevoke + mockMsgExec authz.MsgExec + ) + // Authz: Grant Msg typeURL := sdk.MsgTypeURL(tc.incentivesMsg) grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) require.NoError(t, err) - msgGrant := &authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - require.Equal(t, - formatJsonStr(tc.expectedGrantSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), - ) + msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} + msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) + require.NoError(t, err) // Authz: Revoke Msg - msgRevoke := &authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} - - require.Equal(t, - formatJsonStr(tc.expectedRevokeSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), - ) + msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) + require.NoError(t, err) // Authz: Exec Msg msgAny, _ := cdctypes.NewAnyWithValue(tc.incentivesMsg) - msgExec := &authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) + err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) + require.NoError(t, err) + require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) - require.Equal(t, - formatJsonStr(tc.expectedExecStrSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), - ) }) } } - -func formatJsonStr(jsonStrMsg string) string { - ans := strings.ReplaceAll(jsonStrMsg, "\n", "") - ans = strings.ReplaceAll(ans, "\t", "") - ans = strings.ReplaceAll(ans, " ", "") - - return ans -} diff --git a/x/lockup/types/msg_test.go b/x/lockup/types/msg_test.go index a3e94009daa..644e2850e0c 100644 --- a/x/lockup/types/msg_test.go +++ b/x/lockup/types/msg_test.go @@ -1,20 +1,19 @@ package types import ( - "fmt" - "strings" + "encoding/json" "testing" "time" sdk "github.com/cosmos/cosmos-sdk/types" - appParams "github.com/osmosis-labs/osmosis/v10/app/params" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" "github.com/cosmos/cosmos-sdk/x/authz" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" + + appParams "github.com/osmosis-labs/osmosis/v10/app/params" ) // // Test authz serialize and de-serializes for lockup msg. @@ -31,98 +30,11 @@ func TestAuthzMsg(t *testing.T) { ) testCases := []struct { - name string - expectedGrantSignByteMsg string - expectedRevokeSignByteMsg string - expectedExecStrSignByteMsg string - msg sdk.Msg + name string + msg sdk.Msg }{ { name: "MsgLockTokens", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number": "1", - "chain_id": "foo", - "fee": { - "amount": [], - "gas": "0" - }, - "memo": "memo", - "msgs": [ - { - "type": "cosmos-sdk/MsgGrant", - "value": { - "grant": { - "authorization": { - "type": "cosmos-sdk/GenericAuthorization", - "value": { - "msg": "/osmosis.lockup.MsgLockTokens" - } - }, - "expiration": "0001-01-01T02:01:01.000000001Z" - }, - "grantee": "%s", - "granter": "%s" - } - } - ], - "sequence": "1", - "timeout_height": "1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number": "1", - "chain_id": "foo", - "fee": { - "amount": [], - "gas": "0" - }, - "memo": "memo", - "msgs": [ - { - "type": "cosmos-sdk/MsgRevoke", - "value": { - "grantee": "%s", - "granter": "%s", - "msg_type_url": "/osmosis.lockup.MsgLockTokens" - } - } - ], - "sequence": "1", - "timeout_height": "1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number": "1", - "chain_id": "foo", - "fee": { - "amount": [], - "gas": "0" - }, - "memo": "memo", - "msgs": [ - { - "type": "cosmos-sdk/MsgExec", - "value": { - "grantee": "%s", - "msgs": [ - { - "type": "osmosis/lockup/lock-tokens", - "value": { - "coins": [ - { - "amount": "1", - "denom": "denom" - } - ], - "duration": "3600000000000", - "owner": "%s" - } - } - ] - } - } - ], - "sequence": "1", - "timeout_height": "1" - }`, mockGrantee, addr1), msg: &MsgLockTokens{ Owner: addr1, Duration: time.Hour, @@ -131,90 +43,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgBeginUnlocking", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number": "1", - "chain_id": "foo", - "fee": { - "amount": [], - "gas": "0" - }, - "memo": "memo", - "msgs": [ - { - "type": "cosmos-sdk/MsgGrant", - "value": { - "grant": { - "authorization": { - "type": "cosmos-sdk/GenericAuthorization", - "value": { - "msg": "/osmosis.lockup.MsgBeginUnlocking" - } - }, - "expiration": "0001-01-01T02:01:01.000000001Z" - }, - "grantee": "%s", - "granter": "%s" - } - } - ], - "sequence": "1", - "timeout_height": "1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number": "1", - "chain_id": "foo", - "fee": { - "amount": [], - "gas": "0" - }, - "memo": "memo", - "msgs": [ - { - "type": "cosmos-sdk/MsgRevoke", - "value": { - "grantee": "%s", - "granter": "%s", - "msg_type_url": "/osmosis.lockup.MsgBeginUnlocking" - } - } - ], - "sequence": "1", - "timeout_height": "1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number": "1", - "chain_id": "foo", - "fee": { - "amount": [], - "gas": "0" - }, - "memo": "memo", - "msgs": [ - { - "type": "cosmos-sdk/MsgExec", - "value": { - "grantee": "%s", - "msgs": [ - { - "type": "osmosis/lockup/begin-unlock-period-lock", - "value": { - "ID": "1", - "coins": [ - { - "amount": "1", - "denom": "denom" - } - ], - "owner": "%s" - } - } - ] - } - } - ], - "sequence": "1", - "timeout_height": "1" - }`, mockGrantee, addr1), msg: &MsgBeginUnlocking{ Owner: addr1, ID: 1, @@ -223,83 +51,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgBeginUnlockingAll", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number": "1", - "chain_id": "foo", - "fee": { - "amount": [], - "gas": "0" - }, - "memo": "memo", - "msgs": [ - { - "type": "cosmos-sdk/MsgGrant", - "value": { - "grant": { - "authorization": { - "type": "cosmos-sdk/GenericAuthorization", - "value": { - "msg": "/osmosis.lockup.MsgBeginUnlockingAll" - } - }, - "expiration": "0001-01-01T02:01:01.000000001Z" - }, - "grantee": "%s", - "granter": "%s" - } - } - ], - "sequence": "1", - "timeout_height": "1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number": "1", - "chain_id": "foo", - "fee": { - "amount": [], - "gas": "0" - }, - "memo": "memo", - "msgs": [ - { - "type": "cosmos-sdk/MsgRevoke", - "value": { - "grantee": "%s", - "granter": "%s", - "msg_type_url": "/osmosis.lockup.MsgBeginUnlockingAll" - } - } - ], - "sequence": "1", - "timeout_height": "1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number": "1", - "chain_id": "foo", - "fee": { - "amount": [], - "gas": "0" - }, - "memo": "memo", - "msgs": [ - { - "type": "cosmos-sdk/MsgExec", - "value": { - "grantee": "%s", - "msgs": [ - { - "type": "osmosis/lockup/begin-unlock-tokens", - "value": { - "owner": "%s" - } - } - ] - } - } - ], - "sequence": "1", - "timeout_height": "1" - }`, mockGrantee, addr1), msg: &MsgBeginUnlockingAll{ Owner: addr1, }, @@ -307,41 +58,35 @@ func TestAuthzMsg(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { + var ( + mockMsgGrant authz.MsgGrant + mockMsgRevoke authz.MsgRevoke + mockMsgExec authz.MsgExec + ) + // Authz: Grant Msg typeURL := sdk.MsgTypeURL(tc.msg) grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) require.NoError(t, err) - msgGrant := &authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - require.Equal(t, - formatJsonStr(tc.expectedGrantSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), - ) + msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} + msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) + require.NoError(t, err) // Authz: Revoke Msg - msgRevoke := &authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} - - require.Equal(t, - formatJsonStr(tc.expectedRevokeSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), - ) + msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) + require.NoError(t, err) // Authz: Exec Msg msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) - msgExec := &authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} - - require.Equal(t, - formatJsonStr(tc.expectedExecStrSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), - ) + msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) + err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) + require.NoError(t, err) + require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) }) } } - -func formatJsonStr(jsonStrMsg string) string { - ans := strings.ReplaceAll(jsonStrMsg, "\n", "") - ans = strings.ReplaceAll(ans, "\t", "") - ans = strings.ReplaceAll(ans, " ", "") - - return ans -} diff --git a/x/superfluid/types/msg_test.go b/x/superfluid/types/msg_test.go index 044092c128a..4ead4dc084d 100644 --- a/x/superfluid/types/msg_test.go +++ b/x/superfluid/types/msg_test.go @@ -1,17 +1,16 @@ package types import ( - fmt "fmt" - "strings" + "encoding/json" "testing" "time" sdk "github.com/cosmos/cosmos-sdk/types" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" appParams "github.com/osmosis-labs/osmosis/v10/app/params" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" "github.com/cosmos/cosmos-sdk/x/authz" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" @@ -31,104 +30,11 @@ func TestAuthzMsg(t *testing.T) { ) testCases := []struct { - name string - expectedGrantSignByteMsg string - expectedRevokeSignByteMsg string - expectedExecStrSignByteMsg string - msg sdk.Msg + name string + msg sdk.Msg }{ { name: "MsgLockAndSuperfluidDelegate", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.superfluid.MsgLockAndSuperfluidDelegate" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.superfluid.MsgLockAndSuperfluidDelegate" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/lock-and-superfluid-delegate", - "value":{ - "coins":[ - { - "amount":"1", - "denom":"stake" - } - ], - "sender":"%s", - "val_addr":"valoper1xyz" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), msg: &MsgLockAndSuperfluidDelegate{ Sender: addr1, Coins: sdk.NewCoins(coin), @@ -137,91 +43,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgSuperfluidDelegate", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.superfluid.MsgSuperfluidDelegate" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.superfluid.MsgSuperfluidDelegate" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/superfluid-delegate", - "value":{ - "lock_id":"1", - "sender":"%s", - "val_addr":"valoper1xyz" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), msg: &MsgSuperfluidDelegate{ Sender: addr1, LockId: 1, @@ -230,90 +51,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgSuperfluidUnbondLock", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.superfluid.MsgSuperfluidUnbondLock" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.superfluid.MsgSuperfluidUnbondLock" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/superfluid-unbond-lock", - "value":{ - "lock_id":"1", - "sender":"%s" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), msg: &MsgSuperfluidUnbondLock{ Sender: addr1, LockId: 1, @@ -321,90 +58,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgSuperfluidUndelegate", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.superfluid.MsgSuperfluidUndelegate" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.superfluid.MsgSuperfluidUndelegate" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/superfluid-undelegate", - "value":{ - "lock_id":"1", - "sender":"%s" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), msg: &MsgSuperfluidUndelegate{ Sender: addr1, LockId: 1, @@ -412,90 +65,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgUnPoolWhitelistedPool", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.superfluid.MsgUnPoolWhitelistedPool" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.superfluid.MsgUnPoolWhitelistedPool" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/unpool-whitelisted-pool", - "value":{ - "pool_id":"1", - "sender":"%s" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), msg: &MsgUnPoolWhitelistedPool{ Sender: addr1, PoolId: 1, @@ -504,41 +73,35 @@ func TestAuthzMsg(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { + var ( + mockMsgGrant authz.MsgGrant + mockMsgRevoke authz.MsgRevoke + mockMsgExec authz.MsgExec + ) + // Authz: Grant Msg typeURL := sdk.MsgTypeURL(tc.msg) grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) require.NoError(t, err) - msgGrant := &authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - require.Equal(t, - formatJsonStr(tc.expectedGrantSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), - ) + msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} + msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) + require.NoError(t, err) // Authz: Revoke Msg - msgRevoke := &authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} - - require.Equal(t, - formatJsonStr(tc.expectedRevokeSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), - ) + msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) + require.NoError(t, err) // Authz: Exec Msg msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) - msgExec := &authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} - - require.Equal(t, - formatJsonStr(tc.expectedExecStrSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), - ) + msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) + err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) + require.NoError(t, err) + require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) }) } } - -func formatJsonStr(jsonStrMsg string) string { - ans := strings.ReplaceAll(jsonStrMsg, "\n", "") - ans = strings.ReplaceAll(ans, "\t", "") - ans = strings.ReplaceAll(ans, " ", "") - - return ans -} diff --git a/x/tokenfactory/types/msgs_test.go b/x/tokenfactory/types/msgs_test.go index d57130be027..fede16ea69e 100644 --- a/x/tokenfactory/types/msgs_test.go +++ b/x/tokenfactory/types/msgs_test.go @@ -1,8 +1,7 @@ package types import ( - fmt "fmt" - "strings" + "encoding/json" "testing" "time" @@ -11,7 +10,6 @@ import ( appParams "github.com/osmosis-labs/osmosis/v10/app/params" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" "github.com/cosmos/cosmos-sdk/x/authz" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" @@ -31,98 +29,11 @@ func TestAuthzMsg(t *testing.T) { ) testCases := []struct { - name string - expectedGrantSignByteMsg string - expectedRevokeSignByteMsg string - expectedExecStrSignByteMsg string - msg sdk.Msg + name string + msg sdk.Msg }{ { name: "MsgCreateDenom", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.tokenfactory.v1beta1.MsgCreateDenom" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgCreateDenom" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/tokenfactory/create-denom", - "value":{ - "sender":"%s", - "subdenom":"valoper1xyz" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), msg: &MsgCreateDenom{ Sender: addr1, Subdenom: "valoper1xyz", @@ -130,93 +41,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgBurn", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.tokenfactory.v1beta1.MsgBurn" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgBurn" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/tokenfactory/burn", - "value":{ - "amount":{ - "amount":"1", - "denom":"denom" - }, - "sender":"%s" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), msg: &MsgBurn{ Sender: addr1, Amount: coin, @@ -224,93 +48,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgMint", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.tokenfactory.v1beta1.MsgMint" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgMint" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/tokenfactory/mint", - "value":{ - "amount":{ - "amount":"1", - "denom":"denom" - }, - "sender":"%s" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), msg: &MsgMint{ Sender: addr1, Amount: coin, @@ -318,91 +55,6 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgChangeAdmin", - expectedGrantSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgGrant", - "value":{ - "grant":{ - "authorization":{ - "type":"cosmos-sdk/GenericAuthorization", - "value":{ - "msg":"/osmosis.tokenfactory.v1beta1.MsgChangeAdmin" - } - }, - "expiration":"0001-01-01T02:01:01.000000001Z" - }, - "grantee":"%s", - "granter":"%s" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedRevokeSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgRevoke", - "value":{ - "grantee":"%s", - "granter":"%s", - "msg_type_url":"/osmosis.tokenfactory.v1beta1.MsgChangeAdmin" - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, mockGranter), - expectedExecStrSignByteMsg: fmt.Sprintf(`{ - "account_number":"1", - "chain_id":"foo", - "fee":{ - "amount":[ - - ], - "gas":"0" - }, - "memo":"memo", - "msgs":[ - { - "type":"cosmos-sdk/MsgExec", - "value":{ - "grantee":"%s", - "msgs":[ - { - "type":"osmosis/tokenfactory/change-admin", - "value":{ - "denom":"denom", - "new_admin":"osmo1q8tq5qhrhw6t970egemuuwywhlhpnmdmts6xnu", - "sender":"%s" - } - } - ] - } - } - ], - "sequence":"1", - "timeout_height":"1" - }`, mockGrantee, addr1), msg: &MsgChangeAdmin{ Sender: addr1, Denom: "denom", @@ -412,41 +64,36 @@ func TestAuthzMsg(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { + var ( + mockMsgGrant authz.MsgGrant + mockMsgRevoke authz.MsgRevoke + mockMsgExec authz.MsgExec + ) + // Authz: Grant Msg typeURL := sdk.MsgTypeURL(tc.msg) grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) require.NoError(t, err) - msgGrant := &authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - require.Equal(t, - formatJsonStr(tc.expectedGrantSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), - ) + msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} + msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) + require.NoError(t, err) // Authz: Revoke Msg - msgRevoke := &authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} - - require.Equal(t, - formatJsonStr(tc.expectedRevokeSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), - ) + msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) + require.NoError(t, err) // Authz: Exec Msg msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) - msgExec := &authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) + err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) + require.NoError(t, err) + require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) - require.Equal(t, - formatJsonStr(tc.expectedExecStrSignByteMsg), - string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), - ) }) } } - -func formatJsonStr(jsonStrMsg string) string { - ans := strings.ReplaceAll(jsonStrMsg, "\n", "") - ans = strings.ReplaceAll(ans, "\t", "") - ans = strings.ReplaceAll(ans, " ", "") - - return ans -} From f086c52773579bb289bdd39934d4f1417a72309b Mon Sep 17 00:00:00 2001 From: vuong <56973102+vuong177@users.noreply.github.com> Date: Tue, 2 Aug 2022 11:12:49 +0700 Subject: [PATCH 14/26] Update msgs_test.go --- x/tokenfactory/types/msgs_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/tokenfactory/types/msgs_test.go b/x/tokenfactory/types/msgs_test.go index fede16ea69e..7d7d490f52c 100644 --- a/x/tokenfactory/types/msgs_test.go +++ b/x/tokenfactory/types/msgs_test.go @@ -11,6 +11,7 @@ import ( cdctypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/x/authz" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" ) From 2a47d2248fc854043510c859b745400ca737a2ca Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 5 Aug 2022 13:34:48 +0700 Subject: [PATCH 15/26] refactor authz test in gamm module --- app/apptesting/test_suite.go | 41 ++++++ x/gamm/types/msgs_test.go | 276 +++++++++++++++-------------------- 2 files changed, 161 insertions(+), 156 deletions(-) diff --git a/app/apptesting/test_suite.go b/app/apptesting/test_suite.go index 9f7a6ecf6eb..d3403036819 100644 --- a/app/apptesting/test_suite.go +++ b/app/apptesting/test_suite.go @@ -1,21 +1,27 @@ package apptesting import ( + "encoding/json" "fmt" + "testing" "time" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/store/rootmulti" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/tx/signing" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" + "github.com/cosmos/cosmos-sdk/x/authz" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" "github.com/cosmos/cosmos-sdk/x/staking/teststaking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/crypto/ed25519" @@ -301,3 +307,38 @@ func CreateRandomAccounts(numAccts int) []sdk.AccAddress { return testAddrs } + +func TestMessageAuthzSerialization(t *testing.T, msg sdk.Msg) { + someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + var ( + mockMsgGrant authz.MsgGrant + mockMsgRevoke authz.MsgRevoke + mockMsgExec authz.MsgExec + mockGranter string = "cosmos1abc" + mockGrantee string = "cosmos1xyz" + ) + + // Authz: Grant Msg + typeURL := sdk.MsgTypeURL(msg) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + + msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} + msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) + require.NoError(t, err) + + // Authz: Revoke Msg + msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} + msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) + err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) + require.NoError(t, err) + + // Authz: Exec Msg + msgAny, _ := cdctypes.NewAnyWithValue(msg) + msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} + execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) + err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) + require.NoError(t, err) + require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) +} diff --git a/x/gamm/types/msgs_test.go b/x/gamm/types/msgs_test.go index 21202de43c0..97facc2cd8b 100644 --- a/x/gamm/types/msgs_test.go +++ b/x/gamm/types/msgs_test.go @@ -1,17 +1,15 @@ -package types +package types_test import ( - "encoding/json" "testing" - "time" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/authz" - authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" "github.com/stretchr/testify/require" + gammtypes "github.com/osmosis-labs/osmosis/v10/x/gamm/types" + + "github.com/osmosis-labs/osmosis/v10/app/apptesting" appParams "github.com/osmosis-labs/osmosis/v10/app/params" ) @@ -21,10 +19,10 @@ func TestMsgSwapExactAmountIn(t *testing.T) { addr1 := sdk.AccAddress(pk1.Address()).String() invalidAddr := sdk.AccAddress("invalid") - createMsg := func(after func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn) MsgSwapExactAmountIn { - properMsg := MsgSwapExactAmountIn{ + createMsg := func(after func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { + properMsg := gammtypes.MsgSwapExactAmountIn{ Sender: addr1, - Routes: []SwapAmountInRoute{{ + Routes: []gammtypes.SwapAmountInRoute{{ PoolId: 0, TokenOutDenom: "test", }, { @@ -38,12 +36,12 @@ func TestMsgSwapExactAmountIn(t *testing.T) { return after(properMsg) } - msg := createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { + msg := createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { // Do nothing return msg }) - require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Route(), gammtypes.RouterKey) require.Equal(t, msg.Type(), "swap_exact_amount_in") signers := msg.GetSigners() require.Equal(t, len(signers), 1) @@ -51,12 +49,12 @@ func TestMsgSwapExactAmountIn(t *testing.T) { tests := []struct { name string - msg MsgSwapExactAmountIn + msg gammtypes.MsgSwapExactAmountIn expectPass bool }{ { name: "proper msg", - msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { // Do nothing return msg }), @@ -64,7 +62,7 @@ func TestMsgSwapExactAmountIn(t *testing.T) { }, { name: "invalid sender", - msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { msg.Sender = invalidAddr.String() return msg }), @@ -72,7 +70,7 @@ func TestMsgSwapExactAmountIn(t *testing.T) { }, { name: "empty routes", - msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { msg.Routes = nil return msg }), @@ -80,15 +78,15 @@ func TestMsgSwapExactAmountIn(t *testing.T) { }, { name: "empty routes2", - msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { - msg.Routes = []SwapAmountInRoute{} + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { + msg.Routes = []gammtypes.SwapAmountInRoute{} return msg }), expectPass: false, }, { name: "invalid denom", - msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { msg.Routes[1].TokenOutDenom = "1" return msg }), @@ -96,7 +94,7 @@ func TestMsgSwapExactAmountIn(t *testing.T) { }, { name: "invalid denom2", - msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { msg.TokenIn.Denom = "1" return msg }), @@ -104,7 +102,7 @@ func TestMsgSwapExactAmountIn(t *testing.T) { }, { name: "zero amount token", - msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { msg.TokenIn.Amount = sdk.NewInt(0) return msg }), @@ -112,7 +110,7 @@ func TestMsgSwapExactAmountIn(t *testing.T) { }, { name: "negative amount token", - msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { msg.TokenIn.Amount = sdk.NewInt(-10) return msg }), @@ -120,7 +118,7 @@ func TestMsgSwapExactAmountIn(t *testing.T) { }, { name: "zero amount criteria", - msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { msg.TokenOutMinAmount = sdk.NewInt(0) return msg }), @@ -128,7 +126,7 @@ func TestMsgSwapExactAmountIn(t *testing.T) { }, { name: "negative amount criteria", - msg: createMsg(func(msg MsgSwapExactAmountIn) MsgSwapExactAmountIn { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountIn) gammtypes.MsgSwapExactAmountIn { msg.TokenOutMinAmount = sdk.NewInt(-10) return msg }), @@ -151,10 +149,10 @@ func TestMsgSwapExactAmountOut(t *testing.T) { addr1 := sdk.AccAddress(pk1.Address()).String() invalidAddr := sdk.AccAddress("invalid") - createMsg := func(after func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut) MsgSwapExactAmountOut { - properMsg := MsgSwapExactAmountOut{ + createMsg := func(after func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { + properMsg := gammtypes.MsgSwapExactAmountOut{ Sender: addr1, - Routes: []SwapAmountOutRoute{{ + Routes: []gammtypes.SwapAmountOutRoute{{ PoolId: 0, TokenInDenom: "test", }, { @@ -168,12 +166,12 @@ func TestMsgSwapExactAmountOut(t *testing.T) { return after(properMsg) } - msg := createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { + msg := createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { // Do nothing return msg }) - require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Route(), gammtypes.RouterKey) require.Equal(t, msg.Type(), "swap_exact_amount_out") signers := msg.GetSigners() require.Equal(t, len(signers), 1) @@ -181,12 +179,12 @@ func TestMsgSwapExactAmountOut(t *testing.T) { tests := []struct { name string - msg MsgSwapExactAmountOut + msg gammtypes.MsgSwapExactAmountOut expectPass bool }{ { name: "proper msg", - msg: createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { // Do nothing return msg }), @@ -194,7 +192,7 @@ func TestMsgSwapExactAmountOut(t *testing.T) { }, { name: "invalid sender", - msg: createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { msg.Sender = invalidAddr.String() return msg }), @@ -202,7 +200,7 @@ func TestMsgSwapExactAmountOut(t *testing.T) { }, { name: "empty routes", - msg: createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { msg.Routes = nil return msg }), @@ -210,15 +208,15 @@ func TestMsgSwapExactAmountOut(t *testing.T) { }, { name: "empty routes2", - msg: createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { - msg.Routes = []SwapAmountOutRoute{} + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { + msg.Routes = []gammtypes.SwapAmountOutRoute{} return msg }), expectPass: false, }, { name: "invalid denom", - msg: createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { msg.Routes[1].TokenInDenom = "1" return msg }), @@ -226,7 +224,7 @@ func TestMsgSwapExactAmountOut(t *testing.T) { }, { name: "invalid denom", - msg: createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { msg.TokenOut.Denom = "1" return msg }), @@ -234,7 +232,7 @@ func TestMsgSwapExactAmountOut(t *testing.T) { }, { name: "zero amount token", - msg: createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { msg.TokenOut.Amount = sdk.NewInt(0) return msg }), @@ -242,7 +240,7 @@ func TestMsgSwapExactAmountOut(t *testing.T) { }, { name: "negative amount token", - msg: createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { msg.TokenOut.Amount = sdk.NewInt(-10) return msg }), @@ -250,7 +248,7 @@ func TestMsgSwapExactAmountOut(t *testing.T) { }, { name: "zero amount criteria", - msg: createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { msg.TokenInMaxAmount = sdk.NewInt(0) return msg }), @@ -258,7 +256,7 @@ func TestMsgSwapExactAmountOut(t *testing.T) { }, { name: "negative amount criteria", - msg: createMsg(func(msg MsgSwapExactAmountOut) MsgSwapExactAmountOut { + msg: createMsg(func(msg gammtypes.MsgSwapExactAmountOut) gammtypes.MsgSwapExactAmountOut { msg.TokenInMaxAmount = sdk.NewInt(-10) return msg }), @@ -281,8 +279,8 @@ func TestMsgJoinPool(t *testing.T) { addr1 := sdk.AccAddress(pk1.Address()).String() invalidAddr := sdk.AccAddress("invalid") - createMsg := func(after func(msg MsgJoinPool) MsgJoinPool) MsgJoinPool { - properMsg := MsgJoinPool{ + createMsg := func(after func(msg gammtypes.MsgJoinPool) gammtypes.MsgJoinPool) gammtypes.MsgJoinPool { + properMsg := gammtypes.MsgJoinPool{ Sender: addr1, PoolId: 1, ShareOutAmount: sdk.NewInt(10), @@ -292,12 +290,12 @@ func TestMsgJoinPool(t *testing.T) { return after(properMsg) } - msg := createMsg(func(msg MsgJoinPool) MsgJoinPool { + msg := createMsg(func(msg gammtypes.MsgJoinPool) gammtypes.MsgJoinPool { // Do nothing return msg }) - require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Route(), gammtypes.RouterKey) require.Equal(t, msg.Type(), "join_pool") signers := msg.GetSigners() require.Equal(t, len(signers), 1) @@ -305,12 +303,12 @@ func TestMsgJoinPool(t *testing.T) { tests := []struct { name string - msg MsgJoinPool + msg gammtypes.MsgJoinPool expectPass bool }{ { name: "proper msg", - msg: createMsg(func(msg MsgJoinPool) MsgJoinPool { + msg: createMsg(func(msg gammtypes.MsgJoinPool) gammtypes.MsgJoinPool { // Do nothing return msg }), @@ -318,7 +316,7 @@ func TestMsgJoinPool(t *testing.T) { }, { name: "invalid sender", - msg: createMsg(func(msg MsgJoinPool) MsgJoinPool { + msg: createMsg(func(msg gammtypes.MsgJoinPool) gammtypes.MsgJoinPool { msg.Sender = invalidAddr.String() return msg }), @@ -326,7 +324,7 @@ func TestMsgJoinPool(t *testing.T) { }, { name: "negative requirement", - msg: createMsg(func(msg MsgJoinPool) MsgJoinPool { + msg: createMsg(func(msg gammtypes.MsgJoinPool) gammtypes.MsgJoinPool { msg.ShareOutAmount = sdk.NewInt(-10) return msg }), @@ -334,7 +332,7 @@ func TestMsgJoinPool(t *testing.T) { }, { name: "zero amount", - msg: createMsg(func(msg MsgJoinPool) MsgJoinPool { + msg: createMsg(func(msg gammtypes.MsgJoinPool) gammtypes.MsgJoinPool { msg.TokenInMaxs[1].Amount = sdk.NewInt(0) return msg }), @@ -342,7 +340,7 @@ func TestMsgJoinPool(t *testing.T) { }, { name: "negative amount", - msg: createMsg(func(msg MsgJoinPool) MsgJoinPool { + msg: createMsg(func(msg gammtypes.MsgJoinPool) gammtypes.MsgJoinPool { msg.TokenInMaxs[1].Amount = sdk.NewInt(-10) return msg }), @@ -350,7 +348,7 @@ func TestMsgJoinPool(t *testing.T) { }, { name: "'empty token max in' can pass", - msg: createMsg(func(msg MsgJoinPool) MsgJoinPool { + msg: createMsg(func(msg gammtypes.MsgJoinPool) gammtypes.MsgJoinPool { msg.TokenInMaxs = nil return msg }), @@ -358,7 +356,7 @@ func TestMsgJoinPool(t *testing.T) { }, { name: "'empty token max in' can pass 2", - msg: createMsg(func(msg MsgJoinPool) MsgJoinPool { + msg: createMsg(func(msg gammtypes.MsgJoinPool) gammtypes.MsgJoinPool { msg.TokenInMaxs = sdk.Coins{} return msg }), @@ -381,8 +379,8 @@ func TestMsgExitPool(t *testing.T) { addr1 := sdk.AccAddress(pk1.Address()).String() invalidAddr := sdk.AccAddress("invalid") - createMsg := func(after func(msg MsgExitPool) MsgExitPool) MsgExitPool { - properMsg := MsgExitPool{ + createMsg := func(after func(msg gammtypes.MsgExitPool) gammtypes.MsgExitPool) gammtypes.MsgExitPool { + properMsg := gammtypes.MsgExitPool{ Sender: addr1, PoolId: 1, ShareInAmount: sdk.NewInt(10), @@ -391,12 +389,12 @@ func TestMsgExitPool(t *testing.T) { return after(properMsg) } - msg := createMsg(func(msg MsgExitPool) MsgExitPool { + msg := createMsg(func(msg gammtypes.MsgExitPool) gammtypes.MsgExitPool { // Do nothing return msg }) - require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Route(), gammtypes.RouterKey) require.Equal(t, msg.Type(), "exit_pool") signers := msg.GetSigners() require.Equal(t, len(signers), 1) @@ -404,12 +402,12 @@ func TestMsgExitPool(t *testing.T) { tests := []struct { name string - msg MsgExitPool + msg gammtypes.MsgExitPool expectPass bool }{ { name: "proper msg", - msg: createMsg(func(msg MsgExitPool) MsgExitPool { + msg: createMsg(func(msg gammtypes.MsgExitPool) gammtypes.MsgExitPool { // Do nothing return msg }), @@ -417,7 +415,7 @@ func TestMsgExitPool(t *testing.T) { }, { name: "invalid sender", - msg: createMsg(func(msg MsgExitPool) MsgExitPool { + msg: createMsg(func(msg gammtypes.MsgExitPool) gammtypes.MsgExitPool { msg.Sender = invalidAddr.String() return msg }), @@ -425,7 +423,7 @@ func TestMsgExitPool(t *testing.T) { }, { name: "negative requirement", - msg: createMsg(func(msg MsgExitPool) MsgExitPool { + msg: createMsg(func(msg gammtypes.MsgExitPool) gammtypes.MsgExitPool { msg.ShareInAmount = sdk.NewInt(-10) return msg }), @@ -433,7 +431,7 @@ func TestMsgExitPool(t *testing.T) { }, { name: "zero amount", - msg: createMsg(func(msg MsgExitPool) MsgExitPool { + msg: createMsg(func(msg gammtypes.MsgExitPool) gammtypes.MsgExitPool { msg.TokenOutMins[1].Amount = sdk.NewInt(0) return msg }), @@ -441,7 +439,7 @@ func TestMsgExitPool(t *testing.T) { }, { name: "negative amount", - msg: createMsg(func(msg MsgExitPool) MsgExitPool { + msg: createMsg(func(msg gammtypes.MsgExitPool) gammtypes.MsgExitPool { msg.TokenOutMins[1].Amount = sdk.NewInt(-10) return msg }), @@ -449,7 +447,7 @@ func TestMsgExitPool(t *testing.T) { }, { name: "'empty token min out' can pass", - msg: createMsg(func(msg MsgExitPool) MsgExitPool { + msg: createMsg(func(msg gammtypes.MsgExitPool) gammtypes.MsgExitPool { msg.TokenOutMins = nil return msg }), @@ -457,7 +455,7 @@ func TestMsgExitPool(t *testing.T) { }, { name: "'empty token min out' can pass 2", - msg: createMsg(func(msg MsgExitPool) MsgExitPool { + msg: createMsg(func(msg gammtypes.MsgExitPool) gammtypes.MsgExitPool { msg.TokenOutMins = sdk.Coins{} return msg }), @@ -480,8 +478,8 @@ func TestMsgJoinSwapExternAmountIn(t *testing.T) { addr1 := sdk.AccAddress(pk1.Address()).String() invalidAddr := sdk.AccAddress("invalid") - createMsg := func(after func(msg MsgJoinSwapExternAmountIn) MsgJoinSwapExternAmountIn) MsgJoinSwapExternAmountIn { - properMsg := MsgJoinSwapExternAmountIn{ + createMsg := func(after func(msg gammtypes.MsgJoinSwapExternAmountIn) gammtypes.MsgJoinSwapExternAmountIn) gammtypes.MsgJoinSwapExternAmountIn { + properMsg := gammtypes.MsgJoinSwapExternAmountIn{ Sender: addr1, PoolId: 1, TokenIn: sdk.NewCoin("test", sdk.NewInt(100)), @@ -490,12 +488,12 @@ func TestMsgJoinSwapExternAmountIn(t *testing.T) { return after(properMsg) } - msg := createMsg(func(msg MsgJoinSwapExternAmountIn) MsgJoinSwapExternAmountIn { + msg := createMsg(func(msg gammtypes.MsgJoinSwapExternAmountIn) gammtypes.MsgJoinSwapExternAmountIn { // Do nothing return msg }) - require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Route(), gammtypes.RouterKey) require.Equal(t, msg.Type(), "join_swap_extern_amount_in") signers := msg.GetSigners() require.Equal(t, len(signers), 1) @@ -503,12 +501,12 @@ func TestMsgJoinSwapExternAmountIn(t *testing.T) { tests := []struct { name string - msg MsgJoinSwapExternAmountIn + msg gammtypes.MsgJoinSwapExternAmountIn expectPass bool }{ { name: "proper msg", - msg: createMsg(func(msg MsgJoinSwapExternAmountIn) MsgJoinSwapExternAmountIn { + msg: createMsg(func(msg gammtypes.MsgJoinSwapExternAmountIn) gammtypes.MsgJoinSwapExternAmountIn { // Do nothing return msg }), @@ -516,7 +514,7 @@ func TestMsgJoinSwapExternAmountIn(t *testing.T) { }, { name: "invalid sender", - msg: createMsg(func(msg MsgJoinSwapExternAmountIn) MsgJoinSwapExternAmountIn { + msg: createMsg(func(msg gammtypes.MsgJoinSwapExternAmountIn) gammtypes.MsgJoinSwapExternAmountIn { msg.Sender = invalidAddr.String() return msg }), @@ -524,7 +522,7 @@ func TestMsgJoinSwapExternAmountIn(t *testing.T) { }, { name: "invalid denom", - msg: createMsg(func(msg MsgJoinSwapExternAmountIn) MsgJoinSwapExternAmountIn { + msg: createMsg(func(msg gammtypes.MsgJoinSwapExternAmountIn) gammtypes.MsgJoinSwapExternAmountIn { msg.TokenIn.Denom = "1" return msg }), @@ -532,7 +530,7 @@ func TestMsgJoinSwapExternAmountIn(t *testing.T) { }, { name: "zero amount", - msg: createMsg(func(msg MsgJoinSwapExternAmountIn) MsgJoinSwapExternAmountIn { + msg: createMsg(func(msg gammtypes.MsgJoinSwapExternAmountIn) gammtypes.MsgJoinSwapExternAmountIn { msg.TokenIn.Amount = sdk.NewInt(0) return msg }), @@ -540,7 +538,7 @@ func TestMsgJoinSwapExternAmountIn(t *testing.T) { }, { name: "negative amount", - msg: createMsg(func(msg MsgJoinSwapExternAmountIn) MsgJoinSwapExternAmountIn { + msg: createMsg(func(msg gammtypes.MsgJoinSwapExternAmountIn) gammtypes.MsgJoinSwapExternAmountIn { msg.TokenIn.Amount = sdk.NewInt(-10) return msg }), @@ -548,7 +546,7 @@ func TestMsgJoinSwapExternAmountIn(t *testing.T) { }, { name: "zero criteria", - msg: createMsg(func(msg MsgJoinSwapExternAmountIn) MsgJoinSwapExternAmountIn { + msg: createMsg(func(msg gammtypes.MsgJoinSwapExternAmountIn) gammtypes.MsgJoinSwapExternAmountIn { msg.ShareOutMinAmount = sdk.NewInt(0) return msg }), @@ -556,7 +554,7 @@ func TestMsgJoinSwapExternAmountIn(t *testing.T) { }, { name: "negative criteria", - msg: createMsg(func(msg MsgJoinSwapExternAmountIn) MsgJoinSwapExternAmountIn { + msg: createMsg(func(msg gammtypes.MsgJoinSwapExternAmountIn) gammtypes.MsgJoinSwapExternAmountIn { msg.ShareOutMinAmount = sdk.NewInt(-10) return msg }), @@ -579,8 +577,8 @@ func TestMsgJoinSwapShareAmountOut(t *testing.T) { addr1 := sdk.AccAddress(pk1.Address()).String() invalidAddr := sdk.AccAddress("invalid") - createMsg := func(after func(msg MsgJoinSwapShareAmountOut) MsgJoinSwapShareAmountOut) MsgJoinSwapShareAmountOut { - properMsg := MsgJoinSwapShareAmountOut{ + createMsg := func(after func(msg gammtypes.MsgJoinSwapShareAmountOut) gammtypes.MsgJoinSwapShareAmountOut) gammtypes.MsgJoinSwapShareAmountOut { + properMsg := gammtypes.MsgJoinSwapShareAmountOut{ Sender: addr1, PoolId: 1, TokenInDenom: "test", @@ -590,12 +588,12 @@ func TestMsgJoinSwapShareAmountOut(t *testing.T) { return after(properMsg) } - msg := createMsg(func(msg MsgJoinSwapShareAmountOut) MsgJoinSwapShareAmountOut { + msg := createMsg(func(msg gammtypes.MsgJoinSwapShareAmountOut) gammtypes.MsgJoinSwapShareAmountOut { // Do nothing return msg }) - require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Route(), gammtypes.RouterKey) require.Equal(t, msg.Type(), "join_swap_share_amount_out") signers := msg.GetSigners() require.Equal(t, len(signers), 1) @@ -603,12 +601,12 @@ func TestMsgJoinSwapShareAmountOut(t *testing.T) { tests := []struct { name string - msg MsgJoinSwapShareAmountOut + msg gammtypes.MsgJoinSwapShareAmountOut expectPass bool }{ { name: "proper msg", - msg: createMsg(func(msg MsgJoinSwapShareAmountOut) MsgJoinSwapShareAmountOut { + msg: createMsg(func(msg gammtypes.MsgJoinSwapShareAmountOut) gammtypes.MsgJoinSwapShareAmountOut { // Do nothing return msg }), @@ -616,7 +614,7 @@ func TestMsgJoinSwapShareAmountOut(t *testing.T) { }, { name: "invalid sender", - msg: createMsg(func(msg MsgJoinSwapShareAmountOut) MsgJoinSwapShareAmountOut { + msg: createMsg(func(msg gammtypes.MsgJoinSwapShareAmountOut) gammtypes.MsgJoinSwapShareAmountOut { msg.Sender = invalidAddr.String() return msg }), @@ -624,7 +622,7 @@ func TestMsgJoinSwapShareAmountOut(t *testing.T) { }, { name: "invalid denom", - msg: createMsg(func(msg MsgJoinSwapShareAmountOut) MsgJoinSwapShareAmountOut { + msg: createMsg(func(msg gammtypes.MsgJoinSwapShareAmountOut) gammtypes.MsgJoinSwapShareAmountOut { msg.TokenInDenom = "1" return msg }), @@ -632,7 +630,7 @@ func TestMsgJoinSwapShareAmountOut(t *testing.T) { }, { name: "zero amount", - msg: createMsg(func(msg MsgJoinSwapShareAmountOut) MsgJoinSwapShareAmountOut { + msg: createMsg(func(msg gammtypes.MsgJoinSwapShareAmountOut) gammtypes.MsgJoinSwapShareAmountOut { msg.ShareOutAmount = sdk.NewInt(0) return msg }), @@ -640,7 +638,7 @@ func TestMsgJoinSwapShareAmountOut(t *testing.T) { }, { name: "negative amount", - msg: createMsg(func(msg MsgJoinSwapShareAmountOut) MsgJoinSwapShareAmountOut { + msg: createMsg(func(msg gammtypes.MsgJoinSwapShareAmountOut) gammtypes.MsgJoinSwapShareAmountOut { msg.ShareOutAmount = sdk.NewInt(-10) return msg }), @@ -648,7 +646,7 @@ func TestMsgJoinSwapShareAmountOut(t *testing.T) { }, { name: "zero criteria", - msg: createMsg(func(msg MsgJoinSwapShareAmountOut) MsgJoinSwapShareAmountOut { + msg: createMsg(func(msg gammtypes.MsgJoinSwapShareAmountOut) gammtypes.MsgJoinSwapShareAmountOut { msg.TokenInMaxAmount = sdk.NewInt(0) return msg }), @@ -656,7 +654,7 @@ func TestMsgJoinSwapShareAmountOut(t *testing.T) { }, { name: "negative criteria", - msg: createMsg(func(msg MsgJoinSwapShareAmountOut) MsgJoinSwapShareAmountOut { + msg: createMsg(func(msg gammtypes.MsgJoinSwapShareAmountOut) gammtypes.MsgJoinSwapShareAmountOut { msg.TokenInMaxAmount = sdk.NewInt(-10) return msg }), @@ -679,8 +677,8 @@ func TestMsgExitSwapExternAmountOut(t *testing.T) { addr1 := sdk.AccAddress(pk1.Address()).String() invalidAddr := sdk.AccAddress("invalid") - createMsg := func(after func(msg MsgExitSwapExternAmountOut) MsgExitSwapExternAmountOut) MsgExitSwapExternAmountOut { - properMsg := MsgExitSwapExternAmountOut{ + createMsg := func(after func(msg gammtypes.MsgExitSwapExternAmountOut) gammtypes.MsgExitSwapExternAmountOut) gammtypes.MsgExitSwapExternAmountOut { + properMsg := gammtypes.MsgExitSwapExternAmountOut{ Sender: addr1, PoolId: 1, TokenOut: sdk.NewCoin("test", sdk.NewInt(100)), @@ -689,12 +687,12 @@ func TestMsgExitSwapExternAmountOut(t *testing.T) { return after(properMsg) } - msg := createMsg(func(msg MsgExitSwapExternAmountOut) MsgExitSwapExternAmountOut { + msg := createMsg(func(msg gammtypes.MsgExitSwapExternAmountOut) gammtypes.MsgExitSwapExternAmountOut { // Do nothing return msg }) - require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Route(), gammtypes.RouterKey) require.Equal(t, msg.Type(), "exit_swap_extern_amount_out") signers := msg.GetSigners() require.Equal(t, len(signers), 1) @@ -702,12 +700,12 @@ func TestMsgExitSwapExternAmountOut(t *testing.T) { tests := []struct { name string - msg MsgExitSwapExternAmountOut + msg gammtypes.MsgExitSwapExternAmountOut expectPass bool }{ { name: "proper msg", - msg: createMsg(func(msg MsgExitSwapExternAmountOut) MsgExitSwapExternAmountOut { + msg: createMsg(func(msg gammtypes.MsgExitSwapExternAmountOut) gammtypes.MsgExitSwapExternAmountOut { // Do nothing return msg }), @@ -715,7 +713,7 @@ func TestMsgExitSwapExternAmountOut(t *testing.T) { }, { name: "invalid sender", - msg: createMsg(func(msg MsgExitSwapExternAmountOut) MsgExitSwapExternAmountOut { + msg: createMsg(func(msg gammtypes.MsgExitSwapExternAmountOut) gammtypes.MsgExitSwapExternAmountOut { msg.Sender = invalidAddr.String() return msg }), @@ -723,7 +721,7 @@ func TestMsgExitSwapExternAmountOut(t *testing.T) { }, { name: "invalid denom", - msg: createMsg(func(msg MsgExitSwapExternAmountOut) MsgExitSwapExternAmountOut { + msg: createMsg(func(msg gammtypes.MsgExitSwapExternAmountOut) gammtypes.MsgExitSwapExternAmountOut { msg.TokenOut.Denom = "1" return msg }), @@ -731,7 +729,7 @@ func TestMsgExitSwapExternAmountOut(t *testing.T) { }, { name: "zero amount", - msg: createMsg(func(msg MsgExitSwapExternAmountOut) MsgExitSwapExternAmountOut { + msg: createMsg(func(msg gammtypes.MsgExitSwapExternAmountOut) gammtypes.MsgExitSwapExternAmountOut { msg.TokenOut.Amount = sdk.NewInt(0) return msg }), @@ -739,7 +737,7 @@ func TestMsgExitSwapExternAmountOut(t *testing.T) { }, { name: "negative amount", - msg: createMsg(func(msg MsgExitSwapExternAmountOut) MsgExitSwapExternAmountOut { + msg: createMsg(func(msg gammtypes.MsgExitSwapExternAmountOut) gammtypes.MsgExitSwapExternAmountOut { msg.TokenOut.Amount = sdk.NewInt(-10) return msg }), @@ -747,7 +745,7 @@ func TestMsgExitSwapExternAmountOut(t *testing.T) { }, { name: "zero criteria", - msg: createMsg(func(msg MsgExitSwapExternAmountOut) MsgExitSwapExternAmountOut { + msg: createMsg(func(msg gammtypes.MsgExitSwapExternAmountOut) gammtypes.MsgExitSwapExternAmountOut { msg.ShareInMaxAmount = sdk.NewInt(0) return msg }), @@ -755,7 +753,7 @@ func TestMsgExitSwapExternAmountOut(t *testing.T) { }, { name: "negative criteria", - msg: createMsg(func(msg MsgExitSwapExternAmountOut) MsgExitSwapExternAmountOut { + msg: createMsg(func(msg gammtypes.MsgExitSwapExternAmountOut) gammtypes.MsgExitSwapExternAmountOut { msg.ShareInMaxAmount = sdk.NewInt(-10) return msg }), @@ -778,8 +776,8 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { addr1 := sdk.AccAddress(pk1.Address()).String() invalidAddr := sdk.AccAddress("invalid") - createMsg := func(after func(msg MsgExitSwapShareAmountIn) MsgExitSwapShareAmountIn) MsgExitSwapShareAmountIn { - properMsg := MsgExitSwapShareAmountIn{ + createMsg := func(after func(msg gammtypes.MsgExitSwapShareAmountIn) gammtypes.MsgExitSwapShareAmountIn) gammtypes.MsgExitSwapShareAmountIn { + properMsg := gammtypes.MsgExitSwapShareAmountIn{ Sender: addr1, PoolId: 1, TokenOutDenom: "test", @@ -789,12 +787,12 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { return after(properMsg) } - msg := createMsg(func(msg MsgExitSwapShareAmountIn) MsgExitSwapShareAmountIn { + msg := createMsg(func(msg gammtypes.MsgExitSwapShareAmountIn) gammtypes.MsgExitSwapShareAmountIn { // Do nothing return msg }) - require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Route(), gammtypes.RouterKey) require.Equal(t, msg.Type(), "exit_swap_share_amount_in") signers := msg.GetSigners() require.Equal(t, len(signers), 1) @@ -802,12 +800,12 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { tests := []struct { name string - msg MsgExitSwapShareAmountIn + msg gammtypes.MsgExitSwapShareAmountIn expectPass bool }{ { name: "proper msg", - msg: createMsg(func(msg MsgExitSwapShareAmountIn) MsgExitSwapShareAmountIn { + msg: createMsg(func(msg gammtypes.MsgExitSwapShareAmountIn) gammtypes.MsgExitSwapShareAmountIn { // Do nothing return msg }), @@ -815,7 +813,7 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { }, { name: "invalid sender", - msg: createMsg(func(msg MsgExitSwapShareAmountIn) MsgExitSwapShareAmountIn { + msg: createMsg(func(msg gammtypes.MsgExitSwapShareAmountIn) gammtypes.MsgExitSwapShareAmountIn { msg.Sender = invalidAddr.String() return msg }), @@ -823,7 +821,7 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { }, { name: "invalid denom", - msg: createMsg(func(msg MsgExitSwapShareAmountIn) MsgExitSwapShareAmountIn { + msg: createMsg(func(msg gammtypes.MsgExitSwapShareAmountIn) gammtypes.MsgExitSwapShareAmountIn { msg.TokenOutDenom = "1" return msg }), @@ -831,7 +829,7 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { }, { name: "zero amount", - msg: createMsg(func(msg MsgExitSwapShareAmountIn) MsgExitSwapShareAmountIn { + msg: createMsg(func(msg gammtypes.MsgExitSwapShareAmountIn) gammtypes.MsgExitSwapShareAmountIn { msg.ShareInAmount = sdk.NewInt(0) return msg }), @@ -839,7 +837,7 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { }, { name: "negative amount", - msg: createMsg(func(msg MsgExitSwapShareAmountIn) MsgExitSwapShareAmountIn { + msg: createMsg(func(msg gammtypes.MsgExitSwapShareAmountIn) gammtypes.MsgExitSwapShareAmountIn { msg.ShareInAmount = sdk.NewInt(-10) return msg }), @@ -847,7 +845,7 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { }, { name: "zero criteria", - msg: createMsg(func(msg MsgExitSwapShareAmountIn) MsgExitSwapShareAmountIn { + msg: createMsg(func(msg gammtypes.MsgExitSwapShareAmountIn) gammtypes.MsgExitSwapShareAmountIn { msg.TokenOutMinAmount = sdk.NewInt(0) return msg }), @@ -855,7 +853,7 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { }, { name: "negative criteria", - msg: createMsg(func(msg MsgExitSwapShareAmountIn) MsgExitSwapShareAmountIn { + msg: createMsg(func(msg gammtypes.MsgExitSwapShareAmountIn) gammtypes.MsgExitSwapShareAmountIn { msg.TokenOutMinAmount = sdk.NewInt(-10) return msg }), @@ -878,12 +876,6 @@ func TestAuthzMsg(t *testing.T) { pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() coin := sdk.NewCoin("stake", sdk.NewInt(1)) - someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) - - const ( - mockGranter string = "cosmos1abc" - mockGrantee string = "cosmos1xyz" - ) testCases := []struct { name string @@ -891,7 +883,7 @@ func TestAuthzMsg(t *testing.T) { }{ { name: "MsgExitSwapExternAmountOut", - gammMsg: &MsgExitSwapShareAmountIn{ + gammMsg: &gammtypes.MsgExitSwapShareAmountIn{ Sender: addr1, PoolId: 1, TokenOutDenom: "test", @@ -901,7 +893,7 @@ func TestAuthzMsg(t *testing.T) { }, { name: `MsgExitSwapExternAmountOut`, - gammMsg: &MsgExitSwapExternAmountOut{ + gammMsg: &gammtypes.MsgExitSwapExternAmountOut{ Sender: addr1, PoolId: 1, TokenOut: coin, @@ -910,7 +902,7 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgExitPool", - gammMsg: &MsgExitPool{ + gammMsg: &gammtypes.MsgExitPool{ Sender: addr1, PoolId: 1, ShareInAmount: sdk.NewInt(100), @@ -919,7 +911,7 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgJoinPool", - gammMsg: &MsgJoinPool{ + gammMsg: &gammtypes.MsgJoinPool{ Sender: addr1, PoolId: 1, ShareOutAmount: sdk.NewInt(1), @@ -928,7 +920,7 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgJoinSwapExternAmountIn", - gammMsg: &MsgJoinSwapExternAmountIn{ + gammMsg: &gammtypes.MsgJoinSwapExternAmountIn{ Sender: addr1, PoolId: 1, TokenIn: coin, @@ -937,9 +929,9 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgJoinSwapShareAmountOut", - gammMsg: &MsgSwapExactAmountIn{ + gammMsg: &gammtypes.MsgSwapExactAmountIn{ Sender: addr1, - Routes: []SwapAmountInRoute{{ + Routes: []gammtypes.SwapAmountInRoute{{ PoolId: 0, TokenOutDenom: "test", }, { @@ -952,9 +944,9 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgSwapExactAmountOut", - gammMsg: &MsgSwapExactAmountOut{ + gammMsg: &gammtypes.MsgSwapExactAmountOut{ Sender: addr1, - Routes: []SwapAmountOutRoute{{ + Routes: []gammtypes.SwapAmountOutRoute{{ PoolId: 0, TokenInDenom: "test", }, { @@ -968,35 +960,7 @@ func TestAuthzMsg(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - var ( - mockMsgGrant authz.MsgGrant - mockMsgRevoke authz.MsgRevoke - mockMsgExec authz.MsgExec - ) - - // Authz: Grant Msg - typeURL := sdk.MsgTypeURL(tc.gammMsg) - grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) - require.NoError(t, err) - - msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) - err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) - require.NoError(t, err) - - // Authz: Revoke Msg - msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} - msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) - err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) - require.NoError(t, err) - - // Authz: Exec Msg - msgAny, _ := cdctypes.NewAnyWithValue(tc.gammMsg) - msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} - execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) - err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) - require.NoError(t, err) - require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) + apptesting.TestMessageAuthzSerialization(t, tc.gammMsg) }) } } From ccada40e8cdbc74bd370903a0f22657287ff7dd8 Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 5 Aug 2022 13:46:51 +0700 Subject: [PATCH 16/26] minor lint --- app/apptesting/test_suite.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/apptesting/test_suite.go b/app/apptesting/test_suite.go index c3337c1a191..91370dfb83d 100644 --- a/app/apptesting/test_suite.go +++ b/app/apptesting/test_suite.go @@ -331,12 +331,15 @@ func CreateRandomAccounts(numAccts int) []sdk.AccAddress { func TestMessageAuthzSerialization(t *testing.T, msg sdk.Msg) { someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + const ( + mockGranter string = "cosmos1abc" + mockGrantee string = "cosmos1xyz" + ) + var ( mockMsgGrant authz.MsgGrant mockMsgRevoke authz.MsgRevoke mockMsgExec authz.MsgExec - mockGranter string = "cosmos1abc" - mockGrantee string = "cosmos1xyz" ) // Authz: Grant Msg From a221d02858105cc9bcc178fce9ad23a720f64641 Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 5 Aug 2022 15:26:48 +0700 Subject: [PATCH 17/26] incentives --- x/incentives/types/msgs_test.go | 92 +++++++++++---------------------- 1 file changed, 31 insertions(+), 61 deletions(-) diff --git a/x/incentives/types/msgs_test.go b/x/incentives/types/msgs_test.go index ea3ff69e16e..0236b4a5f3d 100644 --- a/x/incentives/types/msgs_test.go +++ b/x/incentives/types/msgs_test.go @@ -1,18 +1,17 @@ -package types +package types_test import ( - "encoding/json" "testing" time "time" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/authz" - authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" + + incentivestypes "github.com/osmosis-labs/osmosis/v10/x/incentives/types" + + "github.com/osmosis-labs/osmosis/v10/app/apptesting" appParams "github.com/osmosis-labs/osmosis/v10/app/params" lockuptypes "github.com/osmosis-labs/osmosis/v10/x/lockup/types" @@ -25,14 +24,14 @@ func TestMsgCreatePool(t *testing.T) { addr1 := sdk.AccAddress(pk1.Address()) // make a proper createPool message - createMsg := func(after func(msg MsgCreateGauge) MsgCreateGauge) MsgCreateGauge { + createMsg := func(after func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { distributeTo := lockuptypes.QueryCondition{ LockQueryType: lockuptypes.ByDuration, Denom: "lptoken", Duration: time.Second, } - properMsg := *NewMsgCreateGauge( + properMsg := *incentivestypes.NewMsgCreateGauge( false, addr1, distributeTo, @@ -45,10 +44,10 @@ func TestMsgCreatePool(t *testing.T) { } // validate createPool message was created as intended - msg := createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg := createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { return msg }) - require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Route(), incentivestypes.RouterKey) require.Equal(t, msg.Type(), "create_gauge") signers := msg.GetSigners() require.Equal(t, len(signers), 1) @@ -56,19 +55,19 @@ func TestMsgCreatePool(t *testing.T) { tests := []struct { name string - msg MsgCreateGauge + msg incentivestypes.MsgCreateGauge expectPass bool }{ { name: "proper msg", - msg: createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { return msg }), expectPass: true, }, { name: "empty owner", - msg: createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { msg.Owner = "" return msg }), @@ -76,7 +75,7 @@ func TestMsgCreatePool(t *testing.T) { }, { name: "empty distribution denom", - msg: createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { msg.DistributeTo.Denom = "" return msg }), @@ -84,7 +83,7 @@ func TestMsgCreatePool(t *testing.T) { }, { name: "invalid distribution denom", - msg: createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { msg.DistributeTo.Denom = "111" return msg }), @@ -92,7 +91,7 @@ func TestMsgCreatePool(t *testing.T) { }, { name: "invalid lock query type", - msg: createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { msg.DistributeTo.LockQueryType = -1 return msg }), @@ -100,7 +99,7 @@ func TestMsgCreatePool(t *testing.T) { }, { name: "invalid lock query type", - msg: createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { msg.DistributeTo.LockQueryType = -1 return msg }), @@ -108,7 +107,7 @@ func TestMsgCreatePool(t *testing.T) { }, { name: "invalid distribution start time", - msg: createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { msg.StartTime = time.Time{} return msg }), @@ -116,7 +115,7 @@ func TestMsgCreatePool(t *testing.T) { }, { name: "invalid num epochs paid over", - msg: createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { msg.NumEpochsPaidOver = 0 return msg }), @@ -124,7 +123,7 @@ func TestMsgCreatePool(t *testing.T) { }, { name: "invalid num epochs paid over for perpetual gauge", - msg: createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { msg.NumEpochsPaidOver = 2 msg.IsPerpetual = true return msg @@ -133,7 +132,7 @@ func TestMsgCreatePool(t *testing.T) { }, { name: "valid num epochs paid over for perpetual gauge", - msg: createMsg(func(msg MsgCreateGauge) MsgCreateGauge { + msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge { msg.NumEpochsPaidOver = 1 msg.IsPerpetual = true return msg @@ -158,8 +157,8 @@ func TestMsgAddToGauge(t *testing.T) { addr1 := sdk.AccAddress(pk1.Address()) // make a proper addToGauge message - createMsg := func(after func(msg MsgAddToGauge) MsgAddToGauge) MsgAddToGauge { - properMsg := *NewMsgAddToGauge( + createMsg := func(after func(msg incentivestypes.MsgAddToGauge) incentivestypes.MsgAddToGauge) incentivestypes.MsgAddToGauge { + properMsg := *incentivestypes.NewMsgAddToGauge( addr1, 1, sdk.Coins{sdk.NewInt64Coin("stake", 10)}, @@ -169,10 +168,10 @@ func TestMsgAddToGauge(t *testing.T) { } // validate addToGauge message was created as intended - msg := createMsg(func(msg MsgAddToGauge) MsgAddToGauge { + msg := createMsg(func(msg incentivestypes.MsgAddToGauge) incentivestypes.MsgAddToGauge { return msg }) - require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Route(), incentivestypes.RouterKey) require.Equal(t, msg.Type(), "add_to_gauge") signers := msg.GetSigners() require.Equal(t, len(signers), 1) @@ -180,19 +179,19 @@ func TestMsgAddToGauge(t *testing.T) { tests := []struct { name string - msg MsgAddToGauge + msg incentivestypes.MsgAddToGauge expectPass bool }{ { name: "proper msg", - msg: createMsg(func(msg MsgAddToGauge) MsgAddToGauge { + msg: createMsg(func(msg incentivestypes.MsgAddToGauge) incentivestypes.MsgAddToGauge { return msg }), expectPass: true, }, { name: "empty owner", - msg: createMsg(func(msg MsgAddToGauge) MsgAddToGauge { + msg: createMsg(func(msg incentivestypes.MsgAddToGauge) incentivestypes.MsgAddToGauge { msg.Owner = "" return msg }), @@ -200,7 +199,7 @@ func TestMsgAddToGauge(t *testing.T) { }, { name: "empty rewards", - msg: createMsg(func(msg MsgAddToGauge) MsgAddToGauge { + msg: createMsg(func(msg incentivestypes.MsgAddToGauge) incentivestypes.MsgAddToGauge { msg.Rewards = sdk.Coins{} return msg }), @@ -236,7 +235,7 @@ func TestAuthzMsg(t *testing.T) { }{ { name: "MsgAddToGauge", - incentivesMsg: &MsgAddToGauge{ + incentivesMsg: &incentivestypes.MsgAddToGauge{ Owner: addr1, GaugeId: 1, Rewards: sdk.NewCoins(coin), @@ -244,7 +243,7 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgCreateGauge", - incentivesMsg: &MsgCreateGauge{ + incentivesMsg: &incentivestypes.MsgCreateGauge{ IsPerpetual: false, Owner: addr1, DistributeTo: lockuptypes.QueryCondition{ @@ -260,36 +259,7 @@ func TestAuthzMsg(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - var ( - mockMsgGrant authz.MsgGrant - mockMsgRevoke authz.MsgRevoke - mockMsgExec authz.MsgExec - ) - - // Authz: Grant Msg - typeURL := sdk.MsgTypeURL(tc.incentivesMsg) - grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) - require.NoError(t, err) - - msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) - err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) - require.NoError(t, err) - - // Authz: Revoke Msg - msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} - msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) - err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) - require.NoError(t, err) - - // Authz: Exec Msg - msgAny, _ := cdctypes.NewAnyWithValue(tc.incentivesMsg) - msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} - execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) - err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) - require.NoError(t, err) - require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) - + apptesting.TestMessageAuthzSerialization(t, tc.incentivesMsg) }) } } From dd2d1f62d1b48437bc6adbfc806d0f489c81b411 Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 5 Aug 2022 15:32:46 +0700 Subject: [PATCH 18/26] lockup --- x/lockup/types/msg_test.go | 92 ------------------------------------- x/lockup/types/msgs_test.go | 52 ++++++++++++++++++++- 2 files changed, 51 insertions(+), 93 deletions(-) delete mode 100644 x/lockup/types/msg_test.go diff --git a/x/lockup/types/msg_test.go b/x/lockup/types/msg_test.go deleted file mode 100644 index 644e2850e0c..00000000000 --- a/x/lockup/types/msg_test.go +++ /dev/null @@ -1,92 +0,0 @@ -package types - -import ( - "encoding/json" - "testing" - "time" - - sdk "github.com/cosmos/cosmos-sdk/types" - - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/x/authz" - authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" - "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/crypto/ed25519" - - appParams "github.com/osmosis-labs/osmosis/v10/app/params" -) - -// // Test authz serialize and de-serializes for lockup msg. -func TestAuthzMsg(t *testing.T) { - appParams.SetAddressPrefixes() - pk1 := ed25519.GenPrivKey().PubKey() - addr1 := sdk.AccAddress(pk1.Address()).String() - coin := sdk.NewCoin("denom", sdk.NewInt(1)) - someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) - - const ( - mockGranter string = "cosmos1abc" - mockGrantee string = "cosmos1xyz" - ) - - testCases := []struct { - name string - msg sdk.Msg - }{ - { - name: "MsgLockTokens", - msg: &MsgLockTokens{ - Owner: addr1, - Duration: time.Hour, - Coins: sdk.NewCoins(coin), - }, - }, - { - name: "MsgBeginUnlocking", - msg: &MsgBeginUnlocking{ - Owner: addr1, - ID: 1, - Coins: sdk.NewCoins(coin), - }, - }, - { - name: "MsgBeginUnlockingAll", - msg: &MsgBeginUnlockingAll{ - Owner: addr1, - }, - }, - } - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - var ( - mockMsgGrant authz.MsgGrant - mockMsgRevoke authz.MsgRevoke - mockMsgExec authz.MsgExec - ) - - // Authz: Grant Msg - typeURL := sdk.MsgTypeURL(tc.msg) - grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) - require.NoError(t, err) - - msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) - err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) - require.NoError(t, err) - - // Authz: Revoke Msg - msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} - msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) - err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) - require.NoError(t, err) - - // Authz: Exec Msg - msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) - msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} - execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) - err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) - require.NoError(t, err) - require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) - }) - } -} diff --git a/x/lockup/types/msgs_test.go b/x/lockup/types/msgs_test.go index 63d0aa097b4..c4b18a76a89 100644 --- a/x/lockup/types/msgs_test.go +++ b/x/lockup/types/msgs_test.go @@ -9,8 +9,11 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/osmosis-labs/osmosis/v10/app/apptesting" - appParams "github.com/osmosis-labs/osmosis/v10/app/params" "github.com/osmosis-labs/osmosis/v10/x/lockup/types" + + "github.com/tendermint/tendermint/crypto/ed25519" + + appParams "github.com/osmosis-labs/osmosis/v10/app/params" ) func TestMsgLockTokens(t *testing.T) { @@ -248,3 +251,50 @@ func TestMsgExtendLockup(t *testing.T) { }) } } + +// // Test authz serialize and de-serializes for lockup msg. +func TestAuthzMsg(t *testing.T) { + appParams.SetAddressPrefixes() + pk1 := ed25519.GenPrivKey().PubKey() + addr1 := sdk.AccAddress(pk1.Address()).String() + coin := sdk.NewCoin("denom", sdk.NewInt(1)) + someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + + const ( + mockGranter string = "cosmos1abc" + mockGrantee string = "cosmos1xyz" + ) + + testCases := []struct { + name string + msg sdk.Msg + }{ + { + name: "MsgLockTokens", + msg: &types.MsgLockTokens{ + Owner: addr1, + Duration: time.Hour, + Coins: sdk.NewCoins(coin), + }, + }, + { + name: "MsgBeginUnlocking", + msg: &types.MsgBeginUnlocking{ + Owner: addr1, + ID: 1, + Coins: sdk.NewCoins(coin), + }, + }, + { + name: "MsgBeginUnlockingAll", + msg: &types.MsgBeginUnlockingAll{ + Owner: addr1, + }, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + apptesting.TestMessageAuthzSerialization(t, tc.msg) + }) + } +} From 17391d68314890eed4051200c292800b43f9bb35 Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 5 Aug 2022 15:33:07 +0700 Subject: [PATCH 19/26] minor --- x/lockup/types/msgs_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/lockup/types/msgs_test.go b/x/lockup/types/msgs_test.go index c4b18a76a89..e255b933325 100644 --- a/x/lockup/types/msgs_test.go +++ b/x/lockup/types/msgs_test.go @@ -258,7 +258,6 @@ func TestAuthzMsg(t *testing.T) { pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() coin := sdk.NewCoin("denom", sdk.NewInt(1)) - someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) const ( mockGranter string = "cosmos1abc" From 11344a665d6001b779f9f25ab94f06eec88f59db Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 5 Aug 2022 15:34:53 +0700 Subject: [PATCH 20/26] superfluid --- x/superfluid/types/msg_test.go | 51 ++++++---------------------------- 1 file changed, 9 insertions(+), 42 deletions(-) diff --git a/x/superfluid/types/msg_test.go b/x/superfluid/types/msg_test.go index 4ead4dc084d..67c6359dd34 100644 --- a/x/superfluid/types/msg_test.go +++ b/x/superfluid/types/msg_test.go @@ -1,18 +1,14 @@ -package types +package types_test import ( - "encoding/json" "testing" - "time" sdk "github.com/cosmos/cosmos-sdk/types" - authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" + "github.com/osmosis-labs/osmosis/v10/app/apptesting" appParams "github.com/osmosis-labs/osmosis/v10/app/params" + "github.com/osmosis-labs/osmosis/v10/x/superfluid/types" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/x/authz" - "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" ) @@ -22,7 +18,6 @@ func TestAuthzMsg(t *testing.T) { pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() coin := sdk.NewCoin("stake", sdk.NewInt(1)) - someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) const ( mockGranter string = "cosmos1abc" @@ -35,7 +30,7 @@ func TestAuthzMsg(t *testing.T) { }{ { name: "MsgLockAndSuperfluidDelegate", - msg: &MsgLockAndSuperfluidDelegate{ + msg: &types.MsgLockAndSuperfluidDelegate{ Sender: addr1, Coins: sdk.NewCoins(coin), ValAddr: "valoper1xyz", @@ -43,7 +38,7 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgSuperfluidDelegate", - msg: &MsgSuperfluidDelegate{ + msg: &types.MsgSuperfluidDelegate{ Sender: addr1, LockId: 1, ValAddr: "valoper1xyz", @@ -51,21 +46,21 @@ func TestAuthzMsg(t *testing.T) { }, { name: "MsgSuperfluidUnbondLock", - msg: &MsgSuperfluidUnbondLock{ + msg: &types.MsgSuperfluidUnbondLock{ Sender: addr1, LockId: 1, }, }, { name: "MsgSuperfluidUndelegate", - msg: &MsgSuperfluidUndelegate{ + msg: &types.MsgSuperfluidUndelegate{ Sender: addr1, LockId: 1, }, }, { name: "MsgUnPoolWhitelistedPool", - msg: &MsgUnPoolWhitelistedPool{ + msg: &types.MsgUnPoolWhitelistedPool{ Sender: addr1, PoolId: 1, }, @@ -73,35 +68,7 @@ func TestAuthzMsg(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - var ( - mockMsgGrant authz.MsgGrant - mockMsgRevoke authz.MsgRevoke - mockMsgExec authz.MsgExec - ) - - // Authz: Grant Msg - typeURL := sdk.MsgTypeURL(tc.msg) - grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) - require.NoError(t, err) - - msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) - err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) - require.NoError(t, err) - - // Authz: Revoke Msg - msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} - msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) - err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) - require.NoError(t, err) - - // Authz: Exec Msg - msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) - msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} - execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) - err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) - require.NoError(t, err) - require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) + apptesting.TestMessageAuthzSerialization(t, tc.msg) }) } } From 1ad2e65869c5b35108513bd8d4a53d3b2e0a25fd Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 5 Aug 2022 15:36:32 +0700 Subject: [PATCH 21/26] token factory --- x/tokenfactory/types/msgs_test.go | 50 +++++-------------------------- 1 file changed, 8 insertions(+), 42 deletions(-) diff --git a/x/tokenfactory/types/msgs_test.go b/x/tokenfactory/types/msgs_test.go index 7d7d490f52c..47694c7017d 100644 --- a/x/tokenfactory/types/msgs_test.go +++ b/x/tokenfactory/types/msgs_test.go @@ -1,18 +1,14 @@ -package types +package types_test import ( - "encoding/json" "testing" - "time" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/osmosis-labs/osmosis/v10/app/apptesting" appParams "github.com/osmosis-labs/osmosis/v10/app/params" + "github.com/osmosis-labs/osmosis/v10/x/tokenfactory/types" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/x/authz" - authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" - "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" ) @@ -22,7 +18,6 @@ func TestAuthzMsg(t *testing.T) { pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() coin := sdk.NewCoin("denom", sdk.NewInt(1)) - someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) const ( mockGranter string = "cosmos1abc" @@ -35,28 +30,28 @@ func TestAuthzMsg(t *testing.T) { }{ { name: "MsgCreateDenom", - msg: &MsgCreateDenom{ + msg: &types.MsgCreateDenom{ Sender: addr1, Subdenom: "valoper1xyz", }, }, { name: "MsgBurn", - msg: &MsgBurn{ + msg: &types.MsgBurn{ Sender: addr1, Amount: coin, }, }, { name: "MsgMint", - msg: &MsgMint{ + msg: &types.MsgMint{ Sender: addr1, Amount: coin, }, }, { name: "MsgChangeAdmin", - msg: &MsgChangeAdmin{ + msg: &types.MsgChangeAdmin{ Sender: addr1, Denom: "denom", NewAdmin: "osmo1q8tq5qhrhw6t970egemuuwywhlhpnmdmts6xnu", @@ -65,36 +60,7 @@ func TestAuthzMsg(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - var ( - mockMsgGrant authz.MsgGrant - mockMsgRevoke authz.MsgRevoke - mockMsgExec authz.MsgExec - ) - - // Authz: Grant Msg - typeURL := sdk.MsgTypeURL(tc.msg) - grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) - require.NoError(t, err) - - msgGrant := authz.MsgGrant{Granter: mockGranter, Grantee: mockGrantee, Grant: grant} - msgGrantBytes := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgGrant))) - err = authzcodec.ModuleCdc.UnmarshalJSON(msgGrantBytes, &mockMsgGrant) - require.NoError(t, err) - - // Authz: Revoke Msg - msgRevoke := authz.MsgRevoke{Granter: mockGranter, Grantee: mockGrantee, MsgTypeUrl: typeURL} - msgRevokeByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgRevoke))) - err = authzcodec.ModuleCdc.UnmarshalJSON(msgRevokeByte, &mockMsgRevoke) - require.NoError(t, err) - - // Authz: Exec Msg - msgAny, _ := cdctypes.NewAnyWithValue(tc.msg) - msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} - execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) - err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec) - require.NoError(t, err) - require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value) - + apptesting.TestMessageAuthzSerialization(t, tc.msg) }) } } From f9f5fbd8d0c42dbe802bfbe20eb578ab93aec381 Mon Sep 17 00:00:00 2001 From: vuong Date: Fri, 5 Aug 2022 21:52:19 +0700 Subject: [PATCH 22/26] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7021e7154b2..09ea428258f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#2193](https://github.com/osmosis-labs/osmosis/pull/2193) Add TwapKeeper to the Osmosis app * [#2227](https://github.com/osmosis-labs/osmosis/pull/2227) Enable charging fee in base denom for `CreateGauge` and `AddToGauge`. * [#2283](https://github.com/osmosis-labs/osmosis/pull/2283) x/incentives: refactor `CreateGauge` and `AddToGauge` fees to use txfees denom +* [#2206](https://github.com/osmosis-labs/osmosis/pull/2283) Register all Amino interfaces and concrete types on the authz Amino codec. This will allow the authz module to properly serialize and de-serializes instances using Amino. #### Golang API breaks From f3cebe9e97ef6e8a5ed8236956813b127cdf80c5 Mon Sep 17 00:00:00 2001 From: vuong <56973102+vuong177@users.noreply.github.com> Date: Sat, 6 Aug 2022 00:14:27 +0700 Subject: [PATCH 23/26] Update x/gamm/types/msgs_test.go Co-authored-by: Roman --- x/gamm/types/msgs_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/gamm/types/msgs_test.go b/x/gamm/types/msgs_test.go index 97facc2cd8b..983ab039cfe 100644 --- a/x/gamm/types/msgs_test.go +++ b/x/gamm/types/msgs_test.go @@ -872,7 +872,6 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { // Test authz serialize and de-serializes for gamm msg. func TestAuthzMsg(t *testing.T) { - appParams.SetAddressPrefixes() pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() coin := sdk.NewCoin("stake", sdk.NewInt(1)) From f047f32f4613d01c6ba0dcd7133574a36621933f Mon Sep 17 00:00:00 2001 From: vuong Date: Sat, 6 Aug 2022 00:16:59 +0700 Subject: [PATCH 24/26] minor --- x/gamm/types/msgs_test.go | 2 +- x/incentives/types/msgs_test.go | 2 +- x/lockup/types/msgs_test.go | 1 - x/superfluid/types/msg_test.go | 4 +--- 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/x/gamm/types/msgs_test.go b/x/gamm/types/msgs_test.go index 983ab039cfe..7c63a5b988b 100644 --- a/x/gamm/types/msgs_test.go +++ b/x/gamm/types/msgs_test.go @@ -874,7 +874,7 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { func TestAuthzMsg(t *testing.T) { pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() - coin := sdk.NewCoin("stake", sdk.NewInt(1)) + coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1)) testCases := []struct { name string diff --git a/x/incentives/types/msgs_test.go b/x/incentives/types/msgs_test.go index 0236b4a5f3d..cda7d4f362e 100644 --- a/x/incentives/types/msgs_test.go +++ b/x/incentives/types/msgs_test.go @@ -221,7 +221,7 @@ func TestAuthzMsg(t *testing.T) { appParams.SetAddressPrefixes() pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() - coin := sdk.NewCoin("stake", sdk.NewInt(1)) + coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1)) someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) const ( diff --git a/x/lockup/types/msgs_test.go b/x/lockup/types/msgs_test.go index e255b933325..d07ff050252 100644 --- a/x/lockup/types/msgs_test.go +++ b/x/lockup/types/msgs_test.go @@ -254,7 +254,6 @@ func TestMsgExtendLockup(t *testing.T) { // // Test authz serialize and de-serializes for lockup msg. func TestAuthzMsg(t *testing.T) { - appParams.SetAddressPrefixes() pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() coin := sdk.NewCoin("denom", sdk.NewInt(1)) diff --git a/x/superfluid/types/msg_test.go b/x/superfluid/types/msg_test.go index 67c6359dd34..68b4e7aa522 100644 --- a/x/superfluid/types/msg_test.go +++ b/x/superfluid/types/msg_test.go @@ -6,7 +6,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/osmosis-labs/osmosis/v10/app/apptesting" - appParams "github.com/osmosis-labs/osmosis/v10/app/params" "github.com/osmosis-labs/osmosis/v10/x/superfluid/types" "github.com/tendermint/tendermint/crypto/ed25519" @@ -14,10 +13,9 @@ import ( // // Test authz serialize and de-serializes for superfluid msg. func TestAuthzMsg(t *testing.T) { - appParams.SetAddressPrefixes() pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() - coin := sdk.NewCoin("stake", sdk.NewInt(1)) + coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1)) const ( mockGranter string = "cosmos1abc" From 187b95ff6f6255d02630c019ddde1245b2e0b67b Mon Sep 17 00:00:00 2001 From: vuong Date: Sat, 6 Aug 2022 00:40:10 +0700 Subject: [PATCH 25/26] fumpt --- x/tokenfactory/types/msgs_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/x/tokenfactory/types/msgs_test.go b/x/tokenfactory/types/msgs_test.go index 47694c7017d..3e9be48e3db 100644 --- a/x/tokenfactory/types/msgs_test.go +++ b/x/tokenfactory/types/msgs_test.go @@ -6,7 +6,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/osmosis-labs/osmosis/v10/app/apptesting" - appParams "github.com/osmosis-labs/osmosis/v10/app/params" "github.com/osmosis-labs/osmosis/v10/x/tokenfactory/types" "github.com/tendermint/tendermint/crypto/ed25519" @@ -14,7 +13,6 @@ import ( // // Test authz serialize and de-serializes for tokenfactory msg. func TestAuthzMsg(t *testing.T) { - appParams.SetAddressPrefixes() pk1 := ed25519.GenPrivKey().PubKey() addr1 := sdk.AccAddress(pk1.Address()).String() coin := sdk.NewCoin("denom", sdk.NewInt(1)) From 73de3b6fb84a48d107a46f418048a565605dbd63 Mon Sep 17 00:00:00 2001 From: vuong Date: Sat, 6 Aug 2022 00:44:28 +0700 Subject: [PATCH 26/26] handler err when create msgAny --- app/apptesting/test_suite.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/apptesting/test_suite.go b/app/apptesting/test_suite.go index 91370dfb83d..3d3b09152d6 100644 --- a/app/apptesting/test_suite.go +++ b/app/apptesting/test_suite.go @@ -359,7 +359,8 @@ func TestMessageAuthzSerialization(t *testing.T, msg sdk.Msg) { require.NoError(t, err) // Authz: Exec Msg - msgAny, _ := cdctypes.NewAnyWithValue(msg) + msgAny, err := cdctypes.NewAnyWithValue(msg) + require.NoError(t, err) msgExec := authz.MsgExec{Grantee: mockGrantee, Msgs: []*cdctypes.Any{msgAny}} execMsgByte := json.RawMessage(sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msgExec))) err = authzcodec.ModuleCdc.UnmarshalJSON(execMsgByte, &mockMsgExec)