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

133
replay.c
View File

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