Skip to content

Commit

Permalink
feat: implement ENRRequestMessage encoding (#137)
Browse files Browse the repository at this point in the history
This PR depends on #132, #136 

**Motivation**
Implement discv4 protocol

**Description**
Enables `ENRRequestMessage` encoding

Closes #88
  • Loading branch information
ricomateo authored Jul 11, 2024
1 parent 93d3656 commit 6e0c299
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/net/src/discv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl Message {
Message::Ping(msg) => msg.encode(buf),
Message::Pong(msg) => msg.encode(buf),
Message::FindNode(msg) => msg.encode(buf),
Message::ENRRequest(msg) => msg.encode(buf),
Message::Neighbors(msg) => msg.encode(buf),
_ => todo!(),
}
Expand Down Expand Up @@ -417,6 +418,14 @@ impl RLPDecode for ENRRequestMessage {
}
}

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

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

#[test]
fn test_encode_enr_request_message() {
let expiration: u64 = 17195043770;
let msg = Message::ENRRequest(ENRRequestMessage { 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 = "ddb4faf81ed7bee047e42088a0efd01650c2191988c08c71dd10635573bee31f";
let signature = "ec86b35edf60470d81e9796bc4fad68c1d187266492662d91f56b7e42ed46b9317444a72172f13aa91af41ca7a4fec49d5619de9abc0be6c79da0d92bc1c9f3201";
let pkt_type = "05";
let encoded_message = "c6850400e78bba";
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 6e0c299

Please sign in to comment.