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/>.
19 * @file rps/test_rps.c
20 * @brief Testcase for the random peer sampling service. Starts
21 * a peergroup with a given number of peers, then waits to
22 * receive size pushes/pulls from each peer. Expects to wait
23 * for one message from each peer.
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testbed_service.h"
29 #include "gnunet_rps_service.h"
30 #include "rps-test_util.h"
31 #include "gnunet-service-rps_sampler_elem.h"
37 * How many peers do we start?
39 static uint32_t num_peers;
42 * How long do we run the test?
45 static uint32_t timeout_s;
48 * How long do we run the test?
50 //#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
51 static struct GNUNET_TIME_Relative timeout;
55 * Portion of malicious peers
57 static double portion = .1;
60 * Type of malicious peer to test
62 static unsigned int mal_type = 0;
65 * Handles to all of the running peers
67 static struct GNUNET_TESTBED_Peer **testbed_peers;
70 * @brief Indicates whether peer should go off- or online
72 enum PEER_ONLINE_DELTA {
74 * @brief Indicates peer going online
78 * @brief Indicates peer going offline
91 struct OpListEntry *next;
96 struct OpListEntry *prev;
99 * The testbed operation
101 struct GNUNET_TESTBED_Operation *op;
104 * Depending on whether we start or stop RPS service at the peer, set this to
105 * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
107 enum PEER_ONLINE_DELTA delta;
110 * Index of the regarding peer
118 static struct OpListEntry *oplist_head;
123 static struct OpListEntry *oplist_tail;
127 * A pending reply: A request was sent and the reply is pending.
134 struct PendingReply *next;
135 struct PendingReply *prev;
138 * Handle to the request we are waiting for
140 struct GNUNET_RPS_Request_Handle *req_handle;
143 * The peer that requested
145 struct RPSPeer *rps_peer;
150 * A pending request: A request was not made yet but is scheduled for later.
152 struct PendingRequest
157 struct PendingRequest *next;
158 struct PendingRequest *prev;
161 * Handle to the request we are waiting for
163 struct GNUNET_SCHEDULER_Task *request_task;
166 * The peer that requested
168 struct RPSPeer *rps_peer;
173 * Information we track for each peer.
183 * Handle for RPS connect operation.
185 struct GNUNET_TESTBED_Operation *op;
188 * Handle to RPS service.
190 struct GNUNET_RPS_Handle *rps_handle;
193 * Handle to stream requests
195 struct GNUNET_RPS_StreamRequestHandle *rps_srh;
200 struct GNUNET_PeerIdentity *peer_id;
203 * A request handle to check for an request
205 //struct GNUNET_RPS_Request_Handle *req_handle;
208 * Peer on- or offline?
213 * Number of Peer IDs to request during the whole test
215 unsigned int num_ids_to_request;
218 * Pending requests DLL
220 struct PendingRequest *pending_req_head;
221 struct PendingRequest *pending_req_tail;
224 * Number of pending requests
226 unsigned int num_pending_reqs;
229 * Pending replies DLL
231 struct PendingReply *pending_rep_head;
232 struct PendingReply *pending_rep_tail;
235 * Number of pending replies
237 unsigned int num_pending_reps;
240 * Number of received PeerIDs
242 unsigned int num_recv_ids;
245 * Pending operation on that peer
247 const struct OpListEntry *entry_op_manage;
250 * Testbed operation to connect to statistics service
252 struct GNUNET_TESTBED_Operation *stat_op;
255 * Handle to the statistics service
257 struct GNUNET_STATISTICS_Handle *stats_h;
260 * @brief flags to indicate which statistics values have been already
261 * collected from the statistics service.
262 * Used to check whether we are able to shutdown.
264 uint32_t stat_collected_flags;
267 * @brief File name of the file the stats are finally written to
269 const char *file_name_stats;
272 * @brief File name of the file the stats are finally written to
274 const char *file_name_probs;
277 * @brief The current view
279 struct GNUNET_PeerIdentity *cur_view;
282 * @brief Number of peers in the #cur_view.
284 uint32_t cur_view_count;
287 * @brief Number of occurrences in other peer's view
289 uint32_t count_in_views;
292 * @brief statistics values
296 uint64_t num_blocks_many_push;
297 uint64_t num_blocks_no_push;
298 uint64_t num_blocks_no_pull;
299 uint64_t num_blocks_many_push_no_pull;
300 uint64_t num_blocks_no_push_no_pull;
301 uint64_t num_issued_push;
302 uint64_t num_issued_pull_req;
303 uint64_t num_issued_pull_rep;
304 uint64_t num_sent_push;
305 uint64_t num_sent_pull_req;
306 uint64_t num_sent_pull_rep;
307 uint64_t num_recv_push;
308 uint64_t num_recv_pull_req;
309 uint64_t num_recv_pull_rep;
314 STAT_TYPE_ROUNDS = 0x1, /* 1 */
315 STAT_TYPE_BLOCKS = 0x2, /* 2 */
316 STAT_TYPE_BLOCKS_MANY_PUSH = 0x4, /* 3 */
317 STAT_TYPE_BLOCKS_NO_PUSH = 0x8, /* 4 */
318 STAT_TYPE_BLOCKS_NO_PULL = 0x10, /* 5 */
319 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL = 0x20, /* 6 */
320 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL = 0x40, /* 7 */
321 STAT_TYPE_ISSUED_PUSH_SEND = 0x80, /* 8 */
322 STAT_TYPE_ISSUED_PULL_REQ = 0x100, /* 9 */
323 STAT_TYPE_ISSUED_PULL_REP = 0x200, /* 10 */
324 STAT_TYPE_SENT_PUSH_SEND = 0x400, /* 11 */
325 STAT_TYPE_SENT_PULL_REQ = 0x800, /* 12 */
326 STAT_TYPE_SENT_PULL_REP = 0x1000, /* 13 */
327 STAT_TYPE_RECV_PUSH_SEND = 0x2000, /* 14 */
328 STAT_TYPE_RECV_PULL_REQ = 0x4000, /* 15 */
329 STAT_TYPE_RECV_PULL_REP = 0x8000, /* 16 */
330 STAT_TYPE_MAX = 0x80000000, /* 32 */
335 struct RPSPeer *rps_peer;
336 enum STAT_TYPE stat_type;
341 * Information for all the peers.
343 static struct RPSPeer *rps_peers;
346 * Peermap to get the index of a given peer ID quick.
348 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
353 static struct GNUNET_PeerIdentity *rps_peer_ids;
356 * ID of the targeted peer.
358 static struct GNUNET_PeerIdentity *target_peer;
361 * ID of the peer that requests for the evaluation.
363 static struct RPSPeer *eval_peer;
366 * Number of online peers.
368 static unsigned int num_peers_online;
371 * @brief The added sizes of the peer's views
373 static unsigned int view_sizes;
376 * Return value from 'main'.
381 * Identifier for the churn task that runs periodically
383 static struct GNUNET_SCHEDULER_Task *post_test_task;
386 * Identifier for the churn task that runs periodically
388 static struct GNUNET_SCHEDULER_Task *shutdown_task;
391 * Identifier for the churn task that runs periodically
393 static struct GNUNET_SCHEDULER_Task *churn_task;
396 * Called to initialise the given RPSPeer
398 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
401 * @brief Called directly after connecting to the service
403 * @param rps_peer Specific peer the function is called on
404 * @param h the handle to the rps service
406 typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
409 * @brief Executes functions to test the api/service for a given peer
411 * Called from within #rps_connect_complete_cb ()
412 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
413 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
415 * @param rps_peer the peer the task runs on
417 typedef void (*MainTest) (struct RPSPeer *rps_peer);
420 * Callback called once the requested random peers are available
422 typedef void (*ReplyHandle) (void *cls,
424 const struct GNUNET_PeerIdentity *recv_peers);
427 * Called directly before disconnecting from the service
429 typedef void (*PostTest) (struct RPSPeer *peer);
432 * Function called after disconnect to evaluate test success
434 typedef int (*EvaluationCallback) (void);
437 * @brief Do we have Churn?
441 * @brief If we have churn this is set
445 * @brief If we have no churn this is set
451 * @brief Is it ok to quit the test before the timeout?
453 enum OPTION_QUICK_QUIT {
455 * @brief It is ok for the test to quit before the timeout triggers
460 * @brief It is NOT ok for the test to quit before the timeout triggers
466 * @brief Do we collect statistics at the end?
468 enum OPTION_COLLECT_STATISTICS {
470 * @brief We collect statistics at the end
475 * @brief We do not collect statistics at the end
477 NO_COLLECT_STATISTICS,
481 * @brief Do we collect views during run?
483 enum OPTION_COLLECT_VIEW {
485 * @brief We collect view during run
490 * @brief We do not collect the view during run
496 * Structure to define a single test
506 * Called with a single peer in order to initialise that peer
511 * Called directly after connecting to the service
516 * Main function for each peer
521 * Callback called once the requested peers are available
523 ReplyHandle reply_handle;
526 * Called directly before disconnecting from the service
531 * Function to evaluate the test results
533 EvaluationCallback eval_cb;
538 uint32_t request_interval;
541 * Number of Requests to make.
543 uint32_t num_requests;
546 * Run with (-out) churn
548 enum OPTION_CHURN have_churn;
551 * Quit test before timeout?
553 enum OPTION_QUICK_QUIT have_quick_quit;
556 * Collect statistics at the end?
558 enum OPTION_COLLECT_STATISTICS have_collect_statistics;
561 * Collect view during run?
563 enum OPTION_COLLECT_VIEW have_collect_view;
566 * @brief Mark which values from the statistics service to collect at the end
569 uint32_t stat_collect_flags;
573 * Did we finish the test?
575 static int post_test;
578 * Are we shutting down?
580 static int in_shutdown;
583 * Append arguments to file
586 tofile_ (const char *file_name, const char *line)
588 struct GNUNET_DISK_FileHandle *f;
589 /* char output_buffer[512]; */
594 if (NULL == (f = GNUNET_DISK_file_open (file_name,
595 GNUNET_DISK_OPEN_APPEND |
596 GNUNET_DISK_OPEN_WRITE |
597 GNUNET_DISK_OPEN_CREATE,
598 GNUNET_DISK_PERM_USER_READ |
599 GNUNET_DISK_PERM_USER_WRITE |
600 GNUNET_DISK_PERM_GROUP_READ |
601 GNUNET_DISK_PERM_OTHER_READ)))
603 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
604 "Not able to open file %s\n",
608 /* size = GNUNET_snprintf (output_buffer,
609 sizeof (output_buffer),
611 GNUNET_TIME_absolute_get ().abs_value_us,
615 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
616 "Failed to write string to buffer (size: %i)\n",
621 size = strlen (line) * sizeof (char);
623 size2 = GNUNET_DISK_file_write (f, line, size);
626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
627 "Unable to write to file! (Size: %lu, size2: %lu)\n",
630 if (GNUNET_YES != GNUNET_DISK_file_close (f))
632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
633 "Unable to close file\n");
638 if (GNUNET_YES != GNUNET_DISK_file_close (f))
640 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
641 "Unable to close file\n");
646 * This function is used to facilitate writing important information to disk
648 #define tofile(file_name, ...) do {\
651 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
654 "Failed to create tmp_buf\n");\
656 tofile_(file_name,tmp_buf);\
661 * Write the ids and their according index in the given array to a file
665 ids_to_file (char *file_name,
666 struct GNUNET_PeerIdentity *peer_ids,
667 unsigned int num_peer_ids)
671 for (i=0 ; i < num_peer_ids ; i++)
676 GNUNET_i2s_full (&peer_ids[i]));
681 * Task run on timeout to collect statistics and potentially shut down.
684 post_test_op (void *cls);
688 * Test the success of a single test
698 for (i = 0; i < num_peers; i++)
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
701 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
703 GNUNET_i2s (rps_peers[i].peer_id),
704 rps_peers[i].num_recv_ids,
705 rps_peers[i].num_ids_to_request,
706 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
707 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
709 return tmp_ok? 0 : 1;
714 * Creates an oplist entry and adds it to the oplist DLL
716 static struct OpListEntry *
719 struct OpListEntry *entry;
721 entry = GNUNET_new (struct OpListEntry);
722 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
728 * @brief Checks if given peer already received its statistics value from the
729 * statistics service.
731 * @param rps_peer the peer to check for
733 * @return #GNUNET_YES if so
734 * #GNUNET_NO otherwise
736 static int check_statistics_collect_completed_single_peer (
737 const struct RPSPeer *rps_peer)
739 if (cur_test_run.stat_collect_flags !=
740 (cur_test_run.stat_collect_flags &
741 rps_peer->stat_collected_flags))
750 * @brief Checks if all peers already received their statistics value from the
751 * statistics service.
753 * @return #GNUNET_YES if so
754 * #GNUNET_NO otherwise
756 static int check_statistics_collect_completed ()
760 for (i = 0; i < num_peers; i++)
762 if (GNUNET_NO == check_statistics_collect_completed_single_peer (&rps_peers[i]))
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "At least Peer %" PRIu32 " did not yet receive all statistics values\n",
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "All peers received their statistics values\n");
777 * Task run on timeout to shut everything down.
780 shutdown_op (void *cls)
785 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
786 "Shutdown task scheduled, going down.\n");
787 in_shutdown = GNUNET_YES;
788 if (NULL != post_test_task)
790 GNUNET_SCHEDULER_cancel (post_test_task);
793 if (NULL != churn_task)
795 GNUNET_SCHEDULER_cancel (churn_task);
798 for (i = 0; i < num_peers; i++)
800 if (NULL != rps_peers[i].rps_handle)
802 GNUNET_RPS_disconnect (rps_peers[i].rps_handle);
804 if (NULL != rps_peers[i].op)
806 GNUNET_TESTBED_operation_done (rps_peers[i].op);
813 * Task run on timeout to collect statistics and potentially shut down.
816 post_test_op (void *cls)
821 post_test_task = NULL;
822 post_test = GNUNET_YES;
823 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
824 "Post test task scheduled, going down.\n");
825 if (NULL != churn_task)
827 GNUNET_SCHEDULER_cancel (churn_task);
830 for (i = 0; i < num_peers; i++)
832 if (NULL != cur_test_run.post_test)
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n", i);
835 cur_test_run.post_test (&rps_peers[i]);
837 if (NULL != rps_peers[i].op)
839 GNUNET_TESTBED_operation_done (rps_peers[i].op);
840 rps_peers[i].op = NULL;
843 /* If we do not collect statistics, shut down directly */
844 if (NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics ||
845 GNUNET_YES == check_statistics_collect_completed())
847 GNUNET_SCHEDULER_shutdown ();
856 seed_peers (void *cls)
858 struct RPSPeer *peer = cls;
862 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
867 GNUNET_assert (NULL != peer->rps_handle);
869 // TODO if malicious don't seed mal peers
870 amount = round (.5 * num_peers);
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
873 for (i = 0 ; i < amount ; i++)
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
876 GNUNET_i2s (&rps_peer_ids[i]));
878 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
886 seed_peers_big (void *cls)
888 struct RPSPeer *peer = cls;
889 unsigned int seed_msg_size;
890 uint32_t num_peers_max;
894 seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
895 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - seed_msg_size) /
896 sizeof (struct GNUNET_PeerIdentity);
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Peers that fit in one seed msg; %u\n",
900 amount = num_peers_max + (0.5 * num_peers_max);
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
902 "Seeding many (%u) peers:\n",
904 struct GNUNET_PeerIdentity ids_to_seed[amount];
905 for (i = 0; i < amount; i++)
907 ids_to_seed[i] = *peer->peer_id;
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
910 GNUNET_i2s (&ids_to_seed[i]));
913 GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
917 * Get the id of peer i.
920 info_cb (void *cb_cls,
921 struct GNUNET_TESTBED_Operation *op,
922 const struct GNUNET_TESTBED_PeerInformation *pinfo,
925 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
928 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
933 if (NULL == pinfo || NULL != emsg)
935 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
936 GNUNET_TESTBED_operation_done (entry->op);
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 GNUNET_i2s (pinfo->result.id));
945 rps_peer_ids[entry->index] = *(pinfo->result.id);
946 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
948 GNUNET_assert (GNUNET_OK ==
949 GNUNET_CONTAINER_multipeermap_put (peer_map,
950 &rps_peer_ids[entry->index],
951 &rps_peers[entry->index],
952 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
953 tofile ("/tmp/rps/peer_ids",
956 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
958 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
959 GNUNET_TESTBED_operation_done (entry->op);
965 * Callback to be called when RPS service connect operation is completed
967 * @param cls the callback closure from functions generating an operation
968 * @param op the operation that has been finished
969 * @param ca_result the RPS service handle returned from rps_connect_adapter
970 * @param emsg error message in case the operation has failed; will be NULL if
971 * operation has executed successfully.
974 rps_connect_complete_cb (void *cls,
975 struct GNUNET_TESTBED_Operation *op,
979 struct RPSPeer *rps_peer = cls;
980 struct GNUNET_RPS_Handle *rps = ca_result;
982 GNUNET_assert (NULL != ca_result);
984 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
989 rps_peer->rps_handle = rps;
990 rps_peer->online = GNUNET_YES;
993 GNUNET_assert (op == rps_peer->op);
996 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
997 "Failed to connect to RPS service: %s\n",
1000 GNUNET_SCHEDULER_shutdown ();
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
1006 cur_test_run.main_test (rps_peer);
1011 * Adapter function called to establish a connection to
1014 * @param cls closure
1015 * @param cfg configuration of the peer to connect to; will be available until
1016 * GNUNET_TESTBED_operation_done() is called on the operation returned
1017 * from GNUNET_TESTBED_service_connect()
1018 * @return service handle to return in 'op_result', NULL on error
1021 rps_connect_adapter (void *cls,
1022 const struct GNUNET_CONFIGURATION_Handle *cfg)
1024 struct GNUNET_RPS_Handle *h;
1026 h = GNUNET_RPS_connect (cfg);
1027 GNUNET_assert (NULL != h);
1029 if (NULL != cur_test_run.pre_test)
1030 cur_test_run.pre_test (cls, h);
1031 GNUNET_assert (NULL != h);
1037 * Called to open a connection to the peer's statistics
1039 * @param cls peer context
1040 * @param cfg configuration of the peer to connect to; will be available until
1041 * GNUNET_TESTBED_operation_done() is called on the operation returned
1042 * from GNUNET_TESTBED_service_connect()
1043 * @return service handle to return in 'op_result', NULL on error
1046 stat_connect_adapter (void *cls,
1047 const struct GNUNET_CONFIGURATION_Handle *cfg)
1049 struct RPSPeer *peer = cls;
1051 peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
1052 return peer->stats_h;
1056 * Called to disconnect from peer's statistics service
1058 * @param cls peer context
1059 * @param op_result service handle returned from the connect adapter
1062 stat_disconnect_adapter (void *cls, void *op_result)
1064 struct RPSPeer *peer = cls;
1066 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1067 // (peer->stats_h, "core", "# peers connected",
1068 // stat_iterator, peer));
1069 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1070 // (peer->stats_h, "nse", "# peers connected",
1071 // stat_iterator, peer));
1072 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
1073 peer->stats_h = NULL;
1077 * Called after successfully opening a connection to a peer's statistics
1078 * service; we register statistics monitoring for CORE and NSE here.
1080 * @param cls the callback closure from functions generating an operation
1081 * @param op the operation that has been finished
1082 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
1083 * @param emsg error message in case the operation has failed; will be NULL if
1084 * operation has executed successfully.
1087 stat_complete_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1088 void *ca_result, const char *emsg )
1090 //struct GNUNET_STATISTICS_Handle *sh = ca_result;
1091 //struct RPSPeer *peer = (struct RPSPeer *) cls;
1101 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1102 // (sh, "core", "# peers connected",
1103 // stat_iterator, peer));
1104 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1105 // (sh, "nse", "# peers connected",
1106 // stat_iterator, peer));
1111 * Adapter function called to destroy connection to
1114 * @param cls closure
1115 * @param op_result service handle returned from the connect adapter
1118 rps_disconnect_adapter (void *cls,
1121 struct RPSPeer *peer = cls;
1122 struct GNUNET_RPS_Handle *h = op_result;
1124 if (NULL != peer->rps_srh)
1126 GNUNET_RPS_stream_cancel (peer->rps_srh);
1127 peer->rps_srh = NULL;
1129 GNUNET_assert (NULL != peer);
1130 GNUNET_RPS_disconnect (h);
1131 peer->rps_handle = NULL;
1135 /***********************************************************************
1136 * Definition of tests
1137 ***********************************************************************/
1139 // TODO check whether tests can be stopped earlier
1141 default_eval_cb (void)
1153 * Initialise given RPSPeer
1155 static void default_init_peer (struct RPSPeer *rps_peer)
1157 rps_peer->num_ids_to_request = 1;
1161 * Callback to call on receipt of a reply
1163 * @param cls closure
1164 * @param n number of peers
1165 * @param recv_peers the received peers
1168 default_reply_handle (void *cls,
1170 const struct GNUNET_PeerIdentity *recv_peers)
1172 struct RPSPeer *rps_peer;
1173 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1176 rps_peer = pending_rep->rps_peer;
1177 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1178 rps_peer->pending_rep_tail,
1180 rps_peer->num_pending_reps--;
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "[%s] got %" PRIu64 " peers:\n",
1183 GNUNET_i2s (rps_peer->peer_id),
1186 for (i = 0; i < n; i++)
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 GNUNET_i2s (&recv_peers[i]));
1193 rps_peer->num_recv_ids++;
1196 if (0 == evaluate () && HAVE_QUICK_QUIT == cur_test_run.have_quick_quit)
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test succeeded before timeout\n");
1199 GNUNET_assert (NULL != post_test_task);
1200 GNUNET_SCHEDULER_cancel (post_test_task);
1201 post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
1202 GNUNET_assert (NULL!= post_test_task);
1207 * Request random peers.
1210 request_peers (void *cls)
1212 struct PendingRequest *pending_req = cls;
1213 struct RPSPeer *rps_peer;
1214 struct PendingReply *pending_rep;
1216 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1218 rps_peer = pending_req->rps_peer;
1219 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1220 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1221 rps_peer->pending_req_tail,
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1224 "Requesting one peer\n");
1225 pending_rep = GNUNET_new (struct PendingReply);
1226 pending_rep->rps_peer = rps_peer;
1227 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1229 cur_test_run.reply_handle,
1231 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1232 rps_peer->pending_rep_tail,
1234 rps_peer->num_pending_reps++;
1235 rps_peer->num_pending_reqs--;
1239 cancel_pending_req (struct PendingRequest *pending_req)
1241 struct RPSPeer *rps_peer;
1243 rps_peer = pending_req->rps_peer;
1244 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1245 rps_peer->pending_req_tail,
1247 rps_peer->num_pending_reqs--;
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "Cancelling pending request\n");
1250 GNUNET_SCHEDULER_cancel (pending_req->request_task);
1251 GNUNET_free (pending_req);
1255 cancel_request (struct PendingReply *pending_rep)
1257 struct RPSPeer *rps_peer;
1259 rps_peer = pending_rep->rps_peer;
1260 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1261 rps_peer->pending_rep_tail,
1263 rps_peer->num_pending_reps--;
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Cancelling request\n");
1266 GNUNET_RPS_request_cancel (pending_rep->req_handle);
1267 GNUNET_free (pending_rep);
1274 cancel_request_cb (void *cls)
1276 struct RPSPeer *rps_peer = cls;
1277 struct PendingReply *pending_rep;
1279 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1281 pending_rep = rps_peer->pending_rep_head;
1282 GNUNET_assert (1 <= rps_peer->num_pending_reps);
1283 cancel_request (pending_rep);
1288 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1289 * issued, nor replied
1292 schedule_missing_requests (struct RPSPeer *rps_peer)
1295 struct PendingRequest *pending_req;
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 "Scheduling %u - %u missing requests\n",
1299 rps_peer->num_ids_to_request,
1300 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1301 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1302 rps_peer->num_ids_to_request);
1303 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1304 i < rps_peer->num_ids_to_request; i++)
1306 pending_req = GNUNET_new (struct PendingRequest);
1307 pending_req->rps_peer = rps_peer;
1308 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1309 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1310 cur_test_run.request_interval * i),
1313 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1314 rps_peer->pending_req_tail,
1316 rps_peer->num_pending_reqs++;
1321 cancel_pending_req_rep (struct RPSPeer *rps_peer)
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Cancelling all (pending) requests.\n");
1325 while (NULL != rps_peer->pending_req_head)
1326 cancel_pending_req (rps_peer->pending_req_head);
1327 GNUNET_assert (0 == rps_peer->num_pending_reqs);
1328 while (NULL != rps_peer->pending_rep_head)
1329 cancel_request (rps_peer->pending_rep_head);
1330 GNUNET_assert (0 == rps_peer->num_pending_reps);
1333 /***********************************
1335 ***********************************/
1338 * Initialise only non-mal RPSPeers
1340 static void mal_init_peer (struct RPSPeer *rps_peer)
1342 if (rps_peer->index >= round (portion * num_peers))
1343 rps_peer->num_ids_to_request = 1;
1348 * @brief Set peers to (non-)malicious before execution
1350 * Of signature #PreTest
1352 * @param rps_peer the peer to set (non-) malicious
1353 * @param h the handle to the service
1356 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1358 #ifdef ENABLE_MALICIOUS
1359 uint32_t num_mal_peers;
1361 GNUNET_assert ( (1 >= portion) &&
1363 num_mal_peers = round (portion * num_peers);
1365 if (rps_peer->index < num_mal_peers)
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
1370 GNUNET_i2s (rps_peer->peer_id),
1373 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1374 rps_peer_ids, target_peer);
1376 #endif /* ENABLE_MALICIOUS */
1380 mal_cb (struct RPSPeer *rps_peer)
1382 uint32_t num_mal_peers;
1384 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1389 #ifdef ENABLE_MALICIOUS
1390 GNUNET_assert ( (1 >= portion) &&
1392 num_mal_peers = round (portion * num_peers);
1394 if (rps_peer->index >= num_mal_peers)
1395 { /* It's useless to ask a malicious peer about a random sample -
1396 it's not sampling */
1397 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1398 seed_peers, rps_peer);
1399 schedule_missing_requests (rps_peer);
1401 #endif /* ENABLE_MALICIOUS */
1405 /***********************************
1407 ***********************************/
1409 single_req_cb (struct RPSPeer *rps_peer)
1411 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1416 schedule_missing_requests (rps_peer);
1419 /***********************************
1421 ***********************************/
1423 delay_req_cb (struct RPSPeer *rps_peer)
1425 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1430 schedule_missing_requests (rps_peer);
1433 /***********************************
1435 ***********************************/
1437 seed_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, 10),
1446 seed_peers, rps_peer);
1449 /***********************************
1451 ***********************************/
1453 seed_big_cb (struct RPSPeer *rps_peer)
1455 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1460 // TODO test seeding > GNUNET_MAX_MESSAGE_SIZE peers
1461 GNUNET_SCHEDULER_add_delayed (
1462 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1463 seed_peers_big, rps_peer);
1466 /***********************************
1468 ***********************************/
1470 single_peer_seed_cb (struct RPSPeer *rps_peer)
1476 /***********************************
1478 ***********************************/
1480 seed_req_cb (struct RPSPeer *rps_peer)
1482 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1487 GNUNET_SCHEDULER_add_delayed (
1488 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1489 seed_peers, rps_peer);
1490 schedule_missing_requests (rps_peer);
1493 //TODO start big mal
1495 /***********************************
1497 ***********************************/
1499 req_cancel_cb (struct RPSPeer *rps_peer)
1501 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1506 schedule_missing_requests (rps_peer);
1507 GNUNET_SCHEDULER_add_delayed (
1508 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1509 (cur_test_run.request_interval + 1)),
1510 cancel_request_cb, rps_peer);
1513 /***********************************
1515 ***********************************/
1521 * @brief Starts churn
1523 * Has signature of #MainTest
1525 * This is not implemented too nicely as this is called for each peer, but we
1526 * only need to call it once. (Yes we check that we only schedule the task
1529 * @param rps_peer The peer it's called for
1532 churn_test_cb (struct RPSPeer *rps_peer)
1534 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1540 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "Starting churn task\n");
1544 churn_task = GNUNET_SCHEDULER_add_delayed (
1545 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550 "Not starting churn task\n");
1553 schedule_missing_requests (rps_peer);
1556 /***********************************
1558 ***********************************/
1561 got_stream_peer_cb (void *cls,
1563 const struct GNUNET_PeerIdentity *peers)
1565 const struct RPSPeer *rps_peer = cls;
1567 for (uint64_t i = 0; i < num_peers; i++)
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1570 "Peer %" PRIu32 " received [%s] from stream.\n",
1572 GNUNET_i2s (&peers[i]));
1573 if (0 != rps_peer->index &&
1574 0 == memcmp (&peers[i],
1575 &rps_peers[0].peer_id,
1576 sizeof (struct GNUNET_PeerIdentity)))
1578 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Received a peer id outside sub\n");
1581 else if (0 == rps_peer->index &&
1582 0 != memcmp (&peers[i],
1583 &rps_peers[0].peer_id,
1584 sizeof (struct GNUNET_PeerIdentity)))
1586 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Received a peer id outside sub (lonely)\n");
1594 sub_post (struct RPSPeer *rps_peer)
1596 if (0 != rps_peer->index) GNUNET_RPS_sub_stop (rps_peer->rps_handle, "test");
1597 else GNUNET_RPS_sub_stop (rps_peer->rps_handle, "lonely");
1602 sub_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1606 if (0 != rps_peer->index) GNUNET_RPS_sub_start (h, "test");
1607 else GNUNET_RPS_sub_start (h, "lonely"); /* have a group of one */
1608 rps_peer->rps_srh = GNUNET_RPS_stream_request (h,
1609 &got_stream_peer_cb,
1613 /***********************************
1615 ***********************************/
1618 * Callback to be called when RPS service is started or stopped at peers
1621 * @param op the operation handle
1622 * @param emsg NULL on success; otherwise an error description
1625 churn_cb (void *cls,
1626 struct GNUNET_TESTBED_Operation *op,
1631 struct OpListEntry *entry = cls;
1633 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1638 GNUNET_TESTBED_operation_done (entry->op);
1641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1642 GNUNET_SCHEDULER_shutdown ();
1645 GNUNET_assert (0 != entry->delta);
1647 num_peers_online += entry->delta;
1649 if (PEER_GO_OFFLINE == entry->delta)
1650 { /* Peer hopefully just went offline */
1651 if (GNUNET_YES != rps_peers[entry->index].online)
1653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1654 "peer %s was expected to go offline but is still marked as online\n",
1655 GNUNET_i2s (rps_peers[entry->index].peer_id));
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1661 "peer %s probably went offline as expected\n",
1662 GNUNET_i2s (rps_peers[entry->index].peer_id));
1664 rps_peers[entry->index].online = GNUNET_NO;
1667 else if (PEER_GO_ONLINE < entry->delta)
1668 { /* Peer hopefully just went online */
1669 if (GNUNET_NO != rps_peers[entry->index].online)
1671 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1672 "peer %s was expected to go online but is still marked as offline\n",
1673 GNUNET_i2s (rps_peers[entry->index].peer_id));
1678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1679 "peer %s probably went online as expected\n",
1680 GNUNET_i2s (rps_peers[entry->index].peer_id));
1681 if (NULL != cur_test_run.pre_test)
1683 cur_test_run.pre_test (&rps_peers[entry->index],
1684 rps_peers[entry->index].rps_handle);
1685 schedule_missing_requests (&rps_peers[entry->index]);
1688 rps_peers[entry->index].online = GNUNET_YES;
1692 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1693 "Invalid value for delta: %i\n", entry->delta);
1697 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1698 rps_peers[entry->index].entry_op_manage = NULL;
1699 GNUNET_free (entry);
1700 //if (num_peers_in_round[current_round] == peers_running)
1705 * @brief Set the rps-service up or down for a specific peer
1707 * @param i index of action
1708 * @param j index of peer
1709 * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1710 * @param prob_go_on_off the probability of the action
1713 manage_service_wrapper (unsigned int i, unsigned int j,
1714 enum PEER_ONLINE_DELTA delta,
1715 double prob_go_on_off)
1717 struct OpListEntry *entry = NULL;
1720 /* make sure that management operation is not already scheduled */
1721 if (NULL != rps_peers[j].entry_op_manage)
1726 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1729 "%u. selected peer (%u: %s) is %s.\n",
1732 GNUNET_i2s (rps_peers[j].peer_id),
1733 (PEER_GO_ONLINE == delta) ? "online" : "offline");
1734 if (prob < prob_go_on_off * UINT32_MAX)
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1738 GNUNET_i2s (rps_peers[j].peer_id),
1739 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1741 if (PEER_GO_OFFLINE == delta)
1742 cancel_pending_req_rep (&rps_peers[j]);
1743 entry = make_oplist_entry ();
1744 entry->delta = delta;
1746 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1751 (PEER_GO_OFFLINE == delta) ? 0 : 1);
1752 rps_peers[j].entry_op_manage = entry;
1763 double portion_online;
1764 unsigned int *permut;
1765 double prob_go_offline;
1766 double portion_go_online;
1767 double portion_go_offline;
1769 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1774 "Churn function executing\n");
1776 churn_task = NULL; /* Should be invalid by now */
1778 /* Compute the probability for an online peer to go offline
1780 portion_online = num_peers_online * 1.0 / num_peers;
1781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1782 "Portion online: %f\n",
1784 portion_go_online = ((1 - portion_online) * .5 * .66);
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 "Portion that should go online: %f\n",
1788 portion_go_offline = (portion_online + portion_go_online) - .75;
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1790 "Portion that probably goes offline: %f\n",
1791 portion_go_offline);
1792 prob_go_offline = portion_go_offline / (portion_online * .5);
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 "Probability of a selected online peer to go offline: %f\n",
1797 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1798 (unsigned int) num_peers);
1800 /* Go over 50% randomly chosen peers */
1801 for (i = 0; i < .5 * num_peers; i++)
1805 /* If online, shut down with certain probability */
1806 if (GNUNET_YES == rps_peers[j].online)
1808 manage_service_wrapper (i, j, -1, prob_go_offline);
1811 /* If offline, restart with certain probability */
1812 else if (GNUNET_NO == rps_peers[j].online)
1814 manage_service_wrapper (i, j, 1, 0.66);
1818 GNUNET_free (permut);
1820 churn_task = GNUNET_SCHEDULER_add_delayed (
1821 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1828 * Initialise given RPSPeer
1830 static void profiler_init_peer (struct RPSPeer *rps_peer)
1832 if (num_peers - 1 == rps_peer->index)
1833 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1838 * Callback to call on receipt of a reply
1840 * @param cls closure
1841 * @param n number of peers
1842 * @param recv_peers the received peers
1845 profiler_reply_handle (void *cls,
1847 const struct GNUNET_PeerIdentity *recv_peers)
1849 struct RPSPeer *rps_peer;
1850 struct RPSPeer *rcv_rps_peer;
1854 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1856 rps_peer = pending_rep->rps_peer;
1857 file_name = "/tmp/rps/received_ids";
1858 file_name_dh = "/tmp/rps/diehard_input";
1859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1860 "[%s] got %" PRIu64 " peers:\n",
1861 GNUNET_i2s (rps_peer->peer_id),
1863 for (i = 0; i < n; i++)
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1868 GNUNET_i2s (&recv_peers[i]));
1871 GNUNET_i2s_full (&recv_peers[i]));
1872 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1873 GNUNET_assert (NULL != rcv_rps_peer);
1874 tofile (file_name_dh,
1876 (uint32_t) rcv_rps_peer->index);
1878 default_reply_handle (cls, n, recv_peers);
1883 profiler_cb (struct RPSPeer *rps_peer)
1885 if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1891 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1894 "Starting churn task\n");
1895 churn_task = GNUNET_SCHEDULER_add_delayed (
1896 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1901 "Not starting churn task\n");
1904 /* Only request peer ids at one peer.
1905 * (It's the before-last because last one is target of the focussed attack.)
1907 if (eval_peer == rps_peer)
1908 schedule_missing_requests (rps_peer);
1912 * Function called from #profiler_eval with a filename.
1914 * @param cls closure
1915 * @param filename complete filename (absolute path)
1916 * @return #GNUNET_OK to continue to iterate,
1917 * #GNUNET_NO to stop iteration with no error,
1918 * #GNUNET_SYSERR to abort iteration with error!
1921 file_name_cb (void *cls, const char *filename)
1925 if (NULL != strstr (filename, "sampler_el"))
1927 struct RPS_SamplerElement *s_elem;
1928 struct GNUNET_CRYPTO_AuthKey auth_key;
1929 const char *key_char;
1932 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1933 tofile (filename, "--------------------------\n");
1935 auth_key = string_to_auth_key (key_char);
1936 s_elem = RPS_sampler_elem_create ();
1937 RPS_sampler_elem_set (s_elem, auth_key);
1939 for (i = 0; i < num_peers; i++)
1941 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1943 RPS_sampler_elem_destroy (s_elem);
1949 * This is run after the test finished.
1951 * Compute all perfect samples.
1954 profiler_eval (void)
1956 /* Compute perfect sample for each sampler element */
1957 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1959 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1965 static uint32_t fac (uint32_t x)
1971 return x * fac (x - 1);
1974 static uint32_t binom (uint32_t n, uint32_t k)
1976 //GNUNET_assert (n >= k);
1977 if (k > n) return 0;
1978 if (0 > n) return 0;
1979 if (0 > k) return 0;
1980 if (0 == k) return 1;
1983 fac(k) * fac(n - k);
1987 * @brief is b in view of a?
1994 static int is_in_view (uint32_t a, uint32_t b)
1997 for (i = 0; i < rps_peers[a].cur_view_count; i++)
1999 if (0 == memcmp (rps_peers[b].peer_id,
2000 &rps_peers[a].cur_view[i],
2001 sizeof (struct GNUNET_PeerIdentity)))
2009 static uint32_t get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
2013 for (i = 0; i < num_peers; i++)
2015 if (0 == memcmp (pid,
2016 rps_peers[i].peer_id,
2017 sizeof (struct GNUNET_PeerIdentity)))
2022 //return 0; /* Should not happen - make compiler happy */
2023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2024 "No known _PeerIdentity %s!\n",
2025 GNUNET_i2s_full (pid));
2030 * @brief Counts number of peers in view of a that have b in their view
2037 static uint32_t count_containing_views (uint32_t a, uint32_t b)
2043 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2045 peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
2046 if (GNUNET_YES == is_in_view (peer_idx, b))
2055 * @brief Computes the probability for each other peer to be selected by the
2056 * sampling process based on the views of all peers
2058 * @param peer_idx index of the peer that is about to sample
2060 static void compute_probabilities (uint32_t peer_idx)
2062 //double probs[num_peers] = { 0 };
2063 double probs[num_peers];
2064 size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof (char);
2065 char *probs_as_str = GNUNET_malloc (probs_as_str_size);
2066 char *probs_as_str_cpy;
2071 uint32_t cont_views;
2072 uint32_t number_of_being_in_pull_events;
2074 uint32_t count_non_zero_prob = 0;
2076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2077 "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
2078 /* Firstly without knowledge of old views */
2079 for (i = 0; i < num_peers; i++)
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2082 "\tfor peer %" PRIu32 ":\n", i);
2083 view_size = rps_peers[i].cur_view_count;
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2085 "\t\tview_size: %" PRIu32 "\n", view_size);
2086 /* For peer i the probability of being sampled is
2087 * evenly distributed among all possibly observed peers. */
2088 /* We could have observed a peer in three cases:
2089 * 1. peer sent a push
2090 * 2. peer was contained in a pull reply
2091 * 3. peer was in history (sampler) - ignored for now */
2092 /* 1. Probability of having received a push from peer i */
2093 if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
2094 (1 <= (0.45 * view_size)))
2096 prob_push = 1.0 * binom (0.45 * view_size, 1)
2098 binom (view_size, 0.45 * view_size);
2099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2100 "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2105 "\t\tposs choices from view: %" PRIu32 ", containing i: %" PRIu32 "\n",
2106 binom (view_size, 0.45 * view_size),
2107 binom (0.45 * view_size, 1));
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
2115 /* 2. Probability of peer i being contained in pulls */
2116 view_size = rps_peers[peer_idx].cur_view_count;
2117 cont_views = count_containing_views (peer_idx, i);
2118 number_of_being_in_pull_events =
2119 (binom (view_size, 0.45 * view_size) -
2120 binom (view_size - cont_views, 0.45 * view_size));
2121 if (0 != number_of_being_in_pull_events)
2123 prob_pull = number_of_being_in_pull_events
2125 (1.0 * binom (view_size, 0.45 * view_size));
2130 probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2132 "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
2133 " peers in its view who know %" PRIu32 " prob: %f\n",
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2140 "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
2141 binom (view_size, 0.45 * view_size));
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2143 "\t\tnumber of possible pull combinations without %" PRIu32
2146 binom (view_size - cont_views, 0.45 * view_size));
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2148 "\t\tnumber of possible pull combinations with %" PRIu32
2151 number_of_being_in_pull_events);
2153 if (0 != probs[i]) count_non_zero_prob++;
2156 if (0 != count_non_zero_prob)
2158 for (i = 0; i < num_peers; i++)
2160 probs[i] = probs[i] * (1.0 / count_non_zero_prob);
2163 for (i = 0; i < num_peers; i++)
2169 for (i = 0; i < num_peers; i++)
2171 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2172 tmp = GNUNET_snprintf (probs_as_str,
2174 "%s %7.6f", probs_as_str_cpy, probs[i]);
2175 GNUNET_free (probs_as_str_cpy);
2176 GNUNET_assert (0 <= tmp);
2179 to_file_w_len (rps_peers[peer_idx].file_name_probs,
2182 GNUNET_free (probs_as_str);
2186 * @brief This counts the number of peers in which views a given peer occurs.
2188 * It also stores this value in the rps peer.
2190 * @param peer_idx the index of the peer to count the representation
2192 * @return the number of occurrences
2194 static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
2199 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2201 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2203 if (0 == memcmp (rps_peers[peer_idx].peer_id,
2204 &rps_peers[i].cur_view[j],
2205 sizeof (struct GNUNET_PeerIdentity)))
2212 rps_peers[peer_idx].count_in_views = count;
2216 static uint32_t cumulated_view_sizes ()
2221 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2223 view_sizes += rps_peers[i].cur_view_count;
2228 static void count_peer_in_views (uint32_t *count_peers)
2232 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2234 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2236 if (0 == memcmp (rps_peers[i].peer_id,
2237 &rps_peers[i].cur_view[j],
2238 sizeof (struct GNUNET_PeerIdentity)))
2246 void compute_diversity ()
2249 /* ith entry represents the numer of occurrences in other peer's views */
2250 uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
2251 uint32_t views_total_size;
2253 /* deviation from expected number of peers */
2254 double *deviation = GNUNET_new_array (num_peers, double);
2256 views_total_size = 0;
2259 /* For each peer count its representation in other peer's views*/
2260 for (i = 0; i < num_peers; i++) /* Peer to count */
2262 views_total_size += rps_peers[i].cur_view_count;
2263 count_peer_in_views (count_peers);
2264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2265 "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32"\n",
2267 GNUNET_i2s (rps_peers[i].peer_id),
2271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2272 "size of all views combined: %" PRIu32 "\n",
2274 expected = ((double) 1/num_peers) * views_total_size;
2275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2276 "Expected number of occurrences of each peer in all views: %f\n",
2278 for (i = 0; i < num_peers; i++) /* Peer to count */
2280 deviation[i] = expected - count_peers[i];
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2282 "Deviation from expectation: %f\n", deviation[i]);
2284 GNUNET_free (count_peers);
2285 GNUNET_free (deviation);
2288 void print_view_sizes()
2292 for (i = 0; i < num_peers; i++) /* Peer to count */
2294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2295 "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
2297 GNUNET_i2s (rps_peers[i].peer_id),
2298 rps_peers[i].cur_view_count);
2302 void all_views_updated_cb()
2304 compute_diversity();
2308 void view_update_cb (void *cls,
2310 const struct GNUNET_PeerIdentity *peers)
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2313 "View was updated (%" PRIu64 ")\n", view_size);
2314 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
2315 to_file ("/tmp/rps/view_sizes.txt",
2316 "%" PRIu64 " %" PRIu32 "",
2319 for (int i = 0; i < view_size; i++)
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 "\t%s\n", GNUNET_i2s (&peers[i]));
2324 GNUNET_array_grow (rps_peer->cur_view,
2325 rps_peer->cur_view_count,
2327 //*rps_peer->cur_view = *peers;
2328 GNUNET_memcpy (rps_peer->cur_view,
2330 view_size * sizeof (struct GNUNET_PeerIdentity));
2331 to_file ("/tmp/rps/count_in_views.txt",
2332 "%" PRIu64 " %" PRIu32 "",
2334 count_peer_in_views_2 (rps_peer->index));
2335 cumulated_view_sizes();
2338 to_file ("/tmp/rps/repr.txt",
2339 "%" PRIu64 /* index */
2340 " %" PRIu32 /* occurrence in views */
2341 " %" PRIu32 /* view sizes */
2342 " %f" /* fraction of repr in views */
2343 " %f" /* average view size */
2344 " %f" /* prob of occurrence in view slot */
2345 " %f" "", /* exp frac of repr in views */
2347 count_peer_in_views_2 (rps_peer->index),
2349 count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
2350 view_sizes / (view_size * 1.0), /* average view size */
2351 1.0 /view_size, /* prob of occurrence in view slot */
2352 (1.0/view_size) * (view_sizes/view_size) /* expected fraction of repr in views */
2355 compute_probabilities (rps_peer->index);
2356 all_views_updated_cb();
2360 pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
2362 rps_peer->file_name_probs =
2363 store_prefix_file_name (rps_peer->peer_id, "probs");
2364 GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
2367 void write_final_stats (void){
2370 for (i = 0; i < num_peers; i++)
2372 to_file ("/tmp/rps/final_stats.dat",
2373 "%" PRIu32 " " /* index */
2375 PRIu64 " %" /* rounds */
2376 PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" /* blocking */
2377 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* issued */
2378 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* sent */
2379 PRIu64 " %" PRIu64 " %" PRIu64 /* recv */,
2381 GNUNET_i2s (rps_peers[i].peer_id),
2382 rps_peers[i].num_rounds,
2383 rps_peers[i].num_blocks,
2384 rps_peers[i].num_blocks_many_push,
2385 rps_peers[i].num_blocks_no_push,
2386 rps_peers[i].num_blocks_no_pull,
2387 rps_peers[i].num_blocks_many_push_no_pull,
2388 rps_peers[i].num_blocks_no_push_no_pull,
2389 rps_peers[i].num_issued_push,
2390 rps_peers[i].num_issued_pull_req,
2391 rps_peers[i].num_issued_pull_rep,
2392 rps_peers[i].num_sent_push,
2393 rps_peers[i].num_sent_pull_req,
2394 rps_peers[i].num_sent_pull_rep,
2395 rps_peers[i].num_recv_push,
2396 rps_peers[i].num_recv_pull_req,
2397 rps_peers[i].num_recv_pull_rep);
2402 * Continuation called by #GNUNET_STATISTICS_get() functions.
2404 * Remembers that this specific statistics value was received for this peer.
2405 * Checks whether all peers received their statistics yet.
2406 * Issues the shutdown.
2408 * @param cls closure
2409 * @param success #GNUNET_OK if statistics were
2410 * successfully obtained, #GNUNET_SYSERR if not.
2413 post_test_shutdown_ready_cb (void *cls,
2416 struct STATcls *stat_cls = (struct STATcls *) cls;
2417 struct RPSPeer *rps_peer = stat_cls->rps_peer;
2418 if (GNUNET_OK == success)
2420 /* set flag that we we got the value */
2421 rps_peer->stat_collected_flags |= stat_cls->stat_type;
2423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2424 "Peer %u did not receive statistics value\n",
2426 GNUNET_free (stat_cls);
2430 if (NULL != rps_peer->stat_op &&
2431 GNUNET_YES == check_statistics_collect_completed_single_peer (rps_peer))
2433 GNUNET_TESTBED_operation_done (rps_peer->stat_op);
2436 write_final_stats ();
2437 if (GNUNET_YES == check_statistics_collect_completed())
2439 //write_final_stats ();
2440 GNUNET_free (stat_cls);
2441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2443 GNUNET_SCHEDULER_shutdown ();
2445 GNUNET_free (stat_cls);
2450 * @brief Converts string representation to the corresponding #STAT_TYPE enum.
2452 * @param stat_str string representation of statistics specifier
2454 * @return corresponding enum
2456 enum STAT_TYPE stat_str_2_type (const char *stat_str)
2458 if (0 == strncmp ("# rounds blocked - no pull replies", stat_str, strlen ("# rounds blocked - no pull replies")))
2460 return STAT_TYPE_BLOCKS_NO_PULL;
2462 else if (0 == strncmp ("# rounds blocked - too many pushes, no pull replies", stat_str, strlen ("# rounds blocked - too many pushes, no pull replies")))
2464 return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
2466 else if (0 == strncmp ("# rounds blocked - too many pushes", stat_str, strlen ("# rounds blocked - too many pushes")))
2468 return STAT_TYPE_BLOCKS_MANY_PUSH;
2470 else if (0 == strncmp ("# rounds blocked - no pushes, no pull replies", stat_str, strlen ("# rounds blocked - no pushes, no pull replies")))
2472 return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
2474 else if (0 == strncmp ("# rounds blocked - no pushes", stat_str, strlen ("# rounds blocked - no pushes")))
2476 return STAT_TYPE_BLOCKS_NO_PUSH;
2478 else if (0 == strncmp ("# rounds blocked", stat_str, strlen ("# rounds blocked")))
2480 return STAT_TYPE_BLOCKS;
2482 else if (0 == strncmp ("# rounds", stat_str, strlen ("# rounds")))
2484 return STAT_TYPE_ROUNDS;
2486 else if (0 == strncmp ("# push send issued", stat_str, strlen ("# push send issued")))
2488 return STAT_TYPE_ISSUED_PUSH_SEND;
2490 else if (0 == strncmp ("# pull request send issued", stat_str, strlen ("# pull request send issued")))
2492 return STAT_TYPE_ISSUED_PULL_REQ;
2494 else if (0 == strncmp ("# pull reply send issued", stat_str, strlen ("# pull reply send issued")))
2496 return STAT_TYPE_ISSUED_PULL_REP;
2498 else if (0 == strncmp ("# pushes sent", stat_str, strlen ("# pushes sent")))
2500 return STAT_TYPE_SENT_PUSH_SEND;
2502 else if (0 == strncmp ("# pull requests sent", stat_str, strlen ("# pull requests sent")))
2504 return STAT_TYPE_SENT_PULL_REQ;
2506 else if (0 == strncmp ("# pull replys sent", stat_str, strlen ("# pull replys sent")))
2508 return STAT_TYPE_SENT_PULL_REP;
2510 else if (0 == strncmp ("# push message received", stat_str, strlen ("# push message received")))
2512 return STAT_TYPE_RECV_PUSH_SEND;
2514 else if (0 == strncmp ("# pull request message received", stat_str, strlen ("# pull request message received")))
2516 return STAT_TYPE_RECV_PULL_REQ;
2518 else if (0 == strncmp ("# pull reply messages received", stat_str, strlen ("# pull reply messages received")))
2520 return STAT_TYPE_RECV_PULL_REP;
2522 return STAT_TYPE_MAX;
2527 * @brief Converts #STAT_TYPE enum to the equivalent string representation that
2528 * is stored with the statistics service.
2530 * @param stat_type #STAT_TYPE enum
2532 * @return string representation that matches statistics value
2534 char* stat_type_2_str (enum STAT_TYPE stat_type)
2538 case STAT_TYPE_ROUNDS:
2540 case STAT_TYPE_BLOCKS:
2541 return "# rounds blocked";
2542 case STAT_TYPE_BLOCKS_MANY_PUSH:
2543 return "# rounds blocked - too many pushes";
2544 case STAT_TYPE_BLOCKS_NO_PUSH:
2545 return "# rounds blocked - no pushes";
2546 case STAT_TYPE_BLOCKS_NO_PULL:
2547 return "# rounds blocked - no pull replies";
2548 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2549 return "# rounds blocked - too many pushes, no pull replies";
2550 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2551 return "# rounds blocked - no pushes, no pull replies";
2552 case STAT_TYPE_ISSUED_PUSH_SEND:
2553 return "# push send issued";
2554 case STAT_TYPE_ISSUED_PULL_REQ:
2555 return "# pull request send issued";
2556 case STAT_TYPE_ISSUED_PULL_REP:
2557 return "# pull reply send issued";
2558 case STAT_TYPE_SENT_PUSH_SEND:
2559 return "# pushes sent";
2560 case STAT_TYPE_SENT_PULL_REQ:
2561 return "# pull requests sent";
2562 case STAT_TYPE_SENT_PULL_REP:
2563 return "# pull replys sent";
2564 case STAT_TYPE_RECV_PUSH_SEND:
2565 return "# push message received";
2566 case STAT_TYPE_RECV_PULL_REQ:
2567 return "# pull request message received";
2568 case STAT_TYPE_RECV_PULL_REP:
2569 return "# pull reply messages received";
2578 * Callback function to process statistic values.
2580 * @param cls closure
2581 * @param subsystem name of subsystem that created the statistic
2582 * @param name the name of the datum
2583 * @param value the current value
2584 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
2585 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
2588 stat_iterator (void *cls,
2589 const char *subsystem,
2595 (void) is_persistent;
2596 const struct STATcls *stat_cls = (const struct STATcls *) cls;
2597 struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got stat value: %s - %" PRIu64 "\n",
2599 //stat_type_2_str (stat_cls->stat_type),
2602 to_file (rps_peer->file_name_stats,
2603 "%s: %" PRIu64 "\n",
2606 switch (stat_str_2_type (name))
2608 case STAT_TYPE_ROUNDS:
2609 rps_peer->num_rounds = value;
2611 case STAT_TYPE_BLOCKS:
2612 rps_peer->num_blocks = value;
2614 case STAT_TYPE_BLOCKS_MANY_PUSH:
2615 rps_peer->num_blocks_many_push = value;
2617 case STAT_TYPE_BLOCKS_NO_PUSH:
2618 rps_peer->num_blocks_no_push = value;
2620 case STAT_TYPE_BLOCKS_NO_PULL:
2621 rps_peer->num_blocks_no_pull = value;
2623 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2624 rps_peer->num_blocks_many_push_no_pull = value;
2626 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2627 rps_peer->num_blocks_no_push_no_pull = value;
2629 case STAT_TYPE_ISSUED_PUSH_SEND:
2630 rps_peer->num_issued_push = value;
2632 case STAT_TYPE_ISSUED_PULL_REQ:
2633 rps_peer->num_issued_pull_req = value;
2635 case STAT_TYPE_ISSUED_PULL_REP:
2636 rps_peer->num_issued_pull_rep = value;
2638 case STAT_TYPE_SENT_PUSH_SEND:
2639 rps_peer->num_sent_push = value;
2641 case STAT_TYPE_SENT_PULL_REQ:
2642 rps_peer->num_sent_pull_req = value;
2644 case STAT_TYPE_SENT_PULL_REP:
2645 rps_peer->num_sent_pull_rep = value;
2647 case STAT_TYPE_RECV_PUSH_SEND:
2648 rps_peer->num_recv_push = value;
2650 case STAT_TYPE_RECV_PULL_REQ:
2651 rps_peer->num_recv_pull_req = value;
2653 case STAT_TYPE_RECV_PULL_REP:
2654 rps_peer->num_recv_pull_rep = value;
2658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2659 "Unknown statistics string: %s\n",
2666 void post_profiler (struct RPSPeer *rps_peer)
2668 if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2674 "Going to request statistic values with mask 0x%" PRIx32 "\n",
2675 cur_test_run.stat_collect_flags);
2677 struct STATcls *stat_cls;
2679 for (stat_type = STAT_TYPE_ROUNDS;
2680 stat_type < STAT_TYPE_MAX;
2681 stat_type = stat_type <<1)
2683 if (stat_type & cur_test_run.stat_collect_flags)
2685 stat_cls = GNUNET_malloc (sizeof (struct STATcls));
2686 stat_cls->rps_peer = rps_peer;
2687 stat_cls->stat_type = stat_type;
2688 rps_peer->file_name_stats =
2689 store_prefix_file_name (rps_peer->peer_id, "stats");
2690 GNUNET_STATISTICS_get (rps_peer->stats_h,
2692 stat_type_2_str (stat_type),
2693 post_test_shutdown_ready_cb,
2695 (struct STATcls *) stat_cls);
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2697 "Requested statistics for %s (peer %" PRIu32 ")\n",
2698 stat_type_2_str (stat_type),
2705 /***********************************************************************
2706 * /Definition of tests
2707 ***********************************************************************/
2711 * Actual "main" function for the testcase.
2713 * @param cls closure
2714 * @param h the run handle
2715 * @param n_peers number of peers in 'peers'
2716 * @param peers handle to peers run in the testbed
2717 * @param links_succeeded the number of overlay link connection attempts that
2719 * @param links_failed the number of overlay link connection attempts that
2724 struct GNUNET_TESTBED_RunHandle *h,
2725 unsigned int n_peers,
2726 struct GNUNET_TESTBED_Peer **peers,
2727 unsigned int links_succeeded,
2728 unsigned int links_failed)
2732 (void) links_failed;
2734 struct OpListEntry *entry;
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
2738 /* Check whether we timed out */
2739 if (n_peers != num_peers ||
2741 0 == links_succeeded)
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
2745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
2747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
2748 GNUNET_SCHEDULER_shutdown ();
2753 /* Initialize peers */
2754 testbed_peers = peers;
2755 num_peers_online = 0;
2756 for (i = 0; i < num_peers; i++)
2758 entry = make_oplist_entry ();
2760 rps_peers[i].index = i;
2761 if (NULL != cur_test_run.init_peer)
2762 cur_test_run.init_peer (&rps_peers[i]);
2763 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2765 rps_peers->cur_view_count = 0;
2766 rps_peers->cur_view = NULL;
2768 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2769 GNUNET_TESTBED_PIT_IDENTITY,
2774 /* Bring peers up */
2775 GNUNET_assert (num_peers == n_peers);
2776 for (i = 0; i < n_peers; i++)
2778 rps_peers[i].index = i;
2780 GNUNET_TESTBED_service_connect (&rps_peers[i],
2783 &rps_connect_complete_cb,
2785 &rps_connect_adapter,
2786 &rps_disconnect_adapter,
2788 /* Connect all peers to statistics service */
2789 if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2791 rps_peers[i].stat_op =
2792 GNUNET_TESTBED_service_connect (NULL,
2797 &stat_connect_adapter,
2798 &stat_disconnect_adapter,
2803 if (NULL != churn_task)
2804 GNUNET_SCHEDULER_cancel (churn_task);
2805 post_test_task = GNUNET_SCHEDULER_add_delayed (timeout, &post_test_op, NULL);
2806 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2807 (timeout_s * 1.2) + 0.1 * num_peers);
2808 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
2813 * Entry point for the testcase, sets up the testbed.
2815 * @param argc unused
2816 * @param argv unused
2817 * @return 0 on success
2820 main (int argc, char *argv[])
2825 /* Defaults for tests */
2827 cur_test_run.name = "test-rps-default";
2828 cur_test_run.init_peer = default_init_peer;
2829 cur_test_run.pre_test = NULL;
2830 cur_test_run.reply_handle = default_reply_handle;
2831 cur_test_run.eval_cb = default_eval_cb;
2832 cur_test_run.post_test = NULL;
2833 cur_test_run.have_churn = HAVE_CHURN;
2834 cur_test_run.have_collect_statistics = NO_COLLECT_STATISTICS;
2835 cur_test_run.stat_collect_flags = 0;
2836 cur_test_run.have_collect_view = NO_COLLECT_VIEW;
2840 if (strstr (argv[0], "malicious") != NULL)
2842 cur_test_run.pre_test = mal_pre;
2843 cur_test_run.main_test = mal_cb;
2844 cur_test_run.init_peer = mal_init_peer;
2847 if (strstr (argv[0], "_1") != NULL)
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
2850 cur_test_run.name = "test-rps-malicious_1";
2853 else if (strstr (argv[0], "_2") != NULL)
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
2856 cur_test_run.name = "test-rps-malicious_2";
2859 else if (strstr (argv[0], "_3") != NULL)
2861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
2862 cur_test_run.name = "test-rps-malicious_3";
2867 else if (strstr (argv[0], "_single_req") != NULL)
2869 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
2870 cur_test_run.name = "test-rps-single-req";
2871 cur_test_run.main_test = single_req_cb;
2872 cur_test_run.have_churn = HAVE_NO_CHURN;
2875 else if (strstr (argv[0], "_delayed_reqs") != NULL)
2877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
2878 cur_test_run.name = "test-rps-delayed-reqs";
2879 cur_test_run.main_test = delay_req_cb;
2880 cur_test_run.have_churn = HAVE_NO_CHURN;
2883 else if (strstr (argv[0], "_seed_big") != NULL)
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
2887 cur_test_run.name = "test-rps-seed-big";
2888 cur_test_run.main_test = seed_big_cb;
2889 cur_test_run.eval_cb = no_eval;
2890 cur_test_run.have_churn = HAVE_NO_CHURN;
2894 else if (strstr (argv[0], "_single_peer_seed") != NULL)
2896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
2897 cur_test_run.name = "test-rps-single-peer-seed";
2898 cur_test_run.main_test = single_peer_seed_cb;
2899 cur_test_run.have_churn = HAVE_NO_CHURN;
2902 else if (strstr (argv[0], "_seed_request") != NULL)
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
2905 cur_test_run.name = "test-rps-seed-request";
2906 cur_test_run.main_test = seed_req_cb;
2907 cur_test_run.have_churn = HAVE_NO_CHURN;
2910 else if (strstr (argv[0], "_seed") != NULL)
2912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
2913 cur_test_run.name = "test-rps-seed";
2914 cur_test_run.main_test = seed_cb;
2915 cur_test_run.eval_cb = no_eval;
2916 cur_test_run.have_churn = HAVE_NO_CHURN;
2919 else if (strstr (argv[0], "_req_cancel") != NULL)
2921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
2922 cur_test_run.name = "test-rps-req-cancel";
2924 cur_test_run.main_test = req_cancel_cb;
2925 cur_test_run.eval_cb = no_eval;
2926 cur_test_run.have_churn = HAVE_NO_CHURN;
2930 else if (strstr (argv[0], "_churn") != NULL)
2932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
2933 cur_test_run.name = "test-rps-churn";
2935 cur_test_run.init_peer = default_init_peer;
2936 cur_test_run.main_test = churn_test_cb;
2937 cur_test_run.reply_handle = default_reply_handle;
2938 cur_test_run.eval_cb = default_eval_cb;
2939 cur_test_run.have_churn = HAVE_NO_CHURN;
2940 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
2944 else if (strstr (argv[0], "_sub") != NULL)
2946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test subs\n");
2947 cur_test_run.name = "test-rps-sub";
2949 //cur_test_run.init_peer = &default_init_peer;
2950 cur_test_run.pre_test = &sub_pre;
2951 cur_test_run.main_test = &single_req_cb;
2952 //cur_test_run.reply_handle = default_reply_handle;
2953 cur_test_run.post_test = &sub_post;
2954 //cur_test_run.eval_cb = default_eval_cb;
2955 cur_test_run.have_churn = HAVE_NO_CHURN;
2956 cur_test_run.have_quick_quit = HAVE_QUICK_QUIT;
2959 else if (strstr (argv[0], "profiler") != NULL)
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
2962 cur_test_run.name = "test-rps-profiler";
2965 cur_test_run.init_peer = profiler_init_peer;
2966 //cur_test_run.pre_test = mal_pre;
2967 cur_test_run.pre_test = pre_profiler;
2968 cur_test_run.main_test = profiler_cb;
2969 cur_test_run.reply_handle = profiler_reply_handle;
2970 cur_test_run.eval_cb = profiler_eval;
2971 cur_test_run.post_test = post_profiler;
2972 cur_test_run.request_interval = 2;
2973 cur_test_run.num_requests = 5;
2974 //cur_test_run.have_churn = HAVE_CHURN;
2975 cur_test_run.have_churn = HAVE_NO_CHURN;
2976 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
2977 cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
2978 cur_test_run.stat_collect_flags = STAT_TYPE_ROUNDS |
2980 STAT_TYPE_BLOCKS_MANY_PUSH |
2981 STAT_TYPE_BLOCKS_NO_PUSH |
2982 STAT_TYPE_BLOCKS_NO_PULL |
2983 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL |
2984 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL |
2985 STAT_TYPE_ISSUED_PUSH_SEND |
2986 STAT_TYPE_ISSUED_PULL_REQ |
2987 STAT_TYPE_ISSUED_PULL_REP |
2988 STAT_TYPE_SENT_PUSH_SEND |
2989 STAT_TYPE_SENT_PULL_REQ |
2990 STAT_TYPE_SENT_PULL_REP |
2991 STAT_TYPE_RECV_PUSH_SEND |
2992 STAT_TYPE_RECV_PULL_REQ |
2993 STAT_TYPE_RECV_PULL_REP;
2994 cur_test_run.have_collect_view = COLLECT_VIEW;
2997 /* 'Clean' directory */
2998 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
2999 GNUNET_DISK_directory_create ("/tmp/rps/");
3001 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_s);
3003 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
3004 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
3005 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
3006 if ( (2 == mal_type) ||
3008 target_peer = &rps_peer_ids[num_peers - 2];
3009 if (profiler_eval == cur_test_run.eval_cb)
3010 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
3011 malicious peer if not careful
3012 with the malicious portion */
3015 ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
3020 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3021 "_test_run returned.\n");
3022 if (GNUNET_OK != ret_value)
3024 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3025 "Test did not run successfully!\n");
3028 ret_value = cur_test_run.eval_cb();
3030 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
3032 GNUNET_array_grow (rps_peers->cur_view,
3033 rps_peers->cur_view_count,
3036 GNUNET_free (rps_peers);
3037 GNUNET_free (rps_peer_ids);
3038 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
3043 /* end of test_rps.c */