Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MyWalletListener(MoneroWalletListener):
assert tx is not None # the wallet gives the output with its transaction
tx_hash: str | None = tx.hash
is_confirmed: bool | None = tx.is_confirmed
is_locked: bool | None = tx.is_locked
is_locked: bool | None = output.is_locked

# connect to daemon
daemon: MoneroDaemon = MoneroDaemonRpc("http://localhost:38081", "superuser", "abctesting123")
Expand Down
20 changes: 9 additions & 11 deletions docs/mkdocs/docs/en/api/equality.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ as one is alive, so the back references are the objects that they point to:
```python
for output in tx.outputs:
assert output.tx is tx
assert tx.block is None or tx in tx.block.txs
assert tx.block is None or any(block_tx is tx for block_tx in tx.block.txs)
```

When no Python object is alive, the next access makes a new one: the `id()` of a temporary object can change from an access to the
Expand All @@ -66,19 +66,17 @@ if block is not None and id(block) not in seen_blocks:
## Lists of the model

The fields that hold a list of objects of the model, as `block.txs`, `tx.inputs`, `tx.outputs`, `tx.incoming_transfers` or
`tx_set.txs`, are the vectors of C++ (`VectorMoneroTx`, `VectorMoneroOutput`...), and what the vector does compares its objects
**by identity**: `in`, `==`, `index`, `remove` and `count`. A `list` compares them by value:

| | a vector, `tx.outputs` | a `list`, `list(tx.outputs)` |
| --- | --- | --- |
| `output in ...` | the same object is there | an equal object is there |
| `tx1.outputs == tx2.outputs` | the same objects, in the same order | equal objects, in the same order |
`tx_set.txs`, and the lists that the wallets return, as `get_account_tags()`, are the vectors of C++ (`VectorMoneroTx`,
`VectorMoneroOutput`...). They compare their objects **by value**, as a `list` does and as `contains` and `equals` of a `List` of
monero-java: `in`, `index`, `count` and `remove` look for an equal object, and a vector equals a vector or a `list` with equal objects in
the same order. An object is compared by its JSON, so `in` looks for the same object first, which costs nothing: `tx in tx.block.txs`
does not serialize the txs of the block. Their items are still the objects that they hold:

```python
copy: MoneroOutputWallet = output.copy()
assert copy == output
assert copy not in output.tx.outputs # not the same object
assert copy in list(output.tx.outputs) # an equal one
assert copy in output.tx.outputs # an equal one
assert any(tx_output is output for tx_output in output.tx.outputs) # the same object
```

## Representation
Expand All @@ -96,7 +94,7 @@ that they do not end up in a log. `serialize()` keeps them.
| `a.equals(b)`, `assertEquals(a, b)` | `a == b` | the same values |
| `equals` of each class, field by field | `serialize()` of the two objects | the fields that are not serialized are not compared |
| `hashCode()`, `Set<MoneroBlock>`, `Map<MoneroTx, ...>` | not hashable | key by the hash, or by `id()` |
| `list.contains(x)`, with `equals` | `x in vector` by identity, `x in list(vector)` by value | the vectors of the model |
| `list.contains(x)`, with `equals` | `x in vector` | by value, as a `list` |
| `toString()`, an indented text | `repr()`, the JSON on one line | with the secrets hidden |

