mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-04-08 03:01:12 -04:00
refactor: use alloy_primitives::map for all HashMap/HashSet types (#21686)
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
committed by
GitHub
parent
98313a0bea
commit
f53f90d714
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::{engine::DownloadRequest, metrics::BlockDownloaderMetrics};
|
||||
use alloy_consensus::BlockHeader;
|
||||
use alloy_primitives::B256;
|
||||
use alloy_primitives::{map::B256Set, B256};
|
||||
use futures::FutureExt;
|
||||
use reth_consensus::Consensus;
|
||||
use reth_network_p2p::{
|
||||
@@ -12,7 +12,7 @@ use reth_network_p2p::{
|
||||
use reth_primitives_traits::{Block, SealedBlock};
|
||||
use std::{
|
||||
cmp::{Ordering, Reverse},
|
||||
collections::{binary_heap::PeekMut, BinaryHeap, HashSet, VecDeque},
|
||||
collections::{binary_heap::PeekMut, BinaryHeap, VecDeque},
|
||||
fmt::Debug,
|
||||
sync::Arc,
|
||||
task::{Context, Poll},
|
||||
@@ -109,7 +109,7 @@ where
|
||||
}
|
||||
|
||||
/// Processes a block set download request.
|
||||
fn download_block_set(&mut self, hashes: HashSet<B256>) {
|
||||
fn download_block_set(&mut self, hashes: B256Set) {
|
||||
for hash in hashes {
|
||||
self.download_full_block(hash);
|
||||
}
|
||||
@@ -397,7 +397,7 @@ mod tests {
|
||||
|
||||
// send block set download request
|
||||
block_downloader.on_action(DownloadAction::Download(DownloadRequest::BlockSet(
|
||||
HashSet::from([tip.hash(), tip.parent_hash]),
|
||||
B256Set::from_iter([tip.hash(), tip.parent_hash]),
|
||||
)));
|
||||
|
||||
// ensure we have TOTAL_BLOCKS in flight full block request
|
||||
@@ -440,7 +440,7 @@ mod tests {
|
||||
)));
|
||||
|
||||
// send block set download request
|
||||
let download_set = HashSet::from([tip.hash(), tip.parent_hash]);
|
||||
let download_set = B256Set::from_iter([tip.hash(), tip.parent_hash]);
|
||||
block_downloader
|
||||
.on_action(DownloadAction::Download(DownloadRequest::BlockSet(download_set.clone())));
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::{
|
||||
chain::{ChainHandler, FromOrchestrator, HandlerEvent},
|
||||
download::{BlockDownloader, DownloadAction, DownloadOutcome},
|
||||
};
|
||||
use alloy_primitives::B256;
|
||||
use alloy_primitives::{map::B256Set, B256};
|
||||
use crossbeam_channel::Sender;
|
||||
use futures::{Stream, StreamExt};
|
||||
use reth_chain_state::ExecutedBlock;
|
||||
@@ -14,7 +14,6 @@ use reth_ethereum_primitives::EthPrimitives;
|
||||
use reth_payload_primitives::PayloadTypes;
|
||||
use reth_primitives_traits::{Block, NodePrimitives, SealedBlock};
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fmt::Display,
|
||||
task::{ready, Context, Poll},
|
||||
};
|
||||
@@ -341,7 +340,7 @@ pub enum RequestHandlerEvent<T> {
|
||||
#[derive(Debug)]
|
||||
pub enum DownloadRequest {
|
||||
/// Download the given set of blocks.
|
||||
BlockSet(HashSet<B256>),
|
||||
BlockSet(B256Set),
|
||||
/// Download the given range of blocks.
|
||||
BlockRange(B256, u64),
|
||||
}
|
||||
@@ -349,6 +348,6 @@ pub enum DownloadRequest {
|
||||
impl DownloadRequest {
|
||||
/// Returns a [`DownloadRequest`] for a single block.
|
||||
pub fn single_block(hash: B256) -> Self {
|
||||
Self::BlockSet(HashSet::from([hash]))
|
||||
Self::BlockSet(B256Set::from_iter([hash]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use crate::engine::EngineApiKind;
|
||||
use alloy_eips::BlockNumHash;
|
||||
use alloy_primitives::{
|
||||
map::{HashMap, HashSet},
|
||||
map::{B256Map, B256Set},
|
||||
BlockNumber, B256,
|
||||
};
|
||||
use reth_chain_state::{DeferredTrieData, EthPrimitives, ExecutedBlock, LazyOverlay};
|
||||
@@ -25,7 +25,7 @@ pub struct TreeState<N: NodePrimitives = EthPrimitives> {
|
||||
/// __All__ unique executed blocks by block hash that are connected to the canonical chain.
|
||||
///
|
||||
/// This includes blocks of all forks.
|
||||
pub(crate) blocks_by_hash: HashMap<B256, ExecutedBlock<N>>,
|
||||
pub(crate) blocks_by_hash: B256Map<ExecutedBlock<N>>,
|
||||
/// Executed blocks grouped by their respective block number.
|
||||
///
|
||||
/// This maps unique block number to all known blocks for that height.
|
||||
@@ -33,7 +33,7 @@ pub struct TreeState<N: NodePrimitives = EthPrimitives> {
|
||||
/// Note: there can be multiple blocks at the same height due to forks.
|
||||
pub(crate) blocks_by_number: BTreeMap<BlockNumber, Vec<ExecutedBlock<N>>>,
|
||||
/// Map of any parent block hash to its children.
|
||||
pub(crate) parent_to_child: HashMap<B256, HashSet<B256>>,
|
||||
pub(crate) parent_to_child: B256Map<B256Set>,
|
||||
/// Currently tracked canonical head of the chain.
|
||||
pub(crate) current_canonical_head: BlockNumHash,
|
||||
/// The engine API variant of this handler
|
||||
@@ -50,10 +50,10 @@ impl<N: NodePrimitives> TreeState<N> {
|
||||
/// Returns a new, empty tree state that points to the given canonical head.
|
||||
pub fn new(current_canonical_head: BlockNumHash, engine_kind: EngineApiKind) -> Self {
|
||||
Self {
|
||||
blocks_by_hash: HashMap::default(),
|
||||
blocks_by_hash: B256Map::default(),
|
||||
blocks_by_number: BTreeMap::new(),
|
||||
current_canonical_head,
|
||||
parent_to_child: HashMap::default(),
|
||||
parent_to_child: B256Map::default(),
|
||||
engine_kind,
|
||||
cached_canonical_overlay: None,
|
||||
}
|
||||
@@ -178,7 +178,7 @@ impl<N: NodePrimitives> TreeState<N> {
|
||||
/// ## Returns
|
||||
///
|
||||
/// The removed block and the block hashes of its children.
|
||||
fn remove_by_hash(&mut self, hash: B256) -> Option<(ExecutedBlock<N>, HashSet<B256>)> {
|
||||
fn remove_by_hash(&mut self, hash: B256) -> Option<(ExecutedBlock<N>, B256Set)> {
|
||||
let executed = self.blocks_by_hash.remove(&hash)?;
|
||||
|
||||
// Remove this block from collection of children of its parent block.
|
||||
@@ -489,7 +489,7 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
tree_state.parent_to_child.get(&blocks[0].recovered_block().hash()),
|
||||
Some(&HashSet::from_iter([blocks[1].recovered_block().hash()]))
|
||||
Some(&B256Set::from_iter([blocks[1].recovered_block().hash()]))
|
||||
);
|
||||
|
||||
assert!(!tree_state.parent_to_child.contains_key(&blocks[1].recovered_block().hash()));
|
||||
@@ -498,7 +498,7 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
tree_state.parent_to_child.get(&blocks[1].recovered_block().hash()),
|
||||
Some(&HashSet::from_iter([blocks[2].recovered_block().hash()]))
|
||||
Some(&B256Set::from_iter([blocks[2].recovered_block().hash()]))
|
||||
);
|
||||
assert!(tree_state.parent_to_child.contains_key(&blocks[1].recovered_block().hash()));
|
||||
|
||||
@@ -586,11 +586,11 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
tree_state.parent_to_child.get(&blocks[2].recovered_block().hash()),
|
||||
Some(&HashSet::from_iter([blocks[3].recovered_block().hash()]))
|
||||
Some(&B256Set::from_iter([blocks[3].recovered_block().hash()]))
|
||||
);
|
||||
assert_eq!(
|
||||
tree_state.parent_to_child.get(&blocks[3].recovered_block().hash()),
|
||||
Some(&HashSet::from_iter([blocks[4].recovered_block().hash()]))
|
||||
Some(&B256Set::from_iter([blocks[4].recovered_block().hash()]))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -636,11 +636,11 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
tree_state.parent_to_child.get(&blocks[2].recovered_block().hash()),
|
||||
Some(&HashSet::from_iter([blocks[3].recovered_block().hash()]))
|
||||
Some(&B256Set::from_iter([blocks[3].recovered_block().hash()]))
|
||||
);
|
||||
assert_eq!(
|
||||
tree_state.parent_to_child.get(&blocks[3].recovered_block().hash()),
|
||||
Some(&HashSet::from_iter([blocks[4].recovered_block().hash()]))
|
||||
Some(&B256Set::from_iter([blocks[4].recovered_block().hash()]))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -686,11 +686,11 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
tree_state.parent_to_child.get(&blocks[2].recovered_block().hash()),
|
||||
Some(&HashSet::from_iter([blocks[3].recovered_block().hash()]))
|
||||
Some(&B256Set::from_iter([blocks[3].recovered_block().hash()]))
|
||||
);
|
||||
assert_eq!(
|
||||
tree_state.parent_to_child.get(&blocks[3].recovered_block().hash()),
|
||||
Some(&HashSet::from_iter([blocks[4].recovered_block().hash()]))
|
||||
Some(&B256Set::from_iter([blocks[4].recovered_block().hash()]))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ use reth_trie_db::ChangesetCache;
|
||||
|
||||
use alloy_eips::eip1898::BlockWithParent;
|
||||
use alloy_primitives::{
|
||||
map::{HashMap, HashSet},
|
||||
map::{B256Map, B256Set},
|
||||
Bytes, B256,
|
||||
};
|
||||
use alloy_rlp::Decodable;
|
||||
@@ -235,11 +235,11 @@ impl TestHarness {
|
||||
}
|
||||
|
||||
fn with_blocks(mut self, blocks: Vec<ExecutedBlock>) -> Self {
|
||||
let mut blocks_by_hash = HashMap::default();
|
||||
let mut blocks_by_hash = B256Map::default();
|
||||
let mut blocks_by_number = BTreeMap::new();
|
||||
let mut state_by_hash = HashMap::default();
|
||||
let mut state_by_hash = B256Map::default();
|
||||
let mut hash_by_number = BTreeMap::new();
|
||||
let mut parent_to_child: HashMap<B256, HashSet<B256>> = HashMap::default();
|
||||
let mut parent_to_child: B256Map<B256Set> = B256Map::default();
|
||||
let mut parent_hash = B256::ZERO;
|
||||
|
||||
for block in &blocks {
|
||||
@@ -957,7 +957,7 @@ async fn test_engine_tree_fcu_missing_head() {
|
||||
let event = test_harness.from_tree_rx.recv().await.unwrap();
|
||||
match event {
|
||||
EngineApiEvent::Download(DownloadRequest::BlockSet(actual_block_set)) => {
|
||||
let expected_block_set = HashSet::from_iter([missing_block.hash()]);
|
||||
let expected_block_set = B256Set::from_iter([missing_block.hash()]);
|
||||
assert_eq!(actual_block_set, expected_block_set);
|
||||
}
|
||||
_ => panic!("Unexpected event: {event:#?}"),
|
||||
@@ -1002,7 +1002,7 @@ async fn test_engine_tree_live_sync_transition_required_blocks_requested() {
|
||||
let event = test_harness.from_tree_rx.recv().await.unwrap();
|
||||
match event {
|
||||
EngineApiEvent::Download(DownloadRequest::BlockSet(hash_set)) => {
|
||||
assert_eq!(hash_set, HashSet::from_iter([main_chain_last_hash]));
|
||||
assert_eq!(hash_set, B256Set::from_iter([main_chain_last_hash]));
|
||||
}
|
||||
_ => panic!("Unexpected event: {event:#?}"),
|
||||
}
|
||||
@@ -1011,7 +1011,7 @@ async fn test_engine_tree_live_sync_transition_required_blocks_requested() {
|
||||
let event = test_harness.from_tree_rx.recv().await.unwrap();
|
||||
match event {
|
||||
EngineApiEvent::Download(DownloadRequest::BlockSet(hash_set)) => {
|
||||
assert_eq!(hash_set, HashSet::from_iter([main_chain_last_hash]));
|
||||
assert_eq!(hash_set, B256Set::from_iter([main_chain_last_hash]));
|
||||
}
|
||||
_ => panic!("Unexpected event: {event:#?}"),
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use alloy_primitives::{map::HashMap, B256};
|
||||
use alloy_primitives::{
|
||||
map::{B256Map, HashMap},
|
||||
B256,
|
||||
};
|
||||
use reth_db::DatabaseError;
|
||||
use reth_trie::{
|
||||
trie_cursor::{TrieCursor, TrieCursorFactory},
|
||||
@@ -19,7 +22,7 @@ struct EntryDiff<T> {
|
||||
struct TrieUpdatesDiff {
|
||||
account_nodes: HashMap<Nibbles, EntryDiff<Option<BranchNodeCompact>>>,
|
||||
removed_nodes: HashMap<Nibbles, EntryDiff<bool>>,
|
||||
storage_tries: HashMap<B256, StorageTrieUpdatesDiff>,
|
||||
storage_tries: B256Map<StorageTrieUpdatesDiff>,
|
||||
}
|
||||
|
||||
impl TrieUpdatesDiff {
|
||||
|
||||
Reference in New Issue
Block a user