Skip to content

Commit

Permalink
Merge pull request #12 from taikoxyz/gwynethV3
Browse files Browse the repository at this point in the history
integration with gwyneth-reth
  • Loading branch information
Brechtpd authored Oct 11, 2024
2 parents e6417a8 + 6ec8589 commit f5c4aef
Show file tree
Hide file tree
Showing 20 changed files with 416 additions and 285 deletions.
550 changes: 324 additions & 226 deletions Cargo.lock

Large diffs are not rendered by default.

31 changes: 29 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,32 @@ alloy-rpc-types-engine = { version = "0.3.0", features = [
] }
alloy-rpc-types-eth = { version = "0.3.0" }
alloy-signer-local = { version = "0.3.0" }
alloy-sol-types = { version = "0.8.0", default-features = false }
#alloy-contract = { version = "0.3.0", default-features = false }
alloy-sol-types = { version = "0.8.2", default-features = false }

[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-inspectors = { git = "https://github.com/taikoxyz/revm-inspectors.git", branch = "main-rbuilder" }

[patch."https://github.com/paradigmxyz/reth"]
reth = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-db = {git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-db-common = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-errors = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-libmdbx = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-payload-builder = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-node-api = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-trie = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-trie-parallel = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-basic-payload-builder = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-node-core = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-primitives = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-provider = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-chainspec = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-evm = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-evm-ethereum = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-db-api = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-execution-errors = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
reth-trie-db = { git = "https://github.com/taikoxyz/taiko-reth", branch = "rbuilderV3" }
6 changes: 3 additions & 3 deletions crates/rbuilder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
alloy-sol-types.workspace = true
#alloy-contract.workspace = true

ethers = { version = "2.0", features = ["ws", "rustls", "ipc"] }
web3 = "0.19.0"
ethers-providers = { version = "2.0", features = ["ipc"] }
web3 = "0.19.0"
rlp = "0.6.1"
tokio = "1.38.0"
serde = "1.0.188"
Expand Down Expand Up @@ -56,6 +55,7 @@ alloy-consensus.workspace = true
alloy-serde.workspace = true
alloy-signer-local.workspace = true
alloy-eips.workspace = true
alloy-sol-types.workspace = true

ethereum_ssz_derive.workspace = true
ethereum_ssz.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/backtest/backtest_build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ fn print_onchain_block_data(
println!(
" {:>4} address: {:?>24}, key: {:?}",
txs_to_idx.get(tx).unwrap(),
slot.address,
slot.address.1,
slot.key
);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/backtest/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use alloy_primitives::{Address, U256};
use reth::providers::ProviderFactory;
use reth_chainspec::ChainSpec;
use reth_db::{database::Database, DatabaseEnv};
use reth_payload_builder::database::CachedReads;
use reth_payload_builder::database::SyncCachedReads as CachedReads;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use crate::utils::{extract_onchain_block_txs, find_suggested_fee_recipient};
use ahash::{HashMap, HashSet};
use alloy_primitives::{B256, I256};
use eyre::Context;
use reth::blockchain_tree::chain;
use reth_chainspec::ChainSpec;
use reth_db::DatabaseEnv;
use reth_primitives::{Receipt, TransactionSignedEcRecovered, TxHash};
use reth_provider::ProviderFactory;
use revm_primitives::ChainAddress;
use std::sync::Arc;

#[derive(Debug)]
Expand Down Expand Up @@ -60,6 +62,8 @@ pub fn sim_historical_block(
let mut cumulative_blob_gas_used = 0;
let mut written_slots: HashMap<SlotKey, Vec<B256>> = HashMap::default();

let coinbase = ChainAddress(chain_spec.chain.id(), coinbase);

for (idx, tx) in txs.into_iter().enumerate() {
let coinbase_balance_before = state.balance(coinbase)?;
let mut accumulator_tracer = AccumulatorSimulationTracer::default();
Expand Down
4 changes: 1 addition & 3 deletions crates/rbuilder/src/bin/debug-bench-machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//! This only works when reth node is stopped and the chain moved forward form its synced state
//! It downloads block aftre the last one synced and re-executes all the txs in it.
use alloy_provider::Provider;
use ahash::HashMap;
use alloy_primitives::{B256, U256};
use clap::Parser;
use eyre::Context;
use itertools::Itertools;
Expand All @@ -13,7 +11,7 @@ use rbuilder::{
utils::{extract_onchain_block_txs, find_suggested_fee_recipient, http_provider},
};
use reth::providers::BlockNumReader;
use reth_payload_builder::database::CachedReads;
use reth_payload_builder::database::SyncCachedReads as CachedReads;
use reth_provider::StateProvider;
use std::{path::PathBuf, sync::Arc, time::Instant};
use tracing::{debug, info};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use ahash::HashMap;
use alloy_primitives::U256;
use reth::tasks::pool::BlockingTaskPool;
use reth_db::database::Database;
use reth_payload_builder::database::CachedReads;
use reth_payload_builder::database::SyncCachedReads as CachedReads;
use reth_primitives::format_ether;
use reth_provider::{BlockNumReader, ProviderFactory, StateProvider};
use revm_primitives::ChainAddress;
use time::OffsetDateTime;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, trace};
Expand Down Expand Up @@ -283,7 +284,7 @@ impl<DB: Database + Clone + 'static> BlockBuildingHelperFromDB<DB> {
// we check the fee_recipient delta and make our bid include that! This is supposed to be what the relay will check.
let fee_recipient_balance_after = self
.block_state
.balance(self.building_ctx[&self.origin_chain_id].attributes.suggested_fee_recipient)?;
.balance(ChainAddress(self.origin_chain_id, self.building_ctx[&self.origin_chain_id].attributes.suggested_fee_recipient))?;
let fee_recipient_balance_diff = fee_recipient_balance_after
.checked_sub(self._fee_recipient_balance_start)
.unwrap_or_default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
primitives::SimulatedOrder,
};
use alloy_primitives::U256;
use reth_payload_builder::database::CachedReads;
use reth_payload_builder::database::SyncCachedReads as CachedReads;
use reth_primitives::SealedBlock;
use time::OffsetDateTime;

Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/building/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use reth::{
tasks::pool::BlockingTaskPool,
};
use reth_db::database::Database;
use reth_payload_builder::database::CachedReads;
use reth_payload_builder::database::SyncCachedReads as CachedReads;
use std::sync::Arc;
use tokio::sync::{broadcast, broadcast::error::TryRecvError};
use tokio_util::sync::CancellationToken;
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/building/builders/ordering_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tokio_util::sync::CancellationToken;

use crate::{roothash::RootHashConfig, utils::check_provider_factory_health};
use reth::tasks::pool::BlockingTaskPool;
use reth_payload_builder::database::CachedReads;
use reth_payload_builder::database::SyncCachedReads as CachedReads;
use serde::Deserialize;
use std::{os::unix::fs::lchown, time::{Duration, Instant}};
use tracing::{error, info_span, trace};
Expand Down
36 changes: 19 additions & 17 deletions crates/rbuilder/src/building/evm_inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use alloy_primitives::{Address, B256, U256};
use reth_primitives::TransactionSignedEcRecovered;
use revm::{
interpreter::{opcode, CallInputs, CallOutcome, Interpreter},
Database, EvmContext, Inspector,
Database, EvmContext, Inspector, SyncDatabase,
};
use revm_inspectors::access_list::AccessListInspector;
use revm_primitives::ChainAddress;

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SlotKey {
pub address: Address,
pub address: ChainAddress,
pub key: B256,
}

Expand All @@ -23,20 +24,20 @@ pub struct UsedStateTrace {
/// write slot values contains last write
pub written_slot_values: HashMap<SlotKey, B256>,
/// balance of first read
pub read_balances: HashMap<Address, U256>,
pub read_balances: HashMap<ChainAddress, U256>,
/// number of `wei` sent or received during execution
pub received_amount: HashMap<Address, U256>,
pub sent_amount: HashMap<Address, U256>,
pub created_contracts: Vec<Address>,
pub destructed_contracts: Vec<Address>,
pub received_amount: HashMap<ChainAddress, U256>,
pub sent_amount: HashMap<ChainAddress, U256>,
pub created_contracts: Vec<ChainAddress>,
pub destructed_contracts: Vec<ChainAddress>,
}

#[derive(Debug, Clone, Default)]
enum NextStepAction {
#[default]
None,
ReadSloadKeyResult(B256),
ReadBalanceResult(Address),
ReadBalanceResult(ChainAddress),
}

#[derive(Debug)]
Expand All @@ -57,16 +58,17 @@ impl<'a> UsedStateEVMInspector<'a> {
/// Txs with the same nonce are in conflict and origin address is EOA that does not have storage.
/// We convert nonce change to the slot 0 read and write of the signer
fn use_tx_nonce(&mut self, tx: &TransactionSignedEcRecovered) {
let signer = ChainAddress(tx.chain_id().unwrap(), tx.signer());
self.used_state_trace.read_slot_values.insert(
SlotKey {
address: tx.signer(),
address: signer,
key: Default::default(),
},
U256::from(tx.nonce()).into(),
);
self.used_state_trace.written_slot_values.insert(
SlotKey {
address: tx.signer(),
address: signer,
key: Default::default(),
},
U256::from(tx.nonce() + 1).into(),
Expand All @@ -76,7 +78,7 @@ impl<'a> UsedStateEVMInspector<'a> {

impl<'a, DB> Inspector<DB> for UsedStateEVMInspector<'a>
where
DB: Database,
DB: SyncDatabase,
{
fn step(&mut self, interpreter: &mut Interpreter, _: &mut EvmContext<DB>) {
match std::mem::take(&mut self.next_step_action) {
Expand Down Expand Up @@ -134,7 +136,7 @@ where
opcode::BALANCE => {
if let Ok(addr) = interpreter.stack().peek(0) {
let addr = Address::from_word(B256::from(addr.to_be_bytes()));
self.next_step_action = NextStepAction::ReadBalanceResult(addr);
self.next_step_action = NextStepAction::ReadBalanceResult(ChainAddress(interpreter.chain_id, addr));
}
}
opcode::SELFBALANCE => {
Expand Down Expand Up @@ -165,17 +167,17 @@ where

fn create_end(
&mut self,
_: &mut EvmContext<DB>,
ctx: &mut EvmContext<DB>,
_: &revm::interpreter::CreateInputs,
outcome: revm::interpreter::CreateOutcome,
) -> revm::interpreter::CreateOutcome {
if let Some(addr) = outcome.address {
self.used_state_trace.created_contracts.push(addr);
self.used_state_trace.created_contracts.push(ChainAddress(ctx.env.cfg.chain_id, addr));
}
outcome
}

fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
fn selfdestruct(&mut self, contract: ChainAddress, target: ChainAddress, value: U256) {
// selfdestruct can be called multiple times during transaction execution
if self
.used_state_trace
Expand Down Expand Up @@ -238,7 +240,7 @@ impl<'a> RBuilderEVMInspector<'a> {

impl<'a, DB> Inspector<DB> for RBuilderEVMInspector<'a>
where
DB: Database,
DB: SyncDatabase,
UsedStateEVMInspector<'a>: Inspector<DB>,
{
#[inline]
Expand Down Expand Up @@ -277,7 +279,7 @@ where
}

#[inline]
fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
fn selfdestruct(&mut self, contract: ChainAddress, target: ChainAddress, value: U256) {
if let Some(used_state_inspector) = &mut self.used_state_inspector {
used_state_inspector.selfdestruct(contract, target, value)
}
Expand Down
16 changes: 9 additions & 7 deletions crates/rbuilder/src/building/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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 crate::{
primitives::{Order, OrderId, SimValue, SimulatedOrder, TransactionSignedEcRecoveredWithBlobs},
Expand Down Expand Up @@ -41,7 +42,7 @@ use reth_evm::system_calls::{
};
use reth_evm_ethereum::{eip6110::parse_deposits_from_receipts, revm_spec, EthEvmConfig};
use reth_node_api::PayloadBuilderAttributes;
use reth_payload_builder::{database::CachedReads, EthPayloadBuilderAttributes};
use reth_payload_builder::{database::SyncCachedReads as CachedReads, EthPayloadBuilderAttributes};
use revm::{
db::states::bundle_state::BundleRetention::{self, PlainState},
primitives::{BlobExcessGasAndPrice, BlockEnv, CfgEnvWithHandlerCfg, SpecId},
Expand Down Expand Up @@ -100,7 +101,7 @@ impl BlockBuildingContext {
)
.expect("PayloadBuilderAttributes::try_new");
let (initialized_cfg, mut block_env) = attributes.cfg_and_block_env(&chain_spec, parent);
block_env.coinbase = signer.address;
block_env.coinbase = ChainAddress(chain_spec.chain.id(), signer.address);
if let Some(desired_limit) = prefer_gas_limit {
block_env.gas_limit =
U256::from(calc_gas_limit(block_env.gas_limit.to(), desired_limit));
Expand Down Expand Up @@ -172,7 +173,7 @@ impl BlockBuildingContext {
};
let block_env = BlockEnv {
number: U256::from(block_number),
coinbase,
coinbase: ChainAddress(chain_spec.chain.id(), coinbase),
timestamp: U256::from(onchain_block.header.timestamp),
difficulty: onchain_block.header.difficulty,
prevrandao: onchain_block.header.mix_hash,
Expand Down Expand Up @@ -250,7 +251,7 @@ impl BlockBuildingContext {

pub fn modify_use_suggested_fee_recipient_as_coinbase(&mut self) {
self.builder_signer = None;
self.block_env.coinbase = self.attributes.suggested_fee_recipient;
self.block_env.coinbase = ChainAddress(self.chain_spec.chain.id(), self.attributes.suggested_fee_recipient);
}

pub fn timestamp(&self) -> OffsetDateTime {
Expand All @@ -263,7 +264,7 @@ impl BlockBuildingContext {
}

pub fn coinbase_is_suggested_fee_recipient(&self) -> bool {
self.block_env.coinbase == self.attributes.suggested_fee_recipient
self.block_env.coinbase == ChainAddress(self.chain_spec.chain.id(), self.attributes.suggested_fee_recipient)
}
}

Expand Down Expand Up @@ -552,7 +553,7 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
//println!("insert_proposer_payout_tx: builder_signer: {:?}", builder_signer);
self.free_reserved_gas();
let nonce = state
.nonce(builder_signer.address)
.nonce(ChainAddress(ctx.chain_spec.chain.id(), builder_signer.address))
.map_err(CriticalCommitOrderError::Reth)?;
let tx = create_payout_tx(
ctx.chain_spec.as_ref(),
Expand Down Expand Up @@ -649,6 +650,7 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {

let (cached_reads, bundle) = state.clone_bundle_and_cache();
let execution_outcome = ExecutionOutcome::new(
Some(ctx.chain_spec.chain.id()),
bundle,
Receipts::from(vec![self
.receipts
Expand Down Expand Up @@ -715,7 +717,7 @@ impl<Tracer: SimulationTracer> PartialBlock<Tracer> {
let header = Header {
parent_hash: ctx.attributes.parent,
ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: ctx.block_env.coinbase,
beneficiary: ctx.block_env.coinbase.1,
state_root,
transactions_root,
receipts_root,
Expand Down
Loading

0 comments on commit f5c4aef

Please sign in to comment.