Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion files/grest/cron/jobs/populate-next-epoch-nonce.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export CARDANO_NODE_SOCKET_PATH=
echo "$(date +%F_%H:%M:%S) Running next epoch nonce calculation..."

# TODO possibly initialize EPOCH_LENGTH from database as well
PROTO_MAJ=$(psql ${DB_NAME} -c "select protocol_major from epoch_param where epoch_no = (select max(no) from epoch);" -t)
PROTO_MAJ=$(psql ${DB_NAME} -c "select protocol_major from epoch_param where epoch_no = (select max(epoch_no) from public.epoch_param);" -t)
SECURITY_PARAM=$(psql ${DB_NAME} -c "select securityparam from grest.genesis;" -t)
ACTIVE_SLOT_COEFF=$(psql ${DB_NAME} -c "select activeslotcoeff from grest.genesis;" -t)
WINDOW_SIZE=2
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/000_utilities/is_dangling_delegation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DECLARE

BEGIN

SELECT INTO curr_epoch MAX(no) FROM epoch;
SELECT INTO curr_epoch MAX(epoch_param.epoch_no) FROM public.epoch_param;
-- revised logic:
-- check for any pool retirement record exists for the pool corresponding to given delegation
-- pool retiring epoch is current or in the past (future scheduled retirements don't count)
Expand Down
6 changes: 3 additions & 3 deletions files/grest/rpc/00_cached_tables/epoch_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ BEGIN
RAISE EXCEPTION 'Previous epoch_info_cache_update query still running but should have completed! Exiting...';
END IF;

SELECT MAX(no) INTO _curr_epoch
FROM public.epoch;
SELECT MAX(epoch_no) INTO _curr_epoch
FROM public.epoch_param;

IF _epoch_no_to_insert_from IS NULL THEN
SELECT COALESCE(MAX(epoch_no), 0) INTO _latest_epoch_no_in_cache
Expand All @@ -47,7 +47,7 @@ BEGIN

IF _latest_epoch_no_in_cache = 0 THEN
RAISE NOTICE 'Epoch info cache table is empty, starting initial population...';
PERFORM grest.epoch_summary_corrections_update();
-- commenting out for now, epoch is now view and hopefully bugs have been fixed: PERFORM grest.epoch_summary_corrections_update();
PERFORM grest.epoch_info_cache_update(0);
RETURN;
END IF;
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/00_cached_tables/pool_history_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ BEGIN
END IF;

SELECT COALESCE(MAX(epoch_no), 0) INTO _latest_epoch_no_in_cache FROM grest.pool_history_cache;
SELECT MAX(no) INTO _curr_epoch FROM epoch;
SELECT MAX(epoch_param.epoch_no) INTO _curr_epoch FROM public.epoch_param;

IF _epoch_no_to_insert_from IS NULL THEN
IF _latest_epoch_no_in_cache = 0 THEN
Expand Down
32 changes: 28 additions & 4 deletions files/grest/rpc/00_cached_tables/pool_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ DECLARE
_current_epoch_no word31type;
_pool_info_cache_last_block_height bigint;
_pool_info_cache_last_tx_id bigint;
_updated_count bigint;
BEGIN
SELECT MAX(block_no) FROM public.block
WHERE block_no IS NOT NULL INTO _current_block_height;
SELECT MAX(no) FROM public.epoch into _current_epoch_no;
SELECT MAX(epoch_param.epoch_no) FROM public.epoch_param into _current_epoch_no;
SELECT COALESCE(last_value::bigint, 0) INTO _pool_info_cache_last_block_height FROM grest.control_table
WHERE key = 'pool_info_cache_last_block_height';
SELECT COALESCE(MAX(tx.id), 0) INTO _pool_info_cache_last_tx_id
Expand Down Expand Up @@ -150,6 +151,29 @@ BEGIN
pool_status = EXCLUDED.pool_status,
retiring_epoch = EXCLUDED.retiring_epoch;

-- following is needed because simple retirement cert submission doesn't seem to generate new pool_update row in latest db syncs
WITH latest_scheduled_retirements AS (
SELECT DISTINCT ON (pr.hash_id)
pr.hash_id,
pr.retiring_epoch
FROM public.pool_retire AS pr
WHERE pr.announced_tx_id > _pool_info_cache_last_tx_id
AND pr.retiring_epoch > _current_epoch_no -- this is needed to cater for cache rebuilds
-- only looking at pool_retirement requests not followed by update or that are part of update
AND NOT EXISTS
( SELECT null
FROM public.pool_update pu
WHERE pu.hash_id = pr.hash_id AND pu.registered_tx_id >= pr.announced_tx_id )
ORDER BY hash_id, announced_tx_id DESC, cert_index DESC
)
UPDATE grest.pool_info_cache
SET pool_status = 'retiring', retiring_epoch = latest_scheduled_retirements.retiring_epoch
FROM latest_scheduled_retirements
WHERE grest.pool_info_cache.pool_hash_id = latest_scheduled_retirements.hash_id;

GET DIAGNOSTICS _updated_count = ROW_COUNT;
RAISE NOTICE 'Updated % records for scheduled retirement', _updated_count;

INSERT INTO grest.control_table (key, last_value)
VALUES (
'pool_info_cache_last_block_height',
Expand Down Expand Up @@ -222,17 +246,17 @@ BEGIN
UPDATE grest.pool_info_cache
SET pool_status = 'retired'
WHERE pool_status = 'retiring'
AND retiring_epoch <= new.no;
AND retiring_epoch <= new.epoch_no;
RETURN NULL;
END;
$$;

COMMENT ON FUNCTION grest.pool_info_retire_status IS 'Internal function to update pool_info_cache with new retire status based ON epoch switch'; -- noqa: LT01

DROP TRIGGER IF EXISTS pool_info_retire_status_trigger ON public.epoch;
DROP TRIGGER IF EXISTS pool_info_retire_status_trigger ON public.epoch_state;

CREATE TRIGGER pool_info_retire_status_trigger
AFTER INSERT ON public.epoch
AFTER INSERT ON public.epoch_state
FOR EACH ROW EXECUTE FUNCTION grest.pool_info_retire_status();


Expand Down
6 changes: 3 additions & 3 deletions files/grest/rpc/00_cached_tables/stake_distribution_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BEGIN
SELECT (eic.i_last_tx_id) INTO _last_account_tx_id
FROM grest.epoch_info_cache AS eic
WHERE eic.epoch_no = _active_stake_epoch;
SELECT MAX(no) INTO _latest_epoch FROM public.epoch WHERE no IS NOT NULL;
SELECT MAX(epoch_param.epoch_no) INTO _latest_epoch FROM public.epoch_param WHERE epoch_param.epoch_no IS NOT NULL;

WITH
dangling_delegations AS (
Expand Down Expand Up @@ -316,14 +316,14 @@ BEGIN
FROM grest.control_table
WHERE key = 'last_active_stake_validated_epoch'
) OR (
SELECT ((SELECT MAX(no) FROM epoch) - COALESCE((last_value::integer - 2)::integer, 0 )) > 2
SELECT ((SELECT MAX(epoch_param.epoch_no) FROM public.epoch_param) - COALESCE((last_value::integer - 2)::integer, 0 )) > 2
FROM grest.control_table
WHERE key = 'last_active_stake_validated_epoch'
) THEN
RAISE EXCEPTION 'Active Stake cache too far, skipping...';
ELSIF (
SELECT
((SELECT MAX(no) FROM epoch) - (SELECT MAX(epoch_no)::integer FROM grest.epoch_info_cache))::integer > 0
((SELECT MAX(epoch_param.epoch_no) FROM public.epoch_param) - (SELECT MAX(epoch_no)::integer FROM grest.epoch_info_cache))::integer > 0
) THEN
RAISE EXCEPTION 'Epoch Info cache wasnt run yet, skipping...';
END IF;
Expand Down
3 changes: 3 additions & 0 deletions files/grest/rpc/02_indexes/13_7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE INDEX IF NOT EXISTS idx_voting_procedure_drep_voter ON voting_procedure (drep_voter) WHERE drep_voter IS NOT NULL;

CREATE INDEX IF NOT EXISTS delegation_vote_addr_id_idx ON public.delegation_vote (addr_id, tx_id);
16 changes: 8 additions & 8 deletions files/grest/rpc/account/account_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ BEGIN
FROM reward
WHERE reward.addr_id = ANY(sa_id_list)
AND reward.spendable_epoch <= (
SELECT MAX(no)
FROM epoch
SELECT MAX(epoch_param.epoch_no)
FROM public.epoch_param
)
GROUP BY
reward.addr_id
Expand All @@ -165,8 +165,8 @@ BEGIN
WHERE r.addr_id = ANY(sa_id_list)
AND r.type = 'reserves'
AND r.spendable_epoch <= (
SELECT MAX(no)
FROM epoch
SELECT MAX(epoch_param.epoch_no)
FROM public.epoch_param
)
GROUP BY
r.addr_id
Expand All @@ -179,8 +179,8 @@ BEGIN
WHERE t.addr_id = ANY(sa_id_list)
AND t.type = 'treasury'
AND t.spendable_epoch <= (
SELECT MAX(no)
FROM epoch
SELECT MAX(epoch_param.epoch_no)
FROM public.epoch_param
)
GROUP BY
t.addr_id
Expand All @@ -193,8 +193,8 @@ BEGIN
WHERE pr.addr_id = ANY(sa_id_list)
AND pr.type = 'proposal_refund'
AND pr.spendable_epoch <= (
SELECT MAX(no)
FROM epoch
SELECT MAX(epoch_param.epoch_no)
FROM public.epoch_param
)
GROUP BY
pr.addr_id
Expand Down
12 changes: 6 additions & 6 deletions files/grest/rpc/account/account_info_cached.sql
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ BEGIN
WHERE r.addr_id = ANY(sa_id_list)
AND r.type = 'reserves'
AND r.spendable_epoch <= (
SELECT MAX(no)
FROM epoch
SELECT MAX(epoch_param.epoch_no)
FROM public.epoch_param
)
GROUP BY
r.addr_id
Expand All @@ -129,8 +129,8 @@ BEGIN
WHERE t.addr_id = ANY(sa_id_list)
AND t.type = 'treasury'
AND t.spendable_epoch <= (
SELECT MAX(no)
FROM epoch
SELECT MAX(epoch_param.epoch_no)
FROM public.epoch_param
)
GROUP BY
t.addr_id
Expand All @@ -143,8 +143,8 @@ BEGIN
WHERE pr.addr_id = ANY(sa_id_list)
AND pr.type = 'treasury'
AND pr.spendable_epoch <= (
SELECT MAX(no)
FROM epoch
SELECT MAX(epoch_param.epoch_no)
FROM public.epoch_param
)
GROUP BY
pr.addr_id
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/epoch/epoch_block_protocols.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ BEGIN
FROM public.block AS b
LEFT JOIN public.epoch_param AS ep ON ep.epoch_no = b.epoch_no
LEFT JOIN grest.era_map AS em ON ep.protocol_major::text = em.protocol_major::text AND ep.protocol_minor::text = em.protocol_minor::text
WHERE b.epoch_no = (SELECT MAX(no) FROM epoch)
WHERE b.epoch_no = (SELECT MAX(epoch_param.epoch_no) FROM public.epoch_param)
GROUP BY
b.proto_major, b.proto_minor, em.era;
END IF;
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/epoch/epoch_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ BEGIN
LEFT JOIN grest.epoch_active_stake_cache AS eas ON eas.epoch_no = ei.epoch_no
WHERE
CASE
WHEN _epoch_no IS NULL THEN ei.epoch_no <= (SELECT MAX(epoch.no) FROM public.epoch)
WHEN _epoch_no IS NULL THEN ei.epoch_no <= (SELECT MAX(epoch_param.epoch_no) FROM public.epoch_param)
ELSE ei.epoch_no = _epoch_no
END
AND
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/epoch/epoch_params.sql
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ BEGIN
LEFT JOIN public.cost_model AS cm ON cm.id = ep.cost_model_id
WHERE
CASE
WHEN _epoch_no IS NULL THEN ep.epoch_no <= (SELECT MAX(epoch.no) FROM public.epoch)
WHEN _epoch_no IS NULL THEN ep.epoch_no <= (SELECT MAX(epoch_param.epoch_no) FROM public.epoch_param)
ELSE ep.epoch_no = _epoch_no
END
ORDER BY
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/epoch/epoch_summary_corrections_update.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LANGUAGE plpgsql
AS $$
DECLARE
curr_epoch_record record := null;
latest_epoch bigint = (SELECT MAX(no) FROM epoch);
latest_epoch bigint = (SELECT MAX(epoch_param.epoch_no) FROM public.epoch_param);
last_epoch_checked bigint = coalesce((SELECT last_value::bigint FROM grest.control_table WHERE key = 'last_epoch_summary_data_checked'), -1);
BEGIN
RAISE NOTICE 'Last validated epoch was %', last_epoch_checked;
Expand Down
38 changes: 34 additions & 4 deletions files/grest/rpc/governance/drep_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ RETURNS TABLE (
expires_epoch_no numeric,
amount text,
meta_url varchar,
meta_hash text
meta_hash text,
live_delegator_count bigint
)
LANGUAGE plpgsql
AS $$
Expand All @@ -19,7 +20,7 @@ DECLARE
drep_activity word64type;
BEGIN

SELECT INTO curr_epoch MAX(epoch_no) FROM public.block;
SELECT INTO curr_epoch MAX(epoch_param.epoch_no) FROM public.epoch_param;

SELECT INTO drep_activity ep.drep_activity FROM public.epoch_param AS ep WHERE ep.epoch_no = curr_epoch;

Expand Down Expand Up @@ -115,6 +116,33 @@ BEGIN
) AS drep_state
INNER JOIN tx on drep_state.last_tx_id = tx.id
INNER JOIN block AS b ON tx.block_id = b.id
),

