Skip to content

Commit

Permalink
feat: add ecocredit batch metadata migration (#1812)
Browse files Browse the repository at this point in the history
Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
  • Loading branch information
ryanchristo and blushi authored Mar 22, 2023
1 parent 61091a3 commit a76941f
Show file tree
Hide file tree
Showing 7 changed files with 551 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ import (

regenupgrades "github.com/regen-network/regen-ledger/v5/app/upgrades"
"github.com/regen-network/regen-ledger/v5/app/upgrades/v5_0"
"github.com/regen-network/regen-ledger/v5/app/upgrades/v5_1"
"github.com/regen-network/regen-ledger/x/data/v2"
datamodule "github.com/regen-network/regen-ledger/x/data/v2/module"
"github.com/regen-network/regen-ledger/x/ecocredit/v3"
Expand Down Expand Up @@ -193,7 +194,10 @@ var (
return perms
}()

upgrades = []regenupgrades.Upgrade{v5_0.Upgrade}
upgrades = []regenupgrades.Upgrade{
v5_0.Upgrade,
v5_1.Upgrade,
}
)

func init() {
Expand Down
23 changes: 23 additions & 0 deletions app/upgrades/v5_1/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v5_1 //nolint:revive,stylecheck

import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/regen-network/regen-ledger/v5/app/upgrades"
)

const Name = "v5.1"

var Upgrade = upgrades.Upgrade{
UpgradeName: Name,
CreateUpgradeHandler: func(mm *module.Manager, cfg module.Configurator) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// run in-place store migrations for ecocredit module
return mm.RunMigrations(ctx, cfg, fromVM)
}
},
StoreUpgrades: storetypes.StoreUpgrades{},
}
44 changes: 44 additions & 0 deletions app/upgrades/v5_1/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package v5_1_test

import (
"testing"

"github.com/stretchr/testify/suite"

abci "github.com/tendermint/tendermint/abci/types"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/regen-network/regen-ledger/v5/app/testsuite"
)

type UpgradeTestSuite struct {
testsuite.UpgradeTestSuite
}

func TestUpgrade(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite))
}

const upgradeHeight = 5

func (suite *UpgradeTestSuite) TestUpgrade() {
suite.Setup()

suite.Ctx = suite.Ctx.WithBlockHeight(upgradeHeight - 1)
suite.Ctx = suite.Ctx.WithChainID("regen-1")

plan := upgradetypes.Plan{Name: "v5.1", Height: upgradeHeight}
err := suite.App.UpgradeKeeper.ScheduleUpgrade(suite.Ctx, plan)
suite.Require().NoError(err)

_, exists := suite.App.UpgradeKeeper.GetUpgradePlan(suite.Ctx)
suite.Require().True(exists)

suite.Ctx = suite.Ctx.WithBlockHeight(upgradeHeight)

suite.Require().NotPanics(func() {
beginBlockRequest := abci.RequestBeginBlock{}
suite.App.BeginBlocker(suite.Ctx, beginBlockRequest)
})
}
130 changes: 130 additions & 0 deletions x/ecocredit/migrations/v4/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package v4

import (
sdk "github.com/cosmos/cosmos-sdk/types"

ecocreditv1 "github.com/regen-network/regen-ledger/api/v2/regen/ecocredit/v1"
)

// MigrateState performs in-place store migrations from ConsensusVersion 3 to 4.
func MigrateState(sdkCtx sdk.Context, baseStore ecocreditv1.StateStore) error {
var batches []Batch

if sdkCtx.ChainID() == "regen-1" {
batches = getMainnetBatches()
}

for _, batch := range batches {
if err := migrateBatchMetadata(sdkCtx, baseStore, batch); err != nil {
return err
}
}

return nil
}

type Batch struct {
Denom string
NewMetadata string
}

