Skip to content
72 changes: 3 additions & 69 deletions anchor/spec_tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#![cfg(test)]

mod runner;
mod types;
mod utils;

use std::{
fs,
path::{Path, PathBuf},
};
use std::path::Path;

use serde::de::DeserializeOwned;

Expand All @@ -23,76 +21,12 @@ fn run_test<T: SpecTest>(path: &Path, contents: &str) -> Result<(), String> {
test.run()
}

/// Run all type spec tests from the fixture directory.
/// Dispatches each JSON file to the correct test type based on exact prefix match.
fn run_types_tests() {
let dir: PathBuf =
Path::new(env!("CARGO_MANIFEST_DIR")).join("ssv-spec/types/spectest/generate/tests");
assert!(
dir.exists(),
"Fixture directory not found: {}",
dir.display()
);

let mut failures = Vec::new();
let mut count = 0;

for entry in fs::read_dir(&dir).expect("Failed to read fixture directory") {
let entry = entry.expect("Failed to read directory entry");
let path = entry.path();

if path.extension() != Some("json".as_ref()) {
continue;
}

let filename = path.file_name().unwrap().to_string_lossy().to_string();
let contents = fs::read_to_string(&path)
.unwrap_or_else(|e| panic!("Failed to read {}: {e}", path.display()));

// Extract exact type prefix
let prefix = filename.split('_').next().unwrap_or("");

let result = match prefix {
// Encoding tests
"beaconvote.EncodingTest" => {
run_test::<types::BeaconVoteEncodingTest>(&path, &contents)
}

// TODO(spec-tests): Add more test types here as they are implemented.
// This arm will be replaced with panic!() once all test types are added.
_ => {
eprintln!("SKIP (not yet implemented): {prefix}");
continue;
}
};

count += 1;
if let Err(e) = result {
failures.push(format!(" {filename}: {e}"));
}
}

assert!(
count > 0,
"No type spec test fixtures found in {}",
dir.display()
);

if !failures.is_empty() {
panic!(
"\n{} of {count} type spec tests failed:\n{}",
failures.len(),
failures.join("\n")
);
}
}

#[cfg(test)]
mod spec_tests {
use super::*;

#[test]
fn types_spec_tests() {
run_types_tests();
runner::run_all_type_fixtures();
}
}
3 changes: 3 additions & 0 deletions anchor/spec_tests/src/runner/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod types_dispatcher;

pub use types_dispatcher::run_all_type_fixtures;
117 changes: 117 additions & 0 deletions anchor/spec_tests/src/runner/types_dispatcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use std::{
fs,
path::{Path, PathBuf},
};

use crate::{run_test, types};

/// Dispatch a single fixture file to its test type based on the exact prefix before the first `_`.
///
/// Returns `Some(result)` if the prefix matched an implemented test type,
/// or `None` if the fixture was skipped (known-inapplicable or not-yet-implemented).
fn dispatch_fixture_by_prefix(
prefix: &str,
path: &Path,
contents: &str,
skipped_known_count: &mut usize,
skipped_unknown_count: &mut usize,
) -> Option<Result<(), String>> {
match prefix {
// Encoding tests
"aggregatorcommitteeconsensusdata.EncodingTest" => Some(run_test::<
types::AggregatorCommitteeConsensusDataEncodingTest,
>(path, contents)),
"beaconvote.EncodingTest" => {
Some(run_test::<types::BeaconVoteEncodingTest>(path, contents))
}
"partialsigmessage.EncodingTest" => Some(run_test::<types::PartialSigMessageEncodingTest>(
path, contents,
)),
"signedssvmsg.EncodingTest" => Some(run_test::<types::SignedSSVMessageEncodingTest>(
path, contents,
)),
"ssvmsg.EncodingTest" => Some(run_test::<types::SSVMessageEncodingTest>(path, contents)),
"proposerconsensusdata.EncodingTest" => Some(run_test::<
types::ProposerConsensusDataEncodingTest,
>(path, contents)),

// Anchor's `Share` is architecturally different from Go spec's `Share`
// (different fields, decomposed across multiple types). Not applicable.
"share.EncodingTest" => {
*skipped_known_count += 1;
None
}
Comment on lines 38 to 44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

share.EncodingTest is currently a known inapplicable case, but this branch skips it silently. Could we make this explicit in test output (e.g. eprintln!("SKIP (known-inapplicable): {prefix}")) and include skipped-known/skipped-unknown counts in the final summary?

Reason: share is the one intentional coverage exception in this suite, and surfacing it clearly makes CI output auditable and prevents future confusion about missing fixture coverage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel you on that, updated in e6b57e4


// TODO(spec-tests): Add more test types here as they are implemented.
// This arm will be replaced with panic!() once all test types are added.
_ => {
eprintln!("SKIP (not yet implemented): {prefix}");
*skipped_unknown_count += 1;
None
}
}
}

