Skip to content

Commit

Permalink
Merge branch 'v1.0.11rc' into hodlonaut-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlrt committed Aug 28, 2023
2 parents 9304492 + a1e42d5 commit 99d83b9
Show file tree
Hide file tree
Showing 46 changed files with 1,130 additions and 521 deletions.
2 changes: 1 addition & 1 deletion files/grest/cron/jobs/epoch-summary-corrections-update.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
DB_NAME=cexplorer

tip=$(psql ${DB_NAME} -qbt -c "select extract(epoch from time)::integer from block order by id desc limit 1;" | xargs)
tip=$(psql ${DB_NAME} -qbt -c "SELECT EXTRACT(EPOCH FROM time)::integer FROM block ORDER BY id DESC LIMIT 1;" | xargs)

if [[ $(( $(date +%s) - tip )) -gt 300 ]]; then
echo "$(date +%F_%H:%M:%S) Skipping as database has not received a new block in past 300 seconds!" && exit 1
Expand Down
8 changes: 4 additions & 4 deletions files/grest/rpc/01_cached_tables/asset_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ BEGIN
WHERE state = 'active'
AND query ILIKE '%grest.asset_info_cache_update%'
AND datname = (SELECT current_database())
) THEN
) THEN
RAISE EXCEPTION 'Previous asset_info_cache_update query still running but should have completed! Exiting...';
END IF;

Expand All @@ -56,7 +56,7 @@ BEGIN
tx_mint_meta AS (
SELECT
mtm.ident,
MIN(mtm.tx_id) AS first_mint_tx_id,
MIN(mtm.tx_id) AS first_mint_tx_id,
MAX(mtm.tx_id) AS last_mint_tx_id
FROM ma_tx_mint AS mtm
INNER JOIN tx_metadata AS tm ON tm.tx_id = mtm.tx_id
Expand All @@ -74,7 +74,7 @@ BEGIN
tx_mint_nometa AS (
SELECT
mtm.ident,
MIN(mtm.tx_id) AS first_mint_tx_id,
MIN(mtm.tx_id) AS first_mint_tx_id,
MAX(mtm.tx_id) AS last_mint_tx_id
FROM ma_tx_mint AS mtm
LEFT JOIN tx_mint_meta ON tx_mint_meta.ident = mtm.ident
Expand Down Expand Up @@ -111,7 +111,7 @@ BEGIN
FROM tx_mint_nometa
)

INSERT INTO grest.asset_info_cache
INSERT INTO grest.asset_info_cache
SELECT
ma.id,
MIN(B.time) AS creation_time,
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/01_cached_tables/asset_registry_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ BEGIN
decimals
)
VALUES(
_asset_policy,
_asset_policy,
_asset_name,
_name,
_description,
Expand Down
4 changes: 2 additions & 2 deletions files/grest/rpc/01_cached_tables/epoch_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ BEGIN
EXTRACT(EPOCH FROM e.start_time) AS i_first_block_time,
EXTRACT(EPOCH FROM e.end_time) AS i_last_block_time,
CASE -- populated in epoch n + 2
WHEN e.no <= _curr_epoch - 2 THEN reward_pot.amount
WHEN e.no <= _curr_epoch - 2 THEN reward_pot.amount
ELSE NULL
END AS i_total_rewards,
CASE -- populated in epoch n + 2
WHEN e.no <= _curr_epoch THEN ROUND(reward_pot.amount / e.blk_count)
ELSE NULL
END AS i_avg_blk_reward,
END AS i_avg_blk_reward,
last_tx.tx_id AS i_last_tx_id,
ep.min_fee_a AS p_min_fee_a,
ep.min_fee_b AS p_min_fee_b,
Expand Down
12 changes: 6 additions & 6 deletions files/grest/rpc/01_cached_tables/pool_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ BEGIN
ORDER BY pr.id
LIMIT 1;

IF _retiring_epoch IS NULL THEN
IF _retiring_epoch IS NULL THEN
_pool_status := 'registered';
ELSIF _retiring_epoch > _current_epoch_no THEN
_pool_status := 'retiring';
Expand All @@ -69,7 +69,7 @@ BEGIN
tx_hash,
block_time,
pool_hash_id,
pool_id_bech32,
pool_id_bech32,
pool_id_hex,
active_epoch_no,
vrf_key_hash,
Expand All @@ -88,7 +88,7 @@ BEGIN
SELECT
_tx_id,
_update_id,
encode(tx.hash::bytea, 'hex'),
encode(tx.hash::bytea, 'hex'),
EXTRACT(EPOCH FROM b.time),
_hash_id,
ph.view,
Expand Down Expand Up @@ -174,7 +174,7 @@ BEGIN
ORDER BY pr.id
LIMIT 1;

IF _retiring_epoch IS NULL THEN
IF _retiring_epoch IS NULL THEN
_pool_status := 'registered';
ELSIF _retiring_epoch > _current_epoch_no THEN
_pool_status := 'retiring';
Expand Down Expand Up @@ -222,8 +222,8 @@ BEGIN
END IF;

ELSIF (tg_table_name = 'pool_relay') THEN
SELECT pic.id INTO _latest_pool_update_id
FROM grest.pool_info_cache AS pic
SELECT pic.id INTO _latest_pool_update_id
FROM grest.pool_info_cache AS pic
INNER JOIN public.pool_update AS pu ON pu.hash_id = pic.pool_hash_id AND pu.registered_tx_id = pic.tx_id
WHERE pu.id = new.update_id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ BEGIN
SELECT (_current_block_height - _last_update_block_height) INTO _last_update_block_diff;
-- Do nothing until there is a 180 blocks difference in height - 60 minutes theoretical time
-- 185 in check because last block height considered is 5 blocks behind tip

Raise NOTICE 'Last stake distribution update was % blocks ago...',
_last_update_block_diff;
IF (_last_update_block_diff >= 180
Expand Down
10 changes: 3 additions & 7 deletions files/grest/rpc/account/account_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ BEGIN
txo.address,
txo.stake_address_id,
txo.id
FROM
tx_out AS txo
LEFT JOIN tx_in ON txo.tx_id = tx_in.tx_out_id
AND txo.index::smallint = tx_in.tx_out_index::smallint
FROM tx_out AS txo
WHERE
txo.stake_address_id = ANY(sa_id_list)
AND tx_in.tx_out_id IS NULL
AND tx_out.consumed_by_tx_in_id IS NULL
) AS x
)

