Skip to content

Commit 4323283

Browse files
authored
refactor: use generics for consensus test specs testing to reduce code duplication (#1621)
1 parent 5dd8514 commit 4323283

File tree

223 files changed

+553
-346630
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+553
-346630
lines changed

.circleci/config.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ jobs:
222222
- setup-and-restore-sccache-cache
223223
- run:
224224
name: Test Trin workspace
225-
command: cargo test --workspace -- --nocapture
225+
command: make test
226+
- run:
227+
name: Test Consensus spec tests
228+
command: make ef-tests
226229
- save-sccache-cache
227230
# 'cargo check' performs all the compilation without actually building the crate, so it is quicker for the same guarantee
228231
check-workspace-crates:
@@ -232,7 +235,7 @@ jobs:
232235
# parallelism level should be set to the amount of simulators we have or greater
233236
# The reason for this is the CI code currently only supports 1 input at a time
234237
# if we have a parallelism level of 5 and 6 sims one test runner will get 2 test sims and fail
235-
parallelism: 18
238+
parallelism: 19
236239
steps:
237240
- checkout
238241
- run:

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
.python-version
55
.DS_Store
66
*rusty-tags.vi
7+
8+
# Ignore downloaded consensus specs test data
9+
testing/ef-tests/mainnet*

Cargo.lock

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"trin-metrics",
99
"portal-bridge",
1010
"rpc",
11+
"testing/ef-tests",
1112
"trin-beacon",
1213
"trin-evm",
1314
"trin-execution",
@@ -46,6 +47,8 @@ directories = "3.0"
4647
discv5 = { version = "0.4.1", features = ["serde"] }
4748
env_logger = "0.9.0"
4849
eth_trie = "0.5.0"
50+
ethereum_hashing = "0.7.0"
51+
ethereum_serde_utils = "0.7.0"
4952
ethereum_ssz = "0.7.1"
5053
ethereum_ssz_derive = "0.7.1"
5154
futures = "0.3.23"

Makefile

+45-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ PROFILE ?= release
1616
CARGO_INSTALL_EXTRA_FLAGS ?=
1717

1818
.PHONY: lint
19-
lint: # Run clippy and rustfmt
19+
lint: ## Run clippy and rustfmt
2020
cargo +nightly fmt --all
2121
cargo clippy --all --all-targets --all-features --no-deps -- --deny warnings
2222

2323
.PHONY: lint-unstable
24-
lint-unstable: #run clippy, rustfmt and rustc lints with unstable features. expect errors, which cannot be resolved, so the user must step through and evaluate each one manually.
24+
lint-unstable: ## run clippy, rustfmt and rustc lints with unstable features. expect errors, which cannot be resolved, so the user must step through and evaluate each one manually.
2525
cargo +nightly fmt --all
2626
cargo clippy --all --all-targets --all-features --no-deps -- -Wclippy::cargo
2727
RUSTFLAGS="-W unused_crate_dependencies" cargo build
@@ -97,6 +97,49 @@ build-release-tarballs: ## Create a series of `.tar.gz` files in the BIN_DIR dir
9797
$(MAKE) build-x86_64-pc-windows-gnu
9898
$(call tarball_release_binary,"x86_64-pc-windows-gnu","trin.exe","")
9999

100+
##@ Tests
101+
102+
EF_TESTS_TARGET = mainnet.tar.gz
103+
EF_TESTS_DIR = ./testing/ef-tests/mainnet
104+
LATEST_RELEASE_URL = https://api.github.com/repos/ethereum/consensus-spec-tests/releases/latest
105+
106+
download_test_data:
107+
@if [ -d $(EF_TESTS_DIR) ]; then \
108+
echo "$(EF_TESTS_DIR) already downloaded. Skipping download."; \
109+
else \
110+
echo "Fetching the latest release URL for $(EF_TESTS_TARGET)..."; \
111+
curl -s $(LATEST_RELEASE_URL) \
112+
| grep "browser_download_url.*$(EF_TESTS_TARGET)" \
113+
| cut -d : -f 2,3 \
114+
| tr -d \" \
115+
| wget -qi -; \
116+
echo "$(EF_TESTS_TARGET) downloaded successfully."; \
117+
fi
118+
119+
extract_test_data: download_test_data
120+
@if [ -d $(EF_TESTS_DIR) ]; then \
121+
echo "$(EF_TESTS_DIR) already exists. Skipping extraction."; \
122+
else \
123+
echo "Extracting $(EF_TESTS_TARGET) into $(EF_TESTS_DIR)..."; \
124+
mkdir -p $(EF_TESTS_DIR); \
125+
tar -xzf $(EF_TESTS_TARGET) -C $(EF_TESTS_DIR); \
126+
rm -f $(EF_TESTS_TARGET); \
127+
echo "Extraction complete."; \
128+
fi
129+
130+
.PHONY: ef-tests
131+
ef-tests: extract_test_data ## Runs Ethereum Foundation tests.
132+
@echo "Running tests..."
133+
@cargo test -p ef-tests --features ef-tests
134+
@echo "Tests complete."
135+
136+
.PHONY: test
137+
test: ## Runs workspace tests.
138+
cargo test --workspace -- --nocapture
139+
140+
.PHONY: test-full
141+
test-full: test ef-tests ## Runs all tests.
142+
100143
##@ Other
101144

102145
.PHONY: clean

ethportal-api/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ c-kzg = "1.0.0"
2323
const_format = {version = "0.2.0", features = ["rust_1_64"]}
2424
discv5.workspace = true
2525
eth_trie.workspace = true
26-
ethereum_hashing = "0.7.0"
27-
ethereum_serde_utils = "0.7.0"
26+
ethereum_hashing.workspace = true
27+
ethereum_serde_utils.workspace = true
2828
ethereum_ssz.workspace = true
2929
ethereum_ssz_derive.workspace = true
3030
hex.workspace = true

ethportal-api/src/types/consensus/beacon_block.rs

-179
Original file line numberDiff line numberDiff line change
@@ -187,182 +187,3 @@ impl SignedBeaconBlock {
187187
}
188188
}
189189
}
190-
191-
#[cfg(test)]
192-
#[allow(clippy::unwrap_used)]
193-
mod test {
194-
use std::str::FromStr;
195-
196-
use ::ssz::Encode;
197-
use rstest::rstest;
198-
use serde_json::Value;
199-
200-
use super::*;
201-
use crate::consensus::fork::ForkName;
202-
203-
#[rstest]
204-
#[case("case_0")]
205-
#[case("case_1")]
206-
fn serde_signed_beacon_block_bellatrix(#[case] case: &str) {
207-
let value = std::fs::read_to_string(format!(
208-
"../test_assets/beacon/bellatrix/SignedBeaconBlock/ssz_random/{case}/value.yaml"
209-
))
210-
.expect("cannot find test asset");
211-
let value: Value = serde_yaml::from_str(&value).unwrap();
212-
let content: SignedBeaconBlockBellatrix = serde_json::from_value(value.clone()).unwrap();
213-
let serialized = serde_json::to_value(content).unwrap();
214-
assert_eq!(serialized, value);
215-
}
216-
217-
#[rstest]
218-
#[case("case_0")]
219-
#[case("case_1")]
220-
fn ssz_signed_beacon_block_bellatrix(#[case] case: &str) {
221-
let value = std::fs::read_to_string(format!(
222-
"../test_assets/beacon/bellatrix/SignedBeaconBlock/ssz_random/{case}/value.yaml"
223-
))
224-
.expect("cannot find test asset");
225-
let value: Value = serde_yaml::from_str(&value).unwrap();
226-
let content: SignedBeaconBlockBellatrix = serde_json::from_value(value).unwrap();
227-
228-
let compressed = std::fs::read(format!(
229-
"../test_assets/beacon/bellatrix/SignedBeaconBlock/ssz_random/{case}/serialized.ssz_snappy"
230-
))
231-
.expect("cannot find test asset");
232-
let mut decoder = snap::raw::Decoder::new();
233-
let expected = decoder.decompress_vec(&compressed).unwrap();
234-
SignedBeaconBlock::from_ssz_bytes(&expected, ForkName::Bellatrix).unwrap();
235-
assert_eq!(content.as_ssz_bytes(), expected);
236-
}
237-
238-
#[rstest]
239-
#[case("case_0")]
240-
#[case("case_1")]
241-
fn serde_signed_beacon_block_capella(#[case] case: &str) {
242-
let value = std::fs::read_to_string(format!(
243-
"../test_assets/beacon/capella/SignedBeaconBlock/ssz_random/{case}/value.yaml"
244-
))
245-
.expect("cannot find test asset");
246-
let value: Value = serde_yaml::from_str(&value).unwrap();
247-
let content: SignedBeaconBlockCapella = serde_json::from_value(value.clone()).unwrap();
248-
let serialized = serde_json::to_value(content).unwrap();
249-
assert_eq!(serialized, value);
250-
}
251-
252-
#[rstest]
253-
#[case("case_0")]
254-
#[case("case_1")]
255-
fn ssz_signed_beacon_block_capella(#[case] case: &str) {
256-
let value = std::fs::read_to_string(format!(
257-
"../test_assets/beacon/capella/SignedBeaconBlock/ssz_random/{case}/value.yaml"
258-
))
259-
.expect("cannot find test asset");
260-
let value: Value = serde_yaml::from_str(&value).unwrap();
261-
let content: SignedBeaconBlockCapella = serde_json::from_value(value).unwrap();
262-
263-
let compressed = std::fs::read(format!(
264-
"../test_assets/beacon/capella/SignedBeaconBlock/ssz_random/{case}/serialized.ssz_snappy"))
265-
.expect("cannot find test asset");
266-
let mut decoder = snap::raw::Decoder::new();
267-
let expected = decoder.decompress_vec(&compressed).unwrap();
268-
SignedBeaconBlock::from_ssz_bytes(&expected, ForkName::Capella).unwrap();
269-
assert_eq!(content.as_ssz_bytes(), expected);
270-
}
271-
272-
#[rstest]
273-
#[case("case_0")]
274-
#[case("case_1")]
275-
fn serde_signed_beacon_block_deneb(#[case] case: &str) {
276-
let value = std::fs::read_to_string(format!(
277-
"../test_assets/beacon/deneb/SignedBeaconBlock/ssz_random/{case}/value.yaml"
278-
))
279-
.expect("cannot find test asset");
280-
let value: Value = serde_yaml::from_str(&value).unwrap();
281-
let content: SignedBeaconBlockDeneb = serde_json::from_value(value.clone()).unwrap();
282-
let serialized = serde_json::to_value(content).unwrap();
283-
assert_eq!(serialized, value);
284-
}
285-
286-
#[rstest]
287-
#[case("case_0")]
288-
#[case("case_1")]
289-
fn ssz_signed_beacon_block_deneb(#[case] case: &str) {
290-
let value = std::fs::read_to_string(format!(
291-
"../test_assets/beacon/deneb/SignedBeaconBlock/ssz_random/{case}/value.yaml"
292-
))
293-
.expect("cannot find test asset");
294-
let value: Value = serde_yaml::from_str(&value).unwrap();
295-
let content: SignedBeaconBlockDeneb = serde_json::from_value(value).unwrap();
296-
297-
let compressed = std::fs::read(format!(
298-
"../test_assets/beacon/deneb/SignedBeaconBlock/ssz_random/{case}/serialized.ssz_snappy"
299-
))
300-
.expect("cannot find test asset");
301-
let mut decoder = snap::raw::Decoder::new();
302-
let expected = decoder.decompress_vec(&compressed).unwrap();
303-
SignedBeaconBlock::from_ssz_bytes(&expected, ForkName::Deneb).unwrap();
304-
assert_eq!(content.as_ssz_bytes(), expected);
305-
}
306-
307-
#[rstest]
308-
#[case("10232841")]
309-
fn json_signed_beacon_block_deneb(#[case] case: &str) {
310-
let value = std::fs::read_to_string(format!(
311-
"../test_assets/beacon/deneb/SignedBeaconBlock/json/{case}.json"
312-
))
313-
.expect("cannot find test asset");
314-
let value: Value = serde_json::from_str(&value).unwrap();
315-
let _: SignedBeaconBlockDeneb = serde_json::from_value(value["data"].clone()).unwrap();
316-
}
317-
318-
#[test]
319-
fn serde_beacon_block_bellatrix() {
320-
let value = std::fs::read_to_string(
321-
"../test_assets/beacon/bellatrix/BeaconBlock/ssz_random/case_0/value.yaml",
322-
)
323-
.expect("cannot find test asset");
324-
let value: Value = serde_yaml::from_str(&value).unwrap();
325-
let content: BeaconBlockBellatrix = serde_json::from_value(value.clone()).unwrap();
326-
let serialized = serde_json::to_value(content).unwrap();
327-
assert_eq!(serialized, value);
328-
}
329-
330-
#[test]
331-
fn ssz_beacon_block_bellatrix() {
332-
let value = std::fs::read_to_string(
333-
"../test_assets/beacon/bellatrix/BeaconBlock/ssz_random/case_0/value.yaml",
334-
)
335-
.expect("cannot find test asset");
336-
let value: Value = serde_yaml::from_str(&value).unwrap();
337-
let content: BeaconBlockBellatrix = serde_json::from_value(value).unwrap();
338-
339-
let compressed = std::fs::read(
340-
"../test_assets/beacon/bellatrix/BeaconBlock/ssz_random/case_0/serialized.ssz_snappy",
341-
)
342-
.expect("cannot find test asset");
343-
let mut decoder = snap::raw::Decoder::new();
344-
let expected = decoder.decompress_vec(&compressed).unwrap();
345-
BeaconBlock::from_ssz_bytes(&expected, ForkName::Bellatrix).unwrap();
346-
assert_eq!(content.as_ssz_bytes(), expected);
347-
}
348-
349-
#[test]
350-
fn beacon_block_body_root_proof() {
351-
let value = std::fs::read_to_string(
352-
"../test_assets/beacon/bellatrix/BeaconBlock/ssz_random/case_0/value.yaml",
353-
)
354-
.expect("cannot find test asset");
355-
let value: Value = serde_yaml::from_str(&value).unwrap();
356-
let content: BeaconBlockBellatrix = serde_json::from_value(value).unwrap();
357-
let expected_proof = [
358-
"0x0000000000000000000000000000000000000000000000000000000000000000",
359-
"0xf5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b",
360-
"0x32b5f53d4b2729823f200eeb36bcf8a78fc1da2d60fef6df87a64a351fce46e7",
361-
]
362-
.map(|x| B256::from_str(x).unwrap());
363-
let proof = content.build_body_root_proof();
364-
365-
assert_eq!(proof.len(), 3);
366-
assert_eq!(proof, expected_proof.to_vec());
367-
}
368-
}

0 commit comments

Comments
 (0)