Skip to content

Commit 0d8ab7c

Browse files
author
Solar Mithril
committed
Move estimate_gas_for_builder_tx and signed_builder_tx to helpers
1 parent 9ed4f94 commit 0d8ab7c

File tree

4 files changed

+74
-61
lines changed

4 files changed

+74
-61
lines changed

crates/op-rbuilder/src/payload_builder_vanilla.rs

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ use crate::generator::BuildArguments;
33
use crate::{
44
generator::{BlockCell, PayloadBuilder},
55
metrics::OpRBuilderMetrics,
6-
primitives::{ExecutedPayload, ExecutionInfo, PayloadBuilderService},
6+
primitives::{
7+
estimate_gas_for_builder_tx, signed_builder_tx, ExecutedPayload, ExecutionInfo,
8+
PayloadBuilderService,
9+
},
710
tx_signer::Signer,
811
};
912
use alloy_consensus::constants::EMPTY_WITHDRAWALS;
10-
use alloy_consensus::transaction::Recovered;
1113
use alloy_consensus::{
12-
Eip658Value, Header, Transaction, TxEip1559, Typed2718, EMPTY_OMMER_ROOT_HASH,
14+
Eip658Value, Header, Transaction, Typed2718, EMPTY_OMMER_ROOT_HASH,
1315
};
1416
use alloy_eips::merge::BEACON_NONCE;
1517
use alloy_primitives::private::alloy_rlp::Encodable;
16-
use alloy_primitives::{Address, Bytes, TxKind, U256};
18+
use alloy_primitives::{Bytes, U256};
1719
use alloy_rpc_types_engine::PayloadId;
1820
use alloy_rpc_types_eth::Withdrawals;
19-
use op_alloy_consensus::{OpDepositReceipt, OpTypedTransaction};
21+
use op_alloy_consensus::OpDepositReceipt;
2022
use reth::builder::{components::PayloadServiceBuilder, node::FullNodeTypes, BuilderContext};
2123
use reth::core::primitives::InMemorySize;
2224
use reth::payload::PayloadBuilderHandle;
@@ -1232,58 +1234,3 @@ where
12321234
})
12331235
}
12341236
}
1235-
1236-
/// Creates signed builder tx to Address::ZERO and specified message as input
1237-
pub fn signed_builder_tx<DB>(
1238-
db: &mut State<DB>,
1239-
builder_tx_gas: u64,
1240-
message: Vec<u8>,
1241-
signer: Signer,
1242-
base_fee: u64,
1243-
chain_id: u64,
1244-
) -> Result<Recovered<OpTransactionSigned>, PayloadBuilderError>
1245-
where
1246-
DB: Database<Error = ProviderError>,
1247-
{
1248-
// Create message with block number for the builder to sign
1249-
let nonce = db
1250-
.load_cache_account(signer.address)
1251-
.map(|acc| acc.account_info().unwrap_or_default().nonce)
1252-
.map_err(|_| {
1253-
PayloadBuilderError::other(OpPayloadBuilderError::AccountLoadFailed(signer.address))
1254-
})?;
1255-
1256-
// Create the EIP-1559 transaction
1257-
let tx = OpTypedTransaction::Eip1559(TxEip1559 {
1258-
chain_id,
1259-
nonce,
1260-
gas_limit: builder_tx_gas,
1261-
max_fee_per_gas: base_fee.into(),
1262-
max_priority_fee_per_gas: 0,
1263-
to: TxKind::Call(Address::ZERO),
1264-
// Include the message as part of the transaction data
1265-
input: message.into(),
1266-
..Default::default()
1267-
});
1268-
// Sign the transaction
1269-
let builder_tx = signer.sign_tx(tx).map_err(PayloadBuilderError::other)?;
1270-
1271-
Ok(builder_tx)
1272-
}
1273-
1274-
fn estimate_gas_for_builder_tx(input: Vec<u8>) -> u64 {
1275-
// Count zero and non-zero bytes
1276-
let (zero_bytes, nonzero_bytes) = input.iter().fold((0, 0), |(zeros, nonzeros), &byte| {
1277-
if byte == 0 {
1278-
(zeros + 1, nonzeros)
1279-
} else {
1280-
(zeros, nonzeros + 1)
1281-
}
1282-
});
1283-
1284-
// Calculate gas cost (4 gas per zero byte, 16 gas per non-zero byte)
1285-
let zero_cost = zero_bytes * 4;
1286-
let nonzero_cost = nonzero_bytes * 16;
1287-
1288-
zero_cost + nonzero_cost + 21_000
1289-
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use crate::tx_signer::Signer;
2+
use alloy_consensus::transaction::Recovered;
3+
use alloy_consensus::TxEip1559;
4+
use alloy_primitives::{Address, TxKind};
5+
use op_alloy_consensus::OpTypedTransaction;
6+
use reth_evm::Database;
7+
use reth_optimism_payload_builder::error::OpPayloadBuilderError;
8+
use reth_optimism_primitives::OpTransactionSigned;
9+
use reth_payload_primitives::PayloadBuilderError;
10+
use reth_provider::ProviderError;
11+
use revm::db::State;
12+
pub fn estimate_gas_for_builder_tx(input: Vec<u8>) -> u64 {
13+
// Count zero and non-zero bytes
14+
let (zero_bytes, nonzero_bytes) = input.iter().fold((0, 0), |(zeros, nonzeros), &byte| {
15+
if byte == 0 {
16+
(zeros + 1, nonzeros)
17+
} else {
18+
(zeros, nonzeros + 1)
19+
}
20+
});
21+
22+
// Calculate gas cost (4 gas per zero byte, 16 gas per non-zero byte)
23+
let zero_cost = zero_bytes * 4;
24+
let nonzero_cost = nonzero_bytes * 16;
25+
26+
zero_cost + nonzero_cost + 21_000
27+
}
28+
/// Creates signed builder tx to Address::ZERO and specified message as input
29+
pub fn signed_builder_tx<DB>(
30+
db: &mut State<DB>,
31+
builder_tx_gas: u64,
32+
message: Vec<u8>,
33+
signer: Signer,
34+
base_fee: u64,
35+
chain_id: u64,
36+
) -> Result<Recovered<OpTransactionSigned>, PayloadBuilderError>
37+
where
38+
DB: Database<Error = ProviderError>,
39+
{
40+
// Create message with block number for the builder to sign
41+
let nonce = db
42+
.load_cache_account(signer.address)
43+
.map(|acc| acc.account_info().unwrap_or_default().nonce)
44+
.map_err(|_| {
45+
PayloadBuilderError::other(OpPayloadBuilderError::AccountLoadFailed(signer.address))
46+
})?;
47+
48+
// Create the EIP-1559 transaction
49+
let tx = OpTypedTransaction::Eip1559(TxEip1559 {
50+
chain_id,
51+
nonce,
52+
gas_limit: builder_tx_gas,
53+
max_fee_per_gas: base_fee.into(),
54+
max_priority_fee_per_gas: 0,
55+
to: TxKind::Call(Address::ZERO),
56+
// Include the message as part of the transaction data
57+
input: message.into(),
58+
..Default::default()
59+
});
60+
// Sign the transaction
61+
let builder_tx = signer.sign_tx(tx).map_err(PayloadBuilderError::other)?;
62+
63+
Ok(builder_tx)
64+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! This module contains types from the reth that weren't heavily modified
22
mod execution;
3+
mod helpers;
34
mod payload_builder_service;
45

56
pub use payload_builder_service::PayloadBuilderService;
67

8+
pub use helpers::{estimate_gas_for_builder_tx, signed_builder_tx};
9+
710
pub use execution::{ExecutedPayload, ExecutionInfo};

crates/op-rbuilder/src/tester/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use reth_node_api::{EngineTypes, PayloadTypes};
2424
use reth_optimism_node::OpEngineTypes;
2525
use reth_payload_builder::PayloadId;
2626
use reth_rpc_layer::{AuthClientLayer, AuthClientService, JwtSecret};
27-
use serde_json;
2827
use serde_json::Value;
2928
use std::str::FromStr;
3029
use std::time::SystemTime;

0 commit comments

Comments
 (0)