diff --git a/Cargo.lock b/Cargo.lock index 4d162a19a..27fe7dc44 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,17 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "0.7.19" @@ -17,6 +28,12 @@ version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602" +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + [[package]] name = "arrayvec" version = "0.7.2" @@ -610,6 +627,9 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash", +] [[package]] name = "heck" @@ -884,6 +904,9 @@ name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +dependencies = [ + "spin", +] [[package]] name = "lazycell" @@ -987,6 +1010,40 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ae39348c8bc5fbd7f40c727a9925f03517afd2ab27d46702108b6a7e5414c19" +dependencies = [ + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.45" @@ -997,6 +1054,29 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.15" @@ -1016,6 +1096,27 @@ dependencies = [ "libc", ] +[[package]] +name = "num_enum" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf5395665662ef45796a4ff5486c5d41d29e0c09640af4c5f17fd94ee2c119c9" +dependencies = [ + "num_enum_derive", +] + +[[package]] +name = "num_enum_derive" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0498641e53dd6ac1a4f22547548caa6864cc4933784319cd1775271c5a46ce" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "once_cell" version = "1.15.0" @@ -1277,12 +1378,21 @@ dependencies = [ [[package]] name = "reth-executor" version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "reth-interfaces", + "reth-primitives", + "revm", + "thiserror", +] [[package]] name = "reth-interfaces" version = "0.1.0" dependencies = [ "async-trait", + "auto_impl", "reth-primitives", "thiserror", ] @@ -1339,6 +1449,41 @@ dependencies = [ "thiserror", ] +[[package]] +name = "revm" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87344ffd3eec06b568e1fc69c225e4cbd8d68d8d9051b6d2652d596947efa1ce" +dependencies = [ + "arrayref", + "auto_impl", + "bytes", + "hashbrown", + "num_enum", + "primitive-types", + "revm_precompiles", + "rlp", + "sha3", +] + +[[package]] +name = "revm_precompiles" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00e68901326fe20437526cb6d64a2898d2976383b7d222329dfce1717902da50" +dependencies = [ + "bytes", + "hashbrown", + "num", + "once_cell", + "primitive-types", + "ripemd", + "secp256k1", + "sha2", + "sha3", + "substrate-bn", +] + [[package]] name = "rfc6979" version = "0.3.0" @@ -1350,6 +1495,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "ripemd" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd124222d17ad93a644ed9d011a40f4fb64aa54275c08cc216524a9ea82fb09f" +dependencies = [ + "digest 0.10.5", +] + [[package]] name = "rlp" version = "0.5.1" @@ -1435,6 +1589,24 @@ dependencies = [ "zeroize", ] +[[package]] +name = "secp256k1" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7649a0b3ffb32636e60c7ce0d70511eda9c52c658cd0634e194d5a19943aeff" +dependencies = [ + "secp256k1-sys", +] + +[[package]] +name = "secp256k1-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7058dc8eaf3f2810d7828680320acda0b25a288f6d288e19278e249bbf74226b" +dependencies = [ + "cc", +] + [[package]] name = "semver" version = "1.0.14" @@ -1562,6 +1734,12 @@ dependencies = [ "sha-1", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "spki" version = "0.6.0" @@ -1600,6 +1778,19 @@ dependencies = [ "syn", ] +[[package]] +name = "substrate-bn" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b5bbfa79abbae15dd642ea8176a21a635ff3c00059961d1ea27ad04e5b441c" +dependencies = [ + "byteorder", + "crunchy", + "lazy_static", + "rand", + "rustc-hex", +] + [[package]] name = "subtle" version = "2.4.1" diff --git a/Cargo.toml b/Cargo.toml index 0bba41a19..8d0e69f86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,4 @@ members = [ [[bin]] path = "bin/reth.rs" -name = "reth" \ No newline at end of file +name = "reth" diff --git a/crates/executor/Cargo.toml b/crates/executor/Cargo.toml index f1fe8ba34..5d829609d 100644 --- a/crates/executor/Cargo.toml +++ b/crates/executor/Cargo.toml @@ -7,3 +7,9 @@ repository = "https://github.com/foundry-rs/reth" readme = "README.md" [dependencies] +revm = "2.1" +reth-primitives = { path = "../primitives" } +reth-interfaces = { path = "../interfaces" } +async-trait = "0.1.57" +thiserror = "1.0.37" +anyhow = "1.0" diff --git a/crates/executor/src/cfg.rs b/crates/executor/src/cfg.rs new file mode 100644 index 000000000..61160412d --- /dev/null +++ b/crates/executor/src/cfg.rs @@ -0,0 +1,6 @@ +/// Configuration for executor (TODO) +#[derive(Debug, Clone)] +pub struct Config { + /// Example + pub example: bool, +} diff --git a/crates/executor/src/executor.rs b/crates/executor/src/executor.rs new file mode 100644 index 000000000..ef8b19651 --- /dev/null +++ b/crates/executor/src/executor.rs @@ -0,0 +1,37 @@ +#![allow(missing_debug_implementations)] + +use crate::Config; +use reth_interfaces::{ + consensus::Consensus, + executor::{BlockExecutor, Error, ExecutorDb}, +}; +use reth_primitives::Block; + +/// Main block executor +pub struct Executor { + /// Configuration, Spec and optional flags. + config: Config, + /// Database + db: Box, + /// Consensus + consensus: Box, +} + +impl Executor { + /// Create new Executor + pub fn new(config: Config, db: Box, consensus: Box) -> Self { + Self { config, db, consensus } + } + + /// Verify block (TODO) + pub fn verify(&self, block: &Block) -> Result<(), Error> { + // TODO example + let _ = self.consensus.validate_header(&block.header); + if self.config.example { + let _block_exist = self.db.get_block(block.header.number); + } + Err(Error::VerificationFailed) + } +} + +impl BlockExecutor for Executor {} diff --git a/crates/executor/src/lib.rs b/crates/executor/src/lib.rs index 639ddcaea..5961fac18 100644 --- a/crates/executor/src/lib.rs +++ b/crates/executor/src/lib.rs @@ -6,3 +6,9 @@ ))] //! Reth executor executes transaction in block of data. + +mod cfg; +/// Executor +pub mod executor; + +pub use cfg::Config; diff --git a/crates/interfaces/Cargo.toml b/crates/interfaces/Cargo.toml index fc66d9fed..ba97fa617 100644 --- a/crates/interfaces/Cargo.toml +++ b/crates/interfaces/Cargo.toml @@ -9,4 +9,5 @@ readme = "README.md" [dependencies] reth-primitives = { path = "../primitives" } async-trait = "0.1.57" -thiserror = "1.0.37" \ No newline at end of file +thiserror = "1.0.37" +auto_impl = "1.0" \ No newline at end of file diff --git a/crates/interfaces/src/consensus.rs b/crates/interfaces/src/consensus.rs new file mode 100644 index 000000000..6306e1908 --- /dev/null +++ b/crates/interfaces/src/consensus.rs @@ -0,0 +1,21 @@ +use async_trait::async_trait; +use reth_primitives::Header; +use thiserror::Error; + +/// Consensus is a protocol that chooses canonical chain. +/// We are checking validity of block header here. +#[async_trait] +pub trait Consensus { + /// Validate if header is correct and follows consensus specification + fn validate_header(&self, _header: &Header) -> Result<(), Error> { + Ok(()) + } +} + +/// Consensus errors (TODO) +#[derive(Error, Debug)] +pub enum Error { + /// Explanatory + #[error("Example of consensus error")] + ConsensusError, +} diff --git a/crates/interfaces/src/executor.rs b/crates/interfaces/src/executor.rs index 379c0112d..75ee4726d 100644 --- a/crates/interfaces/src/executor.rs +++ b/crates/interfaces/src/executor.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use reth_primitives::Block; +use reth_primitives::{Address, Block, BlockNumber, StorageKey, StorageValue}; use thiserror::Error; /// Takes block and executes it, returns error @@ -18,3 +18,12 @@ pub enum Error { #[error("Example of error.")] VerificationFailed, } + +/// Function needed for executor +pub trait ExecutorDb { + /// Get Block by BlockNumber. + fn get_block(&self, _height: BlockNumber) -> Option; + + /// Get storage. + fn get_storage(&self, account: Address, storage_key: StorageKey) -> Option; +} diff --git a/crates/interfaces/src/lib.rs b/crates/interfaces/src/lib.rs index 07c795a08..c7624b35d 100644 --- a/crates/interfaces/src/lib.rs +++ b/crates/interfaces/src/lib.rs @@ -9,3 +9,6 @@ /// Block Execution traits. pub mod executor; + +/// Consensus traits. +pub mod consensus; diff --git a/crates/primitives/src/account.rs b/crates/primitives/src/account.rs new file mode 100644 index 000000000..fae49f16b --- /dev/null +++ b/crates/primitives/src/account.rs @@ -0,0 +1,9 @@ + + + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Account { + pub nonce: u64, + pub balance: U256, + pub bytecode_hash: H256, +} \ No newline at end of file diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 1c07d5274..e6d3a8c3a 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -19,10 +19,20 @@ pub use log::Log; pub use receipt::Receipt; pub use transaction::{Transaction, TransactionSigned, TxType}; -/// Block Number +/// Block Number is height of chain pub type BlockNumber = u64; /// Ethereum address pub type Address = H160; +/// BlockId is Keccak hash of the header +pub type BlockID = H256; +/// TxHash is Kecack hash of rlp encoded signed transaction +pub type TxHash = H256; + +/// Storage Key +pub type StorageKey = H256; + +/// Storage value +pub type StorageValue = H256; // NOTE: There is a benefit of using wrapped Bytes as it gives us serde and debug pub use ethers_core::{