From c0df1001fbd35c724900cb67ba509e6e021c5245 Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Tue, 17 Nov 2026 03:02:59 +0100 Subject: [PATCH] wallet: add light wallet implementation --- .github/workflows/ci.yml | 4 +- README.md | 8 +- bin/setup_test_environment.sh | 2 +- bin/stop_wallet_rpc_servers.sh | 5 + conftest.py | 7 +- external/monero-cpp | 2 +- pytest.ini | 5 + src/cpp/py_monero_types.h | 3 + src/cpp/wallet/py_monero_wallet_bindings.cpp | 18 + src/python/__init__.pyi | 2 + src/python/monero_wallet_light.pyi | 68 +++ tests/common/test_rpc_connection.py | 11 + tests/config/config.ini | 2 + tests/docker-compose.yml | 38 +- tests/utils/__init__.py | 4 +- tests/utils/from_multiple_tx_sender.py | 2 +- tests/utils/integration_test_utils.py | 32 +- tests/utils/monero_daemon_lws.py | 574 +++++++++++++++++++ tests/utils/send_and_update_txs_tester.py | 7 +- tests/utils/single_tx_sender.py | 11 +- tests/utils/sync_progress_tester.py | 15 +- tests/utils/test_utils.py | 133 ++++- tests/utils/tx_wallet_tester.py | 2 +- tests/utils/wallet_send_utils.py | 6 + tests/utils/wallet_sweeper.py | 67 ++- tests/utils/wallet_test_utils.py | 6 +- tests/utils/wallet_tx_tracker.py | 83 ++- tests/utils/wallet_type.py | 3 + tests/wallet/base_test_monero_wallet.py | 2 + tests/wallet/conftest.py | 32 +- tests/wallet/test_accounts.py | 54 +- tests/wallet/test_connection.py | 12 +- tests/wallet/test_creation.py | 263 ++++++++- tests/wallet/test_equality.py | 7 +- tests/wallet/test_keys_and_addresses.py | 9 +- tests/wallet/test_listeners.py | 7 +- tests/wallet/test_mining.py | 12 +- tests/wallet/test_multisig.py | 7 +- tests/wallet/test_outputs.py | 34 +- tests/wallet/test_pool.py | 27 +- tests/wallet/test_proofs.py | 34 +- tests/wallet/test_send.py | 15 +- tests/wallet/test_sweep.py | 32 +- tests/wallet/test_sync.py | 87 ++- tests/wallet/test_transfers.py | 7 +- tests/wallet/test_txs.py | 26 +- tests/wallet/test_update_locked.py | 7 +- tests/wallet/wallet_support.py | 120 +++- 48 files changed, 1843 insertions(+), 71 deletions(-) create mode 100755 bin/stop_wallet_rpc_servers.sh create mode 100644 src/python/monero_wallet_light.pyi create mode 100644 tests/utils/monero_daemon_lws.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b88bbe2b..4421d6c8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -155,7 +155,7 @@ jobs: env: # what the docker compose commands of the steps work on COMPOSE_FILE: tests/docker-compose.yml - TEST_SERVICES: node_1 node_2 xmr_wallet_1 xmr_wallet_2 xmr_wallet_3 + TEST_SERVICES: node_1 node_2 xmr_wallet_1 xmr_wallet_2 xmr_wallet_3 xmr_lws strategy: fail-fast: false @@ -196,7 +196,7 @@ jobs: - name: Install the Python test requirements (tests/requirements.txt) run: pip3 install -r tests/requirements.txt --break-system-packages - - name: Start the test environment (2 nodes and 3 wallet rpc servers in docker) + - name: Start the test environment (2 nodes, 3 wallet rpc servers and monero-lws in docker) if: ${{ matrix.docker }} run: | read -r -a services <<< "$TEST_SERVICES" diff --git a/README.md b/README.md index 846848c8..ef1fb8a8 100644 --- a/README.md +++ b/README.md @@ -245,7 +245,7 @@ For example: `export LD_PRELOAD=/path/to/libjemalloc.so.2` then run your app. It ``` 3. Setup docker test environment ```bash - docker compose -f tests/docker-compose.yml up -d node_1 node_2 xmr_wallet_1 xmr_wallet_2 xmr_wallet_3 + docker compose -f tests/docker-compose.yml up -d node_1 node_2 xmr_wallet_1 xmr_wallet_2 xmr_wallet_3 xmr_lws ``` 4. Or start RPC servers locally: 1. Download and install [Monero CLI](https://web.getmonero.org/downloads/). @@ -269,6 +269,12 @@ For example: `export LD_PRELOAD=/path/to/libjemalloc.so.2` then run your app. It pytest tests/wallet -k Rpc ``` + The tests of each wallet type also have a marker, `wallet_full`, `wallet_rpc`, `wallet_keys` and `wallet_light`, that [tests/wallet/conftest.py](tests/wallet/conftest.py) adds from the name of the class, so they can be combined with `unit` and `integration`: + ```bash + pytest -m wallet_rpc + pytest -m "wallet_light and integration" + ``` + The daemon tests are split in the same way: [tests/daemon/rpc](tests/daemon/rpc) has the integration tests of the rpc daemon (a file for each topic, such as `test_blocks.py`, `test_tx_pool.py` and `test_peers.py`), [tests/daemon/model](tests/daemon/model) the unit tests of the daemon models, grouped by model, and [tests/daemon/test_interface.py](tests/daemon/test_interface.py) the ones of the daemon interface. The wallet models are tested in [tests/wallet/model](tests/wallet/model) (a file for each kind of model: queries, transfers, outputs, accounts...), the calls of the wallet interface in [tests/wallet/test_interface.py](tests/wallet/test_interface.py), and the tests of what is not about a wallet or a daemon, such as the rpc connection, in [tests/common](tests/common), where the tests of `MoneroUtils` are split by topic in [tests/common/monero_utils](tests/common/monero_utils). diff --git a/bin/setup_test_environment.sh b/bin/setup_test_environment.sh index 3b23543d..b7d8189d 100755 --- a/bin/setup_test_environment.sh +++ b/bin/setup_test_environment.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash # start docker containers -sudo docker compose -f tests/docker-compose.yml up -d node_1 node_2 xmr_wallet_1 xmr_wallet_2 xmr_wallet_3 +sudo docker compose -f tests/docker-compose.yml up -d node_1 node_2 xmr_wallet_1 xmr_wallet_2 xmr_wallet_3 xmr_lws diff --git a/bin/stop_wallet_rpc_servers.sh b/bin/stop_wallet_rpc_servers.sh new file mode 100755 index 00000000..1087673b --- /dev/null +++ b/bin/stop_wallet_rpc_servers.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +# stop the wallet rpc servers of the test environment, only the rpc wallet tests use them +# the containers and their volumes stay, setup_test_environment.sh starts them again +sudo docker compose -f tests/docker-compose.yml stop xmr_wallet_1 xmr_wallet_2 xmr_wallet_3 diff --git a/conftest.py b/conftest.py index 45ccd687..5bcf3624 100644 --- a/conftest.py +++ b/conftest.py @@ -8,7 +8,12 @@ import pytest -from tests.utils.gen_utils import GenUtils +# pytest explains a failed assert (the values that it compared, the diff) only in the modules that it rewrites: the test files and the +# conftests. The helpers of the tests are imported by the test files, and their asserts failed as a bare `AssertionError`. It has to be +# registered before the first import of the package, under the two names that it has (the tests import it as `utils`) +pytest.register_assert_rewrite("utils", "tests.utils") + +from tests.utils.gen_utils import GenUtils # noqa: E402 if TYPE_CHECKING: from pluggy import Result # runtime-optional (pluggy < 1.2), only needed for typing diff --git a/external/monero-cpp b/external/monero-cpp index 6178753b..0ae1d457 160000 --- a/external/monero-cpp +++ b/external/monero-cpp @@ -1 +1 @@ -Subproject commit 6178753bc9b1956b4d2e834507b6ff4cb55229c7 +Subproject commit 0ae1d457e2a32b8ed820579b6007ffb614f2ba2b diff --git a/pytest.ini b/pytest.ini index e34c2319..f0beb1dc 100644 --- a/pytest.ini +++ b/pytest.ini @@ -2,6 +2,7 @@ minversion = 9.0 required_plugins = pytest-rerunfailures pytest-timeout pytest-cov addopts = -s -v --reruns 5 --reruns-delay 10 --only-rerun "Daemon is busy" +timeout = 900 log_level = INFO log_cli = True log_cli_level = INFO @@ -15,5 +16,9 @@ testpaths = markers = unit: fast unit tests, no external dependencies integration: slow integration tests, requires external services + wallet_full: tests of the full wallet, added to the classes of each wallet type by tests/wallet/conftest.py + wallet_rpc: tests of the rpc wallet + wallet_keys: tests of the keys-only wallet + wallet_light: tests of the light wallet not_supported: inherited test whose operation is permanently unsupported for this type; passes when it raises a "not supported" error, fails otherwise not_implemented: inherited test whose operation is not implemented yet; applied as a non-strict xfail (xfails while it raises, xpasses once the feature lands) diff --git a/src/cpp/py_monero_types.h b/src/cpp/py_monero_types.h index 507e3809..480bf903 100644 --- a/src/cpp/py_monero_types.h +++ b/src/cpp/py_monero_types.h @@ -77,6 +77,7 @@ PYBIND11_MAKE_OPAQUE(VectorUint64); #include "wallet/monero_wallet_rpc.h" #include "wallet/monero_wallet_keys.h" #include "wallet/monero_wallet_full.h" +#include "wallet/monero_wallet_light.h" #include "utils/py_monero_utils.h" // The destructor of a daemon or of a wallet waits for the thread of its poller to finish, and that thread can be calling a listener of @@ -177,6 +178,7 @@ struct PyMoneroTypes { py::class_> py_monero_wallet_keys; py::class_> py_monero_wallet_full; py::class_> py_monero_wallet_rpc; + py::class_> py_monero_wallet_light; py::class_ py_monero_utils; py::class_ py_gen_utils; @@ -230,6 +232,7 @@ struct PyMoneroTypes { py_monero_wallet(m, "MoneroWallet"), py_monero_wallet_keys(m, "MoneroWalletKeys"), py_monero_wallet_full(m, "MoneroWalletFull"), + py_monero_wallet_light(m, "MoneroWalletLight"), py_monero_wallet_rpc(m, "MoneroWalletRpc"), py_monero_utils(m, "MoneroUtils"), py_gen_utils(m, "GenUtils"), diff --git a/src/cpp/wallet/py_monero_wallet_bindings.cpp b/src/cpp/wallet/py_monero_wallet_bindings.cpp index 63be0d16..ba2ec1d0 100644 --- a/src/cpp/wallet/py_monero_wallet_bindings.cpp +++ b/src/cpp/wallet/py_monero_wallet_bindings.cpp @@ -1103,6 +1103,24 @@ void py_monero_bind_wallet(py::module_& m, PyMoneroTypes& t) { MONERO_CATCH_AND_RETHROW(py::bytes(self.get_cache_file_buffer())); }); + // monero_wallet_light + t.py_monero_wallet_light + .def_static("wallet_exists", [](const std::string& primary_address, const std::string& private_view_key, const std::shared_ptr& rpc) { + MONERO_CATCH_AND_RETHROW(monero_wallet_light::wallet_exists(primary_address, private_view_key, rpc)); + }, py::arg("primary_address"), py::arg("private_view_key"), py::arg("rpc"), py::call_guard()) + .def_static("wallet_exists", [](const monero_wallet_config& config, const std::shared_ptr& rpc) { + MONERO_CATCH_AND_RETHROW(monero_wallet_light::wallet_exists(config, rpc)); + }, py::arg("config"), py::arg("rpc"), py::call_guard()) + .def_static("open_wallet", [](const monero_wallet_config& config, const std::shared_ptr& rpc) { + MONERO_CATCH_AND_RETHROW(monero_wallet_light::open_wallet(config, rpc)); + }, py::arg("config"), py::arg("rpc"), py::call_guard()) + .def_static("create_wallet", [](const monero_wallet_config& config, const std::shared_ptr& rpc) { + MONERO_CATCH_AND_RETHROW(monero_wallet_light::create_wallet(config, rpc)); + }, py::arg("config"), py::arg("rpc"), py::call_guard()) + .def("get_rpc_connection", [](monero_wallet_light& self) { + MONERO_CATCH_AND_RETHROW(self.get_rpc_connection()); + }, py::call_guard()); + // monero_wallet_rpc t.py_monero_wallet_rpc .def(py::init([](const std::shared_ptr& rpc_connection) { diff --git a/src/python/__init__.pyi b/src/python/__init__.pyi index a953b0e7..74e0b80f 100644 --- a/src/python/__init__.pyi +++ b/src/python/__init__.pyi @@ -140,6 +140,7 @@ from .monero_wallet_full import MoneroWalletFull from .monero_wallet_keys import MoneroWalletKeys from .monero_wallet_listener import MoneroWalletListener from .monero_wallet_rpc import MoneroWalletRpc +from .monero_wallet_light import MoneroWalletLight __all__ = [ @@ -226,6 +227,7 @@ __all__ = [ 'MoneroWalletKeys', 'MoneroWalletListener', 'MoneroWalletRpc', + 'MoneroWalletLight', 'SslOptions', 'SerializableStruct' ] diff --git a/src/python/monero_wallet_light.pyi b/src/python/monero_wallet_light.pyi new file mode 100644 index 00000000..9de28631 --- /dev/null +++ b/src/python/monero_wallet_light.pyi @@ -0,0 +1,68 @@ +from typing import overload + +from .monero_rpc_connection import MoneroRpcConnection +from .monero_wallet_config import MoneroWalletConfig +from .monero_wallet_keys import MoneroWalletKeys + + +class MoneroWalletLight(MoneroWalletKeys): + """ + Implements a Monero wallet using `monero-lws`_. + + .. _monero-lws: https://github.com/vtnerd/monero-lws + """ + + @staticmethod + @overload + def wallet_exists(primary_address: str, private_view_key: str, rpc: MoneroRpcConnection) -> bool: + """ + Check if a wallet exists on the server. + + :param primary_address: The primary address of the wallet. + :param private_view_key: The private view key of the wallet. + :param rpc: The RPC connection to the Monero server. + :return: True if the wallet exists, False otherwise. + """ + ... + + @staticmethod + @overload + def wallet_exists(config: MoneroWalletConfig, rpc: MoneroRpcConnection) -> bool: + """ + Check if a wallet exists on the server. + + :param config: The wallet configuration. + :param rpc: The RPC connection to the Monero server. + :return: True if the wallet exists, False otherwise. + """ + ... + + @staticmethod + def open_wallet(config: MoneroWalletConfig, rpc: MoneroRpcConnection) -> MoneroWalletLight: + """ + Open an existing light wallet with the given configuration. + + :param config: The configuration for opening the wallet. + :param rpc: The RPC connection to the Monero server. + :return: An instance of MoneroWalletLight. + """ + ... + + @staticmethod + def create_wallet(config: MoneroWalletConfig, rpc: MoneroRpcConnection) -> MoneroWalletLight: + """ + Create a new light wallet with the given configuration. + + :param config: The configuration for creating the wallet. + :param rpc: The RPC connection to the Monero server. + :return: An instance of MoneroWalletLight. + """ + ... + + def get_rpc_connection(self) -> MoneroRpcConnection | None: + """ + Get the wallet's RPC connection. + + :returns MoneroRpcConnection | None: the wallet's rpc connection. + """ + ... diff --git a/tests/common/test_rpc_connection.py b/tests/common/test_rpc_connection.py index 698ec416..4bd1739f 100644 --- a/tests/common/test_rpc_connection.py +++ b/tests/common/test_rpc_connection.py @@ -33,6 +33,12 @@ def wallet_connection(cls) -> MoneroRpcConnection: """Rpc connection test instance.""" return MoneroRpcConnection(Utils.WALLET_RPC_URI, Utils.WALLET_RPC_USERNAME, Utils.WALLET_RPC_PASSWORD, timeout_ms=cls.TIMEOUT_MS) + # Light wallet server rpc connection fixture + @pytest.fixture(scope="class") + def lws_connection(self) -> MoneroRpcConnection: + """Rpc connection test instance.""" + return MoneroRpcConnection(Utils.LWS_RPC_URI, timeout_ms=self.TIMEOUT_MS) + #endregion #region Tests @@ -120,6 +126,11 @@ def test_node_rpc_connection(self, node_connection: MoneroRpcConnection) -> None def test_wallet_rpc_connection(self, wallet_connection: MoneroRpcConnection) -> None: RpcConnectionUtils.test_rpc_connection(wallet_connection, Utils.WALLET_RPC_URI, True, MoneroConnectionType.IPV4) + # Test light wallet server rpc connection + @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") + def test_lws_rpc_connection(self, lws_connection: MoneroRpcConnection) -> None: + RpcConnectionUtils.test_rpc_connection(lws_connection, Utils.LWS_RPC_URI, True, MoneroConnectionType.IPV4) + # Test invalid connection @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") def test_invalid_connection(self) -> None: diff --git a/tests/config/config.ini b/tests/config/config.ini index bb0c0f0b..c585eae5 100644 --- a/tests/config/config.ini +++ b/tests/config/config.ini @@ -17,6 +17,8 @@ zmq_uri=tcp://127.0.0.1:18085 zmq_pub_uri=tcp://127.0.0.1:18086 log_level=3 poll_period_in_ms=10000 +lws_uri=http://127.0.0.1:8443 +lws_admin_uri=http://127.0.0.1:8444 [wallet] name=test_wallet_1 diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml index abeaa9b8..b874e83f 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -9,6 +9,7 @@ services: - xmr_wallet_1 - xmr_wallet_2 - xmr_wallet_3 + - xmr_lws node_1: image: ghcr.io/libmonero/monero-daemon-rpc:${MONERO_TAG:-master} @@ -92,6 +93,11 @@ services: ] ports: - "18082:18082" + # A wallet rpc that gets an address that is not valid looks it up as an OpenAlias domain (a DNSSEC query), and it does not answer any + # request until the query ends: in CI it took 3 to 4 minutes, and the tests that send to an invalid address timed out. The tests do + # not use OpenAlias: a resolver that is not reachable makes the query fail at once, and the wallet answers "wrong address" + environment: + DNS_PUBLIC: "tcp://127.0.0.1" volumes: - xmr_wallet_1_data:/home/monero/wallets depends_on: @@ -117,6 +123,8 @@ services: ] ports: - "18083:18083" + environment: + DNS_PUBLIC: "tcp://127.0.0.1" volumes: - xmr_wallet_2_data:/home/monero/wallets depends_on: @@ -142,15 +150,43 @@ services: ] ports: - "18084:18084" + environment: + DNS_PUBLIC: "tcp://127.0.0.1" volumes: - xmr_wallet_3_data:/home/monero/wallets depends_on: - node_1 - node_2 + xmr_lws: + image: vtnerd/monero-lws:master + command: > + --daemon=tcp://node_2:18085 + --sub=tcp://node_2:18086 + --log-level=4 + --webhook-ssl-verification=none + --disable-admin-auth + --rest-server=http://0.0.0.0:8443/ + --admin-rest-server=http://0.0.0.0:8444/ + --access-control-origin=* + --confirm-external-bind + --max-subaddresses=1000 + --auto-accept-creation + --auto-accept-import + --regtest + volumes: + - xmr_lws_data:/data + ports: + - "8443:8443" + - "8444:8444" + depends_on: + - node_1 + - node_2 + volumes: xmr_node_1_data: xmr_node_2_data: xmr_wallet_1_data: xmr_wallet_2_data: - xmr_wallet_3_data: \ No newline at end of file + xmr_wallet_3_data: + xmr_lws_data: diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index d6209bb2..f9ca7469 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -55,6 +55,7 @@ from .wallet_send_utils import WalletSendUtils from .wallet_test_utils import WalletTestUtils from .wallet_error_utils import WalletErrorUtils +from .monero_daemon_lws import MoneroDaemonLws __all__ = [ 'WalletUtils', @@ -105,5 +106,6 @@ 'TxsStructureTester', 'DaemonNotificationCollector', 'FakeDaemonRpcServer', - 'FakeWalletRpcServer' + 'FakeWalletRpcServer', + 'MoneroDaemonLws' ] diff --git a/tests/utils/from_multiple_tx_sender.py b/tests/utils/from_multiple_tx_sender.py index 3560e81d..ebfb70fd 100644 --- a/tests/utils/from_multiple_tx_sender.py +++ b/tests/utils/from_multiple_tx_sender.py @@ -75,7 +75,7 @@ def _get_src_account(self) -> MoneroAccount: if num_subaddress_balances >= self.NUM_SUBADDRESSES + 1: has_balance = True - if len(unlocked_subaddresses) >= self.NUM_SUBADDRESSES + 1: + if len(unlocked_subaddresses) > self.NUM_SUBADDRESSES + 1: src_account = account break diff --git a/tests/utils/integration_test_utils.py b/tests/utils/integration_test_utils.py index 8a692114..094d4dd4 100644 --- a/tests/utils/integration_test_utils.py +++ b/tests/utils/integration_test_utils.py @@ -2,7 +2,7 @@ from abc import ABC from time import sleep -from monero import MoneroWallet, MoneroTxWallet, MoneroTxQuery, MoneroSyncResult +from monero import MoneroWallet, MoneroWalletLight, MoneroTxWallet, MoneroTxQuery, MoneroSyncResult from .wallet_test_utils import WalletTestUtils from .mining_utils import MiningUtils @@ -49,22 +49,38 @@ def setup(cls, wallet_type: WalletType) -> None: elif wallet_type == WalletType.RPC: wallet = TestUtils.get_wallet_rpc() type_str = "RPC" + elif wallet_type == WalletType.LIGHT: + wallet = TestUtils.get_wallet_light() + type_str = "LIGHT" else: - raise ValueError("Only RPC and FULL wallet are supported for integration tests") + raise ValueError("Only RPC, FULL, and LIGHT wallets are supported for integration tests") + # sync before checking for pre-existing txs: MoneroWalletLight has no local persistent + # storage, so its cache (and get_txs()) is empty until synced, even for an already-funded address + wallet.sync() wallet_txs: list[MoneroTxWallet] = wallet.get_txs() num_wallet_txs: int = len(wallet_txs) # fund wallet with mined coins and wait for unlocked balance txs: list[MoneroTxWallet] = cls.fund_wallet_and_wait_for_unlocked(wallet) - # setup first receive height - tx: MoneroTxWallet = txs[0] if num_wallet_txs == 0 else wallet_txs[0] - tx_height: int | None = tx.get_height() - assert tx_height is not None + # setup first receive height: the one of the oldest tx of the wallet, read after the funding. The types of wallet share the seed, so + # the ones that run after the first find the txs of the previous types. A light wallet has not got them all before the blocks of + # the funding: lws scans again from the restore height of the account when a block arrives, and until it ends the wallet has + # only the txs of the last blocks. A wallet that is restored from a later height, as the ground truth of the equality tests is, + # does not have the older txs. + if isinstance(wallet, MoneroWalletLight): + TestUtils.wait_for_lws_scan(wallet) + wallet.sync() + heights: list[int] = [h for h in (wallet_tx.get_height() for wallet_tx in wallet.get_txs()) if h is not None] + tx_height: int | None = min(heights) if len(heights) > 0 else txs[0].get_height() + assert tx_height is not None, "Could not get test wallet first receive height" TestUtils.FIRST_RECEIVE_HEIGHT = tx_height - logger.debug(f"Test wallet first receive height: {tx_height}") + logger.debug(f"FIRST_RECEIVE_HEIGHT = {tx_height}") - if num_wallet_txs < len(txs): + if num_wallet_txs == 0 and TestUtils.REGTEST: + # needed for correct m_num_suggested_confirmations estimate in light wallet + MiningUtils.generate_blocks(wallet.get_primary_address(), 1) + wallet.sync() logger.info(f"Funded test wallet {type_str}") cls._LAST_SETUP = wallet_type diff --git a/tests/utils/monero_daemon_lws.py b/tests/utils/monero_daemon_lws.py new file mode 100644 index 00000000..37e94081 --- /dev/null +++ b/tests/utils/monero_daemon_lws.py @@ -0,0 +1,574 @@ +import logging +import time +from typing import Any +from monero import MoneroRpcConnection + +logger: logging.Logger = logging.getLogger("MoneroDaemonLws") + + +class MoneroDaemonLws: + """Thin client for the monero-lws REST API, both the client (wallet) surface and the + admin surface. + + Client: `/login`, `/get_address_info`, `/get_address_txs`, `/get_random_outs`, + `/get_subaddrs`, `/get_unspent_outs`, `/get_version`, `/import_wallet_request`, + `/provision_subaddrs`, `/submit_raw_tx`, `/upsert_subaddrs`, and the undocumented + `/daemon_status` convenience endpoint. `MoneroWalletLight` exercises this same surface + internally; this exists to inspect the raw server responses directly (e.g. to correlate + `scanned_block_height`/`start_height` against the daemon's own height during debugging). + `/feed` (websocket upgrade) and `/get_tree_path` (fcmp++ tree signing) are out of scope. + + Admin: `/accept_requests`, `/add_account`, `/list_accounts`, `/list_requests`, + `/modify_account_status`, `/reject_requests`, `/rescan`, `/validate`, `/webhook_add`, + `/webhook_delete`, `/webhook_delete_uuid`, and `/webhook_list`. + """ + + rpc: MoneroRpcConnection | None + """Rpc connection to the monero-lws client (wallet) REST server, or `None` if this + instance was constructed without a client `uri` (admin-only usage).""" + admin_rpc: MoneroRpcConnection | None + """Rpc connection to the monero-lws admin REST server, or `None` if this instance was + constructed without an `admin_uri` (client-only usage). + + monero-lws serves the client and admin surfaces on separate listeners (`--rest-server` + and `--admin-rest-server`), which can be different ports entirely, or the same host:port + merged by path prefix -- either way they are logically and physically distinct + connections, so each gets its own `MoneroRpcConnection` rather than sharing one.""" + auth: str | None + """Admin auth key from `monero-lws-admin create_admin`, or `None` if the server was + started with `--disable-admin-auth`.""" + + def __init__(self, uri: str | None = None, admin_uri: str | None = None, auth: str | None = None) -> None: + """Initialize a new monero-lws client, admin, or combined client. + + At least one of `uri`/`admin_uri` must be given. Calling a client method without a + `uri`, or an admin method without an `admin_uri`, raises `RuntimeError`. + + :param str | None uri: client (wallet) REST server uri, e.g. `http://127.0.0.1:8443`. + :param str | None admin_uri: admin REST server uri, e.g. `http://127.0.0.1:8444`. + :param str | None auth: admin auth key, if the server requires one. + """ + if uri is None and admin_uri is None: + raise ValueError("Must provide at least one of uri or admin_uri") + + # do not call MoneroRpcConnection.check_connection() here: it probes with a binary + # get_blocks_by_height request meant for a monerod daemon connection, which monero-lws + # doesn't implement (404, tolerated) -- but reusing that same keep-alive connection + # afterwards for a JSON request (e.g. get_address_info) then arrives at the server with + # a corrupted/truncated body ("missing required field: address"). A connection that's + # genuinely unreachable will fail clearly on the first real request anyway. + self.rpc = MoneroRpcConnection(uri) if uri is not None else None + self.admin_rpc = MoneroRpcConnection(admin_uri) if admin_uri is not None else None + + self.auth = auth + + def _request(self, endpoint: str, params: dict[str, Any] | None = None) -> Any: + """Send an admin request and return the parsed JSON response. + + :param str endpoint: admin endpoint name (without the leading `/`). + :param dict[str, Any] | None params: endpoint-specific `params` object, if any. + :returns Any: the parsed JSON response. + :raises RuntimeError: if this instance was constructed without an `admin_uri`. + """ + if self.admin_rpc is None: + raise RuntimeError("MoneroDaemonLws was constructed without admin_uri; cannot call admin endpoints") + + body: dict[str, Any] = {} + if params is not None: + body["params"] = params + if self.auth is not None: + body["auth"] = self.auth + logger.debug(f"POST (admin) /{endpoint} {body}") + return self.admin_rpc.send_path_request(endpoint, body) + + def _client_request(self, endpoint: str, body: dict[str, Any]) -> Any: + """Send a client (wallet) request and return the parsed JSON response. + + Unlike the admin surface, client endpoints take the request fields directly at the + top level of the body -- there is no `params`/`auth` wrapper. + + :param str endpoint: client endpoint name (without the leading `/`). + :param dict[str, Any] body: the full request body. + :returns Any: the parsed JSON response. + :raises RuntimeError: if this instance was constructed without a client `uri`. + """ + if self.rpc is None: + raise RuntimeError("MoneroDaemonLws was constructed without uri; cannot call client endpoints") + + logger.debug(f"POST /{endpoint} {body}") + return self.rpc.send_path_request(endpoint, body) + + @staticmethod + def _address_meta(lookahead: tuple[int, int]) -> dict[str, int]: + """Build an `address_meta` (subaddress lookahead) object. + + :param tuple[int, int] lookahead: `(maj_i, min_i)`. + :returns dict[str, int]: `{"maj_i": ..., "min_i": ...}`. + """ + return {"maj_i": lookahead[0], "min_i": lookahead[1]} + + #region Client API + + def daemon_status(self) -> dict[str, Any]: + """Get the underlying daemon's connection/sync state, as seen by monero-lws. + + Undocumented convenience endpoint (mentioned in `wallet.yaml`'s description but not + given its own path entry). + + :returns dict[str, Any]: `state`, `outgoing_connections_count`, + `incoming_connections_count`, `height`, `target_height`, `network`. + """ + return self._client_request("daemon_status", {}) + + def wait_for_daemon_height(self, height: int, timeout_seconds: float = 30.0, poll_interval_seconds: float = 0.5) -> int: + """Poll `daemon_status` until the height that monero-lws has of the daemon reaches `height`. + + lws updates it in the background, so right after blocks are generated it can be one block behind, and a light wallet that asks + for the height of the daemon then gets it. + + :param int height: height of the daemon to wait for, as a block count (what `get_height()` of the daemon returns). + :param float timeout_seconds: give up and raise after this many seconds. + :param float poll_interval_seconds: delay between polls. + :returns int: the reached height (>= `height`). + :raises TimeoutError: if `height` isn't reached within `timeout_seconds`. + """ + deadline = time.monotonic() + timeout_seconds + while True: + current: int = self.daemon_status()["height"] + if current >= height: + return current + if time.monotonic() >= deadline: + raise TimeoutError(f"monero-lws did not see the daemon at height {height} within {timeout_seconds}s (stuck at {current})") + time.sleep(poll_interval_seconds) + + def login( + self, + address: str, + view_key: str, + create_account: bool = False, + generated_locally: bool = False, + lookahead: tuple[int, int] | None = None, + ) -> dict[str, Any]: + """Create and/or check an account's status. + + :param str address: base58 primary address. + :param str view_key: hex-encoded private view key for `address`. + :param bool create_account: attempt account creation if it doesn't already exist. + :param bool generated_locally: `True` if this is a brand new (not restored) wallet. + :param tuple[int, int] | None lookahead: desired `(maj_i, min_i)` lookahead for a + newly created account. + :returns dict[str, Any]: `new_address`, `generated_locally`, `start_height`, `lookahead`. + """ + body: dict[str, Any] = { + "address": address, + "view_key": view_key, + "create_account": create_account, + "generated_locally": generated_locally, + } + if lookahead is not None: + body["lookahead"] = self._address_meta(lookahead) + return self._client_request("login", body) + + def get_address_info(self, address: str, view_key: str) -> dict[str, Any]: + """Get the minimal information needed to calculate a wallet's balance. + + :param str address: base58 primary address. + :param str view_key: hex-encoded private view key for `address`. + :returns dict[str, Any]: `locked_funds`, `total_received`, `total_sent`, + `scanned_height`, `scanned_block_height`, `start_height`, `transaction_height`, + `blockchain_height`, `spent_outputs`, `lookahead`, `lookahead_failure`, `rates`. + """ + return self._client_request("get_address_info", {"address": address, "view_key": view_key}) + + def wait_for_scan_height( + self, + address: str, + view_key: str, + height: int, + timeout_seconds: float = 60.0, + poll_interval_seconds: float = 2.0, + ) -> int: + """Poll `get_address_info` for an already-registered account until its scan progress + reaches `height`. + + monero-lws scans blocks asynchronously in the background, so an account's + `scanned_height` can lag behind the daemon's live height for a short while after new + blocks appear. A newly created account's own `start_height` is assigned from the + server's current scan progress at that instant, so callers that need a new account's + `start_height` to match a specific daemon height should wait for an already-registered + account to catch up to that height first, before creating the new one. + + :param str address: base58 primary address of an already-registered account. + :param str view_key: hex-encoded private view key for `address`. + :param int height: block height to wait for. + :param float timeout_seconds: give up and raise after this many seconds. + :param float poll_interval_seconds: delay between polls. + :returns int: the reached `scanned_height` (>= `height`). + :raises TimeoutError: if `height` isn't reached within `timeout_seconds`. + """ + deadline = time.monotonic() + timeout_seconds + scanned_height = 0 + while True: + scanned_height = self.get_address_info(address, view_key)["scanned_height"] + if scanned_height >= height: + return scanned_height + if time.monotonic() >= deadline: + raise TimeoutError( + f"monero-lws scan did not reach height {height} within {timeout_seconds}s " + f"(stuck at {scanned_height})" + ) + time.sleep(poll_interval_seconds) + + def get_address_txs( + self, + address: str, + view_key: str, + since_tx_id: int | None = None, + since_tx_block_hash: str | None = None, + ) -> dict[str, Any]: + """Get transaction history. + + :param str address: base58 primary address. + :param str view_key: hex-encoded private view key for `address`. + :param int | None since_tx_id: only return transactions newer than this known tx id. + :param str | None since_tx_block_hash: block hash of the `since_tx_id` transaction, + required if `since_tx_id` is given (guards against a reorg invalidating it). + :returns dict[str, Any]: `total_received`, `scanned_height`, `scanned_block_height`, + `start_height`, `blockchain_height`, `transactions`, `lookahead`, + `lookahead_failure`, `since_tx_id`. + """ + body: dict[str, Any] = {"address": address, "view_key": view_key} + if since_tx_id is not None: + body["since_tx_id"] = since_tx_id + if since_tx_block_hash is not None: + body["since_tx_block_hash"] = since_tx_block_hash + return self._client_request("get_address_txs", body) + + def get_random_outs(self, count: int, amounts: list[str]) -> dict[str, Any]: + """Get server-selected decoy outputs for ring signatures. + + :param int count: mixin (number of decoys per amount). + :param list[str] amounts: XMR amounts (as decimal strings) that need decoys; `"0"` + for RingCT outputs. + :returns dict[str, Any]: `{"amount_outs": [...]}`. + """ + return self._client_request("get_random_outs", {"count": count, "amounts": amounts}) + + def get_subaddrs(self, address: str, view_key: str) -> dict[str, Any]: + """Get all subaddresses provisioned for a wallet. + + :param str address: base58 primary address. + :param str view_key: hex-encoded private view key for `address`. + :returns dict[str, Any]: `all_subaddrs`, `max_subaddrs`. + """ + return self._client_request("get_subaddrs", {"address": address, "view_key": view_key}) + + def get_unspent_outs( + self, + address: str, + view_key: str, + amount: int, + mixin: int, + use_dust: bool, + dust_threshold: str | None = None, + ) -> dict[str, Any]: + """Get outputs available for spending (client must determine what's already spent). + + :param str address: base58 primary address. + :param str view_key: hex-encoded private view key for `address`. + :param int amount: XMR amount intended to be sent. + :param int mixin: minimum mixin for source outputs. + :param bool use_dust: return all available outputs, including dust. + :param str | None dust_threshold: ignore outputs below this amount (decimal string). + :returns dict[str, Any]: `per_byte_fee`, `fee_mask`, `amount`, `outputs`, `fees` + (priority-ordered fee-per-byte estimates, lowest first). + """ + body: dict[str, Any] = { + "address": address, + "view_key": view_key, + "amount": amount, + "mixin": mixin, + "use_dust": use_dust, + } + if dust_threshold is not None: + body["dust_threshold"] = dust_threshold + return self._client_request("get_unspent_outs", body) + + def get_version(self) -> dict[str, Any]: + """Get basic information about the monero-lws server itself. + + :returns dict[str, Any]: `server_type`, `last_git_commit_hash`, `last_commit_date`, + `monero_version_full`, `blockchain_height`, `api`, `max_subaddresses`, `network`, + `testnet`. + """ + return self._client_request("get_version", {}) + + def import_wallet_request( + self, + address: str, + view_key: str, + from_height: int | None = None, + lookahead: tuple[int, int] | None = None, + ) -> dict[str, Any]: + """Request a rescan from an earlier height, with optional subaddress lookahead. + + :param str address: base58 primary address. + :param str view_key: hex-encoded private view key for `address`. + :param int | None from_height: height to rescan from (server assumes `0` if omitted). + :param tuple[int, int] | None lookahead: desired `(maj_i, min_i)` lookahead for the + rescan (server assumes `(0, 0)` if omitted). + :returns dict[str, Any]: `payment_address`, `payment_id`, `import_fee`, `new_request`, + `request_fulfilled`, `status`, `lookahead`. + """ + body: dict[str, Any] = {"address": address, "view_key": view_key} + if from_height is not None: + body["from_height"] = from_height + if lookahead is not None: + body["lookahead"] = self._address_meta(lookahead) + return self._client_request("import_wallet_request", body) + + def provision_subaddrs( + self, + address: str, + view_key: str, + maj_i: int | None = None, + min_i: int | None = None, + n_maj: int | None = None, + n_min: int | None = None, + get_all: bool | None = None, + ) -> dict[str, Any]: + """Request new subaddress ranges be provisioned, starting at the given lower bounds. + + :param str address: base58 primary address. + :param str view_key: hex-encoded private view key for `address`. + :param int | None maj_i: major index lower bound (server default `0`). + :param int | None min_i: minor index lower bound (server default `0`). + :param int | None n_maj: number of major indices to provision. + :param int | None n_min: number of minor indices to provision (per major). + :param bool | None get_all: include all (not just newly provisioned) subaddresses + in the response (server default `True`). + :returns dict[str, Any]: `new_subaddrs`, `all_subaddrs`. + """ + body: dict[str, Any] = {"address": address, "view_key": view_key} + if maj_i is not None: + body["maj_i"] = maj_i + if min_i is not None: + body["min_i"] = min_i + if n_maj is not None: + body["n_maj"] = n_maj + if n_min is not None: + body["n_min"] = n_min + if get_all is not None: + body["get_all"] = get_all + return self._client_request("provision_subaddrs", body) + + def submit_raw_tx(self, tx_hex: str) -> str: + """Relay a raw transaction to the Monero network. + + :param str tx_hex: hex-encoded raw transaction. + :returns str: the daemon's relay status (typically `"OK"`). + """ + result = self._client_request("submit_raw_tx", {"tx": tx_hex}) + return result["status"] + + def upsert_subaddrs( + self, + address: str, + view_key: str, + subaddrs: dict[str, list[list[int]]], + get_all: bool | None = None, + ) -> dict[str, Any]: + """Upsert subaddresses at specific major/minor indexes (idempotent). + + :param str address: base58 primary address. + :param str view_key: hex-encoded private view key for `address`. + :param dict[str, list[list[int]]] subaddrs: major index (as a string key) mapped to + a list of `[min, max]` inclusive minor-index ranges. + :param bool | None get_all: include all (not just upserted) subaddresses in the + response (server default `True`). + :returns dict[str, Any]: `new_subaddrs`, `all_subaddrs`. + """ + body: dict[str, Any] = {"address": address, "view_key": view_key, "subaddrs": subaddrs} + if get_all is not None: + body["get_all"] = get_all + return self._client_request("upsert_subaddrs", body) + + #endregion + + #region Account Administration + + def accept_requests(self, request_type: str, addresses: list[str]) -> list[str]: + """Accept pending create or import account requests. + + :param str request_type: `"create"` or `"import"`. + :param list[str] addresses: base58 addresses to accept. + :returns list[str]: addresses that were updated. + """ + result = self._request("accept_requests", {"type": request_type, "addresses": addresses}) + return result["updated"] + + def reject_requests(self, request_type: str, addresses: list[str]) -> list[str]: + """Reject pending create or import account requests. + + :param str request_type: `"create"` or `"import"`. + :param list[str] addresses: base58 addresses to reject. + :returns list[str]: addresses that were updated. + """ + result = self._request("reject_requests", {"type": request_type, "addresses": addresses}) + return result["updated"] + + def add_account(self, address: str, view_key: str) -> list[str]: + """Add a new account directly, bypassing the `/login` create-request flow. + + :param str address: base58 primary address. + :param str view_key: hex-encoded private view key for `address`. + :returns list[str]: addresses that were updated. + """ + result = self._request("add_account", {"address": address, "key": view_key}) + return result["updated"] + + def list_accounts(self) -> dict[str, list[dict[str, Any]]]: + """List all accounts by state. + + :returns dict[str, list[dict]]: `{"active": [...], "inactive": [...], "hidden": [...]}`, + each entry an object with `address`, `scan_height`, and `access_time`. + """ + return self._request("list_accounts") + + def list_requests(self) -> dict[str, list[dict[str, Any]]]: + """List all pending create and import account requests. + + :returns dict[str, list[dict]]: `{"create": [...], "import": [...]}`, each entry + an object with `address` and `start_height`. + """ + return self._request("list_requests") + + def modify_account_status(self, status: str, addresses: list[str]) -> list[str]: + """Move account(s) to another state. + + :param str status: `"active"`, `"inactive"`, or `"hidden"`. + :param list[str] addresses: base58 addresses to modify. + :returns list[str]: addresses that were updated. + """ + result = self._request("modify_account_status", {"status": status, "addresses": addresses}) + return result["updated"] + + def rescan(self, height: int, addresses: list[str]) -> list[str]: + """Force account(s) to rescan from a specific block height. + + :param int height: block height to rescan from. + :param list[str] addresses: base58 addresses to rescan. + :returns list[str]: addresses that were updated. + """ + result = self._request("rescan", {"height": height, "addresses": addresses}) + return result["updated"] + + def validate(self, spend_public_hex: str, view_public_hex: str, view_key_hex: str) -> str: + """Validate that a spend public key, view public key, and view secret key are consistent, + and derive the corresponding address. + + :param str spend_public_hex: hex-encoded public spend key. + :param str view_public_hex: hex-encoded public view key. + :param str view_key_hex: hex-encoded private view key. + :returns str: the base58 address derived from the given keys. + :raises RuntimeError: if the keys are not valid/consistent; identifies the offending field. + """ + result = self._request("validate", { + "spend_public_hex": spend_public_hex, + "view_public_hex": view_public_hex, + "view_key_hex": view_key_hex, + }) + if "error" in result: + error = result["error"] + raise RuntimeError(f"{error['field']}: {error['details']}") + return result["address"] + + #endregion + + #region Webhook Administration + + def webhook_add_tx_confirmation( + self, + url: str, + address: str, + payment_id: str | None = None, + token: str | None = None, + confirmations: int | None = None, + ) -> dict[str, Any]: + """Register a webhook fired when a transaction to `address` is confirmed. + + :param str url: destination URL, or `"zmq"` to publish over ZMQ PUB/SUB only. + :param str address: base58 address to watch. + :param str | None payment_id: optional 8-byte hex payment ID filter. + :param str | None token: optional token echoed back in the webhook payload. + :param int | None confirmations: confirmations required before firing (server default 1). + :returns dict[str, Any]: webhook-value with `event_id`, `payment_id`, `token`, + `confirmations`, and `url`. + """ + params: dict[str, Any] = {"type": "tx-confirmation", "url": url, "address": address} + if payment_id is not None: + params["payment_id"] = payment_id + if token is not None: + params["token"] = token + if confirmations is not None: + params["confirmations"] = confirmations + return self._request("webhook_add", params) + + def webhook_add_tx_spend( + self, + url: str, + address: str, + payment_id: str | None = None, + token: str | None = None, + ) -> dict[str, Any]: + """Register a webhook fired when an output belonging to `address` is spent. + + :param str url: destination URL, or `"zmq"` to publish over ZMQ PUB/SUB only. + :param str address: base58 address to watch. + :param str | None payment_id: optional 8-byte hex payment ID filter. + :param str | None token: optional token echoed back in the webhook payload. + :returns dict[str, Any]: webhook-value with `event_id`, `payment_id`, `token`, + `confirmations`, and `url`. + """ + params: dict[str, Any] = {"type": "tx-spend", "url": url, "address": address} + if payment_id is not None: + params["payment_id"] = payment_id + if token is not None: + params["token"] = token + return self._request("webhook_add", params) + + def webhook_add_new_account(self, url: str, token: str | None = None) -> dict[str, Any]: + """Register a webhook fired whenever a new account is created. + + :param str url: destination URL, or `"zmq"` to publish over ZMQ PUB/SUB only. + :param str | None token: optional token echoed back in the webhook payload. + :returns dict[str, Any]: webhook-value with `event_id`, `payment_id`, `token`, + `confirmations`, and `url`. + """ + params: dict[str, Any] = {"type": "new-account", "url": url} + if token is not None: + params["token"] = token + return self._request("webhook_add", params) + + def webhook_delete(self, addresses: list[str]) -> None: + """Delete webhooks associated with the given address(es). + + :param list[str] addresses: base58 addresses whose webhooks should be removed. + """ + self._request("webhook_delete", {"addresses": addresses}) + + def webhook_delete_uuid(self, event_ids: list[str]) -> None: + """Delete webhooks by event UUID. + + :param list[str] event_ids: 16-byte hex event UUIDs to remove. + """ + self._request("webhook_delete_uuid", {"event_ids": event_ids}) + + def webhook_list(self) -> dict[str, Any]: + """List all registered webhooks. + + :returns dict[str, Any]: `{"webhooks": {...}}`. + """ + return self._request("webhook_list") + + #endregion diff --git a/tests/utils/send_and_update_txs_tester.py b/tests/utils/send_and_update_txs_tester.py index 3c919aab..c07c70fa 100644 --- a/tests/utils/send_and_update_txs_tester.py +++ b/tests/utils/send_and_update_txs_tester.py @@ -3,7 +3,7 @@ from time import sleep from monero import ( MoneroWallet, MoneroTxConfig, MoneroTxWallet, - MoneroDaemon, MoneroBlockHeader, MoneroTxQuery + MoneroDaemon, MoneroBlockHeader, MoneroTxQuery, MoneroWalletLight ) from .test_utils import TestUtils @@ -163,6 +163,11 @@ def wait_for_confirmations(self, sent_txs: list[MoneroTxWallet], num_confirmatio # TODO: this lets new block slip, okay? sleep(TestUtils.SYNC_PERIOD_IN_MS / 1000) + if isinstance(self.wallet, MoneroWalletLight): + # lws scans the blocks in the background and does not report the txs of the pool: wait for the one that confirmed them + TestUtils.wait_for_lws_scan(self.wallet) + self.wallet.sync() + # get incoming/outgoing txs with sent hashes tx_hashes: list[str] = [] for sent_tx in sent_txs: diff --git a/tests/utils/single_tx_sender.py b/tests/utils/single_tx_sender.py index b356fe96..bcb3b8fc 100644 --- a/tests/utils/single_tx_sender.py +++ b/tests/utils/single_tx_sender.py @@ -182,11 +182,16 @@ def _send_to_invalid(self, config: MoneroTxConfig) -> None: finally: config.set_address(self.address) - # retry on transient network error - if str(exc_info.value) == "Network error" and attempt <= max_retries: + # retry on transient network error (the message goes on: "Network error: no response from (timeout: ms)") + error: str = str(exc_info.value) + if error.startswith("Network error") and attempt <= max_retries: + logger.warning(f"Network error sending to an invalid address ({attempt}/{max_retries + 1}), retrying: {error}") continue - assert str(exc_info.value) == "Invalid destination address" + assert error == "Invalid destination address", ( + f"Sending to an invalid address ({attempt}/{max_retries + 1}) must fail with 'Invalid destination address', " + f"it failed with {type(exc_info.value).__name__}: {error!r}" + ) return def _send_to_self(self, config: MoneroTxConfig) -> list[MoneroTxWallet]: diff --git a/tests/utils/sync_progress_tester.py b/tests/utils/sync_progress_tester.py index fdc5dfa0..c50e0f13 100644 --- a/tests/utils/sync_progress_tester.py +++ b/tests/utils/sync_progress_tester.py @@ -1,16 +1,18 @@ - +import logging from typing import Optional from typing_extensions import override -from monero import MoneroWalletFull +from monero import MoneroWallet from .wallet_sync_printer import WalletSyncPrinter +logger: logging.Logger = logging.getLogger("SyncProgressTester") + class SyncProgressTester(WalletSyncPrinter): """Wallet sync progress tester.""" - wallet: MoneroWalletFull + wallet: MoneroWallet """Test wallet instance.""" start_height: int """Blockchain start height.""" @@ -35,10 +37,10 @@ def is_notified(self) -> bool: """ return self.prev_height is not None - def __init__(self, wallet: MoneroWalletFull, start_height: int, end_height: int) -> None: + def __init__(self, wallet: MoneroWallet, start_height: int, end_height: int) -> None: """Initialize a new wallet sync progress tester. - :param MoneroWalletFull wallet: wallet to test. + :param MoneroWallet wallet: wallet to test. :param int start_height: wallet start height. :param int end_height: wallet end height. """ @@ -108,8 +110,9 @@ def on_done(self, chain_height: int) -> None: assert self.is_done is False self.is_done = True if self.prev_height is None: + logger.info("Wallet already synced") assert self.prev_complete_height is None - assert chain_height == self.start_height + assert chain_height == self.start_height, f"{chain_height} != {self.start_height}" else: # otherwise the last progress notification reports the final block assert chain_height - 1 == self.prev_height diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index ee0a6b2d..1738d48a 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -9,14 +9,16 @@ MoneroNetworkType, MoneroWalletFull, MoneroRpcConnection, MoneroWalletConfig, MoneroDaemonRpc, MoneroWalletRpc, MoneroWallet, MoneroRpcError, MoneroWalletKeys, - MoneroUtils + MoneroUtils, MoneroWalletLight ) +from .string_utils import StringUtils from .wallet_sync_printer import WalletSyncPrinter from .wallet_tx_tracker import WalletTxTracker from .gen_utils import GenUtils from .daemon_utils import DaemonUtils from .docker_wallet_rpc_manager import DockerWalletRpcManager +from .monero_daemon_lws import MoneroDaemonLws logger: logging.Logger = logging.getLogger("TestUtils") @@ -47,8 +49,12 @@ class TestUtils(ABC): """Default wallet rpc used for tests.""" _WALLET_MINING: Optional[MoneroWalletFull] = None """Mining wallet used for funding test wallets.""" + _WALLET_LIGHT: Optional[MoneroWalletLight] = None + """Default wallet light used for tests.""" _DAEMON_RPC: Optional[MoneroDaemonRpc] = None """Default daemon rpc used for tests.""" + _DAEMON_LWS: Optional[MoneroDaemonLws] = None + """Default daemon lws used for tests.""" _MINING_DAEMON: Optional[MoneroDaemonRpc] = None """Internal daemon used for mining.""" _WALLET_RPC_2: Optional[MoneroWalletRpc] = None @@ -66,6 +72,12 @@ class TestUtils(ABC): """Monero daemon rpc zmq uri.""" DAEMON_RPC_ZMQ_PUB_URI: str = "" """Monero daemon rpc zmq pub uri.""" + + LWS_RPC_URI: str = "" + """Monero daemon lws rpc uri.""" + LWS_ADMIN_RPC_URI: str = "" + """Monero daemon lws admin rpc uri.""" + MAX_LWS_SUBADDRESSES: int = 200 TEST_NON_RELAYS: bool = True """Indicates if non-relays tests are enabled.""" TEST_RELAYS: bool = True @@ -212,6 +224,8 @@ def load_config(cls) -> None: cls.DAEMON_RPC_PASSWORD = parser.get('daemon', 'rpc_password') cls.DAEMON_RPC_ZMQ_URI = parser.get('daemon', 'zmq_uri') cls.DAEMON_RPC_ZMQ_PUB_URI = parser.get('daemon', 'zmq_pub_uri') + cls.LWS_RPC_URI = parser.get('daemon', 'lws_uri') + cls.LWS_ADMIN_RPC_URI = parser.get('daemon', 'lws_admin_uri') # parse wallet config cls.WALLET_NAME = parser.get('wallet', 'name') @@ -307,6 +321,31 @@ def get_daemon_rpc(cls) -> MoneroDaemonRpc: return cls._DAEMON_RPC + @classmethod + def get_daemon_lws(cls) -> MoneroDaemonLws: + """Get test daemon lws. + + :returns MoneroDaemonLws: test daemon lws instance. + """ + + if cls._DAEMON_LWS is None: + cls._DAEMON_LWS = MoneroDaemonLws(cls.LWS_RPC_URI, cls.LWS_ADMIN_RPC_URI) + + return cls._DAEMON_LWS + + @classmethod + def wait_for_lws_scan(cls, wallet: MoneroWalletLight) -> int: + """Wait until monero-lws has scanned the blocks of the daemon for the account of a light wallet. + + lws scans the blocks in the background, so right after some are generated (instantly, on regtest at a low difficulty) the account + does not have yet what they have, and a wallet that syncs then does not see it. + + :param MoneroWalletLight wallet: light wallet whose account is waited for. + :returns int: scanned height of the account. It is one less than the height of the daemon, which counts the blocks. + """ + height: int = cls.get_daemon_rpc().get_height() - 1 + return cls.get_daemon_lws().wait_for_scan_height(wallet.get_primary_address(), wallet.get_private_view_key(), height) + @classmethod def get_mining_daemon_rpc_connection(cls) -> MoneroRpcConnection: """Get the rpc connection of the daemon used for internal mining. @@ -339,7 +378,7 @@ def get_daemon_rpc_connection(cls) -> MoneroRpcConnection: def close_wallets(cls) -> None: """Close the test wallets that were opened, saving the full and the rpc one. The tests of a wallet type share the wallet for the whole session, and this is called at its end.""" - for wallet, save in ((cls._WALLET_FULL, True), (cls._WALLET_RPC, True), (cls._WALLET_KEYS, False)): + for wallet, save in ((cls._WALLET_FULL, True), (cls._WALLET_RPC, True), (cls._WALLET_KEYS, False), (cls._WALLET_LIGHT, False)): if wallet is None: continue closed: bool = not cls.is_wallet_rpc_open(wallet) if isinstance(wallet, MoneroWalletRpc) else wallet.is_closed() @@ -352,6 +391,22 @@ def close_wallets(cls) -> None: if "No wallet file" != str(e): raise + @classmethod + def get_daemon_lws_connection(cls) -> MoneroRpcConnection: + """Get test daemon lws connection. + + :returns MoneroRpcConnection: new test daemon lws connection instance. + """ + return MoneroRpcConnection(cls.LWS_RPC_URI) + + @classmethod + def get_daemon_lws_admin_connection(cls) -> MoneroRpcConnection: + """Get test daemon lws admin connection. + + :returns MoneroRpcConnection: new test daemon lws admin connection instance. + """ + return MoneroRpcConnection(cls.LWS_ADMIN_RPC_URI) + @classmethod def get_wallet_keys_config(cls) -> MoneroWalletConfig: """Get test wallet keys configuration. @@ -594,6 +649,33 @@ def open_wallet_rpc(cls, c: Optional[MoneroWalletConfig]) -> MoneroWalletRpc: """ return cls.RPC_WALLET_MANAGER.open_wallet(c, cls.IN_CONTAINER) + @classmethod + def create_wallet_full(cls, config: Optional[MoneroWalletConfig], random: bool, start_syncing: bool = True) -> MoneroWalletFull: + # assign defaults + if config is None: + config = MoneroWalletConfig() + if config.path is None: + config.path = cls.TEST_WALLETS_DIR + "/" + StringUtils.get_random_string() + if config.password is None: + config.password = cls.WALLET_PASSWORD + if config.network_type is None: + config.network_type = cls.NETWORK_TYPE + if config.server is None: + config.server = cls.get_daemon_rpc_connection() + if config.restore_height is None and not random: + config.restore_height = 0 + + config.regtest = config.network_type == MoneroNetworkType.MAINNET and cls.REGTEST + + # create wallet + wallet = MoneroWalletFull.create_wallet(config) + if not random: + assert config.restore_height == wallet.get_restore_height() + if start_syncing is not False and wallet.is_connected_to_daemon(): + wallet.start_syncing(cls.SYNC_PERIOD_IN_MS) + + return wallet + @classmethod def create_wallet_rpc(cls, c: Optional[MoneroWalletConfig]) -> MoneroWalletRpc: """Create rpc wallet. @@ -603,6 +685,53 @@ def create_wallet_rpc(cls, c: Optional[MoneroWalletConfig]) -> MoneroWalletRpc: """ return cls.RPC_WALLET_MANAGER.create_wallet(c, cls.IN_CONTAINER) + @classmethod + def get_wallet_config_light(cls) -> MoneroWalletConfig: + """Get light wallet configuration. + + :returns MoneroWalletConfig: light wallet configuration. + """ + + config = MoneroWalletConfig() + config.path = cls.get_random_wallet_path() + config.password = cls.WALLET_PASSWORD + config.network_type = cls.NETWORK_TYPE + config.seed = cls.SEED + config.restore_height = cls.FIRST_RECEIVE_HEIGHT + config.regtest = cls.REGTEST + + # The types of wallet that run before this one use the same seed, and the account of lws sees the txs to the accounts and the + # subaddresses that it has registered: the lookahead has to cover the ones that the tests create (the sends to multiple + # subaddresses go up to 5 accounts and 15 subaddresses), or the txs that the other types made to the rest are not seen. + config.account_lookahead = 10 + config.subaddress_lookahead = 30 + + return config + + @classmethod + def get_wallet_light(cls) -> MoneroWalletLight: + """Get test wallet light. + + :returns MoneroWalletLight: light test wallet. + """ + config: MoneroWalletConfig = cls.get_wallet_config_light() + rpc: MoneroRpcConnection = cls.get_daemon_lws_connection() + + if cls._WALLET_LIGHT is None or cls._WALLET_LIGHT.is_closed(): + if not MoneroWalletLight.wallet_exists(config, rpc): + # create wallet from seed if it doesn't exist + cls._WALLET_LIGHT = MoneroWalletLight.create_wallet(config, rpc) + else: + cls._WALLET_LIGHT = MoneroWalletLight.open_wallet(config, rpc) + + # sync and start background synchronizing with sync period, matching get_wallet_full()/get_wallet_rpc() + cls._WALLET_LIGHT.sync() + cls._WALLET_LIGHT.start_syncing(cls.SYNC_PERIOD_IN_MS) + logger.debug("Started light wallet background synchronizing") + logger.info("CREATED LIGHT WALLET: " + cls._WALLET_LIGHT.get_primary_address()) + + return cls._WALLET_LIGHT + @classmethod def get_all_rpc_connections(cls) -> list[MoneroRpcConnection]: """Get all daemon and wallets rpc connections used in tests (ordered by connection uri). diff --git a/tests/utils/tx_wallet_tester.py b/tests/utils/tx_wallet_tester.py index d25f81a6..a9b26a28 100644 --- a/tests/utils/tx_wallet_tester.py +++ b/tests/utils/tx_wallet_tester.py @@ -58,7 +58,7 @@ def _test_common(self) -> None: assert self.tx.unlock_time is not None assert self.tx.unlock_time >= 0 assert self.tx.size is None # TODO monero-wallet-rpc: add tx_size to get_transfers and get_transfer_by_txid - assert self.tx.received_timestamp is None # TODO monero-wallet-rpc: return received timestamp (asked to file issue if wanted) + assert self.tx.received_timestamp is None, self.tx.serialize() # TODO monero-wallet-rpc: return received timestamp (asked to file issue if wanted) def _test_send(self) -> None: """Test send tx fields.""" diff --git a/tests/utils/wallet_send_utils.py b/tests/utils/wallet_send_utils.py index 11abf016..7803b016 100644 --- a/tests/utils/wallet_send_utils.py +++ b/tests/utils/wallet_send_utils.py @@ -10,6 +10,8 @@ from .wallet_sweeper import WalletSweeper from .send_and_update_txs_tester import SendAndUpdateTxsTester from .sync_with_pool_submit_tester import SyncWithPoolSubmitTester +from .tx_wallet_utils import TxWalletUtils +from .test_utils import TestUtils class WalletSendUtils(ABC): @@ -39,6 +41,10 @@ def test_send_from_multiple(cls, wallet: MoneroWallet, can_split: bool | None) - :param MoneroWallet wallet: test wallet to send txs from. :param bool can_split: can split wallet txs. """ + # this is needed for light wallet since lws can be stuck + TestUtils.WALLET_TX_TRACKER.wait_for_wallet_unlocked_balance( + wallet, FromMultipleTxSender.NUM_SUBADDRESSES + 2, TxWalletUtils.MAX_FEE + ) sender: FromMultipleTxSender = FromMultipleTxSender(wallet, can_split) sender.send() diff --git a/tests/utils/wallet_sweeper.py b/tests/utils/wallet_sweeper.py index 6280faf4..22fd7f3f 100644 --- a/tests/utils/wallet_sweeper.py +++ b/tests/utils/wallet_sweeper.py @@ -46,30 +46,73 @@ def _check_for_balance(self) -> None: assert len(subaddresses_balance) >= 2, "Test requires multiple accounts with a balance greater than the fee; run send to multiple first" assert len(subaddresses_unlocked) >= 2, "Wallet is waiting on unlocked funds" - def _check_outputs(self) -> None: - """Check for outputs amount after sweep.""" - # all unspent, unlocked outputs must be less than fee + def _get_unlocked_outputs(self) -> list[MoneroOutputWallet]: + """Get the unspent outputs that are unlocked. + + :returns list[MoneroOutputWallet]: unspent and unlocked outputs of the wallet. + """ query: MoneroOutputQuery = MoneroOutputQuery() query.is_spent = False tx_query: MoneroTxQuery = MoneroTxQuery() tx_query.is_locked = False query.set_tx_query(tx_query, True) - spendable_outputs: list[MoneroOutputWallet] = self._wallet.get_outputs(query) - for spendable_output in spendable_outputs: - assert spendable_output.amount is not None - assert spendable_output.amount < TxWalletUtils.MAX_FEE, f"Unspent output should have been swept\n{spendable_output.serialize()}" - - # all subaddress unlocked balances must be less than fee + outputs: list[MoneroOutputWallet] = self._wallet.get_outputs(query) + for output in outputs: + assert output.stealth_public_key is not None + assert output.amount is not None + assert output.account_index is not None + assert output.subaddress_index is not None + return outputs + + def _check_outputs(self, unlocked_before: set[str]) -> None: + """Check for outputs amount after sweep. + + The sweep takes the outputs that are unlocked when it starts. The ones that are locked then (the change of the txs just before, and + the outputs that the sweep creates: it sends to the primary address of the wallet) are not swept, and the chain does not wait for + the checks: the blocks keep coming while the wallet syncs (the light wallet gets them from lws, which scans after the daemon), so + they can be unlocked when they are checked. They are not funds that the sweep left behind, so they are not counted. + + :param set[str] unlocked_before: stealth public keys of the outputs that were unlocked when the sweep started. + """ + # The balances are read before the outputs: when a block unlocks outputs in between, the outputs that are subtracted from the + # balances are more, not less, and the check does not fail for them + balances: dict[tuple[int, int], int] = {} for account in self._wallet.get_accounts(True): for subaddress in account.subaddresses: assert subaddress.unlocked_balance is not None - assert subaddress.unlocked_balance < TxWalletUtils.MAX_FEE, "No subaddress should have more unlocked than the fee" + assert account.index is not None + assert subaddress.index is not None + balances[(account.index, subaddress.index)] = subaddress.unlocked_balance + + # all the outputs that were unlocked and unspent must be less than fee + unlocked_later: dict[tuple[int, int], int] = {} + for spendable_output in self._get_unlocked_outputs(): + assert spendable_output.amount is not None + assert spendable_output.account_index is not None + assert spendable_output.subaddress_index is not None + if spendable_output.stealth_public_key not in unlocked_before: + key: tuple[int, int] = (spendable_output.account_index, spendable_output.subaddress_index) + unlocked_later[key] = unlocked_later.get(key, 0) + spendable_output.amount + continue + tx_hash: str | None = spendable_output.tx.hash if spendable_output.tx is not None else None # type: ignore + assert spendable_output.amount < TxWalletUtils.MAX_FEE, ( + f"Unspent output should have been swept, it was unlocked before the sweep (tx {tx_hash})\n{spendable_output.serialize()}" + ) + + # all subaddress unlocked balances must be less than fee + for (account_index, subaddress_index), unlocked_balance in balances.items(): + later: int = unlocked_later.get((account_index, subaddress_index), 0) + assert unlocked_balance - later < TxWalletUtils.MAX_FEE, ( + f"No subaddress should have more unlocked than the fee: subaddress ({account_index}, {subaddress_index}) has " + f"{unlocked_balance} unlocked, {later} of outputs that were locked when the sweep started" + ) def sweep(self) -> None: """Sweep outputs from wallet.""" # cleanup and check balance TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(self._wallet) self._check_for_balance() + unlocked_before: set[str] = {str(output.stealth_public_key) for output in self._get_unlocked_outputs()} # sweep funds destination: str = self._wallet.get_primary_address() @@ -104,4 +147,6 @@ def sweep(self) -> None: ctx.is_sweep_response = True TxWalletUtils.test_tx_wallet(tx, ctx) - self._check_outputs() + TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(self._wallet) + + self._check_outputs(unlocked_before) diff --git a/tests/utils/wallet_test_utils.py b/tests/utils/wallet_test_utils.py index 1752ba81..040aa5fb 100644 --- a/tests/utils/wallet_test_utils.py +++ b/tests/utils/wallet_test_utils.py @@ -6,7 +6,7 @@ from monero import ( MoneroNetworkType, MoneroUtils, MoneroAccount, MoneroSubaddress, MoneroWallet, MoneroTxConfig, MoneroDestination, - MoneroTxWallet, MoneroWalletFull, MoneroWalletRpc, + MoneroTxWallet, MoneroWalletFull, MoneroWalletRpc, MoneroWalletLight ) from .test_utils import TestUtils @@ -75,7 +75,7 @@ def is_wallet_funded(cls, wallet: MoneroWallet, xmr_amount_per_address: float, n amount_required: int = amount_required_per_account * num_accounts required_subaddresses: int = num_accounts * (num_subaddresses + 1) # include primary address - if not isinstance(wallet, MoneroWalletFull) and not isinstance(wallet, MoneroWalletRpc): + if not isinstance(wallet, (MoneroWalletFull, MoneroWalletRpc, MoneroWalletLight)): return False # sync wallet @@ -176,7 +176,7 @@ def fund_wallet( amount_required_str: str = f"{MoneroUtils.atomic_units_to_xmr(amount_required)} XMR" logger.debug(f"Funding wallet {primary_addr} with {amount_required_str}...") - supports_get_accounts: bool = isinstance(wallet, MoneroWalletRpc) or isinstance(wallet, MoneroWalletFull) + supports_get_accounts: bool = isinstance(wallet, MoneroWalletRpc) or isinstance(wallet, MoneroWalletFull) or isinstance(wallet, MoneroWalletLight) supports_save: bool = isinstance(wallet, MoneroWalletRpc) or isinstance(wallet, MoneroWalletFull) tx_config: MoneroTxConfig = cls.build_tx_config(wallet, num_accounts, num_subaddresses, amount_per_address, supports_get_accounts) diff --git a/tests/utils/wallet_tx_tracker.py b/tests/utils/wallet_tx_tracker.py index 3457b64e..35f376ce 100644 --- a/tests/utils/wallet_tx_tracker.py +++ b/tests/utils/wallet_tx_tracker.py @@ -1,9 +1,9 @@ import logging -from time import sleep +from time import monotonic, sleep from monero import ( MoneroDaemon, MoneroWallet, MoneroWalletRpc, MoneroTxQuery, MoneroSyncResult, - MoneroTxWallet, MoneroMiningStatus + MoneroTxWallet, MoneroMiningStatus, MoneroAccount, MoneroWalletLight ) from .block_generation import is_generation_fast @@ -30,6 +30,10 @@ class WalletTxTracker: _can_generate_blocks: bool | None = None """Indicates if the daemon generates blocks on request (regtest only), `None` until it is tried.""" + WAIT_TIMEOUT_SECONDS: float = 300.0 + """Longest that a wallet is waited for, to reach the height of the daemon or to have unlocked funds. A wallet that does not get there + (a light wallet when monero-lws does not catch up) fails the test, instead of waiting until the job is cancelled.""" + @property def sync_period(self) -> float: """Sync period in seconds. @@ -49,6 +53,27 @@ def __init__(self, daemon: MoneroDaemon, sync_period_ms: int, mining_address: st self._sync_period_ms = sync_period_ms self._mining_address = mining_address + @classmethod + def get_unlocked_accounts(cls, wallet: MoneroWallet, num_subaddresses: int, min_amount: int) -> list[MoneroAccount]: + """Get the accounts that have at least `num_subaddresses` subaddresses with an unlocked balance greater than `min_amount`. + + The subaddresses are counted inside each account: the unlocked ones of different accounts do not add up. + + :param MoneroWallet wallet: wallet to get the accounts of. + :param int num_subaddresses: minimum number of subaddresses with unlocked balance of an account. + :param int min_amount: unlocked balance that a subaddress must exceed to count. + :returns list[MoneroAccount]: the accounts that satisfy the condition. + """ + accounts: list[MoneroAccount] = [] + for account in wallet.get_accounts(True): + num_unlocked: int = sum( + 1 for subaddress in account.subaddresses + if subaddress.unlocked_balance is not None and subaddress.unlocked_balance > min_amount + ) + if num_unlocked >= num_subaddresses: + accounts.append(account) + return accounts + def _sleep(self) -> None: """Sleep for one sync period.""" sleep(self.sync_period) @@ -171,7 +196,17 @@ def _wait_for_txs_to_clear(self, clear_from_wallet: bool, wallets: list[MoneroWa # sync wallets with the pool for i, wallet in enumerate(wallets): + if isinstance(wallet, MoneroWalletLight): + # lws scans the blocks by itself and the light wallet only gets what it has scanned: wait for it before syncing + from .test_utils import TestUtils # test_utils creates a tracker, so it is imported here + TestUtils.wait_for_lws_scan(wallet) + deadline: float = monotonic() + self.WAIT_TIMEOUT_SECONDS while wallet.get_height() < self._daemon.get_height(): + if monotonic() > deadline: + raise TimeoutError( + f"Wallet {i + 1} is at height {wallet.get_height()}, the daemon at {self._daemon.get_height()}, " + f"after {self.WAIT_TIMEOUT_SECONDS:.0f}s of syncing" + ) result = wallet.sync() assert result.num_blocks_fetched is not None if result.num_blocks_fetched > 0: @@ -272,3 +307,47 @@ def wait_for_unlocked_balance( self._daemon.stop_mining() return unlocked_balance + + def wait_for_wallet_unlocked_balance( + self, wallet: MoneroWallet, + num_subaddresses: int, min_amount: int | None = None + ) -> None: + """Wait until some account has at least `num_subaddresses` subaddresses with unlocked balance. + + :param MoneroWallet wallet: Wallet to wait for unlocked balance. + :param int num_subaddresses: Minimum number of subaddresses with unlocked balance required within a single account. + :param int | None min_amount: Minimum unlocked balance per subaddress to count it (default 0). + """ + if min_amount is None: + min_amount = 0 + + found: list[MoneroAccount] = self.get_unlocked_accounts(wallet, num_subaddresses, min_amount) + if len(found) > 0: + logger.debug(f"Wallet already has an account with {num_subaddresses} unlocked subaddresses") + return + + # start mining, unless the blocks are generated on each iteration (regtest at a low difficulty) + mining_started: bool = False + if not is_generation_fast(self._daemon) and not self._daemon.get_mining_status().is_active: + try: + self._daemon.start_mining(self._mining_address, 1, False, False) + mining_started = True + except Exception as e: + logger.warning(f"An error occurred while starting mining: {str(e)}") + # no problem + + logger.info(f"Waiting for an account with {num_subaddresses} unlocked subaddresses") + deadline: float = monotonic() + self.WAIT_TIMEOUT_SECONDS + try: + while len(found) == 0: + if monotonic() > deadline: + raise TimeoutError(f"No account has {num_subaddresses} unlocked subaddresses after {self.WAIT_TIMEOUT_SECONDS:.0f}s") + if not self._generate_block(): + self._sleep() + wallet.sync() + found = self.get_unlocked_accounts(wallet, num_subaddresses, min_amount) + finally: + # stop mining if started + if mining_started: + self._daemon.stop_mining() + diff --git a/tests/utils/wallet_type.py b/tests/utils/wallet_type.py index 5fda184e..c442182b 100644 --- a/tests/utils/wallet_type.py +++ b/tests/utils/wallet_type.py @@ -13,5 +13,8 @@ class WalletType(IntEnum): FULL = 2 """Full local wallet.""" + LIGHT = 3 + """Light wallet.""" + UNDEFINED = 255 """Invalid wallet type.""" diff --git a/tests/wallet/base_test_monero_wallet.py b/tests/wallet/base_test_monero_wallet.py index a6479d3e..7585ca48 100644 --- a/tests/wallet/base_test_monero_wallet.py +++ b/tests/wallet/base_test_monero_wallet.py @@ -81,6 +81,8 @@ def get_test_wallet(cls) -> MoneroWallet: cls._test_wallet = TestUtils.get_wallet_rpc() elif wallet_type == WalletType.KEYS: cls._test_wallet = TestUtils.get_wallet_keys() + elif wallet_type == WalletType.LIGHT: + cls._test_wallet = TestUtils.get_wallet_light() else: raise Exception("Cannot get test wallet: No wallet type setup for tests") diff --git a/tests/wallet/conftest.py b/tests/wallet/conftest.py index a82096e6..cf39c46a 100644 --- a/tests/wallet/conftest.py +++ b/tests/wallet/conftest.py @@ -16,9 +16,17 @@ def close_test_wallets() -> Generator[None, Any, None]: _SEND_TESTS = "tests/wallet/test_send.py" -_WALLET_TYPES = ("TestMoneroWalletFull", "TestMoneroWalletRpc", "TestMoneroWalletKeys") +_SWEEP_TESTS = "tests/wallet/test_sweep.py" + +_WALLET_TYPES = ("TestMoneroWalletFull", "TestMoneroWalletRpc", "TestMoneroWalletKeys", "TestMoneroWalletLight") """The classes of each type of wallet start with these, in the order that the types run.""" +_LIGHT_RANK = _WALLET_TYPES.index("TestMoneroWalletLight") +"""The place of the light wallet in `_WALLET_TYPES`.""" + +_WALLET_TYPE_MARKERS = ("wallet_full", "wallet_rpc", "wallet_keys", "wallet_light") +"""The marker of each type in `_WALLET_TYPES`, so that `pytest -m wallet_rpc` runs only the tests of the rpc wallet.""" + def _is_wallet_test(item: pytest.Item) -> bool: """A test of tests/wallet that is not about the models, which are unit tests that make nothing.""" @@ -35,10 +43,20 @@ def _wallet_type_rank(item: pytest.Item) -> int: return len(_WALLET_TYPES) +def _place_in_type(item: pytest.Item) -> int: + """The place of a test among the ones of its type: the send tests first, the sweep tests of the light wallet last, the rest between.""" + if item.nodeid.startswith(_SEND_TESTS): + return 0 + if item.nodeid.startswith(_SWEEP_TESTS) and _wallet_type_rank(item) == _LIGHT_RANK: + return 2 + return 1 + + +@pytest.hookimpl(tryfirst=True) def pytest_collection_modifyitems(items: list[pytest.Item]) -> None: """The tests of tests/wallet run one type of wallet at a time, and the send tests first. - A file of a topic has a class for each type of wallet (full, rpc and keys), and the files run in alphabetical order, so the types + A file of a topic has a class for each type of wallet (full, rpc, keys and light), and the files run in alphabetical order, so the types would alternate: the full wallet of a topic, the rpc wallet of the same one, and the next topic. The wallet of a type is the same for all its tests, and the tests of a type use what the ones before made in it, so all the tests of a type run, and then the ones of the next: as when they were in a class for each type. @@ -48,16 +66,24 @@ def pytest_collection_modifyitems(items: list[pytest.Item]) -> None: sends were the first tests when all of them were in a class, the relays tests, and test_proofs.py and test_txs.py come before test_send.py in the alphabet. + The sweep tests of the light wallet are the last ones of its type: they sweep the whole wallet, and the tests that look for the outputs + of its subaddresses (test_get_txs_with_query) need them unspent. They were kept last in its class for it. + Only the tests of tests/wallet move, in the places that they have: everything else stays where it was. It is a stable sort, so inside the groups the order is the one of the files. """ positions: list[int] = [i for i, item in enumerate(items) if _is_wallet_test(item)] if not positions: return + # the marker of the type goes first: `-m` selects the tests after the hooks that add markers ran + for i in positions: + rank: int = _wallet_type_rank(items[i]) + if rank < len(_WALLET_TYPE_MARKERS): + items[i].add_marker(_WALLET_TYPE_MARKERS[rank]) wallet_items: list[pytest.Item] = [items[i] for i in positions] reordered: list[pytest.Item] = sorted( wallet_items, - key=lambda item: (_wallet_type_rank(item), 0 if item.nodeid.startswith(_SEND_TESTS) else 1), + key=lambda item: (_wallet_type_rank(item), _place_in_type(item)), ) for position, item in zip(positions, reordered): items[position] = item diff --git a/tests/wallet/test_accounts.py b/tests/wallet/test_accounts.py index 7a991ca3..31586ca1 100644 --- a/tests/wallet/test_accounts.py +++ b/tests/wallet/test_accounts.py @@ -26,7 +26,7 @@ WalletUtils, ) from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletAccounts") @@ -477,6 +477,58 @@ def test_get_subaddress_rpc(self, wallet: MoneroWalletRpc) -> None: assert subaddress.address == wallet.get_address(account_idx, subaddress_idx) +@pytest.mark.integration +class TestMoneroWalletLightAccounts(LightWalletSupport, BaseTestMoneroWalletAccounts): + """Light wallet accounts integration tests.""" + + # Can create a subaddress without label + @override + def test_create_subaddress(self, wallet: MoneroWallet) -> None: + # create subaddresses across accounts + accounts: list[MoneroAccount] = wallet.get_accounts() + if len(accounts) < 2: + wallet.create_account() + + accounts = wallet.get_accounts() + assert len(accounts) > 1 + account_idx: int = 0 + while account_idx < 2: + # create subaddress with no label + subaddresses = wallet.get_subaddresses(account_idx) + subaddress = wallet.create_subaddress(account_idx) + assert subaddress.label is None + WalletUtils.test_subaddress(subaddress) + subaddresses_new = wallet.get_subaddresses(account_idx) + assert len(subaddresses_new) - 1 == len(subaddresses) + AssertUtils.assert_equals(subaddress, subaddresses_new[len(subaddresses_new) - 1]) + account_idx += 1 + + @pytest.mark.not_supported + @override + def test_create_account_with_label(self, wallet: MoneroWallet) -> None: + return super().test_create_account_with_label(wallet) + + @pytest.mark.not_supported + @override + def test_set_account_label(self, wallet: MoneroWallet) -> None: + return super().test_set_account_label(wallet) + + @pytest.mark.not_supported + @override + def test_set_subaddress_label(self, wallet: MoneroWallet) -> None: + return super().test_set_subaddress_label(wallet) + + @pytest.mark.not_supported + @override + def test_address_book(self, wallet: MoneroWallet) -> None: + return super().test_address_book(wallet) + + @pytest.mark.not_supported + @override + def test_account_tags(self, wallet: MoneroWallet) -> None: + return super().test_account_tags(wallet) + + @pytest.mark.unit class TestMoneroWalletKeysAccounts(KeysWalletSupport, BaseTestMoneroWalletAccounts): """Keys-only wallet accounts unit tests.""" diff --git a/tests/wallet/test_connection.py b/tests/wallet/test_connection.py index 6d0c1c39..c07776ad 100644 --- a/tests/wallet/test_connection.py +++ b/tests/wallet/test_connection.py @@ -8,7 +8,7 @@ from utils import AssertUtils, TestUtils, TestUtils as Utils, WalletErrorUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletConnection") @@ -128,6 +128,16 @@ def test_get_daemon_height(self, wallet: MoneroWallet) -> None: return super().test_get_daemon_height(wallet) +@pytest.mark.integration +class TestMoneroWalletLightConnection(LightWalletSupport, BaseTestMoneroWalletConnection): + """Light wallet connection integration tests.""" + + @pytest.mark.not_supported + @override + def test_set_daemon_connection(self) -> None: + return super().test_set_daemon_connection() + + @pytest.mark.unit class TestMoneroWalletKeysConnection(KeysWalletSupport, BaseTestMoneroWalletConnection): """Keys-only wallet connection unit tests.""" diff --git a/tests/wallet/test_creation.py b/tests/wallet/test_creation.py index 3833741d..cdde7457 100644 --- a/tests/wallet/test_creation.py +++ b/tests/wallet/test_creation.py @@ -17,12 +17,14 @@ MoneroWalletConfig, MoneroWalletFull, MoneroWalletKeys, + MoneroWalletLight, MoneroWalletRpc, ) from typing_extensions import override from utils import ( AssertUtils, + BlockchainUtils, GenUtils, StringUtils, SyncProgressTester, @@ -34,7 +36,7 @@ WalletUtils, ) from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletCreation") @@ -207,19 +209,32 @@ def test_subaddress_lookahead(self, wallet: MoneroWallet) -> None: config: MoneroWalletConfig = MoneroWalletConfig() config.account_lookahead = 1 config.subaddress_lookahead = 100000 + subaddress_idx: int = 85000 + + if isinstance(wallet, MoneroWalletLight): + subaddress_lookahead: int = int(TestUtils.MAX_LWS_SUBADDRESSES / 2) + config.subaddress_lookahead = subaddress_lookahead + subaddress_idx = subaddress_lookahead - 1 + receiver = self._create_wallet(config) # transfer funds to subaddress with high index tx_config: MoneroTxConfig = MoneroTxConfig() tx_config.account_index = 0 dest: MoneroDestination = MoneroDestination() - dest.address = receiver.get_subaddress(0, 85000).address + dest.address = receiver.get_subaddress(0, subaddress_idx).address dest.amount = TxWalletUtils.MAX_FEE tx_config.destinations.append(dest) tx_config.relay = True wallet.create_tx(tx_config) + if isinstance(receiver, MoneroWalletLight): + # lws doesn't report unconfirmed txs, must mine some blocks + current_height: int = BlockchainUtils.wait_for_blocks(10) + while receiver.get_height() < current_height: + receiver.sync() + # observe unconfirmed funds GenUtils.wait_for(1000) receiver.sync() @@ -863,6 +878,250 @@ def test_subaddress_lookahead(self, wallet: MoneroWallet) -> None: return super().test_subaddress_lookahead(wallet) +@pytest.mark.integration +class TestMoneroWalletLightCreation(LightWalletSupport, BaseTestMoneroWalletCreation): + """Light wallet creation integration tests.""" + + @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") + @override + def test_create_wallet_random(self) -> None: + """ + Can create a random wallet. + """ + config = MoneroWalletConfig() + wallet: MoneroWalletLight = self._create_wallet(config) + seed: str = wallet.get_seed() + + try: + MoneroUtils.validate_address(wallet.get_primary_address(), Utils.NETWORK_TYPE) + MoneroUtils.validate_private_view_key(wallet.get_private_view_key()) + MoneroUtils.validate_private_spend_key(wallet.get_private_spend_key()) + MoneroUtils.validate_mnemonic(wallet.get_seed()) + assert MoneroWallet.DEFAULT_LANGUAGE == wallet.get_seed_language() + finally: + self._close_wallet(wallet) + + # attempt to create wallet at same path + try: + config = MoneroWalletConfig() + config.seed = seed + config.network_type = Utils.NETWORK_TYPE + MoneroWalletLight.create_wallet(config, Utils.get_daemon_lws_connection()) + raise Exception("Should have thrown error") + except Exception as e: + e_msg: str = str(e) + assert "Wallet already exists" == e_msg, e_msg + + # attempt to create wallet with unknown language + try: + config = MoneroWalletConfig() + config.language = "english" + config.network_type = Utils.NETWORK_TYPE + self._create_wallet(config) + raise Exception("Should have thrown error") + except Exception as e: + e_msg: str = str(e) + assert "Unknown language: english" == e_msg, e_msg + + # Can create a light wallet from a seed + @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") + @override + def test_create_wallet_from_seed(self, wallet: MoneroWallet, test_config: BaseTestMoneroWallet.Config) -> None: + # create random wallet + config: MoneroWalletConfig = MoneroWalletConfig() + config.network_type = Utils.NETWORK_TYPE + random_wallet: MoneroWalletKeys = MoneroWalletKeys.create_wallet_random(config) + seed: str = random_wallet.get_seed() + + # save for comparison + primary_address = random_wallet.get_primary_address() + private_view_key = random_wallet.get_private_view_key() + private_spend_key = random_wallet.get_private_spend_key() + + config = MoneroWalletConfig() + config.seed = seed + + w: MoneroWalletLight = self._create_wallet(config) + + try: + assert primary_address == w.get_primary_address() + assert private_view_key == w.get_private_view_key() + assert private_spend_key == w.get_private_spend_key() + assert Utils.SEED, w.get_seed() + assert MoneroWallet.DEFAULT_LANGUAGE == w.get_seed_language() + finally: + self._close_wallet(w) + + # attempt to create wallet at same path + try: + config = MoneroWalletConfig() + config.seed = seed + config.network_type = Utils.NETWORK_TYPE + MoneroWalletLight.create_wallet(config, Utils.get_daemon_lws_connection()) + raise Exception("Should have thrown error") + except Exception as e: + e_msg: str = str(e) + assert "Wallet already exists" == e_msg, e_msg + + # Can create a light wallet from keys + @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") + def test_create_wallet_from_keys(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: + # create random wallet + config: MoneroWalletConfig = MoneroWalletConfig() + config.network_type = Utils.NETWORK_TYPE + random_wallet: MoneroWalletKeys = MoneroWalletKeys.create_wallet_random(config) + + # save for comparison + primary_address = random_wallet.get_primary_address() + private_view_key = random_wallet.get_private_view_key() + private_spend_key = random_wallet.get_private_spend_key() + + config = MoneroWalletConfig() + config.primary_address = primary_address + config.private_view_key = private_view_key + config.private_spend_key = private_spend_key + + w: MoneroWalletLight = self._create_wallet(config) + + try: + assert primary_address == w.get_primary_address() + assert private_view_key == w.get_private_view_key() + assert private_spend_key == w.get_private_spend_key() + assert Utils.SEED, w.get_seed() + assert MoneroWallet.DEFAULT_LANGUAGE == w.get_seed_language() + finally: + self._close_wallet(w) + + # attempt to create wallet at same path + try: + config = MoneroWalletConfig() + config.primary_address = primary_address + config.private_view_key = private_view_key + config.private_spend_key = private_spend_key + config.network_type = Utils.NETWORK_TYPE + MoneroWalletLight.create_wallet(config, Utils.get_daemon_lws_connection()) + raise Exception("Should have thrown error") + except Exception as e: + e_msg: str = str(e) + assert "Wallet already exists" in e_msg, e_msg + + @pytest.mark.skipif(Utils.LITE_MODE, reason="LITE_MODE enabled") + @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False and Utils.TEST_RELAYS is False, reason="TEST_NON_RELAYS and TEST_RELAYS disabled") + def test_view_only_and_offline_wallet_compatibility(self, wallet: MoneroWallet) -> None: + # create view only wallet + config: MoneroWalletConfig = MoneroWalletConfig() + config.primary_address = wallet.get_primary_address() + config.private_view_key = wallet.get_private_view_key() + view_only_wallet: MoneroWalletLight = self._create_open_wallet(config) + + config = MoneroWalletConfig() + config.primary_address = wallet.get_primary_address() + config.private_view_key = wallet.get_private_view_key() + config.private_spend_key = wallet.get_private_spend_key() + config.server = MoneroRpcConnection(Utils.OFFLINE_SERVER_URI) + config.restore_height = 0 + offline_wallet: MoneroWallet = Utils.create_wallet_full(config, False) + assert offline_wallet.is_connected_to_daemon() is False + view_only_wallet.sync() + # test tx signing with wallets + try: + tester = ViewOnlyAndOfflineWalletTester(wallet, view_only_wallet, offline_wallet) + tester.test() + finally: + self._close_wallet(view_only_wallet) + self._close_wallet(offline_wallet) + + # Can be closed + # TODO refactor with the test_close of TestMoneroWalletFullCreation + @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") + def test_close(self) -> None: + # create test wallet + config: MoneroWalletConfig = MoneroWalletConfig() + config.seed = Utils.SEED + wallet: MoneroWalletLight = self._create_wallet(config) + try: + wallet.sync() + assert wallet.get_height() > 1, "Wallet height is still 1" + # TODO lws stucks on blockchain height after a reorg + #assert wallet.is_synced(), "Wallet is not synced" + assert wallet.is_closed() is False + + # close wallet + wallet.close() + + assert wallet.is_closed() + + # attempt to interact with the wallet + try: + wallet.get_height() + except Exception as e: + WalletErrorUtils.test_wallet_is_closed_error(e) + + try: + wallet.get_seed() + except Exception as e: + WalletErrorUtils.test_wallet_is_closed_error(e) + + try: + wallet.sync() + except Exception as e: + WalletErrorUtils.test_wallet_is_closed_error(e) + + try: + wallet.start_syncing() + except Exception as e: + WalletErrorUtils.test_wallet_is_closed_error(e) + + try: + wallet.stop_syncing() + except Exception as e: + WalletErrorUtils.test_wallet_is_closed_error(e) + finally: + # close() is idempotent, so this is safe even if already closed above + self._close_wallet(wallet) + + # re-open the wallet + config = MoneroWalletConfig() + config.seed = Utils.SEED + + wallet = self._open_wallet(config) + try: + assert wallet.is_closed() is False + wallet.sync() + # TODO monero-lws get stuck when block reorgs occurs + #assert wallet.get_daemon_height() == wallet.get_height() + assert wallet.is_closed() is False + finally: + # close the wallet + self._close_wallet(wallet) + assert wallet.is_closed() + + @pytest.mark.skip(reason="can't create two wallet on the same monero-lws instance") + @override + def test_view_only_and_offline_wallets(self, wallet: MoneroWallet) -> None: + return super().test_view_only_and_offline_wallets(wallet) + + @pytest.mark.not_supported + @override + def test_get_path(self) -> None: + return super().test_get_path() + + @pytest.mark.not_supported + @override + def test_set_attributes(self, wallet: MoneroWallet) -> None: + return super().test_set_attributes(wallet) + + @pytest.mark.not_supported + @override + def test_change_password(self) -> None: + return super().test_change_password() + + @pytest.mark.not_supported + @override + def test_save_and_close(self) -> None: + return super().test_save_and_close() + + @pytest.mark.unit class TestMoneroWalletKeysCreation(KeysWalletSupport, BaseTestMoneroWalletCreation): """Keys-only wallet creation unit tests.""" diff --git a/tests/wallet/test_equality.py b/tests/wallet/test_equality.py index 2b3555ec..02835083 100644 --- a/tests/wallet/test_equality.py +++ b/tests/wallet/test_equality.py @@ -15,7 +15,7 @@ from utils import GenUtils, TestUtils, WalletEqualityUtils, WalletUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletEquality") @@ -132,6 +132,11 @@ class TestMoneroWalletRpcEquality(RpcWalletSupport, BaseTestMoneroWalletEquality """Rpc wallet equality integration tests.""" +@pytest.mark.integration +class TestMoneroWalletLightEquality(LightWalletSupport, BaseTestMoneroWalletEquality): + """Light wallet equality integration tests.""" + + @pytest.mark.unit class TestMoneroWalletKeysEquality(KeysWalletSupport, BaseTestMoneroWalletEquality): """Keys-only wallet equality unit tests.""" diff --git a/tests/wallet/test_keys_and_addresses.py b/tests/wallet/test_keys_and_addresses.py index 1ecb084b..346cdd9e 100644 --- a/tests/wallet/test_keys_and_addresses.py +++ b/tests/wallet/test_keys_and_addresses.py @@ -20,7 +20,7 @@ from utils import AssertUtils, TestUtils, TestUtils as Utils, WalletErrorUtils, WalletTestUtils, WalletUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletKeysAndAddresses") @@ -237,7 +237,7 @@ def test_get_payment_uri(self, wallet: MoneroWallet) -> None: # test with standalone payment id config1.payment_id = "03284e41c342f03603284e41c342f03603284e41c342f03603284e41c342f036" - with pytest.raises(Exception, match="Cannot make URI from supplied parameters"): + with pytest.raises(Exception, match="Standalone payment id deprecated, use integrated address instead"): wallet.get_payment_uri(config1) @@ -275,6 +275,11 @@ def test_get_public_spend_key(self, wallet: MoneroWallet) -> None: return super().test_get_public_spend_key(wallet) +@pytest.mark.integration +class TestMoneroWalletLightKeysAndAddresses(LightWalletSupport, BaseTestMoneroWalletKeysAndAddresses): + """Light wallet keys and addresses integration tests.""" + + @pytest.mark.unit class TestMoneroWalletKeysKeysAndAddresses(KeysWalletSupport, BaseTestMoneroWalletKeysAndAddresses): """Keys-only wallet keys and addresses unit tests.""" diff --git a/tests/wallet/test_listeners.py b/tests/wallet/test_listeners.py index 13a926b8..6c5d88b2 100644 --- a/tests/wallet/test_listeners.py +++ b/tests/wallet/test_listeners.py @@ -9,7 +9,7 @@ from utils import MiningUtils, TestUtils, TxWalletUtils, WalletNotificationCollector from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletListeners") @@ -95,6 +95,11 @@ class TestMoneroWalletRpcListeners(RpcWalletSupport, BaseTestMoneroWalletListene """Rpc wallet listeners integration tests.""" +@pytest.mark.integration +class TestMoneroWalletLightListeners(LightWalletSupport, BaseTestMoneroWalletListeners): + """Light wallet listeners integration tests.""" + + @pytest.mark.unit class TestMoneroWalletKeysListeners(KeysWalletSupport, BaseTestMoneroWalletListeners): """Keys-only wallet listeners unit tests.""" diff --git a/tests/wallet/test_mining.py b/tests/wallet/test_mining.py index bb62db52..2e93673c 100644 --- a/tests/wallet/test_mining.py +++ b/tests/wallet/test_mining.py @@ -8,7 +8,7 @@ from utils import TestUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletMining") @@ -36,6 +36,16 @@ class TestMoneroWalletRpcMining(RpcWalletSupport, BaseTestMoneroWalletMining): """Rpc wallet mining integration tests.""" +@pytest.mark.integration +class TestMoneroWalletLightMining(LightWalletSupport, BaseTestMoneroWalletMining): + """Light wallet mining integration tests.""" + + @pytest.mark.not_supported + @override + def test_mining(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: + return super().test_mining(daemon, wallet) + + @pytest.mark.unit class TestMoneroWalletKeysMining(KeysWalletSupport, BaseTestMoneroWalletMining): """Keys-only wallet mining unit tests.""" diff --git a/tests/wallet/test_multisig.py b/tests/wallet/test_multisig.py index 91cbc1b6..ac76f4a5 100644 --- a/tests/wallet/test_multisig.py +++ b/tests/wallet/test_multisig.py @@ -8,7 +8,7 @@ from utils import TestUtils, TestUtils as Utils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletMultisig") @@ -49,6 +49,11 @@ def test_multisig_sample(self) -> None: self._test_multisig_sample(2, 2) +@pytest.mark.integration +class TestMoneroWalletLightMultisig(LightWalletSupport, BaseTestMoneroWalletMultisig): + """Light wallet multisig integration tests.""" + + @pytest.mark.unit class TestMoneroWalletKeysMultisig(KeysWalletSupport, BaseTestMoneroWalletMultisig): """Keys-only wallet multisig unit tests.""" diff --git a/tests/wallet/test_outputs.py b/tests/wallet/test_outputs.py index 007c22fd..d8d3d5e9 100644 --- a/tests/wallet/test_outputs.py +++ b/tests/wallet/test_outputs.py @@ -18,12 +18,13 @@ MoneroTxQuery, MoneroTxWallet, MoneroWallet, + MoneroWalletLight, ) from typing_extensions import override from utils import GenUtils, OutputUtils, TestUtils, TxWalletUtils, WalletTestUtils, WalletTxsUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletOutputs") @@ -319,7 +320,11 @@ def test_freeze_outputs(self, wallet: MoneroWallet) -> None: tx_config.key_image = output.key_image.hex with pytest.raises(Exception) as exc_info: wallet.sweep_output(tx_config) - assert str(exc_info.value) == "No outputs found" + raise Exception("Should have thrown error") + + if not isinstance(wallet, MoneroWalletLight): + # TODO MoneroWalletLight does not support sweeping + assert str(exc_info.value) == "No outputs found" # try to freeze empty key image with pytest.raises(Exception) as exc_info: @@ -544,6 +549,31 @@ def test_import_key_images(self, wallet: MoneroWallet) -> None: return super().test_import_key_images(wallet) +@pytest.mark.integration +class TestMoneroWalletLightOutputs(LightWalletSupport, BaseTestMoneroWalletOutputs): + """Light wallet outputs integration tests.""" + + @pytest.mark.skip(reason="TODO monero-project: importing key images can cause erasure of incoming transfers per wallet2.cpp:11957") + @override + def test_import_key_images(self, wallet: MoneroWallet) -> None: + return super().test_import_key_images(wallet) + + @pytest.mark.not_supported + @override + def test_get_new_key_images_from_last_import(self, wallet: MoneroWallet) -> None: + return super().test_get_new_key_images_from_last_import(wallet) + + @pytest.mark.not_supported + @override + def test_import_outputs(self, wallet: MoneroWallet) -> None: + return super().test_import_outputs(wallet) + + @pytest.mark.not_supported + @override + def test_input_key_images(self, wallet: MoneroWallet) -> None: + return super().test_input_key_images(wallet) + + @pytest.mark.unit class TestMoneroWalletKeysOutputs(KeysWalletSupport, BaseTestMoneroWalletOutputs): """Keys-only wallet outputs unit tests.""" diff --git a/tests/wallet/test_pool.py b/tests/wallet/test_pool.py index 4c132ec1..d95cc395 100644 --- a/tests/wallet/test_pool.py +++ b/tests/wallet/test_pool.py @@ -8,7 +8,7 @@ from utils import TestUtils, TxWalletUtils, WalletSendUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletPool") @@ -157,6 +157,31 @@ class TestMoneroWalletRpcPool(RpcWalletSupport, BaseTestMoneroWalletPool): """Rpc wallet pool integration tests.""" +@pytest.mark.integration +class TestMoneroWalletLightPool(LightWalletSupport, BaseTestMoneroWalletPool): + """Light wallet pool integration tests.""" + + @pytest.mark.skip(reason="monero-lws does not support syncing with the pool") + @override + def test_sync_with_pool_same_accounts(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: + return super().test_sync_with_pool_same_accounts(daemon, wallet) + + @pytest.mark.skip(reason="monero-lws does not support syncing with the pool") + @override + def test_sync_with_pool_submit_and_relay(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: + return super().test_sync_with_pool_submit_and_relay(daemon, wallet) + + @pytest.mark.skip(reason="monero-lws does not support syncing with the pool") + @override + def test_sync_with_pool_relay(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: + return super().test_sync_with_pool_relay(daemon, wallet) + + @pytest.mark.skip(reason="monero-lws does not support syncing with the pool") + @override + def test_sync_with_pool_submit_and_flush(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: + return super().test_sync_with_pool_submit_and_flush(daemon, wallet) + + @pytest.mark.unit class TestMoneroWalletKeysPool(KeysWalletSupport, BaseTestMoneroWalletPool): """Keys-only wallet pool unit tests.""" diff --git a/tests/wallet/test_proofs.py b/tests/wallet/test_proofs.py index 0a268fad..7e212de1 100644 --- a/tests/wallet/test_proofs.py +++ b/tests/wallet/test_proofs.py @@ -23,7 +23,7 @@ from utils import TestUtils, TxWalletUtils, WalletErrorUtils, WalletTestUtils, WalletTxsUtils, WalletUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletProofs") @@ -404,6 +404,38 @@ class TestMoneroWalletRpcProofs(RpcWalletSupport, BaseTestMoneroWalletProofs): """Rpc wallet proofs integration tests.""" +@pytest.mark.integration +class TestMoneroWalletLightProofs(LightWalletSupport, BaseTestMoneroWalletProofs): + """Light wallet proofs integration tests.""" + + @pytest.mark.xfail(reason="monero-lws does not allow to fetch non-wallet transactions") + @override + def test_prove_unrelayed_txs(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: + return super().test_prove_unrelayed_txs(daemon, wallet) + + # monero-lws doesn't provide full on-chain data + @pytest.mark.not_supported + @override + def test_get_reserve_proof_wallet(self, wallet: MoneroWallet) -> None: + return super().test_get_reserve_proof_wallet(wallet) + + # monero-lws doesn't provide full on-chain data + @pytest.mark.not_supported + @override + def test_get_reserve_proof_account(self, wallet: MoneroWallet) -> None: + return super().test_get_reserve_proof_account(wallet) + + @pytest.mark.not_supported + @override + def test_check_spend_proof(self, wallet: MoneroWallet) -> None: + return super().test_check_spend_proof(wallet) + + @pytest.mark.not_supported + @override + def test_check_tx_proof(self, wallet: MoneroWallet) -> None: + return super().test_check_tx_proof(wallet) + + @pytest.mark.unit class TestMoneroWalletKeysProofs(KeysWalletSupport, BaseTestMoneroWalletProofs): """Keys-only wallet proofs unit tests.""" diff --git a/tests/wallet/test_send.py b/tests/wallet/test_send.py index b94d1015..ad7264a6 100644 --- a/tests/wallet/test_send.py +++ b/tests/wallet/test_send.py @@ -16,12 +16,13 @@ MoneroUtils, MoneroWallet, MoneroWalletConfig, + MoneroWalletLight, ) from typing_extensions import override -from utils import TestUtils, TxWalletUtils, WalletSendUtils +from utils import BlockchainUtils, TestUtils, TxWalletUtils, WalletSendUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletSend") @@ -127,6 +128,11 @@ def test_send_to_external(self, wallet: MoneroWallet) -> None: txs: list[MoneroTxWallet] = wallet.get_txs(tx_query) assert len(txs) > 0 + if isinstance(recipient, MoneroWalletLight): + # lws doesn't report unconfirmed txs, must mine some blocks + BlockchainUtils.wait_for_blocks(5) + TestUtils.wait_for_lws_scan(recipient) + # test recipient balance after recipient.sync() assert amount == recipient.get_balance() @@ -222,6 +228,11 @@ class TestMoneroWalletRpcSend(RpcWalletSupport, BaseTestMoneroWalletSend): """Rpc wallet send integration tests.""" +@pytest.mark.integration +class TestMoneroWalletLightSend(LightWalletSupport, BaseTestMoneroWalletSend): + """Light wallet send integration tests.""" + + @pytest.mark.unit class TestMoneroWalletKeysSend(KeysWalletSupport, BaseTestMoneroWalletSend): """Keys-only wallet send unit tests.""" diff --git a/tests/wallet/test_sweep.py b/tests/wallet/test_sweep.py index 29c551b7..2762672d 100644 --- a/tests/wallet/test_sweep.py +++ b/tests/wallet/test_sweep.py @@ -17,7 +17,7 @@ from utils import IntegrationTestUtils, OutputUtils, TestUtils, TxContext, TxWalletUtils, WalletSendUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletSweep") @@ -337,6 +337,36 @@ class TestMoneroWalletRpcSweep(RpcWalletSupport, BaseTestMoneroWalletSweep): """Rpc wallet sweep integration tests.""" +@pytest.mark.integration +class TestMoneroWalletLightSweep(LightWalletSupport, BaseTestMoneroWalletSweep): + """Light wallet sweep integration tests.""" + + @pytest.mark.not_supported + @override + def test_sweep_dust(self, wallet: MoneroWallet) -> None: + return super().test_sweep_dust(wallet) + + @pytest.mark.not_supported + @override + def test_sweep_dust_no_relay(self, wallet: MoneroWallet) -> None: + return super().test_sweep_dust_no_relay(wallet) + + @pytest.mark.skipif(TestUtils.TEST_RELAYS is False, reason="TEST_RELAYS disabled") + @override + def test_sweep_outputs(self, wallet: MoneroWallet) -> None: + return super().test_sweep_outputs(wallet) + + @pytest.mark.skipif(TestUtils.TEST_RESETS is False, reason="TEST_RESETS disabled") + @override + def test_sweep_wallet_by_accounts(self, wallet: MoneroWallet) -> None: + return super().test_sweep_wallet_by_accounts(wallet) + + @pytest.mark.skipif(TestUtils.TEST_RESETS is False, reason="TEST_RESETS disabled") + @override + def test_sweep_wallet_by_subaddresses(self, wallet: MoneroWallet) -> None: + return super().test_sweep_wallet_by_subaddresses(wallet) + + @pytest.mark.unit class TestMoneroWalletKeysSweep(KeysWalletSupport, BaseTestMoneroWalletSweep): """Keys-only wallet sweep unit tests.""" diff --git a/tests/wallet/test_sync.py b/tests/wallet/test_sync.py index 0aa01dd5..e65eecb0 100644 --- a/tests/wallet/test_sync.py +++ b/tests/wallet/test_sync.py @@ -15,6 +15,7 @@ MoneroWallet, MoneroWalletConfig, MoneroWalletFull, + MoneroWalletLight, MoneroWalletRpc, ) from typing_extensions import override @@ -23,6 +24,7 @@ AssertUtils, GenUtils, MiningUtils, + MoneroDaemonLws, SyncProgressTester, SyncSeedTester, TestUtils, @@ -33,7 +35,7 @@ WalletTxsUtils, ) from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletSync") @@ -409,6 +411,89 @@ def test_get_height_by_date(self, wallet: MoneroWallet) -> None: return super().test_get_height_by_date(wallet) +@pytest.mark.integration +class TestMoneroWalletLightSync(LightWalletSupport, BaseTestMoneroWalletSync): + """Light wallet sync integration tests.""" + + # Can sync a wallet with a randomly generated seed + @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") + def test_sync_random(self, daemon: MoneroDaemonRpc, daemon_lws: MoneroDaemonLws) -> None: + assert daemon.is_connected(), "Not connected to daemon" + + # wait for lws's own scan progress to catch up to the daemon before creating the wallet, + # otherwise the new account's start_height is assigned from lws's lagging scan height + # instead of the daemon's live height. lws's scanned/start height is the 0-indexed height + # of the last processed block, one less than daemon.get_height()'s block-count convention + daemon_lws.wait_for_scan_height(Utils.ADDRESS, Utils.PRIVATE_VIEW_KEY, daemon.get_height() - 1) + # and for the height that lws has of the daemon, which it updates by itself and can be one block behind the scan + daemon_lws.wait_for_daemon_height(daemon.get_height()) + + # create test wallet + wallet: MoneroWalletLight = self._create_wallet(MoneroWalletConfig(), False) + restore_height: int = daemon.get_height() + + # test wallet's height before syncing + assert restore_height == wallet.get_daemon_height() + assert wallet.is_connected_to_daemon() + assert wallet.is_synced() is False + assert wallet.get_height() == 1 + addr_info = daemon_lws.get_address_info(wallet.get_primary_address(), wallet.get_private_view_key()) + logger.info(f"wallet address info: {addr_info}") + assert wallet.get_restore_height() == restore_height + assert wallet.get_daemon_height() == daemon.get_height() + + # sync the wallet + progress_tester: SyncProgressTester = SyncProgressTester(wallet, wallet.get_restore_height(), wallet.get_daemon_height()) + result: MoneroSyncResult = wallet.sync(progress_tester) + logger.debug(f"Sync result: {result.serialize}") + progress_tester.on_done(wallet.get_daemon_height()) + + # test result after syncing + wallet_gt: MoneroWalletFull = Utils.create_wallet_ground_truth(Utils.NETWORK_TYPE, wallet.get_seed(), None, restore_height) + wallet_gt.sync() + + try: + assert wallet.is_connected_to_daemon() + assert wallet.is_synced() + assert result.num_blocks_fetched == 0 + assert result.received_money is False + assert wallet.get_height() == daemon.get_height() + + # sync the wallet with default params + wallet.sync() + assert wallet.is_synced() + assert wallet.get_height() == daemon.get_height() + + # compare wallet to ground truth + WalletEqualityUtils.test_wallet_equality_on_chain(wallet_gt, wallet) + finally: + wallet_gt.close(True) + wallet.close() + + # attempt to sync unconnected wallet + config: MoneroWalletConfig = MoneroWalletConfig() + config.network_type = Utils.NETWORK_TYPE + wallet = MoneroWalletLight.create_wallet(config, MoneroRpcConnection(Utils.OFFLINE_SERVER_URI)) + try: + wallet.sync() + raise Exception("Should have thrown exception") + except Exception as e: + e_msg: str = str(e) + assert e_msg == "Wallet is not connected to daemon", e_msg + finally: + wallet.close() + + @pytest.mark.not_supported + @override + def test_get_height_by_date(self, wallet: MoneroWallet) -> None: + return super().test_get_height_by_date(wallet) + + @pytest.mark.not_supported + @override + def test_rescan_spent(self, wallet: MoneroWallet) -> None: + return super().test_rescan_spent(wallet) + + @pytest.mark.unit class TestMoneroWalletKeysSync(KeysWalletSupport, BaseTestMoneroWalletSync): """Keys-only wallet sync unit tests.""" diff --git a/tests/wallet/test_transfers.py b/tests/wallet/test_transfers.py index 2b30bc06..591f3c4a 100644 --- a/tests/wallet/test_transfers.py +++ b/tests/wallet/test_transfers.py @@ -16,7 +16,7 @@ from utils import TestUtils, WalletTransfersUtils, WalletTxsUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletTransfers") @@ -293,6 +293,11 @@ class TestMoneroWalletRpcTransfers(RpcWalletSupport, BaseTestMoneroWalletTransfe """Rpc wallet transfers integration tests.""" +@pytest.mark.integration +class TestMoneroWalletLightTransfers(LightWalletSupport, BaseTestMoneroWalletTransfers): + """Light wallet transfers integration tests.""" + + @pytest.mark.unit class TestMoneroWalletKeysTransfers(KeysWalletSupport, BaseTestMoneroWalletTransfers): """Keys-only wallet transfers unit tests.""" diff --git a/tests/wallet/test_txs.py b/tests/wallet/test_txs.py index cdca28a2..d7dc9d8f 100644 --- a/tests/wallet/test_txs.py +++ b/tests/wallet/test_txs.py @@ -27,7 +27,7 @@ WalletTxsUtils, ) from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletTxs") @@ -595,6 +595,30 @@ class TestMoneroWalletRpcTxs(RpcWalletSupport, BaseTestMoneroWalletTxs): """Rpc wallet txs integration tests.""" +@pytest.mark.integration +class TestMoneroWalletLightTxs(LightWalletSupport, BaseTestMoneroWalletTxs): + """Light wallet txs integration tests.""" + + # monero-lws decrypts the encrypted payment id of a tx with the view key of the account, also for the txs that the account sent, and + # the result is random bytes: the light wallet does not use it for the outgoing txs, as a wallet that is restored does not report + # a payment id for them. The txs with a payment id of the other wallets are the ones that they sent to an integrated address, which + # have the id from when they were created. The light wallet does not keep it yet. + @pytest.mark.xfail(reason="the light wallet does not keep the payment id of the txs that it creates, lws reports a wrong one for them") + @override + def test_get_txs_with_payment_ids(self, wallet: MoneroWallet) -> None: + return super().test_get_txs_with_payment_ids(wallet) + + @pytest.mark.not_supported + @override + def test_set_tx_note(self, wallet: MoneroWallet) -> None: + return super().test_set_tx_note(wallet) + + @pytest.mark.not_supported + @override + def test_set_tx_notes(self, wallet: MoneroWallet) -> None: + return super().test_set_tx_notes(wallet) + + @pytest.mark.unit class TestMoneroWalletKeysTxs(KeysWalletSupport, BaseTestMoneroWalletTxs): """Keys-only wallet txs unit tests.""" diff --git a/tests/wallet/test_update_locked.py b/tests/wallet/test_update_locked.py index 28050e99..28803573 100644 --- a/tests/wallet/test_update_locked.py +++ b/tests/wallet/test_update_locked.py @@ -8,7 +8,7 @@ from utils import TestUtils, TxWalletUtils, WalletSendUtils from .base_test_monero_wallet import BaseTestMoneroWallet -from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport +from .wallet_support import FullWalletSupport, LightWalletSupport, RpcWalletSupport, KeysWalletSupport logger: logging.Logger = logging.getLogger("TestMoneroWalletUpdateLocked") @@ -84,6 +84,11 @@ class TestMoneroWalletRpcUpdateLocked(RpcWalletSupport, BaseTestMoneroWalletUpda """Rpc wallet update locked integration tests.""" +@pytest.mark.integration +class TestMoneroWalletLightUpdateLocked(LightWalletSupport, BaseTestMoneroWalletUpdateLocked): + """Light wallet update locked integration tests.""" + + @pytest.mark.unit class TestMoneroWalletKeysUpdateLocked(KeysWalletSupport, BaseTestMoneroWalletUpdateLocked): """Keys-only wallet update locked unit tests.""" diff --git a/tests/wallet/wallet_support.py b/tests/wallet/wallet_support.py index 8264d559..be407e12 100644 --- a/tests/wallet/wallet_support.py +++ b/tests/wallet/wallet_support.py @@ -9,15 +9,18 @@ MoneroDaemon, MoneroDaemonRpc, MoneroNetworkType, + MoneroRpcConnection, + MoneroTxQuery, MoneroWallet, MoneroWalletConfig, MoneroWalletFull, MoneroWalletKeys, + MoneroWalletLight, MoneroWalletRpc, ) from typing_extensions import override -from utils import StringUtils, TestUtils as Utils, WalletType +from utils import BlockchainUtils, MoneroDaemonLws, StringUtils, TestUtils as Utils, WalletType from .base_test_monero_wallet import BaseTestMoneroWallet logger: logging.Logger = logging.getLogger("TestMoneroWallet") @@ -270,3 +273,118 @@ def _get_test_accounts(self, wallet: MoneroWallet, include_subaddresses: bool = accounts.append(account) return accounts + + +class LightWalletSupport(BaseTestMoneroWallet): + """What the light wallet tests do differently from the common ones.""" + + @classmethod + @override + def get_wallet_type(cls) -> WalletType: + return WalletType.LIGHT + + def _create_open_wallet(self, config: Optional[MoneroWalletConfig], create: bool = True, start_syncing: bool = True) -> MoneroWalletLight: + """Create or open a light wallet.""" + # assign defaults + if config is None: + config = MoneroWalletConfig() + if config.network_type is None: + config.network_type = Utils.NETWORK_TYPE + + wallet: MoneroWalletLight + rpc: MoneroRpcConnection = Utils.get_daemon_lws_connection() + logger.info(f"create: {create}, config: {config.serialize()}") + if create and not MoneroWalletLight.wallet_exists(config, rpc): + # create wallet + logger.info("creating wallet") + wallet = MoneroWalletLight.create_wallet(config, rpc) + logger.info(f"Created wallet {wallet.get_primary_address()}") + else: + # open wallet + if config.restore_height is not None: + # A restore height asks lws to scan the account again from there (an import request), and the account of an existing + # wallet is, for the test of a scan, the account that all the tests share: lws forgets what it scanned of it, and it does not + # start again until a block arrives. On regtest at a high difficulty that is minutes, and the tests that follow see a wallet + # without txs. + logger.info(f"opening wallet without its restore height {config.restore_height}, lws would scan it again") + config = config.copy() + config.restore_height = None + logger.info("opening wallet") + wallet = MoneroWalletLight.open_wallet(config, rpc) + + if start_syncing and wallet.is_connected_to_daemon(): + wallet.sync() + wallet.start_syncing(Utils.SYNC_PERIOD_IN_MS) + + # TODO ensure wallet is synced with the daemon + + return wallet + + _unconfirmable_hashes: set[str] = set() + """Hashes of the txs that stayed pending after some blocks, such as the ones that were created and never relayed: the tests do not wait for them.""" + + def _wait_for_pending_txs(self, wallet: MoneroWalletLight, max_blocks: int = 5) -> None: + """Mine until lws and the light wallet report as confirmed the txs that the wallet created. + + lws does not report the txs of the pool, so the light wallet keeps the ones that it creates as pending until lws reports them in a + block. A test that sends and does not mine leaves them, and the next ones would see unconfirmed txs, with a received timestamp, and + outputs that lws does not know yet. The blocks are generated at once on regtest: lws needs time to scan them, and then the wallet to sync. + + :param MoneroWalletLight wallet: light wallet to wait for. + :param int max_blocks: number of blocks to mine at most, the txs that are still pending after them are not waited for again. + """ + query: MoneroTxQuery = MoneroTxQuery() + query.is_confirmed = False + pending: set[str] = set() + for attempt in range(max_blocks + 1): + pending = {tx.hash for tx in wallet.get_txs(query) if tx.hash is not None} - self._unconfirmable_hashes + if not pending: + return + if attempt == max_blocks: + break + logger.debug(f"Mining a block, the light wallet has {len(pending)} pending tx(s): {pending}") + BlockchainUtils.wait_for_blocks(1) + Utils.wait_for_lws_scan(wallet) + wallet.sync() + + logger.warning(f"The light wallet has pending txs after {max_blocks} blocks, they are not waited for again: {pending}") + self._unconfirmable_hashes.update(pending) + + @override + def before_each(self, request: pytest.FixtureRequest) -> None: + super().before_each(request) + self._wait_for_pending_txs(self.get_test_wallet()) + + @pytest.fixture(scope="class") + @classmethod + def daemon_lws(cls) -> MoneroDaemonLws: + """Test monero-lws daemon instance.""" + return Utils.get_daemon_lws() + + @pytest.fixture(scope="class") + @classmethod + @override + def wallet(cls) -> MoneroWalletLight: + """Test light wallet instance.""" + return cls.get_test_wallet() + + @classmethod + @override + def get_test_wallet(cls) -> MoneroWalletLight: + return super().get_test_wallet() # type: ignore + + @override + def _open_wallet(self, config: Optional[MoneroWalletConfig], start_syncing: bool = True) -> MoneroWalletLight: + return self._create_open_wallet(config, False, start_syncing) + + @override + def _create_wallet(self, config: Optional[MoneroWalletConfig], start_syncing: bool = True) -> MoneroWalletLight: + return self._create_open_wallet(config, True, start_syncing) + + @override + def _close_wallet(self, wallet: MoneroWallet, save: bool = False) -> None: + wallet.close(save) + + @override + def _get_seed_languages(self) -> list[str]: + return MoneroWalletLight.get_seed_languages()