-
Notifications
You must be signed in to change notification settings - Fork 165
/
transactionManager_test.go
470 lines (401 loc) · 12.9 KB
/
transactionManager_test.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package eth
import (
"context"
"errors"
"fmt"
"math/big"
"sync"
"testing"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/golang/glog"
"github.com/livepeer/go-livepeer/pm"
"github.com/stretchr/testify/assert"
)
type stubTransactionSenderReader struct {
err map[string]error
pending bool
tx *types.Transaction
receipt *types.Receipt
callsToTxByHash int //reflects number of calls to replace()
}
func (stm *stubTransactionSenderReader) SendTransaction(ctx context.Context, tx *types.Transaction) error {
return stm.err["SendTransaction"]
}
func (stm *stubTransactionSenderReader) TransactionByHash(ctx context.Context, txHash common.Hash) (tx *types.Transaction, isPending bool, err error) {
stm.callsToTxByHash++
return stm.tx, stm.pending, stm.err["TransactionByHash"]
}
func (stm *stubTransactionSenderReader) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
if stm.receipt == nil {
return nil, ethereum.NotFound
}
return stm.receipt, stm.err["TransactionReceipt"]
}
func (stm *stubTransactionSenderReader) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
return []byte{}, stm.err["CodeAt"]
}
type stubTransactionSigner struct {
err error
}
func (sig *stubTransactionSigner) SignTx(tx *types.Transaction) (*types.Transaction, error) {
if sig.err != nil {
return nil, sig.err
}
return tx, nil
}
func TestTxQueue(t *testing.T) {
assert := assert.New(t)
tx := types.NewTransaction(1, pm.RandAddress(), big.NewInt(100), 1, big.NewInt(100), pm.RandBytes(32))
q := transactionQueue{}
q.add(tx)
assert.Len(q, 1)
assert.Equal(tx, q.peek())
assert.Equal(tx, q.pop())
assert.Len(q, 0)
q = transactionQueue{}
assert.Nil(q.pop())
assert.Nil(q.peek())
}
func TestTransactionManager_SendTransaction(t *testing.T) {
assert := assert.New(t)
eth := &stubTransactionSenderReader{
err: make(map[string]error),
}
q := transactionQueue{}
tm := &TransactionManager{
cond: sync.NewCond(&sync.Mutex{}),
eth: eth,
queue: q,
}
// Test error
expErr := errors.New("SendTransaction error")
eth.err["SendTransaction"] = expErr
tx := types.NewTransaction(1, pm.RandAddress(), big.NewInt(100), 100000, big.NewInt(100), pm.RandBytes(68))
errLogsBefore := glog.Stats.Info.Lines()
assert.EqualError(
tm.SendTransaction(context.Background(), tx),
expErr.Error(),
)
errLogsAfter := glog.Stats.Info.Lines()
assert.Equal(errLogsAfter-errLogsBefore, int64(1))
// Test no error
// Adds tx to queue
qLenBefore := q.length()
errLogsBefore = glog.Stats.Info.Lines()
eth.err = nil
assert.NoError(
tm.SendTransaction(context.Background(), tx),
)
qLenAfter := q.length()
errLogsAfter = glog.Stats.Info.Lines()
assert.Equal(errLogsAfter-errLogsBefore, int64(1))
assert.Equal(qLenAfter, qLenBefore, 1)
assert.Equal(tm.queue.peek().Hash(), tx.Hash())
}
func TestTransactionManager_Wait(t *testing.T) {
assert := assert.New(t)
eth := &stubTransactionSenderReader{
err: make(map[string]error),
}
q := transactionQueue{}
tm := &TransactionManager{
cond: sync.NewCond(&sync.Mutex{}),
eth: eth,
queue: q,
txTimeout: 2 * time.Second,
}
// Test error
// This calls bind.WaitMined() on the ethereum client, which will never actually return the actual underlying error and only log it using go-ethereum's custom logger
// The expected error should thus be 'context deadline exceeded'
// https://github.com/ethereum/go-ethereum/blob/aa637fd38a379db6da98df0d520fb1c5139a18ce/accounts/abi/bind/util.go#L41
expErr := errors.New("context deadline exceeded")
eth.err["TransactionByHash"] = expErr
tx := types.NewTransaction(1, pm.RandAddress(), big.NewInt(100), 100000, big.NewInt(100), pm.RandBytes(68))
receipt, err := tm.wait(tx)
assert.Nil(receipt)
assert.EqualError(err, expErr.Error())
// No error, stub a receipt
eth.receipt = types.NewReceipt(pm.RandHash().Bytes(), false, 100000)
eth.err = nil
receipt, err = tm.wait(tx)
assert.Equal(receipt.Status, uint64(1))
assert.Equal(receipt.CumulativeGasUsed, uint64(100000))
assert.Nil(err)
}
func TestTransactionManager_Replace(t *testing.T) {
assert := assert.New(t)
eth := &stubTransactionSenderReader{
err: make(map[string]error),
}
q := transactionQueue{}
baseFee := big.NewInt(9)
gasTipCap := big.NewInt(1)
gasFeeCap := new(big.Int).Add(gasTipCap, new(big.Int).Mul(baseFee, big.NewInt(2)))
gpm := &GasPriceMonitor{
minGasPrice: big.NewInt(0),
maxGasPrice: big.NewInt(0),
gasPrice: big.NewInt(1),
}
tm := &TransactionManager{
cond: sync.NewCond(&sync.Mutex{}),
eth: eth,
queue: q,
txTimeout: 2 * time.Second,
gpm: gpm,
}
stubTx := newStubDynamicFeeTx(gasFeeCap, gasTipCap)
// Test eth.TransactionByHash error
expErr := errors.New("TransactionByHash error")
eth.err["TransactionByHash"] = expErr
tx, err := tm.replace(stubTx)
assert.Nil(tx)
assert.EqualError(err, expErr.Error())
eth.err["TransactionByHash"] = nil
// Test no error - ErrReplacingMinedTx
eth.pending = false
tx, err = tm.replace(stubTx)
assert.Nil(tx)
assert.EqualError(err, ErrReplacingMinedTx.Error())
// Test error is ethereum.NotFound - fail at next step
eth.pending = true
gpm.maxGasPrice = big.NewInt(1)
eth.err["TransactionByHash"] = ethereum.NotFound
tx, err = tm.replace(stubTx)
assert.Nil(tx)
assert.EqualError(
err,
fmt.Sprintf("replacement gas price exceeds max gas price suggested=%v max=%v", applyPriceBump(calcGasPrice(stubTx), priceBump), gpm.maxGasPrice),
)
eth.err["TransactionByHash"] = nil
// Replacement gas price exceeds max gas price
// Throw error
tx, err = tm.replace(stubTx)
assert.Nil(tx)
assert.EqualError(
err,
fmt.Sprintf("replacement gas price exceeds max gas price suggested=%v max=%v", applyPriceBump(calcGasPrice(stubTx), priceBump), gpm.maxGasPrice),
)
// Error signing replacement tx
expErr = errors.New("SignTx error")
sig := &stubTransactionSigner{
err: nil,
}
tm.sig = sig
sig.err = expErr
gpm.maxGasPrice = big.NewInt(99999)
tx, err = tm.replace(stubTx)
assert.Nil(tx)
assert.EqualError(err, expErr.Error())
// Test when max gas price is nil - should still return signing replacement tx error
gpm.maxGasPrice = nil
tx, err = tm.replace(stubTx)
assert.Nil(tx)
assert.EqualError(err, expErr.Error())
sig.err = nil
// Error sending replacement tx
expErr = errors.New("SendTx error")
eth.err["SendTransaction"] = expErr
logsBefore := glog.Stats.Info.Lines()
tx, err = tm.replace(stubTx)
logsAfter := glog.Stats.Info.Lines()
assert.EqualError(err, expErr.Error())
assert.Equal(logsAfter-logsBefore, int64(1))
eth.err["SendTransaction"] = nil
// Success
logsBefore = glog.Stats.Info.Lines()
tx, err = tm.replace(stubTx)
logsAfter = glog.Stats.Info.Lines()
assert.Nil(err)
expTx := newReplacementTx(stubTx)
assert.Equal(tx.Hash(), expTx.Hash())
assert.Equal(logsAfter-logsBefore, int64(1))
}
func TestTransactionManager_CheckTxLoop(t *testing.T) {
assert := assert.New(t)
eth := &stubTransactionSenderReader{
err: make(map[string]error),
}
q := transactionQueue{}
gasPrice := big.NewInt(10)
gpm := &GasPriceMonitor{
minGasPrice: big.NewInt(0),
maxGasPrice: big.NewInt(99999999),
gasPrice: big.NewInt(1),
}
sig := &stubTransactionSigner{
err: nil,
}
tm := &TransactionManager{
maxReplacements: 0,
cond: sync.NewCond(&sync.Mutex{}),
eth: eth,
queue: q,
txTimeout: 2 * time.Second,
gpm: gpm,
sig: sig,
quit: make(chan struct{}),
}
eth.pending = true
receipt := types.NewReceipt(pm.RandHash().Bytes(), false, 100000)
eth.receipt = receipt
stubTx := types.NewTransaction(1, pm.RandAddress(), big.NewInt(100), 100000, gasPrice, pm.RandBytes(68))
go tm.Start()
defer tm.Stop()
sink := make(chan *transactionReceipt, 10)
sub := tm.Subscribe(sink)
tm.SendTransaction(context.Background(), stubTx)
event := <-sink
assert.NotNil(event)
assert.Nil(event.err)
sub.Unsubscribe()
// Wait error no replacements
eth.receipt = nil
eth.err["TransactionReceipt"] = context.DeadlineExceeded
sink = make(chan *transactionReceipt)
sub = tm.Subscribe(sink)
tm.SendTransaction(context.Background(), stubTx)
event = <-sink
assert.NotNil(event.Receipt)
assert.Equal(event.originTxHash, stubTx.Hash())
assert.NotNil(event)
assert.EqualError(event.err, context.DeadlineExceeded.Error())
eth.err["TransactionReceipt"] = nil
sub.Unsubscribe()
// Wait error, replacements
// Replace tx error
tm.maxReplacements = 1
eth.err["TransactionReceipt"] = context.DeadlineExceeded
eth.err["TransactionByHash"] = errors.New("TransactionByHash error")
sink = make(chan *transactionReceipt)
sub = tm.Subscribe(sink)
tm.SendTransaction(context.Background(), stubTx)
event = <-sink
assert.NotNil(event.Receipt)
assert.Equal(event.originTxHash, stubTx.Hash())
assert.EqualError(event.err, eth.err["TransactionByHash"].Error())
assert.Equal(eth.callsToTxByHash, 1)
sub.Unsubscribe()
// Replace multiple times, but fail replacement
// Should replace only once
eth.callsToTxByHash = 0
tm.maxReplacements = 3
sink = make(chan *transactionReceipt)
sub = tm.Subscribe(sink)
tm.SendTransaction(context.Background(), stubTx)
event = <-sink
assert.NotNil(event.Receipt)
assert.Equal(event.originTxHash, stubTx.Hash())
assert.EqualError(event.err, eth.err["TransactionByHash"].Error())
assert.Equal(eth.callsToTxByHash, 1)
assert.LessOrEqual(eth.callsToTxByHash, tm.maxReplacements)
sub.Unsubscribe()
// replace multiple times, time out
// replace 'maxReplacements' times
eth.callsToTxByHash = 0
eth.err["TransactionByHash"] = nil
sink = make(chan *transactionReceipt)
sub = tm.Subscribe(sink)
tm.SendTransaction(context.Background(), stubTx)
event = <-sink
assert.NotNil(event.Receipt)
assert.Equal(event.originTxHash, stubTx.Hash())
assert.EqualError(event.err, context.DeadlineExceeded.Error())
assert.Equal(eth.callsToTxByHash, tm.maxReplacements)
sub.Unsubscribe()
}
func TestApplyPriceBump(t *testing.T) {
assert := assert.New(t)
// priceBump = 0
// 500 * 1 = 500
res := applyPriceBump(big.NewInt(500), 0)
assert.Equal(big.NewInt(500), res)
// priceBump = 0.11
// 500 * 1.11 = 555
res = applyPriceBump(big.NewInt(500), 11)
assert.Equal(big.NewInt(555), res)
// priceBump = 0.17
// 500 * 1.17 = 585
res = applyPriceBump(big.NewInt(500), 17)
assert.Equal(big.NewInt(585), res)
// priceBump > 100
// 500 * 2.01 = 1005
res = applyPriceBump(big.NewInt(500), 101)
assert.Equal(big.NewInt(1005), res)
// Tests round up when result is not a whole number
// 50 * 1.11 = 55.5 -> 56
res = applyPriceBump(big.NewInt(50), 11)
assert.Equal(big.NewInt(56), res)
// 501 * 1.11 = 556.11 -> 557
res = applyPriceBump(big.NewInt(501), 11)
assert.Equal(big.NewInt(557), res)
// 9 * 1.11 = 9.99 -> 10
res = applyPriceBump(big.NewInt(9), 11)
assert.Equal(big.NewInt(10), res)
}
func TestCalcGasPrice(t *testing.T) {
assert := assert.New(t)
// legacy tx, not London ready
gasPrice := big.NewInt(300)
tx := newStubLegacyTx(gasPrice)
result := calcGasPrice(tx)
assert.Equal(gasPrice, result)
// dynamic tx
baseFee := big.NewInt(1000)
gasTipCap := big.NewInt(100)
gasFeeCap := new(big.Int).Add(gasTipCap, new(big.Int).Mul(baseFee, big.NewInt(2)))
tx = newStubDynamicFeeTx(gasFeeCap, gasTipCap)
result = calcGasPrice(tx)
assert.Equal(new(big.Int).Add(baseFee, gasTipCap), result)
}
func TestNewReplacementTx_LegacyTx(t *testing.T) {
assert := assert.New(t)
gasPrice := big.NewInt(100)
tx1 := newStubLegacyTx(gasPrice)
tx2 := newReplacementTx(tx1)
assert.NotEqual(tx1.Hash(), tx2.Hash())
assert.Equal(applyPriceBump(tx1.GasPrice(), priceBump), tx2.GasPrice())
assert.Equal(tx1.Nonce(), tx2.Nonce())
assert.Equal(tx1.Gas(), tx2.Gas())
assert.Equal(tx1.Value(), tx1.Value())
assert.Equal(tx1.To(), tx2.To())
}
func TestNewReplacementTx_DynamicFeeTx(t *testing.T) {
assert := assert.New(t)
gasTipCap := big.NewInt(100)
gasFeeCap := big.NewInt(1000)
tx1 := newStubDynamicFeeTx(gasFeeCap, gasTipCap)
tx2 := newReplacementTx(tx1)
assert.NotEqual(tx1.Hash(), tx2.Hash())
assert.Equal(applyPriceBump(tx1.GasTipCap(), priceBump), tx2.GasTipCap())
assert.Equal(applyPriceBump(tx1.GasFeeCap(), priceBump), tx2.GasFeeCap())
assert.Equal(tx1.Nonce(), tx2.Nonce())
assert.Equal(tx1.Gas(), tx2.Gas())
assert.Equal(tx1.Value(), tx1.Value())
assert.Equal(tx1.To(), tx2.To())
}
func newStubLegacyTx(gasPrice *big.Int) *types.Transaction {
addr := pm.RandAddress()
return types.NewTx(&types.LegacyTx{
Nonce: 1,
GasPrice: gasPrice,
Gas: 1000000,
Value: big.NewInt(100),
Data: pm.RandBytes(68),
To: &addr,
})
}
func newStubDynamicFeeTx(gasFeeCap, gasTipCap *big.Int) *types.Transaction {
addr := pm.RandAddress()
return types.NewTx(&types.DynamicFeeTx{
Nonce: 1,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Gas: 1000000,
Value: big.NewInt(100),
Data: pm.RandBytes(68),
To: &addr,
})
}