Skip to content

Commit

Permalink
fix: fix confusing names in get_balance functions (#3447)
Browse files Browse the repository at this point in the history
Description
---
Fix confusing names in `get_balance` functions

Motivation and Context
---
Comments as per PR #3446

How Has This Been Tested?
---
cargo clippy
cargo formal all
  • Loading branch information
hansieodendaal authored Oct 12, 2021
1 parent e23ceec commit 6cd9228
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
19 changes: 13 additions & 6 deletions base_layer/wallet/src/output_manager_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down Expand Up @@ -406,8 +406,15 @@ where
Ok(())
}

async fn get_balance(&self, current_chain_tip: Option<u64>) -> Result<Balance, OutputManagerError> {
let balance = self.resources.db.get_balance(current_chain_tip).await?;
async fn get_balance(
&self,
current_tip_for_time_lock_calculation: Option<u64>,
) -> Result<Balance, OutputManagerError> {
let balance = self
.resources
.db
.get_balance(current_tip_for_time_lock_calculation)
.await?;
trace!(target: LOG_TARGET, "Balance: {:?}", balance);
Ok(balance)
}
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 9 additions & 3 deletions base_layer/wallet/src/output_manager_service/storage/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>) -> Result<Balance, OutputManagerStorageError>;
fn get_balance(
&self,
current_tip_for_time_lock_calculation: Option<u64>,
) -> Result<Balance, OutputManagerStorageError>;
}

/// Holds the state of the KeyManager being used by the Output Manager Service
Expand Down Expand Up @@ -275,9 +278,12 @@ where T: OutputManagerBackend + 'static
Ok(())
}

pub async fn get_balance(&self, current_chain_tip: Option<u64>) -> Result<Balance, OutputManagerStorageError> {
pub async fn get_balance(
&self,
current_tip_for_time_lock_calculation: Option<u64>,
) -> Result<Balance, OutputManagerStorageError> {
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()))?
}
Expand Down
16 changes: 11 additions & 5 deletions base_layer/wallet/src/output_manager_service/storage/sqlite_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,13 @@ impl OutputManagerBackend for OutputManagerSqliteDatabase {
}
}

fn get_balance(&self, tip: Option<u64>) -> Result<Balance, OutputManagerStorageError> {
fn get_balance(
&self,
current_tip_for_time_lock_calculation: Option<u64>,
) -> Result<Balance, OutputManagerStorageError> {
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> {
Expand Down Expand Up @@ -1037,15 +1040,18 @@ impl OutputSql {
}

/// Return the available, time locked, pending incoming and pending outgoing balance
pub fn get_balance(tip: Option<u64>, conn: &SqliteConnection) -> Result<Balance, OutputManagerStorageError> {
pub fn get_balance(
current_tip_for_time_lock_calculation: Option<u64>,
conn: &SqliteConnection,
) -> Result<Balance, OutputManagerStorageError> {
#[derive(QueryableByName, Clone)]
struct BalanceQueryResult {
#[sql_type = "diesel::sql_types::BigInt"]
amount: i64,
#[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 = ? \
Expand All @@ -1063,7 +1069,7 @@ impl OutputSql {
.bind::<diesel::sql_types::Integer, _>(OutputStatus::Unspent as i32)
// time_locked_balance
.bind::<diesel::sql_types::Integer, _>(OutputStatus::Unspent as i32)
.bind::<diesel::sql_types::BigInt, _>(val as i64)
.bind::<diesel::sql_types::BigInt, _>(current_tip as i64)
// pending_incoming_balance
.bind::<diesel::sql_types::Integer, _>(OutputStatus::EncumberedToBeReceived as i32)
.bind::<diesel::sql_types::Integer, _>(OutputStatus::ShortTermEncumberedToBeReceived as i32)
Expand Down

0 comments on commit 6cd9228

Please sign in to comment.