Skip to content

Commit afd5427

Browse files
authored
CHIA-2823 move sp broadcast (#19654)
* move sp broadcast * refactor to avoid double calls * move up
1 parent 6742513 commit afd5427

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

chia/consensus/blockchain.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -613,20 +613,13 @@ async def _reconsider_peak(
613613
[fork_add.coin for fork_add in fork_info.additions_since_fork.values() if fork_add.is_coinbase],
614614
)
615615

616-
def get_next_difficulty(self, header_hash: bytes32, new_slot: bool) -> uint64:
616+
def get_next_sub_slot_iters_and_difficulty(self, header_hash: bytes32, new_slot: bool) -> tuple[uint64, uint64]:
617617
curr = self.try_block_record(header_hash)
618618
assert curr is not None
619619
if curr.height <= 2:
620-
return self.constants.DIFFICULTY_STARTING
620+
return self.constants.SUB_SLOT_ITERS_STARTING, self.constants.DIFFICULTY_STARTING
621621

622-
return get_next_sub_slot_iters_and_difficulty(self.constants, new_slot, curr, self)[1]
623-
624-
def get_next_slot_iters(self, header_hash: bytes32, new_slot: bool) -> uint64:
625-
curr = self.try_block_record(header_hash)
626-
assert curr is not None
627-
if curr.height <= 2:
628-
return self.constants.SUB_SLOT_ITERS_STARTING
629-
return get_next_sub_slot_iters_and_difficulty(self.constants, new_slot, curr, self)[0]
622+
return get_next_sub_slot_iters_and_difficulty(self.constants, new_slot, curr, self)
630623

631624
async def get_sp_and_ip_sub_slots(
632625
self, header_hash: bytes32

chia/full_node/full_node.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class PeakPostProcessingResult:
102102
fns_peak_result: FullNodeStorePeakResult # The result of calling FullNodeStore.new_peak
103103
hints: list[tuple[bytes32, bytes]] # The hints added to the DB
104104
lookup_coin_ids: list[bytes32] # The coin IDs that we need to look up to notify wallets of changes
105+
signage_points: list[tuple[RespondSignagePoint, WSChiaConnection, Optional[EndOfSubSlotBundle]]]
105106

106107

107108
@dataclasses.dataclass(frozen=True)
@@ -837,7 +838,7 @@ async def send_peak_to_timelords(
837838
peak_block = await self.blockchain.get_full_peak()
838839
if peak_block is not None:
839840
peak = self.blockchain.block_record(peak_block.header_hash)
840-
difficulty = self.blockchain.get_next_difficulty(peak.header_hash, False)
841+
difficulty = self.blockchain.get_next_sub_slot_iters_and_difficulty(peak.header_hash, False)[1]
841842
ses: Optional[SubEpochSummary] = next_sub_epoch_summary(
842843
self.constants,
843844
self.blockchain,
@@ -1807,10 +1808,9 @@ async def signage_point_post_processing(
18071808
# Makes sure to potentially update the difficulty if we are past the peak (into a new sub-slot)
18081809
assert ip_sub_slot is not None
18091810
if request.challenge_chain_vdf.challenge != ip_sub_slot.challenge_chain.get_hash():
1810-
next_difficulty = self.blockchain.get_next_difficulty(peak.header_hash, True)
1811-
next_sub_slot_iters = self.blockchain.get_next_slot_iters(peak.header_hash, True)
1812-
difficulty = next_difficulty
1813-
sub_slot_iters = next_sub_slot_iters
1811+
sub_slot_iters, difficulty = self.blockchain.get_next_sub_slot_iters_and_difficulty(
1812+
peak.header_hash, True
1813+
)
18141814
else:
18151815
difficulty = self.constants.DIFFICULTY_STARTING
18161816
sub_slot_iters = self.constants.SUB_SLOT_ITERS_STARTING
@@ -1845,8 +1845,7 @@ async def peak_post_processing(
18451845
"""
18461846

18471847
record = state_change_summary.peak
1848-
difficulty = self.blockchain.get_next_difficulty(record.header_hash, False)
1849-
sub_slot_iters = self.blockchain.get_next_slot_iters(record.header_hash, False)
1848+
sub_slot_iters, difficulty = self.blockchain.get_next_sub_slot_iters_and_difficulty(record.header_hash, False)
18501849

18511850
self.log.info(
18521851
f"🌱 Updated peak to height {record.height}, weight {record.weight}, "
@@ -1895,6 +1894,7 @@ async def peak_post_processing(
18951894
difficulty,
18961895
)
18971896

1897+
signage_points: list[tuple[RespondSignagePoint, WSChiaConnection, Optional[EndOfSubSlotBundle]]] = []
18981898
if fns_peak_result.new_signage_points is not None and peer is not None:
18991899
for index, sp in fns_peak_result.new_signage_points:
19001900
assert (
@@ -1903,8 +1903,13 @@ async def peak_post_processing(
19031903
and sp.rc_vdf is not None
19041904
and sp.rc_proof is not None
19051905
)
1906-
await self.signage_point_post_processing(
1907-
RespondSignagePoint(index, sp.cc_vdf, sp.cc_proof, sp.rc_vdf, sp.rc_proof), peer, sub_slots[1]
1906+
# Collect the data for networking outside the mutex
1907+
signage_points.append(
1908+
(
1909+
RespondSignagePoint(index, sp.cc_vdf, sp.cc_proof, sp.rc_vdf, sp.rc_proof),
1910+
peer,
1911+
sub_slots[1],
1912+
)
19081913
)
19091914

19101915
if sub_slots[1] is None:
@@ -1934,6 +1939,7 @@ async def peak_post_processing(
19341939
fns_peak_result,
19351940
hints_to_add,
19361941
lookup_coin_ids,
1942+
signage_points=signage_points,
19371943
)
19381944

19391945
async def peak_post_processing_2(
@@ -1948,6 +1954,8 @@ async def peak_post_processing_2(
19481954
with peers
19491955
"""
19501956
record = state_change_summary.peak
1957+
for signage_point in ppp_result.signage_points:
1958+
await self.signage_point_post_processing(*signage_point)
19511959
for new_peak_item in ppp_result.mempool_peak_result:
19521960
self.log.debug(f"Added transaction to mempool: {new_peak_item.transaction_id}")
19531961
mempool_item = self.mempool_manager.get_mempool_item(new_peak_item.transaction_id)
@@ -2673,8 +2681,9 @@ async def add_end_of_sub_slot(
26732681

26742682
peak = self.blockchain.get_peak()
26752683
if peak is not None and peak.height > 2:
2676-
next_sub_slot_iters = self.blockchain.get_next_slot_iters(peak.header_hash, True)
2677-
next_difficulty = self.blockchain.get_next_difficulty(peak.header_hash, True)
2684+
next_sub_slot_iters, next_difficulty = self.blockchain.get_next_sub_slot_iters_and_difficulty(
2685+
peak.header_hash, True
2686+
)
26782687
else:
26792688
next_sub_slot_iters = self.constants.SUB_SLOT_ITERS_STARTING
26802689
next_difficulty = self.constants.DIFFICULTY_STARTING

chia/full_node/full_node_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,9 @@ async def respond_signage_point(
726726
return None
727727
peak = self.full_node.blockchain.get_peak()
728728
if peak is not None and peak.height > self.full_node.constants.MAX_SUB_SLOT_BLOCKS:
729-
next_sub_slot_iters = self.full_node.blockchain.get_next_slot_iters(peak.header_hash, True)
729+
next_sub_slot_iters = self.full_node.blockchain.get_next_sub_slot_iters_and_difficulty(
730+
peak.header_hash, True
731+
)[0]
730732
sub_slots_for_peak = await self.full_node.blockchain.get_sp_and_ip_sub_slots(peak.header_hash)
731733
assert sub_slots_for_peak is not None
732734
ip_sub_slot: Optional[EndOfSubSlotBundle] = sub_slots_for_peak[1]

0 commit comments

Comments
 (0)