2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file rps/test_rps.c
22 * @brief Testcase for the random peer sampling service. Starts
23 * a peergroup with a given number of peers, then waits to
24 * receive size pushes/pulls from each peer. Expects to wait
25 * for one message from each peer.
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
31 #include "gnunet_rps_service.h"
32 #include "rps-test_util.h"
33 #include "gnunet-service-rps_sampler_elem.h"
39 * How many peers do we start?
41 static uint32_t num_peers;
44 * How long do we run the test?
47 static uint32_t timeout_s;
50 * How long do we run the test?
52 // #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
53 static struct GNUNET_TIME_Relative timeout;
57 * Portion of malicious peers
59 static double portion = .1;
62 * Type of malicious peer to test
64 static unsigned int mal_type = 0;
67 * Handles to all of the running peers
69 static struct GNUNET_TESTBED_Peer **testbed_peers;
72 * @brief Indicates whether peer should go off- or online
74 enum PEER_ONLINE_DELTA
77 * @brief Indicates peer going online
81 * @brief Indicates peer going offline
94 struct OpListEntry *next;
99 struct OpListEntry *prev;
102 * The testbed operation
104 struct GNUNET_TESTBED_Operation *op;
107 * Depending on whether we start or stop RPS service at the peer, set this to
108 * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
110 enum PEER_ONLINE_DELTA delta;
113 * Index of the regarding peer
121 static struct OpListEntry *oplist_head;
126 static struct OpListEntry *oplist_tail;
130 * A pending reply: A request was sent and the reply is pending.
137 struct PendingReply *next;
138 struct PendingReply *prev;
141 * Handle to the request we are waiting for
143 struct GNUNET_RPS_Request_Handle *req_handle;
146 * The peer that requested
148 struct RPSPeer *rps_peer;
153 * A pending request: A request was not made yet but is scheduled for later.
155 struct PendingRequest
160 struct PendingRequest *next;
161 struct PendingRequest *prev;
164 * Handle to the request we are waiting for
166 struct GNUNET_SCHEDULER_Task *request_task;
169 * The peer that requested
171 struct RPSPeer *rps_peer;
176 * Information we track for each peer.
186 * Handle for RPS connect operation.
188 struct GNUNET_TESTBED_Operation *op;
191 * Handle to RPS service.
193 struct GNUNET_RPS_Handle *rps_handle;
196 * Handle to stream requests
198 struct GNUNET_RPS_StreamRequestHandle *rps_srh;
203 struct GNUNET_PeerIdentity *peer_id;
206 * A request handle to check for an request
208 // struct GNUNET_RPS_Request_Handle *req_handle;
211 * Peer on- or offline?
216 * Number of Peer IDs to request during the whole test
218 unsigned int num_ids_to_request;
221 * Pending requests DLL
223 struct PendingRequest *pending_req_head;
224 struct PendingRequest *pending_req_tail;
227 * Number of pending requests
229 unsigned int num_pending_reqs;
232 * Pending replies DLL
234 struct PendingReply *pending_rep_head;
235 struct PendingReply *pending_rep_tail;
238 * Number of pending replies
240 unsigned int num_pending_reps;
243 * Number of received PeerIDs
245 unsigned int num_recv_ids;
248 * Pending operation on that peer
250 const struct OpListEntry *entry_op_manage;
253 * Testbed operation to connect to statistics service
255 struct GNUNET_TESTBED_Operation *stat_op;
258 * Handle to the statistics service
260 struct GNUNET_STATISTICS_Handle *stats_h;
263 * @brief flags to indicate which statistics values have been already
264 * collected from the statistics service.
265 * Used to check whether we are able to shutdown.
267 uint32_t stat_collected_flags;
270 * @brief File name of the file the stats are finally written to
272 const char *file_name_stats;
275 * @brief File name of the file the stats are finally written to
277 const char *file_name_probs;
280 * @brief The current view
282 struct GNUNET_PeerIdentity *cur_view;
285 * @brief Number of peers in the #cur_view.
287 uint32_t cur_view_count;
290 * @brief Number of occurrences in other peer's view
292 uint32_t count_in_views;
295 * @brief statistics values
299 uint64_t num_blocks_many_push;
300 uint64_t num_blocks_no_push;
301 uint64_t num_blocks_no_pull;
302 uint64_t num_blocks_many_push_no_pull;
303 uint64_t num_blocks_no_push_no_pull;
304 uint64_t num_issued_push;
305 uint64_t num_issued_pull_req;
306 uint64_t num_issued_pull_rep;
307 uint64_t num_sent_push;
308 uint64_t num_sent_pull_req;
309 uint64_t num_sent_pull_rep;
310 uint64_t num_recv_push;
311 uint64_t num_recv_pull_req;
312 uint64_t num_recv_pull_rep;
317 STAT_TYPE_ROUNDS = 0x1, /* 1 */
318 STAT_TYPE_BLOCKS = 0x2, /* 2 */
319 STAT_TYPE_BLOCKS_MANY_PUSH = 0x4, /* 3 */
320 STAT_TYPE_BLOCKS_NO_PUSH = 0x8, /* 4 */
321 STAT_TYPE_BLOCKS_NO_PULL = 0x10, /* 5 */
322 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL = 0x20, /* 6 */
323 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL = 0x40, /* 7 */
324 STAT_TYPE_ISSUED_PUSH_SEND = 0x80, /* 8 */
325 STAT_TYPE_ISSUED_PULL_REQ = 0x100, /* 9 */
326 STAT_TYPE_ISSUED_PULL_REP = 0x200, /* 10 */
327 STAT_TYPE_SENT_PUSH_SEND = 0x400, /* 11 */
328 STAT_TYPE_SENT_PULL_REQ = 0x800, /* 12 */
329 STAT_TYPE_SENT_PULL_REP = 0x1000, /* 13 */
330 STAT_TYPE_RECV_PUSH_SEND = 0x2000, /* 14 */
331 STAT_TYPE_RECV_PULL_REQ = 0x4000, /* 15 */
332 STAT_TYPE_RECV_PULL_REP = 0x8000, /* 16 */
333 STAT_TYPE_MAX = 0x80000000, /* 32 */
338 struct RPSPeer *rps_peer;
339 enum STAT_TYPE stat_type;
344 * Information for all the peers.
346 static struct RPSPeer *rps_peers;
349 * Peermap to get the index of a given peer ID quick.
351 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
356 static struct GNUNET_PeerIdentity *rps_peer_ids;
359 * ID of the targeted peer.
361 static struct GNUNET_PeerIdentity *target_peer;
364 * ID of the peer that requests for the evaluation.
366 static struct RPSPeer *eval_peer;
369 * Number of online peers.
371 static unsigned int num_peers_online;
374 * @brief The added sizes of the peer's views
376 static unsigned int view_sizes;
379 * Return value from 'main'.
384 * Identifier for the churn task that runs periodically
386 static struct GNUNET_SCHEDULER_Task *post_test_task;
389 * Identifier for the churn task that runs periodically
391 static struct GNUNET_SCHEDULER_Task *shutdown_task;
394 * Identifier for the churn task that runs periodically
396 static struct GNUNET_SCHEDULER_Task *churn_task;
399 * Called to initialise the given RPSPeer
401 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
404 * @brief Called directly after connecting to the service
406 * @param rps_peer Specific peer the function is called on
407 * @param h the handle to the rps service
409 typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
412 * @brief Executes functions to test the api/service for a given peer
414 * Called from within #rps_connect_complete_cb ()
415 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
416 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
418 * @param rps_peer the peer the task runs on
420 typedef void (*MainTest) (struct RPSPeer *rps_peer);
423 * Callback called once the requested random peers are available
425 typedef void (*ReplyHandle) (void *cls,
427 const struct GNUNET_PeerIdentity *recv_peers);
430 * Called directly before disconnecting from the service
432 typedef void (*PostTest) (struct RPSPeer *peer);
435 * Function called after disconnect to evaluate test success
437 typedef int (*EvaluationCallback) (void);
440 * @brief Do we have Churn?
445 * @brief If we have churn this is set
449 * @brief If we have no churn this is set
455 * @brief Is it ok to quit the test before the timeout?
457 enum OPTION_QUICK_QUIT
460 * @brief It is ok for the test to quit before the timeout triggers
465 * @brief It is NOT ok for the test to quit before the timeout triggers
471 * @brief Do we collect statistics at the end?
473 enum OPTION_COLLECT_STATISTICS
476 * @brief We collect statistics at the end
481 * @brief We do not collect statistics at the end
483 NO_COLLECT_STATISTICS,
487 * @brief Do we collect views during run?
489 enum OPTION_COLLECT_VIEW
492 * @brief We collect view during run
497 * @brief We do not collect the view during run
503 * Structure to define a single test
513 * Called with a single peer in order to initialise that peer
518 * Called directly after connecting to the service
523 * Main function for each peer
528 * Callback called once the requested peers are available
530 ReplyHandle reply_handle;
533 * Called directly before disconnecting from the service
538 * Function to evaluate the test results
540 EvaluationCallback eval_cb;
545 uint32_t request_interval;
548 * Number of Requests to make.
550 uint32_t num_requests;
553 * Run with (-out) churn
555 enum OPTION_CHURN have_churn;
558 * Quit test before timeout?
560 enum OPTION_QUICK_QUIT have_quick_quit;
563 * Collect statistics at the end?
565 enum OPTION_COLLECT_STATISTICS have_collect_statistics;
568 * Collect view during run?
570 enum OPTION_COLLECT_VIEW have_collect_view;
573 * @brief Mark which values from the statistics service to collect at the end
576 uint32_t stat_collect_flags;
580 * Did we finish the test?
582 static int post_test;
585 * Are we shutting down?
587 static int in_shutdown;
590 * Append arguments to file
593 tofile_ (const char *file_name, const char *line)
595 struct GNUNET_DISK_FileHandle *f;
596 /* char output_buffer[512]; */
601 if (NULL == (f = GNUNET_DISK_file_open (file_name,
602 GNUNET_DISK_OPEN_APPEND
603 | GNUNET_DISK_OPEN_WRITE
604 | GNUNET_DISK_OPEN_CREATE,
605 GNUNET_DISK_PERM_USER_READ
606 | GNUNET_DISK_PERM_USER_WRITE
607 | GNUNET_DISK_PERM_GROUP_READ
608 | GNUNET_DISK_PERM_OTHER_READ)))
610 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
611 "Not able to open file %s\n",
615 /* size = GNUNET_snprintf (output_buffer,
616 sizeof (output_buffer),
618 GNUNET_TIME_absolute_get ().abs_value_us,
622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
623 "Failed to write string to buffer (size: %i)\n",
626 } */size = strlen (line) * sizeof(char);
628 size2 = GNUNET_DISK_file_write (f, line, size);
631 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
632 "Unable to write to file! (Size: %lu, size2: %lu)\n",
635 if (GNUNET_YES != GNUNET_DISK_file_close (f))
637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
638 "Unable to close file\n");
643 if (GNUNET_YES != GNUNET_DISK_file_close (f))
645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
646 "Unable to close file\n");
652 * This function is used to facilitate writing important information to disk
654 #define tofile(file_name, ...) do { \
657 size = GNUNET_snprintf (tmp_buf, sizeof(tmp_buf), __VA_ARGS__); \
659 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, \
660 "Failed to create tmp_buf\n"); \
662 tofile_ (file_name, tmp_buf); \
667 * Write the ids and their according index in the given array to a file
671 ids_to_file (char *file_name,
672 struct GNUNET_PeerIdentity *peer_ids,
673 unsigned int num_peer_ids)
677 for (i=0 ; i < num_peer_ids ; i++)
682 GNUNET_i2s_full (&peer_ids[i]));
687 * Task run on timeout to collect statistics and potentially shut down.
690 post_test_op (void *cls);
694 * Test the success of a single test
704 for (i = 0; i < num_peers; i++)
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
709 GNUNET_i2s (rps_peers[i].peer_id),
710 rps_peers[i].num_recv_ids,
711 rps_peers[i].num_ids_to_request,
712 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
713 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
715 return tmp_ok ? 0 : 1;
720 * Creates an oplist entry and adds it to the oplist DLL
722 static struct OpListEntry *
725 struct OpListEntry *entry;
727 entry = GNUNET_new (struct OpListEntry);
728 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
734 * @brief Checks if given peer already received its statistics value from the
735 * statistics service.
737 * @param rps_peer the peer to check for
739 * @return #GNUNET_YES if so
740 * #GNUNET_NO otherwise
743 check_statistics_collect_completed_single_peer (
744 const struct RPSPeer *rps_peer)
746 if (cur_test_run.stat_collect_flags !=
747 (cur_test_run.stat_collect_flags
748 & rps_peer->stat_collected_flags))
757 * @brief Checks if all peers already received their statistics value from the
758 * statistics service.
760 * @return #GNUNET_YES if so
761 * #GNUNET_NO otherwise
764 check_statistics_collect_completed ()
768 for (i = 0; i < num_peers; i++)
770 if (GNUNET_NO == check_statistics_collect_completed_single_peer (
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 "At least Peer %" PRIu32
775 " did not yet receive all statistics values\n",
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "All peers received their statistics values\n");
787 * Task run on timeout to shut everything down.
790 shutdown_op (void *cls)
796 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
797 "Shutdown task scheduled, going down.\n");
798 in_shutdown = GNUNET_YES;
799 if (NULL != post_test_task)
801 GNUNET_SCHEDULER_cancel (post_test_task);
804 if (NULL != churn_task)
806 GNUNET_SCHEDULER_cancel (churn_task);
809 for (i = 0; i < num_peers; i++)
811 if (NULL != rps_peers[i].rps_handle)
813 GNUNET_RPS_disconnect (rps_peers[i].rps_handle);
815 if (NULL != rps_peers[i].op)
817 GNUNET_TESTBED_operation_done (rps_peers[i].op);
824 * Task run on timeout to collect statistics and potentially shut down.
827 post_test_op (void *cls)
833 post_test_task = NULL;
834 post_test = GNUNET_YES;
835 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
836 "Post test task scheduled, going down.\n");
837 if (NULL != churn_task)
839 GNUNET_SCHEDULER_cancel (churn_task);
842 for (i = 0; i < num_peers; i++)
844 if (NULL != cur_test_run.post_test)
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n",
848 cur_test_run.post_test (&rps_peers[i]);
850 if (NULL != rps_peers[i].op)
852 GNUNET_TESTBED_operation_done (rps_peers[i].op);
853 rps_peers[i].op = NULL;
856 /* If we do not collect statistics, shut down directly */
857 if ((NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics) ||
858 (GNUNET_YES == check_statistics_collect_completed ()) )
860 GNUNET_SCHEDULER_shutdown ();
869 seed_peers (void *cls)
871 struct RPSPeer *peer = cls;
875 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
880 GNUNET_assert (NULL != peer->rps_handle);
882 // TODO if malicious don't seed mal peers
883 amount = round (.5 * num_peers);
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
886 for (i = 0; i < amount; i++)
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
889 GNUNET_i2s (&rps_peer_ids[i]));
891 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
899 seed_peers_big (void *cls)
901 struct RPSPeer *peer = cls;
902 unsigned int seed_msg_size;
903 uint32_t num_peers_max;
907 seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
908 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - seed_msg_size)
909 / sizeof(struct GNUNET_PeerIdentity);
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911 "Peers that fit in one seed msg; %u\n",
913 amount = num_peers_max + (0.5 * num_peers_max);
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915 "Seeding many (%u) peers:\n",
917 struct GNUNET_PeerIdentity ids_to_seed[amount];
918 for (i = 0; i < amount; i++)
920 ids_to_seed[i] = *peer->peer_id;
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
923 GNUNET_i2s (&ids_to_seed[i]));
926 GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
931 * Get the id of peer i.
934 info_cb (void *cb_cls,
935 struct GNUNET_TESTBED_Operation *op,
936 const struct GNUNET_TESTBED_PeerInformation *pinfo,
939 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
943 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
948 if ((NULL == pinfo) || (NULL != emsg))
950 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
951 GNUNET_TESTBED_operation_done (entry->op);
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 GNUNET_i2s (pinfo->result.id));
960 rps_peer_ids[entry->index] = *(pinfo->result.id);
961 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
963 GNUNET_assert (GNUNET_OK ==
964 GNUNET_CONTAINER_multipeermap_put (peer_map,
965 &rps_peer_ids[entry->index],
966 &rps_peers[entry->index],
967 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
968 tofile ("/tmp/rps/peer_ids",
971 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
973 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
974 GNUNET_TESTBED_operation_done (entry->op);
980 * Callback to be called when RPS service connect operation is completed
982 * @param cls the callback closure from functions generating an operation
983 * @param op the operation that has been finished
984 * @param ca_result the RPS service handle returned from rps_connect_adapter
985 * @param emsg error message in case the operation has failed; will be NULL if
986 * operation has executed successfully.
989 rps_connect_complete_cb (void *cls,
990 struct GNUNET_TESTBED_Operation *op,
994 struct RPSPeer *rps_peer = cls;
995 struct GNUNET_RPS_Handle *rps = ca_result;
997 GNUNET_assert (NULL != ca_result);
999 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1004 rps_peer->rps_handle = rps;
1005 rps_peer->online = GNUNET_YES;
1008 GNUNET_assert (op == rps_peer->op);
1011 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1012 "Failed to connect to RPS service: %s\n",
1015 GNUNET_SCHEDULER_shutdown ();
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
1021 cur_test_run.main_test (rps_peer);
1026 * Adapter function called to establish a connection to
1029 * @param cls closure
1030 * @param cfg configuration of the peer to connect to; will be available until
1031 * GNUNET_TESTBED_operation_done() is called on the operation returned
1032 * from GNUNET_TESTBED_service_connect()
1033 * @return service handle to return in 'op_result', NULL on error
1036 rps_connect_adapter (void *cls,
1037 const struct GNUNET_CONFIGURATION_Handle *cfg)
1039 struct GNUNET_RPS_Handle *h;
1041 h = GNUNET_RPS_connect (cfg);
1042 GNUNET_assert (NULL != h);
1044 if (NULL != cur_test_run.pre_test)
1045 cur_test_run.pre_test (cls, h);
1046 GNUNET_assert (NULL != h);
1053 * Called to open a connection to the peer's statistics
1055 * @param cls peer context
1056 * @param cfg configuration of the peer to connect to; will be available until
1057 * GNUNET_TESTBED_operation_done() is called on the operation returned
1058 * from GNUNET_TESTBED_service_connect()
1059 * @return service handle to return in 'op_result', NULL on error
1062 stat_connect_adapter (void *cls,
1063 const struct GNUNET_CONFIGURATION_Handle *cfg)
1065 struct RPSPeer *peer = cls;
1067 peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
1068 return peer->stats_h;
1073 * Called to disconnect from peer's statistics service
1075 * @param cls peer context
1076 * @param op_result service handle returned from the connect adapter
1079 stat_disconnect_adapter (void *cls, void *op_result)
1081 struct RPSPeer *peer = cls;
1083 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1084 // (peer->stats_h, "core", "# peers connected",
1085 // stat_iterator, peer));
1086 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1087 // (peer->stats_h, "nse", "# peers connected",
1088 // stat_iterator, peer));
1089 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
1090 peer->stats_h = NULL;
1095 * Called after successfully opening a connection to a peer's statistics
1096 * service; we register statistics monitoring for CORE and NSE here.
1098 * @param cls the callback closure from functions generating an operation
1099 * @param op the operation that has been finished
1100 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
1101 * @param emsg error message in case the operation has failed; will be NULL if
1102 * operation has executed successfully.
1105 stat_complete_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1106 void *ca_result, const char *emsg)
1108 // struct GNUNET_STATISTICS_Handle *sh = ca_result;
1109 // struct RPSPeer *peer = (struct RPSPeer *) cls;
1119 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1120 // (sh, "core", "# peers connected",
1121 // stat_iterator, peer));
1122 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1123 // (sh, "nse", "# peers connected",
1124 // stat_iterator, peer));
1129 * Adapter function called to destroy connection to
1132 * @param cls closure
1133 * @param op_result service handle returned from the connect adapter
1136 rps_disconnect_adapter (void *cls,
1139 struct RPSPeer *peer = cls;
1140 struct GNUNET_RPS_Handle *h = op_result;
1142 if (NULL != peer->rps_srh)
1144 GNUNET_RPS_stream_cancel (peer->rps_srh);
1145 peer->rps_srh = NULL;
1147 GNUNET_assert (NULL != peer);
1148 GNUNET_RPS_disconnect (h);
1149 peer->rps_handle = NULL;
1153 /***********************************************************************
1154 * Definition of tests
1155 ***********************************************************************/
1157 // TODO check whether tests can be stopped earlier
1159 default_eval_cb (void)
1173 * Initialise given RPSPeer
1176 default_init_peer (struct RPSPeer *rps_peer)
1178 rps_peer->num_ids_to_request = 1;
1183 * Callback to call on receipt of a reply
1185 * @param cls closure
1186 * @param n number of peers
1187 * @param recv_peers the received peers
1190 default_reply_handle (void *cls,
1192 const struct GNUNET_PeerIdentity *recv_peers)
1194 struct RPSPeer *rps_peer;
1195 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1198 rps_peer = pending_rep->rps_peer;
1199 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1200 rps_peer->pending_rep_tail,
1202 rps_peer->num_pending_reps--;
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "[%s] got %" PRIu64 " peers:\n",
1205 GNUNET_i2s (rps_peer->peer_id),
1208 for (i = 0; i < n; i++)
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 GNUNET_i2s (&recv_peers[i]));
1215 rps_peer->num_recv_ids++;
1218 if ((0 == evaluate ()) && (HAVE_QUICK_QUIT == cur_test_run.have_quick_quit))
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test succeeded before timeout\n");
1221 GNUNET_assert (NULL != post_test_task);
1222 GNUNET_SCHEDULER_cancel (post_test_task);
1223 post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
1224 GNUNET_assert (NULL != post_test_task);
1230 * Request random peers.
1233 request_peers (void *cls)
1235 struct PendingRequest *pending_req = cls;
1236 struct RPSPeer *rps_peer;
1237 struct PendingReply *pending_rep;
1239 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1241 rps_peer = pending_req->rps_peer;
1242 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1243 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1244 rps_peer->pending_req_tail,
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "Requesting one peer\n");
1248 pending_rep = GNUNET_new (struct PendingReply);
1249 pending_rep->rps_peer = rps_peer;
1250 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1252 cur_test_run.reply_handle,
1254 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1255 rps_peer->pending_rep_tail,
1257 rps_peer->num_pending_reps++;
1258 rps_peer->num_pending_reqs--;
1263 cancel_pending_req (struct PendingRequest *pending_req)
1265 struct RPSPeer *rps_peer;
1267 rps_peer = pending_req->rps_peer;
1268 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1269 rps_peer->pending_req_tail,
1271 rps_peer->num_pending_reqs--;
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "Cancelling pending request\n");
1274 GNUNET_SCHEDULER_cancel (pending_req->request_task);
1275 GNUNET_free (pending_req);
1280 cancel_request (struct PendingReply *pending_rep)
1282 struct RPSPeer *rps_peer;
1284 rps_peer = pending_rep->rps_peer;
1285 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1286 rps_peer->pending_rep_tail,
1288 rps_peer->num_pending_reps--;
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Cancelling request\n");
1291 GNUNET_RPS_request_cancel (pending_rep->req_handle);
1292 GNUNET_free (pending_rep);
1300 cancel_request_cb (void *cls)
1302 struct RPSPeer *rps_peer = cls;
1303 struct PendingReply *pending_rep;
1305 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1307 pending_rep = rps_peer->pending_rep_head;
1308 GNUNET_assert (1 <= rps_peer->num_pending_reps);
1309 cancel_request (pending_rep);
1314 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1315 * issued, nor replied
1318 schedule_missing_requests (struct RPSPeer *rps_peer)
1321 struct PendingRequest *pending_req;
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Scheduling %u - %u missing requests\n",
1325 rps_peer->num_ids_to_request,
1326 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1327 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1328 rps_peer->num_ids_to_request);
1329 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1330 i < rps_peer->num_ids_to_request; i++)
1332 pending_req = GNUNET_new (struct PendingRequest);
1333 pending_req->rps_peer = rps_peer;
1334 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1335 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1336 cur_test_run.request_interval * i),
1339 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1340 rps_peer->pending_req_tail,
1342 rps_peer->num_pending_reqs++;
1348 cancel_pending_req_rep (struct RPSPeer *rps_peer)
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 "Cancelling all (pending) requests.\n");
1352 while (NULL != rps_peer->pending_req_head)
1353 cancel_pending_req (rps_peer->pending_req_head);
1354 GNUNET_assert (0 == rps_peer->num_pending_reqs);
1355 while (NULL != rps_peer->pending_rep_head)
1356 cancel_request (rps_peer->pending_rep_head);
1357 GNUNET_assert (0 == rps_peer->num_pending_reps);
1361 /***********************************
1363 ***********************************/
1366 * Initialise only non-mal RPSPeers
1369 mal_init_peer (struct RPSPeer *rps_peer)
1371 if (rps_peer->index >= round (portion * num_peers))
1372 rps_peer->num_ids_to_request = 1;
1377 * @brief Set peers to (non-)malicious before execution
1379 * Of signature #PreTest
1381 * @param rps_peer the peer to set (non-) malicious
1382 * @param h the handle to the service
1385 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1387 #if ENABLE_MALICIOUS
1388 uint32_t num_mal_peers;
1390 GNUNET_assert ((1 >= portion) &&
1392 num_mal_peers = round (portion * num_peers);
1394 if (rps_peer->index < num_mal_peers)
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "%u. peer [%s] of %" PRIu32
1398 " malicious peers turning malicious\n",
1400 GNUNET_i2s (rps_peer->peer_id),
1403 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1404 rps_peer_ids, target_peer);
1406 #endif /* ENABLE_MALICIOUS */
1411 mal_cb (struct RPSPeer *rps_peer)
1413 uint32_t num_mal_peers;
1415 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1420 #if ENABLE_MALICIOUS
1421 GNUNET_assert ((1 >= portion) &&
1423 num_mal_peers = round (portion * num_peers);
1425 if (rps_peer->index >= num_mal_peers)
1426 { /* It's useless to ask a malicious peer about a random sample -
1427 it's not sampling */
1428 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
1429 GNUNET_TIME_UNIT_SECONDS, 2),
1430 seed_peers, rps_peer);
1431 schedule_missing_requests (rps_peer);
1433 #endif /* ENABLE_MALICIOUS */
1437 /***********************************
1439 ***********************************/
1441 single_req_cb (struct RPSPeer *rps_peer)
1443 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1448 schedule_missing_requests (rps_peer);
1452 /***********************************
1454 ***********************************/
1456 delay_req_cb (struct RPSPeer *rps_peer)
1458 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1463 schedule_missing_requests (rps_peer);
1467 /***********************************
1469 ***********************************/
1471 seed_cb (struct RPSPeer *rps_peer)
1473 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1478 GNUNET_SCHEDULER_add_delayed (
1479 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
1480 seed_peers, rps_peer);
1484 /***********************************
1486 ***********************************/
1488 seed_big_cb (struct RPSPeer *rps_peer)
1490 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1495 // TODO test seeding > GNUNET_MAX_MESSAGE_SIZE peers
1496 GNUNET_SCHEDULER_add_delayed (
1497 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1498 seed_peers_big, rps_peer);
1502 /***********************************
1504 ***********************************/
1506 single_peer_seed_cb (struct RPSPeer *rps_peer)
1513 /***********************************
1515 ***********************************/
1517 seed_req_cb (struct RPSPeer *rps_peer)
1519 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1524 GNUNET_SCHEDULER_add_delayed (
1525 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1526 seed_peers, rps_peer);
1527 schedule_missing_requests (rps_peer);
1531 // TODO start big mal
1533 /***********************************
1535 ***********************************/
1537 req_cancel_cb (struct RPSPeer *rps_peer)
1539 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1544 schedule_missing_requests (rps_peer);
1545 GNUNET_SCHEDULER_add_delayed (
1546 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1547 (cur_test_run.request_interval + 1)),
1548 cancel_request_cb, rps_peer);
1552 /***********************************
1554 ***********************************/
1560 * @brief Starts churn
1562 * Has signature of #MainTest
1564 * This is not implemented too nicely as this is called for each peer, but we
1565 * only need to call it once. (Yes we check that we only schedule the task
1568 * @param rps_peer The peer it's called for
1571 churn_test_cb (struct RPSPeer *rps_peer)
1573 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1579 if ((HAVE_CHURN == cur_test_run.have_churn) && (NULL == churn_task))
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1582 "Starting churn task\n");
1583 churn_task = GNUNET_SCHEDULER_add_delayed (
1584 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "Not starting churn task\n");
1594 schedule_missing_requests (rps_peer);
1598 /***********************************
1600 ***********************************/
1603 got_stream_peer_cb (void *cls,
1605 const struct GNUNET_PeerIdentity *peers)
1607 const struct RPSPeer *rps_peer = cls;
1609 for (uint64_t i = 0; i < num_peers; i++)
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 "Peer %" PRIu32 " received [%s] from stream.\n",
1614 GNUNET_i2s (&peers[i]));
1615 if ((0 != rps_peer->index) &&
1616 (0 == memcmp (&peers[i],
1617 &rps_peers[0].peer_id,
1618 sizeof(struct GNUNET_PeerIdentity))))
1620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1621 "Received a peer id outside sub\n");
1624 else if ((0 == rps_peer->index) &&
1625 (0 != memcmp (&peers[i],
1626 &rps_peers[0].peer_id,
1627 sizeof(struct GNUNET_PeerIdentity))))
1629 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1630 "Received a peer id outside sub (lonely)\n");
1638 sub_post (struct RPSPeer *rps_peer)
1640 if (0 != rps_peer->index)
1641 GNUNET_RPS_sub_stop (rps_peer->rps_handle, "test");
1643 GNUNET_RPS_sub_stop (rps_peer->rps_handle, "lonely");
1648 sub_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1652 if (0 != rps_peer->index)
1653 GNUNET_RPS_sub_start (h, "test");
1655 GNUNET_RPS_sub_start (h, "lonely"); /* have a group of one */
1656 rps_peer->rps_srh = GNUNET_RPS_stream_request (h,
1657 &got_stream_peer_cb,
1662 /***********************************
1664 ***********************************/
1667 * Callback to be called when RPS service is started or stopped at peers
1670 * @param op the operation handle
1671 * @param emsg NULL on success; otherwise an error description
1674 churn_cb (void *cls,
1675 struct GNUNET_TESTBED_Operation *op,
1680 struct OpListEntry *entry = cls;
1682 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1687 GNUNET_TESTBED_operation_done (entry->op);
1690 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1691 "Failed to start/stop RPS at a peer\n");
1692 GNUNET_SCHEDULER_shutdown ();
1695 GNUNET_assert (0 != entry->delta);
1697 num_peers_online += entry->delta;
1699 if (PEER_GO_OFFLINE == entry->delta)
1700 { /* Peer hopefully just went offline */
1701 if (GNUNET_YES != rps_peers[entry->index].online)
1703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1704 "peer %s was expected to go offline but is still marked as online\n",
1705 GNUNET_i2s (rps_peers[entry->index].peer_id));
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "peer %s probably went offline as expected\n",
1712 GNUNET_i2s (rps_peers[entry->index].peer_id));
1714 rps_peers[entry->index].online = GNUNET_NO;
1717 else if (PEER_GO_ONLINE < entry->delta)
1718 { /* Peer hopefully just went online */
1719 if (GNUNET_NO != rps_peers[entry->index].online)
1721 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1722 "peer %s was expected to go online but is still marked as offline\n",
1723 GNUNET_i2s (rps_peers[entry->index].peer_id));
1728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1729 "peer %s probably went online as expected\n",
1730 GNUNET_i2s (rps_peers[entry->index].peer_id));
1731 if (NULL != cur_test_run.pre_test)
1733 cur_test_run.pre_test (&rps_peers[entry->index],
1734 rps_peers[entry->index].rps_handle);
1735 schedule_missing_requests (&rps_peers[entry->index]);
1738 rps_peers[entry->index].online = GNUNET_YES;
1742 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1743 "Invalid value for delta: %i\n", entry->delta);
1747 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1748 rps_peers[entry->index].entry_op_manage = NULL;
1749 GNUNET_free (entry);
1750 // if (num_peers_in_round[current_round] == peers_running)
1756 * @brief Set the rps-service up or down for a specific peer
1758 * @param i index of action
1759 * @param j index of peer
1760 * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1761 * @param prob_go_on_off the probability of the action
1764 manage_service_wrapper (unsigned int i, unsigned int j,
1765 enum PEER_ONLINE_DELTA delta,
1766 double prob_go_on_off)
1768 struct OpListEntry *entry = NULL;
1771 /* make sure that management operation is not already scheduled */
1772 if (NULL != rps_peers[j].entry_op_manage)
1777 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1780 "%u. selected peer (%u: %s) is %s.\n",
1783 GNUNET_i2s (rps_peers[j].peer_id),
1784 (PEER_GO_ONLINE == delta) ? "online" : "offline");
1785 if (prob < prob_go_on_off * UINT32_MAX)
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 GNUNET_i2s (rps_peers[j].peer_id),
1790 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1792 if (PEER_GO_OFFLINE == delta)
1793 cancel_pending_req_rep (&rps_peers[j]);
1794 entry = make_oplist_entry ();
1795 entry->delta = delta;
1797 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1802 (PEER_GO_OFFLINE == delta) ?
1804 rps_peers[j].entry_op_manage = entry;
1815 double portion_online;
1816 unsigned int *permut;
1817 double prob_go_offline;
1818 double portion_go_online;
1819 double portion_go_offline;
1821 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 "Churn function executing\n");
1828 churn_task = NULL; /* Should be invalid by now */
1830 /* Compute the probability for an online peer to go offline
1832 portion_online = num_peers_online * 1.0 / num_peers;
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1834 "Portion online: %f\n",
1836 portion_go_online = ((1 - portion_online) * .5 * .66);
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1838 "Portion that should go online: %f\n",
1840 portion_go_offline = (portion_online + portion_go_online) - .75;
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "Portion that probably goes offline: %f\n",
1843 portion_go_offline);
1844 prob_go_offline = portion_go_offline / (portion_online * .5);
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 "Probability of a selected online peer to go offline: %f\n",
1849 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1850 (unsigned int) num_peers);
1852 /* Go over 50% randomly chosen peers */
1853 for (i = 0; i < .5 * num_peers; i++)
1857 /* If online, shut down with certain probability */
1858 if (GNUNET_YES == rps_peers[j].online)
1860 manage_service_wrapper (i, j, -1, prob_go_offline);
1863 /* If offline, restart with certain probability */
1864 else if (GNUNET_NO == rps_peers[j].online)
1866 manage_service_wrapper (i, j, 1, 0.66);
1870 GNUNET_free (permut);
1872 churn_task = GNUNET_SCHEDULER_add_delayed (
1873 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1880 * Initialise given RPSPeer
1883 profiler_init_peer (struct RPSPeer *rps_peer)
1885 if (num_peers - 1 == rps_peer->index)
1886 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1891 * Callback to call on receipt of a reply
1893 * @param cls closure
1894 * @param n number of peers
1895 * @param recv_peers the received peers
1898 profiler_reply_handle (void *cls,
1900 const struct GNUNET_PeerIdentity *recv_peers)
1902 struct RPSPeer *rps_peer;
1903 struct RPSPeer *rcv_rps_peer;
1907 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1909 rps_peer = pending_rep->rps_peer;
1910 file_name = "/tmp/rps/received_ids";
1911 file_name_dh = "/tmp/rps/diehard_input";
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1913 "[%s] got %" PRIu64 " peers:\n",
1914 GNUNET_i2s (rps_peer->peer_id),
1916 for (i = 0; i < n; i++)
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1921 GNUNET_i2s (&recv_peers[i]));
1924 GNUNET_i2s_full (&recv_peers[i]));
1925 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1926 GNUNET_assert (NULL != rcv_rps_peer);
1927 tofile (file_name_dh,
1929 (uint32_t) rcv_rps_peer->index);
1931 default_reply_handle (cls, n, recv_peers);
1936 profiler_cb (struct RPSPeer *rps_peer)
1938 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1944 if ((HAVE_CHURN == cur_test_run.have_churn) && (NULL == churn_task))
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947 "Starting churn task\n");
1948 churn_task = GNUNET_SCHEDULER_add_delayed (
1949 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1956 "Not starting churn task\n");
1959 /* Only request peer ids at one peer.
1960 * (It's the before-last because last one is target of the focussed attack.)
1962 if (eval_peer == rps_peer)
1963 schedule_missing_requests (rps_peer);
1968 * Function called from #profiler_eval with a filename.
1970 * @param cls closure
1971 * @param filename complete filename (absolute path)
1972 * @return #GNUNET_OK to continue to iterate,
1973 * #GNUNET_NO to stop iteration with no error,
1974 * #GNUNET_SYSERR to abort iteration with error!
1977 file_name_cb (void *cls, const char *filename)
1981 if (NULL != strstr (filename, "sampler_el"))
1983 struct RPS_SamplerElement *s_elem;
1984 struct GNUNET_CRYPTO_AuthKey auth_key;
1985 const char *key_char;
1988 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1989 tofile (filename, "--------------------------\n");
1991 auth_key = string_to_auth_key (key_char);
1992 s_elem = RPS_sampler_elem_create ();
1993 RPS_sampler_elem_set (s_elem, auth_key);
1995 for (i = 0; i < num_peers; i++)
1997 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1999 RPS_sampler_elem_destroy (s_elem);
2006 * This is run after the test finished.
2008 * Compute all perfect samples.
2011 profiler_eval (void)
2013 /* Compute perfect sample for each sampler element */
2014 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
2016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
2024 * @brief is b in view of a?
2032 is_in_view (uint32_t a, uint32_t b)
2036 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2038 if (0 == memcmp (rps_peers[b].peer_id,
2039 &rps_peers[a].cur_view[i],
2040 sizeof(struct GNUNET_PeerIdentity)))
2050 get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
2054 for (i = 0; i < num_peers; i++)
2056 if (0 == memcmp (pid,
2057 rps_peers[i].peer_id,
2058 sizeof(struct GNUNET_PeerIdentity)))
2063 // return 0; /* Should not happen - make compiler happy */
2064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2065 "No known _PeerIdentity %s!\n",
2066 GNUNET_i2s_full (pid));
2072 * @brief Counts number of peers in view of a that have b in their view
2080 count_containing_views (uint32_t a, uint32_t b)
2086 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2088 peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
2089 if (GNUNET_YES == is_in_view (peer_idx, b))
2099 * @brief Computes the probability for each other peer to be selected by the
2100 * sampling process based on the views of all peers
2102 * @param peer_idx index of the peer that is about to sample
2105 compute_probabilities (uint32_t peer_idx)
2107 // double probs[num_peers] = { 0 };
2108 double probs[num_peers];
2109 size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof(char);
2110 char *probs_as_str = GNUNET_malloc (probs_as_str_size);
2111 char *probs_as_str_cpy;
2116 uint32_t cont_views;
2117 uint32_t number_of_being_in_pull_events;
2119 uint32_t count_non_zero_prob = 0;
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2122 "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
2123 /* Firstly without knowledge of old views */
2124 for (i = 0; i < num_peers; i++)
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2127 "\tfor peer %" PRIu32 ":\n", i);
2128 view_size = rps_peers[i].cur_view_count;
2129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2130 "\t\tview_size: %" PRIu32 "\n", view_size);
2131 /* For peer i the probability of being sampled is
2132 * evenly distributed among all possibly observed peers. */
2133 /* We could have observed a peer in three cases:
2134 * 1. peer sent a push
2135 * 2. peer was contained in a pull reply
2136 * 3. peer was in history (sampler) - ignored for now */
2137 /* 1. Probability of having received a push from peer i */
2138 if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
2139 (1 <= (0.45 * view_size)))
2141 prob_push = 1.0 * binom (0.45 * view_size, 1)
2143 binom (view_size, 0.45 * view_size);
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2150 "\t\tposs choices from view: %" PRIu32 ", containing i: %"
2152 binom (view_size, 0.45 * view_size),
2153 binom (0.45 * view_size, 1));
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
2163 /* 2. Probability of peer i being contained in pulls */
2164 view_size = rps_peers[peer_idx].cur_view_count;
2165 cont_views = count_containing_views (peer_idx, i);
2166 number_of_being_in_pull_events =
2167 (binom (view_size, 0.45 * view_size)
2168 - binom (view_size - cont_views, 0.45 * view_size));
2169 if (0 != number_of_being_in_pull_events)
2171 prob_pull = number_of_being_in_pull_events
2173 (1.0 * binom (view_size, 0.45 * view_size));
2179 probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
2180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2181 "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
2182 " peers in its view who know %" PRIu32 " prob: %f\n",
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2189 "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
2190 binom (view_size, 0.45 * view_size));
2191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2192 "\t\tnumber of possible pull combinations without %" PRIu32
2195 binom (view_size - cont_views, 0.45 * view_size));
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2197 "\t\tnumber of possible pull combinations with %" PRIu32
2200 number_of_being_in_pull_events);
2203 count_non_zero_prob++;
2206 if (0 != count_non_zero_prob)
2208 for (i = 0; i < num_peers; i++)
2210 probs[i] = probs[i] * (1.0 / count_non_zero_prob);
2215 for (i = 0; i < num_peers; i++)
2221 for (i = 0; i < num_peers; i++)
2223 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2224 tmp = GNUNET_snprintf (probs_as_str,
2226 "%s %7.6f", probs_as_str_cpy, probs[i]);
2227 GNUNET_free (probs_as_str_cpy);
2228 GNUNET_assert (0 <= tmp);
2231 to_file_w_len (rps_peers[peer_idx].file_name_probs,
2234 GNUNET_free (probs_as_str);
2239 * @brief This counts the number of peers in which views a given peer occurs.
2241 * It also stores this value in the rps peer.
2243 * @param peer_idx the index of the peer to count the representation
2245 * @return the number of occurrences
2248 count_peer_in_views_2 (uint32_t peer_idx)
2253 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2255 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2257 if (0 == memcmp (rps_peers[peer_idx].peer_id,
2258 &rps_peers[i].cur_view[j],
2259 sizeof(struct GNUNET_PeerIdentity)))
2266 rps_peers[peer_idx].count_in_views = count;
2272 cumulated_view_sizes ()
2277 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2279 view_sizes += rps_peers[i].cur_view_count;
2286 count_peer_in_views (uint32_t *count_peers)
2290 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2292 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2294 if (0 == memcmp (rps_peers[i].peer_id,
2295 &rps_peers[i].cur_view[j],
2296 sizeof(struct GNUNET_PeerIdentity)))
2306 compute_diversity ()
2309 /* ith entry represents the numer of occurrences in other peer's views */
2310 uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
2311 uint32_t views_total_size;
2313 /* deviation from expected number of peers */
2314 double *deviation = GNUNET_new_array (num_peers, double);
2316 views_total_size = 0;
2319 /* For each peer count its representation in other peer's views*/
2320 for (i = 0; i < num_peers; i++) /* Peer to count */
2322 views_total_size += rps_peers[i].cur_view_count;
2323 count_peer_in_views (count_peers);
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32
2328 GNUNET_i2s (rps_peers[i].peer_id),
2332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2333 "size of all views combined: %" PRIu32 "\n",
2335 expected = ((double) 1 / num_peers) * views_total_size;
2336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2337 "Expected number of occurrences of each peer in all views: %f\n",
2339 for (i = 0; i < num_peers; i++) /* Peer to count */
2341 deviation[i] = expected - count_peers[i];
2342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2343 "Deviation from expectation: %f\n", deviation[i]);
2345 GNUNET_free (count_peers);
2346 GNUNET_free (deviation);
2355 for (i = 0; i < num_peers; i++) /* Peer to count */
2357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2358 "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
2360 GNUNET_i2s (rps_peers[i].peer_id),
2361 rps_peers[i].cur_view_count);
2367 all_views_updated_cb ()
2369 compute_diversity ();
2370 print_view_sizes ();
2375 view_update_cb (void *cls,
2377 const struct GNUNET_PeerIdentity *peers)
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 "View was updated (%" PRIu64 ")\n", view_size);
2381 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
2382 to_file ("/tmp/rps/view_sizes.txt",
2383 "%" PRIu64 " %" PRIu32 "",
2386 for (int i = 0; i < view_size; i++)
2388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2389 "\t%s\n", GNUNET_i2s (&peers[i]));
2391 GNUNET_array_grow (rps_peer->cur_view,
2392 rps_peer->cur_view_count,
2394 // *rps_peer->cur_view = *peers;
2395 GNUNET_memcpy (rps_peer->cur_view,
2397 view_size * sizeof(struct GNUNET_PeerIdentity));
2398 to_file ("/tmp/rps/count_in_views.txt",
2399 "%" PRIu64 " %" PRIu32 "",
2401 count_peer_in_views_2 (rps_peer->index));
2402 cumulated_view_sizes ();
2405 to_file ("/tmp/rps/repr.txt",
2406 "%" PRIu64 /* index */
2407 " %" PRIu32 /* occurrence in views */
2408 " %" PRIu32 /* view sizes */
2409 " %f" /* fraction of repr in views */
2410 " %f" /* average view size */
2411 " %f" /* prob of occurrence in view slot */
2412 " %f" "", /* exp frac of repr in views */
2414 count_peer_in_views_2 (rps_peer->index),
2416 count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
2417 view_sizes / (view_size * 1.0), /* average view size */
2418 1.0 / view_size, /* prob of occurrence in view slot */
2419 (1.0 / view_size) * (view_sizes / view_size) /* expected fraction of repr in views */
2422 compute_probabilities (rps_peer->index);
2423 all_views_updated_cb ();
2428 pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
2430 rps_peer->file_name_probs =
2431 store_prefix_file_name (rps_peer->peer_id, "probs");
2432 GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
2437 write_final_stats (void)
2441 for (i = 0; i < num_peers; i++)
2443 to_file ("/tmp/rps/final_stats.dat",
2444 "%" PRIu32 " " /* index */
2446 PRIu64 " %" /* rounds */
2447 PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
2449 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* issued */
2450 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* sent */
2451 PRIu64 " %" PRIu64 " %" PRIu64 /* recv */,
2453 GNUNET_i2s (rps_peers[i].peer_id),
2454 rps_peers[i].num_rounds,
2455 rps_peers[i].num_blocks,
2456 rps_peers[i].num_blocks_many_push,
2457 rps_peers[i].num_blocks_no_push,
2458 rps_peers[i].num_blocks_no_pull,
2459 rps_peers[i].num_blocks_many_push_no_pull,
2460 rps_peers[i].num_blocks_no_push_no_pull,
2461 rps_peers[i].num_issued_push,
2462 rps_peers[i].num_issued_pull_req,
2463 rps_peers[i].num_issued_pull_rep,
2464 rps_peers[i].num_sent_push,
2465 rps_peers[i].num_sent_pull_req,
2466 rps_peers[i].num_sent_pull_rep,
2467 rps_peers[i].num_recv_push,
2468 rps_peers[i].num_recv_pull_req,
2469 rps_peers[i].num_recv_pull_rep);
2475 * Continuation called by #GNUNET_STATISTICS_get() functions.
2477 * Remembers that this specific statistics value was received for this peer.
2478 * Checks whether all peers received their statistics yet.
2479 * Issues the shutdown.
2481 * @param cls closure
2482 * @param success #GNUNET_OK if statistics were
2483 * successfully obtained, #GNUNET_SYSERR if not.
2486 post_test_shutdown_ready_cb (void *cls,
2489 struct STATcls *stat_cls = (struct STATcls *) cls;
2490 struct RPSPeer *rps_peer = stat_cls->rps_peer;
2492 if (GNUNET_OK == success)
2494 /* set flag that we we got the value */
2495 rps_peer->stat_collected_flags |= stat_cls->stat_type;
2499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2500 "Peer %u did not receive statistics value\n",
2502 GNUNET_free (stat_cls);
2506 if ((NULL != rps_peer->stat_op) &&
2507 (GNUNET_YES == check_statistics_collect_completed_single_peer (
2510 GNUNET_TESTBED_operation_done (rps_peer->stat_op);
2513 write_final_stats ();
2514 if (GNUNET_YES == check_statistics_collect_completed ())
2516 // write_final_stats ();
2517 GNUNET_free (stat_cls);
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2520 GNUNET_SCHEDULER_shutdown ();
2524 GNUNET_free (stat_cls);
2530 * @brief Converts string representation to the corresponding #STAT_TYPE enum.
2532 * @param stat_str string representation of statistics specifier
2534 * @return corresponding enum
2537 stat_str_2_type (const char *stat_str)
2539 if (0 == strncmp ("# rounds blocked - no pull replies", stat_str, strlen (
2540 "# rounds blocked - no pull replies")))
2542 return STAT_TYPE_BLOCKS_NO_PULL;
2544 else if (0 == strncmp ("# rounds blocked - too many pushes, no pull replies",
2546 "# rounds blocked - too many pushes, no pull replies")))
2548 return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
2550 else if (0 == strncmp ("# rounds blocked - too many pushes", stat_str,
2551 strlen ("# rounds blocked - too many pushes")))
2553 return STAT_TYPE_BLOCKS_MANY_PUSH;
2555 else if (0 == strncmp ("# rounds blocked - no pushes, no pull replies",
2557 "# rounds blocked - no pushes, no pull replies")))
2559 return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
2561 else if (0 == strncmp ("# rounds blocked - no pushes", stat_str, strlen (
2562 "# rounds blocked - no pushes")))
2564 return STAT_TYPE_BLOCKS_NO_PUSH;
2566 else if (0 == strncmp ("# rounds blocked", stat_str, strlen (
2567 "# rounds blocked")))
2569 return STAT_TYPE_BLOCKS;
2571 else if (0 == strncmp ("# rounds", stat_str, strlen ("# rounds")))
2573 return STAT_TYPE_ROUNDS;
2575 else if (0 == strncmp ("# push send issued", stat_str, strlen (
2576 "# push send issued")))
2578 return STAT_TYPE_ISSUED_PUSH_SEND;
2580 else if (0 == strncmp ("# pull request send issued", stat_str, strlen (
2581 "# pull request send issued")))
2583 return STAT_TYPE_ISSUED_PULL_REQ;
2585 else if (0 == strncmp ("# pull reply send issued", stat_str, strlen (
2586 "# pull reply send issued")))
2588 return STAT_TYPE_ISSUED_PULL_REP;
2590 else if (0 == strncmp ("# pushes sent", stat_str, strlen ("# pushes sent")))
2592 return STAT_TYPE_SENT_PUSH_SEND;
2594 else if (0 == strncmp ("# pull requests sent", stat_str, strlen (
2595 "# pull requests sent")))
2597 return STAT_TYPE_SENT_PULL_REQ;
2599 else if (0 == strncmp ("# pull replys sent", stat_str, strlen (
2600 "# pull replys sent")))
2602 return STAT_TYPE_SENT_PULL_REP;
2604 else if (0 == strncmp ("# push message received", stat_str, strlen (
2605 "# push message received")))
2607 return STAT_TYPE_RECV_PUSH_SEND;
2609 else if (0 == strncmp ("# pull request message received", stat_str, strlen (
2610 "# pull request message received")))
2612 return STAT_TYPE_RECV_PULL_REQ;
2614 else if (0 == strncmp ("# pull reply messages received", stat_str, strlen (
2615 "# pull reply messages received")))
2617 return STAT_TYPE_RECV_PULL_REP;
2619 return STAT_TYPE_MAX;
2624 * @brief Converts #STAT_TYPE enum to the equivalent string representation that
2625 * is stored with the statistics service.
2627 * @param stat_type #STAT_TYPE enum
2629 * @return string representation that matches statistics value
2632 stat_type_2_str (enum STAT_TYPE stat_type)
2636 case STAT_TYPE_ROUNDS:
2639 case STAT_TYPE_BLOCKS:
2640 return "# rounds blocked";
2642 case STAT_TYPE_BLOCKS_MANY_PUSH:
2643 return "# rounds blocked - too many pushes";
2645 case STAT_TYPE_BLOCKS_NO_PUSH:
2646 return "# rounds blocked - no pushes";
2648 case STAT_TYPE_BLOCKS_NO_PULL:
2649 return "# rounds blocked - no pull replies";
2651 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2652 return "# rounds blocked - too many pushes, no pull replies";
2654 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2655 return "# rounds blocked - no pushes, no pull replies";
2657 case STAT_TYPE_ISSUED_PUSH_SEND:
2658 return "# push send issued";
2660 case STAT_TYPE_ISSUED_PULL_REQ:
2661 return "# pull request send issued";
2663 case STAT_TYPE_ISSUED_PULL_REP:
2664 return "# pull reply send issued";
2666 case STAT_TYPE_SENT_PUSH_SEND:
2667 return "# pushes sent";
2669 case STAT_TYPE_SENT_PULL_REQ:
2670 return "# pull requests sent";
2672 case STAT_TYPE_SENT_PULL_REP:
2673 return "# pull replys sent";
2675 case STAT_TYPE_RECV_PUSH_SEND:
2676 return "# push message received";
2678 case STAT_TYPE_RECV_PULL_REQ:
2679 return "# pull request message received";
2681 case STAT_TYPE_RECV_PULL_REP:
2682 return "# pull reply messages received";
2693 * Callback function to process statistic values.
2695 * @param cls closure
2696 * @param subsystem name of subsystem that created the statistic
2697 * @param name the name of the datum
2698 * @param value the current value
2699 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
2700 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
2703 stat_iterator (void *cls,
2704 const char *subsystem,
2710 (void) is_persistent;
2711 const struct STATcls *stat_cls = (const struct STATcls *) cls;
2712 struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got stat value: %s - %" PRIu64 "\n",
2714 // stat_type_2_str (stat_cls->stat_type),
2717 to_file (rps_peer->file_name_stats,
2718 "%s: %" PRIu64 "\n",
2721 switch (stat_str_2_type (name))
2723 case STAT_TYPE_ROUNDS:
2724 rps_peer->num_rounds = value;
2727 case STAT_TYPE_BLOCKS:
2728 rps_peer->num_blocks = value;
2731 case STAT_TYPE_BLOCKS_MANY_PUSH:
2732 rps_peer->num_blocks_many_push = value;
2735 case STAT_TYPE_BLOCKS_NO_PUSH:
2736 rps_peer->num_blocks_no_push = value;
2739 case STAT_TYPE_BLOCKS_NO_PULL:
2740 rps_peer->num_blocks_no_pull = value;
2743 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2744 rps_peer->num_blocks_many_push_no_pull = value;
2747 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2748 rps_peer->num_blocks_no_push_no_pull = value;
2751 case STAT_TYPE_ISSUED_PUSH_SEND:
2752 rps_peer->num_issued_push = value;
2755 case STAT_TYPE_ISSUED_PULL_REQ:
2756 rps_peer->num_issued_pull_req = value;
2759 case STAT_TYPE_ISSUED_PULL_REP:
2760 rps_peer->num_issued_pull_rep = value;
2763 case STAT_TYPE_SENT_PUSH_SEND:
2764 rps_peer->num_sent_push = value;
2767 case STAT_TYPE_SENT_PULL_REQ:
2768 rps_peer->num_sent_pull_req = value;
2771 case STAT_TYPE_SENT_PULL_REP:
2772 rps_peer->num_sent_pull_rep = value;
2775 case STAT_TYPE_RECV_PUSH_SEND:
2776 rps_peer->num_recv_push = value;
2779 case STAT_TYPE_RECV_PULL_REQ:
2780 rps_peer->num_recv_pull_req = value;
2783 case STAT_TYPE_RECV_PULL_REP:
2784 rps_peer->num_recv_pull_rep = value;
2789 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2790 "Unknown statistics string: %s\n",
2799 post_profiler (struct RPSPeer *rps_peer)
2801 if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2807 "Going to request statistic values with mask 0x%" PRIx32 "\n",
2808 cur_test_run.stat_collect_flags);
2810 struct STATcls *stat_cls;
2812 for (stat_type = STAT_TYPE_ROUNDS;
2813 stat_type < STAT_TYPE_MAX;
2814 stat_type = stat_type << 1)
2816 if (stat_type & cur_test_run.stat_collect_flags)
2818 stat_cls = GNUNET_malloc (sizeof(struct STATcls));
2819 stat_cls->rps_peer = rps_peer;
2820 stat_cls->stat_type = stat_type;
2821 rps_peer->file_name_stats =
2822 store_prefix_file_name (rps_peer->peer_id, "stats");
2823 GNUNET_STATISTICS_get (rps_peer->stats_h,
2825 stat_type_2_str (stat_type),
2826 post_test_shutdown_ready_cb,
2828 (struct STATcls *) stat_cls);
2829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2830 "Requested statistics for %s (peer %" PRIu32 ")\n",
2831 stat_type_2_str (stat_type),
2838 /***********************************************************************
2839 * /Definition of tests
2840 ***********************************************************************/
2844 * Actual "main" function for the testcase.
2846 * @param cls closure
2847 * @param h the run handle
2848 * @param n_peers number of peers in 'peers'
2849 * @param peers handle to peers run in the testbed
2850 * @param links_succeeded the number of overlay link connection attempts that
2852 * @param links_failed the number of overlay link connection attempts that
2857 struct GNUNET_TESTBED_RunHandle *h,
2858 unsigned int n_peers,
2859 struct GNUNET_TESTBED_Peer **peers,
2860 unsigned int links_succeeded,
2861 unsigned int links_failed)
2865 (void) links_failed;
2867 struct OpListEntry *entry;
2869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
2871 /* Check whether we timed out */
2872 if ((n_peers != num_peers) ||
2874 (0 == links_succeeded) )
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2877 "Going down due to args (eg. timeout)\n");
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n",
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
2882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n",
2884 GNUNET_SCHEDULER_shutdown ();
2889 /* Initialize peers */
2890 testbed_peers = peers;
2891 num_peers_online = 0;
2892 for (i = 0; i < num_peers; i++)
2894 entry = make_oplist_entry ();
2896 rps_peers[i].index = i;
2897 if (NULL != cur_test_run.init_peer)
2898 cur_test_run.init_peer (&rps_peers[i]);
2899 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2901 rps_peers->cur_view_count = 0;
2902 rps_peers->cur_view = NULL;
2904 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2905 GNUNET_TESTBED_PIT_IDENTITY,
2910 /* Bring peers up */
2911 GNUNET_assert (num_peers == n_peers);
2912 for (i = 0; i < n_peers; i++)
2914 rps_peers[i].index = i;
2916 GNUNET_TESTBED_service_connect (&rps_peers[i],
2919 &rps_connect_complete_cb,
2921 &rps_connect_adapter,
2922 &rps_disconnect_adapter,
2924 /* Connect all peers to statistics service */
2925 if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2927 rps_peers[i].stat_op =
2928 GNUNET_TESTBED_service_connect (NULL,
2933 &stat_connect_adapter,
2934 &stat_disconnect_adapter,
2939 if (NULL != churn_task)
2940 GNUNET_SCHEDULER_cancel (churn_task);
2941 post_test_task = GNUNET_SCHEDULER_add_delayed (timeout, &post_test_op, NULL);
2942 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2943 (timeout_s * 1.2) + 0.1 * num_peers);
2944 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
2949 * Entry point for the testcase, sets up the testbed.
2951 * @param argc unused
2952 * @param argv unused
2953 * @return 0 on success
2956 main (int argc, char *argv[])
2962 /* Defaults for tests */
2964 cur_test_run.name = "test-rps-default";
2965 cur_test_run.init_peer = default_init_peer;
2966 cur_test_run.pre_test = NULL;
2967 cur_test_run.reply_handle = default_reply_handle;
2968 cur_test_run.eval_cb = default_eval_cb;
2969 cur_test_run.post_test = NULL;
2970 cur_test_run.have_churn = HAVE_CHURN;
2971 cur_test_run.have_collect_statistics = NO_COLLECT_STATISTICS;
2972 cur_test_run.stat_collect_flags = 0;
2973 cur_test_run.have_collect_view = NO_COLLECT_VIEW;
2977 if (strstr (argv[0], "malicious") != NULL)
2979 cur_test_run.pre_test = mal_pre;
2980 cur_test_run.main_test = mal_cb;
2981 cur_test_run.init_peer = mal_init_peer;
2984 if (strstr (argv[0], "_1") != NULL)
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
2987 cur_test_run.name = "test-rps-malicious_1";
2990 else if (strstr (argv[0], "_2") != NULL)
2992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
2993 cur_test_run.name = "test-rps-malicious_2";
2996 else if (strstr (argv[0], "_3") != NULL)
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
2999 cur_test_run.name = "test-rps-malicious_3";
3004 else if (strstr (argv[0], "_single_req") != NULL)
3006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
3007 cur_test_run.name = "test-rps-single-req";
3008 cur_test_run.main_test = single_req_cb;
3009 cur_test_run.have_churn = HAVE_NO_CHURN;
3012 else if (strstr (argv[0], "_delayed_reqs") != NULL)
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
3015 cur_test_run.name = "test-rps-delayed-reqs";
3016 cur_test_run.main_test = delay_req_cb;
3017 cur_test_run.have_churn = HAVE_NO_CHURN;
3020 else if (strstr (argv[0], "_seed_big") != NULL)
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3023 "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
3025 cur_test_run.name = "test-rps-seed-big";
3026 cur_test_run.main_test = seed_big_cb;
3027 cur_test_run.eval_cb = no_eval;
3028 cur_test_run.have_churn = HAVE_NO_CHURN;
3032 else if (strstr (argv[0], "_single_peer_seed") != NULL)
3034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3035 "Test seeding and requesting on a single peer\n");
3036 cur_test_run.name = "test-rps-single-peer-seed";
3037 cur_test_run.main_test = single_peer_seed_cb;
3038 cur_test_run.have_churn = HAVE_NO_CHURN;
3041 else if (strstr (argv[0], "_seed_request") != NULL)
3043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3044 "Test seeding and requesting on multiple peers\n");
3045 cur_test_run.name = "test-rps-seed-request";
3046 cur_test_run.main_test = seed_req_cb;
3047 cur_test_run.have_churn = HAVE_NO_CHURN;
3050 else if (strstr (argv[0], "_seed") != NULL)
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
3053 cur_test_run.name = "test-rps-seed";
3054 cur_test_run.main_test = seed_cb;
3055 cur_test_run.eval_cb = no_eval;
3056 cur_test_run.have_churn = HAVE_NO_CHURN;
3059 else if (strstr (argv[0], "_req_cancel") != NULL)
3061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
3062 cur_test_run.name = "test-rps-req-cancel";
3064 cur_test_run.main_test = req_cancel_cb;
3065 cur_test_run.eval_cb = no_eval;
3066 cur_test_run.have_churn = HAVE_NO_CHURN;
3070 else if (strstr (argv[0], "_churn") != NULL)
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
3073 cur_test_run.name = "test-rps-churn";
3075 cur_test_run.init_peer = default_init_peer;
3076 cur_test_run.main_test = churn_test_cb;
3077 cur_test_run.reply_handle = default_reply_handle;
3078 cur_test_run.eval_cb = default_eval_cb;
3079 cur_test_run.have_churn = HAVE_NO_CHURN;
3080 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
3084 else if (strstr (argv[0], "_sub") != NULL)
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test subs\n");
3087 cur_test_run.name = "test-rps-sub";
3089 // cur_test_run.init_peer = &default_init_peer;
3090 cur_test_run.pre_test = &sub_pre;
3091 cur_test_run.main_test = &single_req_cb;
3092 // cur_test_run.reply_handle = default_reply_handle;
3093 cur_test_run.post_test = &sub_post;
3094 // cur_test_run.eval_cb = default_eval_cb;
3095 cur_test_run.have_churn = HAVE_NO_CHURN;
3096 cur_test_run.have_quick_quit = HAVE_QUICK_QUIT;
3099 else if (strstr (argv[0], "profiler") != NULL)
3101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
3102 cur_test_run.name = "test-rps-profiler";
3105 cur_test_run.init_peer = profiler_init_peer;
3106 // cur_test_run.pre_test = mal_pre;
3107 cur_test_run.pre_test = pre_profiler;
3108 cur_test_run.main_test = profiler_cb;
3109 cur_test_run.reply_handle = profiler_reply_handle;
3110 cur_test_run.eval_cb = profiler_eval;
3111 cur_test_run.post_test = post_profiler;
3112 cur_test_run.request_interval = 2;
3113 cur_test_run.num_requests = 5;
3114 // cur_test_run.have_churn = HAVE_CHURN;
3115 cur_test_run.have_churn = HAVE_NO_CHURN;
3116 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
3117 cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
3118 cur_test_run.stat_collect_flags = STAT_TYPE_ROUNDS
3120 | STAT_TYPE_BLOCKS_MANY_PUSH
3121 | STAT_TYPE_BLOCKS_NO_PUSH
3122 | STAT_TYPE_BLOCKS_NO_PULL
3123 | STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL
3124 | STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL
3125 | STAT_TYPE_ISSUED_PUSH_SEND
3126 | STAT_TYPE_ISSUED_PULL_REQ
3127 | STAT_TYPE_ISSUED_PULL_REP
3128 | STAT_TYPE_SENT_PUSH_SEND
3129 | STAT_TYPE_SENT_PULL_REQ
3130 | STAT_TYPE_SENT_PULL_REP
3131 | STAT_TYPE_RECV_PUSH_SEND
3132 | STAT_TYPE_RECV_PULL_REQ
3133 | STAT_TYPE_RECV_PULL_REP;
3134 cur_test_run.have_collect_view = COLLECT_VIEW;
3137 /* 'Clean' directory */
3138 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
3139 GNUNET_DISK_directory_create ("/tmp/rps/");
3141 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_s);
3143 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
3144 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
3145 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
3146 if ((2 == mal_type) ||
3148 target_peer = &rps_peer_ids[num_peers - 2];
3149 if (profiler_eval == cur_test_run.eval_cb)
3150 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
3151 malicious peer if not careful
3152 with the malicious portion */
3155 ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
3160 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3161 "_test_run returned.\n");
3162 if (GNUNET_OK != ret_value)
3164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3165 "Test did not run successfully!\n");
3168 ret_value = cur_test_run.eval_cb ();
3170 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
3172 GNUNET_array_grow (rps_peers->cur_view,
3173 rps_peers->cur_view_count,
3176 GNUNET_free (rps_peers);
3177 GNUNET_free (rps_peer_ids);
3178 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
3183 /* end of test_rps.c */