Skip to content

Commit

Permalink
refactor(decoder): move receipt log conversions to firehose-protos
Browse files Browse the repository at this point in the history
  • Loading branch information
suchapalaver committed Oct 25, 2024
1 parent 5cf5124 commit 6bdcb09
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 118 deletions.
152 changes: 76 additions & 76 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions crates/firehose-protos/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ pub enum ProtosError {
#[error("GraffitiInvalid")]
GraffitiInvalid,

#[error("Invalid log address: {0}")]
InvalidLogAddress(String),

#[error("Invalid log topic: {0}")]
InvalidLogTopic(String),

#[error("KzgCommitmentInvalid")]
KzgCommitmentInvalid,

Expand Down
46 changes: 46 additions & 0 deletions crates/firehose-protos/src/ethereum_v2/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use alloy_primitives::{hex, Address, Bytes, B256};
use reth_primitives::LogData;

use crate::error::ProtosError;

use super::Log;

impl TryFrom<&Log> for alloy_primitives::Log {
type Error = ProtosError;

fn try_from(log: &Log) -> Result<Self, Self::Error> {
let address = Address::try_from(log)?;
let topics = log.to_topics()?;
let log_data = Bytes::copy_from_slice(log.data.as_slice());
let data = LogData::new_unchecked(topics, log_data);

Ok(alloy_primitives::Log { address, data })
}
}

impl TryFrom<&Log> for Address {
type Error = ProtosError;

fn try_from(log: &Log) -> Result<Self, Self::Error> {
let slice: [u8; 20] = log
.address
.as_slice()
.try_into()
.map_err(|_| Self::Error::InvalidLogAddress(hex::encode(log.address.clone())))?;
Ok(Address::from(slice))
}
}

impl Log {
fn to_topics(&self) -> Result<Vec<B256>, ProtosError> {
fn to_b256(slice: &[u8]) -> Result<B256, ProtosError> {
B256::try_from(slice).map_err(|_| ProtosError::InvalidLogTopic(hex::encode(slice)))
}

self.topics
.iter()
.map(Vec::as_slice)
.map(to_b256)
.collect::<Result<Vec<_>, _>>()
}
}
1 change: 1 addition & 0 deletions crates/firehose-protos/src/ethereum_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//!

pub mod eth_block;
pub mod log;
pub mod transaction;

tonic::include_proto!("sf.ethereum.r#type.v2");
3 changes: 3 additions & 0 deletions crates/flat-files-decoder/src/receipts/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::transactions::tx_type::TransactionTypeError;
use firehose_protos::error::ProtosError;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -19,4 +20,6 @@ pub enum ReceiptError {
MissingRoot,
#[error("Missing receipt")]
MissingReceipt,
#[error("Protos error: {0}")]
ProtosError(#[from] ProtosError),
}
39 changes: 0 additions & 39 deletions crates/flat-files-decoder/src/receipts/logs.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/flat-files-decoder/src/receipts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod error;
pub mod logs;
pub mod receipt;

use crate::receipts::error::ReceiptError;
Expand Down
8 changes: 6 additions & 2 deletions crates/flat-files-decoder/src/receipts/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::receipts::error::ReceiptError;
use crate::receipts::logs::map_logs;
use crate::transactions::tx_type::map_tx_type;
use alloy_primitives::{Bloom, FixedBytes};
use firehose_protos::ethereum_v2::TransactionTrace;
Expand All @@ -21,7 +20,12 @@ impl TryFrom<&TransactionTrace> for FullReceipt {
Some(receipt) => receipt,
None => return Err(ReceiptError::MissingReceipt),
};
let logs: Vec<Log> = map_logs(&trace_receipt.logs)?;
let logs = trace_receipt
.logs
.iter()
.map(Log::try_from)
.collect::<Result<Vec<_>, _>>()?;

let cumulative_gas_used = trace_receipt.cumulative_gas_used;

let receipt = Receipt {
Expand Down

0 comments on commit 6bdcb09

Please sign in to comment.