print stat
[oweals/gnunet.git] / src / dht / gnunet-service-dht_neighbours.c
index 91caed95c70d8ea2b231f63522054e9678abf352..e1491a263d96742cabd9335ba7398969ec78c22e 100644 (file)
@@ -38,7 +38,6 @@
 #include "gnunet_hello_lib.h"
 #include "gnunet_dht_service.h"
 #include "gnunet_statistics_service.h"
-#include "dht.h"
 #include "gnunet-service-dht.h"
 #include "gnunet-service-dht_clients.h"
 #include "gnunet-service-dht_datacache.h"
@@ -47,6 +46,7 @@
 #include "gnunet-service-dht_nse.h"
 #include "gnunet-service-dht_routing.h"
 #include <fenv.h>
+#include "dht.h"
 
 /**
  * How many buckets will we allow total.
 /**
  * What is the maximum number of peers in a given bucket.
  */
-#define DEFAULT_BUCKET_SIZE 4
-
-/**
- * Size of the bloom filter the DHT uses to filter peers.
- */
-#define DHT_BLOOM_SIZE 128
+#define DEFAULT_BUCKET_SIZE 8
 
 /**
  * Desired replication level for FIND PEER requests
 /**
  * How long at least to wait before sending another find peer request.
  */
-#define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
+#define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
 
 /**
  * How long at most to wait before sending another find peer request.
  */
-#define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
+#define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10)
 
 /**
  * How long at most to wait for transmission of a GET request to another peer?
@@ -417,6 +412,7 @@ static struct GNUNET_PeerIdentity my_identity;
 static struct GNUNET_CORE_Handle *coreAPI;
 
 
+
 /**
  * Find the optimal bucket for this key.
  *
@@ -503,11 +499,14 @@ update_core_preference (void *cls,
                                      &peer->id.hashPubKey);
   if (matching >= 64)
     matching = 63;
-  bucket = find_bucket(&peer->id.hashPubKey);
+  bucket = find_bucket (&peer->id.hashPubKey);
   if (bucket == GNUNET_SYSERR)
     preference = 0;
   else
+  {
+    GNUNET_assert (k_buckets[bucket].peers_size != 0);
     preference = (1LL << matching) / k_buckets[bucket].peers_size;
+  }
   if (preference == 0)
     {
       peer->preference_task
@@ -560,6 +559,10 @@ add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
   GNUNET_HashCode mh;
 
   GNUNET_BLOCK_mingle_hash (key, ctx->bf_mutator, &mh);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Adding known peer (%s) to bloomfilter for FIND PEER with mutation %u\n",
+             GNUNET_h2s (key),
+             ctx->bf_mutator);
   GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
   return GNUNET_YES;
 }
@@ -579,6 +582,7 @@ send_find_peer_message (void *cls,
 {
   struct GNUNET_TIME_Relative next_send_time;
   struct BloomConstructorContext bcc;
+  struct GNUNET_CONTAINER_BloomFilter *peer_bf;
 
   find_peer_task = GNUNET_SCHEDULER_NO_TASK;
   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
@@ -593,13 +597,16 @@ send_find_peer_message (void *cls,
   }
   bcc.bf_mutator = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
   bcc.bloom =
-    GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
+    GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, GNUNET_CONSTANTS_BLOOMFILTER_K);
   GNUNET_CONTAINER_multihashmap_iterate (all_known_peers, 
                                         &add_known_to_bloom,
                                          &bcc);
   GNUNET_STATISTICS_update (GDS_stats,
                            gettext_noop ("# FIND PEER messages initiated"), 1,
                            GNUNET_NO);
+  peer_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
+                                              DHT_BLOOM_SIZE,
+                                              GNUNET_CONSTANTS_BLOOMFILTER_K);
   // FIXME: pass priority!?
   GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
                             GNUNET_DHT_RO_FIND_PEER,
@@ -607,14 +614,16 @@ send_find_peer_message (void *cls,
                             0,
                             &my_identity.hashPubKey,
                             NULL, 0,
-                            bcc.bloom, bcc.bf_mutator, NULL);
+                            bcc.bloom, bcc.bf_mutator, 
+                            peer_bf);
+  GNUNET_CONTAINER_bloomfilter_free (peer_bf);
   GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
   /* schedule next round */
-  newly_found_peers = 0;
   next_send_time.rel_value =
