Skip to content

Commit

Permalink
Update address references from tx_out to it's own table
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlrt committed Nov 15, 2024
1 parent c339fac commit 849447f
Show file tree
Hide file tree
Showing 26 changed files with 90 additions and 61 deletions.
2 changes: 1 addition & 1 deletion files/grest/.sqlfluff
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extended_capitalisation_policy = upper
idented_joins = True
indented_using_on = False
tab_space_size = 2
large_file_skip_byte_limit=35000
large_file_skip_byte_limit=50000

[sqlfluff:indentation]
tab_space_size = 2
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/000_utilities/cip129.sql
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ END;
$$;

CREATE OR REPLACE FUNCTION grest.cip129_from_gov_action_id(_proposal_id text)
RETURNS text[]
RETURNS text []
LANGUAGE plpgsql STABLE
AS $$
DECLARE
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/000_utilities/cip5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ BEGIN
RETURN b32_encode('stake', _raw::text);
END IF;
END;
$$;
$$;
2 changes: 1 addition & 1 deletion files/grest/rpc/01_cached_tables/pool_history_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ $$;

COMMENT ON FUNCTION grest.get_pool_history_data_bulk IS 'Pool block production and reward history from a given epoch until optional later epoch, for all or particular subset of pools'; -- noqa: LT01

CREATE OR REPLACE FUNCTION grest.pool_history_cache_update(_epoch_no_to_insert_from bigint DEFAULT NULL)
CREATE OR REPLACE FUNCTION grest.pool_history_cache_update(_epoch_no_to_insert_from bigint DEFAULT null)
RETURNS void
LANGUAGE plpgsql
AS $$
Expand Down
6 changes: 3 additions & 3 deletions files/grest/rpc/02_indexes/13_3_00.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE INDEX IF NOT EXISTS pool_stat_pool_hash_id ON pool_stat(pool_hash_id);
CREATE INDEX IF NOT EXISTS pool_stat_epoch_no ON pool_stat(epoch_no);
CREATE INDEX IF NOT EXISTS pool_stat_pool_hash_id ON pool_stat (pool_hash_id);
CREATE INDEX IF NOT EXISTS pool_stat_epoch_no ON pool_stat (epoch_no);
CREATE INDEX IF NOT EXISTS idx_reward_rest_addr_id ON reward_rest (addr_id);
CREATE INDEX IF NOT EXISTS idx_reward_rest_spendable_epoch ON reward_rest (spendable_epoch);
CREATE INDEX IF NOT EXISTS idx_reward_rest_spendable_epoch ON reward_rest (spendable_epoch);
2 changes: 1 addition & 1 deletion files/grest/rpc/02_indexes/13_5_0_2.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
DROP INDEX IF EXISTS idx_stake_address_view;
CREATE INDEX IF NOT EXISTS idx_stake_address_hash_raw ON stake_address (hash_raw);
CREATE INDEX IF NOT EXISTS idx_drep_hash_raw ON drep_hash (raw);
CREATE INDEX IF NOT EXISTS idx_drep_hash_raw ON drep_hash (raw);
1 change: 1 addition & 0 deletions files/grest/rpc/02_indexes/13_6_0_1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX IF NOT EXISTS idx_address_address ON address USING hash (address);
6 changes: 4 additions & 2 deletions files/grest/rpc/account/account_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ BEGIN
FROM
(
SELECT
txo.address,
a.address,
txo.stake_address_id,
txo.id
FROM tx_out AS txo
LEFT JOIN address AS a ON a.id = txo.address_id
WHERE txo.stake_address_id = ANY(sa_id_list)
AND txo.consumed_by_tx_id IS NULL
) AS x
Expand All @@ -53,10 +54,11 @@ BEGIN
FROM
(
SELECT
txo.address,
a.address,
txo.stake_address_id,
txo.id
FROM tx_out AS txo
LEFT JOIN address AS a ON a.id = txo.address_id
WHERE txo.stake_address_id = ANY(sa_id_list)
LIMIT (CASE WHEN _first_only IS TRUE THEN 1 ELSE NULL END)
) AS x
Expand Down
5 changes: 3 additions & 2 deletions files/grest/rpc/account/account_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ BEGIN
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
tx_out.address::text,
a.address::text,
tx_out.value::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw) as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
ENCODE(a.payment_cred, 'hex') AS payment_cred,
b.epoch_no,
b.block_no,
EXTRACT(EPOCH FROM b.time)::integer AS block_time,
Expand Down Expand Up @@ -89,6 +89,7 @@ BEGIN
END) AS is_spent
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN block AS b ON b.id = tx.block_id
LEFT JOIN datum ON datum.id = tx_out.inline_datum_id
Expand Down
7 changes: 4 additions & 3 deletions files/grest/rpc/address/address_assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BEGIN

