db: Implement insert_collectable_blindcoin

This commit is contained in:
Sree Harsha Totakura 2015-03-07 13:56:26 +01:00
parent 21eae0ff7e
commit f1d86b7ec2
2 changed files with 81 additions and 47 deletions

View File

@ -179,9 +179,9 @@ TALER_MINT_DB_create_tables (int temporary)
SQLEXEC ("CREATE TABLE IF NOT EXISTS collectable_blindcoins" SQLEXEC ("CREATE TABLE IF NOT EXISTS collectable_blindcoins"
"(" "("
"blind_ev BYTEA PRIMARY KEY" "blind_ev BYTEA PRIMARY KEY"
",denom_pub BYTEA NOT NULL" ",denom_pub BYTEA NOT NULL" /* FIXME: Make this a foreign key? */
",reserve_sig BYTEA NOT NULL"
",reserve_pub BYTEA REFERENCES reserves (reserve_pub) ON DELETE CASCADE" ",reserve_pub BYTEA REFERENCES reserves (reserve_pub) ON DELETE CASCADE"
",reserve_sig BYTEA NOT NULL"
");"); ");");
SQLEXEC ("CREATE INDEX collectable_blindcoins_reserve_pub_index ON" SQLEXEC ("CREATE INDEX collectable_blindcoins_reserve_pub_index ON"
" collectable_blindcoins (reserve_pub)"); " collectable_blindcoins (reserve_pub)");
@ -330,10 +330,10 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
4, NULL); 4, NULL);
PREPARE ("insert_collectable_blindcoins", PREPARE ("insert_collectable_blindcoins",
"INSERT INTO collectable_blindcoins ( " "INSERT INTO collectable_blindcoins ( "
" blind_ev, blind_ev_sig " " blind_ev"
",denom_pub, reserve_pub, reserve_sig) " ",denom_pub, reserve_pub, reserve_sig) "
"VALUES ($1, $2, $3, $4, $5)", "VALUES ($1, $2, $3, $4)",
6, NULL); 4, NULL);
PREPARE ("get_collectable_blindcoins", PREPARE ("get_collectable_blindcoins",
"SELECT " "SELECT "
"blind_ev_sig, denom_pub, reserve_sig, reserve_pub " "blind_ev_sig, denom_pub, reserve_sig, reserve_pub "
@ -987,46 +987,35 @@ TALER_MINT_DB_insert_collectable_blindcoin (PGconn *db_conn,
const struct GNUNET_HashCode *h_blind, const struct GNUNET_HashCode *h_blind,
const struct CollectableBlindcoin *collectable) const struct CollectableBlindcoin *collectable)
{ {
// FIXME: check logic!
PGresult *result; PGresult *result;
char *sig_buf; char *denom_pub_enc = NULL;
size_t sig_buf_size; size_t denom_pub_enc_size;
denom_pub_enc_size =
sig_buf_size = GNUNET_CRYPTO_rsa_signature_encode (collectable->sig, GNUNET_CRYPTO_rsa_public_key_encode (collectable->denom_pub,
&sig_buf); &denom_pub_enc);
{
struct TALER_DB_QueryParam params[] = { struct TALER_DB_QueryParam params[] = {
TALER_DB_QUERY_PARAM_PTR (&h_blind), TALER_DB_QUERY_PARAM_PTR (h_blind),
TALER_DB_QUERY_PARAM_PTR_SIZED (sig_buf, sig_buf_size), TALER_DB_QUERY_PARAM_PTR_SIZED (denom_pub_enc, denom_pub_enc_size - 1), /* DB doesn't like the trailing \0 */
TALER_DB_QUERY_PARAM_PTR (&collectable->denom_pub),
TALER_DB_QUERY_PARAM_PTR (&collectable->reserve_pub), TALER_DB_QUERY_PARAM_PTR (&collectable->reserve_pub),
TALER_DB_QUERY_PARAM_PTR (&collectable->reserve_sig), TALER_DB_QUERY_PARAM_PTR (&collectable->reserve_sig),
TALER_DB_QUERY_PARAM_END TALER_DB_QUERY_PARAM_END
}; };
int ret;
result = TALER_DB_exec_prepared (db_conn, result = TALER_DB_exec_prepared (db_conn,
"insert_collectable_blindcoins", "insert_collectable_blindcoins",
params); params);
if (PGRES_COMMAND_OK != PQresultStatus (result)) if (PGRES_COMMAND_OK != PQresultStatus (result))
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, ret = GNUNET_SYSERR;
"Query failed: %s\n", goto cleanup;
PQresultErrorMessage (result));
PQclear (result);
return GNUNET_SYSERR;
} }
ret = GNUNET_OK;
if (0 != strcmp ("1", PQcmdTuples (result))) cleanup:
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Insert failed (updated '%s' tupes instead of '1')\n",
PQcmdTuples (result));
PQclear (result); PQclear (result);
return GNUNET_SYSERR; GNUNET_free_non_null (denom_pub_enc);
} return ret;
PQclear (result);
}
return GNUNET_OK;
} }

