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

ci(node-wasm): Add integration tests for node-wasm #420

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
run: wasm-pack test --headless --chrome node

- name: Test node-wasm crate
run: wasm-pack test --headless --chrome node-wasm
run: wasm-pack test --headless --release --chrome node-wasm

- name: Build and pack node-wasm
run: wasm-pack build --release --target web node-wasm && wasm-pack pack node-wasm
Expand Down
6 changes: 4 additions & 2 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions ci/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ services:
# provide an id for the bridge node (default: 0)
# each node should have a next natural number starting from 0
- NODE_ID=0
# setting SKIP_AUTH to true disables the use of JWT for authentication
- SKIP_AUTH=true
ports:
- 26658:26658
- 36658:26658
zvolin marked this conversation as resolved.
Show resolved Hide resolved
volumes:
- credentials:/credentials
- genesis:/genesis
Expand All @@ -38,10 +40,8 @@ services:
# provide an id for the bridge node (default: 0)
# each node should have a next natural number starting from 0
- NODE_ID=1
# setting SKIP_AUTH to true disables the use of JWT for authentication
- SKIP_AUTH=true
ports:
- 36658:26658
- 26658:26658
volumes:
- credentials:/credentials
- genesis:/genesis
Expand Down
20 changes: 19 additions & 1 deletion ci/run-bridge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ write_jwt_token() {
celestia bridge auth admin --p2p.network "$P2P_NETWORK" > "$NODE_JWT_FILE"
}

connect_to_common_bridge() {
# wait for nodes to spin up
sleep 5
# get PeerId of the common node
local peer_id=$(celestia p2p info --url 'ws://bridge-0:26658' | jq -r '.result.id')
# connect to it
echo "Connecting to $peer_id: /dns/bridge-0/tcp/2121"
celestia p2p connect "$peer_id" "/dns/bridge-0/tcp/2121"
}

main() {
# Initialize the bridge node
celestia bridge init --p2p.network "$P2P_NETWORK"
Expand All @@ -72,7 +82,15 @@ main() {
--rpc.addr 0.0.0.0 \
--core.ip validator \
--keyring.keyname "$NODE_NAME" \
--p2p.network "$P2P_NETWORK"
--p2p.network "$P2P_NETWORK" &

# each node without SKIP_AUTH connects to the one with, so that bridges can discover eachother
if [ ! "$SKIP_AUTH" == "true" ] ; then
connect_to_common_bridge
fi

# don't exit until celestia bridge running in the background does
wait
zvolin marked this conversation as resolved.
Show resolved Hide resolved
}

main
2 changes: 2 additions & 0 deletions node-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ web-sys = { version = "0.3.70", features = [

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.42"
celestia-rpc = { workspace = true, features = ["wasm-bindgen", "p2p"] }
rexie = "0.6.2"

[package.metadata.docs.rs]
targets = ["wasm32-unknown-unknown"]
Expand Down
142 changes: 142 additions & 0 deletions node-wasm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,145 @@ impl WasmNodeConfig {
})
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;

use super::*;
use crate::worker::NodeWorker;
zvolin marked this conversation as resolved.
Show resolved Hide resolved
use celestia_rpc::{prelude::*, Client};
use celestia_types::ExtendedHeader;
use libp2p::{multiaddr::Protocol, Multiaddr};
use rexie::Rexie;
use serde_wasm_bindgen::from_value;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::{
fmt::time::UtcTime, layer::SubscriberExt, util::SubscriberInitExt, Layer,
};
use tracing_web::MakeConsoleWriter;
use wasm_bindgen_futures::spawn_local;
use wasm_bindgen_test::wasm_bindgen_test;
use web_sys::MessageChannel;

const WS_URL: &str = "ws://localhost:36658";

pub async fn fetch_bridge_webtransport_multiaddr(client: &Client) -> Multiaddr {
let bridge_info = client.p2p_info().await.unwrap();

let mut ma = bridge_info
.addrs
.into_iter()
.find(|ma| {
let not_localhost = !ma
.iter()
.any(|prot| prot == Protocol::Ip4("127.0.0.1".parse().unwrap()));
let webtransport = ma
.protocol_stack()
.any(|protocol| protocol == "webtransport");
not_localhost && webtransport
})
.expect("Bridge doesn't listen on webtransport");

if !ma.protocol_stack().any(|protocol| protocol == "p2p") {
ma.push(Protocol::P2p(bridge_info.id.into()))
}

ma
}

async fn remove_database() -> rexie::Result<()> {
Rexie::delete("private").await?;
Rexie::delete("private-blockstore").await?;
Ok(())
}

fn setup_logs() {
let fmt_layer = tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_timer(UtcTime::rfc_3339()) // std::time is not available in browsers
.with_writer(MakeConsoleWriter) // write events to the console
.with_filter(LevelFilter::DEBUG); // TODO: allow customizing the log level
tracing_subscriber::registry().with(fmt_layer).init();
}
zvolin marked this conversation as resolved.
Show resolved Hide resolved

#[wasm_bindgen_test]
async fn request_network_head_header() {
remove_database().await.expect("failed to clear db");
setup_logs();
let rpc_client = Client::new(WS_URL).await.unwrap();

let message_channel = MessageChannel::new().unwrap();
let mut worker = NodeWorker::new(message_channel.port1().into());
let client = NodeClient::new(message_channel.port2().into())
.await
.unwrap();

let ma = fetch_bridge_webtransport_multiaddr(&rpc_client).await;

spawn_local(async move {
worker.run().await.unwrap();
});

assert!(!client.is_running().await.unwrap());

client
.start(&WasmNodeConfig {
network: Network::Private,
bootnodes: vec![format!("{ma}")],
})
.await
.unwrap();

assert!(client.is_running().await.unwrap());

client.wait_connected_trusted().await.unwrap();

let info = client.network_info().await.unwrap();
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();
assert_eq!(head_header, bridge_head_header)
}

#[wasm_bindgen_test]
async fn discover_network_peers() {
remove_database().await.expect("failed to clear db");
setup_logs();
let rpc_client = Client::new(WS_URL).await.unwrap();

let message_channel = MessageChannel::new().unwrap();
let mut worker = NodeWorker::new(message_channel.port1().into());
let client = NodeClient::new(message_channel.port2().into())
.await
.unwrap();

let ma = fetch_bridge_webtransport_multiaddr(&rpc_client).await;

spawn_local(async move {
worker.run().await.unwrap();
});

assert!(!client.is_running().await.unwrap());

client
.start(&WasmNodeConfig {
network: Network::Private,
bootnodes: vec![format!("{ma}")],
})
.await
.unwrap();

client.wait_connected_trusted().await.unwrap();
let info = client.network_info().await.unwrap();
assert_eq!(info.num_peers, 1);

gloo_timers::future::sleep(Duration::from_secs(1)).await;

client.wait_connected_trusted().await.unwrap();
let info = client.network_info().await.unwrap();
assert_eq!(info.num_peers, 2);
}
}
2 changes: 1 addition & 1 deletion node-wasm/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum WorkerError {
}

#[wasm_bindgen]
struct NodeWorker {
pub(crate) struct NodeWorker {
zvolin marked this conversation as resolved.
Show resolved Hide resolved
event_channel_name: String,
node: Option<NodeWorkerInstance>,
request_server: WorkerServer,
Expand Down
Loading