-
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: add ecocredit batch metadata migration (#1812)
Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
- Loading branch information
1 parent
61091a3
commit a76941f
Showing
7 changed files
with
551 additions
and
2 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,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{}, | ||
} |
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,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) | ||
}) | ||
} |
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,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) | ||
} |
Oops, something went wrong.