diff --git a/base_layer/wallet/src/output_manager_service/service.rs b/base_layer/wallet/src/output_manager_service/service.rs index 07d1bb4c8b..238e3dd476 100644 --- a/base_layer/wallet/src/output_manager_service/service.rs +++ b/base_layer/wallet/src/output_manager_service/service.rs @@ -201,11 +201,11 @@ where .await .map(|_| OutputManagerResponse::OutputMetadataSignatureUpdated), OutputManagerRequest::GetBalance => { - let current_chain_tip = match self.base_node_service.get_chain_metadata().await { + let current_tip_for_time_lock_calculation = match self.base_node_service.get_chain_metadata().await { Ok(metadata) => metadata.map(|m| m.height_of_longest_chain()), Err(_) => None, }; - self.get_balance(current_chain_tip) + self.get_balance(current_tip_for_time_lock_calculation) .await .map(OutputManagerResponse::Balance) }, @@ -406,8 +406,15 @@ where Ok(()) } - async fn get_balance(&self, current_chain_tip: Option) -> Result { - let balance = self.resources.db.get_balance(current_chain_tip).await?; + async fn get_balance( + &self, + current_tip_for_time_lock_calculation: Option, + ) -> Result { + let balance = self + .resources + .db + .get_balance(current_tip_for_time_lock_calculation) + .await?; trace!(target: LOG_TARGET, "Balance: {:?}", balance); Ok(balance) } @@ -938,8 +945,8 @@ where let enough_spendable = utxos_total_value > amount + fee_with_change; if !perfect_utxo_selection && !enough_spendable { - let current_chain_tip = chain_metadata.map(|cm| cm.height_of_longest_chain()); - let balance = self.get_balance(current_chain_tip).await?; + let current_tip_for_time_lock_calculation = chain_metadata.map(|cm| cm.height_of_longest_chain()); + let balance = self.get_balance(current_tip_for_time_lock_calculation).await?; let pending_incoming = balance.pending_incoming_balance; if utxos_total_value + pending_incoming >= amount + fee_with_change { return Err(OutputManagerError::FundsPending); diff --git a/base_layer/wallet/src/output_manager_service/storage/database.rs b/base_layer/wallet/src/output_manager_service/storage/database.rs index 6d7c6ae910..f70197c95e 100644 --- a/base_layer/wallet/src/output_manager_service/storage/database.rs +++ b/base_layer/wallet/src/output_manager_service/storage/database.rs @@ -119,7 +119,10 @@ pub trait OutputManagerBackend: Send + Sync + Clone { /// Reinstate a cancelled inbound output fn reinstate_cancelled_inbound_output(&self, tx_id: TxId) -> Result<(), OutputManagerStorageError>; /// Return the available, time locked, pending incoming and pending outgoing balance - fn get_balance(&self, tip: Option) -> Result; + fn get_balance( + &self, + current_tip_for_time_lock_calculation: Option, + ) -> Result; } /// Holds the state of the KeyManager being used by the Output Manager Service @@ -275,9 +278,12 @@ where T: OutputManagerBackend + 'static Ok(()) } - pub async fn get_balance(&self, current_chain_tip: Option) -> Result { + pub async fn get_balance( + &self, + current_tip_for_time_lock_calculation: Option, + ) -> Result { let db_clone = self.db.clone(); - tokio::task::spawn_blocking(move || db_clone.get_balance(current_chain_tip)) + tokio::task::spawn_blocking(move || db_clone.get_balance(current_tip_for_time_lock_calculation)) .await .map_err(|err| OutputManagerStorageError::BlockingTaskSpawnError(err.to_string()))? } diff --git a/base_layer/wallet/src/output_manager_service/storage/sqlite_db.rs b/base_layer/wallet/src/output_manager_service/storage/sqlite_db.rs index 487ac37097..8a448ff1fb 100644 --- a/base_layer/wallet/src/output_manager_service/storage/sqlite_db.rs +++ b/base_layer/wallet/src/output_manager_service/storage/sqlite_db.rs @@ -633,10 +633,13 @@ impl OutputManagerBackend for OutputManagerSqliteDatabase { } } - fn get_balance(&self, tip: Option) -> Result { + fn get_balance( + &self, + current_tip_for_time_lock_calculation: Option, + ) -> Result { let conn = self.database_connection.acquire_lock(); - OutputSql::get_balance(tip, &(*conn)) + OutputSql::get_balance(current_tip_for_time_lock_calculation, &(*conn)) } fn cancel_pending_transaction(&self, tx_id: TxId) -> Result<(), OutputManagerStorageError> { @@ -1037,7 +1040,10 @@ impl OutputSql { } /// Return the available, time locked, pending incoming and pending outgoing balance - pub fn get_balance(tip: Option, conn: &SqliteConnection) -> Result { + pub fn get_balance( + current_tip_for_time_lock_calculation: Option, + conn: &SqliteConnection, + ) -> Result { #[derive(QueryableByName, Clone)] struct BalanceQueryResult { #[sql_type = "diesel::sql_types::BigInt"] @@ -1045,7 +1051,7 @@ impl OutputSql { #[sql_type = "diesel::sql_types::Text"] category: String, } - let balance_query_result = if let Some(val) = tip { + let balance_query_result = if let Some(current_tip) = current_tip_for_time_lock_calculation { let balance_query = sql_query( "SELECT coalesce(sum(value), 0) as amount, 'available_balance' as category \ FROM outputs WHERE status = ? \ @@ -1063,7 +1069,7 @@ impl OutputSql { .bind::(OutputStatus::Unspent as i32) // time_locked_balance .bind::(OutputStatus::Unspent as i32) - .bind::(val as i64) + .bind::(current_tip as i64) // pending_incoming_balance .bind::(OutputStatus::EncumberedToBeReceived as i32) .bind::(OutputStatus::ShortTermEncumberedToBeReceived as i32)