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/SeismicSystems/seismic-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};
93
94pub use transaction::{
95    execute::FillTxEnv,
96    signed::{FullSignedTx, SignedTransaction},
97    FullTransaction, SignerRecoverable, Transaction,
98};
99
100pub mod block;
101pub use block::{
102    body::{BlockBody, FullBlockBody},
103    header::{AlloyBlockHeader, BlockHeader, FullBlockHeader},
104    Block, FullBlock, RecoveredBlock, SealedBlock,
105};
106
107mod withdrawal;
108pub use alloy_eips::eip2718::WithEncoded;
109
110pub mod crypto;
111
112mod error;
113pub use error::{GotExpected, GotExpectedBoxed};
114
115mod log;
116pub use alloy_primitives::{logs_bloom, Log, LogData};
117
118pub mod proofs;
119
120mod storage;
121pub use storage::StorageEntry;
122
123pub mod sync;
124
125mod extended;
126pub use extended::Extended;
127/// Common header types
128pub mod header;
129pub use header::{Header, HeaderError, SealedHeader, SealedHeaderFor};
130
131/// Bincode-compatible serde implementations for common abstracted types in Reth.
132///
133/// `bincode` crate doesn't work with optionally serializable serde fields, but some of the
134/// Reth types require optional serialization for RPC compatibility. This module makes so that
135/// all fields are serialized.
136///
137/// Read more: <https://github.com/bincode-org/bincode/issues/326>
138#[cfg(feature = "serde-bincode-compat")]
139pub mod serde_bincode_compat;
140
141/// Heuristic size trait
142pub mod size;
143pub use size::InMemorySize;
144
145/// Node traits
146pub mod node;
147pub use node::{BlockTy, BodyTy, FullNodePrimitives, HeaderTy, NodePrimitives, ReceiptTy, TxTy};
148
149/// Helper trait that requires de-/serialize implementation since `serde` feature is enabled.
150#[cfg(feature = "serde")]
151pub trait MaybeSerde: serde::Serialize + for<'de> serde::Deserialize<'de> {}
152/// Noop. Helper trait that would require de-/serialize implementation if `serde` feature were
153/// enabled.
154#[cfg(not(feature = "serde"))]
155pub trait MaybeSerde {}
156
157#[cfg(feature = "serde")]
158impl<T> MaybeSerde for T where T: serde::Serialize + for<'de> serde::Deserialize<'de> {}
159#[cfg(not(feature = "serde"))]
160impl<T> MaybeSerde for T {}
161
162/// Helper trait that requires database encoding implementation since `reth-codec` feature is
163/// enabled.
164#[cfg(feature = "reth-codec")]
165pub trait MaybeCompact: reth_codecs::Compact {}
166/// Noop. Helper trait that would require database encoding implementation if `reth-codec` feature
167/// were enabled.
168#[cfg(not(feature = "reth-codec"))]
169pub trait MaybeCompact {}
170
171#[cfg(feature = "reth-codec")]
172impl<T> MaybeCompact for T where T: reth_codecs::Compact {}
173#[cfg(not(feature = "reth-codec"))]
174impl<T> MaybeCompact for T {}
175
176/// Helper trait that requires serde bincode compatibility implementation.
177#[cfg(feature = "serde-bincode-compat")]
178pub trait MaybeSerdeBincodeCompat: crate::serde_bincode_compat::SerdeBincodeCompat {}
179/// Noop. Helper trait that would require serde bincode compatibility implementation if
180/// `serde-bincode-compat` feature were enabled.
181#[cfg(not(feature = "serde-bincode-compat"))]
182pub trait MaybeSerdeBincodeCompat {}
183
184#[cfg(feature = "serde-bincode-compat")]
185impl<T> MaybeSerdeBincodeCompat for T where T: crate::serde_bincode_compat::SerdeBincodeCompat {}
186#[cfg(not(feature = "serde-bincode-compat"))]
187impl<T> MaybeSerdeBincodeCompat for T {}
188
189/// Utilities for testing.
190#[cfg(any(test, feature = "arbitrary", feature = "test-utils"))]
191pub mod test_utils {
192    pub use crate::header::test_utils::{generate_valid_header, valid_header_strategy};
193    #[cfg(any(test, feature = "test-utils"))]
194    pub use crate::{block::TestBlock, header::test_utils::TestHeader};
195}