|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + |
| 6 | + storetypes "cosmossdk.io/store/types" |
| 7 | + "github.com/cosmos/cosmos-sdk/codec" |
| 8 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 9 | + |
| 10 | + "github.com/sideprotocol/side/x/dlc/types" |
| 11 | + tsstypes "github.com/sideprotocol/side/x/tss/types" |
| 12 | +) |
| 13 | + |
| 14 | +// MigrateStore migrates the x/dlc module state from the consensus version 1 to |
| 15 | +// version 2 |
| 16 | +func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, tssStoreKey storetypes.StoreKey, cdc codec.BinaryCodec) error { |
| 17 | + migrateDLCEvents(ctx, storeKey, cdc) |
| 18 | + migrateDCMsAndOracles(ctx, storeKey, tssStoreKey, cdc) |
| 19 | + |
| 20 | + return nil |
| 21 | +} |
| 22 | + |
| 23 | +// migrateDLCEvents performs the dlc events migration |
| 24 | +func migrateDLCEvents(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) { |
| 25 | + store := ctx.KVStore(storeKey) |
| 26 | + |
| 27 | + iterator := storetypes.KVStorePrefixIterator(store, types.EventKeyPrefix) |
| 28 | + defer iterator.Close() |
| 29 | + |
| 30 | + for ; iterator.Valid(); iterator.Next() { |
| 31 | + var dlcEvent types.DLCEvent |
| 32 | + cdc.MustUnmarshal(iterator.Value(), &dlcEvent) |
| 33 | + |
| 34 | + // set the DKG request by status |
| 35 | + store.Set(types.EventByStatusKey(dlcEvent.HasTriggered, dlcEvent.Id), []byte{}) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// 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 |
| 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 | + |
| 70 | +// updateDCM updates the given dcm |
| 71 | +func updateDCM(ctx sdk.Context, storeKey storetypes.StoreKey, dkgId uint64, pubKey []byte, cdc codec.BinaryCodec) { |
| 72 | + store := ctx.KVStore(storeKey) |
| 73 | + |
| 74 | + bz := store.Get(types.DCMByPubKeyKey(pubKey)) |
| 75 | + id := sdk.BigEndianToUint64(bz) |
| 76 | + |
| 77 | + // unmarshal to DCM v1 |
| 78 | + dcmBz := store.Get(types.DCMKey(id)) |
| 79 | + var dcmV1 types.DCMV1 |
| 80 | + cdc.MustUnmarshal(dcmBz, &dcmV1) |
| 81 | + |
| 82 | + // build new DCM |
| 83 | + dcm := &types.DCM{ |
| 84 | + Id: id, |
| 85 | + DkgId: dkgId, |
| 86 | + Desc: dcmV1.Desc, |
| 87 | + Pubkey: dcmV1.Pubkey, |
| 88 | + Time: dcmV1.Time, |
| 89 | + Status: dcmV1.Status, |
| 90 | + } |
| 91 | + |
| 92 | + // update DCM |
| 93 | + store.Set(types.DCMKey(id), cdc.MustMarshal(dcm)) |
| 94 | +} |
| 95 | + |
| 96 | +// updateOracle updates the given oracle |
| 97 | +func updateOracle(ctx sdk.Context, storeKey storetypes.StoreKey, dkgId uint64, pubKey []byte, cdc codec.BinaryCodec) { |
| 98 | + store := ctx.KVStore(storeKey) |
| 99 | + |
| 100 | + bz := store.Get(types.OracleByPubKeyKey(pubKey)) |
| 101 | + id := sdk.BigEndianToUint64(bz) |
| 102 | + |
| 103 | + // unmarshal to oracle v1 |
| 104 | + oracleBz := store.Get(types.OracleKey(id)) |
| 105 | + var oracleV1 types.DLCOracleV1 |
| 106 | + cdc.MustUnmarshal(oracleBz, &oracleV1) |
| 107 | + |
| 108 | + // build new oracle |
| 109 | + oracle := &types.DLCOracle{ |
| 110 | + Id: id, |
| 111 | + DkgId: dkgId, |
| 112 | + Desc: oracleV1.Desc, |
| 113 | + Pubkey: oracleV1.Pubkey, |
| 114 | + NonceIndex: oracleV1.NonceIndex, |
| 115 | + Time: oracleV1.Time, |
| 116 | + Status: oracleV1.Status, |
| 117 | + } |
| 118 | + |
| 119 | + // update oracle |
| 120 | + store.Set(types.OracleKey(id), cdc.MustMarshal(oracle)) |
| 121 | +} |
0 commit comments