gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r26350 - in gnunet/src: consensus include


From: gnunet
Subject: [GNUnet-SVN] r26350 - in gnunet/src: consensus include
Date: Thu, 7 Mar 2013 15:12:11 +0100

Author: dold
Date: 2013-03-07 15:12:11 +0100 (Thu, 07 Mar 2013)
New Revision: 26350

Modified:
   gnunet/src/consensus/consensus_api.c
   gnunet/src/consensus/consensus_protocol.h
   gnunet/src/consensus/gnunet-consensus-ibf.c
   gnunet/src/consensus/gnunet-consensus.c
   gnunet/src/consensus/gnunet-service-consensus.c
   gnunet/src/consensus/ibf.c
   gnunet/src/consensus/ibf.h
   gnunet/src/include/gnunet_consensus_service.h
   gnunet/src/include/gnunet_protocols.h
Log:
consensus now implemented with primitive conclusion group selection


Modified: gnunet/src/consensus/consensus_api.c
===================================================================
--- gnunet/src/consensus/consensus_api.c        2013-03-07 12:30:28 UTC (rev 
26349)
+++ gnunet/src/consensus/consensus_api.c        2013-03-07 14:12:11 UTC (rev 
26350)
@@ -139,6 +139,12 @@
 
   struct QueuedMessage *messages_head;
   struct QueuedMessage *messages_tail;
+
+  /**
+   * GNUNET_YES when currently in a section where destroy may not be
+   * called.
+   */
+  int may_not_destroy;
 };
 
 
@@ -279,7 +285,9 @@
                      struct GNUNET_CONSENSUS_ConcludeDoneMessage *msg)
 {
   GNUNET_assert (NULL != consensus->conclude_cb);
+  consensus->may_not_destroy = GNUNET_YES;
   consensus->conclude_cb (consensus->conclude_cls, NULL);
+  consensus->may_not_destroy = GNUNET_NO;
   consensus->conclude_cb = NULL;
 }
 
