Skip to content

Commit

Permalink
refactor: use generics for consensus test specs testing to reduce cod…
Browse files Browse the repository at this point in the history
…e duplication
  • Loading branch information
KolbyML committed Jan 7, 2025
1 parent 4f80a2d commit 2f80cc2
Show file tree
Hide file tree
Showing 228 changed files with 572 additions and 346,621 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ jobs:
- run:
name: Test Trin workspace
command: cargo test --workspace -- --nocapture
- run:
name: Test Consensus spec tests
command: cd testing/ef-tests && make test
- save-sccache-cache
# 'cargo check' performs all the compilation without actually building the crate, so it is quicker for the same guarantee
check-workspace-crates:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
.python-version
.DS_Store
*rusty-tags.vi

# Ignore downloaded consensus specs test data
testing/ef-tests/mainnet*
21 changes: 21 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ members = [
"trin-metrics",
"portal-bridge",
"rpc",
"testing/ef-tests",
"trin-beacon",
"trin-evm",
"trin-execution",
Expand Down Expand Up @@ -100,6 +101,8 @@ discv5 = { version = "0.4.1", features = ["serde"] }
e2store = { path = "e2store" }
env_logger = "0.9.0"
eth_trie = "0.5.0"
ethereum_hashing = "0.7.0"
ethereum_serde_utils = "0.7.0"
ethereum_ssz = "0.7.1"
ethereum_ssz_derive = "0.7.1"
ethportal-api = { path = "ethportal-api" }
Expand Down
4 changes: 2 additions & 2 deletions ethportal-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ clap.workspace = true
const_format = {version = "0.2.0", features = ["rust_1_64"]}
discv5.workspace = true
eth_trie.workspace = true
ethereum_hashing = "0.7.0"
ethereum_serde_utils = "0.7.0"
ethereum_hashing.workspace = true
ethereum_serde_utils.workspace = true
ethereum_ssz.workspace = true
ethereum_ssz_derive.workspace = true
hex.workspace = true
Expand Down
179 changes: 0 additions & 179 deletions ethportal-api/src/types/consensus/beacon_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,182 +187,3 @@ impl SignedBeaconBlock {
}
}
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
use std::str::FromStr;

use ::ssz::Encode;
use rstest::rstest;
use serde_json::Value;

use super::*;
use crate::consensus::fork::ForkName;

#[rstest]
#[case("case_0")]
#[case("case_1")]
fn serde_signed_beacon_block_bellatrix(#[case] case: &str) {
let value = std::fs::read_to_string(format!(
"../test_assets/beacon/bellatrix/SignedBeaconBlock/ssz_random/{case}/value.yaml"
))
.expect("cannot find test asset");
let value: Value = serde_yaml::from_str(&value).unwrap();
let content: SignedBeaconBlockBellatrix = serde_json::from_value(value.clone()).unwrap();
let serialized = serde_json::to_value(content).unwrap();
assert_eq!(serialized, value);
}

#[rstest]
#[case("case_0")]
#[case("case_1")]
fn ssz_signed_beacon_block_bellatrix(#[case] case: &str) {
let value = std::fs::read_to_string(format!(
"../test_assets/beacon/bellatrix/SignedBeaconBlock/ssz_random/{case}/value.yaml"
))
.expect("cannot find test asset");
let value: Value = serde_yaml::from_str(&value).unwrap();
let content: SignedBeaconBlockBellatrix = serde_json::from_value(value).unwrap();

let compressed = std::fs::read(format!(
"../test_assets/beacon/bellatrix/SignedBeaconBlock/ssz_random/{case}/serialized.ssz_snappy"
))
.expect("cannot find test asset");
let mut decoder = snap::raw::Decoder::new();
let expected = decoder.decompress_vec(&compressed).unwrap();
SignedBeaconBlock::from_ssz_bytes(&expected, ForkName::Bellatrix).unwrap();
assert_eq!(content.as_ssz_bytes(), expected);
}

#[rstest]
#[case("case_0")]
#[case("case_1")]
fn serde_signed_beacon_block_capella(#[case] case: &str) {
let value = std::fs::read_to_string(format!(
"../test_assets/beacon/capella/SignedBeaconBlock/ssz_random/{case}/value.yaml"
))
.expect("cannot find test asset");
let value: Value = serde_yaml::from_str(&value).unwrap();
let content: SignedBeaconBlockCapella = serde_json::from_value(value.clone()).unwrap();
let serialized = serde_json::to_value(content).unwrap();
assert_eq!(serialized, value);
}

