Skip to content

Commit

Permalink
pr comment
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e committed Nov 26, 2024
1 parent 6bbe515 commit 74b532a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
17 changes: 0 additions & 17 deletions packages/fuels-accounts/src/coin_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,6 @@ impl CoinsCache {
}
}

pub fn find_already_inserted<'a>(
&mut self,
coin_ids: impl IntoIterator<Item = (&'a (Bech32Address, AssetId), &'a Vec<CoinTypeId>)>,
) -> Option<(CoinCacheKey, CoinTypeId)> {
for (key, ids) in coin_ids {
if let Some(items) = self.items.get(key) {
for id in ids {
if items.contains(&CoinCacheItem::new(id.clone())) {
return Some((key.clone(), id.clone()));
}
}
}
}

None
}

pub fn get_active(&mut self, key: &CoinCacheKey) -> HashSet<CoinTypeId> {
self.remove_expired_entries(key);

Expand Down
26 changes: 23 additions & 3 deletions packages/fuels-accounts/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,36 @@ impl Provider {
Ok(self.client.submit(&tx.into()).await?)
}

#[cfg(feature = "coin-cache")]
async fn find_in_cache<'a>(
&self,
coin_ids: impl IntoIterator<Item = (&'a (Bech32Address, AssetId), &'a Vec<CoinTypeId>)>,
) -> Option<((Bech32Address, AssetId), CoinTypeId)> {
for (key, ids) in coin_ids {
let items = self.cache.lock().await.get_active(key);

if items.is_empty() {
continue;
}

for id in ids {
if items.contains(id) {
return Some((key.clone(), id.clone()));
}
}
}

None
}

#[cfg(feature = "coin-cache")]
async fn check_inputs_already_in_cache<'a>(
&self,
coin_ids: impl IntoIterator<Item = (&'a (Bech32Address, AssetId), &'a Vec<CoinTypeId>)>,
) -> Result<()> {
use fuels_core::types::errors::{transaction, Error};

if let Some(((addr, asset_id), coin_type_id)) =
self.cache.lock().await.find_already_inserted(coin_ids)
{
if let Some(((addr, asset_id), coin_type_id)) = self.find_in_cache(coin_ids).await {
let msg = match coin_type_id {
CoinTypeId::UtxoId(utxo_id) => format!("coin with utxo_id: `{utxo_id:x}`"),
CoinTypeId::Nonce(nonce) => format!("message with nonce: `{nonce}`"),
Expand Down

0 comments on commit 74b532a

Please sign in to comment.