Skip to content

Commit

Permalink
Replace remaining view references where possible as well as some fixe…
Browse files Browse the repository at this point in the history
…s as found
  • Loading branch information
Scitz0 committed Sep 13, 2024
1 parent 02704f9 commit d707e56
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 71 deletions.
14 changes: 5 additions & 9 deletions files/grest/rpc/01_cached_tables/pool_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ CREATE TABLE grest.pool_info_cache (
fixed_cost lovelace NOT NULL,
pledge lovelace NOT NULL,
deposit lovelace,
reward_addr character varying,
owners character varying [],
reward_addr bigint,
owners bigint [],
relays jsonb [],
meta_id bigint,
meta_url character varying,
Expand Down Expand Up @@ -71,8 +71,6 @@ BEGIN
tx_hash,
block_time,
pool_hash_id,
pool_id_bech32,
pool_id_hex,
active_epoch_no,
vrf_key_hash,
margin,
Expand Down Expand Up @@ -100,11 +98,10 @@ BEGIN
_fixed_cost,
_pledge,
_deposit,
grest.cip5_hex_to_stake_addr(sa.hash_raw),
_reward_addr_id,
ARRAY(
SELECT grest.cip5_hex_to_stake_addr(sa.hash_raw)
SELECT po.addr_id
FROM public.pool_owner AS po
INNER JOIN public.stake_address AS sa ON sa.id = po.addr_id
WHERE po.pool_update_id = _update_id
),
ARRAY(
Expand All @@ -127,7 +124,6 @@ BEGIN
INNER JOIN public.tx ON tx.id = _tx_id
INNER JOIN public.block AS b ON b.id = tx.block_id
LEFT JOIN public.pool_metadata_ref AS pmr ON pmr.id = _meta_id
LEFT JOIN public.stake_address AS sa ON sa.id = _reward_addr_id
WHERE ph.id = _hash_id;
END;
$$;
Expand Down Expand Up @@ -260,7 +256,7 @@ BEGIN
ELSIF (tg_table_name = 'pool_owner') THEN
UPDATE grest.pool_info_cache
SET
owners = owners || (SELECT sa.view FROM public.stake_address AS sa WHERE sa.id = new.addr_id)
owners = owners || (SELECT sa.id FROM public.stake_address AS sa WHERE sa.id = new.addr_id)
WHERE
update_id = new.pool_update_id;
END IF;
Expand Down
19 changes: 9 additions & 10 deletions files/grest/rpc/01_cached_tables/stake_distribution_cache.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS grest.stake_distribution_cache (
stake_address_raw addr29type PRIMARY KEY,
stake_address_id bigint PRIMARY KEY,
pool_id bigint,
total_balance numeric,
utxo numeric,
Expand Down Expand Up @@ -31,7 +31,6 @@ BEGIN
accounts_with_delegated_pools AS (
SELECT DISTINCT ON (stake_address.id)
stake_address.id AS stake_address_id,
stake_address.hash_raw AS stake_address_raw,
pool_hash_id
FROM stake_address
INNER JOIN delegation ON delegation.addr_id = stake_address.id
Expand Down Expand Up @@ -172,7 +171,7 @@ BEGIN
-- INSERT QUERY START
INSERT INTO grest.stake_distribution_cache
SELECT
awdp.stake_address_raw,
awdp.stake_address_id,
pi.pool_id,
COALESCE(aas.amount, 0) + COALESCE(ado.amount, 0) - COALESCE(adi.amount, 0) + COALESCE(adr.rewards, 0) + COALESCE(adir.amount, 0) - COALESCE(adw.withdrawals, 0) AS total_balance,
COALESCE(aas.amount, 0) + COALESCE(ado.amount, 0) - COALESCE(adi.amount, 0) + COALESCE(adr.rewards, 0) + COALESCE(adir.amount, 0) - COALESCE(adw.withdrawals, 0) - COALESCE(atrew.rewards, 0) - COALESCE(atir.amount, 0) + COALESCE(atw.withdrawals, 0) AS utxo,
Expand All @@ -190,7 +189,7 @@ BEGIN
LEFT JOIN account_delta_rewards AS adr ON adr.stake_address_id = awdp.stake_address_id
LEFT JOIN account_delta_instant_rewards AS adir ON adir.stake_address_id = awdp.stake_address_id
LEFT JOIN account_delta_withdrawals AS adw ON adw.stake_address_id = awdp.stake_address_id
ON CONFLICT (stake_address_raw) DO
ON CONFLICT (stake_address_id) DO
UPDATE
SET pool_id = excluded.pool_id,
total_balance = excluded.total_balance,
Expand All @@ -211,9 +210,9 @@ BEGIN

-- Clean up de-registered accounts
DELETE FROM grest.stake_distribution_cache
WHERE stake_address_raw IN (
WHERE stake_address_id IN (
SELECT DISTINCT ON (sa.id)
sa.hash_raw
sa.id
FROM stake_address AS sa
INNER JOIN stake_deregistration AS sd ON sa.id = sd.addr_id
WHERE NOT EXISTS (
Expand All @@ -227,15 +226,15 @@ BEGIN
-- Clean up accounts registered to retired-at-least-once-since pools
RAISE NOTICE 'DANGLING delegation cleanup from SDC commencing';
DELETE FROM grest.stake_distribution_cache
WHERE stake_address_raw in (
SELECT z.stake_address_raw
WHERE stake_address_id in (
SELECT z.stake_address_id
FROM (
SELECT
(
SELECT max(d.id)
FROM delegation d
INNER JOIN stake_address sd ON sd.hash_raw = sdc.stake_address_raw AND sd.id = d.addr_id) AS last_deleg,
sdc.stake_address_raw
INNER JOIN stake_address sd ON sd.id = sdc.stake_address_id AND sd.id = d.addr_id) AS last_deleg,
sdc.stake_address_id
FROM grest.stake_distribution_cache AS sdc
) AS z
WHERE grest.is_dangling_delegation(z.last_deleg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ BEGIN
-- INSERT QUERY START
INSERT INTO grest.stake_distribution_cache
SELECT
nra.stake_address_raw,
nra.stake_address_id,
ai.delegated_pool AS pool_id,
ai.total_balance::lovelace,
ai.utxo::lovelace,
Expand All @@ -76,7 +76,7 @@ BEGIN
ai.rewards_available::lovelace
FROM newly_registered_accounts AS nra,
LATERAL grest.account_info(array[(SELECT grest.cip5_hex_to_stake_addr(nra.stake_address_raw))]) AS ai
ON CONFLICT (stake_address_raw) DO
ON CONFLICT (stake_address_id) DO
UPDATE
SET
pool_id = EXCLUDED.pool_id,
Expand Down
3 changes: 3 additions & 0 deletions files/grest/rpc/03_utilities/cip129.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF _raw IS NULL THEN RETURN NULL END IF;
IF _is_script THEN
RETURN b32_encode('cc_hot', ('\x03'::bytea || _raw)::text);
ELSE
Expand Down Expand Up @@ -91,6 +92,7 @@ RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF _raw IS NULL THEN RETURN NULL END IF;
IF _is_script THEN
RETURN b32_encode('cc_cold', ('\x13'::bytea || _raw)::text);
ELSE
Expand Down Expand Up @@ -130,6 +132,7 @@ RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF _raw IS NULL THEN RETURN NULL END IF;
IF _is_script THEN
RETURN b32_encode('drep', ('\x23'::bytea || _raw)::text);
ELSE
Expand Down
5 changes: 3 additions & 2 deletions files/grest/rpc/03_utilities/cip5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
-- 0005: Common bech32 prefixes https://cips.cardano.org/cip/CIP-0005
-- 0019: Cardano Addresses https://cips.cardano.org/cip/CIP-0019

CREATE OR REPLACE FUNCTION grest.cip5_hex_to_stake_addr(hex bytea)
CREATE OR REPLACE FUNCTION grest.cip5_hex_to_stake_addr(_raw bytea)
RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF SUBSTRING(hex from 2 for 1) = '0' THEN
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);
ELSE
RETURN b32_encode('stake', hex::text);
Expand Down
17 changes: 7 additions & 10 deletions files/grest/rpc/account/account_info_cached.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ BEGIN
RETURN QUERY

SELECT
grest.cip5_hex_to_stake_addr(sdc.stake_address_raw),
grest.cip5_hex_to_stake_addr(sa.hash_raw),
CASE WHEN status_t.registered = TRUE THEN
'registered'
ELSE
Expand All @@ -48,10 +48,10 @@ BEGIN
COALESCE(reserves_t.reserves, 0)::text AS reserves,
COALESCE(treasury_t.treasury, 0)::text AS treasury
FROM grest.stake_distribution_cache AS sdc
INNER JOIN stake_address AS sa ON sa.id = sdc.stake_address_id
LEFT JOIN (
SELECT
sas.id,
sas.hash_raw,
EXISTS (
SELECT TRUE FROM stake_registration
WHERE
Expand All @@ -76,7 +76,7 @@ BEGIN
) AS deposit
FROM public.stake_address AS sas
WHERE sas.id = ANY(sa_id_list)
) AS status_t ON sdc.stake_address_raw = status_t.hash_raw
) AS status_t ON sdc.stake_address_id = status_t.id
LEFT JOIN (
SELECT
dv.addr_id,
Expand Down Expand Up @@ -118,10 +118,7 @@ BEGIN
GROUP BY
t.addr_id
) AS treasury_t ON treasury_t.addr_id = status_t.id
WHERE sdc.stake_address_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex'))
FROM UNNEST(_stake_addresses) AS n
)
WHERE sdc.stake_address_id = ANY(sa_id_list)

UNION ALL

Expand All @@ -141,13 +138,13 @@ BEGIN
FROM
(
SELECT
sa.hash_raw AS stake_address_raw,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address,
sa.id AS addr_id
FROM stake_address AS sa
WHERE sa.id = ANY(sa_id_list)
AND NOT EXISTS (SELECT null FROM grest.stake_distribution_cache AS sdc WHERE sdc.stake_address_raw = sa.hash_raw)
AND NOT EXISTS (SELECT null FROM grest.stake_distribution_cache AS sdc WHERE sdc.stake_address_id = sa.id)
) AS z
, LATERAL grest.account_info(array[(SELECT grest.cip5_hex_to_stake_addr(z.stake_address_raw))]) AS ai
, LATERAL grest.account_info(array[stake_address]) AS ai
;

END;
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/governance/drep_delegators.sql
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ BEGIN
INNER JOIN stake_address AS sa ON ad.addr_id = sa.id
INNER JOIN tx ON ad.tx_id = tx.id
INNER JOIN block AS b ON tx.block_id = b.id
LEFT JOIN grest.stake_distribution_cache AS sdc ON sa.view = sdc.stake_address
LEFT JOIN grest.stake_distribution_cache AS sdc ON sa.id = sdc.stake_address_id
ORDER BY b.epoch_no DESC, grest.cip5_hex_to_stake_addr(sa.hash_raw)
);

Expand Down
12 changes: 6 additions & 6 deletions files/grest/rpc/pool/pool_delegators.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ BEGIN
_all_delegations AS (
SELECT
sa.id AS stake_address_id,
sdc.stake_address_raw,
sa.hash_raw AS stake_address_raw,
(
CASE WHEN sdc.total_balance >= 0
THEN sdc.total_balance
ELSE 0
END
) AS total_balance
FROM grest.stake_distribution_cache AS sdc
INNER JOIN public.stake_address AS sa ON sa.hash_raw = sdc.stake_address_raw
INNER JOIN public.stake_address AS sa ON sa.id = sdc.stake_address_id
WHERE sdc.pool_id = _pool_bech32

UNION ALL
Expand All @@ -43,7 +43,7 @@ BEGIN
AND NOT EXISTS (SELECT null FROM delegation AS d2 WHERE d2.addr_id = d.addr_id AND d2.id > d.id)
AND NOT EXISTS (SELECT null FROM stake_deregistration AS sd WHERE sd.addr_id = d.addr_id AND sd.tx_id > d.tx_id)
-- AND NOT grest.is_dangling_delegation(d.id)
AND NOT EXISTS (SELECT null FROM grest.stake_distribution_cache AS sdc WHERE sdc.stake_address_raw = sa.hash_raw)
AND NOT EXISTS (SELECT null FROM grest.stake_distribution_cache AS sdc WHERE sdc.stake_address_id = sa.id)
) z,
LATERAL grest.account_utxos(array[(SELECT grest.cip5_hex_to_stake_addr(z.stake_address_raw))], false) AS acc_info
GROUP BY
Expand Down Expand Up @@ -88,15 +88,15 @@ BEGIN
_all_delegations AS (
SELECT
sa.id AS stake_address_id,
sdc.stake_address_raw,
sa.hash_raw AS stake_address_raw,
(
CASE WHEN sdc.total_balance >= 0
THEN sdc.total_balance
ELSE 0
END
) AS total_balance
FROM grest.stake_distribution_cache AS sdc
INNER JOIN public.stake_address AS sa ON sa.hash_raw = sdc.stake_address_raw
INNER JOIN public.stake_address AS sa ON sa.id = sdc.stake_address_id
WHERE sdc.pool_id = _pool_bech32

UNION ALL
Expand All @@ -114,7 +114,7 @@ BEGIN
AND NOT EXISTS (SELECT null FROM delegation AS d2 WHERE d2.addr_id = d.addr_id AND d2.id > d.id)
AND NOT EXISTS (SELECT null FROM stake_deregistration AS sd WHERE sd.addr_id = d.addr_id AND sd.tx_id > d.tx_id)
-- AND NOT grest.is_dangling_delegation(d.id)
AND NOT EXISTS (SELECT null FROM grest.stake_distribution_cache AS sdc WHERE sdc.stake_address_raw = sa.hash_raw)
AND NOT EXISTS (SELECT null FROM grest.stake_distribution_cache AS sdc WHERE sdc.stake_address_id = sa.id)
) z,
LATERAL grest.account_utxos(array[(SELECT grest.cip5_hex_to_stake_addr(z.stake_address_raw))], false) AS acc_info
GROUP BY
Expand Down
8 changes: 4 additions & 4 deletions files/grest/rpc/pool/pool_updates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ BEGIN
pu.margin,
pu.fixed_cost::text,
pu.pledge::text,
sa.view AS reward_addr,
JSONB_AGG(po.view) AS owners,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS reward_addr,
JSONB_AGG(po.stake_address) AS owners,
JSONB_AGG(JSONB_BUILD_OBJECT (
'ipv4', pr.ipv4,
'ipv6', pr.ipv6,
Expand All @@ -57,7 +57,7 @@ BEGIN
INNER JOIN public.block AS b ON b.id = tx.block_id
LEFT JOIN public.stake_address AS sa ON pu.reward_addr_id = sa.id
LEFT JOIN (
SELECT po1.pool_update_id, sa1.view
SELECT po1.pool_update_id, grest.cip5_hex_to_stake_addr(sa1.hash_raw) AS stake_address
FROM public.pool_owner AS po1
LEFT JOIN public.stake_address AS sa1 ON sa1.id = po1.addr_id
) AS po ON pu.id = po.pool_update_id
Expand All @@ -66,7 +66,7 @@ BEGIN
LEFT JOIN public.off_chain_pool_data AS ocpd ON pu.meta_id = ocpd.pmr_id
WHERE _pool_bech32 IS NULL
OR ph.hash_raw = DECODE(b32_decode(_pool_bech32),'hex')
GROUP BY tx.hash, b.time, ph.hash_raw, ph.hash_raw, pu.active_epoch_no, pu.vrf_key_hash, pu.margin, pu.fixed_cost, pu.pledge, sa.view, pmr.url, pmr.hash, ocpd.json),
GROUP BY tx.hash, b.time, ph.hash_raw, ph.hash_raw, pu.active_epoch_no, pu.vrf_key_hash, pu.margin, pu.fixed_cost, pu.pledge, sa.hash_raw, pmr.url, pmr.hash, ocpd.json),
pool_dereg AS (
SELECT
ENCODE(tx.hash::bytea, 'hex') AS tx_hash,
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/script/script_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ BEGIN
tx_out.index::smallint,
tx_out.address::text,
tx_out.value::text,
sa.view::text as stake_address,
grest.cip5_hex_to_stake_addr(sa.hash_raw) as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
b.epoch_no,
b.block_no,
Expand Down
Loading

0 comments on commit d707e56

Please sign in to comment.