plugin datastore mysql
[oweals/gnunet.git] / src / rps / test_rps.c
index d3b41801a7b99bcc18ccf337682800f90e030c35..e0fbdbb887ae214d6af6c53a2e16548d449dd264 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009, 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 2012 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -38,7 +38,7 @@
 /**
  * How many peers do we start?
  */
-uint32_t num_peers;
+static uint32_t num_peers;
 
 /**
  * How long do we run the test?
@@ -214,15 +214,10 @@ struct RPSPeer
    */
   unsigned int num_pending_reps;
 
-  /**
-   * Received PeerIDs
-   */
-  struct GNUNET_PeerIdentity *rec_ids;
-
   /**
    * Number of received PeerIDs
    */
-  unsigned int num_rec_ids;
+  unsigned int num_recv_ids;
 };
 
 
@@ -261,18 +256,11 @@ static unsigned int num_peers_online;
  */
 static int ok;
 
-
 /**
  * Identifier for the churn task that runs periodically
  */
 static struct GNUNET_SCHEDULER_Task *churn_task;
 
-/**
- * Identifier for the churn task that runs periodically
- */
-static struct GNUNET_SCHEDULER_Task *shutdown_task;
-
-
 /**
  * Called to initialise the given RPSPeer
  */
@@ -367,7 +355,7 @@ static int in_shutdown;
  * Append arguments to file
  */
 static void
-tofile_ (const char *file_name, char *line)
+tofile_ (const char *file_name, const char *line)
 {
   struct GNUNET_DISK_FileHandle *f;
   /* char output_buffer[512]; */
@@ -408,7 +396,7 @@ tofile_ (const char *file_name, char *line)
   if (size != size2)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Unable to write to file! (Size: %u, size2: %u)\n",
+                "Unable to write to file! (Size: %lu, size2: %lu)\n",
                 size,
                 size2);
     return;
@@ -435,7 +423,7 @@ tofile_ (const char *file_name, char *line)
 
 
 /**
- * Write the ids and their according index in the given array to a file 
+ * Write the ids and their according index in the given array to a file
  * Unused
  */
 /* static void
@@ -471,10 +459,10 @@ evaluate (void)
         "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
         i,
         GNUNET_i2s (rps_peers[i].peer_id),
-        rps_peers[i].num_rec_ids,
+        rps_peers[i].num_recv_ids,
         rps_peers[i].num_ids_to_request,
-        (rps_peers[i].num_ids_to_request == rps_peers[i].num_rec_ids));
-    tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_rec_ids);
+        (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
+    tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
   }
   return tmp_ok? 0 : 1;
 }
@@ -498,16 +486,19 @@ make_oplist_entry ()
  * Task run on timeout to shut everything down.
  */
 static void
-shutdown_op (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_op (void *cls)
 {
   unsigned int i;
 
   in_shutdown = GNUNET_YES;
   if (NULL != churn_task)
+  {
     GNUNET_SCHEDULER_cancel (churn_task);
-
-  for (i = 0 ; i < num_peers ; i++)
-    GNUNET_TESTBED_operation_done (rps_peers[i].op);
+    churn_task = NULL;
+  }
+  for (i = 0; i < num_peers; i++)
+    if (NULL != rps_peers[i].op)
+      GNUNET_TESTBED_operation_done (rps_peers[i].op);
   GNUNET_SCHEDULER_shutdown ();
 }
 
@@ -515,11 +506,11 @@ shutdown_op (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 /**
  * Seed peers.
  */
-  void
-seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+static void
+seed_peers (void *cls)
 {
+  struct RPSPeer *peer = cls;
   unsigned int amount;
-  struct RPSPeer *peer = (struct RPSPeer *) cls;
   unsigned int i;
 
   // TODO if malicious don't seed mal peers
@@ -535,6 +526,40 @@ seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+/**
+ * Seed peers.
+ */
+static void
+seed_peers_big (void *cls)
+{
+  struct RPSPeer *peer = cls;
+  unsigned int seed_msg_size;
+  uint32_t num_peers_max;
+  unsigned int amount;
+  unsigned int i;
+
+  seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
+  num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE - seed_msg_size) /
+    sizeof (struct GNUNET_PeerIdentity);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      "Peers that fit in one seed msg; %u\n",
+      num_peers_max);
+  amount = num_peers_max + (0.5 * num_peers_max);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      "Seeding many (%u) peers:\n",
+      amount);
+  struct GNUNET_PeerIdentity ids_to_seed[amount];
+  for (i = 0; i < amount; i++)
+  {
+    ids_to_seed[i] = *peer->peer_id;
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
+                i,
+                GNUNET_i2s (&ids_to_seed[i]));
+  }
+
+  GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
+}
+
 /**
  * Get the id of peer i.
  */
