Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasm): Run Lumina in a Shared Worker #265

Merged
merged 40 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f74b8de
wip
fl0rek Apr 5, 2024
cef4c0c
first pass
fl0rek Apr 9, 2024
830f8eb
add type safety
fl0rek Apr 10, 2024
ac8c77e
Type safe finish
fl0rek Apr 11, 2024
556003a
more macros less code
fl0rek Apr 12, 2024
ad9a2f5
cleanup
fl0rek Apr 12, 2024
46768ed
Start squeezing the errors through the channel
fl0rek Apr 12, 2024
2680863
cleanup
fl0rek Apr 16, 2024
0040592
Merge remote-tracking branch 'eiger/main' into feat/wasm-node-in-worker
fl0rek Apr 16, 2024
8b44387
Organise the code, add error passing instead of unwraps
fl0rek Apr 17, 2024
700ddec
appease clippy
fl0rek Apr 17, 2024
eefd831
Roll back to runtime type checking
fl0rek Apr 18, 2024
b94c5e0
cleanup, rw fix
fl0rek Apr 19, 2024
431be7a
code organisation and cleanup
fl0rek Apr 19, 2024
49a3a28
finishing touchees
fl0rek Apr 19, 2024
58496b0
pass errors through correctly, fix url blob
fl0rek Apr 23, 2024
fb8c5d6
to all the clippies I loved
fl0rek Apr 23, 2024
a74ba16
Apply suggestions from code review
fl0rek May 8, 2024
93fa8b8
Update node-wasm/src/node.rs
fl0rek May 8, 2024
618c332
Update node-wasm/src/node.rs
fl0rek May 8, 2024
5ad5a9d
fixes
fl0rek May 8, 2024
d9a779f
PR fixes
fl0rek May 8, 2024
d35c4ff
another pass
fl0rek May 8, 2024
2527b22
Merge remote-tracking branch 'eiger/main' into feat/wasm-node-in-worker
fl0rek May 8, 2024
dccdb25
mostly error cleanup
fl0rek May 8, 2024
aa6f173
clippin
fl0rek May 8, 2024
662e808
add close, consolidate errors,go back to NodeClient
fl0rek May 9, 2024
0495d05
switch to spawning worker from a static script
zvolin May 10, 2024
d3a2c92
Add Worker/SharedWorker switch for browser that need it
fl0rek May 16, 2024
e2ebffd
Merge branch 'main' into feat/wasm-node-in-worker
fl0rek May 16, 2024
0867f4b
Update node-wasm/js/worker.js
fl0rek May 17, 2024
aee53e4
Merge remote-tracking branch 'origin/main' into feat/wasm-node-in-worker
fl0rek Jun 20, 2024
db19449
rework errors to use new js errors and results
fl0rek Jun 20, 2024
17ab76a
Merge remote-tracking branch 'origin/main' into feat/wasm-node-in-worker
fl0rek Jun 20, 2024
0d3d7e5
Commit to the bit
fl0rek Jun 21, 2024
1743bf9
Apply suggestions from code review
fl0rek Jun 22, 2024
614cf9e
channel simplification 1/2
fl0rek Jun 22, 2024
48670ee
pr review
fl0rek Jun 22, 2024
d93a126
pr reviews
fl0rek Jun 24, 2024
7b4f80b
final touch
fl0rek Jun 25, 2024
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
10 changes: 10 additions & 0 deletions Cargo.lock

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

26 changes: 15 additions & 11 deletions cli/static/run_node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Error.stackTraceLimit = 99; // rust stack traces can get pretty big, increase the default

import init, { Node, NodeConfig } from "/wasm/lumina_node_wasm.js";
import init, { NodeConfig, NodeClient } from "/wasm/lumina_node_wasm.js";

