Skip to content

Commit

Permalink
Add compare_content function to Enr to allow comparisons modulo s…
Browse files Browse the repository at this point in the history
…ignature (#53)
  • Loading branch information
KolbyML authored Oct 3, 2023
1 parent a7f6639 commit 7ffbb54
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ impl<K: EnrKey> Enr<K> {
}
}

/// Compare if the content of 2 Enr's match.
#[must_use]
pub fn compare_content(&self, other: &Self) -> bool {
self.rlp_content() == other.rlp_content()
}

/// Provides the URL-safe base64 encoded "text" version of the ENR prefixed by "enr:".
#[must_use]
pub fn to_base64(&self) -> String {
Expand Down Expand Up @@ -1703,6 +1709,35 @@ mod tests {
}
}

#[test]
fn test_compare_content() {
let key = k256::ecdsa::SigningKey::random(&mut rand::thread_rng());
let ip = Ipv4Addr::new(10, 0, 0, 1);
let tcp = 30303;

let enr1 = {
let mut builder = EnrBuilder::new("v4");
builder.ip4(ip);
builder.tcp4(tcp);
builder.build(&key).unwrap()
};

let mut enr2 = enr1.clone();
enr2.set_seq(1, &key).unwrap();
let mut enr3 = enr1.clone();
enr3.set_seq(2, &key).unwrap();

// Enr 1 & 2 should be equal, secpk256k1 should have different signatures for the same Enr content
assert_ne!(enr1.signature(), enr2.signature());
assert!(enr1.compare_content(&enr2));
assert_ne!(enr1, enr2);

// Enr 1 & 3 should not be equal, and have different signatures
assert_ne!(enr1.signature(), enr3.signature());
assert!(!enr1.compare_content(&enr3));
assert_ne!(enr1, enr3);
}

fn assert_tcp4(enr: &DefaultEnr, tcp: u16) {
assert!(enr.verify());
assert_eq!(enr.get_raw_rlp("tcp").unwrap(), rlp::encode(&tcp).to_vec());
Expand Down

0 comments on commit 7ffbb54

Please sign in to comment.