add alloy_chains (#5952)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Darex
2024-01-12 20:13:55 +05:30
committed by GitHub
parent 3675c632fd
commit 3ce9fcf7e5
30 changed files with 117 additions and 447 deletions

View File

@ -18,6 +18,7 @@ reth-net-common.workspace = true
# ethereum
alloy-rlp.workspace = true
alloy-chains.workspace = true
secp256k1 = { workspace = true, features = ["global-context", "rand-std", "recovery", "serde"] }
enr = { workspace = true, default-features = false, features = ["rust-secp256k1"] }

View File

@ -409,9 +409,10 @@ fn convert_enr_node_record(enr: &Enr<SecretKey>) -> Option<DnsNodeRecordUpdate>
mod tests {
use super::*;
use crate::tree::TreeRootEntry;
use alloy_chains::Chain;
use alloy_rlp::Encodable;
use enr::{EnrBuilder, EnrKey};
use reth_primitives::{Chain, Hardfork, MAINNET};
use reth_primitives::{Hardfork, MAINNET};
use secp256k1::rand::thread_rng;
use std::{future::poll_fn, net::Ipv4Addr};
use tokio_stream::StreamExt;

View File

@ -17,6 +17,7 @@ reth-codecs.workspace = true
reth-primitives.workspace = true
reth-ecies.workspace = true
alloy-rlp = { workspace = true, features = ["derive"] }
alloy-chains = {workspace = true, features = ["serde", "rlp", "arbitrary"] }
reth-discv4.workspace = true
# metrics

View File

@ -1,7 +1,8 @@
//! Builder structs for messages.
use crate::Status;
use reth_primitives::{Chain, ForkId, B256, U256};
use alloy_chains::Chain;
use reth_primitives::{ForkId, B256, U256};
/// Builder for [`Status`] messages.
///

View File

@ -5,7 +5,8 @@ use crate::{
version::ParseVersionError,
DisconnectReason, EthMessageID, EthVersion,
};
use reth_primitives::{Chain, GotExpected, GotExpectedBoxed, ValidationError, B256};
use alloy_chains::Chain;
use reth_primitives::{GotExpected, GotExpectedBoxed, ValidationError, B256};
use std::io;
/// Errors when sending/receiving messages

View File

@ -335,10 +335,11 @@ mod tests {
types::{broadcast::BlockHashNumber, EthMessage, EthVersion, Status},
EthStream, HelloMessageWithProtocols, PassthroughCodec,
};
use alloy_chains::NamedChain;
use futures::{SinkExt, StreamExt};
use reth_discv4::DEFAULT_DISCOVERY_PORT;
use reth_ecies::{stream::ECIESStream, util::pk2id};
use reth_primitives::{ForkFilter, Head, NamedChain, B256, U256};
use reth_primitives::{ForkFilter, Head, B256, U256};
use secp256k1::{SecretKey, SECP256K1};
use tokio::net::{TcpListener, TcpStream};
use tokio_util::codec::Decoder;

View File

@ -3,9 +3,10 @@
use crate::{
EthVersion, HelloMessageWithProtocols, P2PStream, ProtocolVersion, Status, UnauthedP2PStream,
};
use alloy_chains::Chain;
use reth_discv4::DEFAULT_DISCOVERY_PORT;
use reth_ecies::util::pk2id;
use reth_primitives::{Chain, ForkFilter, Head, B256, U256};
use reth_primitives::{ForkFilter, Head, B256, U256};
use secp256k1::{SecretKey, SECP256K1};
use std::net::SocketAddr;
use tokio::net::TcpStream;

View File

@ -1,9 +1,8 @@
use crate::{EthVersion, StatusBuilder};
use alloy_chains::{Chain, NamedChain};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use reth_codecs::derive_arbitrary;
use reth_primitives::{
hex, Chain, ChainSpec, ForkId, Genesis, Hardfork, Head, NamedChain, B256, MAINNET, U256,
};
use reth_primitives::{hex, ChainSpec, ForkId, Genesis, Hardfork, Head, B256, MAINNET, U256};
use std::fmt::{Debug, Display};
#[cfg(feature = "serde")]
@ -51,7 +50,7 @@ impl From<Genesis> for Status {
Status {
version: EthVersion::Eth68 as u8,
chain: Chain::Id(chain),
chain: Chain::from_id(chain),
total_difficulty,
blockhash: chainspec.genesis_hash(),
genesis: chainspec.genesis_hash(),
@ -138,7 +137,7 @@ impl Default for Status {
let mainnet_genesis = MAINNET.genesis_hash();
Status {
version: EthVersion::Eth68 as u8,
chain: Chain::Named(NamedChain::Mainnet),
chain: Chain::from_named(NamedChain::Mainnet),
total_difficulty: U256::from(17_179_869_184u64),
blockhash: mainnet_genesis,
genesis: mainnet_genesis,
@ -152,11 +151,11 @@ impl Default for Status {
#[cfg(test)]
mod tests {
use crate::types::{EthVersion, Status};
use alloy_chains::{Chain, NamedChain};
use alloy_rlp::{Decodable, Encodable};
use rand::Rng;
use reth_primitives::{
hex, Chain, ChainSpec, ForkCondition, ForkHash, ForkId, Genesis, Hardfork, Head,
NamedChain, B256, U256,
hex, ChainSpec, ForkCondition, ForkHash, ForkId, Genesis, Hardfork, Head, B256, U256,
};
use std::str::FromStr;
@ -165,7 +164,7 @@ mod tests {
let expected = hex!("f85643018a07aac59dabcdd74bc567a0feb27336ca7923f8fab3bd617fcb6e75841538f71c1bcfc267d7838489d9e13da0d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3c684b715077d80");
let status = Status {
version: EthVersion::Eth67 as u8,
chain: Chain::Named(NamedChain::Mainnet),
chain: Chain::from_named(NamedChain::Mainnet),
total_difficulty: U256::from(36206751599115524359527u128),
blockhash: B256::from_str(
"feb27336ca7923f8fab3bd617fcb6e75841538f71c1bcfc267d7838489d9e13d",
@ -188,7 +187,7 @@ mod tests {
let data = hex!("f85643018a07aac59dabcdd74bc567a0feb27336ca7923f8fab3bd617fcb6e75841538f71c1bcfc267d7838489d9e13da0d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3c684b715077d80");
let expected = Status {
version: EthVersion::Eth67 as u8,
chain: Chain::Named(NamedChain::Mainnet),
chain: Chain::from_named(NamedChain::Mainnet),
total_difficulty: U256::from(36206751599115524359527u128),
blockhash: B256::from_str(
"feb27336ca7923f8fab3bd617fcb6e75841538f71c1bcfc267d7838489d9e13d",
@ -209,7 +208,7 @@ mod tests {
let expected = hex!("f850423884024190faa0f8514c4680ef27700751b08f37645309ce65a449616a3ea966bf39dd935bb27ba00d21840abff46b96c84b2ac9e10e4f5cdaeb5693cb665db62a2f3b02d2d57b5bc6845d43d2fd80");
let status = Status {
version: EthVersion::Eth66 as u8,
chain: Chain::Named(NamedChain::BinanceSmartChain),
chain: Chain::from_named(NamedChain::BinanceSmartChain),
total_difficulty: U256::from(37851386u64),
blockhash: B256::from_str(
"f8514c4680ef27700751b08f37645309ce65a449616a3ea966bf39dd935bb27b",
@ -232,7 +231,7 @@ mod tests {
let data = hex!("f850423884024190faa0f8514c4680ef27700751b08f37645309ce65a449616a3ea966bf39dd935bb27ba00d21840abff46b96c84b2ac9e10e4f5cdaeb5693cb665db62a2f3b02d2d57b5bc6845d43d2fd80");
let expected = Status {
version: EthVersion::Eth66 as u8,
chain: Chain::Named(NamedChain::BinanceSmartChain),
chain: Chain::from_named(NamedChain::BinanceSmartChain),
total_difficulty: U256::from(37851386u64),
blockhash: B256::from_str(
"f8514c4680ef27700751b08f37645309ce65a449616a3ea966bf39dd935bb27b",
@ -253,7 +252,7 @@ mod tests {
let data = hex!("f86142820834936d68fcffffffffffffffffffffffffdeab81b8a0523e8163a6d620a4cc152c547a05f28a03fec91a2a615194cb86df9731372c0ca06499dccdc7c7def3ebb1ce4c6ee27ec6bd02aee570625ca391919faf77ef27bdc6841a67ccd880");
let expected = Status {
version: EthVersion::Eth66 as u8,
chain: Chain::Id(2100),
chain: Chain::from_id(2100),
total_difficulty: U256::from_str(
"0x000000000000000000000000006d68fcffffffffffffffffffffffffdeab81b8",
)
@ -300,7 +299,7 @@ mod tests {
(Hardfork::Shanghai, ForkCondition::Timestamp(13)),
];
let mut chainspec = ChainSpec::builder().genesis(genesis).chain(Chain::Id(1337));
let mut chainspec = ChainSpec::builder().genesis(genesis).chain(Chain::from_id(1337));
for (fork, condition) in &hardforks {
chainspec = chainspec.with_fork(*fork, *condition);
@ -323,7 +322,7 @@ mod tests {
let status = Status::spec_builder(&spec, &head).build();
assert_eq!(status.chain, Chain::Id(1337));
assert_eq!(status.chain, Chain::from_id(1337));
assert_eq!(status.forkid, forkid);
assert_eq!(status.total_difficulty, total_difficulty);
assert_eq!(status.blockhash, head_hash);

View File

@ -17,7 +17,7 @@ reth-primitives.workspace = true
reth-eth-wire.workspace = true
reth-rpc-types.workspace = true
reth-discv4.workspace = true
alloy-chains.workspace = true
# io
serde = { workspace = true, features = ["derive"], optional = true }

View File

@ -7,10 +7,11 @@ use crate::{
NetworkError, NetworkInfo, PeerInfo, PeerKind, Peers, PeersInfo, Reputation,
ReputationChangeKind,
};
use alloy_chains::Chain;
use async_trait::async_trait;
use reth_discv4::DEFAULT_DISCOVERY_PORT;
use reth_eth_wire::{DisconnectReason, ProtocolVersion};
use reth_primitives::{Chain, NodeRecord, PeerId};
use reth_primitives::{NodeRecord, PeerId};
use reth_rpc_types::{EthProtocolInfo, NetworkStatus};
use std::net::{IpAddr, SocketAddr};