-
Notifications
You must be signed in to change notification settings - Fork 657
/
Copy pathkeeper.go
309 lines (262 loc) · 9.72 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
package keeper
import (
"context"
"errors"
"fmt"
"strings"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/log"
sdkmath "cosmossdk.io/math"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
cmtbytes "github.com/cometbft/cometbft/libs/bytes"
"github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
porttypes "github.com/cosmos/ibc-go/v9/modules/core/05-port/types"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)
// Keeper defines the IBC fungible transfer keeper
type Keeper struct {
storeService corestore.KVStoreService
cdc codec.BinaryCodec
legacySubspace types.ParamSubspace
ics4Wrapper porttypes.ICS4Wrapper
channelKeeper types.ChannelKeeper
AuthKeeper types.AccountKeeper
BankKeeper types.BankKeeper
// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string
}
// NewKeeper creates a new IBC transfer Keeper instance
func NewKeeper(
cdc codec.BinaryCodec,
storeService corestore.KVStoreService,
legacySubspace types.ParamSubspace,
ics4Wrapper porttypes.ICS4Wrapper,
channelKeeper types.ChannelKeeper,
authKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
authority string,
) Keeper {
// ensure ibc transfer module account is set
if addr := authKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(errors.New("the IBC transfer module account has not been set"))
}
if strings.TrimSpace(authority) == "" {
panic(errors.New("authority must be non-empty"))
}
return Keeper{
cdc: cdc,
storeService: storeService,
legacySubspace: legacySubspace,
ics4Wrapper: ics4Wrapper,
channelKeeper: channelKeeper,
AuthKeeper: authKeeper,
BankKeeper: bankKeeper,
authority: authority,
}
}
// 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
}
// GetAuthority returns the transfer module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
}
// Logger returns a module-specific logger.
func (Keeper) Logger(ctx context.Context) log.Logger {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return sdkCtx.Logger().With("module", "x/"+exported.ModuleName+"-"+types.ModuleName)
}
// GetPort returns the portID for the transfer module. Used in ExportGenesis
func (k Keeper) GetPort(ctx context.Context) string {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.PortKey)
if err != nil {
panic(err)
}
return string(bz)
}
// SetPort sets the portID for the transfer module. Used in InitGenesis
func (k Keeper) SetPort(ctx context.Context, portID string) {
store := k.storeService.OpenKVStore(ctx)
if err := store.Set(types.PortKey, []byte(portID)); err != nil {
panic(err)
}
}
// GetParams returns the current transfer module parameters.
func (k Keeper) GetParams(ctx context.Context) types.Params {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get([]byte(types.ParamsKey))
if err != nil {
panic(err)
}
if bz == nil { // only panic on unset params and not on empty params
panic(errors.New("transfer params are not set in store"))
}
var params types.Params
k.cdc.MustUnmarshal(bz, ¶ms)
return params
}
// SetParams sets the transfer module parameters.
func (k Keeper) SetParams(ctx context.Context, params types.Params) {
store := k.storeService.OpenKVStore(ctx)
bz := k.cdc.MustMarshal(¶ms)
if err := store.Set([]byte(types.ParamsKey), bz); err != nil {
panic(err)
}
}
// GetDenom retrieves the denom from store given the hash of the denom.
func (k Keeper) GetDenom(ctx context.Context, denomHash cmtbytes.HexBytes) (types.Denom, bool) {
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.DenomKey)
bz := store.Get(denomHash)
if len(bz) == 0 {
return types.Denom{}, false
}
var denom types.Denom
k.cdc.MustUnmarshal(bz, &denom)
return denom, true
}
// HasDenom checks if a the key with the given denomination hash exists on the store.
func (k Keeper) HasDenom(ctx context.Context, denomHash cmtbytes.HexBytes) bool {
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.DenomKey)
return store.Has(denomHash)
}
// SetDenom sets a new {denom hash -> denom } pair to the store.
// This allows for reverse lookup of the denom given the hash.
func (k Keeper) SetDenom(ctx context.Context, denom types.Denom) {
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.DenomKey)
bz := k.cdc.MustMarshal(&denom)
store.Set(denom.Hash(), bz)
}
// GetAllDenoms returns all the denominations.
func (k Keeper) GetAllDenoms(ctx context.Context) types.Denoms {
denoms := types.Denoms{}
k.IterateDenoms(ctx, func(denom types.Denom) bool {
denoms = append(denoms, denom)
return false
})
return denoms.Sort()
}
// IterateDenoms iterates over the denominations in the store and performs a callback function.
func (k Keeper) IterateDenoms(ctx context.Context, cb func(denom types.Denom) bool) {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, types.DenomKey)
defer sdk.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() })
for ; iterator.Valid(); iterator.Next() {
var denom types.Denom
k.cdc.MustUnmarshal(iterator.Value(), &denom)
if cb(denom) {
break
}
}
}
// SetDenomMetadata sets an IBC token's denomination metadata
func (k Keeper) SetDenomMetadata(ctx context.Context, denom types.Denom) {
metadata := banktypes.Metadata{
Description: fmt.Sprintf("IBC token from %s", denom.Path()),
DenomUnits: []*banktypes.DenomUnit{
{
Denom: denom.Base,
Exponent: 0,
},
},
// Setting base as IBC hash denom since bank keepers's SetDenomMetadata uses
// Base as key path and the IBC hash is what gives this token uniqueness
// on the executing chain
Base: denom.IBCDenom(),
Display: denom.Path(),
Name: fmt.Sprintf("%s IBC token", denom.Path()),
Symbol: strings.ToUpper(denom.Base),
}
k.BankKeeper.SetDenomMetaData(ctx, metadata)
}
// GetTotalEscrowForDenom gets the total amount of source chain tokens that
// are in escrow, keyed by the denomination.
//
// NOTE: if there is no value stored in state for the provided denom then a new Coin is returned for the denom with an initial value of zero.
// This accommodates callers to simply call `Add()` on the returned Coin as an empty Coin literal (e.g. sdk.Coin{}) will trigger a panic due to the absence of a denom.
func (k Keeper) GetTotalEscrowForDenom(ctx context.Context, denom string) sdk.Coin {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.TotalEscrowForDenomKey(denom))
if err != nil {
panic(err)
}
if len(bz) == 0 {
return sdk.NewCoin(denom, sdkmath.ZeroInt())
}
amount := sdk.IntProto{}
k.cdc.MustUnmarshal(bz, &amount)
return sdk.NewCoin(denom, amount.Int)
}
// SetTotalEscrowForDenom stores the total amount of source chain tokens that are in escrow.
// Amount is stored in state if and only if it is not equal to zero. The function will panic
// if the amount is negative.
func (k Keeper) SetTotalEscrowForDenom(ctx context.Context, coin sdk.Coin) {
if coin.Amount.IsNegative() {
panic(fmt.Errorf("amount cannot be negative: %s", coin.Amount))
}
store := k.storeService.OpenKVStore(ctx)
key := types.TotalEscrowForDenomKey(coin.Denom)
if coin.Amount.IsZero() {
if err := store.Delete(key); err != nil { // delete the key since Cosmos SDK x/bank module will prune any non-zero balances
panic(err)
}
return
}
bz := k.cdc.MustMarshal(&sdk.IntProto{Int: coin.Amount})
if err := store.Set(key, bz); err != nil {
panic(err)
}
}
// GetAllTotalEscrowed returns the escrow information for all the denominations.
func (k Keeper) GetAllTotalEscrowed(ctx context.Context) sdk.Coins {
var escrows sdk.Coins
k.IterateTokensInEscrow(ctx, []byte(types.KeyTotalEscrowPrefix), func(denomEscrow sdk.Coin) bool {
escrows = escrows.Add(denomEscrow)
return false
})
return escrows
}
// IterateTokensInEscrow iterates over the denomination escrows in the store
// and performs a callback function. Denominations for which an invalid value
// (i.e. not integer) is stored, will be skipped.
func (k Keeper) IterateTokensInEscrow(ctx context.Context, storeprefix []byte, cb func(denomEscrow sdk.Coin) bool) {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, storeprefix)
defer sdk.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() })
for ; iterator.Valid(); iterator.Next() {
denom := strings.TrimPrefix(string(iterator.Key()), fmt.Sprintf("%s/", types.KeyTotalEscrowPrefix))
if strings.TrimSpace(denom) == "" {
continue // denom is empty
}
amount := sdk.IntProto{}
if err := k.cdc.Unmarshal(iterator.Value(), &amount); err != nil {
continue // total escrow amount cannot be unmarshalled to integer
}
denomEscrow := sdk.NewCoin(denom, amount.Int)
if cb(denomEscrow) {
break
}
}
}
// IsBlockedAddr checks if the given address is allowed to send or receive tokens.
// The module account is always allowed to send and receive tokens.
func (k Keeper) IsBlockedAddr(addr sdk.AccAddress) bool {
moduleAddr := k.AuthKeeper.GetModuleAddress(types.ModuleName)
if addr.Equals(moduleAddr) {
return false
}
return k.BankKeeper.BlockedAddr(addr)
}