aboutsummaryrefslogtreecommitdiff
path: root/src/consensus/gnunet-service-consensus.c
blob: 6cf3c065363a9385cead7c6baa166f53d24851bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
      This file is part of GNUnet
      (C) 2012 Christian Grothoff (and other contributing authors)

      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
      by the Free Software Foundation; either version 2, or (at your
      option) any later version.

      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA 02111-1307, USA.
 */


#include "platform.h"
#include "gnunet_protocols.h"
#include "gnunet_common.h"
#include "gnunet_service_lib.h"
#include "gnunet_consensus_service.h"
#include "gnunet_core_service.h"
#include "gnunet_container_lib.h"
#include "consensus.h"


struct ConsensusClient;

static void
send_next (struct ConsensusClient *cli);


/**
 * An element that is waiting to be transmitted to a client.
 */
struct PendingElement
{
  /**
   * Pending elements are kept in a DLL.
   */
  struct PendingElement *next;

  /**
   * Pending elements are kept in a DLL.
   */
  struct PendingElement *prev;

  /**
   * The actual element
   */
  struct GNUNET_CONSENSUS_Element *element;
};


/**
 * A consensus session consists of one or more local clients,
 * as well as zero or more remote authorities.
 */
struct ConsensusSession
{
  /**
   * Consensus sessions are kept in a DLL.
   */
  struct ConsensusSession *next;

  /**
   * Consensus sessions are kept in a DLL.
   */
  struct ConsensusSession *prev;

  /**
   * Consensus clients are kept in a DLL.
   */
  struct ConsensusClient *clients_head;

  /**
   * Consensus clients are kept in a DLL.
   */
  struct ConsensusClient *clients_tail;

  /**
  * Local consensus identification, chosen by clients.
  */
  struct GNUNET_HashCode *local_id;
 
  /**
  * Global consensus identification, computed
  * from the local id and participating authorities.
  */
  struct GNUNET_HashCode *global_id;

  /**
   * Values in the consensus set of this session.
   */
  struct GNUNET_CONTAINER_MultiHashMap *values;
};


struct ConsensusClient
{
  /**
   * Consensus clients are kept in a DLL.
   */
  struct ConsensusClient *next;
  /**
   * Consensus clients are kept in a DLL.
   */
  struct ConsensusClient *prev;

  /**
   * Corresponding server handle.
   */
  struct GNUNET_SERVER_Client *client;

  /**
   * Client wants to receive and send updates.
   */
  int begin;

  /**
   * Session this client belongs to
   */
  struct ConsensusSession *session;

  /**
   * Values in the consensus set of this client.
   * Includes pending elements.
   */
  struct GNUNET_CONTAINER_MultiHashMap *values;

  /**
   * Elements that have not been set to the client yet.
   */
  struct PendingElement *pending_head;
  /**
   * Elements that have not been set to the client yet.
   */
  struct PendingElement *pending_tail;

  /**
   * Currently active transmit handle for sending to the client
   */
  struct GNUNET_SERVER_TransmitHandle *th;

  /**
   * Once conclude_requested is GNUNET_YES, the client may not
   * insert any more values.
   */
  int conclude_requested;

  /**
   * Client has been informed about the conclusion.
   */
  int conclude_sent;
};


/**
 * Linked list of sesstions this peer participates in.
 */
static struct ConsensusSession *sessions_head;

/**
 * Linked list of sesstions this peer participates in.
 */
static struct ConsensusSession *sessions_tail;

/**
 * Configuration of the consensus service.
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * Handle to the server for this service.
 */
static struct GNUNET_SERVER_Handle *srv;

/**
 * Peer that runs this service
 */
static struct GNUNET_PeerIdentity *my_peer;


struct ConsensusClient *
find_client (const struct GNUNET_SERVER_Client *srv_client)
{
  struct ConsensusSession *session;
  struct ConsensusClient *client;

  session = sessions_head;
  while (NULL != session)
  {
    client = session->clients_head;
    while (NULL != client)
    {
      if (client->client == srv_client)
      {
        return client;
      }
      client = client->next;
    }
    session = session->next;
  }
  return NULL;
}

static void
disconnect_client (struct GNUNET_SERVER_Client *client)
{
  /* FIXME */
}

static void
compute_global_id (struct GNUNET_HashCode *dst,
                   const struct GNUNET_HashCode *local_id,
                   const struct GNUNET_PeerIdentity *peers,
                   int num_peers)
{
  *dst = *local_id;

  /* FIXME: hash other peers into global id */
}



/**
 * Iterator over hash map entries.
 *
 * @param cls closure, the client
 * @param key current key code
 * @param value value in the hash map
 * @return GNUNET_YES if we should continue to
 *         iterate,
 *         GNUNET_NO if not.
 */
