do_refresh.sql (4951B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 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 -- @author Özgür Kesim 17 18 DROP FUNCTION IF EXISTS exchange_do_refresh; 19 20 CREATE FUNCTION exchange_do_refresh( 21 IN in_rc BYTEA, 22 IN in_now INT8, 23 IN in_refresh_seed BYTEA, 24 IN in_transfer_pubs BYTEA[], 25 IN in_planchets_h BYTEA, 26 IN in_amount_with_fee taler_amount, 27 IN in_blinding_seed BYTEA, 28 IN in_cs_r_values BYTEA[], 29 IN in_cs_r_choices INT8, 30 IN in_selected_h BYTEA, 31 IN in_denom_sigs BYTEA[], 32 IN in_denom_serials INT8[], 33 IN in_old_coin_pub BYTEA, 34 IN in_old_coin_sig BYTEA, 35 IN in_noreveal_index INT4, 36 IN in_zombie_required BOOLEAN, 37 IN in_rc_version INT4, 38 OUT out_coin_found BOOLEAN, 39 OUT out_balance_ok BOOLEAN, 40 OUT out_zombie_bad BOOLEAN, 41 OUT out_nonce_reuse BOOLEAN, 42 OUT out_idempotent BOOLEAN, 43 OUT out_noreveal_index INT4, 44 OUT out_coin_balance taler_amount) 45 LANGUAGE plpgsql 46 AS $$ 47 DECLARE 48 known_coin RECORD; 49 difference RECORD; 50 BEGIN 51 -- Shards: INSERT refresh (by rc) 52 -- (rare:) SELECT refresh (by old_coin_pub) -- crosses shards! 53 -- (rare:) SELECT refresh_revealed_coins (by refresh_id) 54 -- (rare:) PERFORM recoup_refresh (by rrc_serial) -- crosses shards! 55 -- UPDATE known_coins (by coin_pub) 56 57 -- First, find old coin 58 SELECT known_coin_id 59 ,remaining 60 INTO known_coin 61 FROM known_coins 62 WHERE coin_pub = in_old_coin_pub; 63 64 IF NOT FOUND 65 THEN 66 out_coin_found = FALSE; 67 out_balance_ok = TRUE; 68 out_zombie_bad = FALSE; 69 out_nonce_reuse = FALSE; 70 out_idempotent = FALSE; 71 out_noreveal_index = -1 ; 72 out_coin_balance.val = 0; 73 out_coin_balance.frac = 0; 74 RETURN; 75 END IF; 76 77 out_coin_found = TRUE; 78 out_coin_balance = known_coin.remaining; 79 80 -- Next, check for idempotency 81 SELECT noreveal_index 82 INTO out_noreveal_index 83 FROM exchange.refresh 84 WHERE rc=in_rc; 85 out_idempotent = FOUND; 86 87 IF out_idempotent 88 THEN 89 -- out_idempotent is set 90 -- out_noreveal_index is set 91 -- out_coin_found is set 92 -- out_coin_balance is set 93 out_balance_ok = TRUE; 94 out_zombie_bad = FALSE; -- zombie is OK 95 out_nonce_reuse = FALSE; 96 RETURN; 97 END IF; 98 99 out_idempotent = FALSE; 100 out_noreveal_index = in_noreveal_index; 101 102 -- Ensure the uniqueness of the blinding_seed 103 IF in_blinding_seed IS NOT NULL 104 THEN 105 INSERT INTO unique_refresh_blinding_seed 106 (blinding_seed) 107 VALUES 108 (in_blinding_seed) 109 ON CONFLICT DO NOTHING; 110 111 IF NOT FOUND 112 THEN 113 out_nonce_reuse = TRUE; 114 out_balance_ok = TRUE; 115 out_zombie_bad = FALSE; -- zombie is OK 116 RETURN; 117 END IF; 118 END IF; 119 120 out_nonce_reuse = FALSE; 121 122 INSERT INTO exchange.refresh 123 (rc 124 ,execution_date 125 ,old_coin_pub 126 ,old_coin_sig 127 ,planchets_h 128 ,transfer_pubs 129 ,amount_with_fee 130 ,noreveal_index 131 ,refresh_seed 132 ,blinding_seed 133 ,cs_r_values 134 ,cs_r_choices 135 ,selected_h 136 ,denom_sigs 137 ,denom_serials 138 ,rc_version 139 ) 140 VALUES 141 (in_rc 142 ,in_now 143 ,in_old_coin_pub 144 ,in_old_coin_sig 145 ,in_planchets_h 146 ,in_transfer_pubs 147 ,in_amount_with_fee 148 ,in_noreveal_index 149 ,in_refresh_seed 150 ,in_blinding_seed 151 ,in_cs_r_values 152 ,in_cs_r_choices 153 ,in_selected_h 154 ,in_denom_sigs 155 ,in_denom_serials 156 ,in_rc_version 157 ) 158 ON CONFLICT DO NOTHING; 159 160 IF NOT FOUND 161 THEN 162 RAISE EXCEPTION 'Conflict in refresh despite idempotency check for rc(%)!', rc; 163 RETURN; 164 END IF; 165 166 IF in_zombie_required 167 THEN 168 -- Check if this coin was part of a refresh 169 -- operation that was subsequently involved 170 -- in a recoup operation. We begin by all 171 -- refresh operations our coin was involved 172 -- with, then find all associated reveal 173 -- operations, and then see if any of these 174 -- reveal operations was involved in a recoup. 175 PERFORM 176 FROM recoup_refresh 177 WHERE refresh_id IN 178 (SELECT refresh_id 179 FROM refresh 180 WHERE old_coin_pub=in_old_coin_pub); 181 IF NOT FOUND 182 THEN 183 out_zombie_bad=TRUE; 184 out_balance_ok=FALSE; 185 RETURN; 186 END IF; 187 END IF; 188 189 out_zombie_bad=FALSE; -- zombie is OK 190 191 -- Check coin balance is sufficient. 192 SELECT * 193 INTO difference 194 FROM amount_left_minus_right(out_coin_balance 195 ,in_amount_with_fee); 196 197 out_balance_ok = difference.ok; 198 199 IF NOT out_balance_ok 200 THEN 201 RETURN; 202 END IF; 203 204 out_coin_balance = difference.diff; 205 206 -- Check and update balance of the coin. 207 UPDATE known_coins 208 SET 209 remaining = out_coin_balance 210 WHERE 211 known_coin_id = known_coin.known_coin_id; 212 213 END $$;