Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
22105e0
tests: time wait_for with perf_counter
everoddandeven Nov 14, 2026
ca053bf
tests: import override from typing_extensions
everoddandeven Nov 14, 2026
ed105fc
tests: log how long each test took
everoddandeven Nov 14, 2026
446bb45
build: use ccache in build_libmonero_python.sh when it is installed
everoddandeven Nov 14, 2026
bb37bad
tests: remove the f of the strings that have no placeholders
everoddandeven Nov 14, 2026
5c18cec
tests: fix the docstring of the fixture of the full wallet
everoddandeven Nov 14, 2026
6283a43
build: quote the variables of build_libmonero_python.sh and drop the …
everoddandeven Nov 14, 2026
c4b820d
tests: do not give fixed names to the containers of the test environment
everoddandeven Nov 14, 2026
7776eea
docs: say which pytest to run, the one of apt prints a deprecation wa…
everoddandeven Nov 14, 2026
1b2f3d1
tests: rescan the spent outputs of the rpc wallet when a test class g…
everoddandeven Nov 15, 2026
c5ba932
build: use Release when the module is configured without a build type
everoddandeven Nov 15, 2026
ea19918
tests: rescan the spent outputs of the rpc wallet before each of its …
everoddandeven Nov 15, 2026
05859b7
tests: check that a wallet rpc is destroyed while its poller waits fo…
everoddandeven Nov 15, 2026
93be1a6
tests: expect the rpc wallet to say that it is closed, the workaround…
everoddandeven Nov 15, 2026
6293d08
tests: allow a tick of the timer in the wait_for test on Windows, sle…
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
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ endif()

option(ENABLE_COVERAGE "Enable coverage" OFF)

# A build without a type is not optimized (no -O3, and asserts on): `cmake ..` is what the README and bin/build_libmonero_python.sh run,
# while the wheels and the packages are built as Release. Coverage has its own flags below, and a debug build is asked with a type
get_property(MONERO_PYTHON_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(NOT MONERO_PYTHON_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE AND NOT ENABLE_COVERAGE)
message(STATUS "No CMAKE_BUILD_TYPE given, using Release")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

if(ENABLE_COVERAGE)
message(STATUS "Coverage enabled")
add_compile_options(--coverage -O0 -g -fprofile-update=atomic)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ For example: `export LD_PRELOAD=/path/to/libjemalloc.so.2` then run your app. It
pip install -r tests/requirements.txt
```
The `monero` module has to be importable in it: install it in the environment (`pip install .`), or set `PYTHONPATH` to the folder that has the built module.

Run `pytest` from that environment, or as `python3 -m pytest`. The `/usr/bin/pytest` of the `python3-pytest` package of apt calls `pytest.console_main()`, which pytest 9 deprecates, and with a newer pytest installed with pip it prints a `PytestRemovedIn10Warning`. The `pytest` that pip makes in the environment does not.
2. Clone the project repository:
```bash
git clone --recurse-submodules https://github.com/libmonero/monero-python.git
Expand Down
20 changes: 14 additions & 6 deletions bin/build_libmonero_python.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#!/bin/sh
#!/usr/bin/env bash

# ccache for the compilations when it is installed, the plain build otherwise. monero-project and the module look for it themselves
# (USE_CCACHE in their CMakeLists.txt), monero-cpp has to be told to use it
CMAKE_CCACHE_ARGS=()
if command -v ccache > /dev/null 2>&1; then
echo "Using ccache: $(command -v ccache)"
CMAKE_CCACHE_ARGS=(-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache)
fi

# build monero-project dependencies
cd ./external/monero-cpp/external/monero-project/ || exit 1
git submodule update --init --force || exit 1
HOST_NCORES=$(nproc 2>/dev/null || shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 1)
HOST_NCORES=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 1)
if [[ $(uname -s) == "MINGW64_NT"* || $(uname -s) == "MSYS"* ]]; then
bit=$(getconf LONG_BIT)
FOLDER=$(cd ${MINGW_PREFIX}/.. && pwd -W)
FOLDER=$(cd "${MINGW_PREFIX}"/.. && pwd -W)
if [ "$bit" == "64" ]; then
cmake -G "MSYS Makefiles" \
-D STATIC=ON \
Expand Down Expand Up @@ -37,14 +45,14 @@ else
mkdir -p build/release &&
cd build/release &&
cmake -DCMAKE_BUILD_TYPE=Release ../../
make -j$HOST_NCORES wallet cryptonote_protocol || exit 1
make -j"$HOST_NCORES" wallet cryptonote_protocol || exit 1
fi
cd ../../../../

# build libmonero-cpp shared library
mkdir -p build &&
cd build &&
cmake .. &&
cmake .. "${CMAKE_CCACHE_ARGS[@]}" &&
cmake --build . &&
make .
cd ../../../
Expand All @@ -53,5 +61,5 @@ cd ../../../
mkdir -p build &&
cd build &&
cmake ..
make -j$HOST_NCORES
make -j"$HOST_NCORES"

