From d4283ac1ec6c29ba30e3730de25d047858a8bbc2 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@allmydata.com>
Date: Wed, 19 Dec 2007 00:06:53 -0700
Subject: [PATCH] mutable: always include ourselves in the querylist when
 Publishing, because 0.7.0 usually stores our root dirnode only on ourselves,
 and our nodeid might not appear in the first N+epsilon peers

---
 src/allmydata/mutable.py           | 27 ++++++++++++++++++++++++++-
 src/allmydata/test/test_mutable.py |  2 ++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/allmydata/mutable.py b/src/allmydata/mutable.py
index 49493255..65fe89b2 100644
--- a/src/allmydata/mutable.py
+++ b/src/allmydata/mutable.py
@@ -825,7 +825,12 @@ class Publish:
         self._encprivkey_shares = []
 
         EPSILON = total_shares / 2
-        partial_peerlist = islice(peerlist, total_shares + EPSILON)
+        #partial_peerlist = islice(peerlist, total_shares + EPSILON)
+        partial_peerlist = peerlist[:total_shares+EPSILON]
+
+        # make sure our local server is in the list
+        partial_peerlist = self._add_ourselves(partial_peerlist, peerlist)
+
         peer_storage_servers = {}
         dl = []
         for (permutedid, peerid, conn) in partial_peerlist:
@@ -840,9 +845,29 @@ class Publish:
                       total_shares, reachable_peers,
                       current_share_peers, peer_storage_servers)
         # TODO: add an errback to, probably to ignore that peer
+        # TODO: if we can't get a privkey from these servers, consider
+        # looking farther afield. Make sure we include ourselves in the
+        # initial list, because of the 0.7.0 behavior that causes us to
+        # create our initial directory before we've connected to anyone
+        # but ourselves.
         return d
 
+    def _add_ourselves(self, partial_peerlist, peerlist):
+        my_peerid = self._node._client.nodeid
+        for (permutedid, peerid, conn) in partial_peerlist:
+            if peerid == my_peerid:
+                # we're already in there
+                return partial_peerlist
+        for (permutedid, peerid, conn) in peerlist:
+            if peerid == self._node._client.nodeid:
+                # found it
+                partial_peerlist.append( (permutedid, peerid, conn) )
+                return partial_peerlist
+        self.log("WEIRD: we aren't in our own peerlist??")
+        return partial_peerlist
+
     def _do_query(self, conn, peerid, peer_storage_servers, storage_index):
+        self.log("querying %s" % idlib.shortnodeid_b2a(peerid))
         d = conn.callRemote("get_service", "storageserver")
         def _got_storageserver(ss):
             peer_storage_servers[peerid] = ss
diff --git a/src/allmydata/test/test_mutable.py b/src/allmydata/test/test_mutable.py
index 43eb1f93..f42cd3f5 100644
--- a/src/allmydata/test/test_mutable.py
+++ b/src/allmydata/test/test_mutable.py
@@ -79,6 +79,8 @@ class FakeClient:
         self._peerids = [tagged_hash("peerid", "%d" % i)[:20]
                          for i in range(self._num_peers)]
         self.introducer_client = FakeIntroducerClient()
+        self.nodeid = "fakenodeid"
+
     def log(self, msg, **kw):
         return log.msg(msg, **kw)
 
-- 
2.45.2