diff --git a/files/grest/rpc/03_utilities/cip129.sql b/files/grest/rpc/000_utilities/cip129.sql similarity index 75% rename from files/grest/rpc/03_utilities/cip129.sql rename to files/grest/rpc/000_utilities/cip129.sql index 6233173d..38411446 100644 --- a/files/grest/rpc/03_utilities/cip129.sql +++ b/files/grest/rpc/000_utilities/cip129.sql @@ -1,8 +1,8 @@ -- Binary format --- 1 byte variable length +-- 1 byte variable length -- <------> <-------------------> -- ┌────────┬─────────────────────┐ --- │ header │ key │ +-- │ header │ key │ -- └────────┴─────────────────────┘ -- 🔎 -- ╎ 7 6 5 4 3 2 1 0 @@ -34,11 +34,25 @@ BEGIN END; $$; +CREATE OR REPLACE FUNCTION grest.cip129_cc_hot_has_script(_cc_hot text) +RETURNS boolean +LANGUAGE plpgsql STABLE +AS $$ +BEGIN + IF LENGTH(_cc_hot) = 60 THEN + RETURN SUBSTRING(b32_decode(_cc_hot) from 2 for 1) = '3'; + ELSE + RETURN STARTS_WITH(_cc_hot, 'cc_hot_script'); + END IF; +END; +$$; + CREATE OR REPLACE FUNCTION grest.cip129_hex_to_cc_hot(_raw bytea, _is_script boolean) 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 @@ -60,11 +74,25 @@ BEGIN END; $$; +CREATE OR REPLACE FUNCTION grest.cip129_cc_cold_has_script(_cc_cold text) +RETURNS boolean +LANGUAGE plpgsql STABLE +AS $$ +BEGIN + IF LENGTH(_cc_cold) = 61 THEN + RETURN SUBSTRING(b32_decode(_cc_cold) from 2 for 1) = '3'; + ELSE + RETURN STARTS_WITH(_cc_cold, 'cc_cold_script'); + END IF; +END; +$$; + CREATE OR REPLACE FUNCTION grest.cip129_hex_to_cc_cold(_raw bytea, _is_script boolean) 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 @@ -86,11 +114,25 @@ BEGIN END; $$; +CREATE OR REPLACE FUNCTION grest.cip129_drep_id_has_script(_drep_id text) +RETURNS boolean +LANGUAGE plpgsql STABLE +AS $$ +BEGIN + IF LENGTH(_drep_id) = 58 THEN + RETURN SUBSTRING(b32_decode(_drep_id) from 2 for 1) = '3'; + ELSE + RETURN STARTS_WITH(_drep_id, 'drep_script'); + END IF; +END; +$$; + CREATE OR REPLACE FUNCTION grest.cip129_hex_to_drep_id(_raw bytea, _is_script boolean) 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 @@ -121,10 +163,13 @@ END; $$; COMMENT ON FUNCTION grest.cip129_cc_hot_to_hex IS 'Returns binary hex from Constitutional Committee Hot Credential ID in old or new (CIP-129) format'; -- noqa: LT01 +COMMENT ON FUNCTION grest.cip129_cc_hot_has_script IS 'Returns true if Constitutional Committee Hot Credential ID is of type script'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_hex_to_cc_hot IS 'Returns Constitutional Committee Hot Credential ID in CIP-129 format from raw binary hex'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_cc_cold_to_hex IS 'Returns binary hex from Constitutional Committee Cold Credential ID in old or new (CIP-129) format'; -- noqa: LT01 +COMMENT ON FUNCTION grest.cip129_cc_cold_has_script IS 'Returns true if Constitutional Committee Cold Credential ID is of type script'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_hex_to_cc_cold IS 'Returns Constitutional Committee Cold Credential ID in CIP-129 format from raw binary hex'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_drep_id_to_hex IS 'Returns binary hex from DRep Credential ID in old or new (CIP-129) format'; -- noqa: LT01 +COMMENT ON FUNCTION grest.cip129_drep_id_has_script IS 'Returns true if DRep Credential ID is of type script'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_hex_to_drep_id IS 'Returns DRep Credential ID in CIP-129 format from raw binary hex'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_from_gov_action_id IS 'Returns string array containing transaction hash and certificate index from Governance Action Proposal ID in CIP-129 format'; -- noqa: LT01 COMMENT ON FUNCTION grest.cip129_to_gov_action_id IS 'Returns Governance Action Proposal ID in CIP-129 format from transaction hash appended by index of certificate within the transaction'; -- noqa: LT01 diff --git a/files/grest/rpc/000_utilities/cip5.sql b/files/grest/rpc/000_utilities/cip5.sql new file mode 100644 index 00000000..103043ef --- /dev/null +++ b/files/grest/rpc/000_utilities/cip5.sql @@ -0,0 +1,19 @@ +-- CIP References +-- 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(_raw bytea) +RETURNS text +LANGUAGE plpgsql STABLE +AS $$ +BEGIN + 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', _raw::text); + ELSE + RETURN b32_encode('stake', _raw::text); + END IF; +END; +$$; \ No newline at end of file diff --git a/files/grest/rpc/03_utilities/cip67.sql b/files/grest/rpc/000_utilities/cip67.sql similarity index 100% rename from files/grest/rpc/03_utilities/cip67.sql rename to files/grest/rpc/000_utilities/cip67.sql diff --git a/files/grest/rpc/03_utilities/cli_protocol_params.sql b/files/grest/rpc/000_utilities/cli_protocol_params.sql similarity index 100% rename from files/grest/rpc/03_utilities/cli_protocol_params.sql rename to files/grest/rpc/000_utilities/cli_protocol_params.sql diff --git a/files/grest/rpc/03_utilities/is_dangling_delegation.sql b/files/grest/rpc/000_utilities/is_dangling_delegation.sql similarity index 100% rename from files/grest/rpc/03_utilities/is_dangling_delegation.sql rename to files/grest/rpc/000_utilities/is_dangling_delegation.sql diff --git a/files/grest/rpc/00_blockchain/reserve_withdrawals.sql b/files/grest/rpc/00_blockchain/reserve_withdrawals.sql index 70ca1aa9..b12b2522 100644 --- a/files/grest/rpc/00_blockchain/reserve_withdrawals.sql +++ b/files/grest/rpc/00_blockchain/reserve_withdrawals.sql @@ -19,7 +19,7 @@ AS $$ ENCODE(b.hash,'hex'), b.block_no, r.amount::text, - sa.view, + grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address, earned_epoch, spendable_epoch FROM reserve AS r diff --git a/files/grest/rpc/00_blockchain/treasury_withdrawals.sql b/files/grest/rpc/00_blockchain/treasury_withdrawals.sql index aafdfb16..a5959e1c 100644 --- a/files/grest/rpc/00_blockchain/treasury_withdrawals.sql +++ b/files/grest/rpc/00_blockchain/treasury_withdrawals.sql @@ -19,7 +19,7 @@ AS $$ ENCODE(b.hash,'hex'), b.block_no, t.amount::text, - sa.view, + grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address, earned_epoch, spendable_epoch FROM treasury AS t diff --git a/files/grest/rpc/01_cached_tables/active_stake_cache.sql b/files/grest/rpc/01_cached_tables/active_stake_cache.sql index e77dd3eb..ba56d7d5 100644 --- a/files/grest/rpc/01_cached_tables/active_stake_cache.sql +++ b/files/grest/rpc/01_cached_tables/active_stake_cache.sql @@ -1,5 +1,5 @@ CREATE TABLE IF NOT EXISTS grest.pool_active_stake_cache ( - pool_id varchar NOT NULL, + pool_id bigint NOT NULL, epoch_no bigint NOT NULL, amount lovelace NOT NULL, PRIMARY KEY (pool_id, epoch_no) @@ -64,15 +64,14 @@ BEGIN -- POOL ACTIVE STAKE CACHE INSERT INTO grest.pool_active_stake_cache SELECT - pool_hash.view AS pool_id, + epoch_stake.pool_id AS pool_id, epoch_stake.epoch_no, SUM(epoch_stake.amount) AS amount FROM public.epoch_stake - INNER JOIN public.pool_hash ON pool_hash.id = epoch_stake.pool_id WHERE epoch_stake.epoch_no >= _last_active_stake_validated_epoch AND epoch_stake.epoch_no <= _epoch_no GROUP BY - pool_hash.view, + epoch_stake.pool_id, epoch_stake.epoch_no ON CONFLICT ( pool_id, 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 33d8139a..9e32ae66 100644 --- a/files/grest/rpc/01_cached_tables/pool_history_cache.sql +++ b/files/grest/rpc/01_cached_tables/pool_history_cache.sql @@ -1,7 +1,7 @@ DROP TABLE IF EXISTS grest.pool_history_cache; CREATE TABLE grest.pool_history_cache ( - pool_id varchar, + pool_id bigint, epoch_no int8 NULL, active_stake lovelace NULL, active_stake_pct numeric NULL, @@ -21,13 +21,13 @@ COMMENT ON TABLE grest.pool_history_cache IS 'A history of pool performance incl CREATE OR REPLACE FUNCTION grest.get_pool_history_data_bulk(_epoch_no_to_insert_from word31type, _pool_bech32 text [] DEFAULT null, _epoch_no_until word31type DEFAULT null) RETURNS TABLE ( - pool_id_bech32 text, + pool_id bigint, epoch_no bigint, active_stake lovelace, active_stake_pct numeric, saturation_pct numeric, - block_cnt numeric, - delegator_cnt numeric, + block_cnt bigint, + delegator_cnt bigint, margin double precision, fixed_cost lovelace, pool_fees double precision, @@ -41,11 +41,29 @@ AS $$ DECLARE _pool_ids bigint []; BEGIN - _pool_ids := (SELECT array_agg(id) from pool_hash ph where ph.view = ANY(_pool_bech32)); + _pool_ids := (SELECT ARRAY_AGG(id) from pool_hash ph where ph.hash_raw = ANY( + SELECT DECODE(b32_decode(pool),'hex') + FROM UNNEST(_pool_bech32) AS pool) + ); RETURN QUERY WITH + blockcounts AS ( + SELECT + sl.pool_hash_id, + b.epoch_no, + COUNT(*) AS block_cnt + FROM block AS b, + slot_leader AS sl + WHERE b.slot_leader_id = sl.id + AND (_pool_bech32 is null or sl.pool_hash_id = ANY(_pool_ids)) + AND b.epoch_no >= _epoch_no_to_insert_from + AND (_epoch_no_until is null or b.epoch_no <= _epoch_no_until) + GROUP BY + sl.pool_hash_id, + b.epoch_no + ), leadertotals AS ( SELECT r.pool_id, @@ -78,19 +96,18 @@ BEGIN activeandfees AS ( SELECT - ph.view AS pool_id, - ps.epoch_no, - ps.stake AS active_stake, - ps.number_of_blocks AS block_cnt, - COALESCE(ps.number_of_delegators, 0) AS delegator_cnt, + es.pool_id AS pool_id, + es.epoch_no, + SUM(es.amount) AS active_stake, + COUNT(1) AS delegator_cnt, ( SELECT margin FROM pool_update WHERE id = ( SELECT MAX(pup2.id) FROM pool_update AS pup2 - WHERE pup2.hash_id = ps.pool_hash_id - AND pup2.active_epoch_no <= ps.epoch_no + WHERE pup2.hash_id = es.pool_id + AND pup2.active_epoch_no <= es.epoch_no ) ) AS pool_fee_variable, ( @@ -99,103 +116,103 @@ BEGIN WHERE id = ( SELECT MAX(pup2.id) FROM pool_update AS pup2 - WHERE pup2.hash_id = ps.pool_hash_id - AND pup2.active_epoch_no <= ps.epoch_no) + WHERE pup2.hash_id = es.pool_id + AND pup2.active_epoch_no <= es.epoch_no) ) AS pool_fee_fixed, - (ps.stake / ( + (SUM(es.amount) / ( SELECT NULLIF(easc.amount, 0) FROM grest.epoch_active_stake_cache AS easc - WHERE easc.epoch_no = ps.epoch_no + WHERE easc.epoch_no = es.epoch_no ) ) * 100 AS active_stake_pct, ROUND( - (ps.stake / ( + (SUM(es.amount) / ( SELECT supply::bigint / ( SELECT ep.optimal_pool_count FROM epoch_param AS ep - WHERE ep.epoch_no = ps.epoch_no + WHERE ep.epoch_no = es.epoch_no ) - FROM grest.totals (ps.epoch_no) + FROM grest.totals (es.epoch_no) ) * 100 ), 2 ) AS saturation_pct - FROM pool_stat AS ps - INNER JOIN pool_hash AS ph ON ps.pool_hash_id = ph.id - WHERE ps.epoch_no >= _epoch_no_to_insert_from - AND (_epoch_no_until is null or ps.epoch_no < _epoch_no_until) - AND (_pool_bech32 is null or ph.view = ANY(_pool_bech32)) - GROUP BY ps.pool_hash_id, ph.view, ps.epoch_no, ps.stake, ps.number_of_blocks, ps.number_of_delegators + FROM epoch_stake AS es + WHERE es.epoch_no >= _epoch_no_to_insert_from + AND (_epoch_no_until is null or es.epoch_no < _epoch_no_until) + AND (_pool_bech32 is null or es.pool_id = ANY(_pool_ids)) + GROUP BY es.pool_id, es.epoch_no ) - SELECT - actf.pool_id::text, - actf.epoch_no::bigint, - actf.active_stake::lovelace, - actf.active_stake_pct, - actf.saturation_pct, - COALESCE(actf.block_cnt, 0) AS block_cnt, - actf.delegator_cnt, - actf.pool_fee_variable::double precision, - actf.pool_fee_fixed, - -- for debugging: m.memtotal, - -- for debugging: l.leadertotal, - CASE COALESCE(actf.block_cnt, 0) - WHEN 0 THEN - 0 - ELSE - -- special CASE for WHEN reward information is not available yet - CASE COALESCE(l.leadertotal, 0) + COALESCE(m.memtotal, 0) - WHEN 0 THEN NULL - ELSE - CASE - WHEN COALESCE(l.leadertotal, 0) < actf.pool_fee_fixed THEN COALESCE(l.leadertotal, 0) - ELSE ROUND(actf.pool_fee_fixed + (((COALESCE(m.memtotal, 0) + COALESCE(l.leadertotal, 0)) - actf.pool_fee_fixed) * actf.pool_fee_variable)) - END - END - END AS pool_fees, - CASE COALESCE(actf.block_cnt, 0) - WHEN 0 THEN - 0 - ELSE - -- special CASE for WHEN reward information is not available yet - CASE COALESCE(l.leadertotal, 0) + COALESCE(m.memtotal, 0) - WHEN 0 THEN NULL + SELECT + actf.pool_id::bigint, + actf.epoch_no::bigint, + actf.active_stake::lovelace, + actf.active_stake_pct, + actf.saturation_pct, + COALESCE(b.block_cnt, 0) AS block_cnt, + actf.delegator_cnt, + actf.pool_fee_variable::double precision, + actf.pool_fee_fixed, + -- for debugging: m.memtotal, + -- for debugging: l.leadertotal, + CASE COALESCE(b.block_cnt, 0) + WHEN 0 THEN + 0 + ELSE + -- special CASE for WHEN reward information is not available yet + CASE COALESCE(l.leadertotal, 0) + COALESCE(m.memtotal, 0) + WHEN 0 THEN NULL ELSE CASE - WHEN COALESCE(l.leadertotal, 0) < actf.pool_fee_fixed THEN COALESCE(m.memtotal, 0) - ELSE ROUND(COALESCE(m.memtotal, 0) + (COALESCE(l.leadertotal, 0) - (actf.pool_fee_fixed + (((COALESCE(m.memtotal, 0) + COALESCE(l.leadertotal, 0)) - actf.pool_fee_fixed) * actf.pool_fee_variable)))) + WHEN COALESCE(l.leadertotal, 0) < actf.pool_fee_fixed THEN COALESCE(l.leadertotal, 0) + ELSE ROUND(actf.pool_fee_fixed + (((COALESCE(m.memtotal, 0) + COALESCE(l.leadertotal, 0)) - actf.pool_fee_fixed) * actf.pool_fee_variable)) END - END - END AS deleg_rewards, - CASE COALESCE(actf.block_cnt, 0) - WHEN 0 THEN 0 - ELSE - CASE COALESCE(m.memtotal, 0) - WHEN 0 THEN NULL - ELSE COALESCE(m.memtotal, 0) - END - END::double precision AS member_rewards, - CASE COALESCE(actf.block_cnt, 0) - WHEN 0 THEN 0 + END + END AS pool_fees, + CASE COALESCE(b.block_cnt, 0) + WHEN 0 THEN + 0 + ELSE + -- special CASE for WHEN reward information is not available yet + CASE COALESCE(l.leadertotal, 0) + COALESCE(m.memtotal, 0) + WHEN 0 THEN NULL ELSE - -- special CASE for WHEN reward information is not available yet - CASE COALESCE(l.leadertotal, 0) + COALESCE(m.memtotal, 0) - WHEN 0 THEN NULL - ELSE - CASE - WHEN COALESCE(l.leadertotal, 0) < actf.pool_fee_fixed THEN ROUND((((POW((LEAST(((COALESCE(m.memtotal, 0)) / (NULLIF(actf.active_stake, 0))), 1000) + 1), 73) - 1)) * 100)::numeric, 9) - -- using LEAST AS a way to prevent overflow, in CASE of dodgy database data (e.g. giant rewards / tiny active stake) - ELSE ROUND((((POW((LEAST((((COALESCE(m.memtotal, 0) + (COALESCE(l.leadertotal, 0) - (actf.pool_fee_fixed + (((COALESCE(m.memtotal, 0) - + COALESCE(l.leadertotal, 0)) - actf.pool_fee_fixed) * actf.pool_fee_variable))))) / (NULLIF(actf.active_stake, 0))), 1000) + 1), 73) - 1)) * 100)::numeric, 9) - END + CASE + WHEN COALESCE(l.leadertotal, 0) < actf.pool_fee_fixed THEN COALESCE(m.memtotal, 0) + ELSE ROUND(COALESCE(m.memtotal, 0) + (COALESCE(l.leadertotal, 0) - (actf.pool_fee_fixed + (((COALESCE(m.memtotal, 0) + COALESCE(l.leadertotal, 0)) - actf.pool_fee_fixed) * actf.pool_fee_variable)))) END - END AS epoch_ros - FROM pool_hash AS ph - INNER JOIN activeandfees AS actf ON actf.pool_id = ph.view - LEFT JOIN leadertotals AS l ON ph.id = l.pool_id - AND actf.epoch_no = l.earned_epoch - LEFT JOIN membertotals AS m ON ph.id = m.pool_id - AND actf.epoch_no = m.earned_epoch; + END + END AS deleg_rewards, + CASE COALESCE(b.block_cnt, 0) + WHEN 0 THEN 0 + ELSE + CASE COALESCE(m.memtotal, 0) + WHEN 0 THEN NULL + ELSE COALESCE(m.memtotal, 0) + END + END::double precision AS member_rewards, + CASE COALESCE(b.block_cnt, 0) + WHEN 0 THEN 0 + ELSE + -- special CASE for WHEN reward information is not available yet + CASE COALESCE(l.leadertotal, 0) + COALESCE(m.memtotal, 0) + WHEN 0 THEN NULL + ELSE + CASE + WHEN COALESCE(l.leadertotal, 0) < actf.pool_fee_fixed THEN ROUND((((POW((LEAST(((COALESCE(m.memtotal, 0)) / (NULLIF(actf.active_stake, 0))), 1000) + 1), 73) - 1)) * 100)::numeric, 9) + -- using LEAST AS a way to prevent overflow, in CASE of dodgy database data (e.g. giant rewards / tiny active stake) + ELSE ROUND((((POW((LEAST((((COALESCE(m.memtotal, 0) + (COALESCE(l.leadertotal, 0) - (actf.pool_fee_fixed + (((COALESCE(m.memtotal, 0) + + COALESCE(l.leadertotal, 0)) - actf.pool_fee_fixed) * actf.pool_fee_variable))))) / (NULLIF(actf.active_stake, 0))), 1000) + 1), 73) - 1)) * 100)::numeric, 9) + END + END + END AS epoch_ros + FROM activeandfees AS actf + LEFT JOIN blockcounts AS b ON actf.pool_id = b.pool_hash_id + AND actf.epoch_no = b.epoch_no + LEFT JOIN leadertotals AS l ON actf.pool_id = l.pool_id + AND actf.epoch_no = l.earned_epoch + LEFT JOIN membertotals AS m ON actf.pool_id = m.pool_id + AND actf.epoch_no = m.earned_epoch; END; $$; 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 dd589cd3..23c89dc1 100644 --- a/files/grest/rpc/01_cached_tables/pool_info_cache.sql +++ b/files/grest/rpc/01_cached_tables/pool_info_cache.sql @@ -7,19 +7,17 @@ CREATE TABLE grest.pool_info_cache ( tx_hash text, block_time numeric, pool_hash_id bigint NOT NULL, - pool_id_bech32 character varying NOT NULL, - pool_id_hex text NOT NULL, active_epoch_no bigint NOT NULL, vrf_key_hash text NOT NULL, margin double precision NOT NULL, 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, + meta_url varchar, meta_hash text, pool_status text, retiring_epoch word31type @@ -73,8 +71,6 @@ BEGIN tx_hash, block_time, pool_hash_id, - pool_id_bech32, - pool_id_hex, active_epoch_no, vrf_key_hash, margin, @@ -96,19 +92,16 @@ BEGIN encode(tx.hash::bytea, 'hex'), EXTRACT(EPOCH FROM b.time), _hash_id, - ph.view, - encode(ph.hash_raw::bytea, 'hex'), _active_epoch_no, - encode(_vrf_key_hash::bytea, 'hex'), + ENCODE(_vrf_key_hash::bytea, 'hex'), _margin, _fixed_cost, _pledge, _deposit, - sa.view, + _reward_addr_id, ARRAY( - SELECT sa.view + 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( @@ -131,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; $$; @@ -264,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; @@ -348,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/01_cached_tables/stake_distribution_cache.sql b/files/grest/rpc/01_cached_tables/stake_distribution_cache.sql index 42b994a9..3567a3a6 100644 --- a/files/grest/rpc/01_cached_tables/stake_distribution_cache.sql +++ b/files/grest/rpc/01_cached_tables/stake_distribution_cache.sql @@ -1,6 +1,6 @@ CREATE TABLE IF NOT EXISTS grest.stake_distribution_cache ( - stake_address varchar PRIMARY KEY, - pool_id varchar, + stake_address_id bigint PRIMARY KEY, + pool_id bigint, total_balance numeric, utxo numeric, rewards numeric, @@ -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.view AS stake_address, pool_hash_id FROM stake_address INNER JOIN delegation ON delegation.addr_id = stake_address.id @@ -65,7 +64,7 @@ BEGIN pool_ids AS ( SELECT awdp.stake_address_id, - pool_hash.view AS pool_id + pool_hash.id AS pool_id FROM pool_hash INNER JOIN accounts_with_delegated_pools AS awdp ON awdp.pool_hash_id = pool_hash.id ), @@ -172,7 +171,7 @@ BEGIN -- INSERT QUERY START INSERT INTO grest.stake_distribution_cache SELECT - awdp.stake_address, + 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, @@ -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) DO + ON CONFLICT (stake_address_id) DO UPDATE SET pool_id = excluded.pool_id, total_balance = excluded.total_balance, @@ -211,9 +210,9 @@ BEGIN -- Clean up de-registered accounts DELETE FROM grest.stake_distribution_cache - WHERE stake_address IN ( + WHERE stake_address_id IN ( SELECT DISTINCT ON (sa.id) - sa.view + sa.id FROM stake_address AS sa INNER JOIN stake_deregistration AS sd ON sa.id = sd.addr_id WHERE NOT EXISTS ( @@ -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 in ( - SELECT z.stake_address + 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.view = sdc.stake_address AND sd.id = d.addr_id) AS last_deleg, - sdc.stake_address + 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) diff --git a/files/grest/rpc/01_cached_tables/stake_distribution_new_accounts.sql b/files/grest/rpc/01_cached_tables/stake_distribution_new_accounts.sql index 6c44bc61..08e972c0 100644 --- a/files/grest/rpc/01_cached_tables/stake_distribution_new_accounts.sql +++ b/files/grest/rpc/01_cached_tables/stake_distribution_new_accounts.sql @@ -32,7 +32,7 @@ BEGIN newly_registered_accounts AS ( SELECT DISTINCT ON (stake_address.id) stake_address.id AS stake_address_id, - stake_address.view AS stake_address, + stake_address.hash_raw AS stake_address_raw, pool_hash_id FROM stake_address INNER JOIN delegation ON delegation.addr_id = stake_address.id @@ -67,7 +67,7 @@ BEGIN -- INSERT QUERY START INSERT INTO grest.stake_distribution_cache SELECT - nra.stake_address, + nra.stake_address_id, ai.delegated_pool AS pool_id, ai.total_balance::lovelace, ai.utxo::lovelace, @@ -75,8 +75,8 @@ BEGIN ai.withdrawals::lovelace, ai.rewards_available::lovelace FROM newly_registered_accounts AS nra, - LATERAL grest.account_info(array[nra.stake_address]) AS ai - ON CONFLICT (stake_address) DO + LATERAL grest.account_info(array[(SELECT grest.cip5_hex_to_stake_addr(nra.stake_address_raw))]) AS ai + ON CONFLICT (stake_address_id) DO UPDATE SET pool_id = EXCLUDED.pool_id, diff --git a/files/grest/rpc/account/account_addresses.sql b/files/grest/rpc/account/account_addresses.sql index 2bf217cf..acffa3cb 100644 --- a/files/grest/rpc/account/account_addresses.sql +++ b/files/grest/rpc/account/account_addresses.sql @@ -13,7 +13,10 @@ BEGIN FROM stake_address WHERE - stake_address.VIEW = ANY(_stake_addresses); + stake_address.hash_raw = ANY( + SELECT DECODE(b32_decode(n), 'hex') + FROM UNNEST(_stake_addresses) AS n + ); IF _first_only IS NOT TRUE AND _empty IS NOT TRUE THEN RETURN QUERY @@ -34,7 +37,7 @@ BEGIN ) SELECT - sa.view AS stake_address, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address, JSONB_AGG(txo_addr.address) AS addresses FROM txo_addr @@ -60,7 +63,7 @@ BEGIN ) SELECT - sa.view AS stake_address, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address, JSONB_AGG(txo_addr.address) AS addresses FROM txo_addr diff --git a/files/grest/rpc/account/account_assets.sql b/files/grest/rpc/account/account_assets.sql index 5a64210b..71607da2 100644 --- a/files/grest/rpc/account/account_assets.sql +++ b/files/grest/rpc/account/account_assets.sql @@ -14,7 +14,7 @@ BEGIN WITH _all_assets AS ( SELECT - sa.view, + sa.hash_raw, ma.policy, ma.name, ma.fingerprint, @@ -25,21 +25,24 @@ BEGIN 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 INNER JOIN stake_address AS sa ON sa.id = txo.stake_address_id - WHERE sa.view = ANY(_stake_addresses) + WHERE sa.hash_raw = ANY( + SELECT DECODE(b32_decode(n), 'hex') + FROM UNNEST(_stake_addresses) AS n + ) AND txo.consumed_by_tx_id IS NULL GROUP BY - sa.view, ma.policy, ma.name, ma.fingerprint, aic.decimals + sa.hash_raw, ma.policy, ma.name, ma.fingerprint, aic.decimals ) SELECT - aa.view AS stake_address, + grest.cip5_hex_to_stake_addr(aa.hash_raw)::varchar AS stake_address, ENCODE(aa.policy, 'hex') AS policy_id, ENCODE(aa.name, 'hex') AS asset_name, aa.fingerprint AS fingerprint, aa.decimals AS decimals, aa.quantity::text AS quantity FROM _all_assets AS aa - ORDER BY aa.view; + ORDER BY aa.hash_raw; END; $$; diff --git a/files/grest/rpc/account/account_history.sql b/files/grest/rpc/account/account_history.sql index c4a33884..849e4daa 100644 --- a/files/grest/rpc/account/account_history.sql +++ b/files/grest/rpc/account/account_history.sql @@ -13,15 +13,18 @@ BEGIN FROM stake_address WHERE - stake_address.view = ANY(_stake_addresses); + stake_address.hash_raw = ANY( + SELECT DECODE(b32_decode(n), 'hex') + FROM UNNEST(_stake_addresses) AS n + ); IF _epoch_no IS NOT NULL THEN RETURN QUERY SELECT - sa.view AS stake_address, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address, JSONB_AGG( JSONB_BUILD_OBJECT( - 'pool_id', ph.view, + 'pool_id', b32_encode('pool', ph.hash_raw::text), 'epoch_no', es.epoch_no::bigint, 'active_stake', es.amount::text ) @@ -35,14 +38,14 @@ BEGIN AND sa.id = ANY(sa_id_list) GROUP BY - sa.view; + sa.hash_raw; ELSE RETURN QUERY SELECT - sa.view AS stake_address, + grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address, JSONB_AGG( JSONB_BUILD_OBJECT( - 'pool_id', ph.view, + 'pool_id', b32_encode('pool', ph.hash_raw::text), 'epoch_no', es.epoch_no::bigint, 'active_stake', es.amount::text ) @@ -54,7 +57,7 @@ BEGIN WHERE sa.id = ANY(sa_id_list) GROUP BY - sa.view; + sa.hash_raw; END IF; END; $$; diff --git a/files/grest/rpc/account/account_info.sql b/files/grest/rpc/account/account_info.sql index 227b7e0d..1e4f19b7 100644 --- a/files/grest/rpc/account/account_info.sql +++ b/files/grest/rpc/account/account_info.sql @@ -21,12 +21,15 @@ BEGIN SELECT INTO sa_id_list array_agg(id) FROM stake_address - WHERE stake_address.view = ANY(_stake_addresses); + WHERE stake_address.hash_raw = ANY( + SELECT DECODE(b32_decode(n), 'hex') + FROM UNNEST(_stake_addresses) AS n + ); RETURN QUERY SELECT - status_t.view AS stake_address, + grest.cip5_hex_to_stake_addr(status_t.hash_raw)::varchar AS stake_address, CASE WHEN status_t.registered = TRUE THEN 'registered' ELSE @@ -46,7 +49,7 @@ BEGIN ( SELECT sa.id, - sa.view, + sa.hash_raw, EXISTS ( SELECT TRUE FROM stake_registration AS sr WHERE sr.addr_id = sa.id @@ -88,9 +91,9 @@ BEGIN LEFT JOIN ( SELECT delegation.addr_id, - pool_hash.view AS delegated_pool + b32_encode('pool', ph.hash_raw::text)::varchar AS delegated_pool FROM delegation - INNER JOIN pool_hash ON pool_hash.id = delegation.pool_hash_id + INNER JOIN pool_hash AS ph ON ph.id = delegation.pool_hash_id WHERE delegation.addr_id = ANY(sa_id_list) AND NOT EXISTS ( SELECT TRUE diff --git a/files/grest/rpc/account/account_info_cached.sql b/files/grest/rpc/account/account_info_cached.sql index 59622b66..70d7ee99 100644 --- a/files/grest/rpc/account/account_info_cached.sql +++ b/files/grest/rpc/account/account_info_cached.sql @@ -23,18 +23,21 @@ BEGIN FROM stake_address WHERE - stake_address.view = ANY(_stake_addresses); + stake_address.hash_raw = ANY( + SELECT DECODE(b32_decode(n), 'hex') + FROM UNNEST(_stake_addresses) AS n + ); RETURN QUERY SELECT - sdc.stake_address, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar, CASE WHEN status_t.registered = TRUE THEN 'registered' ELSE 'not registered' END AS status, - sdc.pool_id AS pool_id, + b32_encode('pool', ph.hash_raw::text)::varchar AS delegated_pool, vote_t.delegated_drep, sdc.total_balance::text, sdc.utxo::text, @@ -45,10 +48,11 @@ 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 + INNER JOIN pool_hash AS ph ON sdc.pool_id = ph.id LEFT JOIN ( SELECT sas.id, - sas.view, EXISTS ( SELECT TRUE FROM stake_registration WHERE @@ -73,7 +77,7 @@ BEGIN ) AS deposit FROM public.stake_address AS sas WHERE sas.id = ANY(sa_id_list) - ) AS status_t ON sdc.stake_address = status_t.view + ) AS status_t ON sdc.stake_address_id = status_t.id LEFT JOIN ( SELECT dv.addr_id, @@ -115,7 +119,7 @@ BEGIN GROUP BY t.addr_id ) AS treasury_t ON treasury_t.addr_id = status_t.id - WHERE sdc.stake_address = ANY(_stake_addresses) + WHERE sdc.stake_address_id = ANY(sa_id_list) UNION ALL @@ -135,11 +139,11 @@ BEGIN FROM ( SELECT - sa.view AS stake_address, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address, sa.id AS addr_id FROM stake_address AS sa - WHERE view = ANY(_stake_addresses) - AND NOT EXISTS (SELECT null FROM grest.stake_distribution_cache AS sdc WHERE sdc.stake_address = sa.view) + WHERE sa.id = ANY(sa_id_list) + 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[z.stake_address]) AS ai ; @@ -147,4 +151,4 @@ BEGIN END; $$; -COMMENT ON FUNCTION grest.account_info IS 'Get the cached account information for given stake addresses'; -- noqa: LT01 +COMMENT ON FUNCTION grest.account_info_cached IS 'Get the cached account information for given stake addresses'; -- noqa: LT01 diff --git a/files/grest/rpc/account/account_list.sql b/files/grest/rpc/account/account_list.sql index 7e81e260..781db343 100644 --- a/files/grest/rpc/account/account_list.sql +++ b/files/grest/rpc/account/account_list.sql @@ -7,7 +7,7 @@ RETURNS TABLE ( LANGUAGE sql STABLE AS $$ SELECT - sa.view::text, + grest.cip5_hex_to_stake_addr(sa.hash_raw), ENCODE(sa.hash_raw,'hex'), ENCODE(sa.script_hash,'hex') FROM stake_address AS sa diff --git a/files/grest/rpc/account/account_rewards.sql b/files/grest/rpc/account/account_rewards.sql index 8593d74d..761edd95 100644 --- a/files/grest/rpc/account/account_rewards.sql +++ b/files/grest/rpc/account/account_rewards.sql @@ -13,50 +13,48 @@ BEGIN FROM stake_address WHERE - stake_address.VIEW = ANY(_stake_addresses); + stake_address.hash_raw = ANY( + SELECT DECODE(b32_decode(n), 'hex') + FROM UNNEST(_stake_addresses) AS n + ); IF _epoch_no IS NULL THEN RETURN QUERY SELECT - sa.view, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar, JSONB_AGG( JSONB_BUILD_OBJECT( 'earned_epoch', r.earned_epoch, 'spendable_epoch', r.spendable_epoch, 'amount', r.amount::text, 'type', r.type, - 'pool_id', ph.view + 'pool_id', b32_encode('pool', ph.hash_raw::text) ) ) AS rewards - FROM - reward AS r + FROM reward AS r LEFT JOIN pool_hash AS ph ON r.pool_id = ph.id INNER JOIN stake_address AS sa ON sa.id = r.addr_id - WHERE - r.addr_id = ANY(sa_id_list) - GROUP BY sa.id; + WHERE r.addr_id = ANY(sa_id_list) + GROUP BY sa.hash_raw; ELSE RETURN QUERY SELECT - sa.view, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar, JSONB_AGG( JSONB_BUILD_OBJECT( 'earned_epoch', r.earned_epoch, 'spendable_epoch', r.spendable_epoch, 'amount', r.amount::text, 'type', r.type, - 'pool_id', ph.view + 'pool_id', b32_encode('pool', ph.hash_raw::text) ) ) AS rewards - FROM - reward AS r + FROM reward AS r LEFT JOIN pool_hash AS ph ON r.pool_id = ph.id INNER JOIN stake_address AS sa ON sa.id = r.addr_id - WHERE - r.addr_id = ANY(sa_id_list) + WHERE r.addr_id = ANY(sa_id_list) AND r.earned_epoch = _epoch_no - GROUP BY - sa.id; + GROUP BY sa.hash_raw; END IF; END; $$; diff --git a/files/grest/rpc/account/account_txs.sql b/files/grest/rpc/account/account_txs.sql index 282f5fac..5f92628c 100644 --- a/files/grest/rpc/account/account_txs.sql +++ b/files/grest/rpc/account/account_txs.sql @@ -10,18 +10,21 @@ AS $$ DECLARE _tx_id_min bigint; _tx_id_list bigint[]; + _stake_address_id integer; BEGIN SELECT INTO _tx_id_min id FROM tx WHERE block_id >= (SELECT id FROM block WHERE block_no >= _after_block_height ORDER BY id limit 1) ORDER BY id limit 1; + SELECT INTO _stake_address_id id FROM stake_address WHERE hash_raw = (SELECT DECODE(b32_decode(_stake_address), 'hex')); + -- all tx_out & tx_in tx ids SELECT INTO _tx_id_list ARRAY_AGG(tx_id) FROM ( SELECT tx_id FROM tx_out - WHERE stake_address_id = ANY(SELECT id FROM stake_address WHERE view = _stake_address) + WHERE stake_address_id = _stake_address_id AND tx_id >= _tx_id_min -- UNION @@ -29,7 +32,7 @@ BEGIN SELECT consumed_by_tx_id AS tx_id FROM tx_out WHERE tx_out.consumed_by_tx_id IS NOT NULL - AND tx_out.stake_address_id = ANY(SELECT id FROM stake_address WHERE view = _stake_address) + AND tx_out.stake_address_id = _stake_address_id AND tx_out.consumed_by_tx_id >= _tx_id_min ) AS tmp; diff --git a/files/grest/rpc/account/account_updates.sql b/files/grest/rpc/account/account_updates.sql index 3a851529..0456e5c8 100644 --- a/files/grest/rpc/account/account_updates.sql +++ b/files/grest/rpc/account/account_updates.sql @@ -13,11 +13,14 @@ BEGIN FROM stake_address WHERE - stake_address.VIEW = ANY(_stake_addresses); + stake_address.hash_raw = ANY( + SELECT DECODE(b32_decode(n), 'hex') + FROM UNNEST(_stake_addresses) AS n + ); RETURN QUERY SELECT - sa.view AS stake_address, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address, JSONB_AGG( JSONB_BUILD_OBJECT( 'action_type', actions.action_type, @@ -73,7 +76,7 @@ BEGIN INNER JOIN tx ON tx.id = actions.tx_id INNER JOIN stake_address AS sa ON sa.id = actions.addr_id INNER JOIN block AS b ON b.id = tx.block_id - GROUP BY sa.id; + GROUP BY sa.hash_raw; END; $$; diff --git a/files/grest/rpc/account/account_utxos.sql b/files/grest/rpc/account/account_utxos.sql index 679af2f3..e86f39f0 100644 --- a/files/grest/rpc/account/account_utxos.sql +++ b/files/grest/rpc/account/account_utxos.sql @@ -19,7 +19,16 @@ LANGUAGE plpgsql AS $$ DECLARE known_addresses varchar[]; + sa_id_list integer[]; BEGIN + SELECT INTO sa_id_list + ARRAY_AGG(stake_address.id) + FROM stake_address + WHERE stake_address.hash_raw = ANY( + SELECT DECODE(b32_decode(n), 'hex') + FROM UNNEST(_stake_addresses) AS n + ); + RETURN QUERY WITH _assets AS ( @@ -38,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 IN (SELECT sa.id FROM stake_address AS sa WHERE sa.view = ANY(_stake_addresses)) + WHERE txo.stake_address_id = ANY(sa_id_list) AND txo.consumed_by_tx_id IS NULL GROUP BY txo.id ) @@ -47,7 +56,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, @@ -85,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 IN (SELECT sa.id FROM stake_address AS sa WHERE sa.view = ANY(_stake_addresses)) + 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/address/address_info.sql b/files/grest/rpc/address/address_info.sql index e2a494cf..c5c9bb2f 100644 --- a/files/grest/rpc/address/address_info.sql +++ b/files/grest/rpc/address/address_info.sql @@ -2,7 +2,7 @@ CREATE OR REPLACE FUNCTION grest.address_info(_addresses text []) RETURNS TABLE ( address varchar, balance text, - stake_address character varying, + stake_address varchar, script_address boolean, utxo_set jsonb ) @@ -14,7 +14,7 @@ BEGIN CREATE TEMPORARY TABLE _known_addresses AS SELECT DISTINCT ON (tx_out.address) tx_out.address, - sa.view AS stake_address, + sa.hash_raw AS stake_address_raw, COALESCE(tx_out.address_has_script, 'false') AS script_address FROM tx_out LEFT JOIN stake_address AS sa ON sa.id = tx_out.stake_address_id @@ -42,7 +42,7 @@ BEGIN SELECT ka.address, COALESCE(SUM(au.value), '0')::text AS balance, - ka.stake_address, + grest.cip5_hex_to_stake_addr(ka.stake_address_raw)::varchar, ka.script_address, CASE WHEN EXISTS ( @@ -110,7 +110,7 @@ BEGIN LEFT JOIN script ON script.id = au.reference_script_id GROUP BY ka.address, - ka.stake_address, + ka.stake_address_raw, ka.script_address; DROP TABLE _known_addresses; END; diff --git a/files/grest/rpc/address/address_utxos.sql b/files/grest/rpc/address/address_utxos.sql index b317e9e1..5cbc4363 100644 --- a/files/grest/rpc/address/address_utxos.sql +++ b/files/grest/rpc/address/address_utxos.sql @@ -47,7 +47,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)::text as stake_address, ENCODE(tx_out.payment_cred, 'hex') AS payment_cred, b.epoch_no, b.block_no, diff --git a/files/grest/rpc/address/credential_utxos.sql b/files/grest/rpc/address/credential_utxos.sql index 5c160941..57bfe179 100644 --- a/files/grest/rpc/address/credential_utxos.sql +++ b/files/grest/rpc/address/credential_utxos.sql @@ -53,7 +53,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)::text as stake_address, ENCODE(tx_out.payment_cred, 'hex') AS payment_cred, b.epoch_no, b.block_no, diff --git a/files/grest/rpc/assets/asset_addresses.sql b/files/grest/rpc/assets/asset_addresses.sql index 8a3dacb5..466bdc48 100644 --- a/files/grest/rpc/assets/asset_addresses.sql +++ b/files/grest/rpc/assets/asset_addresses.sql @@ -28,13 +28,13 @@ BEGIN RETURN QUERY SELECT x.address, - x.stake_address, + grest.cip5_hex_to_stake_addr(x.stake_address_raw)::varchar, SUM(x.quantity)::text FROM ( SELECT txo.address, - sa.view AS stake_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 @@ -42,18 +42,18 @@ BEGIN WHERE atoc.ma_id = _asset_id AND txo.consumed_by_tx_id IS NULL ) AS x - GROUP BY x.address, x.stake_address; + GROUP BY x.address, x.stake_address_raw; ELSE RETURN QUERY SELECT x.address, - x.stake_address, + grest.cip5_hex_to_stake_addr(x.stake_address_raw)::varchar, SUM(x.quantity)::text FROM ( SELECT txo.address, - sa.view AS stake_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 @@ -61,7 +61,7 @@ BEGIN WHERE mto.ident = _asset_id AND txo.consumed_by_tx_id IS NULL ) AS x - GROUP BY x.address, x.stake_address; + GROUP BY x.address, x.stake_address_raw; END IF; END; $$; diff --git a/files/grest/rpc/assets/asset_history.sql b/files/grest/rpc/assets/asset_history.sql index 77833621..d9555938 100644 --- a/files/grest/rpc/assets/asset_history.sql +++ b/files/grest/rpc/assets/asset_history.sql @@ -2,7 +2,7 @@ CREATE OR REPLACE FUNCTION grest.asset_history(_asset_policy text, _asset_name t RETURNS TABLE ( policy_id text, asset_name text, - fingerprint character varying, + fingerprint varchar, minting_txs jsonb [] ) LANGUAGE plpgsql diff --git a/files/grest/rpc/assets/asset_info.sql b/files/grest/rpc/assets/asset_info.sql index d1cd4bae..4524600f 100644 --- a/files/grest/rpc/assets/asset_info.sql +++ b/files/grest/rpc/assets/asset_info.sql @@ -3,7 +3,7 @@ RETURNS TABLE ( policy_id text, asset_name text, asset_name_ascii text, - fingerprint character varying, + fingerprint varchar, minting_tx_hash text, total_supply text, mint_cnt bigint, diff --git a/files/grest/rpc/assets/asset_info_bulk.sql b/files/grest/rpc/assets/asset_info_bulk.sql index c8b0e4b1..9eca5f99 100644 --- a/files/grest/rpc/assets/asset_info_bulk.sql +++ b/files/grest/rpc/assets/asset_info_bulk.sql @@ -3,7 +3,7 @@ RETURNS TABLE ( policy_id text, asset_name text, asset_name_ascii text, - fingerprint character varying, + fingerprint varchar, minting_tx_hash text, total_supply text, mint_cnt bigint, diff --git a/files/grest/rpc/assets/asset_nft_address.sql b/files/grest/rpc/assets/asset_nft_address.sql index 5e3dac35..2c5ca3f5 100644 --- a/files/grest/rpc/assets/asset_nft_address.sql +++ b/files/grest/rpc/assets/asset_nft_address.sql @@ -28,7 +28,7 @@ BEGIN RETURN QUERY SELECT txo.address, - sa.view AS stake_address + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address FROM tx_out AS txo LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id WHERE txo.id = ( @@ -40,7 +40,7 @@ BEGIN RETURN QUERY SELECT txo.address, - sa.view AS stake_address + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address FROM tx_out AS txo 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 diff --git a/files/grest/rpc/assets/asset_summary.sql b/files/grest/rpc/assets/asset_summary.sql index 213284d6..4c77e222 100644 --- a/files/grest/rpc/assets/asset_summary.sql +++ b/files/grest/rpc/assets/asset_summary.sql @@ -2,7 +2,7 @@ CREATE OR REPLACE FUNCTION grest.asset_summary(_asset_policy text, _asset_name t RETURNS TABLE ( policy_id text, asset_name text, - fingerprint character varying, + fingerprint varchar, total_transactions bigint, staked_wallets bigint, unstaked_addresses bigint, diff --git a/files/grest/rpc/assets/asset_utxos.sql b/files/grest/rpc/assets/asset_utxos.sql index 17392559..25d5df08 100644 --- a/files/grest/rpc/assets/asset_utxos.sql +++ b/files/grest/rpc/assets/asset_utxos.sql @@ -66,7 +66,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)::text as stake_address, ENCODE(tx_out.payment_cred, 'hex') AS payment_cred, b.epoch_no, b.block_no, diff --git a/files/grest/rpc/assets/policy_asset_addresses.sql b/files/grest/rpc/assets/policy_asset_addresses.sql index 32974095..6dc0f3ab 100644 --- a/files/grest/rpc/assets/policy_asset_addresses.sql +++ b/files/grest/rpc/assets/policy_asset_addresses.sql @@ -28,7 +28,7 @@ BEGIN SELECT atoc.ma_id, txo.address, - sa.view AS stake_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 @@ -47,7 +47,7 @@ BEGIN SELECT ENCODE(ma.name, 'hex') AS asset_name, txo.address, - sa.view AS stake_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 @@ -58,7 +58,7 @@ BEGIN GROUP BY ma.name, txo.address, - sa.view; + sa.hash_raw; END IF; END; $$; diff --git a/files/grest/rpc/blocks/block_info.sql b/files/grest/rpc/blocks/block_info.sql index 2aa94a92..55b847a8 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, - ph.view AS pool, + b32_encode('pool', ph.hash_raw::text)::varchar 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 77141539..6a1077b6 100644 --- a/files/grest/rpc/blocks/blocks.sql +++ b/files/grest/rpc/blocks/blocks.sql @@ -8,8 +8,8 @@ RETURNS TABLE ( block_size word31type, block_time integer, tx_count bigint, - vrf_key character varying, - pool character varying, + vrf_key varchar, + pool varchar, proto_major word31type, proto_minor word31type, op_cert_counter word63type, @@ -27,7 +27,7 @@ AS $$ EXTRACT(EPOCH FROM b.time)::integer AS block_time, b.tx_count, b.vrf_key, - ph.view 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/committee_votes.sql b/files/grest/rpc/governance/committee_votes.sql index 14436892..b3520d28 100644 --- a/files/grest/rpc/governance/committee_votes.sql +++ b/files/grest/rpc/governance/committee_votes.sql @@ -31,6 +31,7 @@ AS $$ CASE WHEN _cc_hot_id IS NULL THEN TRUE ELSE ch.raw = DECODE((SELECT grest.cip129_cc_hot_to_hex(_cc_hot_id)), 'hex') + AND ch.has_script = grest.cip129_cc_hot_has_script(_cc_hot_id) END ORDER BY vote_tx.id DESC; diff --git a/files/grest/rpc/governance/drep_delegators.sql b/files/grest/rpc/governance/drep_delegators.sql index ab87da0d..95df4fa5 100644 --- a/files/grest/rpc/governance/drep_delegators.sql +++ b/files/grest/rpc/governance/drep_delegators.sql @@ -13,7 +13,7 @@ DECLARE last_reg_tx_id bigint; BEGIN - IF STARTS_WITH(_drep_id,'drep_') THEN + IF STARTS_WITH(_drep_id,'drep_always') THEN -- predefined DRep roles SELECT INTO drep_idx id FROM public.drep_hash @@ -23,7 +23,8 @@ BEGIN ELSE SELECT INTO drep_idx id FROM public.drep_hash - WHERE raw = DECODE((SELECT grest.cip129_drep_id_to_hex(_drep_id)), 'hex'); + WHERE raw = DECODE((SELECT grest.cip129_drep_id_to_hex(_drep_id)), 'hex') + AND has_script = grest.cip129_drep_id_has_script(_drep_id); SELECT INTO last_reg_tx_id MAX(tx_id) FROM public.drep_registration @@ -79,7 +80,7 @@ BEGIN ) SELECT - sa.view::text, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::text, ENCODE(sa.hash_raw,'hex'), ENCODE(sa.script_hash,'hex'), b.epoch_no, @@ -88,8 +89,8 @@ 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 - ORDER BY b.epoch_no DESC, sa.view + 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) ); END; diff --git a/files/grest/rpc/governance/drep_info.sql b/files/grest/rpc/governance/drep_info.sql index ce58a642..9124981a 100644 --- a/files/grest/rpc/governance/drep_info.sql +++ b/files/grest/rpc/governance/drep_info.sql @@ -8,14 +8,13 @@ RETURNS TABLE ( active boolean, expires_epoch_no numeric, amount text, - url character varying, + url varchar, hash text ) LANGUAGE plpgsql AS $$ DECLARE curr_epoch word31type; - drep_ids_raw hash28type[]; drep_list bigint[]; drep_activity word64type; BEGIN @@ -24,20 +23,22 @@ BEGIN SELECT INTO drep_activity ep.drep_activity FROM public.epoch_param AS ep WHERE ep.epoch_no = curr_epoch; - SELECT INTO drep_ids_raw ARRAY_REMOVE(ARRAY_AGG( - CASE - WHEN STARTS_WITH(n,'drep_') THEN NULL - ELSE - DECODE(grest.cip129_drep_id_to_hex(n), 'hex') - END - ), NULL) FROM UNNEST(_drep_ids) AS n; - -- all DRep ids SELECT INTO drep_list ARRAY_AGG(id) FROM ( SELECT id - FROM public.drep_hash - WHERE raw = ANY(drep_ids_raw) + FROM public.drep_hash AS dh + INNER JOIN ( + SELECT + CASE + WHEN STARTS_WITH(n,'drep_always') THEN NULL + ELSE + DECODE(grest.cip129_drep_id_to_hex(n), 'hex') + END AS hex, + grest.cip129_drep_id_has_script(n) AS has_script + FROM UNNEST(_drep_ids) AS n + ) AS dip ON dip.hex = dh.raw AND dip.has_script = dh.has_script + WHERE dh.raw IS NOT NULL ) AS tmp; IF 'drep_always_abstain' = ANY(_drep_ids) THEN @@ -124,9 +125,9 @@ BEGIN END AS drep_id, ENCODE(dh.raw, 'hex')::text AS hex, dh.has_script AS has_script, - (CASE WHEN starts_with(dh.view,'drep_') OR (COALESCE(dr.deposit, 0) >= 0 AND dr.drep_hash_id IS NOT NULL) THEN TRUE ELSE FALSE END) AS registered, - (CASE WHEN (dr.deposit < 0) OR starts_with(dh.view,'drep_') THEN NULL ELSE ds.deposit END)::text AS deposit, - (CASE WHEN starts_with(dh.view,'drep_') THEN TRUE ELSE COALESCE(dr.deposit, 0) >= 0 AND ds.active END) AS active, + (CASE WHEN starts_with(dh.view,'drep_always') OR (COALESCE(dr.deposit, 0) >= 0 AND dr.drep_hash_id IS NOT NULL) THEN TRUE ELSE FALSE END) AS registered, + (CASE WHEN (dr.deposit < 0) OR starts_with(dh.view,'drep_always') THEN NULL ELSE ds.deposit END)::text AS deposit, + (CASE WHEN starts_with(dh.view,'drep_always') THEN TRUE ELSE COALESCE(dr.deposit, 0) >= 0 AND ds.active END) AS active, (CASE WHEN COALESCE(dr.deposit, 0) >= 0 THEN ds.expires_epoch_no ELSE NULL END) AS expires_epoch_no, COALESCE(dd.amount, 0)::text AS amount, va.url, diff --git a/files/grest/rpc/governance/drep_metadata.sql b/files/grest/rpc/governance/drep_metadata.sql index ebf35824..f8bd9358 100644 --- a/files/grest/rpc/governance/drep_metadata.sql +++ b/files/grest/rpc/governance/drep_metadata.sql @@ -3,23 +3,19 @@ RETURNS TABLE ( drep_id text, hex text, has_script boolean, - url character varying, + url varchar, hash text, json jsonb, bytes text, - warning character varying, - language character varying, - comment character varying, + warning varchar, + language varchar, + comment varchar, is_valid boolean ) LANGUAGE plpgsql AS $$ -DECLARE - drep_ids_raw hash28type[]; BEGIN - SELECT INTO drep_ids_raw ARRAY_AGG(DECODE(grest.cip129_drep_id_to_hex(n), 'hex')) FROM UNNEST(_drep_ids) AS n; - RETURN QUERY ( SELECT DISTINCT ON (dh.raw) grest.cip129_hex_to_drep_id(dh.raw, dh.has_script) AS drep_id, @@ -34,10 +30,20 @@ BEGIN ocvd.comment AS comment, COALESCE(ocvd.is_valid, true) AS is_valid FROM public.drep_hash AS dh + INNER JOIN ( + SELECT + CASE + WHEN STARTS_WITH(n,'drep_always') THEN NULL + ELSE + DECODE(grest.cip129_drep_id_to_hex(n), 'hex') + END AS hex, + grest.cip129_drep_id_has_script(n) AS has_script + FROM UNNEST(_drep_ids) AS n + ) AS dip ON dip.hex = dh.raw AND dip.has_script = dh.has_script INNER JOIN public.drep_registration AS dr ON dh.id = dr.drep_hash_id LEFT JOIN public.voting_anchor AS va ON dr.voting_anchor_id = va.id LEFT JOIN public.off_chain_vote_data AS ocvd ON va.id = ocvd.voting_anchor_id - WHERE dh.raw = ANY(drep_ids_raw) + WHERE dh.raw IS NOT NULL ORDER BY dh.raw, dr.tx_id DESC ); diff --git a/files/grest/rpc/governance/drep_updates.sql b/files/grest/rpc/governance/drep_updates.sql index f569794b..52a5ea8c 100644 --- a/files/grest/rpc/governance/drep_updates.sql +++ b/files/grest/rpc/governance/drep_updates.sql @@ -40,6 +40,7 @@ AS $$ CASE WHEN _drep_id IS NULL THEN TRUE ELSE dh.raw = DECODE((SELECT grest.cip129_drep_id_to_hex(_drep_id)), 'hex') + AND dh.has_script = grest.cip129_drep_id_has_script(_drep_id) END ORDER BY block_time DESC; diff --git a/files/grest/rpc/governance/drep_votes.sql b/files/grest/rpc/governance/drep_votes.sql index 4b32a831..cf2aec5c 100644 --- a/files/grest/rpc/governance/drep_votes.sql +++ b/files/grest/rpc/governance/drep_votes.sql @@ -28,6 +28,7 @@ AS $$ INNER JOIN public.block AS b ON vote_tx.block_id = b.id LEFT JOIN public.voting_anchor AS va ON vp.voting_anchor_id = va.id WHERE dh.raw = DECODE((SELECT grest.cip129_drep_id_to_hex(_drep_id)), 'hex') + AND dh.has_script = grest.cip129_drep_id_has_script(_drep_id) ORDER BY vote_tx.id DESC; $$; diff --git a/files/grest/rpc/governance/proposal_list.sql b/files/grest/rpc/governance/proposal_list.sql index 3910d3ef..79efd47c 100644 --- a/files/grest/rpc/governance/proposal_list.sql +++ b/files/grest/rpc/governance/proposal_list.sql @@ -33,7 +33,7 @@ AS $$ gap.type, gap.description, gap.deposit::text, - sa.view, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::text, b.epoch_no, gap.ratified_epoch, gap.enacted_epoch, @@ -51,7 +51,7 @@ AS $$ ELSE JSONB_BUILD_OBJECT( 'stake_address', ( - SELECT sa2.view + SELECT grest.cip5_hex_to_stake_addr(sa2.hash_raw)::text FROM stake_address AS sa2 WHERE sa2.id = tw.stake_address_id ), diff --git a/files/grest/rpc/governance/proposal_votes.sql b/files/grest/rpc/governance/proposal_votes.sql index c6beee12..174b055a 100644 --- a/files/grest/rpc/governance/proposal_votes.sql +++ b/files/grest/rpc/governance/proposal_votes.sql @@ -6,7 +6,7 @@ RETURNS TABLE ( voter_hex text, voter_has_script boolean, vote vote, - meta_url character varying, + meta_url varchar, meta_hash text ) LANGUAGE plpgsql @@ -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.view IS NOT NULL THEN ph.view + 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 4f02102b..da2a4e52 100644 --- a/files/grest/rpc/governance/proposal_voting_summary.sql +++ b/files/grest/rpc/governance/proposal_voting_summary.sql @@ -20,7 +20,7 @@ RETURNS TABLE ( committee_no_pct numeric ) LANGUAGE plpgsql -as $$ +AS $$ DECLARE proposal text[]; BEGIN @@ -29,12 +29,22 @@ BEGIN RETURN QUERY ( WITH + latest_votes AS ( + SELECT * FROM voting_procedure vp + WHERE NOT EXISTS (SELECT 1 FROM voting_procedure vp2 + WHERE vp2.gov_action_proposal_id = vp.gov_action_proposal_id + AND vp2.id > vp.id + AND vp2.voter_role = vp.voter_role + AND coalesce(vp2.drep_voter, vp2.pool_voter, vp2.committee_voter) = + coalesce(vp.drep_voter, vp.pool_voter, vp.committee_voter)) + ), proposal_epoch_data AS ( SELECT gap.id AS gov_action_proposal_id, gap.type AS proposal_type, expired_epoch, ratified_epoch, + dropped_epoch, (coalesce(ratified_epoch, expired_epoch, dropped_epoch, ( SELECT MAX(no) FROM epoch))) AS epoch_of_interest FROM gov_action_proposal gap INNER JOIN tx t ON gap.tx_id = t.id AND t.hash = DECODE(proposal[1], 'hex') AND gap.index = proposal[2]::smallint @@ -52,8 +62,26 @@ BEGIN vote, COUNT(*) AS active_drep_votes_cast FROM proposal_epoch_data AS ped - INNER JOIN voting_procedure AS vp ON vp.voter_role = 'DRep' AND vp.gov_action_proposal_id = ped.gov_action_proposal_id + INNER JOIN latest_votes AS vp ON vp.voter_role = 'DRep' AND vp.gov_action_proposal_id = ped.gov_action_proposal_id LEFT JOIN drep_distr AS dd ON vp.drep_voter = dd.hash_id AND dd.epoch_no = ped.epoch_of_interest + WHERE NOT EXISTS + ( + SELECT 1 + FROM drep_registration as dr + WHERE dr.drep_hash_id = vp.drep_voter + -- deregistration tx + AND deposit < 0 + -- submitted after vote tx + AND dr.tx_id > vp.tx_id + -- but before last tx of epoch preceding ratification/expiry/drop epoch or last tx of current epoch + AND dr.tx_id < + ( + SELECT i_last_tx_id + FROM grest.epoch_info_cache as eic + WHERE eic.epoch_no = + (coalesce(ped.ratified_epoch, ped.expired_epoch, ped.dropped_epoch, (SELECT max(no) + 1 FROM epoch)) - 1) + ) + ) GROUP BY ped.gov_action_proposal_id, vote ), always_no_conf_data AS ( @@ -78,6 +106,9 @@ BEGIN SUM(voting_power) AS tot_pool_power FROM proposal_epoch_data AS ped INNER JOIN pool_stat ON pool_stat.epoch_no = ped.epoch_of_interest + -- if hard fork initiation, then need to use full SPO voting power otherwise just voted SPO power + WHERE ((ped.proposal_type = 'HardForkInitiation') or EXISTS (SELECT 1 FROM latest_votes vp where vp.voter_role = 'SPO' + AND vp.gov_action_proposal_id = ped.gov_action_proposal_id AND vp.pool_voter = pool_stat.pool_hash_id)) GROUP BY ped.gov_action_proposal_id, pool_stat.epoch_no ), active_prop_pool_votes AS ( @@ -87,7 +118,7 @@ BEGIN vote, COUNT(*) AS pool_votes_cast FROM proposal_epoch_data AS ped - INNER JOIN voting_procedure AS vp ON vp.voter_role = 'SPO' AND vp.gov_action_proposal_id = ped.gov_action_proposal_id + INNER JOIN latest_votes AS vp ON vp.voter_role = 'SPO' AND vp.gov_action_proposal_id = ped.gov_action_proposal_id INNER JOIN pool_stat ON vp.pool_voter = pool_stat.pool_hash_id AND pool_stat.epoch_no = ped.epoch_of_interest GROUP BY ped.gov_action_proposal_id, vote ), @@ -97,13 +128,8 @@ BEGIN vote, COUNT(*) AS committee_votes_cast FROM proposal_epoch_data AS ped - INNER JOIN voting_procedure AS vp ON vp.voter_role = 'ConstitutionalCommittee' + INNER JOIN latest_votes AS vp ON vp.voter_role = 'ConstitutionalCommittee' AND vp.gov_action_proposal_id = ped.gov_action_proposal_id - AND NOT EXISTS ( - SELECT null - FROM voting_procedure AS vp2 - WHERE vp2.gov_action_proposal_id = vp.gov_action_proposal_id - AND vp2.committee_voter = vp.committee_voter AND vp2.id > vp.id) GROUP BY ped.gov_action_proposal_id, vote ), tot_committee_size AS ( diff --git a/files/grest/rpc/governance/voter_proposal_list.sql b/files/grest/rpc/governance/voter_proposal_list.sql index d110c7f5..46e575c5 100644 --- a/files/grest/rpc/governance/voter_proposal_list.sql +++ b/files/grest/rpc/governance/voter_proposal_list.sql @@ -7,18 +7,18 @@ RETURNS TABLE ( proposal_type govactiontype, proposal_description jsonb, deposit text, - return_address character varying, + return_address varchar, proposed_epoch word31type, ratified_epoch word31type, enacted_epoch word31type, dropped_epoch word31type, expired_epoch word31type, expiration word31type, - meta_url character varying, + meta_url varchar, meta_hash text, meta_json jsonb, - meta_comment character varying, - meta_language character varying, + meta_comment varchar, + meta_language varchar, meta_is_valid boolean, withdrawal jsonb, param_proposal jsonb @@ -33,11 +33,11 @@ DECLARE BEGIN IF STARTS_WITH(_voter_id, 'drep') THEN - SELECT INTO _drep_id id FROM public.drep_hash WHERE raw = DECODE((SELECT grest.cip129_drep_id_to_hex(_voter_id)), 'hex'); + SELECT INTO _drep_id id FROM public.drep_hash WHERE raw = DECODE((SELECT grest.cip129_drep_id_to_hex(_voter_id)), 'hex') AND has_script = grest.cip129_drep_id_has_script(_voter_id); ELSIF STARTS_WITH(_voter_id, 'pool') THEN - SELECT INTO _spo_id id FROM public.pool_hash WHERE view = _voter_id; + SELECT INTO _spo_id id FROM public.pool_hash WHERE hash_raw = DECODE(b32_decode(_voter_id),'hex'); ELSIF STARTS_WITH(_voter_id, 'cc_hot') THEN - SELECT INTO _committee_member_id id FROM public.committee_hash WHERE raw = DECODE((SELECT grest.cip129_cc_hot_to_hex(_voter_id)), 'hex'); + SELECT INTO _committee_member_id id FROM public.committee_hash WHERE raw = DECODE((SELECT grest.cip129_cc_hot_to_hex(_voter_id)), 'hex') AND has_script = grest.cip129_cc_hot_has_script(_voter_id); END IF; SELECT INTO _gap_id_list ARRAY_AGG(gov_action_proposal_id) @@ -63,7 +63,7 @@ BEGIN gap.type, gap.description, gap.deposit::text, - sa.view, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::text, b.epoch_no, gap.ratified_epoch, gap.enacted_epoch, @@ -81,7 +81,7 @@ BEGIN ELSE JSONB_BUILD_OBJECT( 'stake_address', ( - SELECT sa2.view + SELECT grest.cip5_hex_to_stake_addr(sa2.hash_raw)::text FROM stake_address AS sa2 WHERE sa2.id = tw.stake_address_id ), diff --git a/files/grest/rpc/pool/pool_blocks.sql b/files/grest/rpc/pool/pool_blocks.sql index e1299b89..9ea6e087 100644 --- a/files/grest/rpc/pool/pool_blocks.sql +++ b/files/grest/rpc/pool/pool_blocks.sql @@ -18,7 +18,7 @@ AS $$ EXTRACT(EPOCH FROM b.time)::integer FROM public.block AS b INNER JOIN public.slot_leader AS sl ON b.slot_leader_id = sl.id - WHERE sl.pool_hash_id = (SELECT id FROM public.pool_hash WHERE view = _pool_bech32) + WHERE sl.pool_hash_id = (SELECT id FROM public.pool_hash WHERE hash_raw = DECODE(b32_decode(_pool_bech32),'hex')) AND (_epoch_no IS NULL OR b.epoch_no = _epoch_no); $$; diff --git a/files/grest/rpc/pool/pool_delegators.sql b/files/grest/rpc/pool/pool_delegators.sql index 740c9559..bd67bc67 100644 --- a/files/grest/rpc/pool/pool_delegators.sql +++ b/files/grest/rpc/pool/pool_delegators.sql @@ -1,6 +1,6 @@ CREATE OR REPLACE FUNCTION grest.pool_delegators(_pool_bech32 text) RETURNS TABLE ( - stake_address character varying, + stake_address varchar, amount text, active_epoch_no bigint, latest_delegation_tx_hash text @@ -16,7 +16,7 @@ BEGIN _all_delegations AS ( SELECT sa.id AS stake_address_id, - sdc.stake_address, + sa.hash_raw AS stake_address_raw, ( CASE WHEN sdc.total_balance >= 0 THEN sdc.total_balance @@ -24,35 +24,35 @@ BEGIN END ) AS total_balance FROM grest.stake_distribution_cache AS sdc - INNER JOIN public.stake_address AS sa ON sa.view = sdc.stake_address - WHERE sdc.pool_id = _pool_bech32 + INNER JOIN public.stake_address AS sa ON sa.id = sdc.stake_address_id + WHERE sdc.pool_id = (SELECT id FROM pool_hash WHERE view = _pool_bech32) UNION ALL -- combine with registered delegations not in stake-dist-cache yet SELECT - z.stake_address_id, z.stake_address, SUM(acc_info.value::numeric) AS total_balance + z.stake_address_id, z.stake_address_raw, SUM(acc_info.value::numeric) AS total_balance FROM ( SELECT sa.id AS stake_address_id, - sa.view AS stake_address + sa.hash_raw AS stake_address_raw FROM delegation AS d - INNER JOIN pool_hash AS ph ON d.pool_hash_id = ph.id AND ph.view = _pool_bech32 + INNER JOIN pool_hash AS ph ON d.pool_hash_id = ph.id AND ph.hash_raw = DECODE(b32_decode(_pool_bech32),'hex') INNER JOIN stake_address AS sa ON d.addr_id = sa.id 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 = sa.view) + 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[z.stake_address], false) AS acc_info + LATERAL grest.account_utxos(array[(SELECT grest.cip5_hex_to_stake_addr(z.stake_address_raw))], false) AS acc_info GROUP BY z.stake_address_id, - z.stake_address + z.stake_address_raw ) - SELECT DISTINCT ON (ad.stake_address) - ad.stake_address, + SELECT DISTINCT ON (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') @@ -60,7 +60,7 @@ BEGIN INNER JOIN public.delegation AS d ON d.addr_id = ad.stake_address_id INNER JOIN public.tx ON tx.id = d.tx_id ORDER BY - ad.stake_address, + ad.stake_address_raw, d.tx_id DESC; END; $$; @@ -72,7 +72,7 @@ COMMENT ON FUNCTION grest.pool_delegators IS 'Return information about live dele CREATE OR REPLACE FUNCTION grest.pool_delegators_list(_pool_bech32 text) RETURNS TABLE ( - stake_address character varying, + stake_address varchar, amount text ) LANGUAGE plpgsql @@ -81,14 +81,14 @@ AS $$ DECLARE _pool_id bigint; BEGIN - SELECT id INTO _pool_id FROM pool_hash WHERE pool_hash.view = _pool_bech32; + SELECT id INTO _pool_id FROM pool_hash WHERE pool_hash.hash_raw = DECODE(b32_decode(_pool_bech32),'hex'); RETURN QUERY WITH _all_delegations AS ( SELECT sa.id AS stake_address_id, - sdc.stake_address, + sa.hash_raw AS stake_address_raw, ( CASE WHEN sdc.total_balance >= 0 THEN sdc.total_balance @@ -96,34 +96,34 @@ BEGIN END ) AS total_balance FROM grest.stake_distribution_cache AS sdc - INNER JOIN public.stake_address AS sa ON sa.view = sdc.stake_address - WHERE sdc.pool_id = _pool_bech32 + INNER JOIN public.stake_address AS sa ON sa.id = sdc.stake_address_id + WHERE sdc.pool_id = _pool_id UNION ALL -- combine with registered delegations not in stake-dist-cache yet SELECT - z.stake_address_id, z.stake_address, SUM(acc_info.value::numeric) AS total_balance + z.stake_address_id, z.stake_address_raw, SUM(acc_info.value::numeric) AS total_balance FROM ( SELECT sa.id AS stake_address_id, - sa.view AS stake_address + sa.hash_raw AS stake_address_raw FROM delegation AS d INNER JOIN stake_address AS sa ON d.addr_id = sa.id and d.pool_hash_id = _pool_id 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 = sa.view) + 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[z.stake_address], false) AS acc_info + LATERAL grest.account_utxos(array[(SELECT grest.cip5_hex_to_stake_addr(z.stake_address_raw))], false) AS acc_info GROUP BY z.stake_address_id, - z.stake_address + z.stake_address_raw ) SELECT - ad.stake_address, + 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 80ac309e..1cae1fd2 100644 --- a/files/grest/rpc/pool/pool_delegators_history.sql +++ b/files/grest/rpc/pool/pool_delegators_history.sql @@ -1,6 +1,6 @@ CREATE OR REPLACE FUNCTION grest.pool_delegators_history(_pool_bech32 text, _epoch_no word31type DEFAULT NULL) RETURNS TABLE ( - stake_address character varying, + stake_address varchar, amount text, epoch_no word31type ) @@ -10,36 +10,22 @@ AS $$ DECLARE _pool_id bigint; BEGIN - SELECT id INTO _pool_id FROM pool_hash WHERE pool_hash.view = _pool_bech32; - IF _epoch_no IS NULL THEN - RETURN QUERY - SELECT - sa.view, - 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 - sa.view, - 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; + SELECT id INTO _pool_id FROM pool_hash WHERE pool_hash.hash_raw = DECODE(b32_decode(_pool_bech32),'hex'); + 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_history.sql b/files/grest/rpc/pool/pool_history.sql index c3c1b8fb..92abc291 100644 --- a/files/grest/rpc/pool/pool_history.sql +++ b/files/grest/rpc/pool/pool_history.sql @@ -4,8 +4,8 @@ RETURNS TABLE ( active_stake text, active_stake_pct numeric, saturation_pct numeric, - block_cnt numeric, - delegator_cnt numeric, + block_cnt bigint, + delegator_cnt bigint, margin double precision, fixed_cost text, pool_fees text, @@ -39,7 +39,7 @@ BEGIN member_rewards::text, COALESCE(epoch_ros, 0) FROM grest.pool_history_cache AS phc - WHERE phc.pool_id = _pool_bech32 + WHERE phc.pool_id = (SELECT id FROM pool_hash AS ph WHERE ph.hash_raw = DECODE(b32_decode(_pool_bech32),'hex')) AND phc.epoch_no < (_curr_epoch - 2) -- temporary condition for testing, until cache table population fixed, then can be removed UNION SELECT diff --git a/files/grest/rpc/pool/pool_info.sql b/files/grest/rpc/pool/pool_info.sql index c5ac76fd..7f6ba7e3 100644 --- a/files/grest/rpc/pool/pool_info.sql +++ b/files/grest/rpc/pool/pool_info.sql @@ -1,6 +1,6 @@ CREATE OR REPLACE FUNCTION grest.pool_info(_pool_bech32_ids text []) RETURNS TABLE ( - pool_id_bech32 character varying, + pool_id_bech32 varchar, pool_id_hex text, active_epoch_no bigint, vrf_key_hash text, @@ -8,10 +8,10 @@ RETURNS TABLE ( fixed_cost text, pledge text, deposit text, - reward_addr character varying, - owners character varying [], + reward_addr varchar, + owners varchar [], relays jsonb [], - meta_url character varying, + meta_url varchar, meta_hash text, meta_json jsonb, pool_status text, @@ -49,17 +49,19 @@ BEGIN pic.pool_status, pic.retiring_epoch, pic.meta_id, - ph.view, + b32_encode('pool', ph.hash_raw::text) AS pool_id_bech32, ph.hash_raw FROM grest.pool_info_cache AS pic INNER JOIN public.pool_hash AS ph ON ph.id = pic.pool_hash_id - WHERE ph.view = ANY(SELECT UNNEST(_pool_bech32_ids)) + WHERE ph.hash_raw = ANY( + SELECT DECODE(b32_decode(p),'hex') + FROM UNNEST(_pool_bech32_ids) AS p) ORDER BY pic.pool_hash_id, pic.tx_id DESC ) SELECT - ph.view AS 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, @@ -67,9 +69,9 @@ BEGIN pu.fixed_cost::text, pu.pledge::text, pu.deposit::text, - sa.view AS reward_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS reward_addr, ARRAY( - SELECT sa.view + 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 @@ -122,7 +124,7 @@ BEGIN LEFT JOIN LATERAL( SELECT amount::lovelace AS as_sum FROM grest.pool_active_stake_cache AS pasc - WHERE pasc.pool_id = api.view + WHERE pasc.pool_id = api.pool_hash_id AND pasc.epoch_no = _epoch_no ) AS active_stake ON TRUE LEFT JOIN LATERAL( @@ -147,8 +149,8 @@ BEGIN THEN NULL ELSE SUM(CASE - WHEN pool_delegs.stake_address IN ( - SELECT sa.view + WHEN DECODE(b32_decode(pool_delegs.stake_address), 'hex') IN ( + SELECT sa.hash_raw 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 @@ -156,7 +158,7 @@ BEGIN ELSE 0 END)::lovelace END AS pledge - FROM grest.pool_delegators_list(api.view) AS pool_delegs + FROM grest.pool_delegators_list(api.pool_id_bech32) AS pool_delegs ) AS live ON TRUE; END; $$; diff --git a/files/grest/rpc/pool/pool_list.sql b/files/grest/rpc/pool/pool_list.sql index 94a15b39..9d267681 100644 --- a/files/grest/rpc/pool/pool_list.sql +++ b/files/grest/rpc/pool/pool_list.sql @@ -1,17 +1,17 @@ CREATE OR REPLACE FUNCTION grest.pool_list() RETURNS TABLE ( - pool_id_bech32 character varying, + pool_id_bech32 varchar, pool_id_hex text, active_epoch_no bigint, margin double precision, fixed_cost text, pledge text, deposit text, - reward_addr character varying, - owners character varying [], + reward_addr varchar, + owners varchar [], relays jsonb [], - ticker character varying, - meta_url character varying, + ticker varchar, + meta_url varchar, meta_hash text, pool_status text, retiring_epoch word31type @@ -19,16 +19,16 @@ RETURNS TABLE ( LANGUAGE sql STABLE AS $$ SELECT DISTINCT ON (pic.pool_hash_id) - ph.view 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, - sa.view AS reward_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS reward_addr, ARRAY( - SELECT sa.view + 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 e2fcf732..d8adc4fc 100644 --- a/files/grest/rpc/pool/pool_metadata.sql +++ b/files/grest/rpc/pool/pool_metadata.sql @@ -1,7 +1,7 @@ CREATE OR REPLACE FUNCTION grest.pool_metadata(_pool_bech32_ids text [] DEFAULT null) RETURNS TABLE ( - pool_id_bech32 character varying, - meta_url character varying, + pool_id_bech32 varchar, + meta_url varchar, meta_hash text, meta_json jsonb ) @@ -10,8 +10,8 @@ AS $$ #variable_conflict use_column BEGIN RETURN QUERY - SELECT DISTINCT ON (ph.view) - ph.view AS pool_id_bech32, + SELECT DISTINCT ON (ph.id) + 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 @@ -21,10 +21,11 @@ BEGIN WHERE CASE WHEN _pool_bech32_ids IS NULL THEN TRUE - WHEN _pool_bech32_ids IS NOT NULL THEN ph.view = ANY(SELECT UNNEST(_pool_bech32_ids)) + WHEN _pool_bech32_ids IS NOT NULL THEN ph.hash_raw = ANY( + SELECT DECODE(b32_decode(p),'hex') + FROM UNNEST(_pool_bech32_ids) AS p) END - ORDER BY - ph.view, + ORDER BY ph.id, pmr.registered_tx_id DESC; END; $$; diff --git a/files/grest/rpc/pool/pool_registrations.sql b/files/grest/rpc/pool/pool_registrations.sql index dcff9f99..9f974300 100644 --- a/files/grest/rpc/pool/pool_registrations.sql +++ b/files/grest/rpc/pool/pool_registrations.sql @@ -11,7 +11,7 @@ RETURNS TABLE ( LANGUAGE sql STABLE AS $$ SELECT - ph.view, + b32_encode('pool', ph.hash_raw::text), ENCODE(tx.hash,'hex'), ENCODE(b.hash,'hex'), b.block_no, diff --git a/files/grest/rpc/pool/pool_relays.sql b/files/grest/rpc/pool/pool_relays.sql index e821bb9a..90c8c6f7 100644 --- a/files/grest/rpc/pool/pool_relays.sql +++ b/files/grest/rpc/pool/pool_relays.sql @@ -1,12 +1,12 @@ CREATE OR REPLACE FUNCTION grest.pool_relays() RETURNS TABLE ( - pool_id_bech32 character varying, + pool_id_bech32 varchar, relays jsonb ) LANGUAGE sql STABLE AS $$ - SELECT DISTINCT ON (ph.view) - ph.view AS pool_id_bech32, + SELECT DISTINCT ON (ph.id) + b32_encode('pool', ph.hash_raw::text)::varchar AS pool_id_bech32, JSONB_AGG(JSONB_BUILD_OBJECT ( 'ipv4', pr.ipv4, 'ipv6', pr.ipv6, @@ -17,9 +17,9 @@ AS $$ FROM public.pool_hash AS ph LEFT JOIN public.pool_update AS pu ON pu.hash_id = ph.id LEFT JOIN public.pool_relay AS pr ON pu.id = pr.update_id - GROUP BY ph.view,pu.registered_tx_id + GROUP BY ph.id, ph.hash_raw,pu.registered_tx_id ORDER BY - ph.view, + ph.id, pu.registered_tx_id DESC ; $$; diff --git a/files/grest/rpc/pool/pool_retirements.sql b/files/grest/rpc/pool/pool_retirements.sql index ea9a9b5a..67c3bb9c 100644 --- a/files/grest/rpc/pool/pool_retirements.sql +++ b/files/grest/rpc/pool/pool_retirements.sql @@ -11,7 +11,7 @@ RETURNS TABLE ( LANGUAGE sql STABLE AS $$ SELECT - ph.view, + b32_encode('pool', ph.hash_raw::text), ENCODE(tx.hash,'hex'), ENCODE(b.hash,'hex'), b.block_no, diff --git a/files/grest/rpc/pool/pool_stake_snapshot.sql b/files/grest/rpc/pool/pool_stake_snapshot.sql index 73a22f21..a94cbb4c 100644 --- a/files/grest/rpc/pool/pool_stake_snapshot.sql +++ b/files/grest/rpc/pool/pool_stake_snapshot.sql @@ -34,8 +34,7 @@ BEGIN grest.pool_active_stake_cache AS pasc INNER JOIN grest.epoch_active_stake_cache AS easc ON easc.epoch_no = pasc.epoch_no LEFT JOIN grest.epoch_info_cache AS eic ON eic.epoch_no = pasc.epoch_no - WHERE - pasc.pool_id = _pool_bech32 + WHERE pasc.pool_id = (SELECT id FROM pool_hash AS ph WHERE ph.hash_raw = DECODE(b32_decode(_pool_bech32),'hex')) AND pasc.epoch_no BETWEEN _go AND _mark ORDER BY pasc.epoch_no; diff --git a/files/grest/rpc/pool/pool_updates.sql b/files/grest/rpc/pool/pool_updates.sql index 3fe1f631..19cafd3e 100644 --- a/files/grest/rpc/pool/pool_updates.sql +++ b/files/grest/rpc/pool/pool_updates.sql @@ -2,17 +2,17 @@ CREATE OR REPLACE FUNCTION grest.pool_updates(_pool_bech32 text DEFAULT NULL) RETURNS TABLE ( tx_hash text, block_time integer, - pool_id_bech32 character varying, + pool_id_bech32 varchar, pool_id_hex text, active_epoch_no bigint, vrf_key_hash text, margin double precision, fixed_cost text, pledge text, - reward_addr character varying, + reward_addr varchar, owners jsonb, relays jsonb, - meta_url character varying, + meta_url varchar, meta_hash text, meta_json jsonb, update_type text, @@ -30,15 +30,15 @@ BEGIN SELECT ENCODE(tx.hash::bytea, 'hex') AS tx_hash, EXTRACT(EPOCH FROM b.time)::integer AS block_time, - ph.view 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, - sa.view AS reward_addr, - JSONB_AGG(po.view) AS owners, + 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, 'ipv6', pr.ipv6, @@ -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 @@ -65,13 +65,13 @@ BEGIN LEFT JOIN public.pool_metadata_ref AS pmr ON pu.meta_id = pmr.id LEFT JOIN public.off_chain_pool_data AS ocpd ON pu.meta_id = ocpd.pmr_id WHERE _pool_bech32 IS NULL - OR ph.view = _pool_bech32 - GROUP BY tx.hash, b.time, ph.view, 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), + 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.hash_raw, pmr.url, pmr.hash, ocpd.json), pool_dereg AS ( SELECT ENCODE(tx.hash::bytea, 'hex') AS tx_hash, EXTRACT(EPOCH FROM b.time)::integer AS block_time, - ph.view AS pool_id_bech32, + b32_encode('pool', ph.hash_raw::text) AS pool_id_bech32, ENCODE(ph.hash_raw::bytea, 'hex') AS pool_id_hex, NULL::bigint AS active_epoch_no, NULL AS vrf_key_hash, @@ -91,7 +91,7 @@ BEGIN INNER JOIN public.tx ON tx.id = pr.announced_tx_id INNER JOIN public.block AS b ON b.id = tx.block_id WHERE _pool_bech32 IS NULL - OR ph.view = _pool_bech32) + OR ph.hash_raw = DECODE(b32_decode(_pool_bech32),'hex')) SELECT * FROM pool_reg UNION SELECT * FROM pool_dereg ORDER BY diff --git a/files/grest/rpc/pool/pool_votes.sql b/files/grest/rpc/pool/pool_votes.sql index d61e1c50..b8e8f321 100644 --- a/files/grest/rpc/pool/pool_votes.sql +++ b/files/grest/rpc/pool/pool_votes.sql @@ -25,7 +25,7 @@ AS $$ INNER JOIN public.tx vote_tx on vp.tx_id = vote_tx.id INNER JOIN public.block AS b ON vote_tx.block_id = b.id LEFT JOIN public.voting_anchor AS va ON vp.voting_anchor_id = va.id - WHERE ph.view = _pool_bech32 + WHERE ph.hash_raw = DECODE(b32_decode(_pool_bech32),'hex') ORDER BY vote_tx.id DESC; $$; diff --git a/files/grest/rpc/script/script_utxos.sql b/files/grest/rpc/script/script_utxos.sql index 68419423..d5120bb3 100644 --- a/files/grest/rpc/script/script_utxos.sql +++ b/files/grest/rpc/script/script_utxos.sql @@ -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, diff --git a/files/grest/rpc/transactions/tx_info.sql b/files/grest/rpc/transactions/tx_info.sql index 8161675b..7b5707f7 100644 --- a/files/grest/rpc/transactions/tx_info.sql +++ b/files/grest/rpc/transactions/tx_info.sql @@ -90,7 +90,7 @@ BEGIN collateral_tx_in.tx_in_id AS tx_id, tx_out.address AS payment_addr_bech32, ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred, - sa.view AS stake_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr, ENCODE(tx.hash, 'hex') AS tx_hash, tx_out.index AS tx_index, tx_out.value::text AS value, @@ -145,7 +145,7 @@ BEGIN reference_tx_in.tx_in_id AS tx_id, tx_out.address AS payment_addr_bech32, ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred, - sa.view AS stake_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr, ENCODE(tx.hash, 'hex') AS tx_hash, tx_out.index AS tx_index, tx_out.value::text AS value, @@ -200,7 +200,7 @@ BEGIN tx_out.consumed_by_tx_id AS tx_id, tx_out.address AS payment_addr_bech32, ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred, - sa.view AS stake_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr, ENCODE(tx.hash, 'hex') AS tx_hash, tx_out.index AS tx_index, tx_out.value::text AS value, @@ -252,7 +252,7 @@ BEGIN tx_out.tx_id, tx_out.address AS payment_addr_bech32, ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred, - sa.view AS stake_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr, ENCODE(tx.hash, 'hex') AS tx_hash, tx_out.index AS tx_index, tx_out.value::text AS value, @@ -292,7 +292,7 @@ BEGIN tx_out.tx_id, tx_out.address AS payment_addr_bech32, ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred, - sa.view AS stake_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr, ENCODE(tx.hash, 'hex') AS tx_hash, tx_out.index AS tx_index, tx_out.value::text AS value, @@ -347,7 +347,7 @@ BEGIN w.tx_id, JSONB_BUILD_OBJECT( 'amount', w.amount::text, - 'stake_addr', sa.view + 'stake_addr', grest.cip5_hex_to_stake_addr(sa.hash_raw) ) AS data FROM withdrawal AS w INNER JOIN stake_address AS sa ON w.addr_id = sa.id @@ -404,7 +404,7 @@ BEGIN 'index', sr.cert_index, 'type', 'stake_registration', 'info', JSONB_BUILD_OBJECT( - 'stake_address', sa.view, + 'stake_address', grest.cip5_hex_to_stake_addr(sa.hash_raw), 'deposit', sr.deposit::text ) ) AS data @@ -421,7 +421,7 @@ BEGIN 'index', sd.cert_index, 'type', 'stake_deregistration', 'info', JSONB_BUILD_OBJECT( - 'stake_address', sa.view + 'stake_address', grest.cip5_hex_to_stake_addr(sa.hash_raw) ) ) AS data FROM public.stake_deregistration AS sd @@ -437,8 +437,8 @@ BEGIN 'index', d.cert_index, 'type', 'pool_delegation', 'info', JSONB_BUILD_OBJECT( - 'stake_address', sa.view, - 'pool_id_bech32', ph.view, + 'stake_address', grest.cip5_hex_to_stake_addr(sa.hash_raw), + 'pool_id_bech32', b32_encode('pool', ph.hash_raw::text), 'pool_id_hex', ENCODE(ph.hash_raw, 'hex') ) ) AS data @@ -456,7 +456,7 @@ BEGIN 'index', t.cert_index, 'type', 'treasury_MIR', 'info', JSONB_BUILD_OBJECT( - 'stake_address', sa.view, + 'stake_address', grest.cip5_hex_to_stake_addr(sa.hash_raw), 'amount', t.amount::text ) ) AS data @@ -473,7 +473,7 @@ BEGIN 'index', r.cert_index, 'type', 'reserve_MIR', 'info', JSONB_BUILD_OBJECT( - 'stake_address', sa.view, + 'stake_address', grest.cip5_hex_to_stake_addr(sa.hash_raw), 'amount', r.amount::text ) ) AS data @@ -521,7 +521,7 @@ BEGIN 'index', pr.cert_index, 'type', 'pool_retire', 'info', JSONB_BUILD_OBJECT( - 'pool_id_bech32', ph.view, + 'pool_id_bech32', b32_encode('pool', ph.hash_raw::text), 'pool_id_hex', ENCODE(ph.hash_raw, 'hex'), 'retiring epoch', pr.retiring_epoch ) @@ -539,15 +539,15 @@ BEGIN 'index', pu.cert_index, 'type', 'pool_update', 'info', JSONB_BUILD_OBJECT( - 'pool_id_bech32', ph.view, + 'pool_id_bech32', b32_encode('pool', ph.hash_raw::text), 'pool_id_hex', ENCODE(ph.hash_raw, 'hex'), 'active_epoch_no', pu.active_epoch_no, 'vrf_key_hash', ENCODE(pu.vrf_key_hash, 'hex'), 'margin', pu.margin, 'fixed_cost', pu.fixed_cost::text, 'pledge', pu.pledge::text, - 'reward_addr', sa.view, - 'owners', JSONB_AGG(po.view), + 'reward_addr', grest.cip5_hex_to_stake_addr(sa.hash_raw), + 'owners', JSONB_AGG(po.stake_address), 'relays', JSONB_AGG(JSONB_BUILD_OBJECT ( 'ipv4', pr.ipv4, 'ipv6', pr.ipv6, @@ -563,7 +563,7 @@ BEGIN LEFT JOIN public.pool_hash AS ph ON pu.hash_id = ph.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 @@ -571,7 +571,7 @@ BEGIN LEFT JOIN public.pool_metadata_ref AS pmr ON pu.meta_id = pmr.id WHERE _certs IS TRUE AND pu.registered_tx_id = ANY(_tx_id_list) - GROUP BY pu.registered_tx_id, pu.cert_index, ph.view, ph.hash_raw, pu.active_epoch_no, pu.vrf_key_hash, pu.margin, pu.fixed_cost, pu.pledge, sa.view, pmr.url, pmr.hash + GROUP BY pu.registered_tx_id, pu.cert_index, 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 -- UNION ALL -- @@ -581,8 +581,8 @@ BEGIN 'index', dv.cert_index, 'type', 'vote_delegation', 'info', JSONB_BUILD_OBJECT( - 'stake_address', sa.view, - 'drep_id', dh.view, + 'stake_address', grest.cip5_hex_to_stake_addr(sa.hash_raw), + 'drep_id', COALESCE(grest.cip129_hex_to_drep_id(dh.raw, dh.has_script), dh.view::text), 'drep_hex', ENCODE(dh.raw, 'hex') ) ) AS data @@ -600,7 +600,7 @@ BEGIN 'index', dr.cert_index, 'type', 'drep_registration', 'info', JSONB_BUILD_OBJECT( - 'drep_id', dh.view, + 'drep_id', grest.cip129_hex_to_drep_id(dh.raw, dh.has_script), 'drep_hex', ENCODE(dh.raw, 'hex'), 'deposit', dr.deposit::text, 'meta_url', va.url, @@ -623,7 +623,7 @@ BEGIN 'index', dr.cert_index, 'type', 'drep_update', 'info', JSONB_BUILD_OBJECT( - 'drep_id', dh.view, + 'drep_id', grest.cip129_hex_to_drep_id(dh.raw, dh.has_script), 'drep_hex', ENCODE(dh.raw, 'hex'), 'meta_url', va.url, 'meta_hash', va.data_hash @@ -644,7 +644,7 @@ BEGIN 'index', dr.cert_index, 'type', 'drep_retire', 'info', JSONB_BUILD_OBJECT( - 'drep_id', dh.view, + 'drep_id', grest.cip129_hex_to_drep_id(dh.raw, dh.has_script), 'drep_hex', ENCODE(dh.raw, 'hex') ) ) AS data @@ -848,7 +848,7 @@ BEGIN 'proposal_tx_hash', ENCODE(tx.hash, 'hex'), 'proposal_index', gap.index, 'voter_role', vp.voter_role, - 'voter', COALESCE(ENCODE(ch.raw, 'hex'), dh.view, ph.view), + 'voter', COALESCE(grest.cip129_hex_to_drep_id(dh.raw, dh.has_script), dh.view::text, grest.cip129_hex_to_cc_hot(ch.raw, ch.has_script), b32_encode('pool', ph.hash_raw::text)), 'voter_hex', COALESCE(ENCODE(ch.raw, 'hex'), ENCODE(dh.raw, 'hex'), ENCODE(ph.hash_raw, 'hex')), 'vote', vp.vote ) AS data @@ -876,7 +876,7 @@ BEGIN 'type', gap.type, 'description', gap.description, 'deposit', gap.deposit::text, - 'return_address', sa.view, + 'return_address', grest.cip5_hex_to_stake_addr(sa.hash_raw), 'expiration', gap.expiration, 'meta_url', va.url, 'meta_hash', ENCODE(va.data_hash, 'hex'), @@ -885,7 +885,7 @@ BEGIN ELSE JSONB_BUILD_OBJECT( 'stake_address', ( - SELECT sa2.view + SELECT grest.cip5_hex_to_stake_addr(sa2.hash_raw) FROM stake_address AS sa2 WHERE sa2.id = tw.stake_address_id ), diff --git a/files/grest/rpc/transactions/tx_utxos.sql b/files/grest/rpc/transactions/tx_utxos.sql index 221b3169..6ac76a61 100644 --- a/files/grest/rpc/transactions/tx_utxos.sql +++ b/files/grest/rpc/transactions/tx_utxos.sql @@ -41,7 +41,7 @@ BEGIN tx_out.consumed_by_tx_id AS tx_id, tx_out.address AS payment_addr_bech32, ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred, - sa.view AS stake_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr, ENCODE(tx.hash, 'hex') AS tx_hash, tx_out.index AS tx_index, tx_out.value::text AS value, @@ -70,7 +70,7 @@ BEGIN tx_out.tx_id, tx_out.address AS payment_addr_bech32, ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred, - sa.view AS stake_addr, + grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr, ENCODE(tx.hash, 'hex') AS tx_hash, tx_out.index AS tx_index, tx_out.value::text AS value, diff --git a/files/grest/rpc/transactions/utxo_info.sql b/files/grest/rpc/transactions/utxo_info.sql index 53b65301..b1129cc1 100644 --- a/files/grest/rpc/transactions/utxo_info.sql +++ b/files/grest/rpc/transactions/utxo_info.sql @@ -66,7 +66,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, diff --git a/tests/setup-tests.sh b/tests/setup-tests.sh index 6bd86044..7dae71e9 100755 --- a/tests/setup-tests.sh +++ b/tests/setup-tests.sh @@ -1,23 +1,23 @@ #!/usr/bin/env bash -sudo apt-get install python3 python3-pip -y >/dev/null -python3 -m pip install virtualenv >/dev/null -export PATH=$PATH:~/.local/bin/ +sudo apt-get install python3 python3-pip python3-virtualenv -y >/dev/null +export PATH="${HOME}"/.local/bin/:$PATH virtualenv koios-tests >/dev/null source koios-tests/bin/activate python3 -m pip install schemathesis pytest-order >/dev/null -curl -sfL https://raw.githubusercontent.com/cardano-community/koios-artifacts/master/tests/not_empty_response.py -o not_empty_response.py >/dev/null +#curl -sfL https://raw.githubusercontent.com/cardano-community/koios-artifacts/main/tests/not_empty_response.py -o not_empty_response.py >/dev/null +export SCHEMATHESIS_HOOKS=not_empty_response cat <<-EOF To run the endpoint validation tests, use the below: - schemathesis --pre-run not_empty_response run --request-timeout 5000 https://guild.koios.rest/koiosapi.yaml --hypothesis-phases=explicit \\ - --hypothesis-verbosity quiet -b http://127.0.0.1:8053/api/v1 -c all --validate-schema=true -H "Content-Type: application/json" + schemathesis run --request-timeout 25000 ../specs/results/koiosapi-guild.yaml --hypothesis-phases=explicit --hypothesis-verbosity quiet \\ + -b http://127.0.0.1:8053/api/v1 -c all --validate-schema=true -H "Content-Type: application/json" --experimental=openapi-3.1 --exclude-checks ignored_auths where http://127.0.0.1:8053/api/v1 is the URL of instance you want to test, and guild.koios.rest is the target enviornment for testing. - To run the data validations tests, use the below: + To run the data validations tests, use the below (WIP - skip for now): pytest --local-url http://127.0.0.1:8053/api/v1 --compare-url https://guild.koios.rest/api/v1 --api-schema-file ../specs/results/koiosapi-guild.yaml -x -v