Skip to content

Commit bcb1588

Browse files
committed
simplify
1 parent 59c75ac commit bcb1588

File tree

9 files changed

+42
-44
lines changed

9 files changed

+42
-44
lines changed

benchmarks/block_ref.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async def main(db_path: Path) -> None:
7171
# make configurable
7272
reserved_cores = 4
7373
height_map = await BlockHeightMap.create(db_path.parent, db_wrapper)
74-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
74+
consensus_store = ConsensusStoreSQLite3(block_store, coin_store, height_map)
7575
blockchain = await Blockchain.create(consensus_store, DEFAULT_CONSTANTS, reserved_cores)
7676

7777
peak = blockchain.get_peak()

chia/_tests/core/full_node/ram_db.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ async def create_ram_blockchain(
2121
) -> AsyncIterator[tuple[DBWrapper2, Blockchain]]:
2222
uri = f"file:db_{random.randint(0, 99999999)}?mode=memory&cache=shared"
2323
async with DBWrapper2.managed(database=uri, uri=True, reader_count=1, db_version=2) as db_wrapper:
24-
block_store = await BlockStore.create(db_wrapper)
25-
coin_store = await CoinStore.create(db_wrapper)
26-
height_map = await BlockHeightMap.create(Path("."), db_wrapper)
27-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
24+
consensus_store = await ConsensusStoreSQLite3.create(db_wrapper, Path("."))
2825
blockchain = await Blockchain.create(consensus_store, consensus_constants, 2)
2926
try:
3027
yield db_wrapper, blockchain