WITH _all_assets AS (
SELECT
txo.address,
a.address,
ma.policy,
ma.name,
ma.fingerprint,
Expand All @@ -24,10 +24,11 @@ BEGIN
INNER JOIN multi_asset AS ma ON ma.id = mtx.ident
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
INNER JOIN tx_out AS txo ON txo.id = mtx.tx_out_id
WHERE txo.address = ANY(_addresses)
INNER JOIN address AS a ON a.id = tx_out.address_id
WHERE a.address = ANY(_addresses)
AND txo.consumed_by_tx_id IS NULL
GROUP BY
txo.address, ma.policy, ma.name, ma.fingerprint, aic.decimals
a.address, ma.policy, ma.name, ma.fingerprint, aic.decimals
)

SELECT
Expand Down
12 changes: 7 additions & 5 deletions files/grest/rpc/address/address_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,32 @@ DECLARE
BEGIN
CREATE TEMPORARY TABLE _known_addresses AS
SELECT
DISTINCT ON (tx_out.address) tx_out.address,
DISTINCT ON (tx_out.address_id) a.address,
sa.hash_raw AS stake_address_raw,
COALESCE(tx_out.address_has_script, 'false') AS script_address
COALESCE(a.has_script, 'false') AS script_address
FROM tx_out
INNER JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON sa.id = tx_out.stake_address_id
WHERE tx_out.address = ANY(_addresses);
WHERE a.address = ANY(_addresses);

RETURN QUERY
WITH _all_utxos AS (
SELECT
tx.id,
tx.hash,
tx_out.id AS txo_id,
tx_out.address,
a.address,
tx_out.value,
tx_out.index,
tx.block_id,
tx_out.data_hash,
tx_out.inline_datum_id,
tx_out.reference_script_id
FROM tx_out
INNER JOIN address AS a ON a.id = tx_out.address_id
INNER JOIN tx ON tx.id = tx_out.tx_id
WHERE tx_out.consumed_by_tx_id IS NULL
AND tx_out.address = ANY(_addresses)
AND a.address = ANY(_addresses)
)

SELECT
Expand Down
3 changes: 2 additions & 1 deletion files/grest/rpc/address/address_txs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ BEGIN
--
SELECT consumed_by_tx_id
FROM tx_out
INNER JOIN address AS a ON a.id = tx_out.address_id
WHERE tx_out.consumed_by_tx_id IS NOT NULL
AND tx_out.address = ANY(_addresses)
AND a.address = ANY(_addresses)
AND tx_out.consumed_by_tx_id >= _tx_id_min
) AS tmp;

Expand Down
10 changes: 6 additions & 4 deletions files/grest/rpc/address/address_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@ BEGIN
)
END) as assets
FROM tx_out AS txo
INNER JOIN address AS a ON a.id = txo.address_id
INNER JOIN ma_tx_out AS mto ON mto.tx_out_id = txo.id
LEFT JOIN multi_asset AS ma ON ma.id = mto.ident
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
WHERE txo.address = ANY(_addresses)
WHERE a.address = ANY(_addresses)
AND txo.consumed_by_tx_id IS NULL
GROUP BY txo.id
)
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
tx_out.address::text,
a.address::text,
tx_out.value::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::text as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
ENCODE(a.payment_cred, 'hex') AS payment_cred,
b.epoch_no,
b.block_no,
EXTRACT(EPOCH FROM b.time)::integer AS block_time,
Expand Down Expand Up @@ -79,13 +80,14 @@ BEGIN
ELSE true
END) AS is_spent
FROM tx_out
INNER JOIN address AS a ON a.id = tx_out.address_id
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN block AS b ON b.id = tx.block_id
LEFT JOIN datum ON datum.id = tx_out.inline_datum_id
LEFT JOIN script ON script.id = tx_out.reference_script_id
LEFT JOIN _assets ON tx_out.id = _assets.id
WHERE tx_out.address = ANY(_addresses)
WHERE a.address = ANY(_addresses)
AND tx_out.consumed_by_tx_id IS NULL
;
END;
Expand Down
6 changes: 4 additions & 2 deletions files/grest/rpc/address/credential_txs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ BEGIN
FROM (
SELECT tx_id
FROM tx_out
WHERE payment_cred = ANY(_payment_cred_bytea)
INNER JOIN address AS a ON tx_out.address_id = a.id
WHERE a.payment_cred = ANY(_payment_cred_bytea)
AND tx_id >= _tx_id_min
--
UNION
--
SELECT consumed_by_tx_id AS tx_id
FROM tx_out
INNER JOIN address AS a ON tx_out.address_id = a.id
WHERE tx_out.consumed_by_tx_id IS NOT NULL
AND tx_out.payment_cred = ANY(_payment_cred_bytea)
AND a.payment_cred = ANY(_payment_cred_bytea)
AND tx_out.consumed_by_tx_id >= _tx_id_min
) AS tmp;

