Skip to content

Commit

Permalink
CHIA-2020 Simplify add_block_batch (#19004)
Browse files Browse the repository at this point in the history
Simplify add_block_batch.

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 authored Dec 10, 2024
1 parent 3650efd commit 9063e29
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 25 deletions.
4 changes: 1 addition & 3 deletions chia/_tests/util/full_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from chia.types.full_block import FullBlock
from chia.types.peer_info import PeerInfo
from chia.types.validation_state import ValidationState
from chia.util.augmented_chain import AugmentedBlockchain
from chia.util.config import load_config
from chia.util.ints import uint16

Expand Down Expand Up @@ -212,8 +211,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
3 changes: 0 additions & 3 deletions chia/_tests/wallet/sync/test_wallet_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
from chia.types.full_block import FullBlock
from chia.types.peer_info import PeerInfo
from chia.types.validation_state import ValidationState
from chia.util.augmented_chain import AugmentedBlockchain
from chia.util.hash import std_hash
from chia.util.ints import uint32, uint64, uint128
from chia.wallet.nft_wallet.nft_wallet import NFTWallet
Expand Down Expand Up @@ -361,7 +360,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 +488,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
10 changes: 2 additions & 8 deletions chia/simulator/add_blocks_in_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from chia.types.full_block import FullBlock
from chia.types.peer_info import PeerInfo
from chia.types.validation_state import ValidationState
from chia.util.augmented_chain import AugmentedBlockchain
from chia.util.batches import to_batches
from chia.util.ints import uint32

Expand Down Expand Up @@ -40,14 +39,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
7 changes: 2 additions & 5 deletions tools/test_full_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from chia.server.ws_connection import WSChiaConnection
from chia.types.full_block import FullBlock
from chia.types.validation_state import ValidationState
from chia.util.augmented_chain import AugmentedBlockchain
from chia.util.config import load_config


Expand Down Expand Up @@ -165,8 +164,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 +187,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 9063e29

Please sign in to comment.