From 4c6ac6fd7d5254664a91514deb91117208125c6b Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:15:51 -0600 Subject: [PATCH 1/2] Add compare_content for enr's --- src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 4a21745..45321e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -421,6 +421,12 @@ impl Enr { } } + /// Compare if the content of 2 Enr's match. + #[must_use] + pub fn compare_content(&self, other: &Enr) -> 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 { @@ -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()); From dd3af6ce88ee10f45e2a37ed4a535dc57ff201c9 Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:27:15 -0600 Subject: [PATCH 2/2] fixup lint 1 --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 45321e7..0bb6ad8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -423,7 +423,7 @@ impl Enr { /// Compare if the content of 2 Enr's match. #[must_use] - pub fn compare_content(&self, other: &Enr) -> bool { + pub fn compare_content(&self, other: &Self) -> bool { self.rlp_content() == other.rlp_content() }