Expand Down
10 changes: 6 additions & 4 deletions files/grest/rpc/address/credential_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,21 @@ BEGIN
)
END) as assets
FROM tx_out AS txo
INNER JOIN address ON a.id = txo.address_id
INNER JOIN ma_tx_out AS mto ON mto.tx_out_id = txo.id
LEFT JOIN multi_asset AS ma ON ma.id = mto.ident
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
WHERE txo.payment_cred = ANY(_payment_cred_bytea)
WHERE a.payment_cred = ANY(_payment_cred_bytea)
AND txo.consumed_by_tx_id IS NULL
GROUP BY txo.id
)
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
tx_out.address::text,
a.address::text,
tx_out.value::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::text as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
ENCODE(a.payment_cred, 'hex') AS payment_cred,
b.epoch_no,
b.block_no,
EXTRACT(EPOCH FROM b.time)::integer AS block_time,
Expand Down Expand Up @@ -85,13 +86,14 @@ BEGIN
ELSE true
END) AS is_spent
FROM tx_out
INNER JOIN address ON a.id = tx_out.address_id
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN block AS b ON b.id = tx.block_id
LEFT JOIN datum ON datum.id = tx_out.inline_datum_id
LEFT JOIN script ON script.id = tx_out.reference_script_id
LEFT JOIN _assets ON tx_out.id = _assets.id
WHERE tx_out.payment_cred = ANY(_payment_cred_bytea)
WHERE a.payment_cred = ANY(_payment_cred_bytea)
AND tx_out.consumed_by_tx_id IS NULL
;
END;
Expand Down
6 changes: 4 additions & 2 deletions files/grest/rpc/assets/asset_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ BEGIN
FROM
(
SELECT
txo.address,
a.address,
sa.hash_raw AS stake_address_raw,
atoc.quantity
FROM grest.asset_tx_out_cache AS atoc
LEFT JOIN tx_out AS txo ON atoc.txo_id = txo.id
LEFT JOIN address AS a ON a.id = txo.address_id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE atoc.ma_id = _asset_id
AND txo.consumed_by_tx_id IS NULL
Expand All @@ -52,11 +53,12 @@ BEGIN
FROM
(
SELECT
txo.address,
a.address,
sa.hash_raw AS stake_address_raw,
mto.quantity
FROM ma_tx_out AS mto
LEFT JOIN tx_out AS txo ON txo.id = mto.tx_out_id
LEFT JOIN address AS a ON a.id = txo.address_id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE mto.ident = _asset_id
AND txo.consumed_by_tx_id IS NULL
Expand Down
6 changes: 4 additions & 2 deletions files/grest/rpc/assets/asset_nft_address.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ BEGIN
IF EXISTS (SELECT * FROM ma_tx_mint WHERE ident = _asset_id and quantity < 0 LIMIT 1) THEN
RETURN QUERY
SELECT
txo.address,
a.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address
FROM tx_out AS txo
LEFT JOIN address AS a ON a.id = txo.address_id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE txo.id = (
SELECT MAX(tx_out_id)
Expand All @@ -39,9 +40,10 @@ BEGIN
ELSE
RETURN QUERY
SELECT
txo.address,
a.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address
FROM tx_out AS txo
LEFT JOIN address AS a ON a.id = txo.address_id
INNER JOIN ma_tx_out mto ON mto.tx_out_id = txo.id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE mto.ident = _asset_id
Expand Down
3 changes: 2 additions & 1 deletion files/grest/rpc/assets/asset_summary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ BEGIN
txo.tx_id AS tx_id,
txo.id AS tx_out_id,
txo.index AS tx_out_idx,
txo.address AS address,
a.address AS address,
txo.stake_address_id AS sa_id
FROM ma_tx_out AS mto
INNER JOIN tx_out AS txo ON txo.id = mto.tx_out_id
LEFT JOIN address AS a ON a.id = txo.address_id
WHERE mto.ident = _asset_id
AND txo.consumed_by_tx_id IS NULL)

Expand Down
5 changes: 3 additions & 2 deletions files/grest/rpc/assets/asset_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ BEGIN
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
tx_out.address::text,
a.address::text,
tx_out.value::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::text as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
ENCODE(a.payment_cred, 'hex') AS payment_cred,
b.epoch_no,
b.block_no,
EXTRACT(EPOCH FROM b.time)::integer AS block_time,
Expand Down Expand Up @@ -99,6 +99,7 @@ BEGIN
END) AS is_spent
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN address AS a ON a.id = tx_out.address_id
INNER JOIN _assets ON tx_out.id = _assets.id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN block AS b ON b.id = tx.block_id
Expand Down
8 changes: 5 additions & 3 deletions files/grest/rpc/assets/policy_asset_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ BEGIN
(
SELECT
atoc.ma_id,
txo.address,
a.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address,
atoc.quantity
FROM grest.asset_tx_out_cache AS atoc
LEFT JOIN multi_asset AS ma ON ma.id = atoc.ma_id
LEFT JOIN tx_out AS txo ON txo.id = atoc.txo_id
LEFT JOIN address AS a ON a.id = txo.address_id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE ma.policy = DECODE(_asset_policy, 'hex')
AND txo.consumed_by_tx_id IS NULL
Expand All @@ -46,18 +47,19 @@ BEGIN
RETURN QUERY
SELECT
ENCODE(ma.name, 'hex') AS asset_name,
txo.address,
a.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address,
SUM(mto.quantity)::text
FROM multi_asset AS ma
LEFT JOIN ma_tx_out AS mto ON mto.ident = ma.id
LEFT JOIN tx_out AS txo ON txo.id = mto.tx_out_id
LEFT JOIN address AS a ON a.id = txo.address_id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE ma.policy = DECODE(_asset_policy, 'hex')
AND txo.consumed_by_tx_id IS NULL
GROUP BY
ma.name,
txo.address,
a.address,
sa.hash_raw;
END IF;
END;
Expand Down
1 change: 0 additions & 1 deletion files/grest/rpc/governance/proposal_voting_summary.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

CREATE OR REPLACE FUNCTION grest.proposal_voting_summary(_proposal_id text)
RETURNS TABLE (
proposal_type text,
Expand Down
Loading

0 comments on commit 849447f

Please sign in to comment.