/
config.go
309 lines (263 loc) · 10.2 KB
/
config.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
package txmgr
import (
"context"
"crypto/ecdsa"
"math/big"
"time"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
type DefaultFlagValues struct {
NumConfirmations uint64
SafeAbortNonceTooLowCount uint64
FeeLimitMultiplier uint64
FeeLimitThresholdGwei float64
ResubmissionTimeout time.Duration
NetworkTimeout time.Duration
TxSendTimeout time.Duration
TxNotInMempoolTimeout time.Duration
}
type CLIConfig struct {
ChainID uint64
NumConfirmations uint64
SafeAbortNonceTooLowCount uint64
FeeLimitMultiplier uint64
FeeLimitThresholdGwei float64
MinBaseFeeGwei float64
MinTipCapGwei float64
ResubmissionTimeout time.Duration
ReceiptQueryInterval time.Duration
NetworkTimeout time.Duration
TxSendTimeout time.Duration
TxNotInMempoolTimeout time.Duration
}
var (
//nolint:gochecknoglobals // should be configurable
DefaultSenderFlagValues = DefaultFlagValues{
NumConfirmations: uint64(1),
SafeAbortNonceTooLowCount: uint64(3),
FeeLimitMultiplier: uint64(5),
FeeLimitThresholdGwei: 100.0,
ResubmissionTimeout: 48 * time.Second,
NetworkTimeout: 30 * time.Second,
TxSendTimeout: 0 * time.Second,
TxNotInMempoolTimeout: 2 * time.Minute,
}
)
func NewCLIConfig(chainID uint64, interval time.Duration, defaults DefaultFlagValues) CLIConfig {
return CLIConfig{
NumConfirmations: defaults.NumConfirmations,
SafeAbortNonceTooLowCount: defaults.SafeAbortNonceTooLowCount,
FeeLimitMultiplier: defaults.FeeLimitMultiplier,
FeeLimitThresholdGwei: defaults.FeeLimitThresholdGwei,
ResubmissionTimeout: defaults.ResubmissionTimeout,
NetworkTimeout: defaults.NetworkTimeout,
TxSendTimeout: defaults.TxSendTimeout,
TxNotInMempoolTimeout: defaults.TxNotInMempoolTimeout,
ReceiptQueryInterval: interval,
ChainID: chainID,
}
}
func (m CLIConfig) Check() error {
if m.NumConfirmations == 0 {
return errors.New("numConfirmations must not be 0")
}
if m.NetworkTimeout == 0 {
return errors.New("must provide NetworkTimeout")
}
if m.FeeLimitMultiplier == 0 {
return errors.New("must provide FeeLimitMultiplier")
}
if m.MinBaseFeeGwei < m.MinTipCapGwei {
return errors.New("minBaseFee smaller than minTipCap",
m.MinBaseFeeGwei, m.MinTipCapGwei)
}
if m.ResubmissionTimeout == 0 {
return errors.New("must provide ResubmissionTimeout")
}
if m.ReceiptQueryInterval == 0 {
return errors.New("must provide ReceiptQueryInterval")
}
if m.TxNotInMempoolTimeout == 0 {
return errors.New("must provide TxNotInMempoolTimeout")
}
if m.SafeAbortNonceTooLowCount == 0 {
return errors.New("safeAbortNonceTooLowCount must not be 0")
}
return nil
}
func externalSignerFn(external ExternalSigner, address common.Address, chainID uint64) SignerFn {
signer := types.LatestSignerForChainID(big.NewInt(int64(chainID)))
return func(ctx context.Context, from common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != from {
return nil, bind.ErrNotAuthorized
}
sig, err := external(ctx, signer.Hash(tx), from)
if err != nil {
return nil, errors.Wrap(err, "external sign")
}
res, err := tx.WithSignature(signer, sig[:])
if err != nil {
return nil, errors.Wrap(err, "set signature")
}
return res, nil
}
}
// privateKeySignerFn returns a SignerFn that signs transactions with the given private key.
func privateKeySignerFn(key *ecdsa.PrivateKey, chainID uint64) SignerFn {
from := crypto.PubkeyToAddress(key.PublicKey)
signer := types.LatestSignerForChainID(big.NewInt(int64(chainID)))
return func(_ context.Context, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != from {
return nil, bind.ErrNotAuthorized
}
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
if err != nil {
return nil, errors.Wrap(err, "could not sign transaction")
}
res, err := tx.WithSignature(signer, signature)
if err != nil {
return nil, errors.Wrap(err, "could not sign transaction")
}
return res, nil
}
}
// ExternalSigner is a function that signs a transaction with a given address and returns the signature.
type ExternalSigner func(context.Context, common.Hash, common.Address) ([65]byte, error)
// SignerFn is a generic transaction signing function. It may be a remote signer so it takes a context.
// It also takes the address that should be used to sign the transaction with.
type SignerFn func(context.Context, common.Address, *types.Transaction) (*types.Transaction, error)
// SignerFactory creates a SignerFn that is bound to a specific chainID.
type SignerFactory func(chainID *big.Int) SignerFn
// Config houses parameters for altering the behavior of a simple.
type Config struct {
Backend ethclient.Client
// ResubmissionTimeout is the interval at which, if no previously
// published transaction has been mined, the new tx with a bumped gas
// price will be published. Only one publication at MaxGasPrice will be
// attempted.
ResubmissionTimeout time.Duration
// The multiplier applied to fee suggestions to put a hard limit on fee increases.
FeeLimitMultiplier uint64
// Minimum threshold (in Wei) at which the FeeLimitMultiplier takes effect.
// On low-fee networks, like test networks, this allows for arbitrary fee bumps
// below this threshold.
FeeLimitThreshold *big.Int
// Minimum base fee (in Wei) to assume when determining tx fees.
MinBaseFee *big.Int
// Minimum tip cap (in Wei) to enforce when determining tx fees.
MinTipCap *big.Int
// ChainID is the chain ID of the L1 chain.
ChainID *big.Int
// TxSendTimeout is how long to wait for sending a transaction.
// By default, it is unbounded. If set, this is recommended to be at least 20 minutes.
TxSendTimeout time.Duration
// TxNotInMempoolTimeout is how long to wait before aborting a transaction doSend if the transaction does not
// make it to the mempool. If the tx is in the mempool, TxSendTimeout is used instead.
TxNotInMempoolTimeout time.Duration
// NetworkTimeout is the allowed duration for a single network request.
// This is intended to be used for network requests that can be replayed.
// todo(lazar): this should be handled by eth client
NetworkTimeout time.Duration
// RequireQueryInterval is the interval at which the tx manager will
// query the backend to check for confirmations after a tx at a
// specific gas price has been published.
ReceiptQueryInterval time.Duration
// NumConfirmations specifies how many blocks are need to consider a
// transaction confirmed.
NumConfirmations uint64
// SafeAbortNonceTooLowCount specifies how many ErrNonceTooLow observations
// are required to give up on a tx at a particular nonce without receiving
// confirmation.
SafeAbortNonceTooLowCount uint64
// Signer is used to sign transactions when the gas price is increased.
Signer SignerFn
From common.Address
}
// NewConfigWithSigner returns a new txmgr config from the given CLI config and external signer.
func NewConfigWithSigner(cfg CLIConfig, external ExternalSigner, from common.Address, client ethclient.Client) (Config, error) {
signer := externalSignerFn(external, from, cfg.ChainID)
return newConfig(cfg, signer, from, client)
}
// NewConfig returns a new txmgr config from the given CLI config and private key.
func NewConfig(cfg CLIConfig, privateKey *ecdsa.PrivateKey, client ethclient.Client) (Config, error) {
signer := privateKeySignerFn(privateKey, cfg.ChainID)
addr := crypto.PubkeyToAddress(privateKey.PublicKey)
return newConfig(cfg, signer, addr, client)
}
func newConfig(cfg CLIConfig, signer SignerFn, from common.Address, client ethclient.Client) (Config, error) {
if err := cfg.Check(); err != nil {
return Config{}, errors.New("invalid config", err)
}
feeLimitThreshold, err := GweiToWei(cfg.FeeLimitThresholdGwei)
if err != nil {
return Config{}, errors.Wrap(err, "invalid fee limit threshold")
}
minBaseFee, err := GweiToWei(cfg.MinBaseFeeGwei)
if err != nil {
return Config{}, errors.Wrap(err, "invalid min base fee")
}
minTipCap, err := GweiToWei(cfg.MinTipCapGwei)
if err != nil {
return Config{}, errors.Wrap(err, "invalid min tip cap")
}
chainID := big.NewInt(int64(cfg.ChainID))
return Config{
Backend: client,
ResubmissionTimeout: cfg.ResubmissionTimeout,
FeeLimitMultiplier: cfg.FeeLimitMultiplier,
FeeLimitThreshold: feeLimitThreshold,
MinBaseFee: minBaseFee,
MinTipCap: minTipCap,
ChainID: chainID,
TxSendTimeout: cfg.TxSendTimeout,
TxNotInMempoolTimeout: cfg.TxNotInMempoolTimeout,
NetworkTimeout: cfg.NetworkTimeout,
ReceiptQueryInterval: cfg.ReceiptQueryInterval,
NumConfirmations: cfg.NumConfirmations,
SafeAbortNonceTooLowCount: cfg.SafeAbortNonceTooLowCount,
Signer: signer,
From: from,
}, nil
}
func (m Config) Check() error {
if m.Backend == nil {
return errors.New("must provide the backend")
}
if m.NumConfirmations == 0 {
return errors.New("numConfirmations must not be 0")
}
if m.NetworkTimeout == 0 {
return errors.New("must provide NetworkTimeout")
}
if m.FeeLimitMultiplier == 0 {
return errors.New("must provide FeeLimitMultiplier")
}
if m.MinBaseFee != nil && m.MinTipCap != nil && m.MinBaseFee.Cmp(m.MinTipCap) == -1 {
return errors.New("minBaseFee smaller than minTipCap",
m.MinBaseFee, m.MinTipCap)
}
if m.ResubmissionTimeout == 0 {
return errors.New("must provide ResubmissionTimeout")
}
if m.ReceiptQueryInterval == 0 {
return errors.New("must provide ReceiptQueryInterval")
}
if m.TxNotInMempoolTimeout == 0 {
return errors.New("must provide TxNotInMempoolTimeout")
}
if m.SafeAbortNonceTooLowCount == 0 {
return errors.New("safeAbortNonceTooLowCount must not be 0")
}
if m.Signer == nil {
return errors.New("must provide the Signer")
}
if m.ChainID == nil {
return errors.New("must provide the chainID")
}
return nil
}