0002-known_coins.sql (3743B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2014--2022 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 FUNCTION create_table_known_coins( 19 IN partition_suffix TEXT DEFAULT NULL 20 ) 21 RETURNS VOID 22 LANGUAGE plpgsql 23 AS $$ 24 DECLARE 25 table_name TEXT default 'known_coins'; 26 BEGIN 27 PERFORM create_partitioned_table( 28 'CREATE TABLE %I' 29 '(known_coin_id BIGINT GENERATED BY DEFAULT AS IDENTITY' 30 ',denominations_serial INT8 NOT NULL' 31 ',coin_pub BYTEA NOT NULL PRIMARY KEY CHECK (LENGTH(coin_pub)=32)' 32 ',age_commitment_hash BYTEA CHECK (LENGTH(age_commitment_hash)=32)' 33 ',denom_sig BYTEA NOT NULL' 34 ',remaining taler_amount NOT NULL DEFAULT(0,0)' 35 ') %s ;' 36 ,table_name 37 ,'PARTITION BY HASH (coin_pub)' 38 ,partition_suffix 39 ); 40 PERFORM comment_partitioned_table( 41 'information about coins and their signatures, so we do not have to store the signatures more than once if a coin is involved in multiple operations' 42 ,table_name 43 ,partition_suffix 44 ); 45 PERFORM comment_partitioned_column( 46 'Denomination of the coin, determines the value of the original coin and applicable fees for coin-specific operations.' 47 ,'denominations_serial' 48 ,table_name 49 ,partition_suffix 50 ); 51 PERFORM comment_partitioned_column( 52 'EdDSA public key of the coin' 53 ,'coin_pub' 54 ,table_name 55 ,partition_suffix 56 ); 57 PERFORM comment_partitioned_column( 58 'Value of the coin that remains to be spent' 59 ,'remaining' 60 ,table_name 61 ,partition_suffix 62 ); 63 PERFORM comment_partitioned_column( 64 'Optional hash of the age commitment for age restrictions as per DD 24 (active if denom_type has the respective bit set)' 65 ,'age_commitment_hash' 66 ,table_name 67 ,partition_suffix 68 ); 69 PERFORM comment_partitioned_column( 70 'This is the signature of the exchange that affirms that the coin is a valid coin. The specific signature type depends on denom_type of the denomination.' 71 ,'denom_sig' 72 ,table_name 73 ,partition_suffix 74 ); 75 END 76 $$; 77 78 79 CREATE FUNCTION constrain_table_known_coins( 80 IN partition_suffix TEXT 81 ) 82 RETURNS void 83 LANGUAGE plpgsql 84 AS $$ 85 DECLARE 86 table_name TEXT default 'known_coins'; 87 BEGIN 88 table_name = concat_ws('_', table_name, partition_suffix); 89 EXECUTE FORMAT ( 90 'ALTER TABLE ' || table_name || 91 ' ADD CONSTRAINT ' || table_name || '_known_coin_id_key' 92 ' UNIQUE (known_coin_id)' 93 ); 94 END 95 $$; 96 97 98 CREATE FUNCTION foreign_table_known_coins() 99 RETURNS void 100 LANGUAGE plpgsql 101 AS $$ 102 DECLARE 103 table_name TEXT default 'known_coins'; 104 BEGIN 105 EXECUTE FORMAT ( 106 'ALTER TABLE ' || table_name || 107 ' ADD CONSTRAINT ' || table_name || '_foreign_denominations' 108 ' FOREIGN KEY (denominations_serial) ' 109 ' REFERENCES denominations (denominations_serial) ON DELETE CASCADE' 110 ); 111 END 112 $$; 113 114 115 INSERT INTO exchange_tables 116 (name 117 ,version 118 ,action 119 ,partitioned 120 ,by_range) 121 VALUES 122 ('known_coins' 123 ,'exchange-0002' 124 ,'create' 125 ,TRUE 126 ,FALSE), 127 ('known_coins' 128 ,'exchange-0002' 129 ,'constrain' 130 ,TRUE 131 ,FALSE), 132 ('known_coins' 133 ,'exchange-0002' 134 ,'foreign' 135 ,TRUE 136 ,FALSE);