Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion crates/net/src/discv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ impl Message {
let (pong, _rest) = PongMessage::decode_unfinished(msg)?;
Ok(Message::Pong(pong))
}
_ => todo!(),
0x03 => {
let (find_node_msg, _rest) = FindNodeMessage::decode_unfinished(msg)?;
Ok(Message::FindNode(find_node_msg))
}
0x04 => todo!(),
0x05 => todo!(),
0x06 => todo!(),
_ => Err(RLPDecodeError::MalformedData),
}
}

Expand Down Expand Up @@ -203,6 +210,17 @@ impl RLPEncode for FindNodeMessage {
}
}

impl RLPDecode for FindNodeMessage {
fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
let (target, decoder) = decoder.decode_field("target")?;
let (expiration, decoder) = decoder.decode_field("expiration")?;
let remaining = decoder.finish_unchecked();
let msg = FindNodeMessage { target, expiration };
Ok((msg, remaining))
}
}

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

#[test]
fn test_decode_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 = Message::decode_with_header(&buf).unwrap();
assert_eq!(result, msg);
}

#[test]
fn test_decode_endpoint() {
let endpoint = Endpoint {
Expand Down