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

Move PayloadBuilderHandle methods out to new trait #10956

Closed
Tracked by #10434
emhane opened this issue Sep 17, 2024 · 3 comments · Fixed by #10965
Closed
Tracked by #10434

Move PayloadBuilderHandle methods out to new trait #10956

emhane opened this issue Sep 17, 2024 · 3 comments · Fixed by #10965
Assignees
Labels
A-block-building Related to block building C-debt Refactor of code section that is hard to understand or maintain D-good-first-issue Nice and easy! A great choice to get started

Comments

@emhane
Copy link
Member

emhane commented Sep 17, 2024

Describe the feature

Move PayloadBuilderHandle methods out to new trait

// === impl PayloadBuilderHandle ===
impl<T> PayloadBuilderHandle<T>
where
T: PayloadTypes + 'static,
{
/// Creates a new payload builder handle for the given channel.
///
/// Note: this is only used internally by the [`PayloadBuilderService`] to manage the payload
/// building flow See [`PayloadBuilderService::poll`] for implementation details.
pub const fn new(to_service: mpsc::UnboundedSender<PayloadServiceCommand<T>>) -> Self {
Self { to_service }
}
/// Resolves the payload job and returns the best payload that has been built so far.
///
/// Note: depending on the installed [`PayloadJobGenerator`], this may or may not terminate the
/// job, See [`PayloadJob::resolve`].
async fn resolve(&self, id: PayloadId) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
let (tx, rx) = oneshot::channel();
self.to_service.send(PayloadServiceCommand::Resolve(id, tx)).ok()?;
match rx.await.transpose()? {
Ok(fut) => Some(fut.await),
Err(e) => Some(Err(e.into())),
}
}
/// Sends a message to the service to start building a new payload for the given payload
/// attributes and returns a future that resolves to the payload.
pub async fn send_and_resolve_payload(
&self,
attr: T::PayloadBuilderAttributes,
) -> Result<PayloadFuture<T::BuiltPayload>, PayloadBuilderError> {
let rx = self.send_new_payload(attr);
let id = rx.await??;
let (tx, rx) = oneshot::channel();
let _ = self.to_service.send(PayloadServiceCommand::Resolve(id, tx));
rx.await?.ok_or(PayloadBuilderError::MissingPayload)
}
/// Returns the best payload for the given identifier.
///
/// Note: this does not resolve the job if it's still in progress.
pub async fn best_payload(
&self,
id: PayloadId,
) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
let (tx, rx) = oneshot::channel();
self.to_service.send(PayloadServiceCommand::BestPayload(id, tx)).ok()?;
rx.await.ok()?
}
/// Returns the payload attributes associated with the given identifier.
///
/// Note: this returns the attributes of the payload and does not resolve the job.
async fn payload_attributes(
&self,
id: PayloadId,
) -> Option<Result<T::PayloadBuilderAttributes, PayloadBuilderError>> {
let (tx, rx) = oneshot::channel();
self.to_service.send(PayloadServiceCommand::PayloadAttributes(id, tx)).ok()?;
rx.await.ok()?
}
/// Sends a message to the service to start building a new payload for the given payload.
///
/// This is the same as [`PayloadBuilderHandle::new_payload`] but does not wait for the result
/// and returns the receiver instead
pub fn send_new_payload(
&self,
attr: T::PayloadBuilderAttributes,
) -> oneshot::Receiver<Result<PayloadId, PayloadBuilderError>> {
let (tx, rx) = oneshot::channel();
let _ = self.to_service.send(PayloadServiceCommand::BuildNewPayload(attr, tx));
rx
}
/// Starts building a new payload for the given payload attributes.
///
/// Returns the identifier of the payload.
///
/// Note: if there's already payload in progress with same identifier, it will be returned.
pub async fn new_payload(
&self,
attr: T::PayloadBuilderAttributes,
) -> Result<PayloadId, PayloadBuilderError> {
self.send_new_payload(attr).await?
}
/// Sends a message to the service to subscribe to payload events.
/// Returns a receiver that will receive them.
pub async fn subscribe(&self) -> Result<PayloadEvents<T>, RecvError> {
let (tx, rx) = oneshot::channel();
let _ = self.to_service.send(PayloadServiceCommand::Subscribe(tx));
Ok(PayloadEvents { receiver: rx.await? })
}
}

Additional context

No response

@emhane emhane added C-debt Refactor of code section that is hard to understand or maintain A-block-building Related to block building labels Sep 17, 2024
@emhane emhane added the D-good-first-issue Nice and easy! A great choice to get started label Sep 17, 2024
@greged93
Copy link
Contributor

Is this free for grabs @emhane ?

@varun-doshi
Copy link
Contributor

I'd like to take this up if its okay with @greged93

@greged93
Copy link
Contributor

sorry already working on it :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-block-building Related to block building C-debt Refactor of code section that is hard to understand or maintain D-good-first-issue Nice and easy! A great choice to get started
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants