- show received elements in consensus profiler
[oweals/gnunet.git] / src / consensus / gnunet-consensus.c
1 /*
2       This file is part of GNUnet
3       (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
22  * @file consensus/gnunet-consensus.c
23  * @brief profiling tool for gnunet-consensus
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_consensus_service.h"
30 #include "gnunet_testbed_service.h"
31
32 static unsigned int num_peers = 2;
33
34 static unsigned int replication = 1;
35
36 static unsigned int num_values = 5;
37
38 static struct GNUNET_TIME_Relative conclude_timeout;
39
40 static struct GNUNET_CONSENSUS_Handle **consensus_handles;
41
42 static struct GNUNET_TESTBED_Operation **testbed_operations;
43
44 static unsigned int num_connected_handles;
45
46 static struct GNUNET_TESTBED_Peer **peers;
47
48 static struct GNUNET_PeerIdentity *peer_ids;
49
50 static unsigned int num_retrieved_peer_ids;
51
52 static struct GNUNET_HashCode session_id;
53
54 static unsigned int peers_done = 0;
55
56 static unsigned *results_for_peer;
57
58
59 /**
60  * Signature of the event handler function called by the
61  * respective event controller.
62  *
63  * @param cls closure
64  * @param event information about the event
65  */
66 static void
67 controller_cb(void *cls,
68               const struct GNUNET_TESTBED_EventInformation *event)
69 {
70   GNUNET_assert (0);
71 }
72
73 static void
74 destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *ctx)
75 {
76   struct GNUNET_CONSENSUS_Handle *consensus;
77   consensus = cls;
78   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying consensus\n");
79   GNUNET_CONSENSUS_destroy (consensus);
80   peers_done++;
81   if (peers_done == num_peers)
82   {
83     unsigned int i;
84     for (i = 0; i < num_peers; i++)
85       GNUNET_TESTBED_operation_done (testbed_operations[i]);
86     for (i = 0; i < num_peers; i++)
87       printf ("P%u got %u of %u elements\n", i, results_for_peer[i], num_values);
88     GNUNET_SCHEDULER_shutdown ();
89   }
90 }
91
92
93 /**
94  * Called when a conclusion was successful.
95  *
96  * @param cls closure, the consensus handle
97  * @return GNUNET_YES if more consensus groups should be offered, GNUNET_NO if not
98  */
99 static void
100 conclude_cb (void *cls)
101 {
102   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "consensus done\n");
103   GNUNET_SCHEDULER_add_now (destroy, cls);
104 }
105
106
107 static void
108 generate_indices (int *indices)
109 {
110   int j;
111   j = 0;
112   while (j < replication)
113   {
114     int n;
115     int k;
116     int repeat;
117     n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
118     repeat = GNUNET_NO;
119     for (k = 0; k < j; k++)
120       if (indices[k] == n)
121       {
122         repeat = GNUNET_YES;
123         break;
124       }
125     if (GNUNET_NO == repeat)
126       indices[j++] = n;
127   }
128 }
129
130
131 static void
132 do_consensus ()
133 {
134   int unique_indices[replication];
135   int i;
136
137   for (i = 0; i < num_values; i++)
138   {
139     int j;
140     struct GNUNET_HashCode *val;
141     struct GNUNET_SET_Element *element;
142     generate_indices(unique_indices);
143
144     val = GNUNET_malloc (sizeof *val);
145     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, val);
146
147     element = GNUNET_malloc (sizeof *element);
148     element->data = val;
149     element->size = sizeof *val;
150
151     for (j = 0; j < replication; j++)
152     {
153       int cid;
154       cid = unique_indices[j];
155       GNUNET_CONSENSUS_insert (consensus_handles[cid], element, NULL, NULL);
156     }
157   }
158
159   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "all elements inserted, calling conclude\n");
160
161   for (i = 0; i < num_peers; i++)
162     GNUNET_CONSENSUS_conclude (consensus_handles[i], conclude_timeout, conclude_cb, consensus_handles[i]);
163 }
164
165
166 /**
167  * Callback to be called when a service connect operation is completed
168  *
169  * @param cls the callback closure from functions generating an operation
170  * @param op the operation that has been finished
171  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
172  * @param emsg error message in case the operation has failed; will be NULL if
173  *          operation has executed successfully.
174  */
175 static void
176 connect_complete (void *cls,
177                   struct GNUNET_TESTBED_Operation *op,
178                   void *ca_result,
179                   const char *emsg)
180 {
181
182   if (NULL != emsg)
183   {
184     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "testbed connect emsg: %s\n", emsg);
185     GNUNET_assert (0);
186   }
187
188   num_connected_handles++;
189
190   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect complete\n");
191
192   if (num_connected_handles == num_peers)
193   {
194     do_consensus ();
195   }
196 }
197
198
199 static void
200 new_element_cb (void *cls,
201                 const struct GNUNET_SET_Element *element)
202 {
203   struct GNUNET_CONSENSUS_Handle **chp = cls;
204
205   GNUNET_assert (NULL != cls);
206   
207   results_for_peer[chp - consensus_handles]++;
208 }
209
210
211 /**
212  * Adapter function called to establish a connection to
213  * a service.
214  *
215  * @param cls closure
216  * @param cfg configuration of the peer to connect to; will be available until
217  *          GNUNET_TESTBED_operation_done() is called on the operation returned
218  *          from GNUNET_TESTBED_service_connect()
219  * @return service handle to return in 'op_result', NULL on error
220  */
221 static void *
222 connect_adapter (void *cls,
223                  const struct GNUNET_CONFIGURATION_Handle *cfg)
224 {
225   struct GNUNET_CONSENSUS_Handle **chp = cls;
226   struct GNUNET_CONSENSUS_Handle *consensus;
227   chp = (struct GNUNET_CONSENSUS_Handle **) cls;
228
229   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect adapter, %d peers\n", num_peers);
230   consensus = GNUNET_CONSENSUS_create (cfg, num_peers, peer_ids, &session_id, new_element_cb, chp);
231   *chp = (struct GNUNET_CONSENSUS_Handle *) consensus;
232   return consensus;
233 }
234
235
236 /**
237  * Adapter function called to destroy a connection to
238  * a service.
239  *
240  * @param cls closure
241  * @param op_result service handle returned from the connect adapter
242  */
243 static void
244 disconnect_adapter(void *cls, void *op_result)
245 {
246   /* FIXME: what to do here? */
247 }
248
249
250 /**
251  * Callback to be called when the requested peer information is available
252  *
253  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
254  * @param op the operation this callback corresponds to
255  * @param pinfo the result; will be NULL if the operation has failed
256  * @param emsg error message if the operation has failed; will be NULL if the
257  *          operation is successfull
258  */
259 static void
260 peer_info_cb (void *cb_cls,
261               struct GNUNET_TESTBED_Operation *op,
262               const struct GNUNET_TESTBED_PeerInformation *pinfo,
263               const char *emsg)
264 {
265   struct GNUNET_PeerIdentity *p;
266   int i;
267
268   GNUNET_assert (NULL == emsg);
269
270   p = (struct GNUNET_PeerIdentity *) cb_cls;
271
272   if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
273   {
274     *p = *pinfo->result.id;
275     num_retrieved_peer_ids++;
276     if (num_retrieved_peer_ids == num_peers)
277       for (i = 0; i < num_peers; i++)
278         testbed_operations[i] =
279             GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, NULL,
280                                             connect_adapter, disconnect_adapter, &consensus_handles[i]);
281   }
282   else
283   {
284     GNUNET_assert (0);
285   }
286
287   GNUNET_TESTBED_operation_done (op);
288 }
289
290
291 /**
292  * Signature of a main function for a testcase.
293  *
294  * @param cls closure
295  * @param num_peers number of peers in 'peers'
296  * @param started_peers handle to peers run in the testbed.  NULL upon timeout (see
297  *          GNUNET_TESTBED_test_run()).
298  * @param links_succeeded the number of overlay link connection attempts that
299  *          succeeded
300  * @param links_failed the number of overlay link connection attempts that
301  *          failed
302  */
303 static void
304 test_master (void *cls,
305              unsigned int num_peers,
306              struct GNUNET_TESTBED_Peer **started_peers,
307              unsigned int links_succeeded,
308              unsigned int links_failed)
309 {
310   int i;
311
312   GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
313
314   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
315
316   peers = started_peers;
317
318   peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
319
320   results_for_peer = GNUNET_malloc (num_peers * sizeof (unsigned int));
321   consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
322   testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
323
324   for (i = 0; i < num_peers; i++)
325     GNUNET_TESTBED_peer_get_information (peers[i],
326                                          GNUNET_TESTBED_PIT_IDENTITY,
327                                          peer_info_cb,
328                                          &peer_ids[i]);
329 }
330
331 static void
332 run (void *cls, char *const *args, const char *cfgfile,
333      const struct GNUNET_CONFIGURATION_Handle *cfg)
334 {
335   static char *session_str = "gnunet-consensus/test";
336   char *topology;
337
338   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
339   {
340     fprintf (stderr, "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
341                      "seems like you passed the wrong configuration file\n");
342     return;
343   }
344
345   if (0 == strcasecmp (topology, "NONE"))
346   {
347     fprintf (stderr, "'OVERLAY_TOPOLOGY' set to 'NONE', "
348                      "seems like you passed the wrong configuration file\n");
349     return;
350   }
351
352   GNUNET_free (topology);
353
354   if (num_peers < replication)
355   {
356     fprintf (stderr, "k must be <=n\n");
357     return;
358   }
359
360   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "running gnunet-consensus\n");
361
362   GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
363
364   (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
365                                   cfgfile,
366                                   num_peers,
367                                   0,
368                                   controller_cb,
369                                   NULL,
370                                   test_master,
371                                   NULL);
372 }
373
374
375 int
376 main (int argc, char **argv)
377 {
378    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
379       { 'n', "num-peers", NULL,
380         gettext_noop ("number of peers in consensus"),
381         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
382       { 'k', "value-replication", NULL,
383         gettext_noop ("how many peers receive one value?"),
384         GNUNET_YES, &GNUNET_GETOPT_set_uint, &replication },
385       { 'x', "num-values", NULL,
386         gettext_noop ("number of values"),
387         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_values },
388       { 't', "timeout", NULL,
389         gettext_noop ("consensus timeout"),
390         GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &conclude_timeout },
391       GNUNET_GETOPT_OPTION_END
392   };
393   conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
394   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus",
395                       "help",
396                       options, &run, NULL, GNUNET_YES);
397   return 0;
398 }
399