async function fetch_config() {
const response = await fetch('/cfg.json');
Expand All @@ -20,7 +20,7 @@ async function fetch_config() {
}

async function show_stats(node) {
if (!node) {
if (!node || !await node.is_running()) {
return;
}
const info = await node.syncer_info();
Expand All @@ -38,7 +38,7 @@ async function show_stats(node) {

document.getElementById("peers").replaceChildren(peers_ul);

const network_head = node.get_network_head_header();
const network_head = await node.get_network_head_header();
if (network_head == null) {
return
}
Expand Down Expand Up @@ -95,21 +95,25 @@ function bind_config(data) {
});
}

async function start_node(config) {
window.node = await new Node(config);

document.getElementById("peer-id").innerText = await window.node.local_peer_id();
document.querySelectorAll(".status").forEach(elem => elem.style.visibility = "visible");
}

async function main(document, window) {
await init();

window.node = await new NodeClient();

bind_config(await fetch_config());

if (await window.node.is_running() === true) {
document.querySelectorAll('.config').forEach(elem => elem.disabled = true);
document.getElementById("peer-id").innerText = await window.node.local_peer_id();
document.querySelectorAll(".status").forEach(elem => elem.style.visibility = "visible");
}

document.getElementById("start").addEventListener("click", async () => {
document.querySelectorAll('.config').forEach(elem => elem.disabled = true);
start_node(window.config);

await window.node.start(window.config);
document.getElementById("peer-id").innerText = await window.node.local_peer_id();
document.querySelectorAll(".status").forEach(elem => elem.style.visibility = "visible");
});

setInterval(async () => await show_stats(window.node), 1000)
Expand Down
25 changes: 22 additions & 3 deletions node-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,41 @@ crate-type = ["cdylib", "rlib"]
blockstore = { workspace = true }
celestia-tendermint = { workspace = true }
celestia-types = { workspace = true }
libp2p = { workspace = true }
libp2p = { workspace = true, features = ["serde"] }
lumina-node = { workspace = true }

anyhow = "1.0.86"
console_error_panic_hook = "0.1.7"
enum-as-inner = "0.6.0"
futures = "0.3.30"
gloo-timers = "0.3.0"
instant = "0.1.13"
js-sys = "0.3.69"
serde = { version = "1.0.203", features = ["derive"] }
serde_repr = "0.1.19"
serde-wasm-bindgen = "0.6.5"
serde_repr = "0.1.19"
thiserror = "1.0.61"
time = { version = "0.3.36", features = ["wasm-bindgen"] }
tokio = { version = "1.38.0", features = ["sync"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["time"] }
tracing-web = "0.1.3"
wasm-bindgen = "0.2.92"
wasm-bindgen-futures = "0.4.42"
web-sys = { version = "0.3.69", features = ["BroadcastChannel", "Crypto"] }
web-sys = { version = "0.3.69", features = [
"BroadcastChannel",
"Crypto",
"DedicatedWorkerGlobalScope",
"MessageEvent",
"MessagePort",
"Navigator",
fl0rek marked this conversation as resolved.
Show resolved Hide resolved
"SharedWorker",
"SharedWorkerGlobalScope",
"Worker",
"WorkerGlobalScope",
"WorkerOptions",
"WorkerType",
] }

[package.metadata.docs.rs]
targets = ["wasm32-unknown-unknown"]
29 changes: 29 additions & 0 deletions node-wasm/js/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// this file will be installed by wasm-pack in pkg/snippets/<pkg-name>-<hash>/js/
import init, { run_worker } from '../../../lumina_node_wasm.js';

// get the path to this file
export function worker_script_url() {
return import.meta.url;
}

// if we are in a worker
if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
Error.stackTraceLimit = 99;

// for SharedWorker we queue incoming connections
// for dedicated Worker we queue incoming messages (coming from the single client)
let queued = [];
if (typeof SharedWorkerGlobalScope !== 'undefined' && self instanceof SharedWorkerGlobalScope) {
onconnect = (event) => {
queued.push(event)
}
} else {
onmessage = (event) => {
queued.push(event);
}
}

await init();
console.log("starting worker, queued messages: ", queued.length);
await run_worker(queued);
}
5 changes: 4 additions & 1 deletion node-wasm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::fmt::Display;

use serde::{Deserialize, Serialize};
use wasm_bindgen::convert::IntoWasmAbi;
use wasm_bindgen::describe::WasmDescribe;
use wasm_bindgen::JsValue;
Expand All @@ -10,7 +11,8 @@ use wasm_bindgen::JsValue;
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// An error that can cross the WASM ABI border.
pub struct Error(JsValue);
#[derive(Debug, Serialize, Deserialize)]
pub struct Error(#[serde(with = "serde_wasm_bindgen::preserve")] JsValue);

impl Error {
/// Create a new `Error` with the specified message.
Expand Down Expand Up @@ -113,6 +115,7 @@ from_display! {
libp2p::multiaddr::Error,
lumina_node::node::NodeError,
lumina_node::store::StoreError,
crate::worker::WorkerError,
}

/// Utility to add more context to the [`Error`].
Expand Down
1 change: 1 addition & 0 deletions node-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
pub mod error;
pub mod node;
pub mod utils;
mod worker;
mod wrapper;
Loading
Loading