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
31 changes: 29 additions & 2 deletions crates/net/src/discv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) enum Message {
Pong(PongMessage),
FindNode(FindNodeMessage),
Neighbors(NeighborsMessage),
ENRRequest(()),
ENRRequest(ENRRequestMessage),
ENRResponse(()),
}

Expand Down Expand Up @@ -116,7 +116,10 @@ impl Message {
let (neighbors_msg, _rest) = NeighborsMessage::decode_unfinished(msg)?;
Ok(Message::Neighbors(neighbors_msg))
}
0x05 => todo!(),
0x05 => {
let (enr_request_msg, _rest) = ENRRequestMessage::decode_unfinished(msg)?;
Ok(Message::ENRRequest(enr_request_msg))
}
0x06 => todo!(),
_ => Err(RLPDecodeError::MalformedData),
}
Expand Down Expand Up @@ -399,6 +402,21 @@ impl RLPDecode for Node {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ENRRequestMessage {
expiration: u64,
}

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

impl RLPEncode for Node {
fn encode(&self, buf: &mut dyn BufMut) {
structs::Encoder::new(buf)
Expand Down Expand Up @@ -735,6 +753,15 @@ mod tests {
assert_eq!(decoded, expected);
}

#[test]
fn test_decode_enr_request_message() {
let encoded = "c6850400e78bba";
let decoded = Message::decode_with_type(0x05, &decode_hex(encoded).unwrap()).unwrap();
let expiration = 0x400E78BBA;
let expected = Message::ENRRequest(ENRRequestMessage { expiration });
assert_eq!(decoded, expected);
}

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