Skip to content

fix: payout gas limit estimation #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
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
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/rbuilder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ built = { version = "0.7.1", features = ["git2", "chrono"] }

[dev-dependencies]
tempfile = "3.8"
assert_matches = "1.5.0"
criterion = { version = "0.5.1", features = ["html_reports", "async_tokio"] }

[features]
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/building/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
nonce,
ctx.attributes.suggested_fee_recipient,
gas_limit,
value.to(),
value,
)?;
// payout tx has no blobs so it's safe to unwrap
let tx = TransactionSignedEcRecoveredWithBlobs::new_no_blobs(tx).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/building/order_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
nonce,
to,
payout.gas_limit,
payout.tx_value.to(),
payout.tx_value,
) {
// payout tx has no blobs so it's safe to unwrap
Ok(tx) => TransactionSignedEcRecoveredWithBlobs::new_no_blobs(tx).unwrap(),
Expand Down
78 changes: 71 additions & 7 deletions crates/rbuilder/src/building/payout_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn create_payout_tx(
nonce: u64,
to: Address,
gas_limit: u64,
value: u128,
value: U256,
) -> Result<Recovered<TransactionSigned>, secp256k1::Error> {
let tx = Transaction::Eip1559(TxEip1559 {
chain_id: chain_spec.chain.id(),
Expand All @@ -24,7 +24,7 @@ pub fn create_payout_tx(
max_fee_per_gas: basefee as u128,
max_priority_fee_per_gas: 0,
to: TransactionKind::Call(to),
value: U256::from(value),
value,
..Default::default()
});

Expand Down Expand Up @@ -53,22 +53,23 @@ pub fn insert_test_payout_tx(

let nonce = state.nonce(builder_signer.address)?;

let mut cfg = ctx.evm_env.cfg_env.clone();
// disable balance check so we can estimate the gas cost without having any funds
cfg.disable_balance_check = true;

let tx_value = 10u128.pow(18); // 10 ether
let tx = create_payout_tx(
ctx.chain_spec.as_ref(),
ctx.evm_env.block_env.basefee,
builder_signer,
nonce,
to,
gas_limit,
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
U256::from(tx_value),
)?;

let mut db = state.new_db_ref();
let mut evm = ctx.evm_factory.create_evm(db.as_mut(), ctx.evm_env.clone());

let cache_account = evm.db_mut().load_cache_account(builder_signer.address)?;
cache_account.increment_balance(tx_value * 2); // double to cover tx value and fee

let res = evm.transact(&tx)?;
match res.result {
ExecutionResult::Success {
Expand Down Expand Up @@ -130,3 +131,66 @@ pub fn estimate_payout_gas_limit(
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::building::builders::mock_block_building_helper::MockRootHasher;
use alloy_eips::eip1559::INITIAL_BASE_FEE;
use alloy_primitives::B256;
use assert_matches::assert_matches;
use reth_chainspec::{EthereumHardfork, MAINNET};
use reth_db::{tables, transaction::DbTxMut};
use reth_primitives::Account;
use reth_provider::test_utils::create_test_provider_factory_with_chain_spec;
use revm::primitives::hardfork::SpecId;
use std::sync::Arc;

#[test]
fn estimate_payout_tx_gas_limit() {
let signer = Signer::random();
let proposer = Address::random();
let chain_spec = MAINNET.clone();
let spec_id = SpecId::CANCUN;
let cancun_timestamp = chain_spec
.fork(EthereumHardfork::Cancun)
.as_timestamp()
.unwrap();

// Insert proposer
let provider_factory = create_test_provider_factory_with_chain_spec(chain_spec.clone());
let provider_rw = provider_factory.provider_rw().unwrap();
provider_rw
.tx_ref()
.put::<tables::PlainAccountState>(
proposer,
Account {
balance: U256::ZERO,
nonce: 1,
bytecode_hash: Some(B256::random()),
},
)
.unwrap();
provider_rw.commit().unwrap();

let mut block: alloy_rpc_types::Block = Default::default();
block.header.base_fee_per_gas = Some(INITIAL_BASE_FEE);
block.header.timestamp = cancun_timestamp + 1;
block.header.gas_limit = 30_000_000;
let ctx = BlockBuildingContext::from_onchain_block(
block,
chain_spec,
Some(spec_id),
Default::default(),
signer.address,
proposer,
Some(signer),
Arc::new(MockRootHasher {}),
);
let mut state = BlockState::new(provider_factory.latest().unwrap());

let estimate_result = estimate_payout_gas_limit(proposer, &ctx, &mut state, 0);
assert_matches!(estimate_result, Ok(_));
assert_eq!(estimate_result.unwrap(), 21_000);
}
}
Loading