Skip to content

Commit

Permalink
refactor(flat-files-decoder): make the receipts module immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
suchapalaver committed Oct 30, 2024
1 parent 1ba0b8e commit 313c838
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 78 deletions.
7 changes: 2 additions & 5 deletions crates/flat-files-decoder/benches/stream_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
});
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/flat-files-decoder/src/error.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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}")]
Expand Down
38 changes: 19 additions & 19 deletions crates/flat-files-decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
pub mod dbin;
pub mod error;
pub mod headers;
pub mod receipts;
pub mod transactions;

use crate::{error::DecodeError, headers::check_valid_header};
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},
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -252,17 +254,17 @@ pub async fn stream_blocks<R: Read, W: Write>(
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)?;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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");
}

Expand Down
24 changes: 0 additions & 24 deletions crates/flat-files-decoder/src/receipts/error.rs

This file was deleted.

27 changes: 0 additions & 27 deletions crates/flat-files-decoder/src/receipts/mod.rs

This file was deleted.

0 comments on commit 313c838

Please sign in to comment.