-
Notifications
You must be signed in to change notification settings - Fork 14
/
pool.go
369 lines (326 loc) · 12.9 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
package keeper
import (
"encoding/binary"
"fmt"
"math/big"
"sort"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/tendermint/tendermint/libs/math"
"github.com/functionx/fx-core/x/gravity/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, counterpartReceiver string, amount sdk.Coin, fee sdk.Coin) (uint64, error) {
totalAmount := amount.Add(fee)
totalInVouchers := sdk.Coins{totalAmount}
// 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.
isCosmosOriginated, tokenContract, err := k.DenomToERC20Lookup(ctx, totalAmount.Denom)
if err != nil {
return 0, err
}
// If it is a fx-originateda asset we lock it
if isCosmosOriginated {
// lock coins in module
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, totalInVouchers); err != nil {
return 0, err
}
} else {
// If it is an ethereum-originated asset we burn it
// send coins to module in prep for burn
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, totalInVouchers); err != nil {
return 0, err
}
// burn vouchers to send them back to ETH
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, totalInVouchers); err != nil {
panic(err)
}
}
// get next tx id from keeper
nextID := k.autoIncrementID(ctx, types.KeyLastTXPoolID)
erc20Fee := types.NewSDKIntERC20Token(fee.Amount, tokenContract)
// 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: nextID,
Sender: sender.String(),
DestAddress: counterpartReceiver,
Erc20Token: types.NewSDKIntERC20Token(amount.Amount, tokenContract),
Erc20Fee: erc20Fee,
}
// set the outgoing tx in the pool index
if err := k.setPoolEntry(ctx, outgoing); err != nil {
return 0, err
}
// add a second index with the fee
k.appendToUnbatchedTXIndex(ctx, tokenContract, *erc20Fee, nextID)
// todo: add second index for sender so that we can easily query: give pending Tx by sender what about a second index for receiver?
k.GetBridgeChainID(ctx)
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeSendToEth,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyOutgoingTxID, fmt.Sprint(nextID)),
))
return nextID, nil
}
// RemoveFromOutgoingPoolAndRefund
// - checks that the provided tx actually exists
// - deletes the unbatched tx from the pool
// - issues the tokens back to the sender
func (k Keeper) RemoveFromOutgoingPoolAndRefund(ctx sdk.Context, txId uint64, sender sdk.AccAddress) error {
// check that we actually have a tx with that id and what it's details are
tx, err := k.getPoolEntry(ctx, txId)
if err != nil {
return err
}
// Check that this user actually sent the transaction, this prevents someone from refunding someone
// elses transaction to themselves.
txSender, err := sdk.AccAddressFromBech32(tx.Sender)
if err != nil {
panic("Invalid address in store!")
}
if !txSender.Equals(sender) {
return sdkerrors.Wrapf(types.ErrInvalid, "Sender %s did not send Id %d", sender, txId)
}
found := false
poolTx := k.GetPoolTransactions(ctx)
for _, pTx := range poolTx {
if pTx.Id == txId {
found = true
}
}
if !found {
return sdkerrors.Wrapf(types.ErrInvalid, "Id %d is in a batch", 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.Erc20Fee.Contract != tx.Erc20Token.Contract {
return sdkerrors.Wrapf(types.ErrInvalid, "Inconsistent tokens to cancel!: %s %s", tx.Erc20Fee.Contract, tx.Erc20Token.Contract)
}
// delete this tx from both indexes
k.removePoolEntry(ctx, txId)
if err = k.removeFromUnbatchedTXIndex(ctx, *tx.Erc20Fee, txId); err != nil {
return err
}
// query denom, if not exist, return error
isFxOriginated, denom := k.ERC20ToDenomLookup(ctx, tx.Erc20Token.Contract)
if !isFxOriginated && denom == "" {
return sdkerrors.Wrapf(types.ErrInvalid, "Invalid token, contract %s", tx.Erc20Token.Contract)
}
// reissue the amount and the fee
totalToRefund := sdk.NewCoin(denom, tx.Erc20Token.Amount)
totalToRefund.Amount = totalToRefund.Amount.Add(tx.Erc20Fee.Amount)
totalToRefundCoins := sdk.NewCoins(totalToRefund)
// If it is a fx-originateda the coins are in the module (see AddToOutgoingPool) so we can just take them out
if isFxOriginated {
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, totalToRefundCoins); err != nil {
return err
}
} else {
// If it is an ethereum-originated asset we have to mint it (see Handle in attestation_handler.go)
// mint coins in module for prep to send
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, totalToRefundCoins); err != nil {
return sdkerrors.Wrapf(err, "mint vouchers coins: %s", totalToRefundCoins)
}
if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, totalToRefundCoins); err != nil {
return sdkerrors.Wrap(err, "transfer vouchers")
}
}
k.GetBridgeChainID(ctx)
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeSendToEthCanceled,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyOutgoingTxID, fmt.Sprint(txId)),
))
return nil
}
// appendToUnbatchedTXIndex add at the end when tx with same fee exists
func (k Keeper) appendToUnbatchedTXIndex(ctx sdk.Context, tokenContract string, fee types.ERC20Token, txID uint64) {
store := ctx.KVStore(k.storeKey)
idxKey := types.GetFeeSecondIndexKey(fee)
var idSet types.IDSet
if store.Has(idxKey) {
bz := store.Get(idxKey)
k.cdc.MustUnmarshalBinaryBare(bz, &idSet)
}
idSet.Ids = append(idSet.Ids, txID)
store.Set(idxKey, k.cdc.MustMarshalBinaryBare(&idSet))
}
// prependToUnbatchedTXIndex add at the top when tx with same fee exists
func (k Keeper) prependToUnbatchedTXIndex(ctx sdk.Context, tokenContract string, fee types.ERC20Token, txID uint64) {
store := ctx.KVStore(k.storeKey)
idxKey := types.GetFeeSecondIndexKey(fee)
var idSet types.IDSet
if store.Has(idxKey) {
bz := store.Get(idxKey)
k.cdc.MustUnmarshalBinaryBare(bz, &idSet)
}
idSet.Ids = append([]uint64{txID}, idSet.Ids...)
store.Set(idxKey, k.cdc.MustMarshalBinaryBare(&idSet))
}
// removeFromUnbatchedTXIndex removes the tx from the index and makes it implicit no available anymore
func (k Keeper) removeFromUnbatchedTXIndex(ctx sdk.Context, fee types.ERC20Token, txID uint64) error {
store := ctx.KVStore(k.storeKey)
idxKey := types.GetFeeSecondIndexKey(fee)
var idSet types.IDSet
bz := store.Get(idxKey)
if bz == nil {
return sdkerrors.Wrap(types.ErrUnknown, "fee")
}
k.cdc.MustUnmarshalBinaryBare(bz, &idSet)
for i := range idSet.Ids {
if idSet.Ids[i] == txID {
idSet.Ids = append(idSet.Ids[0:i], idSet.Ids[i+1:]...)
if len(idSet.Ids) != 0 {
store.Set(idxKey, k.cdc.MustMarshalBinaryBare(&idSet))
} else {
store.Delete(idxKey)
}
return nil
}
}
return sdkerrors.Wrap(types.ErrUnknown, "tx id")
}
func (k Keeper) setPoolEntry(ctx sdk.Context, val *types.OutgoingTransferTx) error {
bz, err := k.cdc.MarshalBinaryBare(val)
if err != nil {
return err
}
store := ctx.KVStore(k.storeKey)
store.Set(types.GetOutgoingTxPoolKey(val.Id), bz)
return nil
}
func (k Keeper) getPoolEntry(ctx sdk.Context, id uint64) (*types.OutgoingTransferTx, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetOutgoingTxPoolKey(id))
if bz == nil {
return nil, types.ErrUnknown
}
var r types.OutgoingTransferTx
k.cdc.MustUnmarshalBinaryBare(bz, &r)
return &r, nil
}
func (k Keeper) removePoolEntry(ctx sdk.Context, id uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetOutgoingTxPoolKey(id))
}
// GetPoolTransactions grabs all transactions from the tx pool, useful for queries or genesis save/load
func (k Keeper) GetPoolTransactions(ctx sdk.Context) []*types.OutgoingTransferTx {
prefixStore := ctx.KVStore(k.storeKey)
// we must use the second index key here because transactions are left in the store, but removed
// from the tx sorting key, while in batches
iter := prefixStore.ReverseIterator(prefixRange([]byte(types.SecondIndexOutgoingTXFeeKey)))
var ret []*types.OutgoingTransferTx
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var ids types.IDSet
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &ids)
for _, id := range ids.Ids {
tx, err := k.getPoolEntry(ctx, id)
if err != nil {
panic("Invalid id in tx index!")
}
ret = append(ret, tx)
}
}
return ret
}
// IterateOutgoingPoolByFee iterates over the outgoing pool which is sorted by fee
func (k Keeper) IterateOutgoingPoolByFee(ctx sdk.Context, contract string, cb func(uint64, *types.OutgoingTransferTx) bool) {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.SecondIndexOutgoingTXFeeKey)
iter := prefixStore.ReverseIterator(prefixRange([]byte(contract)))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var ids types.IDSet
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &ids)
// cb returns true to stop early
for _, id := range ids.Ids {
tx, err := k.getPoolEntry(ctx, id)
if err != nil {
panic("Invalid id in tx index!")
}
if cb(id, tx) {
return
}
}
}
}
// GetBatchFeesByTokenType gets the fees the next batch of a given token type would
// have if created. 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
func (k Keeper) GetBatchFeesByTokenType(ctx sdk.Context, tokenContractAddr string) *types.BatchFees {
batchFeesMap := k.createBatchFees(ctx)
return batchFeesMap[tokenContractAddr]
}
// 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) (batchFees []*types.BatchFees) {
batchFeesMap := k.createBatchFees(ctx)
// 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 outgoing pool and creates batch token fee map
func (k Keeper) createBatchFees(ctx sdk.Context) map[string]*types.BatchFees {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.SecondIndexOutgoingTXFeeKey)
iter := prefixStore.Iterator(nil, nil)
defer iter.Close()
batchFeesMap := make(map[string]*types.BatchFees)
for ; iter.Valid(); iter.Next() {
var ids types.IDSet
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &ids)
// create a map to store the token contract address and its total fee
// Parse the iterator key to get contract address & fee
// If len(ids.Ids) > 1, multiply fee amount with len(ids.Ids) and add it to total fee amount
key := iter.Key()
tokenContractBytes := key[:types.ETHContractAddressLen]
tokenContractAddr := string(tokenContractBytes)
feeAmountBytes := key[len(tokenContractBytes):]
feeAmount := big.NewInt(0).SetBytes(feeAmountBytes)
if _, ok := batchFeesMap[tokenContractAddr]; !ok {
handleSize := math.MinInt(OutgoingTxBatchSize, len(ids.Ids))
keyFeeTotals := feeAmount.Mul(feeAmount, big.NewInt(int64(handleSize)))
batchFeesMap[tokenContractAddr] = &types.BatchFees{
TokenContract: tokenContractAddr,
TotalFees: sdk.NewIntFromBigInt(keyFeeTotals),
TotalTxs: uint64(len(ids.Ids)),
}
continue
}
fullTxBatchSize := OutgoingTxBatchSize - batchFeesMap[tokenContractAddr].TotalTxs
batchFeesMap[tokenContractAddr].TotalTxs = batchFeesMap[tokenContractAddr].TotalTxs + uint64(len(ids.Ids))
if fullTxBatchSize <= 0 {
continue
}
// Fee for filling 100 transactions
fullTxBatchHandleSize := math.MinInt(int(fullTxBatchSize), len(ids.Ids))
fullTxBatchFee := feeAmount.Mul(feeAmount, big.NewInt(int64(fullTxBatchHandleSize)))
batchFeesMap[tokenContractAddr].TotalFees = batchFeesMap[tokenContractAddr].TotalFees.Add(sdk.NewIntFromBigInt(fullTxBatchFee))
}
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
}