## Porting a test from monero-java
Expand Down
2 changes: 1 addition & 1 deletion docs/mkdocs/docs/en/tests/files/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ What is not about a wallet or a daemon.
| File | Marker | What it tests |
| --- | --- | --- |
| `test_common.py` | unit | `MoneroError`, the serialization of the structs, the SSL options |
| `test_serializable_struct.py` | unit | the equality, the hash and the `repr` of the structs, of `MoneroRpcConnection` and of `MoneroWalletConfig` |
| `test_serializable_struct.py` | unit | the equality, the hash and the `repr` of the structs, the equality of their vectors, of `MoneroRpcConnection` and of `MoneroWalletConfig` |
| `test_rpc_connection.py` | integration | `MoneroRpcConnection`: the validation of the uri, the copy and the serialization, the credentials, the priority, the connections to a node and to a wallet rpc server, the json, binary and path requests |
| `test_gen_utils.py` | unit | the helpers of the tests: the uuids, the waits, and the reconciliation of the values of two models (the rules of `merge`) |
| `test_block_generation.py` | unit | the choice of the tests between generating blocks (at a low difficulty) and mining them |
Expand Down
2 changes: 1 addition & 1 deletion external/monero-cpp
Submodule monero-cpp updated 55 files
+10 −0 .github/workflows/build.yml
+1 −1 README.md
+1 −1 docs/doxygen/classmonero_1_1monero__rpc__connection-members.html
+46 −3 docs/doxygen/classmonero_1_1monero__rpc__connection.html
+4 −6 docs/doxygen/classmonero_1_1monero__wallet.html
+4 −4 docs/doxygen/classmonero_1_1monero__wallet__full-members.html
+149 −8 docs/doxygen/classmonero_1_1monero__wallet__full.html
+1 −1 docs/doxygen/classmonero_1_1monero__wallet__rpc-members.html
+2 −42 docs/doxygen/classmonero_1_1monero__wallet__rpc.html
+5 −1 docs/doxygen/doxygen_crawl.html
+2 −2 docs/doxygen/functions_func_g.html
+1 −0 docs/doxygen/functions_func_i.html
+1 −1 docs/doxygen/functions_func_s.html
+1 −1 docs/doxygen/functions_func_t.html
+1 −1 docs/doxygen/functions_func_u.html
+2 −2 docs/doxygen/functions_g.html
+1 −0 docs/doxygen/functions_i.html
+1 −1 docs/doxygen/functions_s.html
+1 −1 docs/doxygen/functions_t.html
+1 −1 docs/doxygen/functions_u.html
+1 −1 docs/doxygen/monero__daemon_8h_source.html
+501 −500 docs/doxygen/monero__daemon__model_8h_source.html
+1 −1 docs/doxygen/monero__daemon__rpc__model_8h_source.html
+20 −18 docs/doxygen/monero__rpc__connection_8h_source.html
+3 −3 docs/doxygen/monero__utils_8h_source.html
+489 −486 docs/doxygen/monero__wallet_8h_source.html
+199 −191 docs/doxygen/monero__wallet__full_8h_source.html
+1 −1 docs/doxygen/monero__wallet__keys_8h_source.html
+1 −1 docs/doxygen/monero__wallet__model_8h_source.html
+204 −206 docs/doxygen/monero__wallet__rpc_8h_source.html
+483 −0 docs/doxygen/navtree.js
+36 −0 docs/doxygen/navtreedata.js
+5 −0 docs/doxygen/navtreeindex0.js
+2 −2 docs/doxygen/search/all_6.js
+17 −16 docs/doxygen/search/all_7.js
+1 −1 docs/doxygen/search/all_d.js
+1 −1 docs/doxygen/search/all_e.js
+1 −1 docs/doxygen/search/all_f.js
+2 −2 docs/doxygen/search/functions_5.js
+17 −16 docs/doxygen/search/functions_6.js
+1 −1 docs/doxygen/search/functions_b.js
+1 −1 docs/doxygen/search/functions_c.js
+1 −1 docs/doxygen/search/functions_d.js
+12 −11 docs/doxygen/structmonero_1_1monero__output-members.html
+3 −0 docs/doxygen/structmonero_1_1monero__output.html
+22 −21 docs/doxygen/structmonero_1_1monero__output__query-members.html
+3 −0 docs/doxygen/structmonero_1_1monero__output__query.html
+17 −16 docs/doxygen/structmonero_1_1monero__output__wallet-members.html
+3 −0 docs/doxygen/structmonero_1_1monero__output__wallet.html
+3 −3 docs/doxygen/structmonero_1_1monero__tx__query.html
+2 −2 src/utils/monero_utils.cpp
+1 −0 src/wallet/monero_wallet_full.cpp
+31 −14 src/wallet/monero_wallet_rpc.cpp
+66 −3 test/monero_tests.cpp
+1 −1 test/sample_code.cpp
109 changes: 83 additions & 26 deletions src/cpp/common/py_monero_common_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,63 @@
*/
#include "py_monero_types.h"

