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(providers): event, polling and streaming methods #274

Merged
merged 46 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
7d6f5f8
feat(providers): event, polling and streaming methods
DaniPopes Mar 8, 2024
f2dac32
update
DaniPopes Mar 11, 2024
bc5fa21
feat: better pending transaction
DaniPopes Mar 11, 2024
4d3046b
feat: test event polling
DaniPopes Mar 11, 2024
b63d5f3
rm subscribe
DaniPopes Mar 11, 2024
7111f00
chore: bump MSRV to 1.76
DaniPopes Mar 11, 2024
14a797b
feat: implement sol! event filters
DaniPopes Mar 11, 2024
2ac273f
comment
DaniPopes Mar 12, 2024
b8912bd
chore: rename, better docs
DaniPopes Mar 12, 2024
6edd8fe
stuff
DaniPopes Mar 12, 2024
188669f
typos
DaniPopes Mar 12, 2024
1218bb2
reorder
DaniPopes Mar 12, 2024
7f9454e
docs: add an example to PollerBuilder
DaniPopes Mar 12, 2024
025415d
docs: add examples to watch functions
DaniPopes Mar 12, 2024
6159a67
feat: make `RawProvider` object-safe
DaniPopes Mar 12, 2024
40b561a
fix: clippy, ci
DaniPopes Mar 12, 2024
069814d
chore: rename to config
DaniPopes Mar 12, 2024
48072de
fix: deserialize `Transaction`s in `FilterChanges`
DaniPopes Mar 12, 2024
1e1a7eb
Merge branch 'main' into dani/provider-streams
DaniPopes Mar 12, 2024
bd0d19d
lints
DaniPopes Mar 12, 2024
2c6f1d0
Merge branch 'main' into dani/provider-streams
DaniPopes Mar 12, 2024
6c6932b
morelints
DaniPopes Mar 12, 2024
acc2544
more const
DaniPopes Mar 12, 2024
87963a7
feat: return ptx builder in send_transaction
DaniPopes Mar 12, 2024
f1adb8e
some more docs
DaniPopes Mar 12, 2024
6845bd8
chore: clippy
DaniPopes Mar 12, 2024
e6a5587
wip: streams
DaniPopes Mar 12, 2024
8adaa41
Merge branch 'main' into dani/provider-streams
DaniPopes Mar 12, 2024
5d8c267
chore: update nonce manager
DaniPopes Mar 12, 2024
240d506
test: ignore box test for now
DaniPopes Mar 12, 2024
168b0c3
docs
DaniPopes Mar 12, 2024
3743e18
fix: expose box transport as any
DaniPopes Mar 12, 2024
12a421f
docs: readme
DaniPopes Mar 12, 2024
ceb8725
chore: note on pub provider field
DaniPopes Mar 12, 2024
eb0ee46
feat: return `PendingTransactionBuilder` in `CallBuilder::send`
DaniPopes Mar 12, 2024
8ddf18c
feat: flatten `Option<Receipt>` into the result
DaniPopes Mar 12, 2024
98fd3f6
chore: make `TransportErrorKind` non exhaustive
DaniPopes Mar 12, 2024
738a8ae
chore: slightly better API
DaniPopes Mar 13, 2024
1e81c8c
docs: add an example for sol! with contracts
DaniPopes Mar 13, 2024
512e15f
fix
DaniPopes Mar 13, 2024
70dd850
docs: show constructor arguments
DaniPopes Mar 13, 2024
19e43b3
word
DaniPopes Mar 13, 2024
2ed0440
feat: more docs and examples, add watch full pending txs
DaniPopes Mar 13, 2024
0dacfb0
feat: remove lag error from subscription streams
DaniPopes Mar 13, 2024
215d6e9
fix
DaniPopes Mar 13, 2024
c76fb8c
docs: mention subscription limitation
DaniPopes Mar 13, 2024
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
Prev Previous commit
Next Next commit
feat: return ptx builder in send_transaction
  • Loading branch information
