txmgr

package
v0.0.0-...-d4adcb1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

We use Optimism's `SimpleTransactionManager` below (as of commit: 02c570c4c05603e1261f664ee8f92502814bfca2).

Index

Constants

View Source
const (
	ResubmissionTimeoutFlagName       = "resubmission-timeout"
	TxSendTimeoutFlagName             = "send-timeout"
	TxNotInMempoolTimeoutFlagName     = "not-in-mempool-timeout"
	NetworkTimeoutFlagName            = "network-timeout"
	ReceiptQueryIntervalFlagName      = "receipt-query-interval"
	NumConfirmationsFlagName          = "num-confirmations"
	SafeAbortNonceTooLowCountFlagName = "safe-abort-nonce-too-low-count"
)

Variables

This section is empty.

Functions

func CLIFlags

func CLIFlags(namespace string) []cli.Flag

Types

type Config

type Config struct {
	// 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

	// 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 send 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.
	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

	From common.Address
}

func NewConfigFromCLI

func NewConfigFromCLI(
	cliCtx *cli.Context,
	namespace string,
	chainID *big.Int,
	from common.Address,
) Config

type ETHBackend

type ETHBackend interface {
	// BlockNumber returns the most recent block number.
	BlockNumber(ctx context.Context) (uint64, error)

	// TransactionReceipt queries the backend for a receipt associated with
	// txHash. If lookup does not fail, but the transaction is not found,
	// nil should be returned for both values.
	TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)

	// SendTransaction submits a signed transaction to L1.
	SendTransaction(ctx context.Context, tx *types.Transaction) error

	// These functions are used to estimate what the basefee & priority fee should be set to.
	// TODO(CLI-3318): Maybe need a generic interface to support different RPC providers
	HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
	SuggestGasTipCap(ctx context.Context) (*big.Int, error)
	// NonceAt returns the account nonce of the given account.
	// The block number can be nil, in which case the nonce is taken from the latest known block.
	NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
	// PendingNonceAt returns the pending nonce.
	PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
	// EstimateGas returns an estimate of the amount of gas needed to execute the given
	// transaction against the current pending block.
	EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error)
	// CallContract executes an eth_call against the provided contract.
	CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
}

ETHBackend is the set of methods that the transaction manager uses to resubmit gas & determine when transactions are included on L1.

type SendState

type SendState struct {
	// contains filtered or unexported fields
}

SendState tracks information about the publication state of a given txn. In this context, a txn may correspond to multiple different txn hashes due to varying gas prices, though we treat them all as the same logical txn. This struct is primarily used to determine whether or not the txmgr should abort a given txn.

func NewSendState

func NewSendState(safeAbortNonceTooLowCount uint64, unableToSendTimeout time.Duration) *SendState

NewSendState creates a new send state

func NewSendStateWithNow

func NewSendStateWithNow(safeAbortNonceTooLowCount uint64, unableToSendTimeout time.Duration, now func() time.Time) *SendState

NewSendStateWithNow creates a new send state with the provided clock.

func (*SendState) IsWaitingForConfirmation

func (s *SendState) IsWaitingForConfirmation() bool

IsWaitingForConfirmation returns true if we have at least one confirmation on one of our txs.

func (*SendState) ProcessSendError

func (s *SendState) ProcessSendError(err error)

ProcessSendError should be invoked with the error returned for each publication. It is safe to call this method with nil or arbitrary errors.

func (*SendState) ShouldAbortImmediately

func (s *SendState) ShouldAbortImmediately() bool

ShouldAbortImmediately returns true if the txmgr should give up on trying a given txn with the target nonce. This occurs when the set of errors recorded indicates that no further progress can be made on this transaction.

func (*SendState) TxMined

func (s *SendState) TxMined(txHash common.Hash)

TxMined records that the txn with txnHash has been mined and is await confirmation. It is safe to call this function multiple times.

func (*SendState) TxNotMined

func (s *SendState) TxNotMined(txHash common.Hash)

TxMined records that the txn with txnHash has not been mined or has been reorg'd out. It is safe to call this function multiple times.

type SignerFn

type SignerFn func(ctx context.Context, address common.Address, tx *types.Transaction) (*types.Transaction, error)

type TxCandidate

type TxCandidate struct {
	// TxData is the transaction data to be used in the constructed tx.
	TxData []byte
	// To is the recipient of the constructed tx. Nil means contract creation.
	To *common.Address
	// GasLimit is the gas limit to be used in the constructed tx.
	GasLimit uint64
	// Value is the value to be used in the constructed tx.
	Value *big.Int
}

TxCandidate is a transaction candidate that can be submitted to ask the TxManager to construct a transaction with gas price bounds.

type TxManager

type TxManager struct {
	// contains filtered or unexported fields
}

TxManager performs linear fee bumping of a tx until it confirms.

func NewTxManager

func NewTxManager(l log.Logger, cfg Config, backend ETHBackend, signer SignerFn) *TxManager

NewTxManager initializes a new TxManager with the passed Config.

func (*TxManager) BlockNumber

func (m *TxManager) BlockNumber(ctx context.Context) (uint64, error)

func (*TxManager) Call

func (m *TxManager) Call(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)

Call is used to call a contract. Internally, it uses the [ethclient.Client.CallContract] method.

func (*TxManager) From

func (m *TxManager) From() common.Address

func (*TxManager) Send

func (m *TxManager) Send(ctx context.Context, candidate TxCandidate) (*types.Receipt, error)

Send is used to publish a transaction with incrementally higher gas prices until the transaction eventually confirms. This method blocks until an invocation of sendTx returns (called with differing gas prices). The method may be canceled using the passed context.

The transaction manager handles all signing. If and only if the gas limit is 0, the transaction manager will do a gas estimation.

NOTE: Send can be called concurrently, the nonce will be managed internally.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL