Skip to content

Commit

Permalink
[explorer/nodewatch] task: fix lint issue
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLaw committed Mar 28, 2023
1 parent 283dd4c commit d655a86
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 52 deletions.
70 changes: 40 additions & 30 deletions explorer/nodewatch/nodewatch/NetworkRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,37 +162,31 @@ def load_node_descriptors(self, nodes_data_filepath):
# sort by name
self.node_descriptors.sort(key=lambda descriptor: descriptor.name)

def _create_descriptor_from_json(self, json_node):
# network crawler extracts as much extra data as possible, but it might not always be available for all nodes
extra_data = (0, 0, 0)
if 'extraData' in json_node:
json_extra_data = json_node['extraData']
extra_data = (json_extra_data.get('height', 0), json_extra_data.get('finalizedHeight', 0), json_extra_data.get('balance', 0))
def _handle_nem_node(self, json_node, extra_data):
network_identifier = json_node['metaData']['networkId']
if network_identifier < 0:
network_identifier += 0x100

if self.is_nem:
network_identifier = json_node['metaData']['networkId']
if network_identifier < 0:
network_identifier += 0x100

if self._network.identifier != network_identifier:
return None

node_protocol = json_node['endpoint']['protocol']
node_host = json_node['endpoint']['host']
node_port = json_node['endpoint']['port']

json_identity = json_node['identity']
main_public_key = PublicKey(json_identity['public-key'])
node_public_key = PublicKey(json_identity['node-public-key']) if 'node-public-key' in json_identity else None
return NodeDescriptor(
self._network.public_key_to_address(main_public_key),
main_public_key,
node_public_key,
f'{node_protocol}://{node_host}:{node_port}',
json_identity['name'],
json_node['metaData']['version'],
*extra_data)
if self._network.identifier != network_identifier:
return None

node_protocol = json_node['endpoint']['protocol']
node_host = json_node['endpoint']['host']
node_port = json_node['endpoint']['port']

json_identity = json_node['identity']
main_public_key = PublicKey(json_identity['public-key'])
node_public_key = PublicKey(json_identity['node-public-key']) if 'node-public-key' in json_identity else None
return NodeDescriptor(
self._network.public_key_to_address(main_public_key),
main_public_key,
node_public_key,
f'{node_protocol}://{node_host}:{node_port}',
json_identity['name'],
json_node['metaData']['version'],
*extra_data)

def _handle_symbol_node(self, json_node, extra_data):
symbol_endpoint = ''
roles = json_node['roles']
has_api = bool(roles & 2)
Expand All @@ -207,7 +201,11 @@ def _create_descriptor_from_json(self, json_node):
api_node_info_data = (None, None, None, None)
if 'apiNodeInfo' in json_node:
json_api_node_info_data = json_node['apiNodeInfo']
api_node_info_data = (json_api_node_info_data.get('isHealth', None), json_api_node_info_data.get('isHttpsEnable', None), json_api_node_info_data.get('isWssEnable', None), json_api_node_info_data.get('restVersion', None))
api_node_info_data = (
json_api_node_info_data.get('isHealth', None),
json_api_node_info_data.get('isHttpsEnable', None),
json_api_node_info_data.get('isWssEnable', None),
json_api_node_info_data.get('restVersion', None))

main_public_key = PublicKey(json_node['publicKey'])
node_public_key = PublicKey(json_node['nodePublicKey']) if 'nodePublicKey' in json_node else None
Expand All @@ -222,6 +220,18 @@ def _create_descriptor_from_json(self, json_node):
*api_node_info_data,
roles)

def _create_descriptor_from_json(self, json_node):
# network crawler extracts as much extra data as possible, but it might not always be available for all nodes
extra_data = (0, 0, 0)
if 'extraData' in json_node:
json_extra_data = json_node['extraData']
extra_data = (json_extra_data.get('height', 0), json_extra_data.get('finalizedHeight', 0), json_extra_data.get('balance', 0))

if self.is_nem:
return self._handle_nem_node(json_node, extra_data)

return self._handle_symbol_node(json_node, extra_data)

@staticmethod
def _format_symbol_version(version):
version_parts = [(version >> 24) & 0xFF, (version >> 16) & 0xFF, (version >> 8) & 0xFF, version & 0xFF]
Expand Down
9 changes: 7 additions & 2 deletions explorer/nodewatch/nodewatch/RoutesFacade.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import datetime
import random
from enum import Enum

from zenlog import log

