From d7a5e3ef53476a975c150e6abb0890e519faa210 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Fri, 28 Oct 2011 01:28:41 +0000
Subject: [PATCH]   * CapacityCalculator: Small boost for connected peers, new
 peers, and     same-country peers; deduct for recently-unreachable peers

---
 .../src/net/i2p/router/CommSystemFacade.java  |  2 ++
 .../peermanager/CapacityCalculator.java       | 22 ++++++++++++++++++-
 .../i2p/router/peermanager/PeerProfile.java   | 16 ++++++++++++++
 .../transport/CommSystemFacadeImpl.java       |  9 ++++++++
 4 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/router/java/src/net/i2p/router/CommSystemFacade.java b/router/java/src/net/i2p/router/CommSystemFacade.java
index 0b5ebb24be..f9644f388e 100644
--- a/router/java/src/net/i2p/router/CommSystemFacade.java
+++ b/router/java/src/net/i2p/router/CommSystemFacade.java
@@ -61,6 +61,8 @@ public abstract class CommSystemFacade implements Service {
     public boolean isEstablished(Hash dest) { return false; }
     public byte[] getIP(Hash dest) { return null; }
     public void queueLookup(byte[] ip) {}
+    /** @since 0.8.11 */
+    public String getOurCountry() { return null; }
     public String getCountry(Hash peer) { return null; }
     public String getCountryName(String code) { return code; }
     public String renderPeerHTML(Hash peer) {
diff --git a/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java b/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java
index 96570260c5..76a6f1b587 100644
--- a/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java
+++ b/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java
@@ -15,6 +15,13 @@ class CapacityCalculator {
     
     /** the calculator estimates over a 1 hour period */
     private static long ESTIMATE_PERIOD = 60*60*1000;
+
+    // total of all possible bonuses should be less than 4, since
+    // crappy peers start at 1 and the base is 5.
+    private static final double BONUS_NEW = 1.25;
+    private static final double BONUS_ESTABLISHED = 1;
+    private static final double BONUS_SAME_COUNTRY = .85;
+    private static final double PENALTY_UNREACHABLE = 2;
     
     public static double calc(PeerProfile profile) {
         double capacity;
@@ -49,7 +56,20 @@ class CapacityCalculator {
             capacity = 1;
         else if (profile.getTunnelHistory().getLastRejectedProbabalistic() > now - 5*60*1000)
             capacity -= _context.random().nextInt(5);
-        
+
+        // boost new profiles
+        if (now - profile.getFirstHeardAbout() < 45*60*1000)
+            capacity += BONUS_NEW;
+        // boost connected peers
+        if (profile.isEstablished())
+            capacity += BONUS_ESTABLISHED;
+        // boost same country
+        if (profile.isSameCountry())
+            capacity += BONUS_SAME_COUNTRY;
+        // penalize unreachable peers
+        if (profile.wasUnreachable())
+            capacity -= PENALTY_UNREACHABLE;
+
         capacity += profile.getCapacityBonus();
         if (capacity < 0)
             capacity = 0;
diff --git a/router/java/src/net/i2p/router/peermanager/PeerProfile.java b/router/java/src/net/i2p/router/peermanager/PeerProfile.java
index 034f559ae4..e21c7c383f 100644
--- a/router/java/src/net/i2p/router/peermanager/PeerProfile.java
+++ b/router/java/src/net/i2p/router/peermanager/PeerProfile.java
@@ -99,6 +99,22 @@ public class PeerProfile {
         return getIsActive(5*60*1000);
     }
     
+    /** @since 0.8.11 */
+    public boolean isEstablished() {
+        return _context.commSystem().isEstablished(_peer);
+    }
+
+    /** @since 0.8.11 */
+    public boolean wasUnreachable() {
+        return _context.commSystem().wasUnreachable(_peer);
+    }
+
+    /** @since 0.8.11 */
+    public boolean isSameCountry() {
+        String us = _context.commSystem().getOurCountry();
+        return us != null && us.equals(_context.commSystem().getCountry(_peer));
+    }
+
     /**
      * Is this peer active at the moment (sending/receiving messages within the 
      * given period?)
diff --git a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
index dfc2a07df4..2f6a478cd1 100644
--- a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
+++ b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
@@ -447,6 +447,15 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
         _geoIP.add(ip);
     }
 
+    /**
+     *  @return two-letter lower-case country code or null
+     *  @since 0.8.11
+     */
+    @Override
+    public String getOurCountry() {
+        return _context.getProperty(GeoIP.PROP_IP_COUNTRY);
+    }
+
     /**
      *  Uses the transport IP first because that lookup is fast,
      *  then the SSU IP from the netDb.
-- 
GitLab