added json-output

This commit is contained in:
Özgür Kesim 2022-09-28 16:28:24 +02:00
parent d5d37b6314
commit 6bda23fb38
Signed by: oec
GPG Key ID: 3D76A56D79EDD9D7

125
replay.c
View File

@ -61,6 +61,9 @@ struct transcript
struct msg *msgs; // Array must be of length 4*n struct msg *msgs; // Array must be of length 4*n
struct BRANDT_Auction *auction; struct BRANDT_Auction *auction;
struct result *results;
size_t results_len;
struct result *expected; struct result *expected;
size_t expected_len; size_t expected_len;
uint16_t id; uint16_t id;
@ -69,12 +72,58 @@ struct transcript
static struct transcript tr; static struct transcript tr;
static void
print_result( struct transcript *tr,
char *error)
{
json_t *output;
json_t *results;
if (NULL != error)
{
output = json_pack("{s:s}", "error", error);
GNUNET_assert(output);
json_dumpfd(output, 1, JSON_INDENT(2));
return;
}
GNUNET_assert(NULL != tr);
output = json_object();
results = json_array();
GNUNET_assert(output);
GNUNET_assert(results);
for (size_t i = 0; i < tr->results_len; i++)
{
json_t *result = json_pack("{s:i, s:s}",
"bidder", tr->results[i].bidder,
"price", tr->results[i].price);
GNUNET_assert(result);
GNUNET_assert(-1 !=
json_array_append_new(results, result));
}
GNUNET_assert(-1 !=
json_object_set_new(output,
"winners",
results));
json_dumpfd(output, 1, JSON_INDENT(2));
}
static void static void
tr_result (void *arg, tr_result (void *arg,
struct BRANDT_Result results[], struct BRANDT_Result results[],
uint16_t results_len) uint16_t results_len)
{ {
struct transcript *tr = (struct transcript *) arg; struct transcript *tr = (struct transcript *) arg;
tr->results = GNUNET_new_array(results_len, struct result);
tr->results_len = results_len;
for (uint16_t i = 0; i < results_len; i++) for (uint16_t i = 0; i < results_len; i++)
{ {
GNUNET_log ( GNUNET_log (
@ -84,6 +133,10 @@ tr_result (void *arg,
results[i].status, results[i].status,
results[i].price, results[i].price,
tr->prices[results[i].price]); tr->prices[results[i].price]);
tr->results[i].bidder = results[i].bidder;
tr->results[i].price_idx = results[i].price;
tr->results[i].price = tr->prices[results[i].price];
} }
for (uint16_t i = 0; i < tr->expected_len; i++) for (uint16_t i = 0; i < tr->expected_len; i++)
@ -96,8 +149,9 @@ tr_result (void *arg,
tr->expected[i].price); tr->expected[i].price);
} }
// TODO: compare computed and expected results!
// TODO: generate JSON-output print_result(tr, NULL);
} }
@ -160,15 +214,14 @@ replay_transcript (void *arg)
tr->public ? tr->edc : NULL); tr->public ? tr->edc : NULL);
if (!tr->auction) if (!tr->auction)
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "REPLAY BRANDT_new() failed.\n"); print_result(NULL, "REPLAY BRANDT_new() failed.");
_exit (1); _exit (1);
// TODO: generate JSON error response
} }
} }
enum GNUNET_GenericReturnValue void
parse_json_stdin (struct transcript *tr) parse_json_stdin (struct transcript *tr)
{ {
json_error_t jerror; json_error_t jerror;
@ -181,10 +234,12 @@ parse_json_stdin (struct transcript *tr)
if (!jtr) if (!jtr)
{ {
// TODO: generate JSON error response char err[4096];
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, snprintf(err,
"REPLAY failed to parse json: %s in line %d, column %d (pos %d)\n", sizeof(err),
"failed to parse json: %s in line %d, column %d (pos %d)",
jerror.text, jerror.line, jerror.column, jerror.position); jerror.text, jerror.line, jerror.column, jerror.position);
print_result(NULL, err);
_exit (1); _exit (1);
} }
@ -202,9 +257,8 @@ parse_json_stdin (struct transcript *tr)
auc = json_object_get (jtr, "auction"); auc = json_object_get (jtr, "auction");
if (NULL == auc) if (NULL == auc)
{ {
// TODO error message as json print_result(NULL,
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no auction found in input");
"no auction found in JSON");
_exit (1); _exit (1);
} }
@ -223,9 +277,8 @@ parse_json_stdin (struct transcript *tr)
if (!json_is_array (prices)) if (!json_is_array (prices))
{ {
// TODO error message as json print_result(NULL,
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no prices found in input");
"no prices found in JSON\n");
_exit (1); _exit (1);
} }
@ -235,8 +288,12 @@ parse_json_stdin (struct transcript *tr)
{ {
if (!json_is_string (val)) if (!json_is_string (val))
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, char err[256];
snprintf(err,
sizeof(err),
"price %ld is not a string\n", idx); "price %ld is not a string\n", idx);
print_result(NULL,
err);
_exit (1); _exit (1);
} }
tr->prices[idx] = (char *) json_string_value (val); tr->prices[idx] = (char *) json_string_value (val);
@ -253,9 +310,9 @@ parse_json_stdin (struct transcript *tr)
bidders = json_object_get (jtr, "bidders"); bidders = json_object_get (jtr, "bidders");
if (!bidders || !json_is_array (bidders)) if (!bidders || !json_is_array (bidders))
{ {
// TODO: json-error
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"bidders missing or not an array\n"); "bidders missing or not an array\n");
print_result(NULL, "bidders missing or not an array");
_exit (1); _exit (1);
} }
@ -273,9 +330,7 @@ parse_json_stdin (struct transcript *tr)
messages = json_object_get (jtr, "transcript"); messages = json_object_get (jtr, "transcript");
if (!json_is_array (messages)) if (!json_is_array (messages))
{ {
// TODO: json-error print_result(NULL, "transcript missing or not an array");
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"transcript missing or not an array\n");
_exit (1); _exit (1);
} }
@ -283,9 +338,7 @@ parse_json_stdin (struct transcript *tr)
if (nm != (4 * tr->n)) if (nm != (4 * tr->n))
{ {
// TODO: json-error print_result(NULL, "wrong number of messages in transript");
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"wrong number of messages in transcript\n");
_exit (1); _exit (1);
} }
@ -311,12 +364,13 @@ parse_json_stdin (struct transcript *tr)
(const char**) &error, (const char**) &error,
NULL)) NULL))
{ {
// TODO: json-error char err[4096];
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, snprintf(err,
"error parsing message[%ld] in transcript: %s\njson:\n%s\n", sizeof(err),
"error parsing message[%ld] in transcript: %s",
idx, idx,
error, error);
json_dumps (val, JSON_INDENT (2))); print_result(NULL, err);
_exit (1); _exit (1);
} }
@ -337,7 +391,6 @@ parse_json_stdin (struct transcript *tr)
if (!json_is_array (winners)) if (!json_is_array (winners))
{ {
// TODO: json-warning
GNUNET_log (GNUNET_ERROR_TYPE_WARNING, GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"winners not provided, continuing without\n"); "winners not provided, continuing without\n");
goto CONT; goto CONT;
@ -366,12 +419,13 @@ parse_json_stdin (struct transcript *tr)
(const char**) &error, (const char**) &error,
NULL)) NULL))
{ {
// TODO: json-error char err[4096];
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, snprintf(err,
"error parsing winners[%ld] in transcript: %s\njson:\n%s\n", sizeof(err),
"error parsing winners[%ld] in transcript: %s",
idx, idx,
error, error);
json_dumps (val, JSON_INDENT (2)));
_exit (1); _exit (1);
} }
@ -379,8 +433,6 @@ parse_json_stdin (struct transcript *tr)
CONT: CONT:
} }
return GNUNET_OK;
} }
@ -395,10 +447,7 @@ main (int argc, char *argv[])
BRANDT_init (); BRANDT_init ();
GNUNET_assert (GNUNET_OK == parse_json_stdin (&tr);
parse_json_stdin (&tr));
// TODO: error as json-output if failure
//
tr.edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024 * 1024 * 40, 1024); tr.edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024 * 1024 * 40, 1024);
GNUNET_SCHEDULER_run (&replay_transcript, &tr); GNUNET_SCHEDULER_run (&replay_transcript, &tr);
GNUNET_CRYPTO_ecc_dlog_release (tr.edc); GNUNET_CRYPTO_ecc_dlog_release (tr.edc);