Skip to content

Commit

Permalink
feat: add ecocredit basket migration (#1842)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanchristo authored Mar 23, 2023
1 parent 2caf7db commit a1eddf1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
37 changes: 28 additions & 9 deletions x/ecocredit/migrations/v4/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ package v4

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

basketv1 "github.com/regen-network/regen-ledger/api/v2/regen/ecocredit/basket/v1"
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

func MigrateState(sdkCtx sdk.Context, baseStore ecocreditv1.StateStore, basketStore basketv1.StateStore) error {
if sdkCtx.ChainID() == "regen-1" {
batches = getMainnetBatches()
}

for _, batch := range batches {
if err := migrateBatchMetadata(sdkCtx, baseStore, batch); err != nil {
// mainnet batch metadata migration
batchUpdates := getBatchUpdates()
for _, batchUpdate := range batchUpdates {
if err := migrateBatchMetadata(sdkCtx, baseStore, batchUpdate); err != nil {
return err
}
}

// mainnet basket criteria migration
if err := migrateBasketCriteria(sdkCtx, basketStore); err != nil {
return err
}
}
Expand All @@ -28,7 +32,7 @@ type Batch struct {
NewMetadata string
}

func getMainnetBatches() []Batch {
func getBatchUpdates() []Batch {
return []Batch{
// http://mainnet.regen.network:1317/regen/ecocredit/v1/batches/C01-001-20150101-20151231-001
{
Expand Down Expand Up @@ -128,3 +132,18 @@ func migrateBatchMetadata(ctx sdk.Context, baseStore ecocreditv1.StateStore, bat

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

func migrateBasketCriteria(ctx sdk.Context, basketStore basketv1.StateStore) error {
b, err := basketStore.BasketTable().GetByBasketDenom(ctx, "eco.uC.NCT")
if err != nil {
return err
}

b.DisableAutoRetire = true

b.DateCriteria = &basketv1.DateCriteria{
YearsInThePast: 10,
}

return basketStore.BasketTable().Update(ctx, b)
}
43 changes: 38 additions & 5 deletions x/ecocredit/migrations/v4/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/require"
dbm "github.com/tendermint/tm-db"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/tendermint/tendermint/libs/log"
Expand All @@ -17,13 +18,14 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

basketapi "github.com/regen-network/regen-ledger/api/v2/regen/ecocredit/basket/v1"
baseapi "github.com/regen-network/regen-ledger/api/v2/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/x/ecocredit/v3"
v4 "github.com/regen-network/regen-ledger/x/ecocredit/v3/migrations/v4"
)

func TestMainnetMigrateBatchMetadata(t *testing.T) {
sdkCtx, baseStore := setup(t)
func TestMainnetMigrations(t *testing.T) {
sdkCtx, baseStore, basketStore := setup(t)
sdkCtx = sdkCtx.WithChainID("regen-1")

// issuer is the same for all credit batches
Expand Down Expand Up @@ -230,8 +232,24 @@ func TestMainnetMigrateBatchMetadata(t *testing.T) {
require.NoError(t, baseStore.BatchTable().Insert(sdkCtx, batch))
}

curator := sdk.MustAccAddressFromBech32("regen1mrvlgpmrjn9s7r7ct69euqfgxjazjt2l5lzqcd")

// http://mainnet.regen.network:1317/regen/ecocredit/basket/v1/baskets/eco.uC.NCT
basket := &basketapi.Basket{
BasketDenom: "eco.uC.NCT",
Name: "NCT",
DisableAutoRetire: false,
CreditTypeAbbrev: "C",
DateCriteria: nil,
Exponent: 6,
Curator: curator,
}

// add basket to state
require.NoError(t, basketStore.BasketTable().Insert(sdkCtx, basket))

// execute state migrations
require.NoError(t, v4.MigrateState(sdkCtx, baseStore))
require.NoError(t, v4.MigrateState(sdkCtx, baseStore, basketStore))

b1, err := baseStore.BatchTable().GetByDenom(sdkCtx, "C01-001-20150101-20151231-001")
require.NoError(t, err)
Expand Down Expand Up @@ -313,9 +331,21 @@ func TestMainnetMigrateBatchMetadata(t *testing.T) {
require.Equal(t, b.IssuanceDate.AsTime(), timestamp.AsTime())
require.Equal(t, b.Open, false)
}

b, err := basketStore.BasketTable().GetByBasketDenom(sdkCtx, "eco.uC.NCT")
require.NoError(t, err)

require.Equal(t, "NCT", b.Name)
require.Equal(t, true, b.DisableAutoRetire)
require.Equal(t, "C", b.CreditTypeAbbrev)
require.Equal(t, (*timestamppb.Timestamp)(nil), b.DateCriteria.MinStartDate)
require.Equal(t, (*durationpb.Duration)(nil), b.DateCriteria.StartDateWindow)
require.Equal(t, uint32(10), b.DateCriteria.YearsInThePast)
require.Equal(t, uint32(6), b.Exponent) //nolint:staticcheck
require.Equal(t, curator.Bytes(), b.Curator)
}

func setup(t *testing.T) (sdk.Context, baseapi.StateStore) {
func setup(t *testing.T) (sdk.Context, baseapi.StateStore, basketapi.StateStore) {
ecocreditKey := sdk.NewKVStoreKey("ecocredit")

db := dbm.NewMemDB()
Expand All @@ -333,5 +363,8 @@ func setup(t *testing.T) (sdk.Context, baseapi.StateStore) {
baseStore, err := baseapi.NewStateStore(modDB)
require.NoError(t, err)

return sdkCtx, baseStore
basketStore, err := basketapi.NewStateStore(modDB)
require.NoError(t, err)

return sdkCtx, baseStore, basketStore
}
4 changes: 2 additions & 2 deletions x/ecocredit/server/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ func (m Migrator) Migrate2to3(ctx sdk.Context) error {

// Migrate3to4 migrates from version 3 to 4.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
baseStore, _, _ := m.keeper.GetStateStores()
return v4.MigrateState(ctx, baseStore)
baseStore, basketStore, _ := m.keeper.GetStateStores()
return v4.MigrateState(ctx, baseStore, basketStore)
}

0 comments on commit a1eddf1

Please sign in to comment.