Skip to content

Commit

Permalink
feat: implement FindNode message encoding (#109)
Browse files Browse the repository at this point in the history
**Motivation**

Having some functionality of the discv4 protocol

**Description**

Enables `FindNode` message encoding

Closes #82
  • Loading branch information
ricomateo authored Jul 2, 2024
1 parent a7d9903 commit b776a10
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions crates/net/src/discv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ethrex_core::rlp::{
error::RLPDecodeError,
structs::{self, Decoder, Encoder},
};
use ethrex_core::H256;
use ethrex_core::{H256, H512};
use k256::ecdsa::{signature::Signer, SigningKey};
use std::net::IpAddr;

Expand All @@ -18,7 +18,7 @@ pub(crate) enum Message {
/// A ping message. Should be responded to with a Pong message.
Ping(PingMessage),
Pong(PongMessage),
FindNode(()),
FindNode(FindNodeMessage),
Neighbors(()),
ENRRequest(()),
ENRResponse(()),
Expand All @@ -33,6 +33,7 @@ impl Message {
match self {
Message::Ping(msg) => msg.encode(&mut data),
Message::Pong(msg) => msg.encode(&mut data),
Message::FindNode(msg) => msg.encode(&mut data),
_ => todo!(),
}

Expand Down Expand Up @@ -161,6 +162,31 @@ impl RLPEncode for PingMessage {
}
}

#[derive(Debug, PartialEq, Eq)]
pub(crate) struct FindNodeMessage {
/// The target is a 64-byte secp256k1 public key.
target: H512,
/// The expiration time of the message. If the message is older than this time,
/// it shouldn't be responded to.
expiration: u64,
}

impl FindNodeMessage {
#[allow(unused)]
pub fn new(target: H512, expiration: u64) -> Self {
Self { target, expiration }
}
}

impl RLPEncode for FindNodeMessage {
fn encode(&self, buf: &mut dyn BufMut) {
structs::Encoder::new(buf)
.encode_field(&self.target)
.encode_field(&self.expiration)
.finish();
}
}

impl RLPDecode for PingMessage {
fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
Expand Down Expand Up @@ -358,6 +384,30 @@ mod tests {
assert_eq!(result, expected);
}

#[test]
fn test_encode_find_node_message() {
let target: H512 = H512::from_str("d860a01f9722d78051619d1e2351aba3f43f943f6f00718d1b9baa4101932a1f5011f16bb2b1bb35db20d6fe28fa0bf09636d26a87d31de9ec6203eeedb1f666").unwrap();
let expiration: u64 = 17195043770;

let msg = Message::FindNode(FindNodeMessage::new(target, expiration));

let key_bytes =
H256::from_str("577d8278cc7748fad214b5378669b420f8221afb45ce930b7f22da49cbc545f3")
.unwrap();
let signer = SigningKey::from_slice(key_bytes.as_bytes()).unwrap();

let mut buf = Vec::new();

msg.encode_with_header(&mut buf, signer);
let result = to_hex(&buf);
let hash = "dabe1b1a4dc26324120594091bb94dbdd4aa98326cbfecb60a4a67778ebb0e67";
let signature = "28cf71e9a929fa29f4e528f5fdc96e25a3086a777c8967710ddc1ef5f456bae40808baf0840d0449ea5a17ba11bbb012719e679cf3cf8d137106884c393ef15b00";
let pkt_type = "03";
let encoded_message = "f848b840d860a01f9722d78051619d1e2351aba3f43f943f6f00718d1b9baa4101932a1f5011f16bb2b1bb35db20d6fe28fa0bf09636d26a87d31de9ec6203eeedb1f666850400e78bba";
let expected = [hash, signature, pkt_type, encoded_message].concat();
assert_eq!(result, expected);
}

#[test]
fn test_decode_pong_message_with_enr_seq() {
let hash = "2e1fc2a02ad95a1742f6dd41fb7cbff1e08548ba87f63a72221e44026ab1c347";
Expand Down

0 comments on commit b776a10

Please sign in to comment.