Skip to content

Commit

Permalink
refactor: use util functions for reading from portal-spec-tests submo…
Browse files Browse the repository at this point in the history
…dule
  • Loading branch information
morph-dev committed Feb 8, 2025
1 parent a92ea33 commit 47a5a30
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 51 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions crates/ethportal-api/src/types/content_value/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,20 +548,21 @@ impl ContentValue for BeaconContentValue {

#[cfg(test)]
mod test {
use std::{fs, str::FromStr};
use std::str::FromStr;

use alloy::primitives::Bytes;
use serde::Deserialize;
use serde_yaml::Value;

use super::*;
use crate::test_utils::read_file_from_tests_submodule;

#[rstest::rstest]
#[case("capella", 6718368)]
#[case("deneb", 10248000)]
fn light_client_bootstrap_encode_decode(#[case] fork_name: &str, #[case] expected_slot: u64) {
let file = fs::read_to_string(format!(
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/bootstrap.yaml",
let file = read_file_from_tests_submodule(format!(
"tests/mainnet/beacon_chain/light_client/{fork_name}/bootstrap.yaml",
))
.unwrap();

Expand Down Expand Up @@ -589,8 +590,8 @@ mod test {
#[case] fork_name: &str,
#[case] expected_slot: u64,
) {
let file = fs::read_to_string(format!(
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/updates.yaml",
let file = read_file_from_tests_submodule(format!(
"tests/mainnet/beacon_chain/light_client/{fork_name}/updates.yaml",
))
.unwrap();

Expand Down Expand Up @@ -622,8 +623,8 @@ mod test {
#[case] fork_name: &str,
#[case] expected_slot: u64,
) {
let file = fs::read_to_string(format!(
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/optimistic_update.yaml",
let file = read_file_from_tests_submodule(format!(
"tests/mainnet/beacon_chain/light_client/{fork_name}/optimistic_update.yaml",
))
.unwrap();

Expand Down Expand Up @@ -655,8 +656,8 @@ mod test {
#[case] fork_name: &str,
#[case] expected_slot: u64,
) {
let file = fs::read_to_string(format!(
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/finality_update.yaml"
let file = read_file_from_tests_submodule(format!(
"tests/mainnet/beacon_chain/light_client/{fork_name}/finality_update.yaml"
))
.unwrap();

Expand All @@ -683,7 +684,9 @@ mod test {

#[test]
fn deneb_historical_summaries_with_proof_encode_decode() {
let file = fs::read_to_string("./../../portal-spec-tests/tests/mainnet/beacon_chain/historical_summaries_with_proof/deneb/historical_summaries_with_proof.yaml").unwrap();
let file = read_file_from_tests_submodule(
"tests/mainnet/beacon_chain/historical_summaries_with_proof/deneb/historical_summaries_with_proof.yaml",
).unwrap();
let value: serde_yaml::Value = serde_yaml::from_str(&file).unwrap();
let content_key = BeaconContentKey::deserialize(&value["content_key"]).unwrap();
let content_bytes = RawContentValue::deserialize(&value["content_value"]).unwrap();
Expand Down
28 changes: 23 additions & 5 deletions crates/utils/src/submodules.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
use std::{
fs::read_to_string,
io,
fs, io,
path::{Path, PathBuf},
};

pub const PORTAL_SPEC_TESTS_SUBMODULE_PATH: &str = "../../../portal-spec-tests";
pub const PORTAL_SPEC_TESTS_SUBMODULE_PATH: [&str; 2] =
["../../portal-spec-tests", "../../../portal-spec-tests"];

/// Reads a file from a "portal-spec-tests" submodule.
/// Returns a path to a file within "portal-spec-tests" submodule
pub fn portal_spec_tests_file_path<P: AsRef<Path>>(path: P) -> PathBuf {
for submodule_path in PORTAL_SPEC_TESTS_SUBMODULE_PATH {
if fs::exists(submodule_path)
.expect("we should be able to check whether submodule path exists")
{
return PathBuf::from(submodule_path).join(path);
}
}

panic!("Submodule directory not found!")
}

/// Reads text file from a "portal-spec-tests" submodule
pub fn read_portal_spec_tests_file<P: AsRef<Path>>(path: P) -> io::Result<String> {
read_to_string(PathBuf::from(PORTAL_SPEC_TESTS_SUBMODULE_PATH).join(path))
fs::read_to_string(portal_spec_tests_file_path(path))
}

/// Reads binary file from a "portal-spec-tests" submodule
pub fn read_portal_spec_tests_file_as_bytes<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
fs::read(portal_spec_tests_file_path(path))
}
2 changes: 1 addition & 1 deletion crates/validation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ quickcheck.workspace = true
quickcheck_macros = "1.0.0"
rstest.workspace = true
serde_yaml.workspace = true

trin-utils.workspace = true
28 changes: 21 additions & 7 deletions crates/validation/src/header_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn calculate_generalized_index(header: &Header) -> u64 {
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
use std::{fs, str::FromStr};
use std::{fs, path::PathBuf, str::FromStr};

use alloy::{
primitives::{Address, Bloom, B256, U256},
Expand All @@ -241,10 +241,15 @@ mod test {
use serde_json::Value;
use ssz::{Decode, Encode};
use tree_hash::TreeHash;
use trin_utils::submodules::{
read_portal_spec_tests_file, read_portal_spec_tests_file_as_bytes,
};

use super::*;
use crate::constants::DEFAULT_PRE_MERGE_ACC_HASH;

const SPEC_TESTS_DIR: &str = "tests/mainnet/history";

#[rstest]
#[case(1_000_001)]
#[case(1_000_002)]
Expand Down Expand Up @@ -385,7 +390,10 @@ mod test {
let header_validator = get_mainnet_header_validator();

// Read the historical roots block proof from a test file
let file = fs::read_to_string("./../../portal-spec-tests/tests/mainnet/history/headers_with_proof/block_proofs_bellatrix/beacon_block_proof-15539558-cdf9ed89b0c43cda17398dc4da9cfc505e5ccd19f7c39e3b43474180f1051e01.yaml").unwrap();
let file = read_portal_spec_tests_file(PathBuf::from(SPEC_TESTS_DIR).join(
"headers_with_proof/block_proofs_bellatrix/beacon_block_proof-15539558-cdf9ed89b0c43cda17398dc4da9cfc505e5ccd19f7c39e3b43474180f1051e01.yaml",
))
.unwrap();
let value: serde_yaml::Value = serde_yaml::from_str(&file).unwrap();
let block_number: u64 = 15539558;
let header_hash = value
Expand Down Expand Up @@ -430,7 +438,10 @@ mod test {
let header_validator = get_mainnet_header_validator();

// Read the historical roots block proof from a test file
let file = fs::read_to_string(format!("./../../portal-spec-tests/tests/mainnet/history/headers_with_proof/block_proofs_capella/beacon_block_proof-{block_number}.yaml")).unwrap();
let file = read_portal_spec_tests_file(PathBuf::from(SPEC_TESTS_DIR).join(format!(
"headers_with_proof/block_proofs_capella/beacon_block_proof-{block_number}.yaml"
)))
.unwrap();
let value: serde_yaml::Value = serde_yaml::from_str(&file).unwrap();
let header_hash = value
.get("execution_block_header")
Expand All @@ -442,8 +453,11 @@ mod test {
serde_yaml::from_value(value).unwrap();

// Load historical summaries from ssz file
let historical_summaries_bytes = std::fs::read("./../../portal-spec-tests/tests/mainnet/history/headers_with_proof/block_proofs_capella/historical_summaries_at_slot_8953856.ssz"
).expect("cannot load HistoricalSummaries bytes from test file");
let historical_summaries_bytes =
read_portal_spec_tests_file_as_bytes(PathBuf::from(SPEC_TESTS_DIR).join(
"headers_with_proof/block_proofs_capella/historical_summaries_at_slot_8953856.ssz",
))
.expect("cannot load HistoricalSummaries bytes from test file");
let historical_summaries = HistoricalSummaries::from_ssz_bytes(&historical_summaries_bytes)
.expect("cannot decode HistoricalSummaries bytes");

Expand Down Expand Up @@ -512,8 +526,8 @@ mod test {
}

fn read_epoch_accumulator_122() -> EpochAccumulator {
let epoch_acc_bytes = fs::read(
"../../portal-spec-tests/tests/mainnet/history/accumulator/epoch-record-00122.ssz",
let epoch_acc_bytes = read_portal_spec_tests_file_as_bytes(
PathBuf::from(SPEC_TESTS_DIR).join("accumulator/epoch-record-00122.ssz"),
)
.unwrap();
EpochAccumulator::from_ssz_bytes(&epoch_acc_bytes).unwrap()
Expand Down
50 changes: 22 additions & 28 deletions testing/ethportal-peertest/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fmt::{self, Display, Formatter},
fs,
path::{Path, PathBuf},
};

use alloy::{primitives::Bytes, rlp::Decodable};
Expand All @@ -19,6 +20,7 @@ use serde::Deserializer;
use serde_yaml::Value;
use ssz::Decode;
use tracing::error;
use trin_utils::submodules::portal_spec_tests_file_path;
use ureq::serde::Deserialize;

pub async fn wait_for_successful_result<Fut, O>(f: impl Fn() -> Fut) -> O
Expand Down Expand Up @@ -93,10 +95,8 @@ pub async fn wait_for_state_content<P: StateNetworkApiClient + std::marker::Sync
.await
}

fn read_history_content_key_value(
file_name: &str,
) -> Result<(HistoryContentKey, HistoryContentValue)> {
let yaml_content = fs::read_to_string(file_name)?;
fn read_history_fixture_from_file(path: &Path) -> Result<(HistoryContentKey, HistoryContentValue)> {
let yaml_content = fs::read_to_string(path)?;

let value: Value = serde_yaml::from_str(&yaml_content)?;

Expand All @@ -108,29 +108,28 @@ fn read_history_content_key_value(
}

/// Wrapper function for fixtures that directly returns the tuple.
fn read_fixture(file_name: &str) -> (HistoryContentKey, HistoryContentValue) {
read_history_content_key_value(file_name)
.unwrap_or_else(|err| panic!("Error reading fixture in {file_name}: {err}"))
fn read_history_fixture(path: &str) -> (HistoryContentKey, HistoryContentValue) {
let path = portal_spec_tests_file_path(PathBuf::from("tests/mainnet/history").join(path));
read_history_fixture_from_file(&path)
.unwrap_or_else(|err| panic!("Error reading fixture in {path:?}: {err}"))
}

/// History HeaderWithProof content key & value
/// Block #1000010
pub fn fixture_header_by_hash_1000010() -> (HistoryContentKey, HistoryContentValue) {
read_fixture("../../portal-spec-tests/tests/mainnet/history/headers_with_proof/1000010.yaml")
read_history_fixture("headers_with_proof/1000010.yaml")
}

/// History HeaderByHash content key & value
/// Block #14764013 (pre-merge)
pub fn fixture_header_by_hash() -> (HistoryContentKey, HistoryContentValue) {
read_fixture("../../portal-spec-tests/tests/mainnet/history/headers_with_proof/14764013.yaml")
read_history_fixture("headers_with_proof/14764013.yaml")
}

/// History HeaderByNumber content key & value
/// Block #14764013 (pre-merge)
pub fn fixture_header_by_number() -> (HistoryContentKey, HistoryContentValue) {
let (_, content_value) = read_fixture(
"../../portal-spec-tests/tests/mainnet/history/headers_with_proof/14764013.yaml",
);
let (_, content_value) = read_history_fixture("headers_with_proof/14764013.yaml");

// Create a content key from the block number
let HistoryContentValue::BlockHeaderWithProof(header_with_proof) = content_value.clone() else {
Expand All @@ -145,13 +144,13 @@ pub fn fixture_header_by_number() -> (HistoryContentKey, HistoryContentValue) {
/// History BlockBody content key & value
/// Block #14764013 (pre-merge)
pub fn fixture_block_body() -> (HistoryContentKey, HistoryContentValue) {
read_fixture("../../portal-spec-tests/tests/mainnet/history/bodies/14764013.yaml")
read_history_fixture("bodies/14764013.yaml")
}

/// History Receipts content key & value
/// Block #14764013 (pre-merge)
pub fn fixture_receipts() -> (HistoryContentKey, HistoryContentValue) {
read_fixture("../../portal-spec-tests/tests/mainnet/history/receipts/14764013.yaml")
read_history_fixture("receipts/14764013.yaml")
}

enum DependentType {
Expand Down Expand Up @@ -283,33 +282,28 @@ where
.map_err(|err| serde::de::Error::custom(format!("Error decoding header: {err}")))
}

fn read_state_fixture_from_file(file_name: &str) -> Result<Vec<StateFixture>> {
let yaml_content = fs::read_to_string(file_name)?;
fn read_state_fixture_from_file(path: &Path) -> Result<Vec<StateFixture>> {
let yaml_content = fs::read_to_string(path)?;
let value: Value = serde_yaml::from_str(&yaml_content)?;

let result = Vec::<StateFixture>::deserialize(value)?;
Ok(result)
}

fn read_state_fixture(file_name: &str) -> Vec<StateFixture> {
read_state_fixture_from_file(file_name)
.unwrap_or_else(|err| panic!("Error reading fixture: {err}"))
fn read_state_fixture(path: &str) -> Vec<StateFixture> {
let path = portal_spec_tests_file_path(PathBuf::from("tests/mainnet/state").join(path));
read_state_fixture_from_file(&path)
.unwrap_or_else(|err| panic!("Error reading fixture in {path:?}: {err}"))
}

pub fn fixtures_state_account_trie_node() -> Vec<StateFixture> {
read_state_fixture(
"../../portal-spec-tests/tests/mainnet/state/validation/account_trie_node.yaml",
)
read_state_fixture("validation/account_trie_node.yaml")
}

pub fn fixtures_state_contract_storage_trie_node() -> Vec<StateFixture> {
read_state_fixture(
"../../portal-spec-tests/tests/mainnet/state/validation/contract_storage_trie_node.yaml",
)
read_state_fixture("validation/contract_storage_trie_node.yaml")
}

pub fn fixtures_state_contract_bytecode() -> Vec<StateFixture> {
read_state_fixture(
"../../portal-spec-tests/tests/mainnet/state/validation/contract_bytecode.yaml",
)
read_state_fixture("validation/contract_bytecode.yaml")
}

0 comments on commit 47a5a30

Please sign in to comment.