Skip to content

Commit ce6d67e

Browse files
Implement avail-light-web
1 parent ab8ba97 commit ce6d67e

File tree

12 files changed

+314
-35
lines changed

12 files changed

+314
-35
lines changed

.github/workflows/default.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ jobs:
3333
- uses: actions-rust-lang/setup-rust-toolchain@v1
3434
with:
3535
components: clippy
36-
- run: cargo clippy --workspace -- -D warnings
36+
# TODO: Enable avail-light-web once issue with rocksdb feature being applied
37+
# accross the workspace is resolved
38+
- run: cargo clippy --workspace --exclude avail-light-web -- -D warnings
3739

3840
test:
3941
name: cargo test
@@ -42,7 +44,9 @@ jobs:
4244
- uses: actions/checkout@v4
4345
- uses: arduino/setup-protoc@v2
4446
- uses: actions-rust-lang/setup-rust-toolchain@v1
45-
- run: cargo test --workspace --benches --tests
47+
# TODO: Enable avail-light-web once issue with rocksdb feature being applied
48+
# accross the workspace is resolved
49+
- run: cargo test --workspace --benches --tests --exclude avail-light-web
4650
env:
4751
RUSTFLAGS: "-C instrument-coverage"
4852
LLVM_PROFILE_FILE: "profile-%p-%m.profraw"

Cargo.lock

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ members = [
77
"crawler",
88
"fat",
99
"relay",
10+
"web",
1011
]
1112
default-members = ["client"]
1213
resolver = "2"
@@ -25,7 +26,7 @@ anyhow = "1.0.71"
2526
async-std = { version = "1.12.0", features = ["attributes"] }
2627
async-trait = "0.1.73"
2728
clap = { version = "4.4.4", features = ["derive", "cargo"] }
28-
color-eyre = "0.6.2"
29+
color-eyre = { version = "0.6.2", default-features = false }
2930
confy = "0.5.1"
3031
hex = "0.4.3"
3132
rand = "0.8.4"

core/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ better-panic = "0.3.0"
2020
blake2b_simd = "1.0.2"
2121
clap = { workspace = true }
2222
codec = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "full", "bit-vec"] }
23-
color-eyre = { workspace = true }
2423
confy = { workspace = true }
2524
derive_more = { version = "1", features = ["from"] }
2625
dusk-bytes = { version = "0.1.6", default-features = false }
@@ -49,6 +48,7 @@ uuid = { workspace = true }
4948
async-std = { workspace = true }
5049
chrono = "0.4.19"
5150
libp2p = { workspace = true }
51+
color-eyre = { workspace = true, default-features = true }
5252
hyper = { version = "0.14.23", features = ["full", "http1"] }
5353
jsonrpsee-core = { version = "0.21.0", features = ["client"] }
5454
libc = "0.2.150"
@@ -76,6 +76,7 @@ thiserror-no-std = "2.0.2"
7676
rand = { version = "0.8.4", default-features = false }
7777
libp2p = { workspace = true, features = ["wasm-bindgen"] }
7878
libp2p-webrtc-websys = { workspace = true }
79+
color-eyre = { workspace = true }
7980
wasm-bindgen = "0.2.90"
8081
wasm-timer = "0.2.5"
8182
web-time = "1.1.0"

core/src/light_client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use crate::{
4343
utils::{blake2_256, calculate_confidence, extract_kate},
4444
};
4545

