forked from althea-net/cosmos-gravity-bridge
-
Notifications
You must be signed in to change notification settings - Fork 10
/
pool.go
365 lines (318 loc) · 14.1 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
package keeper
import (
"encoding/binary"
"fmt"
"sort"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/onomyprotocol/arc/module/eth/x/gravity/types"
)
// AddToOutgoingPool creates a transaction and adds it to the pool, returns the id of the unbatched transaction
// - 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
func (k Keeper) AddToOutgoingPool(
ctx sdk.Context,
sender sdk.AccAddress,
counterpartReceiver types.EthAddress,
amount sdk.Coin,
fee sdk.Coin,
) (uint64, error) {
if ctx.IsZero() || sdk.VerifyAddressFormat(sender) != nil || counterpartReceiver.ValidateBasic() != nil ||
!amount.IsValid() || !fee.IsValid() || fee.Denom != amount.Denom {
return 0, sdkerrors.Wrap(types.ErrInvalid, "arguments")
}
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.
_, tokenContract, err := k.DenomToERC20Lookup(ctx, totalAmount.Denom)
if err != nil {
return 0, err
}
// lock coins in module
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, totalInVouchers); err != nil {
return 0, err
}
// get next tx id from keeper
nextID := k.autoIncrementID(ctx, []byte(types.KeyLastTXPoolID))
erc20Fee, err := types.NewInternalERC20Token(fee.Amount, tokenContract.GetAddress())
if err != nil {
return 0, sdkerrors.Wrapf(err, "invalid Erc20Fee from amount %d and contract %v",
fee.Amount, tokenContract)
}
erc20Token, err := types.NewInternalERC20Token(amount.Amount, tokenContract.GetAddress())
if err != nil {
return 0, sdkerrors.Wrapf(err, "invalid ERC20Token from amount %d and contract %v",
amount.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, err := types.OutgoingTransferTx{
Id: nextID,
Sender: sender.String(),
DestAddress: counterpartReceiver.GetAddress(),
Erc20Token: erc20Token.ToExternal(),
Erc20Fee: erc20Fee.ToExternal(),
}.ToInternal()
if err != nil { // This should never happen since all the components are validated
panic(sdkerrors.Wrap(err, "unable to create InternalOutgoingTransferTx"))
}
// add a second index with the fee
err = k.addUnbatchedTX(ctx, outgoing)
if err != nil {
panic(err)
}
// todo: add second index for sender so that we can easily query: give pending Tx by sender
// todo: what about a second index for receiver?
poolEvent := sdk.NewEvent(
types.EventTypeBridgeWithdrawalReceived,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyContract, k.GetBridgeContractAddress(ctx).GetAddress()),
sdk.NewAttribute(types.AttributeKeyBridgeChainID, strconv.Itoa(int(k.GetBridgeChainID(ctx)))),
sdk.NewAttribute(types.AttributeKeyOutgoingTXID, strconv.Itoa(int(nextID))),
sdk.NewAttribute(types.AttributeKeyNonce, fmt.Sprint(nextID)),
)
ctx.EventManager().EmitEvent(poolEvent)
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 {
if ctx.IsZero() || txId < 1 || sdk.VerifyAddressFormat(sender) != nil {
return sdkerrors.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 sdkerrors.Wrapf(err, "unknown transaction with id %d from sender %s", txId, sender.String())
}
// Check that this user actually sent the transaction, this prevents someone from refunding someone
// elses transaction to themselves.
if !tx.Sender.Equals(sender) {
return sdkerrors.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.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 the pool
err = k.removeUnbatchedTX(ctx, *tx.Erc20Fee, txId)
if err != nil {
return sdkerrors.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.Erc20Fee, tx.Id)
if oldTx != nil || oldTxErr == nil {
return sdkerrors.Wrapf(types.ErrInvalid, "tx with id %d was not fully removed from the pool, a duplicate must exist", txId)
}
// Calculate refund
totalToRefund := tx.Erc20Token.GravityCoin()
totalToRefund.Amount = totalToRefund.Amount.Add(tx.Erc20Fee.Amount)
totalToRefundCoins := sdk.NewCoins(totalToRefund)
// Perform refund
if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, totalToRefundCoins); err != nil {
return sdkerrors.Wrap(err, "transfer vouchers")
}
poolEvent := sdk.NewEvent(
types.EventTypeBridgeWithdrawCanceled,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyContract, k.GetBridgeContractAddress(ctx).GetAddress()),
sdk.NewAttribute(types.AttributeKeyBridgeChainID, strconv.Itoa(int(k.GetBridgeChainID(ctx)))),
)
ctx.EventManager().EmitEvent(poolEvent)
return nil
}
// addUnbatchedTx creates a new transaction in the pool
// WARNING: Do not make this function public
func (k Keeper) addUnbatchedTX(ctx sdk.Context, val *types.InternalOutgoingTransferTx) error {
store := ctx.KVStore(k.storeKey)
idxKey := []byte(types.GetOutgoingTxPoolKey(*val.Erc20Fee, val.Id))
if store.Has(idxKey) {
return sdkerrors.Wrap(types.ErrDuplicate, "transaction already in pool")
}
extVal := val.ToExternal()
bz, err := k.cdc.Marshal(&extVal)
if err != nil {
return err
}
store.Set(idxKey, bz)
return err
}
// removeUnbatchedTXIndex removes the tx from the pool
// WARNING: Do not make this function public
func (k Keeper) removeUnbatchedTX(ctx sdk.Context, fee types.InternalERC20Token, txID uint64) error {
store := ctx.KVStore(k.storeKey)
idxKey := []byte(types.GetOutgoingTxPoolKey(fee, txID))
if !store.Has(idxKey) {
return sdkerrors.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.InternalERC20Token, txID uint64) (*types.InternalOutgoingTransferTx, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(types.GetOutgoingTxPoolKey(fee, txID)))
if bz == nil {
return nil, sdkerrors.Wrap(types.ErrUnknown, "pool transaction")
}
var r types.OutgoingTransferTx
err := k.cdc.Unmarshal(bz, &r)
if err != nil {
panic(sdkerrors.Wrapf(err, "invalid unbatched tx in store: %v", r))
}
intR, err := r.ToInternal()
if err != nil {
panic(sdkerrors.Wrapf(err, "invalid unbatched tx in store: %v", r))
}
return intR, nil
}
// 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.InternalOutgoingTransferTx, error) {
var r *types.InternalOutgoingTransferTx = nil
k.IterateUnbatchedTransactions(ctx, []byte(types.OutgoingTXPoolKey), func(_ []byte, tx *types.InternalOutgoingTransferTx) bool {
if tx.Id == txID {
r = tx
return true
}
return false // iterating DESC, exit early
})
if r == nil {
// We have no return tx, it was either batched or never existed
return nil, sdkerrors.Wrap(types.ErrUnknown, "pool transaction")
}
return r, nil
}
// GetUnbatchedTransactionsByContract, grabs all unbatched transactions from the tx pool for the given contract
// unbatched transactions are sorted by fee amount in DESC order
func (k Keeper) GetUnbatchedTransactionsByContract(ctx sdk.Context, contractAddress types.EthAddress) []*types.InternalOutgoingTransferTx {
return k.collectUnbatchedTransactions(ctx, []byte(types.GetOutgoingTxPoolContractPrefix(contractAddress)))
}
// GetPoolTransactions, grabs all transactions from the tx pool, useful for queries or genesis save/load
func (k Keeper) GetUnbatchedTransactions(ctx sdk.Context) []*types.InternalOutgoingTransferTx {
return k.collectUnbatchedTransactions(ctx, []byte(types.OutgoingTXPoolKey))
}
// Aggregates all unbatched transactions in the store with a given prefix
func (k Keeper) collectUnbatchedTransactions(ctx sdk.Context, prefixKey []byte) (out []*types.InternalOutgoingTransferTx) {
k.IterateUnbatchedTransactions(ctx, prefixKey, func(_ []byte, tx *types.InternalOutgoingTransferTx) bool {
out = append(out, tx)
return false
})
return
}
// IterateUnbatchedTransactionsByContract, iterates through unbatched transactions from the tx pool for the given contract
// unbatched transactions are sorted by fee amount in DESC order
func (k Keeper) IterateUnbatchedTransactionsByContract(ctx sdk.Context, contractAddress types.EthAddress, cb func(key []byte, tx *types.InternalOutgoingTransferTx) bool) {
k.IterateUnbatchedTransactions(ctx, []byte(types.GetOutgoingTxPoolContractPrefix(contractAddress)), cb)
}
// IterateUnbatchedTransactions iterates through all unbatched transactions whose keys begin with prefixKey in DESC order
func (k Keeper) IterateUnbatchedTransactions(ctx sdk.Context, prefixKey []byte, cb func(key []byte, tx *types.InternalOutgoingTransferTx) bool) {
prefixStore := ctx.KVStore(k.storeKey)
iter := prefixStore.ReverseIterator(prefixRange(prefixKey))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var transact types.OutgoingTransferTx
k.cdc.MustUnmarshal(iter.Value(), &transact)
intTx, err := transact.ToInternal()
if err != nil {
panic(sdkerrors.Wrapf(err, "invalid unbatched transaction in store: %v", transact))
}
// cb returns true to stop early
if cb(iter.Key(), intTx) {
break
}
}
}
// GetBatchFeeByTokenType 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) GetBatchFeeByTokenType(ctx sdk.Context, tokenContractAddr types.EthAddress, maxElements uint) *types.BatchFees {
batchFee := types.BatchFees{Token: tokenContractAddr.GetAddress(), TotalFees: sdk.NewInt(0), TxCount: 0}
k.IterateUnbatchedTransactions(ctx, []byte(types.GetOutgoingTxPoolContractPrefix(tokenContractAddr)), func(_ []byte, tx *types.InternalOutgoingTransferTx) bool {
if !k.IsOnBlacklist(ctx, *tx.DestAddress) {
fee := tx.Erc20Fee
if fee.Contract.GetAddress() != tokenContractAddr.GetAddress() {
panic(fmt.Errorf("unexpected fee contract %s when getting batch fees for contract %s", fee.Contract, tokenContractAddr))
}
batchFee.TotalFees = batchFee.TotalFees.Add(fee.Amount)
batchFee.TxCount += 1
return batchFee.TxCount == uint64(maxElements)
} else {
// if the tx was on the blacklist we return false
// to continue to the next loop iteration
return false
}
})
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 desireable to request
func (k Keeper) GetAllBatchFees(ctx sdk.Context, maxElements uint) (batchFees []types.BatchFees) {
batchFeesMap := k.createBatchFees(ctx, maxElements)
// 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].Token < batchFees[j].Token
})
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) map[string]types.BatchFees {
batchFeesMap := make(map[string]types.BatchFees)
k.IterateUnbatchedTransactions(ctx, []byte(types.OutgoingTXPoolKey), func(_ []byte, tx *types.InternalOutgoingTransferTx) bool {
feeAddrStr := tx.Erc20Fee.Contract.GetAddress()
if fees, ok := batchFeesMap[feeAddrStr]; ok {
if fees.TxCount < uint64(maxElements) {
fees.TotalFees = batchFeesMap[feeAddrStr].TotalFees.Add(tx.Erc20Fee.Amount)
fees.TxCount = fees.TxCount + 1
batchFeesMap[feeAddrStr] = fees
}
} else {
batchFeesMap[feeAddrStr] = types.BatchFees{
Token: feeAddrStr,
TotalFees: tx.Erc20Fee.Amount,
TxCount: 1,
}
}
return false
})
return batchFeesMap
}
// a specialized function used for iterating store counters, handling
// returning, initializing and incrementing all at once. This is particularly
// used for the transaction pool and batch pool where each batch or transaction is
// assigned a unique ID.
func (k Keeper) autoIncrementID(ctx sdk.Context, idKey []byte) uint64 {
id := k.getID(ctx, idKey)
id += 1
k.setID(ctx, id, idKey)
return id
}
// gets a generic uint64 counter from the store, initializing to 1 if no value exists
func (k Keeper) getID(ctx sdk.Context, idKey []byte) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(idKey)
id := binary.BigEndian.Uint64(bz)
return id
}
// sets a generic uint64 counter in the store
func (k Keeper) setID(ctx sdk.Context, id uint64, idKey []byte) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(id)
store.Set(idKey, bz)
}