From e30c911cd9567e4d16696fe511b8ea4c6663f09a Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Sun, 22 Nov 2026 18:47:12 +0100 Subject: [PATCH 1/3] bindings: forward the timeout of the json, path and binary requests Co-authored-by: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> --- src/cpp/common/py_monero_common_bindings.cpp | 18 +++++++++--------- tests/common/test_rpc_connection.py | 20 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/cpp/common/py_monero_common_bindings.cpp b/src/cpp/common/py_monero_common_bindings.cpp index f87e92e..4d06773 100644 --- a/src/cpp/common/py_monero_common_bindings.cpp +++ b/src/cpp/common/py_monero_common_bindings.cpp @@ -327,23 +327,23 @@ void py_monero_bind_common(py::module_& m, PyMoneroTypes& t) { .def("check_connection", [](monero_rpc_connection& self, const boost::optional& timeout_ms) { MONERO_CATCH_AND_RETHROW(self.check_connection(timeout_ms)); }, py::arg("timeout_ms") = py::none(), py::call_guard()) - .def("send_json_request", [](monero_rpc_connection& self, const std::string &method, const boost::optional& parameters) { + .def("send_json_request", [](monero_rpc_connection& self, const std::string &method, const boost::optional& parameters, const boost::optional& timeout_ms) { monero_rpc_request request(method, std::make_shared(parameters)); - auto response = self.send_json_request(request); + auto response = self.send_json_request(request, timeout_ms); boost::optional res; if (response.m_result != boost::none) res = PyGenUtils::ptree_to_pyobject(*response.m_result); return res; - }, py::arg("method"), py::arg("parameters") = py::none()) - .def("send_path_request", [](monero_rpc_connection& self, const std::string &method, const boost::optional& parameters) { + }, py::arg("method"), py::arg("parameters") = py::none(), py::arg("timeout_ms") = py::none()) + .def("send_path_request", [](monero_rpc_connection& self, const std::string &method, const boost::optional& parameters, const boost::optional& timeout_ms) { monero_rpc_request request(method, std::make_shared(parameters), false); - auto response = self.send_path_request(request); + auto response = self.send_path_request(request, timeout_ms); boost::optional res; if (response.m_response != boost::none) res = PyGenUtils::ptree_to_pyobject(*response.m_response); return res; - }, py::arg("method"), py::arg("parameters") = py::none()) - .def("send_binary_request", [](monero_rpc_connection& self, const std::string &method, const boost::optional& parameters) { + }, py::arg("method"), py::arg("parameters") = py::none(), py::arg("timeout_ms") = py::none()) + .def("send_binary_request", [](monero_rpc_connection& self, const std::string &method, const boost::optional& parameters, const boost::optional& timeout_ms) { monero_rpc_request request(method, std::make_shared(parameters), false); - auto response = self.send_binary_request(request); + auto response = self.send_binary_request(request, timeout_ms); boost::optional result; if (response.m_binary != boost::none && !response.m_binary->empty()) { @@ -351,5 +351,5 @@ void py_monero_bind_common(py::module_& m, PyMoneroTypes& t) { } // convert binary string to py::bytes return result; - }, py::arg("method"), py::arg("parameters") = py::none()); + }, py::arg("method"), py::arg("parameters") = py::none(), py::arg("timeout_ms") = py::none()); } diff --git a/tests/common/test_rpc_connection.py b/tests/common/test_rpc_connection.py index 698ec41..0678d9c 100644 --- a/tests/common/test_rpc_connection.py +++ b/tests/common/test_rpc_connection.py @@ -1,10 +1,10 @@ import pytest import logging -from monero import MoneroRpcConnection, MoneroConnectionType, MoneroRpcError, MoneroUtils +from monero import MoneroRpcConnection, MoneroConnectionType, MoneroError, MoneroRpcError, MoneroUtils from utils import ( TestUtils as Utils, RpcConnectionUtils, - StringUtils, BaseTestClass + StringUtils, BaseTestClass, FakeDaemonRpcServer ) logger: logging.Logger = logging.getLogger("TestMoneroRpcConnection") @@ -272,3 +272,19 @@ def test_send_path_request(self, node_connection: MoneroRpcConnection) -> None: assert exc_info.value.code == 404 #endregion + + +@pytest.mark.unit +class TestMoneroRpcConnectionTimeout(BaseTestClass): + """Rpc connection request timeout unit tests, on a local rpc server that answers late.""" + + # Can apply the timeout of a request to the request + @pytest.mark.parametrize("kind", ["json", "path", "binary"]) + def test_send_request_timeout(self, kind: str) -> None: + # the bindings hold the gil for the whole request, so the server in this process answers only after the timeout: enough to see + # which timeout the request got + with FakeDaemonRpcServer(response_delay=0.5) as server: + connection: MoneroRpcConnection = MoneroRpcConnection(server.uri) + send = {"json": connection.send_json_request, "path": connection.send_path_request, "binary": connection.send_binary_request}[kind] + with pytest.raises(MoneroError, match=r"Network error: no response from .* \(timeout: 100ms\)"): + send("get_last_block_header", timeout_ms=100) From 64c844a3798c6ba20c7dee9400339f6a82e041f7 Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Sun, 22 Nov 2026 18:47:12 +0100 Subject: [PATCH 2/3] bindings: forward refresh_after_import to the python overrides of import_multisig_hex Co-authored-by: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> --- src/cpp/wallet/py_monero_wallet.h | 2 +- tests/wallet/test_interface.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/cpp/wallet/py_monero_wallet.h b/src/cpp/wallet/py_monero_wallet.h index 87e1c8a..e189099 100644 --- a/src/cpp/wallet/py_monero_wallet.h +++ b/src/cpp/wallet/py_monero_wallet.h @@ -619,7 +619,7 @@ class PyMoneroWallet : public monero_wallet { } int import_multisig_hex(const std::vector& multisig_hexes, bool refresh_after_import = true) override { - PYBIND11_OVERRIDE(int, monero_wallet, import_multisig_hex, multisig_hexes); + PYBIND11_OVERRIDE(int, monero_wallet, import_multisig_hex, multisig_hexes, refresh_after_import); } monero_multisig_sign_result sign_multisig_tx_hex(const std::string& multisig_tx_hex) override { diff --git a/tests/wallet/test_interface.py b/tests/wallet/test_interface.py index f94dd1d..c13dacb 100644 --- a/tests/wallet/test_interface.py +++ b/tests/wallet/test_interface.py @@ -1,6 +1,8 @@ import pytest import logging +from typing_extensions import override + from monero import MoneroWallet, MoneroTxWallet, MoneroMessageSignatureResult from utils import WalletUtils, BaseTestClass, WalletCall, WALLET_CALLS @@ -54,4 +56,23 @@ def test_verify_message(self, wallet: MoneroWallet) -> None: result: MoneroMessageSignatureResult = wallet.verify_message("", "", "") WalletUtils.test_message_signature_result(result, False) + # A python override of import_multisig_hex gets the refresh flag of the caller + def test_import_multisig_hex_forwards_refresh_after_import(self) -> None: + forwarded: list[bool] = [] + + class MultisigWallet(MoneroWallet): + @override + def import_multisig_hex(self, multisig_hexes: list[str], refresh_after_import: bool = True) -> int: + forwarded.append(refresh_after_import) + return 42 + + wallet: MoneroWallet = MultisigWallet() + + # the binding dispatches to the override through the trampoline + assert MoneroWallet.import_multisig_hex(wallet, ["ab"], False) == 42 + assert MoneroWallet.import_multisig_hex(wallet, ["cd"], True) == 42 + assert MoneroWallet.import_multisig_hex(wallet, ["ef"]) == 42 + assert MoneroWallet.import_multisig_hex(wallet, multisig_hexes=["gh"], refresh_after_import=False) == 42 + assert forwarded == [False, True, True, False] + #endregion From c5ddaad793bcf903c7df52a38a012818145c9100 Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Sun, 22 Nov 2026 18:47:12 +0100 Subject: [PATCH 3/3] bindings: keep the back reference of a tx query assigned again to the same query Co-authored-by: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> --- src/cpp/wallet/py_monero_wallet_bindings.cpp | 6 ++++-- tests/wallet/model/test_output_query.py | 10 ++++++++++ tests/wallet/model/test_transfer_query.py | 9 +++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/cpp/wallet/py_monero_wallet_bindings.cpp b/src/cpp/wallet/py_monero_wallet_bindings.cpp index a4a18a5..bff4e06 100644 --- a/src/cpp/wallet/py_monero_wallet_bindings.cpp +++ b/src/cpp/wallet/py_monero_wallet_bindings.cpp @@ -266,7 +266,8 @@ void py_monero_bind_wallet(py::module_& m, PyMoneroTypes& t) { } else self->m_tx_query = nullptr; - if (old_query != nullptr) { + // the same query again keeps the back reference that it was just given + if (old_query != nullptr && old_query != val) { old_query->m_transfer_query = nullptr; } }) @@ -322,7 +323,8 @@ void py_monero_bind_wallet(py::module_& m, PyMoneroTypes& t) { } else { self->m_tx_query = nullptr; } - if (old_query != nullptr) { + // the same query again keeps the back reference that it was just given + if (old_query != nullptr && old_query != val) { if (output_query) old_query->m_output_query = nullptr; else old_query->m_input_query = nullptr; } diff --git a/tests/wallet/model/test_output_query.py b/tests/wallet/model/test_output_query.py index 925a311..1353ea8 100644 --- a/tests/wallet/model/test_output_query.py +++ b/tests/wallet/model/test_output_query.py @@ -71,6 +71,16 @@ def test_input_query(self) -> None: assert input_query.tx_query is None assert tx_query.input_query is None + # Can assign the same tx query again, which keeps its back reference + @pytest.mark.parametrize("output_query", [True, False]) + def test_output_query_reassign_same_tx_query(self, output_query: bool) -> None: + query: MoneroOutputQuery = MoneroOutputQuery() + tx_query: MoneroTxQuery = MoneroTxQuery() + query.set_tx_query(tx_query, output_query) + query.set_tx_query(tx_query, output_query) + assert query.tx_query == tx_query + assert (tx_query.output_query if output_query else tx_query.input_query) == query + def test_output_query_meets_criteria_matches_unconstrained_output(self) -> None: output: MoneroOutputWallet = MoneroOutputWallet() output.amount = 1 diff --git a/tests/wallet/model/test_transfer_query.py b/tests/wallet/model/test_transfer_query.py index 0658430..c87e0a7 100644 --- a/tests/wallet/model/test_transfer_query.py +++ b/tests/wallet/model/test_transfer_query.py @@ -65,6 +65,15 @@ def test_transfer_query(self) -> None: assert transfer_query.incoming is False assert transfer_query.is_incoming() is False + # Can assign the same tx query again, which keeps its back reference + def test_transfer_query_reassign_same_tx_query(self) -> None: + transfer_query: MoneroTransferQuery = MoneroTransferQuery() + tx_query: MoneroTxQuery = MoneroTxQuery() + transfer_query.tx_query = tx_query + transfer_query.tx_query = tx_query + assert transfer_query.tx_query == tx_query + assert tx_query.transfer_query == transfer_query + def test_transfer_query_meets_criteria_matches_unconstrained_transfer(self) -> None: transfer: MoneroIncomingTransfer = MoneroIncomingTransfer() transfer.amount = 100