Skip to content

Commit

Permalink
feat: upgrade tokensfactory to 47 with migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Sep 7, 2023
1 parent 20d00e8 commit 5aa3785
Show file tree
Hide file tree
Showing 16 changed files with 648 additions and 101 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ test-tokenfactory:
test-chain-upgrade:
@echo "Testing software upgrade..."
bash ./scripts/tests/chain-upgrade/chain-upgrade.sh
clean-testing-data

clean-testing-data:
@echo "Killing terrad and removing previous data"
Expand Down
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,11 @@ func NewTerraApp(
// ... other modules keepers
app.TokenFactoryKeeper = tokenfactorykeeper.NewKeeper(
keys[tokenfactorytypes.StoreKey],
app.GetSubspace(tokenfactorytypes.ModuleName),
app.AccountKeeper,
app.BankKeeper,
app.DistrKeeper,
appCodec,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
// Create IBC Keeper
app.IBCKeeper = ibckeeper.NewKeeper(
Expand Down Expand Up @@ -767,7 +767,7 @@ func NewTerraApp(
router.NewAppModule(&app.RouterKeeper),
wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.MsgServiceRouter(), app.GetSubspace(wasmtypes.ModuleName)),
ibchooks.NewAppModule(app.AccountKeeper),
tokenfactory.NewAppModule(app.TokenFactoryKeeper, app.AccountKeeper, app.BankKeeper),
tokenfactory.NewAppModule(app.TokenFactoryKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(tokenfactorytypes.ModuleName)),
alliance.NewAppModule(appCodec, app.AllianceKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
pob.NewAppModule(appCodec, app.BuilderKeeper),
)
Expand Down
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion proto/osmosis/tokenfactory/v1beta1/tx.proto
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
syntax = "proto3";
package osmosis.tokenfactory.v1beta1;

import "cosmos_proto/cosmos.proto";
import "cosmos/msg/v1/msg.proto";
import "gogoproto/gogo.proto";
import "amino/amino.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos/bank/v1beta1/bank.proto";
import "osmosis/tokenfactory/v1beta1/params.proto";

option go_package = "github.com/osmosis-labs/osmosis/v17/x/tokenfactory/types";

// Msg defines the tokefactory module's gRPC message service.
service Msg {
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
rpc CreateDenom(MsgCreateDenom) returns (MsgCreateDenomResponse);
rpc Mint(MsgMint) returns (MsgMintResponse);
rpc Burn(MsgBurn) returns (MsgBurnResponse);
Expand All @@ -21,6 +24,15 @@ service Msg {
rpc ForceTransfer(MsgForceTransfer) returns (MsgForceTransferResponse);
}

message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";

string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
Params params = 2 [(gogoproto.nullable) = false];
}

message MsgUpdateParamsResponse {}

// MsgCreateDenom defines the message structure for the CreateDenom gRPC service
// method. It allows an account to create a new denom. It requires a sender
// address and a sub denomination. The (sender_address, sub_denomination) tuple
Expand Down
26 changes: 26 additions & 0 deletions x/tokenfactory/exported/exported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package exported

import (
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

// GenesisBalance defines a genesis balance interface that allows for account
// address and balance retrieval.
type GenesisBalance interface {
GetAddress() sdk.AccAddress
GetCoins() sdk.Coins
}

type (
ParamSet = paramtypes.ParamSet

// Subspace defines an interface that implements the legacy x/params Subspace
// type.
//
// NOTE: This is used solely for migration of x/params managed parameters.
Subspace interface {
GetParamSetIfExists(ctx sdk.Context, ps ParamSet)
SetParamSet(ctx sdk.Context, ps ParamSet)
}
)
5 changes: 4 additions & 1 deletion x/tokenfactory/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
if genState.Params.DenomCreationFee == nil {
genState.Params.DenomCreationFee = sdk.NewCoins()
}
k.SetParams(ctx, genState.Params)

if err := k.SetParams(ctx, genState.Params); err != nil {
panic(err)
}

for _, genDenom := range genState.GetFactoryDenoms() {
creator, _, err := types.DeconstructDenom(genDenom.GetDenom())
Expand Down
21 changes: 10 additions & 11 deletions x/tokenfactory/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
customtypes "github.com/terra-money/core/v2/custom/bank/keeper"
"github.com/terra-money/core/v2/x/tokenfactory/types"
)
Expand All @@ -20,40 +19,36 @@ type (
Keeper struct {
storeKey storetypes.StoreKey

paramSpace paramtypes.Subspace

accountKeeper types.AccountKeeper
bankKeeper customtypes.Keeper
contractKeeper types.ContractKeeper

communityPoolKeeper types.CommunityPoolKeeper

cdc codec.BinaryCodec
cdc codec.BinaryCodec
authority string
}
)

// NewKeeper returns a new instance of the x/tokenfactory keeper
func NewKeeper(
storeKey storetypes.StoreKey,
paramSpace paramtypes.Subspace,
accountKeeper types.AccountKeeper,
bankKeeper customtypes.Keeper,
communityPoolKeeper types.CommunityPoolKeeper,
cdc codec.BinaryCodec,
authority string,
) Keeper {
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

return Keeper{
storeKey: storeKey,
paramSpace: paramSpace,
storeKey: storeKey,

accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
communityPoolKeeper: communityPoolKeeper,

cdc: cdc,
cdc: cdc,
authority: authority,
}
}

Expand Down Expand Up @@ -92,3 +87,7 @@ func (k *Keeper) SetContractKeeper(contractKeeper types.ContractKeeper) {
func (k Keeper) CreateModuleAccount(ctx sdk.Context) {
k.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
}

func (k Keeper) GetAuthority() string {
return k.authority
}
16 changes: 12 additions & 4 deletions x/tokenfactory/keeper/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/terra-money/core/v2/x/tokenfactory/exported"
v2 "github.com/terra-money/core/v2/x/tokenfactory/migrations/v2"
v3 "github.com/terra-money/core/v2/x/tokenfactory/migrations/v3"
)

type Migrator struct {
keeper Keeper
keeper Keeper
legacySubspace exported.Subspace
}

func NewMigrator(keeper Keeper) Migrator {
func NewMigrator(keeper Keeper, legacySubspace exported.Subspace) Migrator {
return Migrator{
keeper: keeper,
keeper: keeper,
legacySubspace: legacySubspace,
}
}

func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.keeper.paramSpace)
return v2.MigrateStore(ctx, m.legacySubspace, m.keeper.cdc)
}

func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v3.MigrateStore(ctx, m.keeper.storeKey, m.legacySubspace, m.keeper.cdc)
}
14 changes: 14 additions & 0 deletions x/tokenfactory/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package keeper
import (
"context"

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/terra-money/core/v2/x/tokenfactory/types"
)
Expand All @@ -23,6 +25,18 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {

var _ types.MsgServer = msgServer{}

func (m msgServer) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if m.Keeper.GetAuthority() != msg.Authority {
return nil, sdkerrors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", m.GetAuthority(), msg.Authority)
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
if err := m.SetParams(sdkCtx, msg.Params); err != nil {
return nil, err
}
return &types.MsgUpdateParamsResponse{}, nil
}

func (server msgServer) CreateDenom(goCtx context.Context, msg *types.MsgCreateDenom) (*types.MsgCreateDenomResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down
20 changes: 17 additions & 3 deletions x/tokenfactory/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ import (

// GetParams returns the total set params.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramSpace.GetParamSet(ctx, &params)
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
return params
}

k.cdc.MustUnmarshal(bz, &params)
return params
}

// SetParams sets the total set of params.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
store := ctx.KVStore(k.storeKey)
bz, err := k.cdc.Marshal(&params)
if err != nil {
return err
}

store.Set(types.ParamsKey, bz)

return nil
}
9 changes: 5 additions & 4 deletions x/tokenfactory/migrations/v2/store.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package v2

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/terra-money/core/v2/app/config"
"github.com/terra-money/core/v2/x/tokenfactory/exported"
"github.com/terra-money/core/v2/x/tokenfactory/types"
)

func MigrateStore(ctx sdk.Context, subspace paramtypes.Subspace) error {
func MigrateStore(ctx sdk.Context, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error {
var params types.Params
subspace.GetParamSetIfExists(ctx, &params)
legacySubspace.GetParamSetIfExists(ctx, &params)

// FIX: when token factory was implemented for the first time *denom creation fee* field was setup to
// nil which makes this migration fails. This if statement will fix the issue:
Expand All @@ -19,6 +20,6 @@ func MigrateStore(ctx sdk.Context, subspace paramtypes.Subspace) error {
}

params.DenomCreationGasConsume = types.DefaultCreationGasFee
subspace.SetParamSet(ctx, &params)
legacySubspace.SetParamSet(ctx, &params)
return nil
}
32 changes: 32 additions & 0 deletions x/tokenfactory/migrations/v3/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package v3

import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/terra-money/core/v2/x/tokenfactory/exported"
"github.com/terra-money/core/v2/x/tokenfactory/types"
)

// MigrateStore migrates the x/tokenfactory module state from the consensus version 2 to
// version 3. Specifically, it takes the parameters that are currently stored
// and managed by the x/params module and stores them directly into the x/tokenfactory
// module state.
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
var currParams types.Params
legacySubspace.GetParamSetIfExists(ctx, &currParams)

if err := currParams.Validate(); err != nil {
return err
}

bz, err := cdc.Marshal(&currParams)
if err != nil {
return err
}

store.Set(types.ParamsKey, bz)

return nil
}
13 changes: 11 additions & 2 deletions x/tokenfactory/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra"

"github.com/terra-money/core/v2/x/tokenfactory/client/cli"
"github.com/terra-money/core/v2/x/tokenfactory/exported"
"github.com/terra-money/core/v2/x/tokenfactory/keeper"
"github.com/terra-money/core/v2/x/tokenfactory/types"
)
Expand Down Expand Up @@ -104,18 +105,23 @@ type AppModule struct {
keeper keeper.Keeper
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper

// legacySubspace is used solely for migration of x/params managed parameters
legacySubspace exported.Subspace
}

