stratus/eth/primitives/
external_block_with_receipts.rs

1#[cfg(test)]
2use fake::Dummy;
3#[cfg(test)]
4use fake::Faker;
5use serde::Deserialize;
6use serde::Serialize;
7
8use crate::alias::JsonValue;
9use crate::eth::primitives::ExternalBlock;
10use crate::eth::primitives::ExternalReceipt;
11use crate::log_and_err;
12
13#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
14pub struct ExternalBlockWithReceipts {
15    pub block: ExternalBlock,
16    pub receipts: Vec<ExternalReceipt>,
17}
18
19#[cfg(test)]
20impl Dummy<Faker> for ExternalBlockWithReceipts {
21    fn dummy_with_rng<R: rand::Rng + ?Sized>(faker: &Faker, rng: &mut R) -> Self {
22        let block = ExternalBlock::dummy_with_rng(faker, rng);
23
24        let receipts = match &block.transactions {
25            alloy_rpc_types_eth::BlockTransactions::Full(txs) => txs.iter().map(|_| ExternalReceipt::dummy_with_rng(faker, rng)).collect(),
26            alloy_rpc_types_eth::BlockTransactions::Hashes(_) => Vec::new(),
27            alloy_rpc_types_eth::BlockTransactions::Uncle => Vec::new(),
28        };
29
30        Self { block, receipts }
31    }
32}
33
34// -----------------------------------------------------------------------------
35// Conversions: Other -> Self
36// -----------------------------------------------------------------------------
37
38impl TryFrom<JsonValue> for ExternalBlockWithReceipts {
39    type Error = anyhow::Error;
40
41    fn try_from(value: JsonValue) -> Result<Self, Self::Error> {
42        match ExternalBlockWithReceipts::deserialize(&value) {
43            Ok(v) => Ok(v),
44            Err(e) => log_and_err!(reason = e, payload = value, "failed to convert payload value to ExternalBlockWithReceipts"),
45        }
46    }
47}