@@ -523,6 +531,11 @@
 void
 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus)
 {
+  if (GNUNET_YES == consensus->may_not_destroy)
+  {
+    LOG (GNUNET_ERROR_TYPE_ERROR, "destroy may not be called right now\n");
+    GNUNET_assert (0);
+  }
   if (consensus->client != NULL)
   {
     GNUNET_CLIENT_disconnect (consensus->client);

Modified: gnunet/src/consensus/consensus_protocol.h
===================================================================
--- gnunet/src/consensus/consensus_protocol.h   2013-03-07 12:30:28 UTC (rev 
26349)
+++ gnunet/src/consensus/consensus_protocol.h   2013-03-07 14:12:11 UTC (rev 
26350)
@@ -39,6 +39,10 @@
 {
   struct GNUNET_MessageHeader header;
   /**
+   * Number of elements the sender currently has.
+   */
+  uint16_t num_elements;
+  /**
    * Number of strata in this estimator.
    */
   uint16_t num_strata;

Modified: gnunet/src/consensus/gnunet-consensus-ibf.c
===================================================================
--- gnunet/src/consensus/gnunet-consensus-ibf.c 2013-03-07 12:30:28 UTC (rev 
26349)
+++ gnunet/src/consensus/gnunet-consensus-ibf.c 2013-03-07 14:12:11 UTC (rev 
26350)
@@ -56,7 +56,7 @@
 register_hashcode (struct GNUNET_HashCode *hash)
 {
   struct GNUNET_HashCode replicated;
-  uint64_t key;
+  struct IBF_Key key;
   key = ibf_key_from_hashcode (hash);
   ibf_hashcode_from_key (key, &replicated);
   GNUNET_CONTAINER_multihashmap_put (key_to_hashcode, &replicated, 
GNUNET_memdup (hash, sizeof *hash),
@@ -64,7 +64,7 @@
 }
 
 static void
-iter_hashcodes (uint64_t key, GNUNET_CONTAINER_HashMapIterator iter, void *cls)
+iter_hashcodes (struct IBF_Key key, GNUNET_CONTAINER_HashMapIterator iter, 
void *cls)
 {
   struct GNUNET_HashCode replicated;
   ibf_hashcode_from_key (key, &replicated);
@@ -100,7 +100,7 @@
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct GNUNET_HashCode id;
-  uint64_t ibf_key;
+  struct IBF_Key ibf_key;
   int i;
   int side;
   int res;

Modified: gnunet/src/consensus/gnunet-consensus.c
===================================================================
--- gnunet/src/consensus/gnunet-consensus.c     2013-03-07 12:30:28 UTC (rev 
26349)
+++ gnunet/src/consensus/gnunet-consensus.c     2013-03-07 14:12:11 UTC (rev 
26350)
@@ -65,6 +65,16 @@
 }
 
 
+static void
+destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *ctx)
+{
+  struct GNUNET_CONSENSUS_Handle *consensus;
+  consensus = cls;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying consensus\n");
+  GNUNET_CONSENSUS_destroy (consensus);
+}
+
+
 /**
  * Called when a conclusion was successful.
  *
@@ -72,14 +82,13 @@
  * @param group
  * @return GNUNET_YES if more consensus groups should be offered, GNUNET_NO if 
not
  */
-static int
+static void
 conclude_cb (void *cls, const struct GNUNET_CONSENSUS_Group *group)
 {
-  return GNUNET_NO;
+  GNUNET_SCHEDULER_add_now (destroy, cls);
 }
 
 
-
 static void
 generate_indices (int *indices)
 {

Modified: gnunet/src/consensus/gnunet-service-consensus.c
===================================================================
--- gnunet/src/consensus/gnunet-service-consensus.c     2013-03-07 12:30:28 UTC 
(rev 26349)
+++ gnunet/src/consensus/gnunet-service-consensus.c     2013-03-07 14:12:11 UTC 
(rev 26350)
@@ -20,7 +20,7 @@
 
 /**
  * @file consensus/gnunet-service-consensus.c
- * @brief 
+ * @brief multi-peer set reconciliation
  * @author Florian Dold
  */
 
@@ -60,7 +60,7 @@
  * Choose this value so that computing the IBF is still cheaper
  * than transmitting all values.
  */
-#define MAX_IBF_ORDER (32)
+#define MAX_IBF_ORDER (16)
 
 
 /* forward declarations */
@@ -114,11 +114,20 @@
  */
 struct ConsensusPeerInformation
 {
+  /**
+   * Socket for communicating with the peer, either created by the local peer,
+   * or the remote peer.
+   */
   struct GNUNET_STREAM_Socket *socket;
 
   /**
+   * Message tokenizer, for the data received from this peer via the stream 
socket.
+   */
+  struct GNUNET_SERVER_MessageStreamTokenizer *mst;
+
+  /**
    * Is socket's connection established, i.e. can we write to it?
-   * Only relevent on outgoing cpi.
+   * Only relevent to outgoing cpi.
    */
   int is_connected;
 
@@ -150,15 +159,22 @@
   struct GNUNET_STREAM_WriteHandle *wh;
 
   enum {
-    IBF_STATE_NONE,
+    /* beginning of round */
+    IBF_STATE_NONE=0,
+    /* we currently receive an ibf */
     IBF_STATE_RECEIVING,
+    /* we currently transmit an ibf */
     IBF_STATE_TRANSMITTING,
-    IBF_STATE_DECODING
+    /* we decode a received ibf */
+    IBF_STATE_DECODING,
+    /* wait for elements and element requests */
+    IBF_STATE_ANTICIPATE_DIFF
   } ibf_state ;
 
   /**
    * What is the order (=log2 size) of the ibf
    * we're currently dealing with?
+   * Interpretation depends on ibf_state.
    */
   int ibf_order;
 
@@ -169,7 +185,8 @@
   struct InvertibleBloomFilter *ibf;
 
   /**
-   * How many buckets have we transmitted/received (depending on state)?
+   * How many buckets have we transmitted/received? 
+   * Interpretatin depends on ibf_state
    */
   int ibf_bucket_counter;
 
@@ -180,22 +197,43 @@
   struct InvertibleBloomFilter **strata;
 
   /**
-   * difference estimated with the current strata estimator
+   * Elements that the peer is missing from us.
    */
-  unsigned int diff;
+  uint64_t *missing_local;
 
-  struct GNUNET_SERVER_MessageStreamTokenizer *mst;
+  /**
+   * Number of elements in missing_local
+   */
+  unsigned int num_missing_local;
 
   /**
+   * Elements that this peer told us *we* don't have,
+   * i.e. we are the remote peer that has some values missing.
+   */
+  uint64_t *missing_remote;
+
+  /**
+   * Number of elements in missing_local
+   */
+  unsigned int num_missing_remote;
+
+  /**
    * Back-reference to the consensus session,
    * to that ConsensusPeerInformation can be used as a closure
    */
   struct ConsensusSession *session;
 
-  struct PendingElement *send_pending_head;
-  struct PendingElement *send_pending_tail;
+  /**
+   * When decoding the IBF, requests for elements and outgoing elements
+   * have to be queued, to ensure that messages actually fit in the stream 
buffer.
+   */
+  struct QueuedMessage *requests_and_elements_head;
+  struct QueuedMessage *requests_and_elements_tail;
 };
 
+/**
+ * A doubly linked list of messages.
+ */
 struct QueuedMessage
 {
   struct GNUNET_MessageHeader *msg;
@@ -211,31 +249,40 @@
   struct QueuedMessage *prev;
 };
 
+/**
+ * Describes the current round a consensus session is in.
+ */
 enum ConsensusRound
 {
   /**
-   * distribution of information with the exponential scheme
+   * Not started the protocl yet
    */
+  CONSENSUS_ROUND_BEGIN=0,
+  /**
+   * distribution of information with the exponential scheme.
+   */
   CONSENSUS_ROUND_EXP_EXCHANGE,
   /**
-   * All-to-all, exchange missing values
+   * All-to-all, exchange missing values.
    */
   CONSENSUS_ROUND_A2A_EXCHANGE,
   /**
-   * All-to-all, check what values are missing, don't exchange anything
+   * All-to-all, check what values are missing, don't exchange anything.
    */
-  CONSENSUS_ROUND_A2A_INVENTORY
-
-  /*
-  a round to exchange the information for fraud-detection
-  CONSENSUS_ROUNT_A2_INVENTORY_AGREEMENT
-  */
+  CONSENSUS_ROUND_A2A_INVENTORY,
+  /**
+   * All-to-all round to exchange information for byzantine fault detection.
+   */
+  CONSENSUS_ROUND_A2A_INVENTORY_AGREEMENT,
+  /**
+   * Rounds are over
+   */
+  CONSENSUS_ROUND_FINISH
 };
 
 
 /**
  * A consensus session consists of one local client and the remote authorities.
- *
  */
 struct ConsensusSession
 {
@@ -301,15 +348,15 @@
   struct GNUNET_SERVER_TransmitHandle *th;
 
   /**
-   * Once conclude_requested is GNUNET_YES, the client may not
-   * insert any more values.
+   * Timeout for all rounds together, single rounds will schedule a timeout 
task
+   * with a fraction of the conclude timeout.
    */
-  int conclude_requested;
-
+  struct GNUNET_TIME_Relative conclude_timeout;
+  
   /**
-   * Minimum number of peers to form a consensus group
+   * Timeout task identifier for the current round
    */
-  int conclude_group_min;
+  GNUNET_SCHEDULER_TaskIdentifier round_timeout_tid;
 
   /**
    * Number of other peers in the consensus
@@ -353,6 +400,7 @@
 /**
  * Sockets from other peers who want to communicate with us.
  * It may not be known yet which consensus session they belong to.
+ * Also, the session might not exist yet locally.
  */
 struct IncomingSocket
 {
@@ -400,6 +448,7 @@
   struct GNUNET_HashCode *requested_gid;
 };
 
+
 static struct IncomingSocket *incoming_sockets_head;
 static struct IncomingSocket *incoming_sockets_tail;
 
@@ -440,7 +489,7 @@
 
 
 /**
- * Queue a message to be sent to the inhabiting client of a sessino
+ * Queue a message to be sent to the inhabiting client of a session.
  *
  * @param session session
  * @param msg message we want to queue
@@ -465,7 +514,9 @@
 }
 
 /**
- * Mark the peer as bad, free as state we don't need anymore.
+ * Mark the peer as bad, free state we don't need anymore.
+ *
+ * @param cpi consensus peer information of the bad peer
  */
 static void
 mark_peer_bad (struct ConsensusPeerInformation *cpi)
@@ -479,6 +530,11 @@
 /**
  * Estimate set difference with two strata estimators,
  * i.e. arrays of IBFs.
+ * Does not not modify its arguments.
+ *
+ * @param strata1 first strata estimator
+ * @param strata2 second strata estimator
+ * @return the estimated difference
  */
 static int
 estimate_difference (struct InvertibleBloomFilter** strata1,
@@ -490,9 +546,11 @@
   for (i = STRATA_COUNT - 1; i >= 0; i--)
   {
     struct InvertibleBloomFilter *diff;
+    /* number of keys decoded from the ibf */
     int ibf_count;
     int more;
     ibf_count = 0;
+    /* FIXME: implement this without always allocating new IBFs */
     diff = ibf_dup (strata1[i]);
     ibf_subtract (diff, strata2[i]);
     for (;;)
@@ -537,22 +595,17 @@
   int ret;
 
   GNUNET_assert (GNUNET_STREAM_OK == status);
-
   cpi = cls;
-
   GNUNET_assert (NULL != cpi->mst);
-
   ret = GNUNET_SERVER_mst_receive (cpi->mst, cpi, data, size, GNUNET_NO, 
GNUNET_YES);
   if (GNUNET_SYSERR == ret)
   {
     /* FIXME: handle this correctly */
     GNUNET_assert (0);
   }
-
   /* read again */
   cpi->rh = GNUNET_STREAM_read (cpi->socket, GNUNET_TIME_UNIT_FOREVER_REL,
                                 &session_stream_data_processor, cpi);
-
   /* we always read all data */
   return size;
 }
@@ -578,26 +631,62 @@
   int ret;
 
   GNUNET_assert (GNUNET_STREAM_OK == status);
-
   incoming = cls;
-
   ret = GNUNET_SERVER_mst_receive (incoming->mst, incoming, data, size, 
GNUNET_NO, GNUNET_YES);
   if (GNUNET_SYSERR == ret)
   {
     /* FIXME: handle this correctly */
     GNUNET_assert (0);
   }
-
   /* read again */
   incoming->rh = GNUNET_STREAM_read (incoming->socket, 
GNUNET_TIME_UNIT_FOREVER_REL,
                                      &incoming_stream_data_processor, 
incoming);
-
   /* we always read all data */
   return size;
 }
 
 
 /**
+ * Iterator over hash map entries.
+ * Queue elements to be sent to the peer in cls.
+ *
+ * @param cls closure
+ * @param key current key code
+ * @param value value in the hash map
+ * @return GNUNET_YES if we should continue to
+ *         iterate,
+ *         GNUNET_NO if not.
+ */
+static int
+send_element_iter (void *cls,
+                   const struct GNUNET_HashCode *key,
+                   void *value)
+{
+  struct ConsensusPeerInformation *cpi;
+  struct GNUNET_CONSENSUS_Element *element;
+  struct QueuedMessage *qm;
+  struct GNUNET_MessageHeader *element_msg;
+  size_t msize;
+  cpi = cls;
+  element = value;
+  msize = sizeof (struct GNUNET_MessageHeader) + element->size;
+  element_msg = GNUNET_malloc (msize);
+  element_msg->size = htons (msize);
+  if (CONSENSUS_ROUND_A2A_EXCHANGE == cpi->session->current_round)
+    element_msg->type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS);
+  else if (CONSENSUS_ROUND_A2A_INVENTORY == cpi->session->current_round)
+    element_msg->type = htons 
(GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_MISSING_REMOTE);
+  else
+    GNUNET_assert (0);
+  GNUNET_assert (NULL != element->data);
+  memcpy (&element_msg[1], element->data, element->size);
+  qm = GNUNET_malloc (sizeof *qm);
+  qm->msg = element_msg;
+  GNUNET_CONTAINER_DLL_insert (cpi->requests_and_elements_head, 
cpi->requests_and_elements_tail, qm);
+  return GNUNET_YES;
+}
+
+/**
  * Iterator to insert values into an ibf.
  *
  * @param cls closure
@@ -618,6 +707,12 @@
   return GNUNET_YES;
 }
 
+/**
+ * Create and populate an IBF for the specified peer,
+ * if it does not already exist.
+ *
+ * @param peer to create the ibf for
+ */
 static void
 prepare_ibf (struct ConsensusPeerInformation *cpi)
 {
@@ -630,6 +725,42 @@
 
 
 /**
+ * Called when a remote peer wants to inform the local peer
+ * that the remote peer misses elements.
+ * Elements are not reconciled.
+ *
+ * @param cpi session
+ * @param msg message
+ */
+static int
+handle_p2p_missing_local (struct ConsensusPeerInformation *cpi, const struct 
GNUNET_MessageHeader *msg)
+{
+  uint64_t key;
+  key = *(uint64_t *) &msg[1];
+  GNUNET_array_append (cpi->missing_remote, cpi->num_missing_remote, key);
+  return GNUNET_OK;
+}
+
+
+/**
+ * Called when a remote peer wants to inform the local peer
+ * that the local peer misses elements.
+ * Elements are not reconciled.
+ *
+ * @param cpi session
+ * @param msg message
+ */
+static int
+handle_p2p_missing_remote (struct ConsensusPeerInformation *cpi, const struct 
GNUNET_MessageHeader *msg)
+{
+  uint64_t key;
+  key = *(uint64_t *) &msg[1];
+  GNUNET_array_append (cpi->missing_local, cpi->num_missing_local, key);
+  return GNUNET_OK;
+}
+
+
+/**
  * Called when a peer sends us its strata estimator.
  * In response, we sent out IBF of appropriate size back.
  *
@@ -640,9 +771,9 @@
 handle_p2p_strata (struct ConsensusPeerInformation *cpi, const struct 
StrataMessage *strata_msg)
 {
   int i;
-  uint64_t *key_src;
-  uint32_t *hash_src;
-  uint8_t *count_src;
+  unsigned int diff;
+  void *buf;
+  size_t size;
 
   GNUNET_assert (GNUNET_NO == cpi->is_outgoing);
 
@@ -653,47 +784,36 @@
       cpi->strata[i] = ibf_create (STRATA_IBF_BUCKETS, STRATA_HASH_NUM, 0);
   }
 
-  /* for correct message alignment, copy bucket types seperately */
-  key_src = (uint64_t *) &strata_msg[1];
-
+  size = ntohs (strata_msg->header.size);
+  buf = (void *) &strata_msg[1];
   for (i = 0; i < STRATA_COUNT; i++)
   {
-    memcpy (cpi->strata[i]->id_sum, key_src, STRATA_IBF_BUCKETS * sizeof 
*key_src);
-    key_src += STRATA_IBF_BUCKETS;
+    int res;
+    res = ibf_read (&buf, &size, cpi->strata[i]);
+    GNUNET_assert (GNUNET_OK == res);
   }
 
-  hash_src = (uint32_t *) key_src;
+  diff = estimate_difference (cpi->session->strata, cpi->strata);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received strata, diff=%d\n", diff);
 
-  for (i = 0; i < STRATA_COUNT; i++)
+  if ( (CONSENSUS_ROUND_A2A_EXCHANGE == cpi->session->current_round) ||
+       (CONSENSUS_ROUND_A2A_INVENTORY == cpi->session->current_round))
   {
-    memcpy (cpi->strata[i]->hash_sum, hash_src, STRATA_IBF_BUCKETS * sizeof 
*hash_src);
-    hash_src += STRATA_IBF_BUCKETS;
+    /* send IBF of the right size */
+    cpi->ibf_order = 0;
+    while ((1 << cpi->ibf_order) < diff)
+      cpi->ibf_order++;
+    if (cpi->ibf_order > MAX_IBF_ORDER)
+      cpi->ibf_order = MAX_IBF_ORDER;
+    cpi->ibf_order += 1;
+    /* create ibf if not already pre-computed */
+    prepare_ibf (cpi);
+    cpi->ibf = ibf_dup (cpi->session->ibfs[cpi->ibf_order]);
+    cpi->ibf_state = IBF_STATE_TRANSMITTING;
+    cpi->ibf_bucket_counter = 0;
+    write_ibf (cpi, GNUNET_STREAM_OK, 0);
   }
 
-  count_src = (uint8_t *) hash_src;
-
-  for (i = 0; i < STRATA_COUNT; i++)
-  {
-    memcpy (cpi->strata[i]->count, count_src, STRATA_IBF_BUCKETS);
-    count_src += STRATA_IBF_BUCKETS;
-  }
-
-  cpi->diff = estimate_difference (cpi->session->strata, cpi->strata);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received strata, diff=%d\n", cpi->diff);
-
-  /* send IBF of the right size */
-  cpi->ibf_order = 0;
-  while ((1 << cpi->ibf_order) < cpi->diff)
-    cpi->ibf_order++;
-  if (cpi->ibf_order > MAX_IBF_ORDER)
-    cpi->ibf_order = MAX_IBF_ORDER;
-  cpi->ibf_order += 2;
-  /* create ibf if not already pre-computed */
-  prepare_ibf (cpi);
-  cpi->ibf = ibf_dup (cpi->session->ibfs[cpi->ibf_order]);
-  cpi->ibf_state = IBF_STATE_TRANSMITTING;
-  write_ibf (cpi, GNUNET_STREAM_OK, 0);
-
   return GNUNET_YES;
 }
 
@@ -702,52 +822,62 @@
 handle_p2p_ibf (struct ConsensusPeerInformation *cpi, const struct 
DifferenceDigest *digest)
 {
   int num_buckets;
-  uint64_t *key_src;
-  uint32_t *hash_src;
-  uint8_t *count_src;
+  void *buf;
 
   num_buckets = (ntohs (digest->header.size) - (sizeof *digest)) / 
IBF_BUCKET_SIZE;
-
-  if (IBF_STATE_NONE == cpi->ibf_state)
+  switch (cpi->ibf_state)
   {
-    cpi->ibf_state = IBF_STATE_RECEIVING;
-    cpi->ibf_order = digest->order;
-    cpi->ibf_bucket_counter = 0;
+    case IBF_STATE_NONE:
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "receiving first ibf of order %d\n", 
digest->order);
+      cpi->ibf_state = IBF_STATE_RECEIVING;
+      cpi->ibf_order = digest->order;
+      cpi->ibf_bucket_counter = 0;
+      if (NULL != cpi->ibf)
+      {
+        GNUNET_free (cpi->ibf);
+        cpi->ibf = NULL;
+      }
+      break;
+    case IBF_STATE_ANTICIPATE_DIFF:
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "receiving decode fail ibf of order 
%d\n", digest->order);
+      cpi->ibf_state = IBF_STATE_RECEIVING;
+      cpi->ibf_order = digest->order;
+      cpi->ibf_bucket_counter = 0;
+      if (NULL != cpi->ibf)
+      {
+        ibf_destroy (cpi->ibf);
+        cpi->ibf = NULL;
+      }
+      break;
+    case IBF_STATE_RECEIVING:
+      break;
+    default:
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "received ibf unexpectedly in state 
%d\n", cpi->ibf_state);
+      mark_peer_bad (cpi);
+      return GNUNET_NO;
   }
 
-  if ( (IBF_STATE_RECEIVING != cpi->ibf_state) ||
-       (cpi->ibf_bucket_counter + num_buckets > (1 << cpi->ibf_order)) )
+  if (cpi->ibf_bucket_counter + num_buckets > (1 << cpi->ibf_order))
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "received malformed ibf\n");
     mark_peer_bad (cpi);
     return GNUNET_NO;
   }
 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "receiving %d buckets at %d of %d\n", 
num_buckets,
+              cpi->ibf_bucket_counter, (1 << cpi->ibf_order));
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "receiving %d buckets at %d of %d\n", 
num_buckets, cpi->ibf_bucket_counter, (1 << cpi->ibf_order));
-
   if (NULL == cpi->ibf)
     cpi->ibf = ibf_create (1 << cpi->ibf_order, STRATA_HASH_NUM, 0);
 
