|
10 | 10 | from chia.consensus.blockchain import AddBlockResult, Blockchain
|
11 | 11 | from chia.consensus.difficulty_adjustment import get_next_sub_slot_iters_and_difficulty
|
12 | 12 | from chia.consensus.multiprocess_validation import PreValidationResult, pre_validate_block
|
| 13 | +from chia.full_node.consensus_store_sqlite3 import ConsensusStoreSQLite3 |
13 | 14 | from chia.types.validation_state import ValidationState
|
14 | 15 | from chia.util.errors import Err
|
15 | 16 |
|
16 | 17 |
|
17 | 18 | async def check_block_store_invariant(bc: Blockchain):
|
| 19 | + # This function checks the invariant of the sqlite database. |
| 20 | + # Only operate on the sqlite block store. |
| 21 | + if not isinstance(bc.consensus_store, ConsensusStoreSQLite3): |
| 22 | + return |
| 23 | + |
| 24 | + db_wrapper = bc.consensus_store.block_store.db_wrapper |
| 25 | + |
| 26 | + if db_wrapper.db_version == 1: |
| 27 | + return |
| 28 | + |
18 | 29 | in_chain = set()
|
19 | 30 | max_height = -1
|
20 |
| - async for height in bc.consensus_store.get_block_heights_in_main_chain(): |
21 |
| - # if this block is in-chain, ensure we haven't found another block |
22 |
| - # at this height that's also in chain. That would be an invariant |
23 |
| - # violation |
24 |
| - # make sure we don't have any duplicate heights. Each block |
25 |
| - # height can only have a single block with in_main_chain set |
26 |
| - assert height not in in_chain |
27 |
| - in_chain.add(height) |
28 |
| - max_height = max(max_height, height) |
29 |
| - |
30 |
| - # make sure every height is represented in the set |
31 |
| - assert len(in_chain) == max_height + 1 |
| 31 | + async with bc.consensus_store.block_store.transaction() as conn: |
| 32 | + async with conn.execute("SELECT height, in_main_chain FROM full_blocks") as cursor: |
| 33 | + rows = await cursor.fetchall() |
| 34 | + for row in rows: |
| 35 | + height = row[0] |
| 36 | + |
| 37 | + # if this block is in-chain, ensure we haven't found another block |
| 38 | + # at this height that's also in chain. That would be an invariant |
| 39 | + # violation |
| 40 | + if row[1]: |
| 41 | + # make sure we don't have any duplicate heights. Each block |
| 42 | + # height can only have a single block with in_main_chain set |
| 43 | + assert height not in in_chain |
| 44 | + in_chain.add(height) |
| 45 | + max_height = max(max_height, height) |
| 46 | + |
| 47 | + # make sure every height is represented in the set |
| 48 | + assert len(in_chain) == max_height + 1 |
32 | 49 |
|
33 | 50 |
|
34 | 51 | async def _validate_and_add_block(
|
|
0 commit comments