-
Notifications
You must be signed in to change notification settings - Fork 443
feature: stream finalized blocks using slot-based polling and caching #2665
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
base: main
Are you sure you want to change the base?
feature: stream finalized blocks using slot-based polling and caching #2665
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend handrolling a stream impl for this, this is a lot cleaner than doing streams in async code
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Hey @ecol-master, just wrapped up all the stuff you mentioned. Wanna take another look? |
Hi, great job, thank you for your work! I’ll take a look at the code and get back to you soon. |
/// A stream of finalized blocks that yields both the latest block header and finalized block. | ||
#[cfg(feature = "pubsub")] | ||
pub struct FinalizedBlocksStream<N: alloy_network::Network> { | ||
inner: Pin<Box<dyn Stream<Item = N::HeaderResponse> + Send>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we create Pin<Box<dyn Stream>>
instead of inner: SubscriptionStream<N::HeaderResponse>
?
SubscriptionStream
from alloy_pubsub
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use Pin<Box<dyn Stream<...>>>
instead of SubscriptionStream
to abstract away the concrete type, allow internal flexibility (e.g. swapping the stream implementation later), and because storing a stream in a struct requires a type with a known size (Sized), which SubscriptionStream
doesn’t guarantee as it's a generic alias.
don't forget this :( @mattsse |
Closes #2640
PR Summary: Stream finalized blocks using slot-based polling and caching
This PR introduces
FinalizedBlocksStream
, a helper stream that emits the latest head along with the most recent finalized block, usingeth_getBlockByNumber("finalized")
.Key features:
This provides a lightweight alternative to beacon node tracking, suitable for clients without finality sync support.
Implementation Details
This pull request introduces a new feature for subscribing to finalized blocks and refactors some existing code in the
provider
module. The primary changes include the addition ofSubFinalizedBlocks
andFinalizedBlocksStream
types, along with their associated methods, and updates to theProvider
trait to support this functionality.New Feature: Finalized Block Subscription
SubFinalizedBlocks
struct and methods: Introduced a builder type for subscribing to finalized blocks, with options to include full transactions or just transaction hashes. This includes methods likenew
,full
,hashes
,channel_size
, andinto_stream
.FinalizedBlocksStream
struct and methods: Added a stream type to handle finalized block updates, including logic for polling and caching finalized blocks. Includes methods likenew
,should_request_finalized_block
, andpoll_next
.Trait Updates
Provider
trait: Added a new methodsubscribe_finalized_blocks
to theProvider
trait, enabling clients to subscribe to finalized blocks using the newSubFinalizedBlocks
type.Refactoring and Imports
get_block.rs
: Adjusted imports to include the newSubFinalizedBlocks
and reorganized existing imports for clarity. [1] [2]trait.rs
: AddedSubFinalizedBlocks
to the list of imports in theprovider
trait file.