Skip to content

Commit

Permalink
Authz (#2206)
Browse files Browse the repository at this point in the history
* registerCodec for authz

* gamm msg: test authz grant revoke exec

* gamm msg: test authz grant revoke exec

* test authz for incentives module

* test authz for superfluid module

* test authz for tokenfactory module

* gamm test clean

* incentives test clean

* superfluid test clean

* tokenfactory test clean

* tokenfactory

* lockup test

* refactor test for more maintainable

* Update msgs_test.go

* refactor authz test in gamm module

* minor lint

* incentives

* lockup

* minor

* superfluid

* token factory

* add changelog

* Update x/gamm/types/msgs_test.go

Co-authored-by: Roman <[email protected]>

* minor

* fumpt

* handler err when create msgAny

Co-authored-by: Roman <[email protected]>
  • Loading branch information
vuong177 and p0mvn authored Aug 6, 2022
1 parent 9114151 commit 9bc52d4
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 45 additions & 0 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -323,6 +329,45 @@ 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)
const (
mockGranter string = "cosmos1abc"
mockGrantee string = "cosmos1xyz"
)

var (
mockMsgGrant authz.MsgGrant
mockMsgRevoke authz.MsgRevoke
mockMsgExec authz.MsgExec
)

// 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, 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)
require.NoError(t, err)
require.Equal(t, msgExec.Msgs[0].Value, mockMsgExec.Msgs[0].Value)
}

func GenerateTestAddrs() (string, string) {
pk1 := ed25519.GenPrivKey().PubKey()
validAddr := sdk.AccAddress(pk1.Address()).String()
Expand Down
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()
}
95 changes: 95 additions & 0 deletions x/gamm/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

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"
)

Expand Down Expand Up @@ -868,3 +869,97 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) {
}
}
}

// Test authz serialize and de-serializes for gamm msg.
func TestAuthzMsg(t *testing.T) {
pk1 := ed25519.GenPrivKey().PubKey()
addr1 := sdk.AccAddress(pk1.Address()).String()
coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1))

testCases := []struct {
name string
gammMsg sdk.Msg
}{
{
name: "MsgExitSwapExternAmountOut",
gammMsg: &gammtypes.MsgExitSwapShareAmountIn{
Sender: addr1,
PoolId: 1,
TokenOutDenom: "test",
ShareInAmount: sdk.NewInt(100),
TokenOutMinAmount: sdk.NewInt(100),
},
},
{
name: `MsgExitSwapExternAmountOut`,
gammMsg: &gammtypes.MsgExitSwapExternAmountOut{
Sender: addr1,
PoolId: 1,
TokenOut: coin,
ShareInMaxAmount: sdk.NewInt(1),
},
},
{
name: "MsgExitPool",
gammMsg: &gammtypes.MsgExitPool{
Sender: addr1,
PoolId: 1,
ShareInAmount: sdk.NewInt(100),
TokenOutMins: sdk.NewCoins(coin),
},
},
{
name: "MsgJoinPool",
gammMsg: &gammtypes.MsgJoinPool{
Sender: addr1,
PoolId: 1,
ShareOutAmount: sdk.NewInt(1),
TokenInMaxs: sdk.NewCoins(coin),
},
},
{
name: "MsgJoinSwapExternAmountIn",
gammMsg: &gammtypes.MsgJoinSwapExternAmountIn{
Sender: addr1,
PoolId: 1,
TokenIn: coin,
ShareOutMinAmount: sdk.NewInt(1),
},
},
{
name: "MsgJoinSwapShareAmountOut",
gammMsg: &gammtypes.MsgSwapExactAmountIn{
Sender: addr1,
Routes: []gammtypes.SwapAmountInRoute{{
PoolId: 0,
TokenOutDenom: "test",
}, {
PoolId: 1,
TokenOutDenom: "test2",
}},
TokenIn: coin,
TokenOutMinAmount: sdk.NewInt(1),
},
},
{
name: "MsgSwapExactAmountOut",
gammMsg: &gammtypes.MsgSwapExactAmountOut{
Sender: addr1,
Routes: []gammtypes.SwapAmountOutRoute{{
PoolId: 0,
TokenInDenom: "test",
}, {
PoolId: 1,
TokenInDenom: "test2",
}},
TokenOut: coin,
TokenInMaxAmount: sdk.NewInt(1),
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
apptesting.TestMessageAuthzSerialization(t, tc.gammMsg)
})
}
}
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()
}
51 changes: 51 additions & 0 deletions x/incentives/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (

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"
)

Expand Down Expand Up @@ -212,3 +215,51 @@ 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()
addr1 := sdk.AccAddress(pk1.Address()).String()
coin := sdk.NewCoin(sdk.DefaultBondDenom, 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
incentivesMsg sdk.Msg
}{
{
name: "MsgAddToGauge",
incentivesMsg: &incentivestypes.MsgAddToGauge{
Owner: addr1,
GaugeId: 1,
Rewards: sdk.NewCoins(coin),
},
},
{
name: "MsgCreateGauge",
incentivesMsg: &incentivestypes.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 {
t.Run(tc.name, func(t *testing.T) {
apptesting.TestMessageAuthzSerialization(t, tc.incentivesMsg)
})
}
}
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()
}
50 changes: 49 additions & 1 deletion x/lockup/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -248,3 +251,48 @@ func TestMsgExtendLockup(t *testing.T) {
})
}
}

// // Test authz serialize and de-serializes for lockup msg.
func TestAuthzMsg(t *testing.T) {
pk1 := ed25519.GenPrivKey().PubKey()
addr1 := sdk.AccAddress(pk1.Address()).String()
coin := sdk.NewCoin("denom", sdk.NewInt(1))

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)
})
}
}
6 changes: 6 additions & 0 deletions x/superfluid/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"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

Expand Down Expand Up @@ -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()
}
Loading

0 comments on commit 9bc52d4

Please sign in to comment.