diff --git a/.cargo/config.toml b/.cargo/config.toml index a8f7ca1f..ae6f11ec 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -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" diff --git a/.github/workflows/Basic.yml b/.github/workflows/Basic.yml index 0e866a3f..740a2e8c 100644 --- a/.github/workflows/Basic.yml +++ b/.github/workflows/Basic.yml @@ -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: diff --git a/.gitignore b/.gitignore index 21a3a582..67f8f401 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ # IDEs *.iml .idea + +# Generated test data +tally_data_request_fixture.json \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 82b21da0..410ed7a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -522,6 +522,12 @@ dependencies = [ "spki", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "primeorder" version = "0.13.6" @@ -572,6 +578,27 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + [[package]] name = "rand_core" version = "0.5.1" @@ -899,6 +926,11 @@ name = "xtask" version = "0.1.0" dependencies = [ "anyhow", + "hex", + "rand", + "seda-contract", + "semver", + "serde_json", "xshell", ] diff --git a/Cargo.toml b/Cargo.toml index 4f845aeb..6f904d9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" } diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index bbbeffef..a2c3cd1e 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -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 diff --git a/xtask/src/main.rs b/xtask/src/main.rs index b88d108b..33687793 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -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() { @@ -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 @@ -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()?, } @@ -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()?; @@ -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, +) -> (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 { + 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::(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(()) +}