• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MinaProtocol / mina / 765

04 Nov 2025 08:37PM UTC coverage: 36.987%. First build
765

push

buildkite

web-flow
Merge pull request #18055 from MinaProtocol/dkijania/add_select_best_fork_candidate

command to print out last filled block (state hash, global slot, height)

2 of 10 new or added lines in 3 files covered. (20.0%)

26972 of 72922 relevant lines covered (36.99%)

36207.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/src/app/archive_hardfork_toolbox/sql.ml
1
open Core
2
open Caqti_request.Infix
3

4
module type CONNECTION = Mina_caqti.CONNECTION
5

6
let latest_state_hash_query =
7
  Caqti_type.(unit ->! string)
5✔
8
    {sql|
9
          SELECT state_hash from blocks order by height desc limit 1;
10
        |sql}
11

12
let latest_state_hash (module Conn : CONNECTION) =
13
  Conn.find latest_state_hash_query ()
1✔
14

15
let chain_of_query =
16
  {sql|
17
    WITH RECURSIVE chain AS (
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 = ?
26

27
        UNION ALL
28

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
38
    )
39
  |sql}
40

41
let is_in_best_chain_query =
42
  Caqti_type.(t4 string string int int64 ->! bool)
5✔
43
    ( chain_of_query
44
    ^ {sql|
45
    SELECT EXISTS (
46
      SELECT 1 FROM chain
47
      WHERE state_hash = ?
48
        AND height = ?
49
        AND global_slot_since_genesis = ?
50
    );
51
    |sql}
52
    )
53

54
let is_in_best_chain (module Conn : CONNECTION) ~tip_hash ~check_hash
55
    ~check_height ~check_slot =
56
  Conn.find is_in_best_chain_query
1✔
57
    (tip_hash, check_hash, check_height, check_slot)
58

59
let num_of_confirmations_query =
60
  Caqti_type.(t2 string int ->! int)
5✔
61
    ( chain_of_query
62
    ^ {sql|
63
    SELECT count(*) FROM chain 
64
    WHERE global_slot_since_genesis >= ?;
65
    |sql}
66
    )
67

68
let num_of_confirmations (module Conn : CONNECTION) ~latest_state_hash
69
    ~fork_slot =
70
  Conn.find num_of_confirmations_query (latest_state_hash, fork_slot)
1✔
71

72
let number_of_commands_since_block_query block_commands_table =
73
  Caqti_type.(t2 string int ->! t4 string int int int)
3✔
74
    ( chain_of_query
75
    ^ Printf.sprintf
3✔
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;
90
    |sql}
91
        block_commands_table )
92

93
let number_of_user_commands_since_block (module Conn : CONNECTION)
94
    ~fork_state_hash ~fork_slot =
95
  Conn.find
1✔
96
    (number_of_commands_since_block_query "blocks_user_commands")
1✔
97
    (fork_state_hash, fork_slot)
98

99
let number_of_internal_commands_since_block (module Conn : CONNECTION)
100
    ~fork_state_hash ~fork_slot =
101
  Conn.find
1✔
102
    (number_of_commands_since_block_query "blocks_internal_commands")
1✔
103
    (fork_state_hash, fork_slot)
104

105
let number_of_zkapps_commands_since_block (module Conn : CONNECTION)
106
    ~fork_state_hash ~fork_slot =
107
  Conn.find
1✔
108
    (number_of_commands_since_block_query "blocks_zkapp_commands")
1✔
109
    (fork_state_hash, fork_slot)
110

111
let last_fork_block_query =
112
  Caqti_type.(unit ->! t2 string int64)
5✔
113
    {sql|
114
    SELECT state_hash, global_slot_since_genesis FROM blocks
115
    WHERE global_slot_since_hard_fork = 0
116
    ORDER BY height DESC
117
    LIMIT 1;
118
    |sql}
119

120
let last_fork_block (module Conn : CONNECTION) =
121
  Conn.find last_fork_block_query ()
1✔
122

123
let fetch_latest_migration_history_query =
124
  Caqti_type.(unit ->? t3 string string string)
5✔
125
    {|
126
      SELECT
127
        status, protocol_version, migration_version
128
      FROM migration_history
129
      ORDER BY commit_start_at DESC
130
      LIMIT 1;
131
    |}
132

133
let fetch_latest_migration_history (module Conn : CONNECTION) =
134
  Conn.find_opt fetch_latest_migration_history_query ()
×
135

136
(* Fetches last filled block before stop transaction slot.
137

138
   Every block in mina should have internal commands since system transactions (like coinbase, fee transfer etc)
139
   are implemented as internal commands. It CAN have zero user commands and zero zkapp commands,
140
   but it should have internal commands.
141

142
   However, in context of hard fork, we want to stop including any transactions
143
   in the blocks after specified slot (called stop transaction slot). No internal, user or zkapp commands should be included in the blocks after that slot.
144
   Blocks can still be produced with no transactions, to keep chain progressing and give us confirmations but
145
   only from stop transaction slot till stop network slot, where we completely stop the chain.
146
   Knowing above we can detect last filled block by only looking at internal transactions occurrence.
147
   Therefore our fork candidate is the block with highest height that has internal transaction included in it.
148
*)
149
let fetch_last_filled_block_query =
150
  Caqti_type.(unit ->! t3 string int64 int)
5✔
151
    {sql|
152
    SELECT b.state_hash, b.global_slot_since_genesis, b.height
153
    FROM blocks b
154
    INNER JOIN blocks_internal_commands bic ON b.id = bic.block_id
155
    ORDER BY b.height DESC
156
    LIMIT 1;
157
    |sql}
158

159
let fetch_last_filled_block (module Conn : CONNECTION) =
NEW
160
  Conn.find fetch_last_filled_block_query ()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc