Remove Clone from mempool Storage

This commit is contained in:
Conrado Gouvea 2021-09-09 14:28:52 -03:00 committed by Deirdre Connolly
parent 8825a52bb8
commit 34876113c2
3 changed files with 18 additions and 22 deletions

View File

@ -109,10 +109,10 @@ impl Mempool {
/// then it shouldn't be downloaded/verified. /// then it shouldn't be downloaded/verified.
fn should_download_or_verify(&mut self, txid: UnminedTxId) -> Result<(), MempoolError> { fn should_download_or_verify(&mut self, txid: UnminedTxId) -> Result<(), MempoolError> {
// Check if the transaction is already in the mempool. // Check if the transaction is already in the mempool.
if self.storage.clone().contains(&txid) { if self.storage.contains(&txid) {
return Err(MempoolError::InMempool); return Err(MempoolError::InMempool);
} }
if self.storage.clone().contains_rejected(&txid) { if self.storage.contains_rejected(&txid) {
return Err(MempoolError::Rejected); return Err(MempoolError::Rejected);
} }
Ok(()) Ok(())
@ -140,15 +140,15 @@ impl Service<Request> for Mempool {
fn call(&mut self, req: Request) -> Self::Future { fn call(&mut self, req: Request) -> Self::Future {
match req { match req {
Request::TransactionIds => { Request::TransactionIds => {
let res = self.storage.clone().tx_ids(); let res = self.storage.tx_ids();
async move { Ok(Response::TransactionIds(res)) }.boxed() async move { Ok(Response::TransactionIds(res)) }.boxed()
} }
Request::TransactionsById(ids) => { Request::TransactionsById(ids) => {
let rsp = Ok(self.storage.clone().transactions(ids)).map(Response::Transactions); let rsp = Ok(self.storage.transactions(ids)).map(Response::Transactions);
async move { rsp }.boxed() async move { rsp }.boxed()
} }
Request::RejectedTransactionIds(ids) => { Request::RejectedTransactionIds(ids) => {
let rsp = Ok(self.storage.clone().rejected_transactions(ids)) let rsp = Ok(self.storage.rejected_transactions(ids))
.map(Response::RejectedTransactionIds); .map(Response::RejectedTransactionIds);
async move { rsp }.boxed() async move { rsp }.boxed()
} }

View File

@ -31,7 +31,7 @@ pub enum State {
Excess, Excess,
} }
#[derive(Clone, Default)] #[derive(Default)]
pub struct Storage { pub struct Storage {
/// The set of verified transactions in the mempool. This is a /// The set of verified transactions in the mempool. This is a
/// cache of size [`MEMPOOL_SIZE`]. /// cache of size [`MEMPOOL_SIZE`].
@ -87,31 +87,32 @@ impl Storage {
/// Returns `true` if a [`UnminedTx`] matching an [`UnminedTxId`] is in /// Returns `true` if a [`UnminedTx`] matching an [`UnminedTxId`] is in
/// the mempool. /// the mempool.
#[allow(dead_code)] #[allow(dead_code)]
pub fn contains(self, txid: &UnminedTxId) -> bool { pub fn contains(&self, txid: &UnminedTxId) -> bool {
self.verified.iter().any(|tx| &tx.id == txid) self.verified.iter().any(|tx| &tx.id == txid)
} }
/// Returns the set of [`UnminedTxId`]s in the mempool. /// Returns the set of [`UnminedTxId`]s in the mempool.
pub fn tx_ids(self) -> Vec<UnminedTxId> { pub fn tx_ids(&self) -> Vec<UnminedTxId> {
self.verified.iter().map(|tx| tx.id).collect() self.verified.iter().map(|tx| tx.id).collect()
} }
/// Returns the set of [`Transaction`]s matching ids in the mempool. /// Returns the set of [`Transaction`]s matching ids in the mempool.
pub fn transactions(self, tx_ids: HashSet<UnminedTxId>) -> Vec<UnminedTx> { pub fn transactions(&self, tx_ids: HashSet<UnminedTxId>) -> Vec<UnminedTx> {
self.verified self.verified
.into_iter() .iter()
.filter(|tx| tx_ids.contains(&tx.id)) .filter(|tx| tx_ids.contains(&tx.id))
.cloned()
.collect() .collect()
} }
/// Returns `true` if a [`UnminedTx`] matching an [`UnminedTxId`] is in /// Returns `true` if a [`UnminedTx`] matching an [`UnminedTxId`] is in
/// the mempool rejected list. /// the mempool rejected list.
pub fn contains_rejected(self, txid: &UnminedTxId) -> bool { pub fn contains_rejected(&self, txid: &UnminedTxId) -> bool {
self.rejected.contains_key(txid) self.rejected.contains_key(txid)
} }
/// Returns the set of [`UnminedTxId`]s matching ids in the rejected list. /// Returns the set of [`UnminedTxId`]s matching ids in the rejected list.
pub fn rejected_transactions(self, tx_ids: HashSet<UnminedTxId>) -> Vec<UnminedTxId> { pub fn rejected_transactions(&self, tx_ids: HashSet<UnminedTxId>) -> Vec<UnminedTxId> {
tx_ids tx_ids
.into_iter() .into_iter()
.filter(|tx| self.rejected.contains_key(tx)) .filter(|tx| self.rejected.contains_key(tx))

View File

@ -36,7 +36,7 @@ fn mempool_storage_basic_for_network(network: Network) -> Result<()> {
// Make sure the last MEMPOOL_SIZE transactions we sent are in the verified // Make sure the last MEMPOOL_SIZE transactions we sent are in the verified
for tx in unmined_transactions.iter().rev().take(MEMPOOL_SIZE) { for tx in unmined_transactions.iter().rev().take(MEMPOOL_SIZE) {
assert!(storage.clone().contains(&tx.id)); assert!(storage.contains(&tx.id));
} }
// Anything greater should not be in the verified // Anything greater should not be in the verified
@ -44,7 +44,7 @@ fn mempool_storage_basic_for_network(network: Network) -> Result<()> {
.iter() .iter()
.take(unmined_transactions.len() - MEMPOOL_SIZE) .take(unmined_transactions.len() - MEMPOOL_SIZE)
{ {
assert!(!storage.clone().contains(&tx.id)); assert!(!storage.contains(&tx.id));
} }
// Query all the ids we have for rejected, get back `total - MEMPOOL_SIZE` // Query all the ids we have for rejected, get back `total - MEMPOOL_SIZE`
@ -55,18 +55,13 @@ fn mempool_storage_basic_for_network(network: Network) -> Result<()> {
.map(|tx| tx.id) .map(|tx| tx.id)
.collect(); .collect();
// Convert response to a `HashSet` as we need a fixed order to compare. // Convert response to a `HashSet` as we need a fixed order to compare.
let rejected_response: HashSet<UnminedTxId> = storage let rejected_response: HashSet<UnminedTxId> =
.clone() storage.rejected_transactions(all_ids).into_iter().collect();
.rejected_transactions(all_ids)
.into_iter()
.collect();
assert_eq!(rejected_response, rejected_ids); assert_eq!(rejected_response, rejected_ids);
// Use `contains_rejected` to make sure the first id stored is now rejected // Use `contains_rejected` to make sure the first id stored is now rejected
assert!(storage assert!(storage.contains_rejected(&unmined_transactions[0].id));
.clone()
.contains_rejected(&unmined_transactions[0].id));
// Use `contains_rejected` to make sure the last id stored is not rejected // Use `contains_rejected` to make sure the last id stored is not rejected
assert!(!storage.contains_rejected(&unmined_transactions[unmined_transactions.len() - 1].id)); assert!(!storage.contains_rejected(&unmined_transactions[unmined_transactions.len() - 1].id));