Skip to content

Commit 6969867

Browse files
authored
CHIA-3179 Optimize rolled back state construction in _reconsider_peak (#19734)
Optimize rolled back state construction in _reconsider_peak.
1 parent fb08c17 commit 6969867

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,15 @@ async def test_rollback(db_version: int, bt: BlockTools) -> None:
282282

283283
# The reorg will revert the creation and spend of many coins. It will also revert the spend (but not the
284284
# creation) of the selected coin.
285-
changed_records = await coin_store.rollback_to_block(reorg_index)
286-
changed_coin_records = [cr.coin for cr in changed_records]
287-
assert selected_coin in changed_records
285+
coin_changes = await coin_store.rollback_to_block(reorg_index)
286+
changed_coins = {cr.coin for cr in coin_changes.values()}
287+
assert selected_coin.coin in changed_coins
288288
for coin_record in all_records:
289289
assert coin_record is not None
290290
if coin_record.confirmed_block_index > reorg_index:
291-
assert coin_record.coin in changed_coin_records
291+
assert coin_record.coin in changed_coins
292292
if coin_record.spent_block_index > reorg_index:
293-
assert coin_record.coin in changed_coin_records
293+
assert coin_record.coin in changed_coins
294294

295295
for block in blocks:
296296
if not block.is_transaction_block():

chia/consensus/blockchain.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,7 @@ async def _reconsider_peak(
510510
)
511511

512512
if block_record.prev_hash != peak.header_hash:
513-
for coin_record in await self.coin_store.rollback_to_block(fork_info.fork_height):
514-
rolled_back_state[coin_record.name] = coin_record
513+
rolled_back_state = await self.coin_store.rollback_to_block(fork_info.fork_height)
515514
if self._log_coins and len(rolled_back_state) > 0:
516515
log.info(f"rolled back {len(rolled_back_state)} coins, to fork height {fork_info.fork_height}")
517516
log.info(

chia/full_node/coin_store.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,10 @@ async def batch_coin_states_by_puzzle_hashes(
535535

536536
return coin_states, next_height
537537

538-
async def rollback_to_block(self, block_index: int) -> list[CoinRecord]:
538+
async def rollback_to_block(self, block_index: int) -> dict[bytes32, CoinRecord]:
539539
"""
540540
Note that block_index can be negative, in which case everything is rolled back
541-
Returns the list of coin records that have been modified
541+
Returns a map of coin ID to coin record for modified items.
542542
"""
543543

544544
coin_changes: dict[bytes32, CoinRecord] = {}
@@ -572,7 +572,7 @@ async def rollback_to_block(self, block_index: int) -> list[CoinRecord]:
572572
coin_changes[coin_name] = record
573573

574574
await conn.execute("UPDATE coin_record SET spent_index=0 WHERE spent_index>?", (block_index,))
575-
return list(coin_changes.values())
575+
return coin_changes
576576

577577
# Update coin_record to be spent in DB
578578
async def _set_spent(self, coin_names: list[bytes32], index: uint32) -> None:

0 commit comments

Comments
 (0)