Repository: Chia-Network/chia-blockchain
File: chia/protocols/protocol_message_type_to_node_type.py (Python)

Code before:

from chia.protocols.outbound_message import NodeType
from chia.protocols.protocol_message_types import ProtocolMessageTypes

ProtocolMessageTypeToNodeType: dict[ProtocolMessageTypes, set[NodeType]] = {
    # Shared protocol (all services)
    ProtocolMessageTypes.handshake: set(NodeType),
    # Harvester protocol (harvester <-> farmer)
    ProtocolMessageTypes.harvester_handshake: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.new_proof_of_space: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.request_signatures: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_signatures: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.partial_proofs: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.new_signage_point_harvester: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.request_plots: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_plots: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.plot_sync_start: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.plot_sync_loaded: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.plot_sync_removed: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.plot_sync_invalid: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.plot_sync_keys_missing: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.plot_sync_duplicates: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.plot_sync_done: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.plot_sync_response: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    # Farmer protocol (farmer <-> full_node)
    ProtocolMessageTypes.new_signage_point: {NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.declare_proof_of_space: {NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.request_signed_values: {NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.signed_values: {NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.farming_info: {NodeType.HARVESTER, NodeType.FARMER, NodeType.FULL_NODE},
    ProtocolMessageTypes.solution_response: {NodeType.FARMER, NodeType.FULL_NODE},
    # Timelord protocol (timelord <-> full_node)
    ProtocolMessageTypes.new_peak_timelord: {NodeType.TIMELORD, NodeType.FULL_NODE},
    ProtocolMessageTypes.new_unfinished_block_timelord: {NodeType.TIMELORD, NodeType.FULL_NODE},
    ProtocolMessageTypes.new_infusion_point_vdf: {NodeType.TIMELORD, NodeType.FULL_NODE},
    ProtocolMessageTypes.new_signage_point_vdf: {NodeType.TIMELORD, NodeType.FULL_NODE},
    ProtocolMessageTypes.new_end_of_sub_slot_vdf: {NodeType.TIMELORD, NodeType.FULL_NODE},
    ProtocolMessageTypes.request_compact_proof_of_time: {NodeType.TIMELORD, NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_compact_proof_of_time: {NodeType.TIMELORD, NodeType.FULL_NODE},
    # Full node protocol (full_node <-> full_node)
    ProtocolMessageTypes.new_peak: {NodeType.FULL_NODE},
    ProtocolMessageTypes.new_transaction: {NodeType.FULL_NODE},
    ProtocolMessageTypes.request_transaction: {NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_transaction: {NodeType.FULL_NODE},
    ProtocolMessageTypes.request_proof_of_weight: {NodeType.WALLET, NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_proof_of_weight: {NodeType.FULL_NODE},
    ProtocolMessageTypes.request_block: {NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_block: {NodeType.FULL_NODE},
    ProtocolMessageTypes.reject_block: {NodeType.FULL_NODE},
    ProtocolMessageTypes.request_blocks: {NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_blocks: {NodeType.FULL_NODE},

Review comment (suggestion):
I don't think a farmer ever sends this to the full node. So, should `FARMER` really be in this set?

Isn't this indicating which node type is allowed to send a specific message? It looks like most entries here have the *receiving* node type as well. it doesn't look right.

Code after:

from chia.protocols.outbound_message import NodeType
from chia.protocols.protocol_message_types import ProtocolMessageTypes

# Map of protocol message type to the node types that send it. We use this to
# validate which nodes are allowed to send which messages so that all api calls
# are type checked.
ProtocolMessageTypeToNodeType: dict[ProtocolMessageTypes, set[NodeType]] = {
    # Shared protocol (all services)
    ProtocolMessageTypes.handshake: set(NodeType),
    # Harvester protocol (harvester <-> farmer)
    ProtocolMessageTypes.harvester_handshake: {NodeType.FARMER},
    ProtocolMessageTypes.new_proof_of_space: {NodeType.HARVESTER},
    ProtocolMessageTypes.request_signatures: {NodeType.FARMER},
    ProtocolMessageTypes.respond_signatures: {NodeType.HARVESTER},
    ProtocolMessageTypes.partial_proofs: {NodeType.HARVESTER},
    ProtocolMessageTypes.new_signage_point_harvester: {NodeType.FARMER},
    ProtocolMessageTypes.request_plots: {NodeType.FARMER},
    ProtocolMessageTypes.respond_plots: {NodeType.HARVESTER},
    ProtocolMessageTypes.plot_sync_start: {NodeType.HARVESTER},
    ProtocolMessageTypes.plot_sync_loaded: {NodeType.HARVESTER},
    ProtocolMessageTypes.plot_sync_removed: {NodeType.HARVESTER},
    ProtocolMessageTypes.plot_sync_invalid: {NodeType.HARVESTER},
    ProtocolMessageTypes.plot_sync_keys_missing: {NodeType.HARVESTER},
    ProtocolMessageTypes.plot_sync_duplicates: {NodeType.HARVESTER},
    ProtocolMessageTypes.plot_sync_done: {NodeType.HARVESTER},
    ProtocolMessageTypes.plot_sync_response: {NodeType.FARMER},
    # Farmer protocol (farmer <-> full_node)
    ProtocolMessageTypes.new_signage_point: {NodeType.FULL_NODE},
    ProtocolMessageTypes.declare_proof_of_space: {NodeType.FARMER},
    ProtocolMessageTypes.request_signed_values: {NodeType.FULL_NODE},
    ProtocolMessageTypes.signed_values: {NodeType.FARMER},
    ProtocolMessageTypes.farming_info: {NodeType.HARVESTER},
    ProtocolMessageTypes.solution_response: {NodeType.SOLVER},
    # Timelord protocol (timelord <-> full_node)
    ProtocolMessageTypes.new_peak_timelord: {NodeType.FULL_NODE},
    ProtocolMessageTypes.new_unfinished_block_timelord: {NodeType.FULL_NODE},
    ProtocolMessageTypes.new_infusion_point_vdf: {NodeType.TIMELORD},
    ProtocolMessageTypes.new_signage_point_vdf: {NodeType.TIMELORD},
    ProtocolMessageTypes.new_end_of_sub_slot_vdf: {NodeType.TIMELORD},
    ProtocolMessageTypes.request_compact_proof_of_time: {NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_compact_proof_of_time: {NodeType.TIMELORD},
    # Full node protocol (full_node <-> full_node)
    ProtocolMessageTypes.new_peak: {NodeType.FULL_NODE},
    ProtocolMessageTypes.new_transaction: {NodeType.FULL_NODE},
    ProtocolMessageTypes.request_transaction: {NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_transaction: {NodeType.FULL_NODE},
    ProtocolMessageTypes.request_proof_of_weight: {NodeType.WALLET, NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_proof_of_weight: {NodeType.FULL_NODE},
    ProtocolMessageTypes.request_block: {NodeType.FULL_NODE},
    ProtocolMessageTypes.respond_block: {NodeType.FULL_NODE},