Skip to content

Commit ecfbbc3

Browse files
committed
optimize migrations
1 parent 2936120 commit ecfbbc3

3 files changed

Lines changed: 47 additions & 31 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: 44 additions & 28 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
}
@@ -37,34 +37,50 @@ func migrateDLCEvents(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.B
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+
// 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
52+
// }
53+
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+
// }
68+
69+
tssKeeper.IterateDKGRequests(ctx, func(req *tsstypes.DKGRequest) (stop bool) {
70+
if req.Status == tsstypes.DKGStatus_DKG_STATUS_COMPLETED {
71+
// dcm or oracle pub key
72+
pubKey := tssKeeper.GetDKGPubKeys(ctx, req.Id)[0]
73+
pubKeyBz, _ := hex.DecodeString(pubKey)
74+
75+
if req.Type == types.DKG_TYPE_DCM {
76+
updateDCM(ctx, storeKey, req.Id, pubKeyBz, cdc)
77+
} else if req.Type == types.DKG_TYPE_NONCE {
78+
updateOracle(ctx, storeKey, req.Id, pubKeyBz, cdc)
79+
}
5280
}
5381

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-
}
82+
return false
83+
})
6884
}
6985

7086
// 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

0 commit comments

Comments
 (0)