Skip to content

Commit

Permalink
feat: add header AT to provider (paradigmxyz#13030)
Browse files Browse the repository at this point in the history
Co-authored-by: Arsenii Kulikov <[email protected]>
  • Loading branch information
mattsse and klkvr authored Dec 2, 2024
1 parent 519a10a commit 332cce1
Show file tree
Hide file tree
Showing 71 changed files with 667 additions and 432 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
Primitives: NodePrimitives<
Block = reth_primitives::Block,
Receipt = reth_primitives::Receipt,
BlockHeader = reth_primitives::Header,
>,
>,
>(
Expand Down
1 change: 1 addition & 0 deletions bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
Primitives: NodePrimitives<
Block = reth_primitives::Block,
Receipt = reth_primitives::Receipt,
BlockHeader = reth_primitives::Header,
>,
>,
>(
Expand Down
1 change: 1 addition & 0 deletions bin/reth/src/commands/debug_cmd/replay_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
Primitives: NodePrimitives<
Block = reth_primitives::Block,
Receipt = reth_primitives::Receipt,
BlockHeader = reth_primitives::Header,
>,
>,
>(
Expand Down
7 changes: 6 additions & 1 deletion crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,12 @@ mod tests {
}

fn setup_genesis<
N: ProviderNodeTypes<Primitives: FullNodePrimitives<BlockBody = reth_primitives::BlockBody>>,
N: ProviderNodeTypes<
Primitives: FullNodePrimitives<
BlockBody = reth_primitives::BlockBody,
BlockHeader = reth_primitives::Header,
>,
>,
>(
factory: &ProviderFactory<N>,
mut genesis: SealedBlock,
Expand Down
11 changes: 7 additions & 4 deletions crates/chain-state/src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,22 @@ pub struct ForkChoiceNotifications<T = alloy_consensus::Header>(
/// A trait that allows to register to fork choice related events
/// and get notified when a new fork choice is available.
pub trait ForkChoiceSubscriptions: Send + Sync {
/// Block Header type.
type Header: Clone + Send + Sync + 'static;

/// Get notified when a new safe block of the chain is selected.
fn subscribe_safe_block(&self) -> ForkChoiceNotifications;
fn subscribe_safe_block(&self) -> ForkChoiceNotifications<Self::Header>;

/// Get notified when a new finalized block of the chain is selected.
fn subscribe_finalized_block(&self) -> ForkChoiceNotifications;
fn subscribe_finalized_block(&self) -> ForkChoiceNotifications<Self::Header>;

/// Convenience method to get a stream of the new safe blocks of the chain.
fn safe_block_stream(&self) -> ForkChoiceStream<SealedHeader> {
fn safe_block_stream(&self) -> ForkChoiceStream<SealedHeader<Self::Header>> {
ForkChoiceStream::new(self.subscribe_safe_block().0)
}

/// Convenience method to get a stream of the new finalized blocks of the chain.
fn finalized_block_stream(&self) -> ForkChoiceStream<SealedHeader> {
fn finalized_block_stream(&self) -> ForkChoiceStream<SealedHeader<Self::Header>> {
ForkChoiceStream::new(self.subscribe_finalized_block().0)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ reth-cli.workspace = true
reth-ethereum-cli.workspace = true
reth-cli-runner.workspace = true
reth-cli-util.workspace = true
reth-codecs = { workspace = true, optional = true }
reth-codecs.workspace = true
reth-config.workspace = true
reth-consensus.workspace = true
reth-db = { workspace = true, features = ["mdbx"] }
Expand Down Expand Up @@ -110,7 +110,7 @@ arbitrary = [
"reth-prune-types/test-utils",
"reth-stages-types/test-utils",
"reth-trie-common/test-utils",
"reth-codecs?/arbitrary",
"reth-codecs/arbitrary",
"reth-prune-types?/arbitrary",
"reth-stages-types?/arbitrary",
"reth-trie-common?/arbitrary",
Expand Down
25 changes: 13 additions & 12 deletions crates/cli/commands/src/init_state/without_evm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use alloy_primitives::{BlockNumber, B256, U256};
use alloy_rlp::Decodable;

use alloy_consensus::Header;
use alloy_consensus::{BlockHeader, Header};
use reth_codecs::Compact;
use reth_node_builder::NodePrimitives;
use reth_primitives::{SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment};
use reth_provider::{
Expand All @@ -27,26 +28,26 @@ pub(crate) fn read_header_from_file(path: PathBuf) -> Result<Header, eyre::Error
/// first valid block.
pub fn setup_without_evm<Provider>(
provider_rw: &Provider,
header: SealedHeader,
header: SealedHeader<<Provider::Primitives as NodePrimitives>::BlockHeader>,
total_difficulty: U256,
) -> Result<(), eyre::Error>
where
Provider: StaticFileProviderFactory
Provider: StaticFileProviderFactory<Primitives: NodePrimitives<BlockHeader = Header>>
+ StageCheckpointWriter
+ BlockWriter<Block: reth_node_api::Block<Header = reth_primitives::Header>>,
+ BlockWriter<Block = <Provider::Primitives as NodePrimitives>::Block>,
{
info!(target: "reth::cli", "Setting up dummy EVM chain before importing state.");

let static_file_provider = provider_rw.static_file_provider();
// Write EVM dummy data up to `header - 1` block
append_dummy_chain(&static_file_provider, header.number - 1)?;
append_dummy_chain(&static_file_provider, header.number() - 1)?;

info!(target: "reth::cli", "Appending first valid block.");

append_first_block(provider_rw, &header, total_difficulty)?;

for stage in StageId::ALL {
provider_rw.save_stage_checkpoint(stage, StageCheckpoint::new(header.number))?;
provider_rw.save_stage_checkpoint(stage, StageCheckpoint::new(header.number()))?;
}

info!(target: "reth::cli", "Set up finished.");
Expand All @@ -60,12 +61,12 @@ where
/// height.
fn append_first_block<Provider>(
provider_rw: &Provider,
header: &SealedHeader,
header: &SealedHeader<<Provider::Primitives as NodePrimitives>::BlockHeader>,
total_difficulty: U256,
) -> Result<(), eyre::Error>
where
Provider: BlockWriter<Block: reth_node_api::Block<Header = reth_primitives::Header>>
+ StaticFileProviderFactory,
Provider: BlockWriter<Block = <Provider::Primitives as NodePrimitives>::Block>
+ StaticFileProviderFactory<Primitives: NodePrimitives<BlockHeader: Compact>>,
{
provider_rw.insert_block(
SealedBlockWithSenders::new(SealedBlock::new(header.clone(), Default::default()), vec![])
Expand All @@ -81,9 +82,9 @@ where
&header.hash(),
)?;

sf_provider.latest_writer(StaticFileSegment::Receipts)?.increment_block(header.number)?;
sf_provider.latest_writer(StaticFileSegment::Receipts)?.increment_block(header.number())?;

sf_provider.latest_writer(StaticFileSegment::Transactions)?.increment_block(header.number)?;
sf_provider.latest_writer(StaticFileSegment::Transactions)?.increment_block(header.number())?;

Ok(())
}
Expand All @@ -93,7 +94,7 @@ where
/// * Headers: It will push an empty block.
/// * Transactions: It will not push any tx, only increments the end block range.
/// * Receipts: It will not push any receipt, only increments the end block range.
fn append_dummy_chain<N: NodePrimitives>(
fn append_dummy_chain<N: NodePrimitives<BlockHeader = Header>>(
sf_provider: &StaticFileProvider<N>,
target_height: BlockNumber,
) -> Result<(), eyre::Error> {
Expand Down
3 changes: 3 additions & 0 deletions crates/cli/commands/src/stage/dump/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ where
Primitives: NodePrimitives<
Block = reth_primitives::Block,
Receipt = reth_primitives::Receipt,
BlockHeader = reth_primitives::Header,
>,
>,
E: BlockExecutorProvider<Primitives = N::Primitives>,
Expand Down Expand Up @@ -143,6 +144,7 @@ fn unwind_and_copy<
Primitives: NodePrimitives<
Block = reth_primitives::Block,
Receipt = reth_primitives::Receipt,
BlockHeader = reth_primitives::Header,
>,
>,
>(
Expand Down Expand Up @@ -186,6 +188,7 @@ where
Primitives: NodePrimitives<
Block = reth_primitives::Block,
Receipt = reth_primitives::Receipt,
BlockHeader = reth_primitives::Header,
>,
>,
E: BlockExecutorProvider<Primitives = N::Primitives>,
Expand Down
28 changes: 15 additions & 13 deletions crates/cli/commands/src/stage/dump/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ use reth_stages::{
};
use tracing::info;

pub(crate) async fn dump_merkle_stage<
pub(crate) async fn dump_merkle_stage<N>(
db_tool: &DbTool<N>,
from: BlockNumber,
to: BlockNumber,
output_datadir: ChainPath<DataDirPath>,
should_run: bool,
) -> Result<()>
where
N: ProviderNodeTypes<
DB = Arc<DatabaseEnv>,
Primitives: NodePrimitives<
Block = reth_primitives::Block,
Receipt = reth_primitives::Receipt,
BlockHeader = reth_primitives::Header,
>,
>,
>(
db_tool: &DbTool<N>,
from: BlockNumber,
to: BlockNumber,
output_datadir: ChainPath<DataDirPath>,
should_run: bool,
) -> Result<()> {
{
let (output_db, tip_block_number) = setup(from, to, &output_datadir.db(), db_tool)?;

output_db.update(|tx| {
Expand Down Expand Up @@ -81,6 +83,7 @@ fn unwind_and_copy<
Primitives: NodePrimitives<
Block = reth_primitives::Block,
Receipt = reth_primitives::Receipt,
BlockHeader = reth_primitives::Header,
>,
>,
>(
Expand Down Expand Up @@ -161,11 +164,10 @@ fn unwind_and_copy<
}

/// Try to re-execute the stage straight away
fn dry_run<N: ProviderNodeTypes>(
output_provider_factory: ProviderFactory<N>,
to: u64,
from: u64,
) -> eyre::Result<()> {
fn dry_run<N>(output_provider_factory: ProviderFactory<N>, to: u64, from: u64) -> eyre::Result<()>
where
N: ProviderNodeTypes<Primitives: NodePrimitives<BlockHeader = reth_primitives::Header>>,
{
info!(target: "reth::cli", "Executing stage.");
let provider = output_provider_factory.database_provider_rw()?;

Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/beacon/src/engine/hooks/static_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ where
Provider: StaticFileProviderFactory
+ DatabaseProviderFactory<
Provider: StaticFileProviderFactory<
Primitives: NodePrimitives<SignedTx: Value + Compact>,
Primitives: NodePrimitives<SignedTx: Value + Compact, BlockHeader: Value + Compact>,
> + StageCheckpointReader
+ BlockReader
+ ChainStateBlockReader,
Expand Down Expand Up @@ -152,7 +152,7 @@ where
Provider: StaticFileProviderFactory
+ DatabaseProviderFactory<
Provider: StaticFileProviderFactory<
Primitives: NodePrimitives<SignedTx: Value + Compact>,
Primitives: NodePrimitives<SignedTx: Value + Compact, BlockHeader: Value + Compact>,
> + StageCheckpointReader
+ BlockReader
+ ChainStateBlockReader,
Expand Down
17 changes: 11 additions & 6 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use reth_network_p2p::{
sync::{NetworkSyncUpdater, SyncState},
EthBlockClient,
};
use reth_node_types::{Block, BlockTy, NodeTypesWithEngine};
use reth_node_types::{Block, BlockTy, HeaderTy, NodeTypesWithEngine};
use reth_payload_builder::PayloadBuilderHandle;
use reth_payload_builder_primitives::PayloadBuilder;
use reth_payload_primitives::{PayloadAttributes, PayloadBuilderAttributes};
Expand Down Expand Up @@ -234,9 +234,9 @@ impl<N, BT, Client> BeaconConsensusEngine<N, BT, Client>
where
N: EngineNodeTypes,
BT: BlockchainTreeEngine
+ BlockReader<Block = BlockTy<N>>
+ BlockReader<Block = BlockTy<N>, Header = HeaderTy<N>>
+ BlockIdReader
+ CanonChainTracker
+ CanonChainTracker<Header = HeaderTy<N>>
+ StageCheckpointReader
+ ChainSpecProvider<ChainSpec = N::ChainSpec>
+ 'static,
Expand Down Expand Up @@ -1804,9 +1804,9 @@ where
N: EngineNodeTypes,
Client: EthBlockClient + 'static,
BT: BlockchainTreeEngine
+ BlockReader<Block = BlockTy<N>>
+ BlockReader<Block = BlockTy<N>, Header = HeaderTy<N>>
+ BlockIdReader
+ CanonChainTracker
+ CanonChainTracker<Header = HeaderTy<N>>
+ StageCheckpointReader
+ ChainSpecProvider<ChainSpec = N::ChainSpec>
+ Unpin
Expand Down Expand Up @@ -2179,7 +2179,12 @@ mod tests {

fn insert_blocks<
'a,
N: ProviderNodeTypes<Primitives: FullNodePrimitives<BlockBody = reth_primitives::BlockBody>>,
N: ProviderNodeTypes<
Primitives: FullNodePrimitives<
BlockBody = reth_primitives::BlockBody,
BlockHeader = reth_primitives::Header,
>,
>,
>(
provider_factory: ProviderFactory<N>,
mut blocks: impl Iterator<Item = &'a SealedBlock>,
Expand Down
2 changes: 2 additions & 0 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ mod tests {
}

impl HeaderProvider for Provider {
type Header = Header;

fn is_known(&self, _block_hash: &BlockHash) -> ProviderResult<bool> {
Ok(self.is_known)
}
Expand Down
1 change: 1 addition & 0 deletions crates/engine/local/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ reth-transaction-pool.workspace = true
reth-stages-api.workspace = true

# alloy
alloy-consensus.workspace = true
alloy-primitives.workspace = true
alloy-rpc-types-engine.workspace = true

Expand Down
3 changes: 2 additions & 1 deletion crates/engine/local/src/miner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Contains the implementation of the mining mode for the local engine.
use alloy_consensus::BlockHeader;
use alloy_primitives::{TxHash, B256};
use alloy_rpc_types_engine::{CancunPayloadFields, ExecutionPayloadSidecar, ForkchoiceState};
use eyre::OptionExt;
Expand Down Expand Up @@ -114,7 +115,7 @@ where
to_engine,
mode,
payload_builder,
last_timestamp: latest_header.timestamp,
last_timestamp: latest_header.timestamp(),
last_block_hashes: vec![latest_header.hash()],
};

Expand Down
12 changes: 8 additions & 4 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,13 @@ impl<N, P: Debug, E: Debug, T: EngineTypes + Debug, V: Debug> std::fmt::Debug

impl<N, P, E, T, V> EngineApiTreeHandler<N, P, E, T, V>
where
N: NodePrimitives<Block = reth_primitives::Block, Receipt = reth_primitives::Receipt>,
N: NodePrimitives<
Block = reth_primitives::Block,
BlockHeader = reth_primitives::Header,
Receipt = reth_primitives::Receipt,
>,
P: DatabaseProviderFactory
+ BlockReader<Block = reth_primitives::Block>
+ BlockReader<Block = N::Block, Header = N::BlockHeader>
+ StateProviderFactory
+ StateReader<Receipt = reth_primitives::Receipt>
+ Clone
Expand Down Expand Up @@ -1357,7 +1361,7 @@ where
// update the tracked chain height, after backfill sync both the canonical height and
// persisted height are the same
self.state.tree_state.set_canonical_head(new_head.num_hash());
self.persistence_state.finish(new_head.hash(), new_head.number);
self.persistence_state.finish(new_head.hash(), new_head.number());

// update the tracked canonical head
self.canonical_in_memory_state.set_canonical_head(new_head);
Expand Down Expand Up @@ -1622,7 +1626,7 @@ where

// the hash could belong to an unknown block or a persisted block
if let Some(header) = self.provider.header(&hash)? {
debug!(target: "engine::tree", %hash, number = %header.number, "found canonical state for block in database");
debug!(target: "engine::tree", %hash, number = %header.number(), "found canonical state for block in database");
// the block is known and persisted
let historical = self.provider.state_by_block_hash(hash)?;
return Ok(Some(historical))
Expand Down
Loading

0 comments on commit 332cce1

Please sign in to comment.