diff options
author | Marcello Stanisci <marcello.stanisci@inria.fr> | 2016-12-08 18:36:31 +0100 |
---|---|---|
committer | Marcello Stanisci <marcello.stanisci@inria.fr> | 2016-12-08 18:36:31 +0100 |
commit | 47d23897ad8b81e4ea9e38ddc6c3f77d6e251bbf (patch) | |
tree | 5faf3d74017e0da038080787f6f9f260c4ebe021 | |
parent | 51a18d564fdd823be141db77b4e80de506eb5348 (diff) | |
download | merchant-47d23897ad8b81e4ea9e38ddc6c3f77d6e251bbf.tar.gz merchant-47d23897ad8b81e4ea9e38ddc6c3f77d6e251bbf.tar.bz2 merchant-47d23897ad8b81e4ea9e38ddc6c3f77d6e251bbf.zip |
Baking find_contract by hashcode. Invoked in testcase.
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/backenddb/plugin_merchantdb_postgres.c | 59 | ||||
-rw-r--r-- | src/backenddb/test_merchantdb.c | 19 | ||||
-rw-r--r-- | src/include/taler_merchantdb_plugin.h | 18 |
5 files changed, 94 insertions, 5 deletions
diff --git a/configure.ac b/configure.ac index 36ad39a3..138c8a8a 100644 --- a/configure.ac +++ b/configure.ac @@ -279,6 +279,5 @@ src/Makefile src/include/Makefile src/backenddb/Makefile src/backend/Makefile -src/merchant-tools/Makefile src/lib/Makefile]) AC_OUTPUT diff --git a/src/Makefile.am b/src/Makefile.am index e58cf01a..d0b0df1e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,3 @@ # This Makefile is in the public domain AM_CPPFLAGS = -I$(top_srcdir)/src/include -SUBDIRS = include backenddb backend lib merchant-tools +SUBDIRS = include backenddb backend lib diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index 272c12cd..a01357fb 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -155,7 +155,7 @@ postgres_initialize (void *cls) /* Setup tables */ PG_EXEC (pg, "CREATE TABLE IF NOT EXISTS merchant_contract_maps (" - ",h_contract BYTEA NOT NULL CHECK (LENGTH(h_contract)=64)" + "h_contract BYTEA NOT NULL CHECK (LENGTH(h_contract)=64)" ",plain_contract BYTEA NOT NULL" ",PRIMARY KEY (h_contract)" ");"); @@ -381,6 +381,62 @@ postgres_initialize (void *cls) return GNUNET_OK; } +/** + * Retrieve plain contract given its hashcode + * + * @param cls closure + * @param h_contract hashcode of the contract to retrieve + * @param contract where to store the retrieved contract + * @return #GNUNET_OK on success, #GNUNET_NO if no contract is + * found, #GNUNET_SYSERR upon error + */ +static int +postgres_find_contract (void *cls, + json_t *contract, /*Legal?*/ + struct GNUNET_HashCode *h_contract) +{ + struct PostgresClosure *pg = cls; + PGresult *result; + unsigned int i; + + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (h_contract), + GNUNET_PQ_query_param_end + }; + + result = GNUNET_PQ_exec_prepared (pg->conn, + "find_contract", + params); + i = PQntuples (result); + if (1 > i) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Mupltiple contracts share the same hashcode.\n"); + return GNUNET_SYSERR; + } + + if (0 == i) + return GNUNET_NO; + + /* FIXME, figure out how to pass back json_t's */ + struct GNUNET_PQ_ResultSpec rs[] = { + TALER_PQ_result_spec_json ("plain_contract", + &contract), + GNUNET_PQ_result_spec_end + }; + if (GNUNET_OK != + GNUNET_PQ_extract_result (result, + rs, + 0)) + { + GNUNET_break (0); + PQclear (result); + return GNUNET_SYSERR; + } + + return GNUNET_OK; +} + /** * Insert a hash to contract map into the database @@ -1285,6 +1341,7 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) plugin->find_deposits_by_wtid = &postgres_find_deposits_by_wtid; plugin->find_proof_by_wtid = &postgres_find_proof_by_wtid; plugin->store_map = &postgres_store_map; + plugin->find_contract = &postgres_find_contract; return plugin; } diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c index f67b0f7d..4e81dd98 100644 --- a/src/backenddb/test_merchantdb.c +++ b/src/backenddb/test_merchantdb.c @@ -130,6 +130,11 @@ static json_t *deposit_proof; */ static json_t *transfer_proof; +/** + * A mock contract, not need to be well-formed + */ +static json_t *contract; + /** * Function called with information about a transaction. @@ -347,6 +352,20 @@ run (void *cls) json_object_set_new (transfer_proof, "test", json_string ("backenddb test B"))); + contract = json_object (); + + FAILIF (GNUNET_OK != + plugin->store_map (plugin->cls, + &h_contract, + contract)); + + json_t *out; + + FAILIF (GNUNET_OK != + plugin->find_contract (plugin->cls, + &contract, + &out)); + FAILIF (GNUNET_OK != plugin->store_transaction (plugin->cls, transaction_id, diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h index e5507141..a11fa7c7 100644 --- a/src/include/taler_merchantdb_plugin.h +++ b/src/include/taler_merchantdb_plugin.h @@ -159,11 +159,25 @@ struct TALER_MERCHANTDB_Plugin */ int (*store_map) (void *cls, - struct GNUNET_HashCode *h_contract, - const json_t *contract); + struct GNUNET_HashCode *h_contract, + const json_t *contract); /** + * Retrieve plain contract given its hashcode + * + * @param cls closure + * @param h_contract hashcode of the contract to retrieve + * @param contract where to store the retrieved contract + * @return #GNUNET_OK on success, #GNUNET_NO if no contract is + * found, #GNUNET_SYSERR upon error + */ + int + (*find_contract) (void *cls, + json_t *contract, + struct GNUNET_HashCode *h_contract); + + /** * Insert transaction data into the database. * * @param cls closure |