Skip to content

Commit

Permalink
refactor(header-accumulator): clean up tests dependent on binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
suchapalaver committed Nov 6, 2024
1 parent d451a70 commit 144f29d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ fn create_test_reader(path: &str) -> BufReader<File> {
BufReader::new(File::open(path).unwrap())
}

#[test]
fn test_era_validate() -> Result<(), EraValidateError> {
fn main() -> Result<(), EraValidateError> {
let mut headers: Vec<ExtHeaderRecord> = Vec::new();

for number in (0..=8200).step_by(100) {
let file_name = format!("tests/ethereum_firehose_first_8200/{:010}.dbin", number);
let file_name = format!(
"your-test-assets/ethereum_firehose_first_8200/{:010}.dbin",
number
);
let reader = create_test_reader(&file_name);
let blocks = read_blocks_from_reader(reader, Compression::None).unwrap();
let successful_headers = blocks
Expand All @@ -23,19 +26,21 @@ fn test_era_validate() -> Result<(), EraValidateError> {
.collect::<Result<Vec<_>, _>>()?;
headers.extend(successful_headers);
}

assert_eq!(headers.len(), 8300);
assert_eq!(headers[0].block_number, 0);

let premerge_accumulator: EraValidator = PreMergeAccumulator::default().into();
let epoch: Epoch = headers.try_into().unwrap();

let result = premerge_accumulator.validate_era(&epoch)?;

let expected = Hash256::new([
94, 193, 255, 184, 195, 177, 70, 244, 38, 6, 199, 76, 237, 151, 61, 193, 110, 197, 161, 7,
192, 52, 88, 88, 195, 67, 252, 148, 120, 11, 66, 24,
]);
assert_eq!(result, expected);

println!("Era validated successfully!");

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ fn create_test_reader(path: &str) -> BufReader<File> {
BufReader::new(File::open(path).unwrap())
}

#[test]
fn test_inclusion_proof() -> Result<(), EraValidateError> {
fn main() -> Result<(), EraValidateError> {
let mut headers: Vec<ExtHeaderRecord> = Vec::new();
let mut all_blocks: Vec<Block> = Vec::new(); // Vector to hold all blocks
let mut all_blocks: Vec<Block> = Vec::new();

for flat_file_number in (0..=8200).step_by(100) {
let file = format!(
"tests/ethereum_firehose_first_8200/{:010}.dbin",
"your-test-assets/ethereum_firehose_first_8200/{:010}.dbin",
flat_file_number
);
match read_blocks_from_reader(create_test_reader(&file), Compression::None) {
Expand All @@ -28,7 +27,7 @@ fn test_inclusion_proof() -> Result<(), EraValidateError> {
.map(|block| ExtHeaderRecord::try_from(block).unwrap())
.collect::<Vec<ExtHeaderRecord>>(),
);
all_blocks.extend(blocks); // Extend the all_blocks vector with the decoded blocks
all_blocks.extend(blocks);
}
Err(e) => {
eprintln!("error: {:?}", e);
Expand All @@ -42,8 +41,7 @@ fn test_inclusion_proof() -> Result<(), EraValidateError> {
let inclusion_proof =
generate_inclusion_proof(headers, start_block, end_block).unwrap_or_else(|e| {
println!("Error occurred: {}", e);
// Handle the error, e.g., by exiting the program or returning a default value
std::process::exit(1); // Exiting the program, for example
std::process::exit(1);
});
assert_eq!(
inclusion_proof.len() as usize,
Expand All @@ -54,9 +52,11 @@ fn test_inclusion_proof() -> Result<(), EraValidateError> {
let proof_blocks: Vec<Block> = all_blocks[start_block as usize..=end_block as usize].to_vec();
assert!(verify_inclusion_proof(proof_blocks, None, inclusion_proof.clone()).is_ok());

// verify if inclusion proof fails on not proven blocks
// Verify if inclusion proof fails on not proven blocks
let proof_blocks: Vec<Block> = all_blocks[302..=403].to_vec();
assert!(verify_inclusion_proof(proof_blocks, None, inclusion_proof.clone()).is_err());

println!("Inclusion proof verified successfully!");

Ok(())
}

0 comments on commit 144f29d

Please sign in to comment.