Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix confusing names in get_balance functions #3447

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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