/
keeper.go
605 lines (498 loc) · 21 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
package keeper
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"reflect"
"sort"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
porttypes "github.com/cosmos/ibc-go/v6/modules/core/05-port/types"
"github.com/cosmos/ibc-go/v6/modules/core/exported"
"github.com/ethereum/go-ethereum/common"
"github.com/peggyjv/sommelier/v7/x/axelarcork/types"
"github.com/tendermint/tendermint/libs/log"
)
var _ porttypes.ICS4Wrapper = &Keeper{}
// Keeper of the oracle store
type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
paramSpace paramtypes.Subspace
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
stakingKeeper types.StakingKeeper
transferKeeper types.TransferKeeper
distributionKeeper types.DistributionKeeper
gravityKeeper types.GravityKeeper
pubsubKeeper types.PubsubKeeper
Ics4Wrapper types.ICS4Wrapper
}
// NewKeeper creates a new x/axelarcork Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace paramtypes.Subspace,
accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, stakingKeeper types.StakingKeeper,
transferKeeper types.TransferKeeper, distributionKeeper types.DistributionKeeper,
wrapper types.ICS4Wrapper, gravityKeeper types.GravityKeeper, pubsubKeeper types.PubsubKeeper,
) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
stakingKeeper: stakingKeeper,
transferKeeper: transferKeeper,
distributionKeeper: distributionKeeper,
gravityKeeper: gravityKeeper,
pubsubKeeper: pubsubKeeper,
Ics4Wrapper: wrapper,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
// SetTransferKeeper sets the transferKeeper
func (k *Keeper) SetTransferKeeper(transferKeeper types.TransferKeeper) {
k.transferKeeper = transferKeeper
}
////////////
// Params //
////////////
const CorkVoteThresholdStr = "0.67"
// GetParamSet returns the vote period from the parameters
func (k Keeper) GetParamSet(ctx sdk.Context) types.Params {
var p types.Params
k.paramSpace.GetParamSet(ctx, &p)
return p
}
// SetParams sets the parameters in the store
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
// using this direct method instead of k.paramSpace.SetParamSet because our
// param validation should have happened on ValidateBasic, where it is
// contingent on being "enabled"
for _, pair := range params.ParamSetPairs() {
v := reflect.Indirect(reflect.ValueOf(pair.Value)).Interface()
k.paramSpace.Set(ctx, pair.Key, v)
}
}
/////////////////////
// Scheduled Corks //
/////////////////////
func (k Keeper) SetScheduledAxelarCork(ctx sdk.Context, chainID uint64, blockHeight uint64, val sdk.ValAddress, cork types.AxelarCork) []byte {
id := cork.IDHash(blockHeight)
bz := k.cdc.MustMarshal(&cork)
ctx.KVStore(k.storeKey).Set(types.GetScheduledAxelarCorkKey(chainID, blockHeight, id, val, common.HexToAddress(cork.TargetContractAddress)), bz)
return id
}
// GetScheduledAxelarCork gets the scheduled cork for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) GetScheduledAxelarCork(ctx sdk.Context, chainID uint64, blockHeight uint64, id []byte, val sdk.ValAddress, contract common.Address) (types.AxelarCork, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetScheduledAxelarCorkKey(chainID, blockHeight, id, val, contract))
if len(bz) == 0 {
return types.AxelarCork{}, false
}
var cork types.AxelarCork
k.cdc.MustUnmarshal(bz, &cork)
return cork, true
}
// DeleteScheduledAxelarCork deletes the scheduled cork for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) DeleteScheduledAxelarCork(ctx sdk.Context, chainID uint64, blockHeight uint64, id []byte, val sdk.ValAddress, contract common.Address) {
ctx.KVStore(k.storeKey).Delete(types.GetScheduledAxelarCorkKey(chainID, blockHeight, id, val, contract))
}
// IterateScheduledAxelarCorks iterates over all scheduled corks by chain ID
func (k Keeper) IterateScheduledAxelarCorks(ctx sdk.Context, chainID uint64, cb func(val sdk.ValAddress, blockHeight uint64, id []byte, cel common.Address, cork types.AxelarCork) (stop bool)) {
k.IterateScheduledAxelarCorksByPrefix(ctx, types.GetScheduledAxelarCorkKeyPrefix(chainID), cb)
}
func (k Keeper) IterateScheduledAxelarCorksByBlockHeight(ctx sdk.Context, chainID uint64, blockHeight uint64, cb func(val sdk.ValAddress, blockHeight uint64, id []byte, cel common.Address, cork types.AxelarCork) (stop bool)) {
k.IterateScheduledAxelarCorksByPrefix(ctx, types.GetScheduledAxelarCorkKeyByBlockHeightPrefix(chainID, blockHeight), cb)
}
func (k Keeper) IterateScheduledAxelarCorksByPrefix(ctx sdk.Context, prefix []byte, cb func(val sdk.ValAddress, blockHeight uint64, id []byte, cel common.Address, cork types.AxelarCork) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, prefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var cork types.AxelarCork
keyPair := bytes.NewBuffer(iter.Key())
keyPair.Next(1) // trim prefix byte
keyPair.Next(8) // trim chain id, it was filtered in the prefix
blockHeight := sdk.BigEndianToUint64(keyPair.Next(8))
id := keyPair.Next(32)
val := sdk.ValAddress(keyPair.Next(20))
contract := common.BytesToAddress(keyPair.Next(20))
k.cdc.MustUnmarshal(iter.Value(), &cork)
if cb(val, blockHeight, id, contract, cork) {
break
}
}
}
func (k Keeper) GetScheduledAxelarCorks(ctx sdk.Context, chainID uint64) []*types.ScheduledAxelarCork {
var scheduledCorks []*types.ScheduledAxelarCork
k.IterateScheduledAxelarCorks(ctx, chainID, func(val sdk.ValAddress, blockHeight uint64, id []byte, _ common.Address, cork types.AxelarCork) (stop bool) {
scheduledCorks = append(scheduledCorks, &types.ScheduledAxelarCork{
Validator: val.String(),
Cork: &cork,
BlockHeight: blockHeight,
Id: hex.EncodeToString(id),
})
return false
})
return scheduledCorks
}
func (k Keeper) GetScheduledAxelarCorksByBlockHeight(ctx sdk.Context, chainID uint64, height uint64) []*types.ScheduledAxelarCork {
var scheduledCorks []*types.ScheduledAxelarCork
k.IterateScheduledAxelarCorksByBlockHeight(ctx, chainID, height, func(val sdk.ValAddress, blockHeight uint64, Id []byte, _ common.Address, cork types.AxelarCork) (stop bool) {
scheduledCorks = append(scheduledCorks, &types.ScheduledAxelarCork{
Validator: val.String(),
Cork: &cork,
BlockHeight: blockHeight,
Id: hex.EncodeToString(Id),
})
return false
})
return scheduledCorks
}
func (k Keeper) GetScheduledAxelarCorksByID(ctx sdk.Context, chainID uint64, queriedID []byte) []*types.ScheduledAxelarCork {
var scheduledCorks []*types.ScheduledAxelarCork
k.IterateScheduledAxelarCorks(ctx, chainID, func(val sdk.ValAddress, blockHeight uint64, id []byte, _ common.Address, cork types.AxelarCork) (stop bool) {
if bytes.Equal(id, queriedID) {
scheduledCorks = append(scheduledCorks, &types.ScheduledAxelarCork{
Validator: val.String(),
Cork: &cork,
BlockHeight: blockHeight,
Id: hex.EncodeToString(id),
})
}
return false
})
return scheduledCorks
}
/////////////////
// WinningCork //
/////////////////
func (k Keeper) SetWinningAxelarCork(ctx sdk.Context, chainID uint64, blockHeight uint64, cork types.AxelarCork) {
bz := k.cdc.MustMarshal(&cork)
ctx.KVStore(k.storeKey).Set(types.GetWinningAxelarCorkKey(chainID, blockHeight, common.HexToAddress(cork.TargetContractAddress)), bz)
}
func (k Keeper) IterateWinningAxelarCorks(ctx sdk.Context, chainID uint64, cb func(contract common.Address, blockHeight uint64, cork types.AxelarCork) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.GetWinningAxelarCorkKeyPrefix(chainID))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var cork types.AxelarCork
keyPair := bytes.NewBuffer(iter.Key())
keyPair.Next(1) // trim prefix byte
keyPair.Next(8) // trim chain ID
blockHeight := binary.BigEndian.Uint64(keyPair.Next(8))
contractAddress := common.BytesToAddress(keyPair.Next(20)) // contract
k.cdc.MustUnmarshal(iter.Value(), &cork)
if cb(contractAddress, blockHeight, cork) {
break
}
}
}
func (k Keeper) GetWinningAxelarCork(ctx sdk.Context, chainID uint64, contractAddr common.Address) (uint64, types.AxelarCork, bool) {
var bh uint64
var c types.AxelarCork
found := false
k.IterateWinningAxelarCorks(ctx, chainID, func(contract common.Address, blockHeight uint64, cork types.AxelarCork) (stop bool) {
if bytes.Equal(contractAddr.Bytes(), contract.Bytes()) {
bh = blockHeight
c = cork
found = true
return true
}
return false
})
return bh, c, found
}
func (k Keeper) DeleteWinningAxelarCorkByBlockheight(ctx sdk.Context, chainID uint64, blockHeight uint64, cork types.AxelarCork) {
ctx.KVStore(k.storeKey).Delete(types.GetWinningAxelarCorkKey(chainID, blockHeight, common.HexToAddress(cork.TargetContractAddress)))
}
// TODO (Collin): Need pruning logic. This method is unused.
func (k Keeper) DeleteWinningAxelarCork(ctx sdk.Context, chainID uint64, c types.AxelarCork) {
k.IterateWinningAxelarCorks(ctx, chainID, func(contract common.Address, blockHeight uint64, cork types.AxelarCork) (stop bool) {
if c.Equals(cork) {
k.DeleteWinningAxelarCorkByBlockheight(ctx, chainID, blockHeight, cork)
return true
}
return false
})
}
///////////////////////////
// ScheduledBlockHeights //
///////////////////////////
func (k Keeper) GetScheduledBlockHeights(ctx sdk.Context, chainID uint64) []uint64 {
var heights []uint64
latestHeight := uint64(0)
k.IterateScheduledAxelarCorks(ctx, chainID, func(_ sdk.ValAddress, blockHeight uint64, _ []byte, _ common.Address, _ types.AxelarCork) (stop bool) {
if blockHeight > latestHeight {
heights = append(heights, blockHeight)
}
latestHeight = blockHeight
return false
})
return heights
}
////////////////////////
// AxelarCork Results //
////////////////////////
func (k Keeper) SetAxelarCorkResult(ctx sdk.Context, chainID uint64, id []byte, corkResult types.AxelarCorkResult) {
bz := k.cdc.MustMarshal(&corkResult)
ctx.KVStore(k.storeKey).Set(types.GetAxelarCorkResultKey(chainID, id), bz)
}
func (k Keeper) GetAxelarCorkResult(ctx sdk.Context, chainID uint64, id []byte) (types.AxelarCorkResult, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetAxelarCorkResultKey(chainID, id))
if len(bz) == 0 {
return types.AxelarCorkResult{}, false
}
var corkResult types.AxelarCorkResult
k.cdc.MustUnmarshal(bz, &corkResult)
return corkResult, true
}
func (k Keeper) DeleteAxelarCorkResult(ctx sdk.Context, chainID uint64, id []byte) {
ctx.KVStore(k.storeKey).Delete(types.GetAxelarCorkResultKey(chainID, id))
}
// IterateCorksResult iterates over all cork results by chain ID
func (k Keeper) IterateAxelarCorkResults(ctx sdk.Context, chainID uint64, cb func(id []byte, blockHeight uint64, approved bool, approvalPercentage string, corkResult types.AxelarCorkResult) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.GetAxelarCorkResultPrefix(chainID))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var corkResult types.AxelarCorkResult
keyPair := bytes.NewBuffer(iter.Key())
keyPair.Next(1) // trim prefix byte
id := keyPair.Next(32)
k.cdc.MustUnmarshal(iter.Value(), &corkResult)
if cb(id, corkResult.BlockHeight, corkResult.Approved, corkResult.ApprovalPercentage, corkResult) {
break
}
}
}
// GetAxelarCorkResults returns AxelarCorkResults
func (k Keeper) GetAxelarCorkResults(ctx sdk.Context, chainID uint64) []*types.AxelarCorkResult {
var corkResults []*types.AxelarCorkResult
k.IterateAxelarCorkResults(ctx, chainID, func(id []byte, blockHeight uint64, approved bool, approvalPercentage string, corkResult types.AxelarCorkResult) (stop bool) {
corkResults = append(corkResults, &corkResult)
return false
})
return corkResults
}
///////////
// Votes //
///////////
func (k Keeper) GetApprovedScheduledAxelarCorks(ctx sdk.Context, chainID uint64) (approvedCorks []types.AxelarCork) {
currentBlockHeight := uint64(ctx.BlockHeight())
totalPower := k.stakingKeeper.GetLastTotalPower(ctx)
corks := []types.AxelarCork{}
powers := []uint64{}
k.IterateScheduledAxelarCorksByBlockHeight(ctx, chainID, currentBlockHeight, func(val sdk.ValAddress, _ uint64, id []byte, addr common.Address, cork types.AxelarCork) (stop bool) {
validator := k.stakingKeeper.Validator(ctx, val)
validatorPower := uint64(validator.GetConsensusPower(k.stakingKeeper.PowerReduction(ctx)))
found := false
for i, c := range corks {
if c.Equals(cork) {
powers[i] += validatorPower
found = true
break
}
}
if !found {
corks = append(corks, cork)
powers = append(powers, validatorPower)
}
k.DeleteScheduledAxelarCork(ctx, chainID, currentBlockHeight, id, val, addr)
k.DecrementValidatorAxelarCorkCount(ctx, val)
return false
})
threshold := sdk.MustNewDecFromStr(CorkVoteThresholdStr)
for i, power := range powers {
cork := corks[i]
approvalPercentage := sdk.NewDecFromInt(sdk.NewIntFromUint64(power)).Quo(sdk.NewDecFromInt(totalPower))
quorumReached := approvalPercentage.GT(threshold)
corkResult := types.AxelarCorkResult{
Cork: &cork,
BlockHeight: currentBlockHeight,
Approved: quorumReached,
ApprovalPercentage: approvalPercentage.Mul(sdk.NewDec(100)).String(),
}
corkID := cork.IDHash(currentBlockHeight)
k.SetAxelarCorkResult(ctx, chainID, corkID, corkResult)
if quorumReached {
approvedCorks = append(approvedCorks, cork)
}
}
return approvedCorks
}
/////////////
// Cellars //
/////////////
func (k Keeper) SetCellarIDs(ctx sdk.Context, chainID uint64, c types.CellarIDSet) {
bz := k.cdc.MustMarshal(&c)
// always sort before writing to the store
cellarIDs := make([]string, 0, len(c.Ids))
cellarIDs = append(cellarIDs, c.Ids...)
sort.Strings(cellarIDs)
c.Ids = cellarIDs
ctx.KVStore(k.storeKey).Set(types.MakeCellarIDsKey(chainID), bz)
}
func (k Keeper) GetCellarIDs(ctx sdk.Context, chainID uint64) (cellars []common.Address) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.MakeCellarIDsKey(chainID))
var cids types.CellarIDSet
k.cdc.MustUnmarshal(bz, &cids)
for _, cid := range cids.Ids {
cellars = append(cellars, common.HexToAddress(cid))
}
return cellars
}
func (k Keeper) HasCellarID(ctx sdk.Context, chainID uint64, address common.Address) (found bool) {
found = false
for _, id := range k.GetCellarIDs(ctx, chainID) {
if id == address {
found = true
break
}
}
return found
}
/////////////////////////////////
// Axelar Contract Call Nonces //
/////////////////////////////////
// SetAxelarContractCallNonce sets the nonce for the given chainID and address
func (k Keeper) SetAxelarContractCallNonce(ctx sdk.Context, chainID uint64, address string, nonce uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetAxelarContractCallNonceKey(chainID, common.HexToAddress(address)), sdk.Uint64ToBigEndian(nonce))
}
// GetAxelarContractCallNonce returns the nonce for the given chainID and address, returning a zero if not found
func (k Keeper) GetAxelarContractCallNonce(ctx sdk.Context, chainID uint64, address string) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetAxelarContractCallNonceKey(chainID, common.HexToAddress(address)))
if len(bz) == 0 {
return 0
}
return sdk.BigEndianToUint64(bz)
}
// IncrementAxelarContractCallNonce increments the nonce for the given chainID and address
func (k Keeper) IncrementAxelarContractCallNonce(ctx sdk.Context, chainID uint64, address string) uint64 {
nonce := k.GetAxelarContractCallNonce(ctx, chainID, address) + 1
k.SetAxelarContractCallNonce(ctx, chainID, address, nonce)
return nonce
}
// IterateAxelarContractCallNonces iterates over all axelar contract call nonces
func (k Keeper) IterateAxelarContractCallNonces(ctx sdk.Context, cb func(chainID uint64, address common.Address, nonce uint64) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte{types.AxelarContractCallNoncePrefix})
defer iter.Close()
for ; iter.Valid(); iter.Next() {
keyPair := bytes.NewBuffer(iter.Key())
keyPair.Next(1) // trim prefix byte
chainID := sdk.BigEndianToUint64(keyPair.Next(8))
address := common.BytesToAddress(keyPair.Next(20))
nonce := sdk.BigEndianToUint64(iter.Value())
if cb(chainID, address, nonce) {
break
}
}
}
/////////////////////////
// Axelar Upgrade Data //
/////////////////////////
// SetAxelarProxyUpgradeData sets the upgrade data for the given chainID
func (k Keeper) SetAxelarProxyUpgradeData(ctx sdk.Context, chainID uint64, upgradeData types.AxelarUpgradeData) {
ud := k.cdc.MustMarshal(&upgradeData)
ctx.KVStore(k.storeKey).Set(types.GetAxelarProxyUpgradeDataKey(chainID), ud)
}
// GetAxelarProxyUpgradeData returns the upgrade data for the given chainID, returning an empty payload if not found
func (k Keeper) GetAxelarProxyUpgradeData(ctx sdk.Context, chainID uint64) (types.AxelarUpgradeData, bool) {
bz := ctx.KVStore(k.storeKey).Get(types.GetAxelarProxyUpgradeDataKey(chainID))
if len(bz) == 0 {
return types.AxelarUpgradeData{}, false
}
upgradeData := types.AxelarUpgradeData{}
k.cdc.MustUnmarshal(bz, &upgradeData)
return upgradeData, true
}
// DeleteAxelarProxyUpgradeData deletes the upgrade data for the given chainID
func (k Keeper) DeleteAxelarProxyUpgradeData(ctx sdk.Context, chainID uint64) {
ctx.KVStore(k.storeKey).Delete(types.GetAxelarProxyUpgradeDataKey(chainID))
}
// IterateAxelarProxyUpgradeData iterates over all axelar proxy upgrade data
func (k Keeper) IterateAxelarProxyUpgradeData(ctx sdk.Context, cb func(chainID uint64, upgradeData types.AxelarUpgradeData) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte{types.AxelarProxyUpgradeDataPrefix})
defer iter.Close()
for ; iter.Valid(); iter.Next() {
keyPair := bytes.NewBuffer(iter.Key())
keyPair.Next(1) // trim prefix byte
chainID := sdk.BigEndianToUint64(keyPair.Next(8))
upgradeData := types.AxelarUpgradeData{}
k.cdc.MustUnmarshal(iter.Value(), &upgradeData)
if cb(chainID, upgradeData) {
break
}
}
}
///////////////////////////
// Validator Cork counts //
///////////////////////////
func (k Keeper) GetValidatorAxelarCorkCount(ctx sdk.Context, val sdk.ValAddress) (count uint64) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetValidatorAxelarCorkCountKey(val))
if len(bz) == 0 {
return 0
}
return binary.BigEndian.Uint64(bz)
}
func (k Keeper) SetValidatorAxelarCorkCount(ctx sdk.Context, val sdk.ValAddress, count uint64) {
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
ctx.KVStore(k.storeKey).Set(types.GetValidatorAxelarCorkCountKey(val), bz)
}
func (k Keeper) IncrementValidatorAxelarCorkCount(ctx sdk.Context, val sdk.ValAddress) {
count := k.GetValidatorAxelarCorkCount(ctx, val)
k.SetValidatorAxelarCorkCount(ctx, val, count+1)
}
func (k Keeper) DecrementValidatorAxelarCorkCount(ctx sdk.Context, val sdk.ValAddress) {
count := k.GetValidatorAxelarCorkCount(ctx, val)
if count > 0 {
k.SetValidatorAxelarCorkCount(ctx, val, count-1)
}
}
/////////////////////
// Module Accounts //
/////////////////////
func (k Keeper) GetSenderAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
}
///////////////////////////
// ICS4Wrapper functions //
///////////////////////////
func (k Keeper) SendPacket(ctx sdk.Context, chanCap *capabilitytypes.Capability, sourcePort string, sourceChannel string, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, data []byte) (sequence uint64, err error) {
if err := k.ValidateAxelarPacket(ctx, sourceChannel, data); err != nil {
k.Logger(ctx).Error(fmt.Sprintf("ICS20 packet send was denied: %s", err.Error()))
// based on the default implementation of SendPacket in ibc-go, we return 0 for the sequence on error conditions
return 0, err
}
return k.Ics4Wrapper.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data)
}
func (k Keeper) WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet exported.PacketI, ack exported.Acknowledgement) error {
return k.Ics4Wrapper.WriteAcknowledgement(ctx, chanCap, packet, ack)
}
func (k Keeper) GetAppVersion(ctx sdk.Context, portID string, channelID string) (string, bool) {
return k.Ics4Wrapper.GetAppVersion(ctx, portID, channelID)
}