Skip to content

Roll CoinStore, BlockStore and BlockHeightMap into ConsensusStore #19949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion benchmarks/block_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from chia.consensus.block_height_map import BlockHeightMap
from chia.consensus.blockchain import Blockchain
from chia.consensus.consensus_store import ConsensusStore
from chia.consensus.default_constants import DEFAULT_CONSTANTS
from chia.consensus.get_block_generator import get_block_generator
from chia.full_node.block_store import BlockStore
Expand Down Expand Up @@ -70,7 +71,8 @@ async def main(db_path: Path) -> None:
# make configurable
reserved_cores = 4
height_map = await BlockHeightMap.create(db_path.parent, db_wrapper)
blockchain = await Blockchain.create(coin_store, block_store, height_map, DEFAULT_CONSTANTS, reserved_cores)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
blockchain = await Blockchain.create(consensus_store, DEFAULT_CONSTANTS, reserved_cores)

peak = blockchain.get_peak()
assert peak is not None
Expand Down
4 changes: 3 additions & 1 deletion chia/_tests/core/full_node/ram_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from chia.consensus.block_height_map import BlockHeightMap
from chia.consensus.blockchain import Blockchain
from chia.consensus.consensus_store import ConsensusStore
from chia.full_node.block_store import BlockStore
from chia.full_node.coin_store import CoinStore
from chia.util.db_wrapper import DBWrapper2
Expand All @@ -23,7 +24,8 @@ async def create_ram_blockchain(
block_store = await BlockStore.create(db_wrapper)
coin_store = await CoinStore.create(db_wrapper)
height_map = await BlockHeightMap.create(Path("."), db_wrapper)
blockchain = await Blockchain.create(coin_store, block_store, height_map, consensus_constants, 2)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
blockchain = await Blockchain.create(consensus_store, consensus_constants, 2)
try:
yield db_wrapper, blockchain
finally:
Expand Down
37 changes: 25 additions & 12 deletions chia/_tests/core/full_node/stores/test_block_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from chia.consensus.block_body_validation import ForkInfo
from chia.consensus.block_height_map import BlockHeightMap
from chia.consensus.blockchain import AddBlockResult, Blockchain
from chia.consensus.consensus_store import ConsensusStore
from chia.consensus.default_constants import DEFAULT_CONSTANTS
from chia.consensus.full_block_to_block_record import header_block_to_sub_block_record
from chia.full_node.block_store import BlockStore
Expand Down Expand Up @@ -74,7 +75,8 @@ async def test_block_store(tmp_dir: Path, db_version: int, bt: BlockTools, use_c
coin_store_2 = await CoinStore.create(db_wrapper_2)
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
bc = await Blockchain.create(coin_store_2, store_2, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)

store = await BlockStore.create(db_wrapper, use_cache=use_cache)
await BlockStore.create(db_wrapper_2)
Expand Down Expand Up @@ -150,7 +152,8 @@ async def test_get_full_blocks_at(
coin_store = await CoinStore.create(db_wrapper)
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)

count = 0
fork_info = ForkInfo(-1, -1, bt.constants.GENESIS_CHALLENGE)
Expand Down Expand Up @@ -178,7 +181,8 @@ async def test_get_block_records_in_range(
coin_store = await CoinStore.create(db_wrapper)
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)

count = 0
fork_info = ForkInfo(-1, -1, bt.constants.GENESIS_CHALLENGE)
Expand Down Expand Up @@ -208,7 +212,8 @@ async def test_get_block_bytes_in_range_in_main_chain(
coin_store = await CoinStore.create(db_wrapper)
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)
count = 0
fork_info = ForkInfo(-1, -1, bt.constants.GENESIS_CHALLENGE)
for b1, b2 in zip(blocks, alt_blocks):
Expand Down Expand Up @@ -237,7 +242,8 @@ async def test_deadlock(tmp_dir: Path, db_version: int, bt: BlockTools, use_cach
coin_store_2 = await CoinStore.create(wrapper_2)
store_2 = await BlockStore.create(wrapper_2)
height_map = await BlockHeightMap.create(tmp_dir, wrapper_2)
bc = await Blockchain.create(coin_store_2, store_2, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)
block_records = []
for block in blocks:
await _validate_and_add_block(bc, block)
Expand Down Expand Up @@ -268,7 +274,8 @@ async def test_rollback(bt: BlockTools, tmp_dir: Path, use_cache: bool, default_
coin_store = await CoinStore.create(db_wrapper)
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)

# insert all blocks
count = 0
Expand Down Expand Up @@ -331,7 +338,8 @@ async def test_count_compactified_blocks(bt: BlockTools, tmp_dir: Path, db_versi
coin_store = await CoinStore.create(db_wrapper)
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)

count = await block_store.count_compactified_blocks()
assert count == 0
Expand All @@ -352,7 +360,8 @@ async def test_count_uncompactified_blocks(bt: BlockTools, tmp_dir: Path, db_ver
coin_store = await CoinStore.create(db_wrapper)
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)

count = await block_store.count_uncompactified_blocks()
assert count == 0
Expand Down Expand Up @@ -380,7 +389,8 @@ def rand_vdf_proof() -> VDFProof:
coin_store = await CoinStore.create(db_wrapper)
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
bc = await Blockchain.create(coin_store, block_store, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)
for block in blocks:
await _validate_and_add_block(bc, block)

Expand Down Expand Up @@ -461,7 +471,8 @@ async def test_get_blocks_by_hash(tmp_dir: Path, bt: BlockTools, db_version: int
coin_store_2 = await CoinStore.create(db_wrapper_2)
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
bc = await Blockchain.create(coin_store_2, store_2, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)

store = await BlockStore.create(db_wrapper, use_cache=use_cache)
await BlockStore.create(db_wrapper_2)
Expand Down Expand Up @@ -501,7 +512,8 @@ async def test_get_block_bytes_in_range(tmp_dir: Path, bt: BlockTools, db_versio
coin_store_2 = await CoinStore.create(db_wrapper_2)
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
bc = await Blockchain.create(coin_store_2, store_2, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)

await BlockStore.create(db_wrapper_2)

Expand Down Expand Up @@ -574,7 +586,8 @@ async def test_get_prev_hash(tmp_dir: Path, bt: BlockTools, db_version: int, use
coin_store_2 = await CoinStore.create(db_wrapper_2)
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
bc = await Blockchain.create(coin_store_2, store_2, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
bc = await Blockchain.create(consensus_store, bt.constants, 2)

store = await BlockStore.create(db_wrapper, use_cache=use_cache)
await BlockStore.create(db_wrapper_2)
Expand Down
7 changes: 5 additions & 2 deletions chia/_tests/core/full_node/stores/test_coin_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from chia.consensus.block_rewards import calculate_base_farmer_reward, calculate_pool_reward
from chia.consensus.blockchain import AddBlockResult, Blockchain
from chia.consensus.coinbase import create_farmer_coin, create_pool_coin
from chia.consensus.consensus_store import ConsensusStore
from chia.full_node.block_store import BlockStore
from chia.full_node.coin_store import CoinStore
from chia.full_node.hint_store import HintStore
Expand Down Expand Up @@ -318,7 +319,8 @@ async def test_basic_reorg(tmp_dir: Path, db_version: int, bt: BlockTools) -> No
coin_store = await CoinStore.create(db_wrapper)
store = await BlockStore.create(db_wrapper)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
b: Blockchain = await Blockchain.create(coin_store, store, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(store, coin_store, height_map)
b: Blockchain = await Blockchain.create(consensus_store, bt.constants, 2)
try:
records: list[Optional[CoinRecord]] = []

Expand Down Expand Up @@ -385,7 +387,8 @@ async def test_get_puzzle_hash(tmp_dir: Path, db_version: int, bt: BlockTools) -
coin_store = await CoinStore.create(db_wrapper)
store = await BlockStore.create(db_wrapper)
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
b: Blockchain = await Blockchain.create(coin_store, store, height_map, bt.constants, 2)
consensus_store = await ConsensusStore.create(store, coin_store, height_map)
b: Blockchain = await Blockchain.create(consensus_store, bt.constants, 2)
for block in blocks:
await _validate_and_add_block(b, block)
peak = b.get_peak()
Expand Down
4 changes: 3 additions & 1 deletion chia/_tests/core/test_db_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from chia.consensus.block_body_validation import ForkInfo
from chia.consensus.block_height_map import BlockHeightMap
from chia.consensus.blockchain import Blockchain
from chia.consensus.consensus_store import ConsensusStore
from chia.consensus.multiprocess_validation import PreValidationResult
from chia.full_node.block_store import BlockStore
from chia.full_node.coin_store import CoinStore
Expand Down Expand Up @@ -62,7 +63,8 @@ async def test_blocks(default_1000_blocks, with_hints: bool):
await hint_store1.add_hints([(h[0], h[1])])

height_map = await BlockHeightMap.create(Path("."), db_wrapper1)
bc = await Blockchain.create(coin_store1, block_store1, height_map, test_constants, reserved_cores=0)
consensus_store = await ConsensusStore.create(block_store1, coin_store1, height_map)
bc = await Blockchain.create(consensus_store, test_constants, reserved_cores=0)
sub_slot_iters = test_constants.SUB_SLOT_ITERS_STARTING
for block in blocks:
if block.height != 0 and len(block.finished_sub_slots) > 0:
Expand Down
4 changes: 3 additions & 1 deletion chia/_tests/core/test_db_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from chia.consensus.block_body_validation import ForkInfo
from chia.consensus.block_height_map import BlockHeightMap
from chia.consensus.blockchain import Blockchain
from chia.consensus.consensus_store import ConsensusStore
from chia.consensus.default_constants import DEFAULT_CONSTANTS
from chia.consensus.multiprocess_validation import PreValidationResult
from chia.full_node.block_store import BlockStore
Expand Down Expand Up @@ -142,7 +143,8 @@ async def make_db(db_file: Path, blocks: list[FullBlock]) -> None:
coin_store = await CoinStore.create(db_wrapper)
height_map = await BlockHeightMap.create(Path("."), db_wrapper)

bc = await Blockchain.create(coin_store, block_store, height_map, test_constants, reserved_cores=0)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
bc = await Blockchain.create(consensus_store, test_constants, reserved_cores=0)
sub_slot_iters = test_constants.SUB_SLOT_ITERS_STARTING
for block in blocks:
if block.height != 0 and len(block.finished_sub_slots) > 0:
Expand Down
10 changes: 6 additions & 4 deletions chia/_tests/util/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from chia.consensus.block_height_map import BlockHeightMap
from chia.consensus.blockchain import Blockchain
from chia.consensus.consensus_store import ConsensusStore
from chia.full_node.block_store import BlockStore
from chia.full_node.coin_store import CoinStore
from chia.simulator.block_tools import BlockTools
Expand All @@ -25,11 +26,12 @@ async def create_blockchain(
) -> AsyncIterator[tuple[Blockchain, DBWrapper2]]:
db_uri = generate_in_memory_db_uri()
async with DBWrapper2.managed(database=db_uri, uri=True, reader_count=1, db_version=db_version) as wrapper:

block_store = await BlockStore.create(wrapper)
coin_store = await CoinStore.create(wrapper)
store = await BlockStore.create(wrapper)
path = Path(".")
height_map = await BlockHeightMap.create(path, wrapper)
bc1 = await Blockchain.create(coin_store, store, height_map, constants, 3, single_threaded=True, log_coins=True)
height_map = await BlockHeightMap.create(Path("."), wrapper, None)
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
bc1 = await Blockchain.create(consensus_store, constants, 3, single_threaded=True, log_coins=True)
try:
assert bc1.get_peak() is None
yield bc1, wrapper
Expand Down
Loading
Loading