-
Notifications
You must be signed in to change notification settings - Fork 116
/
keeper.go
817 lines (707 loc) · 29.9 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
package keeper
import (
"context"
"encoding/binary"
"fmt"
"reflect"
"time"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
ibchost "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibctmtypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
addresscodec "cosmossdk.io/core/address"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
consumertypes "github.com/cosmos/interchain-security/v6/x/ccv/consumer/types"
"github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
ccv "github.com/cosmos/interchain-security/v6/x/ccv/types"
)
// Keeper defines the Cross-Chain Validation Provider Keeper
type Keeper struct {
// address capable of executing gov messages (gov module account)
authority string
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
scopedKeeper ccv.ScopedKeeper
channelKeeper ccv.ChannelKeeper
portKeeper ccv.PortKeeper
connectionKeeper ccv.ConnectionKeeper
accountKeeper ccv.AccountKeeper
clientKeeper ccv.ClientKeeper
stakingKeeper ccv.StakingKeeper
slashingKeeper ccv.SlashingKeeper
distributionKeeper ccv.DistributionKeeper
bankKeeper ccv.BankKeeper
govKeeper govkeeper.Keeper
feeCollectorName string
validatorAddressCodec addresscodec.Codec
consensusAddressCodec addresscodec.Codec
}
// NewKeeper creates a new provider Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace paramtypes.Subspace, scopedKeeper ccv.ScopedKeeper,
channelKeeper ccv.ChannelKeeper, portKeeper ccv.PortKeeper,
connectionKeeper ccv.ConnectionKeeper, clientKeeper ccv.ClientKeeper,
stakingKeeper ccv.StakingKeeper, slashingKeeper ccv.SlashingKeeper,
accountKeeper ccv.AccountKeeper,
distributionKeeper ccv.DistributionKeeper, bankKeeper ccv.BankKeeper,
govKeeper govkeeper.Keeper,
authority string,
validatorAddressCodec, consensusAddressCodec addresscodec.Codec,
feeCollectorName string,
) Keeper {
k := Keeper{
cdc: cdc,
storeKey: key,
authority: authority,
scopedKeeper: scopedKeeper,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
connectionKeeper: connectionKeeper,
clientKeeper: clientKeeper,
stakingKeeper: stakingKeeper,
slashingKeeper: slashingKeeper,
accountKeeper: accountKeeper,
distributionKeeper: distributionKeeper,
bankKeeper: bankKeeper,
feeCollectorName: feeCollectorName,
validatorAddressCodec: validatorAddressCodec,
consensusAddressCodec: consensusAddressCodec,
govKeeper: govKeeper,
}
k.mustValidateFields()
return k
}
// GetAuthority returns the x/ccv/provider 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
}
// Validates that the provider keeper is initialized with non-zero and
// non-nil values for all its fields. Otherwise this method will panic.
func (k Keeper) mustValidateFields() {
// Ensures no fields are missed in this validation
if reflect.ValueOf(k).NumField() != 17 {
panic(fmt.Sprintf("number of fields in provider keeper is not 18 - have %d", reflect.ValueOf(k).NumField()))
}
if k.validatorAddressCodec == nil || k.consensusAddressCodec == nil {
panic("validator and/or consensus address codec are nil")
}
ccv.PanicIfZeroOrNil(k.cdc, "cdc") // 1
ccv.PanicIfZeroOrNil(k.storeKey, "storeKey") // 2
ccv.PanicIfZeroOrNil(k.scopedKeeper, "scopedKeeper") // 3
ccv.PanicIfZeroOrNil(k.channelKeeper, "channelKeeper") // 4
ccv.PanicIfZeroOrNil(k.portKeeper, "portKeeper") // 5
ccv.PanicIfZeroOrNil(k.connectionKeeper, "connectionKeeper") // 6
ccv.PanicIfZeroOrNil(k.accountKeeper, "accountKeeper") // 7
ccv.PanicIfZeroOrNil(k.clientKeeper, "clientKeeper") // 8
ccv.PanicIfZeroOrNil(k.stakingKeeper, "stakingKeeper") // 9
ccv.PanicIfZeroOrNil(k.slashingKeeper, "slashingKeeper") // 10
ccv.PanicIfZeroOrNil(k.distributionKeeper, "distributionKeeper") // 11
ccv.PanicIfZeroOrNil(k.bankKeeper, "bankKeeper") // 12
ccv.PanicIfZeroOrNil(k.feeCollectorName, "feeCollectorName") // 13
ccv.PanicIfZeroOrNil(k.authority, "authority") // 14
ccv.PanicIfZeroOrNil(k.validatorAddressCodec, "validatorAddressCodec") // 15
ccv.PanicIfZeroOrNil(k.consensusAddressCodec, "consensusAddressCodec") // 16
// this can be nil in tests
// ccv.PanicIfZeroOrNil(k.govKeeper, "govKeeper") // 17
}
func (k *Keeper) SetGovKeeper(govKeeper govkeeper.Keeper) {
k.govKeeper = govKeeper
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx context.Context) log.Logger {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return sdkCtx.Logger().With("module", "x/"+ibchost.ModuleName+"-"+types.ModuleName)
}
// IsBound checks if the CCV module is already bound to the desired port
func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID))
return ok
}
// BindPort defines a wrapper function for the port Keeper's function in
// order to expose it to module's InitGenesis function
func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
cap := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, cap, host.PortPath(portID))
}
// GetPort returns the portID for the CCV module. Used in ExportGenesis
func (k Keeper) GetPort(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
return string(store.Get(types.PortKey()))
}
// SetPort sets the portID for the CCV module. Used in InitGenesis
func (k Keeper) SetPort(ctx sdk.Context, portID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.PortKey(), []byte(portID))
}
// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
return k.scopedKeeper.AuthenticateCapability(ctx, cap, name)
}
// ClaimCapability allows the transfer module that can claim a capability that IBC module
// passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}
// SetConsumerIdToChannelId sets the mapping from a consumer id to the CCV channel id for that consumer chain.
func (k Keeper) SetConsumerIdToChannelId(ctx sdk.Context, consumerId, channelId string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.ConsumerIdToChannelIdKey(consumerId), []byte(channelId))
}
// GetConsumerIdToChannelId gets the CCV channelId for the given consumer id
func (k Keeper) GetConsumerIdToChannelId(ctx sdk.Context, consumerId string) (string, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ConsumerIdToChannelIdKey(consumerId))
if bz == nil {
return "", false
}
return string(bz), true
}
// DeleteConsumerIdToChannelId deletes the CCV channel id for the given consumer id
func (k Keeper) DeleteConsumerIdToChannelId(ctx sdk.Context, consumerId string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ConsumerIdToChannelIdKey(consumerId))
}
// GetAllConsumersWithIBCClients returns the ids of all consumer chains that with IBC clients created.
func (k Keeper) GetAllConsumersWithIBCClients(ctx sdk.Context) []string {
consumerIds := []string{}
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ConsumerIdToClientIdKeyPrefix())
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
// remove 1 byte prefix from key to retrieve consumerId
consumerId := string(iterator.Key()[1:])
consumerIds = append(consumerIds, consumerId)
}
return consumerIds
}
// SetChannelToConsumerId sets the mapping from the CCV channel id to the consumer id.
func (k Keeper) SetChannelToConsumerId(ctx sdk.Context, channelId, consumerId string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.ChannelToConsumerIdKey(channelId), []byte(consumerId))
}
// GetChannelIdToConsumerId gets the consumer id for a given CCV channel id
func (k Keeper) GetChannelIdToConsumerId(ctx sdk.Context, channelID string) (string, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ChannelToConsumerIdKey(channelID))
if bz == nil {
return "", false
}
return string(bz), true
}
// DeleteChannelIdToConsumerId deletes the consumer id for a given CCV channel id
func (k Keeper) DeleteChannelIdToConsumerId(ctx sdk.Context, channelId string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ChannelToConsumerIdKey(channelId))
}
// GetAllChannelToConsumers gets all channel to chain mappings. If a mapping exists,
// then the CCV channel to that consumer chain is established.
//
// Note that mapping from CCV channel IDs to consumer IDs
// is stored under keys with the following format:
// ChannelIdToConsumerIdKeyPrefix | channelID
// Thus, the returned array is in ascending order of channelIDs.
func (k Keeper) GetAllChannelToConsumers(ctx sdk.Context) (channelsToConsumers []struct {
ChannelId string
ConsumerId string
},
) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ChannelIdToConsumerIdKeyPrefix())
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
// remove prefix from key to retrieve channelID
channelID := string(iterator.Key()[1:])
consumerId := string(iterator.Value())
channelsToConsumers = append(channelsToConsumers, struct {
ChannelId string
ConsumerId string
}{
ChannelId: channelID,
ConsumerId: consumerId,
})
}
return channelsToConsumers
}
func (k Keeper) SetConsumerGenesis(ctx sdk.Context, consumerId string, gen ccv.ConsumerGenesisState) error {
store := ctx.KVStore(k.storeKey)
bz, err := gen.Marshal()
if err != nil {
return err
}
store.Set(types.ConsumerGenesisKey(consumerId), bz)
return nil
}
func (k Keeper) GetConsumerGenesis(ctx sdk.Context, consumerId string) (ccv.ConsumerGenesisState, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ConsumerGenesisKey(consumerId))
if bz == nil {
return ccv.ConsumerGenesisState{}, false
}
var data ccv.ConsumerGenesisState
if err := data.Unmarshal(bz); err != nil {
// An error here would indicate something is very wrong,
// the ConsumerGenesis is assumed to be correctly serialized in SetConsumerGenesis.
panic(fmt.Errorf("consumer genesis could not be unmarshaled: %w", err))
}
return data, true
}
func (k Keeper) DeleteConsumerGenesis(ctx sdk.Context, consumerId string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ConsumerGenesisKey(consumerId))
}
// VerifyConsumerChain verifies that the chain trying to connect on the channel handshake
// is the expected consumer chain.
func (k Keeper) VerifyConsumerChain(ctx sdk.Context, channelID string, connectionHops []string) error {
if len(connectionHops) != 1 {
return errorsmod.Wrap(channeltypes.ErrTooManyConnectionHops, "must have direct connection to provider chain")
}
connectionID := connectionHops[0]
clientId, _, err := k.getUnderlyingClient(ctx, connectionID)
if err != nil {
return err
}
consumerId, found := k.GetClientIdToConsumerId(ctx, clientId)
if !found {
return errorsmod.Wrapf(ccv.ErrConsumerChainNotFound, "cannot find consumer id associated with client id: %s", clientId)
}
ccvClientId, found := k.GetConsumerClientId(ctx, consumerId)
if !found {
return errorsmod.Wrapf(ccv.ErrClientNotFound, "cannot find client for consumer chain %s", consumerId)
}
if ccvClientId != clientId {
return errorsmod.Wrapf(types.ErrInvalidConsumerClient, "CCV channel must be built on top of CCV client. expected %s, got %s", ccvClientId, clientId)
}
// Verify that there isn't already a CCV channel for the consumer chain
if prevChannel, ok := k.GetConsumerIdToChannelId(ctx, consumerId); ok {
return errorsmod.Wrapf(ccv.ErrDuplicateChannel, "CCV channel with ID: %s already created for consumer chain %s", prevChannel, consumerId)
}
return nil
}
// SetConsumerChain ensures that the consumer chain has not already been
// set by a different channel, and then sets the consumer chain mappings
// in keeper, and set the channel status to validating.
// If there is already a CCV channel between the provider and consumer
// chain then close the channel, so that another channel can be made.
//
// SetConsumerChain is called by OnChanOpenConfirm.
func (k Keeper) SetConsumerChain(ctx sdk.Context, channelID string) error {
channel, ok := k.channelKeeper.GetChannel(ctx, ccv.ProviderPortID, channelID)
if !ok {
return errorsmod.Wrapf(channeltypes.ErrChannelNotFound, "channel not found for channel ID: %s", channelID)
}
if len(channel.ConnectionHops) != 1 {
return errorsmod.Wrap(channeltypes.ErrTooManyConnectionHops, "must have direct connection to consumer chain")
}
connectionID := channel.ConnectionHops[0]
clientID, tmClient, err := k.getUnderlyingClient(ctx, connectionID)
if err != nil {
return err
}
consumerId, found := k.GetClientIdToConsumerId(ctx, clientID)
if !found {
return errorsmod.Wrapf(types.ErrNoConsumerId, "cannot find a consumer chain associated for this client: %s", clientID)
}
// Verify that there isn't already a CCV channel for the consumer chain
chainID := tmClient.ChainId
if prevChannelID, ok := k.GetConsumerIdToChannelId(ctx, consumerId); ok {
return errorsmod.Wrapf(ccv.ErrDuplicateChannel, "CCV channel with ID: %s already created for consumer chain with id %s", prevChannelID, consumerId)
}
// the CCV channel is established:
// - set channel mappings
k.SetConsumerIdToChannelId(ctx, consumerId, channelID)
k.SetChannelToConsumerId(ctx, channelID, consumerId)
// - set current block height for the consumer chain initialization
k.SetInitChainHeight(ctx, consumerId, uint64(ctx.BlockHeight()))
// emit event on successful addition
ctx.EventManager().EmitEvent(
sdk.NewEvent(
ccv.EventTypeChannelEstablished,
sdk.NewAttribute(sdk.AttributeKeyModule, consumertypes.ModuleName),
sdk.NewAttribute(types.AttributeConsumerId, consumerId),
sdk.NewAttribute(types.AttributeConsumerChainId, chainID),
sdk.NewAttribute(conntypes.AttributeKeyClientID, clientID),
sdk.NewAttribute(channeltypes.AttributeKeyChannelID, channelID),
sdk.NewAttribute(conntypes.AttributeKeyConnectionID, connectionID),
),
)
return nil
}
// Retrieves the underlying client state corresponding to a connection ID.
func (k Keeper) getUnderlyingClient(ctx sdk.Context, connectionID string) (
clientID string, tmClient *ibctmtypes.ClientState, err error,
) {
conn, ok := k.connectionKeeper.GetConnection(ctx, connectionID)
if !ok {
return "", nil, errorsmod.Wrapf(conntypes.ErrConnectionNotFound,
"connection not found for connection ID: %s", connectionID)
}
clientID = conn.ClientId
clientState, ok := k.clientKeeper.GetClientState(ctx, clientID)
if !ok {
return "", nil, errorsmod.Wrapf(clienttypes.ErrClientNotFound,
"client not found for client ID: %s", conn.ClientId)
}
tmClient, ok = clientState.(*ibctmtypes.ClientState)
if !ok {
return "", nil, errorsmod.Wrapf(clienttypes.ErrInvalidClientType,
"invalid client type. expected %s, got %s", ibchost.Tendermint, clientState.ClientType())
}
return clientID, tmClient, nil
}
// chanCloseInit defines a wrapper function for the channel Keeper's function
func (k Keeper) chanCloseInit(ctx sdk.Context, channelID string) error {
capName := host.ChannelCapabilityPath(ccv.ProviderPortID, channelID)
chanCap, ok := k.scopedKeeper.GetCapability(ctx, capName)
if !ok {
return errorsmod.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName)
}
return k.channelKeeper.ChanCloseInit(ctx, ccv.ProviderPortID, channelID, chanCap)
}
func (k Keeper) IncrementValidatorSetUpdateId(ctx sdk.Context) {
validatorSetUpdateId := k.GetValidatorSetUpdateId(ctx)
k.SetValidatorSetUpdateId(ctx, validatorSetUpdateId+1)
}
func (k Keeper) SetValidatorSetUpdateId(ctx sdk.Context, valUpdateID uint64) {
store := ctx.KVStore(k.storeKey)
// Convert back into bytes for storage
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, valUpdateID)
store.Set(types.ValidatorSetUpdateIdKey(), bz)
}
func (k Keeper) GetValidatorSetUpdateId(ctx sdk.Context) (validatorSetUpdateId uint64) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ValidatorSetUpdateIdKey())
if bz == nil {
return 0
}
return binary.BigEndian.Uint64(bz)
}
// SetValsetUpdateBlockHeight sets the block height for a given valset update id
func (k Keeper) SetValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId, blockHeight uint64) {
store := ctx.KVStore(k.storeKey)
heightBytes := make([]byte, 8)
binary.BigEndian.PutUint64(heightBytes, blockHeight)
store.Set(types.ValsetUpdateBlockHeightKey(valsetUpdateId), heightBytes)
}
// GetValsetUpdateBlockHeight gets the block height for a given valset update id
func (k Keeper) GetValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId uint64) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ValsetUpdateBlockHeightKey(valsetUpdateId))
if bz == nil {
return 0, false
}
return binary.BigEndian.Uint64(bz), true
}
// GetAllValsetUpdateBlockHeights gets all the block heights for all valset updates
//
// Note that the mapping from vscIDs to block heights is stored under keys with the following format:
// ValsetUpdateBlockHeightKeyPrefix | vscID
// Thus, the returned array is in ascending order of vscIDs.
func (k Keeper) GetAllValsetUpdateBlockHeights(ctx sdk.Context) (valsetUpdateBlockHeights []types.ValsetUpdateIdToHeight) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ValsetUpdateBlockHeightKeyPrefix())
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
valsetUpdateId := binary.BigEndian.Uint64(iterator.Key()[1:])
height := binary.BigEndian.Uint64(iterator.Value())
valsetUpdateBlockHeights = append(valsetUpdateBlockHeights, types.ValsetUpdateIdToHeight{
ValsetUpdateId: valsetUpdateId,
Height: height,
})
}
return valsetUpdateBlockHeights
}
// DeleteValsetUpdateBlockHeight deletes the block height value for a given vaset update id
func (k Keeper) DeleteValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ValsetUpdateBlockHeightKey(valsetUpdateId))
}
// SetSlashAcks sets the slash acks under the given chain ID
//
// TODO: SlashAcks should be persisted as a list of ConsumerConsAddr types, not strings.
// See https://github.com/cosmos/interchain-security/issues/728
func (k Keeper) SetSlashAcks(ctx sdk.Context, consumerId string, acks []string) {
store := ctx.KVStore(k.storeKey)
sa := types.SlashAcks{
Addresses: acks,
}
bz, err := sa.Marshal()
if err != nil {
// An error here would indicate something is very wrong,
// sa is instantiated in this method and should be able to be marshaled.
panic(fmt.Errorf("failed to marshal SlashAcks: %w", err))
}
store.Set(types.SlashAcksKey(consumerId), bz)
}
// GetSlashAcks returns the slash acks stored under the given consumer id
//
// TODO: SlashAcks should be persisted as a list of ConsumerConsAddr types, not strings.
// See https://github.com/cosmos/interchain-security/issues/728
func (k Keeper) GetSlashAcks(ctx sdk.Context, consumerId string) []string {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.SlashAcksKey(consumerId))
if bz == nil {
return nil
}
var acks types.SlashAcks
if err := acks.Unmarshal(bz); err != nil {
// An error here would indicate something is very wrong,
// the SlashAcks are assumed to be correctly serialized in SetSlashAcks.
panic(fmt.Errorf("failed to unmarshal SlashAcks: %w", err))
}
return acks.GetAddresses()
}
// ConsumeSlashAcks empties and returns the slash acks for a given consumer id
func (k Keeper) ConsumeSlashAcks(ctx sdk.Context, consumerId string) (acks []string) {
acks = k.GetSlashAcks(ctx, consumerId)
if len(acks) < 1 {
return
}
store := ctx.KVStore(k.storeKey)
store.Delete(types.SlashAcksKey(consumerId))
return
}
// DeleteSlashAcks deletes the slash acks for a given consumer id
func (k Keeper) DeleteSlashAcks(ctx sdk.Context, consumerId string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.SlashAcksKey(consumerId))
}
// AppendSlashAck appends the given slash ack to the given consumer id slash acks in store
func (k Keeper) AppendSlashAck(ctx sdk.Context, consumerId,
ack string, // TODO: consumer cons addr should be accepted here, see https://github.com/cosmos/interchain-security/issues/728
) {
acks := k.GetSlashAcks(ctx, consumerId)
acks = append(acks, ack)
k.SetSlashAcks(ctx, consumerId, acks)
}
// SetInitChainHeight sets the provider block height when the given consumer chain was initiated
func (k Keeper) SetInitChainHeight(ctx sdk.Context, consumerId string, height uint64) {
store := ctx.KVStore(k.storeKey)
heightBytes := make([]byte, 8)
binary.BigEndian.PutUint64(heightBytes, height)
store.Set(types.InitChainHeightKey(consumerId), heightBytes)
}
// GetInitChainHeight returns the provider block height when the given consumer chain was initiated
func (k Keeper) GetInitChainHeight(ctx sdk.Context, consumerId string) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.InitChainHeightKey(consumerId))
if bz == nil {
return 0, false
}
return binary.BigEndian.Uint64(bz), true
}
// DeleteInitChainHeight deletes the block height value for which the given consumer chain's channel was established
func (k Keeper) DeleteInitChainHeight(ctx sdk.Context, consumerId string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.InitChainHeightKey(consumerId))
}
// GetPendingVSCPackets returns the list of pending ValidatorSetChange packets stored under consumer id
func (k Keeper) GetPendingVSCPackets(ctx sdk.Context, consumerId string) []ccv.ValidatorSetChangePacketData {
var packets types.ValidatorSetChangePackets
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.PendingVSCsKey(consumerId))
if bz == nil {
return []ccv.ValidatorSetChangePacketData{}
}
if err := packets.Unmarshal(bz); err != nil {
// An error here would indicate something is very wrong,
// the PendingVSCPackets are assumed to be correctly serialized in AppendPendingVSCPackets.
panic(fmt.Errorf("cannot unmarshal pending validator set changes: %w", err))
}
return packets.GetList()
}
// AppendPendingVSCPackets adds the given ValidatorSetChange packet to the list
// of pending ValidatorSetChange packets stored under consumer id
func (k Keeper) AppendPendingVSCPackets(ctx sdk.Context, consumerId string, newPackets ...ccv.ValidatorSetChangePacketData) {
pds := append(k.GetPendingVSCPackets(ctx, consumerId), newPackets...)
store := ctx.KVStore(k.storeKey)
packets := types.ValidatorSetChangePackets{List: pds}
buf, err := packets.Marshal()
if err != nil {
// An error here would indicate something is very wrong,
// packets is instantiated in this method and should be able to be marshaled.
panic(fmt.Errorf("cannot marshal pending validator set changes: %w", err))
}
store.Set(types.PendingVSCsKey(consumerId), buf)
}
// DeletePendingVSCPackets deletes the list of pending ValidatorSetChange packets for chain ID
func (k Keeper) DeletePendingVSCPackets(ctx sdk.Context, consumerId string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.PendingVSCsKey(consumerId))
}
// SetConsumerClientId sets the client id for the given consumer id.
// Note that the method also stores a reverse index that can be accessed
// by calling GetClientIdToConsumerId.
func (k Keeper) SetConsumerClientId(ctx sdk.Context, consumerId, clientId string) {
store := ctx.KVStore(k.storeKey)
if prevClientId, found := k.GetConsumerClientId(ctx, consumerId); found {
// delete reverse index
store.Delete(types.ClientIdToConsumerIdKey(prevClientId))
}
store.Set(types.ConsumerIdToClientIdKey(consumerId), []byte(clientId))
// set the reverse index
store.Set(types.ClientIdToConsumerIdKey(clientId), []byte(consumerId))
}
// GetConsumerClientId returns the client id for the given consumer id.
func (k Keeper) GetConsumerClientId(ctx sdk.Context, consumerId string) (string, bool) {
store := ctx.KVStore(k.storeKey)
clientIdBytes := store.Get(types.ConsumerIdToClientIdKey(consumerId))
if clientIdBytes == nil {
return "", false
}
return string(clientIdBytes), true
}
// GetClientIdToConsumerId returns the consumer id associated with this client id
func (k Keeper) GetClientIdToConsumerId(ctx sdk.Context, clientId string) (string, bool) {
store := ctx.KVStore(k.storeKey)
consumerIdBytes := store.Get(types.ClientIdToConsumerIdKey(clientId))
if consumerIdBytes == nil {
return "", false
}
return string(consumerIdBytes), true
}
// DeleteConsumerClientId removes from the store the client id for the given consumer id.
// Note that the method also removes the reverse index.
func (k Keeper) DeleteConsumerClientId(ctx sdk.Context, consumerId string) {
store := ctx.KVStore(k.storeKey)
if clientId, found := k.GetConsumerClientId(ctx, consumerId); found {
// delete reverse index
store.Delete(types.ClientIdToConsumerIdKey(clientId))
}
store.Delete(types.ConsumerIdToClientIdKey(consumerId))
}
// SetSlashLog updates validator's slash log for a consumer chain
// If an entry exists for a given validator address, at least one
// double signing slash packet was received by the provider from at least one consumer chain
func (k Keeper) SetSlashLog(
ctx sdk.Context,
providerAddr types.ProviderConsAddress,
) {
store := ctx.KVStore(k.storeKey)
store.Set(types.SlashLogKey(providerAddr), []byte{})
}
// GetSlashLog returns a validator's slash log status
// True will be returned if an entry exists for a given validator address
func (k Keeper) GetSlashLog(
ctx sdk.Context,
providerAddr types.ProviderConsAddress,
) (found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.SlashLogKey(providerAddr))
return bz != nil
}
func (k Keeper) BondDenom(ctx sdk.Context) (string, error) {
return k.stakingKeeper.BondDenom(ctx)
}
// GetAllConsumerIds returns all the existing consumer ids
func (k Keeper) GetAllConsumerIds(ctx sdk.Context) []string {
latestConsumerId, found := k.GetConsumerId(ctx)
if !found {
return []string{}
}
consumerIds := []string{}
for i := uint64(0); i < latestConsumerId; i++ {
consumerId := fmt.Sprintf("%d", i)
consumerIds = append(consumerIds, consumerId)
}
return consumerIds
}
// GetAllActiveConsumerIds returns all the consumer ids of chains that are registered, initialized, or launched
func (k Keeper) GetAllActiveConsumerIds(ctx sdk.Context) []string {
consumerIds := []string{}
for _, consumerId := range k.GetAllConsumerIds(ctx) {
if !k.IsConsumerActive(ctx, consumerId) {
continue
}
consumerIds = append(consumerIds, consumerId)
}
return consumerIds
}
// SetConsumerCommissionRate sets a per-consumer chain commission rate
// for the given validator address
func (k Keeper) SetConsumerCommissionRate(
ctx sdk.Context,
consumerId string,
providerAddr types.ProviderConsAddress,
commissionRate math.LegacyDec,
) error {
store := ctx.KVStore(k.storeKey)
bz, err := commissionRate.Marshal()
if err != nil {
err = fmt.Errorf("consumer commission rate marshalling failed: %s", err)
k.Logger(ctx).Error(err.Error())
return err
}
store.Set(types.ConsumerCommissionRateKey(consumerId, providerAddr), bz)
return nil
}
// GetConsumerCommissionRate returns the per-consumer commission rate set
// for the given validator address
func (k Keeper) GetConsumerCommissionRate(
ctx sdk.Context,
consumerId string,
providerAddr types.ProviderConsAddress,
) (math.LegacyDec, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ConsumerCommissionRateKey(consumerId, providerAddr))
if bz == nil {
return math.LegacyZeroDec(), false
}
cr := math.LegacyZeroDec()
// handle error gracefully since it's called in BeginBlockRD
if err := cr.Unmarshal(bz); err != nil {
k.Logger(ctx).Error("consumer commission rate unmarshalling failed: %s", err)
return cr, false
}
return cr, true
}
// GetAllCommissionRateValidators returns all the validator address
// that set a commission rate for the given consumer id
func (k Keeper) GetAllCommissionRateValidators(
ctx sdk.Context,
consumerId string,
) (addresses []types.ProviderConsAddress) {
store := ctx.KVStore(k.storeKey)
key := types.StringIdWithLenKey(types.ConsumerCommissionRateKeyPrefix(), consumerId)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
providerAddr := types.NewProviderConsAddress(iterator.Key()[len(key):])
addresses = append(addresses, providerAddr)
}
return addresses
}
// DeleteConsumerCommissionRate the per-consumer chain commission rate
// associated to the given validator address
func (k Keeper) DeleteConsumerCommissionRate(
ctx sdk.Context,
consumerId string,
providerAddr types.ProviderConsAddress,
) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ConsumerCommissionRateKey(consumerId, providerAddr))
}
func (k Keeper) UnbondingCanComplete(ctx sdk.Context, id uint64) error {
return k.stakingKeeper.UnbondingCanComplete(ctx, id)
}
func (k Keeper) UnbondingTime(ctx sdk.Context) (time.Duration, error) {
return k.stakingKeeper.UnbondingTime(ctx)
}