syncdb_lookup_block_range_TR.sql (4155B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 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 sync_do_lookup_block_range; 18 CREATE FUNCTION sync_do_lookup_block_range ( 19 IN in_account_pub BYTEA, 20 IN in_start_nonce BYTEA, 21 IN in_limit INT2) 22 RETURNS SETOF sync_do_lookup_block_range_return_type 23 LANGUAGE plpgsql 24 AS $$ 25 DECLARE 26 my_first_block BYTEA; 27 my_curr_block BYTEA; 28 my_prev_block BYTEA; 29 my_curr_hash BYTEA; 30 my_curr_prev BYTEA; 31 my_curr_next BYTEA; 32 my_curr_data BYTEA; 33 my_curr_upload_sig BYTEA; 34 my_curr_old_hash BYTEA; 35 my_curr_refs_hash BYTEA; 36 cnt INT := 0; 37 BEGIN 38 39 -- Check if account exists 40 SELECT first_block 41 INTO my_first_block 42 FROM accounts 43 WHERE account_pub = in_account_pub; 44 45 IF NOT FOUND 46 THEN 47 RETURN NEXT (TRUE -- out_account_missing 48 ,FALSE 49 ,FALSE 50 ,COALESCE(in_start_nonce, 51 decode(repeat('00', 24), 'hex')) 52 ,decode(repeat('00', 64), 'hex') 53 ,decode(repeat('00', 24), 'hex') 54 ,decode(repeat('00', 24), 'hex') 55 ,decode(repeat('00', 0), 'hex') 56 ,decode(repeat('00', 64), 'hex') 57 ,decode(repeat('00', 64), 'hex') 58 ,decode(repeat('00', 64), 'hex')); 59 RETURN; 60 END IF; 61 62 -- Check if backup is empty 63 IF my_first_block IS NULL 64 THEN 65 RETURN NEXT (FALSE 66 ,FALSE 67 ,TRUE -- out_backup_empty 68 ,COALESCE(in_start_nonce, 69 decode(repeat('00', 24), 'hex')) 70 ,decode(repeat('00', 64), 'hex') 71 ,decode(repeat('00', 24), 'hex') 72 ,decode(repeat('00', 24), 'hex') 73 ,decode(repeat('00', 0), 'hex') 74 ,decode(repeat('00', 64), 'hex') 75 ,decode(repeat('00', 64), 'hex') 76 ,decode(repeat('00', 64), 'hex')); 77 RETURN; 78 END IF; 79 80 my_curr_block := COALESCE(in_start_nonce, my_first_block); 81 82 IF in_limit <= 0 83 THEN 84 RETURN; 85 END IF; 86 87 WHILE cnt < in_limit 88 AND my_curr_block IS NOT NULL 89 LOOP 90 my_prev_block := my_curr_block; 91 92 SELECT nonce 93 ,block_hash 94 ,prev_nonce 95 ,next_nonce 96 ,data_blob 97 ,upload_sig 98 ,old_hash 99 ,refs_hash 100 INTO my_curr_block 101 ,my_curr_hash 102 ,my_curr_prev 103 ,my_curr_next 104 ,my_curr_data 105 ,my_curr_upload_sig 106 ,my_curr_old_hash 107 ,my_curr_refs_hash 108 FROM blocks 109 WHERE account_pub = in_account_pub 110 AND nonce = my_curr_block; 111 112 IF NOT FOUND 113 THEN 114 RETURN NEXT (FALSE 115 ,TRUE -- out_block_missing 116 ,FALSE 117 ,my_prev_block 118 ,decode(repeat('00', 64), 'hex') 119 ,decode(repeat('00', 24), 'hex') 120 ,decode(repeat('00', 24), 'hex') 121 ,decode(repeat('00', 0), 'hex') 122 ,decode(repeat('00', 64), 'hex') 123 ,decode(repeat('00', 64), 'hex') 124 ,decode(repeat('00', 64), 'hex')); 125 RETURN; 126 END IF; 127 128 RETURN NEXT (FALSE, FALSE, FALSE 129 ,my_curr_block 130 ,my_curr_hash 131 ,my_curr_prev 132 ,my_curr_next 133 ,my_curr_data 134 ,my_curr_upload_sig 135 ,my_curr_old_hash 136 ,my_curr_refs_hash); 137 cnt := cnt + 1; 138 my_curr_block := my_curr_next; 139 END LOOP; 140 141 RETURN; 142 END $$;