_latest_deleg AS ( -- global latest delegation per addr, computed once
SELECT DISTINCT ON (dv.addr_id)
dv.addr_id, dv.tx_id, dv.drep_hash_id
FROM public.delegation_vote AS dv
ORDER BY dv.addr_id, dv.tx_id DESC
),

_all_delegations AS (
SELECT latest.addr_id, latest.tx_id, latest.drep_hash_id
FROM _latest_deleg AS latest
-- only interested in delegations since last registration for normal
-- dreps, and all latest delegations for predefined ones
LEFT JOIN _last_registration lr ON lr.drep = latest.drep_hash_id
WHERE latest.drep_hash_id = ANY(drep_list)
AND (lr.tx_id IS NULL OR latest.tx_id >= lr.tx_id)
AND NOT EXISTS (
SELECT 1 FROM public.stake_deregistration sd
WHERE sd.addr_id = latest.addr_id AND sd.tx_id > latest.tx_id
)
),

_deleg_counts AS ( -- one row per drep, computed once
SELECT drep_hash_id, count(*) AS live_delegator_count
FROM _all_delegations
GROUP BY drep_hash_id
)

SELECT DISTINCT ON (dh.view)
Expand All @@ -132,16 +160,18 @@ BEGIN
ELSE 'registered'
END) AS drep_status,
(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 OR COALESCE(dd.active_until, 0) > curr_epoch) END) AS active,
COALESCE(starts_with(dh.view,'drep_always') OR (COALESCE(dr.deposit, 0) >= 0 AND (ds.active OR COALESCE(dd.active_until, 0) > curr_epoch)), FALSE) AS active,
(CASE WHEN COALESCE(dr.deposit, 0) >= 0 THEN GREATEST(ds.expires_epoch_no, COALESCE(dd.active_until, 0)) ELSE NULL END) AS expires_epoch_no,
COALESCE(dd.amount, 0)::text AS amount,
va.url AS meta_url,
ENCODE(va.data_hash, 'hex') AS meta_hash
ENCODE(va.data_hash, 'hex') AS meta_hash,
COALESCE(dc.live_delegator_count, 0) AS live_delegator_count
FROM public.drep_hash AS dh
LEFT 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.drep_distr AS dd ON dh.id = dd.hash_id AND dd.epoch_no = curr_epoch
LEFT JOIN _drep_state AS ds ON dh.id = ds.drep
LEFT JOIN _deleg_counts AS dc ON dc.drep_hash_id = dh.id
WHERE dh.id = ANY(drep_list)
ORDER BY
dh.view, dr.tx_id DESC
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/governance/drep_voting_power_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ AS $$
WHEN ((SELECT deposit FROM drep_registration dr WHERE dr.drep_hash_id = dh.id ORDER BY id DESC LIMIT 1) < 0)
THEN (SELECT b.epoch_no FROM block b INNER JOIN tx t ON b.id = t.block_id and t.id =
(SELECT tx_id FROM drep_registration dr WHERE dr.drep_hash_id = dh.id ORDER BY id DESC LIMIT 1))
ELSE (SELECT MAX(no) FROM epoch)
ELSE (SELECT MAX(epoch_param.epoch_no) FROM public.epoch_param)
END
)
-- previously was doing INNER JOIN of drep_distr with drep_hash but if zero voting power drep_distr not created
Expand Down
7 changes: 7 additions & 0 deletions files/grest/rpc/governance/proposal_list.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ RETURNS TABLE (
proposal_index integer,
proposal_type text,
proposal_description jsonb,
previous_gov_action_proposal_id text,
deposit text,
return_address text,
proposed_epoch integer,
Expand All @@ -32,6 +33,10 @@ AS $$
gap.index,
gap.type,
gap.description,
CASE
WHEN gap.prev_gov_action_proposal IS NULL THEN NULL
ELSE grest.cip129_to_gov_action_id(prev_tx.hash, prev_gap.index)
END AS previous_gov_action_proposal_id,
gap.deposit::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::text,
b.epoch_no,
Expand Down Expand Up @@ -68,6 +73,8 @@ AS $$
LEFT JOIN public.cost_model AS cm ON cm.id = pp.cost_model_id
LEFT JOIN public.voting_anchor AS va ON gap.voting_anchor_id = va.id
LEFT JOIN public.off_chain_vote_data AS ocvd ON va.id = ocvd.voting_anchor_id
LEFT JOIN public.gov_action_proposal AS prev_gap ON gap.prev_gov_action_proposal = prev_gap.id
LEFT JOIN public.tx AS prev_tx ON prev_gap.tx_id = prev_tx.id
ORDER BY
b.time DESC;
$$;
Expand Down
Loading
Loading