Skeleton primitives and interface crate (#13)

* wip interface primitives

* wip

* Integrate it inside rpc- crates

* fmt

* move tx to mod.rs

* Add interfaces, executor to toml

* Added nits, comments fix
This commit is contained in:
rakita
2022-10-06 14:48:57 +02:00
committed by GitHub
parent 849e8ae518
commit bc30cbce61
28 changed files with 323 additions and 18 deletions

14
Cargo.lock generated
View File

@ -1274,6 +1274,19 @@ dependencies = [
"libmdbx",
]
[[package]]
name = "reth-executor"
version = "0.1.0"
[[package]]
name = "reth-interfaces"
version = "0.1.0"
dependencies = [
"async-trait",
"reth-primitives",
"thiserror",
]
[[package]]
name = "reth-p2p"
version = "0.1.0"
@ -1282,6 +1295,7 @@ version = "0.1.0"
name = "reth-primitives"
version = "0.1.0"
dependencies = [
"bytes",
"ethers-core",
]

View File

@ -6,13 +6,15 @@ edition = "2021"
[workspace]
members = [
"crate-template",
"crates/db",
"crates/executor",
"crates/interfaces",
"crates/net/p2p",
"crates/net/rpc",
"crates/net/rpc-api",
"crates/net/rpc-types",
"crates/primitives",
"crates/stages",
"crates/db"
]
[dependencies]

View File

@ -0,0 +1,9 @@
[package]
name = "reth-executor"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/foundry-rs/reth"
readme = "README.md"
[dependencies]

View File

@ -0,0 +1,8 @@
#![warn(missing_debug_implementations, missing_docs, unreachable_pub)]
#![deny(unused_must_use, rust_2018_idioms)]
#![doc(test(
no_crate_inject,
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
))]
//! Reth executor executes transaction in block of data.

View File

@ -0,0 +1,12 @@
[package]
name = "reth-interfaces"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/foundry-rs/reth"
readme = "README.md"
[dependencies]
reth-primitives = { path = "../primitives" }
async-trait = "0.1.57"
thiserror = "1.0.37"

View File

@ -0,0 +1,20 @@
use async_trait::async_trait;
use reth_primitives::Block;
use thiserror::Error;
/// Takes block and executes it, returns error
#[async_trait]
pub trait BlockExecutor {
/// Execute block
async fn execute(&self, _block: Block) -> Error {
Error::VerificationFailed
}
}
/// BlockExecutor Errors
#[derive(Error, Debug, Clone)]
pub enum Error {
/// Example of error
#[error("Example of error.")]
VerificationFailed,
}

View File

@ -0,0 +1,11 @@
#![warn(missing_debug_implementations, missing_docs, unreachable_pub)]
#![deny(unused_must_use, rust_2018_idioms)]
#![doc(test(
no_crate_inject,
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
))]
//! Reth interface bindings
/// Block Execution traits.
pub mod executor;

View File

@ -1,5 +1,8 @@
use jsonrpsee::{core::RpcResult as Result, proc_macros::rpc};
use reth_primitives::{BlockId, Bytes, H256};
use reth_primitives::{
rpc::{BlockId, Bytes},
H256,
};
use reth_rpc_types::RichBlock;
/// Debug rpc interface.

View File

