diff options
Diffstat (limited to 'src')
28 files changed, 408 insertions, 492 deletions
diff --git a/src/exchangedb/0002-reserves.sql b/src/exchangedb/0002-reserves.sql index 3d345afd5..3e836133b 100644 --- a/src/exchangedb/0002-reserves.sql +++ b/src/exchangedb/0002-reserves.sql @@ -27,8 +27,7 @@ BEGIN 'CREATE TABLE %I' '(reserve_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY' ',reserve_pub BYTEA PRIMARY KEY CHECK(LENGTH(reserve_pub)=32)' - ',current_balance_val INT8 NOT NULL DEFAULT(0)' - ',current_balance_frac INT4 NOT NULL DEFAULT(0)' + ',current_balance taler_amount NOT NULL DEFAULT (0, 0)' ',purses_active INT8 NOT NULL DEFAULT(0)' ',purses_allowed INT8 NOT NULL DEFAULT(0)' ',birthday INT4 NOT NULL DEFAULT(0)' @@ -52,7 +51,7 @@ BEGIN ); PERFORM comment_partitioned_column( 'Current balance remaining with the reserve.' - ,'current_balance_val' + ,'current_balance' ,table_name ,partition_suffix ); @@ -109,8 +108,7 @@ BEGIN 'CREATE INDEX ' || table_name || '_by_expiration_index ' 'ON ' || table_name || ' ' '(expiration_date' - ',current_balance_val' - ',current_balance_frac' + ',current_balance' ');' ); EXECUTE FORMAT ( diff --git a/src/exchangedb/exchange_do_age_withdraw.sql b/src/exchangedb/exchange_do_age_withdraw.sql index ce257a8d9..184a3e49a 100644 --- a/src/exchangedb/exchange_do_age_withdraw.sql +++ b/src/exchangedb/exchange_do_age_withdraw.sql @@ -36,10 +36,9 @@ CREATE OR REPLACE FUNCTION exchange_do_age_withdraw( LANGUAGE plpgsql AS $$ DECLARE - reserve_gc INT8; + reserve RECORD; difference RECORD; balance taler_amount; - new_balance taler_amount; not_before date; earliest_date date; BEGIN @@ -49,16 +48,8 @@ BEGIN -- reserves_in by reserve_pub (SELECT) -- wire_targets by wire_target_h_payto -SELECT - current_balance_val - ,current_balance_frac - ,gc_date - ,birthday - INTO - balance.val - ,balance.frac - ,reserve_gc - ,reserve_birthday +SELECT * + INTO reserve FROM exchange.reserves WHERE reserves.reserve_pub=rpub; @@ -75,10 +66,13 @@ END IF; reserve_found = TRUE; conflict=FALSE; -- not really yet determined +balance = reserve.current_balance; +reserve_birthday = reserve.birthday; + -- Check age requirements -IF (reserve_birthday <> 0) +IF (reserve.birthday <> 0) THEN - not_before=date '1970-01-01' + reserve_birthday; + not_before=date '1970-01-01' + reserve.birthday; earliest_date = current_date - make_interval(maximum_age_committed); -- -- 1970-01-01 + birthday == not_before now @@ -104,12 +98,9 @@ required_age=0; -- Check reserve balance is sufficient. SELECT * -INTO - difference -FROM - amount_left_minus_right( - balance - ,amount_with_fee); +INTO difference +FROM amount_left_minus_right(balance + ,amount_with_fee); balance_ok = difference.ok; @@ -118,16 +109,15 @@ THEN RETURN; END IF; -new_balance = difference.diff; +balance = difference.diff; -- Calculate new expiration dates. -min_reserve_gc=GREATEST(min_reserve_gc,reserve_gc); +min_reserve_gc=GREATEST(min_reserve_gc,reserve.gc_date); -- Update reserve balance. UPDATE reserves SET gc_date=min_reserve_gc - ,current_balance_val=new_balance.val - ,current_balance_frac=new_balance.frac + ,current_balance=balance WHERE reserves.reserve_pub=rpub; diff --git a/src/exchangedb/exchange_do_batch_reserves_update.sql b/src/exchangedb/exchange_do_batch_reserves_update.sql index 39920629b..da7f4508b 100644 --- a/src/exchangedb/exchange_do_batch_reserves_update.sql +++ b/src/exchangedb/exchange_do_batch_reserves_update.sql @@ -18,8 +18,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch_reserves_update( IN in_reserve_pub BYTEA, IN in_expiration_date INT8, IN in_wire_ref INT8, - IN in_credit_val INT8, - IN in_credit_frac INT4, + IN in_credit taler_amount, IN in_exchange_account_name VARCHAR, IN in_wire_source_h_payto BYTEA, IN in_notify text, @@ -38,8 +37,8 @@ BEGIN VALUES (in_reserve_pub ,in_wire_ref - ,in_credit_val - ,in_credit_frac + ,in_credit.val + ,in_credit.frac ,in_exchange_account_name ,in_wire_source_h_payto ,in_expiration_date) @@ -50,15 +49,15 @@ BEGIN out_duplicate = FALSE; UPDATE reserves SET - current_balance_frac = current_balance_frac+in_credit_frac + current_balance.frac = current_balance.frac+in_credit.frac - CASE - WHEN current_balance_frac + in_credit_frac >= 100000000 + WHEN current_balance.frac + in_credit.frac >= 100000000 THEN 100000000 ELSE 1 END - ,current_balance_val = current_balance_val+in_credit_val + ,current_balance.val = current_balance.val+in_credit.val + CASE - WHEN current_balance_frac + in_credit_frac >= 100000000 + WHEN current_balance.frac + in_credit.frac >= 100000000 THEN 1 ELSE 0 END diff --git a/src/exchangedb/exchange_do_batch_withdraw.sql b/src/exchangedb/exchange_do_batch_withdraw.sql index 53e908444..b896e04f2 100644 --- a/src/exchangedb/exchange_do_batch_withdraw.sql +++ b/src/exchangedb/exchange_do_batch_withdraw.sql @@ -17,8 +17,7 @@ -- @author Özgür Kesim CREATE OR REPLACE FUNCTION exchange_do_batch_withdraw( - IN amount_val INT8, - IN amount_frac INT4, + IN amount taler_amount, IN rpub BYTEA, IN now INT8, IN min_reserve_gc INT8, @@ -31,10 +30,8 @@ CREATE OR REPLACE FUNCTION exchange_do_batch_withdraw( LANGUAGE plpgsql AS $$ DECLARE - reserve_gc INT8; - reserve_val INT8; - reserve_frac INT4; - reserve_birthday INT4; + reserve RECORD; + balance taler_amount; not_before date; BEGIN -- Shards: reserves by reserve_pub (SELECT) @@ -44,18 +41,8 @@ BEGIN -- wire_targets by wire_target_h_payto -SELECT - current_balance_val - ,current_balance_frac - ,gc_date - ,birthday - ,reserve_uuid - INTO - reserve_val - ,reserve_frac - ,reserve_gc - ,reserve_birthday - ,ruuid +SELECT * + INTO reserve FROM exchange.reserves WHERE reserves.reserve_pub=rpub; @@ -70,9 +57,10 @@ THEN RETURN; END IF; +ruuid = reserve.reserve_uuid; -- Check if age requirements are present -IF ((NOT do_age_check) OR (reserve_birthday = 0)) +IF ((NOT do_age_check) OR (reserve.birthday = 0)) THEN age_ok = TRUE; allowed_maximum_age = -1; @@ -82,7 +70,7 @@ ELSE -- birthday set (reserve_birthday != 0), but the client called the -- batch-withdraw endpoint instead of the age-withdraw endpoint, which it -- should have. - not_before=date '1970-01-01' + reserve_birthday; + not_before=date '1970-01-01' + reserve.birthday; allowed_maximum_age = extract(year from age(current_date, not_before)); reserve_found=TRUE; @@ -91,22 +79,24 @@ ELSE RETURN; END IF; +balance = reserve.current_balance; + -- Check reserve balance is sufficient. -IF (reserve_val > amount_val) +IF (balance.val > amount.val) THEN - IF (reserve_frac >= amount_frac) + IF (balance.frac >= amount.frac) THEN - reserve_val=reserve_val - amount_val; - reserve_frac=reserve_frac - amount_frac; + balance.val=balance.val - amount.val; + balance.frac=balance.frac - amount.frac; ELSE - reserve_val=reserve_val - amount_val - 1; - reserve_frac=reserve_frac + 100000000 - amount_frac; + balance.val=balance.val - amount.val - 1; + balance.frac=balance.frac + 100000000 - amount.frac; END IF; ELSE - IF (reserve_val = amount_val) AND (reserve_frac >= amount_frac) + IF (balance.val = amount.val) AND (balance.frac >= amount.frac) THEN - reserve_val=0; - reserve_frac=reserve_frac - amount_frac; + balance.val=0; + balance.frac=balance.frac - amount.frac; ELSE balance_ok=FALSE; RETURN; @@ -114,13 +104,12 @@ ELSE END IF; -- Calculate new expiration dates. -min_reserve_gc=GREATEST(min_reserve_gc,reserve_gc); +min_reserve_gc=GREATEST(min_reserve_gc,reserve.gc_date); -- Update reserve balance. UPDATE reserves SET gc_date=min_reserve_gc - ,current_balance_val=reserve_val - ,current_balance_frac=reserve_frac + ,current_balance=balance WHERE reserves.reserve_pub=rpub; @@ -129,6 +118,6 @@ balance_ok=TRUE; END $$; -COMMENT ON FUNCTION exchange_do_batch_withdraw(INT8, INT4, BYTEA, INT8, INT8, BOOLEAN) +COMMENT ON FUNCTION exchange_do_batch_withdraw(taler_amount, BYTEA, INT8, INT8, BOOLEAN) IS 'Checks whether the reserve has sufficient balance for a withdraw operation (or the request is repeated and was previously approved) and that age requirements are formally met. If so updates the database with the result. Excludes storing the planchets.'; diff --git a/src/exchangedb/exchange_do_batch_withdraw_insert.sql b/src/exchangedb/exchange_do_batch_withdraw_insert.sql index b580f6fb7..fd00754ad 100644 --- a/src/exchangedb/exchange_do_batch_withdraw_insert.sql +++ b/src/exchangedb/exchange_do_batch_withdraw_insert.sql @@ -16,8 +16,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch_withdraw_insert( IN cs_nonce BYTEA, - IN amount_val INT8, - IN amount_frac INT4, + IN amount taler_amount, IN h_denom_pub BYTEA, -- FIXME: denom_serials should really be a parameter to this FUNCTION. IN ruuid INT8, IN reserve_sig BYTEA, @@ -74,8 +73,8 @@ VALUES ,ruuid ,reserve_sig ,now - ,amount_val - ,amount_frac) + ,amount.val + ,amount.frac) ON CONFLICT DO NOTHING; IF NOT FOUND @@ -119,6 +118,6 @@ END IF; END $$; -COMMENT ON FUNCTION exchange_do_batch_withdraw_insert(BYTEA, INT8, INT4, BYTEA, INT8, BYTEA, BYTEA, BYTEA, INT8) +COMMENT ON FUNCTION exchange_do_batch_withdraw_insert(BYTEA, taler_amount, BYTEA, INT8, BYTEA, BYTEA, BYTEA, INT8) IS 'Stores information about a planchet for a batch withdraw operation. Checks if the planchet already exists, and in that case indicates a conflict'; diff --git a/src/exchangedb/exchange_do_gc.sql b/src/exchangedb/exchange_do_gc.sql index c6331c18e..75dd120d7 100644 --- a/src/exchangedb/exchange_do_gc.sql +++ b/src/exchangedb/exchange_do_gc.sql @@ -21,15 +21,10 @@ LANGUAGE plpgsql AS $$ DECLARE reserve_uuid_min INT8; -- minimum reserve UUID still alive -DECLARE melt_min INT8; -- minimum melt still alive -DECLARE coin_min INT8; -- minimum known_coin still alive -DECLARE deposit_min INT8; -- minimum deposit still alive -DECLARE reserve_out_min INT8; -- minimum reserve_out still alive -DECLARE denom_min INT8; -- minimum denomination still alive BEGIN @@ -42,8 +37,7 @@ DELETE FROM exchange.wire_fee -- TODO: use closing fee as threshold? DELETE FROM exchange.reserves WHERE gc_date < in_now - AND current_balance_val = 0 - AND current_balance_frac = 0; + AND current_balance = (0, 0); SELECT reserve_out_serial_id diff --git a/src/exchangedb/exchange_do_history_request.sql b/src/exchangedb/exchange_do_history_request.sql index 2f6041741..5cd15bc62 100644 --- a/src/exchangedb/exchange_do_history_request.sql +++ b/src/exchangedb/exchange_do_history_request.sql @@ -19,12 +19,15 @@ CREATE OR REPLACE FUNCTION exchange_do_history_request( IN in_reserve_pub BYTEA, IN in_reserve_sig BYTEA, IN in_request_timestamp INT8, - IN in_history_fee_val INT8, - IN in_history_fee_frac INT4, + IN in_history_fee taler_amount, OUT out_balance_ok BOOLEAN, OUT out_idempotent BOOLEAN) LANGUAGE plpgsql AS $$ +DECLARE + reserve RECORD; + balance taler_amount; + new_balance taler_amount; BEGIN -- Insert and check for idempotency. @@ -38,8 +41,8 @@ BEGIN (in_reserve_pub ,in_request_timestamp ,in_reserve_sig - ,in_history_fee_val - ,in_history_fee_frac) + ,in_history_fee.val + ,in_history_fee.frac) ON CONFLICT DO NOTHING; IF NOT FOUND @@ -51,35 +54,51 @@ BEGIN out_idempotent=FALSE; - -- Update reserve balance. - UPDATE exchange.reserves - SET - current_balance_frac=current_balance_frac-in_history_fee_frac - + CASE - WHEN current_balance_frac < in_history_fee_frac - THEN 100000000 - ELSE 0 - END, - current_balance_val=current_balance_val-in_history_fee_val - - CASE - WHEN current_balance_frac < in_history_fee_frac - THEN 1 - ELSE 0 - END - WHERE - reserve_pub=in_reserve_pub - AND ( (current_balance_val > in_history_fee_val) OR - ( (current_balance_frac >= in_history_fee_frac) AND - (current_balance_val >= in_history_fee_val) ) ); + SELECT * + INTO reserve + FROM exchange.reserves + WHERE reserve_pub=in_reserve_pub; IF NOT FOUND THEN - -- Either reserve does not exist, or balance insufficient. - -- Both we treat the same here as balance insufficient. + -- Reserve does not exist, we treat it the same here + -- as balance insufficient. + out_balance_ok=FALSE; + RETURN; + END IF; + + balance = reserve.current_balance; + + -- check balance + IF ( (balance.val <= in_history_fee.val) AND + ( (balance.frac < in_history_fee.frac) OR + (balance.val < in_history_fee.val) ) ) + THEN out_balance_ok=FALSE; RETURN; END IF; + new_balance.frac=balance.frac-in_history_fee.frac + + CASE + WHEN balance.frac < in_history_fee.frac + THEN 100000000 + ELSE 0 + END; + new_balance.val=balance.val-in_history_fee.val + - CASE + WHEN balance.frac < in_history_fee.frac + THEN 1 + ELSE 0 + END; + + -- Update reserve balance. + UPDATE exchange.reserves + SET current_balance=new_balance + WHERE reserve_pub=in_reserve_pub; + + ASSERT FOUND, 'reserve suddenly disappeared'; + out_balance_ok=TRUE; + END $$; diff --git a/src/exchangedb/exchange_do_purse_deposit.sql b/src/exchangedb/exchange_do_purse_deposit.sql index d710e9002..45bdcdf68 100644 --- a/src/exchangedb/exchange_do_purse_deposit.sql +++ b/src/exchangedb/exchange_do_purse_deposit.sql @@ -17,12 +17,10 @@ CREATE OR REPLACE FUNCTION exchange_do_purse_deposit( IN in_partner_id INT8, IN in_purse_pub BYTEA, - IN in_amount_with_fee_val INT8, - IN in_amount_with_fee_frac INT4, + IN in_amount_with_fee taler_amount, IN in_coin_pub BYTEA, IN in_coin_sig BYTEA, - IN in_amount_without_fee_val INT8, - IN in_amount_without_fee_frac INT4, + IN in_amount_without_fee taler_amount, IN in_reserve_expiration INT8, IN in_now INT8, OUT out_balance_ok BOOLEAN, @@ -32,17 +30,10 @@ LANGUAGE plpgsql AS $$ DECLARE was_merged BOOLEAN; -DECLARE psi INT8; -- partner's serial ID (set if merged) -DECLARE - my_amount_val INT8; -- total in purse -DECLARE - my_amount_frac INT4; -- total in purse -DECLARE + my_amount taler_amount; -- total in purse was_paid BOOLEAN; -DECLARE my_in_reserve_quota BOOLEAN; -DECLARE my_reserve_pub BYTEA; BEGIN @@ -58,8 +49,8 @@ INSERT INTO exchange.purse_deposits (in_partner_id ,in_purse_pub ,in_coin_pub - ,in_amount_with_fee_val - ,in_amount_with_fee_frac + ,in_amount_with_fee.val + ,in_amount_with_fee.frac ,in_coin_sig) ON CONFLICT DO NOTHING; @@ -107,22 +98,22 @@ END IF; -- Check and update balance of the coin. UPDATE known_coins SET - remaining_frac=remaining_frac-in_amount_with_fee_frac + remaining_frac=remaining_frac-in_amount_with_fee.frac + CASE - WHEN remaining_frac < in_amount_with_fee_frac + WHEN remaining_frac < in_amount_with_fee.frac THEN 100000000 ELSE 0 END, - remaining_val=remaining_val-in_amount_with_fee_val + remaining_val=remaining_val-in_amount_with_fee.val - CASE - WHEN remaining_frac < in_amount_with_fee_frac + WHEN remaining_frac < in_amount_with_fee.frac THEN 1 ELSE 0 END WHERE coin_pub=in_coin_pub - AND ( (remaining_val > in_amount_with_fee_val) OR - ( (remaining_frac >= in_amount_with_fee_frac) AND - (remaining_val >= in_amount_with_fee_val) ) ); + AND ( (remaining_val > in_amount_with_fee.val) OR + ( (remaining_frac >= in_amount_with_fee.frac) AND + (remaining_val >= in_amount_with_fee.val) ) ); IF NOT FOUND THEN @@ -137,15 +128,15 @@ END IF; -- Credit the purse. UPDATE purse_requests SET - balance_frac=balance_frac+in_amount_without_fee_frac + balance_frac=balance_frac+in_amount_without_fee.frac - CASE - WHEN balance_frac+in_amount_without_fee_frac >= 100000000 + WHEN balance_frac+in_amount_without_fee.frac >= 100000000 THEN 100000000 ELSE 0 END, - balance_val=balance_val+in_amount_without_fee_val + balance_val=balance_val+in_amount_without_fee.val + CASE - WHEN balance_frac+in_amount_without_fee_frac >= 100000000 + WHEN balance_frac+in_amount_without_fee.frac >= 100000000 THEN 1 ELSE 0 END @@ -174,8 +165,8 @@ SELECT ,amount_with_fee_frac ,in_reserve_quota INTO - my_amount_val - ,my_amount_frac + my_amount.val + ,my_amount.frac ,my_in_reserve_quota FROM exchange.purse_requests WHERE (purse_pub=in_purse_pub) @@ -231,14 +222,12 @@ ELSE -- This is a local reserve, update balance immediately. INSERT INTO reserves (reserve_pub - ,current_balance_frac - ,current_balance_val + ,current_balance ,expiration_date ,gc_date) VALUES (my_reserve_pub - ,my_amount_frac - ,my_amount_val + ,my_amount ,in_reserve_expiration ,in_reserve_expiration) ON CONFLICT DO NOTHING; @@ -248,15 +237,15 @@ ELSE -- Reserve existed, thus UPDATE instead of INSERT. UPDATE reserves SET - current_balance_frac=current_balance_frac+my_amount_frac + current_balance.frac=(current_balance).frac+my_amount.frac - CASE - WHEN current_balance_frac + my_amount_frac >= 100000000 + WHEN (current_balance).frac + my_amount.frac >= 100000000 THEN 100000000 ELSE 0 END - ,current_balance_val=current_balance_val+my_amount_val + ,current_balance.val=(current_balance).val+my_amount.val + CASE - WHEN current_balance_frac + my_amount_frac >= 100000000 + WHEN (current_balance).frac + my_amount.frac >= 100000000 THEN 1 ELSE 0 END diff --git a/src/exchangedb/exchange_do_purse_merge.sql b/src/exchangedb/exchange_do_purse_merge.sql index f02dd5dcd..9780c016c 100644 --- a/src/exchangedb/exchange_do_purse_merge.sql +++ b/src/exchangedb/exchange_do_purse_merge.sql @@ -29,17 +29,12 @@ CREATE OR REPLACE FUNCTION exchange_do_purse_merge( LANGUAGE plpgsql AS $$ DECLARE - my_amount_val INT8; -DECLARE - my_amount_frac INT4; -DECLARE - my_purse_fee_val INT8; -DECLARE - my_purse_fee_frac INT4; -DECLARE + my_amount taler_amount; + my_purse_fee taler_amount; my_partner_serial_id INT8; -DECLARE my_in_reserve_quota BOOLEAN; + reserve RECORD; + balance taler_amount; BEGIN @@ -72,10 +67,10 @@ SELECT amount_with_fee_val ,purse_fee_val ,purse_fee_frac ,in_reserve_quota - INTO my_amount_val - ,my_amount_frac - ,my_purse_fee_val - ,my_purse_fee_frac + INTO my_amount.val + ,my_amount.frac + ,my_purse_fee.val + ,my_purse_fee.frac ,my_in_reserve_quota FROM exchange.purse_requests WHERE purse_pub=in_purse_pub @@ -196,26 +191,33 @@ ELSE -- This is a local reserve, update reserve balance immediately. -- Refund the purse fee, by adding it to the purse value: - my_amount_val = my_amount_val + my_purse_fee_val; - my_amount_frac = my_amount_frac + my_purse_fee_frac; + my_amount.val = my_amount.val + my_purse_fee.val; + my_amount.frac = my_amount.frac + my_purse_fee.frac; -- normalize result - my_amount_val = my_amount_val + my_amount_frac / 100000000; - my_amount_frac = my_amount_frac % 100000000; + my_amount.val = my_amount.val + my_amount.frac / 100000000; + my_amount.frac = my_amount.frac % 100000000; + + SELECT * + INTO reserve + FROM exchange.reserves + WHERE reserve_pub=in_reserve_pub; + + balance = reserve.current_balance; + balance.frac=balance.frac+my_amount.frac + - CASE + WHEN balance.frac + my_amount.frac >= 100000000 + THEN 100000000 + ELSE 0 + END; + balance.val=balance.val+my_amount.val + + CASE + WHEN balance.frac + my_amount.frac >= 100000000 + THEN 1 + ELSE 0 + END; UPDATE exchange.reserves - SET - current_balance_frac=current_balance_frac+my_amount_frac - - CASE - WHEN current_balance_frac + my_amount_frac >= 100000000 - THEN 100000000 - ELSE 0 - END, - current_balance_val=current_balance_val+my_amount_val - + CASE - WHEN current_balance_frac + my_amount_frac >= 100000000 - THEN 1 - ELSE 0 - END + SET current_balance=balance WHERE reserve_pub=in_reserve_pub; END IF; diff --git a/src/exchangedb/exchange_do_recoup_to_reserve.sql b/src/exchangedb/exchange_do_recoup_to_reserve.sql index 39baba8fa..71c1d51b9 100644 --- a/src/exchangedb/exchange_do_recoup_to_reserve.sql +++ b/src/exchangedb/exchange_do_recoup_to_reserve.sql @@ -31,9 +31,10 @@ CREATE OR REPLACE FUNCTION exchange_do_recoup_to_reserve( LANGUAGE plpgsql AS $$ DECLARE - tmp_val INT8; -- amount recouped -DECLARE - tmp_frac INT8; -- amount recouped + tmp taler_amount; -- amount recouped + balance taler_amount; -- current balance of the reserve + new_balance taler_amount; -- new balance of the reserve + reserve RECORD; BEGIN -- Shards: SELECT known_coins (by coin_pub) -- SELECT recoup (by coin_pub) @@ -49,8 +50,8 @@ SELECT remaining_frac ,remaining_val INTO - tmp_frac - ,tmp_val + tmp.frac + ,tmp.val FROM exchange.known_coins WHERE coin_pub=in_coin_pub; @@ -61,7 +62,7 @@ THEN RETURN; END IF; -IF tmp_val + tmp_frac = 0 +IF tmp.val + tmp.frac = 0 THEN -- Check for idempotency SELECT @@ -83,22 +84,31 @@ UPDATE known_coins ,remaining_val=0 WHERE coin_pub=in_coin_pub; +-- Get current balance +SELECT * + INTO reserve + FROM reserves + WHERE reserve_pub=in_reserve_pub; + +balance = reserve.current_balance; +new_balance.frac=balance.frac+tmp.frac + - CASE + WHEN balance.frac+tmp.frac >= 100000000 + THEN 100000000 + ELSE 0 + END; + +new_balance.val=balance.val+tmp.val + + CASE + WHEN balance.frac+tmp.frac >= 100000000 + THEN 1 + ELSE 0 + END; -- Credit the reserve and update reserve timers. UPDATE reserves SET - current_balance_frac=current_balance_frac+tmp_frac - - CASE - WHEN current_balance_frac+tmp_frac >= 100000000 - THEN 100000000 - ELSE 0 - END, - current_balance_val=current_balance_val+tmp_val - + CASE - WHEN current_balance_frac+tmp_frac >= 100000000 - THEN 1 - ELSE 0 - END, + current_balance = new_balance, gc_date=GREATEST(gc_date, in_reserve_gc), expiration_date=GREATEST(expiration_date, in_reserve_expiration) WHERE reserve_pub=in_reserve_pub; @@ -126,8 +136,8 @@ VALUES (in_coin_pub ,in_coin_sig ,in_coin_blind - ,tmp_val - ,tmp_frac + ,tmp.val + ,tmp.frac ,in_recoup_timestamp ,in_reserve_out_serial_id); diff --git a/src/exchangedb/exchange_do_reserve_open.sql b/src/exchangedb/exchange_do_reserve_open.sql index 5e80f713f..7aca78b8c 100644 --- a/src/exchangedb/exchange_do_reserve_open.sql +++ b/src/exchangedb/exchange_do_reserve_open.sql @@ -16,62 +16,36 @@ CREATE OR REPLACE FUNCTION exchange_do_reserve_open( IN in_reserve_pub BYTEA, - IN in_total_paid_val INT8, - IN in_total_paid_frac INT4, - IN in_reserve_payment_val INT8, - IN in_reserve_payment_frac INT4, + IN in_total_paid taler_amount, + IN in_reserve_payment taler_amount, IN in_min_purse_limit INT4, IN in_default_purse_limit INT4, IN in_reserve_sig BYTEA, IN in_desired_expiration INT8, IN in_reserve_gc_delay INT8, IN in_now INT8, - IN in_open_fee_val INT8, - IN in_open_fee_frac INT4, - OUT out_open_cost_val INT8, - OUT out_open_cost_frac INT4, + IN in_open_fee taler_amount, + OUT out_open_cost taler_amount, OUT out_final_expiration INT8, OUT out_no_funds BOOLEAN) LANGUAGE plpgsql AS $$ DECLARE - my_balance_val INT8; -DECLARE - my_balance_frac INT4; -DECLARE - my_cost_val INT8; -DECLARE + my_balance taler_amount; + my_cost taler_amount; my_cost_tmp INT8; -DECLARE - my_cost_frac INT4; -DECLARE my_years_tmp INT4; -DECLARE my_years INT4; -DECLARE my_needs_update BOOL; -DECLARE - my_purses_allowed INT8; -DECLARE my_expiration_date INT8; -DECLARE - my_reserve_expiration INT8; + reserve RECORD; BEGIN -- FIXME: use SELECT FOR UPDATE? -SELECT - purses_allowed - ,expiration_date - ,current_balance_val - ,current_balance_frac -INTO - my_purses_allowed - ,my_reserve_expiration - ,my_balance_val - ,my_balance_frac -FROM reserves -WHERE - reserve_pub=in_reserve_pub; +SELECT * + INTO reserve + FROM reserves + WHERE reserve_pub=in_reserve_pub; IF NOT FOUND THEN @@ -80,16 +54,18 @@ THEN RETURN; END IF; +my_balance = reserve.current_balance; + -- Do not allow expiration time to start in the past already -IF (my_reserve_expiration < in_now) +IF (reserve.expiration_date < in_now) THEN my_expiration_date = in_now; ELSE - my_expiration_date = my_reserve_expiration; + my_expiration_date = reserve.expiration_date; END IF; -my_cost_val = 0; -my_cost_frac = 0; +my_cost.val = 0; +my_cost.frac = 0; my_needs_update = FALSE; my_years = 0; @@ -97,62 +73,62 @@ my_years = 0; IF (my_expiration_date < in_desired_expiration) THEN my_years = (31535999999999 + in_desired_expiration - my_expiration_date) / 31536000000000; - my_purses_allowed = in_default_purse_limit; + reserve.purses_allowed = in_default_purse_limit; my_expiration_date = my_expiration_date + 31536000000000 * my_years; END IF; -- Increase years based on purses requested -IF (my_purses_allowed < in_min_purse_limit) +IF (reserve.purses_allowed < in_min_purse_limit) THEN my_years = (31535999999999 + in_desired_expiration - in_now) / 31536000000000; my_expiration_date = in_now + 31536000000000 * my_years; - my_years_tmp = (in_min_purse_limit + in_default_purse_limit - my_purses_allowed - 1) / in_default_purse_limit; + my_years_tmp = (in_min_purse_limit + in_default_purse_limit - reserve.purses_allowed - 1) / in_default_purse_limit; my_years = my_years + my_years_tmp; - my_purses_allowed = my_purses_allowed + (in_default_purse_limit * my_years_tmp); + reserve.purses_allowed = reserve.purses_allowed + (in_default_purse_limit * my_years_tmp); END IF; -- Compute cost based on annual fees IF (my_years > 0) THEN - my_cost_val = my_years * in_open_fee_val; - my_cost_tmp = my_years * in_open_fee_frac / 100000000; - IF (CAST (my_cost_val + my_cost_tmp AS INT8) < my_cost_val) + my_cost.val = my_years * in_open_fee.val; + my_cost_tmp = my_years * in_open_fee.frac / 100000000; + IF (CAST (my_cost.val + my_cost_tmp AS INT8) < my_cost.val) THEN - out_open_cost_val=9223372036854775807; - out_open_cost_frac=2147483647; + out_open_cost.val=9223372036854775807; + out_open_cost.frac=2147483647; out_final_expiration=my_expiration_date; out_no_funds=FALSE; RAISE NOTICE 'arithmetic issue computing amount'; RETURN; END IF; - my_cost_val = CAST (my_cost_val + my_cost_tmp AS INT8); - my_cost_frac = my_years * in_open_fee_frac % 100000000; + my_cost.val = CAST (my_cost.val + my_cost_tmp AS INT8); + my_cost.frac = my_years * in_open_fee.frac % 100000000; my_needs_update = TRUE; END IF; -- check if we actually have something to do IF NOT my_needs_update THEN - out_final_expiration = my_reserve_expiration; - out_open_cost_val = 0; - out_open_cost_frac = 0; + out_final_expiration = reserve.expiration_date; + out_open_cost.val = 0; + out_open_cost.frac = 0; out_no_funds=FALSE; RAISE NOTICE 'no change required'; RETURN; END IF; -- Check payment (coins and reserve) would be sufficient. -IF ( (in_total_paid_val < my_cost_val) OR - ( (in_total_paid_val = my_cost_val) AND - (in_total_paid_frac < my_cost_frac) ) ) +IF ( (in_total_paid.val < my_cost.val) OR + ( (in_total_paid.val = my_cost.val) AND + (in_total_paid.frac < my_cost.frac) ) ) THEN - out_open_cost_val = my_cost_val; - out_open_cost_frac = my_cost_frac; + out_open_cost.val = my_cost.val; + out_open_cost.frac = my_cost.frac; out_no_funds=FALSE; -- We must return a failure, which is indicated by -- the expiration being below the desired expiration. - IF (my_reserve_expiration >= in_desired_expiration) + IF (reserve.expiration_date >= in_desired_expiration) THEN -- This case is relevant especially if the purse -- count was to be increased and the payment was @@ -160,32 +136,32 @@ THEN RAISE NOTICE 'forcing low expiration time'; out_final_expiration = 0; ELSE - out_final_expiration = my_reserve_expiration; + out_final_expiration = reserve.expiration_date; END IF; RAISE NOTICE 'amount paid too low'; RETURN; END IF; -- Check reserve balance is sufficient. -IF (my_balance_val > in_reserve_payment_val) +IF (my_balance.val > in_reserve_payment.val) THEN - IF (my_balance_frac >= in_reserve_payment_frac) + IF (my_balance.frac >= in_reserve_payment.frac) THEN - my_balance_val=my_balance_val - in_reserve_payment_val; - my_balance_frac=my_balance_frac - in_reserve_payment_frac; + my_balance.val=my_balance.val - in_reserve_payment.val; + my_balance.frac=my_balance.frac - in_reserve_payment.frac; ELSE - my_balance_val=my_balance_val - in_reserve_payment_val - 1; - my_balance_frac=my_balance_frac + 100000000 - in_reserve_payment_frac; + my_balance.val=my_balance.val - in_reserve_payment.val - 1; + my_balance.frac=my_balance.frac + 100000000 - in_reserve_payment.frac; END IF; ELSE - IF (my_balance_val = in_reserve_payment_val) AND (my_balance_frac >= in_reserve_payment_frac) + IF (my_balance.val = in_reserve_payment.val) AND (my_balance.frac >= in_reserve_payment.frac) THEN - my_balance_val=0; - my_balance_frac=my_balance_frac - in_reserve_payment_frac; + my_balance.val=0; + my_balance.frac=my_balance.frac - in_reserve_payment.frac; ELSE - out_final_expiration = my_reserve_expiration; - out_open_cost_val = my_cost_val; - out_open_cost_frac = my_cost_frac; + out_final_expiration = reserve.expiration_date; + out_open_cost.val = my_cost.val; + out_open_cost.frac = my_cost.frac; out_no_funds=TRUE; RAISE NOTICE 'reserve balance too low'; RETURN; @@ -193,17 +169,15 @@ ELSE END IF; UPDATE reserves SET - current_balance_val=my_balance_val - ,current_balance_frac=my_balance_frac - ,gc_date=my_reserve_expiration + in_reserve_gc_delay + current_balance=my_balance + ,gc_date=reserve.expiration_date + in_reserve_gc_delay ,expiration_date=my_expiration_date - ,purses_allowed=my_purses_allowed + ,purses_allowed=reserve.purses_allowed WHERE reserve_pub=in_reserve_pub; out_final_expiration=my_expiration_date; -out_open_cost_val = my_cost_val; -out_open_cost_frac = my_cost_frac; +out_open_cost = my_cost; out_no_funds=FALSE; RETURN; diff --git a/src/exchangedb/exchange_do_reserve_purse.sql b/src/exchangedb/exchange_do_reserve_purse.sql index 0476e60d1..43d4c91a8 100644 --- a/src/exchangedb/exchange_do_reserve_purse.sql +++ b/src/exchangedb/exchange_do_reserve_purse.sql @@ -22,8 +22,7 @@ CREATE OR REPLACE FUNCTION exchange_do_reserve_purse( IN in_reserve_gc INT8, IN in_reserve_sig BYTEA, IN in_reserve_quota BOOLEAN, - IN in_purse_fee_val INT8, - IN in_purse_fee_frac INT4, + IN in_purse_fee taler_amount, IN in_reserve_pub BYTEA, IN in_wallet_h_payto BYTEA, OUT out_no_funds BOOLEAN, @@ -101,8 +100,8 @@ ELSE -- UPDATE reserves balance (and check if balance is enough to pay the fee) IF (out_no_reserve) THEN - IF ( (0 != in_purse_fee_val) OR - (0 != in_purse_fee_frac) ) + IF ( (0 != in_purse_fee.val) OR + (0 != in_purse_fee.frac) ) THEN out_no_funds=TRUE; RETURN; @@ -118,22 +117,22 @@ ELSE ELSE UPDATE exchange.reserves SET - current_balance_frac=current_balance_frac-in_purse_fee_frac + current_balance.frac=(current_balance).frac-in_purse_fee.frac + CASE - WHEN current_balance_frac < in_purse_fee_frac + WHEN (current_balance).frac < in_purse_fee.frac THEN 100000000 ELSE 0 END, - current_balance_val=current_balance_val-in_purse_fee_val + current_balance.val=(current_balance).val-in_purse_fee.val - CASE - WHEN current_balance_frac < in_purse_fee_frac + WHEN (current_balance).frac < in_purse_fee.frac THEN 1 ELSE 0 END WHERE reserve_pub=in_reserve_pub - AND ( (current_balance_val > in_purse_fee_val) OR - ( (current_balance_frac >= in_purse_fee_frac) AND - (current_balance_val >= in_purse_fee_val) ) ); + AND ( ((current_balance).val > in_purse_fee.val) OR + ( ((current_balance).frac >= in_purse_fee.frac) AND + ((current_balance).val >= in_purse_fee.val) ) ); IF NOT FOUND THEN out_no_funds=TRUE; @@ -159,5 +158,5 @@ INSERT INTO exchange.account_merges END $$; -COMMENT ON FUNCTION exchange_do_reserve_purse(BYTEA, BYTEA, INT8, INT8, INT8, BYTEA, BOOLEAN, INT8, INT4, BYTEA, BYTEA) +COMMENT ON FUNCTION exchange_do_reserve_purse(BYTEA, BYTEA, INT8, INT8, INT8, BYTEA, BOOLEAN, taler_amount, BYTEA, BYTEA) IS 'Create a purse for a reserve.'; diff --git a/src/exchangedb/exchange_do_reserves_in_insert.sql b/src/exchangedb/exchange_do_reserves_in_insert.sql index 26966a940..fcf8ef4c4 100644 --- a/src/exchangedb/exchange_do_reserves_in_insert.sql +++ b/src/exchangedb/exchange_do_reserves_in_insert.sql @@ -19,8 +19,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch_reserves_in_insert( IN in_reserve_expiration INT8, IN in_reserve_pub BYTEA, IN in_wire_ref INT8, - IN in_credit_val INT8, - IN in_credit_frac INT4, + IN in_credit taler_amount, IN in_exchange_account_name VARCHAR, IN in_execution_date INT8, IN in_wire_source_h_payto BYTEA, @@ -42,14 +41,12 @@ BEGIN INSERT INTO reserves (reserve_pub - ,current_balance_val - ,current_balance_frac + ,current_balance ,expiration_date ,gc_date) VALUES (in_reserve_pub - ,in_credit_val - ,in_credit_frac + ,in_credit ,in_reserve_expiration ,in_gc_date) ON CONFLICT DO NOTHING @@ -67,8 +64,8 @@ BEGIN VALUES (in_reserve_pub ,in_wire_ref - ,in_credit_val - ,in_credit_frac + ,in_credit.val + ,in_credit.frac ,in_exchange_account_name ,in_wire_source_h_payto ,in_execution_date) @@ -90,8 +87,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch2_reserves_insert( IN in_reserve_expiration INT8, IN in0_reserve_pub BYTEA, IN in0_wire_ref INT8, - IN in0_credit_val INT8, - IN in0_credit_frac INT4, + IN in0_credit taler_amount, IN in0_exchange_account_name VARCHAR, IN in0_execution_date INT8, IN in0_wire_source_h_payto BYTEA, @@ -99,8 +95,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch2_reserves_insert( IN in0_notify TEXT, IN in1_reserve_pub BYTEA, IN in1_wire_ref INT8, - IN in1_credit_val INT8, - IN in1_credit_frac INT4, + IN in1_credit taler_amount, IN in1_exchange_account_name VARCHAR, IN in1_execution_date INT8, IN in1_wire_source_h_payto BYTEA, @@ -114,11 +109,8 @@ LANGUAGE plpgsql AS $$ DECLARE curs_reserve_exist REFCURSOR; -DECLARE k INT8; -DECLARE curs_transaction_exist REFCURSOR; -DECLARE i RECORD; BEGIN transaction_duplicate0 = TRUE; @@ -138,19 +130,16 @@ BEGIN WITH reserve_changes AS ( INSERT INTO reserves (reserve_pub - ,current_balance_val - ,current_balance_frac + ,current_balance ,expiration_date ,gc_date) VALUES (in0_reserve_pub - ,in0_credit_val - ,in0_credit_frac + ,in0_credit ,in_reserve_expiration ,in_gc_date), (in1_reserve_pub - ,in1_credit_val - ,in1_credit_frac + ,in1_credit ,in_reserve_expiration ,in_gc_date) ON CONFLICT DO NOTHING @@ -200,15 +189,15 @@ BEGIN VALUES (in0_reserve_pub ,in0_wire_ref - ,in0_credit_val - ,in0_credit_frac + ,in0_credit.val + ,in0_credit.frac ,in0_exchange_account_name ,in0_wire_source_h_payto ,in0_execution_date), (in1_reserve_pub ,in1_wire_ref - ,in1_credit_val - ,in1_credit_frac + ,in1_credit.val + ,in1_credit.frac ,in1_exchange_account_name ,in1_wire_source_h_payto ,in1_execution_date) @@ -261,8 +250,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch4_reserves_insert( IN in_reserve_expiration INT8, IN in0_reserve_pub BYTEA, IN in0_wire_ref INT8, - IN in0_credit_val INT8, - IN in0_credit_frac INT4, + IN in0_credit taler_amount, IN in0_exchange_account_name VARCHAR, IN in0_execution_date INT8, IN in0_wire_source_h_payto BYTEA, @@ -270,8 +258,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch4_reserves_insert( IN in0_notify TEXT, IN in1_reserve_pub BYTEA, IN in1_wire_ref INT8, - IN in1_credit_val INT8, - IN in1_credit_frac INT4, + IN in1_credit taler_amount, IN in1_exchange_account_name VARCHAR, IN in1_execution_date INT8, IN in1_wire_source_h_payto BYTEA, @@ -279,8 +266,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch4_reserves_insert( IN in1_notify TEXT, IN in2_reserve_pub BYTEA, IN in2_wire_ref INT8, - IN in2_credit_val INT8, - IN in2_credit_frac INT4, + IN in2_credit taler_amount, IN in2_exchange_account_name VARCHAR, IN in2_execution_date INT8, IN in2_wire_source_h_payto BYTEA, @@ -288,8 +274,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch4_reserves_insert( IN in2_notify TEXT, IN in3_reserve_pub BYTEA, IN in3_wire_ref INT8, - IN in3_credit_val INT8, - IN in3_credit_frac INT4, + IN in3_credit taler_amount, IN in3_exchange_account_name VARCHAR, IN in3_execution_date INT8, IN in3_wire_source_h_payto BYTEA, @@ -307,11 +292,8 @@ LANGUAGE plpgsql AS $$ DECLARE curs_reserve_exist REFCURSOR; -DECLARE k INT8; -DECLARE curs_transaction_exist REFCURSOR; -DECLARE i RECORD; BEGIN transaction_duplicate0=TRUE; @@ -337,29 +319,24 @@ BEGIN WITH reserve_changes AS ( INSERT INTO reserves (reserve_pub - ,current_balance_val - ,current_balance_frac + ,current_balance ,expiration_date ,gc_date) VALUES (in0_reserve_pub - ,in0_credit_val - ,in0_credit_frac + ,in0_credit ,in_reserve_expiration ,in_gc_date), (in1_reserve_pub - ,in1_credit_val - ,in1_credit_frac + ,in1_credit ,in_reserve_expiration ,in_gc_date), (in2_reserve_pub - ,in2_credit_val - ,in2_credit_frac + ,in2_credit ,in_reserve_expiration ,in_gc_date), (in3_reserve_pub - ,in3_credit_val - ,in3_credit_frac + ,in3_credit ,in_reserve_expiration ,in_gc_date) ON CONFLICT DO NOTHING @@ -425,29 +402,29 @@ BEGIN VALUES (in0_reserve_pub ,in0_wire_ref - ,in0_credit_val - ,in0_credit_frac + ,in0_credit.val + ,in0_credit.frac ,in0_exchange_account_name ,in0_wire_source_h_payto ,in0_execution_date), (in1_reserve_pub ,in1_wire_ref - ,in1_credit_val - ,in1_credit_frac + ,in1_credit.val + ,in1_credit.frac ,in1_exchange_account_name ,in1_wire_source_h_payto ,in1_execution_date), (in2_reserve_pub ,in2_wire_ref - ,in2_credit_val - ,in2_credit_frac + ,in2_credit.val + ,in2_credit.frac ,in2_exchange_account_name ,in2_wire_source_h_payto ,in2_execution_date), (in3_reserve_pub ,in3_wire_ref - ,in3_credit_val - ,in3_credit_frac + ,in3_credit.val + ,in3_credit.frac ,in3_exchange_account_name ,in3_wire_source_h_payto ,in3_execution_date) @@ -522,8 +499,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch8_reserves_insert( IN in_reserve_expiration INT8, IN in0_reserve_pub BYTEA, IN in0_wire_ref INT8, - IN in0_credit_val INT8, - IN in0_credit_frac INT4, + IN in0_credit taler_amount, IN in0_exchange_account_name VARCHAR, IN in0_execution_date INT8, IN in0_wire_source_h_payto BYTEA, @@ -531,8 +507,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch8_reserves_insert( IN in0_notify TEXT, IN in1_reserve_pub BYTEA, IN in1_wire_ref INT8, - IN in1_credit_val INT8, - IN in1_credit_frac INT4, + IN in1_credit taler_amount, IN in1_exchange_account_name VARCHAR, IN in1_execution_date INT8, IN in1_wire_source_h_payto BYTEA, @@ -540,8 +515,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch8_reserves_insert( IN in1_notify TEXT, IN in2_reserve_pub BYTEA, IN in2_wire_ref INT8, - IN in2_credit_val INT8, - IN in2_credit_frac INT4, + IN in2_credit taler_amount, IN in2_exchange_account_name VARCHAR, IN in2_execution_date INT8, IN in2_wire_source_h_payto BYTEA, @@ -549,8 +523,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch8_reserves_insert( IN in2_notify TEXT, IN in3_reserve_pub BYTEA, IN in3_wire_ref INT8, - IN in3_credit_val INT8, - IN in3_credit_frac INT4, + IN in3_credit taler_amount, IN in3_exchange_account_name VARCHAR, IN in3_execution_date INT8, IN in3_wire_source_h_payto BYTEA, @@ -558,8 +531,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch8_reserves_insert( IN in3_notify TEXT, IN in4_reserve_pub BYTEA, IN in4_wire_ref INT8, - IN in4_credit_val INT8, - IN in4_credit_frac INT4, + IN in4_credit taler_amount, IN in4_exchange_account_name VARCHAR, IN in4_execution_date INT8, IN in4_wire_source_h_payto BYTEA, @@ -567,8 +539,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch8_reserves_insert( IN in4_notify TEXT, IN in5_reserve_pub BYTEA, IN in5_wire_ref INT8, - IN in5_credit_val INT8, - IN in5_credit_frac INT4, + IN in5_credit taler_amount, IN in5_exchange_account_name VARCHAR, IN in5_execution_date INT8, IN in5_wire_source_h_payto BYTEA, @@ -576,8 +547,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch8_reserves_insert( IN in5_notify TEXT, IN in6_reserve_pub BYTEA, IN in6_wire_ref INT8, - IN in6_credit_val INT8, - IN in6_credit_frac INT4, + IN in6_credit taler_amount, IN in6_exchange_account_name VARCHAR, IN in6_execution_date INT8, IN in6_wire_source_h_payto BYTEA, @@ -585,8 +555,7 @@ CREATE OR REPLACE FUNCTION exchange_do_batch8_reserves_insert( IN in6_notify TEXT, IN in7_reserve_pub BYTEA, IN in7_wire_ref INT8, - IN in7_credit_val INT8, - IN in7_credit_frac INT4, + IN in7_credit taler_amount, IN in7_exchange_account_name VARCHAR, IN in7_execution_date INT8, IN in7_wire_source_h_payto BYTEA, @@ -612,13 +581,9 @@ LANGUAGE plpgsql AS $$ DECLARE curs_reserve_exist REFCURSOR; -DECLARE k INT8; -DECLARE curs_transaction_exist REFCURSOR; -DECLARE i RECORD; -DECLARE r RECORD; BEGIN @@ -657,49 +622,40 @@ BEGIN WITH reserve_changes AS ( INSERT INTO reserves (reserve_pub - ,current_balance_val - ,current_balance_frac + ,current_balance ,expiration_date ,gc_date) VALUES (in0_reserve_pub - ,in0_credit_val - ,in0_credit_frac + ,in0_credit ,in_reserve_expiration ,in_gc_date), (in1_reserve_pub - ,in1_credit_val - ,in1_credit_frac + ,in1_credit ,in_reserve_expiration ,in_gc_date), (in2_reserve_pub - ,in2_credit_val - ,in2_credit_frac + ,in2_credit ,in_reserve_expiration ,in_gc_date), (in3_reserve_pub - ,in3_credit_val - ,in3_credit_frac + ,in3_credit ,in_reserve_expiration ,in_gc_date), (in4_reserve_pub - ,in4_credit_val - ,in4_credit_frac + ,in4_credit ,in_reserve_expiration ,in_gc_date), (in5_reserve_pub - ,in5_credit_val - ,in5_credit_frac + ,in5_credit ,in_reserve_expiration ,in_gc_date), (in6_reserve_pub - ,in6_credit_val - ,in6_credit_frac + ,in6_credit ,in_reserve_expiration ,in_gc_date), (in7_reserve_pub - ,in7_credit_val - ,in7_credit_frac + ,in7_credit ,in_reserve_expiration ,in_gc_date) ON CONFLICT DO NOTHING @@ -802,57 +758,57 @@ BEGIN VALUES (in0_reserve_pub ,in0_wire_ref - ,in0_credit_val - ,in0_credit_frac + ,in0_credit.val + ,in0_credit.frac ,in0_exchange_account_name ,in0_wire_source_h_payto ,in0_execution_date), (in1_reserve_pub ,in1_wire_ref - ,in1_credit_val - ,in1_credit_frac + ,in1_credit.val + ,in1_credit.frac ,in1_exchange_account_name ,in1_wire_source_h_payto ,in1_execution_date), (in2_reserve_pub ,in2_wire_ref - ,in2_credit_val - ,in2_credit_frac + ,in2_credit.val + ,in2_credit.frac ,in2_exchange_account_name ,in2_wire_source_h_payto ,in2_execution_date), (in3_reserve_pub ,in3_wire_ref - ,in3_credit_val - ,in3_credit_frac + ,in3_credit.val + ,in3_credit.frac ,in3_exchange_account_name ,in3_wire_source_h_payto ,in3_execution_date), (in4_reserve_pub ,in4_wire_ref - ,in4_credit_val - ,in4_credit_frac + ,in4_credit.val + ,in4_credit.frac ,in4_exchange_account_name ,in4_wire_source_h_payto ,in4_execution_date), (in5_reserve_pub ,in5_wire_ref - ,in5_credit_val - ,in5_credit_frac + ,in5_credit.val + ,in5_credit.frac ,in5_exchange_account_name ,in5_wire_source_h_payto ,in5_execution_date), (in6_reserve_pub ,in6_wire_ref - ,in6_credit_val - ,in6_credit_frac + ,in6_credit.val + ,in6_credit.frac ,in6_exchange_account_name ,in6_wire_source_h_payto ,in6_execution_date), (in7_reserve_pub ,in7_wire_ref - ,in7_credit_val - ,in7_credit_frac + ,in7_credit.val + ,in7_credit.frac ,in7_exchange_account_name ,in7_wire_source_h_payto ,in7_execution_date) @@ -983,13 +939,9 @@ LANGUAGE plpgsql AS $$ DECLARE curs REFCURSOR; -DECLARE conflict BOOL; -DECLARE dup BOOL; -DECLARE uuid INT8; -DECLARE i RECORD; BEGIN diff --git a/src/exchangedb/exchange_do_withdraw.sql b/src/exchangedb/exchange_do_withdraw.sql index f6632c543..60a63e674 100644 --- a/src/exchangedb/exchange_do_withdraw.sql +++ b/src/exchangedb/exchange_do_withdraw.sql @@ -17,8 +17,7 @@ CREATE OR REPLACE FUNCTION exchange_do_withdraw( IN cs_nonce BYTEA, - IN amount_val INT8, - IN amount_frac INT4, + IN amount taler_amount, IN h_denom_pub BYTEA, IN rpub BYTEA, IN reserve_sig BYTEA, @@ -36,11 +35,9 @@ CREATE OR REPLACE FUNCTION exchange_do_withdraw( LANGUAGE plpgsql AS $$ DECLARE - reserve_gc INT8; + reserve RECORD; denom_serial INT8; - reserve_val INT8; - reserve_frac INT4; - reserve_birthday INT4; + balance taler_amount; not_before date; BEGIN -- Shards: reserves by reserve_pub (SELECT) @@ -67,18 +64,8 @@ THEN END IF; -SELECT - current_balance_val - ,current_balance_frac - ,gc_date - ,birthday - ,reserve_uuid - INTO - reserve_val - ,reserve_frac - ,reserve_gc - ,reserve_birthday - ,ruuid +SELECT * + INTO reserve FROM exchange.reserves WHERE reserves.reserve_pub=rpub; @@ -94,8 +81,11 @@ THEN RETURN; END IF; +balance = reserve.current_balance; +ruuid = reserve.reserve_uuid; + -- Check if age requirements are present -IF ((NOT do_age_check) OR (reserve_birthday = 0)) +IF ((NOT do_age_check) OR (reserve.birthday = 0)) THEN age_ok = TRUE; allowed_maximum_age = -1; @@ -105,7 +95,7 @@ ELSE -- birthday set (reserve_birthday != 0), but the client called the -- batch-withdraw endpoint instead of the age-withdraw endpoint, which it -- should have. - not_before=date '1970-01-01' + reserve_birthday; + not_before=date '1970-01-01' + reserve.birthday; allowed_maximum_age = extract(year from age(current_date, not_before)); reserve_found=TRUE; @@ -133,8 +123,8 @@ VALUES ,ruuid ,reserve_sig ,now - ,amount_val - ,amount_frac) + ,amount.val + ,amount.frac) ON CONFLICT DO NOTHING; IF NOT FOUND @@ -147,21 +137,21 @@ THEN END IF; -- Check reserve balance is sufficient. -IF (reserve_val > amount_val) +IF (balance.val > amount.val) THEN - IF (reserve_frac >= amount_frac) + IF (balance.frac >= amount.frac) THEN - reserve_val=reserve_val - amount_val; - reserve_frac=reserve_frac - amount_frac; + balance.val=balance.val - amount.val; + balance.frac=balance.frac - amount.frac; ELSE - reserve_val=reserve_val - amount_val - 1; - reserve_frac=reserve_frac + 100000000 - amount_frac; + balance.val=balance.val - amount.val - 1; + balance.frac=balance.frac + 100000000 - amount.frac; END IF; ELSE - IF (reserve_val = amount_val) AND (reserve_frac >= amount_frac) + IF (balance.val = amount.val) AND (balance.frac >= amount.frac) THEN - reserve_val=0; - reserve_frac=reserve_frac - amount_frac; + balance.val=0; + balance.frac=balance.frac - amount.frac; ELSE reserve_found=TRUE; nonce_ok=TRUE; -- we do not really know @@ -171,13 +161,12 @@ ELSE END IF; -- Calculate new expiration dates. -min_reserve_gc=GREATEST(min_reserve_gc,reserve_gc); +min_reserve_gc=GREATEST(min_reserve_gc,reserve.gc_date); -- Update reserve balance. UPDATE reserves SET gc_date=min_reserve_gc - ,current_balance_val=reserve_val - ,current_balance_frac=reserve_frac + ,current_balance=balance WHERE reserves.reserve_pub=rpub; @@ -222,7 +211,6 @@ END IF; END $$; - -COMMENT ON FUNCTION exchange_do_withdraw(BYTEA, INT8, INT4, BYTEA, BYTEA, BYTEA, BYTEA, BYTEA, INT8, INT8, BOOLEAN) +COMMENT ON FUNCTION exchange_do_withdraw(BYTEA, taler_amount, BYTEA, BYTEA, BYTEA, BYTEA, BYTEA, INT8, INT8, BOOLEAN) IS 'Checks whether the reserve has sufficient balance for a withdraw operation (or the request is repeated and was previously approved) and if the age requirements are formally met. If so updates the database with the result'; diff --git a/src/exchangedb/pg_do_batch_withdraw.c b/src/exchangedb/pg_do_batch_withdraw.c index 45b1d3229..8e118a99b 100644 --- a/src/exchangedb/pg_do_batch_withdraw.c +++ b/src/exchangedb/pg_do_batch_withdraw.c @@ -43,7 +43,8 @@ TEH_PG_do_batch_withdraw ( struct PostgresClosure *pg = cls; struct GNUNET_TIME_Timestamp gc; struct GNUNET_PQ_QueryParam params[] = { - TALER_PQ_query_param_amount (amount), + TALER_PQ_query_param_amount_tuple (pg->conn, + amount), GNUNET_PQ_query_param_auto_from_type (reserve_pub), GNUNET_PQ_query_param_timestamp (&now), GNUNET_PQ_query_param_timestamp (&gc), @@ -80,7 +81,7 @@ TEH_PG_do_batch_withdraw ( ",allowed_maximum_age" ",ruuid" " FROM exchange_do_batch_withdraw" - " ($1,$2,$3,$4,$5,$6);"); + " ($1,$2,$3,$4,$5);"); return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "call_batch_withdraw", params, diff --git a/src/exchangedb/pg_do_batch_withdraw_insert.c b/src/exchangedb/pg_do_batch_withdraw_insert.c index 8be18f7cc..2952483cb 100644 --- a/src/exchangedb/pg_do_batch_withdraw_insert.c +++ b/src/exchangedb/pg_do_batch_withdraw_insert.c @@ -41,7 +41,8 @@ TEH_PG_do_batch_withdraw_insert ( NULL == nonce ? GNUNET_PQ_query_param_null () : GNUNET_PQ_query_param_auto_from_type (nonce), - TALER_PQ_query_param_amount (&collectable->amount_with_fee), + TALER_PQ_query_param_amount_tuple (pg->conn, + &collectable->amount_with_fee), GNUNET_PQ_query_param_auto_from_type (&collectable->denom_pub_hash), GNUNET_PQ_query_param_uint64 (&ruuid), GNUNET_PQ_query_param_auto_from_type (&collectable->reserve_sig), @@ -69,7 +70,7 @@ TEH_PG_do_batch_withdraw_insert ( ",out_conflict AS conflict" ",out_nonce_reuse AS nonce_reuse" " FROM exchange_do_batch_withdraw_insert" - " ($1,$2,$3,$4,$5,$6,$7,$8,$9);"); + " ($1,$2,$3,$4,$5,$6,$7,$8);"); return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "call_batch_withdraw_insert", params, diff --git a/src/exchangedb/pg_do_purse_deposit.c b/src/exchangedb/pg_do_purse_deposit.c index ba6f03c11..3ac3cfbfc 100644 --- a/src/exchangedb/pg_do_purse_deposit.c +++ b/src/exchangedb/pg_do_purse_deposit.c @@ -47,10 +47,14 @@ TEH_PG_do_purse_deposit ( ? GNUNET_PQ_query_param_null () : GNUNET_PQ_query_param_uint64 (&partner_id), GNUNET_PQ_query_param_auto_from_type (purse_pub), - TALER_PQ_query_param_amount (amount), + TALER_PQ_query_param_amount_tuple ( + pg->conn, + amount), GNUNET_PQ_query_param_auto_from_type (coin_pub), GNUNET_PQ_query_param_auto_from_type (coin_sig), - TALER_PQ_query_param_amount (amount_minus_fee), + TALER_PQ_query_param_amount_tuple ( + pg->conn, + amount_minus_fee), GNUNET_PQ_query_param_timestamp (&reserve_expiration), GNUNET_PQ_query_param_timestamp (&now), GNUNET_PQ_query_param_end @@ -77,7 +81,7 @@ TEH_PG_do_purse_deposit ( ",out_conflict AS conflict" ",out_late AS too_late" " FROM exchange_do_purse_deposit" - " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);"); + " ($1,$2,$3,$4,$5,$6,$7,$8);"); return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "call_purse_deposit", diff --git a/src/exchangedb/pg_do_reserve_open.c b/src/exchangedb/pg_do_reserve_open.c index 542d1f468..cdc42964c 100644 --- a/src/exchangedb/pg_do_reserve_open.c +++ b/src/exchangedb/pg_do_reserve_open.c @@ -44,20 +44,26 @@ TEH_PG_do_reserve_open ( struct PostgresClosure *pg = cls; struct GNUNET_PQ_QueryParam params[] = { GNUNET_PQ_query_param_auto_from_type (reserve_pub), - TALER_PQ_query_param_amount (total_paid), - TALER_PQ_query_param_amount (reserve_payment), + TALER_PQ_query_param_amount_tuple ( + pg->conn, + total_paid), + TALER_PQ_query_param_amount_tuple ( + pg->conn, + reserve_payment), GNUNET_PQ_query_param_uint32 (&min_purse_limit), GNUNET_PQ_query_param_uint32 (&pg->def_purse_limit), GNUNET_PQ_query_param_auto_from_type (reserve_sig), GNUNET_PQ_query_param_timestamp (&desired_expiration), GNUNET_PQ_query_param_relative_time (&pg->legal_reserve_expiration_time), GNUNET_PQ_query_param_timestamp (&now), - TALER_PQ_query_param_amount (open_fee), + TALER_PQ_query_param_amount_tuple (pg->conn, + open_fee), GNUNET_PQ_query_param_end }; struct GNUNET_PQ_ResultSpec rs[] = { - TALER_PQ_RESULT_SPEC_AMOUNT ("out_open_cost", - open_cost), + TALER_PQ_result_spec_amount_tuple ("out_open_cost", + pg->currency, + open_cost), GNUNET_PQ_result_spec_timestamp ("out_final_expiration", final_expiration), GNUNET_PQ_result_spec_bool ("out_no_funds", @@ -68,12 +74,11 @@ TEH_PG_do_reserve_open ( PREPARE (pg, "do_reserve_open", "SELECT " - " out_open_cost_val" - ",out_open_cost_frac" + " out_open_cost" ",out_final_expiration" ",out_no_funds" " FROM exchange_do_reserve_open" - " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13);"); + " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);"); return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "do_reserve_open", params, diff --git a/src/exchangedb/pg_do_reserve_purse.c b/src/exchangedb/pg_do_reserve_purse.c index b08594175..6e05a5ab0 100644 --- a/src/exchangedb/pg_do_reserve_purse.c +++ b/src/exchangedb/pg_do_reserve_purse.c @@ -74,9 +74,10 @@ TEH_PG_do_reserve_purse ( GNUNET_PQ_query_param_timestamp (&reserve_gc), GNUNET_PQ_query_param_auto_from_type (reserve_sig), GNUNET_PQ_query_param_bool (NULL == purse_fee), - TALER_PQ_query_param_amount (NULL == purse_fee - ? &zero_fee - : purse_fee), + TALER_PQ_query_param_amount_tuple (pg->conn, + NULL == purse_fee + ? &zero_fee + : purse_fee), GNUNET_PQ_query_param_auto_from_type (reserve_pub), GNUNET_PQ_query_param_auto_from_type (&h_payto), GNUNET_PQ_query_param_end @@ -111,7 +112,7 @@ TEH_PG_do_reserve_purse ( ",out_no_reserve AS no_reserve" ",out_conflict AS conflict" " FROM exchange_do_reserve_purse" - " ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);"); + " ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);"); return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "call_reserve_purse", diff --git a/src/exchangedb/pg_do_withdraw.c b/src/exchangedb/pg_do_withdraw.c index 99732df6b..037941792 100644 --- a/src/exchangedb/pg_do_withdraw.c +++ b/src/exchangedb/pg_do_withdraw.c @@ -46,7 +46,8 @@ TEH_PG_do_withdraw ( NULL == nonce ? GNUNET_PQ_query_param_null () : GNUNET_PQ_query_param_auto_from_type (nonce), - TALER_PQ_query_param_amount (&collectable->amount_with_fee), + TALER_PQ_query_param_amount_tuple (pg->conn, + &collectable->amount_with_fee), GNUNET_PQ_query_param_auto_from_type (&collectable->denom_pub_hash), GNUNET_PQ_query_param_auto_from_type (&collectable->reserve_pub), GNUNET_PQ_query_param_auto_from_type (&collectable->reserve_sig), @@ -83,7 +84,7 @@ TEH_PG_do_withdraw ( ",allowed_maximum_age" ",ruuid" " FROM exchange_do_withdraw" - " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11);"); + " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);"); gc = GNUNET_TIME_absolute_to_timestamp ( GNUNET_TIME_absolute_add (now.abs_time, pg->legal_reserve_expiration_time)); diff --git a/src/exchangedb/pg_get_expired_reserves.c b/src/exchangedb/pg_get_expired_reserves.c index c7162dc6b..852d1780a 100644 --- a/src/exchangedb/pg_get_expired_reserves.c +++ b/src/exchangedb/pg_get_expired_reserves.c @@ -84,8 +84,9 @@ reserve_expired_cb (void *cls, &account_details), GNUNET_PQ_result_spec_auto_from_type ("reserve_pub", &reserve_pub), - TALER_PQ_RESULT_SPEC_AMOUNT ("current_balance", - &remaining_balance), + TALER_PQ_result_spec_amount_tuple ("current_balance", + pg->currency, + &remaining_balance), GNUNET_PQ_result_spec_end }; @@ -137,7 +138,7 @@ TEH_PG_get_expired_reserves (void *cls, " SELECT * " " FROM reserves " " WHERE expiration_date <= $1 " - " AND (current_balance_val != 0 OR current_balance_frac != 0) " + " AND ((current_balance).val != 0 OR (current_balance).frac != 0) " " ORDER BY expiration_date ASC " " LIMIT 1 " ") " @@ -145,8 +146,7 @@ TEH_PG_get_expired_reserves (void *cls, " ed.expiration_date " " ,payto_uri AS account_details " " ,ed.reserve_pub " - " ,current_balance_val " - " ,current_balance_frac " + " ,current_balance " "FROM ( " " SELECT " " * " diff --git a/src/exchangedb/pg_get_reserve_balance.c b/src/exchangedb/pg_get_reserve_balance.c index 7d5eb58f8..8886d2b1c 100644 --- a/src/exchangedb/pg_get_reserve_balance.c +++ b/src/exchangedb/pg_get_reserve_balance.c @@ -36,16 +36,16 @@ TEH_PG_get_reserve_balance (void *cls, GNUNET_PQ_query_param_end }; struct GNUNET_PQ_ResultSpec rs[] = { - TALER_PQ_RESULT_SPEC_AMOUNT ("current_balance", - balance), + TALER_PQ_result_spec_amount_tuple ("current_balance", + pg->currency, + balance), GNUNET_PQ_result_spec_end }; PREPARE (pg, "get_reserve_balance", "SELECT" - " current_balance_val" - ",current_balance_frac" + " current_balance" " FROM reserves" " WHERE reserve_pub=$1;"); return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, diff --git a/src/exchangedb/pg_insert_history_request.c b/src/exchangedb/pg_insert_history_request.c index ab3f39133..63a175de9 100644 --- a/src/exchangedb/pg_insert_history_request.c +++ b/src/exchangedb/pg_insert_history_request.c @@ -41,7 +41,8 @@ TEH_PG_insert_history_request ( GNUNET_PQ_query_param_auto_from_type (reserve_pub), GNUNET_PQ_query_param_auto_from_type (reserve_sig), GNUNET_PQ_query_param_timestamp (&request_timestamp), - TALER_PQ_query_param_amount (history_fee), + TALER_PQ_query_param_amount_tuple (pg->conn, + history_fee), GNUNET_PQ_query_param_end }; struct GNUNET_PQ_ResultSpec rs[] = { @@ -58,7 +59,7 @@ TEH_PG_insert_history_request ( " out_balance_ok AS balance_ok" " ,out_idempotent AS idempotent" " FROM exchange_do_history_request" - " ($1, $2, $3, $4, $5)"); + " ($1, $2, $3, $4)"); return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "call_history_request", params, diff --git a/src/exchangedb/pg_reserves_get.c b/src/exchangedb/pg_reserves_get.c index d081ca00f..5b487b860 100644 --- a/src/exchangedb/pg_reserves_get.c +++ b/src/exchangedb/pg_reserves_get.c @@ -35,8 +35,9 @@ TEH_PG_reserves_get (void *cls, GNUNET_PQ_query_param_end }; struct GNUNET_PQ_ResultSpec rs[] = { - TALER_PQ_RESULT_SPEC_AMOUNT ("current_balance", - &reserve->balance), + TALER_PQ_result_spec_amount_tuple ("current_balance", + pg->currency, + &reserve->balance), GNUNET_PQ_result_spec_timestamp ("expiration_date", &reserve->expiry), GNUNET_PQ_result_spec_timestamp ("gc_date", @@ -47,8 +48,7 @@ TEH_PG_reserves_get (void *cls, PREPARE (pg, "reserves_get", "SELECT" - " current_balance_val" - ",current_balance_frac" + " current_balance" ",expiration_date" ",gc_date" " FROM reserves" diff --git a/src/exchangedb/pg_reserves_in_insert.c b/src/exchangedb/pg_reserves_in_insert.c index 7f59826c7..ff33a2f11 100644 --- a/src/exchangedb/pg_reserves_in_insert.c +++ b/src/exchangedb/pg_reserves_in_insert.c @@ -100,7 +100,7 @@ struct ReserveRecord #define RR_QUERY_PARAM(rr,index) \ GNUNET_PQ_query_param_auto_from_type (rr[index].reserve->reserve_pub), \ GNUNET_PQ_query_param_uint64 (&rr[index].reserve->wire_reference), \ - TALER_PQ_query_param_amount (rr[index].reserve->balance), \ + TALER_PQ_query_param_amount_tuple (pg->conn, rr[index].reserve->balance), \ GNUNET_PQ_query_param_string (rr[index].reserve->exchange_account_name), \ GNUNET_PQ_query_param_timestamp (&rr[index].reserve->execution_time), \ GNUNET_PQ_query_param_auto_from_type (&rr[index].h_payto), \ @@ -154,7 +154,7 @@ insert1 (struct PostgresClosure *pg, " transaction_duplicate0 AS transaction_duplicate0" ",ruuid0 AS reserve_uuid0" " FROM exchange_do_batch_reserves_in_insert" - " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11);"); + " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);"); qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "batch1_reserve_create", params, @@ -214,7 +214,7 @@ insert2 (struct PostgresClosure *pg, ",ruuid0 AS reserve_uuid0" ",ruuid1 AS reserve_uuid1" " FROM exchange_do_batch2_reserves_insert" - " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20);"); + " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18);"); qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "batch2_reserve_create", params, @@ -285,7 +285,7 @@ insert4 (struct PostgresClosure *pg, ",ruuid2 AS reserve_uuid2" ",ruuid3 AS reserve_uuid3" " FROM exchange_do_batch4_reserves_insert" - " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38);"); + " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34);"); qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "batch4_reserve_create", params, @@ -373,7 +373,7 @@ insert8 (struct PostgresClosure *pg, ",ruuid6 AS reserve_uuid6" ",ruuid7 AS reserve_uuid7" " FROM exchange_do_batch8_reserves_insert" - " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39, $40, $41,$42,$43,$44,$45,$46,$47,$48,$49,$50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$60,$61,$62,$63,$64,$65,$66,$67,$68,$69,$70,$71,$72,$73,$74);"); + " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39, $40, $41,$42,$43,$44,$45,$46,$47,$48,$49,$50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$60,$61,$62,$63,$64,$65,$66);"); qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "batch8_reserve_create", @@ -537,7 +537,8 @@ transact ( GNUNET_PQ_query_param_auto_from_type (rr[i].reserve->reserve_pub), GNUNET_PQ_query_param_timestamp (&reserve_expiration), GNUNET_PQ_query_param_uint64 (&rr[i].reserve->wire_reference), - TALER_PQ_query_param_amount (rr[i].reserve->balance), + TALER_PQ_query_param_amount_tuple (pg->conn, + rr[i].reserve->balance), GNUNET_PQ_query_param_string (rr[i].reserve->exchange_account_name), GNUNET_PQ_query_param_auto_from_type (&rr[i].h_payto), GNUNET_PQ_query_param_string (rr[i].notify_s), @@ -859,7 +860,8 @@ TEH_PG_reserves_in_insertN ( GNUNET_PQ_query_param_auto_from_type (reserve_pubs[i]), GNUNET_PQ_query_param_timestamp (&reserve_expiration), GNUNET_PQ_query_param_uint64 (&wire_reference[i]), - TALER_PQ_query_param_amount (balances[i]), + TALER_PQ_query_param_amount_tuple (pg->conn, + balances[i]), GNUNET_PQ_query_param_string (exchange_account_names[i]), GNUNET_PQ_query_param_auto_from_type (h_paytos[i]), GNUNET_PQ_query_param_string (notify_s[i]), diff --git a/src/exchangedb/pg_reserves_update.c b/src/exchangedb/pg_reserves_update.c index a76c37c27..ecec81fc0 100644 --- a/src/exchangedb/pg_reserves_update.c +++ b/src/exchangedb/pg_reserves_update.c @@ -33,7 +33,8 @@ TEH_PG_reserves_update (void *cls, struct GNUNET_PQ_QueryParam params[] = { GNUNET_PQ_query_param_timestamp (&reserve->expiry), GNUNET_PQ_query_param_timestamp (&reserve->gc), - TALER_PQ_query_param_amount (&reserve->balance), + TALER_PQ_query_param_amount_tuple (pg->conn, + &reserve->balance), GNUNET_PQ_query_param_auto_from_type (&reserve->pub), GNUNET_PQ_query_param_end }; @@ -44,9 +45,8 @@ TEH_PG_reserves_update (void *cls, " SET" " expiration_date=$1" ",gc_date=$2" - ",current_balance_val=$3" - ",current_balance_frac=$4" - " WHERE reserve_pub=$5;"); + ",current_balance=$3" + " WHERE reserve_pub=$4;"); return GNUNET_PQ_eval_prepared_non_select (pg->conn, "reserve_update", params); diff --git a/src/exchangedb/pg_select_reserve_close_info.c b/src/exchangedb/pg_select_reserve_close_info.c index 973f5fa51..06e8dda2e 100644 --- a/src/exchangedb/pg_select_reserve_close_info.c +++ b/src/exchangedb/pg_select_reserve_close_info.c @@ -39,8 +39,9 @@ TEH_PG_select_reserve_close_info ( GNUNET_PQ_query_param_end }; struct GNUNET_PQ_ResultSpec rs[] = { - TALER_PQ_RESULT_SPEC_AMOUNT ("current_balance", - balance), + TALER_PQ_result_spec_amount_tuple ("current_balance", + pg->currency, + balance), GNUNET_PQ_result_spec_string ("payto_uri", payto_uri), GNUNET_PQ_result_spec_end @@ -49,8 +50,7 @@ TEH_PG_select_reserve_close_info ( PREPARE (pg, "select_reserve_close_info", "SELECT " - " r.current_balance_val" - ",r.current_balance_frac" + " r.current_balance" ",wt.payto_uri" " FROM reserves r" " LEFT JOIN reserves_in ri USING (reserve_pub)" diff --git a/src/exchangedb/plugin_exchangedb_postgres.c b/src/exchangedb/plugin_exchangedb_postgres.c index 93ea09cd8..4f8959ff1 100644 --- a/src/exchangedb/plugin_exchangedb_postgres.c +++ b/src/exchangedb/plugin_exchangedb_postgres.c @@ -294,14 +294,6 @@ TEH_PG_internal_setup (struct PostgresClosure *pg) if (NULL == db_conn) return GNUNET_SYSERR; - if (GNUNET_OK != TALER_PQ_load_oids_for_composite_types (db_conn)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Failed to load OIDs for composite types\n"); - GNUNET_PQ_disconnect (db_conn); - return GNUNET_SYSERR; - } - pg->prep_gen++; pg->conn = db_conn; } @@ -787,6 +779,12 @@ libtaler_plugin_exchangedb_postgres_init (void *cls) plugin->batch_ensure_coin_known = &TEH_PG_batch_ensure_coin_known; + if (GNUNET_OK != TALER_PQ_load_oids_for_composite_types (pg->conn)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to load OIDs for composite types\n"); + } + return plugin; } |