Skip to content

Commit 8390f2d

Browse files
authored
perf: configure provider poll interval (#8624)
1 parent 75342b1 commit 8390f2d

File tree

1 file changed

+27
-2
lines changed
  • crates/common/src/provider

1 file changed

+27
-2
lines changed

crates/common/src/provider/mod.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ use std::{
2727
};
2828
use url::ParseError;
2929

30+
/// The assumed block time for unknown chains.
31+
/// We assume that these are chains have a faster block time.
32+
const DEFAULT_UNKNOWN_CHAIN_BLOCK_TIME: Duration = Duration::from_secs(3);
33+
34+
/// The factor to scale the block time by to get the poll interval.
35+
const POLL_INTERVAL_BLOCK_TIME_SCALE_FACTOR: f32 = 0.6;
36+
3037
/// Helper type alias for a retry provider
3138
pub type RetryProvider<N = AnyNetwork> = RootProvider<RetryBackoffService<RuntimeTransport>, N>;
3239

@@ -229,7 +236,7 @@ impl ProviderBuilder {
229236
pub fn build(self) -> Result<RetryProvider> {
230237
let Self {
231238
url,
232-
chain: _,
239+
chain,
233240
max_retry,
234241
initial_backoff,
235242
timeout,
@@ -250,6 +257,15 @@ impl ProviderBuilder {
250257
.build();
251258
let client = ClientBuilder::default().layer(retry_layer).transport(transport, is_local);
252259

260+
if !is_local {
261+
client.set_poll_interval(
262+
chain
263+
.average_blocktime_hint()
264+
.unwrap_or(DEFAULT_UNKNOWN_CHAIN_BLOCK_TIME)
265+
.mul_f32(POLL_INTERVAL_BLOCK_TIME_SCALE_FACTOR),
266+
);
267+
}
268+
253269
let provider = AlloyProviderBuilder::<_, _, AnyNetwork>::default()
254270
.on_provider(RootProvider::new(client));
255271

@@ -260,7 +276,7 @@ impl ProviderBuilder {
260276
pub fn build_with_wallet(self, wallet: EthereumWallet) -> Result<RetryProviderWithSigner> {
261277
let Self {
262278
url,
263-
chain: _,
279+
chain,
264280
max_retry,
265281
initial_backoff,
266282
timeout,
@@ -282,6 +298,15 @@ impl ProviderBuilder {
282298

283299
let client = ClientBuilder::default().layer(retry_layer).transport(transport, is_local);
284300

301+
if !is_local {
302+
client.set_poll_interval(
303+
chain
304+
.average_blocktime_hint()
305+
.unwrap_or(DEFAULT_UNKNOWN_CHAIN_BLOCK_TIME)
306+
.mul_f32(POLL_INTERVAL_BLOCK_TIME_SCALE_FACTOR),
307+
);
308+
}
309+
285310
let provider = AlloyProviderBuilder::<_, _, AnyNetwork>::default()
286311
.with_recommended_fillers()
287312
.wallet(wallet)

0 commit comments

Comments
 (0)