Skip to content

Commit

Permalink
don't block on start (#1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yostra committed Mar 24, 2021
1 parent 73287e2 commit 3676748
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/full_node/block_store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import logging
from typing import Dict, List, Optional, Tuple

Expand Down Expand Up @@ -191,6 +192,8 @@ async def get_header_blocks_in_range(
await cursor.close()
ret: Dict[bytes32, HeaderBlock] = {}
for row in rows:
# Ugly hack, until full_block.get_block_header is rewritten as part of generator runner change
await asyncio.sleep(0.001)
header_hash = bytes.fromhex(row[0])
full_block: FullBlock = FullBlock.from_bytes(row[1])
ret[header_hash] = full_block.get_block_header()
Expand Down
16 changes: 11 additions & 5 deletions src/full_node/full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class FullNode:
state_changed_callback: Optional[Callable]
timelord_lock: asyncio.Lock
initialized: bool
weight_proof_handler: Optional[WeightProofHandler]

def __init__(
self,
Expand Down Expand Up @@ -111,7 +112,8 @@ async def _start(self):
start_time = time.time()
self.blockchain = await Blockchain.create(self.coin_store, self.block_store, self.constants)
self.mempool_manager = MempoolManager(self.coin_store, self.constants)
self.weight_proof_handler = WeightProofHandler(self.constants, self.blockchain)
self.weight_proof_handler = None
asyncio.create_task(self.initialize_weight_proof())
self._sync_task = None
time_taken = time.time() - start_time
if self.blockchain.get_peak() is None:
Expand All @@ -122,7 +124,6 @@ async def _start(self):
f" {self.blockchain.get_peak().height}, "
f"time taken: {int(time_taken)}s"
)
await self.weight_proof_handler.get_proof_of_weight(self.blockchain.get_peak().header_hash)
pending_tx = await self.mempool_manager.new_peak(self.blockchain.get_peak())
assert len(pending_tx) == 0 # no pending transactions when starting up

Expand All @@ -141,6 +142,10 @@ async def _start(self):
)
self.initialized = True

async def initialize_weight_proof(self):
self.weight_proof_handler = WeightProofHandler(self.constants, self.blockchain)
await self.weight_proof_handler.get_proof_of_weight(self.blockchain.get_peak().header_hash)

def set_server(self, server: ChiaServer):
self.server = server
try:
Expand Down Expand Up @@ -513,7 +518,8 @@ async def _sync(self):
- Download blocks in batch (and in parallel) and verify them one at a time
- Disconnect peers that provide invalid blocks or don't have the blocks
"""

if self.weight_proof_handler is None:
return
# Ensure we are only syncing once and not double calling this method
if self.sync_store.get_sync_mode():
return
Expand Down Expand Up @@ -733,7 +739,7 @@ async def receive_block_batch(
self.log.error(f"Error: {error}, Invalid block from peer: {peer.get_peer_info()} ")
return False, advanced_peak, fork_height
block_record = self.blockchain.block_record(block.header_hash)
if block_record.sub_epoch_summary_included is not None:
if block_record.sub_epoch_summary_included is not None and self.weight_proof_handler is not None:
await self.weight_proof_handler.create_prev_sub_epoch_segments()
if advanced_peak:
self._state_changed("new_peak")
Expand Down Expand Up @@ -761,7 +767,7 @@ async def _finish_sync(self):
if peak is not None:
await self.peak_post_processing(peak_fb, peak, peak.height - 1, None)

if peak is not None:
if peak is not None and self.weight_proof_handler is not None:
await self.weight_proof_handler.get_proof_of_weight(peak.header_hash)
self._state_changed("block")

Expand Down
2 changes: 2 additions & 0 deletions src/full_node/full_node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ async def respond_transaction(

@api_request
async def request_proof_of_weight(self, request: full_node_protocol.RequestProofOfWeight) -> Optional[Message]:
if self.full_node.weight_proof_handler is None:
return None
if not self.full_node.blockchain.contains_block(request.tip):
self.log.error(f"got weight proof request for unknown peak {request.tip}")
return None
Expand Down

0 comments on commit 3676748

Please sign in to comment.