/**
* @file consensus/gnunet-consensus.c
- * @brief
+ * @brief profiling tool for gnunet-consensus
* @author Florian Dold
*/
#include "platform.h"
+#include "gnunet_common.h"
#include "gnunet_util_lib.h"
#include "gnunet_consensus_service.h"
+#include "gnunet_testbed_service.h"
+static unsigned int num_peers = 2;
+static unsigned int replication = 1;
-/**
- * Handle to the consensus service
- */
-static struct GNUNET_CONSENSUS_Handle *consensus;
-/**
- * Session id
- */
-static char *session_id_str;
+static unsigned int num_values = 5;
-/**
- * File handle to STDIN
- */
-static struct GNUNET_DISK_FileHandle *stdin_fh;
+static struct GNUNET_TIME_Relative conclude_timeout;
-/**
- * Task for reading from stdin
- */
-static GNUNET_SCHEDULER_TaskIdentifier stdin_tid = GNUNET_SCHEDULER_NO_TASK;
+static struct GNUNET_CONSENSUS_Handle **consensus_handles;
+
+static unsigned int num_connected_handles;
+
+static struct GNUNET_TESTBED_Peer **peers;
+
+static struct GNUNET_PeerIdentity *peer_ids;
+
+static unsigned int num_retrieved_peer_ids;
+static struct GNUNET_HashCode session_id;
+
+/**
+ * Signature of the event handler function called by the
+ * respective event controller.
+ *
+ * @param cls closure
+ * @param event information about the event
+ */
static void
-stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+controller_cb(void *cls,
+ const struct GNUNET_TESTBED_EventInformation *event)
+{
+ GNUNET_assert (0);
+}
/**
* Called when a conclusion was successful.
*
* @param cls
- * @param num_peers_in_consensus
- * @param peers_in_consensus
+ * @param group
+ * @return GNUNET_YES if more consensus groups should be offered, GNUNET_NO if not
*/
+static int
+conclude_cb (void *cls, const struct GNUNET_CONSENSUS_Group *group)
+{
+ return GNUNET_NO;
+}
+
+
+
static void
-conclude_cb (void *cls,
- unsigned int num_peers_in_consensus,
- const struct GNUNET_PeerIdentity *peers_in_consensus)
+generate_indices (int *indices)
{
- printf("reached conclusion with %d peers\n", num_peers_in_consensus);
- GNUNET_SCHEDULER_shutdown ();
+ int j;
+ j = 0;
+ while (j < replication)
+ {
+ int n;
+ int k;
+ int repeat;
+ n = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
+ repeat = GNUNET_NO;
+ for (k = 0; k < j; k++)
+ if (indices[k] == n)
+ {
+ repeat = GNUNET_YES;
+ break;
+ }
+ if (GNUNET_NO == repeat)
+ indices[j++] = n;
+ }
}
static void
-insert_done_cb (void *cls,
- int success)
+do_consensus ()
{
- struct GNUNET_CONSENSUS_Element *element = cls;
+ int unique_indices[replication];
+ int i;
- GNUNET_free (element);
- if (GNUNET_YES != success)
+ for (i = 0; i < num_values; i++)
{
- printf ("insert failed\n");
- GNUNET_SCHEDULER_shutdown ();
- return;
+ int j;
+ struct GNUNET_HashCode *val;
+ struct GNUNET_CONSENSUS_Element *element;
+ generate_indices(unique_indices);
+
+ val = GNUNET_malloc (sizeof *val);
+ GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, val);
+
+ element = GNUNET_malloc (sizeof *element);
+ element->data = val;
+ element->size = sizeof *val;
+
+ for (j = 0; j < replication; j++)
+ {
+ int cid;
+ cid = unique_indices[j];
+ GNUNET_CONSENSUS_insert (consensus_handles[cid], element, NULL, NULL);
+ }
}
- GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == stdin_tid);
- stdin_tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fh,
- &stdin_cb, NULL);
+
+ for (i = 0; i < num_peers; i++)
+ GNUNET_CONSENSUS_conclude (consensus_handles[i], conclude_timeout, 0, conclude_cb, consensus_handles[i]);
}
/**
- * Called whenever we can read stdin non-blocking
+ * Callback to be called when a service connect operation is completed
*
- * @param cls unused
- * @param tc scheduler context
+ * @param cls the callback closure from functions generating an operation
+ * @param op the operation that has been finished
+ * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
+ * @param emsg error message in case the operation has failed; will be NULL if
+ * operation has executed successfully.
*/
static void
-stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+connect_complete (void *cls,
+ struct GNUNET_TESTBED_Operation *op,
+ void *ca_result,
+ const char *emsg)
{
- char buf[1024];
- char *ret;
- struct GNUNET_CONSENSUS_Element *element;
-
- stdin_tid = GNUNET_SCHEDULER_NO_TASK;
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return; /* we're done here */
- ret = fgets (buf, 1024, stdin);
- if (NULL == ret)
+ struct GNUNET_CONSENSUS_Handle **chp;
+
+ if (NULL != emsg)
{
- if (feof (stdin))
- {
- printf ("concluding ...\n");
- GNUNET_CONSENSUS_conclude (consensus, GNUNET_TIME_UNIT_FOREVER_REL, conclude_cb, NULL);
- }
- return;
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "testbed connect emsg: %s\n", emsg);
+ GNUNET_assert (0);
}
- printf("read: %s", buf);
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect complete\n");
+
+ chp = (struct GNUNET_CONSENSUS_Handle **) cls;
+ *chp = (struct GNUNET_CONSENSUS_Handle *) ca_result;
+ num_connected_handles++;
+
+ if (num_connected_handles == num_peers)
+ {
+ do_consensus ();
+ }
+}
+
- element = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_Element) + strlen(buf) + 1);
- element->type = 0;
- element->size = strlen(buf) + 1;
- element->data = &element[1];
- strcpy ((char *) &element[1], buf);
- GNUNET_CONSENSUS_insert (consensus, element, &insert_done_cb, element);
+static int
+new_element_cb (void *cls,
+ struct GNUNET_CONSENSUS_Element *element)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received new element\n");
+ return GNUNET_YES;
}
/**
- * Called when a new element was received from another peer, or an error occured.
- *
- * May deliver duplicate values.
- *
- * Elements given to a consensus operation by the local peer are NOT given
- * to this callback.
+ * Adapter function called to establish a connection to
+ * a service.
*
* @param cls closure
- * @param element new element, NULL on error
- * @return GNUNET_OK if the valid is well-formed and should be added to the consensus,
- * GNUNET_SYSERR if the element should be ignored and not be propagated
+ * @param cfg configuration of the peer to connect to; will be available until
+ * GNUNET_TESTBED_operation_done() is called on the operation returned
+ * from GNUNET_TESTBED_service_connect()
+ * @return service handle to return in 'op_result', NULL on error
*/
-static int
-cb (void *cls,
- struct GNUNET_CONSENSUS_Element *element)
+static void *
+connect_adapter (void *cls,
+ const struct GNUNET_CONFIGURATION_Handle *cfg)
{
- printf("got element\n");
- return GNUNET_YES;
+ struct GNUNET_CONSENSUS_Handle *consensus;
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect adapter, %d peers\n", num_peers);
+ consensus = GNUNET_CONSENSUS_create (cfg, num_peers, peer_ids, &session_id, new_element_cb, NULL);
+ GNUNET_assert (NULL != consensus);
+ return consensus;
}
/**
- * Function run on shutdown to clean up.
+ * Adapter function called to destroy a connection to
+ * a service.
*
- * @param cls the statistics handle
- * @param tc scheduler context
+ * @param cls closure
+ * @param op_result service handle returned from the connect adapter
*/
static void
-shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+disconnect_adapter(void *cls, void *op_result)
{
- GNUNET_log (GNUNET_ERROR_TYPE_INFO, "shutting down\n");
- if (NULL != consensus)
- {
- GNUNET_CONSENSUS_destroy (consensus);
- consensus = NULL;
- }
+ /* FIXME: what to do here? */
}
+/**
+ * Callback to be called when the requested peer information is available
+ *
+ * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
+ * @param op the operation this callback corresponds to
+ * @param pinfo the result; will be NULL if the operation has failed
+ * @param emsg error message if the operation has failed; will be NULL if the
+ * operation is successfull
+ */
static void
-run (void *cls, char *const *args, const char *cfgfile,
- const struct GNUNET_CONFIGURATION_Handle *cfg)
+peer_info_cb (void *cb_cls,
+ struct GNUNET_TESTBED_Operation *op,
+ const struct GNUNET_TESTBED_PeerInformation *pinfo,
+ const char *emsg)
{
- struct GNUNET_HashCode sid;
- struct GNUNET_PeerIdentity *pids;
- int count;
+ struct GNUNET_PeerIdentity *p;
int i;
- if (NULL == session_id_str)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no session id given\n");
- return;
- }
+ GNUNET_assert (NULL == emsg);
- for (count = 0; NULL != args[count]; count++);
-
- if (0 != count)
- {
- pids = GNUNET_malloc (count * sizeof (struct GNUNET_PeerIdentity));
+ p = (struct GNUNET_PeerIdentity *) cb_cls;
+
+ if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
+ {
+ *p = *pinfo->result.id;
+ num_retrieved_peer_ids++;
+ if (num_retrieved_peer_ids == num_peers)
+ for (i = 0; i < num_peers; i++)
+ GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, &consensus_handles[i],
+ connect_adapter, disconnect_adapter, NULL);
}
else
{
- pids = NULL;
+ GNUNET_assert (0);
}
+}
- for (i = 0; i < count; i++)
+
+static void
+test_master (void *cls,
+ unsigned int num_peers,
+ struct GNUNET_TESTBED_Peer **started_peers)
+{
+ int i;
+
+
+ GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
+
+
+ peers = started_peers;
+
+ peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
+
+ consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
+
+ for (i = 0; i < num_peers; i++)
+ GNUNET_TESTBED_peer_get_information (peers[i],
+ GNUNET_TESTBED_PIT_IDENTITY,
+ peer_info_cb,
+ &peer_ids[i]);
+}
+
+static void
+run (void *cls, char *const *args, const char *cfgfile,
+ const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+ static char *session_str = "gnunet-consensus/test";
+
+ if (num_peers < replication)
{
- int ret;
- ret = GNUNET_CRYPTO_hash_from_string (args[i], &pids[i].hashPubKey);
- if (GNUNET_OK != ret)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "peer identity '%s' is malformed\n", args[i]);
- return;
- }
+ fprintf (stderr, "k must be <=n\n");
+ return;
}
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
- &shutdown_task, NULL);
-
- consensus =
- GNUNET_CONSENSUS_create (cfg,
- count, pids,
- &sid,
- &cb, NULL);
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "running gnunet-consensus\n");
- GNUNET_CONSENSUS_begin (consensus);
+ GNUNET_CRYPTO_hash (session_str, strlen(session_str), &session_id);
-
- stdin_fh = GNUNET_DISK_get_handle_from_native (stdin);
- stdin_tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fh,
- &stdin_cb, NULL);
+ (void) GNUNET_TESTBED_test_run ("gnunet-consensus",
+ cfgfile,
+ num_peers,
+ 0,
+ controller_cb,
+ NULL,
+ test_master,
+ NULL);
}
main (int argc, char **argv)
{
static const struct GNUNET_GETOPT_CommandLineOption options[] = {
- { 's', "session-id", "ID",
- gettext_noop ("session identifier"),
- GNUNET_YES, &GNUNET_GETOPT_set_string, &session_id_str },
- GNUNET_GETOPT_OPTION_END
- };
- GNUNET_PROGRAM_run (argc, argv, "gnunet-consensus",
+ { 'n', "num-peers", NULL,
+ gettext_noop ("number of peers in consensus"),
+ GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
+ { 'k', "value-replication", NULL,
+ gettext_noop ("how many peers receive one value?"),
+ GNUNET_YES, &GNUNET_GETOPT_set_uint, &replication },
+ { 'x', "num-values", NULL,
+ gettext_noop ("number of values"),
+ GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_values },
+ { 't', "timeout", NULL,
+ gettext_noop ("consensus timeout"),
+ GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &conclude_timeout },
+ GNUNET_GETOPT_OPTION_END
+ };
+ conclude_timeout = GNUNET_TIME_UNIT_SECONDS;
+ GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus",
"help",
- options, &run, NULL);
+ options, &run, NULL, GNUNET_YES);
return 0;
}
+