finish serialization/deserialization logic for test wire transfers

This commit is contained in:
Christian Grothoff 2016-01-25 11:21:55 +01:00
parent 9b4a9cde87
commit 891b533a21
2 changed files with 83 additions and 10 deletions

View File

@ -30,8 +30,8 @@
* Callback with prepared transaction.
*
* @param cls closure
* @param buf transaction data to persist
* @param buf_size number of bytes in @a buf
* @param buf transaction data to persist, NULL on error
* @param buf_size number of bytes in @a buf, 0 on error
*/
typedef void
(*TALER_WIRE_PrepareTransactionCallback) (void *cls,

View File

@ -240,6 +240,29 @@ test_wire_validate (const json_t *wire)
}
GNUNET_NETWORK_STRUCT_BEGIN
/**
* Format we used for serialized transaction data.
*/
struct BufFormatP
{
/**
* The wire transfer identifier.
*/
struct TALER_WireTransferIdentifierRawP wtid;
/**
* The amount.
*/
struct TALER_AmountNBO amount;
/* followed by serialized 'wire' JSON data */
};
GNUNET_NETWORK_STRUCT_END
/**
* Prepare for exeuction of a wire transfer. Calls the
* callback with the serialized state.
@ -252,12 +275,44 @@ do_prepare (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *sct)
{
struct TALER_WIRE_PrepareHandle *pth = cls;
char buf[42]; // FIXME: init: serialize buf!
char *wire_enc;
size_t len;
struct BufFormatP bf;
pth->task = NULL;
pth->ptc (pth->ptc_cls,
buf,
sizeof (buf));
/* serialize the state into a 'buf' */
wire_enc = json_dumps (pth->wire,
JSON_COMPACT | JSON_SORT_KEYS);
if (NULL == wire_enc)
{
GNUNET_break (0);
pth->ptc (pth->ptc_cls,
NULL,
0);
GNUNET_free (pth);
return;
}
len = strlen (wire_enc) + 1;
bf.wtid = pth->wtid;
TALER_amount_hton (&bf.amount,
&pth->amount);
{
char buf[sizeof (struct BufFormatP) + len];
memcpy (buf,
&bf,
sizeof (struct BufFormatP));
memcpy (&buf[sizeof (struct BufFormatP)],
wire_enc,
len);
/* finally give the state back */
pth->ptc (pth->ptc_cls,
buf,
sizeof (buf));
}
free (wire_enc); /* not using GNUNET_free(),
as this one is allocated by libjansson */
GNUNET_free (pth);
}
@ -373,10 +428,27 @@ test_execute_wire_transfer (void *cls,
struct TALER_WIRE_ExecuteHandle *eh;
json_t *wire;
struct TALER_Amount amount;
struct TALER_WireTransferIdentifierRawP wtid;
struct BufFormatP bf;
/* FIXME: deserialize buf */
wire = NULL;
if ( (buf_size <= sizeof (struct BufFormatP)) ||
('\0' != buf[buf_size -1]) )
{
GNUNET_break (0);
return NULL;
}
memcpy (&bf,
buf,
sizeof (bf));
TALER_amount_ntoh (&amount,
&bf.amount);
wire = json_loads (&buf[sizeof (struct BufFormatP)],
JSON_REJECT_DUPLICATES,
NULL);
if (NULL == wire)
{
GNUNET_break (0);
return NULL;
}
GNUNET_assert (GNUNET_YES ==
test_wire_validate (wire));
@ -384,11 +456,12 @@ test_execute_wire_transfer (void *cls,
eh->cc = cc;
eh->cc_cls = cc_cls;
eh->aaih = TALER_BANK_admin_add_incoming (tc->bank,
&wtid,
&bf.wtid,
&amount,
wire,
&execute_cb,
eh);
json_decref (wire);
if (NULL == eh->aaih)
{
GNUNET_break (0);