|
| 1 | +use reth_network::{ |
| 2 | + import::{BlockImport as RethBlockImport, BlockValidation}, |
| 3 | + NetworkPrimitives, |
| 4 | +}; |
| 5 | +use reth_network_peers::PeerId; |
| 6 | +use reth_scroll_node::ScrollNetworkPrimitives; |
| 7 | +use reth_scroll_primitives::ScrollBlock; |
| 8 | +use scroll_wire::Event; |
| 9 | +use secp256k1::ecdsa::Signature; |
| 10 | +use std::{ |
| 11 | + sync::Arc, |
| 12 | + task::{Context, Poll}, |
| 13 | +}; |
| 14 | +use tokio::sync::mpsc::UnboundedSender; |
| 15 | +use tracing::{trace, warn}; |
| 16 | + |
| 17 | +const ECDSA_SIGNATURE_LEN: usize = 64; |
| 18 | + |
| 19 | +/// A block import implementation for the eth-wire protocol that sends block to the scroll-wire |
| 20 | +/// protocol. |
| 21 | +/// |
| 22 | +/// The block import implementation delegates the block import to the inner block import and then |
| 23 | +/// sends the block to the scroll-wire protocol if the block is valid. |
| 24 | +#[derive(Debug)] |
| 25 | +pub struct BridgeBlockImport { |
| 26 | + /// A sender for sending events to the scroll-wire protocol. |
| 27 | + to_scroll_network_manager: UnboundedSender<Event>, |
| 28 | + /// The inner block import. |
| 29 | + inner: Box<dyn RethBlockImport<reth_scroll_primitives::ScrollBlock>>, |
| 30 | +} |
| 31 | + |
| 32 | +impl BridgeBlockImport { |
| 33 | + /// Creates a new [`BridgeBlockImport`] instance with the provided events sender and inner block |
| 34 | + /// import. |
| 35 | + pub fn new( |
| 36 | + events: UnboundedSender<Event>, |
| 37 | + inner_block_import: Box<dyn RethBlockImport<reth_scroll_primitives::ScrollBlock>>, |
| 38 | + ) -> Self { |
| 39 | + Self { to_scroll_network_manager: events, inner: inner_block_import } |
| 40 | + } |
| 41 | + |
| 42 | + /// Bridges a new block from the eth-wire protocol to the scroll-wire protocol. |
| 43 | + fn bridge_new_block_to_scroll_wire( |
| 44 | + &self, |
| 45 | + peer_id: PeerId, |
| 46 | + block: Arc<reth_eth_wire_types::NewBlock<ScrollBlock>>, |
| 47 | + ) { |
| 48 | + // We create a reference to the extra data of the incoming block. |
| 49 | + let extra_data = &block.block.extra_data; |
| 50 | + |
| 51 | + // If we can extract a signature from the extra data we send the block to the scroll-wire |
| 52 | + // protocol. The signature is extracted from the last `ECDSA_SIGNATURE_LEN` bytes of the |
| 53 | + // extra data field. |
| 54 | + if let Some(signature) = extra_data |
| 55 | + .len() |
| 56 | + .checked_sub(ECDSA_SIGNATURE_LEN) |
| 57 | + .and_then(|i| Signature::from_compact(&extra_data[i..]).ok()) |
| 58 | + { |
| 59 | + let block = block.block.clone(); |
| 60 | + trace!(target: "bridge::import", peer_id = %peer_id, block = ?block, "Received new block from eth-wire protocol"); |
| 61 | + |
| 62 | + // We trigger a new block event to be sent to the rollup node's network manager. If this |
| 63 | + // results in an error it means the network manager has been dropped. |
| 64 | + let _ = |
| 65 | + self.to_scroll_network_manager.send(Event::NewBlock { peer_id, block, signature }); |
| 66 | + } else { |
| 67 | + warn!(target: "bridge::import", peer_id = %peer_id, "Failed to extract signature from block extra data"); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl RethBlockImport<reth_scroll_primitives::ScrollBlock> for BridgeBlockImport { |
| 73 | + /// This function is called when a new block is received from the network, it delegates the |
| 74 | + /// block import to the inner block import. |
| 75 | + fn on_new_block( |
| 76 | + &mut self, |
| 77 | + peer_id: PeerId, |
| 78 | + incoming_block: reth_network::message::NewBlockMessage< |
| 79 | + <ScrollNetworkPrimitives as NetworkPrimitives>::Block, |
| 80 | + >, |
| 81 | + ) { |
| 82 | + // We then delegate the block import to the inner block import. |
| 83 | + self.inner.on_new_block(peer_id, incoming_block); |
| 84 | + } |
| 85 | + |
| 86 | + /// This function is called when the block import is polled. |
| 87 | + /// |
| 88 | + /// If the block import is ready we check if the block is valid and if it is we send the block |
| 89 | + /// to the scroll-wire protocol and then return the outcome. |
| 90 | + fn poll( |
| 91 | + &mut self, |
| 92 | + cx: &mut Context<'_>, |
| 93 | + ) -> Poll< |
| 94 | + reth_network::import::BlockImportOutcome< |
| 95 | + <ScrollNetworkPrimitives as NetworkPrimitives>::Block, |
| 96 | + >, |
| 97 | + > { |
| 98 | + if let Poll::Ready(outcome) = self.inner.poll(cx) { |
| 99 | + match outcome.result { |
| 100 | + Ok(BlockValidation::ValidBlock { ref block }) | |
| 101 | + Ok(BlockValidation::ValidHeader { ref block }) => { |
| 102 | + self.bridge_new_block_to_scroll_wire(outcome.peer, block.block.clone()); |
| 103 | + return Poll::Ready(outcome) |
| 104 | + } |
| 105 | + Err(_) => Poll::Ready(outcome), |
| 106 | + } |
| 107 | + } else { |
| 108 | + return Poll::Pending; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments