From 313c8386567150e324a76eab4fe4a65be3896787 Mon Sep 17 00:00:00 2001 From: Joseph Livesey Date: Tue, 29 Oct 2024 18:55:46 -0400 Subject: [PATCH] refactor(flat-files-decoder): make the receipts module immutable --- .../benches/stream_blocks.rs | 7 +--- crates/flat-files-decoder/src/error.rs | 5 +-- crates/flat-files-decoder/src/lib.rs | 38 +++++++++---------- .../flat-files-decoder/src/receipts/error.rs | 24 ------------ crates/flat-files-decoder/src/receipts/mod.rs | 27 ------------- 5 files changed, 23 insertions(+), 78 deletions(-) delete mode 100644 crates/flat-files-decoder/src/receipts/error.rs delete mode 100644 crates/flat-files-decoder/src/receipts/mod.rs diff --git a/crates/flat-files-decoder/benches/stream_blocks.rs b/crates/flat-files-decoder/benches/stream_blocks.rs index c0906386..af6d4e8e 100644 --- a/crates/flat-files-decoder/benches/stream_blocks.rs +++ b/crates/flat-files-decoder/benches/stream_blocks.rs @@ -4,10 +4,7 @@ use std::{ }; use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use flat_files_decoder::{ - dbin::{error::DbinFileError, DbinFile}, - receipts::check_receipt_root, -}; +use flat_files_decoder::dbin::{error::DbinFileError, DbinFile}; use prost::Message; const ITERS_PER_FILE: usize = 10; @@ -138,7 +135,7 @@ fn read_decode_check_bench(c: &mut Criterion) { ) .unwrap(); b.iter(|| { - black_box(check_receipt_root(&block)).unwrap(); + black_box(block.receipt_root_is_verified()); }); } } diff --git a/crates/flat-files-decoder/src/error.rs b/crates/flat-files-decoder/src/error.rs index fa1614b8..139162c8 100644 --- a/crates/flat-files-decoder/src/error.rs +++ b/crates/flat-files-decoder/src/error.rs @@ -1,6 +1,5 @@ use crate::dbin::error::DbinFileError; use crate::headers::error::BlockHeaderError; -use crate::receipts::error::ReceiptError; use thiserror::Error; use tokio::task::JoinError; @@ -16,8 +15,8 @@ pub enum DecodeError { BlockHeaderError(#[from] BlockHeaderError), #[error("Invalid Transaction Root")] TransactionRoot, - #[error("Invalid Receipt Root: {0}")] - ReceiptError(#[from] ReceiptError), + #[error("Invalid Receipt Root")] + ReceiptRoot, #[error("IO Error: {0}")] IoError(#[from] std::io::Error), #[error("Invalid content type: {0}")] diff --git a/crates/flat-files-decoder/src/lib.rs b/crates/flat-files-decoder/src/lib.rs index b4c0874c..26a4756e 100644 --- a/crates/flat-files-decoder/src/lib.rs +++ b/crates/flat-files-decoder/src/lib.rs @@ -7,7 +7,6 @@ pub mod dbin; pub mod error; pub mod headers; -pub mod receipts; pub mod transactions; use crate::{error::DecodeError, headers::check_valid_header}; @@ -15,7 +14,6 @@ use dbin::DbinFile; use firehose_protos::ethereum_v2::Block; use headers::HeaderRecordWithNumber; use prost::Message; -use receipts::check_receipt_root; use simple_log::log; use std::{ fs::{self, File}, @@ -202,8 +200,12 @@ fn handle_block( if let Some(headers_dir) = headers_dir { check_valid_header(&block, headers_dir)?; } + if block.number != 0 { - check_receipt_root(&block)?; + if !block.receipt_root_is_verified() { + return Err(DecodeError::ReceiptRoot); + } + if !block.transaction_root_is_verified() { return Err(DecodeError::TransactionRoot); } @@ -252,17 +254,17 @@ pub async fn stream_blocks( let block = decode_block_from_bytes(&message)?; block_number = block.number as usize; - let receipts_check_process = spawn_check(&block, |b| { - check_receipt_root(b).map_err(DecodeError::ReceiptError) - }); + let receipts_check_process = + spawn_check(&block, |b| match b.receipt_root_is_verified() { + true => Ok(()), + false => Err(DecodeError::ReceiptRoot), + }); - let transactions_check_process = spawn_check(&block, |b| { - if !b.transaction_root_is_verified() { - Err(DecodeError::TransactionRoot) - } else { - Ok(()) - } - }); + let transactions_check_process = + spawn_check(&block, |b| match b.transaction_root_is_verified() { + true => Ok(()), + false => Err(DecodeError::TransactionRoot), + }); let joint_return = join![receipts_check_process, transactions_check_process]; joint_return.0.map_err(DecodeError::JoinError)?; @@ -358,12 +360,7 @@ mod tests { // Remove an item from the block to make the receipt root invalid block.transaction_traces.pop(); - let result = check_receipt_root(&block); - - matches!( - result.unwrap_err(), - receipts::error::ReceiptError::MismatchedRoot(_, _) - ); + assert!(!block.receipt_root_is_verified()); } #[test] @@ -406,6 +403,9 @@ mod tests { .expect("Failed to read file"); let result = handle_buf(&buffer, Decompression::None); + if let Err(e) = result { + panic!("handle_buf failed: {}", e); + } assert!(result.is_ok(), "handle_buf should complete successfully"); } diff --git a/crates/flat-files-decoder/src/receipts/error.rs b/crates/flat-files-decoder/src/receipts/error.rs deleted file mode 100644 index 2be2bbb8..00000000 --- a/crates/flat-files-decoder/src/receipts/error.rs +++ /dev/null @@ -1,24 +0,0 @@ -use firehose_protos::error::ProtosError; -use thiserror::Error; - -#[derive(Error, Debug)] -pub enum ReceiptError { - #[error("Invalid status")] - InvalidStatus, - #[error("Invalid address: {0}")] - InvalidAddress(String), - #[error("Invalid topic: {0}")] - InvalidTopic(String), - #[error("Invalid data: {0}")] - InvalidBloom(String), - #[error("Receipt root mismatch: {0} != {1}")] - MismatchedRoot(String, String), - #[error("Missing receipt root")] - MissingRoot, - #[error("Missing receipt")] - MissingReceipt, - #[error("Protos error: {0}")] - ProtosError(#[from] ProtosError), - #[error("TryFromSliceError: {0}")] - TryFromSliceError(#[from] std::array::TryFromSliceError), -} diff --git a/crates/flat-files-decoder/src/receipts/mod.rs b/crates/flat-files-decoder/src/receipts/mod.rs deleted file mode 100644 index baa30c05..00000000 --- a/crates/flat-files-decoder/src/receipts/mod.rs +++ /dev/null @@ -1,27 +0,0 @@ -pub mod error; - -use crate::receipts::error::ReceiptError; -use firehose_protos::ethereum_v2::Block; -use revm_primitives::hex; - -/// Verifies the receipt root in a given block's header against a -/// computed receipt root from the block's body. -/// -/// # Arguments -/// -/// * `block` reference to the block which the root will be verified -pub fn check_receipt_root(block: &Block) -> Result<(), ReceiptError> { - let computed_root = block.calculate_receipt_root()?; - let receipt_root = match block.header { - Some(ref header) => header.receipt_root.as_slice(), - None => return Err(ReceiptError::MissingRoot), - }; - if computed_root.as_slice() != receipt_root { - return Err(ReceiptError::MismatchedRoot( - hex::encode(computed_root.as_slice()), - hex::encode(receipt_root), - )); - } - - Ok(()) -}