-  key_src = (uint64_t *) &digest[1];
+  buf = (void *) &digest[1];
+  ibf_read_slice (&buf, NULL, cpi->ibf_bucket_counter, num_buckets, cpi->ibf);
 
-  memcpy (cpi->ibf->hash_sum, key_src, num_buckets * sizeof *key_src);
-  hash_src += num_buckets;
-
-  hash_src = (uint32_t *) key_src;
-
-  memcpy (cpi->ibf->id_sum, hash_src, num_buckets * sizeof *hash_src);
-  hash_src += num_buckets;
-
-  count_src = (uint8_t *) hash_src;
-
-  memcpy (cpi->ibf->count, count_src, num_buckets * sizeof *count_src);
-
   cpi->ibf_bucket_counter += num_buckets;
 
   if (cpi->ibf_bucket_counter == (1 << cpi->ibf_order))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received full ibf\n");
-    GNUNET_assert (NULL != cpi->wh);
     cpi->ibf_state = IBF_STATE_DECODING;
     prepare_ibf (cpi);
     ibf_subtract (cpi->ibf, cpi->session->ibfs[cpi->ibf_order]);
@@ -794,15 +924,62 @@
 
 
 /**
+ * Functions of this signature are called whenever writing operations
+ * on a stream are executed
+ *
+ * @param cls the closure from GNUNET_STREAM_write
+ * @param status the status of the stream at the time this function is called;
+ *          GNUNET_STREAM_OK if writing to stream was completed successfully;
+ *          GNUNET_STREAM_TIMEOUT if the given data is not sent successfully
+ *          (this doesn't mean that the data is never sent, the receiver may
+ *          have read the data but its ACKs may have been lost);
+ *          GNUNET_STREAM_SHUTDOWN if the stream is shutdown for writing in the
+ *          mean time; GNUNET_STREAM_SYSERR if the stream is broken and cannot
+ *          be processed.
+ * @param size the number of bytes written
+ */
+static void 
+write_requested_elements (void *cls, enum GNUNET_STREAM_Status status, size_t 
size)
+{
+  struct ConsensusPeerInformation *cpi;
+  cpi = cls;
+  GNUNET_assert (NULL == cpi->wh);
+  cpi->wh = NULL;
+  if (NULL != cpi->requests_and_elements_head)
+  {
+    struct QueuedMessage *qm;
+    qm = cpi->requests_and_elements_head;
+    GNUNET_CONTAINER_DLL_remove (cpi->requests_and_elements_head, 
cpi->requests_and_elements_tail, qm);
+
+    cpi->wh = GNUNET_STREAM_write (cpi->socket, qm->msg, ntohs (qm->msg->size),
+                                   GNUNET_TIME_UNIT_FOREVER_REL,
+                                   write_requested_elements, cpi);
+    GNUNET_assert (NULL != cpi->wh);
+  }
+}
+
+
+/**
  * Handle a request for elements.
  * Only allowed in exchange-rounds.
- *
- * FIXME: implement
  */
 static int
 handle_p2p_element_request (struct ConsensusPeerInformation *cpi, const struct 
ElementRequest *msg)
 {
-  /* FIXME: implement */
+  struct GNUNET_HashCode *hashcode;
+  unsigned int num;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "handling element request\n");
+  num = ntohs (msg->header.size) / sizeof (struct GNUNET_HashCode);
+  hashcode = (struct GNUNET_HashCode *) &msg[1];
+  while (num--)
+  {
+    GNUNET_assert (IBF_STATE_ANTICIPATE_DIFF == cpi->ibf_state);
+    GNUNET_CONTAINER_multihashmap_get_multiple (cpi->session->values, 
hashcode, send_element_iter, cpi);
+    if (NULL == cpi->wh)
+      write_requested_elements (cpi, GNUNET_STREAM_OK, 0);
+    hashcode++;
+  }
   return GNUNET_YES;
 }
 
@@ -831,6 +1008,12 @@
       inc->cpi->mst = inc->mst;
       inc->cpi->hello = GNUNET_YES;
       inc->cpi->socket = inc->socket;
+
+      if ( (CONSENSUS_ROUND_A2A_EXCHANGE == session->current_round) &&
+           (GNUNET_YES == inc->cpi->is_outgoing))
+      {
+        write_strata (&session->info[idx], GNUNET_STREAM_OK, 0);
+      }
       return GNUNET_YES;
     }
     session = session->next;
@@ -866,6 +1049,10 @@
       return handle_p2p_ibf (cpi, (struct DifferenceDigest *) message);
     case GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS:
       return handle_p2p_element (cpi, message);
+    case GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_MISSING_LOCAL:
+      return handle_p2p_missing_local (cpi, message);
+    case GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_MISSING_REMOTE:
+      return handle_p2p_missing_remote (cpi, message);
     case GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_REQUEST:
       return handle_p2p_element_request (cpi, (struct ElementRequest *) 
message);
     default:
@@ -938,7 +1125,6 @@
   incoming->rh = GNUNET_STREAM_read (socket, GNUNET_TIME_UNIT_FOREVER_REL,
                                      &incoming_stream_data_processor, 
incoming);
 
-
   incoming->mst = GNUNET_SERVER_mst_create (mst_incoming_callback, incoming);
 
   GNUNET_CONTAINER_DLL_insert_tail (incoming_sockets_head, 
incoming_sockets_tail, incoming);
@@ -947,6 +1133,11 @@
 }
 
 
