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 * Called when a conclusion was successful.
73 * @return GNUNET_YES if more consensus groups should be offered, GNUNET_NO if not
76 conclude_cb (void *cls, const struct GNUNET_CONSENSUS_Group *group)
84 generate_indices (int *indices)
88 while (j < replication)
93 n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
95 for (k = 0; k < j; k++)
101 if (GNUNET_NO == repeat)
110 int unique_indices[replication];
113 for (i = 0; i < num_values; i++)
116 struct GNUNET_HashCode *val;
117 struct GNUNET_CONSENSUS_Element *element;
118 generate_indices(unique_indices);
120 val = GNUNET_malloc (sizeof *val);
121 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, val);
123 element = GNUNET_malloc (sizeof *element);
125 element->size = sizeof *val;
127 for (j = 0; j < replication; j++)
130 cid = unique_indices[j];
131 GNUNET_CONSENSUS_insert (consensus_handles[cid], element, NULL, NULL);
135 for (i = 0; i < num_peers; i++)
136 GNUNET_CONSENSUS_conclude (consensus_handles[i], conclude_timeout, 0, conclude_cb, consensus_handles[i]);
141 * Callback to be called when a service connect operation is completed
143 * @param cls the callback closure from functions generating an operation
144 * @param op the operation that has been finished
145 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
146 * @param emsg error message in case the operation has failed; will be NULL if
147 * operation has executed successfully.
150 connect_complete (void *cls,
151 struct GNUNET_TESTBED_Operation *op,
155 struct GNUNET_CONSENSUS_Handle **chp;
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "testbed connect emsg: %s\n", emsg);
163 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect complete\n");
165 chp = (struct GNUNET_CONSENSUS_Handle **) cls;
166 *chp = (struct GNUNET_CONSENSUS_Handle *) ca_result;
167 num_connected_handles++;
169 if (num_connected_handles == num_peers)
177 new_element_cb (void *cls,
178 struct GNUNET_CONSENSUS_Element *element)
185 * Adapter function called to establish a connection to
189 * @param cfg configuration of the peer to connect to; will be available until
190 * GNUNET_TESTBED_operation_done() is called on the operation returned
191 * from GNUNET_TESTBED_service_connect()
192 * @return service handle to return in 'op_result', NULL on error
195 connect_adapter (void *cls,
196 const struct GNUNET_CONFIGURATION_Handle *cfg)
198 struct GNUNET_CONSENSUS_Handle *consensus;
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect adapter, %d peers\n", num_peers);
200 consensus = GNUNET_CONSENSUS_create (cfg, num_peers, peer_ids, &session_id, new_element_cb, NULL);
201 GNUNET_assert (NULL != consensus);
207 * Adapter function called to destroy a connection to
211 * @param op_result service handle returned from the connect adapter
214 disconnect_adapter(void *cls, void *op_result)
216 /* FIXME: what to do here? */
221 * Callback to be called when the requested peer information is available
223 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
224 * @param op the operation this callback corresponds to
225 * @param pinfo the result; will be NULL if the operation has failed
226 * @param emsg error message if the operation has failed; will be NULL if the
227 * operation is successfull
230 peer_info_cb (void *cb_cls,
231 struct GNUNET_TESTBED_Operation *op,
232 const struct GNUNET_TESTBED_PeerInformation *pinfo,
235 struct GNUNET_PeerIdentity *p;
238 GNUNET_assert (NULL == emsg);
240 p = (struct GNUNET_PeerIdentity *) cb_cls;
242 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
244 *p = *pinfo->result.id;
245 num_retrieved_peer_ids++;
246 if (num_retrieved_peer_ids == num_peers)
247 for (i = 0; i < num_peers; i++)
248 GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, &consensus_handles[i],
249 connect_adapter, disconnect_adapter, NULL);
259 test_master (void *cls,
260 unsigned int num_peers,
261 struct GNUNET_TESTBED_Peer **started_peers)
266 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
268 peers = started_peers;
270 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
272 consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
274 for (i = 0; i < num_peers; i++)
275 GNUNET_TESTBED_peer_get_information (peers[i],
276 GNUNET_TESTBED_PIT_IDENTITY,
282 run (void *cls, char *const *args, const char *cfgfile,
283 const struct GNUNET_CONFIGURATION_Handle *cfg)
285 static char *session_str = "gnunet-consensus/test";
288 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "running gnunet-consensus\n");
290 GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
292 (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
304 main (int argc, char **argv)
306 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
307 { 'n', "num-peers", NULL,
308 gettext_noop ("number of peers in consensus"),
309 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
310 { 'k', "value-replication", NULL,
311 gettext_noop ("how many peers receive one value?"),
312 GNUNET_YES, &GNUNET_GETOPT_set_uint, &replication },
313 { 'x', "num-values", NULL,
314 gettext_noop ("number of values"),
315 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_values },
316 { 't', "timeout", NULL,
317 gettext_noop ("consensus timeout"),
318 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &conclude_timeout },
319 GNUNET_GETOPT_OPTION_END
321 conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
322 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus",
324 options, &run, NULL, GNUNET_YES);