Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
b6f7353
Request/Response: remove NetworkCodec trait
acerone85 Oct 23, 2024
529d1d6
Dinstinguish between Format being used and Codec for requests and res…
acerone85 Oct 23, 2024
a4465d5
Move RequestResponse protocol definitions to dedicated module
acerone85 Oct 23, 2024
7b858c5
Decouple GossibSub and RequestResponse codecs
acerone85 Oct 23, 2024
f2cd720
Merge branch 'master' into 2368-remove-p2pservice-networkcodec-trait
acerone85 Oct 28, 2024
1aa98dc
Simplify DataFormat trait
acerone85 Oct 28, 2024
79b507d
Use Encode and Decode trait in favour of DataFormat
acerone85 Oct 28, 2024
ead761a
Rename codecs to message handlers
acerone85 Oct 28, 2024
ed29376
Format
acerone85 Oct 28, 2024
138716b
Encode trait function takes &self as argument
acerone85 Oct 28, 2024
58ffe67
Minor improvements
acerone85 Oct 28, 2024
c564941
Decode trait function takes &self as argument
acerone85 Oct 28, 2024
c5aa362
DataFormat is now Codec
acerone85 Oct 28, 2024
89b022c
Typo
acerone85 Oct 28, 2024
45bbd91
CHANGELOG
acerone85 Oct 28, 2024
caf4f00
Add issue to TODO
acerone85 Oct 28, 2024
a55f695
Rename lifetime
acerone85 Oct 28, 2024
fc2d71d
Formatting
acerone85 Oct 29, 2024
136e6b4
Placate Clippy
acerone85 Oct 29, 2024
5bd9e21
Merge branch 'master' into 2368-remove-p2pservice-networkcodec-trait
acerone85 Nov 15, 2024
029fbff
Fix test compilation
acerone85 Nov 15, 2024
39e1ed9
Merge branch 'master' into 2368-remove-p2pservice-networkcodec-trait
acerone85 Nov 26, 2024
05c50fd
Reference todo issue
acerone85 Nov 26, 2024
e9f1404
Avoid dereference
acerone85 Nov 26, 2024
b94c451
Todo: max_response_size should be u64
acerone85 Nov 26, 2024
93324e7
P2pService: Make max_response_size in PostcardCodec a u64
acerone85 Nov 26, 2024
4fdf8f5
CHANGELOG
acerone85 Nov 26, 2024
fa021c5
Merge branch 'master' into 2368-remove-p2pservice-networkcodec-trait
acerone85 Feb 18, 2025
a9c9eca
Remove issue to move codec
acerone85 Feb 18, 2025
cbfd9a7
Remove spurious anyhow::Error
acerone85 Feb 18, 2025
d4e2198
se Vec<u8> for Encoder to avoid cloning
acerone85 Feb 18, 2025
d40b532
Remove Send trait bound
acerone85 Feb 18, 2025
5063dec
futures::AsyncWrite -> AsyncWrite
acerone85 Feb 18, 2025
07924f6
unboundedCodec -> PostcardCodec
acerone85 Feb 18, 2025
ad57a6b
Remove default implementation of ReqRes protocol
acerone85 Feb 18, 2025
8faebd1
as_vec -> to_vec
acerone85 Feb 18, 2025
061886d
Revert "se Vec<u8> for Encoder to avoid cloning"
acerone85 Feb 18, 2025
37e4e23
Do not clone into Vec<u8>
acerone85 Feb 18, 2025
be931ff
Update crates/services/p2p/src/codecs.rs
xgreenx Feb 18, 2025
b92e7e0
Merge branch 'refs/heads/master' into 2368-remove-p2pservice-networkc…
xgreenx Feb 20, 2025
b608036
Use new Changelog
xgreenx Feb 20, 2025
1cbdfb4
Use new Changelog
xgreenx Feb 20, 2025
05fde0b
Merge branch 'refs/heads/2368-remove-p2pservice-networkcodec-trait' i…
xgreenx Feb 20, 2025
fd9bc83
Use nwe changelog system
xgreenx Feb 20, 2025
6fe229f
Merge branch 'refs/heads/master' into 2459_max_response_size_u64
xgreenx Feb 24, 2025
d8e73c7
Fix merging conflicts
xgreenx Feb 24, 2025
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
1 change: 1 addition & 0 deletions .changes/changed/2460.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The type of the `max_response_size`for the postcard codec used in `RequestResponse` protocols has been changed from `usize` to `u64`.
10 changes: 7 additions & 3 deletions bin/fuel-core/src/cli/run/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use std::{
str::FromStr,
};