+/**
+ * Destroy a session, free all resources associated with it.
+ * 
+ * @param session the session to destroy
+ */
 static void
 destroy_session (struct ConsensusSession *session)
 {
@@ -1013,10 +1204,7 @@
 
 
 /**
- * Function called to notify a client about the connection
- * begin ready to queue more data.  "buf" will be
- * NULL and "size" zero if the connection was closed for
- * writing in the meantime.
+ * Transmit a queued message to the session's client.
  *
  * @param cls consensus session
  * @param size number of bytes available in buf
@@ -1034,7 +1222,6 @@
   session = cls;
   session->th = NULL;
 
-
   qmsg = session->client_messages_head;
   GNUNET_CONTAINER_DLL_remove (session->client_messages_head, 
session->client_messages_tail, qmsg);
   GNUNET_assert (qmsg);
@@ -1060,7 +1247,7 @@
 
 
 /**
- * Schedule sending the next message (if there is any) to a client.
+ * Schedule transmitting the next queued message (if any) to a client.
  *
  * @param cli the client to send the next message to
  */
@@ -1118,7 +1305,6 @@
 }
 
 
-
 /**
  * Called when stream has finishes writing the hello message
  */
@@ -1128,21 +1314,25 @@
   struct ConsensusPeerInformation *cpi;
 
   cpi = cls;
+  cpi->wh = NULL;
   cpi->hello = GNUNET_YES;
   
   GNUNET_assert (GNUNET_STREAM_OK == status);
 
-  if (cpi->session->conclude_requested)
+  /* FIXME: other rounds */
+
+  if ( (CONSENSUS_ROUND_A2A_EXCHANGE == cpi->session->current_round) &&
+       (GNUNET_YES == cpi->is_outgoing))
   {
-    write_strata (cpi, GNUNET_STREAM_OK, 0);  
+    write_strata (cpi, GNUNET_STREAM_OK, 0);
   }
 }
 
 
 /**
- * Functions of this type will be called when a stream is established
+ * Called when we established a stream connection to another peer
  *
- * @param cls the closure from GNUNET_STREAM_open
+ * @param cls cpi of the peer we just connected to
  * @param socket socket to use to communicate with the other side (read/write)
  */
 static void
@@ -1151,9 +1341,9 @@
   struct ConsensusPeerInformation *cpi;
   struct ConsensusHello *hello;
 
-
   cpi = cls;
   cpi->is_connected = GNUNET_YES;
+  cpi->wh = NULL;
 
   hello = GNUNET_malloc (sizeof *hello);
   hello->header.size = htons (sizeof *hello);
@@ -1165,7 +1355,6 @@
 
   cpi->rh = GNUNET_STREAM_read (socket, GNUNET_TIME_UNIT_FOREVER_REL,
                                 &session_stream_data_processor, cpi);
-
 }
 
 
@@ -1182,7 +1371,7 @@
     session->info[i].session = session;
   }
 
-  session->current_round = CONSENSUS_ROUND_A2A_EXCHANGE;
+  session->current_round = CONSENSUS_ROUND_BEGIN;
 
   last = (session->local_peer_idx + ((session->num_peers - 1) / 2) + 1) % 
session->num_peers;
   i = (session->local_peer_idx + 1) % session->num_peers;
@@ -1267,6 +1456,47 @@
 
 
 /**
+ * Add incoming peer connections to the session,
+ * for peers who have connected to us before the local session has been 
established
+ *
+ * @param session ...
+ */
+static void
+add_incoming_peers (struct ConsensusSession *session)
+{
+  struct IncomingSocket *inc;
+  inc = incoming_sockets_head;
+
+  while (NULL != inc)
+  {
+    if (0 == GNUNET_CRYPTO_hash_cmp (&session->global_id, inc->requested_gid))
+    {
+      int i;
+      for (i = 0; i < session->num_peers; i++)
+      {
+        struct ConsensusPeerInformation *cpi;
+        cpi = &session->info[i];
+        if (0 == memcmp (inc->peer, &cpi->session->peers[i], sizeof (struct 
GNUNET_PeerIdentity)))
+        {
+          if (GNUNET_YES == cpi->is_outgoing)
+          {
+            /* FIXME: disconnect */
+            continue;
+          }
+          cpi->socket = inc->socket;
+          inc->cpi = cpi;
+          inc->cpi->mst = inc->mst;
+          inc->cpi->hello = GNUNET_YES;
+          break;
+        }
+      }
+    }
+    inc = inc->next;
+  }
+}
+
+
+/**
  * Initialize the session, continue receiving messages from the owning client
  *
  * @param session the session to initialize
@@ -1311,7 +1541,7 @@
   for (i = 0; i < STRATA_COUNT; i++)
     session->strata[i] = ibf_create (STRATA_IBF_BUCKETS, STRATA_HASH_NUM, 0);
 
-  session->ibfs = GNUNET_malloc (MAX_IBF_ORDER * sizeof (struct 
InvertibleBloomFilter *));
+  session->ibfs = GNUNET_malloc ((MAX_IBF_ORDER+1) * sizeof (struct 
InvertibleBloomFilter *));
 
   session->info = GNUNET_malloc (session->num_peers * sizeof (struct 
ConsensusPeerInformation));
   initialize_session_info (session);
@@ -1319,6 +1549,8 @@
   GNUNET_free (session->join_msg);
   session->join_msg = NULL;
 
+  add_incoming_peers (session);
+
   GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "session %s initialized\n", GNUNET_h2s 
(&session->global_id));
 }
@@ -1370,6 +1602,19 @@
 
 
 /**
+ * Hash a block of data, producing a replicated ibf hash.
+ */
+static void
+hash_for_ibf (const void *block, size_t size, struct GNUNET_HashCode *ret)
+{
+  struct IBF_Key ibf_key;
+  GNUNET_CRYPTO_hash (block, size, ret);
+  ibf_key = ibf_key_from_hashcode (ret);
+  ibf_hashcode_from_key (ibf_key, ret);
+}
+
+
+/**
  * Called when a client performs an insert operation.
  *
  * @param cls (unused)
@@ -1384,7 +1629,7 @@
   struct ConsensusSession *session;
   struct GNUNET_CONSENSUS_ElementMessage *msg;
   struct GNUNET_CONSENSUS_Element *element;
-  struct GNUNET_HashCode key;
+  struct GNUNET_HashCode hash;
   int element_size;
 
   session = sessions_head;
@@ -1413,12 +1658,14 @@
 
   GNUNET_assert (NULL != element->data);
 
-  GNUNET_CRYPTO_hash (element, element_size, &key);
+  hash_for_ibf (element, element_size, &hash);
 
-  GNUNET_CONTAINER_multihashmap_put (session->values, &key, element,
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "inserting with hash_for_ibf %s\n", 
GNUNET_h2s (&hash));
+
+  GNUNET_CONTAINER_multihashmap_put (session->values, &hash, element,
                                      
GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
 
-  strata_insert (session->strata, &key);
+  strata_insert (session->strata, &hash);
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 
@@ -1426,8 +1673,6 @@
 }
 
 
-
-
 /**
  * Functions of this signature are called whenever writing operations
  * on a stream are executed
@@ -1446,10 +1691,14 @@
 static void 
 write_strata_done (void *cls, enum GNUNET_STREAM_Status status, size_t size)
 {
+  struct ConsensusPeerInformation *cpi;
+  cpi = cls;
+  cpi->wh = NULL;
   GNUNET_assert (GNUNET_STREAM_OK == status);
   /* just wait for the ibf */
 }
 
+
 /**
  * Functions of this signature are called whenever writing operations
  * on a stream are executed
@@ -1470,11 +1719,9 @@
 {
   struct ConsensusPeerInformation *cpi;
   struct StrataMessage *strata_msg;
+  void *buf;
   size_t msize;
   int i;
-  uint64_t *key_dst;
-  uint32_t *hash_dst;
-  uint8_t *count_dst;
 
   cpi = cls;
   cpi->wh = NULL;
@@ -1491,39 +1738,33 @@
   strata_msg = GNUNET_malloc (msize);
   strata_msg->header.size = htons (msize);
   strata_msg->header.type = htons 
(GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_DELTA_ESTIMATE);
-
-  /* for correct message alignment, copy bucket types seperately */
-  key_dst = (uint64_t *) &strata_msg[1];
-
+  
+  buf = &strata_msg[1];
   for (i = 0; i < STRATA_COUNT; i++)
   {
-    memcpy (key_dst, cpi->session->strata[i]->id_sum, STRATA_IBF_BUCKETS * 
sizeof *key_dst);
-    key_dst += STRATA_IBF_BUCKETS;
+    ibf_write (cpi->session->strata[i], &buf, NULL);
   }
 
-  hash_dst = (uint32_t *) key_dst;
-
-  for (i = 0; i < STRATA_COUNT; i++)
-  {
-    memcpy (hash_dst, cpi->session->strata[i]->hash_sum, STRATA_IBF_BUCKETS * 
sizeof *hash_dst);
-    hash_dst += STRATA_IBF_BUCKETS;
-  }
-
-  count_dst = (uint8_t *) hash_dst;
-
-  for (i = 0; i < STRATA_COUNT; i++)
-  {
-    memcpy (count_dst, cpi->session->strata[i]->count, STRATA_IBF_BUCKETS);
-    count_dst += STRATA_IBF_BUCKETS;
-  }
-
   cpi->wh = GNUNET_STREAM_write (cpi->socket, strata_msg, msize, 
GNUNET_TIME_UNIT_FOREVER_REL,
                                  write_strata_done, cpi);
 
   GNUNET_assert (NULL != cpi->wh);
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "strata written\n");
 }
 
 
+static void 
+write_ibf_done (void *cls, enum GNUNET_STREAM_Status status, size_t size)
+{
+  struct ConsensusPeerInformation *cpi;
+  cpi = cls;
+  cpi->wh = NULL;
+  GNUNET_assert (GNUNET_STREAM_OK == status);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "write ibf done callback\n");
+}
+
+
 /**
  * Functions of this signature are called whenever writing operations
  * on a stream are executed
@@ -1545,24 +1786,17 @@
   struct ConsensusPeerInformation *cpi;
   struct DifferenceDigest *digest;
   int msize;
-  uint64_t *key_dst;
-  uint32_t *hash_dst;
-  uint8_t *count_dst;
   int num_buckets;
+  void *buf;
 
   cpi = cls;
   cpi->wh = NULL;
 
   GNUNET_assert (GNUNET_STREAM_OK == status);
-
   GNUNET_assert (IBF_STATE_TRANSMITTING == cpi->ibf_state);
 
-  if (cpi->ibf_bucket_counter == (1 << cpi->ibf_order))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ibf completely written\n");
-    /* we now wait for values / requests / another IBF because peer could not 
decode with our IBF */
-    return;
-  }
+  /* we should not be done here! */
+  GNUNET_assert (cpi->ibf_bucket_counter != (1 << cpi->ibf_order));
 
   /* remaining buckets */
   num_buckets = (1 << cpi->ibf_order) - cpi->ibf_bucket_counter;
@@ -1580,24 +1814,23 @@
   digest->header.type = htons 
(GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_DIFFERENCE_DIGEST);
   digest->order = cpi->ibf_order;
 
-  key_dst = (uint64_t *) &digest[1];
+  buf = &digest[1];
+  ibf_write_slice (cpi->ibf, cpi->ibf_bucket_counter, num_buckets, &buf, NULL);
 
-  memcpy (key_dst, cpi->ibf->id_sum, num_buckets * sizeof *key_dst);
-  key_dst += num_buckets;
-
-  hash_dst = (uint32_t *) key_dst;
-
-  memcpy (hash_dst, cpi->ibf->id_sum, num_buckets * sizeof *hash_dst);
-  hash_dst += num_buckets;
-
-  count_dst = (uint8_t *) hash_dst;
-
-  memcpy (count_dst, cpi->ibf->count, num_buckets * sizeof *count_dst);
-
   cpi->ibf_bucket_counter += num_buckets;
 
-  cpi->wh = GNUNET_STREAM_write (cpi->socket, digest, msize, 
GNUNET_TIME_UNIT_FOREVER_REL,
-                                 write_ibf, cpi);
+  /* we have to set the new state here, because of non-deterministic 
schedulung */
+  if (cpi->ibf_bucket_counter == (1 << cpi->ibf_order))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ibf completely written\n");
+    /* we now wait for values / requests / another IBF because peer could not 
decode with our IBF */
+    cpi->ibf_state = IBF_STATE_ANTICIPATE_DIFF;
+    cpi->wh = GNUNET_STREAM_write (cpi->socket, digest, msize, 
GNUNET_TIME_UNIT_FOREVER_REL, write_ibf_done, cpi);
+  }
+  else
+  {
+    cpi->wh = GNUNET_STREAM_write (cpi->socket, digest, msize, 
GNUNET_TIME_UNIT_FOREVER_REL, write_ibf, cpi);
+  }
 
   GNUNET_assert (NULL != cpi->wh);
 }
@@ -1622,30 +1855,46 @@
 write_requests_and_elements (void *cls, enum GNUNET_STREAM_Status status, 
size_t size)
 {
   struct ConsensusPeerInformation *cpi;
-  uint64_t key;
+  struct IBF_Key key;
   struct GNUNET_HashCode hashcode;
   int side;
-  int msize;
 
   GNUNET_assert (GNUNET_STREAM_OK == status);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "transmitting value\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "decoding\n");
 
   cpi = cls;
+  GNUNET_assert (IBF_STATE_DECODING == cpi->ibf_state);
   cpi->wh = NULL;
 
-  GNUNET_assert (IBF_STATE_DECODING == cpi->ibf_state);
+  if (NULL != cpi->requests_and_elements_head)
+  {
+    struct QueuedMessage *qm;
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sending queued element\n");
+    qm = cpi->requests_and_elements_head;
+    GNUNET_CONTAINER_DLL_remove (cpi->requests_and_elements_head, 
cpi->requests_and_elements_tail, qm);
 
+    cpi->wh = GNUNET_STREAM_write (cpi->socket, qm->msg, ntohs (qm->msg->size),
+                                   GNUNET_TIME_UNIT_FOREVER_REL,
+                                   write_requests_and_elements, cpi);
+    GNUNET_assert (NULL != cpi->wh);
+    /* some elements / requests have queued up, we have to transmit them first 
*/
+    return;
+  }
+
   for (;;)
   {
     int res;
     res = ibf_decode (cpi->ibf, &side, &key);
     if (GNUNET_SYSERR == res)
     {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "decoding failed, transmitting 
larger IBF\n");
+      /* decoding failed, we tell the other peer by sending our ibf with a 
larger order */
       cpi->ibf_order++;
       prepare_ibf (cpi);
       cpi->ibf = ibf_dup (cpi->session->ibfs[cpi->ibf_order]);
       cpi->ibf_state = IBF_STATE_TRANSMITTING;
+      cpi->ibf_bucket_counter = 0;
       write_ibf (cls, status, size);
       return;
     }
@@ -1656,38 +1905,52 @@
     }
     if (-1 == side)
     {
-      struct GNUNET_CONSENSUS_Element *element;
-      struct GNUNET_MessageHeader *element_msg;
+      /* we have the element, send it to the other peer */
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sending element\n");
       ibf_hashcode_from_key (key, &hashcode);
-      /* FIXME: this only transmits one element stored with the key */
-      element = GNUNET_CONTAINER_multihashmap_get (cpi->session->values, 
&hashcode);
-      if (NULL == element)
-        continue;
-      msize = sizeof (struct GNUNET_MessageHeader) + element->size;
-      element_msg = GNUNET_malloc (msize);
-      element_msg->size = htons (msize);
-      element_msg->type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS);
-      GNUNET_assert (NULL != element->data);
-      memcpy (&element_msg[1], element->data, element->size);
-      cpi->wh = GNUNET_STREAM_write (cpi->socket, element_msg, msize, 
GNUNET_TIME_UNIT_FOREVER_REL,
-                                     write_requests_and_elements, cpi);
-      GNUNET_free (element_msg);
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "transmitted value\n");
-
-      GNUNET_assert (NULL != cpi->wh);
+      GNUNET_CONTAINER_multihashmap_get_multiple (cpi->session->values, 
&hashcode, send_element_iter, cpi);
+      /* send the first message, because we can! */
+      if (NULL != cpi->requests_and_elements_head)
+      {
+        struct QueuedMessage *qm;
+        qm = cpi->requests_and_elements_head;
+        GNUNET_CONTAINER_DLL_remove (cpi->requests_and_elements_head, 
cpi->requests_and_elements_tail, qm);
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "writing element\n");
+        cpi->wh = GNUNET_STREAM_write (cpi->socket, qm->msg, ntohs 
(qm->msg->size),
+                                       GNUNET_TIME_UNIT_FOREVER_REL,
+                                       write_requests_and_elements, cpi);
+        GNUNET_assert (NULL != cpi->wh);
+      }
+      else
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "no element found for decoded hash 
%s\n", GNUNET_h2s (&hashcode));
+      }
       return;
     }
     else
     {
       struct ElementRequest *msg;
       size_t msize;
-      uint64_t *p;
+      struct IBF_Key *p;
 
       msize = (sizeof *msg) + sizeof (uint64_t);
       msg = GNUNET_malloc (msize);
-      msg->header.type = htons 
(GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_REQUEST);
+      if (CONSENSUS_ROUND_A2A_EXCHANGE == cpi->session->current_round)
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sending request for element\n");
+        msg->header.type = htons 
(GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_REQUEST);
+      }
+      else if (CONSENSUS_ROUND_A2A_INVENTORY == cpi->session->current_round)
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sending locally missing 
element\n");
+        msg->header.type = htons 
(GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_MISSING_LOCAL);
+      }
+      else
+      {
+        GNUNET_assert (0);
+      }
       msg->header.size = htons (msize);
-      p = (uint64_t *) &msg[1];
+      p = (struct IBF_Key *) &msg[1];
       *p = key;
 
       cpi->wh = GNUNET_STREAM_write (cpi->socket, msg, msize, 
GNUNET_TIME_UNIT_FOREVER_REL,
@@ -1697,20 +1960,154 @@
       return;
     }
   }
+}
 
+
+static double
+compute_similarity (struct ConsensusSession *session, int p1, int p2)
+{
+  /* FIXME: simplistic dummy implementation, use real set union/intersecion */
+  return (session->info[p1].num_missing_local + 
session->info[p2].num_missing_local) /
+      ((double) (session->info[p1].num_missing_remote + 
session->info[p2].num_missing_remote + 1));
 }
 
 
-
-/*
 static void
-select_best_group (struct ConsensusSession *session)
+select_fittest_group (struct ConsensusSession *session)
 {
+  /* simplistic implementation: compute the similarity with the latest strata 
estimator,
+   * rank the results once */
+  struct GNUNET_PeerIdentity *group;
+  double rating[session->num_peers];
+  struct GNUNET_CONSENSUS_ConcludeDoneMessage *done_msg;
+  size_t msize;
+  int i;
+  int j;
+  /* number of peers in the consensus group */
+  int k;
+
+  k = ceil(session->num_peers / 3.0) * 2;
+  group = GNUNET_malloc (k * sizeof *group);
+
+  /* do strata subtraction */
+  /* FIXME: we know the real sets, subtract them! */
+  for (i = 0; i < session->num_peers; i++)
+  {
+    rating[i] = 0;
+    for (j = 0; j < i; j++)
+    {
+      double sim;
+      sim = compute_similarity (session, i, j);
+      rating[i] += sim;
+      rating[j] += sim;
+    }
+  }
+  for (i = 0; i < k; i++)
+  {
+    int best_idx = 0;
+    for (j = 1; j < session->num_peers; j++)
+      if (rating[j] > rating[best_idx])
+        best_idx = j;
+    rating[best_idx] = -1;
+    group[i] = session->peers[best_idx];
+  }
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got group!\n");
+
+  msize = sizeof *done_msg + k * sizeof *group;
+
+  done_msg = GNUNET_malloc (msize);
+  done_msg->header.size = htons (msize);
+  done_msg->header.type = htons 
(GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE);
+  memcpy (&done_msg[1], group, k * sizeof *group);
+
+  queue_client_message (session, (struct GNUNET_MessageHeader *) done_msg);
+  send_next (session);
 }
-*/
 
 
 /**
+ * Select and kick off the next round, based on the current round.
+ * @param cls the session
+ * @param tc task context, for when this task is invoked by the scheduler,
+ *           NULL if invoked for another reason
+ */
+static void 
+round_over (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct ConsensusSession *session;
+  int i;
+
+  /* don't kick off next round if we're shutting down */
+  if ((NULL != tc) && (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "round over\n");
+  session = cls;
+
+  if ((NULL == tc) && (session->round_timeout_tid != GNUNET_SCHEDULER_NO_TASK))
+  {
+    GNUNET_SCHEDULER_cancel (session->round_timeout_tid);
+    session->round_timeout_tid = GNUNET_SCHEDULER_NO_TASK;
+  }
+
+  for (i = 0; i < session->num_peers; i++)
+  {
+    if ((NULL != session->info) && (NULL != session->info[i].wh))
+      GNUNET_STREAM_write_cancel (session->info[i].wh);
+  }
+
+  switch (session->current_round)
+  {
+    case CONSENSUS_ROUND_BEGIN:
+    {
+      session->current_round = CONSENSUS_ROUND_A2A_EXCHANGE;
+      session->round_timeout_tid = GNUNET_SCHEDULER_add_delayed 
(GNUNET_TIME_relative_divide (session->conclude_timeout, 4),
+                                                                 round_over, 
session);
+      for (i = 0; i < session->num_peers; i++)
+      {
+        /* we can only talk to hello'ed peers */
+        if ( (GNUNET_YES == session->info[i].is_outgoing) &&
+             (GNUNET_YES == session->info[i].hello) )
+        {
+          /* kick off transmitting strata by calling the write continuation */
+          write_strata (&session->info[i], GNUNET_STREAM_OK, 0);
+        }
+      }
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "conclude started, timeout=%llu\n", 
session->conclude_timeout.rel_value);
+      break;
+    }
+    case CONSENSUS_ROUND_A2A_EXCHANGE:
+    {
+      session->current_round = CONSENSUS_ROUND_A2A_INVENTORY;
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "starting inventory round\n");
+      session->round_timeout_tid = GNUNET_SCHEDULER_add_delayed 
(GNUNET_TIME_relative_divide (session->conclude_timeout, 4),
+                                                                 round_over, 
session);
+      for (i = 0; i < session->num_peers; i++)
+      {
+        session->info[i].ibf_state = IBF_STATE_NONE;
+        if ( (GNUNET_YES == session->info[i].is_outgoing) &&
+             (GNUNET_YES == session->info[i].hello) )
+        {
+          /* kick off transmitting strata by calling the write continuation */
+          write_strata (&session->info[i], GNUNET_STREAM_OK, 0);
+        }
+      }
+      break;
+    }
+    case CONSENSUS_ROUND_A2A_INVENTORY:
+      /* finally, we are done and select the most fitting group */
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "protocol rounds done\n");
+      session->current_round = CONSENSUS_ROUND_FINISH;
+      select_fittest_group (session);
+      break;
+    default:
+      GNUNET_assert (0);
+  }
+}
+
+
+/**
  * Called when a client performs the conclude operation.
  *
  * @param cls (unused)
@@ -1719,13 +2116,13 @@
  */
 static void
 client_conclude (void *cls,
-             struct GNUNET_SERVER_Client *client,
-             const struct GNUNET_MessageHeader *message)
+                 struct GNUNET_SERVER_Client *client,
+                 const struct GNUNET_MessageHeader *message)
 {
   struct ConsensusSession *session;
-  int i;
+  struct GNUNET_CONSENSUS_ConcludeMessage *cmsg;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "conclude requested\n");
+  cmsg = (struct GNUNET_CONSENSUS_ConcludeMessage *) message;
 
   session = sessions_head;
   while ((session != NULL) && (session->client != client))
@@ -1738,25 +2135,19 @@
     return;
   }
 
-  if (GNUNET_YES == session->conclude_requested)
+  if (CONSENSUS_ROUND_BEGIN != session->current_round)
   {
     /* client requested conclude twice */
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "unexpected round at conclude: %d\n", 
session->current_round);
     GNUNET_break (0);
     disconnect_client (client);
     return;
   }
 
-  session->conclude_requested = GNUNET_YES;
+  session->conclude_timeout = GNUNET_TIME_relative_ntoh (cmsg->timeout);
 
-  for (i = 0; i < session->num_peers; i++)
-  {
-    if ( (GNUNET_YES == session->info[i].is_outgoing) &&
-         (GNUNET_YES == session->info[i].hello) )
-    {
-      /* kick off transmitting strata by calling the write continuation */
-      write_strata (&session->info[i], GNUNET_STREAM_OK, 0);
-    }
-  }
+  /* the 'begin' round is over, start with the next, real round */
+  round_over (session, NULL);
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
   send_next (session);
@@ -1803,12 +2194,19 @@
 
   if (msg->keep)
   {
+    int i;
     element = pending->element;
-    GNUNET_CRYPTO_hash (element, element->size, &key);
+    hash_for_ibf (element, element->size, &key);
 
     GNUNET_CONTAINER_multihashmap_put (session->values, &key, element,
                                        
GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
     strata_insert (session->strata, &key);
+    
+    for (i = 0; i <= MAX_IBF_ORDER; i++)
+    {
+      if (NULL != session->ibfs[i])
+        ibf_insert (session->ibfs[i], ibf_key_from_hashcode (&key));
+    }
   }
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -1862,7 +2260,6 @@
 shutdown_task (void *cls,
                const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-
   /* FIXME: complete; write separate destructors for different data types */
 
   while (NULL != incoming_sockets_head)
@@ -1884,15 +2281,16 @@
 
     session = sessions_head;
 
-    for (i = 0; session->num_peers; i++)
-    {
-      struct ConsensusPeerInformation *cpi;
-      cpi = &session->info[i];
-      if ((NULL != cpi) && (NULL != cpi->socket))
+    if (NULL != session->info)
+      for (i = 0; i < session->num_peers; i++)
       {
-        GNUNET_STREAM_close (cpi->socket);
+        struct ConsensusPeerInformation *cpi;
+        cpi = &session->info[i];
+        if ((NULL != cpi) && (NULL != cpi->socket))
+        {
+          GNUNET_STREAM_close (cpi->socket);
+        }
       }
-    }
 
     if (NULL != session->client)
       GNUNET_SERVER_client_disconnect (session->client);
@@ -1952,7 +2350,6 @@
                                    listen_cb, NULL,
                                    GNUNET_STREAM_OPTION_END);
 
-
   /* we have to wait for the core_startup callback before proceeding with the 
consensus service startup */
   core = GNUNET_CORE_connect (c, NULL, &core_startup, NULL, NULL, NULL, 
GNUNET_NO, NULL, GNUNET_NO, core_handlers);
   GNUNET_assert (NULL != core);

Modified: gnunet/src/consensus/ibf.c
===================================================================
--- gnunet/src/consensus/ibf.c  2013-03-07 12:30:28 UTC (rev 26349)
+++ gnunet/src/consensus/ibf.c  2013-03-07 14:12:11 UTC (rev 26350)
@@ -18,30 +18,27 @@
       Boston, MA 02111-1307, USA.
 */
 
-
 /**
  * @file consensus/ibf.c
  * @brief implementation of the invertible bloom filter
  * @author Florian Dold
  */
 
-
 #include "ibf.h"
 
-
 /**
  * Create a key from a hashcode.
  *
  * @param hash the hashcode
  * @return a key
  */
-uint64_t
+struct IBF_Key
 ibf_key_from_hashcode (const struct GNUNET_HashCode *hash)
 {
-  return GNUNET_ntohll (*(uint64_t *) hash);
+  /* FIXME: endianess */
+  return *(struct IBF_Key *) hash;
 }
 
-
 /**
  * Create a hashcode from a key, by replicating the key
  * until the hascode is filled
@@ -50,12 +47,13 @@
  * @param dst hashcode to store the result in
  */
 void
-ibf_hashcode_from_key (uint64_t key, struct GNUNET_HashCode *dst)
+ibf_hashcode_from_key (struct IBF_Key key, struct GNUNET_HashCode *dst)
 {
-  uint64_t *p;
-  int i;
-  p = (uint64_t *) dst;
-  for (i = 0; i < 8; i++)
+  struct IBF_Key *p;
+  unsigned int i;
+  const unsigned int keys_per_hashcode = sizeof (struct GNUNET_HashCode) / 
sizeof (struct IBF_Key);
+  p = (struct IBF_Key *) dst;
+  for (i = 0; i < keys_per_hashcode; i++)
     *p++ = key;
 }
 
@@ -70,14 +68,16 @@
  * @return the newly created invertible bloom filter
  */
 struct InvertibleBloomFilter *
-ibf_create (uint32_t size, unsigned int hash_num, uint32_t salt)
+ibf_create (uint32_t size, uint8_t hash_num, uint32_t salt)
 {
   struct InvertibleBloomFilter *ibf;
 
+  /* TODO: use malloc_large */
+
   ibf = GNUNET_malloc (sizeof (struct InvertibleBloomFilter));
   ibf->count = GNUNET_malloc (size * sizeof (uint8_t));
-  ibf->id_sum = GNUNET_malloc (size * sizeof (struct GNUNET_HashCode));
-  ibf->hash_sum = GNUNET_malloc (size * sizeof (struct GNUNET_HashCode));
+  ibf->key_sum = GNUNET_malloc (size * sizeof (struct GNUNET_HashCode));
+  ibf->key_hash_sum = GNUNET_malloc (size * sizeof (struct GNUNET_HashCode));
   ibf->size = size;
   ibf->hash_num = hash_num;
 
@@ -89,7 +89,7 @@
  */
 static inline void
 ibf_get_indices (const struct InvertibleBloomFilter *ibf,
-                 uint64_t key, int *dst)
+                 struct IBF_Key key, int *dst)
 {
   struct GNUNET_HashCode bucket_indices;
   unsigned int filled = 0;
@@ -113,20 +113,20 @@
 
 static void
 ibf_insert_into  (struct InvertibleBloomFilter *ibf,
-                  uint64_t key,
+                  struct IBF_Key key,
                   const int *buckets, int side)
 {
   int i;
   struct GNUNET_HashCode key_hash_sha;
-  uint32_t key_hash;
+  struct IBF_KeyHash key_hash;
   GNUNET_CRYPTO_hash (&key, sizeof key, &key_hash_sha);
-  key_hash = key_hash_sha.bits[0];
+  key_hash.key_hash_val = key_hash_sha.bits[0];
   for (i = 0; i < ibf->hash_num; i++)
   {
     const int bucket = buckets[i];
-    ibf->count[bucket] += side;
-    ibf->id_sum[bucket] ^= key;
-    ibf->hash_sum[bucket] ^= key_hash;
+    ibf->count[bucket].count_val += side;
+    ibf->key_sum[bucket].key_val ^= key.key_val;
+    ibf->key_hash_sum[bucket].key_hash_val ^= key_hash.key_hash_val;
   }
 }
 
@@ -138,7 +138,7 @@
  * @param id the element's hash code
  */
 void
-ibf_insert (struct InvertibleBloomFilter *ibf, uint64_t key)
+ibf_insert (struct InvertibleBloomFilter *ibf, struct IBF_Key key)
 {
   int buckets[ibf->hash_num];
   ibf_get_indices (ibf, key, buckets);
@@ -154,11 +154,11 @@
   int i;
   for (i = 0; i < ibf->size; i++)
   {
-    if (0 != ibf->count[i])
+    if (0 != ibf->count[i].count_val)
       return GNUNET_NO;
-    if (0 != ibf->hash_sum[i])
+    if (0 != ibf->key_hash_sum[i].key_hash_val)
       return GNUNET_NO;
-    if (0 != ibf->id_sum[i])
+    if (0 != ibf->key_sum[i].key_val)
       return GNUNET_NO;
   }
   return GNUNET_YES;
@@ -179,9 +179,9 @@
  */
 int
 ibf_decode (struct InvertibleBloomFilter *ibf,
-            int *ret_side, uint64_t *ret_id)
+            int *ret_side, struct IBF_Key *ret_id)
 {
-  uint32_t hash;
+  struct IBF_KeyHash hash;
   int i;
   struct GNUNET_HashCode key_hash_sha;
   int buckets[ibf->hash_num];
@@ -194,20 +194,20 @@
     int hit;
 
     /* we can only decode from pure buckets */
-    if ((1 != ibf->count[i]) && (-1 != ibf->count[i]))
+    if ((1 != ibf->count[i].count_val) && (-1 != ibf->count[i].count_val))
       continue;
 
-    GNUNET_CRYPTO_hash (&ibf->id_sum[i], sizeof (uint64_t), &key_hash_sha);
-    hash = key_hash_sha.bits[0];
+    GNUNET_CRYPTO_hash (&ibf->key_sum[i], sizeof (struct IBF_Key), 
&key_hash_sha);
+    hash.key_hash_val = key_hash_sha.bits[0];
 
     /* test if the hash matches the key */
-    if (hash != ibf->hash_sum[i])
+    if (hash.key_hash_val != ibf->key_hash_sum[i].key_hash_val)
       continue;
 
     /* test if key in bucket hits its own location,
      * if not, the key hash was subject to collision */
     hit = GNUNET_NO;
-    ibf_get_indices (ibf, ibf->id_sum[i], buckets);
+    ibf_get_indices (ibf, ibf->key_sum[i], buckets);
     for (j = 0; j < ibf->hash_num; j++)
       if (buckets[j] == i)
         hit = GNUNET_YES;
@@ -216,12 +216,12 @@
       continue;
 
     if (NULL != ret_side)
-      *ret_side = ibf->count[i];
+      *ret_side = ibf->count[i].count_val;
     if (NULL != ret_id)
-      *ret_id = ibf->id_sum[i];
+      *ret_id = ibf->key_sum[i];
 
     /* insert on the opposite side, effectively removing the element */
-    ibf_insert_into (ibf, ibf->id_sum[i], buckets, -ibf->count[i]);
+    ibf_insert_into (ibf, ibf->key_sum[i], buckets, -ibf->count[i].count_val);
 
     return GNUNET_YES;
   }
@@ -233,6 +233,128 @@
 
 
 /**
+ * Write an ibf.
+ * 
+ * @param ibf the ibf to write
+ * @param start with which bucket to start
+ * @param count how many buckets to write
+ * @param buf buffer to write the data to, will be updated to point to the
+ *            first byte after the written data
+ * @param size pointer to the size of the buffer, will be updated, can be NULL
+ */
+void
+ibf_write_slice (const struct InvertibleBloomFilter *ibf, uint32_t start, 
uint32_t count, void **buf, size_t *size)
+{
+  struct IBF_Key *key_dst;
+  struct IBF_KeyHash *key_hash_dst;
+  struct IBF_Count *count_dst;
+
+  /* update size and check for overflow */
+  if (NULL != size)
+  {
+    size_t old_size;
+    old_size = *size;
+    *size = *size - count * IBF_BUCKET_SIZE;
+    GNUNET_assert (*size < old_size);
+  }
+  /* copy keys */
+  key_dst = (struct IBF_Key *) *buf;
+  memcpy (key_dst, ibf->key_sum + start, count * sizeof *key_dst);
+  key_dst += count;
+  /* copy key hashes */
+  key_hash_dst = (struct IBF_KeyHash *) key_dst;
+  memcpy (key_hash_dst, ibf->key_hash_sum + start, count * sizeof 
*key_hash_dst);
+  key_hash_dst += count;
+  /* copy counts */
+  count_dst = (struct IBF_Count *) key_hash_dst;
+  memcpy (count_dst, ibf->count + start, count * sizeof *count_dst);
+  count_dst += count;
+  /* returned buffer is at the end of written data*/
+  *buf = (void *) count_dst;
+}
+
+
+/**
+ * Read an ibf.
+ *
+ * @param buf pointer to the buffer to write to, will point to first
+ *            byte after the written data
+ * @param size size of the buffer, will be updated
+ * @param start which bucket to start at
+ * @param count how many buckets to read
+ * @param dst ibf to write buckets to
+ * @return GNUNET_OK on success
+ */
+int
+ibf_read_slice (void **buf, size_t *size, uint32_t start, uint32_t count, 
struct InvertibleBloomFilter *ibf)
+{
+  struct IBF_Key *key_src;
+  struct IBF_KeyHash *key_hash_src;
+  struct IBF_Count *count_src;
+
+  /* update size and check for overflow */
+  if (NULL != size)
+  {
+    size_t old_size;
+    old_size = *size;
+    *size = *size - count * IBF_BUCKET_SIZE;
+    if (*size > old_size)
+      return GNUNET_SYSERR;
+  }
+  /* copy keys */
+  key_src = (struct IBF_Key *) *buf;
+  memcpy (ibf->key_sum + start, key_src, count * sizeof *key_src);
+  key_src += count;
+  /* copy key hashes */
+  key_hash_src = (struct IBF_KeyHash *) key_src;
+  memcpy (ibf->key_hash_sum + start, key_hash_src, count * sizeof 
*key_hash_src);
+  key_hash_src += count;
+  /* copy counts */
+  count_src = (struct IBF_Count *) key_hash_src;
+  memcpy (ibf->count + start, count_src, count * sizeof *count_src);
+  count_src += count;
+  /* returned buffer is at the end of written data*/
+  *buf = (void *) count_src;
+  return GNUNET_OK;
+}
+
+
+/**
+ * Write an ibf.
+ * 
+ * @param ibf the ibf to write
+ * @param start with which bucket to start
+ * @param count how many buckets to write
+ * @param buf buffer to write the data to, will be updated to point to the
+ *            first byte after the written data
+ * @param size pointer to the size of the buffer, will be updated, can be NULL
+ */
+void
+ibf_write (const struct InvertibleBloomFilter *ibf, void **buf, size_t *size)
+{
+  ibf_write_slice (ibf, 0, ibf->size, buf, size);
+}
+
+
+/**
+ * Read an ibf.
+ *
+ * @param buf pointer to the buffer to write to, will point to first
+ *            byte after the written data
+ * @param size size of the buffer, will be updated
+ * @param start which bucket to start at
+ * @param count how many buckets to read
+ * @param dst ibf to write buckets to
+ * @return GNUNET_OK on success
+ */
+int
+ibf_read (void **buf, size_t *size, struct InvertibleBloomFilter *dst)
+{
+  return ibf_read_slice (buf, size, 0, dst->size, dst);
+}
+
+
+/**
  * Subtract ibf2 from ibf1, storing the result in ibf1.
  * The two IBF's must have the same parameters size and hash_num.
  *
@@ -250,31 +372,33 @@
 
   for (i = 0; i < ibf1->size; i++)
   {
-    ibf1->count[i] -= ibf2->count[i];
-    ibf1->hash_sum[i] ^= ibf2->hash_sum[i];
-    ibf1->id_sum[i] ^= ibf2->id_sum[i];
+    ibf1->count[i].count_val -= ibf2->count[i].count_val;
+    ibf1->key_hash_sum[i].key_hash_val ^= ibf2->key_hash_sum[i].key_hash_val;
+    ibf1->key_sum[i].key_val ^= ibf2->key_sum[i].key_val;
   }
 }
 
+
 /**
  * Create a copy of an IBF, the copy has to be destroyed properly.
  *
  * @param ibf the IBF to copy
  */
 struct InvertibleBloomFilter *
-ibf_dup (struct InvertibleBloomFilter *ibf)
+ibf_dup (const struct InvertibleBloomFilter *ibf)
 {
   struct InvertibleBloomFilter *copy;
   copy = GNUNET_malloc (sizeof *copy);
   copy->hash_num = ibf->hash_num;
   copy->salt = ibf->salt;
   copy->size = ibf->size;
-  copy->hash_sum = GNUNET_memdup (ibf->hash_sum, ibf->size * sizeof (struct 
GNUNET_HashCode));
-  copy->id_sum = GNUNET_memdup (ibf->id_sum, ibf->size * sizeof (struct 
GNUNET_HashCode));
-  copy->count = GNUNET_memdup (ibf->count, ibf->size * sizeof (uint8_t));
+  copy->key_hash_sum = GNUNET_memdup (ibf->key_hash_sum, ibf->size * sizeof 
(struct IBF_KeyHash));
+  copy->key_sum = GNUNET_memdup (ibf->key_sum, ibf->size * sizeof (struct 
IBF_Key));
+  copy->count = GNUNET_memdup (ibf->count, ibf->size * sizeof (struct 
IBF_Count));
   return copy;
 }
 
+
 /**
  * Destroy all resources associated with the invertible bloom filter.
  * No more ibf_*-functions may be called on ibf after calling destroy.
@@ -284,8 +408,9 @@
 void
 ibf_destroy (struct InvertibleBloomFilter *ibf)
 {
-  GNUNET_free (ibf->hash_sum);
-  GNUNET_free (ibf->id_sum);
+  GNUNET_free (ibf->key_sum);
+  GNUNET_free (ibf->key_hash_sum);
   GNUNET_free (ibf->count);
   GNUNET_free (ibf);
 }
+

Modified: gnunet/src/consensus/ibf.h
===================================================================
--- gnunet/src/consensus/ibf.h  2013-03-07 12:30:28 UTC (rev 26349)
+++ gnunet/src/consensus/ibf.h  2013-03-07 14:12:11 UTC (rev 26350)
@@ -39,12 +39,28 @@
 #endif
 #endif
 
+
+struct IBF_Key
+{
+  uint64_t key_val;
+};
+
+struct IBF_KeyHash
+{
+  uint32_t key_hash_val;
+};
+
+struct IBF_Count
+{
+  int8_t count_val;
+};
+
 /**
  * Size of one ibf bucket in bytes
  */
-#define IBF_BUCKET_SIZE (8+4+1)
+#define IBF_BUCKET_SIZE (sizeof (struct IBF_Count) + sizeof (struct IBF_Key) + 
\
+    sizeof (struct IBF_KeyHash))
 
-
 /**
  * Invertible bloom filter (IBF).
  *
@@ -62,7 +78,7 @@
    * In how many cells do we hash one element?
    * Usually 4 or 3.
    */
-  unsigned int hash_num;
+  uint8_t hash_num;
 
   /**
    * Salt for mingling hashes
@@ -70,30 +86,91 @@
   uint32_t salt;
 
   /**
-   * xor sums of the elements' hash codes, used to identify the elements.
+   * Xor sums of the elements' keys, used to identify the elements.
+   * Array of 'size' elements.
    */
-  uint64_t *id_sum;
+  struct IBF_Key *key_sum;
 
   /**
-   * xor sums of the "hash of the hash".
+   * Xor sums of the hashes of the keys of inserted elements.
+   * Array of 'size' elements.
    */
-  uint32_t *hash_sum;
+  struct IBF_KeyHash *key_hash_sum;
 
   /**
    * How many times has a bucket been hit?
    * Can be negative, as a result of IBF subtraction.
+   * Array of 'size' elements.
    */
-  int8_t *count;
+  struct IBF_Count *count;
 };
 
 
 /**
+ * Write an ibf.
+ * 
+ * @param ibf the ibf to write
+ * @param start with which bucket to start
+ * @param count how many buckets to write
+ * @param buf buffer to write the data to, will be updated to point to the
+ *            first byte after the written data
+ * @param size pointer to the size of the buffer, will be updated, can be NULL
+ */
+void
+ibf_write_slice (const struct InvertibleBloomFilter *ibf, uint32_t start, 
uint32_t count, void **buf, size_t *size);
+
+
+/**
+ * Read an ibf.
+ *
+ * @param buf pointer to the buffer to write to, will point to first
+ *            byte after the written data
+ * @param size size of the buffer, will be updated
+ * @param start which bucket to start at
+ * @param count how many buckets to read
+ * @param dst ibf to write buckets to
+ * @return GNUNET_OK on success
+ */
+int
+ibf_read_slice (void **buf, size_t *size, uint32_t start, uint32_t count, 
struct InvertibleBloomFilter *dst);
+
+
+/**
+ * Write an ibf.
+ * 
+ * @param ibf the ibf to write
+ * @param start with which bucket to start
+ * @param count how many buckets to write
+ * @param buf buffer to write the data to, will be updated to point to the
+ *            first byte after the written data
+ * @param size pointer to the size of the buffer, will be updated, can be NULL
+ */
+void
+ibf_write (const struct InvertibleBloomFilter *ibf, void **buf, size_t *size);
+
+
+/**
+ * Read an ibf.
+ *
+ * @param buf pointer to the buffer to write to, will point to first
+ *            byte after the written data
+ * @param size size of the buffer, will be updated
+ * @param start which bucket to start at
+ * @param count how many buckets to read
+ * @param dst ibf to write buckets to
+ * @return GNUNET_OK on success
+ */
+int
+ibf_read (void **buf, size_t *size, struct InvertibleBloomFilter *dst);
+
+
+/**
  * Create a key from a hashcode.
  *
  * @param hash the hashcode
  * @return a key
  */
