From cc4a418ddf73ae5af240b403e4b6e1d191130cee Mon Sep 17 00:00:00 2001 From: Andrzej Sulkowski <111314156+andrzejSulkowski@users.noreply.github.com> Date: Sat, 27 Apr 2024 06:18:59 +0200 Subject: [PATCH] refactor: extract peer types to net/types (#7912) Co-authored-by: Matthias Seitz --- Cargo.lock | 30 ++- Cargo.toml | 4 +- crates/consensus/auto-seal/Cargo.toml | 1 + crates/consensus/auto-seal/src/client.rs | 5 +- crates/interfaces/Cargo.toml | 1 + crates/interfaces/src/p2p/download.rs | 2 +- crates/interfaces/src/p2p/either.rs | 2 +- crates/interfaces/src/p2p/error.rs | 5 +- crates/interfaces/src/p2p/full_block.rs | 3 +- crates/interfaces/src/test_utils/bodies.rs | 2 +- .../interfaces/src/test_utils/full_block.rs | 5 +- crates/interfaces/src/test_utils/headers.rs | 3 +- crates/net/common/Cargo.toml | 2 +- crates/net/common/src/ban_list.rs | 2 +- crates/net/discv4/Cargo.toml | 1 + crates/net/discv4/src/lib.rs | 6 +- crates/net/discv4/src/node.rs | 3 +- crates/net/discv4/src/proto.rs | 5 +- crates/net/discv4/src/table.rs | 2 +- crates/net/discv4/src/test_utils.rs | 3 +- crates/net/discv5/Cargo.toml | 1 + crates/net/discv5/src/enr.rs | 2 +- crates/net/discv5/src/lib.rs | 3 +- crates/net/dns/Cargo.toml | 1 + crates/net/dns/src/lib.rs | 3 +- crates/net/downloaders/Cargo.toml | 1 + crates/net/downloaders/src/bodies/request.rs | 5 +- crates/net/downloaders/src/file_client.rs | 3 +- .../src/headers/reverse_headers.rs | 4 +- .../src/test_utils/bodies_client.rs | 3 +- crates/net/ecies/Cargo.toml | 1 + crates/net/ecies/src/algorithm.rs | 3 +- crates/net/ecies/src/stream.rs | 2 +- crates/net/eth-wire/Cargo.toml | 1 + crates/net/eth-wire/src/ethstream.rs | 3 +- crates/net/eth-wire/src/hello.rs | 9 +- crates/net/eth-wire/src/muxdemux.rs | 3 +- crates/net/eth-wire/src/test_utils.rs | 3 +- crates/net/network-api/Cargo.toml | 1 + crates/net/network-api/src/lib.rs | 3 +- crates/net/network-api/src/noop.rs | 3 +- crates/net/network/Cargo.toml | 1 + crates/net/network/src/config.rs | 4 +- crates/net/network/src/discovery.rs | 3 +- crates/net/network/src/eth_requests.rs | 3 +- crates/net/network/src/fetch/client.rs | 3 +- crates/net/network/src/fetch/mod.rs | 3 +- crates/net/network/src/import.rs | 2 +- crates/net/network/src/manager.rs | 3 +- crates/net/network/src/message.rs | 3 +- crates/net/network/src/network.rs | 3 +- crates/net/network/src/peers/manager.rs | 6 +- crates/net/network/src/session/active.rs | 5 +- crates/net/network/src/session/handle.rs | 2 +- crates/net/network/src/session/mod.rs | 3 +- crates/net/network/src/state.rs | 6 +- crates/net/network/src/swarm.rs | 2 +- crates/net/network/src/test_utils/init.rs | 2 +- crates/net/network/src/test_utils/testnet.rs | 3 +- .../net/network/src/transactions/fetcher.rs | 3 +- crates/net/network/src/transactions/mod.rs | 4 +- crates/net/types/Cargo.toml | 27 ++ crates/net/types/src/lib.rs | 239 ++++++++++++++++++ crates/primitives/Cargo.toml | 2 - crates/primitives/src/lib.rs | 2 - crates/rpc/rpc-api/Cargo.toml | 1 + crates/rpc/rpc-api/src/admin.rs | 3 +- crates/rpc/rpc/Cargo.toml | 1 + crates/rpc/rpc/src/admin.rs | 3 +- crates/stages/Cargo.toml | 1 + crates/stages/src/lib.rs | 3 +- crates/transaction-pool/Cargo.toml | 1 + crates/transaction-pool/src/traits.rs | 8 +- examples/manual-p2p/Cargo.toml | 11 +- examples/manual-p2p/src/main.rs | 3 +- 75 files changed, 428 insertions(+), 86 deletions(-) create mode 100644 crates/net/types/Cargo.toml create mode 100644 crates/net/types/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c66304e91..a40fb4513 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4741,6 +4741,7 @@ dependencies = [ "reth-ecies", "reth-eth-wire", "reth-network", + "reth-network-types", "reth-primitives", "secp256k1", "tokio", @@ -6323,6 +6324,7 @@ dependencies = [ "reth-engine-primitives", "reth-evm", "reth-interfaces", + "reth-network-types", "reth-primitives", "reth-provider", "reth-revm", @@ -6553,6 +6555,7 @@ dependencies = [ "rand 0.8.5", "reth-net-common", "reth-net-nat", + "reth-network-types", "reth-primitives", "reth-tracing", "secp256k1", @@ -6578,6 +6581,7 @@ dependencies = [ "multiaddr", "rand 0.8.5", "reth-metrics", + "reth-network-types", "reth-primitives", "reth-tracing", "secp256k1", @@ -6597,6 +6601,7 @@ dependencies = [ "parking_lot 0.12.2", "rand 0.8.5", "reth-net-common", + "reth-network-types", "reth-primitives", "reth-tracing", "schnellru", @@ -6628,6 +6633,7 @@ dependencies = [ "reth-db", "reth-interfaces", "reth-metrics", + "reth-network-types", "reth-primitives", "reth-provider", "reth-tasks", @@ -6689,6 +6695,7 @@ dependencies = [ "pin-project", "rand 0.8.5", "reth-net-common", + "reth-network-types", "reth-primitives", "secp256k1", "sha2 0.10.8", @@ -6732,6 +6739,7 @@ dependencies = [ "reth-eth-wire-types", "reth-metrics", "reth-net-common", + "reth-network-types", "reth-primitives", "reth-tracing", "secp256k1", @@ -6888,6 +6896,7 @@ dependencies = [ "reth-consensus", "reth-eth-wire-types", "reth-network-api", + "reth-network-types", "reth-primitives", "secp256k1", "thiserror", @@ -6978,7 +6987,7 @@ name = "reth-net-common" version = "0.2.0-beta.6" dependencies = [ "pin-project", - "reth-primitives", + "reth-network-types", "tokio", ] @@ -7031,6 +7040,7 @@ dependencies = [ "reth-net-common", "reth-network", "reth-network-api", + "reth-network-types", "reth-primitives", "reth-provider", "reth-rpc-types", @@ -7059,6 +7069,7 @@ dependencies = [ "enr", "reth-discv4", "reth-eth-wire", + "reth-network-types", "reth-primitives", "reth-rpc-types", "serde", @@ -7066,6 +7077,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "reth-network-types" +version = "0.2.0-beta.6" +dependencies = [ + "enr", + "reth-primitives", + "reth-rpc-types", + "secp256k1", + "serde_with", +] + [[package]] name = "reth-nippy-jar" version = "0.2.0-beta.6" @@ -7369,7 +7391,6 @@ dependencies = [ "clap", "criterion", "derive_more", - "enr", "hash-db", "itertools 0.12.1", "modular-bitfield", @@ -7390,7 +7411,6 @@ dependencies = [ "secp256k1", "serde", "serde_json", - "serde_with", "sha2 0.10.8", "strum 0.26.2", "sucds", @@ -7500,6 +7520,7 @@ dependencies = [ "reth-interfaces", "reth-metrics", "reth-network-api", + "reth-network-types", "reth-primitives", "reth-provider", "reth-revm", @@ -7531,6 +7552,7 @@ version = "0.2.0-beta.6" dependencies = [ "jsonrpsee", "reth-engine-primitives", + "reth-network-types", "reth-primitives", "reth-rpc-types", "serde", @@ -7679,6 +7701,7 @@ dependencies = [ "reth-evm-ethereum", "reth-exex", "reth-interfaces", + "reth-network-types", "reth-primitives", "reth-provider", "reth-revm", @@ -7794,6 +7817,7 @@ dependencies = [ "rand 0.8.5", "reth-eth-wire", "reth-metrics", + "reth-network-types", "reth-primitives", "reth-provider", "reth-revm", diff --git a/Cargo.toml b/Cargo.toml index b62467405..d56392c1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ members = [ "crates/net/nat/", "crates/net/network/", "crates/net/network-api/", + "crates/net/types/", "crates/payload/basic/", "crates/payload/builder/", "crates/payload/ethereum/", @@ -223,7 +224,6 @@ reth-engine-primitives = { path = "crates/engine-primitives" } reth-ethereum-engine-primitives = { path = "crates/ethereum/engine-primitives" } reth-node-builder = { path = "crates/node-builder" } reth-node-ethereum = { path = "crates/node-ethereum" } -reth-node-events = { path = "crates/node/events" } reth-node-optimism = { path = "crates/optimism/node" } reth-evm-optimism = { path = "crates/optimism/evm" } reth-node-core = { path = "crates/node-core" } @@ -249,6 +249,7 @@ reth-net-common = { path = "crates/net/common" } reth-net-nat = { path = "crates/net/nat" } reth-network = { path = "crates/net/network" } reth-network-api = { path = "crates/net/network-api" } +reth-network-types = { path = "crates/net/types" } reth-nippy-jar = { path = "crates/storage/nippy-jar" } reth-payload-builder = { path = "crates/payload/builder" } reth-payload-validator = { path = "crates/payload/validator" } @@ -273,6 +274,7 @@ reth-transaction-pool = { path = "crates/transaction-pool" } reth-trie = { path = "crates/trie" } reth-trie-parallel = { path = "crates/trie-parallel" } reth-optimism-consensus = { path = "crates/optimism/consensus" } +reth-node-events = { path = "crates/node/events" } # revm revm = { version = "8.0.0", features = ["std", "secp256k1"], default-features = false } diff --git a/crates/consensus/auto-seal/Cargo.toml b/crates/consensus/auto-seal/Cargo.toml index ec96426a4..435ade53d 100644 --- a/crates/consensus/auto-seal/Cargo.toml +++ b/crates/consensus/auto-seal/Cargo.toml @@ -24,6 +24,7 @@ reth-evm.workspace = true reth-engine-primitives.workspace = true reth-consensus.workspace = true reth-rpc-types.workspace = true +reth-network-types.workspace = true # async futures-util.workspace = true diff --git a/crates/consensus/auto-seal/src/client.rs b/crates/consensus/auto-seal/src/client.rs index 7ed69c289..67a84d5d9 100644 --- a/crates/consensus/auto-seal/src/client.rs +++ b/crates/consensus/auto-seal/src/client.rs @@ -7,9 +7,8 @@ use reth_interfaces::p2p::{ headers::client::{HeadersClient, HeadersFut, HeadersRequest}, priority::Priority, }; -use reth_primitives::{ - BlockBody, BlockHashOrNumber, Header, HeadersDirection, PeerId, WithPeerId, B256, -}; +use reth_network_types::{PeerId, WithPeerId}; +use reth_primitives::{BlockBody, BlockHashOrNumber, Header, HeadersDirection, B256}; use std::fmt::Debug; use tracing::{trace, warn}; diff --git a/crates/interfaces/Cargo.toml b/crates/interfaces/Cargo.toml index 6c066593b..c2e276a33 100644 --- a/crates/interfaces/Cargo.toml +++ b/crates/interfaces/Cargo.toml @@ -15,6 +15,7 @@ reth-primitives.workspace = true reth-network-api.workspace = true reth-eth-wire-types.workspace = true reth-consensus.workspace = true +reth-network-types.workspace = true # async futures.workspace = true diff --git a/crates/interfaces/src/p2p/download.rs b/crates/interfaces/src/p2p/download.rs index b9fb6ab3e..823860507 100644 --- a/crates/interfaces/src/p2p/download.rs +++ b/crates/interfaces/src/p2p/download.rs @@ -1,4 +1,4 @@ -use reth_primitives::PeerId; +use reth_network_types::PeerId; use std::fmt::Debug; /// Generic download client for peer penalization diff --git a/crates/interfaces/src/p2p/either.rs b/crates/interfaces/src/p2p/either.rs index 1a6bd170c..af7f15018 100644 --- a/crates/interfaces/src/p2p/either.rs +++ b/crates/interfaces/src/p2p/either.rs @@ -22,7 +22,7 @@ where A: DownloadClient, B: DownloadClient, { - fn report_bad_message(&self, peer_id: reth_primitives::PeerId) { + fn report_bad_message(&self, peer_id: reth_network_types::PeerId) { match self { EitherDownloader::Left(a) => a.report_bad_message(peer_id), EitherDownloader::Right(b) => b.report_bad_message(peer_id), diff --git a/crates/interfaces/src/p2p/error.rs b/crates/interfaces/src/p2p/error.rs index f63f8879a..1a847b649 100644 --- a/crates/interfaces/src/p2p/error.rs +++ b/crates/interfaces/src/p2p/error.rs @@ -2,8 +2,9 @@ use super::headers::client::HeadersRequest; use crate::{db::DatabaseError, provider::ProviderError}; use reth_consensus::ConsensusError; use reth_network_api::ReputationChangeKind; +use reth_network_types::WithPeerId; use reth_primitives::{ - BlockHashOrNumber, BlockNumber, GotExpected, GotExpectedBoxed, Header, WithPeerId, B256, + BlockHashOrNumber, BlockNumber, GotExpected, GotExpectedBoxed, Header, B256, }; use std::ops::RangeInclusive; use thiserror::Error; @@ -12,7 +13,7 @@ use tokio::sync::{mpsc, oneshot}; /// Result alias for result of a request. pub type RequestResult = Result; -/// Result with [PeerId][reth_primitives::PeerId] +/// Result with [PeerId][reth_network_types::PeerId] pub type PeerRequestResult = RequestResult>; /// Helper trait used to validate responses. diff --git a/crates/interfaces/src/p2p/full_block.rs b/crates/interfaces/src/p2p/full_block.rs index 6cf3f2c81..dd8cfff4d 100644 --- a/crates/interfaces/src/p2p/full_block.rs +++ b/crates/interfaces/src/p2p/full_block.rs @@ -6,8 +6,9 @@ use crate::p2p::{ }; use futures::Stream; use reth_consensus::{Consensus, ConsensusError}; +use reth_network_types::WithPeerId; use reth_primitives::{ - BlockBody, GotExpected, Header, HeadersDirection, SealedBlock, SealedHeader, WithPeerId, B256, + BlockBody, GotExpected, Header, HeadersDirection, SealedBlock, SealedHeader, B256, }; use std::{ cmp::Reverse, diff --git a/crates/interfaces/src/test_utils/bodies.rs b/crates/interfaces/src/test_utils/bodies.rs index e1d42a2a5..8f0bfcef0 100644 --- a/crates/interfaces/src/test_utils/bodies.rs +++ b/crates/interfaces/src/test_utils/bodies.rs @@ -22,7 +22,7 @@ impl Debug for TestBodiesClient { } impl DownloadClient for TestBodiesClient { - fn report_bad_message(&self, _peer_id: reth_primitives::PeerId) { + fn report_bad_message(&self, _peer_id: reth_network_types::PeerId) { // noop } diff --git a/crates/interfaces/src/test_utils/full_block.rs b/crates/interfaces/src/test_utils/full_block.rs index a97104919..95c1c2b3a 100644 --- a/crates/interfaces/src/test_utils/full_block.rs +++ b/crates/interfaces/src/test_utils/full_block.rs @@ -6,9 +6,10 @@ use crate::p2p::{ priority::Priority, }; use parking_lot::Mutex; +use reth_network_types::{PeerId, WithPeerId}; use reth_primitives::{ - BlockBody, BlockHashOrNumber, BlockNumHash, Header, HeadersDirection, PeerId, SealedBlock, - SealedHeader, WithPeerId, B256, + BlockBody, BlockHashOrNumber, BlockNumHash, Header, HeadersDirection, SealedBlock, + SealedHeader, B256, }; use std::{collections::HashMap, sync::Arc}; diff --git a/crates/interfaces/src/test_utils/headers.rs b/crates/interfaces/src/test_utils/headers.rs index 304f394c8..0272c68d3 100644 --- a/crates/interfaces/src/test_utils/headers.rs +++ b/crates/interfaces/src/test_utils/headers.rs @@ -24,7 +24,8 @@ use crate::p2p::{ priority::Priority, }; use reth_consensus::{test_utils::TestConsensus, Consensus}; -use reth_primitives::{Header, HeadersDirection, PeerId, SealedHeader, WithPeerId}; +use reth_network_types::{PeerId, WithPeerId}; +use reth_primitives::{Header, HeadersDirection, SealedHeader}; /// A test downloader which just returns the values that have been pushed to it. #[derive(Debug)] diff --git a/crates/net/common/Cargo.toml b/crates/net/common/Cargo.toml index 8d85fc906..0c3b253a5 100644 --- a/crates/net/common/Cargo.toml +++ b/crates/net/common/Cargo.toml @@ -13,7 +13,7 @@ workspace = true [dependencies] # reth -reth-primitives.workspace = true +reth-network-types.workspace = true # async pin-project.workspace = true diff --git a/crates/net/common/src/ban_list.rs b/crates/net/common/src/ban_list.rs index 0527c8620..11d4c6049 100644 --- a/crates/net/common/src/ban_list.rs +++ b/crates/net/common/src/ban_list.rs @@ -1,6 +1,6 @@ //! Support for banning peers. -use reth_primitives::PeerId; +use reth_network_types::PeerId; use std::{collections::HashMap, net::IpAddr, time::Instant}; /// Determines whether or not the IP is globally routable. diff --git a/crates/net/discv4/Cargo.toml b/crates/net/discv4/Cargo.toml index 9a7cb943d..bd7e99ee6 100644 --- a/crates/net/discv4/Cargo.toml +++ b/crates/net/discv4/Cargo.toml @@ -16,6 +16,7 @@ workspace = true reth-primitives.workspace = true reth-net-common.workspace = true reth-net-nat.workspace = true +reth-network-types.workspace = true # ethereum alloy-rlp = { workspace = true, features = ["derive"] } diff --git a/crates/net/discv4/src/lib.rs b/crates/net/discv4/src/lib.rs index 071b81df9..061e4a33b 100644 --- a/crates/net/discv4/src/lib.rs +++ b/crates/net/discv4/src/lib.rs @@ -39,7 +39,8 @@ use discv5::{ use enr::Enr; use parking_lot::Mutex; use proto::{EnrRequest, EnrResponse}; -use reth_primitives::{bytes::Bytes, hex, ForkId, PeerId, B256}; +use reth_network_types::PeerId; +use reth_primitives::{bytes::Bytes, hex, ForkId, B256}; use secp256k1::SecretKey; use std::{ cell::RefCell, @@ -210,7 +211,8 @@ impl Discv4 { /// # use std::io; /// use rand::thread_rng; /// use reth_discv4::{Discv4, Discv4Config}; - /// use reth_primitives::{pk2id, NodeRecord, PeerId}; + /// use reth_network_types::{pk2id, PeerId}; + /// use reth_primitives::NodeRecord; /// use secp256k1::SECP256K1; /// use std::{net::SocketAddr, str::FromStr}; /// # async fn t() -> io::Result<()> { diff --git a/crates/net/discv4/src/node.rs b/crates/net/discv4/src/node.rs index 2e8dc1773..62e45db0e 100644 --- a/crates/net/discv4/src/node.rs +++ b/crates/net/discv4/src/node.rs @@ -1,5 +1,6 @@ use generic_array::GenericArray; -use reth_primitives::{keccak256, NodeRecord, PeerId}; +use reth_network_types::PeerId; +use reth_primitives::{keccak256, NodeRecord}; /// The key type for the table. #[derive(Debug, Copy, Clone, Eq, PartialEq)] diff --git a/crates/net/discv4/src/proto.rs b/crates/net/discv4/src/proto.rs index 059ecc5bb..da84dc05a 100644 --- a/crates/net/discv4/src/proto.rs +++ b/crates/net/discv4/src/proto.rs @@ -1,11 +1,12 @@ //! Discovery v4 protocol implementation. -use crate::{error::DecodePacketError, PeerId, MAX_PACKET_SIZE, MIN_PACKET_SIZE}; +use crate::{error::DecodePacketError, MAX_PACKET_SIZE, MIN_PACKET_SIZE}; use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header, RlpDecodable, RlpEncodable}; use enr::Enr; +use reth_network_types::{pk2id, PeerId}; use reth_primitives::{ bytes::{Buf, BufMut, Bytes, BytesMut}, - keccak256, pk2id, EnrForkIdEntry, ForkId, NodeRecord, B256, + keccak256, EnrForkIdEntry, ForkId, NodeRecord, B256, }; use secp256k1::{ ecdsa::{RecoverableSignature, RecoveryId}, diff --git a/crates/net/discv4/src/table.rs b/crates/net/discv4/src/table.rs index c7d75778c..00e1fe50c 100644 --- a/crates/net/discv4/src/table.rs +++ b/crates/net/discv4/src/table.rs @@ -1,6 +1,6 @@ //! Additional support for tracking nodes. -use reth_primitives::PeerId; +use reth_network_types::PeerId; use std::{collections::HashMap, net::IpAddr, time::Instant}; /// Keeps track of nodes from which we have received a `Pong` message. diff --git a/crates/net/discv4/src/test_utils.rs b/crates/net/discv4/src/test_utils.rs index ccd4f9a03..dae3ea388 100644 --- a/crates/net/discv4/src/test_utils.rs +++ b/crates/net/discv4/src/test_utils.rs @@ -6,7 +6,8 @@ use crate::{ IngressReceiver, PeerId, SAFE_MAX_DATAGRAM_NEIGHBOUR_RECORDS, }; use rand::{thread_rng, Rng, RngCore}; -use reth_primitives::{hex, pk2id, ForkHash, ForkId, NodeRecord, B256}; +use reth_network_types::pk2id; +use reth_primitives::{hex, ForkHash, ForkId, NodeRecord, B256}; use secp256k1::{SecretKey, SECP256K1}; use std::{ collections::{HashMap, HashSet}, diff --git a/crates/net/discv5/Cargo.toml b/crates/net/discv5/Cargo.toml index 705ea17a8..a73888ae0 100644 --- a/crates/net/discv5/Cargo.toml +++ b/crates/net/discv5/Cargo.toml @@ -15,6 +15,7 @@ workspace = true # reth reth-primitives.workspace = true reth-metrics.workspace = true +reth-network-types.workspace = true # ethereum alloy-rlp.workspace = true diff --git a/crates/net/discv5/src/enr.rs b/crates/net/discv5/src/enr.rs index 088baf18e..162370bb4 100644 --- a/crates/net/discv5/src/enr.rs +++ b/crates/net/discv5/src/enr.rs @@ -3,7 +3,7 @@ use discv5::enr::{CombinedPublicKey, EnrPublicKey, NodeId}; use enr::Enr; -use reth_primitives::{id2pk, pk2id, PeerId}; +use reth_network_types::{id2pk, pk2id, PeerId}; use secp256k1::{PublicKey, SecretKey}; /// Extracts a [`CombinedPublicKey::Secp256k1`] from a [`discv5::Enr`] and converts it to a diff --git a/crates/net/discv5/src/lib.rs b/crates/net/discv5/src/lib.rs index 14414abf7..5275956bf 100644 --- a/crates/net/discv5/src/lib.rs +++ b/crates/net/discv5/src/lib.rs @@ -22,7 +22,8 @@ use enr::{discv4_id_to_discv5_id, EnrCombinedKeyWrapper}; use futures::future::join_all; use itertools::Itertools; use rand::{Rng, RngCore}; -use reth_primitives::{bytes::Bytes, EnrForkIdEntry, ForkId, NodeRecord, PeerId}; +use reth_network_types::PeerId; +use reth_primitives::{bytes::Bytes, EnrForkIdEntry, ForkId, NodeRecord}; use secp256k1::SecretKey; use tokio::{sync::mpsc, task}; use tracing::{debug, error, trace}; diff --git a/crates/net/dns/Cargo.toml b/crates/net/dns/Cargo.toml index 18d7bf815..8076bd4e1 100644 --- a/crates/net/dns/Cargo.toml +++ b/crates/net/dns/Cargo.toml @@ -15,6 +15,7 @@ workspace = true # reth reth-primitives.workspace = true reth-net-common.workspace = true +reth-network-types.workspace = true # ethereum secp256k1 = { workspace = true, features = ["global-context", "rand-std", "recovery", "serde"] } diff --git a/crates/net/dns/src/lib.rs b/crates/net/dns/src/lib.rs index b72a45b31..e5ddc0fd1 100644 --- a/crates/net/dns/src/lib.rs +++ b/crates/net/dns/src/lib.rs @@ -22,7 +22,8 @@ use crate::{ pub use config::DnsDiscoveryConfig; use enr::Enr; use error::ParseDnsEntryError; -use reth_primitives::{pk2id, EnrForkIdEntry, ForkId, NodeRecord}; +use reth_network_types::pk2id; +use reth_primitives::{EnrForkIdEntry, ForkId, NodeRecord}; use schnellru::{ByLength, LruMap}; use secp256k1::SecretKey; use std::{ diff --git a/crates/net/downloaders/Cargo.toml b/crates/net/downloaders/Cargo.toml index f1f14c85c..353956d3b 100644 --- a/crates/net/downloaders/Cargo.toml +++ b/crates/net/downloaders/Cargo.toml @@ -19,6 +19,7 @@ reth-tasks.workspace = true reth-provider.workspace = true reth-config.workspace = true reth-consensus.workspace = true +reth-network-types.workspace = true # async futures.workspace = true diff --git a/crates/net/downloaders/src/bodies/request.rs b/crates/net/downloaders/src/bodies/request.rs index 032fb3ebc..dfe877a0b 100644 --- a/crates/net/downloaders/src/bodies/request.rs +++ b/crates/net/downloaders/src/bodies/request.rs @@ -6,9 +6,8 @@ use reth_interfaces::p2p::{ error::{DownloadError, DownloadResult}, priority::Priority, }; -use reth_primitives::{ - BlockBody, GotExpected, PeerId, SealedBlock, SealedHeader, WithPeerId, B256, -}; +use reth_network_types::{PeerId, WithPeerId}; +use reth_primitives::{BlockBody, GotExpected, SealedBlock, SealedHeader, B256}; use std::{ collections::VecDeque, mem, diff --git a/crates/net/downloaders/src/file_client.rs b/crates/net/downloaders/src/file_client.rs index ce8f3898b..362ed3c40 100644 --- a/crates/net/downloaders/src/file_client.rs +++ b/crates/net/downloaders/src/file_client.rs @@ -7,9 +7,10 @@ use reth_interfaces::p2p::{ headers::client::{HeadersClient, HeadersFut, HeadersRequest}, priority::Priority, }; +use reth_network_types::PeerId; use reth_primitives::{ BlockBody, BlockHash, BlockHashOrNumber, BlockNumber, BytesMut, Header, HeadersDirection, - PeerId, SealedHeader, B256, + SealedHeader, B256, }; use std::{collections::HashMap, path::Path}; use thiserror::Error; diff --git a/crates/net/downloaders/src/headers/reverse_headers.rs b/crates/net/downloaders/src/headers/reverse_headers.rs index 5c12a161a..273f97e58 100644 --- a/crates/net/downloaders/src/headers/reverse_headers.rs +++ b/crates/net/downloaders/src/headers/reverse_headers.rs @@ -16,9 +16,9 @@ use reth_interfaces::p2p::{ }, priority::Priority, }; +use reth_network_types::PeerId; use reth_primitives::{ - BlockHashOrNumber, BlockNumber, GotExpected, Header, HeadersDirection, PeerId, SealedHeader, - B256, + BlockHashOrNumber, BlockNumber, GotExpected, Header, HeadersDirection, SealedHeader, B256, }; use reth_tasks::{TaskSpawner, TokioTaskExecutor}; use std::{ diff --git a/crates/net/downloaders/src/test_utils/bodies_client.rs b/crates/net/downloaders/src/test_utils/bodies_client.rs index 2f3cf2f29..a7387fa88 100644 --- a/crates/net/downloaders/src/test_utils/bodies_client.rs +++ b/crates/net/downloaders/src/test_utils/bodies_client.rs @@ -3,7 +3,8 @@ use reth_interfaces::p2p::{ download::DownloadClient, priority::Priority, }; -use reth_primitives::{BlockBody, PeerId, B256}; +use reth_network_types::PeerId; +use reth_primitives::{BlockBody, B256}; use std::{ collections::HashMap, fmt::Debug, diff --git a/crates/net/ecies/Cargo.toml b/crates/net/ecies/Cargo.toml index 461aad885..d4a4de32a 100644 --- a/crates/net/ecies/Cargo.toml +++ b/crates/net/ecies/Cargo.toml @@ -13,6 +13,7 @@ workspace = true [dependencies] reth-primitives.workspace = true reth-net-common.workspace = true +reth-network-types.workspace = true alloy-rlp = { workspace = true, features = ["derive"] } futures.workspace = true diff --git a/crates/net/ecies/src/algorithm.rs b/crates/net/ecies/src/algorithm.rs index bd1eb1d32..52398de4f 100644 --- a/crates/net/ecies/src/algorithm.rs +++ b/crates/net/ecies/src/algorithm.rs @@ -13,9 +13,10 @@ use ctr::Ctr64BE; use digest::{crypto_common::KeyIvInit, Digest}; use educe::Educe; use rand::{thread_rng, Rng}; +use reth_network_types::{id2pk, pk2id}; use reth_primitives::{ bytes::{BufMut, Bytes, BytesMut}, - id2pk, pk2id, B128, B256, B512 as PeerId, + B128, B256, B512 as PeerId, }; use secp256k1::{ ecdsa::{RecoverableSignature, RecoveryId}, diff --git a/crates/net/ecies/src/stream.rs b/crates/net/ecies/src/stream.rs index 47518aa25..4538fc059 100644 --- a/crates/net/ecies/src/stream.rs +++ b/crates/net/ecies/src/stream.rs @@ -175,7 +175,7 @@ where #[cfg(test)] mod tests { use super::*; - use reth_primitives::pk2id; + use reth_network_types::pk2id; use secp256k1::SECP256K1; use tokio::net::{TcpListener, TcpStream}; diff --git a/crates/net/eth-wire/Cargo.toml b/crates/net/eth-wire/Cargo.toml index cddc84cf9..0cfdfef24 100644 --- a/crates/net/eth-wire/Cargo.toml +++ b/crates/net/eth-wire/Cargo.toml @@ -19,6 +19,7 @@ reth-ecies.workspace = true alloy-rlp = { workspace = true, features = ["derive"] } reth-discv4.workspace = true reth-eth-wire-types.workspace = true +reth-network-types.workspace = true # metrics reth-metrics.workspace = true diff --git a/crates/net/eth-wire/src/ethstream.rs b/crates/net/eth-wire/src/ethstream.rs index cbd1e3150..8de509034 100644 --- a/crates/net/eth-wire/src/ethstream.rs +++ b/crates/net/eth-wire/src/ethstream.rs @@ -354,7 +354,8 @@ mod tests { use futures::{SinkExt, StreamExt}; use reth_discv4::DEFAULT_DISCOVERY_PORT; use reth_ecies::stream::ECIESStream; - use reth_primitives::{pk2id, ForkFilter, Head, NamedChain, B256, U256}; + use reth_network_types::pk2id; + use reth_primitives::{ForkFilter, Head, NamedChain, B256, U256}; use secp256k1::{SecretKey, SECP256K1}; use std::time::Duration; use tokio::net::{TcpListener, TcpStream}; diff --git a/crates/net/eth-wire/src/hello.rs b/crates/net/eth-wire/src/hello.rs index 6ca8d9d99..f953c4aae 100644 --- a/crates/net/eth-wire/src/hello.rs +++ b/crates/net/eth-wire/src/hello.rs @@ -2,7 +2,8 @@ use crate::{capability::Capability, EthVersion, ProtocolVersion}; use alloy_rlp::{RlpDecodable, RlpEncodable}; use reth_codecs::derive_arbitrary; use reth_discv4::DEFAULT_DISCOVERY_PORT; -use reth_primitives::{constants::RETH_CLIENT_VERSION, PeerId}; +use reth_network_types::PeerId; +use reth_primitives::constants::RETH_CLIENT_VERSION; use crate::protocol::Protocol; #[cfg(feature = "serde")] @@ -38,7 +39,7 @@ impl HelloMessageWithProtocols { /// /// ``` /// use reth_eth_wire::HelloMessageWithProtocols; - /// use reth_primitives::pk2id; + /// use reth_network_types::pk2id; /// use secp256k1::{SecretKey, SECP256K1}; /// let secret_key = SecretKey::new(&mut rand::thread_rng()); /// let id = pk2id(&secret_key.public_key(SECP256K1)); @@ -120,7 +121,7 @@ impl HelloMessage { /// /// ``` /// use reth_eth_wire::HelloMessage; - /// use reth_primitives::pk2id; + /// use reth_network_types::pk2id; /// use secp256k1::{SecretKey, SECP256K1}; /// let secret_key = SecretKey::new(&mut rand::thread_rng()); /// let id = pk2id(&secret_key.public_key(SECP256K1)); @@ -209,7 +210,7 @@ impl HelloMessageBuilder { mod tests { use alloy_rlp::{Decodable, Encodable, EMPTY_STRING_CODE}; use reth_discv4::DEFAULT_DISCOVERY_PORT; - use reth_primitives::pk2id; + use reth_network_types::pk2id; use secp256k1::{SecretKey, SECP256K1}; use crate::{ diff --git a/crates/net/eth-wire/src/muxdemux.rs b/crates/net/eth-wire/src/muxdemux.rs index 3aa7bc1dd..a9bbe2fdb 100644 --- a/crates/net/eth-wire/src/muxdemux.rs +++ b/crates/net/eth-wire/src/muxdemux.rs @@ -357,9 +357,10 @@ mod tests { UnauthedEthStream, UnauthedP2PStream, }; use futures::{Future, SinkExt, StreamExt}; + use reth_network_types::pk2id; use reth_primitives::{ bytes::{BufMut, Bytes, BytesMut}, - pk2id, ForkFilter, Hardfork, MAINNET, + ForkFilter, Hardfork, MAINNET, }; use secp256k1::{SecretKey, SECP256K1}; use std::{net::SocketAddr, pin::Pin}; diff --git a/crates/net/eth-wire/src/test_utils.rs b/crates/net/eth-wire/src/test_utils.rs index 1708e1ffa..0783e4dad 100644 --- a/crates/net/eth-wire/src/test_utils.rs +++ b/crates/net/eth-wire/src/test_utils.rs @@ -4,7 +4,8 @@ use crate::{ EthVersion, HelloMessageWithProtocols, P2PStream, ProtocolVersion, Status, UnauthedP2PStream, }; use reth_discv4::DEFAULT_DISCOVERY_PORT; -use reth_primitives::{pk2id, Chain, ForkFilter, Head, B256, U256}; +use reth_network_types::pk2id; +use reth_primitives::{Chain, ForkFilter, Head, B256, U256}; use secp256k1::{SecretKey, SECP256K1}; use std::net::SocketAddr; use tokio::net::TcpStream; diff --git a/crates/net/network-api/Cargo.toml b/crates/net/network-api/Cargo.toml index dcf4089cd..81536aad9 100644 --- a/crates/net/network-api/Cargo.toml +++ b/crates/net/network-api/Cargo.toml @@ -17,6 +17,7 @@ reth-primitives.workspace = true reth-eth-wire.workspace = true reth-rpc-types.workspace = true reth-discv4.workspace = true +reth-network-types.workspace = true # eth enr = { workspace = true, default-features = false, features = ["rust-secp256k1"] } diff --git a/crates/net/network-api/src/lib.rs b/crates/net/network-api/src/lib.rs index 0c43273cd..6c3040bd9 100644 --- a/crates/net/network-api/src/lib.rs +++ b/crates/net/network-api/src/lib.rs @@ -14,7 +14,8 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] use reth_eth_wire::{DisconnectReason, EthVersion, Status}; -use reth_primitives::{NodeRecord, PeerId}; +use reth_network_types::PeerId; +use reth_primitives::NodeRecord; use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant}; pub use error::NetworkError; diff --git a/crates/net/network-api/src/noop.rs b/crates/net/network-api/src/noop.rs index b6a0fa846..2ace603e3 100644 --- a/crates/net/network-api/src/noop.rs +++ b/crates/net/network-api/src/noop.rs @@ -10,7 +10,8 @@ use crate::{ use enr::{secp256k1::SecretKey, Enr}; use reth_discv4::DEFAULT_DISCOVERY_PORT; use reth_eth_wire::{DisconnectReason, ProtocolVersion}; -use reth_primitives::{Chain, NodeRecord, PeerId}; +use reth_network_types::PeerId; +use reth_primitives::{Chain, NodeRecord}; use reth_rpc_types::{admin::EthProtocolInfo, NetworkStatus}; use std::net::{IpAddr, SocketAddr}; diff --git a/crates/net/network/Cargo.toml b/crates/net/network/Cargo.toml index c06ff1518..aa6da6ea2 100644 --- a/crates/net/network/Cargo.toml +++ b/crates/net/network/Cargo.toml @@ -28,6 +28,7 @@ reth-provider.workspace = true reth-rpc-types.workspace = true reth-tokio-util.workspace = true reth-consensus.workspace = true +reth-network-types.workspace = true # ethereum enr = { workspace = true, features = ["serde", "rust-secp256k1"] } diff --git a/crates/net/network/src/config.rs b/crates/net/network/src/config.rs index 3e89a1f3a..463bde78d 100644 --- a/crates/net/network/src/config.rs +++ b/crates/net/network/src/config.rs @@ -12,9 +12,9 @@ use reth_discv4::{Discv4Config, Discv4ConfigBuilder, DEFAULT_DISCOVERY_ADDRESS}; use reth_discv5::network_key; use reth_dns_discovery::DnsDiscoveryConfig; use reth_eth_wire::{HelloMessage, HelloMessageWithProtocols, Status}; +use reth_network_types::{pk2id, PeerId}; use reth_primitives::{ - mainnet_nodes, pk2id, sepolia_nodes, ChainSpec, ForkFilter, Head, NamedChain, NodeRecord, - PeerId, MAINNET, + mainnet_nodes, sepolia_nodes, ChainSpec, ForkFilter, Head, NamedChain, NodeRecord, MAINNET, }; use reth_provider::{BlockReader, HeaderProvider}; use reth_tasks::{TaskSpawner, TokioTaskExecutor}; diff --git a/crates/net/network/src/discovery.rs b/crates/net/network/src/discovery.rs index d95f2f957..67d659454 100644 --- a/crates/net/network/src/discovery.rs +++ b/crates/net/network/src/discovery.rs @@ -12,7 +12,8 @@ use reth_discv5::{DiscoveredPeer, Discv5}; use reth_dns_discovery::{ DnsDiscoveryConfig, DnsDiscoveryHandle, DnsDiscoveryService, DnsNodeRecordUpdate, DnsResolver, }; -use reth_primitives::{EnrForkIdEntry, ForkId, NodeRecord, PeerId}; +use reth_network_types::PeerId; +use reth_primitives::{EnrForkIdEntry, ForkId, NodeRecord}; use secp256k1::SecretKey; use std::{ collections::VecDeque, diff --git a/crates/net/network/src/eth_requests.rs b/crates/net/network/src/eth_requests.rs index baa636b93..57e83391d 100644 --- a/crates/net/network/src/eth_requests.rs +++ b/crates/net/network/src/eth_requests.rs @@ -11,7 +11,8 @@ use reth_eth_wire::{ Receipts, }; use reth_interfaces::p2p::error::RequestResult; -use reth_primitives::{BlockBody, BlockHashOrNumber, Header, HeadersDirection, PeerId}; +use reth_network_types::PeerId; +use reth_primitives::{BlockBody, BlockHashOrNumber, Header, HeadersDirection}; use reth_provider::{BlockReader, HeaderProvider, ReceiptProvider}; use std::{ future::Future, diff --git a/crates/net/network/src/fetch/client.rs b/crates/net/network/src/fetch/client.rs index eab474506..63e22abe0 100644 --- a/crates/net/network/src/fetch/client.rs +++ b/crates/net/network/src/fetch/client.rs @@ -11,7 +11,8 @@ use reth_interfaces::p2p::{ priority::Priority, }; use reth_network_api::ReputationChangeKind; -use reth_primitives::{Header, PeerId, B256}; +use reth_network_types::PeerId; +use reth_primitives::{Header, B256}; use std::sync::{ atomic::{AtomicUsize, Ordering}, Arc, diff --git a/crates/net/network/src/fetch/mod.rs b/crates/net/network/src/fetch/mod.rs index 1f85f242d..3a529c97e 100644 --- a/crates/net/network/src/fetch/mod.rs +++ b/crates/net/network/src/fetch/mod.rs @@ -9,7 +9,8 @@ use reth_interfaces::p2p::{ priority::Priority, }; use reth_network_api::ReputationChangeKind; -use reth_primitives::{BlockBody, Header, PeerId, B256}; +use reth_network_types::PeerId; +use reth_primitives::{BlockBody, Header, B256}; use std::{ collections::{HashMap, VecDeque}, sync::{ diff --git a/crates/net/network/src/import.rs b/crates/net/network/src/import.rs index 738851f0a..2d18da9d4 100644 --- a/crates/net/network/src/import.rs +++ b/crates/net/network/src/import.rs @@ -1,7 +1,7 @@ //! This module provides an abstraction over block import in the form of the `BlockImport` trait. use crate::message::NewBlockMessage; -use reth_primitives::PeerId; +use reth_network_types::PeerId; use std::task::{Context, Poll}; /// Abstraction over block import. diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index 5783c4ebd..39d29ee71 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -44,7 +44,8 @@ use reth_eth_wire::{ use reth_metrics::common::mpsc::UnboundedMeteredSender; use reth_net_common::bandwidth_meter::BandwidthMeter; use reth_network_api::ReputationChangeKind; -use reth_primitives::{ForkId, NodeRecord, PeerId}; +use reth_network_types::PeerId; +use reth_primitives::{ForkId, NodeRecord}; use reth_provider::{BlockNumReader, BlockReader}; use reth_rpc_types::{admin::EthProtocolInfo, NetworkStatus}; use reth_tasks::shutdown::GracefulShutdown; diff --git a/crates/net/network/src/message.rs b/crates/net/network/src/message.rs index b6861267a..2086fd60e 100644 --- a/crates/net/network/src/message.rs +++ b/crates/net/network/src/message.rs @@ -11,8 +11,9 @@ use reth_eth_wire::{ SharedTransactions, Transactions, }; use reth_interfaces::p2p::error::{RequestError, RequestResult}; +use reth_network_types::PeerId; use reth_primitives::{ - BlockBody, Bytes, Header, PeerId, PooledTransactionsElement, ReceiptWithBloom, B256, + BlockBody, Bytes, Header, PooledTransactionsElement, ReceiptWithBloom, B256, }; use std::{ fmt, diff --git a/crates/net/network/src/network.rs b/crates/net/network/src/network.rs index 7104e442e..86669bf19 100644 --- a/crates/net/network/src/network.rs +++ b/crates/net/network/src/network.rs @@ -13,7 +13,8 @@ use reth_network_api::{ NetworkError, NetworkInfo, PeerInfo, PeerKind, Peers, PeersInfo, Reputation, ReputationChangeKind, }; -use reth_primitives::{Head, NodeRecord, PeerId, TransactionSigned, B256}; +use reth_network_types::PeerId; +use reth_primitives::{Head, NodeRecord, TransactionSigned, B256}; use reth_rpc_types::NetworkStatus; use secp256k1::SecretKey; use std::{ diff --git a/crates/net/network/src/peers/manager.rs b/crates/net/network/src/peers/manager.rs index b94c22db7..d6ae9c4da 100644 --- a/crates/net/network/src/peers/manager.rs +++ b/crates/net/network/src/peers/manager.rs @@ -14,7 +14,8 @@ use futures::StreamExt; use reth_eth_wire::{errors::EthStreamError, DisconnectReason}; use reth_net_common::ban_list::BanList; use reth_network_api::{PeerKind, ReputationChangeKind}; -use reth_primitives::{ForkId, NodeRecord, PeerId}; +use reth_network_types::PeerId; +use reth_primitives::{ForkId, NodeRecord}; use std::{ collections::{hash_map::Entry, HashMap, HashSet, VecDeque}, fmt::Display, @@ -1558,7 +1559,8 @@ mod tests { }; use reth_net_common::ban_list::BanList; use reth_network_api::{Direction, ReputationChangeKind}; - use reth_primitives::{PeerId, B512}; + use reth_network_types::PeerId; + use reth_primitives::B512; use std::{ collections::HashSet, future::{poll_fn, Future}, diff --git a/crates/net/network/src/session/active.rs b/crates/net/network/src/session/active.rs index 33c0a66e3..32bfb72ac 100644 --- a/crates/net/network/src/session/active.rs +++ b/crates/net/network/src/session/active.rs @@ -20,7 +20,7 @@ use reth_eth_wire::{ }; use reth_interfaces::p2p::error::RequestError; use reth_metrics::common::mpsc::MeteredPollSender; -use reth_primitives::PeerId; +use reth_network_types::PeerId; use std::{ collections::VecDeque, future::Future, @@ -769,7 +769,8 @@ mod tests { UnauthedEthStream, UnauthedP2PStream, }; use reth_net_common::bandwidth_meter::{BandwidthMeter, MeteredStream}; - use reth_primitives::{pk2id, ForkFilter, Hardfork, MAINNET}; + use reth_network_types::pk2id; + use reth_primitives::{ForkFilter, Hardfork, MAINNET}; use secp256k1::{SecretKey, SECP256K1}; use tokio::{ net::{TcpListener, TcpStream}, diff --git a/crates/net/network/src/session/handle.rs b/crates/net/network/src/session/handle.rs index 80298f324..c48fff618 100644 --- a/crates/net/network/src/session/handle.rs +++ b/crates/net/network/src/session/handle.rs @@ -12,7 +12,7 @@ use reth_eth_wire::{ DisconnectReason, EthVersion, Status, }; use reth_network_api::PeerInfo; -use reth_primitives::PeerId; +use reth_network_types::PeerId; use std::{io, net::SocketAddr, sync::Arc, time::Instant}; use tokio::sync::{ mpsc::{self, error::SendError}, diff --git a/crates/net/network/src/session/mod.rs b/crates/net/network/src/session/mod.rs index 94d41226e..95f426c54 100644 --- a/crates/net/network/src/session/mod.rs +++ b/crates/net/network/src/session/mod.rs @@ -19,7 +19,8 @@ use reth_net_common::{ bandwidth_meter::{BandwidthMeter, MeteredStream}, stream::HasRemoteAddr, }; -use reth_primitives::{ForkFilter, ForkId, ForkTransition, Head, PeerId}; +use reth_network_types::PeerId; +use reth_primitives::{ForkFilter, ForkId, ForkTransition, Head}; use reth_tasks::TaskSpawner; use secp256k1::SecretKey; use std::{ diff --git a/crates/net/network/src/state.rs b/crates/net/network/src/state.rs index d75a1aaa5..0020b4927 100644 --- a/crates/net/network/src/state.rs +++ b/crates/net/network/src/state.rs @@ -18,7 +18,8 @@ use reth_eth_wire::{ capability::Capabilities, BlockHashNumber, DisconnectReason, NewBlockHashes, Status, }; use reth_network_api::PeerKind; -use reth_primitives::{ForkId, PeerId, B256}; +use reth_network_types::PeerId; +use reth_primitives::{ForkId, B256}; use reth_provider::BlockNumReader; use std::{ collections::{HashMap, VecDeque}, @@ -537,7 +538,8 @@ mod tests { BlockBodies, EthVersion, }; use reth_interfaces::p2p::{bodies::client::BodiesClient, error::RequestError}; - use reth_primitives::{BlockBody, Header, PeerId, B256}; + use reth_network_types::PeerId; + use reth_primitives::{BlockBody, Header, B256}; use reth_provider::test_utils::NoopProvider; use std::{ future::poll_fn, diff --git a/crates/net/network/src/swarm.rs b/crates/net/network/src/swarm.rs index 136ece0bd..11ac5949a 100644 --- a/crates/net/network/src/swarm.rs +++ b/crates/net/network/src/swarm.rs @@ -12,7 +12,7 @@ use reth_eth_wire::{ errors::EthStreamError, EthVersion, Status, }; -use reth_primitives::PeerId; +use reth_network_types::PeerId; use reth_provider::{BlockNumReader, BlockReader}; use std::{ io, diff --git a/crates/net/network/src/test_utils/init.rs b/crates/net/network/src/test_utils/init.rs index 1419191aa..b72046a7f 100644 --- a/crates/net/network/src/test_utils/init.rs +++ b/crates/net/network/src/test_utils/init.rs @@ -1,5 +1,5 @@ use enr::{k256::ecdsa::SigningKey, Enr, EnrPublicKey}; -use reth_primitives::PeerId; +use reth_network_types::PeerId; use std::{net::SocketAddr, time::Duration}; /// The timeout for tests that create a GethInstance diff --git a/crates/net/network/src/test_utils/testnet.rs b/crates/net/network/src/test_utils/testnet.rs index 9720b7a93..a92934c0c 100644 --- a/crates/net/network/src/test_utils/testnet.rs +++ b/crates/net/network/src/test_utils/testnet.rs @@ -14,7 +14,8 @@ use futures::{FutureExt, StreamExt}; use pin_project::pin_project; use reth_eth_wire::{protocol::Protocol, DisconnectReason, HelloMessageWithProtocols}; use reth_network_api::{NetworkInfo, Peers}; -use reth_primitives::{PeerId, MAINNET}; +use reth_network_types::PeerId; +use reth_primitives::MAINNET; use reth_provider::{ test_utils::NoopProvider, BlockReader, BlockReaderIdExt, HeaderProvider, StateProviderFactory, }; diff --git a/crates/net/network/src/transactions/fetcher.rs b/crates/net/network/src/transactions/fetcher.rs index cbec0f1e6..e82a20a31 100644 --- a/crates/net/network/src/transactions/fetcher.rs +++ b/crates/net/network/src/transactions/fetcher.rs @@ -41,7 +41,8 @@ use reth_eth_wire::{ PartiallyValidData, RequestTxHashes, ValidAnnouncementData, }; use reth_interfaces::p2p::error::{RequestError, RequestResult}; -use reth_primitives::{PeerId, PooledTransactionsElement, TxHash}; +use reth_network_types::PeerId; +use reth_primitives::{PooledTransactionsElement, TxHash}; use schnellru::ByLength; #[cfg(debug_assertions)] use smallvec::{smallvec, SmallVec}; diff --git a/crates/net/network/src/transactions/mod.rs b/crates/net/network/src/transactions/mod.rs index 83176c566..f7d03520f 100644 --- a/crates/net/network/src/transactions/mod.rs +++ b/crates/net/network/src/transactions/mod.rs @@ -26,9 +26,9 @@ use reth_interfaces::{ }; use reth_metrics::common::mpsc::UnboundedMeteredReceiver; use reth_network_api::{Peers, ReputationChangeKind}; +use reth_network_types::PeerId; use reth_primitives::{ - FromRecoveredPooledTransaction, PeerId, PooledTransactionsElement, TransactionSigned, TxHash, - B256, + FromRecoveredPooledTransaction, PooledTransactionsElement, TransactionSigned, TxHash, B256, }; use reth_transaction_pool::{ error::{PoolError, PoolResult}, diff --git a/crates/net/types/Cargo.toml b/crates/net/types/Cargo.toml new file mode 100644 index 000000000..841a76dfe --- /dev/null +++ b/crates/net/types/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "reth-network-types" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true +description = "Network types and utils" + +[lints] +workspace = true + +[dependencies] +# reth +reth-rpc-types.workspace = true +reth-primitives.workspace = true + +# eth +enr.workspace = true + +# crypto +secp256k1 = { workspace = true, features = ["global-context", "recovery", "rand"] } + +# misc +serde_with.workspace = true + diff --git a/crates/net/types/src/lib.rs b/crates/net/types/src/lib.rs new file mode 100644 index 000000000..ccd9757c9 --- /dev/null +++ b/crates/net/types/src/lib.rs @@ -0,0 +1,239 @@ +//! Network Types and Utilities. +//! +//! This crate manages and converts Ethereum network entities such as node records, peer IDs, and +//! Ethereum Node Records (ENRs) + +#![doc( + html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png", + html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256", + issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/" +)] +#![cfg_attr(not(test), warn(unused_crate_dependencies))] +#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] + +use secp256k1::{constants::UNCOMPRESSED_PUBLIC_KEY_SIZE, PublicKey, SecretKey}; +use std::{net::IpAddr, str::FromStr}; + +// Re-export PeerId for ease of use. +pub use enr::Enr; +pub use reth_rpc_types::{NodeRecord, PeerId}; + +/// This tag should be set to indicate to libsecp256k1 that the following bytes denote an +/// uncompressed pubkey. +/// +/// `SECP256K1_TAG_PUBKEY_UNCOMPRESSED` = `0x04` +/// +/// See: +const SECP256K1_TAG_PUBKEY_UNCOMPRESSED: u8 = 4; + +/// Converts a [secp256k1::PublicKey] to a [PeerId] by stripping the +/// `SECP256K1_TAG_PUBKEY_UNCOMPRESSED` tag and storing the rest of the slice in the [PeerId]. +#[inline] +pub fn pk2id(pk: &PublicKey) -> PeerId { + PeerId::from_slice(&pk.serialize_uncompressed()[1..]) +} + +/// Converts a [PeerId] to a [secp256k1::PublicKey] by prepending the [PeerId] bytes with the +/// SECP256K1_TAG_PUBKEY_UNCOMPRESSED tag. +#[inline] +pub fn id2pk(id: PeerId) -> Result { + // NOTE: B512 is used as a PeerId because 512 bits is enough to represent an uncompressed + // public key. + let mut s = [0u8; UNCOMPRESSED_PUBLIC_KEY_SIZE]; + s[0] = SECP256K1_TAG_PUBKEY_UNCOMPRESSED; + s[1..].copy_from_slice(id.as_slice()); + PublicKey::from_slice(&s) +} + +/// A peer that can come in ENR or [NodeRecord] form. +#[derive( + Debug, Clone, Eq, PartialEq, Hash, serde_with::SerializeDisplay, serde_with::DeserializeFromStr, +)] +pub enum AnyNode { + /// An "enode:" peer with full ip + NodeRecord(NodeRecord), + /// An "enr:" + Enr(Enr), + /// An incomplete "enode" with only a peer id + PeerId(PeerId), +} + +impl AnyNode { + /// Returns the peer id of the node. + pub fn peer_id(&self) -> PeerId { + match self { + AnyNode::NodeRecord(record) => record.id, + AnyNode::Enr(enr) => pk2id(&enr.public_key()), + AnyNode::PeerId(peer_id) => *peer_id, + } + } + + /// Returns the full node record if available. + pub fn node_record(&self) -> Option { + match self { + AnyNode::NodeRecord(record) => Some(*record), + AnyNode::Enr(enr) => { + let node_record = NodeRecord { + address: enr.ip4().map(IpAddr::from).or_else(|| enr.ip6().map(IpAddr::from))?, + tcp_port: enr.tcp4().or_else(|| enr.tcp6())?, + udp_port: enr.udp4().or_else(|| enr.udp6())?, + id: pk2id(&enr.public_key()), + } + .into_ipv4_mapped(); + Some(node_record) + } + _ => None, + } + } +} + +impl From for AnyNode { + fn from(value: NodeRecord) -> Self { + Self::NodeRecord(value) + } +} + +impl From> for AnyNode { + fn from(value: Enr) -> Self { + Self::Enr(value) + } +} + +impl FromStr for AnyNode { + type Err = String; + + fn from_str(s: &str) -> Result { + if let Some(rem) = s.strip_prefix("enode://") { + if let Ok(record) = NodeRecord::from_str(s) { + return Ok(AnyNode::NodeRecord(record)) + } + // incomplete enode + if let Ok(peer_id) = PeerId::from_str(rem) { + return Ok(AnyNode::PeerId(peer_id)) + } + return Err(format!("invalid public key: {rem}")) + } + if s.starts_with("enr:") { + return Enr::from_str(s).map(AnyNode::Enr) + } + Err("missing 'enr:' prefix for base64-encoded record".to_string()) + } +} + +impl std::fmt::Display for AnyNode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AnyNode::NodeRecord(record) => write!(f, "{record}"), + AnyNode::Enr(enr) => write!(f, "{enr}"), + AnyNode::PeerId(peer_id) => { + write!(f, "enode://{}", reth_primitives::hex::encode(peer_id.as_slice())) + } + } + } +} + +/// Generic wrapper with peer id +#[derive(Debug)] +pub struct WithPeerId(PeerId, pub T); + +impl From<(PeerId, T)> for WithPeerId { + fn from(value: (PeerId, T)) -> Self { + Self(value.0, value.1) + } +} + +impl WithPeerId { + /// Wraps the value with the peerid. + pub fn new(peer: PeerId, value: T) -> Self { + Self(peer, value) + } + + /// Get the peer id + pub fn peer_id(&self) -> PeerId { + self.0 + } + + /// Get the underlying data + pub fn data(&self) -> &T { + &self.1 + } + + /// Returns ownership of the underlying data. + pub fn into_data(self) -> T { + self.1 + } + + /// Transform the data + pub fn transform>(self) -> WithPeerId { + WithPeerId(self.0, self.1.into()) + } + + /// Split the wrapper into [PeerId] and data tuple + pub fn split(self) -> (PeerId, T) { + (self.0, self.1) + } + + /// Maps the inner value to a new value using the given function. + pub fn map U>(self, op: F) -> WithPeerId { + WithPeerId(self.0, op(self.1)) + } +} + +impl WithPeerId> { + /// returns `None` if the inner value is `None`, otherwise returns `Some(WithPeerId)`. + pub fn transpose(self) -> Option> { + self.1.map(|v| WithPeerId(self.0, v)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use secp256k1::SECP256K1; + + #[test] + fn test_node_record_parse() { + let url = "enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@10.3.58.6:30303?discport=30301"; + let node: AnyNode = url.parse().unwrap(); + assert_eq!(node, AnyNode::NodeRecord(NodeRecord { + address: IpAddr::V4([10,3,58,6].into()), + tcp_port: 30303, + udp_port: 30301, + id: "6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0".parse().unwrap(), + })); + assert_eq!(node.to_string(), url) + } + + #[test] + fn test_peer_id_parse() { + let url = "enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"; + let node: AnyNode = url.parse().unwrap(); + assert_eq!(node, AnyNode::PeerId("6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0".parse().unwrap())); + assert_eq!(node.to_string(), url); + + let url = "enode://"; + let err = url.parse::().unwrap_err(); + assert_eq!(err, "invalid public key: "); + } + + // + #[test] + fn test_enr_parse() { + let url = "enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8"; + let node: AnyNode = url.parse().unwrap(); + assert_eq!( + node.peer_id(), + "0xca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f" + .parse::() + .unwrap() + ); + assert_eq!(node.to_string(), url); + } + + #[test] + fn pk2id2pk() { + let prikey = SecretKey::new(&mut secp256k1::rand::thread_rng()); + let pubkey = PublicKey::from_secret_key(SECP256K1, &prikey); + assert_eq!(pubkey, id2pk(pk2id(&pubkey)).unwrap()); + } +} diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index ab6b44303..4fa504665 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -27,7 +27,6 @@ alloy-trie = { workspace = true, features = ["serde"] } nybbles = { workspace = true, features = ["serde", "rlp"] } alloy-genesis.workspace = true alloy-eips.workspace = true -enr.workspace = true # crypto secp256k1 = { workspace = true, features = ["global-context", "recovery", "rand"] } @@ -43,7 +42,6 @@ itertools.workspace = true modular-bitfield.workspace = true once_cell.workspace = true rayon.workspace = true -serde_with.workspace = true serde.workspace = true serde_json.workspace = true sha2 = { version = "0.10.7", optional = true } diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 9e77b4c15..1c8808628 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -33,7 +33,6 @@ mod header; mod integer_list; mod log; mod net; -mod peer; pub mod proofs; mod prune; mod receipt; @@ -77,7 +76,6 @@ pub use net::{ NodeRecordParseError, GOERLI_BOOTNODES, HOLESKY_BOOTNODES, MAINNET_BOOTNODES, SEPOLIA_BOOTNODES, }; -pub use peer::{id2pk, pk2id, AnyNode, PeerId, WithPeerId}; pub use prune::{ PruneCheckpoint, PruneInterruptReason, PruneLimiter, PruneMode, PruneModes, PruneProgress, PrunePurpose, PruneSegment, PruneSegmentError, ReceiptsLogPruneConfig, diff --git a/crates/rpc/rpc-api/Cargo.toml b/crates/rpc/rpc-api/Cargo.toml index c2ada1e88..fe22eae0f 100644 --- a/crates/rpc/rpc-api/Cargo.toml +++ b/crates/rpc/rpc-api/Cargo.toml @@ -16,6 +16,7 @@ workspace = true reth-primitives.workspace = true reth-rpc-types.workspace = true reth-engine-primitives.workspace = true +reth-network-types.workspace = true # misc jsonrpsee = { workspace = true, features = ["server", "macros"] } diff --git a/crates/rpc/rpc-api/src/admin.rs b/crates/rpc/rpc-api/src/admin.rs index 7497d1205..4c31221cd 100644 --- a/crates/rpc/rpc-api/src/admin.rs +++ b/crates/rpc/rpc-api/src/admin.rs @@ -1,5 +1,6 @@ use jsonrpsee::{core::RpcResult, proc_macros::rpc}; -use reth_primitives::{AnyNode, NodeRecord}; +use reth_network_types::AnyNode; +use reth_primitives::NodeRecord; use reth_rpc_types::{admin::NodeInfo, PeerInfo}; /// Admin namespace rpc interface that gives access to several non-standard RPC methods. diff --git a/crates/rpc/rpc/Cargo.toml b/crates/rpc/rpc/Cargo.toml index d5bd324ae..81788f0a3 100644 --- a/crates/rpc/rpc/Cargo.toml +++ b/crates/rpc/rpc/Cargo.toml @@ -27,6 +27,7 @@ reth-consensus-common.workspace = true reth-rpc-types-compat.workspace = true revm-inspectors.workspace = true reth-evm.workspace = true +reth-network-types.workspace = true # eth alloy-rlp.workspace = true diff --git a/crates/rpc/rpc/src/admin.rs b/crates/rpc/rpc/src/admin.rs index 3f5578433..6f3125e06 100644 --- a/crates/rpc/rpc/src/admin.rs +++ b/crates/rpc/rpc/src/admin.rs @@ -3,7 +3,8 @@ use alloy_primitives::B256; use async_trait::async_trait; use jsonrpsee::core::RpcResult; use reth_network_api::{NetworkInfo, PeerKind, Peers}; -use reth_primitives::{AnyNode, ChainSpec, NodeRecord}; +use reth_network_types::AnyNode; +use reth_primitives::{ChainSpec, NodeRecord}; use reth_rpc_api::AdminApiServer; use reth_rpc_types::{ admin::{EthProtocolInfo, NodeInfo, Ports, ProtocolInfo}, diff --git a/crates/stages/Cargo.toml b/crates/stages/Cargo.toml index 2692c9410..f3bd16a5e 100644 --- a/crates/stages/Cargo.toml +++ b/crates/stages/Cargo.toml @@ -51,6 +51,7 @@ reth-revm.workspace = true reth-static-file.workspace = true reth-trie = { workspace = true, features = ["test-utils"] } reth-provider = { workspace = true, features = ["test-utils"] } +reth-network-types.workspace = true alloy-rlp.workspace = true itertools.workspace = true diff --git a/crates/stages/src/lib.rs b/crates/stages/src/lib.rs index 4b6df9391..3fea3e04d 100644 --- a/crates/stages/src/lib.rs +++ b/crates/stages/src/lib.rs @@ -17,7 +17,8 @@ //! # use reth_downloaders::headers::reverse_headers::ReverseHeadersDownloaderBuilder; //! # use reth_interfaces::test_utils::{TestBodiesClient, TestHeadersClient}; //! # use reth_revm::EvmProcessorFactory; -//! # use reth_primitives::{PeerId, MAINNET, B256, PruneModes}; +//! # use reth_primitives::{MAINNET, B256, PruneModes}; +//! # use reth_network_types::PeerId; //! # use reth_stages::Pipeline; //! # use reth_stages::sets::DefaultStages; //! # use tokio::sync::watch; diff --git a/crates/transaction-pool/Cargo.toml b/crates/transaction-pool/Cargo.toml index 5b6b85486..4365245cf 100644 --- a/crates/transaction-pool/Cargo.toml +++ b/crates/transaction-pool/Cargo.toml @@ -20,6 +20,7 @@ reth-tasks.workspace = true revm.workspace = true alloy-rlp.workspace = true reth-revm = { workspace = true, optional = true } +reth-network-types.workspace = true # async/futures futures-util.workspace = true diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index c5603ec7b..79b9af698 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -9,13 +9,13 @@ use crate::{ }; use futures_util::{ready, Stream}; use reth_eth_wire::HandleMempoolData; +use reth_network_types::PeerId; use reth_primitives::{ kzg::KzgSettings, transaction::TryFromRecoveredTransactionError, AccessList, Address, BlobTransactionSidecar, BlobTransactionValidationError, FromRecoveredPooledTransaction, - IntoRecoveredTransaction, PeerId, PooledTransactionsElement, - PooledTransactionsElementEcRecovered, SealedBlock, Transaction, TransactionSignedEcRecovered, - TryFromRecoveredTransaction, TxEip4844, TxHash, TxKind, B256, EIP1559_TX_TYPE_ID, - EIP4844_TX_TYPE_ID, U256, + IntoRecoveredTransaction, PooledTransactionsElement, PooledTransactionsElementEcRecovered, + SealedBlock, Transaction, TransactionSignedEcRecovered, TryFromRecoveredTransaction, TxEip4844, + TxHash, TxKind, B256, EIP1559_TX_TYPE_ID, EIP4844_TX_TYPE_ID, U256, }; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; diff --git a/examples/manual-p2p/Cargo.toml b/examples/manual-p2p/Cargo.toml index a9c7f2513..139cb0e18 100644 --- a/examples/manual-p2p/Cargo.toml +++ b/examples/manual-p2p/Cargo.toml @@ -6,14 +6,17 @@ edition.workspace = true license.workspace = true [dependencies] -once_cell.workspace = true -eyre.workspace = true - reth-primitives.workspace = true reth-network.workspace = true reth-discv4.workspace = true reth-eth-wire.workspace = true reth-ecies.workspace = true -futures.workspace = true +reth-network-types.workspace = true + secp256k1 = { workspace = true, features = ["global-context", "rand-std", "recovery"] } + +futures.workspace = true tokio.workspace = true + +eyre.workspace = true +once_cell.workspace = true diff --git a/examples/manual-p2p/src/main.rs b/examples/manual-p2p/src/main.rs index 737daf728..e97cb3662 100644 --- a/examples/manual-p2p/src/main.rs +++ b/examples/manual-p2p/src/main.rs @@ -16,8 +16,9 @@ use reth_eth_wire::{ EthMessage, EthStream, HelloMessage, P2PStream, Status, UnauthedEthStream, UnauthedP2PStream, }; use reth_network::config::rng_secret_key; +use reth_network_types::pk2id; use reth_primitives::{ - mainnet_nodes, pk2id, Chain, Hardfork, Head, NodeRecord, MAINNET, MAINNET_GENESIS_HASH, + mainnet_nodes, Chain, Hardfork, Head, NodeRecord, MAINNET, MAINNET_GENESIS_HASH, }; use secp256k1::{SecretKey, SECP256K1}; use tokio::net::TcpStream;