From cdaf1ce69b5fb56f09bbdc00942b03f039710614 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 9 Feb 2020 16:34:40 +0100 Subject: rename SQL files to make filenames consistent with versioning name --- src/auditordb/0000.sql | 293 ---------------------- src/auditordb/0001.sql | 239 ------------------ src/auditordb/Makefile.am | 4 +- src/auditordb/auditor-0000.sql | 293 ++++++++++++++++++++++ src/auditordb/auditor-0001.sql | 239 ++++++++++++++++++ src/auditordb/plugin_auditordb_postgres.c | 93 ++----- src/exchangedb/0000.sql | 293 ---------------------- src/exchangedb/0001.sql | 368 ---------------------------- src/exchangedb/Makefile.am | 4 +- src/exchangedb/exchange-0000.sql | 293 ++++++++++++++++++++++ src/exchangedb/exchange-0001.sql | 368 ++++++++++++++++++++++++++++ src/exchangedb/plugin_exchangedb_postgres.c | 68 ++--- 12 files changed, 1238 insertions(+), 1317 deletions(-) delete mode 100644 src/auditordb/0000.sql delete mode 100644 src/auditordb/0001.sql create mode 100644 src/auditordb/auditor-0000.sql create mode 100644 src/auditordb/auditor-0001.sql delete mode 100644 src/exchangedb/0000.sql delete mode 100644 src/exchangedb/0001.sql create mode 100644 src/exchangedb/exchange-0000.sql create mode 100644 src/exchangedb/exchange-0001.sql (limited to 'src') diff --git a/src/auditordb/0000.sql b/src/auditordb/0000.sql deleted file mode 100644 index 1483e2015..000000000 --- a/src/auditordb/0000.sql +++ /dev/null @@ -1,293 +0,0 @@ --- LICENSE AND COPYRIGHT --- --- Copyright (C) 2010 Hubert depesz Lubaczewski --- --- This program is distributed under the (Revised) BSD License: --- L --- --- Redistribution and use in source and binary forms, with or without --- modification, are permitted provided that the following conditions --- are met: --- --- * Redistributions of source code must retain the above copyright --- notice, this list of conditions and the following disclaimer. --- --- * Redistributions in binary form must reproduce the above copyright --- notice, this list of conditions and the following disclaimer in the --- documentation and/or other materials provided with the distribution. --- --- * Neither the name of Hubert depesz Lubaczewski's Organization --- nor the names of its contributors may be used to endorse or --- promote products derived from this software without specific --- prior written permission. --- --- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" --- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE --- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE --- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE --- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL --- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR --- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER --- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, --- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --- --- Code origin: https://gitlab.com/depesz/Versioning/blob/master/install.versioning.sql --- --- --- # NAME --- --- **Versioning** - simplistic take on tracking and applying changes to databases. --- --- # DESCRIPTION --- --- This project strives to provide simple way to manage changes to --- database. --- --- Instead of making changes on development server, then finding --- differences between production and development, deciding which ones --- should be installed on production, and finding a way to install them - --- you start with writing diffs themselves! --- --- # INSTALLATION --- --- To install versioning simply run install.versioning.sql in your database --- (all of them: production, stage, test, devel, ...). --- --- # USAGE --- --- In your files with patches to database, put whole logic in single --- transaction, and use \_v.\* functions - usually \_v.register_patch() at --- least to make sure everything is OK. --- --- For example. Let's assume you have patch files: --- --- ## 0001.sql: --- --- ``` --- create table users (id serial primary key, username text); --- ``` --- --- ## 0002.sql: --- --- ``` --- insert into users (username) values ('depesz'); --- ``` --- To change it to use versioning you would change the files, to this --- state: --- --- 0000.sql: --- --- ``` --- BEGIN; --- select _v.register_patch('000-base', NULL, NULL); --- create table users (id serial primary key, username text); --- COMMIT; --- ``` --- --- ## 0002.sql: --- --- ``` --- BEGIN; --- select _v.register_patch('001-users', ARRAY['000-base'], NULL); --- insert into users (username) values ('depesz'); --- COMMIT; --- ``` --- --- This will make sure that patch 001-users can only be applied after --- 000-base. --- --- # AVAILABLE FUNCTIONS --- --- ## \_v.register_patch( TEXT ) --- --- Registers named patch, or dies if it is already registered. --- --- Returns integer which is id of patch in \_v.patches table - only if it --- succeeded. --- --- ## \_v.register_patch( TEXT, TEXT[] ) --- --- Same as \_v.register_patch( TEXT ), but checks is all given patches (given as --- array in second argument) are already registered. --- --- ## \_v.register_patch( TEXT, TEXT[], TEXT[] ) --- --- Same as \_v.register_patch( TEXT, TEXT[] ), but also checks if there are no conflicts with preexisting patches. --- --- Third argument is array of names of patches that conflict with current one. So --- if any of them is installed - register_patch will error out. --- --- ## \_v.unregister_patch( TEXT ) --- --- Removes information about given patch from the versioning data. --- --- It doesn't remove objects that were created by this patch - just removes --- metainformation. --- --- ## \_v.assert_user_is_superuser() --- --- Make sure that current patch is being loaded by superuser. --- --- If it's not - it will raise exception, and break transaction. --- --- ## \_v.assert_user_is_not_superuser() --- --- Make sure that current patch is not being loaded by superuser. --- --- If it is - it will raise exception, and break transaction. --- --- ## \_v.assert_user_is_one_of(TEXT, TEXT, ... ) --- --- Make sure that current patch is being loaded by one of listed users. --- --- If ```current_user``` is not listed as one of arguments - function will raise --- exception and break the transaction. - -BEGIN; - --- This file adds versioning support to database it will be loaded to. --- It requires that PL/pgSQL is already loaded - will raise exception otherwise. --- All versioning "stuff" (tables, functions) is in "_v" schema. - --- All functions are defined as 'RETURNS SETOF INT4' to be able to make them to RETURN literaly nothing (0 rows). --- >> RETURNS VOID<< IS similar, but it still outputs "empty line" in psql when calling. -CREATE SCHEMA IF NOT EXISTS _v; -COMMENT ON SCHEMA _v IS 'Schema for versioning data and functionality.'; - -CREATE TABLE IF NOT EXISTS _v.patches ( - patch_name TEXT PRIMARY KEY, - applied_tsz TIMESTAMPTZ NOT NULL DEFAULT now(), - applied_by TEXT NOT NULL, - requires TEXT[], - conflicts TEXT[] -); -COMMENT ON TABLE _v.patches IS 'Contains information about what patches are currently applied on database.'; -COMMENT ON COLUMN _v.patches.patch_name IS 'Name of patch, has to be unique for every patch.'; -COMMENT ON COLUMN _v.patches.applied_tsz IS 'When the patch was applied.'; -COMMENT ON COLUMN _v.patches.applied_by IS 'Who applied this patch (PostgreSQL username)'; -COMMENT ON COLUMN _v.patches.requires IS 'List of patches that are required for given patch.'; -COMMENT ON COLUMN _v.patches.conflicts IS 'List of patches that conflict with given patch.'; - -CREATE OR REPLACE FUNCTION _v.register_patch( IN in_patch_name TEXT, IN in_requirements TEXT[], in_conflicts TEXT[], OUT versioning INT4 ) RETURNS setof INT4 AS $$ -DECLARE - t_text TEXT; - t_text_a TEXT[]; - i INT4; -BEGIN - -- Thanks to this we know only one patch will be applied at a time - LOCK TABLE _v.patches IN EXCLUSIVE MODE; - - SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_patch_name; - IF FOUND THEN - RAISE EXCEPTION 'Patch % is already applied!', in_patch_name; - END IF; - - t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE patch_name = any( in_conflicts ) ); - IF array_upper( t_text_a, 1 ) IS NOT NULL THEN - RAISE EXCEPTION 'Versioning patches conflict. Conflicting patche(s) installed: %.', array_to_string( t_text_a, ', ' ); - END IF; - - IF array_upper( in_requirements, 1 ) IS NOT NULL THEN - t_text_a := '{}'; - FOR i IN array_lower( in_requirements, 1 ) .. array_upper( in_requirements, 1 ) LOOP - SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_requirements[i]; - IF NOT FOUND THEN - t_text_a := t_text_a || in_requirements[i]; - END IF; - END LOOP; - IF array_upper( t_text_a, 1 ) IS NOT NULL THEN - RAISE EXCEPTION 'Missing prerequisite(s): %.', array_to_string( t_text_a, ', ' ); - END IF; - END IF; - - INSERT INTO _v.patches (patch_name, applied_tsz, applied_by, requires, conflicts ) VALUES ( in_patch_name, now(), current_user, coalesce( in_requirements, '{}' ), coalesce( in_conflicts, '{}' ) ); - RETURN; -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[], TEXT[] ) IS 'Function to register patches in database. Raises exception if there are conflicts, prerequisites are not installed or the migration has already been installed.'; - -CREATE OR REPLACE FUNCTION _v.register_patch( TEXT, TEXT[] ) RETURNS setof INT4 AS $$ - SELECT _v.register_patch( $1, $2, NULL ); -$$ language sql; -COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[] ) IS 'Wrapper to allow registration of patches without conflicts.'; -CREATE OR REPLACE FUNCTION _v.register_patch( TEXT ) RETURNS setof INT4 AS $$ - SELECT _v.register_patch( $1, NULL, NULL ); -$$ language sql; -COMMENT ON FUNCTION _v.register_patch( TEXT ) IS 'Wrapper to allow registration of patches without requirements and conflicts.'; - -CREATE OR REPLACE FUNCTION _v.unregister_patch( IN in_patch_name TEXT, OUT versioning INT4 ) RETURNS setof INT4 AS $$ -DECLARE - i INT4; - t_text_a TEXT[]; -BEGIN - -- Thanks to this we know only one patch will be applied at a time - LOCK TABLE _v.patches IN EXCLUSIVE MODE; - - t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE in_patch_name = ANY( requires ) ); - IF array_upper( t_text_a, 1 ) IS NOT NULL THEN - RAISE EXCEPTION 'Cannot uninstall %, as it is required by: %.', in_patch_name, array_to_string( t_text_a, ', ' ); - END IF; - - DELETE FROM _v.patches WHERE patch_name = in_patch_name; - GET DIAGNOSTICS i = ROW_COUNT; - IF i < 1 THEN - RAISE EXCEPTION 'Patch % is not installed, so it can''t be uninstalled!', in_patch_name; - END IF; - - RETURN; -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.unregister_patch( TEXT ) IS 'Function to unregister patches in database. Dies if the patch is not registered, or if unregistering it would break dependencies.'; - -CREATE OR REPLACE FUNCTION _v.assert_patch_is_applied( IN in_patch_name TEXT ) RETURNS TEXT as $$ -DECLARE - t_text TEXT; -BEGIN - SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_patch_name; - IF NOT FOUND THEN - RAISE EXCEPTION 'Patch % is not applied!', in_patch_name; - END IF; - RETURN format('Patch %s is applied.', in_patch_name); -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.assert_patch_is_applied( TEXT ) IS 'Function that can be used to make sure that patch has been applied.'; - -CREATE OR REPLACE FUNCTION _v.assert_user_is_superuser() RETURNS TEXT as $$ -DECLARE - v_super bool; -BEGIN - SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user; - IF v_super THEN - RETURN 'assert_user_is_superuser: OK'; - END IF; - RAISE EXCEPTION 'Current user is not superuser - cannot continue.'; -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.assert_user_is_superuser() IS 'Function that can be used to make sure that patch is being applied using superuser account.'; - -CREATE OR REPLACE FUNCTION _v.assert_user_is_not_superuser() RETURNS TEXT as $$ -DECLARE - v_super bool; -BEGIN - SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user; - IF v_super THEN - RAISE EXCEPTION 'Current user is superuser - cannot continue.'; - END IF; - RETURN 'assert_user_is_not_superuser: OK'; -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.assert_user_is_not_superuser() IS 'Function that can be used to make sure that patch is being applied using normal (not superuser) account.'; - -CREATE OR REPLACE FUNCTION _v.assert_user_is_one_of(VARIADIC p_acceptable_users TEXT[] ) RETURNS TEXT as $$ -DECLARE -BEGIN - IF current_user = any( p_acceptable_users ) THEN - RETURN 'assert_user_is_one_of: OK'; - END IF; - RAISE EXCEPTION 'User is not one of: % - cannot continue.', p_acceptable_users; -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.assert_user_is_one_of(TEXT[]) IS 'Function that can be used to make sure that patch is being applied by one of defined users.'; - -COMMIT; diff --git a/src/auditordb/0001.sql b/src/auditordb/0001.sql deleted file mode 100644 index 3e666519c..000000000 --- a/src/auditordb/0001.sql +++ /dev/null @@ -1,239 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2014--2020 Taler Systems SA --- --- TALER is free software; you can redistribute it and/or modify it under the --- terms of the GNU General Public License as published by the Free Software --- Foundation; either version 3, or (at your option) any later version. --- --- TALER is distributed in the hope that it will be useful, but WITHOUT ANY --- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR --- A PARTICULAR PURPOSE. See the GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License along with --- TALER; see the file COPYING. If not, see --- - --- Everything in one big transaction -BEGIN; - --- Check patch versioning is in place. -SELECT _v.register_patch('auditor-0001', NULL, NULL); - - -CREATE TABLE IF NOT EXISTS auditor_exchanges - (master_pub BYTEA PRIMARY KEY CHECK (LENGTH(master_pub)=32) - ,exchange_url VARCHAR NOT NULL - ); --- Table with list of signing keys of exchanges we are auditing -CREATE TABLE IF NOT EXISTS auditor_exchange_signkeys - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,ep_start INT8 NOT NULL - ,ep_expire INT8 NOT NULL - ,ep_end INT8 NOT NULL - ,exchange_pub BYTEA NOT NULL CHECK (LENGTH(exchange_pub)=32) - ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64) - ); --- Table with all of the denomination keys that the auditor --- is aware of. -CREATE TABLE IF NOT EXISTS auditor_denominations - (denom_pub_hash BYTEA PRIMARY KEY CHECK (LENGTH(denom_pub_hash)=64) - ,master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,valid_from INT8 NOT NULL - ,expire_withdraw INT8 NOT NULL - ,expire_deposit INT8 NOT NULL - ,expire_legal INT8 NOT NULL - ,coin_val INT8 NOT NULL - ,coin_frac INT4 NOT NULL - ,fee_withdraw_val INT8 NOT NULL - ,fee_withdraw_frac INT4 NOT NULL - ,fee_deposit_val INT8 NOT NULL - ,fee_deposit_frac INT4 NOT NULL - ,fee_refresh_val INT8 NOT NULL - ,fee_refresh_frac INT4 NOT NULL - ,fee_refund_val INT8 NOT NULL - ,fee_refund_frac INT4 NOT NULL - ); --- Table indicating up to which transactions the auditor has --- processed the exchange database. Used for SELECTing the --- statements to process. The indices below include the last --- serial ID from the respective tables that we have --- processed. Thus, we need to select those table entries that are --- strictly larger (and process in monotonically increasing --- order). -CREATE TABLE IF NOT EXISTS auditor_progress_reserve - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,last_reserve_in_serial_id INT8 NOT NULL DEFAULT 0 - ,last_reserve_out_serial_id INT8 NOT NULL DEFAULT 0 - ,last_reserve_recoup_serial_id INT8 NOT NULL DEFAULT 0 - ,last_reserve_close_serial_id INT8 NOT NULL DEFAULT 0 - ); -CREATE TABLE IF NOT EXISTS auditor_progress_aggregation - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,last_wire_out_serial_id INT8 NOT NULL DEFAULT 0 - ); -CREATE TABLE IF NOT EXISTS auditor_progress_deposit_confirmation - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,last_deposit_confirmation_serial_id INT8 NOT NULL DEFAULT 0 - ); -CREATE TABLE IF NOT EXISTS auditor_progress_coin - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,last_withdraw_serial_id INT8 NOT NULL DEFAULT 0 - ,last_deposit_serial_id INT8 NOT NULL DEFAULT 0 - ,last_melt_serial_id INT8 NOT NULL DEFAULT 0 - ,last_refund_serial_id INT8 NOT NULL DEFAULT 0 - ,last_recoup_serial_id INT8 NOT NULL DEFAULT 0 - ,last_recoup_refresh_serial_id INT8 NOT NULL DEFAULT 0 - ); -CREATE TABLE IF NOT EXISTS wire_auditor_account_progress - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,account_name TEXT NOT NULL - ,last_wire_reserve_in_serial_id INT8 NOT NULL DEFAULT 0 - ,last_wire_wire_out_serial_id INT8 NOT NULL DEFAULT 0 - ,wire_in_off INT8 - ,wire_out_off INT8 - ); -CREATE TABLE IF NOT EXISTS wire_auditor_progress - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,last_timestamp INT8 NOT NULL - ,last_reserve_close_uuid INT8 NOT NULL - ); --- Table with all of the customer reserves and their respective --- balances that the auditor is aware of. --- last_reserve_out_serial_id marks the last withdrawal from --- reserves_out about this reserve that the auditor is aware of, --- and last_reserve_in_serial_id is the last reserve_in --- operation about this reserve that the auditor is aware of. -CREATE TABLE IF NOT EXISTS auditor_reserves - (reserve_pub BYTEA NOT NULL CHECK(LENGTH(reserve_pub)=32) - ,master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,reserve_balance_val INT8 NOT NULL - ,reserve_balance_frac INT4 NOT NULL - ,withdraw_fee_balance_val INT8 NOT NULL - ,withdraw_fee_balance_frac INT4 NOT NULL - ,expiration_date INT8 NOT NULL - ,auditor_reserves_rowid BIGSERIAL UNIQUE - ,origin_account TEXT - ); -CREATE INDEX IF NOT EXISTS auditor_reserves_by_reserve_pub - ON auditor_reserves - (reserve_pub); --- Table with the sum of the balances of all customer reserves --- (by exchange's master public key) -CREATE TABLE IF NOT EXISTS auditor_reserve_balance - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,reserve_balance_val INT8 NOT NULL - ,reserve_balance_frac INT4 NOT NULL - ,withdraw_fee_balance_val INT8 NOT NULL - ,withdraw_fee_balance_frac INT4 NOT NULL - ); --- Table with the sum of the balances of all wire fees --- (by exchange's master public key) -CREATE TABLE IF NOT EXISTS auditor_wire_fee_balance - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,wire_fee_balance_val INT8 NOT NULL - ,wire_fee_balance_frac INT4 NOT NULL - ); --- Table with all of the outstanding denomination coins that the --- exchange is aware of and what the respective balances are --- (outstanding as well as issued overall which implies the --- maximum value at risk). We also count the number of coins --- issued (withdraw, refresh-reveal) and the number of coins seen --- at the exchange (refresh-commit, deposit), not just the amounts. */GNUNET_PQ_make_execute ( -CREATE TABLE IF NOT EXISTS auditor_denomination_pending - (denom_pub_hash BYTEA PRIMARY KEY REFERENCES auditor_denominations (denom_pub_hash) ON DELETE CASCADE - ,denom_balance_val INT8 NOT NULL - ,denom_balance_frac INT4 NOT NULL - ,denom_loss_val INT8 NOT NULL - ,denom_loss_frac INT4 NOT NULL - ,num_issued INT8 NOT NULL - ,denom_risk_val INT8 NOT NULL - ,denom_risk_frac INT4 NOT NULL - ,recoup_loss_val INT8 NOT NULL - ,recoup_loss_frac INT4 NOT NULL - ); --- Table with the sum of the outstanding coins from --- auditor_denomination_pending (denom_pubs must belong to the --- respective's exchange's master public key); it represents the --- auditor_balance_summary of the exchange at this point (modulo --- unexpected historic_loss-style events where denomination keys are --- compromised) -CREATE TABLE IF NOT EXISTS auditor_balance_summary - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,denom_balance_val INT8 NOT NULL - ,denom_balance_frac INT4 NOT NULL - ,deposit_fee_balance_val INT8 NOT NULL - ,deposit_fee_balance_frac INT4 NOT NULL - ,melt_fee_balance_val INT8 NOT NULL - ,melt_fee_balance_frac INT4 NOT NULL - ,refund_fee_balance_val INT8 NOT NULL - ,refund_fee_balance_frac INT4 NOT NULL - ,risk_val INT8 NOT NULL - ,risk_frac INT4 NOT NULL - ,loss_val INT8 NOT NULL - ,loss_frac INT4 NOT NULL - ,irregular_recoup_val INT8 NOT NULL - ,irregular_recoup_frac INT4 NOT NULL - ); --- Table with historic profits; basically, when a denom_pub has --- expired and everything associated with it is garbage collected, --- the final profits end up in here; note that the denom_pub here --- is not a foreign key, we just keep it as a reference point. --- revenue_balance is the sum of all of the profits we made on the --- coin except for withdraw fees (which are in --- historic_reserve_revenue); the deposit, melt and refund fees are given --- individually; the delta to the revenue_balance is from coins that --- were withdrawn but never deposited prior to expiration. -CREATE TABLE IF NOT EXISTS auditor_historic_denomination_revenue - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,denom_pub_hash BYTEA PRIMARY KEY CHECK (LENGTH(denom_pub_hash)=64) - ,revenue_timestamp INT8 NOT NULL - ,revenue_balance_val INT8 NOT NULL - ,revenue_balance_frac INT4 NOT NULL - ,loss_balance_val INT8 NOT NULL - ,loss_balance_frac INT4 NOT NULL - ); --- Table with historic profits from reserves; we eventually --- GC auditor_historic_reserve_revenue, and then store the totals --- in here (by time intervals). -CREATE TABLE IF NOT EXISTS auditor_historic_reserve_summary - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,start_date INT8 NOT NULL - ,end_date INT8 NOT NULL - ,reserve_profits_val INT8 NOT NULL - ,reserve_profits_frac INT4 NOT NULL - ); -CREATE INDEX IF NOT EXISTS auditor_historic_reserve_summary_by_master_pub_start_date - ON auditor_historic_reserve_summary - (master_pub - ,start_date); --- Table with deposit confirmation sent to us by merchants; --- we must check that the exchange reported these properly. -CREATE TABLE IF NOT EXISTS deposit_confirmations - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,serial_id BIGSERIAL UNIQUE - ,h_contract_terms BYTEA CHECK (LENGTH(h_contract_terms)=64) - ,h_wire BYTEA CHECK (LENGTH(h_wire)=64) - ,timestamp INT8 NOT NULL - ,refund_deadline INT8 NOT NULL - ,amount_without_fee_val INT8 NOT NULL - ,amount_without_fee_frac INT4 NOT NULL - ,coin_pub BYTEA CHECK (LENGTH(coin_pub)=32) - ,merchant_pub BYTEA CHECK (LENGTH(merchant_pub)=32) - ,exchange_sig BYTEA CHECK (LENGTH(exchange_sig)=64) - ,exchange_pub BYTEA CHECK (LENGTH(exchange_pub)=32) - ,master_sig BYTEA CHECK (LENGTH(master_sig)=64) - ,PRIMARY KEY (h_contract_terms,h_wire,coin_pub,merchant_pub,exchange_sig,exchange_pub,master_sig) - ); --- Table with the sum of the ledger, auditor_historic_revenue and --- the auditor_reserve_balance. This is the --- final amount that the exchange should have in its bank account --- right now. -CREATE TABLE IF NOT EXISTS auditor_predicted_result - (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE - ,balance_val INT8 NOT NULL - ,balance_frac INT4 NOT NULL - ); - --- Finally, commit everything -COMMIT; diff --git a/src/auditordb/Makefile.am b/src/auditordb/Makefile.am index 1378c5496..3efdc200e 100644 --- a/src/auditordb/Makefile.am +++ b/src/auditordb/Makefile.am @@ -14,8 +14,8 @@ pkgcfg_DATA = \ sqldir = $(prefix)/share/taler/sql/auditor/ sql_DATA = \ - 0000.sql \ - 0001.sql \ + auditor-0000.sql \ + auditor-0001.sql \ drop0000.sql \ restart0000.sql diff --git a/src/auditordb/auditor-0000.sql b/src/auditordb/auditor-0000.sql new file mode 100644 index 000000000..1483e2015 --- /dev/null +++ b/src/auditordb/auditor-0000.sql @@ -0,0 +1,293 @@ +-- LICENSE AND COPYRIGHT +-- +-- Copyright (C) 2010 Hubert depesz Lubaczewski +-- +-- This program is distributed under the (Revised) BSD License: +-- L +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions +-- are met: +-- +-- * Redistributions of source code must retain the above copyright +-- notice, this list of conditions and the following disclaimer. +-- +-- * Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- +-- * Neither the name of Hubert depesz Lubaczewski's Organization +-- nor the names of its contributors may be used to endorse or +-- promote products derived from this software without specific +-- prior written permission. +-- +-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE +-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +-- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +-- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +-- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +-- +-- Code origin: https://gitlab.com/depesz/Versioning/blob/master/install.versioning.sql +-- +-- +-- # NAME +-- +-- **Versioning** - simplistic take on tracking and applying changes to databases. +-- +-- # DESCRIPTION +-- +-- This project strives to provide simple way to manage changes to +-- database. +-- +-- Instead of making changes on development server, then finding +-- differences between production and development, deciding which ones +-- should be installed on production, and finding a way to install them - +-- you start with writing diffs themselves! +-- +-- # INSTALLATION +-- +-- To install versioning simply run install.versioning.sql in your database +-- (all of them: production, stage, test, devel, ...). +-- +-- # USAGE +-- +-- In your files with patches to database, put whole logic in single +-- transaction, and use \_v.\* functions - usually \_v.register_patch() at +-- least to make sure everything is OK. +-- +-- For example. Let's assume you have patch files: +-- +-- ## 0001.sql: +-- +-- ``` +-- create table users (id serial primary key, username text); +-- ``` +-- +-- ## 0002.sql: +-- +-- ``` +-- insert into users (username) values ('depesz'); +-- ``` +-- To change it to use versioning you would change the files, to this +-- state: +-- +-- 0000.sql: +-- +-- ``` +-- BEGIN; +-- select _v.register_patch('000-base', NULL, NULL); +-- create table users (id serial primary key, username text); +-- COMMIT; +-- ``` +-- +-- ## 0002.sql: +-- +-- ``` +-- BEGIN; +-- select _v.register_patch('001-users', ARRAY['000-base'], NULL); +-- insert into users (username) values ('depesz'); +-- COMMIT; +-- ``` +-- +-- This will make sure that patch 001-users can only be applied after +-- 000-base. +-- +-- # AVAILABLE FUNCTIONS +-- +-- ## \_v.register_patch( TEXT ) +-- +-- Registers named patch, or dies if it is already registered. +-- +-- Returns integer which is id of patch in \_v.patches table - only if it +-- succeeded. +-- +-- ## \_v.register_patch( TEXT, TEXT[] ) +-- +-- Same as \_v.register_patch( TEXT ), but checks is all given patches (given as +-- array in second argument) are already registered. +-- +-- ## \_v.register_patch( TEXT, TEXT[], TEXT[] ) +-- +-- Same as \_v.register_patch( TEXT, TEXT[] ), but also checks if there are no conflicts with preexisting patches. +-- +-- Third argument is array of names of patches that conflict with current one. So +-- if any of them is installed - register_patch will error out. +-- +-- ## \_v.unregister_patch( TEXT ) +-- +-- Removes information about given patch from the versioning data. +-- +-- It doesn't remove objects that were created by this patch - just removes +-- metainformation. +-- +-- ## \_v.assert_user_is_superuser() +-- +-- Make sure that current patch is being loaded by superuser. +-- +-- If it's not - it will raise exception, and break transaction. +-- +-- ## \_v.assert_user_is_not_superuser() +-- +-- Make sure that current patch is not being loaded by superuser. +-- +-- If it is - it will raise exception, and break transaction. +-- +-- ## \_v.assert_user_is_one_of(TEXT, TEXT, ... ) +-- +-- Make sure that current patch is being loaded by one of listed users. +-- +-- If ```current_user``` is not listed as one of arguments - function will raise +-- exception and break the transaction. + +BEGIN; + +-- This file adds versioning support to database it will be loaded to. +-- It requires that PL/pgSQL is already loaded - will raise exception otherwise. +-- All versioning "stuff" (tables, functions) is in "_v" schema. + +-- All functions are defined as 'RETURNS SETOF INT4' to be able to make them to RETURN literaly nothing (0 rows). +-- >> RETURNS VOID<< IS similar, but it still outputs "empty line" in psql when calling. +CREATE SCHEMA IF NOT EXISTS _v; +COMMENT ON SCHEMA _v IS 'Schema for versioning data and functionality.'; + +CREATE TABLE IF NOT EXISTS _v.patches ( + patch_name TEXT PRIMARY KEY, + applied_tsz TIMESTAMPTZ NOT NULL DEFAULT now(), + applied_by TEXT NOT NULL, + requires TEXT[], + conflicts TEXT[] +); +COMMENT ON TABLE _v.patches IS 'Contains information about what patches are currently applied on database.'; +COMMENT ON COLUMN _v.patches.patch_name IS 'Name of patch, has to be unique for every patch.'; +COMMENT ON COLUMN _v.patches.applied_tsz IS 'When the patch was applied.'; +COMMENT ON COLUMN _v.patches.applied_by IS 'Who applied this patch (PostgreSQL username)'; +COMMENT ON COLUMN _v.patches.requires IS 'List of patches that are required for given patch.'; +COMMENT ON COLUMN _v.patches.conflicts IS 'List of patches that conflict with given patch.'; + +CREATE OR REPLACE FUNCTION _v.register_patch( IN in_patch_name TEXT, IN in_requirements TEXT[], in_conflicts TEXT[], OUT versioning INT4 ) RETURNS setof INT4 AS $$ +DECLARE + t_text TEXT; + t_text_a TEXT[]; + i INT4; +BEGIN + -- Thanks to this we know only one patch will be applied at a time + LOCK TABLE _v.patches IN EXCLUSIVE MODE; + + SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_patch_name; + IF FOUND THEN + RAISE EXCEPTION 'Patch % is already applied!', in_patch_name; + END IF; + + t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE patch_name = any( in_conflicts ) ); + IF array_upper( t_text_a, 1 ) IS NOT NULL THEN + RAISE EXCEPTION 'Versioning patches conflict. Conflicting patche(s) installed: %.', array_to_string( t_text_a, ', ' ); + END IF; + + IF array_upper( in_requirements, 1 ) IS NOT NULL THEN + t_text_a := '{}'; + FOR i IN array_lower( in_requirements, 1 ) .. array_upper( in_requirements, 1 ) LOOP + SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_requirements[i]; + IF NOT FOUND THEN + t_text_a := t_text_a || in_requirements[i]; + END IF; + END LOOP; + IF array_upper( t_text_a, 1 ) IS NOT NULL THEN + RAISE EXCEPTION 'Missing prerequisite(s): %.', array_to_string( t_text_a, ', ' ); + END IF; + END IF; + + INSERT INTO _v.patches (patch_name, applied_tsz, applied_by, requires, conflicts ) VALUES ( in_patch_name, now(), current_user, coalesce( in_requirements, '{}' ), coalesce( in_conflicts, '{}' ) ); + RETURN; +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[], TEXT[] ) IS 'Function to register patches in database. Raises exception if there are conflicts, prerequisites are not installed or the migration has already been installed.'; + +CREATE OR REPLACE FUNCTION _v.register_patch( TEXT, TEXT[] ) RETURNS setof INT4 AS $$ + SELECT _v.register_patch( $1, $2, NULL ); +$$ language sql; +COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[] ) IS 'Wrapper to allow registration of patches without conflicts.'; +CREATE OR REPLACE FUNCTION _v.register_patch( TEXT ) RETURNS setof INT4 AS $$ + SELECT _v.register_patch( $1, NULL, NULL ); +$$ language sql; +COMMENT ON FUNCTION _v.register_patch( TEXT ) IS 'Wrapper to allow registration of patches without requirements and conflicts.'; + +CREATE OR REPLACE FUNCTION _v.unregister_patch( IN in_patch_name TEXT, OUT versioning INT4 ) RETURNS setof INT4 AS $$ +DECLARE + i INT4; + t_text_a TEXT[]; +BEGIN + -- Thanks to this we know only one patch will be applied at a time + LOCK TABLE _v.patches IN EXCLUSIVE MODE; + + t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE in_patch_name = ANY( requires ) ); + IF array_upper( t_text_a, 1 ) IS NOT NULL THEN + RAISE EXCEPTION 'Cannot uninstall %, as it is required by: %.', in_patch_name, array_to_string( t_text_a, ', ' ); + END IF; + + DELETE FROM _v.patches WHERE patch_name = in_patch_name; + GET DIAGNOSTICS i = ROW_COUNT; + IF i < 1 THEN + RAISE EXCEPTION 'Patch % is not installed, so it can''t be uninstalled!', in_patch_name; + END IF; + + RETURN; +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.unregister_patch( TEXT ) IS 'Function to unregister patches in database. Dies if the patch is not registered, or if unregistering it would break dependencies.'; + +CREATE OR REPLACE FUNCTION _v.assert_patch_is_applied( IN in_patch_name TEXT ) RETURNS TEXT as $$ +DECLARE + t_text TEXT; +BEGIN + SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_patch_name; + IF NOT FOUND THEN + RAISE EXCEPTION 'Patch % is not applied!', in_patch_name; + END IF; + RETURN format('Patch %s is applied.', in_patch_name); +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.assert_patch_is_applied( TEXT ) IS 'Function that can be used to make sure that patch has been applied.'; + +CREATE OR REPLACE FUNCTION _v.assert_user_is_superuser() RETURNS TEXT as $$ +DECLARE + v_super bool; +BEGIN + SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user; + IF v_super THEN + RETURN 'assert_user_is_superuser: OK'; + END IF; + RAISE EXCEPTION 'Current user is not superuser - cannot continue.'; +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.assert_user_is_superuser() IS 'Function that can be used to make sure that patch is being applied using superuser account.'; + +CREATE OR REPLACE FUNCTION _v.assert_user_is_not_superuser() RETURNS TEXT as $$ +DECLARE + v_super bool; +BEGIN + SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user; + IF v_super THEN + RAISE EXCEPTION 'Current user is superuser - cannot continue.'; + END IF; + RETURN 'assert_user_is_not_superuser: OK'; +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.assert_user_is_not_superuser() IS 'Function that can be used to make sure that patch is being applied using normal (not superuser) account.'; + +CREATE OR REPLACE FUNCTION _v.assert_user_is_one_of(VARIADIC p_acceptable_users TEXT[] ) RETURNS TEXT as $$ +DECLARE +BEGIN + IF current_user = any( p_acceptable_users ) THEN + RETURN 'assert_user_is_one_of: OK'; + END IF; + RAISE EXCEPTION 'User is not one of: % - cannot continue.', p_acceptable_users; +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.assert_user_is_one_of(TEXT[]) IS 'Function that can be used to make sure that patch is being applied by one of defined users.'; + +COMMIT; diff --git a/src/auditordb/auditor-0001.sql b/src/auditordb/auditor-0001.sql new file mode 100644 index 000000000..3e666519c --- /dev/null +++ b/src/auditordb/auditor-0001.sql @@ -0,0 +1,239 @@ +-- +-- This file is part of TALER +-- Copyright (C) 2014--2020 Taler Systems SA +-- +-- TALER is free software; you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation; either version 3, or (at your option) any later version. +-- +-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +-- A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- TALER; see the file COPYING. If not, see +-- + +-- Everything in one big transaction +BEGIN; + +-- Check patch versioning is in place. +SELECT _v.register_patch('auditor-0001', NULL, NULL); + + +CREATE TABLE IF NOT EXISTS auditor_exchanges + (master_pub BYTEA PRIMARY KEY CHECK (LENGTH(master_pub)=32) + ,exchange_url VARCHAR NOT NULL + ); +-- Table with list of signing keys of exchanges we are auditing +CREATE TABLE IF NOT EXISTS auditor_exchange_signkeys + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,ep_start INT8 NOT NULL + ,ep_expire INT8 NOT NULL + ,ep_end INT8 NOT NULL + ,exchange_pub BYTEA NOT NULL CHECK (LENGTH(exchange_pub)=32) + ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64) + ); +-- Table with all of the denomination keys that the auditor +-- is aware of. +CREATE TABLE IF NOT EXISTS auditor_denominations + (denom_pub_hash BYTEA PRIMARY KEY CHECK (LENGTH(denom_pub_hash)=64) + ,master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,valid_from INT8 NOT NULL + ,expire_withdraw INT8 NOT NULL + ,expire_deposit INT8 NOT NULL + ,expire_legal INT8 NOT NULL + ,coin_val INT8 NOT NULL + ,coin_frac INT4 NOT NULL + ,fee_withdraw_val INT8 NOT NULL + ,fee_withdraw_frac INT4 NOT NULL + ,fee_deposit_val INT8 NOT NULL + ,fee_deposit_frac INT4 NOT NULL + ,fee_refresh_val INT8 NOT NULL + ,fee_refresh_frac INT4 NOT NULL + ,fee_refund_val INT8 NOT NULL + ,fee_refund_frac INT4 NOT NULL + ); +-- Table indicating up to which transactions the auditor has +-- processed the exchange database. Used for SELECTing the +-- statements to process. The indices below include the last +-- serial ID from the respective tables that we have +-- processed. Thus, we need to select those table entries that are +-- strictly larger (and process in monotonically increasing +-- order). +CREATE TABLE IF NOT EXISTS auditor_progress_reserve + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,last_reserve_in_serial_id INT8 NOT NULL DEFAULT 0 + ,last_reserve_out_serial_id INT8 NOT NULL DEFAULT 0 + ,last_reserve_recoup_serial_id INT8 NOT NULL DEFAULT 0 + ,last_reserve_close_serial_id INT8 NOT NULL DEFAULT 0 + ); +CREATE TABLE IF NOT EXISTS auditor_progress_aggregation + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,last_wire_out_serial_id INT8 NOT NULL DEFAULT 0 + ); +CREATE TABLE IF NOT EXISTS auditor_progress_deposit_confirmation + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,last_deposit_confirmation_serial_id INT8 NOT NULL DEFAULT 0 + ); +CREATE TABLE IF NOT EXISTS auditor_progress_coin + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,last_withdraw_serial_id INT8 NOT NULL DEFAULT 0 + ,last_deposit_serial_id INT8 NOT NULL DEFAULT 0 + ,last_melt_serial_id INT8 NOT NULL DEFAULT 0 + ,last_refund_serial_id INT8 NOT NULL DEFAULT 0 + ,last_recoup_serial_id INT8 NOT NULL DEFAULT 0 + ,last_recoup_refresh_serial_id INT8 NOT NULL DEFAULT 0 + ); +CREATE TABLE IF NOT EXISTS wire_auditor_account_progress + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,account_name TEXT NOT NULL + ,last_wire_reserve_in_serial_id INT8 NOT NULL DEFAULT 0 + ,last_wire_wire_out_serial_id INT8 NOT NULL DEFAULT 0 + ,wire_in_off INT8 + ,wire_out_off INT8 + ); +CREATE TABLE IF NOT EXISTS wire_auditor_progress + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,last_timestamp INT8 NOT NULL + ,last_reserve_close_uuid INT8 NOT NULL + ); +-- Table with all of the customer reserves and their respective +-- balances that the auditor is aware of. +-- last_reserve_out_serial_id marks the last withdrawal from +-- reserves_out about this reserve that the auditor is aware of, +-- and last_reserve_in_serial_id is the last reserve_in +-- operation about this reserve that the auditor is aware of. +CREATE TABLE IF NOT EXISTS auditor_reserves + (reserve_pub BYTEA NOT NULL CHECK(LENGTH(reserve_pub)=32) + ,master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,reserve_balance_val INT8 NOT NULL + ,reserve_balance_frac INT4 NOT NULL + ,withdraw_fee_balance_val INT8 NOT NULL + ,withdraw_fee_balance_frac INT4 NOT NULL + ,expiration_date INT8 NOT NULL + ,auditor_reserves_rowid BIGSERIAL UNIQUE + ,origin_account TEXT + ); +CREATE INDEX IF NOT EXISTS auditor_reserves_by_reserve_pub + ON auditor_reserves + (reserve_pub); +-- Table with the sum of the balances of all customer reserves +-- (by exchange's master public key) +CREATE TABLE IF NOT EXISTS auditor_reserve_balance + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,reserve_balance_val INT8 NOT NULL + ,reserve_balance_frac INT4 NOT NULL + ,withdraw_fee_balance_val INT8 NOT NULL + ,withdraw_fee_balance_frac INT4 NOT NULL + ); +-- Table with the sum of the balances of all wire fees +-- (by exchange's master public key) +CREATE TABLE IF NOT EXISTS auditor_wire_fee_balance + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,wire_fee_balance_val INT8 NOT NULL + ,wire_fee_balance_frac INT4 NOT NULL + ); +-- Table with all of the outstanding denomination coins that the +-- exchange is aware of and what the respective balances are +-- (outstanding as well as issued overall which implies the +-- maximum value at risk). We also count the number of coins +-- issued (withdraw, refresh-reveal) and the number of coins seen +-- at the exchange (refresh-commit, deposit), not just the amounts. */GNUNET_PQ_make_execute ( +CREATE TABLE IF NOT EXISTS auditor_denomination_pending + (denom_pub_hash BYTEA PRIMARY KEY REFERENCES auditor_denominations (denom_pub_hash) ON DELETE CASCADE + ,denom_balance_val INT8 NOT NULL + ,denom_balance_frac INT4 NOT NULL + ,denom_loss_val INT8 NOT NULL + ,denom_loss_frac INT4 NOT NULL + ,num_issued INT8 NOT NULL + ,denom_risk_val INT8 NOT NULL + ,denom_risk_frac INT4 NOT NULL + ,recoup_loss_val INT8 NOT NULL + ,recoup_loss_frac INT4 NOT NULL + ); +-- Table with the sum of the outstanding coins from +-- auditor_denomination_pending (denom_pubs must belong to the +-- respective's exchange's master public key); it represents the +-- auditor_balance_summary of the exchange at this point (modulo +-- unexpected historic_loss-style events where denomination keys are +-- compromised) +CREATE TABLE IF NOT EXISTS auditor_balance_summary + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,denom_balance_val INT8 NOT NULL + ,denom_balance_frac INT4 NOT NULL + ,deposit_fee_balance_val INT8 NOT NULL + ,deposit_fee_balance_frac INT4 NOT NULL + ,melt_fee_balance_val INT8 NOT NULL + ,melt_fee_balance_frac INT4 NOT NULL + ,refund_fee_balance_val INT8 NOT NULL + ,refund_fee_balance_frac INT4 NOT NULL + ,risk_val INT8 NOT NULL + ,risk_frac INT4 NOT NULL + ,loss_val INT8 NOT NULL + ,loss_frac INT4 NOT NULL + ,irregular_recoup_val INT8 NOT NULL + ,irregular_recoup_frac INT4 NOT NULL + ); +-- Table with historic profits; basically, when a denom_pub has +-- expired and everything associated with it is garbage collected, +-- the final profits end up in here; note that the denom_pub here +-- is not a foreign key, we just keep it as a reference point. +-- revenue_balance is the sum of all of the profits we made on the +-- coin except for withdraw fees (which are in +-- historic_reserve_revenue); the deposit, melt and refund fees are given +-- individually; the delta to the revenue_balance is from coins that +-- were withdrawn but never deposited prior to expiration. +CREATE TABLE IF NOT EXISTS auditor_historic_denomination_revenue + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,denom_pub_hash BYTEA PRIMARY KEY CHECK (LENGTH(denom_pub_hash)=64) + ,revenue_timestamp INT8 NOT NULL + ,revenue_balance_val INT8 NOT NULL + ,revenue_balance_frac INT4 NOT NULL + ,loss_balance_val INT8 NOT NULL + ,loss_balance_frac INT4 NOT NULL + ); +-- Table with historic profits from reserves; we eventually +-- GC auditor_historic_reserve_revenue, and then store the totals +-- in here (by time intervals). +CREATE TABLE IF NOT EXISTS auditor_historic_reserve_summary + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,start_date INT8 NOT NULL + ,end_date INT8 NOT NULL + ,reserve_profits_val INT8 NOT NULL + ,reserve_profits_frac INT4 NOT NULL + ); +CREATE INDEX IF NOT EXISTS auditor_historic_reserve_summary_by_master_pub_start_date + ON auditor_historic_reserve_summary + (master_pub + ,start_date); +-- Table with deposit confirmation sent to us by merchants; +-- we must check that the exchange reported these properly. +CREATE TABLE IF NOT EXISTS deposit_confirmations + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,serial_id BIGSERIAL UNIQUE + ,h_contract_terms BYTEA CHECK (LENGTH(h_contract_terms)=64) + ,h_wire BYTEA CHECK (LENGTH(h_wire)=64) + ,timestamp INT8 NOT NULL + ,refund_deadline INT8 NOT NULL + ,amount_without_fee_val INT8 NOT NULL + ,amount_without_fee_frac INT4 NOT NULL + ,coin_pub BYTEA CHECK (LENGTH(coin_pub)=32) + ,merchant_pub BYTEA CHECK (LENGTH(merchant_pub)=32) + ,exchange_sig BYTEA CHECK (LENGTH(exchange_sig)=64) + ,exchange_pub BYTEA CHECK (LENGTH(exchange_pub)=32) + ,master_sig BYTEA CHECK (LENGTH(master_sig)=64) + ,PRIMARY KEY (h_contract_terms,h_wire,coin_pub,merchant_pub,exchange_sig,exchange_pub,master_sig) + ); +-- Table with the sum of the ledger, auditor_historic_revenue and +-- the auditor_reserve_balance. This is the +-- final amount that the exchange should have in its bank account +-- right now. +CREATE TABLE IF NOT EXISTS auditor_predicted_result + (master_pub BYTEA CONSTRAINT master_pub_ref REFERENCES auditor_exchanges(master_pub) ON DELETE CASCADE + ,balance_val INT8 NOT NULL + ,balance_frac INT4 NOT NULL + ); + +-- Finally, commit everything +COMMIT; diff --git a/src/auditordb/plugin_auditordb_postgres.c b/src/auditordb/plugin_auditordb_postgres.c index 08d24fd7f..ca07d406b 100644 --- a/src/auditordb/plugin_auditordb_postgres.c +++ b/src/auditordb/plugin_auditordb_postgres.c @@ -80,15 +80,9 @@ struct PostgresClosure pthread_key_t db_conn_threadlocal; /** - * Directory with SQL statements to run to create tables. + * Our configuration. */ - char *sql_dir; - - /** - * Database connection string, as read from - * the configuration. - */ - char *connection_cfg_str; + const struct GNUNET_CONFIGURATION_Handle *cfg; /** * Which currency should we assume all amounts to be in? @@ -114,16 +108,12 @@ postgres_drop_tables (void *cls, { struct PostgresClosure *pc = cls; struct GNUNET_PQ_Context *conn; - char *exec_dir; - - GNUNET_asprintf (&exec_dir, - (drop_exchangelist) ? "%sdrop" : "%srestart", - pc->sql_dir); - conn = GNUNET_PQ_connect (pc->connection_cfg_str, - exec_dir, - NULL, - NULL); - GNUNET_free (exec_dir); + + conn = GNUNET_PQ_connect_with_cfg (pc->cfg, + "auditordb-postgres", + (drop_exchangelist) ? "drop" : "restart", + NULL, + NULL); if (NULL == conn) return GNUNET_SYSERR; GNUNET_PQ_disconnect (conn); @@ -143,10 +133,11 @@ postgres_create_tables (void *cls) struct PostgresClosure *pc = cls; struct GNUNET_PQ_Context *conn; - conn = GNUNET_PQ_connect (pc->connection_cfg_str, - pc->sql_dir, - NULL, - NULL); + conn = GNUNET_PQ_connect_with_cfg (pc->cfg, + "auditordb-postgres", + "auditor-", + NULL, + NULL); if (NULL == conn) return GNUNET_SYSERR; GNUNET_PQ_disconnect (conn); @@ -734,10 +725,11 @@ postgres_get_session (void *cls) GNUNET_PQ_reconnect_if_down (session->conn); return session; } - db_conn = GNUNET_PQ_connect (pc->connection_cfg_str, - NULL, - NULL, - ps); + db_conn = GNUNET_PQ_connect_with_cfg (pc->cfg, + "auditordb-postgres", + NULL, + NULL, + ps); if (NULL == db_conn) return NULL; session = GNUNET_new (struct TALER_AUDITORDB_Session); @@ -897,10 +889,11 @@ postgres_gc (void *cls) }; now = GNUNET_TIME_absolute_get (); - conn = GNUNET_PQ_connect (pc->connection_cfg_str, - NULL, - NULL, - ps); + conn = GNUNET_PQ_connect_with_cfg (pc->cfg, + "auditordb-postgres", + NULL, + NULL, + ps); if (NULL == conn) return GNUNET_SYSERR; GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -3247,50 +3240,16 @@ libtaler_plugin_auditordb_postgres_init (void *cls) const struct GNUNET_CONFIGURATION_Handle *cfg = cls; struct PostgresClosure *pg; struct TALER_AUDITORDB_Plugin *plugin; - const char *ec; pg = GNUNET_new (struct PostgresClosure); - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (cfg, - "auditordb-postgres", - "SQL_DIR", - &pg->sql_dir)) - { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "auditordb-postgres", - "SQL_DIR"); - GNUNET_free (pg); - return NULL; - } + pg->cfg = cfg; if (0 != pthread_key_create (&pg->db_conn_threadlocal, &db_conn_destroy)) { TALER_LOG_ERROR ("Cannnot create pthread key.\n"); - GNUNET_free (pg->sql_dir); GNUNET_free (pg); return NULL; } - ec = getenv ("TALER_AUDITORDB_POSTGRES_CONFIG"); - if (NULL != ec) - { - pg->connection_cfg_str = GNUNET_strdup (ec); - } - else - { - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, - "auditordb-postgres", - "CONFIG", - &pg->connection_cfg_str)) - { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "auditordb-postgres", - "CONFIG"); - GNUNET_free (pg->sql_dir); - GNUNET_free (pg); - return NULL; - } - } if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "taler", @@ -3300,8 +3259,6 @@ libtaler_plugin_auditordb_postgres_init (void *cls) GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "taler", "CURRENCY"); - GNUNET_free (pg->connection_cfg_str); - GNUNET_free (pg->sql_dir); GNUNET_free (pg); return NULL; } @@ -3407,8 +3364,6 @@ libtaler_plugin_auditordb_postgres_done (void *cls) struct TALER_AUDITORDB_Plugin *plugin = cls; struct PostgresClosure *pg = plugin->cls; - GNUNET_free (pg->connection_cfg_str); - GNUNET_free (pg->sql_dir); GNUNET_free (pg->currency); GNUNET_free (pg); GNUNET_free (plugin); diff --git a/src/exchangedb/0000.sql b/src/exchangedb/0000.sql deleted file mode 100644 index 1483e2015..000000000 --- a/src/exchangedb/0000.sql +++ /dev/null @@ -1,293 +0,0 @@ --- LICENSE AND COPYRIGHT --- --- Copyright (C) 2010 Hubert depesz Lubaczewski --- --- This program is distributed under the (Revised) BSD License: --- L --- --- Redistribution and use in source and binary forms, with or without --- modification, are permitted provided that the following conditions --- are met: --- --- * Redistributions of source code must retain the above copyright --- notice, this list of conditions and the following disclaimer. --- --- * Redistributions in binary form must reproduce the above copyright --- notice, this list of conditions and the following disclaimer in the --- documentation and/or other materials provided with the distribution. --- --- * Neither the name of Hubert depesz Lubaczewski's Organization --- nor the names of its contributors may be used to endorse or --- promote products derived from this software without specific --- prior written permission. --- --- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" --- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE --- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE --- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE --- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL --- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR --- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER --- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, --- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --- --- Code origin: https://gitlab.com/depesz/Versioning/blob/master/install.versioning.sql --- --- --- # NAME --- --- **Versioning** - simplistic take on tracking and applying changes to databases. --- --- # DESCRIPTION --- --- This project strives to provide simple way to manage changes to --- database. --- --- Instead of making changes on development server, then finding --- differences between production and development, deciding which ones --- should be installed on production, and finding a way to install them - --- you start with writing diffs themselves! --- --- # INSTALLATION --- --- To install versioning simply run install.versioning.sql in your database --- (all of them: production, stage, test, devel, ...). --- --- # USAGE --- --- In your files with patches to database, put whole logic in single --- transaction, and use \_v.\* functions - usually \_v.register_patch() at --- least to make sure everything is OK. --- --- For example. Let's assume you have patch files: --- --- ## 0001.sql: --- --- ``` --- create table users (id serial primary key, username text); --- ``` --- --- ## 0002.sql: --- --- ``` --- insert into users (username) values ('depesz'); --- ``` --- To change it to use versioning you would change the files, to this --- state: --- --- 0000.sql: --- --- ``` --- BEGIN; --- select _v.register_patch('000-base', NULL, NULL); --- create table users (id serial primary key, username text); --- COMMIT; --- ``` --- --- ## 0002.sql: --- --- ``` --- BEGIN; --- select _v.register_patch('001-users', ARRAY['000-base'], NULL); --- insert into users (username) values ('depesz'); --- COMMIT; --- ``` --- --- This will make sure that patch 001-users can only be applied after --- 000-base. --- --- # AVAILABLE FUNCTIONS --- --- ## \_v.register_patch( TEXT ) --- --- Registers named patch, or dies if it is already registered. --- --- Returns integer which is id of patch in \_v.patches table - only if it --- succeeded. --- --- ## \_v.register_patch( TEXT, TEXT[] ) --- --- Same as \_v.register_patch( TEXT ), but checks is all given patches (given as --- array in second argument) are already registered. --- --- ## \_v.register_patch( TEXT, TEXT[], TEXT[] ) --- --- Same as \_v.register_patch( TEXT, TEXT[] ), but also checks if there are no conflicts with preexisting patches. --- --- Third argument is array of names of patches that conflict with current one. So --- if any of them is installed - register_patch will error out. --- --- ## \_v.unregister_patch( TEXT ) --- --- Removes information about given patch from the versioning data. --- --- It doesn't remove objects that were created by this patch - just removes --- metainformation. --- --- ## \_v.assert_user_is_superuser() --- --- Make sure that current patch is being loaded by superuser. --- --- If it's not - it will raise exception, and break transaction. --- --- ## \_v.assert_user_is_not_superuser() --- --- Make sure that current patch is not being loaded by superuser. --- --- If it is - it will raise exception, and break transaction. --- --- ## \_v.assert_user_is_one_of(TEXT, TEXT, ... ) --- --- Make sure that current patch is being loaded by one of listed users. --- --- If ```current_user``` is not listed as one of arguments - function will raise --- exception and break the transaction. - -BEGIN; - --- This file adds versioning support to database it will be loaded to. --- It requires that PL/pgSQL is already loaded - will raise exception otherwise. --- All versioning "stuff" (tables, functions) is in "_v" schema. - --- All functions are defined as 'RETURNS SETOF INT4' to be able to make them to RETURN literaly nothing (0 rows). --- >> RETURNS VOID<< IS similar, but it still outputs "empty line" in psql when calling. -CREATE SCHEMA IF NOT EXISTS _v; -COMMENT ON SCHEMA _v IS 'Schema for versioning data and functionality.'; - -CREATE TABLE IF NOT EXISTS _v.patches ( - patch_name TEXT PRIMARY KEY, - applied_tsz TIMESTAMPTZ NOT NULL DEFAULT now(), - applied_by TEXT NOT NULL, - requires TEXT[], - conflicts TEXT[] -); -COMMENT ON TABLE _v.patches IS 'Contains information about what patches are currently applied on database.'; -COMMENT ON COLUMN _v.patches.patch_name IS 'Name of patch, has to be unique for every patch.'; -COMMENT ON COLUMN _v.patches.applied_tsz IS 'When the patch was applied.'; -COMMENT ON COLUMN _v.patches.applied_by IS 'Who applied this patch (PostgreSQL username)'; -COMMENT ON COLUMN _v.patches.requires IS 'List of patches that are required for given patch.'; -COMMENT ON COLUMN _v.patches.conflicts IS 'List of patches that conflict with given patch.'; - -CREATE OR REPLACE FUNCTION _v.register_patch( IN in_patch_name TEXT, IN in_requirements TEXT[], in_conflicts TEXT[], OUT versioning INT4 ) RETURNS setof INT4 AS $$ -DECLARE - t_text TEXT; - t_text_a TEXT[]; - i INT4; -BEGIN - -- Thanks to this we know only one patch will be applied at a time - LOCK TABLE _v.patches IN EXCLUSIVE MODE; - - SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_patch_name; - IF FOUND THEN - RAISE EXCEPTION 'Patch % is already applied!', in_patch_name; - END IF; - - t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE patch_name = any( in_conflicts ) ); - IF array_upper( t_text_a, 1 ) IS NOT NULL THEN - RAISE EXCEPTION 'Versioning patches conflict. Conflicting patche(s) installed: %.', array_to_string( t_text_a, ', ' ); - END IF; - - IF array_upper( in_requirements, 1 ) IS NOT NULL THEN - t_text_a := '{}'; - FOR i IN array_lower( in_requirements, 1 ) .. array_upper( in_requirements, 1 ) LOOP - SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_requirements[i]; - IF NOT FOUND THEN - t_text_a := t_text_a || in_requirements[i]; - END IF; - END LOOP; - IF array_upper( t_text_a, 1 ) IS NOT NULL THEN - RAISE EXCEPTION 'Missing prerequisite(s): %.', array_to_string( t_text_a, ', ' ); - END IF; - END IF; - - INSERT INTO _v.patches (patch_name, applied_tsz, applied_by, requires, conflicts ) VALUES ( in_patch_name, now(), current_user, coalesce( in_requirements, '{}' ), coalesce( in_conflicts, '{}' ) ); - RETURN; -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[], TEXT[] ) IS 'Function to register patches in database. Raises exception if there are conflicts, prerequisites are not installed or the migration has already been installed.'; - -CREATE OR REPLACE FUNCTION _v.register_patch( TEXT, TEXT[] ) RETURNS setof INT4 AS $$ - SELECT _v.register_patch( $1, $2, NULL ); -$$ language sql; -COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[] ) IS 'Wrapper to allow registration of patches without conflicts.'; -CREATE OR REPLACE FUNCTION _v.register_patch( TEXT ) RETURNS setof INT4 AS $$ - SELECT _v.register_patch( $1, NULL, NULL ); -$$ language sql; -COMMENT ON FUNCTION _v.register_patch( TEXT ) IS 'Wrapper to allow registration of patches without requirements and conflicts.'; - -CREATE OR REPLACE FUNCTION _v.unregister_patch( IN in_patch_name TEXT, OUT versioning INT4 ) RETURNS setof INT4 AS $$ -DECLARE - i INT4; - t_text_a TEXT[]; -BEGIN - -- Thanks to this we know only one patch will be applied at a time - LOCK TABLE _v.patches IN EXCLUSIVE MODE; - - t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE in_patch_name = ANY( requires ) ); - IF array_upper( t_text_a, 1 ) IS NOT NULL THEN - RAISE EXCEPTION 'Cannot uninstall %, as it is required by: %.', in_patch_name, array_to_string( t_text_a, ', ' ); - END IF; - - DELETE FROM _v.patches WHERE patch_name = in_patch_name; - GET DIAGNOSTICS i = ROW_COUNT; - IF i < 1 THEN - RAISE EXCEPTION 'Patch % is not installed, so it can''t be uninstalled!', in_patch_name; - END IF; - - RETURN; -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.unregister_patch( TEXT ) IS 'Function to unregister patches in database. Dies if the patch is not registered, or if unregistering it would break dependencies.'; - -CREATE OR REPLACE FUNCTION _v.assert_patch_is_applied( IN in_patch_name TEXT ) RETURNS TEXT as $$ -DECLARE - t_text TEXT; -BEGIN - SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_patch_name; - IF NOT FOUND THEN - RAISE EXCEPTION 'Patch % is not applied!', in_patch_name; - END IF; - RETURN format('Patch %s is applied.', in_patch_name); -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.assert_patch_is_applied( TEXT ) IS 'Function that can be used to make sure that patch has been applied.'; - -CREATE OR REPLACE FUNCTION _v.assert_user_is_superuser() RETURNS TEXT as $$ -DECLARE - v_super bool; -BEGIN - SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user; - IF v_super THEN - RETURN 'assert_user_is_superuser: OK'; - END IF; - RAISE EXCEPTION 'Current user is not superuser - cannot continue.'; -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.assert_user_is_superuser() IS 'Function that can be used to make sure that patch is being applied using superuser account.'; - -CREATE OR REPLACE FUNCTION _v.assert_user_is_not_superuser() RETURNS TEXT as $$ -DECLARE - v_super bool; -BEGIN - SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user; - IF v_super THEN - RAISE EXCEPTION 'Current user is superuser - cannot continue.'; - END IF; - RETURN 'assert_user_is_not_superuser: OK'; -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.assert_user_is_not_superuser() IS 'Function that can be used to make sure that patch is being applied using normal (not superuser) account.'; - -CREATE OR REPLACE FUNCTION _v.assert_user_is_one_of(VARIADIC p_acceptable_users TEXT[] ) RETURNS TEXT as $$ -DECLARE -BEGIN - IF current_user = any( p_acceptable_users ) THEN - RETURN 'assert_user_is_one_of: OK'; - END IF; - RAISE EXCEPTION 'User is not one of: % - cannot continue.', p_acceptable_users; -END; -$$ language plpgsql; -COMMENT ON FUNCTION _v.assert_user_is_one_of(TEXT[]) IS 'Function that can be used to make sure that patch is being applied by one of defined users.'; - -COMMIT; diff --git a/src/exchangedb/0001.sql b/src/exchangedb/0001.sql deleted file mode 100644 index 02dc68cf4..000000000 --- a/src/exchangedb/0001.sql +++ /dev/null @@ -1,368 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2014--2020 Taler Systems SA --- --- TALER is free software; you can redistribute it and/or modify it under the --- terms of the GNU General Public License as published by the Free Software --- Foundation; either version 3, or (at your option) any later version. --- --- TALER is distributed in the hope that it will be useful, but WITHOUT ANY --- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR --- A PARTICULAR PURPOSE. See the GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License along with --- TALER; see the file COPYING. If not, see --- - --- Everything in one big transaction -BEGIN; - --- Check patch versioning is in place. -SELECT _v.register_patch('exchange-0001', NULL, NULL); - - --- Main denominations table. All the coins the exchange knows about. -CREATE TABLE IF NOT EXISTS denominations - (denom_pub_hash BYTEA PRIMARY KEY CHECK (LENGTH(denom_pub_hash)=64) - ,denom_pub BYTEA NOT NULL - ,master_pub BYTEA NOT NULL CHECK (LENGTH(master_pub)=32) - ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64) - ,valid_from INT8 NOT NULL - ,expire_withdraw INT8 NOT NULL - ,expire_deposit INT8 NOT NULL - ,expire_legal INT8 NOT NULL - ,coin_val INT8 NOT NULL - ,coin_frac INT4 NOT NULL - ,fee_withdraw_val INT8 NOT NULL - ,fee_withdraw_frac INT4 NOT NULL - ,fee_deposit_val INT8 NOT NULL - ,fee_deposit_frac INT4 NOT NULL - ,fee_refresh_val INT8 NOT NULL - ,fee_refresh_frac INT4 NOT NULL - ,fee_refund_val INT8 NOT NULL - ,fee_refund_frac INT4 NOT NULL - ); -CREATE INDEX IF NOT EXISTS denominations_expire_legal_index - ON denominations - (expire_legal); - --- denomination_revocations table is for remembering which denomination keys have been revoked -CREATE TABLE IF NOT EXISTS denomination_revocations - (denom_revocations_serial_id BIGSERIAL UNIQUE - ,denom_pub_hash BYTEA PRIMARY KEY REFERENCES denominations (denom_pub_hash) ON DELETE CASCADE - ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64) - ); --- reserves table is for summarization of a reserve. It is updated when new --- funds are added and existing funds are withdrawn. The 'expiration_date' --- can be used to eventually get rid of reserves that have not been used --- for a very long time (usually by refunding the owner) -CREATE TABLE IF NOT EXISTS reserves - (reserve_pub BYTEA PRIMARY KEY CHECK(LENGTH(reserve_pub)=32) - ,account_details TEXT NOT NULL - ,current_balance_val INT8 NOT NULL - ,current_balance_frac INT4 NOT NULL - ,expiration_date INT8 NOT NULL - ,gc_date INT8 NOT NULL - ); --- index on reserves table (TODO: useless due to primary key!?) -CREATE INDEX IF NOT EXISTS reserves_reserve_pub_index - ON reserves - (reserve_pub); --- index for get_expired_reserves -CREATE INDEX IF NOT EXISTS reserves_expiration_index - ON reserves - (expiration_date - ,current_balance_val - ,current_balance_frac - ); --- index for reserve GC operations -CREATE INDEX IF NOT EXISTS reserves_gc_index - ON reserves - (gc_date); --- reserves_in table collects the transactions which transfer funds --- into the reserve. The rows of this table correspond to each --- incoming transaction. -CREATE TABLE IF NOT EXISTS reserves_in - (reserve_in_serial_id BIGSERIAL UNIQUE - ,reserve_pub BYTEA NOT NULL REFERENCES reserves (reserve_pub) ON DELETE CASCADE - ,wire_reference INT8 NOT NULL - ,credit_val INT8 NOT NULL - ,credit_frac INT4 NOT NULL - ,sender_account_details TEXT NOT NULL - ,exchange_account_section TEXT NOT NULL - ,execution_date INT8 NOT NULL - ,PRIMARY KEY (reserve_pub, wire_reference) - ); --- Create indices on reserves_in -CREATE INDEX IF NOT EXISTS reserves_in_execution_index - ON reserves_in - (exchange_account_section - ,execution_date - ); -CREATE INDEX IF NOT EXISTS reserves_in_exchange_account_serial - ON reserves_in - (exchange_account_section, - reserve_in_serial_id DESC - ); --- This table contains the data for wire transfers the exchange has --- executed to close a reserve. -CREATE TABLE IF NOT EXISTS reserves_close - (close_uuid BIGSERIAL PRIMARY KEY - ,reserve_pub BYTEA NOT NULL REFERENCES reserves (reserve_pub) ON DELETE CASCADE - ,execution_date INT8 NOT NULL - ,wtid BYTEA NOT NULL CHECK (LENGTH(wtid)=32) - ,receiver_account TEXT NOT NULL - ,amount_val INT8 NOT NULL - ,amount_frac INT4 NOT NULL - ,closing_fee_val INT8 NOT NULL - ,closing_fee_frac INT4 NOT NULL); -CREATE INDEX IF NOT EXISTS reserves_close_by_reserve - ON reserves_close - (reserve_pub); --- Table with the withdraw operations that have been performed on a reserve. --- The 'h_blind_ev' is the hash of the blinded coin. It serves as a primary --- key, as (broken) clients that use a non-random coin and blinding factor --- should fail to even withdraw, as otherwise the coins will fail to deposit --- (as they really must be unique). --- For the denom_pub, we do NOT CASCADE on DELETE, we may keep the denomination key alive! -CREATE TABLE IF NOT EXISTS reserves_out - (reserve_out_serial_id BIGSERIAL UNIQUE - ,h_blind_ev BYTEA PRIMARY KEY CHECK (LENGTH(h_blind_ev)=64) - ,denom_pub_hash BYTEA NOT NULL REFERENCES denominations (denom_pub_hash) - ,denom_sig BYTEA NOT NULL - ,reserve_pub BYTEA NOT NULL REFERENCES reserves (reserve_pub) ON DELETE CASCADE - ,reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64) - ,execution_date INT8 NOT NULL - ,amount_with_fee_val INT8 NOT NULL - ,amount_with_fee_frac INT4 NOT NULL - ); --- Index blindcoins(reserve_pub) for get_reserves_out statement -CREATE INDEX IF NOT EXISTS reserves_out_reserve_pub_index - ON reserves_out - (reserve_pub); -CREATE INDEX IF NOT EXISTS reserves_out_execution_date - ON reserves_out - (execution_date); -CREATE INDEX IF NOT EXISTS reserves_out_for_get_withdraw_info - ON reserves_out - (denom_pub_hash - ,h_blind_ev - ); --- Table with coins that have been (partially) spent, used to track --- coin information only once. -CREATE TABLE IF NOT EXISTS known_coins - (coin_pub BYTEA NOT NULL PRIMARY KEY CHECK (LENGTH(coin_pub)=32) - ,denom_pub_hash BYTEA NOT NULL REFERENCES denominations (denom_pub_hash) ON DELETE CASCADE - ,denom_sig BYTEA NOT NULL - ); -CREATE INDEX IF NOT EXISTS known_coins_by_denomination - ON known_coins - (denom_pub_hash); --- Table with the commitments made when melting a coin. */ -CREATE TABLE IF NOT EXISTS refresh_commitments - (melt_serial_id BIGSERIAL UNIQUE - ,rc BYTEA PRIMARY KEY CHECK (LENGTH(rc)=64) - ,old_coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) ON DELETE CASCADE - ,old_coin_sig BYTEA NOT NULL CHECK(LENGTH(old_coin_sig)=64) - ,amount_with_fee_val INT8 NOT NULL - ,amount_with_fee_frac INT4 NOT NULL - ,noreveal_index INT4 NOT NULL - ); -CREATE INDEX IF NOT EXISTS refresh_commitments_old_coin_pub_index - ON refresh_commitments - (old_coin_pub); --- Table with the revelations about the new coins that are to be created --- during a melting session. Includes the session, the cut-and-choose --- index and the index of the new coin, and the envelope of the new --- coin to be signed, as well as the encrypted information about the --- private key and the blinding factor for the coin (for verification --- in case this newcoin_index is chosen to be revealed) -CREATE TABLE IF NOT EXISTS refresh_revealed_coins - (rc BYTEA NOT NULL REFERENCES refresh_commitments (rc) ON DELETE CASCADE - ,newcoin_index INT4 NOT NULL - ,link_sig BYTEA NOT NULL CHECK(LENGTH(link_sig)=64) - ,denom_pub_hash BYTEA NOT NULL REFERENCES denominations (denom_pub_hash) ON DELETE CASCADE - ,coin_ev BYTEA UNIQUE NOT NULL - ,h_coin_ev BYTEA NOT NULL CHECK(LENGTH(h_coin_ev)=64) - ,ev_sig BYTEA NOT NULL - ,PRIMARY KEY (rc, newcoin_index) - ,UNIQUE (h_coin_ev) - ); -CREATE INDEX IF NOT EXISTS refresh_revealed_coins_coin_pub_index - ON refresh_revealed_coins - (denom_pub_hash); --- Table with the transfer keys of a refresh operation; includes --- the rc for which this is the link information, the --- transfer public key (for gamma) and the revealed transfer private --- keys (array of TALER_CNC_KAPPA - 1 entries, with gamma being skipped) */ -CREATE TABLE IF NOT EXISTS refresh_transfer_keys - (rc BYTEA NOT NULL PRIMARY KEY REFERENCES refresh_commitments (rc) ON DELETE CASCADE - ,transfer_pub BYTEA NOT NULL CHECK(LENGTH(transfer_pub)=32) - ,transfer_privs BYTEA NOT NULL - ); --- for get_link (not sure if this helps, as there should be very few --- transfer_pubs per rc, but at least in theory this helps the ORDER BY --- clause. -CREATE INDEX IF NOT EXISTS refresh_transfer_keys_coin_tpub - ON refresh_transfer_keys - (rc - ,transfer_pub - ); --- This table contains the wire transfers the exchange is supposed to --- execute to transmit funds to the merchants (and manage refunds). -CREATE TABLE IF NOT EXISTS deposits - (deposit_serial_id BIGSERIAL PRIMARY KEY - ,coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) ON DELETE CASCADE - ,amount_with_fee_val INT8 NOT NULL - ,amount_with_fee_frac INT4 NOT NULL - ,timestamp INT8 NOT NULL - ,refund_deadline INT8 NOT NULL - ,wire_deadline INT8 NOT NULL - ,merchant_pub BYTEA NOT NULL CHECK (LENGTH(merchant_pub)=32) - ,h_contract_terms BYTEA NOT NULL CHECK (LENGTH(h_contract_terms)=64) - ,h_wire BYTEA NOT NULL CHECK (LENGTH(h_wire)=64) - ,coin_sig BYTEA NOT NULL CHECK (LENGTH(coin_sig)=64) - ,wire TEXT NOT NULL - ,tiny BOOLEAN NOT NULL DEFAULT FALSE - ,done BOOLEAN NOT NULL DEFAULT FALSE - ,UNIQUE (coin_pub, merchant_pub, h_contract_terms) - ); --- Index for get_deposit_for_wtid and get_deposit_statement */ -CREATE INDEX IF NOT EXISTS deposits_coin_pub_merchant_contract_index - ON deposits - (coin_pub - ,merchant_pub - ,h_contract_terms - ); --- Index for deposits_get_ready -CREATE INDEX IF NOT EXISTS deposits_get_ready_index - ON deposits - (tiny - ,done - ,wire_deadline - ,refund_deadline - ); --- Index for deposits_iterate_matching -CREATE INDEX IF NOT EXISTS deposits_iterate_matching - ON deposits - (merchant_pub - ,h_wire - ,done - ,wire_deadline - ); --- Table with information about coins that have been refunded. (Technically --- one of the deposit operations that a coin was involved with is refunded.) --- The combo of coin_pub, merchant_pub, h_contract_terms and rtransaction_id --- MUST be unique, and we usually select by coin_pub so that one goes first. */ -CREATE TABLE IF NOT EXISTS refunds - (refund_serial_id BIGSERIAL UNIQUE - ,coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) ON DELETE CASCADE - ,merchant_pub BYTEA NOT NULL CHECK(LENGTH(merchant_pub)=32) - ,merchant_sig BYTEA NOT NULL CHECK(LENGTH(merchant_sig)=64) - ,h_contract_terms BYTEA NOT NULL CHECK(LENGTH(h_contract_terms)=64) - ,rtransaction_id INT8 NOT NULL - ,amount_with_fee_val INT8 NOT NULL - ,amount_with_fee_frac INT4 NOT NULL - ,PRIMARY KEY (coin_pub, merchant_pub, h_contract_terms, rtransaction_id) - ); -CREATE INDEX IF NOT EXISTS refunds_coin_pub_index - ON refunds - (coin_pub); --- This table contains the data for --- wire transfers the exchange has executed. -CREATE TABLE IF NOT EXISTS wire_out - (wireout_uuid BIGSERIAL PRIMARY KEY - ,execution_date INT8 NOT NULL - ,wtid_raw BYTEA UNIQUE NOT NULL CHECK (LENGTH(wtid_raw)=32) - ,wire_target TEXT NOT NULL - ,exchange_account_section TEXT NOT NULL - ,amount_val INT8 NOT NULL - ,amount_frac INT4 NOT NULL - ); --- Table for the tracking API, mapping from wire transfer identifier --- to transactions and back -CREATE TABLE IF NOT EXISTS aggregation_tracking - (aggregation_serial_id BIGSERIAL UNIQUE - ,deposit_serial_id INT8 PRIMARY KEY REFERENCES deposits (deposit_serial_id) ON DELETE CASCADE - ,wtid_raw BYTEA CONSTRAINT wire_out_ref REFERENCES wire_out(wtid_raw) ON DELETE CASCADE DEFERRABLE - ); --- Index for lookup_transactions statement on wtid -CREATE INDEX IF NOT EXISTS aggregation_tracking_wtid_index - ON aggregation_tracking - (wtid_raw); --- Table for the wire fees. -CREATE TABLE IF NOT EXISTS wire_fee - (wire_method VARCHAR NOT NULL - ,start_date INT8 NOT NULL - ,end_date INT8 NOT NULL - ,wire_fee_val INT8 NOT NULL - ,wire_fee_frac INT4 NOT NULL - ,closing_fee_val INT8 NOT NULL - ,closing_fee_frac INT4 NOT NULL - ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64) - ,PRIMARY KEY (wire_method, start_date) - ); -CREATE INDEX IF NOT EXISTS wire_fee_gc_index - ON wire_fee - (end_date); --- Table for /recoup information --- Do not cascade on the coin_pub, as we may keep the coin alive! */ -CREATE TABLE IF NOT EXISTS recoup - (recoup_uuid BIGSERIAL UNIQUE - ,coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) - ,coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64) - ,coin_blind BYTEA NOT NULL CHECK(LENGTH(coin_blind)=32) - ,amount_val INT8 NOT NULL - ,amount_frac INT4 NOT NULL - ,timestamp INT8 NOT NULL - ,h_blind_ev BYTEA NOT NULL REFERENCES reserves_out (h_blind_ev) ON DELETE CASCADE - ); -CREATE INDEX IF NOT EXISTS recoup_by_coin_index - ON recoup - (coin_pub); -CREATE INDEX IF NOT EXISTS recoup_by_h_blind_ev - ON recoup - (h_blind_ev); -CREATE INDEX IF NOT EXISTS recoup_for_by_reserve - ON recoup - (coin_pub - ,h_blind_ev - ); --- Table for /recoup-refresh information --- Do not cascade on the coin_pub, as we may keep the coin alive! */ -CREATE TABLE IF NOT EXISTS recoup_refresh - (recoup_refresh_uuid BIGSERIAL UNIQUE - ,coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) - ,coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64) - ,coin_blind BYTEA NOT NULL CHECK(LENGTH(coin_blind)=32) - ,amount_val INT8 NOT NULL - ,amount_frac INT4 NOT NULL - ,timestamp INT8 NOT NULL - ,h_blind_ev BYTEA NOT NULL REFERENCES refresh_revealed_coins (h_coin_ev) ON DELETE CASCADE - ); -CREATE INDEX IF NOT EXISTS recoup_refresh_by_coin_index - ON recoup_refresh - (coin_pub); -CREATE INDEX IF NOT EXISTS recoup_refresh_by_h_blind_ev - ON recoup_refresh - (h_blind_ev); -CREATE INDEX IF NOT EXISTS recoup_refresh_for_by_reserve - ON recoup_refresh - (coin_pub - ,h_blind_ev - ); --- This table contains the pre-commit data for --- wire transfers the exchange is about to execute. -CREATE TABLE IF NOT EXISTS prewire - (prewire_uuid BIGSERIAL PRIMARY KEY - ,type TEXT NOT NULL - ,finished BOOLEAN NOT NULL DEFAULT false - ,buf BYTEA NOT NULL - ); --- Index for wire_prepare_data_get and gc_prewire statement -CREATE INDEX IF NOT EXISTS prepare_iteration_index - ON prewire - (finished); - --- Complete transaction -COMMIT; diff --git a/src/exchangedb/Makefile.am b/src/exchangedb/Makefile.am index e7ac4d719..37809f6b4 100644 --- a/src/exchangedb/Makefile.am +++ b/src/exchangedb/Makefile.am @@ -15,8 +15,8 @@ pkgcfg_DATA = \ sqldir = $(prefix)/share/taler/sql/exchange/ sql_DATA = \ - 0000.sql \ - 0001.sql \ + exchange-0000.sql \ + exchange-0001.sql \ drop0000.sql EXTRA_DIST = \ diff --git a/src/exchangedb/exchange-0000.sql b/src/exchangedb/exchange-0000.sql new file mode 100644 index 000000000..1483e2015 --- /dev/null +++ b/src/exchangedb/exchange-0000.sql @@ -0,0 +1,293 @@ +-- LICENSE AND COPYRIGHT +-- +-- Copyright (C) 2010 Hubert depesz Lubaczewski +-- +-- This program is distributed under the (Revised) BSD License: +-- L +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions +-- are met: +-- +-- * Redistributions of source code must retain the above copyright +-- notice, this list of conditions and the following disclaimer. +-- +-- * Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- +-- * Neither the name of Hubert depesz Lubaczewski's Organization +-- nor the names of its contributors may be used to endorse or +-- promote products derived from this software without specific +-- prior written permission. +-- +-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE +-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +-- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +-- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +-- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +-- +-- Code origin: https://gitlab.com/depesz/Versioning/blob/master/install.versioning.sql +-- +-- +-- # NAME +-- +-- **Versioning** - simplistic take on tracking and applying changes to databases. +-- +-- # DESCRIPTION +-- +-- This project strives to provide simple way to manage changes to +-- database. +-- +-- Instead of making changes on development server, then finding +-- differences between production and development, deciding which ones +-- should be installed on production, and finding a way to install them - +-- you start with writing diffs themselves! +-- +-- # INSTALLATION +-- +-- To install versioning simply run install.versioning.sql in your database +-- (all of them: production, stage, test, devel, ...). +-- +-- # USAGE +-- +-- In your files with patches to database, put whole logic in single +-- transaction, and use \_v.\* functions - usually \_v.register_patch() at +-- least to make sure everything is OK. +-- +-- For example. Let's assume you have patch files: +-- +-- ## 0001.sql: +-- +-- ``` +-- create table users (id serial primary key, username text); +-- ``` +-- +-- ## 0002.sql: +-- +-- ``` +-- insert into users (username) values ('depesz'); +-- ``` +-- To change it to use versioning you would change the files, to this +-- state: +-- +-- 0000.sql: +-- +-- ``` +-- BEGIN; +-- select _v.register_patch('000-base', NULL, NULL); +-- create table users (id serial primary key, username text); +-- COMMIT; +-- ``` +-- +-- ## 0002.sql: +-- +-- ``` +-- BEGIN; +-- select _v.register_patch('001-users', ARRAY['000-base'], NULL); +-- insert into users (username) values ('depesz'); +-- COMMIT; +-- ``` +-- +-- This will make sure that patch 001-users can only be applied after +-- 000-base. +-- +-- # AVAILABLE FUNCTIONS +-- +-- ## \_v.register_patch( TEXT ) +-- +-- Registers named patch, or dies if it is already registered. +-- +-- Returns integer which is id of patch in \_v.patches table - only if it +-- succeeded. +-- +-- ## \_v.register_patch( TEXT, TEXT[] ) +-- +-- Same as \_v.register_patch( TEXT ), but checks is all given patches (given as +-- array in second argument) are already registered. +-- +-- ## \_v.register_patch( TEXT, TEXT[], TEXT[] ) +-- +-- Same as \_v.register_patch( TEXT, TEXT[] ), but also checks if there are no conflicts with preexisting patches. +-- +-- Third argument is array of names of patches that conflict with current one. So +-- if any of them is installed - register_patch will error out. +-- +-- ## \_v.unregister_patch( TEXT ) +-- +-- Removes information about given patch from the versioning data. +-- +-- It doesn't remove objects that were created by this patch - just removes +-- metainformation. +-- +-- ## \_v.assert_user_is_superuser() +-- +-- Make sure that current patch is being loaded by superuser. +-- +-- If it's not - it will raise exception, and break transaction. +-- +-- ## \_v.assert_user_is_not_superuser() +-- +-- Make sure that current patch is not being loaded by superuser. +-- +-- If it is - it will raise exception, and break transaction. +-- +-- ## \_v.assert_user_is_one_of(TEXT, TEXT, ... ) +-- +-- Make sure that current patch is being loaded by one of listed users. +-- +-- If ```current_user``` is not listed as one of arguments - function will raise +-- exception and break the transaction. + +BEGIN; + +-- This file adds versioning support to database it will be loaded to. +-- It requires that PL/pgSQL is already loaded - will raise exception otherwise. +-- All versioning "stuff" (tables, functions) is in "_v" schema. + +-- All functions are defined as 'RETURNS SETOF INT4' to be able to make them to RETURN literaly nothing (0 rows). +-- >> RETURNS VOID<< IS similar, but it still outputs "empty line" in psql when calling. +CREATE SCHEMA IF NOT EXISTS _v; +COMMENT ON SCHEMA _v IS 'Schema for versioning data and functionality.'; + +CREATE TABLE IF NOT EXISTS _v.patches ( + patch_name TEXT PRIMARY KEY, + applied_tsz TIMESTAMPTZ NOT NULL DEFAULT now(), + applied_by TEXT NOT NULL, + requires TEXT[], + conflicts TEXT[] +); +COMMENT ON TABLE _v.patches IS 'Contains information about what patches are currently applied on database.'; +COMMENT ON COLUMN _v.patches.patch_name IS 'Name of patch, has to be unique for every patch.'; +COMMENT ON COLUMN _v.patches.applied_tsz IS 'When the patch was applied.'; +COMMENT ON COLUMN _v.patches.applied_by IS 'Who applied this patch (PostgreSQL username)'; +COMMENT ON COLUMN _v.patches.requires IS 'List of patches that are required for given patch.'; +COMMENT ON COLUMN _v.patches.conflicts IS 'List of patches that conflict with given patch.'; + +CREATE OR REPLACE FUNCTION _v.register_patch( IN in_patch_name TEXT, IN in_requirements TEXT[], in_conflicts TEXT[], OUT versioning INT4 ) RETURNS setof INT4 AS $$ +DECLARE + t_text TEXT; + t_text_a TEXT[]; + i INT4; +BEGIN + -- Thanks to this we know only one patch will be applied at a time + LOCK TABLE _v.patches IN EXCLUSIVE MODE; + + SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_patch_name; + IF FOUND THEN + RAISE EXCEPTION 'Patch % is already applied!', in_patch_name; + END IF; + + t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE patch_name = any( in_conflicts ) ); + IF array_upper( t_text_a, 1 ) IS NOT NULL THEN + RAISE EXCEPTION 'Versioning patches conflict. Conflicting patche(s) installed: %.', array_to_string( t_text_a, ', ' ); + END IF; + + IF array_upper( in_requirements, 1 ) IS NOT NULL THEN + t_text_a := '{}'; + FOR i IN array_lower( in_requirements, 1 ) .. array_upper( in_requirements, 1 ) LOOP + SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_requirements[i]; + IF NOT FOUND THEN + t_text_a := t_text_a || in_requirements[i]; + END IF; + END LOOP; + IF array_upper( t_text_a, 1 ) IS NOT NULL THEN + RAISE EXCEPTION 'Missing prerequisite(s): %.', array_to_string( t_text_a, ', ' ); + END IF; + END IF; + + INSERT INTO _v.patches (patch_name, applied_tsz, applied_by, requires, conflicts ) VALUES ( in_patch_name, now(), current_user, coalesce( in_requirements, '{}' ), coalesce( in_conflicts, '{}' ) ); + RETURN; +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[], TEXT[] ) IS 'Function to register patches in database. Raises exception if there are conflicts, prerequisites are not installed or the migration has already been installed.'; + +CREATE OR REPLACE FUNCTION _v.register_patch( TEXT, TEXT[] ) RETURNS setof INT4 AS $$ + SELECT _v.register_patch( $1, $2, NULL ); +$$ language sql; +COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[] ) IS 'Wrapper to allow registration of patches without conflicts.'; +CREATE OR REPLACE FUNCTION _v.register_patch( TEXT ) RETURNS setof INT4 AS $$ + SELECT _v.register_patch( $1, NULL, NULL ); +$$ language sql; +COMMENT ON FUNCTION _v.register_patch( TEXT ) IS 'Wrapper to allow registration of patches without requirements and conflicts.'; + +CREATE OR REPLACE FUNCTION _v.unregister_patch( IN in_patch_name TEXT, OUT versioning INT4 ) RETURNS setof INT4 AS $$ +DECLARE + i INT4; + t_text_a TEXT[]; +BEGIN + -- Thanks to this we know only one patch will be applied at a time + LOCK TABLE _v.patches IN EXCLUSIVE MODE; + + t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE in_patch_name = ANY( requires ) ); + IF array_upper( t_text_a, 1 ) IS NOT NULL THEN + RAISE EXCEPTION 'Cannot uninstall %, as it is required by: %.', in_patch_name, array_to_string( t_text_a, ', ' ); + END IF; + + DELETE FROM _v.patches WHERE patch_name = in_patch_name; + GET DIAGNOSTICS i = ROW_COUNT; + IF i < 1 THEN + RAISE EXCEPTION 'Patch % is not installed, so it can''t be uninstalled!', in_patch_name; + END IF; + + RETURN; +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.unregister_patch( TEXT ) IS 'Function to unregister patches in database. Dies if the patch is not registered, or if unregistering it would break dependencies.'; + +CREATE OR REPLACE FUNCTION _v.assert_patch_is_applied( IN in_patch_name TEXT ) RETURNS TEXT as $$ +DECLARE + t_text TEXT; +BEGIN + SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_patch_name; + IF NOT FOUND THEN + RAISE EXCEPTION 'Patch % is not applied!', in_patch_name; + END IF; + RETURN format('Patch %s is applied.', in_patch_name); +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.assert_patch_is_applied( TEXT ) IS 'Function that can be used to make sure that patch has been applied.'; + +CREATE OR REPLACE FUNCTION _v.assert_user_is_superuser() RETURNS TEXT as $$ +DECLARE + v_super bool; +BEGIN + SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user; + IF v_super THEN + RETURN 'assert_user_is_superuser: OK'; + END IF; + RAISE EXCEPTION 'Current user is not superuser - cannot continue.'; +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.assert_user_is_superuser() IS 'Function that can be used to make sure that patch is being applied using superuser account.'; + +CREATE OR REPLACE FUNCTION _v.assert_user_is_not_superuser() RETURNS TEXT as $$ +DECLARE + v_super bool; +BEGIN + SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user; + IF v_super THEN + RAISE EXCEPTION 'Current user is superuser - cannot continue.'; + END IF; + RETURN 'assert_user_is_not_superuser: OK'; +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.assert_user_is_not_superuser() IS 'Function that can be used to make sure that patch is being applied using normal (not superuser) account.'; + +CREATE OR REPLACE FUNCTION _v.assert_user_is_one_of(VARIADIC p_acceptable_users TEXT[] ) RETURNS TEXT as $$ +DECLARE +BEGIN + IF current_user = any( p_acceptable_users ) THEN + RETURN 'assert_user_is_one_of: OK'; + END IF; + RAISE EXCEPTION 'User is not one of: % - cannot continue.', p_acceptable_users; +END; +$$ language plpgsql; +COMMENT ON FUNCTION _v.assert_user_is_one_of(TEXT[]) IS 'Function that can be used to make sure that patch is being applied by one of defined users.'; + +COMMIT; diff --git a/src/exchangedb/exchange-0001.sql b/src/exchangedb/exchange-0001.sql new file mode 100644 index 000000000..02dc68cf4 --- /dev/null +++ b/src/exchangedb/exchange-0001.sql @@ -0,0 +1,368 @@ +-- +-- This file is part of TALER +-- Copyright (C) 2014--2020 Taler Systems SA +-- +-- TALER is free software; you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation; either version 3, or (at your option) any later version. +-- +-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +-- A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- TALER; see the file COPYING. If not, see +-- + +-- Everything in one big transaction +BEGIN; + +-- Check patch versioning is in place. +SELECT _v.register_patch('exchange-0001', NULL, NULL); + + +-- Main denominations table. All the coins the exchange knows about. +CREATE TABLE IF NOT EXISTS denominations + (denom_pub_hash BYTEA PRIMARY KEY CHECK (LENGTH(denom_pub_hash)=64) + ,denom_pub BYTEA NOT NULL + ,master_pub BYTEA NOT NULL CHECK (LENGTH(master_pub)=32) + ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64) + ,valid_from INT8 NOT NULL + ,expire_withdraw INT8 NOT NULL + ,expire_deposit INT8 NOT NULL + ,expire_legal INT8 NOT NULL + ,coin_val INT8 NOT NULL + ,coin_frac INT4 NOT NULL + ,fee_withdraw_val INT8 NOT NULL + ,fee_withdraw_frac INT4 NOT NULL + ,fee_deposit_val INT8 NOT NULL + ,fee_deposit_frac INT4 NOT NULL + ,fee_refresh_val INT8 NOT NULL + ,fee_refresh_frac INT4 NOT NULL + ,fee_refund_val INT8 NOT NULL + ,fee_refund_frac INT4 NOT NULL + ); +CREATE INDEX IF NOT EXISTS denominations_expire_legal_index + ON denominations + (expire_legal); + +-- denomination_revocations table is for remembering which denomination keys have been revoked +CREATE TABLE IF NOT EXISTS denomination_revocations + (denom_revocations_serial_id BIGSERIAL UNIQUE + ,denom_pub_hash BYTEA PRIMARY KEY REFERENCES denominations (denom_pub_hash) ON DELETE CASCADE + ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64) + ); +-- reserves table is for summarization of a reserve. It is updated when new +-- funds are added and existing funds are withdrawn. The 'expiration_date' +-- can be used to eventually get rid of reserves that have not been used +-- for a very long time (usually by refunding the owner) +CREATE TABLE IF NOT EXISTS reserves + (reserve_pub BYTEA PRIMARY KEY CHECK(LENGTH(reserve_pub)=32) + ,account_details TEXT NOT NULL + ,current_balance_val INT8 NOT NULL + ,current_balance_frac INT4 NOT NULL + ,expiration_date INT8 NOT NULL + ,gc_date INT8 NOT NULL + ); +-- index on reserves table (TODO: useless due to primary key!?) +CREATE INDEX IF NOT EXISTS reserves_reserve_pub_index + ON reserves + (reserve_pub); +-- index for get_expired_reserves +CREATE INDEX IF NOT EXISTS reserves_expiration_index + ON reserves + (expiration_date + ,current_balance_val + ,current_balance_frac + ); +-- index for reserve GC operations +CREATE INDEX IF NOT EXISTS reserves_gc_index + ON reserves + (gc_date); +-- reserves_in table collects the transactions which transfer funds +-- into the reserve. The rows of this table correspond to each +-- incoming transaction. +CREATE TABLE IF NOT EXISTS reserves_in + (reserve_in_serial_id BIGSERIAL UNIQUE + ,reserve_pub BYTEA NOT NULL REFERENCES reserves (reserve_pub) ON DELETE CASCADE + ,wire_reference INT8 NOT NULL + ,credit_val INT8 NOT NULL + ,credit_frac INT4 NOT NULL + ,sender_account_details TEXT NOT NULL + ,exchange_account_section TEXT NOT NULL + ,execution_date INT8 NOT NULL + ,PRIMARY KEY (reserve_pub, wire_reference) + ); +-- Create indices on reserves_in +CREATE INDEX IF NOT EXISTS reserves_in_execution_index + ON reserves_in + (exchange_account_section + ,execution_date + ); +CREATE INDEX IF NOT EXISTS reserves_in_exchange_account_serial + ON reserves_in + (exchange_account_section, + reserve_in_serial_id DESC + ); +-- This table contains the data for wire transfers the exchange has +-- executed to close a reserve. +CREATE TABLE IF NOT EXISTS reserves_close + (close_uuid BIGSERIAL PRIMARY KEY + ,reserve_pub BYTEA NOT NULL REFERENCES reserves (reserve_pub) ON DELETE CASCADE + ,execution_date INT8 NOT NULL + ,wtid BYTEA NOT NULL CHECK (LENGTH(wtid)=32) + ,receiver_account TEXT NOT NULL + ,amount_val INT8 NOT NULL + ,amount_frac INT4 NOT NULL + ,closing_fee_val INT8 NOT NULL + ,closing_fee_frac INT4 NOT NULL); +CREATE INDEX IF NOT EXISTS reserves_close_by_reserve + ON reserves_close + (reserve_pub); +-- Table with the withdraw operations that have been performed on a reserve. +-- The 'h_blind_ev' is the hash of the blinded coin. It serves as a primary +-- key, as (broken) clients that use a non-random coin and blinding factor +-- should fail to even withdraw, as otherwise the coins will fail to deposit +-- (as they really must be unique). +-- For the denom_pub, we do NOT CASCADE on DELETE, we may keep the denomination key alive! +CREATE TABLE IF NOT EXISTS reserves_out + (reserve_out_serial_id BIGSERIAL UNIQUE + ,h_blind_ev BYTEA PRIMARY KEY CHECK (LENGTH(h_blind_ev)=64) + ,denom_pub_hash BYTEA NOT NULL REFERENCES denominations (denom_pub_hash) + ,denom_sig BYTEA NOT NULL + ,reserve_pub BYTEA NOT NULL REFERENCES reserves (reserve_pub) ON DELETE CASCADE + ,reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64) + ,execution_date INT8 NOT NULL + ,amount_with_fee_val INT8 NOT NULL + ,amount_with_fee_frac INT4 NOT NULL + ); +-- Index blindcoins(reserve_pub) for get_reserves_out statement +CREATE INDEX IF NOT EXISTS reserves_out_reserve_pub_index + ON reserves_out + (reserve_pub); +CREATE INDEX IF NOT EXISTS reserves_out_execution_date + ON reserves_out + (execution_date); +CREATE INDEX IF NOT EXISTS reserves_out_for_get_withdraw_info + ON reserves_out + (denom_pub_hash + ,h_blind_ev + ); +-- Table with coins that have been (partially) spent, used to track +-- coin information only once. +CREATE TABLE IF NOT EXISTS known_coins + (coin_pub BYTEA NOT NULL PRIMARY KEY CHECK (LENGTH(coin_pub)=32) + ,denom_pub_hash BYTEA NOT NULL REFERENCES denominations (denom_pub_hash) ON DELETE CASCADE + ,denom_sig BYTEA NOT NULL + ); +CREATE INDEX IF NOT EXISTS known_coins_by_denomination + ON known_coins + (denom_pub_hash); +-- Table with the commitments made when melting a coin. */ +CREATE TABLE IF NOT EXISTS refresh_commitments + (melt_serial_id BIGSERIAL UNIQUE + ,rc BYTEA PRIMARY KEY CHECK (LENGTH(rc)=64) + ,old_coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) ON DELETE CASCADE + ,old_coin_sig BYTEA NOT NULL CHECK(LENGTH(old_coin_sig)=64) + ,amount_with_fee_val INT8 NOT NULL + ,amount_with_fee_frac INT4 NOT NULL + ,noreveal_index INT4 NOT NULL + ); +CREATE INDEX IF NOT EXISTS refresh_commitments_old_coin_pub_index + ON refresh_commitments + (old_coin_pub); +-- Table with the revelations about the new coins that are to be created +-- during a melting session. Includes the session, the cut-and-choose +-- index and the index of the new coin, and the envelope of the new +-- coin to be signed, as well as the encrypted information about the +-- private key and the blinding factor for the coin (for verification +-- in case this newcoin_index is chosen to be revealed) +CREATE TABLE IF NOT EXISTS refresh_revealed_coins + (rc BYTEA NOT NULL REFERENCES refresh_commitments (rc) ON DELETE CASCADE + ,newcoin_index INT4 NOT NULL + ,link_sig BYTEA NOT NULL CHECK(LENGTH(link_sig)=64) + ,denom_pub_hash BYTEA NOT NULL REFERENCES denominations (denom_pub_hash) ON DELETE CASCADE + ,coin_ev BYTEA UNIQUE NOT NULL + ,h_coin_ev BYTEA NOT NULL CHECK(LENGTH(h_coin_ev)=64) + ,ev_sig BYTEA NOT NULL + ,PRIMARY KEY (rc, newcoin_index) + ,UNIQUE (h_coin_ev) + ); +CREATE INDEX IF NOT EXISTS refresh_revealed_coins_coin_pub_index + ON refresh_revealed_coins + (denom_pub_hash); +-- Table with the transfer keys of a refresh operation; includes +-- the rc for which this is the link information, the +-- transfer public key (for gamma) and the revealed transfer private +-- keys (array of TALER_CNC_KAPPA - 1 entries, with gamma being skipped) */ +CREATE TABLE IF NOT EXISTS refresh_transfer_keys + (rc BYTEA NOT NULL PRIMARY KEY REFERENCES refresh_commitments (rc) ON DELETE CASCADE + ,transfer_pub BYTEA NOT NULL CHECK(LENGTH(transfer_pub)=32) + ,transfer_privs BYTEA NOT NULL + ); +-- for get_link (not sure if this helps, as there should be very few +-- transfer_pubs per rc, but at least in theory this helps the ORDER BY +-- clause. +CREATE INDEX IF NOT EXISTS refresh_transfer_keys_coin_tpub + ON refresh_transfer_keys + (rc + ,transfer_pub + ); +-- This table contains the wire transfers the exchange is supposed to +-- execute to transmit funds to the merchants (and manage refunds). +CREATE TABLE IF NOT EXISTS deposits + (deposit_serial_id BIGSERIAL PRIMARY KEY + ,coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) ON DELETE CASCADE + ,amount_with_fee_val INT8 NOT NULL + ,amount_with_fee_frac INT4 NOT NULL + ,timestamp INT8 NOT NULL + ,refund_deadline INT8 NOT NULL + ,wire_deadline INT8 NOT NULL + ,merchant_pub BYTEA NOT NULL CHECK (LENGTH(merchant_pub)=32) + ,h_contract_terms BYTEA NOT NULL CHECK (LENGTH(h_contract_terms)=64) + ,h_wire BYTEA NOT NULL CHECK (LENGTH(h_wire)=64) + ,coin_sig BYTEA NOT NULL CHECK (LENGTH(coin_sig)=64) + ,wire TEXT NOT NULL + ,tiny BOOLEAN NOT NULL DEFAULT FALSE + ,done BOOLEAN NOT NULL DEFAULT FALSE + ,UNIQUE (coin_pub, merchant_pub, h_contract_terms) + ); +-- Index for get_deposit_for_wtid and get_deposit_statement */ +CREATE INDEX IF NOT EXISTS deposits_coin_pub_merchant_contract_index + ON deposits + (coin_pub + ,merchant_pub + ,h_contract_terms + ); +-- Index for deposits_get_ready +CREATE INDEX IF NOT EXISTS deposits_get_ready_index + ON deposits + (tiny + ,done + ,wire_deadline + ,refund_deadline + ); +-- Index for deposits_iterate_matching +CREATE INDEX IF NOT EXISTS deposits_iterate_matching + ON deposits + (merchant_pub + ,h_wire + ,done + ,wire_deadline + ); +-- Table with information about coins that have been refunded. (Technically +-- one of the deposit operations that a coin was involved with is refunded.) +-- The combo of coin_pub, merchant_pub, h_contract_terms and rtransaction_id +-- MUST be unique, and we usually select by coin_pub so that one goes first. */ +CREATE TABLE IF NOT EXISTS refunds + (refund_serial_id BIGSERIAL UNIQUE + ,coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) ON DELETE CASCADE + ,merchant_pub BYTEA NOT NULL CHECK(LENGTH(merchant_pub)=32) + ,merchant_sig BYTEA NOT NULL CHECK(LENGTH(merchant_sig)=64) + ,h_contract_terms BYTEA NOT NULL CHECK(LENGTH(h_contract_terms)=64) + ,rtransaction_id INT8 NOT NULL + ,amount_with_fee_val INT8 NOT NULL + ,amount_with_fee_frac INT4 NOT NULL + ,PRIMARY KEY (coin_pub, merchant_pub, h_contract_terms, rtransaction_id) + ); +CREATE INDEX IF NOT EXISTS refunds_coin_pub_index + ON refunds + (coin_pub); +-- This table contains the data for +-- wire transfers the exchange has executed. +CREATE TABLE IF NOT EXISTS wire_out + (wireout_uuid BIGSERIAL PRIMARY KEY + ,execution_date INT8 NOT NULL + ,wtid_raw BYTEA UNIQUE NOT NULL CHECK (LENGTH(wtid_raw)=32) + ,wire_target TEXT NOT NULL + ,exchange_account_section TEXT NOT NULL + ,amount_val INT8 NOT NULL + ,amount_frac INT4 NOT NULL + ); +-- Table for the tracking API, mapping from wire transfer identifier +-- to transactions and back +CREATE TABLE IF NOT EXISTS aggregation_tracking + (aggregation_serial_id BIGSERIAL UNIQUE + ,deposit_serial_id INT8 PRIMARY KEY REFERENCES deposits (deposit_serial_id) ON DELETE CASCADE + ,wtid_raw BYTEA CONSTRAINT wire_out_ref REFERENCES wire_out(wtid_raw) ON DELETE CASCADE DEFERRABLE + ); +-- Index for lookup_transactions statement on wtid +CREATE INDEX IF NOT EXISTS aggregation_tracking_wtid_index + ON aggregation_tracking + (wtid_raw); +-- Table for the wire fees. +CREATE TABLE IF NOT EXISTS wire_fee + (wire_method VARCHAR NOT NULL + ,start_date INT8 NOT NULL + ,end_date INT8 NOT NULL + ,wire_fee_val INT8 NOT NULL + ,wire_fee_frac INT4 NOT NULL + ,closing_fee_val INT8 NOT NULL + ,closing_fee_frac INT4 NOT NULL + ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64) + ,PRIMARY KEY (wire_method, start_date) + ); +CREATE INDEX IF NOT EXISTS wire_fee_gc_index + ON wire_fee + (end_date); +-- Table for /recoup information +-- Do not cascade on the coin_pub, as we may keep the coin alive! */ +CREATE TABLE IF NOT EXISTS recoup + (recoup_uuid BIGSERIAL UNIQUE + ,coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) + ,coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64) + ,coin_blind BYTEA NOT NULL CHECK(LENGTH(coin_blind)=32) + ,amount_val INT8 NOT NULL + ,amount_frac INT4 NOT NULL + ,timestamp INT8 NOT NULL + ,h_blind_ev BYTEA NOT NULL REFERENCES reserves_out (h_blind_ev) ON DELETE CASCADE + ); +CREATE INDEX IF NOT EXISTS recoup_by_coin_index + ON recoup + (coin_pub); +CREATE INDEX IF NOT EXISTS recoup_by_h_blind_ev + ON recoup + (h_blind_ev); +CREATE INDEX IF NOT EXISTS recoup_for_by_reserve + ON recoup + (coin_pub + ,h_blind_ev + ); +-- Table for /recoup-refresh information +-- Do not cascade on the coin_pub, as we may keep the coin alive! */ +CREATE TABLE IF NOT EXISTS recoup_refresh + (recoup_refresh_uuid BIGSERIAL UNIQUE + ,coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) + ,coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64) + ,coin_blind BYTEA NOT NULL CHECK(LENGTH(coin_blind)=32) + ,amount_val INT8 NOT NULL + ,amount_frac INT4 NOT NULL + ,timestamp INT8 NOT NULL + ,h_blind_ev BYTEA NOT NULL REFERENCES refresh_revealed_coins (h_coin_ev) ON DELETE CASCADE + ); +CREATE INDEX IF NOT EXISTS recoup_refresh_by_coin_index + ON recoup_refresh + (coin_pub); +CREATE INDEX IF NOT EXISTS recoup_refresh_by_h_blind_ev + ON recoup_refresh + (h_blind_ev); +CREATE INDEX IF NOT EXISTS recoup_refresh_for_by_reserve + ON recoup_refresh + (coin_pub + ,h_blind_ev + ); +-- This table contains the pre-commit data for +-- wire transfers the exchange is about to execute. +CREATE TABLE IF NOT EXISTS prewire + (prewire_uuid BIGSERIAL PRIMARY KEY + ,type TEXT NOT NULL + ,finished BOOLEAN NOT NULL DEFAULT false + ,buf BYTEA NOT NULL + ); +-- Index for wire_prepare_data_get and gc_prewire statement +CREATE INDEX IF NOT EXISTS prepare_iteration_index + ON prewire + (finished); + +-- Complete transaction +COMMIT; diff --git a/src/exchangedb/plugin_exchangedb_postgres.c b/src/exchangedb/plugin_exchangedb_postgres.c index fe0021fa6..9df2fe770 100644 --- a/src/exchangedb/plugin_exchangedb_postgres.c +++ b/src/exchangedb/plugin_exchangedb_postgres.c @@ -113,12 +113,6 @@ struct PostgresClosure */ const struct GNUNET_CONFIGURATION_Handle *cfg; - /** - * Database connection string, as read from - * the configuration. - */ - char *connection_cfg_str; - /** * Directory with SQL statements to run to create tables. */ @@ -163,16 +157,12 @@ postgres_drop_tables (void *cls) { struct PostgresClosure *pc = cls; struct GNUNET_PQ_Context *conn; - char *drop_dir; - - GNUNET_asprintf (&drop_dir, - "%sdrop", - pc->sql_dir); - conn = GNUNET_PQ_connect (pc->connection_cfg_str, - drop_dir, - NULL, - NULL); - GNUNET_free (drop_dir); + + conn = GNUNET_PQ_connect_with_cfg (pc->cfg, + "exchangedb-postgres", + "drop", + NULL, + NULL); if (NULL == conn) return GNUNET_SYSERR; GNUNET_PQ_disconnect (conn); @@ -194,7 +184,7 @@ postgres_create_tables (void *cls) conn = GNUNET_PQ_connect_with_cfg (pc->cfg, "exchangedb-postgres", - "", + "exchange-", NULL, NULL); if (NULL == conn) @@ -1393,10 +1383,11 @@ postgres_get_session (void *cls) GNUNET_PQ_PREPARED_STATEMENT_END }; - db_conn = GNUNET_PQ_connect (pc->connection_cfg_str, - NULL, - es, - ps); + db_conn = GNUNET_PQ_connect_with_cfg (pc->cfg, + "exchangedb-postgres", + NULL, + es, + ps); } if (NULL == db_conn) return NULL; @@ -5377,10 +5368,11 @@ postgres_gc (void *cls) GNUNET_PQ_PREPARED_STATEMENT_END }; - conn = GNUNET_PQ_connect (pg->connection_cfg_str, - NULL, - NULL, - ps); + conn = GNUNET_PQ_connect_with_cfg (pg->cfg, + "exchangedb-postgres", + NULL, + NULL, + ps); } if (NULL == conn) return GNUNET_SYSERR; @@ -7221,7 +7213,6 @@ libtaler_plugin_exchangedb_postgres_init (void *cls) const struct GNUNET_CONFIGURATION_Handle *cfg = cls; struct PostgresClosure *pg; struct TALER_EXCHANGEDB_Plugin *plugin; - const char *ec; pg = GNUNET_new (struct PostgresClosure); pg->cfg = cfg; @@ -7246,28 +7237,6 @@ libtaler_plugin_exchangedb_postgres_init (void *cls) GNUNET_free (pg); return NULL; } - ec = getenv ("TALER_EXCHANGEDB_POSTGRES_CONFIG"); - if (NULL != ec) - { - pg->connection_cfg_str = GNUNET_strdup (ec); - } - else - { - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, - "exchangedb-postgres", - "CONFIG", - &pg->connection_cfg_str)) - { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "exchangedb-postgres", - "CONFIG"); - GNUNET_free (pg->sql_dir); - GNUNET_free (pg); - return NULL; - } - } - if ( (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "exchangedb", @@ -7283,7 +7252,6 @@ libtaler_plugin_exchangedb_postgres_init (void *cls) GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "exchangedb", "LEGAL/IDLE_RESERVE_EXPIRATION_TIME"); - GNUNET_free (pg->connection_cfg_str); GNUNET_free (pg->sql_dir); GNUNET_free (pg); return NULL; @@ -7297,7 +7265,6 @@ libtaler_plugin_exchangedb_postgres_init (void *cls) GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "taler", "CURRENCY"); - GNUNET_free (pg->connection_cfg_str); GNUNET_free (pg->sql_dir); GNUNET_free (pg); return NULL; @@ -7413,7 +7380,6 @@ libtaler_plugin_exchangedb_postgres_done (void *cls) /* If we launched a session for the main thread, kill it here before we unload */ db_conn_destroy (pg->main_session); - GNUNET_free (pg->connection_cfg_str); GNUNET_free (pg->sql_dir); GNUNET_free (pg->currency); GNUNET_free (pg); -- cgit v1.2.3