summaryrefslogtreecommitdiff
path: root/src/exchangedb
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-06-13 15:31:52 +0200
committerChristian Grothoff <christian@grothoff.org>2022-06-13 15:31:52 +0200
commit58a0882909f2b1ede572ae575b83c18746f96cff (patch)
tree8d96afaec04abb7fd82d32a7a1b8b4ca1e9dd52d /src/exchangedb
parent70a5ceecc1a15d4f811636f5980cedf653988879 (diff)
downloadexchange-58a0882909f2b1ede572ae575b83c18746f96cff.tar.gz
exchange-58a0882909f2b1ede572ae575b83c18746f96cff.tar.bz2
exchange-58a0882909f2b1ede572ae575b83c18746f96cff.zip
-towards coin audits with purse deposits
Diffstat (limited to 'src/exchangedb')
-rw-r--r--src/exchangedb/Makefile.am26
-rw-r--r--src/exchangedb/common-0001.sql70
-rw-r--r--src/exchangedb/drop-common.sql2
-rw-r--r--src/exchangedb/drop0001-exchange-part.sql1
-rw-r--r--src/exchangedb/exchange-0001-part.sql20
-rw-r--r--src/exchangedb/irbt_callbacks.c25
-rw-r--r--src/exchangedb/lrbt_callbacks.c45
-rw-r--r--src/exchangedb/plugin_exchangedb_postgres.c349
-rw-r--r--src/exchangedb/shard-0001-part.sql27
-rw-r--r--src/exchangedb/test_exchangedb.c3
10 files changed, 552 insertions, 16 deletions
diff --git a/src/exchangedb/Makefile.am b/src/exchangedb/Makefile.am
index 6145740c4..07c0bf68a 100644
--- a/src/exchangedb/Makefile.am
+++ b/src/exchangedb/Makefile.am
@@ -14,17 +14,25 @@ pkgcfg_DATA = \
sqldir = $(prefix)/share/taler/sql/exchange/
-sql_DATA = \
- benchmark-0000.sql \
- benchmark-0001.sql \
- exchange-0000.sql \
- exchange-0001-part.sql \
- shard-0001-part.sql \
+sqlinputs = \
common-0001.sql \
drop-common.sql \
+ exchange-0001-part.sql \
drop0001-exchange-part.sql \
+ shard-0001-part.sql \
drop0001-shard-part.sql
+sql_DATA = \
+ benchmark-0000.sql \
+ benchmark-0001.sql \
+ exchange-0000.sql \
+ exchange-0001.sql \
+ drop0001.sql \
+ shard-0000.sql \
+ shard-0001.sql \
+ shard-drop0001.sql
+
+
BUILT_SOURCES = \
shard-0000.sql \
shard-0001.sql \
@@ -40,22 +48,27 @@ CLEANFILES = \
shard-drop0001.sql
exchange-0001.sql: common-0001.sql exchange-0001-part.sql
+ chmod +w $@ || true
cat common-0001.sql exchange-0001-part.sql >$@
chmod -w $@
shard-0001.sql: common-0001.sql shard-0001-part.sql
+ chmod +w $@ || true
cat common-0001.sql shard-0001-part.sql >$@
chmod -w $@
shard-0000.sql: exchange-0000.sql
+ chmod +w $@ || true
cp exchange-0000.sql $@
chmod -w $@
drop0001.sql: drop-common.sql drop0001-exchange-part.sql
+ chmod +w $@ || true
cat drop-common.sql drop0001-exchange-part.sql >$@
chmod -w $@
shard-drop0001.sql: drop-common.sql drop0001-shard-part.sql
+ chmod +w $@ || true
cat drop-common.sql drop0001-shard-part.sql >$@
chmod -w $@
@@ -67,6 +80,7 @@ EXTRA_DIST = \
lrbt_callbacks.c \
bench-db-postgres.conf \
test-exchange-db-postgres.conf \
+ $(sqlinputs) \
$(sql_DATA)
plugindir = $(libdir)/taler
diff --git a/src/exchangedb/common-0001.sql b/src/exchangedb/common-0001.sql
index ea3b74ecd..c493af9bd 100644
--- a/src/exchangedb/common-0001.sql
+++ b/src/exchangedb/common-0001.sql
@@ -1219,6 +1219,54 @@ BEGIN
END
$$;
+
+------------------------------- purse_refunds ----------------------------------------
+
+CREATE OR REPLACE FUNCTION create_table_purse_refunds(
+ IN shard_suffix VARCHAR DEFAULT NULL
+)
+RETURNS VOID
+LANGUAGE plpgsql
+AS $$
+DECLARE
+ table_name VARCHAR DEFAULT 'purse_refunds';
+BEGIN
+
+ PERFORM create_partitioned_table(
+ 'CREATE TABLE IF NOT EXISTS %I '
+ '(purse_refunds_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
+ ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
+ ',PRIMARY KEY (purse_pub)'
+ ') %s ;'
+ ,table_name
+ ,'PARTITION BY HASH (purse_pub)'
+ ,shard_suffix
+ );
+
+ table_name = concat_ws('_', table_name, shard_suffix);
+
+END
+$$;
+
+CREATE OR REPLACE FUNCTION add_constraints_to_purse_refunds_partition(
+ IN partition_suffix VARCHAR
+)
+RETURNS VOID
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ EXECUTE FORMAT (
+ 'ALTER TABLE purse_refunds_' || partition_suffix || ' '
+ 'ADD CONSTRAINT purse_refunds_' || partition_suffix || '_purse_refunds_serial_id_key '
+ 'UNIQUE (purse_refunds_serial_id) '
+ );
+END
+$$;
+
+
+
+
+
---------------------------- purse_merges -----------------------------
CREATE OR REPLACE FUNCTION create_table_purse_merges(
@@ -1828,6 +1876,9 @@ BEGIN
ALTER TABLE IF EXISTS purse_requests
DETACH partition purse_requests_default;
+ ALTER TABLE IF EXISTS purse_refunds
+ DETACH partition purse_refunds_default;
+
ALTER TABLE IF EXISTS purse_merges
DETACH partition purse_merges_default;
@@ -1894,6 +1945,7 @@ BEGIN
DROP TABLE IF EXISTS cs_nonce_locks_default;
DROP TABLE IF EXISTS purse_requests_default;
+ DROP TABLE IF EXISTS purse_refunds_default;
DROP TABLE IF EXISTS purse_merges_default;
DROP TABLE IF EXISTS account_merges_default;
DROP TABLE IF EXISTS contracts_default;
@@ -2106,6 +2158,13 @@ BEGIN
PERFORM add_constraints_to_purse_requests_partition(num_partitions::varchar);
PERFORM create_hash_partition(
+ 'purse_refunds'
+ ,modulus
+ ,num_partitions
+ );
+ PERFORM add_constraints_to_purse_refunds_partition(num_partitions::varchar);
+
+ PERFORM create_hash_partition(
'purse_merges'
,modulus
,num_partitions
@@ -2316,6 +2375,10 @@ BEGIN
DROP CONSTRAINT IF EXISTS purse_requests_pkey CASCADE
;
+ ALTER TABLE IF EXISTS purse_refunds
+ DROP CONSTRAINT IF EXISTS purse_refunds_pkey CASCADE
+ ;
+
ALTER TABLE IF EXISTS purse_merges
DROP CONSTRAINT IF EXISTS purse_merges_pkey CASCADE
;
@@ -2572,6 +2635,13 @@ BEGIN
,local_user
);
PERFORM create_foreign_hash_partition(
+ 'purse_refunds'
+ ,total_num_shards
+ ,shard_suffix
+ ,current_shard_num
+ ,local_user
+ );
+ PERFORM create_foreign_hash_partition(
'purse_merges'
,total_num_shards
,shard_suffix
diff --git a/src/exchangedb/drop-common.sql b/src/exchangedb/drop-common.sql
index 3bdff7de0..63628952c 100644
--- a/src/exchangedb/drop-common.sql
+++ b/src/exchangedb/drop-common.sql
@@ -63,6 +63,8 @@ DROP FUNCTION IF EXISTS add_constraints_to_cs_nonce_locks_partition;
DROP FUNCTION IF EXISTS create_table_purse_requests;
DROP FUNCTION IF EXISTS add_constraints_to_purse_requests_partition;
+DROP FUNCTION IF EXISTS create_table_purse_refunds;
+DROP FUNCTION IF EXISTS add_constraints_to_purse_refunds_partition;
DROP FUNCTION IF EXISTS create_table_purse_merges;
DROP FUNCTION IF EXISTS add_constraints_to_purse_merges_partition;
DROP FUNCTION IF EXISTS create_table_account_merges;
diff --git a/src/exchangedb/drop0001-exchange-part.sql b/src/exchangedb/drop0001-exchange-part.sql
index 6ea859fb4..4cf6a72fc 100644
--- a/src/exchangedb/drop0001-exchange-part.sql
+++ b/src/exchangedb/drop0001-exchange-part.sql
@@ -73,6 +73,7 @@ DROP TABLE IF EXISTS contracts CASCADE;
DROP TABLE IF EXISTS history_requests CASCADE;
DROP TABLE IF EXISTS close_requests CASCADE;
DROP TABLE IF EXISTS purse_requests CASCADE;
+DROP TABLE IF EXISTS purse_refunds CASCADE;
DROP TABLE IF EXISTS wads_out CASCADE;
DROP TABLE IF EXISTS wad_out_entries CASCADE;
DROP TABLE IF EXISTS wads_in CASCADE;
diff --git a/src/exchangedb/exchange-0001-part.sql b/src/exchangedb/exchange-0001-part.sql
index 75f65ffb6..cc34b6be3 100644
--- a/src/exchangedb/exchange-0001-part.sql
+++ b/src/exchangedb/exchange-0001-part.sql
@@ -1040,6 +1040,21 @@ CREATE TABLE IF NOT EXISTS purse_requests_default
SELECT add_constraints_to_purse_requests_partition('default');
+-- ------------------------------ purse_refunds ----------------------------------------
+
+SELECT create_table_purse_refunds();
+
+COMMENT ON TABLE purse_refunds
+ IS 'Purses that were refunded due to expiration';
+COMMENT ON COLUMN purse_refunds.purse_pub
+ IS 'Public key of the purse';
+
+CREATE TABLE IF NOT EXISTS purse_refunds_default
+ PARTITION OF purse_refunds
+ FOR VALUES WITH (MODULUS 1, REMAINDER 0);
+
+SELECT add_constraints_to_purse_refunds_partition('default');
+
-- ------------------------------ purse_merges ----------------------------------------
@@ -3485,6 +3500,11 @@ UPDATE purse_requests
finished=TRUE
WHERE purse_pub=my_purse_pub;
+INSERT INTO purse_refunds
+ (purse_pub)
+ VALUES
+ (my_purse_pub);
+
-- restore balance to each coin deposited into the purse
FOR my_deposit IN
SELECT coin_pub
diff --git a/src/exchangedb/irbt_callbacks.c b/src/exchangedb/irbt_callbacks.c
index 820911956..250b167ef 100644
--- a/src/exchangedb/irbt_callbacks.c
+++ b/src/exchangedb/irbt_callbacks.c
@@ -810,6 +810,8 @@ irbt_cb_table_purse_requests (struct PostgresClosure *pg,
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_uint64 (&td->serial),
GNUNET_PQ_query_param_auto_from_type (
+ &td->details.purse_requests.purse_pub),
+ GNUNET_PQ_query_param_auto_from_type (
&td->details.purse_requests.merge_pub),
GNUNET_PQ_query_param_timestamp (
&td->details.purse_requests.purse_creation),
@@ -833,6 +835,29 @@ irbt_cb_table_purse_requests (struct PostgresClosure *pg,
/**
+ * Function called with purse_refunds records to insert into table.
+ *
+ * @param pg plugin context
+ * @param td record to insert
+ */
+static enum GNUNET_DB_QueryStatus
+irbt_cb_table_purse_refunds (struct PostgresClosure *pg,
+ const struct TALER_EXCHANGEDB_TableData *td)
+{
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_uint64 (&td->serial),
+ GNUNET_PQ_query_param_auto_from_type (
+ &td->details.purse_refunds.purse_pub),
+ GNUNET_PQ_query_param_end
+ };
+
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "insert_into_table_purse_refunds",
+ params);
+}
+
+
+/**
* Function called with purse_merges records to insert into table.
*
* @param pg plugin context
diff --git a/src/exchangedb/lrbt_callbacks.c b/src/exchangedb/lrbt_callbacks.c
index 7050199e5..b8ab49cf1 100644
--- a/src/exchangedb/lrbt_callbacks.c
+++ b/src/exchangedb/lrbt_callbacks.c
@@ -1550,6 +1550,51 @@ lrbt_cb_table_purse_requests (void *cls,
/**
+ * Function called with purse_refunds table entries.
+ *
+ * @param cls closure
+ * @param result the postgres result
+ * @param num_results the number of results in @a result
+ */
+static void
+lrbt_cb_table_purse_refunds (void *cls,
+ PGresult *result,
+ unsigned int num_results)
+{
+ struct LookupRecordsByTableContext *ctx = cls;
+ struct TALER_EXCHANGEDB_TableData td = {
+ .table = TALER_EXCHANGEDB_RT_PURSE_REFUNDS
+ };
+
+ for (unsigned int i = 0; i<num_results; i++)
+ {
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ GNUNET_PQ_result_spec_uint64 (
+ "purse_refunds_serial_id",
+ &td.serial),
+ GNUNET_PQ_result_spec_auto_from_type (
+ "purse_pub",
+ &td.details.purse_refunds.purse_pub),
+ GNUNET_PQ_result_spec_end
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_PQ_extract_result (result,
+ rs,
+ i))
+ {
+ GNUNET_break (0);
+ ctx->error = true;
+ return;
+ }
+ ctx->cb (ctx->cb_cls,
+ &td);
+ GNUNET_PQ_cleanup_result (rs);
+ }
+}
+
+
+/**
* Function called with purse_merges table entries.
*
* @param cls closure
diff --git a/src/exchangedb/plugin_exchangedb_postgres.c b/src/exchangedb/plugin_exchangedb_postgres.c
index dac783e53..f63790caa 100644
--- a/src/exchangedb/plugin_exchangedb_postgres.c
+++ b/src/exchangedb/plugin_exchangedb_postgres.c
@@ -1407,6 +1407,19 @@ prepare_statements (struct PostgresClosure *pg)
" WHERE ref.refund_serial_id>=$1"
" ORDER BY ref.refund_serial_id ASC;",
1),
+ GNUNET_PQ_make_prepare (
+ "test_refund_full",
+ "SELECT"
+ " CAST(SUM(CAST(ref.amount_with_fee_frac AS INT8)) AS INT8) AS s_f"
+ ",CAST(SUM(ref.amount_with_fee_val) AS INT8) AS s_v"
+ ",dep.amount_with_fee_val"
+ ",dep.amount_with_fee_frac"
+ " FROM refunds ref"
+ " JOIN deposits dep"
+ " ON (ref.coin_pub=dep.coin_pub AND ref.deposit_serial_id=dep.deposit_serial_id)"
+ " WHERE ref.refund_serial_id=$1"
+ " GROUP BY (dep.amount_with_fee_val, dep.amount_with_fee_frac);",
+ 1),
/* Store information about a /deposit the exchange is to execute.
Used in #postgres_insert_deposit(). Only used in test cases. */
@@ -1508,6 +1521,29 @@ prepare_statements (struct PostgresClosure *pg)
" )"
" ORDER BY purse_deposit_serial_id ASC;",
1),
+ GNUNET_PQ_make_prepare (
+ "audit_get_purse_deposits_by_purse",
+ "SELECT"
+ " pd.amount_with_fee_val"
+ ",pd.amount_with_fee_frac"
+ ",pd.coin_pub"
+ ",denom.denom_pub"
+ " FROM purse_deposits pd"
+ " JOIN known_coins kc USING (coin_pub)"
+ " JOIN denominations denom USING (denominations_serial)"
+ " WHERE purse_pub=$1;",
+ 1),
+ GNUNET_PQ_make_prepare (
+ "audit_get_purse_refunds_incr",
+ "SELECT"
+ " purse_pub"
+ ",purse_refunds_serial_id"
+ " FROM purse_refunds"
+ " WHERE ("
+ " (purse_refunds_serial_id>=$1)"
+ " )"
+ " ORDER BY purse_refunds_serial_id ASC;",
+ 1),
/* Fetch an existing deposit request.
Used in #postgres_lookup_transfer_by_deposit(). */
GNUNET_PQ_make_prepare (
@@ -2779,6 +2815,14 @@ prepare_statements (struct PostgresClosure *pg)
" LIMIT 1;",
0),
GNUNET_PQ_make_prepare (
+ "select_serial_by_table_purse_refunds",
+ "SELECT"
+ " purse_refunds_serial_id AS serial"
+ " FROM purse_refunds"
+ " ORDER BY purse_refunds_serial_id DESC"
+ " LIMIT 1;",
+ 0),
+ GNUNET_PQ_make_prepare (
"select_serial_by_table_purse_merges",
"SELECT"
" purse_merge_request_serial_id AS serial"
@@ -3213,6 +3257,15 @@ prepare_statements (struct PostgresClosure *pg)
" ORDER BY purse_requests_serial_id ASC;",
1),
GNUNET_PQ_make_prepare (
+ "select_above_serial_by_table_purse_refunds",
+ "SELECT"
+ " purse_refunds_serial_id"
+ ",purse_pub"
+ " FROM purse_refunds"
+ " WHERE purse_refunds_serial_id > $1"
+ " ORDER BY purse_refunds_serial_id ASC;",
+ 1),
+ GNUNET_PQ_make_prepare (
"select_above_serial_by_table_purse_merges",
"SELECT"
" purse_merge_request_serial_id"
@@ -3689,6 +3742,14 @@ prepare_statements (struct PostgresClosure *pg)
"($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13);",
13),
GNUNET_PQ_make_prepare (
+ "insert_into_table_purse_refunds",
+ "INSERT INTO purse_refunds"
+ "(purse_refunds_serial_id"
+ ",purse_pub"
+ ") VALUES "
+ "($1, $2);",
+ 2),
+ GNUNET_PQ_make_prepare (
"insert_into_table_purse_merges",
"INSERT INTO purse_merges"
"(purse_merge_request_serial_id"
@@ -10497,6 +10558,242 @@ postgres_select_purse_deposits_above_serial_id (
/**
+ * Closure for #purse_refund_serial_helper_cb().
+ */
+struct PurseRefundSerialContext
+{
+
+ /**
+ * Callback to call.
+ */
+ TALER_EXCHANGEDB_PurseRefundCallback cb;
+
+ /**
+ * Closure for @e cb.
+ */
+ void *cb_cls;
+
+ /**
+ * Plugin context.
+ */
+ struct PostgresClosure *pg;
+
+ /**
+ * Status code, set to #GNUNET_SYSERR on hard errors.
+ */
+ enum GNUNET_GenericReturnValue status;
+};
+
+
+/**
+ * Helper function to be called with the results of a SELECT statement
+ * that has returned @a num_results results.
+ *
+ * @param cls closure of type `struct PurseRefundSerialContext`
+ * @param result the postgres result
+ * @param num_results the number of results in @a result
+ */
+static void
+purse_refund_serial_helper_cb (void *cls,
+ PGresult *result,
+ unsigned int num_results)
+{
+ struct PurseRefundSerialContext *dsc = cls;
+
+ for (unsigned int i = 0; i<num_results; i++)
+ {
+ struct TALER_PurseContractPublicKeyP purse_pub;
+ uint64_t rowid;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ GNUNET_PQ_result_spec_auto_from_type ("purse_pub",
+ &purse_pub),
+ GNUNET_PQ_result_spec_uint64 ("purse_deposit_serial_id",
+ &rowid),
+ GNUNET_PQ_result_spec_end
+ };
+ enum GNUNET_GenericReturnValue ret;
+
+ if (GNUNET_OK !=
+ GNUNET_PQ_extract_result (result,
+ rs,
+ i))
+ {
+ GNUNET_break (0);
+ dsc->status = GNUNET_SYSERR;
+ return;
+ }
+ ret = dsc->cb (dsc->cb_cls,
+ rowid,
+ &purse_pub);
+ GNUNET_PQ_cleanup_result (rs);
+ if (GNUNET_OK != ret)
+ break;
+ }
+}
+
+
+/**
+ * Select purse refunds above @a serial_id in monotonically increasing
+ * order.
+ *
+ * @param cls closure
+ * @param serial_id highest serial ID to exclude (select strictly larger)
+ * @param cb function to call on each result
+ * @param cb_cls closure for @a cb
+ * @return transaction status code
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_select_purse_refunds_above_serial_id (
+ void *cls,
+ uint64_t serial_id,
+ TALER_EXCHANGEDB_PurseRefundCallback cb,
+ void *cb_cls)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_uint64 (&serial_id),
+ GNUNET_PQ_query_param_end
+ };
+ struct PurseRefundSerialContext dsc = {
+ .cb = cb,
+ .cb_cls = cb_cls,
+ .pg = pg,
+ .status = GNUNET_OK
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+ qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
+ "audit_get_purse_refunds_incr",
+ params,
+ &purse_refund_serial_helper_cb,
+ &dsc);
+ if (GNUNET_OK != dsc.status)
+ return GNUNET_DB_STATUS_HARD_ERROR;
+ return qs;
+}
+
+
+/**
+ * Closure for #purse_refund_coin_helper_cb().
+ */
+struct PurseRefundCoinContext
+{
+
+ /**
+ * Callback to call.
+ */
+ TALER_EXCHANGEDB_PurseRefundCoinCallback cb;
+
+ /**
+ * Closure for @e cb.
+ */
+ void *cb_cls;
+
+ /**
+ * Plugin context.
+ */
+ struct PostgresClosure *pg;
+
+ /**
+ * Status code, set to #GNUNET_SYSERR on hard errors.
+ */
+ enum GNUNET_GenericReturnValue status;
+};
+
+
+/**
+ * Helper function to be called with the results of a SELECT statement
+ * that has returned @a num_results results.
+ *
+ * @param cls closure of type `struct PurseRefundCoinContext`
+ * @param result the postgres result
+ * @param num_results the number of results in @a result
+ */
+static void
+purse_refund_coin_helper_cb (void *cls,
+ PGresult *result,
+ unsigned int num_results)
+{
+ struct PurseRefundCoinContext *dsc = cls;
+ struct PostgresClosure *pg = dsc->pg;
+
+ for (unsigned int i = 0; i<num_results; i++)
+ {
+ struct TALER_Amount amount_with_fee;
+ struct TALER_CoinSpendPublicKeyP coin_pub;
+ struct TALER_DenominationPublicKey denom_pub;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ TALER_PQ_result_spec_denom_pub ("denom_pub",
+ &denom_pub),
+ TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
+ &amount_with_fee),
+ GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
+ &coin_pub),
+ GNUNET_PQ_result_spec_end
+ };
+ enum GNUNET_GenericReturnValue ret;
+
+ if (GNUNET_OK !=
+ GNUNET_PQ_extract_result (result,
+ rs,
+ i))
+ {
+ GNUNET_break (0);
+ dsc->status = GNUNET_SYSERR;
+ return;
+ }
+ ret = dsc->cb (dsc->cb_cls,
+ &amount_with_fee,
+ &coin_pub,
+ &denom_pub);
+ GNUNET_PQ_cleanup_result (rs);
+ if (GNUNET_OK != ret)
+ break;
+ }
+}
+
+
+/**
+ * Select coin affected by purse refund.
+ *
+ * @param cls closure
+ * @param purse_pub purse that was refunded
+ * @param cb function to call on each result
+ * @param cb_cls closure for @a cb
+ * @return transaction status code
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_select_purse_deposits_by_purse (
+ void *cls,
+ const struct TALER_PurseContractPublicKeyP *purse_pub,
+ TALER_EXCHANGEDB_PurseRefundCoinCallback cb,
+ void *cb_cls)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (purse_pub),
+ GNUNET_PQ_query_param_end
+ };
+ struct PurseRefundCoinContext dsc = {
+ .cb = cb,
+ .cb_cls = cb_cls,
+ .pg = pg,
+ .status = GNUNET_OK
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+ qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
+ "audit_get_purse_deposits_by_purse",
+ params,
+ &purse_refund_coin_helper_cb,
+ &dsc);
+ if (GNUNET_OK != dsc.status)
+ return GNUNET_DB_STATUS_HARD_ERROR;
+ return qs;
+}
+
+
+/**
* Closure for #refreshs_serial_helper_cb().
*/
struct RefreshsSerialContext
@@ -10690,6 +10987,7 @@ refunds_serial_helper_cb (void *cls,
struct TALER_EXCHANGEDB_Refund refund;
struct TALER_DenominationPublicKey denom_pub;
uint64_t rowid;
+ bool full_refund;
struct GNUNET_PQ_ResultSpec rs[] = {
GNUNET_PQ_result_spec_auto_from_type ("merchant_pub",
&refund.details.merchant_pub),
@@ -10720,6 +11018,42 @@ refunds_serial_helper_cb (void *cls,
rsc->status = GNUNET_SYSERR;
return;
}
+ {
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_uint64 (&rowid),
+ GNUNET_PQ_query_param_end
+ };
+ struct TALER_Amount amount_with_fee;
+ uint64_t s_f;
+ uint64_t s_v;
+ struct GNUNET_PQ_ResultSpec rs2[] = {
+ GNUNET_PQ_result_spec_uint64 ("s_v",
+ &s_v),
+ GNUNET_PQ_result_spec_uint64 ("s_f",
+ &s_f),
+ TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
+ &amount_with_fee),
+ GNUNET_PQ_result_spec_end
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+ qs = GNUNET_PQ_eval_prepared_singleton_select (
+ pg->conn,
+ "test_refund_full",
+ params,
+ rs2);
+ if (qs <= 0)
+ {
+ GNUNET_break (0);
+ rsc->status = GNUNET_SYSERR;
+ return;
+ }
+ /* normalize */
+ s_v += s_f / TALER_AMOUNT_FRAC_BASE;
+ s_f %= TALER_AMOUNT_FRAC_BASE;
+ full_refund = (s_v >= amount_with_fee.value) &&
+ (s_f >= amount_with_fee.fraction);
+ }
ret = rsc->cb (rsc->cb_cls,
rowid,
&denom_pub,
@@ -10728,6 +11062,7 @@ refunds_serial_helper_cb (void *cls,
&refund.details.merchant_sig,
&refund.details.h_contract_terms,
refund.details.rtransaction_id,
+ full_refund,
&refund.details.refund_amount);
GNUNET_PQ_cleanup_result (rs);
if (GNUNET_OK != ret)
@@ -13129,6 +13464,9 @@ postgres_lookup_serial_by_table (void *cls,
case TALER_EXCHANGEDB_RT_PURSE_REQUESTS:
statement = "select_serial_by_table_purse_requests";
break;
+ case TALER_EXCHANGEDB_RT_PURSE_REFUNDS:
+ statement = "select_serial_by_table_purse_refunds";
+ break;
case TALER_EXCHANGEDB_RT_PURSE_MERGES:
statement = "select_serial_by_table_purse_merges";
break;
@@ -13337,6 +13675,10 @@ postgres_lookup_records_by_table (void *cls,
statement = "select_above_serial_by_table_purse_requests";
rh = &lrbt_cb_table_purse_requests;
break;
+ case TALER_EXCHANGEDB_RT_PURSE_REFUNDS:
+ statement = "select_above_serial_by_table_purse_refunds";
+ rh = &lrbt_cb_table_purse_refunds;
+ break;
case TALER_EXCHANGEDB_RT_PURSE_MERGES:
statement = "select_above_serial_by_table_purse_merges";
rh = &lrbt_cb_table_purse_merges;
@@ -13510,6 +13852,9 @@ postgres_insert_records_by_table (void *cls,
case TALER_EXCHANGEDB_RT_PURSE_REQUESTS:
rh = &irbt_cb_table_purse_requests;
break;
+ case TALER_EXCHANGEDB_RT_PURSE_REFUNDS:
+ rh = &irbt_cb_table_purse_refunds;
+ break;
case TALER_EXCHANGEDB_RT_PURSE_MERGES:
rh = &irbt_cb_table_purse_merges;
break;
@@ -15203,6 +15548,10 @@ libtaler_plugin_exchangedb_postgres_init (void *cls)
= &postgres_select_deposits_above_serial_id;
plugin->select_purse_deposits_above_serial_id
= &postgres_select_purse_deposits_above_serial_id;
+ plugin->select_purse_refunds_above_serial_id
+ = &postgres_select_purse_refunds_above_serial_id;
+ plugin->select_purse_deposits_by_purse
+ = &postgres_select_purse_deposits_by_purse;
plugin->select_refreshes_above_serial_id
= &postgres_select_refreshes_above_serial_id;
plugin->select_refunds_above_serial_id
diff --git a/src/exchangedb/shard-0001-part.sql b/src/exchangedb/shard-0001-part.sql
index aa912c058..e642fe65e 100644
--- a/src/exchangedb/shard-0001-part.sql
+++ b/src/exchangedb/shard-0001-part.sql
@@ -26,7 +26,7 @@ CREATE OR REPLACE FUNCTION setup_shard(
RETURNS VOID
LANGUAGE plpgsql
AS $$
-DECLARE
+DECLARE
shard_suffix VARCHAR;
BEGIN
@@ -51,26 +51,26 @@ BEGIN
PERFORM create_table_refresh_commitments(shard_suffix);
PERFORM add_constraints_to_refresh_commitments_partition(shard_suffix);
-
+
PERFORM create_table_refresh_revealed_coins(shard_suffix);
PERFORM add_constraints_to_refresh_revealed_coins_partition(shard_suffix);
-
+
PERFORM create_table_refresh_transfer_keys(shard_suffix);
PERFORM add_constraints_to_refresh_transfer_keys_partition(shard_suffix);
-
+
PERFORM create_table_deposits(shard_suffix);
PERFORM add_constraints_to_deposits_partition(shard_suffix);
PERFORM create_table_deposits_by_ready(shard_suffix);
PERFORM create_table_deposits_for_matching(shard_suffix);
-
+
PERFORM create_table_refunds(shard_suffix);
PERFORM add_constraints_to_refunds_partition(shard_suffix);
-
+
PERFORM create_table_wire_out(shard_suffix);
PERFORM add_constraints_to_wire_out_partition(shard_suffix);
-
+
PERFORM create_table_aggregation_transient(shard_suffix);
PERFORM create_table_aggregation_tracking(shard_suffix);
@@ -92,6 +92,9 @@ BEGIN
PERFORM create_table_purse_requests(shard_suffix);
PERFORM add_constraints_to_purse_requests_partition(shard_suffix);
+ PERFORM create_table_purse_refunds(shard_suffix);
+ PERFORM add_constraints_to_purse_refunds_partition(shard_suffix);
+
PERFORM create_table_purse_merges(shard_suffix);
PERFORM add_constraints_to_purse_merges_partition(shard_suffix);
@@ -121,15 +124,15 @@ $$;
CREATE OR REPLACE FUNCTION drop_shard(
- shard_idx INTEGER
+ shard_idx INTEGER
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
-DECLARE
+DECLARE
shard_suffix VARCHAR;
BEGIN
-
+
shard_suffix = shard_idx::varchar;
EXECUTE FORMAT(
@@ -226,6 +229,10 @@ BEGIN
);
EXECUTE FORMAT(
'DROP TABLE IF EXISTS %I CASCADE'
+ ,'purse_refunds_' || shard_suffix
+ );
+ EXECUTE FORMAT(
+ 'DROP TABLE IF EXISTS %I CASCADE'
,'purse_merges_' || shard_suffix
);
EXECUTE FORMAT(
diff --git a/src/exchangedb/test_exchangedb.c b/src/exchangedb/test_exchangedb.c
index 127895d65..54601c48d 100644
--- a/src/exchangedb/test_exchangedb.c
+++ b/src/exchangedb/test_exchangedb.c
@@ -646,6 +646,7 @@ audit_deposit_cb (void *cls,
* @param h_contract_terms hash of the proposal data in
* the contract between merchant and customer
* @param rtransaction_id refund transaction ID chosen by the merchant
+ * @param full_refund the deposit
* @param amount_with_fee amount that was deposited including fee
* @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
*/
@@ -658,6 +659,7 @@ audit_refund_cb (void *cls,
const struct TALER_MerchantSignatureP *merchant_sig,
const struct TALER_PrivateContractHashP *h_contract_terms,
uint64_t rtransaction_id,
+ bool full_refund,
const struct TALER_Amount *amount_with_fee)
{
(void) cls;
@@ -669,6 +671,7 @@ audit_refund_cb (void *cls,
(void) h_contract_terms;
(void) rtransaction_id;
(void) amount_with_fee;
+ (void) full_refund;
auditor_row_cnt++;
return GNUNET_OK;
}