Skip to content

Commit

Permalink
feat: support taiko protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
johntaiko committed Jun 26, 2024
1 parent d20c700 commit 0eb09a3
Show file tree
Hide file tree
Showing 25 changed files with 2,011 additions and 699 deletions.
2,428 changes: 1,774 additions & 654 deletions Cargo.lock

Large diffs are not rendered by default.

32 changes: 20 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ reth-optimism-primitives = { path = "crates/optimism/primitives" }
reth-payload-builder = { path = "crates/payload/builder" }
reth-payload-primitives = { path = "crates/payload/primitives" }
reth-payload-validator = { path = "crates/payload/validator" }
reth-primitives = { path = "crates/primitives" }
reth-primitives = { path = "crates/primitives", features = ["taiko"] }
reth-primitives-traits = { path = "crates/primitives-traits" }
reth-provider = { path = "crates/storage/provider" }
reth-prune = { path = "crates/prune/prune" }
Expand Down Expand Up @@ -342,7 +342,9 @@ reth-trie-parallel = { path = "crates/trie/parallel" }
revm = { version = "9.0.0", features = [
"std",
"secp256k1",
"serde",
"blst",
"taiko",
], default-features = false }
revm-primitives = { version = "4.0.0", features = [
"std",
Expand All @@ -360,11 +362,15 @@ alloy-rpc-types = { version = "0.1", default-features = false, features = [
"eth",
] }
alloy-rpc-types-anvil = { version = "0.1", default-features = false }
alloy-rpc-types-beacon = { version = "0.1", default-features = false }
alloy-rpc-types-beacon = { version = "0.1", default-features = false, features = [
"taiko",
] }
alloy-rpc-types-admin = { version = "0.1", default-features = false }
alloy-rpc-types-txpool = { version = "0.1", default-features = false }
alloy-serde = { version = "0.1", default-features = false }
alloy-rpc-types-engine = { version = "0.1", default-features = false }
alloy-rpc-types-engine = { version = "0.1", default-features = false, features = [
"taiko",
] }
alloy-rpc-types-eth = { version = "0.1", default-features = false }
alloy-rpc-types-trace = { version = "0.1", default-features = false }
alloy-genesis = { version = "0.1", default-features = false }
Expand Down Expand Up @@ -506,13 +512,15 @@ raiko-lib = { git = "https://github.com/taikoxyz/raiko.git", branch = "reth-wip"
anyhow = "1.0"

[patch.crates-io]
revm = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko", default-features = false, features = [
"std",
"serde",
"taiko",
]}
revm-interpreter = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko" }
revm-precompile = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko" }
revm-primitives = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko"}
alloy-rpc-types-engine = { git = "https://github.com/taikoxyz/alloy.git", branch = "v0.1.1-taiko" }
alloy-rpc-types-beacon = { git = "https://github.com/taikoxyz/alloy.git", branch = "v0.1.1-taiko" }
alloy-eips = { git = "https://github.com/taikoxyz/alloy.git", branch = "v0.1.1-taiko" }
revm = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko-reth" }
revm-interpreter = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko-reth" }
revm-precompile = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko-reth" }
revm-primitives = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko-reth" }
c-kzg = { git = "https://github.com/brechtpd/c-kzg-4844", branch = "for-alpha7" }
secp256k1 = { git = "https://github.com/CeciliaZ030/rust-secp256k1", branch = "sp1-patch" }
secp256k1 = { git = "https://github.com/CeciliaZ030/rust-secp256k1", branch = "sp1-patch" }

[patch.'https://github.com/bluealloy/revm.git']
revm = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko-reth" }
2 changes: 2 additions & 0 deletions crates/consensus/debug-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ fn rich_block_to_execution_payload_v3(block: RichBlock) -> ExecutionNewPayload {
buffer.into()
})
.collect(),
tx_hash: Default::default(),
withdrawals_hash: Default::default(),
},
withdrawals: block.withdrawals.clone().unwrap_or_default(),
},
Expand Down
49 changes: 35 additions & 14 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
use crate::{
dao_fork::{DAO_HARDFORK_BENEFICIARY, DAO_HARDKFORK_ACCOUNTS},
taiko::{check_anchor_tx, TaikoData}, EthEvmConfig,
taiko::{check_anchor_tx, TaikoData},
EthEvmConfig,
};
use anyhow::Result;
use reth_chainspec::{ChainSpec, MAINNET};
pub use reth_ethereum_consensus::validate_block_post_execution;
use reth_evm::{
Expand All @@ -25,15 +27,13 @@ use reth_revm::{
apply_beacon_root_contract_call, apply_blockhashes_update,
apply_withdrawal_requests_contract_call, post_block_balance_increments,
},
Evm, State,
JournaledState,
Evm, JournaledState, State,
};
use revm_primitives::{
db::{Database, DatabaseCommit}, Address, BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState,
EVMError, HashSet,
db::{Database, DatabaseCommit},
Address, BlockEnv, CfgEnvWithHandlerCfg, EVMError, EnvWithHandlerCfg, HashSet, ResultAndState,
};
use std::sync::Arc;
use anyhow::Result;

