consensus now implemented with primitive conclusion group selection
[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 static void
69 destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *ctx)
70 {
71   struct GNUNET_CONSENSUS_Handle *consensus;
72   consensus = cls;
73   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying consensus\n");
74   GNUNET_CONSENSUS_destroy (consensus);
75 }
76
77
78 /**
79  * Called when a conclusion was successful.
80  *
81  * @param cls
82  * @param group
83  * @return GNUNET_YES if more consensus groups should be offered, GNUNET_NO if not
84  */
85 static void
86 conclude_cb (void *cls, const struct GNUNET_CONSENSUS_Group *group)
87 {
88   GNUNET_SCHEDULER_add_now (destroy, cls);
89 }
90
91
92 static void
93 generate_indices (int *indices)
94 {
95   int j;
96   j = 0;
97   while (j < replication)
98   {
99     int n;
100     int k;
101     int repeat;
102     n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
103     repeat = GNUNET_NO;
104     for (k = 0; k < j; k++)
105       if (indices[k] == n)
106       {
107         repeat = GNUNET_YES;
108         break;
109       }
110     if (GNUNET_NO == repeat)
111       indices[j++] = n;
112   }
113 }
114
115
116 static void
117 do_consensus ()
118 {
119   int unique_indices[replication];
120   int i;
121
122   for (i = 0; i < num_values; i++)
123   {
124     int j;
125     struct GNUNET_HashCode *val;
126     struct GNUNET_CONSENSUS_Element *element;
127     generate_indices(unique_indices);
128
129     val = GNUNET_malloc (sizeof *val);
130     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, val);
131
132     element = GNUNET_malloc (sizeof *element);
133     element->data = val;
134     element->size = sizeof *val;
135
136     for (j = 0; j < replication; j++)
137     {
138       int cid;
139       cid = unique_indices[j];
140       GNUNET_CONSENSUS_insert (consensus_handles[cid], element, NULL, NULL);
141     }
142   }
143
144   for (i = 0; i < num_peers; i++)
145     GNUNET_CONSENSUS_conclude (consensus_handles[i], conclude_timeout, 0, conclude_cb, consensus_handles[i]);
146 }
147
148
149 /**
150  * Callback to be called when a service connect operation is completed
151  *
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.
157  */
158 static void
159 connect_complete (void *cls,
160                   struct GNUNET_TESTBED_Operation *op,
161                   void *ca_result,
162                   const char *emsg)
163 {
164   struct GNUNET_CONSENSUS_Handle **chp;
165
166   if (NULL != emsg)
167   {
168     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "testbed connect emsg: %s\n", emsg);
169     GNUNET_assert (0);
170   }
171
172   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect complete\n");
173
174   chp = (struct GNUNET_CONSENSUS_Handle **) cls;
175   *chp = (struct GNUNET_CONSENSUS_Handle *) ca_result;
176   num_connected_handles++;
177
178   if (num_connected_handles == num_peers)
179   {
180     do_consensus ();
181   }
182 }
183
184
185 static int
186 new_element_cb (void *cls,
187                 struct GNUNET_CONSENSUS_Element *element)
188 {
189   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received new element\n");
190   return GNUNET_YES;
191 }
192
193
194 /**
195  * Adapter function called to establish a connection to
196  * a service.
197  *
198  * @param cls closure
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
203  */
204 static void *
205 connect_adapter (void *cls,
206                  const struct GNUNET_CONFIGURATION_Handle *cfg)
207 {
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);
212   return consensus;
213 }
214
215
216 /**
217  * Adapter function called to destroy a connection to
218  * a service.
219  *
220  * @param cls closure
221  * @param op_result service handle returned from the connect adapter
222  */
223 static void
224 disconnect_adapter(void *cls, void *op_result)
225 {
226   /* FIXME: what to do here? */
227 }
228
229
230 /**
231  * Callback to be called when the requested peer information is available
232  *
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
238  */
239 static void
240 peer_info_cb (void *cb_cls,
241               struct GNUNET_TESTBED_Operation *op,
242               const struct GNUNET_TESTBED_PeerInformation *pinfo,
243               const char *emsg)
244 {
245   struct GNUNET_PeerIdentity *p;
246   int i;
247
248   GNUNET_assert (NULL == emsg);
249
250   p = (struct GNUNET_PeerIdentity *) cb_cls;
251
252   if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
253   {
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);
260   }
261   else
262   {
263     GNUNET_assert (0);
264   }
265 }
266
267
268 static void
269 test_master (void *cls,
270              unsigned int num_peers,
271              struct GNUNET_TESTBED_Peer **started_peers)
272 {
273   int i;
274
275
276   GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
277
278   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
279
280
281   peers = started_peers;
282
283   peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
284
285   consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
286
287   for (i = 0; i < num_peers; i++)
288     GNUNET_TESTBED_peer_get_information (peers[i],
289                                          GNUNET_TESTBED_PIT_IDENTITY,
290                                          peer_info_cb,
291                                          &peer_ids[i]);
292 }
293
294 static void
295 run (void *cls, char *const *args, const char *cfgfile,
296      const struct GNUNET_CONFIGURATION_Handle *cfg)
297 {
298   static char *session_str = "gnunet-consensus/test";
299
300   if (num_peers < replication)
301   {
302     fprintf (stderr, "k must be <=n\n");
303     return;
304   }
305
306   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "running gnunet-consensus\n");
307
308   GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
309
310   (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
311                                   cfgfile,
312                                   num_peers,
313                                   0,
314                                   controller_cb,
315                                   NULL,
316                                   test_master,
317                                   NULL);
318 }
319
320
321 int
322 main (int argc, char **argv)
323 {
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
338   };
339   conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
340   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus",
341                       "help",
342                       options, &run, NULL, GNUNET_YES);
343   return 0;
344 }
345