fix #5010 for /track/transaction

This commit is contained in:
Christian Grothoff 2017-06-19 20:46:24 +02:00
parent a0c66e7919
commit 703c54a279
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
7 changed files with 257 additions and 309 deletions

View File

@ -27,6 +27,69 @@
#include "taler-exchange-httpd_validation.h"
/**
* Add an incoming transaction to the database. Checks if the
* transaction is fresh (not a duplicate) and if so adds it to
* the database.
*
* @param connection the MHD connection to handle
* @param reserve_pub public key of the reserve
* @param amount amount to add to the reserve
* @param execution_time when did we receive the wire transfer
* @param sender_account_details which account send the funds
* @param transfer_details information that uniquely identifies the transfer
* @return MHD result code
*/
static int
execute_admin_add_incoming (struct MHD_Connection *connection,
const struct TALER_ReservePublicKeyP *reserve_pub,
const struct TALER_Amount *amount,
struct GNUNET_TIME_Absolute execution_time,
const json_t *sender_account_details,
const json_t *transfer_details)
{
struct TALER_EXCHANGEDB_Session *session;
int ret;
void *json_str;
if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
{
GNUNET_break (0);
return TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_DB_SETUP_FAILED);
}
json_str = json_dumps (transfer_details,
JSON_INDENT(2));
if (NULL == json_str)
{
GNUNET_break (0);
return TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_PARSER_OUT_OF_MEMORY);
}
ret = TEH_plugin->reserves_in_insert (TEH_plugin->cls,
session,
reserve_pub,
amount,
execution_time,
sender_account_details,
json_str,
strlen (json_str));
free (json_str);
if (GNUNET_SYSERR == ret)
{
GNUNET_break (0);
return TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_ADMIN_ADD_INCOMING_DB_STORE);
}
return TEH_RESPONSE_reply_json_pack (connection,
MHD_HTTP_OK,
"{s:s}",
"status",
(GNUNET_OK == ret)
? "NEW"
: "DUP");
}
/**
* Handle a "/admin/add/incoming" request. Parses the
@ -108,12 +171,12 @@ TEH_ADMIN_handler_admin_add_incoming (struct TEH_RequestHandler *rh,
TALER_EC_ADMIN_ADD_INCOMING_CURRENCY_UNSUPPORTED,
"amount:currency");
}
res = TEH_DB_execute_admin_add_incoming (connection,
&reserve_pub,
&amount,
at,
sender_account_details,
transfer_details);
res = execute_admin_add_incoming (connection,
&reserve_pub,
&amount,
at,
sender_account_details,
transfer_details);
GNUNET_JSON_parse_free (spec);
return res;
}

View File

@ -1145,68 +1145,4 @@ TEH_DB_execute_refresh_link (struct MHD_Connection *connection,
}
/**
* Add an incoming transaction to the database. Checks if the
* transaction is fresh (not a duplicate) and if so adds it to
* the database.
*
* @param connection the MHD connection to handle
* @param reserve_pub public key of the reserve
* @param amount amount to add to the reserve
* @param execution_time when did we receive the wire transfer
* @param sender_account_details which account send the funds
* @param transfer_details information that uniquely identifies the transfer
* @return MHD result code
*/
int
TEH_DB_execute_admin_add_incoming (struct MHD_Connection *connection,
const struct TALER_ReservePublicKeyP *reserve_pub,
const struct TALER_Amount *amount,
struct GNUNET_TIME_Absolute execution_time,
const json_t *sender_account_details,
const json_t *transfer_details)
{
struct TALER_EXCHANGEDB_Session *session;
int ret;
void *json_str;
if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
{
GNUNET_break (0);
return TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_DB_SETUP_FAILED);
}
json_str = json_dumps (transfer_details,
JSON_INDENT(2));
if (NULL == json_str)
{
GNUNET_break (0);
return TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_PARSER_OUT_OF_MEMORY);
}
ret = TEH_plugin->reserves_in_insert (TEH_plugin->cls,
session,
reserve_pub,
amount,
execution_time,
sender_account_details,
json_str,
strlen (json_str));
free (json_str);
if (GNUNET_SYSERR == ret)
{
GNUNET_break (0);
return TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_ADMIN_ADD_INCOMING_DB_STORE);
}
return TEH_RESPONSE_reply_json_pack (connection,
MHD_HTTP_OK,
"{s:s}",
"status",
(GNUNET_OK == ret)
? "NEW"
: "DUP");
}
/* end of taler-exchange-httpd_db.c */

View File

@ -175,27 +175,5 @@ int
TEH_DB_execute_refresh_link (struct MHD_Connection *connection,
const struct TALER_CoinSpendPublicKeyP *coin_pub);
/**
* Add an incoming transaction to the database.
*
* @param connection the MHD connection to handle
* @param reserve_pub public key of the reserve
* @param amount amount to add to the reserve
* @param execution_time when did we receive the wire transfer
* @param sender_account_details which account send the funds
* @param transfer_details information that uniquely identifies the transfer
* @return MHD result code
*/
int
TEH_DB_execute_admin_add_incoming (struct MHD_Connection *connection,
const struct TALER_ReservePublicKeyP *reserve_pub,
const struct TALER_Amount *amount,
struct GNUNET_TIME_Absolute execution_time,
const json_t *sender_account_details,
const json_t *transfer_details);
#endif
/* TALER_EXCHANGE_HTTPD_DB_H */

View File

@ -44,14 +44,14 @@
* @param exec_time execution time of the wire transfer
* @return MHD result code
*/
int
TEH_RESPONSE_reply_track_transaction (struct MHD_Connection *connection,
const struct GNUNET_HashCode *h_contract_terms,
const struct GNUNET_HashCode *h_wire,
const struct TALER_CoinSpendPublicKeyP *coin_pub,
const struct TALER_Amount *coin_contribution,
const struct TALER_WireTransferIdentifierRawP *wtid,
struct GNUNET_TIME_Absolute exec_time)
static int
reply_track_transaction (struct MHD_Connection *connection,
const struct GNUNET_HashCode *h_contract_terms,
const struct GNUNET_HashCode *h_wire,
const struct TALER_CoinSpendPublicKeyP *coin_pub,
const struct TALER_Amount *coin_contribution,
const struct TALER_WireTransferIdentifierRawP *wtid,
struct GNUNET_TIME_Absolute exec_time)
{
struct TALER_ConfirmWirePS cw;
struct TALER_ExchangePublicKeyP pub;
@ -87,29 +87,47 @@ struct DepositWtidContext
{
/**
* Where should we send the reply?
* Deposit details.
*/
struct MHD_Connection *connection;
const struct TALER_DepositTrackPS *tps;
/**
* Hash of the proposal data we are looking up.
* Public key of the merchant.
*/
struct GNUNET_HashCode h_contract_terms;
const struct TALER_MerchantPublicKeyP *merchant_pub;
/**
* Set by #handle_wtid data to the wire transfer ID.
*/
struct TALER_WireTransferIdentifierRawP wtid;
/**
* Set by #handle_wtid data to the coin's contribution to the wire transfer.
*/
struct TALER_Amount coin_contribution;
/**
* Set by #handle_wtid data to the fee charged to the coin.
*/
struct TALER_Amount coin_fee;
/**
* Hash of the wire transfer details we are looking up.
*/
struct GNUNET_HashCode h_wire;
* Set by #handle_wtid data to the wire transfer execution time.
*/
struct GNUNET_TIME_Absolute execution_time;
/**
* Public key we are looking up.
* Set by #handle_wtid to the coin contribution to the transaction
* (that is, @e coin_contribution minus @e coin_fee).
*/
struct TALER_CoinSpendPublicKeyP coin_pub;
struct TALER_Amount coin_delta;
/**
* MHD result code to return.
* Set to #GNUNET_YES by #handle_wtid if the wire transfer is still pending
* (and the above were not set).
* Set to #GNUNET_SYSERR if there was a serious error.
*/
int res;
int pending;
};
@ -136,35 +154,26 @@ handle_wtid_data (void *cls,
struct GNUNET_TIME_Absolute execution_time)
{
struct DepositWtidContext *ctx = cls;
struct TALER_Amount coin_delta;
if (NULL == wtid)
{
ctx->res = TEH_RESPONSE_reply_transfer_pending (ctx->connection,
execution_time);
ctx->pending = GNUNET_YES;
ctx->execution_time = execution_time;
return;
}
else
if (GNUNET_SYSERR ==
TALER_amount_subtract (&ctx->coin_delta,
coin_contribution,
coin_fee))
{
if (GNUNET_SYSERR ==
TALER_amount_subtract (&coin_delta,
coin_contribution,
coin_fee))
{
GNUNET_break (0);
ctx->res = TEH_RESPONSE_reply_internal_db_error (ctx->connection,
TALER_EC_TRACK_TRANSACTION_DB_FEE_INCONSISTENT);
}
else
{
ctx->res = TEH_RESPONSE_reply_track_transaction (ctx->connection,
&ctx->h_contract_terms,
&ctx->h_wire,
&ctx->coin_pub,
&coin_delta,
wtid,
execution_time);
}
GNUNET_break (0);
ctx->pending = GNUNET_SYSERR;
return;
}
ctx->wtid = *wtid;
ctx->execution_time = execution_time;
ctx->coin_contribution = *coin_contribution;
ctx->coin_fee = *coin_fee;
}
@ -172,64 +181,53 @@ handle_wtid_data (void *cls,
* Execute a "/track/transaction". Returns the transfer information
* associated with the given deposit.
*
* @param connection the MHD connection to handle
* @param h_contract_terms hash of the proposal data
* @param h_wire hash of the wire details
* @param coin_pub public key of the coin to link
* @param merchant_pub public key of the merchant
* @return MHD result code
* If it returns a non-error code, the transaction logic MUST
* NOT queue a MHD response. IF it returns an hard error, the
* transaction logic MUST queue a MHD response and set @a mhd_ret. IF
* it returns the soft error code, the function MAY be called again to
* retry and MUST not queue a MHD response.
*
* @param cls closure of type `struct DepositWtidContext *`
* @param connection MHD request which triggered the transaction
* @param session database session to use
* @param[out] mhd_ret set to MHD response status for @a connection,
* if transaction failed (!)
* @return transaction status
*/
int
TEH_DB_execute_track_transaction (struct MHD_Connection *connection,
const struct GNUNET_HashCode *h_contract_terms,
const struct GNUNET_HashCode *h_wire,
const struct TALER_CoinSpendPublicKeyP *coin_pub,
const struct TALER_MerchantPublicKeyP *merchant_pub)
static enum GNUNET_DB_QueryStatus
track_transaction_transaction (void *cls,
struct MHD_Connection *connection,
struct TALER_EXCHANGEDB_Session *session,
int *mhd_ret)
{
int ret;
struct DepositWtidContext ctx;
struct TALER_EXCHANGEDB_Session *session;
struct DepositWtidContext *ctx = cls;
enum GNUNET_DB_QueryStatus qs;
if (NULL == (session = TEH_plugin->get_session (TEH_plugin->cls)))
qs = TEH_plugin->wire_lookup_deposit_wtid (TEH_plugin->cls,
session,
&ctx->tps->h_contract_terms,
&ctx->tps->h_wire,
&ctx->tps->coin_pub,
ctx->merchant_pub,
&handle_wtid_data,
ctx);
if (0 > qs)
{
GNUNET_break (0);
return TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_DB_SETUP_FAILED);
if (GNUNET_DB_STATUS_HARD_ERROR == qs)
{
GNUNET_break (0);
*mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_TRACK_TRANSACTION_DB_FETCH_FAILED);
}
return qs;
}
ctx.connection = connection;
ctx.h_contract_terms = *h_contract_terms;
ctx.h_wire = *h_wire;
ctx.coin_pub = *coin_pub;
ctx.res = GNUNET_SYSERR;
ret = TEH_plugin->wire_lookup_deposit_wtid (TEH_plugin->cls,
session,
h_contract_terms,
h_wire,
coin_pub,
merchant_pub,
&handle_wtid_data,
&ctx);
if (GNUNET_SYSERR == ret)
if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
{
GNUNET_break (0);
GNUNET_break (GNUNET_SYSERR == ctx.res);
return TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_TRACK_TRANSACTION_DB_FETCH_FAILED);
*mhd_ret = TEH_RESPONSE_reply_transaction_unknown (connection,
TALER_EC_TRACK_TRANSACTION_NOT_FOUND);
return GNUNET_DB_STATUS_HARD_ERROR;
}
if (GNUNET_NO == ret)
{
GNUNET_break (GNUNET_SYSERR == ctx.res);
return TEH_RESPONSE_reply_transaction_unknown (connection,
TALER_EC_TRACK_TRANSACTION_NOT_FOUND);
}
if (GNUNET_SYSERR == ctx.res)
{
GNUNET_break (0);
return TEH_RESPONSE_reply_internal_error (connection,
TALER_EC_TRACK_TRANSACTION_WTID_RESOLUTION_ERROR,
"bug resolving deposit wtid");
}
return ctx.res;
return qs;
}
@ -246,9 +244,12 @@ TEH_DB_execute_track_transaction (struct MHD_Connection *connection,
static int
check_and_handle_track_transaction_request (struct MHD_Connection *connection,
const struct TALER_DepositTrackPS *tps,
struct TALER_MerchantPublicKeyP *merchant_pub,
struct TALER_MerchantSignatureP *merchant_sig)
const struct TALER_MerchantPublicKeyP *merchant_pub,
const struct TALER_MerchantSignatureP *merchant_sig)
{
struct DepositWtidContext ctx;
int mhd_ret;
if (GNUNET_OK !=
GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_MERCHANT_TRACK_TRANSACTION,
&tps->purpose,
@ -260,11 +261,29 @@ check_and_handle_track_transaction_request (struct MHD_Connection *connection,
TALER_EC_TRACK_TRANSACTION_MERCHANT_SIGNATURE_INVALID,
"merchant_sig");
}
return TEH_DB_execute_track_transaction (connection,
&tps->h_contract_terms,
&tps->h_wire,
&tps->coin_pub,
merchant_pub);
ctx.pending = GNUNET_NO;
ctx.tps = tps;
ctx.merchant_pub = merchant_pub;
if (GNUNET_OK !=
TEH_DB_run_transaction (connection,
&mhd_ret,
&track_transaction_transaction,
&ctx))
return mhd_ret;
if (GNUNET_YES == ctx.pending)
return TEH_RESPONSE_reply_transfer_pending (connection,
ctx.execution_time);
if (GNUNET_SYSERR == ctx.pending)
return TEH_RESPONSE_reply_internal_db_error (connection,
TALER_EC_TRACK_TRANSACTION_DB_FEE_INCONSISTENT);
return reply_track_transaction (connection,
&tps->h_contract_terms,
&tps->h_wire,
&tps->coin_pub,
&ctx.coin_delta,
&ctx.wtid,
ctx.execution_time);
}

View File

@ -4584,10 +4584,9 @@ postgres_lookup_wire_transfer (void *cls,
* @param merchant_pub merchant public key
* @param cb function to call with the result
* @param cb_cls closure to pass to @a cb
* @return #GNUNET_OK on success, #GNUNET_SYSERR on DB errors,
* #GNUNET_NO if nothing was found
* @return transaction status code
*/
static int
static enum GNUNET_DB_QueryStatus
postgres_wire_lookup_deposit_wtid (void *cls,
struct TALER_EXCHANGEDB_Session *session,
const struct GNUNET_HashCode *h_contract_terms,
@ -4597,7 +4596,7 @@ postgres_wire_lookup_deposit_wtid (void *cls,
TALER_EXCHANGEDB_TrackTransactionCallback cb,
void *cb_cls)
{
PGresult *result;
enum GNUNET_DB_QueryStatus qs;
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_auto_from_type (coin_pub),
GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
@ -4605,122 +4604,76 @@ postgres_wire_lookup_deposit_wtid (void *cls,
GNUNET_PQ_query_param_auto_from_type (merchant_pub),
GNUNET_PQ_query_param_end
};
int nrows;
struct TALER_WireTransferIdentifierRawP wtid;
struct GNUNET_TIME_Absolute exec_time;
struct TALER_Amount amount_with_fee;
struct TALER_Amount deposit_fee;
struct GNUNET_PQ_ResultSpec rs[] = {
GNUNET_PQ_result_spec_auto_from_type ("wtid_raw", &wtid),
GNUNET_PQ_result_spec_absolute_time ("execution_date", &exec_time),
TALER_PQ_result_spec_amount ("amount_with_fee", &amount_with_fee),
TALER_PQ_result_spec_amount ("fee_deposit", &deposit_fee),
GNUNET_PQ_result_spec_end
};
/* check if the melt record exists and get it */
result = GNUNET_PQ_exec_prepared (session->conn,
"lookup_deposit_wtid",
params);
if (PGRES_TUPLES_OK != PQresultStatus (result))
qs = GNUNET_PQ_eval_prepared_singleton_select (session->conn,
"lookup_deposit_wtid",
params,
rs);
if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
{
BREAK_DB_ERR (result, session->conn);
PQclear (result);
return GNUNET_SYSERR;
}
nrows = PQntuples (result);
if (0 == nrows)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"lookup_deposit_wtid returned 0 matching rows\n");
PQclear (result);
/* Check if transaction exists in deposits, so that we just
do not have a WTID yet, if so, do call the CB with a NULL wtid
and return #GNUNET_YES! */
{
struct GNUNET_PQ_QueryParam params2[] = {
GNUNET_PQ_query_param_auto_from_type (coin_pub),
GNUNET_PQ_query_param_auto_from_type (merchant_pub),
GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
GNUNET_PQ_query_param_auto_from_type (h_wire),
GNUNET_PQ_query_param_end
};
result = GNUNET_PQ_exec_prepared (session->conn,
"get_deposit_for_wtid",
params2);
if (PGRES_TUPLES_OK != PQresultStatus (result))
{
BREAK_DB_ERR (result, session->conn);
PQclear (result);
return GNUNET_SYSERR;
}
}
nrows = PQntuples (result);
if (0 == nrows)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"get_deposit_for_wtid returned 0 matching rows\n");
PQclear (result);
return GNUNET_NO;
}
/* Ok, we're aware of the transaction, but it has not yet been
executed */
{
struct GNUNET_TIME_Absolute exec_time;
struct TALER_Amount amount_with_fee;
struct TALER_Amount deposit_fee;
struct GNUNET_PQ_ResultSpec rs[] = {
TALER_PQ_result_spec_amount ("amount_with_fee", &amount_with_fee),
TALER_PQ_result_spec_amount ("fee_deposit", &deposit_fee),
GNUNET_PQ_result_spec_absolute_time ("wire_deadline", &exec_time),
GNUNET_PQ_result_spec_end
};
if (GNUNET_OK !=
GNUNET_PQ_extract_result (result,
rs,
0))
{
GNUNET_break (0);
PQclear (result);
return GNUNET_SYSERR;
}
cb (cb_cls,
NULL,
&amount_with_fee,
&deposit_fee,
exec_time);
PQclear (result);
return GNUNET_YES;
}
}
if (1 != nrows)
{
GNUNET_break (0);
PQclear (result);
return GNUNET_SYSERR;
}
{
struct TALER_WireTransferIdentifierRawP wtid;
struct GNUNET_TIME_Absolute exec_time;
struct TALER_Amount amount_with_fee;
struct TALER_Amount deposit_fee;
struct GNUNET_PQ_ResultSpec rs[] = {
GNUNET_PQ_result_spec_auto_from_type ("wtid_raw", &wtid),
GNUNET_PQ_result_spec_absolute_time ("execution_date", &exec_time),
TALER_PQ_result_spec_amount ("amount_with_fee", &amount_with_fee),
TALER_PQ_result_spec_amount ("fee_deposit", &deposit_fee),
GNUNET_PQ_result_spec_end
};
if (GNUNET_OK !=
GNUNET_PQ_extract_result (result,
rs,
0))
{
GNUNET_break (0);
PQclear (result);
return GNUNET_SYSERR;
}
cb (cb_cls,
&wtid,
&amount_with_fee,
&deposit_fee,
exec_time);
return qs;
}
if (0 > qs)
return qs;
GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"lookup_deposit_wtid returned 0 matching rows\n");
{
/* Check if transaction exists in deposits, so that we just
do not have a WTID yet, if so, do call the CB with a NULL wtid
and return #GNUNET_YES! */
struct GNUNET_PQ_QueryParam params2[] = {
GNUNET_PQ_query_param_auto_from_type (coin_pub),
GNUNET_PQ_query_param_auto_from_type (merchant_pub),
GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
GNUNET_PQ_query_param_auto_from_type (h_wire),
GNUNET_PQ_query_param_end
};
struct GNUNET_TIME_Absolute exec_time;
struct TALER_Amount amount_with_fee;
struct TALER_Amount deposit_fee;
struct GNUNET_PQ_ResultSpec rs2[] = {
TALER_PQ_result_spec_amount ("amount_with_fee", &amount_with_fee),
TALER_PQ_result_spec_amount ("fee_deposit", &deposit_fee),
GNUNET_PQ_result_spec_absolute_time ("wire_deadline", &exec_time),
GNUNET_PQ_result_spec_end
};
qs = GNUNET_PQ_eval_prepared_singleton_select (session->conn,
"get_deposit_for_wtid",
params2,
rs2);
if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
{
/* Ok, we're aware of the transaction, but it has not yet been
executed */
cb (cb_cls,
NULL,
&amount_with_fee,
&deposit_fee,
exec_time);
return qs;
}
return qs;
}
PQclear (result);
return GNUNET_OK;
}

View File

@ -1313,7 +1313,7 @@ test_wire_out (struct TALER_EXCHANGEDB_Session *session,
struct GNUNET_HashCode h_contract_terms_wt2 = h_contract_terms_wt;
h_contract_terms_wt2.bits[0]++;
FAILIF (GNUNET_NO !=
FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
plugin->wire_lookup_deposit_wtid (plugin->cls,
session,
&h_contract_terms_wt2,
@ -1350,7 +1350,7 @@ test_wire_out (struct TALER_EXCHANGEDB_Session *session,
&wire_out_wtid,
&cb_wt_check,
&cb_wt_never));
FAILIF (GNUNET_OK !=
FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
plugin->wire_lookup_deposit_wtid (plugin->cls,
session,
&h_contract_terms_wt,

View File

@ -1754,10 +1754,9 @@ struct TALER_EXCHANGEDB_Plugin
* @param merchant_pub merchant public key
* @param cb function to call with the result
* @param cb_cls closure to pass to @a cb
* @return #GNUNET_OK on success, #GNUNET_SYSERR on DB errors,
* #GNUNET_NO if nothing was found
* @return transaction status code
*/
int
enum GNUNET_DB_QueryStatus
(*wire_lookup_deposit_wtid)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
const struct GNUNET_HashCode *h_contract_terms,