Skip to content
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

ULTRA TX #27

Merged
merged 2 commits into from
Feb 15, 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
12 changes: 6 additions & 6 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ revm-inspectors = "0.6"
revm-primitives = { version = "9.0.0", features = [
"std",
], default-features = false }
revm-interpreter = { version = "9.0.0", features = [
"std",
], default-features = false }

ethereum_ssz_derive = "0.7"
ethereum_ssz = "0.7"
Expand Down
41 changes: 27 additions & 14 deletions crates/rbuilder/src/building/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use block_orders::BlockOrders;
use eth_sparse_mpt::SparseTrieSharedCache;
use reth_primitives::{proofs::calculate_requests_root, Requests};
use reth_provider::execution_outcome_to_state_diff;
use revm_primitives::{Bytes, ChainAddress, B256};
use revm_primitives::{create_state_diff, Bytes, ChainAddress, B256};

use crate::{
primitives::{Order, OrderId, SimValue, SimulatedOrder, TransactionSignedEcRecoveredWithBlobs},
Expand Down Expand Up @@ -47,6 +47,7 @@ use reth_payload_builder::{database::SyncCachedReads as CachedReads, EthPayloadB
use revm::{
db::states::{bundle_state::BundleRetention::{self, PlainState}, reverts::Reverts},
primitives::{BlobExcessGasAndPrice, BlockEnv, CfgEnvWithHandlerCfg, SpecId}, TransitionState,
interpreter::primitives::StateChanges,
};
use serde::Deserialize;
use std::{hash::Hash, str::FromStr, sync::Arc, thread::sleep, time::Duration};
Expand All @@ -63,7 +64,7 @@ pub use order_commit::*;
pub use payout_tx::*;
pub use sim::simulate_order;

pub const MAX_CALLDATA_LEN: u64 = 128000;
pub const MAX_CALLDATA_LEN: u64 = 256000;

#[derive(Debug, Clone)]
pub struct BlockBuildingContext {
Expand Down Expand Up @@ -429,6 +430,7 @@ pub struct PartialBlock<Tracer: SimulationTracer> {
pub executed_tx: Vec<TransactionSignedEcRecoveredWithBlobs>,
/// Receipts belonging to successfully executed orders.
pub receipts: Vec<Receipt>,
pub state_changes: Vec<StateChanges>,
pub tracer: Tracer,
}

Expand All @@ -443,6 +445,7 @@ pub struct ExecutionResult {
/// Fully dropped orders (TxRevertBehavior::AllowedExcluded allows it!) are not included.
pub original_order_ids: Vec<OrderId>,
pub receipts: Vec<Receipt>,
pub state_changes: Vec<StateChanges>,
pub nonces_updated: Vec<(Address, u64)>,
pub paid_kickbacks: Vec<(Address, U256)>,
}
Expand Down Expand Up @@ -541,6 +544,7 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
coinbase_profit: self.coinbase_profit,
executed_tx: self.executed_tx,
receipts: self.receipts,
state_changes: self.state_changes,
tracer,
}
}
Expand Down Expand Up @@ -607,6 +611,7 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
self.coinbase_profit += ok_result.coinbase_profit;
self.executed_tx.extend(ok_result.txs.clone());
self.receipts.extend(ok_result.receipts.clone());
self.state_changes.extend(ok_result.state_changes.clone());

//println!("self.executed_tx num: {:?}", self.executed_tx.len());
//println!("self.executed_tx: {:?}", self.executed_tx);
Expand All @@ -619,6 +624,7 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
txs: ok_result.txs,
original_order_ids: ok_result.original_order_ids,
receipts: ok_result.receipts,
state_changes: ok_result.state_changes,
nonces_updated: ok_result.nonces_updated,
paid_kickbacks: ok_result.paid_kickbacks,
}))
Expand Down Expand Up @@ -680,6 +686,7 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
self.blob_gas_used += ok_result.blob_gas_used;
self.executed_tx.push(ok_result.tx);
self.receipts.push(ok_result.receipt);
self.state_changes.push(ok_result.state_changes);

