diff --git a/minotari-scanning/src/scanning/http/scanner.rs b/minotari-scanning/src/scanning/http/scanner.rs index ecc78a0..1489d48 100644 --- a/minotari-scanning/src/scanning/http/scanner.rs +++ b/minotari-scanning/src/scanning/http/scanner.rs @@ -353,6 +353,14 @@ where "start_height cannot be greater than end_height".to_string(), )); } + let tip = self.get_tip_info().await?; + if tip.best_block_height <= config.start_height { + debug!( + "Tip height {} is less than requested start height {}, returning empty results", + tip.best_block_height, config.start_height + ); + return Ok((Vec::new(), false)); + } match &self.current_in_progress.get_config() { Some(existing_scan) if *existing_scan == config => { diff --git a/minotari/src/scan/coordinator.rs b/minotari/src/scan/coordinator.rs index 924ba8f..adc87a9 100644 --- a/minotari/src/scan/coordinator.rs +++ b/minotari/src/scan/coordinator.rs @@ -136,7 +136,6 @@ impl ScanCoordinator { ) -> Result { let key_manager = account.get_key_manager(password)?; let view_key = key_manager.get_private_view_key(); - let reorg_result = reorg::handle_reorgs(scanner, conn, account.id, self.webhook_config.clone()) .await .map_err(ScanError::Fatal)?; @@ -391,7 +390,6 @@ impl ScanCoordinator { } let mut conn = db_handler.get_connection().await?; - for target in targets { let res = reorg::handle_reorgs(scanner, &mut conn, target.account.id, self.webhook_config.clone()) .await diff --git a/minotari/src/scan/reorg.rs b/minotari/src/scan/reorg.rs index ebd672f..6058a26 100644 --- a/minotari/src/scan/reorg.rs +++ b/minotari/src/scan/reorg.rs @@ -43,7 +43,6 @@ pub async fn handle_reorgs( reorg_information: None, }); } - let mut reorg_start_height = 0; let mut is_reorg_detected = false; @@ -67,7 +66,6 @@ pub async fn handle_reorgs( is_reorg_detected = true; } } - if is_reorg_detected { warn!( target: "audit", diff --git a/minotari/src/scan/scan.rs b/minotari/src/scan/scan.rs index e1548c6..d40b24b 100644 --- a/minotari/src/scan/scan.rs +++ b/minotari/src/scan/scan.rs @@ -353,7 +353,6 @@ async fn wait_for_next_poll_cycle( } let mut conn = db_handler.get_connection().await?; - let reorg_result = reorg::handle_reorgs(&mut scanner_context.scanner, &mut conn, scanner_context.account_id) .await .map_err(ScanError::Fatal)?; @@ -423,7 +422,6 @@ async fn prepare_account_scan( let mut scanner = HttpBlockchainScanner::new(base_url.to_string(), vec![key_manager.clone()], processing_threads) .await .map_err(|e| ScanError::Intermittent(e.to_string()))?; - let reorg_result = reorg::handle_reorgs(&mut scanner, conn, account.id) .await .map_err(ScanError::Fatal)?; @@ -615,7 +613,6 @@ async fn run_scan_loop( })); } } - if more_blocks && blocks_since_reorg_check >= scanner_context.reorg_check_interval { let mut conn = db_handler.get_connection().await?; let reorg_result = diff --git a/minotari/src/transactions/displayed_transaction_processor/processor.rs b/minotari/src/transactions/displayed_transaction_processor/processor.rs index f8497cc..7eaa82d 100644 --- a/minotari/src/transactions/displayed_transaction_processor/processor.rs +++ b/minotari/src/transactions/displayed_transaction_processor/processor.rs @@ -9,6 +9,7 @@ use crate::scan::block_event_accumulator::BlockEventAccumulator; use crate::scan::{DetectedOutput, MemoInfo, SpentInput}; use log::debug; use rusqlite::Connection; +use std::cmp::Reverse; use std::collections::HashMap; use tari_common_types::transaction::TxId; use tari_common_types::types::FixedHash; @@ -112,6 +113,7 @@ impl DisplayedTransactionProcessor { // we need to create a new display tx for this input new_debit.push((balance_change.clone(), output.clone())); } + new_debit.sort_by_key(|a| a.1.output.value()); //Now we have a list of inputs and outputs that don't have matching transactions let mut new_transactions = Vec::new(); @@ -165,7 +167,7 @@ impl DisplayedTransactionProcessor { // So this is change from our wallet. let total_send = amount + output.output.value() + output.output.payment_id().get_fee().unwrap_or_default(); - let mut selected_inputs = Vec::new(); + let mut selected_inputs = Vec::with_capacity(new_debit.len()); if Self::iter_search_for_matching_inputs( total_send, MicroMinotari::from(0), @@ -255,34 +257,32 @@ impl DisplayedTransactionProcessor { fn iter_search_for_matching_inputs( amount_sent: MicroMinotari, amount_already_selected: MicroMinotari, - spent_inputs: &Vec<(BalanceChange, SpentInput)>, + spent_inputs: &[(BalanceChange, SpentInput)], selected_inputs: &mut Vec, start_index: usize, ) -> bool { for i in start_index..spent_inputs.len() { - if selected_inputs.contains(&i) { - continue; - } - let input = &spent_inputs.get(i).expect("index is in bounds"); - if amount_already_selected + input.1.output.value() > amount_sent { - continue; + let input_value = spent_inputs.get(i).expect("bounds are checked").1.output.value(); + let new_total = amount_already_selected + input_value; + if new_total > amount_sent { + return false; } - let new_total = amount_already_selected + input.1.output.value(); + if new_total == amount_sent { - selected_inputs.push(i); // we have our match, stop searching + selected_inputs.push(i); return true; } - // search this branch further - let mut new_selected = selected_inputs.clone(); - new_selected.push(i); - if Self::iter_search_for_matching_inputs(amount_sent, new_total, spent_inputs, &mut new_selected, i + 1) { - *selected_inputs = new_selected; + if i + 1 == spent_inputs.len() { + return false; + } + // search this branch further; backtrack if no match found + selected_inputs.push(i); + if Self::iter_search_for_matching_inputs(amount_sent, new_total, spent_inputs, selected_inputs, i + 1) { return true; } - //on to next output + selected_inputs.pop(); } - // no match found in this search branch false }