-    (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
-    GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
-                             DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2);
+    DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
+    GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
+                             DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / (newly_found_peers+1));
+  newly_found_peers = 0;
   find_peer_task = GNUNET_SCHEDULER_add_delayed (next_send_time, 
                                                 &send_find_peer_message,
                                                 NULL);  
@@ -638,6 +647,10 @@ handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
   /* Check for connect to self message */
   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
     return;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Connected %s to %s\n",
+             GNUNET_i2s (&my_identity),
+             GNUNET_h2s (&peer->hashPubKey));
   if (GNUNET_YES ==
       GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
                                               &peer->hashPubKey))
@@ -663,8 +676,10 @@ handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
                               peer_bucket);
   if ( (peer_bucket > 0) &&
        (k_buckets[peer_bucket].peers_size <= bucket_size) )
+  {
     ret->preference_task = GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
-  newly_found_peers++;
+    newly_found_peers++;
+  }
   GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONTAINER_multihashmap_put (all_known_peers, 
                                                    &peer->hashPubKey, ret,
@@ -690,10 +705,15 @@ handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
   struct PeerInfo *to_remove;
   int current_bucket;
   struct P2PPendingMessage *pos;
+  unsigned int discarded;
 
   /* Check for disconnect from self message */
   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
     return;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Disconnected %s from %s\n",
+             GNUNET_i2s (&my_identity),
+             GNUNET_h2s (&peer->hashPubKey));
   to_remove =
       GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
   if (NULL == to_remove)
@@ -713,6 +733,11 @@ handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
     GNUNET_CORE_peer_change_preference_cancel (to_remove->info_ctx);
     to_remove->info_ctx = NULL;
   }
+  if (GNUNET_SCHEDULER_NO_TASK != to_remove->preference_task)
+  {
+    GNUNET_SCHEDULER_cancel (to_remove->preference_task);
+    to_remove->preference_task = GNUNET_SCHEDULER_NO_TASK;
+  }
   current_bucket = find_bucket (&to_remove->id.hashPubKey);
   GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head,
                               k_buckets[current_bucket].tail,
@@ -728,13 +753,19 @@ handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
     GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th);
     to_remove->th = NULL;
   }
+  discarded = 0;
   while (NULL != (pos = to_remove->head))
   {
     GNUNET_CONTAINER_DLL_remove (to_remove->head,
                                 to_remove->tail,
                                 pos);
+    discarded++;
     GNUNET_free (pos);
   }
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# Queued messages discarded (peer disconnected)"), discarded,
+                           GNUNET_NO);
+  GNUNET_free (to_remove);
 }
 
 
@@ -757,16 +788,29 @@ core_transmit_notify (void *cls, size_t size, void *buf)
   size_t msize;
 
   peer->th = NULL;
-  if (buf == NULL)
+  while ( (NULL != (pending = peer->head)) &&
+         (GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value == 0) )
   {
-    /* client disconnected */
-    return 0;
+    peer->pending_count--;
+    GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
+    GNUNET_free (pending);
   }
-  if (peer->head == NULL)
+  if (pending == NULL)
   {
     /* no messages pending */
     return 0;
   }
+  if (buf == NULL)
+  {
+    peer->th 
+      = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
+                                          pending->importance,
+                                          GNUNET_TIME_absolute_get_remaining (pending->timeout),
+                                          &peer->id, ntohs (pending->msg->size),
+                                          &core_transmit_notify, peer);
+    GNUNET_break (NULL != peer->th);
+    return 0;
+  }
   off = 0;
   while ( (NULL != (pending = peer->head)) &&
          (size - off >= (msize = ntohs (pending->msg->size))) )
@@ -781,12 +825,15 @@ core_transmit_notify (void *cls, size_t size, void *buf)
     GNUNET_free (pending);
   }
   if (peer->head != NULL)
+  {
     peer->th 
       = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
-                                           pending->importance,
-                                           GNUNET_TIME_absolute_get_remaining (pending->timeout),
+                                          pending->importance,
+                                          GNUNET_TIME_absolute_get_remaining (pending->timeout),
                                           &peer->id, msize,
-                                           &core_transmit_notify, peer);
+                                          &core_transmit_notify, peer);
+    GNUNET_break (NULL != peer->th);
+  }
   return off;
 }
 
