Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Authz #2206

Merged
merged 28 commits into from
Aug 6, 2022
Merged

Authz #2206

Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions x/gamm/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
150 changes: 150 additions & 0 deletions x/gamm/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -867,3 +872,148 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) {
}
}
}

// Test authz
Copy link
Member

Choose a reason for hiding this comment

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

Please elaborate elaborate here that this test is meant more so to test the authz than the gamm message

func TestAuthzMsg(t *testing.T) {
appParams.SetAddressPrefixes()
vuong177 marked this conversation as resolved.
Show resolved Hide resolved
pk1 := ed25519.GenPrivKey().PubKey()
addr1 := sdk.AccAddress(pk1.Address()).String()
coin := sdk.NewCoin("stake", sdk.NewInt(1))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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)

testCases := []struct {
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
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"}`,
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to have this represented in a struct format and the unmarshal to json string if needed?

It's hard to follow this in a string format.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a way to have this represented in a struct format and the unmarshal to json string if needed?

It's hard to follow this in a string format.

I think it's a bit complex. Why we don't use pretty json in expectedGrantSignByteMsg. Like that

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"
}`

instead of use one line string

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good, thanks for suggesting the alternative

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,
PoolId: 1,
TokenOutDenom: "test",
ShareInAmount: sdk.NewInt(100),
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
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}
Copy link
Member

Choose a reason for hiding this comment

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

can we extract cosmos1abc and cosmos1def to constants instead of hardcoding in multiple places, please?

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")),
)
}
}
6 changes: 6 additions & 0 deletions x/incentives/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()
}
75 changes: 75 additions & 0 deletions x/incentives/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")),
)
}
}
6 changes: 6 additions & 0 deletions x/lockup/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
}
Loading