exchange

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

sync_refresh.c (5291B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2026 Taler Systems SA
      4 
      5    TALER is free software; you can redistribute it and/or modify it under the
      6    terms of the GNU General Public License as published by the Free Software
      7    Foundation; either version 3, or (at your option) any later version.
      8 
      9    TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11    A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13    You should have received a copy of the GNU General Public License along with
     14    TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 /**
     17  * @file exchangedb/sync_refresh.c
     18  * @brief reconcile previously copied melts with the exchange
     19  */
     20 #include "helper.h"
     21 #include "exchange-database/sync_refresh.h"
     22 
     23 
     24 enum GNUNET_DB_QueryStatus
     25 TALER_EXCHANGEDB_sync_refresh (
     26   struct TALER_EXCHANGEDB_PostgresContext *src,
     27   struct TALER_EXCHANGEDB_PostgresContext *dst,
     28   uint64_t limit,
     29   uint64_t *after)
     30 {
     31   char *candidates = NULL;
     32   char *changes = NULL;
     33   enum GNUNET_DB_QueryStatus qs;
     34 
     35   /* With 64 hex-encoded transfer keys per melt, 1024 rows serialize to
     36      less than 5 MiB, below GNUnet's checked-allocation limit. */
     37   limit = GNUNET_MIN (limit, 1024);
     38 
     39   /* Old replicas omitted both transfer_pubs and revealed. Their migration
     40      consequently classified these rows as v27, including pre-v45 melts
     41      which had transfer keys on the source. A reveal can also happen after
     42      the initial copy. An old sync process still running after migration
     43      inserts rows with the new default version but without transfer keys. */
     44   PREPARE (dst,
     45            "refresh_sync_candidates",
     46            "SELECT max(refresh_id) AS last_id,"
     47            " jsonb_agg(jsonb_build_object('rc',rc,'rc_version',rc_version,"
     48            " 'missing_keys',missing_keys))::text AS batch"
     49            " FROM (SELECT refresh_id,rc,rc_version,transfer_pubs IS NULL AS missing_keys"
     50            "       FROM refresh WHERE refresh_id > $1"
     51            "        AND (NOT revealed OR (rc_version <> 0 AND transfer_pubs IS NULL))"
     52            "        ORDER BY refresh_id LIMIT $2) pending"
     53            " HAVING count(*) > 0");
     54   PREPARE (src,
     55            "refresh_sync_changes",
     56            "SELECT jsonb_agg(jsonb_build_object('rc',r.rc,"
     57            " 'revealed',r.revealed,'rc_version',r.rc_version,"
     58            " 'transfer_pubs',CASE WHEN r.rc_version <> q.rc_version"
     59            "                       OR q.missing_keys THEN r.transfer_pubs END))::text AS batch"
     60            " FROM jsonb_to_recordset($1::text::jsonb) q(rc bytea,rc_version int4,missing_keys bool)"
     61            " JOIN refresh r USING (rc)"
     62            " WHERE r.revealed OR r.rc_version <> q.rc_version"
     63            "    OR (q.missing_keys AND r.transfer_pubs IS NOT NULL)"
     64            " HAVING count(*) > 0");
     65   PREPARE (dst,
     66            "refresh_sync_apply",
     67            "UPDATE refresh r SET revealed=(r.revealed OR s.revealed),"
     68            " rc_version=s.rc_version,"
     69            " transfer_pubs=CASE WHEN r.rc_version <> s.rc_version"
     70            "                      OR r.transfer_pubs IS NULL"
     71            "                    THEN s.transfer_pubs ELSE r.transfer_pubs END"
     72            " FROM jsonb_to_recordset($1::text::jsonb)"
     73            " AS s(rc bytea,rc_version int4,transfer_pubs bytea[],revealed bool)"
     74            " WHERE r.rc=s.rc AND (r.rc_version <> s.rc_version"
     75            "   OR (r.transfer_pubs IS NULL AND s.transfer_pubs IS NOT NULL)"
     76            "   OR (s.revealed AND NOT r.revealed))");
     77   {
     78     struct GNUNET_PQ_QueryParam params[] = {
     79       GNUNET_PQ_query_param_uint64 (after),
     80       GNUNET_PQ_query_param_uint64 (&limit),
     81       GNUNET_PQ_query_param_end
     82     };
     83     struct GNUNET_PQ_ResultSpec rs[] = {
     84       GNUNET_PQ_result_spec_uint64 ("last_id", after),
     85       GNUNET_PQ_result_spec_string ("batch", &candidates),
     86       GNUNET_PQ_result_spec_end
     87     };
     88 
     89     qs = GNUNET_PQ_eval_prepared_singleton_select (dst->conn,
     90                                                    "refresh_sync_candidates",
     91                                                    params,
     92                                                    rs);
     93     if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
     94       *after = 0;
     95     if (qs <= 0)
     96       return qs;
     97   }
     98   {
     99     struct GNUNET_PQ_QueryParam params[] = {
    100       GNUNET_PQ_query_param_string (candidates),
    101       GNUNET_PQ_query_param_end
    102     };
    103     struct GNUNET_PQ_ResultSpec rs[] = {
    104       GNUNET_PQ_result_spec_string ("batch", &changes),
    105       GNUNET_PQ_result_spec_end
    106     };
    107 
    108     qs = GNUNET_PQ_eval_prepared_singleton_select (src->conn,
    109                                                    "refresh_sync_changes",
    110                                                    params,
    111                                                    rs);
    112     GNUNET_free (candidates);
    113     if (qs <= 0)
    114       return qs;
    115   }
    116   {
    117     struct GNUNET_PQ_QueryParam params[] = {
    118       GNUNET_PQ_query_param_string (changes),
    119       GNUNET_PQ_query_param_end
    120     };
    121 
    122     qs = GNUNET_PQ_eval_prepared_non_select (dst->conn,
    123                                             "refresh_sync_apply",
    124                                             params);
    125     GNUNET_free (changes);
    126     return qs;
    127   }
    128 }