Skip to content

Commit

Permalink
fix(x/ecocredit): add basket state migration (backport #1284) (#1287)
Browse files Browse the repository at this point in the history
* fix(x/ecocredit): add basket state migration (#1284)

* fix: add basket state migration

* update changelog

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>
(cherry picked from commit 6c9e000)

# Conflicts:
#	CHANGELOG.md

* resolve conflicts

Co-authored-by: MD Aleem <72057206+aleem1314@users.noreply.github.com>
Co-authored-by: ryanchristo <12519942+ryanchristo@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 20, 2022
1 parent b1c45c6 commit 256bc27
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- [#1278](https://github.com/regen-network/regen-ledger/pull/1278) Fix `open` not set in `Msg/CreateBatch`
- [#1281](https://github.com/regen-network/regen-ledger/pull/1281) Fix curator address in state migrations
- [#1284](https://github.com/regen-network/regen-ledger/pull/1284) Fix basket state migration

## [v4.0.0-rc1](https://github.com/regen-network/regen-ledger/releases/tag/v4.0.0-rc1) - 2022-07-15

Expand Down
40 changes: 40 additions & 0 deletions x/ecocredit/migrations/v3/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,53 @@ func MigrateState(sdkCtx sdk.Context, storeKey storetypes.StoreKey,
return err
}

// migrate basket state
if err = migrateBasketState(basketStore, ctx, oldBatchDenomToNewDenomMap); err != nil {
return err
}

if err := patchMigrate(ctx, sdkCtx, ss, basketStore, oldBatchDenomToNewDenomMap); err != nil {
return err
}

return nil
}

// migrateBasketState updates basket balance old batch denom to new batch denom.
func migrateBasketState(basketStore basketapi.StateStore, ctx context.Context, oldBatchDenomToNewDenomMap map[string]string) error {
var balances []*basketapi.BasketBalance
itr, err := basketStore.BasketBalanceTable().List(ctx, basketapi.BasketBalancePrimaryKey{})
if err != nil {
return err
}

for itr.Next() {
bBalance, err := itr.Value()
if err != nil {
return err
}

balances = append(balances, bBalance)
}
itr.Close()

for i := 0; i < len(balances); i++ {
if err := basketStore.BasketBalanceTable().Delete(ctx, balances[i]); err != nil {
return err
}
}

for i := 0; i < len(balances); i++ {
update := balances[i]
update.BatchDenom = oldBatchDenomToNewDenomMap[update.BatchDenom]
if err := basketStore.BasketBalanceTable().Insert(ctx, update); err != nil {
return err
}
}

return nil
}

// migrateBalances migrates ecocredit tradable and retired balances to orm v1
func migrateBalances(store storetypes.KVStore, ss api.StateStore, ctx context.Context, batchDenomToBatchMap map[string]batchMapT) error {
// migrate tradable balances to ORM v1
Expand Down
25 changes: 25 additions & 0 deletions x/ecocredit/migrations/v3/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ func TestMigrations(t *testing.T) {
basketStore, err := basketapi.NewStateStore(ormdb)
require.Nil(t, err)

require.NoError(t, basketStore.BasketBalanceTable().Insert(ormCtx, &basketapi.BasketBalance{
BasketId: 1,
BatchDenom: bd1,
Balance: "100",
BatchStartDate: nil,
}))
require.NoError(t, basketStore.BasketBalanceTable().Insert(ormCtx, &basketapi.BasketBalance{
BasketId: 2,
BatchDenom: bd2,
Balance: "100",
BatchStartDate: nil,
}))

err = v3.MigrateState(sdkCtx, ecocreditKey, encCfg.Marshaler, ss, basketStore, paramStore)
require.NoError(t, err)

Expand Down Expand Up @@ -330,6 +343,18 @@ func TestMigrations(t *testing.T) {
bz = store.Get(tradableSKey1)
require.Nil(t, bz)

// verify basket denom migration
bBalance, err := basketStore.BasketBalanceTable().Get(ormCtx, 1, expbd1)
require.NoError(t, err)
require.Equal(t, bBalance.BatchDenom, expbd1)
require.Equal(t, bBalance.Balance, "100")
require.Equal(t, bBalance.BasketId, uint64(1))

bBalance, err = basketStore.BasketBalanceTable().Get(ormCtx, 2, expbd2)
require.NoError(t, err)
require.Equal(t, bBalance.BatchDenom, expbd2)
require.Equal(t, bBalance.Balance, "100")
require.Equal(t, bBalance.BasketId, uint64(2))
}

func formatBatchDenom(classId string, batchSeqNo uint64, startDate *time.Time, endDate *time.Time) string {
Expand Down

0 comments on commit 256bc27

Please sign in to comment.