Skip to content

Commit 1b9e01d

Browse files
authored
refactor: use util functions for reading from portal-spec-tests submodule (#1673)
1 parent 4a56db1 commit 1b9e01d

File tree

12 files changed

+123
-162
lines changed

12 files changed

+123
-162
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ethportal-api/src/assets/test/fluffy_header_with_proofs.json

Lines changed: 0 additions & 42 deletions
This file was deleted.

crates/ethportal-api/src/types/content_value/beacon.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -548,20 +548,21 @@ impl ContentValue for BeaconContentValue {
548548

549549
#[cfg(test)]
550550
mod test {
551-
use std::{fs, str::FromStr};
551+
use std::str::FromStr;
552552

553553
use alloy::primitives::Bytes;
554554
use serde::Deserialize;
555555
use serde_yaml::Value;
556556

557557
use super::*;
558+
use crate::test_utils::read_file_from_tests_submodule;
558559

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

@@ -589,8 +590,8 @@ mod test {
589590
#[case] fork_name: &str,
590591
#[case] expected_slot: u64,
591592
) {
592-
let file = fs::read_to_string(format!(
593-
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/updates.yaml",
593+
let file = read_file_from_tests_submodule(format!(
594+
"tests/mainnet/beacon_chain/light_client/{fork_name}/updates.yaml",
594595
))
595596
.unwrap();
596597

@@ -622,8 +623,8 @@ mod test {
622623
#[case] fork_name: &str,
623624
#[case] expected_slot: u64,
624625
) {
625-
let file = fs::read_to_string(format!(
626-
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/optimistic_update.yaml",
626+
let file = read_file_from_tests_submodule(format!(
627+
"tests/mainnet/beacon_chain/light_client/{fork_name}/optimistic_update.yaml",
627628
))
628629
.unwrap();
629630

@@ -655,8 +656,8 @@ mod test {
655656
#[case] fork_name: &str,
656657
#[case] expected_slot: u64,
657658
) {
658-
let file = fs::read_to_string(format!(
659-
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/finality_update.yaml"
659+
let file = read_file_from_tests_submodule(format!(
660+
"tests/mainnet/beacon_chain/light_client/{fork_name}/finality_update.yaml"
660661
))
661662
.unwrap();
662663

@@ -683,7 +684,9 @@ mod test {
683684

684685
#[test]
685686
fn deneb_historical_summaries_with_proof_encode_decode() {
686-
let file = fs::read_to_string("./../../portal-spec-tests/tests/mainnet/beacon_chain/historical_summaries_with_proof/deneb/historical_summaries_with_proof.yaml").unwrap();
687+
let file = read_file_from_tests_submodule(
688+
"tests/mainnet/beacon_chain/historical_summaries_with_proof/deneb/historical_summaries_with_proof.yaml",
689+
).unwrap();
687690
let value: serde_yaml::Value = serde_yaml::from_str(&file).unwrap();
688691
let content_key = BeaconContentKey::deserialize(&value["content_key"]).unwrap();
689692
let content_bytes = RawContentValue::deserialize(&value["content_value"]).unwrap();

crates/ethportal-api/src/types/content_value/history.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,24 @@ impl ContentValue for HistoryContentValue {
5757

5858
#[cfg(test)]
5959
mod test {
60-
use std::fs;
61-
6260
use serde_json::Value;
6361

6462
use super::*;
65-
use crate::{utils::bytes::hex_decode, HistoryContentValue};
63+
use crate::{
64+
test_utils::read_file_from_tests_submodule, utils::bytes::hex_decode, HistoryContentValue,
65+
};
6666

6767
#[test]
6868
fn header_with_proof_encode_decode_fluffy() {
69-
let file =
70-
fs::read_to_string("../validation/src/assets/fluffy/header_with_proofs.json").unwrap();
69+
let file = read_file_from_tests_submodule(
70+
"tests/mainnet/history/headers_with_proof/1000001-1000010.json",
71+
)
72+
.unwrap();
7173
let json: Value = serde_json::from_str(&file).unwrap();
7274
let json = json.as_object().unwrap();
7375
for (block_num, obj) in json {
7476
let block_num: u64 = block_num.parse().unwrap();
75-
let header_with_proof = obj.get("value").unwrap().as_str().unwrap();
77+
let header_with_proof = obj.get("content_value").unwrap().as_str().unwrap();
7678
let header_with_proof_encoded = hex_decode(header_with_proof).unwrap();
7779
let header_with_proof =
7880
HeaderWithProof::from_ssz_bytes(&header_with_proof_encoded).unwrap();

crates/ethportal-api/src/types/execution/header_with_proof.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,24 +184,28 @@ impl ssz::Encode for SszNone {
184184
#[cfg(test)]
185185
#[allow(clippy::unwrap_used)]
186186
mod tests {
187-
use std::fs;
188187

189188
use serde_json::Value;
190189
use ssz::Decode;
191190

192191
use super::*;
193-
use crate::utils::bytes::{hex_decode, hex_encode};
192+
use crate::{
193+
test_utils::read_file_from_tests_submodule,
194+
utils::bytes::{hex_decode, hex_encode},
195+
};
194196

195197
#[test_log::test]
196198
fn decode_encode_header_with_proofs() {
197-
let file =
198-
fs::read_to_string("../validation/src/assets/fluffy/header_with_proofs.json").unwrap();
199+
let file = read_file_from_tests_submodule(
200+
"tests/mainnet/history/headers_with_proof/1000001-1000010.json",
201+
)
202+
.unwrap();
199203
let json: Value = serde_json::from_str(&file).unwrap();
200204
let hwps = json.as_object().unwrap();
201205
for (block_number, obj) in hwps {
202206
let _content_key = obj.get("content_key").unwrap();
203207
let block_number: u64 = block_number.parse().unwrap();
204-
let proof = obj.get("value").unwrap().as_str().unwrap();
208+
let proof = obj.get("content_value").unwrap().as_str().unwrap();
205209
let hwp = HeaderWithProof::from_ssz_bytes(&hex_decode(proof).unwrap()).unwrap();
206210
assert_eq!(block_number, hwp.header.number);
207211
let encoded = hex_encode(hwp.as_ssz_bytes());

crates/subnetworks/history/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ ssz_types.workspace = true
3737
test-log.workspace = true
3838
tokio-test.workspace = true
3939
tracing-subscriber.workspace = true
40+
trin-utils.workspace = true
4041
ureq.workspace = true

crates/subnetworks/history/src/validation.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,23 @@ impl Validator<HistoryContentKey> for ChainHistoryValidator {
122122
#[cfg(test)]
123123
#[allow(clippy::unwrap_used)]
124124
mod tests {
125-
use std::fs;
126-
127125
use alloy::primitives::U256;
128126
use ethportal_api::utils::bytes::hex_decode;
129127
use serde_json::Value;
130128
use ssz::Encode;
129+
use trin_utils::submodules::read_portal_spec_tests_file;
131130

132131
use super::*;
133132

134133
fn get_header_with_proof_ssz() -> Vec<u8> {
135-
let file = fs::read_to_string("../../validation/src/assets/fluffy/header_with_proofs.json")
136-
.unwrap();
134+
let file = read_portal_spec_tests_file(
135+
"tests/mainnet/history/headers_with_proof/1000001-1000010.json",
136+
)
137+
.unwrap();
137138
let json: Value = serde_json::from_str(&file).unwrap();
138139
let json = json.as_object().unwrap();
139140
let raw_header = json.get("1000001").unwrap().as_object().unwrap();
140-
let raw_header = raw_header.get("value").unwrap().as_str().unwrap();
141+
let raw_header = raw_header.get("content_value").unwrap().as_str().unwrap();
141142
hex_decode(raw_header).unwrap()
142143
}
143144

crates/utils/src/submodules.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
use std::{
2-
fs::read_to_string,
3-
io,
2+
fs, io,
43
path::{Path, PathBuf},
54
};
65

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

9-
/// Reads a file from a "portal-spec-tests" submodule.
9+
/// Returns a path to a file within "portal-spec-tests" submodule
10+
pub fn portal_spec_tests_file_path<P: AsRef<Path>>(path: P) -> PathBuf {
11+
for submodule_path in PORTAL_SPEC_TESTS_SUBMODULE_PATH {
12+
if fs::exists(submodule_path)
13+
.expect("we should be able to check whether submodule path exists")
14+
{
15+
return PathBuf::from(submodule_path).join(path);
16+
}
17+
}
18+
19+
panic!("Submodule directory not found!")
20+
}
21+
22+
/// Reads text file from a "portal-spec-tests" submodule
1023
pub fn read_portal_spec_tests_file<P: AsRef<Path>>(path: P) -> io::Result<String> {
11-
read_to_string(PathBuf::from(PORTAL_SPEC_TESTS_SUBMODULE_PATH).join(path))
24+
fs::read_to_string(portal_spec_tests_file_path(path))
25+
}
26+
27+
/// Reads binary file from a "portal-spec-tests" submodule
28+
pub fn read_portal_spec_tests_file_as_bytes<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
29+
fs::read(portal_spec_tests_file_path(path))
1230
}

crates/validation/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ quickcheck.workspace = true
3333
quickcheck_macros = "1.0.0"
3434
rstest.workspace = true
3535
serde_yaml.workspace = true
36-
36+
trin-utils.workspace = true

0 commit comments

Comments
 (0)