Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add trie prefetch when executing blocks #56

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ members = [
"crates/transaction-pool/",
"crates/trie/common",
"crates/trie/parallel/",
"crates/trie/prefetch/",
"crates/trie/trie",
"crates/bsc/node/",
"crates/bsc/engine/",
Expand Down Expand Up @@ -384,6 +385,7 @@ reth-trie = { path = "crates/trie/trie" }
reth-trie-common = { path = "crates/trie/common" }
reth-trie-db = { path = "crates/trie/db" }
reth-trie-parallel = { path = "crates/trie/parallel" }
reth-trie-prefetch = { path = "crates/trie/prefetch" }

# revm
revm = { version = "12.1.0", features = [
Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ install: ## Build and install the reth binary under `~/.cargo/bin`.
.PHONY: install-op
install-op: ## Build and install the op-reth binary under `~/.cargo/bin`.
cargo install --path bin/reth --bin op-reth --force --locked \
--features "optimism opbnb $(FEATURES)" \
--features "optimism,opbnb,$(FEATURES)" \
--profile "$(PROFILE)" \
$(CARGO_INSTALL_EXTRA_FLAGS)

.PHONY: install-bsc
install-bsc: ## Build and install the bsc-reth binary under `~/.cargo/bin`.
cargo install --path bin/reth --bin bsc-reth --force --locked \
--features "bsc $(FEATURES)" \
--features "bsc,prefetch,$(FEATURES)" \
--profile "$(PROFILE)" \
$(CARGO_INSTALL_EXTRA_FLAGS)

Expand All @@ -70,11 +70,11 @@ build: ## Build the reth binary into `target` directory.

.PHONY: build-op
build-op: ## Build the op-reth binary into `target` directory.
cargo build --bin op-reth --features "optimism opbnb $(FEATURES)" --profile "$(PROFILE)"
cargo build --bin op-reth --features "optimism,opbnb,$(FEATURES)" --profile "$(PROFILE)"
yutianwu marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: build-bsc
build-bsc: ## Build the bsc-reth binary into `target` directory.
cargo build --bin bsc-reth --features "bsc $(FEATURES)" --profile "$(PROFILE)"
cargo build --bin bsc-reth --features "bsc,prefetch,$(FEATURES)" --profile "$(PROFILE)"

# Builds the reth binary natively.
build-native-%:
Expand All @@ -84,7 +84,7 @@ op-build-native-%:
cargo build --bin op-reth --target $* --features "optimism,opbnb,$(FEATURES)" --profile "$(PROFILE)"

bsc-build-native-%:
cargo build --bin bsc-reth --target $* --features "bsc,$(FEATURES)" --profile "$(PROFILE)"
cargo build --bin bsc-reth --target $* --features "bsc,prefetch,$(FEATURES)" --profile "$(PROFILE)"

# The following commands use `cross` to build a cross-compile.
#
Expand Down Expand Up @@ -127,7 +127,7 @@ op-build-%:

bsc-build-%:
RUSTFLAGS="-C link-arg=-lgcc -Clink-arg=-static-libgcc" \
cross build --bin bsc-reth --target $* --features "bsc,$(FEATURES)" --profile "$(PROFILE)"
cross build --bin bsc-reth --target $* --features "bsc,prefetch,$(FEATURES)" --profile "$(PROFILE)"

# Unfortunately we can't easily use cross to build for Darwin because of licensing issues.
# If we wanted to, we would need to build a custom Docker image with the SDK available.
Expand Down
2 changes: 2 additions & 0 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ bsc = [
"reth-beacon-consensus/bsc",
]

prefetch = ["reth-blockchain-tree/prefetch"]

# no-op feature flag for switching between the `optimism` and default functionality in CI matrices
ethereum = []

Expand Down
4 changes: 2 additions & 2 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ impl Command {
#[cfg(feature = "bsc")]
let executor =
block_executor!(provider_factory.chain_spec(), provider_factory.clone())
.executor(db);
.executor(db, None);
#[cfg(not(feature = "bsc"))]
let executor = block_executor!(provider_factory.chain_spec()).executor(db);
let executor = block_executor!(provider_factory.chain_spec()).executor(db, None);

let BlockExecutionOutput { state, receipts, requests, .. } = executor
.execute((&block_with_senders.clone().unseal(), U256::MAX, None).into())?;
Expand Down
6 changes: 3 additions & 3 deletions bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ impl Command {
));

#[cfg(feature = "bsc")]
let executor =
block_executor!(provider_factory.chain_spec(), provider_factory.clone()).executor(db);
let executor = block_executor!(provider_factory.chain_spec(), provider_factory.clone())
.executor(db, None);
#[cfg(not(feature = "bsc"))]
let executor = block_executor!(provider_factory.chain_spec()).executor(db);
let executor = block_executor!(provider_factory.chain_spec()).executor(db, None);

let merkle_block_td =
provider.header_td_by_number(merkle_block_number)?.unwrap_or_default();
Expand Down
2 changes: 2 additions & 0 deletions crates/blockchain-tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ reth-trie = { workspace = true, features = ["metrics"] }
reth-trie-parallel = { workspace = true, features = ["parallel"] }
reth-network.workspace = true
reth-consensus.workspace = true
reth-trie-prefetch = { workspace = true, optional = true }

# common
parking_lot.workspace = true
Expand Down Expand Up @@ -59,3 +60,4 @@ alloy-genesis.workspace = true
[features]
test-utils = []
optimism = ["reth-primitives/optimism", "reth-provider/optimism"]
prefetch = ["dep:reth-trie-prefetch"]
2 changes: 1 addition & 1 deletion crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<DB, E> BlockchainTree<DB, E> {

impl<DB, E> BlockchainTree<DB, E>
where
DB: Database + Clone,
DB: Database + Clone + 'static,
E: BlockExecutorProvider,
{
/// Builds the blockchain tree for the node.
Expand Down
42 changes: 37 additions & 5 deletions crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ use reth_provider::{
use reth_revm::database::StateProviderDatabase;
use reth_trie::updates::TrieUpdates;
use reth_trie_parallel::parallel_root::ParallelStateRoot;
#[cfg(feature = "prefetch")]
use reth_trie_prefetch::TriePrefetch;
#[cfg(feature = "prefetch")]
use std::sync::Arc;
use std::{
collections::{BTreeMap, HashMap},
ops::{Deref, DerefMut},
Expand Down Expand Up @@ -77,7 +81,7 @@ impl AppendableChain {
block_validation_kind: BlockValidationKind,
) -> Result<Self, InsertBlockErrorKind>
where
DB: Database + Clone,
DB: Database + Clone + 'static,
E: BlockExecutorProvider,
{
let execution_outcome = ExecutionOutcome::default();
Expand Down Expand Up @@ -116,7 +120,7 @@ impl AppendableChain {
block_validation_kind: BlockValidationKind,
) -> Result<Self, InsertBlockErrorKind>
where
DB: Database + Clone,
DB: Database + Clone + 'static,
E: BlockExecutorProvider,
{
let parent_number =
Expand Down Expand Up @@ -181,7 +185,7 @@ impl AppendableChain {
) -> Result<(ExecutionOutcome, Option<TrieUpdates>), BlockExecutionError>
where
EDP: FullExecutionDataProvider,
DB: Database + Clone,
DB: Database + Clone + 'static,
E: BlockExecutorProvider,
{
// some checks are done before blocks comes here.
Expand All @@ -208,11 +212,35 @@ impl AppendableChain {

let provider = BundleStateProvider::new(state_provider, bundle_state_data_provider);

#[cfg(feature = "prefetch")]
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
let (prefetch_tx, prefetch_rx) = tokio::sync::mpsc::unbounded_channel();

let db = StateProviderDatabase::new(&provider);
let executor = externals.executor_factory.executor(db);
#[cfg(feature = "prefetch")]
let executor = externals.executor_factory.executor(db, Some(prefetch_tx));
#[cfg(not(feature = "prefetch"))]
let executor = externals.executor_factory.executor(db, None);

let block_hash = block.hash();
let block = block.unseal();

#[cfg(feature = "prefetch")]
let (interrupt_tx, interrupt_rx) = tokio::sync::oneshot::channel();

#[cfg(feature = "prefetch")]
{
let mut trie_prefetch = TriePrefetch::new();
let consistent_view = Arc::new(ConsistentDbView::new_with_latest_tip(
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
externals.provider_factory.clone(),
)?);

tokio::spawn({
async move {
trie_prefetch.run::<DB>(consistent_view, prefetch_rx, interrupt_rx).await;
}
});
}

let state = executor.execute((&block, U256::MAX, ancestor_blocks).into())?;
let BlockExecutionOutput { state, receipts, requests, .. } = state;
externals
Expand All @@ -222,6 +250,10 @@ impl AppendableChain {
let initial_execution_outcome =
ExecutionOutcome::new(state, receipts.into(), block.number, vec![requests.into()]);

// stop the prefetch task.
#[cfg(feature = "prefetch")]
let _ = interrupt_tx.send(());

// check state root if the block extends the canonical chain __and__ if state root
// validation was requested.
if block_validation_kind.is_exhaustive() {
Expand Down Expand Up @@ -284,7 +316,7 @@ impl AppendableChain {
block_validation_kind: BlockValidationKind,
) -> Result<(), InsertBlockErrorKind>
where
DB: Database + Clone,
DB: Database + Clone + 'static,
E: BlockExecutorProvider,
{
let parent_block = self.chain.tip();
Expand Down
6 changes: 3 additions & 3 deletions crates/blockchain-tree/src/shareable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<DB, E> ShareableBlockchainTree<DB, E> {

impl<DB, E> BlockchainTreeEngine for ShareableBlockchainTree<DB, E>
where
DB: Database + Clone,
DB: Database + Clone + 'static,
E: BlockExecutorProvider,
{
fn buffer_block(&self, block: SealedBlockWithSenders) -> Result<(), InsertBlockError> {
Expand Down Expand Up @@ -108,7 +108,7 @@ where

impl<DB, E> BlockchainTreeViewer for ShareableBlockchainTree<DB, E>
where
DB: Database + Clone,
DB: Database + Clone + 'static,
E: BlockExecutorProvider,
{
fn header_by_hash(&self, hash: BlockHash) -> Option<SealedHeader> {
Expand Down Expand Up @@ -171,7 +171,7 @@ where

impl<DB, E> BlockchainTreePendingStateProvider for ShareableBlockchainTree<DB, E>
where
DB: Database + Clone,
DB: Database + Clone + 'static,
E: BlockExecutorProvider,
{
fn find_pending_state_provider(
Expand Down
3 changes: 3 additions & 0 deletions crates/bsc/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ bitset = "0.1.2"
lru = "0.12.3"
blst = "0.3.12"

# async
tokio = { workspace = true, features = ["sync", "time"] }

[dev-dependencies]
reth-revm = { workspace = true, features = ["test-utils"] }
reth-provider = { workspace = true, features = ["test-utils"] }
Expand Down
Loading
Loading