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: Add remaining node types for wasm #476

Merged
merged 11 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 16 additions & 13 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ prost-types = "0.13.3"
#libp2p = { path = "../../rust-libp2p/libp2p" }
#libp2p-core = { path = "../../rust-libp2p/core" }
#libp2p-swarm = { path = "../../rust-libp2p/swarm" }
#wasm-bindgen = { path = "../wasm-bindgen" }

# Uncomment this if you need debug symbols in release.
# Also check node-wasm's `Cargo.toml`.
Expand Down
4 changes: 2 additions & 2 deletions cli/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ async function showStats(node) {
return;
}

const squareRows = networkHead.dah.row_roots.length;
const squareCols = networkHead.dah.column_roots.length;
const squareRows = networkHead.dah.row_roots().length;
const squareCols = networkHead.dah.column_roots().length;

document.getElementById("block-height").innerText = networkHead.header.height;
document.getElementById("block-hash").innerText = networkHead.commit.block_id.hash;
Expand Down
6 changes: 3 additions & 3 deletions node-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ crate-type = ["cdylib", "rlib"]

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

anyhow = "1.0.86"
Expand All @@ -46,7 +46,7 @@ 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.93"
wasm-bindgen = "0.2.99"
wasm-bindgen-futures = "0.4.43"
web-sys = { version = "0.3.70", features = [
"BroadcastChannel",
Expand Down
91 changes: 28 additions & 63 deletions node-wasm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
use std::time::Duration;

use blockstore::EitherBlockstore;
use celestia_types::ExtendedHeader;
use js_sys::Array;
use lumina_node::blockstore::{InMemoryBlockstore, IndexedDbBlockstore};
use lumina_node::network;
use lumina_node::node::{NodeBuilder, MIN_PRUNING_DELAY, MIN_SAMPLING_WINDOW};
use lumina_node::store::{EitherStore, InMemoryStore, IndexedDbStore, SamplingMetadata};
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::to_value;
use tracing::{debug, error};
use wasm_bindgen::prelude::*;
use web_sys::BroadcastChannel;

use lumina_node::blockstore::{InMemoryBlockstore, IndexedDbBlockstore};
use lumina_node::network;
use lumina_node::node::{NodeBuilder, MIN_PRUNING_DELAY, MIN_SAMPLING_WINDOW};
use lumina_node::store::{EitherStore, InMemoryStore, IndexedDbStore};

use crate::commands::{CheckableResponseExt, NodeCommand, SingleHeaderQuery};
use crate::error::{Context, Result};
use crate::ports::WorkerClient;
Expand Down Expand Up @@ -218,64 +217,44 @@ impl NodeClient {
}

/// Request the head header from the network.
///
/// Returns a javascript object with given structure:
/// https://docs.rs/celestia-types/latest/celestia_types/struct.ExtendedHeader.html
#[wasm_bindgen(js_name = requestHeadHeader)]
pub async fn request_head_header(&self) -> Result<JsValue> {
pub async fn request_head_header(&self) -> Result<ExtendedHeader> {
let command = NodeCommand::RequestHeader(SingleHeaderQuery::Head);
let response = self.worker.exec(command).await?;
let header = response.into_header().check_variant()?;

header.into()
response.into_header().check_variant()?
}

/// Request a header for the block with a given hash from the network.
///
/// Returns a javascript object with given structure:
/// https://docs.rs/celestia-types/latest/celestia_types/struct.ExtendedHeader.html
#[wasm_bindgen(js_name = requestHeaderByHash)]
pub async fn request_header_by_hash(&self, hash: &str) -> Result<JsValue> {
pub async fn request_header_by_hash(&self, hash: &str) -> Result<ExtendedHeader> {
let command = NodeCommand::RequestHeader(SingleHeaderQuery::ByHash(hash.parse()?));
let response = self.worker.exec(command).await?;
let header = response.into_header().check_variant()?;

header.into()
response.into_header().check_variant()?
}

/// Request a header for the block with a given height from the network.
///
/// Returns a javascript object with given structure:
/// https://docs.rs/celestia-types/latest/celestia_types/struct.ExtendedHeader.html
#[wasm_bindgen(js_name = requestHeaderByHeight)]
pub async fn request_header_by_height(&self, height: u64) -> Result<JsValue> {
pub async fn request_header_by_height(&self, height: u64) -> Result<ExtendedHeader> {
let command = NodeCommand::RequestHeader(SingleHeaderQuery::ByHeight(height));
let response = self.worker.exec(command).await?;
let header = response.into_header().check_variant()?;

header.into()
response.into_header().check_variant()?
}

/// Request headers in range (from, from + amount] from the network.
///
/// The headers will be verified with the `from` header.
///
/// Returns an array of javascript objects with given structure:
/// https://docs.rs/celestia-types/latest/celestia_types/struct.ExtendedHeader.html
#[wasm_bindgen(js_name = requestVerifiedHeaders)]
pub async fn request_verified_headers(
&self,
from_header: JsValue,
from: &ExtendedHeader,
amount: u64,
) -> Result<Array> {
) -> Result<Vec<ExtendedHeader>> {
let command = NodeCommand::GetVerifiedHeaders {
from: from_header,
from: from.to_owned(),
amount,
};
let response = self.worker.exec(command).await?;
let headers = response.into_headers().check_variant()?;

headers.into()
response.into_headers().check_variant()?
}

/// Get current header syncing info.
Expand All @@ -293,51 +272,43 @@ impl NodeClient {
/// Returns a javascript object with given structure:
/// https://docs.rs/celestia-types/latest/celestia_types/struct.ExtendedHeader.html
#[wasm_bindgen(js_name = getNetworkHeadHeader)]
pub async fn get_network_head_header(&self) -> Result<JsValue> {
pub async fn get_network_head_header(&self) -> Result<Option<ExtendedHeader>> {
let command = NodeCommand::LastSeenNetworkHead;
let response = self.worker.exec(command).await?;
let header = response.into_last_seen_network_head().check_variant()?;

header.into()
response.into_last_seen_network_head().check_variant()?
}

/// Get the latest locally synced header.
///
/// Returns a javascript object with given structure:
/// https://docs.rs/celestia-types/latest/celestia_types/struct.ExtendedHeader.html
#[wasm_bindgen(js_name = getLocalHeadHeader)]
pub async fn get_local_head_header(&self) -> Result<JsValue> {
pub async fn get_local_head_header(&self) -> Result<ExtendedHeader> {
let command = NodeCommand::GetHeader(SingleHeaderQuery::Head);
let response = self.worker.exec(command).await?;
let header = response.into_header().check_variant()?;

header.into()
response.into_header().check_variant()?
}

/// Get a synced header for the block with a given hash.
///
/// Returns a javascript object with given structure:
/// https://docs.rs/celestia-types/latest/celestia_types/struct.ExtendedHeader.html
#[wasm_bindgen(js_name = getHeaderByHash)]
pub async fn get_header_by_hash(&self, hash: &str) -> Result<JsValue> {
pub async fn get_header_by_hash(&self, hash: &str) -> Result<ExtendedHeader> {
let command = NodeCommand::GetHeader(SingleHeaderQuery::ByHash(hash.parse()?));
let response = self.worker.exec(command).await?;
let header = response.into_header().check_variant()?;

header.into()
response.into_header().check_variant()?
}

/// Get a synced header for the block with a given height.
///
/// Returns a javascript object with given structure:
/// https://docs.rs/celestia-types/latest/celestia_types/struct.ExtendedHeader.html
#[wasm_bindgen(js_name = getHeaderByHeight)]
pub async fn get_header_by_height(&self, height: u64) -> Result<JsValue> {
pub async fn get_header_by_height(&self, height: u64) -> Result<ExtendedHeader> {
let command = NodeCommand::GetHeader(SingleHeaderQuery::ByHeight(height));
let response = self.worker.exec(command).await?;
let header = response.into_header().check_variant()?;

header.into()
response.into_header().check_variant()?
}

/// Get synced headers from the given heights range.
Expand All @@ -357,28 +328,24 @@ impl NodeClient {
&self,
start_height: Option<u64>,
end_height: Option<u64>,
) -> Result<Array> {
) -> Result<Vec<ExtendedHeader>> {
let command = NodeCommand::GetHeadersRange {
start_height,
end_height,
};
let response = self.worker.exec(command).await?;
let headers = response.into_headers().check_variant()?;

headers.into()
response.into_headers().check_variant()?
}

/// Get data sampling metadata of an already sampled height.
///
/// Returns a javascript object with given structure:
/// https://docs.rs/lumina-node/latest/lumina_node/store/struct.SamplingMetadata.html
#[wasm_bindgen(js_name = getSamplingMetadata)]
pub async fn get_sampling_metadata(&self, height: u64) -> Result<JsValue> {
pub async fn get_sampling_metadata(&self, height: u64) -> Result<Option<SamplingMetadata>> {
let command = NodeCommand::GetSamplingMetadata { height };
let response = self.worker.exec(command).await?;
let metadata = response.into_sampling_metadata().check_variant()?;

Ok(to_value(&metadata?)?)
response.into_sampling_metadata().check_variant()?
}

/// Returns a [`BroadcastChannel`] for events generated by [`Node`].
Expand Down Expand Up @@ -474,7 +441,6 @@ mod tests {
use gloo_timers::future::sleep;
use libp2p::{multiaddr::Protocol, Multiaddr};
use rexie::Rexie;
use serde_wasm_bindgen::from_value;
use wasm_bindgen_futures::spawn_local;
use wasm_bindgen_test::wasm_bindgen_test;
use web_sys::MessageChannel;
Expand All @@ -496,8 +462,7 @@ mod tests {
assert_eq!(info.num_peers, 1);

let bridge_head_header = rpc_client.header_network_head().await.unwrap();
let head_header: ExtendedHeader =
from_value(client.request_head_header().await.unwrap()).unwrap();
let head_header: ExtendedHeader = client.request_head_header().await.unwrap();
assert_eq!(head_header, bridge_head_header);
rpc_client
.p2p_close_peer(&PeerId(
Expand Down
Loading
Loading