mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-04-30 03:01:58 -04: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:
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