feat: make max EthMessageID dependent on the EthVersion (#16405)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Femi Bankole
2025-05-22 21:02:58 +01:00
committed by GitHub
parent 29eeb78ad0
commit 641f99ffda
5 changed files with 20 additions and 18 deletions

View File

@@ -494,9 +494,13 @@ impl EthMessageID {
}
}
/// Returns the max value.
pub const fn max() -> u8 {
Self::Receipts.to_u8()
/// Returns the max value for the given version.
pub const fn max(version: EthVersion) -> u8 {
if version.is_eth69() {
Self::BlockRangeUpdate.to_u8()
} else {
Self::Receipts.to_u8()
}
}
}

View File

@@ -41,8 +41,8 @@ impl EthVersion {
// eth/67,68 are eth/66 minus GetNodeData and NodeData messages
13
}
// eth69 is both eth67 and eth68 minus NewBlockHashes and NewBlock
Self::Eth69 => 11,
// eth69 is both eth67 and eth68 minus NewBlockHashes and NewBlock + BlockRangeUpdate
Self::Eth69 => 12,
}
}
@@ -265,6 +265,6 @@ mod tests {
assert_eq!(EthVersion::Eth66.total_messages(), 15);
assert_eq!(EthVersion::Eth67.total_messages(), 13);
assert_eq!(EthVersion::Eth68.total_messages(), 13);
assert_eq!(EthVersion::Eth69.total_messages(), 11);
assert_eq!(EthVersion::Eth69.total_messages(), 12);
}
}

View File

@@ -134,7 +134,7 @@ impl SharedCapability {
/// Returns the number of protocol messages supported by this capability.
pub const fn num_messages(&self) -> u8 {
match self {
Self::Eth { version: _version, .. } => EthMessageID::max() + 1,
Self::Eth { version, .. } => EthMessageID::max(*version) + 1,
Self::UnknownCapability { messages, .. } => *messages,
}
}

View File

@@ -223,7 +223,7 @@ where
// and eth message IDs are <= [`EthMessageID::max()`],
// snap message IDs are > [`EthMessageID::max()`].
// See also <https://github.com/paradigmxyz/reth/blob/main/crates/net/eth-wire/src/capability.rs#L272-L283>.
if message_id <= EthMessageID::max() {
if message_id <= EthMessageID::max(self.eth_version) {
let mut buf = bytes.as_ref();
match ProtocolMessage::decode_message(self.eth_version, &mut buf) {
Ok(protocol_msg) => {
@@ -236,8 +236,9 @@ where
Err(EthSnapStreamError::InvalidMessage(self.eth_version, err.to_string()))
}
}
} else if message_id > EthMessageID::max() &&
message_id <= EthMessageID::max() + 1 + SnapMessageId::TrieNodes as u8
} else if message_id > EthMessageID::max(self.eth_version) &&
message_id <=
EthMessageID::max(self.eth_version) + 1 + SnapMessageId::TrieNodes as u8
{
// Checks for multiplexed snap message IDs :
// - message_id > EthMessageID::max() : ensures it's not an eth message
@@ -245,7 +246,7 @@ where
// range
// Message IDs are assigned lexicographically during capability negotiation
// So real_snap_id = multiplexed_id - num_eth_messages
let adjusted_message_id = message_id - (EthMessageID::max() + 1);
let adjusted_message_id = message_id - (EthMessageID::max(self.eth_version) + 1);
let mut buf = &bytes[1..];
match SnapProtocolMessage::decode(adjusted_message_id, &mut buf) {
@@ -275,7 +276,7 @@ where
let encoded = message.encode();
let message_id = encoded[0];
let adjusted_id = message_id + EthMessageID::max() + 1;
let adjusted_id = message_id + EthMessageID::max(self.eth_version) + 1;
let mut adjusted = Vec::with_capacity(encoded.len());
adjusted.push(adjusted_id);
@@ -396,7 +397,7 @@ mod tests {
let inner = EthSnapStreamInner::<EthNetworkPrimitives>::new(EthVersion::Eth67);
// Create a bytes buffer with eth message ID at the max boundary with minimal content
let eth_max_id = EthMessageID::max();
let eth_max_id = EthMessageID::max(EthVersion::Eth67);
let mut eth_boundary_bytes = BytesMut::new();
eth_boundary_bytes.extend_from_slice(&[eth_max_id]);
eth_boundary_bytes.extend_from_slice(&[0, 0]);

View File

@@ -1,6 +1,6 @@
//! A Protocol defines a P2P subprotocol in an `RLPx` connection
use crate::{Capability, EthMessageID, EthVersion};
use crate::{Capability, EthVersion};
/// Type that represents a [Capability] and the number of messages it uses.
///
@@ -52,10 +52,7 @@ impl Protocol {
}
/// The number of values needed to represent all message IDs of capability.
pub fn messages(&self) -> u8 {
if self.cap.is_eth() {
return EthMessageID::max() + 1
}
pub const fn messages(&self) -> u8 {
self.messages
}
}