Skip to content

Commit

Permalink
refactor(decoder): remove unnecessary pub visibility and unused
Browse files Browse the repository at this point in the history
  • Loading branch information
suchapalaver committed Oct 24, 2024
1 parent 6b704d9 commit 5523285
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 54 deletions.
2 changes: 1 addition & 1 deletion crates/flat-files-decoder/src/dbin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl DbinFile {

impl DbinFile {
/// Reads a single message
pub fn read_message<R: Read>(read: &mut R) -> Result<Vec<u8>, DbinFileError> {
fn read_message<R: Read>(read: &mut R) -> Result<Vec<u8>, DbinFileError> {
let mut size: [u8; 4] = [0; 4];
read.read_exact(&mut size)?;

Expand Down
5 changes: 1 addition & 4 deletions crates/flat-files-decoder/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ pub enum DecodeError {
JoinError(JoinError),
}

// Define an enum for all possible error types
#[derive(Debug)]
pub enum CheckError {
ReceiptError(ReceiptError), // Replace with actual error types
ReceiptError(ReceiptError),
TransactionError(TransactionError),
// Add more as needed
}

impl std::fmt::Display for CheckError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
CheckError::ReceiptError(e) => write!(f, "Receipt Error: {}", e),
CheckError::TransactionError(e) => write!(f, "Transaction Error: {}", e),
// Handle other errors
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/flat-files-decoder/src/headers/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ pub enum BlockHeaderError {
ReadError(#[from] std::io::Error),
#[error("JSON Error")]
JsonError(#[from] serde_json::Error),
#[error("Invalid input")]
InvalidInput,
#[error("Mismatched roots")]
MismatchedRoots(Box<(BlockHeaderRoots, BlockHeaderRoots)>),
#[error("Missing header")]
Expand Down
4 changes: 2 additions & 2 deletions crates/flat-files-decoder/src/headers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl TryFrom<BlockHeader> for BlockHeaderRoots {
}
}

pub fn check_valid_header(block: &Block, header_dir: &str) -> Result<(), BlockHeaderError> {
pub(crate) fn check_valid_header(block: &Block, header_dir: &str) -> Result<(), BlockHeaderError> {
let header_file_path = format!("{}/{}.json", header_dir, block.number);
let header_file = File::open(header_file_path)?;

Expand All @@ -49,7 +49,7 @@ pub fn check_valid_header(block: &Block, header_dir: &str) -> Result<(), BlockHe
}

#[derive(Serialize, Deserialize)]
pub struct HeaderRecordWithNumber {
pub(crate) struct HeaderRecordWithNumber {
pub block_hash: Vec<u8>,
pub total_difficulty: Vec<u8>,
pub block_number: u64,
Expand Down
36 changes: 3 additions & 33 deletions crates/flat-files-decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use error::CheckError;
use firehose_protos::ethereum_v2::Block;
use headers::HeaderRecordWithNumber;
use prost::Message;
use rayon::prelude::*;
use receipts::check_receipt_root;
use simple_log::log;
use std::{
Expand All @@ -37,31 +36,16 @@ pub enum Decompression {
None,
}

impl From<bool> for Decompression {
fn from(value: bool) -> Self {
if value {
Decompression::Zstd
} else {
Decompression::None
}
}
}

impl From<&str> for Decompression {
fn from(value: &str) -> Self {
match value {
"true" => Decompression::Zstd,
"false" => Decompression::None,
"true" | "1" => Decompression::Zstd,
"false" | "0" => Decompression::None,
_ => Decompression::None,
}
}
}

pub enum DecodeInput {
Path(String),
Reader(Box<dyn Read>),
}

/// Decodes and optionally verifies block flat files from a given directory or single file.
///
/// This function processes input which can be a file or a directory containing multiple `.dbin` files.
Expand Down Expand Up @@ -185,7 +169,7 @@ pub fn handle_file(

/// Decodes a flat file from a buffer containing its contents and optionally decompresses it.
///
/// Decodes flat files that are already loaded into memory, without direct file system ac cess.
/// Decodes flat files that are already loaded into memory, without direct file system access.
/// It can handle both compressed (if `zstd` decompression is specified) and uncompressed data. Upon successful
/// decoding, it returns a vector of all the blocks contained within the flat file. The actual number of blocks
/// returned depends on the format and content of the flat file—ranging from a single block to multiple blocks.
Expand Down Expand Up @@ -241,20 +225,6 @@ fn handle_block(
Ok(block)
}

/// Gets a vector of blocks from a single .dbin file
pub fn extract_blocks<R: Read>(mut reader: R) -> Result<Vec<Block>, DecodeError> {
log::debug!("Reading messages");
let dbin_file = DbinFile::try_from_read(&mut reader)?;
log::debug!("Validating blocks");

// Parallel processing of block headers
dbin_file
.messages
.par_iter()
.map(|message| handle_block(message, None, None))
.collect()
}

/// Decode blocks from a reader and writes them, serialized, to a writer
///
/// data can be piped into this function from stdin via `cargo run stream < ./example0017686312.dbin`.
Expand Down
6 changes: 3 additions & 3 deletions crates/flat-files-decoder/src/receipts/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::convert::TryInto;

use firehose_protos::ethereum_v2::Log as BlockLog;

pub fn map_logs(logs: &[BlockLog]) -> Result<Vec<Log>, ReceiptError> {
pub(crate) fn map_logs(logs: &[BlockLog]) -> Result<Vec<Log>, ReceiptError> {
logs.iter().map(block_log_to_log).collect()
}

pub fn block_log_to_log(log: &BlockLog) -> Result<Log, ReceiptError> {
pub(crate) fn block_log_to_log(log: &BlockLog) -> Result<Log, ReceiptError> {
let slice: [u8; 20] = log
.address
.as_slice()
Expand All @@ -26,7 +26,7 @@ pub fn block_log_to_log(log: &BlockLog) -> Result<Log, ReceiptError> {
Ok(Log { address, data })
}

fn map_topics(topics: &[Vec<u8>]) -> Result<Vec<B256>, ReceiptError> {
pub(crate) fn map_topics(topics: &[Vec<u8>]) -> Result<Vec<B256>, ReceiptError> {
topics.iter().map(map_topic).collect()
}

Expand Down
2 changes: 1 addition & 1 deletion crates/flat-files-decoder/src/receipts/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use firehose_protos::ethereum_v2::TransactionTrace;
use reth_primitives::{Log, Receipt, ReceiptWithBloom};
use revm_primitives::hex;

pub struct FullReceipt {
pub(crate) struct FullReceipt {
pub receipt: ReceiptWithBloom,
pub state_root: Vec<u8>,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/flat-files-decoder/src/transactions/access_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) fn compute_access_list(
Ok(AccessList(access_list_items))
}

pub fn atuple_to_alist_item(tuple: &AccessTuple) -> Result<AccessListItem, TransactionError> {
fn atuple_to_alist_item(tuple: &AccessTuple) -> Result<AccessListItem, TransactionError> {
let address: Address = Address::from_slice(tuple.address.as_slice());
let storage_keys = tuple
.storage_keys
Expand Down
2 changes: 1 addition & 1 deletion crates/flat-files-decoder/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn check_transaction_root(block: &Block) -> Result<(), TransactionError> {
Ok(())
}

pub fn bigint_to_u128(value: BigInt) -> Result<u128, TransactionError> {
pub(crate) fn bigint_to_u128(value: BigInt) -> Result<u128, TransactionError> {
let slice = value.bytes.as_slice();
let n = U128::try_from_be_slice(slice)
.ok_or(TransactionError::InvalidBigInt(hex::encode(slice)))?;
Expand Down
4 changes: 3 additions & 1 deletion crates/flat-files-decoder/src/transactions/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub enum InvalidSignatureError {
V(u8),
}

pub fn signature_from_trace(trace: &TransactionTrace) -> Result<Signature, InvalidSignatureError> {
pub(crate) fn signature_from_trace(
trace: &TransactionTrace,
) -> Result<Signature, InvalidSignatureError> {
let r_bytes: [u8; 32] = trace
.r
.as_slice()
Expand Down
6 changes: 4 additions & 2 deletions crates/flat-files-decoder/src/transactions/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use super::bigint_to_u128;

pub const CHAIN_ID: ChainId = 1;

pub fn trace_to_transaction(trace: &TransactionTrace) -> Result<Transaction, TransactionError> {
pub(crate) fn trace_to_transaction(
trace: &TransactionTrace,
) -> Result<Transaction, TransactionError> {
let tx_type = map_tx_type(&trace.r#type)?;

let nonce = trace.nonce;
Expand Down Expand Up @@ -102,7 +104,7 @@ pub fn trace_to_transaction(trace: &TransactionTrace) -> Result<Transaction, Tra
Ok(transaction)
}

pub fn get_tx_kind(trace: &TransactionTrace) -> Result<TxKind, TransactionError> {
fn get_tx_kind(trace: &TransactionTrace) -> Result<TxKind, TransactionError> {
let first_call = trace.calls.first().ok_or(TransactionError::MissingCall)?;

let call_type = first_call.call_type();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::str::FromStr;

use super::{signature::signature_from_trace, transaction::trace_to_transaction};

pub fn trace_to_signed(trace: &TransactionTrace) -> Result<TransactionSigned, TransactionError> {
pub(crate) fn trace_to_signed(
trace: &TransactionTrace,
) -> Result<TransactionSigned, TransactionError> {
let transaction = trace_to_transaction(trace)?;
let signature = signature_from_trace(trace)?;
let hash = FixedBytes::from_str(&hex::encode(trace.hash.as_slice()))
Expand Down
4 changes: 2 additions & 2 deletions crates/flat-files-decoder/src/transactions/tx_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum TransactionTypeError {
Missing,
}

pub fn map_tx_type(tx_type: &i32) -> Result<TxType, TransactionTypeError> {
let tx_type = Type::try_from(*tx_type).map_err(|_| TransactionTypeError::Missing)?; // 1
pub(crate) fn map_tx_type(tx_type: &i32) -> Result<TxType, TransactionTypeError> {
let tx_type = Type::try_from(*tx_type).map_err(|_| TransactionTypeError::Missing)?;
Ok(TxType::from(tx_type))
}

0 comments on commit 5523285

Please sign in to comment.