summaryrefslogtreecommitdiff
path: root/src/exchangedb/0002-known_coins.sql
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-11-27 14:05:47 +0100
committerChristian Grothoff <christian@grothoff.org>2022-11-27 14:05:47 +0100
commita322770d290cae69e7d2f7629ee575e068254428 (patch)
tree75a80ac74d165fa0dd00df6095ad0c482d706da5 /src/exchangedb/0002-known_coins.sql
parentbe2c11a1797d8d16b86439a80a4f110f82bb5829 (diff)
downloadexchange-a322770d290cae69e7d2f7629ee575e068254428.tar.gz
exchange-a322770d290cae69e7d2f7629ee575e068254428.tar.bz2
exchange-a322770d290cae69e7d2f7629ee575e068254428.zip
more work on SQL refactoring
Diffstat (limited to 'src/exchangedb/0002-known_coins.sql')
-rw-r--r--src/exchangedb/0002-known_coins.sql87
1 files changed, 74 insertions, 13 deletions
diff --git a/src/exchangedb/0002-known_coins.sql b/src/exchangedb/0002-known_coins.sql
index af2610c66..a45c7bc85 100644
--- a/src/exchangedb/0002-known_coins.sql
+++ b/src/exchangedb/0002-known_coins.sql
@@ -15,7 +15,7 @@
--
-CREATE OR REPLACE FUNCTION create_table_known_coins(
+CREATE FUNCTION create_table_known_coins(
IN shard_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
@@ -24,11 +24,10 @@ AS $$
DECLARE
table_name VARCHAR default 'known_coins';
BEGIN
-
PERFORM create_partitioned_table(
- 'CREATE TABLE IF NOT EXISTS %I'
- '(known_coin_id BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE'
- ',denominations_serial INT8 NOT NULL' -- REFERENCES denominations (denominations_serial) ON DELETE CASCADE'
+ 'CREATE TABLE %I'
+ '(known_coin_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
+ ',denominations_serial INT8 NOT NULL'
',coin_pub BYTEA NOT NULL PRIMARY KEY CHECK (LENGTH(coin_pub)=32)'
',age_commitment_hash BYTEA CHECK (LENGTH(age_commitment_hash)=32)'
',denom_sig BYTEA NOT NULL'
@@ -36,26 +35,78 @@ BEGIN
',remaining_frac INT4 NOT NULL DEFAULT(0)'
') %s ;'
,table_name
- ,'PARTITION BY HASH (coin_pub)' -- FIXME: or include denominations_serial? or multi-level partitioning?;
+ ,'PARTITION BY HASH (coin_pub)'
+ ,shard_suffix
+ );
+ PERFORM comment_partitioned_table(
+ 'information about coins and their signatures, so we do not have to store the signatures more than once if a coin is involved in multiple operations'
+ ,table_name
+ ,shard_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Denomination of the coin, determines the value of the original coin and applicable fees for coin-specific operations.'
+ ,'denominations_serial'
+ ,table_name
+ ,shard_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'EdDSA public key of the coin'
+ ,'coin_pub'
+ ,table_name
+ ,shard_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Value of the coin that remains to be spent'
+ ,'remaining_val'
+ ,table_name
+ ,shard_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Optional hash of the age commitment for age restrictions as per DD 24 (active if denom_type has the respective bit set)'
+ ,'age_commitment_hash'
+ ,table_name
+ ,shard_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'This is the signature of the exchange that affirms that the coin is a valid coin. The specific signature type depends on denom_type of the denomination.'
+ ,'denom_sig'
+ ,table_name
,shard_suffix
);
-
- table_name = concat_ws('_', table_name, shard_suffix);
-
END
$$;
-CREATE OR REPLACE FUNCTION add_constraints_to_known_coins_partition(
+
+CREATE FUNCTION constrain_table_known_coins(
IN partition_suffix VARCHAR
)
RETURNS void
LANGUAGE plpgsql
AS $$
+DECLARE
+ table_name VARCHAR default 'known_coins';
+BEGIN
+ table_name = concat_ws('_', table_name, shard_suffix);
+ EXECUTE FORMAT (
+ 'ALTER TABLE ' || table_name ||
+ ' ADD CONSTRAINT ' || table_name || '_known_coin_id_key'
+ ' UNIQUE (known_coin_id)'
+ );
+END
+$$;
+
+
+CREATE FUNCTION foreign_table_known_coins()
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+DECLARE
+ table_name VARCHAR default 'known_coins';
BEGIN
EXECUTE FORMAT (
- 'ALTER TABLE known_coins_' || partition_suffix || ' '
- 'ADD CONSTRAINT known_coins_' || partition_suffix || '_known_coin_id_key '
- 'UNIQUE (known_coin_id)'
+ 'ALTER TABLE ' || table_name ||
+ ' ADD CONSTRAINT ' || table_name || '_foreign_denominations'
+ ' REFERENCES denominations (denominations_serial) ON DELETE CASCADE'
);
END
$$;
@@ -72,4 +123,14 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
+ ,FALSE),
+ ('known_coins'
+ ,'exchange-0002'
+ ,'constrain'
+ ,TRUE
+ ,FALSE),
+ ('known_coins'
+ ,'exchange-0002'
+ ,'foreign'
+ ,TRUE
,FALSE);