mempool

package
v0.0.0-...-be384fd Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PeerCatchupSleepIntervalMS defines how much time to sleep if a peer is behind
	PeerCatchupSleepIntervalMS = 100

	// UnknownPeerID is the peer ID to use when running CheckTx when there is
	// no peer (e.g. RPC)
	UnknownPeerID uint16 = 0

	MaxActiveIDs = math.MaxUint16
)
View Source
const (
	// MetricsSubsystem is a subsystem shared by all metrics exposed by this
	// package.
	MetricsSubsystem = "mempool"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type IDs

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

func NewMempoolIDs

func NewMempoolIDs() *IDs

func (*IDs) GetForPeer

func (ids *IDs) GetForPeer(peerID types.NodeID) uint16

GetForPeer returns an ID reserved for the peer.

func (*IDs) Reclaim

func (ids *IDs) Reclaim(peerID types.NodeID)

Reclaim returns the ID reserved for the peer back to unused pool.

func (*IDs) ReserveForPeer

func (ids *IDs) ReserveForPeer(peerID types.NodeID)

ReserveForPeer searches for the next unused ID and assigns it to the provided peer.

type LRUTxCache

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

LRUTxCache maintains a thread-safe LRU cache of raw transactions. The cache only stores the hash of the raw transaction.

func NewLRUTxCache

func NewLRUTxCache(cacheSize int) *LRUTxCache

func (*LRUTxCache) GetList

func (c *LRUTxCache) GetList() *list.List

GetList returns the underlying linked-list that backs the LRU cache. Note, this should be used for testing purposes only!

func (*LRUTxCache) Has

func (c *LRUTxCache) Has(tx types.Tx) bool

func (*LRUTxCache) Push

func (c *LRUTxCache) Push(tx types.Tx) bool

func (*LRUTxCache) Remove

func (c *LRUTxCache) Remove(tx types.Tx)

func (*LRUTxCache) Reset

func (c *LRUTxCache) Reset()

type Mempool

type Mempool interface {
	TxChecker

	// RemoveTxByKey removes a transaction, identified by its key,
	// from the mempool.
	RemoveTxByKey(txKey types.TxKey) error

	// ReapMaxBytesMaxGas reaps transactions from the mempool up to maxBytes
	// bytes total with the condition that the total gasWanted must be less than
	// maxGas.
	//
	// If both maxes are negative, there is no cap on the size of all returned
	// transactions (~ all available transactions).
	ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs

	// ReapMaxTxs reaps up to max transactions from the mempool. If max is
	// negative, there is no cap on the size of all returned transactions
	// (~ all available transactions).
	ReapMaxTxs(max int) types.Txs

	// Lock locks the mempool. The consensus must be able to hold lock to safely
	// update.
	Lock()

	// Unlock unlocks the mempool.
	Unlock()

	// Update informs the mempool that the given txs were committed and can be
	// discarded.
	//
	// NOTE:
	// 1. This should be called *after* block is committed by consensus.
	// 2. Lock/Unlock must be managed by the caller.
	Update(
		ctx context.Context,
		blockHeight int64,
		blockTxs types.Txs,
		txResults []*abci.ExecTxResult,
		newPreFn PreCheckFunc,
		newPostFn PostCheckFunc,
		recheck bool,
	) error

	// FlushAppConn flushes the mempool connection to ensure async callback calls
	// are done, e.g. from CheckTx.
	//
	// NOTE:
	// 1. Lock/Unlock must be managed by caller.
	FlushAppConn(context.Context) error

	// Flush removes all transactions from the mempool and caches.
	Flush()

	// TxsAvailable returns a channel which fires once for every height, and only
	// when transactions are available in the mempool.
	//
	// NOTE:
	// 1. The returned channel may be nil if EnableTxsAvailable was not called.
	TxsAvailable() <-chan struct{}

	// EnableTxsAvailable initializes the TxsAvailable channel, ensuring it will
	// trigger once every height when transactions are available.
	EnableTxsAvailable()

	// Size returns the number of transactions in the mempool.
	Size() int

	// SizeBytes returns the total size of all txs in the mempool.
	SizeBytes() int64
}

Mempool defines the mempool interface.

Updates to the mempool need to be synchronized with committing a block so applications can reset their transient state on Commit.

type Metrics

type Metrics struct {
	// Number of uncommitted transactions in the mempool.
	Size metrics.Gauge

	// Histogram of transaction sizes in bytes.
	TxSizeBytes metrics.Histogram `metrics_buckettype:"exp" metrics_bucketsizes:"1,3,7"`

	// Number of failed transactions.
	FailedTxs metrics.Counter

	// RejectedTxs defines the number of rejected transactions. These are
	// transactions that passed CheckTx but failed to make it into the mempool
	// due to resource limits, e.g. mempool is full and no lower priority
	// transactions exist in the mempool.
	//metrics:Number of rejected transactions.
	RejectedTxs metrics.Counter

	// EvictedTxs defines the number of evicted transactions. These are valid
	// transactions that passed CheckTx and existed in the mempool but were later
	// evicted to make room for higher priority valid transactions that passed
	// CheckTx.
	//metrics:Number of evicted transactions.
	EvictedTxs metrics.Counter

	// Number of times transactions are rechecked in the mempool.
	RecheckTimes metrics.Counter
}

Metrics contains metrics exposed by this package. see MetricsProvider for descriptions.

func NopMetrics

func NopMetrics() *Metrics

func PrometheusMetrics

func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics

type NopTxCache

type NopTxCache struct{}

NopTxCache defines a no-op raw transaction cache.

func (NopTxCache) Has

func (NopTxCache) Has(types.Tx) bool

func (NopTxCache) Push

func (NopTxCache) Push(types.Tx) bool

func (NopTxCache) Remove

func (NopTxCache) Remove(types.Tx)

func (NopTxCache) Reset

func (NopTxCache) Reset()

type PostCheckFunc

type PostCheckFunc func(types.Tx, *abci.ResponseCheckTx) error

PostCheckFunc is an optional filter executed after CheckTx and rejects transaction if false is returned. An example would be to ensure a transaction doesn't require more gas than available for the block.

func PostCheckMaxGas

func PostCheckMaxGas(maxGas int64) PostCheckFunc

PostCheckMaxGas checks that the wanted gas is smaller or equal to the passed maxGas. Returns nil if maxGas is -1.

type PreCheckFunc

type PreCheckFunc func(types.Tx) error

PreCheckFunc is an optional filter executed before CheckTx and rejects transaction if false is returned. An example would be to ensure that a transaction doesn't exceeded the block size.

func PreCheckMaxBytes

func PreCheckMaxBytes(maxBytes int64) PreCheckFunc

PreCheckMaxBytes checks that the size of the transaction is smaller or equal to the expected maxBytes.

type Reactor

type Reactor struct {
	service.BaseService
	// contains filtered or unexported fields
}

Reactor implements a service that contains mempool of txs that are broadcasted amongst peers. It maintains a map from peer ID to counter, to prevent gossiping txs to the peers you received it from.

func NewReactor

func NewReactor(
	logger log.Logger,
	cfg *config.MempoolConfig,
	txmp *TxMempool,
	p2pClient *client.Client,
	peerEvents p2p.PeerEventSubscriber,
) *Reactor

NewReactor returns a reference to a new reactor.

func (*Reactor) OnStart

func (r *Reactor) OnStart(ctx context.Context) error

OnStart starts separate go routines for each p2p Channel and listens for envelopes on each. In addition, it also listens for peer updates and handles messages on that p2p channel accordingly. The caller must be sure to execute OnStop to ensure the outbound p2p Channels are closed.

func (*Reactor) OnStop

func (r *Reactor) OnStop()

OnStop stops the reactor by signaling to all spawned goroutines to exit and blocking until they all exit.

type TxCache

type TxCache interface {
	// Reset resets the cache to an empty state.
	Reset()

	// Push adds the given raw transaction to the cache and returns true if it was
	// newly added. Otherwise, it returns false.
	Push(tx types.Tx) bool

	// Remove removes the given raw transaction from the cache.
	Remove(tx types.Tx)

	// Has reports whether tx is present in the cache. Checking for presence is
	// not treated as an access of the value.
	Has(tx types.Tx) bool
}

TxCache defines an interface for raw transaction caching in a mempool. Currently, a TxCache does not allow direct reading or getting of transaction values. A TxCache is used primarily to push transactions and removing transactions. Pushing via Push returns a boolean telling the caller if the transaction already exists in the cache or not.

type TxChecker

type TxChecker interface {
	// CheckTx executes a new transaction against the application to determine
	// its validity and whether it should be added to the checker.
	CheckTx(ctx context.Context, tx types.Tx, cb func(*abci.ResponseCheckTx), txInfo TxInfo) error
}

TxChecker is the interface that wraps CheckTx method

type TxInfo

type TxInfo struct {
	// SenderID is the internal peer ID used in the mempool to identify the
	// sender, storing two bytes with each transaction instead of 20 bytes for
	// the types.NodeID.
	SenderID uint16

	// SenderNodeID is the actual types.NodeID of the sender.
	SenderNodeID types.NodeID
}

TxInfo are parameters that get passed when attempting to add a tx to the mempool.

type TxMempool

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

TxMempool implemements the Mempool interface and allows the application to set priority values on transactions in the CheckTx response. When selecting transactions to include in a block, higher-priority transactions are chosen first. When evicting transactions from the mempool for size constraints, lower-priority transactions are evicted sooner.

Within the mempool, transactions are ordered by time of arrival, and are gossiped to the rest of the network based on that order (gossip order does not take priority into account).

func NewTxMempool

func NewTxMempool(
	logger log.Logger,
	cfg *config.MempoolConfig,
	proxyAppConn abciclient.Client,
	options ...TxMempoolOption,
) *TxMempool

NewTxMempool constructs a new, empty priority mempool at the specified initial height and using the given config and options.

func (*TxMempool) CheckTx

func (txmp *TxMempool) CheckTx(
	ctx context.Context,
	tx types.Tx,
	cb func(*abci.ResponseCheckTx),
	txInfo TxInfo,
) error

CheckTx adds the given transaction to the mempool if it fits and passes the application's ABCI CheckTx method.

CheckTx reports an error without adding tx if:

- The size of tx exceeds the configured maximum transaction size. - The pre-check hook is defined and reports an error for tx. - The transaction already exists in the cache. - The proxy connection to the application fails.

If tx passes all of the above conditions, it is passed (asynchronously) to the application's ABCI CheckTx method and this CheckTx method returns nil. If cb != nil, it is called when the ABCI request completes to report the application response.

If the application accepts the transaction and the mempool is full, the mempool evicts one or more of the lowest-priority transaction whose priority is (strictly) lower than the priority of tx and whose size together exceeds the size of tx, and adds tx instead. If no such transactions exist, tx is discarded.

func (*TxMempool) EnableTxsAvailable

func (txmp *TxMempool) EnableTxsAvailable()

EnableTxsAvailable enables the mempool to trigger events when transactions are available on a block by block basis.

func (*TxMempool) Flush

func (txmp *TxMempool) Flush()

Flush purges the contents of the mempool and the cache, leaving both empty. The current height is not modified by this operation.

func (*TxMempool) FlushAppConn

func (txmp *TxMempool) FlushAppConn(ctx context.Context) error

FlushAppConn executes Flush on the mempool's proxyAppConn.

The caller must hold an exclusive mempool lock (by calling txmp.Lock) before calling FlushAppConn.

func (*TxMempool) Lock

func (txmp *TxMempool) Lock()

Lock obtains a write-lock on the mempool. A caller must be sure to explicitly release the lock when finished.

func (*TxMempool) ReapMaxBytesMaxGas

func (txmp *TxMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs

ReapMaxBytesMaxGas returns a slice of valid transactions that fit within the size and gas constraints. The results are ordered by nonincreasing priority, with ties broken by increasing order of arrival. Reaping transactions does not remove them from the mempool.

If maxBytes < 0, no limit is set on the total size in bytes. If maxGas < 0, no limit is set on the total gas cost.

If the mempool is empty or has no transactions fitting within the given constraints, the result will also be empty.

func (*TxMempool) ReapMaxTxs

func (txmp *TxMempool) ReapMaxTxs(max int) types.Txs

ReapMaxTxs returns up to max transactions from the mempool. The results are ordered by nonincreasing priority with ties broken by increasing order of arrival. Reaping transactions does not remove them from the mempool.

If max < 0, all transactions in the mempool are reaped.

The result may have fewer than max elements (possibly zero) if the mempool does not have that many transactions available.

func (*TxMempool) RemoveTxByKey

func (txmp *TxMempool) RemoveTxByKey(txKey types.TxKey) error

RemoveTxByKey removes the transaction with the specified key from the mempool. It reports an error if no such transaction exists. This operation does not remove the transaction from the cache.

func (*TxMempool) Size

func (txmp *TxMempool) Size() int

Size returns the number of valid transactions in the mempool. It is thread-safe.

func (*TxMempool) SizeBytes

func (txmp *TxMempool) SizeBytes() int64

SizeBytes return the total sum in bytes of all the valid transactions in the mempool. It is thread-safe.

func (*TxMempool) TxsAvailable

func (txmp *TxMempool) TxsAvailable() <-chan struct{}

TxsAvailable returns a channel which fires once for every height, and only when transactions are available in the mempool. It is thread-safe.

func (*TxMempool) TxsFront

func (txmp *TxMempool) TxsFront() *clist.CElement

TxsFront returns the frontmost element of the pending transaction list. It will be nil if the mempool is empty.

func (*TxMempool) TxsWaitChan

func (txmp *TxMempool) TxsWaitChan() <-chan struct{}

TxsWaitChan returns a channel that is closed when there is at least one transaction available to be gossiped.

func (*TxMempool) Unlock

func (txmp *TxMempool) Unlock()

Unlock releases a write-lock on the mempool.

func (*TxMempool) Update

func (txmp *TxMempool) Update(
	ctx context.Context,
	blockHeight int64,
	blockTxs types.Txs,
	deliverTxResponses []*abci.ExecTxResult,
	newPreFn PreCheckFunc,
	newPostFn PostCheckFunc,
	recheck bool,
) error

Update removes all the given transactions from the mempool and the cache, and updates the current block height. The blockTxs and deliverTxResponses must have the same length with each response corresponding to the tx at the same offset.

If the configuration enables recheck, Update sends each remaining transaction after removing blockTxs to the ABCI CheckTx method. Any transactions marked as invalid during recheck are also removed.

The caller must hold an exclusive mempool lock (by calling txmp.Lock) before calling Update.

type TxMempoolOption

type TxMempoolOption func(*TxMempool)

TxMempoolOption sets an optional parameter on the TxMempool.

func WithMetrics

func WithMetrics(metrics *Metrics) TxMempoolOption

WithMetrics sets the mempool's metrics collector.

func WithPostCheck

func WithPostCheck(f PostCheckFunc) TxMempoolOption

WithPostCheck sets a filter for the mempool to reject a transaction if f(tx, resp) returns an error. This is executed after CheckTx. It only applies to the first created block. After that, Update overwrites the existing value.

func WithPreCheck

func WithPreCheck(f PreCheckFunc) TxMempoolOption

WithPreCheck sets a filter for the mempool to reject a transaction if f(tx) returns an error. This is executed before CheckTx. It only applies to the first created block. After that, Update() overwrites the existing value.

type WrappedTx

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

WrappedTx defines a wrapper around a raw transaction with additional metadata that is used for indexing.

func (*WrappedTx) GasWanted

func (w *WrappedTx) GasWanted() int64

GasWanted reports the application-assigned gas requirement of w.

func (*WrappedTx) HasPeer

func (w *WrappedTx) HasPeer(id uint16) bool

HasPeer reports whether the specified peer ID is a sender of w.

func (*WrappedTx) Priority

func (w *WrappedTx) Priority() int64

Priority reports the application-assigned priority of w.

func (*WrappedTx) Sender

func (w *WrappedTx) Sender() string

Sender reports the application-assigned sender of w.

func (*WrappedTx) SetGasWanted

func (w *WrappedTx) SetGasWanted(gas int64)

SetGasWanted sets the application-assigned gas requirement of w.

func (*WrappedTx) SetPeer

func (w *WrappedTx) SetPeer(id uint16)

SetPeer adds the specified peer ID as a sender of w.

func (*WrappedTx) SetPriority

func (w *WrappedTx) SetPriority(p int64)

SetPriority sets the application-assigned priority of w.

func (*WrappedTx) SetSender

func (w *WrappedTx) SetSender(sender string)

SetSender sets the application-assigned sender of w.

func (*WrappedTx) Size

func (w *WrappedTx) Size() int64

Size reports the size of the raw transaction in bytes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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