Skip to content

Commit

Permalink
Merge pull request #20 from taikoxyz/cross-chain
Browse files Browse the repository at this point in the history
sync composability with state deltas
  • Loading branch information
Brechtpd authored Jan 11, 2025
2 parents 3afe8f0 + 5bd61a2 commit f2810ee
Show file tree
Hide file tree
Showing 10 changed files with 451 additions and 180 deletions.
227 changes: 117 additions & 110 deletions Cargo.lock

Large diffs are not rendered by default.

75 changes: 51 additions & 24 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ reth-trie-parallel = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.
reth-basic-payload-builder = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-node-core = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-primitives = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-provider = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6", features = ["test-utils"] }
reth-provider = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6", features = ["test-utils", "serde"] }
reth-chainspec = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-evm = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-evm-ethereum = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
Expand Down Expand Up @@ -77,30 +77,57 @@ alloy-rpc-types-eth = { version = "0.3.0" }
alloy-signer-local = { version = "0.3.0" }
alloy-sol-types = { version = "0.8.2", default-features = false }

# misc
bincode = "1.3"

[patch.crates-io]
revm = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
revm-primitives = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
revm-interpreter = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
revm-precompile = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
# revm = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
# revm-primitives = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
# revm-interpreter = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
# revm-precompile = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
revm = { git = "https://github.com/taikoxyz/revm.git", branch = "gwyneth-debug-shit" }
revm-primitives = { git = "https://github.com/taikoxyz/revm.git", branch = "gwyneth-debug-shit" }
revm-interpreter = { git = "https://github.com/taikoxyz/revm.git", branch = "gwyneth-debug-shit" }
revm-precompile = { git = "https://github.com/taikoxyz/revm.git", branch = "gwyneth-debug-shit" }
revm-inspectors = { git = "https://github.com/taikoxyz/revm-inspectors.git", branch = "main-rbuilder" }

