Skip to content

Commit 3da5434

Browse files
authored
fix serialization of partial-proofs (#20188)
* fix serialization of partial-proofs (list[uint64]) to bytes, for use as key in dictionary * improve tests for partial-proof serialization
1 parent 794cce7 commit 3da5434

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

chia/_tests/farmer_harvester/test_farmer_harvester.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from chia._tests.util.time_out_assert import time_out_assert
1818
from chia.cmds.cmds_util import get_any_service_client
1919
from chia.farmer.farmer import Farmer
20+
from chia.farmer.farmer_api import serialize
2021
from chia.farmer.farmer_service import FarmerService
2122
from chia.harvester.harvester_rpc_client import HarvesterRpcClient
2223
from chia.harvester.harvester_service import HarvesterService
@@ -338,7 +339,7 @@ async def test_v2_partial_proofs_new_sp_hash(
338339
challenge_hash=bytes32(b"2" * 32),
339340
sp_hash=sp_hash,
340341
plot_identifier="test_plot_id",
341-
partial_proofs=[[uint64(1), uint64(2), uint64(3), uint64(4)]],
342+
partial_proofs=[[uint64(100), uint64(200), uint64(300), uint64(400)]],
342343
signage_point_index=uint8(0),
343344
plot_size=uint8(32),
344345
strength=uint8(5),
@@ -369,7 +370,7 @@ async def test_v2_partial_proofs_missing_sp_hash(
369370
challenge_hash=bytes32(b"2" * 32),
370371
sp_hash=sp_hash,
371372
plot_identifier="test_plot_id",
372-
partial_proofs=[[uint64(1), uint64(2), uint64(3), uint64(4)]],
373+
partial_proofs=[[uint64(100), uint64(200), uint64(300), uint64(400)]],
373374
signage_point_index=uint8(0),
374375
plot_size=uint8(32),
375376
plot_id=bytes32.fromhex("abababababababababababababababababababababababababababababababab"),
@@ -413,7 +414,10 @@ async def test_v2_partial_proofs_with_existing_sp(
413414
challenge_hash=challenge_hash,
414415
sp_hash=sp_hash,
415416
plot_identifier="test_plot_id",
416-
partial_proofs=[[uint64(1), uint64(2), uint64(3), uint64(4)], [uint64(2), uint64(3), uint64(4), uint64(5)]],
417+
partial_proofs=[
418+
[uint64(100), uint64(200), uint64(300), uint64(400)],
419+
[uint64(2222), uint64(3333), uint64(4444), uint64(5555)],
420+
],
417421
signage_point_index=uint8(0),
418422
plot_size=uint8(32),
419423
plot_id=bytes32.fromhex("abababababababababababababababababababababababababababababababab"),
@@ -447,7 +451,7 @@ async def test_solution_response_handler(
447451
challenge_hash=challenge_hash,
448452
sp_hash=sp_hash,
449453
plot_identifier="test_plot_id",
450-
partial_proofs=[[uint64(1), uint64(2), uint64(3), uint64(4)]],
454+
partial_proofs=[[uint64(1111), uint64(2222), uint64(3333), uint64(4444)]],
451455
signage_point_index=uint8(0),
452456
plot_size=uint8(32),
453457
plot_id=bytes32.fromhex("abababababababababababababababababababababababababababababababab"),
@@ -460,7 +464,7 @@ async def test_solution_response_handler(
460464
harvester_peer = await get_harvester_peer(farmer)
461465

462466
# manually add pending request
463-
key = bytes(partial_proofs.partial_proofs[0])
467+
key = serialize(partial_proofs.partial_proofs[0])
464468
farmer.pending_solver_requests[key] = {
465469
"proof_data": partial_proofs,
466470
"peer": harvester_peer,
@@ -486,7 +490,7 @@ async def test_solution_response_handler(
486490
assert original_peer == harvester_peer
487491

488492
# verify pending request was removed
489-
key = bytes(partial_proofs.partial_proofs[0])
493+
key = serialize(partial_proofs.partial_proofs[0])
490494
assert key not in farmer.pending_solver_requests
491495

492496

@@ -530,7 +534,10 @@ async def test_solution_response_empty_proof(
530534
challenge_hash=challenge_hash,
531535
sp_hash=sp_hash,
532536
plot_identifier="test_plot_id",
533-
partial_proofs=[[uint64(1), uint64(2), uint64(3), uint64(4)], [uint64(2), uint64(3), uint64(4), uint64(5)]],
537+
partial_proofs=[
538+
[uint64(100), uint64(200), uint64(300), uint64(400)],
539+
[uint64(2222), uint64(3333), uint64(4444), uint64(5555)],
540+
],
534541
signage_point_index=uint8(0),
535542
plot_size=uint8(32),
536543
plot_id=bytes32.fromhex("abababababababababababababababababababababababababababababababab"),
@@ -544,7 +551,7 @@ async def test_solution_response_empty_proof(
544551
harvester_peer.peer_node_id = "harvester_peer"
545552

546553
# manually add pending request
547-
key = bytes(partial_proofs.partial_proofs[0])
554+
key = serialize(partial_proofs.partial_proofs[0])
548555
farmer.pending_solver_requests[key] = {
549556
"proof_data": partial_proofs,
550557
"peer": harvester_peer,
@@ -563,7 +570,7 @@ async def test_solution_response_empty_proof(
563570
mock_new_proof.assert_not_called()
564571

565572
# verify pending request was removed (cleanup still happens)
566-
key = bytes(partial_proofs.partial_proofs[0])
573+
key = serialize(partial_proofs.partial_proofs[0])
567574
assert key not in farmer.pending_solver_requests
568575

569576

@@ -595,7 +602,10 @@ async def test_v2_partial_proofs_solver_exception(
595602
challenge_hash=challenge_hash,
596603
sp_hash=sp_hash,
597604
plot_identifier="test_plot_id",
598-
partial_proofs=[[uint64(1), uint64(2), uint64(3), uint64(4)], [uint64(2), uint64(3), uint64(4), uint64(5)]],
605+
partial_proofs=[
606+
[uint64(100), uint64(200), uint64(300), uint64(400)],
607+
[uint64(2222), uint64(3333), uint64(4444), uint64(5555)],
608+
],
599609
signage_point_index=uint8(0),
600610
plot_size=uint8(32),
601611
plot_id=bytes32.fromhex("abababababababababababababababababababababababababababababababab"),
@@ -612,5 +622,5 @@ async def test_v2_partial_proofs_solver_exception(
612622
await farmer_api.partial_proofs(partial_proofs, harvester_peer)
613623

614624
# verify pending request was cleaned up after exception
615-
key = bytes(partial_proofs.partial_proofs[0])
625+
key = serialize(partial_proofs.partial_proofs[0])
616626
assert key not in farmer.pending_solver_requests

chia/_tests/solver/test_solver_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async def test_solver_api_methods(blockchain_constants: ConsensusConstants, tmp_
2424
solver_api = solver_service._api
2525
assert solver_api.ready() is True
2626
test_info = SolverInfo(
27-
partial_proof=[uint64(1), uint64(2), uint64(3), uint64(4)],
27+
partial_proof=[uint64(1000), uint64(2000), uint64(3000), uint64(4000)],
2828
plot_id=bytes32.fromhex("abababababababababababababababababababababababababababababababab"),
2929
strength=uint8(5),
3030
size=uint8(28),

chia/_tests/util/network_protocol_data.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@
155155
bytes32.fromhex("42743566108589c11bb3811b347900b6351fd3e25bad6c956c0bf1c05a4d93fb"),
156156
bytes32.fromhex("8a346e8dc02e9b44c0571caa74fd99f163d4c5d7deaedac87125528721493f7a"),
157157
"plot-filename",
158-
[[uint64(1), uint64(2), uint64(3), uint64(4)], [uint64(2), uint64(3), uint64(4), uint64(5)]],
158+
[
159+
[uint64(1111), uint64(2222), uint64(3333), uint64(4444)],
160+
[uint64(2222), uint64(3333), uint64(4444), uint64(5555)],
161+
],
159162
uint8(4),
160163
uint8(32),
161164
uint8(5),
0 Bytes
Binary file not shown.

chia/_tests/util/protocol_messages_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"challenge_hash": "0x42743566108589c11bb3811b347900b6351fd3e25bad6c956c0bf1c05a4d93fb",
7070
"sp_hash": "0x8a346e8dc02e9b44c0571caa74fd99f163d4c5d7deaedac87125528721493f7a",
7171
"plot_identifier": "plot-filename",
72-
"partial_proofs": [[1, 2, 3, 4], [2, 3, 4, 5]],
72+
"partial_proofs": [[1111, 2222, 3333, 4444], [2222, 3333, 4444, 5555]],
7373
"signage_point_index": 4,
7474
"plot_size": 32,
7575
"strength": 5,

chia/farmer/farmer_api.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
)
5050

5151

52+
def serialize(partial_proof: list[uint64]) -> bytes:
53+
key = bytearray()
54+
for val in partial_proof:
55+
key += val.stream_to_bytes()
56+
return bytes(key)
57+
58+
5259
class FarmerAPI:
5360
if TYPE_CHECKING:
5461
from chia.server.api_protocol import ApiProtocol
@@ -514,7 +521,7 @@ async def partial_proofs(self, partial_proof_data: PartialProofsData, peer: WSCh
514521
size=partial_proof_data.plot_size,
515522
)
516523

517-
key = bytes(partial_proof)
524+
key = serialize(partial_proof)
518525
try:
519526
# store pending request data for matching with response
520527
self.farmer.pending_solver_requests[key] = {
@@ -543,7 +550,7 @@ async def solution_response(self, response: SolverResponse, peer: WSChiaConnecti
543550

544551
# find the matching pending request using partial_proof
545552

546-
key = bytes(response.partial_proof)
553+
key = serialize(response.partial_proof)
547554
if key not in self.farmer.pending_solver_requests:
548555
self.farmer.log.warning(f"Received solver response for unknown partial proof {response.partial_proof[:5]}")
549556
return

0 commit comments

Comments
 (0)