exchange

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

0002-purse_deposits.sql (4359B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2014--2023 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 CREATE FUNCTION create_table_purse_deposits(
     18   IN partition_suffix TEXT DEFAULT NULL
     19 )
     20 RETURNS VOID
     21 LANGUAGE plpgsql
     22 AS $$
     23 DECLARE
     24   table_name TEXT DEFAULT 'purse_deposits';
     25 BEGIN
     26   PERFORM create_partitioned_table(
     27     'CREATE TABLE %I '
     28       '(purse_deposit_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
     29       ',partner_serial_id INT8'
     30       ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
     31       ',coin_pub BYTEA NOT NULL'
     32       ',amount_with_fee taler_amount NOT NULL'
     33       ',coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64)'
     34       ',PRIMARY KEY (purse_pub,coin_pub)'
     35     ') %s ;'
     36     ,table_name
     37     ,'PARTITION BY HASH (purse_pub)'
     38     ,partition_suffix
     39   );
     40   PERFORM comment_partitioned_table(
     41      'Requests depositing coins into a purse'
     42     ,table_name
     43     ,partition_suffix
     44   );
     45   PERFORM comment_partitioned_column(
     46      'identifies the partner exchange, NULL in case the target purse lives at this exchange'
     47     ,'partner_serial_id'
     48     ,table_name
     49     ,partition_suffix
     50   );
     51   PERFORM comment_partitioned_column(
     52      'Public key of the purse'
     53     ,'purse_pub'
     54     ,table_name
     55     ,partition_suffix
     56   );
     57   PERFORM comment_partitioned_column(
     58      'Public key of the coin being deposited'
     59     ,'coin_pub'
     60     ,table_name
     61     ,partition_suffix
     62   );
     63   PERFORM comment_partitioned_column(
     64      'Total amount being deposited'
     65     ,'amount_with_fee'
     66     ,table_name
     67     ,partition_suffix
     68   );
     69   PERFORM comment_partitioned_column(
     70      'Signature of the coin affirming the deposit into the purse, of type TALER_SIGNATURE_PURSE_DEPOSIT'
     71     ,'coin_sig'
     72     ,table_name
     73     ,partition_suffix
     74   );
     75 END
     76 $$;
     77 
     78 
     79 CREATE FUNCTION constrain_table_purse_deposits(
     80   IN partition_suffix TEXT
     81 )
     82 RETURNS VOID
     83 LANGUAGE plpgsql
     84 AS $$
     85 DECLARE
     86   table_name TEXT DEFAULT 'purse_deposits';
     87 BEGIN
     88   table_name = concat_ws('_', table_name, partition_suffix);
     89 
     90   EXECUTE FORMAT (
     91     'ALTER TABLE ' || table_name ||
     92     ' ADD CONSTRAINT ' || table_name || '_purse_deposit_serial_id_key'
     93     ' UNIQUE (purse_deposit_serial_id) '
     94   );
     95 END
     96 $$;
     97 
     98 
     99 CREATE FUNCTION foreign_table_purse_deposits()
    100 RETURNS VOID
    101 LANGUAGE plpgsql
    102 AS $$
    103 DECLARE
    104   table_name TEXT DEFAULT 'purse_deposits';
    105 BEGIN
    106   EXECUTE FORMAT (
    107     'ALTER TABLE ' || table_name ||
    108     ' ADD CONSTRAINT ' || table_name || '_foreign_partner'
    109     ' FOREIGN KEY (partner_serial_id) '
    110     ' REFERENCES partners(partner_serial_id) ON DELETE CASCADE'
    111     ',ADD CONSTRAINT ' || table_name || '_foreign_coin_pub'
    112     ' FOREIGN KEY (coin_pub) '
    113     ' REFERENCES known_coins (coin_pub) ON DELETE CASCADE'
    114   );
    115 END
    116 $$;
    117 
    118 
    119 CREATE OR REPLACE FUNCTION purse_deposits_insert_trigger()
    120   RETURNS trigger
    121   LANGUAGE plpgsql
    122   AS $$
    123 BEGIN
    124   INSERT INTO exchange.coin_history
    125     (coin_pub
    126     ,table_name
    127     ,serial_id)
    128  VALUES
    129     (NEW.coin_pub
    130     ,'purse_deposits'
    131     ,NEW.purse_deposit_serial_id);
    132   RETURN NEW;
    133 END $$;
    134 COMMENT ON FUNCTION purse_deposits_insert_trigger()
    135   IS 'Automatically generate coin history entry.';
    136 
    137 
    138 CREATE FUNCTION master_table_purse_deposits()
    139 RETURNS VOID
    140 LANGUAGE plpgsql
    141 AS $$
    142 BEGIN
    143   CREATE TRIGGER purse_deposits_on_insert
    144     AFTER INSERT
    145      ON purse_deposits
    146      FOR EACH ROW EXECUTE FUNCTION purse_deposits_insert_trigger();
    147 END $$;
    148 
    149 
    150 INSERT INTO exchange_tables
    151     (name
    152     ,version
    153     ,action
    154     ,partitioned
    155     ,by_range)
    156   VALUES
    157     ('purse_deposits'
    158     ,'exchange-0002'
    159     ,'create'
    160     ,TRUE
    161     ,FALSE),
    162     ('purse_deposits'
    163     ,'exchange-0002'
    164     ,'constrain'
    165     ,TRUE
    166     ,FALSE),
    167     ('purse_deposits'
    168     ,'exchange-0002'
    169     ,'foreign'
    170     ,TRUE
    171     ,FALSE),
    172     ('purse_deposits'
    173     ,'exchange-0002'
    174     ,'master'
    175     ,TRUE
    176     ,FALSE);