-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ClientRefs, Poller, and Streams (#179)
* wip: weak and ref clients * nit: use type alias * feat: poller and subscription streams * lint: clippy * doc: fix links * feat: polling limit * fix: name * feat: block heartbeat * wip: chaintask * feat: chain stream poller take 1 * fix: pubsub * chore: misc cleanups, nfc * feat: move implementations to trait, implement send transaction * chore: rename errors in logs to 'err' * fix: correct ser/de, add more tracing * test: add a primitive test * chore: unused import * feat: add retrying and ser caching to poller * feat: reduce polling interval if provider is local * fix: import serde * fix: export PendingTransaction * fix: PTX outputs hash, >1 waiting txs, fix initial block yield * nit * fix: pin the sleep * clippies * wasmm * fix: feature --------- Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
- Loading branch information
Showing
31 changed files
with
1,404 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use crate::{new::RootProviderInner, Provider, RootProvider, WeakProvider}; | ||
use alloy_network::Network; | ||
use alloy_primitives::{BlockNumber, U64}; | ||
use alloy_rpc_client::{PollTask, WeakClient}; | ||
use alloy_rpc_types::Block; | ||
use alloy_transport::{RpcError, Transport}; | ||
use async_stream::stream; | ||
use futures::{Stream, StreamExt}; | ||
use lru::LruCache; | ||
use std::{num::NonZeroUsize, sync::Arc, time::Duration}; | ||
|
||
/// The size of the block cache. | ||
const BLOCK_CACHE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(10) }; | ||
|
||
/// Maximum number of retries for fetching a block. | ||
const MAX_RETRIES: usize = 3; | ||
|
||
/// Default block number for when we don't have a block yet. | ||
const NO_BLOCK_NUMBER: BlockNumber = BlockNumber::MAX; | ||
|
||
pub(crate) struct ChainStreamPoller<P, T: Transport + Clone> { | ||
provider: WeakProvider<P>, | ||
poll_task: PollTask<T, (), U64>, | ||
next_yield: BlockNumber, | ||
known_blocks: LruCache<BlockNumber, Block>, | ||
} | ||
|
||
impl<N: Network, T: Transport + Clone> ChainStreamPoller<RootProviderInner<N, T>, T> { | ||
pub(crate) fn from_root(p: &RootProvider<N, T>) -> Self { | ||
let mut this = Self::new(Arc::downgrade(&p.inner), p.inner.weak_client()); | ||
if p.client().is_local() { | ||
this.poll_task.set_poll_interval(Duration::from_secs(1)); | ||
} | ||
this | ||
} | ||
} | ||
|
||
impl<P, T: Transport + Clone> ChainStreamPoller<P, T> { | ||
pub(crate) fn new(provider: WeakProvider<P>, client: WeakClient<T>) -> Self { | ||
Self { | ||
provider, | ||
poll_task: PollTask::new(client, "eth_blockNumber", ()), | ||
next_yield: NO_BLOCK_NUMBER, | ||
known_blocks: LruCache::new(BLOCK_CACHE_SIZE), | ||
} | ||
} | ||
|
||
pub(crate) fn into_stream<N: Network>(mut self) -> impl Stream<Item = Block> | ||
where | ||
P: Provider<N, T>, | ||
{ | ||
stream! { | ||
let mut poll_task = self.poll_task.spawn().into_stream(); | ||
'task: loop { | ||
// Clear any buffered blocks. | ||
while let Some(known_block) = self.known_blocks.pop(&self.next_yield) { | ||
debug!(number=self.next_yield, "yielding block"); | ||
self.next_yield += 1; | ||
yield known_block; | ||
} | ||
|
||
// Get the tip. | ||
let block_number = match poll_task.next().await { | ||
Some(Ok(block_number)) => block_number, | ||
Some(Err(err)) => { | ||
// This is fine. | ||
debug!(%err, "polling stream lagged"); | ||
continue 'task; | ||
} | ||
None => { | ||
debug!("polling stream ended"); | ||
break 'task; | ||
} | ||
}; | ||
let block_number = block_number.to::<u64>(); | ||
if self.next_yield == NO_BLOCK_NUMBER { | ||
assert!(block_number < NO_BLOCK_NUMBER, "too many blocks"); | ||
self.next_yield = block_number; | ||
} else if block_number < self.next_yield { | ||
debug!(block_number, self.next_yield, "not advanced yet"); | ||
continue 'task; | ||
} | ||
|
||
// Upgrade the provider. | ||
let Some(provider) = self.provider.upgrade() else { | ||
debug!("provider dropped"); | ||
break 'task; | ||
}; | ||
|
||
// Then try to fill as many blocks as possible. | ||
// TODO: Maybe use `join_all` | ||
let mut retries = MAX_RETRIES; | ||
for number in self.next_yield..=block_number { | ||
debug!(number, "fetching block"); | ||
let block = match provider.get_block_by_number(number, false).await { | ||
Ok(block) => block, | ||
Err(RpcError::Transport(err)) if retries > 0 && err.recoverable() => { | ||
debug!(number, %err, "failed to fetch block, retrying"); | ||
retries -= 1; | ||
continue; | ||
} | ||
Err(err) => { | ||
error!(number, %err, "failed to fetch block"); | ||
break 'task; | ||
} | ||
}; | ||
self.known_blocks.put(number, block); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.