reth_rpc_convert/
block.rs

1//! Conversion traits for block responses to primitive block types.
2
3use alloy_network::Network;
4use std::convert::Infallible;
5
6/// Trait for converting network block responses to primitive block types.
7pub trait TryFromBlockResponse<N: Network> {
8    /// The error type returned if the conversion fails.
9    type Error: core::error::Error + Send + Sync + Unpin;
10
11    /// Converts a network block response to a primitive block type.
12    ///
13    /// # Returns
14    ///
15    /// Returns `Ok(Self)` on successful conversion, or `Err(Self::Error)` if the conversion fails.
16    fn from_block_response(block_response: N::BlockResponse) -> Result<Self, Self::Error>
17    where
18        Self: Sized;
19}
20
21impl<N: Network, T> TryFromBlockResponse<N> for alloy_consensus::Block<T>
22where
23    N::BlockResponse: Into<Self>,
24{
25    type Error = Infallible;
26
27    fn from_block_response(block_response: N::BlockResponse) -> Result<Self, Self::Error> {
28        Ok(block_response.into())
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use alloy_consensus::{Block, TxEnvelope};
36    use alloy_network::Ethereum;
37    use alloy_rpc_types_eth::BlockTransactions;
38
39    #[test]
40    fn test_try_from_block_response() {
41        let rpc_block: alloy_rpc_types_eth::Block =
42            alloy_rpc_types_eth::Block::new(Default::default(), BlockTransactions::Full(vec![]));
43        let result =
44            <Block<TxEnvelope> as TryFromBlockResponse<Ethereum>>::from_block_response(rpc_block);
45        assert!(result.is_ok());
46    }
47}