-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(x/ecocredit): add basket state migration (#1397)
* init * feat: add basket state migrations * feat: wire up state migration * chore: fix lint error * chore: update package name * fix: update upgrade handler * Update x/ecocredit/migrations/v3/state.go Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com> Co-authored-by: ryanchristo <12519942+ryanchristo@users.noreply.github.com>
- Loading branch information
1 parent
900d0f1
commit 494d801
Showing
8 changed files
with
192 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package v3 | ||
|
||
import ( | ||
basev1beta1 "github.com/cosmos/cosmos-sdk/api/cosmos/base/v1beta1" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" | ||
|
||
basketapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" | ||
api "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1" | ||
"github.com/regen-network/regen-ledger/x/ecocredit/core" | ||
) | ||
|
||
// MigrateState performs in-place store migrations from ConsensusVersion 2 to 3. | ||
func MigrateState(sdkCtx sdk.Context, ss api.StateStore, basketStore basketapi.StateStore, subspace paramtypes.Subspace) error { | ||
// TODO: migrate core params | ||
|
||
// migrate basket params | ||
var params core.Params | ||
subspace.GetParamSet(sdkCtx, ¶ms) | ||
|
||
// verify basket fee is valid | ||
if err := params.BasketFee.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
basketFees := []*basev1beta1.Coin{} | ||
for _, coin := range params.BasketFee { | ||
basketFees = append(basketFees, &basev1beta1.Coin{ | ||
Denom: coin.Denom, | ||
Amount: coin.Amount.String(), | ||
}) | ||
} | ||
if err := basketStore.BasketFeesTable().Save(sdkCtx, &basketapi.BasketFees{ | ||
Fees: basketFees, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package v3_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/orm/model/ormdb" | ||
"github.com/cosmos/cosmos-sdk/orm/model/ormtable" | ||
"github.com/cosmos/cosmos-sdk/orm/testing/ormtest" | ||
"github.com/cosmos/cosmos-sdk/simapp" | ||
"github.com/cosmos/cosmos-sdk/store" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
dbm "github.com/tendermint/tm-db" | ||
"gotest.tools/v3/assert" | ||
|
||
basketapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" | ||
api "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1" | ||
"github.com/regen-network/regen-ledger/x/ecocredit" | ||
"github.com/regen-network/regen-ledger/x/ecocredit/core" | ||
v3 "github.com/regen-network/regen-ledger/x/ecocredit/migrations/v3" | ||
) | ||
|
||
func TestMigrations(t *testing.T) { | ||
|
||
ecocreditKey := sdk.NewKVStoreKey("ecocredit") | ||
tecocreditKey := sdk.NewTransientStoreKey("transient_test") | ||
encCfg := simapp.MakeTestEncodingConfig() | ||
paramStore := paramtypes.NewSubspace(encCfg.Codec, encCfg.Amino, ecocreditKey, tecocreditKey, ecocredit.ModuleName) | ||
|
||
db := dbm.NewMemDB() | ||
cms := store.NewCommitMultiStore(db) | ||
cms.MountStoreWithDB(ecocreditKey, storetypes.StoreTypeIAVL, db) | ||
cms.MountStoreWithDB(tecocreditKey, storetypes.StoreTypeTransient, db) | ||
assert.NilError(t, cms.LoadLatestVersion()) | ||
ormCtx := ormtable.WrapContextDefault(ormtest.NewMemoryBackend()) | ||
sdkCtx := sdk.NewContext(cms, tmproto.Header{}, false, log.NewNopLogger()).WithContext(ormCtx) | ||
|
||
paramStore.WithKeyTable(core.ParamKeyTable()) | ||
|
||
// initialize params | ||
paramStore.SetParamSet(sdkCtx, &core.Params{ | ||
CreditClassFee: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))), | ||
BasketFee: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10)), sdk.NewCoin("uregen", sdk.NewInt(2000000))), | ||
AllowedClassCreators: []string{}, | ||
AllowlistEnabled: true, | ||
}) | ||
|
||
var params core.Params | ||
paramStore.GetParamSet(sdkCtx, ¶ms) | ||
|
||
ormdb, err := ormdb.NewModuleDB(&ecocredit.ModuleSchema, ormdb.ModuleDBOptions{}) | ||
assert.NilError(t, err) | ||
coreStore, err := api.NewStateStore(ormdb) | ||
assert.NilError(t, err) | ||
|
||
basketStore, err := basketapi.NewStateStore(ormdb) | ||
assert.NilError(t, err) | ||
|
||
assert.NilError(t, v3.MigrateState(sdkCtx, coreStore, basketStore, paramStore)) | ||
|
||
// verify basket params migrated to orm table | ||
basketFees, err := basketStore.BasketFeesTable().Get(sdkCtx) | ||
assert.NilError(t, err) | ||
|
||
assert.Equal(t, len(basketFees.Fees), 2) | ||
assert.Equal(t, basketFees.Fees[0].Denom, sdk.DefaultBondDenom) | ||
assert.Equal(t, basketFees.Fees[0].Amount, "10") | ||
assert.Equal(t, basketFees.Fees[1].Denom, "uregen") | ||
assert.Equal(t, basketFees.Fees[1].Amount, "2000000") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" | ||
v3 "github.com/regen-network/regen-ledger/x/ecocredit/migrations/v3" | ||
) | ||
|
||
func (s serverImpl) RunMigrations(ctx sdk.Context, cdc codec.Codec) error { | ||
// Migrator is a struct for handling in-place store migrations. | ||
type Migrator struct { | ||
keeper Keeper | ||
legacySubspace paramtypes.Subspace | ||
} | ||
|
||
// NewMigrator returns a new Migrator. | ||
func NewMigrator(keeper Keeper, legacySubspace paramtypes.Subspace) Migrator { | ||
return Migrator{ | ||
keeper: keeper, | ||
legacySubspace: legacySubspace, | ||
} | ||
} | ||
|
||
// Migrate2to3 migrates from version 2 to 3. | ||
func (m Migrator) Migrate2to3(ctx sdk.Context) error { | ||
|
||
coreStore, basketStore, _ := m.keeper.GetStateStores() | ||
if err := v3.MigrateState(ctx, coreStore, basketStore, m.legacySubspace); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters