|
1 | 1 | use alloy_primitives::Bytes;
|
2 |
| -use anyhow::bail; |
3 |
| -use ethportal_api::{ContentValue, HistoryContentKey, HistoryContentValue}; |
| 2 | +use anyhow::{anyhow, bail, ensure}; |
| 3 | +use ethportal_api::{ |
| 4 | + types::{ |
| 5 | + content_key::beacon::HistoricalSummariesWithProofKey, |
| 6 | + content_value::beacon::ForkVersionedHistoricalSummariesWithProof, |
| 7 | + }, |
| 8 | + BeaconContentKey, BeaconContentValue, ContentValue, HistoryContentKey, HistoryContentValue, |
| 9 | +}; |
4 | 10 | use serde_yaml::Value;
|
5 |
| -use std::str::FromStr; |
| 11 | +use ssz::Decode; |
| 12 | +use std::{ |
| 13 | + fs::{self, File}, |
| 14 | + io::Read, |
| 15 | + path::PathBuf, |
| 16 | + str::FromStr, |
| 17 | + sync::{Arc, LazyLock}, |
| 18 | +}; |
6 | 19 |
|
7 | 20 | // trin-bridge constants
|
8 | 21 | pub const TRIN_BRIDGE_CLIENT_TYPE: &str = "trin-bridge";
|
9 | 22 |
|
10 |
| -pub const TEST_DATA_FILE_PATH: &str = "./portal-spec-tests/tests/mainnet/history/hive/success"; |
| 23 | +pub const BASE_TEST_PATH: &str = "./portal-spec-tests/tests/mainnet/history/hive"; |
| 24 | +const HISTORICAL_SUMMARIES_FILE_PREFIX: &str = "fork_digest_historical_summaries_"; |
| 25 | +const HISTORICAL_SUMMARIES_FILE_SUFFIX: &str = ".ssz_snappy"; |
11 | 26 |
|
12 |
| -pub fn get_test_data() -> anyhow::Result<Vec<(HistoryContentKey, HistoryContentValue)>> { |
| 27 | +/// The content key and value for the latest historical summaries |
| 28 | +/// |
| 29 | +/// This is a static variable that is initialized once and can be reused |
| 30 | +pub static LATEST_HISTORICAL_SUMMARIES: LazyLock<Arc<(BeaconContentKey, BeaconContentValue)>> = |
| 31 | + LazyLock::new(|| { |
| 32 | + let (content_key, content_value) = |
| 33 | + get_latest_historical_summaries().expect("Failed to load latest historical summaries"); |
| 34 | + Arc::new((content_key, content_value)) |
| 35 | + }); |
| 36 | + |
| 37 | +/// Get the latest historical summaries |
| 38 | +/// |
| 39 | +/// This should only be used in LATEST_HISTORICAL_SUMMARIES |
| 40 | +fn get_latest_historical_summaries() -> anyhow::Result<(BeaconContentKey, BeaconContentValue)> { |
| 41 | + let mut latest_epoch = 0; |
| 42 | + let mut latest_file: Option<PathBuf> = None; |
| 43 | + |
| 44 | + for entry in fs::read_dir(BASE_TEST_PATH)? { |
| 45 | + let entry = entry?; |
| 46 | + if entry.path().is_dir() { |
| 47 | + continue; |
| 48 | + } |
| 49 | + let file_name = entry.file_name(); |
| 50 | + let file_name_str = file_name.to_string_lossy(); |
| 51 | + |
| 52 | + if file_name_str.starts_with(HISTORICAL_SUMMARIES_FILE_PREFIX) |
| 53 | + && file_name_str.ends_with(HISTORICAL_SUMMARIES_FILE_SUFFIX) |
| 54 | + { |
| 55 | + let epoch_str = &file_name_str[HISTORICAL_SUMMARIES_FILE_PREFIX.len() |
| 56 | + ..file_name_str.len() - HISTORICAL_SUMMARIES_FILE_SUFFIX.len()]; |
| 57 | + if let Ok(epoch) = epoch_str.parse::<u64>() { |
| 58 | + if epoch > latest_epoch { |
| 59 | + latest_epoch = epoch; |
| 60 | + latest_file = Some(entry.path()); |
| 61 | + } |
| 62 | + } else { |
| 63 | + bail!("Unable to parse epoch from file name: {file_name_str}"); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + let Some(path) = latest_file else { |
| 69 | + bail!("No historical summaries files found in the directory"); |
| 70 | + }; |
| 71 | + |
| 72 | + let mut encoded = vec![]; |
| 73 | + let historical_summaries = File::open(path)?.read_to_end(&mut encoded)?; |
| 74 | + ensure!( |
| 75 | + historical_summaries > 0, |
| 76 | + "Historical summaries file is empty", |
| 77 | + ); |
| 78 | + |
| 79 | + let mut decoder = snap::read::FrameDecoder::new(&*encoded); |
| 80 | + let mut decoded: Vec<u8> = vec![]; |
| 81 | + decoder.read_to_end(&mut decoded)?; |
| 82 | + |
| 83 | + let historical_summaries_with_proof = |
| 84 | + ForkVersionedHistoricalSummariesWithProof::from_ssz_bytes(&decoded).map_err(|err| { |
| 85 | + anyhow!("Failed to decoded ForkVersionedHistoricalSummariesWithProof {err:?}") |
| 86 | + })?; |
| 87 | + |
| 88 | + let content_key = |
| 89 | + BeaconContentKey::HistoricalSummariesWithProof(HistoricalSummariesWithProofKey { |
| 90 | + epoch: historical_summaries_with_proof |
| 91 | + .historical_summaries_with_proof |
| 92 | + .epoch, |
| 93 | + }); |
| 94 | + let content_value = |
| 95 | + BeaconContentValue::HistoricalSummariesWithProof(historical_summaries_with_proof); |
| 96 | + |
| 97 | + Ok((content_key, content_value)) |
| 98 | +} |
| 99 | + |
| 100 | +pub fn get_success_test_data() -> anyhow::Result<Vec<(HistoryContentKey, HistoryContentValue)>> { |
| 101 | + get_test_data("success") |
| 102 | +} |
| 103 | + |
| 104 | +fn get_test_data(test_case: &str) -> anyhow::Result<Vec<(HistoryContentKey, HistoryContentValue)>> { |
13 | 105 | let mut content = vec![];
|
14 | 106 |
|
15 |
| - for entry in std::fs::read_dir(TEST_DATA_FILE_PATH)? { |
| 107 | + let success_test_path = format!("{BASE_TEST_PATH}/{test_case}"); |
| 108 | + for entry in std::fs::read_dir(success_test_path)? { |
16 | 109 | let entry = entry?;
|
17 | 110 | let test_path = entry.path();
|
18 | 111 | if !test_path.is_file() {
|
|
0 commit comments