@@ -549,6 +574,7 @@ info_cb (void *cb_cls,
   if (NULL == pinfo || NULL != emsg)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
+    GNUNET_TESTBED_operation_done (entry->op);
     return;
   }
 
@@ -559,8 +585,6 @@ info_cb (void *cb_cls,
 
   rps_peer_ids[entry->index] = *(pinfo->result.id);
   rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
-  rps_peers[entry->index].rec_ids = NULL;
-  rps_peers[entry->index].num_rec_ids = 0;
 
   GNUNET_CONTAINER_multipeermap_put (peer_map,
       &rps_peer_ids[entry->index],
@@ -571,11 +595,8 @@ info_cb (void *cb_cls,
            entry->index,
            GNUNET_i2s_full (&rps_peer_ids[entry->index]));
 
-  if (NULL != cur_test_run.init_peer)
-    cur_test_run.init_peer (&rps_peers[entry->index]);
-
-  GNUNET_TESTBED_operation_done (entry->op);
   GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
+  GNUNET_TESTBED_operation_done (entry->op);
   GNUNET_free (entry);
 }
 
@@ -713,7 +734,7 @@ default_reply_handle (void *cls,
               "[%s] got %" PRIu64 " peers:\n",
               GNUNET_i2s (rps_peer->peer_id),
               n);
-  
+
   for (i = 0; i < n; i++)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -721,14 +742,12 @@ default_reply_handle (void *cls,
                 i,
                 GNUNET_i2s (&recv_peers[i]));
 
-    /* GNUNET_array_append (rps_peer->rec_ids, rps_peer->num_rec_ids, recv_peers[i]); */
-    rps_peer->num_rec_ids++;
+    rps_peer->num_recv_ids++;
   }
 
   if (0 == evaluate ())
   {
-    GNUNET_SCHEDULER_cancel (shutdown_task);
-    shutdown_task = GNUNET_SCHEDULER_add_now (&shutdown_op, NULL);
+    GNUNET_SCHEDULER_shutdown ();
   }
 }
 
