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 |
|---|---|---|
|
| The Wasm engine instance used for executing Wasm binaries. |
|
| File system path to the root directory containing Wasm binaries. |
|
| Set of 32-byte hashes representing allowed Wasm binaries. |
|
| Cache mapping Wasm binary hashes to their precompiled components. |
Methods
new() -> anyhow::Result<Self>
Creates a new instance of WasmNodeCache.
Functionality
Sets the Wasm binary root directory path to
"./config/wasm".Retrieves the Wasm hash whitelist by invoking
get_wasm_hash_whitelist(). If retrieval fails, it returns an error wrapped inanyhow::Result.Initializes the Wasm engine and precompiles all Wasm components from the whitelist by calling
init_wasm_engine_and_precompile_components().Returns a fully initialized
WasmNodeCacheor an error if any step fails.
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
Converts each hex string in the predefined list into a byte array of length 32.
Inserts each byte array into a
HashSet.If any hash string is invalid (wrong length or parsing error), returns an error.
The current implementation uses a hardcoded whitelist but leaves commented code for loading from a configuration file (
wasm.conf).
Usage Example
let whitelist = WasmNodeCache::get_wasm_hash_whitelist()?;
init_wasm_engine() -> anyhow::Result<wasmtime::Engine>
Initializes the Wasm engine.
Implementation Details
Calls an external function
tvm_vm::executor::Engine::extern_wasm_engine_init()to initialize the Wasm engine.Wraps the result in an
anyhow::Result, returning an error with context if initialization fails.
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 |
|---|---|---|
|
| Set of allowed Wasm binary hashes to precompile. |
|
| Filesystem path to the Wasm binaries root directory. |
Returns
A tuple containing:
The initialized Wasm engine (
wasmtime::Engine).A cache map of Wasm binary hashes to their precompiled components.
Implementation Details
Calls
init_wasm_engine()to create the Wasm engine.Uses
Engine::extern_precompile_all_wasm_from_hash_list()from thetvm_vm::executorto precompile all Wasm binaries specified by the whitelist.Returns both the engine and the precompiled components on success.
Usage Example
let (engine, component_cache) = WasmNodeCache::init_wasm_engine_and_precompile_components(
whitelist,
"./config/wasm".to_string(),
)?;
Important Implementation Details
Wasm Hash Whitelist: This file enforces a security model by restricting the Wasm binaries that can be executed to a fixed set of known hashes. This prevents execution of unauthorized or malicious Wasm code.
Precompilation: Precompiling Wasm components at initialization improves runtime performance by avoiding compilation during execution.
Error Handling: Uses the
anyhowcrate to propagate errors with context, ensuring that misconfigurations or bugs during initialization are promptly reported.External Engine Integration: The Wasm engine lifecycle and component precompilation rely on external APIs from
tvm_vm::executor::Engineto handle environment-specific Wasm execution logic.
Interaction with Other Parts of the System
tvm_vm::executor::Engine: Provides the external functions:
extern_wasm_engine_init()for Wasm engine creation.extern_precompile_all_wasm_from_hash_list()for precompiling Wasm components.
wasmtime: The
wasmtimecrate is used to manage Wasm engine instances and Wasm components.Filesystem: Reads Wasm binaries from a configurable root path (
./config/wasmby default).The cache produced by this file is intended to be used by the Wasm execution runtime elsewhere in the node to efficiently load and run allowed Wasm modules.
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