-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
common.go
85 lines (73 loc) · 2.66 KB
/
common.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
package txmgr
import (
"context"
"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/sqlx"
evmclient "github.com/smartcontractkit/chainlink/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/core/logger"
)
// timeout value for batchSendTransactions
const batchSendTransactionTimeout = 30 * time.Second
// Tries to send transactions in batches. Even if some batch(es) fail to get sent, it tries all remaining batches,
// before returning with error for the latest batch send. If a batch send fails, this sets the error on all
// elements in that batch.
func batchSendTransactions(
ctx context.Context,
db *sqlx.DB,
attempts []EthTxAttempt,
batchSize int,
logger logger.Logger,
ethClient evmclient.Client) ([]rpc.BatchElem, error) {
if len(attempts) == 0 {
return nil, nil
}
reqs := make([]rpc.BatchElem, len(attempts))
ethTxIDs := make([]int64, len(attempts))
hashes := make([]common.Hash, len(attempts))
for i, attempt := range attempts {
ethTxIDs[i] = attempt.EthTxID
hashes[i] = attempt.Hash
req := rpc.BatchElem{
Method: "eth_sendRawTransaction",
Args: []interface{}{hexutil.Encode(attempt.SignedRawTx)},
Result: &common.Hash{},
}
reqs[i] = req
}
logger.Debugw(fmt.Sprintf("Batch sending %d unconfirmed transactions.", len(attempts)), "n", len(attempts), "ethTxIDs", ethTxIDs, "hashes", hashes)
now := time.Now()
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("Batch sending transactions %v thru %v", i, j))
if err := ethClient.BatchCallContextAll(ctx, reqs[i:j]); err != nil {
return reqs, errors.Wrap(err, "failed to batch send transactions")
}
if err := updateBroadcastAts(db, now, ethTxIDs[i:j]); err != nil {
return reqs, errors.Wrap(err, "failed to update last succeeded on attempts")
}
}
return reqs, nil
}
func updateBroadcastAts(db *sqlx.DB, 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 EthConfirmer/EthResender can race (totally OK since highest
// priced transaction always wins) we only want to update broadcast_at if
// our version is later.
_, err := db.Exec(`UPDATE eth_txes SET broadcast_at = $1 WHERE id = ANY($2) AND broadcast_at < $1`, now, pq.Array(etxIDs))
return errors.Wrap(err, "updateBroadcastAts failed to update eth_txes")
}