Skip to content

Commit

Permalink
fix(provider): use wasmtimer for wasm32 target (#1426)
Browse files Browse the repository at this point in the history
* fix(provider): use wasmtimer for wasm32 target

The `std::time` library is mostly not supported on the wasm32 target.
For this reason, we switch out some of these calls to use the crate
`wasmtimer`.

* chore: update other places too

---------

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
  • Loading branch information
b-zee and DaniPopes authored Oct 3, 2024
1 parent 11a32cf commit a17aad0
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ futures = "0.3"
futures-util = "0.3"
futures-executor = "0.3"
futures-utils-wasm = "0.1"
wasmtimer = "0.2.0"

hyper = { version = "1.2", default-features = false }
hyper-util = "0.1"
Expand Down
3 changes: 3 additions & 0 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ tokio = { workspace = true, features = ["sync", "macros"] }
tracing.workspace = true
url = { workspace = true, optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasmtimer.workspace = true

[dev-dependencies]
alloy-consensus = { workspace = true, features = ["kzg"] }
alloy-primitives = { workspace = true, features = ["rand"] }
Expand Down
10 changes: 8 additions & 2 deletions crates/provider/src/heart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ use std::{
collections::{BTreeMap, VecDeque},
fmt,
future::Future,
time::{Duration, Instant},
time::Duration,
};
use tokio::{
select,
sync::{mpsc, oneshot, watch},
};

#[cfg(target_arch = "wasm32")]
use wasmtimer::{std::Instant, tokio::sleep_until};

#[cfg(not(target_arch = "wasm32"))]
use {std::time::Instant, tokio::time::sleep_until};

/// Errors which may occur when watching a pending transaction.
#[derive(Debug, thiserror::Error)]
pub enum PendingTransactionError {
Expand Down Expand Up @@ -664,7 +670,7 @@ impl<N: Network, S: Stream<Item = N::BlockResponse> + Unpin + 'static> Heartbeat
'shutdown: loop {
{
let next_reap = self.next_reap();
let sleep = std::pin::pin!(tokio::time::sleep_until(next_reap.into()));
let sleep = std::pin::pin!(sleep_until(next_reap.into()));

// We bias the select so that we always handle new messages
// before checking blocks, and reap timeouts are last.
Expand Down
1 change: 1 addition & 0 deletions crates/transport-ws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ rustls = { workspace = true, features = ["ring"] }
# WASM only
[target.'cfg(target_arch = "wasm32")'.dependencies]
ws_stream_wasm = "0.7.4"
wasmtimer.workspace = true
7 changes: 6 additions & 1 deletion crates/transport-ws/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ use alloy_transport::{utils::Spawnable, Authorization, TransportErrorKind, Trans
use futures::{SinkExt, StreamExt};
use serde_json::value::RawValue;
use std::time::Duration;
use tokio::time::sleep;
use tokio_tungstenite::{
tungstenite::{self, client::IntoClientRequest, Message},
MaybeTlsStream, WebSocketStream,
};

#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;

#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;

type TungsteniteStream = WebSocketStream<MaybeTlsStream<tokio::net::TcpStream>>;

const KEEPALIVE: u64 = 10;
Expand Down
1 change: 1 addition & 0 deletions crates/transport/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tokio = { workspace = true, features = ["rt", "time"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = { version = "0.4", optional = true }
wasmtimer.workspace = true

[features]
wasm-bindgen = ["dep:wasm-bindgen-futures"]
8 changes: 7 additions & 1 deletion crates/transport/src/layers/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ use std::{
use tower::{Layer, Service};
use tracing::trace;

#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;

#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;

/// A Transport Layer that is responsible for retrying requests based on the
/// error type. See [`TransportError`].
///
Expand Down Expand Up @@ -192,7 +198,7 @@ where
"(all in ms) backing off due to rate limit"
);

tokio::time::sleep(total_backoff).await;
sleep(total_backoff).await;
} else {
this.requests_enqueued.fetch_sub(1, Ordering::SeqCst);
return Err(err);
Expand Down

0 comments on commit a17aad0

Please sign in to comment.