ethtxmanager

package
v0.0.0-...-38c47b8 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2023 License: AGPL-3.0, AGPL-3.0-or-later Imports: 17 Imported by: 0

Documentation

Overview

Package ethtxmanager handles ethereum transactions: It makes calls to send and to aggregate batch, checks possible errors, like wrong nonce or gas limit too low and make correct adjustments to request according to it. Also, it tracks transaction receipt and status of tx in case tx is rejected and send signals to sequencer/aggregator to resend sequence/batch

Index

Constants

View Source
const (
	// MonitoredTxStatusCreated mean the tx was just added to the storage
	MonitoredTxStatusCreated = MonitoredTxStatus("created")

	// MonitoredTxStatusSent means that at least a eth tx was sent to the network
	MonitoredTxStatusSent = MonitoredTxStatus("sent")

	// MonitoredTxStatusFailed means the tx was already mined and failed with an
	// error that can't be recovered automatically, ex: the data in the tx is invalid
	// and the tx gets reverted
	MonitoredTxStatusFailed = MonitoredTxStatus("failed")

	// MonitoredTxStatusConfirmed means the tx was already mined and the receipt
	// status is Successful
	MonitoredTxStatusConfirmed = MonitoredTxStatus("confirmed")

	// MonitoredTxStatusReorged is used when a monitored tx was already confirmed but
	// the L1 block where this tx was confirmed has been reorged, in this situation
	// the caller needs to review this information and wait until it gets confirmed
	// again in a future block
	MonitoredTxStatusReorged = MonitoredTxStatus("reorged")

	// MonitoredTxStatusDone means the tx was set by the owner as done
	MonitoredTxStatusDone = MonitoredTxStatus("done")
)

Variables

View Source
var (
	// ErrNotFound when the object is not found
	ErrNotFound = errors.New("not found")
	// ErrAlreadyExists when the object already exists
	ErrAlreadyExists = errors.New("already exists")

	// ErrExecutionReverted returned when trying to get the revert message
	// but the call fails without revealing the revert reason
	ErrExecutionReverted = errors.New("execution reverted")
)

Functions

func CreateLogger

func CreateLogger(owner, monitoredTxId string, from common.Address, to *common.Address) *log.Logger

CreateLogger creates an instance of logger with all the important fields already set for a monitoredTx without requiring an instance of monitoredTx, this should be use in for callers before calling the ADD method

func CreateMonitoredTxResultLogger

func CreateMonitoredTxResultLogger(owner string, mTxResult MonitoredTxResult) *log.Logger

CreateMonitoredTxResultLogger creates an instance of logger with all the important fields already set for a MonitoredTxResult

Types

type Client

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

Client for eth tx manager

func New

func New(cfg Config, ethMan ethermanInterface, storage storageInterface, state stateInterface) *Client

New creates new eth tx manager

func (*Client) Add

func (c *Client) Add(ctx context.Context, owner, id string, from common.Address, to *common.Address, value *big.Int, data []byte, gasOffset uint64, dbTx pgx.Tx) error

Add a transaction to be sent and monitored

func (*Client) ProcessPendingMonitoredTxs

func (c *Client) ProcessPendingMonitoredTxs(ctx context.Context, owner string, resultHandler ResultHandler, dbTx pgx.Tx)

ProcessPendingMonitoredTxs will check all monitored txs of this owner and wait until all of them are either confirmed or failed before continuing

for the confirmed and failed ones, the resultHandler will be triggered

func (*Client) Reorg

func (c *Client) Reorg(ctx context.Context, fromBlockNumber uint64, dbTx pgx.Tx) error

Reorg updates all monitored txs from provided block number until the last one to Reorged status, allowing it to be reprocessed by the tx monitoring

func (*Client) Result

func (c *Client) Result(ctx context.Context, owner, id string, dbTx pgx.Tx) (MonitoredTxResult, error)

Result returns the current result of the transaction execution with all the details

func (*Client) ResultsByStatus

func (c *Client) ResultsByStatus(ctx context.Context, owner string, statuses []MonitoredTxStatus, dbTx pgx.Tx) ([]MonitoredTxResult, error)

ResultsByStatus returns all the results for all the monitored txs related to the owner and matching the provided statuses if the statuses are empty, all the statuses are considered.

the slice is returned is in order by created_at field ascending

func (*Client) Start

func (c *Client) Start()

Start will start the tx management, reading txs from storage, send then to the blockchain and keep monitoring them until they get mined

