From 0d8ab7cbf7cf1997ed9b80f456e07553adf254f7 Mon Sep 17 00:00:00 2001 From: Solar Mithril Date: Mon, 24 Feb 2025 16:28:23 +0700 Subject: [PATCH] Move estimate_gas_for_builder_tx and signed_builder_tx to helpers --- .../src/payload_builder_vanilla.rs | 67 ++----------------- crates/op-rbuilder/src/primitives/helpers.rs | 64 ++++++++++++++++++ crates/op-rbuilder/src/primitives/mod.rs | 3 + crates/op-rbuilder/src/tester/mod.rs | 1 - 4 files changed, 74 insertions(+), 61 deletions(-) create mode 100644 crates/op-rbuilder/src/primitives/helpers.rs diff --git a/crates/op-rbuilder/src/payload_builder_vanilla.rs b/crates/op-rbuilder/src/payload_builder_vanilla.rs index 1921bf9a..65b5cc2b 100644 --- a/crates/op-rbuilder/src/payload_builder_vanilla.rs +++ b/crates/op-rbuilder/src/payload_builder_vanilla.rs @@ -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; @@ -1232,58 +1234,3 @@ where }) } } - -/// Creates signed builder tx to Address::ZERO and specified message as input -pub fn signed_builder_tx( - db: &mut State, - builder_tx_gas: u64, - message: Vec, - signer: Signer, - base_fee: u64, - chain_id: u64, -) -> Result, PayloadBuilderError> -where - DB: Database, -{ - // 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) -> 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 -} diff --git a/crates/op-rbuilder/src/primitives/helpers.rs b/crates/op-rbuilder/src/primitives/helpers.rs new file mode 100644 index 00000000..bdac9b51 --- /dev/null +++ b/crates/op-rbuilder/src/primitives/helpers.rs @@ -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) -> 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: &mut State, + builder_tx_gas: u64, + message: Vec, + signer: Signer, + base_fee: u64, + chain_id: u64, +) -> Result, PayloadBuilderError> +where + DB: Database, +{ + // 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) +} diff --git a/crates/op-rbuilder/src/primitives/mod.rs b/crates/op-rbuilder/src/primitives/mod.rs index 839640f2..604fda1d 100644 --- a/crates/op-rbuilder/src/primitives/mod.rs +++ b/crates/op-rbuilder/src/primitives/mod.rs @@ -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}; diff --git a/crates/op-rbuilder/src/tester/mod.rs b/crates/op-rbuilder/src/tester/mod.rs index 7261a3b5..0da1ed05 100644 --- a/crates/op-rbuilder/src/tester/mod.rs +++ b/crates/op-rbuilder/src/tester/mod.rs @@ -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;