namespace {

template <typename T> struct is_shared_ptr : std::false_type {};
template <typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};

// the vectors of the objects of the model compare them by value with their __eq__, like a list and like contains() and equals() of
// monero-java: bind_vector compares their shared_ptr, so an object equal to one of the vector would not be in it. != is derived from
// __eq__ once bind_vector's is removed
template <typename Vector, typename Class_>
void compare_by_value(Class_ cl) {
py::dict attrs = cl.attr("__dict__");
for (const char* name : {"__contains__", "count", "remove", "__eq__", "__ne__"}) {
if (attrs.contains(name)) py::delattr(cl, name); // the vectors of a class without == do not have them
}
cl.def("__contains__", [](const Vector& v, const py::object& x) {
// the same object first, which does not serialize, as with the back references: tx in tx.block.txs. Only here: index() and
// remove() take the first equal object and count() all of them, which the same object found first would change
using Item = typename Vector::value_type;
if constexpr (is_shared_ptr<Item>::value) {
using Element = typename Item::element_type;
if (py::isinstance<Element>(x)) {
const Element* ptr = x.cast<const Element*>();
for (const auto& item : v) if (item.get() == ptr) return true;
}
}
for (const auto& item : v) if (py::cast(item).equal(x)) return true;
return false;
}, py::arg("x"), "Return true the container contains an object equal to ``x``");
cl.def("count", [](const Vector& v, const py::object& x) {
size_t count = 0;
for (const auto& item : v) if (py::cast(item).equal(x)) count++;
return count;
}, py::arg("x"), "Return the number of objects equal to ``x``");
cl.def("index", [](const Vector& v, const py::object& x) {
for (size_t i = 0; i < v.size(); i++) if (py::cast(v[i]).equal(x)) return i;
throw py::value_error("x not in vector");
}, py::arg("x"), "Return the index of the first object equal to ``x``");
cl.def("remove", [](Vector& v, const py::object& x) {
for (auto it = v.begin(); it != v.end(); it++) {
if (py::cast(*it).equal(x)) {
v.erase(it);
return;
}
}
throw py::value_error("x not in vector");
}, py::arg("x"), "Remove the first object equal to ``x``");
cl.def("__eq__", [](const Vector& v, const py::object& other) -> py::object {
// equal to a vector of the same type or to a list with equal objects in the same order
if (!py::isinstance<Vector>(other) && !py::isinstance<py::list>(other)) return py::reinterpret_borrow<py::object>(Py_NotImplemented);
py::sequence seq = py::reinterpret_borrow<py::sequence>(other);
if (seq.size() != v.size()) return py::bool_(false);
for (size_t i = 0; i < v.size(); i++) if (!py::cast(v[i]).equal(seq[i])) return py::bool_(false);
return py::bool_(true);
}, py::arg("other"), py::is_operator());
}
}

