-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathmigration.go
75 lines (63 loc) · 2.75 KB
/
migration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
consumertypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types"
)
// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
ccvConsumerKeeper Keeper
ccvConsumerParamSpace paramtypes.Subspace
}
// NewMigrator returns a new Migrator.
func NewMigrator(ccvConsumerKeeper Keeper, ccvConsumerParamSpace paramtypes.Subspace) Migrator {
return Migrator{ccvConsumerKeeper: ccvConsumerKeeper, ccvConsumerParamSpace: ccvConsumerParamSpace}
}
// Migrate1to2 migrates x/ccvconsumer state from consensus version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return m.ccvConsumerKeeper.MigrateConsumerPacketData(ctx)
}
// MigrateConsumerPacketData migrates consumer packet data according to
// https://github.com/cosmos/interchain-security/pull/1037
//
// Note an equivalent migration is not required for providers.
func (k Keeper) MigrateConsumerPacketData(ctx sdk.Context) error {
// deserialize packet data from old format
var depreciatedType ccvtypes.ConsumerPacketDataList
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte{consumertypes.PendingDataPacketsBytePrefix})
if bz == nil {
ctx.Logger().Info("no pending data packets to migrate")
return nil
}
err := depreciatedType.Unmarshal(bz)
if err != nil {
return fmt.Errorf("failed to unmarshal pending data packets: %w", err)
}
// Delete old data
store.Delete([]byte{consumertypes.PendingDataPacketsBytePrefix})
// re-serialize packet data in new format, with the same key prefix,
// where indexes are added internally to AppendPendingPacket.
for _, data := range depreciatedType.List {
k.AppendPendingPacket(ctx, data.Type, data.Data)
}
return nil
}
// TODO: the following hackyness could be removed if we're able to reference older versions of ICS.
// This would likely require go.mod split, and a testing module that could depend on multiple ICS versions.
func PendingDataPacketsKeyOnlyForTesting() []byte {
return []byte{consumertypes.PendingDataPacketsBytePrefix} // Assumes keys haven't been shuffled
}
// Note: a better test of the old functionality would be to directly reference the old ICS version,
// including the version of ccv.ConsumerPacketDataList has a list of ccv.ConsumerPacketData without indexes.
func (k Keeper) SetPendingPacketsOnlyForTesting(ctx sdk.Context, packets ccvtypes.ConsumerPacketDataList) {
store := ctx.KVStore(k.storeKey)
bz, err := packets.Marshal()
if err != nil {
// This should never happen
panic(fmt.Errorf("failed to marshal ConsumerPacketDataList: %w", err))
}
store.Set(PendingDataPacketsKeyOnlyForTesting(), bz)
}