ethtxmanager

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: AGPL-3.0, AGPL-3.0-or-later Imports: 21 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 means 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")

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

	// MonitoredTxStatusConsolidated means the tx was already mined N blocks ago
	MonitoredTxStatusConsolidated = MonitoredTxStatus("consolidated")

	// MonitoredTxStatusFinalized means the tx was already mined M (M > N) blocks ago
	MonitoredTxStatusFinalized = MonitoredTxStatus("finalized")
)

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(monitoredTxId common.Hash, 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(mTxResult MonitoredTxResult) *log.Logger

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

Types

type BatchCall

type BatchCall struct {
	Method     string
	Parameters []interface{}
}

BatchCall used in batch requests to send multiple methods and parameters at once

type Client

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

Client for eth tx manager

func New

func New(cfg Config) (*Client, error)

New creates new eth tx manager func New(cfg Config, ethMan ethermanInterface, storage storageInterface, state stateInterface) *Client {

func (*Client) Add

func (c *Client) Add(ctx context.Context, to *common.Address, forcedNonce *uint64, value *big.Int, data []byte, sidecar *types.BlobTxSidecar) (common.Hash, error)

Add a transaction to be sent and monitored

func (*Client) EncodeBlobData added in v0.1.0

func (c *Client) EncodeBlobData(data []byte) (kzg4844.Blob, error)

EncodeBlobData encodes data into blob data type

func (*Client) MakeBlobSidecar added in v0.1.0

func (c *Client) MakeBlobSidecar(blobs []kzg4844.Blob) *types.BlobTxSidecar

MakeBlobSidecar constructs a blob tx sidecar

func (*Client) ProcessPendingMonitoredTxs

func (c *Client) ProcessPendingMonitoredTxs(ctx context.Context, resultHandler ResultHandler)

ProcessPendingMonitoredTxs will check all monitored txs 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) Remove added in v0.0.6

func (c *Client) Remove(ctx context.Context, id common.Hash) error

Remove a transaction from the monitored txs

func (*Client) Result

func (c *Client) Result(ctx context.Context, id common.Hash) (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, statuses []MonitoredTxStatus) ([]MonitoredTxResult, error)

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

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"`
	// WaitReceiptToBeGenerated time to wait after transaction was mined to get the receipt
	WaitReceiptToBeGenerated types.Duration `mapstructure:"WaitReceiptToBeGenerated"`
	// ConsolidationL1ConfirmationBlocks is the number of blocks to wait for a L1 tx to be consolidated
	ConsolidationL1ConfirmationBlocks uint64 `mapstructure:"ConsolidationL1ConfirmationBlocks"`
	// FinalizationL1ConfirmationBlocks is the number of blocks to wait for a L1 tx to be finalized
	FinalizationL1ConfirmationBlocks uint64 `mapstructure:"FinalizationL1ConfirmationBlocks"`

	// 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"`
	// PersistenceFilename is the filename to store the memory storage
	PersistenceFilename string `mapstructure:"PersistenceFilename"`
	// Etherman configuration
	Etherman etherman.Config `mapstructure:"Etherman"`

	// Log configuration
	Log log.Config `mapstructure:"Log"`
}

Config is configuration for ethereum transaction manager

type MemStorage

type MemStorage struct {
	TxsMutex            sync.RWMutex
	FileMutex           sync.RWMutex
	Transactions        map[common.Hash]monitoredTx
	PersistenceFilename string
}

MemStorage hold txs to be managed

func NewMemStorage

func NewMemStorage(persistenceFilename string) *MemStorage

NewMemStorage creates a new instance of storage

func (*MemStorage) Add

func (s *MemStorage) Add(ctx context.Context, mTx monitoredTx) error

Add persist a monitored tx

func (*MemStorage) Get

func (s *MemStorage) Get(ctx context.Context, id common.Hash) (monitoredTx, error)

Get loads a persisted monitored tx

func (*MemStorage) GetByBlock

func (s *MemStorage) GetByBlock(ctx context.Context, fromBlock, toBlock *uint64) ([]monitoredTx, error)

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

func (*MemStorage) GetByStatus

func (s *MemStorage) GetByStatus(ctx context.Context, statuses []MonitoredTxStatus) ([]monitoredTx, error)

GetByStatus loads all monitored tx that match the provided status

func (*MemStorage) Remove added in v0.0.6

func (s *MemStorage) Remove(ctx context.Context, id common.Hash) error

Remove a persisted monitored tx

func (*MemStorage) Update

func (s *MemStorage) Update(ctx context.Context, mTx monitoredTx) error

Update a persisted monitored tx

type MonitoredTxResult

type MonitoredTxResult struct {
	ID                 common.Hash
	To                 *common.Address
	Nonce              uint64
	Value              *big.Int
	Data               []byte
	MinedAtBlockNumber *big.Int
	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 RPCClient

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

RPCClient is a client to interact with the Ethereum JSON RPC Server

func NewRPCClient

func NewRPCClient(url string) *RPCClient

NewRPCClient creates an instance of client

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      interface{}     `json:"id"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
}

Request is a jsonrpc request

type Response

type Response struct {
	JSONRPC string
	ID      interface{}
	Result  json.RawMessage
}

Response is a jsonrpc success response

func JSONRPCBatchCall

func JSONRPCBatchCall(url string, httpHeaders map[string]string, calls ...BatchCall) ([]Response, error)

JSONRPCBatchCall executes a 2.0 JSON RPC HTTP Post Batch Request to the provided URL with the provided method and parameters groups, which is compatible with the Ethereum JSON RPC Server.

func JSONRPCCall

func JSONRPCCall(url, method string, httpHeaders map[string]string, parameters ...interface{}) (Response, error)

JSONRPCCall executes a 2.0 JSON RPC HTTP Post Request to the provided URL with the provided method and parameters, which is compatible with the Ethereum JSON RPC Server.

type ResultHandler

type ResultHandler func(MonitoredTxResult)

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