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 17 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
41 changes: 27 additions & 14 deletions Cargo.lock

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

29 changes: 17 additions & 12 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, NodeDriver } 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 @@ -36,11 +36,12 @@ 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
}

console.log(network_head);
const square_rows = network_head.dah.row_roots.length;
const square_cols = network_head.dah.column_roots.length;

Expand Down Expand Up @@ -93,24 +94,28 @@ 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.driver = await new NodeDriver();

bind_config(await fetch_config());

if (await window.driver.is_running() === true) {
document.querySelectorAll('.config').forEach(elem => elem.disabled = true);
document.getElementById("peer-id").innerText = await window.driver.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.driver.start(window.config);
document.getElementById("peer-id").innerText = await window.driver.local_peer_id();
document.querySelectorAll(".status").forEach(elem => elem.style.visibility = "visible");
});

setInterval(async () => await show_stats(window.node), 1000)
setInterval(async () => await show_stats(window.driver), 1000)
}

await main(document, window);
17 changes: 17 additions & 0 deletions cli/static/worker.js
fl0rek marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Error.stackTraceLimit = 99; // rust stack traces can get pretty big, increase the default

import init, { run_worker } from "/wasm/lumina_node_wasm.js";

// unfortunately, `init()` takes long enough for the first connection (from the tab which
// starts Shared Worker) to be missed. As a workaround, we queue connections in js and then
// pass them to Rust, when it's ready. Rust code replaces `onconnect` handler, so this is
// only necessary on startup.
let queued = [];
onconnect = (event) => {
console.log("Queued connection", event);
queued.push(event.ports[0]);
}

await init();
await run_worker(queued);

13 changes: 11 additions & 2 deletions node-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ crate-type = ["cdylib", "rlib"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
celestia-types = { workspace = true }
libp2p = { workspace = true }
libp2p = { workspace = true, features = ["serde"] }
lumina-node = { workspace = true }
blockstore = { workspace = true }
celestia-tendermint = { workspace = true }

anyhow = "1.0.71"
console_error_panic_hook = "0.1.7"
futures = "0.3"
gloo-timers = "0.3"
instant = "0.1"
js-sys = "0.3.64"
serde = { version = "1.0.164", features = ["derive"] }
serde_repr = "0.1"
serde-wasm-bindgen = "0.6.0"
serde_repr = "0.1"
thiserror = "1.0"
fl0rek marked this conversation as resolved.
Show resolved Hide resolved
time = { version = "0.3", features = ["wasm-bindgen"] }
tokio = { version = "*", features = ["sync"]}
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.18", features = ["time"] }
tracing-web = "0.1.2"
wasm-bindgen = "0.2.88"
wasm-bindgen-futures = "0.4.37"
web-sys = { version = "0.3.69", features = ["BroadcastChannel", "MessageEvent", "Worker", "WorkerOptions", "WorkerType", "SharedWorker", "MessagePort", "SharedWorkerGlobalScope", "Blob", "BlobPropertyBag", "Url"]}
zvolin marked this conversation as resolved.
Show resolved Hide resolved
enum-as-inner = "0.6"
zvolin marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion node-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use wasm_bindgen::JsError;

pub mod node;
pub mod utils;
mod worker;
mod wrapper;

/// Alias for a `Result` with the error type [`JsError`].
pub type Result<T> = std::result::Result<T, JsError>;
pub type Result<T, E = JsError> = std::result::Result<T, E>;
Loading
Loading