Expand Down Expand Up @@ -67,9 +66,15 @@ def html_nodes(self):
'explorer_endpoint': self.explorer_endpoint
})

def json_nodes(self, role=None, exact_match=False, limit=None, ssl=None, order=None):
def json_nodes(self, **kwargs):
"""Returns all nodes with condition."""

role = kwargs.get('role', None)
exact_match = kwargs.get('exact_match', False)
limit = kwargs.get('limit', None)
ssl = kwargs.get('ssl', None)
order = kwargs.get('order', None)

def filter_custom(descriptor):
role_condition = True

Expand Down
13 changes: 7 additions & 6 deletions explorer/nodewatch/nodewatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@


class Field(Enum):
MAIN_PUBLIC_KEY = "mainPublicKey"
NODE_PUBLIC_KEY = "nodePublicKey"
MAIN_PUBLIC_KEY = 'mainPublicKey'
NODE_PUBLIC_KEY = 'nodePublicKey'


def str_to_bool(value):
if value.lower() == 'true':
return True
elif value.lower() == 'false':
if value.lower() == 'false':
return False
else:
return None
return None


def create_app():
# pylint: disable=too-many-locals
Expand Down Expand Up @@ -74,7 +75,7 @@ def nem_summary(): # pylint: disable=unused-variable

@app.route('/api/nem/nodes')
def api_nem_nodes(): # pylint: disable=unused-variable
return jsonify(nem_routes_facade.json_nodes(1))
return jsonify(nem_routes_facade.json_nodes(role=1))

@app.route('/api/nem/chart/height')
def api_nem_chart_height(): # pylint: disable=unused-variable
Expand Down
2 changes: 1 addition & 1 deletion explorer/nodewatch/tests/test_NetworkRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def test_can_load_symbol_node_descriptors(self):
is_https_enable=False,
is_wss_enable=False,
rest_version='2.4.2',
has_api=True) # simulates ssl disabled
has_api=True) # simulates ssl disabled

def test_can_format_node_descriptor_as_json(self):
# Arrange:
Expand Down
8 changes: 4 additions & 4 deletions explorer/nodewatch/tests/test_RoutesFacade.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_can_generate_nodes_json(self):
facade.reload_all(Path('tests/resources'), True)

# Act:
node_descriptors = facade.json_nodes(1)
node_descriptors = facade.json_nodes(role=1)

# Assert: spot check names and roles
self.assertEqual(4, len(node_descriptors))
Expand Down Expand Up @@ -361,7 +361,7 @@ def test_can_generate_nodes_json(self):
facade.reload_all(Path('tests/resources'), True)

# Act:
node_descriptors = facade.json_nodes(1)
node_descriptors = facade.json_nodes(role=1)

# Assert: spot check names and roles
self.assertEqual(5, len(node_descriptors))
Expand All @@ -378,7 +378,7 @@ def test_can_generate_nodes_json_filtered(self):
facade.reload_all(Path('tests/resources'), True)

# Act: select nodes with api role (role 2)
node_descriptors = facade.json_nodes(2)
node_descriptors = facade.json_nodes(role=2)

# Assert: spot check names and roles
self.assertEqual(5, len(node_descriptors))
Expand All @@ -395,7 +395,7 @@ def test_can_generate_nodes_json_filtered_exact_match(self):
facade.reload_all(Path('tests/resources'), True)

# Act: select nodes with only api role (role 2)
node_descriptors = facade.json_nodes(2, True)
node_descriptors = facade.json_nodes(role=2, exact_match=True)

# Assert: spot check names and roles
self.assertEqual(1, len(node_descriptors))
Expand Down
26 changes: 17 additions & 9 deletions explorer/nodewatch/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ def test_get_api_symbol_nodes_peer(client): # pylint: disable=redefined-outer-n
'Apple', 'Shin-Kuma-Node', 'ibone74', 'jaguar', 'symbol.ooo maxUnlockedAccounts:100', 'xym pool', 'yasmine farm'
] == list(map(lambda descriptor: descriptor['name'], response_json))

def test_get_api_symbol_nodes(client): # pylint: disable=redefined-outer-name

def test_get_api_symbol_nodes(client): # pylint: disable=redefined-outer-name
# Act:
response = client.get('/api/symbol/nodes')
response_json = json.loads(response.data)
Expand All @@ -202,7 +203,8 @@ def test_get_api_symbol_nodes(client): # pylint: disable=redefined-outer-name
'Allnodes250', 'Apple', 'Shin-Kuma-Node', 'ibone74', 'jaguar', 'symbol.ooo maxUnlockedAccounts:100', 'xym pool', 'yasmine farm'
] == list(map(lambda descriptor: descriptor['name'], response_json))