@ -3,8 +3,8 @@ use jsonrpsee::{
proc_macros::rpc,
};
use reth_primitives::{
transaction::eip2930::AccessListWithGasUsed, Address, BlockId, BlockNumber, Bytes, H256, H64,
U256, U64,
rpc::{transaction::eip2930::AccessListWithGasUsed, BlockId},
Address, BlockNumber, Bytes, H256, H64, U256, U64,
};
use reth_rpc_types::{
CallRequest, EIP1186AccountProofResponse, FeeHistory, Index, RichBlock, SyncStatus,

View File

@ -1,6 +1,6 @@
use jsonrpsee::{core::RpcResult as Result, proc_macros::rpc};
use reth_primitives::U256;
use reth_rpc_types::{Filter, FilterChanges, Index, Log};
use reth_primitives::{rpc::Filter, U256};
use reth_rpc_types::{FilterChanges, Index, Log};
/// Rpc Interface for poll-based ethereum filter API.
#[rpc(server)]

View File

@ -1,5 +1,5 @@
use jsonrpsee::{core::RpcResult as Result, proc_macros::rpc};
use reth_primitives::{BlockId, Bytes, H256};
use reth_primitives::{rpc::BlockId, Bytes, H256};
use reth_rpc_types::{
trace::{filter::TraceFilter, parity::*},
CallRequest, Index,

View File

@ -1,4 +1,5 @@
use reth_primitives::{Address, Bloom, Bytes, Transaction, H256, H64, U256};
use crate::Transaction;
use reth_primitives::{rpc::H64, Address, Bloom, Bytes, H256, U256};
use serde::{ser::Error, Serialize, Serializer};
use std::{collections::BTreeMap, ops::Deref};

View File

@ -1,4 +1,4 @@
use reth_primitives::{transaction::eip2930::AccessListItem, Address, Bytes, U256};
use reth_primitives::{rpc::transaction::eip2930::AccessListItem, Address, Bytes, U256};
use serde::Deserialize;
/// Call request

View File

@ -1,6 +1,5 @@
use crate::Log;
use reth_primitives::H256;
pub use reth_primitives::{Filter, FilteredParams};
use serde::{Serialize, Serializer};
/// Response of the `eth_getFilterChanges` RPC.

View File

@ -1,7 +1,7 @@
//! Ethereum types for pub-sub
use crate::{Filter, Log, RichHeader};
use reth_primitives::H256;
use crate::{Log, RichHeader};
use reth_primitives::{rpc::Filter, H256};
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
/// Subscription result.

View File

@ -7,7 +7,7 @@ pub use request::TransactionRequest;
pub use typed::*;
use reth_primitives::{
transaction::eip2930::AccessListItem, Address, Bytes, H256, H512, U256, U64,
rpc::transaction::eip2930::AccessListItem, Address, Bytes, H256, H512, U256, U64,
};
use serde::Serialize;

View File

@ -1,4 +1,4 @@
use reth_primitives::{Address, Bloom, Log, H256, U256, U64};
use reth_primitives::{rpc::Log, Address, Bloom, H256, U256, U64};
use serde::Serialize;
/// Transaction receipt

View File

@ -2,7 +2,7 @@ use crate::eth::transaction::typed::{
EIP1559TransactionRequest, EIP2930TransactionRequest, LegacyTransactionRequest,
TransactionKind, TypedTransactionRequest,
};
use reth_primitives::{transaction::eip2930::AccessListItem, Address, Bytes, U256};
use reth_primitives::{rpc::transaction::eip2930::AccessListItem, Address, Bytes, U256};
use serde::{Deserialize, Serialize};
/// Represents _all_ transaction requests received from RPC

View File

@ -4,7 +4,7 @@
//! container type [`TypedTransactionRequest`].
use fastrlp::{RlpDecodable, RlpEncodable};
use reth_primitives::{transaction::eip2930::AccessListItem, Address, Bytes, U256};
use reth_primitives::{rpc::transaction::eip2930::AccessListItem, Address, Bytes, U256};
use serde::{Deserialize, Serialize};
/// Container type for various Ethereum transaction requests

View File

@ -8,4 +8,5 @@ readme = "README.md"
description = "Commonly used types in reth."
[dependencies]
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
bytes = "1.2"

View File

@ -0,0 +1,12 @@
use crate::{Header, Receipt, Transaction};
/// Ethereum full block.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Block {
/// Block header.
pub header: Header,
/// Transactions in this block.
pub body: Vec<Transaction>,
/// Block receipts.
pub receipts: Vec<Receipt>,
}

View File

@ -0,0 +1,100 @@
use crate::{BlockNumber, Bytes, H160, H256, U256};
/// Block header
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Header {
/// The Keccak 256-bit hash of the parent
/// blocks header, in its entirety; formally Hp.
pub parent_hash: H256,
/// The Keccak 256-bit hash of the ommers list portion of this block; formally Ho.
pub ommers_hash: H256,
/// The 160-bit address to which all fees collected from the successful mining of this block
/// be transferred; formally Hc.
pub beneficiary: H160,
/// The Keccak 256-bit hash of the root node of the state trie, after all transactions are
/// executed and finalisations applied; formally Hr.
pub state_root: H256,
/// The Keccak 256-bit hash of the root node of the trie structure populated with each
/// transaction in the transactions list portion of the
/// block; formally Ht.
pub transactions_root: H256,
/// The Keccak 256-bit hash of the root
/// node of the trie structure populated with the receipts of each transaction in the
/// transactions list portion of the block; formally He.
pub receipts_root: H256,
/// The Bloom filter composed from indexable information (logger address and log topics)
/// contained in each log entry from the receipt of each transaction in the transactions list;
/// formally Hb.
pub logs_bloom: H256, // change to Bloom
/// A scalar value corresponding to the difficulty level of this block. This can be calculated
/// from the previous blocks difficulty level and the timestamp; formally Hd.
pub difficulty: U256,
/// A scalar value equal to the number of ancestor blocks. The genesis block has a number of
/// zero; formally Hi.
pub number: BlockNumber,
/// A scalar value equal to the current limit of gas expenditure per block; formally Hl.
pub gas_limit: u64,
/// A scalar value equal to the total gas used in transactions in this block; formally Hg.
pub gas_used: u64,
/// A scalar value equal to the reasonable output of Unixs time() at this blocks inception;
/// formally Hs.
pub timestamp: u64,
/// An arbitrary byte array containing data relevant to this block. This must be 32 bytes or
/// fewer; formally Hx.
pub extra_data: Bytes,
/// A 256-bit hash which, combined with the
/// nonce, proves that a sufficient amount of computation has been carried out on this block;
/// formally Hm.
pub mix_hash: H256,
/// A 64-bit value which, combined with the mixhash, proves that a sufficient amount of
/// computation has been carried out on this block; formally Hn.
pub nonce: u64,
/// A scalar representing EIP1559 base fee which can move up or down each block according
/// to a formula which is a function of gas used in parent block and gas target
/// (block gas limit divided by elasticity multiplier) of parent block.
/// The algorithm results in the base fee per gas increasing when blocks are
/// above the gas target, and decreasing when blocks are below the gas target. The base fee per
/// gas is burned.
pub base_fee_per_gas: Option<u64>,
}
impl Header {
/// Heavy function that will calculate hash of data and will *not* save the change to metadata.
/// Use lock, HeaderLocked and unlock if you need hash to be persistent.
pub fn hash_slow(&self) -> H256 {
todo!()
}
/// Calculate hash and lock the Header so that it can't be changed.
pub fn lock(self) -> HeaderLocked {
let hash = self.hash_slow();
HeaderLocked { header: self, hash }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
/// HeaderLocked that has precalculated hash, use unlock if you want to modify header.
pub struct HeaderLocked {
/// Locked Header fields.
header: Header,
/// Locked Header hash.
hash: H256,
}
impl AsRef<Header> for HeaderLocked {
fn as_ref(&self) -> &Header {
&self.header
}
}
impl HeaderLocked {
/// Extract raw header that can be modified.
pub fn unlock(self) -> Header {
self.header
}
/// Return header/block hash.
pub fn hash(&self) -> H256 {
self.hash
}
}

View File

@ -7,4 +7,25 @@
//! Commonly used types in reth.
pub use ethers_core::types::*;
mod block;
mod header;
mod log;
mod receipt;
mod transaction;
pub use block::Block;
pub use header::{Header, HeaderLocked};
pub use log::Log;
pub use receipt::Receipt;
pub use transaction::{Transaction, TransactionSigned, TxType};
/// Block Number
pub type BlockNumber = u64;
/// Ethereum address
pub type Address = H160;
// NOTE: There is a benefit of using wrapped Bytes as it gives us serde and debug
pub use ethers_core::{
types as rpc,
types::{Bloom, Bytes, H160, H256, H512, H64, U256, U64},
};

View File

@ -0,0 +1,12 @@
use crate::{Address, Bytes, H256};
/// Ethereum Log
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Log {
/// Contract that emitted this log.
pub address: Address,
/// Topics of the log. The number of logs depend on what `LOG` opcode is used.
pub topics: Vec<H256>,
/// Arbitrary length data.
pub data: Bytes,
}

View File

@ -0,0 +1,16 @@
use crate::{Log, TxType, H256};
/// Receipt containing result of transaction execution.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Receipt {
/// Receipt type.
pub tx_type: TxType,
/// If transaction is executed successfully.
pub success: bool,
/// Gas used
pub cumulative_gas_used: u64,
/// Bloom filter.
pub bloom: H256,
/// Log send from contracts.
pub logs: Vec<Log>,
}

View File

@ -0,0 +1,45 @@
mod signature;
mod tx_type;
use signature::Signature;
pub use tx_type::TxType;
/// Raw Transaction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Transaction {
/// Legacy transaciton.
Legacy {
/// Nonce.
nonce: u64,
},
/// Transaction with AccessList.
Eip2930 {
/// nonce.
nonce: u64,
},
/// Transaction with priority fee.
Eip1559 {
/// Nonce.
nonce: u64,
},
}
/// Signed transaction.
#[derive(Debug, Clone)]
pub struct TransactionSigned {
transaction: Transaction,
signature: Signature,
}
impl AsRef<Transaction> for TransactionSigned {
fn as_ref(&self) -> &Transaction {
&self.transaction
}
}
impl TransactionSigned {
/// Transaction signature.
pub fn signature(&self) -> &Signature {
&self.signature
}
}

View File

@ -0,0 +1,9 @@
use crate::H256;
/// Signature still TODO
#[derive(Debug, Clone)]
pub struct Signature {
pub r: H256,
pub s: H256,
pub y_parity: u8,
}

View File

@ -0,0 +1,10 @@
/// Transaction Type
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum TxType {
/// Legacy transaction pre EIP-2929
Legacy = 0,
/// AccessList transaction
EIP2930 = 1,
/// Transaction with Priority fee
EIP1559 = 2,
}