Expand All @@ -57,8 +54,7 @@ BEGIN
txo.address,
txo.stake_address_id,
txo.id
FROM
tx_out AS txo
FROM tx_out AS txo
WHERE
txo.stake_address_id = ANY(sa_id_list)
LIMIT (CASE WHEN _first_only IS TRUE THEN 1 ELSE NULL END)
Expand Down
6 changes: 2 additions & 4 deletions files/grest/rpc/account/account_assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ BEGIN
INNER JOIN tx_out AS txo ON txo.id = mtx.tx_out_id
INNER JOIN stake_address AS sa ON sa.id = txo.stake_address_id
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
LEFT JOIN tx_in ON txo.tx_id = tx_in.tx_out_id
AND txo.index::smallint = tx_in.tx_out_index::smallint
WHERE
sa.id = ANY(sa_id_list)
AND tx_in.tx_out_id IS NULL
AND tx_out.consumed_by_tx_in_id IS NULL
GROUP BY
sa.view, ma.policy, ma.name, ma.fingerprint, aic.decimals
)
Expand Down Expand Up @@ -61,4 +59,4 @@ BEGIN
END;
$$;

COMMENT ON FUNCTION grest.account_assets IS 'Get the native asset balance of given accounts'; -- noqa: LT01
COMMENT ON FUNCTION grest.account_assets IS 'Get the native asset balance of given accounts'; -- noqa: LT01
13 changes: 4 additions & 9 deletions files/grest/rpc/account/account_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,10 @@ BEGIN
SELECT
tx_out.stake_address_id,
COALESCE(SUM(VALUE), 0) AS utxo
FROM
tx_out
LEFT JOIN tx_in ON tx_out.tx_id = tx_in.tx_out_id
AND tx_out.index::smallint = tx_in.tx_out_index::smallint
WHERE
tx_out.stake_address_id = ANY(sa_id_list)
AND tx_in.tx_out_id IS NULL
GROUP BY
tx_out.stake_address_id
FROM tx_out
WHERE tx_out.stake_address_id = ANY(sa_id_list)
AND tx_out.consumed_by_tx_in_id IS NULL
GROUP BY tx_out.stake_address_id
) AS utxo_t ON utxo_t.stake_address_id = status_t.id
LEFT JOIN (
SELECT
Expand Down
53 changes: 53 additions & 0 deletions files/grest/rpc/account/account_txs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
CREATE OR REPLACE FUNCTION grest.account_txs(_stake_address text, _after_block_height integer DEFAULT 0)
RETURNS TABLE (
tx_hash text,
epoch_no word31type,
block_height word31type,
block_time integer
)
LANGUAGE plpgsql
AS $$
DECLARE
_tx_id_min bigint;
_tx_id_list bigint[];
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;

-- 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)
AND tx_id >= _tx_id_min
--
UNION
--
SELECT tx_in_id AS tx_id
FROM tx_out
LEFT JOIN tx_in ON tx_out.tx_id = tx_in.tx_out_id
AND tx_out.index = tx_in.tx_out_index
WHERE
tx_in.tx_in_id IS NOT NULL
AND tx_out.stake_address_id = ANY(SELECT id FROM stake_address WHERE view = _stake_address)
AND tx_in.tx_in_id >= _tx_id_min
) AS tmp;

RETURN QUERY
SELECT
DISTINCT(ENCODE(tx.hash, 'hex')) AS tx_hash,
b.epoch_no,
b.block_no AS block_height,
EXTRACT(EPOCH FROM b.time)::integer AS block_time
FROM public.tx
INNER JOIN public.block AS b ON b.id = tx.block_id
WHERE tx.id = ANY(_tx_id_list)
AND block.block_no >= _after_block_height
ORDER BY block.block_no DESC;
END;
$$;

COMMENT ON FUNCTION grest.account_txs IS 'Get transactions associated with a given stake address'; -- noqa: LT01
2 changes: 1 addition & 1 deletion files/grest/rpc/account/account_updates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DECLARE
sa_id_list integer[] DEFAULT NULL;
BEGIN
SELECT INTO sa_id_list
ARRAY_AGG(stake_address.id)
ARRAY_AGG(stake_address.id)
FROM
stake_address
WHERE
Expand Down
88 changes: 68 additions & 20 deletions files/grest/rpc/account/account_utxos.sql
Original file line number Diff line number Diff line change
@@ -1,34 +1,82 @@
CREATE OR REPLACE FUNCTION grest.account_utxos(_stake_address text)
CREATE OR REPLACE FUNCTION grest.account_utxos(_stake_addresses text [], _extended boolean DEFAULT false)
RETURNS TABLE (
tx_hash text,
tx_index smallint,
address varchar,
address text,
value text,
stake_address text,
payment_cred text,
epoch_no word31type,
block_height word31type,
block_time integer
block_time integer,
datum_hash text,
inline_datum jsonb,
reference_script jsonb,
asset_list jsonb,
is_spent boolean
)
LANGUAGE plpgsql
AS $$
DECLARE
known_addresses varchar[];
BEGIN
RETURN QUERY
SELECT
ENCODE(tx.hash,'hex') AS tx_hash,
tx_out.index::smallint AS tx_index,
tx_out.address,
tx_out.value::text AS value,
b.block_no AS block_height,
EXTRACT(EPOCH FROM b.time)::integer AS block_time
FROM
tx_out
LEFT JOIN tx_in ON tx_in.tx_out_id = tx_out.tx_id
AND tx_in.tx_out_index = tx_out.index
INNER JOIN tx ON tx.id = tx_out.tx_id
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
tx_out.address::text,
tx_out.value::text,
sa.view::text as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
b.epoch_no,
b.block_no,
EXTRACT(EPOCH FROM b.time)::integer AS block_time,
ENCODE(tx_out.data_hash, 'hex') AS datum_hash,
(CASE
WHEN _extended = false OR tx_out.inline_datum_id IS NULL THEN NULL
ELSE JSONB_BUILD_OBJECT(
'bytes', ENCODE(datum.bytes, 'hex'),
'value', datum.value
)
END) AS inline_datum,
(CASE
WHEN _extended = false OR tx_out.reference_script_id IS NULL THEN NULL
ELSE JSONB_BUILD_OBJECT(
'hash', ENCODE(script.hash, 'hex'),
'bytes', ENCODE(script.bytes, 'hex'),
'value', script.json,
'type', script.type::text,
'size', script.serialised_size
)
END) AS reference_script,
(CASE
WHEN _extended = false OR ma.policy IS NULL THEN NULL
ELSE JSONB_BUILD_OBJECT(
'policy_id', ENCODE(ma.policy, 'hex'),
'asset_name', ENCODE(ma.name, 'hex'),
'fingerprint', ma.fingerprint,
'decimals', aic.decimals,
'quantity', mto.quantity::text
)
END
) AS asset_list,
(CASE
WHEN tx_out.consumed_by_tx_in_id IS NULL THEN false
ELSE true
END) AS is_spent
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN block AS b ON b.id = tx.block_id
WHERE
tx_in.tx_out_id IS NULL
AND
tx_out.stake_address_id = (SELECT id FROM stake_address WHERE view = _stake_address);
LEFT JOIN ma_tx_out AS mto ON mto.tx_out_id = tx_out.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
LEFT JOIN datum ON datum.id = tx_out.inline_datum_id
LEFT JOIN script ON script.tx_id = tx.id
WHERE
tx_out.stake_address_id IN (SELECT sa.id FROM stake_address AS sa WHERE sa.view = ANY(_stake_addresses))
;
END;
$$;