def test_get_api_symbol_nodes_order_random(client): # pylint: disable=redefined-outer-name

def test_get_api_symbol_nodes_order_random(client): # pylint: disable=redefined-outer-name
# Act:
response = client.get('/api/symbol/nodes?order=random')
response_json = json.loads(response.data)
Expand All @@ -212,12 +214,13 @@ def test_get_api_symbol_nodes_order_random(client): # pylint: disable=redefined-
assert 'application/json' == response.headers['Content-Type']
assert 8 == len(response_json)
expected_names = [
'Allnodes250', 'Apple', 'Shin-Kuma-Node', 'ibone74', 'jaguar', 'symbol.ooo maxUnlockedAccounts:100', 'xym pool', 'yasmine farm'
]
'Allnodes250', 'Apple', 'Shin-Kuma-Node', 'ibone74', 'jaguar', 'symbol.ooo maxUnlockedAccounts:100', 'xym pool', 'yasmine farm'
]
actual_names = list(map(lambda descriptor: descriptor['name'], response_json))
assert sorted(expected_names) == sorted(actual_names)

def test_get_api_symbol_nodes_limit_2(client): # pylint: disable=redefined-outer-name

def test_get_api_symbol_nodes_limit_2(client): # pylint: disable=redefined-outer-name
# Act:
response = client.get('/api/symbol/nodes?limit=2')
response_json = json.loads(response.data)
Expand All @@ -230,7 +233,8 @@ def test_get_api_symbol_nodes_limit_2(client): # pylint: disable=redefined-outer
'Allnodes250', 'Apple'
] == list(map(lambda descriptor: descriptor['name'], response_json))

def test_get_api_symbol_nodes_ssl_true(client): # pylint: disable=redefined-outer-name

def test_get_api_symbol_nodes_ssl_true(client): # pylint: disable=redefined-outer-name
# Act:
response = client.get('/api/symbol/nodes?ssl=true')
response_json = json.loads(response.data)
Expand All @@ -243,7 +247,8 @@ def test_get_api_symbol_nodes_ssl_true(client): # pylint: disable=redefined-oute
'xym pool'
] == list(map(lambda descriptor: descriptor['name'], response_json))

def test_get_api_symbol_nodes_ssl_false(client): # pylint: disable=redefined-outer-name

def test_get_api_symbol_nodes_ssl_false(client): # pylint: disable=redefined-outer-name
# Act:
response = client.get('/api/symbol/nodes?ssl=false')
response_json = json.loads(response.data)
Expand All @@ -256,7 +261,8 @@ def test_get_api_symbol_nodes_ssl_false(client): # pylint: disable=redefined-out
'yasmine farm'
] == list(map(lambda descriptor: descriptor['name'], response_json))

def test_get_api_symbol_node_with_main_public_key(client): # pylint: disable=redefined-outer-name

def test_get_api_symbol_node_with_main_public_key(client): # pylint: disable=redefined-outer-name
# Act:
response = client.get('/api/symbol/nodes/mainPublicKey/A0AA48B6417BDB1845EB55FB0B1E13255EA8BD0D8FA29AD2D8A906E220571F21')
response_json = json.loads(response.data)
Expand All @@ -266,7 +272,8 @@ def test_get_api_symbol_node_with_main_public_key(client): # pylint: disable=red
assert 'application/json' == response.headers['Content-Type']
assert 'Allnodes250' == response_json['name']

def test_get_api_symbol_node_with_node_public_key(client): # pylint: disable=redefined-outer-name

def test_get_api_symbol_node_with_node_public_key(client): # pylint: disable=redefined-outer-name
# Act:
response = client.get('/api/symbol/nodes/nodePublicKey/D05BE3101F2916AA34839DDC1199BE45092103A9B66172FA3D05911DC041AADA')
response_json = json.loads(response.data)
Expand All @@ -276,6 +283,7 @@ def test_get_api_symbol_node_with_node_public_key(client): # pylint: disable=red
assert 'application/json' == response.headers['Content-Type']
assert 'yasmine farm' == response_json['name']


def test_get_api_symbol_network_height(client): # pylint: disable=redefined-outer-name
# Act:
response = client.get('/api/symbol/height')
Expand Down

0 comments on commit d655a86

Please sign in to comment.