2 This file is part of GNUnet
3 (C) 2012 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-consensus.c
23 * @brief profiling tool for gnunet-consensus
24 * @author Florian Dold
27 #include "gnunet_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_consensus_service.h"
30 #include "gnunet_testbed_service.h"
32 static unsigned int num_peers = 2;
34 static unsigned int replication = 1;
36 static unsigned int num_values = 5;
38 static struct GNUNET_TIME_Relative conclude_timeout;
40 static struct GNUNET_CONSENSUS_Handle **consensus_handles;
42 static unsigned int num_connected_handles;
44 static struct GNUNET_TESTBED_Peer **peers;
46 static struct GNUNET_PeerIdentity *peer_ids;
48 static unsigned int num_retrieved_peer_ids;
50 static struct GNUNET_HashCode session_id;
54 * Signature of the event handler function called by the
55 * respective event controller.
58 * @param event information about the event
61 controller_cb(void *cls,
62 const struct GNUNET_TESTBED_EventInformation *event)
69 destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *ctx)
71 struct GNUNET_CONSENSUS_Handle *consensus;
73 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying consensus\n");
74 GNUNET_CONSENSUS_destroy (consensus);
79 * Called when a conclusion was successful.
83 * @return GNUNET_YES if more consensus groups should be offered, GNUNET_NO if not
86 conclude_cb (void *cls)
88 GNUNET_SCHEDULER_add_now (destroy, cls);
93 generate_indices (int *indices)
97 while (j < replication)
102 n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
104 for (k = 0; k < j; k++)
110 if (GNUNET_NO == repeat)
119 int unique_indices[replication];
122 for (i = 0; i < num_values; i++)
125 struct GNUNET_HashCode *val;
126 struct GNUNET_CONSENSUS_Element *element;
127 generate_indices(unique_indices);
129 val = GNUNET_malloc (sizeof *val);
130 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, val);
132 element = GNUNET_malloc (sizeof *element);
134 element->size = sizeof *val;
136 for (j = 0; j < replication; j++)
139 cid = unique_indices[j];
140 GNUNET_CONSENSUS_insert (consensus_handles[cid], element, NULL, NULL);
144 for (i = 0; i < num_peers; i++)
145 GNUNET_CONSENSUS_conclude (consensus_handles[i], conclude_timeout, conclude_cb, consensus_handles[i]);
150 * Callback to be called when a service connect operation is completed
152 * @param cls the callback closure from functions generating an operation
153 * @param op the operation that has been finished
154 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
155 * @param emsg error message in case the operation has failed; will be NULL if
156 * operation has executed successfully.
159 connect_complete (void *cls,
160 struct GNUNET_TESTBED_Operation *op,
164 struct GNUNET_CONSENSUS_Handle **chp;
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "testbed connect emsg: %s\n", emsg);
172 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect complete\n");
174 chp = (struct GNUNET_CONSENSUS_Handle **) cls;
175 *chp = (struct GNUNET_CONSENSUS_Handle *) ca_result;
176 num_connected_handles++;
178 if (num_connected_handles == num_peers)
186 new_element_cb (void *cls,
187 const struct GNUNET_CONSENSUS_Element *element)
189 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received new element\n");
195 * Adapter function called to establish a connection to
199 * @param cfg configuration of the peer to connect to; will be available until
200 * GNUNET_TESTBED_operation_done() is called on the operation returned
201 * from GNUNET_TESTBED_service_connect()
202 * @return service handle to return in 'op_result', NULL on error
205 connect_adapter (void *cls,
206 const struct GNUNET_CONFIGURATION_Handle *cfg)
208 struct GNUNET_CONSENSUS_Handle *consensus;
209 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect adapter, %d peers\n", num_peers);
210 consensus = GNUNET_CONSENSUS_create (cfg, num_peers, peer_ids, &session_id, new_element_cb, NULL);
211 GNUNET_assert (NULL != consensus);
217 * Adapter function called to destroy a connection to
221 * @param op_result service handle returned from the connect adapter
224 disconnect_adapter(void *cls, void *op_result)
226 /* FIXME: what to do here? */
231 * Callback to be called when the requested peer information is available
233 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
234 * @param op the operation this callback corresponds to
235 * @param pinfo the result; will be NULL if the operation has failed
236 * @param emsg error message if the operation has failed; will be NULL if the
237 * operation is successfull
240 peer_info_cb (void *cb_cls,
241 struct GNUNET_TESTBED_Operation *op,
242 const struct GNUNET_TESTBED_PeerInformation *pinfo,
245 struct GNUNET_PeerIdentity *p;
248 GNUNET_assert (NULL == emsg);
250 p = (struct GNUNET_PeerIdentity *) cb_cls;
252 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
254 *p = *pinfo->result.id;
255 num_retrieved_peer_ids++;
256 if (num_retrieved_peer_ids == num_peers)
257 for (i = 0; i < num_peers; i++)
258 GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, &consensus_handles[i],
259 connect_adapter, disconnect_adapter, NULL);
269 test_master (void *cls,
270 unsigned int num_peers,
271 struct GNUNET_TESTBED_Peer **started_peers)
276 GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
278 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
281 peers = started_peers;
283 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
285 consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
287 for (i = 0; i < num_peers; i++)
288 GNUNET_TESTBED_peer_get_information (peers[i],
289 GNUNET_TESTBED_PIT_IDENTITY,
295 run (void *cls, char *const *args, const char *cfgfile,
296 const struct GNUNET_CONFIGURATION_Handle *cfg)
298 static char *session_str = "gnunet-consensus/test";
300 if (num_peers < replication)
302 fprintf (stderr, "k must be <=n\n");
306 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "running gnunet-consensus\n");
308 GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
310 (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
322 main (int argc, char **argv)
324 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
325 { 'n', "num-peers", NULL,
326 gettext_noop ("number of peers in consensus"),
327 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
328 { 'k', "value-replication", NULL,
329 gettext_noop ("how many peers receive one value?"),
330 GNUNET_YES, &GNUNET_GETOPT_set_uint, &replication },
331 { 'x', "num-values", NULL,
332 gettext_noop ("number of values"),
333 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_values },
334 { 't', "timeout", NULL,
335 gettext_noop ("consensus timeout"),
336 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &conclude_timeout },
337 GNUNET_GETOPT_OPTION_END
339 conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
340 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus",
342 options, &run, NULL, GNUNET_YES);