chia/_tests/core/full_node/stores/test_block_store.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async def test_block_store(tmp_dir: Path, db_version: int, bt: BlockTools, use_c
7575
coin_store_2 = await CoinStore.create(db_wrapper_2)
7676
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
7777
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
78-
consensus_store = await ConsensusStoreSQLite3.create(store_2, coin_store_2, height_map)
78+
consensus_store = ConsensusStoreSQLite3(store_2, coin_store_2, height_map)
7979
bc = await Blockchain.create(consensus_store, bt.constants, 2)
8080

8181
store = await BlockStore.create(db_wrapper, use_cache=use_cache)
@@ -149,10 +149,8 @@ async def test_get_full_blocks_at(
149149

150150
async with DBConnection(2) as db_wrapper:
151151
# Use a different file for the blockchain
152-
coin_store = await CoinStore.create(db_wrapper)
153-
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
154-
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
155-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
152+
consensus_store = await ConsensusStoreSQLite3.create(db_wrapper, tmp_dir, use_cache=use_cache)
153+
block_store = consensus_store.block_store
156154
bc = await Blockchain.create(consensus_store, bt.constants, 2)
157155

158156
count = 0
@@ -178,10 +176,8 @@ async def test_get_block_records_in_range(
178176

179177
async with DBConnection(2) as db_wrapper:
180178
# Use a different file for the blockchain
181-
coin_store = await CoinStore.create(db_wrapper)
182-
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
183-
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
184-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
179+
consensus_store = await ConsensusStoreSQLite3.create(db_wrapper, tmp_dir, use_cache=use_cache)
180+
block_store = consensus_store.block_store
185181
bc = await Blockchain.create(consensus_store, bt.constants, 2)
186182

187183
count = 0
@@ -209,10 +205,8 @@ async def test_get_block_bytes_in_range_in_main_chain(
209205

210206
async with DBConnection(2) as db_wrapper:
211207
# Use a different file for the blockchain
212-
coin_store = await CoinStore.create(db_wrapper)
213-
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
214-
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
215-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
208+
consensus_store = await ConsensusStoreSQLite3.create(db_wrapper, tmp_dir, use_cache=use_cache)
209+
block_store = consensus_store.block_store
216210
bc = await Blockchain.create(consensus_store, bt.constants, 2)
217211
count = 0
218212
fork_info = ForkInfo(-1, -1, bt.constants.GENESIS_CHALLENGE)
@@ -242,7 +236,7 @@ async def test_deadlock(tmp_dir: Path, db_version: int, bt: BlockTools, use_cach
242236
coin_store_2 = await CoinStore.create(wrapper_2)
243237
store_2 = await BlockStore.create(wrapper_2)
244238
height_map = await BlockHeightMap.create(tmp_dir, wrapper_2)
245-
consensus_store = await ConsensusStoreSQLite3.create(store_2, coin_store_2, height_map)
239+
consensus_store = ConsensusStoreSQLite3(store_2, coin_store_2, height_map)
246240
bc = await Blockchain.create(consensus_store, bt.constants, 2)
247241
block_records = []
248242
for block in blocks:
@@ -271,10 +265,8 @@ async def test_rollback(bt: BlockTools, tmp_dir: Path, use_cache: bool, default_
271265

272266
async with DBConnection(2) as db_wrapper:
273267
# Use a different file for the blockchain
274-
coin_store = await CoinStore.create(db_wrapper)
275-
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
276-
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
277-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
268+
consensus_store = await ConsensusStoreSQLite3.create(db_wrapper, tmp_dir, use_cache=use_cache)
269+
block_store = consensus_store.block_store
278270
bc = await Blockchain.create(consensus_store, bt.constants, 2)
279271

280272
# insert all blocks
@@ -338,7 +330,7 @@ async def test_count_compactified_blocks(bt: BlockTools, tmp_dir: Path, db_versi
338330
coin_store = await CoinStore.create(db_wrapper)
339331
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
340332
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
341-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
333+
consensus_store = ConsensusStoreSQLite3(block_store, coin_store, height_map)
342334
bc = await Blockchain.create(consensus_store, bt.constants, 2)
343335

344336
count = await block_store.count_compactified_blocks()
@@ -360,7 +352,7 @@ async def test_count_uncompactified_blocks(bt: BlockTools, tmp_dir: Path, db_ver
360352
coin_store = await CoinStore.create(db_wrapper)
361353
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
362354
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
363-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
355+
consensus_store = ConsensusStoreSQLite3(block_store, coin_store, height_map)
364356
bc = await Blockchain.create(consensus_store, bt.constants, 2)
365357

366358
count = await block_store.count_uncompactified_blocks()
@@ -389,7 +381,7 @@ def rand_vdf_proof() -> VDFProof:
389381
coin_store = await CoinStore.create(db_wrapper)
390382
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
391383
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
392-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
384+
consensus_store = ConsensusStoreSQLite3(block_store, coin_store, height_map)
393385
bc = await Blockchain.create(consensus_store, bt.constants, 2)
394386
for block in blocks:
395387
await _validate_and_add_block(bc, block)
@@ -471,7 +463,7 @@ async def test_get_blocks_by_hash(tmp_dir: Path, bt: BlockTools, db_version: int
471463
coin_store_2 = await CoinStore.create(db_wrapper_2)
472464
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
473465
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
474-
consensus_store = await ConsensusStoreSQLite3.create(store_2, coin_store_2, height_map)
466+
consensus_store = ConsensusStoreSQLite3(store_2, coin_store_2, height_map)
475467
bc = await Blockchain.create(consensus_store, bt.constants, 2)
476468

477469
store = await BlockStore.create(db_wrapper, use_cache=use_cache)
@@ -512,7 +504,7 @@ async def test_get_block_bytes_in_range(tmp_dir: Path, bt: BlockTools, db_versio
512504
coin_store_2 = await CoinStore.create(db_wrapper_2)
513505
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
514506
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
515-
consensus_store = await ConsensusStoreSQLite3.create(store_2, coin_store_2, height_map)
507+
consensus_store = ConsensusStoreSQLite3(store_2, coin_store_2, height_map)
516508
bc = await Blockchain.create(consensus_store, bt.constants, 2)
517509

518510
await BlockStore.create(db_wrapper_2)
@@ -586,7 +578,7 @@ async def test_get_prev_hash(tmp_dir: Path, bt: BlockTools, db_version: int, use
586578
coin_store_2 = await CoinStore.create(db_wrapper_2)
587579
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
588580
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
589-
consensus_store = await ConsensusStoreSQLite3.create(store_2, coin_store_2, height_map)
581+
consensus_store = ConsensusStoreSQLite3(store_2, coin_store_2, height_map)
590582
bc = await Blockchain.create(consensus_store, bt.constants, 2)
591583

592584
store = await BlockStore.create(db_wrapper, use_cache=use_cache)

chia/_tests/core/full_node/stores/test_coin_store.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ async def test_basic_reorg(tmp_dir: Path, db_version: int, bt: BlockTools) -> No
319319
coin_store = await CoinStore.create(db_wrapper)
320320
store = await BlockStore.create(db_wrapper)
321321
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
322-
consensus_store = await ConsensusStoreSQLite3.create(store, coin_store, height_map)
322+
consensus_store = ConsensusStoreSQLite3(store, coin_store, height_map)
323323
b: Blockchain = await Blockchain.create(consensus_store, bt.constants, 2)
324324
try:
325325
records: list[Optional[CoinRecord]] = []
@@ -387,7 +387,7 @@ async def test_get_puzzle_hash(tmp_dir: Path, db_version: int, bt: BlockTools) -
387387
coin_store = await CoinStore.create(db_wrapper)
388388
store = await BlockStore.create(db_wrapper)
389389
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
390-
consensus_store = await ConsensusStoreSQLite3.create(store, coin_store, height_map)
390+
consensus_store = ConsensusStoreSQLite3(store, coin_store, height_map)
391391
b: Blockchain = await Blockchain.create(consensus_store, bt.constants, 2)
392392
for block in blocks:
393393
await _validate_and_add_block(b, block)

chia/_tests/core/test_db_conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async def test_blocks(default_1000_blocks, with_hints: bool):
6363
await hint_store1.add_hints([(h[0], h[1])])
6464

6565
height_map = await BlockHeightMap.create(Path("."), db_wrapper1)
66-
consensus_store = await ConsensusStoreSQLite3.create(block_store1, coin_store1, height_map)
66+
consensus_store = ConsensusStoreSQLite3(block_store1, coin_store1, height_map)
6767
bc = await Blockchain.create(consensus_store, test_constants, reserved_cores=0)
6868
sub_slot_iters = test_constants.SUB_SLOT_ITERS_STARTING
6969
for block in blocks:

chia/_tests/core/test_db_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ async def make_db(db_file: Path, blocks: list[FullBlock]) -> None:
142142
block_store = await BlockStore.create(db_wrapper)
143143
coin_store = await CoinStore.create(db_wrapper)
144144
height_map = await BlockHeightMap.create(Path("."), db_wrapper)
145-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
145+
consensus_store = ConsensusStoreSQLite3(block_store, coin_store, height_map)
146146
bc = await Blockchain.create(consensus_store, test_constants, reserved_cores=0)
147147
sub_slot_iters = test_constants.SUB_SLOT_ITERS_STARTING
148148
for block in blocks:

chia/_tests/util/blockchain.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ async def create_blockchain(
2626
) -> AsyncIterator[tuple[Blockchain, DBWrapper2]]:
2727
db_uri = generate_in_memory_db_uri()
2828
async with DBWrapper2.managed(database=db_uri, uri=True, reader_count=1, db_version=db_version) as wrapper:
29-
block_store = await BlockStore.create(wrapper)
30-
coin_store = await CoinStore.create(wrapper)
31-
height_map = await BlockHeightMap.create(Path("."), wrapper, None)
32-
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
29+
consensus_store = await ConsensusStoreSQLite3.create(wrapper, Path("."))
3330
bc1 = await Blockchain.create(consensus_store, constants, 3, single_threaded=True, log_coins=True)
3431
try:
3532
assert bc1.get_peak() is None

chia/full_node/consensus_store_sqlite3.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dataclasses
44
from collections.abc import AsyncIterator, Collection
55
from contextlib import AbstractAsyncContextManager
6+
from pathlib import Path
67
from types import TracebackType
78
from typing import TYPE_CHECKING, Any, Optional
89

@@ -15,6 +16,7 @@
1516
from chia.full_node.coin_store import CoinStore
1617
from chia.types.blockchain_format.coin import Coin
1718
from chia.types.coin_record import CoinRecord
19+
from chia.util.db_wrapper import DBWrapper2
1820

1921

2022
class ConsensusStoreSQLite3Writer:
@@ -71,15 +73,25 @@ class ConsensusStoreSQLite3:
7173
@classmethod
7274
async def create(
7375
cls,
74-
block_store: BlockStore,
75-
coin_store: CoinStore,
76-
height_map: BlockHeightMap,
76+
db_wrapper: DBWrapper2,
77+
blockchain_dir: Path,
78+
*,
79+
use_cache: bool = True,
80+
selected_network: Optional[str] = None,
7781
) -> ConsensusStoreSQLite3:
78-
"""Create a new ConsensusStore instance from existing sub-stores.
82+
"""Create a new ConsensusStore instance, creating all underlying sub-stores internally.
7983
80-
This factory does not create sub-stores. Construct BlockStore, CoinStore,
81-
and BlockHeightMap separately and pass them in here.
84+
Args:
85+
db_wrapper: Database wrapper to use for all stores
86+
blockchain_dir: Directory path for blockchain data (used by BlockHeightMap)
87+
use_cache: Whether to enable caching in BlockStore (default: True)
88+
selected_network: Network selection for BlockHeightMap (default: None)
8289
"""
90+
# Create underlying stores
91+
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
92+
coin_store = await CoinStore.create(db_wrapper)
93+
height_map = await BlockHeightMap.create(blockchain_dir, db_wrapper, selected_network)
94+
8395
return cls(
8496
block_store=block_store,
8597
coin_store=coin_store,

chia/full_node/full_node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ async def manage(self) -> AsyncIterator[None]:
267267
self.multiprocessing_context = multiprocessing.get_context(method=multiprocessing_start_method)
268268
selected_network = self.config.get("selected_network")
269269
height_map = await BlockHeightMap.create(self.db_path.parent, self.db_wrapper, selected_network)
270-
consensus_store = await ConsensusStoreSQLite3.create(self.block_store, self.coin_store, height_map)
270+
consensus_store = ConsensusStoreSQLite3(self.block_store, self.coin_store, height_map)
271271
self._blockchain = await Blockchain.create(
272272
consensus_store=consensus_store,
273273
consensus_constants=self.constants,

0 commit comments

Comments
 (0)