From 236b10f1c5964f4a271f686032a533ef5f9a8557 Mon Sep 17 00:00:00 2001 From: Alex Metelli Date: Sat, 26 Oct 2024 08:50:44 +0800 Subject: [PATCH] debug --- src/block_header.rs | 2 -- src/eras/genesis.rs | 4 ++-- src/lib.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/block_header.rs b/src/block_header.rs index 2091c6d..6668a70 100644 --- a/src/block_header.rs +++ b/src/block_header.rs @@ -140,8 +140,6 @@ pub trait BlockHeaderTrait { content.to_string() }; - println!("hex_str: {}", hex_str); - // Decode the hex string let bytes = hex::decode(&padded_content).expect("Failed to decode hex string"); diff --git a/src/eras/genesis.rs b/src/eras/genesis.rs index d64b0a8..22a8d60 100644 --- a/src/eras/genesis.rs +++ b/src/eras/genesis.rs @@ -63,9 +63,9 @@ impl BlockHeaderGenesis { /// A `BlockHeaderGenesis` instance containing the parsed and validated block header data. pub fn from_db_header(db_header: VerifiableBlockHeader) -> Self { let logs_bloom = ::hex_to_fixed_array::<256>( - &db_header.logs_bloom.unwrap_or_default(), + &db_header.logs_bloom.clone().unwrap_or_default(), ); - let nonce = ::hex_to_fixed_array::<8>(&db_header.nonce); + let nonce = ::hex_to_fixed_array::<8>(&db_header.nonce.clone()); BlockHeaderGenesis { parent_hash: H256::from_str(&db_header.parent_hash.unwrap_or_default()).unwrap(), diff --git a/src/lib.rs b/src/lib.rs index 3f7ca0b..19ab97d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,3 +65,54 @@ pub fn encode_block_header( pub fn decode_block_header(block_number: u64, encoded: &[u8]) -> Option { eras::determine_era_decoder(block_number).and_then(|decoder| decoder(encoded).ok()) } + +#[cfg(test)] +mod tests { + use super::{encode_block_header, decode_block_header}; + use crate::test_helpers::create_test_block_header_london; // Adjust import as needed + + #[test] + fn test_block_header_encoding_decoding_debug() { + // Create the test block header for London era + let original_header = create_test_block_header_london(); + let block_number = original_header.number as u64; + + println!("Original Header: {:#?}", original_header); + + // Step 1: Encode the block header + let encoded = encode_block_header(block_number, original_header.clone()) + .expect("Encoding failed"); + + println!("Encoded Bytes: {:?}", encoded); + + // Debug: Reprint encoded bytes as hex + let encoded_hex: String = encoded.iter().map(|b| format!("{:02x}", b)).collect(); + println!("Encoded Hex: {}", encoded_hex); + + // Step 2: Decode the block header + let decoded_header = decode_block_header(block_number, &encoded) + .expect("Decoding failed"); + + println!("Decoded Header: {:#?}", decoded_header); + + // Step 3: Compare individual fields for discrepancies + if original_header.parent_hash != decoded_header.parent_hash { + println!( + "Mismatch in parent_hash:\nOriginal: {}\nDecoded: {}", + original_header.parent_hash.clone().unwrap_or_default(), + decoded_header.parent_hash.clone().unwrap_or_default() + ); + } + assert_eq!(original_header.parent_hash, decoded_header.parent_hash, "Mismatch in parent_hash"); + + assert_eq!(original_header.nonce, decoded_header.nonce, "Mismatch in nonce"); + + assert_eq!( + original_header.base_fee_per_gas, decoded_header.base_fee_per_gas, + "Mismatch in base_fee_per_gas" + ); + + assert_eq!(original_header, decoded_header, "The original and decoded headers do not match"); + } + +}