-
Notifications
You must be signed in to change notification settings - Fork 639
/
Copy pathkeeper.go
434 lines (358 loc) · 14.7 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
package keeper
import (
"context"
"cosmossdk.io/core/appmodule"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/v9/modules/apps/29-fee/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v9/modules/core/05-port/types"
coretypes "github.com/cosmos/ibc-go/v9/modules/core/types"
)
// Middleware must implement types.ChannelKeeper and types.PortKeeper expected interfaces
// so that it can wrap IBC channel and port logic for underlying application.
var _ types.ChannelKeeper = (*Keeper)(nil)
// Keeper defines the IBC fungible transfer keeper
type Keeper struct {
appmodule.Environment
cdc codec.BinaryCodec
authKeeper types.AuthKeeper
ics4Wrapper porttypes.ICS4Wrapper
channelKeeper types.ChannelKeeper
bankKeeper types.BankKeeper
}
// NewKeeper creates a new 29-fee Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, env appmodule.Environment,
ics4Wrapper porttypes.ICS4Wrapper, channelKeeper types.ChannelKeeper,
authKeeper types.AuthKeeper, bankKeeper types.BankKeeper,
) Keeper {
return Keeper{
cdc: cdc,
Environment: env,
ics4Wrapper: ics4Wrapper,
channelKeeper: channelKeeper,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
}
}
// WithICS4Wrapper sets the ICS4Wrapper. This function may be used after
// the keepers creation to set the middleware which is above this module
// in the IBC application stack.
func (k *Keeper) WithICS4Wrapper(wrapper porttypes.ICS4Wrapper) {
k.ics4Wrapper = wrapper
}
// GetICS4Wrapper returns the ICS4Wrapper.
func (k Keeper) GetICS4Wrapper() porttypes.ICS4Wrapper {
return k.ics4Wrapper
}
// GetChannel wraps IBC ChannelKeeper's GetChannel function
func (k Keeper) GetChannel(ctx context.Context, portID, channelID string) (channeltypes.Channel, bool) {
return k.channelKeeper.GetChannel(ctx, portID, channelID)
}
// HasChannel returns true if the channel with the given identifiers exists in state.
func (k Keeper) HasChannel(ctx context.Context, portID, channelID string) bool {
return k.channelKeeper.HasChannel(ctx, portID, channelID)
}
// GetPacketCommitment wraps IBC ChannelKeeper's GetPacketCommitment function
func (k Keeper) GetPacketCommitment(ctx context.Context, portID, channelID string, sequence uint64) []byte {
return k.channelKeeper.GetPacketCommitment(ctx, portID, channelID, sequence)
}
// GetNextSequenceSend wraps IBC ChannelKeeper's GetNextSequenceSend function
func (k Keeper) GetNextSequenceSend(ctx context.Context, portID, channelID string) (uint64, bool) {
return k.channelKeeper.GetNextSequenceSend(ctx, portID, channelID)
}
// GetFeeModuleAddress returns the ICS29 Fee ModuleAccount address
func (k Keeper) GetFeeModuleAddress() sdk.AccAddress {
return k.authKeeper.GetModuleAddress(types.ModuleName)
}
// EscrowAccountHasBalance verifies if the escrow account has the provided fee.
func (k Keeper) EscrowAccountHasBalance(ctx context.Context, coins sdk.Coins) bool {
for _, coin := range coins {
if !k.bankKeeper.HasBalance(ctx, k.GetFeeModuleAddress(), coin) {
return false
}
}
return true
}
// lockFeeModule sets a flag to determine if fee handling logic should run for the given channel
// identified by channel and port identifiers.
// Please see ADR 004 for more information.
func (k Keeper) lockFeeModule(ctx context.Context) {
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(types.KeyLocked(), []byte{1}); err != nil {
panic(err)
}
}
// IsLocked indicates if the fee module is locked
// Please see ADR 004 for more information.
func (k Keeper) IsLocked(ctx context.Context) bool {
store := k.KVStoreService.OpenKVStore(ctx)
has, err := store.Has(types.KeyLocked())
if err != nil {
panic(err)
}
return has
}
// SetFeeEnabled sets a flag to determine if fee handling logic should run for the given channel
// identified by channel and port identifiers.
func (k Keeper) SetFeeEnabled(ctx context.Context, portID, channelID string) {
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(types.KeyFeeEnabled(portID, channelID), []byte{1}); err != nil {
panic(err)
}
}
// DeleteFeeEnabled deletes the fee enabled flag for a given portID and channelID
func (k Keeper) DeleteFeeEnabled(ctx context.Context, portID, channelID string) {
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Delete(types.KeyFeeEnabled(portID, channelID)); err != nil {
panic(err)
}
}
// IsFeeEnabled returns whether fee handling logic should be run for the given port. It will check the
// fee enabled flag for the given port and channel identifiers
func (k Keeper) IsFeeEnabled(ctx context.Context, portID, channelID string) bool {
store := k.KVStoreService.OpenKVStore(ctx)
has, err := store.Has(types.KeyFeeEnabled(portID, channelID))
if err != nil {
panic(err)
}
return has
}
// GetAllFeeEnabledChannels returns a list of all ics29 enabled channels containing portID & channelID that are stored in state
func (k Keeper) GetAllFeeEnabledChannels(ctx context.Context) []types.FeeEnabledChannel {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(types.FeeEnabledKeyPrefix))
defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() })
var enabledChArr []types.FeeEnabledChannel
for ; iterator.Valid(); iterator.Next() {
portID, channelID, err := types.ParseKeyFeeEnabled(string(iterator.Key()))
if err != nil {
panic(err)
}
ch := types.FeeEnabledChannel{
PortId: portID,
ChannelId: channelID,
}
enabledChArr = append(enabledChArr, ch)
}
return enabledChArr
}
// GetPayeeAddress retrieves the fee payee address stored in state given the provided channel identifier and relayer address
func (k Keeper) GetPayeeAddress(ctx context.Context, relayerAddr, channelID string) (string, bool) {
store := k.KVStoreService.OpenKVStore(ctx)
key := types.KeyPayee(relayerAddr, channelID)
bz, err := store.Get(key)
if err != nil {
panic(err)
}
if len(bz) == 0 {
return "", false
}
return string(bz), true
}
// SetPayeeAddress stores the fee payee address in state keyed by the provided channel identifier and relayer address
func (k Keeper) SetPayeeAddress(ctx context.Context, relayerAddr, payeeAddr, channelID string) {
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(types.KeyPayee(relayerAddr, channelID), []byte(payeeAddr)); err != nil {
panic(err)
}
}
// GetAllPayees returns all registered payees addresses
func (k Keeper) GetAllPayees(ctx context.Context) []types.RegisteredPayee {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(types.PayeeKeyPrefix))
defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() })
var registeredPayees []types.RegisteredPayee
for ; iterator.Valid(); iterator.Next() {
relayerAddr, channelID, err := types.ParseKeyPayeeAddress(string(iterator.Key()))
if err != nil {
panic(err)
}
payee := types.RegisteredPayee{
Relayer: relayerAddr,
Payee: string(iterator.Value()),
ChannelId: channelID,
}
registeredPayees = append(registeredPayees, payee)
}
return registeredPayees
}
// SetCounterpartyPayeeAddress maps the destination chain counterparty payee address to the source relayer address
// The receiving chain must store the mapping from: address -> counterpartyPayeeAddress for the given channel
func (k Keeper) SetCounterpartyPayeeAddress(ctx context.Context, address, counterpartyAddress, channelID string) {
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(types.KeyCounterpartyPayee(address, channelID), []byte(counterpartyAddress)); err != nil {
panic(err)
}
}
// GetCounterpartyPayeeAddress gets the counterparty payee address given a destination relayer address
func (k Keeper) GetCounterpartyPayeeAddress(ctx context.Context, address, channelID string) (string, bool) {
store := k.KVStoreService.OpenKVStore(ctx)
key := types.KeyCounterpartyPayee(address, channelID)
addr, err := store.Get(key)
if err != nil {
panic(err)
}
if len(addr) == 0 {
return "", false
}
return string(addr), true
}
// GetAllCounterpartyPayees returns all registered counterparty payee addresses
func (k Keeper) GetAllCounterpartyPayees(ctx context.Context) []types.RegisteredCounterpartyPayee {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(types.CounterpartyPayeeKeyPrefix))
defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() })
var registeredCounterpartyPayees []types.RegisteredCounterpartyPayee
for ; iterator.Valid(); iterator.Next() {
relayerAddr, channelID, err := types.ParseKeyCounterpartyPayee(string(iterator.Key()))
if err != nil {
panic(err)
}
counterpartyPayee := types.RegisteredCounterpartyPayee{
Relayer: relayerAddr,
CounterpartyPayee: string(iterator.Value()),
ChannelId: channelID,
}
registeredCounterpartyPayees = append(registeredCounterpartyPayees, counterpartyPayee)
}
return registeredCounterpartyPayees
}
// SetRelayerAddressForAsyncAck sets the forward relayer address during OnRecvPacket in case of async acknowledgement
func (k Keeper) SetRelayerAddressForAsyncAck(ctx context.Context, packetID channeltypes.PacketId, address string) {
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(types.KeyRelayerAddressForAsyncAck(packetID), []byte(address)); err != nil {
panic(err)
}
}
// GetRelayerAddressForAsyncAck gets forward relayer address for a particular packet
func (k Keeper) GetRelayerAddressForAsyncAck(ctx context.Context, packetID channeltypes.PacketId) (string, bool) {
store := k.KVStoreService.OpenKVStore(ctx)
key := types.KeyRelayerAddressForAsyncAck(packetID)
addr, err := store.Get(key)
if err != nil {
panic(err)
}
if len(addr) == 0 {
return "", false
}
return string(addr), true
}
// GetAllForwardRelayerAddresses returns all forward relayer addresses stored for async acknowledgements
func (k Keeper) GetAllForwardRelayerAddresses(ctx context.Context) []types.ForwardRelayerAddress {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(types.ForwardRelayerPrefix))
defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() })
var forwardRelayerAddr []types.ForwardRelayerAddress
for ; iterator.Valid(); iterator.Next() {
packetID, err := types.ParseKeyRelayerAddressForAsyncAck(string(iterator.Key()))
if err != nil {
panic(err)
}
addr := types.ForwardRelayerAddress{
Address: string(iterator.Value()),
PacketId: packetID,
}
forwardRelayerAddr = append(forwardRelayerAddr, addr)
}
return forwardRelayerAddr
}
// DeleteForwardRelayerAddress deletes the forwardRelayerAddr associated with the packetID
func (k Keeper) DeleteForwardRelayerAddress(ctx context.Context, packetID channeltypes.PacketId) {
store := k.KVStoreService.OpenKVStore(ctx)
key := types.KeyRelayerAddressForAsyncAck(packetID)
if err := store.Delete(key); err != nil {
panic(err)
}
}
// GetFeesInEscrow returns all escrowed packet fees for a given packetID
func (k Keeper) GetFeesInEscrow(ctx context.Context, packetID channeltypes.PacketId) (types.PacketFees, bool) {
store := k.KVStoreService.OpenKVStore(ctx)
key := types.KeyFeesInEscrow(packetID)
bz, err := store.Get(key)
if err != nil {
panic(err)
}
if len(bz) == 0 {
return types.PacketFees{}, false
}
return k.MustUnmarshalFees(bz), true
}
// HasFeesInEscrow returns true if packet fees exist for the provided packetID
func (k Keeper) HasFeesInEscrow(ctx context.Context, packetID channeltypes.PacketId) bool {
store := k.KVStoreService.OpenKVStore(ctx)
key := types.KeyFeesInEscrow(packetID)
has, err := store.Has(key)
if err != nil {
panic(err)
}
return has
}
// SetFeesInEscrow sets the given packet fees in escrow keyed by the packetID
func (k Keeper) SetFeesInEscrow(ctx context.Context, packetID channeltypes.PacketId, fees types.PacketFees) {
store := k.KVStoreService.OpenKVStore(ctx)
bz := k.MustMarshalFees(fees)
if err := store.Set(types.KeyFeesInEscrow(packetID), bz); err != nil {
panic(err)
}
}
// DeleteFeesInEscrow deletes the fee associated with the given packetID
func (k Keeper) DeleteFeesInEscrow(ctx context.Context, packetID channeltypes.PacketId) {
store := k.KVStoreService.OpenKVStore(ctx)
key := types.KeyFeesInEscrow(packetID)
if err := store.Delete(key); err != nil {
panic(err)
}
}
// GetIdentifiedPacketFeesForChannel returns all the currently escrowed fees on a given channel.
func (k Keeper) GetIdentifiedPacketFeesForChannel(ctx context.Context, portID, channelID string) []types.IdentifiedPacketFees {
var identifiedPacketFees []types.IdentifiedPacketFees
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, types.KeyFeesInEscrowChannelPrefix(portID, channelID))
defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() })
for ; iterator.Valid(); iterator.Next() {
packetID, err := types.ParseKeyFeesInEscrow(string(iterator.Key()))
if err != nil {
panic(err)
}
packetFees := k.MustUnmarshalFees(iterator.Value())
identifiedFee := types.NewIdentifiedPacketFees(packetID, packetFees.PacketFees)
identifiedPacketFees = append(identifiedPacketFees, identifiedFee)
}
return identifiedPacketFees
}
// GetAllIdentifiedPacketFees returns a list of all IdentifiedPacketFees that are stored in state
func (k Keeper) GetAllIdentifiedPacketFees(ctx context.Context) []types.IdentifiedPacketFees {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(types.FeesInEscrowPrefix))
defer coretypes.LogDeferred(k.Logger, func() error { return iterator.Close() })
var identifiedFees []types.IdentifiedPacketFees
for ; iterator.Valid(); iterator.Next() {
packetID, err := types.ParseKeyFeesInEscrow(string(iterator.Key()))
if err != nil {
panic(err)
}
feesInEscrow := k.MustUnmarshalFees(iterator.Value())
identifiedFee := types.IdentifiedPacketFees{
PacketId: packetID,
PacketFees: feesInEscrow.PacketFees,
}
identifiedFees = append(identifiedFees, identifiedFee)
}
return identifiedFees
}
// MustMarshalFees attempts to encode a Fee object and returns the
// raw encoded bytes. It panics on error.
func (k Keeper) MustMarshalFees(fees types.PacketFees) []byte {
return k.cdc.MustMarshal(&fees)
}
// MustUnmarshalFees attempts to decode and return a Fee object from
// raw encoded bytes. It panics on error.
func (k Keeper) MustUnmarshalFees(bz []byte) types.PacketFees {
var fees types.PacketFees
k.cdc.MustUnmarshal(bz, &fees)
return fees
}