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