exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

commit 0b87586b9f4e1fef16c7f2723ae6f53505af67e0
parent 719e034c7ad70b382e6bb8b9ca2ad5077d0db39b
Author: Florian Dold <dold@taler.net>
Date:   Wed, 18 Nov 2026 14:44:07 +0100

auditor: sync later reveals and repair missing melt keys

Sync missed reveals after the first copy, and older replicas lacked
transfer keys. Recheck incomplete melts against the exchange and copy
their commitment version, transfer keys and reveal flag.

Diffstat:
Msrc/auditor/taler-auditor-sync.c | 24+++++++++++++++++++++++-
Msrc/auditor/test-sync.sh | 48++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/exchangedb/meson.build | 1+
Msrc/exchangedb/sql-schema/exchange-0020.sql | 4++--
Asrc/exchangedb/sync_refresh.c | 128+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/exchange-database/sync_refresh.h | 41+++++++++++++++++++++++++++++++++++++++++
6 files changed, 243 insertions(+), 3 deletions(-)

diff --git a/src/auditor/taler-auditor-sync.c b/src/auditor/taler-auditor-sync.c @@ -136,6 +136,7 @@ struct InsertContext; #include "exchange-database/iterate_records_by_table.h" #include "exchange-database/get_rules_by_access_token.h" #include "exchange-database/get_serial_by_table.h" +#include "exchange-database/sync_refresh.h" #include "exchange-database/get_signkey.h" #include "exchange-database/get_signkey_revocation.h" #include "exchange-database/get_transfer_by_deposit.h" @@ -749,7 +750,28 @@ transact (void) } } } - /* we do not care about conflicting UPDATEs to src table, so safe to just rollback */ + { + uint64_t after = 0; + + do { + enum GNUNET_DB_QueryStatus qs; + + qs = TALER_EXCHANGEDB_sync_refresh (src, + dst, + GNUNET_MAX (1, transaction_size), + &after); + if (qs < 0) + { + if (GNUNET_DB_STATUS_HARD_ERROR == qs) + global_ret = EXIT_FAILURE; + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Failed to synchronize refresh metadata (%d)\n", + (int) qs); + return GNUNET_SYSERR; + } + actual_size += qs; + } while (0 != after); + } GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sync pass completed successfully with %llu updates\n", actual_size); diff --git a/src/auditor/test-sync.sh b/src/auditor/test-sync.sh @@ -92,6 +92,50 @@ EOF export PGHOST } +# Exercise an upgrade from a replica which copied neither transfer keys +# nor the reveal flag. The plain reference database contains real melts. +function check_refresh_sync() +{ + local fields="SELECT rc,rc_version,transfer_pubs,revealed FROM exchange.refresh ORDER BY rc" + local expected actual revealed + psql -Xq -v ON_ERROR_STOP=1 talercheck-in -c "UPDATE exchange.refresh SET revealed=FALSE" + expected=$(psql -XAt -v ON_ERROR_STOP=1 talercheck-in -c "$fields") + [[ -n "$expected" ]] || exit_fail "Missing refresh fixture" + + psql -Xq -v ON_ERROR_STOP=1 talercheck-out -c \ + "UPDATE exchange.refresh SET rc_version=0,transfer_pubs=NULL,revealed=FALSE" + actual=$(psql -XAt -v ON_ERROR_STOP=1 talercheck-out -c "$fields") + [[ "$actual" != "$expected" ]] || exit_fail "Missing refresh with transfer keys" + + taler-auditor-sync -s test-sync-in.conf -d test-sync-out.conf -t -b 1 + actual=$(psql -XAt -v ON_ERROR_STOP=1 talercheck-out -c "$fields") + [[ "$actual" == "$expected" ]] || exit_fail "Refresh metadata differs after sync" + + psql -Xq -v ON_ERROR_STOP=1 talercheck-in -c "UPDATE exchange.refresh SET revealed=TRUE" + expected=$(psql -XAt -v ON_ERROR_STOP=1 talercheck-in -c "$fields") + taler-auditor-sync -s test-sync-in.conf -d test-sync-out.conf -t -b 1 + actual=$(psql -XAt -v ON_ERROR_STOP=1 talercheck-out -c "$fields") + [[ "$actual" == "$expected" ]] || exit_fail "Later reveal missing on replica" + + # An old writer running after migration gets the current version by + # default, even though it omits transfer_pubs from its INSERT. + psql -Xq -v ON_ERROR_STOP=1 talercheck-in -c \ + "UPDATE exchange.refresh SET rc_version=DEFAULT WHERE transfer_pubs IS NOT NULL" + psql -Xq -v ON_ERROR_STOP=1 talercheck-out -c \ + "UPDATE exchange.refresh SET rc_version=DEFAULT WHERE transfer_pubs IS NOT NULL" + expected=$(psql -XAt -v ON_ERROR_STOP=1 talercheck-in -c "$fields") + for revealed in FALSE TRUE; do + # Also exercise a replica which already copied the reveal flag. + psql -Xq -v ON_ERROR_STOP=1 talercheck-out -c \ + "UPDATE exchange.refresh SET transfer_pubs=NULL,revealed=$revealed WHERE rc_version=2" + actual=$(psql -XAt -v ON_ERROR_STOP=1 talercheck-out -c "$fields") + [[ "$actual" != "$expected" ]] || exit_fail "Missing current-version refresh fixture" + taler-auditor-sync -s test-sync-in.conf -d test-sync-out.conf -t -b 1 + actual=$(psql -XAt -v ON_ERROR_STOP=1 talercheck-out -c "$fields") + [[ "$actual" == "$expected" ]] || exit_fail "Missing transfer keys after rolling upgrade" + done +} + function check_with_database() { echo -n "Testing synchronization logic ..." @@ -132,6 +176,10 @@ function check_with_database() fi done + if [[ "$1" == "$MYDIR/auditor-basedb" ]]; then + check_refresh_sync + fi + echo -n ". " dropdb talercheck-in dropdb talercheck-out diff --git a/src/exchangedb/meson.build b/src/exchangedb/meson.build @@ -172,6 +172,7 @@ libtalerexchangedb = library( 'iterate_records_by_table.c', 'get_rules_by_access_token.c', 'get_serial_by_table.c', + 'sync_refresh.c', 'get_signkey.c', 'get_signkey_revocation.c', 'get_transfer_by_deposit.c', diff --git a/src/exchangedb/sql-schema/exchange-0020.sql b/src/exchangedb/sql-schema/exchange-0020.sql @@ -50,9 +50,9 @@ BEGIN ALTER TABLE refresh ALTER COLUMN rc_version SET DEFAULT (2); - -- Auditor sync revisits melts that were pending when first copied. + -- Auditor sync revisits pending melts and incomplete replica rows. CREATE INDEX refresh_pending_reveal_idx ON refresh (refresh_id) - WHERE NOT revealed; + WHERE NOT revealed OR (rc_version <> 0 AND transfer_pubs IS NULL); END $$; diff --git a/src/exchangedb/sync_refresh.c b/src/exchangedb/sync_refresh.c @@ -0,0 +1,128 @@ +/* + This file is part of TALER + Copyright (C) 2026 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ +/** + * @file exchangedb/sync_refresh.c + * @brief reconcile previously copied melts with the exchange + */ +#include "helper.h" +#include "exchange-database/sync_refresh.h" + + +enum GNUNET_DB_QueryStatus +TALER_EXCHANGEDB_sync_refresh ( + struct TALER_EXCHANGEDB_PostgresContext *src, + struct TALER_EXCHANGEDB_PostgresContext *dst, + uint64_t limit, + uint64_t *after) +{ + char *candidates = NULL; + char *changes = NULL; + enum GNUNET_DB_QueryStatus qs; + + /* With 64 hex-encoded transfer keys per melt, 1024 rows serialize to + less than 5 MiB, below GNUnet's checked-allocation limit. */ + limit = GNUNET_MIN (limit, 1024); + + /* Old replicas omitted both transfer_pubs and revealed. Their migration + consequently classified these rows as v27, including pre-v45 melts + which had transfer keys on the source. A reveal can also happen after + the initial copy. An old sync process still running after migration + inserts rows with the new default version but without transfer keys. */ + PREPARE (dst, + "refresh_sync_candidates", + "SELECT max(refresh_id) AS last_id," + " jsonb_agg(jsonb_build_object('rc',rc,'rc_version',rc_version," + " 'missing_keys',missing_keys))::text AS batch" + " FROM (SELECT refresh_id,rc,rc_version,transfer_pubs IS NULL AS missing_keys" + " FROM refresh WHERE refresh_id > $1" + " AND (NOT revealed OR (rc_version <> 0 AND transfer_pubs IS NULL))" + " ORDER BY refresh_id LIMIT $2) pending" + " HAVING count(*) > 0"); + PREPARE (src, + "refresh_sync_changes", + "SELECT jsonb_agg(jsonb_build_object('rc',r.rc," + " 'revealed',r.revealed,'rc_version',r.rc_version," + " 'transfer_pubs',CASE WHEN r.rc_version <> q.rc_version" + " OR q.missing_keys THEN r.transfer_pubs END))::text AS batch" + " FROM jsonb_to_recordset($1::text::jsonb) q(rc bytea,rc_version int4,missing_keys bool)" + " JOIN refresh r USING (rc)" + " WHERE r.revealed OR r.rc_version <> q.rc_version" + " OR (q.missing_keys AND r.transfer_pubs IS NOT NULL)" + " HAVING count(*) > 0"); + PREPARE (dst, + "refresh_sync_apply", + "UPDATE refresh r SET revealed=(r.revealed OR s.revealed)," + " rc_version=s.rc_version," + " transfer_pubs=CASE WHEN r.rc_version <> s.rc_version" + " OR r.transfer_pubs IS NULL" + " THEN s.transfer_pubs ELSE r.transfer_pubs END" + " FROM jsonb_to_recordset($1::text::jsonb)" + " AS s(rc bytea,rc_version int4,transfer_pubs bytea[],revealed bool)" + " WHERE r.rc=s.rc AND (r.rc_version <> s.rc_version" + " OR (r.transfer_pubs IS NULL AND s.transfer_pubs IS NOT NULL)" + " OR (s.revealed AND NOT r.revealed))"); + { + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_uint64 (after), + GNUNET_PQ_query_param_uint64 (&limit), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_uint64 ("last_id", after), + GNUNET_PQ_result_spec_string ("batch", &candidates), + GNUNET_PQ_result_spec_end + }; + + qs = GNUNET_PQ_eval_prepared_singleton_select (dst->conn, + "refresh_sync_candidates", + params, + rs); + if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) + *after = 0; + if (qs <= 0) + return qs; + } + { + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (candidates), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_string ("batch", &changes), + GNUNET_PQ_result_spec_end + }; + + qs = GNUNET_PQ_eval_prepared_singleton_select (src->conn, + "refresh_sync_changes", + params, + rs); + GNUNET_free (candidates); + if (qs <= 0) + return qs; + } + { + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (changes), + GNUNET_PQ_query_param_end + }; + + qs = GNUNET_PQ_eval_prepared_non_select (dst->conn, + "refresh_sync_apply", + params); + GNUNET_free (changes); + return qs; + } +} diff --git a/src/include/exchange-database/sync_refresh.h b/src/include/exchange-database/sync_refresh.h @@ -0,0 +1,41 @@ +/* + This file is part of TALER + Copyright (C) 2026 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ +/** + * @file exchange-database/sync_refresh.h + * @brief reconcile refresh data on an auditor replica + */ +#ifndef EXCHANGE_DATABASE_SYNC_REFRESH_H +#define EXCHANGE_DATABASE_SYNC_REFRESH_H + +#include "exchangedb_lib.h" + +/** + * Check up to @a limit previously copied melts against the source. + * + * @param src authoritative exchange database + * @param dst auditor replica + * @param limit maximum number of destination rows to check + * @param[in,out] after last refresh_id checked, initially zero; zero on exhaustion + * @return number of destination rows changed, or a database error + */ +enum GNUNET_DB_QueryStatus +TALER_EXCHANGEDB_sync_refresh ( + struct TALER_EXCHANGEDB_PostgresContext *src, + struct TALER_EXCHANGEDB_PostgresContext *dst, + uint64_t limit, + uint64_t *after); + +#endif