mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
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:
9
crates/executor/Cargo.toml
Normal file
9
crates/executor/Cargo.toml
Normal 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]
|
||||
8
crates/executor/src/lib.rs
Normal file
8
crates/executor/src/lib.rs
Normal 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.
|
||||
12
crates/interfaces/Cargo.toml
Normal file
12
crates/interfaces/Cargo.toml
Normal 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"
|
||||
20
crates/interfaces/src/executor.rs
Normal file
20
crates/interfaces/src/executor.rs
Normal 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,
|
||||
}
|
||||
11
crates/interfaces/src/lib.rs
Normal file
11
crates/interfaces/src/lib.rs
Normal 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;
|
||||
@ -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.
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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)]
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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};
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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"
|
||||
12
crates/primitives/src/block.rs
Normal file
12
crates/primitives/src/block.rs
Normal 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>,
|
||||
}
|
||||
100
crates/primitives/src/header.rs
Normal file
100
crates/primitives/src/header.rs
Normal 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
|
||||
/// block’s 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 block’s 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 Unix’s time() at this block’s 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
|
||||
}
|
||||
}
|
||||
@ -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},
|
||||
};
|
||||
|
||||
12
crates/primitives/src/log.rs
Normal file
12
crates/primitives/src/log.rs
Normal 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,
|
||||
}
|
||||
16
crates/primitives/src/receipt.rs
Normal file
16
crates/primitives/src/receipt.rs
Normal 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>,
|
||||
}
|
||||
45
crates/primitives/src/transaction/mod.rs
Normal file
45
crates/primitives/src/transaction/mod.rs
Normal 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
|
||||
}
|
||||
}
|
||||
9
crates/primitives/src/transaction/signature.rs
Normal file
9
crates/primitives/src/transaction/signature.rs
Normal 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,
|
||||
}
|
||||
10
crates/primitives/src/transaction/tx_type.rs
Normal file
10
crates/primitives/src/transaction/tx_type.rs
Normal 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,
|
||||
}
|
||||
Reference in New Issue
Block a user