Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
fix(mempool): check exsit before insert a transaction (#257)
Browse files Browse the repository at this point in the history
* fix: check exsit before insert transaction

* cargo fmt

* cargo fmt
  • Loading branch information
Eason Gao authored Apr 27, 2020
1 parent 4f1a985 commit be3c139
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion core/mempool/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,16 @@ impl<V> Bucket<V>
where
V: Send + Sync + Clone,
{
/// Before inserting a transaction into the bucket, you must check whether
/// the transaction is in the bucket first. Never use the insert function to
/// check this.
fn insert(&self, hash: Hash, value: V) -> Option<V> {
self.store.write().insert(hash, value)
let mut lock_data = self.store.write();
if lock_data.contains_key(&hash) {
Some(value)
} else {
lock_data.insert(hash, value)
}
}

fn contains_key(&self, hash: &Hash) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion core/mempool/src/tx_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ mod tests {
let tx_wrapper_1 = TxWrapper::new(tx.clone());
map.insert(tx.tx_hash.clone(), Arc::new(tx_wrapper_1));
let shared_tx_1 = map.get(&tx.tx_hash).unwrap();
assert!(!shared_tx_1.is_removed());
assert!(shared_tx_1.is_removed());
}

#[bench]
Expand Down

0 comments on commit be3c139

Please sign in to comment.