-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
common.go
89 lines (77 loc) · 2.67 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
86
87
88
89
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/pkg/errors"
txmgrtypes "github.com/smartcontractkit/chainlink/v2/common/txmgr/types"
"github.com/smartcontractkit/chainlink/v2/common/types"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/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[
CHAIN_ID txmgrtypes.ID,
ADDR types.Hashable,
TX_HASH types.Hashable,
BLOCK_HASH types.Hashable,
R txmgrtypes.ChainReceipt[TX_HASH, BLOCK_HASH],
SEQ txmgrtypes.Sequence,
FEE txmgrtypes.Fee,
ADD any,
](
ctx context.Context,
txStore txmgrtypes.TxStore[ADDR, CHAIN_ID, TX_HASH, BLOCK_HASH, R, SEQ, FEE, ADD],
attempts []txmgrtypes.TxAttempt[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE, ADD],
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([]string, len(attempts))
for i, attempt := range attempts {
ethTxIDs[i] = attempt.TxID
hashes[i] = attempt.Hash.String()
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 := txStore.UpdateBroadcastAts(now, ethTxIDs[i:j]); err != nil {
return reqs, errors.Wrap(err, "failed to update last succeeded on attempts")
}
}
return reqs, nil
}
func stringToGethAddress(s string) (common.Address, error) {
if !common.IsHexAddress(s) {
return common.Address{}, fmt.Errorf("invalid hex address: %s", s)
}
return common.HexToAddress(s), nil
}