implement DB plugin function to obtain reserve information, migrate logic to simplified API spec (#3887); use plugin API in /payback DB implementation

This commit is contained in:
Christian Grothoff 2017-04-01 23:43:55 +02:00
parent c329b92ccf
commit b293bda4ac
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
11 changed files with 245 additions and 90 deletions

View File

@ -95,14 +95,13 @@ verify_payback_signature_ok (const struct TALER_EXCHANGE_PaybackHandle *ph,
struct TALER_ExchangeSignatureP exchange_sig;
struct TALER_Amount amount;
struct GNUNET_TIME_Absolute deadline;
const char *wire_subject;
const struct TALER_EXCHANGE_Keys *key_state;
struct GNUNET_JSON_Specification spec[] = {
GNUNET_JSON_spec_fixed_auto ("exchange_sig", &exchange_sig),
GNUNET_JSON_spec_fixed_auto ("exchange_pub", &exchange_pub),
TALER_JSON_spec_amount ("amount", &amount),
GNUNET_JSON_spec_absolute_time ("payback_deadline", &deadline),
GNUNET_JSON_spec_string ("wire_subject", &wire_subject),
GNUNET_JSON_spec_fixed_auto ("reserve_pub", &pc.reserve_pub),
GNUNET_JSON_spec_end()
};
@ -128,9 +127,6 @@ verify_payback_signature_ok (const struct TALER_EXCHANGE_PaybackHandle *ph,
TALER_amount_hton (&pc.payback_amount,
&amount);
pc.coin_pub = ph->coin_pub;
GNUNET_CRYPTO_hash (wire_subject,
strlen (wire_subject),
&pc.h_wire_subject);
if (GNUNET_OK !=
GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_EXCHANGE_CONFIRM_PAYBACK,
&pc.purpose,
@ -145,7 +141,7 @@ verify_payback_signature_ok (const struct TALER_EXCHANGE_PaybackHandle *ph,
TALER_EC_NONE,
&amount,
deadline,
wire_subject,
&pc.reserve_pub,
json);
return GNUNET_OK;
}

View File

@ -2269,6 +2269,7 @@ TEH_DB_execute_track_transaction (struct MHD_Connection *connection,
* @param coin information about the coin
* @param value how much are coins of the @a coin's denomination worth?
* @param h_blind blinded coin to use for the lookup
* @param coin_blind blinding factor used (for later verification by the auditor)
* @param coin_sig signature of the coin (to be stored)
* @return MHD result code
*/
@ -2277,13 +2278,13 @@ TEH_DB_execute_payback (struct MHD_Connection *connection,
const struct TALER_CoinPublicInfo *coin,
const struct TALER_Amount *value,
const struct GNUNET_HashCode *h_blind,
const struct TALER_DenominationBlindingKeyP *coin_blind,
const struct TALER_CoinSpendSignatureP *coin_sig)
{
int ret;
struct TALER_EXCHANGEDB_Session *session;
struct TALER_EXCHANGEDB_TransactionList *tl;
struct TALER_EXCHANGEDB_CollectableBlindcoin collectable;
char wire_subject[42]; // FIXME: size? (#3887)
struct TALER_ReservePublicKeyP reserve_pub;
struct TALER_Amount amount;
struct TALER_Amount spent;
struct GNUNET_TIME_Absolute payback_deadline;
@ -2297,12 +2298,12 @@ TEH_DB_execute_payback (struct MHD_Connection *connection,
START_TRANSACTION (session, connection);
/* FIXME (#3887): not _exactly_ the right call, we need to get the
reserve's incoming wire transfer data, not 'collectable' */
ret = TEH_plugin->get_withdraw_info (TEH_plugin->cls,
session,
h_blind,
&collectable);
/* Check whether a payback is allowed, and if so, to which
reserve / account the money should go */
ret = TEH_plugin->get_reserve_by_h_blind (TEH_plugin->cls,
session,
h_blind,
&reserve_pub);
if (GNUNET_SYSERR == ret)
{
GNUNET_break (0);
@ -2358,8 +2359,16 @@ TEH_DB_execute_payback (struct MHD_Connection *connection,
TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
tl);
/* FIXME: add coin to list of wire transfers for payback */
// ret = TEH_plugin->(); // #3887
/* add coin to list of wire transfers for payback */
ret = TEH_plugin->insert_payback_request (TEH_plugin->cls,
session,
&reserve_pub,
coin,
coin_sig,
coin_blind,
h_blind,
&amount,
&payback_deadline);
if (GNUNET_SYSERR == ret)
{
TALER_LOG_WARNING ("Failed to store /payback information in database\n");
@ -2373,7 +2382,7 @@ TEH_DB_execute_payback (struct MHD_Connection *connection,
return TEH_RESPONSE_reply_payback_success (connection,
&coin->coin_pub,
wire_subject,
&reserve_pub,
&amount,
payback_deadline);
}

View File

@ -246,6 +246,7 @@ TEH_DB_execute_track_transaction (struct MHD_Connection *connection,
* @param coin information about the coin
* @param value how much are coins of the @a coin's denomination worth?
* @param h_blind blinded coin to use for the lookup
* @param coin_blind blinding factor used (for later verification by the auditor)
* @param coin_sig signature of the coin
* @return MHD result code
*/
@ -254,6 +255,7 @@ TEH_DB_execute_payback (struct MHD_Connection *connection,
const struct TALER_CoinPublicInfo *coin,
const struct TALER_Amount *value,
const struct GNUNET_HashCode *h_blind,
const struct TALER_DenominationBlindingKeyP *coin_blind,
const struct TALER_CoinSpendSignatureP *coin_sig);

View File

@ -134,6 +134,7 @@ verify_and_execute_payback (struct MHD_Connection *connection,
coin,
&value,
&h_blind,
coin_bks,
coin_sig);
}

View File

@ -1320,7 +1320,8 @@ TEH_RESPONSE_reply_payback_unknown (struct MHD_Connection *connection,
* A wallet asked for /payback, return the successful response.
*
* @param connection connection to the client
* @param wire_subject the wire subject we will use for the pay back operation
* @param coin_pub coin for which we are processing the payback request
* @param reserve_pub public key of the reserve that will receive the payback
* @param amount the amount we will wire back
* @param payback_deadline deadline by which the exchange promises to pay
* @return MHD result code
@ -1328,7 +1329,7 @@ TEH_RESPONSE_reply_payback_unknown (struct MHD_Connection *connection,
int
TEH_RESPONSE_reply_payback_success (struct MHD_Connection *connection,
const struct TALER_CoinSpendPublicKeyP *coin_pub,
const char *wire_subject,
const struct TALER_ReservePublicKeyP *reserve_pub,
const struct TALER_Amount *amount,
struct GNUNET_TIME_Absolute payback_deadline)
{
@ -1342,16 +1343,14 @@ TEH_RESPONSE_reply_payback_success (struct MHD_Connection *connection,
TALER_amount_hton (&pc.payback_amount,
amount);
pc.coin_pub = *coin_pub;
GNUNET_CRYPTO_hash (wire_subject,
strlen (wire_subject),
&pc.h_wire_subject);
pc.reserve_pub = *reserve_pub;
TEH_KS_sign (&pc.purpose,
&pub,
&sig);
return TEH_RESPONSE_reply_json_pack (connection,
MHD_HTTP_OK,
"{s:s, s:o, s:o, s:o, s:o}",
"wire_subject", wire_subject,
"{s:o, s:o, s:o, s:o, s:o}",
"reserve_pub", GNUNET_JSON_from_data_auto (reserve_pub),
"payback_deadline", GNUNET_JSON_from_time_abs (payback_deadline),
"amount", TALER_JSON_from_amount (amount),
"exchange_sig", GNUNET_JSON_from_data_auto (&sig),

View File

@ -577,7 +577,7 @@ TEH_RESPONSE_reply_payback_unknown (struct MHD_Connection *connection,
*
* @param connection connection to the client
* @param coin_pub coin for which we are processing the payback request
* @param wire_subject the wire subject we will use for the pay back operation
* @param reserve_pub public key of the reserve that will receive the payback
* @param amount the amount we will wire back
* @param payback_deadline deadline by which the exchange promises to pay
* @return MHD result code
@ -585,7 +585,7 @@ TEH_RESPONSE_reply_payback_unknown (struct MHD_Connection *connection,
int
TEH_RESPONSE_reply_payback_success (struct MHD_Connection *connection,
const struct TALER_CoinSpendPublicKeyP *coin_pub,
const char *wire_subject,
const struct TALER_ReservePublicKeyP *reserve_pub,
const struct TALER_Amount *amount,
struct GNUNET_TIME_Absolute payback_deadline);

View File

@ -39,6 +39,7 @@ common_free_reserve_history (void *cls,
switch(rh->type)
{
case TALER_EXCHANGEDB_RO_BANK_TO_EXCHANGE:
case TALER_EXCHANGEDB_RO_EXCHANGE_TO_BANK:
bt = rh->details.bank;
if (NULL != bt->sender_account_details)
json_decref (bt->sender_account_details);
@ -52,6 +53,9 @@ common_free_reserve_history (void *cls,
GNUNET_CRYPTO_rsa_public_key_free (cbc->denom_pub.rsa_public_key);
GNUNET_free (cbc);
break;
case TALER_EXCHANGEDB_RO_PAYBACK_COIN:
GNUNET_free (rh->details.payback);
break;
}
backref = rh;
rh = rh->next;
@ -125,6 +129,9 @@ common_free_coin_transaction_list (void *cls,
GNUNET_CRYPTO_rsa_signature_free (list->details.refund->coin.denom_sig.rsa_signature);
GNUNET_free (list->details.refund);
break;
case TALER_EXCHANGEDB_TT_PAYBACK:
GNUNET_free (list->details.payback);
break;
}
GNUNET_free (list);
list = next;

View File

@ -1383,6 +1383,15 @@ postgres_prepare (PGconn *db_conn)
" ORDER BY wireout_uuid ASC",
1, NULL);
/* Used in #postgres_get_reserve_by_h_blind() */
PREPARE ("reserve_by_h_blind",
"SELECT"
" reserve_pub"
" FROM reserves_out"
" WHERE h_blind_ev=$1"
" LIMIT 1;",
1, NULL);
PREPARE ("gc_denominations",
"DELETE"
" FROM denominations"
@ -5400,6 +5409,107 @@ postgres_select_wire_out_above_serial_id (void *cls,
}
/**
* Function called to add a request for an emergency payback for a
* coin. The funds are to be added back to the reserve. The function
* should return the @a deadline by which the exchange will trigger a
* wire transfer back to the customer's account for the reserve.
*
* @param cls closure
* @param session database connection
* @param reserve_pub public key of the reserve that is being refunded
* @param coin information about the coin
* @param coin_sig signature of the coin of type #TALER_SIGNATURE_WALLET_COIN_PAYBACK
* @param coin_blind blinding key of the coin
* @param h_blind_ev blinded envelope, as calculated by the exchange
* @param amount total amount to be paid back
* @param receiver_account_details who should receive the funds
* @param[out] deadline set to absolute time by when the exchange plans to pay it back
* @return #GNUNET_OK on success,
* #GNUNET_SYSERR on DB errors
*/
static int
postgres_insert_payback_request (void *cls,
struct TALER_EXCHANGEDB_Session *session,
const struct TALER_ReservePublicKeyP *reserve_pub,
const struct TALER_CoinPublicInfo *coin,
const struct TALER_CoinSpendSignatureP *coin_sig,
const struct TALER_DenominationBlindingKeyP *coin_blind,
const struct GNUNET_HashCode *h_blinded_ev,
const struct TALER_Amount *amount,
struct GNUNET_TIME_Absolute *deadline)
{
GNUNET_break (0);
return GNUNET_SYSERR;
}
/**
* Obtain information about which reserve a coin was generated
* from given the hash of the blinded coin.
*
* @param cls closure
* @param session a session
* @param h_blind_ev hash of the blinded coin
* @param[out] reserve_pub set to information about the reserve (on success only)
* @return #GNUNET_OK on success,
* #GNUNET_NO if there are no entries,
* #GNUNET_SYSERR on DB errors
*/
static int
postgres_get_reserve_by_h_blind (void *cls,
struct TALER_EXCHANGEDB_Session *session,
const struct GNUNET_HashCode *h_blind_ev,
struct TALER_ReservePublicKeyP *reserve_pub)
{
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_auto_from_type (h_blind_ev),
GNUNET_PQ_query_param_end
};
PGresult *result;
result = GNUNET_PQ_exec_prepared (session->conn,
"reserve_by_h_blind",
params);
if (PGRES_TUPLES_OK !=
PQresultStatus (result))
{
BREAK_DB_ERR (result, session->conn);
PQclear (result);
return GNUNET_SYSERR;
}
int nrows;
nrows = PQntuples (result);
if (0 == nrows)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"reserve_by_h_blind() returned 0 matching rows\n");
PQclear (result);
return GNUNET_NO;
}
{
struct GNUNET_PQ_ResultSpec rs[] = {
GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
reserve_pub),
GNUNET_PQ_result_spec_end
};
if (GNUNET_OK !=
GNUNET_PQ_extract_result (result,
rs,
0))
{
GNUNET_break (0);
PQclear (result);
return GNUNET_SYSERR;
}
}
PQclear (result);
return GNUNET_OK;
}
/**
* Initialize Postgres database subsystem.
*
@ -5500,6 +5610,8 @@ libtaler_plugin_exchangedb_postgres_init (void *cls)
plugin->select_reserves_in_above_serial_id = &postgres_select_reserves_in_above_serial_id;
plugin->select_reserves_out_above_serial_id = &postgres_select_reserves_out_above_serial_id;
plugin->select_wire_out_above_serial_id = &postgres_select_wire_out_above_serial_id;
plugin->insert_payback_request = &postgres_insert_payback_request;
plugin->get_reserve_by_h_blind = &postgres_get_reserve_by_h_blind;
return plugin;
}

View File

@ -1345,7 +1345,7 @@ struct TALER_EXCHANGE_PaybackHandle;
* @param ec taler-specific error code, #TALER_EC_NONE on success
* @param amount amount the exchange will wire back for this coin
* @param deadline by when will the exchange wire the funds?
* @param wire_subject which wire subject will the exchange use?
* @param reserve_pub public key of the reserve receiving the payback
* @param full_response full response from the exchange (for logging, in case of errors)
*/
typedef void
@ -1354,7 +1354,7 @@ typedef void
enum TALER_ErrorCode ec,
const struct TALER_Amount *amount,
struct GNUNET_TIME_Absolute deadline,
const char *wire_subject,
const struct TALER_ReservePublicKeyP *reserve_pub,
const json_t *full_response);

View File

@ -28,13 +28,14 @@
/**
* @brief Information we keep on bank transfer(s) that established a reserve.
* @brief Information we keep on bank transfer(s) that established or
* closed a reserve.
*/
struct TALER_EXCHANGEDB_BankTransfer
{
/**
* Public key of the reserve that was filled.
* Public key of the reserve that was filled or depleted.
*/
struct TALER_ReservePublicKeyP reserve_pub;
@ -51,7 +52,7 @@ struct TALER_EXCHANGEDB_BankTransfer
struct GNUNET_TIME_Absolute execution_date;
/**
* Detailed wire information about the sending account.
* Detailed wire information about the sending (or receiving) account.
*/
json_t *sender_account_details;
@ -145,21 +146,69 @@ struct TALER_EXCHANGEDB_CollectableBlindcoin
};
/**
* Information the exchange records about a /payback request.
*/
struct TALER_EXCHANGEDB_Payback
{
/**
* Which coin was paid back?
*/
struct TALER_CoinPublicInfo coin;
/**
* How much was the coin still worth at this time?
*/
struct TALER_Amount value;
/**
* Blinding factor supplied to prove to the exchange that
* the coin came from this reserve.
*/
struct TALER_DenominationBlindingKeyP coin_blind;
/**
* Signature of the coin of type
* #TALER_SIGNATURE_WALLET_COIN_PAYBACK.
*/
struct TALER_CoinSpendSignatureP coin_sig;
/**
* Public key of the reserve the coin was paid back into.
*/
struct TALER_ReservePublicKeyP reserve_pub;
};
/**
* @brief Types of operations on a reserved.
* @brief Types of operations on a reserve.
*/
enum TALER_EXCHANGEDB_ReserveOperation
{
/**
* Money was deposited into the reserve via a bank transfer.
* This is how customers establish a reserve at the exchange.
*/
TALER_EXCHANGEDB_RO_BANK_TO_EXCHANGE = 0,
/**
* A Coin was withdrawn from the reserve using /withdraw.
*/
TALER_EXCHANGEDB_RO_WITHDRAW_COIN = 1
TALER_EXCHANGEDB_RO_WITHDRAW_COIN = 1,
/**
* A coin was returned to the reserve using /payback.
*/
TALER_EXCHANGEDB_RO_PAYBACK_COIN = 2,
/**
* The exchange send inactive funds back from the reserve to the
* customer's bank account. This happens when the exchange
* closes a reserve with a non-zero amount left in it.
*/
TALER_EXCHANGEDB_RO_EXCHANGE_TO_BANK = 3
};
@ -189,7 +238,9 @@ struct TALER_EXCHANGEDB_ReserveHistory
{
/**
* Details about a bank transfer to the exchange.
* Details about a bank transfer to the exchange (reserve
* was established) or from the exchange (reserve was
* closed).
*/
struct TALER_EXCHANGEDB_BankTransfer *bank;
@ -198,6 +249,11 @@ struct TALER_EXCHANGEDB_ReserveHistory
*/
struct TALER_EXCHANGEDB_CollectableBlindcoin *withdraw;
/**
* Details about a /payback operation.
*/
struct TALER_EXCHANGEDB_Payback *payback;
} details;
};
@ -485,7 +541,12 @@ enum TALER_EXCHANGEDB_TransactionType
/**
* /refund operation.
*/
TALER_EXCHANGEDB_TT_REFUND = 2
TALER_EXCHANGEDB_TT_REFUND = 2,
/**
* /payback operation.
*/
TALER_EXCHANGEDB_TT_PAYBACK = 3
};
@ -527,6 +588,11 @@ struct TALER_EXCHANGEDB_TransactionList
*/
struct TALER_EXCHANGEDB_Refund *refund;
/**
* Details if transaction was a /payback operation.
*/
struct TALER_EXCHANGEDB_Payback *payback;
} details;
};
@ -1863,25 +1929,21 @@ struct TALER_EXCHANGEDB_Plugin
/**
* Function called to add a request for an emergency payback for a coin.
* Note that this function must check if there is an aggregation for the
* respective reserve, if not create one, and return the identifiers for
* the aggregate in @a wire_subject and @a deadline. The
* @a acceptable_delay will be constant for an exchange, so if an
* aggregate exists it must either be past the deadline or be usable,
* in which case this function should update the aggregate's total amount.
*
* If no aggregate exists, a fresh @a wire_subject is picked at random.
* Function called to add a request for an emergency payback for a
* coin. The funds are to be added back to the reserve. The
* function should return the @a deadline by which the exchange will
* trigger a wire transfer back to the customer's account for the
* reserve.
*
* @param cls closure
* @param session database connection
* @param reserve_pub public key of the reserve that is being refunded
* @param coin information about the coin
* @param coin_sig signature of the coin of type #TALER_SIGNATURE_WALLET_COIN_PAYBACK
* @param coin_blind blinding key of the coin
* @param h_blind_ev blinded envelope, as calculated by the exchange
* @param amount total amount to be paid back
* @param acceptable_delay how long could a wire transfer be delayed
* @param[out] wire_subject wire subject the database selected for the transfer
* @param receiver_account_details who should receive the funds
* @param[out] deadline set to absolute time by when the exchange plans to pay it back
* @return #GNUNET_OK on success,
* #GNUNET_SYSERR on DB errors
@ -1889,64 +1951,32 @@ struct TALER_EXCHANGEDB_Plugin
int
(*insert_payback_request)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
const struct TALER_ReservePublicKeyP *reserve_pub,
const struct TALER_CoinPublicInfo *coin,
const struct TALER_CoinSpendSignatureP *coin_sig,
const struct TALER_DenominationBlindingKeyP *coin_blind,
const struct GNUNET_HashCode *h_blinded_ev,
const struct TALER_Amount *amount,
struct GNUNET_TIME_Relative acceptable_delay,
struct TALER_WireTransferIdentifierRawP *wire_subject,
struct GNUNET_TIME_Absolute *deadline);
/**
* Return all (already aggregated!) payback payments due between @e
* start_time and @e end_time. To be used by the special
* 'emergency' aggregator to make the paybacks (which presumably
* only runs if there are paybacks to be made, and which is
* restricted to only accept paybacks for approved denomination
* keys).
*
* @param cls closure
* @param session database connection
* @param start_time beginning of selection range, inclusive
* @param end_time end of selection range, exclusive
* @param cb function to call on each required payback operation
* @param cb_cls closure for @a cb
* @return #GNUNET_OK on success,
* #GNUNET_NO if there are no entries,
* #GNUNET_SYSERR on DB errors
*/
int
(*select_payback_requests)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
struct GNUNET_TIME_Absolute start_time,
struct GNUNET_TIME_Absolute end_time,
TALER_EXCHANGEDB_PaybackCallback cb,
void *cb_cls);
/**
* Obtain the individual payback requests that justified the aggregate
* wire transfer. Usually used by the auditor to verify losses from
* paybacks.
* Obtain information about which reserve a coin was generated
* from given the hash of the blinded coin.
*
* @param cls closure
* @param session a session
* @param wire_subject wire subject of the payback wire transfer
* @param cb callback to call with the justification
* @param cb_cls closure for @a cb
* @param h_blind_ev hash of the blinded coin
* @param[out] reserve_pub set to information about the reserve (on success only)
* @return #GNUNET_OK on success,
* #GNUNET_NO if there are no entries,
* #GNUNET_SYSERR on DB errors
*/
int
(*get_payback_justification)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
const struct TALER_WireTransferIdentifierRawP *wire_subject,
// ? add constraints like h_wire of receiver?
TALER_EXCHANGEDB_PaybackJustificationCallback cb,
void *cb_cls);
(*get_reserve_by_h_blind)(void *cls,
struct TALER_EXCHANGEDB_Session *session,
const struct GNUNET_HashCode *h_blind_ev,
struct TALER_ReservePublicKeyP *reserve_pub);
};

View File

@ -1192,10 +1192,9 @@ struct TALER_PaybackConfirmationPS
struct TALER_CoinSpendPublicKeyP coin_pub;
/**
* Hash (!) of the wire transfer subject that the exchange
* will use. Hash excludes the 0-terminator.
* Public key of the reserve that will receive the payback.
*/
struct GNUNET_HashCode h_wire_subject;
struct TALER_ReservePublicKeyP reserve_pub;
};