mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-08 23:08:19 -05:00
docs(network): sync struct definitions with sour (#20747)
This commit is contained in:
@@ -229,28 +229,32 @@ The field of note here is `to_manager_tx`, which is a handle that can be used to
|
||||
|
||||
[File: crates/net/network/src/manager.rs](https://github.com/paradigmxyz/reth/blob/1563506aea09049a85e5cc72c2894f3f7a371581/crates/net/network/src/manager.rs)
|
||||
```rust,ignore
|
||||
pub struct NetworkManager<C> {
|
||||
pub struct NetworkManager<N: NetworkPrimitives = EthNetworkPrimitives> {
|
||||
/// The type that manages the actual network part, which includes connections.
|
||||
swarm: Swarm<C>,
|
||||
swarm: Swarm<N>,
|
||||
/// Underlying network handle that can be shared.
|
||||
handle: NetworkHandle,
|
||||
handle: NetworkHandle<N>,
|
||||
/// Receiver half of the command channel set up between this type and the [`NetworkHandle`]
|
||||
from_handle_rx: UnboundedReceiverStream<NetworkHandleMessage>,
|
||||
from_handle_rx: UnboundedReceiverStream<NetworkHandleMessage<N>>,
|
||||
/// Handles block imports according to the `eth` protocol.
|
||||
block_import: Box<dyn BlockImport>,
|
||||
/// All listeners for high level network events.
|
||||
event_listeners: NetworkEventListeners,
|
||||
block_import: Box<dyn BlockImport<N::NewBlockPayload>>,
|
||||
/// Sender for high level network events.
|
||||
event_sender: EventSender<NetworkEvent<PeerRequest<N>>>,
|
||||
/// Sender half to send events to the
|
||||
/// [`TransactionsManager`](crate::transactions::TransactionsManager) task, if configured.
|
||||
to_transactions_manager: Option<mpsc::UnboundedSender<NetworkTransactionEvent>>,
|
||||
to_transactions_manager: Option<UnboundedMeteredSender<NetworkTransactionEvent<N>>>,
|
||||
/// Sender half to send events to the
|
||||
/// [`EthRequestHandler`](crate::eth_requests::EthRequestHandler) task, if configured.
|
||||
to_eth_request_handler: Option<mpsc::UnboundedSender<IncomingEthRequest>>,
|
||||
to_eth_request_handler: Option<mpsc::Sender<IncomingEthRequest<N>>>,
|
||||
/// Tracks the number of active sessions (connected peers).
|
||||
///
|
||||
/// This is updated via internal events and shared via `Arc` with the [`NetworkHandle`]
|
||||
/// Updated by the `NetworkWorker` and loaded by the `NetworkService`.
|
||||
num_active_peers: Arc<AtomicUsize>,
|
||||
/// Metrics for the Network
|
||||
metrics: NetworkMetrics,
|
||||
/// Disconnect metrics for the Network
|
||||
disconnect_metrics: DisconnectMetrics,
|
||||
}
|
||||
```
|
||||
|
||||
@@ -689,34 +693,46 @@ Again, like the network management and ETH requests tasks, the transactions task
|
||||
|
||||
[File: crates/net/network/src/transactions.rs](https://github.com/paradigmxyz/reth/blob/1563506aea09049a85e5cc72c2894f3f7a371581/crates/net/network/src/transactions.rs)
|
||||
```rust,ignore
|
||||
pub struct TransactionsManager<Pool> {
|
||||
pub struct TransactionsManager<Pool, N: NetworkPrimitives = EthNetworkPrimitives> {
|
||||
/// Access to the transaction pool.
|
||||
pool: Pool,
|
||||
/// Network access.
|
||||
network: NetworkHandle,
|
||||
/// Subscriptions to all network-related events.
|
||||
network: NetworkHandle<N>,
|
||||
/// Subscriptions to all network related events.
|
||||
///
|
||||
/// From which we get all new incoming transaction-related messages.
|
||||
network_events: UnboundedReceiverStream<NetworkEvent>,
|
||||
/// All currently active requests for pooled transactions.
|
||||
inflight_requests: Vec<GetPooledTxRequest>,
|
||||
/// From which we get all new incoming transaction related messages.
|
||||
network_events: EventStream<NetworkEvent<PeerRequest<N>>>,
|
||||
/// Transaction fetcher to handle inflight and missing transaction requests.
|
||||
transaction_fetcher: TransactionFetcher<N>,
|
||||
/// All currently pending transactions grouped by peers.
|
||||
///
|
||||
/// This way we can track incoming transactions and prevent multiple pool imports for the same
|
||||
/// transaction
|
||||
transactions_by_peers: HashMap<TxHash, Vec<PeerId>>,
|
||||
/// Transactions that are currently imported into the `Pool`
|
||||
transactions_by_peers: HashMap<TxHash, HashSet<PeerId>>,
|
||||
/// Transactions that are currently imported into the `Pool`.
|
||||
pool_imports: FuturesUnordered<PoolImportFuture>,
|
||||
/// Stats on pending pool imports that help the node self-monitor.
|
||||
pending_pool_imports_info: PendingPoolImportsInfo,
|
||||
/// Bad imports.
|
||||
bad_imports: LruCache<TxHash>,
|
||||
/// All the connected peers.
|
||||
peers: HashMap<PeerId, Peer>,
|
||||
peers: HashMap<PeerId, PeerMetadata<N>>,
|
||||
/// Send half for the command channel.
|
||||
command_tx: mpsc::UnboundedSender<TransactionsCommand>,
|
||||
command_tx: mpsc::UnboundedSender<TransactionsCommand<N>>,
|
||||
/// Incoming commands from [`TransactionsHandle`].
|
||||
command_rx: UnboundedReceiverStream<TransactionsCommand>,
|
||||
/// Incoming commands from [`TransactionsHandle`].
|
||||
pending_transactions: ReceiverStream<TxHash>,
|
||||
command_rx: UnboundedReceiverStream<TransactionsCommand<N>>,
|
||||
/// A stream that yields new __pending__ transactions.
|
||||
pending_transactions: mpsc::Receiver<TxHash>,
|
||||
/// Incoming events from the [`NetworkManager`](crate::NetworkManager).
|
||||
transaction_events: UnboundedReceiverStream<NetworkTransactionEvent>,
|
||||
transaction_events: UnboundedMeteredReceiver<NetworkTransactionEvent<N>>,
|
||||
/// How the `TransactionsManager` is configured.
|
||||
config: TransactionsManagerConfig,
|
||||
/// Network Policies
|
||||
policies: NetworkPolicies<N>,
|
||||
/// `TransactionsManager` metrics
|
||||
metrics: TransactionsManagerMetrics,
|
||||
/// `AnnouncedTxTypes` metrics
|
||||
announced_tx_types_metrics: AnnouncedTxTypesMetrics,
|
||||
}
|
||||
```
|
||||
|
||||
@@ -870,23 +886,29 @@ Removes the peer given by `NetworkEvent::SessionClosed.peer_id` from the `Transa
|
||||
Closes the peer session after disconnection
|
||||
|
||||
**`PeerEvent::SessionEstablished`**
|
||||
Begins by inserting a `Peer` into `TransactionsManager.peers` by `peer_id`, which is a struct of the following form:
|
||||
Begins by inserting a `PeerMetadata` into `TransactionsManager.peers` by `peer_id`, which is a struct of the following form:
|
||||
|
||||
[File: crates/net/network/src/transactions.rs](https://github.com/paradigmxyz/reth/blob/1563506aea09049a85e5cc72c2894f3f7a371581/crates/net/network/src/transactions.rs)
|
||||
```rust,ignore
|
||||
struct Peer {
|
||||
/// Keeps track of transactions that we know the peer has seen.
|
||||
transactions: LruCache<B256>,
|
||||
/// A communication channel directly to the session task.
|
||||
request_tx: PeerRequestSender,
|
||||
pub struct PeerMetadata<N: NetworkPrimitives = EthNetworkPrimitives> {
|
||||
/// Optimistically keeps track of transactions that we know the peer has seen.
|
||||
seen_transactions: LruCache<TxHash>,
|
||||
/// A communication channel directly to the peer's session task.
|
||||
request_tx: PeerRequestSender<PeerRequest<N>>,
|
||||
/// Negotiated version of the session.
|
||||
version: EthVersion,
|
||||
/// The peer's client version.
|
||||
client_version: Arc<str>,
|
||||
/// The kind of peer.
|
||||
peer_kind: PeerKind,
|
||||
}
|
||||
```
|
||||
|
||||
Note that the `Peer` struct contains a field `transactions`, which is an [LRU cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)) of the transactions this peer is aware of.
|
||||
Note that the `PeerMetadata` struct contains a field `seen_transactions`, which is an [LRU cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)) of the transactions this peer is aware of.
|
||||
|
||||
The `request_tx` field on the `Peer` is used as the sender end of a channel to send requests to the session with the peer.
|
||||
The `request_tx` field on the `PeerMetadata` is used as the sender end of a channel to send requests to the session with the peer.
|
||||
|
||||
After the `Peer` is added to `TransactionsManager.peers`, the hashes of all of the transactions in the node's transaction pool are sent to the peer in a [`NewPooledTransactionHashes` message](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#newpooledtransactionhashes-0x08).
|
||||
After the `PeerMetadata` is added to `TransactionsManager.peers`, the hashes of all of the transactions in the node's transaction pool are sent to the peer in a [`NewPooledTransactionHashes` message](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#newpooledtransactionhashes-0x08).
|
||||
|
||||
[File: crates/net/network/src/transactions.rs](https://github.com/paradigmxyz/reth/blob/1563506aea09049a85e5cc72c2894f3f7a371581/crates/net/network/src/transactions.rs)
|
||||
```rust,ignore
|
||||
@@ -1015,7 +1037,7 @@ These events are handled with the `on_network_tx_event` method, which responds t
|
||||
|
||||
This event is generated from the [`Transactions` protocol message](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#transactions-0x02), and is handled by the `import_transactions` method.
|
||||
|
||||
Here, for each transaction in the variant's `msg` field, we attempt to recover the signer, insert the transaction into LRU cache of the `Peer` identified by the variant's `peer_id` field, and add the `peer_id` to the vector of peer IDs keyed by the transaction's hash in `TransactionsManager.transactions_by_peers`. If an entry does not already exist for the transaction hash, then it begins importing the transaction object into the node's transaction pool, adding a `PoolImportFuture` to `TransactionsManager.pool_imports`. If there was an issue recovering the signer, `report_bad_message` is called for the `peer_id`, which decreases the peer's reputation.
|
||||
Here, for each transaction in the variant's `msg` field, we attempt to recover the signer, insert the transaction into LRU cache of the `PeerMetadata` identified by the variant's `peer_id` field, and add the `peer_id` to the vector of peer IDs keyed by the transaction's hash in `TransactionsManager.transactions_by_peers`. If an entry does not already exist for the transaction hash, then it begins importing the transaction object into the node's transaction pool, adding a `PoolImportFuture` to `TransactionsManager.pool_imports`. If there was an issue recovering the signer, `report_bad_message` is called for the `peer_id`, which decreases the peer's reputation.
|
||||
|
||||
To understand this a bit better, let's double back and examine what `TransactionsManager.transactions_by_peers` and `TransactionsManager.pool_imports` are used for.
|
||||
|
||||
@@ -1071,7 +1093,7 @@ fn import_transactions(&mut self, peer_id: PeerId, transactions: Vec<Transaction
|
||||
|
||||
This event is generated from the [`NewPooledTransactionHashes` protocol message](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#newpooledtransactionhashes-0x08), and is handled by the `on_new_pooled_transactions` method.
|
||||
|
||||
Here, it begins by adding the transaction hashes included in the `NewPooledTransactionHashes` payload to the LRU cache for the `Peer` identified by `peer_id` in `TransactionsManager.peers`. Next, it filters the list of hashes to those that are not already present in the transaction pool, and for each such hash, requests its full transaction object from the peer by sending it a [`GetPooledTransactions` protocol message](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#getpooledtransactions-0x09) through the `Peer.request_tx` channel. If the request was successfully sent, a `GetPooledTxRequest` gets added to `TransactionsManager.inflight_requests` vector:
|
||||
Here, it begins by adding the transaction hashes included in the `NewPooledTransactionHashes` payload to the LRU cache for the `PeerMetadata` identified by `peer_id` in `TransactionsManager.peers`. Next, it filters the list of hashes to those that are not already present in the transaction pool, and for each such hash, requests its full transaction object from the peer by sending it a [`GetPooledTransactions` protocol message](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#getpooledtransactions-0x09) through the `PeerMetadata.request_tx` channel. If the request was successfully sent, a `GetPooledTxRequest` gets added to `TransactionsManager.inflight_requests` vector:
|
||||
|
||||
[File: crates/net/network/src/transactions.rs](https://github.com/paradigmxyz/reth/blob/1563506aea09049a85e5cc72c2894f3f7a371581/crates/net/network/src/transactions.rs)
|
||||
```rust,ignore
|
||||
|
||||
Reference in New Issue
Block a user