/
tx_manager.go
771 lines (641 loc) · 23.2 KB
/
tx_manager.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
package store
import (
"fmt"
"math/big"
"regexp"
"strings"
"sync"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/gobuffalo/packr"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"gopkg.in/guregu/null.v3"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/store/assets"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/orm"
"github.com/tevino/abool"
"go.uber.org/multierr"
)
// DefaultGasLimit sets the default gas limit for outgoing transactions.
// if updating DefaultGasLimit, be sure it matches with the
// DefaultGasLimit specified in evm/test/Oracle_test.js
const DefaultGasLimit uint64 = 500000
const nonceReloadLimit int = 1
// ErrPendingConnection is the error returned if TxManager is not connected.
var ErrPendingConnection = errors.New("Cannot talk to chain, pending connection")
// TxManager represents an interface for interacting with the blockchain
type TxManager interface {
HeadTrackable
Connected() bool
Register(accounts []accounts.Account)
CreateTx(to common.Address, data []byte) (*models.Tx, error)
CreateTxWithGas(surrogateID null.String, to common.Address, data []byte, gasPriceWei *big.Int, gasLimit uint64) (*models.Tx, error)
CreateTxWithEth(from, to common.Address, value *assets.Eth) (*models.Tx, error)
CheckAttempt(txAttempt *models.TxAttempt, blockHeight uint64) (*models.TxReceipt, AttemptState, error)
BumpGasUntilSafe(hash common.Hash) (*models.TxReceipt, AttemptState, error)
ContractLINKBalance(wr models.WithdrawalRequest) (assets.Link, error)
WithdrawLINK(wr models.WithdrawalRequest) (common.Hash, error)
GetLINKBalance(address common.Address) (*assets.Link, error)
NextActiveAccount() *ManagedAccount
GetEthBalance(address common.Address) (*assets.Eth, error)
SubscribeToNewHeads(channel chan<- models.BlockHeader) (models.EthSubscription, error)
GetBlockByNumber(hex string) (models.BlockHeader, error)
SubscribeToLogs(channel chan<- models.Log, q ethereum.FilterQuery) (models.EthSubscription, error)
GetLogs(q ethereum.FilterQuery) ([]models.Log, error)
GetTxReceipt(common.Hash) (*models.TxReceipt, error)
GetChainID() (uint64, error)
}
//go:generate mockgen -package=mocks -destination=../internal/mocks/tx_manager_mocks.go github.com/smartcontractkit/chainlink/core/store TxManager
// EthTxManager contains fields for the Ethereum client, the KeyStore,
// the local Config for the application, and the database.
type EthTxManager struct {
*EthClient
keyStore *KeyStore
config Config
orm *orm.ORM
registeredAccounts []accounts.Account
availableAccounts []*ManagedAccount
availableAccountIdx int
accountsMutex *sync.Mutex
connected *abool.AtomicBool
}
// NewEthTxManager constructs an EthTxManager using the passed variables and
// initializing internal variables.
func NewEthTxManager(ethClient *EthClient, config Config, keyStore *KeyStore, orm *orm.ORM) *EthTxManager {
return &EthTxManager{
EthClient: ethClient,
config: config,
keyStore: keyStore,
orm: orm,
accountsMutex: &sync.Mutex{},
connected: abool.New(),
}
}
// Register activates accounts for outgoing transactions and client side
// nonce management.
func (txm *EthTxManager) Register(accts []accounts.Account) {
txm.accountsMutex.Lock()
defer txm.accountsMutex.Unlock()
cp := make([]accounts.Account, len(accts))
copy(cp, accts)
txm.registeredAccounts = cp
}
// Connected returns a bool indicating whether or not it is connected.
func (txm *EthTxManager) Connected() bool {
return txm.connected.IsSet()
}
// Connect iterates over the available accounts to retrieve their nonce
// for client side management.
func (txm *EthTxManager) Connect(bn *models.Head) error {
txm.accountsMutex.Lock()
defer txm.accountsMutex.Unlock()
txm.availableAccounts = []*ManagedAccount{}
var merr error
for _, a := range txm.registeredAccounts {
ma, err := txm.activateAccount(a)
merr = multierr.Append(merr, err)
if err == nil {
txm.availableAccounts = append(txm.availableAccounts, ma)
}
}
txm.connected.Set()
return merr
}
// Disconnect marks this instance as disconnected.
func (txm *EthTxManager) Disconnect() {
txm.connected.UnSet()
}
// OnNewHead does nothing; exists to comply with interface.
func (txm *EthTxManager) OnNewHead(*models.Head) {}
// CreateTx signs and sends a transaction to the Ethereum blockchain.
func (txm *EthTxManager) CreateTx(to common.Address, data []byte) (*models.Tx, error) {
return txm.CreateTxWithGas(null.String{}, to, data, txm.config.EthGasPriceDefault(), DefaultGasLimit)
}
// CreateTxWithGas signs and sends a transaction to the Ethereum blockchain.
func (txm *EthTxManager) CreateTxWithGas(surrogateID null.String, to common.Address, data []byte, gasPriceWei *big.Int, gasLimit uint64) (*models.Tx, error) {
ma, err := txm.nextAccount()
if err != nil {
return nil, err
}
gasPriceWei, gasLimit = normalizeGasParams(gasPriceWei, gasLimit, txm.config)
return txm.createTx(surrogateID, ma, to, data, gasPriceWei, gasLimit, nil)
}
// CreateTxWithEth signs and sends a transaction with some ETH to transfer.
func (txm *EthTxManager) CreateTxWithEth(from, to common.Address, value *assets.Eth) (*models.Tx, error) {
ma := txm.getAccount(from)
if ma == nil {
return nil, errors.New("account does not exist")
}
return txm.createTx(null.String{}, ma, to, []byte{}, txm.config.EthGasPriceDefault(), DefaultGasLimit, value)
}
func (txm *EthTxManager) nextAccount() (*ManagedAccount, error) {
if !txm.Connected() {
return nil, errors.Wrap(ErrPendingConnection, "EthTxManager#nextAccount")
}
ma := txm.NextActiveAccount()
if ma == nil {
return nil, errors.New("Must connect and activate an account before creating a transaction")
}
return ma, nil
}
func normalizeGasParams(gasPriceWei *big.Int, gasLimit uint64, config Config) (*big.Int, uint64) {
if !config.Dev() {
return config.EthGasPriceDefault(), DefaultGasLimit
}
if gasPriceWei == nil {
gasPriceWei = config.EthGasPriceDefault()
}
if gasLimit == 0 {
gasLimit = DefaultGasLimit
}
return gasPriceWei, gasLimit
}
// createTx creates an ethereum transaction, and retries to submit the
// transaction if a nonce too low error is returned
func (txm *EthTxManager) createTx(
surrogateID null.String,
ma *ManagedAccount,
to common.Address,
data []byte,
gasPriceWei *big.Int,
gasLimit uint64,
value *assets.Eth) (*models.Tx, error) {
tx, err := txm.sendInitialTx(surrogateID, ma, to, data, gasPriceWei, gasLimit, value)
for nrc := 0; isNonceTooLowError(err); nrc++ {
logger.Warnw("Tx #0: nonce too low, retrying with network nonce")
if nrc >= nonceReloadLimit {
err = fmt.Errorf(
"Transaction reattempt limit reached for 'nonce is too low' error. Limit: %v",
nonceReloadLimit,
)
return nil, err
}
err = txm.retryInitialTx(tx, ma, gasPriceWei)
}
return tx, err
}
// sendInitialTx creates the initial Tx record + attempt for an Ethereum Tx,
// there should only ever be one of those for a "job"
func (txm *EthTxManager) sendInitialTx(
surrogateID null.String,
ma *ManagedAccount,
to common.Address,
data []byte,
gasPriceWei *big.Int,
gasLimit uint64,
value *assets.Eth) (*models.Tx, error) {
blockHeight, err := txm.getBlockNumber()
if err != nil {
return nil, errors.Wrap(err, "TxManager#sendInitialTx getBlockNumber")
}
var tx *models.Tx
err = ma.GetAndIncrementNonce(func(nonce uint64) error {
ethTx, err := txm.newEthTx(
ma.Account,
nonce,
to,
value.ToInt(),
gasLimit,
gasPriceWei,
data)
if err != nil {
return errors.Wrap(err, "TxManager#sendInitialTx newEthTx")
}
tx, err = txm.orm.CreateTx(surrogateID, ethTx, &ma.Address, blockHeight)
if err != nil {
return errors.Wrap(err, "TxManager#sendInitialTx CreateTx")
}
logger.Debugw(fmt.Sprintf("Adding Tx attempt #%d", 0), "txID", tx.ID)
_, err = txm.SendRawTx(tx.SignedRawTx)
if err != nil {
return errors.Wrap(err, "TxManager#sendInitialTx SendRawTx")
}
return nil
})
// XXX: Small subtlety here: return the tx as it is initialized and is passed to retryInitialTx
if err != nil {
err = errors.Wrap(err, "TxManager#sendInitialTx ma#GetAndIncrementNonce")
}
return tx, err
}
// retryInitialTx is used to update the Tx record and attempt for an Ethereum
// Tx when a Tx is reattempted because of a nonce too low error
func (txm *EthTxManager) retryInitialTx(
tx *models.Tx,
ma *ManagedAccount,
gasPriceWei *big.Int) error {
err := ma.ReloadNonce(txm)
if err != nil {
return errors.Wrap(err, "TxManager#retryInitialTx ReloadNonce")
}
blockHeight, err := txm.getBlockNumber()
if err != nil {
return errors.Wrap(err, "TxManager#retryInitialTx getBlockNumber")
}
err = ma.GetAndIncrementNonce(func(nonce uint64) error {
ethTx, err := txm.newEthTx(
ma.Account,
nonce,
tx.To,
tx.Value.ToInt(),
tx.GasLimit,
gasPriceWei,
tx.Data)
if err != nil {
return errors.Wrap(err, "TxManager#retryInitialTx newEthTx")
}
err = txm.orm.UpdateTx(tx, ethTx, &ma.Address, blockHeight)
if err != nil {
return errors.Wrap(err, "TxManager#retryInitialTx UpdateTx")
}
_, err = txm.SendRawTx(tx.SignedRawTx)
if err != nil {
return errors.Wrap(err, "TxManager#retryInitialTx SendRawTx")
}
return nil
})
if err != nil {
return errors.Wrap(err, "TxManager#retryInitialTx ma#GetAndIncrementNonce")
}
return nil
}
var (
nonceTooLowRegex = regexp.MustCompile("(nonce .*too low|same hash was already imported)")
)
func isNonceTooLowError(err error) bool {
return err != nil && nonceTooLowRegex.MatchString(err.Error())
}
// newEthTx returns a newly signed Ethereum Transaction
func (txm *EthTxManager) newEthTx(
account accounts.Account,
nonce uint64,
to common.Address,
amount *big.Int,
gasLimit uint64,
gasPrice *big.Int,
data []byte) (*types.Transaction, error) {
ethTx := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data)
ethTx, err := txm.keyStore.SignTx(account, ethTx, txm.config.ChainID())
if err != nil {
return nil, errors.Wrap(err, "TxManager keyStore.SignTx")
}
return ethTx, nil
}
// GetLINKBalance returns the balance of LINK at the given address
func (txm *EthTxManager) GetLINKBalance(address common.Address) (*assets.Link, error) {
contractAddress := common.HexToAddress(txm.config.LinkContractAddress())
balance, err := txm.GetERC20Balance(address, contractAddress)
if err != nil {
return assets.NewLink(0), err
}
return (*assets.Link)(balance), nil
}
// BumpGasUntilSafe process a collection of related TxAttempts, trying to get
// at least one TxAttempt into a safe state, bumping gas if needed
func (txm *EthTxManager) BumpGasUntilSafe(hash common.Hash) (*models.TxReceipt, AttemptState, error) {
blockHeight, err := txm.getBlockNumber()
if err != nil {
return nil, Unknown, errors.Wrap(err, "BumpGasUntilSafe getBlockNumber")
}
tx, _, err := txm.orm.FindTxByAttempt(hash)
if err != nil {
return nil, Unknown, errors.Wrap(err, "BumpGasUntilSafe FindTxByAttempt")
}
var merr error
for attemptIndex := range tx.Attempts {
receipt, state, err := txm.processAttempt(tx, attemptIndex, blockHeight)
if state == Safe || state == Confirmed {
return receipt, state, err // success, so all other attempt errors can be ignored.
}
merr = multierr.Append(merr, err)
}
return nil, Unconfirmed, merr
}
// ContractLINKBalance returns the balance for the contract associated with this
// withdrawal request, or any errors
func (txm *EthTxManager) ContractLINKBalance(wr models.WithdrawalRequest) (assets.Link, error) {
contractAddress := &wr.ContractAddress
if (*contractAddress == common.Address{}) {
if txm.config.OracleContractAddress() == nil {
return assets.Link{}, errors.New(
"OracleContractAddress not set; cannot check LINK balance")
}
contractAddress = txm.config.OracleContractAddress()
}
linkBalance, err := txm.GetLINKBalance(*contractAddress)
if err != nil {
return assets.Link{}, multierr.Combine(
fmt.Errorf("Could not check LINK balance for %v",
contractAddress),
err)
}
return *linkBalance, nil
}
// GetETHAndLINKBalances attempts to retrieve the ethereum node's perception of
// the latest ETH and LINK balances for the active account on the txm, or an
// error on failure.
func (txm *EthTxManager) GetETHAndLINKBalances(address common.Address) (*big.Int, *assets.Link, error) {
linkBalance, linkErr := txm.GetLINKBalance(address)
ethBalance, ethErr := txm.EthClient.GetWeiBalance(address)
merr := multierr.Append(linkErr, ethErr)
return ethBalance, linkBalance, merr
}
// WithdrawLINK withdraws the given amount of LINK from the contract to the
// configured withdrawal address. If wr.ContractAddress is empty (zero address),
// funds are withdrawn from configured OracleContractAddress.
func (txm *EthTxManager) WithdrawLINK(wr models.WithdrawalRequest) (common.Hash, error) {
oracle, err := GetContract("Oracle")
if err != nil {
return common.Hash{}, err
}
data, err := oracle.EncodeMessageCall("withdraw", wr.DestinationAddress, (*big.Int)(wr.Amount))
if err != nil {
return common.Hash{}, err
}
contractAddress := &wr.ContractAddress
if (*contractAddress == common.Address{}) {
if txm.config.OracleContractAddress() == nil {
return common.Hash{}, errors.New(
"OracleContractAddress not set; cannot withdraw")
}
contractAddress = txm.config.OracleContractAddress()
}
tx, err := txm.CreateTx(*contractAddress, data)
if err != nil {
return common.Hash{}, err
}
return tx.Hash, nil
}
// CheckAttempt retrieves a receipt for a TxAttempt, and check if it meets the
// minimum number of confirmations
func (txm *EthTxManager) CheckAttempt(txAttempt *models.TxAttempt, blockHeight uint64) (*models.TxReceipt, AttemptState, error) {
receipt, err := txm.GetTxReceipt(txAttempt.Hash)
if err != nil {
return nil, Unknown, errors.Wrap(err, "CheckAttempt GetTxReceipt failed")
}
if receipt.Unconfirmed() {
return receipt, Unconfirmed, nil
}
minimumConfirmations := new(big.Int).SetUint64(txm.config.MinOutgoingConfirmations())
confirmedAt := new(big.Int).Add(minimumConfirmations, receipt.BlockNumber.ToInt())
// 0 based indexing since receipt is 1 conf
confirmedAt.Sub(confirmedAt, big.NewInt(1))
if new(big.Int).SetUint64(blockHeight).Cmp(confirmedAt) == -1 {
return receipt, Confirmed, nil
}
return receipt, Safe, nil
}
// AttemptState enumerates the possible states of a transaction attempt as it
// gets accepted and confirmed by the blockchain
type AttemptState int
const (
// Unknown is returned when the state of a transaction could not be
// determined because of an error
Unknown AttemptState = iota
// Unconfirmed means that a transaction has had no confirmations at all
Unconfirmed
// Confirmed means that a transaftion has had at least one transaction, but
// not enough to satisfy the minimum number of confirmations configuration
// option
Confirmed
// Safe has the required number of confirmations or more
Safe
)
// String conforms to the Stringer interface for AttemptState
func (a AttemptState) String() string {
switch a {
case Unconfirmed:
return "unconfirmed"
case Confirmed:
return "confirmed"
case Safe:
return "safe"
default:
return "unknown"
}
}
// processAttempt checks the state of a transaction attempt on the blockchain
// and decides if it is safe, needs bumping or more confirmations are needed to
// decide
func (txm *EthTxManager) processAttempt(
tx *models.Tx,
attemptIndex int,
blockHeight uint64,
) (*models.TxReceipt, AttemptState, error) {
txAttempt := tx.Attempts[attemptIndex]
logger.Debugw(
fmt.Sprintf("Tx #%d checking on-chain state", attemptIndex),
"txHash", txAttempt.Hash.String(),
"txID", txAttempt.TxID,
)
receipt, state, err := txm.CheckAttempt(txAttempt, blockHeight)
if err != nil {
return nil, Unknown, errors.Wrap(err, "processAttempt CheckAttempt failed")
}
logger.Debugw(
fmt.Sprintf("Tx #%d is %s", attemptIndex, state),
"txHash", txAttempt.Hash.String(),
"txID", txAttempt.TxID,
"receiptBlockNumber", receipt.BlockNumber.ToInt(),
"currentBlockNumber", blockHeight,
"receiptHash", receipt.Hash.Hex(),
)
switch state {
case Safe:
return receipt, state, txm.handleSafe(tx, attemptIndex)
case Confirmed: // nothing to do, need to wait
return receipt, state, nil
case Unconfirmed:
if isLatestAttempt(tx, attemptIndex) && txm.hasTxAttemptMetGasBumpThreshold(tx, attemptIndex, blockHeight) {
logger.Debugw(
fmt.Sprintf("Tx #%d has met gas bump threshold, bumping gas", attemptIndex),
"txHash", txAttempt.Hash.String(),
"txID", txAttempt.TxID,
)
err = txm.bumpGas(tx, attemptIndex, blockHeight)
}
return receipt, state, err
}
panic("invariant violated, 'Unknown' state returned without error")
}
// hasTxAttemptMetGasBumpThreshold returns true if the current block height
// exceeds the configured gas bump threshold, indicating that it is time for a
// new transaction attempt to be created with an increased gas price
func (txm *EthTxManager) hasTxAttemptMetGasBumpThreshold(
tx *models.Tx,
attemptIndex int,
blockHeight uint64) bool {
gasBumpThreshold := txm.config.EthGasBumpThreshold()
txAttempt := tx.Attempts[attemptIndex]
return blockHeight >= txAttempt.SentAt+gasBumpThreshold
}
// isLatestAttempt returns true only if the attempt is the last
// attempt associated with the transaction, alluding to the fact that
// it has the highest gas price after subsequent bumps.
func isLatestAttempt(tx *models.Tx, attemptIndex int) bool {
return attemptIndex+1 == len(tx.Attempts)
}
// handleSafe marks a transaction as safe, no more work needs to be done
func (txm *EthTxManager) handleSafe(
tx *models.Tx,
attemptIndex int) error {
txAttempt := tx.Attempts[attemptIndex]
if err := txm.orm.MarkTxSafe(tx, txAttempt); err != nil {
return errors.Wrap(err, "handleSafe MarkTxSafe failed")
}
minimumConfirmations := txm.config.MinOutgoingConfirmations()
ethBalance, linkBalance, balanceErr := txm.GetETHAndLINKBalances(tx.From)
logger.Infow(
fmt.Sprintf("Tx #%d got minimum confirmations (%d)", attemptIndex, minimumConfirmations),
"txHash", txAttempt.Hash.String(),
"txID", txAttempt.TxID,
"ethBalance", ethBalance,
"linkBalance", linkBalance,
"err", balanceErr,
)
return nil
}
// bumpGas creates a new transaction attempt with an increased gas cost
func (txm *EthTxManager) bumpGas(tx *models.Tx, attemptIndex int, blockHeight uint64) error {
txAttempt := tx.Attempts[attemptIndex]
originalGasPrice := txAttempt.GasPrice.ToInt()
bumpedGasPrice := new(big.Int).Add(originalGasPrice, txm.config.EthGasBumpWei())
bumpedTxAttempt, err := txm.createAttempt(tx, bumpedGasPrice, blockHeight)
if err != nil {
return errors.Wrapf(err, "bumpGas from Tx #%s", txAttempt.Hash.Hex())
}
logger.Infow(
fmt.Sprintf("Tx #%d created with bumped gas %v", attemptIndex+1, bumpedGasPrice),
"originalTxHash", txAttempt.Hash,
"newTxHash", bumpedTxAttempt.Hash)
return nil
}
// createAttempt adds a new transaction attempt to a transaction record
func (txm *EthTxManager) createAttempt(
tx *models.Tx,
gasPriceWei *big.Int,
blockHeight uint64,
) (*models.TxAttempt, error) {
ma := txm.getAccount(tx.From)
if ma == nil {
return nil, fmt.Errorf("Unable to locate %v as an available account in EthTxManager. Has TxManager been started or has the address been removed?", tx.From.Hex())
}
etx := tx.EthTx(gasPriceWei)
etx, err := txm.keyStore.SignTx(ma.Account, etx, txm.config.ChainID())
if err != nil {
return nil, errors.Wrap(err, "createAttempt#SignTx failed")
}
logger.Debugw(fmt.Sprintf("Adding Tx attempt #%d", len(tx.Attempts)+1), "txID", tx.ID)
txAttempt, err := txm.orm.AddTxAttempt(tx, etx, blockHeight)
if err != nil {
return nil, errors.Wrap(err, "createAttempt#AddTxAttempt failed")
}
if _, err = txm.SendRawTx(txAttempt.SignedRawTx); err != nil {
return nil, errors.Wrap(err, "createAttempt#SendRawTx failed")
}
return txAttempt, nil
}
// NextActiveAccount uses round robin to select a managed account
// from the list of available accounts as defined in Register(...)
func (txm *EthTxManager) NextActiveAccount() *ManagedAccount {
txm.accountsMutex.Lock()
defer txm.accountsMutex.Unlock()
if len(txm.availableAccounts) == 0 {
return nil
}
account := txm.availableAccounts[txm.availableAccountIdx]
txm.availableAccountIdx = (txm.availableAccountIdx + 1) % len(txm.availableAccounts)
return account
}
func (txm *EthTxManager) getAccount(from common.Address) *ManagedAccount {
txm.accountsMutex.Lock()
defer txm.accountsMutex.Unlock()
for _, a := range txm.availableAccounts {
if a.Address == from {
return a
}
}
return nil
}
// ActivateAccount retrieves an account's nonce from the blockchain for client
// side management in ManagedAccount.
func (txm *EthTxManager) activateAccount(account accounts.Account) (*ManagedAccount, error) {
nonce, err := txm.GetNonce(account.Address)
if err != nil {
return nil, err
}
return NewManagedAccount(account, nonce), nil
}
// ManagedAccount holds the account information alongside a client managed nonce
// to coordinate outgoing transactions.
type ManagedAccount struct {
accounts.Account
nonce uint64
mutex *sync.Mutex
}
// NewManagedAccount creates a managed account that handles nonce increments
// locally.
func NewManagedAccount(a accounts.Account, nonce uint64) *ManagedAccount {
return &ManagedAccount{Account: a, nonce: nonce, mutex: &sync.Mutex{}}
}
// Nonce returns the client side managed nonce.
func (a *ManagedAccount) Nonce() uint64 {
return a.nonce
}
// ReloadNonce fetch and update the current nonce via eth_getTransactionCount
func (a *ManagedAccount) ReloadNonce(txm *EthTxManager) error {
a.mutex.Lock()
defer a.mutex.Unlock()
nonce, err := txm.GetNonce(a.Address)
if err != nil {
return fmt.Errorf("TxManager ReloadNonce: %v", err)
}
a.nonce = nonce
return nil
}
// GetAndIncrementNonce will Yield the current nonce to a callback function and increment it once the
// callback has finished executing
func (a *ManagedAccount) GetAndIncrementNonce(callback func(uint64) error) error {
a.mutex.Lock()
defer a.mutex.Unlock()
err := callback(a.nonce)
if err == nil {
a.nonce = a.nonce + 1
}
return err
}
func (txm *EthTxManager) getBlockNumber() (uint64, error) {
if !txm.Connected() {
return 0, errors.Wrap(ErrPendingConnection, "EthTxManager#getBlockNumber")
}
return txm.GetBlockNumber()
}
// Contract holds the solidity contract's parsed ABI
type Contract struct {
ABI abi.ABI
}
// GetContract loads the contract JSON file from ../evm/build/contracts
// and parses the ABI JSON contents into an abi.ABI object
func GetContract(name string) (*Contract, error) {
box := packr.NewBox("../../evm/build/contracts")
jsonFile, err := box.Find(name + ".json")
if err != nil {
return nil, errors.New("unable to read contract JSON")
}
abiBytes := gjson.GetBytes(jsonFile, "abi")
abiParsed, err := abi.JSON(strings.NewReader(abiBytes.Raw))
if err != nil {
return nil, err
}
return &Contract{abiParsed}, nil
}
// EncodeMessageCall encodes method name and arguments into a byte array
// to conform with the contract's ABI
func (contract *Contract) EncodeMessageCall(method string, args ...interface{}) ([]byte, error) {
return contract.ABI.Pack(method, args...)
}