specifications/mempool/README.md
This document describes the spec of a Diem node's mempool component, which is responsible for accepting transaction submitted from other nodes or directly to the node and relaying them to consensus.
This document is organized as follows:
Mempool is a system component of the Diem node that is responsible for accepting, validating, and prioritizing, and broadcasting pending transactions.
Mempool consists of a local memory-buffer that holds the transactions that are waiting to be executed (core mempool) and a network stack (shared mempool) that manages communication with other peers, as well as other system components.
The JSON RPC module proxies transactions to mempool. Mempool validates transactions and stores it locally.
On the validator node, consensus pulls ordered transactions from mempool to form a block.
Mempool is notified by consensus or state sync once a transaction is fully executed and committed. Mempool then drops this transaction from its local state.
A transaction remains in mempool at least until the earliest of the following:
This section (1) outlines the modules of mempool and their core functionalities, and (2) defines some mempool terminology. Note: the function signatures defined for each component's functionality is part of a sample interface not strictly required by the specification.
A transaction in mempool can be (but not limited to):
consensus-ready: transaction has some probability of being included in the next block
broadcast-ready: transaction is eligible to be broadcasted to a peer node
fn add_txn(&self, txn: SignedTransaction) -> MempoolStatus
Expected input:
Required behavior:
fn get_block(&mut self, ...) -> Vec<SignedTransaction>
Required behavior:
fn read_timeline(&mut self, ...) -> Vec<SignedTransaction>
Required behavior:
fn remove_transaction(txn: SignedTransaction, is_rejected: bool)
Expected inputs:
txn - transaction to remove from core mempoolis_rejected - whether this transaction was rejected from consensus or notRequired behavior:
is_rejected, all subsequent transactions for that account must be removedfn process_consensus_request(msg: ConsensusRequest)
Expected input:
Required behavior:
msg is q get-block request, return a response with consensus-ready transactionsmsg is notification of rejected txns, remove the transactions rejected from consensus from core mempoolfn process_state_sync_request(msg: CommitNotification)
Expected input:
Required behavior:
CommitResponse to callbackfn process_client_transactions(txn: SignedTransaction, callback)
Expected input:
txn - transaction submitted by JSON RPC client proxied to mempool by JSON RPC servicecallback - callback to JSON RPC serviceRequired behavior:
fn broadcast_single_peer(peer, peer_sync_data)
Expected input:
peer - peer to broadcast topeer_sync_data - peer-specific information to determine which transactions to broadcast to this peer. This can contain information like the transaction broadcast history for this peerrequired:
MempoolSyncMsg::BroadcastTransactionsRequest with broadcast-ready transactions for this peerfn process_transaction_broadcast(msg: MempoolSyncMsg)
Expected input:
MempoolSyncMsg::BroadcastTransactionsRequestRequired behavior:
MempoolSyncMsg::BroadcastTransactionsResponse and send to peer (via network::send_to(peer, msg))Mempool performs transaction signature verification and validations via the VM module.
fn validate_transaction(&self, txn: SignedTransaction)
Consensus interacts with mempool by (1) requesting a block of transactions ready for consensus, and (2) notifying mempool of rejected committed transactions
Mempool receives notification from state sync about committed transactions - see here for the required behavior for this interaction. Otherwise, implementing and coordinating this interaction is an implementation detail.
Mempool interacts with the networking module to send messages defined in the network protocol to peers in the network.
The following is a sample interface that is not strictly required by the specification.
fn send_to(recipient: PeerId, message: MempoolSyncMsg)
message to recipientJSON RPC service proxies the transaction submission it receives to mempool with
Network message used for broadcast of pending transactions to remote peers
pub enum MempoolSyncMsg {
BroadcastTransactionsRequest {
request_id: Vec<u8>,
transactions: Vec<SignedTransaction>,
},
BroadcastTransactionsResponse {
request_id: Vec<u8>,
retry: bool,
backoff: bool,
},
}
Fields:
BroadcastTransactionsRequest - represents broadcast request issued by the sender
request_id - unique id of sync request. Can be used by sender for rebroadcast analysistransactions - shared transactions in this batchBroadcastTransactionsResponse - used by receiver to notify sender that BroadcastTransactionsRequest with the matching request_id was received, but does not provide any indication of whether the transactions passed remote validation and/or will be forwarded further
request_id - id of the corresponding BroadcastTransactionsRequest of this responseretry - retry signal from recipient if there are txns in corresponding broadcast
that were rejected from mempool but may succeed on resendbackoff - backpressure signal from ACK sender to broadcast request sender