Skip to content

Commit

Permalink
Revert to raw encoding for non-list entries (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgeManning authored Jun 16, 2024
1 parent 12b3bc7 commit 4e56f1c
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,12 +1041,19 @@ impl<K: EnrKey> Decodable for Enr<K> {
_ => {
let other_header = Header::decode(payload)?;
let value = &payload[..other_header.payload_length];
// Preserve the valid encoding
payload.advance(other_header.payload_length);
let mut out = Vec::<u8>::new();
other_header.encode(&mut out);
out.extend_from_slice(value);
out

// Encode the header for list values, for non-list objects, we remove the
// header for compatibility with commonly used key entries (i.e it's the
// current convention).
if other_header.list {
let mut out = Vec::<u8>::new();
other_header.encode(&mut out);
out.extend_from_slice(value);
out
} else {
alloy_rlp::encode(value)
}
}
};
content.insert(key.to_vec(), Bytes::from(value));
Expand Down Expand Up @@ -1961,4 +1968,17 @@ mod tests {
record.set_seq(30, &key).unwrap();
assert_eq!(record.seq(), 30);
}

/// Tests a common ENR which uses RLP encoded values without the header
#[test]
fn test_common_rlp_convention() {
const COMMON_VALID_ENR: &str = concat!(
"-LW4QCAyOCtqvQjd8AgpqbaCgfjy8oN8cBBRT5jtzarkGJQWZx1eN70EM0QafVCugLa-Bv493DPNzflagqfTOsWSF78Ih2F0d",
"G5ldHOIAGAAAAAAAACEZXRoMpBqlaGpBAAAAP__________gmlkgnY0hHF1aWOCIymJc2VjcDI1NmsxoQPg_HgqXzwRIK39Oy",
"lGdC30YUFwsfXvATnGUvEZ6MtBQIhzeW5jbmV0cwCDdGNwgiMo"
);

// Expect this to be able to be decoded
let _decoded: DefaultEnr = COMMON_VALID_ENR.parse().unwrap();
}
}

0 comments on commit 4e56f1c

Please sign in to comment.