void py_monero_bind_vectors_and_maps(py::module_& m) {

py::bind_vector<VectorInt>(m, "VectorInt")
Expand All @@ -75,67 +132,67 @@ void py_monero_bind_vectors_and_maps(py::module_& m) {
.def("copy", [](const VectorString& v) {
return VectorString(v);
});
py::bind_vector<std::vector<std::shared_ptr<monero_account_tag>>>(m, "VectorMoneroAccountTagPtr")
compare_by_value<std::vector<std::shared_ptr<monero_account_tag>>>(py::bind_vector<std::vector<std::shared_ptr<monero_account_tag>>>(m, "VectorMoneroAccountTagPtr")
.def("copy", [](const std::vector<std::shared_ptr<monero_account_tag>>& v) {
return std::vector<std::shared_ptr<monero_account_tag>>(v);
});
py::bind_vector<std::vector<std::shared_ptr<monero_key_image>>>(m, "VectorKeyImagePtr")
}));
compare_by_value<std::vector<std::shared_ptr<monero_key_image>>>(py::bind_vector<std::vector<std::shared_ptr<monero_key_image>>>(m, "VectorKeyImagePtr")
.def("copy", [](const std::vector<std::shared_ptr<monero_key_image>>& v) {
return std::vector<std::shared_ptr<monero_key_image>>(v);
});
py::bind_vector<std::vector<std::shared_ptr<monero_block>>>(m, "VectorMoneroBlock")
}));
compare_by_value<std::vector<std::shared_ptr<monero_block>>>(py::bind_vector<std::vector<std::shared_ptr<monero_block>>>(m, "VectorMoneroBlock")
.def("copy", [](const std::vector<std::shared_ptr<monero_block>>& v) {
return std::vector<std::shared_ptr<monero_block>>(v);
});
py::bind_vector<std::vector<std::shared_ptr<monero_block_header>>>(m, "VectorMoneroBlockHeader")
}));
compare_by_value<std::vector<std::shared_ptr<monero_block_header>>>(py::bind_vector<std::vector<std::shared_ptr<monero_block_header>>>(m, "VectorMoneroBlockHeader")
.def("copy", [](const std::vector<std::shared_ptr<monero_block_header>>& v) {
return std::vector<std::shared_ptr<monero_block_header>>(v);
});
py::bind_vector<VectorMoneroTx>(m, "VectorMoneroTx")
}));
compare_by_value<VectorMoneroTx>(py::bind_vector<VectorMoneroTx>(m, "VectorMoneroTx")
.def("sort", [](VectorMoneroTx &v) {
std::sort(v.begin(), v.end(), monero_tx_height_comparator());
})
.def("copy", [](const VectorMoneroTx& v) {
return VectorMoneroTx(v);
});
py::bind_vector<VectorMoneroTxWallet>(m, "VectorMoneroTxWallet")
}));
compare_by_value<VectorMoneroTxWallet>(py::bind_vector<VectorMoneroTxWallet>(m, "VectorMoneroTxWallet")
.def("sort", [](VectorMoneroTxWallet &v) {
std::sort(v.begin(), v.end(), monero_tx_height_comparator());
})
.def("copy", [](const VectorMoneroTxWallet& v) {
return VectorMoneroTxWallet(v);
});
py::bind_vector<std::vector<std::shared_ptr<monero_output>>>(m, "VectorMoneroOutput")
}));
compare_by_value<std::vector<std::shared_ptr<monero_output>>>(py::bind_vector<std::vector<std::shared_ptr<monero_output>>>(m, "VectorMoneroOutput")
.def("copy", [](const std::vector<std::shared_ptr<monero_output>>& v) {
return std::vector<std::shared_ptr<monero_output>>(v);
});
py::bind_vector<std::vector<std::shared_ptr<monero_output_wallet>>>(m, "VectorMoneroOutputWallet")
}));
compare_by_value<std::vector<std::shared_ptr<monero_output_wallet>>>(py::bind_vector<std::vector<std::shared_ptr<monero_output_wallet>>>(m, "VectorMoneroOutputWallet")
.def("copy", [](const std::vector<std::shared_ptr<monero_output_wallet>>& v) {
return std::vector<std::shared_ptr<monero_output_wallet>>(v);
});
py::bind_vector<std::vector<std::shared_ptr<monero_transfer>>>(m, "VectorMoneroTransfer")
}));
compare_by_value<std::vector<std::shared_ptr<monero_transfer>>>(py::bind_vector<std::vector<std::shared_ptr<monero_transfer>>>(m, "VectorMoneroTransfer")
.def("copy", [](const std::vector<std::shared_ptr<monero_transfer>>& v) {
return std::vector<std::shared_ptr<monero_transfer>>(v);
});
py::bind_vector<VectorMoneroIncomingTransfer>(m, "VectorMoneroIncomingTransfer")
}));
compare_by_value<VectorMoneroIncomingTransfer>(py::bind_vector<VectorMoneroIncomingTransfer>(m, "VectorMoneroIncomingTransfer")
.def("sort", [](VectorMoneroIncomingTransfer &v) {
std::sort(v.begin(), v.end(), monero_incoming_transfer_comparator());
})
.def("copy", [](const VectorMoneroIncomingTransfer& v) {
return VectorMoneroIncomingTransfer(v);
});
py::bind_vector<VectorMoneroOutgoingTransfer>(m, "VectorMoneroOutgoingTransfer")
}));
compare_by_value<VectorMoneroOutgoingTransfer>(py::bind_vector<VectorMoneroOutgoingTransfer>(m, "VectorMoneroOutgoingTransfer")
.def("copy", [](const VectorMoneroOutgoingTransfer& v) {
return VectorMoneroOutgoingTransfer(v);
});
py::bind_vector<VectorMoneroSubaddress>(m, "VectorMoneroSubaddress")
}));
compare_by_value<VectorMoneroSubaddress>(py::bind_vector<VectorMoneroSubaddress>(m, "VectorMoneroSubaddress")
.def("copy", [](const VectorMoneroSubaddress& v) {
return VectorMoneroSubaddress(v);
});
py::bind_vector<VectorMoneroDestination>(m, "VectorMoneroDestination")
}));
compare_by_value<VectorMoneroDestination>(py::bind_vector<VectorMoneroDestination>(m, "VectorMoneroDestination")
.def("copy", [](const VectorMoneroDestination& v) {
return VectorMoneroDestination(v);
});
}));

