Skip to content

Commit

Permalink
Different lock for compact proofs (Chia-Network#5539)
Browse files Browse the repository at this point in the history
* test

* compact proof lock

* lint

* remove wrong commit

* remove wrong commit

* check vdf info for ip and sp

* return if no proof to replace
  • Loading branch information
almogdepaz committed May 20, 2021
1 parent 431f15b commit f1120d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion chia/consensus/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Blockchain(BlockchainInterface):

# Lock to prevent simultaneous reads and writes
lock: asyncio.Lock
compact_proof_lock: asyncio.Lock

@staticmethod
async def create(
Expand All @@ -96,6 +97,7 @@ async def create(
"""
self = Blockchain()
self.lock = asyncio.Lock() # External lock handled by full node
self.compact_proof_lock = asyncio.Lock()
cpu_count = multiprocessing.cpu_count()
if cpu_count > 61:
cpu_count = 61 # Windows Server 2016 has an issue https://bugs.python.org/issue26903
Expand Down Expand Up @@ -172,7 +174,6 @@ async def receive_block(
invalid. Also returns the fork height, in the case of a new peak.
"""
genesis: bool = block.height == 0

if self.contains_block(block.header_hash):
return ReceiveBlockResult.ALREADY_HAVE_BLOCK, None, None

Expand Down
16 changes: 10 additions & 6 deletions chia/full_node/full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,11 +1762,15 @@ async def _replace_proof(
new_block = dataclasses.replace(block, finished_sub_slots=new_finished_subslots)
break
if field_vdf == CompressibleVDFField.CC_SP_VDF:
assert block.challenge_chain_sp_proof is not None
new_block = dataclasses.replace(block, challenge_chain_sp_proof=vdf_proof)
if block.reward_chain_block.challenge_chain_sp_vdf == vdf_info:
assert block.challenge_chain_sp_proof is not None
new_block = dataclasses.replace(block, challenge_chain_sp_proof=vdf_proof)
if field_vdf == CompressibleVDFField.CC_IP_VDF:
new_block = dataclasses.replace(block, challenge_chain_ip_proof=vdf_proof)
assert new_block is not None
if block.reward_chain_block.challenge_chain_ip_vdf == vdf_info:
new_block = dataclasses.replace(block, challenge_chain_ip_proof=vdf_proof)
if new_block is None:
self.log.debug("did not replace any proof, vdf does not match")
return
async with self.db_wrapper.lock:
await self.block_store.add_full_block(new_block.header_hash, new_block, block_record)
await self.block_store.db_wrapper.commit_transaction()
Expand All @@ -1777,7 +1781,7 @@ async def respond_compact_proof_of_time(self, request: timelord_protocol.Respond
request.vdf_info, request.vdf_proof, request.height, request.header_hash, field_vdf
):
return None
async with self.blockchain.lock:
async with self.blockchain.compact_proof_lock:
await self._replace_proof(request.vdf_info, request.vdf_proof, request.height, field_vdf)
msg = make_msg(
ProtocolMessageTypes.new_compact_vdf,
Expand Down Expand Up @@ -1854,7 +1858,7 @@ async def respond_compact_vdf(self, request: full_node_protocol.RespondCompactVD
request.vdf_info, request.vdf_proof, request.height, request.header_hash, field_vdf
):
return None
async with self.blockchain.lock:
async with self.blockchain.compact_proof_lock:
if self.blockchain.seen_compact_proofs(request.vdf_info, request.height):
return None
await self._replace_proof(request.vdf_info, request.vdf_proof, request.height, field_vdf)
Expand Down

0 comments on commit f1120d9

Please sign in to comment.