loom_node_grpc_exex_proto/
codec_extra.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::proto;
use crate::proto::tx_kind::Kind;
use alloy_primitives::{Address, BlockHash, B256};
use eyre::OptionExt;

impl TryFrom<&proto::BundleState> for reth::revm::db::BundleState {
    type Error = eyre::Error;

    fn try_from(bundle: &crate::proto::BundleState) -> Result<Self, Self::Error> {
        let ret = reth::revm::db::BundleState {
            state: bundle.state.iter().map(TryInto::try_into).collect::<eyre::Result<_>>()?,
            contracts: bundle
                .contracts
                .iter()
                .map(|contract| {
                    Ok((B256::try_from(contract.hash.as_slice())?, contract.bytecode.as_ref().ok_or_eyre("no bytecode")?.try_into()?))
                })
                .collect::<eyre::Result<_>>()?,
            reverts: reth::revm::db::states::reverts::Reverts::new(
                bundle
                    .reverts
                    .iter()
                    .map(|block_reverts| block_reverts.reverts.iter().map(TryInto::try_into).collect::<eyre::Result<_>>())
                    .collect::<eyre::Result<_>>()?,
            ),
            state_size: bundle.state_size as usize,
            reverts_size: bundle.reverts_size as usize,
        };
        Ok(ret)
    }
}

impl From<proto::TxKind> for Address {
    fn from(tx_kind: proto::TxKind) -> Self {
        match tx_kind.kind {
            Some(kind) => match kind {
                Kind::Create(_address) => Address::ZERO,
                Kind::Call(address) => Address::from_slice(address.as_slice()),
            },
            _ => Address::ZERO,
        }
    }
}

impl TryFrom<proto::SealedHeader> for reth::primitives::SealedHeader {
    type Error = eyre::Error;

    fn try_from(sealed_header: proto::SealedHeader) -> Result<Self, Self::Error> {
        let header = sealed_header.header.as_ref().ok_or_eyre("no header")?;
        Ok(reth::primitives::SealedHeader::new(header.try_into()?, BlockHash::try_from(sealed_header.hash.as_slice())?))
    }
}