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

Fix bugs on aleph node cmds + sanitize_url + missing tests #329

Merged
merged 2 commits into from
Jan 23, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/aleph_client/commands/instance/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def fetch_crn_info(node_url: str) -> dict | None:
"""
url = ""
try:
base_url: str = sanitize_url(node_url.rstrip("/"))
base_url: str = sanitize_url(node_url)
timeout = aiohttp.ClientTimeout(total=settings.HTTP_REQUEST_TIMEOUT)
async with aiohttp.ClientSession(timeout=timeout) as session:
info: dict
Expand Down
37 changes: 25 additions & 12 deletions src/aleph_client/commands/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,26 @@ def _filter_node(
logger.debug(e)
for node in core_info:
try:
sanitized_url = node["address"] or sanitize_url(node["address"])
if (
(not active or (node["status"] == "linked" and node["score"] > 0))
and (not address or node["owner"] == address)
and (not payg_receiver or node["stream_reward"] == payg_receiver)
and (not crn_url or (sanitized_url == node_url))
and (not crn_hash or node["hash"] == crn_hash)
and (not ccn_hash or node["parent"] == ccn_hash)
):
node["address"] = sanitized_url
result.append(node)
if "total_staked" in node: # CCN
if (
(not active or (node["status"] == "active" and node["score"] > 0))
and (not address or node["owner"] == address)
and (not ccn_hash or node["hash"] == ccn_hash)
):
result.append(node)
elif "parent" in node: # CRN
sanitized_url = "address" in node and sanitize_url(node["address"])
if sanitized_url:
node["address"] = sanitized_url
if (
(not active or (node["status"] == "linked" and node["score"] > 0))
and (not address or node["owner"] == address)
and (not payg_receiver or node["stream_reward"] == payg_receiver)
and (not crn_url or node["address"] == node_url)
and (not crn_hash or node["hash"] == crn_hash)
and (not ccn_hash or node["parent"] == ccn_hash)
):
result.append(node)
except Exception as e:
logger.debug(e)
return result
Expand All @@ -148,12 +157,14 @@ def _show_core(node_info):
table.add_column("Linked", style="#029AFF", justify="left")
table.add_column("Creation Time", style="#029AFF", justify="center")
table.add_column("Status", style="green", justify="right")
table.add_column("Item Hash", style="green", justify="center")

for node in node_info:
# Prevent escaping with name
node_name = node["name"]
node_name = _escape_and_normalize(node_name)
node_name = _remove_ansi_escape(node_name)
node_hash = node["hash"]

# Format Value
creation_time = datetime.datetime.fromtimestamp(node["time"]).strftime("%Y-%m-%d %H:%M:%S")
Expand All @@ -167,6 +178,7 @@ def _show_core(node_info):
str(len(node["resource_nodes"])),
creation_time,
status,
node_hash,
)

console = Console()
Expand Down Expand Up @@ -212,13 +224,14 @@ async def core(
json: bool = typer.Option(default=False, help="Print as json instead of rich table"),
active: bool = typer.Option(default=False, help="Only show active nodes"),
address: Optional[str] = typer.Option(default=None, help="Owner address to filter by"),
ccn_hash: Optional[str] = typer.Option(default=None, help="CCN hash to filter by"),
debug: bool = False,
):
"""Get all core node (CCN) on aleph"""
setup_logging(debug)

core_info: NodeInfo = await _fetch_nodes()
core_info.core_node = _filter_node(core_info=core_info.core_node, active=active, address=address)
core_info.core_node = _filter_node(core_info=core_info.core_node, active=active, address=address, ccn_hash=ccn_hash)

if not json:
_show_core(node_info=core_info.core_node)
Expand Down
2 changes: 1 addition & 1 deletion src/aleph_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,4 @@ def sanitize_url(url: str) -> str:
f"({', '.join(FORBIDDEN_HOSTS)})"
)
raise aiohttp.InvalidURL("Invalid URL host")
return url
return url.strip("/")
4 changes: 2 additions & 2 deletions tests/unit/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
FAKE_VM_HASH = "ab12" * 16
FAKE_PROGRAM_HASH = "cd34" * 16
FAKE_PROGRAM_HASH_2 = "ef56" * 16
FAKE_CRN_HASH = "2cdb78cf561c6f0f839edb817395d3b5ece20d89125c5afba658f9170d6932c8"
FAKE_CRN_URL = "https://dchq.staging.aleph.sh"
FAKE_CRN_HASH = "cb764fe80f76cd5ec395952263fcbf0f5d2cc0dfe1ed98c90e13734b3fb2df3e"
FAKE_CRN_URL = "https://coco-1.crn.aleph.sh"
FAKE_FLOW_HASH = "0xfake_flow_hash"


Expand Down
34 changes: 34 additions & 0 deletions tests/unit/test_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

import pytest

from aleph_client.commands.node import compute, core


@pytest.mark.asyncio
async def test_node_core(capsys):
await core(
json=False,
active=True,
address=None,
ccn_hash=None,
debug=False,
)
captured = capsys.readouterr()
assert "Core Channel Node Information" in captured.out


@pytest.mark.asyncio
async def test_node_compute(capsys):
await compute(
json=False,
active=True,
address=None,
payg_receiver=None,
crn_url=None,
crn_hash=None,
ccn_hash=None,
debug=False,
)
captured = capsys.readouterr()
assert "Compute Node Information" in captured.out
Loading