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 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.
16 * @file rps/test_rps.c
17 * @brief Testcase for the random peer sampling service. Starts
18 * a peergroup with a given number of peers, then waits to
19 * receive size pushes/pulls from each peer. Expects to wait
20 * for one message from each peer.
23 #include "gnunet_util_lib.h"
24 #include "gnunet_testbed_service.h"
26 #include "gnunet_rps_service.h"
27 #include "rps-test_util.h"
28 #include "gnunet-service-rps_sampler_elem.h"
34 * How many peers do we start?
36 static uint32_t num_peers;
39 * How long do we run the test?
42 static uint32_t timeout_s;
45 * How long do we run the test?
47 //#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
48 static struct GNUNET_TIME_Relative timeout;
52 * Portion of malicious peers
54 static double portion = .1;
57 * Type of malicious peer to test
59 static unsigned int mal_type = 0;
62 * Handles to all of the running peers
64 static struct GNUNET_TESTBED_Peer **testbed_peers;
67 * @brief Indicates whether peer should go off- or online
69 enum PEER_ONLINE_DELTA {
71 * @brief Indicates peer going online
75 * @brief Indicates peer going offline
88 struct OpListEntry *next;
93 struct OpListEntry *prev;
96 * The testbed operation
98 struct GNUNET_TESTBED_Operation *op;
101 * Depending on whether we start or stop RPS service at the peer, set this to
102 * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
104 enum PEER_ONLINE_DELTA delta;
107 * Index of the regarding peer
115 static struct OpListEntry *oplist_head;
120 static struct OpListEntry *oplist_tail;
124 * A pending reply: A request was sent and the reply is pending.
131 struct PendingReply *next;
132 struct PendingReply *prev;
135 * Handle to the request we are waiting for
137 struct GNUNET_RPS_Request_Handle *req_handle;
140 * The peer that requested
142 struct RPSPeer *rps_peer;
147 * A pending request: A request was not made yet but is scheduled for later.
149 struct PendingRequest
154 struct PendingRequest *next;
155 struct PendingRequest *prev;
158 * Handle to the request we are waiting for
160 struct GNUNET_SCHEDULER_Task *request_task;
163 * The peer that requested
165 struct RPSPeer *rps_peer;
170 * Information we track for each peer.
180 * Handle for RPS connect operation.
182 struct GNUNET_TESTBED_Operation *op;
185 * Handle to RPS service.
187 struct GNUNET_RPS_Handle *rps_handle;
192 struct GNUNET_PeerIdentity *peer_id;
195 * A request handle to check for an request
197 //struct GNUNET_RPS_Request_Handle *req_handle;
200 * Peer on- or offline?
205 * Number of Peer IDs to request during the whole test
207 unsigned int num_ids_to_request;
210 * Pending requests DLL
212 struct PendingRequest *pending_req_head;
213 struct PendingRequest *pending_req_tail;
216 * Number of pending requests
218 unsigned int num_pending_reqs;
221 * Pending replies DLL
223 struct PendingReply *pending_rep_head;
224 struct PendingReply *pending_rep_tail;
227 * Number of pending replies
229 unsigned int num_pending_reps;
232 * Number of received PeerIDs
234 unsigned int num_recv_ids;
237 * Pending operation on that peer
239 const struct OpListEntry *entry_op_manage;
242 * Testbed operation to connect to statistics service
244 struct GNUNET_TESTBED_Operation *stat_op;
247 * Handle to the statistics service
249 struct GNUNET_STATISTICS_Handle *stats_h;
252 * @brief flags to indicate which statistics values have been already
253 * collected from the statistics service.
254 * Used to check whether we are able to shutdown.
256 uint32_t stat_collected_flags;
259 * @brief File name of the file the stats are finally written to
261 const char *file_name_stats;
264 * @brief File name of the file the stats are finally written to
266 const char *file_name_probs;
269 * @brief The current view
271 struct GNUNET_PeerIdentity *cur_view;
274 * @brief Number of peers in the #cur_view.
276 uint32_t cur_view_count;
279 * @brief Number of occurrences in other peer's view
281 uint32_t count_in_views;
284 * @brief statistics values
288 uint64_t num_blocks_many_push;
289 uint64_t num_blocks_no_push;
290 uint64_t num_blocks_no_pull;
291 uint64_t num_blocks_many_push_no_pull;
292 uint64_t num_blocks_no_push_no_pull;
293 uint64_t num_issued_push;
294 uint64_t num_issued_pull_req;
295 uint64_t num_issued_pull_rep;
296 uint64_t num_sent_push;
297 uint64_t num_sent_pull_req;
298 uint64_t num_sent_pull_rep;
299 uint64_t num_recv_push;
300 uint64_t num_recv_pull_req;
301 uint64_t num_recv_pull_rep;
306 STAT_TYPE_ROUNDS = 0x1, /* 1 */
307 STAT_TYPE_BLOCKS = 0x2, /* 2 */
308 STAT_TYPE_BLOCKS_MANY_PUSH = 0x4, /* 3 */
309 STAT_TYPE_BLOCKS_NO_PUSH = 0x8, /* 4 */
310 STAT_TYPE_BLOCKS_NO_PULL = 0x10, /* 5 */
311 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL = 0x20, /* 6 */
312 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL = 0x40, /* 7 */
313 STAT_TYPE_ISSUED_PUSH_SEND = 0x80, /* 8 */
314 STAT_TYPE_ISSUED_PULL_REQ = 0x100, /* 9 */
315 STAT_TYPE_ISSUED_PULL_REP = 0x200, /* 10 */
316 STAT_TYPE_SENT_PUSH_SEND = 0x400, /* 11 */
317 STAT_TYPE_SENT_PULL_REQ = 0x800, /* 12 */
318 STAT_TYPE_SENT_PULL_REP = 0x1000, /* 13 */
319 STAT_TYPE_RECV_PUSH_SEND = 0x2000, /* 14 */
320 STAT_TYPE_RECV_PULL_REQ = 0x4000, /* 15 */
321 STAT_TYPE_RECV_PULL_REP = 0x8000, /* 16 */
322 STAT_TYPE_MAX = 0x80000000, /* 32 */
327 struct RPSPeer *rps_peer;
328 enum STAT_TYPE stat_type;
333 * Information for all the peers.
335 static struct RPSPeer *rps_peers;
338 * Peermap to get the index of a given peer ID quick.
340 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
345 static struct GNUNET_PeerIdentity *rps_peer_ids;
348 * ID of the targeted peer.
350 static struct GNUNET_PeerIdentity *target_peer;
353 * ID of the peer that requests for the evaluation.
355 static struct RPSPeer *eval_peer;
358 * Number of online peers.
360 static unsigned int num_peers_online;
363 * @brief The added sizes of the peer's views
365 static unsigned int view_sizes;
368 * Return value from 'main'.
373 * Identifier for the churn task that runs periodically
375 static struct GNUNET_SCHEDULER_Task *post_test_task;
378 * Identifier for the churn task that runs periodically
380 static struct GNUNET_SCHEDULER_Task *shutdown_task;
383 * Identifier for the churn task that runs periodically
385 static struct GNUNET_SCHEDULER_Task *churn_task;
388 * Called to initialise the given RPSPeer
390 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
393 * @brief Called directly after connecting to the service
395 * @param rps_peer Specific peer the function is called on
396 * @param h the handle to the rps service
398 typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
401 * @brief Executes functions to test the api/service for a given peer
403 * Called from within #rps_connect_complete_cb ()
404 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
405 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
407 * @param rps_peer the peer the task runs on
409 typedef void (*MainTest) (struct RPSPeer *rps_peer);
412 * Callback called once the requested random peers are available
414 typedef void (*ReplyHandle) (void *cls,
416 const struct GNUNET_PeerIdentity *recv_peers);
419 * Called directly before disconnecting from the service
421 typedef void (*PostTest) (struct RPSPeer *peer);
424 * Function called after disconnect to evaluate test success
426 typedef int (*EvaluationCallback) (void);
429 * @brief Do we have Churn?
433 * @brief If we have churn this is set
437 * @brief If we have no churn this is set
443 * @brief Is it ok to quit the test before the timeout?
445 enum OPTION_QUICK_QUIT {
447 * @brief It is ok for the test to quit before the timeout triggers
452 * @brief It is NOT ok for the test to quit before the timeout triggers
458 * @brief Do we collect statistics at the end?
460 enum OPTION_COLLECT_STATISTICS {
462 * @brief We collect statistics at the end
467 * @brief We do not collect statistics at the end
469 NO_COLLECT_STATISTICS,
473 * @brief Do we collect views during run?
475 enum OPTION_COLLECT_VIEW {
477 * @brief We collect view during run
482 * @brief We do not collect the view during run
488 * Structure to define a single test
498 * Called with a single peer in order to initialise that peer
503 * Called directly after connecting to the service
508 * Main function for each peer
513 * Callback called once the requested peers are available
515 ReplyHandle reply_handle;
518 * Called directly before disconnecting from the service
523 * Function to evaluate the test results
525 EvaluationCallback eval_cb;
530 uint32_t request_interval;
533 * Number of Requests to make.
535 uint32_t num_requests;
538 * Run with (-out) churn
540 enum OPTION_CHURN have_churn;
543 * Quit test before timeout?
545 enum OPTION_QUICK_QUIT have_quick_quit;
548 * Collect statistics at the end?
550 enum OPTION_COLLECT_STATISTICS have_collect_statistics;
553 * Collect view during run?
555 enum OPTION_COLLECT_VIEW have_collect_view;
558 * @brief Mark which values from the statistics service to collect at the end
561 uint32_t stat_collect_flags;
565 * Did we finish the test?
567 static int post_test;
570 * Are we shutting down?
572 static int in_shutdown;
575 * Append arguments to file
578 tofile_ (const char *file_name, const char *line)
580 struct GNUNET_DISK_FileHandle *f;
581 /* char output_buffer[512]; */
586 if (NULL == (f = GNUNET_DISK_file_open (file_name,
587 GNUNET_DISK_OPEN_APPEND |
588 GNUNET_DISK_OPEN_WRITE |
589 GNUNET_DISK_OPEN_CREATE,
590 GNUNET_DISK_PERM_USER_READ |
591 GNUNET_DISK_PERM_USER_WRITE |
592 GNUNET_DISK_PERM_GROUP_READ |
593 GNUNET_DISK_PERM_OTHER_READ)))
595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
596 "Not able to open file %s\n",
600 /* size = GNUNET_snprintf (output_buffer,
601 sizeof (output_buffer),
603 GNUNET_TIME_absolute_get ().abs_value_us,
607 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
608 "Failed to write string to buffer (size: %i)\n",
613 size = strlen (line) * sizeof (char);
615 size2 = GNUNET_DISK_file_write (f, line, size);
618 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
619 "Unable to write to file! (Size: %lu, size2: %lu)\n",
622 if (GNUNET_YES != GNUNET_DISK_file_close (f))
624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
625 "Unable to close file\n");
630 if (GNUNET_YES != GNUNET_DISK_file_close (f))
632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
633 "Unable to close file\n");
638 * This function is used to facilitate writing important information to disk
640 #define tofile(file_name, ...) do {\
643 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
646 "Failed to create tmp_buf\n");\
648 tofile_(file_name,tmp_buf);\
653 * Write the ids and their according index in the given array to a file
657 ids_to_file (char *file_name,
658 struct GNUNET_PeerIdentity *peer_ids,
659 unsigned int num_peer_ids)
663 for (i=0 ; i < num_peer_ids ; i++)
668 GNUNET_i2s_full (&peer_ids[i]));
673 * Test the success of a single test
683 for (i = 0; i < num_peers; i++)
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
688 GNUNET_i2s (rps_peers[i].peer_id),
689 rps_peers[i].num_recv_ids,
690 rps_peers[i].num_ids_to_request,
691 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
692 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
694 return tmp_ok? 0 : 1;
699 * Creates an oplist entry and adds it to the oplist DLL
701 static struct OpListEntry *
704 struct OpListEntry *entry;
706 entry = GNUNET_new (struct OpListEntry);
707 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
713 * @brief Checks if given peer already received its statistics value from the
714 * statistics service.
716 * @param rps_peer the peer to check for
718 * @return #GNUNET_YES if so
719 * #GNUNET_NO otherwise
721 static int check_statistics_collect_completed_single_peer (
722 const struct RPSPeer *rps_peer)
724 if (cur_test_run.stat_collect_flags !=
725 (cur_test_run.stat_collect_flags &
726 rps_peer->stat_collected_flags))
733 * @brief Checks if all peers already received their statistics value from the
734 * statistics service.
736 * @return #GNUNET_YES if so
737 * #GNUNET_NO otherwise
739 static int check_statistics_collect_completed ()
743 for (i = 0; i < num_peers; i++)
745 if (GNUNET_NO == check_statistics_collect_completed_single_peer (&rps_peers[i]))
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "At least Peer %" PRIu32 " did not yet receive all statistics values\n",
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "All peers received their statistics values\n");
759 * Task run on timeout to shut everything down.
762 shutdown_op (void *cls)
766 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
767 "Shutdown task scheduled, going down.\n");
768 in_shutdown = GNUNET_YES;
769 if (NULL != post_test_task)
771 GNUNET_SCHEDULER_cancel (post_test_task);
773 if (NULL != churn_task)
775 GNUNET_SCHEDULER_cancel (churn_task);
778 for (i = 0; i < num_peers; i++)
780 if (NULL != rps_peers[i].rps_handle)
782 GNUNET_RPS_disconnect (rps_peers[i].rps_handle);
784 if (NULL != rps_peers[i].op)
786 GNUNET_TESTBED_operation_done (rps_peers[i].op);
793 * Task run on timeout to collect statistics and potentially shut down.
796 post_test_op (void *cls)
800 post_test_task = NULL;
801 post_test = GNUNET_YES;
802 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
803 "Post test task scheduled, going down.\n");
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].op)
813 GNUNET_TESTBED_operation_done (rps_peers[i].op);
814 rps_peers[i].op = NULL;
816 if (NULL != cur_test_run.post_test)
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n", i);
819 cur_test_run.post_test (&rps_peers[i]);
822 /* If we do not collect statistics, shut down directly */
823 if (NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics ||
824 GNUNET_YES == check_statistics_collect_completed())
826 GNUNET_SCHEDULER_shutdown ();
835 seed_peers (void *cls)
837 struct RPSPeer *peer = cls;
841 // TODO if malicious don't seed mal peers
842 amount = round (.5 * num_peers);
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
845 for (i = 0 ; i < amount ; i++)
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
848 GNUNET_i2s (&rps_peer_ids[i]));
850 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
858 seed_peers_big (void *cls)
860 struct RPSPeer *peer = cls;
861 unsigned int seed_msg_size;
862 uint32_t num_peers_max;
866 seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
867 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - seed_msg_size) /
868 sizeof (struct GNUNET_PeerIdentity);
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Peers that fit in one seed msg; %u\n",
872 amount = num_peers_max + (0.5 * num_peers_max);
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
874 "Seeding many (%u) peers:\n",
876 struct GNUNET_PeerIdentity ids_to_seed[amount];
877 for (i = 0; i < amount; i++)
879 ids_to_seed[i] = *peer->peer_id;
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
882 GNUNET_i2s (&ids_to_seed[i]));
885 GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
889 * Get the id of peer i.
892 info_cb (void *cb_cls,
893 struct GNUNET_TESTBED_Operation *op,
894 const struct GNUNET_TESTBED_PeerInformation *pinfo,
897 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
899 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
904 if (NULL == pinfo || NULL != emsg)
906 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
907 GNUNET_TESTBED_operation_done (entry->op);
911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 GNUNET_i2s (pinfo->result.id));
916 rps_peer_ids[entry->index] = *(pinfo->result.id);
917 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
919 GNUNET_assert (GNUNET_OK ==
920 GNUNET_CONTAINER_multipeermap_put (peer_map,
921 &rps_peer_ids[entry->index],
922 &rps_peers[entry->index],
923 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
924 tofile ("/tmp/rps/peer_ids",
927 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
929 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
930 GNUNET_TESTBED_operation_done (entry->op);
936 * Callback to be called when RPS service connect operation is completed
938 * @param cls the callback closure from functions generating an operation
939 * @param op the operation that has been finished
940 * @param ca_result the RPS service handle returned from rps_connect_adapter
941 * @param emsg error message in case the operation has failed; will be NULL if
942 * operation has executed successfully.
945 rps_connect_complete_cb (void *cls,
946 struct GNUNET_TESTBED_Operation *op,
950 struct RPSPeer *rps_peer = cls;
951 struct GNUNET_RPS_Handle *rps = ca_result;
953 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
958 rps_peer->rps_handle = rps;
959 rps_peer->online = GNUNET_YES;
962 GNUNET_assert (op == rps_peer->op);
965 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
966 "Failed to connect to RPS service: %s\n",
969 GNUNET_SCHEDULER_shutdown ();
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
975 cur_test_run.main_test (rps_peer);
980 * Adapter function called to establish a connection to
984 * @param cfg configuration of the peer to connect to; will be available until
985 * GNUNET_TESTBED_operation_done() is called on the operation returned
986 * from GNUNET_TESTBED_service_connect()
987 * @return service handle to return in 'op_result', NULL on error
990 rps_connect_adapter (void *cls,
991 const struct GNUNET_CONFIGURATION_Handle *cfg)
993 struct GNUNET_RPS_Handle *h;
995 h = GNUNET_RPS_connect (cfg);
997 if (NULL != cur_test_run.pre_test)
998 cur_test_run.pre_test (cls, h);
1004 * Called to open a connection to the peer's statistics
1006 * @param cls peer context
1007 * @param cfg configuration of the peer to connect to; will be available until
1008 * GNUNET_TESTBED_operation_done() is called on the operation returned
1009 * from GNUNET_TESTBED_service_connect()
1010 * @return service handle to return in 'op_result', NULL on error
1013 stat_connect_adapter (void *cls,
1014 const struct GNUNET_CONFIGURATION_Handle *cfg)
1016 struct RPSPeer *peer = cls;
1018 peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
1019 return peer->stats_h;
1023 * Called to disconnect from peer's statistics service
1025 * @param cls peer context
1026 * @param op_result service handle returned from the connect adapter
1029 stat_disconnect_adapter (void *cls, void *op_result)
1031 struct RPSPeer *peer = cls;
1033 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1034 // (peer->stats_h, "core", "# peers connected",
1035 // stat_iterator, peer));
1036 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1037 // (peer->stats_h, "nse", "# peers connected",
1038 // stat_iterator, peer));
1039 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
1040 peer->stats_h = NULL;
1044 * Called after successfully opening a connection to a peer's statistics
1045 * service; we register statistics monitoring for CORE and NSE here.
1047 * @param cls the callback closure from functions generating an operation
1048 * @param op the operation that has been finished
1049 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
1050 * @param emsg error message in case the operation has failed; will be NULL if
1051 * operation has executed successfully.
1054 stat_complete_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1055 void *ca_result, const char *emsg )
1057 //struct GNUNET_STATISTICS_Handle *sh = ca_result;
1058 //struct RPSPeer *peer = (struct RPSPeer *) cls;
1065 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1066 // (sh, "core", "# peers connected",
1067 // stat_iterator, peer));
1068 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1069 // (sh, "nse", "# peers connected",
1070 // stat_iterator, peer));
1075 * Adapter function called to destroy connection to
1078 * @param cls closure
1079 * @param op_result service handle returned from the connect adapter
1082 rps_disconnect_adapter (void *cls,
1085 struct RPSPeer *peer = cls;
1086 struct GNUNET_RPS_Handle *h = op_result;
1087 GNUNET_assert (NULL != peer);
1088 GNUNET_RPS_disconnect (h);
1089 peer->rps_handle = NULL;
1093 /***********************************************************************
1094 * Definition of tests
1095 ***********************************************************************/
1097 // TODO check whether tests can be stopped earlier
1099 default_eval_cb (void)
1111 * Initialise given RPSPeer
1113 static void default_init_peer (struct RPSPeer *rps_peer)
1115 rps_peer->num_ids_to_request = 1;
1119 * Callback to call on receipt of a reply
1121 * @param cls closure
1122 * @param n number of peers
1123 * @param recv_peers the received peers
1126 default_reply_handle (void *cls,
1128 const struct GNUNET_PeerIdentity *recv_peers)
1130 struct RPSPeer *rps_peer;
1131 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1134 rps_peer = pending_rep->rps_peer;
1135 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1136 rps_peer->pending_rep_tail,
1138 rps_peer->num_pending_reps--;
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "[%s] got %" PRIu64 " peers:\n",
1141 GNUNET_i2s (rps_peer->peer_id),
1144 for (i = 0; i < n; i++)
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 GNUNET_i2s (&recv_peers[i]));
1151 rps_peer->num_recv_ids++;
1154 if (0 == evaluate () && HAVE_QUICK_QUIT == cur_test_run.have_quick_quit)
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test succeeded before timeout\n");
1157 GNUNET_assert (NULL != post_test_task);
1158 GNUNET_SCHEDULER_cancel (post_test_task);
1159 post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
1160 GNUNET_assert (NULL!= post_test_task);
1165 * Request random peers.
1168 request_peers (void *cls)
1170 struct PendingRequest *pending_req = cls;
1171 struct RPSPeer *rps_peer;
1172 struct PendingReply *pending_rep;
1174 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1176 rps_peer = pending_req->rps_peer;
1177 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1178 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1179 rps_peer->pending_req_tail,
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "Requesting one peer\n");
1183 pending_rep = GNUNET_new (struct PendingReply);
1184 pending_rep->rps_peer = rps_peer;
1185 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1187 cur_test_run.reply_handle,
1189 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1190 rps_peer->pending_rep_tail,
1192 rps_peer->num_pending_reps++;
1193 rps_peer->num_pending_reqs--;
1197 cancel_pending_req (struct PendingRequest *pending_req)
1199 struct RPSPeer *rps_peer;
1201 rps_peer = pending_req->rps_peer;
1202 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1203 rps_peer->pending_req_tail,
1205 rps_peer->num_pending_reqs--;
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Cancelling pending request\n");
1208 GNUNET_SCHEDULER_cancel (pending_req->request_task);
1209 GNUNET_free (pending_req);
1213 cancel_request (struct PendingReply *pending_rep)
1215 struct RPSPeer *rps_peer;
1217 rps_peer = pending_rep->rps_peer;
1218 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1219 rps_peer->pending_rep_tail,
1221 rps_peer->num_pending_reps--;
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Cancelling request\n");
1224 GNUNET_RPS_request_cancel (pending_rep->req_handle);
1225 GNUNET_free (pending_rep);
1232 cancel_request_cb (void *cls)
1234 struct RPSPeer *rps_peer = cls;
1235 struct PendingReply *pending_rep;
1237 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1239 pending_rep = rps_peer->pending_rep_head;
1240 GNUNET_assert (1 <= rps_peer->num_pending_reps);
1241 cancel_request (pending_rep);
1246 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1247 * issued, nor replied
1250 schedule_missing_requests (struct RPSPeer *rps_peer)
1253 struct PendingRequest *pending_req;
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "Scheduling %u - %u missing requests\n",
1257 rps_peer->num_ids_to_request,
1258 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1259 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1260 rps_peer->num_ids_to_request);
1261 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1262 i < rps_peer->num_ids_to_request; i++)
1264 pending_req = GNUNET_new (struct PendingRequest);
1265 pending_req->rps_peer = rps_peer;
1266 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1267 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1268 cur_test_run.request_interval * i),
1271 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1272 rps_peer->pending_req_tail,
1274 rps_peer->num_pending_reqs++;
1279 cancel_pending_req_rep (struct RPSPeer *rps_peer)
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1282 "Cancelling all (pending) requests.\n");
1283 while (NULL != rps_peer->pending_req_head)
1284 cancel_pending_req (rps_peer->pending_req_head);
1285 GNUNET_assert (0 == rps_peer->num_pending_reqs);
1286 while (NULL != rps_peer->pending_rep_head)
1287 cancel_request (rps_peer->pending_rep_head);
1288 GNUNET_assert (0 == rps_peer->num_pending_reps);
1291 /***********************************
1293 ***********************************/
1296 * Initialise only non-mal RPSPeers
1298 static void mal_init_peer (struct RPSPeer *rps_peer)
1300 if (rps_peer->index >= round (portion * num_peers))
1301 rps_peer->num_ids_to_request = 1;
1306 * @brief Set peers to (non-)malicious before execution
1308 * Of signature #PreTest
1310 * @param rps_peer the peer to set (non-) malicious
1311 * @param h the handle to the service
1314 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1316 #ifdef ENABLE_MALICIOUS
1317 uint32_t num_mal_peers;
1319 GNUNET_assert ( (1 >= portion) &&
1321 num_mal_peers = round (portion * num_peers);
1323 if (rps_peer->index < num_mal_peers)
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
1328 GNUNET_i2s (rps_peer->peer_id),
1331 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1332 rps_peer_ids, target_peer);
1334 #endif /* ENABLE_MALICIOUS */
1338 mal_cb (struct RPSPeer *rps_peer)
1340 uint32_t num_mal_peers;
1342 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1347 #ifdef ENABLE_MALICIOUS
1348 GNUNET_assert ( (1 >= portion) &&
1350 num_mal_peers = round (portion * num_peers);
1352 if (rps_peer->index >= num_mal_peers)
1353 { /* It's useless to ask a malicious peer about a random sample -
1354 it's not sampling */
1355 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1356 seed_peers, rps_peer);
1357 schedule_missing_requests (rps_peer);
1359 #endif /* ENABLE_MALICIOUS */
1363 /***********************************
1365 ***********************************/
1367 single_req_cb (struct RPSPeer *rps_peer)
1369 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1374 schedule_missing_requests (rps_peer);
1377 /***********************************
1379 ***********************************/
1381 delay_req_cb (struct RPSPeer *rps_peer)
1383 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1388 schedule_missing_requests (rps_peer);
1391 /***********************************
1393 ***********************************/
1395 seed_cb (struct RPSPeer *rps_peer)
1397 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1402 GNUNET_SCHEDULER_add_delayed (
1403 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
1404 seed_peers, rps_peer);
1407 /***********************************
1409 ***********************************/
1411 seed_big_cb (struct RPSPeer *rps_peer)
1413 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1418 // TODO test seeding > GNUNET_MAX_MESSAGE_SIZE peers
1419 GNUNET_SCHEDULER_add_delayed (
1420 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1421 seed_peers_big, rps_peer);
1424 /***********************************
1426 ***********************************/
1428 single_peer_seed_cb (struct RPSPeer *rps_peer)
1433 /***********************************
1435 ***********************************/
1437 seed_req_cb (struct RPSPeer *rps_peer)
1439 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1444 GNUNET_SCHEDULER_add_delayed (
1445 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1446 seed_peers, rps_peer);
1447 schedule_missing_requests (rps_peer);
1450 //TODO start big mal
1452 /***********************************
1454 ***********************************/
1456 req_cancel_cb (struct RPSPeer *rps_peer)
1458 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1463 schedule_missing_requests (rps_peer);
1464 GNUNET_SCHEDULER_add_delayed (
1465 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1466 (cur_test_run.request_interval + 1)),
1467 cancel_request_cb, rps_peer);
1470 /***********************************
1472 ***********************************/
1478 * @brief Starts churn
1480 * Has signature of #MainTest
1482 * This is not implemented too nicely as this is called for each peer, but we
1483 * only need to call it once. (Yes we check that we only schedule the task
1486 * @param rps_peer The peer it's called for
1489 churn_test_cb (struct RPSPeer *rps_peer)
1491 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1497 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1500 "Starting churn task\n");
1501 churn_task = GNUNET_SCHEDULER_add_delayed (
1502 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "Not starting churn task\n");
1510 schedule_missing_requests (rps_peer);
1513 /***********************************
1515 ***********************************/
1518 * Callback to be called when RPS service is started or stopped at peers
1521 * @param op the operation handle
1522 * @param emsg NULL on success; otherwise an error description
1525 churn_cb (void *cls,
1526 struct GNUNET_TESTBED_Operation *op,
1530 struct OpListEntry *entry = cls;
1532 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1537 GNUNET_TESTBED_operation_done (entry->op);
1540 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1541 GNUNET_SCHEDULER_shutdown ();
1544 GNUNET_assert (0 != entry->delta);
1546 num_peers_online += entry->delta;
1548 if (PEER_GO_OFFLINE == entry->delta)
1549 { /* Peer hopefully just went offline */
1550 if (GNUNET_YES != rps_peers[entry->index].online)
1552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1553 "peer %s was expected to go offline but is still marked as online\n",
1554 GNUNET_i2s (rps_peers[entry->index].peer_id));
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "peer %s probably went offline as expected\n",
1561 GNUNET_i2s (rps_peers[entry->index].peer_id));
1563 rps_peers[entry->index].online = GNUNET_NO;
1566 else if (PEER_GO_ONLINE < entry->delta)
1567 { /* Peer hopefully just went online */
1568 if (GNUNET_NO != rps_peers[entry->index].online)
1570 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1571 "peer %s was expected to go online but is still marked as offline\n",
1572 GNUNET_i2s (rps_peers[entry->index].peer_id));
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1578 "peer %s probably went online as expected\n",
1579 GNUNET_i2s (rps_peers[entry->index].peer_id));
1580 if (NULL != cur_test_run.pre_test)
1582 cur_test_run.pre_test (&rps_peers[entry->index],
1583 rps_peers[entry->index].rps_handle);
1584 schedule_missing_requests (&rps_peers[entry->index]);
1587 rps_peers[entry->index].online = GNUNET_YES;
1591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1592 "Invalid value for delta: %i\n", entry->delta);
1596 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1597 rps_peers[entry->index].entry_op_manage = NULL;
1598 GNUNET_free (entry);
1599 //if (num_peers_in_round[current_round] == peers_running)
1604 * @brief Set the rps-service up or down for a specific peer
1606 * @param i index of action
1607 * @param j index of peer
1608 * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1609 * @param prob_go_on_off the probability of the action
1612 manage_service_wrapper (unsigned int i, unsigned int j,
1613 enum PEER_ONLINE_DELTA delta,
1614 double prob_go_on_off)
1616 struct OpListEntry *entry = NULL;
1619 /* make sure that management operation is not already scheduled */
1620 if (NULL != rps_peers[j].entry_op_manage)
1625 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1628 "%u. selected peer (%u: %s) is %s.\n",
1631 GNUNET_i2s (rps_peers[j].peer_id),
1632 (PEER_GO_ONLINE == delta) ? "online" : "offline");
1633 if (prob < prob_go_on_off * UINT32_MAX)
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1637 GNUNET_i2s (rps_peers[j].peer_id),
1638 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1640 if (PEER_GO_OFFLINE == delta)
1641 cancel_pending_req_rep (&rps_peers[j]);
1642 entry = make_oplist_entry ();
1643 entry->delta = delta;
1645 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1650 (PEER_GO_OFFLINE == delta) ? 0 : 1);
1651 rps_peers[j].entry_op_manage = entry;
1661 double portion_online;
1662 unsigned int *permut;
1663 double prob_go_offline;
1664 double portion_go_online;
1665 double portion_go_offline;
1667 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1672 "Churn function executing\n");
1674 churn_task = NULL; /* Should be invalid by now */
1676 /* Compute the probability for an online peer to go offline
1678 portion_online = num_peers_online * 1.0 / num_peers;
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680 "Portion online: %f\n",
1682 portion_go_online = ((1 - portion_online) * .5 * .66);
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 "Portion that should go online: %f\n",
1686 portion_go_offline = (portion_online + portion_go_online) - .75;
1687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1688 "Portion that probably goes offline: %f\n",
1689 portion_go_offline);
1690 prob_go_offline = portion_go_offline / (portion_online * .5);
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Probability of a selected online peer to go offline: %f\n",
1695 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1696 (unsigned int) num_peers);
1698 /* Go over 50% randomly chosen peers */
1699 for (i = 0; i < .5 * num_peers; i++)
1703 /* If online, shut down with certain probability */
1704 if (GNUNET_YES == rps_peers[j].online)
1706 manage_service_wrapper (i, j, -1, prob_go_offline);
1709 /* If offline, restart with certain probability */
1710 else if (GNUNET_NO == rps_peers[j].online)
1712 manage_service_wrapper (i, j, 1, 0.66);
1716 GNUNET_free (permut);
1718 churn_task = GNUNET_SCHEDULER_add_delayed (
1719 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1726 * Initialise given RPSPeer
1728 static void profiler_init_peer (struct RPSPeer *rps_peer)
1730 if (num_peers - 1 == rps_peer->index)
1731 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1736 * Callback to call on receipt of a reply
1738 * @param cls closure
1739 * @param n number of peers
1740 * @param recv_peers the received peers
1743 profiler_reply_handle (void *cls,
1745 const struct GNUNET_PeerIdentity *recv_peers)
1747 struct RPSPeer *rps_peer;
1748 struct RPSPeer *rcv_rps_peer;
1752 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1754 rps_peer = pending_rep->rps_peer;
1755 file_name = "/tmp/rps/received_ids";
1756 file_name_dh = "/tmp/rps/diehard_input";
1757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1758 "[%s] got %" PRIu64 " peers:\n",
1759 GNUNET_i2s (rps_peer->peer_id),
1761 for (i = 0; i < n; i++)
1763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766 GNUNET_i2s (&recv_peers[i]));
1769 GNUNET_i2s_full (&recv_peers[i]));
1770 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1771 GNUNET_assert (NULL != rcv_rps_peer);
1772 tofile (file_name_dh,
1774 (uint32_t) rcv_rps_peer->index);
1776 default_reply_handle (cls, n, recv_peers);
1781 profiler_cb (struct RPSPeer *rps_peer)
1783 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1789 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1792 "Starting churn task\n");
1793 churn_task = GNUNET_SCHEDULER_add_delayed (
1794 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1799 "Not starting churn task\n");
1802 /* Only request peer ids at one peer.
1803 * (It's the before-last because last one is target of the focussed attack.)
1805 if (eval_peer == rps_peer)
1806 schedule_missing_requests (rps_peer);
1810 * Function called from #profiler_eval with a filename.
1812 * @param cls closure
1813 * @param filename complete filename (absolute path)
1814 * @return #GNUNET_OK to continue to iterate,
1815 * #GNUNET_NO to stop iteration with no error,
1816 * #GNUNET_SYSERR to abort iteration with error!
1819 file_name_cb (void *cls, const char *filename)
1821 if (NULL != strstr (filename, "sampler_el"))
1823 struct RPS_SamplerElement *s_elem;
1824 struct GNUNET_CRYPTO_AuthKey auth_key;
1825 const char *key_char;
1828 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1829 tofile (filename, "--------------------------\n");
1831 auth_key = string_to_auth_key (key_char);
1832 s_elem = RPS_sampler_elem_create ();
1833 RPS_sampler_elem_set (s_elem, auth_key);
1835 for (i = 0; i < num_peers; i++)
1837 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1839 RPS_sampler_elem_destroy (s_elem);
1845 * This is run after the test finished.
1847 * Compute all perfect samples.
1850 profiler_eval (void)
1852 /* Compute perfect sample for each sampler element */
1853 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1855 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1861 static uint32_t fac (uint32_t x)
1867 return x * fac (x - 1);
1870 static uint32_t binom (uint32_t n, uint32_t k)
1872 //GNUNET_assert (n >= k);
1873 if (k > n) return 0;
1874 if (0 > n) return 0;
1875 if (0 > k) return 0;
1876 if (0 == k) return 1;
1879 fac(k) * fac(n - k);
1883 * @brief is b in view of a?
1890 static int is_in_view (uint32_t a, uint32_t b)
1893 for (i = 0; i < rps_peers[a].cur_view_count; i++)
1895 if (0 == memcmp (rps_peers[b].peer_id,
1896 &rps_peers[a].cur_view[i],
1897 sizeof (struct GNUNET_PeerIdentity)))
1905 static uint32_t get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
1909 for (i = 0; i < num_peers; i++)
1911 if (0 == memcmp (pid,
1912 rps_peers[i].peer_id,
1913 sizeof (struct GNUNET_PeerIdentity)))
1918 //return 0; /* Should not happen - make compiler happy */
1919 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1920 "No known _PeerIdentity %s!\n",
1921 GNUNET_i2s_full (pid));
1926 * @brief Counts number of peers in view of a that have b in their view
1933 static uint32_t count_containing_views (uint32_t a, uint32_t b)
1939 for (i = 0; i < rps_peers[a].cur_view_count; i++)
1941 peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
1942 if (GNUNET_YES == is_in_view (peer_idx, b))
1951 * @brief Computes the probability for each other peer to be selected by the
1952 * sampling process based on the views of all peers
1954 * @param peer_idx index of the peer that is about to sample
1956 static void compute_probabilities (uint32_t peer_idx)
1958 //double probs[num_peers] = { 0 };
1959 double probs[num_peers];
1960 size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof (char);
1961 char *probs_as_str = GNUNET_malloc (probs_as_str_size);
1962 char *probs_as_str_cpy;
1967 uint32_t cont_views;
1968 uint32_t number_of_being_in_pull_events;
1970 uint32_t count_non_zero_prob = 0;
1972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1973 "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
1974 /* Firstly without knowledge of old views */
1975 for (i = 0; i < num_peers; i++)
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1978 "\tfor peer %" PRIu32 ":\n", i);
1979 view_size = rps_peers[i].cur_view_count;
1980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1981 "\t\tview_size: %" PRIu32 "\n", view_size);
1982 /* For peer i the probability of being sampled is
1983 * evenly distributed among all possibly observed peers. */
1984 /* We could have observed a peer in three cases:
1985 * 1. peer sent a push
1986 * 2. peer was contained in a pull reply
1987 * 3. peer was in history (sampler) - ignored for now */
1988 /* 1. Probability of having received a push from peer i */
1989 if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
1990 (1 <= (0.45 * view_size)))
1992 prob_push = 1.0 * binom (0.45 * view_size, 1)
1994 binom (view_size, 0.45 * view_size);
1995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1996 "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2001 "\t\tposs choices from view: %" PRIu32 ", containing i: %" PRIu32 "\n",
2002 binom (view_size, 0.45 * view_size),
2003 binom (0.45 * view_size, 1));
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007 "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
2011 /* 2. Probability of peer i being contained in pulls */
2012 view_size = rps_peers[peer_idx].cur_view_count;
2013 cont_views = count_containing_views (peer_idx, i);
2014 number_of_being_in_pull_events =
2015 (binom (view_size, 0.45 * view_size) -
2016 binom (view_size - cont_views, 0.45 * view_size));
2017 if (0 != number_of_being_in_pull_events)
2019 prob_pull = number_of_being_in_pull_events
2021 (1.0 * binom (view_size, 0.45 * view_size));
2026 probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
2027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2028 "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
2029 " peers in its view who know %" PRIu32 " prob: %f\n",
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2036 "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
2037 binom (view_size, 0.45 * view_size));
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2039 "\t\tnumber of possible pull combinations without %" PRIu32
2042 binom (view_size - cont_views, 0.45 * view_size));
2043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2044 "\t\tnumber of possible pull combinations with %" PRIu32
2047 number_of_being_in_pull_events);
2049 if (0 != probs[i]) count_non_zero_prob++;
2052 if (0 != count_non_zero_prob)
2054 for (i = 0; i < num_peers; i++)
2056 probs[i] = probs[i] * (1.0 / count_non_zero_prob);
2059 for (i = 0; i < num_peers; i++)
2065 for (i = 0; i < num_peers; i++)
2067 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2068 tmp = GNUNET_snprintf (probs_as_str,
2070 "%s %7.6f", probs_as_str_cpy, probs[i]);
2071 GNUNET_free (probs_as_str_cpy);
2072 GNUNET_assert (0 <= tmp);
2075 to_file_w_len (rps_peers[peer_idx].file_name_probs,
2078 GNUNET_free (probs_as_str);
2082 * @brief This counts the number of peers in which views a given peer occurs.
2084 * It also stores this value in the rps peer.
2086 * @param peer_idx the index of the peer to count the representation
2088 * @return the number of occurrences
2090 static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
2095 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2097 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2099 if (0 == memcmp (rps_peers[peer_idx].peer_id,
2100 &rps_peers[i].cur_view[j],
2101 sizeof (struct GNUNET_PeerIdentity)))
2108 rps_peers[peer_idx].count_in_views = count;
2112 static uint32_t cumulated_view_sizes ()
2117 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2119 view_sizes += rps_peers[i].cur_view_count;
2124 static void count_peer_in_views (uint32_t *count_peers)
2128 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2130 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2132 if (0 == memcmp (rps_peers[i].peer_id,
2133 &rps_peers[i].cur_view[j],
2134 sizeof (struct GNUNET_PeerIdentity)))
2142 void compute_diversity ()
2145 /* ith entry represents the numer of occurrences in other peer's views */
2146 uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
2147 uint32_t views_total_size;
2149 /* deviation from expected number of peers */
2150 double *deviation = GNUNET_new_array (num_peers, double);
2152 views_total_size = 0;
2155 /* For each peer count its representation in other peer's views*/
2156 for (i = 0; i < num_peers; i++) /* Peer to count */
2158 views_total_size += rps_peers[i].cur_view_count;
2159 count_peer_in_views (count_peers);
2160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2161 "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32"\n",
2163 GNUNET_i2s (rps_peers[i].peer_id),
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168 "size of all views combined: %" PRIu32 "\n",
2170 expected = ((double) 1/num_peers) * views_total_size;
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 "Expected number of occurrences of each peer in all views: %f\n",
2174 for (i = 0; i < num_peers; i++) /* Peer to count */
2176 deviation[i] = expected - count_peers[i];
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2178 "Deviation from expectation: %f\n", deviation[i]);
2180 GNUNET_free (count_peers);
2181 GNUNET_free (deviation);
2184 void print_view_sizes()
2188 for (i = 0; i < num_peers; i++) /* Peer to count */
2190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2191 "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
2193 GNUNET_i2s (rps_peers[i].peer_id),
2194 rps_peers[i].cur_view_count);
2198 void all_views_updated_cb()
2200 compute_diversity();
2204 void view_update_cb (void *cls,
2206 const struct GNUNET_PeerIdentity *peers)
2208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2209 "View was updated (%" PRIu64 ")\n", view_size);
2210 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
2211 to_file ("/tmp/rps/view_sizes.txt",
2212 "%" PRIu64 " %" PRIu32 "",
2215 for (int i = 0; i < view_size; i++)
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2218 "\t%s\n", GNUNET_i2s (&peers[i]));
2220 GNUNET_array_grow (rps_peer->cur_view,
2221 rps_peer->cur_view_count,
2223 //*rps_peer->cur_view = *peers;
2224 GNUNET_memcpy (rps_peer->cur_view,
2226 view_size * sizeof (struct GNUNET_PeerIdentity));
2227 to_file ("/tmp/rps/count_in_views.txt",
2228 "%" PRIu64 " %" PRIu32 "",
2230 count_peer_in_views_2 (rps_peer->index));
2231 cumulated_view_sizes();
2234 to_file ("/tmp/rps/repr.txt",
2235 "%" PRIu64 /* index */
2236 " %" PRIu32 /* occurrence in views */
2237 " %" PRIu32 /* view sizes */
2238 " %f" /* fraction of repr in views */
2239 " %f" /* average view size */
2240 " %f" /* prob of occurrence in view slot */
2241 " %f" "", /* exp frac of repr in views */
2243 count_peer_in_views_2 (rps_peer->index),
2245 count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
2246 view_sizes / (view_size * 1.0), /* average view size */
2247 1.0 /view_size, /* prob of occurrence in view slot */
2248 (1.0/view_size) * (view_sizes/view_size) /* expected fraction of repr in views */
2251 compute_probabilities (rps_peer->index);
2252 all_views_updated_cb();
2256 pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
2258 rps_peer->file_name_probs =
2259 store_prefix_file_name (rps_peer->peer_id, "probs");
2260 GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
2263 void write_final_stats (void){
2266 for (i = 0; i < num_peers; i++)
2268 to_file ("/tmp/rps/final_stats.dat",
2269 "%" PRIu32 " " /* index */
2271 PRIu64 " %" /* rounds */
2272 PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" /* blocking */
2273 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* issued */
2274 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* sent */
2275 PRIu64 " %" PRIu64 " %" PRIu64 /* recv */,
2277 GNUNET_i2s (rps_peers[i].peer_id),
2278 rps_peers[i].num_rounds,
2279 rps_peers[i].num_blocks,
2280 rps_peers[i].num_blocks_many_push,
2281 rps_peers[i].num_blocks_no_push,
2282 rps_peers[i].num_blocks_no_pull,
2283 rps_peers[i].num_blocks_many_push_no_pull,
2284 rps_peers[i].num_blocks_no_push_no_pull,
2285 rps_peers[i].num_issued_push,
2286 rps_peers[i].num_issued_pull_req,
2287 rps_peers[i].num_issued_pull_rep,
2288 rps_peers[i].num_sent_push,
2289 rps_peers[i].num_sent_pull_req,
2290 rps_peers[i].num_sent_pull_rep,
2291 rps_peers[i].num_recv_push,
2292 rps_peers[i].num_recv_pull_req,
2293 rps_peers[i].num_recv_pull_rep);
2298 * Continuation called by #GNUNET_STATISTICS_get() functions.
2300 * Remembers that this specific statistics value was received for this peer.
2301 * Checks whether all peers received their statistics yet.
2302 * Issues the shutdown.
2304 * @param cls closure
2305 * @param success #GNUNET_OK if statistics were
2306 * successfully obtained, #GNUNET_SYSERR if not.
2309 post_test_shutdown_ready_cb (void *cls,
2312 struct STATcls *stat_cls = (struct STATcls *) cls;
2313 struct RPSPeer *rps_peer = stat_cls->rps_peer;
2314 if (GNUNET_OK == success)
2316 /* set flag that we we got the value */
2317 rps_peer->stat_collected_flags |= stat_cls->stat_type;
2319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2320 "Peer %u did not receive statistics value\n",
2322 GNUNET_free (stat_cls);
2326 if (NULL != rps_peer->stat_op &&
2327 GNUNET_YES == check_statistics_collect_completed_single_peer (rps_peer))
2329 GNUNET_TESTBED_operation_done (rps_peer->stat_op);
2332 write_final_stats ();
2333 if (GNUNET_YES == check_statistics_collect_completed())
2335 //write_final_stats ();
2336 GNUNET_free (stat_cls);
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2339 GNUNET_SCHEDULER_shutdown ();
2341 GNUNET_free (stat_cls);
2346 * @brief Converts string representation to the corresponding #STAT_TYPE enum.
2348 * @param stat_str string representation of statistics specifier
2350 * @return corresponding enum
2352 enum STAT_TYPE stat_str_2_type (const char *stat_str)
2354 if (0 == strncmp ("# rounds blocked - no pull replies", stat_str, strlen ("# rounds blocked - no pull replies")))
2356 return STAT_TYPE_BLOCKS_NO_PULL;
2358 else if (0 == strncmp ("# rounds blocked - too many pushes, no pull replies", stat_str, strlen ("# rounds blocked - too many pushes, no pull replies")))
2360 return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
2362 else if (0 == strncmp ("# rounds blocked - too many pushes", stat_str, strlen ("# rounds blocked - too many pushes")))
2364 return STAT_TYPE_BLOCKS_MANY_PUSH;
2366 else if (0 == strncmp ("# rounds blocked - no pushes, no pull replies", stat_str, strlen ("# rounds blocked - no pushes, no pull replies")))
2368 return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
2370 else if (0 == strncmp ("# rounds blocked - no pushes", stat_str, strlen ("# rounds blocked - no pushes")))
2372 return STAT_TYPE_BLOCKS_NO_PUSH;
2374 else if (0 == strncmp ("# rounds blocked", stat_str, strlen ("# rounds blocked")))
2376 return STAT_TYPE_BLOCKS;
2378 else if (0 == strncmp ("# rounds", stat_str, strlen ("# rounds")))
2380 return STAT_TYPE_ROUNDS;
2382 else if (0 == strncmp ("# push send issued", stat_str, strlen ("# push send issued")))
2384 return STAT_TYPE_ISSUED_PUSH_SEND;
2386 else if (0 == strncmp ("# pull request send issued", stat_str, strlen ("# pull request send issued")))
2388 return STAT_TYPE_ISSUED_PULL_REQ;
2390 else if (0 == strncmp ("# pull reply send issued", stat_str, strlen ("# pull reply send issued")))
2392 return STAT_TYPE_ISSUED_PULL_REP;
2394 else if (0 == strncmp ("# pushes sent", stat_str, strlen ("# pushes sent")))
2396 return STAT_TYPE_SENT_PUSH_SEND;
2398 else if (0 == strncmp ("# pull requests sent", stat_str, strlen ("# pull requests sent")))
2400 return STAT_TYPE_SENT_PULL_REQ;
2402 else if (0 == strncmp ("# pull replys sent", stat_str, strlen ("# pull replys sent")))
2404 return STAT_TYPE_SENT_PULL_REP;
2406 else if (0 == strncmp ("# push message received", stat_str, strlen ("# push message received")))
2408 return STAT_TYPE_RECV_PUSH_SEND;
2410 else if (0 == strncmp ("# pull request message received", stat_str, strlen ("# pull request message received")))
2412 return STAT_TYPE_RECV_PULL_REQ;
2414 else if (0 == strncmp ("# pull reply messages received", stat_str, strlen ("# pull reply messages received")))
2416 return STAT_TYPE_RECV_PULL_REP;
2418 return STAT_TYPE_MAX;
2423 * @brief Converts #STAT_TYPE enum to the equivalent string representation that
2424 * is stored with the statistics service.
2426 * @param stat_type #STAT_TYPE enum
2428 * @return string representation that matches statistics value
2430 char* stat_type_2_str (enum STAT_TYPE stat_type)
2434 case STAT_TYPE_ROUNDS:
2436 case STAT_TYPE_BLOCKS:
2437 return "# rounds blocked";
2438 case STAT_TYPE_BLOCKS_MANY_PUSH:
2439 return "# rounds blocked - too many pushes";
2440 case STAT_TYPE_BLOCKS_NO_PUSH:
2441 return "# rounds blocked - no pushes";
2442 case STAT_TYPE_BLOCKS_NO_PULL:
2443 return "# rounds blocked - no pull replies";
2444 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2445 return "# rounds blocked - too many pushes, no pull replies";
2446 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2447 return "# rounds blocked - no pushes, no pull replies";
2448 case STAT_TYPE_ISSUED_PUSH_SEND:
2449 return "# push send issued";
2450 case STAT_TYPE_ISSUED_PULL_REQ:
2451 return "# pull request send issued";
2452 case STAT_TYPE_ISSUED_PULL_REP:
2453 return "# pull reply send issued";
2454 case STAT_TYPE_SENT_PUSH_SEND:
2455 return "# pushes sent";
2456 case STAT_TYPE_SENT_PULL_REQ:
2457 return "# pull requests sent";
2458 case STAT_TYPE_SENT_PULL_REP:
2459 return "# pull replys sent";
2460 case STAT_TYPE_RECV_PUSH_SEND:
2461 return "# push message received";
2462 case STAT_TYPE_RECV_PULL_REQ:
2463 return "# pull request message received";
2464 case STAT_TYPE_RECV_PULL_REP:
2465 return "# pull reply messages received";
2474 * Callback function to process statistic values.
2476 * @param cls closure
2477 * @param subsystem name of subsystem that created the statistic
2478 * @param name the name of the datum
2479 * @param value the current value
2480 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
2481 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
2484 stat_iterator (void *cls,
2485 const char *subsystem,
2490 const struct STATcls *stat_cls = (const struct STATcls *) cls;
2491 struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
2492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got stat value: %s - %" PRIu64 "\n",
2493 //stat_type_2_str (stat_cls->stat_type),
2496 to_file (rps_peer->file_name_stats,
2497 "%s: %" PRIu64 "\n",
2500 switch (stat_str_2_type (name))
2502 case STAT_TYPE_ROUNDS:
2503 rps_peer->num_rounds = value;
2505 case STAT_TYPE_BLOCKS:
2506 rps_peer->num_blocks = value;
2508 case STAT_TYPE_BLOCKS_MANY_PUSH:
2509 rps_peer->num_blocks_many_push = value;
2511 case STAT_TYPE_BLOCKS_NO_PUSH:
2512 rps_peer->num_blocks_no_push = value;
2514 case STAT_TYPE_BLOCKS_NO_PULL:
2515 rps_peer->num_blocks_no_pull = value;
2517 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2518 rps_peer->num_blocks_many_push_no_pull = value;
2520 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2521 rps_peer->num_blocks_no_push_no_pull = value;
2523 case STAT_TYPE_ISSUED_PUSH_SEND:
2524 rps_peer->num_issued_push = value;
2526 case STAT_TYPE_ISSUED_PULL_REQ:
2527 rps_peer->num_issued_pull_req = value;
2529 case STAT_TYPE_ISSUED_PULL_REP:
2530 rps_peer->num_issued_pull_rep = value;
2532 case STAT_TYPE_SENT_PUSH_SEND:
2533 rps_peer->num_sent_push = value;
2535 case STAT_TYPE_SENT_PULL_REQ:
2536 rps_peer->num_sent_pull_req = value;
2538 case STAT_TYPE_SENT_PULL_REP:
2539 rps_peer->num_sent_pull_rep = value;
2541 case STAT_TYPE_RECV_PUSH_SEND:
2542 rps_peer->num_recv_push = value;
2544 case STAT_TYPE_RECV_PULL_REQ:
2545 rps_peer->num_recv_pull_req = value;
2547 case STAT_TYPE_RECV_PULL_REP:
2548 rps_peer->num_recv_pull_rep = value;
2552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2553 "Unknown statistics string: %s\n",
2560 void post_profiler (struct RPSPeer *rps_peer)
2562 if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2568 "Going to request statistic values with mask 0x%" PRIx32 "\n",
2569 cur_test_run.stat_collect_flags);
2571 struct STATcls *stat_cls;
2573 for (stat_type = STAT_TYPE_ROUNDS;
2574 stat_type < STAT_TYPE_MAX;
2575 stat_type = stat_type <<1)
2577 if (stat_type & cur_test_run.stat_collect_flags)
2579 stat_cls = GNUNET_malloc (sizeof (struct STATcls));
2580 stat_cls->rps_peer = rps_peer;
2581 stat_cls->stat_type = stat_type;
2582 rps_peer->file_name_stats =
2583 store_prefix_file_name (rps_peer->peer_id, "stats");
2584 GNUNET_STATISTICS_get (rps_peer->stats_h,
2586 stat_type_2_str (stat_type),
2587 post_test_shutdown_ready_cb,
2589 (struct STATcls *) stat_cls);
2590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2591 "Requested statistics for %s (peer %" PRIu32 ")\n",
2592 stat_type_2_str (stat_type),
2599 /***********************************************************************
2600 * /Definition of tests
2601 ***********************************************************************/
2605 * Actual "main" function for the testcase.
2607 * @param cls closure
2608 * @param h the run handle
2609 * @param n_peers number of peers in 'peers'
2610 * @param peers handle to peers run in the testbed
2611 * @param links_succeeded the number of overlay link connection attempts that
2613 * @param links_failed the number of overlay link connection attempts that
2618 struct GNUNET_TESTBED_RunHandle *h,
2619 unsigned int n_peers,
2620 struct GNUNET_TESTBED_Peer **peers,
2621 unsigned int links_succeeded,
2622 unsigned int links_failed)
2625 struct OpListEntry *entry;
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
2629 /* Check whether we timed out */
2630 if (n_peers != num_peers ||
2632 0 == links_succeeded)
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
2635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
2638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
2639 GNUNET_SCHEDULER_shutdown ();
2644 /* Initialize peers */
2645 testbed_peers = peers;
2646 num_peers_online = 0;
2647 for (i = 0; i < num_peers; i++)
2649 entry = make_oplist_entry ();
2651 rps_peers[i].index = i;
2652 if (NULL != cur_test_run.init_peer)
2653 cur_test_run.init_peer (&rps_peers[i]);
2654 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2656 rps_peers->cur_view_count = 0;
2657 rps_peers->cur_view = NULL;
2659 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2660 GNUNET_TESTBED_PIT_IDENTITY,
2665 /* Bring peers up */
2666 GNUNET_assert (num_peers == n_peers);
2667 for (i = 0; i < n_peers; i++)
2669 rps_peers[i].index = i;
2671 GNUNET_TESTBED_service_connect (&rps_peers[i],
2674 &rps_connect_complete_cb,
2676 &rps_connect_adapter,
2677 &rps_disconnect_adapter,
2679 /* Connect all peers to statistics service */
2680 if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2682 rps_peers[i].stat_op =
2683 GNUNET_TESTBED_service_connect (NULL,
2688 &stat_connect_adapter,
2689 &stat_disconnect_adapter,
2694 if (NULL != churn_task)
2695 GNUNET_SCHEDULER_cancel (churn_task);
2696 post_test_task = GNUNET_SCHEDULER_add_delayed (timeout, &post_test_op, NULL);
2697 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2698 (timeout_s * 1.2) + 0.1 * num_peers);
2699 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
2700 shutdown_task = GNUNET_SCHEDULER_add_shutdown (shutdown_op, NULL);
2706 * Entry point for the testcase, sets up the testbed.
2708 * @param argc unused
2709 * @param argv unused
2710 * @return 0 on success
2713 main (int argc, char *argv[])
2717 /* Defaults for tests */
2719 cur_test_run.name = "test-rps-default";
2720 cur_test_run.init_peer = default_init_peer;
2721 cur_test_run.pre_test = NULL;
2722 cur_test_run.reply_handle = default_reply_handle;
2723 cur_test_run.eval_cb = default_eval_cb;
2724 cur_test_run.post_test = NULL;
2725 cur_test_run.have_churn = HAVE_CHURN;
2726 cur_test_run.have_collect_statistics = NO_COLLECT_STATISTICS;
2727 cur_test_run.stat_collect_flags = 0;
2728 cur_test_run.have_collect_view = NO_COLLECT_VIEW;
2732 if (strstr (argv[0], "malicious") != NULL)
2734 cur_test_run.pre_test = mal_pre;
2735 cur_test_run.main_test = mal_cb;
2736 cur_test_run.init_peer = mal_init_peer;
2738 if (strstr (argv[0], "_1") != NULL)
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
2741 cur_test_run.name = "test-rps-malicious_1";
2744 else if (strstr (argv[0], "_2") != NULL)
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
2747 cur_test_run.name = "test-rps-malicious_2";
2750 else if (strstr (argv[0], "_3") != NULL)
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
2753 cur_test_run.name = "test-rps-malicious_3";
2758 else if (strstr (argv[0], "_single_req") != NULL)
2760 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
2761 cur_test_run.name = "test-rps-single-req";
2762 cur_test_run.main_test = single_req_cb;
2763 cur_test_run.have_churn = HAVE_NO_CHURN;
2766 else if (strstr (argv[0], "_delayed_reqs") != NULL)
2768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
2769 cur_test_run.name = "test-rps-delayed-reqs";
2770 cur_test_run.main_test = delay_req_cb;
2771 cur_test_run.have_churn = HAVE_NO_CHURN;
2774 else if (strstr (argv[0], "_seed_big") != NULL)
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
2778 cur_test_run.name = "test-rps-seed-big";
2779 cur_test_run.main_test = seed_big_cb;
2780 cur_test_run.eval_cb = no_eval;
2781 cur_test_run.have_churn = HAVE_NO_CHURN;
2785 else if (strstr (argv[0], "_single_peer_seed") != NULL)
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
2788 cur_test_run.name = "test-rps-single-peer-seed";
2789 cur_test_run.main_test = single_peer_seed_cb;
2790 cur_test_run.have_churn = HAVE_NO_CHURN;
2793 else if (strstr (argv[0], "_seed_request") != NULL)
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
2796 cur_test_run.name = "test-rps-seed-request";
2797 cur_test_run.main_test = seed_req_cb;
2798 cur_test_run.have_churn = HAVE_NO_CHURN;
2801 else if (strstr (argv[0], "_seed") != NULL)
2803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
2804 cur_test_run.name = "test-rps-seed";
2805 cur_test_run.main_test = seed_cb;
2806 cur_test_run.eval_cb = no_eval;
2807 cur_test_run.have_churn = HAVE_NO_CHURN;
2810 else if (strstr (argv[0], "_req_cancel") != NULL)
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
2813 cur_test_run.name = "test-rps-req-cancel";
2815 cur_test_run.main_test = req_cancel_cb;
2816 cur_test_run.eval_cb = no_eval;
2817 cur_test_run.have_churn = HAVE_NO_CHURN;
2821 else if (strstr (argv[0], "_churn") != NULL)
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
2824 cur_test_run.name = "test-rps-churn";
2826 cur_test_run.init_peer = default_init_peer;
2827 cur_test_run.main_test = churn_test_cb;
2828 cur_test_run.reply_handle = default_reply_handle;
2829 cur_test_run.eval_cb = default_eval_cb;
2830 cur_test_run.have_churn = HAVE_NO_CHURN;
2831 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
2835 else if (strstr (argv[0], "profiler") != NULL)
2837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
2838 cur_test_run.name = "test-rps-profiler";
2841 cur_test_run.init_peer = profiler_init_peer;
2842 //cur_test_run.pre_test = mal_pre;
2843 cur_test_run.pre_test = pre_profiler;
2844 cur_test_run.main_test = profiler_cb;
2845 cur_test_run.reply_handle = profiler_reply_handle;
2846 cur_test_run.eval_cb = profiler_eval;
2847 cur_test_run.post_test = post_profiler;
2848 cur_test_run.request_interval = 2;
2849 cur_test_run.num_requests = 5;
2850 //cur_test_run.have_churn = HAVE_CHURN;
2851 cur_test_run.have_churn = HAVE_NO_CHURN;
2852 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
2853 cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
2854 cur_test_run.stat_collect_flags = STAT_TYPE_ROUNDS |
2856 STAT_TYPE_BLOCKS_MANY_PUSH |
2857 STAT_TYPE_BLOCKS_NO_PUSH |
2858 STAT_TYPE_BLOCKS_NO_PULL |
2859 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL |
2860 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL |
2861 STAT_TYPE_ISSUED_PUSH_SEND |
2862 STAT_TYPE_ISSUED_PULL_REQ |
2863 STAT_TYPE_ISSUED_PULL_REP |
2864 STAT_TYPE_SENT_PUSH_SEND |
2865 STAT_TYPE_SENT_PULL_REQ |
2866 STAT_TYPE_SENT_PULL_REP |
2867 STAT_TYPE_RECV_PUSH_SEND |
2868 STAT_TYPE_RECV_PULL_REQ |
2869 STAT_TYPE_RECV_PULL_REP;
2870 cur_test_run.have_collect_view = COLLECT_VIEW;
2873 /* 'Clean' directory */
2874 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
2875 GNUNET_DISK_directory_create ("/tmp/rps/");
2877 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_s);
2879 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
2880 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
2881 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
2882 if ( (2 == mal_type) ||
2884 target_peer = &rps_peer_ids[num_peers - 2];
2885 if (profiler_eval == cur_test_run.eval_cb)
2886 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
2887 malicious peer if not careful
2888 with the malicious portion */
2891 ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
2896 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2897 "_test_run returned.\n");
2898 if (GNUNET_OK != ret_value)
2900 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2901 "Test did not run successfully!\n");
2904 ret_value = cur_test_run.eval_cb();
2905 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2907 GNUNET_array_grow (rps_peers->cur_view,
2908 rps_peers->cur_view_count,
2911 GNUNET_free (rps_peers);
2912 GNUNET_free (rps_peer_ids);
2913 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2917 /* end of test_rps.c */