From b28c40223ed0c0477de778abd4448d8e79547c8a Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Tue, 21 Mar 2023 00:14:23 -0400 Subject: [PATCH] chore: move block body to primitives (#1874) --- Cargo.lock | 1 - crates/interfaces/src/p2p/bodies/client.rs | 3 +- crates/interfaces/src/test_utils/bodies.rs | 3 +- crates/net/downloaders/Cargo.toml | 1 - crates/net/downloaders/src/bodies/bodies.rs | 3 +- crates/net/downloaders/src/bodies/request.rs | 3 +- .../net/downloaders/src/bodies/test_utils.rs | 3 +- .../src/headers/reverse_headers.rs | 4 +-- .../downloaders/src/test_utils/file_client.rs | 4 +-- crates/net/downloaders/src/test_utils/mod.rs | 3 +- .../downloaders/src/test_utils/test_client.rs | 3 +- crates/net/eth-wire/src/types/blocks.rs | 33 +------------------ crates/net/network/src/eth_requests.rs | 6 ++-- crates/net/network/src/fetch/mod.rs | 4 +-- crates/net/network/src/message.rs | 8 ++--- crates/net/network/src/state.rs | 4 +-- crates/net/network/tests/it/requests.rs | 3 +- crates/primitives/src/block.rs | 29 ++++++++++++++++ crates/primitives/src/lib.rs | 3 +- crates/stages/src/stages/bodies.rs | 3 +- 20 files changed, 56 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d1b92e0b2..d7135377b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4709,7 +4709,6 @@ dependencies = [ "pin-project", "rayon", "reth-db", - "reth-eth-wire", "reth-interfaces", "reth-metrics-derive", "reth-primitives", diff --git a/crates/interfaces/src/p2p/bodies/client.rs b/crates/interfaces/src/p2p/bodies/client.rs index e75f9aa01..3dbd8b1d9 100644 --- a/crates/interfaces/src/p2p/bodies/client.rs +++ b/crates/interfaces/src/p2p/bodies/client.rs @@ -2,8 +2,7 @@ use std::pin::Pin; use crate::p2p::{download::DownloadClient, error::PeerRequestResult, priority::Priority}; use futures::Future; -use reth_eth_wire::BlockBody; -use reth_primitives::H256; +use reth_primitives::{BlockBody, H256}; /// The bodies future type pub type BodiesFut = Pin>> + Send + Sync>>; diff --git a/crates/interfaces/src/test_utils/bodies.rs b/crates/interfaces/src/test_utils/bodies.rs index e6e242919..6a6e9ad85 100644 --- a/crates/interfaces/src/test_utils/bodies.rs +++ b/crates/interfaces/src/test_utils/bodies.rs @@ -6,8 +6,7 @@ use crate::p2p::{ }; use async_trait::async_trait; use futures::{future, Future, FutureExt}; -use reth_eth_wire::BlockBody; -use reth_primitives::{WithPeerId, H256}; +use reth_primitives::{BlockBody, WithPeerId, H256}; use std::{ fmt::{Debug, Formatter}, pin::Pin, diff --git a/crates/net/downloaders/Cargo.toml b/crates/net/downloaders/Cargo.toml index 76a7fc67a..e81663017 100644 --- a/crates/net/downloaders/Cargo.toml +++ b/crates/net/downloaders/Cargo.toml @@ -11,7 +11,6 @@ description = "Implementations of various block downloaders" # reth reth-interfaces = { path = "../../interfaces" } reth-primitives = { path = "../../primitives" } -reth-eth-wire = { path = "../eth-wire" } reth-db = { path = "../../storage/db" } reth-tasks = { path = "../../tasks" } reth-metrics-derive = { path = "../../metrics/metrics-derive" } diff --git a/crates/net/downloaders/src/bodies/bodies.rs b/crates/net/downloaders/src/bodies/bodies.rs index ef104b48e..c3cd2603f 100644 --- a/crates/net/downloaders/src/bodies/bodies.rs +++ b/crates/net/downloaders/src/bodies/bodies.rs @@ -536,9 +536,8 @@ mod tests { use assert_matches::assert_matches; use futures_util::stream::StreamExt; use reth_db::mdbx::{test_utils::create_test_db, EnvKind, WriteMap}; - use reth_eth_wire::BlockBody; use reth_interfaces::test_utils::{generators::random_block_range, TestConsensus}; - use reth_primitives::H256; + use reth_primitives::{BlockBody, H256}; use std::{collections::HashMap, sync::Arc}; // Check that the blocks are emitted in order of block number, not in order of diff --git a/crates/net/downloaders/src/bodies/request.rs b/crates/net/downloaders/src/bodies/request.rs index 262664a1a..caf4a557a 100644 --- a/crates/net/downloaders/src/bodies/request.rs +++ b/crates/net/downloaders/src/bodies/request.rs @@ -1,6 +1,5 @@ use crate::metrics::DownloaderMetrics; use futures::{Future, FutureExt}; -use reth_eth_wire::BlockBody; use reth_interfaces::{ consensus::{Consensus as ConsensusTrait, Consensus}, p2p::{ @@ -9,7 +8,7 @@ use reth_interfaces::{ priority::Priority, }, }; -use reth_primitives::{PeerId, SealedBlock, SealedHeader, WithPeerId, H256}; +use reth_primitives::{BlockBody, PeerId, SealedBlock, SealedHeader, WithPeerId, H256}; use std::{ collections::VecDeque, pin::Pin, diff --git a/crates/net/downloaders/src/bodies/test_utils.rs b/crates/net/downloaders/src/bodies/test_utils.rs index c69d26d47..1780ea2ad 100644 --- a/crates/net/downloaders/src/bodies/test_utils.rs +++ b/crates/net/downloaders/src/bodies/test_utils.rs @@ -6,9 +6,8 @@ use reth_db::{ tables, transaction::DbTxMut, }; -use reth_eth_wire::BlockBody; use reth_interfaces::{db, p2p::bodies::response::BlockResponse}; -use reth_primitives::{Block, SealedBlock, SealedHeader, H256}; +use reth_primitives::{Block, BlockBody, SealedBlock, SealedHeader, H256}; use std::collections::HashMap; pub(crate) fn zip_blocks<'a>( diff --git a/crates/net/downloaders/src/headers/reverse_headers.rs b/crates/net/downloaders/src/headers/reverse_headers.rs index 241bf3ded..4f5bff539 100644 --- a/crates/net/downloaders/src/headers/reverse_headers.rs +++ b/crates/net/downloaders/src/headers/reverse_headers.rs @@ -1010,8 +1010,8 @@ impl Default for ReverseHeadersDownloaderBuilder { impl ReverseHeadersDownloaderBuilder { /// Set the request batch size. /// - /// This determines the `limit` for a [GetHeaders](reth_eth_wire::GetBlockHeaders) requests, the - /// number of headers we ask for. + /// This determines the `limit` for a `GetBlockHeaders` requests, the number of headers we ask + /// for. pub fn request_limit(mut self, limit: u64) -> Self { self.request_limit = limit; self diff --git a/crates/net/downloaders/src/test_utils/file_client.rs b/crates/net/downloaders/src/test_utils/file_client.rs index 064e03dd2..50c570d4a 100644 --- a/crates/net/downloaders/src/test_utils/file_client.rs +++ b/crates/net/downloaders/src/test_utils/file_client.rs @@ -1,6 +1,5 @@ use super::file_codec::BlockFileCodec; use itertools::Either; -use reth_eth_wire::BlockBody; use reth_interfaces::{ p2p::{ bodies::client::{BodiesClient, BodiesFut}, @@ -12,7 +11,8 @@ use reth_interfaces::{ sync::{SyncState, SyncStateProvider, SyncStateUpdater}, }; use reth_primitives::{ - Block, BlockHash, BlockHashOrNumber, BlockNumber, Header, HeadersDirection, PeerId, H256, + Block, BlockBody, BlockHash, BlockHashOrNumber, BlockNumber, Header, HeadersDirection, PeerId, + H256, }; use reth_rlp::{Decodable, Header as RlpHeader}; use std::{ diff --git a/crates/net/downloaders/src/test_utils/mod.rs b/crates/net/downloaders/src/test_utils/mod.rs index 93128af09..0bc953e71 100644 --- a/crates/net/downloaders/src/test_utils/mod.rs +++ b/crates/net/downloaders/src/test_utils/mod.rs @@ -2,9 +2,8 @@ //! Test helper impls use crate::bodies::test_utils::create_raw_bodies; use futures::SinkExt; -use reth_eth_wire::BlockBody; use reth_interfaces::test_utils::generators::random_block_range; -use reth_primitives::{SealedHeader, H256}; +use reth_primitives::{BlockBody, SealedHeader, H256}; use std::{collections::HashMap, io::SeekFrom}; use tokio::{ fs::File, diff --git a/crates/net/downloaders/src/test_utils/test_client.rs b/crates/net/downloaders/src/test_utils/test_client.rs index 562baad3b..9f120c0b2 100644 --- a/crates/net/downloaders/src/test_utils/test_client.rs +++ b/crates/net/downloaders/src/test_utils/test_client.rs @@ -1,10 +1,9 @@ -use reth_eth_wire::BlockBody; use reth_interfaces::p2p::{ bodies::client::{BodiesClient, BodiesFut}, download::DownloadClient, priority::Priority, }; -use reth_primitives::{PeerId, H256}; +use reth_primitives::{BlockBody, PeerId, H256}; use std::{ collections::HashMap, fmt::Debug, diff --git a/crates/net/eth-wire/src/types/blocks.rs b/crates/net/eth-wire/src/types/blocks.rs index 1890b355d..c0db52f2d 100644 --- a/crates/net/eth-wire/src/types/blocks.rs +++ b/crates/net/eth-wire/src/types/blocks.rs @@ -1,9 +1,7 @@ //! Implements the `GetBlockHeaders`, `GetBlockBodies`, `BlockHeaders`, and `BlockBodies` message //! types. use reth_codecs::derive_arbitrary; -use reth_primitives::{ - Block, BlockHashOrNumber, Header, HeadersDirection, TransactionSigned, Withdrawal, H256, -}; +use reth_primitives::{BlockBody, BlockHashOrNumber, Header, HeadersDirection, H256}; use reth_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper}; #[cfg(feature = "serde")] @@ -68,35 +66,6 @@ impl From> for GetBlockBodies { } } -// TODO(onbjerg): We should have this type in primitives -/// A response to [`GetBlockBodies`], containing bodies if any bodies were found. -/// -/// Withdrawals can be optionally included at the end of the RLP encoded message. -#[derive_arbitrary(rlp, 10)] -#[derive(Clone, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[rlp(trailing)] -pub struct BlockBody { - /// Transactions in the block - pub transactions: Vec, - /// Uncle headers for the given block - pub ommers: Vec
, - /// Withdrawals in the block. - pub withdrawals: Option>, -} - -impl BlockBody { - /// Create a [`Block`](reth_primitives::Block) from the body and its header. - pub fn create_block(&self, header: Header) -> Block { - Block { - header, - body: self.transactions.clone(), - ommers: self.ommers.clone(), - withdrawals: self.withdrawals.clone(), - } - } -} - /// The response to [`GetBlockBodies`], containing the block bodies that the peer knows about if /// any were found. #[derive_arbitrary(rlp, 1)] diff --git a/crates/net/network/src/eth_requests.rs b/crates/net/network/src/eth_requests.rs index 80d36cee7..4d156498b 100644 --- a/crates/net/network/src/eth_requests.rs +++ b/crates/net/network/src/eth_requests.rs @@ -3,11 +3,11 @@ use crate::peers::PeersHandle; use futures::StreamExt; use reth_eth_wire::{ - BlockBodies, BlockBody, BlockHeaders, GetBlockBodies, GetBlockHeaders, GetNodeData, - GetReceipts, NodeData, Receipts, + BlockBodies, BlockHeaders, GetBlockBodies, GetBlockHeaders, GetNodeData, GetReceipts, NodeData, + Receipts, }; use reth_interfaces::p2p::error::RequestResult; -use reth_primitives::{BlockHashOrNumber, Header, HeadersDirection, PeerId}; +use reth_primitives::{BlockBody, BlockHashOrNumber, Header, HeadersDirection, PeerId}; use reth_provider::{BlockProvider, HeaderProvider}; use std::{ borrow::Borrow, diff --git a/crates/net/network/src/fetch/mod.rs b/crates/net/network/src/fetch/mod.rs index 8672fa6a7..995b606c3 100644 --- a/crates/net/network/src/fetch/mod.rs +++ b/crates/net/network/src/fetch/mod.rs @@ -2,14 +2,14 @@ use crate::{message::BlockRequest, peers::PeersHandle}; use futures::StreamExt; -use reth_eth_wire::{BlockBody, GetBlockBodies, GetBlockHeaders}; +use reth_eth_wire::{GetBlockBodies, GetBlockHeaders}; use reth_interfaces::p2p::{ error::{EthResponseValidator, PeerRequestResult, RequestError, RequestResult}, headers::client::HeadersRequest, priority::Priority, }; use reth_network_api::ReputationChangeKind; -use reth_primitives::{Header, PeerId, H256}; +use reth_primitives::{BlockBody, Header, PeerId, H256}; use std::{ collections::{HashMap, VecDeque}, sync::{ diff --git a/crates/net/network/src/message.rs b/crates/net/network/src/message.rs index f2027c074..d14e31442 100644 --- a/crates/net/network/src/message.rs +++ b/crates/net/network/src/message.rs @@ -5,13 +5,13 @@ use futures::FutureExt; use reth_eth_wire::{ - capability::RawCapabilityMessage, message::RequestPair, BlockBodies, BlockBody, BlockHeaders, - EthMessage, GetBlockBodies, GetBlockHeaders, GetNodeData, GetPooledTransactions, GetReceipts, - NewBlock, NewBlockHashes, NewPooledTransactionHashes, NodeData, PooledTransactions, Receipts, + capability::RawCapabilityMessage, message::RequestPair, BlockBodies, BlockHeaders, EthMessage, + GetBlockBodies, GetBlockHeaders, GetNodeData, GetPooledTransactions, GetReceipts, NewBlock, + NewBlockHashes, NewPooledTransactionHashes, NodeData, PooledTransactions, Receipts, SharedTransactions, Transactions, }; use reth_interfaces::p2p::error::{RequestError, RequestResult}; -use reth_primitives::{Bytes, Header, PeerId, Receipt, TransactionSigned, H256}; +use reth_primitives::{BlockBody, Bytes, Header, PeerId, Receipt, TransactionSigned, H256}; use std::{ fmt, sync::Arc, diff --git a/crates/net/network/src/state.rs b/crates/net/network/src/state.rs index 995b06be3..11baeae95 100644 --- a/crates/net/network/src/state.rs +++ b/crates/net/network/src/state.rs @@ -511,10 +511,10 @@ mod tests { }; use reth_eth_wire::{ capability::{Capabilities, Capability}, - BlockBodies, BlockBody, EthVersion, Status, + BlockBodies, EthVersion, Status, }; use reth_interfaces::p2p::{bodies::client::BodiesClient, error::RequestError}; - use reth_primitives::{Header, PeerId, H256}; + use reth_primitives::{BlockBody, Header, PeerId, H256}; use reth_provider::test_utils::NoopProvider; use std::{ future::poll_fn, diff --git a/crates/net/network/tests/it/requests.rs b/crates/net/network/tests/it/requests.rs index 496461408..01bb7fb67 100644 --- a/crates/net/network/tests/it/requests.rs +++ b/crates/net/network/tests/it/requests.rs @@ -1,7 +1,6 @@ //! Tests for eth related requests use rand::Rng; -use reth_eth_wire::BlockBody; use reth_interfaces::p2p::{ bodies::client::BodiesClient, headers::client::{HeadersClient, HeadersRequest}, @@ -9,7 +8,7 @@ use reth_interfaces::p2p::{ use reth_network::test_utils::{NetworkEventStream, Testnet}; use reth_network_api::{NetworkInfo, Peers}; use reth_primitives::{ - Block, Bytes, Header, HeadersDirection, Signature, Transaction, TransactionKind, + Block, BlockBody, Bytes, Header, HeadersDirection, Signature, Transaction, TransactionKind, TransactionSigned, TxEip2930, H256, U256, }; use reth_provider::test_utils::MockEthProvider; diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index 8b00b8246..d5b7a77bd 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -570,6 +570,35 @@ impl AsRef for BlockHash { } } +/// A response to `GetBlockBodies`, containing bodies if any bodies were found. +/// +/// Withdrawals can be optionally included at the end of the RLP encoded message. +#[derive_arbitrary(rlp, 10)] +#[derive( + Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize, RlpEncodable, RlpDecodable, +)] +#[rlp(trailing)] +pub struct BlockBody { + /// Transactions in the block + pub transactions: Vec, + /// Uncle headers for the given block + pub ommers: Vec
, + /// Withdrawals in the block. + pub withdrawals: Option>, +} + +impl BlockBody { + /// Create a [`Block`](Block) from the body and its header. + pub fn create_block(&self, header: Header) -> Block { + Block { + header, + body: self.transactions.clone(), + ommers: self.ommers.clone(), + withdrawals: self.withdrawals.clone(), + } + } +} + #[cfg(test)] mod test { use super::{BlockId, BlockNumberOrTag::*, *}; diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index da7ec3984..79e8bb1ca 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -39,7 +39,8 @@ pub mod proofs; pub use account::{Account, Bytecode}; pub use bits::H512; pub use block::{ - Block, BlockHashOrNumber, BlockId, BlockNumberOrTag, SealedBlock, SealedBlockWithSenders, + Block, BlockBody, BlockHashOrNumber, BlockId, BlockNumberOrTag, SealedBlock, + SealedBlockWithSenders, }; pub use bloom::Bloom; pub use chain::{ diff --git a/crates/stages/src/stages/bodies.rs b/crates/stages/src/stages/bodies.rs index 8f6aaf87c..f90891b16 100644 --- a/crates/stages/src/stages/bodies.rs +++ b/crates/stages/src/stages/bodies.rs @@ -447,7 +447,6 @@ mod tests { tables, transaction::{DbTx, DbTxMut}, }; - use reth_eth_wire::BlockBody; use reth_interfaces::{ consensus::Consensus, p2p::{ @@ -465,7 +464,7 @@ mod tests { TestConsensus, }, }; - use reth_primitives::{BlockNumber, SealedBlock, SealedHeader, TxNumber, H256}; + use reth_primitives::{BlockBody, BlockNumber, SealedBlock, SealedHeader, TxNumber, H256}; use std::{ collections::{HashMap, VecDeque}, ops::Range,