Skip to content

Commit

Permalink
Simplify add_block_batch.
Browse files Browse the repository at this point in the history
1. No need to accept AugmentedBlockchain and reconstruct it again.
2. No need to return both success and an unused optional error.
  • Loading branch information
AmineKhaldi committed Dec 9, 2024
1 parent 465f56e commit 9b68d14
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 21 deletions.
3 changes: 1 addition & 2 deletions chia/_tests/util/full_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ async def run_sync_test(
)
fork_height = block_batch[0].height - 1
header_hash = block_batch[0].prev_header_hash
success, summary, _err = await full_node.add_block_batch(
AugmentedBlockchain(full_node.blockchain),
success, summary = await full_node.add_block_batch(
block_batch,
peer_info,
ForkInfo(fork_height, fork_height, header_hash),
Expand Down
2 changes: 0 additions & 2 deletions chia/_tests/wallet/sync/test_wallet_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ async def test_long_sync_wallet(
)
fork_height = blocks_reorg[-num_blocks - 10].height - 1
await full_node.add_block_batch(
AugmentedBlockchain(full_node.blockchain),
blocks_reorg[-num_blocks - 10 : -1],
PeerInfo("0.0.0.0", 0),
ForkInfo(fork_height, fork_height, blocks_reorg[-num_blocks - 10].prev_header_hash),
Expand Down Expand Up @@ -490,7 +489,6 @@ async def test_wallet_reorg_get_coinbase(
full_node.constants, True, block_record, full_node.blockchain
)
await full_node.add_block_batch(
AugmentedBlockchain(full_node.blockchain),
blocks_reorg_2[-44:],
PeerInfo("0.0.0.0", 0),
ForkInfo(blocks_reorg_2[-45].height, blocks_reorg_2[-45].height, blocks_reorg_2[-45].header_hash),
Expand Down
11 changes: 5 additions & 6 deletions chia/full_node/full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,8 @@ async def short_sync_batch(self, peer: WSChiaConnection, start_height: uint32, t
self.constants, new_slot, prev_b, self.blockchain
)
vs = ValidationState(ssi, diff, None)
success, state_change_summary, _err = await self.add_block_batch(
AugmentedBlockchain(self.blockchain), response.blocks, peer_info, fork_info, vs
success, state_change_summary = await self.add_block_batch(
response.blocks, peer_info, fork_info, vs
)
if not success:
raise ValueError(f"Error short batch syncing, failed to validate blocks {height}-{end_height}")
Expand Down Expand Up @@ -1467,13 +1467,12 @@ async def update_wallets(self, wallet_update: WalletUpdate) -> None:

async def add_block_batch(
self,
blockchain: AugmentedBlockchain,
all_blocks: list[FullBlock],
peer_info: PeerInfo,
fork_info: ForkInfo,
vs: ValidationState, # in-out parameter
wp_summaries: Optional[list[SubEpochSummary]] = None,
) -> tuple[bool, Optional[StateChangeSummary], Optional[Err]]:
) -> tuple[bool, Optional[StateChangeSummary]]:
# Precondition: All blocks must be contiguous blocks, index i+1 must be the parent of index i
# Returns a bool for success, as well as a StateChangeSummary if the peak was advanced

Expand All @@ -1482,7 +1481,7 @@ async def add_block_batch(
blocks_to_validate = await self.skip_blocks(blockchain, all_blocks, fork_info, vs)

if len(blocks_to_validate) == 0:
return True, None, None
return True, None

futures = await self.prevalidate_blocks(
blockchain,
Expand All @@ -1508,7 +1507,7 @@ async def add_block_batch(
f"Total time for {len(blocks_to_validate)} blocks: {time.monotonic() - pre_validate_start}, "
f"advanced: True"
)
return err is None, agg_state_change_summary, err
return err is None, agg_state_change_summary

async def skip_blocks(
self,
Expand Down
9 changes: 2 additions & 7 deletions chia/simulator/add_blocks_in_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,9 @@ async def add_blocks_in_batches(
if (b.height % 128) == 0:
print(f"main chain: {b.height:4} weight: {b.weight}")
# vs is updated by the call to add_block_batch()
success, state_change_summary, err = await full_node.add_block_batch(
AugmentedBlockchain(full_node.blockchain),
block_batch.entries,
PeerInfo("0.0.0.0", 0),
fork_info,
vs,
success, state_change_summary = await full_node.add_block_batch(
block_batch.entries, PeerInfo("0.0.0.0", 0), fork_info, vs
)
assert err is None
assert success is True
if state_change_summary is not None:
peak_fb: Optional[FullBlock] = await full_node.blockchain.get_full_peak()
Expand Down
6 changes: 2 additions & 4 deletions tools/test_full_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ async def run_sync_checkpoint(
fork_height = block_batch[0].height - 1
header_hash = block_batch[0].prev_header_hash

success, _, _err = await full_node.add_block_batch(
AugmentedBlockchain(full_node.blockchain),
success, _ = await full_node.add_block_batch(
block_batch,
peer_info,
ForkInfo(fork_height, fork_height, header_hash),
Expand All @@ -189,8 +188,7 @@ async def run_sync_checkpoint(
)
fork_height = block_batch[0].height - 1
fork_header_hash = block_batch[0].prev_header_hash
success, _, _err = await full_node.add_block_batch(
AugmentedBlockchain(full_node.blockchain),
success, _ = await full_node.add_block_batch(
block_batch,
peer_info,
ForkInfo(fork_height, fork_height, fork_header_hash),
Expand Down

0 comments on commit 9b68d14

Please sign in to comment.