-
Notifications
You must be signed in to change notification settings - Fork 7
/
upgrades.go
178 lines (158 loc) · 7.29 KB
/
upgrades.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
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
package v2
import (
errorsmod "cosmossdk.io/errors"
"fmt"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ica "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts"
genesistypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/genesis/types"
icahosttypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
"github.com/planq-network/planq/v2/app/keepers"
evmkeeper "github.com/planq-network/planq/v2/x/evm/keeper"
evmtypes "github.com/planq-network/planq/v2/x/evm/types"
feemarkettypes "github.com/planq-network/planq/v2/x/feemarket/types"
)
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
keepers *keepers.AppKeepers,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
logger := ctx.Logger().With("upgrade", UpgradeName)
logger.Info("migrate ibc escrow accounts")
MigrateEscrowAccounts(ctx, logger, keepers.AccountKeeper)
logger.Info("create ICA module")
// create ICS27 Controller submodule params, with the controller module NOT enabled
gs := &genesistypes.GenesisState{
ControllerGenesisState: genesistypes.ControllerGenesisState{},
HostGenesisState: genesistypes.HostGenesisState{
Port: icatypes.HostPortID,
Params: icahosttypes.Params{
HostEnabled: true,
AllowMessages: []string{
sdk.MsgTypeURL(&banktypes.MsgSend{}),
sdk.MsgTypeURL(&banktypes.MsgMultiSend{}),
sdk.MsgTypeURL(&distrtypes.MsgSetWithdrawAddress{}),
sdk.MsgTypeURL(&distrtypes.MsgWithdrawDelegatorReward{}),
sdk.MsgTypeURL(&govtypesv1beta1.MsgVote{}),
sdk.MsgTypeURL(&govtypesv1beta1.MsgVoteWeighted{}),
sdk.MsgTypeURL(&stakingtypes.MsgDelegate{}),
sdk.MsgTypeURL(&stakingtypes.MsgUndelegate{}),
sdk.MsgTypeURL(&stakingtypes.MsgCancelUnbondingDelegation{}),
sdk.MsgTypeURL(&stakingtypes.MsgBeginRedelegate{}),
sdk.MsgTypeURL(&ibctransfertypes.MsgTransfer{}),
},
},
},
}
bz, err := icatypes.ModuleCdc.MarshalJSON(gs)
if err != nil {
return nil, errorsmod.Wrapf(err, "failed to marshal %s genesis state", icatypes.ModuleName)
}
// Register the consensus version in the version map to avoid the SDK from triggering the default
// InitGenesis function.
vm[icatypes.ModuleName] = ica.AppModule{}.ConsensusVersion()
m := mm.Modules[icatypes.ModuleName].(module.HasGenesis)
m.InitGenesis(ctx, icatypes.ModuleCdc, bz)
logger.Info("migrating legacy params")
// https://github.com/cosmos/cosmos-sdk/blob/v0.47.10/simapp/upgrades.go#L32
for _, subspace := range keepers.ParamsKeeper.GetSubspaces() {
var keyTable paramstypes.KeyTable
switch subspace.Name() {
case authtypes.ModuleName:
keyTable = authtypes.ParamKeyTable() //nolint:staticcheck
case banktypes.ModuleName:
keyTable = banktypes.ParamKeyTable() //nolint:staticcheck,nolintlint
case stakingtypes.ModuleName:
keyTable = stakingtypes.ParamKeyTable()
case minttypes.ModuleName:
keyTable = minttypes.ParamKeyTable() //nolint:staticcheck
case distrtypes.ModuleName:
keyTable = distrtypes.ParamKeyTable() //nolint:staticcheck,nolintlint
case slashingtypes.ModuleName:
keyTable = slashingtypes.ParamKeyTable() //nolint:staticcheck
case govtypes.ModuleName:
keyTable = govv1.ParamKeyTable() //nolint:staticcheck
case crisistypes.ModuleName:
keyTable = crisistypes.ParamKeyTable() //nolint:staticcheck
case ibctransfertypes.ModuleName:
keyTable = ibctransfertypes.ParamKeyTable()
case evmtypes.ModuleName:
keyTable = evmtypes.ParamKeyTable() //nolint:staticcheck
case feemarkettypes.ModuleName:
keyTable = feemarkettypes.ParamKeyTable()
default:
continue
}
if !subspace.HasKeyTable() {
subspace.WithKeyTable(keyTable)
}
}
baseAppLegacySS := keepers.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())
baseapp.MigrateParams(ctx, baseAppLegacySS, &keepers.ConsensusParamsKeeper)
logger.Info("adding EIP 3855 to EVM parameters")
evmErr := EnableEIPs(ctx, keepers.EvmKeeper, 3855)
if evmErr != nil {
logger.Error("error while enabling EIPs", "error", err)
}
// Leave modules are as-is to avoid running InitGenesis.
logger.Debug("running module migrations ...")
return mm.RunMigrations(ctx, configurator, vm)
}
}
// MigrateEscrowAccounts updates the IBC transfer escrow accounts type to ModuleAccount
func MigrateEscrowAccounts(ctx sdk.Context, logger log.Logger, ak authkeeper.AccountKeeper) {
for i := 0; i <= OpenChannels; i++ {
channelID := fmt.Sprintf("channel-%d", i)
address := ibctransfertypes.GetEscrowAddress(ibctransfertypes.PortID, channelID)
// check if account exists
existingAcc := ak.GetAccount(ctx, address)
// account does NOT exist, so don't create it
if existingAcc == nil {
continue
}
// if existing account is ModuleAccount, no-op
if _, isModuleAccount := existingAcc.(authtypes.ModuleAccountI); isModuleAccount {
continue
}
// account name based on the address derived by the transfertypes.GetEscrowAddress
// this function appends the current IBC transfer module version to the provided port and channel IDs
// To pass account validation, need to have address derived from account name
accountName := fmt.Sprintf("%s\x00%s/%s", ibctransfertypes.Version, ibctransfertypes.PortID, channelID)
baseAcc := authtypes.NewBaseAccountWithAddress(address)
// Set same account number and sequence as the existing account
if err := baseAcc.SetAccountNumber(existingAcc.GetAccountNumber()); err != nil {
// log error instead of aborting the upgrade
logger.Error("failed to set escrow account number for account", accountName, "error", err.Error())
}
if err := baseAcc.SetSequence(existingAcc.GetSequence()); err != nil {
// log error instead of aborting the upgrade
logger.Error("failed to set escrow account sequence for account", accountName, "error", err.Error())
}
// no special permissions defined for the module account
acc := authtypes.NewModuleAccount(baseAcc, accountName)
ak.SetModuleAccount(ctx, acc)
}
}
func EnableEIPs(ctx sdk.Context, ek *evmkeeper.Keeper, eips ...int64) error {
evmParams := ek.GetParams(ctx)
evmParams.ExtraEIPs = append(evmParams.ExtraEIPs, eips...)
return ek.SetParams(ctx, evmParams)
}