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 all 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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ alloy-sol-types = { version = "0.7.2", default-features = false }

alloy-rlp = { version = "0.3", default-features = false }

alloy-chains = { version = "0.1.18", default-features = false }

# ethereum
ethereum_ssz_derive = "0.5"
ethereum_ssz = "0.5"
Expand Down
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.workspace = true
async-stream = "0.3"
async-trait.workspace = true
auto_impl.workspace = true
Expand Down
13 changes: 13 additions & 0 deletions crates/provider/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
provider::SendableTx,
Provider, RootProvider,
};
use alloy_chains::NamedChain;
use alloy_network::{Ethereum, Network};
use alloy_rpc_client::{BuiltInConnectionString, ClientBuilder, RpcClient};
use alloy_transport::{BoxTransport, Transport, TransportError, TransportResult};
Expand Down Expand Up @@ -216,6 +217,18 @@ impl<L, F, N> ProviderBuilder<L, F, N> {
ProviderBuilder { layer: self.layer, filler: self.filler, network: PhantomData }
}

/// Add a chain layer to the stack being built. The layer will set
/// the client's poll interval based on the average block time for this chain.
///
/// Does nothing to the client with a local transport.
pub fn with_chain(
self,
chain: NamedChain,
) -> ProviderBuilder<Stack<crate::layers::ChainLayer, L>, F, N> {
let chain_layer = crate::layers::ChainLayer::from(chain);
self.layer(chain_layer)
}

/// Finish the layer stack by providing a root [`Provider`], outputting
/// the final [`Provider`] type with all stack components.
pub fn on_provider<P, T>(self, provider: P) -> F::Provider
Expand Down
44 changes: 44 additions & 0 deletions crates/provider/src/layers/chain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use alloy_chains::NamedChain;
use alloy_network::Ethereum;
use alloy_transport::Transport;
use std::time::Duration;

use crate::{Provider, ProviderLayer};

/// A layer that wraps a [`NamedChain`]. The layer will be used to set
/// the client's poll interval based on the average block time for this chain.
///
/// Does nothing to the client with a local transport.
#[derive(Debug, Clone, Copy)]
pub struct ChainLayer(NamedChain);

impl ChainLayer {
/// Get the chain's average blocktime, if applicable.
pub const fn average_blocktime_hint(&self) -> Option<Duration> {
self.0.average_blocktime_hint()
}
}

impl From<NamedChain> for ChainLayer {
fn from(chain: NamedChain) -> Self {
Self(chain)
}
}

impl<P, T> ProviderLayer<P, T, Ethereum> for ChainLayer
where
P: Provider<T>,
T: Transport + Clone,
{
type Provider = P;

fn layer(&self, inner: P) -> Self::Provider {
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
if !inner.client().is_local() {
if let Some(avg_block_time) = self.average_blocktime_hint() {
let poll_interval = avg_block_time.mul_f32(0.6);
inner.client().set_poll_interval(poll_interval);
}
}
inner
}
}
7 changes: 5 additions & 2 deletions crates/provider/src/layers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! Useful layer implementations for the provider. Currently this
//! module contains the `AnvilLayer` and `AnvilProvider` types, when the anvil
//! feature is enabled.
//! module contains the `AnvilLayer`, `AnvilProvider` and `ChainLayer`
//! types.

#[cfg(any(test, feature = "anvil"))]
mod anvil;
#[cfg(any(test, feature = "anvil"))]
pub use anvil::{AnvilLayer, AnvilProvider};

mod chain;
pub use chain::ChainLayer;
15 changes: 8 additions & 7 deletions crates/rpc-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<T> RpcClient<T> {
///
/// 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.
pub fn with_poll_interval(self, poll_interval: u64) -> Self {
pub fn with_poll_interval(self, poll_interval: Duration) -> Self {
RexCloud marked this conversation as resolved.
Show resolved Hide resolved
self.inner().set_poll_interval(poll_interval);
self
}
Expand Down Expand Up @@ -185,9 +185,10 @@ impl<T> RpcClientInner<T> {
Duration::from_millis(self.poll_interval.load(Ordering::Relaxed))
}

/// Set the poll interval for the client in milliseconds.
pub fn set_poll_interval(&self, poll_interval: u64) {
self.poll_interval.store(poll_interval, Ordering::Relaxed);
/// Set the poll interval for the client in milliseconds. Default:
/// 7s for remote and 250ms for local transports.
pub fn set_poll_interval(&self, poll_interval: Duration) {
self.poll_interval.store(poll_interval.as_millis() as u64, Ordering::Relaxed);
}

/// Returns a reference to the underlying transport.
Expand Down Expand Up @@ -344,9 +345,9 @@ mod tests {

#[test]
fn test_client_with_poll_interval() {
let poll_interval = Duration::from_millis(5_000);
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));
.with_poll_interval(poll_interval);
assert_eq!(client.poll_interval(), poll_interval);
}
}
Loading