Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use constant for loopback address #2629

Merged
merged 2 commits into from Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/run_demo_ursula_fleet.py
Expand Up @@ -24,11 +24,12 @@

from nucypher.characters.lawful import Ursula
from nucypher.config.constants import APP_DIR, TEMPORARY_DOMAIN
from nucypher.utilities.networking import LOOPBACK_ADDRESS

FLEET_POPULATION = 12
DEMO_NODE_STARTING_PORT = 11500

ursula_maker = partial(Ursula, rest_host='127.0.0.1',
ursula_maker = partial(Ursula, rest_host=LOOPBACK_ADDRESS,
federated_only=True,
domain=TEMPORARY_DOMAIN)

Expand Down
1 change: 1 addition & 0 deletions newsfragments/2629.misc.rst
@@ -0,0 +1 @@
Use constant for loopback address across the codebase.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at the towncrier documentation (https://github.com/twisted/towncrier#philosophy). IMO this PR does not need a newsfragment. If we had a formal changelog I think it might be better suited there. In any case, I prefer to err on the side of verbosity.

4 changes: 3 additions & 1 deletion nucypher/cli/commands/felix.py
Expand Up @@ -56,6 +56,7 @@
from nucypher.cli.types import NETWORK_PORT
from nucypher.config.characters import FelixConfiguration
from nucypher.config.constants import DEFAULT_CONFIG_ROOT, NUCYPHER_ENVVAR_WORKER_ETH_PASSWORD
from nucypher.utilities.networking import LOOPBACK_ADDRESS

option_port = click.option('--port', help="The host port to run Felix HTTP services on", type=NETWORK_PORT, default=FelixConfiguration.DEFAULT_REST_PORT)

Expand Down Expand Up @@ -128,7 +129,8 @@ def generate_config(self, config_root, discovery_port):
network=option_network(),
provider_uri=option_provider_uri(),
signer_uri=option_signer_uri,
host=click.option('--host', help="The host to run Felix HTTP services on", type=click.STRING, default='127.0.0.1'),
host=click.option('--host', help="The host to run Felix HTTP services on", type=click.STRING,
default=LOOPBACK_ADDRESS),
db_filepath=option_db_filepath,
checksum_address=option_checksum_address,
registry_filepath=option_registry_filepath,
Expand Down
5 changes: 3 additions & 2 deletions nucypher/config/characters.py
Expand Up @@ -32,6 +32,7 @@
NUCYPHER_ENVVAR_BOB_ETH_PASSWORD
)
from nucypher.config.keyring import NucypherKeyring
from nucypher.utilities.networking import LOOPBACK_ADDRESS


class UrsulaConfiguration(CharacterConfiguration):
Expand All @@ -41,7 +42,7 @@ class UrsulaConfiguration(CharacterConfiguration):
NAME = CHARACTER_CLASS.__name__.lower()

DEFAULT_REST_PORT = 9151
DEFAULT_DEVELOPMENT_REST_HOST = '127.0.0.1'
DEFAULT_DEVELOPMENT_REST_HOST = LOOPBACK_ADDRESS
DEFAULT_DEVELOPMENT_REST_PORT = 10151
DEFAULT_DB_NAME = f'{NAME}.db'
DEFAULT_AVAILABILITY_CHECKS = False
Expand Down Expand Up @@ -255,7 +256,7 @@ class FelixConfiguration(CharacterConfiguration):
DEFAULT_DB_FILEPATH = os.path.join(DEFAULT_CONFIG_ROOT, DEFAULT_DB_NAME)
DEFAULT_REST_PORT = 6151
DEFAULT_LEARNER_PORT = 9151
DEFAULT_REST_HOST = '127.0.0.1'
DEFAULT_REST_HOST = LOOPBACK_ADDRESS
__DEFAULT_TLS_CURVE = ec.SECP384R1


Expand Down
4 changes: 3 additions & 1 deletion nucypher/network/protocols.py
Expand Up @@ -18,6 +18,8 @@
from eth_utils import is_checksum_address
from urllib.parse import urlparse

from nucypher.utilities.networking import LOOPBACK_ADDRESS


class SuspiciousActivity(RuntimeError):
"""raised when an action appears to amount to malicious conduct."""
Expand Down Expand Up @@ -63,7 +65,7 @@ class InterfaceInfo:
expected_bytes_length = lambda: VariableLengthBytestring

def __init__(self, host, port) -> None:
loopback, localhost = '127.0.0.1', 'localhost'
loopback, localhost = LOOPBACK_ADDRESS, 'localhost'
self.host = loopback if host == localhost else host
self.port = int(port)

Expand Down
2 changes: 1 addition & 1 deletion nucypher/utilities/networking.py
Expand Up @@ -40,7 +40,7 @@ class InvalidWorkerIP(RuntimeError):

CENTRALIZED_IP_ORACLE_URL = 'https://ifconfig.me/'

LOOPBACK_ADDRESS = '127.0.0.1' # TODO use across code base - #2538
LOOPBACK_ADDRESS = '127.0.0.1'

RequestErrors = (
# https://requests.readthedocs.io/en/latest/user/quickstart/#errors-and-exceptions
Expand Down
3 changes: 2 additions & 1 deletion scripts/local_fleet/run_single_ursula.py
Expand Up @@ -26,6 +26,7 @@

from nucypher.cli.main import nucypher_cli
from nucypher.exceptions import DevelopmentInstallationRequired
from nucypher.utilities.networking import LOOPBACK_ADDRESS

try:
from tests.utils.ursula import select_test_port
Expand All @@ -40,7 +41,7 @@
args = ['ursula', 'run',
'--debug',
'--federated-only',
'--teacher', f'https://127.0.0.1:{DEMO_FLEET_STARTING_PORT}',
'--teacher', f'https://{LOOPBACK_ADDRESS}:{DEMO_FLEET_STARTING_PORT}',
'--rest-port', DEMO_NODE_PORT,
'--dev'
]
Expand Down
6 changes: 3 additions & 3 deletions tests/acceptance/cli/ursula/test_run_ursula.py
Expand Up @@ -30,7 +30,7 @@
from nucypher.config.characters import UrsulaConfiguration
from nucypher.config.constants import NUCYPHER_ENVVAR_KEYRING_PASSWORD, TEMPORARY_DOMAIN
from nucypher.network.nodes import Teacher
from nucypher.utilities.networking import UnknownIPAddress
from nucypher.utilities.networking import LOOPBACK_ADDRESS, UnknownIPAddress
from tests.constants import (
FAKE_PASSWORD_CONFIRMED,
INSECURE_DEVELOPMENT_PASSWORD,
Expand Down Expand Up @@ -114,7 +114,7 @@ def test_run_lone_federated_default_development_ursula(click_runner):
time.sleep(Learner._SHORT_LEARNING_DELAY)
assert result.exit_code == 0, result.output
assert "Running" in result.output
assert "127.0.0.1:{}".format(deploy_port) in result.output
assert f"{LOOPBACK_ADDRESS}:{deploy_port}" in result.output

reserved_ports = (UrsulaConfiguration.DEFAULT_REST_PORT, UrsulaConfiguration.DEFAULT_DEVELOPMENT_REST_PORT)
assert deploy_port not in reserved_ports
Expand Down Expand Up @@ -153,7 +153,7 @@ def run_ursula():

assert result.exit_code == 0
assert "Starting services" in result.output
assert f"127.0.0.1:{deploy_port}" in result.output
assert f"{LOOPBACK_ADDRESS}:{deploy_port}" in result.output

reserved_ports = (UrsulaConfiguration.DEFAULT_REST_PORT, UrsulaConfiguration.DEFAULT_DEVELOPMENT_REST_PORT)
assert deploy_port not in reserved_ports
Expand Down
3 changes: 2 additions & 1 deletion tests/acceptance/cli/ursula/test_stakeholder_and_ursula.py
Expand Up @@ -37,6 +37,7 @@
from nucypher.config.characters import StakeHolderConfiguration, UrsulaConfiguration
from nucypher.config.constants import TEMPORARY_DOMAIN
from nucypher.utilities.logging import Logger
from nucypher.utilities.networking import LOOPBACK_ADDRESS
from tests.constants import (
FAKE_PASSWORD_CONFIRMED,
FEE_RATE_RANGE,
Expand Down Expand Up @@ -583,7 +584,7 @@ def test_collect_rewards_integration(click_runner,
signer=Web3Signer(testerchain.client),
worker_address=worker_address,
registry=agency_local_registry,
rest_host='127.0.0.1',
rest_host=LOOPBACK_ADDRESS,
rest_port=ursula_port,
provider_uri=TEST_PROVIDER_URI,
network_middleware=MockRestMiddleware(),
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_web3_clients.py
Expand Up @@ -24,6 +24,7 @@
from nucypher.blockchain.eth.clients import (GanacheClient, GethClient, InfuraClient, PUBLIC_CHAINS,
ParityClient, AlchemyClient)
from nucypher.blockchain.eth.interfaces import BlockchainInterface
from nucypher.utilities.networking import LOOPBACK_ADDRESS

DEFAULT_GAS_PRICE = 42
GAS_PRICE_FROM_STRATEGY = 1234
Expand Down Expand Up @@ -59,7 +60,7 @@ class MockAlchemyProvider:


class MockWebSocketProvider:
endpoint_uri = 'ws://127.0.0.1:8546'
endpoint_uri = f'ws://{LOOPBACK_ADDRESS}:8546'
clientVersion = 'Geth/v1.8.23-omnibus-2ad89aaa/linux-amd64/go1.11.1'


Expand Down Expand Up @@ -260,7 +261,7 @@ def test_detect_provider_type_https():


def test_detect_provider_type_ws():
interface = ProviderTypeTestClient(provider_uri='ws://127.0.0.1:8546',
interface = ProviderTypeTestClient(provider_uri=f'ws://{LOOPBACK_ADDRESS}:8546',
expected_provider_class=WebsocketProvider,
actual_provider_to_attach=MockWebSocketProvider())
interface.connect()
Expand Down