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

chore: Gen Tally Test Fixtures #167

Merged
merged 2 commits into from
Jun 5, 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
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"
4 changes: 1 addition & 3 deletions .github/workflows/Basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ jobs:
run: git diff --exit-code schema

- name: 🦾 Compile WASM contract
run: cargo wasm --locked
env:
RUSTFLAGS: "-C link-arg=-s"
run: cargo wasm


fmt:
Expand Down
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.1" }
serde_json = "1.0.117"
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
81 changes: 78 additions & 3 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 All @@ -49,7 +53,7 @@ fn print_help() -> Result<()> {
fn wasm(sh: &Shell) -> Result<()> {
cmd!(
sh,
"cargo build --release --lib --target wasm32-unknown-unknown --locked"
"cargo build -p seda-contract --release --lib --target wasm32-unknown-unknown --locked"
)
.env("RUSTFLAGS", "-C link-arg=-s")
.run()?;
Expand All @@ -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(())
}