py::implicitly_convertible<py::iterable, VectorInt>();
py::implicitly_convertible<py::iterable, VectorUint8>();
Expand Down
30 changes: 29 additions & 1 deletion tests/common/monero_utils/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any

import pytest
from monero import MoneroUtils
from monero import MoneroError, MoneroUtils

from .base_test_monero_utils import BaseTestMoneroUtils

Expand Down Expand Up @@ -90,3 +90,31 @@ def test_serialize_text_long(self, config: BaseTestMoneroUtils.Config) -> None:

json_map2: dict[Any, Any] = MoneroUtils.binary_to_dict(binary)
assert json_map == json_map2

# Can reject invalid json and binary
def test_binary_conversion_invalid(self) -> None:
json_map: dict[Any, Any] = {
"heights": [[111], [222]]
}
with pytest.raises(MoneroError) as exc_info:
MoneroUtils.dict_to_binary(json_map)
assert str(exc_info.value) == "Invalid JSON"

json_map["heights"] = [111, 222, 333]
binary: bytes = MoneroUtils.dict_to_binary(json_map)
for size in range(len(binary)):
with pytest.raises(MoneroError) as exc_info:
MoneroUtils.binary_to_dict(binary[:size])
assert str(exc_info.value) == "Invalid binary"
assert json_map == MoneroUtils.binary_to_dict(binary)

# a field name of 255 bytes is parsed but can not be serialized
with pytest.raises(MoneroError) as exc_info:
MoneroUtils.dict_to_binary({"k" * 255: 1})
assert str(exc_info.value) == "Failed to serialize binary"

# Can reject invalid binary blocks
def test_binary_blocks_invalid(self) -> None:
with pytest.raises(MoneroError) as exc_info:
MoneroUtils.binary_blocks_to_json(bytes([1, 2, 3]))
assert str(exc_info.value) == "failed to parse get_blocks_by_height.bin response"
41 changes: 40 additions & 1 deletion tests/common/test_serializable_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging

