-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathupgrades.go
More file actions
235 lines (210 loc) · 7.24 KB
/
upgrades.go
File metadata and controls
235 lines (210 loc) · 7.24 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package app
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
circuittypes "cosmossdk.io/x/circuit/types"
"cosmossdk.io/x/nft"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/CosmWasm/wasmd/app/upgrades"
"github.com/CosmWasm/wasmd/app/upgrades/noop"
v060 "github.com/CosmWasm/wasmd/app/upgrades/v060"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
consensusparamtypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
epochstypes "github.com/cosmos/cosmos-sdk/x/epochs/types"
"github.com/cosmos/cosmos-sdk/x/group"
//minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
protocolpooltypes "github.com/cosmos/cosmos-sdk/x/protocolpool/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
liquidtypes "github.com/cosmos/gaia/v25/x/liquid/types"
icacontrollertypes "github.com/cosmos/ibc-go/v10/modules/apps/27-interchain-accounts/controller/types"
icatypes "github.com/cosmos/ibc-go/v10/modules/apps/27-interchain-accounts/types"
ibctransfertypes "github.com/cosmos/ibc-go/v10/modules/apps/transfer/types"
ibcexported "github.com/cosmos/ibc-go/v10/modules/core/exported"
"github.com/fetchai/fetchd/app/ica_migration"
"github.com/fetchai/fetchd/app/traces"
)
// ---- Match this to the plan name that is already stored on disk and halted the chain.
const UpgradeNameV053 = "v0.15.0-rc1-gemini"
// List ALL new/renamed/deleted KV stores at this upgrade height.
var v053StoreUpgrades = storetypes.StoreUpgrades{
Added: []string{
protocolpooltypes.StoreKey,
circuittypes.StoreKey,
epochstypes.StoreKey,
consensusparamtypes.StoreKey,
group.StoreKey,
icacontrollertypes.StoreKey,
nft.StoreKey,
liquidtypes.StoreKey,
},
Renamed: []storetypes.StoreRename{
// {OldKey: "oldkey", NewKey: "newkey"},
},
Deleted: []string{
// Only if truly removed and fully migrated:
// "params",
},
}
// Keep your existing wasmd upgrades
var Upgrades = []upgrades.Upgrade{v060.Upgrade}
func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
if len(Upgrades) == 0 {
Upgrades = append(Upgrades, noop.NewUpgrade(app.Version()))
}
keepers := upgrades.AppKeepers{
AccountKeeper: &app.AccountKeeper,
ConsensusParamsKeeper: &app.ConsensusParamsKeeper,
IBCKeeper: app.IBCKeeper,
Codec: app.appCodec,
GetStoreKey: app.GetKey,
}
app.GetStoreKeys()
// 1) Existing wasmd handlers
for _, upgrade := range Upgrades {
app.UpgradeKeeper.SetUpgradeHandler(
upgrade.UpgradeName,
upgrade.CreateUpgradeHandler(app.mm, cfg, &keepers),
)
}
// 2) The v0.53 handler that runs per-module Migrators
app.UpgradeKeeper.SetUpgradeHandler(
UpgradeNameV053,
func(ctx context.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
// Migrate ICA controller
ICAmigrator := ica_migration.NewICAMigrator(sdkCtx, sdkCtx.KVStore(app.GetKey(icacontrollertypes.StoreKey)), sdkCtx.KVStore(app.GetKey(ibcexported.StoreKey)), sdkCtx.KVStore(app.GetMemKey(CapabilityMemStoreKey)), app.GetKey(CapabilityStoreKey))
err := cfg.RegisterMigration(icatypes.ModuleName, 1, ICAmigrator.AssertChannelCapabilityMigrations)
if err != nil {
return nil, err
}
// Migrate transfer traces
m := traces.NewDenomTracesMigrator(sdkCtx.KVStore(app.GetKey(ibctransfertypes.StoreKey)))
err = cfg.RegisterMigration(ibctransfertypes.ModuleName, 1, m.MigrateTraces)
if err != nil {
return nil, err
}
/*
// Pre-seed legacy x/params for mint
if ss, ok := app.ParamsKeeper.GetSubspace(minttypes.ModuleName); ok {
if !ss.Has(sdkCtx, minttypes.KeyInflationRateChange) {
p := minttypes.DefaultParams()
p.MintDenom = "afet"
// TODO: if your chain had custom values, set them here:
ss.SetParamSet(sdkCtx, &p)
}
}
*/
err = migrateConsensusParamsFromParamsStore(app, sdkCtx)
if err != nil {
return nil, err
}
// Bootstrap liquid staking
err = app.StakingKeeper.IterateValidators(ctx, func(_ int64, v stakingtypes.ValidatorI) (stop bool) {
lv := liquidtypes.LiquidValidator{
OperatorAddress: v.GetOperator(),
LiquidShares: math.LegacyZeroDec(),
}
err := app.LiquidKeeper.SetLiquidValidator(ctx, lv)
if err != nil {
return false
}
return false
})
if err != nil {
return nil, err
}
// If you must pin any module "from" versions, adjust fromVM here.
return app.mm.RunMigrations(ctx, cfg, fromVM)
},
)
// 3) Load the correct store shape at the upgrade height
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Sprintf("failed to read upgrade info from disk %s", err))
}
if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
return
}
// Try wasmd-defined upgrades first
for _, u := range Upgrades {
if upgradeInfo.Name == u.UpgradeName {
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &u.StoreUpgrades))
return
}
}
// Then our v0.53 plan
if upgradeInfo.Name == UpgradeNameV053 {
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &v053StoreUpgrades))
}
}
func migrateConsensusParamsFromParamsStore(app *App, ctx sdk.Context) error {
paramsStore := ctx.KVStore(app.GetKey(paramstypes.StoreKey))
// BlockParams
var blockParams tmproto.BlockParams
if bz := paramsStore.Get([]byte("baseapp/BlockParams")); len(bz) > 0 {
// these are plain numbers; codec JSON is fine
if err := app.AppCodec().UnmarshalJSON(bz, &blockParams); err != nil {
return err
}
}
// --- EvidenceParams (strings; duration in ns) ---
var epRaw struct {
MaxAgeNumBlocks string `json:"max_age_num_blocks"`
MaxAgeDuration string `json:"max_age_duration"` // ns as string
MaxBytes string `json:"max_bytes"`
}
if err := json.Unmarshal(paramsStore.Get([]byte("baseapp/EvidenceParams")), &epRaw); err != nil {
return err
}
blocks, err := parseI64(epRaw.MaxAgeNumBlocks)
if err != nil {
return err
}
durNs, err := parseI64(epRaw.MaxAgeDuration)
if err != nil {
return err
}
evMaxBytes, err := parseI64(epRaw.MaxBytes)
if err != nil {
return err
}
evidenceParams := tmproto.EvidenceParams{
MaxAgeNumBlocks: blocks,
MaxAgeDuration: time.Duration(durNs), // ns → time.Duration
MaxBytes: evMaxBytes,
}
// ValidatorParams
var validatorParams tmproto.ValidatorParams
if bz := paramsStore.Get([]byte("baseapp/ValidatorParams")); len(bz) > 0 {
if err := app.AppCodec().UnmarshalJSON(bz, &validatorParams); err != nil {
return err
}
}
cp := tmproto.ConsensusParams{
Block: &blockParams,
Evidence: &evidenceParams,
Validator: &validatorParams,
Version: &tmproto.VersionParams{App: 0},
}
// Write to x/consensus so it’s persisted in app state
if err := app.ConsensusParamsKeeper.ParamsStore.Set(ctx, cp); err != nil {
return err
}
// Also write to BaseApp so CometBFT uses it immediately
if err := app.BaseApp.StoreConsensusParams(ctx, cp); err != nil {
return err
}
return nil
}
func parseI64(s string) (int64, error) {
return strconv.ParseInt(strings.TrimSpace(s), 10, 64)
}