1use std::collections::{BTreeMap, HashMap, HashSet};
9
10use anyhow::Context;
11use bitcoin::Amount;
12use bitcoin::secp256k1::{Keypair, PublicKey};
13use log::{debug, info, warn};
14
15use ark::{ProtocolEncoding, Vtxo, VtxoId};
16use ark::attestations::VtxoStatusAttestation;
17use ark::mailbox::MailboxAuthorization;
18use ark::vtxo::Full;
19use bitcoin_ext::BlockHeight;
20use server_rpc::TryFromBytes;
21use server_rpc::protos::{self, VtxoSpendState};
22use server_rpc::protos::mailbox_server::mailbox_message::Message;
23
24use crate::Wallet;
25use crate::vtxo::VtxoState;
26
27const STOP_GAP: u32 = 50;
29
30#[derive(Debug, Default, Clone)]
31pub struct RecoveryReportEntry(HashMap<VtxoId, Option<Amount>>);
32
33impl RecoveryReportEntry {
34 pub fn is_empty(&self) -> bool {
35 self.0.is_empty()
36 }
37
38 pub fn len(&self) -> usize {
39 self.0.len()
40 }
41
42 pub fn ids(&self) -> impl Iterator<Item = VtxoId> {
43 self.0.keys().cloned()
44 }
45
46 pub fn total_amount(&self) -> Amount {
47 self.0.values().filter_map(|a| *a).sum()
48 }
49
50 fn insert(&mut self, vtxo_id: VtxoId, amount: Option<Amount>) {
51 if !self.0.contains_key(&vtxo_id) {
52 self.0.insert(vtxo_id, amount);
53 }
54 }
55
56 fn remove(&mut self, vtxo_id: VtxoId) {
57 self.0.remove(&vtxo_id);
58 }
59}
60
61#[derive(Debug, Default, Clone)]
70pub struct RecoveryReport {
71 recovered: RecoveryReportEntry,
73 skipped: RecoveryReportEntry,
76 failed: RecoveryReportEntry,
79 foreign: RecoveryReportEntry,
84 exited: RecoveryReportEntry,
86}
87
88impl RecoveryReport {
89 pub fn is_complete(&self) -> bool {
95 self.failed.is_empty() && self.foreign.is_empty()
96 }
97
98 pub fn recovered(&self) -> &RecoveryReportEntry {
99 &self.recovered
100 }
101
102 pub fn push_recovered(&mut self, vtxo: &Vtxo<Full>) {
103 self.failed.remove(vtxo.id());
104 self.recovered.insert(vtxo.id(), Some(vtxo.amount()));
105 }
106
107 pub fn skipped(&self) -> &RecoveryReportEntry {
108 &self.skipped
109 }
110
111 pub fn push_skipped(&mut self, vtxo: &Vtxo<Full>) {
112 self.failed.remove(vtxo.id());
113 self.skipped.insert(vtxo.id(), Some(vtxo.amount()));
114 }
115
116 pub fn foreign(&self) -> &RecoveryReportEntry {
117 &self.foreign
118 }
119
120 pub fn push_foreign(&mut self, vtxo: &Vtxo<Full>) {
121 self.failed.remove(vtxo.id());
122 self.foreign.insert(vtxo.id(), Some(vtxo.amount()));
123 }
124
125 pub fn failed(&self) -> &RecoveryReportEntry {
126 &self.failed
127 }
128
129 pub fn push_failed(&mut self, id: VtxoId, amount: Option<Amount>) {
130 self.failed.insert(id, amount);
131 }
132
133 pub fn exited(&self) -> &RecoveryReportEntry {
134 &self.exited
135 }
136
137 pub fn push_exited(&mut self, vtxo: &Vtxo<Full>) {
138 self.failed.remove(vtxo.id());
139 self.exited.insert(vtxo.id(), Some(vtxo.amount()));
140 }
141}
142
143pub(crate) struct OwnedVtxo {
148 vtxo: Vtxo<Full>,
149 keypair: Keypair,
150}
151
152impl OwnedVtxo {
153 fn new(vtxo: Vtxo<Full>, keypair: Keypair) -> Self {
156 debug_assert_eq!(
157 vtxo.user_pubkey(), keypair.public_key(),
158 "OwnedVtxo keypair must match the VTXO's owner pubkey",
159 );
160 OwnedVtxo { vtxo, keypair }
161 }
162}
163
164#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
172struct ChainOrder {
173 expiry: BlockHeight,
174 depth: u16,
175}
176
177impl ChainOrder {
178 fn of(vtxo: &Vtxo<Full>) -> Self {
179 ChainOrder { expiry: vtxo.expiry_height(), depth: vtxo.exit_depth() }
180 }
181}
182
183impl Wallet {
184 fn mailbox_request(&self, checkpoint: u64) -> protos::mailbox_server::MailboxRequest {
185 let expiry = chrono::Local::now() + std::time::Duration::from_secs(60);
186 let auth = MailboxAuthorization::new(&self.recovery_mailbox_keypair(), expiry);
187 let mailbox_id = auth.mailbox();
188
189 protos::mailbox_server::MailboxRequest {
190 mailbox_id: mailbox_id.serialize(),
191 authorization: Some(auth.serialize()),
192 checkpoint,
193 }
194 }
195
196 async fn fetch_valid_owned_vtxos(&self, report: &mut RecoveryReport, ids: &[VtxoId]) ->
197 anyhow::Result<Vec<OwnedVtxo>>
198 {
199 let mut candidates = HashMap::new();
202
203 for id in ids {
204 match self.fetch_vtxo(*id).await {
205 Ok(vtxo) => {
206 candidates.insert(*id, vtxo);
207 },
208 Err(e) => {
209 warn!("Could not fetch recovery vtxo {id}: {:#}", e);
210 report.push_failed(*id, None);
211 }
212 }
213 }
214
215 let candidates = candidates.into_values().collect();
217 let owned = self.resolve_owned_vtxos(candidates, report).await?;
218
219 let mut vtxos = BTreeMap::<ChainOrder, Vec<OwnedVtxo>>::new();
221 for owned in owned {
222 vtxos.entry(ChainOrder::of(&owned.vtxo)).or_default().push(owned);
223 }
224
225 Ok(vtxos.into_values().flatten().collect())
226 }
227
228
229 async fn read_mailbox_recovery_vtxo_ids(&self) -> anyhow::Result<(HashSet<VtxoId>, u64)> {
236 let (mut srv, _) = self.require_server().await?;
237
238 let mut ids = HashSet::new();
241
242 let mut iteration = 0;
243 let mut checkpoint = 0u64;
244 loop {
245 iteration += 1;
246
247 let req = self.mailbox_request(checkpoint);
248 let resp = srv.mailbox_client.read_mailbox(req).await
249 .context("error reading recovery mailbox")?.into_inner();
250
251 debug!("Recovery mailbox returned {} messages on iteration {iteration}", resp.messages.len());
252
253 let prev_checkpoint = checkpoint;
254 for msg in &resp.messages {
255 match &msg.message {
256 Some(Message::RecoveryVtxoIds(m)) => {
257 checkpoint = checkpoint.max(msg.checkpoint);
258 for raw in &m.vtxo_ids {
259 let Ok(id) = VtxoId::from_bytes(raw.clone()) else {
260 warn!("Ignoring undecodable recovery vtxo id: {raw:?}");
261 continue;
262 };
263 ids.insert(id);
264 }
265 },
266 Some(Message::Arkoor(m)) => {
267 checkpoint = checkpoint.max(msg.checkpoint);
268 for raw in &m.vtxos {
269 let Ok(vtxo) = Vtxo::<Full>::from_bytes(raw.clone()) else {
270 warn!("Ignoring undecodable vtxo: {raw:?}");
271 continue;
272 };
273 ids.insert(vtxo.id());
274 }
275 },
276 Some(Message::RoundParticipationCompleted(_)) |
277 Some(Message::IncomingLightningPayment(_)) |
278 Some(Message::LightningSendFinished(_)) => {},
279 None => {
280 warn!("Recovery mailbox returned a message with no content: {msg:?}");
281 },
282 }
283 }
284
285 if !resp.have_more {
286 break;
287 }
288
289 if checkpoint == prev_checkpoint {
293 warn!("Recovery mailbox iteration {iteration} made no progress \
294 at checkpoint {checkpoint}; stopping");
295 break;
296 }
297 }
298
299 Ok((ids, checkpoint))
300 }
301
302 async fn fetch_vtxo(&self, id: VtxoId) -> anyhow::Result<Vtxo<Full>> {
307 let (mut srv, _) = self.require_server().await?;
308 let resp = srv.client.get_vtxo(protos::GetVtxoRequest {
309 vtxo_id: id.to_bytes().to_vec(),
310 }).await.with_context(|| format!("error fetching vtxo {id} from server"))?.into_inner();
311
312 Vtxo::<Full>::deserialize(&resp.vtxo)
313 .with_context(|| format!("server returned an undecodable vtxo for {id}"))
314 }
315
316 async fn check_vtxo_onchain_status(&self, report: &mut RecoveryReport, vtxo: &Vtxo<Full>) -> anyhow::Result<bool> {
321 match self.inner.chain.tx_confirmed(vtxo.point().txid).await {
326 Ok(Some(height)) => {
327 self.store_vtxos(&vec![vtxo.clone()], &VtxoState::Exited).await?;
328 self.exit_mgr().start_exit_for_vtxos_including_non_standard(&vec![vtxo.to_bare()]).await?;
329 report.push_exited(vtxo);
330 debug!("Skipping recovery vtxo {}: confirmed on-chain at height {height} (exited)", vtxo.id());
331 Ok(true)
332 },
333 Ok(None) | Err(_) => Ok(false),
336 }
337 }
338
339 async fn check_vtxo_server_status(
344 &self,
345 report: &mut RecoveryReport,
346 vtxo: &Vtxo<Full>,
347 keypair: &Keypair,
348 ) -> anyhow::Result<bool> {
349 let (mut srv, _) = self.require_server().await?;
350 let vtxo_id = vtxo.id();
351 let attestation = VtxoStatusAttestation::new(vtxo_id, keypair);
352 let resp = srv.client.get_vtxo_status(protos::GetVtxoStatusRequest {
353 vtxo_id: vtxo_id.to_bytes().to_vec(),
354 attestation: attestation.serialize(),
355 }).await.with_context(|| format!("error fetching status for vtxo {vtxo_id}"))?.into_inner();
356
357 let spend_state = protos::VtxoSpendState::try_from(resp.spend_state)
358 .map_err(|_| anyhow::anyhow!(
359 "server returned unknown spend state {} for vtxo {vtxo_id}", resp.spend_state,
360 ));
361
362 match spend_state {
366 Ok(VtxoSpendState::Spendable) => return Ok(false),
367 Ok(state @ (
369 VtxoSpendState::Spent
370 | VtxoSpendState::Unclaimed
371 | VtxoSpendState::Unregistered
372 | VtxoSpendState::HtlcRecvUnclaimed
373 )) => {
374 debug!("Recovery vtxo {vtxo_id} not spendable ({state:?}), skipping");
375 report.push_skipped(vtxo);
376 },
377 Ok(VtxoSpendState::Unspecified) => {
380 warn!("Server returned an unspecified spend state for recovery vtxo {vtxo_id}");
381 report.push_failed(vtxo_id, Some(vtxo.amount()));
382 },
383 Err(e) => {
384 warn!("Could not get status for recovery vtxo {vtxo_id}: {:#}", e);
385 report.push_failed(vtxo_id, Some(vtxo.amount()));
386 },
387 }
388
389 Ok(true)
390 }
391
392 async fn resolve_owned_vtxos(
404 &self,
405 vtxos: Vec<Vtxo<Full>>,
406 report: &mut RecoveryReport,
407 ) -> anyhow::Result<Vec<OwnedVtxo>> {
408 let mut pending = HashMap::<PublicKey, Vec<Vtxo<Full>>>::new();
411 let mut matched = Vec::<(Vtxo<Full>, Keypair)>::new();
412
413 for vtxo in vtxos {
414 match self.pubkey_keypair(&vtxo.user_pubkey()).await? {
415 Some((_idx, keypair)) => matched.push((vtxo, keypair)),
416 None => pending.entry(vtxo.user_pubkey()).or_default().push(vtxo),
417 }
418 }
419
420 let start_idx = self.inner.db.get_last_vtxo_key_index().await?.map(|i| i + 1).unwrap_or(0);
422 let mut frontier = start_idx.saturating_add(STOP_GAP);
423 let mut gap = Vec::<(u32, PublicKey)>::new();
424 let mut idx = start_idx;
425 while idx <= frontier && !pending.is_empty() {
426 let keypair = self.inner.seed.derive_vtxo_keypair(idx);
427 let pubkey = keypair.public_key();
428 if let Some(owned_vtxos) = pending.remove(&pubkey) {
429 for (i, pk) in gap.drain(..) {
432 self.inner.db.store_vtxo_key(i, pk).await?;
433 }
434 self.inner.db.store_vtxo_key(idx, pubkey).await?;
435 frontier = idx.saturating_add(STOP_GAP);
436 matched.extend(owned_vtxos.into_iter().map(|v| (v, keypair)));
437 } else {
438 gap.push((idx, pubkey));
439 }
440 let Some(next_idx) = idx.checked_add(1) else { break };
443 idx = next_idx;
444 }
445
446 for vtxo in pending.into_values().flatten() {
448 report.push_foreign(&vtxo);
449 }
450
451 let mut owned = Vec::with_capacity(matched.len());
454 for (vtxo, keypair) in matched {
455 if let Err(e) = self.validate_vtxo(&vtxo).await {
456 warn!("Could not validate recovery vtxo {}: {:#}", vtxo.id(), e);
457 report.push_failed(vtxo.id(), Some(vtxo.amount()));
458 } else {
459 owned.push(OwnedVtxo::new(vtxo, keypair));
460 }
461 }
462
463 Ok(owned)
464 }
465
466 async fn inner_recover_vtxos(
467 &self,
468 report: &mut RecoveryReport,
469 ids: impl IntoIterator<Item = VtxoId>,
470 ) -> anyhow::Result<()> {
471 let ids = ids.into_iter().collect::<Vec<_>>();
472 let owned = self.fetch_valid_owned_vtxos(report, &ids).await?;
473
474 let mut spent = HashSet::<VtxoId>::new();
477
478 for o in owned {
479 let id = o.vtxo.id();
480
481 if spent.contains(&id) {
483 debug!("Skipping recovery vtxo {id}: spent into a newer recovered vtxo");
484 report.push_skipped(&o.vtxo);
485 continue;
486 }
487
488 spent.extend(o.vtxo.ancestor_ids());
490
491 if self.check_vtxo_onchain_status(report, &o.vtxo).await? {
492 continue;
493 }
494
495 if self.check_vtxo_server_status(report, &o.vtxo, &o.keypair).await? {
499 continue;
500 }
501
502 match self.store_vtxos([&o.vtxo], &VtxoState::Spendable).await {
504 Ok(()) => {
505 report.push_recovered(&o.vtxo);
506 debug!("Recovered spendable vtxo {id} ({})", o.vtxo.amount());
507 },
508 Err(e) => {
509 warn!("Failed to store recovered vtxo {id}: {:#}", e);
510 report.push_failed(id, Some(o.vtxo.amount()));
511 },
512 }
513 }
514
515 Ok(())
516 }
517
518 pub async fn recover_vtxos(&self, ids: impl IntoIterator<Item = VtxoId>)
519 -> anyhow::Result<RecoveryReport>
520 {
521 let mut report = RecoveryReport::default();
522 self.inner_recover_vtxos(&mut report, ids).await?;
523 Ok(report)
524 }
525
526 pub(crate) async fn recover_from_mailbox(&self) -> anyhow::Result<RecoveryReport> {
538 let mut report = RecoveryReport::default();
539
540 let (ids, checkpoint) = self.read_mailbox_recovery_vtxo_ids().await?;
542 debug!("Found {} distinct vtxo ids in the recovery mailbox", ids.len());
543
544 self.inner_recover_vtxos(&mut report, ids).await?;
545
546 if !report.foreign.is_empty() {
551 warn!(
552 "Recovery mailbox held {} vtxo(s) not derivable from this seed within the \
553 gap limit ({STOP_GAP}); if any are ours they were not recovered: {:?}",
554 report.foreign.len(), report.foreign,
555 );
556 }
557
558 if report.is_complete() {
559 info!(
560 "Recovered {} spendable vtxos from the recovery mailbox ({} skipped)",
561 report.recovered.len(), report.skipped.len(),
562 );
563 }
564
565 for _ in 0..3 {
567 if report.failed.is_empty() {
568 break;
569 }
570
571 let ids = report.failed.ids().collect::<Vec<_>>();
572 self.inner_recover_vtxos(&mut report, ids).await?;
573 }
574
575 if !report.failed.is_empty() {
576 warn!(
577 "Recovery incomplete: recovered {} spendable vtxos, but {} could not be \
578 checked due to errors; funds may be missing — retry recovery to recover \
579 them ({} skipped). Failed vtxos: {:?}",
580 report.recovered.len(), report.failed.len(),
581 report.skipped.len(), report.failed,
582 );
583 }
584
585 self.inner.db.store_mailbox_checkpoint(checkpoint).await?;
587
588 Ok(report)
589 }
590}
591
592#[cfg(test)]
593mod test {
594 use super::*;
595 use bitcoin::Amount;
596 use bitcoin::hashes::Hash;
597
598 fn dummy_id(vout: u32) -> VtxoId {
599 bitcoin::OutPoint::new(bitcoin::Txid::all_zeros(), vout).into()
600 }
601
602 #[test]
606 fn recovery_report_completeness() {
607 let clean = RecoveryReport {
608 recovered: RecoveryReportEntry(HashMap::from([(dummy_id(0), Some(Amount::from_sat(1000)))])),
609 skipped: RecoveryReportEntry(HashMap::from([(dummy_id(1), Some(Amount::from_sat(1000)))])),
610 foreign: RecoveryReportEntry(HashMap::new()),
611 failed: RecoveryReportEntry(HashMap::new()),
612 exited: RecoveryReportEntry(HashMap::new()),
613 };
614 assert!(clean.is_complete(),
615 "recovered and skipped VTXOs are clean decisions, not failures");
616
617 assert!(RecoveryReport::default().is_complete(),
618 "an empty report is trivially complete");
619 assert!(!RecoveryReport {
620 failed: RecoveryReportEntry(HashMap::from([(dummy_id(0), Some(Amount::from_sat(1000)))])),
621 ..Default::default()
622 }.is_complete(), "a failed VTXO means recovery is incomplete");
623 assert!(!RecoveryReport {
624 foreign: RecoveryReportEntry(HashMap::from([(dummy_id(2), Some(Amount::from_sat(1000)))])),
625 ..Default::default()
626 }.is_complete(), "a foreign id is likely an owned VTXO beyond the gap limit, so recovery is incomplete");
627 }
628}
629