|
| 1 | +use reth::{ |
| 2 | + core::primitives::SealedBlock, |
| 3 | + primitives::{Block, RecoveredBlock}, |
| 4 | + providers::{CanonStateNotification, Chain}, |
| 5 | +}; |
| 6 | +use signet_types::MagicSig; |
| 7 | +use std::sync::Arc; |
1 | 8 |
|
| 9 | +/// Removes Signet system transactions from the block. |
| 10 | +fn strip_block(block: RecoveredBlock<Block>) -> RecoveredBlock<Block> { |
| 11 | + let (sealed, senders) = block.split_sealed(); |
| 12 | + let (header, mut body) = sealed.split_sealed_header_body(); |
| 13 | + |
| 14 | + // This is the index of the first transaction that has a system magic |
| 15 | + // signature. |
| 16 | + let sys_index = body |
| 17 | + .transactions |
| 18 | + .partition_point(|tx| MagicSig::try_from_signature(tx.signature()).is_some()); |
| 19 | + |
| 20 | + body.transactions.truncate(sys_index); |
| 21 | + |
| 22 | + let sealed = SealedBlock::from_sealed_parts(header, body); |
| 23 | + RecoveredBlock::new_sealed(sealed, senders) |
| 24 | +} |
| 25 | + |
| 26 | +/// Removes Signet system transactions from the chain. This function uses |
| 27 | +/// `Arc::make_mut` to clone the contents of the Arc and modify the new |
| 28 | +/// instance. |
| 29 | +fn strip_chain(chain: &mut Arc<Chain>) { |
| 30 | + let chain = Arc::make_mut(chain); |
| 31 | + |
| 32 | + let (blocks, outcome, trie) = std::mem::take(chain).into_inner(); |
| 33 | + let blocks = blocks.into_blocks().map(strip_block); |
| 34 | + |
| 35 | + *chain = Chain::new(blocks, outcome, trie); |
| 36 | +} |
| 37 | + |
| 38 | +/// Strips Signet system transactions from the `CanonStateNotification`. |
| 39 | +pub(crate) fn strip_signet_system_txns(notif: CanonStateNotification) -> CanonStateNotification { |
| 40 | + // Cloning here ensures that the `make_mut` invocations in |
| 41 | + // `strip_chain` do not ever modify the original `notif` object. |
| 42 | + let _c = notif.clone(); |
| 43 | + |
| 44 | + match notif { |
| 45 | + CanonStateNotification::Commit { mut new } => { |
| 46 | + strip_chain(&mut new); |
| 47 | + CanonStateNotification::Commit { new } |
| 48 | + } |
| 49 | + CanonStateNotification::Reorg { mut old, mut new } => { |
| 50 | + strip_chain(&mut old); |
| 51 | + strip_chain(&mut new); |
| 52 | + |
| 53 | + CanonStateNotification::Reorg { old, new } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments