2 This file is part of GNUnet
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file consensus/gnunet-service-consensus.c
23 * @brief multi-peer set reconciliation
24 * @author Florian Dold
28 #include "gnunet_common.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_applications.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_set_service.h"
33 #include "gnunet_consensus_service.h"
34 #include "consensus_protocol.h"
35 #include "consensus.h"
39 * Log macro that prefixes the local peer and the peer we are in contact with.
41 #define LOG_PP(kind, cpi, m,...) GNUNET_log (kind, "P%d for P%d: " m, \
42 cpi->session->local_peer_idx, (int) (cpi - cpi->session->info),##__VA_ARGS__)
46 * Number of exponential rounds, used in the exp and completion round.
48 #define NUM_EXP_ROUNDS 4
50 /* forward declarations */
52 /* mutual recursion with struct ConsensusSession */
53 struct ConsensusPeerInformation;
55 /* mutual recursion with round_over */
57 subround_over (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
61 * Describes the current round a consensus session is in.
66 * Not started the protocol yet.
68 CONSENSUS_ROUND_BEGIN=0,
70 * Distribution of elements with the exponential scheme.
72 CONSENSUS_ROUND_EXCHANGE,
74 * Exchange which elements each peer has, but don't
75 * transmit the element's data, only their SHA-512 hashes.
76 * This round uses the all-to-all scheme.
78 CONSENSUS_ROUND_INVENTORY,
80 * Collect and distribute missing values with the exponential scheme.
82 CONSENSUS_ROUND_COMPLETION,
84 * Consensus concluded. After timeout and finished communication with client,
85 * consensus session will be destroyed.
87 CONSENSUS_ROUND_FINISH
92 * Complete information about the current round and all
98 * The current main round.
100 enum ConsensusRound round;
102 * The current exp round, valid if
103 * the main round is an exp round.
107 * The current exp subround, valid if
108 * the main round is an exp round.
110 uint32_t exp_subround;
115 * A consensus session consists of one local client and the remote authorities.
117 struct ConsensusSession
120 * Consensus sessions are kept in a DLL.
122 struct ConsensusSession *next;
125 * Consensus sessions are kept in a DLL.
127 struct ConsensusSession *prev;
130 * Global consensus identification, computed
131 * from the session id and participating authorities.
133 struct GNUNET_HashCode global_id;
136 * Client that inhabits the session
138 struct GNUNET_SERVER_Client *client;
141 * Queued messages to the client.
143 struct GNUNET_MQ_Handle *client_mq;
146 * Timeout for all rounds together, single rounds will schedule a timeout task
147 * with a fraction of the conclude timeout.
148 * Only valid once the current round is not CONSENSUS_ROUND_BEGIN.
150 struct GNUNET_TIME_Relative conclude_timeout;
153 * Timeout task identifier for the current round.
155 GNUNET_SCHEDULER_TaskIdentifier round_timeout_tid;
158 * Number of other peers in the consensus.
160 unsigned int num_peers;
163 * Information about the other peers,
166 struct ConsensusPeerInformation *info;
169 * Index of the local peer in the peers array
171 unsigned int local_peer_idx;
176 enum ConsensusRound current_round;
179 * Permutation of peers for the current round,
180 * maps logical index (for current round) to physical index (location in info array)
185 * Current round of the exponential scheme.
190 * Current sub-round of the exponential scheme.
192 uint32_t exp_subround;
195 * The partner for the current exp-round
197 struct ConsensusPeerInformation *partner_outgoing;
200 * The partner for the current exp-round
202 struct ConsensusPeerInformation *partner_incoming;
205 * The consensus set of this session.
207 struct GNUNET_SET_Handle *element_set;
210 * Listener for requests from other peers.
211 * Uses the session's global id as app id.
213 struct GNUNET_SET_ListenHandle *set_listener;
218 * Information about a peer that is in a consensus session.
220 struct ConsensusPeerInformation
223 * Peer identitty of the peer in the consensus session
225 struct GNUNET_PeerIdentity peer_id;
228 * Back-reference to the consensus session,
229 * to that ConsensusPeerInformation can be used as a closure
231 struct ConsensusSession *session;
234 * We have finishes the exp-subround with the peer.
236 int exp_subround_finished;
239 * Set operation we are currently executing with this peer.
241 struct GNUNET_SET_OperationHandle *set_op;
244 * Set operation we are planning on executing with this peer.
246 struct GNUNET_SET_OperationHandle *delayed_set_op;
249 * Info about the round of the delayed set operation.
251 struct RoundInfo delayed_round_info;
256 * Linked list of sessions this peer participates in.
258 static struct ConsensusSession *sessions_head;
261 * Linked list of sessions this peer participates in.
263 static struct ConsensusSession *sessions_tail;
266 * Configuration of the consensus service.
268 static const struct GNUNET_CONFIGURATION_Handle *cfg;
271 * Handle to the server for this service.
273 static struct GNUNET_SERVER_Handle *srv;
276 * Peer that runs this service.
278 static struct GNUNET_PeerIdentity my_peer;
282 have_exp_subround_finished (const struct ConsensusSession *session)
286 if ( (NULL != session->partner_outgoing) &&
287 (GNUNET_NO == session->partner_outgoing->exp_subround_finished) )
289 if ( (NULL != session->partner_incoming) &&
290 (GNUNET_NO == session->partner_incoming->exp_subround_finished) )
292 if (0 == not_finished)
299 * Destroy a session, free all resources associated with it.
301 * @param session the session to destroy
304 destroy_session (struct ConsensusSession *session)
308 GNUNET_CONTAINER_DLL_remove (sessions_head, sessions_tail, session);
309 if (NULL != session->element_set)
311 GNUNET_SET_destroy (session->element_set);
312 session->element_set = NULL;
314 if (NULL != session->set_listener)
316 GNUNET_SET_listen_cancel (session->set_listener);
317 session->set_listener = NULL;
319 if (NULL != session->client_mq)
321 GNUNET_MQ_destroy (session->client_mq);
322 session->client_mq = NULL;
324 if (NULL != session->client)
326 GNUNET_SERVER_client_disconnect (session->client);
327 session->client = NULL;
329 if (NULL != session->shuffle)
331 GNUNET_free (session->shuffle);
332 session->shuffle = NULL;
334 if (NULL != session->info)
336 for (i = 0; i < session->num_peers; i++)
338 struct ConsensusPeerInformation *cpi;
339 cpi = &session->info[i];
340 if (NULL != cpi->set_op)
342 GNUNET_SET_operation_cancel (cpi->set_op);
346 GNUNET_free (session->info);
347 session->info = NULL;
349 GNUNET_free (session);
354 * Iterator for set elements.
357 * @param element the current element, NULL if all elements have been
359 * @return GNUNET_YES to continue iterating, GNUNET_NO to stop.
362 send_to_client_iter (void *cls,
363 const struct GNUNET_SET_Element *element)
365 struct ConsensusSession *session = cls;
366 struct GNUNET_MQ_Envelope *ev;
370 struct GNUNET_CONSENSUS_ElementMessage *m;
372 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d: got element for client\n",
373 session->local_peer_idx);
375 ev = GNUNET_MQ_msg_extra (m, element->size, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT);
376 m->element_type = htons (element->type);
377 memcpy (&m[1], element->data, element->size);
378 GNUNET_MQ_send (session->client_mq, ev);
382 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d: finished iterating elements for client\n",
383 session->local_peer_idx);
384 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE);
385 GNUNET_MQ_send (session->client_mq, ev);
392 * Start the next round.
393 * This function can be invoked as a timeout task, or called manually (tc will be NULL then).
395 * @param cls the session
396 * @param tc task context, for when this task is invoked by the scheduler,
397 * NULL if invoked for another reason
400 round_over (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
402 struct ConsensusSession *session;
404 /* don't kick off next round if we're shutting down */
405 if ((NULL != tc) && (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
409 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d: round over\n", session->local_peer_idx);
411 if (session->round_timeout_tid != GNUNET_SCHEDULER_NO_TASK)
413 GNUNET_SCHEDULER_cancel (session->round_timeout_tid);
414 session->round_timeout_tid = GNUNET_SCHEDULER_NO_TASK;
417 switch (session->current_round)
419 case CONSENSUS_ROUND_BEGIN:
420 session->current_round = CONSENSUS_ROUND_EXCHANGE;
421 session->exp_round = 0;
422 subround_over (session, NULL);
424 case CONSENSUS_ROUND_EXCHANGE:
425 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d: finished, sending elements to client\n",
426 session->local_peer_idx);
427 session->current_round = CONSENSUS_ROUND_FINISH;
428 GNUNET_SET_iterate (session->element_set, send_to_client_iter, session);
437 * Create a new permutation for the session's peers in session->shuffle.
438 * Uses a Fisher-Yates shuffle with pseudo-randomness coming from
439 * both the global session id and the current round index.
441 * @param session the session to create the new permutation for
444 shuffle (struct ConsensusSession *session)
447 uint32_t randomness[session->num_peers-1];
449 if (NULL == session->shuffle)
450 session->shuffle = GNUNET_malloc (session->num_peers * sizeof (*session->shuffle));
452 GNUNET_CRYPTO_kdf (randomness, sizeof (randomness),
453 &session->exp_round, sizeof (uint32_t),
454 &session->global_id, sizeof (struct GNUNET_HashCode),
457 for (i = 0; i < session->num_peers; i++)
458 session->shuffle[i] = i;
460 for (i = session->num_peers - 1; i > 0; i--)
464 x = randomness[i-1] % session->num_peers;
465 tmp = session->shuffle[x];
466 session->shuffle[x] = session->shuffle[i];
467 session->shuffle[i] = tmp;
473 * Find and set the partner_incoming and partner_outgoing of our peer,
474 * one of them may not exist (and thus set to NULL) if the number of peers
475 * in the session is not a power of two.
477 * @param session the consensus session
480 find_partners (struct ConsensusSession *session)
483 unsigned int num_ghosts;
484 unsigned int largest_arc;
487 /* shuffled local index */
488 int my_idx = session->shuffle[session->local_peer_idx];
490 /* distance to neighboring peer in current subround */
491 arc = 1 << session->exp_subround;
493 while (largest_arc < session->num_peers)
495 num_ghosts = largest_arc - session->num_peers;
497 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "num ghosts: %d\n", num_ghosts);
499 if (0 == (my_idx & arc))
501 /* we are outgoing */
502 partner_idx = (my_idx + arc) % session->num_peers;
503 session->partner_outgoing = &session->info[session->shuffle[partner_idx]];
504 session->partner_outgoing->exp_subround_finished = GNUNET_NO;
505 /* are we a 'ghost' of a peer that would exist if
506 * the number of peers was a power of two, and thus have to partner
507 * with an additional peer?
509 if (my_idx < num_ghosts)
511 int ghost_partner_idx;
512 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "my index %d, arc %d, peers %u\n", my_idx, arc, session->num_peers);
513 ghost_partner_idx = (my_idx - (int) arc) % (int) session->num_peers;
514 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ghost partner is before %d\n", ghost_partner_idx);
515 /* platform dependent; modulo sometimes returns negative values */
516 if (ghost_partner_idx < 0)
517 ghost_partner_idx += session->num_peers;
518 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ghost partner is after %d\n", ghost_partner_idx);
519 session->partner_incoming = &session->info[session->shuffle[ghost_partner_idx]];
520 session->partner_incoming->exp_subround_finished = GNUNET_NO;
523 session->partner_incoming = NULL;
526 partner_idx = (my_idx - (int) arc) % (int) session->num_peers;
528 partner_idx += session->num_peers;
529 session->partner_outgoing = NULL;
530 session->partner_incoming = &session->info[session->shuffle[partner_idx]];
531 session->partner_incoming->exp_subround_finished = GNUNET_NO;
536 * Callback for set operation results. Called for each element
540 * @param element a result element, only valid if status is GNUNET_SET_STATUS_OK
541 * @param status see enum GNUNET_SET_Status
544 set_result_cb (void *cls,
545 const struct GNUNET_SET_Element *element,
546 enum GNUNET_SET_Status status)
548 struct ConsensusPeerInformation *cpi = cls;
549 unsigned int remote_idx = cpi - cpi->session->info;
550 unsigned int local_idx = cpi->session->local_peer_idx;
552 GNUNET_assert ((cpi == cpi->session->partner_outgoing) ||
553 (cpi == cpi->session->partner_incoming));
557 case GNUNET_SET_STATUS_OK:
558 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: set result from P%u: element\n",
559 local_idx, remote_idx);
561 case GNUNET_SET_STATUS_FAILURE:
562 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: set result from P%u: failure\n",
563 local_idx, remote_idx);
566 case GNUNET_SET_STATUS_HALF_DONE:
567 case GNUNET_SET_STATUS_DONE:
568 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: set result from P%u: done\n",
569 local_idx, remote_idx);
570 cpi->exp_subround_finished = GNUNET_YES;
572 if (have_exp_subround_finished (cpi->session) == GNUNET_YES)
574 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: all reconciliations of subround done\n",
576 subround_over (cpi->session, NULL);
580 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: waiting for further set results\n",
589 switch (cpi->session->current_round)
591 case CONSENSUS_ROUND_EXCHANGE:
592 GNUNET_SET_add_element (cpi->session->element_set, element, NULL, NULL);
602 * Compare the round the session is in with the round of the given context message.
604 * @param session a consensus session
605 * @param round a round context message
606 * @return 0 if it's the same round, -1 if the session is in an earlier round,
607 * 1 if the session is in a later round
610 rounds_compare (struct ConsensusSession *session,
611 struct RoundInfo* ri)
613 if (session->current_round < ri->round)
615 if (session->current_round > ri->round)
617 if (session->current_round == CONSENSUS_ROUND_EXCHANGE)
619 if (session->exp_round < ri->exp_round)
621 if (session->exp_round > ri->exp_round)
623 if (session->exp_subround < ri->exp_subround)
625 if (session->exp_subround < ri->exp_subround)
629 /* comparing rounds when we are not in a exp round */
635 * Do the next subround in the exp-scheme.
636 * This function can be invoked as a timeout task, or called manually (tc will be NULL then).
638 * @param cls the session
639 * @param tc task context, for when this task is invoked by the scheduler,
640 * NULL if invoked for another reason
643 subround_over (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
645 struct ConsensusSession *session;
648 /* don't kick off next subround if we're shutting down */
649 if ((NULL != tc) && (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
653 if (session->round_timeout_tid != GNUNET_SCHEDULER_NO_TASK)
655 GNUNET_SCHEDULER_cancel (session->round_timeout_tid);
656 session->round_timeout_tid = GNUNET_SCHEDULER_NO_TASK;
659 if (session->exp_round >= NUM_EXP_ROUNDS)
661 round_over (session, NULL);
665 if (session->exp_round == 0)
667 /* initialize everything for the log-rounds */
668 session->exp_round = 1;
669 session->exp_subround = 0;
670 if (NULL == session->shuffle)
671 session->shuffle = GNUNET_malloc ((sizeof (int)) * session->num_peers);
672 for (i = 0; i < session->num_peers; i++)
673 session->shuffle[i] = i;
675 else if (session->exp_subround + 1 >= (int) ceil (log2 (session->num_peers)))
677 /* subrounds done, start new log-round */
678 session->exp_round++;
679 session->exp_subround = 0;
684 session->exp_subround++;
687 /* determine the incoming and outgoing partner */
688 find_partners (session);
690 GNUNET_assert (session->partner_outgoing != &session->info[session->local_peer_idx]);
691 GNUNET_assert (session->partner_incoming != &session->info[session->local_peer_idx]);
693 /* initiate set operation with the outgoing partner */
694 if (NULL != session->partner_outgoing)
696 struct GNUNET_CONSENSUS_RoundContextMessage *msg;
697 msg = GNUNET_new (struct GNUNET_CONSENSUS_RoundContextMessage);
698 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ROUND_CONTEXT);
699 msg->header.size = htons (sizeof *msg);
700 msg->round = htonl (session->current_round);
701 msg->exp_round = htonl (session->exp_round);
702 msg->exp_subround = htonl (session->exp_subround);
704 if (NULL != session->partner_outgoing->set_op)
706 GNUNET_SET_operation_cancel (session->partner_outgoing->set_op);
708 session->partner_outgoing->set_op =
709 GNUNET_SET_prepare (&session->partner_outgoing->peer_id,
711 (struct GNUNET_MessageHeader *) msg,
713 GNUNET_SET_RESULT_ADDED,
714 set_result_cb, session->partner_outgoing);
716 GNUNET_SET_commit (session->partner_outgoing->set_op, session->element_set);
719 /* commit to the delayed set operation */
720 if ((NULL != session->partner_incoming) && (NULL != session->partner_incoming->delayed_set_op))
722 int cmp = rounds_compare (session, &session->partner_incoming->delayed_round_info);
724 if (NULL != session->partner_incoming->set_op)
726 GNUNET_SET_operation_cancel (session->partner_incoming->set_op);
727 session->partner_incoming->set_op = NULL;
731 GNUNET_SET_commit (session->partner_incoming->delayed_set_op, session->element_set);
732 session->partner_incoming->set_op = session->partner_incoming->delayed_set_op;
733 session->partner_incoming->delayed_set_op = NULL;
734 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d resumed delayed round with P%d\n",
735 session->local_peer_idx, (int) (session->partner_incoming - session->info));
739 /* this should not happen -- a round has been skipped! */
744 #ifdef GNUNET_EXTRA_LOGGING
748 if (session->partner_outgoing == NULL)
751 out = (int) (session->partner_outgoing - session->info);
752 if (session->partner_incoming == NULL)
755 in = (int) (session->partner_incoming - session->info);
756 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: doing exp-round, r=%d, sub=%d, in: %d, out: %d\n", session->local_peer_idx,
757 session->exp_round, session->exp_subround, in, out);
759 #endif /* GNUNET_EXTRA_LOGGING */
765 * Search peer in the list of peers in session.
767 * @param peer peer to find
768 * @param session session with peer
769 * @return index of peer, -1 if peer is not in session
772 get_peer_idx (const struct GNUNET_PeerIdentity *peer, const struct ConsensusSession *session)
775 for (i = 0; i < session->num_peers; i++)
776 if (0 == memcmp (peer, &session->info[i].peer_id, sizeof *peer))
783 * Compute a global, (hopefully) unique consensus session id,
784 * from the local id of the consensus session, and the identities of all participants.
785 * Thus, if the local id of two consensus sessions coincide, but are not comprised of
786 * exactly the same peers, the global id will be different.
788 * @param session session to generate the global id for
789 * @param session_id local id of the consensus session
792 compute_global_id (struct ConsensusSession *session, const struct GNUNET_HashCode *session_id)
795 struct GNUNET_HashCode tmp;
797 /* FIXME: use kdf? */
799 session->global_id = *session_id;
800 for (i = 0; i < session->num_peers; ++i)
802 GNUNET_CRYPTO_hash_xor (&session->global_id, &session->info[i].peer_id.hashPubKey, &tmp);
803 session->global_id = tmp;
804 GNUNET_CRYPTO_hash (&session->global_id, sizeof (struct GNUNET_PeerIdentity), &tmp);
805 session->global_id = tmp;
811 * Although GNUNET_CRYPTO_hash_cmp exisits, it does not have
812 * the correct signature to be used with e.g. qsort.
813 * We use this function instead.
815 * @param h1 some hash code
816 * @param h2 some hash code
817 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
820 hash_cmp (const void *h1, const void *h2)
822 return GNUNET_CRYPTO_hash_cmp ((struct GNUNET_HashCode *) h1, (struct GNUNET_HashCode *) h2);
827 * Create the sorted list of peers for the session,
828 * add the local peer if not in the join message.
831 initialize_session_peer_list (struct ConsensusSession *session,
832 struct GNUNET_CONSENSUS_JoinMessage *join_msg)
834 unsigned int local_peer_in_list;
835 uint32_t listed_peers;
836 const struct GNUNET_PeerIdentity *msg_peers;
837 struct GNUNET_PeerIdentity *peers;
840 GNUNET_assert (NULL != join_msg);
842 /* peers in the join message, may or may not include the local peer */
843 listed_peers = ntohl (join_msg->num_peers);
845 session->num_peers = listed_peers;
847 msg_peers = (struct GNUNET_PeerIdentity *) &join_msg[1];
849 local_peer_in_list = GNUNET_NO;
850 for (i = 0; i < listed_peers; i++)
852 if (0 == memcmp (&msg_peers[i], &my_peer, sizeof (struct GNUNET_PeerIdentity)))
854 local_peer_in_list = GNUNET_YES;
859 if (GNUNET_NO == local_peer_in_list)
860 session->num_peers++;
862 peers = GNUNET_malloc (session->num_peers * sizeof (struct GNUNET_PeerIdentity));
864 if (GNUNET_NO == local_peer_in_list)
865 peers[session->num_peers - 1] = my_peer;
867 memcpy (peers, msg_peers, listed_peers * sizeof (struct GNUNET_PeerIdentity));
868 qsort (peers, session->num_peers, sizeof (struct GNUNET_PeerIdentity), &hash_cmp);
870 session->info = GNUNET_malloc (session->num_peers * sizeof (struct ConsensusPeerInformation));
872 for (i = 0; i < session->num_peers; ++i)
874 /* initialize back-references, so consensus peer information can
875 * be used as closure */
876 session->info[i].session = session;
877 session->info[i].peer_id = peers[i];
885 * Called when another peer wants to do a set operation with the
889 * @param other_peer the other peer
890 * @param context_msg message with application specific information from
892 * @param request request from the other peer, use GNUNET_SET_accept
893 * to accept it, otherwise the request will be refused
894 * Note that we don't use a return value here, as it is also
895 * necessary to specify the set we want to do the operation with,
896 * whith sometimes can be derived from the context message.
897 * Also necessary to specify the timeout.
900 set_listen_cb (void *cls,
901 const struct GNUNET_PeerIdentity *other_peer,
902 const struct GNUNET_MessageHeader *context_msg,
903 struct GNUNET_SET_Request *request)
905 struct ConsensusSession *session = cls;
906 struct GNUNET_CONSENSUS_RoundContextMessage *msg = (struct GNUNET_CONSENSUS_RoundContextMessage *) context_msg;
907 struct ConsensusPeerInformation *cpi;
908 struct GNUNET_SET_OperationHandle *set_op;
909 struct RoundInfo round_info;
913 if (NULL == context_msg)
919 index = get_peer_idx (other_peer, session);
927 round_info.round = ntohl (msg->round);
928 round_info.exp_round = ntohl (msg->exp_round);
929 round_info.exp_subround = ntohl (msg->exp_subround);
931 cpi = &session->info[index];
933 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d got set request from P%d\n", session->local_peer_idx, index);
935 switch (session->current_round)
937 case CONSENSUS_ROUND_EXCHANGE:
938 cmp = rounds_compare (session, &round_info);
941 /* the other peer is too late */
945 /* kill old request, if any. this is legal,
946 * as the other peer would not make a new request if it would want to
947 * complete the old one! */
948 if (NULL != cpi->set_op)
950 GNUNET_SET_operation_cancel (cpi->set_op);
953 set_op = GNUNET_SET_accept (request, GNUNET_SET_RESULT_ADDED,
954 set_result_cb, &session->info[index]);
957 cpi->set_op = set_op;
958 GNUNET_SET_commit (set_op, session->element_set);
959 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d commited to set request from P%d\n", session->local_peer_idx, index);
963 /* if there's a exp subround running, mark it as finished, as the set op has been canceled! */
964 cpi->delayed_set_op = set_op;
965 cpi->delayed_round_info = round_info;
966 cpi->exp_subround_finished = GNUNET_YES;
967 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d delaying set request from P%d\n", session->local_peer_idx, index);
978 * Initialize the session, continue receiving messages from the owning client
980 * @param session the session to initialize
981 * @param join_msg the join message from the client
984 initialize_session (struct ConsensusSession *session,
985 struct GNUNET_CONSENSUS_JoinMessage *join_msg)
987 struct ConsensusSession *other_session;
989 initialize_session_peer_list (session, join_msg);
990 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "session with %u peers\n", session->num_peers);
991 compute_global_id (session, &join_msg->session_id);
993 /* check if some local client already owns the session.
994 * it is only legal to have a session with an existing global id
995 * if all other sessions with this global id are finished.*/
996 other_session = sessions_head;
997 while (NULL != other_session)
999 if ((other_session != session) &&
1000 (0 == GNUNET_CRYPTO_hash_cmp (&session->global_id, &other_session->global_id)))
1002 if (CONSENSUS_ROUND_FINISH != other_session->current_round)
1005 destroy_session (session);
1010 other_session = other_session->next;
1013 session->local_peer_idx = get_peer_idx (&my_peer, session);
1014 GNUNET_assert (-1 != session->local_peer_idx);
1015 session->element_set = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
1016 GNUNET_assert (NULL != session->element_set);
1017 session->set_listener = GNUNET_SET_listen (cfg, GNUNET_SET_OPERATION_UNION,
1018 &session->global_id,
1019 set_listen_cb, session);
1020 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%d is the local peer\n", session->local_peer_idx);
1021 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "session %s initialized\n", GNUNET_h2s (&session->global_id));
1025 static struct ConsensusSession *
1026 get_session_by_client (struct GNUNET_SERVER_Client *client)
1028 struct ConsensusSession *session;
1030 session = sessions_head;
1031 while (NULL != session)
1033 if (session->client == client)
1035 session = session->next;
1042 * Called when a client wants to join a consensus session.
1045 * @param client client that sent the message
1046 * @param m message sent by the client
1049 client_join (void *cls,
1050 struct GNUNET_SERVER_Client *client,
1051 const struct GNUNET_MessageHeader *m)
1053 struct ConsensusSession *session;
1055 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "join message sent by client\n");
1057 session = get_session_by_client (client);
1058 if (NULL != session)
1061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1064 session = GNUNET_new (struct ConsensusSession);
1065 session->client = client;
1066 session->client_mq = GNUNET_MQ_queue_for_server_client (client);
1067 GNUNET_CONTAINER_DLL_insert (sessions_head, sessions_tail, session);
1068 initialize_session (session, (struct GNUNET_CONSENSUS_JoinMessage *) m);
1069 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1071 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "join done\n");
1076 * Called when a client performs an insert operation.
1078 * @param cls (unused)
1079 * @param client client handle
1080 * @param m message sent by the client
1083 client_insert (void *cls,
1084 struct GNUNET_SERVER_Client *client,
1085 const struct GNUNET_MessageHeader *m)
1087 struct ConsensusSession *session;
1088 struct GNUNET_CONSENSUS_ElementMessage *msg;
1089 struct GNUNET_SET_Element *element;
1090 ssize_t element_size;
1092 session = get_session_by_client (client);
1094 if (NULL == session)
1097 GNUNET_SERVER_client_disconnect (client);
1101 if (CONSENSUS_ROUND_BEGIN != session->current_round)
1104 GNUNET_SERVER_client_disconnect (client);
1108 msg = (struct GNUNET_CONSENSUS_ElementMessage *) m;
1109 element_size = ntohs (msg->header.size) - sizeof (struct GNUNET_CONSENSUS_ElementMessage);
1110 if (element_size < 0)
1116 element = GNUNET_malloc (sizeof (struct GNUNET_SET_Element) + element_size);
1117 element->type = msg->element_type;
1118 element->size = element_size;
1119 memcpy (&element[1], &msg[1], element_size);
1120 element->data = &element[1];
1121 GNUNET_SET_add_element (session->element_set, element, NULL, NULL);
1122 GNUNET_free (element);
1123 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1125 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: element added\n", session->local_peer_idx);
1130 * Called when a client performs the conclude operation.
1132 * @param cls (unused)
1133 * @param client client handle
1134 * @param message message sent by the client
1137 client_conclude (void *cls,
1138 struct GNUNET_SERVER_Client *client,
1139 const struct GNUNET_MessageHeader *message)
1141 struct ConsensusSession *session;
1142 struct GNUNET_CONSENSUS_ConcludeMessage *cmsg;
1145 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "conclude requested\n");
1146 cmsg = (struct GNUNET_CONSENSUS_ConcludeMessage *) message;
1147 session = get_session_by_client (client);
1148 if (NULL == session)
1150 /* client not found */
1152 GNUNET_SERVER_client_disconnect (client);
1155 if (CONSENSUS_ROUND_BEGIN != session->current_round)
1157 /* client requested conclude twice */
1161 if (session->num_peers <= 1)
1163 /* FIXME: what to do here? */
1164 //send_client_conclude_done (session);
1168 session->conclude_timeout = GNUNET_TIME_relative_ntoh (cmsg->timeout);
1169 /* the 'begin' round is over, start with the next, actual round */
1170 round_over (session, NULL);
1173 GNUNET_assert (CONSENSUS_ROUND_BEGIN != session->current_round);
1174 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1179 * Called to clean up, after a shutdown has been requested.
1181 * @param cls closure
1182 * @param tc context information (why was this task triggered now)
1185 shutdown_task (void *cls,
1186 const struct GNUNET_SCHEDULER_TaskContext *tc)
1188 while (NULL != sessions_head)
1189 destroy_session (sessions_head);
1191 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "handled shutdown request\n");
1196 * Clean up after a client after it is
1197 * disconnected (either by us or by itself)
1199 * @param cls closure, unused
1200 * @param client the client to clean up after
1203 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
1205 struct ConsensusSession *session;
1207 session = get_session_by_client (client);
1208 if (NULL == session)
1210 if ((CONSENSUS_ROUND_BEGIN == session->current_round) ||
1211 (CONSENSUS_ROUND_FINISH == session->current_round))
1212 destroy_session (session);
1214 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client disconnected, but waiting for consensus to finish\n");
1219 * Start processing consensus requests.
1221 * @param cls closure
1222 * @param server the initialized server
1223 * @param c configuration to use
1226 run (void *cls, struct GNUNET_SERVER_Handle *server,
1227 const struct GNUNET_CONFIGURATION_Handle *c)
1229 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1230 {&client_conclude, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE,
1231 sizeof (struct GNUNET_CONSENSUS_ConcludeMessage)},
1232 {&client_insert, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT, 0},
1233 {&client_join, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN, 0},
1239 if (GNUNET_OK != GNUNET_CRYPTO_get_host_identity (cfg, &my_peer))
1241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
1243 GNUNET_SCHEDULER_shutdown ();
1246 GNUNET_SERVER_add_handlers (server, server_handlers);
1247 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
1248 GNUNET_SERVER_disconnect_notify (server, handle_client_disconnect, NULL);
1249 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "consensus running\n");
1254 * The main function for the consensus service.
1256 * @param argc number of arguments from the command line
1257 * @param argv command line arguments
1258 * @return 0 ok, 1 on error
1261 main (int argc, char *const *argv)
1264 ret = GNUNET_SERVICE_run (argc, argv, "consensus", GNUNET_SERVICE_OPTION_NONE, &run, NULL);
1265 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit (%d)\n", GNUNET_OK != ret);
1266 return (GNUNET_OK == ret) ? 0 : 1;