Skip to content

Commit fd1edd5

Browse files
committed
Self
1 parent 5e394bb commit fd1edd5

File tree

2 files changed

+9
-38
lines changed

2 files changed

+9
-38
lines changed

chia/consensus/blockchain.py

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -155,57 +155,27 @@ def shut_down(self) -> None:
155155
self._shut_down = True
156156
self.pool.shutdown(wait=True)
157157

158-
def _initialize_caches(self) -> None:
158+
async def _load_chain_from_store(self) -> None:
159159
"""
160-
Initialize the blockchain cache data structures.
160+
Initializes the state of the Blockchain class from the database.
161161
"""
162162
self._block_records = {}
163163
self._heights_in_cache = {}
164-
165-
async def _load_recent_blocks_from_store(self) -> tuple[dict[bytes32, BlockRecord], Optional[bytes32]]:
166-
"""
167-
Load recent blocks from the consensus store.
168-
Returns block records and peak hash.
169-
"""
170-
return await self.consensus_store.get_block_records_close_to_peak(self.constants.BLOCKS_CACHE_SIZE)
171-
172-
def _populate_cache_with_blocks(self, block_records: dict[bytes32, BlockRecord]) -> None:
173-
"""
174-
Add the loaded block records to the cache.
175-
"""
164+
block_records, peak = await self.consensus_store.get_block_records_close_to_peak(
165+
self.constants.BLOCKS_CACHE_SIZE
166+
)
176167
for block in block_records.values():
177168
self.add_block_record(block)
178169

179-
def _set_peak_height_from_blocks(self, block_records: dict[bytes32, BlockRecord], peak: Optional[bytes32]) -> None:
180-
"""
181-
Set the peak height based on loaded blocks.
182-
Handles the case where no blocks are loaded (empty blockchain).
183-
"""
184170
if len(block_records) == 0:
185171
assert peak is None
186172
self._peak_height = None
187173
return
188174

189175
assert peak is not None
190176
self._peak_height = self.block_record(peak).height
191-
192-
def _validate_blockchain_state(self) -> None:
193-
"""
194-
Validate the loaded blockchain state for consistency.
195-
"""
196-
if self._peak_height is not None:
197-
assert self.consensus_store.contains_height(self._peak_height)
198-
assert not self.consensus_store.contains_height(uint32(self._peak_height + 1))
199-
200-
async def _load_chain_from_store(self) -> None:
201-
"""
202-
Initializes the state of the Blockchain class from the database.
203-
"""
204-
self._initialize_caches()
205-
block_records, peak = await self._load_recent_blocks_from_store()
206-
self._populate_cache_with_blocks(block_records)
207-
self._set_peak_height_from_blocks(block_records, peak)
208-
self._validate_blockchain_state()
177+
assert self.consensus_store.contains_height(self._peak_height)
178+
assert not self.consensus_store.contains_height(uint32(self._peak_height + 1))
209179

210180
def get_peak(self) -> Optional[BlockRecord]:
211181
"""

chia/full_node/consensus_store_sqlite3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from chia_rs import BlockRecord, FullBlock, SubEpochChallengeSegment, SubEpochSummary
1010
from chia_rs.sized_bytes import bytes32
1111
from chia_rs.sized_ints import uint32, uint64
12+
from typing_extensions import Self
1213

1314
from chia.consensus.block_height_map import BlockHeightMap
1415
from chia.full_node.block_store import BlockStore
@@ -55,7 +56,7 @@ async def new_block(
5556
await self.coin_store.new_block(height, timestamp, included_reward_coins, tx_additions, tx_removals)
5657

5758
@asynccontextmanager
58-
async def writer(self) -> AsyncIterator[ConsensusStoreSQLite3Writer]:
59+
async def writer(self) -> AsyncIterator[Self]:
5960
# Return self as the writer facade
6061
async with self.block_store.transaction():
6162
yield self

0 commit comments

Comments
 (0)