From dd5233574b404107f57c6063db07ff4a254c8c05 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Thu, 11 Aug 2022 20:46:47 +0200
Subject: [PATCH] fix #1: add_link without rem_link calls must be idempotent

---
 dubp-wot/src/data/mod.rs   |  2 ++
 dubp-wot/src/data/rusty.rs |  2 ++
 dubp-wot/src/lib.rs        | 10 ++++++++++
 3 files changed, 14 insertions(+)

diff --git a/dubp-wot/src/data/mod.rs b/dubp-wot/src/data/mod.rs
index eddc11e..6f7ec6b 100644
--- a/dubp-wot/src/data/mod.rs
+++ b/dubp-wot/src/data/mod.rs
@@ -92,6 +92,8 @@ pub enum NewLinkResult {
     Ok(usize),
     /// All available certifications has been used.
     AllCertificationsUsed(usize),
+    /// Already existing certification (maybe a replay).
+    AlreadyExistingCertification(usize),
     /// Unknown source.
     UnknownSource(),
     /// Unknown target.
diff --git a/dubp-wot/src/data/rusty.rs b/dubp-wot/src/data/rusty.rs
index 3980273..5367b7c 100644
--- a/dubp-wot/src/data/rusty.rs
+++ b/dubp-wot/src/data/rusty.rs
@@ -138,6 +138,8 @@ impl WebOfTrust for RustyWebOfTrust {
             NewLinkResult::UnknownTarget()
         } else if self.nodes[source.0].issued_count >= self.max_links {
             NewLinkResult::AllCertificationsUsed(self.nodes[target.0].links_source.len())
+        } else if self.nodes[target.0].links_source.contains(&source) {
+            NewLinkResult::AlreadyExistingCertification(self.nodes[target.0].links_source.len())
         } else {
             self.nodes[source.0].issued_count += 1;
             self.nodes[target.0].links_source.insert(source);
diff --git a/dubp-wot/src/lib.rs b/dubp-wot/src/lib.rs
index 06989dc..54dc449 100644
--- a/dubp-wot/src/lib.rs
+++ b/dubp-wot/src/lib.rs
@@ -197,6 +197,16 @@ mod tests {
         // should be able to add some links, cert count is returned
         assert_eq!(wot.add_link(WotId(2), WotId(0)), NewLinkResult::Ok(1));
         assert_eq!(wot.add_link(WotId(4), WotId(0)), NewLinkResult::Ok(2));
+        // Check that certification replay is handled
+        assert_eq!(
+            wot.add_link(WotId(4), WotId(0)),
+            NewLinkResult::AlreadyExistingCertification(2)
+        );
+        // Check that certification re-replay is handled
+        assert_eq!(
+            wot.add_link(WotId(4), WotId(0)),
+            NewLinkResult::AlreadyExistingCertification(2)
+        );
         assert_eq!(wot.add_link(WotId(5), WotId(0)), NewLinkResult::Ok(3));
 
         // should exist new links
-- 
GitLab