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 {
76 * @brief Indicates peer going online
80 * @brief Indicates peer going offline
93 struct OpListEntry *next;
98 struct OpListEntry *prev;
101 * The testbed operation
103 struct GNUNET_TESTBED_Operation *op;
106 * Depending on whether we start or stop RPS service at the peer, set this to
107 * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
109 enum PEER_ONLINE_DELTA delta;
112 * Index of the regarding peer
120 static struct OpListEntry *oplist_head;
125 static struct OpListEntry *oplist_tail;
129 * A pending reply: A request was sent and the reply is pending.
136 struct PendingReply *next;
137 struct PendingReply *prev;
140 * Handle to the request we are waiting for
142 struct GNUNET_RPS_Request_Handle *req_handle;
145 * The peer that requested
147 struct RPSPeer *rps_peer;
152 * A pending request: A request was not made yet but is scheduled for later.
154 struct PendingRequest
159 struct PendingRequest *next;
160 struct PendingRequest *prev;
163 * Handle to the request we are waiting for
165 struct GNUNET_SCHEDULER_Task *request_task;
168 * The peer that requested
170 struct RPSPeer *rps_peer;
175 * Information we track for each peer.
185 * Handle for RPS connect operation.
187 struct GNUNET_TESTBED_Operation *op;
190 * Handle to RPS service.
192 struct GNUNET_RPS_Handle *rps_handle;
195 * Handle to stream requests
197 struct GNUNET_RPS_StreamRequestHandle *rps_srh;
202 struct GNUNET_PeerIdentity *peer_id;
205 * A request handle to check for an request
207 //struct GNUNET_RPS_Request_Handle *req_handle;
210 * Peer on- or offline?
215 * Number of Peer IDs to request during the whole test
217 unsigned int num_ids_to_request;
220 * Pending requests DLL
222 struct PendingRequest *pending_req_head;
223 struct PendingRequest *pending_req_tail;
226 * Number of pending requests
228 unsigned int num_pending_reqs;
231 * Pending replies DLL
233 struct PendingReply *pending_rep_head;
234 struct PendingReply *pending_rep_tail;
237 * Number of pending replies
239 unsigned int num_pending_reps;
242 * Number of received PeerIDs
244 unsigned int num_recv_ids;
247 * Pending operation on that peer
249 const struct OpListEntry *entry_op_manage;
252 * Testbed operation to connect to statistics service
254 struct GNUNET_TESTBED_Operation *stat_op;
257 * Handle to the statistics service
259 struct GNUNET_STATISTICS_Handle *stats_h;
262 * @brief flags to indicate which statistics values have been already
263 * collected from the statistics service.
264 * Used to check whether we are able to shutdown.
266 uint32_t stat_collected_flags;
269 * @brief File name of the file the stats are finally written to
271 const char *file_name_stats;
274 * @brief File name of the file the stats are finally written to
276 const char *file_name_probs;
279 * @brief The current view
281 struct GNUNET_PeerIdentity *cur_view;
284 * @brief Number of peers in the #cur_view.
286 uint32_t cur_view_count;
289 * @brief Number of occurrences in other peer's view
291 uint32_t count_in_views;
294 * @brief statistics values
298 uint64_t num_blocks_many_push;
299 uint64_t num_blocks_no_push;
300 uint64_t num_blocks_no_pull;
301 uint64_t num_blocks_many_push_no_pull;
302 uint64_t num_blocks_no_push_no_pull;
303 uint64_t num_issued_push;
304 uint64_t num_issued_pull_req;
305 uint64_t num_issued_pull_rep;
306 uint64_t num_sent_push;
307 uint64_t num_sent_pull_req;
308 uint64_t num_sent_pull_rep;
309 uint64_t num_recv_push;
310 uint64_t num_recv_pull_req;
311 uint64_t num_recv_pull_rep;
316 STAT_TYPE_ROUNDS = 0x1, /* 1 */
317 STAT_TYPE_BLOCKS = 0x2, /* 2 */
318 STAT_TYPE_BLOCKS_MANY_PUSH = 0x4, /* 3 */
319 STAT_TYPE_BLOCKS_NO_PUSH = 0x8, /* 4 */
320 STAT_TYPE_BLOCKS_NO_PULL = 0x10, /* 5 */
321 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL = 0x20, /* 6 */
322 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL = 0x40, /* 7 */
323 STAT_TYPE_ISSUED_PUSH_SEND = 0x80, /* 8 */
324 STAT_TYPE_ISSUED_PULL_REQ = 0x100, /* 9 */
325 STAT_TYPE_ISSUED_PULL_REP = 0x200, /* 10 */
326 STAT_TYPE_SENT_PUSH_SEND = 0x400, /* 11 */
327 STAT_TYPE_SENT_PULL_REQ = 0x800, /* 12 */
328 STAT_TYPE_SENT_PULL_REP = 0x1000, /* 13 */
329 STAT_TYPE_RECV_PUSH_SEND = 0x2000, /* 14 */
330 STAT_TYPE_RECV_PULL_REQ = 0x4000, /* 15 */
331 STAT_TYPE_RECV_PULL_REP = 0x8000, /* 16 */
332 STAT_TYPE_MAX = 0x80000000, /* 32 */
337 struct RPSPeer *rps_peer;
338 enum STAT_TYPE stat_type;
343 * Information for all the peers.
345 static struct RPSPeer *rps_peers;
348 * Peermap to get the index of a given peer ID quick.
350 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
355 static struct GNUNET_PeerIdentity *rps_peer_ids;
358 * ID of the targeted peer.
360 static struct GNUNET_PeerIdentity *target_peer;
363 * ID of the peer that requests for the evaluation.
365 static struct RPSPeer *eval_peer;
368 * Number of online peers.
370 static unsigned int num_peers_online;
373 * @brief The added sizes of the peer's views
375 static unsigned int view_sizes;
378 * Return value from 'main'.
383 * Identifier for the churn task that runs periodically
385 static struct GNUNET_SCHEDULER_Task *post_test_task;
388 * Identifier for the churn task that runs periodically
390 static struct GNUNET_SCHEDULER_Task *shutdown_task;
393 * Identifier for the churn task that runs periodically
395 static struct GNUNET_SCHEDULER_Task *churn_task;
398 * Called to initialise the given RPSPeer
400 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
403 * @brief Called directly after connecting to the service
405 * @param rps_peer Specific peer the function is called on
406 * @param h the handle to the rps service
408 typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
411 * @brief Executes functions to test the api/service for a given peer
413 * Called from within #rps_connect_complete_cb ()
414 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
415 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
417 * @param rps_peer the peer the task runs on
419 typedef void (*MainTest) (struct RPSPeer *rps_peer);
422 * Callback called once the requested random peers are available
424 typedef void (*ReplyHandle) (void *cls,
426 const struct GNUNET_PeerIdentity *recv_peers);
429 * Called directly before disconnecting from the service
431 typedef void (*PostTest) (struct RPSPeer *peer);
434 * Function called after disconnect to evaluate test success
436 typedef int (*EvaluationCallback) (void);
439 * @brief Do we have Churn?
443 * @brief If we have churn this is set
447 * @brief If we have no churn this is set
453 * @brief Is it ok to quit the test before the timeout?
455 enum OPTION_QUICK_QUIT {
457 * @brief It is ok for the test to quit before the timeout triggers
462 * @brief It is NOT ok for the test to quit before the timeout triggers
468 * @brief Do we collect statistics at the end?
470 enum OPTION_COLLECT_STATISTICS {
472 * @brief We collect statistics at the end
477 * @brief We do not collect statistics at the end
479 NO_COLLECT_STATISTICS,
483 * @brief Do we collect views during run?
485 enum OPTION_COLLECT_VIEW {
487 * @brief We collect view during run
492 * @brief We do not collect the view during run
498 * Structure to define a single test
508 * Called with a single peer in order to initialise that peer
513 * Called directly after connecting to the service
518 * Main function for each peer
523 * Callback called once the requested peers are available
525 ReplyHandle reply_handle;
528 * Called directly before disconnecting from the service
533 * Function to evaluate the test results
535 EvaluationCallback eval_cb;
540 uint32_t request_interval;
543 * Number of Requests to make.
545 uint32_t num_requests;
548 * Run with (-out) churn
550 enum OPTION_CHURN have_churn;
553 * Quit test before timeout?
555 enum OPTION_QUICK_QUIT have_quick_quit;
558 * Collect statistics at the end?
560 enum OPTION_COLLECT_STATISTICS have_collect_statistics;
563 * Collect view during run?
565 enum OPTION_COLLECT_VIEW have_collect_view;
568 * @brief Mark which values from the statistics service to collect at the end
571 uint32_t stat_collect_flags;
575 * Did we finish the test?
577 static int post_test;
580 * Are we shutting down?
582 static int in_shutdown;
585 * Append arguments to file
588 tofile_ (const char *file_name, const char *line)
590 struct GNUNET_DISK_FileHandle *f;
591 /* char output_buffer[512]; */
596 if (NULL == (f = GNUNET_DISK_file_open (file_name,
597 GNUNET_DISK_OPEN_APPEND |
598 GNUNET_DISK_OPEN_WRITE |
599 GNUNET_DISK_OPEN_CREATE,
600 GNUNET_DISK_PERM_USER_READ |
601 GNUNET_DISK_PERM_USER_WRITE |
602 GNUNET_DISK_PERM_GROUP_READ |
603 GNUNET_DISK_PERM_OTHER_READ)))
605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
606 "Not able to open file %s\n",
610 /* size = GNUNET_snprintf (output_buffer,
611 sizeof (output_buffer),
613 GNUNET_TIME_absolute_get ().abs_value_us,
617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
618 "Failed to write string to buffer (size: %i)\n",
623 size = strlen (line) * sizeof (char);
625 size2 = GNUNET_DISK_file_write (f, line, size);
628 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
629 "Unable to write to file! (Size: %lu, size2: %lu)\n",
632 if (GNUNET_YES != GNUNET_DISK_file_close (f))
634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
635 "Unable to close file\n");
640 if (GNUNET_YES != GNUNET_DISK_file_close (f))
642 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
643 "Unable to close file\n");
648 * This function is used to facilitate writing important information to disk
650 #define tofile(file_name, ...) do {\
653 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
655 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
656 "Failed to create tmp_buf\n");\
658 tofile_(file_name,tmp_buf);\
663 * Write the ids and their according index in the given array to a file
667 ids_to_file (char *file_name,
668 struct GNUNET_PeerIdentity *peer_ids,
669 unsigned int num_peer_ids)
673 for (i=0 ; i < num_peer_ids ; i++)
678 GNUNET_i2s_full (&peer_ids[i]));
683 * Task run on timeout to collect statistics and potentially shut down.
686 post_test_op (void *cls);
690 * Test the success of a single test
700 for (i = 0; i < num_peers; i++)
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
703 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
705 GNUNET_i2s (rps_peers[i].peer_id),
706 rps_peers[i].num_recv_ids,
707 rps_peers[i].num_ids_to_request,
708 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
709 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
711 return tmp_ok? 0 : 1;
716 * Creates an oplist entry and adds it to the oplist DLL
718 static struct OpListEntry *
721 struct OpListEntry *entry;
723 entry = GNUNET_new (struct OpListEntry);
724 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
730 * @brief Checks if given peer already received its statistics value from the
731 * statistics service.
733 * @param rps_peer the peer to check for
735 * @return #GNUNET_YES if so
736 * #GNUNET_NO otherwise
738 static int check_statistics_collect_completed_single_peer (
739 const struct RPSPeer *rps_peer)
741 if (cur_test_run.stat_collect_flags !=
742 (cur_test_run.stat_collect_flags &
743 rps_peer->stat_collected_flags))
752 * @brief Checks if all peers already received their statistics value from the
753 * statistics service.
755 * @return #GNUNET_YES if so
756 * #GNUNET_NO otherwise
758 static int check_statistics_collect_completed ()
762 for (i = 0; i < num_peers; i++)
764 if (GNUNET_NO == check_statistics_collect_completed_single_peer (&rps_peers[i]))
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "At least Peer %" PRIu32 " did not yet receive all statistics values\n",
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 "All peers received their statistics values\n");
779 * Task run on timeout to shut everything down.
782 shutdown_op (void *cls)
787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
788 "Shutdown task scheduled, going down.\n");
789 in_shutdown = GNUNET_YES;
790 if (NULL != post_test_task)
792 GNUNET_SCHEDULER_cancel (post_test_task);
795 if (NULL != churn_task)
797 GNUNET_SCHEDULER_cancel (churn_task);
800 for (i = 0; i < num_peers; i++)
802 if (NULL != rps_peers[i].rps_handle)
804 GNUNET_RPS_disconnect (rps_peers[i].rps_handle);
806 if (NULL != rps_peers[i].op)
808 GNUNET_TESTBED_operation_done (rps_peers[i].op);
815 * Task run on timeout to collect statistics and potentially shut down.
818 post_test_op (void *cls)
823 post_test_task = NULL;
824 post_test = GNUNET_YES;
825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
826 "Post test task scheduled, going down.\n");
827 if (NULL != churn_task)
829 GNUNET_SCHEDULER_cancel (churn_task);
832 for (i = 0; i < num_peers; i++)
834 if (NULL != cur_test_run.post_test)
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n", i);
837 cur_test_run.post_test (&rps_peers[i]);
839 if (NULL != rps_peers[i].op)
841 GNUNET_TESTBED_operation_done (rps_peers[i].op);
842 rps_peers[i].op = NULL;
845 /* If we do not collect statistics, shut down directly */
846 if (NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics ||
847 GNUNET_YES == check_statistics_collect_completed())
849 GNUNET_SCHEDULER_shutdown ();
858 seed_peers (void *cls)
860 struct RPSPeer *peer = cls;
864 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
869 GNUNET_assert (NULL != peer->rps_handle);
871 // TODO if malicious don't seed mal peers
872 amount = round (.5 * num_peers);
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
875 for (i = 0 ; i < amount ; i++)
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
878 GNUNET_i2s (&rps_peer_ids[i]));
880 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
888 seed_peers_big (void *cls)
890 struct RPSPeer *peer = cls;
891 unsigned int seed_msg_size;
892 uint32_t num_peers_max;
896 seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
897 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - seed_msg_size) /
898 sizeof (struct GNUNET_PeerIdentity);
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Peers that fit in one seed msg; %u\n",
902 amount = num_peers_max + (0.5 * num_peers_max);
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Seeding many (%u) peers:\n",
906 struct GNUNET_PeerIdentity ids_to_seed[amount];
907 for (i = 0; i < amount; i++)
909 ids_to_seed[i] = *peer->peer_id;
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
912 GNUNET_i2s (&ids_to_seed[i]));
915 GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
919 * Get the id of peer i.
922 info_cb (void *cb_cls,
923 struct GNUNET_TESTBED_Operation *op,
924 const struct GNUNET_TESTBED_PeerInformation *pinfo,
927 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
930 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
935 if (NULL == pinfo || NULL != emsg)
937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
938 GNUNET_TESTBED_operation_done (entry->op);
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 GNUNET_i2s (pinfo->result.id));
947 rps_peer_ids[entry->index] = *(pinfo->result.id);
948 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
950 GNUNET_assert (GNUNET_OK ==
951 GNUNET_CONTAINER_multipeermap_put (peer_map,
952 &rps_peer_ids[entry->index],
953 &rps_peers[entry->index],
954 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
955 tofile ("/tmp/rps/peer_ids",
958 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
960 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
961 GNUNET_TESTBED_operation_done (entry->op);
967 * Callback to be called when RPS service connect operation is completed
969 * @param cls the callback closure from functions generating an operation
970 * @param op the operation that has been finished
971 * @param ca_result the RPS service handle returned from rps_connect_adapter
972 * @param emsg error message in case the operation has failed; will be NULL if
973 * operation has executed successfully.
976 rps_connect_complete_cb (void *cls,
977 struct GNUNET_TESTBED_Operation *op,
981 struct RPSPeer *rps_peer = cls;
982 struct GNUNET_RPS_Handle *rps = ca_result;
984 GNUNET_assert (NULL != ca_result);
986 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
991 rps_peer->rps_handle = rps;
992 rps_peer->online = GNUNET_YES;
995 GNUNET_assert (op == rps_peer->op);
998 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
999 "Failed to connect to RPS service: %s\n",
1002 GNUNET_SCHEDULER_shutdown ();
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
1008 cur_test_run.main_test (rps_peer);
1013 * Adapter function called to establish a connection to
1016 * @param cls closure
1017 * @param cfg configuration of the peer to connect to; will be available until
1018 * GNUNET_TESTBED_operation_done() is called on the operation returned
1019 * from GNUNET_TESTBED_service_connect()
1020 * @return service handle to return in 'op_result', NULL on error
1023 rps_connect_adapter (void *cls,
1024 const struct GNUNET_CONFIGURATION_Handle *cfg)
1026 struct GNUNET_RPS_Handle *h;
1028 h = GNUNET_RPS_connect (cfg);
1029 GNUNET_assert (NULL != h);
1031 if (NULL != cur_test_run.pre_test)
1032 cur_test_run.pre_test (cls, h);
1033 GNUNET_assert (NULL != h);
1039 * Called to open a connection to the peer's statistics
1041 * @param cls peer context
1042 * @param cfg configuration of the peer to connect to; will be available until
1043 * GNUNET_TESTBED_operation_done() is called on the operation returned
1044 * from GNUNET_TESTBED_service_connect()
1045 * @return service handle to return in 'op_result', NULL on error
1048 stat_connect_adapter (void *cls,
1049 const struct GNUNET_CONFIGURATION_Handle *cfg)
1051 struct RPSPeer *peer = cls;
1053 peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
1054 return peer->stats_h;
1058 * Called to disconnect from peer's statistics service
1060 * @param cls peer context
1061 * @param op_result service handle returned from the connect adapter
1064 stat_disconnect_adapter (void *cls, void *op_result)
1066 struct RPSPeer *peer = cls;
1068 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1069 // (peer->stats_h, "core", "# peers connected",
1070 // stat_iterator, peer));
1071 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1072 // (peer->stats_h, "nse", "# peers connected",
1073 // stat_iterator, peer));
1074 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
1075 peer->stats_h = NULL;
1079 * Called after successfully opening a connection to a peer's statistics
1080 * service; we register statistics monitoring for CORE and NSE here.
1082 * @param cls the callback closure from functions generating an operation
1083 * @param op the operation that has been finished
1084 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
1085 * @param emsg error message in case the operation has failed; will be NULL if
1086 * operation has executed successfully.
1089 stat_complete_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1090 void *ca_result, const char *emsg )
1092 //struct GNUNET_STATISTICS_Handle *sh = ca_result;
1093 //struct RPSPeer *peer = (struct RPSPeer *) cls;
1103 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1104 // (sh, "core", "# peers connected",
1105 // stat_iterator, peer));
1106 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1107 // (sh, "nse", "# peers connected",
1108 // stat_iterator, peer));
1113 * Adapter function called to destroy connection to
1116 * @param cls closure
1117 * @param op_result service handle returned from the connect adapter
1120 rps_disconnect_adapter (void *cls,
1123 struct RPSPeer *peer = cls;
1124 struct GNUNET_RPS_Handle *h = op_result;
1126 if (NULL != peer->rps_srh)
1128 GNUNET_RPS_stream_cancel (peer->rps_srh);
1129 peer->rps_srh = NULL;
1131 GNUNET_assert (NULL != peer);
1132 GNUNET_RPS_disconnect (h);
1133 peer->rps_handle = NULL;
1137 /***********************************************************************
1138 * Definition of tests
1139 ***********************************************************************/
1141 // TODO check whether tests can be stopped earlier
1143 default_eval_cb (void)
1155 * Initialise given RPSPeer
1157 static void default_init_peer (struct RPSPeer *rps_peer)
1159 rps_peer->num_ids_to_request = 1;
1163 * Callback to call on receipt of a reply
1165 * @param cls closure
1166 * @param n number of peers
1167 * @param recv_peers the received peers
1170 default_reply_handle (void *cls,
1172 const struct GNUNET_PeerIdentity *recv_peers)
1174 struct RPSPeer *rps_peer;
1175 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1178 rps_peer = pending_rep->rps_peer;
1179 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1180 rps_peer->pending_rep_tail,
1182 rps_peer->num_pending_reps--;
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "[%s] got %" PRIu64 " peers:\n",
1185 GNUNET_i2s (rps_peer->peer_id),
1188 for (i = 0; i < n; i++)
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193 GNUNET_i2s (&recv_peers[i]));
1195 rps_peer->num_recv_ids++;
1198 if (0 == evaluate () && HAVE_QUICK_QUIT == cur_test_run.have_quick_quit)
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test succeeded before timeout\n");
1201 GNUNET_assert (NULL != post_test_task);
1202 GNUNET_SCHEDULER_cancel (post_test_task);
1203 post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
1204 GNUNET_assert (NULL!= post_test_task);
1209 * Request random peers.
1212 request_peers (void *cls)
1214 struct PendingRequest *pending_req = cls;
1215 struct RPSPeer *rps_peer;
1216 struct PendingReply *pending_rep;
1218 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1220 rps_peer = pending_req->rps_peer;
1221 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1222 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1223 rps_peer->pending_req_tail,
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 "Requesting one peer\n");
1227 pending_rep = GNUNET_new (struct PendingReply);
1228 pending_rep->rps_peer = rps_peer;
1229 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1231 cur_test_run.reply_handle,
1233 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1234 rps_peer->pending_rep_tail,
1236 rps_peer->num_pending_reps++;
1237 rps_peer->num_pending_reqs--;
1241 cancel_pending_req (struct PendingRequest *pending_req)
1243 struct RPSPeer *rps_peer;
1245 rps_peer = pending_req->rps_peer;
1246 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1247 rps_peer->pending_req_tail,
1249 rps_peer->num_pending_reqs--;
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1251 "Cancelling pending request\n");
1252 GNUNET_SCHEDULER_cancel (pending_req->request_task);
1253 GNUNET_free (pending_req);
1257 cancel_request (struct PendingReply *pending_rep)
1259 struct RPSPeer *rps_peer;
1261 rps_peer = pending_rep->rps_peer;
1262 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1263 rps_peer->pending_rep_tail,
1265 rps_peer->num_pending_reps--;
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 "Cancelling request\n");
1268 GNUNET_RPS_request_cancel (pending_rep->req_handle);
1269 GNUNET_free (pending_rep);
1276 cancel_request_cb (void *cls)
1278 struct RPSPeer *rps_peer = cls;
1279 struct PendingReply *pending_rep;
1281 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1283 pending_rep = rps_peer->pending_rep_head;
1284 GNUNET_assert (1 <= rps_peer->num_pending_reps);
1285 cancel_request (pending_rep);
1290 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1291 * issued, nor replied
1294 schedule_missing_requests (struct RPSPeer *rps_peer)
1297 struct PendingRequest *pending_req;
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 "Scheduling %u - %u missing requests\n",
1301 rps_peer->num_ids_to_request,
1302 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1303 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1304 rps_peer->num_ids_to_request);
1305 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1306 i < rps_peer->num_ids_to_request; i++)
1308 pending_req = GNUNET_new (struct PendingRequest);
1309 pending_req->rps_peer = rps_peer;
1310 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1311 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1312 cur_test_run.request_interval * i),
1315 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1316 rps_peer->pending_req_tail,
1318 rps_peer->num_pending_reqs++;
1323 cancel_pending_req_rep (struct RPSPeer *rps_peer)
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 "Cancelling all (pending) requests.\n");
1327 while (NULL != rps_peer->pending_req_head)
1328 cancel_pending_req (rps_peer->pending_req_head);
1329 GNUNET_assert (0 == rps_peer->num_pending_reqs);
1330 while (NULL != rps_peer->pending_rep_head)
1331 cancel_request (rps_peer->pending_rep_head);
1332 GNUNET_assert (0 == rps_peer->num_pending_reps);
1335 /***********************************
1337 ***********************************/
1340 * Initialise only non-mal RPSPeers
1342 static void mal_init_peer (struct RPSPeer *rps_peer)
1344 if (rps_peer->index >= round (portion * num_peers))
1345 rps_peer->num_ids_to_request = 1;
1350 * @brief Set peers to (non-)malicious before execution
1352 * Of signature #PreTest
1354 * @param rps_peer the peer to set (non-) malicious
1355 * @param h the handle to the service
1358 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1360 #if ENABLE_MALICIOUS
1361 uint32_t num_mal_peers;
1363 GNUNET_assert ( (1 >= portion) &&
1365 num_mal_peers = round (portion * num_peers);
1367 if (rps_peer->index < num_mal_peers)
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
1372 GNUNET_i2s (rps_peer->peer_id),
1375 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1376 rps_peer_ids, target_peer);
1378 #endif /* ENABLE_MALICIOUS */
1382 mal_cb (struct RPSPeer *rps_peer)
1384 uint32_t num_mal_peers;
1386 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1391 #if ENABLE_MALICIOUS
1392 GNUNET_assert ( (1 >= portion) &&
1394 num_mal_peers = round (portion * num_peers);
1396 if (rps_peer->index >= num_mal_peers)
1397 { /* It's useless to ask a malicious peer about a random sample -
1398 it's not sampling */
1399 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1400 seed_peers, rps_peer);
1401 schedule_missing_requests (rps_peer);
1403 #endif /* ENABLE_MALICIOUS */
1407 /***********************************
1409 ***********************************/
1411 single_req_cb (struct RPSPeer *rps_peer)
1413 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1418 schedule_missing_requests (rps_peer);
1421 /***********************************
1423 ***********************************/
1425 delay_req_cb (struct RPSPeer *rps_peer)
1427 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1432 schedule_missing_requests (rps_peer);
1435 /***********************************
1437 ***********************************/
1439 seed_cb (struct RPSPeer *rps_peer)
1441 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1446 GNUNET_SCHEDULER_add_delayed (
1447 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
1448 seed_peers, rps_peer);
1451 /***********************************
1453 ***********************************/
1455 seed_big_cb (struct RPSPeer *rps_peer)
1457 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1462 // TODO test seeding > GNUNET_MAX_MESSAGE_SIZE peers
1463 GNUNET_SCHEDULER_add_delayed (
1464 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1465 seed_peers_big, rps_peer);
1468 /***********************************
1470 ***********************************/
1472 single_peer_seed_cb (struct RPSPeer *rps_peer)
1478 /***********************************
1480 ***********************************/
1482 seed_req_cb (struct RPSPeer *rps_peer)
1484 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1489 GNUNET_SCHEDULER_add_delayed (
1490 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1491 seed_peers, rps_peer);
1492 schedule_missing_requests (rps_peer);
1495 //TODO start big mal
1497 /***********************************
1499 ***********************************/
1501 req_cancel_cb (struct RPSPeer *rps_peer)
1503 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1508 schedule_missing_requests (rps_peer);
1509 GNUNET_SCHEDULER_add_delayed (
1510 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1511 (cur_test_run.request_interval + 1)),
1512 cancel_request_cb, rps_peer);
1515 /***********************************
1517 ***********************************/
1523 * @brief Starts churn
1525 * Has signature of #MainTest
1527 * This is not implemented too nicely as this is called for each peer, but we
1528 * only need to call it once. (Yes we check that we only schedule the task
1531 * @param rps_peer The peer it's called for
1534 churn_test_cb (struct RPSPeer *rps_peer)
1536 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1542 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 "Starting churn task\n");
1546 churn_task = GNUNET_SCHEDULER_add_delayed (
1547 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Not starting churn task\n");
1555 schedule_missing_requests (rps_peer);
1558 /***********************************
1560 ***********************************/
1563 got_stream_peer_cb (void *cls,
1565 const struct GNUNET_PeerIdentity *peers)
1567 const struct RPSPeer *rps_peer = cls;
1569 for (uint64_t i = 0; i < num_peers; i++)
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 "Peer %" PRIu32 " received [%s] from stream.\n",
1574 GNUNET_i2s (&peers[i]));
1575 if (0 != rps_peer->index &&
1576 0 == memcmp (&peers[i],
1577 &rps_peers[0].peer_id,
1578 sizeof (struct GNUNET_PeerIdentity)))
1580 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Received a peer id outside sub\n");
1583 else if (0 == rps_peer->index &&
1584 0 != memcmp (&peers[i],
1585 &rps_peers[0].peer_id,
1586 sizeof (struct GNUNET_PeerIdentity)))
1588 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Received a peer id outside sub (lonely)\n");
1596 sub_post (struct RPSPeer *rps_peer)
1598 if (0 != rps_peer->index) GNUNET_RPS_sub_stop (rps_peer->rps_handle, "test");
1599 else GNUNET_RPS_sub_stop (rps_peer->rps_handle, "lonely");
1604 sub_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1608 if (0 != rps_peer->index) GNUNET_RPS_sub_start (h, "test");
1609 else GNUNET_RPS_sub_start (h, "lonely"); /* have a group of one */
1610 rps_peer->rps_srh = GNUNET_RPS_stream_request (h,
1611 &got_stream_peer_cb,
1615 /***********************************
1617 ***********************************/
1620 * Callback to be called when RPS service is started or stopped at peers
1623 * @param op the operation handle
1624 * @param emsg NULL on success; otherwise an error description
1627 churn_cb (void *cls,
1628 struct GNUNET_TESTBED_Operation *op,
1633 struct OpListEntry *entry = cls;
1635 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1640 GNUNET_TESTBED_operation_done (entry->op);
1643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1644 GNUNET_SCHEDULER_shutdown ();
1647 GNUNET_assert (0 != entry->delta);
1649 num_peers_online += entry->delta;
1651 if (PEER_GO_OFFLINE == entry->delta)
1652 { /* Peer hopefully just went offline */
1653 if (GNUNET_YES != rps_peers[entry->index].online)
1655 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1656 "peer %s was expected to go offline but is still marked as online\n",
1657 GNUNET_i2s (rps_peers[entry->index].peer_id));
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663 "peer %s probably went offline as expected\n",
1664 GNUNET_i2s (rps_peers[entry->index].peer_id));
1666 rps_peers[entry->index].online = GNUNET_NO;
1669 else if (PEER_GO_ONLINE < entry->delta)
1670 { /* Peer hopefully just went online */
1671 if (GNUNET_NO != rps_peers[entry->index].online)
1673 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1674 "peer %s was expected to go online but is still marked as offline\n",
1675 GNUNET_i2s (rps_peers[entry->index].peer_id));
1680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1681 "peer %s probably went online as expected\n",
1682 GNUNET_i2s (rps_peers[entry->index].peer_id));
1683 if (NULL != cur_test_run.pre_test)
1685 cur_test_run.pre_test (&rps_peers[entry->index],
1686 rps_peers[entry->index].rps_handle);
1687 schedule_missing_requests (&rps_peers[entry->index]);
1690 rps_peers[entry->index].online = GNUNET_YES;
1694 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1695 "Invalid value for delta: %i\n", entry->delta);
1699 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1700 rps_peers[entry->index].entry_op_manage = NULL;
1701 GNUNET_free (entry);
1702 //if (num_peers_in_round[current_round] == peers_running)
1707 * @brief Set the rps-service up or down for a specific peer
1709 * @param i index of action
1710 * @param j index of peer
1711 * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1712 * @param prob_go_on_off the probability of the action
1715 manage_service_wrapper (unsigned int i, unsigned int j,
1716 enum PEER_ONLINE_DELTA delta,
1717 double prob_go_on_off)
1719 struct OpListEntry *entry = NULL;
1722 /* make sure that management operation is not already scheduled */
1723 if (NULL != rps_peers[j].entry_op_manage)
1728 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1731 "%u. selected peer (%u: %s) is %s.\n",
1734 GNUNET_i2s (rps_peers[j].peer_id),
1735 (PEER_GO_ONLINE == delta) ? "online" : "offline");
1736 if (prob < prob_go_on_off * UINT32_MAX)
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1740 GNUNET_i2s (rps_peers[j].peer_id),
1741 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1743 if (PEER_GO_OFFLINE == delta)
1744 cancel_pending_req_rep (&rps_peers[j]);
1745 entry = make_oplist_entry ();
1746 entry->delta = delta;
1748 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1753 (PEER_GO_OFFLINE == delta) ? 0 : 1);
1754 rps_peers[j].entry_op_manage = entry;
1765 double portion_online;
1766 unsigned int *permut;
1767 double prob_go_offline;
1768 double portion_go_online;
1769 double portion_go_offline;
1771 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "Churn function executing\n");
1778 churn_task = NULL; /* Should be invalid by now */
1780 /* Compute the probability for an online peer to go offline
1782 portion_online = num_peers_online * 1.0 / num_peers;
1783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1784 "Portion online: %f\n",
1786 portion_go_online = ((1 - portion_online) * .5 * .66);
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1788 "Portion that should go online: %f\n",
1790 portion_go_offline = (portion_online + portion_go_online) - .75;
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1792 "Portion that probably goes offline: %f\n",
1793 portion_go_offline);
1794 prob_go_offline = portion_go_offline / (portion_online * .5);
1795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1796 "Probability of a selected online peer to go offline: %f\n",
1799 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1800 (unsigned int) num_peers);
1802 /* Go over 50% randomly chosen peers */
1803 for (i = 0; i < .5 * num_peers; i++)
1807 /* If online, shut down with certain probability */
1808 if (GNUNET_YES == rps_peers[j].online)
1810 manage_service_wrapper (i, j, -1, prob_go_offline);
1813 /* If offline, restart with certain probability */
1814 else if (GNUNET_NO == rps_peers[j].online)
1816 manage_service_wrapper (i, j, 1, 0.66);
1820 GNUNET_free (permut);
1822 churn_task = GNUNET_SCHEDULER_add_delayed (
1823 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1830 * Initialise given RPSPeer
1832 static void profiler_init_peer (struct RPSPeer *rps_peer)
1834 if (num_peers - 1 == rps_peer->index)
1835 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1840 * Callback to call on receipt of a reply
1842 * @param cls closure
1843 * @param n number of peers
1844 * @param recv_peers the received peers
1847 profiler_reply_handle (void *cls,
1849 const struct GNUNET_PeerIdentity *recv_peers)
1851 struct RPSPeer *rps_peer;
1852 struct RPSPeer *rcv_rps_peer;
1856 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1858 rps_peer = pending_rep->rps_peer;
1859 file_name = "/tmp/rps/received_ids";
1860 file_name_dh = "/tmp/rps/diehard_input";
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 "[%s] got %" PRIu64 " peers:\n",
1863 GNUNET_i2s (rps_peer->peer_id),
1865 for (i = 0; i < n; i++)
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1870 GNUNET_i2s (&recv_peers[i]));
1873 GNUNET_i2s_full (&recv_peers[i]));
1874 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1875 GNUNET_assert (NULL != rcv_rps_peer);
1876 tofile (file_name_dh,
1878 (uint32_t) rcv_rps_peer->index);
1880 default_reply_handle (cls, n, recv_peers);
1885 profiler_cb (struct RPSPeer *rps_peer)
1887 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1893 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1896 "Starting churn task\n");
1897 churn_task = GNUNET_SCHEDULER_add_delayed (
1898 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903 "Not starting churn task\n");
1906 /* Only request peer ids at one peer.
1907 * (It's the before-last because last one is target of the focussed attack.)
1909 if (eval_peer == rps_peer)
1910 schedule_missing_requests (rps_peer);
1914 * Function called from #profiler_eval with a filename.
1916 * @param cls closure
1917 * @param filename complete filename (absolute path)
1918 * @return #GNUNET_OK to continue to iterate,
1919 * #GNUNET_NO to stop iteration with no error,
1920 * #GNUNET_SYSERR to abort iteration with error!
1923 file_name_cb (void *cls, const char *filename)
1927 if (NULL != strstr (filename, "sampler_el"))
1929 struct RPS_SamplerElement *s_elem;
1930 struct GNUNET_CRYPTO_AuthKey auth_key;
1931 const char *key_char;
1934 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1935 tofile (filename, "--------------------------\n");
1937 auth_key = string_to_auth_key (key_char);
1938 s_elem = RPS_sampler_elem_create ();
1939 RPS_sampler_elem_set (s_elem, auth_key);
1941 for (i = 0; i < num_peers; i++)
1943 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1945 RPS_sampler_elem_destroy (s_elem);
1951 * This is run after the test finished.
1953 * Compute all perfect samples.
1956 profiler_eval (void)
1958 /* Compute perfect sample for each sampler element */
1959 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1961 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1969 * @brief is b in view of a?
1976 static int is_in_view (uint32_t a, uint32_t b)
1979 for (i = 0; i < rps_peers[a].cur_view_count; i++)
1981 if (0 == memcmp (rps_peers[b].peer_id,
1982 &rps_peers[a].cur_view[i],
1983 sizeof (struct GNUNET_PeerIdentity)))
1991 static uint32_t get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
1995 for (i = 0; i < num_peers; i++)
1997 if (0 == memcmp (pid,
1998 rps_peers[i].peer_id,
1999 sizeof (struct GNUNET_PeerIdentity)))
2004 //return 0; /* Should not happen - make compiler happy */
2005 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2006 "No known _PeerIdentity %s!\n",
2007 GNUNET_i2s_full (pid));
2012 * @brief Counts number of peers in view of a that have b in their view
2019 static uint32_t count_containing_views (uint32_t a, uint32_t b)
2025 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2027 peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
2028 if (GNUNET_YES == is_in_view (peer_idx, b))
2037 * @brief Computes the probability for each other peer to be selected by the
2038 * sampling process based on the views of all peers
2040 * @param peer_idx index of the peer that is about to sample
2042 static void compute_probabilities (uint32_t peer_idx)
2044 //double probs[num_peers] = { 0 };
2045 double probs[num_peers];
2046 size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof (char);
2047 char *probs_as_str = GNUNET_malloc (probs_as_str_size);
2048 char *probs_as_str_cpy;
2053 uint32_t cont_views;
2054 uint32_t number_of_being_in_pull_events;
2056 uint32_t count_non_zero_prob = 0;
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2059 "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
2060 /* Firstly without knowledge of old views */
2061 for (i = 0; i < num_peers; i++)
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2064 "\tfor peer %" PRIu32 ":\n", i);
2065 view_size = rps_peers[i].cur_view_count;
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2067 "\t\tview_size: %" PRIu32 "\n", view_size);
2068 /* For peer i the probability of being sampled is
2069 * evenly distributed among all possibly observed peers. */
2070 /* We could have observed a peer in three cases:
2071 * 1. peer sent a push
2072 * 2. peer was contained in a pull reply
2073 * 3. peer was in history (sampler) - ignored for now */
2074 /* 1. Probability of having received a push from peer i */
2075 if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
2076 (1 <= (0.45 * view_size)))
2078 prob_push = 1.0 * binom (0.45 * view_size, 1)
2080 binom (view_size, 0.45 * view_size);
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2082 "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
2086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2087 "\t\tposs choices from view: %" PRIu32 ", containing i: %" PRIu32 "\n",
2088 binom (view_size, 0.45 * view_size),
2089 binom (0.45 * view_size, 1));
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2093 "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
2097 /* 2. Probability of peer i being contained in pulls */
2098 view_size = rps_peers[peer_idx].cur_view_count;
2099 cont_views = count_containing_views (peer_idx, i);
2100 number_of_being_in_pull_events =
2101 (binom (view_size, 0.45 * view_size) -
2102 binom (view_size - cont_views, 0.45 * view_size));
2103 if (0 != number_of_being_in_pull_events)
2105 prob_pull = number_of_being_in_pull_events
2107 (1.0 * binom (view_size, 0.45 * view_size));
2112 probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
2113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2114 "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
2115 " peers in its view who know %" PRIu32 " prob: %f\n",
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2122 "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
2123 binom (view_size, 0.45 * view_size));
2124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2125 "\t\tnumber of possible pull combinations without %" PRIu32
2128 binom (view_size - cont_views, 0.45 * view_size));
2129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2130 "\t\tnumber of possible pull combinations with %" PRIu32
2133 number_of_being_in_pull_events);
2135 if (0 != probs[i]) count_non_zero_prob++;
2138 if (0 != count_non_zero_prob)
2140 for (i = 0; i < num_peers; i++)
2142 probs[i] = probs[i] * (1.0 / count_non_zero_prob);
2145 for (i = 0; i < num_peers; i++)
2151 for (i = 0; i < num_peers; i++)
2153 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2154 tmp = GNUNET_snprintf (probs_as_str,
2156 "%s %7.6f", probs_as_str_cpy, probs[i]);
2157 GNUNET_free (probs_as_str_cpy);
2158 GNUNET_assert (0 <= tmp);
2161 to_file_w_len (rps_peers[peer_idx].file_name_probs,
2164 GNUNET_free (probs_as_str);
2168 * @brief This counts the number of peers in which views a given peer occurs.
2170 * It also stores this value in the rps peer.
2172 * @param peer_idx the index of the peer to count the representation
2174 * @return the number of occurrences
2176 static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
2181 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2183 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2185 if (0 == memcmp (rps_peers[peer_idx].peer_id,
2186 &rps_peers[i].cur_view[j],
2187 sizeof (struct GNUNET_PeerIdentity)))
2194 rps_peers[peer_idx].count_in_views = count;
2198 static uint32_t cumulated_view_sizes ()
2203 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2205 view_sizes += rps_peers[i].cur_view_count;
2210 static void count_peer_in_views (uint32_t *count_peers)
2214 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2216 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2218 if (0 == memcmp (rps_peers[i].peer_id,
2219 &rps_peers[i].cur_view[j],
2220 sizeof (struct GNUNET_PeerIdentity)))
2228 void compute_diversity ()
2231 /* ith entry represents the numer of occurrences in other peer's views */
2232 uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
2233 uint32_t views_total_size;
2235 /* deviation from expected number of peers */
2236 double *deviation = GNUNET_new_array (num_peers, double);
2238 views_total_size = 0;
2241 /* For each peer count its representation in other peer's views*/
2242 for (i = 0; i < num_peers; i++) /* Peer to count */
2244 views_total_size += rps_peers[i].cur_view_count;
2245 count_peer_in_views (count_peers);
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2247 "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32"\n",
2249 GNUNET_i2s (rps_peers[i].peer_id),
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2254 "size of all views combined: %" PRIu32 "\n",
2256 expected = ((double) 1/num_peers) * views_total_size;
2257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2258 "Expected number of occurrences of each peer in all views: %f\n",
2260 for (i = 0; i < num_peers; i++) /* Peer to count */
2262 deviation[i] = expected - count_peers[i];
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 "Deviation from expectation: %f\n", deviation[i]);
2266 GNUNET_free (count_peers);
2267 GNUNET_free (deviation);
2270 void print_view_sizes()
2274 for (i = 0; i < num_peers; i++) /* Peer to count */
2276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2277 "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
2279 GNUNET_i2s (rps_peers[i].peer_id),
2280 rps_peers[i].cur_view_count);
2284 void all_views_updated_cb()
2286 compute_diversity();
2290 void view_update_cb (void *cls,
2292 const struct GNUNET_PeerIdentity *peers)
2294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2295 "View was updated (%" PRIu64 ")\n", view_size);
2296 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
2297 to_file ("/tmp/rps/view_sizes.txt",
2298 "%" PRIu64 " %" PRIu32 "",
2301 for (int i = 0; i < view_size; i++)
2303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2304 "\t%s\n", GNUNET_i2s (&peers[i]));
2306 GNUNET_array_grow (rps_peer->cur_view,
2307 rps_peer->cur_view_count,
2309 //*rps_peer->cur_view = *peers;
2310 GNUNET_memcpy (rps_peer->cur_view,
2312 view_size * sizeof (struct GNUNET_PeerIdentity));
2313 to_file ("/tmp/rps/count_in_views.txt",
2314 "%" PRIu64 " %" PRIu32 "",
2316 count_peer_in_views_2 (rps_peer->index));
2317 cumulated_view_sizes();
2320 to_file ("/tmp/rps/repr.txt",
2321 "%" PRIu64 /* index */
2322 " %" PRIu32 /* occurrence in views */
2323 " %" PRIu32 /* view sizes */
2324 " %f" /* fraction of repr in views */
2325 " %f" /* average view size */
2326 " %f" /* prob of occurrence in view slot */
2327 " %f" "", /* exp frac of repr in views */
2329 count_peer_in_views_2 (rps_peer->index),
2331 count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
2332 view_sizes / (view_size * 1.0), /* average view size */
2333 1.0 /view_size, /* prob of occurrence in view slot */
2334 (1.0/view_size) * (view_sizes/view_size) /* expected fraction of repr in views */
2337 compute_probabilities (rps_peer->index);
2338 all_views_updated_cb();
2342 pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
2344 rps_peer->file_name_probs =
2345 store_prefix_file_name (rps_peer->peer_id, "probs");
2346 GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
2349 void write_final_stats (void){
2352 for (i = 0; i < num_peers; i++)
2354 to_file ("/tmp/rps/final_stats.dat",
2355 "%" PRIu32 " " /* index */
2357 PRIu64 " %" /* rounds */
2358 PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" /* blocking */
2359 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* issued */
2360 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* sent */
2361 PRIu64 " %" PRIu64 " %" PRIu64 /* recv */,
2363 GNUNET_i2s (rps_peers[i].peer_id),
2364 rps_peers[i].num_rounds,
2365 rps_peers[i].num_blocks,
2366 rps_peers[i].num_blocks_many_push,
2367 rps_peers[i].num_blocks_no_push,
2368 rps_peers[i].num_blocks_no_pull,
2369 rps_peers[i].num_blocks_many_push_no_pull,
2370 rps_peers[i].num_blocks_no_push_no_pull,
2371 rps_peers[i].num_issued_push,
2372 rps_peers[i].num_issued_pull_req,
2373 rps_peers[i].num_issued_pull_rep,
2374 rps_peers[i].num_sent_push,
2375 rps_peers[i].num_sent_pull_req,
2376 rps_peers[i].num_sent_pull_rep,
2377 rps_peers[i].num_recv_push,
2378 rps_peers[i].num_recv_pull_req,
2379 rps_peers[i].num_recv_pull_rep);
2384 * Continuation called by #GNUNET_STATISTICS_get() functions.
2386 * Remembers that this specific statistics value was received for this peer.
2387 * Checks whether all peers received their statistics yet.
2388 * Issues the shutdown.
2390 * @param cls closure
2391 * @param success #GNUNET_OK if statistics were
2392 * successfully obtained, #GNUNET_SYSERR if not.
2395 post_test_shutdown_ready_cb (void *cls,
2398 struct STATcls *stat_cls = (struct STATcls *) cls;
2399 struct RPSPeer *rps_peer = stat_cls->rps_peer;
2400 if (GNUNET_OK == success)
2402 /* set flag that we we got the value */
2403 rps_peer->stat_collected_flags |= stat_cls->stat_type;
2405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2406 "Peer %u did not receive statistics value\n",
2408 GNUNET_free (stat_cls);
2412 if (NULL != rps_peer->stat_op &&
2413 GNUNET_YES == check_statistics_collect_completed_single_peer (rps_peer))
2415 GNUNET_TESTBED_operation_done (rps_peer->stat_op);
2418 write_final_stats ();
2419 if (GNUNET_YES == check_statistics_collect_completed())
2421 //write_final_stats ();
2422 GNUNET_free (stat_cls);
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 GNUNET_SCHEDULER_shutdown ();
2427 GNUNET_free (stat_cls);
2432 * @brief Converts string representation to the corresponding #STAT_TYPE enum.
2434 * @param stat_str string representation of statistics specifier
2436 * @return corresponding enum
2438 enum STAT_TYPE stat_str_2_type (const char *stat_str)
2440 if (0 == strncmp ("# rounds blocked - no pull replies", stat_str, strlen ("# rounds blocked - no pull replies")))
2442 return STAT_TYPE_BLOCKS_NO_PULL;
2444 else if (0 == strncmp ("# rounds blocked - too many pushes, no pull replies", stat_str, strlen ("# rounds blocked - too many pushes, no pull replies")))
2446 return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
2448 else if (0 == strncmp ("# rounds blocked - too many pushes", stat_str, strlen ("# rounds blocked - too many pushes")))
2450 return STAT_TYPE_BLOCKS_MANY_PUSH;
2452 else if (0 == strncmp ("# rounds blocked - no pushes, no pull replies", stat_str, strlen ("# rounds blocked - no pushes, no pull replies")))
2454 return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
2456 else if (0 == strncmp ("# rounds blocked - no pushes", stat_str, strlen ("# rounds blocked - no pushes")))
2458 return STAT_TYPE_BLOCKS_NO_PUSH;
2460 else if (0 == strncmp ("# rounds blocked", stat_str, strlen ("# rounds blocked")))
2462 return STAT_TYPE_BLOCKS;
2464 else if (0 == strncmp ("# rounds", stat_str, strlen ("# rounds")))
2466 return STAT_TYPE_ROUNDS;
2468 else if (0 == strncmp ("# push send issued", stat_str, strlen ("# push send issued")))
2470 return STAT_TYPE_ISSUED_PUSH_SEND;
2472 else if (0 == strncmp ("# pull request send issued", stat_str, strlen ("# pull request send issued")))
2474 return STAT_TYPE_ISSUED_PULL_REQ;
2476 else if (0 == strncmp ("# pull reply send issued", stat_str, strlen ("# pull reply send issued")))
2478 return STAT_TYPE_ISSUED_PULL_REP;
2480 else if (0 == strncmp ("# pushes sent", stat_str, strlen ("# pushes sent")))
2482 return STAT_TYPE_SENT_PUSH_SEND;
2484 else if (0 == strncmp ("# pull requests sent", stat_str, strlen ("# pull requests sent")))
2486 return STAT_TYPE_SENT_PULL_REQ;
2488 else if (0 == strncmp ("# pull replys sent", stat_str, strlen ("# pull replys sent")))
2490 return STAT_TYPE_SENT_PULL_REP;
2492 else if (0 == strncmp ("# push message received", stat_str, strlen ("# push message received")))
2494 return STAT_TYPE_RECV_PUSH_SEND;
2496 else if (0 == strncmp ("# pull request message received", stat_str, strlen ("# pull request message received")))
2498 return STAT_TYPE_RECV_PULL_REQ;
2500 else if (0 == strncmp ("# pull reply messages received", stat_str, strlen ("# pull reply messages received")))
2502 return STAT_TYPE_RECV_PULL_REP;
2504 return STAT_TYPE_MAX;
2509 * @brief Converts #STAT_TYPE enum to the equivalent string representation that
2510 * is stored with the statistics service.
2512 * @param stat_type #STAT_TYPE enum
2514 * @return string representation that matches statistics value
2516 char* stat_type_2_str (enum STAT_TYPE stat_type)
2520 case STAT_TYPE_ROUNDS:
2522 case STAT_TYPE_BLOCKS:
2523 return "# rounds blocked";
2524 case STAT_TYPE_BLOCKS_MANY_PUSH:
2525 return "# rounds blocked - too many pushes";
2526 case STAT_TYPE_BLOCKS_NO_PUSH:
2527 return "# rounds blocked - no pushes";
2528 case STAT_TYPE_BLOCKS_NO_PULL:
2529 return "# rounds blocked - no pull replies";
2530 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2531 return "# rounds blocked - too many pushes, no pull replies";
2532 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2533 return "# rounds blocked - no pushes, no pull replies";
2534 case STAT_TYPE_ISSUED_PUSH_SEND:
2535 return "# push send issued";
2536 case STAT_TYPE_ISSUED_PULL_REQ:
2537 return "# pull request send issued";
2538 case STAT_TYPE_ISSUED_PULL_REP:
2539 return "# pull reply send issued";
2540 case STAT_TYPE_SENT_PUSH_SEND:
2541 return "# pushes sent";
2542 case STAT_TYPE_SENT_PULL_REQ:
2543 return "# pull requests sent";
2544 case STAT_TYPE_SENT_PULL_REP:
2545 return "# pull replys sent";
2546 case STAT_TYPE_RECV_PUSH_SEND:
2547 return "# push message received";
2548 case STAT_TYPE_RECV_PULL_REQ:
2549 return "# pull request message received";
2550 case STAT_TYPE_RECV_PULL_REP:
2551 return "# pull reply messages received";
2560 * Callback function to process statistic values.
2562 * @param cls closure
2563 * @param subsystem name of subsystem that created the statistic
2564 * @param name the name of the datum
2565 * @param value the current value
2566 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
2567 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
2570 stat_iterator (void *cls,
2571 const char *subsystem,
2577 (void) is_persistent;
2578 const struct STATcls *stat_cls = (const struct STATcls *) cls;
2579 struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got stat value: %s - %" PRIu64 "\n",
2581 //stat_type_2_str (stat_cls->stat_type),
2584 to_file (rps_peer->file_name_stats,
2585 "%s: %" PRIu64 "\n",
2588 switch (stat_str_2_type (name))
2590 case STAT_TYPE_ROUNDS:
2591 rps_peer->num_rounds = value;
2593 case STAT_TYPE_BLOCKS:
2594 rps_peer->num_blocks = value;
2596 case STAT_TYPE_BLOCKS_MANY_PUSH:
2597 rps_peer->num_blocks_many_push = value;
2599 case STAT_TYPE_BLOCKS_NO_PUSH:
2600 rps_peer->num_blocks_no_push = value;
2602 case STAT_TYPE_BLOCKS_NO_PULL:
2603 rps_peer->num_blocks_no_pull = value;
2605 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2606 rps_peer->num_blocks_many_push_no_pull = value;
2608 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2609 rps_peer->num_blocks_no_push_no_pull = value;
2611 case STAT_TYPE_ISSUED_PUSH_SEND:
2612 rps_peer->num_issued_push = value;
2614 case STAT_TYPE_ISSUED_PULL_REQ:
2615 rps_peer->num_issued_pull_req = value;
2617 case STAT_TYPE_ISSUED_PULL_REP:
2618 rps_peer->num_issued_pull_rep = value;
2620 case STAT_TYPE_SENT_PUSH_SEND:
2621 rps_peer->num_sent_push = value;
2623 case STAT_TYPE_SENT_PULL_REQ:
2624 rps_peer->num_sent_pull_req = value;
2626 case STAT_TYPE_SENT_PULL_REP:
2627 rps_peer->num_sent_pull_rep = value;
2629 case STAT_TYPE_RECV_PUSH_SEND:
2630 rps_peer->num_recv_push = value;
2632 case STAT_TYPE_RECV_PULL_REQ:
2633 rps_peer->num_recv_pull_req = value;
2635 case STAT_TYPE_RECV_PULL_REP:
2636 rps_peer->num_recv_pull_rep = value;
2640 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2641 "Unknown statistics string: %s\n",
2648 void post_profiler (struct RPSPeer *rps_peer)
2650 if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2656 "Going to request statistic values with mask 0x%" PRIx32 "\n",
2657 cur_test_run.stat_collect_flags);
2659 struct STATcls *stat_cls;
2661 for (stat_type = STAT_TYPE_ROUNDS;
2662 stat_type < STAT_TYPE_MAX;
2663 stat_type = stat_type <<1)
2665 if (stat_type & cur_test_run.stat_collect_flags)
2667 stat_cls = GNUNET_malloc (sizeof (struct STATcls));
2668 stat_cls->rps_peer = rps_peer;
2669 stat_cls->stat_type = stat_type;
2670 rps_peer->file_name_stats =
2671 store_prefix_file_name (rps_peer->peer_id, "stats");
2672 GNUNET_STATISTICS_get (rps_peer->stats_h,
2674 stat_type_2_str (stat_type),
2675 post_test_shutdown_ready_cb,
2677 (struct STATcls *) stat_cls);
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2679 "Requested statistics for %s (peer %" PRIu32 ")\n",
2680 stat_type_2_str (stat_type),
2687 /***********************************************************************
2688 * /Definition of tests
2689 ***********************************************************************/
2693 * Actual "main" function for the testcase.
2695 * @param cls closure
2696 * @param h the run handle
2697 * @param n_peers number of peers in 'peers'
2698 * @param peers handle to peers run in the testbed
2699 * @param links_succeeded the number of overlay link connection attempts that
2701 * @param links_failed the number of overlay link connection attempts that
2706 struct GNUNET_TESTBED_RunHandle *h,
2707 unsigned int n_peers,
2708 struct GNUNET_TESTBED_Peer **peers,
2709 unsigned int links_succeeded,
2710 unsigned int links_failed)
2714 (void) links_failed;
2716 struct OpListEntry *entry;
2718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
2720 /* Check whether we timed out */
2721 if (n_peers != num_peers ||
2723 0 == links_succeeded)
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
2728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
2730 GNUNET_SCHEDULER_shutdown ();
2735 /* Initialize peers */
2736 testbed_peers = peers;
2737 num_peers_online = 0;
2738 for (i = 0; i < num_peers; i++)
2740 entry = make_oplist_entry ();
2742 rps_peers[i].index = i;
2743 if (NULL != cur_test_run.init_peer)
2744 cur_test_run.init_peer (&rps_peers[i]);
2745 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2747 rps_peers->cur_view_count = 0;
2748 rps_peers->cur_view = NULL;
2750 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2751 GNUNET_TESTBED_PIT_IDENTITY,
2756 /* Bring peers up */
2757 GNUNET_assert (num_peers == n_peers);
2758 for (i = 0; i < n_peers; i++)
2760 rps_peers[i].index = i;
2762 GNUNET_TESTBED_service_connect (&rps_peers[i],
2765 &rps_connect_complete_cb,
2767 &rps_connect_adapter,
2768 &rps_disconnect_adapter,
2770 /* Connect all peers to statistics service */
2771 if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2773 rps_peers[i].stat_op =
2774 GNUNET_TESTBED_service_connect (NULL,
2779 &stat_connect_adapter,
2780 &stat_disconnect_adapter,
2785 if (NULL != churn_task)
2786 GNUNET_SCHEDULER_cancel (churn_task);
2787 post_test_task = GNUNET_SCHEDULER_add_delayed (timeout, &post_test_op, NULL);
2788 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2789 (timeout_s * 1.2) + 0.1 * num_peers);
2790 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
2795 * Entry point for the testcase, sets up the testbed.
2797 * @param argc unused
2798 * @param argv unused
2799 * @return 0 on success
2802 main (int argc, char *argv[])
2807 /* Defaults for tests */
2809 cur_test_run.name = "test-rps-default";
2810 cur_test_run.init_peer = default_init_peer;
2811 cur_test_run.pre_test = NULL;
2812 cur_test_run.reply_handle = default_reply_handle;
2813 cur_test_run.eval_cb = default_eval_cb;
2814 cur_test_run.post_test = NULL;
2815 cur_test_run.have_churn = HAVE_CHURN;
2816 cur_test_run.have_collect_statistics = NO_COLLECT_STATISTICS;
2817 cur_test_run.stat_collect_flags = 0;
2818 cur_test_run.have_collect_view = NO_COLLECT_VIEW;
2822 if (strstr (argv[0], "malicious") != NULL)
2824 cur_test_run.pre_test = mal_pre;
2825 cur_test_run.main_test = mal_cb;
2826 cur_test_run.init_peer = mal_init_peer;
2829 if (strstr (argv[0], "_1") != NULL)
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
2832 cur_test_run.name = "test-rps-malicious_1";
2835 else if (strstr (argv[0], "_2") != NULL)
2837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
2838 cur_test_run.name = "test-rps-malicious_2";
2841 else if (strstr (argv[0], "_3") != NULL)
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
2844 cur_test_run.name = "test-rps-malicious_3";
2849 else if (strstr (argv[0], "_single_req") != NULL)
2851 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
2852 cur_test_run.name = "test-rps-single-req";
2853 cur_test_run.main_test = single_req_cb;
2854 cur_test_run.have_churn = HAVE_NO_CHURN;
2857 else if (strstr (argv[0], "_delayed_reqs") != NULL)
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
2860 cur_test_run.name = "test-rps-delayed-reqs";
2861 cur_test_run.main_test = delay_req_cb;
2862 cur_test_run.have_churn = HAVE_NO_CHURN;
2865 else if (strstr (argv[0], "_seed_big") != NULL)
2867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
2869 cur_test_run.name = "test-rps-seed-big";
2870 cur_test_run.main_test = seed_big_cb;
2871 cur_test_run.eval_cb = no_eval;
2872 cur_test_run.have_churn = HAVE_NO_CHURN;
2876 else if (strstr (argv[0], "_single_peer_seed") != NULL)
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
2879 cur_test_run.name = "test-rps-single-peer-seed";
2880 cur_test_run.main_test = single_peer_seed_cb;
2881 cur_test_run.have_churn = HAVE_NO_CHURN;
2884 else if (strstr (argv[0], "_seed_request") != NULL)
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
2887 cur_test_run.name = "test-rps-seed-request";
2888 cur_test_run.main_test = seed_req_cb;
2889 cur_test_run.have_churn = HAVE_NO_CHURN;
2892 else if (strstr (argv[0], "_seed") != NULL)
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
2895 cur_test_run.name = "test-rps-seed";
2896 cur_test_run.main_test = seed_cb;
2897 cur_test_run.eval_cb = no_eval;
2898 cur_test_run.have_churn = HAVE_NO_CHURN;
2901 else if (strstr (argv[0], "_req_cancel") != NULL)
2903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
2904 cur_test_run.name = "test-rps-req-cancel";
2906 cur_test_run.main_test = req_cancel_cb;
2907 cur_test_run.eval_cb = no_eval;
2908 cur_test_run.have_churn = HAVE_NO_CHURN;
2912 else if (strstr (argv[0], "_churn") != NULL)
2914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
2915 cur_test_run.name = "test-rps-churn";
2917 cur_test_run.init_peer = default_init_peer;
2918 cur_test_run.main_test = churn_test_cb;
2919 cur_test_run.reply_handle = default_reply_handle;
2920 cur_test_run.eval_cb = default_eval_cb;
2921 cur_test_run.have_churn = HAVE_NO_CHURN;
2922 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
2926 else if (strstr (argv[0], "_sub") != NULL)
2928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test subs\n");
2929 cur_test_run.name = "test-rps-sub";
2931 //cur_test_run.init_peer = &default_init_peer;
2932 cur_test_run.pre_test = &sub_pre;
2933 cur_test_run.main_test = &single_req_cb;
2934 //cur_test_run.reply_handle = default_reply_handle;
2935 cur_test_run.post_test = &sub_post;
2936 //cur_test_run.eval_cb = default_eval_cb;
2937 cur_test_run.have_churn = HAVE_NO_CHURN;
2938 cur_test_run.have_quick_quit = HAVE_QUICK_QUIT;
2941 else if (strstr (argv[0], "profiler") != NULL)
2943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
2944 cur_test_run.name = "test-rps-profiler";
2947 cur_test_run.init_peer = profiler_init_peer;
2948 //cur_test_run.pre_test = mal_pre;
2949 cur_test_run.pre_test = pre_profiler;
2950 cur_test_run.main_test = profiler_cb;
2951 cur_test_run.reply_handle = profiler_reply_handle;
2952 cur_test_run.eval_cb = profiler_eval;
2953 cur_test_run.post_test = post_profiler;
2954 cur_test_run.request_interval = 2;
2955 cur_test_run.num_requests = 5;
2956 //cur_test_run.have_churn = HAVE_CHURN;
2957 cur_test_run.have_churn = HAVE_NO_CHURN;
2958 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
2959 cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
2960 cur_test_run.stat_collect_flags = STAT_TYPE_ROUNDS |
2962 STAT_TYPE_BLOCKS_MANY_PUSH |
2963 STAT_TYPE_BLOCKS_NO_PUSH |
2964 STAT_TYPE_BLOCKS_NO_PULL |
2965 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL |
2966 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL |
2967 STAT_TYPE_ISSUED_PUSH_SEND |
2968 STAT_TYPE_ISSUED_PULL_REQ |
2969 STAT_TYPE_ISSUED_PULL_REP |
2970 STAT_TYPE_SENT_PUSH_SEND |
2971 STAT_TYPE_SENT_PULL_REQ |
2972 STAT_TYPE_SENT_PULL_REP |
2973 STAT_TYPE_RECV_PUSH_SEND |
2974 STAT_TYPE_RECV_PULL_REQ |
2975 STAT_TYPE_RECV_PULL_REP;
2976 cur_test_run.have_collect_view = COLLECT_VIEW;
2979 /* 'Clean' directory */
2980 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
2981 GNUNET_DISK_directory_create ("/tmp/rps/");
2983 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_s);
2985 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
2986 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
2987 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
2988 if ( (2 == mal_type) ||
2990 target_peer = &rps_peer_ids[num_peers - 2];
2991 if (profiler_eval == cur_test_run.eval_cb)
2992 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
2993 malicious peer if not careful
2994 with the malicious portion */
2997 ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
3002 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3003 "_test_run returned.\n");
3004 if (GNUNET_OK != ret_value)
3006 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3007 "Test did not run successfully!\n");
3010 ret_value = cur_test_run.eval_cb();
3012 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
3014 GNUNET_array_grow (rps_peers->cur_view,
3015 rps_peers->cur_view_count,
3018 GNUNET_free (rps_peers);
3019 GNUNET_free (rps_peer_ids);
3020 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
3025 /* end of test_rps.c */