Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
36d08a0
tests: split the wallet tests in a file for each topic
everoddandeven Nov 14, 2026
f019438
tests: split the daemon tests in a file for each topic
everoddandeven Nov 14, 2026
eb3e2dd
tests: share the test wallets and the setup for the whole session
everoddandeven Nov 14, 2026
f65f538
tests: split the wallet model tests and move the small ones in folders
everoddandeven Nov 14, 2026
74ae630
tests: split the utils tests in a file for each topic
everoddandeven Nov 14, 2026
5a088f9
tests: run the send tests of the wallets first
everoddandeven Nov 14, 2026
cd50da4
tests: do not create the open rpc wallet again from its seed
everoddandeven Nov 14, 2026
c84f00d
tests: ask the server if the rpc wallet is open, is_closed of monero-…
everoddandeven Nov 14, 2026
daa2f8a
tests: generate the blocks that a regtest chain needs, do not wait fo…
everoddandeven Nov 14, 2026
6f2212e
tests: run the wallet tests one type of wallet at a time
everoddandeven Nov 14, 2026
73b02ce
bindings: keep the listeners alive while the daemon or wallet that ha…
everoddandeven Nov 14, 2026
0308321
tests: remove the daemon listeners also when a test fails
everoddandeven Nov 14, 2026
ee3fbdf
build: link boost regex, monero-project uses it
everoddandeven Nov 14, 2026
17670a4
tests: generate a block to clear the pool on regtest, do not mine wit…
everoddandeven Nov 14, 2026
d4f8972
tests: check that daemon and wallet keep their listeners alive
everoddandeven Nov 14, 2026
1bc2dfb
bindings: destroy the daemons and the wallets without the gil, their …
everoddandeven Nov 14, 2026
a8d89e9
tests: check that a daemon is destroyed while its poller waits for th…
everoddandeven Nov 14, 2026
2bdc7dc
build: link boost regex only before 1.84, where it is a library
everoddandeven Nov 14, 2026
022dfe6
tests: generate blocks only at a low difficulty, mining is faster at …
everoddandeven Nov 15, 2026
de5fc43
tests: fund the test wallet again when the tests of the next wallet t…
everoddandeven Nov 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ if (Boost_VERSION_STRING VERSION_LESS 1.69.0)
list(APPEND BOOST_COMPONENTS system)
endif()

# Boost Regex is header-only since 1.84. monero-project uses it, and before that it is a library that the module has to link
if (Boost_VERSION_STRING VERSION_LESS 1.84.0)
list(APPEND BOOST_COMPONENTS regex)
endif()

find_package(Boost ${BOOST_MIN_VER} QUIET REQUIRED COMPONENTS ${BOOST_COMPONENTS})
message(STATUS "Boost components: ${BOOST_COMPONENTS}")
message(STATUS "Using Boost include dir at ${Boost_INCLUDE_DIR}")
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ For example: `export LD_PRELOAD=/path/to/libjemalloc.so.2` then run your app. It
```bash
pytest -m unit
```

The wallet tests are in [tests/wallet](tests/wallet), a file for each topic (`test_sweep.py`, `test_txs.py`, `test_outputs.py`...). Each file has a base class with the tests that every wallet should pass, and a class for each wallet type (`TestMoneroWalletFullSweep`, `TestMoneroWalletRpcSweep`, `TestMoneroWalletKeysSweep`) with what that type does differently. To run one topic, or one wallet type:
```bash
pytest tests/wallet/test_sweep.py
pytest tests/wallet -k Rpc
```

The daemon tests are split in the same way: [tests/daemon/rpc](tests/daemon/rpc) has the integration tests of the rpc daemon (a file for each topic, such as `test_blocks.py`, `test_tx_pool.py` and `test_peers.py`), [tests/daemon/model](tests/daemon/model) the unit tests of the daemon models, grouped by model, and [tests/daemon/test_interface.py](tests/daemon/test_interface.py) the ones of the daemon interface.

