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

Fix network handling #303

Merged
merged 15 commits into from
Dec 16, 2022
Merged
17 changes: 11 additions & 6 deletions src/nile/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ def get_gateways():
"""Get the StarkNet node details."""
try:
with open(NODE_FILENAME, "r") as f:
gateway = json.load(f)
return gateway
gateways = json.load(f)
return gateways

except FileNotFoundError:
with open(NODE_FILENAME, "w") as f:
gateways = {"localhost": "http://127.0.0.1:5050/", **DEFAULT_GATEWAYS}
f.write(json.dumps(gateways, indent=2))
gateways = create_node_json()
return gateways

return gateways

def create_node_json(network="localhost", gateway_url="http://127.0.0.1:5050/"):
"""Create node.json and return gateways."""
with open(NODE_FILENAME, "w") as f:
gateways = {network: gateway_url, **DEFAULT_GATEWAYS}
f.write(json.dumps(gateways, indent=2))
return gateways


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

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


def node(host="127.0.0.1", port=5050, seed=None, lite_mode=False):
ericnordelo marked this conversation as resolved.
Show resolved Hide resolved
"""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}/"
gateways = {network: gateway_url, **DEFAULT_GATEWAYS}

with open(file, "w+") as f:
json.dump(gateways, f, indent=2)
create_node_json(network, gateway_url)

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

Expand Down
25 changes: 24 additions & 1 deletion tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""Tests for common library."""
import json
from unittest.mock import mock_open, patch

import pytest

from nile.common import (
DEFAULT_GATEWAYS,
NODE_FILENAME,
create_node_json,
get_gateways,
parse_information,
prepare_params,
Expand All @@ -16,6 +21,7 @@
LIST3 = [1, 2, 3, [4, 5, 6, [7, 8, 9]]]
STDOUT_1 = "SDTOUT_1"
STDOUT_2 = "SDTOUT_2"
LOCAL_GATEWAY = {"localhost": "http://127.0.0.1:5050/"}


@pytest.mark.parametrize(
Expand Down Expand Up @@ -58,6 +64,23 @@ def test_parse_information():

def test_get_gateways():
gateways = get_gateways()
expected = {"localhost": "http://127.0.0.1:5050/", **DEFAULT_GATEWAYS}
expected = {**LOCAL_GATEWAY, **DEFAULT_GATEWAYS}

assert gateways == expected


@pytest.mark.parametrize(
"args, gateway", [(None, LOCAL_GATEWAY), (["a", "b"], {"a": "b"})]
)
def test_create_node_json(args, gateway):
open_mock = mock_open()
with patch("nile.common.open", open_mock, create=True):
if args is None:
create_node_json()
else:
create_node_json(*args)

expected = json.dumps({**gateway, **DEFAULT_GATEWAYS}, indent=2)

open_mock.assert_called_with(NODE_FILENAME, "w")
open_mock.return_value.write.assert_called_once_with(expected)