DaniPopes committed Mar 12, 2024
commit 87963a78101d8c8b7c07c7ccaa467043fe56838d
4 changes: 2 additions & 2 deletions crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ impl<N: Network, T: Transport + Clone, P: Provider<N, T>, D: CallDecoder> CallBu
pub async fn send(
&self,
) -> Result<impl Future<Output = Result<Option<N::ReceiptResponse>>> + '_> {
let config = self.provider.send_transaction(self.request.clone()).await?;
Ok(config.with_provider(&self.provider).get_receipt().map_err(Into::into))
let builder = self.provider.send_transaction(self.request.clone()).await?;
Ok(builder.get_receipt().map_err(Into::into))
}

/// Calculates the address that will be created by the transaction, if any.
Expand Down
31 changes: 14 additions & 17 deletions crates/provider/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{new::RootProviderInner, Provider, RootProvider, WeakProvider};
use crate::{Provider, RootProvider};
use alloy_network::Network;
use alloy_primitives::{BlockNumber, U64};
use alloy_rpc_client::{PollerBuilder, WeakClient};
Expand All @@ -7,7 +7,7 @@ use alloy_transport::{RpcError, Transport};
use async_stream::stream;
use futures::{Stream, StreamExt};
use lru::LruCache;
use std::{num::NonZeroUsize, sync::Arc};
use std::{marker::PhantomData, num::NonZeroUsize};

/// The size of the block cache.
const BLOCK_CACHE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(10) };
Expand All @@ -18,33 +18,30 @@ 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>,
pub(crate) struct ChainStreamPoller<N, T> {
client: WeakClient<T>,
poll_task: PollerBuilder<T, (), U64>,
next_yield: BlockNumber,
known_blocks: LruCache<BlockNumber, Block>,
_phantom: PhantomData<N>,
}

