|
| 1 | +use alloy_primitives::PrimitiveSignature; |
| 2 | +use reth_network::NetworkHandle as RethNetworkHandle; |
| 3 | +use reth_network_peers::PeerId; |
| 4 | +use reth_primitives::Block; |
| 5 | +use secp256k1::SecretKey; |
| 6 | +use std::sync::Arc; |
| 7 | +use tokio::sync::{mpsc::UnboundedSender, oneshot}; |
| 8 | + |
| 9 | +/// A _sharable_ frontend used to communicate with the [`NetworkManager`]. |
| 10 | +#[derive(Debug, Clone)] |
| 11 | +pub struct NetworkHandle { |
| 12 | + /// A reference to the inner network handle. |
| 13 | + pub(crate) inner: Arc<NetworkInner>, |
| 14 | +} |
| 15 | + |
| 16 | +impl NetworkHandle { |
| 17 | + /// Creates a new [`NetworkHandle`] instance from the given [`UnboundedSender`] and [`RethNetworkHandle`]. |
| 18 | + pub fn new( |
| 19 | + to_manager_tx: UnboundedSender<NetworkHandleMessage>, |
| 20 | + inner_network_handle: RethNetworkHandle, |
| 21 | + ) -> Self { |
| 22 | + let inner = NetworkInner { |
| 23 | + to_manager_tx, |
| 24 | + inner_network_handle, |
| 25 | + }; |
| 26 | + Self { |
| 27 | + inner: Arc::new(inner), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// The inner state of the [`NetworkHandle`]. |
| 33 | +#[derive(Debug)] |
| 34 | +pub struct NetworkInner { |
| 35 | + /// The sender half of the channel set up between this type and the [`NetworkManager`]. |
| 36 | + pub(crate) to_manager_tx: UnboundedSender<NetworkHandleMessage>, |
| 37 | + /// A reference to the inner network handle which is used to communicate with the inner network. |
| 38 | + pub inner_network_handle: RethNetworkHandle, |
| 39 | +} |
| 40 | + |
| 41 | +impl NetworkHandle { |
| 42 | + /// Returns a reference to the inner network handle. |
| 43 | + pub fn inner(&self) -> &RethNetworkHandle { |
| 44 | + &self.inner.inner_network_handle |
| 45 | + } |
| 46 | + |
| 47 | + /// Returns the peer id of the network handle. |
| 48 | + pub fn peer_id(&self) -> &PeerId { |
| 49 | + &self.inner.inner_network_handle.peer_id() |
| 50 | + } |
| 51 | + |
| 52 | + /// Sends a message to the network manager. |
| 53 | + pub fn send_message(&self, msg: NetworkHandleMessage) { |
| 54 | + let _ = self.inner.to_manager_tx.send(msg); |
| 55 | + } |
| 56 | + |
| 57 | + /// Announces a block to the network. |
| 58 | + pub fn announce_block(&self, block: Block, signature: PrimitiveSignature) { |
| 59 | + self.send_message(NetworkHandleMessage::AnnounceBlock { block, signature }); |
| 60 | + } |
| 61 | + |
| 62 | + /// Shuts down the network handle. |
| 63 | + pub async fn shutdown(&self) -> Result<(), oneshot::error::RecvError> { |
| 64 | + let (tx, rx) = oneshot::channel(); |
| 65 | + self.send_message(NetworkHandleMessage::Shutdown(tx)); |
| 66 | + rx.await |
| 67 | + } |
| 68 | + |
| 69 | + /// Returns the secret key of the network handle. |
| 70 | + pub fn secret_key(&self) -> &SecretKey { |
| 71 | + &self.inner.inner_network_handle.secret_key() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// A message type used for communication between the [`NetworkHandle`] and the [`super::NetworkManager`]. |
| 76 | +pub enum NetworkHandleMessage { |
| 77 | + AnnounceBlock { |
| 78 | + block: Block, |
| 79 | + signature: PrimitiveSignature, |
| 80 | + }, |
| 81 | + Shutdown(oneshot::Sender<()>), |
| 82 | +} |
0 commit comments