Skip to content

Commit 8720984

Browse files
committed
More protocol
1 parent 1eb36b8 commit 8720984

File tree

1 file changed

+51
-50
lines changed

1 file changed

+51
-50
lines changed

chia/consensus/blockchain.py

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -486,59 +486,59 @@ async def _reconsider_peak(
486486
if genesis and peak is not None:
487487
return [], None
488488

489-
if peak is not None:
490-
if block_record.weight < peak.weight:
491-
# This is not a heavier block than the heaviest we have seen, so we don't change the coin set
492-
return [], None
493-
if block_record.weight == peak.weight and peak.total_iters <= block_record.total_iters:
494-
# this is an equal weight block but our peak has lower iterations, so we dont change the coin set
495-
return [], None
496-
if block_record.weight == peak.weight:
497-
log.info(
498-
f"block has equal weight as our peak ({peak.weight}), but fewer "
499-
f"total iterations {block_record.total_iters} "
500-
f"peak: {peak.total_iters} "
501-
f"peak-hash: {peak.header_hash}"
502-
)
503-
504-
if block_record.prev_hash != peak.header_hash:
505-
rolled_back_state = await self.consensus_store.rollback_to_block(fork_info.fork_height)
506-
if self._log_coins and len(rolled_back_state) > 0:
507-
log.info(f"rolled back {len(rolled_back_state)} coins, to fork height {fork_info.fork_height}")
508-
log.info(
509-
"removed: %s",
510-
",".join(
511-
[
512-
name.hex()[0:6]
513-
for name, state in rolled_back_state.items()
514-
if state.confirmed_block_index == 0
515-
]
516-
),
517-
)
489+
async with self.consensus_store as writer:
490+
if peak is not None:
491+
if block_record.weight < peak.weight:
492+
# This is not a heavier block than the heaviest we have seen, so we don't change the coin set
493+
return [], None
494+
if block_record.weight == peak.weight and peak.total_iters <= block_record.total_iters:
495+
# this is an equal weight block but our peak has lower iterations, so we dont change the coin set
496+
return [], None
497+
if block_record.weight == peak.weight:
518498
log.info(
519-
"unspent: %s",
520-
",".join(
521-
[
522-
name.hex()[0:6]
523-
for name, state in rolled_back_state.items()
524-
if state.confirmed_block_index != 0
525-
]
526-
),
499+
f"block has equal weight as our peak ({peak.weight}), but fewer "
500+
f"total iterations {block_record.total_iters} "
501+
f"peak: {peak.total_iters} "
502+
f"peak-hash: {peak.header_hash}"
527503
)
528504

529-
# Collects all blocks from fork point to new peak
530-
records_to_add: list[BlockRecord] = []
531-
532-
if genesis:
533-
records_to_add = [block_record]
534-
elif fork_info.block_hashes == [block_record.header_hash]:
535-
# in the common case, we just add a block on top of the chain. Check
536-
# for that here to avoid an unnecessary database lookup.
537-
records_to_add = [block_record]
538-
else:
539-
records_to_add = await self.consensus_store.get_block_records_by_hash(fork_info.block_hashes)
505+
if block_record.prev_hash != peak.header_hash:
506+
rolled_back_state = await writer.rollback_to_block(fork_info.fork_height)
507+
if self._log_coins and len(rolled_back_state) > 0:
508+
log.info(f"rolled back {len(rolled_back_state)} coins, to fork height {fork_info.fork_height}")
509+
log.info(
510+
"removed: %s",
511+
",".join(
512+
[
513+
name.hex()[0:6]
514+
for name, state in rolled_back_state.items()
515+
if state.confirmed_block_index == 0
516+
]
517+
),
518+
)
519+
log.info(
520+
"unspent: %s",
521+
",".join(
522+
[
523+
name.hex()[0:6]
524+
for name, state in rolled_back_state.items()
525+
if state.confirmed_block_index != 0
526+
]
527+
),
528+
)
529+
530+
# Collects all blocks from fork point to new peak
531+
records_to_add: list[BlockRecord] = []
532+
533+
if genesis:
534+
records_to_add = [block_record]
535+
elif fork_info.block_hashes == [block_record.header_hash]:
536+
# in the common case, we just add a block on top of the chain. Check
537+
# for that here to avoid an unnecessary database lookup.
538+
records_to_add = [block_record]
539+
else:
540+
records_to_add = await self.consensus_store.get_block_records_by_hash(fork_info.block_hashes)
540541

541-
async with self.consensus_store as writer:
542542
for fetched_block_record in records_to_add:
543543
if not fetched_block_record.is_transaction_block:
544544
# Coins are only created in TX blocks so there are no state updates for this block
@@ -981,7 +981,8 @@ def add_block_record(self, block_record: BlockRecord) -> None:
981981
async def persist_sub_epoch_challenge_segments(
982982
self, ses_block_hash: bytes32, segments: list[SubEpochChallengeSegment]
983983
) -> None:
984-
await self.consensus_store.persist_sub_epoch_challenge_segments(ses_block_hash, segments)
984+
async with self.consensus_store as writer:
985+
await writer.persist_sub_epoch_challenge_segments(ses_block_hash, segments)
985986

986987
async def get_sub_epoch_challenge_segments(
987988
self,

0 commit comments

Comments
 (0)