Skip to content

Commit 538d94d

Browse files
committed
Koios v1.3.2 (#328)
- `/address_outputs` - Basic transaction output info for given addresses - `/pool_calidus_keys` - List of valid calidus keys for all pools - `/pool_groups` - List of all registered pool and their groups across sources from [pool_groups](https://github.com/cardano-community/pool_groups) repository. This is only relevant for mainnet - `/pool_owner_history` - Return information about pool owner's historical stake and their promised pledge to their pools - `/account_stake_history` - Get history for dreps voting power distribution - `/account_reward_history` - Get the full rewards history (including MIR) for given stake addresses - `/account_update_history` - Get historical updates (registration, deregistration, delegation and withdrawals) for given stake addresses - `/drep_voting_power_history` - Renamed from drep_history (left drep_history for consistency) - `/pool_voting_power_history` - New endpoint to give voting_power_history by pool - `/vote_list` - List of all votes posted on-chain - Input (non-breaking) - `/voter_proposal_list` - Make _voter_id optional - Output - `/tip` - Deprecate column `block_no` and use `block_height` to be consistent across all endpoints - Output - `/pool_list` - Add column `pool_group` to output - Output - `/pool_voting_summary` - Update fields with data type lovelace/integer to string - Output - `/account_info` - Add column proposal_refund to output - `/account_history` - Part of flattening the JSON heirarchy (so that horizontal filtering is optimal), use `/account_stake_history` instead - `/account_rewards` - Part of flattening the JSON heirarchy (so that horizontal filtering is optimal), use `/account_rewards_history` instead - `/account_updates` - Part of flattening the JSON heirarchy (so that horizontal filtering is optimal), use `/account_update_history` instead - `/drep_history` - Renamed to `/drep_voting_power_history` to be more precise - `/drep_votes` - Should be available via `/vote_list` (can use horizontal filtering) - `/pool_votes` - Should be available via `/vote_list` (can use horizontal filtering) - Replace [pg_bech32](https://github.com/cardano-community/pg_bech32) library with [pg_cardano](https://github.com/cardano-community/pg_cardano) - Improve grest.epoch_info_cache performance - `/proposal_voting_summary` - Do lookup for gov action id beforehand, Added handling of inactive DRep stake in voting as abstain, unless they voted in epoch of interest - `/account_info` - Add limit filters to ensure there is a single hit per account - Add indexes for voting_procedure - None
1 parent a44aa22 commit 538d94d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+5866
-3030
lines changed

files/grest/.sqlfluff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[sqlfluff]
22
dialect = postgres
3-
exclude_rules = structure.column_order, references.keywords
3+
#exclude_rules = structure.column_order, references.keywords
44
max_line_length=260
55
capitalisation_policy = upper
66
extended_capitalisation_policy = upper
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
DB_NAME=cexplorer
3+
POOL_GROUP_URL=https://raw.githubusercontent.com/cardano-community/pool_groups/refs/heads/main/spos.json
4+
5+
dttime=$(date +%F_%H:%M:%S)
6+
7+
network=$(psql ${DB_NAME} -qbt -c "SELECT network_name from public.meta;" | awk '{print $1}')
8+
[[ "${network}" != "mainnet" ]] && echo "pool_groups endpoint is not applicable for networks other than mainnet!" && exit 0
9+
10+
echo "${dttime} - START - Import pool groups"
11+
12+
curl -sfL "${POOL_GROUP_URL}" -o .poolgroups.json
13+
14+
[[ -f '.poolgroups.csv' ]] && rm -f .poolgroups.csv
15+
jq -er '.[] | [ .pool_id_bech32,.group,.ticker,.adastat_group,.balanceanalytics_group ] | @csv' .poolgroups.json > .poolgroups.csv
16+
17+
cat << EOF > .poolgroups.sql
18+
CREATE TEMP TABLE tmppoolgrps (like grest.pool_groups);
19+
\COPY tmppoolgrps FROM '.poolgroups.csv' DELIMITER ',' CSV;
20+
INSERT INTO grest.pool_groups SELECT * FROM tmppoolgrps ON CONFLICT(pool_id_bech32) DO UPDATE SET pool_id_bech32=excluded.pool_id_bech32, pool_group=excluded.pool_group, ticker=excluded.ticker, adastat_group=excluded.adastat_group, balanceanalytics_group=excluded.balanceanalytics_group;
21+
EOF
22+
23+
psql ${DB_NAME} -qb -f .poolgroups.sql >/dev/null && rm -f .poolgroups.sql
24+
psql ${DB_NAME} -qb -c "INSERT INTO grest.control_table (key, last_value) VALUES ('pool_groups_check','${dttime}') ON CONFLICT(key) DO UPDATE SET last_value='${dttime}'"
25+
echo "$(date +%F_%H:%M:%S) - END - Pool Groups Update finished."

files/grest/rpc/000_utilities/cip129.sql

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ LANGUAGE plpgsql STABLE
2727
AS $$
2828
BEGIN
2929
IF LENGTH(_cc_hot) = 60 THEN
30-
RETURN SUBSTRING(b32_decode(_cc_hot) from 3);
30+
RETURN SUBSTRING(ENCODE(cardano.bech32_decode_data(_cc_hot),'hex') from 3);
3131
ELSE
32-
RETURN b32_decode(_cc_hot);
32+
RETURN ENCODE(cardano.bech32_decode_data(_cc_hot),'hex');
3333
END IF;
3434
END;
3535
$$;
@@ -40,7 +40,7 @@ LANGUAGE plpgsql STABLE
4040
AS $$
4141
BEGIN
4242
IF LENGTH(_cc_hot) = 60 THEN
43-
RETURN SUBSTRING(b32_decode(_cc_hot) from 2 for 1) = '3';
43+
RETURN SUBSTRING(ENCODE(cardano.bech32_decode_data(_cc_hot),'hex') from 2 for 1) = '3';
4444
ELSE
4545
RETURN STARTS_WITH(_cc_hot, 'cc_hot_script');
4646
END IF;
@@ -54,9 +54,9 @@ AS $$
5454
BEGIN
5555
IF _raw IS NULL THEN RETURN NULL; END IF;
5656
IF _is_script THEN
57-
RETURN b32_encode('cc_hot', ('\x03'::bytea || _raw)::text);
57+
RETURN cardano.bech32_encode('cc_hot', ('\x03'::bytea || _raw));
5858
ELSE
59-
RETURN b32_encode('cc_hot', ('\x02'::bytea || _raw)::text);
59+
RETURN cardano.bech32_encode('cc_hot', ('\x02'::bytea || _raw));
6060
END IF;
6161
END;
6262
$$;
@@ -67,9 +67,9 @@ LANGUAGE plpgsql STABLE
6767
AS $$
6868
BEGIN
6969
IF LENGTH(_cc_cold) = 61 THEN
70-
RETURN SUBSTRING(b32_decode(_cc_cold) from 3);
70+
RETURN SUBSTRING(ENCODE(cardano.bech32_decode_data(_cc_cold),'hex') from 3);
7171
ELSE
72-
RETURN b32_decode(_cc_cold);
72+
RETURN ENCODE(cardano.bech32_decode_data(_cc_cold),'hex');
7373
END IF;
7474
END;
7575
$$;
@@ -80,7 +80,7 @@ LANGUAGE plpgsql STABLE
8080
AS $$
8181
BEGIN
8282
IF LENGTH(_cc_cold) = 61 THEN
83-
RETURN SUBSTRING(b32_decode(_cc_cold) from 2 for 1) = '3';
83+
RETURN SUBSTRING(ENCODE(cardano.bech32_decode_data(_cc_cold),'hex') from 2 for 1) = '3';
8484
ELSE
8585
RETURN STARTS_WITH(_cc_cold, 'cc_cold_script');
8686
END IF;
@@ -94,9 +94,9 @@ AS $$
9494
BEGIN
9595
IF _raw IS NULL THEN RETURN NULL; END IF;
9696
IF _is_script THEN
97-
RETURN b32_encode('cc_cold', ('\x13'::bytea || _raw)::text);
97+
RETURN cardano.bech32_encode('cc_cold', ('\x13'::bytea || _raw));
9898
ELSE
99-
RETURN b32_encode('cc_cold', ('\x12'::bytea || _raw)::text);
99+
RETURN cardano.bech32_encode('cc_cold', ('\x12'::bytea || _raw));
100100
END IF;
101101
END;
102102
$$;
@@ -107,9 +107,9 @@ LANGUAGE plpgsql STABLE
107107
AS $$
108108
BEGIN
109109
IF LENGTH(_drep_id) = 58 THEN
110-
RETURN SUBSTRING(b32_decode(_drep_id) from 3);
110+
RETURN SUBSTRING(ENCODE(cardano.bech32_decode_data(_drep_id),'hex') from 3);
111111
ELSE
112-
RETURN b32_decode(_drep_id);
112+
RETURN ENCODE(cardano.bech32_decode_data(_drep_id),'hex');
113113
END IF;
114114
END;
115115
$$;
@@ -120,7 +120,7 @@ LANGUAGE plpgsql STABLE
120120
AS $$
121121
BEGIN
122122
IF LENGTH(_drep_id) = 58 THEN
123-
RETURN SUBSTRING(b32_decode(_drep_id) from 2 for 1) = '3';
123+
RETURN SUBSTRING(ENCODE(cardano.bech32_decode_data(_drep_id),'hex') from 2 for 1) = '3';
124124
ELSE
125125
RETURN STARTS_WITH(_drep_id, 'drep_script');
126126
END IF;
@@ -134,9 +134,9 @@ AS $$
134134
BEGIN
135135
IF _raw IS NULL THEN RETURN NULL; END IF;
136136
IF _is_script THEN
137-
RETURN b32_encode('drep', ('\x23'::bytea || _raw)::text);
137+
RETURN cardano.bech32_encode('drep', ('\x23'::bytea || _raw));
138138
ELSE
139-
RETURN b32_encode('drep', ('\x22'::bytea || _raw)::text);
139+
RETURN cardano.bech32_encode('drep', ('\x22'::bytea || _raw));
140140
END IF;
141141
END;
142142
$$;
@@ -148,7 +148,7 @@ AS $$
148148
DECLARE
149149
proposal_id_hex text;
150150
BEGIN
151-
SELECT INTO proposal_id_hex b32_decode(_proposal_id);
151+
SELECT INTO proposal_id_hex ENCODE(cardano.bech32_decode_data(_proposal_id),'hex');
152152
RETURN ARRAY[LEFT(proposal_id_hex, 64), ('x' || RIGHT(proposal_id_hex, -64))::bit(8)::int::text];
153153
END;
154154
$$;
@@ -158,7 +158,7 @@ RETURNS text
158158
LANGUAGE plpgsql STABLE
159159
AS $$
160160
BEGIN
161-
RETURN b32_encode('gov_action', (_tx_hash || DECODE(LPAD(TO_HEX(_index), 2, '0'), 'hex'))::text);
161+
RETURN cardano.bech32_encode('gov_action', (_tx_hash || DECODE(LPAD(TO_HEX(_index), 2, '0'), 'hex')));
162162
END;
163163
$$;
164164

files/grest/rpc/000_utilities/cip5.sql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ AS $$
99
BEGIN
1010
IF _raw IS NULL THEN
1111
RETURN NULL;
12-
END IF;
13-
IF SUBSTRING(ENCODE(_raw, 'hex') from 2 for 1) = '0' THEN
14-
RETURN b32_encode('stake_test', _raw::text);
1512
ELSE
16-
RETURN b32_encode('stake', _raw::text);
13+
RETURN cardano.tools_shelley_address_build(
14+
''::bytea,
15+
FALSE,
16+
SUBSTRING(_raw FROM 2),
17+
FALSE,
18+
SUBSTRING(ENCODE(_raw, 'hex') from 2 for 1)::integer
19+
)::text;
1720
END IF;
1821
END;
1922
$$;

files/grest/rpc/00_blockchain/tip.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ RETURNS TABLE (
44
epoch_no word31type,
55
abs_slot word63type,
66
epoch_slot word31type,
7+
block_height word31type,
78
block_no word31type,
89
block_time integer
910
)
@@ -14,7 +15,8 @@ AS $$
1415
b.epoch_no AS epoch_no,
1516
b.slot_no AS abs_slot,
1617
b.epoch_slot_no AS epoch_slot,
17-
b.block_no,
18+
b.block_no AS block_height,
19+
b.block_no AS block_no,
1820
EXTRACT(EPOCH FROM b.time)::integer
1921
FROM block AS b
2022
ORDER BY b.id DESC

files/grest/rpc/01_cached_tables/epoch_info_cache.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ AS $$
2424
DECLARE
2525
_curr_epoch bigint;
2626
_latest_epoch_no_in_cache bigint;
27+
_cutoff_epoch bigint;
2728
BEGIN
2829
-- Check previous cache update completed before running
2930
IF (
@@ -83,12 +84,25 @@ BEGIN
8384
tx_id bigint
8485
);
8586

87+
-- cutoff epoch is the last epoch with at least one tx referenced in cache table
88+
SELECT COALESCE((
89+
SELECT MIN(eic.epoch_no)
90+
FROM grest.epoch_info_cache eic
91+
WHERE eic.i_last_tx_id =
92+
(SELECT MAX(i_last_tx_id)
93+
FROM grest.epoch_info_cache eic2
94+
WHERE i_last_tx_id is not null
95+
)
96+
), 0)
97+
INTO _cutoff_epoch;
98+
8699
INSERT INTO last_tx_id_subset
87100
SELECT b.epoch_no, MAX(tx.id)
88101
FROM block AS b
89102
INNER JOIN tx ON tx.block_id = b.id
90103
WHERE b.block_no IS NOT NULL
91104
AND b.tx_count != 0
105+
AND b.epoch_no >= _cutoff_epoch
92106
GROUP BY b.epoch_no;
93107

94108
INSERT INTO grest.epoch_info_cache
@@ -148,16 +162,33 @@ CREATE OR REPLACE FUNCTION grest.update_latest_epoch_info_cache(_curr_epoch bigi
148162
RETURNS void
149163
LANGUAGE plpgsql
150164
AS $$
165+
DECLARE
166+
_cutoff_epoch bigint;
151167
BEGIN
168+
169+
-- cutoff epoch is the last epoch with at least one tx referenced in cache table
170+
SELECT COALESCE((
171+
SELECT MIN(eic.epoch_no)
172+
FROM grest.epoch_info_cache eic
173+
WHERE eic.i_last_tx_id =
174+
(SELECT MAX(i_last_tx_id)
175+
FROM grest.epoch_info_cache eic2
176+
WHERE i_last_tx_id is not null
177+
)
178+
), 0)
179+
INTO _cutoff_epoch;
180+
152181
-- only update last tx id in case of new epoch
153182
IF _curr_epoch <> _epoch_no_to_update THEN
183+
154184
UPDATE grest.epoch_info_cache
155185
SET i_last_tx_id = last_tx.tx_id
156186
FROM (
157187
SELECT MAX(tx.id) AS tx_id
158188
FROM block AS b
159189
INNER JOIN tx ON tx.block_id = b.id
160190
WHERE b.epoch_no <= _epoch_no_to_update
191+
AND b.epoch_no >= _cutoff_epoch
161192
AND b.block_no IS NOT NULL
162193
AND b.tx_count != 0
163194
) AS last_tx

files/grest/rpc/01_cached_tables/pool_history_cache.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ DECLARE
4040
_pool_ids bigint [];
4141
BEGIN
4242
_pool_ids := (SELECT ARRAY_AGG(id) from pool_hash ph where ph.hash_raw = ANY(
43-
SELECT DECODE(b32_decode(pool),'hex')
43+
SELECT cardano.bech32_decode_data(pool)
4444
FROM UNNEST(_pool_bech32) AS pool)
4545
);
4646

0 commit comments

Comments
 (0)