From 0bb008625b47d2cf573a772d24ccb710b9752ff4 Mon Sep 17 00:00:00 2001 From: sword-smith Date: Wed, 15 Jan 2025 00:11:33 +0100 Subject: [PATCH] docs: trace-log MUTXO info in wallet-updater Implement `Display` for `MonitoredUtxo` and trace-log this output in the wallet-state method that updates the wallet state with a new block. This feature was inspired by the bug hunt leading to #328. --- src/models/state/wallet/monitored_utxo.rs | 41 +++++++++++++++++++++++ src/models/state/wallet/wallet_state.rs | 9 +++++ 2 files changed, 50 insertions(+) diff --git a/src/models/state/wallet/monitored_utxo.rs b/src/models/state/wallet/monitored_utxo.rs index f4df5c74..db8650e7 100644 --- a/src/models/state/wallet/monitored_utxo.rs +++ b/src/models/state/wallet/monitored_utxo.rs @@ -1,5 +1,7 @@ use std::collections::VecDeque; +use std::fmt::Display; +use itertools::Itertools; use serde::Deserialize; use serde::Serialize; use tasm_lib::triton_vm::prelude::Tip5; @@ -40,6 +42,45 @@ pub struct MonitoredUtxo { pub abandoned_at: Option<(Digest, Timestamp, BlockHeight)>, } +impl Display for MonitoredUtxo { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let aocl_leaf_index = match self.get_latest_membership_proof_entry() { + Some(msmp) => msmp.1.aocl_leaf_index.to_string(), + None => "not mined".to_owned(), + }; + let spent = match self.spent_in_block { + Some((block_hash, block_timestamp, block_height)) => { + format!("spent in {block_hash}, at {block_timestamp}, block height {block_height}.") + } + None => "not spent".to_owned(), + }; + let confirmed = match self.confirmed_in_block { + Some((block_hash, block_timestamp, block_height)) => { + format!( + "received in {block_hash}, at {block_timestamp}, block height {block_height}." + ) + } + None => "not mined yet".to_owned(), + }; + let msmp_for_blocks = format!( + "valid MSMPs for blocks\n{}\n", + self.blockhash_to_membership_proof + .iter() + .map(|(digest, _)| digest) + .join("\n") + ); + + write!( + f, + "AOCL-leaf index: {aocl_leaf_index}\n\ + {spent}\n\ + {confirmed}\n\ + {msmp_for_blocks}\n + " + ) + } +} + impl MonitoredUtxo { pub(crate) fn new(utxo: Utxo, max_number_of_mps_stored: usize) -> Self { Self { diff --git a/src/models/state/wallet/wallet_state.rs b/src/models/state/wallet/wallet_state.rs index f406f678..a1706e60 100644 --- a/src/models/state/wallet/wallet_state.rs +++ b/src/models/state/wallet/wallet_state.rs @@ -980,6 +980,11 @@ impl WalletState { let (mut valid_membership_proofs_and_own_utxo_count, all_existing_mutxos) = preprocess_own_mutxos(monitored_utxos, new_block).await; + debug!( + "handling {} monitored UTXOs", + valid_membership_proofs_and_own_utxo_count.len() + ); + // Loop over all input UTXOs, applying all addition records. In each iteration, // a) Update all existing MS membership proofs // b) Register incoming transactions and derive their membership proofs @@ -1147,6 +1152,10 @@ impl WalletState { valid_membership_proofs_and_own_utxo_count.values() { let mut monitored_utxo = monitored_utxos.get(*own_utxo_index).await; + trace!( + "Updating MSMP for MUTXO with wallet-index {own_utxo_index}; with AOCL leaf-index {}. MUTXO:\n{monitored_utxo}", + updated_ms_mp.aocl_leaf_index + ); monitored_utxo.add_membership_proof_for_tip(new_block.hash(), updated_ms_mp.to_owned()); // Sanity check that membership proofs of non-spent transactions are still valid