Skip to content

Commit

Permalink
Send RewardChainBlockUnfinished to third-party harvesters (Chia-Netwo…
Browse files Browse the repository at this point in the history
…rk#17435)

* Send RewardChainBlockUnfinished to the harvester

- Simplify data pasing in request_signed_values

* Fix instances where the rc_block_unfinished must be sent

* Lint, fix test, generate protocol messages

* Rate Limit Adjustment for RequestSignedValues (Chia-Network#17481)

* Further bump request_signed_values rate limit

---------

Co-authored-by: Amine Khaldi <[email protected]>
Co-authored-by: Luna <[email protected]>
  • Loading branch information
3 people authored and felixbrucker committed Feb 16, 2024
1 parent 8ea2660 commit 7d3f9c2
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 20 deletions.
32 changes: 16 additions & 16 deletions chia/farmer/farmer_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ async def new_proof_of_space(
new_proof_of_space.sp_hash,
[sp.challenge_chain_sp, sp.reward_chain_sp],
message_data=sp_src_data,
rc_block_unfinished=None,
)

if new_proof_of_space.sp_hash not in self.farmer.proofs_of_space:
Expand Down Expand Up @@ -284,6 +285,7 @@ async def new_proof_of_space(
new_proof_of_space.sp_hash,
[m_to_sign],
message_data=m_src_data,
rc_block_unfinished=None,
)
response: Any = await peer.call_api(HarvesterAPI.request_signatures, request)
if not isinstance(response, harvester_protocol.RespondSignatures):
Expand Down Expand Up @@ -565,30 +567,28 @@ async def request_signed_values(self, full_node_request: farmer_protocol.Request
full_node_request.quality_string
]

foliage_block_data: Optional[SignatureRequestSourceData] = None
foliage_transaction_block_data: Optional[SignatureRequestSourceData] = None
include_source_data = False
message_data: Optional[List[Optional[SignatureRequestSourceData]]] = None

if full_node_request.foliage_block_data is not None:
include_source_data = True
foliage_block_data = SignatureRequestSourceData(
uint8(SigningDataKind.FOLIAGE_BLOCK_DATA), bytes(full_node_request.foliage_block_data)
)

if full_node_request.foliage_transaction_block_data is not None:
assert foliage_block_data
include_source_data = True
foliage_transaction_block_data = SignatureRequestSourceData(
uint8(SigningDataKind.FOLIAGE_TRANSACTION_BLOCK),
bytes(full_node_request.foliage_transaction_block_data),
)
message_data = [
SignatureRequestSourceData(
uint8(SigningDataKind.FOLIAGE_BLOCK_DATA), bytes(full_node_request.foliage_block_data)
),
None
if full_node_request.foliage_transaction_block_data is None
else SignatureRequestSourceData(
uint8(SigningDataKind.FOLIAGE_TRANSACTION_BLOCK),
bytes(full_node_request.foliage_transaction_block_data),
),
]

request = harvester_protocol.RequestSignatures(
plot_identifier,
challenge_hash,
sp_hash,
[full_node_request.foliage_block_data_hash, full_node_request.foliage_transaction_block_hash],
message_data=[foliage_block_data, foliage_transaction_block_data] if include_source_data else None,
message_data=message_data,
rc_block_unfinished=full_node_request.rc_block_unfinished,
)

response = await self.farmer.server.call_api_of_specific(HarvesterAPI.request_signatures, request, node_id)
Expand Down
8 changes: 6 additions & 2 deletions chia/full_node/full_node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from chia.types.blockchain_format.foliage import FoliageBlockData, FoliageTransactionBlock
from chia.types.blockchain_format.pool_target import PoolTarget
from chia.types.blockchain_format.proof_of_space import verify_and_get_quality_string
from chia.types.blockchain_format.reward_chain_block import RewardChainBlockUnfinished
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.blockchain_format.sub_epoch_summary import SubEpochSummary
from chia.types.coin_record import CoinRecord
Expand Down Expand Up @@ -1021,17 +1022,20 @@ def get_pool_sig(_1: PoolTarget, _2: Optional[G1Element]) -> Optional[G2Element]

foliage_block_data: Optional[FoliageBlockData] = None
foliage_transaction_block_data: Optional[FoliageTransactionBlock] = None
rc_block_unfinished: Optional[RewardChainBlockUnfinished] = None
if request.include_signature_source_data:
foliage_block_data = unfinished_block.foliage.foliage_block_data
rc_block_unfinished = unfinished_block.reward_chain_block
if unfinished_block.is_transaction_block():
foliage_transaction_block_data = unfinished_block.foliage_transaction_block

message = farmer_protocol.RequestSignedValues(
quality_string,
foliage_sb_data_hash,
foliage_transaction_block_hash,
foliage_block_data,
foliage_transaction_block_data,
foliage_block_data=foliage_block_data,
foliage_transaction_block_data=foliage_transaction_block_data,
rc_block_unfinished=rc_block_unfinished,
)
await peer.send_message(make_msg(ProtocolMessageTypes.request_signed_values, message))

Expand Down
2 changes: 2 additions & 0 deletions chia/protocols/farmer_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from chia.types.blockchain_format.foliage import FoliageBlockData, FoliageTransactionBlock
from chia.types.blockchain_format.pool_target import PoolTarget
from chia.types.blockchain_format.proof_of_space import ProofOfSpace
from chia.types.blockchain_format.reward_chain_block import RewardChainBlockUnfinished
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.blockchain_format.slots import ChallengeChainSubSlot, RewardChainSubSlot
from chia.util.ints import uint8, uint32, uint64
Expand Down Expand Up @@ -78,6 +79,7 @@ class RequestSignedValues(Streamable):
foliage_transaction_block_hash: bytes32
foliage_block_data: Optional[FoliageBlockData] = None
foliage_transaction_block_data: Optional[FoliageTransactionBlock] = None
rc_block_unfinished: Optional[RewardChainBlockUnfinished] = None


@streamable
Expand Down
3 changes: 3 additions & 0 deletions chia/protocols/harvester_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from chia_rs import G1Element, G2Element

from chia.types.blockchain_format.proof_of_space import ProofOfSpace
from chia.types.blockchain_format.reward_chain_block import RewardChainBlockUnfinished
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.ints import int16, uint8, uint32, uint64
from chia.util.streamable import Streamable, streamable
Expand Down Expand Up @@ -90,7 +91,9 @@ class RequestSignatures(Streamable):
challenge_hash: bytes32
sp_hash: bytes32
messages: List[bytes32]
# This, and rc_block_unfinished are only set when using a third-party harvester (see CHIP-22)
message_data: Optional[List[Optional[SignatureRequestSourceData]]]
rc_block_unfinished: Optional[RewardChainBlockUnfinished]


@streamable
Expand Down
2 changes: 1 addition & 1 deletion chia/server/rate_limit_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def compose_rate_limits(old_rate_limits: Dict[str, Any], new_rate_limits: Dict[s
ProtocolMessageTypes.respond_signatures: RLSettings(100, 2048),
ProtocolMessageTypes.new_signage_point: RLSettings(200, 2048),
ProtocolMessageTypes.declare_proof_of_space: RLSettings(100, 10 * 1024),
ProtocolMessageTypes.request_signed_values: RLSettings(100, 512),
ProtocolMessageTypes.request_signed_values: RLSettings(100, 10 * 1024),
ProtocolMessageTypes.farming_info: RLSettings(100, 1024),
ProtocolMessageTypes.signed_values: RLSettings(100, 1024),
ProtocolMessageTypes.new_peak_timelord: RLSettings(100, 20 * 1024),
Expand Down
5 changes: 5 additions & 0 deletions tests/farmer_harvester/test_third_party_harvesters.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ async def intercept_harvester_request_signatures(*args: Any) -> Message:
return make_msg(ProtocolMessageTypes.respond_signatures, response)

def validate_harvester_request_signatures(request: harvester_protocol.RequestSignatures) -> None:
nonlocal farmer
nonlocal full_node_2
nonlocal farmer_reward_address
nonlocal validated_foliage_data
Expand Down Expand Up @@ -163,6 +164,10 @@ def validate_harvester_request_signatures(request: harvester_protocol.RequestSig
or data.farmer_reward_puzzle_hash
== bytes32(full_node_2.constants.GENESIS_PRE_FARM_FARMER_PUZZLE_HASH)
)
# RC block unfinished must always be present when foliage block data is present
assert request.rc_block_unfinished
assert request.rc_block_unfinished.get_hash() == data.unfinished_reward_block_hash

if data.farmer_reward_puzzle_hash == farmer_reward_address:
validated_foliage_data = True
elif src.kind == uint8(SigningDataKind.FOLIAGE_TRANSACTION_BLOCK):
Expand Down
7 changes: 6 additions & 1 deletion tests/util/network_protocol_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from chia_rs import G1Element, G2Element
from chia_rs import G1Element, G2Element, RewardChainBlockUnfinished

from chia.protocols import (
farmer_protocol,
Expand Down Expand Up @@ -777,6 +777,11 @@
bytes(post_partial_payload),
),
],
RewardChainBlockUnfinished.from_bytes(
bytes.fromhex(
"000000000000000000000000000e006704e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85511ddefb0e56532907ceee3d48b23896b55863b9ba71e06441caf274389616c7600011b9d1eaa3c6a9b27cd90ad9070eb012794a74b277446417bc7b904145010c0878fde05dff50c7409c6ac3c5c42d7cdf596e6232bdfa833e715dbd19cfec3e9d08a407b11288a0a6fd25a932ebd0d6a3f14000000a06b99913f88112ab38726b8119e7f7006b24274f1a8c4c552c2a23c8076bdf0d4e8546c4a47fb5b82041e429a3fd44936ce5e9d811a4996a6f0d2896805b6ae018430d944db57080b1abb5863cb48c6deeaf86e93e6c2f0f8f88f527aa4966d88e1e607dadb45869f18225ef1b987d2b43a08a8defff0b0320106fdaad531edfca2b7b78d873d3f20a3780b2baaea3caafb8ad47f43e649872d16e69afd06527d01e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855000000000008000003005f4faabe7ceb8796e2d8db3db666212273962e2bf1e951717352e0d284ec7b18f4595dafdb7edd4e0e2ae7b231bce7ca75f551afcea51b32679713aa01faf62f375f821e835c333cc2d2a4d8c9e5edaae1702dd82f1f9dd63841e572df587d110100b243a5388d5858bcfa76cc93f53eba8479136480ae9c1b892ebdae4180f4f4c2fbf9a1ae01a59e634f877cb1b74cbc7805c0be9c97e6c133a0e7c29a9d89bfc8b08250184d85275c8cb64401ade1bb06443c92df171ca527dee1af4e81cd931001e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855000000000008000003005f4faabe7ceb8796e2d8db3db666212273962e2bf1e951717352e0d284ec7b18f4595dafdb7edd4e0e2ae7b231bce7ca75f551afcea51b32679713aa01faf62f375f821e835c333cc2d2a4d8c9e5edaae1702dd82f1f9dd63841e572df587d110100b243a5388d5858bcfa76cc93f53eba8479136480ae9c1b892ebdae4180f4f4c2fbf9a1ae01a59e634f877cb1b74cbc7805c0be9c97e6c133a0e7c29a9d89bfc8b08250184d85275c8cb64401ade1bb06443c92df171ca527dee1af4e81cd9310"
)
),
)

respond_signatures = harvester_protocol.RespondSignatures(
Expand Down
Binary file modified tests/util/protocol_messages_bytes-v1.0
Binary file not shown.
30 changes: 30 additions & 0 deletions tests/util/protocol_messages_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"foliage_transaction_block_hash": "0x5d76a4bcb3524d862e92317410583daf50828927885444c6d62ca8843635c46f",
"foliage_block_data": None,
"foliage_transaction_block_data": None,
"rc_block_unfinished": None,
}

farming_info_json: Dict[str, Any] = {
Expand Down Expand Up @@ -2095,6 +2096,35 @@
"data": "0xdada61e179e67e5e8bc7aaab16e192facf0f15871f0c479d2a96ac5f85721a1a2293a92f557d08441fb331df88bc142e70c110e21620374118fb220ccc3ef621378197e850882ec901a04c6b5ac7dfb935f6feecfdd72348ccf1d4be4fe7e26acf271ea3b7d308da61e0a308f7a62495328a81f5147b66634c00b6449c2c68df97c19e884427e42ee7350982d4020571ead08732615ff39bd216bfd630b6460784982bec98b49fea79d0cc000000b0a67188ae0c02c49b0e821a9773033a3fbd338030c383080dbb8b1d63f07af427d8075e59d911f85ea562fd967823588f9a405a4464fdf5dc0866ee15bebd6b94cb147e28aa9cf96da930611486b779737ed721ea376b9939ba05357141223d75d21b21f310ec32d85ed3b98cf301494ea91b8501138481f3bfa1c384fd998b1fdd2855ac6f0c8554c520fb0bfa3663f238124035e14682bc11eaf7c372b6af4ed7f59a406810c71711906f8c91f94b1f929287fab514e2204808821e2afe8c4d84f0093c75554b067fe4fca272890c9d00f98dff6bdcc3926b33cb8ab22e11bd15c13d6a9b6832ac948b3273f5ccd8e7ec",
},
],
"rc_block_unfinished": {
"total_iters": 917607,
"signage_point_index": 4,
"pos_ss_cc_challenge_hash": "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"proof_of_space": {
"challenge": "0x11ddefb0e56532907ceee3d48b23896b55863b9ba71e06441caf274389616c76",
"pool_public_key": None,
"pool_contract_puzzle_hash": "0x1b9d1eaa3c6a9b27cd90ad9070eb012794a74b277446417bc7b904145010c087",
"plot_public_key": "0x8fde05dff50c7409c6ac3c5c42d7cdf596e6232bdfa833e715dbd19cfec3e9d08a407b11288a0a6fd25a932ebd0d6a3f",
"size": 20,
"proof": "0x6b99913f88112ab38726b8119e7f7006b24274f1a8c4c552c2a23c8076bdf0d4e8546c4a47fb5b82041e429a3fd44936ce5e9d811a4996a6f0d2896805b6ae018430d944db57080b1abb5863cb48c6deeaf86e93e6c2f0f8f88f527aa4966d88e1e607dadb45869f18225ef1b987d2b43a08a8defff0b0320106fdaad531edfca2b7b78d873d3f20a3780b2baaea3caafb8ad47f43e649872d16e69afd06527d",
},
"challenge_chain_sp_vdf": {
"challenge": "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"number_of_iterations": 524288,
"output": {
"data": "0x03005f4faabe7ceb8796e2d8db3db666212273962e2bf1e951717352e0d284ec7b18f4595dafdb7edd4e0e2ae7b231bce7ca75f551afcea51b32679713aa01faf62f375f821e835c333cc2d2a4d8c9e5edaae1702dd82f1f9dd63841e572df587d110100"
},
},
"challenge_chain_sp_signature": "0xb243a5388d5858bcfa76cc93f53eba8479136480ae9c1b892ebdae4180f4f4c2fbf9a1ae01a59e634f877cb1b74cbc7805c0be9c97e6c133a0e7c29a9d89bfc8b08250184d85275c8cb64401ade1bb06443c92df171ca527dee1af4e81cd9310",
"reward_chain_sp_vdf": {
"challenge": "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"number_of_iterations": 524288,
"output": {
"data": "0x03005f4faabe7ceb8796e2d8db3db666212273962e2bf1e951717352e0d284ec7b18f4595dafdb7edd4e0e2ae7b231bce7ca75f551afcea51b32679713aa01faf62f375f821e835c333cc2d2a4d8c9e5edaae1702dd82f1f9dd63841e572df587d110100"
},
},
"reward_chain_sp_signature": "0xb243a5388d5858bcfa76cc93f53eba8479136480ae9c1b892ebdae4180f4f4c2fbf9a1ae01a59e634f877cb1b74cbc7805c0be9c97e6c133a0e7c29a9d89bfc8b08250184d85275c8cb64401ade1bb06443c92df171ca527dee1af4e81cd9310",
},
}

respond_signatures_json: Dict[str, Any] = {
Expand Down

0 comments on commit 7d3f9c2

Please sign in to comment.