View File

@ -33,6 +33,10 @@ static int result;
} while (0) } while (0)
#define RND_BLK(ptr) \
GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, ptr, sizeof (*ptr))
/** /**
* Checks if the given reserve has the given amount of balance and expiry * Checks if the given reserve has the given amount of balance and expiry
* *
@ -66,6 +70,31 @@ check_reserve (PGconn *db,
} }
struct DenomKeyPair
{
struct GNUNET_CRYPTO_rsa_PrivateKey *priv;
struct GNUNET_CRYPTO_rsa_PublicKey *pub;
};
struct DenomKeyPair *
create_denom_key_pair (unsigned int size)
{
struct DenomKeyPair *dkp;
dkp = GNUNET_new (struct DenomKeyPair);
dkp->priv = GNUNET_CRYPTO_rsa_private_key_create (size);
GNUNET_assert (NULL != dkp->priv);
dkp->pub = GNUNET_CRYPTO_rsa_private_key_get_public (dkp->priv);
return dkp;
}
destroy_denon_key_pair (struct DenomKeyPair *dkp)
{
GNUNET_CRYPTO_rsa_public_key_free (dkp->pub);
GNUNET_CRYPTO_rsa_private_key_free (dkp->priv);
GNUNET_free (dkp);
}
/** /**
* Main function that will be run by the scheduler. * Main function that will be run by the scheduler.
* *
@ -79,12 +108,16 @@ run (void *cls, char *const *args, const char *cfgfile,
const struct GNUNET_CONFIGURATION_Handle *config) const struct GNUNET_CONFIGURATION_Handle *config)
{ {
PGconn *db; PGconn *db;
struct GNUNET_CRYPTO_EddsaPublicKey pub; struct GNUNET_CRYPTO_EddsaPublicKey reserve_pub;
struct Reserve reserve; struct Reserve reserve;
struct GNUNET_TIME_Absolute expiry; struct GNUNET_TIME_Absolute expiry;
struct TALER_Amount amount; struct TALER_Amount amount;
struct DenomKeyPair *dkp;
struct GNUNET_HashCode *h_blind;
struct CollectableBlindcoin cbc;
db = NULL; db = NULL;
dkp = NULL;
if (GNUNET_OK != TALER_MINT_DB_init ("postgres:///taler")) if (GNUNET_OK != TALER_MINT_DB_init ("postgres:///taler"))
{ {
result = 1; result = 1;
@ -100,9 +133,8 @@ run (void *cls, char *const *args, const char *cfgfile,
result = 3; result = 3;
goto drop; goto drop;
} }
GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, RND_BLK (&reserve_pub);
&pub, sizeof (pub)); reserve.pub = &reserve_pub;
reserve.pub = &pub;
amount.value = 1; amount.value = 1;
amount.fraction = 1; amount.fraction = 1;
strcpy (amount.currency, "EUR"); strcpy (amount.currency, "EUR");
@ -114,7 +146,7 @@ run (void *cls, char *const *args, const char *cfgfile,
amount, amount,
expiry)); expiry));
FAILIF (GNUNET_OK != check_reserve (db, FAILIF (GNUNET_OK != check_reserve (db,
&pub, &reserve_pub,
amount.value, amount.value,
amount.fraction, amount.fraction,
amount.currency, amount.currency,
@ -124,15 +156,28 @@ run (void *cls, char *const *args, const char *cfgfile,
amount, amount,
expiry)); expiry));
FAILIF (GNUNET_OK != check_reserve (db, FAILIF (GNUNET_OK != check_reserve (db,
&pub, &reserve_pub,
++amount.value, ++amount.value,
++amount.fraction, ++amount.fraction,
amount.currency, amount.currency,
expiry.abs_value_us)); expiry.abs_value_us));
dkp = create_denom_key_pair (1024);
RND_BLK(&h_blind);
RND_BLK(&cbc.reserve_sig);
cbc.denom_pub = dkp->pub;
cbc.sig = NULL;
memcpy (&cbc.reserve_pub, &reserve_pub, sizeof (reserve_pub));
TALER_MINT_DB_insert_collectable_blindcoin (db,
&h_blind,
&cbc);
result = 0; result = 0;
drop: drop:
if (NULL != db) if (NULL != db)
GNUNET_break (GNUNET_OK == TALER_MINT_DB_drop_temporary (db)); GNUNET_break (GNUNET_OK == TALER_MINT_DB_drop_temporary (db));
if (NULL != dkp)
destroy_denon_key_pair (dkp);
dkp = NULL;
} }