From d49a0d6567e35450fe227ab4ba335af6285b63eb Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Mon, 25 May 2015 17:40:27 +0200 Subject: mintdb postgres: add get_known_coin() and insert_known_coin() --- src/include/taler_mintdb_plugin.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/include') diff --git a/src/include/taler_mintdb_plugin.h b/src/include/taler_mintdb_plugin.h index 6cc8fd76c..0cbcb3c4e 100644 --- a/src/include/taler_mintdb_plugin.h +++ b/src/include/taler_mintdb_plugin.h @@ -842,6 +842,39 @@ struct TALER_MINTDB_Plugin const struct TALER_MINTDB_RefreshSession *refresh_session); + /** + * Retrieve the record for a known coin. + * + * @param cls the plugin closure + * @param session the database session handle + * @param coin_pub the public key of the coin to search for + * @param ret_coin_info place holder for the returned coin information object + * @return #GNUNET_SYSERR upon error; #GNUNET_NO if no coin is found; #GNUNET_OK + * if upon succesfullying retrieving the record data info @a + * ret_coin_info + */ + int + (*get_known_coin) (void *cls, + struct TALER_MINTDB_Session *session, + const struct TALER_CoinSpendPublicKeyP *coin_pub, + struct TALER_CoinPublicInfo **ret_coin_info); + + + /** + * Insert a coin we know of into the DB. The coin can then be referenced by + * tables for deposits, lock and refresh functionality. + * + * @param cls plugin closure + * @param session the shared database session + * @param coin_info the public coin info + * @return #GNUNET_SYSERR upon error; #GNUNET_OK upon success + */ + int + (*insert_known_coin) (void *cls, + struct TALER_MINTDB_Session *session, + const struct TALER_CoinPublicInfo *coin_info); + + /** * Store the given /refresh/melt request in the database. * -- cgit v1.2.3 From 1d551bf36b5e7d47dead516d42ece48420809fb4 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Wed, 27 May 2015 14:20:07 +0200 Subject: mintdb get_known_coin(): Do not allocate memory for return paramter. Instead populate the fields of the placeholder return variable. --- src/include/taler_mintdb_plugin.h | 6 +++--- src/mintdb/plugin_mintdb_postgres.c | 28 +++++++++++++--------------- src/mintdb/test_mintdb.c | 17 +++++------------ 3 files changed, 21 insertions(+), 30 deletions(-) (limited to 'src/include') diff --git a/src/include/taler_mintdb_plugin.h b/src/include/taler_mintdb_plugin.h index 0cbcb3c4e..63dacf73b 100644 --- a/src/include/taler_mintdb_plugin.h +++ b/src/include/taler_mintdb_plugin.h @@ -332,7 +332,7 @@ struct TALER_MINTDB_RefreshMelt */ struct TALER_Amount amount_with_fee; - /** + /** FIXME: This can be retrieved from the Denomination? Do we need this? * Melting fee charged by the mint. This must match the Mint's * denomination key's melting fee. If the client puts in an invalid * melting fee (too high or too low) that does not match the Mint's @@ -848,7 +848,7 @@ struct TALER_MINTDB_Plugin * @param cls the plugin closure * @param session the database session handle * @param coin_pub the public key of the coin to search for - * @param ret_coin_info place holder for the returned coin information object + * @param coin_info place holder for the returned coin information object * @return #GNUNET_SYSERR upon error; #GNUNET_NO if no coin is found; #GNUNET_OK * if upon succesfullying retrieving the record data info @a * ret_coin_info @@ -857,7 +857,7 @@ struct TALER_MINTDB_Plugin (*get_known_coin) (void *cls, struct TALER_MINTDB_Session *session, const struct TALER_CoinSpendPublicKeyP *coin_pub, - struct TALER_CoinPublicInfo **ret_coin_info); + struct TALER_CoinPublicInfo *coin_info); /** diff --git a/src/mintdb/plugin_mintdb_postgres.c b/src/mintdb/plugin_mintdb_postgres.c index 67097261b..1157ca09a 100644 --- a/src/mintdb/plugin_mintdb_postgres.c +++ b/src/mintdb/plugin_mintdb_postgres.c @@ -1641,23 +1641,24 @@ postgres_insert_known_coin (void *cls, * @param cls the plugin closure * @param session the database session handle * @param coin_pub the public key of the coin to search for - * @param ret_coin_info place holder for the returned coin information object + * @param coin_info place holder for the returned coin information object * @return #GNUNET_SYSERR upon error; #GNUNET_NO if no coin is found; #GNUNET_OK * if upon succesfullying retrieving the record data info @a - * ret_coin_info + * coin_info */ static int postgres_get_known_coin (void *cls, struct TALER_MINTDB_Session *session, const struct TALER_CoinSpendPublicKeyP *coin_pub, - struct TALER_CoinPublicInfo **ret_coin_info) + struct TALER_CoinPublicInfo *coin_info) { PGresult *result; struct TALER_PQ_QueryParam params[] = { TALER_PQ_query_param_auto_from_type (coin_pub), TALER_PQ_query_param_end }; - struct TALER_CoinPublicInfo *coin_info; + int nrows; + result = TALER_PQ_exec_prepared (session->conn, "get_known_coin", params); @@ -1667,17 +1668,13 @@ postgres_get_known_coin (void *cls, PQclear (result); return GNUNET_SYSERR; } - if (0 == PQntuples (result)) + nrows = PQntuples (result); + if (0 == nrows) { PQclear (result); return GNUNET_NO; } - if ((NULL == ret_coin_info) && (1 == PQntuples (result))) - { - PQclear (result); - return GNUNET_OK; - } - coin_info = GNUNET_new (struct TALER_CoinPublicInfo); + GNUNET_assert (1 == nrows); /* due to primary key */ struct TALER_PQ_ResultSpec rs[] = { TALER_PQ_result_spec_rsa_public_key ("denom_pub", &coin_info->denom_pub.rsa_public_key), TALER_PQ_result_spec_rsa_signature ("denom_sig", &coin_info->denom_sig.rsa_signature), @@ -1691,10 +1688,11 @@ postgres_get_known_coin (void *cls, return GNUNET_SYSERR; } PQclear (result); - (void) memcpy (&coin_info->coin_pub, - coin_pub, - sizeof (struct TALER_CoinSpendPublicKeyP)); - *ret_coin_info = coin_info; + /* no need to copy if the src and dest are same */ + if (coin_pub != &coin_info->coin_pub) + (void) memcpy (&coin_info->coin_pub, + coin_pub, + sizeof (struct TALER_CoinSpendPublicKeyP)); return GNUNET_OK; } diff --git a/src/mintdb/test_mintdb.c b/src/mintdb/test_mintdb.c index d5399d4c4..9e93401c7 100644 --- a/src/mintdb/test_mintdb.c +++ b/src/mintdb/test_mintdb.c @@ -180,7 +180,7 @@ static int test_known_coins (struct TALER_MINTDB_Session *session) { struct TALER_CoinPublicInfo coin_info; - struct TALER_CoinPublicInfo *ret_coin_info; + struct TALER_CoinPublicInfo ret_coin_info; struct DenomKeyPair *dkp; int ret = GNUNET_SYSERR; @@ -190,13 +190,11 @@ test_known_coins (struct TALER_MINTDB_Session *session) coin_info.denom_sig.rsa_signature = GNUNET_CRYPTO_rsa_sign (dkp->priv.rsa_private_key, "foobar", 6); - ret_coin_info = NULL; FAILIF (GNUNET_NO != plugin->get_known_coin (plugin->cls, session, &coin_info.coin_pub, &ret_coin_info)); - FAILIF (NULL != ret_coin_info); FAILIF (GNUNET_OK != plugin->insert_known_coin (plugin->cls, session, @@ -206,23 +204,18 @@ test_known_coins (struct TALER_MINTDB_Session *session) session, &coin_info.coin_pub, &ret_coin_info)); - FAILIF (NULL == ret_coin_info); FAILIF (0 != GNUNET_CRYPTO_rsa_public_key_cmp - (ret_coin_info->denom_pub.rsa_public_key, + (ret_coin_info.denom_pub.rsa_public_key, coin_info.denom_pub.rsa_public_key)); FAILIF (0 != GNUNET_CRYPTO_rsa_signature_cmp - (ret_coin_info->denom_sig.rsa_signature, + (ret_coin_info.denom_sig.rsa_signature, coin_info.denom_sig.rsa_signature)); + GNUNET_CRYPTO_rsa_public_key_free (ret_coin_info.denom_pub.rsa_public_key); + GNUNET_CRYPTO_rsa_signature_free (ret_coin_info.denom_sig.rsa_signature); ret = GNUNET_OK; drop: destroy_denom_key_pair (dkp); GNUNET_CRYPTO_rsa_signature_free (coin_info.denom_sig.rsa_signature); - if (NULL != ret_coin_info) - { - GNUNET_CRYPTO_rsa_public_key_free (ret_coin_info->denom_pub.rsa_public_key); - GNUNET_CRYPTO_rsa_signature_free (ret_coin_info->denom_sig.rsa_signature); - GNUNET_free (ret_coin_info); - } return ret; } -- cgit v1.2.3