46+
#[derive(Debug)]
4647
pub enum OutputEvent {
4748
RecordBlockProcessingDelay(f64),
4849
CountSessionBlocks,

core/src/network/p2p.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,9 @@ async fn build_swarm(
317317
};
318318
#[cfg(target_arch = "wasm32")]
319319
{
320+
use libp2p_webrtc_websys as webrtc;
320321
swarm = tokio_swarm
321-
.with_other_transport(|key| {
322-
libp2p_webrtc_websys::Transport::new(libp2p_webrtc_websys::Config::new(&key))
323-
})?
322+
.with_other_transport(|key| webrtc::Transport::new(webrtc::Config::new(&key)))?
324323
.with_relay_client(noise::Config::new, yamux::Config::default)?
325324
.with_behaviour(behaviour)?
326325
.with_swarm_config(|c| generate_config(c, cfg))

core/src/network/rpc/client.rs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,11 @@ impl GenesisHash {
105105
}
106106

107107
let bytes: [u8; 32] = from_hex(hex_str)
108-
.map_err(|_| ClientCreationError::InvalidGenesisHash(hex_str.to_string()))?
108+
.map_err(|_| ClientCreationError::InvalidGenesisHash(hex_str.to_string()))
109+
.map_err(Report::msg)?
109110
.try_into()
110-
.map_err(|_| ClientCreationError::InvalidGenesisHash(hex_str.to_string()))?;
111+
.map_err(|_| ClientCreationError::InvalidGenesisHash(hex_str.to_string()))
112+
.map_err(Report::msg)?;
111113

112114
Ok(Self::Hash(H256::from(bytes)))
113115
}
@@ -262,8 +264,8 @@ impl<D: Database> Client<D> {
262264

263265
match connection_result {
264266
Ok(Ok(ConnectionAttempt { client, node, .. })) => Ok((client, node)),
265-
Ok(Err(err)) => Err(RetryError::ConnectionFailed(err).into()),
266-
Err(err) => Err(RetryError::Shutdown(err.to_string()).into()),
267+
Ok(Err(err)) => Err(Report::msg(RetryError::ConnectionFailed(err))),
268+
Err(err) => Err(Report::msg(RetryError::Shutdown(err.to_string()))),
267269
}
268270
}
269271

@@ -289,7 +291,7 @@ impl<D: Database> Client<D> {
289291
Fut: std::future::Future<Output = Result<T>>,
290292
{
291293
if nodes.is_empty() {
292-
return Err(ClientCreationError::NoNodesAvailable.into());
294+
return Err(Report::msg(ClientCreationError::NoNodesAvailable));
293295
}
294296

295297
let mut last_error = None;
@@ -306,10 +308,9 @@ impl<D: Database> Client<D> {
306308
}
307309
}
308310

309-
Err(ClientCreationError::AllNodesFailed {
311+
Err(Report::msg(ClientCreationError::AllNodesFailed {
310312
last_error: last_error.unwrap_or_else(|| eyre!("No error recorded")),
311-
}
312-
.into())
313+
}))
313314
}
314315

315316
// Tries to connect to the provided RPC host, verifies the genesis hash,
@@ -326,33 +327,32 @@ impl<D: Database> Client<D> {
326327
match Self::create_rpc_client(&node.host, expected_genesis_hash).await {
327328
Ok((client, node)) => {
328329
// Execute the provided RPC function call with the created client
329-
let result =
330-
f(client.clone())
331-
.await
332-
.map_err(|e| ClientCreationError::RpcCallFailed {
333-
host: node.host.clone(),
334-
error: e,
335-
})?;
330+
let result = f(client.clone()).await.map_err(|e| {
331+
Report::msg(ClientCreationError::RpcCallFailed {
332+
host: node.host.clone(),
333+
error: e,
334+
})
335+
})?;
336336

337337
Ok(ConnectionAttempt {
338338
client,
339339
node,
340340
result,
341341
})
342342
},
343-
Err(err) => Err(ClientCreationError::ConnectionFailed {
343+
Err(err) => Err(Report::msg(ClientCreationError::ConnectionFailed {
344344
host: node.host.clone(),
345345
error: err,
346-
}
347-
.into()),
346+
})),
348347
}
349348
}
350349

351350
// Creates the RPC client by connecting to the provided RPC host and checks if the provided genesis hash matches the one from the client
352351
async fn create_rpc_client(host: &str, expected_genesis_hash: &str) -> Result<(SDK, Node)> {
353352
let client = SDK::new_insecure(host)
354353
.await
355-
.map_err(|e| ClientCreationError::SdkFailure(eyre!("{e}")))?;
354+
.map_err(|e| ClientCreationError::SdkFailure(eyre!("{e}")))
355+
.map_err(Report::msg)?;
356356

357357
// Verify genesis hash
358358
let genesis_hash = client.api.genesis_hash();
@@ -361,12 +361,11 @@ impl<D: Database> Client<D> {
361361
let expected_hash = GenesisHash::from_hex(expected_genesis_hash)?;
362362

363363
if !expected_hash.matches(&genesis_hash) {
364-
return Err(ClientCreationError::GenesisHashMismatch {
364+
return Err(Report::msg(ClientCreationError::GenesisHashMismatch {
365365
host: host.to_string(),
366366
expected: expected_genesis_hash.to_string(),
367367
found: format!("{:?}", genesis_hash),
368-
}
369-
.into());
368+
}));
370369
}
371370

372371
// Fetch system and runtime information
@@ -375,7 +374,8 @@ impl<D: Database> Client<D> {
375374
.system
376375
.version()
377376
.await
378-
.map_err(|e| ClientCreationError::SystemVersionError(e.into()))?;
377+
.map_err(|e| ClientCreationError::SystemVersionError(e.into()))
378+
.map_err(Report::msg)?;
379379

380380
let runtime_version = client.api.runtime_version();
381381

@@ -402,7 +402,11 @@ impl<D: Database> Client<D> {
402402
}
403403

404404
// If current client failed try to reconnect using stored node
405-
let connected_node = self.db.get(RpcNodeKey).ok_or(RetryError::NoPreviousNode)?;
405+
let connected_node = self
406+
.db
407+
.get(RpcNodeKey)
408+
.ok_or(RetryError::NoPreviousNode)
409+
.map_err(Report::msg)?;
406410

407411
warn!(
408412
"Executing RPC call with host: {} failed. Trying to create a new RPC connection.",
@@ -436,8 +440,8 @@ impl<D: Database> Client<D> {
436440

437441
match retry_result {
438442
Ok(Ok(result)) => Ok(result),
439-
Ok(Err(err)) => Err(RetryError::ConnectionFailed(err).into()),
440-
Err(err) => Err(RetryError::Shutdown(err.to_string()).into()),
443+
Ok(Err(err)) => Err(Report::msg(RetryError::ConnectionFailed(err))),
444+
Err(err) => Err(Report::msg(RetryError::Shutdown(err.to_string()))),
441445
}
442446
}
443447

@@ -464,8 +468,8 @@ impl<D: Database> Client<D> {
464468
self.db.put(RpcNodeKey, node);
465469
Ok(result)
466470
},
467-
Ok(Err(err)) => Err(RetryError::ConnectionFailed(err).into()),
468-
Err(err) => Err(RetryError::Shutdown(err.to_string()).into()),
471+
Ok(Err(err)) => Err(Report::msg(RetryError::ConnectionFailed(err))),
472+
Err(err) => Err(Report::msg(RetryError::Shutdown(err.to_string()))),
469473
}
470474
}
471475

core/src/utils.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,19 @@ pub fn filter_auth_set_changes(header: &AvailHeader) -> Vec<Vec<(AuthorityId, u6
115115
}
116116

117117
pub fn install_panic_hooks(shutdown: Controller<String>) -> Result<()> {
118+
#[cfg(not(target_arch = "wasm32"))]
118119
// initialize color-eyre hooks
119120
let (panic_hook, eyre_hook) = color_eyre::config::HookBuilder::default()
120121
.display_location_section(true)
121122
.display_env_section(true)
122123
.into_hooks();
123124

125+
#[cfg(target_arch = "wasm32")]
126+
// initialize color-eyre hooks
127+
let (panic_hook, eyre_hook) = color_eyre::config::HookBuilder::default()
128+
.display_env_section(true)
129+
.into_hooks();
130+
124131
// install hook as global handler
125132
eyre_hook.install()?;
126133

web/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "avail-light-web"
3+
version = "0.1.0"
4+
authors.workspace = true
5+
edition = "2021"
6+
repository.workspace = true
7+
8+
[lib]
9+
crate-type = ["cdylib", "rlib"]
10+
11+
[features]
12+
default = ["console_error_panic_hook"]
13+
14+
[dependencies]
15+
avail-light-core = { workspace = true }
16+
avail-rust = { workspace = true }
17+
clap = { workspace = true }
18+
console_error_panic_hook = { version = "0.1.7", optional = true }
19+
futures = { workspace = true }
20+
libp2p = { workspace = true }
21+
sp-io = { version = "30", features = ["disable_allocator", "disable_panic_handler"], default-features = false }
22+
tokio = { version = "^1", default-features = false, features = ["sync", "macros", "io-util", "rt"] }
23+
tokio_with_wasm = { version = "0.7.1", features = ["sync", "macros", "rt"] }
24+
tracing = { workspace = true }
25+
tracing-subscriber = { workspace = true }
26+
tracing-wasm = "0.2.1"
27+
wasm-bindgen = "0.2.93"
28+
wasm-bindgen-futures = "0.4.43"
29+
web-sys = { version = "0.3.70", features = ["console", "Window", "UrlSearchParams"] }
30+
web-time = "1.1.0"

web/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Avail Light Client (Web)
2+
3+
## Compile
4+
5+
`wasm-pack build --target web --dev`
6+
7+
## Run
8+
9+
`cp www/index.html pkg/`
10+
`cd pkg`
11+
`python3 -m http.server --directory .`
12+
13+
# Start LC
14+
15+
- Safari: http://localhost:8000/?network=hex&bootstrap=%2Fip4%2F209.38.38.158%2Fudp%2F39001%2Fwebrtc-direct%2Fcerthash%2FuEiCVz-CTCrMq4I2xwW_WznQPML3dos4GNWiXE_fJjvHiIg
16+
- Firefox: 0.0.0.0:8000/?network=hex&bootstrap=%2Fip4%2F209.38.38.158%2Fudp%2F39001%2Fwebrtc-direct%2Fcerthash%2FuEiCVz-CTCrMq4I2xwW_WznQPML3dos4GNWiXE_fJjvHiIg

0 commit comments

Comments
 (0)