From b00bea0b44fff4169c1c629bc6908a7ddddfd74d Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Mon, 25 May 2015 17:30:46 +0200 Subject: copy currency into fee_withdrawl --- src/mintdb/test_mintdb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/mintdb/test_mintdb.c b/src/mintdb/test_mintdb.c index 212e763bc..14a4ccc30 100644 --- a/src/mintdb/test_mintdb.c +++ b/src/mintdb/test_mintdb.c @@ -232,7 +232,7 @@ run (void *cls, (void) strcpy (dki.issue.value.currency, CURRENCY); dki.issue.fee_withdraw.value = 0; dki.issue.fee_withdraw.fraction = htonl (100); - (void) strcpy (dki.issue.value.currency, CURRENCY); + (void) strcpy (dki.issue.fee_withdraw.currency, CURRENCY); dki.issue.fee_refresh = dki.issue.fee_withdraw; FAILIF (GNUNET_OK != plugin->insert_denomination (plugin->cls, -- cgit v1.2.3 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 +++++++++ src/mintdb/plugin_mintdb_postgres.c | 130 ++++++++++++++++++++++++++++-------- src/mintdb/test_mintdb.c | 76 +++++++++++++++++++++ 3 files changed, 213 insertions(+), 26 deletions(-) (limited to 'src') 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. * diff --git a/src/mintdb/plugin_mintdb_postgres.c b/src/mintdb/plugin_mintdb_postgres.c index 3800f03b2..35861c9c8 100644 --- a/src/mintdb/plugin_mintdb_postgres.c +++ b/src/mintdb/plugin_mintdb_postgres.c @@ -254,10 +254,6 @@ postgres_create_tables (void *cls, " coin_pub BYTEA NOT NULL PRIMARY KEY" ",denom_pub BYTEA NOT NULL REFERENCES denominations (pub)" ",denom_sig BYTEA NOT NULL" - ",expended_val INT8 NOT NULL" - ",expended_frac INT4 NOT NULL" - ",expended_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL" - ",refresh_session_hash BYTEA" ")"); /** * The DB will show negative values for some values of the following fields as @@ -491,39 +487,20 @@ postgres_prepare (PGconn *db_conn) ") " "VALUES ($1, $2, $3, $4) ", 4, NULL); - PREPARE ("get_known_coin", "SELECT" - " coin_pub, denom_pub, denom_sig " - ",expended_val, expended_frac, expended_curr " - ",refresh_session_hash " + " denom_pub, denom_sig " "FROM known_coins " "WHERE coin_pub = $1", 1, NULL); - PREPARE ("update_known_coin", - "UPDATE known_coins " - "SET" - " denom_pub = $2 " - ",denom_sig = $3 " - ",expended_val = $4 " - ",expended_frac = $5 " - ",expended_curr = $6 " - ",refresh_session_hash = $7 " - "WHERE " - " coin_pub = $1", - 7, NULL); PREPARE ("insert_known_coin", "INSERT INTO known_coins (" " coin_pub" ",denom_pub" ",denom_sig" - ",expended_val" - ",expended_frac" - ",expended_curr" - ",refresh_session_hash" ")" - "VALUES ($1,$2,$3,$4,$5,$6,$7)", - 7, NULL); + "VALUES ($1,$2,$3)", + 3, NULL); PREPARE ("get_refresh_commit_link", "SELECT" " transfer_pub " @@ -1624,6 +1601,105 @@ postgres_create_refresh_session (void *cls, } +/** + * 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 + */ +static int +postgres_insert_known_coin (void *cls, + struct TALER_MINTDB_Session *session, + const struct TALER_CoinPublicInfo *coin_info) +{ + PGresult *result; + struct TALER_PQ_QueryParam params[] = { + TALER_PQ_query_param_auto_from_type (&coin_info->coin_pub), + TALER_PQ_query_param_rsa_public_key (coin_info->denom_pub.rsa_public_key), + TALER_PQ_query_param_rsa_signature (coin_info->denom_sig.rsa_signature), + TALER_PQ_query_param_end + }; + result = TALER_PQ_exec_prepared (session->conn, + "insert_known_coin", + params); + if (PGRES_COMMAND_OK != PQresultStatus (result)) + { + BREAK_DB_ERR (result); + PQclear (result); + return GNUNET_SYSERR; + } + PQclear (result); + return GNUNET_OK; +} + + +/** + * 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 + */ +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) +{ + 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; + result = TALER_PQ_exec_prepared (session->conn, + "get_known_coin", + params); + if (PGRES_TUPLES_OK != PQresultStatus (result)) + { + BREAK_DB_ERR (result); + PQclear (result); + return GNUNET_SYSERR; + } + if (0 == PQntuples (result)) + { + 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); + 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), + TALER_PQ_result_spec_end + }; + if (GNUNET_OK != TALER_PQ_extract_result (result, rs, 0)) + { + PQclear (result); + GNUNET_break (0); + GNUNET_free (coin_info); + return GNUNET_SYSERR; + } + PQclear (result); + (void) memcpy (&coin_info->coin_pub, + coin_pub, + sizeof (struct TALER_CoinSpendPublicKeyP)); + *ret_coin_info = coin_info; + return GNUNET_OK; +} + + /** * Store the given /refresh/melt request in the database. * @@ -2506,6 +2582,8 @@ libtaler_plugin_mintdb_postgres_init (void *cls) plugin->insert_deposit = &postgres_insert_deposit; plugin->get_refresh_session = &postgres_get_refresh_session; plugin->create_refresh_session = &postgres_create_refresh_session; + plugin->get_known_coin = &postgres_get_known_coin; + plugin->insert_known_coin = &postgres_insert_known_coin; plugin->insert_refresh_melt = &postgres_insert_refresh_melt; plugin->get_refresh_melt = &postgres_get_refresh_melt; plugin->insert_refresh_order = &postgres_insert_refresh_order; diff --git a/src/mintdb/test_mintdb.c b/src/mintdb/test_mintdb.c index 14a4ccc30..8a7e8cade 100644 --- a/src/mintdb/test_mintdb.c +++ b/src/mintdb/test_mintdb.c @@ -111,6 +111,81 @@ destroy_denom_key_pair (struct DenomKeyPair *dkp) GNUNET_free (dkp); } +static int +test_known_coins (struct TALER_MINTDB_Session *session) +{ + struct TALER_CoinPublicInfo coin_info; + struct TALER_CoinPublicInfo *ret_coin_info; + struct DenomKeyPair *dkp; + struct TALER_MINTDB_DenominationKeyIssueInformation dki; + int ret = GNUNET_SYSERR; + dkp = create_denom_key_pair (1024); + dki.denom_pub = dkp->pub; + dki.issue.start = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); + dki.issue.expire_withdraw = GNUNET_TIME_absolute_hton + (GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), + GNUNET_TIME_UNIT_HOURS)); + dki.issue.expire_spend = GNUNET_TIME_absolute_hton + (GNUNET_TIME_absolute_add + (GNUNET_TIME_absolute_get (), + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 2))); + dki.issue.expire_legal = GNUNET_TIME_absolute_hton + (GNUNET_TIME_absolute_add + (GNUNET_TIME_absolute_get (), + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 3))); + dki.issue.value.value = GNUNET_htonll (1); + dki.issue.value.fraction = htonl (100); + (void) strcpy (dki.issue.value.currency, CURRENCY); + dki.issue.fee_withdraw.value = 0; + dki.issue.fee_withdraw.fraction = htonl (100); + (void) strcpy (dki.issue.fee_withdraw.currency, CURRENCY); + dki.issue.fee_refresh = dki.issue.fee_withdraw; + FAILIF (GNUNET_OK != + plugin->insert_denomination (plugin->cls, + session, + &dki)); + RND_BLK (&coin_info); + coin_info.denom_pub = dkp->pub; + 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, + &coin_info)); + FAILIF (GNUNET_YES != + plugin->get_known_coin (plugin->cls, + 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, + coin_info.denom_pub.rsa_public_key)); + FAILIF (0 != GNUNET_CRYPTO_rsa_signature_cmp + (ret_coin_info->denom_sig.rsa_signature, + 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; +} + + /** * Main function that will be run by the scheduler. * @@ -374,6 +449,7 @@ run (void *cls, &refresh_session, sizeof (refresh_session))); } + FAILIF (GNUNET_OK != test_known_coins (session)); result = 0; drop: -- cgit v1.2.3 From 126a747f82e7894445775eead9ef2191e99f09c1 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Mon, 25 May 2015 22:47:00 +0200 Subject: mintdb postgres: add deposit fee to the denominations --- src/mintdb/plugin_mintdb_postgres.c | 13 ++++++++++--- src/mintdb/test_mintdb.c | 2 ++ 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/mintdb/plugin_mintdb_postgres.c b/src/mintdb/plugin_mintdb_postgres.c index 35861c9c8..35aab98bd 100644 --- a/src/mintdb/plugin_mintdb_postgres.c +++ b/src/mintdb/plugin_mintdb_postgres.c @@ -196,6 +196,9 @@ postgres_create_tables (void *cls, ",fee_withdraw_val INT8 NOT NULL" ",fee_withdraw_frac INT4 NOT NULL" ",fee_withdraw_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL" + ",fee_deposit_val INT8 NOT NULL" + ",fee_deposit_frac INT4 NOT NULL" + ",fee_deposit_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL" ",fee_refresh_val INT8 NOT NULL" ",fee_refresh_frac INT4 NOT NULL" ",fee_refresh_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL" @@ -393,13 +396,16 @@ postgres_prepare (PGconn *db_conn) ",coin_curr" /* assuming same currency for fees */ ",fee_withdraw_val" ",fee_withdraw_frac" - ",fee_withdraw_curr" /* must match coin_currency */ + ",fee_withdraw_curr" /* must match coin_curr */ + ",fee_deposit_val" + ",fee_deposit_frac" + ",fee_deposit_curr" /* must match coin_curr */ ",fee_refresh_val" ",fee_refresh_frac" - ",fee_refresh_curr" /* must match coin_currency */ + ",fee_refresh_curr" /* must match coin_curr */ ") VALUES " "($1, $2, $3, $4, $5, $6," - "$7, $8, $9, $10, $11, $12, $13, $14);", + "$7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17);", 14, NULL); PREPARE ("get_reserve", "SELECT " @@ -845,6 +851,7 @@ postgres_insert_denomination (void *cls, TALER_PQ_query_param_auto_from_type (&issue->expire_legal.abs_value_us__), TALER_PQ_query_param_amount_nbo (&issue->value), TALER_PQ_query_param_amount_nbo (&issue->fee_withdraw), + TALER_PQ_query_param_amount_nbo (&issue->fee_deposit), TALER_PQ_query_param_amount_nbo (&issue->fee_refresh), TALER_PQ_query_param_end }; diff --git a/src/mintdb/test_mintdb.c b/src/mintdb/test_mintdb.c index 8a7e8cade..b93965665 100644 --- a/src/mintdb/test_mintdb.c +++ b/src/mintdb/test_mintdb.c @@ -139,6 +139,7 @@ test_known_coins (struct TALER_MINTDB_Session *session) dki.issue.fee_withdraw.value = 0; dki.issue.fee_withdraw.fraction = htonl (100); (void) strcpy (dki.issue.fee_withdraw.currency, CURRENCY); + dki.issue.fee_deposit = dki.issue.fee_withdraw; dki.issue.fee_refresh = dki.issue.fee_withdraw; FAILIF (GNUNET_OK != plugin->insert_denomination (plugin->cls, @@ -308,6 +309,7 @@ run (void *cls, dki.issue.fee_withdraw.value = 0; dki.issue.fee_withdraw.fraction = htonl (100); (void) strcpy (dki.issue.fee_withdraw.currency, CURRENCY); + dki.issue.fee_deposit = dki.issue.fee_withdraw; dki.issue.fee_refresh = dki.issue.fee_withdraw; FAILIF (GNUNET_OK != plugin->insert_denomination (plugin->cls, -- cgit v1.2.3 From 6ad16aee31ae64d51e2230232702eaccd22dcd5c Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Mon, 25 May 2015 22:50:57 +0200 Subject: mintdb postgres: implement insert_refresh_melt() --- src/mintdb/plugin_mintdb_postgres.c | 88 ++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/mintdb/plugin_mintdb_postgres.c b/src/mintdb/plugin_mintdb_postgres.c index 35aab98bd..67097261b 100644 --- a/src/mintdb/plugin_mintdb_postgres.c +++ b/src/mintdb/plugin_mintdb_postgres.c @@ -277,6 +277,16 @@ postgres_create_tables (void *cls, // and the new coin signatures are ready ",reveal_ok BOOLEAN NOT NULL DEFAULT false" ") "); + SQLEXEC("CREATE TABLE IF NOT EXISTS refresh_melts " + "(" + " coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub)" + ",session BYTEA NOT NULL REFERENCES refresh_sessions (session_hash)" + ",oldcoin_index INT2 NOT NULL" + ",coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64)" + ",melt_val INT8 NOT NULL" + ",melt_frac INT8 NOT NULL" + ",melt_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL" + ") "); SQLEXEC("CREATE TABLE IF NOT EXISTS refresh_order " "( " " session_hash BYTEA NOT NULL CHECK (LENGTH(session_hash)=64) REFERENCES refresh_sessions (session_hash)" @@ -305,21 +315,6 @@ postgres_create_tables (void *cls, ",cnc_index INT2 NOT NULL" ",coin_ev BYTEA NOT NULL" ")"); - SQLEXEC("CREATE TABLE IF NOT EXISTS refresh_melt" - "(" - " session_hash BYTEA NOT NULL CHECK(LENGTH(session_hash)=64) REFERENCES refresh_sessions (session_hash) " - ",coin_pub BYTEA NOT NULL CHECK(LENGTH(coin_pub)=32) REFERENCES known_coins (coin_pub) " - ",coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64)" - ",denom_pub BYTEA NOT NULL " - ",denom_sig BYTEA NOT NULL " - ",amount_val INT8 NOT NULL " - ",amount_frac INT8 NOT NULL " - ",amount_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL " - ",fee_val INT8 NOT NULL " - ",fee_frac INT8 NOT NULL " - ",fee_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL " - ",oldcoin_index INT2 NOT NULL" - ")"); SQLEXEC("CREATE TABLE IF NOT EXISTS refresh_collectable" "(" " session_hash BYTEA NOT NULL CHECK(LENGTH(session_hash)=64) REFERENCES refresh_sessions (session_hash) " @@ -530,22 +525,17 @@ postgres_prepare (PGconn *db_conn) "VALUES ($1, $2, $3)", 3, NULL); PREPARE ("insert_refresh_melt", - "INSERT INTO refresh_melt (" - " session_hash " + "INSERT INTO refresh_melts (" + " coin_pub " + ",session" ",oldcoin_index " - ",coin_pub " ",coin_sig " - ",denom_pub " - ",denom_sig " - ",amount_val " - ",amount_frac " - ",amount_curr " - ",fee_val " - ",fee_frac " - ",fee_curr " + ",melt_val " + ",melt_frac " + ",melt_curr " ") " - "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)", - 12, NULL); + "VALUES ($1, $2, $3, $4, $5, $6, $7)", + 7, NULL); PREPARE ("get_refresh_order", "SELECT denom_pub " "FROM refresh_order " @@ -556,6 +546,7 @@ postgres_prepare (PGconn *db_conn) "FROM refresh_collectable " "WHERE session_hash = $1 AND newcoin_index = $2", 2, NULL); +#if 0 /* FIXME: not complete yet */ PREPARE ("get_refresh_melt", "SELECT coin_pub,coin_sig,denom_pub,denom_sig,amount_val,amount_frac,amount_curr,fee_val,fee_frac,fee_curr " "FROM refresh_melt " @@ -621,6 +612,7 @@ postgres_prepare (PGconn *db_conn) " WHERE newcoin_index = 0 AND rcc2.session_hash = rm.session_hash " " ) ", 1, NULL); +#endif PREPARE ("insert_deposit", "INSERT INTO deposits (" "coin_pub," @@ -1724,22 +1716,38 @@ postgres_insert_refresh_melt (void *cls, uint16_t oldcoin_index, const struct TALER_MINTDB_RefreshMelt *melt) { - // FIXME: check logic! - uint16_t oldcoin_index_nbo = htons (oldcoin_index); + uint16_t oldcoin_index_nbo; PGresult *result; + struct TALER_PQ_QueryParam params[] = { + TALER_PQ_query_param_auto_from_type(&melt->coin.coin_pub), + TALER_PQ_query_param_auto_from_type(&melt->session_hash), + TALER_PQ_query_param_auto_from_type(&oldcoin_index_nbo), + TALER_PQ_query_param_auto_from_type (&melt->coin_sig), + TALER_PQ_query_param_amount (&melt->amount_with_fee), + TALER_PQ_query_param_end + }; + int ret; + /* check if the coin is already known */ + ret = postgres_get_known_coin (cls, + session, + &melt->coin.coin_pub, + NULL); + if (GNUNET_SYSERR == ret) + return GNUNET_SYSERR; + if (GNUNET_NO == ret) /* if not, insert it */ { - struct TALER_PQ_QueryParam params[] = { - TALER_PQ_query_param_auto_from_type(&melt->session_hash), - TALER_PQ_query_param_auto_from_type(&oldcoin_index_nbo), - TALER_PQ_query_param_auto_from_type(&melt->coin.coin_pub), - TALER_PQ_query_param_rsa_public_key(melt->coin.denom_pub.rsa_public_key), - TALER_PQ_query_param_end - }; - result = TALER_PQ_exec_prepared (session->conn, - "insert_refresh_melt", - params); + ret = postgres_insert_known_coin (cls, + session, + &melt->coin); + if (ret == GNUNET_SYSERR) + return GNUNET_SYSERR; } + /* insert the melt */ + oldcoin_index_nbo = htons (oldcoin_index); + result = TALER_PQ_exec_prepared (session->conn, + "insert_refresh_melt", + params); if (PGRES_COMMAND_OK != PQresultStatus (result)) { BREAK_DB_ERR (result); -- cgit v1.2.3 From 02c237d2699b354b05763831afb40cac18e17468 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Tue, 26 May 2015 10:06:41 +0200 Subject: mintdb postgres: sanitize test case --- src/mintdb/test_mintdb.c | 126 ++++++++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/mintdb/test_mintdb.c b/src/mintdb/test_mintdb.c index b93965665..d5399d4c4 100644 --- a/src/mintdb/test_mintdb.c +++ b/src/mintdb/test_mintdb.c @@ -89,8 +89,60 @@ struct DenomKeyPair }; +/** + * Register a denomination in the DB. + * + * @param dkp the denomination key pair + * @param session the DB session + * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure + */ +static int +register_denomination(struct TALER_DenominationPublicKey denom_pub, + struct TALER_MINTDB_Session *session) +{ + struct TALER_MINTDB_DenominationKeyIssueInformation dki; + dki.denom_pub = denom_pub; + dki.issue.start = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); + dki.issue.expire_withdraw = GNUNET_TIME_absolute_hton + (GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), + GNUNET_TIME_UNIT_HOURS)); + dki.issue.expire_spend = GNUNET_TIME_absolute_hton + (GNUNET_TIME_absolute_add + (GNUNET_TIME_absolute_get (), + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 2))); + dki.issue.expire_legal = GNUNET_TIME_absolute_hton + (GNUNET_TIME_absolute_add + (GNUNET_TIME_absolute_get (), + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 3))); + dki.issue.value.value = GNUNET_htonll (1); + dki.issue.value.fraction = htonl (100); + (void) strcpy (dki.issue.value.currency, CURRENCY); + dki.issue.fee_withdraw.value = 0; + dki.issue.fee_withdraw.fraction = htonl (100); + (void) strcpy (dki.issue.fee_withdraw.currency, CURRENCY); + dki.issue.fee_deposit = dki.issue.fee_withdraw; + dki.issue.fee_refresh = dki.issue.fee_withdraw; + if (GNUNET_OK != + plugin->insert_denomination (plugin->cls, + session, + &dki)) + { + GNUNET_break(0); + return GNUNET_SYSERR; + } + return GNUNET_OK; +} + + +/** + * Create a denominaiton key pair + * + * @param size the size of the denomination key + * @param session the DB session + * @return the denominaiton key pair; NULL upon error + */ static struct DenomKeyPair * -create_denom_key_pair (unsigned int size) +create_denom_key_pair (unsigned int size, struct TALER_MINTDB_Session *session) { struct DenomKeyPair *dkp; @@ -99,10 +151,16 @@ create_denom_key_pair (unsigned int size) GNUNET_assert (NULL != dkp->priv.rsa_private_key); dkp->pub.rsa_public_key = GNUNET_CRYPTO_rsa_private_key_get_public (dkp->priv.rsa_private_key); + (void) register_denomination (dkp->pub, session); return dkp; } +/** + * Destroy a denomination key pair. The key is not necessarily removed from the DB. + * + * @param dkp the keypair to destroy + */ static void destroy_denom_key_pair (struct DenomKeyPair *dkp) { @@ -111,40 +169,22 @@ destroy_denom_key_pair (struct DenomKeyPair *dkp) GNUNET_free (dkp); } + +/** + * Tests on the known_coins relation + * + * @param session the DB session + * @return #GNUNET_OK if the tests are successful; #GNUNET_SYSERR if not. + */ static int test_known_coins (struct TALER_MINTDB_Session *session) { struct TALER_CoinPublicInfo coin_info; struct TALER_CoinPublicInfo *ret_coin_info; struct DenomKeyPair *dkp; - struct TALER_MINTDB_DenominationKeyIssueInformation dki; int ret = GNUNET_SYSERR; - dkp = create_denom_key_pair (1024); - dki.denom_pub = dkp->pub; - dki.issue.start = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); - dki.issue.expire_withdraw = GNUNET_TIME_absolute_hton - (GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), - GNUNET_TIME_UNIT_HOURS)); - dki.issue.expire_spend = GNUNET_TIME_absolute_hton - (GNUNET_TIME_absolute_add - (GNUNET_TIME_absolute_get (), - GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 2))); - dki.issue.expire_legal = GNUNET_TIME_absolute_hton - (GNUNET_TIME_absolute_add - (GNUNET_TIME_absolute_get (), - GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 3))); - dki.issue.value.value = GNUNET_htonll (1); - dki.issue.value.fraction = htonl (100); - (void) strcpy (dki.issue.value.currency, CURRENCY); - dki.issue.fee_withdraw.value = 0; - dki.issue.fee_withdraw.fraction = htonl (100); - (void) strcpy (dki.issue.fee_withdraw.currency, CURRENCY); - dki.issue.fee_deposit = dki.issue.fee_withdraw; - dki.issue.fee_refresh = dki.issue.fee_withdraw; - FAILIF (GNUNET_OK != - plugin->insert_denomination (plugin->cls, - session, - &dki)); + + dkp = create_denom_key_pair (1024, session); RND_BLK (&coin_info); coin_info.denom_pub = dkp->pub; coin_info.denom_sig.rsa_signature = @@ -287,35 +327,7 @@ run (void *cls, ++amount.fraction, amount.currency, expiry.abs_value_us)); - dkp = create_denom_key_pair (1024); - { - struct TALER_MINTDB_DenominationKeyIssueInformation dki; - dki.denom_pub = dkp->pub; - dki.issue.start = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); - dki.issue.expire_withdraw = GNUNET_TIME_absolute_hton - (GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), - GNUNET_TIME_UNIT_HOURS)); - dki.issue.expire_spend = GNUNET_TIME_absolute_hton - (GNUNET_TIME_absolute_add - (GNUNET_TIME_absolute_get (), - GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 2))); - dki.issue.expire_legal = GNUNET_TIME_absolute_hton - (GNUNET_TIME_absolute_add - (GNUNET_TIME_absolute_get (), - GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 3))); - dki.issue.value.value = GNUNET_htonll (1); - dki.issue.value.fraction = htonl (100); - (void) strcpy (dki.issue.value.currency, CURRENCY); - dki.issue.fee_withdraw.value = 0; - dki.issue.fee_withdraw.fraction = htonl (100); - (void) strcpy (dki.issue.fee_withdraw.currency, CURRENCY); - dki.issue.fee_deposit = dki.issue.fee_withdraw; - dki.issue.fee_refresh = dki.issue.fee_withdraw; - FAILIF (GNUNET_OK != - plugin->insert_denomination (plugin->cls, - session, - &dki)); - } + dkp = create_denom_key_pair (1024, session); RND_BLK(&h_blind); RND_BLK(&cbc.reserve_sig); cbc.denom_pub = dkp->pub; -- 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') 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 From 6cc0b4bad8471f29674f1a9ee6ed8e8b3fd0dec7 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Wed, 27 May 2015 14:22:25 +0200 Subject: mintdb postgres: Implement get_refresh_melt(). --- src/mintdb/plugin_mintdb_postgres.c | 90 ++++++++++++++++++++++++------------- src/mintdb/test_mintdb.c | 1 + 2 files changed, 60 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/mintdb/plugin_mintdb_postgres.c b/src/mintdb/plugin_mintdb_postgres.c index 1157ca09a..5f4fa02d0 100644 --- a/src/mintdb/plugin_mintdb_postgres.c +++ b/src/mintdb/plugin_mintdb_postgres.c @@ -283,9 +283,11 @@ postgres_create_tables (void *cls, ",session BYTEA NOT NULL REFERENCES refresh_sessions (session_hash)" ",oldcoin_index INT2 NOT NULL" ",coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64)" - ",melt_val INT8 NOT NULL" - ",melt_frac INT8 NOT NULL" - ",melt_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL" + ",amount_val INT8 NOT NULL" + ",amount_frac INT8 NOT NULL" + ",amount_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL" + ", PRIMARY KEY (session, oldcoin_index)" /* a coin can be used only + once in a refresh session */ ") "); SQLEXEC("CREATE TABLE IF NOT EXISTS refresh_order " "( " @@ -530,12 +532,22 @@ postgres_prepare (PGconn *db_conn) ",session" ",oldcoin_index " ",coin_sig " - ",melt_val " - ",melt_frac " - ",melt_curr " + ",amount_val " + ",amount_frac " + ",amount_curr " ") " "VALUES ($1, $2, $3, $4, $5, $6, $7)", 7, NULL); + PREPARE ("get_refresh_melt", + "SELECT" + " coin_pub" + ",coin_sig" + ",amount_val" + ",amount_frac" + ",amount_curr" + " FROM refresh_melts " + "WHERE session = $1 AND oldcoin_index = $2", + 2, NULL); PREPARE ("get_refresh_order", "SELECT denom_pub " "FROM refresh_order " @@ -547,11 +559,6 @@ postgres_prepare (PGconn *db_conn) "WHERE session_hash = $1 AND newcoin_index = $2", 2, NULL); #if 0 /* FIXME: not complete yet */ - PREPARE ("get_refresh_melt", - "SELECT coin_pub,coin_sig,denom_pub,denom_sig,amount_val,amount_frac,amount_curr,fee_val,fee_frac,fee_curr " - "FROM refresh_melt " - "WHERE session_hash = $1 AND oldcoin_index = $2", - 2, NULL); PREPARE ("insert_refresh_commit_link", "INSERT INTO refresh_commit_link (" " session_hash " @@ -1775,43 +1782,64 @@ postgres_get_refresh_melt (void *cls, uint16_t oldcoin_index, struct TALER_MINTDB_RefreshMelt *melt) { -#if 0 - // FIXME: check logic! + PGresult *result; + struct TALER_CoinPublicInfo coin; + struct TALER_CoinSpendSignatureP coin_sig; + struct TALER_Amount amount; uint16_t oldcoin_index_nbo = htons (oldcoin_index); - struct TALER_PQ_Query params[] = { + struct TALER_PQ_QueryParam params[] = { TALER_PQ_query_param_auto_from_type (session_hash), TALER_PQ_query_param_auto_from_type (&oldcoin_index_nbo), TALER_PQ_query_param_end }; - struct TALER_PQ_ResultSpec rs[] = { - TALER_PQ_result_spec_auto_from_type ("coin_pub", &melt->coin), - TALER_PQ_result_spec_auto_from_type ("coin_sig", &melt->coin_sig), - TALER_PQ_result_spec_auto_from_type ("denom_pub", &melt->coin), - TALER_PQ_result_spec_auto_from_type ("denom_sig", &melt->coin), - TALER_PQ_result_spec_amount ("amount", melt->amount_with_fee), - TALER_PQ_result_spec_amount ("fee", melt->melt_fee), - TALER_PQ_result_spec_end - }; + int nrows; + /* check if the melt record exists and get it */ result = TALER_PQ_exec_prepared (session->conn, "get_refresh_melt", params); - if (0 == PQntuples (result)) + if (PGRES_TUPLES_OK != PQresultStatus (result)) { + BREAK_DB_ERR (result); PQclear (result); return GNUNET_SYSERR; } - if (1 != PQntuples (result)) + nrows = PQntuples (result); + if (0 == nrows) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "get_refresh_melt() returned 0 matching rows\n"); + PQclear (result); + return GNUNET_NO; + } + GNUNET_assert (1 == nrows); /* due to primary key constraint */ + struct TALER_PQ_ResultSpec rs[] = { + TALER_PQ_result_spec_auto_from_type ("coin_pub", &coin.coin_pub), + TALER_PQ_result_spec_auto_from_type ("coin_sig", &coin_sig), + TALER_PQ_result_spec_amount ("amount", &amount), + TALER_PQ_result_spec_end + }; + if (GNUNET_OK != TALER_PQ_extract_result (result, rs, 0)) { PQclear (result); - GNUNET_break (0); return GNUNET_SYSERR; } -#endif - melt->session_hash = *session_hash; - - GNUNET_break (0); - return GNUNET_SYSERR; + PQclear (result); + /* fetch the coin info and denomination info */ + if (GNUNET_OK != postgres_get_known_coin (cls, + session, + &coin.coin_pub, + &coin)) + return GNUNET_SYSERR; + if (NULL == melt) + return GNUNET_OK; + melt->coin = coin; + melt->coin_sig = coin_sig; + if (session_hash != &melt->session_hash) + melt->session_hash = *session_hash; + melt->amount_with_fee = amount; + /* FIXME: melt->melt_fee = ?? */ + return GNUNET_OK; } diff --git a/src/mintdb/test_mintdb.c b/src/mintdb/test_mintdb.c index 9e93401c7..55a8395e3 100644 --- a/src/mintdb/test_mintdb.c +++ b/src/mintdb/test_mintdb.c @@ -459,6 +459,7 @@ run (void *cls, FAILIF (GNUNET_OK != test_known_coins (session)); result = 0; + /* FIXME: test_refresh_melts */ drop: if (NULL != wire) json_decref (wire); -- cgit v1.2.3