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
64 changes: 62 additions & 2 deletions src/cpp/wallet/py_monero_wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,71 @@ class PyMoneroWalletListener : public monero_wallet_listener {
}

void on_output_received(const monero_output_wallet& output) override {
PYBIND11_OVERRIDE(void, monero_wallet_listener, on_output_received, output);
py::gil_scoped_acquire gil;
py::function override = py::get_override(static_cast<const monero_wallet_listener*>(this), "on_output_received");
if (override) override(copy_notified_output(output));
else monero_wallet_listener::on_output_received(output);
}

void on_output_spent(const monero_output_wallet& output) override {
PYBIND11_OVERRIDE(void, monero_wallet_listener, on_output_spent, output);
py::gil_scoped_acquire gil;
py::function override = py::get_override(static_cast<const monero_wallet_listener*>(this), "on_output_spent");
if (override) override(copy_notified_output(output));
else monero_wallet_listener::on_output_spent(output);
}

private:

/**
* The wallets notify a reference to an output of a tx that they free after the notification, and pybind11 would give python an
* isolated copy of the output, which is not one of the inputs or outputs of its tx. The listener gets the output of a copy of the tx
* instead: `output in output.tx.inputs` (or `outputs`) holds, and the tx stays as it was notified after the wallet frees it.
*
* @param output notified output
* @return the output of a copy of its tx, or a copy of the output if it is not one of the inputs or outputs of its tx
*/
static std::shared_ptr<monero_output_wallet> copy_notified_output(const monero_output_wallet& output) {
std::shared_ptr<monero_tx_wallet> tx = std::dynamic_pointer_cast<monero_tx_wallet>(output.m_tx);
if (tx == nullptr) return std::make_shared<monero_output_wallet>(output);

// find the output in its tx
bool is_input = true;
auto index = find_output(tx->m_inputs, output);
if (index == boost::none) {
is_input = false;
index = find_output(tx->m_outputs, output);
}
if (index == boost::none) return std::make_shared<monero_output_wallet>(output);

// copy the tx, which does not copy its block: the copy gets one with the same header
std::shared_ptr<monero_tx_wallet> tx_copy = tx->copy(tx, std::make_shared<monero_tx_wallet>());
if (tx->m_block != nullptr) {
std::shared_ptr<monero_block> block_copy = std::make_shared<monero_block>();
tx->m_block->monero_block_header::copy(tx->m_block, block_copy);
block_copy->m_txs.push_back(tx_copy);
tx_copy->m_block = block_copy;
}

// like monero-java, which rebuilds the notified tx in its jni layer, the tx is confirmed if it has a height and in the pool otherwise.
// Only the fields that the wallet did not set: the rpc wallet notifies the txs that it gets from get_txs(), which have them
// TODO monero-cpp: the full wallet notifies txs without m_is_confirmed and m_in_tx_pool (monero_wallet_full.cpp notify_outputs()),
// set them there and remove this
if (tx_copy->m_is_confirmed == boost::none) {
bool is_confirmed = tx_copy->get_height() != boost::none;
tx_copy->m_is_confirmed = is_confirmed;
if (tx_copy->m_in_tx_pool == boost::none) tx_copy->m_in_tx_pool = !is_confirmed;
if (is_confirmed && tx_copy->m_is_failed == boost::none) tx_copy->m_is_failed = false;
}

const std::vector<std::shared_ptr<monero_output>>& outputs_copy = is_input ? tx_copy->m_inputs : tx_copy->m_outputs;
return std::static_pointer_cast<monero_output_wallet>(outputs_copy[index.get()]);
}

static boost::optional<size_t> find_output(const std::vector<std::shared_ptr<monero_output>>& outputs, const monero_output_wallet& output) {
for (size_t i = 0; i < outputs.size(); i++) {
if (outputs[i].get() == &output) return i;
}
return boost::none;
}
};

Expand Down
4 changes: 4 additions & 0 deletions src/monero/monero_wallet_rpc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class MoneroWalletRpc(MoneroWallet):
"""
Implements a Monero wallet using `monero-wallet-rpc`_.

Note: the listeners are notified by polling the server every sync period. The outputs of the txs in the pool are not notified
yet: the outputs spent and received by a tx are notified when it is confirmed and when it unlocks. The balance notifications
include the txs in the pool.

.. _monero-wallet-rpc: https://docs.getmonero.org/rpc-library/wallet-rpc/
"""

Expand Down
2 changes: 2 additions & 0 deletions tests/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from .fake_daemon_rpc_server import FakeDaemonRpcServer
from .fake_wallet_rpc_server import FakeWalletRpcServer
from .wallet_notification_collector import WalletNotificationCollector
from .wallet_notifications_tester import WalletNotificationsTester
from .submit_then_relay_tx_tester import SubmitThenRelayTxTester
from .multisig_sample_code_tester import MultisigSampleCodeTester
from .wallet_sync_tester import WalletSyncTester
Expand Down Expand Up @@ -89,6 +90,7 @@
'WalletType',
'ViewOnlyAndOfflineWalletTester',
'WalletNotificationCollector',
'WalletNotificationsTester',
'SubmitThenRelayTxTester',
'MultisigSampleCodeTester',
'WalletSyncTester',
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/wallet_notification_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def on_balances_changed(self, new_balance: int, new_unlocked_balance: int) -> No
if num_balance_notifications > 0:
last_notification: tuple[int, int] = self.balance_notifications[num_balance_notifications - 1]
# test that balances change
assert new_balance != last_notification[0] or new_balance != last_notification[1]
assert new_balance != last_notification[0] or new_unlocked_balance != last_notification[1]

# collect balance notification
self.balance_notifications.append((new_balance, new_unlocked_balance))
Expand Down
Loading
Loading