Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Oct 26, 2024
1 parent 347522d commit 236b10f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
4 changes: 2 additions & 2 deletions src/eras/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Self as BlockHeaderTrait>::hex_to_fixed_array::<256>(
&db_header.logs_bloom.unwrap_or_default(),
&db_header.logs_bloom.clone().unwrap_or_default(),
);
let nonce = <Self as BlockHeaderTrait>::hex_to_fixed_array::<8>(&db_header.nonce);
let nonce = <Self as BlockHeaderTrait>::hex_to_fixed_array::<8>(&db_header.nonce.clone());

BlockHeaderGenesis {
parent_hash: H256::from_str(&db_header.parent_hash.unwrap_or_default()).unwrap(),
Expand Down
51 changes: 51 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,54 @@ pub fn encode_block_header(
pub fn decode_block_header(block_number: u64, encoded: &[u8]) -> Option<VerifiableBlockHeader> {
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");
}

}

0 comments on commit 236b10f

Please sign in to comment.