func (*Client) Stop

func (c *Client) Stop()

Stop will stops the monitored tx management

type Config

type Config struct {
	// FrequencyToMonitorTxs frequency of the resending failed txs
	FrequencyToMonitorTxs types.Duration `mapstructure:"FrequencyToMonitorTxs"`
	// WaitTxToBeMined time to wait after transaction was sent to the ethereum
	WaitTxToBeMined types.Duration `mapstructure:"WaitTxToBeMined"`

	// PrivateKeys defines all the key store files that are going
	// to be read in order to provide the private keys to sign the L1 txs
	PrivateKeys []types.KeystoreFileConfig `mapstructure:"PrivateKeys"`

	// ForcedGas is the amount of gas to be forced in case of gas estimation error
	ForcedGas uint64 `mapstructure:"ForcedGas"`

	// GasPriceMarginFactor is used to multiply the suggested gas price provided by the network
	// in order to allow a different gas price to be set for all the transactions and making it
	// easier to have the txs prioritized in the pool, default value is 1.
	//
	// ex:
	// suggested gas price: 100
	// GasPriceMarginFactor: 1
	// gas price = 100
	//
	// suggested gas price: 100
	// GasPriceMarginFactor: 1.1
	// gas price = 110
	GasPriceMarginFactor float64 `mapstructure:"GasPriceMarginFactor"`

	// MaxGasPriceLimit helps avoiding transactions to be sent over an specified
	// gas price amount, default value is 0, which means no limit.
	// If the gas price provided by the network and adjusted by the GasPriceMarginFactor
	// is greater than this configuration, transaction will have its gas price set to
	// the value configured in this config as the limit.
	//
	// ex:
	//
	// suggested gas price: 100
	// gas price margin factor: 20%
	// max gas price limit: 150
	// tx gas price = 120
	//
	// suggested gas price: 100
	// gas price margin factor: 20%
	// max gas price limit: 110
	// tx gas price = 110
	MaxGasPriceLimit uint64 `mapstructure:"MaxGasPriceLimit"`
}

Config is configuration for ethereum transaction manager

type MonitoredTxResult

type MonitoredTxResult struct {
	ID     string
	Status MonitoredTxStatus
	Txs    map[common.Hash]TxResult
}

MonitoredTxResult represents the result of a execution of a monitored tx

type MonitoredTxStatus

type MonitoredTxStatus string

MonitoredTxStatus represents the status of a monitored tx

func (MonitoredTxStatus) String

func (s MonitoredTxStatus) String() string

String returns a string representation of the status

type PostgresStorage

type PostgresStorage struct {
	*pgxpool.Pool
}

PostgresStorage hold txs to be managed

func NewPostgresStorage

func NewPostgresStorage(dbCfg db.Config) (*PostgresStorage, error)

NewPostgresStorage creates a new instance of storage that use postgres to store data

func (*PostgresStorage) Add

func (s *PostgresStorage) Add(ctx context.Context, mTx monitoredTx, dbTx pgx.Tx) error

Add persist a monitored tx

func (*PostgresStorage) Get

func (s *PostgresStorage) Get(ctx context.Context, owner, id string, dbTx pgx.Tx) (monitoredTx, error)

Get loads a persisted monitored tx

func (*PostgresStorage) GetByBlock

func (s *PostgresStorage) GetByBlock(ctx context.Context, fromBlock, toBlock *uint64, dbTx pgx.Tx) ([]monitoredTx, error)

GetByBlock loads all monitored tx that have the blockNumber between fromBlock and toBlock

func (*PostgresStorage) GetByStatus

func (s *PostgresStorage) GetByStatus(ctx context.Context, owner *string, statuses []MonitoredTxStatus, dbTx pgx.Tx) ([]monitoredTx, error)

GetByStatus loads all monitored tx that match the provided status

func (*PostgresStorage) Update

func (s *PostgresStorage) Update(ctx context.Context, mTx monitoredTx, dbTx pgx.Tx) error

Update a persisted monitored tx

type ResultHandler

type ResultHandler func(MonitoredTxResult, pgx.Tx)

ResultHandler used by the caller to handle results when processing monitored txs

type TxResult

type TxResult struct {
	Tx            *types.Transaction
	Receipt       *types.Receipt
	RevertMessage string
}

TxResult represents the result of a execution of a ethereum transaction in the block chain

Jump to

Keyboard shortcuts

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