Fix eth message ID length bug (#5666)

This commit is contained in:
Emilia Hane
2023-12-04 17:01:40 +01:00
committed by GitHub
parent 0555800818
commit f15e878250
3 changed files with 26 additions and 5 deletions

View File

@ -5,7 +5,7 @@ use crate::{
p2pstream::MAX_RESERVED_MESSAGE_ID,
protocol::{ProtoVersion, Protocol},
version::ParseVersionError,
EthMessage, EthVersion,
EthMessage, EthMessageID, EthVersion,
};
use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};
use reth_codecs::add_arbitrary_tests;
@ -98,6 +98,12 @@ impl Capability {
pub fn is_eth_v68(&self) -> bool {
self.name == "eth" && self.version == 68
}
/// Whether this is any eth version.
#[inline]
pub fn is_eth(&self) -> bool {
self.is_eth_v66() || self.is_eth_v67() || self.is_eth_v68()
}
}
impl fmt::Display for Capability {
@ -320,7 +326,7 @@ impl SharedCapability {
/// Returns the number of protocol messages supported by this capability.
pub fn num_messages(&self) -> Result<u8, SharedCapabilityError> {
match self {
SharedCapability::Eth { version, .. } => Ok(version.total_messages()),
SharedCapability::Eth { version: _version, .. } => Ok(EthMessageID::max() + 1),
_ => Err(SharedCapabilityError::UnknownCapability),
}
}
@ -676,7 +682,7 @@ mod tests {
assert_eq!(shared_eth.name(), proto.cap.name);
// the 6th shared message is the first message of the eth capability
let shared_eth = shared.find_by_relative_offset(1 + proto.messages).unwrap();
let shared_eth = shared.find_by_relative_offset(1 + proto.messages()).unwrap();
assert_eq!(shared_eth.name(), "eth");
}
}

View File

@ -1,6 +1,6 @@
//! A Protocol defines a P2P subprotocol in a RLPx connection
use crate::{capability::Capability, EthVersion};
use crate::{capability::Capability, EthMessageID, EthVersion};
/// Type that represents a [Capability] and the number of messages it uses.
///
@ -14,7 +14,7 @@ pub struct Protocol {
/// The number of messages used/reserved by this protocol
///
/// This is used for message ID multiplexing
pub messages: u8,
messages: u8,
}
impl Protocol {
@ -50,6 +50,14 @@ impl Protocol {
pub(crate) fn split(self) -> (Capability, u8) {
(self.cap, self.messages)
}
/// 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
}
self.messages
}
}
impl From<EthVersion> for Protocol {

View File

@ -319,6 +319,13 @@ pub enum EthMessageID {
Receipts = 0x10,
}
impl EthMessageID {
/// Returns the max value.
pub const fn max() -> u8 {
Self::Receipts as u8
}
}
impl Encodable for EthMessageID {
fn encode(&self, out: &mut dyn BufMut) {
out.put_u8(*self as u8);