Skip to content

Commit

Permalink
daemon: mempool remove_tx function
Browse files Browse the repository at this point in the history
  • Loading branch information
Slixe committed Jan 3, 2024
1 parent 02819ec commit 1afa0f9
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions xelis_daemon/src/core/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,66 @@ impl Mempool {
Ok(())
}

// Remove a TX using its hash from mempool
// This will recalculate the cache bounds
pub fn remove_tx(&mut self, hash: &Hash) -> Result<(), BlockchainError> {
let tx = self.txs.remove(hash).ok_or_else(|| BlockchainError::TxNotFound(hash.clone()))?;
// remove the tx hash from sorted txs
let key = tx.get_tx().get_owner();
let mut delete = false;
if let Some(cache) = self.nonces_cache.get_mut(key) {
if !cache.txs.remove(hash) {
warn!("TX {} not found in mempool while deleting", hash);
} else {
trace!("TX {} removed from cache", hash);
delete = cache.txs.is_empty();
if !delete {
trace!("Updating cache bounds");
let mut max: Option<u64> = None;
let mut min: Option<u64> = None;
// Update cache bounds
for tx_hash in cache.txs.iter() {
let tx = self.txs.get(tx_hash).ok_or_else(|| BlockchainError::TxNotFound(hash.clone()))?;
let nonce = tx.get_tx().get_nonce();

// Update cache highest bounds
if let Some(v) = max.clone() {
if v < nonce {
max = Some(nonce);
}
} else {
max = Some(nonce);
}

// Update cache lowest bounds
if let Some(v) = min.clone() {
if v > nonce {
min = Some(nonce);
}
} else {
min = Some(nonce);
}
}

if let (Some(min), Some(max)) = (min, max) {
trace!("Updating cache bounds to {}-{}", min, max);
cache.min = min;
cache.max = max;
}
}
}
} else {
warn!("No cache found for owner {} while deleting TX {}", tx.get_tx().get_owner(), hash);
}

if delete {
trace!("Removing empty nonce cache for owner {}", key);
self.nonces_cache.remove(key);
}

Ok(())
}

pub fn get_nonces_cache(&self) -> &HashMap<PublicKey, NonceCache> {
&self.nonces_cache
}
Expand Down

0 comments on commit 1afa0f9

Please sign in to comment.