//println!("self.executed_tx num: {:?}", self.executed_tx.len());
//println!("self.executed_tx: {:?}", self.executed_tx);
Expand Down Expand Up @@ -828,21 +835,24 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
}
}

let state_changes = StateChanges {
entries: self.state_changes.iter().cloned().map(|s| s.entries).collect::<Vec<_>>().into_iter().flatten().collect(),
};
//println!("L1 state changes: {:?}", state_changes);

let l1_state_diff = create_state_diff(state_changes, super_ctx.parent_chain_id);

//println!("state_changes [{:?}] ({:?}): {:?}", self.executed_tx.len(), self.state_changes.len(), self.state_changes);
//println!("l1_state_diff: {:?}", l1_state_diff);

let mut blocks = HashMap::default();
for chain_id in chain_ids {
let mut execution_outcome = execution_outcome.filter_chain(chain_id);

let mut state_diff = execution_outcome_to_state_diff(&execution_outcome, B256::ZERO, self.gas_used);
// Filter out accounts
state_diff.accounts = state_diff.clone().accounts.into_iter().filter(|account| account.address != alloy_eips::eip4788::BEACON_ROOTS_ADDRESS && account.address != alloy_eips::eip2935::HISTORY_STORAGE_ADDRESS).collect::<Vec<_>>();
if chain_id == super_ctx.parent_chain_id {
state_diff.accounts = state_diff.clone().accounts.into_iter().filter(|account| account.address != ctx.block_env.coinbase.1).collect::<Vec<_>>();
continue;
}

if chain_id == super_ctx.parent_chain_id {
// The reverts will still contain the address but that's fine, we're never going to use that on L1 anyway
execution_outcome.bundle.state = execution_outcome.bundle.state.into_iter().filter(|account| account.0.1 != ctx.block_env.coinbase.1).collect();
}
let execution_outcome = execution_outcome.filter_chain(chain_id);
let mut state_diff = execution_outcome_to_state_diff(&execution_outcome, B256::ZERO, self.gas_used);

// Only make a block for chains that have changes
if !state_diff.accounts.is_empty() {
Expand All @@ -862,6 +872,8 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
state_diff.transactions_root = transactions_root;
state_diff.bundle.reverts = reth_provider::merge_reverts(&state_diff.bundle.reverts);

//println!("state diff {}: {:?}", chain_id, state_diff.bundle.state);

let extra_data = Bytes::from(bincode::serialize(&state_diff).unwrap());
// println!("extra_data: {}", extra_data);

Expand Down Expand Up @@ -906,15 +918,15 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {

let sealed_block = block.seal_slow();

sleep(Duration::from_millis(10));
sleep(Duration::from_millis(100));

println!("[{}] chain {} calculated block hash: {:?}", super_ctx.block(), chain_id, sealed_block.hash());

blocks.insert(chain_id, sealed_block);
}
}

let extra_data = Bytes::from(bincode::serialize(&(execution_outcome, blocks)).unwrap());
let extra_data = Bytes::from(bincode::serialize(&(execution_outcome, l1_state_diff, blocks)).unwrap());

let header = Header {
parent_hash: ctx.attributes.parent,
Expand Down Expand Up @@ -1003,6 +1015,7 @@ impl PartialBlock<()> {
coinbase_profit: U256::ZERO,
executed_tx: Vec::new(),
receipts: Vec::new(),
state_changes: Vec::new(),
tracer: (),
}
}
Expand Down
17 changes: 17 additions & 0 deletions crates/rbuilder/src/building/order_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use revm::{
db::{states::bundle_state::BundleRetention, BundleState},
inspector_handle_register,
primitives::{db::WrapDatabaseRef, EVMError, Env, ExecutionResult, InvalidTransaction, TxEnv}, DatabaseCommit, State, SyncDatabase as Database,
interpreter::primitives::StateChanges,
};
use revm_primitives::ChainAddress;

Expand Down Expand Up @@ -201,6 +202,7 @@ pub struct TransactionOk {
/// account nonce was 0, tx was included, nonce is 1. => nonce_updated.1 == 1
pub nonce_updated: (Address, u64),
pub receipt: Receipt,
pub state_changes: StateChanges,
}

#[derive(Error, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -228,6 +230,7 @@ pub struct BundleOk {
/// nonces_updates has a set of deduplicated final nonces of the txs in the order
pub nonces_updated: Vec<(Address, u64)>,
pub receipts: Vec<Receipt>,
pub state_changes: Vec<StateChanges>,
pub paid_kickbacks: Vec<(Address, U256)>,
/// Only for sbundles we accumulate ShareBundleInner::original_order_id that executed ok.
/// Its original use is for only one level or orders with original_order_id but if nesting happens the parent order original_order_id goes before its children (pre-order DFS)
Expand Down Expand Up @@ -293,6 +296,7 @@ pub struct OrderOk {
/// nonces_updates has a set of deduplicated final nonces of the txs in the order
pub nonces_updated: Vec<(Address, u64)>,
pub receipts: Vec<Receipt>,
pub state_changes: Vec<StateChanges>,
pub paid_kickbacks: Vec<(Address, U256)>,
pub used_state_trace: Option<UsedStateTrace>,
}
Expand Down Expand Up @@ -458,6 +462,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {

let mut env = env.clone();
env.cfg.chain_id = tx.chain_id().unwrap();
env.cfg.xchain = true;
//println!("active remv chain_id: {}", env.cfg.chain_id);

let mut evm = revm::Evm::builder()
Expand Down Expand Up @@ -572,6 +577,8 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
logs: res.result.logs().to_vec(),
};

let state_changes = res.result.state_changes();

// if !res.result.is_success() {
// println!("tx reverted with reason: {:?}", res.result);
// }
Expand All @@ -586,6 +593,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
tx: tx_with_blobs.clone(),
nonce_updated: (tx.signer(), tx.nonce() + 1),
receipt,
state_changes,
}))
}

