reth_primitives_traits/lib.rs
1//! Commonly used types and traits in Reth.
2//!
3//! This crate contains various primitive traits used across reth's components.
4//! It provides the [`Block`] trait which is used to represent a block and all its components.
5//! A [`Block`] is composed of a [`Header`] and a [`BlockBody`]. In ethereum (and optimism), a block
6//! body consists of a list of transactions, a list of uncle headers, and a list of withdrawals. For
7//! optimism, uncle headers and withdrawals are always empty lists.
8//!
9//! ## Feature Flags
10//!
11//! - `arbitrary`: Adds `proptest` and `arbitrary` support for primitive types.
12//! - `op`: Implements the traits for various [op-alloy](https://github.com/alloy-rs/op-alloy)
13//! types.
14//! - `reth-codec`: Enables db codec support for reth types including zstd compression for certain
15//! types.
16//! - `serde`: Adds serde support for all types.
17//! - `secp256k1`: Adds secp256k1 support for transaction signing/recovery. (By default the no-std
18//! friendly `k256` is used)
19//! - `rayon`: Uses `rayon` for parallel transaction sender recovery in [`BlockBody`] by default.
20//! - `serde-bincode-compat` provides helpers for dealing with the `bincode` crate.
21//!
22//! ## Overview
23//!
24//! This crate defines various traits and types that form the foundation of the reth stack.
25//! The top-level trait is [`Block`] which represents a block in the blockchain. A [`Block`] is
26//! composed of a [`Header`] and a [`BlockBody`]. A [`BlockBody`] contains the transactions in the
27//! block any additional data that is part of the block. A [`Header`] contains the metadata of the
28//! block.
29//!
30//! ### Sealing (Hashing)
31//!
32//! The block hash is derived from the [`Header`] and is used to uniquely identify the block. This
33//! operation is referred to as sealing in the context of this crate. Sealing is an expensive
34//! operation. This crate provides various wrapper types that cache the hash of the block to avoid
35//! recomputing it: [`SealedHeader`] and [`SealedBlock`]. All sealed types can be downgraded to
36//! their unsealed counterparts.
37//!
38//! ### Recovery
39//!
40//! The raw consensus transactions that make up a block don't include the sender's address. This
41//! information is recovered from the transaction signature. This operation is referred to as
42//! recovery in the context of this crate and is an expensive operation. The [`RecoveredBlock`]
43//! represents a [`SealedBlock`] with the sender addresses recovered. A [`SealedBlock`] can be
44//! upgraded to a [`RecoveredBlock`] by recovering the sender addresses:
45//! [`SealedBlock::try_recover`]. A [`RecoveredBlock`] can be downgraded to a [`SealedBlock`] by
46//! removing the sender addresses: [`RecoveredBlock::into_sealed_block`].
47//!
48//! #### Naming
49//!
50//! The types in this crate support multiple recovery functions, e.g.
51//! [`SealedBlock::try_recover_unchecked`] and [`SealedBlock::try_recover_unchecked`]. The `_unchecked` suffix indicates that this function recovers the signer _without ensuring that the signature has a low `s` value_, in other words this rule introduced in [EIP-2](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md) is ignored.
52//! Hence this function is necessary when dealing with pre EIP-2 transactions on the ethereum
53//! mainnet. Newer transactions must always be recovered with the regular `recover` functions, see
54//! also [`recover_signer`](crypto::secp256k1::recover_signer).
55//!
56//! ## Bincode serde compatibility
57//!
58//! The [bincode-crate](https://github.com/bincode-org/bincode) is often used by additional tools when sending data over the network.
59//! `bincode` crate doesn't work well with optionally serializable serde fields, but some of the consensus types require optional serialization for RPC compatibility. Read more: <https://github.com/bincode-org/bincode/issues/326>
60//!
61//! As a workaround this crate introduces the
62//! [`SerdeBincodeCompat`](serde_bincode_compat::SerdeBincodeCompat) trait used to a bincode
63//! compatible serde representation.
64
65#![doc(
66 html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
67 html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
68 issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
69)]
70#![cfg_attr(not(test), warn(unused_crate_dependencies))]
71#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
72#![cfg_attr(not(feature = "std"), no_std)]
73
74#[macro_use]
75extern crate alloc;
76
77/// Common constants.
78pub mod constants;
79pub use constants::gas_units::{format_gas, format_gas_throughput};
80
81/// Minimal account
82pub mod account;
83pub use account::{Account, Bytecode};
84
85pub mod receipt;
86pub use receipt::{FullReceipt, Receipt};
87
88pub mod transaction;
89pub use alloy_consensus::{
90 transaction::{Recovered, TransactionMeta},
91 ReceiptWithBloom,
92};
93pub use transaction::{
94 execute::FillTxEnv,
95 signed::{FullSignedTx, SignedTransaction},
96 FullTransaction, Transaction,
97};
98
99pub mod block;
100pub use block::{
101 body::{BlockBody, FullBlockBody},
102 header::{AlloyBlockHeader, BlockHeader, FullBlockHeader},
103 Block, FullBlock, RecoveredBlock, SealedBlock,
104};
105
106mod withdrawal;
107pub use alloy_eips::eip2718::WithEncoded;
108
109pub mod crypto;
110
111mod error;
112pub use error::{GotExpected, GotExpectedBoxed};
113
114mod log;
115pub use alloy_primitives::{logs_bloom, Log, LogData};
116
117pub mod proofs;
118
119mod storage;
120pub use storage::StorageEntry;
121
122pub mod sync;
123
124/// Common header types
125pub mod header;
126pub use header::{Header, HeaderError, SealedHeader, SealedHeaderFor};
127
128/// Bincode-compatible serde implementations for common abstracted types in Reth.
129///
130/// `bincode` crate doesn't work with optionally serializable serde fields, but some of the
131/// Reth types require optional serialization for RPC compatibility. This module makes so that
132/// all fields are serialized.
133///
134/// Read more: <https://github.com/bincode-org/bincode/issues/326>
135#[cfg(feature = "serde-bincode-compat")]
136pub mod serde_bincode_compat;
137
138/// Heuristic size trait
139pub mod size;
140pub use size::InMemorySize;
141
142/// Node traits
143pub mod node;
144pub use node::{BlockTy, BodyTy, FullNodePrimitives, HeaderTy, NodePrimitives, ReceiptTy, TxTy};
145
146/// Helper trait that requires de-/serialize implementation since `serde` feature is enabled.
147#[cfg(feature = "serde")]
148pub trait MaybeSerde: serde::Serialize + for<'de> serde::Deserialize<'de> {}
149/// Noop. Helper trait that would require de-/serialize implementation if `serde` feature were
150/// enabled.
151#[cfg(not(feature = "serde"))]
152pub trait MaybeSerde {}
153
154#[cfg(feature = "serde")]
155impl<T> MaybeSerde for T where T: serde::Serialize + for<'de> serde::Deserialize<'de> {}
156#[cfg(not(feature = "serde"))]
157impl<T> MaybeSerde for T {}
158
159/// Helper trait that requires database encoding implementation since `reth-codec` feature is
160/// enabled.
161#[cfg(feature = "reth-codec")]
162pub trait MaybeCompact: reth_codecs::Compact {}
163/// Noop. Helper trait that would require database encoding implementation if `reth-codec` feature
164/// were enabled.
165#[cfg(not(feature = "reth-codec"))]
166pub trait MaybeCompact {}
167
168#[cfg(feature = "reth-codec")]
169impl<T> MaybeCompact for T where T: reth_codecs::Compact {}
170#[cfg(not(feature = "reth-codec"))]
171impl<T> MaybeCompact for T {}
172
173/// Helper trait that requires serde bincode compatibility implementation.
174#[cfg(feature = "serde-bincode-compat")]
175pub trait MaybeSerdeBincodeCompat: crate::serde_bincode_compat::SerdeBincodeCompat {}
176/// Noop. Helper trait that would require serde bincode compatibility implementation if
177/// `serde-bincode-compat` feature were enabled.
178#[cfg(not(feature = "serde-bincode-compat"))]
179pub trait MaybeSerdeBincodeCompat {}
180
181#[cfg(feature = "serde-bincode-compat")]
182impl<T> MaybeSerdeBincodeCompat for T where T: crate::serde_bincode_compat::SerdeBincodeCompat {}
183#[cfg(not(feature = "serde-bincode-compat"))]
184impl<T> MaybeSerdeBincodeCompat for T {}
185
186/// Utilities for testing.
187#[cfg(any(test, feature = "arbitrary", feature = "test-utils"))]
188pub mod test_utils {
189 pub use crate::header::test_utils::{generate_valid_header, valid_header_strategy};
190 #[cfg(any(test, feature = "test-utils"))]
191 pub use crate::{block::TestBlock, header::test_utils::TestHeader};
192}