From 0ab429e1ed9c6797ee38aff83a95095a9152c5a5 Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Tue, 23 Mar 2021 08:17:38 -0400
Subject: [PATCH] NetDB: Sort published addresses for consistency

---
 .../transport/CommSystemFacadeImpl.java       | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
index cdcd28837c..61d988db37 100644
--- a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
+++ b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
@@ -9,9 +9,11 @@ package net.i2p.router.transport;
  */
 
 import java.io.IOException;
+import java.io.Serializable;
 import java.io.Writer;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Set;
@@ -268,10 +270,32 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
         //if (_context.router().isHidden())
         //    return Collections.EMPTY_SET;
         List<RouterAddress> addresses = new ArrayList<RouterAddress>(_manager.getAddresses());
+        if (addresses.size() > 1)
+            Collections.sort(addresses, new AddrComparator());
         if (_log.shouldLog(Log.INFO))
             _log.info("Creating addresses: " + addresses, new Exception("creator"));
         return addresses;
     }
+
+    /**
+     *  Arbitrary sort for consistency.
+     *  Note that the console UI has its own sorter.
+     *  @since 0.9.50
+     */
+    private static class AddrComparator implements Comparator<RouterAddress>, Serializable {
+        public int compare(RouterAddress l, RouterAddress r) {
+            int rv = l.getCost() - r.getCost();
+            if (rv != 0)
+                return rv;
+            int lh = l.hashCode();
+            int rh = l.hashCode();
+            if (lh > rh)
+                return 1;
+            if (lh < rh)
+                return -1;
+            return 0;
+        }
+    }
     
     /**
      * UDP changed addresses, tell NTCP and restart
-- 
GitLab