This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
299 lines (253 loc) · 8.93 KB
/
store.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Package v043 creates in-place store migrations for fixing tracking
// delegations with vesting accounts.
// ref: https://github.com/cosmos/cosmos-sdk/issues/8601
// ref: https://github.com/cosmos/cosmos-sdk/issues/8812
//
// The migration script modifies x/auth state, hence lives in the `x/auth/legacy`
// folder. However, it needs access to staking and bank state. To avoid
// cyclic dependencies, we cannot import those 2 keepers in this file. To solve
// this, we use the baseapp router to do inter-module querying, by importing
// the `baseapp.QueryRouter grpc.Server`. This is really hacky.
//
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
//
// Proposals to refactor this file have been made in:
// https://github.com/cosmos/cosmos-sdk/issues/9070
// The preferred solution is to use inter-module communication (ADR-033), and
// this file will be refactored to use ADR-033 once it's ready.
package v043
import (
"errors"
"fmt"
"github.com/gogo/protobuf/grpc"
"github.com/gogo/protobuf/proto"
abci "github.com/tendermint/tendermint/abci/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/Pylons-tech/cosmos-sdk/baseapp"
sdk "github.com/Pylons-tech/cosmos-sdk/types"
sdkerrors "github.com/Pylons-tech/cosmos-sdk/types/errors"
"github.com/Pylons-tech/cosmos-sdk/x/auth/types"
"github.com/Pylons-tech/cosmos-sdk/x/auth/vesting/exported"
vestingtypes "github.com/Pylons-tech/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/Pylons-tech/cosmos-sdk/x/bank/types"
stakingtypes "github.com/Pylons-tech/cosmos-sdk/x/staking/types"
)
const (
delegatorDelegationPath = "/cosmos.staking.v1beta1.Query/DelegatorDelegations"
stakingParamsPath = "/cosmos.staking.v1beta1.Query/Params"
delegatorUnbondingDelegationsPath = "/cosmos.staking.v1beta1.Query/DelegatorUnbondingDelegations"
balancesPath = "/cosmos.bank.v1beta1.Query/AllBalances"
)
// We use the baseapp.QueryRouter here to do inter-module state querying.
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
func migrateVestingAccounts(ctx sdk.Context, account types.AccountI, queryServer grpc.Server) (types.AccountI, error) {
bondDenom, err := getBondDenom(ctx, queryServer)
if err != nil {
return nil, err
}
asVesting, ok := account.(exported.VestingAccount)
if !ok {
return nil, nil
}
addr := account.GetAddress().String()
balance, err := getBalance(
ctx,
addr,
queryServer,
)
if err != nil {
return nil, err
}
delegations, err := getDelegatorDelegationsSum(
ctx,
addr,
queryServer,
)
if err != nil {
return nil, err
}
unbondingDelegations, err := getDelegatorUnbondingDelegationsSum(
ctx,
addr,
bondDenom,
queryServer,
)
if err != nil {
return nil, err
}
delegations = delegations.Add(unbondingDelegations...)
asVesting, ok = resetVestingDelegatedBalances(asVesting)
if !ok {
return nil, nil
}
// balance before any delegation includes balance of delegation
for _, coin := range delegations {
balance = balance.Add(coin)
}
asVesting.TrackDelegation(ctx.BlockTime(), balance, delegations)
return asVesting.(types.AccountI), nil
}
func resetVestingDelegatedBalances(evacct exported.VestingAccount) (exported.VestingAccount, bool) {
// reset `DelegatedVesting` and `DelegatedFree` to zero
df := sdk.NewCoins()
dv := sdk.NewCoins()
switch vacct := evacct.(type) {
case *vestingtypes.ContinuousVestingAccount:
vacct.DelegatedVesting = dv
vacct.DelegatedFree = df
return vacct, true
case *vestingtypes.DelayedVestingAccount:
vacct.DelegatedVesting = dv
vacct.DelegatedFree = df
return vacct, true
case *vestingtypes.PeriodicVestingAccount:
vacct.DelegatedVesting = dv
vacct.DelegatedFree = df
return vacct, true
default:
return nil, false
}
}
// We use the baseapp.QueryRouter here to do inter-module state querying.
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
func getDelegatorDelegationsSum(ctx sdk.Context, address string, queryServer grpc.Server) (sdk.Coins, error) {
querier, ok := queryServer.(*baseapp.GRPCQueryRouter)
if !ok {
return nil, fmt.Errorf("unexpected type: %T wanted *baseapp.GRPCQueryRouter", queryServer)
}
queryFn := querier.Route(delegatorDelegationPath)
q := &stakingtypes.QueryDelegatorDelegationsRequest{
DelegatorAddr: address,
}
b, err := proto.Marshal(q)
if err != nil {
return nil, fmt.Errorf("cannot marshal staking type query request, %w", err)
}
req := abci.RequestQuery{
Data: b,
Path: delegatorDelegationPath,
}
resp, err := queryFn(ctx, req)
if err != nil {
e, ok := status.FromError(err)
if ok && e.Code() == codes.NotFound {
return nil, nil
}
return nil, fmt.Errorf("staking query error, %w", err)
}
balance := new(stakingtypes.QueryDelegatorDelegationsResponse)
if err := proto.Unmarshal(resp.Value, balance); err != nil {
return nil, fmt.Errorf("unable to unmarshal delegator query delegations: %w", err)
}
res := sdk.NewCoins()
for _, i := range balance.DelegationResponses {
res = res.Add(i.Balance)
}
return res, nil
}
// We use the baseapp.QueryRouter here to do inter-module state querying.
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
func getDelegatorUnbondingDelegationsSum(ctx sdk.Context, address, bondDenom string, queryServer grpc.Server) (sdk.Coins, error) {
querier, ok := queryServer.(*baseapp.GRPCQueryRouter)
if !ok {
return nil, fmt.Errorf("unexpected type: %T wanted *baseapp.GRPCQueryRouter", queryServer)
}
queryFn := querier.Route(delegatorUnbondingDelegationsPath)
q := &stakingtypes.QueryDelegatorUnbondingDelegationsRequest{
DelegatorAddr: address,
}
b, err := proto.Marshal(q)
if err != nil {
return nil, fmt.Errorf("cannot marshal staking type query request, %w", err)
}
req := abci.RequestQuery{
Data: b,
Path: delegatorUnbondingDelegationsPath,
}
resp, err := queryFn(ctx, req)
if err != nil && !errors.Is(err, sdkerrors.ErrNotFound) {
e, ok := status.FromError(err)
if ok && e.Code() == codes.NotFound {
return nil, nil
}
return nil, fmt.Errorf("staking query error, %w", err)
}
balance := new(stakingtypes.QueryDelegatorUnbondingDelegationsResponse)
if err := proto.Unmarshal(resp.Value, balance); err != nil {
return nil, fmt.Errorf("unable to unmarshal delegator query delegations: %w", err)
}
res := sdk.NewCoins()
for _, i := range balance.UnbondingResponses {
for _, r := range i.Entries {
res = res.Add(sdk.NewCoin(bondDenom, r.Balance))
}
}
return res, nil
}
// We use the baseapp.QueryRouter here to do inter-module state querying.
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
func getBalance(ctx sdk.Context, address string, queryServer grpc.Server) (sdk.Coins, error) {
querier, ok := queryServer.(*baseapp.GRPCQueryRouter)
if !ok {
return nil, fmt.Errorf("unexpected type: %T wanted *baseapp.GRPCQueryRouter", queryServer)
}
queryFn := querier.Route(balancesPath)
q := &banktypes.QueryAllBalancesRequest{
Address: address,
Pagination: nil,
}
b, err := proto.Marshal(q)
if err != nil {
return nil, fmt.Errorf("cannot marshal bank type query request, %w", err)
}
req := abci.RequestQuery{
Data: b,
Path: balancesPath,
}
resp, err := queryFn(ctx, req)
if err != nil {
return nil, fmt.Errorf("bank query error, %w", err)
}
balance := new(banktypes.QueryAllBalancesResponse)
if err := proto.Unmarshal(resp.Value, balance); err != nil {
return nil, fmt.Errorf("unable to unmarshal bank balance response: %w", err)
}
return balance.Balances, nil
}
// We use the baseapp.QueryRouter here to do inter-module state querying.
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
func getBondDenom(ctx sdk.Context, queryServer grpc.Server) (string, error) {
querier, ok := queryServer.(*baseapp.GRPCQueryRouter)
if !ok {
return "", fmt.Errorf("unexpected type: %T wanted *baseapp.GRPCQueryRouter", queryServer)
}
queryFn := querier.Route(stakingParamsPath)
q := &stakingtypes.QueryParamsRequest{}
b, err := proto.Marshal(q)
if err != nil {
return "", fmt.Errorf("cannot marshal staking params query request, %w", err)
}
req := abci.RequestQuery{
Data: b,
Path: stakingParamsPath,
}
resp, err := queryFn(ctx, req)
if err != nil {
return "", fmt.Errorf("staking query error, %w", err)
}
params := new(stakingtypes.QueryParamsResponse)
if err := proto.Unmarshal(resp.Value, params); err != nil {
return "", fmt.Errorf("unable to unmarshal delegator query delegations: %w", err)
}
return params.Params.BondDenom, nil
}
// MigrateAccount migrates vesting account to make the DelegatedVesting and DelegatedFree fields correctly
// track delegations.
// References: https://github.com/cosmos/cosmos-sdk/issues/8601, https://github.com/cosmos/cosmos-sdk/issues/8812
//
// We use the baseapp.QueryRouter here to do inter-module state querying.
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
func MigrateAccount(ctx sdk.Context, account types.AccountI, queryServer grpc.Server) (types.AccountI, error) {
return migrateVestingAccounts(ctx, account, queryServer)
}