From 4d3ce3490160500ca93f8ee098b7e94a0a6a26bd Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 3 Jul 2023 14:40:15 +0200 Subject: [PATCH] docs: add payload builder example (#3545) --- crates/payload/builder/src/lib.rs | 82 +++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 3 deletions(-) diff --git a/crates/payload/builder/src/lib.rs b/crates/payload/builder/src/lib.rs index 8bc35c469..fdc57c913 100644 --- a/crates/payload/builder/src/lib.rs +++ b/crates/payload/builder/src/lib.rs @@ -16,13 +16,89 @@ attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables)) ))] -//! This trait implements the [PayloadBuilderService] responsible for managing payload jobs. -//! -//! It Defines the abstractions to create and update payloads: +//! This crate defines the abstractions to create and update payloads: //! - [PayloadJobGenerator]: a type that knows how to create new jobs for creating payloads based //! on [PayloadAttributes](reth_rpc_types::engine::PayloadAttributes). //! - [PayloadJob]: a type that can yields (better) payloads over time. //! +//! This crate comes with the generic [PayloadBuilderService] responsible for managing payload jobs. +//! +//! ## Node integration +//! +//! In a standard node the [PayloadBuilderService] sits downstream of the engine API or rather the +//! component that handles requests from the Beacon Node like `engine_forkchoiceUpdatedV1`. +//! Payload building is enabled if the forkchoice update request contains payload attributes. +//! See also +//! If the forkchoice update request is VALID and contains payload attributes the +//! [PayloadBuilderService] will create a new [PayloadJob] via the [PayloadJobGenerator] and start +//! polling it until the payload is requested by the CL and the payload job is resolved: +//! [PayloadJob::resolve] +//! +//! ## Example +//! +//! A simple example of a [PayloadJobGenerator] that creates empty blocks: +//! +//! ``` +//! use std::future::Future; +//! use std::pin::Pin; +//! use std::sync::Arc; +//! use std::task::{Context, Poll}; +//! use reth_payload_builder::{BuiltPayload, KeepPayloadJobAlive, PayloadBuilderAttributes, PayloadJob, PayloadJobGenerator}; +//! use reth_payload_builder::error::PayloadBuilderError; +//! use reth_primitives::{Block, Header, U256}; +//! +//! /// The generator type that creates new jobs that builds empty blocks. +//! pub struct EmptyBlockPayloadJobGenerator; +//! +//! impl PayloadJobGenerator for EmptyBlockPayloadJobGenerator { +//! type Job = EmptyBlockPayloadJob; +//! +//! /// This is invoked when the node receives payload attributes from the beacon node via `engine_forkchoiceUpdatedV1` +//! fn new_payload_job(&self, attr: PayloadBuilderAttributes) -> Result { +//! Ok(EmptyBlockPayloadJob{ attributes: attr,}) +//! } +//! +//! } +//! +//! /// A [PayloadJob] that builds empty blocks. +//! pub struct EmptyBlockPayloadJob { +//! attributes: PayloadBuilderAttributes, +//! } +//! +//! impl PayloadJob for EmptyBlockPayloadJob { +//! type ResolvePayloadFuture = futures_util::future::Ready, PayloadBuilderError>>; +//! +//! fn best_payload(&self) -> Result, PayloadBuilderError> { +//! // NOTE: some fields are omitted here for brevity +//! let payload = Block { +//! header: Header { +//! parent_hash: self.attributes.parent, +//! timestamp: self.attributes.timestamp, +//! beneficiary: self.attributes.suggested_fee_recipient, +//! ..Default::default() +//! }, +//! ..Default::default() +//! }; +//! let payload = BuiltPayload::new(self.attributes.id, payload.seal_slow(), U256::ZERO); +//! Ok(Arc::new(payload)) +//! } +//! +//! fn resolve(&mut self) -> (Self::ResolvePayloadFuture, KeepPayloadJobAlive) { +//! let payload = self.best_payload(); +//! (futures_util::future::ready(payload), KeepPayloadJobAlive::No) +//! } +//! } +//! +//! /// A [PayloadJob] is a a future that's being polled by the `PayloadBuilderService` +//! impl Future for EmptyBlockPayloadJob { +//! type Output = Result<(), PayloadBuilderError>; +//! +//! fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { +//! Poll::Pending +//! } +//! } +//! ``` +//! //! ## Feature Flags //! //! - `test-utils`: Export utilities for testing