Skip to content

Commit 3fb260a

Browse files
committed
docs(handover): Add webnode docs
1 parent 6d5d6ed commit 3fb260a

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed

docs/handover/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ structured onboarding experience for developers new to the project.
2222
routing and service patterns
2323
- **Testing framework**: [Testing Infrastructure](testing-infrastructure.md) -
2424
scenario-based testing with extensive examples
25+
- **Browser deployment**: [Webnode Implementation](webnode.md) - WebAssembly
26+
build target for running OpenMina in browsers
2527
- **Ledger implementation overview**: [Ledger Crate](ledger-crate.md) - OCaml
2628
port with Rust adaptations
2729
- **Service integration**: [Services](services.md) - complete service inventory
@@ -55,6 +57,7 @@ structured onboarding experience for developers new to the project.
5557
- [Fuzzing Infrastructure](#fuzzing-infrastructure)
5658
- [Services Technical Debt](#services-technical-debt)
5759
- [State Machine Technical Debt](#state-machine-technical-debt)
60+
- [Webnode Implementation](#webnode-implementation)
5861
- [Circuits](#circuits)
5962
- [Debug Block Proof Generation](#debug-block-proof-generation)
6063
- [Persistence](#persistence)
@@ -193,6 +196,16 @@ Systemic architectural issues in state machine implementations:
193196
- Safety and linting improvements (including clippy lints)
194197
- Prioritized refactoring roadmap
195198

199+
### [Webnode Implementation](webnode.md)
200+
201+
WebAssembly build target for browser deployment:
202+
203+
- WASM compilation with browser-specific threading adaptations
204+
- WebRTC-based P2P networking with pull-based protocol
205+
- JavaScript API for web application integration
206+
- Block production capabilities with browser-based SNARK proving
207+
- Technical constraints and workarounds for browser environment
208+
196209
### [Circuits](circuits.md)
197210

198211
Circuit generation process and distribution:

docs/handover/webnode.md

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# OpenMina Webnode Implementation
2+
3+
This document covers the WebAssembly (WASM) build target of OpenMina located in
4+
`node/web/`.
5+
6+
## Overview
7+
8+
The webnode compiles the full OpenMina node to WebAssembly for browser
9+
execution. It includes block production, transaction processing, SNARK
10+
verification, and WebRTC-based P2P networking.
11+
12+
### Design Goals
13+
14+
- Run the full node stack in browsers without plugins
15+
- Maintain compatibility with the main OpenMina implementation
16+
- Support block production with browser-based proving
17+
- Provide JavaScript API for web applications
18+
19+
## Architecture
20+
21+
### WASM Target
22+
23+
Builds as both `cdylib` and `rlib` crate types. Code is conditionally compiled
24+
with `#[cfg(target_family = "wasm")]` guards.
25+
26+
#### Build Process
27+
28+
```bash
29+
cd node/web
30+
cargo +nightly build --release --target wasm32-unknown-unknown
31+
wasm-bindgen --keep-debug --web --out-dir ../../frontend/src/assets/webnode/pkg ../../target/wasm32-unknown-unknown/release/openmina_node_web.wasm
32+
```
33+
34+
Requires nightly toolchain and generates bindings for
35+
`frontend/src/assets/webnode/pkg/`.
36+
37+
### Threading
38+
39+
Browser threading constraints require specific adaptations:
40+
41+
#### Rayon Setup
42+
43+
`init_rayon()` in `rayon.rs` configures the thread pool using
44+
`num_cpus.max(2) - 1` threads. Must be called before SNARK verification.
45+
46+
#### Task Spawning
47+
48+
- `P2pTaskSpawner`: Uses `wasm_bindgen_futures::spawn_local()`
49+
- `P2pTaskRemoteSpawner`: Routes tasks to main thread via
50+
`thread::start_task_in_main_thread()` because WebRTC APIs are main-thread only
51+
52+
## Features
53+
54+
Provides the same functionality as the native node:
55+
56+
- Transaction validation and application
57+
- Ledger state management
58+
- SNARK verification using browser-compiled circuits
59+
- Consensus participation
60+
- RPC interface for web applications
61+
62+
### Block Production
63+
64+
Supports block production with:
65+
66+
- Plain text or encrypted private keys (parsed in `parse_bp_key()`)
67+
- Custom coinbase receivers
68+
- Browser-based SNARK proving via `BlockProver::make()`
69+
70+
### Networking
71+
72+
#### WebRTC P2P Layer
73+
74+
- **Transport**: WebRTC DataChannels for browser-to-browser communication
75+
- **Protocol**: Pull-based networking (see [P2P README](../p2p/readme.md))
76+
- **Default Peer**:
77+
`/2bjYBqn45MmtismsAYP9rZ6Xns9snCcNsN1eDgQZB5s6AzY2CR2/https/webrtc3.webnode.openmina.com/443`
78+
- **Channels**: 8 distinct DataChannels for different protocol types (see
79+
[P2P README](../p2p/readme.md#channels))
80+
81+
#### Network Configuration
82+
83+
```rust
84+
initial_peers: Vec<P2pConnectionOutgoingInitOpts>
85+
peer_discovery: !self.p2p_no_discovery
86+
max_peers: Some(100)
87+
```
88+
89+
## Implementation Details
90+
91+
### Key Files
92+
93+
#### `lib.rs` - Main Entry Point
94+
95+
- **`main()`**: Automatic WASM initialization
96+
- **`run()`**: Primary node startup function
97+
- **`build_env()`**: Build information export
98+
- **`parse_bp_key()`**: Block producer key parsing
99+
100+
#### `node/builder.rs` - Node Construction
101+
102+
- **`NodeBuilder`**: Node configuration
103+
- **Configuration Methods**: P2P setup, block production, verification
104+
- **Default Peers**: Single hardcoded WebRTC peer for bootstrap
105+
106+
#### `node/mod.rs` - Type Definitions
107+
108+
- **Type Aliases**: `Node = openmina_node_common::Node<NodeService>`
109+
- **Task Spawners**: P2P-specific spawning implementations for browser
110+
constraints
111+
112+
#### `rayon.rs` - Threading Setup
113+
114+
- **`init_rayon()`**: Required initialization for multi-threading
115+
- **CPU Detection**: Automatic core count with minimum guarantees
116+
117+
### JavaScript Interface
118+
119+
#### Main Entry Point
120+
121+
```javascript
122+
const rpcSender = await run(blockProducerKey, seedNodesUrl, genesisConfigUrl);
123+
```
124+
125+
- `blockProducerKey`: Optional string or `[encrypted, password]` array
126+
- `seedNodesUrl`: Optional URL returning newline-separated peer addresses
127+
- `genesisConfigUrl`: Optional URL returning binary genesis config (defaults to
128+
`DEVNET_CONFIG`)
129+
130+
#### Setup
131+
132+
- `console_error_panic_hook` enables panic traces in browser console
133+
- `keep_worker_alive_cursed_hack()` prevents worker termination (wasm-bindgen
134+
issue #2945)
135+
136+
### Performance
137+
138+
- Parallel SNARK verification using `num_cpus.max(2) - 1` threads
139+
- Circuit reuse for verification operations
140+
- 100 peer connection limit configured in `P2pLimits`
141+
- Statistics collection via `gather_stats()` when enabled
142+
143+
## Dependencies
144+
145+
**WASM-specific**: `wasm-bindgen`, `wasm-bindgen-futures`, `js-sys`,
146+
`console_error_panic_hook`, `gloo-utils`
147+
148+
**Core**: Standard OpenMina workspace crates plus `rayon` for threading
149+
150+
## Known Issues
151+
152+
- Worker lifecycle requires `keep_worker_alive_cursed_hack()` due to
153+
wasm-bindgen issue #2945
154+
- WebRTC operations restricted to main thread
155+
- Careful initialization ordering required
156+
157+
## Technical Debt
158+
159+
- TODO in `setup_node()` for seed nodes refactoring
160+
- Single hardcoded default peer
161+
- Commented HTTP client and peer loading code in `builder.rs`
162+
163+
## Usage
164+
165+
```javascript
166+
// Basic startup
167+
const rpc = await run();
168+
169+
// With block producer
170+
const rpc = await run("private-key");
171+
const rpc = await run([encryptedKey, "password"]);
172+
173+
// With custom configuration
174+
const rpc = await run(key, peersUrl, genesisUrl);
175+
176+
// RPC access
177+
const peers = await rpc.state().peers();
178+
const stats = await rpc.stats().sync();
179+
```
180+
181+
## Future Work
182+
183+
- Split prover to its own WASM heap
184+
(https://github.com/openmina/openmina/issues/1128)
185+
- API for zkApp integration

0 commit comments

Comments
 (0)