Skip to content

Commit

Permalink
refactor(era_validator): uses MasterAccumulator instead of string to …
Browse files Browse the repository at this point in the history
…represent a path
  • Loading branch information
pedrohba1 committed Apr 2, 2024
1 parent 88e6b3f commit aeab1e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
17 changes: 4 additions & 13 deletions src/era_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,20 @@ use crate::{
///
/// * `headers`- A mutable vector of [`ExtHeaderRecord`]. The Vector can be any size, however, it must be in chunks of 8192 blocks to work properly
/// to function without error
/// * `master_accumulator_file`- An instance of [`MasterAccumulator`] which is a file that maintains a record of historical epoch
/// * `macc`- An instance of [`MasterAccumulator`] which is a representation of a file that maintains a record of historical epoch
/// it is used to verify canonical-ness of headers accumulated from the `blocks`
/// * `start_epoch` - The epoch number that all the first 8192 blocks are set located
/// * `end_epoch` - The epoch number that all the last 8192 blocks are located
/// * `use_lock` - when set to true, uses the lockfile to store already processed blocks. True by default
pub fn era_validate(
mut headers: Vec<ExtHeaderRecord>,
master_accumulator_file: Option<&String>,
macc: MasterAccumulator,
start_epoch: usize,
end_epoch: Option<usize>,
use_lock: Option<bool>, // Changed to Option<bool>
) -> Result<Vec<usize>, EraValidateError> {
let use_lock = use_lock.unwrap_or(true);

// Load master accumulator if available, otherwise use default from Portal Network
let master_accumulator = match master_accumulator_file {
Some(master_accumulator_file) => {
MasterAccumulator::try_from_file(master_accumulator_file.into())
.map_err(|_| EraValidateError::InvalidMasterAccumulatorFile)?
}
None => MasterAccumulator::default(),
};

let end_epoch = match end_epoch {
Some(end_epoch) => end_epoch,
None => start_epoch + 1,
Expand All @@ -60,7 +51,7 @@ pub fn era_validate(
match check_sync_state(
Path::new("./lockfile.json"),
epoch.to_string(),
master_accumulator.historical_epochs[epoch].0,
macc.historical_epochs[epoch].0,
) {
Ok(true) => {
log::info!("Skipping, epoch already synced: {}", epoch);
Expand All @@ -78,7 +69,7 @@ pub fn era_validate(
}
}
let epoch_headers: Vec<ExtHeaderRecord> = headers.drain(0..MAX_EPOCH_SIZE).collect();
let root = process_headers(epoch_headers, epoch, &master_accumulator)?;
let root = process_headers(epoch_headers, epoch, &macc)?;
validated_epochs.push(epoch);
// stores the validated epoch into lockfile to avoid validating again and keeping a concise state
if use_lock {
Expand Down
17 changes: 11 additions & 6 deletions tests/era_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use decoder::decode_flat_files;
use header_accumulator::{
era_validator::era_validate, errors::EraValidateError, types::ExtHeaderRecord,
};
use trin_validation::accumulator::MasterAccumulator;

#[test]
fn test_era_validate() -> Result<(), EraValidateError> {
Expand Down Expand Up @@ -43,20 +44,21 @@ fn test_era_validate() -> Result<(), EraValidateError> {
}
assert_eq!(headers.len(), 8300);
assert_eq!(headers[0].block_number, 0);
let macc = MasterAccumulator::default();

let result = era_validate(headers.clone(), None, 0, None, Some(false))?;
let result = era_validate(headers.clone(), macc.clone(), 0, None, Some(false))?;
println!("result 1: {:?}", result);

assert!(result.contains(&0), "The vector does not contain 0");

// Test with creating a lockfile
let result = era_validate(headers.clone(), None, 0, None, Some(true))?;
let result = era_validate(headers.clone(), macc.clone(), 0, None, Some(true))?;
println!("result 2: {:?}", result);

assert!(result.contains(&0), "The vector does not contain 0");

// test with the lockfile created before.
let result = era_validate(headers.clone(), None, 0, None, Some(true))?;
let result = era_validate(headers.clone(), macc.clone(), 0, None, Some(true))?;

// already validated epochs are not included in the array.
assert_eq!(result.len(), 0);
Expand Down Expand Up @@ -110,19 +112,22 @@ fn test_era_validate_compressed() -> Result<(), EraValidateError> {
assert_eq!(headers.len(), 8300);
assert_eq!(headers[0].block_number, 0);

let result = era_validate(headers.clone(), None, 0, None, Some(false))?;
let macc = MasterAccumulator::default();

let result = era_validate(headers.clone(), macc.clone(), 0, None, Some(false))?;
println!("result 1: {:?}", result);

assert!(result.contains(&0), "The vector does not contain 0");

// Test with creating a lockfile
let result = era_validate(headers.clone(), None, 0, None, Some(true))?;
let result = era_validate(headers.clone(), macc.clone(), 0, None, Some(true))?;
println!("result 2: {:?}", result);

assert!(result.contains(&0), "The vector does not contain 0");

// test with the lockfile created before.
let result = era_validate(headers.clone(), None, 0, None, Some(true))?;

let result = era_validate(headers.clone(), macc.clone(), 0, None, Some(true))?;

// already validated epochs are not included in the array.
assert_eq!(result.len(), 0);
Expand Down

0 comments on commit aeab1e3

Please sign in to comment.