-uint64_t
+struct IBF_Key
 ibf_key_from_hashcode (const struct GNUNET_HashCode *hash);
 
 
@@ -105,7 +182,7 @@
  * @param dst hashcode to store the result in
  */
 void
-ibf_hashcode_from_key (uint64_t key, struct GNUNET_HashCode *dst);
+ibf_hashcode_from_key (struct IBF_Key key, struct GNUNET_HashCode *dst);
 
 
 /**
@@ -118,17 +195,17 @@
  * @return the newly created invertible bloom filter
  */
 struct InvertibleBloomFilter *
-ibf_create(uint32_t size, unsigned int hash_num, uint32_t salt);
+ibf_create (uint32_t size, uint8_t hash_num, uint32_t salt);
 
 
 /**
  * Insert an element into an IBF.
  *
  * @param ibf the IBF
- * @param id the element's hash code
+ * @param key the element's hash code
  */
 void
-ibf_insert (struct InvertibleBloomFilter *ibf, uint64_t id);
+ibf_insert (struct InvertibleBloomFilter *ibf, struct IBF_Key key);
 
 
 /**
@@ -154,7 +231,7 @@
  *         GNUNET_SYSERR if the decoding has faile
  */
 int
-ibf_decode (struct InvertibleBloomFilter *ibf, int *side, uint64_t *ret_id);
+ibf_decode (struct InvertibleBloomFilter *ibf, int *side, struct IBF_Key 
*ret_key);
 
 
 /**
@@ -163,7 +240,7 @@
  * @param ibf the IBF to copy
  */
 struct InvertibleBloomFilter *
-ibf_dup (struct InvertibleBloomFilter *ibf);
+ibf_dup (const struct InvertibleBloomFilter *ibf);
 
 /**
  * Destroy all resources associated with the invertible bloom filter.

Modified: gnunet/src/include/gnunet_consensus_service.h
===================================================================
--- gnunet/src/include/gnunet_consensus_service.h       2013-03-07 12:30:28 UTC 
(rev 26349)
+++ gnunet/src/include/gnunet_consensus_service.h       2013-03-07 14:12:11 UTC 
(rev 26350)
@@ -20,7 +20,7 @@
 
 /**
  * @file include/gnunet_consensus_service.h
- * @brief 
+ * @brief multi-peer set reconciliation
  * @author Florian Dold
  */
 
@@ -65,9 +65,7 @@
 
 /**
  * Called when a new element was received from another peer, or an error 
occured.
- *
  * May deliver duplicate values.
- *
  * Elements given to a consensus operation by the local peer are NOT given
  * to this callback.
  *
@@ -147,32 +145,17 @@
                          void *idc_cls);
 
 
-/**
- * Begin reconciling elements with other peers.
- * May not be called if an insert operation has not yet finished.
- *
- * @param consensus handle for the consensus session
- */
-void
-GNUNET_CONSENSUS_begin (struct GNUNET_CONSENSUS_Handle *consensus);
-
-
-
 struct GNUNET_CONSENSUS_DeltaRequest;
 
 /**
- * We are finished inserting new elements into the consensus;
- * try to conclude the consensus within a given time window.
+ * FIXME
  *
  * @param consensus consensus session
- * @param timeout timeout after which the conculde callback
- *                must be called
- * @param conclude called when the conclusion was successful
- * @param conclude_cls closure for the conclude callback
+ * @param remove_element_cb callback that receives the removed elements
+ * @return a handle to cancel the request
  */
 struct GNUNET_CONSENSUS_DeltaRequest *
 GNUNET_CONSENSUS_get_delta (struct GNUNET_CONSENSUS_Handle *consensus,
-                            uint32_t group_id,
                             GNUNET_CONSENSUS_ElementCallback remove_element_cb,
                             void *remove_element_cb_cls);
 
@@ -194,9 +177,8 @@
  *
  * @param cls
  * @param group
- * @return GNUNET_YES if more consensus groups should be offered, GNUNET_NO if 
not
  */
-typedef int (*GNUNET_CONSENSUS_ConcludeCallback) (void *cls, const struct 
GNUNET_CONSENSUS_Group *group);
+typedef void (*GNUNET_CONSENSUS_ConcludeCallback) (void *cls, const struct 
GNUNET_CONSENSUS_Group *group);
 
 
 /**

Modified: gnunet/src/include/gnunet_protocols.h
===================================================================
--- gnunet/src/include/gnunet_protocols.h       2013-03-07 12:30:28 UTC (rev 
26349)
+++ gnunet/src/include/gnunet_protocols.h       2013-03-07 14:12:11 UTC (rev 
26350)
@@ -1684,20 +1684,29 @@
 #define GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_DIFFERENCE_DIGEST 542
 
 /**
- * Elements, and requests for further elements
+ * One or more elements that are sent from peer to peer.
  */
 #define GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS 543
 
-
 /**
  * Elements, and requests for further elements
  */
 #define GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_REQUEST 544
 
+/**
+ * Elements that a peer misses locally.
+ */
+#define GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_MISSING_LOCAL 545
+
+/**
+ * Elements that a peer reports to be missing at the remote peer.
+ */
+#define GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_MISSING_REMOTE 546
+
 /*
  * Initialization message for consensus p2p communication.
  */
-#define GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_HELLO 544
+#define GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_HELLO 547
 
 
 /**




reply via email to

[Prev in Thread] Current Thread [Next in Thread]