Skip to content

Commit 9e349d6

Browse files
committed
optimize migrations
1 parent 2936120 commit 9e349d6

5 files changed

Lines changed: 25 additions & 43 deletions

File tree

x/dlc/keeper/migrations.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package keeper
22

33
import (
4-
storetypes "cosmossdk.io/store/types"
54
sdk "github.com/cosmos/cosmos-sdk/types"
65

76
v2 "github.com/sideprotocol/side/x/dlc/migrations/v2"
8-
tsstypes "github.com/sideprotocol/side/x/tss/types"
97
)
108

119
// Migrator is a struct for handling in-place store migrations
@@ -20,5 +18,5 @@ func NewMigrator(keeper Keeper) Migrator {
2018

2119
// Migrate1to2 migrates from version 1 to 2
2220
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
23-
return v2.MigrateStore(ctx, m.keeper.storeKey, storetypes.NewKVStoreKey(tsstypes.StoreKey), m.keeper.cdc)
21+
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.tssKeeper, m.keeper.cdc)
2422
}

x/dlc/migrations/v2/store.go

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313

1414
// MigrateStore migrates the x/dlc module state from the consensus version 1 to
1515
// version 2
16-
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, tssStoreKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
16+
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, tssKeeper types.TSSKeeper, cdc codec.BinaryCodec) error {
1717
migrateDLCEvents(ctx, storeKey, cdc)
18-
migrateDCMsAndOracles(ctx, storeKey, tssStoreKey, cdc)
18+
migrateDCMsAndOracles(ctx, storeKey, tssKeeper, cdc)
1919

2020
return nil
2121
}
@@ -31,40 +31,28 @@ func migrateDLCEvents(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.B
3131
var dlcEvent types.DLCEvent
3232
cdc.MustUnmarshal(iterator.Value(), &dlcEvent)
3333

34-
// set the DKG request by status
34+
// set event by status
3535
store.Set(types.EventByStatusKey(dlcEvent.HasTriggered, dlcEvent.Id), []byte{})
3636
}
3737
}
3838