/// Provides executors to execute regular ethereum blocks
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -185,7 +185,9 @@ where
continue;
}
// In all other cases, the tx needs to have a valid signature
return Err(BlockExecutionError::CanonicalRevert { inner: "invalid tx".to_string() });
return Err(BlockExecutionError::CanonicalRevert {
inner: "invalid tx".to_string(),
});
}

// The sum of the transaction’s gas limit, Tg, and the gas utilized in this block prior,
Expand Down Expand Up @@ -219,7 +221,8 @@ where
});
if res.is_err() {
// Clear the state for the next tx
evm.context.evm.journaled_state = JournaledState::new(evm.context.evm.journaled_state.spec, HashSet::new());
evm.context.evm.journaled_state =
JournaledState::new(evm.context.evm.journaled_state.spec, HashSet::new());

if optimistic {
continue;
Expand All @@ -236,11 +239,13 @@ where
println!("Invalid tx at {}: {:?}", idx, invalid_transaction);
// skip the tx
continue;
},
}
_ => {
// any other error is not allowed
return Err(BlockExecutionError::Validation(BlockValidationError::EVM { hash, error }));
},
return Err(BlockExecutionError::Validation(
BlockValidationError::EVM { hash, error },
));
}
},
_ => {
// Any other type of error is not allowed
Expand Down Expand Up @@ -307,7 +312,12 @@ pub struct EthBlockExecutor<EvmConfig, DB> {
impl<EvmConfig, DB> EthBlockExecutor<EvmConfig, DB> {
/// Creates a new Ethereum block executor.
pub const fn new(chain_spec: Arc<ChainSpec>, evm_config: EvmConfig, state: State<DB>) -> Self {
Self { executor: EthEvmExecutor { chain_spec, evm_config }, state, optimistic: false, taiko_data: None }
Self {
executor: EthEvmExecutor { chain_spec, evm_config },
state,
optimistic: false,
taiko_data: None,
}
}

/// Optimistic execution
Expand Down Expand Up @@ -376,7 +386,12 @@ where
let env = self.evm_env_for_block(&block.header, total_difficulty);
let output = {
let evm = self.executor.evm_config.evm_with_env(&mut self.state, env);
self.executor.execute_state_transitions(block, evm, self.optimistic, self.taiko_data.clone())
self.executor.execute_state_transitions(
block,
evm,
self.optimistic,
self.taiko_data.clone(),
)
}?;

// 3. apply post execution changes
Expand Down Expand Up @@ -456,7 +471,13 @@ where
// NOTE: we need to merge keep the reverts for the bundle retention
self.state.merge_transitions(BundleRetention::Reverts);

Ok(BlockExecutionOutput { state: self.state.take_bundle(), receipts, requests, gas_used, db: self.state })
Ok(BlockExecutionOutput {
state: self.state.take_bundle(),
receipts,
requests,
gas_used,
db: Some(self.state),
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub struct BlockExecutionOutput<T, DB> {
/// The total gas used by the block.
pub gas_used: u64,
/// The full state.
pub db: State<DB>,
pub db: Option<State<DB>>,
}

/// A helper type for ethereum block inputs that consists of a block and the total difficulty.
Expand Down
1 change: 1 addition & 0 deletions crates/evm/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl<DB> Executor<DB> for MockExecutorProvider {
receipts: receipts.into_iter().flatten().flatten().collect(),
requests: requests.into_iter().flatten().collect(),
gas_used: 0,
db: None,
})
}
}
Expand Down
32 changes: 26 additions & 6 deletions crates/net/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ discv5.workspace = true
# async/futures
futures.workspace = true
pin-project.workspace = true
tokio = { workspace = true, features = ["io-util", "net", "macros", "rt-multi-thread", "time"] }
tokio = { workspace = true, features = [
"io-util",
"net",
"macros",
"rt-multi-thread",
"time",
] }
tokio-stream.workspace = true
tokio-util = { workspace = true, features = ["codec"] }

Expand All @@ -60,7 +66,11 @@ fnv = "1.0"
thiserror.workspace = true
parking_lot.workspace = true
rand.workspace = true
secp256k1 = { workspace = true, features = ["global-context", "rand-std", "recovery"] }
secp256k1 = { workspace = true, features = [
"global-context",
"rand-std",
"recovery",
] }
derive_more.workspace = true
schnellru.workspace = true
itertools.workspace = true
Expand All @@ -83,7 +93,7 @@ reth-transaction-pool = { workspace = true, features = ["test-utils"] }

# alloy deps for testing against nodes
alloy-node-bindings.workspace = true
alloy-provider= { workspace = true, features = ["admin-api"] }
alloy-provider = { workspace = true, features = ["admin-api"] }

# misc
serial_test.workspace = true
Expand All @@ -94,9 +104,19 @@ pprof = { workspace = true, features = ["criterion", "flamegraph"] }
criterion = { workspace = true, features = ["async_tokio", "html_reports"] }

[features]
default = ["serde"]
serde = ["dep:serde", "dep:humantime-serde", "secp256k1/serde", "enr/serde", "dep:serde_json"]
test-utils = ["reth-provider/test-utils", "dep:tempfile", "reth-transaction-pool/test-utils"]
default = ["serde", "reth-chainspec/network"]
serde = [
"dep:serde",
"dep:humantime-serde",
"secp256k1/serde",
"enr/serde",
"dep:serde_json",
]
test-utils = [
"reth-provider/test-utils",
"dep:tempfile",
"reth-transaction-pool/test-utils",
]
geth-tests = []

[[bench]]
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/revm/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ pub fn calculate_intrinsic_gas_after_merge(
is_shanghai: bool,
) -> u64 {
let spec_id = if is_shanghai { SpecId::SHANGHAI } else { SpecId::MERGE };
validate_initial_tx_gas(spec_id, input, kind.is_create(), access_list, &[])
validate_initial_tx_gas(spec_id, input, kind.is_create(), access_list)
}
2 changes: 0 additions & 2 deletions crates/primitives/src/revm/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ fn fill_tx_env_with_system_contract_call(
// blob fields can be None for this tx
blob_hashes: Vec::new(),
max_fee_per_blob_gas: None,
eof_initcodes: Vec::new(),
eof_initcodes_hashed: std::collections::HashMap::new(),
#[cfg(feature = "optimism")]
optimism: OptimismFields {
source_hash: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/state_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use reth_storage_errors::provider::ProviderError;
use revm::{
interpreter::Host,
primitives::{
Account, AccountInfo, Bytecode, StorageSlot as EvmStorageSlot, ExecutionResult, FixedBytes, ResultAndState,
Account, AccountInfo, Bytecode, EvmStorageSlot, ExecutionResult, FixedBytes, ResultAndState,
},
Database, DatabaseCommit, Evm,
};
Expand Down
8 changes: 8 additions & 0 deletions crates/rpc/rpc-types-compat/src/engine/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ pub fn block_to_payload_v1(value: SealedBlock) -> ExecutionPayloadV1 {
base_fee_per_gas: U256::from(value.base_fee_per_gas.unwrap_or_default()),
block_hash: value.hash(),
transactions,
tx_hash: Default::default(),
withdrawals_hash: Default::default(),
}
}

Expand All @@ -172,6 +174,8 @@ pub fn block_to_payload_v2(value: SealedBlock) -> ExecutionPayloadV2 {
base_fee_per_gas: U256::from(value.base_fee_per_gas.unwrap_or_default()),
block_hash: value.hash(),
transactions,
tx_hash: Default::default(),
withdrawals_hash: Default::default(),
},
withdrawals: value.withdrawals.unwrap_or_default().into_inner(),
}
Expand Down Expand Up @@ -201,6 +205,8 @@ pub fn block_to_payload_v3(value: SealedBlock) -> (ExecutionPayloadV3, Option<B2
base_fee_per_gas: U256::from(value.base_fee_per_gas.unwrap_or_default()),
block_hash: value.hash(),
transactions,
tx_hash: Default::default(),
withdrawals_hash: Default::default(),
},
withdrawals: value.withdrawals.unwrap_or_default().into_inner(),
},
Expand Down Expand Up @@ -386,6 +392,8 @@ pub fn execution_payload_from_sealed_block(value: SealedBlock) -> ExecutionPaylo
base_fee_per_gas: U256::from(value.base_fee_per_gas.unwrap_or_default()),
block_hash: value.hash(),
transactions,
tx_hash: Default::default(),
withdrawals_hash: Default::default(),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/rpc/rpc/src/eth/revm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ pub fn create_txn_env(block_env: &BlockEnv, request: TransactionRequest) -> EthR
#[cfg(feature = "optimism")]
optimism: OptimismFields { enveloped_tx: Some(Bytes::new()), ..Default::default() },
taiko: revm_primitives::TaikoFields::default(),
..Default::default()
};

