Skip to content

Commit

Permalink
Update asset_query.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
bvrooman committed Sep 12, 2023
1 parent 01673f8 commit 38aa61e
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions crates/fuel-core/src/query/balance/asset_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,8 @@ impl<'a> AssetsQuery<'a> {
}
}

/// Returns the iterator over all valid(spendable, allowed by `exclude`) coins of the `owner`.
///
/// # Note: The coins of different type are not grouped by the `asset_id`.
// TODO: Optimize this by creating an index
// https://github.com/FuelLabs/fuel-core/issues/588
pub fn coins(&self) -> impl Iterator<Item = StorageResult<CoinType>> + '_ {
let coins_iter = self
.database
fn coins_iter(&self) -> impl Iterator<Item = StorageResult<CoinType>> + '_ {
self.database
.owned_coins_ids(self.owner, None, IterDirection::Forward)
.map(|id| id.map(CoinId::from))
.filter_ok(|id| {
Expand Down Expand Up @@ -121,15 +115,11 @@ impl<'a> AssetsQuery<'a> {
} else {
true
}
});
})
}

let predicate = self
.assets
.as_ref()
.map(|assets| assets.contains(self.base_asset_id))
.unwrap_or(true);
let messages_iter = self
.database
fn messages_iter(&self) -> impl Iterator<Item = StorageResult<CoinType>> + '_ {
self.database
.owned_message_ids(self.owner, None, IterDirection::Forward)
.map(|id| id.map(CoinId::from))
.filter_ok(|id| {
Expand Down Expand Up @@ -160,9 +150,30 @@ impl<'a> AssetsQuery<'a> {
)
})
})
.take_while(move |_| predicate);
.into_iter()
}

coins_iter.chain(messages_iter)
fn has_base_asset(&self) -> bool {
self.assets
.as_ref()
.map(|assets| assets.contains(self.base_asset_id))
.unwrap_or(true)
}

/// Returns the iterator over all valid(spendable, allowed by `exclude`) coins of the `owner`.
///
/// # Note: The coins of different type are not grouped by the `asset_id`.
// TODO: Optimize this by creating an index
// https://github.com/FuelLabs/fuel-core/issues/588
pub fn coins(&self) -> Box<dyn Iterator<Item = StorageResult<CoinType>> + '_> {
let coins_iter = self.coins_iter();
if self.has_base_asset() {
let messages_iter = self.messages_iter();
let iter = coins_iter.chain(messages_iter);
Box::new(iter)
} else {
Box::new(coins_iter)
}
}
}

Expand Down

0 comments on commit 38aa61e

Please sign in to comment.