-
Notifications
You must be signed in to change notification settings - Fork 636
/
keeper.go
382 lines (312 loc) · 13.2 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
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/ibc-go/v4/modules/apps/29-fee/types"
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v4/modules/core/24-host"
)
// 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{}
_ types.PortKeeper = Keeper{}
)
// Keeper defines the IBC fungible transfer keeper
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
authKeeper types.AccountKeeper
ics4Wrapper types.ICS4Wrapper
channelKeeper types.ChannelKeeper
portKeeper types.PortKeeper
bankKeeper types.BankKeeper
}
// NewKeeper creates a new 29-fee Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
ics4Wrapper types.ICS4Wrapper, channelKeeper types.ChannelKeeper, portKeeper types.PortKeeper, authKeeper types.AccountKeeper, bankKeeper types.BankKeeper,
) Keeper {
return Keeper{
cdc: cdc,
storeKey: key,
ics4Wrapper: ics4Wrapper,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+host.ModuleName+"-"+types.ModuleName)
}
// 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) *capabilitytypes.Capability {
return k.portKeeper.BindPort(ctx, portID)
}
// GetChannel wraps IBC ChannelKeeper's GetChannel function
func (k Keeper) GetChannel(ctx sdk.Context, portID, channelID string) (channeltypes.Channel, bool) {
return k.channelKeeper.GetChannel(ctx, portID, channelID)
}
// GetPacketCommitment wraps IBC ChannelKeeper's GetPacketCommitment function
func (k Keeper) GetPacketCommitment(ctx sdk.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 sdk.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 sdk.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 sdk.Context) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyLocked(), []byte{1})
}
// IsLocked indicates if the fee module is locked
// Please see ADR 004 for more information.
func (k Keeper) IsLocked(ctx sdk.Context) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.KeyLocked())
}
// 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 sdk.Context, portID, channelID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyFeeEnabled(portID, channelID), []byte{1})
}
// DeleteFeeEnabled deletes the fee enabled flag for a given portID and channelID
func (k Keeper) DeleteFeeEnabled(ctx sdk.Context, portID, channelID string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.KeyFeeEnabled(portID, channelID))
}
// 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 sdk.Context, portID, channelID string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.KeyFeeEnabled(portID, channelID))
}
// GetAllFeeEnabledChannels returns a list of all ics29 enabled channels containing portID & channelID that are stored in state
func (k Keeper) GetAllFeeEnabledChannels(ctx sdk.Context) []types.FeeEnabledChannel {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.FeeEnabledKeyPrefix))
defer 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 sdk.Context, relayerAddr, channelID string) (string, bool) {
store := ctx.KVStore(k.storeKey)
key := types.KeyPayee(relayerAddr, channelID)
if !store.Has(key) {
return "", false
}
return string(store.Get(key)), true
}
// SetPayeeAddress stores the fee payee address in state keyed by the provided channel identifier and relayer address
func (k Keeper) SetPayeeAddress(ctx sdk.Context, relayerAddr, payeeAddr, channelID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyPayee(relayerAddr, channelID), []byte(payeeAddr))
}
// GetAllPayees returns all registered payees addresses
func (k Keeper) GetAllPayees(ctx sdk.Context) []types.RegisteredPayee {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.PayeeKeyPrefix))
defer 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 sdk.Context, address, counterpartyAddress, channelID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyCounterpartyPayee(address, channelID), []byte(counterpartyAddress))
}
// GetCounterpartyPayeeAddress gets the counterparty payee address given a destination relayer address
func (k Keeper) GetCounterpartyPayeeAddress(ctx sdk.Context, address, channelID string) (string, bool) {
store := ctx.KVStore(k.storeKey)
key := types.KeyCounterpartyPayee(address, channelID)
if !store.Has(key) {
return "", false
}
addr := string(store.Get(key))
return addr, true
}
// GetAllCounterpartyPayees returns all registered counterparty payee addresses
func (k Keeper) GetAllCounterpartyPayees(ctx sdk.Context) []types.RegisteredCounterpartyPayee {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.CounterpartyPayeeKeyPrefix))
defer 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 sdk.Context, packetID channeltypes.PacketId, address string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyRelayerAddressForAsyncAck(packetID), []byte(address))
}
// GetRelayerAddressForAsyncAck gets forward relayer address for a particular packet
func (k Keeper) GetRelayerAddressForAsyncAck(ctx sdk.Context, packetID channeltypes.PacketId) (string, bool) {
store := ctx.KVStore(k.storeKey)
key := types.KeyRelayerAddressForAsyncAck(packetID)
if !store.Has(key) {
return "", false
}
addr := string(store.Get(key))
return addr, true
}
// GetAllForwardRelayerAddresses returns all forward relayer addresses stored for async acknowledgements
func (k Keeper) GetAllForwardRelayerAddresses(ctx sdk.Context) []types.ForwardRelayerAddress {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.ForwardRelayerPrefix))
defer 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
}
// Deletes the forwardRelayerAddr associated with the packetID
func (k Keeper) DeleteForwardRelayerAddress(ctx sdk.Context, packetID channeltypes.PacketId) {
store := ctx.KVStore(k.storeKey)
key := types.KeyRelayerAddressForAsyncAck(packetID)
store.Delete(key)
}
// GetFeesInEscrow returns all escrowed packet fees for a given packetID
func (k Keeper) GetFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) (types.PacketFees, bool) {
store := ctx.KVStore(k.storeKey)
key := types.KeyFeesInEscrow(packetID)
bz := store.Get(key)
if bz == nil {
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 sdk.Context, packetID channeltypes.PacketId) bool {
store := ctx.KVStore(k.storeKey)
key := types.KeyFeesInEscrow(packetID)
return store.Has(key)
}
// SetFeesInEscrow sets the given packet fees in escrow keyed by the packetID
func (k Keeper) SetFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId, fees types.PacketFees) {
store := ctx.KVStore(k.storeKey)
bz := k.MustMarshalFees(fees)
store.Set(types.KeyFeesInEscrow(packetID), bz)
}
// DeleteFeesInEscrow deletes the fee associated with the given packetID
func (k Keeper) DeleteFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) {
store := ctx.KVStore(k.storeKey)
key := types.KeyFeesInEscrow(packetID)
store.Delete(key)
}
// GetIdentifiedPacketFeesForChannel returns all the currently escrowed fees on a given channel.
func (k Keeper) GetIdentifiedPacketFeesForChannel(ctx sdk.Context, portID, channelID string) []types.IdentifiedPacketFees {
var identifiedPacketFees []types.IdentifiedPacketFees
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyFeesInEscrowChannelPrefix(portID, channelID))
defer 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 sdk.Context) []types.IdentifiedPacketFees {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.FeesInEscrowPrefix))
defer 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
}