Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonWeng committed Apr 26, 2023
1 parent 278c8a7 commit 89c3b6a
Show file tree
Hide file tree
Showing 8 changed files with 1,185 additions and 144 deletions.
38 changes: 38 additions & 0 deletions proto/mint/v1beta1/mint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,41 @@ message Params {
];
}


// Legacy Protobufs used for migration purposes

// Minter represents the most recent
message Version2Minter {
string last_mint_amount = 1 [
(gogoproto.moretags) = "yaml:\"last_mint_amount\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string last_mint_date = 2 [
(gogoproto.moretags) = "yaml:\"last_mint_date\""
];
int64 last_mint_height = 3 [
(gogoproto.moretags) = "yaml:\"last_mint_height\""
];
string denom = 4 [
(gogoproto.moretags) = "yaml:\"denom\""
];
}

message Version2ScheduledTokenRelease {
string date = 1; // yyyy-mm-dd
int64 token_release_amount = 2;
}

// Params holds parameters for the mint module.
message Version2Params {
option (gogoproto.goproto_stringer) = false;

// type of coin to mint
string mint_denom = 1;
// List of token release schedules
repeated Version2ScheduledTokenRelease token_release_schedule = 2 [
(gogoproto.moretags) = "yaml:\"token_release_schedule\"",
(gogoproto.nullable) = false
];
}
19 changes: 17 additions & 2 deletions x/mint/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"
"time"

"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
epochTypes "github.com/sei-protocol/sei-chain/x/epoch/types"
"github.com/sei-protocol/sei-chain/x/mint/types"
"github.com/tendermint/tendermint/libs/log"
)

// Keeper of the mint store
Expand Down Expand Up @@ -149,6 +148,22 @@ func (k Keeper) GetOrUpdateLatestMinter(
)
}

func (k Keeper) GetCdc() codec.BinaryCodec {
return k.cdc
}

func (k Keeper) GetStoreKey() sdk.StoreKey {
return k.storeKey
}

func (k Keeper) GetParamSpace() paramtypes.Subspace {
return k.paramSpace
}

func (k *Keeper) SetParamSpace(subspace paramtypes.Subspace) {
k.paramSpace = subspace
}

func GetNextScheduledTokenRelease(
epoch epochTypes.Epoch,
tokenReleaseSchedule []types.ScheduledTokenRelease,
Expand Down
66 changes: 66 additions & 0 deletions x/mint/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package keeper

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/mint/types"
)
Expand All @@ -21,3 +24,66 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error {
m.keeper.paramSpace.SetParamSet(ctx, &defaultParams)
return nil
}

// Migrate1to2 migrates from version 1 to 2
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
ctx.Logger().Info("Migrating mint module from v2 to v3")
store := ctx.KVStore(m.keeper.storeKey)
// Migrate Minter First
minterBytes := store.Get(types.MinterKey)
if minterBytes == nil {
panic("stored minter should not have been nil")
}

var oldMinter types.Version2Minter
m.keeper.cdc.MustUnmarshal(minterBytes, &oldMinter)

newMinter := types.Minter{
StartDate: oldMinter.GetLastMintDate(),
EndDate: oldMinter.GetLastMintDate(),
Denom: sdk.DefaultBondDenom,
TotalMintAmount: oldMinter.LastMintAmount.RoundInt().Uint64(),
RemainingMintAmount: 0,
LastMintDate: oldMinter.GetLastMintDate(),
LastMintHeight: uint64(oldMinter.GetLastMintHeight()),
LastMintAmount: oldMinter.LastMintAmount.RoundInt().Uint64(),
}
ctx.Logger().Info("Migrating mint module from v2 to v3", "oldMinter", oldMinter.String(), "newMinter", newMinter.String())
m.keeper.SetMinter(ctx, newMinter)

// Migrate TokenReleaseSchedule

var oldTokenReleaseSchedules []types.Version2ScheduledTokenRelease
oldTokenReleaseSchedulesBytes := m.keeper.GetParamSpace().GetRaw(ctx, types.KeyTokenReleaseSchedule)
err := codec.NewLegacyAmino().UnmarshalJSON(oldTokenReleaseSchedulesBytes, &oldTokenReleaseSchedules)
if err != nil {
panic(fmt.Sprintf("Key not found or error: %s", err))
}

var oldMintDenom string
oldMintDenomBytes := m.keeper.GetParamSpace().GetRaw(ctx, types.KeyMintDenom)
err = codec.NewLegacyAmino().UnmarshalJSON(oldMintDenomBytes, &oldMintDenom)
if err != nil {
panic(fmt.Sprintf("Key not found or error: %s", err))
}
ctx.Logger().Info("Migrating mint module from v2 to v3", "oldTokenReleaseSchedules", oldTokenReleaseSchedules, "oldMintDenom", oldMintDenom)
fmt.Println("Migrating mint module from v2 to v3", "oldTokenReleaseSchedules", oldTokenReleaseSchedules, "oldMintDenom", oldMintDenom)

newTokenReleaseSchedule := []types.ScheduledTokenRelease{}
for _, oldTokenReleaseSchedule := range oldTokenReleaseSchedules {
newSchedule := types.ScheduledTokenRelease{
TokenReleaseAmount: uint64(oldTokenReleaseSchedule.GetTokenReleaseAmount()),
StartDate: oldTokenReleaseSchedule.GetDate(),
EndDate: oldTokenReleaseSchedule.GetDate(),
}
newTokenReleaseSchedule = append(newTokenReleaseSchedule, newSchedule)
}
newParams := types.Params{
MintDenom: oldMintDenom,
TokenReleaseSchedule: newTokenReleaseSchedule,
}
m.keeper.SetParams(ctx, newParams)
ctx.Logger().Info("Migrating mint module from v2 to v3", "newParams", newParams.String())

