txmgr

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: GPL-3.0 Imports: 24 Imported by: 0

README

txmgr package

This package has been copied and modified from the optimism repository.

Documentation

Index

Constants

View Source
const (
	// PriceBump geth requires a minimum fee bump of 10% for regular tx resubmission.
	PriceBump int64 = 10
)

Variables

View Source
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,
	}
)
View Source
var (
	ErrClosed = errors.New("transaction manager is closed")
)

Functions

func Do

func Do[T any](ctx context.Context, maxAttempts int, strategy Strategy, op func() (T, error)) (T, error)

Do perform the provided Operation up to maxAttempts times with delays in between each retry according to the provided Strategy.

func GweiToWei

func GweiToWei(gwei float64) (*big.Int, error)

GweiToWei converts a float64 GWei value into a big.Int Wei value.

Types

type CLIConfig

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
}

func NewCLIConfig

func NewCLIConfig(chainID uint64, interval time.Duration, defaults DefaultFlagValues) CLIConfig

func (CLIConfig) Check

func (m CLIConfig) Check() error

type Config

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
}

Config houses parameters for altering the behavior of a simple.

func NewConfig

func NewConfig(cfg CLIConfig, privateKey *ecdsa.PrivateKey, client ethclient.Client) (Config, error)

NewConfig returns a new txmgr config from the given CLI config and private key.

func NewConfigWithSigner added in v0.1.2

func NewConfigWithSigner(cfg CLIConfig, external ExternalSigner, from common.Address, client ethclient.Client) (Config, error)

NewConfigWithSigner returns a new txmgr config from the given CLI config and external signer.

func (Config) Check

func (m Config) Check() error

type DefaultFlagValues

type DefaultFlagValues struct {
	NumConfirmations          uint64
	SafeAbortNonceTooLowCount uint64
	FeeLimitMultiplier        uint64
	FeeLimitThresholdGwei     float64
	ResubmissionTimeout       time.Duration
	NetworkTimeout            time.Duration
	TxSendTimeout             time.Duration
	TxNotInMempoolTimeout     time.Duration
}

type ExternalSigner added in v0.1.2

type ExternalSigner func(context.Context, common.Hash, common.Address) ([65]byte, error)

ExternalSigner is a function that signs a transaction with a given address and returns the signature.

type FailedPermanentlyError

type FailedPermanentlyError struct {
	LastErr error
	// contains filtered or unexported fields
}

FailedPermanentlyError is an error raised by Do when the underlying Operation has been retried maxAttempts times.

func (*FailedPermanentlyError) Error

func (e *FailedPermanentlyError) Error() string

func (*FailedPermanentlyError) Unwrap

func (e *FailedPermanentlyError) Unwrap() error

type FixedStrategy

type FixedStrategy struct {
	Dur time.Duration
}

func (*FixedStrategy) Duration

func (f *FixedStrategy) Duration(_ int) time.Duration

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 the txmgr should abort a given txn.

func NewSendState

func NewSendState(nonceTooLowCount uint64, timeout time.Duration) *SendState

NewSendState creates a new doSend state.

func NewSendStateWithNow

func NewSendStateWithNow(nonceTooLowCount uint64, timeout time.Duration,
	now func() time.Time) *SendState

NewSendStateWithNow creates a new doSend 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)

TxNotMined 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 SignerFactory

type SignerFactory func(chainID *big.Int) SignerFn

SignerFactory creates a SignerFn that is bound to a specific chainID.

type SignerFn

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 Strategy

type Strategy interface {
	// Duration returns how long to wait for a given retry attempt.
	Duration(attempt int) time.Duration
}

Strategy is used to calculate how long a particular Operation should wait between attempts.

func Fixed

func Fixed(dur time.Duration) Strategy

type TxCandidate

type TxCandidate struct {
	// TxData is the transaction calldata 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 interface {
	// Send is used to create & doSend a transaction. It will handle increasing
	// the gas price & ensuring that the transaction remains in the transaction pool.
	// It can be stopped by canceling the provided context; however, the transaction
	// may be included on L1 even if the context is canceled.
	//
	// NOTE: Send can be called concurrently, the nonce will be managed internally.
	Send(ctx context.Context, candidate TxCandidate) (*types.Transaction, *types.Receipt, error)

	// From returns the sending address associated with the instance of the transaction manager.
	// It is static for a single instance of a TxManager.
	From() common.Address

	// BlockNumber returns the most recent block number from the underlying network.
	BlockNumber(ctx context.Context) (uint64, error)

	// Close the underlying connection
	Close()
}

TxManager is an interface that allows callers to reliably publish txs, bumping the gas price if needed, and obtain the receipt of the resulting tx.

func NewSimple added in v0.1.2

func NewSimple(chainName string, conf Config) (TxManager, error)

NewSimple initializes a new simple with the passed Config.

Jump to

Keyboard shortcuts

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