wasm.rs

Overview

This file defines the WasmNodeCache struct and its associated methods to manage WebAssembly (Wasm) execution resources within a node environment. It focuses on initializing and caching Wasm engines and precompiled Wasm components, as well as maintaining a whitelist of allowed Wasm binary hashes. The caching mechanism improves performance by avoiding repeated compilation and ensures security by restricting execution to whitelisted Wasm binaries.

The file integrates tightly with the Wasm execution engine from the wasmtime crate and leverages external functions from the tvm_vm::executor::Engine to handle Wasm engine initialization and precompilation.


WasmNodeCache Struct

Description

WasmNodeCache holds cached resources necessary for efficient Wasm execution on the node. It encapsulates the Wasm engine instance, the root directory path for Wasm binaries, a whitelist of allowed Wasm binary hashes, and a cache mapping those hashes to their corresponding precompiled Wasm components.

Fields

Field Name

Type

Description

wasm_engine

wasmtime::Engine

The Wasm engine instance used for executing Wasm binaries.

wasm_binary_root_path

String

File system path to the root directory containing Wasm binaries.

wasm_hash_whitelist

HashSet<[u8; 32]>

Set of 32-byte hashes representing allowed Wasm binaries.

wasm_component_cache

HashMap<[u8; 32], wasmtime::component::Component>

Cache mapping Wasm binary hashes to their precompiled components.


Methods

new() -> anyhow::Result<Self>

Creates a new instance of WasmNodeCache.

Functionality

Usage Example

let cache = WasmNodeCache::new()?;

get_wasm_hash_whitelist() -> anyhow::Result<HashSet<[u8; 32]>>

Returns a set of whitelisted Wasm binary hashes. These hashes are hardcoded as hexadecimal strings and converted into 32-byte arrays.

Implementation Details

Usage Example

let whitelist = WasmNodeCache::get_wasm_hash_whitelist()?;

init_wasm_engine() -> anyhow::Result<wasmtime::Engine>

Initializes the Wasm engine.

Implementation Details

Usage Example

let engine = WasmNodeCache::init_wasm_engine()?;

init_wasm_engine_and_precompile_components(wasm_hash_whitelist: HashSet<[u8; 32]>, wasm_binary_root_path: String) -> anyhow::Result<(wasmtime::Engine, HashMap<[u8; 32], wasmtime::component::Component>)>

Initializes the Wasm engine and precompiles all Wasm components specified by the whitelist.

Parameters

Name

Type

Description

wasm_hash_whitelist

HashSet<[u8; 32]>

Set of allowed Wasm binary hashes to precompile.

wasm_binary_root_path

String

Filesystem path to the Wasm binaries root directory.

Returns

Implementation Details

Usage Example

let (engine, component_cache) = WasmNodeCache::init_wasm_engine_and_precompile_components(
    whitelist,
    "./config/wasm".to_string(),
)?;

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Structure of WasmNodeCache

classDiagram
class WasmNodeCache {
-wasm_engine: Engine
-wasm_binary_root_path: String
-wasm_hash_whitelist: HashSet
-wasm_component_cache: HashMap
+new()
+get_wasm_hash_whitelist()
+init_wasm_engine()
+init_wasm_engine_and_precompile_components()
}
WasmNodeCache ..> Engine : uses
WasmNodeCache ..> HashSet : uses
WasmNodeCache ..> HashMap : uses