-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
keeper.go
375 lines (344 loc) · 15 KB
/
keeper.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package keeper
import (
"fmt"
"time"
gogotypes "github.com/cosmos/gogoproto/types"
"cosmossdk.io/collections"
collcodec "cosmossdk.io/collections/codec"
"cosmossdk.io/collections/indexes"
addresscodec "cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/comet"
"cosmossdk.io/math"
"cosmossdk.io/x/staking/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Implements ValidatorSet interface
var _ types.ValidatorSet = Keeper{}
// Implements DelegationSet interface
var _ types.DelegationSet = Keeper{}
type rotationHistoryIndexes struct {
Block *indexes.Multi[uint64, collections.Pair[[]byte, uint64], types.ConsPubKeyRotationHistory]
}
func (a rotationHistoryIndexes) IndexesList() []collections.Index[collections.Pair[[]byte, uint64], types.ConsPubKeyRotationHistory] {
return []collections.Index[collections.Pair[[]byte, uint64], types.ConsPubKeyRotationHistory]{
a.Block,
}
}
func NewRotationHistoryIndexes(sb *collections.SchemaBuilder) rotationHistoryIndexes {
return rotationHistoryIndexes{
Block: indexes.NewMulti(
sb,
types.BlockConsPubKeyRotationHistoryKey,
"cons_pubkey_history_by_block",
collections.Uint64Key,
collections.PairKeyCodec(collections.BytesKey, collections.Uint64Key),
func(key collections.Pair[[]byte, uint64], v types.ConsPubKeyRotationHistory) (uint64, error) {
return v.Height, nil
},
),
}
}
// Keeper of the x/staking store
type Keeper struct {
appmodule.Environment
cdc codec.BinaryCodec
authKeeper types.AccountKeeper
bankKeeper types.BankKeeper
consensusKeeper types.ConsensusKeeper
hooks types.StakingHooks
authority string
validatorAddressCodec addresscodec.Codec
consensusAddressCodec addresscodec.Codec
cometInfoService comet.Service
Schema collections.Schema
// LastTotalPower value: LastTotalPower
LastTotalPower collections.Item[math.Int]
// DelegationsByValidator key: valAddr+delAddr | value: none used (index key for delegations by validator index)
DelegationsByValidator collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], []byte]
UnbondingID collections.Sequence
// ValidatorByConsensusAddress key: consAddr | value: valAddr
ValidatorByConsensusAddress collections.Map[sdk.ConsAddress, sdk.ValAddress]
// UnbondingType key: unbondingID | value: index of UnbondingType
UnbondingType collections.Map[uint64, uint64]
// Redelegations key: AccAddr+SrcValAddr+DstValAddr | value: Redelegation
Redelegations collections.Map[collections.Triple[[]byte, []byte, []byte], types.Redelegation]
// Delegations key: AccAddr+valAddr | value: Delegation
Delegations collections.Map[collections.Pair[sdk.AccAddress, sdk.ValAddress], types.Delegation]
// UnbondingIndex key:UnbondingID | value: ubdKey (ubdKey = [UnbondingDelegationKey(Prefix)+len(delAddr)+delAddr+len(valAddr)+valAddr])
UnbondingIndex collections.Map[uint64, []byte]
// UnbondingQueue key: Timestamp | value: DVPairs [delAddr+valAddr]
UnbondingQueue collections.Map[time.Time, types.DVPairs]
// Validators key: valAddr | value: Validator
Validators collections.Map[[]byte, types.Validator]
// UnbondingDelegations key: delAddr+valAddr | value: UnbondingDelegation
UnbondingDelegations collections.Map[collections.Pair[[]byte, []byte], types.UnbondingDelegation]
// RedelegationsByValDst key: DstValAddr+DelAccAddr+SrcValAddr | value: none used (index key for Redelegations stored by DstVal index)
RedelegationsByValDst collections.Map[collections.Triple[[]byte, []byte, []byte], []byte]
// RedelegationsByValSrc key: SrcValAddr+DelAccAddr+DstValAddr | value: none used (index key for Redelegations stored by SrcVal index)
RedelegationsByValSrc collections.Map[collections.Triple[[]byte, []byte, []byte], []byte]
// UnbondingDelegationByValIndex key: valAddr+delAddr | value: none used (index key for UnbondingDelegations stored by validator index)
UnbondingDelegationByValIndex collections.Map[collections.Pair[[]byte, []byte], []byte]
// RedelegationQueue key: Timestamp | value: DVVTriplets [delAddr+valSrcAddr+valDstAddr]
RedelegationQueue collections.Map[time.Time, types.DVVTriplets]
// ValidatorQueue key: len(timestamp bytes)+timestamp+height | value: ValAddresses
ValidatorQueue collections.Map[collections.Triple[uint64, time.Time, uint64], types.ValAddresses]
// LastValidatorPower key: valAddr | value: power(gogotypes.Int64Value())
LastValidatorPower collections.Map[[]byte, gogotypes.Int64Value]
// Params key: ParamsKeyPrefix | value: Params
Params collections.Item[types.Params]
// ValidatorConsensusKeyRotationRecordIndexKey: this key is used to restrict the validator next rotation within waiting (unbonding) period
ValidatorConsensusKeyRotationRecordIndexKey collections.KeySet[collections.Pair[[]byte, time.Time]]
// ValidatorConsensusKeyRotationRecordQueue: this key is used to set the unbonding period time on each rotation
ValidatorConsensusKeyRotationRecordQueue collections.Map[time.Time, types.ValAddrsOfRotatedConsKeys]
// ConsAddrToValidatorIdentifierMap: maps the new cons addr to the initial cons addr
ConsAddrToValidatorIdentifierMap collections.Map[[]byte, []byte]
// OldToNewConsAddrMap: maps the old cons addr to the new cons addr
OldToNewConsAddrMap collections.Map[[]byte, []byte]
// ValidatorConsPubKeyRotationHistory: consPubkey rotation history by validator
// A index is being added with key `BlockConsPubKeyRotationHistory`: consPubkey rotation history by height
RotationHistory *collections.IndexedMap[collections.Pair[[]byte, uint64], types.ConsPubKeyRotationHistory, rotationHistoryIndexes]
}
// NewKeeper creates a new staking Keeper instance
func NewKeeper(
cdc codec.BinaryCodec,
env appmodule.Environment,
ak types.AccountKeeper,
bk types.BankKeeper,
ck types.ConsensusKeeper,
authority string,
validatorAddressCodec addresscodec.Codec,
consensusAddressCodec addresscodec.Codec,
cometInfoService comet.Service,
) *Keeper {
sb := collections.NewSchemaBuilder(env.KVStoreService)
// ensure bonded and not bonded module accounts are set
if addr := ak.GetModuleAddress(types.BondedPoolName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName))
}
if addr := ak.GetModuleAddress(types.NotBondedPoolName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
}
// ensure that authority is a valid AccAddress
if _, err := ak.AddressCodec().StringToBytes(authority); err != nil {
panic("authority is not a valid acc address")
}
if validatorAddressCodec == nil || consensusAddressCodec == nil {
panic("validator and/or consensus address codec are nil")
}
k := &Keeper{
Environment: env,
cdc: cdc,
authKeeper: ak,
bankKeeper: bk,
consensusKeeper: ck,
hooks: nil,
authority: authority,
validatorAddressCodec: validatorAddressCodec,
consensusAddressCodec: consensusAddressCodec,
cometInfoService: cometInfoService,
LastTotalPower: collections.NewItem(sb, types.LastTotalPowerKey, "last_total_power", sdk.IntValue),
Delegations: collections.NewMap(
sb, types.DelegationKey, "delegations",
collections.NamedPairKeyCodec(
"delegator",
sdk.LengthPrefixedAddressKey(sdk.AccAddressKey), //nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
"validator_address_key",
sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), //nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
),
codec.CollValue[types.Delegation](cdc),
),
DelegationsByValidator: collections.NewMap(
sb, types.DelegationByValIndexKey,
"delegations_by_validator",
collections.NamedPairKeyCodec(
"validator_address",
sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), //nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
"delegator",
sdk.AccAddressKey,
),
collections.BytesValue,
),
UnbondingID: collections.NewSequence(sb, types.UnbondingIDKey, "unbonding_id"),
ValidatorByConsensusAddress: collections.NewMap(
sb, types.ValidatorsByConsAddrKey,
"validator_by_cons_addr",
sdk.LengthPrefixedAddressKey(sdk.ConsAddressKey).WithName("cons_address"), //nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
collcodec.KeyToValueCodec(sdk.ValAddressKey),
),
UnbondingType: collections.NewMap(sb, types.UnbondingTypeKey, "unbonding_type", collections.Uint64Key.WithName("unbonding_id"), collections.Uint64Value),
// key format is: 52 | lengthPrefixedBytes(AccAddr) | lengthPrefixedBytes(SrcValAddr) | lengthPrefixedBytes(DstValAddr)
Redelegations: collections.NewMap(
sb, types.RedelegationKey,
"redelegations",
collections.NamedTripleKeyCodec(
"delegator",
collections.BytesKey,
"src_validator",
collections.BytesKey,
"dst_validator",
sdk.LengthPrefixedBytesKey, // sdk.LengthPrefixedBytesKey is needed to retain state compatibility
),
codec.CollValue[types.Redelegation](cdc),
),
UnbondingIndex: collections.NewMap(sb, types.UnbondingIndexKey, "unbonding_index", collections.Uint64Key.WithName("index"), collections.BytesValue.WithName("ubd_key")),
UnbondingDelegationByValIndex: collections.NewMap(
sb, types.UnbondingDelegationByValIndexKey,
"unbonding_delegation_by_val_index",
collections.NamedPairKeyCodec(
"validator_address",
sdk.LengthPrefixedBytesKey,
"delegator",
sdk.LengthPrefixedBytesKey,
), // sdk.LengthPrefixedBytesKey is needed to retain state compatibility
collections.BytesValue,
),
UnbondingQueue: collections.NewMap(sb, types.UnbondingQueueKey, "unbonidng_queue", sdk.TimeKey.WithName("unbonding_time"), codec.CollValue[types.DVPairs](cdc)),
// key format is: 53 | lengthPrefixedBytes(SrcValAddr) | lengthPrefixedBytes(AccAddr) | lengthPrefixedBytes(DstValAddr)
RedelegationsByValSrc: collections.NewMap(
sb, types.RedelegationByValSrcIndexKey,
"redelegations_by_val_src",
collections.NamedTripleKeyCodec(
"src_validator",
collections.BytesKey,
"delegator",
collections.BytesKey,
"dst_validator",
sdk.LengthPrefixedBytesKey, // sdk.LengthPrefixedBytesKey is needed to retain state compatibility
),
collections.BytesValue,
),
// key format is: 17 | lengthPrefixedBytes(valAddr) | power
LastValidatorPower: collections.NewMap(sb, types.LastValidatorPowerKey, "last_validator_power", sdk.LengthPrefixedBytesKey.WithName("validator_address"), codec.CollValue[gogotypes.Int64Value](cdc)), // sdk.LengthPrefixedBytesKey is needed to retain state compatibility
// key format is: 54 | lengthPrefixedBytes(DstValAddr) | lengthPrefixedBytes(AccAddr) | lengthPrefixedBytes(SrcValAddr)
RedelegationsByValDst: collections.NewMap(
sb, types.RedelegationByValDstIndexKey,
"redelegations_by_val_dst",
collections.NamedTripleKeyCodec(
"dst_validator",
collections.BytesKey,
"delegator",
collections.BytesKey,
"src_validator",
sdk.LengthPrefixedBytesKey, // sdk.LengthPrefixedBytesKey is needed to retain state compatibility
),
collections.BytesValue,
),
RedelegationQueue: collections.NewMap(sb, types.RedelegationQueueKey, "redelegation_queue", sdk.TimeKey.WithName("completion_time"), codec.CollValue[types.DVVTriplets](cdc)),
Validators: collections.NewMap(
sb,
types.ValidatorsKey,
"validators",
sdk.LengthPrefixedBytesKey.WithName("validator_address"), // sdk.LengthPrefixedBytesKey is needed to retain state compatibility
codec.CollValue[types.Validator](cdc),
),
UnbondingDelegations: collections.NewMap(
sb, types.UnbondingDelegationKey,
"unbonding_delegation",
collections.NamedPairKeyCodec(
"delegator",
collections.BytesKey,
"validator",
sdk.LengthPrefixedBytesKey, // sdk.LengthPrefixedBytesKey is needed to retain state compatibility
),
codec.CollValue[types.UnbondingDelegation](cdc),
),
// key format is: 67 | length(timestamp Bytes) | timestamp | height
// Note: We use 3 keys here because we prefixed time bytes with its length previously and to retain state compatibility we remain to use the same
ValidatorQueue: collections.NewMap(
sb, types.ValidatorQueueKey,
"validator_queue",
collections.NamedTripleKeyCodec(
"ts_length",
collections.Uint64Key,
"timestamp",
sdk.TimeKey,
"height",
collections.Uint64Key,
),
codec.CollValue[types.ValAddresses](cdc),
),
// key is: 113 (it's a direct prefix)
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
// key format is: 104 | valAddr | time
ValidatorConsensusKeyRotationRecordIndexKey: collections.NewKeySet(
sb, types.ValidatorConsensusKeyRotationRecordIndexKey,
"cons_pub_rotation_index",
collections.NamedPairKeyCodec(
"validator_address",
collections.BytesKey,
"time",
sdk.TimeKey),
),
// key format is: 103 | time
ValidatorConsensusKeyRotationRecordQueue: collections.NewMap(
sb, types.ValidatorConsensusKeyRotationRecordQueueKey,
"cons_pub_rotation_queue",
sdk.TimeKey,
codec.CollValue[types.ValAddrsOfRotatedConsKeys](cdc),
),
// key format is: 105 | consAddr
ConsAddrToValidatorIdentifierMap: collections.NewMap(
sb, types.ConsAddrToValidatorIdentifierMapPrefix,
"new_to_old_cons_addr_map",
collections.BytesKey,
collections.BytesValue,
),
// key format is: 106 | consAddr
OldToNewConsAddrMap: collections.NewMap(
sb, types.OldToNewConsAddrMap,
"old_to_new_cons_key_map",
collections.BytesKey,
collections.BytesValue,
),
// key format is : 101 | rotation history
// index is : 102 | rotation history
RotationHistory: collections.NewIndexedMap(
sb,
types.ValidatorConsPubKeyRotationHistoryKey,
"cons_pub_rotation_history",
collections.NamedPairKeyCodec(
"validator_address",
collections.BytesKey,
"height_key",
collections.Uint64Key),
codec.CollValue[types.ConsPubKeyRotationHistory](cdc),
NewRotationHistoryIndexes(sb),
),
}
schema, err := sb.Build()
if err != nil {
panic(err)
}
k.Schema = schema
return k
}
// Hooks gets the hooks for staking *Keeper {
func (k *Keeper) Hooks() types.StakingHooks {
if k.hooks == nil {
// return a no-op implementation if no hooks are set
return types.MultiStakingHooks{}
}
return k.hooks
}
// SetHooks sets the validator hooks. In contrast to other receivers, this method must take a pointer due to nature
// of the hooks interface and SDK start up sequence.
func (k *Keeper) SetHooks(sh types.StakingHooks) {
if k.hooks != nil {
panic("cannot set validator hooks twice")
}
k.hooks = sh
}
// GetAuthority returns the x/staking module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
}
// ValidatorAddressCodec returns the app validator address codec.
func (k Keeper) ValidatorAddressCodec() addresscodec.Codec {
return k.validatorAddressCodec
}
// ConsensusAddressCodec returns the app consensus address codec.
func (k Keeper) ConsensusAddressCodec() addresscodec.Codec {
return k.consensusAddressCodec
}