[patch."https://github.com/paradigmxyz/reth"]
reth = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-db = {git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-db-common = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-errors = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-libmdbx = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-payload-builder = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-node-api = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-trie = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-trie-parallel = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-basic-payload-builder = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-node-core = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-primitives = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-provider = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-chainspec = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-evm = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-evm-ethereum = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-db-api = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-execution-errors = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth-trie-db = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth" }
reth = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-db = {git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-db-common = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-errors = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-libmdbx = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-payload-builder = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-node-api = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-trie = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-trie-parallel = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-basic-payload-builder = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-node-core = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-primitives = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-provider = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-chainspec = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-evm = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-evm-ethereum = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-db-api = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-execution-errors = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }
reth-trie-db = { git = "https://github.com/taikoxyz/gwyneth", branch = "gwyneth_with_debug_shit_revm_and_inspector" }

#reth = { path = "../../revm-dani/gwyneth/bin/reth" }
#reth-db = { path = "../../revm-dani/gwyneth/crates/storage/db" }
#reth-db-common = { path = "../../revm-dani/gwyneth/crates/storage/db-common" }
#reth-errors = { path = "../../revm-dani/gwyneth/crates/errors" }
#reth-libmdbx = { path = "../../revm-dani/gwyneth/crates/storage/libmdbx-rs" }
#reth-payload-builder = { path = "../../revm-dani/gwyneth/crates/payload/builder" }
#reth-node-api = { path = "../../revm-dani/gwyneth/crates/node/api" }
#reth-trie = { path = "../../revm-dani/gwyneth/crates/trie/trie" }
#reth-trie-parallel = { path = "../../revm-dani/gwyneth/crates/trie/parallel" }
#reth-basic-payload-builder = { path = "../../revm-dani/gwyneth/crates/payload/basic" }
#reth-node-core = { path = "../../revm-dani/gwyneth/crates/node/core" }
#reth-primitives = { path = "../../revm-dani/gwyneth/crates/primitives" }
#reth-provider = { path = "../../revm-dani/gwyneth/crates/storage/provider" }
#reth-chainspec = { path = "../../revm-dani/gwyneth/crates/chainspec" }
#reth-evm = { path = "../../revm-dani/gwyneth/crates/evm" }
#reth-evm-ethereum = { path = "../../revm-dani/gwyneth/crates/ethereum/evm" }
#reth-db-api = { path = "../../revm-dani/gwyneth/crates/storage/db-api" }
#reth-execution-errors = { path = "../../revm-dani/gwyneth/crates/evm/execution-errors" }
#reth-trie-db = { path = "../../revm-dani/gwyneth/crates/trie/db" }
2 changes: 2 additions & 0 deletions crates/rbuilder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ alloy-sol-types.workspace = true
ethereum_ssz_derive.workspace = true
ethereum_ssz.workspace = true

bincode.workspace = true

test_utils = { path = "src/test_utils" }
metrics_macros = { path = "src/telemetry/metrics_macros" }

Expand Down
7 changes: 5 additions & 2 deletions crates/rbuilder/src/bin/debug-bench-machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use reth::providers::BlockNumReader;
use reth_payload_builder::database::SyncCachedReads as CachedReads;
use reth_provider::StateProvider;
use revm_primitives::ChainAddress;
use std::{path::PathBuf, sync::Arc, time::Instant};
use std::{collections::HashMap, path::PathBuf, sync::Arc, time::Instant};
use tracing::{debug, info};

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -116,11 +116,14 @@ async fn main() -> eyre::Result<()> {

let build_time = build_time.elapsed();

let mut factories = HashMap::default();
factories.insert(chain_id, factory);

let finalize_time = Instant::now();
let finalized_block = partial_block.finalize(
&mut state,
&ctx,
factory.clone(),
factories,
root_hash_config.clone(),
config.base_config().root_hash_task_pool()?,
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl<DB: Database + Clone + 'static> BlockBuildingHelper for BlockBuildingHelper
let finalized_block = match self.partial_block.clone().finalize(
&mut self.block_state,
&self.building_ctx,
provider_factory.clone(),
self.provider_factory.clone(),
self.root_hash_config,
self.root_hash_task_pool,
) {
Expand Down
157 changes: 127 additions & 30 deletions crates/rbuilder/src/building/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ pub mod testing;
pub mod tracers;
pub use block_orders::BlockOrders;
use eth_sparse_mpt::SparseTrieSharedCache;
use reth_primitives::proofs::calculate_requests_root;
use revm_primitives::ChainAddress;
use reth_primitives::{proofs::calculate_requests_root, Requests};
use reth_provider::execution_outcome_to_state_diff;
use revm_primitives::{Bytes, ChainAddress, B256};

use crate::{
primitives::{Order, OrderId, SimValue, SimulatedOrder, TransactionSignedEcRecoveredWithBlobs},
Expand Down Expand Up @@ -186,6 +187,7 @@ impl ChainBlockBuildingContext {
extra_data: Vec<u8>,
spec_id: Option<SpecId>,
) -> ChainBlockBuildingContext {
println!("from_attributes");
let attributes = EthPayloadBuilderAttributes::try_new(
attributes.data.parent_block_hash,
attributes.data.payload_attributes.clone(),
Expand Down Expand Up @@ -683,11 +685,13 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
self,
state: &mut BlockState,
ctx: &BlockBuildingContext,
provider_factory: ProviderFactory<DB>,
provider_factories: HashMap<u64, ProviderFactory<DB>>,
root_hash_config: RootHashConfig,
root_hash_task_pool: BlockingTaskPool,
) -> Result<FinalizeResult, FinalizeError> {
let ctx = &ctx.chains[&ctx.parent_chain_id];
let super_ctx = ctx;
let chain_id = ctx.parent_chain_id;
let ctx = &super_ctx.chains[&chain_id];
let (withdrawals_root, withdrawals) = {
let mut db = state.new_db_ref();
let WithdrawalsOutcome {
Expand Down Expand Up @@ -738,6 +742,7 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
let requests_root = calculate_requests_root(&requests);
(Some(requests.into()), Some(requests_root))
} else {
println!("prague not active");
(None, None)
};

Expand All @@ -764,15 +769,15 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {

// Brecht: state root calculation
// TODO Brecht: Fix
let mut root_hash_config = root_hash_config;
root_hash_config.mode = RootHashMode::IgnoreParentHash;
let mut root_hash_config = root_hash_config.clone();
//root_hash_config.mode = RootHashMode::IgnoreParentHash;
let state_root = calculate_state_root(
provider_factory,
provider_factories.get(&chain_id).unwrap().clone(),
ctx.attributes.parent,
&execution_outcome,
root_hash_task_pool,
root_hash_task_pool.clone(),
ctx.shared_sparse_mpt_cache.clone(),
root_hash_config,
root_hash_config.clone(),
)?;

// create the block header
Expand Down Expand Up @@ -807,6 +812,97 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
(Some(0), Some(0))
};

let mut chain_ids = Vec::new();
for account in execution_outcome.bundle.state.keys() {
if !chain_ids.contains(&account.0) {
chain_ids.push(account.0);
}
}

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<_>>();
}

if chain_id == super_ctx.parent_chain_id {
execution_outcome.bundle.state = execution_outcome.bundle.state.into_iter().filter(|account| account.0.1 != ctx.block_env.coinbase.1).collect();
}

if !state_diff.accounts.is_empty() {
let ctx = &super_ctx.chains[&chain_id];

let state_root = calculate_state_root(
provider_factories.get(&chain_id).unwrap().clone(),
ctx.attributes.parent,
&execution_outcome,
root_hash_task_pool.clone(),
//ctx.shared_sparse_mpt_cache.clone(),
SparseTrieSharedCache::default(),
root_hash_config.clone(),
)?;

state_diff.state_root = state_root;

//let extra_data = Bytes::from(serde_json::to_string(&state_diff).unwrap().into_bytes());
let extra_data = Bytes::from(bincode::serialize(&state_diff).unwrap());

println!("extra_data: {}", extra_data);

let header = Header {
parent_hash: ctx.attributes.parent,
ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: ctx.block_env.coinbase.1,
state_root,
transactions_root,
receipts_root,
withdrawals_root,
logs_bloom,
timestamp: ctx.attributes.timestamp,
mix_hash: /* ctx.attributes.prev_randao */ B256::ZERO,
nonce: BEACON_NONCE,
base_fee_per_gas: Some(ctx.block_env.basefee.to()),
number: ctx.block_env.number.to::<u64>(),
gas_limit: ctx.block_env.gas_limit.to(),
difficulty: U256::ZERO,
gas_used: self.gas_used,
extra_data,
parent_beacon_block_root: /* ctx.attributes.parent_beacon_block_root */ Some(B256::ZERO),
blob_gas_used,
excess_blob_gas,
requests_root,
};

println!("chain {} header: {:?}", chain_id, header);

let block = Block {
header,
body: self
.executed_tx
.clone()
.into_iter()
.map(|t| t.into_internal_tx_unsecure().into())
.collect(),
ommers: vec![],
withdrawals: Some(Withdrawals::default()),
requests: Some(Requests::default()),
};

let sealed_block = block.seal_slow();

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

blocks.insert(chain_id, sealed_block);
}
}

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

let header = Header {
parent_hash: ctx.attributes.parent,
ommers_hash: EMPTY_OMMER_ROOT_HASH,
Expand All @@ -824,7 +920,7 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
gas_limit: ctx.block_env.gas_limit.to(),
difficulty: U256::ZERO,
gas_used: self.gas_used,
extra_data: ctx.extra_data.clone().into(),
extra_data/*: ctx.extra_data.clone().into()*/,
parent_beacon_block_root: ctx.attributes.parent_beacon_block_root,
blob_gas_used,
excess_blob_gas,
Expand Down Expand Up @@ -857,26 +953,27 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
ctx: &BlockBuildingContext,
state: &mut BlockState,
) -> eyre::Result<()> {
let ctx = &ctx.chains[&ctx.parent_chain_id];
let evm_config = EthEvmConfig::default();
let mut db = state.new_db_ref();
pre_block_beacon_root_contract_call(
db.as_mut(),
&evm_config,
&ctx.chain_spec,
&ctx.initialized_cfg,
&ctx.block_env,
ctx.attributes.parent_beacon_block_root(),
)?;
pre_block_blockhashes_contract_call(
db.as_mut(),
&evm_config,
&ctx.chain_spec,
&ctx.initialized_cfg,
&ctx.block_env,
ctx.attributes.parent,
)?;
db.as_mut().merge_transitions(BundleRetention::Reverts);
// TODO(Brecht): all chains (or none)
// let ctx = &ctx.chains[&ctx.parent_chain_id];
// let evm_config = EthEvmConfig::default();
// let mut db = state.new_db_ref();
// pre_block_beacon_root_contract_call(
// db.as_mut(),
// &evm_config,
// &ctx.chain_spec,
// &ctx.initialized_cfg,
// &ctx.block_env,
// ctx.attributes.parent_beacon_block_root(),
// )?;
// pre_block_blockhashes_contract_call(
// db.as_mut(),
// &evm_config,
// &ctx.chain_spec,
// &ctx.initialized_cfg,
// &ctx.block_env,
// ctx.attributes.parent,
// )?;
// db.as_mut().merge_transitions(BundleRetention::Reverts);
Ok(())
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/rbuilder/src/building/order_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,9 +1168,10 @@ fn coinbase_profit(
if coinbase_balance_after >= coinbase_balance_before {
Ok(coinbase_balance_after - coinbase_balance_before)
} else {
Err(OrderErr::NegativeProfit(
coinbase_balance_before - coinbase_balance_after,
))
Ok(U256::ZERO)
// Err(OrderErr::NegativeProfit(
// coinbase_balance_before - coinbase_balance_after,
// ))
}
}

Expand Down
Loading

0 comments on commit f2810ee

Please sign in to comment.