|
| 1 | +package v3 |
| 2 | + |
| 3 | +import ( |
| 4 | + storetypes "cosmossdk.io/store/types" |
| 5 | + "github.com/cosmos/cosmos-sdk/codec" |
| 6 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 7 | + |
| 8 | + "github.com/sideprotocol/side/x/dlc/types" |
| 9 | +) |
| 10 | + |
| 11 | +// MigrateStore migrates the x/dlc module state from the consensus version 1 to |
| 12 | +// version 2 |
| 13 | +func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error { |
| 14 | + migrateParams(ctx, storeKey, cdc) |
| 15 | + migrateStore(ctx, storeKey, cdc) |
| 16 | + |
| 17 | + return nil |
| 18 | +} |
| 19 | + |
| 20 | +// migrateParams performs the params migration |
| 21 | +func migrateParams(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) { |
| 22 | + store := ctx.KVStore(storeKey) |
| 23 | + |
| 24 | + // get current params |
| 25 | + var paramsV1 types.ParamsV1 |
| 26 | + bz := store.Get(types.ParamsKey) |
| 27 | + cdc.MustUnmarshal(bz, ¶msV1) |
| 28 | + |
| 29 | + // build new params |
| 30 | + params := &types.Params{ |
| 31 | + NonceQueueSize: paramsV1.LendingEventNonceQueueSize, |
| 32 | + NonceGenerationBatchSize: paramsV1.NonceGenerationBatchSize, |
| 33 | + NonceGenerationInterval: paramsV1.NonceGenerationInterval, |
| 34 | + AllowedOracleParticipants: paramsV1.AllowedOracleParticipants, |
| 35 | + OracleParticipantNum: paramsV1.OracleParticipantNum, |
| 36 | + OracleParticipantThreshold: paramsV1.OracleParticipantThreshold, |
| 37 | + } |
| 38 | + |
| 39 | + bz = cdc.MustMarshal(params) |
| 40 | + store.Set(types.ParamsKey, bz) |
| 41 | + |
| 42 | + for _, participant := range params.AllowedOracleParticipants { |
| 43 | + liveness := &types.OracleParticipantLiveness{ |
| 44 | + ConsensusPubkey: participant, |
| 45 | + IsAlive: true, |
| 46 | + LastDkgId: 0, |
| 47 | + LastBlockHeight: 0, |
| 48 | + } |
| 49 | + |
| 50 | + bz := cdc.MustMarshal(liveness) |
| 51 | + |
| 52 | + store.Set(types.OracleParticipantLivenessKey(participant), bz) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// migrateStore performs the store migration |
| 57 | +func migrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) { |
| 58 | + store := ctx.KVStore(storeKey) |
| 59 | + |
| 60 | + // get the current dcm id |
| 61 | + dcmIdBz := store.Get(types.DCMIdKey) |
| 62 | + |
| 63 | + // update the current oracle id to the latest dcm id |
| 64 | + store.Set(types.OracleIdKey, dcmIdBz) |
| 65 | + |
| 66 | + // update the current dcm id to the latest dcm id |
| 67 | + iterateDCMs(ctx, storeKey, cdc, func(dcm *types.DCM) (stop bool) { |
| 68 | + store.Set(types.DCMIdKey, sdk.Uint64ToBigEndian(dcm.Id)) |
| 69 | + return false |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +// iterateDCMs iterates through all DCMs |
| 74 | +func iterateDCMs(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, cb func(dcm *types.DCM) (stop bool)) { |
| 75 | + store := ctx.KVStore(storeKey) |
| 76 | + |
| 77 | + iterator := storetypes.KVStorePrefixIterator(store, types.DCMKeyPrefix) |
| 78 | + defer iterator.Close() |
| 79 | + |
| 80 | + for ; iterator.Valid(); iterator.Next() { |
| 81 | + var dcm types.DCM |
| 82 | + cdc.MustUnmarshal(iterator.Value(), &dcm) |
| 83 | + |
| 84 | + if cb(&dcm) { |
| 85 | + break |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments