mintdb postgres: Implement get_refresh_melt().

This commit is contained in:
Sree Harsha Totakura 2015-05-27 14:22:25 +02:00
parent 1d551bf36b
commit 6cc0b4bad8
2 changed files with 63 additions and 34 deletions

View File

@ -283,9 +283,11 @@ postgres_create_tables (void *cls,
",session BYTEA NOT NULL REFERENCES refresh_sessions (session_hash)" ",session BYTEA NOT NULL REFERENCES refresh_sessions (session_hash)"
",oldcoin_index INT2 NOT NULL" ",oldcoin_index INT2 NOT NULL"
",coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64)" ",coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64)"
",melt_val INT8 NOT NULL" ",amount_val INT8 NOT NULL"
",melt_frac INT8 NOT NULL" ",amount_frac INT8 NOT NULL"
",melt_curr VARCHAR("TALER_CURRENCY_LEN_STR") 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 " SQLEXEC("CREATE TABLE IF NOT EXISTS refresh_order "
"( " "( "
@ -530,12 +532,22 @@ postgres_prepare (PGconn *db_conn)
",session" ",session"
",oldcoin_index " ",oldcoin_index "
",coin_sig " ",coin_sig "
",melt_val " ",amount_val "
",melt_frac " ",amount_frac "
",melt_curr " ",amount_curr "
") " ") "
"VALUES ($1, $2, $3, $4, $5, $6, $7)", "VALUES ($1, $2, $3, $4, $5, $6, $7)",
7, NULL); 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", PREPARE ("get_refresh_order",
"SELECT denom_pub " "SELECT denom_pub "
"FROM refresh_order " "FROM refresh_order "
@ -547,11 +559,6 @@ postgres_prepare (PGconn *db_conn)
"WHERE session_hash = $1 AND newcoin_index = $2", "WHERE session_hash = $1 AND newcoin_index = $2",
2, NULL); 2, NULL);
#if 0 /* FIXME: not complete yet */ #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", PREPARE ("insert_refresh_commit_link",
"INSERT INTO refresh_commit_link (" "INSERT INTO refresh_commit_link ("
" session_hash " " session_hash "
@ -1775,43 +1782,64 @@ postgres_get_refresh_melt (void *cls,
uint16_t oldcoin_index, uint16_t oldcoin_index,
struct TALER_MINTDB_RefreshMelt *melt) struct TALER_MINTDB_RefreshMelt *melt)
{ {
#if 0 PGresult *result;
// FIXME: check logic! struct TALER_CoinPublicInfo coin;
struct TALER_CoinSpendSignatureP coin_sig;
struct TALER_Amount amount;
uint16_t oldcoin_index_nbo = htons (oldcoin_index); 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 (session_hash),
TALER_PQ_query_param_auto_from_type (&oldcoin_index_nbo), TALER_PQ_query_param_auto_from_type (&oldcoin_index_nbo),
TALER_PQ_query_param_end TALER_PQ_query_param_end
}; };
struct TALER_PQ_ResultSpec rs[] = { int nrows;
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
};
/* check if the melt record exists and get it */
result = TALER_PQ_exec_prepared (session->conn, result = TALER_PQ_exec_prepared (session->conn,
"get_refresh_melt", "get_refresh_melt",
params); params);
if (0 == PQntuples (result)) if (PGRES_TUPLES_OK != PQresultStatus (result))
{
BREAK_DB_ERR (result);
PQclear (result);
return GNUNET_SYSERR;
}
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); PQclear (result);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
if (1 != PQntuples (result)) PQclear (result);
{ /* fetch the coin info and denomination info */
PQclear (result); if (GNUNET_OK != postgres_get_known_coin (cls,
GNUNET_break (0); session,
&coin.coin_pub,
&coin))
return GNUNET_SYSERR; return GNUNET_SYSERR;
} if (NULL == melt)
#endif return GNUNET_OK;
melt->session_hash = *session_hash; melt->coin = coin;
melt->coin_sig = coin_sig;
GNUNET_break (0); if (session_hash != &melt->session_hash)
return GNUNET_SYSERR; melt->session_hash = *session_hash;
melt->amount_with_fee = amount;
/* FIXME: melt->melt_fee = ?? */
return GNUNET_OK;
} }

View File

@ -459,6 +459,7 @@ run (void *cls,
FAILIF (GNUNET_OK != test_known_coins (session)); FAILIF (GNUNET_OK != test_known_coins (session));
result = 0; result = 0;
/* FIXME: test_refresh_melts */
drop: drop:
if (NULL != wire) if (NULL != wire)
json_decref (wire); json_decref (wire);