From c8b949cef57c0e2c3433a912956758123540eeea Mon Sep 17 00:00:00 2001
From: HelloZeroNet <hello@noloop.me>
Date: Wed, 30 Mar 2016 23:05:43 +0200
Subject: [PATCH] Save last received update time from peer to avoid send the
 same update on publish

---
 src/File/FileRequest.py | 3 ++-
 src/Peer/Peer.py        | 3 ++-
 src/Site/Site.py        | 7 +++++++
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/File/FileRequest.py b/src/File/FileRequest.py
index 08e3600d..63b1074a 100644
--- a/src/File/FileRequest.py
+++ b/src/File/FileRequest.py
@@ -117,6 +117,7 @@ class FileRequest(object):
             else:
                 peer = site.addPeer(self.connection.ip, self.connection.port, return_peer=True)  # Add or get peer
             if peer:
+                peer.last_content_json_update = site.content_manager.contents[params["inner_path"]]["modified"]
                 if config.verbose:
                     self.log.debug(
                         "Same version, adding new peer for locked files: %s, tasks: %s" %
@@ -130,7 +131,7 @@ class FileRequest(object):
             self.response({"ok": "File not changed"})
             self.connection.badAction()
 
-        else:  # Invalid sign or sha1 hash
+        else:  # Invalid sign or sha hash
             self.log.debug("Update for %s is invalid" % params["inner_path"])
             self.response({"error": "File invalid"})
             self.connection.badAction(5)
diff --git a/src/Peer/Peer.py b/src/Peer/Peer.py
index a8ad1948..5d4b061f 100644
--- a/src/Peer/Peer.py
+++ b/src/Peer/Peer.py
@@ -18,7 +18,7 @@ if config.use_tempfiles:
 class Peer(object):
     __slots__ = (
         "ip", "port", "site", "key", "connection", "connection_server", "time_found", "time_response", "time_hashfield", "time_added",
-        "time_my_hashfield_sent", "last_ping", "hashfield", "connection_error", "hash_failed", "download_bytes", "download_time"
+        "time_my_hashfield_sent", "last_ping", "last_content_json_update", "hashfield", "connection_error", "hash_failed", "download_bytes", "download_time"
     )
 
     def __init__(self, ip, port, site=None, connection_server=None):
@@ -36,6 +36,7 @@ class Peer(object):
         self.time_response = None  # Time of last successful response from peer
         self.time_added = time.time()
         self.last_ping = None  # Last response time for ping
+        self.last_content_json_update = 0.0  # Modify date of last received content.json
 
         self.connection_error = 0  # Series of connection error
         self.hash_failed = 0  # Number of bad files from peer
diff --git a/src/Site/Site.py b/src/Site/Site.py
index 49019e83..85b78229 100644
--- a/src/Site/Site.py
+++ b/src/Site/Site.py
@@ -123,6 +123,9 @@ class Site(object):
         self.log.debug("Got %s" % inner_path)
         changed, deleted = self.content_manager.loadContent(inner_path, load_includes=False)
 
+        if peer:  # Update last received update from peer to prevent re-sending the same update to it
+            peer.last_content_json_update = self.content_manager.contents[inner_path]["modified"]
+
         # Start download files
         file_threads = []
         if download_files:
@@ -309,6 +312,7 @@ class Site(object):
     # Publish worker
     def publisher(self, inner_path, peers, published, limit, event_done=None):
         file_size = self.storage.getSize(inner_path)
+        content_json_modified = self.content_manager.contents[inner_path]["modified"]
         body = self.storage.read(inner_path)
 
         # Find out my ip and port
@@ -333,6 +337,9 @@ class Site(object):
             peer = peers.pop(0)
             if peer in published:
                 continue
+            if peer.last_content_json_update == content_json_modified:
+                self.log.debug("%s already received this update for %s, skipping" % (peer, inner_path))
+                continue
 
             if peer.connection and peer.connection.last_ping_delay:  # Peer connected
                 # Timeout: 5sec + size in kb + last_ping