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
80 changes: 80 additions & 0 deletions tests/common/test_wallet_tx_tracker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from __future__ import annotations

from typing import Any

import pytest
from monero import MoneroSyncResult, MoneroTx, MoneroTxQuery, MoneroTxWallet, MoneroWalletRpc

from utils import BaseTestClass, FakeWalletRpcServer, WalletTxTracker


class _Daemon:
"""The part of a daemon that the tracker uses when there is nothing to wait for."""

def get_height(self) -> int:
return 0

def get_tx_pool(self) -> list[MoneroTx]:
return []

def stop_mining(self) -> None:
pass


class _Wallet:
"""A wallet that is not a wallet rpc and has no pending txs, that counts the times that it was asked which outputs are spent."""

def __init__(self) -> None:
self.num_rescans: int = 0

def sync(self) -> MoneroSyncResult:
result: MoneroSyncResult = MoneroSyncResult()
result.num_blocks_fetched = 0
return result

def get_txs(self, query: MoneroTxQuery) -> list[MoneroTxWallet]:
return []

def get_height(self) -> int:
return 0

def rescan_spent(self) -> None:
self.num_rescans += 1


@pytest.mark.unit
class TestWalletTxTracker(BaseTestClass):
"""The tracker waits for the txs of a wallet to leave the pool, and then the wallet is fit for the next tx."""

#region Tests

# A wallet rpc is asked which of its outputs are spent, when its txs left the pool
def test_rpc_wallet_rescans_spent_outputs_after_the_pool_is_clear(self) -> None:
# the server of a wallet rpc sets back as unspent the inputs of a tx that it saw in the pool and that left it, 500 s after, and
# the next tx used them: the daemon rejected it as a double spend (test_subtract_fee_from, in CI)
tracker: WalletTxTracker = WalletTxTracker(_Daemon(), 10, "") # type: ignore[arg-type]
with FakeWalletRpcServer() as server:
wallet: MoneroWalletRpc = MoneroWalletRpc(server.uri)
tracker.wait_for_txs_to_clear_pool([wallet])
methods: list[str] = server.methods
assert "refresh" in methods, f"The wallet was not synced: {methods}"
assert "rescan_spent" in methods, f"The wallet was not asked which outputs are spent: {methods}"
assert methods.index("rescan_spent") > methods.index("refresh"), f"It was asked before it synced: {methods}"

# Every wallet rpc of a list is asked, not only the first one
def test_each_rpc_wallet_rescans_spent_outputs(self) -> None:
tracker: WalletTxTracker = WalletTxTracker(_Daemon(), 10, "") # type: ignore[arg-type]
with FakeWalletRpcServer() as server1, FakeWalletRpcServer() as server2:
wallets: list[Any] = [MoneroWalletRpc(server1.uri), MoneroWalletRpc(server2.uri)]
tracker.wait_for_txs_to_clear_pool(wallets)
assert "rescan_spent" in server1.methods
assert "rescan_spent" in server2.methods

# The other wallets are not asked: the full wallet follows its own outputs, and a wallet that is not a wallet rpc may not have it
def test_other_wallets_do_not_rescan_spent_outputs(self) -> None:
tracker: WalletTxTracker = WalletTxTracker(_Daemon(), 10, "") # type: ignore[arg-type]
wallet: _Wallet = _Wallet()
tracker.wait_for_txs_to_clear_pool([wallet]) # type: ignore[list-item]
assert wallet.num_rescans == 0

#endregion
2 changes: 1 addition & 1 deletion tests/utils/fake_daemon_rpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class _Handler(FakeRpcHandler):
"""Answers the json rpc requests of a daemon poller, with a new block at each request."""