The wallet models are tested in [tests/wallet/model](tests/wallet/model) (a file for each kind of model: queries, transfers, outputs, accounts...), the calls of the wallet interface in [tests/wallet/test_interface.py](tests/wallet/test_interface.py), and the tests of what is not about a wallet or a daemon, such as the rpc connection, in [tests/common](tests/common), where the tests of `MoneroUtils` are split by topic in [tests/common/monero_utils](tests/common/monero_utils).
7. Cleanup docker test environment
```bash
docker compose -f tests/docker-compose.yml down -v
Expand Down
10 changes: 7 additions & 3 deletions src/cpp/daemon/py_monero_daemon_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ void py_monero_bind_daemon(py::module_& m, PyMoneroTypes& t) {
.def(py::init<>())
.def("add_listener", [](monero_daemon& self, monero_daemon_listener& listener) {
MONERO_CATCH_AND_RETHROW(self.add_listener(listener));
}, py::arg("listener"), py::call_guard<py::gil_scoped_release>())
}, py::arg("listener"), py::keep_alive<1, 2>(), py::call_guard<py::gil_scoped_release>())
.def("remove_listener", [](monero_daemon& self, monero_daemon_listener& listener) {
MONERO_CATCH_AND_RETHROW(self.remove_listener(listener));
}, py::arg("listener"), py::call_guard<py::gil_scoped_release>())
Expand Down Expand Up @@ -851,8 +851,12 @@ void py_monero_bind_daemon(py::module_& m, PyMoneroTypes& t) {

// monero_daemon_rpc
t.py_monero_daemon_rpc
.def(py::init<const std::shared_ptr<monero_rpc_connection>&>(), py::arg("rpc"), py::call_guard<py::gil_scoped_release>())
.def(py::init<const std::string&, const std::string&, const std::string&, const std::string&, const std::string&, const boost::optional<uint32_t>&>(), py::arg("uri"), py::arg("username") = "", py::arg("password") = "", py::arg("proxy_uri") = "", py::arg("zmq_uri") = "", py::arg("timeout_ms") = py::none(), py::call_guard<py::gil_scoped_release>())
.def(py::init([](const std::shared_ptr<monero_rpc_connection>& rpc) {
return make_gil_free_holder(new monero_daemon_rpc(rpc));
}), py::arg("rpc"), py::call_guard<py::gil_scoped_release>())
.def(py::init([](const std::string& uri, const std::string& username, const std::string& password, const std::string& proxy_uri, const std::string& zmq_uri, const boost::optional<uint32_t>& timeout) {
return make_gil_free_holder(new monero_daemon_rpc(uri, username, password, proxy_uri, zmq_uri, timeout));
}), py::arg("uri"), py::arg("username") = "", py::arg("password") = "", py::arg("proxy_uri") = "", py::arg("zmq_uri") = "", py::arg("timeout_ms") = py::none(), py::call_guard<py::gil_scoped_release>())
.def("get_rpc_connection", [](const monero_daemon_rpc& self) {
MONERO_CATCH_AND_RETHROW(self.get_rpc_connection());
})
Expand Down
21 changes: 21 additions & 0 deletions src/cpp/py_monero_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ PYBIND11_MAKE_OPAQUE(VectorUint64);
#include "wallet/monero_wallet_full.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
// python, which needs the GIL. A python object is destroyed with the GIL, so the destructor and the thread waited for each wait for the other
// one and the process stays stuck. The holders of these objects release the GIL to delete them.
struct gil_free_delete {
template <class T>
void operator()(T* ptr) const {
// the GIL is not held when the last reference is dropped by a thread of c++
if (PyGILState_Check()) {
py::gil_scoped_release release;
delete ptr;
} else {
delete ptr;
}
}
};

template <class T>
std::shared_ptr<T> make_gil_free_holder(T* ptr) {
return std::shared_ptr<T>(ptr, gil_free_delete());
}

#define MONERO_CATCH_AND_RETHROW(expr) \
try { \
return expr; \
Expand Down
22 changes: 13 additions & 9 deletions src/cpp/wallet/py_monero_wallet_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ void py_monero_bind_wallet(py::module_& m, PyMoneroTypes& t) {
}, py::arg("year"), py::arg("month"), py::arg("day"), py::call_guard<py::gil_scoped_release>())
.def("add_listener", [](PyMoneroWallet& self, monero_wallet_listener& listener) {
MONERO_CATCH_AND_RETHROW(self.add_listener(listener));
}, py::arg("listener"), py::call_guard<py::gil_scoped_release>())
}, py::arg("listener"), py::keep_alive<1, 2>(), py::call_guard<py::gil_scoped_release>())
.def("remove_listener", [](PyMoneroWallet& self, monero_wallet_listener& listener) {
MONERO_CATCH_AND_RETHROW(self.remove_listener(listener));
}, py::arg("listener"), py::call_guard<py::gil_scoped_release>())
Expand Down Expand Up @@ -1064,13 +1064,13 @@ void py_monero_bind_wallet(py::module_& m, PyMoneroTypes& t) {
// monero_wallet_keys
t.py_monero_wallet_keys
.def_static("create_wallet_random", [](const monero_wallet_config& config) {
MONERO_CATCH_AND_RETHROW(monero_wallet_keys::create_wallet_random(config));
MONERO_CATCH_AND_RETHROW(make_gil_free_holder(monero_wallet_keys::create_wallet_random(config)));
}, py::arg("config"), py::call_guard<py::gil_scoped_release>())
.def_static("create_wallet_from_seed", [](const monero_wallet_config& config) {
MONERO_CATCH_AND_RETHROW(monero_wallet_keys::create_wallet_from_seed(config));
MONERO_CATCH_AND_RETHROW(make_gil_free_holder(monero_wallet_keys::create_wallet_from_seed(config)));
}, py::arg("config"), py::call_guard<py::gil_scoped_release>())
.def_static("create_wallet_from_keys", [](const monero_wallet_config& config) {
MONERO_CATCH_AND_RETHROW(monero_wallet_keys::create_wallet_from_keys(config));
MONERO_CATCH_AND_RETHROW(make_gil_free_holder(monero_wallet_keys::create_wallet_from_keys(config)));
}, py::arg("config"), py::call_guard<py::gil_scoped_release>())
.def_static("get_seed_languages", []() {
MONERO_CATCH_AND_RETHROW(monero_wallet_keys::get_seed_languages());
Expand All @@ -1082,13 +1082,13 @@ void py_monero_bind_wallet(py::module_& m, PyMoneroTypes& t) {
MONERO_CATCH_AND_RETHROW(monero_wallet_full::wallet_exists(path));
}, py::arg("path"), py::call_guard<py::gil_scoped_release>())
.def_static("open_wallet", [](const std::string& path, const std::string& password, monero_network_type nettype, bool regtest) {
MONERO_CATCH_AND_RETHROW(monero_wallet_full::open_wallet(path, password, nettype, regtest));
MONERO_CATCH_AND_RETHROW(make_gil_free_holder(monero_wallet_full::open_wallet(path, password, nettype, regtest)));
}, py::arg("path"), py::arg("password"), py::arg("nettype"), py::arg("regtest") = false, py::call_guard<py::gil_scoped_release>())
.def_static("open_wallet_data", [](const std::string& password, monero_network_type nettype, const std::string& keys_data, const std::string& cache_data, const std::shared_ptr<monero_rpc_connection>& daemon_connection, bool regtest) {
MONERO_CATCH_AND_RETHROW(monero_wallet_full::open_wallet_data(password, nettype, keys_data, cache_data, daemon_connection, nullptr, regtest));
MONERO_CATCH_AND_RETHROW(make_gil_free_holder(monero_wallet_full::open_wallet_data(password, nettype, keys_data, cache_data, daemon_connection, nullptr, regtest)));
}, py::arg("password"), py::arg("nettype"), py::arg("keys_data"), py::arg("cache_data"), py::arg("daemon_connection") = py::none(), py::arg("regtest") = false, py::call_guard<py::gil_scoped_release>())
.def_static("create_wallet", [](const monero_wallet_config& config) {
MONERO_CATCH_AND_RETHROW(monero_wallet_full::create_wallet(config));
MONERO_CATCH_AND_RETHROW(make_gil_free_holder(monero_wallet_full::create_wallet(config)));
}, py::arg("config"), py::call_guard<py::gil_scoped_release>())
.def_static("get_seed_languages", []() {
MONERO_CATCH_AND_RETHROW(monero_wallet_full::get_seed_languages());
Expand All @@ -1105,8 +1105,12 @@ void py_monero_bind_wallet(py::module_& m, PyMoneroTypes& t) {

// monero_wallet_rpc
t.py_monero_wallet_rpc
.def(py::init<const std::shared_ptr<monero_rpc_connection>&>(), py::arg("rpc_connection"), py::call_guard<py::gil_scoped_release>())
.def(py::init<const std::string&, const std::string&, const std::string&, const std::string&, const std::string&, const boost::optional<uint32_t>&>(), py::arg("uri") = "", py::arg("username") = "", py::arg("password") = "", py::arg("proxy_uri") = "", py::arg("zmq_uri") = "", py::arg("timeout_ms") = py::none(), py::call_guard<py::gil_scoped_release>())
.def(py::init([](const std::shared_ptr<monero_rpc_connection>& rpc_connection) {
return make_gil_free_holder(new monero_wallet_rpc(rpc_connection));
}), py::arg("rpc_connection"), py::call_guard<py::gil_scoped_release>())
.def(py::init([](const std::string& uri, const std::string& username, const std::string& password, const std::string& proxy_uri, const std::string& zmq_uri, const boost::optional<uint32_t>& timeout) {
return make_gil_free_holder(new monero_wallet_rpc(uri, username, password, proxy_uri, zmq_uri, timeout));
}), py::arg("uri") = "", py::arg("username") = "", py::arg("password") = "", py::arg("proxy_uri") = "", py::arg("zmq_uri") = "", py::arg("timeout_ms") = py::none(), py::call_guard<py::gil_scoped_release>())
.def("create_wallet", [](monero_wallet_rpc& self, const std::shared_ptr<monero_wallet_config>& config) {
MONERO_CATCH_AND_RETHROW(self.create_wallet(config));
}, py::arg("config"), py::call_guard<py::gil_scoped_release>())
Expand Down
Empty file added tests/common/__init__.py
Empty file.
Empty file.
53 changes: 53 additions & 0 deletions tests/common/monero_utils/base_test_monero_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from __future__ import annotations

import logging
from configparser import ConfigParser

import pytest

from utils import AddressBook, BaseTestClass, KeysBook

logger: logging.Logger = logging.getLogger("TestMoneroUtils")


class BaseTestMoneroUtils(BaseTestClass):
"""Monero utilities unit tests."""

class Config:
"""Utils tests configuration."""
mainnet: AddressBook = AddressBook()
"""Mainnet address book."""
testnet: AddressBook = AddressBook()
"""Testnet address book."""
stagenet: AddressBook = AddressBook()
"""Stagenet address book."""
keys: KeysBook = KeysBook()
"""Wallet keys book."""
serialization_msg: str = ''
"""Message to serialize."""

@classmethod
def parse(cls, parser: ConfigParser) -> BaseTestMoneroUtils.Config:
"""Parse utils tests configuration.

:param ConfigParser parser: configuration parser.
:returns BaseTestMoneroUtils.Config: parsed test utils configuration.
"""
config: BaseTestMoneroUtils.Config = cls()
# check section
assert parser.has_section("serialization"), "Section [serialization] not found in test config"
# load address books
config.mainnet = AddressBook.parse(parser, "mainnet")
config.testnet = AddressBook.parse(parser, "testnet")
config.stagenet = AddressBook.parse(parser, "stagenet")
# load keys book
config.keys = KeysBook.parse(parser)
config.serialization_msg = parser.get("serialization", "msg")
return config

@pytest.fixture(scope="class")
@classmethod
def config(cls) -> BaseTestMoneroUtils.Config:
parser: ConfigParser = ConfigParser()
parser.read('tests/config/test_monero_utils.ini')
return BaseTestMoneroUtils.Config.parse(parser)
100 changes: 100 additions & 0 deletions tests/common/monero_utils/test_amounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from __future__ import annotations

import logging

import pytest
from monero import MoneroUtils

from utils import BaseTestClass

logger: logging.Logger = logging.getLogger("TestMoneroUtilsAmounts")


@pytest.mark.unit
class TestMoneroUtilsAmounts(BaseTestClass):
"""Monero utilities amounts unit tests."""

# Can convert between XMR and atomic units
def test_atomic_unit_conversion(self) -> None:
assert 1000000000000 == MoneroUtils.xmr_to_atomic_units(1)
assert 1 == MoneroUtils.atomic_units_to_xmr(1000000000000)
assert 1000000000 == MoneroUtils.xmr_to_atomic_units(0.001)
assert 0.001 == MoneroUtils.atomic_units_to_xmr(1000000000)
assert 250000000000 == MoneroUtils.xmr_to_atomic_units(0.25)
assert 0.25 == MoneroUtils.atomic_units_to_xmr(250000000000)
assert 1250000000000 == MoneroUtils.xmr_to_atomic_units(1.25)
assert 1.25 == MoneroUtils.atomic_units_to_xmr(1250000000000)
assert 2796726190000 == MoneroUtils.xmr_to_atomic_units(2.79672619)
assert 2.79672619 == MoneroUtils.atomic_units_to_xmr(2796726190000)
assert 2796726190001 == MoneroUtils.xmr_to_atomic_units(2.796726190001)
assert 2.796726190001 == MoneroUtils.atomic_units_to_xmr(2796726190001)
assert 2796726189999 == MoneroUtils.xmr_to_atomic_units(2.796726189999)
assert 2.796726189999 == MoneroUtils.atomic_units_to_xmr(2796726189999)
assert 2796726180000 == MoneroUtils.xmr_to_atomic_units(2.79672618)
assert 2.79672618 == MoneroUtils.atomic_units_to_xmr(2796726180000)

# xmr_to_atomic_units(0.0) and negative zero are valid, not errors
def test_xmr_to_atomic_units_zero(self) -> None:
assert 0 == MoneroUtils.xmr_to_atomic_units(0.0)
# -0.0 < 0 is False in IEEE 754, so it must not be rejected by the
# "amount must be non-negative" check
assert 0 == MoneroUtils.xmr_to_atomic_units(-0.0)

# Amounts smaller than half an atomic unit (1e-12 XMR) round down to 0
def test_xmr_to_atomic_units_rounds_down_to_zero(self) -> None:
assert 0 == MoneroUtils.xmr_to_atomic_units(1e-13)
assert 0 == MoneroUtils.xmr_to_atomic_units(4e-13)

# xmr_to_atomic_units() rounds the underlying long double * 1e12 product to
# the nearest atomic unit (away from zero on an exact .5). Decimal literals
# ending in .5e-12 are not necessarily exact halves once represented as a
# double, so which way they round depends on whether the closest double is
# a hair above or below the nominal decimal value, and that itself can
# depend on the platform's "long double" precision (80-bit extended on
# x86_64 Linux/glibc, but identical to a plain 64-bit double on Apple
# Silicon/macOS), so the same literal can round differently on different
# platforms. This is inherent to IEEE 754, not an inconsistency in
# xmr_to_atomic_units() itself, assert the result lands on one of the
# two atomic units the value sits between, not a specific platform's tie-break.
def test_xmr_to_atomic_units_half_atomic_unit_rounding(self) -> None:
cases: list[tuple[float, int, int]] = [
(0.5e-12, 0, 1),
(1.5e-12, 1, 2),
(2.5e-12, 2, 3),
(3.5e-12, 3, 4),
]
for amount_xmr, floor_atomic, ceil_atomic in cases:
actual: int = MoneroUtils.xmr_to_atomic_units(amount_xmr)
logger.debug(f"xmr_to_atomic_units({amount_xmr!r}) = {actual} (expected {floor_atomic} or {ceil_atomic})")
assert actual in (floor_atomic, ceil_atomic), f"xmr_to_atomic_units({amount_xmr!r}) == {actual}, expected {floor_atomic} or {ceil_atomic}"

# amount_xmr must be finite and non-negative
@pytest.mark.parametrize("amount_xmr", [-1.0, -0.0000000001, float("nan"), float("inf"), float("-inf")])
def test_xmr_to_atomic_units_invalid_amount(self, amount_xmr: float) -> None:
with pytest.raises(RuntimeError, match="amount must be a finite, non-negative number"):
MoneroUtils.xmr_to_atomic_units(amount_xmr)

# amounts whose rounded atomic-unit value would overflow uint64_t are rejected
# rather than silently wrapping or invoking undefined behavior on the cast
def test_xmr_to_atomic_units_overflow(self) -> None:
# UINT64_MAX atomic units is ~18446744.0737... XMR; comfortably over that
# (with margin for float imprecision) must raise
with pytest.raises(RuntimeError, match="amount exceeds maximum representable atomic units"):
MoneroUtils.xmr_to_atomic_units(18446745.0)
with pytest.raises(RuntimeError, match="amount exceeds maximum representable atomic units"):
MoneroUtils.xmr_to_atomic_units(2e22)

# values comfortably below the uint64_t boundary succeed
def test_xmr_to_atomic_units_near_uint64_max_boundary(self) -> None:
uint64_max: int = 2 ** 64 - 1
boundary_xmr: float = uint64_max / 1e12 # ~18446744.073709551615 XMR
margin_xmr: float = 1.0

safely_below: float = boundary_xmr - margin_xmr
below: int = MoneroUtils.xmr_to_atomic_units(safely_below)
logger.debug(f"xmr_to_atomic_units({safely_below!r}) = {below} (uint64_max = {uint64_max})")
assert below <= uint64_max

safely_above: float = boundary_xmr + margin_xmr
with pytest.raises(RuntimeError, match="amount exceeds maximum representable atomic units"):
MoneroUtils.xmr_to_atomic_units(safely_above)
Loading
Loading