Commit a2b595c3 authored by Adrian Sutton's avatar Adrian Sutton Committed by GitHub

Merge pull request #7022 from conduitxyz/txmgr-reuse

Allow passing Config and Value to TxMgr
parents efa6313d 60c3439b
...@@ -290,3 +290,34 @@ type Config struct { ...@@ -290,3 +290,34 @@ type Config struct {
Signer opcrypto.SignerFn Signer opcrypto.SignerFn
From common.Address From common.Address
} }
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.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
}
...@@ -112,7 +112,14 @@ func NewSimpleTxManager(name string, l log.Logger, m metrics.TxMetricer, cfg CLI ...@@ -112,7 +112,14 @@ func NewSimpleTxManager(name string, l log.Logger, m metrics.TxMetricer, cfg CLI
if err != nil { if err != nil {
return nil, err return nil, err
} }
return NewSimpleTxManagerFromConfig(name, l, m, conf)
}
// NewSimpleTxManager initializes a new SimpleTxManager with the passed Config.
func NewSimpleTxManagerFromConfig(name string, l log.Logger, m metrics.TxMetricer, conf Config) (*SimpleTxManager, error) {
if err := conf.Check(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
return &SimpleTxManager{ return &SimpleTxManager{
chainID: conf.ChainID, chainID: conf.ChainID,
name: name, name: name,
...@@ -140,6 +147,8 @@ type TxCandidate struct { ...@@ -140,6 +147,8 @@ type TxCandidate struct {
To *common.Address To *common.Address
// GasLimit is the gas limit to be used in the constructed tx. // GasLimit is the gas limit to be used in the constructed tx.
GasLimit uint64 GasLimit uint64
// Value is the value to be used in the constructed tx.
Value *big.Int
} }
// Send is used to publish a transaction with incrementally higher gas prices // Send is used to publish a transaction with incrementally higher gas prices
...@@ -214,6 +223,7 @@ func (m *SimpleTxManager) craftTx(ctx context.Context, candidate TxCandidate) (* ...@@ -214,6 +223,7 @@ func (m *SimpleTxManager) craftTx(ctx context.Context, candidate TxCandidate) (*
GasTipCap: gasTipCap, GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap, GasFeeCap: gasFeeCap,
Data: candidate.TxData, Data: candidate.TxData,
Value: candidate.Value,
} }
m.l.Info("Creating tx", "to", rawTx.To, "from", m.cfg.From) m.l.Info("Creating tx", "to", rawTx.To, "from", m.cfg.From)
...@@ -229,6 +239,7 @@ func (m *SimpleTxManager) craftTx(ctx context.Context, candidate TxCandidate) (* ...@@ -229,6 +239,7 @@ func (m *SimpleTxManager) craftTx(ctx context.Context, candidate TxCandidate) (*
GasFeeCap: gasFeeCap, GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap, GasTipCap: gasTipCap,
Data: rawTx.Data, Data: rawTx.Data,
Value: rawTx.Value,
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to estimate gas: %w", err) return nil, fmt.Errorf("failed to estimate gas: %w", err)
......
...@@ -603,12 +603,21 @@ func TestWaitMinedMultipleConfs(t *testing.T) { ...@@ -603,12 +603,21 @@ func TestWaitMinedMultipleConfs(t *testing.T) {
require.Equal(t, txHash, receipt.TxHash) require.Equal(t, txHash, receipt.TxHash)
} }
// TestManagerErrsOnZeroCLIConfs ensures that the NewSimpleTxManager will error
// when attempting to configure with NumConfirmations set to zero.
func TestManagerErrsOnZeroCLIConfs(t *testing.T) {
t.Parallel()
_, err := NewSimpleTxManager("TEST", testlog.Logger(t, log.LvlCrit), &metrics.NoopTxMetrics{}, CLIConfig{})
require.Error(t, err)
}
// TestManagerErrsOnZeroConfs ensures that the NewSimpleTxManager will error // TestManagerErrsOnZeroConfs ensures that the NewSimpleTxManager will error
// when attempting to configure with NumConfirmations set to zero. // when attempting to configure with NumConfirmations set to zero.
func TestManagerErrsOnZeroConfs(t *testing.T) { func TestManagerErrsOnZeroConfs(t *testing.T) {
t.Parallel() t.Parallel()
_, err := NewSimpleTxManager("TEST", testlog.Logger(t, log.LvlCrit), &metrics.NoopTxMetrics{}, CLIConfig{}) _, err := NewSimpleTxManagerFromConfig("TEST", testlog.Logger(t, log.LvlCrit), &metrics.NoopTxMetrics{}, Config{})
require.Error(t, err) require.Error(t, err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment