-
Notifications
You must be signed in to change notification settings - Fork 12
/
pool.go
392 lines (342 loc) · 14.5 KB
/
pool.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
package keeper
import (
"encoding/binary"
"fmt"
"sort"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
fxtypes "github.com/functionx/fx-core/v4/types"
"github.com/functionx/fx-core/v4/x/crosschain/types"
)
// AddToOutgoingPool
// - checks a counterpart denominator exists for the given voucher type
// - burns the voucher for transfer amount and fees
// - persists an OutgoingTx
// - adds the TX to the `available` TX pool via a second index
func (k Keeper) AddToOutgoingPool(ctx sdk.Context, sender sdk.AccAddress, receiver string, amount sdk.Coin, fee sdk.Coin) (uint64, error) {
bridgeToken := k.GetDenomBridgeToken(ctx, amount.Denom)
if bridgeToken == nil {
return 0, errorsmod.Wrap(types.ErrInvalid, "bridge token is not exist")
}
totalInVouchers := sdk.NewCoins(amount.Add(fee))
// If the coin is a gravity voucher, burn the coins. If not, check if there is a deployed ERC20 contract representing it.
// If there is, lock the coins.
isOriginDenom := k.erc20Keeper.IsOriginDenom(ctx, amount.Denom)
if isOriginDenom {
// lock coins in module
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, k.moduleName, totalInVouchers); err != nil {
return 0, err
}
} else {
// If it is an external blockchain asset we burn it send coins to module in prep for burn
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, k.moduleName, totalInVouchers); err != nil {
return 0, err
}
// burn vouchers to send them back to external blockchain
if err := k.bankKeeper.BurnCoins(ctx, k.moduleName, totalInVouchers); err != nil {
return 0, err
}
}
// get next tx id from keeper
nextTxID := k.autoIncrementID(ctx, types.KeyLastTxPoolID)
// construct outgoing tx, as part of this process we represent
// the token as an ERC20 token since it is preparing to go to ETH
// rather than the denom that is the input to this function.
outgoing := &types.OutgoingTransferTx{
Id: nextTxID,
Sender: sender.String(),
DestAddress: receiver,
Token: types.NewERC20Token(amount.Amount, bridgeToken.Token),
Fee: types.NewERC20Token(fee.Amount, bridgeToken.Token),
}
if err := k.AddUnbatchedTx(ctx, outgoing); err != nil {
return 0, err
}
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeSendToExternal,
sdk.NewAttribute(sdk.AttributeKeyModule, k.moduleName),
sdk.NewAttribute(types.AttributeKeyOutgoingTxID, fmt.Sprint(nextTxID)),
))
return nextTxID, nil
}
// RemoveFromOutgoingPoolAndRefund
// - checks that the provided tx actually exists
// - deletes the unbatched tx from the pool
// - issues the tokens back to the sender
//
//gocyclo:ignore
func (k Keeper) RemoveFromOutgoingPoolAndRefund(ctx sdk.Context, txId uint64, sender sdk.AccAddress) (sdk.Coin, error) {
if ctx.IsZero() || txId < 1 || sender.Empty() {
return sdk.Coin{}, errorsmod.Wrap(types.ErrInvalid, "arguments")
}
// check that we actually have a tx with that id and what it's details are
tx, err := k.GetUnbatchedTxById(ctx, txId)
if err != nil {
return sdk.Coin{}, err
}
// Check that this user actually sent the transaction, this prevents someone from refunding someone
// else transaction to themselves.
txSender := sdk.MustAccAddressFromBech32(tx.Sender)
if !txSender.Equals(sender) {
return sdk.Coin{}, errorsmod.Wrapf(types.ErrInvalid, "Sender %s did not send Id %d", sender, txId)
}
// An inconsistent entry should never enter the store, but this is the ideal place to exploit
// it such a bug if it did ever occur, so we should double check to be really sure
if tx.Fee.Contract != tx.Token.Contract {
return sdk.Coin{}, errorsmod.Wrapf(types.ErrInvalid, "Inconsistent tokens to cancel!: %s %s", tx.Fee.Contract, tx.Token.Contract)
}
// delete this tx from the pool
if err = k.removeUnbatchedTx(ctx, tx.Fee, txId); err != nil {
return sdk.Coin{}, errorsmod.Wrapf(types.ErrInvalid, "txId %d not in unbatched index! Must be in a batch!", txId)
}
// Make sure the tx was removed
oldTx, oldTxErr := k.GetUnbatchedTxByFeeAndId(ctx, tx.Fee, tx.Id)
if oldTx != nil || oldTxErr == nil {
return sdk.Coin{}, errorsmod.Wrapf(types.ErrInvalid, "tx with id %d was not fully removed from the pool, a duplicate must exist", txId)
}
// query denom, if not exist, return error
bridgeToken := k.GetBridgeTokenDenom(ctx, tx.Token.Contract)
if bridgeToken == nil {
return sdk.Coin{}, errorsmod.Wrapf(types.ErrInvalid, "Invalid token, contract %s", tx.Token.Contract)
}
// reissue the amount and the fee
totalToRefund := sdk.NewCoin(bridgeToken.Denom, tx.Token.Amount)
totalToRefund.Amount = totalToRefund.Amount.Add(tx.Fee.Amount)
totalToRefundCoins := sdk.NewCoins(totalToRefund)
if bridgeToken.Denom == fxtypes.DefaultDenom {
if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, k.moduleName, sender, totalToRefundCoins); err != nil {
return sdk.Coin{}, err
}
} else {
if err = k.bankKeeper.MintCoins(ctx, k.moduleName, totalToRefundCoins); err != nil {
return sdk.Coin{}, errorsmod.Wrapf(err, "mint vouchers coins: %s", totalToRefundCoins)
}
if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, k.moduleName, sender, totalToRefundCoins); err != nil {
return sdk.Coin{}, errorsmod.Wrap(err, "transfer vouchers")
}
}
targetCoin, err := k.erc20Keeper.ConvertDenomToTarget(ctx, sender, totalToRefund, fxtypes.ParseFxTarget(fxtypes.ERC20Target))
if err != nil {
return sdk.Coin{}, errorsmod.Wrap(err, "convert denom to erc20")
}
// if not origin token, refund to contract token
if k.erc20Keeper.HasOutgoingTransferRelation(ctx, k.moduleName, txId) {
if err = k.erc20Keeper.HookOutgoingRefund(ctx, k.moduleName, txId, sender, targetCoin); err != nil {
return sdk.Coin{}, errorsmod.Wrap(err, "outgoing refund")
}
}
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeSendToExternalCanceled,
sdk.NewAttribute(sdk.AttributeKeyModule, k.moduleName),
sdk.NewAttribute(types.AttributeKeyOutgoingTxID, fmt.Sprint(txId)),
))
return targetCoin, nil
}
func (k Keeper) AddUnbatchedTxBridgeFee(ctx sdk.Context, txId uint64, sender sdk.AccAddress, addBridgeFee sdk.Coin) error {
if ctx.IsZero() || txId < 1 || sender.Empty() || addBridgeFee.IsZero() {
return errorsmod.Wrap(types.ErrInvalid, "arguments")
}
// check that we actually have a tx with that id and what it's details are
tx, err := k.GetUnbatchedTxById(ctx, txId)
if err != nil {
return errorsmod.Wrapf(types.ErrInvalid, "txId %d not in unbatched index! Must be in a batch!", txId)
}
bridgeToken := k.GetDenomBridgeToken(ctx, addBridgeFee.Denom)
if bridgeToken == nil {
return errorsmod.Wrap(types.ErrInvalid, "bridge token is not exist")
}
if tx.Fee.Contract != bridgeToken.Token {
return errorsmod.Wrap(types.ErrInvalid, "token not equal tx fee token")
}
// If the coin is a gravity voucher, burn the coins. If not, check if there is a deployed ERC20 contract representing it.
// If there is, lock the coins.
if addBridgeFee.Denom == fxtypes.DefaultDenom {
// lock coins in module
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, k.moduleName, sdk.NewCoins(addBridgeFee)); err != nil {
return err
}
} else {
// If it is an external blockchain asset we burn it send coins to module in prep for burn
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, k.moduleName, sdk.NewCoins(addBridgeFee)); err != nil {
return err
}
// burn vouchers to send them back to external blockchain
if err := k.bankKeeper.BurnCoins(ctx, k.moduleName, sdk.NewCoins(addBridgeFee)); err != nil {
return err
}
}
if err := k.removeUnbatchedTx(ctx, tx.Fee, txId); err != nil {
return err
}
// add bridge fee amount
tx.Fee.Amount = tx.Fee.Amount.Add(addBridgeFee.Amount)
if err := k.AddUnbatchedTx(ctx, tx); err != nil {
return err
}
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeIncreaseBridgeFee,
sdk.NewAttribute(sdk.AttributeKeyModule, k.moduleName),
sdk.NewAttribute(types.AttributeKeyOutgoingTxID, fmt.Sprint(tx.Id)),
sdk.NewAttribute(types.AttributeKeyIncreaseFee, addBridgeFee.String()),
))
return nil
}
// AddUnbatchedTx creates a new transaction in the pool
func (k Keeper) AddUnbatchedTx(ctx sdk.Context, outgoingTransferTx *types.OutgoingTransferTx) error {
store := ctx.KVStore(k.storeKey)
idxKey := types.GetOutgoingTxPoolKey(outgoingTransferTx.Fee, outgoingTransferTx.Id)
if store.Has(idxKey) {
return errorsmod.Wrap(types.ErrDuplicate, "transaction already in pool")
}
store.Set(idxKey, k.cdc.MustMarshal(outgoingTransferTx))
return nil
}
// removeUnbatchedTXIndex removes the tx from the pool
func (k Keeper) removeUnbatchedTx(ctx sdk.Context, fee types.ERC20Token, txID uint64) error {
store := ctx.KVStore(k.storeKey)
idxKey := types.GetOutgoingTxPoolKey(fee, txID)
if !store.Has(idxKey) {
return errorsmod.Wrap(types.ErrUnknown, "pool transaction")
}
store.Delete(idxKey)
return nil
}
// GetUnbatchedTxByFeeAndId grabs a tx from the pool given its fee and txID
func (k Keeper) GetUnbatchedTxByFeeAndId(ctx sdk.Context, fee types.ERC20Token, txID uint64) (*types.OutgoingTransferTx, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetOutgoingTxPoolKey(fee, txID))
if bz == nil {
return nil, errorsmod.Wrap(types.ErrUnknown, "pool transaction")
}
var r types.OutgoingTransferTx
err := k.cdc.Unmarshal(bz, &r)
return &r, err
}
// GetUnbatchedTxById grabs a tx from the pool given only the txID
// note that due to the way unbatched txs are indexed, the GetUnbatchedTxByFeeAndId method is much faster
func (k Keeper) GetUnbatchedTxById(ctx sdk.Context, txID uint64) (*types.OutgoingTransferTx, error) {
var r *types.OutgoingTransferTx = nil
k.IterateUnbatchedTransactions(ctx, "", func(tx *types.OutgoingTransferTx) bool {
if tx.Id == txID {
r = tx
return true
}
return false
})
if r == nil {
// We have no return tx, it was either batched or never existed
return nil, errorsmod.Wrap(types.ErrUnknown, "pool transaction")
}
return r, nil
}
// GetUnbatchedTransactions used in testing
func (k Keeper) GetUnbatchedTransactions(ctx sdk.Context) []*types.OutgoingTransferTx {
var txs []*types.OutgoingTransferTx
k.IterateUnbatchedTransactions(ctx, "", func(tx *types.OutgoingTransferTx) bool {
txs = append(txs, tx)
return false
})
return txs
}
// IterateUnbatchedTransactions iterates through all unbatched transactions
func (k Keeper) IterateUnbatchedTransactions(ctx sdk.Context, tokenContract string, cb func(tx *types.OutgoingTransferTx) bool) {
store := ctx.KVStore(k.storeKey)
prefixKey := types.GetOutgoingTxPoolContractPrefix(tokenContract)
iter := sdk.KVStoreReversePrefixIterator(store, prefixKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var transact types.OutgoingTransferTx
k.cdc.MustUnmarshal(iter.Value(), &transact)
// cb returns true to stop early
if cb(&transact) {
break
}
}
}
// GetBatchFeesByTokenType gets the fee the next batch of a given token type would
// have if created right now. This info is both presented to relayers for the purpose of determining
// when to request batches and also used by the batch creation process to decide not to create
// a new batch (fees must be increasing)
func (k Keeper) GetBatchFeesByTokenType(ctx sdk.Context, tokenContract string, maxElements uint, baseFee sdkmath.Int) *types.BatchFees {
batchFee := &types.BatchFees{TokenContract: tokenContract, TotalFees: sdkmath.NewInt(0), TotalAmount: sdkmath.NewInt(0)}
k.IterateUnbatchedTransactions(ctx, tokenContract, func(tx *types.OutgoingTransferTx) bool {
if tx.Fee.Contract != tokenContract {
panic(fmt.Errorf("unexpected fee contract %s when getting batch fees for contract %s", tx.Fee.Contract, tokenContract))
}
if tx.Fee.Amount.LT(baseFee) {
// sort by fee and use ReverseIterator, so the fee behind is less than base fee
return true
}
batchFee.TotalFees = batchFee.TotalFees.Add(tx.Fee.Amount)
batchFee.TotalAmount = batchFee.TotalAmount.Add(tx.Token.Amount)
batchFee.TotalTxs += 1
return batchFee.TotalTxs == uint64(maxElements)
})
return batchFee
}
// GetAllBatchFees creates a fee entry for every batch type currently in the store
// this can be used by relayers to determine what batch types are desirable to request
func (k Keeper) GetAllBatchFees(ctx sdk.Context, maxElements uint, minBatchFees []types.MinBatchFee) (batchFees []*types.BatchFees) {
batchFeesMap := k.createBatchFees(ctx, maxElements, minBatchFees)
// create array of batchFees
for _, batchFee := range batchFeesMap {
batchFees = append(batchFees, batchFee)
}
// quick sort by token to make this function safe for use
// in consensus computations
sort.Slice(batchFees, func(i, j int) bool {
return batchFees[i].TokenContract < batchFees[j].TokenContract
})
return batchFees
}
// createBatchFees iterates over the unbatched transaction pool and creates batch token fee map
// Implicitly creates batches with the highest potential fee because the transaction keys enforce an order which goes
// fee contract address -> fee amount -> transaction nonce
func (k Keeper) createBatchFees(ctx sdk.Context, maxElements uint, minBatchFees []types.MinBatchFee) map[string]*types.BatchFees {
batchFeesMap := make(map[string]*types.BatchFees)
txCountMap := make(map[string]int)
baseFees := types.MinBatchFeeToBaseFees(minBatchFees)
k.IterateUnbatchedTransactions(ctx, "", func(tx *types.OutgoingTransferTx) bool {
fee := tx.Fee
baseFee, ok := baseFees[fee.Contract]
if ok && fee.Amount.LT(baseFee) {
return false // sort by token address and fee, behind have other token
}
if txCountMap[fee.Contract] < int(maxElements) {
addFeeToMap(tx.Token, fee, batchFeesMap, txCountMap)
}
return false
})
return batchFeesMap
}
func (k Keeper) autoIncrementID(ctx sdk.Context, idKey []byte) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(idKey)
var id uint64 = 1
if bz != nil {
id = binary.BigEndian.Uint64(bz)
}
bz = sdk.Uint64ToBigEndian(id + 1)
store.Set(idKey, bz)
return id
}
// Helper method for creating batch fees
func addFeeToMap(amt, fee types.ERC20Token, batchFeesMap map[string]*types.BatchFees, txCountMap map[string]int) {
txCountMap[fee.Contract] = txCountMap[fee.Contract] + 1
// add fee amount
if _, ok := batchFeesMap[fee.Contract]; ok {
batchFees := batchFeesMap[fee.Contract]
batchFees.TotalFees = batchFees.TotalFees.Add(fee.Amount)
batchFees.TotalTxs = batchFees.TotalTxs + 1
batchFees.TotalAmount = batchFees.TotalAmount.Add(amt.Amount)
batchFeesMap[fee.Contract] = batchFees
} else {
batchFeesMap[fee.Contract] = &types.BatchFees{
TokenContract: fee.Contract,
TotalFees: fee.Amount,
TotalTxs: 1,
TotalAmount: amt.Amount,
}
}
}