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

Add lite-mode #153

Merged
merged 17 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,34 @@ This command creates the project directory structure and installs `cairo-lang`,

Run a local [`starknet-devnet`](https://github.com/Shard-Labs/starknet-devnet/) node:

```sh
```text
nile node [--host HOST] [--port PORT] [--lite_mode]

optional arguments:
--host HOST Specify the address to listen at; defaults to
127.0.0.1 (use the address the program outputs on
start)
--port PORT Specify the port to listen at; defaults to 5050
--lite-mode Applies all lite-mode optimizations by disabling
features such as block hash calculation and deploy
hash
andrew-fleming marked this conversation as resolved.
Show resolved Hide resolved
```

```text
nile node

* Serving Flask app 'starknet_devnet.server' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5050/ (Press CTRL+C to quit)
Account #0
Address: 0x877b050406a54adb5940227e51265a201e467e520ca85dc7f024abd03dcc61
Public key: 0x256b8dc218586160ef80d3454a7cd51046271fbf091bd6779e3513304f22156
Private key: 0xb204ff062d85674b467789f07826bb2

...

Initial balance of each account: 1000000000000000000000 WEI
Seed to replicate this account sequence: 2128506880
WARNING: Use these accounts and their keys ONLY for local testing. DO NOT use them on mainnet or other live networks because you will LOSE FUNDS.

* Listening on http://127.0.0.1:5050/ (Press CTRL+C to quit)
```

### `compile`
Expand Down
8 changes: 6 additions & 2 deletions src/nile/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,20 @@ def clean():
@cli.command()
@click.option("--host", default="127.0.0.1")
@click.option("--port", default=5050)
def node(host, port):
@click.option("--lite_mode", is_flag=True)
def node(host, port, lite_mode):
"""Start StarkNet local network.

$ nile node
Start StarkNet local network at port 5050

$ nile node --host HOST --port 5001
Start StarkNet network on address HOST listening at port 5001

$ nile node --lite_mode
Start StarkNet network on lite-mode
"""
node_command(host, port)
node_command(host, port, lite_mode)


@cli.command()
Expand Down
18 changes: 12 additions & 6 deletions src/nile/core/node.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Command to start StarkNet local network."""
import json
import logging
import subprocess

from nile.common import NODE_FILENAME


def node(host="127.0.0.1", port=5050):
def node(host="127.0.0.1", port=5050, lite_mode=False):
"""Start StarkNet local network."""
try:
# Save host and port information to be used by other commands
Expand All @@ -20,11 +21,16 @@ def node(host="127.0.0.1", port=5050):
with open(file, "w+") as f:
json.dump(gateway, f)

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

if lite_mode:
command.append("--lite-mode")

# Start network
subprocess.check_call(["starknet-devnet", "--host", host, "--port", str(port)])
subprocess.check_call(command)

except FileNotFoundError:
print("")
print("😰 Could not find starknet-devnet, is it installed? Try with:\n")
print(" pip install starknet-devnet")
print("")
logging.error(
"\n\n😰 Could not find starknet-devnet, is it installed? Try with:\n"
" pip install starknet-devnet"
)
48 changes: 48 additions & 0 deletions tests/commands/test_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Tests for node command."""
import logging
from unittest.mock import patch

import pytest

from nile.core.node import node

HOSTS = ["127.0.0.1", "goerli"]
PORTS = ["5050", "5001"]
LITE = "--lite-mode"


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


@pytest.mark.parametrize(
"args, host, port, mode",
[
([], HOSTS[0], PORTS[0], None),
([HOSTS[1], PORTS[1]], HOSTS[1], PORTS[1], None),
([HOSTS[1], PORTS[1], LITE], HOSTS[1], PORTS[1], LITE),
Copy link
Contributor

Choose a reason for hiding this comment

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

i’m not sure why these two lines use HOSTS[1], PORTS[1], and LITE as args. i guess they’re just placeholder values but if that's the case then it causes a bit of confusion. or do they serve a different purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's to make sure that when these args are explicitly passed, it doesn't cause any issues with the command (like it did with --account_contract and disable_hint_validation in #150). I'll refactor to make it less confusing

],
)
@patch("nile.core.node.subprocess.check_call")
def test_node_call(mock_subprocess, args, host, port, mode):
node(*args)

command = ["starknet-devnet", "--host", host, "--port", port]
if mode:
command.append(mode)
mock_subprocess.assert_called_once_with(command)
Copy link
Contributor

Choose a reason for hiding this comment

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

aren't we testing args?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! I think the weirdness of this test comes from checking the defaults (which differ from args except when explicitly set)



@patch("nile.core.node.subprocess.check_call")
def test_node_error(mock_subprocess, caplog):
logging.getLogger().setLevel(logging.INFO)

mock_subprocess.side_effect = FileNotFoundError

node()
assert (
"\n\n😰 Could not find starknet-devnet, is it installed? Try with:\n"
" pip install starknet-devnet"
) in caplog.text