diff --git a/testing/wasm-lightclient-tests/Cargo.lock b/testing/wasm-lightclient-tests/Cargo.lock index f806c1842f..c9d536cded 100644 --- a/testing/wasm-lightclient-tests/Cargo.lock +++ b/testing/wasm-lightclient-tests/Cargo.lock @@ -1114,9 +1114,9 @@ checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" [[package]] name = "js-sys" -version = "0.3.66" +version = "0.3.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +checksum = "9a1d36f1235bc969acba30b7f5990b864423a6068a10f7c90ae8f0112e3a59d1" dependencies = [ "wasm-bindgen", ] @@ -1916,6 +1916,19 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "scale-typegen" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00860983481ac590ac87972062909bef0d6a658013b592ccc0f2feb272feab11" +dependencies = [ + "proc-macro2", + "quote", + "scale-info", + "syn 2.0.48", + "thiserror", +] + [[package]] name = "scale-value" version = "0.13.0" @@ -2310,6 +2323,7 @@ dependencies = [ "getrandom", "hex", "impl-serde", + "instant", "jsonrpsee", "parity-scale-codec", "primitive-types", @@ -2343,6 +2357,7 @@ dependencies = [ "proc-macro2", "quote", "scale-info", + "scale-typegen", "subxt-metadata", "syn 2.0.48", "thiserror", @@ -2759,9 +2774,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" +checksum = "b1223296a201415c7fad14792dbefaace9bd52b62d33453ade1c5b5f07555406" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -2769,9 +2784,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +checksum = "fcdc935b63408d58a32f8cc9738a0bffd8f05cc7c002086c6ef20b7312ad9dcd" dependencies = [ "bumpalo", "log", @@ -2796,9 +2811,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +checksum = "3e4c238561b2d428924c49815533a8b9121c664599558a5d9ec51f8a1740a999" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2806,9 +2821,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +checksum = "bae1abb6806dc1ad9e560ed242107c0f6c84335f1749dd4e8ddb012ebd5e25a7" dependencies = [ "proc-macro2", "quote", @@ -2819,9 +2834,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" +checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" [[package]] name = "wasm-bindgen-test" diff --git a/testing/wasm-lightclient-tests/tests/wasm.rs b/testing/wasm-lightclient-tests/tests/wasm.rs index 04018babeb..92ef3543bb 100644 --- a/testing/wasm-lightclient-tests/tests/wasm.rs +++ b/testing/wasm-lightclient-tests/tests/wasm.rs @@ -1,10 +1,10 @@ #![cfg(target_arch = "wasm32")] -use subxt::{ - config::PolkadotConfig, - client::{LightClient, LightClientBuilder}, -}; use futures_util::StreamExt; +use subxt::{ + client::{LightClient, LightClientBuilder}, + config::PolkadotConfig, +}; use wasm_bindgen_test::*; wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); @@ -12,16 +12,7 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); // Run the tests by calling: // // ```text -// wasm-pack test --firefox --headless` -// ``` -// -// You'll need to have a substrate/polkadot node running: -// -// ```bash -// # Polkadot does not accept by default WebSocket connections to the P2P network. -// # Ensure `--listen-addr` is provided with valid ws address endpoint. -// # The `--node-key` provides a deterministic p2p address for the node. -// ./polkadot --dev --node-key 0000000000000000000000000000000000000000000000000000000000000001 --listen-addr /ip4/0.0.0.0/tcp/30333/ws +// wasm-pack test --firefox --headless // ``` // // Use the following to enable logs: @@ -32,17 +23,18 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); #[wasm_bindgen_test] async fn light_client_works() { - console_error_panic_hook::set_once(); - - let api: LightClient = LightClientBuilder::new() - .build_from_url("wss://rpc.polkadot.io:443") - .await - .expect("Cannot construct light client"); + let api = connect_to_rpc_node().await; tracing::info!("Subscribe to latest finalized blocks: "); - let mut blocks_sub = api.blocks().subscribe_finalized().await.expect("Cannot subscribe to finalized hashes").take(3); - // For each block, print a bunch of information about it: + let mut blocks_sub = api + .blocks() + .subscribe_finalized() + .await + .expect("Cannot subscribe to finalized hashes") + .take(3); + + // For each block, print information about it: while let Some(block) = blocks_sub.next().await { let block = block.expect("Block not valid"); @@ -53,3 +45,25 @@ async fn light_client_works() { tracing::info!(" Hash: {block_hash}"); } } + +/// We connect to an RPC node because the light client can struggle to sync in +/// time to a new local node for some reason. Because this can be brittle (eg RPC nodes can +/// go down or have network issues), we try a few RPC nodes until we find one that works. +async fn connect_to_rpc_node() -> LightClient { + let rpc_node_urls = [ + "wss://rpc.polkadot.io", + "wss://1rpc.io/dot", + "wss://polkadot-public-rpc.blockops.network/ws", + ]; + + for url in rpc_node_urls { + let res = LightClientBuilder::new().build_from_url(url).await; + + match res { + Ok(api) => return api, + Err(e) => tracing::warn!("Error connecting to RPC node {url}: {e}"), + } + } + + panic!("Could not connect to any RPC node") +} diff --git a/testing/wasm-rpc-tests/Cargo.lock b/testing/wasm-rpc-tests/Cargo.lock index b422d26688..ad6dd4fada 100644 --- a/testing/wasm-rpc-tests/Cargo.lock +++ b/testing/wasm-rpc-tests/Cargo.lock @@ -1121,9 +1121,9 @@ checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" [[package]] name = "js-sys" -version = "0.3.66" +version = "0.3.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +checksum = "9a1d36f1235bc969acba30b7f5990b864423a6068a10f7c90ae8f0112e3a59d1" dependencies = [ "wasm-bindgen", ] @@ -1928,6 +1928,19 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "scale-typegen" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00860983481ac590ac87972062909bef0d6a658013b592ccc0f2feb272feab11" +dependencies = [ + "proc-macro2", + "quote", + "scale-info", + "syn 2.0.48", + "thiserror", +] + [[package]] name = "scale-value" version = "0.13.0" @@ -2338,6 +2351,7 @@ dependencies = [ "getrandom", "hex", "impl-serde", + "instant", "jsonrpsee", "parity-scale-codec", "primitive-types", @@ -2370,6 +2384,7 @@ dependencies = [ "proc-macro2", "quote", "scale-info", + "scale-typegen", "subxt-metadata", "syn 2.0.48", "thiserror", @@ -2781,9 +2796,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" +checksum = "b1223296a201415c7fad14792dbefaace9bd52b62d33453ade1c5b5f07555406" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -2791,9 +2806,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +checksum = "fcdc935b63408d58a32f8cc9738a0bffd8f05cc7c002086c6ef20b7312ad9dcd" dependencies = [ "bumpalo", "log", @@ -2818,9 +2833,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +checksum = "3e4c238561b2d428924c49815533a8b9121c664599558a5d9ec51f8a1740a999" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2828,9 +2843,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +checksum = "bae1abb6806dc1ad9e560ed242107c0f6c84335f1749dd4e8ddb012ebd5e25a7" dependencies = [ "proc-macro2", "quote", @@ -2841,9 +2856,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" +checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" [[package]] name = "wasm-bindgen-test" diff --git a/testing/wasm-rpc-tests/tests/wasm.rs b/testing/wasm-rpc-tests/tests/wasm.rs index 67378b9900..6f8ae1e891 100644 --- a/testing/wasm-rpc-tests/tests/wasm.rs +++ b/testing/wasm-rpc-tests/tests/wasm.rs @@ -1,6 +1,6 @@ #![cfg(target_arch = "wasm32")] -use subxt::{config::PolkadotConfig}; +use subxt::config::SubstrateConfig; use wasm_bindgen_test::*; wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); @@ -11,10 +11,10 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); // wasm-pack test --firefox --headless` // ``` // -// You'll need to have a substrate/polkadot node running: +// You'll need to have a substrate node running: // // ```bash -// ./polkadot --dev +// ./substrate-node --dev --node-key 0000000000000000000000000000000000000000000000000000000000000001 --listen-addr /ip4/0.0.0.0/tcp/30333/ws // ``` // // Use the following to enable logs: @@ -25,7 +25,9 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); #[wasm_bindgen_test] async fn wasm_ws_transport_works() { - let client = subxt::client::OnlineClient::::from_url("ws://127.0.0.1:9944") + console_error_panic_hook::set_once(); + tracing_wasm::set_as_global_default(); + let client = subxt::client::OnlineClient::::from_url("ws://127.0.0.1:9944") .await .unwrap();