From 9bf906447622e92470de47da16b84bec0d84bd3e Mon Sep 17 00:00:00 2001 From: Alexis Asseman Date: Fri, 27 Oct 2023 16:56:01 -0700 Subject: [PATCH] refactor: update checks in received receipt for batches Signed-off-by: Alexis Asseman --- tap_core/src/tap_manager/manager.rs | 10 +- tap_core/src/tap_receipt/receipt_auditor.rs | 133 +++++++++---------- tap_core/src/tap_receipt/received_receipt.rs | 15 +++ 3 files changed, 82 insertions(+), 76 deletions(-) diff --git a/tap_core/src/tap_manager/manager.rs b/tap_core/src/tap_manager/manager.rs index 5d8ccee6..5fb3ce41 100644 --- a/tap_core/src/tap_manager/manager.rs +++ b/tap_core/src/tap_manager/manager.rs @@ -240,10 +240,12 @@ impl< received_receipts.into_iter().map(|e| e.1).collect(); for check in self.required_checks.iter() { - self.receipt_auditor - .check_bunch(check, &mut received_receipts) - .await - .unwrap(); + ReceivedReceipt::perform_check_batch( + &mut received_receipts, + check, + &self.receipt_auditor, + ) + .await?; } for received_receipt in received_receipts { diff --git a/tap_core/src/tap_receipt/receipt_auditor.rs b/tap_core/src/tap_receipt/receipt_auditor.rs index e70dda25..e0242829 100644 --- a/tap_core/src/tap_receipt/receipt_auditor.rs +++ b/tap_core/src/tap_receipt/receipt_auditor.rs @@ -63,29 +63,23 @@ impl ReceiptAuditor { } } - pub async fn check_bunch( + pub async fn check_batch( &self, receipt_check: &ReceiptCheck, received_receipts: &mut [ReceivedReceipt], - ) -> Result<()> { - let result = match receipt_check { - ReceiptCheck::CheckUnique => self.check_uniqueness_bunch(received_receipts).await, + ) -> Vec> { + match receipt_check { + ReceiptCheck::CheckUnique => self.check_uniqueness_batch(received_receipts).await, ReceiptCheck::CheckAllocationId => { - self.check_allocation_id_bunch(received_receipts).await + self.check_allocation_id_batch(received_receipts).await } - ReceiptCheck::CheckSignature => self.check_signature_bunch(received_receipts).await, - ReceiptCheck::CheckTimestamp => self.check_timestamp_bunch(received_receipts).await, - ReceiptCheck::CheckValue => self.check_value_bunch(received_receipts).await, + ReceiptCheck::CheckSignature => self.check_signature_batch(received_receipts).await, + ReceiptCheck::CheckTimestamp => self.check_timestamp_batch(received_receipts).await, + ReceiptCheck::CheckValue => self.check_value_batch(received_receipts).await, ReceiptCheck::CheckAndReserveEscrow => { - self.check_and_reserve_escrow_bunch(received_receipts).await + self.check_and_reserve_escrow_batch(received_receipts).await } - }; - - for received_receipt in received_receipts.iter_mut() { - received_receipt.update_state(); } - - result } async fn check_uniqueness( @@ -106,14 +100,16 @@ impl ReceiptAuditor { Ok(()) } - async fn check_uniqueness_bunch( + async fn check_uniqueness_batch( &self, received_receipts: &mut [ReceivedReceipt], - ) -> Result<()> { - // If at least one of the receipts in the bunch hasn't been checked for uniqueness yet, check the whole bunch. + ) -> Vec> { + let mut results = Vec::new(); + + // If at least one of the receipts in the batch hasn't been checked for uniqueness yet, check the whole batch. if received_receipts .iter() - .filter(|r| r.checks.get(&ReceiptCheck::CheckUnique).is_some()) + .filter(|r| r.checks.contains_key(&ReceiptCheck::CheckUnique)) .any(|r| r.checks[&ReceiptCheck::CheckUnique].is_none()) { let mut signatures: HashSet = HashSet::new(); @@ -121,21 +117,14 @@ impl ReceiptAuditor { for received_receipt in received_receipts { let signature = received_receipt.signed_receipt.signature; if signatures.insert(signature) { - received_receipt - .update_check(&ReceiptCheck::CheckUnique, Some(Ok(()))) - .unwrap(); + results.push(Ok(())); } else { - received_receipt - .update_check( - &ReceiptCheck::CheckUnique, - Some(Err(ReceiptError::NonUniqueReceipt)), - ) - .unwrap(); + results.push(Err(ReceiptError::NonUniqueReceipt)); } } } - Ok(()) + results } async fn check_allocation_id( @@ -157,25 +146,23 @@ impl ReceiptAuditor { Ok(()) } - async fn check_allocation_id_bunch( + async fn check_allocation_id_batch( &self, received_receipts: &mut [ReceivedReceipt], - ) -> Result<()> { + ) -> Vec> { + let mut results = Vec::new(); + for received_receipt in received_receipts .iter_mut() - .filter(|r| r.checks.get(&ReceiptCheck::CheckAllocationId).is_some()) + .filter(|r| r.checks.contains_key(&ReceiptCheck::CheckAllocationId)) { if received_receipt.checks[&ReceiptCheck::CheckAllocationId].is_none() { let signed_receipt = &received_receipt.signed_receipt; - let result = self.check_allocation_id(signed_receipt).await; - - received_receipt - .update_check(&ReceiptCheck::CheckAllocationId, Some(result)) - .unwrap(); + results.push(self.check_allocation_id(signed_receipt).await); } } - Ok(()) + results } async fn check_timestamp( @@ -192,22 +179,23 @@ impl ReceiptAuditor { Ok(()) } - async fn check_timestamp_bunch(&self, received_receipts: &mut [ReceivedReceipt]) -> Result<()> { + async fn check_timestamp_batch( + &self, + received_receipts: &mut [ReceivedReceipt], + ) -> Vec> { + let mut results = Vec::new(); + for received_receipt in received_receipts .iter_mut() - .filter(|r| r.checks.get(&ReceiptCheck::CheckTimestamp).is_some()) + .filter(|r| r.checks.contains_key(&ReceiptCheck::CheckTimestamp)) { if received_receipt.checks[&ReceiptCheck::CheckTimestamp].is_none() { let signed_receipt = &received_receipt.signed_receipt; - let result = self.check_timestamp(signed_receipt).await; - - received_receipt - .update_check(&ReceiptCheck::CheckTimestamp, Some(result)) - .unwrap(); + results.push(self.check_timestamp(signed_receipt).await); } } - Ok(()) + results } async fn check_value( @@ -230,24 +218,26 @@ impl ReceiptAuditor { Ok(()) } - async fn check_value_bunch(&self, received_receipts: &mut [ReceivedReceipt]) -> Result<()> { + async fn check_value_batch( + &self, + received_receipts: &mut [ReceivedReceipt], + ) -> Vec> { + let mut results = Vec::new(); + for received_receipt in received_receipts .iter_mut() - .filter(|r| r.checks.get(&ReceiptCheck::CheckValue).is_some()) + .filter(|r| r.checks.contains_key(&ReceiptCheck::CheckValue)) { if received_receipt.checks[&ReceiptCheck::CheckValue].is_none() { let signed_receipt = &received_receipt.signed_receipt; - let result = self - .check_value(signed_receipt, received_receipt.query_id) - .await; - - received_receipt - .update_check(&ReceiptCheck::CheckValue, Some(result)) - .unwrap(); + results.push( + self.check_value(signed_receipt, received_receipt.query_id) + .await, + ); } } - Ok(()) + results } async fn check_signature( @@ -277,22 +267,23 @@ impl ReceiptAuditor { Ok(()) } - async fn check_signature_bunch(&self, received_receipts: &mut [ReceivedReceipt]) -> Result<()> { + async fn check_signature_batch( + &self, + received_receipts: &mut [ReceivedReceipt], + ) -> Vec> { + let mut results = Vec::new(); + for received_receipt in received_receipts .iter_mut() - .filter(|r| r.checks.get(&ReceiptCheck::CheckSignature).is_some()) + .filter(|r| r.checks.contains_key(&ReceiptCheck::CheckSignature)) { if received_receipt.checks[&ReceiptCheck::CheckSignature].is_none() { let signed_receipt = &received_receipt.signed_receipt; - let result = self.check_signature(signed_receipt).await; - - received_receipt - .update_check(&ReceiptCheck::CheckSignature, Some(result)) - .unwrap(); + results.push(self.check_signature(signed_receipt).await); } } - Ok(()) + results } async fn check_and_reserve_escrow( @@ -316,22 +307,20 @@ impl ReceiptAuditor { Ok(()) } - async fn check_and_reserve_escrow_bunch( + async fn check_and_reserve_escrow_batch( &self, received_receipts: &mut [ReceivedReceipt], - ) -> Result<()> { + ) -> Vec> { + let mut results = Vec::new(); + for received_receipt in received_receipts.iter_mut().filter(|r| { r.escrow_reserve_attempt_required() && !r.escrow_reserve_attempt_completed() }) { let signed_receipt = &received_receipt.signed_receipt; - let result = self.check_and_reserve_escrow(signed_receipt).await; - - received_receipt - .update_check(&ReceiptCheck::CheckAndReserveEscrow, Some(result)) - .unwrap(); + results.push(self.check_and_reserve_escrow(signed_receipt).await); } - Ok(()) + results } pub async fn check_rav_signature( diff --git a/tap_core/src/tap_receipt/received_receipt.rs b/tap_core/src/tap_receipt/received_receipt.rs index f42bdc7a..0b5510c0 100644 --- a/tap_core/src/tap_receipt/received_receipt.rs +++ b/tap_core/src/tap_receipt/received_receipt.rs @@ -147,6 +147,21 @@ impl ReceivedReceipt { result } + pub async fn perform_check_batch( + batch: &mut [Self], + check: &ReceiptCheck, + receipt_auditor: &ReceiptAuditor, + ) -> Result<()> { + let results = receipt_auditor.check_batch(check, batch).await; + + for (receipt, result) in batch.iter_mut().zip(results) { + receipt.update_check(check, Some(result))?; + receipt.update_state(); + } + + Ok(()) + } + /// Completes a list of *incomplete* check and stores the result, if the check already has a result it is skipped /// /// Returns `Err` only if unable to complete a check, returns `Ok` if the checks were completed (*Important:* this is not the result of the check, just the result of _completing_ the check)