Expand Down Expand Up @@ -656,6 +664,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
txs: Vec::new(),
nonces_updated: Vec::new(),
receipts: Vec::new(),
state_changes: Vec::new(),
paid_kickbacks: Vec::new(),
original_order_ids: Vec::new(),
};
Expand Down Expand Up @@ -684,6 +693,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
insert.txs.push(res.tx);
update_nonce_list(&mut insert.nonces_updated, res.nonce_updated);
insert.receipts.push(res.receipt);
insert.state_changes.push(res.state_changes);
}
Err(err) => {
// if optional transaction, skip
Expand Down Expand Up @@ -829,6 +839,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
insert.txs.push(res.tx);
update_nonce_list(&mut insert.nonces_updated, res.nonce_updated);
insert.receipts.push(res.receipt);
insert.state_changes.push(res.state_changes);
insert.paid_kickbacks.push((to, value));
}
Err(err) => {
Expand Down Expand Up @@ -892,6 +903,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
txs: Vec::new(),
nonces_updated: Vec::new(),
receipts: Vec::new(),
state_changes: Vec::new(),
paid_kickbacks: Vec::new(),
original_order_ids: Vec::new(),
};
Expand Down Expand Up @@ -951,6 +963,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
insert.txs.push(res.tx);
update_nonce_list(&mut insert.nonces_updated, res.nonce_updated);
insert.receipts.push(res.receipt);
insert.state_changes.push(res.state_changes);
}
Err(err) => {
// if optional transaction, skip
Expand Down Expand Up @@ -1002,6 +1015,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
res.bundle_ok.nonces_updated,
);
insert.receipts.extend(res.bundle_ok.receipts);
insert.state_changes.extend(res.bundle_ok.state_changes);

