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

perf(wasm): log RPC calls latency #818

Merged
merged 12 commits into from
Oct 31, 2024
14 changes: 13 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ helios = { git = "https://github.com/sergey-melnychuk/helios", branch = "beerus-
reqwest = { version = "0.12.3", default-features = false, features = ["json"] }

gloo-timers = { version = "0.3.0", features = ["futures"] }
web-sys = "0.3.69"
web-time = "1.1.0"


[dev-dependencies]
Expand Down
30 changes: 29 additions & 1 deletion src/eth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::str::FromStr;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

use ethers::types::{Address, Bytes, SyncingStatus, H256};
use eyre::{Context, Result};
Expand Down Expand Up @@ -85,7 +87,8 @@ impl EthereumClient {
let ret = self
.helios
.get_block_by_number(BlockTag::Number(block_number), false)
.await?
.await
.context("helios:get_block_by_number")?
.map(|block| (block_number, block.hash))
.ok_or_else(|| eyre::eyre!("Failed to fetch latest block"))?;
Ok(ret)
Expand All @@ -95,6 +98,9 @@ impl EthereumClient {
let (number, _) = self.latest().await?;
let tag = BlockTag::Number(number);

#[cfg(target_arch = "wasm32")]
let now = Instant::now();

let data = 0x35befa5du32.to_be_bytes(); // keccak("stateBlockNumber()")
let block_number: [u8; 32] = self
.call(&data, tag)
Expand All @@ -103,14 +109,29 @@ impl EthereumClient {
let block_number: [u8; 8] = block_number[24..].try_into().unwrap();
let block_number = u64::from_be_bytes(block_number);

#[cfg(target_arch = "wasm32")]
log_latency("stateBlockNumber", &now);

#[cfg(target_arch = "wasm32")]
let now = Instant::now();

let data = 0x382d83e3u32.to_be_bytes(); // keccak("stateBlockHash()")
let block_hash: H256 =
self.call(&data, tag).await.context("helios: state block hash")?;

#[cfg(target_arch = "wasm32")]
log_latency("stateBlockHash", &now);

#[cfg(target_arch = "wasm32")]
let now = Instant::now();

let data = 0x9588eca2u32.to_be_bytes(); // keccak("stateRoot()")"
let root: H256 =
self.call(&data, tag).await.context("helios: state root")?;

#[cfg(target_arch = "wasm32")]
log_latency("stateRoot", &now);

tracing::debug!(block_number, ?block_hash, ?root, "starknet state");

Ok((block_number, block_hash, root))
Expand Down Expand Up @@ -140,6 +161,13 @@ impl EthereumClient {
}
}

#[cfg(target_arch = "wasm32")]
fn log_latency(method: &str, instant: &Instant) {
let millis = instant.elapsed().as_millis();
let message = format!("call to {method} completed in {millis} ms");
web_sys::console::log_1(&message.into());
}

async fn get_client(
rpc: &str,
network: Network,
Expand Down
1 change: 0 additions & 1 deletion web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ document.addEventListener("DOMContentLoaded", () => {
return;
}
const config = JSON.stringify({
network: "mainnet",
ethereum_url: `http://127.0.0.1:3000/eth-mainnet.g.alchemy.com/v2/${alchemyKey}`,
starknet_url: `http://127.0.0.1:3000/starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/${alchemyKey}`
});
Expand Down
Loading
Loading