mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-19 13:44:55 +01:00
feat: optimize SQL balance calculation (#1152)
* feat: optimize SQL balance calculation replace proof-fetching approach with SUM aggregation - add get_balance() method to Database trait - implement SQL SUM aggregation in cdk-sql-common - update total_balance() to use get_balance() instead of get_unspent_proofs() - redb impl maintains existing behavior --------- Co-authored-by: thesimplekid <tsk@thesimplekid.com> Co-authored-by: Cesar Rodas <cesar@rodasm.com.py>
This commit is contained in:
@@ -721,6 +721,18 @@ impl WalletDatabase for WalletRedbDatabase {
|
||||
Ok(proofs)
|
||||
}
|
||||
|
||||
async fn get_balance(
|
||||
&self,
|
||||
mint_url: Option<MintUrl>,
|
||||
unit: Option<CurrencyUnit>,
|
||||
state: Option<Vec<State>>,
|
||||
) -> Result<u64, database::Error> {
|
||||
// For redb, we still need to fetch all proofs and sum them
|
||||
// since redb doesn't have SQL aggregation
|
||||
let proofs = self.get_proofs(mint_url, unit, state, None).await?;
|
||||
Ok(proofs.iter().map(|p| u64::from(p.proof.amount)).sum())
|
||||
}
|
||||
|
||||
async fn update_proofs_state(
|
||||
&self,
|
||||
ys: Vec<PublicKey>,
|
||||
|
||||
Reference in New Issue
Block a user