Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 33 additions & 29 deletions crates/op-rbuilder/src/builders/builder_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloy_consensus::TxEip1559;
use alloy_eips::{Encodable2718, eip7623::TOTAL_COST_FLOOR_PER_TOKEN};
use alloy_evm::Database;
use alloy_primitives::{
Address, TxKind,
Address, TxKind, U256,
map::foldhash::{HashSet, HashSetExt},
};
use core::fmt::Debug;
Expand Down Expand Up @@ -30,7 +30,10 @@ use crate::{
pub struct BuilderTransactionCtx {
pub gas_used: u64,
pub da_size: u64,
pub signed_tx: Option<Recovered<OpTransactionSigned>>,
pub signed_tx: Recovered<OpTransactionSigned>,
// whether the transaction should be a top of block or
// bottom of block transaction
pub is_top_of_block: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to switch to bottom of every flashblock how difficult will it be to change this? Not sure if we want something like this configurable or not but it might be easier to start that way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in construction of each transaction added to builder_tx just flip the boolean

Copy link
Contributor

Choose a reason for hiding this comment

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

How do you flip it? Is it a code change? Then it needs to be built and released and it's a lot more difficult to change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh you mean configuring via cli / env? I'm not sure this is something we want to change very often since changes to the builder tx position breaks external integrations / observability. Also the same builder tx have different positions in the block depending on whether the flashblock contract call is enabled or not etc. and the complexity would easily cause misconfiguration.

}

/// Possible error variants during construction of builder txs.
Expand Down Expand Up @@ -80,7 +83,6 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
info: &mut ExecutionInfo<Extra>,
ctx: &OpPayloadBuilderCtx<ExtraCtx>,
db: &mut State<impl Database>,
top_of_block: bool,
) -> Result<Vec<BuilderTransactionCtx>, BuilderTransactionError>;

