Skip to content

Commit

Permalink
chore: add a way to generate tally test fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax committed Jun 4, 2024
1 parent 56a54e8 commit b8fc32d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 5 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ unit-test = "test --lib"
wasm = "run --package xtask -- wasm"
wasm-opt = "run --package xtask -- wasm-opt"
xtask = "run --package xtask --"
tally-data-req-fixture = "run --package xtask -- tally-data-req-fixture"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
# IDEs
*.iml
.idea

# Generated test data
tally_data_request_fixture.json
32 changes: 32 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ cw-utils = "1.0.3"
hex = "0.4.3"
k256 = { version = "0.13", default-features = false, features = ["ecdsa"] }
libfuzzer-sys = "0.4"
proxy-contract = { path = "./packages/proxy" }
rand = "0.8"
schemars = { version = "0.8", features = ["semver"] }
data-requests = { path = "./packages/data-requests" }
staking = { path = "./packages/staking" }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde-big-array = { version = "0.5" }
serde_json = "1.0"
Expand All @@ -41,3 +39,5 @@ thiserror = { version = "1.0" }
semver = { version = "1.0", features = ["serde"] }
vrf-rs = "0.0.0"
xshell = "0.2"

seda-contract = { path = "./contract" }
5 changes: 5 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ edition = "2021"

[dependencies]
anyhow.workspace = true
hex.workspace = true
seda-contract.workspace = true
semver.workspace = true
serde_json.workspace = true
rand.workspace = true
xshell.workspace = true
79 changes: 77 additions & 2 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{env, process::Command};
use std::{collections::HashMap, env, process::Command};

use anyhow::{bail, Context, Result};
use rand::Rng;
use seda_contract::msgs::data_requests::{DataRequest, DR};
use serde_json::json;
use xshell::{cmd, Shell};

fn main() {
Expand All @@ -10,7 +13,7 @@ fn main() {
}
}

const TASKS: &[&str] = &["help", "wasm"];
const TASKS: &[&str] = &["help", "wasm", "wasm-opt", "tally-data-req-fixture"];

fn try_main() -> Result<()> {
// Ensure our working directory is the toplevel
Expand All @@ -32,6 +35,7 @@ fn try_main() -> Result<()> {
Some("help") => print_help()?,
Some("wasm") => wasm(&sh)?,
Some("wasm-opt") => wasm_opt(&sh)?,
Some("tally-data-req-fixture") => data_req_fixture(&sh)?,
_ => print_help()?,
}

Expand Down Expand Up @@ -64,3 +68,74 @@ fn wasm_opt(sh: &Shell) -> Result<()> {
).run()?;
Ok(())
}

fn create_data_request(
dr_binary_id: [u8; 32],
tally_binary_id: [u8; 32],
replication_factor: u16,
tally_inputs: Vec<u8>,
) -> (String, DR) {
let id = rand::random();
let dr = DataRequest {
version: semver::Version {
major: 1,
minor: 0,
patch: 0,
pre: semver::Prerelease::EMPTY,
build: semver::BuildMetadata::EMPTY,
},
id,
dr_binary_id,
tally_binary_id,
dr_inputs: Default::default(),
tally_inputs,
memo: Default::default(),
replication_factor,
gas_price: 10u128.into(),
gas_limit: 20u128.into(),
seda_payload: Default::default(),
commits: Default::default(),
reveals: Default::default(),
payback_address: Default::default(),
};

(hex::encode(id), DR::Request(Box::new(dr)))
}

fn tally_test_fixture() -> HashMap<String, DR> {
let dr_binary_id: [u8; 32] = rand::random();
let tally_binary_id: [u8; 32] = rand::random();
let replication_factor = rand::thread_rng().gen_range(1..=3);

(0..replication_factor)
.map(|_| {
let inputs = [rand::thread_rng().gen_range::<u8, _>(1..=10); 5]
.into_iter()
.flat_map(|i| i.to_be_bytes())
.collect();

create_data_request(dr_binary_id, tally_binary_id, replication_factor, inputs)
})
.collect()
}

fn data_req_fixture(_sh: &Shell) -> Result<()> {
let file = std::fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open("tally_data_request_fixture.json")?;

let mut test_two_dr_ready_to_tally_data = tally_test_fixture();
test_two_dr_ready_to_tally_data.extend(tally_test_fixture());

serde_json::to_writer(
file,
&json!({
"test_one_dr_ready_to_tally": tally_test_fixture(),
"test_two_dr_ready_to_tally": test_two_dr_ready_to_tally_data,
}),
)?;

Ok(())
}

0 comments on commit b8fc32d

Please sign in to comment.