#[rstest]
#[case("case_0")]
#[case("case_1")]
fn ssz_signed_beacon_block_capella(#[case] case: &str) {
let value = std::fs::read_to_string(format!(
"../test_assets/beacon/capella/SignedBeaconBlock/ssz_random/{case}/value.yaml"
))
.expect("cannot find test asset");
let value: Value = serde_yaml::from_str(&value).unwrap();
let content: SignedBeaconBlockCapella = serde_json::from_value(value).unwrap();

let compressed = std::fs::read(format!(
"../test_assets/beacon/capella/SignedBeaconBlock/ssz_random/{case}/serialized.ssz_snappy"))
.expect("cannot find test asset");
let mut decoder = snap::raw::Decoder::new();
let expected = decoder.decompress_vec(&compressed).unwrap();
SignedBeaconBlock::from_ssz_bytes(&expected, ForkName::Capella).unwrap();
assert_eq!(content.as_ssz_bytes(), expected);
}

#[rstest]
#[case("case_0")]
#[case("case_1")]
fn serde_signed_beacon_block_deneb(#[case] case: &str) {
let value = std::fs::read_to_string(format!(
"../test_assets/beacon/deneb/SignedBeaconBlock/ssz_random/{case}/value.yaml"
))
.expect("cannot find test asset");
let value: Value = serde_yaml::from_str(&value).unwrap();
let content: SignedBeaconBlockDeneb = serde_json::from_value(value.clone()).unwrap();
let serialized = serde_json::to_value(content).unwrap();
assert_eq!(serialized, value);
}

#[rstest]
#[case("case_0")]
#[case("case_1")]
fn ssz_signed_beacon_block_deneb(#[case] case: &str) {
let value = std::fs::read_to_string(format!(
"../test_assets/beacon/deneb/SignedBeaconBlock/ssz_random/{case}/value.yaml"
))
.expect("cannot find test asset");
let value: Value = serde_yaml::from_str(&value).unwrap();
let content: SignedBeaconBlockDeneb = serde_json::from_value(value).unwrap();

let compressed = std::fs::read(format!(
"../test_assets/beacon/deneb/SignedBeaconBlock/ssz_random/{case}/serialized.ssz_snappy"
))
.expect("cannot find test asset");
let mut decoder = snap::raw::Decoder::new();
let expected = decoder.decompress_vec(&compressed).unwrap();
SignedBeaconBlock::from_ssz_bytes(&expected, ForkName::Deneb).unwrap();
assert_eq!(content.as_ssz_bytes(), expected);
}

#[rstest]
#[case("10232841")]
fn json_signed_beacon_block_deneb(#[case] case: &str) {
let value = std::fs::read_to_string(format!(
"../test_assets/beacon/deneb/SignedBeaconBlock/json/{case}.json"
))
.expect("cannot find test asset");
let value: Value = serde_json::from_str(&value).unwrap();
let _: SignedBeaconBlockDeneb = serde_json::from_value(value["data"].clone()).unwrap();
}

#[test]
fn serde_beacon_block_bellatrix() {
let value = std::fs::read_to_string(
"../test_assets/beacon/bellatrix/BeaconBlock/ssz_random/case_0/value.yaml",
)
.expect("cannot find test asset");
let value: Value = serde_yaml::from_str(&value).unwrap();
let content: BeaconBlockBellatrix = serde_json::from_value(value.clone()).unwrap();
let serialized = serde_json::to_value(content).unwrap();
assert_eq!(serialized, value);
}

#[test]
fn ssz_beacon_block_bellatrix() {
let value = std::fs::read_to_string(
"../test_assets/beacon/bellatrix/BeaconBlock/ssz_random/case_0/value.yaml",
)
.expect("cannot find test asset");
let value: Value = serde_yaml::from_str(&value).unwrap();
let content: BeaconBlockBellatrix = serde_json::from_value(value).unwrap();

let compressed = std::fs::read(
"../test_assets/beacon/bellatrix/BeaconBlock/ssz_random/case_0/serialized.ssz_snappy",
)
.expect("cannot find test asset");
let mut decoder = snap::raw::Decoder::new();
let expected = decoder.decompress_vec(&compressed).unwrap();
BeaconBlock::from_ssz_bytes(&expected, ForkName::Bellatrix).unwrap();
assert_eq!(content.as_ssz_bytes(), expected);
}

#[test]
fn beacon_block_body_root_proof() {
let value = std::fs::read_to_string(
"../test_assets/beacon/bellatrix/BeaconBlock/ssz_random/case_0/value.yaml",
)
.expect("cannot find test asset");
let value: Value = serde_yaml::from_str(&value).unwrap();
let content: BeaconBlockBellatrix = serde_json::from_value(value).unwrap();
let expected_proof = [
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0xf5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b",
"0x32b5f53d4b2729823f200eeb36bcf8a78fc1da2d60fef6df87a64a351fce46e7",
]
.map(|x| B256::from_str(x).unwrap());
let proof = content.build_body_root_proof();

assert_eq!(proof.len(), 3);
assert_eq!(proof, expected_proof.to_vec());
}
}
Loading

0 comments on commit 2f80cc2

Please sign in to comment.