fn add_builder_txs<Extra: Debug + Default>(
Expand All @@ -98,30 +100,26 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {

let mut invalid: HashSet<Address> = HashSet::new();

let builder_txs = self.simulate_builder_txs(
state_provider,
info,
builder_ctx,
evm.db_mut(),
top_of_block,
)?;
let builder_txs =
self.simulate_builder_txs(state_provider, info, builder_ctx, evm.db_mut())?;
for builder_tx in builder_txs.iter() {
let signed_tx = match builder_tx.signed_tx.clone() {
Some(tx) => tx,
None => continue,
};
if invalid.contains(&signed_tx.signer()) {
warn!(target: "payload_builder", tx_hash = ?signed_tx.tx_hash(), "builder signer invalid as previous builder tx reverted");
if builder_tx.is_top_of_block != top_of_block {
// don't commit tx if the buidler tx is not being added in the intended
// position in the block
continue;
}
if invalid.contains(&builder_tx.signed_tx.signer()) {
warn!(target: "payload_builder", tx_hash = ?builder_tx.signed_tx.tx_hash(), "builder signer invalid as previous builder tx reverted");
continue;
}

let ResultAndState { result, state } = evm
.transact(&signed_tx)
.transact(&builder_tx.signed_tx)
.map_err(|err| BuilderTransactionError::EvmExecutionError(Box::new(err)))?;

if !result.is_success() {
warn!(target: "payload_builder", tx_hash = ?signed_tx.tx_hash(), "builder tx reverted");
invalid.insert(signed_tx.signer());
warn!(target: "payload_builder", tx_hash = ?builder_tx.signed_tx.tx_hash(), "builder tx reverted");
invalid.insert(builder_tx.signed_tx.signer());
continue;
}

Expand All @@ -130,7 +128,7 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
info.cumulative_gas_used += gas_used;

let ctx = ReceiptBuilderCtx {
tx: signed_tx.inner(),
tx: builder_tx.signed_tx.inner(),
evm: &evm,
result,
state: &state,
Expand All @@ -142,9 +140,9 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
evm.db_mut().commit(state);

// Append sender and transaction to the respective lists
info.executed_senders.push(signed_tx.signer());
info.executed_senders.push(builder_tx.signed_tx.signer());
info.executed_transactions
.push(signed_tx.clone().into_inner());
.push(builder_tx.signed_tx.clone().into_inner());
}

// Release the db reference by dropping evm
Expand Down Expand Up @@ -172,12 +170,8 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
.evm_with_env(&mut simulation_state, ctx.evm_env.clone());

for builder_tx in builder_txs {
let signed_tx = match builder_tx.signed_tx.clone() {
Some(tx) => tx,
None => continue,
};
let ResultAndState { state, .. } = evm
.transact(&signed_tx)
.transact(&builder_tx.signed_tx)
.map_err(|err| BuilderTransactionError::EvmExecutionError(Box::new(err)))?;

evm.db_mut().commit(state);
Expand Down Expand Up @@ -214,7 +208,8 @@ impl BuilderTxBase {
Ok(Some(BuilderTransactionCtx {
gas_used,
da_size,
signed_tx: Some(signed_tx),
signed_tx,
is_top_of_block: false,
}))
}
None => Ok(None),
Expand Down Expand Up @@ -276,11 +271,20 @@ impl BuilderTxBase {
}
}

pub(super) fn get_nonce(
pub(crate) fn get_nonce(
db: &mut State<impl Database>,
address: Address,
) -> Result<u64, BuilderTransactionError> {
db.load_cache_account(address)
.map(|acc| acc.account_info().unwrap_or_default().nonce)
.map_err(|_| BuilderTransactionError::AccountLoadFailed(address))
}

pub(crate) fn get_balance(
db: &mut State<impl Database>,
address: Address,
) -> Result<U256, BuilderTransactionError> {
db.load_cache_account(address)
.map(|acc| acc.account_info().unwrap_or_default().balance)
.map_err(|_| BuilderTransactionError::AccountLoadFailed(address))
}
46 changes: 28 additions & 18 deletions crates/op-rbuilder/src/builders/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloy_consensus::{Eip658Value, Transaction, conditional::BlockConditionalAtt
use alloy_eips::Typed2718;
use alloy_evm::Database;
use alloy_op_evm::block::receipt_builder::OpReceiptBuilder;
use alloy_primitives::{Bytes, U256};
use alloy_primitives::{BlockHash, Bytes, U256};
use alloy_rpc_types_eth::Withdrawals;
use core::fmt::Debug;
use op_alloy_consensus::OpDepositReceipt;
Expand Down Expand Up @@ -75,41 +75,51 @@ pub struct OpPayloadBuilderCtx<ExtraCtx: Debug + Default = ()> {

impl<ExtraCtx: Debug + Default> OpPayloadBuilderCtx<ExtraCtx> {
/// Returns the parent block the payload will be build on.
pub(super) fn parent(&self) -> &SealedHeader {
pub(crate) fn parent(&self) -> &SealedHeader {
&self.config.parent_header
}

/// Returns the parent hash
pub(crate) fn parent_hash(&self) -> BlockHash {
self.parent().hash()
}

/// Returns the timestamp
pub(crate) fn timestamp(&self) -> u64 {
self.attributes().timestamp()
}

/// Returns the builder attributes.
pub(super) const fn attributes(&self) -> &OpPayloadBuilderAttributes<OpTransactionSigned> {
pub(crate) const fn attributes(&self) -> &OpPayloadBuilderAttributes<OpTransactionSigned> {
&self.config.attributes
}

/// Returns the withdrawals if shanghai is active.
pub(super) fn withdrawals(&self) -> Option<&Withdrawals> {
pub(crate) fn withdrawals(&self) -> Option<&Withdrawals> {
self.chain_spec
.is_shanghai_active_at_timestamp(self.attributes().timestamp())
.then(|| &self.attributes().payload_attributes.withdrawals)
}

/// Returns the block gas limit to target.
pub(super) fn block_gas_limit(&self) -> u64 {
pub(crate) fn block_gas_limit(&self) -> u64 {
self.attributes()
.gas_limit
.unwrap_or(self.evm_env.block_env.gas_limit)
}

/// Returns the block number for the block.
pub(super) fn block_number(&self) -> u64 {
pub(crate) fn block_number(&self) -> u64 {
as_u64_saturated!(self.evm_env.block_env.number)
}

/// Returns the current base fee
pub(super) fn base_fee(&self) -> u64 {
pub(crate) fn base_fee(&self) -> u64 {
self.evm_env.block_env.basefee
}

/// Returns the current blob gas price.
pub(super) fn get_blob_gasprice(&self) -> Option<u64> {
pub(crate) fn get_blob_gasprice(&self) -> Option<u64> {
self.evm_env
.block_env
.blob_gasprice()
Expand All @@ -119,7 +129,7 @@ impl<ExtraCtx: Debug + Default> OpPayloadBuilderCtx<ExtraCtx> {
/// Returns the blob fields for the header.
///
/// This will always return `Some(0)` after ecotone.
pub(super) fn blob_fields(&self) -> (Option<u64>, Option<u64>) {
pub(crate) fn blob_fields(&self) -> (Option<u64>, Option<u64>) {
// OP doesn't support blobs/EIP-4844.
// https://specs.optimism.io/protocol/exec-engine.html#ecotone-disable-blob-transactions
// Need [Some] or [None] based on hardfork to match block hash.
Expand All @@ -133,7 +143,7 @@ impl<ExtraCtx: Debug + Default> OpPayloadBuilderCtx<ExtraCtx> {
/// Returns the extra data for the block.
///
/// After holocene this extracts the extradata from the paylpad
pub(super) fn extra_data(&self) -> Result<Bytes, PayloadBuilderError> {
pub(crate) fn extra_data(&self) -> Result<Bytes, PayloadBuilderError> {
if self.is_holocene_active() {
self.attributes()
.get_holocene_extra_data(
Expand All @@ -148,47 +158,47 @@ impl<ExtraCtx: Debug + Default> OpPayloadBuilderCtx<ExtraCtx> {
}

/// Returns the current fee settings for transactions from the mempool
pub(super) fn best_transaction_attributes(&self) -> BestTransactionsAttributes {
pub(crate) fn best_transaction_attributes(&self) -> BestTransactionsAttributes {
BestTransactionsAttributes::new(self.base_fee(), self.get_blob_gasprice())
}

/// Returns the unique id for this payload job.
pub(super) fn payload_id(&self) -> PayloadId {
pub(crate) fn payload_id(&self) -> PayloadId {
self.attributes().payload_id()
}

/// Returns true if regolith is active for the payload.
pub(super) fn is_regolith_active(&self) -> bool {
pub(crate) fn is_regolith_active(&self) -> bool {
self.chain_spec
.is_regolith_active_at_timestamp(self.attributes().timestamp())
}

/// Returns true if ecotone is active for the payload.
pub(super) fn is_ecotone_active(&self) -> bool {
pub(crate) fn is_ecotone_active(&self) -> bool {
self.chain_spec
.is_ecotone_active_at_timestamp(self.attributes().timestamp())
}

/// Returns true if canyon is active for the payload.
pub(super) fn is_canyon_active(&self) -> bool {
pub(crate) fn is_canyon_active(&self) -> bool {
self.chain_spec
.is_canyon_active_at_timestamp(self.attributes().timestamp())
}

/// Returns true if holocene is active for the payload.
pub(super) fn is_holocene_active(&self) -> bool {
pub(crate) fn is_holocene_active(&self) -> bool {
self.chain_spec
.is_holocene_active_at_timestamp(self.attributes().timestamp())
}

/// Returns true if isthmus is active for the payload.
pub(super) fn is_isthmus_active(&self) -> bool {
pub(crate) fn is_isthmus_active(&self) -> bool {
self.chain_spec
.is_isthmus_active_at_timestamp(self.attributes().timestamp())
}

/// Returns the chain id
pub(super) fn chain_id(&self) -> u64 {
pub(crate) fn chain_id(&self) -> u64 {
self.chain_spec.chain_id()
}
}
Expand Down
Loading
Loading