from monero import (
MoneroBlock, MoneroOutput, MoneroOutputWallet, MoneroRpcConnection, MoneroSubaddress, MoneroTx, MoneroTxWallet,
MoneroAccount, MoneroBlock, MoneroOutput, MoneroOutputWallet, MoneroRpcConnection, MoneroSubaddress, MoneroTx, MoneroTxWallet,
MoneroWalletConfig
)

Expand Down Expand Up @@ -61,6 +61,45 @@ def test_equals_children(self) -> None:
other_block.txs = [tx2]
assert block == other_block

# Can compare the structs of a vector by value, like a list and like contains() and equals() of monero-java
def test_vector_equals(self) -> None:
tx: MoneroTx = self._get_tx()
output: MoneroOutput = tx.outputs[0]
copy: MoneroOutput = output.copy()
other: MoneroOutput = output.copy()
other.amount = 2

# an equal struct is in the vector, which keeps the same objects
assert output in tx.outputs
assert copy in tx.outputs
assert other not in tx.outputs
assert "my tx hash" not in tx.outputs
assert tx.outputs[0] is output
assert tx.outputs.count(copy) == 1
assert tx.outputs.count(other) == 0
assert tx.outputs.index(copy) == 0
with pytest.raises(ValueError):
tx.outputs.index(other)

# a vector equals a vector or a list with equal structs in the same order
assert tx.outputs == self._get_tx().outputs
assert tx.outputs == [copy]
assert tx.outputs != [other]
assert tx.outputs != [copy, copy]
assert tx.outputs != (copy,)

# the vectors of structs without a shared_ptr too
account: MoneroAccount = MoneroAccount()
account.subaddresses = [self._get_subaddress()]
assert self._get_subaddress() in account.subaddresses
assert account.subaddresses == [self._get_subaddress()]

# an equal struct is removed
with pytest.raises(ValueError):
tx.outputs.remove(other)
tx.outputs.remove(copy)
assert len(tx.outputs) == 0

# Cannot hash the structs, which are mutable
@pytest.mark.parametrize("struct", [MoneroSubaddress(), MoneroTxWallet(), MoneroRpcConnection(), MoneroWalletConfig()])
def test_not_hashable(self, struct: object) -> None:
Expand Down
17 changes: 17 additions & 0 deletions tests/wallet/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
MoneroAccount,
MoneroAccountTag,
MoneroAddressBookEntry,
MoneroError,
MoneroIntegratedAddress,
MoneroSubaddress,
MoneroWallet,
Expand Down Expand Up @@ -284,6 +285,12 @@ def test_account_tags(self, wallet: MoneroWallet) -> None:
assert tagged_accounts[1].index == 1
assert tagged_accounts[1].tag == tag.tag

# tag has no label until one is set
unlabeled_tag: MoneroAccountTag = MoneroAccountTag()
unlabeled_tag.tag = tag.tag
unlabeled_tag.account_indices = tag.account_indices
assert unlabeled_tag in wallet.get_account_tags()

# set tag label
wallet.set_account_tag_label(tag.tag, tag.label)

Expand Down Expand Up @@ -313,6 +320,16 @@ def test_account_tags(self, wallet: MoneroWallet) -> None:
assert tagged_accounts[0].index == 0
assert tagged_accounts[0].tag == tag.tag

# test that tagging an account out of range fails
with pytest.raises(MoneroError) as exc_info:
wallet.tag_accounts(tag.tag, [len(accounts)])
assert str(exc_info.value) == "Account index out of bound"

# test that labeling an unregistered tag fails
with pytest.raises(MoneroError) as exc_info:
wallet.set_account_tag_label("non_existing_tag", "my label")
assert str(exc_info.value) == "Tag is unregistered"

# untag and query accounts
wallet.untag_accounts([0, 1])
assert len(wallet.get_account_tags()) == 0
Expand Down
Loading