impl<N: Network, T: Transport + Clone> ChainStreamPoller<RootProviderInner<N, T>, T> {
impl<N: Network, T: Transport + Clone> ChainStreamPoller<N, T> {
pub(crate) fn from_root(p: &RootProvider<N, T>) -> Self {
Self::new(Arc::downgrade(&p.inner), p.inner.weak_client())
Self::new(p.weak_client())
}
}

impl<P, T: Transport + Clone> ChainStreamPoller<P, T> {
pub(crate) fn new(provider: WeakProvider<P>, client: WeakClient<T>) -> Self {
pub(crate) fn new(client: WeakClient<T>) -> Self {
Self {
provider,
client: client.clone(),
poll_task: PollerBuilder::new(client, "eth_blockNumber", ()),
next_yield: NO_BLOCK_NUMBER,
known_blocks: LruCache::new(BLOCK_CACHE_SIZE),
_phantom: PhantomData,
}
}

pub(crate) fn into_stream<N: Network>(mut self) -> impl Stream<Item = Block>
where
P: Provider<N, T>,
{
pub(crate) fn into_stream(mut self) -> impl Stream<Item = Block> {
stream! {
let mut poll_task = self.poll_task.spawn().into_stream_raw();
'task: loop {
Expand Down Expand Up @@ -78,8 +75,8 @@ impl<P, T: Transport + Clone> ChainStreamPoller<P, T> {
}

// Upgrade the provider.
let Some(provider) = self.provider.upgrade() else {
debug!("provider dropped");
let Some(client) = self.client.upgrade() else {
debug!("client dropped");
break 'task;
};

Expand All @@ -88,7 +85,7 @@ impl<P, T: Transport + Clone> ChainStreamPoller<P, T> {
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.into(), false).await {
let block = match client.prepare("eth_getBlockByNumber", (U64::from(number), false)).await {
Ok(Some(block)) => block,
Err(RpcError::Transport(err)) if retries > 0 && err.recoverable() => {
debug!(number, %err, "failed to fetch block, retrying");
Expand Down
45 changes: 16 additions & 29 deletions crates/provider/src/heart.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Block heartbeat and pending transaction watcher.

use crate::Provider;
use crate::{Provider, RootProvider};
use alloy_network::Network;
use alloy_primitives::{B256, U256};
use alloy_rpc_types::Block;
Expand All @@ -10,7 +10,6 @@ use std::{
collections::{BTreeMap, HashMap},
fmt,
future::Future,
marker::PhantomData,
time::{Duration, Instant},
};
use tokio::{
Expand All @@ -31,7 +30,6 @@ use tokio::{
/// .await?
/// .with_confirmations(2)
/// .with_timeout(Some(std::time::Duration::from_secs(60)));
/// # let builder = builder.with_provider(&provider); // TODO
/// // Register the pending transaction with the provider.
/// let pending_transaction = builder.register().await?;
/// // Wait for the transaction to be confirmed 2 times.
Expand All @@ -47,29 +45,30 @@ use tokio::{
/// .await?
/// .with_confirmations(2)
/// .with_timeout(Some(std::time::Duration::from_secs(60)))
/// # .with_provider(&provider) // TODO
/// .watch()
/// .await?;
/// # Ok(())
/// # }
/// ```
#[must_use = "this type does nothing unless you call `register`, `watch` or `get_receipt`"]
#[derive(Debug)]
pub struct PendingTransactionBuilder<N, T, P> {
pub struct PendingTransactionBuilder<'a, N, T> {
config: PendingTransactionConfig,
provider: P,
_phantom: PhantomData<(N, T)>,
provider: &'a RootProvider<N, T>,
}

impl<N: Network, T: Transport + Clone, P: Provider<N, T>> PendingTransactionBuilder<N, T, P> {
impl<'a, N: Network, T: Transport + Clone> PendingTransactionBuilder<'a, N, T> {
/// Creates a new pending transaction builder.
pub const fn new(provider: P, tx_hash: B256) -> Self {
pub const fn new(provider: &'a RootProvider<N, T>, tx_hash: B256) -> Self {
Self::from_config(provider, PendingTransactionConfig::new(tx_hash))
}

/// Creates a new pending transaction builder from the given configuration.
pub const fn from_config(provider: P, inner: PendingTransactionConfig) -> Self {
Self { config: inner, provider, _phantom: PhantomData }
pub const fn from_config(
provider: &'a RootProvider<N, T>,
config: PendingTransactionConfig,
) -> Self {
Self { config, provider }
}

/// Returns the inner configuration.
Expand All @@ -83,12 +82,12 @@ impl<N: Network, T: Transport + Clone, P: Provider<N, T>> PendingTransactionBuil
}

/// Returns the provider.
pub const fn provider(&self) -> &P {
&self.provider
pub const fn provider(&self) -> &'a RootProvider<N, T> {
self.provider
}

/// Consumes this builder, returning the provider and the configuration.
pub fn split(self) -> (P, PendingTransactionConfig) {
pub fn split(self) -> (&'a RootProvider<N, T>, PendingTransactionConfig) {
(self.provider, self.config)
}

Expand Down Expand Up @@ -179,18 +178,6 @@ impl<N: Network, T: Transport + Clone, P: Provider<N, T>> PendingTransactionBuil
}
}

impl<N, T, P: Clone> PendingTransactionBuilder<N, T, &P> {
/// Clones the provider and returns a new pending transaction configuration with the cloned
/// provider.
pub fn with_cloned_provider(self) -> PendingTransactionBuilder<N, T, P> {
PendingTransactionBuilder {
config: self.config,
provider: self.provider.clone(),
_phantom: PhantomData,
}
}
}

/// Configuration for watching a pending transaction.
///
/// This type can be used to create a [`PendingTransactionBuilder`], but in general it is only used
Expand Down Expand Up @@ -264,10 +251,10 @@ impl PendingTransactionConfig {
}

/// Wraps this configuration with a provider to expose watching methods.
pub const fn with_provider<N: Network, T: Transport + Clone, P: Provider<N, T>>(
pub const fn with_provider<N: Network, T: Transport + Clone>(
self,
provider: P,
) -> PendingTransactionBuilder<N, T, P> {
provider: &RootProvider<N, T>,
) -> PendingTransactionBuilder<'_, N, T> {
PendingTransactionBuilder::from_config(provider, self)
}
}
Expand Down
81 changes: 41 additions & 40 deletions crates/provider/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
chain::ChainStreamPoller,
heart::{Heartbeat, HeartbeatHandle, PendingTransaction, PendingTransactionConfig},
utils::{self, EstimatorFunction},
PendingTransactionBuilder,
};
use alloy_json_rpc::{RpcParam, RpcReturn};
use alloy_network::{Network, TransactionBuilder};
Expand Down Expand Up @@ -103,11 +104,11 @@ impl<N, T> RootProviderInner<N, T> {
Self { client, heart: OnceLock::new(), _network: PhantomData }
}

fn weak_client(&self) -> WeakClient<T> {
pub(crate) fn weak_client(&self) -> WeakClient<T> {
self.client.get_weak()
}

fn client_ref(&self) -> ClientRef<'_, T> {
pub(crate) fn client_ref(&self) -> ClientRef<'_, T> {
self.client.get_ref()
}
}
Expand All @@ -126,19 +127,31 @@ impl<N, T: Transport + Clone> RootProviderInner<N, T> {
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[auto_impl::auto_impl(&, &mut, Rc, Arc, Box)]
pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync {
/// Returns the root provider.
fn root(&self) -> &RootProvider<N, T>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the downside of this? when would you use a custom provider as your root provider?

if we go with this, we should update the ProviderBuilder to not accept anything other than RootProvider as the final provider (i.e. prob just remove ProviderBuilder::provider)


/// Returns the RPC client used to send requests.
fn client(&self) -> ClientRef<'_, T>;
#[inline]
fn client(&self) -> ClientRef<'_, T> {
self.root().client()
}

/// Returns a [`Weak`] RPC client used to send requests.
fn weak_client(&self) -> WeakClient<T>;
#[inline]
fn weak_client(&self) -> WeakClient<T> {
self.root().weak_client()
}

/// Watch for the confirmation of a single pending transaction with the given configuration.
///
/// Note that this is handled internally rather than calling any specific RPC method.
#[inline]
async fn watch_pending_transaction(
&self,
config: PendingTransactionConfig,
) -> TransportResult<PendingTransaction>;
) -> TransportResult<PendingTransaction> {
self.root().watch_pending_transaction(config).await
}

/// Watch for new blocks by polling the provider with
/// [`eth_getFilterChanges`](Self::get_filter_changes).
Expand Down Expand Up @@ -339,7 +352,6 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync
/// .await?
/// .with_confirmations(2)
/// .with_timeout(Some(std::time::Duration::from_secs(60)))
/// # .with_provider(&provider) // TODO
/// .watch()
/// .await?;
/// # Ok(())
Expand All @@ -348,9 +360,9 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync
async fn send_transaction(
&self,
tx: N::TransactionRequest,
) -> TransportResult<PendingTransactionConfig> {
) -> TransportResult<PendingTransactionBuilder<'_, N, T>> {
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
let tx_hash = self.client().prepare("eth_sendTransaction", (tx,)).await?;
Ok(PendingTransactionConfig::new(tx_hash))
Ok(PendingTransactionBuilder::new(self.root(), tx_hash))
}

/// Broadcasts a raw transaction RLP bytes to the network.
Expand All @@ -359,10 +371,10 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync
async fn send_raw_transaction(
&self,
rlp_bytes: &[u8],
) -> TransportResult<PendingTransactionConfig> {
) -> TransportResult<PendingTransactionBuilder<'_, N, T>> {
let rlp_hex = hex::encode(rlp_bytes);
let tx_hash = self.client().prepare("eth_sendRawTransaction", (rlp_hex,)).await?;
Ok(PendingTransactionConfig::new(tx_hash))
Ok(PendingTransactionBuilder::new(self.root(), tx_hash))
}

/// Gets the balance of the account at the specified tag, which defaults to latest.
Expand Down Expand Up @@ -673,6 +685,11 @@ impl<P, N: Network, T: Transport + Clone> RawProvider<N, T> for P where P: Provi
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl<N: Network, T: Transport + Clone> Provider<N, T> for RootProvider<N, T> {
#[inline]
fn root(&self) -> &RootProvider<N, T> {
self
}

#[inline]
fn client(&self) -> ClientRef<'_, T> {
self.inner.client_ref()
Expand All @@ -692,28 +709,6 @@ impl<N: Network, T: Transport + Clone> Provider<N, T> for RootProvider<N, T> {
}
}

// Internal implementation for [`ChainStreamPoller`].
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl<N: Network, T: Transport + Clone> Provider<N, T> for RootProviderInner<N, T> {
#[inline]
fn client(&self) -> ClientRef<'_, T> {
self.client_ref()
}

#[inline]
fn weak_client(&self) -> WeakClient<T> {
self.weak_client()
}

async fn watch_pending_transaction(
&self,
_config: PendingTransactionConfig,
) -> TransportResult<PendingTransaction> {
unimplemented!()
}
}

#[cfg(test)]
#[allow(clippy::missing_const_for_fn)]
mod tests {
Expand Down Expand Up @@ -765,11 +760,19 @@ mod tests {
fn object_safety_types() {
fn is_provider<N: Network, T: Transport + Clone, P: Provider<N, T>>() {}
fn is_raw_provider<N: Network, T: Transport + Clone, P: RawProvider<N, T>>() {}
fn is_anvil_provider<N: Network, T: Transport + Clone, P: AnvilProvider<N, T>>() {}

is_provider::<_, _, Box<dyn Provider<Ethereum>>>();
is_provider::<_, _, Box<dyn AnvilProvider<Ethereum>>>();
is_provider::<_, _, Box<dyn RawProvider<Ethereum>>>();

is_raw_provider::<_, _, Box<dyn Provider<Ethereum>>>();
is_raw_provider::<_, _, Box<dyn AnvilProvider<Ethereum>>>();
is_raw_provider::<_, _, Box<dyn RawProvider<Ethereum>>>();

is_anvil_provider::<_, _, Box<dyn Provider<Ethereum>>>();
is_anvil_provider::<_, _, Box<dyn AnvilProvider<Ethereum>>>();
is_anvil_provider::<_, _, Box<dyn RawProvider<Ethereum>>>();
}

#[tokio::test]
Expand All @@ -785,16 +788,14 @@ mod tests {
..Default::default()
};

let pending_tx = provider.send_transaction(tx.clone()).await.expect("failed to send tx");
let hash1 = *pending_tx.tx_hash();
let hash2 =
pending_tx.with_provider(&provider).watch().await.expect("failed to await pending tx");
let builder = provider.send_transaction(tx.clone()).await.expect("failed to send tx");
let hash1 = *builder.tx_hash();
let hash2 = builder.watch().await.expect("failed to await pending tx");
assert_eq!(hash1, hash2);

let pending_tx = provider.send_transaction(tx).await.expect("failed to send tx");
let hash1 = *pending_tx.tx_hash();
let hash2 = pending_tx
.with_provider(provider)
let builder = provider.send_transaction(tx).await.expect("failed to send tx");
let hash1 = *builder.tx_hash();
let hash2 = builder
.get_receipt()
.await
.expect("failed to await pending tx")
Expand Down
Loading
Loading