exchange

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

do_insert_known_coins.sql (3858B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2014--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 DROP FUNCTION IF EXISTS exchange_do_insert_known_coins;
     18 
     19 -- Make a batch of coins known.  The five input arrays are parallel; entry
     20 -- i of each describes coin i.  Age commitment hashes are only used where
     21 -- in_has_age_commitments is TRUE (the arrays cannot carry NULL elements).
     22 --
     23 -- One row is returned per input coin, ordered by the input position:
     24 --   out_idx                 1-based position of the coin in the input
     25 --   out_existed             FALSE if the coin was inserted by this call
     26 --   out_known_coin_id       row of the coin, NULL if the coin was neither
     27 --                           inserted (unknown denomination) nor found
     28 --   out_denom_pub_hash      denomination stored for an existing coin, NULL
     29 --                           for a freshly inserted one
     30 --   out_age_commitment_hash age commitment stored for an existing coin
     31 --                           (NULL if none, or if freshly inserted)
     32 --
     33 -- The coin public keys in the input must be distinct.
     34 CREATE FUNCTION exchange_do_insert_known_coins(
     35   IN in_coin_pubs BYTEA[],
     36   IN in_denom_pub_hashes BYTEA[],
     37   IN in_has_age_commitments BOOLEAN[],
     38   IN in_age_commitment_hashes BYTEA[],
     39   IN in_denom_sigs BYTEA[],
     40   OUT out_idx INT8,
     41   OUT out_existed BOOLEAN,
     42   OUT out_known_coin_id INT8,
     43   OUT out_denom_pub_hash BYTEA,
     44   OUT out_age_commitment_hash BYTEA)
     45 RETURNS SETOF RECORD
     46 LANGUAGE plpgsql
     47 AS $$
     48 BEGIN
     49   RETURN QUERY
     50   WITH input_rows AS (
     51     SELECT
     52       t.coin_pub
     53      ,t.denom_pub_hash
     54      ,CASE WHEN t.has_age_commitment
     55            THEN t.age_commitment_hash
     56            ELSE NULL
     57       END AS age_commitment_hash
     58      ,t.denom_sig
     59      ,t.idx
     60     FROM UNNEST (in_coin_pubs
     61                 ,in_denom_pub_hashes
     62                 ,in_has_age_commitments
     63                 ,in_age_commitment_hashes
     64                 ,in_denom_sigs)
     65          WITH ORDINALITY
     66          AS t (coin_pub
     67               ,denom_pub_hash
     68               ,has_age_commitment
     69               ,age_commitment_hash
     70               ,denom_sig
     71               ,idx)
     72   ), ins AS (
     73     INSERT INTO known_coins
     74       (coin_pub
     75       ,denominations_serial
     76       ,age_commitment_hash
     77       ,denom_sig
     78       ,remaining)
     79     SELECT
     80       ir.coin_pub
     81      ,d.denominations_serial
     82      ,ir.age_commitment_hash
     83      ,ir.denom_sig
     84      ,d.coin
     85     FROM input_rows ir
     86     JOIN denominations d
     87       ON (d.denom_pub_hash = ir.denom_pub_hash)
     88     ON CONFLICT (coin_pub) DO NOTHING
     89     RETURNING
     90       known_coins.coin_pub
     91      ,known_coins.known_coin_id
     92   )
     93   SELECT
     94     ir.idx
     95    ,(ins.known_coin_id IS NULL) AS existed
     96    ,COALESCE (ins.known_coin_id, kc.known_coin_id)
     97    ,kd.denom_pub_hash
     98    ,kc.age_commitment_hash
     99   FROM input_rows ir
    100   LEFT JOIN ins
    101     ON (ins.coin_pub = ir.coin_pub)
    102   LEFT JOIN known_coins kc
    103     ON (kc.coin_pub = ir.coin_pub)
    104   LEFT JOIN denominations kd
    105     ON (kd.denominations_serial = kc.denominations_serial)
    106   ORDER BY ir.idx ASC;
    107 END $$;
    108 
    109 COMMENT ON FUNCTION exchange_do_insert_known_coins
    110   IS 'Inserts the coins of a batch into known_coins where they are new and returns, for every coin, whether it was known before together with the stored denomination and age commitment hashes';