Skip to content

Commit 062f6ae

Browse files
authored
feat: implement FindNodeMessage decoding (#114)
**Motivation** Have some functionality of the discv4 protocol **Description** Enables RLP decoding for `FindNodeMessage` Closes #83
1 parent b3a16bf commit 062f6ae

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

crates/net/src/discv4.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ impl Message {
8181
let (pong, _rest) = PongMessage::decode_unfinished(msg)?;
8282
Ok(Message::Pong(pong))
8383
}
84-
_ => todo!(),
84+
0x03 => {
85+
let (find_node_msg, _rest) = FindNodeMessage::decode_unfinished(msg)?;
86+
Ok(Message::FindNode(find_node_msg))
87+
}
88+
0x04 => todo!(),
89+
0x05 => todo!(),
90+
0x06 => todo!(),
91+
_ => Err(RLPDecodeError::MalformedData),
8592
}
8693
}
8794

@@ -203,6 +210,17 @@ impl RLPEncode for FindNodeMessage {
203210
}
204211
}
205212

213+
impl RLPDecode for FindNodeMessage {
214+
fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> {
215+
let decoder = Decoder::new(rlp)?;
216+
let (target, decoder) = decoder.decode_field("target")?;
217+
let (expiration, decoder) = decoder.decode_field("expiration")?;
218+
let remaining = decoder.finish_unchecked();
219+
let msg = FindNodeMessage { target, expiration };
220+
Ok((msg, remaining))
221+
}
222+
}
223+
206224
impl RLPDecode for PingMessage {
207225
fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> {
208226
let decoder = Decoder::new(rlp)?;
@@ -543,6 +561,24 @@ mod tests {
543561
assert_eq!(result, msg);
544562
}
545563

564+
#[test]
565+
fn test_decode_find_node_message() {
566+
let target: H512 = H512::from_str("d860a01f9722d78051619d1e2351aba3f43f943f6f00718d1b9baa4101932a1f5011f16bb2b1bb35db20d6fe28fa0bf09636d26a87d31de9ec6203eeedb1f666").unwrap();
567+
let expiration: u64 = 17195043770;
568+
let msg = Message::FindNode(FindNodeMessage::new(target, expiration));
569+
570+
let key_bytes =
571+
H256::from_str("577d8278cc7748fad214b5378669b420f8221afb45ce930b7f22da49cbc545f3")
572+
.unwrap();
573+
let signer = SigningKey::from_slice(key_bytes.as_bytes()).unwrap();
574+
575+
let mut buf = Vec::new();
576+
577+
msg.encode_with_header(&mut buf, signer);
578+
let result = Message::decode_with_header(&buf).unwrap();
579+
assert_eq!(result, msg);
580+
}
581+
546582
#[test]
547583
fn test_decode_endpoint() {
548584
let endpoint = Endpoint {

0 commit comments

Comments
 (0)