Skip to content

Commit

Permalink
feat(eth-wire): derive serde traits for eth-wire types (paradigmxyz#348)
Browse files Browse the repository at this point in the history
* wip: start adding serde to stuff

* add serde traits to NewBlockHashes

* derive serde traits for the rest of the types

* derive serde traits for p2p subprotocol messages

* cargo fmt
  • Loading branch information
Rjected authored Dec 7, 2022
1 parent 292f5f2 commit 6c91322
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 35 deletions.
8 changes: 6 additions & 2 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/net/eth-wire/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ readme = "README.md"
bytes = { version = "1.1" }
hex = "0.4"
thiserror = "1"
serde = "1"

# reth
reth-primitives = { path = "../../primitives" }
Expand All @@ -25,7 +26,7 @@ tokio-stream = "0.1.11"
pin-project = "1.0"
tracing = "0.1.37"
snap = "1.0.5"
smol_str = { version = "0.1", default-features = false }
smol_str = { version = "0.1", features = ["serde"] }

[dev-dependencies]
reth-ecies = { path = "../ecies" }
Expand Down
7 changes: 4 additions & 3 deletions crates/net/eth-wire/src/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
use crate::{version::ParseVersionError, EthMessage, EthVersion};
use bytes::{BufMut, Bytes};
use reth_rlp::{Decodable, DecodeError, Encodable, RlpDecodable, RlpEncodable};
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;

/// A Capability message consisting of the message-id and the payload
#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct RawCapabilityMessage {
/// Identifier of the message.
pub id: usize,
Expand All @@ -16,7 +17,7 @@ pub struct RawCapabilityMessage {

/// Various protocol related event types bubbled up from a session that need to be handled by the
/// network.
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub enum CapabilityMessage {
/// Eth sub-protocol message.
Eth(EthMessage),
Expand All @@ -25,7 +26,7 @@ pub enum CapabilityMessage {
}

/// A message indicating a supported capability and capability version.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable)]
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable, Serialize, Deserialize)]
pub struct Capability {
/// The name of the subprotocol
pub name: SmolStr,
Expand Down
3 changes: 2 additions & 1 deletion crates/net/eth-wire/src/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
use bytes::Buf;
use reth_rlp::{Decodable, DecodeError, Encodable, EMPTY_LIST_CODE};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use thiserror::Error;

/// RLPx disconnect reason.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DisconnectReason {
/// Disconnect requested by the local node or remote peer.
DisconnectRequested = 0x00,
Expand Down
7 changes: 4 additions & 3 deletions crates/net/eth-wire/src/p2pstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use futures::{Sink, SinkExt, StreamExt};
use pin_project::pin_project;
use reth_primitives::H512 as PeerId;
use reth_rlp::{Decodable, DecodeError, Encodable, RlpDecodable, RlpEncodable, EMPTY_STRING_CODE};
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeSet, HashMap, VecDeque},
io,
Expand Down Expand Up @@ -523,7 +524,7 @@ pub fn set_capability_offsets(
}

/// This represents only the reserved `p2p` subprotocol messages.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum P2PMessage {
/// The first packet sent over the connection, and sent once by both sides.
Hello(HelloMessage),
Expand Down Expand Up @@ -655,7 +656,7 @@ impl TryFrom<u8> for P2PMessageID {
// TODO: determine if we should allow for the extra fields at the end like EIP-706 suggests
/// Message used in the `p2p` handshake, containing information about the supported RLPx protocol
/// version and capabilities.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable)]
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable, Serialize, Deserialize)]
pub struct HelloMessage {
/// The version of the `p2p` protocol.
pub protocol_version: ProtocolVersion,
Expand All @@ -671,7 +672,7 @@ pub struct HelloMessage {
}

/// RLPx `p2p` protocol version
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProtocolVersion {
/// `p2p` version 4
V4 = 4,
Expand Down
19 changes: 14 additions & 5 deletions crates/net/eth-wire/src/types/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use super::RawBlockBody;
use reth_primitives::{BlockHashOrNumber, Header, HeadersDirection, TransactionSigned, H256};
use reth_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper};
use serde::{Deserialize, Serialize};

/// 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 All @@ -13,7 +14,9 @@ use reth_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrap
///
/// If the [`skip`](#structfield.skip) field is non-zero, the peer must skip that amount of headers
/// in the direction specified by [`reverse`](#structfield.reverse).
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RlpEncodable, RlpDecodable)]
#[derive(
Copy, Clone, Debug, PartialEq, Eq, Hash, RlpEncodable, RlpDecodable, Serialize, Deserialize,
)]
pub struct GetBlockHeaders {
/// The block number or hash that the peer should start returning headers from.
pub start_block: BlockHashOrNumber,
Expand All @@ -32,7 +35,9 @@ pub struct GetBlockHeaders {
}

/// The response to [`GetBlockHeaders`], containing headers if any headers were found.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct BlockHeaders(
/// The requested headers.
pub Vec<Header>,
Expand All @@ -45,7 +50,9 @@ impl From<Vec<Header>> for BlockHeaders {
}

/// A request for a peer to return block bodies for the given block hashes.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct GetBlockBodies(
/// The block hashes to request bodies for.
pub Vec<H256>,
Expand All @@ -59,7 +66,7 @@ impl From<Vec<H256>> for GetBlockBodies {

// TODO(onbjerg): We should have this type in primitives
/// A response to [`GetBlockBodies`], containing bodies if any bodies were found.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable)]
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable, Serialize, Deserialize)]
pub struct BlockBody {
/// Transactions in the block
pub transactions: Vec<TransactionSigned>,
Expand All @@ -80,7 +87,9 @@ impl BlockBody {

/// The response to [`GetBlockBodies`], containing the block bodies that the peer knows about if
/// any were found.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct BlockBodies(
/// The requested block bodies, each of which should correspond to a hash in the request.
pub Vec<BlockBody>,
Expand Down
21 changes: 15 additions & 6 deletions crates/net/eth-wire/src/types/broadcast.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! Types for broadcasting new data.
use reth_primitives::{Header, TransactionSigned, H256, U128};
use reth_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// This informs peers of new blocks that have appeared on the network.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct NewBlockHashes(
/// New block hashes and the block number for each blockhash.
/// Clients should request blocks using a [`GetBlockBodies`](crate::GetBlockBodies) message.
Expand All @@ -26,7 +29,7 @@ impl NewBlockHashes {
}

/// A block hash _and_ a block number.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable)]
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable, Serialize, Deserialize)]
pub struct BlockHashNumber {
/// The block hash
pub hash: H256,
Expand All @@ -47,7 +50,9 @@ impl From<NewBlockHashes> for Vec<BlockHashNumber> {
}

/// A block body, including transactions and uncle headers.
#[derive(Debug, Clone, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)]
#[derive(
Debug, Clone, PartialEq, Eq, Default, RlpEncodable, RlpDecodable, Serialize, Deserialize,
)]
pub struct RawBlockBody {
/// This block's header
pub header: Header,
Expand All @@ -59,7 +64,7 @@ pub struct RawBlockBody {

/// A new block with the current total difficulty, which includes the difficulty of the returned
/// block.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable)]
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable, Serialize, Deserialize)]
pub struct NewBlock {
/// A new block.
pub block: RawBlockBody,
Expand All @@ -69,7 +74,9 @@ pub struct NewBlock {

/// 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)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct Transactions(
/// New transactions for the peer to include in its mempool.
pub Vec<TransactionSigned>,
Expand Down Expand Up @@ -99,7 +106,9 @@ pub struct SharedTransactions(

/// This informs peers of transaction hashes for transactions that have appeared on the network,
/// but have not been included in a block.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct NewPooledTransactionHashes(
/// Transaction hashes for new transactions that have appeared on the network.
/// Clients should request the transactions with the given hashes using a
Expand Down
9 changes: 5 additions & 4 deletions crates/net/eth-wire/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use super::{
use crate::SharedTransactions;
use bytes::{Buf, BufMut};
use reth_rlp::{length_of_length, Decodable, Encodable, Header};
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, sync::Arc};

/// An `eth` protocol message, containing a message ID and payload.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProtocolMessage {
pub message_type: EthMessageID,
pub message: EthMessage,
Expand Down Expand Up @@ -139,7 +140,7 @@ impl From<EthBroadcastMessage> for ProtocolBroadcastMessage {
///
/// The newer `eth/66` is an efficiency upgrade on top of `eth/65`, introducing a request id to
/// correlate request-response message pairs. This allows for request multiplexing.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum EthMessage {
/// Status is required for the protocol handshake
Status(Status),
Expand Down Expand Up @@ -269,7 +270,7 @@ impl Encodable for EthBroadcastMessage {

/// Represents message IDs for eth protocol messages.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum EthMessageID {
Status = 0x00,
NewBlockHashes = 0x01,
Expand Down Expand Up @@ -351,7 +352,7 @@ impl TryFrom<usize> for EthMessageID {
/// This is used for all request-response style `eth` protocol messages.
/// This can represent either a request or a response, since both include a message payload and
/// request id.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RequestPair<T> {
/// id for the contained request or response message
pub request_id: u64,
Expand Down
9 changes: 7 additions & 2 deletions crates/net/eth-wire/src/types/receipts.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
//! Implements the `GetReceipts` and `Receipts` message types.
use reth_primitives::{Receipt, H256};
use reth_rlp::{RlpDecodableWrapper, RlpEncodableWrapper};
use serde::{Deserialize, Serialize};

/// A request for transaction receipts from the given block hashes.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct GetReceipts(
/// The block hashes to request receipts for.
pub Vec<H256>,
);

/// The response to [`GetReceipts`], containing receipt lists that correspond to each block
/// requested.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct Receipts(
/// Each receipt hash should correspond to a block hash in the request.
pub Vec<Vec<Receipt>>,
Expand Down
9 changes: 7 additions & 2 deletions crates/net/eth-wire/src/types/state.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
//! Implements the `GetNodeData` and `NodeData` message types.
use reth_primitives::H256;
use reth_rlp::{RlpDecodableWrapper, RlpEncodableWrapper};
use serde::{Deserialize, Serialize};

/// A request for state tree nodes corresponding to the given hashes.
/// This message was removed in `eth/67`, only clients running `eth/66` or earlier will respond to
/// this message.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct GetNodeData(pub Vec<H256>);

/// The response to [`GetNodeData`], containing the state tree nodes or contract bytecode
/// corresponding to the requested hashes.
///
/// Not all nodes are guaranteed to be returned by the peer.
/// This message was removed in `eth/67`.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct NodeData(pub Vec<bytes::Bytes>);

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion crates/net/eth-wire/src/types/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{EthVersion, StatusBuilder};

use reth_primitives::{Chain, ForkId, Hardfork, H256, MAINNET_GENESIS, U256};
use reth_rlp::{RlpDecodable, RlpEncodable};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display};

/// The status message is used in the eth protocol handshake to ensure that peers are on the same
Expand All @@ -11,7 +12,7 @@ use std::fmt::{Debug, Display};
///
/// When performing a handshake, the total difficulty is not guaranteed to correspond to the block
/// hash. This information should be treated as untrusted.
#[derive(Copy, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
#[derive(Copy, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable, Serialize, Deserialize)]
pub struct Status {
/// The current protocol version. For example, peers running `eth/66` would have a version of
/// 66.
Expand Down
9 changes: 7 additions & 2 deletions crates/net/eth-wire/src/types/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! Implements the `GetPooledTransactions` and `PooledTransactions` message types.
use reth_primitives::{TransactionSigned, H256};
use reth_rlp::{RlpDecodableWrapper, RlpEncodableWrapper};
use serde::{Deserialize, Serialize};

/// A list of transaction hashes that the peer would like transaction bodies for.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct GetPooledTransactions(
/// The transaction hashes to request transaction bodies for.
pub Vec<H256>,
Expand All @@ -25,7 +28,9 @@ where
/// as the request's hashes. Hashes may be skipped, and the client should ensure that each body
/// corresponds to a requested hash. Hashes may need to be re-requested if the bodies are not
/// included in the response.
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper)]
#[derive(
Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Serialize, Deserialize,
)]
pub struct PooledTransactions(
/// The transaction bodies, each of which should correspond to a requested hash.
pub Vec<TransactionSigned>,
Expand Down
Loading

0 comments on commit 6c91322

Please sign in to comment.