Skip to content

Commit

Permalink
unify encoding rlp content
Browse files Browse the repository at this point in the history
  • Loading branch information
divagant-martian committed Sep 25, 2023
1 parent 6553de9 commit 76256f1
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,17 +767,29 @@ impl<K: EnrKey> Enr<K> {

// Private Functions //

/// Evaluates the RLP-encoding of the content of the ENR record.
fn rlp_content(&self) -> BytesMut {
let mut stream = RlpStream::new_with_buffer(BytesMut::with_capacity(MAX_ENR_SIZE));
stream.begin_list(self.content.len() * 2 + 1);
/// Encodes the ENR's content (sequence number + ordered (key, value) pairs) into the stream.
fn append_rlp_content(&self, stream: &mut RlpStream, include_signature: bool) {
// signature(optional) + sequence number + (key, value) pairs
let content_pairs = self.content.len() * 2;
let item_count = include_signature.then_some(1).unwrap_or(0) + 1 + content_pairs;
stream.begin_list(item_count);
if include_signature {
stream.append(&self.signature);
}
stream.append(&self.seq);
for (k, v) in &self.content {
// Keys are bytes
stream.append(k);
// Values are raw RLP encoded data
stream.append_raw(v, 1);
}
}

/// Encodes the ENR's content (sequence number + ordered (key, value) pairs).
fn rlp_content(&self) -> BytesMut {
let mut stream = RlpStream::new_with_buffer(BytesMut::with_capacity(MAX_ENR_SIZE));
let include_signature = false;
self.append_rlp_content(&mut stream, include_signature);
stream.out()
}

Expand Down Expand Up @@ -918,17 +930,9 @@ impl<'de, K: EnrKey> Deserialize<'de> for Enr<K> {
}

impl<K: EnrKey> rlp::Encodable for Enr<K> {
fn rlp_append(&self, s: &mut RlpStream) {
s.begin_list(self.content.len() * 2 + 2);
s.append(&self.signature);
s.append(&self.seq);
// must use rlp_content to preserve ordering.
for (k, v) in &self.content {
// Keys are byte data
s.append(k);
// Values are raw RLP encoded data
s.append_raw(v, 1);
}
fn rlp_append(&self, stream: &mut RlpStream) {
let include_signature = true;
self.append_rlp_content(stream, include_signature)
}
}

Expand Down

0 comments on commit 76256f1

Please sign in to comment.