/
upgrade.go
220 lines (179 loc) · 6.21 KB
/
upgrade.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
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
// Package v0_24_0
// nolint revive
package v0_24_0
import (
"fmt"
"time"
"github.com/tendermint/tendermint/libs/log"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/feegrant"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
"github.com/akash-network/akash-api/go/node/escrow/v1beta3"
astakingtypes "github.com/akash-network/akash-api/go/node/staking/v1beta3"
apptypes "github.com/akash-network/node/app/types"
utypes "github.com/akash-network/node/upgrades/types"
agov "github.com/akash-network/node/x/gov"
astaking "github.com/akash-network/node/x/staking"
atake "github.com/akash-network/node/x/take"
)
const (
upgradeName = "v0.24.0"
)
type upgrade struct {
*apptypes.App
log log.Logger
}
var _ utypes.IUpgrade = (*upgrade)(nil)
func initUpgrade(log log.Logger, app *apptypes.App) (utypes.IUpgrade, error) {
up := &upgrade{
App: app,
log: log.With(fmt.Sprintf("upgrade/%s", upgradeName)),
}
if _, exists := up.MM.Modules[agov.ModuleName]; !exists {
return nil, fmt.Errorf(
"module %s has not been initialized",
agov.ModuleName,
) // nolint: goerr113
}
if _, exists := up.MM.Modules[astaking.ModuleName]; !exists {
return nil, fmt.Errorf(
"module %s has not been initialized",
astaking.ModuleName,
) // nolint: goerr113
}
if _, exists := up.MM.Modules[atake.ModuleName]; !exists {
return nil, fmt.Errorf(
"module %s has not been initialized",
atake.ModuleName,
) // nolint: goerr113
}
if _, exists := up.MM.Modules[feegrant.ModuleName]; !exists {
return nil, fmt.Errorf(
"module %s has not been initialized",
feegrant.ModuleName,
) // nolint: goerr113
}
return up, nil
}
func (up *upgrade) StoreLoader() *storetypes.StoreUpgrades {
upgrades := &storetypes.StoreUpgrades{
Added: []string{
feegrant.StoreKey,
agov.StoreKey,
astaking.StoreKey,
atake.StoreKey,
},
}
return upgrades
}
func (up *upgrade) UpgradeHandler() upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// initializing akash staking module here so enforceMinValidatorCommission below can use params
ctx.Logger().Info("initializing parameters in astaking module...")
if err := up.Keepers.Akash.Staking.SetParams(ctx, astakingtypes.DefaultParams()); err != nil {
return nil, err
}
if err := up.enforceMinValidatorCommission(ctx); err != nil {
return nil, err
}
up.patchDanglingEscrowPayments(ctx)
ctx.Logger().Info("starting module migrations...")
// migrate to new deployment params schema
up.Keepers.Akash.Deployment.SetParams(ctx, dtypes.DefaultParams())
return up.MM.RunMigrations(ctx, up.Configurator, fromVM)
}
}
func (up *upgrade) enforceMinValidatorCommission(ctx sdk.Context) error {
minRate := up.Keepers.Akash.Staking.MinCommissionRate(ctx)
validators := up.Keepers.Cosmos.Staking.GetAllValidators(ctx)
for _, validator := range validators {
// update MaxRate if it is less than minimum required rate
if validator.Commission.MaxRate.LT(minRate) {
validator.Commission.MaxRate = minRate
}
if validator.GetCommission().LT(minRate) {
up.log.Info(
fmt.Sprintf(
"validator's `%s` current commission is %s%% < %[3]s%%(min required). Force updating to %[3]s%%",
validator.OperatorAddress,
validator.Commission.Rate,
minRate,
),
)
// set max change rate temporarily to 100%
maxRateCh := validator.Commission.MaxChangeRate
validator.Commission.MaxChangeRate = sdk.NewDecWithPrec(1, 0)
if _, err := updateValidatorCommission(ctx, validator, minRate); err != nil {
return err
}
validator.Commission.MaxChangeRate = maxRateCh
up.Keepers.Cosmos.Staking.BeforeValidatorModified(ctx, validator.GetOperator())
up.Keepers.Cosmos.Staking.SetValidator(ctx, validator)
}
}
return nil
}
// updateValidatorCommission use custom implementation of update commission,
// this prevents panic during upgrade if any of validators have changed their
// commission within 24h of upgrade height
func updateValidatorCommission(
ctx sdk.Context,
validator stakingtypes.Validator,
newRate sdk.Dec,
) (stakingtypes.Commission, error) {
commission := validator.Commission
blockTime := ctx.BlockHeader().Time
if err := validateNewRate(commission, newRate, blockTime); err != nil {
return commission, err
}
commission.Rate = newRate
commission.UpdateTime = blockTime
return commission, nil
}
// validateNewRate performs basic sanity validation checks of a new commission
// rate. If validation fails, an SDK error is returned.
func validateNewRate(commission stakingtypes.Commission, newRate sdk.Dec, _ time.Time) error {
switch {
case newRate.IsNegative():
// new rate cannot be negative
return stakingtypes.ErrCommissionNegative
case newRate.GT(commission.MaxRate):
// new rate cannot be greater than the max rate
return stakingtypes.ErrCommissionGTMaxRate
case newRate.Sub(commission.Rate).GT(commission.MaxChangeRate):
// new rate % points change cannot be greater than the max change rate
return stakingtypes.ErrCommissionGTMaxChangeRate
}
return nil
}
func (up *upgrade) patchDanglingEscrowPayments(ctx sdk.Context) {
up.Keepers.Akash.Escrow.WithPayments(ctx, func(payment v1beta3.FractionalPayment) bool {
acc, _ := up.Keepers.Akash.Escrow.GetAccount(ctx, payment.AccountID)
if (payment.State == v1beta3.PaymentOpen && acc.State != v1beta3.AccountOpen) ||
(payment.State == v1beta3.PaymentOverdrawn && acc.State != v1beta3.AccountOverdrawn) {
up.log.Info(
fmt.Sprintf(
"payment id state `%s:%s` does not match account state `%s:%s`. forcing payment state to %[4]s",
payment.PaymentID,
payment.State,
acc.ID,
acc.State,
),
)
switch acc.State {
case v1beta3.AccountOpen:
payment.State = v1beta3.PaymentOpen
case v1beta3.AccountClosed:
payment.State = v1beta3.PaymentClosed
case v1beta3.AccountOverdrawn:
payment.State = v1beta3.PaymentOverdrawn
}
}
up.Keepers.Akash.Escrow.SavePayment(ctx, payment)
return true
})
}