int
update_pending (void *cls,
                 const struct GNUNET_HashCode *key,
                 void *value)
{
  struct ConsensusClient *cli;
  struct GNUNET_CONSENSUS_Element *element;
  struct PendingElement *pending_element;

  cli = (struct ConsensusClient *) cls;
  element = (struct GNUNET_CONSENSUS_Element *) value;
  pending_element = GNUNET_malloc (sizeof (struct PendingElement));
  pending_element->element = element;

  if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (cli->values, key))
  {
    GNUNET_CONTAINER_DLL_insert_tail (cli->pending_head, cli->pending_tail, pending_element);
    GNUNET_CONTAINER_multihashmap_put (cli->values, key, element, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
  }
  
  return GNUNET_YES;
}




static size_t
transmit_pending (void *cls, size_t size, void *buf)
{
  struct GNUNET_CONSENSUS_Element *element;
  struct GNUNET_CONSENSUS_ElementMessage *msg;
  struct ConsensusClient *cli;

  cli = (struct ConsensusClient *) cls;
  msg = (struct GNUNET_CONSENSUS_ElementMessage *) buf;
  element = cli->pending_head->element;

  GNUNET_assert (NULL != element);

  cli->th = NULL;

  msg->element_type = element->type;
  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT);
  msg->header.size = htons (sizeof (struct GNUNET_CONSENSUS_ElementMessage) + element->size);
  memcpy (&msg[1], &element[1], element->size);


  cli->pending_head = cli->pending_head->next;

  send_next (cli);

  return sizeof (struct GNUNET_CONSENSUS_ElementMessage) + element->size;
}


static size_t
transmit_conclude_done (void *cls, size_t size, void *buf)
{
  struct GNUNET_CONSENSUS_ConcludeDoneMessage *msg;

  msg = (struct GNUNET_CONSENSUS_ConcludeDoneMessage *) buf;
  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE);
  msg->header.size = htons (sizeof (struct GNUNET_CONSENSUS_ConcludeDoneMessage));
  msg->num_peers = htons (0);

  return sizeof (struct GNUNET_CONSENSUS_ConcludeDoneMessage);
}


/**
 * Schedule sending the next message (if there is any) to a client.
 *
 * @param cli the client to send the next message to
 */
static void
send_next (struct ConsensusClient *cli)
{
  int msize;

  GNUNET_assert (NULL != cli);

  if (NULL != cli->th)
  {
    return;
  }

  if ((cli->conclude_requested == GNUNET_YES) && (cli->conclude_sent == GNUNET_NO))
  {
    /* just the conclude message with no other authorities in the dummy */
    msize = sizeof (struct GNUNET_CONSENSUS_ConcludeMessage);
    cli->th =
        GNUNET_SERVER_notify_transmit_ready (cli->client, msize,
                                             GNUNET_TIME_UNIT_FOREVER_REL, &transmit_conclude_done, cli);
    cli->conclude_sent = GNUNET_YES;
  }
  else if (NULL != cli->pending_head)
  {
    msize = cli->pending_head->element->size + sizeof (struct GNUNET_CONSENSUS_ElementMessage);
    cli->th =
        GNUNET_SERVER_notify_transmit_ready (cli->client, msize,
                                             GNUNET_TIME_UNIT_FOREVER_REL, &transmit_pending, cli);
  }
}


/**
 * Called when a client wants to join a consensus session.
 */
static void
client_join (void *cls,
             struct GNUNET_SERVER_Client *client,
             const struct GNUNET_MessageHeader *m)
{
  struct GNUNET_HashCode global_id;
  const struct GNUNET_CONSENSUS_JoinMessage *msg;
  struct ConsensusSession *session;
  struct ConsensusClient *consensus_client;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "join\n");

  fprintf(stderr, "foobar\n");

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client joined\n");

  msg = (struct GNUNET_CONSENSUS_JoinMessage *) m;
  
  /* kill the client if it already is in a session */
  if (NULL != find_client (client))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to join twice\n");
    disconnect_client (client);
    return;
  }

  consensus_client = GNUNET_malloc (sizeof (struct ConsensusClient));
  consensus_client->client = client;
  consensus_client->begin = GNUNET_NO;
  consensus_client->values = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);

  GNUNET_SERVER_client_keep (client);

  GNUNET_assert (NULL != consensus_client->values);

  compute_global_id (&global_id, &msg->session_id, (struct GNUNET_PeerIdentity *) &m[1], msg->num_peers);

  /* look if we already have a session for this local id */
  session = sessions_head;
  while (NULL != session)
  {
    if (0 == memcmp(&global_id, session->global_id, sizeof (struct GNUNET_HashCode)))
    {
      GNUNET_CONTAINER_DLL_insert (session->clients_head, session->clients_tail, consensus_client);
      GNUNET_SERVER_receive_done (client, GNUNET_OK);
      return;
    }
    session = session->next;
  }

  /* session does not exist yet, create it */
  session = GNUNET_malloc (sizeof (struct ConsensusSession));
  session->local_id = GNUNET_memdup (&msg->session_id, sizeof (struct GNUNET_HashCode));
  session->global_id = GNUNET_memdup (&global_id, sizeof (struct GNUNET_HashCode));
  session->values = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);

  GNUNET_CONTAINER_DLL_insert (sessions_head, sessions_tail, session);
  GNUNET_CONTAINER_DLL_insert (session->clients_head, session->clients_tail, consensus_client);

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "created new session");

  GNUNET_SERVER_receive_done (client, GNUNET_OK);
}


