Skip to content

Commit 79173af

Browse files
committed
Add type checking
1 parent 023484c commit 79173af

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

chia/consensus/consensus_store_protocol.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
from collections.abc import Collection
44
from contextlib import AbstractAsyncContextManager
5-
from typing import Optional, Protocol
5+
from typing import Any, Optional, Protocol
66

7-
import aiosqlite
87
from chia_rs import BlockRecord, FullBlock, SubEpochChallengeSegment, SubEpochSummary
98
from chia_rs.sized_bytes import bytes32
109
from chia_rs.sized_ints import uint32, uint64
@@ -19,8 +18,7 @@ class ConsensusStoreProtocol(Protocol):
1918
the consensus-related data in the blockchain.
2019
"""
2120

22-
# Block store methods
23-
def transaction(self) -> AbstractAsyncContextManager[aiosqlite.Connection]: ...
21+
def transaction(self) -> AbstractAsyncContextManager[Any]: ...
2422

2523
async def get_block_records_close_to_peak(
2624
self, blocks_n: int
@@ -46,7 +44,6 @@ async def get_sub_epoch_challenge_segments(
4644
async def get_generator(self, header_hash: bytes32) -> Optional[bytes]: ...
4745
async def get_generators_at(self, heights: set[uint32]) -> dict[uint32, bytes]: ...
4846

49-
# Coin store methods
5047
async def get_coin_records(self, names: Collection[bytes32]) -> list[CoinRecord]: ...
5148
async def rollback_to_block(self, block_index: int) -> dict[bytes32, CoinRecord]: ...
5249
async def new_block(
@@ -60,7 +57,6 @@ async def new_block(
6057
async def get_coins_added_at_height(self, height: uint32) -> list[CoinRecord]: ...
6158
async def get_coins_removed_at_height(self, height: uint32) -> list[CoinRecord]: ...
6259

63-
# Height map methods
6460
def get_ses_heights(self) -> list[uint32]: ...
6561
def get_ses(self, height: uint32) -> SubEpochSummary: ...
6662
def contains_height(self, height: uint32) -> bool: ...

chia/full_node/consensus_store_sqlite3.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@
33
import dataclasses
44
from collections.abc import Collection
55
from contextlib import AbstractAsyncContextManager
6-
from pathlib import Path
7-
from typing import Optional
6+
from typing import TYPE_CHECKING, Optional
87

98
import aiosqlite
109
from chia_rs import BlockRecord, FullBlock, SubEpochChallengeSegment, SubEpochSummary
1110
from chia_rs.sized_bytes import bytes32
1211
from chia_rs.sized_ints import uint32, uint64
12+
from typing_extensions import Self
1313

1414
from chia.consensus.block_height_map import BlockHeightMap
15-
from chia.consensus.consensus_store_protocol import ConsensusStoreProtocol
1615
from chia.full_node.block_store import BlockStore
1716
from chia.full_node.coin_store import CoinStore
1817
from chia.types.blockchain_format.coin import Coin
1918
from chia.types.coin_record import CoinRecord
20-
from chia.util.db_wrapper import DBWrapper2
2119

2220

2321
@dataclasses.dataclass
24-
class ConsensusStoreSQLite3(ConsensusStoreProtocol):
22+
class ConsensusStoreSQLite3:
2523
"""
2624
Consensus store that combines block_store, coin_store, and height_map functionality.
2725
"""
@@ -36,7 +34,7 @@ async def create(
3634
block_store: BlockStore,
3735
coin_store: CoinStore,
3836
height_map: BlockHeightMap,
39-
) -> ConsensusStoreSQLite3:
37+
) -> Self:
4038
"""Create a new ConsensusStore instance from existing sub-stores.
4139
4240
This factory does not create sub-stores. Construct BlockStore, CoinStore,
@@ -158,23 +156,12 @@ async def maybe_flush_height_map(self) -> None:
158156
await self.height_map.maybe_flush()
159157

160158

161-
async def build_consensus_store(
162-
db_wrapper: DBWrapper2,
163-
blockchain_dir: Path,
164-
selected_network: Optional[str] = None,
165-
*,
166-
use_cache: bool = True,
167-
) -> ConsensusStoreSQLite3:
168-
"""Convenience utility to construct a ConsensusStore from DB and path.
159+
if TYPE_CHECKING:
160+
# here we validate that ConsensusStoreSQLite3 implements ConsensusStoreProtocol
161+
from typing import cast
169162

170-
This keeps ConsensusStore.create minimal (only accepts sub-stores) while still
171-
providing a one-stop helper for callers that need to create the sub-stores.
172-
"""
173-
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
174-
coin_store = await CoinStore.create(db_wrapper)
175-
height_map = await BlockHeightMap.create(blockchain_dir, db_wrapper, selected_network)
176-
return await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
163+
from chia.consensus.consensus_store_protocol import ConsensusStoreProtocol
177164

165+
def _protocol_check(_: ConsensusStoreProtocol): ...
178166

179-
# Backwards-compatible alias
180-
ConsensusStore = ConsensusStoreSQLite3
167+
_protocol_check(cast(ConsensusStoreSQLite3, None))

0 commit comments

Comments
 (0)