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: subscribe to finalized and safe headers #9402

Merged
merged 6 commits into from
Jul 15, 2024

Conversation

greged93
Copy link
Contributor

@greged93 greged93 commented Jul 9, 2024

Resolves #9269.

@greged93 greged93 marked this pull request as draft July 9, 2024 16:40

/// A type that allows to register finalized block related event subscriptions
/// and get notified when a new finalized block header is available.
pub trait FinalizedBlockHeaderSubscription: Send + Sync {
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's combine safe and finalized into one trait

maybe we can come up with a name that isn't also tied to headers, essentially these are derived from forkchoice, so perhaps Forkchoice(Block)Subscriptions

/// and get notified when a new finalized block header is available.
pub trait FinalizedBlockHeaderSubscription: Send + Sync {
/// Get notified when a new finalized block header is available.
fn subscribe_to_finalized_header(&self) -> broadcast::Receiver<SealedHeader>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd like to roll a standalone wrapper type for this, because perhaps we want to put some more features into this,

I know we didn't do this for CanonStateNotifications but we should have

@greged93 greged93 force-pushed the feat/finalize-safe-subscription branch from 7534153 to f13dbbc Compare July 12, 2024 08:04
@greged93 greged93 marked this pull request as ready for review July 12, 2024 08:06
@@ -139,3 +139,44 @@ impl CanonStateNotification {
receipts
}
}

/// Wrapper around a broadcast receiver that receives fork choice notifications.
#[derive(Debug)]
Copy link
Member

Choose a reason for hiding this comment

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

can we please derive derive_more::Deref and derive_more::DerefMut for wrapper types

Comment on lines +174 to +178
Some(Err(err)) => {
debug!(%err, "finalized header notification stream lagging behind");
continue
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Some(Err(err)) => {
debug!(%err, "finalized header notification stream lagging behind");
continue
}
Some(Err(err)) => {
debug!(target: "provider::fcu",
%err,
"finalized header notification stream lagging behind"
);
cx.waker().wake_by_ref();
return Poll::Pending
}

if we don't return the error here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

should I also modify CanonStateNotificationStream?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pushed without for now, if needed I can make a follow up PR

Copy link
Member

Choose a reason for hiding this comment

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

yeah, please modify CanonStateNotificationStream in a new PR, you can ref this comment in the PR description

Copy link
Member

Choose a reason for hiding this comment

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

thanks!

@greged93 greged93 force-pushed the feat/finalize-safe-subscription branch from 951b89a to 278a19f Compare July 14, 2024 17:25
@greged93 greged93 requested a review from gakonst as a code owner July 14, 2024 17:25
@greged93
Copy link
Contributor Author

@mattsse @emhane is it ok to merge this?

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

last nit

Comment on lines 171 to 183
return match ready!(self.as_mut().project().st.poll_next(cx)) {
Some(Ok(notification)) => Poll::Ready(Some(notification)),
Some(Err(err)) => {
debug!(target: "provider::fcu",
%err,
"finalized header notification stream lagging behind"
);

cx.waker().wake_by_ref();
Poll::Pending
}
None => Poll::Ready(None),
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should be in a loop then we don't need the manual waker call

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made this update from a comment from @emhane, afaiu it's better to remove the loop, call the waker and yield to the executor no? can't you otherwise end up with the stream taking up excessive resources if it keeps erroring?

Copy link
Member

Choose a reason for hiding this comment

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

exactly, it can lead to starvation (for no need)

Copy link
Collaborator

Choose a reason for hiding this comment

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

remove the loop, call the waker and yield to the executor no?

this is effectively a loop with extra steps

Comment on lines 171 to 183
return match ready!(self.as_mut().project().st.poll_next(cx)) {
Some(Ok(notification)) => Poll::Ready(Some(notification)),
Some(Err(err)) => {
debug!(target: "provider::fcu",
%err,
"finalized header notification stream lagging behind"
);

cx.waker().wake_by_ref();
Poll::Pending
}
None => Poll::Ready(None),
};
Copy link
Member

Choose a reason for hiding this comment

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

exactly, it can lead to starvation (for no need)

@@ -139,3 +139,47 @@ impl CanonStateNotification {
receipts
}
}

/// Wrapper around a broadcast receiver that receives fork choice notifications.
#[derive(Debug, derive_more::Deref, derive_more::DerefMut)]
Copy link
Member

Choose a reason for hiding this comment

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

Deref and DerefMut can be brought into scope in the prelude

@mattsse mattsse enabled auto-merge July 15, 2024 13:31
@mattsse mattsse added this pull request to the merge queue Jul 15, 2024
Merged via the queue into paradigmxyz:main with commit e140421 Jul 15, 2024
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add trait to subscribe to changed finalized + safe heads
3 participants