func getMainnetBatches() []Batch {
return []Batch{
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-001-20150101-20151231-001
{
Denom: "C01-001-20150101-20151231-001",
NewMetadata: "regen:13toVg38ZRvFxPA2TBNnxGhabgogpJnv4LDm7YPgSuzuETiXz8GbnTF.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-001-20150101-20151231-002
{
Denom: "C01-001-20150101-20151231-002",
NewMetadata: "regen:13toVhTFtGtXxoHw7yy3QQVDGEpSQoVy4VARhtTWeuNQa5V25WUhagq.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-001-20150101-20151231-003
{
Denom: "C01-001-20150101-20151231-003",
NewMetadata: "regen:13toVghYySmmX9gm76MuwiPCC9AJy6Psb7wj6uj9JiBk4NvACGkpJDw.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-001-20150101-20151231-004
{
Denom: "C01-001-20150101-20151231-004",
NewMetadata: "regen:13toVgGhCxGuNrqKKugLY9thKAdLTgXHGxhbVutz2QLgtFmdZzPAKUB.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-001-20150101-20151231-005
{
Denom: "C01-001-20150101-20151231-005",
NewMetadata: "regen:13toVhaDUK1CHmqdZfKr6ZdF1L1ekTvUgjbEiGxWYqDWVZ937GUviFr.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-002-20190101-20191231-001
{
Denom: "C01-002-20190101-20191231-001",
NewMetadata: "regen:13toVgu5VbjKdfDKuwPfUoMeo2isi1ApbsCsaTCoyNknKnN9FE6j1hW.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-002-20190101-20191231-002
{
Denom: "C01-002-20190101-20191231-002",
NewMetadata: "regen:13toVgxDAxBev51DadD1he6gdkF6UPAoe25Y2xsSn3uDpzmHG3qGqRh.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-002-20190101-20191231-003
{
Denom: "C01-002-20190101-20191231-003",
NewMetadata: "regen:13toVgsTujvEeCS4hG9MXZii8eF9LBkrgzaw8mh2q54KfFtGFtH5DLi.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-002-20190101-20191231-004
{
Denom: "C01-002-20190101-20191231-004",
NewMetadata: "regen:13toVhKuR8NndSGZdciYTtCJf11hjYGwNvsWdjSPBmXNAqk8oL7u7XW.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-003-20150701-20160630-001
{
Denom: "C01-003-20150701-20160630-001",
NewMetadata: "regen:13toVhMT8c7hFZePMqa8raLBgCuLFo4MWbJ7QJRheFRC5dfPcmFZ4hk.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C02-001-20180101-20181231-001
{
Denom: "C02-001-20180101-20181231-001",
NewMetadata: "regen:13toVgpAwAm6fzYUUkD8UmioCYCP3GMbA3pdNkTM4wKeWc5UxmmCZW8.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C02-003-20200630-20220629-001
{
Denom: "C02-003-20200630-20220629-001",
NewMetadata: "regen:13toVh5g1AhGAWcTQCBXra1YfD2XJUbH35dvSLuEPAR8mQHJDY5ovVe.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C02-002-20211012-20241013-001
{
Denom: "C02-002-20211012-20241013-001",
NewMetadata: "regen:13toVhAukPXsjX5gMTADfUQzJQBjehJJoPwSavuU6GjyH5DtxZ5oVYS.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C02-004-20210102-20211207-001
{
Denom: "C02-004-20210102-20211207-001",
NewMetadata: "regen:13toVh1EoPoJJs1VSvmeQB3HHpXDgFBA19KqiP1tg4NByjWsFLJdjuq.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-002-20190101-20191231-005
{
Denom: "C01-002-20190101-20191231-005",
NewMetadata: "regen:13toVgwjqzxx3b9cRiRXBxrsUQB6D1WC4Kk8zZuXUfwnZ8WYtxRy4r5.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-001-20150101-20151231-006
{
Denom: "C01-001-20150101-20151231-006",
NewMetadata: "regen:13toVhaPG4MzeWcmoriPhQ5jRGx6ohhdzBPREoarrdqTRVCP8Xj7scM.rdf",
},
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-002-20190101-20191231-006
{
Denom: "C01-002-20190101-20191231-006",
NewMetadata: "regen:13toVh3NyL4uDzLFcrf6rUFMQnV8af87tBdSzh1Dvsc8zgEx193Y7hr.rdf",
},
}
}

func migrateBatchMetadata(ctx sdk.Context, baseStore ecocreditv1.StateStore, batch Batch) error {
b, err := baseStore.BatchTable().GetByDenom(ctx, batch.Denom)
if err != nil {
return err
}

b.Metadata = batch.NewMetadata

return baseStore.BatchTable().Update(ctx, b)
}
Loading

0 comments on commit a76941f

Please sign in to comment.