Skip to content

Commit

Permalink
breakup multi-second WeightProofHandler.validate_weight_proof with as…
Browse files Browse the repository at this point in the history
…ync sleeps
  • Loading branch information
altendky committed Feb 10, 2022
1 parent 10fd5f9 commit a2ed37d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions chia/full_node/weight_proof.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,13 @@ async def validate_weight_proof(self, weight_proof: WeightProof) -> Tuple[bool,
peak_height = weight_proof.recent_chain_data[-1].reward_chain_block.height
log.info(f"validate weight proof peak height {peak_height}")

# TODO: Consider if this can be spun off to a thread as an alternative to
# sprinkling async sleeps around.

# timing reference: start
summaries, sub_epoch_weight_list = _validate_sub_epoch_summaries(self.constants, weight_proof)
await asyncio.sleep(0) # break up otherwise multi-second sync code
# timing reference: 1 second
if summaries is None:
log.error("weight proof failed sub epoch data validation")
return False, uint32(0), []
Expand All @@ -611,12 +617,17 @@ async def validate_weight_proof(self, weight_proof: WeightProof) -> Tuple[bool,
log.error("failed weight proof sub epoch sample validation")
return False, uint32(0), []

# timing reference: 1 second
with ProcessPoolExecutor(6) as executor:
try:
await asyncio.sleep(0) # break up otherwise multi-second sync code
# timing reference: 1.1 second
constants, summary_bytes, wp_segment_bytes, wp_recent_chain_bytes = vars_to_bytes(
self.constants, summaries, weight_proof
)
await asyncio.sleep(0) # break up otherwise multi-second sync code

# timing reference: 2 second
recent_blocks_validation_task = asyncio.get_running_loop().run_in_executor(
executor,
_validate_recent_blocks,
Expand All @@ -626,14 +637,18 @@ async def validate_weight_proof(self, weight_proof: WeightProof) -> Tuple[bool,
pathlib.Path(self._executor_shutdown_tempfile.name),
)

# timing reference: 2 second
segments_validated, vdfs_to_validate = _validate_sub_epoch_segments(
constants, rng, wp_segment_bytes, summary_bytes
)
await asyncio.sleep(0) # break up otherwise multi-second sync code
if not segments_validated:
return False, uint32(0), []

# timing reference: 4 second
vdf_chunks = chunks(vdfs_to_validate, self._num_processes)
vdf_tasks = []
# timing reference: 4 second
for chunk in vdf_chunks:
byte_chunks = []
for vdf_proof, classgroup, vdf_info in chunk:
Expand All @@ -650,6 +665,7 @@ async def validate_weight_proof(self, weight_proof: WeightProof) -> Tuple[bool,
# give other stuff a turn
await asyncio.sleep(0)

# timing reference: 4 second
for vdf_task in asyncio.as_completed(vdf_tasks):
validated = await vdf_task
if not validated:
Expand Down

0 comments on commit a2ed37d

Please sign in to comment.