Ok(env)
Expand Down
2 changes: 2 additions & 0 deletions crates/storage/codecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ reth-codecs-derive = { path = "./derive", default-features = false }

# eth
alloy-consensus = { workspace = true, optional = true }
alloy-rpc-types-engine = { workspace = true, optional = true }
alloy-eips = { workspace = true, optional = true }
alloy-genesis = { workspace = true, optional = true }
alloy-primitives.workspace = true
Expand Down Expand Up @@ -46,5 +47,6 @@ alloy = [
"dep:alloy-eips",
"dep:alloy-genesis",
"dep:modular-bitfield",
"dep:alloy-rpc-types-engine",
]
optimism = ["reth-codecs-derive/optimism"]
1 change: 1 addition & 0 deletions crates/storage/codecs/src/alloy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ mod access_list;
mod genesis_account;
mod log;
mod request;
mod taiko;
mod txkind;
mod withdrawal;
42 changes: 42 additions & 0 deletions crates/storage/codecs/src/alloy/taiko.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Native Compact codec impl for Taiko types.
use crate::Compact;
use alloy_primitives::{B256, U256};
use alloy_rpc_types_engine::L1Origin as AlloyL1Origin;

use reth_codecs_derive::main_codec;

#[main_codec]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
struct L1Origin {
pub block_id: U256,
pub l2_block_hash: B256,
pub l1_block_height: U256,
pub l1_block_hash: B256,
}

impl Compact for AlloyL1Origin {
fn to_compact<B>(self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
let l1_origin = L1Origin {
block_id: self.block_id,
l2_block_hash: self.l2_block_hash,
l1_block_height: self.l1_block_height,
l1_block_hash: self.l1_block_hash,
};
l1_origin.to_compact(buf)
}

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (l1_origin, _) = L1Origin::from_compact(buf, len);
let alloy_l1_origin = Self {
block_id: l1_origin.block_id,
l2_block_hash: l1_origin.l2_block_hash,
l1_block_height: l1_origin.l1_block_height,
l1_block_hash: l1_origin.l1_block_hash,
};
(alloy_l1_origin, buf)
}
}
Loading

0 comments on commit 0eb09a3

Please sign in to comment.