Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
Fix network handling (#303)
Browse files Browse the repository at this point in the history
* add other networks to node.json by default

* clean up test

* Apply suggestions from code review

Co-authored-by: Eric Nordelo <[email protected]>

* centralize file writes

* remove writing DEFAULT_NETWORKS to node.json

* fix formatting

* compare gateway to default

* fix get_gateways, refactor create_node_json

* fix node timer and test

* fix node_json and get_gateways tests

* fix test

* Update src/nile/common.py

Co-authored-by: Martín Triay <[email protected]>

* improve write_node_json

* change gateways variable to custom_gateways

Co-authored-by: Eric Nordelo <[email protected]>
Co-authored-by: Martín Triay <[email protected]>
  • Loading branch information
3 people authored Dec 16, 2022
1 parent 77ba209 commit 8ca995a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 27 deletions.
38 changes: 24 additions & 14 deletions src/nile/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,35 @@
# subject to change
"0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"
)
DEFAULT_GATEWAYS = {
"localhost": "http://127.0.0.1:5050/",
"goerli2": "https://alpha4-2.starknet.io",
"integration": "https://external.integration.starknet.io",
}


def get_gateways():
"""Get the StarkNet node details."""
try:
if os.path.exists(NODE_FILENAME):
with open(NODE_FILENAME, "r") as f:
gateway = json.load(f)
return gateway

except FileNotFoundError:
with open(NODE_FILENAME, "w") as f:
networks = {
"localhost": "http://127.0.0.1:5050/",
"goerli2": "https://alpha4-2.starknet.io",
"integration": "https://external.integration.starknet.io",
}
f.write(json.dumps(networks, indent=2))

return networks
custom_gateways = json.load(f)
gateways = {**DEFAULT_GATEWAYS, **custom_gateways}
return gateways
else:
return DEFAULT_GATEWAYS


def write_node_json(network, gateway_url):
"""Create or update node.json with custom network."""
if not os.path.exists(NODE_FILENAME):
with open(NODE_FILENAME, "w") as fp:
json.dump({network: gateway_url}, fp)
else:
with open(NODE_FILENAME, "r+") as fp:
gateways = json.load(fp)
gateways[network] = gateway_url
fp.seek(0)
json.dump(gateways, fp, indent=2)


GATEWAYS = get_gateways()
Expand Down
10 changes: 3 additions & 7 deletions src/nile/core/node.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
"""Command to start StarkNet local network."""
import json
import logging
import subprocess

from nile.common import NODE_FILENAME
from nile.common import DEFAULT_GATEWAYS, write_node_json


def node(host="127.0.0.1", port=5050, seed=None, lite_mode=False):
"""Start StarkNet local network."""
try:
# Save host and port information to be used by other commands
file = NODE_FILENAME
if host == "127.0.0.1":
network = "localhost"
else:
network = host
gateway_url = f"http://{host}:{port}/"
gateway = {network: gateway_url}

with open(file, "w+") as f:
json.dump(gateway, f)
if DEFAULT_GATEWAYS.get(network) != gateway_url:
write_node_json(network, gateway_url)

command = ["starknet-devnet", "--host", host, "--port", str(port)]

Expand Down
11 changes: 6 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ async def test_node_forwards_args(mock_subprocess):
)
async def test_node_runs_gateway(opts, expected):
# Node life
seconds = 15
seconds = 20

host = opts.get("--host", "127.0.0.1")
port = opts.get("--port", "5050")
Expand Down Expand Up @@ -177,10 +177,11 @@ async def test_node_runs_gateway(opts, expected):
assert status == 200

# Assert network and gateway_url is correct in node.json file
file = NODE_FILENAME
with open(file, "r") as f:
gateway = json.load(f)
assert gateway.get(network) == expected
if expected != "http://127.0.0.1:5050/":
file = NODE_FILENAME
with open(file, "r") as f:
gateway = json.load(f)
assert gateway.get(network) == expected


@pytest.mark.asyncio
Expand Down
61 changes: 60 additions & 1 deletion tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
"""Tests for common library."""
import json

import pytest

from nile.common import parse_information, prepare_params, stringify
from nile.common import (
DEFAULT_GATEWAYS,
NODE_FILENAME,
write_node_json,
get_gateways,
parse_information,
prepare_params,
stringify,
)

NETWORK = "goerli"
ARGS = ["1", "2", "3"]
Expand All @@ -12,6 +22,12 @@
STDOUT_2 = "SDTOUT_2"


@pytest.fixture(autouse=True)
def tmp_working_dir(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)
return tmp_path


@pytest.mark.parametrize(
"args, expected",
[
Expand Down Expand Up @@ -48,3 +64,46 @@ def test_parse_information():

_a, _b = parse_information(target)
assert _a, _b == (a, b)


@pytest.mark.parametrize(
"network, url, gateway",
[
(None, None, {}),
("localhost", "5051", {"localhost": "5051"}),
("host", "port", {"host": "port"}),
],
)
def test_get_gateways(network, url, gateway):
if network is not None:
write_node_json(network, url)

gateways = get_gateways()
expected = {**DEFAULT_GATEWAYS, **gateway}
assert gateways == expected

# Check that node.json gateway returns in the case of duplicate keys
if network == "localhost":
assert expected["localhost"] != "5050"
assert expected["localhost"] == "5051"


@pytest.mark.parametrize(
"args1, args2, gateways",
[
(
["NETWORK1", "URL1"],
["NETWORK2", "URL2"],
{"NETWORK1": "URL1", "NETWORK2": "URL2"},
),
],
)
def test_write_node_json(args1, args2, gateways):
# Check that node.json is created and adds keys
write_node_json(*args1)
write_node_json(*args2)

with open(NODE_FILENAME, "r") as fp:
result = fp.read()
expected = json.dumps(gateways, indent=2)
assert result == expected

0 comments on commit 8ca995a

Please sign in to comment.