@@ -801,10 +848,14 @@ process_peer_queue (struct PeerInfo *peer)
 {
   struct P2PPendingMessage *pending;
 
-  if (NULL != (pending = peer->head))
+  if (NULL == (pending = peer->head))
     return;
   if (NULL != peer->th)
     return;
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# Bytes of bandwdith requested from core"),
+                           ntohs (pending->msg->size),
+                           GNUNET_NO);
   peer->th 
     = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
                                         pending->importance,
@@ -812,6 +863,7 @@ process_peer_queue (struct PeerInfo *peer)
                                         &peer->id,
                                         ntohs (pending->msg->size),
                                         &core_transmit_notify, peer);
+  GNUNET_break (NULL != peer->th);
 }
 
 
@@ -831,12 +883,12 @@ get_forward_count (uint32_t hop_count,
   uint32_t forward_count;
   float target_value;
 
-  if (hop_count > GDS_NSE_get () * 4.0)
+  if (hop_count > GDS_NSE_get () * 6.0)
   {
     /* forcefully terminate */
     return 0;
   }
-  if (hop_count > GDS_NSE_get () * 2.0)
+  if (hop_count > GDS_NSE_get () * 4.0)
   {
     /* Once we have reached our ideal number of hops, only forward to 1 peer */
     return 1;
@@ -853,7 +905,7 @@ get_forward_count (uint32_t hop_count,
   /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
   target_value = target_value - forward_count;
   random_value =
-    GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX); 
+    GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); 
   if (random_value < (target_value * UINT32_MAX))
     forward_count++;
   return forward_count;
@@ -1001,15 +1053,16 @@ select_peer (const GNUNET_HashCode *key,
     /* greedy selection (closest peer that is not in bloomfilter) */
     smallest_distance = UINT_MAX;
     chosen = NULL;
-    for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
+    for (bc = 0; bc < closest_bucket; bc++)
     {
       pos = k_buckets[bc].head;
       count = 0;
       while ((pos != NULL) && (count < bucket_size))
       {
-        if (GNUNET_NO ==
-            GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
-        {
+        if ( (bloom == NULL) ||
+            (GNUNET_NO ==
+             GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
+       {
           dist = get_distance (key, &pos->id.hashPubKey);
           if (dist < smallest_distance)
           {
@@ -1017,10 +1070,24 @@ select_peer (const GNUNET_HashCode *key,
             smallest_distance = dist;
           }
         }
+        else
+       {
+         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                     "Excluded peer `%s' due to BF match in greedy routing for %s\n",
+                     GNUNET_i2s (&pos->id),
+                     GNUNET_h2s (key));
+         GNUNET_STATISTICS_update (GDS_stats,
+                                   gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1,
+                                   GNUNET_NO);
+       }
         count++;
         pos = pos->next;
       }
     }
+    if (NULL == chosen)
+      GNUNET_STATISTICS_update (GDS_stats,
+                               gettext_noop ("# Peer selection failed"), 1,
+                               GNUNET_NO);
     return chosen;
   }
 
@@ -1032,9 +1099,17 @@ select_peer (const GNUNET_HashCode *key,
     pos = k_buckets[bc].head;
     while ((pos != NULL) && (count < bucket_size))
     {
-      if (GNUNET_YES ==
-          GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+      if ( (bloom != NULL) &&
+          (GNUNET_YES ==
+           GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
       {
+       GNUNET_STATISTICS_update (GDS_stats,
+                                 gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1,
+                                 GNUNET_NO);
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                   "Excluded peer `%s' due to BF match in random routing for %s\n",
+                   GNUNET_i2s (&pos->id),
+                   GNUNET_h2s (key));
         pos = pos->next;
         continue;               /* Ignore bloomfiltered peers */
       }
@@ -1044,6 +1119,9 @@ select_peer (const GNUNET_HashCode *key,
   }
   if (count == 0)               /* No peers to select from! */
   {
+    GNUNET_STATISTICS_update (GDS_stats,
+                             gettext_noop ("# Peer selection failed"), 1,
+                             GNUNET_NO);
     return NULL;
   }
   /* Now actually choose a peer */
@@ -1054,8 +1132,9 @@ select_peer (const GNUNET_HashCode *key,
     pos = k_buckets[bc].head;
     while ((pos != NULL) && (count < bucket_size))
     {
-      if (GNUNET_YES ==
-          GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+      if ( (bloom != NULL) &&
+          (GNUNET_YES ==
+           GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
       {
         pos = pos->next;
         continue;               /* Ignore bloomfiltered peers */
@@ -1095,6 +1174,7 @@ get_target_peers (const GNUNET_HashCode *key,
   struct PeerInfo **rtargets;
   struct PeerInfo *nxt;
 
+  GNUNET_assert (NULL != bloom);
   ret = get_forward_count (hop_count, target_replication);
   if (ret == 0)
   {
@@ -1102,15 +1182,23 @@ get_target_peers (const GNUNET_HashCode *key,
     return 0;
   }
   rtargets = GNUNET_malloc (sizeof (struct PeerInfo*) * ret);
-  off = 0;
-  while (ret-- > 0)
+  for (off = 0; off < ret; off++)
   {
     nxt = select_peer (key, bloom, hop_count);
     if (nxt == NULL)
-      break;
-    rtargets[off++] = nxt;
-    GNUNET_CONTAINER_bloomfilter_add (bloom, &nxt->id.hashPubKey);
+      break;      
+    rtargets[off] = nxt;
+    GNUNET_break (GNUNET_NO ==
+                 GNUNET_CONTAINER_bloomfilter_test (bloom, &nxt->id.hashPubKey));
+    GNUNET_CONTAINER_bloomfilter_add (bloom, &rtargets[off]->id.hashPubKey);
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Selected %u/%u peers at hop %u for %s (target was %u)\n",
+             off,
+             GNUNET_CONTAINER_multihashmap_size (all_known_peers),
+             (unsigned int) hop_count,
+             GNUNET_h2s (key),
+             ret);
   if (0 == off)
   {
     GNUNET_free (rtargets);
@@ -1163,6 +1251,12 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
   struct PeerPutMessage *ppm;
   struct GNUNET_PeerIdentity *pp;
   
+  GNUNET_assert (NULL != bf);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Adding myself (%s) to PUT bloomfilter for %s\n",
+             GNUNET_i2s (&my_identity),
+             GNUNET_h2s (key));
+  GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity.hashPubKey);
   GNUNET_STATISTICS_update (GDS_stats,
                            gettext_noop ("# PUT requests routed"), 1,
                            GNUNET_NO);
@@ -1170,7 +1264,14 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
                                   desired_replication_level,
                                   &targets);
   if (0 == target_count)
-    return;
+    { 
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Routing PUT for %s terminates after %u hops at %s\n",
+                 GNUNET_h2s (key),
+                 (unsigned int) hop_count,
+                 GNUNET_i2s (&my_identity));
+      return;
+    }
   msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size + sizeof (struct PeerPutMessage);
   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
   {
@@ -1183,11 +1284,16 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
     return;
   }
   GNUNET_STATISTICS_update (GDS_stats,
-                           gettext_noop ("# Peers selected as targets for PUT requests"), target_count,
+                           gettext_noop ("# PUT messages queued for transmission"), target_count,
                            GNUNET_NO);
   for (i=0;i<target_count;i++)
   {
     target = targets[i];
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Routing PUT for %s after %u hops to %s\n",
+               GNUNET_h2s (key),
+               (unsigned int) hop_count,
+               GNUNET_i2s (&target->id));
     pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
     pending->importance = 0; /* FIXME */
     pending->timeout = expiration_time;   
@@ -1201,6 +1307,7 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
     ppm->desired_replication_level = htonl (desired_replication_level);
     ppm->put_path_length = htonl (put_path_length);
     ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
+    GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &target->id.hashPubKey));
     GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
                                                              ppm->bloomfilter,
@@ -1257,15 +1364,28 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
   struct PeerGetMessage *pgm;
   char *xq;
   size_t reply_bf_size;
-  
+
+  GNUNET_assert (NULL != peer_bf);  
   GNUNET_STATISTICS_update (GDS_stats,
                            gettext_noop ("# GET requests routed"), 1,
                            GNUNET_NO);
   target_count = get_target_peers (key, peer_bf, hop_count,
                                   desired_replication_level,
                                   &targets);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Adding myself (%s) to GET bloomfilter for %s\n",
+             GNUNET_i2s (&my_identity),
+             GNUNET_h2s (key));
+  GNUNET_CONTAINER_bloomfilter_add (peer_bf, &my_identity.hashPubKey);
   if (0 == target_count)
-    return;
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Routing GET for %s terminates after %u hops at %s\n",
+                 GNUNET_h2s (key),
+                 (unsigned int) hop_count,
+                 GNUNET_i2s (&my_identity));
+      return;
+    }
   reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
   msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
@@ -1274,12 +1394,17 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
     return;
   }
   GNUNET_STATISTICS_update (GDS_stats,
-                           gettext_noop ("# Peers selected as targets for GET requests"), target_count,
+                           gettext_noop ("# GET messages queued for transmission"), target_count,
                            GNUNET_NO);
   /* forward request */
   for (i=0;i<target_count;i++)
   {
     target = targets[i];
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Routing GET for %s after %u hops to %s\n",
+               GNUNET_h2s (key),
+               (unsigned int) hop_count,
+               GNUNET_i2s (&target->id));
     pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); 
     pending->importance = 0; /* FIXME */
     pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
@@ -1293,6 +1418,7 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
     pgm->desired_replication_level = htonl (desired_replication_level);
     pgm->xquery_size = htonl (xquery_size);
     pgm->bf_mutator = reply_bf_mutator; 
+    GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (peer_bf, &target->id.hashPubKey));
     GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
                                                              pgm->bloomfilter,
@@ -1300,10 +1426,11 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
     pgm->key = *key;
     xq = (char *) &pgm[1];
     memcpy (xq, xquery, xquery_size);
-    GNUNET_assert (GNUNET_OK ==
-                  GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
-                                                             &xq[xquery_size],
-                                                             reply_bf_size));
+    if (NULL != reply_bf)
+      GNUNET_assert (GNUNET_OK ==
+                    GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
+                                                               &xq[xquery_size],
+                                                               reply_bf_size));
     GNUNET_CONTAINER_DLL_insert_tail (target->head,
                                      target->tail,
                                      pending);
@@ -1366,7 +1493,7 @@ GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
     return;
   }
   GNUNET_STATISTICS_update (GDS_stats,
-                           gettext_noop ("# REPLIES routed"), 1,
+                           gettext_noop ("# RESULT messages queued for transmission"), 1,
                            GNUNET_NO);
   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); 
   pending->importance = 0; /* FIXME */
@@ -1481,9 +1608,14 @@ handle_dht_p2p_put (void *cls,
     /* cannot verify, good luck */
     break;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "PUT for %s at %s\n",
+             GNUNET_h2s (&put->key),
+             GNUNET_i2s (&my_identity));
   bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter,
                                          DHT_BLOOM_SIZE,
-                                         DHT_BLOOM_K);
+                                         GNUNET_CONSTANTS_BLOOMFILTER_K);
+  GNUNET_break_op (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &peer->hashPubKey));
   {
     struct GNUNET_PeerIdentity pp[putlen+1];
   
@@ -1521,7 +1653,7 @@ handle_dht_p2p_put (void *cls,
                               options,
                               ntohl (put->desired_replication_level),
                               GNUNET_TIME_absolute_ntoh (put->expiration_time),
-                              ntohl (put->hop_count) + 1 /* who adds +1? */,
+                              ntohl (put->hop_count),
                               bf,
                               &put->key,
                               putlen, pp,
@@ -1559,8 +1691,9 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender,
   if (NULL != GDS_my_hello)
   {
     GNUNET_BLOCK_mingle_hash (&my_identity.hashPubKey, bf_mutator, &mhash);
-    if (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash))
-      
+    if ( (NULL == bf) ||
+        (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) )
+    {
       GDS_NEIGHBOURS_handle_reply (sender,
                                   GNUNET_BLOCK_TYPE_DHT_HELLO,
                                   GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
@@ -1569,22 +1702,39 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender,
                                   0, NULL,
                                   GDS_my_hello,
                                   GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*) GDS_my_hello));
+    }
+    else
+    {
+      GNUNET_STATISTICS_update (GDS_stats,
+                               gettext_noop ("# FIND PEER requests ignored due to Bloomfilter"), 1,
+                               GNUNET_NO);
+    }
+  }
+  else
+  {
+    GNUNET_STATISTICS_update (GDS_stats,
+                             gettext_noop ("# FIND PEER requests ignored due to lack of HELLO"), 1,
+                             GNUNET_NO);
   }
 
   /* then, also consider sending a random HELLO from the closest bucket */
