Skip to content

Commit 46fd426

Browse files
committed
archive-hardfork-toolbox: Refactor so to remove duplicates in sql.ml
1 parent e151b84 commit 46fd426

File tree

1 file changed

+57
-75
lines changed
  • src/app/archive_hardfork_toolbox

1 file changed

+57
-75
lines changed

src/app/archive_hardfork_toolbox/sql.ml

Lines changed: 57 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,44 @@ let latest_state_hash_query =
1212
let latest_state_hash (module Conn : CONNECTION) =
1313
Conn.find latest_state_hash_query ()
1414

15-
let is_in_best_chain_query =
16-
(Caqti_type.(t4 string string int int64) ->! Caqti_type.bool)
17-
{sql|
15+
let chain_of_query =
16+
{sql|
1817
WITH RECURSIVE chain AS (
19-
SELECT
20-
b.id,
21-
NULLIF(b.parent_id, 0) AS parent_id,
22-
b.state_hash,
23-
b.height,
24-
b.global_slot_since_genesis,
25-
ARRAY[b.id] AS path
26-
FROM blocks b
27-
WHERE b.state_hash = ?
18+
SELECT
19+
b.id AS id,
20+
b.parent_id AS parent_id,
21+
b.state_hash AS state_hash,
22+
b.height AS height,
23+
b.global_slot_since_genesis AS global_slot_since_genesis
24+
FROM blocks b
25+
WHERE b.state_hash = ?
2826

29-
UNION ALL
27+
UNION ALL
3028

31-
SELECT
32-
p.id,
33-
NULLIF(p.parent_id, 0) AS parent_id,
34-
p.state_hash,
35-
p.height,
36-
p.global_slot_since_genesis,
37-
c.path || p.id
38-
FROM blocks p
39-
JOIN chain c ON p.id = c.parent_id
40-
WHERE NOT p.id = ANY (c.path)
29+
SELECT
30+
p.id,
31+
p.parent_id,
32+
p.state_hash,
33+
p.height,
34+
p.global_slot_since_genesis
35+
FROM blocks p
36+
JOIN chain c ON p.id = c.parent_id
37+
WHERE p.parent_id IS NOT NULL
4138
)
39+
|sql}
40+
41+
let is_in_best_chain_query =
42+
(Caqti_type.(t4 string string int int64) ->! Caqti_type.bool)
43+
( chain_of_query
44+
^ {sql|
4245
SELECT EXISTS (
4346
SELECT 1 FROM chain
4447
WHERE state_hash = ?
4548
AND height = ?
4649
AND global_slot_since_genesis = ?
47-
) AS is_in_chain;
50+
);
4851
|sql}
52+
)
4953

5054
let is_in_best_chain (module Conn : CONNECTION) ~tip_hash ~check_hash
5155
~check_height ~check_slot =
@@ -54,76 +58,54 @@ let is_in_best_chain (module Conn : CONNECTION) ~tip_hash ~check_hash
5458

