From 03e11aa0feb610f7a9c4739f26a38c18c3dfdf43 Mon Sep 17 00:00:00 2001 From: gr0vity-dev Date: Tue, 16 Apr 2024 13:34:41 +0200 Subject: [PATCH] add nanorpc as library, include requests dependency --- .gitignore | 3 + nanolab/publisher/block_asserts.py | 12 +- nanolab/publisher/block_generator.py | 5 +- nanolab/publisher/confirmation_stats.py | 10 +- nanolab/publisher/test_case.py | 1 - nanolab/src/nano_rpc.py | 506 ------------------------ requirements.txt | 1 + setup.py | 4 +- unit_tests/test_block_asserts.py | 2 +- 9 files changed, 21 insertions(+), 523 deletions(-) delete mode 100644 nanolab/src/nano_rpc.py diff --git a/.gitignore b/.gitignore index 1d1b098..2774bf1 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ venv_py/ **/._.DS_Store debug unit_tests/testcases +snippets/ +testcases/ +nl_config.toml \ No newline at end of file diff --git a/nanolab/publisher/block_asserts.py b/nanolab/publisher/block_asserts.py index c07bd19..7b1ba46 100644 --- a/nanolab/publisher/block_asserts.py +++ b/nanolab/publisher/block_asserts.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from nanolab.src.nano_rpc import NanoRpcV2 +from nanorpc.client import NanoRpcTyped from nanolab.publisher.block_event import BlockConfirmationEvent from nanolab.src.utils import get_config_parser import asyncio @@ -44,8 +44,9 @@ def __init__(self, event_bus=None, node_name=None): self.event_bus = event_bus conf_p = get_config_parser() node_name = node_name if node_name else conf_p.get_nodes_name()[:-1] - self.nano_rpc_default = NanoRpcV2(conf_p.get_node_rpc(node_name)) - self.nano_rpc_all = [NanoRpcV2(url) for url in conf_p.get_nodes_rpc()] + self.nano_rpc_default = NanoRpcTyped(conf_p.get_node_rpc(node_name)) + self.nano_rpc_all = [NanoRpcTyped(url) + for url in conf_p.get_nodes_rpc()] async def _fetch_block_info(self, block_hash): return await self.nano_rpc_default.block_info(block_hash, @@ -101,7 +102,8 @@ async def assert_blocks_confirmed(self, block_hashes): async def _publish_block_confirmation_event(self, wait_s, block_hash, timeout): - if not self.event_bus: return + if not self.event_bus: + return event = BlockConfirmationEvent(block_hash, timeout, wait_s) await self.event_bus.publish('block_confirmed', event) @@ -135,4 +137,4 @@ async def assert_single_block_confirmed(self, block_hash: str): async def assert_single_block_confirmed_wait(self, block_hash, wait_s, interval): - await self.assert_blocks_confirmed_wait([block_hash], wait_s, interval) \ No newline at end of file + await self.assert_blocks_confirmed_wait([block_hash], wait_s, interval) diff --git a/nanolab/publisher/block_generator.py b/nanolab/publisher/block_generator.py index 31ba650..5297f05 100644 --- a/nanolab/publisher/block_generator.py +++ b/nanolab/publisher/block_generator.py @@ -1,4 +1,3 @@ -#from nanolab.src.nano_rpc_v2 import NanoRpcV2 from nanomock.modules.nl_rpc import NanoRpc, NanoLibTools from nanolab.src.utils import get_config_parser from abc import ABC, abstractmethod @@ -67,7 +66,7 @@ def set_broadcast_blocks(self, broadcast): def create_send_and_open_block(self, send_amount_raw, source_seed, source_index, destination_seed, destination_index, representative): - #destination = self.nano_rpc_default.generate_account(destination_seed, destination_index) + # destination = self.nano_rpc_default.generate_account(destination_seed, destination_index) return self.blockgen_single_account_opener( representative=representative, source_seed=source_seed, @@ -121,4 +120,4 @@ def blockgen_single_account_opener( open_block["account_data"]["source_index"] = destination_index res = [send_block, open_block] - return res \ No newline at end of file + return res diff --git a/nanolab/publisher/confirmation_stats.py b/nanolab/publisher/confirmation_stats.py index 8650c6c..adfe0aa 100644 --- a/nanolab/publisher/confirmation_stats.py +++ b/nanolab/publisher/confirmation_stats.py @@ -1,4 +1,4 @@ -from nanolab.src.nano_rpc import NanoRpcV2 +from nanorpc.client import NanoRpcTyped from nanolab.src.utils import get_config_parser from time import strftime, gmtime, time from math import ceil @@ -12,16 +12,16 @@ def __init__(self, block_timeout_s): self.formatter = ConfirmationTableFormatter() async def initialize(self, node_name): - self.rpc_v2 = NanoRpcV2(get_config_parser().get_node_rpc(node_name)) - self.rpc_v2.create_session() - start_block_count = await self.rpc_v2.block_count() + self.nanorpc = NanoRpcTyped( + get_config_parser().get_node_rpc(node_name)) + start_block_count = await self.nanorpc.block_count() self.calculator.set_start_block_count(start_block_count) def set_end_block_count(self, end_block_count: dict) -> None: self.calculator.set_end_block_count(end_block_count) async def print_stats(self) -> None: - end_block_count = await self.rpc_v2.block_count() + end_block_count = await self.nanorpc.block_count() self.calculator.set_end_block_count(end_block_count) stats = self.calculator.compute_stats() self.printer.print_stats(stats, self.formatter) diff --git a/nanolab/publisher/test_case.py b/nanolab/publisher/test_case.py index be1ab5a..deb95b4 100644 --- a/nanolab/publisher/test_case.py +++ b/nanolab/publisher/test_case.py @@ -1,5 +1,4 @@ from abc import ABC, abstractmethod -from nanolab.src.nano_rpc import NanoRpcV2 from nanolab.publisher.block_asserts import BlockAsserts from nanolab.publisher.block_generator import BlockGenerator from nanolab.publisher.confirmation_stats import ConfirmationStatsManager diff --git a/nanolab/src/nano_rpc.py b/nanolab/src/nano_rpc.py deleted file mode 100644 index 78ba1cd..0000000 --- a/nanolab/src/nano_rpc.py +++ /dev/null @@ -1,506 +0,0 @@ -import aiohttp -import asyncio - -COMMANDS = { - "account_balance": { - "required": ["account"], - "optional": [] - }, - "account_info": { - "required": ["account"], - "optional": - ["representative", "weight", "receivable", "include_confirmed"] - }, - "account_get": { - "required": ["key"], - "optional": [] - }, - "account_history": { - "required": ["account", "count"], - "optional": ["raw", "head", "offset", "reverse", "account_filter"] - }, - "account_key": { - "required": ["account"], - "optional": [] - }, - "account_representative": { - "required": ["account"], - "optional": [] - }, - "account_weight": { - "required": ["account"], - "optional": [] - }, - "accounts_balances": { - "required": ["accounts"], - "optional": [] - }, - "accounts_frontiers": { - "required": ["accounts"], - "optional": [] - }, - "accounts_receivable": { - "required": ["accounts", "count"], - "optional": [ - "threshold", "source", "include_active", "sorting", - "include_only_confirmed" - ] - }, - "accounts_representatives": { - "required": ["accounts"], - "optional": [] - }, - "available_supply": { - "required": [], - "optional": [] - }, - "block_account": { - "required": ["hash"], - "optional": [] - }, - "block_confirm": { - "required": ["hash"], - "optional": [] - }, - "block_count": { - "required": [], - "optional": ["include_cemented"] - }, - "block_create": { - "required": - ["type", "balance", "key", "representative", "link", "previous"], - "optional": ["work", "version", "difficulty", "json_block"] - }, - "block_hash": { - "required": ["block"], - "optional": ["json_block"] - }, - "block_info": { - "required": ["hash"], - "optional": ["json_block"] - }, - "blocks": { - "required": ["hashes"], - "optional": ["json_block"] - }, - "blocks_info": { - "required": ["hashes"], - "optional": [ - "json_block", "receivable", "source", "pending", "receive_hash", - "include_not_found" - ] - }, - "bootstrap": { - "required": ["address", "port"], - "optional": ["bypass_frontier_confirmation", "id"] - }, - "bootstrap_any": { - "required": [], - "optional": ["force", "id", "account"] - }, - "bootstrap_lazy": { - "required": ["hash"], - "optional": ["force", "id"] - }, - "bootstrap_status": { - "required": [], - "optional": [] - }, - "chain": { - "required": ["block", "count"], - "optional": ["offset", "reverse"] - }, - "confirmation_active": { - "required": [], - "optional": ["announcements"] - }, - "confirmation_height_currently_processing": { - "required": [], - "optional": [] - }, - "confirmation_history": { - "required": [], - "optional": ["hash"] - }, - "confirmation_info": { - "required": ["json_block", "root"], - "optional": ["contents", "json_block", "representatives"] - }, - "confirmation_quorum": { - "required": [], - "optional": ["peer_details"] - }, - "database_txn_tracker": { - "required": ["min_read_time", "min_write_time"], - "optional": [] - }, - "delegators": { - "required": ["account"], - "optional": ["threshold", "count", "start"] - }, - "delegators_count": { - "required": ["account"], - "optional": [] - }, - "deterministic_key": { - "required": ["seed", "index"], - "optional": [] - }, - "epoch_upgrade": { - "required": ["epoch", "key"], - "optional": ["count", "threads"] - }, - "frontier_count": { - "required": [], - "optional": [] - }, - "frontiers": { - "required": ["account", "count"], - "optional": [] - }, - "keepalive": { - "required": ["address", "port"], - "optional": [] - }, - "key_create": { - "required": [], - "optional": [] - }, - "key_expand": { - "required": ["key"], - "optional": [] - }, - "ledger": { - "required": ["account", "count"], - "optional": [ - "representative", "weight", "receivable", "modified_since", - "sorting", "threshold" - ] - }, - "node_id": { - "required": [], - "optional": [] - }, - "node_id_delete": { - "required": [], - "optional": [] - }, - "peers": { - "required": [], - "optional": ["peer_details"] - }, - "populate_backlog": { - "required": [], - "optional": [] - }, - "process": { - "required": ["json_block", "subtype", "block"], - "optional": ["force", "subtype", "json_block", "watch_work", "async"] - }, - "receivable": { - "required": ["account", "count"], - "optional": [ - "count", "threshold", "source", "include_active", "min_version", - "sorting", "include_only_confirmed" - ] - }, - "receivable_exists": { - "required": ["hash"], - "optional": ["include_active", "include_only_confirmed"] - }, - "representatives": { - "required": [], - "optional": ["count", "sorting"] - }, - "representatives_online": { - "required": [], - "optional": ["weight", "accounts"] - }, - "republish": { - "required": ["hash"], - "optional": ["sources", "destinations"] - }, - "sign": { - "required": ["json_block", "key", "block"], - "requiredAlternative": ["hash"], - "optional": [] - }, - "stats": { - "required": ["type"], - "optional": [] - }, - "stats_clear": { - "required": [], - "optional": [] - }, - "stop": { - "required": [], - "optional": [] - }, - "successors": { - "required": ["block", "count"], - "optional": ["offset", "reverse"] - }, - "telemetry": { - "required": [], - "optional": [] - }, - "validate_account_number": { - "required": ["account"], - "optional": [] - }, - "version": { - "required": [], - "optional": [] - }, - "unchecked": { - "required": ["json_block", "count"], - "optional": [] - }, - "unchecked_clear": { - "required": [], - "optional": [] - }, - "unchecked_get": { - "required": ["json_block", "hash"], - "optional": [] - }, - "unchecked_keys": { - "required": ["json_block", "key", "count"], - "optional": [] - }, - "unopened": { - "required": ["account", "count"], - "optional": ["threshold"] - }, - "uptime": { - "required": [], - "optional": [] - }, - "work_cancel": { - "required": ["hash"], - "optional": [] - }, - "work_generate": { - "required": ["hash"], - "optional": [ - "use_peers", "difficulty", "multiplier", "account", "version", - "block", "json_block" - ] - }, - "work_peer_add": { - "required": ["address", "port"], - "optional": [] - }, - "work_peers": { - "required": [], - "optional": [] - }, - "work_peers_clear": { - "required": [], - "optional": [] - }, - "work_validate": { - "required": ["work", "hash"], - "optional": ["difficulty", "multiplier", "version"] - }, - "account_create": { - "required": ["wallet"], - "optional": ["index", "work"] - }, - "account_list": { - "required": ["wallet"], - "optional": [] - }, - "account_move": { - "required": ["wallet", "source", "accounts"], - "optional": [] - }, - "account_remove": { - "required": ["wallet", "account"], - "optional": [] - }, - "account_representative_set": { - "required": ["wallet", "account", "representative"], - "optional": ["work"] - }, - "accounts_create": { - "required": ["wallet", "count"], - "optional": ["work"] - }, - "password_change": { - "required": ["wallet", "password"], - "optional": [] - }, - "password_enter": { - "required": ["wallet", "password"], - "optional": [] - }, - "password_valid": { - "required": ["wallet"], - "optional": [] - }, - "receive": { - "required": ["wallet", "account", "block"], - "optional": [] - }, - "receive_minimum": { - "required": [], - "optional": [] - }, - "receive_minimum_set": { - "required": ["amount"], - "optional": [] - }, - "search_receivable": { - "required": ["wallet"], - "optional": [] - }, - "search_receivable_all": { - "required": [], - "optional": [] - }, - "send": { - "required": - ["action", "wallet", "source", "destination", "amount", "id"], - "optional": ["work"] - }, - "wallet_add": { - "required": ["wallet", "key"], - "optional": ["work"] - }, - "wallet_add_watch": { - "required": ["wallet", "accounts"], - "optional": [] - }, - "wallet_balances": { - "required": ["wallet"], - "optional": ["threshold"] - }, - "wallet_change_seed": { - "required": ["wallet", "seed"], - "optional": ["count"] - }, - "wallet_contains": { - "required": ["wallet", "account"], - "optional": [] - }, - "wallet_create": { - "required": [], - "optional": [] - }, - "wallet_destroy": { - "required": ["wallet"], - "optional": [] - }, - "wallet_export": { - "required": ["wallet"], - "optional": [] - }, - "wallet_frontiers": { - "required": ["wallet"], - "optional": [] - }, - "wallet_history": { - "required": ["wallet"], - "optional": ["modified_since"] - }, - "wallet_info": { - "required": ["wallet"], - "optional": [] - }, - "wallet_ledger": { - "required": ["wallet"], - "optional": ["representative", "weight", "pending"] - }, - "wallet_lock": { - "required": ["wallet"], - "optional": [] - }, - "wallet_pending": { - "required": ["wallet"], - "optional": ["count", "threshold", "source"] - }, - "wallet_representative": { - "required": ["wallet"], - "optional": [] - }, - "wallet_representative_set": { - "required": ["wallet", "representative"], - "optional": ["work"] - }, - "wallet_republish": { - "required": ["wallet"], - "optional": ["count"] - }, - "wallet_work_get": { - "required": ["wallet"], - "optional": [] - }, - "work_get": { - "required": ["wallet"], - "optional": [] - }, - "work_set": { - "required": ["wallet", "account", "work"], - "optional": [] - }, - "nano_to_raw": { - "required": ["amount"], - "optional": [] - }, - "raw_to_nano": { - "required": ["amount"], - "optional": [] - }, - "active_difficulty": { - "required": [], - "optional": ["include_trend"] - } -} - - -def generate_method(command, required, optional): - - async def method(self, *args, **kwargs): - payload = {"action": command} - for arg, value in zip(required, args): - payload[arg] = value - for arg in optional: - if arg in kwargs: - payload[arg] = kwargs[arg] - return await self.process_payloads([payload]) - - # Set the name and the docstring of the new function - method.__name__ = command - method.__doc__ = f"Execute the {command} command." - - return method - - -class NanoRpcV2: - - def __init__(self, url, username=None, password=None): - self.url = url - self.username = username - self.password = password - self.auth = aiohttp.BasicAuth( - username, password) if username and password else None - self.session: aiohttp.ClientSession = None - - def create_session(self): - self.session = aiohttp.ClientSession() - - async def close_session(self): - await self.session.close() - - async def process_payloads(self, payloads): - async with aiohttp.ClientSession(auth=self.auth) as session: - async with session.post(self.url, json=payloads[0]) as response: - return await response.json() - - -# Generate the methods -for command, params in COMMANDS.items(): - method = generate_method(command, params["required"], params["optional"]) - setattr(NanoRpcV2, method.__name__, method) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 437de2e..57daac9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ pytest pytest-asyncio sqlalchemy +requests nanomock>=0.0.16 \ No newline at end of file diff --git a/setup.py b/setup.py index ff57c7a..9abd9cd 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = fh.read() setup(name="nanolab", - version="0.0.21", + version="0.0.22", author="gr0vity", description="testing tool using nanomock", long_description=long_description, @@ -13,7 +13,7 @@ packages=find_packages(exclude=["unit_tests"]), include_package_data=True, install_requires=["nanomock>=0.0.14", - "py_ed25519_blake2b", "sqlalchemy", "aiohttp", "nanolog_parser"], + "py_ed25519_blake2b", "sqlalchemy", "requests", "aiohttp", "nanolog_parser"], entry_points={ 'console_scripts': [ 'nanolab=nanolab.main:main', diff --git a/unit_tests/test_block_asserts.py b/unit_tests/test_block_asserts.py index 6a5bd95..30fb19c 100644 --- a/unit_tests/test_block_asserts.py +++ b/unit_tests/test_block_asserts.py @@ -10,7 +10,7 @@ @pytest.fixture def mock_rpc(): - with patch('nanolab.src.nano_rpc.NanoRpcV2') as mock: + with patch('nanorpc.client.NanoRpcTyped') as mock: yield mock