forked from smartcontractkit/chainlink
/
bulletprooftxmanager.go
541 lines (471 loc) · 19 KB
/
bulletprooftxmanager.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
package bulletprooftxmanager
import (
"bytes"
"context"
"fmt"
"math/big"
"sync"
"time"
"github.com/DCMMC/chainlink/core/assets"
"github.com/DCMMC/chainlink/core/chains"
"github.com/DCMMC/chainlink/core/logger"
"github.com/DCMMC/chainlink/core/null"
"github.com/DCMMC/chainlink/core/service"
"github.com/DCMMC/chainlink/core/services/eth"
"github.com/DCMMC/chainlink/core/services/gas"
httypes "github.com/DCMMC/chainlink/core/services/headtracker/types"
"github.com/DCMMC/chainlink/core/services/keystore/keys/ethkey"
"github.com/DCMMC/chainlink/core/services/postgres"
"github.com/DCMMC/chainlink/core/static"
"github.com/DCMMC/chainlink/core/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
gethTypes "github.com/ethereum/go-ethereum/core/types"
exchainutils "github.com/okex/exchain-ethereum-compatible/utils"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"gorm.io/gorm"
)
// Config encompasses config used by bulletprooftxmanager package
// Unless otherwise specified, these should support changing at runtime
//go:generate mockery --recursive --name Config --output ./mocks/ --case=underscore --structname Config --filename config.go
type Config interface {
gas.Config
EthTxReaperInterval() time.Duration
EthTxReaperThreshold() time.Duration
EthTxResendAfterThreshold() time.Duration
EvmGasBumpThreshold() uint64
EvmGasBumpTxDepth() uint16
EvmGasLimitDefault() uint64
EvmMaxInFlightTransactions() uint32
EvmMaxQueuedTransactions() uint64
EvmNonceAutoSync() bool
EvmRPCDefaultBatchSize() uint32
KeySpecificMaxGasPriceWei(addr common.Address) *big.Int
TriggerFallbackDBPollInterval() time.Duration
}
// KeyStore encompasses the subset of keystore used by bulletprooftxmanager
type KeyStore interface {
GetStatesForChain(chainID *big.Int) ([]ethkey.State, error)
SignTx(fromAddress common.Address, tx *gethTypes.Transaction, chainID *big.Int) (*gethTypes.Transaction, error)
SubscribeToKeyChanges() (ch chan struct{}, unsub func())
}
// For more information about the BulletproofTxManager architecture, see the design doc:
// https://www.notion.so/chainlink/BulletproofTxManager-Architecture-Overview-9dc62450cd7a443ba9e7dceffa1a8d6b
var _ TxManager = &BulletproofTxManager{}
// ResumeCallback is assumed to be idempotent
type ResumeCallback func(id uuid.UUID, result interface{}, err error) error
//go:generate mockery --recursive --name TxManager --output ./mocks/ --case=underscore --structname TxManager --filename tx_manager.go
type TxManager interface {
httypes.HeadTrackable
service.Service
Trigger(addr common.Address)
CreateEthTransaction(db *gorm.DB, newTx NewTx) (etx EthTx, err error)
GetGasEstimator() gas.Estimator
RegisterResumeCallback(fn ResumeCallback)
}
type BulletproofTxManager struct {
utils.StartStopOnce
logger logger.Logger
db *gorm.DB
ethClient eth.Client
config Config
keyStore KeyStore
eventBroadcaster postgres.EventBroadcaster
gasEstimator gas.Estimator
chainID big.Int
chHeads chan eth.Head
trigger chan common.Address
resumeCallback ResumeCallback
chStop chan struct{}
chSubbed chan struct{}
wg sync.WaitGroup
reaper *Reaper
ethResender *EthResender
}
func (b *BulletproofTxManager) RegisterResumeCallback(fn ResumeCallback) {
b.resumeCallback = fn
}
func NewBulletproofTxManager(db *gorm.DB, ethClient eth.Client, config Config, keyStore KeyStore, eventBroadcaster postgres.EventBroadcaster, lggr logger.Logger) *BulletproofTxManager {
b := BulletproofTxManager{
StartStopOnce: utils.StartStopOnce{},
logger: lggr,
db: db,
ethClient: ethClient,
config: config,
keyStore: keyStore,
eventBroadcaster: eventBroadcaster,
gasEstimator: gas.NewEstimator(lggr, ethClient, config),
chainID: *ethClient.ChainID(),
chHeads: make(chan eth.Head),
trigger: make(chan common.Address),
chStop: make(chan struct{}),
chSubbed: make(chan struct{}),
}
if config.EthTxResendAfterThreshold() > 0 {
b.ethResender = NewEthResender(lggr, db, ethClient, defaultResenderPollInterval, config)
} else {
b.logger.Info("EthResender: Disabled")
}
if config.EthTxReaperThreshold() > 0 {
b.reaper = NewReaper(lggr, db, config, *ethClient.ChainID())
} else {
b.logger.Info("EthTxReaper: Disabled")
}
return &b
}
func (b *BulletproofTxManager) Start() (merr error) {
return b.StartOnce("BulletproofTxManager", func() error {
keyStates, err := b.keyStore.GetStatesForChain(&b.chainID)
if err != nil {
return errors.Wrap(err, "BulletproofTxManager: failed to load key states")
}
if len(keyStates) > 0 {
b.logger.Debugw(fmt.Sprintf("BulletproofTxManager: booting with %d keys", len(keyStates)), "keys", keyStates)
} else {
b.logger.Warnf("BulletproofTxManager: chain %s does not have any eth keys, no transactions will be sent on this chain", b.chainID.String())
}
eb := NewEthBroadcaster(b.db, b.ethClient, b.config, b.keyStore, b.eventBroadcaster, keyStates, b.gasEstimator, b.resumeCallback, b.logger)
ec := NewEthConfirmer(b.db, b.ethClient, b.config, b.keyStore, keyStates, b.gasEstimator, b.resumeCallback, b.logger)
if err := eb.Start(); err != nil {
return errors.Wrap(err, "BulletproofTxManager: EthBroadcaster failed to start")
}
if err := ec.Start(); err != nil {
return errors.Wrap(err, "BulletproofTxManager: EthConfirmer failed to start")
}
if err := b.gasEstimator.Start(); err != nil {
return errors.Wrap(err, "BulletproofTxManager: Estimator failed to start")
}
b.wg.Add(1)
go b.runLoop(eb, ec)
<-b.chSubbed
if b.reaper != nil {
b.reaper.Start()
}
if b.ethResender != nil {
b.ethResender.Start()
}
return nil
})
}
func (b *BulletproofTxManager) Close() (merr error) {
return b.StopOnce("BulletproofTxManager", func() error {
close(b.chStop)
if b.reaper != nil {
b.reaper.Stop()
}
if b.ethResender != nil {
b.ethResender.Stop()
}
b.wg.Wait()
b.gasEstimator.Close()
return nil
})
}
func (b *BulletproofTxManager) runLoop(eb *EthBroadcaster, ec *EthConfirmer) {
defer b.wg.Done()
keysChanged, unsub := b.keyStore.SubscribeToKeyChanges()
defer unsub()
close(b.chSubbed)
for {
select {
case address := <-b.trigger:
eb.Trigger(address)
case head := <-b.chHeads:
ec.mb.Deliver(head)
case <-b.chStop:
b.logger.ErrorIfCalling(eb.Close)
b.logger.ErrorIfCalling(ec.Close)
return
case <-keysChanged:
keyStates, err := b.keyStore.GetStatesForChain(&b.chainID)
if err != nil {
b.logger.Errorf("BulletproofTxManager: failed to reload key states after key change")
continue
}
b.logger.Debugw("BulletproofTxManager: keys changed, reloading", "keyStates", keyStates)
b.logger.ErrorIfCalling(eb.Close)
b.logger.ErrorIfCalling(ec.Close)
eb = NewEthBroadcaster(b.db, b.ethClient, b.config, b.keyStore, b.eventBroadcaster, keyStates, b.gasEstimator, b.resumeCallback, b.logger)
ec = NewEthConfirmer(b.db, b.ethClient, b.config, b.keyStore, keyStates, b.gasEstimator, b.resumeCallback, b.logger)
b.logger.ErrorIfCalling(eb.Start)
b.logger.ErrorIfCalling(ec.Start)
}
}
}
// OnNewLongestChain conforms to HeadTrackable
func (b *BulletproofTxManager) OnNewLongestChain(ctx context.Context, head eth.Head) {
ok := b.IfStarted(func() {
if b.reaper != nil {
b.reaper.SetLatestBlockNum(head.Number)
}
b.gasEstimator.OnNewLongestChain(ctx, head)
select {
case b.chHeads <- head:
case <-ctx.Done():
b.logger.Errorw("BulletproofTxManager: timed out handling head", "blockNum", head.Number, "ctxErr", ctx.Err())
}
})
if !ok {
b.logger.Debugw("BulletproofTxManager: not started; ignoring head", "head", head, "state", b.State())
}
}
// Trigger forces the EthBroadcaster to check early for the given address
func (b *BulletproofTxManager) Trigger(addr common.Address) {
select {
case b.trigger <- addr:
default:
}
}
type NewTx struct {
FromAddress common.Address
ToAddress common.Address
EncodedPayload []byte
GasLimit uint64
Meta *EthTxMeta
MinConfirmations null.Uint32
PipelineTaskRunID *uuid.UUID
Strategy TxStrategy
}
// CreateEthTransaction inserts a new transaction
func (b *BulletproofTxManager) CreateEthTransaction(db *gorm.DB, newTx NewTx) (etx EthTx, err error) {
err = CheckEthTxQueueCapacity(db, newTx.FromAddress, b.config.EvmMaxQueuedTransactions(), b.chainID)
if err != nil {
return etx, errors.Wrap(err, "BulletproofTxManager#CreateEthTransaction")
}
value := 0
err = postgres.GormTransactionWithDefaultContext(db, func(tx *gorm.DB) error {
if newTx.PipelineTaskRunID != nil {
err = tx.Raw(`SELECT * FROM eth_txes WHERE pipeline_task_run_id = ? AND evm_chain_id = ?`, newTx.PipelineTaskRunID, b.chainID.String()).Scan(&etx).Error
if err != nil {
return errors.Wrap(err, "BulletproofTxManager#CreateEthTransaction")
}
// if a previous transaction for this task run exists, immediately return it
if etx.ID != 0 {
return nil
}
}
if err = b.checkStateExists(tx, newTx.FromAddress); err != nil {
return err
}
res := tx.Raw(`
INSERT INTO eth_txes (from_address, to_address, encoded_payload, value, gas_limit, state, created_at, meta, subject, evm_chain_id, min_confirmations, pipeline_task_run_id, simulate)
VALUES (
?,?,?,?,?,'unstarted',NOW(),?,?,?,?,?,?
)
RETURNING "eth_txes".*
`, newTx.FromAddress, newTx.ToAddress, newTx.EncodedPayload, value, newTx.GasLimit, newTx.Meta, newTx.Strategy.Subject(), b.chainID.String(), newTx.MinConfirmations, newTx.PipelineTaskRunID, newTx.Strategy.Simulate()).Scan(&etx)
err = res.Error
if err != nil {
return errors.Wrap(err, "BulletproofTxManager#CreateEthTransaction failed to insert eth_tx")
}
pruned, err := newTx.Strategy.PruneQueue(tx)
if err != nil {
return errors.Wrap(err, "BulletproofTxManager#CreateEthTransaction failed to prune eth_txes")
}
if pruned > 0 {
b.logger.Warnw(fmt.Sprintf("BulletproofTxManager: dropped %d old transactions from transaction queue", pruned), "fromAddress", newTx.FromAddress, "toAddress", newTx.ToAddress, "meta", newTx.Meta, "subject", newTx.Strategy.Subject(), "replacementID", etx.ID)
}
return nil
})
return
}
func (b *BulletproofTxManager) checkStateExists(tx *gorm.DB, addr common.Address) error {
var state ethkey.State
err := tx.First(&state, "address = ?", addr).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.Errorf("no eth key exists with address %s", addr.Hex())
} else if err != nil {
return errors.Wrap(err, "failed to query state")
}
if state.EVMChainID.Cmp(utils.NewBig(&b.chainID)) != 0 {
return errors.Errorf("cannot send transaction on chain ID %s; eth key with address %s is pegged to chain ID %s", b.chainID.String(), addr.Hex(), state.EVMChainID.String())
}
return nil
}
// GetGasEstimator returns the gas estimator, mostly useful for tests
func (b *BulletproofTxManager) GetGasEstimator() gas.Estimator {
return b.gasEstimator
}
// SendEther creates a transaction that transfers the given value of ether
func SendEther(db *gorm.DB, chainID *big.Int, from, to common.Address, value assets.Eth, gasLimit uint64) (etx EthTx, err error) {
if to == utils.ZeroAddress {
return etx, errors.New("cannot send ether to zero address")
}
etx = EthTx{
FromAddress: from,
ToAddress: to,
EncodedPayload: []byte{},
Value: value,
GasLimit: gasLimit,
State: EthTxUnstarted,
EVMChainID: *utils.NewBig(chainID),
}
err = db.Create(&etx).Error
return etx, err
}
type ChainKeyStore struct {
chainID big.Int
config Config
keystore KeyStore
}
func NewChainKeyStore(chainID big.Int, config Config, keystore KeyStore) ChainKeyStore {
return ChainKeyStore{chainID, config, keystore}
}
func (c *ChainKeyStore) SignTx(address common.Address, tx *gethTypes.Transaction) (common.Hash, []byte, error) {
signedTx, err := c.keystore.SignTx(address, tx, &c.chainID)
if err != nil {
return common.Hash{}, nil, errors.Wrap(err, "SignTx failed")
}
rlp := new(bytes.Buffer)
if err = signedTx.EncodeRLP(rlp); err != nil {
return common.Hash{}, nil, errors.Wrap(err, "SignTx failed")
}
var hash common.Hash
hash, err = signedTxHash(signedTx, c.config.ChainType())
if err != nil {
return hash, nil, err
}
return hash, rlp.Bytes(), nil
}
func signedTxHash(signedTx *gethTypes.Transaction, chainType chains.ChainType) (hash common.Hash, err error) {
if chainType == chains.ExChain {
hash, err = exchainutils.Hash(signedTx)
if err != nil {
return hash, errors.Wrap(err, "error getting signed tx hash from exchain")
}
} else {
hash = signedTx.Hash()
}
return hash, nil
}
// send broadcasts the transaction to the ethereum network, writes any relevant
// data onto the attempt and returns an error (or nil) depending on the status
func sendTransaction(ctx context.Context, ethClient eth.Client, a EthTxAttempt, e EthTx, logger logger.Logger) *eth.SendError {
signedTx, err := a.GetSignedTx()
if err != nil {
return eth.NewFatalSendError(err)
}
ctx, cancel := eth.DefaultQueryCtx(ctx)
defer cancel()
err = ethClient.SendTransaction(ctx, signedTx)
err = errors.WithStack(err)
a.EthTx = e // for logging
logger.Debugw("BulletproofTxManager: Sent transaction", "ethTxAttemptID", a.ID, "txHash", a.Hash, "err", err, "meta", e.Meta, "gasLimit", e.GasLimit, "attempt", a)
sendErr := eth.NewSendError(err)
if sendErr.IsTransactionAlreadyInMempool() {
logger.Debugw("transaction already in mempool", "txHash", a.Hash, "nodeErr", sendErr.Error())
return nil
}
return eth.NewSendError(err)
}
// gimulateTransaction pretends to "send" the transaction using eth_call
// returns error on revert
func simulateTransaction(ctx context.Context, ethClient eth.Client, a EthTxAttempt, e EthTx) (hexutil.Bytes, error) {
ctx, cancel := eth.DefaultQueryCtx(ctx)
defer cancel()
// See: https://github.com/ethereum/go-ethereum/blob/acdf9238fb03d79c9b1c20c2fa476a7e6f4ac2ac/ethclient/gethclient/gethclient.go#L193
callArg := map[string]interface{}{
"from": e.FromAddress,
"to": &e.ToAddress,
"gas": hexutil.Uint64(a.ChainSpecificGasLimit),
// NOTE: Deliberately do not include gas prices. We never want to fatally error a transaction just because the wallet has insufficient eth.
// Relevant info regarding EIP1559 transactions: https://github.com/ethereum/go-ethereum/pull/23027
"gasPrice": nil,
"maxFeePerGas": nil,
"maxPriorityFeePerGas": nil,
"value": (*hexutil.Big)(e.Value.ToInt()),
"data": hexutil.Bytes(e.EncodedPayload),
}
var b hexutil.Bytes
baseErr := ethClient.CallContext(ctx, &b, "eth_call", callArg, eth.ToBlockNumArg(nil)) // always run simulation on "latest" block
return b, errors.Wrap(baseErr, "transaction simulation using eth_call failed")
}
// sendEmptyTransaction sends a transaction with 0 Eth and an empty payload to the burn address
// May be useful for clearing stuck nonces
func sendEmptyTransaction(
ethClient eth.Client,
keyStore KeyStore,
nonce uint64,
gasLimit uint64,
gasPriceWei *big.Int,
fromAddress common.Address,
chainID *big.Int,
) (_ *gethTypes.Transaction, err error) {
defer utils.WrapIfError(&err, "sendEmptyTransaction failed")
signedTx, err := makeEmptyTransaction(keyStore, nonce, gasLimit, gasPriceWei, fromAddress, chainID)
if err != nil {
return nil, err
}
ctx, cancel := eth.DefaultQueryCtx()
defer cancel()
err = ethClient.SendTransaction(ctx, signedTx)
return signedTx, err
}
// makes a transaction that sends 0 eth to self
func makeEmptyTransaction(keyStore KeyStore, nonce uint64, gasLimit uint64, gasPriceWei *big.Int, fromAddress common.Address, chainID *big.Int) (*gethTypes.Transaction, error) {
value := big.NewInt(0)
payload := []byte{}
tx := gethTypes.NewTransaction(nonce, fromAddress, value, gasLimit, gasPriceWei, payload)
return keyStore.SignTx(fromAddress, tx, chainID)
}
func saveReplacementInProgressAttempt(db *gorm.DB, oldAttempt EthTxAttempt, replacementAttempt *EthTxAttempt) error {
if oldAttempt.State != EthTxAttemptInProgress || replacementAttempt.State != EthTxAttemptInProgress {
return errors.New("expected attempts to be in_progress")
}
if oldAttempt.ID == 0 {
return errors.New("expected oldAttempt to have an ID")
}
return postgres.GormTransactionWithDefaultContext(db, func(tx *gorm.DB) error {
if err := tx.Exec(`DELETE FROM eth_tx_attempts WHERE id = ? `, oldAttempt.ID).Error; err != nil {
return errors.Wrap(err, "saveReplacementInProgressAttempt failed")
}
return errors.Wrap(tx.Create(replacementAttempt).Error, "saveReplacementInProgressAttempt failed")
})
}
// CountUnconfirmedTransactions returns the number of unconfirmed transactions
func CountUnconfirmedTransactions(db *gorm.DB, fromAddress common.Address, chainID big.Int) (count uint32, err error) {
return countTransactionsWithState(db, fromAddress, EthTxUnconfirmed, chainID)
}
// CountUnstartedTransactions returns the number of unconfirmed transactions
func CountUnstartedTransactions(db *gorm.DB, fromAddress common.Address, chainID big.Int) (count uint32, err error) {
return countTransactionsWithState(db, fromAddress, EthTxUnstarted, chainID)
}
func countTransactionsWithState(db *gorm.DB, fromAddress common.Address, state EthTxState, chainID big.Int) (count uint32, err error) {
ctx, cancel := postgres.DefaultQueryCtx()
defer cancel()
err = db.WithContext(ctx).Raw(`SELECT count(*) FROM eth_txes WHERE from_address = ? AND state = ? AND evm_chain_id = ?`, fromAddress, state, chainID.String()).Scan(&count).Error
return
}
// CheckEthTxQueueCapacity returns an error if inserting this transaction would
// exceed the maximum queue size.
func CheckEthTxQueueCapacity(db *gorm.DB, fromAddress common.Address, maxQueuedTransactions uint64, chainID big.Int) (err error) {
if maxQueuedTransactions == 0 {
return nil
}
var count uint64
err = db.Raw(`SELECT count(*) FROM eth_txes WHERE from_address = ? AND state = 'unstarted' AND evm_chain_id = ?`, fromAddress, chainID.String()).Scan(&count).Error
if err != nil {
err = errors.Wrap(err, "bulletprooftxmanager.CheckEthTxQueueCapacity query failed")
return
}
if count >= maxQueuedTransactions {
err = errors.Errorf("cannot create transaction; too many unstarted transactions in the queue (%v/%v). %s", count, maxQueuedTransactions, static.EvmMaxQueuedTransactionsLabel)
}
return
}
var _ TxManager = &NullTxManager{}
type NullTxManager struct {
ErrMsg string
}
func (n *NullTxManager) OnNewLongestChain(context.Context, eth.Head) {}
func (n *NullTxManager) Start() error { return nil }
func (n *NullTxManager) Close() error { return nil }
func (n *NullTxManager) Trigger(common.Address) { panic(n.ErrMsg) }
func (n *NullTxManager) CreateEthTransaction(*gorm.DB, NewTx) (etx EthTx, err error) {
return etx, errors.New(n.ErrMsg)
}
func (n *NullTxManager) Healthy() error { return nil }
func (n *NullTxManager) Ready() error { return nil }
func (n *NullTxManager) GetGasEstimator() gas.Estimator { return nil }
func (n *NullTxManager) RegisterResumeCallback(fn ResumeCallback) {}