5559
let no_of_confirmations_query =
5660
(Caqti_type.(t2 string int) ->! Caqti_type.int)
57-
{sql|
58-
WITH RECURSIVE chain AS
59-
(
60-
SELECT id, parent_id, chain_status, state_hash, height, global_slot_since_genesis FROM blocks
61-
WHERE state_hash = ?
62-
63-
UNION ALL
64-
SELECT b.id, b.parent_id, b.chain_status, b.state_hash, b.height, b.global_slot_since_genesis FROM blocks b
65-
INNER JOIN chain ON b.id = chain.parent_id AND (chain.id <> 0 OR b.id = 0)
66-
) SELECT count(*) FROM chain where global_slot_since_genesis >= ?;
61+
( chain_of_query
62+
^ {sql|
63+
SELECT count(*) FROM chain
64+
WHERE global_slot_since_genesis >= ?;
6765
|sql}
66+
)
6867
6968
let no_of_confirmations (module Conn : CONNECTION) ~latest_state_hash ~fork_slot
7069
=
7170
Conn.find no_of_confirmations_query (latest_state_hash, fork_slot)
7271
73-
let number_of_user_commands_since_block_query =
72+
let number_of_commands_since_block_query block_commands_table =
7473
(Caqti_type.(t2 string int) ->! Caqti_type.(t4 string int int int))
75-
{sql|
76-
WITH RECURSIVE chain AS
77-
(
78-
SELECT id, parent_id, chain_status, state_hash, height, global_slot_since_genesis FROM blocks
79-
WHERE state_hash = ?
80-
UNION ALL
81-
SELECT b.id, b.parent_id, b.chain_status, b.state_hash, b.height, b.global_slot_since_genesis FROM blocks b
82-
INNER JOIN chain ON b.id = chain.parent_id AND (chain.id <> 0 OR b.id = 0)
83-
) SELECT state_hash, height, global_slot_since_genesis, count(bc.block_id) as user_command_count FROM chain left join blocks_user_commands bc on bc.block_id = id where global_slot_since_genesis >= ? group by state_hash, height, global_slot_since_genesis;
74+
( chain_of_query
75+
^ Printf.sprintf
76+
{sql|
77+
SELECT
78+
state_hash,
79+
height,
80+
global_slot_since_genesis,
81+
COUNT(bc.block_id) AS command_count
82+
FROM chain
83+
LEFT JOIN %s bc
84+
ON chain.id = bc.block_id
85+
WHERE global_slot_since_genesis >= ?
86+
GROUP BY
87+
state_hash,
88+
height,
89+
global_slot_since_genesis;
8490
|sql}
91+
block_commands_table )
8592
8693
let number_of_user_commands_since_block (module Conn : CONNECTION)
8794
~fork_state_hash ~fork_slot =
88-
Conn.find number_of_user_commands_since_block_query
95+
Conn.find
96+
(number_of_commands_since_block_query "blocks_user_commands")
8997
(fork_state_hash, fork_slot)
9098
91-
let number_of_internal_commands_since_block_query =
92-
(Caqti_type.(t2 string int) ->! Caqti_type.(t4 string int int int))
93-
{sql|
94-
WITH RECURSIVE chain AS
95-
(
96-
SELECT id, parent_id, chain_status, state_hash, height, global_slot_since_genesis FROM blocks
97-
WHERE state_hash = ?
98-
UNION ALL
99-
SELECT b.id, b.parent_id, b.chain_status, b.state_hash, b.height, b.global_slot_since_genesis FROM blocks b
100-
INNER JOIN chain ON b.id = chain.parent_id AND (chain.id <> 0 OR b.id = 0)
101-
) SELECT state_hash, height, global_slot_since_genesis, count(bc.block_id) as internal_command_count FROM chain left join blocks_internal_commands bc on bc.block_id = id where global_slot_since_genesis >= ? group by state_hash, height, global_slot_since_genesis
102-
;
103-
|sql}
104-
10599
let number_of_internal_commands_since_block (module Conn : CONNECTION)
106100
~fork_state_hash ~fork_slot =
107-
Conn.find number_of_internal_commands_since_block_query
101+
Conn.find
102+
(number_of_commands_since_block_query "blocks_internal_commands")
108103
(fork_state_hash, fork_slot)
109104
110-
let number_of_zkapps_commands_since_block_query =
111-
(Caqti_type.(t2 string int) ->! Caqti_type.(t4 string int int int))
112-
{sql|
113-
WITH RECURSIVE chain AS
114-
(
115-
SELECT id, parent_id, chain_status, state_hash, height, global_slot_since_genesis FROM blocks
116-
WHERE state_hash = ?
117-
UNION ALL
118-
SELECT b.id, b.parent_id, b.chain_status, b.state_hash, b.height, b.global_slot_since_genesis FROM blocks b
119-
INNER JOIN chain ON b.id = chain.parent_id AND (chain.id <> 0 OR b.id = 0)
120-
) SELECT state_hash, height, global_slot_since_genesis, count(bc.block_id) as zkapp_command_count FROM chain left join blocks_zkapp_commands bc on bc.block_id = id where global_slot_since_genesis >= ? group by state_hash, height, global_slot_since_genesis
121-
;
122-
|sql}
123-
124105
let number_of_zkapps_commands_since_block (module Conn : CONNECTION)
125106
~fork_state_hash ~fork_slot =
126-
Conn.find number_of_zkapps_commands_since_block_query
107+
Conn.find
108+
(number_of_commands_since_block_query "blocks_zkapp_commands")
127109
(fork_state_hash, fork_slot)
128110
129111
let last_fork_block_query =

0 commit comments

Comments
 (0)