Skip to content

Commit

Permalink
refactor(firehose-protos): reorganize ethereum v1 module
Browse files Browse the repository at this point in the history
  • Loading branch information
suchapalaver committed Oct 25, 2024
1 parent 5523285 commit a83c70a
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 218 deletions.
218 changes: 0 additions & 218 deletions crates/firehose-protos/src/ethereum_v2.rs

This file was deleted.

130 changes: 130 additions & 0 deletions crates/firehose-protos/src/ethereum_v2/eth_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use super::Block;
use alloy_primitives::{Address, Bloom, FixedBytes, Uint};
use ethportal_api::types::execution::header::Header;
use prost::Message;
use prost_wkt_types::Any;

use crate::{
error::ProtosError,
firehose::v2::{Response, SingleBlockResponse},
};

impl TryFrom<&Block> for Header {
type Error = ProtosError;

fn try_from(block: &Block) -> Result<Self, Self::Error> {
let block_header = block
.header
.as_ref()
.ok_or(ProtosError::BlockConversionError)?;

let parent_hash = FixedBytes::from_slice(block_header.parent_hash.as_slice());
let uncles_hash = FixedBytes::from_slice(block_header.uncle_hash.as_slice());
let author = Address::from_slice(block_header.coinbase.as_slice());
let state_root = FixedBytes::from_slice(block_header.state_root.as_slice());
let transactions_root = FixedBytes::from_slice(block_header.transactions_root.as_slice());
let receipts_root = FixedBytes::from_slice(block_header.receipt_root.as_slice());
let logs_bloom = Bloom::from_slice(block_header.logs_bloom.as_slice());
let difficulty = Uint::from_be_slice(
block_header
.difficulty
.as_ref()
.ok_or(ProtosError::BlockConversionError)?
.bytes
.as_slice(),
);
let number = block_header.number;
let gas_limit = Uint::from(block_header.gas_limit);
let gas_used = Uint::from(block_header.gas_used);
let timestamp = block_header
.timestamp
.as_ref()
.ok_or(ProtosError::BlockConversionError)?
.seconds as u64;
let extra_data = block_header.extra_data.clone();
let mix_hash = Some(FixedBytes::from_slice(block_header.mix_hash.as_slice()));
let nonce = Some(FixedBytes::from_slice(&block_header.nonce.to_be_bytes()));
let base_fee_per_gas = block_header
.base_fee_per_gas
.as_ref()
.map(|base_fee_per_gas| Uint::from_be_slice(base_fee_per_gas.bytes.as_slice()));
let withdrawals_root = match block_header.withdrawals_root.is_empty() {
true => None,
false => Some(FixedBytes::from_slice(
block_header.withdrawals_root.as_slice(),
)),
};
let blob_gas_used = block_header.blob_gas_used.map(Uint::from);
let excess_blob_gas = block_header.excess_blob_gas.map(Uint::from);
let parent_beacon_block_root = match block_header.parent_beacon_root.is_empty() {
true => None,
false => Some(FixedBytes::from_slice(
block_header.parent_beacon_root.as_slice(),
)),
};

Ok(Header {
parent_hash,
uncles_hash,
author,
state_root,
transactions_root,
receipts_root,
logs_bloom,
difficulty,
number,
gas_limit,
gas_used,
timestamp,
extra_data,
mix_hash,
nonce,
base_fee_per_gas,
withdrawals_root,
blob_gas_used,
excess_blob_gas,
parent_beacon_block_root,
})
}
}

fn decode_block<M>(response: M) -> Result<Block, ProtosError>
where
M: MessageWithBlock,
{
let any = response.block().ok_or(ProtosError::NullBlock)?;
let block = Block::decode(any.value.as_ref())?;
Ok(block)
}

trait MessageWithBlock {
fn block(&self) -> Option<&Any>;
}

impl MessageWithBlock for SingleBlockResponse {
fn block(&self) -> Option<&Any> {
self.block.as_ref()
}
}

impl MessageWithBlock for Response {
fn block(&self) -> Option<&Any> {
self.block.as_ref()
}
}

impl TryFrom<SingleBlockResponse> for Block {
type Error = ProtosError;

fn try_from(response: SingleBlockResponse) -> Result<Self, Self::Error> {
decode_block(response)
}
}

impl TryFrom<Response> for Block {
type Error = ProtosError;

fn try_from(response: Response) -> Result<Self, Self::Error> {
decode_block(response)
}
}
Loading

0 comments on commit a83c70a

Please sign in to comment.