/// Run all type spec tests from the fixture directory.
///
/// Iterates over every `.json` fixture, dispatches each to the appropriate test type,
/// and reports failures at the end.
pub fn run_all_type_fixtures() {
let dir: PathBuf =
Path::new(env!("CARGO_MANIFEST_DIR")).join("ssv-spec/types/spectest/generate/tests");
assert!(
dir.exists(),
"Fixture directory not found: {}",
dir.display()
);

let mut failures = Vec::new();
let mut executed_count = 0;
let mut skipped_known_count = 0;
let mut skipped_unknown_count = 0;

for entry in fs::read_dir(&dir).expect("Failed to read fixture directory") {
let entry = entry.expect("Failed to read directory entry");
let path = entry.path();

if path.extension() != Some("json".as_ref()) {
continue;
}

let filename = path.file_name().unwrap().to_string_lossy().to_string();
let contents = fs::read_to_string(&path)
.unwrap_or_else(|e| panic!("Failed to read {}: {e}", path.display()));

// Extract exact type prefix
let prefix = filename.split('_').next().unwrap_or("");

let Some(result) = dispatch_fixture_by_prefix(
prefix,
&path,
&contents,
&mut skipped_known_count,
&mut skipped_unknown_count,
) else {
continue;
};

executed_count += 1;
if let Err(e) = result {
failures.push(format!(" {filename}: {e}"));
}
}

assert!(
executed_count > 0,
"No type spec test fixtures found in {}",
dir.display()
);

if !failures.is_empty() {
panic!(
"\n{} of {executed_count} type spec tests failed:\n{}",
failures.len(),
failures.join("\n")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use serde::Deserialize;
use types::Hash256;

use crate::{
SpecTest,
utils::{
check_roundtrip_with_root,
deserializers::{deserialize_base64, deserialize_bytes_to_hash256},
},
};

/// Mirrors Go's `EncodingTest` for `AggregatorCommitteeConsensusData`.
///
/// Validates SSZ encode/decode roundtrip and hash tree root.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct AggregatorCommitteeConsensusDataEncodingTest {
#[serde(deserialize_with = "deserialize_base64")]
data: Vec<u8>,
#[serde(deserialize_with = "deserialize_bytes_to_hash256")]
expected_root: Hash256,
}

impl SpecTest for AggregatorCommitteeConsensusDataEncodingTest {
fn run(&self) -> Result<(), String> {
check_roundtrip_with_root::<
ssv_types::consensus::AggregatorCommitteeConsensusData<types::MainnetEthSpec>,
>(&self.data, self.expected_root)
}
}
35 changes: 8 additions & 27 deletions anchor/spec_tests/src/types/beacon_vote_encoding.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use serde::Deserialize;
use ssz::{Decode, Encode};
use tree_hash::TreeHash;
use types::Hash256;

use crate::{
SpecTest,
utils::deserializers::{deserialize_base64, deserialize_bytes_to_hash256},
utils::{
check_roundtrip_with_root,
deserializers::{deserialize_base64, deserialize_bytes_to_hash256},
},
};

/// Mirrors Go's `EncodingTest` for `BeaconVote`.
Expand All @@ -22,29 +23,9 @@ pub struct BeaconVoteEncodingTest {

impl SpecTest for BeaconVoteEncodingTest {
fn run(&self) -> Result<(), String> {
// Decode BeaconVote from SSZ bytes
let decoded = ssv_types::consensus::BeaconVote::from_ssz_bytes(&self.data)
.map_err(|e| format!("SSZ decode failed: {e:?}"))?;

// Encode back and verify roundtrip
let encoded = decoded.as_ssz_bytes();
if encoded != self.data {
return Err(format!(
"SSZ roundtrip mismatch: encoded {} bytes, expected {} bytes",
encoded.len(),
self.data.len()
));
}

// Verify hash tree root
let root = decoded.tree_hash_root();
if root != self.expected_root {
return Err(format!(
"Hash tree root mismatch: got {root:?}, expected {:?}",
self.expected_root
));
}

Ok(())
check_roundtrip_with_root::<ssv_types::consensus::BeaconVote>(
&self.data,
self.expected_root,
)
}
}
11 changes: 11 additions & 0 deletions anchor/spec_tests/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
mod aggregator_committee_consensus_data_encoding;
mod beacon_vote_encoding;
mod partial_sig_message_encoding;
mod proposer_consensus_data_encoding;
mod signed_ssv_msg_encoding;
mod ssv_message_encoding;

pub use aggregator_committee_consensus_data_encoding::*;
pub use beacon_vote_encoding::*;
pub use partial_sig_message_encoding::*;
pub use proposer_consensus_data_encoding::*;
pub use signed_ssv_msg_encoding::*;
pub use ssv_message_encoding::*;
31 changes: 31 additions & 0 deletions anchor/spec_tests/src/types/partial_sig_message_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use serde::Deserialize;
use types::Hash256;

use crate::{
SpecTest,
utils::{
check_roundtrip_with_root,
deserializers::{deserialize_base64, deserialize_bytes_to_hash256},
},
};

/// Mirrors Go's `EncodingTest` for `PartialSignatureMessages`.
///
/// Validates SSZ encode/decode roundtrip and hash tree root.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct PartialSigMessageEncodingTest {
#[serde(deserialize_with = "deserialize_base64")]
data: Vec<u8>,
#[serde(deserialize_with = "deserialize_bytes_to_hash256")]
expected_root: Hash256,
}

impl SpecTest for PartialSigMessageEncodingTest {
fn run(&self) -> Result<(), String> {
check_roundtrip_with_root::<ssv_types::partial_sig::PartialSignatureMessages>(
&self.data,
self.expected_root,
)
}
}
31 changes: 31 additions & 0 deletions anchor/spec_tests/src/types/proposer_consensus_data_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use serde::Deserialize;
use types::Hash256;

use crate::{
SpecTest,
utils::{
check_roundtrip_with_root,
deserializers::{deserialize_base64, deserialize_bytes_to_hash256},
},
};

/// Mirrors Go's `EncodingTest` for `ProposerConsensusData`.
///
/// Validates SSZ encode/decode roundtrip and hash tree root.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ProposerConsensusDataEncodingTest {
#[serde(deserialize_with = "deserialize_base64")]
data: Vec<u8>,
#[serde(deserialize_with = "deserialize_bytes_to_hash256")]
expected_root: Hash256,
}

impl SpecTest for ProposerConsensusDataEncodingTest {
fn run(&self) -> Result<(), String> {
check_roundtrip_with_root::<ssv_types::consensus::ProposerConsensusData>(
&self.data,
self.expected_root,
)
}
}
22 changes: 22 additions & 0 deletions anchor/spec_tests/src/types/signed_ssv_msg_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use serde::Deserialize;

use crate::{
SpecTest,
utils::{check_roundtrip, deserializers::deserialize_base64},
};

/// Mirrors Go's `EncodingTest` for `SignedSSVMessage`.
///
/// Validates SSZ encode/decode roundtrip (no tree hash root check).
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SignedSSVMessageEncodingTest {
#[serde(deserialize_with = "deserialize_base64")]
data: Vec<u8>,
}

impl SpecTest for SignedSSVMessageEncodingTest {
fn run(&self) -> Result<(), String> {
check_roundtrip::<ssv_types::message::SignedSSVMessage>(&self.data)
}
}
Loading
Loading