reth_e2e_test_utils/
payload.rsuse futures_util::StreamExt;
use reth_payload_builder::{PayloadBuilderHandle, PayloadId};
use reth_payload_builder_primitives::{Events, PayloadBuilder};
use reth_payload_primitives::{BuiltPayload, PayloadBuilderAttributes, PayloadTypes};
use tokio_stream::wrappers::BroadcastStream;
#[derive(derive_more::Debug)]
pub struct PayloadTestContext<T: PayloadTypes> {
pub payload_event_stream: BroadcastStream<Events<T>>,
payload_builder: PayloadBuilderHandle<T>,
pub timestamp: u64,
#[debug(skip)]
attributes_generator: Box<dyn Fn(u64) -> T::PayloadBuilderAttributes>,
}
impl<T: PayloadTypes> PayloadTestContext<T> {
pub async fn new(
payload_builder: PayloadBuilderHandle<T>,
attributes_generator: impl Fn(u64) -> T::PayloadBuilderAttributes + 'static,
) -> eyre::Result<Self> {
let payload_events = payload_builder.subscribe().await?;
let payload_event_stream = payload_events.into_stream();
Ok(Self {
payload_event_stream,
payload_builder,
timestamp: 1710338135,
attributes_generator: Box::new(attributes_generator),
})
}
pub async fn new_payload(&mut self) -> eyre::Result<T::PayloadBuilderAttributes> {
self.timestamp += 1;
let attributes = (self.attributes_generator)(self.timestamp);
self.payload_builder.send_new_payload(attributes.clone()).await.unwrap()?;
Ok(attributes)
}
pub async fn expect_attr_event(
&mut self,
attrs: T::PayloadBuilderAttributes,
) -> eyre::Result<()> {
let first_event = self.payload_event_stream.next().await.unwrap()?;
if let Events::Attributes(attr) = first_event {
assert_eq!(attrs.timestamp(), attr.timestamp());
} else {
panic!("Expect first event as payload attributes.")
}
Ok(())
}
pub async fn wait_for_built_payload(&self, payload_id: PayloadId) {
loop {
let payload = self.payload_builder.best_payload(payload_id).await.unwrap().unwrap();
if payload.block().body.transactions.is_empty() {
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
continue
}
break
}
}
pub async fn expect_built_payload(&mut self) -> eyre::Result<T::BuiltPayload> {
let second_event = self.payload_event_stream.next().await.unwrap()?;
if let Events::BuiltPayload(payload) = second_event {
Ok(payload)
} else {
panic!("Expect a built payload event.");
}
}
}