diff --git a/tap_core/src/adapters/mock.rs b/tap_core/src/adapters/mock.rs index df1b69cf..d5900997 100644 --- a/tap_core/src/adapters/mock.rs +++ b/tap_core/src/adapters/mock.rs @@ -1,5 +1,4 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -pub mod auditor_executor_mock; pub mod executor_mock; diff --git a/tap_core/src/adapters/mock/auditor_executor_mock.rs b/tap_core/src/adapters/mock/auditor_executor_mock.rs deleted file mode 100644 index 4acf914d..00000000 --- a/tap_core/src/adapters/mock/auditor_executor_mock.rs +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2023-, Semiotic AI, Inc. -// SPDX-License-Identifier: Apache-2.0 - -use crate::adapters::escrow_adapter::EscrowAdapter; -use alloy_primitives::Address; -use async_trait::async_trait; -use std::{collections::HashMap, sync::Arc}; -use tokio::sync::RwLock; - -use super::executor_mock::AdapterErrorMock; - - -#[derive(Clone)] -pub struct AuditorExecutorMock { - sender_escrow_storage: Arc>>, -} - -impl AuditorExecutorMock { - pub fn new(sender_escrow_storage: Arc>>) -> Self { - AuditorExecutorMock { - sender_escrow_storage, - } - } -} - -impl AuditorExecutorMock { - pub async fn escrow(&self, sender_id: Address) -> Result { - let sender_escrow_storage = self.sender_escrow_storage.read().await; - if let Some(escrow) = sender_escrow_storage.get(&sender_id) { - return Ok(*escrow); - } - Err(AdapterErrorMock::AdapterError { - error: "No escrow exists for provided sender ID.".to_owned(), - }) - } - - pub async fn increase_escrow(&mut self, sender_id: Address, value: u128) { - let mut sender_escrow_storage = self.sender_escrow_storage.write().await; - - if let Some(current_value) = sender_escrow_storage.get(&sender_id) { - let mut sender_escrow_storage = self.sender_escrow_storage.write().await; - sender_escrow_storage.insert(sender_id, current_value + value); - } else { - sender_escrow_storage.insert(sender_id, value); - } - } - - pub async fn reduce_escrow( - &self, - sender_id: Address, - value: u128, - ) -> Result<(), AdapterErrorMock> { - let mut sender_escrow_storage = self.sender_escrow_storage.write().await; - - if let Some(current_value) = sender_escrow_storage.get(&sender_id) { - let checked_new_value = current_value.checked_sub(value); - if let Some(new_value) = checked_new_value { - sender_escrow_storage.insert(sender_id, new_value); - return Ok(()); - } - } - Err(AdapterErrorMock::AdapterError { - error: "Provided value is greater than existing escrow.".to_owned(), - }) - } -} - -#[async_trait] -impl EscrowAdapter for AuditorExecutorMock { - type AdapterError = AdapterErrorMock; - async fn get_available_escrow(&self, sender_id: Address) -> Result { - self.escrow(sender_id).await - } - async fn subtract_escrow( - &self, - sender_id: Address, - value: u128, - ) -> Result<(), Self::AdapterError> { - self.reduce_escrow(sender_id, value).await - } -} diff --git a/tap_core/src/tap_receipt/received_receipt/received_receipt_unit_test.rs b/tap_core/src/tap_receipt/received_receipt/received_receipt_unit_test.rs index 7968b388..786285be 100644 --- a/tap_core/src/tap_receipt/received_receipt/received_receipt_unit_test.rs +++ b/tap_core/src/tap_receipt/received_receipt/received_receipt_unit_test.rs @@ -7,10 +7,7 @@ use rstest::*; use tokio::sync::RwLock; use crate::{ - adapters::{ - auditor_executor_mock::AuditorExecutorMock, - executor_mock::{EscrowStorage, QueryAppraisals}, - }, + adapters::executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, checks::{tests::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, get_current_timestamp_u64_ns, tap_eip712_domain, @@ -84,15 +81,17 @@ fn receipt_storage() -> Arc>> { } #[fixture] -fn auditor_executor() -> (AuditorExecutorMock, EscrowStorage, QueryAppraisals) { +fn executor_mock() -> (ExecutorMock, EscrowStorage, QueryAppraisals) { + let rav_storage = Arc::new(RwLock::new(None)); + let receipt_storage = Arc::new(RwLock::new(HashMap::new())); + let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); + + let executor = ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()); + let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); - ( - AuditorExecutorMock::new(sender_escrow_storage.clone()), - sender_escrow_storage, - query_appraisal_storage, - ) + (executor, sender_escrow_storage, query_appraisal_storage) } #[fixture] @@ -103,12 +102,12 @@ fn domain_separator() -> Eip712Domain { #[fixture] fn checks( domain_separator: Eip712Domain, - auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), + executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), receipt_storage: Arc>>, allocation_ids: Vec
, sender_ids: Vec
, ) -> Vec { - let (_, _escrow_storage, query_appraisal_storage) = auditor_executor; + let (_, _, query_appraisal_storage) = executor_mock; get_full_list_of_checks( domain_separator, sender_ids.iter().cloned().collect(), @@ -151,10 +150,10 @@ async fn partial_then_full_check_valid_receipt( keys: (LocalWallet, Address), domain_separator: Eip712Domain, allocation_ids: Vec
, - auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), + executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), checks: Vec, ) { - let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; + let (executor, escrow_storage, query_appraisal_storage) = executor_mock; // give receipt 5 second variance for min start time let _starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; let _receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); @@ -210,13 +209,13 @@ async fn partial_then_finalize_valid_receipt( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, - auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), + executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), checks: Vec, ) { - let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; + let (executor, escrow_storage, query_appraisal_storage) = executor_mock; // give receipt 5 second variance for min start time let _starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; - let _receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); + let receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); let query_value = 20u128; let signed_receipt = EIP712SignedMessage::new( @@ -263,7 +262,7 @@ async fn partial_then_finalize_valid_receipt( let awaiting_escrow_receipt = awaiting_escrow_receipt.unwrap(); let receipt = awaiting_escrow_receipt - .check_and_reserve_escrow(&_receipt_auditor) + .check_and_reserve_escrow(&receipt_auditor) .await; assert!(receipt.is_ok()); } @@ -274,10 +273,10 @@ async fn standard_lifetime_valid_receipt( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, - auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), + executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), checks: Vec, ) { - let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; + let (executor, escrow_storage, query_appraisal_storage) = executor_mock; // give receipt 5 second variance for min start time let _starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; let _receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor);