2022-02-17 03:34:03 -08:00
|
|
|
package v046_test
|
2022-02-16 09:36:50 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/authz"
|
2022-02-17 03:34:03 -08:00
|
|
|
v046 "github.com/cosmos/cosmos-sdk/x/authz/migrations/v046"
|
2022-02-16 09:36:50 -08:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
|
|
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMigration(t *testing.T) {
|
|
|
|
encCfg := simapp.MakeTestEncodingConfig()
|
|
|
|
cdc := encCfg.Codec
|
|
|
|
authzKey := sdk.NewKVStoreKey("authz")
|
|
|
|
ctx := testutil.DefaultContext(authzKey, sdk.NewTransientStoreKey("transient_test"))
|
|
|
|
granter1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
|
|
|
|
grantee1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
|
|
|
|
granter2 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
|
|
|
|
grantee2 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
|
|
|
|
|
|
|
|
sendMsgType := banktypes.SendAuthorization{}.MsgTypeURL()
|
|
|
|
genericMsgType := sdk.MsgTypeURL(&govtypes.MsgVote{})
|
|
|
|
coins100 := sdk.NewCoins(sdk.NewInt64Coin("atom", 100))
|
2022-03-24 11:27:23 -07:00
|
|
|
blockTime := ctx.BlockTime()
|
|
|
|
oneDay := blockTime.AddDate(0, 0, 1)
|
|
|
|
oneYear := blockTime.AddDate(1, 0, 0)
|
2022-02-16 09:36:50 -08:00
|
|
|
|
|
|
|
grants := []struct {
|
|
|
|
granter sdk.AccAddress
|
|
|
|
grantee sdk.AccAddress
|
|
|
|
msgType string
|
|
|
|
authorization func() authz.Grant
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
granter1,
|
|
|
|
grantee1,
|
|
|
|
sendMsgType,
|
|
|
|
func() authz.Grant {
|
|
|
|
any, err := codectypes.NewAnyWithValue(banktypes.NewSendAuthorization(coins100))
|
|
|
|
require.NoError(t, err)
|
|
|
|
return authz.Grant{
|
|
|
|
Authorization: any,
|
2022-03-24 11:27:23 -07:00
|
|
|
Expiration: &oneDay,
|
2022-02-16 09:36:50 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
granter1,
|
|
|
|
grantee2,
|
|
|
|
sendMsgType,
|
|
|
|
func() authz.Grant {
|
|
|
|
any, err := codectypes.NewAnyWithValue(banktypes.NewSendAuthorization(coins100))
|
|
|
|
require.NoError(t, err)
|
|
|
|
return authz.Grant{
|
|
|
|
Authorization: any,
|
2022-03-24 11:27:23 -07:00
|
|
|
Expiration: &oneDay,
|
2022-02-16 09:36:50 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
granter2,
|
|
|
|
grantee1,
|
|
|
|
genericMsgType,
|
|
|
|
func() authz.Grant {
|
|
|
|
any, err := codectypes.NewAnyWithValue(authz.NewGenericAuthorization(genericMsgType))
|
|
|
|
require.NoError(t, err)
|
|
|
|
return authz.Grant{
|
|
|
|
Authorization: any,
|
2022-03-24 11:27:23 -07:00
|
|
|
Expiration: &oneYear,
|
2022-02-16 09:36:50 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
granter2,
|
|
|
|
grantee2,
|
|
|
|
genericMsgType,
|
|
|
|
func() authz.Grant {
|
|
|
|
any, err := codectypes.NewAnyWithValue(authz.NewGenericAuthorization(genericMsgType))
|
|
|
|
require.NoError(t, err)
|
|
|
|
return authz.Grant{
|
|
|
|
Authorization: any,
|
2022-03-24 11:27:23 -07:00
|
|
|
Expiration: &blockTime,
|
2022-02-16 09:36:50 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
store := ctx.KVStore(authzKey)
|
|
|
|
|
|
|
|
for _, g := range grants {
|
|
|
|
grant := g.authorization()
|
2022-02-17 03:34:03 -08:00
|
|
|
store.Set(v046.GrantStoreKey(g.grantee, g.granter, g.msgType), cdc.MustMarshal(&grant))
|
2022-02-16 09:36:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(1 * time.Hour))
|
2022-02-17 03:34:03 -08:00
|
|
|
require.NoError(t, v046.MigrateStore(ctx, authzKey, cdc))
|
2022-02-16 09:36:50 -08:00
|
|
|
|
2022-02-17 03:34:03 -08:00
|
|
|
require.NotNil(t, store.Get(v046.GrantStoreKey(grantee1, granter2, genericMsgType)))
|
|
|
|
require.NotNil(t, store.Get(v046.GrantStoreKey(grantee1, granter1, sendMsgType)))
|
|
|
|
require.Nil(t, store.Get(v046.GrantStoreKey(grantee2, granter2, genericMsgType)))
|
2022-02-16 09:36:50 -08:00
|
|
|
}
|