15 changes: 10 additions & 5 deletions tests/common/test_gen_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import logging
import re
import sys
import time

from monero import GenUtils
Expand All @@ -26,16 +27,20 @@ def test_get_uuid_format_and_uniqueness(self) -> None:
assert uuid1 != uuid2

def test_wait_for_blocks_for_at_least_duration(self) -> None:
start: float = time.monotonic()
# wait_for is std::this_thread::sleep_for in C++. On Windows it follows the timer of the system, which ticks every 15.6 ms, and it
# can return up to a tick before the time that was asked, however it is measured (49.8 ms for wait_for(50) was seen in CI,
# with perf_counter too). Everywhere else it is not shorter.
tolerance_ms: float = 16 if sys.platform == "win32" else 0
start: float = time.perf_counter()
GenUtils.wait_for(50)
elapsed_ms: float = (time.monotonic() - start) * 1000
elapsed_ms: float = (time.perf_counter() - start) * 1000
logger.debug(f"wait_for(50) actually took {elapsed_ms:.1f} ms")
assert elapsed_ms >= 50
assert elapsed_ms >= 50 - tolerance_ms

def test_wait_for_zero_does_not_block(self) -> None:
start: float = time.monotonic()
start: float = time.perf_counter()
GenUtils.wait_for(0)
elapsed_ms: float = (time.monotonic() - start) * 1000
elapsed_ms: float = (time.perf_counter() - start) * 1000
assert elapsed_ms < 50

def test_wait_for_negative_raises(self) -> None:
Expand Down
6 changes: 0 additions & 6 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ services:
# The dev container is not used, it is just handy to run `docker-compose up dev` to start all services
dev:
image: alpine:3.22.1
container_name: dev
command: [ "/bin/sh", "-c", "trap : TERM INT; while :; do echo Ready to code and debug like a rockstar!!!; sleep 2073600; done & wait" ]
depends_on:
- node_2
Expand All @@ -13,7 +12,6 @@ services:

