GNUnet 0.28.0-dev.3-7-g31e20e2e6
 
Loading...
Searching...
No Matches
gnunet-service-consensus.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2012, 2013, 2017, 2020 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
28#include "gnunet_protocols.h"
29#include "gnunet_set_service.h"
31#include "consensus_protocol.h"
32#include "consensus.h"
33
34
51
52
59
60
77
78
90
98
106
107
109
114{
119
125
130
135
142};
143
144
157
158
160{
161 struct SetKey key;
163
169};
170
171
180
187
188
190
191
206
207
209{
211};
212
218{
221};
222
223
224struct TaskEntry;
225
226
227typedef void
228(*TaskFunc) (struct TaskEntry *task);
229
230
231/*
232 * Node in the consensus task graph.
233 */
235{
236 struct TaskKey key;
237
238 struct Step *step;
239
241
243
246
248};
249
250
251struct Step
252{
257 struct Step *prev;
258
263 struct Step *next;
264
266
270 struct TaskEntry **tasks;
271 unsigned int tasks_len;
272 unsigned int tasks_cap;
273
274 unsigned int finished_tasks;
275
276 /*
277 * Tasks that have this task as dependency.
278 *
279 * We store pointers to subordinates rather
280 * than to prerequisites since it makes
281 * tracking the readiness of a task easier.
282 */
284 unsigned int subordinates_len;
285 unsigned int subordinates_cap;
286
291
296
297 unsigned int is_running;
298
299 unsigned int is_finished;
300
304 unsigned int round;
305
310
320};
321
322
324{
326
330 int *votes;
331
336};
337
338
340{
341 struct RfnKey key;
342
343 /*
344 * Elements where there is at least one proposed change.
345 *
346 * Maps the hash of the GNUNET_SET_Element
347 * to 'struct RfnElementInfo'.
348 */
350
351 unsigned int num_peers;
352
364
365
372};
373
374
376{
378
384};
385
386
395
397{
400
402};
403
404
409{
414
419
421
425
430
431 /*
432 * Mapping from (hashed) TaskKey to TaskEntry.
433 *
434 * We map the application_id for a round to the task that should be
435 * executed, so we don't have to go through all task whenever we get
436 * an incoming set op request.
437 */
439
442
444
446
452
457
462
467
474
476
480 unsigned int num_peers;
481
485 unsigned int local_peer_idx;
486
492
497
501 uint64_t first_size;
502
504
508 uint64_t lower_bound;
509
512};
513
518
523
527static const struct GNUNET_CONFIGURATION_Handle *cfg;
528
533
538
539
540static void
541finish_task (struct TaskEntry *task);
542
543
544static void
545run_ready_steps (struct ConsensusSession *session);
546
547
548static const char *
549phasename (uint16_t phase)
550{
551 switch (phase)
552 {
553 case PHASE_KIND_ALL_TO_ALL: return "ALL_TO_ALL";
554
555 case PHASE_KIND_ALL_TO_ALL_2: return "ALL_TO_ALL_2";
556
557 case PHASE_KIND_FINISH: return "FINISH";
558
559 case PHASE_KIND_GRADECAST_LEADER: return "GRADECAST_LEADER";
560
561 case PHASE_KIND_GRADECAST_ECHO: return "GRADECAST_ECHO";
562
563 case PHASE_KIND_GRADECAST_ECHO_GRADE: return "GRADECAST_ECHO_GRADE";
564
565 case PHASE_KIND_GRADECAST_CONFIRM: return "GRADECAST_CONFIRM";
566
567 case PHASE_KIND_GRADECAST_CONFIRM_GRADE: return "GRADECAST_CONFIRM_GRADE";
568
569 case PHASE_KIND_APPLY_REP: return "APPLY_REP";
570
571 default: return "(unknown)";
572 }
573}
574
575
576static const char *
577setname (uint16_t kind)
578{
579 switch (kind)
580 {
581 case SET_KIND_CURRENT: return "CURRENT";
582
583 case SET_KIND_LEADER_PROPOSAL: return "LEADER_PROPOSAL";
584
585 case SET_KIND_NONE: return "NONE";
586
587 default: return "(unknown)";
588 }
589}
590
591
592static const char *
593rfnname (uint16_t kind)
594{
595 switch (kind)
596 {
597 case RFN_KIND_NONE: return "NONE";
598
599 case RFN_KIND_ECHO: return "ECHO";
600
601 case RFN_KIND_CONFIRM: return "CONFIRM";
602
603 default: return "(unknown)";
604 }
605}
606
607
608static const char *
609diffname (uint16_t kind)
610{
611 switch (kind)
612 {
613 case DIFF_KIND_NONE: return "NONE";
614
615 case DIFF_KIND_LEADER_CONSENSUS: return "LEADER_CONSENSUS";
616
617 case DIFF_KIND_GRADECAST_RESULT: return "GRADECAST_RESULT";
618
619 case DIFF_KIND_LEADER_PROPOSAL: return "LEADER_PROPOSAL";
620
621 default: return "(unknown)";
622 }
623}
624
625
626#ifdef GNUNET_EXTRA_LOGGING
627
628
629static const char *
630debug_str_element (const struct GNUNET_SET_Element *el)
631{
632 struct GNUNET_HashCode hash;
633
635
636 return GNUNET_h2s (&hash);
637}
638
639
640static const char *
641debug_str_task_key (const struct TaskKey *tk)
642{
643 static char buf[256];
644
645 snprintf (buf, sizeof(buf),
646 "TaskKey kind=%s, p1=%d, p2=%d, l=%d, rep=%d",
647 phasename (tk->kind), tk->peer1, tk->peer2,
648 tk->leader, tk->repetition);
649
650 return buf;
651}
652
653
654static const char *
655debug_str_diff_key (const struct DiffKey *dk)
656{
657 static char buf[256];
658
659 snprintf (buf, sizeof(buf),
660 "DiffKey kind=%s, k1=%d, k2=%d",
661 diffname (dk->diff_kind), dk->k1, dk->k2);
662
663 return buf;
664}
665
666
667static const char *
668debug_str_set_key (const struct SetKey *sk)
669{
670 static char buf[256];
671
672 snprintf (buf, sizeof(buf),
673 "SetKey kind=%s, k1=%d, k2=%d",
674 setname (sk->set_kind),
675 sk->k1,
676 sk->k2);
677 return buf;
678}
679
680
681static const char *
682debug_str_rfn_key (const struct RfnKey *rk)
683{
684 static char buf[256];
685
686 snprintf (buf, sizeof(buf),
687 "RfnKey kind=%s, k1=%d, k2=%d",
688 rfnname (rk->rfn_kind),
689 rk->k1,
690 rk->k2);
691 return buf;
692}
693
694
695#endif /* GNUNET_EXTRA_LOGGING */
696
697
707static int
709 const struct GNUNET_SET_Element *element)
710{
711 struct TaskEntry *task = (struct TaskEntry *) cls;
712 struct ConsensusSession *session = task->step->session;
713 struct GNUNET_MQ_Envelope *ev;
714
715 if (NULL != element)
716 {
718 const struct ConsensusElement *ce;
719
721 element->element_type);
722 ce = element->data;
723
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "marker is %u\n",
725 (unsigned) ce->marker);
726
727 if (0 != ce->marker)
728 return GNUNET_YES;
729
731 "P%d: sending element %s to client\n",
732 session->local_peer_idx,
733 debug_str_element (element));
734
736 element->size - sizeof(struct ConsensusElement),
738 m->element_type = ce->payload_type;
739 GNUNET_memcpy (&m[1],
740 &ce[1],
741 element->size - sizeof(struct ConsensusElement));
742 GNUNET_MQ_send (session->client_mq,
743 ev);
744 }
745 else
746 {
748 "P%d: finished iterating elements for client\n",
749 session->local_peer_idx);
752 GNUNET_MQ_send (session->client_mq,
753 ev);
754 }
755 return GNUNET_YES;
756}
757
758
759static struct SetEntry *
761 const struct SetKey *key)
762{
763 struct GNUNET_HashCode hash;
764
766 "P%u: looking up set {%s}\n",
767 session->local_peer_idx,
768 debug_str_set_key (key));
769
770 GNUNET_assert (SET_KIND_NONE != key->set_kind);
772 sizeof(struct SetKey),
773 &hash);
775 &hash);
776}
777
778
779static struct DiffEntry *
781 const struct DiffKey *key)
782{
783 struct GNUNET_HashCode hash;
784
786 "P%u: looking up diff {%s}\n",
787 session->local_peer_idx,
788 debug_str_diff_key (key));
789 GNUNET_assert (DIFF_KIND_NONE != key->diff_kind);
791 sizeof(struct DiffKey),
792 &hash);
794 &hash);
795}
796
797
798static struct ReferendumEntry *
800 const struct RfnKey *key)
801{
802 struct GNUNET_HashCode hash;
803
805 "P%u: looking up rfn {%s}\n",
806 session->local_peer_idx,
807 debug_str_rfn_key (key));
808 GNUNET_assert (RFN_KIND_NONE != key->rfn_kind);
810 sizeof(struct RfnKey),
811 &hash);
813 &hash);
814}
815
816
817static void
818diff_insert (struct DiffEntry *diff,
819 int weight,
820 const struct GNUNET_SET_Element *element)
821{
822 struct DiffElementInfo *di;
823 struct GNUNET_HashCode hash;
824
825 GNUNET_assert ((1 == weight) || (-1 == weight));
826
828 "diff_insert with element size %u\n",
829 element->size);
830
832 "hashing element\n");
833
834 GNUNET_SET_element_hash (element, &hash);
835
837 "hashed element\n");
838
839 di = GNUNET_CONTAINER_multihashmap_get (diff->changes, &hash);
840
841 if (NULL == di)
842 {
843 di = GNUNET_new (struct DiffElementInfo);
844 di->element = GNUNET_SET_element_dup (element);
847 &hash,
848 di,
850 }
851
852 di->weight = weight;
853}
854
855
856static void
858 uint16_t commit_peer)
859{
860 GNUNET_assert (commit_peer < rfn->num_peers);
861
862 rfn->peer_commited[commit_peer] = GNUNET_YES;
863}
864
865
866static void
868 uint16_t contested_peer)
869{
870 GNUNET_assert (contested_peer < rfn->num_peers);
871
872 rfn->peer_contested[contested_peer] = GNUNET_YES;
873}
874
875
876static uint16_t
878{
879 uint16_t ret;
880
881 ret = 0;
882 for (uint16_t i = 0; i < rfn->num_peers; i++)
883 if ((GNUNET_YES == rfn->peer_commited[i]) && (GNUNET_NO ==
884 rfn->peer_contested[i]))
885 ret++;
886
887 return ret;
888}
889
890
891static void
893 uint16_t voting_peer,
894 enum ReferendumVote vote,
895 const struct GNUNET_SET_Element *element)
896{
897 struct RfnElementInfo *ri;
898 struct GNUNET_HashCode hash;
899
900 GNUNET_assert (voting_peer < rfn->num_peers);
901
902 /* Explicit voting only makes sense with VOTE_ADD or VOTE_REMOTE,
903 since VOTE_KEEP is implicit in not voting. */
904 GNUNET_assert ((VOTE_ADD == vote) || (VOTE_REMOVE == vote));
905
906 GNUNET_SET_element_hash (element, &hash);
908
909 if (NULL == ri)
910 {
911 ri = GNUNET_new (struct RfnElementInfo);
912 ri->element = GNUNET_SET_element_dup (element);
913 ri->votes = GNUNET_new_array (rfn->num_peers, int);
916 &hash, ri,
918 }
919
920 ri->votes[voting_peer] = GNUNET_YES;
921 ri->proposal = vote;
922}
923
924
925static uint16_t
927{
928 uint16_t me = task->step->session->local_peer_idx;
929
930 if (task->key.peer1 == me)
931 return task->key.peer2;
932 return task->key.peer1;
933}
934
935
936static int
937cmp_uint64_t (const void *pa, const void *pb)
938{
939 uint64_t a = *(uint64_t *) pa;
940 uint64_t b = *(uint64_t *) pb;
941
942 if (a == b)
943 return 0;
944 if (a < b)
945 return -1;
946 return 1;
947}
948
949
959static void
960set_result_cb (void *cls,
961 const struct GNUNET_SET_Element *element,
962 uint64_t current_size,
964{
965 struct TaskEntry *task = cls;
966 struct ConsensusSession *session = task->step->session;
967 struct SetEntry *output_set = NULL;
968 struct DiffEntry *output_diff = NULL;
969 struct ReferendumEntry *output_rfn = NULL;
970 unsigned int other_idx;
971 struct SetOpCls *setop;
972 const struct ConsensusElement *consensus_element = NULL;
973
974 if (NULL != element)
975 {
977 "P%u: got element of type %u, status %u\n",
978 session->local_peer_idx,
979 (unsigned) element->element_type,
980 (unsigned) status);
982 element->element_type);
983 consensus_element = element->data;
984 }
985
986 setop = &task->cls.setop;
987
988
990 "P%u: got set result for {%s}, status %u\n",
991 session->local_peer_idx,
992 debug_str_task_key (&task->key),
993 status);
994
995 if (GNUNET_NO == task->is_started)
996 {
997 GNUNET_break_op (0);
998 return;
999 }
1000
1001 if (GNUNET_YES == task->is_finished)
1002 {
1003 GNUNET_break_op (0);
1004 return;
1005 }
1006
1007 other_idx = task_other_peer (task);
1008
1009 if (SET_KIND_NONE != setop->output_set.set_kind)
1010 {
1011 output_set = lookup_set (session,
1012 &setop->output_set);
1013 GNUNET_assert (NULL != output_set);
1014 }
1015
1016 if (DIFF_KIND_NONE != setop->output_diff.diff_kind)
1017 {
1018 output_diff = lookup_diff (session, &setop->output_diff);
1019 GNUNET_assert (NULL != output_diff);
1020 }
1021
1022 if (RFN_KIND_NONE != setop->output_rfn.rfn_kind)
1023 {
1024 output_rfn = lookup_rfn (session, &setop->output_rfn);
1025 GNUNET_assert (NULL != output_rfn);
1026 }
1027
1028 if (GNUNET_YES == session->peers_blacklisted[other_idx])
1029 {
1030 /* Peer might have been blacklisted
1031 by a gradecast running in parallel, ignore elements from now */
1033 return;
1035 return;
1036 }
1037
1038 if ((NULL != consensus_element) && (0 != consensus_element->marker))
1039 {
1041 "P%u: got some marker\n",
1042 session->local_peer_idx);
1043 if ((GNUNET_YES == setop->transceive_contested) &&
1044 (CONSENSUS_MARKER_CONTESTED == consensus_element->marker))
1045 {
1046 GNUNET_assert (NULL != output_rfn);
1047 rfn_contest (output_rfn, task_other_peer (task));
1048 return;
1049 }
1050
1051 if (CONSENSUS_MARKER_SIZE == consensus_element->marker)
1052 {
1054 "P%u: got size marker\n",
1055 session->local_peer_idx);
1056
1057 {
1058 struct ConsensusSizeElement *cse = (void *) consensus_element;
1059 uint64_t *copy;
1060
1061 if (cse->sender_index == other_idx)
1062 {
1063 if (NULL == session->first_sizes_received)
1065 ,
1066 uint64_t);
1067 session->first_sizes_received[other_idx] = GNUNET_ntohll (cse->size);
1068
1069 copy = GNUNET_memdup (session->first_sizes_received,
1070 sizeof(uint64_t) * session->num_peers);
1071 qsort (copy, session->num_peers, sizeof(uint64_t), cmp_uint64_t);
1072 session->lower_bound = copy[session->num_peers / 3 + 1];
1074 "P%u: lower bound %llu\n",
1075 session->local_peer_idx,
1076 (long long) session->lower_bound);
1077 GNUNET_free (copy);
1078 }
1079 return;
1080 }
1081 }
1082
1083 return;
1084 }
1085
1086 switch (status)
1087 {
1089 GNUNET_assert (NULL != consensus_element);
1091 "Adding element in Task {%s}\n",
1092 debug_str_task_key (&task->key));
1093 if (NULL != output_set)
1094 {
1095 // FIXME: record pending adds, use callback
1096 GNUNET_SET_add_element (output_set->h,
1097 element,
1098 NULL,
1099 NULL);
1100#ifdef GNUNET_EXTRA_LOGGING
1102 "P%u: adding element %s into set {%s} of task {%s}\n",
1103 session->local_peer_idx,
1104 debug_str_element (element),
1105 debug_str_set_key (&setop->output_set),
1106 debug_str_task_key (&task->key));
1107#endif
1108 }
1109 if (NULL != output_diff)
1110 {
1111 diff_insert (output_diff, 1, element);
1112#ifdef GNUNET_EXTRA_LOGGING
1114 "P%u: adding element %s into diff {%s} of task {%s}\n",
1115 session->local_peer_idx,
1116 debug_str_element (element),
1117 debug_str_diff_key (&setop->output_diff),
1118 debug_str_task_key (&task->key));
1119#endif
1120 }
1121 if (NULL != output_rfn)
1122 {
1123 rfn_vote (output_rfn, task_other_peer (task), VOTE_ADD, element);
1124#ifdef GNUNET_EXTRA_LOGGING
1126 "P%u: adding element %s into rfn {%s} of task {%s}\n",
1127 session->local_peer_idx,
1128 debug_str_element (element),
1129 debug_str_rfn_key (&setop->output_rfn),
1130 debug_str_task_key (&task->key));
1131#endif
1132 }
1133 // XXX: add result to structures in task
1134 break;
1135
1137 GNUNET_assert (NULL != consensus_element);
1138 if (GNUNET_YES == setop->do_not_remove)
1139 break;
1140 if (CONSENSUS_MARKER_CONTESTED == consensus_element->marker)
1141 break;
1143 "Removing element in Task {%s}\n",
1144 debug_str_task_key (&task->key));
1145 if (NULL != output_set)
1146 {
1147 // FIXME: record pending adds, use callback
1148 GNUNET_SET_remove_element (output_set->h,
1149 element,
1150 NULL,
1151 NULL);
1152#ifdef GNUNET_EXTRA_LOGGING
1154 "P%u: removing element %s from set {%s} of task {%s}\n",
1155 session->local_peer_idx,
1156 debug_str_element (element),
1157 debug_str_set_key (&setop->output_set),
1158 debug_str_task_key (&task->key));
1159#endif
1160 }
1161 if (NULL != output_diff)
1162 {
1163 diff_insert (output_diff, -1, element);
1164#ifdef GNUNET_EXTRA_LOGGING
1166 "P%u: removing element %s from diff {%s} of task {%s}\n",
1167 session->local_peer_idx,
1168 debug_str_element (element),
1169 debug_str_diff_key (&setop->output_diff),
1170 debug_str_task_key (&task->key));
1171#endif
1172 }
1173 if (NULL != output_rfn)
1174 {
1175 rfn_vote (output_rfn, task_other_peer (task), VOTE_REMOVE, element);
1176#ifdef GNUNET_EXTRA_LOGGING
1178 "P%u: removing element %s from rfn {%s} of task {%s}\n",
1179 session->local_peer_idx,
1180 debug_str_element (element),
1181 debug_str_rfn_key (&setop->output_rfn),
1182 debug_str_task_key (&task->key));
1183#endif
1184 }
1185 break;
1186
1188 // XXX: check first if any changes to the underlying
1189 // set are still pending
1191 "P%u: Finishing setop in Task {%s} (%u/%u)\n",
1192 session->local_peer_idx,
1193 debug_str_task_key (&task->key),
1194 (unsigned int) task->step->finished_tasks,
1195 (unsigned int) task->step->tasks_len);
1196 if (NULL != output_rfn)
1197 {
1198 rfn_commit (output_rfn, task_other_peer (task));
1199 }
1200 if (PHASE_KIND_ALL_TO_ALL == task->key.kind)
1201 {
1202 session->first_size = current_size;
1203 }
1204 finish_task (task);
1205 break;
1206
1208 // XXX: cleanup
1209 GNUNET_break_op (0);
1210 finish_task (task);
1211 return;
1212
1213 default:
1214 /* not reached */
1215 GNUNET_assert (0);
1216 }
1217}
1218
1219
1220#ifdef EVIL
1221
1222enum EvilnessType
1223{
1224 EVILNESS_NONE,
1225 EVILNESS_CRAM_ALL,
1226 EVILNESS_CRAM_LEAD,
1227 EVILNESS_CRAM_ECHO,
1228 EVILNESS_SLACK,
1229 EVILNESS_SLACK_A2A,
1230};
1231
1232enum EvilnessSubType
1233{
1234 EVILNESS_SUB_NONE,
1235 EVILNESS_SUB_REPLACEMENT,
1236 EVILNESS_SUB_NO_REPLACEMENT,
1237};
1238
1239struct Evilness
1240{
1241 enum EvilnessType type;
1242 enum EvilnessSubType subtype;
1243 unsigned int num;
1244};
1245
1246
1247static int
1248parse_evilness_cram_subtype (const char *evil_subtype_str,
1249 struct Evilness *evil)
1250{
1251 if (0 == strcmp ("replace", evil_subtype_str))
1252 {
1253 evil->subtype = EVILNESS_SUB_REPLACEMENT;
1254 }
1255 else if (0 == strcmp ("noreplace", evil_subtype_str))
1256 {
1257 evil->subtype = EVILNESS_SUB_NO_REPLACEMENT;
1258 }
1259 else
1260 {
1262 "Malformed field '%s' in EVIL_SPEC (unknown subtype), behaving like a good peer.\n",
1263 evil_subtype_str);
1264 return GNUNET_SYSERR;
1265 }
1266 return GNUNET_OK;
1267}
1268
1269
1270static void
1271get_evilness (struct ConsensusSession *session, struct Evilness *evil)
1272{
1273 char *evil_spec;
1274 char *field;
1275 char *evil_type_str = NULL;
1276 char *evil_subtype_str = NULL;
1277
1278 GNUNET_assert (NULL != evil);
1279
1281 "EVIL_SPEC",
1282 &evil_spec))
1283 {
1285 "P%u: no evilness\n",
1286 session->local_peer_idx);
1287 evil->type = EVILNESS_NONE;
1288 return;
1289 }
1291 "P%u: got evilness spec\n",
1292 session->local_peer_idx);
1293
1294 for (field = strtok (evil_spec, "/");
1295 NULL != field;
1296 field = strtok (NULL, "/"))
1297 {
1298 unsigned int peer_num;
1299 unsigned int evil_num;
1300 int ret;
1301
1302 evil_type_str = NULL;
1303 evil_subtype_str = NULL;
1304
1305 ret = sscanf (field, "%u;%m[a-z-];%m[a-z-];%u", &peer_num, &evil_type_str,
1306 &evil_subtype_str, &evil_num);
1307
1308 if (ret != 4)
1309 {
1311 "Malformed field '%s' in EVIL_SPEC (expected 4 components got %d), behaving like a good peer.\n",
1312 field,
1313 ret);
1314 goto not_evil;
1315 }
1316
1317 GNUNET_assert (NULL != evil_type_str);
1318 GNUNET_assert (NULL != evil_subtype_str);
1319
1320 if (peer_num == session->local_peer_idx)
1321 {
1322 if (0 == strcmp ("slack", evil_type_str))
1323 {
1324 evil->type = EVILNESS_SLACK;
1325 }
1326 if (0 == strcmp ("slack-a2a", evil_type_str))
1327 {
1328 evil->type = EVILNESS_SLACK_A2A;
1329 }
1330 else if (0 == strcmp ("cram-all", evil_type_str))
1331 {
1332 evil->type = EVILNESS_CRAM_ALL;
1333 evil->num = evil_num;
1334 if (GNUNET_OK != parse_evilness_cram_subtype (evil_subtype_str, evil))
1335 goto not_evil;
1336 }
1337 else if (0 == strcmp ("cram-lead", evil_type_str))
1338 {
1339 evil->type = EVILNESS_CRAM_LEAD;
1340 evil->num = evil_num;
1341 if (GNUNET_OK != parse_evilness_cram_subtype (evil_subtype_str, evil))
1342 goto not_evil;
1343 }
1344 else if (0 == strcmp ("cram-echo", evil_type_str))
1345 {
1346 evil->type = EVILNESS_CRAM_ECHO;
1347 evil->num = evil_num;
1348 if (GNUNET_OK != parse_evilness_cram_subtype (evil_subtype_str, evil))
1349 goto not_evil;
1350 }
1351 else
1352 {
1354 "Malformed field '%s' in EVIL_SPEC (unknown type), behaving like a good peer.\n",
1355 evil_type_str);
1356 goto not_evil;
1357 }
1358 goto cleanup;
1359 }
1360 /* No GNUNET_free since memory was allocated by libc */
1361 free (evil_type_str);
1362 evil_type_str = NULL;
1363 evil_subtype_str = NULL;
1364 }
1365not_evil:
1366 evil->type = EVILNESS_NONE;
1367cleanup:
1368 GNUNET_free (evil_spec);
1369 /* no GNUNET_free since it wasn't
1370 * allocated with GNUNET_malloc */
1371 if (NULL != evil_type_str)
1372 free (evil_type_str);
1373 if (NULL != evil_subtype_str)
1374 free (evil_subtype_str);
1375}
1376
1377
1378#endif
1379
1380
1384static void
1386 struct TaskEntry *task)
1387{
1388 struct SetEntry *set;
1389 struct SetOpCls *setop = &task->cls.setop;
1390
1391 GNUNET_assert (NULL != setop->op);
1392 set = lookup_set (session, &setop->input_set);
1393 GNUNET_assert (NULL != set);
1394
1395 if ((GNUNET_YES == setop->transceive_contested) && (GNUNET_YES ==
1396 set->is_contested))
1397 {
1398 struct GNUNET_SET_Element element;
1399 struct ConsensusElement ce = { 0 };
1400
1402 element.data = &ce;
1403 element.size = sizeof(struct ConsensusElement);
1405 GNUNET_SET_add_element (set->h, &element, NULL, NULL);
1406 }
1407
1408 if (PHASE_KIND_ALL_TO_ALL_2 == task->key.kind)
1409 {
1410 struct GNUNET_SET_Element element;
1411 struct ConsensusSizeElement cse = {
1412 .size = 0,
1413 .sender_index = 0
1414 };
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inserting size marker\n");
1417 cse.size = GNUNET_htonll (session->first_size);
1418 cse.sender_index = session->local_peer_idx;
1419 element.data = &cse;
1420 element.size = sizeof(struct ConsensusSizeElement);
1422 GNUNET_SET_add_element (set->h, &element, NULL, NULL);
1423 }
1424
1425#ifdef EVIL
1426 {
1427 struct Evilness evil;
1428
1429 get_evilness (session, &evil);
1430 if (EVILNESS_NONE != evil.type)
1431 {
1432 /* Useful for evaluation */
1434 "is evil",
1435 1,
1436 GNUNET_NO);
1437 }
1438 switch (evil.type)
1439 {
1440 case EVILNESS_CRAM_ALL:
1441 case EVILNESS_CRAM_LEAD:
1442 case EVILNESS_CRAM_ECHO:
1443 /* We're not cramming elements in the
1444 all-to-all round, since that would just
1445 add more elements to the result set, but
1446 wouldn't test robustness. */
1447 if (PHASE_KIND_ALL_TO_ALL == task->key.kind)
1448 {
1449 GNUNET_SET_commit (setop->op, set->h);
1450 break;
1451 }
1452 if ((EVILNESS_CRAM_LEAD == evil.type) &&
1453 ((PHASE_KIND_GRADECAST_LEADER != task->key.kind) ||
1454 (SET_KIND_CURRENT != set->key.set_kind) ))
1455 {
1456 GNUNET_SET_commit (setop->op, set->h);
1457 break;
1458 }
1459 if ((EVILNESS_CRAM_ECHO == evil.type) && (PHASE_KIND_GRADECAST_ECHO !=
1460 task->key.kind))
1461 {
1462 GNUNET_SET_commit (setop->op, set->h);
1463 break;
1464 }
1465 for (unsigned int i = 0; i < evil.num; i++)
1466 {
1467 struct GNUNET_SET_Element element;
1468 struct ConsensusStuffedElement se = {
1469 .ce.payload_type = 0,
1470 .ce.marker = 0,
1471 };
1472 element.data = &se;
1473 element.size = sizeof(struct ConsensusStuffedElement);
1475
1476 if (EVILNESS_SUB_REPLACEMENT == evil.subtype)
1477 {
1478 /* Always generate a new element. */
1479 GNUNET_CRYPTO_hash_create_random (&se.rand);
1480 }
1481 else if (EVILNESS_SUB_NO_REPLACEMENT == evil.subtype)
1482 {
1483 /* Always cram the same elements, derived from counter. */
1484 GNUNET_CRYPTO_hash (&i, sizeof(i), &se.rand);
1485 }
1486 else
1487 {
1488 GNUNET_assert (0);
1489 }
1490 GNUNET_SET_add_element (set->h, &element, NULL, NULL);
1491#ifdef GNUNET_EXTRA_LOGGING
1493 "P%u: evil peer: cramming element %s into set {%s} of task {%s}\n",
1494 session->local_peer_idx,
1495 debug_str_element (&element),
1496 debug_str_set_key (&setop->input_set),
1497 debug_str_task_key (&task->key));
1498#endif
1499 }
1501 "# stuffed elements",
1502 evil.num,
1503 GNUNET_NO);
1504 GNUNET_SET_commit (setop->op, set->h);
1505 break;
1506
1507 case EVILNESS_SLACK:
1509 "P%u: evil peer: slacking\n",
1510 (unsigned int) session->local_peer_idx);
1511
1512 /* Do nothing. */
1513 case EVILNESS_SLACK_A2A:
1514 if ((PHASE_KIND_ALL_TO_ALL_2 == task->key.kind) ||
1515 (PHASE_KIND_ALL_TO_ALL == task->key.kind))
1516 {
1517 struct GNUNET_SET_Handle *empty_set;
1519 GNUNET_SET_commit (setop->op, empty_set);
1520 GNUNET_SET_destroy (empty_set);
1521 }
1522 else
1523 {
1524 GNUNET_SET_commit (setop->op,
1525 set->h);
1526 }
1527 break;
1528
1529 case EVILNESS_NONE:
1530 GNUNET_SET_commit (setop->op,
1531 set->h);
1532 break;
1533 }
1534 }
1535#else
1536 if (GNUNET_NO == session->peers_blacklisted[task_other_peer (task)])
1537 {
1538 GNUNET_SET_commit (setop->op, set->h);
1539 }
1540 else
1541 {
1542 /* For our testcases, we don't want the blacklisted
1543 peers to wait. */
1545 setop->op = NULL;
1546 finish_task (task);
1547 }
1548#endif
1549}
1550
1551
1552static void
1554 struct DiffEntry *diff)
1555{
1556 struct GNUNET_HashCode hash;
1557
1558 GNUNET_CRYPTO_hash (&diff->key,
1559 sizeof(struct DiffKey),
1560 &hash);
1563 &hash,
1564 diff,
1566}
1567
1568
1569static void
1570put_set (struct ConsensusSession *session,
1571 struct SetEntry *set)
1572{
1573 struct GNUNET_HashCode hash;
1574
1575 GNUNET_assert (NULL != set->h);
1577 "Putting set %s\n",
1578 debug_str_set_key (&set->key));
1579 GNUNET_CRYPTO_hash (&set->key,
1580 sizeof(struct SetKey),
1581 &hash);
1584 &hash,
1585 set,
1587}
1588
1589
1590static void
1591put_rfn (struct ConsensusSession *session,
1592 struct ReferendumEntry *rfn)
1593{
1594 struct GNUNET_HashCode hash;
1595
1596 GNUNET_CRYPTO_hash (&rfn->key, sizeof(struct RfnKey), &hash);
1599 &hash,
1600 rfn,
1602}
1603
1604
1605static void
1607{
1608 /* not implemented yet */
1609 GNUNET_assert (0);
1610}
1611
1612
1613static void
1615 struct ReferendumEntry *rfn,
1616 uint16_t voting_peer,
1617 uint16_t num_peers)
1618{
1620 struct DiffElementInfo *di;
1621
1623
1624 while (GNUNET_YES ==
1626 NULL,
1627 (const void **) &di))
1628 {
1629 if (di->weight > 0)
1630 {
1631 rfn_vote (rfn, voting_peer, VOTE_ADD, di->element);
1632 }
1633 if (di->weight < 0)
1634 {
1635 rfn_vote (rfn, voting_peer, VOTE_REMOVE, di->element);
1636 }
1637 }
1638
1640}
1641
1642
1643static struct DiffEntry *
1645{
1646 struct DiffEntry *d = GNUNET_new (struct DiffEntry);
1647
1649 GNUNET_NO);
1650
1651 return d;
1652}
1653
1654
1655#if 0
1656static struct DiffEntry *
1657diff_compose (struct DiffEntry *diff_1,
1658 struct DiffEntry *diff_2)
1659{
1660 struct DiffEntry *diff_new;
1662 struct DiffElementInfo *di;
1663
1664 diff_new = diff_create ();
1665
1667 while (GNUNET_YES ==
1669 NULL,
1670 (const void **) &di))
1671 {
1672 diff_insert (diff_new,
1673 di->weight,
1674 di->element);
1675 }
1677
1679 while (GNUNET_YES ==
1681 NULL,
1682 (const void **) &di))
1683 {
1684 diff_insert (diff_new,
1685 di->weight,
1686 di->element);
1687 }
1689
1690 return diff_new;
1691}
1692
1693
1694#endif
1695
1696
1697static struct ReferendumEntry *
1699{
1700 struct ReferendumEntry *rfn;
1701
1702 rfn = GNUNET_new (struct ReferendumEntry);
1704 rfn->peer_commited = GNUNET_new_array (size, int);
1705 rfn->peer_contested = GNUNET_new_array (size, int);
1706 rfn->num_peers = size;
1707
1708 return rfn;
1709}
1710
1711
1712#if UNUSED
1713static void
1714diff_destroy (struct DiffEntry *diff)
1715{
1717 GNUNET_free (diff);
1718}
1719
1720
1721#endif
1722
1723
1729static void
1731 const struct RfnElementInfo *ri,
1732 uint16_t *ret_majority,
1733 enum ReferendumVote *ret_vote)
1734{
1735 uint16_t votes_yes = 0;
1736 uint16_t num_commited = 0;
1737
1739 "Computing rfn majority for element %s of rfn {%s}\n",
1740 debug_str_element (ri->element),
1741 debug_str_rfn_key (&rfn->key));
1742
1743 for (uint16_t i = 0; i < rfn->num_peers; i++)
1744 {
1745 if (GNUNET_NO == rfn->peer_commited[i])
1746 continue;
1747 num_commited++;
1748
1749 if (GNUNET_YES == ri->votes[i])
1750 votes_yes++;
1751 }
1752
1753 if (votes_yes > (num_commited) / 2)
1754 {
1755 *ret_vote = ri->proposal;
1756 *ret_majority = votes_yes;
1757 }
1758 else
1759 {
1760 *ret_vote = VOTE_STAY;
1761 *ret_majority = num_commited - votes_yes;
1762 }
1763}
1764
1765
1767{
1770};
1771
1772
1773static void
1774set_copy_cb (void *cls,
1775 struct GNUNET_SET_Handle *copy)
1776{
1777 struct SetCopyCls *scc = cls;
1778 struct TaskEntry *task = scc->task;
1779 struct SetKey dst_set_key = scc->dst_set_key;
1780 struct SetEntry *set;
1781 struct SetHandle *sh = GNUNET_new (struct SetHandle);
1782
1783 sh->h = copy;
1786 sh);
1787
1788 GNUNET_free (scc);
1789 set = GNUNET_new (struct SetEntry);
1790 set->h = copy;
1791 set->key = dst_set_key;
1792 put_set (task->step->session, set);
1793
1794 task->start (task);
1795}
1796
1797
1802static void
1804 struct SetKey *src_set_key,
1805 struct SetKey *dst_set_key)
1806{
1807 struct SetEntry *src_set;
1808 struct SetCopyCls *scc = GNUNET_new (struct SetCopyCls);
1809
1811 "Copying set {%s} to {%s} for task {%s}\n",
1812 debug_str_set_key (src_set_key),
1813 debug_str_set_key (dst_set_key),
1814 debug_str_task_key (&task->key));
1815
1816 scc->task = task;
1817 scc->dst_set_key = *dst_set_key;
1818 src_set = lookup_set (task->step->session, src_set_key);
1819 GNUNET_assert (NULL != src_set);
1820 GNUNET_SET_copy_lazy (src_set->h,
1822 scc);
1823}
1824
1825
1834
1835
1836static void
1838{
1839 struct SetMutationProgressCls *pc = cls;
1840
1841 GNUNET_assert (pc->num_pending > 0);
1842
1843 pc->num_pending--;
1844
1845 if (0 == pc->num_pending)
1846 {
1847 struct TaskEntry *task = pc->task;
1848 GNUNET_free (pc);
1849 finish_task (task);
1850 }
1851}
1852
1853
1854static void
1856{
1857 if (GNUNET_YES == step->is_running)
1858 return;
1859 if (GNUNET_YES == step->is_finished)
1860 return;
1862 return;
1863
1865
1866#ifdef GNUNET_EXTRA_LOGGING
1868 "Finishing step `%s' early.\n",
1869 step->debug_name);
1870#endif
1871
1872 for (unsigned int i = 0; i < step->subordinates_len; i++)
1873 {
1876#ifdef GNUNET_EXTRA_LOGGING
1878 "Decreased pending_prereq to %u for step `%s'.\n",
1879 (unsigned int) step->subordinates[i]->pending_prereq,
1881#endif
1883 }
1884
1885 // XXX: maybe schedule as task to avoid recursion?
1887}
1888
1889
1890static void
1892{
1896
1897#ifdef GNUNET_EXTRA_LOGGING
1899 "All tasks of step `%s' with %u subordinates finished.\n",
1902#endif
1903
1904 for (unsigned int i = 0; i < step->subordinates_len; i++)
1905 {
1908#ifdef GNUNET_EXTRA_LOGGING
1910 "Decreased pending_prereq to %u for step `%s'.\n",
1911 (unsigned int) step->subordinates[i]->pending_prereq,
1913#endif
1914 }
1915
1917
1918 // XXX: maybe schedule as task to avoid recursion?
1920}
1921
1922
1929static void
1931{
1932 struct ConsensusSession *session = task->step->session;
1933 struct SetKey sk_in;
1934 struct SetKey sk_out;
1935 struct RfnKey rk_in;
1936 struct SetEntry *set_out;
1937 struct ReferendumEntry *rfn_in;
1939 struct RfnElementInfo *ri;
1940 struct SetMutationProgressCls *progress_cls;
1941 uint16_t worst_majority = UINT16_MAX;
1942
1943 sk_in = (struct SetKey) { SET_KIND_CURRENT, task->key.repetition };
1944 rk_in = (struct RfnKey) { RFN_KIND_GRADECAST_RESULT, task->key.repetition };
1945 sk_out = (struct SetKey) { SET_KIND_CURRENT, task->key.repetition + 1 };
1946
1947 set_out = lookup_set (session, &sk_out);
1948 if (NULL == set_out)
1949 {
1951 &sk_in,
1952 &sk_out);
1953 return;
1954 }
1955
1956 rfn_in = lookup_rfn (session, &rk_in);
1957 GNUNET_assert (NULL != rfn_in);
1958
1959 progress_cls = GNUNET_new (struct SetMutationProgressCls);
1960 progress_cls->task = task;
1961
1963
1964 while (GNUNET_YES ==
1966 NULL,
1967 (const void **) &ri))
1968 {
1969 uint16_t majority_num;
1970 enum ReferendumVote majority_vote;
1971
1972 rfn_majority (rfn_in, ri, &majority_num, &majority_vote);
1973
1974 if (worst_majority > majority_num)
1975 worst_majority = majority_num;
1976
1977 switch (majority_vote)
1978 {
1979 case VOTE_ADD:
1980 progress_cls->num_pending++;
1982 GNUNET_SET_add_element (set_out->h,
1983 ri->element,
1985 progress_cls));
1987 "P%u: apply round: adding element %s with %u-majority.\n",
1988 session->local_peer_idx,
1989 debug_str_element (ri->element), majority_num);
1990 break;
1991
1992 case VOTE_REMOVE:
1993 progress_cls->num_pending++;
1995 GNUNET_SET_remove_element (set_out->h,
1996 ri->element,
1998 progress_cls));
2000 "P%u: apply round: deleting element %s with %u-majority.\n",
2001 session->local_peer_idx,
2002 debug_str_element (ri->element), majority_num);
2003 break;
2004
2005 case VOTE_STAY:
2007 "P%u: apply round: keeping element %s with %u-majority.\n",
2008 session->local_peer_idx,
2009 debug_str_element (ri->element), majority_num);
2010 // do nothing
2011 break;
2012
2013 default:
2014 GNUNET_assert (0);
2015 break;
2016 }
2017 }
2018
2019 if (0 == progress_cls->num_pending)
2020 {
2021 // call closure right now, no pending ops
2022 GNUNET_free (progress_cls);
2023 finish_task (task);
2024 }
2025
2026 {
2027 uint16_t thresh = (session->num_peers / 3) * 2;
2028
2029 if (worst_majority >= thresh)
2030 {
2031 switch (session->early_stopping)
2032 {
2036 "P%u: Stopping early (after one more superround)\n",
2037 session->local_peer_idx);
2038 break;
2039
2042 "P%u: finishing steps due to early finish\n",
2043 session->local_peer_idx);
2045 {
2046 struct Step *step;
2047 for (step = session->steps_head; NULL != step; step = step->next)
2048 try_finish_step_early (step);
2049 }
2050 break;
2051
2053 /* We shouldn't be here anymore after early stopping */
2054 GNUNET_break (0);
2055 break;
2056
2057 default:
2058 GNUNET_assert (0);
2059 break;
2060 }
2061 }
2063 {
2064 // Our assumption about the number of bad peers
2065 // has been broken.
2066 GNUNET_break_op (0);
2067 }
2068 else
2069 {
2071 "P%u: NOT finishing early (majority not good enough)\n",
2073 }
2074 }
2076}
2077
2078
2079static void
2081{
2082 struct ConsensusSession *session = task->step->session;
2083 struct ReferendumEntry *output_rfn;
2084 struct ReferendumEntry *input_rfn;
2085 struct DiffEntry *input_diff;
2086 struct RfnKey rfn_key;
2087 struct DiffKey diff_key;
2089 struct RfnElementInfo *ri;
2090 unsigned int gradecast_confidence = 2;
2091
2092 rfn_key = (struct RfnKey) { RFN_KIND_GRADECAST_RESULT, task->key.repetition };
2093 output_rfn = lookup_rfn (session, &rfn_key);
2094 if (NULL == output_rfn)
2095 {
2096 output_rfn = rfn_create (session->num_peers);
2097 output_rfn->key = rfn_key;
2098 put_rfn (session, output_rfn);
2099 }
2100
2101 diff_key = (struct DiffKey) { DIFF_KIND_LEADER_PROPOSAL, task->key.repetition,
2102 task->key.leader };
2103 input_diff = lookup_diff (session, &diff_key);
2104 GNUNET_assert (NULL != input_diff);
2105
2106 rfn_key = (struct RfnKey) { RFN_KIND_ECHO, task->key.repetition,
2107 task->key.leader };
2108 input_rfn = lookup_rfn (session, &rfn_key);
2109 GNUNET_assert (NULL != input_rfn);
2110
2112 input_rfn->rfn_elements);
2113
2114 apply_diff_to_rfn (input_diff, output_rfn, task->key.leader,
2115 session->num_peers);
2116
2117 while (GNUNET_YES ==
2119 NULL,
2120 (const void **) &ri))
2121 {
2122 uint16_t majority_num;
2123 enum ReferendumVote majority_vote;
2124
2125 // XXX: we need contested votes and non-contested votes here
2126 rfn_majority (input_rfn, ri, &majority_num, &majority_vote);
2127
2128 if (majority_num <= session->num_peers / 3)
2129 majority_vote = VOTE_REMOVE;
2130
2131 switch (majority_vote)
2132 {
2133 case VOTE_STAY:
2134 break;
2135
2136 case VOTE_ADD:
2137 rfn_vote (output_rfn, task->key.leader, VOTE_ADD, ri->element);
2138 break;
2139
2140 case VOTE_REMOVE:
2141 rfn_vote (output_rfn, task->key.leader, VOTE_REMOVE, ri->element);
2142 break;
2143
2144 default:
2145 GNUNET_assert (0);
2146 break;
2147 }
2148 }
2150
2151 {
2152 uint16_t noncontested;
2153 noncontested = rfn_noncontested (input_rfn);
2154 if (noncontested < (session->num_peers / 3) * 2)
2155 {
2156 gradecast_confidence = GNUNET_MIN (1, gradecast_confidence);
2157 }
2158 if (noncontested < (session->num_peers / 3) + 1)
2159 {
2160 gradecast_confidence = 0;
2161 }
2162 }
2163
2164 if (gradecast_confidence >= 1)
2165 rfn_commit (output_rfn, task->key.leader);
2166
2167 if (gradecast_confidence <= 1)
2168 session->peers_blacklisted[task->key.leader] = GNUNET_YES;
2169
2170 finish_task (task);
2171}
2172
2173
2174static void
2176{
2177 struct SetEntry *input;
2178 struct SetOpCls *setop = &task->cls.setop;
2179 struct ConsensusSession *session = task->step->session;
2180
2181 input = lookup_set (session, &setop->input_set);
2182 GNUNET_assert (NULL != input);
2183 GNUNET_assert (NULL != input->h);
2184
2185 /* We create the outputs for the operation here
2186 (rather than in the set operation callback)
2187 because we want something valid in there, even
2188 if the other peer doesn't talk to us */
2189
2190 if (SET_KIND_NONE != setop->output_set.set_kind)
2191 {
2192 /* If we don't have an existing output set,
2193 we clone the input set. */
2194 if (NULL == lookup_set (session, &setop->output_set))
2195 {
2197 &setop->input_set,
2198 &setop->output_set);
2199 return;
2200 }
2201 }
2202
2203 if (RFN_KIND_NONE != setop->output_rfn.rfn_kind)
2204 {
2205 if (NULL == lookup_rfn (session, &setop->output_rfn))
2206 {
2207 struct ReferendumEntry *rfn;
2208
2210 "P%u: output rfn <%s> missing, creating.\n",
2211 session->local_peer_idx,
2212 debug_str_rfn_key (&setop->output_rfn));
2213
2214 rfn = rfn_create (session->num_peers);
2215 rfn->key = setop->output_rfn;
2216 put_rfn (session, rfn);
2217 }
2218 }
2219
2220 if (DIFF_KIND_NONE != setop->output_diff.diff_kind)
2221 {
2222 if (NULL == lookup_diff (session, &setop->output_diff))
2223 {
2224 struct DiffEntry *diff;
2225
2226 diff = diff_create ();
2227 diff->key = setop->output_diff;
2228 put_diff (session, diff);
2229 }
2230 }
2231
2232 if ((task->key.peer1 == session->local_peer_idx) && (task->key.peer2 ==
2233 session->local_peer_idx))
2234 {
2235 /* XXX: mark the corresponding rfn as committed if necessary */
2236 finish_task (task);
2237 return;
2238 }
2239
2240 if (task->key.peer1 == session->local_peer_idx)
2241 {
2243
2245 "P%u: Looking up set {%s} to run remote union\n",
2246 session->local_peer_idx,
2247 debug_str_set_key (&setop->input_set));
2249 rcm.header.size = htons (sizeof(rcm));
2250 rcm.kind = htons (task->key.kind);
2251 rcm.peer1 = htons (task->key.peer1);
2252 rcm.peer2 = htons (task->key.peer2);
2253 rcm.leader = htons (task->key.leader);
2254 rcm.repetition = htons (task->key.repetition);
2255 rcm.is_contested = htons (0);
2256
2257 GNUNET_assert (NULL == setop->op);
2259 "P%u: initiating set op with P%u, our set is %s\n",
2260 session->local_peer_idx,
2261 task->key.peer2,
2262 debug_str_set_key (&setop->input_set));
2263
2264 {
2265 struct GNUNET_SET_Option opts[] = {
2266 { GNUNET_SET_OPTION_BYZANTINE, { .num = session->lower_bound } },
2268 };
2269
2270 // XXX: maybe this should be done while
2271 // setting up tasks alreays?
2272 setop->op = GNUNET_SET_prepare (&session->peers[task->key.peer2],
2273 &session->global_id,
2274 &rcm.header,
2276 opts,
2278 task);
2279
2280 commit_set (session, task);
2281 }
2282 }
2283 else if (task->key.peer2 == session->local_peer_idx)
2284 {
2285 /* Wait for the other peer to contact us */
2286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: waiting set op with P%u\n",
2287 session->local_peer_idx, task->key.peer1);
2288
2289 if (NULL != setop->op)
2290 {
2291 commit_set (session, task);
2292 }
2293 }
2294 else
2295 {
2296 /* We made an error while constructing the task graph. */
2297 GNUNET_assert (0);
2298 }
2299}
2300
2301
2302static void
2304{
2306 struct ReferendumEntry *input_rfn;
2307 struct RfnElementInfo *ri;
2308 struct SetEntry *output_set;
2309 struct SetMutationProgressCls *progress_cls;
2310 struct ConsensusSession *session = task->step->session;
2311 struct SetKey sk_in;
2312 struct SetKey sk_out;
2313 struct RfnKey rk_in;
2314
2315 sk_in = (struct SetKey) { SET_KIND_LEADER_PROPOSAL, task->key.repetition,
2316 task->key.leader };
2317 sk_out = (struct SetKey) { SET_KIND_ECHO_RESULT, task->key.repetition,
2318 task->key.leader };
2319 output_set = lookup_set (session, &sk_out);
2320 if (NULL == output_set)
2321 {
2323 &sk_in,
2324 &sk_out);
2325 return;
2326 }
2327
2328 {
2329 // FIXME: should be marked as a shallow copy, so
2330 // we can destroy everything correctly
2331 struct SetEntry *last_set = GNUNET_new (struct SetEntry);
2332
2333 last_set->h = output_set->h;
2334 last_set->key = (struct SetKey) { SET_KIND_LAST_GRADECAST };
2335 put_set (session, last_set);
2336 }
2337
2339 "Evaluating referendum in Task {%s}\n",
2340 debug_str_task_key (&task->key));
2341
2342 progress_cls = GNUNET_new (struct SetMutationProgressCls);
2343 progress_cls->task = task;
2344 rk_in = (struct RfnKey) { RFN_KIND_ECHO, task->key.repetition,
2345 task->key.leader };
2346 input_rfn = lookup_rfn (session, &rk_in);
2347 GNUNET_assert (NULL != input_rfn);
2349 input_rfn->rfn_elements);
2350 GNUNET_assert (NULL != iter);
2351
2352 while (GNUNET_YES ==
2354 NULL,
2355 (const void **) &ri))
2356 {
2357 enum ReferendumVote majority_vote;
2358 uint16_t majority_num;
2359
2360 rfn_majority (input_rfn, ri, &majority_num, &majority_vote);
2361
2362 if (majority_num < session->num_peers / 3)
2363 {
2364 /* It is not the case that all nonfaulty peers
2365 echoed the same value. Since we're doing a set reconciliation, we
2366 can't simply send "nothing" for the value. Thus we mark our 'confirm'
2367 reconciliation as contested. Other peers might not know that the
2368 leader is faulty, thus we still re-distribute in the confirmation
2369 round. *///
2370 output_set->is_contested = GNUNET_YES;
2371 }
2372
2373 switch (majority_vote)
2374 {
2375 case VOTE_ADD:
2376 progress_cls->num_pending++;
2378 GNUNET_SET_add_element (output_set->h,
2379 ri->element,
2381 progress_cls));
2382 break;
2383
2384 case VOTE_REMOVE:
2385 progress_cls->num_pending++;
2387 GNUNET_SET_remove_element (output_set->h,
2388 ri->element,
2390 progress_cls));
2391 break;
2392
2393 case VOTE_STAY:
2394 /* Nothing to do. */
2395 break;
2396
2397 default:
2398 /* not reached */
2399 GNUNET_assert (0);
2400 }
2401 }
2402
2404
2405 if (0 == progress_cls->num_pending)
2406 {
2407 // call closure right now, no pending ops
2408 GNUNET_free (progress_cls);
2409 finish_task (task);
2410 }
2411}
2412
2413
2414static void
2416{
2417 struct SetEntry *final_set;
2418 struct ConsensusSession *session = task->step->session;
2419
2420 final_set = lookup_set (session,
2421 &task->cls.finish.input_set);
2422 GNUNET_assert (NULL != final_set);
2423 GNUNET_SET_iterate (final_set->h,
2425 task);
2426}
2427
2428
2429static void
2431 struct TaskEntry *task)
2432{
2434 "P%u: starting task {%s}\n",
2435 session->local_peer_idx,
2436 debug_str_task_key (&task->key));
2439 GNUNET_assert (NULL != task->start);
2440 task->start (task);
2441 task->is_started = GNUNET_YES;
2442}
2443
2444
2445/*
2446 * Run all steps of the session that don't any
2447 * more dependencies.
2448 */
2449static void
2451{
2452 struct Step *step;
2453
2454 step = session->steps_head;
2455
2456 while (NULL != step)
2457 {
2458 if ((GNUNET_NO == step->is_running) && (0 == step->pending_prereq) &&
2459 (GNUNET_NO == step->is_finished))
2460 {
2461 GNUNET_assert (0 == step->finished_tasks);
2462
2463#ifdef GNUNET_EXTRA_LOGGING
2465 "P%u: Running step `%s' of round %d with %d tasks and %d subordinates\n",
2467 step->debug_name,
2468 step->round, step->tasks_len, step->subordinates_len);
2469#endif
2470
2471 step->is_running = GNUNET_YES;
2472 for (size_t i = 0; i < step->tasks_len; i++)
2473 start_task (session, step->tasks[i]);
2474
2475 /* Sometimes there is no task to trigger finishing the step, so we have to do it here. */
2476 if ((step->finished_tasks == step->tasks_len) && (GNUNET_NO ==
2477 step->is_finished))
2478 finish_step (step);
2479
2480 /* Running the next ready steps will be triggered by task completion */
2481 return;
2482 }
2483 step = step->next;
2484 }
2485
2486 return;
2487}
2488
2489
2490static void
2492{
2494 task->is_finished = GNUNET_YES;
2495 task->step->finished_tasks++;
2497 "P%u: Finishing Task {%s} (now %u/%u tasks finished in step)\n",
2498 task->step->session->local_peer_idx,
2499 debug_str_task_key (&task->key),
2500 (unsigned int) task->step->finished_tasks,
2501 (unsigned int) task->step->tasks_len);
2502
2503 if (task->step->finished_tasks == task->step->tasks_len)
2504 finish_step (task->step);
2505}
2506
2507
2515static int
2517 const struct ConsensusSession *session)
2518{
2519 for (int i = 0; i < session->num_peers; i++)
2520 if (0 == GNUNET_memcmp (peer, &session->peers[i]))
2521 return i;
2522 return -1;
2523}
2524
2525
2535static void
2537 const struct GNUNET_HashCode *local_session_id)
2538{
2539 const char *salt = "gnunet-service-consensus/session_id";
2540
2544 sizeof(struct GNUNET_HashCode),
2545 salt,
2546 strlen (salt),
2547 session->peers,
2548 session->num_peers * sizeof(struct
2550 GNUNET_CRYPTO_kdf_arg_auto (local_session_id)));
2551}
2552
2553
2561static int
2562peer_id_cmp (const void *h1,
2563 const void *h2)
2564{
2565 return memcmp (h1, h2, sizeof(struct GNUNET_PeerIdentity));
2566}
2567
2568
2576static void
2578 struct ConsensusSession *session,
2579 const struct GNUNET_CONSENSUS_JoinMessage *join_msg)
2580{
2581 const struct GNUNET_PeerIdentity *msg_peers
2582 = (const struct GNUNET_PeerIdentity *) &join_msg[1];
2583 int local_peer_in_list;
2584
2585 session->num_peers = ntohl (join_msg->num_peers);
2586
2587 /* Peers in the join message, may or may not include the local peer,
2588 Add it if it is missing. */
2589 local_peer_in_list = GNUNET_NO;
2590 for (unsigned int i = 0; i < session->num_peers; i++)
2591 {
2592 if (0 == GNUNET_memcmp (&msg_peers[i],
2593 &my_peer))
2594 {
2595 local_peer_in_list = GNUNET_YES;
2596 break;
2597 }
2598 }
2599 if (GNUNET_NO == local_peer_in_list)
2600 session->num_peers++;
2601
2602 session->peers = GNUNET_new_array (session->num_peers,
2603 struct GNUNET_PeerIdentity);
2604 if (GNUNET_NO == local_peer_in_list)
2605 session->peers[session->num_peers - 1] = my_peer;
2606 GNUNET_memcpy (session->peers,
2607 msg_peers,
2608 ntohl (join_msg->num_peers)
2609 * sizeof(struct GNUNET_PeerIdentity));
2610 qsort (session->peers,
2611 session->num_peers,
2612 sizeof (struct GNUNET_PeerIdentity),
2613 &peer_id_cmp);
2614}
2615
2616
2617static struct TaskEntry *
2618lookup_task (const struct ConsensusSession *session,
2619 const struct TaskKey *key)
2620{
2621 struct GNUNET_HashCode hash;
2622
2624 sizeof(struct TaskKey),
2625 &hash);
2627 "Looking up task hash %s\n",
2628 GNUNET_h2s (&hash));
2630 &hash);
2631}
2632
2633
2649static void
2650set_listen_cb (void *cls,
2651 const struct GNUNET_PeerIdentity *other_peer,
2652 const struct GNUNET_MessageHeader *context_msg,
2654{
2655 struct ConsensusSession *session = cls;
2656 struct TaskKey tk;
2657 struct TaskEntry *task;
2659
2660 if (NULL == context_msg)
2661 {
2662 GNUNET_break_op (0);
2663 return;
2664 }
2665
2667 context_msg->type))
2668 {
2669 GNUNET_break_op (0);
2670 return;
2671 }
2672
2673 if (sizeof(struct GNUNET_CONSENSUS_RoundContextMessage) != ntohs (
2674 context_msg->size))
2675 {
2676 GNUNET_break_op (0);
2677 return;
2678 }
2679
2680 cm = (struct GNUNET_CONSENSUS_RoundContextMessage *) context_msg;
2681
2682 tk = ((struct TaskKey) {
2683 .kind = ntohs (cm->kind),
2684 .peer1 = ntohs (cm->peer1),
2685 .peer2 = ntohs (cm->peer2),
2686 .repetition = ntohs (cm->repetition),
2687 .leader = ntohs (cm->leader),
2688 });
2689
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: got req for task %s\n",
2691 session->local_peer_idx, debug_str_task_key (&tk));
2692
2693 task = lookup_task (session, &tk);
2694
2695 if (NULL == task)
2696 {
2697 GNUNET_break_op (0);
2698 return;
2699 }
2700
2701 if (GNUNET_YES == task->is_finished)
2702 {
2703 GNUNET_break_op (0);
2704 return;
2705 }
2706
2707 if (task->key.peer2 != session->local_peer_idx)
2708 {
2709 /* We're being asked, so we must be the 2nd peer. */
2710 GNUNET_break_op (0);
2711 return;
2712 }
2713
2714 GNUNET_assert (! ((task->key.peer1 == session->local_peer_idx) &&
2715 (task->key.peer2 == session->local_peer_idx)));
2716
2717 {
2718 struct GNUNET_SET_Option opts[] = {
2719 { GNUNET_SET_OPTION_BYZANTINE, { .num = session->lower_bound } },
2721 };
2722
2725 opts,
2727 task);
2728 }
2729 /* If the task hasn't been started yet,
2730 we wait for that until we commit. */
2731
2732 if (GNUNET_YES == task->is_started)
2733 {
2734 commit_set (session, task);
2735 }
2736}
2737
2738
2739static void
2741 struct TaskEntry *t)
2742{
2743 struct GNUNET_HashCode round_hash;
2744 struct Step *s;
2745
2746 GNUNET_assert (NULL != t->step);
2747 t = GNUNET_memdup (t, sizeof(struct TaskEntry));
2748 s = t->step;
2749 if (s->tasks_len == s->tasks_cap)
2750 {
2751 unsigned int target_size = 3 * (s->tasks_cap + 1) / 2;
2753 s->tasks_cap,
2754 target_size);
2755 }
2756
2757#ifdef GNUNET_EXTRA_LOGGING
2758 GNUNET_assert (NULL != s->debug_name);
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting task <%s> into step `%s'\n",
2760 debug_str_task_key (&t->key),
2761 s->debug_name);
2762#endif
2763
2764 s->tasks[s->tasks_len] = t;
2765 s->tasks_len++;
2766
2767 GNUNET_CRYPTO_hash (&t->key,
2768 sizeof(struct TaskKey),
2769 &round_hash);
2772 &round_hash,
2773 t,
2775}
2776
2777
2778static void
2780{
2781 /* Given the fully constructed task graph
2782 with rounds for tasks, we can give the tasks timeouts. */
2783
2784 // unsigned int max_round;
2785
2786 /* XXX: implement! */
2787}
2788
2789
2790/*
2791 * Arrange two peers in some canonical order.
2792 */
2793static void
2794arrange_peers (uint16_t *p1,
2795 uint16_t *p2,
2796 uint16_t n)
2797{
2798 uint16_t a;
2799 uint16_t b;
2800
2801 GNUNET_assert (*p1 < n);
2802 GNUNET_assert (*p2 < n);
2803
2804 if (*p1 < *p2)
2805 {
2806 a = *p1;
2807 b = *p2;
2808 }
2809 else
2810 {
2811 a = *p2;
2812 b = *p1;
2813 }
2814
2815 /* For uniformly random *p1, *p2,
2816 this condition is true with 50% chance */
2817 if (((b - a) + n) % n <= n / 2)
2818 {
2819 *p1 = a;
2820 *p2 = b;
2821 }
2822 else
2823 {
2824 *p1 = b;
2825 *p2 = a;
2826 }
2827}
2828
2829
2833static void
2834step_depend_on (struct Step *step,
2835 struct Step *dep)
2836{
2837 /* We're not checking for cyclic dependencies,
2838 but this is a cheap sanity check. */
2839 GNUNET_assert (step != dep);
2840 GNUNET_assert (NULL != step);
2841 GNUNET_assert (NULL != dep);
2842 GNUNET_assert (dep->round <= step->round);
2843
2844#ifdef GNUNET_EXTRA_LOGGING
2845 /* Make sure we have complete debugging information.
2846 Also checks that we don't screw up too badly
2847 constructing the task graph. */
2848 GNUNET_assert (NULL != step->debug_name);
2849 GNUNET_assert (NULL != dep->debug_name);
2851 "Making step `%s' depend on `%s'\n",
2852 step->debug_name,
2853 dep->debug_name);
2854#endif
2855
2856 if (dep->subordinates_cap == dep->subordinates_len)
2857 {
2858 unsigned int target_size = 3 * (dep->subordinates_cap + 1) / 2;
2860 dep->subordinates_cap,
2861 target_size);
2862 }
2863
2865
2866 dep->subordinates[dep->subordinates_len] = step;
2867 dep->subordinates_len++;
2868
2869 step->pending_prereq++;
2870}
2871
2872
2873static struct Step *
2875 int round,
2876 int early_finishable)
2877{
2878 struct Step *step;
2879
2880 step = GNUNET_new (struct Step);
2881 step->session = session;
2882 step->round = round;
2886 step);
2887 return step;
2888}
2889
2890
2894static void
2896 uint16_t rep,
2897 uint16_t lead,
2898 struct Step *step_before,
2899 struct Step *step_after)
2900{
2901 uint16_t n = session->num_peers;
2902 uint16_t me = session->local_peer_idx;
2903 uint16_t p1;
2904 uint16_t p2;
2905 /* The task we're currently setting up. */
2906 struct TaskEntry task;
2907 struct Step *step;
2908 struct Step *prev_step;
2909 uint16_t round;
2910
2911 round = step_before->round + 1;
2912
2913 /* gcast step 1: leader disseminates */
2914 step = create_step (session,
2915 round,
2916 GNUNET_YES);
2917#ifdef GNUNET_EXTRA_LOGGING
2919 "disseminate leader %u rep %u",
2920 lead,
2921 rep);
2922#endif
2923 step_depend_on (step,
2924 step_before);
2925
2926 if (lead == me)
2927 {
2928 for (unsigned int k = 0; k < n; k++)
2929 {
2930 if (k == me)
2931 continue;
2932 p1 = me;
2933 p2 = k;
2934 arrange_peers (&p1, &p2, n);
2935 task = ((struct TaskEntry) {
2936 .step = step,
2937 .start = task_start_reconcile,
2938 .cancel = task_cancel_reconcile,
2939 .key = (struct TaskKey) { PHASE_KIND_GRADECAST_LEADER, p1, p2, rep,
2940 me },
2941 });
2942 task.cls.setop.input_set = (struct SetKey) { SET_KIND_CURRENT, rep };
2943 put_task (session->taskmap, &task);
2944 }
2945 /* We run this task to make sure that the leader
2946 has the stored the SET_KIND_LEADER set of himself,
2947 so it can participate in the rest of the gradecast
2948 without the code having to handle any special cases. */
2949 task = ((struct TaskEntry) {
2950 .step = step,
2951 .key = (struct TaskKey) { PHASE_KIND_GRADECAST_LEADER, me, me, rep, me },
2952 .start = task_start_reconcile,
2953 .cancel = task_cancel_reconcile,
2954 });
2955 task.cls.setop.input_set = (struct SetKey) { SET_KIND_CURRENT, rep };
2956 task.cls.setop.output_set = (struct SetKey) { SET_KIND_LEADER_PROPOSAL, rep,
2957 me };
2959 rep, me };
2960 put_task (session->taskmap, &task);
2961 }
2962 else
2963 {
2964 p1 = me;
2965 p2 = lead;
2966 arrange_peers (&p1, &p2, n);
2967 task = ((struct TaskEntry) {
2968 .step = step,
2969 .key = (struct TaskKey) { PHASE_KIND_GRADECAST_LEADER, p1, p2, rep,
2970 lead },
2971 .start = task_start_reconcile,
2972 .cancel = task_cancel_reconcile,
2973 });
2974 task.cls.setop.input_set = (struct SetKey) { SET_KIND_CURRENT, rep };
2975 task.cls.setop.output_set = (struct SetKey) { SET_KIND_LEADER_PROPOSAL, rep,
2976 lead };
2978 rep, lead };
2979 put_task (session->taskmap, &task);
2980 }
2981
2982 /* gcast phase 2: echo */
2983 prev_step = step;
2984 round += 1;
2985 step = create_step (session,
2986 round,
2987 GNUNET_YES);
2988#ifdef GNUNET_EXTRA_LOGGING
2990 "echo leader %u rep %u",
2991 lead,
2992 rep);
2993#endif
2994 step_depend_on (step,
2995 prev_step);
2996
2997 for (unsigned int k = 0; k < n; k++)
2998 {
2999 p1 = k;
3000 p2 = me;
3001 arrange_peers (&p1, &p2, n);
3002 task = ((struct TaskEntry) {
3003 .step = step,
3004 .key = (struct TaskKey) { PHASE_KIND_GRADECAST_ECHO, p1, p2, rep, lead },
3005 .start = task_start_reconcile,
3006 .cancel = task_cancel_reconcile,
3007 });
3008 task.cls.setop.input_set = (struct SetKey) { SET_KIND_LEADER_PROPOSAL, rep,
3009 lead };
3010 task.cls.setop.output_rfn = (struct RfnKey) { RFN_KIND_ECHO, rep, lead };
3011 put_task (session->taskmap, &task);
3012 }
3013
3014 prev_step = step;
3015 /* Same round, since step only has local tasks */
3016 step = create_step (session, round, GNUNET_YES);
3017#ifdef GNUNET_EXTRA_LOGGING
3018 GNUNET_asprintf (&step->debug_name, "echo grade leader %u rep %u", lead, rep);
3019#endif
3020 step_depend_on (step, prev_step);
3021
3022 arrange_peers (&p1, &p2, n);
3023 task = ((struct TaskEntry) {
3024 .key = (struct TaskKey) { PHASE_KIND_GRADECAST_ECHO_GRADE, -1, -1, rep,
3025 lead },
3026 .step = step,
3027 .start = task_start_eval_echo
3028 });
3029 put_task (session->taskmap, &task);
3030
3031 prev_step = step;
3032 round += 1;
3033 step = create_step (session, round, GNUNET_YES);
3034#ifdef GNUNET_EXTRA_LOGGING
3035 GNUNET_asprintf (&step->debug_name, "confirm leader %u rep %u", lead, rep);
3036#endif
3037 step_depend_on (step, prev_step);
3038
3039 /* gcast phase 3: confirmation and grading */
3040 for (unsigned int k = 0; k < n; k++)
3041 {
3042 p1 = k;
3043 p2 = me;
3044 arrange_peers (&p1, &p2, n);
3045 task = ((struct TaskEntry) {
3046 .step = step,
3047 .start = task_start_reconcile,
3048 .cancel = task_cancel_reconcile,
3049 .key = (struct TaskKey) { PHASE_KIND_GRADECAST_CONFIRM, p1, p2, rep,
3050 lead },
3051 });
3052 task.cls.setop.input_set = (struct SetKey) { SET_KIND_ECHO_RESULT, rep,
3053 lead };
3054 task.cls.setop.output_rfn = (struct RfnKey) { RFN_KIND_CONFIRM, rep, lead };
3055 /* If there was at least one element in the echo round that was
3056 contested (i.e. it had no n-t majority), then we let the other peers
3057 know, and other peers let us know. The contested flag for each peer is
3058 stored in the rfn. */
3060 put_task (session->taskmap, &task);
3061 }
3062
3063 prev_step = step;
3064 /* Same round, since step only has local tasks */
3065 step = create_step (session, round, GNUNET_YES);
3066#ifdef GNUNET_EXTRA_LOGGING
3067 GNUNET_asprintf (&step->debug_name, "confirm grade leader %u rep %u", lead,
3068 rep);
3069#endif
3070 step_depend_on (step, prev_step);
3071
3072 task = ((struct TaskEntry) {
3073 .step = step,
3074 .key = (struct TaskKey) { PHASE_KIND_GRADECAST_CONFIRM_GRADE, -1, -1, rep,
3075 lead },
3076 .start = task_start_grade,
3077 });
3078 put_task (session->taskmap, &task);
3079
3080 step_depend_on (step_after, step);
3081}
3082
3083
3084static void
3086{
3087 uint16_t n = session->num_peers;
3088 uint16_t t = n / 3;
3089 uint16_t me = session->local_peer_idx;
3090 /* The task we're currently setting up. */
3091 struct TaskEntry task;
3092 /* Current leader */
3093 unsigned int lead;
3094 struct Step *step;
3095 struct Step *prev_step;
3096 unsigned int round = 0;
3097
3098 // XXX: introduce first step,
3099 // where we wait for all insert acks
3100 // from the set service
3101
3102 /* faster but brittle all-to-all */
3103
3104 // XXX: Not implemented yet
3105
3106 /* all-to-all step */
3107
3109
3110#ifdef GNUNET_EXTRA_LOGGING
3111 step->debug_name = GNUNET_strdup ("all to all");
3112#endif
3113
3114 for (unsigned int i = 0; i < n; i++)
3115 {
3116 uint16_t p1;
3117 uint16_t p2;
3118
3119 p1 = me;
3120 p2 = i;
3121 arrange_peers (&p1, &p2, n);
3122 task = ((struct TaskEntry) {
3123 .key = (struct TaskKey) { PHASE_KIND_ALL_TO_ALL, p1, p2, -1, -1 },
3124 .step = step,
3125 .start = task_start_reconcile,
3126 .cancel = task_cancel_reconcile,
3127 });
3128 task.cls.setop.input_set = (struct SetKey) { SET_KIND_CURRENT, 0 };
3129 task.cls.setop.output_set = task.cls.setop.input_set;
3131 put_task (session->taskmap, &task);
3132 }
3133
3134 round += 1;
3135 prev_step = step;
3136 step = create_step (session, round, GNUNET_NO);;
3137#ifdef GNUNET_EXTRA_LOGGING
3138 step->debug_name = GNUNET_strdup ("all to all 2");
3139#endif
3140 step_depend_on (step, prev_step);
3141
3142
3143 for (unsigned int i = 0; i < n; i++)
3144 {
3145 uint16_t p1;
3146 uint16_t p2;
3147
3148 p1 = me;
3149 p2 = i;
3150 arrange_peers (&p1, &p2, n);
3151 task = ((struct TaskEntry) {
3152 .key = (struct TaskKey) { PHASE_KIND_ALL_TO_ALL_2, p1, p2, -1, -1 },
3153 .step = step,
3154 .start = task_start_reconcile,
3155 .cancel = task_cancel_reconcile,
3156 });
3157 task.cls.setop.input_set = (struct SetKey) { SET_KIND_CURRENT, 0 };
3158 task.cls.setop.output_set = task.cls.setop.input_set;
3160 put_task (session->taskmap, &task);
3161 }
3162
3163 round += 1;
3164
3165 prev_step = step;
3166 step = NULL;
3167
3168
3169 /* Byzantine union */
3170
3171 /* sequential repetitions of the gradecasts */
3172 for (unsigned int i = 0; i < t + 1; i++)
3173 {
3174 struct Step *step_rep_start;
3175 struct Step *step_rep_end;
3176
3177 /* Every repetition is in a separate round. */
3178 step_rep_start = create_step (session, round, GNUNET_YES);
3179#ifdef GNUNET_EXTRA_LOGGING
3180 GNUNET_asprintf (&step_rep_start->debug_name, "gradecast start rep %u", i);
3181#endif
3182
3183 step_depend_on (step_rep_start, prev_step);
3184
3185 /* gradecast has three rounds */
3186 round += 3;
3187 step_rep_end = create_step (session, round, GNUNET_YES);
3188#ifdef GNUNET_EXTRA_LOGGING
3189 GNUNET_asprintf (&step_rep_end->debug_name, "gradecast end rep %u", i);
3190#endif
3191
3192 /* parallel gradecasts */
3193 for (lead = 0; lead < n; lead++)
3194 construct_task_graph_gradecast (session, i, lead, step_rep_start,
3195 step_rep_end);
3196
3197 task = ((struct TaskEntry) {
3198 .step = step_rep_end,
3199 .key = (struct TaskKey) { PHASE_KIND_APPLY_REP, -1, -1, i, -1 },
3200 .start = task_start_apply_round,
3201 });
3202 put_task (session->taskmap, &task);
3203
3204 prev_step = step_rep_end;
3205 }
3206
3207 /* There is no next gradecast round, thus the final
3208 start step is the overall end step of the gradecasts */
3209 round += 1;
3210 step = create_step (session, round, GNUNET_NO);
3211#ifdef GNUNET_EXTRA_LOGGING
3212 GNUNET_asprintf (&step->debug_name, "finish");
3213#endif
3214 step_depend_on (step, prev_step);
3215
3216 task = ((struct TaskEntry) {
3217 .step = step,
3218 .key = (struct TaskKey) { PHASE_KIND_FINISH, -1, -1, -1, -1 },
3219 .start = task_start_finish,
3220 });
3222
3223 put_task (session->taskmap, &task);
3224}
3225
3226
3234static int
3236 const struct GNUNET_CONSENSUS_JoinMessage *m)
3237{
3238 uint32_t listed_peers = ntohl (m->num_peers);
3239
3240 if ((ntohs (m->header.size) - sizeof(*m)) !=
3241 listed_peers * sizeof(struct GNUNET_PeerIdentity))
3242 {
3243 GNUNET_break (0);
3244 return GNUNET_SYSERR;
3245 }
3246 return GNUNET_OK;
3247}
3248
3249
3256static void
3258 const struct GNUNET_CONSENSUS_JoinMessage *m)
3259{
3260 struct ConsensusSession *session = cls;
3261 struct ConsensusSession *other_session;
3262
3264 m);
3265 compute_global_id (session,
3266 &m->session_id);
3267
3268 /* Check if some local client already owns the session.
3269 It is only legal to have a session with an existing global id
3270 if all other sessions with this global id are finished.*/
3271 for (other_session = sessions_head;
3272 NULL != other_session;
3273 other_session = other_session->next)
3274 {
3275 if ( (other_session != session) &&
3276 (0 == GNUNET_CRYPTO_hash_cmp (&session->global_id,
3277 &other_session->global_id)) )
3278 break;
3279 }
3280
3281 session->conclude_deadline
3282 = GNUNET_TIME_absolute_ntoh (m->deadline);
3283 session->conclude_start
3284 = GNUNET_TIME_absolute_ntoh (m->start);
3285 session->local_peer_idx = get_peer_idx (&my_peer,
3286 session);
3287 GNUNET_assert (-1 != session->local_peer_idx);
3288
3290 "Joining consensus session %s containing %u peers as %u with timeout %s\n",
3291 GNUNET_h2s (&m->session_id),
3292 session->num_peers,
3293 session->local_peer_idx,
3296 session->conclude_deadline)
3297 ,
3298 GNUNET_YES));
3299
3300 session->set_listener
3303 &session->global_id,
3305 session);
3306
3308 GNUNET_NO);
3310 GNUNET_NO);
3312 GNUNET_NO);
3314 GNUNET_NO);
3315
3316 {
3317 struct SetEntry *client_set = GNUNET_new (struct SetEntry);
3318 struct SetHandle *sh = GNUNET_new (struct SetHandle);
3319
3320 client_set->h = GNUNET_SET_create (cfg,
3322 sh->h = client_set->h;
3324 session->set_handles_tail,
3325 sh);
3326 client_set->key = ((struct SetKey) { SET_KIND_CURRENT, 0, 0 });
3327 put_set (session,
3328 client_set);
3329 }
3330
3331 session->peers_blacklisted = GNUNET_new_array (session->num_peers,
3332 int);
3333
3334 /* Just construct the task graph,
3335 but don't run anything until the client calls conclude. */
3336 construct_task_graph (session);
3338}
3339
3340
3348static int
3351{
3352 return GNUNET_OK;
3353}
3354
3355
3362static void
3365{
3366 struct ConsensusSession *session = cls;
3367 ssize_t element_size;
3368 struct GNUNET_SET_Handle *initial_set;
3369 struct ConsensusElement *ce;
3370
3371 if (GNUNET_YES == session->conclude_started)
3372 {
3373 GNUNET_break (0);
3375 return;
3376 }
3377 element_size = ntohs (msg->header.size) - sizeof(*msg);
3378 ce = GNUNET_malloc (sizeof(struct ConsensusElement) + element_size);
3379 GNUNET_memcpy (&ce[1],
3380 &msg[1],
3381 element_size);
3382 ce->payload_type = msg->element_type;
3383
3384 {
3385 struct SetKey key = { SET_KIND_CURRENT, 0, 0 };
3386 struct SetEntry *entry;
3387
3388 entry = lookup_set (session,
3389 &key);
3390 GNUNET_assert (NULL != entry);
3391 initial_set = entry->h;
3392 }
3393
3394 session->num_client_insert_pending++;
3395
3396 {
3397 struct GNUNET_SET_Element element = {
3399 .size = sizeof(struct ConsensusElement) + element_size,
3400 .data = ce,
3401 };
3402
3403 GNUNET_SET_add_element (initial_set,
3404 &element,
3405 NULL,
3406 NULL);
3407#ifdef GNUNET_EXTRA_LOGGING
3409 "P%u: element %s added\n",
3410 session->local_peer_idx,
3411 debug_str_element (&element));
3412#endif
3413 }
3414 GNUNET_free (ce);
3416}
3417
3418
3425static void
3427 const struct GNUNET_MessageHeader *message)
3428{
3429 struct ConsensusSession *session = cls;
3430
3431 if (GNUNET_YES == session->conclude_started)
3432 {
3433 /* conclude started twice */
3434 GNUNET_break (0);
3436 return;
3437 }
3439 "conclude requested\n");
3440 session->conclude_started = GNUNET_YES;
3441 install_step_timeouts (session);
3442 run_ready_steps (session);
3444}
3445
3446
3452static void
3453shutdown_task (void *cls)
3454{
3456 "shutting down\n");
3458 GNUNET_NO);
3459 statistics = NULL;
3460}
3461
3462
3470static void
3471run (void *cls,
3472 const struct GNUNET_CONFIGURATION_Handle *c,
3474{
3475 cfg = c;
3476 if (GNUNET_OK !=
3478 &my_peer))
3479 {
3481 "Could not retrieve host identity\n");
3483 return;
3484 }
3485 statistics = GNUNET_STATISTICS_create ("consensus",
3486 cfg);
3488 NULL);
3489}
3490
3491
3500static void *
3502 struct GNUNET_SERVICE_Client *c,
3503 struct GNUNET_MQ_Handle *mq)
3504{
3505 struct ConsensusSession *session = GNUNET_new (struct ConsensusSession);
3506
3507 session->client = c;
3508 session->client_mq = mq;
3511 session);
3512 return session;
3513}
3514
3515
3523static void
3525 struct GNUNET_SERVICE_Client *c,
3526 void *internal_cls)
3527{
3528 struct ConsensusSession *session = internal_cls;
3529
3530 if (NULL != session->set_listener)
3531 {
3533 session->set_listener = NULL;
3534 }
3537 session);
3538 while (session->set_handles_head)
3539 {
3540 struct SetHandle *sh = session->set_handles_head;
3541
3542 session->set_handles_head = sh->next;
3544 GNUNET_free (sh);
3545 }
3546 GNUNET_free (session);
3547}
3548
3549
3555 "consensus",
3557 &run,
3560 NULL,
3561 GNUNET_MQ_hd_fixed_size (client_conclude,
3563 struct GNUNET_MessageHeader,
3564 NULL),
3565 GNUNET_MQ_hd_var_size (client_insert,
3568 NULL),
3569 GNUNET_MQ_hd_var_size (client_join,
3572 NULL),
3574
3575/* end of gnunet-service-consensus.c */
struct GNUNET_MessageHeader * msg
Definition 005.c:2
p2p message definitions for consensus
@ CONSENSUS_MARKER_CONTESTED
@ CONSENSUS_MARKER_SIZE
static mp_limb_t d[(((256)+GMP_NUMB_BITS - 1)/GMP_NUMB_BITS)]
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition gnunet-arm.c:103
static int ret
Final status code.
Definition gnunet-arm.c:93
static unsigned int phase
Processing stage that we are in.
Definition gnunet-arm.c:113
static GNUNET_NETWORK_STRUCT_END struct GNUNET_PeerIdentity me
Our own peer identity.
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_IDENTITY_Handle * sh
Handle to IDENTITY service.
struct GNUNET_SCHEDULER_Task * shutdown_task
static struct GNUNET_SCHEDULER_Task * t
Main task.
static uint32_t type
Type string converted to DNS type value.
static int status
The program status; 0 for success.
Definition gnunet-nse.c:39
static struct GNUNET_FS_PublishContext * pc
Handle to FS-publishing operation.
static struct GNUNET_IDENTITY_EgoLookup * el
Handle for our ego lookup.
static struct GNUNET_CRYPTO_PowSalt salt
Salt for PoW calculations.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
static void rfn_contest(struct ReferendumEntry *rfn, uint16_t contested_peer)
static void rfn_vote(struct ReferendumEntry *rfn, uint16_t voting_peer, enum ReferendumVote vote, const struct GNUNET_SET_Element *element)
static void handle_client_join(void *cls, const struct GNUNET_CONSENSUS_JoinMessage *m)
Called when a client wants to join a consensus session.
static int get_peer_idx(const struct GNUNET_PeerIdentity *peer, const struct ConsensusSession *session)
Search peer in the list of peers in session.
static void task_start_grade(struct TaskEntry *task)
static struct ConsensusSession * sessions_head
Linked list of sessions this peer participates in.
static uint16_t task_other_peer(struct TaskEntry *task)
static void handle_client_insert(void *cls, const struct GNUNET_CONSENSUS_ElementMessage *msg)
Called when a client performs an insert operation.
static struct SetEntry * lookup_set(struct ConsensusSession *session, const struct SetKey *key)
static void apply_diff_to_rfn(struct DiffEntry *diff, struct ReferendumEntry *rfn, uint16_t voting_peer, uint16_t num_peers)
static struct Step * create_step(struct ConsensusSession *session, int round, int early_finishable)
static void arrange_peers(uint16_t *p1, uint16_t *p2, uint16_t n)
static void start_task(struct ConsensusSession *session, struct TaskEntry *task)
static void initialize_session_peer_list(struct ConsensusSession *session, const struct GNUNET_CONSENSUS_JoinMessage *join_msg)
Create the sorted list of peers for the session, add the local peer if not in the join message.
@ DIFF_KIND_LEADER_PROPOSAL
@ DIFF_KIND_LEADER_CONSENSUS
@ DIFF_KIND_GRADECAST_RESULT
static void compute_global_id(struct ConsensusSession *session, const struct GNUNET_HashCode *local_session_id)
Compute a global, (hopefully) unique consensus session id, from the local id of the consensus session...
static void set_listen_cb(void *cls, const struct GNUNET_PeerIdentity *other_peer, const struct GNUNET_MessageHeader *context_msg, struct GNUNET_SET_Request *request)
Called when another peer wants to do a set operation with the local peer.
@ PHASE_KIND_GRADECAST_CONFIRM
@ PHASE_KIND_GRADECAST_ECHO
@ PHASE_KIND_ALL_TO_ALL_2
@ PHASE_KIND_APPLY_REP
Apply a repetition of the all-to-all gradecast to the current set.
@ PHASE_KIND_GRADECAST_LEADER
@ PHASE_KIND_GRADECAST_ECHO_GRADE
@ PHASE_KIND_GRADECAST_CONFIRM_GRADE
@ PHASE_KIND_ALL_TO_ALL
static struct DiffEntry * lookup_diff(struct ConsensusSession *session, const struct DiffKey *key)
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration of the consensus service.
static void finish_step(struct Step *step)
static int peer_id_cmp(const void *h1, const void *h2)
Compare two peer identities (for qsort()).
static void put_rfn(struct ConsensusSession *session, struct ReferendumEntry *rfn)
static void put_diff(struct ConsensusSession *session, struct DiffEntry *diff)
static void try_finish_step_early(struct Step *step)
static void put_set(struct ConsensusSession *session, struct SetEntry *set)
struct GNUNET_STATISTICS_Handle * statistics
Statistics handle.
static void task_cancel_reconcile(struct TaskEntry *task)
static const char * setname(uint16_t kind)
static void construct_task_graph(struct ConsensusSession *session)
@ VOTE_ADD
Vote that an element should be added.
@ VOTE_REMOVE
Vote that an element should be removed.
@ VOTE_STAY
Vote that nothing should change.
static void diff_insert(struct DiffEntry *diff, int weight, const struct GNUNET_SET_Element *element)
static void put_task(struct GNUNET_CONTAINER_MultiHashMap *taskmap, struct TaskEntry *t)
static void finish_task(struct TaskEntry *task)
static void construct_task_graph_gradecast(struct ConsensusSession *session, uint16_t rep, uint16_t lead, struct Step *step_before, struct Step *step_after)
Construct the task graph for a single gradecast.
static int check_client_join(void *cls, const struct GNUNET_CONSENSUS_JoinMessage *m)
Check join message.
@ RFN_KIND_GRADECAST_RESULT
static void task_start_finish(struct TaskEntry *task)
static void rfn_majority(const struct ReferendumEntry *rfn, const struct RfnElementInfo *ri, uint16_t *ret_majority, enum ReferendumVote *ret_vote)
For a given majority, count what the outcome is (add/remove/keep), and give the number of peers that ...
static void run_ready_steps(struct ConsensusSession *session)
static const char * phasename(uint16_t phase)
static void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service)
Start processing consensus requests.
static struct ConsensusSession * sessions_tail
Linked list of sessions this peer participates in.
static void task_start_reconcile(struct TaskEntry *task)
static void step_depend_on(struct Step *step, struct Step *dep)
Record dep as a dependency of step.
static void install_step_timeouts(struct ConsensusSession *session)
static struct ReferendumEntry * lookup_rfn(struct ConsensusSession *session, const struct RfnKey *key)
static struct TaskEntry * lookup_task(const struct ConsensusSession *session, const struct TaskKey *key)
static void set_copy_cb(void *cls, struct GNUNET_SET_Handle *copy)
static void create_set_copy_for_task(struct TaskEntry *task, struct SetKey *src_set_key, struct SetKey *dst_set_key)
Call the start function of the given task again after we created a copy of the given set.
static void * client_connect_cb(void *cls, struct GNUNET_SERVICE_Client *c, struct GNUNET_MQ_Handle *mq)
Callback called when a client connects to the service.
static void client_disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
Callback called when a client disconnected from the service.
static uint16_t rfn_noncontested(struct ReferendumEntry *rfn)
static void set_result_cb(void *cls, const struct GNUNET_SET_Element *element, uint64_t current_size, enum GNUNET_SET_Status status)
Callback for set operation results.
@ SET_KIND_ECHO_RESULT
@ SET_KIND_LEADER_PROPOSAL
@ SET_KIND_LAST_GRADECAST
Last result set from a gradecast.
static int cmp_uint64_t(const void *pa, const void *pb)
static const char * rfnname(uint16_t kind)
static void commit_set(struct ConsensusSession *session, struct TaskEntry *task)
Commit the appropriate set for a task.
static struct ReferendumEntry * rfn_create(uint16_t size)
static const char * diffname(uint16_t kind)
static struct DiffEntry * diff_create(void)
static void rfn_commit(struct ReferendumEntry *rfn, uint16_t commit_peer)
static void task_start_apply_round(struct TaskEntry *task)
Apply the result from one round of gradecasts (i.e.
static int check_client_insert(void *cls, const struct GNUNET_CONSENSUS_ElementMessage *msg)
Called when a client performs an insert operation.
@ EARLY_STOPPING_DONE
@ EARLY_STOPPING_NONE
@ EARLY_STOPPING_ONE_MORE
static int send_to_client_iter(void *cls, const struct GNUNET_SET_Element *element)
Send the final result set of the consensus to the client, element by element.
static struct GNUNET_PeerIdentity my_peer
Peer that runs this service.
void(* TaskFunc)(struct TaskEntry *task)
static void set_mutation_done(void *cls)
static void task_start_eval_echo(struct TaskEntry *task)
static void handle_client_conclude(void *cls, const struct GNUNET_MessageHeader *message)
Called when a client performs the conclude operation.
static void cleanup()
Cleanup task.
static struct GNUNET_VPN_RedirectionRequest * request
Opaque redirection request handle.
Definition gnunet-vpn.c:40
@ GNUNET_BLOCK_TYPE_CONSENSUS_ELEMENT
Block type for consensus elements.
static unsigned int num_peers
Number of peers.
Constants for network protocols.
Two-peer set operations.
API to create, modify and access statistics.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_PeerIdentity *dst)
Retrieve the identity of the host's peer.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition crypto_hash.c:40
#define GNUNET_CRYPTO_hkdf_gnunet(result, out_len, xts, xts_len, skm, skm_len,...)
A peculiar HKDF instantiation that tried to mimic Truncated NMAC.
int GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
Compare function for HashCodes, producing a total ordering of all hashcodes.
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_iterator_next(struct GNUNET_CONTAINER_MultiHashMapIterator *iter, struct GNUNET_HashCode *key, const void **value)
Retrieve the next element from the hash map at the iterator's position.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
void GNUNET_CONTAINER_multihashmap_iterator_destroy(struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
Destroy a multihashmap iterator.
struct GNUNET_CONTAINER_MultiHashMapIterator * GNUNET_CONTAINER_multihashmap_iterator_create(const struct GNUNET_CONTAINER_MultiHashMap *map)
Create an iterator for a multihashmap.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE
If a value with the given key exists, replace it.
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_log(kind,...)
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
#define GNUNET_CRYPTO_kdf_arg_auto(d)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MIN(a, b)
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#define GNUNET_PACKED
gcc-ism to get packed structs.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition mq.c:305
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
#define GNUNET_MQ_msg_header(type)
Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
#define GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN
Join a consensus session.
#define GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE
Sent by service to client in order to signal a completed consensus conclusion.
#define GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT
Sent by service when a new element is added.
#define GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT
Insert an element.
#define GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE
Sent by client to service in order to start the consensus conclusion.
#define GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ROUND_CONTEXT
Provide context for a consensus round.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition scheduler.c:572
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition scheduler.c:1345
#define GNUNET_SERVICE_MAIN(pd, service_name, service_options, init_cb, connect_cb, disconnect_cb, cls,...)
Creates the "main" function for a GNUnet service.
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition service.c:2463
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition service.c:2434
@ GNUNET_SERVICE_OPTION_NONE
Use defaults.
GNUNET_SET_Status
Status for the result callback.
void GNUNET_SET_destroy(struct GNUNET_SET_Handle *set)
Destroy the set handle if no operations are left, mark the set for destruction otherwise.
Definition set_api.c:745
void GNUNET_SET_copy_lazy(struct GNUNET_SET_Handle *set, GNUNET_SET_CopyReadyCallback cb, void *cls)
Definition set_api.c:1139
struct GNUNET_SET_Element * GNUNET_SET_element_dup(const struct GNUNET_SET_Element *element)
Create a copy of an element.
Definition set_api.c:1168
void GNUNET_SET_listen_cancel(struct GNUNET_SET_ListenHandle *lh)
Cancel the given listen operation.
Definition set_api.c:1010
int GNUNET_SET_remove_element(struct GNUNET_SET_Handle *set, const struct GNUNET_SET_Element *element, GNUNET_SET_Continuation cont, void *cont_cls)
Remove an element to the given set.
Definition set_api.c:707
int GNUNET_SET_iterate(struct GNUNET_SET_Handle *set, GNUNET_SET_ElementIterator iter, void *iter_cls)
Iterate over all elements in the given set.
Definition set_api.c:1117
void GNUNET_SET_operation_cancel(struct GNUNET_SET_OperationHandle *oh)
Cancel the given set operation.
Definition set_api.c:516
struct GNUNET_SET_Handle * GNUNET_SET_create(const struct GNUNET_CONFIGURATION_Handle *cfg, enum GNUNET_SET_OperationType op)
Create an empty set, supporting the specified operation.
Definition set_api.c:656
int GNUNET_SET_commit(struct GNUNET_SET_OperationHandle *oh, struct GNUNET_SET_Handle *set)
Commit a set to be used with a set operation.
Definition set_api.c:1073
struct GNUNET_SET_ListenHandle * GNUNET_SET_listen(const struct GNUNET_CONFIGURATION_Handle *cfg, enum GNUNET_SET_OperationType operation, const struct GNUNET_HashCode *app_id, GNUNET_SET_ListenCallback listen_cb, void *listen_cls)
Wait for set operation requests for the given application id.
Definition set_api.c:976
struct GNUNET_SET_OperationHandle * GNUNET_SET_prepare(const struct GNUNET_PeerIdentity *other_peer, const struct GNUNET_HashCode *app_id, const struct GNUNET_MessageHeader *context_msg, enum GNUNET_SET_ResultMode result_mode, struct GNUNET_SET_Option options[], GNUNET_SET_ResultIterator result_cb, void *result_cls)
Prepare a set operation to be evaluated with another peer.
Definition set_api.c:772
struct GNUNET_SET_OperationHandle * GNUNET_SET_accept(struct GNUNET_SET_Request *request, enum GNUNET_SET_ResultMode result_mode, struct GNUNET_SET_Option options[], GNUNET_SET_ResultIterator result_cb, void *result_cls)
Accept a request we got via GNUNET_SET_listen().
Definition set_api.c:1030
int GNUNET_SET_add_element(struct GNUNET_SET_Handle *set, const struct GNUNET_SET_Element *element, GNUNET_SET_Continuation cont, void *cont_cls)
Add an element to the given set.
Definition set_api.c:673
void GNUNET_SET_element_hash(const struct GNUNET_SET_Element *element, struct GNUNET_HashCode *ret_hash)
Hash a set element.
Definition set_api.c:1184
@ GNUNET_SET_STATUS_FAILURE
The other peer refused to to the operation with us, or something went wrong.
@ GNUNET_SET_STATUS_ADD_REMOTE
Element should be added to the result set of the remote peer, i.e.
@ GNUNET_SET_STATUS_DONE
Success, all elements have been sent (and received).
@ GNUNET_SET_STATUS_ADD_LOCAL
Element should be added to the result set of the local peer, i.e.
@ GNUNET_SET_OPTION_END
List terminator.
@ GNUNET_SET_OPTION_BYZANTINE
Fail set operations when the other peer shows weird behavior that might by a Byzantine fault.
@ GNUNET_SET_RESULT_SYMMETRIC
Client gets notified of the required changes for both the local and the remote set.
@ GNUNET_SET_OPERATION_UNION
Set union, return all elements that are in at least one of the sets.
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
void GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle, const char *name, uint64_t value, int make_persistent)
Set statistic value for the peer.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition strings.c:610
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition time.c:737
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Absolute end)
Compute the time difference between the given start and end times.
Definition time.c:423
static unsigned int size
Size of the "table".
Definition peer.c:68
static struct GNUNET_MQ_Handle * mq
Our connection to the resolver service, created on-demand, but then persists until error or shutdown.
Consensus element, either marker or payload.
uint16_t payload_type
Payload element_type, only valid if this is not a marker element.
uint8_t marker
Is this a marker element?
A consensus session consists of one local client and the remote authorities.
struct GNUNET_HashCode global_id
Global consensus identification, computed from the session id and participating authorities.
struct GNUNET_TIME_Absolute conclude_start
Time when the conclusion of the consensus should begin.
struct GNUNET_CONTAINER_MultiHashMap * diffmap
struct SetHandle * set_handles_tail
struct GNUNET_CONTAINER_MultiHashMap * rfnmap
unsigned int local_peer_idx
Index of the local peer in the peers array.
unsigned int num_peers
Number of other peers in the consensus.
struct ConsensusSession * prev
Consensus sessions are kept in a DLL.
struct GNUNET_PeerIdentity * peers
struct GNUNET_TIME_Absolute conclude_deadline
Timeout for all rounds together, single rounds will schedule a timeout task with a fraction of the co...
struct GNUNET_MQ_Handle * client_mq
Queued messages to the client.
uint64_t first_size
Our set size from the first round.
struct GNUNET_SERVICE_Client * client
Client that inhabits the session.
struct GNUNET_SET_ListenHandle * set_listener
Listener for requests from other peers.
struct ConsensusSession * next
Consensus sessions are kept in a DLL.
int * peers_blacklisted
Array of peers with length 'num_peers'.
unsigned int num_client_insert_pending
uint64_t lower_bound
Bounded Eppstein lower bound.
struct SetHandle * set_handles_head
int early_stopping
State of our early stopping scheme.
struct GNUNET_CONTAINER_MultiHashMap * setmap
struct GNUNET_CONTAINER_MultiHashMap * taskmap
struct ConsensusElement ce
struct GNUNET_HashCode rand
struct ConsensusElement ce
int weight
Positive weight for 'add', negative weights for 'remove'.
const struct GNUNET_SET_Element * element
struct GNUNET_CONTAINER_MultiHashMap * changes
enum DiffKind diff_kind
struct SetKey input_set
Message with an element.
Definition consensus.h:73
Sent by the client to the service, when the client wants the service to join a consensus session.
Definition consensus.h:38
uint32_t num_peers
Number of peers (at the end of this message) that want to participate in the consensus.
Definition consensus.h:48
Sent as context message for set reconciliation.
int16_t repetition
Repetition of the gradecast phase.
int16_t peer1
Number of the first peer in canonical order.
int16_t leader
Leader in the gradecast phase.
int16_t peer2
Number of the second peer in canonical order.
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ROUND_CONTEXT.
uint16_t kind
A value from 'enum PhaseKind'.
uint16_t is_contested
Non-zero if this set reconciliation had elements removed because they were contested.
Internal representation of the hash map.
A 512-bit hashcode.
Handle to a message queue.
Definition mq.c:87
Header for all communications.
The identity of the host (wraps the signing key of the peer).
Entry in list of pending tasks.
Definition scheduler.c:141
Handle to a client that is connected to a service.
Definition service.c:249
Handle to a service.
Definition service.c:116
Element stored in a set.
uint16_t size
Number of bytes in the buffer pointed to by data.
const void * data
Actual data of the element.
uint16_t element_type
Application-specific element type.
Opaque handle to a set.
Definition set_api.c:50
Opaque handle to a listen operation.
Definition set_api.c:187
Handle to an operation.
Definition set_api.c:136
Option for set operations.
Handle for a set operation request from another peer.
Definition set_api.c:116
Handle for the service.
Time for absolute times used by GNUnet, in microseconds.
int * peer_commited
Stores, for every peer in the session, whether the peer finished the whole referendum.
struct GNUNET_CONTAINER_MultiHashMap * rfn_elements
int * peer_contested
Contestation state of the peer.
enum ReferendumVote proposal
Proposal for this element, can only be VOTE_ADD or VOTE_REMOVE.
int * votes
GNUNET_YES if the peer votes for the proposal.
const struct GNUNET_SET_Element * element
enum RfnKind rfn_kind
struct SetKey dst_set_key
struct TaskEntry * task
int is_contested
GNUNET_YES if the set resulted from applying a referendum with contested elements.
struct GNUNET_SET_Handle * h
struct SetHandle * prev
struct SetHandle * next
struct GNUNET_SET_Handle * h
int k1
Repetition counter.
int k2
Leader (or 0).
enum SetKind set_kind
struct TaskEntry * task
Task to finish once all changes are through.
struct SetKey output_set
struct DiffKey output_diff
struct RfnKey output_rfn
struct GNUNET_SET_OperationHandle * op
struct SetKey input_set
unsigned int subordinates_cap
unsigned int round
Synchrony round of the task.
int early_finishable
When we're doing an early finish, how should this step be treated? If GNUNET_YES, the step will be ma...
unsigned int tasks_len
unsigned int is_finished
struct GNUNET_SCHEDULER_Task * timeout_task
Task that will run this step despite any pending prerequisites.
size_t pending_prereq
Counter for the prerequisites of this step.
char * debug_name
Human-readable name for the task, used for debugging.
struct Step * next
All steps of one session are in a linked list for easier deallocation.
struct ConsensusSession * session
struct TaskEntry ** tasks
Tasks that this step is composed of.
unsigned int is_running
struct Step ** subordinates
unsigned int tasks_cap
unsigned int finished_tasks
struct Step * prev
All steps of one session are in a linked list for easier deallocation.
unsigned int subordinates_len
union TaskFuncCls cls
Tuple of integers that together identify a task uniquely.
uint16_t kind
A value from 'enum PhaseKind'.
int16_t peer1
Number of the first peer in canonical order.
int16_t peer2
Number of the second peer in canonical order.
int16_t repetition
Repetition of the gradecast phase.
int16_t leader
Leader in the gradecast phase.
enum GNUNET_TIME_RounderInterval ri
Definition time.c:1251
Closure for both start_task and cancel_task.
struct FinishCls finish
struct SetOpCls setop