Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions crates/op-rbuilder/src/tests/framework/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,66 @@ impl<P: Protocol> ChainDriverExt for ChainDriver<P> {
}
}

/// Result of builder transaction validation in a block.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BuilderTxInfo {
/// Number of builder transactions found in the block.
pub count: usize,
/// Indices of builder transactions within the block.
pub indices: Vec<usize>,
}

impl BuilderTxInfo {
/// Returns true if the block contains at least one builder transaction.
pub fn has_builder_tx(&self) -> bool {
self.count > 0
}
}

pub trait BlockTransactionsExt {
fn includes(&self, txs: &impl AsTxs) -> bool;
}

/// Extension trait for validating builder transactions in blocks.
pub trait BuilderTxValidation {
/// Checks if the block contains builder transactions from the configured builder address.
/// Returns information about builder transactions found in the block.
fn find_builder_txs(&self) -> BuilderTxInfo;

/// Returns true if the block contains at least one builder transaction.
fn has_builder_tx(&self) -> bool {
self.find_builder_txs().has_builder_tx()
}

/// Asserts that the block contains exactly the expected number of builder transactions.
fn assert_builder_tx_count(&self, expected: usize) {
let info = self.find_builder_txs();
assert_eq!(
info.count, expected,
"Expected {} builder transaction(s), found {} at indices {:?}",
expected, info.count, info.indices
);
}
}

impl BuilderTxValidation for Block<Transaction> {
fn find_builder_txs(&self) -> BuilderTxInfo {
let builder_address = builder_signer().address;
let mut indices = Vec::new();

for (idx, tx) in self.transactions.txns().enumerate() {
if tx.from() == builder_address {
indices.push(idx);
}
}

BuilderTxInfo {
count: indices.len(),
indices,
}
}
}

impl BlockTransactionsExt for Block<Transaction> {
fn includes(&self, txs: &impl AsTxs) -> bool {
txs.as_txs()
Expand Down
33 changes: 32 additions & 1 deletion crates/op-rbuilder/src/tests/smoke.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
args::OpRbuilderArgs,
tests::{LocalInstance, TransactionBuilderExt},
tests::{BuilderTxValidation, LocalInstance, TransactionBuilderExt},
};
use alloy_primitives::TxHash;

Expand Down Expand Up @@ -296,3 +296,34 @@ async fn chain_produces_big_tx_without_gas_limit(rbuilder: LocalInstance) -> eyr

Ok(())
}

/// Validates that each block contains builder transactions using the
/// BuilderTxValidation utility.
#[rb_test]
async fn block_includes_builder_transaction(rbuilder: LocalInstance) -> eyre::Result<()> {
let driver = rbuilder.driver().await?;

const SAMPLE_SIZE: usize = 5;

for _ in 0..SAMPLE_SIZE {
let block = driver.build_new_block_with_current_timestamp(None).await?;

// Validate that the block contains builder transactions
assert!(
block.has_builder_tx(),
"Block should contain at least one builder transaction"
);

// Standard builder: 1 builder tx
// Flashblocks builder: 2 builder txs (fallback + flashblock number)
if_standard! {
block.assert_builder_tx_count(1);
}

if_flashblocks! {
block.assert_builder_tx_count(2);
}
}

Ok(())
}
Loading