Skip to content

Commit

Permalink
refactor: update checks in received receipt for batches
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Asseman <[email protected]>
  • Loading branch information
aasseman committed Oct 27, 2023
1 parent b44358e commit 9bf9064
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 76 deletions.
10 changes: 6 additions & 4 deletions tap_core/src/tap_manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
133 changes: 61 additions & 72 deletions tap_core/src/tap_receipt/receipt_auditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,23 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
}
}

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<ReceiptResult<()>> {
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(
Expand All @@ -106,36 +100,31 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
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<ReceiptResult<()>> {
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<Signature> = HashSet::new();

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(
Expand All @@ -157,25 +146,23 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
Ok(())
}

async fn check_allocation_id_bunch(
async fn check_allocation_id_batch(
&self,
received_receipts: &mut [ReceivedReceipt],
) -> Result<()> {
) -> Vec<ReceiptResult<()>> {
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(
Expand All @@ -192,22 +179,23 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
Ok(())
}

async fn check_timestamp_bunch(&self, received_receipts: &mut [ReceivedReceipt]) -> Result<()> {
async fn check_timestamp_batch(
&self,
received_receipts: &mut [ReceivedReceipt],
) -> Vec<ReceiptResult<()>> {
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(
Expand All @@ -230,24 +218,26 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
Ok(())
}

async fn check_value_bunch(&self, received_receipts: &mut [ReceivedReceipt]) -> Result<()> {
async fn check_value_batch(
&self,
received_receipts: &mut [ReceivedReceipt],
) -> Vec<ReceiptResult<()>> {
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(
Expand Down Expand Up @@ -277,22 +267,23 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
Ok(())
}

async fn check_signature_bunch(&self, received_receipts: &mut [ReceivedReceipt]) -> Result<()> {
async fn check_signature_batch(
&self,
received_receipts: &mut [ReceivedReceipt],
) -> Vec<ReceiptResult<()>> {
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(
Expand All @@ -316,22 +307,20 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
Ok(())
}

async fn check_and_reserve_escrow_bunch(
async fn check_and_reserve_escrow_batch(
&self,
received_receipts: &mut [ReceivedReceipt],
) -> Result<()> {
) -> Vec<ReceiptResult<()>> {
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(
Expand Down
15 changes: 15 additions & 0 deletions tap_core/src/tap_receipt/received_receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ impl ReceivedReceipt {
result
}

pub async fn perform_check_batch<CA: EscrowAdapter, RCA: ReceiptChecksAdapter>(
batch: &mut [Self],
check: &ReceiptCheck,
receipt_auditor: &ReceiptAuditor<CA, RCA>,
) -> 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)
Expand Down

0 comments on commit 9bf9064

Please sign in to comment.