node_1:
image: ghcr.io/libmonero/monero-daemon-rpc:${MONERO_TAG:-master}
container_name: node_1
command: [
"--fixed-difficulty=500",
"--log-level=3",
Expand Down Expand Up @@ -42,7 +40,6 @@ services:

node_2:
image: ghcr.io/libmonero/monero-daemon-rpc:${MONERO_TAG:-master}
container_name: node_2
command: [
"--fixed-difficulty=500",
"--log-level=3",
Expand Down Expand Up @@ -78,7 +75,6 @@ services:

xmr_wallet_1:
image: ghcr.io/libmonero/monero-wallet-rpc:${MONERO_TAG:-master}
container_name: xmr_wallet_1
command: [
"--log-level=3",
"--allow-mismatched-daemon-version",
Expand All @@ -104,7 +100,6 @@ services:

xmr_wallet_2:
image: ghcr.io/libmonero/monero-wallet-rpc:${MONERO_TAG:-master}
container_name: xmr_wallet_2
command: [
"--log-level=3",
"--allow-mismatched-daemon-version",
Expand All @@ -130,7 +125,6 @@ services:

xmr_wallet_3:
image: ghcr.io/libmonero/monero-wallet-rpc:${MONERO_TAG:-master}
container_name: xmr_wallet_3
command: [
"--log-level=3",
"--allow-mismatched-daemon-version",
Expand Down
4 changes: 3 additions & 1 deletion tests/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .view_only_and_offline_wallet_tester import ViewOnlyAndOfflineWalletTester
from .daemon_notification_collector import DaemonNotificationCollector
from .fake_daemon_rpc_server import FakeDaemonRpcServer
from .fake_wallet_rpc_server import FakeWalletRpcServer
from .wallet_notification_collector import WalletNotificationCollector
from .submit_then_relay_tx_tester import SubmitThenRelayTxTester
from .multisig_sample_code_tester import MultisigSampleCodeTester
Expand Down Expand Up @@ -103,5 +104,6 @@
'WalletTestUtils',
'TxsStructureTester',
'DaemonNotificationCollector',
'FakeDaemonRpcServer'
'FakeDaemonRpcServer',
'FakeWalletRpcServer'
]
17 changes: 16 additions & 1 deletion tests/utils/base_test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from abc import ABC
from time import perf_counter
from typing import Any, Generator
from monero import MoneroUtils

Expand All @@ -11,6 +12,9 @@
class BaseTestClass(ABC):
"""Base test class with common fixtures."""

_elapsed_ms: float = 0.0
"""Milliseconds that the last test took, set when the test ends and before `after_each`."""

# Setup and teardown of test class
@pytest.fixture(scope="class", autouse=True)
@classmethod
Expand All @@ -36,7 +40,10 @@ def setup_and_teardown(self, request: pytest.FixtureRequest) -> Generator[None,
teardown once the test has completed.
"""
self.before_each(request)
# the time of the test itself: after what before_each does, until the test ends
started: float = perf_counter()
yield
self._elapsed_ms = (perf_counter() - started) * 1000
self.after_each(request)

# Before all tests
Expand Down Expand Up @@ -69,6 +76,14 @@ def after_each(self, request: pytest.FixtureRequest) -> None:

:param pytest.FixtureRequest request: Request fixture.
"""
msg: str = f"After {request.node.name}" # type: ignore
msg: str = self.after_message(request)
MoneroUtils.log_info(msg)
logger.info(msg)

def after_message(self, request: pytest.FixtureRequest) -> str:
"""The message that says that a test ended, with the time that it took.

:param pytest.FixtureRequest request: Request fixture.
:returns str: `After <test> (<milliseconds> ms)`.
"""
return f"After {request.node.name} ({self._elapsed_ms:.1f} ms)" # type: ignore
2 changes: 1 addition & 1 deletion tests/utils/daemon_notification_collector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from typing import override
from typing_extensions import override

from monero import MoneroDaemonRpc, MoneroDaemonListener, MoneroBlockHeader

Expand Down
130 changes: 29 additions & 101 deletions tests/utils/fake_daemon_rpc_server.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,40 @@
import json
import logging
import threading

from time import sleep

from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from typing import Any, override

logger: logging.Logger = logging.getLogger("FakeDaemonRpcServer")
from .fake_rpc_server import FakeRpcHandler, FakeRpcServer


class _Handler(BaseHTTPRequestHandler):
class _Handler(FakeRpcHandler):
"""Answers the json rpc requests of a daemon poller, with a new block at each request."""

server: "FakeDaemonRpcServer" # type: ignore[assignment]

def do_POST(self) -> None:
"""Answer a request with the last block header, a block newer than the one of the previous request."""
try:
length: int = int(self.headers.get("Content-Length", "0"))
body: dict[str, Any] = json.loads(self.rfile.read(length) or b"{}")
height: int = self.server.next_height()
if self.server.response_delay > 0:
sleep(self.server.response_delay)
response: dict[str, Any] = {
"jsonrpc": "2.0",
"id": body.get("id", "0"),
"result": {
"status": "OK",
"untrusted": False,
"block_header": {
"hash": f"{height:064x}",
"prev_hash": f"{height - 1:064x}",
"miner_tx_hash": f"{height + 1:064x}",
"height": height,
"major_version": 16,
"minor_version": 16,
"timestamp": 1700000000 + height,
"nonce": 1,
"orphan_status": False,
"depth": 0,
"difficulty": 5,
"cumulative_difficulty": 5 * height,
"reward": 600000000000,
"block_size": 100,
"block_weight": 100,
"num_txes": 0,
"long_term_weight": 100,
"pow_hash": ""
}
}
}
data: bytes = json.dumps(response).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
except Exception as e:
# a request that is not json (the daemon connection probes the server too)
logger.debug(f"Request not answered: {e}")
self.send_error(404)

@override
def log_message(self, format: str, *args: Any) -> None:
pass
def build_result(self, height: int) -> dict[str, Any]:
return {
"status": "OK",
"untrusted": False,
"block_header": {
"hash": f"{height:064x}",
"prev_hash": f"{height - 1:064x}",
"miner_tx_hash": f"{height + 1:064x}",
"height": height,
"major_version": 16,
"minor_version": 16,
"timestamp": 1700000000 + height,
"nonce": 1,
"orphan_status": False,
"depth": 0,
"difficulty": 5,
"cumulative_difficulty": 5 * height,
"reward": 600000000000,
"block_size": 100,
"block_weight": 100,
"num_txes": 0,
"long_term_weight": 100,
"pow_hash": ""
}
}


class FakeDaemonRpcServer(ThreadingHTTPServer):
class FakeDaemonRpcServer(FakeRpcServer):
"""A daemon rpc server for unit tests, that needs no node: every request for the last block header gets a new block,
so the poller of a `MoneroDaemonRpc` notifies its listeners at each poll.

Expand All @@ -80,43 +47,4 @@ def __init__(self, first_height: int = 100, response_delay: float = 0.0) -> None
:param int first_height: height of the first block.
:param float response_delay: seconds that the server takes to answer a request, to have requests in flight.
"""
super().__init__(("127.0.0.1", 0), _Handler)
self._first_height: int = first_height
self._height: int = first_height
self.response_delay: float = response_delay
self._lock: threading.Lock = threading.Lock()
self._thread: threading.Thread = threading.Thread(target=self.serve_forever, daemon=True)

@property
def uri(self) -> str:
"""Uri of the server.

:returns str: uri to give to a `MoneroDaemonRpc`.
"""
return f"http://127.0.0.1:{self.server_address[1]}"

@property
def num_requests(self) -> int:
"""Number of requests received so far.

:returns int: number of requests received.
"""
return self._height - self._first_height

def next_height(self) -> int:
"""Get the height of the block of the next response.

:returns int: new block height.
"""
with self._lock:
self._height += 1
return self._height

def __enter__(self) -> "FakeDaemonRpcServer":
self._thread.start()
return self

def __exit__(self, *args: Any) -> None:
self.shutdown()
self.server_close()
self._thread.join(timeout=5)
super().__init__(_Handler, first_height, response_delay)
Loading
Loading