diff options
author | Christian Grothoff <christian@grothoff.org> | 2022-05-22 16:18:09 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2022-05-22 16:18:09 +0200 |
commit | 4a5d71cca2297cfd98b5dd907df2fc355d0da297 (patch) | |
tree | cfa1a24613b09d5bc6e36bad41479bb4551761df /src/exchangedb/exchange-0001-part.sql | |
parent | 96fb11bed0def1bfb3666b732dd32be253f46c01 (diff) | |
download | exchange-4a5d71cca2297cfd98b5dd907df2fc355d0da297.tar.xz |
-implement reserve history DB logic
Diffstat (limited to 'src/exchangedb/exchange-0001-part.sql')
-rw-r--r-- | src/exchangedb/exchange-0001-part.sql | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/src/exchangedb/exchange-0001-part.sql b/src/exchangedb/exchange-0001-part.sql index d8855b011..a47a2aede 100644 --- a/src/exchangedb/exchange-0001-part.sql +++ b/src/exchangedb/exchange-0001-part.sql @@ -3429,11 +3429,65 @@ CREATE OR REPLACE FUNCTION exchange_do_history_request( IN in_history_fee_val INT8, IN in_history_fee_frac INT4, OUT out_balance_ok BOOLEAN, - OUT out_conflict BOOLEAN) + OUT out_idempotent BOOLEAN) LANGUAGE plpgsql AS $$ BEGIN - -- FIXME + + -- Insert and check for idempotency. + INSERT INTO history_requests + (reserve_pub + ,request_timestamp + ,reserve_sig + ,history_fee_val + ,history_fee_frac) + VALUES + (in_reserve_pub + ,in_request_timestamp + ,in_reserve_sig + ,in_history_fee_val + ,in_history_fee_frac) + ON CONFLICT DO NOTHING; + + IF NOT FOUND + THEN + out_balance_ok=TRUE; + out_conflict=TRUE; + RETURN; + END IF; + + out_conflict=FALSE; + + -- Update reserve balance. + UPDATE reserves + SET + current_balance_frac=current_balance_frac-in_history_fee_frac + + CASE + WHEN reserve_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) ) ); + + IF NOT FOUND + THEN + -- Either reserve does not exist, or balance insufficient. + -- Both we treat the same here as balance insufficient. + out_balance_ok=FALSE; + RETURN; + END IF; + + out_balance_ok=TRUE; END $$; |