Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
chore: enforce type checks on logging and build info (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Jun 20, 2023
1 parent 9263984 commit 325a423
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 72 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ wasm-logger = "0.2.0"
console_error_panic_hook = "0.1.7"
gloo-utils = { version = "0.1", features = ["serde"] }
js-sys = "0.3.62"
const-str = "0.5.5"

# Barretenberg WASM dependencies
thiserror = "1.0.21"
Expand Down
46 changes: 46 additions & 0 deletions src/build_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use gloo_utils::format::JsValueSerdeExt;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

#[wasm_bindgen(typescript_custom_section)]
const LOG_LEVEL: &'static str = r#"
/**
* @typedef {Object} BuildInfo - Information about how the installed package was built
* @property {string} gitHash - The hash of the git commit from which the package was built.
* @property {string} version - The version of the package at the built git commit.
* @property {boolean} dirty - Whether the package contained uncommitted changes when built.
*/
export type BuildInfo = {
gitHash: string;
version: string;
dirty: string;
}
"#;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "BuildInfo")]
pub type JsBuildInfo;
}

#[derive(Serialize, Deserialize)]
struct BuildInfo {
#[serde(rename = "gitHash")]
git_hash: &'static str,
version: &'static str,
dirty: bool,
}

const BUILD_INFO: BuildInfo = BuildInfo {
git_hash: env!("GIT_COMMIT"),
version: env!("CARGO_PKG_VERSION"),
dirty: const_str::equal!(env!("GIT_DIRTY"), "true"),
};

/// Returns the `BuildInfo` object containing information about how the installed package was built.
/// @returns {BuildInfo} - Information on how the installed package was built.
#[wasm_bindgen(js_name = buildInfo, skip_jsdoc)]
pub fn build_info() -> JsBuildInfo {
console_error_panic_hook::set_once();
<JsValue as JsValueSerdeExt>::from_serde(&BUILD_INFO).unwrap().into()
}
2 changes: 1 addition & 1 deletion src/foreign_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use acvm::FieldElement;
use js_sys::JsString;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};

use crate::js_transforms::js_value_to_field_element;
use crate::js_witness_map::js_value_to_field_element;

#[wasm_bindgen(typescript_custom_section)]
const FOREIGN_CALL_HANDLER: &'static str = r#"
Expand Down
28 changes: 25 additions & 3 deletions src/js_transforms.rs → src/js_witness_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,32 @@ use acvm::{
acir::native_types::{Witness, WitnessMap},
FieldElement,
};
use js_sys::JsString;
use wasm_bindgen::JsValue;
use js_sys::{JsString, Map};
use wasm_bindgen::prelude::{wasm_bindgen, JsValue};

use crate::JsWitnessMap;
#[wasm_bindgen(typescript_custom_section)]
const WITNESS_MAP: &'static str = r#"
// Map from witness index to hex string value of witness.
export type WitnessMap = Map<number, string>;
"#;

// WitnessMap
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Map, js_name = "WitnessMap", typescript_type = "WitnessMap")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub type JsWitnessMap;

#[wasm_bindgen(constructor, js_class = "Map")]
pub fn new() -> JsWitnessMap;

}

impl Default for JsWitnessMap {
fn default() -> Self {
Self::new()
}
}

impl From<WitnessMap> for JsWitnessMap {
fn from(witness_map: WitnessMap) -> Self {
Expand Down
69 changes: 6 additions & 63 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,77 +1,20 @@
#![warn(unused_crate_dependencies, unused_extern_crates)]
#![warn(unreachable_pub)]

use gloo_utils::format::JsValueSerdeExt;
use js_sys::Map;
use log::Level;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use wasm_bindgen::prelude::*;

mod abi;
mod barretenberg;
mod build_info;
mod compression;
mod execute;
mod foreign_calls;
mod js_transforms;
mod js_witness_map;
mod logging;
mod public_witness;

pub use abi::{abi_decode, abi_encode};
pub use build_info::build_info;
pub use compression::{compress_witness, decompress_witness};
pub use execute::execute_circuit;
pub use js_witness_map::JsWitnessMap;
pub use logging::{init_log_level, LogLevel};
pub use public_witness::{get_public_parameters_witness, get_public_witness, get_return_witness};

#[derive(Serialize, Deserialize)]
pub struct BuildInfo {
git_hash: &'static str,
version: &'static str,
dirty: &'static str,
}

#[wasm_bindgen]
pub fn init_log_level(level: String) {
// Set the static variable from Rust
use std::sync::Once;

let log_level = Level::from_str(&level).unwrap_or(Level::Error);
static SET_HOOK: Once = Once::new();
SET_HOOK.call_once(|| {
wasm_logger::init(wasm_logger::Config::new(log_level));
});
}

const BUILD_INFO: BuildInfo = BuildInfo {
git_hash: env!("GIT_COMMIT"),
version: env!("CARGO_PKG_VERSION"),
dirty: env!("GIT_DIRTY"),
};

#[wasm_bindgen(js_name = buildInfo)]
pub fn build_info() -> JsValue {
console_error_panic_hook::set_once();
<JsValue as JsValueSerdeExt>::from_serde(&BUILD_INFO).unwrap()
}

#[wasm_bindgen(typescript_custom_section)]
const WITNESS_MAP: &'static str = r#"
// Map from witness index to hex string value of witness.
export type WitnessMap = Map<number, string>;
"#;

// WitnessMap
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Map, js_name = "WitnessMap", typescript_type = "WitnessMap")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub type JsWitnessMap;

#[wasm_bindgen(constructor, js_class = "Map")]
pub fn new() -> JsWitnessMap;

}

impl Default for JsWitnessMap {
fn default() -> Self {
Self::new()
}
}
28 changes: 28 additions & 0 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use js_sys::JsString;
use log::Level;
use std::str::FromStr;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(typescript_custom_section)]
const LOG_LEVEL: &'static str = r#"
export type LogLevel = "OFF" | "ERROR" | "WARN" | "INFO" | "DEBUG" | "TRACE";
"#;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = JsString, typescript_type = "LogLevel")]
pub type LogLevel;
}

#[wasm_bindgen(js_name = initLogLevel)]
pub fn init_log_level(level: LogLevel) {
// Set the static variable from Rust
use std::sync::Once;

let log_level = level.as_string().unwrap();
let log_level = Level::from_str(&log_level).unwrap_or(Level::Error);
static SET_HOOK: Once = Once::new();
SET_HOOK.call_once(|| {
wasm_logger::init(wasm_logger::Config::new(log_level));
});
}
4 changes: 2 additions & 2 deletions test/browser/execute_circuit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import initACVMSimulator, {
abiDecode,
executeCircuit,
WitnessMap,
init_log_level,
initLogLevel,
} from "../../result/";

beforeEach(async () => {
await initACVMSimulator();

init_log_level("INFO");
initLogLevel("INFO");
});

it("successfully executes circuit and extracts return value", async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/node/build_info.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect } from "chai";
import { buildInfo } from "../../result/";
import { BuildInfo, buildInfo } from "../../result/";
import child_process from "child_process";
import pkg from "../../package.json";

it("returns the correct build into", () => {
const info = buildInfo();
const info: BuildInfo = buildInfo();

// TODO: enforce that `package.json` and `Cargo.toml` are consistent.
expect(info.version).to.be.eq(pkg.version);
Expand All @@ -13,5 +13,5 @@ it("returns the correct build into", () => {
.execSync("git rev-parse HEAD")
.toString()
.trim();
expect(info.git_hash).to.be.eq(revision);
expect(info.gitHash).to.be.eq(revision);
});

0 comments on commit 325a423

Please sign in to comment.