/**
 * Called when a client performs an insert operation.
 */
void
client_insert (void *cls,
             struct GNUNET_SERVER_Client *client,
             const struct GNUNET_MessageHeader *m)
{
  struct ConsensusClient *consensus_client;
  struct GNUNET_CONSENSUS_ElementMessage *msg;
  struct GNUNET_CONSENSUS_Element *element;
  struct PendingElement *pending_element;
  struct GNUNET_HashCode key;
  int element_size;

  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "insert\n");

  consensus_client = find_client (client);

  if (NULL == consensus_client)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to insert, but client is not in any session\n");
    GNUNET_SERVER_client_disconnect (client);
    return;
  }

  msg = (struct GNUNET_CONSENSUS_ElementMessage *) m;
  element_size = msg->header.size - sizeof (struct GNUNET_CONSENSUS_ElementMessage);

  element = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_Element) + element_size);

  element->type = msg->element_type;
  element->size = element_size;
  memcpy (&element[1], &msg[1], element_size);
  element->data = &element[1];

  GNUNET_CRYPTO_hash (element, element_size, &key);

  GNUNET_CONTAINER_multihashmap_put (consensus_client->session->values, &key, element,
                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
  GNUNET_CONTAINER_multihashmap_put (consensus_client->values, &key, element,
                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);

  /* send the new value to all clients that don't have it */

  consensus_client = consensus_client->session->clients_head;
  while (NULL != consensus_client)
  {
    if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (consensus_client->values, &key))
    {
      pending_element = GNUNET_malloc (sizeof (struct PendingElement));
      pending_element->element = element;
      GNUNET_CONTAINER_DLL_insert_tail (consensus_client->pending_head, consensus_client->pending_tail, pending_element);
      GNUNET_CONTAINER_multihashmap_put (consensus_client->values, &key, element,
                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
      send_next (consensus_client);
    }
  }

  GNUNET_SERVER_receive_done (client, GNUNET_OK);
}


/**
 * Called when a client wants to begin
 */
void
client_begin (void *cls,
             struct GNUNET_SERVER_Client *client,
             const struct GNUNET_MessageHeader *message)
{
  struct ConsensusClient *consensus_client;

  consensus_client = find_client (client);

  if (NULL == consensus_client)
  {
    GNUNET_SERVER_client_disconnect (client);
    return;
  }

  consensus_client->begin = GNUNET_YES;

  GNUNET_CONTAINER_multihashmap_iterate (consensus_client->session->values, &update_pending, NULL);
  send_next (consensus_client);

  GNUNET_SERVER_receive_done (client, GNUNET_OK);
}



/**
 * Called when a client performs the conclude operation.
 */
void
client_conclude (void *cls,
             struct GNUNET_SERVER_Client *client,
             const struct GNUNET_MessageHeader *message)
{
  struct ConsensusClient *consensus_client;

  consensus_client = find_client (client);
  if (NULL == consensus_client)
  {
    GNUNET_SERVER_client_disconnect (client);
    return;
  }
  consensus_client->conclude_requested = GNUNET_YES;
  send_next (consensus_client);

  GNUNET_SERVER_receive_done (client, GNUNET_OK);
}

/**
 * Task that disconnects from core.
 *
 * @param cls core handle
 * @param tc context information (why was this task triggered now)
 */
static void
disconnect_core (void *cls,
                 const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_CORE_Handle *core;
  core = (struct GNUNET_CORE_Handle *) cls;
  GNUNET_CORE_disconnect (core);

  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "disconnected from core\n");
}


static void
core_startup (void *cls,
              struct GNUNET_CORE_Handle *core,
              const struct GNUNET_PeerIdentity *peer)
{
  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
    {&client_join, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN, 0},
    {&client_insert, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT, 0},
    {&client_begin, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_BEGIN,
        sizeof (struct GNUNET_MessageHeader)},
    {&client_conclude, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE,
        sizeof (struct GNUNET_CONSENSUS_ConcludeMessage)},
    {NULL, NULL, 0, 0}
  };


  GNUNET_SERVER_add_handlers (srv, handlers);

  my_peer = GNUNET_memdup(peer, sizeof (struct GNUNET_PeerIdentity));

  GNUNET_SCHEDULER_add_now (&disconnect_core, core);

  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "connected to core\n");
}


/**
 * Process consensus requests.
 *
 * @param cls closure
 * @param server the initialized server
 * @param c configuration to use
 */
static void
run (void *cls, struct GNUNET_SERVER_Handle *server, const struct GNUNET_CONFIGURATION_Handle *c)
{
  struct GNUNET_CORE_Handle *my_core;
  static const struct GNUNET_CORE_MessageHandler handlers[] = {
    {NULL, 0, 0}
  };

  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "run\n");

  cfg = c;
  srv = server;
  my_core = GNUNET_CORE_connect (c, NULL, &core_startup, NULL, NULL, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
  GNUNET_assert (NULL != my_core);
}


/**
 * The main function for the statistics service.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  return (GNUNET_OK ==
          GNUNET_SERVICE_run (argc, argv, "consensus", GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
}