-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
eth_resender.go
187 lines (161 loc) · 5.78 KB
/
eth_resender.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
package bulletprooftxmanager
import (
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/null"
"github.com/smartcontractkit/chainlink/core/services/eth"
"github.com/smartcontractkit/chainlink/core/static"
"github.com/smartcontractkit/chainlink/core/utils"
"gorm.io/gorm"
)
// pollInterval is the maximum amount of time in addition to
// EthTxResendAfterThreshold that we will wait before resending an attempt
const defaultResenderPollInterval = 5 * time.Second
// EthResender periodically picks up transactions that have been languishing
// unconfirmed for a configured amount of time without being sent, and sends
// their highest priced attempt again. This helps to defend against geth/parity
// silently dropping txes, or txes being ejected from the mempool.
//
// Previously we relied on the bumper to do this for us implicitly but there
// can occasionally be problems with this (e.g. abnormally long block times, or
// if gas bumping is disabled)
type EthResender struct {
db *gorm.DB
ethClient eth.Client
interval time.Duration
config Config
chStop chan struct{}
chDone chan struct{}
}
func NewEthResender(db *gorm.DB, ethClient eth.Client, pollInterval time.Duration, config Config) *EthResender {
if config.EthTxResendAfterThreshold() == 0 {
panic("EthResender requires a non-zero threshold")
}
return &EthResender{
db,
ethClient,
pollInterval,
config,
make(chan struct{}),
make(chan struct{}),
}
}
func (er *EthResender) Start() {
logger.Infof("EthResender: Enabled with poll interval of %s and age threshold of %s", er.interval, er.config.EthTxResendAfterThreshold())
go er.runLoop()
}
func (er *EthResender) Stop() {
close(er.chStop)
<-er.chDone
}
func (er *EthResender) runLoop() {
defer close(er.chDone)
if err := er.resendUnconfirmed(); err != nil {
logger.Warnw("EthResender: failed to resend unconfirmed transactions", "err", err)
}
ticker := time.NewTicker(utils.WithJitter(er.interval))
defer ticker.Stop()
for {
select {
case <-er.chStop:
return
case <-ticker.C:
if err := er.resendUnconfirmed(); err != nil {
logger.Warnw("EthResender: failed to resend unconfirmed transactions", "err", err)
}
}
}
}
func (er *EthResender) resendUnconfirmed() error {
ageThreshold := er.config.EthTxResendAfterThreshold()
maxInFlightTransactions := er.config.EvmMaxInFlightTransactions()
olderThan := time.Now().Add(-ageThreshold)
attempts, err := FindEthTxesRequiringResend(er.db, olderThan, maxInFlightTransactions)
if err != nil {
return errors.Wrap(err, "failed to findEthTxAttemptsRequiringReceiptFetch")
}
if len(attempts) == 0 {
return nil
}
logger.Infow(fmt.Sprintf("EthResender: re-sending %d unconfirmed transactions that were last sent over %s ago. These transactions are taking longer than usual to be mined. %s", len(attempts), ageThreshold, static.EthNodeConnectivityProblemLabel), "n", len(attempts))
reqs := make([]rpc.BatchElem, len(attempts))
ethTxIDs := make([]int64, len(attempts))
for i, attempt := range attempts {
ethTxIDs[i] = attempt.EthTxID
req := rpc.BatchElem{
Method: "eth_sendRawTransaction",
Args: []interface{}{hexutil.Encode(attempt.SignedRawTx)},
Result: &common.Hash{},
}
reqs[i] = req
}
now := time.Now()
batchSize := int(er.config.EvmRPCDefaultBatchSize())
if batchSize == 0 {
batchSize = len(reqs)
}
for i := 0; i < len(reqs); i += batchSize {
j := i + batchSize
if j > len(reqs) {
j = len(reqs)
}
logger.Debugw(fmt.Sprintf("EthResender: batch resending transactions %v thru %v", i, j))
ctx, cancel := eth.DefaultQueryCtx()
if err := er.ethClient.RoundRobinBatchCallContext(ctx, reqs[i:j]); err != nil {
return errors.Wrap(err, "failed to re-send transactions")
}
cancel()
if err := er.updateBroadcastAts(now, ethTxIDs[i:j]); err != nil {
return errors.Wrap(err, "failed to update last succeeded on attempts")
}
}
logResendResult(reqs)
return nil
}
// FindEthTxesRequiringResend returns the highest priced attempt for each
// eth_tx that was last sent before or at the given time (up to limit)
func FindEthTxesRequiringResend(db *gorm.DB, olderThan time.Time, maxInFlightTransactions uint32) (attempts []EthTxAttempt, err error) {
var limit null.Uint32
if maxInFlightTransactions > 0 {
limit = null.Uint32From(maxInFlightTransactions)
}
err = db.Raw(`
SELECT DISTINCT ON (eth_tx_id) eth_tx_attempts.*
FROM eth_tx_attempts
JOIN eth_txes ON eth_txes.id = eth_tx_attempts.eth_tx_id AND eth_txes.state IN ('unconfirmed', 'confirmed_missing_receipt')
WHERE eth_tx_attempts.state <> 'in_progress' AND eth_txes.broadcast_at <= ?
ORDER BY eth_tx_attempts.eth_tx_id ASC, eth_txes.nonce ASC, eth_tx_attempts.gas_price DESC
LIMIT ?
`, olderThan, limit).
Find(&attempts).Error
return
}
func (er *EthResender) updateBroadcastAts(now time.Time, etxIDs []int64) error {
// Deliberately do nothing on NULL broadcast_at because that indicates the
// tx has been moved into a state where broadcast_at is not relevant, e.g.
// fatally errored.
//
// Since we may have raced with the EthConfirmer (totally OK since highest
// priced transaction always wins) we only want to update broadcast_at if
// our version is later.
return er.db.Exec(`UPDATE eth_txes SET broadcast_at = ? WHERE id = ANY(?) AND broadcast_at < ?`, now, pq.Array(etxIDs), now).Error
}
func logResendResult(reqs []rpc.BatchElem) {
var nNew int
var nFatal int
for _, req := range reqs {
serr := eth.NewSendError(req.Error)
if serr == nil {
nNew++
} else if serr.Fatal() {
nFatal++
}
}
logger.Debugw("EthResender: completed", "n", len(reqs), "nNew", nNew, "nFatal", nFatal)
}