Skip to content

Commit e7db247

Browse files
committed
ConsensusStoreSQLite3
1 parent 27bda67 commit e7db247

File tree

10 files changed

+56
-43
lines changed

10 files changed

+56
-43
lines changed

benchmarks/block_ref.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
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
1918
from chia.consensus.default_constants import DEFAULT_CONSTANTS
2019
from chia.consensus.get_block_generator import get_block_generator
2120
from chia.full_node.block_store import BlockStore
2221
from chia.full_node.coin_store import CoinStore
22+
from chia.full_node.consensus_store_sqlite3 import ConsensusStoreSQLite3
2323
from chia.types.blockchain_format.serialized_program import SerializedProgram
2424
from chia.util.db_version import lookup_db_version
2525
from chia.util.db_wrapper import DBWrapper2
@@ -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 ConsensusStore.create(block_store, coin_store, height_map)
74+
consensus_store = await ConsensusStoreSQLite3.create(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/blockchain/test_blockchain.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,7 +2075,8 @@ async def test_timelock_conditions(
20752075

20762076
if expected == AddBlockResult.NEW_PEAK:
20772077
# ensure coin was in fact spent
2078-
c = await b.coin_store.get_coin_record(coin.name())
2078+
recs = await b.consensus_store.get_coin_records([coin.name()])
2079+
c = recs[0] if len(recs) > 0 else None
20792080
assert c is not None and c.spent
20802081

20812082
@pytest.mark.anyio
@@ -2285,10 +2286,12 @@ async def test_ephemeral_timelock(
22852286

22862287
if expected == AddBlockResult.NEW_PEAK:
22872288
# ensure coin1 was in fact spent
2288-
c = await b.coin_store.get_coin_record(coin1.name())
2289+
recs1 = await b.consensus_store.get_coin_records([coin1.name()])
2290+
c = recs1[0] if len(recs1) > 0 else None
22892291
assert c is not None and c.spent
22902292
# ensure coin2 was NOT spent
2291-
c = await b.coin_store.get_coin_record(coin2.name())
2293+
recs2 = await b.consensus_store.get_coin_records([coin2.name()])
2294+
c = recs2[0] if len(recs2) > 0 else None
22922295
assert c is not None and not c.spent
22932296

22942297
@pytest.mark.anyio
@@ -3102,9 +3105,11 @@ async def test_double_spent_in_reorg(self, empty_blockchain: Blockchain, bt: Blo
31023105
)
31033106

31043107
# ephemeral coin is spent
3105-
first_coin = await b.coin_store.get_coin_record(new_coin.name())
3108+
recs_first = await b.consensus_store.get_coin_records([new_coin.name()])
3109+
first_coin = recs_first[0] if len(recs_first) > 0 else None
31063110
assert first_coin is not None and first_coin.spent
3107-
second_coin = await b.coin_store.get_coin_record(tx_2.additions()[0].name())
3111+
recs_second = await b.consensus_store.get_coin_records([tx_2.additions()[0].name()])
3112+
second_coin = recs_second[0] if len(recs_second) > 0 else None
31083113
assert second_coin is not None and not second_coin.spent
31093114

31103115
farmer_coin = create_farmer_coin(
@@ -3120,7 +3125,8 @@ async def test_double_spent_in_reorg(self, empty_blockchain: Blockchain, bt: Blo
31203125
)
31213126
await _validate_and_add_block(b, blocks_reorg[-1])
31223127

3123-
farmer_coin_record = await b.coin_store.get_coin_record(farmer_coin.name())
3128+
recs_farmer = await b.consensus_store.get_coin_records([farmer_coin.name()])
3129+
farmer_coin_record = recs_farmer[0] if len(recs_farmer) > 0 else None
31243130
assert farmer_coin_record is not None and farmer_coin_record.spent
31253131

31263132
@pytest.mark.anyio
@@ -3875,11 +3881,13 @@ async def test_chain_failed_rollback(empty_blockchain: Blockchain, bt: BlockTool
38753881
await _validate_and_add_block(b, block, expected_result=AddBlockResult.ADDED_AS_ORPHAN, fork_info=fork_info)
38763882

38773883
# Incorrectly set the height as spent in DB to trigger an error
3878-
print(f"{await b.coin_store.get_coin_record(spend_bundle.coin_spends[0].coin.name())}")
3884+
recs_dbg1 = await b.consensus_store.get_coin_records([spend_bundle.coin_spends[0].coin.name()])
3885+
print(f"{recs_dbg1[0] if len(recs_dbg1) > 0 else None}")
38793886
print(spend_bundle.coin_spends[0].coin.name())
3880-
# await b.coin_store._set_spent([spend_bundle.coin_spends[0].coin.name()], 8)
3881-
await b.coin_store.rollback_to_block(2)
3882-
print(f"{await b.coin_store.get_coin_record(spend_bundle.coin_spends[0].coin.name())}")
3887+
# await b.consensus_store._set_spent([spend_bundle.coin_spends[0].coin.name()], 8)
3888+
await b.consensus_store.rollback_to_block(2)
3889+
recs_dbg2 = await b.consensus_store.get_coin_records([spend_bundle.coin_spends[0].coin.name()])
3890+
print(f"{recs_dbg2[0] if len(recs_dbg2) > 0 else None}")
38833891

38843892
fork_block = blocks_reorg_chain[10 - 1]
38853893
# fork_info = ForkInfo(fork_block.height, fork_block.height, fork_block.header_hash)
@@ -4208,7 +4216,9 @@ async def get_fork_info(blockchain: Blockchain, block: FullBlock, peak: BlockRec
42084216
counter = 0
42094217
start = time.monotonic()
42104218
for height in range(fork_info.fork_height + 1, block.height):
4211-
fork_block: Optional[FullBlock] = await blockchain.block_store.get_full_block(fork_chain[uint32(height)])
4219+
fork_block: Optional[FullBlock] = await blockchain.consensus_store.get_full_block(
4220+
fork_chain[uint32(height)]
4221+
)
42124222
assert fork_block is not None
42134223
assert fork_block.height - 1 == fork_info.peak_height
42144224
assert fork_block.height == 0 or fork_block.prev_header_hash == fork_info.peak_hash

chia/_tests/core/full_node/ram_db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
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
1312
from chia.full_node.block_store import BlockStore
1413
from chia.full_node.coin_store import CoinStore
14+
from chia.full_node.consensus_store_sqlite3 import ConsensusStoreSQLite3
1515
from chia.util.db_wrapper import DBWrapper2
1616

1717

@@ -24,7 +24,7 @@ async def create_ram_blockchain(
2424
block_store = await BlockStore.create(db_wrapper)
2525
coin_store = await CoinStore.create(db_wrapper)
2626
height_map = await BlockHeightMap.create(Path("."), db_wrapper)
27-
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
27+
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
2828
blockchain = await Blockchain.create(consensus_store, consensus_constants, 2)
2929
try:
3030
yield db_wrapper, blockchain

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
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
2423
from chia.consensus.default_constants import DEFAULT_CONSTANTS
2524
from chia.consensus.full_block_to_block_record import header_block_to_sub_block_record
2625
from chia.full_node.block_store import BlockStore
2726
from chia.full_node.coin_store import CoinStore
27+
from chia.full_node.consensus_store_sqlite3 import ConsensusStoreSQLite3
2828
from chia.full_node.full_block_utils import GeneratorBlockInfo
2929
from chia.simulator.block_tools import BlockTools
3030
from chia.simulator.wallet_tools import WalletTool
@@ -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 ConsensusStore.create(store_2, coin_store_2, height_map)
78+
consensus_store = await ConsensusStoreSQLite3.create(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)
@@ -152,7 +152,7 @@ async def test_get_full_blocks_at(
152152
coin_store = await CoinStore.create(db_wrapper)
153153
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
154154
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
155-
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
155+
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
156156
bc = await Blockchain.create(consensus_store, bt.constants, 2)
157157

158158
count = 0
@@ -181,7 +181,7 @@ async def test_get_block_records_in_range(
181181
coin_store = await CoinStore.create(db_wrapper)
182182
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
183183
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
184-
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
184+
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
185185
bc = await Blockchain.create(consensus_store, bt.constants, 2)
186186

187187
count = 0
@@ -212,7 +212,7 @@ async def test_get_block_bytes_in_range_in_main_chain(
212212
coin_store = await CoinStore.create(db_wrapper)
213213
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
214214
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
215-
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
215+
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
216216
bc = await Blockchain.create(consensus_store, bt.constants, 2)
217217
count = 0
218218
fork_info = ForkInfo(-1, -1, bt.constants.GENESIS_CHALLENGE)
@@ -242,7 +242,7 @@ async def test_deadlock(tmp_dir: Path, db_version: int, bt: BlockTools, use_cach
242242
coin_store_2 = await CoinStore.create(wrapper_2)
243243
store_2 = await BlockStore.create(wrapper_2)
244244
height_map = await BlockHeightMap.create(tmp_dir, wrapper_2)
245-
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
245+
consensus_store = await ConsensusStoreSQLite3.create(store_2, coin_store_2, height_map)
246246
bc = await Blockchain.create(consensus_store, bt.constants, 2)
247247
block_records = []
248248
for block in blocks:
@@ -274,7 +274,7 @@ async def test_rollback(bt: BlockTools, tmp_dir: Path, use_cache: bool, default_
274274
coin_store = await CoinStore.create(db_wrapper)
275275
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
276276
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
277-
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
277+
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
278278
bc = await Blockchain.create(consensus_store, bt.constants, 2)
279279

280280
# insert all blocks
@@ -338,7 +338,7 @@ async def test_count_compactified_blocks(bt: BlockTools, tmp_dir: Path, db_versi
338338
coin_store = await CoinStore.create(db_wrapper)
339339
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
340340
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
341-
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
341+
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
342342
bc = await Blockchain.create(consensus_store, bt.constants, 2)
343343

344344
count = await block_store.count_compactified_blocks()
@@ -360,7 +360,7 @@ async def test_count_uncompactified_blocks(bt: BlockTools, tmp_dir: Path, db_ver
360360
coin_store = await CoinStore.create(db_wrapper)
361361
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
362362
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
363-
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
363+
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
364364
bc = await Blockchain.create(consensus_store, bt.constants, 2)
365365

366366
count = await block_store.count_uncompactified_blocks()
@@ -389,7 +389,7 @@ def rand_vdf_proof() -> VDFProof:
389389
coin_store = await CoinStore.create(db_wrapper)
390390
block_store = await BlockStore.create(db_wrapper, use_cache=use_cache)
391391
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper)
392-
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
392+
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
393393
bc = await Blockchain.create(consensus_store, bt.constants, 2)
394394
for block in blocks:
395395
await _validate_and_add_block(bc, block)
@@ -471,7 +471,7 @@ async def test_get_blocks_by_hash(tmp_dir: Path, bt: BlockTools, db_version: int
471471
coin_store_2 = await CoinStore.create(db_wrapper_2)
472472
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
473473
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
474-
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
474+
consensus_store = await ConsensusStoreSQLite3.create(store_2, coin_store_2, height_map)
475475
bc = await Blockchain.create(consensus_store, bt.constants, 2)
476476

477477
store = await BlockStore.create(db_wrapper, use_cache=use_cache)
@@ -586,7 +586,7 @@ async def test_get_prev_hash(tmp_dir: Path, bt: BlockTools, db_version: int, use
586586
coin_store_2 = await CoinStore.create(db_wrapper_2)
587587
store_2 = await BlockStore.create(db_wrapper_2, use_cache=use_cache)
588588
height_map = await BlockHeightMap.create(tmp_dir, db_wrapper_2)
589-
consensus_store = await ConsensusStore.create(store_2, coin_store_2, height_map)
589+
consensus_store = await ConsensusStoreSQLite3.create(store_2, coin_store_2, height_map)
590590
bc = await Blockchain.create(consensus_store, bt.constants, 2)
591591

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
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
2423
from chia.full_node.block_store import BlockStore
2524
from chia.full_node.coin_store import CoinStore
25+
from chia.full_node.consensus_store_sqlite3 import ConsensusStoreSQLite3
2626
from chia.full_node.hint_store import HintStore
2727
from chia.simulator.block_tools import BlockTools, test_constants
2828
from chia.simulator.wallet_tools import WalletTool
@@ -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 ConsensusStore.create(store, coin_store, height_map)
322+
consensus_store = await ConsensusStoreSQLite3.create(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 ConsensusStore.create(store, coin_store, height_map)
390+
consensus_store = await ConsensusStoreSQLite3.create(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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
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
1615
from chia.consensus.multiprocess_validation import PreValidationResult
1716
from chia.full_node.block_store import BlockStore
1817
from chia.full_node.coin_store import CoinStore
18+
from chia.full_node.consensus_store_sqlite3 import ConsensusStoreSQLite3
1919
from chia.full_node.hint_store import HintStore
2020
from chia.simulator.block_tools import test_constants
2121
from chia.util.db_wrapper import DBWrapper2
@@ -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 ConsensusStore.create(block_store1, coin_store1, height_map)
66+
consensus_store = await ConsensusStoreSQLite3.create(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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
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
1918
from chia.consensus.default_constants import DEFAULT_CONSTANTS
2019
from chia.consensus.multiprocess_validation import PreValidationResult
2120
from chia.full_node.block_store import BlockStore
2221
from chia.full_node.coin_store import CoinStore
22+
from chia.full_node.consensus_store_sqlite3 import ConsensusStoreSQLite3
2323
from chia.simulator.block_tools import test_constants
2424
from chia.util.db_wrapper import DBWrapper2
2525

@@ -142,8 +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-
146-
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
145+
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
147146
bc = await Blockchain.create(consensus_store, test_constants, reserved_cores=0)
148147
sub_slot_iters = test_constants.SUB_SLOT_ITERS_STARTING
149148
for block in blocks:

chia/_tests/util/blockchain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
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
1615
from chia.full_node.block_store import BlockStore
1716
from chia.full_node.coin_store import CoinStore
17+
from chia.full_node.consensus_store_sqlite3 import ConsensusStoreSQLite3
1818
from chia.simulator.block_tools import BlockTools
1919
from chia.util.db_wrapper import DBWrapper2, generate_in_memory_db_uri
2020
from chia.util.default_root import DEFAULT_ROOT_PATH
@@ -30,7 +30,7 @@ async def create_blockchain(
3030
block_store = await BlockStore.create(wrapper)
3131
coin_store = await CoinStore.create(wrapper)
3232
height_map = await BlockHeightMap.create(Path("."), wrapper, None)
33-
consensus_store = await ConsensusStore.create(block_store, coin_store, height_map)
33+
consensus_store = await ConsensusStoreSQLite3.create(block_store, coin_store, height_map)
3434
bc1 = await Blockchain.create(consensus_store, constants, 3, single_threaded=True, log_coins=True)
3535
try:
3636
assert bc1.get_peak() is None

0 commit comments

Comments
 (0)