wallet: derive readiness from the wallet's get_height, not a nonexistent get_info
monero-wallet-rpc does not implement get_info (-32601 Method not found); it exists on the daemon only. Every wallet therefore sat in syncing forever: no balance, spends refused, receipts never verified. Readiness is now the pair: - the wallet RPC's own get_height (its scanned height), and - the chain tip from the existing daemon client (GET /get_height). ready = height > 0 && (tip == 0 || height + 2 >= tip). The 2-block slack is deliberate: the tip moves every ~2 min and the two reads are not a snapshot. A daemon that does not answer, a missing height and -13 No wallet file are all not-ready, never an error banner. ChildConfig::daemon_probe_addr resolves a tip source for clearnet remotes (the wallet child already dials them); a .onion daemon is not dialled without the SOCKS child and stays fail-closed. docs/WALLET.md now documents the real rule. Tests cover -32601 get_info, equal tip, slack boundary, dead daemon and the no-wallet-file answer.
This commit is contained in:
parent
dddf74f274
commit
3aafaefae8
6 changed files with 476 additions and 159 deletions
246
tests/wallet.rs
246
tests/wallet.rs
|
|
@ -150,12 +150,14 @@ async fn mock_create_address_and_transfer() {
|
|||
listener,
|
||||
Arc::new(Mutex::new(Vec::new())),
|
||||
Arc::new(move |method| match method {
|
||||
// A spend is only attempted on a synced wallet.
|
||||
"get_info" => synced_info(),
|
||||
// A spend is only attempted on a synced wallet: the wallet's own
|
||||
// scanned height plus the daemon tip.
|
||||
"get_height" => wallet_height(3210000),
|
||||
_ => rpc_result(&format!(r#"{{"address":"{addr_hex}","tx_hash":"abc123"}}"#)),
|
||||
}),
|
||||
));
|
||||
let w = recording_wallet(port);
|
||||
let daemon = mock_daemon(3210000, 3210000).await;
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(daemon));
|
||||
let created = w.create_address().await.expect("create_address");
|
||||
assert!(created.starts_with('8'));
|
||||
|
||||
|
|
@ -441,10 +443,59 @@ fn rpc_result(result: &str) -> String {
|
|||
format!(r#"{{"jsonrpc":"2.0","id":"0","result":{result}}}"#)
|
||||
}
|
||||
|
||||
/// What a fully synced wallet answers to `get_info`: the daemon has reached the
|
||||
/// tip and the wallet has scanned it. This is the honest readiness signal.
|
||||
fn synced_info() -> String {
|
||||
rpc_result(r#"{"height":3210000,"target_height":3210000,"synchronized":true}"#)
|
||||
/// What the wallet RPC answers to `get_height`: the wallet's own scanned height.
|
||||
/// This is the honest wallet-side signal — `get_info` does not exist there.
|
||||
fn wallet_height(height: u64) -> String {
|
||||
rpc_result(&format!(r#"{{"height":{height}}}"#))
|
||||
}
|
||||
|
||||
/// A chain-daemon stub. `GET /get_height` answers the tip; `/json_rpc get_info`
|
||||
/// answers height and target. A daemon is unauthenticated, so no Digest flow.
|
||||
async fn serve_daemon(
|
||||
listener: TcpListener,
|
||||
height: u64,
|
||||
target: u64,
|
||||
log: Option<Arc<Mutex<Vec<String>>>>,
|
||||
) {
|
||||
while let Ok((mut sock, _)) = listener.accept().await {
|
||||
let mut buf = vec![0u8; 8192];
|
||||
let Ok(n) = sock.read(&mut buf).await else {
|
||||
return;
|
||||
};
|
||||
if n == 0 {
|
||||
continue;
|
||||
}
|
||||
let req = String::from_utf8_lossy(&buf[..n]).to_string();
|
||||
if let Some(log) = &log {
|
||||
log.lock().expect("daemon log").push(req.clone());
|
||||
}
|
||||
let body = if req.starts_with("GET /get_height") {
|
||||
format!(r#"{{"height":{height}}}"#)
|
||||
} else if req.contains("get_info") {
|
||||
format!(
|
||||
r#"{{"jsonrpc":"2.0","id":"0","result":{{"height":{height},"target_height":{target}}}}}"#
|
||||
)
|
||||
} else {
|
||||
"{}".to_string()
|
||||
};
|
||||
let _ = sock
|
||||
.write_all(
|
||||
format!(
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
||||
body.len()
|
||||
)
|
||||
.as_bytes(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Start a daemon stub on an ephemeral loopback port; returns `host:port`.
|
||||
async fn mock_daemon(height: u64, target: u64) -> String {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let addr = listener.local_addr().unwrap().to_string();
|
||||
tokio::spawn(serve_daemon(listener, height, target, None));
|
||||
addr
|
||||
}
|
||||
|
||||
fn recording_wallet(port: u16) -> Wallet {
|
||||
|
|
@ -532,11 +583,12 @@ async fn view_only_wallet_maps_transfer_failure_to_clear_message() {
|
|||
Arc::clone(&log),
|
||||
Arc::new(|method| match method {
|
||||
"query_key" => rpc_error(-29, "The wallet is watch-only. Cannot retrieve spend key."),
|
||||
"get_info" => synced_info(),
|
||||
"get_height" => wallet_height(3210000),
|
||||
_ => rpc_error(-18, "No transaction created"),
|
||||
}),
|
||||
));
|
||||
let w = recording_wallet(port);
|
||||
let daemon = mock_daemon(3210000, 3210000).await;
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(daemon));
|
||||
assert!(!w.no_spend());
|
||||
let err = w
|
||||
.transfer(
|
||||
|
|
@ -564,11 +616,13 @@ async fn spend_capable_wallet_keeps_the_original_transfer_error() {
|
|||
Arc::new(Mutex::new(Vec::new())),
|
||||
Arc::new(|method| match method {
|
||||
"query_key" => rpc_result(r#"{"key":"00112233"}"#),
|
||||
"get_info" => synced_info(),
|
||||
"get_height" => wallet_height(3210000),
|
||||
_ => rpc_error(-18, "No transaction created"),
|
||||
}),
|
||||
));
|
||||
let daemon = mock_daemon(3210000, 3210000).await;
|
||||
let err = recording_wallet(port)
|
||||
.with_daemon_probe(Some(daemon))
|
||||
.transfer(
|
||||
"8BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
|
||||
1,
|
||||
|
|
@ -1217,7 +1271,8 @@ async fn serve_delayed(listener: TcpListener, delay: std::time::Duration) {
|
|||
serve_rpc_conns(
|
||||
listener,
|
||||
Arc::new(|method: &str| match method {
|
||||
"get_info" => synced_info(),
|
||||
"get_height" => wallet_height(3210000),
|
||||
"get_transfers" => rpc_result(r#"{"in":[],"pending":[]}"#),
|
||||
_ => rpc_result(
|
||||
r#"{"address":"4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}"#,
|
||||
),
|
||||
|
|
@ -1242,15 +1297,14 @@ async fn unsynced_wallet_is_not_ready_and_refuses_to_spend() {
|
|||
listener,
|
||||
Arc::clone(&log),
|
||||
Arc::new(|method| match method {
|
||||
"get_info" => {
|
||||
rpc_result(r#"{"height":1200000,"target_height":3210000,"synchronized":false}"#)
|
||||
}
|
||||
"get_height" => wallet_height(1200000),
|
||||
_ => rpc_result(r#"{"tx_hash":"abc123"}"#),
|
||||
}),
|
||||
));
|
||||
let w = recording_wallet(port);
|
||||
let daemon = mock_daemon(3210000, 3210000).await;
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(daemon));
|
||||
assert!(
|
||||
!w.chain_ready().await.expect("get_info"),
|
||||
!w.chain_ready().await.expect("get_height"),
|
||||
"must not be ready"
|
||||
);
|
||||
assert!(
|
||||
|
|
@ -1267,16 +1321,115 @@ async fn unsynced_wallet_is_not_ready_and_refuses_to_spend() {
|
|||
);
|
||||
}
|
||||
|
||||
/// A missing/absent `synchronized` (or a wallet behind the daemon) is not-ready,
|
||||
/// never a silent pass. Fail closed.
|
||||
/// `monero-wallet-rpc` has no `get_info` (it answers `-32601 Method not found`).
|
||||
/// Readiness must never depend on that call: the wallet's own `get_height` is
|
||||
/// compared with the daemon's tip.
|
||||
#[tokio::test]
|
||||
async fn missing_or_behind_synchronized_is_not_ready() {
|
||||
for info in [
|
||||
r#"{"height":10}"#,
|
||||
r#"{"height":100,"target_height":200,"synchronized":true}"#,
|
||||
r#"{"height":0,"target_height":0,"synchronized":true}"#,
|
||||
r#"{"synchronized":"true"}"#,
|
||||
async fn readiness_never_asks_the_wallet_rpc_for_get_info() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let log = Arc::new(Mutex::new(Vec::new()));
|
||||
tokio::spawn(serve_recording(
|
||||
listener,
|
||||
Arc::clone(&log),
|
||||
Arc::new(|method| match method {
|
||||
"get_height" => wallet_height(3210000),
|
||||
"get_info" => rpc_error(-32601, "Method not found"),
|
||||
_ => rpc_result("{}"),
|
||||
}),
|
||||
));
|
||||
let daemon = mock_daemon(3210000, 3210000).await;
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(daemon));
|
||||
assert!(
|
||||
w.chain_ready().await.expect("get_height"),
|
||||
"a wallet at the daemon tip is ready without get_info"
|
||||
);
|
||||
let seen = log.lock().expect("log").clone();
|
||||
assert!(
|
||||
seen.iter().any(|r| r.contains("\"method\":\"get_height\"")),
|
||||
"the wallet's own scanned height must be asked: {seen:?}"
|
||||
);
|
||||
assert!(
|
||||
!seen.iter().any(|r| r.contains("\"method\":\"get_info\"")),
|
||||
"get_info is not a wallet-RPC method and must not be probed: {seen:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Readiness against a daemon tip: equal is ready, the tip moves every ~2 min so
|
||||
/// a two-block gap is ready, a real lag is not.
|
||||
#[tokio::test]
|
||||
async fn a_wallet_within_the_slack_of_the_daemon_tip_is_ready() {
|
||||
for (wallet, tip, ready) in [
|
||||
(3210000u64, 3210000u64, true),
|
||||
(3209998, 3210000, true),
|
||||
(3209997, 3210000, false),
|
||||
] {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
tokio::spawn(serve_recording(
|
||||
listener,
|
||||
Arc::new(Mutex::new(Vec::new())),
|
||||
Arc::new(move |_| wallet_height(wallet)),
|
||||
));
|
||||
let daemon = mock_daemon(tip, tip).await;
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(daemon));
|
||||
assert_eq!(
|
||||
w.chain_ready().await.expect("get_height"),
|
||||
ready,
|
||||
"wallet {wallet} / tip {tip}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A daemon that does not answer leaves the tip unknown. Unknown is *not*
|
||||
/// caught up — fail closed, never a silent pass.
|
||||
#[tokio::test]
|
||||
async fn an_unreachable_daemon_is_not_ready() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
tokio::spawn(serve_recording(
|
||||
listener,
|
||||
Arc::new(Mutex::new(Vec::new())),
|
||||
Arc::new(|_| wallet_height(3210000)),
|
||||
));
|
||||
// Bind, read the port, drop: nothing is listening there.
|
||||
let dead = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let dead_addr = dead.local_addr().unwrap().to_string();
|
||||
drop(dead);
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(dead_addr));
|
||||
assert!(
|
||||
!w.chain_ready().await.expect("get_height"),
|
||||
"an unanswered daemon must not read as ready"
|
||||
);
|
||||
}
|
||||
|
||||
/// A wallet RPC with no wallet file open answers `-13 No wallet file` to
|
||||
/// `get_height`. That is a state, not an error banner: not ready, no Err.
|
||||
#[tokio::test]
|
||||
async fn an_rpc_with_no_wallet_open_is_not_ready_and_not_an_error() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
tokio::spawn(serve_recording(
|
||||
listener,
|
||||
Arc::new(Mutex::new(Vec::new())),
|
||||
Arc::new(|_| rpc_error(-13, "No wallet file")),
|
||||
));
|
||||
let daemon = mock_daemon(3210000, 3210000).await;
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(daemon));
|
||||
assert!(
|
||||
!w.chain_ready()
|
||||
.await
|
||||
.expect("no wallet file is a state, not an error"),
|
||||
"a wallet with no file open is not ready"
|
||||
);
|
||||
assert!(!w.can_verify_receipts().await);
|
||||
}
|
||||
|
||||
/// An answered `get_height` with no height (or height 0) is unknown, and a
|
||||
/// daemon tip of 0 is a daemon that has not indexed anything: not ready.
|
||||
#[tokio::test]
|
||||
async fn a_missing_or_zero_wallet_height_is_not_ready() {
|
||||
for info in [r#"{}"#, r#"{"height":0}"#, r#"{"height":"10"}"#] {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let canned = rpc_result(info);
|
||||
|
|
@ -1285,9 +1438,10 @@ async fn missing_or_behind_synchronized_is_not_ready() {
|
|||
Arc::new(Mutex::new(Vec::new())),
|
||||
Arc::new(move |_| canned.clone()),
|
||||
));
|
||||
let w = recording_wallet(port);
|
||||
let daemon = mock_daemon(0, 0).await;
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(daemon));
|
||||
assert!(
|
||||
!w.chain_ready().await.expect("get_info"),
|
||||
!w.chain_ready().await.expect("get_height"),
|
||||
"{info} must not read as ready"
|
||||
);
|
||||
}
|
||||
|
|
@ -1301,12 +1455,13 @@ async fn synced_wallet_is_ready_and_spends() {
|
|||
listener,
|
||||
Arc::new(Mutex::new(Vec::new())),
|
||||
Arc::new(|method| match method {
|
||||
"get_info" => synced_info(),
|
||||
"get_height" => wallet_height(3210000),
|
||||
_ => rpc_result(r#"{"tx_hash":"abc123"}"#),
|
||||
}),
|
||||
));
|
||||
let w = recording_wallet(port);
|
||||
assert!(w.chain_ready().await.expect("get_info"));
|
||||
let daemon = mock_daemon(3210000, 3210000).await;
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(daemon));
|
||||
assert!(w.chain_ready().await.expect("get_height"));
|
||||
assert!(w.can_verify_receipts().await);
|
||||
assert_eq!(w.transfer(SPEND_ADDR, 1).await.expect("transfer"), "abc123");
|
||||
}
|
||||
|
|
@ -1325,12 +1480,13 @@ async fn spend_ready_needs_an_unlocked_balance() {
|
|||
listener,
|
||||
Arc::new(Mutex::new(Vec::new())),
|
||||
Arc::new(move |method| match method {
|
||||
"get_info" => synced_info(),
|
||||
"get_height" => wallet_height(3210000),
|
||||
"get_balance" => balance.clone(),
|
||||
_ => rpc_result(r#"{}"#),
|
||||
_ => rpc_result("{}"),
|
||||
}),
|
||||
));
|
||||
let w = recording_wallet(port);
|
||||
let daemon = mock_daemon(3210000, 3210000).await;
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(daemon));
|
||||
assert_eq!(w.spend_ready().await.expect("spend_ready"), ready);
|
||||
}
|
||||
}
|
||||
|
|
@ -1369,16 +1525,15 @@ async fn unsynced_wallet_leaves_a_matching_receipt_unverified() {
|
|||
listener,
|
||||
Arc::new(Mutex::new(Vec::new())),
|
||||
Arc::new(move |method| match method {
|
||||
"get_info" => {
|
||||
rpc_result(r#"{"height":1200000,"target_height":3210000,"synchronized":false}"#)
|
||||
}
|
||||
"get_height" => wallet_height(1200000),
|
||||
"get_transfers" => rpc_result(&format!(
|
||||
r#"{{"in":[{{"txid":"deadbeef","amount":5,"address":"{addr_owned}"}}],"pending":[]}}"#
|
||||
)),
|
||||
_ => rpc_result(r#"{}"#),
|
||||
_ => rpc_result("{}"),
|
||||
}),
|
||||
));
|
||||
let w = recording_wallet(port);
|
||||
let daemon = mock_daemon(3210000, 3210000).await;
|
||||
let w = recording_wallet(port).with_daemon_probe(Some(daemon));
|
||||
|
||||
// The same shape node.rs::ingest_receipt runs.
|
||||
if w.can_verify_receipts().await
|
||||
|
|
@ -1393,8 +1548,10 @@ async fn unsynced_wallet_leaves_a_matching_receipt_unverified() {
|
|||
);
|
||||
}
|
||||
|
||||
/// Chain-dependent calls get a longer budget than local wallet-management calls,
|
||||
/// and the budget is the backend's to set — not a hard-coded 5s.
|
||||
/// Chain-dependent calls (`get_transfers`, `transfer`) get a longer budget than
|
||||
/// local wallet-management calls, and the budget is the backend's to set — not a
|
||||
/// hard-coded 5s. Readiness is no longer a chain wait: it is one local
|
||||
/// `get_height` plus one bounded daemon probe.
|
||||
#[tokio::test]
|
||||
async fn chain_calls_get_a_longer_budget_than_local_calls() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
|
|
@ -1412,12 +1569,11 @@ async fn chain_calls_get_a_longer_budget_than_local_calls() {
|
|||
err.to_ascii_lowercase().contains("timed out"),
|
||||
"the local call must hit the short budget: {err}"
|
||||
);
|
||||
assert!(
|
||||
w.chain_ready()
|
||||
.await
|
||||
.expect("get_info within the chain budget"),
|
||||
"chain calls must outlast daemon latency"
|
||||
);
|
||||
let rows = w
|
||||
.get_transfers()
|
||||
.await
|
||||
.expect("get_transfers within the chain budget");
|
||||
assert!(rows.is_empty(), "{rows:?}");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Reference in a new issue