-
Notifications
You must be signed in to change notification settings - Fork 215
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
feat: add sql query to obtain balance #3446
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,6 +23,7 @@ | |||||
use crate::{ | ||||||
output_manager_service::{ | ||||||
error::OutputManagerStorageError, | ||||||
service::Balance, | ||||||
storage::{ | ||||||
database::{DbKey, DbKeyValuePair, DbValue, KeyManagerState, OutputManagerBackend, WriteOperation}, | ||||||
models::{DbUnblindedOutput, KnownOneSidedPaymentScript, OutputStatus}, | ||||||
|
@@ -38,7 +39,7 @@ use crate::{ | |||||
}; | ||||||
use aes_gcm::{aead::Error as AeadError, Aes256Gcm, Error}; | ||||||
use chrono::{NaiveDateTime, Utc}; | ||||||
use diesel::{prelude::*, result::Error as DieselError, SqliteConnection}; | ||||||
use diesel::{prelude::*, result::Error as DieselError, sql_query, SqliteConnection}; | ||||||
use log::*; | ||||||
use std::{ | ||||||
convert::{TryFrom, TryInto}, | ||||||
|
@@ -330,24 +331,6 @@ impl OutputManagerBackend for OutputManagerSqliteDatabase { | |||||
.collect::<Result<Vec<_>, _>>() | ||||||
} | ||||||
|
||||||
fn fetch_pending_outgoing_outputs(&self) -> Result<Vec<DbUnblindedOutput>, OutputManagerStorageError> { | ||||||
let conn = self.database_connection.acquire_lock(); | ||||||
|
||||||
let mut outputs = OutputSql::index_status(OutputStatus::EncumberedToBeSpent, &conn)?; | ||||||
outputs.extend(OutputSql::index_status( | ||||||
OutputStatus::ShortTermEncumberedToBeSpent, | ||||||
&conn, | ||||||
)?); | ||||||
outputs.extend(OutputSql::index_status(OutputStatus::SpentMinedUnconfirmed, &conn)?); | ||||||
for o in outputs.iter_mut() { | ||||||
self.decrypt_if_necessary(o)?; | ||||||
} | ||||||
outputs | ||||||
.iter() | ||||||
.map(|o| DbUnblindedOutput::try_from(o.clone())) | ||||||
.collect::<Result<Vec<_>, _>>() | ||||||
} | ||||||
|
||||||
fn write(&self, op: WriteOperation) -> Result<Option<DbValue>, OutputManagerStorageError> { | ||||||
let conn = self.database_connection.acquire_lock(); | ||||||
|
||||||
|
@@ -650,6 +633,12 @@ impl OutputManagerBackend for OutputManagerSqliteDatabase { | |||||
} | ||||||
} | ||||||
|
||||||
fn get_balance(&self, tip: Option<u64>) -> Result<Balance, OutputManagerStorageError> { | ||||||
let conn = self.database_connection.acquire_lock(); | ||||||
|
||||||
OutputSql::get_balance(tip, &(*conn)) | ||||||
} | ||||||
|
||||||
fn cancel_pending_transaction(&self, tx_id: TxId) -> Result<(), OutputManagerStorageError> { | ||||||
let conn = self.database_connection.acquire_lock(); | ||||||
|
||||||
|
@@ -1047,6 +1036,104 @@ impl OutputSql { | |||||
.first::<OutputSql>(conn)?) | ||||||
} | ||||||
|
||||||
/// Return the available, time locked, pending incoming and pending outgoing balance | ||||||
pub fn get_balance(tip: 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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the generic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||||||
let balance_query = sql_query( | ||||||
"SELECT coalesce(sum(value), 0) as amount, 'available_balance' as category \ | ||||||
FROM outputs WHERE status = ? \ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should take the mined heights into account when the tip is specified
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a job for the validation code though, I think it is cleaner that this function is based purely on the status. |
||||||
UNION ALL \ | ||||||
SELECT coalesce(sum(value), 0) as amount, 'time_locked_balance' as category \ | ||||||
FROM outputs WHERE status = ? AND maturity > ? \ | ||||||
UNION ALL \ | ||||||
SELECT coalesce(sum(value), 0) as amount, 'pending_incoming_balance' as category \ | ||||||
FROM outputs WHERE status = ? OR status = ? OR status = ? \ | ||||||
UNION ALL \ | ||||||
SELECT coalesce(sum(value), 0) as amount, 'pending_outgoing_balance' as category \ | ||||||
FROM outputs WHERE status = ? OR status = ? OR status = ?", | ||||||
) | ||||||
// available_balance | ||||||
.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) | ||||||
// pending_incoming_balance | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::EncumberedToBeReceived as i32) | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::ShortTermEncumberedToBeReceived as i32) | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::UnspentMinedUnconfirmed as i32) | ||||||
// pending_outgoing_balance | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::EncumberedToBeSpent as i32) | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::ShortTermEncumberedToBeSpent as i32) | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::SpentMinedUnconfirmed as i32); | ||||||
balance_query.load::<BalanceQueryResult>(conn)? | ||||||
} else { | ||||||
let balance_query = sql_query( | ||||||
"SELECT coalesce(sum(value), 0) as amount, 'available_balance' as category \ | ||||||
FROM outputs WHERE status = ? \ | ||||||
UNION ALL \ | ||||||
SELECT coalesce(sum(value), 0) as amount, 'pending_incoming_balance' as category \ | ||||||
FROM outputs WHERE status = ? OR status = ? OR status = ? \ | ||||||
UNION ALL \ | ||||||
SELECT coalesce(sum(value), 0) as amount, 'pending_outgoing_balance' as category \ | ||||||
FROM outputs WHERE status = ? OR status = ? OR status = ?", | ||||||
) | ||||||
// available_balance | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::Unspent as i32) | ||||||
// pending_incoming_balance | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::EncumberedToBeReceived as i32) | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::ShortTermEncumberedToBeReceived as i32) | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::UnspentMinedUnconfirmed as i32) | ||||||
// pending_outgoing_balance | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::EncumberedToBeSpent as i32) | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::ShortTermEncumberedToBeSpent as i32) | ||||||
.bind::<diesel::sql_types::Integer, _>(OutputStatus::SpentMinedUnconfirmed as i32); | ||||||
balance_query.load::<BalanceQueryResult>(conn)? | ||||||
}; | ||||||
let mut available_balance = None; | ||||||
let mut time_locked_balance = Some(None); | ||||||
let mut pending_incoming_balance = None; | ||||||
let mut pending_outgoing_balance = None; | ||||||
for balance in balance_query_result.clone() { | ||||||
match balance.category.as_str() { | ||||||
"available_balance" => available_balance = Some(MicroTari::from(balance.amount as u64)), | ||||||
"time_locked_balance" => time_locked_balance = Some(Some(MicroTari::from(balance.amount as u64))), | ||||||
"pending_incoming_balance" => pending_incoming_balance = Some(MicroTari::from(balance.amount as u64)), | ||||||
"pending_outgoing_balance" => pending_outgoing_balance = Some(MicroTari::from(balance.amount as u64)), | ||||||
_ => { | ||||||
return Err(OutputManagerStorageError::UnexpectedResult( | ||||||
"Unexpected category in balance query".to_string(), | ||||||
)) | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
Ok(Balance { | ||||||
available_balance: available_balance.ok_or_else(|| { | ||||||
OutputManagerStorageError::UnexpectedResult("Available balance could not be calculated".to_string()) | ||||||
})?, | ||||||
time_locked_balance: time_locked_balance.ok_or_else(|| { | ||||||
OutputManagerStorageError::UnexpectedResult("Time locked balance could not be calculated".to_string()) | ||||||
})?, | ||||||
pending_incoming_balance: pending_incoming_balance.ok_or_else(|| { | ||||||
OutputManagerStorageError::UnexpectedResult( | ||||||
"Pending incoming balance could not be calculated".to_string(), | ||||||
) | ||||||
})?, | ||||||
pending_outgoing_balance: pending_outgoing_balance.ok_or_else(|| { | ||||||
OutputManagerStorageError::UnexpectedResult( | ||||||
"Pending outgoing balance could not be calculated".to_string(), | ||||||
) | ||||||
})?, | ||||||
}) | ||||||
} | ||||||
|
||||||
pub fn find_by_commitment( | ||||||
commitment: &[u8], | ||||||
conn: &SqliteConnection, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be clearer what the
tip
argument is for if we rename it to something liketip_for_timelock_calculation
. Currently it is not clear what thetip
is for and that it actually affects whether the timelock balance is returned or not. The comment should also make the behaviour of the function clear when the tip is provided or not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will make work these two comments into a new small PR after the merge.