From 8687791c838050406871439f1ff832a9b56a6e24 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

---
 Cargo.lock                 |  4 +++-
 dubp-wot/Cargo.toml        |  2 +-
 dubp-wot/src/data/mod.rs   |  2 ++
 dubp-wot/src/data/rusty.rs |  2 ++
 dubp-wot/src/lib.rs        | 10 ++++++++++
 5 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 7cff9c8..6abf32c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,5 +1,7 @@
 # This file is automatically @generated by Cargo.
 # It is not intended for manual editing.
+version = 3
+
 [[package]]
 name = "aho-corasick"
 version = "0.7.15"
@@ -773,7 +775,7 @@ dependencies = [
 
 [[package]]
 name = "dubp-wot"
-version = "0.11.0"
+version = "0.11.1"
 dependencies = [
  "bincode",
  "log",
diff --git a/dubp-wot/Cargo.toml b/dubp-wot/Cargo.toml
index 0821f2f..14ac5b9 100644
--- a/dubp-wot/Cargo.toml
+++ b/dubp-wot/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "dubp-wot"
-version = "0.11.0"
+version = "0.11.1"
 authors = ["nanocryk <nanocryk@duniter.org>", "elois <elois@duniter.org>"]
 description = "Makes Web of Trust computations for the Duniter project."
 repository = "https://git.duniter.org/nodes/typescript/duniter"
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