reth_engine_tree/
lib.rs

1//! This crate includes the core components for advancing a reth chain.
2//!
3//! ## Functionality
4//!
5//! The components in this crate are involved in:
6//! * Handling and reacting to incoming consensus events ([`EngineHandler`](engine::EngineHandler))
7//! * Advancing the chain ([`ChainOrchestrator`](chain::ChainOrchestrator))
8//! * Keeping track of the chain structure in-memory ([`TreeState`](tree::TreeState))
9//! * Performing backfill sync and handling its progress ([`BackfillSync`](backfill::BackfillSync))
10//! * Downloading blocks ([`BlockDownloader`](download::BlockDownloader)), and
11//! * Persisting blocks and performing pruning
12//!   ([`PersistenceService`](persistence::PersistenceService))
13//!
14//! ## Design and motivation
15//!
16//! The node must keep up with the state of the chain and validate new updates to the chain state.
17//!
18//! In order to respond to consensus messages and advance the chain quickly, validation code must
19//! avoid database write operations and perform as much work as possible in-memory. This requirement
20//! is what informs the architecture of the components this crate.
21//!
22//! ## Chain synchronization
23//!
24//! When the node receives a block with an unknown parent, or cannot find a certain block hash, it
25//! needs to download and validate the part of the chain that is missing.
26//!
27//! This can happen during a live sync when the node receives a forkchoice update from the consensus
28//! layer which causes the node to have to walk back from the received head, downloading the block's
29//! parents until it reaches a known block.
30//!
31//! This can also technically happen when a finalized block is fetched, before checking distance,
32//! but this is a very unlikely case.
33//!
34//! There are two mutually-exclusive ways the node can bring itself in sync with the chain:
35//! * Backfill sync: downloading and validating large ranges of blocks in a structured manner,
36//!   performing different parts of the validation process in sequence.
37//! * Live sync: By responding to new blocks from a connected consensus layer and downloading any
38//!   missing blocks on the fly.
39//!
40//! To determine which sync type to use, the node checks how many blocks it needs to execute to
41//! catch up to the tip of the chain. If this range is large, backfill sync will be used. Otherwise,
42//! live sync will be used.
43//!
44//! The backfill sync is driven by components which implement the
45//! [`BackfillSync`](backfill::BackfillSync) trait, like [`PipelineSync`](backfill::PipelineSync).
46//!
47//! ## Handling consensus messages
48//!
49//! Consensus message handling is performed by three main components:
50//! 1. The [`EngineHandler`](engine::EngineHandler), which takes incoming consensus messages and
51//!    manages any requested backfill or download work.
52//! 2. The [`EngineApiRequestHandler`](engine::EngineApiRequestHandler), which processes messages
53//!    from the [`EngineHandler`](engine::EngineHandler) and delegates them to the
54//!    [`EngineApiTreeHandler`](tree::EngineApiTreeHandler).
55//! 3. The [`EngineApiTreeHandler`](tree::EngineApiTreeHandler), which processes incoming tree
56//!    events, such as new payload events, sending back requests for any needed backfill or download
57//!    work.
58//!
59//! ## Chain representation
60//!
61//! The chain is represented by the [`TreeState`](tree::TreeState) data structure, which keeps
62//! tracks of blocks by hash and number, as well as keeping track of parent-child relationships
63//! between blocks. The hash and number of the current head of the canonical chain is also tracked
64//! in the [`TreeState`](tree::TreeState).
65//!
66//! ## Persistence model
67//!
68//! Because the node minimizes database writes in the critical path for handling consensus messages,
69//! it must perform database writes in the background.
70//!
71//! Performing writes in the background has two advantages:
72//! 1. As mentioned, writes are not in the critical path of request processing.
73//! 2. Writes can be larger and done at a lower frequency, allowing for more efficient writes.
74//!
75//! This is achieved by spawning a separate thread which is sent different commands corresponding to
76//! different types of writes, for example a command to write a list of transactions, or remove a
77//! specific range of blocks.
78//!
79//! The persistence service must also respond to these commands, to ensure that any in-memory state
80//! that is on-disk can be cleaned up, conserving memory and allowing us to add new blocks
81//! indefinitely.
82//!
83//! ## Feature Flags
84//!
85//! - `test-utils`: Export utilities for testing
86
87#![doc(
88    html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
89    html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
90    issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
91)]
92#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
93#![cfg_attr(not(test), warn(unused_crate_dependencies))]
94
95/// Support for backfill sync mode.
96pub mod backfill;
97/// The type that drives the chain forward.
98pub mod chain;
99/// Support for downloading blocks on demand for live sync.
100pub mod download;
101/// Engine Api chain handler support.
102pub mod engine;
103/// Metrics support.
104pub mod metrics;
105/// The background writer service, coordinating write operations on static files and the database.
106pub mod persistence;
107/// Support for interacting with the blockchain tree.
108pub mod tree;
109
110/// Test utilities.
111#[cfg(any(test, feature = "test-utils"))]
112pub mod test_utils;