diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ee090c5b..22c06dd9 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 9d4c399a..1b34efba 100644 --- a/README.md +++ b/README.md @@ -269,7 +269,7 @@ For example: `export LD_PRELOAD=/path/to/libjemalloc.so.2` then run your app. It ``` 3. Start the regtest network of the integration tests in docker (two daemons and three wallet RPC servers), run them, and stop it: ```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 pytest -m integration docker compose -f tests/docker-compose.yml down -v ``` diff --git a/bin/setup_test_environment.sh b/bin/setup_test_environment.sh index 5bf3d5e1..baa473f7 100755 --- a/bin/setup_test_environment.sh +++ b/bin/setup_test_environment.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash # start docker containers -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 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/docs/mkdocs/docs/en/api/architecture.md b/docs/mkdocs/docs/en/api/architecture.md index 4c4b6bf6..d39afe1e 100644 --- a/docs/mkdocs/docs/en/api/architecture.md +++ b/docs/mkdocs/docs/en/api/architecture.md @@ -26,7 +26,7 @@ to, or a wallet server for the RPC wallet. ## The daemon and the wallets -The daemon has one implementation, the wallet has three. Each wallet implements the same interface, [`MoneroWallet`][monero.MoneroWallet], +The daemon has one implementation, the wallet has four. Each wallet implements the same interface, [`MoneroWallet`][monero.MoneroWallet], and code written for it works with any of them, within what each one supports ([Wallet Support](wallet_support.md)). | Class | What it is made on | What it needs | @@ -34,6 +34,7 @@ and code written for it works with any of them, within what each one supports ([ | [`MoneroDaemonRpc`][monero.MoneroDaemonRpc] | a client of the RPC of `monerod`, over HTTP(S) | a daemon | | [`MoneroWalletFull`][monero.MoneroWalletFull] | `wallet2` in the process: a wallet file on disk, which syncs with a daemon | a daemon to sync with | | [`MoneroWalletRpc`][monero.MoneroWalletRpc] | a client of `monero-wallet-rpc`, over HTTP(S): the wallet files are on the server | a wallet server, connected to a daemon | +| [`MoneroWalletLight`][monero.MoneroWalletLight] | a client of [`monero-lws`](https://github.com/vtnerd/monero-lws), which scans the chain for it: the wallet keeps the spend key and signs | a `monero-lws` server | | [`MoneroWalletKeys`][monero.MoneroWalletKeys] | the keys of a wallet alone, in memory: seed, keys, addresses | nothing, it works offline | The connection to a server is a [`MoneroRpcConnection`][monero.MoneroRpcConnection]: its URI, its credentials, a proxy, and the time to diff --git a/docs/mkdocs/docs/en/api/wallet.md b/docs/mkdocs/docs/en/api/wallet.md index 082f5ce1..6f4b5f7a 100644 --- a/docs/mkdocs/docs/en/api/wallet.md +++ b/docs/mkdocs/docs/en/api/wallet.md @@ -3,9 +3,9 @@ title: Wallet Interface --- # Wallet Interface -[`MoneroWallet`][monero.MoneroWallet] is the interface that the three wallets implement: code written for it works with a -[full](wallet_full.md), an [RPC](wallet_rpc.md) or a [keys-only](wallet_keys.md) wallet, within what each one supports -([Wallet Support](wallet_support.md), which groups its methods by topic). +[`MoneroWallet`][monero.MoneroWallet] is the interface that the four wallets implement: code written for it works with a +[full](wallet_full.md), an [RPC](wallet_rpc.md), a [light](wallet_light.md) or a [keys-only](wallet_keys.md) wallet, within what each +one supports ([Wallet Support](wallet_support.md), which groups its methods by topic). | Topic | Methods | | --- | --- | diff --git a/docs/mkdocs/docs/en/api/wallet_light.md b/docs/mkdocs/docs/en/api/wallet_light.md new file mode 100644 index 00000000..85050b68 --- /dev/null +++ b/docs/mkdocs/docs/en/api/wallet_light.md @@ -0,0 +1,66 @@ +--- +title: Light Wallet +--- +# Light Wallet + +A light wallet is a client of [`monero-lws`](https://github.com/vtnerd/monero-lws), the light wallet server: the server scans the chain +for the wallet, which does not download blocks. The wallet keeps the keys in memory, builds and signs its transactions, and sends them +through the server. + +!!! warning "The server learns the private view key" + + The wallet gives the server its primary address and its private view key: the server sees what the wallet receives. The spend key + stays in the wallet, so the server can not spend the funds. Use a server that you run, or one that you trust with that. + +## Creating and opening + +The server is a [`MoneroRpcConnection`][monero.MoneroRpcConnection], given when the wallet is made: + +```python +from monero import MoneroNetworkType, MoneroRpcConnection, MoneroWalletConfig, MoneroWalletLight + +server = MoneroRpcConnection("http://localhost:8443") + +# a new wallet, whose account is registered on the server +config = MoneroWalletConfig() +config.network_type = MoneroNetworkType.STAGENET +wallet = MoneroWalletLight.create_wallet(config, server) +seed = wallet.get_seed() +wallet.close() + +# the same wallet later, from its seed +config.seed = seed +wallet = MoneroWalletLight.open_wallet(config, server) + +wallet.sync() # fetch what the server has scanned +wallet.start_syncing(5000) # and keep fetching it, every 5 seconds +print(wallet.get_balance()) +wallet.close() +``` + +A light wallet has no file: it is opened from its `seed`, or from its `primary_address` and `private_view_key`, with the +`private_spend_key` or without it, which makes it view-only. [`create_wallet`][monero.MoneroWalletLight.create_wallet] fails when the +server already has the account, [`wallet_exists`][monero.MoneroWalletLight.wallet_exists] tells whether it has it. + +A server may wait for its administrator to accept a new account before it scans it. + +## Accounts, subaddresses and restore height + +- The server reports the transactions of the accounts and subaddresses that are registered on it. + [`create_account`][monero.MoneroWallet.create_account] and [`create_subaddress`][monero.MoneroWallet.create_subaddress] register them, + without a label. To restore a wallet that already used more of them, set `account_lookahead` and `subaddress_lookahead` in the config, + both together. +- `restore_height` asks the server to scan the account again from that height. A server may ask for a payment to do it: the wallet then + raises an error with the address and the amount. + +## What it does differently + +- The server does not report the transactions of the pool: the wallet sees a transaction that it receives after its first confirmation. + It reports the ones that it sends as unconfirmed on its own, and forgets them when it is closed. +- It does not keep the payment id of the transactions that it creates. +- Its listeners are called after a sync, with what the server has scanned since the one before. +- Nothing is saved: [`close`][monero.MoneroWallet.close] refuses `save=True`. +- No attributes, labels, account tags, notes or address book, no transaction, spend or reserve proofs, no multisig and no mining + ([Wallet Support](wallet_support.md)). + +::: monero.MoneroWalletLight diff --git a/docs/mkdocs/docs/en/api/wallet_support.md b/docs/mkdocs/docs/en/api/wallet_support.md index 39d60c81..768235cf 100644 --- a/docs/mkdocs/docs/en/api/wallet_support.md +++ b/docs/mkdocs/docs/en/api/wallet_support.md @@ -4,8 +4,9 @@ title: Wallet Support # Wallet Support Which methods of the [wallet interface][monero.MoneroWallet] each wallet implements: the [full wallet][monero.MoneroWalletFull], the -[RPC wallet][monero.MoneroWalletRpc] and the [keys-only wallet][monero.MoneroWalletKeys]. A method that a wallet does not implement -raises an error that says it is not supported. The unit tests call every method on each wallet and fail when this table says otherwise. +[RPC wallet][monero.MoneroWalletRpc], the [light wallet][monero.MoneroWalletLight] and the [keys-only wallet][monero.MoneroWalletKeys]. +A method that a wallet does not implement raises an error that says it is not supported. The unit tests call every method on each wallet +and fail when this table says otherwise. {% set icons = { "supported": ':material-check:{ .wallet-support--supported title="Supported" }', @@ -25,9 +26,9 @@ raises an error that says it is not supported. The unit tests call every method {% for section, methods in wallet_support.items() %} ## {{ section }} -| Method | Full | RPC | Keys-only | -| --- | :---: | :---: | :---: | +| Method | Full | RPC | Light | Keys-only | +| --- | :---: | :---: | :---: | :---: | {% for method, entry in methods.items() -%} -| [`{{ method }}`][monero.MoneroWallet.{{ method }}]{% if entry.get("note") %}
{{ entry["note"] }}{% endif %} | {{ icons[entry["full"]] }} | {{ icons[entry["rpc"]] }} | {{ icons[entry["keys"]] }} | +| [`{{ method }}`][monero.MoneroWallet.{{ method }}]{% if entry.get("note") %}
{{ entry["note"] }}{% endif %} | {{ icons[entry["full"]] }} | {{ icons[entry["rpc"]] }} | {{ icons[entry["light"]] }} | {{ icons[entry["keys"]] }} | {% endfor %} {% endfor %} diff --git a/docs/mkdocs/docs/en/index.md b/docs/mkdocs/docs/en/index.md index 13e615e4..43c17260 100644 --- a/docs/mkdocs/docs/en/index.md +++ b/docs/mkdocs/docs/en/index.md @@ -19,7 +19,7 @@ Contributions can be made via issues and pull requests on GitHub, or communicate ## What it has - **Daemon**: `MoneroDaemonRpc`, a client of `monerod`: blocks, transactions, the pool, peers, mining, and listeners of the new blocks -- **Wallets**: three implementations of `MoneroWallet`: `MoneroWalletFull`, a wallet file that syncs with a daemon through the wallet of monero-project; `MoneroWalletRpc`, a client of `monero-wallet-rpc`; `MoneroWalletKeys`, the keys and the addresses of a wallet, offline. [Wallet Support](api/wallet_support.md) says what each of them does +- **Wallets**: four implementations of `MoneroWallet`: `MoneroWalletFull`, a wallet file that syncs with a daemon through the wallet of monero-project; `MoneroWalletRpc`, a client of `monero-wallet-rpc`; `MoneroWalletLight`, a client of `monero-lws`, which scans the chain for it; `MoneroWalletKeys`, the keys and the addresses of a wallet, offline. [Wallet Support](api/wallet_support.md) says what each of them does - **Data model**: blocks, transactions, transfers, outputs and the queries that filter them, all serializable to JSON - **Utilities**: `MoneroUtils`: the validation of addresses, keys and seeds, amounts in atomic units, payment uris - **Platforms**: deb packages for Debian and Ubuntu on amd64, arm64 and armhf, and wheels for macOS and Windows, with every [release](https://github.com/libmonero/monero-python/releases) diff --git a/docs/mkdocs/mkdocs.yml b/docs/mkdocs/mkdocs.yml index 0a18f416..f1a45af7 100644 --- a/docs/mkdocs/mkdocs.yml +++ b/docs/mkdocs/mkdocs.yml @@ -49,6 +49,7 @@ nav: - Interface: 'api/wallet.md' - RPC: 'api/wallet_rpc.md' - Full: 'api/wallet_full.md' + - Light: 'api/wallet_light.md' - Keys-Only: 'api/wallet_keys.md' - Support: 'api/wallet_support.md' - Utils: diff --git a/external/monero-cpp b/external/monero-cpp index d04eaa63..e1cf2f3d 160000 --- a/external/monero-cpp +++ b/external/monero-cpp @@ -1 +1 @@ -Subproject commit d04eaa63e53a4d9977902ce8ff741e45df7188a5 +Subproject commit e1cf2f3dbd6a1200717e8cc34e109675b2a1169c diff --git a/pytest.ini b/pytest.ini index ac322543..f38251af 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 strict xfail (xfails while it raises, fails once the feature lands, so that the marker is removed) diff --git a/src/cpp/py_monero_types.h b/src/cpp/py_monero_types.h index 92ef271f..50420bb5 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 9612f6da..01222c51 100644 --- a/src/cpp/wallet/py_monero_wallet_bindings.cpp +++ b/src/cpp/wallet/py_monero_wallet_bindings.cpp @@ -1143,6 +1143,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/monero/__init__.pyi b/src/monero/__init__.pyi index 5621550d..da29385d 100644 --- a/src/monero/__init__.pyi +++ b/src/monero/__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/monero/monero_wallet_light.pyi b/src/monero/monero_wallet_light.pyi new file mode 100644 index 00000000..7f11c9c5 --- /dev/null +++ b/src/monero/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 str primary_address: The primary address of the wallet. + :param str private_view_key: The private view key of the wallet. + :param MoneroRpcConnection rpc: The RPC connection to the Monero server. + :returns bool: `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 MoneroWalletConfig config: The wallet configuration. + :param MoneroRpcConnection rpc: The RPC connection to the Monero server. + :returns bool: `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 MoneroWalletConfig config: The configuration for opening the wallet. + :param MoneroRpcConnection rpc: The RPC connection to the Monero server. + :returns MoneroWalletLight: an instance of MoneroWalletLight. + """ + ... + + @staticmethod + def create_wallet(config: MoneroWalletConfig, rpc: MoneroRpcConnection) -> MoneroWalletLight: + """ + Create a new light wallet with the given configuration. + + :param MoneroWalletConfig config: The configuration for creating the wallet. + :param MoneroRpcConnection rpc: The RPC connection to the Monero server. + :returns MoneroWalletLight: 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 99200133..7d6d1e0a 100644 --- a/tests/common/test_rpc_connection.py +++ b/tests/common/test_rpc_connection.py @@ -75,6 +75,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 @@ -162,6 +168,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 440c3f8a..f7f3f748 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -58,6 +58,7 @@ from .wallet_test_utils import WalletTestUtils from .wallet_error_utils import WalletErrorUtils from .wallet_calls import WalletCall, WALLET_CALLS, NOT_SUPPORTED_HINTS +from .monero_daemon_lws import MoneroDaemonLws __all__ = [ 'WalletUtils', @@ -113,5 +114,6 @@ 'DaemonNotificationCollector', 'FakeDaemonRpcServer', 'FakeWalletRpcServer', - 'FakeRawRpcServer' + 'FakeRawRpcServer', + 'MoneroDaemonLws' ] diff --git a/tests/utils/from_multiple_tx_sender.py b/tests/utils/from_multiple_tx_sender.py index ee55466a..9c9ae2a8 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 296254e9..ab1461e6 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 3f9da93c..daa09f46 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 f9098b28..d4bd084d 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') @@ -305,6 +319,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. @@ -337,7 +376,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 if wallet.is_closed(): @@ -349,6 +388,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. @@ -568,6 +623,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. @@ -577,6 +659,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 f296d7fa..2fb7145b 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_notifications_tester.py b/tests/utils/wallet_notifications_tester.py index 83b6a0d7..f9ecb4a9 100644 --- a/tests/utils/wallet_notifications_tester.py +++ b/tests/utils/wallet_notifications_tester.py @@ -5,8 +5,8 @@ from typing import Callable, Optional from monero import ( - MoneroDaemonRpc, MoneroDestination, MoneroOutputQuery, MoneroOutputWallet, - MoneroTxConfig, MoneroTxQuery, MoneroTxWallet, MoneroWallet, MoneroWalletRpc + MoneroDaemonRpc, MoneroDestination, MoneroOutputQuery, MoneroOutputWallet, MoneroTx, + MoneroTxConfig, MoneroTxQuery, MoneroTxWallet, MoneroWallet, MoneroWalletLight, MoneroWalletRpc ) from .context import TxContext @@ -86,6 +86,12 @@ def same_wallet(self) -> bool: """`True` if the funds are sent to the same wallet.""" return self.sender is self.receiver + @property + def receiver_sees_unconfirmed(self) -> bool: + """`False` if the receiver sees the tx only once it is confirmed: a light wallet that did not create it, as monero-lws does not + report the txs of the pool.""" + return self.same_wallet or not isinstance(self.receiver, MoneroWalletLight) + def test(self, test_name: str) -> None: """Run the test and fail if an issue is an error. @@ -257,39 +263,12 @@ def _run(self) -> None: # test receiver after a sync period GenUtils.wait_for(self._remaining_sync_period_ms(start_time)) start_time = monotonic() # reset timer - receiver_tx: MoneroTxWallet | None = receiver.get_tx(tx_hash) - assert receiver_tx is not None - if sender_tx.get_outgoing_amount() != receiver_tx.get_incoming_amount() and self.same_account: - issues.append( - "WARNING: sender tx outgoing amount != receiver tx incoming amount when sent to same account " - f"({sender_tx.get_outgoing_amount()} != {receiver_tx.get_incoming_amount()})" - ) - receiver_fee: int = 0 - if self.same_wallet: - assert receiver_tx.fee is not None - receiver_fee = receiver_tx.fee - receiver_balance: int = receiver.get_balance() - expected_receiver_balance: int = ( - receiver_balance_before + receiver_tx.get_incoming_amount() - receiver_tx.get_outgoing_amount() - receiver_fee - ) - if receiver_balance != expected_receiver_balance: - level: str = "WARNING" if self.same_account else "ERROR" - suffix: str = " when sent to same account" if self.same_account else "" - issues.append( - f"{level}: after sending, receiver balance != balance before + incoming amount - outgoing amount - tx fee{suffix} " - f"({receiver_balance} != {receiver_balance_before} + {receiver_tx.get_incoming_amount()} - " - f"{receiver_tx.get_outgoing_amount()} - {receiver_fee})" + if self.receiver_sees_unconfirmed: + self._test_unconfirmed_receiver( + sender_tx, tx_hash, expected_outputs, receiver_balance_before, receiver_unlocked_balance_before ) - if not self.same_wallet and receiver.get_unlocked_balance() != receiver_unlocked_balance_before: - issues.append("ERROR: receiver unlocked balance should not have changed after sending") - self._test_last_balance_notification( - receiver, receiver_collector, "receiver", "after funds received", "did not notify balance change when funds received" - ) - if len(receiver_collector.get_outputs_received(tx_outputs_query)) == 0: - issues.append("ERROR: receiver did not announce unconfirmed received output") - else: - for output in self.get_missing_outputs(expected_outputs, receiver_collector.get_outputs_received(tx_outputs_query), True): - issues.append(f"ERROR: receiver did not announce received output for {self._output_str(output)}") + + self._confirm_for_light_wallets(tx_hash) # mine until the funds unlock if not MiningUtils.is_mining(): @@ -313,15 +292,21 @@ def _run(self) -> None: # get updated tx tx: MoneroTxWallet | None = receiver.get_tx(tx_hash) - assert tx is not None + if tx is None: + assert not self.receiver_sees_unconfirmed, f"Receiver does not report tx {tx_hash}" + + # the receiver has until the funds unlock to report the confirmed tx + if self._get_num_confirmations(tx_hash) >= self.NUM_BLOCKS_LOCKED: + issues.append("ERROR: receiver did not report the confirmed tx before its funds unlocked") + break # break if tx fails - if tx.is_failed: + elif tx.is_failed: issues.append("ERROR: tx failed in tx pool") break # test confirm notifications - if tx.is_confirmed: + elif tx.is_confirmed: confirm_height = tx.get_height() assert confirm_height is not None expected_unlock_height = max(confirm_height + self.NUM_BLOCKS_LOCKED, expected_unlock_height) # exact unlock time known @@ -353,6 +338,56 @@ def _run(self) -> None: #region Checks + def _test_unconfirmed_receiver( + self, sender_tx: MoneroTxWallet, tx_hash: str, expected_outputs: list[MoneroOutputWallet], + receiver_balance_before: int, receiver_unlocked_balance_before: int + ) -> None: + """Test the tx, the balances and the notifications of the receiver before the tx is confirmed. + + :param MoneroTxWallet sender_tx: tx of the sender. + :param str tx_hash: hash of the tx. + :param list[MoneroOutputWallet] expected_outputs: outputs that the tx sends. + :param int receiver_balance_before: balance of the receiver before sending. + :param int receiver_unlocked_balance_before: unlocked balance of the receiver before sending. + """ + receiver: MoneroWallet = self.receiver + receiver_collector: WalletNotificationCollector = self._receiver_collector + issues: list[str] = self.issues + tx_outputs_query: MoneroOutputQuery = self._outputs_query(tx_hash) + receiver_tx: MoneroTxWallet | None = receiver.get_tx(tx_hash) + assert receiver_tx is not None + if sender_tx.get_outgoing_amount() != receiver_tx.get_incoming_amount() and self.same_account: + issues.append( + "WARNING: sender tx outgoing amount != receiver tx incoming amount when sent to same account " + f"({sender_tx.get_outgoing_amount()} != {receiver_tx.get_incoming_amount()})" + ) + receiver_fee: int = 0 + if self.same_wallet: + assert receiver_tx.fee is not None + receiver_fee = receiver_tx.fee + receiver_balance: int = receiver.get_balance() + expected_receiver_balance: int = ( + receiver_balance_before + receiver_tx.get_incoming_amount() - receiver_tx.get_outgoing_amount() - receiver_fee + ) + if receiver_balance != expected_receiver_balance: + level: str = "WARNING" if self.same_account else "ERROR" + suffix: str = " when sent to same account" if self.same_account else "" + issues.append( + f"{level}: after sending, receiver balance != balance before + incoming amount - outgoing amount - tx fee{suffix} " + f"({receiver_balance} != {receiver_balance_before} + {receiver_tx.get_incoming_amount()} - " + f"{receiver_tx.get_outgoing_amount()} - {receiver_fee})" + ) + if not self.same_wallet and receiver.get_unlocked_balance() != receiver_unlocked_balance_before: + issues.append("ERROR: receiver unlocked balance should not have changed after sending") + self._test_last_balance_notification( + receiver, receiver_collector, "receiver", "after funds received", "did not notify balance change when funds received" + ) + if len(receiver_collector.get_outputs_received(tx_outputs_query)) == 0: + issues.append("ERROR: receiver did not announce unconfirmed received output") + else: + for output in self.get_missing_outputs(expected_outputs, receiver_collector.get_outputs_received(tx_outputs_query), True): + issues.append(f"ERROR: receiver did not announce received output for {self._output_str(output)}") + def _block_notifications_check(self, start_height: int, end_height: int) -> Callable[[], None]: """The check that the wallets announced the blocks from `start_height` to `end_height` (excluded).""" def check() -> None: @@ -476,6 +511,37 @@ def test_notified_output(cls, output: MoneroOutputWallet, is_tx_input: bool, iss #region Utils + def _confirm_for_light_wallets(self, tx_hash: str) -> None: + """Confirm the tx, one block at a time, until the light wallets report it, while its funds are locked. Nothing to do without them. + + A light wallet reports a block after monero-lws scanned it and the wallet synced. Mining meanwhile adds blocks faster than that, and + the first thing that the wallet knows of the tx can be that its funds are unlocked: it never notifies them as confirmed and locked. + + :param str tx_hash: hash of the tx. + """ + wallets: list[MoneroWallet] = [self.sender] if self.same_wallet else [self.sender, self.receiver] + light_wallets: list[MoneroWalletLight] = [wallet for wallet in wallets if isinstance(wallet, MoneroWalletLight)] + if len(light_wallets) == 0: + return + + # half of the blocks that the funds are locked for, the mining that follows has the other ones + for _ in range(self.NUM_BLOCKS_LOCKED // 2): + MiningUtils.generate_blocks(TestUtils.MINING_ADDRESS, 1) + for wallet in light_wallets: + TestUtils.wait_for_lws_scan(wallet) + wallet.sync() + txs: list[MoneroTxWallet | None] = [wallet.get_tx(tx_hash) for wallet in light_wallets] + if all(tx is not None and tx.is_confirmed for tx in txs): + return + logger.warning(f"The light wallets do not report the confirmed tx {tx_hash}") + + def _get_num_confirmations(self, tx_hash: str) -> int: + """Number of blocks from the one of the tx to the top of the chain, according to the daemon (0 if the tx is not confirmed).""" + tx: MoneroTx | None = self.daemon.get_tx(tx_hash) + assert tx is not None, f"Daemon does not report tx {tx_hash}" + tx_height: int | None = tx.get_height() + return 0 if tx_height is None else self.daemon.get_height() - tx_height + @staticmethod def get_output_state(output: MoneroOutputWallet) -> str: """Get the state of a notified output. 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 b75b7037..c5c58210 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 a612abb6..3d1e73aa 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 12a830bc..deaf0638 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 8a14a39e..7f525166 100644 --- a/tests/wallet/test_accounts.py +++ b/tests/wallet/test_accounts.py @@ -27,7 +27,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") @@ -488,6 +488,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 c40bf4e2..9bc89b8a 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() @@ -858,6 +873,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 a0b6bd74..27775f64 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 66390854..f53c42cf 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") @@ -285,7 +285,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) @@ -323,6 +323,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 4ccf046b..1622866b 100644 --- a/tests/wallet/test_listeners.py +++ b/tests/wallet/test_listeners.py @@ -9,7 +9,7 @@ from utils import MiningUtils, TestUtils, TxWalletUtils, WalletNotificationCollector, WalletNotificationsTester 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") @@ -182,6 +182,11 @@ def test_notifications_same_account_sweep_output_when_relayed(self, daemon: Mone return super().test_notifications_same_account_sweep_output_when_relayed(daemon, wallet) +@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 8e5f7d1e..76a8252d 100644 --- a/tests/wallet/test_multisig.py +++ b/tests/wallet/test_multisig.py @@ -12,7 +12,7 @@ from utils import GenUtils, MiningUtils, TestUtils, TestUtils as Utils, TxWalletUtils, 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("TestMoneroWalletMultisig") @@ -494,6 +494,21 @@ 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.not_supported + @override + def test_multisig(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: + return super().test_multisig(daemon, wallet) + + @override + def _close_wallet(self, wallet: MoneroWallet, save: bool = False) -> None: + # a light wallet has nothing to persist and refuses to save: closing the participants with `save` would hide the error of the test + wallet.close() + + @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 444e291f..8a5067c8 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_support.py b/tests/wallet/test_support.py index f4537bcf..bf0a49fa 100644 --- a/tests/wallet/test_support.py +++ b/tests/wallet/test_support.py @@ -5,7 +5,9 @@ import pytest import yaml -from monero import MoneroWallet, MoneroWalletConfig, MoneroWalletFull, MoneroWalletKeys, MoneroWalletRpc +from monero import ( + MoneroRpcConnection, MoneroWallet, MoneroWalletConfig, MoneroWalletFull, MoneroWalletKeys, MoneroWalletLight, MoneroWalletRpc +) from utils import BaseTestClass, TestUtils as Utils, WalletCall, WALLET_CALLS @@ -13,7 +15,7 @@ # the support matrix of the docs, which these tests keep true SUPPORT_FILE: Path = Path(__file__).parent / "wallet_support.yml" -WALLETS: tuple[str, ...] = ("full", "rpc", "keys") +WALLETS: tuple[str, ...] = ("full", "rpc", "light", "keys") STATES: tuple[str, ...] = ("supported", "partial", "todo", "unsupported") # every method once, in the order of the calls: close() is the last one METHODS: list[str] = list(dict.fromkeys(call.method for call in WALLET_CALLS)) @@ -22,7 +24,7 @@ @pytest.mark.unit class TestMoneroWalletSupport(BaseTestClass): """Checks the support matrix of the docs (tests/wallet/wallet_support.yml) against the bindings: every method of the interface is - called on a full, an rpc and a keys-only wallet, and the methods that a wallet does not implement raise "not supported".""" + called on a full, an rpc, a light and a keys-only wallet, and the methods that a wallet does not implement raise "not supported".""" @pytest.fixture(scope="class") @classmethod @@ -36,16 +38,20 @@ def support(cls) -> dict[str, dict[str, str]]: @classmethod def wallets(cls) -> dict[str, MoneroWallet]: """A wallet of each type, of its own: the calls change and close them. None of them reaches the network: the full one has no - daemon and the rpc one has no server, so their calls fail at once when they are implemented.""" + daemon, the rpc one has no server and the server of the light one does not exist, so their calls fail at once when they are + implemented.""" full_config: MoneroWalletConfig = MoneroWalletConfig() full_config.path = "" full_config.password = Utils.WALLET_PASSWORD full_config.network_type = Utils.NETWORK_TYPE keys_config: MoneroWalletConfig = MoneroWalletConfig() keys_config.network_type = Utils.NETWORK_TYPE + light_config: MoneroWalletConfig = MoneroWalletConfig() + light_config.network_type = Utils.NETWORK_TYPE return { "full": MoneroWalletFull.create_wallet(full_config), "rpc": MoneroWalletRpc(), + "light": MoneroWalletLight.create_wallet(light_config, MoneroRpcConnection(Utils.OFFLINE_SERVER_URI)), "keys": MoneroWalletKeys.create_wallet_random(keys_config), } diff --git a/tests/wallet/test_sweep.py b/tests/wallet/test_sweep.py index 62a0aa70..ec45cbf1 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 4877fe1e..fe1941e3 100644 --- a/tests/wallet/test_transfers.py +++ b/tests/wallet/test_transfers.py @@ -16,7 +16,7 @@ from utils import TestUtils, TransferUtils, 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") @@ -360,6 +360,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 cfe5e370..7e778ddd 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 516515b3..c8fbae65 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() diff --git a/tests/wallet/wallet_support.yml b/tests/wallet/wallet_support.yml index 1d07835f..a2afb58f 100644 --- a/tests/wallet/wallet_support.yml +++ b/tests/wallet/wallet_support.yml @@ -1,6 +1,6 @@ # Which wallet implements which method of MoneroWallet: the "Wallet support" page of the docs is made from this file, by section and in -# this order. tests/wallet/test_support.py calls every method on a full, an rpc and a keys-only wallet, and fails when this file says -# otherwise, so a change of the bindings comes with its change here. +# this order. tests/wallet/test_support.py calls every method on a full, an rpc, a light and a keys-only wallet, and fails when this +# file says otherwise, so a change of the bindings comes with its change here. # # supported the wallet implements it # partial the wallet implements some of its overloads: the note says which @@ -10,151 +10,151 @@ # The calls only tell implemented from not, todo and unsupported are the same to the test: which one is written here. Wallet: - get_version: {full: supported, rpc: supported, keys: supported} - get_path: {full: supported, rpc: supported, keys: unsupported} - get_network_type: {full: supported, rpc: unsupported, keys: supported} - is_view_only: {full: supported, rpc: supported, keys: supported} - get_attribute: {full: supported, rpc: supported, keys: unsupported} - set_attribute: {full: supported, rpc: supported, keys: unsupported} - change_password: {full: supported, rpc: supported, keys: unsupported} - move_to: {full: supported, rpc: unsupported, keys: unsupported} - save: {full: supported, rpc: supported, keys: unsupported} + get_version: {full: supported, rpc: supported, light: supported, keys: supported} + get_path: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + get_network_type: {full: supported, rpc: unsupported, light: supported, keys: supported} + is_view_only: {full: supported, rpc: supported, light: supported, keys: supported} + get_attribute: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + set_attribute: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + change_password: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + move_to: {full: supported, rpc: unsupported, light: unsupported, keys: unsupported} + save: {full: supported, rpc: supported, light: unsupported, keys: unsupported} Keys and addresses: - get_seed: {full: supported, rpc: supported, keys: supported} - get_seed_language: {full: supported, rpc: unsupported, keys: supported} - get_public_view_key: {full: supported, rpc: supported, keys: supported} - get_private_view_key: {full: supported, rpc: supported, keys: supported} - get_public_spend_key: {full: supported, rpc: supported, keys: supported} - get_private_spend_key: {full: supported, rpc: supported, keys: supported} - get_primary_address: {full: supported, rpc: supported, keys: supported} - get_address: {full: supported, rpc: supported, keys: supported} - get_address_index: {full: supported, rpc: supported, keys: unsupported} - get_integrated_address: {full: supported, rpc: supported, keys: supported} - decode_integrated_address: {full: supported, rpc: supported, keys: supported} + get_seed: {full: supported, rpc: supported, light: supported, keys: supported} + get_seed_language: {full: supported, rpc: unsupported, light: supported, keys: supported} + get_public_view_key: {full: supported, rpc: supported, light: supported, keys: supported} + get_private_view_key: {full: supported, rpc: supported, light: supported, keys: supported} + get_public_spend_key: {full: supported, rpc: supported, light: supported, keys: supported} + get_private_spend_key: {full: supported, rpc: supported, light: supported, keys: supported} + get_primary_address: {full: supported, rpc: supported, light: supported, keys: supported} + get_address: {full: supported, rpc: supported, light: supported, keys: supported} + get_address_index: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_integrated_address: {full: supported, rpc: supported, light: supported, keys: supported} + decode_integrated_address: {full: supported, rpc: supported, light: supported, keys: supported} Daemon connection: - set_daemon_connection: {full: supported, rpc: supported, keys: unsupported} - get_daemon_connection: {full: supported, rpc: supported, keys: unsupported} - is_connected_to_daemon: {full: supported, rpc: supported, keys: unsupported} - is_daemon_synced: {full: supported, rpc: unsupported, keys: unsupported} - is_daemon_trusted: {full: supported, rpc: unsupported, keys: unsupported} - get_daemon_height: {full: supported, rpc: unsupported, keys: unsupported} - get_daemon_max_peer_height: {full: supported, rpc: unsupported, keys: unsupported} - get_height_by_date: {full: supported, rpc: unsupported, keys: unsupported} - wait_for_next_block: {full: supported, rpc: unsupported, keys: unsupported} + set_daemon_connection: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + get_daemon_connection: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + is_connected_to_daemon: {full: supported, rpc: supported, light: supported, keys: unsupported} + is_daemon_synced: {full: supported, rpc: unsupported, light: supported, keys: unsupported} + is_daemon_trusted: {full: supported, rpc: unsupported, light: supported, keys: unsupported} + get_daemon_height: {full: supported, rpc: unsupported, light: supported, keys: unsupported} + get_daemon_max_peer_height: {full: supported, rpc: unsupported, light: supported, keys: unsupported} + get_height_by_date: {full: supported, rpc: unsupported, light: unsupported, keys: unsupported} + wait_for_next_block: {full: supported, rpc: unsupported, light: supported, keys: unsupported} Sync: - is_synced: {full: supported, rpc: unsupported, keys: unsupported} - get_height: {full: supported, rpc: supported, keys: unsupported} - get_restore_height: {full: supported, rpc: unsupported, keys: unsupported} - set_restore_height: {full: supported, rpc: unsupported, keys: unsupported} - sync: {full: supported, rpc: partial, keys: unsupported, note: "rpc: sync() and sync(start_height), not with a listener"} - start_syncing: {full: supported, rpc: supported, keys: unsupported} - stop_syncing: {full: supported, rpc: supported, keys: unsupported} - scan_txs: {full: supported, rpc: supported, keys: unsupported} - rescan_spent: {full: supported, rpc: supported, keys: unsupported} - rescan_blockchain: {full: supported, rpc: supported, keys: unsupported} + is_synced: {full: supported, rpc: unsupported, light: supported, keys: unsupported} + get_height: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_restore_height: {full: supported, rpc: unsupported, light: supported, keys: unsupported} + set_restore_height: {full: supported, rpc: unsupported, light: supported, keys: unsupported} + sync: {full: supported, rpc: partial, light: supported, keys: unsupported, note: "rpc: sync() and sync(start_height), not with a listener"} + start_syncing: {full: supported, rpc: supported, light: supported, keys: unsupported} + stop_syncing: {full: supported, rpc: supported, light: supported, keys: unsupported} + scan_txs: {full: supported, rpc: supported, light: supported, keys: unsupported} + rescan_spent: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + rescan_blockchain: {full: supported, rpc: supported, light: unsupported, keys: unsupported} Listeners: - add_listener: {full: supported, rpc: supported, keys: unsupported} - remove_listener: {full: supported, rpc: supported, keys: unsupported} - get_listeners: {full: supported, rpc: supported, keys: unsupported} + add_listener: {full: supported, rpc: supported, light: supported, keys: unsupported} + remove_listener: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_listeners: {full: supported, rpc: supported, light: supported, keys: unsupported} Balances: - get_balance: {full: supported, rpc: supported, keys: unsupported} - get_unlocked_balance: {full: supported, rpc: supported, keys: unsupported} + get_balance: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_unlocked_balance: {full: supported, rpc: supported, light: supported, keys: unsupported} Accounts and subaddresses: - get_accounts: {full: supported, rpc: supported, keys: unsupported} - get_account: {full: supported, rpc: supported, keys: supported} - create_account: {full: supported, rpc: supported, keys: unsupported} - set_account_label: {full: supported, rpc: supported, keys: unsupported} - get_subaddresses: {full: supported, rpc: supported, keys: supported} - get_subaddress: {full: supported, rpc: supported, keys: supported} - create_subaddress: {full: supported, rpc: supported, keys: unsupported} - set_subaddress_label: {full: supported, rpc: supported, keys: unsupported} + get_accounts: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_account: {full: supported, rpc: supported, light: supported, keys: supported} + create_account: {full: supported, rpc: supported, light: supported, keys: unsupported} + set_account_label: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + get_subaddresses: {full: supported, rpc: supported, light: supported, keys: supported} + get_subaddress: {full: supported, rpc: supported, light: supported, keys: supported} + create_subaddress: {full: supported, rpc: supported, light: supported, keys: unsupported} + set_subaddress_label: {full: supported, rpc: supported, light: unsupported, keys: unsupported} Account tags: - tag_accounts: {full: supported, rpc: supported, keys: unsupported} - untag_accounts: {full: supported, rpc: supported, keys: unsupported} - get_account_tags: {full: supported, rpc: supported, keys: unsupported} - set_account_tag_label: {full: supported, rpc: supported, keys: unsupported} + tag_accounts: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + untag_accounts: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + get_account_tags: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + set_account_tag_label: {full: supported, rpc: supported, light: unsupported, keys: unsupported} Transactions, transfers and outputs: - get_tx: {full: supported, rpc: supported, keys: unsupported} - get_txs: {full: supported, rpc: supported, keys: unsupported} - get_transfers: {full: supported, rpc: supported, keys: unsupported} - get_incoming_transfers: {full: supported, rpc: supported, keys: unsupported} - get_outgoing_transfers: {full: supported, rpc: supported, keys: unsupported} - get_outputs: {full: supported, rpc: supported, keys: unsupported} - export_outputs: {full: supported, rpc: supported, keys: unsupported} - import_outputs: {full: supported, rpc: supported, keys: unsupported} - export_key_images: {full: supported, rpc: supported, keys: unsupported} - import_key_images: {full: supported, rpc: supported, keys: unsupported} - get_new_key_images_from_last_import: {full: supported, rpc: supported, keys: unsupported} - freeze_output: {full: supported, rpc: supported, keys: unsupported} - thaw_output: {full: supported, rpc: supported, keys: unsupported} - is_output_frozen: {full: supported, rpc: supported, keys: unsupported} + get_tx: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_txs: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_transfers: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_incoming_transfers: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_outgoing_transfers: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_outputs: {full: supported, rpc: supported, light: supported, keys: unsupported} + export_outputs: {full: supported, rpc: supported, light: supported, keys: unsupported} + import_outputs: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + export_key_images: {full: supported, rpc: supported, light: supported, keys: unsupported} + import_key_images: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_new_key_images_from_last_import: {full: supported, rpc: supported, light: supported, keys: unsupported} + freeze_output: {full: supported, rpc: supported, light: supported, keys: unsupported} + thaw_output: {full: supported, rpc: supported, light: supported, keys: unsupported} + is_output_frozen: {full: supported, rpc: supported, light: supported, keys: unsupported} Sending: - get_default_fee_priority: {full: supported, rpc: supported, keys: unsupported} - create_tx: {full: supported, rpc: supported, keys: unsupported} - create_txs: {full: supported, rpc: supported, keys: unsupported} - sweep_unlocked: {full: supported, rpc: supported, keys: unsupported} - sweep_output: {full: supported, rpc: supported, keys: unsupported} - sweep_dust: {full: supported, rpc: supported, keys: unsupported} - relay_tx: {full: supported, rpc: supported, keys: unsupported} - relay_txs: {full: supported, rpc: supported, keys: unsupported} - describe_tx_set: {full: supported, rpc: supported, keys: unsupported} - describe_unsigned_tx_set: {full: supported, rpc: supported, keys: unsupported} - describe_multisig_tx_set: {full: supported, rpc: supported, keys: unsupported} - sign_txs: {full: supported, rpc: supported, keys: unsupported} - submit_txs: {full: supported, rpc: supported, keys: unsupported} + get_default_fee_priority: {full: supported, rpc: supported, light: supported, keys: unsupported} + create_tx: {full: supported, rpc: supported, light: supported, keys: unsupported} + create_txs: {full: supported, rpc: supported, light: supported, keys: unsupported} + sweep_unlocked: {full: supported, rpc: supported, light: supported, keys: unsupported} + sweep_output: {full: supported, rpc: supported, light: supported, keys: unsupported} + sweep_dust: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + relay_tx: {full: supported, rpc: supported, light: supported, keys: unsupported} + relay_txs: {full: supported, rpc: supported, light: supported, keys: unsupported} + describe_tx_set: {full: supported, rpc: supported, light: supported, keys: unsupported} + describe_unsigned_tx_set: {full: supported, rpc: supported, light: supported, keys: unsupported} + describe_multisig_tx_set: {full: supported, rpc: supported, light: supported, keys: unsupported} + sign_txs: {full: supported, rpc: supported, light: supported, keys: unsupported} + submit_txs: {full: supported, rpc: supported, light: supported, keys: unsupported} Proofs: - get_tx_key: {full: supported, rpc: supported, keys: unsupported} - check_tx_key: {full: supported, rpc: supported, keys: unsupported} - get_tx_proof: {full: supported, rpc: supported, keys: unsupported} - check_tx_proof: {full: supported, rpc: supported, keys: unsupported} - get_spend_proof: {full: supported, rpc: supported, keys: unsupported} - check_spend_proof: {full: supported, rpc: supported, keys: unsupported} - get_reserve_proof_wallet: {full: supported, rpc: supported, keys: unsupported} - get_reserve_proof_account: {full: supported, rpc: supported, keys: unsupported} - check_reserve_proof: {full: supported, rpc: supported, keys: unsupported} + get_tx_key: {full: supported, rpc: supported, light: supported, keys: unsupported} + check_tx_key: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_tx_proof: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + check_tx_proof: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + get_spend_proof: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + check_spend_proof: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + get_reserve_proof_wallet: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + get_reserve_proof_account: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + check_reserve_proof: {full: supported, rpc: supported, light: unsupported, keys: unsupported} Messages and payment URIs: - sign_message: {full: supported, rpc: supported, keys: supported} - verify_message: {full: supported, rpc: supported, keys: supported} - get_payment_uri: {full: supported, rpc: supported, keys: supported} - parse_payment_uri: {full: supported, rpc: supported, keys: supported} + sign_message: {full: supported, rpc: supported, light: supported, keys: supported} + verify_message: {full: supported, rpc: supported, light: supported, keys: supported} + get_payment_uri: {full: supported, rpc: supported, light: supported, keys: supported} + parse_payment_uri: {full: supported, rpc: supported, light: supported, keys: supported} Transaction notes and address book: - get_tx_note: {full: supported, rpc: supported, keys: unsupported} - get_tx_notes: {full: supported, rpc: supported, keys: unsupported} - set_tx_note: {full: supported, rpc: supported, keys: unsupported} - set_tx_notes: {full: supported, rpc: supported, keys: unsupported} - get_address_book_entries: {full: supported, rpc: supported, keys: unsupported} - add_address_book_entry: {full: supported, rpc: supported, keys: unsupported} - edit_address_book_entry: {full: supported, rpc: supported, keys: unsupported} - delete_address_book_entry: {full: supported, rpc: supported, keys: unsupported} + get_tx_note: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + get_tx_notes: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + set_tx_note: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + set_tx_notes: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + get_address_book_entries: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + add_address_book_entry: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + edit_address_book_entry: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + delete_address_book_entry: {full: supported, rpc: supported, light: unsupported, keys: unsupported} Multisig: - is_multisig_import_needed: {full: supported, rpc: supported, keys: unsupported} - is_multisig: {full: supported, rpc: supported, keys: unsupported} - get_multisig_info: {full: supported, rpc: supported, keys: unsupported} - prepare_multisig: {full: supported, rpc: supported, keys: unsupported} - make_multisig: {full: supported, rpc: supported, keys: unsupported} - exchange_multisig_keys: {full: supported, rpc: supported, keys: unsupported} - export_multisig_hex: {full: supported, rpc: supported, keys: unsupported} - import_multisig_hex: {full: supported, rpc: supported, keys: unsupported} - sign_multisig_tx_hex: {full: supported, rpc: supported, keys: unsupported} - submit_multisig_tx_hex: {full: supported, rpc: supported, keys: unsupported} + is_multisig_import_needed: {full: supported, rpc: supported, light: supported, keys: unsupported} + is_multisig: {full: supported, rpc: supported, light: supported, keys: unsupported} + get_multisig_info: {full: supported, rpc: supported, light: supported, keys: unsupported} + prepare_multisig: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + make_multisig: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + exchange_multisig_keys: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + export_multisig_hex: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + import_multisig_hex: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + sign_multisig_tx_hex: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + submit_multisig_tx_hex: {full: supported, rpc: supported, light: unsupported, keys: unsupported} Mining: - start_mining: {full: supported, rpc: supported, keys: unsupported} - stop_mining: {full: supported, rpc: supported, keys: unsupported} + start_mining: {full: supported, rpc: supported, light: unsupported, keys: unsupported} + stop_mining: {full: supported, rpc: supported, light: unsupported, keys: unsupported} Closing: - is_closed: {full: supported, rpc: supported, keys: supported} - close: {full: supported, rpc: supported, keys: supported} + is_closed: {full: supported, rpc: supported, light: supported, keys: supported} + close: {full: supported, rpc: supported, light: supported, keys: supported}