const MAX_RESPONSE_SIZE_STR: &str = const_format::formatcp!("{MAX_RESPONSE_SIZE}");
const MAX_RESPONSE_SIZE_STR: &str =
const_format::formatcp!("{}", MAX_RESPONSE_SIZE.get());

#[derive(Debug, Clone, Args)]
pub struct P2PArgs {
Expand Down Expand Up @@ -59,7 +60,7 @@ pub struct P2PArgs {

/// Max Block size
#[clap(long = "max-block-size", default_value = MAX_RESPONSE_SIZE_STR, env)]
pub max_block_size: usize,
pub max_block_size: u32,

/// Max number of blocks/headers in a single headers request response
#[clap(long = "max-headers-per-request", default_value = "100", env)]
Expand Down Expand Up @@ -308,6 +309,9 @@ impl P2PArgs {
)
};

let max_block_size = NonZeroU32::new(self.max_block_size)
.ok_or(anyhow!("max block size must be greater than zero"))?;

let config = Config {
keypair: local_keypair,
network_name,
Expand All @@ -317,7 +321,7 @@ impl P2PArgs {
.unwrap_or_else(|| IpAddr::V4(Ipv4Addr::from([0, 0, 0, 0]))),
public_address: self.public_address,
tcp_port: self.peering_port,
max_block_size: self.max_block_size,
max_block_size,
max_headers_per_request: self.max_headers_per_request,
max_txs_per_request: self.max_txs_per_request,
bootstrap_nodes: self.bootstrap_nodes,
Expand Down
27 changes: 13 additions & 14 deletions crates/services/p2p/src/codecs/postcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ use super::{
use std::{
borrow::Cow,
io,
num::NonZeroU32,
};

#[derive(Clone, Default)]
pub struct PostcardCodec;

impl RequestResponseMessageHandler<PostcardCodec> {
pub fn new(max_block_size: usize) -> Self {
assert_ne!(
max_block_size, 0,
"RequestResponseMessageHandler does not support zero block size"
);

pub fn new(max_block_size: NonZeroU32) -> Self {
Self {
codec: PostcardCodec,
max_response_size: max_block_size,
Expand Down Expand Up @@ -76,17 +72,20 @@ mod tests {
ResponseMessageErrorCode,
V1ResponseMessage,
V2ResponseMessage,
MAX_REQUEST_SIZE,
},
protocols::RequestResponseProtocol,
},
};

const MAX_REQUEST_SIZE: NonZeroU32 = unsafe { NonZeroU32::new_unchecked(1024) };

#[test]
fn test_request_size_fits() {
let arbitrary_range = 2..6;
let m = RequestMessage::Transactions(arbitrary_range);
assert!(postcard::to_stdvec(&m).unwrap().len() <= MAX_REQUEST_SIZE);
assert!(
postcard::to_stdvec(&m).unwrap().len() <= MAX_REQUEST_SIZE.get() as usize
);
}

#[tokio::test]
Expand All @@ -96,7 +95,7 @@ mod tests {
let sealed_block_headers = vec![SealedBlockHeader::default()];
let response = V2ResponseMessage::SealedHeaders(Ok(sealed_block_headers.clone()));
let mut codec: RequestResponseMessageHandler<PostcardCodec> =
RequestResponseMessageHandler::new(1024);
RequestResponseMessageHandler::new(MAX_REQUEST_SIZE);
let mut buf = Vec::with_capacity(1024);

// When
Expand Down Expand Up @@ -124,7 +123,7 @@ mod tests {
let sealed_block_headers = vec![SealedBlockHeader::default()];
let response = V2ResponseMessage::SealedHeaders(Ok(sealed_block_headers.clone()));
let mut codec: RequestResponseMessageHandler<PostcardCodec> =
RequestResponseMessageHandler::new(1024);
RequestResponseMessageHandler::new(MAX_REQUEST_SIZE);
let mut buf = Vec::with_capacity(1024);

// When
Expand Down Expand Up @@ -152,7 +151,7 @@ mod tests {
ResponseMessageErrorCode::ProtocolV1EmptyResponse,
));
let mut codec: RequestResponseMessageHandler<PostcardCodec> =
RequestResponseMessageHandler::new(1024);
RequestResponseMessageHandler::new(MAX_REQUEST_SIZE);
let mut buf = Vec::with_capacity(1024);

// When
Expand Down Expand Up @@ -183,7 +182,7 @@ mod tests {
ResponseMessageErrorCode::RequestedRangeTooLarge,
));
let mut codec: RequestResponseMessageHandler<PostcardCodec> =
RequestResponseMessageHandler::new(1024);
RequestResponseMessageHandler::new(MAX_REQUEST_SIZE);
let mut buf = Vec::with_capacity(1024);

// When
Expand Down Expand Up @@ -213,7 +212,7 @@ mod tests {
ResponseMessageErrorCode::ProtocolV1EmptyResponse,
));
let mut codec: RequestResponseMessageHandler<PostcardCodec> =
RequestResponseMessageHandler::new(1024);
RequestResponseMessageHandler::new(MAX_REQUEST_SIZE);
let mut buf = Vec::with_capacity(1024);

// When
Expand All @@ -239,7 +238,7 @@ mod tests {
// Given
let response = V1ResponseMessage::SealedHeaders(None);
let mut codec: RequestResponseMessageHandler<PostcardCodec> =
RequestResponseMessageHandler::new(1024);
RequestResponseMessageHandler::new(MAX_REQUEST_SIZE);

// When
let buf = codec
Expand Down
8 changes: 3 additions & 5 deletions crates/services/p2p/src/codecs/request_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ pub struct RequestResponseMessageHandler<Codec> {
/// Used for `max_size` parameter when reading Response Message
/// Necessary in order to avoid DoS attacks
/// Currently the size mostly depends on the max size of the Block
// TODO: https://github.com/FuelLabs/fuel-core/issues/2459.
// Make this a u64 instead of usize.
pub(crate) max_response_size: usize,
pub(crate) max_response_size: std::num::NonZeroU32,
}

/// Since Postcard does not support async reads or writes out of the box
Expand Down Expand Up @@ -68,7 +66,7 @@ where
{
let mut response = Vec::new();
socket
.take(self.max_response_size as u64)
.take(self.max_response_size.get() as u64)
.read_to_end(&mut response)
.await?;
self.codec.decode(&response)
Expand All @@ -84,7 +82,7 @@ where
{
let mut response = Vec::new();
socket
.take(self.max_response_size as u64)
.take(self.max_response_size.get() as u64)
.read_to_end(&mut response)
.await?;

Expand Down
7 changes: 5 additions & 2 deletions crates/services/p2p/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ use std::{
IpAddr,
Ipv4Addr,
},
num::NonZeroU32,
time::Duration,
};

mod connection_tracker;
mod fuel_authenticated;
pub(crate) mod fuel_upgrade;
Expand All @@ -40,7 +42,8 @@ const REQ_RES_TIMEOUT: Duration = Duration::from_secs(20);
/// The configuration of the ingress should be the same:
/// - `nginx.org/client-max-body-size`
/// - `nginx.ingress.kubernetes.io/proxy-body-size`
pub const MAX_RESPONSE_SIZE: usize = 50 * 1024 * 1024;
pub const MAX_RESPONSE_SIZE: NonZeroU32 =
unsafe { NonZeroU32::new_unchecked(50 * 1024 * 1024) };

/// Maximum number of blocks per request.
pub const MAX_HEADERS_PER_REQUEST: usize = 100;
Expand Down Expand Up @@ -69,7 +72,7 @@ pub struct Config<State = Initialized> {
pub tcp_port: u16,

/// Max Size of a Block in bytes
pub max_block_size: usize,
pub max_block_size: NonZeroU32,
pub max_headers_per_request: usize,

// Maximum of txs id asked in a single request
Expand Down
2 changes: 1 addition & 1 deletion crates/services/p2p/src/gossipsub/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub(crate) fn default_gossipsub_config() -> gossipsub::Config {
.gossip_lazy(6)
.history_length(5)
.history_gossip(3)
.max_transmit_size(MAX_RESPONSE_SIZE)
.max_transmit_size(MAX_RESPONSE_SIZE.get() as usize)
.heartbeat_interval(Duration::from_millis(700))
.fanout_ttl(Duration::from_secs(60))
.build()
Expand Down
4 changes: 0 additions & 4 deletions crates/services/p2p/src/request_response/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ use std::ops::Range;
use thiserror::Error;
use tokio::sync::oneshot;

/// Max Size in Bytes of the Request Message
#[cfg(test)]
pub(crate) const MAX_REQUEST_SIZE: usize = core::mem::size_of::<RequestMessage>();

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
pub enum RequestMessage {
SealedHeaders(Range<u32>),
Expand Down
Loading