Skip to content
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

Updating generated test vectors #448

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/src/fixture_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
mod examples;
mod generator;
mod parser;
mod reader;
pub mod reader;
pub mod writer;

pub(crate) use parser::Args;
Expand Down
4 changes: 4 additions & 0 deletions examples/src/fixture_generator/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub trait Reader {
/// Reads a delta object for a given epoch.
#[allow(dead_code)]
fn read_delta(&mut self, epoch: u32) -> Result<Delta, ReaderError>;

/// Reads a String (freeform).
#[allow(dead_code)]
fn read_string(&mut self) -> Result<String, ReaderError>;
}

#[derive(Debug, PartialEq, Eq)]
Expand Down
4 changes: 4 additions & 0 deletions examples/src/fixture_generator/reader/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,8 @@ impl Reader for YamlFileReader {
fn read_delta(&mut self, epoch: u32) -> Result<Delta, ReaderError> {
self.read_impl(|delta: &Delta| delta.epoch == epoch)
}

fn read_string(&mut self) -> Result<String, ReaderError> {
self.read_impl(|_: &String| true)
}
}
10 changes: 5 additions & 5 deletions examples/src/test_vectors/experimental.yaml

Large diffs are not rendered by default.

138 changes: 102 additions & 36 deletions examples/src/test_vectors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ pub struct Metadata {
pub domain_label: String,
}

#[derive(Debug, PartialEq, Eq)]
struct TestVectorBytes {
vrf_pk: String,
epoch_hash: String,
epoch: u64,
label: String,
lookup_proof: String,
history_proof_recent: String,
history_proof_complete: String,
history_proof_complete_len: usize,
}

#[derive(Parser, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct Args {
/// Name of output path.
Expand Down Expand Up @@ -97,6 +109,39 @@ pub(crate) async fn generate<TC: NamedConfiguration, L: DomainLabel>(args: &Args
writer.write_comment(&comment);
writer.write_object(metadata);

let test_vector_bytes = generate_impl::<TC>().await?;

writer.write_line();
writer.write_comment("Public Key");
writer.write_object(test_vector_bytes.vrf_pk);
writer.write_line();
writer.write_comment("Epoch Hash");
writer.write_object(test_vector_bytes.epoch_hash);
writer.write_line();
writer.write_comment("Epoch");
writer.write_object(test_vector_bytes.epoch);
writer.write_line();
writer.write_comment("Label");
writer.write_object(test_vector_bytes.label);
writer.write_line();
writer.write_comment("Lookup Proof");
writer.write_object(test_vector_bytes.lookup_proof);
writer.write_line();
writer.write_comment("History Proof (HistoryParams::MostRecent(1))");
writer.write_object(test_vector_bytes.history_proof_recent);
writer.write_line();
writer.write_comment(&format!(
"History Proof (HistoryParams::Complete with {} versions)",
test_vector_bytes.history_proof_complete_len
));
writer.write_object(test_vector_bytes.history_proof_complete);

// flush writer and exit
writer.flush();
Ok(())
}

async fn generate_impl<TC: NamedConfiguration>() -> Result<TestVectorBytes> {
let db = AsyncInMemoryDatabase::new();
let storage_manager = StorageManager::new_no_cache(db);
let vrf = HardCodedAkdVRF {};
Expand Down Expand Up @@ -206,44 +251,65 @@ pub(crate) async fn generate<TC: NamedConfiguration, L: DomainLabel>(args: &Args
}

if (i, epoch) == (label_to_write, epoch_to_write) {
writer.write_line();
writer.write_comment("Public Key");
writer.write_object(hex::encode(vrf_pk.as_bytes()));
writer.write_line();
writer.write_comment("Epoch Hash");
writer.write_object(hex::encode(epoch_hash.hash()));
writer.write_line();
writer.write_comment("Epoch");
writer.write_object(epoch_hash.epoch());
writer.write_line();
writer.write_comment("Label");
writer.write_object(hex::encode(&label.clone().0));
writer.write_line();
writer.write_comment("Lookup Proof");
writer.write_object(hex::encode(
akd_core::proto::specs::types::LookupProof::from(&lookup_proof)
.write_to_bytes()?,
));
writer.write_line();
writer.write_comment("History Proof (HistoryParams::MostRecent(1))");
writer.write_object(hex::encode(
akd_core::proto::specs::types::HistoryProof::from(&history_proof_partial)
.write_to_bytes()?,
));
writer.write_line();
writer.write_comment(&format!(
"History Proof (HistoryParams::Complete with {} versions)",
history_results_complete.len()
));
writer.write_object(hex::encode(
akd_core::proto::specs::types::HistoryProof::from(&history_proof_complete)
.write_to_bytes()?,
));
return Ok(TestVectorBytes {
vrf_pk: hex::encode(vrf_pk.as_bytes()),
epoch_hash: hex::encode(epoch_hash.hash()),
epoch: epoch_hash.epoch(),
label: hex::encode(&label.clone().0),
lookup_proof: hex::encode(
akd_core::proto::specs::types::LookupProof::from(&lookup_proof)
.write_to_bytes()?,
),
history_proof_recent: hex::encode(
akd_core::proto::specs::types::HistoryProof::from(&history_proof_partial)
.write_to_bytes()?,
),
history_proof_complete: hex::encode(
akd_core::proto::specs::types::HistoryProof::from(&history_proof_complete)
.write_to_bytes()?,
),
history_proof_complete_len: history_results_complete.len(),
});
}
}
}
panic!("Test vector not found");
}

// flush writer and exit
writer.flush();
Ok(())
#[cfg(test)]
mod tests {
use super::*;
use crate::{
fixture_generator::reader::{yaml::YamlFileReader, Reader},
test_config,
};

// Path to where the test vector .yaml files are located
const TEST_VECTORS_FILE_PATH: &str = "src/test_vectors";

// Ensures that the output of the test vectors matches the expected output
// If this test fails, then this means that the test vectors have changed and the
// expected output needs to be updated by regenerating the test vectors
test_config!(test_output_vectors);
async fn test_output_vectors<TC: NamedConfiguration>() {
let mut reader = YamlFileReader::new(
File::open(format!("{}/{}.yaml", TEST_VECTORS_FILE_PATH, TC::name())).unwrap(),
)
.unwrap();

let test_vector_bytes_generated = generate_impl::<TC>().await.unwrap();

let test_vector_bytes_read = TestVectorBytes {
vrf_pk: reader.read_string().unwrap(),
epoch_hash: reader.read_string().unwrap(),
epoch: reader.read_string().unwrap().parse().unwrap(),
label: reader.read_string().unwrap(),
lookup_proof: reader.read_string().unwrap(),
history_proof_recent: reader.read_string().unwrap(),
history_proof_complete: reader.read_string().unwrap(),
history_proof_complete_len: 3, // This is a bit of a hack and is hard-coded
};

assert_eq!(test_vector_bytes_read, test_vector_bytes_generated);
}
}
Loading
Loading