From 3d49a57429749414516961b1b52b058c14848b1c Mon Sep 17 00:00:00 2001 From: rdlrt <3169068+rdlrt@users.noreply.github.com> Date: Sun, 15 Sep 2024 11:48:05 +1000 Subject: [PATCH] Various SQL Fixes (WIP) --- .../01_cached_tables/pool_history_cache.sql | 6 +-- .../rpc/01_cached_tables/pool_info_cache.sql | 2 +- files/grest/rpc/03_utilities/cip5.sql | 8 ++-- files/grest/rpc/account/account_addresses.sql | 2 +- files/grest/rpc/account/account_assets.sql | 2 +- files/grest/rpc/account/account_history.sql | 2 +- files/grest/rpc/account/account_info.sql | 2 +- .../grest/rpc/account/account_info_cached.sql | 2 +- files/grest/rpc/account/account_rewards.sql | 6 +-- files/grest/rpc/account/account_updates.sql | 2 +- files/grest/rpc/account/account_utxos.sql | 12 +++-- files/grest/rpc/blocks/block_info.sql | 2 +- files/grest/rpc/blocks/blocks.sql | 2 +- files/grest/rpc/governance/proposal_votes.sql | 2 +- .../governance/proposal_voting_summary.sql | 2 +- files/grest/rpc/pool/pool_delegators.sql | 8 ++-- .../rpc/pool/pool_delegators_history.sql | 44 +++++++------------ files/grest/rpc/pool/pool_info.sql | 8 ++-- files/grest/rpc/pool/pool_list.sql | 6 +-- files/grest/rpc/pool/pool_metadata.sql | 4 +- files/grest/rpc/pool/pool_relays.sql | 2 +- files/grest/rpc/pool/pool_updates.sql | 4 +- 22 files changed, 58 insertions(+), 72 deletions(-) diff --git a/files/grest/rpc/01_cached_tables/pool_history_cache.sql b/files/grest/rpc/01_cached_tables/pool_history_cache.sql index a167da81..9e32ae66 100644 --- a/files/grest/rpc/01_cached_tables/pool_history_cache.sql +++ b/files/grest/rpc/01_cached_tables/pool_history_cache.sql @@ -42,8 +42,8 @@ DECLARE _pool_ids bigint []; BEGIN _pool_ids := (SELECT ARRAY_AGG(id) from pool_hash ph where ph.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(pool),'hex')) - FROM UNNEST(_pool_bech32) AS pool + SELECT DECODE(b32_decode(pool),'hex') + FROM UNNEST(_pool_bech32) AS pool) ); RETURN QUERY @@ -144,7 +144,7 @@ BEGIN ) SELECT - actf.pool_id::text, + actf.pool_id::bigint, actf.epoch_no::bigint, actf.active_stake::lovelace, actf.active_stake_pct, diff --git a/files/grest/rpc/01_cached_tables/pool_info_cache.sql b/files/grest/rpc/01_cached_tables/pool_info_cache.sql index e9c9aa81..59083ac6 100644 --- a/files/grest/rpc/01_cached_tables/pool_info_cache.sql +++ b/files/grest/rpc/01_cached_tables/pool_info_cache.sql @@ -340,8 +340,8 @@ BEGIN ); END LOOP; + CREATE INDEX IF NOT EXISTS idx_id ON grest.pool_info_cache (tx_id); CREATE INDEX IF NOT EXISTS idx_tx_id ON grest.pool_info_cache (tx_id); - CREATE INDEX IF NOT EXISTS idx_pool_id_bech32 ON grest.pool_info_cache (pool_id_bech32); CREATE INDEX IF NOT EXISTS idx_pool_hash_id ON grest.pool_info_cache (pool_hash_id); CREATE INDEX IF NOT EXISTS idx_pool_status ON grest.pool_info_cache (pool_status); CREATE INDEX IF NOT EXISTS idx_meta_id ON grest.pool_info_cache (meta_id); diff --git a/files/grest/rpc/03_utilities/cip5.sql b/files/grest/rpc/03_utilities/cip5.sql index edd91e50..9d3dc5fe 100644 --- a/files/grest/rpc/03_utilities/cip5.sql +++ b/files/grest/rpc/03_utilities/cip5.sql @@ -7,11 +7,13 @@ RETURNS text LANGUAGE plpgsql STABLE AS $$ BEGIN - IF _raw IS NULL THEN RETURN NULL END IF; + IF _raw IS NULL THEN + RETURN NULL; + END IF; IF SUBSTRING(ENCODE(_raw, 'hex') from 2 for 1) = '0' THEN - RETURN b32_encode('stake_test', hex::text); + RETURN b32_encode('stake_test', _raw::text); ELSE - RETURN b32_encode('stake', hex::text); + RETURN b32_encode('stake', _raw::text); END IF; END; $$; \ No newline at end of file diff --git a/files/grest/rpc/account/account_addresses.sql b/files/grest/rpc/account/account_addresses.sql index dea09d92..649e2c3f 100644 --- a/files/grest/rpc/account/account_addresses.sql +++ b/files/grest/rpc/account/account_addresses.sql @@ -14,7 +14,7 @@ BEGIN stake_address WHERE stake_address.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex')) + SELECT DECODE(b32_decode(n), 'hex') FROM UNNEST(_stake_addresses) AS n ); diff --git a/files/grest/rpc/account/account_assets.sql b/files/grest/rpc/account/account_assets.sql index 8521ea8e..0fc4d5dd 100644 --- a/files/grest/rpc/account/account_assets.sql +++ b/files/grest/rpc/account/account_assets.sql @@ -26,7 +26,7 @@ BEGIN INNER JOIN tx_out AS txo ON txo.id = mtx.tx_out_id INNER JOIN stake_address AS sa ON sa.id = txo.stake_address_id WHERE sa.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex')) + SELECT DECODE(b32_decode(n), 'hex') FROM UNNEST(_stake_addresses) AS n ) AND txo.consumed_by_tx_id IS NULL diff --git a/files/grest/rpc/account/account_history.sql b/files/grest/rpc/account/account_history.sql index 498ed977..f6bf783b 100644 --- a/files/grest/rpc/account/account_history.sql +++ b/files/grest/rpc/account/account_history.sql @@ -14,7 +14,7 @@ BEGIN stake_address WHERE stake_address.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex')) + SELECT DECODE(b32_decode(n), 'hex') FROM UNNEST(_stake_addresses) AS n ); diff --git a/files/grest/rpc/account/account_info.sql b/files/grest/rpc/account/account_info.sql index c41ad4bf..7f7ee5d9 100644 --- a/files/grest/rpc/account/account_info.sql +++ b/files/grest/rpc/account/account_info.sql @@ -22,7 +22,7 @@ BEGIN array_agg(id) FROM stake_address WHERE stake_address.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex')) + SELECT DECODE(b32_decode(n), 'hex') FROM UNNEST(_stake_addresses) AS n ); diff --git a/files/grest/rpc/account/account_info_cached.sql b/files/grest/rpc/account/account_info_cached.sql index e2b23761..17168bde 100644 --- a/files/grest/rpc/account/account_info_cached.sql +++ b/files/grest/rpc/account/account_info_cached.sql @@ -24,7 +24,7 @@ BEGIN stake_address WHERE stake_address.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex')) + SELECT DECODE(b32_decode(n), 'hex') FROM UNNEST(_stake_addresses) AS n ); diff --git a/files/grest/rpc/account/account_rewards.sql b/files/grest/rpc/account/account_rewards.sql index 12a9a998..ed63febc 100644 --- a/files/grest/rpc/account/account_rewards.sql +++ b/files/grest/rpc/account/account_rewards.sql @@ -14,7 +14,7 @@ BEGIN stake_address WHERE stake_address.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex')) + SELECT DECODE(b32_decode(n), 'hex') FROM UNNEST(_stake_addresses) AS n ); @@ -28,7 +28,7 @@ BEGIN 'spendable_epoch', r.spendable_epoch, 'amount', r.amount::text, 'type', r.type, - 'pool_id', b32_encode('pool', DECODE(ph.hash_raw,'hex')::text) + 'pool_id', b32_encode('pool', ph.hash_raw::text) ) ) AS rewards FROM @@ -48,7 +48,7 @@ BEGIN 'spendable_epoch', r.spendable_epoch, 'amount', r.amount::text, 'type', r.type, - 'pool_id', b32_encode('pool', DECODE(ph.hash_raw,'hex')::text) + 'pool_id', b32_encode('pool', ph.hash_raw::text) ) ) AS rewards FROM diff --git a/files/grest/rpc/account/account_updates.sql b/files/grest/rpc/account/account_updates.sql index 24fcd989..a097c8a5 100644 --- a/files/grest/rpc/account/account_updates.sql +++ b/files/grest/rpc/account/account_updates.sql @@ -14,7 +14,7 @@ BEGIN stake_address WHERE stake_address.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex')) + SELECT DECODE(b32_decode(n), 'hex') FROM UNNEST(_stake_addresses) AS n ); diff --git a/files/grest/rpc/account/account_utxos.sql b/files/grest/rpc/account/account_utxos.sql index 455f7f32..e86f39f0 100644 --- a/files/grest/rpc/account/account_utxos.sql +++ b/files/grest/rpc/account/account_utxos.sql @@ -23,11 +23,9 @@ DECLARE BEGIN SELECT INTO sa_id_list ARRAY_AGG(stake_address.id) - FROM - stake_address - WHERE - stake_address.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex')) + FROM stake_address + WHERE stake_address.hash_raw = ANY( + SELECT DECODE(b32_decode(n), 'hex') FROM UNNEST(_stake_addresses) AS n ); @@ -49,7 +47,7 @@ BEGIN 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.stake_address_id ANY(sa_id_list) + WHERE txo.stake_address_id = ANY(sa_id_list) AND txo.consumed_by_tx_id IS NULL GROUP BY txo.id ) @@ -96,7 +94,7 @@ BEGIN 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.stake_address_id ANY(sa_id_list) + WHERE tx_out.stake_address_id = ANY(sa_id_list) AND tx_out.consumed_by_tx_id IS NULL ; END; diff --git a/files/grest/rpc/blocks/block_info.sql b/files/grest/rpc/blocks/block_info.sql index 496fcbf8..7a231d15 100644 --- a/files/grest/rpc/blocks/block_info.sql +++ b/files/grest/rpc/blocks/block_info.sql @@ -59,7 +59,7 @@ BEGIN b.vrf_key, ENCODE(b.op_cert::bytea, 'hex') AS op_cert, b.op_cert_counter, - b32_encode('pool', DECODE(ph.hash_raw,'hex')::text) AS pool, + b32_encode('pool', ph.hash_raw::text) AS pool, b.proto_major, b.proto_minor, block_data.total_output::text, diff --git a/files/grest/rpc/blocks/blocks.sql b/files/grest/rpc/blocks/blocks.sql index 12696b73..bfec8426 100644 --- a/files/grest/rpc/blocks/blocks.sql +++ b/files/grest/rpc/blocks/blocks.sql @@ -27,7 +27,7 @@ AS $$ EXTRACT(EPOCH FROM b.time)::integer AS block_time, b.tx_count, b.vrf_key, - b32_encode('pool', DECODE(ph.hash_raw,'hex')::text) AS pool, + b32_encode('pool', ph.hash_raw::text) AS pool, b.proto_major, b.proto_minor, b.op_cert_counter, diff --git a/files/grest/rpc/governance/proposal_votes.sql b/files/grest/rpc/governance/proposal_votes.sql index 472c54ce..a5025b45 100644 --- a/files/grest/rpc/governance/proposal_votes.sql +++ b/files/grest/rpc/governance/proposal_votes.sql @@ -25,7 +25,7 @@ BEGIN vp.voter_role::text, CASE WHEN dh.raw IS NOT NULL THEN grest.cip129_hex_to_drep_id(dh.raw, dh.has_script) - WHEN ph.id IS NOT NULL THEN b32_encode('pool', DECODE(ph.hash_raw,'hex')::text) + WHEN ph.id IS NOT NULL THEN b32_encode('pool', ph.hash_raw::text) WHEN ch.raw IS NOT NULL THEN grest.cip129_hex_to_cc_hot(ch.raw, ch.has_script) ELSE '' -- shouldn't happen diff --git a/files/grest/rpc/governance/proposal_voting_summary.sql b/files/grest/rpc/governance/proposal_voting_summary.sql index b2f396a1..fe93cbc8 100644 --- a/files/grest/rpc/governance/proposal_voting_summary.sql +++ b/files/grest/rpc/governance/proposal_voting_summary.sql @@ -275,7 +275,7 @@ BEGIN FROM combined_data AS c1 ) AS y ORDER BY 1 DESC - ); + ); END; $$; diff --git a/files/grest/rpc/pool/pool_delegators.sql b/files/grest/rpc/pool/pool_delegators.sql index d77c82da..0dd77e06 100644 --- a/files/grest/rpc/pool/pool_delegators.sql +++ b/files/grest/rpc/pool/pool_delegators.sql @@ -25,7 +25,7 @@ BEGIN ) AS total_balance FROM grest.stake_distribution_cache AS sdc INNER JOIN public.stake_address AS sa ON sa.id = sdc.stake_address_id - WHERE sdc.pool_id = _pool_bech32 + WHERE sdc.pool_id = (SELECT id FROM pool_hash WHERE view = _pool_bech32) UNION ALL @@ -52,7 +52,7 @@ BEGIN ) SELECT DISTINCT ON (ad.stake_address_raw) - grest.cip5_hex_to_stake_addr(ad.stake_address_raw), + grest.cip5_hex_to_stake_addr(ad.stake_address_raw)::varchar, ad.total_balance::text, d.active_epoch_no, ENCODE(tx.hash, 'hex') @@ -97,7 +97,7 @@ BEGIN ) AS total_balance FROM grest.stake_distribution_cache AS sdc INNER JOIN public.stake_address AS sa ON sa.id = sdc.stake_address_id - WHERE sdc.pool_id = _pool_bech32 + WHERE sdc.pool_id = _pool_id UNION ALL @@ -123,7 +123,7 @@ BEGIN ) SELECT - grest.cip5_hex_to_stake_addr(ad.stake_address_raw), + grest.cip5_hex_to_stake_addr(ad.stake_address_raw)::varchar, ad.total_balance::text FROM _all_delegations AS ad; diff --git a/files/grest/rpc/pool/pool_delegators_history.sql b/files/grest/rpc/pool/pool_delegators_history.sql index 9d93de7b..9c51edc3 100644 --- a/files/grest/rpc/pool/pool_delegators_history.sql +++ b/files/grest/rpc/pool/pool_delegators_history.sql @@ -11,35 +11,21 @@ DECLARE _pool_id bigint; BEGIN SELECT id INTO _pool_id FROM pool_hash WHERE pool_hash.hash_raw = DECODE(b32_decode(_pool_bech32),'hex'); - IF _epoch_no IS NULL THEN - RETURN QUERY - SELECT - grest.cip5_hex_to_stake_addr(sa.hash_raw), - es.amount::text, - es.epoch_no - FROM - public.epoch_stake AS es - INNER JOIN public.stake_address AS sa ON es.addr_id = sa.id - WHERE - es.pool_id = _pool_id - ORDER BY - es.epoch_no DESC, es.amount DESC; - ELSE - RETURN QUERY - SELECT - grest.cip5_hex_to_stake_addr(sa.hash_raw), - es.amount::text, - es.epoch_no - FROM - public.epoch_stake AS es - INNER JOIN public.stake_address AS sa ON es.addr_id = sa.id - WHERE - es.pool_id = _pool_id - AND - es.epoch_no = _epoch_no - ORDER BY - es.amount DESC; - END IF; + RETURN QUERY + SELECT + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar, + es.amount::text, + es.epoch_no + FROM public.epoch_stake AS es + INNER JOIN public.stake_address AS sa ON es.addr_id = sa.id + WHERE es.pool_id = _pool_id + AND ( + CASE + WHEN _epoch_no IS NULL THEN TRUE + ELSE es.epoch_no = _epoch_no + END + ) + ORDER BY es.epoch_no DESC; END; $$; diff --git a/files/grest/rpc/pool/pool_info.sql b/files/grest/rpc/pool/pool_info.sql index 16cb1786..8a80a782 100644 --- a/files/grest/rpc/pool/pool_info.sql +++ b/files/grest/rpc/pool/pool_info.sql @@ -54,14 +54,14 @@ BEGIN FROM grest.pool_info_cache AS pic INNER JOIN public.pool_hash AS ph ON ph.id = pic.pool_hash_id WHERE ph.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(p),'hex')) + SELECT DECODE(b32_decode(p),'hex') FROM UNNEST(_pool_bech32_ids) AS p) ORDER BY pic.pool_hash_id, pic.tx_id DESC ) SELECT - api.pool_id_bech32, + api.pool_id_bech32::varchar, ENCODE(ph.hash_raw::bytea, 'hex') AS pool_id_hex, pu.active_epoch_no, ENCODE(pu.vrf_key_hash, 'hex') AS vrf_key_hash, @@ -69,9 +69,9 @@ BEGIN pu.fixed_cost::text, pu.pledge::text, pu.deposit::text, - grest.cip5_hex_to_stake_addr(sa.hash_raw) AS reward_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS reward_addr, ARRAY( - SELECT grest.cip5_hex_to_stake_addr(sa.hash_raw) + SELECT grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar FROM public.pool_owner AS po INNER JOIN public.stake_address AS sa ON sa.id = po.addr_id WHERE po.pool_update_id = api.update_id diff --git a/files/grest/rpc/pool/pool_list.sql b/files/grest/rpc/pool/pool_list.sql index e6476e40..c7f4d7b0 100644 --- a/files/grest/rpc/pool/pool_list.sql +++ b/files/grest/rpc/pool/pool_list.sql @@ -19,16 +19,16 @@ RETURNS TABLE ( LANGUAGE sql STABLE AS $$ SELECT DISTINCT ON (pic.pool_hash_id) - b32_encode('pool', ph.hash_raw::text) AS pool_id_bech32, + b32_encode('pool', ph.hash_raw::text)::varchar AS pool_id_bech32, ENCODE(ph.hash_raw,'hex') as pool_id_hex, pu.active_epoch_no, pu.margin, pu.fixed_cost::text, pu.pledge::text, pu.deposit::text, - grest.cip5_hex_to_stake_addr(sa.hash_raw) AS reward_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS reward_addr, ARRAY( - SELECT grest.cip5_hex_to_stake_addr(sa.hash_raw) + SELECT grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar FROM public.pool_owner AS po INNER JOIN public.stake_address AS sa ON sa.id = po.addr_id WHERE po.pool_update_id = pic.update_id diff --git a/files/grest/rpc/pool/pool_metadata.sql b/files/grest/rpc/pool/pool_metadata.sql index a074f299..4b2df266 100644 --- a/files/grest/rpc/pool/pool_metadata.sql +++ b/files/grest/rpc/pool/pool_metadata.sql @@ -11,7 +11,7 @@ AS $$ BEGIN RETURN QUERY SELECT DISTINCT ON (ph.id) - b32_encode('pool', ph.hash_raw::text) AS pool_id_bech32, + b32_encode('pool', ph.hash_raw::text)::varchar AS pool_id_bech32, pmr.url AS meta_url, ENCODE(pmr.hash, 'hex') AS meta_hash, ocpd.json AS meta_json @@ -22,7 +22,7 @@ BEGIN CASE WHEN _pool_bech32_ids IS NULL THEN TRUE WHEN _pool_bech32_ids IS NOT NULL THEN ph.hash_raw = ANY( - SELECT ARRAY_AGG(DECODE(b32_decode(p),'hex')) + SELECT DECODE(b32_decode(p),'hex') FROM UNNEST(_pool_bech32_ids) AS p) END ORDER BY diff --git a/files/grest/rpc/pool/pool_relays.sql b/files/grest/rpc/pool/pool_relays.sql index cb365e30..0aa2f159 100644 --- a/files/grest/rpc/pool/pool_relays.sql +++ b/files/grest/rpc/pool/pool_relays.sql @@ -6,7 +6,7 @@ RETURNS TABLE ( LANGUAGE sql STABLE AS $$ SELECT DISTINCT ON (ph.id) - b32_encode('pool', ph.hash_raw::text) AS pool_id_bech32, + b32_encode('pool', ph.hash_raw::text)::varchar AS pool_id_bech32, JSONB_AGG(JSONB_BUILD_OBJECT ( 'ipv4', pr.ipv4, 'ipv6', pr.ipv6, diff --git a/files/grest/rpc/pool/pool_updates.sql b/files/grest/rpc/pool/pool_updates.sql index 990f2879..b5643b33 100644 --- a/files/grest/rpc/pool/pool_updates.sql +++ b/files/grest/rpc/pool/pool_updates.sql @@ -30,14 +30,14 @@ BEGIN SELECT ENCODE(tx.hash::bytea, 'hex') AS tx_hash, EXTRACT(EPOCH FROM b.time)::integer AS block_time, - b32_encode('pool', ph.hash_raw::text) AS pool_id_bech32, + b32_encode('pool', ph.hash_raw::text)::varchar AS pool_id_bech32, ENCODE(ph.hash_raw::bytea, 'hex') AS pool_id_hex, pu.active_epoch_no, ENCODE(pu.vrf_key_hash, 'hex') AS vrf_key_hash, pu.margin, pu.fixed_cost::text, pu.pledge::text, - grest.cip5_hex_to_stake_addr(sa.hash_raw) AS reward_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS reward_addr, JSONB_AGG(po.stake_address) AS owners, JSONB_AGG(JSONB_BUILD_OBJECT ( 'ipv4', pr.ipv4,