Skip to content

Commit

Permalink
Move estimate_gas_for_builder_tx and signed_builder_tx to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
SozinM committed Feb 24, 2025
1 parent 9ed4f94 commit 0d8ab7c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 61 deletions.
67 changes: 7 additions & 60 deletions crates/op-rbuilder/src/payload_builder_vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ use crate::generator::BuildArguments;
use crate::{
generator::{BlockCell, PayloadBuilder},
metrics::OpRBuilderMetrics,
primitives::{ExecutedPayload, ExecutionInfo, PayloadBuilderService},
primitives::{
estimate_gas_for_builder_tx, signed_builder_tx, ExecutedPayload, ExecutionInfo,
PayloadBuilderService,
},
tx_signer::Signer,
};
use alloy_consensus::constants::EMPTY_WITHDRAWALS;
use alloy_consensus::transaction::Recovered;
use alloy_consensus::{
Eip658Value, Header, Transaction, TxEip1559, Typed2718, EMPTY_OMMER_ROOT_HASH,
Eip658Value, Header, Transaction, Typed2718, EMPTY_OMMER_ROOT_HASH,
};
use alloy_eips::merge::BEACON_NONCE;
use alloy_primitives::private::alloy_rlp::Encodable;
use alloy_primitives::{Address, Bytes, TxKind, U256};
use alloy_primitives::{Bytes, U256};
use alloy_rpc_types_engine::PayloadId;
use alloy_rpc_types_eth::Withdrawals;
use op_alloy_consensus::{OpDepositReceipt, OpTypedTransaction};
use op_alloy_consensus::OpDepositReceipt;
use reth::builder::{components::PayloadServiceBuilder, node::FullNodeTypes, BuilderContext};
use reth::core::primitives::InMemorySize;
use reth::payload::PayloadBuilderHandle;
Expand Down Expand Up @@ -1232,58 +1234,3 @@ where
})
}
}

/// Creates signed builder tx to Address::ZERO and specified message as input
pub fn signed_builder_tx<DB>(
db: &mut State<DB>,
builder_tx_gas: u64,
message: Vec<u8>,
signer: Signer,
base_fee: u64,
chain_id: u64,
) -> Result<Recovered<OpTransactionSigned>, PayloadBuilderError>
where
DB: Database<Error = ProviderError>,
{
// Create message with block number for the builder to sign
let nonce = db
.load_cache_account(signer.address)
.map(|acc| acc.account_info().unwrap_or_default().nonce)
.map_err(|_| {
PayloadBuilderError::other(OpPayloadBuilderError::AccountLoadFailed(signer.address))
})?;

// Create the EIP-1559 transaction
let tx = OpTypedTransaction::Eip1559(TxEip1559 {
chain_id,
nonce,
gas_limit: builder_tx_gas,
max_fee_per_gas: base_fee.into(),
max_priority_fee_per_gas: 0,
to: TxKind::Call(Address::ZERO),
// Include the message as part of the transaction data
input: message.into(),
..Default::default()
});
// Sign the transaction
let builder_tx = signer.sign_tx(tx).map_err(PayloadBuilderError::other)?;

Ok(builder_tx)
}

fn estimate_gas_for_builder_tx(input: Vec<u8>) -> u64 {
// Count zero and non-zero bytes
let (zero_bytes, nonzero_bytes) = input.iter().fold((0, 0), |(zeros, nonzeros), &byte| {
if byte == 0 {
(zeros + 1, nonzeros)
} else {
(zeros, nonzeros + 1)
}
});

// Calculate gas cost (4 gas per zero byte, 16 gas per non-zero byte)
let zero_cost = zero_bytes * 4;
let nonzero_cost = nonzero_bytes * 16;

zero_cost + nonzero_cost + 21_000
}
64 changes: 64 additions & 0 deletions crates/op-rbuilder/src/primitives/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::tx_signer::Signer;
use alloy_consensus::transaction::Recovered;
use alloy_consensus::TxEip1559;
use alloy_primitives::{Address, TxKind};
use op_alloy_consensus::OpTypedTransaction;
use reth_evm::Database;
use reth_optimism_payload_builder::error::OpPayloadBuilderError;
use reth_optimism_primitives::OpTransactionSigned;
use reth_payload_primitives::PayloadBuilderError;
use reth_provider::ProviderError;
use revm::db::State;
pub fn estimate_gas_for_builder_tx(input: Vec<u8>) -> u64 {
// Count zero and non-zero bytes
let (zero_bytes, nonzero_bytes) = input.iter().fold((0, 0), |(zeros, nonzeros), &byte| {
if byte == 0 {
(zeros + 1, nonzeros)
} else {
(zeros, nonzeros + 1)
}
});

// Calculate gas cost (4 gas per zero byte, 16 gas per non-zero byte)
let zero_cost = zero_bytes * 4;
let nonzero_cost = nonzero_bytes * 16;

zero_cost + nonzero_cost + 21_000
}
/// Creates signed builder tx to Address::ZERO and specified message as input
pub fn signed_builder_tx<DB>(
db: &mut State<DB>,
builder_tx_gas: u64,
message: Vec<u8>,
signer: Signer,
base_fee: u64,
chain_id: u64,
) -> Result<Recovered<OpTransactionSigned>, PayloadBuilderError>
where
DB: Database<Error = ProviderError>,
{
// Create message with block number for the builder to sign
let nonce = db
.load_cache_account(signer.address)
.map(|acc| acc.account_info().unwrap_or_default().nonce)
.map_err(|_| {
PayloadBuilderError::other(OpPayloadBuilderError::AccountLoadFailed(signer.address))
})?;

// Create the EIP-1559 transaction
let tx = OpTypedTransaction::Eip1559(TxEip1559 {
chain_id,
nonce,
gas_limit: builder_tx_gas,
max_fee_per_gas: base_fee.into(),
max_priority_fee_per_gas: 0,
to: TxKind::Call(Address::ZERO),
// Include the message as part of the transaction data
input: message.into(),
..Default::default()
});
// Sign the transaction
let builder_tx = signer.sign_tx(tx).map_err(PayloadBuilderError::other)?;

Ok(builder_tx)
}
3 changes: 3 additions & 0 deletions crates/op-rbuilder/src/primitives/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! This module contains types from the reth that weren't heavily modified
mod execution;
mod helpers;
mod payload_builder_service;

pub use payload_builder_service::PayloadBuilderService;

pub use helpers::{estimate_gas_for_builder_tx, signed_builder_tx};

pub use execution::{ExecutedPayload, ExecutionInfo};
1 change: 0 additions & 1 deletion crates/op-rbuilder/src/tester/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use reth_node_api::{EngineTypes, PayloadTypes};
use reth_optimism_node::OpEngineTypes;
use reth_payload_builder::PayloadId;
use reth_rpc_layer::{AuthClientLayer, AuthClientService, JwtSecret};
use serde_json;
use serde_json::Value;
use std::str::FromStr;
use std::time::SystemTime;
Expand Down

0 comments on commit 0d8ab7c

Please sign in to comment.