Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky committed Oct 31, 2024
1 parent f938193 commit 78e9c3e
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 157 deletions.
4 changes: 2 additions & 2 deletions chia/_tests/core/mempool/test_mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,12 @@ async def test_basic_mempool(
assert spend_bundle is not None


api = ApiMetadata()
metadata = ApiMetadata()


# this (method'ish) function is not designed per normal uses so allowing the ignore
# for the different return type. normal is Optional[Message]
@api.request(peer_required=True, bytes_required=True) # type: ignore[type-var]
@metadata.request(peer_required=True, bytes_required=True) # type: ignore[type-var]
async def respond_transaction(
self: FullNodeAPI,
tx: full_node_protocol.RespondTransaction,
Expand Down
4 changes: 2 additions & 2 deletions chia/_tests/core/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
@dataclass
class TestAPI:
log: logging.Logger = logging.getLogger(__name__)
api: ClassVar[ApiMetadata] = ApiMetadata()
metadata: ClassVar[ApiMetadata] = ApiMetadata()

def ready(self) -> bool:
return True

# API call from FullNodeAPI
@api.request()
@metadata.request()
async def request_transaction(self, request: RequestTransaction) -> None:
raise ApiError(Err.NO_TRANSACTIONS_WHILE_SYNCING, f"Some error message: {request.transaction_id}", b"ab")

Expand Down
47 changes: 27 additions & 20 deletions chia/_tests/farmer_harvester/test_third_party_harvesters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import base64
import contextlib
import dataclasses
import json
import logging
Expand All @@ -10,7 +11,6 @@

import pytest
from chia_rs import G1Element
from pytest_mock import MockerFixture

from chia._tests.util.misc import patch_request_handler
from chia._tests.util.time_out_assert import time_out_assert
Expand Down Expand Up @@ -60,7 +60,6 @@ async def test_harvester_receive_source_signing_data(
Union[FullNodeService, SimulatorFullNodeService],
BlockTools,
],
mocker: MockerFixture,
) -> None:
"""
Tests that the source data for the signatures requests sent to the
Expand Down Expand Up @@ -243,29 +242,37 @@ async def intercept_farmer_request_signed_values(*args: Any) -> Optional[Message

return await FarmerAPI.request_signed_values(farmer.server.api, request)

with patch_request_handler(
api=FarmerAPI,
handler=intercept_farmer_request_signed_values,
request_type=ProtocolMessageTypes.request_signed_values,
):
with patch_request_handler(
api=FarmerAPI,
handler=intercept_farmer_new_proof_of_space,
request_type=ProtocolMessageTypes.new_proof_of_space,
):
with patch_request_handler(
with contextlib.ExitStack() as exit_stack:
exit_stack.enter_context(
patch_request_handler(
api=FarmerAPI,
handler=intercept_farmer_request_signed_values,
request_type=ProtocolMessageTypes.request_signed_values,
)
)
exit_stack.enter_context(
patch_request_handler(
api=FarmerAPI,
handler=intercept_farmer_new_proof_of_space,
request_type=ProtocolMessageTypes.new_proof_of_space,
)
)
exit_stack.enter_context(
patch_request_handler(
api=HarvesterAPI,
handler=intercept_harvester_request_signatures,
request_type=ProtocolMessageTypes.request_signatures,
):
# Start injecting signage points
await inject_signage_points(signage_points, full_node_1, full_node_2)
)
)

# Start injecting signage points
await inject_signage_points(signage_points, full_node_1, full_node_2)

# Wait until test finishes
def did_finished_validating_data() -> bool:
return finished_validating_data
# Wait until test finishes
def did_finished_validating_data() -> bool:
return finished_validating_data

await time_out_assert(60 * 60, did_finished_validating_data, True)
await time_out_assert(60 * 60, did_finished_validating_data, True)


@pytest.mark.anyio
Expand Down
4 changes: 2 additions & 2 deletions chia/_tests/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,11 @@ def patch_request_handler(
if request_type is None:
request_type = ProtocolMessageTypes[handler.__name__]

api_metadata = ApiMetadata(message_type_to_request=dict(api.api.message_type_to_request))
api_metadata = ApiMetadata(message_type_to_request=dict(api.metadata.message_type_to_request))
del api_metadata.message_type_to_request[request_type]
api_metadata.request(peer_required=peer_required, request_type=request_type)(handler)

with pytest.MonkeyPatch().context() as m:
m.setattr(api, "api", api_metadata)
m.setattr(api, "metadata", api_metadata)

yield
9 changes: 6 additions & 3 deletions chia/_tests/wallet/test_wallet_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from chia._tests.util.misc import CoinGenerator, patch_request_handler
from chia._tests.util.setup_nodes import OldSimulatorsAndWallets
from chia._tests.util.time_out_assert import time_out_assert
from chia.full_node.full_node_api import FullNodeAPI
from chia.protocols import wallet_protocol
from chia.protocols.protocol_message_types import ProtocolMessageTypes
from chia.protocols.wallet_protocol import CoinState
Expand Down Expand Up @@ -630,7 +629,8 @@ async def send_transaction(
logged_spends.append(request.transaction.name())
return None

with patch_request_handler(api=FullNodeAPI, handler=send_transaction):
assert full_node_api.full_node._server is not None
with patch_request_handler(api=full_node_api.full_node._server.get_connections()[0].api, handler=send_transaction):
# Generate the transaction
async with wallet.wallet_state_manager.new_action_scope(DEFAULT_TX_CONFIG, push=True) as action_scope:
await wallet.generate_signed_transaction(uint64(0), bytes32.zeros, action_scope)
Expand Down Expand Up @@ -700,7 +700,10 @@ async def validate_received_state_from_peer(*args: Any) -> bool:
# It's an interesting case here where we don't hit this unless something is broken
return True # pragma: no cover

with patch_request_handler(api=FullNodeAPI, handler=register_interest_in_coin):
assert full_node_api.full_node._server is not None
with patch_request_handler(
api=full_node_api.full_node._server.get_connections()[0].api, handler=register_interest_in_coin
):
monkeypatch.setattr(
wallet_node,
"validate_received_state_from_peer",
Expand Down
2 changes: 1 addition & 1 deletion chia/data_layer/data_layer_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DataLayerAPI:

log: logging.Logger
data_layer: DataLayer
api: ClassVar[ApiMetadata] = ApiMetadata()
metadata: ClassVar[ApiMetadata] = ApiMetadata()

def __init__(self, data_layer: DataLayer) -> None:
self.log = logging.getLogger(__name__)
Expand Down
28 changes: 14 additions & 14 deletions chia/farmer/farmer_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FarmerAPI:

log: logging.Logger
farmer: Farmer
api: ClassVar[ApiMetadata] = ApiMetadata()
metadata: ClassVar[ApiMetadata] = ApiMetadata()

def __init__(self, farmer: Farmer) -> None:
self.log = logging.getLogger(__name__)
Expand All @@ -64,7 +64,7 @@ def __init__(self, farmer: Farmer) -> None:
def ready(self) -> bool:
return self.farmer.started

@api.request(peer_required=True)
@metadata.request(peer_required=True)
async def new_proof_of_space(
self, new_proof_of_space: harvester_protocol.NewProofOfSpace, peer: WSChiaConnection
) -> None:
Expand Down Expand Up @@ -477,7 +477,7 @@ async def new_proof_of_space(

return

@api.request()
@metadata.request()
async def respond_signatures(self, response: harvester_protocol.RespondSignatures) -> None:
request = self._process_respond_signatures(response)
if request is None:
Expand All @@ -495,7 +495,7 @@ async def respond_signatures(self, response: harvester_protocol.RespondSignature
FARMER PROTOCOL (FARMER <-> FULL NODE)
"""

@api.request()
@metadata.request()
async def new_signage_point(self, new_signage_point: farmer_protocol.NewSignagePoint) -> None:
if new_signage_point.challenge_chain_sp not in self.farmer.sps:
self.farmer.sps[new_signage_point.challenge_chain_sp] = []
Expand Down Expand Up @@ -564,7 +564,7 @@ async def new_signage_point(self, new_signage_point: farmer_protocol.NewSignageP
{"sp_hash": new_signage_point.challenge_chain_sp, "missing_signage_points": missing_signage_points},
)

@api.request()
@metadata.request()
async def request_signed_values(self, full_node_request: farmer_protocol.RequestSignedValues) -> Optional[Message]:
if full_node_request.quality_string not in self.farmer.quality_str_to_identifiers:
self.farmer.log.error(f"Do not have quality string {full_node_request.quality_string}")
Expand Down Expand Up @@ -613,7 +613,7 @@ async def request_signed_values(self, full_node_request: farmer_protocol.Request

return make_msg(ProtocolMessageTypes.signed_values, signed_values)

@api.request(peer_required=True)
@metadata.request(peer_required=True)
async def farming_info(self, request: farmer_protocol.FarmingInfo, peer: WSChiaConnection) -> None:
self.farmer.state_changed(
"new_farming_info",
Expand All @@ -631,35 +631,35 @@ async def farming_info(self, request: farmer_protocol.FarmingInfo, peer: WSChiaC
},
)

@api.request(peer_required=True)
@metadata.request(peer_required=True)
async def respond_plots(self, _: harvester_protocol.RespondPlots, peer: WSChiaConnection) -> None:
self.farmer.log.warning(f"Respond plots came too late from: {peer.get_peer_logging()}")

@api.request(peer_required=True)
@metadata.request(peer_required=True)
async def plot_sync_start(self, message: PlotSyncStart, peer: WSChiaConnection) -> None:
await self.farmer.plot_sync_receivers[peer.peer_node_id].sync_started(message)

@api.request(peer_required=True)
@metadata.request(peer_required=True)
async def plot_sync_loaded(self, message: PlotSyncPlotList, peer: WSChiaConnection) -> None:
await self.farmer.plot_sync_receivers[peer.peer_node_id].process_loaded(message)

@api.request(peer_required=True)
@metadata.request(peer_required=True)
async def plot_sync_removed(self, message: PlotSyncPathList, peer: WSChiaConnection) -> None:
await self.farmer.plot_sync_receivers[peer.peer_node_id].process_removed(message)

@api.request(peer_required=True)
@metadata.request(peer_required=True)
async def plot_sync_invalid(self, message: PlotSyncPathList, peer: WSChiaConnection) -> None:
await self.farmer.plot_sync_receivers[peer.peer_node_id].process_invalid(message)

@api.request(peer_required=True)
@metadata.request(peer_required=True)
async def plot_sync_keys_missing(self, message: PlotSyncPathList, peer: WSChiaConnection) -> None:
await self.farmer.plot_sync_receivers[peer.peer_node_id].process_keys_missing(message)

@api.request(peer_required=True)
@metadata.request(peer_required=True)
async def plot_sync_duplicates(self, message: PlotSyncPathList, peer: WSChiaConnection) -> None:
await self.farmer.plot_sync_receivers[peer.peer_node_id].process_duplicates(message)

@api.request(peer_required=True)
@metadata.request(peer_required=True)
async def plot_sync_done(self, message: PlotSyncDone, peer: WSChiaConnection) -> None:
await self.farmer.plot_sync_receivers[peer.peer_node_id].sync_done(message)

Expand Down
Loading

0 comments on commit 78e9c3e

Please sign in to comment.