mirror of https://github.com/nucypher/nucypher.git
Finishes removal of control package, it's usages and test utils
parent
73b6b8d15d
commit
4a4e8dd594
|
@ -211,13 +211,6 @@ class Character(Learner):
|
|||
|
||||
self.__setup_nickname(is_me=is_me)
|
||||
|
||||
# Character Control
|
||||
# TODO: have argument about meaning of 'lawful' and whether maybe only Lawful characters have an interface
|
||||
if hasattr(self, '_interface_class'):
|
||||
# Controller Interface
|
||||
self.interface = self._interface_class(character=self)
|
||||
self.controller = NO_CONTROL_PROTOCOL
|
||||
|
||||
def __eq__(self, other) -> bool:
|
||||
try:
|
||||
other_stamp = other.stamp
|
||||
|
|
|
@ -93,14 +93,6 @@ def option_contract_name(required: bool = False,
|
|||
)
|
||||
|
||||
|
||||
def option_controller_port(default=None):
|
||||
return click.option(
|
||||
'--controller-port',
|
||||
help="The host port to run HTTP services on",
|
||||
type=NETWORK_PORT,
|
||||
default=default)
|
||||
|
||||
|
||||
def option_discovery_port(default=None):
|
||||
return click.option(
|
||||
'--discovery-port',
|
||||
|
|
|
@ -22,7 +22,6 @@ from pathlib import Path
|
|||
from typing import Dict, Optional, Tuple
|
||||
|
||||
import click
|
||||
from constant_sorrow.constants import NO_CONTROL_PROTOCOL
|
||||
from web3.types import BlockIdentifier
|
||||
|
||||
from nucypher.blockchain.eth.agents import EthereumContractAgent
|
||||
|
@ -116,9 +115,6 @@ def make_cli_character(character_config,
|
|||
# Post-Init
|
||||
#
|
||||
|
||||
if CHARACTER.controller is not NO_CONTROL_PROTOCOL:
|
||||
CHARACTER.controller.emitter = emitter
|
||||
|
||||
# Federated
|
||||
if character_config.federated_only:
|
||||
emitter.message(FEDERATED_WARNING, color='yellow')
|
||||
|
|
|
@ -325,7 +325,6 @@ class CharacterConfiguration(BaseConfiguration):
|
|||
VERSION = 4 # bump when static payload scheme changes
|
||||
|
||||
CHARACTER_CLASS = NotImplemented
|
||||
DEFAULT_CONTROLLER_PORT = NotImplemented
|
||||
MNEMONIC_KEYSTORE = False
|
||||
DEFAULT_DOMAIN = NetworksInventory.DEFAULT
|
||||
DEFAULT_NETWORK_MIDDLEWARE = RestMiddleware
|
||||
|
@ -383,7 +382,6 @@ class CharacterConfiguration(BaseConfiguration):
|
|||
start_learning_now: bool = True,
|
||||
|
||||
# Network
|
||||
controller_port: int = None,
|
||||
domain: str = DEFAULT_DOMAIN,
|
||||
network_middleware: RestMiddleware = None,
|
||||
lonely: bool = False,
|
||||
|
@ -573,7 +571,6 @@ class CharacterConfiguration(BaseConfiguration):
|
|||
self._setup_node_storage(node_storage=node_storage)
|
||||
|
||||
# Network
|
||||
self.controller_port = controller_port or self.DEFAULT_CONTROLLER_PORT
|
||||
self.network_middleware = network_middleware or self.DEFAULT_NETWORK_MIDDLEWARE(registry=self.registry)
|
||||
|
||||
super().__init__(filepath=self.config_file_location, config_root=self.config_root)
|
||||
|
|
|
@ -145,8 +145,6 @@ class AliceConfiguration(CharacterConfiguration):
|
|||
CHARACTER_CLASS = Alice
|
||||
NAME = CHARACTER_CLASS.__name__.lower()
|
||||
|
||||
DEFAULT_CONTROLLER_PORT = 8151
|
||||
|
||||
# TODO: Best (Sane) Defaults
|
||||
DEFAULT_THRESHOLD = 2
|
||||
DEFAULT_SHARES = 3
|
||||
|
@ -211,7 +209,6 @@ class BobConfiguration(CharacterConfiguration):
|
|||
|
||||
CHARACTER_CLASS = Bob
|
||||
NAME = CHARACTER_CLASS.__name__.lower()
|
||||
DEFAULT_CONTROLLER_PORT = 7151
|
||||
DEFAULT_STORE_POLICIES = True
|
||||
DEFAULT_STORE_CARDS = True
|
||||
SIGNER_ENVVAR = NUCYPHER_ENVVAR_BOB_ETH_PASSWORD
|
||||
|
|
|
@ -20,16 +20,12 @@ from collections import defaultdict
|
|||
import pytest
|
||||
from eth_utils.crypto import keccak
|
||||
|
||||
from nucypher.utilities.emitters import WebEmitter
|
||||
from nucypher.crypto.powers import TransactingPower
|
||||
from nucypher.network.nodes import Learner
|
||||
from nucypher.network.trackers import AvailabilityTracker
|
||||
from nucypher.utilities.logging import GlobalLoggerSettings
|
||||
from tests.constants import MOCK_IP_ADDRESS
|
||||
|
||||
# Crash on server error by default
|
||||
WebEmitter._crash_on_error_default = True
|
||||
|
||||
# Dont re-lock account in background while making commitments
|
||||
LOCK_FUNCTION = TransactingPower.lock_account
|
||||
TransactingPower.lock_account = lambda *a, **k: True
|
||||
|
|
|
@ -82,16 +82,3 @@ def test_get_pyevm_backend_without_development_installation(import_mocker):
|
|||
with pytest.raises(DevelopmentInstallationRequired, match=message): # Expect lazy failure
|
||||
_get_pyevm_test_backend()
|
||||
|
||||
|
||||
def test_rpc_test_client_without_development_installation(import_mocker, mocker):
|
||||
|
||||
# Expected error message (Related object)
|
||||
from tests.utils.controllers import JSONRPCTestClient
|
||||
import_path = f'{JSONRPCTestClient.__module__}.{JSONRPCTestClient.__name__}'
|
||||
message = DevelopmentInstallationRequired.MESSAGE.format(importable_name=import_path)
|
||||
del JSONRPCTestClient
|
||||
|
||||
with import_mocker:
|
||||
from nucypher.control.controllers import JSONRPCController
|
||||
with pytest.raises(DevelopmentInstallationRequired, match=message): # Expect lazy failure
|
||||
JSONRPCController.test_client(self=mocker.Mock())
|
||||
|
|
|
@ -1,176 +0,0 @@
|
|||
"""
|
||||
This file is part of nucypher.
|
||||
|
||||
nucypher is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
nucypher is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
import json
|
||||
from io import StringIO
|
||||
from typing import Union
|
||||
|
||||
import nucypher
|
||||
|
||||
|
||||
def get_fields(interface, method_name):
|
||||
spec = getattr(interface, method_name)._schema
|
||||
input_fields = [k for k, f in spec.load_fields.items() if f.required]
|
||||
optional_fields = [k for k, f in spec.load_fields.items() if not f.required]
|
||||
required_output_fileds = list(spec.dump_fields.keys())
|
||||
|
||||
return (
|
||||
input_fields,
|
||||
optional_fields,
|
||||
required_output_fileds
|
||||
)
|
||||
|
||||
|
||||
def validate_json_rpc_response_data(response, method_name, interface):
|
||||
required_output_fields = get_fields(interface, method_name)[-1]
|
||||
assert 'jsonrpc' in response.data
|
||||
for output_field in required_output_fields:
|
||||
assert output_field in response.content
|
||||
return True
|
||||
|
||||
|
||||
class TestRPCResponse:
|
||||
"""A mock RPC response object"""
|
||||
|
||||
delimiter = '\n'
|
||||
|
||||
def __init__(self,
|
||||
id: int,
|
||||
payload: dict,
|
||||
success: bool,
|
||||
error: bool):
|
||||
|
||||
# Initial State
|
||||
self.id = id
|
||||
self.data = payload
|
||||
self.success = success
|
||||
self.error = error
|
||||
|
||||
def __bytes__(self):
|
||||
return json.dumps(self.data)
|
||||
|
||||
@property
|
||||
def error_code(self):
|
||||
if self.error:
|
||||
return int(self.data['error']['code'])
|
||||
else:
|
||||
return 0
|
||||
|
||||
@property
|
||||
def content(self):
|
||||
if self.success:
|
||||
return self.data['result']
|
||||
else:
|
||||
return self.data['error']
|
||||
|
||||
@classmethod
|
||||
def from_string(cls, response_line: str):
|
||||
outgoing_responses = response_line.strip(cls.delimiter).split(cls.delimiter)
|
||||
|
||||
responses = list()
|
||||
for response in outgoing_responses:
|
||||
# Deserialize
|
||||
response_data = json.loads(response)
|
||||
|
||||
# Check for Success or Error
|
||||
error = False
|
||||
response_id_value = response_data['id']
|
||||
try:
|
||||
response_id = int(response_id_value)
|
||||
except TypeError:
|
||||
if response_id_value is not None:
|
||||
raise
|
||||
error, response_id = True, None
|
||||
|
||||
instance = cls(payload=response_data,
|
||||
success=not error,
|
||||
error=error,
|
||||
id=response_id)
|
||||
|
||||
responses.append(instance)
|
||||
|
||||
# handle one or many requests
|
||||
final_response = responses
|
||||
if not len(responses) > 1:
|
||||
final_response = responses[0]
|
||||
|
||||
return final_response
|
||||
|
||||
|
||||
class JSONRPCTestClient:
|
||||
"""A test client for character RPC control."""
|
||||
|
||||
MESSAGE_ID = 0
|
||||
|
||||
__preamble = json.dumps(dict(jsonrpc="2.0", version=str(nucypher.__version__)))
|
||||
__io = StringIO(initial_value=str(__preamble.encode()))
|
||||
response_sink = __io.write
|
||||
|
||||
def __init__(self, rpc_controller):
|
||||
|
||||
# Divert the emitter flow to the RPC pipe
|
||||
rpc_controller.emitter.sink = self.response_sink
|
||||
self._controller = rpc_controller
|
||||
|
||||
def assemble_request(self, request: Union[dict, list]) -> dict:
|
||||
"""Assemble a JSONRPC2.0 formatted dict for JSON use."""
|
||||
JSONRPCTestClient.MESSAGE_ID += 1
|
||||
method, params = request['method'], request['params']
|
||||
response_data = {'jsonrpc': '2.0',
|
||||
'id': str(JSONRPCTestClient.MESSAGE_ID),
|
||||
'method': method,
|
||||
'params': params}
|
||||
|
||||
return response_data
|
||||
|
||||
def receive(self, size: int):
|
||||
current_cursor_position = self.__io.tell()
|
||||
cursor_position = current_cursor_position - size
|
||||
self.__io.seek(cursor_position)
|
||||
stdout = self.__io.read(size)
|
||||
response = TestRPCResponse.from_string(response_line=stdout)
|
||||
return response
|
||||
|
||||
def send(self, request: Union[dict, list], malformed: bool = False) -> TestRPCResponse:
|
||||
|
||||
# Assemble
|
||||
if malformed:
|
||||
# Allow a malformed request for testing and
|
||||
# bypass all this business below
|
||||
payload = json.dumps(request)
|
||||
|
||||
else:
|
||||
# Handle single or bulk requests
|
||||
requests = request
|
||||
if isinstance(request, dict):
|
||||
requests = [request]
|
||||
|
||||
payload = list()
|
||||
for r in requests:
|
||||
assembled_request = self.assemble_request(request=r)
|
||||
payload.append(assembled_request)
|
||||
|
||||
if not len(payload) > 1:
|
||||
payload = payload[0]
|
||||
|
||||
payload = json.dumps(payload)
|
||||
|
||||
# Request
|
||||
response_size = self._controller.handle_request(control_request=payload)
|
||||
|
||||
# Respond
|
||||
return self.receive(size=response_size)
|
Loading…
Reference in New Issue