-  bucket_idx = find_bucket (key);
+  if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
+    bucket_idx = closest_bucket;
+  else
+    bucket_idx = GNUNET_MIN (closest_bucket, find_bucket (key));
   if (bucket_idx == GNUNET_SYSERR)
     return;
   bucket = &k_buckets[bucket_idx];
   if (bucket->peers_size == 0)
     return;
-  choice = GNUNET_CRYPTO_random_u32 (bucket->peers_size,
-                                    GNUNET_CRYPTO_QUALITY_WEAK);
+  choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                    bucket->peers_size);
   peer = bucket->head;
   while (choice > 0)
   {
     GNUNET_assert (peer != NULL);
     peer = peer->next;
+    choice--;
   }
   choice = bucket->peers_size;
   do
@@ -1637,7 +1787,8 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
   struct GNUNET_CONTAINER_BloomFilter *reply_bf;
   struct GNUNET_CONTAINER_BloomFilter *peer_bf;
   const char *xquery;
-                      
+
+  GNUNET_break (0 != memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)));
   /* parse and validate message */
   msize = ntohs (message->size);
   if (msize < sizeof (struct PeerGetMessage))
@@ -1663,7 +1814,7 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
   if (reply_bf_size > 0)
     reply_bf = GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size],
                                                  reply_bf_size,
