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