@override
def build_result(self, height: int) -> dict[str, Any]:
def build_result(self, height: int, method: str) -> dict[str, Any]:
return {
"status": "OK",
"untrusted": False,
Expand Down
24 changes: 19 additions & 5 deletions tests/utils/fake_rpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class FakeRpcHandler(BaseHTTPRequestHandler):

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

def build_result(self, height: int) -> dict[str, Any]:
def build_result(self, height: int, method: str) -> dict[str, Any]:
"""Build the result of a response.

:param int height: height of the response, a new one at each request.
:param str method: json rpc method that was called.
:returns dict[str, Any]: result of the json rpc response.
"""
raise NotImplementedError
Expand All @@ -28,10 +29,11 @@ def do_POST(self) -> None:
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()
method: str = str(body.get("method", ""))
height: int = self.server.next_height(method)
if self.server.response_delay > 0:
sleep(self.server.response_delay)
response: dict[str, Any] = {"jsonrpc": "2.0", "id": body.get("id", "0"), "result": self.build_result(height)}
response: dict[str, Any] = {"jsonrpc": "2.0", "id": body.get("id", "0"), "result": self.build_result(height, method)}
data: bytes = json.dumps(response).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
Expand Down Expand Up @@ -67,6 +69,7 @@ def __init__(self, handler: type[FakeRpcHandler], first_height: int = 100, respo
self._height: int = first_height
self.response_delay: float = response_delay
self._lock: threading.Lock = threading.Lock()
self._methods: list[str] = []
self._thread: threading.Thread = threading.Thread(target=self.serve_forever, daemon=True)

@property
Expand All @@ -85,13 +88,24 @@ def num_requests(self) -> int:
"""
return self._height - self._first_height

def next_height(self) -> int:
"""Get the height of the next response.
@property
def methods(self) -> list[str]:
"""Json rpc methods that were called so far, in the order of the requests.

:returns list[str]: the methods.
"""
with self._lock:
return list(self._methods)

def next_height(self, method: str = "") -> int:
"""Get the height of the next response, and take note of the method that asked for it.

:param str method: json rpc method of the request.
:returns int: new height.
"""
with self._lock:
self._height += 1
self._methods.append(method)
return self._height

def __enter__(self) -> "FakeRpcServer":
Expand Down
11 changes: 8 additions & 3 deletions tests/utils/fake_wallet_rpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@


class _Handler(FakeRpcHandler):
"""Answers the json rpc requests of a wallet poller, with a new height at each request. Any method gets the same result: what
the poller asks for and the result does not have, it takes as an error of the request and polls again."""
"""Answers the json rpc requests of a wallet, with a new height at each request. The methods that a test needs have the result of a
wallet that has nothing, any other gets a result with the height: what is asked for and the result does not have, the client takes
as an error of the request."""

@override
def build_result(self, height: int) -> dict[str, Any]:
def build_result(self, height: int, method: str) -> dict[str, Any]:
if method == "refresh":
return {"blocks_fetched": 0, "received_money": False}
if method in ("get_transfers", "rescan_spent"):
return {}
return {"height": height, "balance": 0, "unlocked_balance": 0}


Expand Down
11 changes: 10 additions & 1 deletion tests/utils/wallet_tx_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from time import sleep
from monero import (
MoneroDaemon, MoneroWallet, MoneroTxQuery, MoneroSyncResult,
MoneroDaemon, MoneroWallet, MoneroWalletRpc, MoneroTxQuery, MoneroSyncResult,
MoneroTxWallet, MoneroMiningStatus
)

Expand Down Expand Up @@ -177,6 +177,15 @@ def _wait_for_txs_to_clear(self, clear_from_wallet: bool, wallets: list[MoneroWa
if result.num_blocks_fetched > 0:
logger.debug(f"Synced wallet {i + 1} with the pool, fetched {result.num_blocks_fetched} blocks")

# The server of a wallet rpc has the same keys as the full wallet, which sends txs and flushes them from the pool. The server keeps
# as pending what it saw in the pool, and 500 seconds after, when it is not there, it marks it failed and sets its inputs as
# unspent, even when another tx spent them. The next tx would use them and the daemon would reject it as a double spend. The
# daemon says which outputs are spent (the ones in the pool too): asking it here, right before the txs that follow, leaves no time
# for the server to change them again
for wallet in wallets:
if isinstance(wallet, MoneroWalletRpc):
wallet.rescan_spent()

msg: str = f"Cleared pending wallet transactions from {len(wallets)} wallets"
if not clear_from_wallet:
msg = "Cleared pool pending wallet transactions"
Expand Down
Loading