exchange

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

exchange_do_recoup_by_reserve.sql (2498B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2014--2025 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 
     18 CREATE OR REPLACE FUNCTION exchange_do_recoup_by_reserve(
     19   IN res_pub BYTEA
     20 )
     21 RETURNS TABLE
     22 (
     23   denom_sig            BYTEA,
     24   denominations_serial INT8,
     25   coin_pub             BYTEA,
     26   coin_sig             BYTEA,
     27   coin_blind           BYTEA,
     28   amount               taler_amount,
     29   recoup_timestamp     INT8
     30 )
     31 LANGUAGE plpgsql
     32 AS $$
     33 DECLARE
     34   res_uuid INT8;
     35   blind_ev BYTEA;
     36   c_pub    BYTEA;
     37 BEGIN
     38   SELECT reserve_uuid
     39    INTO res_uuid
     40    FROM reserves
     41    WHERE reserve_pub = res_pub;
     42 
     43   FOR blind_ev IN
     44     SELECT unnest(h_blind_evs)
     45       FROM withdraw wd
     46       JOIN reserve_history rh
     47         ON (rh.serial_id = wd.withdraw_id)
     48     WHERE rh.reserve_pub = res_pub
     49       AND rh.table_name='withdraw'
     50   LOOP
     51     SELECT robr.coin_pub
     52       INTO c_pub
     53       FROM exchange.recoup_by_reserve robr
     54     WHERE robr.withdraw_id = (
     55       SELECT withdraw_id
     56         FROM withdraw
     57       WHERE h_blind_ev = ANY(h_blind_evs)
     58     );
     59     RETURN QUERY
     60       SELECT kc.denom_sig,
     61              kc.denominations_serial,
     62              rc.coin_pub,
     63              rc.coin_sig,
     64              rc.coin_blind,
     65              rc.amount,
     66              rc.recoup_timestamp
     67       FROM (
     68         SELECT denom_sig
     69               ,denominations_serial
     70         FROM exchange.known_coins
     71         WHERE known_coins.coin_pub = c_pub
     72       ) kc
     73       JOIN (
     74         SELECT coin_pub
     75               ,coin_sig
     76               ,coin_blind
     77               ,amount
     78               ,recoup_timestamp
     79         FROM exchange.recoup
     80         WHERE recoup.coin_pub = c_pub
     81       ) rc USING (coin_pub);
     82   END LOOP;
     83 END;
     84 $$;
     85 
     86 COMMENT ON FUNCTION exchange_do_recoup_by_reserve
     87   IS 'Recoup by reserve as a function to make sure we hit only the needed partition and not all when joining as joins on distributed tables fetch ALL rows from the shards';