Skip to content

Commit cc4a289

Browse files
committed
fix(#2671): searching for drep off_chain data
1 parent 95955bb commit cc4a289

File tree

1 file changed

+165
-151
lines changed

1 file changed

+165
-151
lines changed

govtool/backend/sql/list-dreps.sql

Lines changed: 165 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -35,168 +35,182 @@ LatestVoteEpoch AS (
3535
JOIN block ON block.id = tx.block_id
3636
WHERE
3737
lvp.rn = 1
38-
)
39-
SELECT DISTINCT ON (dh.raw)
40-
encode(dh.raw, 'hex'),
41-
dh.view,
42-
dh.has_script,
43-
va.url,
44-
encode(va.data_hash, 'hex'),
45-
dr_deposit.deposit,
46-
DRepDistr.amount,
47-
(DRepActivity.epoch_no - GREATEST(COALESCE(block.epoch_no, block_first_register.epoch_no), lve.epoch_no, newestRegister.epoch_no)) <= DRepActivity.drep_activity AS active,
48-
encode(dr_voting_anchor.tx_hash, 'hex') AS tx_hash,
49-
newestRegister.time AS last_register_time,
50-
COALESCE(latestDeposit.deposit, 0),
51-
non_deregister_voting_anchor.url IS NOT NULL AS has_non_deregister_voting_anchor,
52-
fetch_error.message,
53-
off_chain_vote_drep_data.payment_address,
54-
off_chain_vote_drep_data.given_name,
55-
off_chain_vote_drep_data.objectives,
56-
off_chain_vote_drep_data.motivations,
57-
off_chain_vote_drep_data.qualifications,
58-
off_chain_vote_drep_data.image_url,
59-
off_chain_vote_drep_data.image_hash
60-
FROM
61-
drep_hash dh
62-
JOIN (
63-
SELECT
64-
dr.id,
65-
dr.drep_hash_id,
66-
dr.deposit,
67-
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn
68-
FROM
69-
drep_registration dr
70-
WHERE
71-
dr.deposit IS NOT NULL
72-
) AS dr_deposit ON dr_deposit.drep_hash_id = dh.id AND dr_deposit.rn = 1
73-
JOIN (
74-
SELECT
38+
),
39+
RankedDRepRegistration AS (
40+
SELECT
7541
dr.id,
7642
dr.drep_hash_id,
7743
dr.deposit,
78-
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn
79-
FROM
80-
drep_registration dr
81-
) AS latestDeposit ON latestDeposit.drep_hash_id = dh.id AND latestDeposit.rn = 1
82-
LEFT JOIN (
83-
SELECT
84-
dr.id,
85-
dr.drep_hash_id,
8644
dr.voting_anchor_id,
8745
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn,
88-
tx.hash AS tx_hash
46+
encode(tx.hash, 'hex') AS tx_hash
8947
FROM
9048
drep_registration dr
91-
JOIN tx ON tx.id = dr.tx_id
92-
WHERE
93-
COALESCE(dr.deposit, 0) >= 0
94-
) AS dr_voting_anchor ON dr_voting_anchor.drep_hash_id = dh.id AND dr_voting_anchor.rn = 1
95-
LEFT JOIN (
96-
SELECT
97-
dr.id,
98-
dr.drep_hash_id,
99-
dr.voting_anchor_id,
100-
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn,
101-
tx.hash AS tx_hash
102-
FROM
103-
drep_registration dr
104-
JOIN tx ON tx.id = dr.tx_id
105-
) AS dr_non_deregister_voting_anchor ON dr_non_deregister_voting_anchor.drep_hash_id = dh.id AND dr_non_deregister_voting_anchor.rn = 1
106-
LEFT JOIN (
107-
SELECT
108-
dr.id,
109-
dr.drep_hash_id,
110-
dr.voting_anchor_id,
111-
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn
112-
FROM
113-
drep_registration dr
114-
) AS second_to_newest_drep_registration ON second_to_newest_drep_registration.drep_hash_id = dh.id AND second_to_newest_drep_registration.rn = 2
115-
LEFT JOIN DRepDistr ON DRepDistr.hash_id = dh.id AND DRepDistr.rn = 1
116-
LEFT JOIN voting_anchor va ON va.id = dr_voting_anchor.voting_anchor_id
117-
LEFT JOIN voting_anchor non_deregister_voting_anchor ON non_deregister_voting_anchor.id = dr_non_deregister_voting_anchor.voting_anchor_id
118-
LEFT JOIN (
119-
SELECT
120-
fetch_error as message,
121-
voting_anchor_id
122-
FROM
49+
JOIN tx ON tx.id = dr.tx_id
50+
),
51+
FetchError AS (
52+
SELECT
53+
fetch_error as message,
54+
voting_anchor_id
55+
FROM
56+
off_chain_vote_fetch_error
57+
WHERE
58+
fetch_time = (
59+
SELECT
60+
max(fetch_time)
61+
FROM
12362
off_chain_vote_fetch_error
124-
WHERE
125-
fetch_time = (
126-
SELECT
127-
max(fetch_time)
128-
FROM
129-
off_chain_vote_fetch_error
130-
)
131-
GROUP BY
132-
fetch_error,
133-
voting_anchor_id
134-
) AS fetch_error ON fetch_error.voting_anchor_id = va.id
135-
LEFT JOIN off_chain_vote_data ON off_chain_vote_data.voting_anchor_id = va.id
136-
LEFT JOIN off_chain_vote_drep_data ON off_chain_vote_drep_data.off_chain_vote_data_id = off_chain_vote_data.id
137-
CROSS JOIN DRepActivity
138-
LEFT JOIN voting_procedure ON voting_procedure.drep_voter = dh.id
139-
LEFT JOIN tx ON tx.id = voting_procedure.tx_id
140-
LEFT JOIN block ON block.id = tx.block_id
141-
LEFT JOIN (
142-
SELECT
143-
block.epoch_no,
144-
block.time,
145-
dr.drep_hash_id,
146-
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn
147-
FROM
63+
)
64+
GROUP BY
65+
fetch_error,
66+
voting_anchor_id
67+
),
68+
LatestExistingVotingAnchor AS (
69+
SELECT
70+
dr.id AS drep_registration_id,
71+
dr.drep_hash_id,
72+
va.id AS voting_anchor_id,
73+
va.url,
74+
encode(va.data_hash, 'hex') AS metadata_hash
75+
FROM
76+
drep_registration dr
77+
JOIN voting_anchor va ON dr.voting_anchor_id = va.id
78+
JOIN off_chain_vote_data ocvd ON va.id = ocvd.voting_anchor_id
79+
WHERE
80+
ocvd.voting_anchor_id IS NOT NULL
81+
ORDER BY
82+
dr.tx_id DESC
83+
),
84+
HasNonDeregisterVotingAnchor AS (
85+
SELECT
86+
dr.drep_hash_id,
87+
EXISTS (
88+
SELECT 1
89+
FROM drep_registration dr_sub
90+
WHERE
91+
dr_sub.drep_hash_id = dr.drep_hash_id
92+
AND dr_sub.voting_anchor_id IS NULL
93+
AND COALESCE(dr_sub.deposit, 0) >= 0
94+
) AS value
95+
FROM
96+
drep_registration dr
97+
GROUP BY
98+
dr.drep_hash_id
99+
),
100+
DRepData AS (
101+
SELECT DISTINCT ON (dh.raw)
102+
encode(dh.raw, 'hex') drep_hash,
103+
dh.view,
104+
dh.has_script,
105+
leva.url,
106+
leva.metadata_hash,
107+
COALESCE(dr_deposit.deposit, 0) as deposit,
108+
DRepDistr.amount,
109+
(DRepActivity.epoch_no - GREATEST(COALESCE(voting_procedure_block.epoch_no, block_first_register.epoch_no), lve.epoch_no, newestRegister.epoch_no)) <= DRepActivity.drep_activity AS active,
110+
RankedDRepRegistration.tx_hash,
111+
newestRegister.time AS last_register_time,
112+
COALESCE(RankedDRepRegistration.deposit, 0) as latest_deposit,
113+
hndva.value AS has_non_deregister_voting_anchor,
114+
fetch_error.message AS fetch_error,
115+
off_chain_vote_drep_data.payment_address,
116+
off_chain_vote_drep_data.given_name,
117+
off_chain_vote_drep_data.objectives,
118+
off_chain_vote_drep_data.motivations,
119+
off_chain_vote_drep_data.qualifications,
120+
off_chain_vote_drep_data.image_url,
121+
off_chain_vote_drep_data.image_hash
122+
FROM
123+
drep_hash dh
124+
JOIN RankedDRepRegistration ON RankedDRepRegistration.drep_hash_id = dh.id AND RankedDRepRegistration.rn = 1
125+
JOIN (
126+
SELECT
127+
dr.id,
128+
dr.drep_hash_id,
129+
dr.deposit,
130+
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn
131+
FROM
132+
drep_registration dr
133+
WHERE
134+
dr.deposit IS NOT NULL
135+
) AS dr_deposit ON dr_deposit.drep_hash_id = dh.id AND dr_deposit.rn = 1
136+
LEFT JOIN (
137+
SELECT
138+
dr.id,
139+
dr.drep_hash_id,
140+
dr.deposit,
141+
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn
142+
FROM
143+
drep_registration dr
144+
) AS latestDeposit ON latestDeposit.drep_hash_id = dh.id AND latestDeposit.rn = 1
145+
LEFT JOIN LatestExistingVotingAnchor leva ON leva.drep_hash_id = dh.id
146+
LEFT JOIN DRepDistr ON DRepDistr.hash_id = dh.id AND DRepDistr.rn = 1
147+
LEFT JOIN FetchError fetch_error ON fetch_error.voting_anchor_id = leva.voting_anchor_id
148+
LEFT JOIN HasNonDeregisterVotingAnchor hndva ON hndva.drep_hash_id = dh.id
149+
LEFT JOIN off_chain_vote_data ocvd ON ocvd.voting_anchor_id = leva.voting_anchor_id
150+
LEFT JOIN off_chain_vote_drep_data ON off_chain_vote_drep_data.off_chain_vote_data_id = ocvd.id
151+
LEFT JOIN voting_procedure ON voting_procedure.drep_voter = dh.id
152+
LEFT JOIN tx voting_procedure_transaction ON voting_procedure_transaction.id = voting_procedure.tx_id
153+
LEFT JOIN block voting_procedure_block ON voting_procedure_block.id = voting_procedure_transaction.block_id
154+
LEFT JOIN (
155+
SELECT
156+
block.epoch_no,
157+
block.time,
158+
dr.drep_hash_id,
159+
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn
160+
FROM
148161
drep_registration dr
149162
JOIN tx ON tx.id = dr.tx_id
150163
JOIN block ON block.id = tx.block_id
151-
WHERE
152-
COALESCE(dr.deposit, 0) >= 0
153-
) AS newestRegister ON newestRegister.drep_hash_id = dh.id AND newestRegister.rn = 1
154-
LEFT JOIN (
155-
SELECT
156-
dr.tx_id,
157-
dr.drep_hash_id,
158-
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id ASC) AS rn
159-
FROM
160-
drep_registration dr
161-
) AS dr_first_register ON dr_first_register.drep_hash_id = dh.id AND dr_first_register.rn = 1
162-
LEFT JOIN tx AS tx_first_register ON tx_first_register.id = dr_first_register.tx_id
163-
LEFT JOIN block AS block_first_register ON block_first_register.id = tx_first_register.block_id
164-
LEFT JOIN LatestVoteEpoch lve ON lve.drep_id = dh.id
164+
WHERE
165+
COALESCE(dr.deposit, 0) >= 0
166+
) AS newestRegister ON newestRegister.drep_hash_id = dh.id AND newestRegister.rn = 1
167+
LEFT JOIN (
168+
SELECT
169+
dr.tx_id,
170+
dr.drep_hash_id,
171+
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id ASC) AS rn
172+
FROM
173+
drep_registration dr
174+
) AS dr_first_register ON dr_first_register.drep_hash_id = dh.id AND dr_first_register.rn = 1
175+
LEFT JOIN tx AS tx_first_register ON tx_first_register.id = dr_first_register.tx_id
176+
LEFT JOIN block AS block_first_register ON block_first_register.id = tx_first_register.block_id
177+
LEFT JOIN LatestVoteEpoch lve ON lve.drep_id = dh.id
178+
CROSS JOIN DRepActivity
179+
GROUP BY
180+
dh.raw,
181+
dh.view,
182+
dh.has_script,
183+
leva.url,
184+
leva.metadata_hash,
185+
dr_deposit.deposit,
186+
DRepDistr.amount,
187+
DRepActivity.epoch_no,
188+
voting_procedure_block.epoch_no,
189+
block_first_register.epoch_no,
190+
lve.epoch_no, newestRegister.epoch_no,
191+
DRepActivity.drep_activity,
192+
RankedDRepRegistration.tx_hash,
193+
newestRegister.time,
194+
RankedDRepRegistration.deposit,
195+
hndva.value,
196+
fetch_error.message,
197+
off_chain_vote_drep_data.payment_address,
198+
off_chain_vote_drep_data.given_name,
199+
off_chain_vote_drep_data.objectives,
200+
off_chain_vote_drep_data.motivations,
201+
off_chain_vote_drep_data.qualifications,
202+
off_chain_vote_drep_data.image_url,
203+
off_chain_vote_drep_data.image_hash
204+
)
205+
SELECT * FROM DRepData
165206
WHERE
166207
(
167208
COALESCE(?, '') = '' OR
168-
(CASE WHEN LENGTH(?) % 2 = 0 AND ? ~ '^[0-9a-fA-F]+$' THEN dh.raw = decode(?, 'hex') ELSE false END) OR
169-
dh.view ILIKE ? OR
170-
off_chain_vote_drep_data.given_name ILIKE ? OR
171-
off_chain_vote_drep_data.payment_address ILIKE ? OR
172-
off_chain_vote_drep_data.objectives ILIKE ? OR
173-
off_chain_vote_drep_data.motivations ILIKE ? OR
174-
off_chain_vote_drep_data.qualifications ILIKE ?
175-
)
176-
GROUP BY
177-
block_first_register.epoch_no,
178-
block.epoch_no,
179-
dh.raw,
180-
second_to_newest_drep_registration.voting_anchor_id,
181-
dh.view,
182-
dh.has_script,
183-
va.url,
184-
va.data_hash,
185-
dr_deposit.deposit,
186-
DRepDistr.amount,
187-
DRepActivity.epoch_no,
188-
DRepActivity.drep_activity,
189-
lve.epoch_no,
190-
dr_voting_anchor.tx_hash,
191-
newestRegister.time,
192-
newestRegister.epoch_no,
193-
latestDeposit.deposit,
194-
non_deregister_voting_anchor.url,
195-
fetch_error.message,
196-
off_chain_vote_drep_data.payment_address,
197-
off_chain_vote_drep_data.given_name,
198-
off_chain_vote_drep_data.objectives,
199-
off_chain_vote_drep_data.motivations,
200-
off_chain_vote_drep_data.qualifications,
201-
off_chain_vote_drep_data.image_url,
202-
off_chain_vote_drep_data.image_hash;
209+
(CASE WHEN LENGTH(?) % 2 = 0 AND ? ~ '^[0-9a-fA-F]+$' THEN drep_hash = ? ELSE false END) OR
210+
view ILIKE ? OR
211+
given_name ILIKE ? OR
212+
payment_address ILIKE ? OR
213+
objectives ILIKE ? OR
214+
motivations ILIKE ? OR
215+
qualifications ILIKE ?
216+
)

0 commit comments

Comments
 (0)