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

feat: set poll interval based on connected chain #767

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ alloy-pubsub = { workspace = true, optional = true }
alloy-transport.workspace = true
alloy-primitives.workspace = true

alloy-chains = "0.1.18"
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
async-stream = "0.3"
async-trait.workspace = true
auto_impl.workspace = true
Expand Down
20 changes: 17 additions & 3 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
EthCall, PendingTransaction, PendingTransactionBuilder, PendingTransactionConfig, RootProvider,
RpcWithBlock, SendableTx,
};
use alloy_chains::Chain;
use alloy_eips::eip2718::Encodable2718;
use alloy_json_rpc::{RpcError, RpcParam, RpcReturn};
use alloy_network::{Ethereum, Network};
Expand Down Expand Up @@ -537,9 +538,22 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
self.client().request("web3_clientVersion", ()).await
}

/// Gets the chain ID.
fn get_chain_id(&self) -> RpcCall<T, (), U64, u64> {
self.client().request("eth_chainId", ()).map_resp(crate::utils::convert_u64)
/// Gets the chain ID. Sets the client's poll interval (if default)
/// to match the average block time for this chain.
async fn get_chain_id(&self) -> TransportResult<u64> {
self.client().request("eth_chainId", ()).map_resp(crate::utils::convert_u64).await.and_then(
|chain_id| {
let client = self.client();
if !client.is_custom_poll_interval() && !client.is_local() {
let poll_interval = match Chain::from_id(chain_id).average_blocktime_hint() {
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
Some(avg_block_time) => avg_block_time,
None => client.poll_interval(),
};
client.set_poll_interval(poll_interval.as_millis() as u64);
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
}
Ok(chain_id)
},
)
}

/// Gets the network ID. Same as `eth_chainId`.
Expand Down
20 changes: 15 additions & 5 deletions crates/rpc-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
borrow::Cow,
ops::Deref,
sync::{
atomic::{AtomicU64, Ordering},
atomic::{AtomicBool, AtomicU64, Ordering},
Arc, Weak,
},
time::Duration,
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<T> RpcClient<T> {
&self.0
}

/// Sets the poll interval for the client in milliseconds.
/// Sets custom poll interval for the client in milliseconds.
///
/// Note: This will only set the poll interval for the client if it is the only reference to the
/// inner client. If the reference is held by many, then it will not update the poll interval.
Expand Down Expand Up @@ -163,6 +163,8 @@ pub struct RpcClientInner<T> {
pub(crate) id: AtomicU64,
/// The poll interval for the client in milliseconds.
pub(crate) poll_interval: AtomicU64,
/// `true` if the poll interval is custom.
pub(crate) is_custom_poll_interval: AtomicBool,
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
}

impl<T> RpcClientInner<T> {
Expand All @@ -177,17 +179,24 @@ impl<T> RpcClientInner<T> {
is_local,
id: AtomicU64::new(0),
poll_interval: if is_local { AtomicU64::new(250) } else { AtomicU64::new(7000) },
is_custom_poll_interval: AtomicBool::new(false),
}
}

/// Returns the default poll interval (milliseconds) for the client.
/// Returns the poll interval (milliseconds) for the client.
pub fn poll_interval(&self) -> Duration {
Duration::from_millis(self.poll_interval.load(Ordering::Relaxed))
}

/// Set the poll interval for the client in milliseconds.
/// Set custom poll interval for the client in milliseconds.
pub fn set_poll_interval(&self, poll_interval: u64) {
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
self.poll_interval.store(poll_interval, Ordering::Relaxed);
self.is_custom_poll_interval.store(true, Ordering::Relaxed);
}

/// Returns `true` if the poll interval is custom.
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
pub fn is_custom_poll_interval(&self) -> bool {
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
self.is_custom_poll_interval.load(Ordering::Relaxed)
}

/// Returns a reference to the underlying transport.
Expand Down Expand Up @@ -287,6 +296,7 @@ impl<T: Transport + Clone> RpcClientInner<T> {
is_local: self.is_local,
id: self.id,
poll_interval: self.poll_interval,
is_custom_poll_interval: self.is_custom_poll_interval,
}
}
}
Expand Down Expand Up @@ -346,7 +356,7 @@ mod tests {
fn test_client_with_poll_interval() {
let client = RpcClient::new_http(reqwest::Url::parse("http://localhost").unwrap())
.with_poll_interval(5000);
// let client = client;
assert_eq!(client.poll_interval(), Duration::from_millis(5000));
assert!(client.is_custom_poll_interval());
}
}