COMMENT ON FUNCTION grest.account_utxos IS 'Get non-empty UTxOs associated with a given stake address'; -- noqa: LT01
COMMENT ON FUNCTION grest.address_utxos IS 'Get UTxO details for requested addresses'; -- noqa: LT01
22 changes: 8 additions & 14 deletions files/grest/rpc/address/address_assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ BEGIN
ma.fingerprint,
COALESCE(aic.decimals, 0) AS decimals,
SUM(mtx.quantity) AS quantity
FROM
ma_tx_out AS mtx
INNER JOIN multi_asset AS ma ON ma.id = mtx.ident
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
INNER JOIN tx_out AS txo ON txo.id = mtx.tx_out_id
LEFT JOIN tx_in ON txo.tx_id = tx_in.tx_out_id
AND txo.index::smallint = tx_in.tx_out_index::smallint
WHERE
txo.address = ANY(_addresses)
AND tx_in.tx_out_id IS NULL
FROM ma_tx_out AS mtx
INNER JOIN multi_asset AS ma ON ma.id = mtx.ident
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
INNER JOIN tx_out AS txo ON txo.id = mtx.tx_out_id
WHERE txo.address = ANY(_addresses)
AND tx_out.consumed_by_tx_in_id IS NULL
GROUP BY
txo.address, ma.policy, ma.name, ma.fingerprint, aic.decimals
)
Expand All @@ -45,10 +41,8 @@ BEGIN
'quantity', aa.quantity::text
)
) AS asset_list
FROM
_all_assets AS aa
GROUP BY
aa.address
FROM _all_assets AS aa
GROUP BY aa.address
) assets_grouped;
END;
$$;
Expand Down
Loading

0 comments on commit 99d83b9

Please sign in to comment.