3939
// migrateDCMsAndOracles performs the DCMs and oracles migration
40-
func migrateDCMsAndOracles(ctx sdk.Context, storeKey storetypes.StoreKey, tssStoreKey storetypes.StoreKey, cdc codec.BinaryCodec) {
41-
tssStore := ctx.KVStore(tssStoreKey)
42-
43-
iterator := storetypes.KVStorePrefixIterator(tssStore, tsstypes.DKGRequestKeyPrefix)
44-
defer iterator.Close()
45-
46-
for ; iterator.Valid(); iterator.Next() {
47-
var dkgRequest tsstypes.DKGRequest
48-
cdc.MustUnmarshal(iterator.Value(), &dkgRequest)
49-
50-
if dkgRequest.Status != tsstypes.DKGStatus_DKG_STATUS_COMPLETED {
51-
continue
40+
func migrateDCMsAndOracles(ctx sdk.Context, storeKey storetypes.StoreKey, tssKeeper types.TSSKeeper, cdc codec.BinaryCodec) {
41+
tssKeeper.IterateDKGRequests(ctx, func(req *tsstypes.DKGRequest) (stop bool) {
42+
if req.Status == tsstypes.DKGStatus_DKG_STATUS_COMPLETED {
43+
// dcm or oracle pub key
44+
pubKey := tssKeeper.GetDKGPubKeys(ctx, req.Id)[0]
45+
pubKeyBz, _ := hex.DecodeString(pubKey)
46+
47+
if req.Type == types.DKG_TYPE_DCM {
48+
updateDCM(ctx, storeKey, req.Id, pubKeyBz, cdc)
49+
} else if req.Type == types.DKG_TYPE_NONCE {
50+
updateOracle(ctx, storeKey, req.Id, pubKeyBz, cdc)
51+
}
5252
}
5353

54-
bz := tssStore.Get(tsstypes.DKGCompletionKey(dkgRequest.Id, dkgRequest.Participants[0]))
55-
var dkgCompletion tsstypes.DKGCompletion
56-
cdc.MustUnmarshal(bz, &dkgCompletion)
57-
58-
// DCM or oracle pub key
59-
pubKey := dkgCompletion.PubKeys[0]
60-
pubKeyBz, _ := hex.DecodeString(pubKey)
61-
62-
if dkgRequest.Type == types.DKG_TYPE_DCM {
63-
updateDCM(ctx, storeKey, dkgRequest.Id, pubKeyBz, cdc)
64-
} else if dkgRequest.Type == types.DKG_TYPE_NONCE {
65-
updateOracle(ctx, storeKey, dkgRequest.Id, pubKeyBz, cdc)
66-
}
67-
}
54+
return false
55+
})
6856
}
6957

7058
// updateDCM updates the given dcm

x/dlc/types/expected_keepers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type TSSKeeper interface {
2929

3030
HasDKGRequest(ctx sdk.Context, id uint64) bool
3131
GetDKGRequest(ctx sdk.Context, id uint64) *tsstypes.DKGRequest
32+
GetDKGPubKeys(ctx sdk.Context, id uint64) []string
33+
IterateDKGRequests(ctx sdk.Context, cb func(req *tsstypes.DKGRequest) (stop bool))
3234

3335
InitiateDKG(ctx sdk.Context, module string, ty string, intent int32, participants []string, threshold uint32, batchSize uint32) *tsstypes.DKGRequest
3436
InitiateSigningRequest(ctx sdk.Context, module string, scopedId string, ty tsstypes.SigningType, intent int32, pubKey string, sigHashes []string, options *tsstypes.SigningOptions) *tsstypes.SigningRequest

x/lending/keeper/migrations.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package keeper
22

33
import (
4-
storetypes "cosmossdk.io/store/types"
54
sdk "github.com/cosmos/cosmos-sdk/types"
65

7-
dlctypes "github.com/sideprotocol/side/x/dlc/types"
86
v2 "github.com/sideprotocol/side/x/lending/migrations/v2"
97
v3 "github.com/sideprotocol/side/x/lending/migrations/v3"
108
v4 "github.com/sideprotocol/side/x/lending/migrations/v4"
@@ -38,5 +36,5 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error {
3836

3937
// Migrate4to5 migrates from version 4 to 5
4038
func (m Migrator) Migrate4to5(ctx sdk.Context) error {
41-
return v5.MigrateStore(ctx, m.keeper.storeKey, storetypes.NewKVStoreKey(dlctypes.StoreKey), m.keeper.cdc)
39+
return v5.MigrateStore(ctx, m.keeper.storeKey, m.keeper.dlcKeeper, m.keeper.cdc)
4240
}

x/lending/migrations/v5/store.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@ import (
55
"github.com/cosmos/cosmos-sdk/codec"
66
sdk "github.com/cosmos/cosmos-sdk/types"
77

8-
dlctypes "github.com/sideprotocol/side/x/dlc/types"
98
"github.com/sideprotocol/side/x/lending/types"
109
)
1110

1211
// MigrateStore migrates the x/lending module state from the consensus version 4 to
1312
// version 5
14-
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, dlcStoreKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
15-
migrateLoans(ctx, storeKey, dlcStoreKey, cdc)
13+
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, dlcKeeper types.DLCKeeper, cdc codec.BinaryCodec) error {
14+
migrateLoans(ctx, storeKey, dlcKeeper, cdc)
1615

1716
return nil
1817
}
1918

2019
// migrateLoans performs the loans migration
21-
func migrateLoans(ctx sdk.Context, storeKey storetypes.StoreKey, dlcStoreKey storetypes.StoreKey, cdc codec.BinaryCodec) {
20+
func migrateLoans(ctx sdk.Context, storeKey storetypes.StoreKey, dlcKeeper types.DLCKeeper, cdc codec.BinaryCodec) {
2221
store := ctx.KVStore(storeKey)
23-
dlcStore := ctx.KVStore(dlcStoreKey)
2422

2523
iterator := storetypes.KVStorePrefixIterator(store, types.LoanKeyPrefix)
2624
defer iterator.Close()
@@ -29,9 +27,7 @@ func migrateLoans(ctx sdk.Context, storeKey storetypes.StoreKey, dlcStoreKey sto
2927
var loan types.Loan
3028
cdc.MustUnmarshal(iterator.Value(), &loan)
3129

32-
bz := dlcStore.Get(dlctypes.EventKey(loan.DlcEventId))
33-
var dlcEvent dlctypes.DLCEvent
34-
cdc.MustUnmarshal(bz, &dlcEvent)
30+
dlcEvent := dlcKeeper.GetEvent(ctx, loan.DlcEventId)
3531

3632
// set loan by status
3733
store.Set(types.LoanByStatusKey(loan.Status, loan.VaultAddress), []byte{})

0 commit comments

Comments
 (0)