more work on SQL refactoring

This commit is contained in:
Christian Grothoff 2022-11-27 15:31:39 +01:00
parent 85ce53a49b
commit 2eff222c52
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
10 changed files with 684 additions and 367 deletions

View File

@ -14,8 +14,8 @@
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
CREATE OR REPLACE FUNCTION create_table_account_merges(
IN shard_suffix VARCHAR DEFAULT NULL
CREATE FUNCTION create_table_account_merges(
IN partition_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
@ -23,44 +23,85 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'account_merges';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I '
'(account_merge_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE
',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)' -- REFERENCES reserves (reserve_pub) ON DELETE CASCADE
'(account_merge_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)'
',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)' -- REFERENCES purse_requests (purse_pub)
',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
',wallet_h_payto BYTEA NOT NULL CHECK (LENGTH(wallet_h_payto)=32)'
',PRIMARY KEY (purse_pub)'
') %s ;'
,table_name
,'PARTITION BY HASH (purse_pub)'
,shard_suffix
,partition_suffix
);
table_name = concat_ws('_', table_name, shard_suffix);
-- FIXME: change to materialized index by reserve_pub!
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_reserve_pub '
'ON ' || table_name || ' '
'(reserve_pub);'
PERFORM comment_partitioned_table(
'Merge requests where a purse- and account-owner requested merging the purse into the account'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'public key of the target reserve'
,'reserve_pub'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'public key of the purse'
,'purse_pub'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'signature by the reserve private key affirming the merge, of type TALER_SIGNATURE_WALLET_ACCOUNT_MERGE'
,'reserve_sig'
,table_name
,partition_suffix
);
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_account_merges_partition(
CREATE FUNCTION constrain_table_account_merges(
IN partition_suffix VARCHAR
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'account_merges';
BEGIN
table_name = concat_ws('_', table_name, partition_suffix);
-- FIXME: change to materialized index by reserve_pub!?
EXECUTE FORMAT (
'CREATE INDEX ' || table_name || '_by_reserve_pub '
'ON ' || table_name || ' '
'(reserve_pub);'
);
EXECUTE FORMAT (
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_account_merge_request_serial_id_key'
' UNIQUE (account_merge_request_serial_id) '
);
END
$$;
CREATE FUNCTION foreign_table_account_merges()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'account_merges';
BEGIN
EXECUTE FORMAT (
'ALTER TABLE account_merges_' || partition_suffix || ' '
'ADD CONSTRAINT account_merges_' || partition_suffix || '_account_merge_request_serial_id_key '
'UNIQUE (account_merge_request_serial_id) '
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub'
' REFERENCES reserves (reserve_pub) ON DELETE CASCADE'
',ADD CONSTRAINT ' || table_name || '_foreign_purse_pub'
' REFERENCES purse_requests (purse_pub)'
);
END
$$;
@ -77,4 +118,14 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
,FALSE),
('account_merges'
,'exchange-0002'
,'constrain'
,TRUE
,FALSE),
('account_merges'
,'exchange-0002'
,'foreign'
,TRUE
,FALSE);

View File

@ -14,8 +14,8 @@
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
CREATE OR REPLACE FUNCTION create_table_close_requests(
IN shard_suffix VARCHAR DEFAULT NULL
CREATE FUNCTION create_table_close_requests(
IN partition_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
@ -23,11 +23,10 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'close_requests';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I '
'(close_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE'
',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)' -- REFERENCES reserves(reserve_pub) ON DELETE CASCADE
'CREATE TABLE %I '
'(close_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)'
',close_timestamp INT8 NOT NULL'
',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
',close_val INT8 NOT NULL'
@ -40,13 +39,41 @@ BEGIN
') %s ;'
,table_name
,'PARTITION BY HASH (reserve_pub)'
,shard_suffix
,partition_suffix
);
END
$$;
PERFORM comment_partitioned_table(
'Explicit requests by a reserve owner to close a reserve immediately'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'When the request was created by the client'
,'close_timestamp'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Signature affirming that the reserve is to be closed'
,'reserve_sig'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Balance of the reserve at the time of closing, to be wired to the associated bank account (minus the closing fee)'
,'close_val'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Identifies the credited bank account. Optional.'
,'payto_uri'
,table_name
,partition_suffix
);
END $$;
CREATE OR REPLACE FUNCTION add_constraints_to_close_requests(
CREATE FUNCTION constrain_table_close_requests(
IN partition_suffix VARCHAR
)
RETURNS VOID
@ -55,31 +82,37 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'close_requests';
BEGIN
table_name = concat_ws('_', table_name, partition_suffix);
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_close_request_uuid_index '
'CREATE INDEX ' || table_name || '_by_close_request_uuid_index '
'ON ' || table_name || ' '
'(close_request_serial_id);'
);
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_close_request_done_index '
'CREATE INDEX ' || table_name || '_by_close_request_done_index '
'ON ' || table_name || ' '
'(done);'
);
EXECUTE FORMAT (
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_close_request_uuid_pkey'
' UNIQUE (close_request_serial_id)'
);
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_close_requests_partition(
IN partition_suffix VARCHAR
)
RETURNS void
CREATE FUNCTION foreign_table_close_requests()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'close_requests';
BEGIN
EXECUTE FORMAT (
'ALTER TABLE close_requests_' || partition_suffix || ' '
'ADD CONSTRAINT close_requests_' || partition_suffix || '_close_request_uuid_pkey '
'UNIQUE (close_request_serial_id)'
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub'
' REFERENCES reserves(reserve_pub) ON DELETE CASCADE'
);
END
$$;
@ -96,4 +129,14 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
,FALSE),
('close_requests'
,'exchange-0002'
,'constrain'
,TRUE
,FALSE),
('close_requests'
,'exchange-0002'
,'foreign'
,TRUE
,FALSE);

View File

@ -15,8 +15,8 @@
--
CREATE OR REPLACE FUNCTION create_table_contracts(
IN shard_suffix VARCHAR DEFAULT NULL
CREATE FUNCTION create_table_contracts(
IN partition_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
@ -24,36 +24,67 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'contracts';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I '
'(contract_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
',pub_ckey BYTEA NOT NULL CHECK (LENGTH(pub_ckey)=32)'
',contract_sig BYTEA NOT NULL CHECK (LENGTH(contract_sig)=64)'
',e_contract BYTEA NOT NULL'
',purse_expiration INT8 NOT NULL'
',PRIMARY KEY (purse_pub)'
') %s ;'
'CREATE TABLE %I '
'(contract_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
',pub_ckey BYTEA NOT NULL CHECK (LENGTH(pub_ckey)=32)'
',contract_sig BYTEA NOT NULL CHECK (LENGTH(contract_sig)=64)'
',e_contract BYTEA NOT NULL'
',purse_expiration INT8 NOT NULL'
',PRIMARY KEY (purse_pub)'
') %s ;'
,table_name
,'PARTITION BY HASH (purse_pub)'
,shard_suffix
,partition_suffix
);
PERFORM comment_partitioned_table(
'encrypted contracts associated with purses'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'public key of the purse that the contract is associated with'
,'purse_pub'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'signature over the encrypted contract by the purse contract key'
,'contract_sig'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Public ECDH key used to encrypt the contract, to be used with the purse private key for decryption'
,'pub_ckey'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'AES-GCM encrypted contract terms (contains gzip compressed JSON after decryption)'
,'e_contract'
,table_name
,partition_suffix
);
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_contracts_partition(
CREATE FUNCTION constrain_table_contracts(
IN partition_suffix VARCHAR
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'contracts';
BEGIN
table_name = concat_ws('_', table_name, partition_suffix);
EXECUTE FORMAT (
'ALTER TABLE contracts_' || partition_suffix || ' '
'ADD CONSTRAINT contracts_' || partition_suffix || '_contract_serial_id_key '
'UNIQUE (contract_serial_id) '
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_contract_serial_id_key'
' UNIQUE (contract_serial_id) '
);
END
$$;
@ -70,4 +101,9 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
,FALSE),
('contracts'
,'exchange-0002'
,'constrain'
,TRUE
,FALSE);

View File

@ -15,7 +15,7 @@
--
CREATE OR REPLACE FUNCTION create_table_history_requests(
CREATE OR create_table_history_requests(
IN shard_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
@ -24,11 +24,10 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'history_requests';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I '
'(history_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE'
',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)' -- REFERENCES reserves(reserve_pub) ON DELETE CASCADE
'CREATE TABLE %I '
'(history_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)'
',request_timestamp INT8 NOT NULL'
',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
',history_fee_val INT8 NOT NULL'
@ -39,9 +38,45 @@ BEGIN
,'PARTITION BY HASH (reserve_pub)'
,shard_suffix
);
PERFORM comment_partitioned_table(
'Paid history requests issued by a client against a reserve'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'When was the history request made'
,'request_timestamp'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'Signature approving payment for the history request'
,'reserve_sig'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'History fee approved by the signature'
,'history_fee_val'
,table_name
,shard_suffix
);
END $$;
END
$$;
CREATE OR foreign_table_history_requests()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'history_requests';
BEGIN
EXECUTE FORMAT (
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub'
' REFERENCES reserves(reserve_pub) ON DELETE CASCADE'
);
END $$;
INSERT INTO exchange_tables
@ -55,4 +90,9 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
,FALSE),
('history_requests'
,'exchange-0002'
,'foreign'
,TRUE
,FALSE);

View File

@ -14,8 +14,8 @@
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
CREATE OR REPLACE FUNCTION create_table_purse_deposits(
IN shard_suffix VARCHAR DEFAULT NULL
CREATE FUNCTION create_table_purse_deposits(
IN partition_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
@ -23,13 +23,12 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'purse_deposits';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I '
'(purse_deposit_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE
',partner_serial_id INT8' -- REFERENCES partners(partner_serial_id) ON DELETE CASCADE'
'CREATE TABLE %I '
'(purse_deposit_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
',partner_serial_id INT8'
',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
',coin_pub BYTEA NOT NULL' -- REFERENCES known_coins (coin_pub) ON DELETE CASCADE'
',coin_pub BYTEA NOT NULL'
',amount_with_fee_val INT8 NOT NULL'
',amount_with_fee_frac INT4 NOT NULL'
',coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64)'
@ -37,32 +36,85 @@ BEGIN
') %s ;'
,table_name
,'PARTITION BY HASH (purse_pub)'
,shard_suffix
,partition_suffix
);
table_name = concat_ws('_', table_name, shard_suffix);
-- FIXME: change to materialized index by coin_pub!
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_coin_pub '
'ON ' || table_name || ' '
'(coin_pub);'
PERFORM comment_partitioned_table(
'Requests depositing coins into a purse'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'identifies the partner exchange, NULL in case the target purse lives at this exchange'
,'partner_serial_id'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Public key of the purse'
,'purse_pub'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Public key of the coin being deposited'
,'coin_pub'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Total amount being deposited'
,'amount_with_fee_val'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Signature of the coin affirming the deposit into the purse, of type TALER_SIGNATURE_PURSE_DEPOSIT'
,'coin_sig'
,table_name
,partition_suffix
);
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_purse_deposits_partition(
CREATE FUNCTION constrain_table_purse_deposits(
IN partition_suffix VARCHAR
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'purse_deposits';
BEGIN
table_name = concat_ws('_', table_name, partition_suffix);
-- FIXME: change to materialized index by coin_pub!
EXECUTE FORMAT (
'CREATE INDEX ' || table_name || '_by_coin_pub'
' ON ' || table_name || ' (coin_pub);'
);
EXECUTE FORMAT (
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_purse_deposit_serial_id_key'
' UNIQUE (purse_deposit_serial_id) '
);
END
$$;
CREATE FUNCTION foreign_table_purse_deposits()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'purse_deposits';
BEGIN
EXECUTE FORMAT (
'ALTER TABLE purse_deposits_' || partition_suffix || ' '
'ADD CONSTRAINT purse_deposits_' || partition_suffix || '_purse_deposit_serial_id_key '
'UNIQUE (purse_deposit_serial_id) '
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_foreign_partner'
' REFERENCES partners(partner_serial_id) ON DELETE CASCADE'
',ADD CONSTRAINT ' || table_name || '_foreign_coin_pub'
' REFERENCES known_coins (coin_pub) ON DELETE CASCADE'
);
END
$$;
@ -79,4 +131,14 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
,FALSE),
('purse-deposits'
,'exchange-0002'
,'constrain'
,TRUE
,FALSE),
('purse-deposits'
,'exchange-0002'
,'foreign'
,TRUE
,FALSE);

View File

@ -14,8 +14,8 @@
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
CREATE OR REPLACE FUNCTION create_table_wad_in_entries(
IN shard_suffix VARCHAR DEFAULT NULL
CREATE FUNCTION create_table_wad_in_entries(
IN partition_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
@ -23,11 +23,10 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'wad_in_entries';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I '
'(wad_in_entry_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
',wad_in_serial_id INT8' -- REFERENCES wads_in (wad_in_serial_id) ON DELETE CASCADE
'CREATE TABLE %I '
'(wad_in_entry_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
',wad_in_serial_id INT8'
',reserve_pub BYTEA NOT NULL CHECK(LENGTH(reserve_pub)=32)'
',purse_pub BYTEA PRIMARY KEY CHECK(LENGTH(purse_pub)=32)'
',h_contract BYTEA NOT NULL CHECK(LENGTH(h_contract)=64)'
@ -44,14 +43,96 @@ BEGIN
') %s ;'
,table_name
,'PARTITION BY HASH (purse_pub)'
,shard_suffix
,partition_suffix
);
PERFORM comment_partitioned_table(
'list of purses aggregated in a wad according to the sending exchange'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'wad for which the given purse was included in the aggregation'
,'wad_in_serial_id'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'target account of the purse (must be at the local exchange)'
,'reserve_pub'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'public key of the purse that was merged'
,'purse_pub'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'hash of the contract terms of the purse'
,'h_contract'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Time when the purse was set to expire'
,'purse_expiration'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Time when the merge was approved'
,'merge_timestamp'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Total amount in the purse'
,'amount_with_fee_val'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Total wad fees paid by the purse'
,'wad_fee_val'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Total deposit fees paid when depositing coins into the purse'
,'deposit_fees_val'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Signature by the receiving reserve, of purpose TALER_SIGNATURE_ACCOUNT_MERGE'
,'reserve_sig'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Signature by the purse of purpose TALER_SIGNATURE_PURSE_MERGE'
,'purse_sig'
,table_name
,partition_suffix
);
END $$;
table_name = concat_ws('_', table_name, shard_suffix);
CREATE FUNCTION constrain_table_wad_in_entries(
IN partition_suffix VARCHAR
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'wad_in_entries';
BEGIN
table_name = concat_ws('_', table_name, partition_suffix);
-- FIXME: change to materialized index by reserve_pub!
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_reserve_pub '
'CREATE INDEX ' || table_name || '_reserve_pub '
'ON ' || table_name || ' '
'(reserve_pub);'
);
@ -59,24 +140,27 @@ BEGIN
'COMMENT ON INDEX ' || table_name || '_reserve_pub '
'IS ' || quote_literal('needed in reserve history computation') || ';'
);
EXECUTE FORMAT (
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_wad_in_entry_serial_id_key'
' UNIQUE (wad_in_entry_serial_id) '
);
END $$;
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_wad_in_entries_partition(
IN partition_suffix VARCHAR
)
CREATE FUNCTION foreign_table_wad_in_entries()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'wad_in_entries';
BEGIN
EXECUTE FORMAT (
'ALTER TABLE wad_in_entries_' || partition_suffix || ' '
'ADD CONSTRAINT wad_in_entries_' || partition_suffix || '_wad_in_entry_serial_id_key '
'UNIQUE (wad_in_entry_serial_id) '
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_foreign_wad_in'
' REFERENCES wads_in (wad_in_serial_id) ON DELETE CASCADE'
);
END
$$;
END $$;
INSERT INTO exchange_tables
@ -90,4 +174,14 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
,FALSE),
('wad_in_entries'
,'exchange-0002'
,'constrain'
,TRUE
,FALSE),
('wad_in_entries'
,'exchange-0002'
,'foreign'
,TRUE
,FALSE);

View File

@ -15,8 +15,8 @@
--
CREATE OR REPLACE FUNCTION create_table_wad_out_entries(
IN shard_suffix VARCHAR DEFAULT NULL
CREATE FUNCTION create_table_wad_out_entries(
IN partition_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
@ -24,53 +24,140 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'wad_out_entries';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I '
'(wad_out_entry_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
',wad_out_serial_id INT8' -- REFERENCES wads_out (wad_out_serial_id) ON DELETE CASCADE
',reserve_pub BYTEA NOT NULL CHECK(LENGTH(reserve_pub)=32)'
',purse_pub BYTEA PRIMARY KEY CHECK(LENGTH(purse_pub)=32)'
',h_contract BYTEA NOT NULL CHECK(LENGTH(h_contract)=64)'
',purse_expiration INT8 NOT NULL'
',merge_timestamp INT8 NOT NULL'
',amount_with_fee_val INT8 NOT NULL'
',amount_with_fee_frac INT4 NOT NULL'
',wad_fee_val INT8 NOT NULL'
',wad_fee_frac INT4 NOT NULL'
',deposit_fees_val INT8 NOT NULL'
',deposit_fees_frac INT4 NOT NULL'
',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
',purse_sig BYTEA NOT NULL CHECK (LENGTH(purse_sig)=64)'
') %s ;'
'CREATE TABLE %I '
'(wad_out_entry_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
',wad_out_serial_id INT8'
',reserve_pub BYTEA NOT NULL CHECK(LENGTH(reserve_pub)=32)'
',purse_pub BYTEA PRIMARY KEY CHECK(LENGTH(purse_pub)=32)'
',h_contract BYTEA NOT NULL CHECK(LENGTH(h_contract)=64)'
',purse_expiration INT8 NOT NULL'
',merge_timestamp INT8 NOT NULL'
',amount_with_fee_val INT8 NOT NULL'
',amount_with_fee_frac INT4 NOT NULL'
',wad_fee_val INT8 NOT NULL'
',wad_fee_frac INT4 NOT NULL'
',deposit_fees_val INT8 NOT NULL'
',deposit_fees_frac INT4 NOT NULL'
',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
',purse_sig BYTEA NOT NULL CHECK (LENGTH(purse_sig)=64)'
') %s ;'
,table_name
,'PARTITION BY HASH (purse_pub)'
,shard_suffix
,partition_suffix
);
table_name = concat_ws('_', table_name, shard_suffix);
-- FIXME: change to materialized index by reserve_pub!
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_reserve_pub '
'ON ' || table_name || ' '
'(reserve_pub);'
PERFORM comment_partitioned_table(
'Purses combined into a wad'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Wad the purse was part of'
,'wad_out_serial_id'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Target reserve for the purse'
,'reserve_pub'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Public key of the purse'
,'purse_pub'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Hash of the contract associated with the purse'
,'h_contract'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Time when the purse expires'
,'purse_expiration'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Time when the merge was approved'
,'merge_timestamp'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Total amount in the purse'
,'amount_with_fee_val'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Wad fee charged to the purse'
,'wad_fee_val'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Total deposit fees charged to the purse'
,'deposit_fees_val'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Signature by the receiving reserve, of purpose TALER_SIGNATURE_ACCOUNT_MERGE'
,'reserve_sig'
,table_name
,partition_suffix
);
PERFORM comment_partitioned_column(
'Signature by the purse of purpose TALER_SIGNATURE_PURSE_MERGE'
,'purse_sig'
,table_name
,partition_suffix
);
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_wad_out_entries_partition(
CREATE FUNCTION constrain_table_wad_out_entries(
IN partition_suffix VARCHAR
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'wad_out_entries';
BEGIN
table_name = concat_ws('_', table_name, partition_suffix);
-- FIXME: change to materialized index by reserve_pub!
EXECUTE FORMAT (
'CREATE INDEX ' || table_name || '_by_reserve_pub '
'ON ' || table_name || ' '
'(reserve_pub);'
);
EXECUTE FORMAT (
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_wad_out_entry_serial_id_key'
' UNIQUE (wad_out_entry_serial_id) '
);
END
$$;
CREATE FUNCTION foreign_table_wad_out_entries()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'wad_out_entries';
BEGIN
EXECUTE FORMAT (
'ALTER TABLE wad_out_entries_' || partition_suffix || ' '
'ADD CONSTRAINT wad_out_entries_' || partition_suffix || '_wad_out_entry_serial_id_key '
'UNIQUE (wad_out_entry_serial_id) '
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_foreign_wad_out'
' REFERENCES wads_out (wad_out_serial_id) ON DELETE CASCADE'
);
END
$$;
@ -87,4 +174,14 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
,FALSE),
('wad_out_entries'
,'exchange-0002'
,'constrain'
,TRUE
,FALSE),
('wad_out_entries'
,'exchange-0002'
,'foreign'
,TRUE
,FALSE);

View File

@ -14,7 +14,7 @@
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
CREATE OR REPLACE FUNCTION create_table_wads_in(
CREATE FUNCTION create_table_wads_in(
IN shard_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
@ -23,41 +23,70 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'wads_in';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I '
'(wad_in_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
',wad_id BYTEA PRIMARY KEY CHECK (LENGTH(wad_id)=24)'
',origin_exchange_url TEXT NOT NULL'
',amount_val INT8 NOT NULL'
',amount_frac INT4 NOT NULL'
',arrival_time INT8 NOT NULL'
',UNIQUE (wad_id, origin_exchange_url)'
') %s ;'
'CREATE TABLE %I '
'(wad_in_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
',wad_id BYTEA PRIMARY KEY CHECK (LENGTH(wad_id)=24)'
',origin_exchange_url TEXT NOT NULL'
',amount_val INT8 NOT NULL'
',amount_frac INT4 NOT NULL'
',arrival_time INT8 NOT NULL'
',UNIQUE (wad_id, origin_exchange_url)'
') %s ;'
,table_name
,'PARTITION BY HASH (wad_id)'
,shard_suffix
);
PERFORM comment_partitioned_table(
'Incoming exchange-to-exchange wad wire transfers'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'Unique identifier of the wad, part of the wire transfer subject'
,'wad_id'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'Base URL of the originating URL, also part of the wire transfer subject'
,'origin_exchange_url'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'Actual amount that was received by our exchange'
,'amount_val'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'Time when the wad was received'
,'arrival_time'
,table_name
,shard_suffix
);
END $$;
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_wads_in_partition(
CREATE FUNCTION constrain_table_wads_in(
IN partition_suffix VARCHAR
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'wads_in';
BEGIN
table_name = concat_ws('_', table_name, partition_suffix);
EXECUTE FORMAT (
'ALTER TABLE wads_in_' || partition_suffix || ' '
'ADD CONSTRAINT wads_in_' || partition_suffix || '_wad_in_serial_id_key '
'UNIQUE (wad_in_serial_id) '
',ADD CONSTRAINT wads_in_' || partition_suffix || '_wad_is_origin_exchange_url_key '
'UNIQUE (wad_id, origin_exchange_url) '
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_wad_in_serial_id_key'
' UNIQUE (wad_in_serial_id) '
',ADD CONSTRAINT ' || table_name || '_wad_is_origin_exchange_url_key'
' UNIQUE (wad_id, origin_exchange_url) '
);
END
$$;
END $$;
INSERT INTO exchange_tables
@ -71,4 +100,9 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
,FALSE),
('wads_in'
,'exchange-0002'
,'constrain'
,TRUE
,FALSE);

View File

@ -14,7 +14,7 @@
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
CREATE OR REPLACE FUNCTION create_table_wads_out(
CREATE FUNCTION create_table_wads_out(
IN shard_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
@ -23,12 +23,11 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'wads_out';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I '
'(wad_out_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
'CREATE TABLE %I '
'(wad_out_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
',wad_id BYTEA PRIMARY KEY CHECK (LENGTH(wad_id)=24)'
',partner_serial_id INT8 NOT NULL' -- REFERENCES partners(partner_serial_id) ON DELETE CASCADE
',partner_serial_id INT8 NOT NULL'
',amount_val INT8 NOT NULL'
',amount_frac INT4 NOT NULL'
',execution_time INT8 NOT NULL'
@ -37,21 +36,69 @@ BEGIN
,'PARTITION BY HASH (wad_id)'
,shard_suffix
);
PERFORM comment_partitioned_table(
'Wire transfers made to another exchange to transfer purse funds'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'Unique identifier of the wad, part of the wire transfer subject'
,'wad_id'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'target exchange of the wad'
,'partner_serial_id'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'Amount that was wired'
,'amount_val'
,table_name
,shard_suffix
);
PERFORM comment_partitioned_column(
'Time when the wire transfer was scheduled'
,'execution_time'
,table_name
,shard_suffix
);
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_wads_out_partition(
CREATE FUNCTION constrain_table_wads_out(
IN partition_suffix VARCHAR
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'wads_out';
BEGIN
table_name = concat_ws('_', table_name, partition_suffix);
EXECUTE FORMAT (
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_wad_out_serial_id_key'
' UNIQUE (wad_out_serial_id) '
);
END
$$;
CREATE FUNCTION foreign_table_wads_out()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR DEFAULT 'wads_out';
BEGIN
EXECUTE FORMAT (
'ALTER TABLE wads_out_' || partition_suffix || ' '
'ADD CONSTRAINT wads_out_' || partition_suffix || '_wad_out_serial_id_key '
'UNIQUE (wad_out_serial_id) '
'ALTER TABLE ' || table_name ||
' ADD CONSTRAINT ' || table_name || '_foreign_partner'
' REFERENCES partners(partner_serial_id) ON DELETE CASCADE'
);
END
$$;
@ -68,4 +115,14 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
,FALSE),
('wads_out'
,'exchange-0002'
,'constrain'
,TRUE
,FALSE),
('wads_out'
,'exchange-0002'
,'foreign'
,TRUE
,FALSE);

View File

@ -1,197 +0,0 @@
--
-- This file is part of TALER
-- Copyright (C) 2014--2022 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 <http://www.gnu.org/licenses/>
--
-- ------------------------------ account_merges ----------------------------------------
SELECT create_table_account_merges();
COMMENT ON TABLE account_merges
IS 'Merge requests where a purse- and account-owner requested merging the purse into the account';
COMMENT ON COLUMN account_merges.reserve_pub
IS 'public key of the target reserve';
COMMENT ON COLUMN account_merges.purse_pub
IS 'public key of the purse';
COMMENT ON COLUMN account_merges.reserve_sig
IS 'signature by the reserve private key affirming the merge, of type TALER_SIGNATURE_WALLET_ACCOUNT_MERGE';
SELECT add_constraints_to_account_merges_partition('default');
-- ------------------------------ contracts ----------------------------------------
SELECT create_table_contracts();
COMMENT ON TABLE contracts
IS 'encrypted contracts associated with purses';
COMMENT ON COLUMN contracts.purse_pub
IS 'public key of the purse that the contract is associated with';
COMMENT ON COLUMN contracts.contract_sig
IS 'signature over the encrypted contract by the purse contract key';
COMMENT ON COLUMN contracts.pub_ckey
IS 'Public ECDH key used to encrypt the contract, to be used with the purse private key for decryption';
COMMENT ON COLUMN contracts.e_contract
IS 'AES-GCM encrypted contract terms (contains gzip compressed JSON after decryption)';
SELECT add_constraints_to_contracts_partition('default');
-- ------------------------------ history_requests ----------------------------------------
SELECT create_table_history_requests();
COMMENT ON TABLE history_requests
IS 'Paid history requests issued by a client against a reserve';
COMMENT ON COLUMN history_requests.request_timestamp
IS 'When was the history request made';
COMMENT ON COLUMN history_requests.reserve_sig
IS 'Signature approving payment for the history request';
COMMENT ON COLUMN history_requests.history_fee_val
IS 'History fee approved by the signature';
-- ------------------------------ close_requests ----------------------------------------
SELECT create_table_close_requests();
COMMENT ON TABLE close_requests
IS 'Explicit requests by a reserve owner to close a reserve immediately';
COMMENT ON COLUMN close_requests.close_timestamp
IS 'When the request was created by the client';
COMMENT ON COLUMN close_requests.reserve_sig
IS 'Signature affirming that the reserve is to be closed';
COMMENT ON COLUMN close_requests.close_val
IS 'Balance of the reserve at the time of closing, to be wired to the associated bank account (minus the closing fee)';
COMMENT ON COLUMN close_requests.payto_uri
IS 'Identifies the credited bank account. Optional.';
SELECT add_constraints_to_close_requests_partition('default');
-- ------------------------------ purse_deposits ----------------------------------------
SELECT create_table_purse_deposits();
COMMENT ON TABLE purse_deposits
IS 'Requests depositing coins into a purse';
COMMENT ON COLUMN purse_deposits.partner_serial_id
IS 'identifies the partner exchange, NULL in case the target purse lives at this exchange';
COMMENT ON COLUMN purse_deposits.purse_pub
IS 'Public key of the purse';
COMMENT ON COLUMN purse_deposits.coin_pub
IS 'Public key of the coin being deposited';
COMMENT ON COLUMN purse_deposits.amount_with_fee_val
IS 'Total amount being deposited';
COMMENT ON COLUMN purse_deposits.coin_sig
IS 'Signature of the coin affirming the deposit into the purse, of type TALER_SIGNATURE_PURSE_DEPOSIT';
SELECT add_constraints_to_purse_deposits_partition('default');
-- ------------------------------ wads_out ----------------------------------------
SELECT create_table_wads_out();
COMMENT ON TABLE wads_out
IS 'Wire transfers made to another exchange to transfer purse funds';
COMMENT ON COLUMN wads_out.wad_id
IS 'Unique identifier of the wad, part of the wire transfer subject';
COMMENT ON COLUMN wads_out.partner_serial_id
IS 'target exchange of the wad';
COMMENT ON COLUMN wads_out.amount_val
IS 'Amount that was wired';
COMMENT ON COLUMN wads_out.execution_time
IS 'Time when the wire transfer was scheduled';
SELECT add_constraints_to_wads_out_partition('default');
-- ------------------------------ wads_out_entries ----------------------------------------
SELECT create_table_wad_out_entries();
COMMENT ON TABLE wad_out_entries
IS 'Purses combined into a wad';
COMMENT ON COLUMN wad_out_entries.wad_out_serial_id
IS 'Wad the purse was part of';
COMMENT ON COLUMN wad_out_entries.reserve_pub
IS 'Target reserve for the purse';
COMMENT ON COLUMN wad_out_entries.purse_pub
IS 'Public key of the purse';
COMMENT ON COLUMN wad_out_entries.h_contract
IS 'Hash of the contract associated with the purse';
COMMENT ON COLUMN wad_out_entries.purse_expiration
IS 'Time when the purse expires';
COMMENT ON COLUMN wad_out_entries.merge_timestamp
IS 'Time when the merge was approved';
COMMENT ON COLUMN wad_out_entries.amount_with_fee_val
IS 'Total amount in the purse';
COMMENT ON COLUMN wad_out_entries.wad_fee_val
IS 'Wat fee charged to the purse';
COMMENT ON COLUMN wad_out_entries.deposit_fees_val
IS 'Total deposit fees charged to the purse';
COMMENT ON COLUMN wad_out_entries.reserve_sig
IS 'Signature by the receiving reserve, of purpose TALER_SIGNATURE_ACCOUNT_MERGE';
COMMENT ON COLUMN wad_out_entries.purse_sig
IS 'Signature by the purse of purpose TALER_SIGNATURE_PURSE_MERGE';
SELECT add_constraints_to_wad_out_entries_partition('default');
-- ------------------------------ wads_in ----------------------------------------
SELECT create_table_wads_in();
COMMENT ON TABLE wads_in
IS 'Incoming exchange-to-exchange wad wire transfers';
COMMENT ON COLUMN wads_in.wad_id
IS 'Unique identifier of the wad, part of the wire transfer subject';
COMMENT ON COLUMN wads_in.origin_exchange_url
IS 'Base URL of the originating URL, also part of the wire transfer subject';
COMMENT ON COLUMN wads_in.amount_val
IS 'Actual amount that was received by our exchange';
COMMENT ON COLUMN wads_in.arrival_time
IS 'Time when the wad was received';
SELECT add_constraints_to_wads_in_partition('default');
-- ------------------------------ wads_in_entries ----------------------------------------
SELECT create_table_wad_in_entries();
COMMENT ON TABLE wad_in_entries
IS 'list of purses aggregated in a wad according to the sending exchange';
COMMENT ON COLUMN wad_in_entries.wad_in_serial_id
IS 'wad for which the given purse was included in the aggregation';
COMMENT ON COLUMN wad_in_entries.reserve_pub
IS 'target account of the purse (must be at the local exchange)';
COMMENT ON COLUMN wad_in_entries.purse_pub
IS 'public key of the purse that was merged';
COMMENT ON COLUMN wad_in_entries.h_contract
IS 'hash of the contract terms of the purse';
COMMENT ON COLUMN wad_in_entries.purse_expiration
IS 'Time when the purse was set to expire';
COMMENT ON COLUMN wad_in_entries.merge_timestamp
IS 'Time when the merge was approved';
COMMENT ON COLUMN wad_in_entries.amount_with_fee_val
IS 'Total amount in the purse';
COMMENT ON COLUMN wad_in_entries.wad_fee_val
IS 'Total wad fees paid by the purse';
COMMENT ON COLUMN wad_in_entries.deposit_fees_val
IS 'Total deposit fees paid when depositing coins into the purse';
COMMENT ON COLUMN wad_in_entries.reserve_sig
IS 'Signature by the receiving reserve, of purpose TALER_SIGNATURE_ACCOUNT_MERGE';
COMMENT ON COLUMN wad_in_entries.purse_sig
IS 'Signature by the purse of purpose TALER_SIGNATURE_PURSE_MERGE';
SELECT add_constraints_to_wad_in_entries_partition('default');