return nil
}
152 changes: 152 additions & 0 deletions x/mint/keeper/migrations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package keeper_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/sei-protocol/sei-chain/x/mint/keeper"
"github.com/sei-protocol/sei-chain/x/mint/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
)

type MockAccountKeeper struct {
ModuleAddress sdk.AccAddress
ModuleAccount authtypes.ModuleAccountI
}

func (m MockAccountKeeper) GetModuleAddress(name string) sdk.AccAddress {
address, _ := sdk.AccAddressFromBech32("sei1t4xhq2pnhnf223zr4z5lw02vsrxwf74z604kja")
return address
}

func (m MockAccountKeeper) SetModuleAccount(ctx sdk.Context, account authtypes.ModuleAccountI) {
m.ModuleAccount = account
}

func (m MockAccountKeeper) GetModuleAccount(ctx sdk.Context, moduleName string) authtypes.ModuleAccountI {
return m.ModuleAccount
}

func TestMigrate2to3(t *testing.T) {

storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, sdk.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

paramsSubspace := typesparams.NewSubspace(cdc,
codec.NewLegacyAmino(),
storeKey,
memStoreKey,
"MintParams",
)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
if !paramsSubspace.HasKeyTable() {
paramsSubspace = paramsSubspace.WithKeyTable(paramtypes.NewKeyTable().RegisterParamSet(&types.Version2Params{}))
}
store := ctx.KVStore(storeKey)

// Set up the old Minter and Params
oldMinter := types.Version2Minter{
LastMintAmount: sdk.NewDec(1000),
LastMintDate: "2021-01-01",
LastMintHeight: 100,
Denom: sdk.DefaultBondDenom,
}

oldTokenReleaseSchedule := []types.Version2ScheduledTokenRelease{
{
Date: "2021-02-01",
TokenReleaseAmount: 500,
},
{
Date: "2021-03-01",
TokenReleaseAmount: 1000,
},
}

// Start up post upgrade with new Param Space
newParamsSubspace := typesparams.NewSubspace(cdc,
codec.NewLegacyAmino(),
storeKey,
memStoreKey,
"MintParams",
)
mintKeeper := keeper.NewKeeper(
cdc,
storeKey,
newParamsSubspace,
nil,
MockAccountKeeper{},
nil,
nil,
"fee_collector",
)

oldParams := types.Version2Params{
MintDenom: sdk.DefaultBondDenom,
TokenReleaseSchedule: oldTokenReleaseSchedule,
}

// Store the old Minter and Params
b := cdc.MustMarshal(&oldMinter)
store.Set(types.MinterKey, b)
paramsSubspace.SetParamSet(ctx, &oldParams)

// Perform the migration

// Use new keeper or param space here
migrator := keeper.NewMigrator(mintKeeper)
err := migrator.Migrate2to3(ctx)
require.NoError(t, err)

// Check if the new Minter was stored correctly
minterBytes := store.Get(types.MinterKey)
if minterBytes == nil {
panic("stored minter should not have been nil")
}
var newMinter types.Minter
cdc.MustUnmarshal(minterBytes, &newMinter)

require.Equal(t, oldMinter.LastMintDate, newMinter.StartDate)
require.Equal(t, oldMinter.LastMintDate, newMinter.EndDate)
require.Equal(t, oldMinter.LastMintDate, newMinter.LastMintDate)
require.Equal(t, oldMinter.LastMintHeight, int64(newMinter.LastMintHeight))
require.Equal(t, oldMinter.LastMintAmount.RoundInt().Uint64(), newMinter.TotalMintAmount)
require.Equal(t, oldMinter.LastMintAmount.RoundInt().Uint64(), newMinter.LastMintAmount)

// Check if the new Params were stored correctly
var newTokenReleaseSchedules []types.ScheduledTokenRelease
mintKeeper.GetParamSpace().Get(ctx, types.KeyTokenReleaseSchedule, &newTokenReleaseSchedules)

var newMintDenom string
mintKeeper.GetParamSpace().Get(ctx, types.KeyMintDenom, &newMintDenom)

require.Equal(t, oldParams.MintDenom, newMintDenom)
require.Len(t, newTokenReleaseSchedules, len(oldParams.TokenReleaseSchedule))

for i, oldSchedule := range oldParams.TokenReleaseSchedule {
newSchedule := newTokenReleaseSchedules[i]

require.Equal(t, oldSchedule.Date, newSchedule.StartDate)
require.Equal(t, oldSchedule.Date, newSchedule.EndDate)
require.Equal(t, uint64(oldSchedule.TokenReleaseAmount), newSchedule.TokenReleaseAmount)
}
}
3 changes: 2 additions & 1 deletion x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper))
m := keeper.NewMigrator(am.keeper)
_ = cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2)
_ = cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3)
}

// InitGenesis performs genesis initialization for the mint module. It returns
Expand All @@ -148,7 +149,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

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

// BeginBlock returns the begin blocker for the mint module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down
2 changes: 2 additions & 0 deletions x/mint/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const (
// StoreKey is the default store key for mint
StoreKey = ModuleName

MemStoreKey = "mem_mint"

// QuerierRoute is the querier route for the minting store.
QuerierRoute = StoreKey

Expand Down
Loading

0 comments on commit 89c3b6a

Please sign in to comment.