Skip to content

Commit

Permalink
Fix names that are exact or effects depending on the list
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 authored and conradoplg committed Oct 7, 2021
1 parent eab95fd commit 0d52fec
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions zebrad/src/components/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl Mempool {
if storage.contains_transaction_exact(&txid) {
return Err(MempoolError::InMempool);
}
if let Some(error) = storage.rejection_error_exact(&txid) {
if let Some(error) = storage.rejection_error(&txid) {
return Err(error);
}
Ok(())
Expand Down Expand Up @@ -266,7 +266,7 @@ impl Service<Request> for Mempool {
async move { Ok(Response::Transactions(res)) }.boxed()
}
Request::RejectedTransactionIds(ids) => {
let res = storage.rejected_transactions_exact(ids).collect();
let res = storage.rejected_transactions(ids).collect();
async move { Ok(Response::RejectedTransactionIds(res)) }.boxed()
}
Request::Queue(gossiped_txs) => {
Expand Down
24 changes: 12 additions & 12 deletions zebrad/src/components/mempool/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Storage {
let tx_id = tx.id;

// First, check if we have a cached rejection for this transaction.
if let Some(error) = self.rejection_error_exact(&tx.id) {
if let Some(error) = self.rejection_error(&tx.id) {
return Err(error);
}

Expand Down Expand Up @@ -214,11 +214,11 @@ impl Storage {
self.rejected_exact.len() + self.rejected_same_effects.len()
}

/// Returns `true` if a [`UnminedTx`] exactly matching an [`UnminedTxId`] is in
/// the mempool rejected list.
/// Returns `true` if a [`UnminedTx`] matching an [`UnminedTxId`] is in
/// any mempool rejected list.
///
/// This matches the exact transaction, with identical blockchain effects, signatures, and proofs.
pub fn rejection_error_exact(&self, txid: &UnminedTxId) -> Option<MempoolError> {
/// This matches transactions based on each rejection list's matching rule.
pub fn rejection_error(&self, txid: &UnminedTxId) -> Option<MempoolError> {
if let Some(exact_error) = self.rejected_exact.get(txid) {
return Some(exact_error.clone().into());
}
Expand All @@ -230,23 +230,23 @@ impl Storage {
None
}

/// Returns the set of [`UnminedTxId`]s exactly matching ids in the rejected list.
/// Returns the set of [`UnminedTxId`]s matching `tx_ids` in the rejected list.
///
/// This matches the exact transaction, with identical blockchain effects, signatures, and proofs.
pub fn rejected_transactions_exact(
/// This matches transactions based on each rejection list's matching rule.
pub fn rejected_transactions(
&self,
tx_ids: HashSet<UnminedTxId>,
) -> impl Iterator<Item = UnminedTxId> + '_ {
tx_ids
.into_iter()
.filter(move |txid| self.contains_rejected_exact(txid))
.filter(move |txid| self.contains_rejected(txid))
}

/// Returns `true` if a [`UnminedTx`] exactly matching the supplied [`UnminedTxId`] is in
/// Returns `true` if a [`UnminedTx`] matching the supplied [`UnminedTxId`] is in
/// the mempool rejected list.
///
/// This matches the exact transaction, with identical blockchain effects, signatures, and proofs.
pub fn contains_rejected_exact(&self, txid: &UnminedTxId) -> bool {
/// This matches transactions based on each rejection list's matching rule.
pub fn contains_rejected(&self, txid: &UnminedTxId) -> bool {
self.rejected_exact.contains_key(txid)
|| self.rejected_same_effects.contains_key(&txid.mined_id())
}
Expand Down
2 changes: 1 addition & 1 deletion zebrad/src/components/mempool/storage/tests/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ proptest! {
Err(MempoolError::StorageEffects(SameEffectsRejectionError::SpendConflict))
);

assert!(storage.contains_rejected_exact(&id_to_reject));
assert!(storage.contains_rejected(&id_to_reject));

storage.clear();
}
Expand Down
10 changes: 4 additions & 6 deletions zebrad/src/components/mempool/storage/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,17 @@ fn mempool_storage_basic_for_network(network: Network) -> Result<()> {
let all_ids: HashSet<UnminedTxId> = unmined_transactions.iter().map(|tx| tx.id).collect();

// Convert response to a `HashSet`, because the order of the response doesn't matter.
let rejected_response: HashSet<UnminedTxId> = storage
.rejected_transactions_exact(all_ids)
.into_iter()
.collect();
let rejected_response: HashSet<UnminedTxId> =
storage.rejected_transactions(all_ids).into_iter().collect();

let rejected_ids = expected_to_be_rejected.iter().map(|tx| tx.id).collect();

assert_eq!(rejected_response, rejected_ids);

// Make sure the first id stored is now rejected
assert!(storage.contains_rejected_exact(&expected_to_be_rejected[0].id));
assert!(storage.contains_rejected(&expected_to_be_rejected[0].id));
// Make sure the last id stored is not rejected
assert!(!storage.contains_rejected_exact(&expected_in_mempool[0].id));
assert!(!storage.contains_rejected(&expected_in_mempool[0].id));

Ok(())
}

0 comments on commit 0d52fec

Please sign in to comment.