Skip to content
Draft
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: 2 additions & 0 deletions sdk/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export {
VerifyingKey,
ViewKey,
initThreadPool,
initDevMode,
maybeInitDevMode,
getOrInitConsensusVersionTestHeights,
verifyFunctionExecution,
} from "./wasm.js";
Expand Down
51 changes: 51 additions & 0 deletions sdk/src/wasm.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
initDevMode as initDevModeWasm,
} from "@provablehq/wasm/%%NETWORK%%.js";

export {
Address,
Authorization,
Expand Down Expand Up @@ -51,3 +55,50 @@ export {
getOrInitConsensusVersionTestHeights,
verifyFunctionExecution,
} from "@provablehq/wasm/%%NETWORK%%.js";

/**
* Initialize development mode by setting consensus version heights to [0..ConsensusVersion::latest()].
*
* This function automatically sets up development consensus heights without requiring
* manual specification of the number of consensus versions. It should be called before
* initializing the thread pool when working with a local development network.
*
* @returns An array of block heights at which each consensus version applies.
*
* @example
* import { initDevMode, initThreadPool } from "@provablehq/sdk";
*
* // Initialize dev mode before the thread pool
* initDevMode();
* await initThreadPool();
*/
export function initDevMode(): number[] {
return initDevModeWasm();
}
Comment on lines +75 to +77

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra wrapper doesn't add much, the wasm method itself is sufficient and can be exported directly.


/**
* Check if the ALEO_NETWORK environment variable is set to "local" and if so,
* automatically call initDevMode() to set up development consensus heights.
*
* This function is useful for Node.js applications that want to automatically
* detect when running against a local development network.
*
* @returns An array of block heights if dev mode was initialized, undefined otherwise.
*
* @example
* import { maybeInitDevMode, initThreadPool } from "@provablehq/sdk";
*
* // Automatically initialize dev mode if ALEO_NETWORK=local
* maybeInitDevMode();
* await initThreadPool();
*/
export function maybeInitDevMode(): number[] | undefined {
try {
if (typeof process !== 'undefined' && process.env?.ALEO_NETWORK === 'local') {
return initDevModeWasm();
}
} catch {
// Ignore errors when process is not available (e.g., in browsers)
}
return undefined;
}
Comment on lines +95 to +104

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This process detection is inane. Node.ts already runs all node related code, so anything that is set by default can be set there.

28 changes: 28 additions & 0 deletions wasm/src/utilities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ pub fn get_or_init_consensus_version_heights(heights: Option<String>) -> js_sys:
pairs.iter().map(|(_, height)| wasm_bindgen::JsValue::from_f64(*height as f64)).collect::<js_sys::Array>()
}

/// Initialize development mode by setting consensus version heights to [0..ConsensusVersion::latest()].
///
/// This function automatically sets up development consensus heights without requiring
/// manual specification of the number of consensus versions. It should be called before
/// initializing the thread pool when working with a local development network.
///
/// @returns {Array} An array of block heights at which each consensus version applies.
///
/// @example
/// import { initDevMode, initThreadPool } from "@provablehq/sdk";
///
/// // Initialize dev mode before the thread pool
/// initDevMode();
/// await initThreadPool();
/// // Rest of the code...
#[wasm_bindgen::prelude::wasm_bindgen(js_name = initDevMode)]
pub fn init_dev_mode() -> js_sys::Array {
// Get the latest consensus version index
let latest = snarkvm_console::network::ConsensusVersion::latest();
let num_versions = latest as u32 + 1;

// Create a comma-separated string of heights from 0 to num_versions-1
let heights: String = (0..num_versions).map(|i| i.to_string()).collect::<Vec<_>>().join(",");

// Initialize the consensus version heights
get_or_init_consensus_version_heights(Some(heights))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down