Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: NetworkPrimitives #12435

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 19 additions & 39 deletions crates/net/eth-wire-types/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::HeadersDirection;
use alloy_eips::BlockHashOrNumber;
use alloy_primitives::B256;
use alloy_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper};
use reth_codecs_derive::add_arbitrary_tests;
use reth_primitives::{BlockBody, Header};
use reth_codecs_derive::{add_arbitrary_tests, generate_tests};

/// A request for a peer to return block headers starting at the requested block.
/// The peer must return at most [`limit`](#structfield.limit) headers.
Expand Down Expand Up @@ -41,34 +40,16 @@ pub struct GetBlockHeaders {
/// The response to [`GetBlockHeaders`], containing headers if any headers were found.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[add_arbitrary_tests(rlp, 10)]
pub struct BlockHeaders(
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
pub struct BlockHeaders<H = reth_primitives::Header>(
/// The requested headers.
pub Vec<Header>,
pub Vec<H>,
);

#[cfg(any(test, feature = "arbitrary"))]
impl<'a> arbitrary::Arbitrary<'a> for BlockHeaders {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let headers_count: usize = u.int_in_range(0..=10)?;
let mut headers = Vec::with_capacity(headers_count);

for _ in 0..headers_count {
headers.push(reth_primitives::generate_valid_header(
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
))
}

Ok(Self(headers))
}
}
generate_tests!(#[rlp, 10] BlockHeaders<reth_primitives::Header>, EthBlockHeadersTests);

impl From<Vec<Header>> for BlockHeaders {
fn from(headers: Vec<Header>) -> Self {
impl<H> From<Vec<H>> for BlockHeaders<H> {
fn from(headers: Vec<H>) -> Self {
Self(headers)
}
}
Expand All @@ -94,14 +75,15 @@ impl From<Vec<B256>> for GetBlockBodies {
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[add_arbitrary_tests(rlp, 16)]
pub struct BlockBodies(
pub struct BlockBodies<B = reth_primitives::BlockBody>(
/// The requested block bodies, each of which should correspond to a hash in the request.
pub Vec<BlockBody>,
pub Vec<B>,
);

impl From<Vec<BlockBody>> for BlockBodies {
fn from(bodies: Vec<BlockBody>) -> Self {
generate_tests!(#[rlp, 16] BlockBodies<reth_primitives::BlockBody>, EthBlockBodiesTests);

impl<B> From<Vec<B>> for BlockBodies<B> {
fn from(bodies: Vec<B>) -> Self {
Self(bodies)
}
}
Expand All @@ -116,11 +98,9 @@ mod tests {
use alloy_eips::BlockHashOrNumber;
use alloy_primitives::{hex, PrimitiveSignature as Signature, TxKind, U256};
use alloy_rlp::{Decodable, Encodable};
use reth_primitives::{Header, Transaction, TransactionSigned};
use reth_primitives::{BlockBody, Header, Transaction, TransactionSigned};
use std::str::FromStr;

use super::BlockBody;

#[test]
fn decode_hash() {
// this is a valid 32 byte rlp string
Expand Down Expand Up @@ -254,7 +234,7 @@ mod tests {
// [ (f90202) 0x0457 = 1111, [ (f901fc) [ (f901f9) header ] ] ]
let expected = hex!("f90202820457f901fcf901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000");
let mut data = vec![];
RequestPair::<BlockHeaders> {
RequestPair::<BlockHeaders<_>> {
request_id: 1111,
message: BlockHeaders(vec![
Header {
Expand Down Expand Up @@ -289,7 +269,7 @@ mod tests {
#[test]
fn decode_block_header() {
let data = hex!("f90202820457f901fcf901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000");
let expected = RequestPair::<BlockHeaders> {
let expected = RequestPair::<BlockHeaders<_>> {
request_id: 1111,
message: BlockHeaders(vec![
Header {
Expand Down Expand Up @@ -357,7 +337,7 @@ mod tests {
fn encode_block_bodies() {
let expected = hex!("f902dc820457f902d6f902d3f8d2f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afbf901fcf901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000");
let mut data = vec![];
let request = RequestPair::<BlockBodies> {
let request = RequestPair::<BlockBodies<_>> {
request_id: 1111,
message: BlockBodies(vec![
BlockBody {
Expand Down Expand Up @@ -428,7 +408,7 @@ mod tests {
#[test]
fn decode_block_bodies() {
let data = hex!("f902dc820457f902d6f902d3f8d2f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afbf901fcf901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000");
let expected = RequestPair::<BlockBodies> {
let expected = RequestPair::<BlockBodies<_>> {
request_id: 1111,
message: BlockBodies(vec![
BlockBody {
Expand Down Expand Up @@ -504,7 +484,7 @@ mod tests {
let body = BlockBodies::default();
let mut buf = Vec::new();
body.encode(&mut buf);
let decoded = BlockBodies::decode(&mut buf.as_slice()).unwrap();
let decoded = BlockBodies::<BlockBody>::decode(&mut buf.as_slice()).unwrap();
assert_eq!(body, decoded);
}
}
15 changes: 8 additions & 7 deletions crates/net/eth-wire-types/src/broadcast.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Types for broadcasting new data.

use crate::{EthMessage, EthVersion};
use crate::{EthMessage, EthVersion, NetworkPrimitives};
use alloy_rlp::{
Decodable, Encodable, RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper,
};

use alloy_primitives::{Bytes, TxHash, B256, U128};
use derive_more::{Constructor, Deref, DerefMut, From, IntoIterator};
use reth_codecs_derive::add_arbitrary_tests;
use reth_primitives::{Block, PooledTransactionsElement, TransactionSigned};
use reth_codecs_derive::{add_arbitrary_tests, generate_tests};
use reth_primitives::{PooledTransactionsElement, TransactionSigned};

use std::{
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -75,14 +75,15 @@ impl From<NewBlockHashes> for Vec<BlockHashNumber> {
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[add_arbitrary_tests(rlp, 25)]
pub struct NewBlock {
pub struct NewBlock<B = reth_primitives::Block> {
/// A new block.
pub block: Block,
pub block: B,
/// The current total difficulty.
pub td: U128,
}

generate_tests!(#[rlp, 25] NewBlock<reth_primitives::Block>, EthNewBlockTests);

/// This informs peers of transactions that have appeared on the network and are not yet included
/// in a block.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Default)]
Expand Down Expand Up @@ -269,7 +270,7 @@ impl NewPooledTransactionHashes {
}
}

impl From<NewPooledTransactionHashes> for EthMessage {
impl<N: NetworkPrimitives> From<NewPooledTransactionHashes> for EthMessage<N> {
fn from(value: NewPooledTransactionHashes) -> Self {
match value {
NewPooledTransactionHashes::Eth66(msg) => Self::NewPooledTransactionHashes66(msg),
Expand Down
3 changes: 3 additions & 0 deletions crates/net/eth-wire-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ pub use disconnect_reason::*;

pub mod capability;
pub use capability::*;

pub mod primitives;
pub use primitives::*;
Loading
Loading