for (addr, reserve) in res.payouts_promissed {
inner_payouts
Expand Down Expand Up @@ -1168,6 +1182,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
txs: vec![ok.tx],
nonces_updated: vec![ok.nonce_updated],
receipts: vec![ok.receipt],
state_changes: vec![ok.state_changes],
paid_kickbacks: Vec::new(),
used_state_trace: self.get_used_state_trace(),
original_order_ids: Vec::new(),
Expand Down Expand Up @@ -1208,6 +1223,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
txs: ok.txs,
nonces_updated: ok.nonces_updated,
receipts: ok.receipts,
state_changes: ok.state_changes,
paid_kickbacks: ok.paid_kickbacks,
used_state_trace: self.get_used_state_trace(),
original_order_ids: ok.original_order_ids,
Expand Down Expand Up @@ -1248,6 +1264,7 @@ impl<'a, 'b, Tracer: SimulationTracer> PartialBlockFork<'a, 'b, Tracer> {
txs: ok.txs,
nonces_updated: ok.nonces_updated,
receipts: ok.receipts,
state_changes: ok.state_changes,
paid_kickbacks: ok.paid_kickbacks,
used_state_trace: self.get_used_state_trace(),
original_order_ids: ok.original_order_ids,
Expand Down
5 changes: 3 additions & 2 deletions crates/rbuilder/src/live_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ impl<DB: Database + Clone + 'static, BuilderSourceType: SlotSource>
inc_active_slots();

println!("Dani debug: build block context");
let block_ctx = ChainBlockBuildingContext::from_attributes(
let parent_block_ctx = ChainBlockBuildingContext::from_attributes(
payload.payload_attributes_event.clone(),
&parent_header,
self.coinbase_signer.clone(),
Expand Down Expand Up @@ -310,7 +310,7 @@ impl<DB: Database + Clone + 'static, BuilderSourceType: SlotSource>
}

println!("setting up {}", chain_id);
let mut block_ctx = block_ctx.clone();
let mut block_ctx = parent_block_ctx.clone();
let mut chain_spec = (*block_ctx.chain_spec).clone();
println!("chain spec chain id: {}", chain_spec.chain.id());
if chain_spec.chain.id() != chain_id {
Expand Down Expand Up @@ -338,6 +338,7 @@ impl<DB: Database + Clone + 'static, BuilderSourceType: SlotSource>
//block_ctx.block_env.difficulty = U256::ZERO;
//block_ctx.block_env.blob_excess_gas_and_price = Some(BlobExcessGasAndPrice::new(0));
block_ctx.block_env.coinbase = ChainAddress(chain_id, block_ctx.block_env.coinbase.1);
block_ctx.initialized_cfg.parent_chain_id = Some(parent_block_ctx.chain_spec.chain().id());
} else {
println!("failed to get latest block for {}", chain_id);
}
Expand Down
14 changes: 9 additions & 5 deletions crates/rbuilder/src/live_builder/order_input/txpool_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use alloy_primitives::{hex, Bytes, FixedBytes};
use alloy_provider::{IpcConnect, Provider, ProviderBuilder, RootProvider};
use alloy_pubsub::PubSubFrontend;
use futures::StreamExt;
use revm_primitives::{address, ChainAddress};
use std::{pin::pin, time::Instant};
use tokio::{
sync::{mpsc, mpsc::error::SendTimeoutError},
Expand Down Expand Up @@ -44,11 +45,6 @@ pub async fn subscribe_to_txpool_with_blobs(
while let Some(tx_hash) = stream.next().await {
println!("New tx arrived on {:?}!", config.ipc_path);

// TODO: Skip L1 transactions for now because circular
if config.ipc_path.to_str().unwrap() == "/tmp/reth.ipc" {
println!("skipping!");
continue;
}
let start = Instant::now();

let tx_with_blobs = match get_tx_with_blobs(tx_hash, &provider).await {
Expand All @@ -68,6 +64,14 @@ pub async fn subscribe_to_txpool_with_blobs(
}
};

// TODO: Skip propose transactions from the proposer
if config.ipc_path.to_str().unwrap() == "/tmp/reth.ipc" &&
tx_with_blobs.signer() == address!("E25583099BA105D9ec0A67f5Ae86D90e50036425") &&
tx_with_blobs.to().unwrap_or_default() == address!("9fCF7D13d10dEdF17d0f24C62f0cf4ED462f65b7") {
println!("skipping! {:?} from {:?}", tx_hash, tx_with_blobs.signer());
continue;
}

let tx = MempoolTx::new(tx_with_blobs);

let order = Order::Tx(tx);
Expand Down
Loading
Loading