Repository: paradigmxyz/reth (language: rust)

Issue:
Add encodable+decodable support for rawcapability message
### Describe the feature

we don't have rlp support for this type:
https://github.com/paradigmxyz/reth/blob/86e8a2a245490a1503c3c05c246f63f3d48e0175/crates/net/eth-wire/src/capability.rs#L17-L25

the way this is encoded is:

https://github.com/paradigmxyz/reth/blob/86e8a2a245490a1503c3c05c246f63f3d48e0175/crates/net/eth-wire/src/ethstream.rs#L261-L262

and decoded the inverse so that the payload remains rlp encoded

## TODO 
* impl encodable+decodable for rawcap message

### Additional context

_No response_


Discussion / hints:
@mattsse can you assign me
@mattsse can i get assigned to this? 
eta: 3 days

Fix patch:
diff --git a/crates/net/eth-wire/src/capability.rs b/crates/net/eth-wire/src/capability.rs
index 327292b2d9d6..9eea58b6a662 100644
--- a/crates/net/eth-wire/src/capability.rs
+++ b/crates/net/eth-wire/src/capability.rs
@@ -8,6 +8,7 @@ use crate::{
     Capability, EthMessageID, EthVersion,
 };
 use alloy_primitives::bytes::Bytes;
+use alloy_rlp::{BufMut, Decodable, Encodable};
 use derive_more::{Deref, DerefMut};
 use std::{
     borrow::Cow,
@@ -40,6 +41,30 @@ impl RawCapabilityMessage {
     }
 }
 
+impl Encodable for RawCapabilityMessage {
+    /// Encodes the `RawCapabilityMessage` into an RLP byte stream.
+    fn encode(&self, out: &mut dyn BufMut) {
+        self.id.encode(out);
+        out.put_slice(&self.payload);
+    }
+
+    /// Returns the total length of the encoded message.
+    fn length(&self) -> usize {
+        self.id.length() + self.payload.len()
+    }
+}
+
+impl Decodable for RawCapabilityMessage {
+    /// Decodes a `RawCapabilityMessage` from an RLP byte stream.
+    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
+        let id = usize::decode(buf)?;
+        let payload = Bytes::copy_from_slice(buf);
+        *buf = &buf[buf.len()..];
+
+        Ok(Self { id, payload })
+    }
+}
+
 /// This represents a shared capability, its version, and its message id offset.
 ///
 /// The [offset](SharedCapability::message_id_offset) is the message ID offset for this shared
@@ -383,6 +408,8 @@ pub struct UnsupportedCapabilityError {
 mod tests {
     use super::*;
     use crate::{Capabilities, Capability};
+    use alloy_primitives::bytes::Bytes;
+    use alloy_rlp::{Decodable, Encodable};
 
     #[test]
     fn from_eth_68() {
@@ -542,4 +569,19 @@ mod tests {
         let shared_eth = shared.find_by_relative_offset(1 + proto.messages()).unwrap();
         assert_eq!(shared_eth.name(), "eth");
     }
+
+    #[test]
+    fn test_raw_capability_rlp() {
+        let msg = RawCapabilityMessage { id: 1, payload: Bytes::from(vec![0x01, 0x02, 0x03]) };
+
+        // Encode the message into bytes
+        let mut encoded = Vec::new();
+        msg.encode(&mut encoded);
+
+        // Decode the bytes back into RawCapbailitMessage
+        let decoded = RawCapabilityMessage::decode(&mut &encoded[..]).unwrap();
+
+        // Verify that the decoded message matches the original
+        assert_eq!(msg, decoded);
+    }
 }


Test patch: