pub struct ReorgDetector { /* private fields */ }Expand description
Chain reorganization detector.
Maintains a sliding window of block hashes and detects when the chain reorganizes by comparing new block hashes against stored ones.
Thread-safe via interior Mutexes — suitable for shared access across
Tokio tasks behind an Arc.
Implementations§
Source§impl ReorgDetector
impl ReorgDetector
Sourcepub fn new(config: ReorgConfig) -> Self
pub fn new(config: ReorgConfig) -> Self
Create a new reorg detector with the given configuration.
Sourcepub fn check_block(
&self,
block_number: u64,
block_hash: &str,
) -> Option<ReorgEvent>
pub fn check_block( &self, block_number: u64, block_hash: &str, ) -> Option<ReorgEvent>
Check a block against the window. Returns Some(ReorgEvent) if reorg detected.
Call this with each new block as it arrives.
Sourcepub async fn fetch_block_hash(
transport: &dyn RpcTransport,
block_number: u64,
) -> Result<Option<String>, TransportError>
pub async fn fetch_block_hash( transport: &dyn RpcTransport, block_number: u64, ) -> Result<Option<String>, TransportError>
Query the transport for a block hash at a given height.
Sourcepub async fn poll_and_check(
&self,
transport: &dyn RpcTransport,
) -> Result<Option<ReorgEvent>, TransportError>
pub async fn poll_and_check( &self, transport: &dyn RpcTransport, ) -> Result<Option<ReorgEvent>, TransportError>
Poll the chain for new blocks and check for reorgs.
- Gets the current block number
- Fetches the block hash
- Calls check_block
Returns any detected ReorgEvent.
Sourcepub fn safe_block(&self) -> Option<u64>
pub fn safe_block(&self) -> Option<u64>
Get the safe block number (current tip - safe_depth).
Sourcepub async fn fetch_finalized_block(
transport: &dyn RpcTransport,
) -> Result<u64, TransportError>
pub async fn fetch_finalized_block( transport: &dyn RpcTransport, ) -> Result<u64, TransportError>
Get the finalized block from the chain.
Sourcepub fn reorg_history(&self) -> Vec<ReorgEvent>
pub fn reorg_history(&self) -> Vec<ReorgEvent>
Get reorg history.
Sourcepub fn window_size(&self) -> usize
pub fn window_size(&self) -> usize
Number of blocks currently in the window.
Sourcepub fn is_block_safe(&self, block_number: u64) -> bool
pub fn is_block_safe(&self, block_number: u64) -> bool
Check if a block number is in the safe zone (below safe_depth).