Skip to content

Commit 27bda67

Browse files
committed
ConsensusStore
1 parent 4e6038b commit 27bda67

File tree

11 files changed

+351
-87
lines changed

11 files changed

+351
-87
lines changed

benchmarks/block_ref.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from chia.consensus.block_height_map import BlockHeightMap
1717
from chia.consensus.blockchain import Blockchain
18+
from chia.consensus.consensus_store import ConsensusStore
1819
from chia.consensus.default_constants import DEFAULT_CONSTANTS
1920
from chia.consensus.get_block_generator import get_block_generator
2021
from chia.full_node.block_store import BlockStore
@@ -70,7 +71,8 @@ async def main(db_path: Path) -> None:
7071
# make configurable
7172
reserved_cores = 4
7273
height_map = await BlockHeightMap.create(db_path.parent, db_wrapper)
73-
blockchain = await Blockchain.create(coin_store, block_store, height_map, DEFAULT_CONSTANTS, reserved_cores)
74+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
75+
blockchain = await Blockchain.create(consensus_store, DEFAULT_CONSTANTS, reserved_cores)
7476

7577
peak = blockchain.get_peak()
7678
assert peak is not None

chia/_tests/core/full_node/ram_db.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from chia.consensus.block_height_map import BlockHeightMap
1111
from chia.consensus.blockchain import Blockchain
12+
from chia.consensus.consensus_store import ConsensusStore
1213
from chia.full_node.block_store import BlockStore
1314
from chia.full_node.coin_store import CoinStore
1415
from chia.util.db_wrapper import DBWrapper2
@@ -23,7 +24,8 @@ async def create_ram_blockchain(
2324
block_store = await BlockStore.create(db_wrapper)
2425
coin_store = await CoinStore.create(db_wrapper)
2526
height_map = await BlockHeightMap.create(Path("."), db_wrapper)
26-
blockchain = await Blockchain.create(coin_store, block_store, height_map, consensus_constants, 2)
27+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
28+
blockchain = await Blockchain.create(consensus_store, consensus_constants, 2)
2729
try:
2830
yield db_wrapper, blockchain
2931
finally:

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from chia.consensus.block_body_validation import ForkInfo
2121
from chia.consensus.block_height_map import BlockHeightMap
2222
from chia.consensus.blockchain import AddBlockResult, Blockchain
23+
from chia.consensus.consensus_store import ConsensusStore
2324
from chia.consensus.default_constants import DEFAULT_CONSTANTS
2425
from chia.consensus.full_block_to_block_record import header_block_to_sub_block_record
2526
from chia.full_node.block_store import BlockStore
@@ -74,7 +75,8 @@ async def test_block_store(tmp_dir: Path, db_version: int, bt: BlockTools, use_c
7475
coin_store_2 = await CoinStore.create(db_wrapper_2)
7576
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
7677
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
77-
bc = await Blockchain.create(coin_store_2, store_2, height_map, bt.constants, 2)
78+
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
79+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
7880

7981
store = await BlockStore.create(db_wrapper, use_cache=use_cache)
8082
await BlockStore.create(db_wrapper_2)
@@ -150,7 +152,8 @@ async def test_get_full_blocks_at(
150152
coin_store = await CoinStore.create(db_wrapper)
151153
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
152154
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
153-
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
155+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
156+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
154157

155158
count = 0
156159
fork_info = ForkInfo(-1, -1, bt.constants.GENESIS_CHALLENGE)
@@ -178,7 +181,8 @@ async def test_get_block_records_in_range(
178181
coin_store = await CoinStore.create(db_wrapper)
179182
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
180183
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
181-
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
184+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
185+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
182186

183187
count = 0
184188
fork_info = ForkInfo(-1, -1, bt.constants.GENESIS_CHALLENGE)
@@ -208,7 +212,8 @@ async def test_get_block_bytes_in_range_in_main_chain(
208212
coin_store = await CoinStore.create(db_wrapper)
209213
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
210214
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
211-
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
215+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
216+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
212217
count = 0
213218
fork_info = ForkInfo(-1, -1, bt.constants.GENESIS_CHALLENGE)
214219
for b1, b2 in zip(blocks, alt_blocks):
@@ -237,7 +242,8 @@ async def test_deadlock(tmp_dir: Path, db_version: int, bt: BlockTools, use_cach
237242
coin_store_2 = await CoinStore.create(wrapper_2)
238243
store_2 = await BlockStore.create(wrapper_2)
239244
height_map = await BlockHeightMap.create(tmp_dir, wrapper_2)
240-
bc = await Blockchain.create(coin_store_2, store_2, height_map, bt.constants, 2)
245+
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
246+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
241247
block_records = []
242248
for block in blocks:
243249
await _validate_and_add_block(bc, block)
@@ -268,7 +274,8 @@ async def test_rollback(bt: BlockTools, tmp_dir: Path, use_cache: bool, default_
268274
coin_store = await CoinStore.create(db_wrapper)
269275
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
270276
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
271-
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
277+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
278+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
272279

273280
# insert all blocks
274281
count = 0
@@ -331,7 +338,8 @@ async def test_count_compactified_blocks(bt: BlockTools, tmp_dir: Path, db_versi
331338
coin_store = await CoinStore.create(db_wrapper)
332339
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
333340
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
334-
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
341+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
342+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
335343

336344
count = await block_store.count_compactified_blocks()
337345
assert count == 0
@@ -352,7 +360,8 @@ async def test_count_uncompactified_blocks(bt: BlockTools, tmp_dir: Path, db_ver
352360
coin_store = await CoinStore.create(db_wrapper)
353361
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
354362
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
355-
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
363+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
364+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
356365

357366
count = await block_store.count_uncompactified_blocks()
358367
assert count == 0
@@ -380,7 +389,8 @@ def rand_vdf_proof() -> VDFProof:
380389
coin_store = await CoinStore.create(db_wrapper)
381390
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
382391
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
383-
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
392+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
393+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
384394
for block in blocks:
385395
await _validate_and_add_block(bc, block)
386396

@@ -461,7 +471,8 @@ async def test_get_blocks_by_hash(tmp_dir: Path, bt: BlockTools, db_version: int
461471
coin_store_2 = await CoinStore.create(db_wrapper_2)
462472
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
463473
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
464-
bc = await Blockchain.create(coin_store_2, store_2, height_map, bt.constants, 2)
474+
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
475+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
465476

466477
store = await BlockStore.create(db_wrapper, use_cache=use_cache)
467478
await BlockStore.create(db_wrapper_2)
@@ -501,7 +512,8 @@ async def test_get_block_bytes_in_range(tmp_dir: Path, bt: BlockTools, db_versio
501512
coin_store_2 = await CoinStore.create(db_wrapper_2)
502513
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
503514
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
504-
bc = await Blockchain.create(coin_store_2, store_2, height_map, bt.constants, 2)
515+
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
516+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
505517

506518
await BlockStore.create(db_wrapper_2)
507519

@@ -574,7 +586,8 @@ async def test_get_prev_hash(tmp_dir: Path, bt: BlockTools, db_version: int, use
574586
coin_store_2 = await CoinStore.create(db_wrapper_2)
575587
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
576588
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
577-
bc = await Blockchain.create(coin_store_2, store_2, height_map, bt.constants, 2)
589+
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
590+
bc = await Blockchain.create(consensus_store, bt.constants, 2)
578591

579592
store = await BlockStore.create(db_wrapper, use_cache=use_cache)
580593
await BlockStore.create(db_wrapper_2)

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from chia.consensus.block_rewards import calculate_base_farmer_reward, calculate_pool_reward
2121
from chia.consensus.blockchain import AddBlockResult, Blockchain
2222
from chia.consensus.coinbase import create_farmer_coin, create_pool_coin
23+
from chia.consensus.consensus_store import ConsensusStore
2324
from chia.full_node.block_store import BlockStore
2425
from chia.full_node.coin_store import CoinStore
2526
from chia.full_node.hint_store import HintStore
@@ -318,7 +319,8 @@ async def test_basic_reorg(tmp_dir: Path, db_version: int, bt: BlockTools) -> No
318319
coin_store = await CoinStore.create(db_wrapper)
319320
store = await BlockStore.create(db_wrapper)
320321
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
321-
b: Blockchain = await Blockchain.create(coin_store, store, height_map, bt.constants, 2)
322+
consensus_store = await ConsensusStore.create(store, coin_store, height_map)
323+
b: Blockchain = await Blockchain.create(consensus_store, bt.constants, 2)
322324
try:
323325
records: list[Optional[CoinRecord]] = []
324326

@@ -385,7 +387,8 @@ async def test_get_puzzle_hash(tmp_dir: Path, db_version: int, bt: BlockTools) -
385387
coin_store = await CoinStore.create(db_wrapper)
386388
store = await BlockStore.create(db_wrapper)
387389
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
388-
b: Blockchain = await Blockchain.create(coin_store, store, height_map, bt.constants, 2)
390+
consensus_store = await ConsensusStore.create(store, coin_store, height_map)
391+
b: Blockchain = await Blockchain.create(consensus_store, bt.constants, 2)
389392
for block in blocks:
390393
await _validate_and_add_block(b, block)
391394
peak = b.get_peak()

chia/_tests/core/test_db_conversion.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from chia.consensus.block_body_validation import ForkInfo
1313
from chia.consensus.block_height_map import BlockHeightMap
1414
from chia.consensus.blockchain import Blockchain
15+
from chia.consensus.consensus_store import ConsensusStore
1516
from chia.consensus.multiprocess_validation import PreValidationResult
1617
from chia.full_node.block_store import BlockStore
1718
from chia.full_node.coin_store import CoinStore
@@ -62,7 +63,8 @@ async def test_blocks(default_1000_blocks, with_hints: bool):
6263
await hint_store1.add_hints([(h[0], h[1])])
6364

6465
height_map = await BlockHeightMap.create(Path("."), db_wrapper1)
65-
bc = await Blockchain.create(coin_store1, block_store1, height_map, test_constants, reserved_cores=0)
66+
consensus_store = await ConsensusStore.create(block_store1, coin_store1, height_map)
67+
bc = await Blockchain.create(consensus_store, test_constants, reserved_cores=0)
6668
sub_slot_iters = test_constants.SUB_SLOT_ITERS_STARTING
6769
for block in blocks:
6870
if block.height != 0 and len(block.finished_sub_slots) > 0:

chia/_tests/core/test_db_validation.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from chia.consensus.block_body_validation import ForkInfo
1616
from chia.consensus.block_height_map import BlockHeightMap
1717
from chia.consensus.blockchain import Blockchain
18+
from chia.consensus.consensus_store import ConsensusStore
1819
from chia.consensus.default_constants import DEFAULT_CONSTANTS
1920
from chia.consensus.multiprocess_validation import PreValidationResult
2021
from chia.full_node.block_store import BlockStore
@@ -142,7 +143,8 @@ async def make_db(db_file: Path, blocks: list[FullBlock]) -> None:
142143
coin_store = await CoinStore.create(db_wrapper)
143144
height_map = await BlockHeightMap.create(Path("."), db_wrapper)
144145

145-
bc = await Blockchain.create(coin_store, block_store, height_map, test_constants, reserved_cores=0)
146+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
147+
bc = await Blockchain.create(consensus_store, test_constants, reserved_cores=0)
146148
sub_slot_iters = test_constants.SUB_SLOT_ITERS_STARTING
147149
for block in blocks:
148150
if block.height != 0 and len(block.finished_sub_slots) > 0:

chia/_tests/util/blockchain.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from chia.consensus.block_height_map import BlockHeightMap
1414
from chia.consensus.blockchain import Blockchain
15+
from chia.consensus.consensus_store import ConsensusStore
1516
from chia.full_node.block_store import BlockStore
1617
from chia.full_node.coin_store import CoinStore
1718
from chia.simulator.block_tools import BlockTools
@@ -25,11 +26,12 @@ async def create_blockchain(
2526
) -> AsyncIterator[tuple[Blockchain, DBWrapper2]]:
2627
db_uri = generate_in_memory_db_uri()
2728
async with DBWrapper2.managed(database=db_uri, uri=True, reader_count=1, db_version=db_version) as wrapper:
29+
30+
block_store = await BlockStore.create(wrapper)
2831
coin_store = await CoinStore.create(wrapper)
29-
store = await BlockStore.create(wrapper)
30-
path = Path(".")
31-
height_map = await BlockHeightMap.create(path, wrapper)
32-
bc1 = await Blockchain.create(coin_store, store, height_map, constants, 3, single_threaded=True, log_coins=True)
32+
height_map = await BlockHeightMap.create(Path("."), wrapper, None)
33+
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
34+
bc1 = await Blockchain.create(consensus_store, constants, 3, single_threaded=True, log_coins=True)
3335
try:
3436
assert bc1.get_peak() is None
3537
yield bc1, wrapper

0 commit comments

Comments
 (0)