-
Notifications
You must be signed in to change notification settings - Fork 28
test: add encoding spec tests for SSV types #835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shane-moore
wants to merge
9
commits into
sigp:unstable
Choose a base branch
from
shane-moore:feat/encoding-tests
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
682fb84
chor: add partial sig message encoding test
shane-moore e4bd0c3
chore: add signed ssv message encoding test
shane-moore b9deff3
chore: add ssv message encoding test
shane-moore f8515c9
chore: add agg committee encoding test
shane-moore 0a3c00c
chore: add proposer consensus data encoding test
shane-moore a4f509b
chore: skip share encoding test
shane-moore 70fda6f
refactor: move spec test run to new module
shane-moore 90c5244
refactor: make spec test encoding helpers
shane-moore e6b57e4
chore: add context to share test being skipped
shane-moore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
|
|
||
| // 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") | ||
| ); | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
anchor/spec_tests/src/types/aggregator_committee_consensus_data_encoding.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
31
anchor/spec_tests/src/types/partial_sig_message_encoding.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
31
anchor/spec_tests/src/types/proposer_consensus_data_encoding.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
share.EncodingTestis 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:
shareis the one intentional coverage exception in this suite, and surfacing it clearly makes CI output auditable and prevents future confusion about missing fixture coverage.There was a problem hiding this comment.
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