Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compare_content function to Enr to allow comparisons modulo signature #53

Merged
merged 2 commits into from
Oct 3, 2023
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
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
Loading