@@ -736,11 +755,10 @@ default_reply_handle (void *cls,
  * Request random peers.
  */
 static void
-request_peers (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+request_peers (void *cls)
 {
+  struct PendingRequest *pending_req = cls;
   struct RPSPeer *rps_peer;
-  struct PendingRequest *pending_req = (struct PendingRequest *) cls;
   struct PendingReply *pending_rep;
 
   if (GNUNET_YES == in_shutdown)
@@ -801,11 +819,10 @@ cancel_request (struct PendingReply *pending_rep)
  * Cancel a request.
  */
 static void
-cancel_request_cb (void *cls,
-                const struct GNUNET_SCHEDULER_TaskContext *tc)
+cancel_request_cb (void *cls)
 {
+  struct RPSPeer *rps_peer = cls;
   struct PendingReply *pending_rep;
-  struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
 
   if (GNUNET_YES == in_shutdown)
     return;
@@ -944,8 +961,9 @@ delay_req_cb (struct RPSPeer *rps_peer)
 static void
 seed_cb (struct RPSPeer *rps_peer)
 {
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
-                                seed_peers, rps_peer);
+  GNUNET_SCHEDULER_add_delayed (
+      GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
+      seed_peers, rps_peer);
 }
 
 /***********************************
@@ -955,6 +973,9 @@ static void
 seed_big_cb (struct RPSPeer *rps_peer)
 {
   // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
+  GNUNET_SCHEDULER_add_delayed (
+      GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
+      seed_peers_big, rps_peer);
 }
 
 /***********************************
@@ -972,8 +993,9 @@ single_peer_seed_cb (struct RPSPeer *rps_peer)
 static void
 seed_req_cb (struct RPSPeer *rps_peer)
 {
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
-                                seed_peers, rps_peer);
+  GNUNET_SCHEDULER_add_delayed (
+      GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
+      seed_peers, rps_peer);
   schedule_missing_requests (rps_peer);
 }
 
@@ -1106,8 +1128,9 @@ manage_service_wrapper (unsigned int i, unsigned int j, int delta,
   }
 }
 
+
 static void
-churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+churn (void *cls)
 {
   unsigned int i;
   unsigned int j;
@@ -1318,37 +1341,46 @@ run (void *cls,
 {
   unsigned int i;
   struct OpListEntry *entry;
+  uint32_t num_mal_peers;
 
   testbed_peers = peers;
   num_peers_online = 0;
-  for (i = 0 ; i < num_peers ; i++)
+  for (i = 0; i < num_peers; i++)
   {
     entry = make_oplist_entry ();
     entry->index = i;
+    rps_peers[i].index = i;
+    if (NULL != cur_test_run.init_peer)
+      cur_test_run.init_peer (&rps_peers[i]);
     entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
                                                      GNUNET_TESTBED_PIT_IDENTITY,
                                                      &info_cb,
                                                      entry);
   }
 
+  num_mal_peers = round (portion * num_peers);
   GNUNET_assert (num_peers == n_peers);
-  for (i = 0 ; i < n_peers ; i++)
+  for (i = 0; i < n_peers; i++)
   {
     rps_peers[i].index = i;
-    rps_peers[i].op =
-      GNUNET_TESTBED_service_connect (&rps_peers[i],
-                                      peers[i],
-                                      "rps",
-                                      &rps_connect_complete_cb,
-                                      &rps_peers[i],
-                                      &rps_connect_adapter,
-                                      &rps_disconnect_adapter,
-                                      &rps_peers[i]);
+    if ( (rps_peers[i].num_recv_ids < rps_peers[i].num_ids_to_request) ||
+         (i < num_mal_peers) )
+    {
+      rps_peers[i].op =
+        GNUNET_TESTBED_service_connect (&rps_peers[i],
+                                        peers[i],
+                                        "rps",
+                                        &rps_connect_complete_cb,
+                                        &rps_peers[i],
+                                        &rps_connect_adapter,
+                                        &rps_disconnect_adapter,
+                                        &rps_peers[i]);
+    }
   }
 
   if (NULL != churn_task)
     GNUNET_SCHEDULER_cancel (churn_task);
-  shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
+  GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
 }
 
 
@@ -1416,8 +1448,11 @@ main (int argc, char *argv[])
   else if (strstr (argv[0], "_seed_big") != NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
+    num_peers = 1;
     cur_test_run.name = "test-rps-seed-big";
     cur_test_run.main_test = seed_big_cb;
+    cur_test_run.eval_cb = no_eval;
+    timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
   }
 
   else if (strstr (argv[0], "_single_peer_seed") != NULL)
@@ -1479,7 +1514,9 @@ main (int argc, char *argv[])
        (3 == mal_type))
     target_peer = &rps_peer_ids[num_peers - 2];
   if (profiler_eval == cur_test_run.eval_cb)
-    eval_peer = &rps_peers[num_peers - 1];
+    eval_peer = &rps_peers[num_peers - 1];    /* FIXME: eval_peer could be a
+                                                 malicious peer if not careful
+                                                 with the malicious portion */
 
   ok = 1;
   (void) GNUNET_TESTBED_test_run (cur_test_run.name,