func NewAppModule(
keeper keeper.Keeper,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
legacySubspace exported.Subspace,
) AppModule {
return AppModule{
AppModuleBasic: NewAppModuleBasic(),
keeper: keeper,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
legacySubspace: legacySubspace,
}
}

Expand All @@ -133,10 +139,13 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

migrator := keeper.NewMigrator(am.keeper)
migrator := keeper.NewMigrator(am.keeper, am.legacySubspace)
if err := cfg.RegisterMigration(types.ModuleName, 1, migrator.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/tokenfactory from version 1 to 2: %v", err))
}
if err := cfg.RegisterMigration(types.ModuleName, 2, migrator.Migrate2to3); err != nil {
panic(fmt.Sprintf("failed to migrate x/tokenfactory from version 2 to 3: %v", err))
}
}

// RegisterInvariants registers the x/tokenfactory module's invariants.
Expand All @@ -161,7 +170,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return 3 }

// BeginBlock executes all ABCI BeginBlock logic respective to the tokenfactory module.
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down
2 changes: 2 additions & 0 deletions x/tokenfactory/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var (
BeforeSendHookAddressPrefixKey = "beforesendhook"
)

var ParamsKey = []byte{0x00}

// GetDenomPrefixStore returns the store prefix where all the data associated with a specific denom
// is stored
func GetDenomPrefixStore(denom string) []byte {
Expand Down
Loading

0 comments on commit 5aa3785

Please sign in to comment.