-                                                 GNUNET_DHT_GET_BLOOMFILTER_K);
+                                                 GNUNET_CONSTANTS_BLOOMFILTER_K);
   eval = GNUNET_BLOCK_evaluate (GDS_block_context,
                                type,
                                &get->key,
@@ -1682,8 +1833,8 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
   peer_bf =
     GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, 
                                       DHT_BLOOM_SIZE,
-                                      DHT_BLOOM_K);
-
+                                      GNUNET_CONSTANTS_BLOOMFILTER_K);
+  GNUNET_break_op (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (peer_bf, &peer->hashPubKey));
   /* remember request for routing replies */
   GDS_ROUTING_add (peer,
                   type,
@@ -1691,12 +1842,16 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
                   &get->key,
                   xquery, xquery_size,
                   reply_bf, get->bf_mutator);
-
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "GET for %s at %s after %u hops\n",
+             GNUNET_h2s (&get->key),
+             GNUNET_i2s (&my_identity),
+             (unsigned int) ntohl (get->hop_count));
   /* local lookup (this may update the reply_bf) */
   if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
        (am_closest_peer (&get->key,
                         peer_bf) ) )
-  {
+    {
     if ( (0 != (options & GNUNET_DHT_RO_FIND_PEER)))
     {
       GNUNET_STATISTICS_update (GDS_stats,
@@ -1716,13 +1871,19 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
                                       get->bf_mutator);
     }
   }
+  else
+  {
+    GNUNET_STATISTICS_update (GDS_stats,
+                             gettext_noop ("# P2P GET requests ONLY routed"), 1,
+                             GNUNET_NO);
+  }
   
   /* P2P forwarding */
   if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
     GDS_NEIGHBOURS_handle_get (type,
                               options,
                               ntohl (get->desired_replication_level),
-                              ntohl (get->hop_count) + 1, /* CHECK: where (else) do we do +1? */
+                              ntohl (get->hop_count),
                               &get->key,
                               xquery, xquery_size,
                               reply_bf,
@@ -1815,17 +1976,20 @@ handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
       GNUNET_break_op (0);
       return GNUNET_YES;
     }
-    bucket = find_bucket (&pid.hashPubKey);
-    if ( (bucket >= 0) &&
-        (k_buckets[bucket].peers_size < bucket_size) )
-    {    
-      if (NULL != GDS_transport_handle)
-       GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
-                                     h, NULL, NULL);
-      (void) GNUNET_CORE_peer_request_connect (coreAPI,
-                                              &pid, 
-                                              NULL, NULL);
-    }   
+    if (0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
+    {
+      bucket = find_bucket (&pid.hashPubKey);
+      if ( (bucket >= 0) &&
+          (k_buckets[bucket].peers_size < bucket_size) )
+       {    
+         if (NULL != GDS_transport_handle)
+           GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
+                                         h, NULL, NULL);
+         (void) GNUNET_CORE_peer_request_connect (coreAPI,
+                                                  &pid, 
+                                                  NULL, NULL);
+       }   
+    }
   }
 
   /* append 'peer' to 'get_path' */