From ec5bad8264a457b9c3b1a3ebfb8b0d3d77021689 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Thu, 21 Dec 2023 11:53:24 -0300 Subject: [PATCH 01/32] refactor: add check trait Signed-off-by: Gustavo Inacio --- tap_core/Cargo.toml | 3 +- tap_core/src/checks/mod.rs | 126 +++++++++++++++++++++++++++++++++++++ tap_core/src/lib.rs | 1 + 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 tap_core/src/checks/mod.rs diff --git a/tap_core/Cargo.toml b/tap_core/Cargo.toml index bed741d2..f7fe58e6 100644 --- a/tap_core/Cargo.toml +++ b/tap_core/Cargo.toml @@ -7,7 +7,7 @@ description = "Core Timeline Aggregation Protocol library: a fast, efficient and [dependencies] rand_core = "0.6.4" -serde = { version = "1.0", features = ["derive"] } +serde = { version = "1.0", features = ["derive", "rc"] } rand = "0.8.5" thiserror = "1.0.38" ethereum-types = { version = "0.14.1" } @@ -24,6 +24,7 @@ strum = "0.24.1" strum_macros = "0.24.3" async-trait = "0.1.72" tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } +typetag = "0.2.14" [dev-dependencies] criterion = { version = "0.5", features = ["async_std"] } diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs new file mode 100644 index 00000000..2dccccfa --- /dev/null +++ b/tap_core/src/checks/mod.rs @@ -0,0 +1,126 @@ +use crate::tap_receipt::{Checking, ReceiptError, ReceiptResult, ReceiptWithState}; +use ethers::types::Signature; +use serde::{Deserialize, Serialize}; +use std::{collections::HashSet, sync::Arc}; +use tokio::sync::RwLock; + +pub type BoxedCheck = Arc; + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub enum CheckingChecks { + Pending(BoxedCheck), + Executed(ReceiptResult<()>), +} + +impl CheckingChecks { + pub fn new(check: BoxedCheck) -> Self { + Self::Pending(check) + } + + pub async fn execute(self, receipt: &ReceiptWithState) -> Self { + match self { + Self::Pending(check) => { + let result = check.check(&receipt).await; + Self::Executed(result) + } + Self::Executed(_) => self, + } + } +} + +#[async_trait::async_trait] +#[typetag::serde(tag = "type")] +pub trait Check: std::fmt::Debug + Send + Sync { + async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()>; +} + +#[async_trait::async_trait] +trait CheckBatch { + async fn check_batch(receipt: &[ReceiptWithState]) -> Vec>; +} + +// #[async_trait::async_trait] +// impl CheckBatch for T +// where +// T: Check, +// { +// async fn check_batch(receipt: &[ReceiptWithState]) -> ReceiptResult<()> { +// todo!() +// } +// } + +#[derive(Debug, Serialize, Deserialize)] +struct UniqueCheck; + +#[async_trait::async_trait] +#[typetag::serde] +impl Check for UniqueCheck { + async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { + println!("UniqueCheck"); + Ok(()) + } +} + +#[async_trait::async_trait] +impl CheckBatch for UniqueCheck { + async fn check_batch(receipts: &[ReceiptWithState]) -> Vec> { + let mut signatures: HashSet = HashSet::new(); + let mut results = Vec::new(); + + for received_receipt in receipts { + let signature = received_receipt.signed_receipt.signature; + if signatures.insert(signature) { + results.push(Ok(())); + } else { + results.push(Err(ReceiptError::NonUniqueReceipt)); + } + } + results + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct TimestampCheck { + #[serde(skip)] + min_timestamp_ns: RwLock, +} + +impl TimestampCheck { + pub fn new(min_timestamp_ns: u64) -> Self { + Self { + min_timestamp_ns: RwLock::new(min_timestamp_ns), + } + } + /// Updates the minimum timestamp that will be accepted for a receipt (exclusive). + pub async fn update_min_timestamp_ns(&self, min_timestamp_ns: u64) { + *self.min_timestamp_ns.write().await = min_timestamp_ns; + } +} + +#[async_trait::async_trait] +#[typetag::serde] +impl Check for TimestampCheck { + async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { + let min_timestamp_ns = *self.min_timestamp_ns.read().await; + let signed_receipt = receipt.signed_receipt(); + if signed_receipt.message.timestamp_ns <= min_timestamp_ns { + return Err(ReceiptError::InvalidTimestamp { + received_timestamp: signed_receipt.message.timestamp_ns, + timestamp_min: min_timestamp_ns, + }); + } + Ok(()) + } +} + +#[derive(Debug, Serialize, Deserialize)] +struct AllocationId; + +#[async_trait::async_trait] +#[typetag::serde] +impl Check for AllocationId { + async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { + println!("AllocationId"); + Ok(()) + } +} diff --git a/tap_core/src/lib.rs b/tap_core/src/lib.rs index 42fcc0bb..3f88c94a 100644 --- a/tap_core/src/lib.rs +++ b/tap_core/src/lib.rs @@ -12,6 +12,7 @@ use alloy_sol_types::eip712_domain; use thiserror::Error; pub mod adapters; +pub mod checks; pub mod eip_712_signed_message; mod error; pub mod receipt_aggregate_voucher; From 0a898010aaebfb71c0d56cb111865317cd971180 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Thu, 21 Dec 2023 11:56:38 -0300 Subject: [PATCH 02/32] refactor: use check trait instead of receipt check enum Signed-off-by: Gustavo Inacio --- tap_core/src/tap_manager/manager.rs | 42 +-- tap_core/src/tap_manager/test/manager_test.rs | 7 +- tap_core/src/tap_receipt/mod.rs | 26 +- tap_core/src/tap_receipt/receipt_auditor.rs | 290 +----------------- tap_core/src/tap_receipt/received_receipt.rs | 118 +++---- 5 files changed, 88 insertions(+), 395 deletions(-) diff --git a/tap_core/src/tap_manager/manager.rs b/tap_core/src/tap_manager/manager.rs index 78ac2412..bdc57f77 100644 --- a/tap_core/src/tap_manager/manager.rs +++ b/tap_core/src/tap_manager/manager.rs @@ -1,6 +1,8 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 +use std::sync::Arc; + use alloy_sol_types::Eip712Domain; use super::{RAVRequest, SignedRAV, SignedReceipt}; @@ -8,9 +10,9 @@ use crate::{ adapters::{ escrow_adapter::EscrowAdapter, rav_storage_adapter::{RAVRead, RAVStore}, - receipt_checks_adapter::ReceiptChecksAdapter, receipt_storage_adapter::{ReceiptRead, ReceiptStore}, }, + checks::{BoxedCheck, TimestampCheck}, receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::{ CategorizedReceiptsWithState, Failed, ReceiptAuditor, ReceiptCheck, ReceiptWithId, @@ -23,10 +25,12 @@ pub struct Manager { /// Executor that implements adapters executor: E, /// Checks that must be completed for each receipt before being confirmed or denied for rav request - required_checks: Vec, + required_checks: Vec, /// Struct responsible for doing checks for receipt. Ownership stays with manager allowing manager /// to update configuration ( like minimum timestamp ). receipt_auditor: ReceiptAuditor, + + timestamp_check: Arc, } impl Manager @@ -43,6 +47,8 @@ where required_checks: Vec, starting_min_timestamp_ns: u64, ) -> Self { + let timestamp_check = Arc::new(TimestampCheck::new(starting_min_timestamp_ns)); + required_checks.push(timestamp_check.clone()); let receipt_auditor = ReceiptAuditor::new( domain_separator, executor.clone(), @@ -52,13 +58,14 @@ where executor, required_checks, receipt_auditor, + timestamp_check, } } } impl Manager where - E: RAVStore + ReceiptChecksAdapter, + E: RAVStore, { /// Verify `signed_rav` matches all values on `expected_rav`, and that `signed_rav` has a valid signer. /// @@ -71,9 +78,10 @@ where expected_rav: ReceiptAggregateVoucher, signed_rav: SignedRAV, ) -> std::result::Result<(), Error> { - self.receipt_auditor - .check_rav_signature(&signed_rav) - .await?; + // TODO + // self.receipt_auditor + // .check_rav_signature(&signed_rav) + // .await?; if signed_rav.message != expected_rav { return Err(Error::InvalidReceivedRAV { @@ -111,7 +119,7 @@ where impl Manager where - E: ReceiptRead + EscrowAdapter + ReceiptChecksAdapter, + E: ReceiptRead + EscrowAdapter, { async fn collect_receipts( &self, @@ -151,11 +159,9 @@ where for received_receipt in checking_receipts { let ReceiptWithId { receipt, - receipt_id, + receipt_id: _, } = received_receipt; - let receipt = receipt - .finalize_receipt_checks(receipt_id, &self.receipt_auditor) - .await; + let receipt = receipt.finalize_receipt_checks().await; match receipt { Ok(checked) => awaiting_reserve_receipts.push(checked), @@ -178,7 +184,7 @@ where impl Manager where - E: ReceiptRead + RAVRead + EscrowAdapter + ReceiptChecksAdapter, + E: ReceiptRead + RAVRead + EscrowAdapter, { /// Completes remaining checks on all receipts up to (current time - `timestamp_buffer_ns`). Returns them in /// two lists (valid receipts and invalid receipts) along with the expected RAV that should be received @@ -207,8 +213,8 @@ where let expected_rav = Self::generate_expected_rav(&valid_receipts, previous_rav.clone())?; - self.receipt_auditor - .update_min_timestamp_ns(expected_rav.timestampNs) + self.timestamp_check + .update_min_timestamp_ns(expected_rav.timestamp_ns) .await; let valid_receipts = valid_receipts .into_iter() @@ -274,7 +280,7 @@ where impl Manager where - E: ReceiptStore + EscrowAdapter + ReceiptChecksAdapter, + E: ReceiptStore + EscrowAdapter, { /// Runs `initial_checks` on `signed_receipt` for initial verification, then stores received receipt. /// The provided `query_id` will be used as a key when chaecking query appraisal. @@ -291,7 +297,7 @@ where &self, signed_receipt: SignedReceipt, query_id: u64, - initial_checks: &[ReceiptCheck], + initial_checks: &[BoxedCheck], ) -> std::result::Result<(), Error> { let mut received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &self.required_checks); @@ -308,9 +314,7 @@ where })?; if let ReceivedReceipt::Checking(received_receipt) = &mut received_receipt { - received_receipt - .perform_checks(initial_checks, receipt_id, &self.receipt_auditor) - .await; + received_receipt.perform_checks(initial_checks).await; } self.executor diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index dc04882c..28ef6363 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -23,6 +23,7 @@ mod manager_unit_test { receipt_checks_adapter_mock::ReceiptChecksAdapterMock, receipt_storage_adapter::ReceiptRead, }, + checks::BoxedCheck, eip_712_signed_message::EIP712SignedMessage, get_current_timestamp_u64_ns, tap_eip712_domain, tap_receipt::{get_full_list_of_checks, Receipt, ReceiptCheck}, @@ -118,15 +119,15 @@ mod manager_unit_test { #[rstest] #[case::full_checks(get_full_list_of_checks())] - #[case::partial_checks(vec![ReceiptCheck::CheckSignature])] - #[case::no_checks(Vec::::new())] + #[case::partial_checks(todo!())] + #[case::no_checks(Vec::::new())] #[tokio::test] async fn manager_verify_and_store_varying_initial_checks( executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, - #[case] initial_checks: Vec, + #[case] initial_checks: Vec, ) { let (executor, escrow_storage, query_appraisal_storage) = executor_mock; // give receipt 5 second variance for min start time diff --git a/tap_core/src/tap_receipt/mod.rs b/tap_core/src/tap_receipt/mod.rs index 8e6912ca..9cb63f1c 100644 --- a/tap_core/src/tap_receipt/mod.rs +++ b/tap_core/src/tap_receipt/mod.rs @@ -18,6 +18,8 @@ use serde::{Deserialize, Serialize}; use strum_macros::{Display, EnumString}; use thiserror::Error; +use crate::checks::{BoxedCheck, CheckingChecks}; + #[derive(Error, Debug, Clone, Serialize, Deserialize)] pub enum ReceiptError { #[error("invalid allocation ID: {received_allocation_id}")] @@ -40,7 +42,7 @@ pub enum ReceiptError { } pub type ReceiptResult = Result; -pub type ReceiptCheckResults = HashMap>>; +pub type ReceiptCheckResults = HashMap<&'static str, CheckingChecks>; #[derive(Hash, Eq, PartialEq, Debug, Clone, EnumString, Display, Serialize, Deserialize)] pub enum ReceiptCheck { CheckUnique, @@ -52,22 +54,22 @@ pub enum ReceiptCheck { pub fn get_full_list_of_receipt_check_results() -> ReceiptCheckResults { let mut all_checks_list = ReceiptCheckResults::new(); - all_checks_list.insert(ReceiptCheck::CheckUnique, None); - all_checks_list.insert(ReceiptCheck::CheckAllocationId, None); - all_checks_list.insert(ReceiptCheck::CheckTimestamp, None); - all_checks_list.insert(ReceiptCheck::CheckValue, None); - all_checks_list.insert(ReceiptCheck::CheckSignature, None); + // all_checks_list.insert(ReceiptCheck::CheckUnique, None); + // all_checks_list.insert(ReceiptCheck::CheckAllocationId, None); + // all_checks_list.insert(ReceiptCheck::CheckTimestamp, None); + // all_checks_list.insert(ReceiptCheck::CheckValue, None); + // all_checks_list.insert(ReceiptCheck::CheckSignature, None); all_checks_list } -pub fn get_full_list_of_checks() -> Vec { +pub fn get_full_list_of_checks() -> Vec { vec![ - ReceiptCheck::CheckUnique, - ReceiptCheck::CheckAllocationId, - ReceiptCheck::CheckTimestamp, - ReceiptCheck::CheckValue, - ReceiptCheck::CheckSignature, + // ReceiptCheck::CheckUnique, + // ReceiptCheck::CheckAllocationId, + // ReceiptCheck::CheckTimestamp, + // ReceiptCheck::CheckValue, + // ReceiptCheck::CheckSignature, ] } diff --git a/tap_core/src/tap_receipt/receipt_auditor.rs b/tap_core/src/tap_receipt/receipt_auditor.rs index 02cbea08..1793f0eb 100644 --- a/tap_core/src/tap_receipt/receipt_auditor.rs +++ b/tap_core/src/tap_receipt/receipt_auditor.rs @@ -1,21 +1,14 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use std::collections::HashSet; - use alloy_sol_types::Eip712Domain; -use ethers::types::Signature; -use tokio::sync::RwLock; use crate::{ - adapters::{escrow_adapter::EscrowAdapter, receipt_checks_adapter::ReceiptChecksAdapter}, - eip_712_signed_message::EIP712SignedMessage, - receipt_aggregate_voucher::ReceiptAggregateVoucher, - tap_receipt::{Receipt, ReceiptCheck, ReceiptError, ReceiptResult}, - Error, Result, + adapters::escrow_adapter::EscrowAdapter, + tap_receipt::{ReceiptError, ReceiptResult}, }; -use super::{received_receipt::Checking, AwaitingReserve, ReceiptWithState}; +use super::{AwaitingReserve, ReceiptWithState}; pub struct ReceiptAuditor { domain_separator: Eip712Domain, @@ -31,285 +24,12 @@ impl ReceiptAuditor { ) -> Self { Self { domain_separator, - executor, - min_timestamp_ns: RwLock::new(starting_min_timestamp_ns), - } - } - - /// Updates the minimum timestamp that will be accepted for a receipt (exclusive). - pub async fn update_min_timestamp_ns(&self, min_timestamp_ns: u64) { - *self.min_timestamp_ns.write().await = min_timestamp_ns; - } - - async fn check_timestamp( - &self, - signed_receipt: &EIP712SignedMessage, - ) -> ReceiptResult<()> { - let min_timestamp_ns = *self.min_timestamp_ns.read().await; - if signed_receipt.message.timestamp_ns <= min_timestamp_ns { - return Err(ReceiptError::InvalidTimestamp { - received_timestamp: signed_receipt.message.timestamp_ns, - timestamp_min: min_timestamp_ns, - }); - } - Ok(()) - } - - async fn check_timestamp_batch( - &self, - received_receipts: &mut [ReceiptWithState], - ) -> Vec> { - let mut results = Vec::new(); - - for received_receipt in received_receipts - .iter_mut() - .filter(|r| r.state.checks.contains_key(&ReceiptCheck::CheckTimestamp)) - { - if received_receipt.state.checks[&ReceiptCheck::CheckTimestamp].is_none() { - let signed_receipt = &received_receipt.signed_receipt; - results.push(self.check_timestamp(signed_receipt).await); - } - } - - results - } - - async fn check_uniqueness_batch( - &self, - received_receipts: &mut [ReceiptWithState], - ) -> 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.state.checks.contains_key(&ReceiptCheck::CheckUnique)) - .any(|r| r.state.checks[&ReceiptCheck::CheckUnique].is_none()) - { - let mut signatures: HashSet = HashSet::new(); - - for received_receipt in received_receipts { - let signature = received_receipt.signed_receipt.signature; - if signatures.insert(signature) { - results.push(Ok(())); - } else { - results.push(Err(ReceiptError::NonUniqueReceipt)); - } - } - } - - results - } -} - -impl ReceiptAuditor -where - E: EscrowAdapter + ReceiptChecksAdapter, -{ - pub async fn check( - &self, - receipt_check: &ReceiptCheck, - signed_receipt: &EIP712SignedMessage, - query_id: u64, - receipt_id: u64, - ) -> ReceiptResult<()> { - match receipt_check { - ReceiptCheck::CheckUnique => self.check_uniqueness(signed_receipt, receipt_id).await, - ReceiptCheck::CheckAllocationId => self.check_allocation_id(signed_receipt).await, - ReceiptCheck::CheckSignature => self.check_signature(signed_receipt).await, - ReceiptCheck::CheckTimestamp => self.check_timestamp(signed_receipt).await, - ReceiptCheck::CheckValue => self.check_value(signed_receipt, query_id).await, - } - } - - pub async fn check_batch( - &self, - receipt_check: &ReceiptCheck, - received_receipts: &mut [ReceiptWithState], - ) -> Vec> { - match receipt_check { - ReceiptCheck::CheckUnique => self.check_uniqueness_batch(received_receipts).await, - ReceiptCheck::CheckAllocationId => { - self.check_allocation_id_batch(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, - } - } -} - -impl ReceiptAuditor -where - E: ReceiptChecksAdapter, -{ - async fn check_uniqueness( - &self, - signed_receipt: &EIP712SignedMessage, - receipt_id: u64, - ) -> ReceiptResult<()> { - if !self - .executor - .is_unique(signed_receipt, receipt_id) - .await - .map_err(|e| ReceiptError::CheckFailedToComplete { - source_error_message: e.to_string(), - })? - { - return Err(ReceiptError::NonUniqueReceipt); - } - Ok(()) - } - - async fn check_allocation_id( - &self, - signed_receipt: &EIP712SignedMessage, - ) -> ReceiptResult<()> { - if !self - .executor - .is_valid_allocation_id(signed_receipt.message.allocation_id) - .await - .map_err(|e| ReceiptError::CheckFailedToComplete { - source_error_message: e.to_string(), - })? - { - return Err(ReceiptError::InvalidAllocationID { - received_allocation_id: signed_receipt.message.allocation_id, - }); - } - Ok(()) - } - - async fn check_allocation_id_batch( - &self, - received_receipts: &mut [ReceiptWithState], - ) -> Vec> { - let mut results = Vec::new(); - - for received_receipt in received_receipts.iter_mut().filter(|r| { - r.state - .checks - .contains_key(&ReceiptCheck::CheckAllocationId) - }) { - if received_receipt.state.checks[&ReceiptCheck::CheckAllocationId].is_none() { - let signed_receipt = &received_receipt.signed_receipt; - results.push(self.check_allocation_id(signed_receipt).await); - } - } - - results - } - - async fn check_value( - &self, - signed_receipt: &EIP712SignedMessage, - query_id: u64, - ) -> ReceiptResult<()> { - if !self - .executor - .is_valid_value(signed_receipt.message.value, query_id) - .await - .map_err(|e| ReceiptError::CheckFailedToComplete { - source_error_message: e.to_string(), - })? - { - return Err(ReceiptError::InvalidValue { - received_value: signed_receipt.message.value, - }); - } - Ok(()) - } - - async fn check_value_batch( - &self, - received_receipts: &mut [ReceiptWithState], - ) -> Vec> { - let mut results = Vec::new(); - - for received_receipt in received_receipts - .iter_mut() - .filter(|r| r.state.checks.contains_key(&ReceiptCheck::CheckValue)) - { - if received_receipt.state.checks[&ReceiptCheck::CheckValue].is_none() { - let signed_receipt = &received_receipt.signed_receipt; - results.push( - self.check_value(signed_receipt, received_receipt.query_id) - .await, - ); - } - } - - results - } - - async fn check_signature( - &self, - signed_receipt: &EIP712SignedMessage, - ) -> ReceiptResult<()> { - let receipt_signer_address = signed_receipt - .recover_signer(&self.domain_separator) - .map_err(|err| ReceiptError::InvalidSignature { - source_error_message: err.to_string(), - })?; - if !self - .executor - .is_valid_sender_id(receipt_signer_address) - .await - .map_err(|e| ReceiptError::CheckFailedToComplete { - source_error_message: e.to_string(), - })? - { - return Err(ReceiptError::InvalidSignature { - source_error_message: format!( - "Recovered sender id is not valid: {}", - receipt_signer_address - ), - }); + escrow_adapter, } - Ok(()) - } - - async fn check_signature_batch( - &self, - received_receipts: &mut [ReceiptWithState], - ) -> Vec> { - let mut results = Vec::new(); - - for received_receipt in received_receipts - .iter_mut() - .filter(|r| r.state.checks.contains_key(&ReceiptCheck::CheckSignature)) - { - if received_receipt.state.checks[&ReceiptCheck::CheckSignature].is_none() { - let signed_receipt = &received_receipt.signed_receipt; - results.push(self.check_signature(signed_receipt).await); - } - } - - results - } - - pub async fn check_rav_signature( - &self, - signed_rav: &EIP712SignedMessage, - ) -> Result<()> { - let rav_signer_address = signed_rav.recover_signer(&self.domain_separator)?; - if !self - .executor - .is_valid_sender_id(rav_signer_address) - .await - .map_err(|err| Error::AdapterError { - source_error: anyhow::Error::new(err), - })? - { - return Err(Error::InvalidRecoveredSigner { - address: rav_signer_address, - }); - } - Ok(()) } } -impl ReceiptAuditor +impl ReceiptAuditor where E: EscrowAdapter, { diff --git a/tap_core/src/tap_receipt/received_receipt.rs b/tap_core/src/tap_receipt/received_receipt.rs index d008ef55..fd6763ba 100644 --- a/tap_core/src/tap_receipt/received_receipt.rs +++ b/tap_core/src/tap_receipt/received_receipt.rs @@ -17,17 +17,16 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; -use super::{receipt_auditor::ReceiptAuditor, Receipt, ReceiptCheck, ReceiptCheckResults}; +use super::{receipt_auditor::ReceiptAuditor, Receipt, ReceiptCheckResults}; use crate::{ - adapters::{ - escrow_adapter::EscrowAdapter, receipt_checks_adapter::ReceiptChecksAdapter, - receipt_storage_adapter::StoredReceipt, - }, + adapters::{escrow_adapter::EscrowAdapter, receipt_storage_adapter::StoredReceipt}, + checks::{BoxedCheck, Check, CheckingChecks}, eip_712_signed_message::EIP712SignedMessage, Error, Result, }; #[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(bound(deserialize = "'de: 'static"))] pub struct Checking { /// A list of checks to be completed for the receipt, along with their current result pub(crate) checks: ReceiptCheckResults, @@ -173,7 +172,7 @@ impl ReceivedReceipt { pub fn new( signed_receipt: EIP712SignedMessage, query_id: u64, - required_checks: &[ReceiptCheck], + required_checks: &[BoxedCheck], ) -> Self { let checks = ReceiptWithState::get_empty_required_checks_hashmap(required_checks); @@ -243,45 +242,18 @@ impl ReceiptWithState { /// /// Returns [`Error::InvalidCheckError] if requested error in not a required check (list of required checks provided by user on construction) /// - pub async fn perform_check( + pub async fn perform_check( &mut self, - check: &ReceiptCheck, - receipt_id: u64, - receipt_auditor: &ReceiptAuditor, - ) where - A: EscrowAdapter + ReceiptChecksAdapter, - { + check: &BoxedCheck, + ) { // Only perform check if it is incomplete // Don't check if already failed if !self.check_is_complete(check) && !self.any_check_resulted_in_error() { - let _ = self.update_check( - check, - Some( - receipt_auditor - .check(check, &self.signed_receipt, self.query_id, receipt_id) - .await, - ), - ); + let execute_check = self.state.checks.remove(check.typetag_name()).unwrap(); + let _ = self.update_check(check, execute_check.execute(&self).await); } } - pub async fn perform_check_batch( - batch: &mut [Self], - check: &ReceiptCheck, - receipt_auditor: &ReceiptAuditor, - ) -> Result<()> - where - A: EscrowAdapter + ReceiptChecksAdapter, - { - let results = receipt_auditor.check_batch(check, batch).await; - - for (receipt, result) in batch.iter_mut().zip(results) { - receipt.update_check(check, Some(result))?; - } - - 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) @@ -292,16 +264,12 @@ impl ReceiptWithState { /// /// Returns [`Error::InvalidCheckError] if requested error in not a required check (list of required checks provided by user on construction) /// - pub async fn perform_checks( + pub async fn perform_checks( &mut self, - checks: &[ReceiptCheck], - receipt_id: u64, - receipt_auditor: &ReceiptAuditor, - ) where - A: EscrowAdapter + ReceiptChecksAdapter, - { + checks: &[BoxedCheck], + ) { for check in checks { - self.perform_check(check, receipt_id, receipt_auditor).await; + self.perform_check(check).await; } } @@ -309,17 +277,12 @@ impl ReceiptWithState { /// /// Returns `Err` only if unable to complete a check, returns `Ok` if no check failed to complete (*Important:* this is not the result of the check, just the result of _completing_ the check) /// - pub async fn finalize_receipt_checks( + pub async fn finalize_receipt_checks( mut self, - receipt_id: u64, - receipt_auditor: &ReceiptAuditor, - ) -> ResultReceipt - where - A: EscrowAdapter + ReceiptChecksAdapter, - { + ) -> ResultReceipt { let incomplete_checks = self.incomplete_checks(); - self.perform_checks(incomplete_checks.as_slice(), receipt_id, receipt_auditor) + self.perform_checks(incomplete_checks.as_slice()) .await; if self.any_check_resulted_in_error() { @@ -337,9 +300,9 @@ impl ReceiptWithState { .checks .iter() .filter_map(|(check, result)| { - if let Some(unwrapped_result) = result { + if let CheckingChecks::Executed(unwrapped_result) = result { if unwrapped_result.is_err() { - return Some(((*check).clone(), Some((*unwrapped_result).clone()))); + return Some(((*check).clone(), result.clone())); } } None @@ -348,17 +311,14 @@ impl ReceiptWithState { } /// Returns all checks that have not been completed - pub fn incomplete_checks(&self) -> Vec { - let incomplete_checks: Vec = self + pub fn incomplete_checks(&self) -> Vec { + let incomplete_checks = self .state .checks .iter() - .filter_map(|(check, result)| { - if result.is_none() { - Some((*check).clone()) - } else { - None - } + .filter_map(|(check, result)| match result { + CheckingChecks::Pending(check) => Some(check.clone()), + CheckingChecks::Executed(_) => None, }) .collect(); incomplete_checks @@ -366,39 +326,45 @@ impl ReceiptWithState { pub(crate) fn update_check( &mut self, - check: &ReceiptCheck, - result: Option>, + check: &BoxedCheck, + result: CheckingChecks, ) -> Result<()> { - if !self.state.checks.contains_key(check) { + if !self.state.checks.contains_key(check.typetag_name()) { return Err(Error::InvalidCheckError { - check_string: check.to_string(), + check_string: check.typetag_name().to_string(), }); } - self.state.checks.insert(check.clone(), result); + self.state.checks.insert(check.typetag_name(), result); Ok(()) } /// returns true `check` has a result, otherwise false - pub(crate) fn check_is_complete(&self, check: &ReceiptCheck) -> bool { - matches!(self.state.checks.get(check), Some(Some(_))) + pub(crate) fn check_is_complete(&self, check: &BoxedCheck) -> bool { + matches!( + self.state.checks.get(check.typetag_name()), + Some(CheckingChecks::Executed(_)) + ) } fn any_check_resulted_in_error(&self) -> bool { self.state.checks.iter().any(|(_, status)| match &status { - Some(result) => result.is_err(), - None => false, + CheckingChecks::Executed(result) => result.is_err(), + _ => false, }) } pub fn checking_is_complete(&self) -> bool { - self.state.checks.iter().all(|(_, status)| status.is_some()) + self.state + .checks + .iter() + .all(|(_, status)| matches!(status, CheckingChecks::Executed(_))) } - fn get_empty_required_checks_hashmap(required_checks: &[ReceiptCheck]) -> ReceiptCheckResults { + fn get_empty_required_checks_hashmap(required_checks: &[BoxedCheck]) -> ReceiptCheckResults { required_checks .iter() - .map(|check| (check.clone(), None)) + .map(|check| (check.typetag_name(), CheckingChecks::Pending(check.clone()))) .collect() } } From 1266c4e2ca6b52cc372cfa01aef09d678a2491e6 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Thu, 21 Dec 2023 11:57:11 -0300 Subject: [PATCH 03/32] refactor: remove receipt checks adapter Signed-off-by: Gustavo Inacio --- .../mock/receipt_checks_adapter_mock.rs | 48 +------------ tap_core/src/adapters/mod.rs | 1 - .../src/adapters/receipt_checks_adapter.rs | 69 ------------------- .../test/receipt_checks_adapter_test.rs | 5 +- 4 files changed, 2 insertions(+), 121 deletions(-) delete mode 100644 tap_core/src/adapters/receipt_checks_adapter.rs diff --git a/tap_core/src/adapters/mock/receipt_checks_adapter_mock.rs b/tap_core/src/adapters/mock/receipt_checks_adapter_mock.rs index 3dd0f4d7..29e07c23 100644 --- a/tap_core/src/adapters/mock/receipt_checks_adapter_mock.rs +++ b/tap_core/src/adapters/mock/receipt_checks_adapter_mock.rs @@ -11,11 +11,7 @@ use async_trait::async_trait; use thiserror::Error; use tokio::sync::RwLock; -use crate::{ - adapters::receipt_checks_adapter::ReceiptChecksAdapter, - eip_712_signed_message::EIP712SignedMessage, - tap_receipt::{Receipt, ReceiptError, ReceivedReceipt}, -}; +use crate::tap_receipt::{ReceiptError, ReceivedReceipt}; pub struct ReceiptChecksAdapterMock { receipt_storage: Arc>>, @@ -53,45 +49,3 @@ impl ReceiptChecksAdapterMock { } } } - -#[async_trait] -impl ReceiptChecksAdapter for ReceiptChecksAdapterMock { - type AdapterError = AdapterErrorMock; - - async fn is_unique( - &self, - receipt: &EIP712SignedMessage, - receipt_id: u64, - ) -> Result { - let receipt_storage = self.receipt_storage.read().await; - Ok(receipt_storage - .iter() - .all(|(stored_receipt_id, stored_receipt)| { - (stored_receipt.signed_receipt().message != receipt.message) - || *stored_receipt_id == receipt_id - })) - } - - async fn is_valid_allocation_id( - &self, - allocation_id: Address, - ) -> Result { - let allocation_ids = self.allocation_ids.read().await; - Ok(allocation_ids.contains(&allocation_id)) - } - - async fn is_valid_value(&self, value: u128, query_id: u64) -> Result { - let query_appraisals = self.query_appraisals.read().await; - let appraised_value = query_appraisals.get(&query_id).unwrap(); - - if value != *appraised_value { - return Ok(false); - } - Ok(true) - } - - async fn is_valid_sender_id(&self, sender_id: Address) -> Result { - let sender_ids = self.sender_ids.read().await; - Ok(sender_ids.contains(&sender_id)) - } -} diff --git a/tap_core/src/adapters/mod.rs b/tap_core/src/adapters/mod.rs index 96ab5cea..6b045e4e 100644 --- a/tap_core/src/adapters/mod.rs +++ b/tap_core/src/adapters/mod.rs @@ -17,7 +17,6 @@ pub mod escrow_adapter; pub mod rav_storage_adapter; -pub mod receipt_checks_adapter; pub mod receipt_storage_adapter; #[cfg(feature = "mock")] diff --git a/tap_core/src/adapters/receipt_checks_adapter.rs b/tap_core/src/adapters/receipt_checks_adapter.rs deleted file mode 100644 index ed4dbd7f..00000000 --- a/tap_core/src/adapters/receipt_checks_adapter.rs +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2023-, Semiotic AI, Inc. -// SPDX-License-Identifier: Apache-2.0 - -use crate::{eip_712_signed_message::EIP712SignedMessage, tap_receipt::Receipt}; -use alloy_primitives::Address; -use async_trait::async_trait; - -/// `ReceiptChecksAdapter` defines a trait for adapters to handle checks related to TAP receipts. -/// -/// This trait is designed to be implemented by users of this library who want to -/// customize the checks done on TAP receipts. This includes ensuring the receipt is unique, -/// verifying the allocation ID, the value and the sender ID. -/// -/// # Usage -/// -/// The `is_unique` method should be used to check if the given receipt is unique in the system. -/// -/// The `is_valid_allocation_id` method should verify if the allocation ID is valid. -/// -/// The `is_valid_value` method should confirm the value of the receipt is valid for the given query ID. -/// -/// The `is_valid_sender_id` method should confirm the sender ID is valid. -/// -/// This trait is utilized by [crate::tap_manager], which relies on these -/// operations for managing TAP receipts. -/// -/// # Example -/// -/// For example code see [crate::adapters::receipt_checks_adapter_mock] - -#[async_trait] -pub trait ReceiptChecksAdapter { - /// Defines the user-specified error type. - /// - /// This error type should implement the `Error` and `Debug` traits from the standard library. - /// Errors of this type are returned to the user when an operation fails. - type AdapterError: std::error::Error + std::fmt::Debug + Send + Sync + 'static; - - /// Checks if the given receipt is unique in the system. - /// - /// This method should be implemented to verify the uniqueness of a given receipt in your system. Keep in mind that - /// the receipt likely will be in storage when this check is performed so the receipt id should be used to check - /// for uniqueness. - async fn is_unique( - &self, - receipt: &EIP712SignedMessage, - receipt_id: u64, - ) -> Result; - - /// Verifies if the allocation ID is valid. - /// - /// This method should be implemented to validate the given allocation ID is a valid allocation for the indexer. Valid is defined as - /// an allocation ID that is owned by the indexer and still available for redeeming. - async fn is_valid_allocation_id( - &self, - allocation_id: Address, - ) -> Result; - - /// Confirms the value of the receipt is valid for the given query ID. - /// - /// This method should be implemented to confirm the validity of the given value for a specific query ID. - async fn is_valid_value(&self, value: u128, query_id: u64) -> Result; - - /// Confirms the sender ID is valid. - /// - /// This method should be implemented to validate the given sender ID is one associated with a sender the indexer considers valid. - /// The provided sender ID is the address of the sender that is recovered from the signature of the receipt. - async fn is_valid_sender_id(&self, sender_id: Address) -> Result; -} diff --git a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs index 0d25f771..977a6b6b 100644 --- a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs @@ -17,10 +17,7 @@ mod receipt_checks_adapter_unit_test { use tokio::sync::RwLock; use crate::{ - adapters::{ - receipt_checks_adapter::ReceiptChecksAdapter, - receipt_checks_adapter_mock::ReceiptChecksAdapterMock, - }, + adapters::receipt_checks_adapter_mock::ReceiptChecksAdapterMock, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, tap_receipt::{get_full_list_of_checks, Receipt, ReceivedReceipt}, From b778f841e2bd5d9eb45b5ea0eddafbc28d1cc1a0 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 15 Jan 2024 19:52:56 -0300 Subject: [PATCH 04/32] refactor: fix rebase conflicts Signed-off-by: Gustavo Inacio --- tap_core/src/adapters/mock.rs | 1 - .../adapters/mock/auditor_executor_mock.rs | 50 ++---------------- tap_core/src/adapters/mock/executor_mock.rs | 50 ++---------------- .../mock/receipt_checks_adapter_mock.rs | 51 ------------------- tap_core/src/checks/mod.rs | 4 +- tap_core/src/tap_manager/manager.rs | 12 ++--- tap_core/src/tap_manager/rav_request.rs | 2 +- tap_core/src/tap_receipt/mod.rs | 2 +- tap_core/src/tap_receipt/receipt_auditor.rs | 6 +-- tap_core/src/tap_receipt/received_receipt.rs | 28 ++++------ .../tests/received_receipt_tests.rs | 10 ++-- 11 files changed, 32 insertions(+), 184 deletions(-) delete mode 100644 tap_core/src/adapters/mock/receipt_checks_adapter_mock.rs diff --git a/tap_core/src/adapters/mock.rs b/tap_core/src/adapters/mock.rs index 85c63db2..1c4056de 100644 --- a/tap_core/src/adapters/mock.rs +++ b/tap_core/src/adapters/mock.rs @@ -5,5 +5,4 @@ pub mod auditor_executor_mock; pub mod escrow_adapter_mock; pub mod executor_mock; pub mod rav_storage_adapter_mock; -pub mod receipt_checks_adapter_mock; pub mod receipt_storage_adapter_mock; diff --git a/tap_core/src/adapters/mock/auditor_executor_mock.rs b/tap_core/src/adapters/mock/auditor_executor_mock.rs index 49fa8eff..873918ec 100644 --- a/tap_core/src/adapters/mock/auditor_executor_mock.rs +++ b/tap_core/src/adapters/mock/auditor_executor_mock.rs @@ -1,11 +1,10 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use super::{escrow_adapter_mock::AdpaterErrorMock, receipt_checks_adapter_mock::AdapterErrorMock}; +use super::escrow_adapter_mock::AdpaterErrorMock; use crate::adapters::escrow_adapter::EscrowAdapter; -use crate::adapters::receipt_checks_adapter::ReceiptChecksAdapter; -use crate::eip_712_signed_message::EIP712SignedMessage; -use crate::tap_receipt::{Receipt, ReceivedReceipt}; +// use crate::adapters::receipt_checks_adapter::ReceiptChecksAdapter; +use crate::tap_receipt::ReceivedReceipt; use alloy_primitives::Address; use async_trait::async_trait; use std::{ @@ -15,6 +14,7 @@ use std::{ use tokio::sync::RwLock; #[derive(Clone)] +#[allow(dead_code)] pub struct AuditorExecutorMock { receipt_storage: Arc>>, @@ -99,45 +99,3 @@ impl EscrowAdapter for AuditorExecutorMock { self.reduce_escrow(sender_id, value).await } } - -#[async_trait] -impl ReceiptChecksAdapter for AuditorExecutorMock { - type AdapterError = AdapterErrorMock; - - async fn is_unique( - &self, - receipt: &EIP712SignedMessage, - receipt_id: u64, - ) -> Result { - let receipt_storage = self.receipt_storage.read().await; - Ok(receipt_storage - .iter() - .all(|(stored_receipt_id, stored_receipt)| { - (stored_receipt.signed_receipt().message != receipt.message) - || *stored_receipt_id == receipt_id - })) - } - - async fn is_valid_allocation_id( - &self, - allocation_id: Address, - ) -> Result { - let allocation_ids = self.allocation_ids.read().await; - Ok(allocation_ids.contains(&allocation_id)) - } - - async fn is_valid_value(&self, value: u128, query_id: u64) -> Result { - let query_appraisals = self.query_appraisals.read().await; - let appraised_value = query_appraisals.get(&query_id).unwrap(); - - if value != *appraised_value { - return Ok(false); - } - Ok(true) - } - - async fn is_valid_sender_id(&self, sender_id: Address) -> Result { - let sender_ids = self.sender_ids.read().await; - Ok(sender_ids.contains(&sender_id)) - } -} diff --git a/tap_core/src/adapters/mock/executor_mock.rs b/tap_core/src/adapters/mock/executor_mock.rs index 76898659..33eefde7 100644 --- a/tap_core/src/adapters/mock/executor_mock.rs +++ b/tap_core/src/adapters/mock/executor_mock.rs @@ -1,14 +1,13 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use super::{escrow_adapter_mock::AdpaterErrorMock, receipt_checks_adapter_mock::AdapterErrorMock}; +use super::escrow_adapter_mock::AdpaterErrorMock; +use super::receipt_storage_adapter_mock::AdapterErrorMock; use crate::adapters::escrow_adapter::EscrowAdapter; -use crate::adapters::receipt_checks_adapter::ReceiptChecksAdapter; use crate::adapters::receipt_storage_adapter::{ safe_truncate_receipts, ReceiptRead, ReceiptStore, StoredReceipt, }; -use crate::eip_712_signed_message::EIP712SignedMessage; -use crate::tap_receipt::{Receipt, ReceivedReceipt}; +use crate::tap_receipt::ReceivedReceipt; use crate::{ adapters::rav_storage_adapter::{RAVRead, RAVStore}, tap_manager::SignedRAV, @@ -26,6 +25,7 @@ pub type EscrowStorage = Arc>>; pub type QueryAppraisals = Arc>>; #[derive(Clone)] +#[allow(dead_code)] pub struct ExecutorMock { /// local RAV store with rwlocks to allow sharing with other compenents as needed rav_storage: Arc>>, @@ -200,45 +200,3 @@ impl EscrowAdapter for ExecutorMock { self.reduce_escrow(sender_id, value).await } } - -#[async_trait] -impl ReceiptChecksAdapter for ExecutorMock { - type AdapterError = AdapterErrorMock; - - async fn is_unique( - &self, - receipt: &EIP712SignedMessage, - receipt_id: u64, - ) -> Result { - let receipt_storage = self.receipt_storage.read().await; - Ok(receipt_storage - .iter() - .all(|(stored_receipt_id, stored_receipt)| { - (stored_receipt.signed_receipt().message != receipt.message) - || *stored_receipt_id == receipt_id - })) - } - - async fn is_valid_allocation_id( - &self, - allocation_id: Address, - ) -> Result { - let allocation_ids = self.allocation_ids.read().await; - Ok(allocation_ids.contains(&allocation_id)) - } - - async fn is_valid_value(&self, value: u128, query_id: u64) -> Result { - let query_appraisals = self.query_appraisals.read().await; - let appraised_value = query_appraisals.get(&query_id).unwrap(); - - if value != *appraised_value { - return Ok(false); - } - Ok(true) - } - - async fn is_valid_sender_id(&self, sender_id: Address) -> Result { - let sender_ids = self.sender_ids.read().await; - Ok(sender_ids.contains(&sender_id)) - } -} diff --git a/tap_core/src/adapters/mock/receipt_checks_adapter_mock.rs b/tap_core/src/adapters/mock/receipt_checks_adapter_mock.rs deleted file mode 100644 index 29e07c23..00000000 --- a/tap_core/src/adapters/mock/receipt_checks_adapter_mock.rs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2023-, Semiotic AI, Inc. -// SPDX-License-Identifier: Apache-2.0 - -use std::{ - collections::{HashMap, HashSet}, - sync::Arc, -}; - -use alloy_primitives::Address; -use async_trait::async_trait; -use thiserror::Error; -use tokio::sync::RwLock; - -use crate::tap_receipt::{ReceiptError, ReceivedReceipt}; - -pub struct ReceiptChecksAdapterMock { - receipt_storage: Arc>>, - query_appraisals: Arc>>, - allocation_ids: Arc>>, - sender_ids: Arc>>, -} - -#[derive(Debug, Error)] -pub enum AdapterErrorMock { - #[error("something went wrong: {error}")] - AdapterError { error: String }, -} - -impl From for ReceiptError { - fn from(val: AdapterErrorMock) -> Self { - ReceiptError::CheckFailedToComplete { - source_error_message: val.to_string(), - } - } -} - -impl ReceiptChecksAdapterMock { - pub fn new( - receipt_storage: Arc>>, - query_appraisals: Arc>>, - allocation_ids: Arc>>, - sender_ids: Arc>>, - ) -> Self { - Self { - receipt_storage, - query_appraisals, - allocation_ids, - sender_ids, - } - } -} diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs index 2dccccfa..4879ffa3 100644 --- a/tap_core/src/checks/mod.rs +++ b/tap_core/src/checks/mod.rs @@ -55,7 +55,7 @@ struct UniqueCheck; #[async_trait::async_trait] #[typetag::serde] impl Check for UniqueCheck { - async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { + async fn check(&self, _receipt: &ReceiptWithState) -> ReceiptResult<()> { println!("UniqueCheck"); Ok(()) } @@ -119,7 +119,7 @@ struct AllocationId; #[async_trait::async_trait] #[typetag::serde] impl Check for AllocationId { - async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { + async fn check(&self, _receipt: &ReceiptWithState) -> ReceiptResult<()> { println!("AllocationId"); Ok(()) } diff --git a/tap_core/src/tap_manager/manager.rs b/tap_core/src/tap_manager/manager.rs index bdc57f77..61f72f8f 100644 --- a/tap_core/src/tap_manager/manager.rs +++ b/tap_core/src/tap_manager/manager.rs @@ -15,8 +15,8 @@ use crate::{ checks::{BoxedCheck, TimestampCheck}, receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::{ - CategorizedReceiptsWithState, Failed, ReceiptAuditor, ReceiptCheck, ReceiptWithId, - ReceiptWithState, ReceivedReceipt, Reserved, + CategorizedReceiptsWithState, Failed, ReceiptAuditor, ReceiptWithId, ReceiptWithState, + ReceivedReceipt, Reserved, }, Error, }; @@ -44,16 +44,12 @@ where pub fn new( domain_separator: Eip712Domain, executor: E, - required_checks: Vec, + mut required_checks: Vec, starting_min_timestamp_ns: u64, ) -> Self { let timestamp_check = Arc::new(TimestampCheck::new(starting_min_timestamp_ns)); required_checks.push(timestamp_check.clone()); - let receipt_auditor = ReceiptAuditor::new( - domain_separator, - executor.clone(), - starting_min_timestamp_ns, - ); + let receipt_auditor = ReceiptAuditor::new(domain_separator, executor.clone()); Self { executor, required_checks, diff --git a/tap_core/src/tap_manager/rav_request.rs b/tap_core/src/tap_manager/rav_request.rs index bcf395e4..4b59b5f8 100644 --- a/tap_core/src/tap_manager/rav_request.rs +++ b/tap_core/src/tap_manager/rav_request.rs @@ -10,7 +10,7 @@ use crate::{ }; #[derive(Debug, Serialize, Deserialize, Clone)] - +#[serde(bound(deserialize = "'de: 'static"))] pub struct RAVRequest { pub valid_receipts: Vec, pub previous_rav: Option, diff --git a/tap_core/src/tap_receipt/mod.rs b/tap_core/src/tap_receipt/mod.rs index 9cb63f1c..dc625fa0 100644 --- a/tap_core/src/tap_receipt/mod.rs +++ b/tap_core/src/tap_receipt/mod.rs @@ -53,7 +53,7 @@ pub enum ReceiptCheck { } pub fn get_full_list_of_receipt_check_results() -> ReceiptCheckResults { - let mut all_checks_list = ReceiptCheckResults::new(); + let all_checks_list = ReceiptCheckResults::new(); // all_checks_list.insert(ReceiptCheck::CheckUnique, None); // all_checks_list.insert(ReceiptCheck::CheckAllocationId, None); // all_checks_list.insert(ReceiptCheck::CheckTimestamp, None); diff --git a/tap_core/src/tap_receipt/receipt_auditor.rs b/tap_core/src/tap_receipt/receipt_auditor.rs index 1793f0eb..dd2616c0 100644 --- a/tap_core/src/tap_receipt/receipt_auditor.rs +++ b/tap_core/src/tap_receipt/receipt_auditor.rs @@ -13,23 +13,21 @@ use super::{AwaitingReserve, ReceiptWithState}; pub struct ReceiptAuditor { domain_separator: Eip712Domain, executor: E, - min_timestamp_ns: RwLock, } impl ReceiptAuditor { pub fn new( domain_separator: Eip712Domain, executor: E, - starting_min_timestamp_ns: u64, ) -> Self { Self { domain_separator, - escrow_adapter, + executor, } } } -impl ReceiptAuditor +impl ReceiptAuditor where E: EscrowAdapter, { diff --git a/tap_core/src/tap_receipt/received_receipt.rs b/tap_core/src/tap_receipt/received_receipt.rs index fd6763ba..a716208a 100644 --- a/tap_core/src/tap_receipt/received_receipt.rs +++ b/tap_core/src/tap_receipt/received_receipt.rs @@ -20,7 +20,7 @@ use serde::{Deserialize, Serialize}; use super::{receipt_auditor::ReceiptAuditor, Receipt, ReceiptCheckResults}; use crate::{ adapters::{escrow_adapter::EscrowAdapter, receipt_storage_adapter::StoredReceipt}, - checks::{BoxedCheck, Check, CheckingChecks}, + checks::{BoxedCheck, CheckingChecks}, eip_712_signed_message::EIP712SignedMessage, Error, Result, }; @@ -31,7 +31,9 @@ pub struct Checking { /// A list of checks to be completed for the receipt, along with their current result pub(crate) checks: ReceiptCheckResults, } + #[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(bound(deserialize = "'de: 'static"))] pub struct Failed { /// A list of checks to be completed for the receipt, along with their current result pub(crate) checks: ReceiptCheckResults, @@ -79,6 +81,7 @@ pub struct ReceiptWithId where T: ReceiptState, { + #[allow(dead_code)] pub(crate) receipt_id: u64, pub(crate) receipt: ReceiptWithState, } @@ -222,7 +225,7 @@ impl ReceiptWithState { auditor: &ReceiptAuditor, ) -> ResultReceipt where - A: EscrowAdapter + ReceiptChecksAdapter, + A: EscrowAdapter, { match auditor.check_and_reserve_escrow(&self).await { Ok(_) => Ok(self.perform_state_changes(Reserved)), @@ -242,10 +245,7 @@ impl ReceiptWithState { /// /// Returns [`Error::InvalidCheckError] if requested error in not a required check (list of required checks provided by user on construction) /// - pub async fn perform_check( - &mut self, - check: &BoxedCheck, - ) { + pub async fn perform_check(&mut self, check: &BoxedCheck) { // Only perform check if it is incomplete // Don't check if already failed if !self.check_is_complete(check) && !self.any_check_resulted_in_error() { @@ -264,10 +264,7 @@ impl ReceiptWithState { /// /// Returns [`Error::InvalidCheckError] if requested error in not a required check (list of required checks provided by user on construction) /// - pub async fn perform_checks( - &mut self, - checks: &[BoxedCheck], - ) { + pub async fn perform_checks(&mut self, checks: &[BoxedCheck]) { for check in checks { self.perform_check(check).await; } @@ -277,13 +274,10 @@ impl ReceiptWithState { /// /// Returns `Err` only if unable to complete a check, returns `Ok` if no check failed to complete (*Important:* this is not the result of the check, just the result of _completing_ the check) /// - pub async fn finalize_receipt_checks( - mut self, - ) -> ResultReceipt { + pub async fn finalize_receipt_checks(mut self) -> ResultReceipt { let incomplete_checks = self.incomplete_checks(); - self.perform_checks(incomplete_checks.as_slice()) - .await; + self.perform_checks(incomplete_checks.as_slice()).await; if self.any_check_resulted_in_error() { let failed = self.perform_state_changes_into(); @@ -302,7 +296,7 @@ impl ReceiptWithState { .filter_map(|(check, result)| { if let CheckingChecks::Executed(unwrapped_result) = result { if unwrapped_result.is_err() { - return Some(((*check).clone(), result.clone())); + return Some((*check, result.clone())); } } None @@ -316,7 +310,7 @@ impl ReceiptWithState { .state .checks .iter() - .filter_map(|(check, result)| match result { + .filter_map(|(_check, result)| match result { CheckingChecks::Pending(check) => Some(check.clone()), CheckingChecks::Executed(_) => None, }) diff --git a/tap_core/src/tap_receipt/tests/received_receipt_tests.rs b/tap_core/src/tap_receipt/tests/received_receipt_tests.rs index 64a52029..49d6718d 100644 --- a/tap_core/src/tap_receipt/tests/received_receipt_tests.rs +++ b/tap_core/src/tap_receipt/tests/received_receipt_tests.rs @@ -163,8 +163,7 @@ mod received_receipt_unit_test { let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; // 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, starting_min_timestamp); + let receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); let query_value = 20u128; let signed_receipt = EIP712SignedMessage::new( @@ -223,9 +222,7 @@ mod received_receipt_unit_test { let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; // 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, starting_min_timestamp); - + let receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); let query_value = 20u128; let signed_receipt = EIP712SignedMessage::new( &domain_separator, @@ -284,8 +281,7 @@ mod received_receipt_unit_test { let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; // 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, starting_min_timestamp); + let receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); let query_value = 20u128; let signed_receipt = EIP712SignedMessage::new( From 5da3e9c80ae9f1ba088b0ee04e4a55a67864cd02 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 15 Jan 2024 20:05:36 -0300 Subject: [PATCH 05/32] refactor: use check batch inside check trait Signed-off-by: Gustavo Inacio --- tap_core/src/checks/mod.rs | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs index 4879ffa3..a62bd237 100644 --- a/tap_core/src/checks/mod.rs +++ b/tap_core/src/checks/mod.rs @@ -32,23 +32,17 @@ impl CheckingChecks { #[typetag::serde(tag = "type")] pub trait Check: std::fmt::Debug + Send + Sync { async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()>; -} -#[async_trait::async_trait] -trait CheckBatch { - async fn check_batch(receipt: &[ReceiptWithState]) -> Vec>; + async fn check_batch(&self, receipts: &[ReceiptWithState]) -> Vec> { + let mut results = Vec::new(); + for receipt in receipts { + let result = self.check(receipt).await; + results.push(result); + } + results + } } -// #[async_trait::async_trait] -// impl CheckBatch for T -// where -// T: Check, -// { -// async fn check_batch(receipt: &[ReceiptWithState]) -> ReceiptResult<()> { -// todo!() -// } -// } - #[derive(Debug, Serialize, Deserialize)] struct UniqueCheck; @@ -59,11 +53,8 @@ impl Check for UniqueCheck { println!("UniqueCheck"); Ok(()) } -} -#[async_trait::async_trait] -impl CheckBatch for UniqueCheck { - async fn check_batch(receipts: &[ReceiptWithState]) -> Vec> { + async fn check_batch(&self, receipts: &[ReceiptWithState]) -> Vec> { let mut signatures: HashSet = HashSet::new(); let mut results = Vec::new(); From f7e6c15415fa96d91af1abb62fa35b245dfa8f65 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 12:16:36 -0300 Subject: [PATCH 06/32] refactor: use mutable reference for checkingchecks Signed-off-by: Gustavo Inacio --- Cargo.toml | 9 +-- tap_core/src/checks/mod.rs | 17 ++++- tap_core/src/tap_manager/manager.rs | 10 ++- tap_core/src/tap_receipt/mod.rs | 4 +- tap_core/src/tap_receipt/received_receipt.rs | 73 ++++++++------------ 5 files changed, 57 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 21985cbc..9af67434 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,8 @@ [workspace] -members = [ - "tap_core", - "tap_aggregator", - "tap_integration_tests" -] +resolver = "2" +members = ["tap_core", "tap_aggregator", "tap_integration_tests"] [workspace.package] -version="0.1.0" +version = "0.1.0" edition = "2021" license = "Apache-2.0" diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs index a62bd237..3289219c 100644 --- a/tap_core/src/checks/mod.rs +++ b/tap_core/src/checks/mod.rs @@ -17,15 +17,28 @@ impl CheckingChecks { Self::Pending(check) } - pub async fn execute(self, receipt: &ReceiptWithState) -> Self { + pub async fn execute(&mut self, receipt: &ReceiptWithState) -> &mut Self { match self { Self::Pending(check) => { let result = check.check(&receipt).await; - Self::Executed(result) + *self = Self::Executed(result); + self } Self::Executed(_) => self, } } + + pub fn is_failed(&self) -> bool { + matches!(self, Self::Executed(Err(_))) + } + + pub fn is_pending(&self) -> bool { + matches!(self, Self::Pending(_)) + } + + pub fn is_complete(&self) -> bool { + matches!(self, Self::Executed(_)) + } } #[async_trait::async_trait] diff --git a/tap_core/src/tap_manager/manager.rs b/tap_core/src/tap_manager/manager.rs index 61f72f8f..939bb584 100644 --- a/tap_core/src/tap_manager/manager.rs +++ b/tap_core/src/tap_manager/manager.rs @@ -310,7 +310,15 @@ where })?; if let ReceivedReceipt::Checking(received_receipt) = &mut received_receipt { - received_receipt.perform_checks(initial_checks).await; + received_receipt + .perform_checks( + initial_checks + .iter() + .map(|check| check.typetag_name()) + .collect::>() + .as_slice(), + ) + .await; } self.executor diff --git a/tap_core/src/tap_receipt/mod.rs b/tap_core/src/tap_receipt/mod.rs index dc625fa0..369ec461 100644 --- a/tap_core/src/tap_receipt/mod.rs +++ b/tap_core/src/tap_receipt/mod.rs @@ -4,7 +4,7 @@ mod receipt; mod receipt_auditor; mod received_receipt; -use std::collections::HashMap; +use std::{collections::HashMap, sync::{Arc, Mutex, RwLock}}; use alloy_primitives::Address; pub use receipt::Receipt; @@ -42,7 +42,7 @@ pub enum ReceiptError { } pub type ReceiptResult = Result; -pub type ReceiptCheckResults = HashMap<&'static str, CheckingChecks>; +pub type ReceiptCheckResults = HashMap<&'static str, Arc>>; #[derive(Hash, Eq, PartialEq, Debug, Clone, EnumString, Display, Serialize, Deserialize)] pub enum ReceiptCheck { CheckUnique, diff --git a/tap_core/src/tap_receipt/received_receipt.rs b/tap_core/src/tap_receipt/received_receipt.rs index a716208a..2227e7ae 100644 --- a/tap_core/src/tap_receipt/received_receipt.rs +++ b/tap_core/src/tap_receipt/received_receipt.rs @@ -13,7 +13,7 @@ //! This module is useful for managing and tracking the state of received receipts, as well as //! their progress through various checks and stages of inclusion in RAV requests and received RAVs. -use std::collections::HashMap; +use std::{collections::HashMap, sync::{Arc, RwLock}}; use serde::{Deserialize, Serialize}; @@ -22,7 +22,6 @@ use crate::{ adapters::{escrow_adapter::EscrowAdapter, receipt_storage_adapter::StoredReceipt}, checks::{BoxedCheck, CheckingChecks}, eip_712_signed_message::EIP712SignedMessage, - Error, Result, }; #[derive(Debug, Serialize, Deserialize, Clone)] @@ -245,12 +244,14 @@ impl ReceiptWithState { /// /// Returns [`Error::InvalidCheckError] if requested error in not a required check (list of required checks provided by user on construction) /// - pub async fn perform_check(&mut self, check: &BoxedCheck) { + pub async fn perform_check(&mut self, check: &'static str) { // Only perform check if it is incomplete // Don't check if already failed - if !self.check_is_complete(check) && !self.any_check_resulted_in_error() { - let execute_check = self.state.checks.remove(check.typetag_name()).unwrap(); - let _ = self.update_check(check, execute_check.execute(&self).await); + let mut checks = self.state.checks.clone(); + let check = checks.get_mut(check); + if let Some(check) = check { + let mut check = check.write().unwrap(); + check.execute(&self).await; } } @@ -264,7 +265,7 @@ impl ReceiptWithState { /// /// Returns [`Error::InvalidCheckError] if requested error in not a required check (list of required checks provided by user on construction) /// - pub async fn perform_checks(&mut self, checks: &[BoxedCheck]) { + pub async fn perform_checks(&mut self, checks: &[&'static str]) { for check in checks { self.perform_check(check).await; } @@ -289,15 +290,13 @@ impl ReceiptWithState { } /// Returns all checks that completed with errors - pub fn completed_checks_with_errors(&self) -> ReceiptCheckResults { + pub(crate) fn completed_checks_with_errors(&self) -> ReceiptCheckResults { self.state .checks .iter() .filter_map(|(check, result)| { - if let CheckingChecks::Executed(unwrapped_result) = result { - if unwrapped_result.is_err() { - return Some((*check, result.clone())); - } + if result.read().unwrap().is_failed() { + return Some((*check, result.clone())); } None }) @@ -305,60 +304,44 @@ impl ReceiptWithState { } /// Returns all checks that have not been completed - pub fn incomplete_checks(&self) -> Vec { + pub(crate) fn incomplete_checks(&self) -> Vec<&'static str> { let incomplete_checks = self .state .checks .iter() - .filter_map(|(_check, result)| match result { - CheckingChecks::Pending(check) => Some(check.clone()), - CheckingChecks::Executed(_) => None, + .filter_map(|(check, result)| { + if result.read().unwrap().is_complete() { + return None; + } + Some(*check) }) .collect(); incomplete_checks } - pub(crate) fn update_check( - &mut self, - check: &BoxedCheck, - result: CheckingChecks, - ) -> Result<()> { - if !self.state.checks.contains_key(check.typetag_name()) { - return Err(Error::InvalidCheckError { - check_string: check.typetag_name().to_string(), - }); - } - - self.state.checks.insert(check.typetag_name(), result); - Ok(()) - } - - /// returns true `check` has a result, otherwise false - pub(crate) fn check_is_complete(&self, check: &BoxedCheck) -> bool { - matches!( - self.state.checks.get(check.typetag_name()), - Some(CheckingChecks::Executed(_)) - ) - } - fn any_check_resulted_in_error(&self) -> bool { - self.state.checks.iter().any(|(_, status)| match &status { - CheckingChecks::Executed(result) => result.is_err(), - _ => false, - }) + self.state + .checks + .iter() + .any(|(_, status)| status.read().unwrap().is_failed()) } pub fn checking_is_complete(&self) -> bool { self.state .checks .iter() - .all(|(_, status)| matches!(status, CheckingChecks::Executed(_))) + .all(|(_, status)| status.read().unwrap().is_complete()) } fn get_empty_required_checks_hashmap(required_checks: &[BoxedCheck]) -> ReceiptCheckResults { required_checks .iter() - .map(|check| (check.typetag_name(), CheckingChecks::Pending(check.clone()))) + .map(|check| { + ( + check.typetag_name(), + Arc::new(RwLock::new(CheckingChecks::Pending(check.clone()))), + ) + }) .collect() } } From 3de3cc717c5e51da0132f60d5291c50b4c8172e0 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 12:45:57 -0300 Subject: [PATCH 07/32] refactor: add rav signature checker Signed-off-by: Gustavo Inacio --- tap_core/Cargo.toml | 1 + tap_core/src/tap_manager/manager.rs | 25 +++++++++++++-------- tap_core/src/tap_receipt/receipt_auditor.rs | 23 +++++++++++++++++++ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/tap_core/Cargo.toml b/tap_core/Cargo.toml index f7fe58e6..ba0b35ac 100644 --- a/tap_core/Cargo.toml +++ b/tap_core/Cargo.toml @@ -25,6 +25,7 @@ strum_macros = "0.24.3" async-trait = "0.1.72" tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } typetag = "0.2.14" +futures = "0.3.30" [dev-dependencies] criterion = { version = "0.5", features = ["async_std"] } diff --git a/tap_core/src/tap_manager/manager.rs b/tap_core/src/tap_manager/manager.rs index 939bb584..9fd23bee 100644 --- a/tap_core/src/tap_manager/manager.rs +++ b/tap_core/src/tap_manager/manager.rs @@ -3,7 +3,9 @@ use std::sync::Arc; +use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; +use futures::Future; use super::{RAVRequest, SignedRAV, SignedReceipt}; use crate::{ @@ -69,15 +71,19 @@ where /// /// Returns [`Error::AdapterError`] if there are any errors while storing RAV /// - pub async fn verify_and_store_rav( + pub async fn verify_and_store_rav( &self, expected_rav: ReceiptAggregateVoucher, signed_rav: SignedRAV, - ) -> std::result::Result<(), Error> { - // TODO - // self.receipt_auditor - // .check_rav_signature(&signed_rav) - // .await?; + verify_signer: F, + ) -> std::result::Result<(), Error> + where + F: FnOnce(Address) -> Fut, + Fut: Future>, + { + self.receipt_auditor + .check_rav_signature(&signed_rav, verify_signer) + .await?; if signed_rav.message != expected_rav { return Err(Error::InvalidReceivedRAV { @@ -86,6 +92,10 @@ where }); } + self.timestamp_check + .update_min_timestamp_ns(expected_rav.timestamp_ns) + .await; + self.executor .update_last_rav(signed_rav) .await @@ -209,9 +219,6 @@ where let expected_rav = Self::generate_expected_rav(&valid_receipts, previous_rav.clone())?; - self.timestamp_check - .update_min_timestamp_ns(expected_rav.timestamp_ns) - .await; let valid_receipts = valid_receipts .into_iter() .map(|rx_receipt| rx_receipt.signed_receipt) diff --git a/tap_core/src/tap_receipt/receipt_auditor.rs b/tap_core/src/tap_receipt/receipt_auditor.rs index dd2616c0..c5da0860 100644 --- a/tap_core/src/tap_receipt/receipt_auditor.rs +++ b/tap_core/src/tap_receipt/receipt_auditor.rs @@ -1,11 +1,15 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 +use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; +use futures::Future; use crate::{ adapters::escrow_adapter::EscrowAdapter, + tap_manager::SignedRAV, tap_receipt::{ReceiptError, ReceiptResult}, + Error, }; use super::{AwaitingReserve, ReceiptWithState}; @@ -25,6 +29,25 @@ impl ReceiptAuditor { executor, } } + + pub async fn check_rav_signature( + &self, + signed_rav: &SignedRAV, + verify_signer: F, + ) -> Result<(), Error> + where + F: FnOnce(Address) -> Fut, + Fut: Future>, + { + let recovered_address = signed_rav.recover_signer(&self.domain_separator)?; + if verify_signer(recovered_address).await? { + Ok(()) + } else { + Err(Error::InvalidRecoveredSigner { + address: recovered_address, + }) + } + } } impl ReceiptAuditor From bfb1eb07aa2d26f611ce14d4d0b57fddb9da2ea6 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 12:48:33 -0300 Subject: [PATCH 08/32] fix: update timestamp check only after rav update Signed-off-by: Gustavo Inacio --- tap_core/src/tap_manager/manager.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tap_core/src/tap_manager/manager.rs b/tap_core/src/tap_manager/manager.rs index 9fd23bee..d8f01fd1 100644 --- a/tap_core/src/tap_manager/manager.rs +++ b/tap_core/src/tap_manager/manager.rs @@ -103,6 +103,10 @@ where source_error: anyhow::Error::new(err), })?; + self.timestamp_check + .update_min_timestamp_ns(expected_rav.timestamp_ns) + .await; + Ok(()) } } From 06cf24cac1b4e50bcd8234557e09556b7de75007 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 14:34:04 -0300 Subject: [PATCH 09/32] fix: compile tests Signed-off-by: Gustavo Inacio --- .../test/receipt_checks_adapter_test.rs | 30 ++++---- tap_core/src/checks/mod.rs | 13 ++-- tap_core/src/tap_manager/manager.rs | 8 +- tap_core/src/tap_manager/test/manager_test.rs | 75 +++++++++---------- tap_core/src/tap_receipt/mod.rs | 24 +++--- tap_core/src/tap_receipt/received_receipt.rs | 30 +++----- .../tests/received_receipt_tests.rs | 32 ++++---- .../tests/indexer_mock/mod.rs | 23 ++---- tap_integration_tests/tests/showcase.rs | 21 +++--- 9 files changed, 121 insertions(+), 135 deletions(-) diff --git a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs index 977a6b6b..70139eb4 100644 --- a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs @@ -108,22 +108,22 @@ mod receipt_checks_adapter_unit_test { .await .insert(unique_receipt_id, new_receipt.1.clone()); - assert!(receipt_checks_adapter - .is_unique(new_receipt.1.signed_receipt(), unique_receipt_id) - .await - .unwrap()); - assert!(receipt_checks_adapter - .is_valid_allocation_id(new_receipt.1.signed_receipt().message.allocation_id) - .await - .unwrap()); + // assert!(receipt_checks_adapter + // .is_unique(new_receipt.1.signed_receipt(), unique_receipt_id) + // .await + // .unwrap()); + // assert!(receipt_checks_adapter + // .is_valid_allocation_id(new_receipt.1.signed_receipt().message.allocation_id) + // .await + // .unwrap()); // TODO: Add check when sender_id is available from received receipt (issue: #56) // assert!(receipt_checks_adapter.is_valid_sender_id(sender_id)); - assert!(receipt_checks_adapter - .is_valid_value( - new_receipt.1.signed_receipt().message.value, - new_receipt.1.query_id() - ) - .await - .unwrap()); + // assert!(receipt_checks_adapter + // .is_valid_value( + // new_receipt.1.signed_receipt().message.value, + // new_receipt.1.query_id() + // ) + // .await + // .unwrap()); } } diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs index 3289219c..1e277903 100644 --- a/tap_core/src/checks/mod.rs +++ b/tap_core/src/checks/mod.rs @@ -4,25 +4,26 @@ use serde::{Deserialize, Serialize}; use std::{collections::HashSet, sync::Arc}; use tokio::sync::RwLock; -pub type BoxedCheck = Arc; +pub type ReceiptCheck = Arc; #[derive(Serialize, Deserialize, Clone, Debug)] pub enum CheckingChecks { - Pending(BoxedCheck), + Pending(ReceiptCheck), Executed(ReceiptResult<()>), } impl CheckingChecks { - pub fn new(check: BoxedCheck) -> Self { + pub fn new(check: ReceiptCheck) -> Self { Self::Pending(check) } - pub async fn execute(&mut self, receipt: &ReceiptWithState) -> &mut Self { + pub async fn execute(self, receipt: &ReceiptWithState) -> Self { match self { Self::Pending(check) => { let result = check.check(&receipt).await; - *self = Self::Executed(result); - self + // *self = Self::Executed(result); + // self + Self::Executed(result) } Self::Executed(_) => self, } diff --git a/tap_core/src/tap_manager/manager.rs b/tap_core/src/tap_manager/manager.rs index d8f01fd1..ba44f39e 100644 --- a/tap_core/src/tap_manager/manager.rs +++ b/tap_core/src/tap_manager/manager.rs @@ -14,7 +14,7 @@ use crate::{ rav_storage_adapter::{RAVRead, RAVStore}, receipt_storage_adapter::{ReceiptRead, ReceiptStore}, }, - checks::{BoxedCheck, TimestampCheck}, + checks::{ReceiptCheck, TimestampCheck}, receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::{ CategorizedReceiptsWithState, Failed, ReceiptAuditor, ReceiptWithId, ReceiptWithState, @@ -27,7 +27,7 @@ pub struct Manager { /// Executor that implements adapters executor: E, /// Checks that must be completed for each receipt before being confirmed or denied for rav request - required_checks: Vec, + required_checks: Vec, /// Struct responsible for doing checks for receipt. Ownership stays with manager allowing manager /// to update configuration ( like minimum timestamp ). receipt_auditor: ReceiptAuditor, @@ -46,7 +46,7 @@ where pub fn new( domain_separator: Eip712Domain, executor: E, - mut required_checks: Vec, + mut required_checks: Vec, starting_min_timestamp_ns: u64, ) -> Self { let timestamp_check = Arc::new(TimestampCheck::new(starting_min_timestamp_ns)); @@ -304,7 +304,7 @@ where &self, signed_receipt: SignedReceipt, query_id: u64, - initial_checks: &[BoxedCheck], + initial_checks: &[ReceiptCheck], ) -> std::result::Result<(), Error> { let mut received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &self.required_checks); diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index 28ef6363..7e63ebad 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -20,13 +20,12 @@ mod manager_unit_test { adapters::{ escrow_adapter_mock::EscrowAdapterMock, executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, - receipt_checks_adapter_mock::ReceiptChecksAdapterMock, - receipt_storage_adapter::ReceiptRead, + receipt_storage_adapter::ReceiptRead, receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }, - checks::BoxedCheck, + checks::ReceiptCheck, eip_712_signed_message::EIP712SignedMessage, get_current_timestamp_u64_ns, tap_eip712_domain, - tap_receipt::{get_full_list_of_checks, Receipt, ReceiptCheck}, + tap_receipt::{get_full_list_of_checks, Receipt}, }; #[fixture] @@ -100,19 +99,19 @@ mod manager_unit_test { } #[fixture] - fn receipt_adapters() -> (ReceiptChecksAdapterMock, Arc>>) { + fn receipt_adapters() -> (ReceiptStorageAdapterMock, Arc>>) { let receipt_storage = Arc::new(RwLock::new(HashMap::new())); - let allocation_ids_set = Arc::new(RwLock::new(HashSet::from_iter(allocation_ids()))); - let sender_ids_set = Arc::new(RwLock::new(HashSet::from_iter(sender_ids()))); + // let allocation_ids_set = Arc::new(RwLock::new(HashSet::from_iter(allocation_ids()))); + // let sender_ids_set = Arc::new(RwLock::new(HashSet::from_iter(sender_ids()))); let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); - let receipt_checks_adapter = ReceiptChecksAdapterMock::new( - Arc::clone(&receipt_storage), - Arc::clone(&query_appraisal_storage), - Arc::clone(&allocation_ids_set), - Arc::clone(&sender_ids_set), - ); + // let receipt_checks_adapter = ReceiptChecksAdapterMock::new( + // Arc::clone(&receipt_storage), + // Arc::clone(&query_appraisal_storage), + // Arc::clone(&allocation_ids_set), + // Arc::clone(&sender_ids_set), + // ); (receipt_checks_adapter, query_appraisal_storage) } @@ -120,14 +119,14 @@ mod manager_unit_test { #[rstest] #[case::full_checks(get_full_list_of_checks())] #[case::partial_checks(todo!())] - #[case::no_checks(Vec::::new())] + #[case::no_checks(Vec::::new())] #[tokio::test] async fn manager_verify_and_store_varying_initial_checks( executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, - #[case] initial_checks: Vec, + #[case] initial_checks: Vec, ) { let (executor, escrow_storage, query_appraisal_storage) = executor_mock; // give receipt 5 second variance for min start time @@ -163,7 +162,7 @@ mod manager_unit_test { #[rstest] #[case::full_checks(get_full_list_of_checks())] - #[case::partial_checks(vec![ReceiptCheck::CheckSignature])] + // #[case::partial_checks(vec![ReceiptCheck::CheckSignature])] #[case::no_checks(Vec::::new())] #[tokio::test] async fn manager_create_rav_request_all_valid_receipts( @@ -221,15 +220,15 @@ mod manager_unit_test { EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) .await .unwrap(); - assert!(manager - .verify_and_store_rav(rav_request.expected_rav, signed_rav) - .await - .is_ok()); + // assert!(manager + // .verify_and_store_rav(rav_request.expected_rav, signed_rav) + // .await + // .is_ok()); } #[rstest] #[case::full_checks(get_full_list_of_checks())] - #[case::partial_checks(vec![ReceiptCheck::CheckSignature])] + // #[case::partial_checks(vec![ReceiptCheck::CheckSignature])] #[case::no_checks(Vec::::new())] #[tokio::test] async fn manager_create_multiple_rav_requests_all_valid_receipts( @@ -297,10 +296,10 @@ mod manager_unit_test { EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) .await .unwrap(); - assert!(manager - .verify_and_store_rav(rav_request.expected_rav, signed_rav) - .await - .is_ok()); + // assert!(manager + // .verify_and_store_rav(rav_request.expected_rav, signed_rav) + // .await + // .is_ok()); stored_signed_receipts.clear(); for query_id in 10..20 { @@ -346,10 +345,10 @@ mod manager_unit_test { EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) .await .unwrap(); - assert!(manager - .verify_and_store_rav(rav_request.expected_rav, signed_rav) - .await - .is_ok()); + // assert!(manager + // .verify_and_store_rav(rav_request.expected_rav, signed_rav) + // .await + // .is_ok()); } #[rstest] @@ -359,7 +358,7 @@ mod manager_unit_test { keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, - #[values(get_full_list_of_checks(), vec![ReceiptCheck::CheckSignature], Vec::::new())] + #[values(get_full_list_of_checks(), /* vec![ReceiptCheck::CheckSignature], */Vec::::new())] initial_checks: Vec, #[values(true, false)] remove_old_receipts: bool, ) { @@ -429,10 +428,10 @@ mod manager_unit_test { ) .await .unwrap(); - assert!(manager - .verify_and_store_rav(rav_request_1.expected_rav, signed_rav_1) - .await - .is_ok()); + // assert!(manager + // .verify_and_store_rav(rav_request_1.expected_rav, signed_rav_1) + // .await + // .is_ok()); stored_signed_receipts.clear(); for query_id in 10..20 { @@ -495,9 +494,9 @@ mod manager_unit_test { ) .await .unwrap(); - assert!(manager - .verify_and_store_rav(rav_request_2.expected_rav, signed_rav_2) - .await - .is_ok()); + // assert!(manager + // .verify_and_store_rav(rav_request_2.expected_rav, signed_rav_2) + // .await + // .is_ok()); } } diff --git a/tap_core/src/tap_receipt/mod.rs b/tap_core/src/tap_receipt/mod.rs index 369ec461..f431019a 100644 --- a/tap_core/src/tap_receipt/mod.rs +++ b/tap_core/src/tap_receipt/mod.rs @@ -4,7 +4,7 @@ mod receipt; mod receipt_auditor; mod received_receipt; -use std::{collections::HashMap, sync::{Arc, Mutex, RwLock}}; +use std::{collections::HashMap, sync::{Arc, RwLock}}; use alloy_primitives::Address; pub use receipt::Receipt; @@ -18,7 +18,7 @@ use serde::{Deserialize, Serialize}; use strum_macros::{Display, EnumString}; use thiserror::Error; -use crate::checks::{BoxedCheck, CheckingChecks}; +use crate::checks::{CheckingChecks, ReceiptCheck}; #[derive(Error, Debug, Clone, Serialize, Deserialize)] pub enum ReceiptError { @@ -42,15 +42,15 @@ pub enum ReceiptError { } pub type ReceiptResult = Result; -pub type ReceiptCheckResults = HashMap<&'static str, Arc>>; -#[derive(Hash, Eq, PartialEq, Debug, Clone, EnumString, Display, Serialize, Deserialize)] -pub enum ReceiptCheck { - CheckUnique, - CheckAllocationId, - CheckTimestamp, - CheckValue, - CheckSignature, -} +pub type ReceiptCheckResults = HashMap<&'static str, CheckingChecks>; +// #[derive(Hash, Eq, PartialEq, Debug, Clone, EnumString, Display, Serialize, Deserialize)] +// pub enum ReceiptCheck { +// CheckUnique, +// CheckAllocationId, +// CheckTimestamp, +// CheckValue, +// CheckSignature, +// } pub fn get_full_list_of_receipt_check_results() -> ReceiptCheckResults { let all_checks_list = ReceiptCheckResults::new(); @@ -63,7 +63,7 @@ pub fn get_full_list_of_receipt_check_results() -> ReceiptCheckResults { all_checks_list } -pub fn get_full_list_of_checks() -> Vec { +pub fn get_full_list_of_checks() -> Vec { vec![ // ReceiptCheck::CheckUnique, // ReceiptCheck::CheckAllocationId, diff --git a/tap_core/src/tap_receipt/received_receipt.rs b/tap_core/src/tap_receipt/received_receipt.rs index 2227e7ae..84527409 100644 --- a/tap_core/src/tap_receipt/received_receipt.rs +++ b/tap_core/src/tap_receipt/received_receipt.rs @@ -20,7 +20,7 @@ use serde::{Deserialize, Serialize}; use super::{receipt_auditor::ReceiptAuditor, Receipt, ReceiptCheckResults}; use crate::{ adapters::{escrow_adapter::EscrowAdapter, receipt_storage_adapter::StoredReceipt}, - checks::{BoxedCheck, CheckingChecks}, + checks::{CheckingChecks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, }; @@ -174,7 +174,7 @@ impl ReceivedReceipt { pub fn new( signed_receipt: EIP712SignedMessage, query_id: u64, - required_checks: &[BoxedCheck], + required_checks: &[ReceiptCheck], ) -> Self { let checks = ReceiptWithState::get_empty_required_checks_hashmap(required_checks); @@ -244,14 +244,13 @@ impl ReceiptWithState { /// /// Returns [`Error::InvalidCheckError] if requested error in not a required check (list of required checks provided by user on construction) /// - pub async fn perform_check(&mut self, check: &'static str) { + pub async fn perform_check(&mut self, check_name: &'static str) { // Only perform check if it is incomplete // Don't check if already failed - let mut checks = self.state.checks.clone(); - let check = checks.get_mut(check); + let check = self.state.checks.remove(check_name); if let Some(check) = check { - let mut check = check.write().unwrap(); - check.execute(&self).await; + let result = check.execute(&self).await; + self.state.checks.insert(check_name, result); } } @@ -295,7 +294,7 @@ impl ReceiptWithState { .checks .iter() .filter_map(|(check, result)| { - if result.read().unwrap().is_failed() { + if result.is_failed() { return Some((*check, result.clone())); } None @@ -310,7 +309,7 @@ impl ReceiptWithState { .checks .iter() .filter_map(|(check, result)| { - if result.read().unwrap().is_complete() { + if result.is_complete() { return None; } Some(*check) @@ -323,25 +322,20 @@ impl ReceiptWithState { self.state .checks .iter() - .any(|(_, status)| status.read().unwrap().is_failed()) + .any(|(_, status)| status.is_failed()) } pub fn checking_is_complete(&self) -> bool { self.state .checks .iter() - .all(|(_, status)| status.read().unwrap().is_complete()) + .all(|(_, status)| status.is_complete()) } - fn get_empty_required_checks_hashmap(required_checks: &[BoxedCheck]) -> ReceiptCheckResults { + fn get_empty_required_checks_hashmap(required_checks: &[ReceiptCheck]) -> ReceiptCheckResults { required_checks .iter() - .map(|check| { - ( - check.typetag_name(), - Arc::new(RwLock::new(CheckingChecks::Pending(check.clone()))), - ) - }) + .map(|check| (check.typetag_name(), CheckingChecks::Pending(check.clone()))) .collect() } } diff --git a/tap_core/src/tap_receipt/tests/received_receipt_tests.rs b/tap_core/src/tap_receipt/tests/received_receipt_tests.rs index 49d6718d..13b016fd 100644 --- a/tap_core/src/tap_receipt/tests/received_receipt_tests.rs +++ b/tap_core/src/tap_receipt/tests/received_receipt_tests.rs @@ -199,15 +199,15 @@ mod received_receipt_unit_test { }; // perform single arbitrary check - let arbitrary_check_to_perform = ReceiptCheck::CheckUnique; - received_receipt - .perform_check(&arbitrary_check_to_perform, receipt_id, &receipt_auditor) - .await; - assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); - - received_receipt - .perform_checks(&checks, receipt_id, &receipt_auditor) - .await; + // let arbitrary_check_to_perform = ReceiptCheck::CheckUnique; + // received_receipt + // .perform_check(&arbitrary_check_to_perform, receipt_id, &receipt_auditor) + // .await; + // assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); + // + // received_receipt + // .perform_checks(&checks, receipt_id, &receipt_auditor) + // .await; assert!(received_receipt.checking_is_complete()); } @@ -257,15 +257,15 @@ mod received_receipt_unit_test { }; // perform single arbitrary check - let arbitrary_check_to_perform = ReceiptCheck::CheckUnique; + // let arbitrary_check_to_perform = ReceiptCheck::CheckUnique; - received_receipt - .perform_check(&arbitrary_check_to_perform, receipt_id, &receipt_auditor) - .await; - assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); + // received_receipt + // .perform_check(/** Check Unique */) + // .await; + // assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); assert!(received_receipt - .finalize_receipt_checks(receipt_id, &receipt_auditor) + .finalize_receipt_checks() .await .is_ok()); } @@ -317,7 +317,7 @@ mod received_receipt_unit_test { }; assert!(received_receipt - .finalize_receipt_checks(receipt_id, &receipt_auditor) + .finalize_receipt_checks() .await .is_ok()); } diff --git a/tap_integration_tests/tests/indexer_mock/mod.rs b/tap_integration_tests/tests/indexer_mock/mod.rs index 45a9c309..20f7af4e 100644 --- a/tap_integration_tests/tests/indexer_mock/mod.rs +++ b/tap_integration_tests/tests/indexer_mock/mod.rs @@ -23,11 +23,10 @@ use tap_core::{ adapters::{ escrow_adapter::EscrowAdapter, rav_storage_adapter::{RAVRead, RAVStore}, - receipt_checks_adapter::ReceiptChecksAdapter, receipt_storage_adapter::{ReceiptRead, ReceiptStore}, }, + checks::ReceiptCheck, tap_manager::{Manager, SignedRAV, SignedReceipt}, - tap_receipt::ReceiptCheck, Error as TapCoreError, }; /// Rpc trait represents a JSON-RPC server that has a single async method `request`. @@ -95,15 +94,7 @@ where #[async_trait] impl RpcServer for RpcManager where - E: ReceiptStore - + ReceiptRead - + RAVStore - + RAVRead - + ReceiptChecksAdapter - + EscrowAdapter - + Send - + Sync - + 'static, + E: ReceiptStore + ReceiptRead + RAVStore + RAVRead + EscrowAdapter + Send + Sync + 'static, { async fn request( &self, @@ -169,7 +160,6 @@ where + ReceiptRead + RAVStore + RAVRead - + ReceiptChecksAdapter + EscrowAdapter + Clone + Send @@ -206,7 +196,7 @@ async fn request_rav( threshold: usize, ) -> Result<()> where - E: ReceiptRead + RAVRead + RAVStore + EscrowAdapter + ReceiptChecksAdapter, + E: ReceiptRead + RAVRead + RAVStore + EscrowAdapter, { // Create the aggregate_receipts request params let rav_request = manager.create_rav_request(time_stamp_buffer, None).await?; @@ -223,9 +213,10 @@ where .0 .request("aggregate_receipts", params) .await?; - manager - .verify_and_store_rav(rav_request.expected_rav, remote_rav_result.data) - .await?; + // TODO update this with the async function + // manager + // .verify_and_store_rav(rav_request.expected_rav, remote_rav_result.data) + // .await?; // For these tests, we expect every receipt to be valid, i.e. there should be no invalid receipts, nor any missing receipts (less than the expected threshold). // If there is throw an error. diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 9121270a..79fd2eb5 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -32,6 +32,7 @@ use tap_core::{ receipt_checks_adapter_mock::ReceiptChecksAdapterMock, receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }, + checks::ReceiptCheck, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, tap_manager::SignedRAV, @@ -229,11 +230,11 @@ fn rav_storage_adapter() -> RAVStorageAdapterMock { #[fixture] fn required_checks() -> Vec { vec![ - ReceiptCheck::CheckAllocationId, - ReceiptCheck::CheckSignature, - ReceiptCheck::CheckTimestamp, - ReceiptCheck::CheckUnique, - ReceiptCheck::CheckValue, + // ReceiptCheck::CheckAllocationId, + // ReceiptCheck::CheckSignature, + // ReceiptCheck::CheckTimestamp, + // ReceiptCheck::CheckUnique, + // ReceiptCheck::CheckValue, ] } @@ -242,11 +243,11 @@ fn required_checks() -> Vec { #[fixture] fn initial_checks() -> Vec { vec![ - ReceiptCheck::CheckAllocationId, - ReceiptCheck::CheckSignature, - ReceiptCheck::CheckTimestamp, - ReceiptCheck::CheckUnique, - ReceiptCheck::CheckValue, + // ReceiptCheck::CheckAllocationId, + // ReceiptCheck::CheckSignature, + // ReceiptCheck::CheckTimestamp, + // ReceiptCheck::CheckUnique, + // ReceiptCheck::CheckValue, ] } From b32d969bf16748473a9cf8f4e0f8cb7d2621fddb Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 15:24:59 -0300 Subject: [PATCH 10/32] refactor: remove unneeded async function Signed-off-by: Gustavo Inacio --- tap_aggregator/src/aggregator.rs | 37 +++------ tap_aggregator/src/server.rs | 26 ++---- .../timeline_aggretion_protocol_benchmark.rs | 41 +++------- .../adapters/test/rav_storage_adapter_test.rs | 4 - .../test/receipt_checks_adapter_test.rs | 2 - .../test/receipt_storage_adapter_test.rs | 7 +- tap_core/src/eip_712_signed_message.rs | 2 +- tap_core/src/lib.rs | 28 +++---- tap_core/src/tap_manager/test/manager_test.rs | 81 ++++++++++--------- .../tests/received_receipt_tests.rs | 14 +--- tap_integration_tests/tests/showcase.rs | 74 ++++++++--------- 11 files changed, 122 insertions(+), 194 deletions(-) diff --git a/tap_aggregator/src/aggregator.rs b/tap_aggregator/src/aggregator.rs index 22afa6e2..bbd6a6ca 100644 --- a/tap_aggregator/src/aggregator.rs +++ b/tap_aggregator/src/aggregator.rs @@ -14,7 +14,7 @@ use tap_core::{ receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::Receipt, }; -pub async fn check_and_aggregate_receipts( +pub fn check_and_aggregate_receipts( domain_separator: &Eip712Domain, receipts: &[EIP712SignedMessage], previous_rav: Option>, @@ -69,7 +69,7 @@ pub async fn check_and_aggregate_receipts( let rav = ReceiptAggregateVoucher::aggregate_receipts(allocation_id, receipts, previous_rav)?; // Sign the rav and return - Ok(EIP712SignedMessage::new(domain_separator, rav, wallet).await?) + Ok(EIP712SignedMessage::new(domain_separator, rav, wallet)?) } fn check_signature_is_from_one_of_addresses( @@ -170,8 +170,8 @@ mod tests { } #[rstest] - #[tokio::test] - async fn check_signatures_unique_fail( + #[test] + fn check_signatures_unique_fail( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -183,7 +183,6 @@ mod tests { Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) - .await .unwrap(); receipts.push(receipt.clone()); receipts.push(receipt); @@ -193,8 +192,8 @@ mod tests { } #[rstest] - #[tokio::test] - async fn check_signatures_unique_ok( + #[test] + fn check_signatures_unique_ok( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -207,7 +206,6 @@ mod tests { Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) - .await .unwrap(), ); receipts.push( @@ -216,7 +214,6 @@ mod tests { Receipt::new(allocation_ids[0], 43).unwrap(), &keys.0, ) - .await .unwrap(), ); @@ -225,9 +222,9 @@ mod tests { } #[rstest] - #[tokio::test] + #[test] /// Test that a receipt with a timestamp greater then the rav timestamp passes - async fn check_receipt_timestamps( + fn check_receipt_timestamps( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -247,7 +244,6 @@ mod tests { }, &keys.0, ) - .await .unwrap(), ); } @@ -262,7 +258,6 @@ mod tests { }, &keys.0, ) - .await .unwrap(); assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_ok()); @@ -277,7 +272,6 @@ mod tests { }, &keys.0, ) - .await .unwrap(); assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_err()); @@ -292,16 +286,15 @@ mod tests { }, &keys.0, ) - .await .unwrap(); assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_err()); } #[rstest] - #[tokio::test] + #[test] /// Test check_allocation_id with 2 receipts that have the correct allocation id /// and 1 receipt that has the wrong allocation id - async fn check_allocation_id_fail( + fn check_allocation_id_fail( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -313,7 +306,6 @@ mod tests { Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) - .await .unwrap(), ); receipts.push( @@ -322,7 +314,6 @@ mod tests { Receipt::new(allocation_ids[0], 43).unwrap(), &keys.0, ) - .await .unwrap(), ); receipts.push( @@ -331,7 +322,6 @@ mod tests { Receipt::new(allocation_ids[1], 44).unwrap(), &keys.0, ) - .await .unwrap(), ); @@ -341,9 +331,9 @@ mod tests { } #[rstest] - #[tokio::test] + #[test] /// Test check_allocation_id with 3 receipts that have the correct allocation id - async fn check_allocation_id_ok( + fn check_allocation_id_ok( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -355,7 +345,6 @@ mod tests { Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) - .await .unwrap(), ); receipts.push( @@ -364,7 +353,6 @@ mod tests { Receipt::new(allocation_ids[0], 43).unwrap(), &keys.0, ) - .await .unwrap(), ); receipts.push( @@ -373,7 +361,6 @@ mod tests { Receipt::new(allocation_ids[0], 44).unwrap(), &keys.0, ) - .await .unwrap(), ); diff --git a/tap_aggregator/src/server.rs b/tap_aggregator/src/server.rs index aea28468..92b3b9ef 100644 --- a/tap_aggregator/src/server.rs +++ b/tap_aggregator/src/server.rs @@ -7,11 +7,7 @@ use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; use anyhow::Result; use ethers_signers::LocalWallet; -use jsonrpsee::{ - proc_macros::rpc, - server::ServerBuilder, - {core::async_trait, server::ServerHandle}, -}; +use jsonrpsee::{proc_macros::rpc, server::ServerBuilder, server::ServerHandle}; use lazy_static::lazy_static; use prometheus::{register_counter, register_int_counter, Counter, IntCounter}; @@ -82,12 +78,12 @@ lazy_static! { pub trait Rpc { /// Returns the versions of the TAP JSON-RPC API implemented by this server. #[method(name = "api_versions")] - async fn api_versions(&self) -> JsonRpcResult; + fn api_versions(&self) -> JsonRpcResult; /// Aggregates the given receipts into a receipt aggregate voucher. /// Returns an error if the user expected API version is not supported. #[method(name = "aggregate_receipts")] - async fn aggregate_receipts( + fn aggregate_receipts( &self, api_version: String, receipts: Vec>, @@ -131,7 +127,7 @@ fn check_api_version_deprecation(api_version: &TapRpcApiVersion) -> Option, @@ -179,13 +175,12 @@ async fn aggregate_receipts_( } } -#[async_trait] impl RpcServer for RpcImpl { - async fn api_versions(&self) -> JsonRpcResult { + fn api_versions(&self) -> JsonRpcResult { Ok(JsonRpcResponse::ok(tap_rpc_api_versions_info())) } - async fn aggregate_receipts( + fn aggregate_receipts( &self, api_version: String, receipts: Vec>, @@ -202,9 +197,7 @@ impl RpcServer for RpcImpl { &self.domain_separator, receipts, previous_rav, - ) - .await - { + ) { Ok(res) => { TOTAL_GRT_AGGREGATED.inc_by(receipts_grt as f64); TOTAL_AGGREGATED_RECEIPTS.inc_by(receipts_count); @@ -410,7 +403,6 @@ mod tests { Receipt::new(allocation_ids[0], value).unwrap(), &all_wallets.choose(&mut rng).unwrap().wallet, ) - .await .unwrap(), ); } @@ -492,7 +484,6 @@ mod tests { Receipt::new(allocation_ids[0], value).unwrap(), &all_wallets.choose(&mut rng).unwrap().wallet, ) - .await .unwrap(), ); } @@ -509,7 +500,6 @@ mod tests { prev_rav, &all_wallets.choose(&mut rng).unwrap().wallet, ) - .await .unwrap(); // Create new RAV from last half of receipts and prev_rav through the JSON-RPC server @@ -569,7 +559,6 @@ mod tests { Receipt::new(allocation_ids[0], 42).unwrap(), &keys_main.wallet, ) - .await .unwrap()]; // Skipping receipts validation in this test, aggregate_receipts assumes receipts are valid. @@ -665,7 +654,6 @@ mod tests { Receipt::new(allocation_ids[0], u128::MAX / 1000).unwrap(), &keys_main.wallet, ) - .await .unwrap(), ); } diff --git a/tap_core/benches/timeline_aggretion_protocol_benchmark.rs b/tap_core/benches/timeline_aggretion_protocol_benchmark.rs index 7bcc36e8..5c015538 100644 --- a/tap_core/benches/timeline_aggretion_protocol_benchmark.rs +++ b/tap_core/benches/timeline_aggretion_protocol_benchmark.rs @@ -11,8 +11,7 @@ use std::str::FromStr; use alloy_primitives::Address; -use alloy_sol_types::Eip712Domain; -use criterion::async_executor::AsyncStdExecutor; +use alloy_sol_types::{eip712_domain, Eip712Domain}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use ethers::signers::{LocalWallet, Signer, Wallet}; use ethers_core::k256::ecdsa::SigningKey; @@ -22,9 +21,8 @@ use tap_core::{ eip_712_signed_message::EIP712SignedMessage, receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::Receipt, }; -use tokio::runtime::Runtime; -pub async fn create_and_sign_receipt( +pub fn create_and_sign_receipt( domain_separator: &Eip712Domain, allocation_id: Address, value: u128, @@ -35,15 +33,12 @@ pub async fn create_and_sign_receipt( Receipt::new(allocation_id, value).unwrap(), wallet, ) - .await .unwrap() } pub fn criterion_benchmark(c: &mut Criterion) { let domain_seperator = tap_eip712_domain(1, Address::from([0x11u8; 20])); - let async_runtime = Runtime::new().unwrap(); - let wallet = LocalWallet::new(&mut OsRng); let address: [u8; 20] = wallet.address().into(); let address: Address = address.into(); @@ -53,7 +48,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { let value = 12345u128; c.bench_function("Create Receipt", |b| { - b.to_async(AsyncStdExecutor).iter(|| { + b.iter(|| { create_and_sign_receipt( black_box(&domain_seperator), black_box(allocation_id), @@ -63,12 +58,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { }) }); - let receipt = async_runtime.block_on(create_and_sign_receipt( - &domain_seperator, - allocation_id, - value, - &wallet, - )); + let receipt = create_and_sign_receipt(&domain_seperator, allocation_id, value, &wallet); c.bench_function("Validate Receipt", |b| { b.iter(|| { @@ -82,14 +72,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { for log_number_of_receipts in 10..30 { let receipts = (0..2 ^ log_number_of_receipts) - .map(|_| { - async_runtime.block_on(create_and_sign_receipt( - &domain_seperator, - allocation_id, - value, - &wallet, - )) - }) + .map(|_| create_and_sign_receipt(&domain_seperator, allocation_id, value, &wallet)) .collect::>(); rav_group.bench_function( @@ -105,14 +88,12 @@ pub fn criterion_benchmark(c: &mut Criterion) { }, ); - let signed_rav = async_runtime - .block_on(EIP712SignedMessage::new( - &domain_seperator, - ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None) - .unwrap(), - &wallet, - )) - .unwrap(); + let signed_rav = EIP712SignedMessage::new( + &domain_seperator, + ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None).unwrap(), + &wallet, + ) + .unwrap(); rav_group.bench_function( &format!("Validate RAV w/ 2^{} receipt's", log_number_of_receipts), diff --git a/tap_core/src/adapters/test/rav_storage_adapter_test.rs b/tap_core/src/adapters/test/rav_storage_adapter_test.rs index ce5c324f..09677d96 100644 --- a/tap_core/src/adapters/test/rav_storage_adapter_test.rs +++ b/tap_core/src/adapters/test/rav_storage_adapter_test.rs @@ -53,7 +53,6 @@ mod rav_storage_adapter_unit_test { Receipt::new(allocation_id, value).unwrap(), &wallet, ) - .await .unwrap(), ); } @@ -63,7 +62,6 @@ mod rav_storage_adapter_unit_test { ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None).unwrap(), &wallet, ) - .await .unwrap(); rav_storage_adapter @@ -86,7 +84,6 @@ mod rav_storage_adapter_unit_test { Receipt::new(allocation_id, value).unwrap(), &wallet, ) - .await .unwrap(), ); } @@ -96,7 +93,6 @@ mod rav_storage_adapter_unit_test { ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None).unwrap(), &wallet, ) - .await .unwrap(); // Update the last rav diff --git a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs index 70139eb4..86f6f532 100644 --- a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs @@ -64,7 +64,6 @@ mod receipt_checks_adapter_unit_test { Receipt::new(allocation_ids[0], value).unwrap(), &wallet, ) - .await .unwrap(), id, &get_full_list_of_checks(), @@ -95,7 +94,6 @@ mod receipt_checks_adapter_unit_test { Receipt::new(allocation_ids[0], value).unwrap(), &wallet, ) - .await .unwrap(), 10u64, &get_full_list_of_checks(), diff --git a/tap_core/src/adapters/test/receipt_storage_adapter_test.rs b/tap_core/src/adapters/test/receipt_storage_adapter_test.rs index 26beeaf4..9f8cd71f 100644 --- a/tap_core/src/adapters/test/receipt_storage_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_storage_adapter_test.rs @@ -55,7 +55,6 @@ mod receipt_storage_adapter_unit_test { Receipt::new(allocation_id, value).unwrap(), &wallet, ) - .await .unwrap(), query_id, &get_full_list_of_checks(), @@ -117,7 +116,6 @@ mod receipt_storage_adapter_unit_test { Receipt::new(allocation_id, value).unwrap(), &wallet, ) - .await .unwrap(), query_id as u64, &get_full_list_of_checks(), @@ -185,8 +183,8 @@ mod receipt_storage_adapter_unit_test { #[case(vec![1, 2, 3, 3, 4, 5], 3, vec![1, 2])] #[case(vec![1, 2, 3, 4, 4, 4], 3, vec![1, 2, 3])] #[case(vec![1, 1, 1, 1, 2, 3], 3, vec![])] - #[tokio::test] - async fn safe_truncate_receipts_test( + #[test] + fn safe_truncate_receipts_test( domain_separator: Eip712Domain, #[case] input: Vec, #[case] limit: u64, @@ -215,7 +213,6 @@ mod receipt_storage_adapter_unit_test { }, &wallet, ) - .await .unwrap(), i as u64, // Will use that to check the IDs &get_full_list_of_checks(), diff --git a/tap_core/src/eip_712_signed_message.rs b/tap_core/src/eip_712_signed_message.rs index 4d050ab0..b68c4aa2 100644 --- a/tap_core/src/eip_712_signed_message.rs +++ b/tap_core/src/eip_712_signed_message.rs @@ -21,7 +21,7 @@ pub struct EIP712SignedMessage { impl EIP712SignedMessage { /// creates signed message with signed EIP712 hash of `message` using `signing_wallet` - pub async fn new( + pub fn new( domain_separator: &Eip712Domain, message: M, signing_wallet: &LocalWallet, diff --git a/tap_core/src/lib.rs b/tap_core/src/lib.rs index 3f88c94a..fd61aabc 100644 --- a/tap_core/src/lib.rs +++ b/tap_core/src/lib.rs @@ -88,8 +88,8 @@ mod tap_tests { #[rstest] #[case::basic_rav_test (vec![45,56,34,23])] #[case::rav_from_zero_valued_receipts (vec![0,0,0,0])] - #[tokio::test] - async fn signed_rav_is_valid_with_no_previous_rav( + #[test] + fn signed_rav_is_valid_with_no_previous_rav( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -104,7 +104,6 @@ mod tap_tests { Receipt::new(allocation_ids[0], value).unwrap(), &keys.0, ) - .await .unwrap(), ); } @@ -113,17 +112,15 @@ mod tap_tests { let rav = ReceiptAggregateVoucher::aggregate_receipts(allocation_ids[0], &receipts, None) .unwrap(); - let signed_rav = EIP712SignedMessage::new(&domain_separator, rav, &keys.0) - .await - .unwrap(); + let signed_rav = EIP712SignedMessage::new(&domain_separator, rav, &keys.0).unwrap(); assert!(signed_rav.recover_signer(&domain_separator).unwrap() == keys.1); } #[rstest] #[case::basic_rav_test(vec![45,56,34,23])] #[case::rav_from_zero_valued_receipts(vec![0,0,0,0])] - #[tokio::test] - async fn signed_rav_is_valid_with_previous_rav( + #[test] + fn signed_rav_is_valid_with_previous_rav( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -138,7 +135,6 @@ mod tap_tests { Receipt::new(allocation_ids[0], value).unwrap(), &keys.0, ) - .await .unwrap(), ); } @@ -150,9 +146,8 @@ mod tap_tests { None, ) .unwrap(); - let signed_prev_rav = EIP712SignedMessage::new(&domain_separator, prev_rav, &keys.0) - .await - .unwrap(); + let signed_prev_rav = + EIP712SignedMessage::new(&domain_separator, prev_rav, &keys.0).unwrap(); // Create new RAV from last half of receipts and prev_rav let rav = ReceiptAggregateVoucher::aggregate_receipts( @@ -161,16 +156,14 @@ mod tap_tests { Some(signed_prev_rav), ) .unwrap(); - let signed_rav = EIP712SignedMessage::new(&domain_separator, rav, &keys.0) - .await - .unwrap(); + let signed_rav = EIP712SignedMessage::new(&domain_separator, rav, &keys.0).unwrap(); assert!(signed_rav.recover_signer(&domain_separator).unwrap() == keys.1); } #[rstest] - #[tokio::test] - async fn verify_signature( + #[test] + fn verify_signature( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -180,7 +173,6 @@ mod tap_tests { Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) - .await .unwrap(); assert!(signed_message.verify(&domain_separator, keys.1).is_ok()); diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index 7e63ebad..dbc94534 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -118,7 +118,7 @@ mod manager_unit_test { #[rstest] #[case::full_checks(get_full_list_of_checks())] - #[case::partial_checks(todo!())] + // #[case::partial_checks(todo!())] #[case::no_checks(Vec::::new())] #[tokio::test] async fn manager_verify_and_store_varying_initial_checks( @@ -146,7 +146,6 @@ mod manager_unit_test { Receipt::new(allocation_ids[0], value).unwrap(), &keys.0, ) - .await .unwrap(); query_appraisal_storage .write() @@ -192,7 +191,6 @@ mod manager_unit_test { Receipt::new(allocation_ids[0], value).unwrap(), &keys.0, ) - .await .unwrap(); stored_signed_receipts.push(signed_receipt.clone()); query_appraisal_storage @@ -218,12 +216,15 @@ mod manager_unit_test { let signed_rav = EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) - .await .unwrap(); - // assert!(manager - // .verify_and_store_rav(rav_request.expected_rav, signed_rav) - // .await - // .is_ok()); + assert!(manager + .verify_and_store_rav( + rav_request.expected_rav, + signed_rav, + |address: Address| async move { Ok(keys.1 == address) } + ) + .await + .is_ok()); } #[rstest] @@ -260,7 +261,6 @@ mod manager_unit_test { Receipt::new(allocation_ids[0], value).unwrap(), &keys.0, ) - .await .unwrap(); stored_signed_receipts.push(signed_receipt.clone()); query_appraisal_storage @@ -294,12 +294,15 @@ mod manager_unit_test { let signed_rav = EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) - .await .unwrap(); - // assert!(manager - // .verify_and_store_rav(rav_request.expected_rav, signed_rav) - // .await - // .is_ok()); + assert!(manager + .verify_and_store_rav( + rav_request.expected_rav, + signed_rav, + |address: Address| async move { Ok(keys.1 == address) } + ) + .await + .is_ok()); stored_signed_receipts.clear(); for query_id in 10..20 { @@ -309,7 +312,6 @@ mod manager_unit_test { Receipt::new(allocation_ids[0], value).unwrap(), &keys.0, ) - .await .unwrap(); stored_signed_receipts.push(signed_receipt.clone()); query_appraisal_storage @@ -343,12 +345,15 @@ mod manager_unit_test { let signed_rav = EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) - .await .unwrap(); - // assert!(manager - // .verify_and_store_rav(rav_request.expected_rav, signed_rav) - // .await - // .is_ok()); + assert!(manager + .verify_and_store_rav( + rav_request.expected_rav, + signed_rav, + |address: Address| async move { Ok(keys.1 == address) } + ) + .await + .is_ok()); } #[rstest] @@ -381,9 +386,8 @@ mod manager_unit_test { let value = 20u128; let mut receipt = Receipt::new(allocation_ids[0], value).unwrap(); receipt.timestamp_ns = starting_min_timestamp + query_id + 1; - let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &keys.0) - .await - .unwrap(); + let signed_receipt = + EIP712SignedMessage::new(&domain_separator, receipt, &keys.0).unwrap(); stored_signed_receipts.push(signed_receipt.clone()); query_appraisal_storage .write() @@ -426,21 +430,23 @@ mod manager_unit_test { rav_request_1.expected_rav.clone(), &keys.0, ) - .await .unwrap(); - // assert!(manager - // .verify_and_store_rav(rav_request_1.expected_rav, signed_rav_1) - // .await - // .is_ok()); + assert!(manager + .verify_and_store_rav( + rav_request_1.expected_rav, + signed_rav_1, + |address: Address| async move { Ok(keys.1 == address) } + ) + .await + .is_ok()); stored_signed_receipts.clear(); for query_id in 10..20 { let value = 20u128; let mut receipt = Receipt::new(allocation_ids[0], value).unwrap(); receipt.timestamp_ns = starting_min_timestamp + query_id + 1; - let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &keys.0) - .await - .unwrap(); + let signed_receipt = + EIP712SignedMessage::new(&domain_separator, receipt, &keys.0).unwrap(); stored_signed_receipts.push(signed_receipt.clone()); query_appraisal_storage .write() @@ -492,11 +498,14 @@ mod manager_unit_test { rav_request_2.expected_rav.clone(), &keys.0, ) - .await .unwrap(); - // assert!(manager - // .verify_and_store_rav(rav_request_2.expected_rav, signed_rav_2) - // .await - // .is_ok()); + assert!(manager + .verify_and_store_rav( + rav_request_2.expected_rav, + signed_rav_2, + |address: Address| async move { Ok(keys.1 == address) } + ) + .await + .is_ok()); } } diff --git a/tap_core/src/tap_receipt/tests/received_receipt_tests.rs b/tap_core/src/tap_receipt/tests/received_receipt_tests.rs index 13b016fd..5cd732ef 100644 --- a/tap_core/src/tap_receipt/tests/received_receipt_tests.rs +++ b/tap_core/src/tap_receipt/tests/received_receipt_tests.rs @@ -136,7 +136,6 @@ mod received_receipt_unit_test { Receipt::new(allocation_ids[0], 10).unwrap(), &keys.0, ) - .await .unwrap(); let query_id = 1; let checks = get_full_list_of_checks(); @@ -171,7 +170,6 @@ mod received_receipt_unit_test { Receipt::new(allocation_ids[0], query_value).unwrap(), &keys.0, ) - .await .unwrap(); let query_id = 1; @@ -229,7 +227,6 @@ mod received_receipt_unit_test { Receipt::new(allocation_ids[0], query_value).unwrap(), &keys.0, ) - .await .unwrap(); let query_id = 1; @@ -264,10 +261,7 @@ mod received_receipt_unit_test { // .await; // assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); - assert!(received_receipt - .finalize_receipt_checks() - .await - .is_ok()); + assert!(received_receipt.finalize_receipt_checks().await.is_ok()); } #[rstest] @@ -289,7 +283,6 @@ mod received_receipt_unit_test { Receipt::new(allocation_ids[0], query_value).unwrap(), &keys.0, ) - .await .unwrap(); let query_id = 1; @@ -316,9 +309,6 @@ mod received_receipt_unit_test { _ => panic!("ReceivedReceipt should be in Checking state"), }; - assert!(received_receipt - .finalize_receipt_checks() - .await - .is_ok()); + assert!(received_receipt.finalize_receipt_checks().await.is_ok()); } } diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 79fd2eb5..18825dae 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -264,7 +264,7 @@ fn indexer_2_adapters(executor: ExecutorMock) -> ExecutorMock { // Helper fixture to generate a batch of receipts to be sent to the Indexer. // Messages are formatted according to TAP spec and signed according to EIP-712. #[fixture] -async fn requests_1( +fn requests_1( keys_sender: (LocalWallet, Address), query_price: Vec, num_batches: u64, @@ -279,13 +279,12 @@ async fn requests_1( &sender_key, allocation_ids[0], &domain_separator, - ) - .await?; + )?; Ok(requests) } #[fixture] -async fn requests_2( +fn requests_2( keys_sender: (LocalWallet, Address), query_price: Vec, num_batches: u64, @@ -300,13 +299,12 @@ async fn requests_2( &sender_key, allocation_ids[1], &domain_separator, - ) - .await?; + )?; Ok(requests) } #[fixture] -async fn repeated_timestamp_request( +fn repeated_timestamp_request( keys_sender: (LocalWallet, Address), query_price: Vec, allocation_ids: Vec
, @@ -323,8 +321,7 @@ async fn repeated_timestamp_request( &sender_key, allocation_ids[0], &domain_separator, - ) - .await?; + )?; // Create a new receipt with the timestamp equal to the latest receipt in the first RAV request batch let repeat_timestamp = requests[receipt_threshold_1 as usize - 1] @@ -341,12 +338,12 @@ async fn repeated_timestamp_request( // Sign the new receipt and insert it in the second batch requests[receipt_threshold_1 as usize].0 = - EIP712SignedMessage::new(&domain_separator, repeat_receipt, &sender_key).await?; + EIP712SignedMessage::new(&domain_separator, repeat_receipt, &sender_key)?; Ok(requests) } #[fixture] -async fn repeated_timestamp_incremented_by_one_request( +fn repeated_timestamp_incremented_by_one_request( keys_sender: (LocalWallet, Address), query_price: Vec, allocation_ids: Vec
, @@ -362,8 +359,7 @@ async fn repeated_timestamp_incremented_by_one_request( &sender_key, allocation_ids[0], &domain_separator, - ) - .await?; + )?; // Create a new receipt with the timestamp equal to the latest receipt timestamp+1 in the first RAV request batch let repeat_timestamp = requests[receipt_threshold_1 as usize - 1] @@ -381,12 +377,12 @@ async fn repeated_timestamp_incremented_by_one_request( // Sign the new receipt and insert it in the second batch requests[receipt_threshold_1 as usize].0 = - EIP712SignedMessage::new(&domain_separator, repeat_receipt, &sender_key).await?; + EIP712SignedMessage::new(&domain_separator, repeat_receipt, &sender_key)?; Ok(requests) } #[fixture] -async fn wrong_requests( +fn wrong_requests( wrong_keys_sender: (LocalWallet, Address), query_price: Vec, num_batches: u64, @@ -402,8 +398,7 @@ async fn wrong_requests( &sender_key, allocation_ids[0], &domain_separator, - ) - .await?; + )?; Ok(requests) } @@ -568,13 +563,13 @@ async fn test_manager_one_indexer( (ServerHandle, SocketAddr, ServerHandle, SocketAddr), Error, >, - #[future] requests_1: Result, u64)>>, + requests_1: Result, u64)>>, ) -> Result<(), Box> { let (_server_handle, socket_addr, _sender_handle, _sender_addr) = single_indexer_test_server.await?; let indexer_1_address = "http://".to_string() + &socket_addr.to_string(); let client_1 = HttpClientBuilder::default().build(indexer_1_address)?; - let requests = requests_1.await?; + let requests = requests_1?; for (receipt_1, id) in requests { let result = client_1.request("request", (id, receipt_1)).await; @@ -602,8 +597,8 @@ async fn test_manager_two_indexers( ), Error, >, - #[future] requests_1: Result, u64)>>, - #[future] requests_2: Result, u64)>>, + requests_1: Result, u64)>>, + requests_2: Result, u64)>>, ) -> Result<()> { let ( _server_handle_1, @@ -618,8 +613,8 @@ async fn test_manager_two_indexers( let indexer_2_address = "http://".to_string() + &socket_addr_2.to_string(); let client_1 = HttpClientBuilder::default().build(indexer_1_address)?; let client_2 = HttpClientBuilder::default().build(indexer_2_address)?; - let requests_1 = requests_1.await?; - let requests_2 = requests_2.await?; + let requests_1 = requests_1?; + let requests_2 = requests_2?; for ((receipt_1, id_1), (receipt_2, id_2)) in requests_1.iter().zip(requests_2) { let future_1 = client_1.request("request", (id_1, receipt_1)); @@ -639,14 +634,14 @@ async fn test_manager_wrong_aggregator_keys( (ServerHandle, SocketAddr, ServerHandle, SocketAddr), Error, >, - #[future] requests_1: Result, u64)>>, + requests_1: Result, u64)>>, receipt_threshold_1: u64, ) -> Result<()> { let (_server_handle, socket_addr, _sender_handle, _sender_addr) = single_indexer_wrong_sender_test_server.await?; let indexer_1_address = "http://".to_string() + &socket_addr.to_string(); let client_1 = HttpClientBuilder::default().build(indexer_1_address)?; - let requests = requests_1.await?; + let requests = requests_1?; let mut counter = 1; for (receipt_1, id) in requests { @@ -682,14 +677,14 @@ async fn test_manager_wrong_requestor_keys( (ServerHandle, SocketAddr, ServerHandle, SocketAddr), Error, >, - #[future] wrong_requests: Result, u64)>>, + wrong_requests: Result, u64)>>, receipt_threshold_1: u64, ) -> Result<()> { let (_server_handle, socket_addr, _sender_handle, _sender_addr) = single_indexer_test_server.await?; let indexer_1_address = "http://".to_string() + &socket_addr.to_string(); let client_1 = HttpClientBuilder::default().build(indexer_1_address)?; - let requests = wrong_requests.await?; + let requests = wrong_requests?; let mut counter = 1; for (receipt_1, id) in requests { @@ -728,10 +723,8 @@ async fn test_tap_manager_rav_timestamp_cuttoff( ), Error, >, - #[future] repeated_timestamp_request: Result, u64)>>, - #[future] repeated_timestamp_incremented_by_one_request: Result< - Vec<(EIP712SignedMessage, u64)>, - >, + repeated_timestamp_request: Result, u64)>>, + repeated_timestamp_incremented_by_one_request: Result, u64)>>, receipt_threshold_1: u64, ) -> Result<(), Box> { // This test checks that tap_core is correctly filtering receipts by timestamp. @@ -748,7 +741,7 @@ async fn test_tap_manager_rav_timestamp_cuttoff( let indexer_2_address = "http://".to_string() + &socket_addr_2.to_string(); let client_1 = HttpClientBuilder::default().build(indexer_1_address)?; let client_2 = HttpClientBuilder::default().build(indexer_2_address)?; - let requests = repeated_timestamp_request.await?; + let requests = repeated_timestamp_request?; let mut counter = 1; for (receipt_1, id) in requests { @@ -775,7 +768,7 @@ async fn test_tap_manager_rav_timestamp_cuttoff( // Here the timestamp first receipt in the second batch is equal to timestamp + 1 of the last receipt in the first batch. // No errors are expected. - let requests = repeated_timestamp_incremented_by_one_request.await?; + let requests = repeated_timestamp_incremented_by_one_request?; for (receipt_1, id) in requests { let result = client_2.request("request", (id, receipt_1)).await; match result { @@ -794,10 +787,8 @@ async fn test_tap_aggregator_rav_timestamp_cuttoff( http_request_size_limit: u32, http_response_size_limit: u32, http_max_concurrent_connections: u32, - #[future] repeated_timestamp_request: Result, u64)>>, - #[future] repeated_timestamp_incremented_by_one_request: Result< - Vec<(EIP712SignedMessage, u64)>, - >, + repeated_timestamp_request: Result, u64)>>, + repeated_timestamp_incremented_by_one_request: Result, u64)>>, receipt_threshold_1: u64, ) -> Result<(), Box> { // This test checks that tap_aggregator is correctly rejecting receipts with invalid timestamps @@ -815,7 +806,7 @@ async fn test_tap_aggregator_rav_timestamp_cuttoff( // The second batch has one receipt with the same timestamp as the latest receipt in the first batch. // The first RAV will have the same timestamp as one receipt in the second batch. // tap_aggregator should reject the second RAV request due to the repeated timestamp. - let requests = repeated_timestamp_request.await?; + let requests = repeated_timestamp_request?; let first_batch = &requests[0..receipt_threshold_1 as usize]; let second_batch = &requests[receipt_threshold_1 as usize..2 * receipt_threshold_1 as usize]; @@ -848,7 +839,7 @@ async fn test_tap_aggregator_rav_timestamp_cuttoff( // This is the second part of the test, two batches of receipts are sent to the aggregator. // The second batch has one receipt with the timestamp = timestamp+1 of the latest receipt in the first batch. // tap_aggregator should accept the second RAV request. - let requests = repeated_timestamp_incremented_by_one_request.await?; + let requests = repeated_timestamp_incremented_by_one_request?; let first_batch = &requests[0..receipt_threshold_1 as usize]; let second_batch = &requests[receipt_threshold_1 as usize..2 * receipt_threshold_1 as usize]; @@ -883,7 +874,7 @@ async fn test_tap_aggregator_rav_timestamp_cuttoff( Ok(()) } -async fn generate_requests( +fn generate_requests( query_price: Vec, num_batches: u64, sender_key: &LocalWallet, @@ -900,8 +891,7 @@ async fn generate_requests( domain_separator, Receipt::new(allocation_id, *value)?, sender_key, - ) - .await?, + )?, counter, )); counter += 1; From a970a4c88335b8b41083a23e9196cf0d2eba9884 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 15:37:11 -0300 Subject: [PATCH 11/32] test: add sender_id to indexer_mock Signed-off-by: Gustavo Inacio --- .../tests/indexer_mock/mod.rs | 19 +++++++++++++++---- tap_integration_tests/tests/showcase.rs | 1 + 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/tap_integration_tests/tests/indexer_mock/mod.rs b/tap_integration_tests/tests/indexer_mock/mod.rs index 20f7af4e..b7cc34f0 100644 --- a/tap_integration_tests/tests/indexer_mock/mod.rs +++ b/tap_integration_tests/tests/indexer_mock/mod.rs @@ -8,6 +8,7 @@ use std::{ time::{SystemTime, UNIX_EPOCH}, }; +use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; use anyhow::{Error, Result}; use jsonrpsee::{ @@ -55,6 +56,7 @@ pub struct RpcManager { receipt_count: Arc, // Thread-safe atomic counter for receipts threshold: u64, // The count at which a RAV request will be triggered aggregator_client: (HttpClient, String), // HTTP client for sending requests to the aggregator server + sender_id: Address, // The sender address } /// Implementation for `RpcManager`, includes the constructor and the `request` method. @@ -70,6 +72,7 @@ where initial_checks: Vec, required_checks: Vec, threshold: u64, + sender_id: Address, aggregate_server_address: String, aggregate_server_api_version: String, ) -> Result { @@ -83,6 +86,7 @@ where initial_checks, receipt_count: Arc::new(AtomicU64::new(0)), threshold, + sender_id, aggregator_client: ( HttpClientBuilder::default().build(aggregate_server_address)?, aggregate_server_api_version, @@ -126,6 +130,7 @@ where time_stamp_buffer, &self.aggregator_client, self.threshold as usize, + self.sender_id, ) .await { @@ -154,6 +159,7 @@ pub async fn run_server( threshold: u64, // The count at which a RAV request will be triggered aggregate_server_address: String, // Address of the aggregator server aggregate_server_api_version: String, // API version of the aggregator server + sender_id: Address, // The sender address ) -> Result<(ServerHandle, std::net::SocketAddr)> where E: ReceiptStore @@ -180,6 +186,7 @@ where initial_checks, required_checks, threshold, + sender_id, aggregate_server_address, aggregate_server_api_version, )?; @@ -194,6 +201,7 @@ async fn request_rav( time_stamp_buffer: u64, // Buffer for timestamping, see tap_core for details aggregator_client: &(HttpClient, String), // HttpClient for making requests to the tap_aggregator server threshold: usize, + expected_sender_id: Address, ) -> Result<()> where E: ReceiptRead + RAVRead + RAVStore + EscrowAdapter, @@ -213,10 +221,13 @@ where .0 .request("aggregate_receipts", params) .await?; - // TODO update this with the async function - // manager - // .verify_and_store_rav(rav_request.expected_rav, remote_rav_result.data) - // .await?; + manager + .verify_and_store_rav( + rav_request.expected_rav, + remote_rav_result.data, + |address| async move { Ok(address == expected_sender_id) }, + ) + .await?; // For these tests, we expect every receipt to be valid, i.e. there should be no invalid receipts, nor any missing receipts (less than the expected threshold). // If there is throw an error. diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 18825dae..6a4d5e15 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -930,6 +930,7 @@ async fn start_indexer_server( receipt_threshold, aggregate_server_address, aggregate_server_api_version(), + sender_id, ) .await?; From ab0ddb57171abd65f1fe5cafe60fbaba3c0e3413 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 15:53:25 -0300 Subject: [PATCH 12/32] refactor: remove checks mock adapter, fix warnings, update tests Signed-off-by: Gustavo Inacio --- .../test/receipt_checks_adapter_test.rs | 19 +- tap_core/src/tap_manager/test/manager_test.rs | 9 +- tap_core/src/tap_receipt/mod.rs | 6 +- tap_core/src/tap_receipt/receipt.rs | 61 ++++ tap_core/src/tap_receipt/received_receipt.rs | 28 +- .../received_receipt_unit_test.rs | 315 ++++++++++++++++++ tap_core/src/tap_receipt/tests/mod.rs | 5 - .../src/tap_receipt/tests/receipt_tests.rs | 66 ---- .../tests/received_receipt_tests.rs | 314 ----------------- tap_integration_tests/tests/showcase.rs | 28 +- 10 files changed, 394 insertions(+), 457 deletions(-) create mode 100644 tap_core/src/tap_receipt/received_receipt/received_receipt_unit_test.rs delete mode 100644 tap_core/src/tap_receipt/tests/mod.rs delete mode 100644 tap_core/src/tap_receipt/tests/receipt_tests.rs delete mode 100644 tap_core/src/tap_receipt/tests/received_receipt_tests.rs diff --git a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs index 86f6f532..c59545ac 100644 --- a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs @@ -17,7 +17,6 @@ mod receipt_checks_adapter_unit_test { use tokio::sync::RwLock; use crate::{ - adapters::receipt_checks_adapter_mock::ReceiptChecksAdapterMock, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, tap_receipt::{get_full_list_of_checks, Receipt, ReceivedReceipt}, @@ -36,14 +35,14 @@ mod receipt_checks_adapter_unit_test { Address::from_str("0xfafafafafafafafafafafafafafafafafafafafa").unwrap(), Address::from_str("0xadadadadadadadadadadadadadadadadadadadad").unwrap(), ]; - let sender_ids_set = Arc::new(RwLock::new(HashSet::from(sender_ids))); + let _sender_ids_set = Arc::new(RwLock::new(HashSet::from(sender_ids))); let allocation_ids = [ Address::from_str("0xabababababababababababababababababababab").unwrap(), Address::from_str("0xbabababababababababababababababababababa").unwrap(), Address::from_str("0xdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdf").unwrap(), ]; - let allocation_ids_set = Arc::new(RwLock::new(HashSet::from(allocation_ids))); + let _allocation_ids_set = Arc::new(RwLock::new(HashSet::from(allocation_ids))); let wallet: LocalWallet = MnemonicBuilder::::default() .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") @@ -77,14 +76,14 @@ mod receipt_checks_adapter_unit_test { let query_appraisals = (0..11).map(|id| (id, 100u128)).collect::>(); - let query_appraisals_storage = Arc::new(RwLock::new(query_appraisals)); + let _query_appraisals_storage = Arc::new(RwLock::new(query_appraisals)); - let receipt_checks_adapter = ReceiptChecksAdapterMock::new( - Arc::clone(&receipt_storage), - query_appraisals_storage, - allocation_ids_set, - sender_ids_set, - ); + // let receipt_checks_adapter = ReceiptChecksAdapterMock::new( + // Arc::clone(&receipt_storage), + // query_appraisals_storage, + // allocation_ids_set, + // sender_ids_set, + // ); let new_receipt = ( 10u64, diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index dbc94534..b39e0955 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -3,11 +3,7 @@ #[cfg(test)] #[allow(clippy::too_many_arguments)] mod manager_unit_test { - use std::{ - collections::{HashMap, HashSet}, - str::FromStr, - sync::Arc, - }; + use std::{collections::HashMap, str::FromStr, sync::Arc}; use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; @@ -20,7 +16,8 @@ mod manager_unit_test { adapters::{ escrow_adapter_mock::EscrowAdapterMock, executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, - receipt_storage_adapter::ReceiptRead, receipt_storage_adapter_mock::ReceiptStorageAdapterMock, + receipt_storage_adapter::ReceiptRead, + receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }, checks::ReceiptCheck, eip_712_signed_message::EIP712SignedMessage, diff --git a/tap_core/src/tap_receipt/mod.rs b/tap_core/src/tap_receipt/mod.rs index f431019a..31910031 100644 --- a/tap_core/src/tap_receipt/mod.rs +++ b/tap_core/src/tap_receipt/mod.rs @@ -4,7 +4,7 @@ mod receipt; mod receipt_auditor; mod received_receipt; -use std::{collections::HashMap, sync::{Arc, RwLock}}; +use std::collections::HashMap; use alloy_primitives::Address; pub use receipt::Receipt; @@ -15,7 +15,6 @@ pub use received_receipt::{ }; use serde::{Deserialize, Serialize}; -use strum_macros::{Display, EnumString}; use thiserror::Error; use crate::checks::{CheckingChecks, ReceiptCheck}; @@ -72,6 +71,3 @@ pub fn get_full_list_of_checks() -> Vec { // ReceiptCheck::CheckSignature, ] } - -#[cfg(test)] -pub mod tests; diff --git a/tap_core/src/tap_receipt/receipt.rs b/tap_core/src/tap_receipt/receipt.rs index 43c1317c..630d337b 100644 --- a/tap_core/src/tap_receipt/receipt.rs +++ b/tap_core/src/tap_receipt/receipt.rs @@ -41,3 +41,64 @@ impl Receipt { }) } } + +#[cfg(test)] +mod receipt_unit_test { + use super::*; + use rstest::*; + use std::str::FromStr; + use std::time::{SystemTime, UNIX_EPOCH}; + + #[fixture] + fn allocation_ids() -> Vec
{ + vec![ + Address::from_str("0xabababababababababababababababababababab").unwrap(), + Address::from_str("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddead").unwrap(), + Address::from_str("0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef").unwrap(), + Address::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(), + ] + } + + #[rstest] + fn test_new_receipt(allocation_ids: Vec
) { + let value = 1234; + + let receipt = Receipt::new(allocation_ids[0], value).unwrap(); + + assert_eq!(receipt.allocation_id, allocation_ids[0]); + assert_eq!(receipt.value, value); + + // Check that the timestamp is within a reasonable range + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Current system time should be greater than `UNIX_EPOCH`") + .as_nanos() as u64; + assert!(receipt.timestamp_ns <= now); + assert!(receipt.timestamp_ns >= now - 5000000); // 5 second tolerance + } + + #[rstest] + fn test_unique_nonce_and_timestamp(allocation_ids: Vec
) { + let value = 1234; + + let receipt1 = Receipt::new(allocation_ids[0], value).unwrap(); + let receipt2 = Receipt::new(allocation_ids[0], value).unwrap(); + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Current system time should be greater than `UNIX_EPOCH`") + .as_nanos() as u64; + + // Check that nonces are different + // Note: This test has an *extremely low* (~1/2^64) probability of false failure, if a failure happens + // once it is not neccessarily a sign of an issue. If this test fails more than once, especially + // in a short period of time (within a ) then there may be an issue with randomness + // of the nonce generation. + assert_ne!(receipt1.nonce, receipt2.nonce); + + assert!(receipt1.timestamp_ns <= now); + assert!(receipt1.timestamp_ns >= now - 5000000); // 5 second tolerance + + assert!(receipt2.timestamp_ns <= now); + assert!(receipt2.timestamp_ns >= now - 5000000); // 5 second tolerance + } +} diff --git a/tap_core/src/tap_receipt/received_receipt.rs b/tap_core/src/tap_receipt/received_receipt.rs index 84527409..51cd8427 100644 --- a/tap_core/src/tap_receipt/received_receipt.rs +++ b/tap_core/src/tap_receipt/received_receipt.rs @@ -13,7 +13,7 @@ //! This module is useful for managing and tracking the state of received receipts, as well as //! their progress through various checks and stages of inclusion in RAV requests and received RAVs. -use std::{collections::HashMap, sync::{Arc, RwLock}}; +use std::collections::HashMap; use serde::{Deserialize, Serialize}; @@ -80,8 +80,7 @@ pub struct ReceiptWithId where T: ReceiptState, { - #[allow(dead_code)] - pub(crate) receipt_id: u64, + pub receipt_id: u64, pub(crate) receipt: ReceiptWithState, } @@ -288,20 +287,6 @@ impl ReceiptWithState { } } - /// Returns all checks that completed with errors - pub(crate) fn completed_checks_with_errors(&self) -> ReceiptCheckResults { - self.state - .checks - .iter() - .filter_map(|(check, result)| { - if result.is_failed() { - return Some((*check, result.clone())); - } - None - }) - .collect() - } - /// Returns all checks that have not been completed pub(crate) fn incomplete_checks(&self) -> Vec<&'static str> { let incomplete_checks = self @@ -325,13 +310,6 @@ impl ReceiptWithState { .any(|(_, status)| status.is_failed()) } - pub fn checking_is_complete(&self) -> bool { - self.state - .checks - .iter() - .all(|(_, status)| status.is_complete()) - } - fn get_empty_required_checks_hashmap(required_checks: &[ReceiptCheck]) -> ReceiptCheckResults { required_checks .iter() @@ -375,3 +353,5 @@ where self.query_id } } +#[cfg(test)] +pub mod received_receipt_unit_test; 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 new file mode 100644 index 00000000..1ee37320 --- /dev/null +++ b/tap_core/src/tap_receipt/received_receipt/received_receipt_unit_test.rs @@ -0,0 +1,315 @@ +use std::{ + collections::{HashMap, HashSet}, + str::FromStr, + sync::Arc, +}; + +use alloy_primitives::Address; +use alloy_sol_types::Eip712Domain; +use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer}; +use rstest::*; +use tokio::sync::RwLock; + +use crate::{ + adapters::{ + auditor_executor_mock::AuditorExecutorMock, + escrow_adapter_mock::EscrowAdapterMock, + executor_mock::{EscrowStorage, QueryAppraisals}, + receipt_storage_adapter_mock::ReceiptStorageAdapterMock, + }, + eip_712_signed_message::EIP712SignedMessage, + get_current_timestamp_u64_ns, tap_eip712_domain, + tap_receipt::{ + get_full_list_of_checks, Receipt, ReceiptAuditor, ReceiptCheckResults, ReceivedReceipt, + }, +}; + +use super::{Checking, ReceiptWithState}; + +impl ReceiptWithState { + fn checking_is_complete(&self) -> bool { + self.state + .checks + .iter() + .all(|(_, status)| status.is_complete()) + } + /// Returns all checks that completed with errors + fn completed_checks_with_errors(&self) -> ReceiptCheckResults { + self.state + .checks + .iter() + .filter_map(|(check, result)| { + if result.is_failed() { + return Some((*check, result.clone())); + } + None + }) + .collect() + } +} + +#[fixture] +fn keys() -> (LocalWallet, Address) { + let wallet: LocalWallet = MnemonicBuilder::::default() + .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") + .build() + .unwrap(); + // Alloy library does not have feature parity with ethers library (yet) This workaround is needed to get the address + // to convert to an alloy Address. This will not be needed when the alloy library has wallet support. + let address: [u8; 20] = wallet.address().into(); + + (wallet, address.into()) +} + +#[fixture] +fn allocation_ids() -> Vec
{ + vec![ + Address::from_str("0xabababababababababababababababababababab").unwrap(), + Address::from_str("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddead").unwrap(), + Address::from_str("0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef").unwrap(), + Address::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(), + ] +} + +#[fixture] +fn sender_ids() -> Vec
{ + vec![ + Address::from_str("0xfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb").unwrap(), + Address::from_str("0xfafafafafafafafafafafafafafafafafafafafa").unwrap(), + Address::from_str("0xadadadadadadadadadadadadadadadadadadadad").unwrap(), + keys().1, + ] +} + +#[fixture] +fn receipt_adapters() -> (ReceiptStorageAdapterMock, Arc>>) { + let receipt_storage = Arc::new(RwLock::new(HashMap::new())); + let receipt_storage_adapter = ReceiptStorageAdapterMock::new(Arc::clone(&receipt_storage)); + + let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); + + (receipt_storage_adapter, query_appraisal_storage) +} + +#[fixture] +fn escrow_adapters() -> (EscrowAdapterMock, Arc>>) { + let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); + let escrow_adapter = EscrowAdapterMock::new(Arc::clone(&sender_escrow_storage)); + (escrow_adapter, sender_escrow_storage) +} + +#[fixture] +fn auditor_executor() -> (AuditorExecutorMock, EscrowStorage, QueryAppraisals) { + let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); + + let receipt_storage = Arc::new(RwLock::new(HashMap::new())); + + let allocation_ids_set = Arc::new(RwLock::new(HashSet::from_iter(allocation_ids()))); + let sender_ids_set = Arc::new(RwLock::new(HashSet::from_iter(sender_ids()))); + let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); + ( + AuditorExecutorMock::new( + receipt_storage, + sender_escrow_storage.clone(), + query_appraisal_storage.clone(), + allocation_ids_set, + sender_ids_set, + ), + sender_escrow_storage, + query_appraisal_storage, + ) +} + +#[fixture] +fn domain_separator() -> Eip712Domain { + tap_eip712_domain(1, Address::from([0x11u8; 20])) +} + +#[rstest] +#[tokio::test] +async fn initialization_valid_receipt( + keys: (LocalWallet, Address), + allocation_ids: Vec
, + domain_separator: Eip712Domain, +) { + let signed_receipt = EIP712SignedMessage::new( + &domain_separator, + Receipt::new(allocation_ids[0], 10).unwrap(), + &keys.0, + ) + .unwrap(); + let query_id = 1; + let checks = get_full_list_of_checks(); + + let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); + + let received_receipt = match received_receipt { + ReceivedReceipt::Checking(checking) => checking, + _ => panic!("ReceivedReceipt should be in Checking state"), + }; + + assert!(received_receipt.completed_checks_with_errors().is_empty()); + assert!(received_receipt.incomplete_checks().len() == checks.len()); +} + +#[rstest] +#[tokio::test] +async fn partial_then_full_check_valid_receipt( + keys: (LocalWallet, Address), + domain_separator: Eip712Domain, + allocation_ids: Vec
, + auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), +) { + let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; + // 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 query_value = 20u128; + let signed_receipt = EIP712SignedMessage::new( + &domain_separator, + Receipt::new(allocation_ids[0], query_value).unwrap(), + &keys.0, + ) + .unwrap(); + + let query_id = 1; + + // prepare adapters and storage to correctly validate receipt + + // add escrow for sender + escrow_storage + .write() + .await + .insert(keys.1, query_value + 500); + // appraise query + query_appraisal_storage + .write() + .await + .insert(query_id, query_value); + + let checks = get_full_list_of_checks(); + let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); + let _receipt_id = 0u64; + + let received_receipt = match received_receipt { + ReceivedReceipt::Checking(checking) => checking, + _ => panic!("ReceivedReceipt should be in Checking state"), + }; + + // perform single arbitrary check + // let arbitrary_check_to_perform = ReceiptCheck::CheckUnique; + // received_receipt + // .perform_check(&arbitrary_check_to_perform, receipt_id, &receipt_auditor) + // .await; + // assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); + // + // received_receipt + // .perform_checks(&checks, receipt_id, &receipt_auditor) + // .await; + assert!(received_receipt.checking_is_complete()); +} + +#[rstest] +#[tokio::test] +async fn partial_then_finalize_valid_receipt( + keys: (LocalWallet, Address), + allocation_ids: Vec
, + domain_separator: Eip712Domain, + auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), +) { + let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; + // 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 query_value = 20u128; + let signed_receipt = EIP712SignedMessage::new( + &domain_separator, + Receipt::new(allocation_ids[0], query_value).unwrap(), + &keys.0, + ) + .unwrap(); + + let query_id = 1; + + // prepare adapters and storage to correctly validate receipt + + // add escrow for sender + escrow_storage + .write() + .await + .insert(keys.1, query_value + 500); + // appraise query + query_appraisal_storage + .write() + .await + .insert(query_id, query_value); + + let checks = get_full_list_of_checks(); + let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); + let _receipt_id = 0u64; + + let received_receipt = match received_receipt { + ReceivedReceipt::Checking(checking) => checking, + _ => panic!("ReceivedReceipt should be in Checking state"), + }; + + // perform single arbitrary check + // let arbitrary_check_to_perform = ReceiptCheck::CheckUnique; + + // received_receipt + // .perform_check(/** Check Unique */) + // .await; + // assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); + + assert!(received_receipt.finalize_receipt_checks().await.is_ok()); +} + +#[rstest] +#[tokio::test] +async fn standard_lifetime_valid_receipt( + keys: (LocalWallet, Address), + allocation_ids: Vec
, + domain_separator: Eip712Domain, + auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), +) { + let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; + // 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 query_value = 20u128; + let signed_receipt = EIP712SignedMessage::new( + &domain_separator, + Receipt::new(allocation_ids[0], query_value).unwrap(), + &keys.0, + ) + .unwrap(); + + let query_id = 1; + + // prepare adapters and storage to correctly validate receipt + + // add escrow for sender + escrow_storage + .write() + .await + .insert(keys.1, query_value + 500); + // appraise query + query_appraisal_storage + .write() + .await + .insert(query_id, query_value); + + let checks = get_full_list_of_checks(); + let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); + let _receipt_id = 0u64; + + let received_receipt = match received_receipt { + ReceivedReceipt::Checking(checking) => checking, + _ => panic!("ReceivedReceipt should be in Checking state"), + }; + + assert!(received_receipt.finalize_receipt_checks().await.is_ok()); +} diff --git a/tap_core/src/tap_receipt/tests/mod.rs b/tap_core/src/tap_receipt/tests/mod.rs deleted file mode 100644 index 73889387..00000000 --- a/tap_core/src/tap_receipt/tests/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright 2023-, Semiotic AI, Inc. -// SPDX-License-Identifier: Apache-2.0 - -pub mod receipt_tests; -pub mod received_receipt_tests; diff --git a/tap_core/src/tap_receipt/tests/receipt_tests.rs b/tap_core/src/tap_receipt/tests/receipt_tests.rs deleted file mode 100644 index 1820cdd0..00000000 --- a/tap_core/src/tap_receipt/tests/receipt_tests.rs +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2023-, Semiotic AI, Inc. -// SPDX-License-Identifier: Apache-2.0 - -#[cfg(test)] -mod receipt_unit_test { - use std::str::FromStr; - use std::time::{SystemTime, UNIX_EPOCH}; - - use alloy_primitives::Address; - use rstest::*; - - use crate::tap_receipt::Receipt; - - #[fixture] - fn allocation_ids() -> Vec
{ - vec![ - Address::from_str("0xabababababababababababababababababababab").unwrap(), - Address::from_str("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddead").unwrap(), - Address::from_str("0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef").unwrap(), - Address::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(), - ] - } - - #[rstest] - fn test_new_receipt(allocation_ids: Vec
) { - let value = 1234; - - let receipt = Receipt::new(allocation_ids[0], value).unwrap(); - - assert_eq!(receipt.allocation_id, allocation_ids[0]); - assert_eq!(receipt.value, value); - - // Check that the timestamp is within a reasonable range - let now = SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("Current system time should be greater than `UNIX_EPOCH`") - .as_nanos() as u64; - assert!(receipt.timestamp_ns <= now); - assert!(receipt.timestamp_ns >= now - 5000000); // 5 second tolerance - } - - #[rstest] - fn test_unique_nonce_and_timestamp(allocation_ids: Vec
) { - let value = 1234; - - let receipt1 = Receipt::new(allocation_ids[0], value).unwrap(); - let receipt2 = Receipt::new(allocation_ids[0], value).unwrap(); - let now = SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("Current system time should be greater than `UNIX_EPOCH`") - .as_nanos() as u64; - - // Check that nonces are different - // Note: This test has an *extremely low* (~1/2^64) probability of false failure, if a failure happens - // once it is not neccessarily a sign of an issue. If this test fails more than once, especially - // in a short period of time (within a ) then there may be an issue with randomness - // of the nonce generation. - assert_ne!(receipt1.nonce, receipt2.nonce); - - assert!(receipt1.timestamp_ns <= now); - assert!(receipt1.timestamp_ns >= now - 5000000); // 5 second tolerance - - assert!(receipt2.timestamp_ns <= now); - assert!(receipt2.timestamp_ns >= now - 5000000); // 5 second tolerance - } -} diff --git a/tap_core/src/tap_receipt/tests/received_receipt_tests.rs b/tap_core/src/tap_receipt/tests/received_receipt_tests.rs deleted file mode 100644 index 5cd732ef..00000000 --- a/tap_core/src/tap_receipt/tests/received_receipt_tests.rs +++ /dev/null @@ -1,314 +0,0 @@ -// Copyright 2023-, Semiotic AI, Inc. -// SPDX-License-Identifier: Apache-2.0 - -#[cfg(test)] -mod received_receipt_unit_test { - use std::{ - collections::{HashMap, HashSet}, - str::FromStr, - sync::Arc, - }; - - use alloy_primitives::Address; - use alloy_sol_types::Eip712Domain; - use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer}; - use rstest::*; - use tokio::sync::RwLock; - - use crate::{ - adapters::{ - auditor_executor_mock::AuditorExecutorMock, - escrow_adapter_mock::EscrowAdapterMock, - executor_mock::{EscrowStorage, QueryAppraisals}, - receipt_checks_adapter_mock::ReceiptChecksAdapterMock, - receipt_storage_adapter_mock::ReceiptStorageAdapterMock, - }, - eip_712_signed_message::EIP712SignedMessage, - get_current_timestamp_u64_ns, tap_eip712_domain, - tap_receipt::{ - get_full_list_of_checks, Receipt, ReceiptAuditor, ReceiptCheck, ReceivedReceipt, - }, - }; - - #[fixture] - fn keys() -> (LocalWallet, Address) { - let wallet: LocalWallet = MnemonicBuilder::::default() - .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") - .build() - .unwrap(); - // Alloy library does not have feature parity with ethers library (yet) This workaround is needed to get the address - // to convert to an alloy Address. This will not be needed when the alloy library has wallet support. - let address: [u8; 20] = wallet.address().into(); - - (wallet, address.into()) - } - - #[fixture] - fn allocation_ids() -> Vec
{ - vec![ - Address::from_str("0xabababababababababababababababababababab").unwrap(), - Address::from_str("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddead").unwrap(), - Address::from_str("0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef").unwrap(), - Address::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(), - ] - } - - #[fixture] - fn sender_ids() -> Vec
{ - vec![ - Address::from_str("0xfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb").unwrap(), - Address::from_str("0xfafafafafafafafafafafafafafafafafafafafa").unwrap(), - Address::from_str("0xadadadadadadadadadadadadadadadadadadadad").unwrap(), - keys().1, - ] - } - - #[fixture] - fn receipt_adapters() -> ( - ReceiptStorageAdapterMock, - ReceiptChecksAdapterMock, - Arc>>, - ) { - let receipt_storage = Arc::new(RwLock::new(HashMap::new())); - let receipt_storage_adapter = ReceiptStorageAdapterMock::new(Arc::clone(&receipt_storage)); - - let allocation_ids_set = Arc::new(RwLock::new(HashSet::from_iter(allocation_ids()))); - let sender_ids_set = Arc::new(RwLock::new(HashSet::from_iter(sender_ids()))); - let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); - - let receipt_checks_adapter = ReceiptChecksAdapterMock::new( - Arc::clone(&receipt_storage), - Arc::clone(&query_appraisal_storage), - Arc::clone(&allocation_ids_set), - Arc::clone(&sender_ids_set), - ); - - ( - receipt_storage_adapter, - receipt_checks_adapter, - query_appraisal_storage, - ) - } - - #[fixture] - fn escrow_adapters() -> (EscrowAdapterMock, Arc>>) { - let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); - let escrow_adapter = EscrowAdapterMock::new(Arc::clone(&sender_escrow_storage)); - (escrow_adapter, sender_escrow_storage) - } - - #[fixture] - fn auditor_executor() -> (AuditorExecutorMock, EscrowStorage, QueryAppraisals) { - let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); - - let receipt_storage = Arc::new(RwLock::new(HashMap::new())); - - let allocation_ids_set = Arc::new(RwLock::new(HashSet::from_iter(allocation_ids()))); - let sender_ids_set = Arc::new(RwLock::new(HashSet::from_iter(sender_ids()))); - let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); - ( - AuditorExecutorMock::new( - receipt_storage, - sender_escrow_storage.clone(), - query_appraisal_storage.clone(), - allocation_ids_set, - sender_ids_set, - ), - sender_escrow_storage, - query_appraisal_storage, - ) - } - - #[fixture] - fn domain_separator() -> Eip712Domain { - tap_eip712_domain(1, Address::from([0x11u8; 20])) - } - - #[rstest] - #[tokio::test] - async fn initialization_valid_receipt( - keys: (LocalWallet, Address), - allocation_ids: Vec
, - domain_separator: Eip712Domain, - ) { - let signed_receipt = EIP712SignedMessage::new( - &domain_separator, - Receipt::new(allocation_ids[0], 10).unwrap(), - &keys.0, - ) - .unwrap(); - let query_id = 1; - let checks = get_full_list_of_checks(); - - let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); - - let received_receipt = match received_receipt { - ReceivedReceipt::Checking(checking) => checking, - _ => panic!("ReceivedReceipt should be in Checking state"), - }; - - assert!(received_receipt.completed_checks_with_errors().is_empty()); - assert!(received_receipt.incomplete_checks().len() == checks.len()); - } - - #[rstest] - #[tokio::test] - async fn partial_then_full_check_valid_receipt( - keys: (LocalWallet, Address), - domain_separator: Eip712Domain, - allocation_ids: Vec
, - auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), - ) { - let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; - // 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 query_value = 20u128; - let signed_receipt = EIP712SignedMessage::new( - &domain_separator, - Receipt::new(allocation_ids[0], query_value).unwrap(), - &keys.0, - ) - .unwrap(); - - let query_id = 1; - - // prepare adapters and storage to correctly validate receipt - - // add escrow for sender - escrow_storage - .write() - .await - .insert(keys.1, query_value + 500); - // appraise query - query_appraisal_storage - .write() - .await - .insert(query_id, query_value); - - let checks = get_full_list_of_checks(); - let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); - let receipt_id = 0u64; - - let mut received_receipt = match received_receipt { - ReceivedReceipt::Checking(checking) => checking, - _ => panic!("ReceivedReceipt should be in Checking state"), - }; - - // perform single arbitrary check - // let arbitrary_check_to_perform = ReceiptCheck::CheckUnique; - // received_receipt - // .perform_check(&arbitrary_check_to_perform, receipt_id, &receipt_auditor) - // .await; - // assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); - // - // received_receipt - // .perform_checks(&checks, receipt_id, &receipt_auditor) - // .await; - assert!(received_receipt.checking_is_complete()); - } - - #[rstest] - #[tokio::test] - async fn partial_then_finalize_valid_receipt( - keys: (LocalWallet, Address), - allocation_ids: Vec
, - domain_separator: Eip712Domain, - auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), - ) { - let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; - // 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 query_value = 20u128; - let signed_receipt = EIP712SignedMessage::new( - &domain_separator, - Receipt::new(allocation_ids[0], query_value).unwrap(), - &keys.0, - ) - .unwrap(); - - let query_id = 1; - - // prepare adapters and storage to correctly validate receipt - - // add escrow for sender - escrow_storage - .write() - .await - .insert(keys.1, query_value + 500); - // appraise query - query_appraisal_storage - .write() - .await - .insert(query_id, query_value); - - let checks = get_full_list_of_checks(); - let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); - let receipt_id = 0u64; - - let mut received_receipt = match received_receipt { - ReceivedReceipt::Checking(checking) => checking, - _ => panic!("ReceivedReceipt should be in Checking state"), - }; - - // perform single arbitrary check - // let arbitrary_check_to_perform = ReceiptCheck::CheckUnique; - - // received_receipt - // .perform_check(/** Check Unique */) - // .await; - // assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); - - assert!(received_receipt.finalize_receipt_checks().await.is_ok()); - } - - #[rstest] - #[tokio::test] - async fn standard_lifetime_valid_receipt( - keys: (LocalWallet, Address), - allocation_ids: Vec
, - domain_separator: Eip712Domain, - auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), - ) { - let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; - // 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 query_value = 20u128; - let signed_receipt = EIP712SignedMessage::new( - &domain_separator, - Receipt::new(allocation_ids[0], query_value).unwrap(), - &keys.0, - ) - .unwrap(); - - let query_id = 1; - - // prepare adapters and storage to correctly validate receipt - - // add escrow for sender - escrow_storage - .write() - .await - .insert(keys.1, query_value + 500); - // appraise query - query_appraisal_storage - .write() - .await - .insert(query_id, query_value); - - let checks = get_full_list_of_checks(); - let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); - let receipt_id = 0u64; - - let received_receipt = match received_receipt { - ReceivedReceipt::Checking(checking) => checking, - _ => panic!("ReceivedReceipt should be in Checking state"), - }; - - assert!(received_receipt.finalize_receipt_checks().await.is_ok()); - } -} diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 6a4d5e15..8b8ea19f 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -5,9 +5,8 @@ // The tests use a mock Indexer server running a tap_manager instance and a tap_aggregator to handle RAV requests. // An Indexer checks and stores receipts. After receiving a specific number of receipts, the Indexer sends a RAV request to the aggregator. use std::{ - collections::{HashMap, HashSet}, + collections::HashMap, convert::TryInto, - iter::FromIterator, net::{SocketAddr, TcpListener}, str::FromStr, sync::Arc, @@ -29,7 +28,6 @@ use tap_core::{ adapters::{ escrow_adapter_mock::EscrowAdapterMock, executor_mock::ExecutorMock, rav_storage_adapter_mock::RAVStorageAdapterMock, - receipt_checks_adapter_mock::ReceiptChecksAdapterMock, receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }, checks::ReceiptCheck, @@ -195,30 +193,6 @@ fn receipt_storage_adapter( ReceiptStorageAdapterMock::new(receipt_storage) } -// This adapter is used by the Indexer to check the validity of the receipt. -#[fixture] -fn receipt_checks_adapter( - keys_sender: (LocalWallet, Address), - query_price: Vec, - allocation_ids: Vec
, - receipt_storage: Arc>>, -) -> ReceiptChecksAdapterMock { - let (_, sender_address) = keys_sender; - let query_appraisals: HashMap<_, _> = (0u64..).zip(query_price).collect(); - let query_appraisals_storage = Arc::new(RwLock::new(query_appraisals)); - let allocation_ids: Arc>> = - Arc::new(RwLock::new(HashSet::from_iter(allocation_ids))); - let sender_ids: Arc>> = - Arc::new(RwLock::new(HashSet::from([sender_address]))); - - ReceiptChecksAdapterMock::new( - receipt_storage, - query_appraisals_storage, - allocation_ids, - sender_ids, - ) -} - // A structure for storing received RAVs. #[fixture] fn rav_storage_adapter() -> RAVStorageAdapterMock { From 8c9595456bad9c334b539d0363de884239f14157 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 17:40:31 -0300 Subject: [PATCH 13/32] fix: missing merge conflicts Signed-off-by: Gustavo Inacio --- tap_core/src/tap_manager/test/manager_test.rs | 25 ++++--------------- tap_integration_tests/tests/showcase.rs | 2 +- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index b39e0955..c806257a 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -3,7 +3,11 @@ #[cfg(test)] #[allow(clippy::too_many_arguments)] mod manager_unit_test { - use std::{collections::HashMap, str::FromStr, sync::Arc}; + use std::{ + collections::{HashMap, HashSet}, + str::FromStr, + sync::Arc, + }; use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; @@ -17,7 +21,6 @@ mod manager_unit_test { escrow_adapter_mock::EscrowAdapterMock, executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, receipt_storage_adapter::ReceiptRead, - receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }, checks::ReceiptCheck, eip_712_signed_message::EIP712SignedMessage, @@ -95,24 +98,6 @@ mod manager_unit_test { (escrow_adapter, sender_escrow_storage) } - #[fixture] - fn receipt_adapters() -> (ReceiptStorageAdapterMock, Arc>>) { - let receipt_storage = Arc::new(RwLock::new(HashMap::new())); - - // let allocation_ids_set = Arc::new(RwLock::new(HashSet::from_iter(allocation_ids()))); - // let sender_ids_set = Arc::new(RwLock::new(HashSet::from_iter(sender_ids()))); - let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); - - // let receipt_checks_adapter = ReceiptChecksAdapterMock::new( - // Arc::clone(&receipt_storage), - // Arc::clone(&query_appraisal_storage), - // Arc::clone(&allocation_ids_set), - // Arc::clone(&sender_ids_set), - // ); - - (receipt_checks_adapter, query_appraisal_storage) - } - #[rstest] #[case::full_checks(get_full_list_of_checks())] // #[case::partial_checks(todo!())] diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 8b8ea19f..363dabd0 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -5,7 +5,7 @@ // The tests use a mock Indexer server running a tap_manager instance and a tap_aggregator to handle RAV requests. // An Indexer checks and stores receipts. After receiving a specific number of receipts, the Indexer sends a RAV request to the aggregator. use std::{ - collections::HashMap, + collections::{HashMap, HashSet}, convert::TryInto, net::{SocketAddr, TcpListener}, str::FromStr, From 0c7bc1c9cebbb8dfad1c83067a1457380009925f Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 17:47:42 -0300 Subject: [PATCH 14/32] fix: rebase main Signed-off-by: Gustavo Inacio --- tap_aggregator/src/server.rs | 1 - tap_core/src/tap_manager/manager.rs | 4 ++-- tap_integration_tests/tests/showcase.rs | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/tap_aggregator/src/server.rs b/tap_aggregator/src/server.rs index 92b3b9ef..2f71edf6 100644 --- a/tap_aggregator/src/server.rs +++ b/tap_aggregator/src/server.rs @@ -160,7 +160,6 @@ fn aggregate_receipts_( wallet, accepted_addresses, ) - .await } }; diff --git a/tap_core/src/tap_manager/manager.rs b/tap_core/src/tap_manager/manager.rs index ba44f39e..e9994532 100644 --- a/tap_core/src/tap_manager/manager.rs +++ b/tap_core/src/tap_manager/manager.rs @@ -93,7 +93,7 @@ where } self.timestamp_check - .update_min_timestamp_ns(expected_rav.timestamp_ns) + .update_min_timestamp_ns(expected_rav.timestampNs) .await; self.executor @@ -104,7 +104,7 @@ where })?; self.timestamp_check - .update_min_timestamp_ns(expected_rav.timestamp_ns) + .update_min_timestamp_ns(expected_rav.timestampNs) .await; Ok(()) diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 363dabd0..3e1fb35d 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -34,7 +34,7 @@ use tap_core::{ eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, tap_manager::SignedRAV, - tap_receipt::{Receipt, ReceiptCheck, ReceivedReceipt}, + tap_receipt::{Receipt, ReceivedReceipt}, }; use crate::indexer_mock; From e8980e6e7a95fb6fc22cce4fa0345073a7b0b8c7 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 19:35:18 -0300 Subject: [PATCH 15/32] test: add mock checks Signed-off-by: Gustavo Inacio --- .../test/receipt_checks_adapter_test.rs | 24 ++- .../test/receipt_storage_adapter_test.rs | 31 ++- tap_core/src/checks/mod.rs | 195 ++++++++++++++---- tap_core/src/tap_manager/test/manager_test.rs | 29 ++- tap_core/src/tap_receipt/mod.rs | 31 +-- .../received_receipt_unit_test.rs | 95 ++++++--- 6 files changed, 282 insertions(+), 123 deletions(-) diff --git a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs index c59545ac..9bc205ea 100644 --- a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs @@ -17,9 +17,10 @@ mod receipt_checks_adapter_unit_test { use tokio::sync::RwLock; use crate::{ + checks::{tests::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, - tap_receipt::{get_full_list_of_checks, Receipt, ReceivedReceipt}, + tap_receipt::{Receipt, ReceivedReceipt}, }; #[fixture] @@ -27,9 +28,23 @@ mod receipt_checks_adapter_unit_test { tap_eip712_domain(1, Address::from([0x11u8; 20])) } + #[fixture] + fn checks(domain_separator: Eip712Domain) -> Vec { + get_full_list_of_checks( + domain_separator, + HashSet::new(), + Arc::new(RwLock::new(HashSet::new())), + Arc::new(RwLock::new(HashMap::new())), + Arc::new(RwLock::new(HashMap::new())), + ) + } + #[rstest] #[tokio::test] - async fn receipt_checks_adapter_test(domain_separator: Eip712Domain) { + async fn receipt_checks_adapter_test( + domain_separator: Eip712Domain, + checks: Vec, + ) { let sender_ids = [ Address::from_str("0xfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb").unwrap(), Address::from_str("0xfafafafafafafafafafafafafafafafafafafafa").unwrap(), @@ -54,6 +69,7 @@ mod receipt_checks_adapter_unit_test { .then(|id| { let wallet = wallet.clone(); let domain_separator = &domain_separator; + let checks = checks.clone(); async move { ( id, @@ -65,7 +81,7 @@ mod receipt_checks_adapter_unit_test { ) .unwrap(), id, - &get_full_list_of_checks(), + &checks, ), ) } @@ -95,7 +111,7 @@ mod receipt_checks_adapter_unit_test { ) .unwrap(), 10u64, - &get_full_list_of_checks(), + &checks, ), ); diff --git a/tap_core/src/adapters/test/receipt_storage_adapter_test.rs b/tap_core/src/adapters/test/receipt_storage_adapter_test.rs index 9f8cd71f..14bc048e 100644 --- a/tap_core/src/adapters/test/receipt_storage_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_storage_adapter_test.rs @@ -5,7 +5,7 @@ mod receipt_storage_adapter_unit_test { use rand::seq::SliceRandom; use rand::thread_rng; - use std::collections::HashMap; + use std::collections::{HashMap, HashSet}; use std::str::FromStr; use std::sync::Arc; @@ -20,21 +20,31 @@ mod receipt_storage_adapter_unit_test { receipt_storage_adapter::ReceiptStore, receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }; + use crate::checks::tests::get_full_list_of_checks; + use crate::checks::ReceiptCheck; use crate::tap_eip712_domain; use crate::tap_receipt::ReceivedReceipt; - use crate::{ - eip_712_signed_message::EIP712SignedMessage, tap_receipt::get_full_list_of_checks, - tap_receipt::Receipt, - }; + use crate::{eip_712_signed_message::EIP712SignedMessage, tap_receipt::Receipt}; #[fixture] fn domain_separator() -> Eip712Domain { tap_eip712_domain(1, Address::from([0x11u8; 20])) } + #[fixture] + fn checks(domain_separator: Eip712Domain) -> Vec { + get_full_list_of_checks( + domain_separator, + HashSet::new(), + Arc::new(RwLock::new(HashSet::new())), + Arc::new(RwLock::new(HashMap::new())), + Arc::new(RwLock::new(HashMap::new())), + ) + } + #[rstest] #[tokio::test] - async fn receipt_adapter_test(domain_separator: Eip712Domain) { + async fn receipt_adapter_test(domain_separator: Eip712Domain, checks: Vec) { let receipt_storage = Arc::new(RwLock::new(HashMap::new())); let mut receipt_adapter = ReceiptStorageAdapterMock::new(receipt_storage); @@ -57,7 +67,7 @@ mod receipt_storage_adapter_unit_test { ) .unwrap(), query_id, - &get_full_list_of_checks(), + &checks, ); let receipt_store_result = receipt_adapter.store_receipt(received_receipt).await; @@ -95,7 +105,7 @@ mod receipt_storage_adapter_unit_test { #[rstest] #[tokio::test] - async fn multi_receipt_adapter_test(domain_separator: Eip712Domain) { + async fn multi_receipt_adapter_test(domain_separator: Eip712Domain, checks: Vec) { let receipt_storage = Arc::new(RwLock::new(HashMap::new())); let mut receipt_adapter = ReceiptStorageAdapterMock::new(receipt_storage); @@ -118,7 +128,7 @@ mod receipt_storage_adapter_unit_test { ) .unwrap(), query_id as u64, - &get_full_list_of_checks(), + &checks, )); } let mut receipt_ids = Vec::new(); @@ -186,6 +196,7 @@ mod receipt_storage_adapter_unit_test { #[test] fn safe_truncate_receipts_test( domain_separator: Eip712Domain, + checks: Vec, #[case] input: Vec, #[case] limit: u64, #[case] expected: Vec, @@ -215,7 +226,7 @@ mod receipt_storage_adapter_unit_test { ) .unwrap(), i as u64, // Will use that to check the IDs - &get_full_list_of_checks(), + &checks, ), )); } diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs index 1e277903..786dcc2b 100644 --- a/tap_core/src/checks/mod.rs +++ b/tap_core/src/checks/mod.rs @@ -1,7 +1,6 @@ use crate::tap_receipt::{Checking, ReceiptError, ReceiptResult, ReceiptWithState}; -use ethers::types::Signature; use serde::{Deserialize, Serialize}; -use std::{collections::HashSet, sync::Arc}; +use std::sync::Arc; use tokio::sync::RwLock; pub type ReceiptCheck = Arc; @@ -21,8 +20,6 @@ impl CheckingChecks { match self { Self::Pending(check) => { let result = check.check(&receipt).await; - // *self = Self::Executed(result); - // self Self::Executed(result) } Self::Executed(_) => self, @@ -57,33 +54,6 @@ pub trait Check: std::fmt::Debug + Send + Sync { } } -#[derive(Debug, Serialize, Deserialize)] -struct UniqueCheck; - -#[async_trait::async_trait] -#[typetag::serde] -impl Check for UniqueCheck { - async fn check(&self, _receipt: &ReceiptWithState) -> ReceiptResult<()> { - println!("UniqueCheck"); - Ok(()) - } - - async fn check_batch(&self, receipts: &[ReceiptWithState]) -> Vec> { - let mut signatures: HashSet = HashSet::new(); - let mut results = Vec::new(); - - for received_receipt in receipts { - let signature = received_receipt.signed_receipt.signature; - if signatures.insert(signature) { - results.push(Ok(())); - } else { - results.push(Err(ReceiptError::NonUniqueReceipt)); - } - } - results - } -} - #[derive(Debug, Serialize, Deserialize)] pub struct TimestampCheck { #[serde(skip)] @@ -118,14 +88,161 @@ impl Check for TimestampCheck { } } -#[derive(Debug, Serialize, Deserialize)] -struct AllocationId; +#[cfg(test)] +pub mod tests { + + use super::*; + use crate::tap_receipt::ReceivedReceipt; + use alloy_primitives::Address; + use alloy_sol_types::Eip712Domain; + use std::{ + collections::{HashMap, HashSet}, + fmt::Debug, + }; + + pub fn get_full_list_of_checks( + domain_separator: Eip712Domain, + valid_signers: HashSet
, + allocation_ids: Arc>>, + receipt_storage: Arc>>, + query_appraisals: Arc>>, + ) -> Vec { + vec![ + Arc::new(UniqueCheck { receipt_storage }), + Arc::new(ValueCheck { query_appraisals }), + Arc::new(AllocationIdCheck { allocation_ids }), + Arc::new(SignatureCheck { + domain_separator, + valid_signers, + }), + Arc::new(TimestampCheck::new(0)), + ] + } -#[async_trait::async_trait] -#[typetag::serde] -impl Check for AllocationId { - async fn check(&self, _receipt: &ReceiptWithState) -> ReceiptResult<()> { - println!("AllocationId"); - Ok(()) + #[derive(Serialize, Deserialize)] + struct UniqueCheck { + #[serde(skip)] + receipt_storage: Arc>>, + } + impl Debug for UniqueCheck { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "UniqueCheck") + } + } + + #[async_trait::async_trait] + #[typetag::serde] + impl Check for UniqueCheck { + async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { + let receipt_storage = self.receipt_storage.read().await; + // let receipt_id = receipt. + receipt_storage + .iter() + .all(|(_stored_receipt_id, stored_receipt)| { + stored_receipt.signed_receipt().message != receipt.signed_receipt().message + }) + .then_some(()) + .ok_or(ReceiptError::NonUniqueReceipt) + } + + async fn check_batch( + &self, + receipts: &[ReceiptWithState], + ) -> Vec> { + let mut signatures: HashSet = HashSet::new(); + let mut results = Vec::new(); + + for received_receipt in receipts { + let signature = received_receipt.signed_receipt.signature; + if signatures.insert(signature) { + results.push(Ok(())); + } else { + results.push(Err(ReceiptError::NonUniqueReceipt)); + } + } + results + } + } + + #[derive(Debug, Serialize, Deserialize)] + struct ValueCheck { + #[serde(skip)] + query_appraisals: Arc>>, + } + + #[async_trait::async_trait] + #[typetag::serde] + impl Check for ValueCheck { + async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { + let query_id = receipt.query_id; + let value = receipt.signed_receipt().message.value; + let query_appraisals = self.query_appraisals.read().await; + let appraised_value = + query_appraisals + .get(&query_id) + .ok_or(ReceiptError::CheckFailedToComplete { + source_error_message: "Could not find query_appraisals".into(), + })?; + + if value != *appraised_value { + Err(ReceiptError::InvalidValue { + received_value: value, + }) + } else { + Ok(()) + } + } + } + + #[derive(Debug, Serialize, Deserialize)] + struct AllocationIdCheck { + #[serde(skip)] + allocation_ids: Arc>>, + } + + #[async_trait::async_trait] + #[typetag::serde] + impl Check for AllocationIdCheck { + async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { + let received_allocation_id = receipt.signed_receipt().message.allocation_id; + if self + .allocation_ids + .read() + .await + .contains(&received_allocation_id) + { + Ok(()) + } else { + Err(ReceiptError::InvalidAllocationID { + received_allocation_id, + }) + } + } + } + + #[derive(Debug, Serialize, Deserialize)] + struct SignatureCheck { + domain_separator: Eip712Domain, + valid_signers: HashSet
, + } + + #[async_trait::async_trait] + #[typetag::serde] + impl Check for SignatureCheck { + async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { + let recovered_address = receipt + .signed_receipt() + .recover_signer(&self.domain_separator) + .map_err(|e| ReceiptError::InvalidSignature { + source_error_message: e.to_string(), + })?; + if !self.valid_signers.contains(&recovered_address) { + Err(ReceiptError::InvalidSignature { + source_error_message: "Invalid signer".to_string(), + }) + } else { + Ok(()) + } + } } } diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index c806257a..ac2f83c6 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -22,10 +22,10 @@ mod manager_unit_test { executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, receipt_storage_adapter::ReceiptRead, }, - checks::ReceiptCheck, + checks::{tests::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, get_current_timestamp_u64_ns, tap_eip712_domain, - tap_receipt::{get_full_list_of_checks, Receipt}, + tap_receipt::Receipt, }; #[fixture] @@ -61,6 +61,11 @@ mod manager_unit_test { ] } + #[fixture] + fn full_list_of_checks() -> Vec { + vec![] + } + #[fixture] fn domain_separator() -> Eip712Domain { tap_eip712_domain(1, Address::from([0x11u8; 20])) @@ -99,7 +104,7 @@ mod manager_unit_test { } #[rstest] - #[case::full_checks(get_full_list_of_checks())] + // #[case::full_checks(get_full_list_of_checks())] // #[case::partial_checks(todo!())] #[case::no_checks(Vec::::new())] #[tokio::test] @@ -109,6 +114,7 @@ mod manager_unit_test { allocation_ids: Vec
, domain_separator: Eip712Domain, #[case] initial_checks: Vec, + full_list_of_checks: Vec, ) { let (executor, escrow_storage, query_appraisal_storage) = executor_mock; // give receipt 5 second variance for min start time @@ -117,7 +123,7 @@ mod manager_unit_test { let manager = Manager::new( domain_separator.clone(), executor, - get_full_list_of_checks(), + full_list_of_checks, starting_min_timestamp, ); @@ -142,7 +148,7 @@ mod manager_unit_test { } #[rstest] - #[case::full_checks(get_full_list_of_checks())] + // #[case::full_checks(get_full_list_of_checks())] // #[case::partial_checks(vec![ReceiptCheck::CheckSignature])] #[case::no_checks(Vec::::new())] #[tokio::test] @@ -152,6 +158,7 @@ mod manager_unit_test { allocation_ids: Vec
, domain_separator: Eip712Domain, #[case] initial_checks: Vec, + full_list_of_checks: Vec, ) { let (executor, escrow_storage, query_appraisal_storage) = executor_mock; // give receipt 5 second variance for min start time @@ -160,7 +167,7 @@ mod manager_unit_test { let manager = Manager::new( domain_separator.clone(), executor, - get_full_list_of_checks(), + full_list_of_checks, starting_min_timestamp, ); escrow_storage.write().await.insert(keys.1, 999999); @@ -210,7 +217,7 @@ mod manager_unit_test { } #[rstest] - #[case::full_checks(get_full_list_of_checks())] + // #[case::full_checks(get_full_list_of_checks())] // #[case::partial_checks(vec![ReceiptCheck::CheckSignature])] #[case::no_checks(Vec::::new())] #[tokio::test] @@ -220,6 +227,7 @@ mod manager_unit_test { allocation_ids: Vec
, domain_separator: Eip712Domain, #[case] initial_checks: Vec, + full_list_of_checks: Vec, ) { let (executor, escrow_storage, query_appraisal_storage) = executor_mock; // give receipt 5 second variance for min start time @@ -228,7 +236,7 @@ mod manager_unit_test { let manager = Manager::new( domain_separator.clone(), executor, - get_full_list_of_checks(), + full_list_of_checks, starting_min_timestamp, ); @@ -345,9 +353,10 @@ mod manager_unit_test { keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, - #[values(get_full_list_of_checks(), /* vec![ReceiptCheck::CheckSignature], */Vec::::new())] + #[values(/*get_full_list_of_checks(), vec![ReceiptCheck::CheckSignature], */Vec::::new())] initial_checks: Vec, #[values(true, false)] remove_old_receipts: bool, + full_list_of_checks: Vec, ) { let (executor, escrow_storage, query_appraisal_storage) = executor_mock; // give receipt 5 second variance for min start time @@ -356,7 +365,7 @@ mod manager_unit_test { let manager = Manager::new( domain_separator.clone(), executor, - get_full_list_of_checks(), + full_list_of_checks, starting_min_timestamp, ); diff --git a/tap_core/src/tap_receipt/mod.rs b/tap_core/src/tap_receipt/mod.rs index 31910031..4261a21b 100644 --- a/tap_core/src/tap_receipt/mod.rs +++ b/tap_core/src/tap_receipt/mod.rs @@ -17,7 +17,7 @@ pub use received_receipt::{ use serde::{Deserialize, Serialize}; use thiserror::Error; -use crate::checks::{CheckingChecks, ReceiptCheck}; +use crate::checks::CheckingChecks; #[derive(Error, Debug, Clone, Serialize, Deserialize)] pub enum ReceiptError { @@ -42,32 +42,3 @@ pub enum ReceiptError { pub type ReceiptResult = Result; pub type ReceiptCheckResults = HashMap<&'static str, CheckingChecks>; -// #[derive(Hash, Eq, PartialEq, Debug, Clone, EnumString, Display, Serialize, Deserialize)] -// pub enum ReceiptCheck { -// CheckUnique, -// CheckAllocationId, -// CheckTimestamp, -// CheckValue, -// CheckSignature, -// } - -pub fn get_full_list_of_receipt_check_results() -> ReceiptCheckResults { - let all_checks_list = ReceiptCheckResults::new(); - // all_checks_list.insert(ReceiptCheck::CheckUnique, None); - // all_checks_list.insert(ReceiptCheck::CheckAllocationId, None); - // all_checks_list.insert(ReceiptCheck::CheckTimestamp, None); - // all_checks_list.insert(ReceiptCheck::CheckValue, None); - // all_checks_list.insert(ReceiptCheck::CheckSignature, None); - - all_checks_list -} - -pub fn get_full_list_of_checks() -> Vec { - vec![ - // ReceiptCheck::CheckUnique, - // ReceiptCheck::CheckAllocationId, - // ReceiptCheck::CheckTimestamp, - // ReceiptCheck::CheckValue, - // ReceiptCheck::CheckSignature, - ] -} 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 1ee37320..b4601702 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 @@ -17,16 +17,19 @@ use crate::{ executor_mock::{EscrowStorage, QueryAppraisals}, receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }, + checks::{tests::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, get_current_timestamp_u64_ns, tap_eip712_domain, - tap_receipt::{ - get_full_list_of_checks, Receipt, ReceiptAuditor, ReceiptCheckResults, ReceivedReceipt, - }, + tap_receipt::{Receipt, ReceiptAuditor, ReceiptCheckResults, ReceivedReceipt}, }; use super::{Checking, ReceiptWithState}; impl ReceiptWithState { + fn check_is_complete(&self, check: &str) -> bool { + self.state.checks.get(check).unwrap().is_complete() + } + fn checking_is_complete(&self) -> bool { self.state .checks @@ -82,13 +85,14 @@ fn sender_ids() -> Vec
{ } #[fixture] -fn receipt_adapters() -> (ReceiptStorageAdapterMock, Arc>>) { +fn receipt_adapters() -> ( + ReceiptStorageAdapterMock, + Arc>>, +) { let receipt_storage = Arc::new(RwLock::new(HashMap::new())); let receipt_storage_adapter = ReceiptStorageAdapterMock::new(Arc::clone(&receipt_storage)); - let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); - - (receipt_storage_adapter, query_appraisal_storage) + (receipt_storage_adapter, receipt_storage) } #[fixture] @@ -125,12 +129,35 @@ fn domain_separator() -> Eip712Domain { tap_eip712_domain(1, Address::from([0x11u8; 20])) } +#[fixture] +fn checks( + domain_separator: Eip712Domain, + auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), + receipt_adapters: ( + ReceiptStorageAdapterMock, + Arc>>, + ), + allocation_ids: Vec
, + sender_ids: Vec
, +) -> Vec { + let (_, receipt_storage) = receipt_adapters; + let (_, _escrow_storage, query_appraisal_storage) = auditor_executor; + get_full_list_of_checks( + domain_separator, + sender_ids.iter().cloned().collect(), + Arc::new(RwLock::new(allocation_ids.iter().cloned().collect())), + receipt_storage, + query_appraisal_storage, + ) +} + #[rstest] #[tokio::test] async fn initialization_valid_receipt( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, + checks: Vec, ) { let signed_receipt = EIP712SignedMessage::new( &domain_separator, @@ -139,7 +166,6 @@ async fn initialization_valid_receipt( ) .unwrap(); let query_id = 1; - let checks = get_full_list_of_checks(); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); @@ -159,6 +185,7 @@ async fn partial_then_full_check_valid_receipt( domain_separator: Eip712Domain, allocation_ids: Vec
, auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), + checks: Vec, ) { let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; // give receipt 5 second variance for min start time @@ -188,25 +215,25 @@ async fn partial_then_full_check_valid_receipt( .await .insert(query_id, query_value); - let checks = get_full_list_of_checks(); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); let _receipt_id = 0u64; - let received_receipt = match received_receipt { + let mut received_receipt = match received_receipt { ReceivedReceipt::Checking(checking) => checking, _ => panic!("ReceivedReceipt should be in Checking state"), }; // perform single arbitrary check - // let arbitrary_check_to_perform = ReceiptCheck::CheckUnique; - // received_receipt - // .perform_check(&arbitrary_check_to_perform, receipt_id, &receipt_auditor) - // .await; - // assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); - // - // received_receipt - // .perform_checks(&checks, receipt_id, &receipt_auditor) - // .await; + let arbitrary_check_to_perform = checks[0].typetag_name(); + + received_receipt + .perform_check(arbitrary_check_to_perform) + .await; + assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); + + received_receipt + .perform_checks(&checks.iter().map(|c| c.typetag_name()).collect::>()) + .await; assert!(received_receipt.checking_is_complete()); } @@ -217,6 +244,7 @@ async fn partial_then_finalize_valid_receipt( allocation_ids: Vec
, domain_separator: Eip712Domain, auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), + checks: Vec, ) { let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; // give receipt 5 second variance for min start time @@ -246,24 +274,31 @@ async fn partial_then_finalize_valid_receipt( .await .insert(query_id, query_value); - let checks = get_full_list_of_checks(); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); let _receipt_id = 0u64; - let received_receipt = match received_receipt { + let mut received_receipt = match received_receipt { ReceivedReceipt::Checking(checking) => checking, _ => panic!("ReceivedReceipt should be in Checking state"), }; // perform single arbitrary check - // let arbitrary_check_to_perform = ReceiptCheck::CheckUnique; - - // received_receipt - // .perform_check(/** Check Unique */) - // .await; - // assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); - - assert!(received_receipt.finalize_receipt_checks().await.is_ok()); + let arbitrary_check_to_perform = checks[0].typetag_name(); + + received_receipt + .perform_check(arbitrary_check_to_perform) + .await; + assert!(received_receipt.check_is_complete(arbitrary_check_to_perform)); + + let awaiting_escrow_receipt = received_receipt.finalize_receipt_checks().await; + println!("{:?}", awaiting_escrow_receipt); + assert!(awaiting_escrow_receipt.is_ok()); + + let awaiting_escrow_receipt = awaiting_escrow_receipt.unwrap(); + let receipt = awaiting_escrow_receipt + .check_and_reserve_escrow(&_receipt_auditor) + .await; + assert!(receipt.is_ok()); } #[rstest] @@ -273,6 +308,7 @@ async fn standard_lifetime_valid_receipt( allocation_ids: Vec
, domain_separator: Eip712Domain, auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), + checks: Vec, ) { let (executor, escrow_storage, query_appraisal_storage) = auditor_executor; // give receipt 5 second variance for min start time @@ -302,7 +338,6 @@ async fn standard_lifetime_valid_receipt( .await .insert(query_id, query_value); - let checks = get_full_list_of_checks(); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); let _receipt_id = 0u64; From 3d6ec98107150fb5fde5176d16dbb36129b4abf8 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 20:27:13 -0300 Subject: [PATCH 16/32] style: remove unused parameters Signed-off-by: Gustavo Inacio --- .../adapters/mock/auditor_executor_mock.rs | 26 ++----------------- tap_core/src/adapters/mock/executor_mock.rs | 16 +----------- tap_core/src/tap_manager/test/manager_test.rs | 19 +++----------- .../received_receipt_unit_test.rs | 18 ++----------- tap_integration_tests/tests/showcase.rs | 23 ++-------------- 5 files changed, 10 insertions(+), 92 deletions(-) diff --git a/tap_core/src/adapters/mock/auditor_executor_mock.rs b/tap_core/src/adapters/mock/auditor_executor_mock.rs index 873918ec..5c119368 100644 --- a/tap_core/src/adapters/mock/auditor_executor_mock.rs +++ b/tap_core/src/adapters/mock/auditor_executor_mock.rs @@ -3,42 +3,20 @@ use super::escrow_adapter_mock::AdpaterErrorMock; use crate::adapters::escrow_adapter::EscrowAdapter; -// use crate::adapters::receipt_checks_adapter::ReceiptChecksAdapter; -use crate::tap_receipt::ReceivedReceipt; use alloy_primitives::Address; use async_trait::async_trait; -use std::{ - collections::{HashMap, HashSet}, - sync::Arc, -}; +use std::{collections::HashMap, sync::Arc}; use tokio::sync::RwLock; #[derive(Clone)] -#[allow(dead_code)] pub struct AuditorExecutorMock { - receipt_storage: Arc>>, - sender_escrow_storage: Arc>>, - - query_appraisals: Arc>>, - allocation_ids: Arc>>, - sender_ids: Arc>>, } impl AuditorExecutorMock { - pub fn new( - receipt_storage: Arc>>, - sender_escrow_storage: Arc>>, - query_appraisals: Arc>>, - allocation_ids: Arc>>, - sender_ids: Arc>>, - ) -> Self { + pub fn new(sender_escrow_storage: Arc>>) -> Self { AuditorExecutorMock { - receipt_storage, sender_escrow_storage, - allocation_ids, - sender_ids, - query_appraisals, } } } diff --git a/tap_core/src/adapters/mock/executor_mock.rs b/tap_core/src/adapters/mock/executor_mock.rs index 33eefde7..52fadf9b 100644 --- a/tap_core/src/adapters/mock/executor_mock.rs +++ b/tap_core/src/adapters/mock/executor_mock.rs @@ -15,17 +15,13 @@ use crate::{ use alloy_primitives::Address; use async_trait::async_trait; use std::ops::RangeBounds; -use std::{ - collections::{HashMap, HashSet}, - sync::Arc, -}; +use std::{collections::HashMap, sync::Arc}; use tokio::sync::RwLock; pub type EscrowStorage = Arc>>; pub type QueryAppraisals = Arc>>; #[derive(Clone)] -#[allow(dead_code)] pub struct ExecutorMock { /// local RAV store with rwlocks to allow sharing with other compenents as needed rav_storage: Arc>>, @@ -33,10 +29,6 @@ pub struct ExecutorMock { unique_id: Arc>, sender_escrow_storage: EscrowStorage, - - query_appraisals: QueryAppraisals, - allocation_ids: Arc>>, - sender_ids: Arc>>, } impl ExecutorMock { @@ -44,18 +36,12 @@ impl ExecutorMock { rav_storage: Arc>>, receipt_storage: Arc>>, sender_escrow_storage: Arc>>, - query_appraisals: Arc>>, - allocation_ids: Arc>>, - sender_ids: Arc>>, ) -> Self { ExecutorMock { rav_storage, receipt_storage, unique_id: Arc::new(RwLock::new(0)), sender_escrow_storage, - allocation_ids, - sender_ids, - query_appraisals, } } } diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index ac2f83c6..25a434ec 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -3,11 +3,7 @@ #[cfg(test)] #[allow(clippy::too_many_arguments)] mod manager_unit_test { - use std::{ - collections::{HashMap, HashSet}, - str::FromStr, - sync::Arc, - }; + use std::{collections::HashMap, str::FromStr, sync::Arc}; use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; @@ -22,7 +18,7 @@ mod manager_unit_test { executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, receipt_storage_adapter::ReceiptRead, }, - checks::{tests::get_full_list_of_checks, ReceiptCheck}, + checks::ReceiptCheck, eip_712_signed_message::EIP712SignedMessage, get_current_timestamp_u64_ns, tap_eip712_domain, tap_receipt::Receipt, @@ -78,19 +74,10 @@ mod manager_unit_test { let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); - let allocation_ids_set = Arc::new(RwLock::new(HashSet::from_iter(allocation_ids()))); - let sender_ids_set = Arc::new(RwLock::new(HashSet::from_iter(sender_ids()))); let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); ( - ExecutorMock::new( - rav_storage, - receipt_storage, - sender_escrow_storage.clone(), - query_appraisal_storage.clone(), - allocation_ids_set, - sender_ids_set, - ), + ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()), sender_escrow_storage, query_appraisal_storage, ) 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 b4601702..b34387cf 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 @@ -1,8 +1,4 @@ -use std::{ - collections::{HashMap, HashSet}, - str::FromStr, - sync::Arc, -}; +use std::{collections::HashMap, str::FromStr, sync::Arc}; use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; @@ -106,19 +102,9 @@ fn escrow_adapters() -> (EscrowAdapterMock, Arc>>) fn auditor_executor() -> (AuditorExecutorMock, EscrowStorage, QueryAppraisals) { let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); - let receipt_storage = Arc::new(RwLock::new(HashMap::new())); - - let allocation_ids_set = Arc::new(RwLock::new(HashSet::from_iter(allocation_ids()))); - let sender_ids_set = Arc::new(RwLock::new(HashSet::from_iter(sender_ids()))); let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); ( - AuditorExecutorMock::new( - receipt_storage, - sender_escrow_storage.clone(), - query_appraisal_storage.clone(), - allocation_ids_set, - sender_ids_set, - ), + AuditorExecutorMock::new(sender_escrow_storage.clone()), sender_escrow_storage, query_appraisal_storage, ) diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 3e1fb35d..ac9834c7 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -154,31 +154,12 @@ fn escrow_adapter() -> EscrowAdapterMock { } #[fixture] -fn executor( - keys_sender: (LocalWallet, Address), - query_price: Vec, - allocation_ids: Vec
, - receipt_storage: Arc>>, -) -> ExecutorMock { - let (_, sender_address) = keys_sender; - let query_appraisals: HashMap<_, _> = (0u64..).zip(query_price).collect(); - let query_appraisal_storage = Arc::new(RwLock::new(query_appraisals)); - let allocation_ids: Arc>> = - Arc::new(RwLock::new(HashSet::from_iter(allocation_ids))); - let sender_ids: Arc>> = - Arc::new(RwLock::new(HashSet::from([sender_address]))); +fn executor(receipt_storage: Arc>>) -> ExecutorMock { let rav_storage = Arc::new(RwLock::new(None)); let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); - ExecutorMock::new( - rav_storage, - receipt_storage, - sender_escrow_storage, - query_appraisal_storage, - allocation_ids, - sender_ids, - ) + ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage) } #[fixture] From 3391b4526f7ee5ab7b4121a0679f8f7a5c749e78 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 20:27:55 -0300 Subject: [PATCH 17/32] style: clippy and fmt Signed-off-by: Gustavo Inacio --- tap_aggregator/src/server.rs | 16 +++++++--------- .../timeline_aggretion_protocol_benchmark.rs | 2 +- tap_core/src/checks/mod.rs | 2 +- tap_core/src/tap_receipt/receipt_auditor.rs | 5 +---- tap_core/src/tap_receipt/received_receipt.rs | 2 +- .../received_receipt_unit_test.rs | 2 +- 6 files changed, 12 insertions(+), 17 deletions(-) diff --git a/tap_aggregator/src/server.rs b/tap_aggregator/src/server.rs index 2f71edf6..6d0d1a6e 100644 --- a/tap_aggregator/src/server.rs +++ b/tap_aggregator/src/server.rs @@ -152,15 +152,13 @@ fn aggregate_receipts_( } let res = match api_version { - TapRpcApiVersion::V0_0 => { - check_and_aggregate_receipts( - domain_separator, - &receipts, - previous_rav, - wallet, - accepted_addresses, - ) - } + TapRpcApiVersion::V0_0 => check_and_aggregate_receipts( + domain_separator, + &receipts, + previous_rav, + wallet, + accepted_addresses, + ), }; // Handle aggregation error diff --git a/tap_core/benches/timeline_aggretion_protocol_benchmark.rs b/tap_core/benches/timeline_aggretion_protocol_benchmark.rs index 5c015538..326db0f5 100644 --- a/tap_core/benches/timeline_aggretion_protocol_benchmark.rs +++ b/tap_core/benches/timeline_aggretion_protocol_benchmark.rs @@ -11,7 +11,7 @@ use std::str::FromStr; use alloy_primitives::Address; -use alloy_sol_types::{eip712_domain, Eip712Domain}; +use alloy_sol_types::{Eip712Domain}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use ethers::signers::{LocalWallet, Signer, Wallet}; use ethers_core::k256::ecdsa::SigningKey; diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs index 786dcc2b..a236cd09 100644 --- a/tap_core/src/checks/mod.rs +++ b/tap_core/src/checks/mod.rs @@ -19,7 +19,7 @@ impl CheckingChecks { pub async fn execute(self, receipt: &ReceiptWithState) -> Self { match self { Self::Pending(check) => { - let result = check.check(&receipt).await; + let result = check.check(receipt).await; Self::Executed(result) } Self::Executed(_) => self, diff --git a/tap_core/src/tap_receipt/receipt_auditor.rs b/tap_core/src/tap_receipt/receipt_auditor.rs index c5da0860..34f10578 100644 --- a/tap_core/src/tap_receipt/receipt_auditor.rs +++ b/tap_core/src/tap_receipt/receipt_auditor.rs @@ -20,10 +20,7 @@ pub struct ReceiptAuditor { } impl ReceiptAuditor { - pub fn new( - domain_separator: Eip712Domain, - executor: E, - ) -> Self { + pub fn new(domain_separator: Eip712Domain, executor: E) -> Self { Self { domain_separator, executor, diff --git a/tap_core/src/tap_receipt/received_receipt.rs b/tap_core/src/tap_receipt/received_receipt.rs index 51cd8427..8a69d181 100644 --- a/tap_core/src/tap_receipt/received_receipt.rs +++ b/tap_core/src/tap_receipt/received_receipt.rs @@ -248,7 +248,7 @@ impl ReceiptWithState { // Don't check if already failed let check = self.state.checks.remove(check_name); if let Some(check) = check { - let result = check.execute(&self).await; + let result = check.execute(self).await; self.state.checks.insert(check_name, result); } } 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 b34387cf..788ec53e 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 @@ -215,7 +215,7 @@ async fn partial_then_full_check_valid_receipt( received_receipt .perform_check(arbitrary_check_to_perform) .await; - assert!(received_receipt.check_is_complete(&arbitrary_check_to_perform)); + assert!(received_receipt.check_is_complete(arbitrary_check_to_perform)); received_receipt .perform_checks(&checks.iter().map(|c| c.typetag_name()).collect::>()) From 9aa467abc7d31b321518442b3d596054ef77a267 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 20:38:33 -0300 Subject: [PATCH 18/32] refactor: remove escrow adapter mock Signed-off-by: Gustavo Inacio --- tap_core/src/adapters/mock.rs | 1 - .../adapters/mock/auditor_executor_mock.rs | 14 ++-- .../src/adapters/mock/escrow_adapter_mock.rs | 83 ------------------- tap_core/src/adapters/mock/executor_mock.rs | 24 ++++-- .../adapters/mock/rav_storage_adapter_mock.rs | 13 +-- .../mock/receipt_storage_adapter_mock.rs | 9 +- .../src/adapters/test/escrow_adapter_test.rs | 10 ++- .../adapters/test/rav_storage_adapter_test.rs | 20 +++-- tap_core/src/tap_manager/test/manager_test.rs | 8 -- .../received_receipt_unit_test.rs | 8 -- tap_integration_tests/tests/showcase.rs | 9 +- 11 files changed, 50 insertions(+), 149 deletions(-) delete mode 100644 tap_core/src/adapters/mock/escrow_adapter_mock.rs diff --git a/tap_core/src/adapters/mock.rs b/tap_core/src/adapters/mock.rs index 1c4056de..972bedc1 100644 --- a/tap_core/src/adapters/mock.rs +++ b/tap_core/src/adapters/mock.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 pub mod auditor_executor_mock; -pub mod escrow_adapter_mock; pub mod executor_mock; pub mod rav_storage_adapter_mock; pub mod receipt_storage_adapter_mock; diff --git a/tap_core/src/adapters/mock/auditor_executor_mock.rs b/tap_core/src/adapters/mock/auditor_executor_mock.rs index 5c119368..4acf914d 100644 --- a/tap_core/src/adapters/mock/auditor_executor_mock.rs +++ b/tap_core/src/adapters/mock/auditor_executor_mock.rs @@ -1,13 +1,15 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use super::escrow_adapter_mock::AdpaterErrorMock; 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>>, @@ -22,12 +24,12 @@ impl AuditorExecutorMock { } impl AuditorExecutorMock { - pub async fn escrow(&self, sender_id: Address) -> Result { + 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(AdpaterErrorMock::AdapterError { + Err(AdapterErrorMock::AdapterError { error: "No escrow exists for provided sender ID.".to_owned(), }) } @@ -47,7 +49,7 @@ impl AuditorExecutorMock { &self, sender_id: Address, value: u128, - ) -> Result<(), AdpaterErrorMock> { + ) -> Result<(), AdapterErrorMock> { let mut sender_escrow_storage = self.sender_escrow_storage.write().await; if let Some(current_value) = sender_escrow_storage.get(&sender_id) { @@ -57,7 +59,7 @@ impl AuditorExecutorMock { return Ok(()); } } - Err(AdpaterErrorMock::AdapterError { + Err(AdapterErrorMock::AdapterError { error: "Provided value is greater than existing escrow.".to_owned(), }) } @@ -65,7 +67,7 @@ impl AuditorExecutorMock { #[async_trait] impl EscrowAdapter for AuditorExecutorMock { - type AdapterError = AdpaterErrorMock; + type AdapterError = AdapterErrorMock; async fn get_available_escrow(&self, sender_id: Address) -> Result { self.escrow(sender_id).await } diff --git a/tap_core/src/adapters/mock/escrow_adapter_mock.rs b/tap_core/src/adapters/mock/escrow_adapter_mock.rs deleted file mode 100644 index b52517eb..00000000 --- a/tap_core/src/adapters/mock/escrow_adapter_mock.rs +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2023-, Semiotic AI, Inc. -// SPDX-License-Identifier: Apache-2.0 - -use std::{collections::HashMap, sync::Arc}; - -use alloy_primitives::Address; -use async_trait::async_trait; -use tokio::sync::RwLock; - -use crate::adapters::escrow_adapter::EscrowAdapter; - -pub struct EscrowAdapterMock { - sender_escrow_storage: Arc>>, -} - -use thiserror::Error; -#[derive(Debug, Error)] -pub enum AdpaterErrorMock { - #[error("something went wrong: {error}")] - AdapterError { error: String }, -} - -impl EscrowAdapterMock { - pub fn new(sender_escrow_storage: Arc>>) -> Self { - EscrowAdapterMock { - sender_escrow_storage, - } - } - 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(AdpaterErrorMock::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<(), AdpaterErrorMock> { - 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(AdpaterErrorMock::AdapterError { - error: "Provided value is greater than existing escrow.".to_owned(), - }) - } -} - -#[async_trait] -impl EscrowAdapter for EscrowAdapterMock { - type AdapterError = AdpaterErrorMock; - 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/adapters/mock/executor_mock.rs b/tap_core/src/adapters/mock/executor_mock.rs index 52fadf9b..79585c0e 100644 --- a/tap_core/src/adapters/mock/executor_mock.rs +++ b/tap_core/src/adapters/mock/executor_mock.rs @@ -1,8 +1,6 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use super::escrow_adapter_mock::AdpaterErrorMock; -use super::receipt_storage_adapter_mock::AdapterErrorMock; use crate::adapters::escrow_adapter::EscrowAdapter; use crate::adapters::receipt_storage_adapter::{ safe_truncate_receipts, ReceiptRead, ReceiptStore, StoredReceipt, @@ -21,6 +19,14 @@ use tokio::sync::RwLock; pub type EscrowStorage = Arc>>; pub type QueryAppraisals = Arc>>; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum AdapterErrorMock { + #[error("something went wrong: {error}")] + AdapterError { error: String }, +} + #[derive(Clone)] pub struct ExecutorMock { /// local RAV store with rwlocks to allow sharing with other compenents as needed @@ -48,7 +54,7 @@ impl ExecutorMock { #[async_trait] impl RAVStore for ExecutorMock { - type AdapterError = AdpaterErrorMock; + type AdapterError = AdapterErrorMock; async fn update_last_rav(&self, rav: SignedRAV) -> Result<(), Self::AdapterError> { let mut rav_storage = self.rav_storage.write().await; @@ -59,7 +65,7 @@ impl RAVStore for ExecutorMock { #[async_trait] impl RAVRead for ExecutorMock { - type AdapterError = AdpaterErrorMock; + type AdapterError = AdapterErrorMock; async fn last_rav(&self) -> Result, Self::AdapterError> { Ok(self.rav_storage.read().await.clone()) @@ -131,12 +137,12 @@ impl ReceiptRead for ExecutorMock { } impl ExecutorMock { - pub async fn escrow(&self, sender_id: Address) -> Result { + 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(AdpaterErrorMock::AdapterError { + Err(AdapterErrorMock::AdapterError { error: "No escrow exists for provided sender ID.".to_owned(), }) } @@ -156,7 +162,7 @@ impl ExecutorMock { &self, sender_id: Address, value: u128, - ) -> Result<(), AdpaterErrorMock> { + ) -> Result<(), AdapterErrorMock> { let mut sender_escrow_storage = self.sender_escrow_storage.write().await; if let Some(current_value) = sender_escrow_storage.get(&sender_id) { @@ -166,7 +172,7 @@ impl ExecutorMock { return Ok(()); } } - Err(AdpaterErrorMock::AdapterError { + Err(AdapterErrorMock::AdapterError { error: "Provided value is greater than existing escrow.".to_owned(), }) } @@ -174,7 +180,7 @@ impl ExecutorMock { #[async_trait] impl EscrowAdapter for ExecutorMock { - type AdapterError = AdpaterErrorMock; + type AdapterError = AdapterErrorMock; async fn get_available_escrow(&self, sender_id: Address) -> Result { self.escrow(sender_id).await } diff --git a/tap_core/src/adapters/mock/rav_storage_adapter_mock.rs b/tap_core/src/adapters/mock/rav_storage_adapter_mock.rs index 603af06d..2c1403a0 100644 --- a/tap_core/src/adapters/mock/rav_storage_adapter_mock.rs +++ b/tap_core/src/adapters/mock/rav_storage_adapter_mock.rs @@ -4,7 +4,6 @@ use std::sync::Arc; use async_trait::async_trait; -use thiserror::Error; use tokio::sync::RwLock; use crate::{ @@ -12,6 +11,8 @@ use crate::{ tap_manager::SignedRAV, }; +use super::executor_mock::AdapterErrorMock; + /// `RAVStorageAdapterMock` is a mock implementation of the `RAVStorageAdapter` trait. /// /// It serves two main purposes: @@ -51,15 +52,9 @@ impl RAVStorageAdapterMock { } } -#[derive(Debug, Error)] -pub enum AdpaterErrorMock { - #[error("something went wrong: {error}")] - AdapterError { error: String }, -} - #[async_trait] impl RAVStore for RAVStorageAdapterMock { - type AdapterError = AdpaterErrorMock; + type AdapterError = AdapterErrorMock; async fn update_last_rav(&self, rav: SignedRAV) -> Result<(), Self::AdapterError> { let mut rav_storage = self.rav_storage.write().await; @@ -70,7 +65,7 @@ impl RAVStore for RAVStorageAdapterMock { #[async_trait] impl RAVRead for RAVStorageAdapterMock { - type AdapterError = AdpaterErrorMock; + type AdapterError = AdapterErrorMock; async fn last_rav(&self) -> Result, Self::AdapterError> { Ok(self.rav_storage.read().await.clone()) diff --git a/tap_core/src/adapters/mock/receipt_storage_adapter_mock.rs b/tap_core/src/adapters/mock/receipt_storage_adapter_mock.rs index af444c51..b9b5f9c8 100644 --- a/tap_core/src/adapters/mock/receipt_storage_adapter_mock.rs +++ b/tap_core/src/adapters/mock/receipt_storage_adapter_mock.rs @@ -13,6 +13,8 @@ use crate::{ tap_receipt::ReceivedReceipt, }; +use super::executor_mock::AdapterErrorMock; + pub struct ReceiptStorageAdapterMock { receipt_storage: Arc>>, unique_id: RwLock, @@ -78,13 +80,6 @@ impl ReceiptStorageAdapterMock { } } -use thiserror::Error; -#[derive(Debug, Error)] -pub enum AdapterErrorMock { - #[error("something went wrong: {error}")] - AdapterError { error: String }, -} - #[async_trait] impl ReceiptStore for ReceiptStorageAdapterMock { type AdapterError = AdapterErrorMock; diff --git a/tap_core/src/adapters/test/escrow_adapter_test.rs b/tap_core/src/adapters/test/escrow_adapter_test.rs index 1b35b9be..edefae6d 100644 --- a/tap_core/src/adapters/test/escrow_adapter_test.rs +++ b/tap_core/src/adapters/test/escrow_adapter_test.rs @@ -9,13 +9,17 @@ mod escrow_adapter_unit_test { use rstest::*; use tokio::sync::RwLock; - use crate::adapters::{escrow_adapter::EscrowAdapter, escrow_adapter_mock::EscrowAdapterMock}; + use crate::adapters::{escrow_adapter::EscrowAdapter, executor_mock::ExecutorMock}; #[rstest] #[tokio::test] async fn escrow_adapter_test() { - let escrow_storage = Arc::new(RwLock::new(HashMap::new())); - let mut escrow_adapter = EscrowAdapterMock::new(escrow_storage); + 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 mut escrow_adapter = + ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()); let wallet: LocalWallet = MnemonicBuilder::::default() .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") diff --git a/tap_core/src/adapters/test/rav_storage_adapter_test.rs b/tap_core/src/adapters/test/rav_storage_adapter_test.rs index 09677d96..0555eed0 100644 --- a/tap_core/src/adapters/test/rav_storage_adapter_test.rs +++ b/tap_core/src/adapters/test/rav_storage_adapter_test.rs @@ -3,6 +3,7 @@ #[cfg(test)] mod rav_storage_adapter_unit_test { + use std::collections::HashMap; use std::{str::FromStr, sync::Arc}; use alloy_primitives::Address; @@ -12,14 +13,15 @@ mod rav_storage_adapter_unit_test { use rstest::*; use tokio::sync::RwLock; - use crate::adapters::rav_storage_adapter::RAVRead; - use crate::adapters::{ - rav_storage_adapter::RAVStore, rav_storage_adapter_mock::RAVStorageAdapterMock, - }; - use crate::tap_eip712_domain; use crate::{ + adapters::{ + executor_mock::ExecutorMock, + rav_storage_adapter::{RAVRead, RAVStore}, + }, eip_712_signed_message::EIP712SignedMessage, - receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::Receipt, + receipt_aggregate_voucher::ReceiptAggregateVoucher, + tap_eip712_domain, + tap_receipt::Receipt, }; #[fixture] @@ -31,7 +33,11 @@ mod rav_storage_adapter_unit_test { #[tokio::test] async fn rav_storage_adapter_test(domain_separator: Eip712Domain) { let rav_storage = Arc::new(RwLock::new(None)); - let rav_storage_adapter = RAVStorageAdapterMock::new(rav_storage); + let receipt_storage = Arc::new(RwLock::new(HashMap::new())); + let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); + + let rav_storage_adapter = + ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()); let wallet: LocalWallet = MnemonicBuilder::::default() .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index 25a434ec..c25a2390 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -14,7 +14,6 @@ mod manager_unit_test { use super::super::Manager; use crate::{ adapters::{ - escrow_adapter_mock::EscrowAdapterMock, executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, receipt_storage_adapter::ReceiptRead, }, @@ -83,13 +82,6 @@ mod manager_unit_test { ) } - #[fixture] - fn escrow_adapters() -> (EscrowAdapterMock, EscrowStorage) { - let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); - let escrow_adapter = EscrowAdapterMock::new(Arc::clone(&sender_escrow_storage)); - (escrow_adapter, sender_escrow_storage) - } - #[rstest] // #[case::full_checks(get_full_list_of_checks())] // #[case::partial_checks(todo!())] 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 788ec53e..e2637fa0 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 @@ -9,7 +9,6 @@ use tokio::sync::RwLock; use crate::{ adapters::{ auditor_executor_mock::AuditorExecutorMock, - escrow_adapter_mock::EscrowAdapterMock, executor_mock::{EscrowStorage, QueryAppraisals}, receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }, @@ -91,13 +90,6 @@ fn receipt_adapters() -> ( (receipt_storage_adapter, receipt_storage) } -#[fixture] -fn escrow_adapters() -> (EscrowAdapterMock, Arc>>) { - let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); - let escrow_adapter = EscrowAdapterMock::new(Arc::clone(&sender_escrow_storage)); - (escrow_adapter, sender_escrow_storage) -} - #[fixture] fn auditor_executor() -> (AuditorExecutorMock, EscrowStorage, QueryAppraisals) { let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index ac9834c7..5d9f4308 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -26,8 +26,7 @@ use tokio::sync::RwLock; use tap_aggregator::{jsonrpsee_helpers, server as agg_server}; use tap_core::{ adapters::{ - escrow_adapter_mock::EscrowAdapterMock, executor_mock::ExecutorMock, - rav_storage_adapter_mock::RAVStorageAdapterMock, + executor_mock::ExecutorMock, rav_storage_adapter_mock::RAVStorageAdapterMock, receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }, checks::ReceiptCheck, @@ -147,12 +146,6 @@ fn available_escrow(query_price: Vec, num_batches: u64) -> u128 { (num_batches as u128) * query_price.into_iter().sum::() } -// The escrow adapter, a storage struct that the Indexer uses to track the available escrow for each Sender -#[fixture] -fn escrow_adapter() -> EscrowAdapterMock { - EscrowAdapterMock::new(Arc::new(RwLock::new(HashMap::new()))) -} - #[fixture] fn executor(receipt_storage: Arc>>) -> ExecutorMock { let rav_storage = Arc::new(RwLock::new(None)); From 0dfbf9700213eb18ef0aeb0aaa3ccaac11c7bc86 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 20:40:20 -0300 Subject: [PATCH 19/32] refactor: remove rav storage adapter Signed-off-by: Gustavo Inacio --- tap_core/src/adapters/mock.rs | 1 - .../adapters/mock/rav_storage_adapter_mock.rs | 73 ------------------- tap_integration_tests/tests/showcase.rs | 9 +-- 3 files changed, 1 insertion(+), 82 deletions(-) delete mode 100644 tap_core/src/adapters/mock/rav_storage_adapter_mock.rs diff --git a/tap_core/src/adapters/mock.rs b/tap_core/src/adapters/mock.rs index 972bedc1..ac10669a 100644 --- a/tap_core/src/adapters/mock.rs +++ b/tap_core/src/adapters/mock.rs @@ -3,5 +3,4 @@ pub mod auditor_executor_mock; pub mod executor_mock; -pub mod rav_storage_adapter_mock; pub mod receipt_storage_adapter_mock; diff --git a/tap_core/src/adapters/mock/rav_storage_adapter_mock.rs b/tap_core/src/adapters/mock/rav_storage_adapter_mock.rs deleted file mode 100644 index 2c1403a0..00000000 --- a/tap_core/src/adapters/mock/rav_storage_adapter_mock.rs +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2023-, Semiotic AI, Inc. -// SPDX-License-Identifier: Apache-2.0 - -use std::sync::Arc; - -use async_trait::async_trait; -use tokio::sync::RwLock; - -use crate::{ - adapters::rav_storage_adapter::{RAVRead, RAVStore}, - tap_manager::SignedRAV, -}; - -use super::executor_mock::AdapterErrorMock; - -/// `RAVStorageAdapterMock` is a mock implementation of the `RAVStorageAdapter` trait. -/// -/// It serves two main purposes: -/// -/// 1. **Unit Testing**: The `RAVStorageAdapterMock` is primarily intended to be used for unit tests, -/// providing a way to simulate the behavior of a real `RAVStorageAdapter` without requiring a real -/// implementation. By using a mock implementation, you can create predictable behaviors and -/// responses, enabling isolated and focused testing of the logic that depends on the `RAVStorageAdapter` trait. -/// -/// 2. **Example Implementation**: New users of the `RAVStorageAdapter` trait can look to -/// `RAVStorageAdapterMock` as a basic example of how to implement the trait. -/// -/// Note: This mock implementation is not suitable for production use. Its methods simply manipulate a -/// local `RwLock>`, and it provides no real error handling. -/// -/// # Usage -/// -/// To use `RAVStorageAdapterMock`, first create an `Arc>>`, then pass it to -/// `RAVStorageAdapterMock::new()`. Now, it can be used anywhere a `RAVStorageAdapter` is required. -/// -/// ```rust -/// use std::sync::{Arc}; -/// use tokio::sync::RwLock; -/// use tap_core::{tap_manager::SignedRAV, adapters::rav_storage_adapter_mock::RAVStorageAdapterMock}; -/// -/// let rav_storage: Arc>> = Arc::new(RwLock::new(None)); -/// let adapter = RAVStorageAdapterMock::new(rav_storage); -/// ``` -pub struct RAVStorageAdapterMock { - /// local RAV store with rwlocks to allow sharing with other compenents as needed - rav_storage: Arc>>, -} - -impl RAVStorageAdapterMock { - pub fn new(rav_storage: Arc>>) -> Self { - RAVStorageAdapterMock { rav_storage } - } -} - -#[async_trait] -impl RAVStore for RAVStorageAdapterMock { - type AdapterError = AdapterErrorMock; - - async fn update_last_rav(&self, rav: SignedRAV) -> Result<(), Self::AdapterError> { - let mut rav_storage = self.rav_storage.write().await; - *rav_storage = Some(rav); - Ok(()) - } -} - -#[async_trait] -impl RAVRead for RAVStorageAdapterMock { - type AdapterError = AdapterErrorMock; - - async fn last_rav(&self) -> Result, Self::AdapterError> { - Ok(self.rav_storage.read().await.clone()) - } -} diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 5d9f4308..3fe55dbc 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -26,8 +26,7 @@ use tokio::sync::RwLock; use tap_aggregator::{jsonrpsee_helpers, server as agg_server}; use tap_core::{ adapters::{ - executor_mock::ExecutorMock, rav_storage_adapter_mock::RAVStorageAdapterMock, - receipt_storage_adapter_mock::ReceiptStorageAdapterMock, + executor_mock::ExecutorMock, receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }, checks::ReceiptCheck, eip_712_signed_message::EIP712SignedMessage, @@ -167,12 +166,6 @@ fn receipt_storage_adapter( ReceiptStorageAdapterMock::new(receipt_storage) } -// A structure for storing received RAVs. -#[fixture] -fn rav_storage_adapter() -> RAVStorageAdapterMock { - RAVStorageAdapterMock::new(Arc::new(RwLock::new(None))) -} - // These are the checks that the Indexer will perform when requesting a RAV. // Testing with all checks enabled. #[fixture] From e782b05cd3fc729e39f8fe5100aa99e245dc9f3f Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 20:47:39 -0300 Subject: [PATCH 20/32] refactor: remove receipt storage adapter mock Signed-off-by: Gustavo Inacio --- tap_core/src/adapters/mock.rs | 1 - tap_core/src/adapters/mock/executor_mock.rs | 55 +++++++ .../mock/receipt_storage_adapter_mock.rs | 148 ------------------ .../test/receipt_storage_adapter_test.rs | 26 +-- .../received_receipt_unit_test.rs | 17 +- tap_integration_tests/tests/showcase.rs | 11 +- 6 files changed, 75 insertions(+), 183 deletions(-) delete mode 100644 tap_core/src/adapters/mock/receipt_storage_adapter_mock.rs diff --git a/tap_core/src/adapters/mock.rs b/tap_core/src/adapters/mock.rs index ac10669a..df1b69cf 100644 --- a/tap_core/src/adapters/mock.rs +++ b/tap_core/src/adapters/mock.rs @@ -3,4 +3,3 @@ pub mod auditor_executor_mock; pub mod executor_mock; -pub mod receipt_storage_adapter_mock; diff --git a/tap_core/src/adapters/mock/executor_mock.rs b/tap_core/src/adapters/mock/executor_mock.rs index 79585c0e..78ea36c1 100644 --- a/tap_core/src/adapters/mock/executor_mock.rs +++ b/tap_core/src/adapters/mock/executor_mock.rs @@ -50,6 +50,61 @@ impl ExecutorMock { sender_escrow_storage, } } + + pub async fn retrieve_receipt_by_id( + &self, + receipt_id: u64, + ) -> Result { + let receipt_storage = self.receipt_storage.read().await; + + receipt_storage + .get(&receipt_id) + .cloned() + .ok_or(AdapterErrorMock::AdapterError { + error: "No receipt found with ID".to_owned(), + }) + } + + pub async fn retrieve_receipts_by_timestamp( + &self, + timestamp_ns: u64, + ) -> Result, AdapterErrorMock> { + let receipt_storage = self.receipt_storage.read().await; + Ok(receipt_storage + .iter() + .filter(|(_, rx_receipt)| { + rx_receipt.signed_receipt().message.timestamp_ns == timestamp_ns + }) + .map(|(&id, rx_receipt)| (id, rx_receipt.clone())) + .collect()) + } + + pub async fn retrieve_receipts_upto_timestamp( + &self, + timestamp_ns: u64, + ) -> Result, AdapterErrorMock> { + self.retrieve_receipts_in_timestamp_range(..=timestamp_ns, None) + .await + } + + pub async fn remove_receipt_by_id(&mut self, receipt_id: u64) -> Result<(), AdapterErrorMock> { + let mut receipt_storage = self.receipt_storage.write().await; + receipt_storage + .remove(&receipt_id) + .map(|_| ()) + .ok_or(AdapterErrorMock::AdapterError { + error: "No receipt found with ID".to_owned(), + }) + } + pub async fn remove_receipts_by_ids( + &mut self, + receipt_ids: &[u64], + ) -> Result<(), AdapterErrorMock> { + for receipt_id in receipt_ids { + self.remove_receipt_by_id(*receipt_id).await?; + } + Ok(()) + } } #[async_trait] diff --git a/tap_core/src/adapters/mock/receipt_storage_adapter_mock.rs b/tap_core/src/adapters/mock/receipt_storage_adapter_mock.rs deleted file mode 100644 index b9b5f9c8..00000000 --- a/tap_core/src/adapters/mock/receipt_storage_adapter_mock.rs +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright 2023-, Semiotic AI, Inc. -// SPDX-License-Identifier: Apache-2.0 - -use std::{collections::HashMap, ops::RangeBounds, sync::Arc}; - -use async_trait::async_trait; -use tokio::sync::RwLock; - -use crate::{ - adapters::receipt_storage_adapter::{ - safe_truncate_receipts, ReceiptRead, ReceiptStore, StoredReceipt, - }, - tap_receipt::ReceivedReceipt, -}; - -use super::executor_mock::AdapterErrorMock; - -pub struct ReceiptStorageAdapterMock { - receipt_storage: Arc>>, - unique_id: RwLock, -} - -impl ReceiptStorageAdapterMock { - pub fn new(receipt_storage: Arc>>) -> Self { - Self { - receipt_storage, - unique_id: RwLock::new(0u64), - } - } - pub async fn retrieve_receipt_by_id( - &self, - receipt_id: u64, - ) -> Result { - let receipt_storage = self.receipt_storage.read().await; - - receipt_storage - .get(&receipt_id) - .cloned() - .ok_or(AdapterErrorMock::AdapterError { - error: "No receipt found with ID".to_owned(), - }) - } - pub async fn retrieve_receipts_by_timestamp( - &self, - timestamp_ns: u64, - ) -> Result, AdapterErrorMock> { - let receipt_storage = self.receipt_storage.read().await; - Ok(receipt_storage - .iter() - .filter(|(_, rx_receipt)| { - rx_receipt.signed_receipt().message.timestamp_ns == timestamp_ns - }) - .map(|(&id, rx_receipt)| (id, rx_receipt.clone())) - .collect()) - } - pub async fn retrieve_receipts_upto_timestamp( - &self, - timestamp_ns: u64, - ) -> Result, AdapterErrorMock> { - self.retrieve_receipts_in_timestamp_range(..=timestamp_ns, None) - .await - } - pub async fn remove_receipt_by_id(&mut self, receipt_id: u64) -> Result<(), AdapterErrorMock> { - let mut receipt_storage = self.receipt_storage.write().await; - receipt_storage - .remove(&receipt_id) - .map(|_| ()) - .ok_or(AdapterErrorMock::AdapterError { - error: "No receipt found with ID".to_owned(), - }) - } - pub async fn remove_receipts_by_ids( - &mut self, - receipt_ids: &[u64], - ) -> Result<(), AdapterErrorMock> { - for receipt_id in receipt_ids { - self.remove_receipt_by_id(*receipt_id).await?; - } - Ok(()) - } -} - -#[async_trait] -impl ReceiptStore for ReceiptStorageAdapterMock { - type AdapterError = AdapterErrorMock; - async fn store_receipt(&self, receipt: ReceivedReceipt) -> Result { - let mut id_pointer = self.unique_id.write().await; - let id_previous = *id_pointer; - let mut receipt_storage = self.receipt_storage.write().await; - receipt_storage.insert(*id_pointer, receipt); - *id_pointer += 1; - Ok(id_previous) - } - async fn update_receipt_by_id( - &self, - receipt_id: u64, - receipt: ReceivedReceipt, - ) -> Result<(), Self::AdapterError> { - let mut receipt_storage = self.receipt_storage.write().await; - - if !receipt_storage.contains_key(&receipt_id) { - return Err(AdapterErrorMock::AdapterError { - error: "Invalid receipt_id".to_owned(), - }); - }; - - receipt_storage.insert(receipt_id, receipt); - *self.unique_id.write().await += 1; - Ok(()) - } - async fn remove_receipts_in_timestamp_range + std::marker::Send>( - &self, - timestamp_ns: R, - ) -> Result<(), Self::AdapterError> { - let mut receipt_storage = self.receipt_storage.write().await; - receipt_storage.retain(|_, rx_receipt| { - !timestamp_ns.contains(&rx_receipt.signed_receipt().message.timestamp_ns) - }); - Ok(()) - } -} - -#[async_trait] -impl ReceiptRead for ReceiptStorageAdapterMock { - type AdapterError = AdapterErrorMock; - async fn retrieve_receipts_in_timestamp_range + std::marker::Send>( - &self, - timestamp_range_ns: R, - limit: Option, - ) -> Result, Self::AdapterError> { - let receipt_storage = self.receipt_storage.read().await; - let mut receipts_in_range: Vec<(u64, ReceivedReceipt)> = receipt_storage - .iter() - .filter(|(_, rx_receipt)| { - timestamp_range_ns.contains(&rx_receipt.signed_receipt().message.timestamp_ns) - }) - .map(|(&id, rx_receipt)| (id, rx_receipt.clone())) - .collect(); - - if limit.is_some_and(|limit| receipts_in_range.len() > limit as usize) { - safe_truncate_receipts(&mut receipts_in_range, limit.unwrap()); - - Ok(receipts_in_range.into_iter().map(|r| r.into()).collect()) - } else { - Ok(receipts_in_range.into_iter().map(|r| r.into()).collect()) - } - } -} diff --git a/tap_core/src/adapters/test/receipt_storage_adapter_test.rs b/tap_core/src/adapters/test/receipt_storage_adapter_test.rs index 14bc048e..563bbeef 100644 --- a/tap_core/src/adapters/test/receipt_storage_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_storage_adapter_test.rs @@ -16,15 +16,13 @@ mod receipt_storage_adapter_unit_test { use rstest::*; use tokio::sync::RwLock; - use crate::adapters::{ - receipt_storage_adapter::ReceiptStore, - receipt_storage_adapter_mock::ReceiptStorageAdapterMock, + use crate::{ + adapters::{executor_mock::ExecutorMock, receipt_storage_adapter::ReceiptStore}, + checks::{tests::get_full_list_of_checks, ReceiptCheck}, + eip_712_signed_message::EIP712SignedMessage, + tap_eip712_domain, + tap_receipt::{Receipt, ReceivedReceipt}, }; - use crate::checks::tests::get_full_list_of_checks; - use crate::checks::ReceiptCheck; - use crate::tap_eip712_domain; - use crate::tap_receipt::ReceivedReceipt; - use crate::{eip_712_signed_message::EIP712SignedMessage, tap_receipt::Receipt}; #[fixture] fn domain_separator() -> Eip712Domain { @@ -45,8 +43,12 @@ mod receipt_storage_adapter_unit_test { #[rstest] #[tokio::test] async fn receipt_adapter_test(domain_separator: Eip712Domain, checks: Vec) { + let rav_storage = Arc::new(RwLock::new(None)); let receipt_storage = Arc::new(RwLock::new(HashMap::new())); - let mut receipt_adapter = ReceiptStorageAdapterMock::new(receipt_storage); + let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); + + let mut receipt_adapter = + ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()); let wallet: LocalWallet = MnemonicBuilder::::default() .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") @@ -106,8 +108,12 @@ mod receipt_storage_adapter_unit_test { #[rstest] #[tokio::test] async fn multi_receipt_adapter_test(domain_separator: Eip712Domain, checks: Vec) { + let rav_storage = Arc::new(RwLock::new(None)); let receipt_storage = Arc::new(RwLock::new(HashMap::new())); - let mut receipt_adapter = ReceiptStorageAdapterMock::new(receipt_storage); + let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); + + let mut receipt_adapter = + ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()); let wallet: LocalWallet = MnemonicBuilder::::default() .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") 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 e2637fa0..7968b388 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 @@ -10,7 +10,6 @@ use crate::{ adapters::{ auditor_executor_mock::AuditorExecutorMock, executor_mock::{EscrowStorage, QueryAppraisals}, - receipt_storage_adapter_mock::ReceiptStorageAdapterMock, }, checks::{tests::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, @@ -80,14 +79,8 @@ fn sender_ids() -> Vec
{ } #[fixture] -fn receipt_adapters() -> ( - ReceiptStorageAdapterMock, - Arc>>, -) { - let receipt_storage = Arc::new(RwLock::new(HashMap::new())); - let receipt_storage_adapter = ReceiptStorageAdapterMock::new(Arc::clone(&receipt_storage)); - - (receipt_storage_adapter, receipt_storage) +fn receipt_storage() -> Arc>> { + Arc::new(RwLock::new(HashMap::new())) } #[fixture] @@ -111,14 +104,10 @@ fn domain_separator() -> Eip712Domain { fn checks( domain_separator: Eip712Domain, auditor_executor: (AuditorExecutorMock, EscrowStorage, QueryAppraisals), - receipt_adapters: ( - ReceiptStorageAdapterMock, - Arc>>, - ), + receipt_storage: Arc>>, allocation_ids: Vec
, sender_ids: Vec
, ) -> Vec { - let (_, receipt_storage) = receipt_adapters; let (_, _escrow_storage, query_appraisal_storage) = auditor_executor; get_full_list_of_checks( domain_separator, diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 3fe55dbc..a53ab054 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -25,9 +25,7 @@ use tokio::sync::RwLock; use tap_aggregator::{jsonrpsee_helpers, server as agg_server}; use tap_core::{ - adapters::{ - executor_mock::ExecutorMock, receipt_storage_adapter_mock::ReceiptStorageAdapterMock, - }, + adapters::executor_mock::ExecutorMock, checks::ReceiptCheck, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, @@ -158,13 +156,6 @@ fn executor(receipt_storage: Arc>>) -> Exec fn receipt_storage() -> Arc>> { Arc::new(RwLock::new(HashMap::new())) } -// A storage struct used by the Indexer to store Receipts, all recieved receipts are stored here. There are flags which indicate the validity of the receipt. -#[fixture] -fn receipt_storage_adapter( - receipt_storage: Arc>>, -) -> ReceiptStorageAdapterMock { - ReceiptStorageAdapterMock::new(receipt_storage) -} // These are the checks that the Indexer will perform when requesting a RAV. // Testing with all checks enabled. From 09a15f1071fb515fcedb392f9cb514c8f59b4fc3 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 20:53:40 -0300 Subject: [PATCH 21/32] refactor: remove auditor executor mock Signed-off-by: Gustavo Inacio --- tap_core/src/adapters/mock.rs | 1 - .../adapters/mock/auditor_executor_mock.rs | 81 ------------------- .../received_receipt_unit_test.rs | 39 +++++---- 3 files changed, 19 insertions(+), 102 deletions(-) delete mode 100644 tap_core/src/adapters/mock/auditor_executor_mock.rs 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); From a9921816f28d01d94acfb04f57ed89d2d6621944 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 21:06:38 -0300 Subject: [PATCH 22/32] chore: add license Signed-off-by: Gustavo Inacio --- tap_core/src/checks/mod.rs | 3 +++ .../tap_receipt/received_receipt/received_receipt_unit_test.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs index a236cd09..7b4e9303 100644 --- a/tap_core/src/checks/mod.rs +++ b/tap_core/src/checks/mod.rs @@ -1,3 +1,6 @@ +// Copyright 2023-, Semiotic AI, Inc. +// SPDX-License-Identifier: Apache-2.0 + use crate::tap_receipt::{Checking, ReceiptError, ReceiptResult, ReceiptWithState}; use serde::{Deserialize, Serialize}; use std::sync::Arc; 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 786285be..857e0843 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 @@ -1,3 +1,6 @@ +// Copyright 2023-, Semiotic AI, Inc. +// SPDX-License-Identifier: Apache-2.0 + use std::{collections::HashMap, str::FromStr, sync::Arc}; use alloy_primitives::Address; From bc86d16e84e250a626962da0345985ef9bceec21 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Mon, 4 Mar 2024 21:13:41 -0300 Subject: [PATCH 23/32] style: fix fmt and clippy Signed-off-by: Gustavo Inacio --- tap_aggregator/src/aggregator.rs | 25 +++++-------------- .../timeline_aggretion_protocol_benchmark.rs | 2 +- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/tap_aggregator/src/aggregator.rs b/tap_aggregator/src/aggregator.rs index bbd6a6ca..7479cd9c 100644 --- a/tap_aggregator/src/aggregator.rs +++ b/tap_aggregator/src/aggregator.rs @@ -199,23 +199,20 @@ mod tests { domain_separator: Eip712Domain, ) { // Create 2 different receipts - let mut receipts = Vec::new(); - receipts.push( + let receipts = vec![ EIP712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) .unwrap(), - ); - receipts.push( EIP712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 43).unwrap(), &keys.0, ) .unwrap(), - ); + ]; let res = aggregator::check_signatures_unique(&receipts); assert!(res.is_ok()); @@ -299,31 +296,26 @@ mod tests { allocation_ids: Vec
, domain_separator: Eip712Domain, ) { - let mut receipts = Vec::new(); - receipts.push( + let receipts = vec![ EIP712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) .unwrap(), - ); - receipts.push( EIP712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 43).unwrap(), &keys.0, ) .unwrap(), - ); - receipts.push( EIP712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[1], 44).unwrap(), &keys.0, ) .unwrap(), - ); + ]; let res = aggregator::check_allocation_id(&receipts, allocation_ids[0]); @@ -338,31 +330,26 @@ mod tests { allocation_ids: Vec
, domain_separator: Eip712Domain, ) { - let mut receipts = Vec::new(); - receipts.push( + let receipts = vec![ EIP712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0, ) .unwrap(), - ); - receipts.push( EIP712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 43).unwrap(), &keys.0, ) .unwrap(), - ); - receipts.push( EIP712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 44).unwrap(), &keys.0, ) .unwrap(), - ); + ]; let res = aggregator::check_allocation_id(&receipts, allocation_ids[0]); diff --git a/tap_core/benches/timeline_aggretion_protocol_benchmark.rs b/tap_core/benches/timeline_aggretion_protocol_benchmark.rs index 326db0f5..37cbc183 100644 --- a/tap_core/benches/timeline_aggretion_protocol_benchmark.rs +++ b/tap_core/benches/timeline_aggretion_protocol_benchmark.rs @@ -11,7 +11,7 @@ use std::str::FromStr; use alloy_primitives::Address; -use alloy_sol_types::{Eip712Domain}; +use alloy_sol_types::Eip712Domain; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use ethers::signers::{LocalWallet, Signer, Wallet}; use ethers_core::k256::ecdsa::SigningKey; From 568144fa1b945b78139761c53a18dc83b3128326 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Tue, 5 Mar 2024 13:00:52 -0300 Subject: [PATCH 24/32] test: fix manager and received receipt tests Signed-off-by: Gustavo Inacio --- tap_core/src/adapters/mock/executor_mock.rs | 12 +- tap_core/src/checks/mod.rs | 9 +- tap_core/src/tap_manager/test/manager_test.rs | 889 +++++++++--------- .../received_receipt_unit_test.rs | 127 ++- 4 files changed, 550 insertions(+), 487 deletions(-) diff --git a/tap_core/src/adapters/mock/executor_mock.rs b/tap_core/src/adapters/mock/executor_mock.rs index 78ea36c1..f8f62ed8 100644 --- a/tap_core/src/adapters/mock/executor_mock.rs +++ b/tap_core/src/adapters/mock/executor_mock.rs @@ -18,6 +18,8 @@ use tokio::sync::RwLock; pub type EscrowStorage = Arc>>; pub type QueryAppraisals = Arc>>; +pub type ReceiptStorage = Arc>>; +pub type RAVStorage = Arc>>; use thiserror::Error; @@ -30,8 +32,8 @@ pub enum AdapterErrorMock { #[derive(Clone)] pub struct ExecutorMock { /// local RAV store with rwlocks to allow sharing with other compenents as needed - rav_storage: Arc>>, - receipt_storage: Arc>>, + rav_storage: RAVStorage, + receipt_storage: ReceiptStorage, unique_id: Arc>, sender_escrow_storage: EscrowStorage, @@ -39,9 +41,9 @@ pub struct ExecutorMock { impl ExecutorMock { pub fn new( - rav_storage: Arc>>, - receipt_storage: Arc>>, - sender_escrow_storage: Arc>>, + rav_storage: RAVStorage, + receipt_storage: ReceiptStorage, + sender_escrow_storage: EscrowStorage, ) -> Self { ExecutorMock { rav_storage, diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs index 7b4e9303..9dd62864 100644 --- a/tap_core/src/checks/mod.rs +++ b/tap_core/src/checks/mod.rs @@ -139,13 +139,14 @@ pub mod tests { async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { let receipt_storage = self.receipt_storage.read().await; // let receipt_id = receipt. - receipt_storage + let unique = receipt_storage .iter() .all(|(_stored_receipt_id, stored_receipt)| { stored_receipt.signed_receipt().message != receipt.signed_receipt().message - }) - .then_some(()) - .ok_or(ReceiptError::NonUniqueReceipt) + || stored_receipt.query_id() == receipt.query_id + }); + + unique.then_some(()).ok_or(ReceiptError::NonUniqueReceipt) } async fn check_batch( diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index c25a2390..b957193b 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -1,112 +1,197 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -#[cfg(test)] -#[allow(clippy::too_many_arguments)] -mod manager_unit_test { - use std::{collections::HashMap, str::FromStr, sync::Arc}; - - use alloy_primitives::Address; - use alloy_sol_types::Eip712Domain; - use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer}; - use rstest::*; - use tokio::sync::RwLock; - - use super::super::Manager; - use crate::{ - adapters::{ - executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, - receipt_storage_adapter::ReceiptRead, - }, - checks::ReceiptCheck, - eip_712_signed_message::EIP712SignedMessage, - get_current_timestamp_u64_ns, tap_eip712_domain, - tap_receipt::Receipt, - }; - - #[fixture] - fn keys() -> (LocalWallet, Address) { - let wallet: LocalWallet = MnemonicBuilder::::default() - .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") - .build() - .unwrap(); - // Alloy library does not have feature parity with ethers library (yet) This workaround is needed to get the address - // to convert to an alloy Address. This will not be needed when the alloy library has wallet support. - let address: [u8; 20] = wallet.address().into(); - - (wallet, address.into()) - } +use std::{collections::HashMap, ops::Range, str::FromStr, sync::Arc}; + +use alloy_primitives::Address; +use alloy_sol_types::Eip712Domain; +use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer}; +use rstest::*; +use tokio::sync::RwLock; + +use super::super::Manager; +use crate::{ + adapters::{ + executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals, RAVStorage, ReceiptStorage}, + receipt_storage_adapter::ReceiptRead, + }, + checks::{tests::get_full_list_of_checks, ReceiptCheck}, + eip_712_signed_message::EIP712SignedMessage, + get_current_timestamp_u64_ns, tap_eip712_domain, + tap_receipt::Receipt, +}; + +#[fixture] +fn keys() -> (LocalWallet, Address) { + let wallet: LocalWallet = MnemonicBuilder::::default() + .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") + .build() + .unwrap(); + // Alloy library does not have feature parity with ethers library (yet) This workaround is needed to get the address + // to convert to an alloy Address. This will not be needed when the alloy library has wallet support. + let address: [u8; 20] = wallet.address().into(); - #[fixture] - fn allocation_ids() -> Vec
{ - vec![ - Address::from_str("0xabababababababababababababababababababab").unwrap(), - Address::from_str("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddead").unwrap(), - Address::from_str("0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef").unwrap(), - Address::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(), - ] - } + (wallet, address.into()) +} - #[fixture] - fn sender_ids() -> Vec
{ - vec![ - Address::from_str("0xfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb").unwrap(), - Address::from_str("0xfafafafafafafafafafafafafafafafafafafafa").unwrap(), - Address::from_str("0xadadadadadadadadadadadadadadadadadadadad").unwrap(), - keys().1, - ] - } +#[fixture] +fn allocation_ids() -> Vec
{ + vec![ + Address::from_str("0xabababababababababababababababababababab").unwrap(), + Address::from_str("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddead").unwrap(), + Address::from_str("0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef").unwrap(), + Address::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(), + ] +} - #[fixture] - fn full_list_of_checks() -> Vec { - vec![] - } +#[fixture] +fn sender_ids() -> Vec
{ + vec![ + Address::from_str("0xfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb").unwrap(), + Address::from_str("0xfafafafafafafafafafafafafafafafafafafafa").unwrap(), + Address::from_str("0xadadadadadadadadadadadadadadadadadadadad").unwrap(), + keys().1, + ] +} - #[fixture] - fn domain_separator() -> Eip712Domain { - tap_eip712_domain(1, Address::from([0x11u8; 20])) - } +#[fixture] +fn receipt_storage() -> ReceiptStorage { + Arc::new(RwLock::new(HashMap::new())) +} - #[fixture] - fn executor_mock() -> (ExecutorMock, EscrowStorage, QueryAppraisals) { - let rav_storage = Arc::new(RwLock::new(None)); - let receipt_storage = Arc::new(RwLock::new(HashMap::new())); +#[fixture] +fn query_appraisal_storage() -> QueryAppraisals { + Arc::new(RwLock::new(HashMap::new())) +} - let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); +#[fixture] +fn rav_storage() -> RAVStorage { + Arc::new(RwLock::new(None)) +} - let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); +#[fixture] +fn escrow_storage() -> EscrowStorage { + Arc::new(RwLock::new(HashMap::new())) +} - ( - ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()), - sender_escrow_storage, - query_appraisal_storage, - ) +#[fixture] +fn domain_separator() -> Eip712Domain { + tap_eip712_domain(1, Address::from([0x11u8; 20])) +} + +struct ExecutorFixture { + executor: ExecutorMock, + escrow_storage: EscrowStorage, + query_appraisals: QueryAppraisals, + checks: Vec, +} + +#[fixture] +fn executor_mock( + domain_separator: Eip712Domain, + allocation_ids: Vec
, + sender_ids: Vec
, + receipt_storage: ReceiptStorage, + query_appraisal_storage: QueryAppraisals, + rav_storage: RAVStorage, + escrow_storage: EscrowStorage, +) -> ExecutorFixture { + let executor = ExecutorMock::new(rav_storage, receipt_storage.clone(), escrow_storage.clone()); + + let checks = get_full_list_of_checks( + domain_separator, + sender_ids.iter().cloned().collect(), + Arc::new(RwLock::new(allocation_ids.iter().cloned().collect())), + receipt_storage, + query_appraisal_storage.clone(), + ); + + ExecutorFixture { + executor, + escrow_storage, + query_appraisals: query_appraisal_storage, + checks, } +} - #[rstest] - // #[case::full_checks(get_full_list_of_checks())] - // #[case::partial_checks(todo!())] - #[case::no_checks(Vec::::new())] - #[tokio::test] - async fn manager_verify_and_store_varying_initial_checks( - executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), - keys: (LocalWallet, Address), - allocation_ids: Vec
, - domain_separator: Eip712Domain, - #[case] initial_checks: Vec, - full_list_of_checks: Vec, - ) { - 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 manager = Manager::new( - domain_separator.clone(), - executor, - full_list_of_checks, - starting_min_timestamp, - ); +#[rstest] +#[case::full_checks(0..5)] +#[case::partial_checks(0..2)] +#[case::no_checks(0..0)] +#[tokio::test] +async fn manager_verify_and_store_varying_initial_checks( + keys: (LocalWallet, Address), + allocation_ids: Vec
, + domain_separator: Eip712Domain, + #[case] range: Range, + executor_mock: ExecutorFixture, +) { + let ExecutorFixture { + executor, + checks, + query_appraisals, + escrow_storage, + .. + } = executor_mock; + // give receipt 5 second variance for min start time + let starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; + + let manager = Manager::new( + domain_separator.clone(), + executor, + checks.clone(), + starting_min_timestamp, + ); + + let query_id = 1; + let value = 20u128; + let signed_receipt = EIP712SignedMessage::new( + &domain_separator, + Receipt::new(allocation_ids[0], value).unwrap(), + &keys.0, + ) + .unwrap(); + query_appraisals.write().await.insert(query_id, value); + escrow_storage.write().await.insert(keys.1, 999999); + + assert!(manager + .verify_and_store_receipt(signed_receipt, query_id, &checks[range]) + .await + .is_ok()); +} - let query_id = 1; +#[rstest] +#[case::full_checks(0..5)] +#[case::partial_checks(0..2)] +#[case::no_checks(0..0)] +#[tokio::test] +async fn manager_create_rav_request_all_valid_receipts( + keys: (LocalWallet, Address), + allocation_ids: Vec
, + domain_separator: Eip712Domain, + #[case] range: Range, + executor_mock: ExecutorFixture, +) { + let ExecutorFixture { + executor, + checks, + query_appraisals, + escrow_storage, + .. + } = executor_mock; + let initial_checks = &checks[range]; + // give receipt 5 second variance for min start time + let starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; + + let manager = Manager::new( + domain_separator.clone(), + executor, + checks.clone(), + starting_min_timestamp, + ); + escrow_storage.write().await.insert(keys.1, 999999); + + let mut stored_signed_receipts = Vec::new(); + for query_id in 0..10 { let value = 20u128; let signed_receipt = EIP712SignedMessage::new( &domain_separator, @@ -114,368 +199,316 @@ mod manager_unit_test { &keys.0, ) .unwrap(); - query_appraisal_storage - .write() - .await - .insert(query_id, value); - escrow_storage.write().await.insert(keys.1, 999999); - + stored_signed_receipts.push(signed_receipt.clone()); + query_appraisals.write().await.insert(query_id, value); assert!(manager - .verify_and_store_receipt(signed_receipt, query_id, initial_checks.as_slice()) + .verify_and_store_receipt(signed_receipt, query_id, initial_checks) .await .is_ok()); } - - #[rstest] - // #[case::full_checks(get_full_list_of_checks())] - // #[case::partial_checks(vec![ReceiptCheck::CheckSignature])] - #[case::no_checks(Vec::::new())] - #[tokio::test] - async fn manager_create_rav_request_all_valid_receipts( - executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), - keys: (LocalWallet, Address), - allocation_ids: Vec
, - domain_separator: Eip712Domain, - #[case] initial_checks: Vec, - full_list_of_checks: Vec, - ) { - 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 manager = Manager::new( - domain_separator.clone(), - executor, - full_list_of_checks, - starting_min_timestamp, - ); - escrow_storage.write().await.insert(keys.1, 999999); - - let mut stored_signed_receipts = Vec::new(); - for query_id in 0..10 { - let value = 20u128; - let signed_receipt = EIP712SignedMessage::new( - &domain_separator, - Receipt::new(allocation_ids[0], value).unwrap(), - &keys.0, - ) + let rav_request_result = manager.create_rav_request(0, None).await; + println!("{:?}", rav_request_result); + assert!(rav_request_result.is_ok()); + + let rav_request = rav_request_result.unwrap(); + // all passing + assert_eq!( + rav_request.valid_receipts.len(), + stored_signed_receipts.len() + ); + // no failing + assert_eq!(rav_request.invalid_receipts.len(), 0); + + let signed_rav = + EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) .unwrap(); - stored_signed_receipts.push(signed_receipt.clone()); - query_appraisal_storage - .write() - .await - .insert(query_id, value); - assert!(manager - .verify_and_store_receipt(signed_receipt, query_id, initial_checks.as_slice()) - .await - .is_ok()); - } - let rav_request_result = manager.create_rav_request(0, None).await; - assert!(rav_request_result.is_ok()); - - let rav_request = rav_request_result.unwrap(); - // all passing - assert_eq!( - rav_request.valid_receipts.len(), - stored_signed_receipts.len() - ); - // no failing - assert_eq!(rav_request.invalid_receipts.len(), 0); + assert!(manager + .verify_and_store_rav( + rav_request.expected_rav, + signed_rav, + |address: Address| async move { Ok(keys.1 == address) } + ) + .await + .is_ok()); +} - let signed_rav = - EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) - .unwrap(); +#[rstest] +#[case::full_checks(0..5)] +#[case::partial_checks(0..2)] +#[case::no_checks(0..0)] +#[tokio::test] +async fn manager_create_multiple_rav_requests_all_valid_receipts( + keys: (LocalWallet, Address), + allocation_ids: Vec
, + domain_separator: Eip712Domain, + #[case] range: Range, + executor_mock: ExecutorFixture, +) { + let ExecutorFixture { + executor, + checks, + query_appraisals, + escrow_storage, + .. + } = executor_mock; + let initial_checks = &checks[range]; + // give receipt 5 second variance for min start time + let starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; + + let manager = Manager::new( + domain_separator.clone(), + executor, + checks.clone(), + starting_min_timestamp, + ); + + escrow_storage.write().await.insert(keys.1, 999999); + + let mut stored_signed_receipts = Vec::new(); + let mut expected_accumulated_value = 0; + for query_id in 0..10 { + let value = 20u128; + let signed_receipt = EIP712SignedMessage::new( + &domain_separator, + Receipt::new(allocation_ids[0], value).unwrap(), + &keys.0, + ) + .unwrap(); + stored_signed_receipts.push(signed_receipt.clone()); + query_appraisals.write().await.insert(query_id, value); assert!(manager - .verify_and_store_rav( - rav_request.expected_rav, - signed_rav, - |address: Address| async move { Ok(keys.1 == address) } - ) + .verify_and_store_receipt(signed_receipt, query_id, initial_checks) .await .is_ok()); + expected_accumulated_value += value; } - - #[rstest] - // #[case::full_checks(get_full_list_of_checks())] - // #[case::partial_checks(vec![ReceiptCheck::CheckSignature])] - #[case::no_checks(Vec::::new())] - #[tokio::test] - async fn manager_create_multiple_rav_requests_all_valid_receipts( - executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), - keys: (LocalWallet, Address), - allocation_ids: Vec
, - domain_separator: Eip712Domain, - #[case] initial_checks: Vec, - full_list_of_checks: Vec, - ) { - 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 manager = Manager::new( - domain_separator.clone(), - executor, - full_list_of_checks, - starting_min_timestamp, - ); - - escrow_storage.write().await.insert(keys.1, 999999); - - let mut stored_signed_receipts = Vec::new(); - let mut expected_accumulated_value = 0; - for query_id in 0..10 { - let value = 20u128; - let signed_receipt = EIP712SignedMessage::new( - &domain_separator, - Receipt::new(allocation_ids[0], value).unwrap(), - &keys.0, - ) + let rav_request_result = manager.create_rav_request(0, None).await; + assert!(rav_request_result.is_ok()); + + let rav_request = rav_request_result.unwrap(); + // all receipts passing + assert_eq!( + rav_request.valid_receipts.len(), + stored_signed_receipts.len() + ); + // no receipts failing + assert_eq!(rav_request.invalid_receipts.len(), 0); + // accumulated value is correct + assert_eq!( + rav_request.expected_rav.valueAggregate, + expected_accumulated_value + ); + // no previous rav + assert!(rav_request.previous_rav.is_none()); + + let signed_rav = + EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) .unwrap(); - stored_signed_receipts.push(signed_receipt.clone()); - query_appraisal_storage - .write() - .await - .insert(query_id, value); - assert!(manager - .verify_and_store_receipt(signed_receipt, query_id, initial_checks.as_slice()) - .await - .is_ok()); - expected_accumulated_value += value; - } - let rav_request_result = manager.create_rav_request(0, None).await; - assert!(rav_request_result.is_ok()); - - let rav_request = rav_request_result.unwrap(); - // all receipts passing - assert_eq!( - rav_request.valid_receipts.len(), - stored_signed_receipts.len() - ); - // no receipts failing - assert_eq!(rav_request.invalid_receipts.len(), 0); - // accumulated value is correct - assert_eq!( - rav_request.expected_rav.valueAggregate, - expected_accumulated_value - ); - // no previous rav - assert!(rav_request.previous_rav.is_none()); + assert!(manager + .verify_and_store_rav( + rav_request.expected_rav, + signed_rav, + |address: Address| async move { Ok(keys.1 == address) } + ) + .await + .is_ok()); - let signed_rav = - EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) - .unwrap(); + stored_signed_receipts.clear(); + for query_id in 10..20 { + let value = 20u128; + let signed_receipt = EIP712SignedMessage::new( + &domain_separator, + Receipt::new(allocation_ids[0], value).unwrap(), + &keys.0, + ) + .unwrap(); + stored_signed_receipts.push(signed_receipt.clone()); + query_appraisals.write().await.insert(query_id, value); assert!(manager - .verify_and_store_rav( - rav_request.expected_rav, - signed_rav, - |address: Address| async move { Ok(keys.1 == address) } - ) + .verify_and_store_receipt(signed_receipt, query_id, initial_checks) .await .is_ok()); - - stored_signed_receipts.clear(); - for query_id in 10..20 { - let value = 20u128; - let signed_receipt = EIP712SignedMessage::new( - &domain_separator, - Receipt::new(allocation_ids[0], value).unwrap(), - &keys.0, - ) + expected_accumulated_value += value; + } + let rav_request_result = manager.create_rav_request(0, None).await; + assert!(rav_request_result.is_ok()); + + let rav_request = rav_request_result.unwrap(); + // all receipts passing + assert_eq!( + rav_request.valid_receipts.len(), + stored_signed_receipts.len() + ); + // no receipts failing + assert_eq!(rav_request.invalid_receipts.len(), 0); + // accumulated value is correct + assert_eq!( + rav_request.expected_rav.valueAggregate, + expected_accumulated_value + ); + // Verify there is a previous rav + assert!(rav_request.previous_rav.is_some()); + + let signed_rav = + EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) .unwrap(); - stored_signed_receipts.push(signed_receipt.clone()); - query_appraisal_storage - .write() - .await - .insert(query_id, value); - assert!(manager - .verify_and_store_receipt(signed_receipt, query_id, initial_checks.as_slice()) - .await - .is_ok()); - expected_accumulated_value += value; - } - let rav_request_result = manager.create_rav_request(0, None).await; - assert!(rav_request_result.is_ok()); - - let rav_request = rav_request_result.unwrap(); - // all receipts passing - assert_eq!( - rav_request.valid_receipts.len(), - stored_signed_receipts.len() - ); - // no receipts failing - assert_eq!(rav_request.invalid_receipts.len(), 0); - // accumulated value is correct - assert_eq!( - rav_request.expected_rav.valueAggregate, - expected_accumulated_value - ); - // Verify there is a previous rav - assert!(rav_request.previous_rav.is_some()); + assert!(manager + .verify_and_store_rav( + rav_request.expected_rav, + signed_rav, + |address: Address| async move { Ok(keys.1 == address) } + ) + .await + .is_ok()); +} - let signed_rav = - EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &keys.0) - .unwrap(); +#[rstest] +#[tokio::test] +async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_timestamps( + keys: (LocalWallet, Address), + allocation_ids: Vec
, + domain_separator: Eip712Domain, + #[values(0..0, 0..2, 0..5)] range: Range, + #[values(true, false)] remove_old_receipts: bool, + executor_mock: ExecutorFixture, +) { + let ExecutorFixture { + executor, + checks, + query_appraisals, + escrow_storage, + .. + } = executor_mock; + let initial_checks = &checks[range]; + // give receipt 5 second variance for min start time + let starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; + + let manager = Manager::new( + domain_separator.clone(), + executor, + checks.clone(), + starting_min_timestamp, + ); + + escrow_storage.write().await.insert(keys.1, 999999); + + let mut stored_signed_receipts = Vec::new(); + let mut expected_accumulated_value = 0; + for query_id in 0..10 { + let value = 20u128; + let mut receipt = Receipt::new(allocation_ids[0], value).unwrap(); + receipt.timestamp_ns = starting_min_timestamp + query_id + 1; + let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &keys.0).unwrap(); + stored_signed_receipts.push(signed_receipt.clone()); + query_appraisals.write().await.insert(query_id, value); assert!(manager - .verify_and_store_rav( - rav_request.expected_rav, - signed_rav, - |address: Address| async move { Ok(keys.1 == address) } - ) + .verify_and_store_receipt(signed_receipt, query_id, initial_checks) .await .is_ok()); + expected_accumulated_value += value; } - #[rstest] - #[tokio::test] - async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_timestamps( - executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), - keys: (LocalWallet, Address), - allocation_ids: Vec
, - domain_separator: Eip712Domain, - #[values(/*get_full_list_of_checks(), vec![ReceiptCheck::CheckSignature], */Vec::::new())] - initial_checks: Vec, - #[values(true, false)] remove_old_receipts: bool, - full_list_of_checks: Vec, - ) { - 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 manager = Manager::new( - domain_separator.clone(), - executor, - full_list_of_checks, - starting_min_timestamp, - ); - - escrow_storage.write().await.insert(keys.1, 999999); - - let mut stored_signed_receipts = Vec::new(); - let mut expected_accumulated_value = 0; - for query_id in 0..10 { - let value = 20u128; - let mut receipt = Receipt::new(allocation_ids[0], value).unwrap(); - receipt.timestamp_ns = starting_min_timestamp + query_id + 1; - let signed_receipt = - EIP712SignedMessage::new(&domain_separator, receipt, &keys.0).unwrap(); - stored_signed_receipts.push(signed_receipt.clone()); - query_appraisal_storage - .write() - .await - .insert(query_id, value); - assert!(manager - .verify_and_store_receipt(signed_receipt, query_id, initial_checks.as_slice()) - .await - .is_ok()); - expected_accumulated_value += value; - } - - // Remove old receipts if requested - // This shouldn't do anything since there has been no rav created yet - if remove_old_receipts { - manager.remove_obsolete_receipts().await.unwrap(); - } - - let rav_request_1_result = manager.create_rav_request(0, None).await; - assert!(rav_request_1_result.is_ok()); - - let rav_request_1 = rav_request_1_result.unwrap(); - // all receipts passing - assert_eq!( - rav_request_1.valid_receipts.len(), - stored_signed_receipts.len() - ); - // no receipts failing - assert_eq!(rav_request_1.invalid_receipts.len(), 0); - // accumulated value is correct - assert_eq!( - rav_request_1.expected_rav.valueAggregate, - expected_accumulated_value - ); - // no previous rav - assert!(rav_request_1.previous_rav.is_none()); + // Remove old receipts if requested + // This shouldn't do anything since there has been no rav created yet + if remove_old_receipts { + manager.remove_obsolete_receipts().await.unwrap(); + } - let signed_rav_1 = EIP712SignedMessage::new( - &domain_separator, - rav_request_1.expected_rav.clone(), - &keys.0, + let rav_request_1_result = manager.create_rav_request(0, None).await; + assert!(rav_request_1_result.is_ok()); + + let rav_request_1 = rav_request_1_result.unwrap(); + // all receipts passing + assert_eq!( + rav_request_1.valid_receipts.len(), + stored_signed_receipts.len() + ); + // no receipts failing + assert_eq!(rav_request_1.invalid_receipts.len(), 0); + // accumulated value is correct + assert_eq!( + rav_request_1.expected_rav.valueAggregate, + expected_accumulated_value + ); + // no previous rav + assert!(rav_request_1.previous_rav.is_none()); + + let signed_rav_1 = EIP712SignedMessage::new( + &domain_separator, + rav_request_1.expected_rav.clone(), + &keys.0, + ) + .unwrap(); + assert!(manager + .verify_and_store_rav( + rav_request_1.expected_rav, + signed_rav_1, + |address: Address| async move { Ok(keys.1 == address) } ) - .unwrap(); + .await + .is_ok()); + + stored_signed_receipts.clear(); + for query_id in 10..20 { + let value = 20u128; + let mut receipt = Receipt::new(allocation_ids[0], value).unwrap(); + receipt.timestamp_ns = starting_min_timestamp + query_id + 1; + let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &keys.0).unwrap(); + stored_signed_receipts.push(signed_receipt.clone()); + query_appraisals.write().await.insert(query_id, value); assert!(manager - .verify_and_store_rav( - rav_request_1.expected_rav, - signed_rav_1, - |address: Address| async move { Ok(keys.1 == address) } - ) + .verify_and_store_receipt(signed_receipt, query_id, initial_checks) .await .is_ok()); + expected_accumulated_value += value; + } - stored_signed_receipts.clear(); - for query_id in 10..20 { - let value = 20u128; - let mut receipt = Receipt::new(allocation_ids[0], value).unwrap(); - receipt.timestamp_ns = starting_min_timestamp + query_id + 1; - let signed_receipt = - EIP712SignedMessage::new(&domain_separator, receipt, &keys.0).unwrap(); - stored_signed_receipts.push(signed_receipt.clone()); - query_appraisal_storage - .write() - .await - .insert(query_id, value); - assert!(manager - .verify_and_store_receipt(signed_receipt, query_id, initial_checks.as_slice()) - .await - .is_ok()); - expected_accumulated_value += value; - } - - // Remove old receipts if requested - if remove_old_receipts { - manager.remove_obsolete_receipts().await.unwrap(); - // We expect to have 10 receipts left in receipt storage - assert_eq!( - manager - .executor - .retrieve_receipts_in_timestamp_range(.., None) - .await - .unwrap() - .len(), - 10 - ); - } - - let rav_request_2_result = manager.create_rav_request(0, None).await; - assert!(rav_request_2_result.is_ok()); - - let rav_request_2 = rav_request_2_result.unwrap(); - // all receipts passing - assert_eq!( - rav_request_2.valid_receipts.len(), - stored_signed_receipts.len() - ); - // no receipts failing - assert_eq!(rav_request_2.invalid_receipts.len(), 0); - // accumulated value is correct + // Remove old receipts if requested + if remove_old_receipts { + manager.remove_obsolete_receipts().await.unwrap(); + // We expect to have 10 receipts left in receipt storage assert_eq!( - rav_request_2.expected_rav.valueAggregate, - expected_accumulated_value + manager + .executor + .retrieve_receipts_in_timestamp_range(.., None) + .await + .unwrap() + .len(), + 10 ); - // Verify there is a previous rav - assert!(rav_request_2.previous_rav.is_some()); + } - let signed_rav_2 = EIP712SignedMessage::new( - &domain_separator, - rav_request_2.expected_rav.clone(), - &keys.0, + let rav_request_2_result = manager.create_rav_request(0, None).await; + assert!(rav_request_2_result.is_ok()); + + let rav_request_2 = rav_request_2_result.unwrap(); + // all receipts passing + assert_eq!( + rav_request_2.valid_receipts.len(), + stored_signed_receipts.len() + ); + // no receipts failing + assert_eq!(rav_request_2.invalid_receipts.len(), 0); + // accumulated value is correct + assert_eq!( + rav_request_2.expected_rav.valueAggregate, + expected_accumulated_value + ); + // Verify there is a previous rav + assert!(rav_request_2.previous_rav.is_some()); + + let signed_rav_2 = EIP712SignedMessage::new( + &domain_separator, + rav_request_2.expected_rav.clone(), + &keys.0, + ) + .unwrap(); + assert!(manager + .verify_and_store_rav( + rav_request_2.expected_rav, + signed_rav_2, + |address: Address| async move { Ok(keys.1 == address) } ) - .unwrap(); - assert!(manager - .verify_and_store_rav( - rav_request_2.expected_rav, - signed_rav_2, - |address: Address| async move { Ok(keys.1 == address) } - ) - .await - .is_ok()); - } + .await + .is_ok()); } 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 857e0843..5e4f3118 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 @@ -10,7 +10,9 @@ use rstest::*; use tokio::sync::RwLock; use crate::{ - adapters::executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, + adapters::executor_mock::{ + EscrowStorage, ExecutorMock, QueryAppraisals, RAVStorage, ReceiptStorage, + }, checks::{tests::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, get_current_timestamp_u64_ns, tap_eip712_domain, @@ -79,22 +81,23 @@ fn sender_ids() -> Vec
{ } #[fixture] -fn receipt_storage() -> Arc>> { +fn receipt_storage() -> ReceiptStorage { Arc::new(RwLock::new(HashMap::new())) } #[fixture] -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()); +fn query_appraisal_storage() -> QueryAppraisals { + Arc::new(RwLock::new(HashMap::new())) +} - let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); +#[fixture] +fn rav_storage() -> RAVStorage { + Arc::new(RwLock::new(None)) +} - let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new())); - (executor, sender_escrow_storage, query_appraisal_storage) +#[fixture] +fn escrow_storage() -> EscrowStorage { + Arc::new(RwLock::new(HashMap::new())) } #[fixture] @@ -102,22 +105,39 @@ fn domain_separator() -> Eip712Domain { tap_eip712_domain(1, Address::from([0x11u8; 20])) } +struct ExecutorFixture { + executor: ExecutorMock, + escrow_storage: EscrowStorage, + query_appraisals: QueryAppraisals, + checks: Vec, +} + #[fixture] -fn checks( +fn executor_mock( domain_separator: Eip712Domain, - executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), - receipt_storage: Arc>>, allocation_ids: Vec
, sender_ids: Vec
, -) -> Vec { - let (_, _, query_appraisal_storage) = executor_mock; - get_full_list_of_checks( + receipt_storage: ReceiptStorage, + query_appraisal_storage: QueryAppraisals, + rav_storage: RAVStorage, + escrow_storage: EscrowStorage, +) -> ExecutorFixture { + let executor = ExecutorMock::new(rav_storage, receipt_storage.clone(), escrow_storage.clone()); + + let checks = get_full_list_of_checks( domain_separator, sender_ids.iter().cloned().collect(), Arc::new(RwLock::new(allocation_ids.iter().cloned().collect())), receipt_storage, - query_appraisal_storage, - ) + query_appraisal_storage.clone(), + ); + + ExecutorFixture { + executor, + escrow_storage, + query_appraisals: query_appraisal_storage, + checks, + } } #[rstest] @@ -126,8 +146,10 @@ async fn initialization_valid_receipt( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, - checks: Vec, + executor_mock: ExecutorFixture, ) { + let ExecutorFixture { checks, .. } = executor_mock; + let signed_receipt = EIP712SignedMessage::new( &domain_separator, Receipt::new(allocation_ids[0], 10).unwrap(), @@ -153,13 +175,18 @@ async fn partial_then_full_check_valid_receipt( keys: (LocalWallet, Address), domain_separator: Eip712Domain, allocation_ids: Vec
, - executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), - checks: Vec, + executor_mock: ExecutorFixture, ) { - let (executor, escrow_storage, query_appraisal_storage) = executor_mock; + let ExecutorFixture { + checks, + executor, + escrow_storage, + query_appraisals, + .. + } = 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 starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; + let receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); let query_value = 20u128; let signed_receipt = EIP712SignedMessage::new( @@ -179,13 +206,10 @@ async fn partial_then_full_check_valid_receipt( .await .insert(keys.1, query_value + 500); // appraise query - query_appraisal_storage - .write() - .await - .insert(query_id, query_value); + query_appraisals.write().await.insert(query_id, query_value); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); - let _receipt_id = 0u64; + let receipt_id = 0u64; let mut received_receipt = match received_receipt { ReceivedReceipt::Checking(checking) => checking, @@ -212,12 +236,17 @@ async fn partial_then_finalize_valid_receipt( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, - executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), - checks: Vec, + executor_mock: ExecutorFixture, ) { - let (executor, escrow_storage, query_appraisal_storage) = executor_mock; + let ExecutorFixture { + checks, + executor, + escrow_storage, + query_appraisals, + .. + } = executor_mock; // give receipt 5 second variance for min start time - let _starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; + let starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; let receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); let query_value = 20u128; @@ -238,13 +267,10 @@ async fn partial_then_finalize_valid_receipt( .await .insert(keys.1, query_value + 500); // appraise query - query_appraisal_storage - .write() - .await - .insert(query_id, query_value); + query_appraisals.write().await.insert(query_id, query_value); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); - let _receipt_id = 0u64; + let receipt_id = 0u64; let mut received_receipt = match received_receipt { ReceivedReceipt::Checking(checking) => checking, @@ -260,7 +286,6 @@ async fn partial_then_finalize_valid_receipt( assert!(received_receipt.check_is_complete(arbitrary_check_to_perform)); let awaiting_escrow_receipt = received_receipt.finalize_receipt_checks().await; - println!("{:?}", awaiting_escrow_receipt); assert!(awaiting_escrow_receipt.is_ok()); let awaiting_escrow_receipt = awaiting_escrow_receipt.unwrap(); @@ -276,13 +301,18 @@ async fn standard_lifetime_valid_receipt( keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, - executor_mock: (ExecutorMock, EscrowStorage, QueryAppraisals), - checks: Vec, + executor_mock: ExecutorFixture, ) { - let (executor, escrow_storage, query_appraisal_storage) = executor_mock; + let ExecutorFixture { + checks, + executor, + escrow_storage, + query_appraisals, + .. + } = 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 starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; + let receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); let query_value = 20u128; let signed_receipt = EIP712SignedMessage::new( @@ -302,13 +332,10 @@ async fn standard_lifetime_valid_receipt( .await .insert(keys.1, query_value + 500); // appraise query - query_appraisal_storage - .write() - .await - .insert(query_id, query_value); + query_appraisals.write().await.insert(query_id, query_value); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); - let _receipt_id = 0u64; + let receipt_id = 0u64; let received_receipt = match received_receipt { ReceivedReceipt::Checking(checking) => checking, From 2eca6a1049724d91bdf2daeae20d2b4b0f4ad5a7 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Tue, 5 Mar 2024 13:04:47 -0300 Subject: [PATCH 25/32] test: fix unused variables Signed-off-by: Gustavo Inacio --- .../received_receipt_unit_test.rs | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) 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 5e4f3118..91614cf9 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 @@ -15,7 +15,7 @@ use crate::{ }, checks::{tests::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, - get_current_timestamp_u64_ns, tap_eip712_domain, + tap_eip712_domain, tap_receipt::{Receipt, ReceiptAuditor, ReceiptCheckResults, ReceivedReceipt}, }; @@ -179,14 +179,10 @@ async fn partial_then_full_check_valid_receipt( ) { let ExecutorFixture { checks, - executor, escrow_storage, query_appraisals, .. } = 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 query_value = 20u128; let signed_receipt = EIP712SignedMessage::new( @@ -209,7 +205,6 @@ async fn partial_then_full_check_valid_receipt( query_appraisals.write().await.insert(query_id, query_value); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); - let receipt_id = 0u64; let mut received_receipt = match received_receipt { ReceivedReceipt::Checking(checking) => checking, @@ -246,7 +241,6 @@ async fn partial_then_finalize_valid_receipt( .. } = 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 query_value = 20u128; @@ -270,7 +264,6 @@ async fn partial_then_finalize_valid_receipt( query_appraisals.write().await.insert(query_id, query_value); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); - let receipt_id = 0u64; let mut received_receipt = match received_receipt { ReceivedReceipt::Checking(checking) => checking, @@ -311,7 +304,6 @@ async fn standard_lifetime_valid_receipt( .. } = 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 query_value = 20u128; @@ -335,12 +327,18 @@ async fn standard_lifetime_valid_receipt( query_appraisals.write().await.insert(query_id, query_value); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); - let receipt_id = 0u64; let received_receipt = match received_receipt { ReceivedReceipt::Checking(checking) => checking, _ => panic!("ReceivedReceipt should be in Checking state"), }; - assert!(received_receipt.finalize_receipt_checks().await.is_ok()); + let awaiting_escrow_receipt = received_receipt.finalize_receipt_checks().await; + assert!(awaiting_escrow_receipt.is_ok()); + + let awaiting_escrow_receipt = awaiting_escrow_receipt.unwrap(); + let receipt = awaiting_escrow_receipt + .check_and_reserve_escrow(&receipt_auditor) + .await; + assert!(receipt.is_ok()); } From f763410c678e23fefd1e0c2bc1dc77e8d135dbd9 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Tue, 5 Mar 2024 13:23:55 -0300 Subject: [PATCH 26/32] test: convert checks to mock Signed-off-by: Gustavo Inacio --- .../test/receipt_checks_adapter_test.rs | 2 +- .../test/receipt_storage_adapter_test.rs | 2 +- tap_core/src/checks/mod.rs | 5 +- tap_core/src/tap_manager/test/manager_test.rs | 12 +- .../received_receipt_unit_test.rs | 2 +- tap_integration_tests/tests/showcase.rs | 129 +++++++++++------- 6 files changed, 88 insertions(+), 64 deletions(-) diff --git a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs index 9bc205ea..ff74eb01 100644 --- a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs @@ -17,7 +17,7 @@ mod receipt_checks_adapter_unit_test { use tokio::sync::RwLock; use crate::{ - checks::{tests::get_full_list_of_checks, ReceiptCheck}, + checks::{mock::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, tap_receipt::{Receipt, ReceivedReceipt}, diff --git a/tap_core/src/adapters/test/receipt_storage_adapter_test.rs b/tap_core/src/adapters/test/receipt_storage_adapter_test.rs index 563bbeef..453f5f45 100644 --- a/tap_core/src/adapters/test/receipt_storage_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_storage_adapter_test.rs @@ -18,7 +18,7 @@ mod receipt_storage_adapter_unit_test { use crate::{ adapters::{executor_mock::ExecutorMock, receipt_storage_adapter::ReceiptStore}, - checks::{tests::get_full_list_of_checks, ReceiptCheck}, + checks::{mock::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, tap_receipt::{Receipt, ReceivedReceipt}, diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs index 9dd62864..aaf89ed0 100644 --- a/tap_core/src/checks/mod.rs +++ b/tap_core/src/checks/mod.rs @@ -91,8 +91,8 @@ impl Check for TimestampCheck { } } -#[cfg(test)] -pub mod tests { +#[cfg(feature = "mock")] +pub mod mock { use super::*; use crate::tap_receipt::ReceivedReceipt; @@ -118,7 +118,6 @@ pub mod tests { domain_separator, valid_signers, }), - Arc::new(TimestampCheck::new(0)), ] } diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index b957193b..93c03d67 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -14,12 +14,14 @@ use crate::{ executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals, RAVStorage, ReceiptStorage}, receipt_storage_adapter::ReceiptRead, }, - checks::{tests::get_full_list_of_checks, ReceiptCheck}, + checks::{mock::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, get_current_timestamp_u64_ns, tap_eip712_domain, tap_receipt::Receipt, }; +const LENGTH_OF_CHECKS: usize = 4; + #[fixture] fn keys() -> (LocalWallet, Address) { let wallet: LocalWallet = MnemonicBuilder::::default() @@ -114,7 +116,7 @@ fn executor_mock( } #[rstest] -#[case::full_checks(0..5)] +#[case::full_checks(0..LENGTH_OF_CHECKS)] #[case::partial_checks(0..2)] #[case::no_checks(0..0)] #[tokio::test] @@ -160,7 +162,7 @@ async fn manager_verify_and_store_varying_initial_checks( } #[rstest] -#[case::full_checks(0..5)] +#[case::full_checks(0..LENGTH_OF_CHECKS)] #[case::partial_checks(0..2)] #[case::no_checks(0..0)] #[tokio::test] @@ -233,7 +235,7 @@ async fn manager_create_rav_request_all_valid_receipts( } #[rstest] -#[case::full_checks(0..5)] +#[case::full_checks(0..LENGTH_OF_CHECKS)] #[case::partial_checks(0..2)] #[case::no_checks(0..0)] #[tokio::test] @@ -368,7 +370,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim keys: (LocalWallet, Address), allocation_ids: Vec
, domain_separator: Eip712Domain, - #[values(0..0, 0..2, 0..5)] range: Range, + #[values(0..0, 0..2, 0..LENGTH_OF_CHECKS)] range: Range, #[values(true, false)] remove_old_receipts: bool, executor_mock: ExecutorFixture, ) { 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 91614cf9..eb8c9fee 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 @@ -13,7 +13,7 @@ use crate::{ adapters::executor_mock::{ EscrowStorage, ExecutorMock, QueryAppraisals, RAVStorage, ReceiptStorage, }, - checks::{tests::get_full_list_of_checks, ReceiptCheck}, + checks::{mock::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, tap_receipt::{Receipt, ReceiptAuditor, ReceiptCheckResults, ReceivedReceipt}, diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index a53ab054..b2391b7a 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -25,12 +25,14 @@ use tokio::sync::RwLock; use tap_aggregator::{jsonrpsee_helpers, server as agg_server}; use tap_core::{ - adapters::executor_mock::ExecutorMock, - checks::ReceiptCheck, + adapters::executor_mock::{ + EscrowStorage, ExecutorMock, QueryAppraisals, RAVStorage, ReceiptStorage, + }, + checks::{mock::get_full_list_of_checks, ReceiptCheck}, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, tap_manager::SignedRAV, - tap_receipt::{Receipt, ReceivedReceipt}, + tap_receipt::Receipt, }; use crate::indexer_mock; @@ -118,6 +120,16 @@ fn allocation_ids() -> Vec
{ ] } +#[fixture] +fn sender_ids() -> Vec
{ + vec![ + Address::from_str("0xfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb").unwrap(), + Address::from_str("0xfafafafafafafafafafafafafafafafafafafafa").unwrap(), + Address::from_str("0xadadadadadadadadadadadadadadadadadadadad").unwrap(), + keys_sender().1, + ] +} + // Domain separator is used to sign receipts/RAVs according to EIP-712 #[fixture] fn domain_separator() -> Eip712Domain { @@ -144,52 +156,60 @@ fn available_escrow(query_price: Vec, num_batches: u64) -> u128 { } #[fixture] -fn executor(receipt_storage: Arc>>) -> ExecutorMock { - let rav_storage = Arc::new(RwLock::new(None)); +fn receipt_storage() -> ReceiptStorage { + Arc::new(RwLock::new(HashMap::new())) +} - let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new())); +#[fixture] +fn query_appraisals() -> QueryAppraisals { + Arc::new(RwLock::new(HashMap::new())) +} - ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage) +#[fixture] +fn rav_storage() -> RAVStorage { + Arc::new(RwLock::new(None)) } #[fixture] -fn receipt_storage() -> Arc>> { +fn escrow_storage() -> EscrowStorage { Arc::new(RwLock::new(HashMap::new())) } -// These are the checks that the Indexer will perform when requesting a RAV. -// Testing with all checks enabled. -#[fixture] -fn required_checks() -> Vec { - vec![ - // ReceiptCheck::CheckAllocationId, - // ReceiptCheck::CheckSignature, - // ReceiptCheck::CheckTimestamp, - // ReceiptCheck::CheckUnique, - // ReceiptCheck::CheckValue, - ] +struct ExecutorFixture { + executor: ExecutorMock, + checks: Vec, } -// These are the checks that the Indexer will perform for each received receipt, i.e. before requesting a RAV. -// Testing with all checks enabled. #[fixture] -fn initial_checks() -> Vec { - vec![ - // ReceiptCheck::CheckAllocationId, - // ReceiptCheck::CheckSignature, - // ReceiptCheck::CheckTimestamp, - // ReceiptCheck::CheckUnique, - // ReceiptCheck::CheckValue, - ] +fn executor( + domain_separator: Eip712Domain, + allocation_ids: Vec
, + sender_ids: Vec
, + receipt_storage: ReceiptStorage, + query_appraisals: QueryAppraisals, + rav_storage: RAVStorage, + escrow_storage: EscrowStorage, +) -> ExecutorFixture { + let executor = ExecutorMock::new(rav_storage, receipt_storage.clone(), escrow_storage.clone()); + + let checks = get_full_list_of_checks( + domain_separator, + sender_ids.iter().cloned().collect(), + Arc::new(RwLock::new(allocation_ids.iter().cloned().collect())), + receipt_storage, + query_appraisals, + ); + + ExecutorFixture { executor, checks } } #[fixture] -fn indexer_1_adapters(executor: ExecutorMock) -> ExecutorMock { +fn indexer_1_adapters(executor: ExecutorFixture) -> ExecutorFixture { executor } #[fixture] -fn indexer_2_adapters(executor: ExecutorMock) -> ExecutorMock { +fn indexer_2_adapters(executor: ExecutorFixture) -> ExecutorFixture { executor } @@ -342,10 +362,8 @@ async fn single_indexer_test_server( http_request_size_limit: u32, http_response_size_limit: u32, http_max_concurrent_connections: u32, - indexer_1_adapters: ExecutorMock, + indexer_1_adapters: ExecutorFixture, available_escrow: u128, - initial_checks: Vec, - required_checks: Vec, receipt_threshold_1: u64, ) -> Result<(ServerHandle, SocketAddr, ServerHandle, SocketAddr)> { let sender_id = keys_sender.1; @@ -357,14 +375,14 @@ async fn single_indexer_test_server( http_max_concurrent_connections, ) .await?; - let executor = indexer_1_adapters; + let ExecutorFixture { executor, checks } = indexer_1_adapters; let (indexer_handle, indexer_addr) = start_indexer_server( domain_separator.clone(), executor, sender_id, available_escrow, - initial_checks, - required_checks, + checks.clone(), + checks, receipt_threshold_1, sender_aggregator_addr, ) @@ -384,11 +402,9 @@ async fn two_indexers_test_servers( http_request_size_limit: u32, http_response_size_limit: u32, http_max_concurrent_connections: u32, - indexer_1_adapters: ExecutorMock, - indexer_2_adapters: ExecutorMock, + indexer_1_adapters: ExecutorFixture, + indexer_2_adapters: ExecutorFixture, available_escrow: u128, - initial_checks: Vec, - required_checks: Vec, receipt_threshold_1: u64, ) -> Result<( ServerHandle, @@ -407,16 +423,23 @@ async fn two_indexers_test_servers( http_max_concurrent_connections, ) .await?; - let executor_1 = indexer_1_adapters; - let executor_2 = indexer_2_adapters; + let ExecutorFixture { + executor: executor_1, + checks: checks_1, + } = indexer_1_adapters; + + let ExecutorFixture { + executor: executor_2, + checks: checks_2, + } = indexer_2_adapters; let (indexer_handle, indexer_addr) = start_indexer_server( domain_separator.clone(), executor_1, sender_id, available_escrow, - initial_checks.clone(), - required_checks.clone(), + checks_1.clone(), + checks_1, receipt_threshold_1, sender_aggregator_addr, ) @@ -427,8 +450,8 @@ async fn two_indexers_test_servers( executor_2, sender_id, available_escrow, - initial_checks, - required_checks, + checks_2.clone(), + checks_2, receipt_threshold_1, sender_aggregator_addr, ) @@ -451,10 +474,8 @@ async fn single_indexer_wrong_sender_test_server( http_request_size_limit: u32, http_response_size_limit: u32, http_max_concurrent_connections: u32, - indexer_1_adapters: ExecutorMock, + indexer_1_adapters: ExecutorFixture, available_escrow: u128, - initial_checks: Vec, - required_checks: Vec, receipt_threshold_1: u64, ) -> Result<(ServerHandle, SocketAddr, ServerHandle, SocketAddr)> { let sender_id = wrong_keys_sender.1; @@ -466,15 +487,17 @@ async fn single_indexer_wrong_sender_test_server( http_max_concurrent_connections, ) .await?; - let executor = indexer_1_adapters; + let ExecutorFixture { + executor, checks, .. + } = indexer_1_adapters; let (indexer_handle, indexer_addr) = start_indexer_server( domain_separator.clone(), executor, sender_id, available_escrow, - initial_checks, - required_checks, + checks.clone(), + checks, receipt_threshold_1, sender_aggregator_addr, ) From 9523309bf7739688bd2686492e4c905c132bd987 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Tue, 5 Mar 2024 13:51:41 -0300 Subject: [PATCH 27/32] test: fix integration tests Signed-off-by: Gustavo Inacio --- tap_core/src/tap_manager/manager.rs | 2 +- .../tests/indexer_mock/mod.rs | 22 +++----------- tap_integration_tests/tests/showcase.rs | 29 ++++++++++++------- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/tap_core/src/tap_manager/manager.rs b/tap_core/src/tap_manager/manager.rs index e9994532..2af6e8dd 100644 --- a/tap_core/src/tap_manager/manager.rs +++ b/tap_core/src/tap_manager/manager.rs @@ -166,7 +166,7 @@ where mut reserved_receipts, } = received_receipts.into(); - for received_receipt in checking_receipts { + for received_receipt in checking_receipts.into_iter() { let ReceiptWithId { receipt, receipt_id: _, diff --git a/tap_integration_tests/tests/indexer_mock/mod.rs b/tap_integration_tests/tests/indexer_mock/mod.rs index b7cc34f0..9b2d97d6 100644 --- a/tap_integration_tests/tests/indexer_mock/mod.rs +++ b/tap_integration_tests/tests/indexer_mock/mod.rs @@ -1,11 +1,8 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use std::{ - sync::{ - atomic::{AtomicU64, Ordering}, - Arc, - }, - time::{SystemTime, UNIX_EPOCH}, +use std::sync::{ + atomic::{AtomicU64, Ordering}, + Arc, }; use alloy_primitives::Address; @@ -28,7 +25,6 @@ use tap_core::{ }, checks::ReceiptCheck, tap_manager::{Manager, SignedRAV, SignedReceipt}, - Error as TapCoreError, }; /// Rpc trait represents a JSON-RPC server that has a single async method `request`. /// This method is designed to handle incoming JSON-RPC requests. @@ -81,7 +77,7 @@ where domain_separator, executor, required_checks, - get_current_timestamp_u64_ns()?, + 0, )), initial_checks, receipt_count: Arc::new(AtomicU64::new(0)), @@ -239,16 +235,6 @@ where Ok(()) } -// get_current_timestamp_u64_ns function returns current system time since UNIX_EPOCH as a 64-bit unsigned integer. -fn get_current_timestamp_u64_ns() -> Result { - Ok(SystemTime::now() - .duration_since(UNIX_EPOCH) - .map_err(|err| TapCoreError::InvalidSystemTime { - source_error_message: err.to_string(), - })? - .as_nanos() as u64) -} - fn to_rpc_error(e: Box, msg: &str) -> jsonrpsee::types::ErrorObjectOwned { jsonrpsee::types::ErrorObject::owned(-32000, format!("{} - {}", e, msg), None::<()>) } diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index b2391b7a..88fc28cc 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -138,6 +138,7 @@ fn domain_separator() -> Eip712Domain { // Query price will typically be set by the Indexer. It's assumed to be part of the Indexer service. #[fixture] +#[once] fn query_price() -> Vec { let seed: Vec = (0..32u8).collect(); // A seed of your choice let mut rng: StdRng = SeedableRng::from_seed(seed.try_into().unwrap()); @@ -151,8 +152,8 @@ fn query_price() -> Vec { // Available escrow is set by a Sender. It's assumed the Indexer has way of knowing this value. #[fixture] -fn available_escrow(query_price: Vec, num_batches: u64) -> u128 { - (num_batches as u128) * query_price.into_iter().sum::() +fn available_escrow(query_price: &Vec, num_batches: u64) -> u128 { + (num_batches as u128) * query_price.iter().sum::() } #[fixture] @@ -161,8 +162,14 @@ fn receipt_storage() -> ReceiptStorage { } #[fixture] -fn query_appraisals() -> QueryAppraisals { - Arc::new(RwLock::new(HashMap::new())) +fn query_appraisals(query_price: &Vec) -> QueryAppraisals { + Arc::new(RwLock::new( + query_price + .iter() + .enumerate() + .map(|(i, p)| (i as u64, *p)) + .collect(), + )) } #[fixture] @@ -218,7 +225,7 @@ fn indexer_2_adapters(executor: ExecutorFixture) -> ExecutorFixture { #[fixture] fn requests_1( keys_sender: (LocalWallet, Address), - query_price: Vec, + query_price: &Vec, num_batches: u64, allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -238,7 +245,7 @@ fn requests_1( #[fixture] fn requests_2( keys_sender: (LocalWallet, Address), - query_price: Vec, + query_price: &Vec, num_batches: u64, allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -258,7 +265,7 @@ fn requests_2( #[fixture] fn repeated_timestamp_request( keys_sender: (LocalWallet, Address), - query_price: Vec, + query_price: &Vec, allocation_ids: Vec
, domain_separator: Eip712Domain, num_batches: u64, @@ -297,7 +304,7 @@ fn repeated_timestamp_request( #[fixture] fn repeated_timestamp_incremented_by_one_request( keys_sender: (LocalWallet, Address), - query_price: Vec, + query_price: &Vec, allocation_ids: Vec
, domain_separator: Eip712Domain, num_batches: u64, @@ -336,7 +343,7 @@ fn repeated_timestamp_incremented_by_one_request( #[fixture] fn wrong_requests( wrong_keys_sender: (LocalWallet, Address), - query_price: Vec, + query_price: &Vec, num_batches: u64, allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -830,7 +837,7 @@ async fn test_tap_aggregator_rav_timestamp_cuttoff( } fn generate_requests( - query_price: Vec, + query_price: &Vec, num_batches: u64, sender_key: &LocalWallet, allocation_id: Address, @@ -840,7 +847,7 @@ fn generate_requests( let mut counter = 0; for _ in 0..num_batches { - for value in &query_price { + for value in query_price { requests.push(( EIP712SignedMessage::new( domain_separator, From ace9c00783d2c514c027d27c1ffd34aceb71aa5a Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Tue, 5 Mar 2024 13:55:54 -0300 Subject: [PATCH 28/32] test: use static slice for prices Signed-off-by: Gustavo Inacio --- tap_integration_tests/tests/showcase.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index 88fc28cc..c7bc9409 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -139,7 +139,7 @@ fn domain_separator() -> Eip712Domain { // Query price will typically be set by the Indexer. It's assumed to be part of the Indexer service. #[fixture] #[once] -fn query_price() -> Vec { +fn query_price() -> &'static [u128] { let seed: Vec = (0..32u8).collect(); // A seed of your choice let mut rng: StdRng = SeedableRng::from_seed(seed.try_into().unwrap()); let mut v = Vec::new(); @@ -147,12 +147,12 @@ fn query_price() -> Vec { for _ in 0..num_queries() { v.push(rng.gen::() % 100); } - v + Box::leak(v.into_boxed_slice()) } // Available escrow is set by a Sender. It's assumed the Indexer has way of knowing this value. #[fixture] -fn available_escrow(query_price: &Vec, num_batches: u64) -> u128 { +fn available_escrow(query_price: &[u128], num_batches: u64) -> u128 { (num_batches as u128) * query_price.iter().sum::() } @@ -162,7 +162,7 @@ fn receipt_storage() -> ReceiptStorage { } #[fixture] -fn query_appraisals(query_price: &Vec) -> QueryAppraisals { +fn query_appraisals(query_price: &[u128]) -> QueryAppraisals { Arc::new(RwLock::new( query_price .iter() @@ -225,7 +225,7 @@ fn indexer_2_adapters(executor: ExecutorFixture) -> ExecutorFixture { #[fixture] fn requests_1( keys_sender: (LocalWallet, Address), - query_price: &Vec, + query_price: &[u128], num_batches: u64, allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -245,7 +245,7 @@ fn requests_1( #[fixture] fn requests_2( keys_sender: (LocalWallet, Address), - query_price: &Vec, + query_price: &[u128], num_batches: u64, allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -265,7 +265,7 @@ fn requests_2( #[fixture] fn repeated_timestamp_request( keys_sender: (LocalWallet, Address), - query_price: &Vec, + query_price: &[u128], allocation_ids: Vec
, domain_separator: Eip712Domain, num_batches: u64, @@ -304,7 +304,7 @@ fn repeated_timestamp_request( #[fixture] fn repeated_timestamp_incremented_by_one_request( keys_sender: (LocalWallet, Address), - query_price: &Vec, + query_price: &[u128], allocation_ids: Vec
, domain_separator: Eip712Domain, num_batches: u64, @@ -343,7 +343,7 @@ fn repeated_timestamp_incremented_by_one_request( #[fixture] fn wrong_requests( wrong_keys_sender: (LocalWallet, Address), - query_price: &Vec, + query_price: &[u128], num_batches: u64, allocation_ids: Vec
, domain_separator: Eip712Domain, @@ -837,7 +837,7 @@ async fn test_tap_aggregator_rav_timestamp_cuttoff( } fn generate_requests( - query_price: &Vec, + query_price: &[u128], num_batches: u64, sender_key: &LocalWallet, allocation_id: Address, From e6aec70cce374c84b39bab3c2d2f3e27cf4ff263 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Tue, 5 Mar 2024 14:01:55 -0300 Subject: [PATCH 29/32] test: remove unneeded test Signed-off-by: Gustavo Inacio --- tap_core/src/adapters/test/mod.rs | 1 - .../test/receipt_checks_adapter_test.rs | 142 ------------------ 2 files changed, 143 deletions(-) delete mode 100644 tap_core/src/adapters/test/receipt_checks_adapter_test.rs diff --git a/tap_core/src/adapters/test/mod.rs b/tap_core/src/adapters/test/mod.rs index 318c6216..33fb14c0 100644 --- a/tap_core/src/adapters/test/mod.rs +++ b/tap_core/src/adapters/test/mod.rs @@ -3,5 +3,4 @@ pub mod escrow_adapter_test; pub mod rav_storage_adapter_test; -pub mod receipt_checks_adapter_test; pub mod receipt_storage_adapter_test; diff --git a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs b/tap_core/src/adapters/test/receipt_checks_adapter_test.rs deleted file mode 100644 index ff74eb01..00000000 --- a/tap_core/src/adapters/test/receipt_checks_adapter_test.rs +++ /dev/null @@ -1,142 +0,0 @@ -// Copyright 2023-, Semiotic AI, Inc. -// SPDX-License-Identifier: Apache-2.0 - -#[cfg(test)] -mod receipt_checks_adapter_unit_test { - use std::{ - collections::{HashMap, HashSet}, - str::FromStr, - sync::Arc, - }; - - use alloy_primitives::Address; - use alloy_sol_types::Eip712Domain; - use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder}; - use futures::{stream, StreamExt}; - use rstest::*; - use tokio::sync::RwLock; - - use crate::{ - checks::{mock::get_full_list_of_checks, ReceiptCheck}, - eip_712_signed_message::EIP712SignedMessage, - tap_eip712_domain, - tap_receipt::{Receipt, ReceivedReceipt}, - }; - - #[fixture] - fn domain_separator() -> Eip712Domain { - tap_eip712_domain(1, Address::from([0x11u8; 20])) - } - - #[fixture] - fn checks(domain_separator: Eip712Domain) -> Vec { - get_full_list_of_checks( - domain_separator, - HashSet::new(), - Arc::new(RwLock::new(HashSet::new())), - Arc::new(RwLock::new(HashMap::new())), - Arc::new(RwLock::new(HashMap::new())), - ) - } - - #[rstest] - #[tokio::test] - async fn receipt_checks_adapter_test( - domain_separator: Eip712Domain, - checks: Vec, - ) { - let sender_ids = [ - Address::from_str("0xfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb").unwrap(), - Address::from_str("0xfafafafafafafafafafafafafafafafafafafafa").unwrap(), - Address::from_str("0xadadadadadadadadadadadadadadadadadadadad").unwrap(), - ]; - let _sender_ids_set = Arc::new(RwLock::new(HashSet::from(sender_ids))); - - let allocation_ids = [ - Address::from_str("0xabababababababababababababababababababab").unwrap(), - Address::from_str("0xbabababababababababababababababababababa").unwrap(), - Address::from_str("0xdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdf").unwrap(), - ]; - let _allocation_ids_set = Arc::new(RwLock::new(HashSet::from(allocation_ids))); - - let wallet: LocalWallet = MnemonicBuilder::::default() - .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") - .build() - .unwrap(); - let value = 100u128; - - let receipts: HashMap = stream::iter(0..10) - .then(|id| { - let wallet = wallet.clone(); - let domain_separator = &domain_separator; - let checks = checks.clone(); - async move { - ( - id, - ReceivedReceipt::new( - EIP712SignedMessage::new( - domain_separator, - Receipt::new(allocation_ids[0], value).unwrap(), - &wallet, - ) - .unwrap(), - id, - &checks, - ), - ) - } - }) - .collect::>() - .await; - let receipt_storage = Arc::new(RwLock::new(receipts)); - - let query_appraisals = (0..11).map(|id| (id, 100u128)).collect::>(); - - let _query_appraisals_storage = Arc::new(RwLock::new(query_appraisals)); - - // let receipt_checks_adapter = ReceiptChecksAdapterMock::new( - // Arc::clone(&receipt_storage), - // query_appraisals_storage, - // allocation_ids_set, - // sender_ids_set, - // ); - - let new_receipt = ( - 10u64, - ReceivedReceipt::new( - EIP712SignedMessage::new( - &domain_separator, - Receipt::new(allocation_ids[0], value).unwrap(), - &wallet, - ) - .unwrap(), - 10u64, - &checks, - ), - ); - - let unique_receipt_id = 0u64; - receipt_storage - .write() - .await - .insert(unique_receipt_id, new_receipt.1.clone()); - - // assert!(receipt_checks_adapter - // .is_unique(new_receipt.1.signed_receipt(), unique_receipt_id) - // .await - // .unwrap()); - // assert!(receipt_checks_adapter - // .is_valid_allocation_id(new_receipt.1.signed_receipt().message.allocation_id) - // .await - // .unwrap()); - // TODO: Add check when sender_id is available from received receipt (issue: #56) - // assert!(receipt_checks_adapter.is_valid_sender_id(sender_id)); - // assert!(receipt_checks_adapter - // .is_valid_value( - // new_receipt.1.signed_receipt().message.value, - // new_receipt.1.query_id() - // ) - // .await - // .unwrap()); - } -} From 0a3b983b5f20b27c0afdc02d9db682481e7493a9 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Tue, 5 Mar 2024 14:53:08 -0300 Subject: [PATCH 30/32] refactor!: remove timestamp check from manager Signed-off-by: Gustavo Inacio --- tap_core/src/adapters/mock/executor_mock.rs | 53 ++++---- .../src/adapters/test/escrow_adapter_test.rs | 58 +++++---- .../adapters/test/rav_storage_adapter_test.rs | 37 +++--- .../test/receipt_storage_adapter_test.rs | 120 +++++++++--------- tap_core/src/checks/mod.rs | 15 +-- tap_core/src/tap_manager/manager.rs | 20 +-- tap_core/src/tap_manager/test/manager_test.rs | 108 ++++++---------- .../received_receipt_unit_test.rs | 79 ++++++------ .../tests/indexer_mock/mod.rs | 1 - tap_integration_tests/tests/showcase.rs | 44 +++---- 10 files changed, 240 insertions(+), 295 deletions(-) diff --git a/tap_core/src/adapters/mock/executor_mock.rs b/tap_core/src/adapters/mock/executor_mock.rs index f8f62ed8..28ee8194 100644 --- a/tap_core/src/adapters/mock/executor_mock.rs +++ b/tap_core/src/adapters/mock/executor_mock.rs @@ -5,6 +5,7 @@ use crate::adapters::escrow_adapter::EscrowAdapter; use crate::adapters::receipt_storage_adapter::{ safe_truncate_receipts, ReceiptRead, ReceiptStore, StoredReceipt, }; +use crate::checks::TimestampCheck; use crate::tap_receipt::ReceivedReceipt; use crate::{ adapters::rav_storage_adapter::{RAVRead, RAVStore}, @@ -13,8 +14,8 @@ use crate::{ use alloy_primitives::Address; use async_trait::async_trait; use std::ops::RangeBounds; +use std::sync::RwLock; use std::{collections::HashMap, sync::Arc}; -use tokio::sync::RwLock; pub type EscrowStorage = Arc>>; pub type QueryAppraisals = Arc>>; @@ -37,6 +38,8 @@ pub struct ExecutorMock { unique_id: Arc>, sender_escrow_storage: EscrowStorage, + + timestamp_check: Arc, } impl ExecutorMock { @@ -44,12 +47,14 @@ impl ExecutorMock { rav_storage: RAVStorage, receipt_storage: ReceiptStorage, sender_escrow_storage: EscrowStorage, + timestamp_check: Arc, ) -> Self { ExecutorMock { rav_storage, receipt_storage, unique_id: Arc::new(RwLock::new(0)), sender_escrow_storage, + timestamp_check, } } @@ -57,7 +62,7 @@ impl ExecutorMock { &self, receipt_id: u64, ) -> Result { - let receipt_storage = self.receipt_storage.read().await; + let receipt_storage = self.receipt_storage.read().unwrap(); receipt_storage .get(&receipt_id) @@ -71,7 +76,7 @@ impl ExecutorMock { &self, timestamp_ns: u64, ) -> Result, AdapterErrorMock> { - let receipt_storage = self.receipt_storage.read().await; + let receipt_storage = self.receipt_storage.read().unwrap(); Ok(receipt_storage .iter() .filter(|(_, rx_receipt)| { @@ -90,7 +95,7 @@ impl ExecutorMock { } pub async fn remove_receipt_by_id(&mut self, receipt_id: u64) -> Result<(), AdapterErrorMock> { - let mut receipt_storage = self.receipt_storage.write().await; + let mut receipt_storage = self.receipt_storage.write().unwrap(); receipt_storage .remove(&receipt_id) .map(|_| ()) @@ -114,8 +119,10 @@ impl RAVStore for ExecutorMock { type AdapterError = AdapterErrorMock; async fn update_last_rav(&self, rav: SignedRAV) -> Result<(), Self::AdapterError> { - let mut rav_storage = self.rav_storage.write().await; + let mut rav_storage = self.rav_storage.write().unwrap(); + let timestamp = rav.message.timestampNs; *rav_storage = Some(rav); + self.timestamp_check.update_min_timestamp_ns(timestamp); Ok(()) } } @@ -125,7 +132,7 @@ impl RAVRead for ExecutorMock { type AdapterError = AdapterErrorMock; async fn last_rav(&self) -> Result, Self::AdapterError> { - Ok(self.rav_storage.read().await.clone()) + Ok(self.rav_storage.read().unwrap().clone()) } } @@ -133,9 +140,9 @@ impl RAVRead for ExecutorMock { impl ReceiptStore for ExecutorMock { type AdapterError = AdapterErrorMock; async fn store_receipt(&self, receipt: ReceivedReceipt) -> Result { - let mut id_pointer = self.unique_id.write().await; + let mut id_pointer = self.unique_id.write().unwrap(); let id_previous = *id_pointer; - let mut receipt_storage = self.receipt_storage.write().await; + let mut receipt_storage = self.receipt_storage.write().unwrap(); receipt_storage.insert(*id_pointer, receipt); *id_pointer += 1; Ok(id_previous) @@ -145,7 +152,7 @@ impl ReceiptStore for ExecutorMock { receipt_id: u64, receipt: ReceivedReceipt, ) -> Result<(), Self::AdapterError> { - let mut receipt_storage = self.receipt_storage.write().await; + let mut receipt_storage = self.receipt_storage.write().unwrap(); if !receipt_storage.contains_key(&receipt_id) { return Err(AdapterErrorMock::AdapterError { @@ -154,14 +161,14 @@ impl ReceiptStore for ExecutorMock { }; receipt_storage.insert(receipt_id, receipt); - *self.unique_id.write().await += 1; + *self.unique_id.write().unwrap() += 1; Ok(()) } async fn remove_receipts_in_timestamp_range + std::marker::Send>( &self, timestamp_ns: R, ) -> Result<(), Self::AdapterError> { - let mut receipt_storage = self.receipt_storage.write().await; + let mut receipt_storage = self.receipt_storage.write().unwrap(); receipt_storage.retain(|_, rx_receipt| { !timestamp_ns.contains(&rx_receipt.signed_receipt().message.timestamp_ns) }); @@ -177,7 +184,7 @@ impl ReceiptRead for ExecutorMock { timestamp_range_ns: R, limit: Option, ) -> Result, Self::AdapterError> { - let receipt_storage = self.receipt_storage.read().await; + let receipt_storage = self.receipt_storage.read().unwrap(); let mut receipts_in_range: Vec<(u64, ReceivedReceipt)> = receipt_storage .iter() .filter(|(_, rx_receipt)| { @@ -194,8 +201,8 @@ impl ReceiptRead for ExecutorMock { } impl ExecutorMock { - pub async fn escrow(&self, sender_id: Address) -> Result { - let sender_escrow_storage = self.sender_escrow_storage.read().await; + pub fn escrow(&self, sender_id: Address) -> Result { + let sender_escrow_storage = self.sender_escrow_storage.read().unwrap(); if let Some(escrow) = sender_escrow_storage.get(&sender_id) { return Ok(*escrow); } @@ -204,23 +211,19 @@ impl ExecutorMock { }) } - pub async fn increase_escrow(&mut self, sender_id: Address, value: u128) { - let mut sender_escrow_storage = self.sender_escrow_storage.write().await; + pub fn increase_escrow(&mut self, sender_id: Address, value: u128) { + let mut sender_escrow_storage = self.sender_escrow_storage.write().unwrap(); if let Some(current_value) = sender_escrow_storage.get(&sender_id) { - let mut sender_escrow_storage = self.sender_escrow_storage.write().await; + let mut sender_escrow_storage = self.sender_escrow_storage.write().unwrap(); 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; + pub fn reduce_escrow(&self, sender_id: Address, value: u128) -> Result<(), AdapterErrorMock> { + let mut sender_escrow_storage = self.sender_escrow_storage.write().unwrap(); if let Some(current_value) = sender_escrow_storage.get(&sender_id) { let checked_new_value = current_value.checked_sub(value); @@ -239,13 +242,13 @@ impl ExecutorMock { impl EscrowAdapter for ExecutorMock { type AdapterError = AdapterErrorMock; async fn get_available_escrow(&self, sender_id: Address) -> Result { - self.escrow(sender_id).await + self.escrow(sender_id) } async fn subtract_escrow( &self, sender_id: Address, value: u128, ) -> Result<(), Self::AdapterError> { - self.reduce_escrow(sender_id, value).await + self.reduce_escrow(sender_id, value) } } diff --git a/tap_core/src/adapters/test/escrow_adapter_test.rs b/tap_core/src/adapters/test/escrow_adapter_test.rs index edefae6d..18c28fd5 100644 --- a/tap_core/src/adapters/test/escrow_adapter_test.rs +++ b/tap_core/src/adapters/test/escrow_adapter_test.rs @@ -3,24 +3,37 @@ #[cfg(test)] mod escrow_adapter_unit_test { - use std::{collections::HashMap, sync::Arc}; + use std::{ + collections::HashMap, + sync::{Arc, RwLock}, + }; use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer}; use rstest::*; - use tokio::sync::RwLock; - use crate::adapters::{escrow_adapter::EscrowAdapter, executor_mock::ExecutorMock}; + use crate::{ + adapters::{escrow_adapter::EscrowAdapter, executor_mock::ExecutorMock}, + checks::TimestampCheck, + }; - #[rstest] - #[tokio::test] - async fn escrow_adapter_test() { + #[fixture] + fn executor() -> ExecutorMock { + let escrow_storage = Arc::new(RwLock::new(HashMap::new())); 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 mut escrow_adapter = - ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()); + let timestamp_check = Arc::new(TimestampCheck::new(0)); + ExecutorMock::new( + rav_storage, + receipt_storage.clone(), + escrow_storage.clone(), + timestamp_check, + ) + } + #[rstest] + #[tokio::test] + async fn escrow_adapter_test(mut executor: ExecutorMock) { let wallet: LocalWallet = MnemonicBuilder::::default() .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") .build() @@ -39,42 +52,31 @@ mod escrow_adapter_unit_test { let initial_value = 500u128; - escrow_adapter - .increase_escrow(sender_id, initial_value) - .await; + executor.increase_escrow(sender_id, initial_value); // Check that sender exists and has valid value through adapter - assert!(escrow_adapter.get_available_escrow(sender_id).await.is_ok()); + assert!(executor.get_available_escrow(sender_id).await.is_ok()); assert_eq!( - escrow_adapter - .get_available_escrow(sender_id) - .await - .unwrap(), + executor.get_available_escrow(sender_id).await.unwrap(), initial_value ); // Check that subtracting is valid for valid sender, and results in expected value - assert!(escrow_adapter + assert!(executor .subtract_escrow(sender_id, initial_value) .await .is_ok()); - assert!(escrow_adapter.get_available_escrow(sender_id).await.is_ok()); - assert_eq!( - escrow_adapter - .get_available_escrow(sender_id) - .await - .unwrap(), - 0 - ); + assert!(executor.get_available_escrow(sender_id).await.is_ok()); + assert_eq!(executor.get_available_escrow(sender_id).await.unwrap(), 0); // Check that subtracting to negative escrow results in err - assert!(escrow_adapter + assert!(executor .subtract_escrow(sender_id, initial_value) .await .is_err()); // Check that accessing non initialized sender results in err - assert!(escrow_adapter + assert!(executor .get_available_escrow(invalid_sender_id) .await .is_err()); diff --git a/tap_core/src/adapters/test/rav_storage_adapter_test.rs b/tap_core/src/adapters/test/rav_storage_adapter_test.rs index 0555eed0..3b51999b 100644 --- a/tap_core/src/adapters/test/rav_storage_adapter_test.rs +++ b/tap_core/src/adapters/test/rav_storage_adapter_test.rs @@ -4,6 +4,7 @@ #[cfg(test)] mod rav_storage_adapter_unit_test { use std::collections::HashMap; + use std::sync::RwLock; use std::{str::FromStr, sync::Arc}; use alloy_primitives::Address; @@ -11,8 +12,8 @@ mod rav_storage_adapter_unit_test { use ethers::signers::coins_bip39::English; use ethers::signers::{LocalWallet, MnemonicBuilder}; use rstest::*; - use tokio::sync::RwLock; + use crate::checks::TimestampCheck; use crate::{ adapters::{ executor_mock::ExecutorMock, @@ -29,16 +30,24 @@ mod rav_storage_adapter_unit_test { tap_eip712_domain(1, Address::from([0x11u8; 20])) } - #[rstest] - #[tokio::test] - async fn rav_storage_adapter_test(domain_separator: Eip712Domain) { + #[fixture] + fn executor() -> ExecutorMock { + let escrow_storage = Arc::new(RwLock::new(HashMap::new())); 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 rav_storage_adapter = - ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()); + let timestamp_check = Arc::new(TimestampCheck::new(0)); + ExecutorMock::new( + rav_storage, + receipt_storage.clone(), + escrow_storage.clone(), + timestamp_check, + ) + } + #[rstest] + #[tokio::test] + async fn rav_storage_adapter_test(domain_separator: Eip712Domain, executor: ExecutorMock) { let wallet: LocalWallet = MnemonicBuilder::::default() .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") .build() @@ -70,13 +79,10 @@ mod rav_storage_adapter_unit_test { ) .unwrap(); - rav_storage_adapter - .update_last_rav(signed_rav.clone()) - .await - .unwrap(); + executor.update_last_rav(signed_rav.clone()).await.unwrap(); // Retreive rav - let retrieved_rav = rav_storage_adapter.last_rav().await; + let retrieved_rav = executor.last_rav().await; assert!(retrieved_rav.unwrap().unwrap() == signed_rav); // Testing the last rav update... @@ -102,13 +108,10 @@ mod rav_storage_adapter_unit_test { .unwrap(); // Update the last rav - rav_storage_adapter - .update_last_rav(signed_rav.clone()) - .await - .unwrap(); + executor.update_last_rav(signed_rav.clone()).await.unwrap(); // Retreive rav - let retrieved_rav = rav_storage_adapter.last_rav().await; + let retrieved_rav = executor.last_rav().await; assert!(retrieved_rav.unwrap().unwrap() == signed_rav); } } diff --git a/tap_core/src/adapters/test/receipt_storage_adapter_test.rs b/tap_core/src/adapters/test/receipt_storage_adapter_test.rs index 453f5f45..b6158edf 100644 --- a/tap_core/src/adapters/test/receipt_storage_adapter_test.rs +++ b/tap_core/src/adapters/test/receipt_storage_adapter_test.rs @@ -7,15 +7,9 @@ mod receipt_storage_adapter_unit_test { use rand::thread_rng; use std::collections::{HashMap, HashSet}; use std::str::FromStr; - use std::sync::Arc; - - use alloy_primitives::Address; - use alloy_sol_types::Eip712Domain; - use ethers::signers::coins_bip39::English; - use ethers::signers::{LocalWallet, MnemonicBuilder}; - use rstest::*; - use tokio::sync::RwLock; + use std::sync::{Arc, RwLock}; + use crate::checks::TimestampCheck; use crate::{ adapters::{executor_mock::ExecutorMock, receipt_storage_adapter::ReceiptStore}, checks::{mock::get_full_list_of_checks, ReceiptCheck}, @@ -23,32 +17,55 @@ mod receipt_storage_adapter_unit_test { tap_eip712_domain, tap_receipt::{Receipt, ReceivedReceipt}, }; + use alloy_primitives::Address; + use alloy_sol_types::Eip712Domain; + use ethers::signers::coins_bip39::English; + use ethers::signers::{LocalWallet, MnemonicBuilder}; + use rstest::*; #[fixture] fn domain_separator() -> Eip712Domain { tap_eip712_domain(1, Address::from([0x11u8; 20])) } + struct ExecutorFixture { + executor: ExecutorMock, + checks: Vec, + } + #[fixture] - fn checks(domain_separator: Eip712Domain) -> Vec { - get_full_list_of_checks( + fn executor_mock(domain_separator: Eip712Domain) -> ExecutorFixture { + let escrow_storage = Arc::new(RwLock::new(HashMap::new())); + let rav_storage = Arc::new(RwLock::new(None)); + let query_appraisals = Arc::new(RwLock::new(HashMap::new())); + let receipt_storage = Arc::new(RwLock::new(HashMap::new())); + + let timestamp_check = Arc::new(TimestampCheck::new(0)); + let executor = ExecutorMock::new( + rav_storage, + receipt_storage.clone(), + escrow_storage.clone(), + timestamp_check.clone(), + ); + let mut checks = get_full_list_of_checks( domain_separator, HashSet::new(), Arc::new(RwLock::new(HashSet::new())), - Arc::new(RwLock::new(HashMap::new())), - Arc::new(RwLock::new(HashMap::new())), - ) + receipt_storage, + query_appraisals.clone(), + ); + checks.push(timestamp_check); + + ExecutorFixture { executor, checks } } #[rstest] #[tokio::test] - async fn receipt_adapter_test(domain_separator: Eip712Domain, checks: Vec) { - 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 mut receipt_adapter = - ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()); + async fn receipt_adapter_test(domain_separator: Eip712Domain, executor_mock: ExecutorFixture) { + let ExecutorFixture { + mut executor, + checks, + } = executor_mock; let wallet: LocalWallet = MnemonicBuilder::::default() .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") @@ -72,48 +89,37 @@ mod receipt_storage_adapter_unit_test { &checks, ); - let receipt_store_result = receipt_adapter.store_receipt(received_receipt).await; + let receipt_store_result = executor.store_receipt(received_receipt).await; assert!(receipt_store_result.is_ok()); let receipt_id = receipt_store_result.unwrap(); // Retreive receipt with id expected to be valid - assert!(receipt_adapter - .retrieve_receipt_by_id(receipt_id) - .await - .is_ok()); + assert!(executor.retrieve_receipt_by_id(receipt_id).await.is_ok()); // Retreive receipt with arbitrary id expected to be invalid - assert!(receipt_adapter.retrieve_receipt_by_id(999).await.is_err()); + assert!(executor.retrieve_receipt_by_id(999).await.is_err()); // Remove receipt with id expected to be valid - assert!(receipt_adapter - .remove_receipt_by_id(receipt_id) - .await - .is_ok()); + assert!(executor.remove_receipt_by_id(receipt_id).await.is_ok()); // Remove receipt with arbitrary id expected to be invalid - assert!(receipt_adapter.remove_receipt_by_id(999).await.is_err()); + assert!(executor.remove_receipt_by_id(999).await.is_err()); // Retreive receipt that was removed previously - assert!(receipt_adapter - .retrieve_receipt_by_id(receipt_id) - .await - .is_err()); + assert!(executor.retrieve_receipt_by_id(receipt_id).await.is_err()); // Remove receipt that was removed previously - assert!(receipt_adapter - .remove_receipt_by_id(receipt_id) - .await - .is_err()); + assert!(executor.remove_receipt_by_id(receipt_id).await.is_err()); } #[rstest] #[tokio::test] - async fn multi_receipt_adapter_test(domain_separator: Eip712Domain, checks: Vec) { - 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 mut receipt_adapter = - ExecutorMock::new(rav_storage, receipt_storage, sender_escrow_storage.clone()); + async fn multi_receipt_adapter_test( + domain_separator: Eip712Domain, + executor_mock: ExecutorFixture, + ) { + let ExecutorFixture { + mut executor, + checks, + } = executor_mock; let wallet: LocalWallet = MnemonicBuilder::::default() .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") @@ -141,7 +147,7 @@ mod receipt_storage_adapter_unit_test { let mut receipt_timestamps = Vec::new(); for received_receipt in received_receipts { receipt_ids.push( - receipt_adapter + executor .store_receipt(received_receipt.clone()) .await .unwrap(), @@ -150,23 +156,23 @@ mod receipt_storage_adapter_unit_test { } // Retreive receipts with timestamp - assert!(receipt_adapter + assert!(executor .retrieve_receipts_by_timestamp(receipt_timestamps[0]) .await .is_ok()); - assert!(!receipt_adapter + assert!(!executor .retrieve_receipts_by_timestamp(receipt_timestamps[0]) .await .unwrap() .is_empty()); // Retreive receipts before timestamp - assert!(receipt_adapter + assert!(executor .retrieve_receipts_upto_timestamp(receipt_timestamps[3]) .await .is_ok()); assert!( - receipt_adapter + executor .retrieve_receipts_upto_timestamp(receipt_timestamps[3]) .await .unwrap() @@ -175,21 +181,18 @@ mod receipt_storage_adapter_unit_test { ); // Remove all receipts with one call - assert!(receipt_adapter + assert!(executor .remove_receipts_by_ids(receipt_ids.as_slice()) .await .is_ok()); // Removal should no longer be valid - assert!(receipt_adapter + assert!(executor .remove_receipts_by_ids(receipt_ids.as_slice()) .await .is_err()); // Retrieval should be invalid for receipt_id in receipt_ids { - assert!(receipt_adapter - .retrieve_receipt_by_id(receipt_id) - .await - .is_err()); + assert!(executor.retrieve_receipt_by_id(receipt_id).await.is_err()); } } @@ -202,7 +205,7 @@ mod receipt_storage_adapter_unit_test { #[test] fn safe_truncate_receipts_test( domain_separator: Eip712Domain, - checks: Vec, + executor_mock: ExecutorFixture, #[case] input: Vec, #[case] limit: u64, #[case] expected: Vec, @@ -211,6 +214,7 @@ mod receipt_storage_adapter_unit_test { .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") .build() .unwrap(); + let checks = executor_mock.checks; // Vec of (id, receipt) let mut receipts_orig: Vec<(u64, ReceivedReceipt)> = Vec::new(); diff --git a/tap_core/src/checks/mod.rs b/tap_core/src/checks/mod.rs index aaf89ed0..709708ea 100644 --- a/tap_core/src/checks/mod.rs +++ b/tap_core/src/checks/mod.rs @@ -3,8 +3,7 @@ use crate::tap_receipt::{Checking, ReceiptError, ReceiptResult, ReceiptWithState}; use serde::{Deserialize, Serialize}; -use std::sync::Arc; -use tokio::sync::RwLock; +use std::sync::{Arc, RwLock}; pub type ReceiptCheck = Arc; @@ -70,8 +69,8 @@ impl TimestampCheck { } } /// Updates the minimum timestamp that will be accepted for a receipt (exclusive). - pub async fn update_min_timestamp_ns(&self, min_timestamp_ns: u64) { - *self.min_timestamp_ns.write().await = min_timestamp_ns; + pub fn update_min_timestamp_ns(&self, min_timestamp_ns: u64) { + *self.min_timestamp_ns.write().unwrap() = min_timestamp_ns; } } @@ -79,7 +78,7 @@ impl TimestampCheck { #[typetag::serde] impl Check for TimestampCheck { async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { - let min_timestamp_ns = *self.min_timestamp_ns.read().await; + let min_timestamp_ns = *self.min_timestamp_ns.read().unwrap(); let signed_receipt = receipt.signed_receipt(); if signed_receipt.message.timestamp_ns <= min_timestamp_ns { return Err(ReceiptError::InvalidTimestamp { @@ -136,7 +135,7 @@ pub mod mock { #[typetag::serde] impl Check for UniqueCheck { async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { - let receipt_storage = self.receipt_storage.read().await; + let receipt_storage = self.receipt_storage.read().unwrap(); // let receipt_id = receipt. let unique = receipt_storage .iter() @@ -179,7 +178,7 @@ pub mod mock { async fn check(&self, receipt: &ReceiptWithState) -> ReceiptResult<()> { let query_id = receipt.query_id; let value = receipt.signed_receipt().message.value; - let query_appraisals = self.query_appraisals.read().await; + let query_appraisals = self.query_appraisals.read().unwrap(); let appraised_value = query_appraisals .get(&query_id) @@ -211,7 +210,7 @@ pub mod mock { if self .allocation_ids .read() - .await + .unwrap() .contains(&received_allocation_id) { Ok(()) diff --git a/tap_core/src/tap_manager/manager.rs b/tap_core/src/tap_manager/manager.rs index 2af6e8dd..1f959a63 100644 --- a/tap_core/src/tap_manager/manager.rs +++ b/tap_core/src/tap_manager/manager.rs @@ -1,8 +1,6 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use std::sync::Arc; - use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; use futures::Future; @@ -14,7 +12,7 @@ use crate::{ rav_storage_adapter::{RAVRead, RAVStore}, receipt_storage_adapter::{ReceiptRead, ReceiptStore}, }, - checks::{ReceiptCheck, TimestampCheck}, + checks::ReceiptCheck, receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::{ CategorizedReceiptsWithState, Failed, ReceiptAuditor, ReceiptWithId, ReceiptWithState, @@ -31,8 +29,6 @@ pub struct Manager { /// Struct responsible for doing checks for receipt. Ownership stays with manager allowing manager /// to update configuration ( like minimum timestamp ). receipt_auditor: ReceiptAuditor, - - timestamp_check: Arc, } impl Manager @@ -46,17 +42,13 @@ where pub fn new( domain_separator: Eip712Domain, executor: E, - mut required_checks: Vec, - starting_min_timestamp_ns: u64, + required_checks: Vec, ) -> Self { - let timestamp_check = Arc::new(TimestampCheck::new(starting_min_timestamp_ns)); - required_checks.push(timestamp_check.clone()); let receipt_auditor = ReceiptAuditor::new(domain_separator, executor.clone()); Self { executor, required_checks, receipt_auditor, - timestamp_check, } } } @@ -92,10 +84,6 @@ where }); } - self.timestamp_check - .update_min_timestamp_ns(expected_rav.timestampNs) - .await; - self.executor .update_last_rav(signed_rav) .await @@ -103,10 +91,6 @@ where source_error: anyhow::Error::new(err), })?; - self.timestamp_check - .update_min_timestamp_ns(expected_rav.timestampNs) - .await; - Ok(()) } } diff --git a/tap_core/src/tap_manager/test/manager_test.rs b/tap_core/src/tap_manager/test/manager_test.rs index 93c03d67..c231559d 100644 --- a/tap_core/src/tap_manager/test/manager_test.rs +++ b/tap_core/src/tap_manager/test/manager_test.rs @@ -1,20 +1,24 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use std::{collections::HashMap, ops::Range, str::FromStr, sync::Arc}; +use std::{ + collections::HashMap, + ops::Range, + str::FromStr, + sync::{Arc, RwLock}, +}; use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer}; use rstest::*; -use tokio::sync::RwLock; use super::super::Manager; use crate::{ adapters::{ - executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals, RAVStorage, ReceiptStorage}, + executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, receipt_storage_adapter::ReceiptRead, }, - checks::{mock::get_full_list_of_checks, ReceiptCheck}, + checks::{mock::get_full_list_of_checks, ReceiptCheck, TimestampCheck}, eip_712_signed_message::EIP712SignedMessage, get_current_timestamp_u64_ns, tap_eip712_domain, tap_receipt::Receipt, @@ -55,26 +59,6 @@ fn sender_ids() -> Vec
{ ] } -#[fixture] -fn receipt_storage() -> ReceiptStorage { - Arc::new(RwLock::new(HashMap::new())) -} - -#[fixture] -fn query_appraisal_storage() -> QueryAppraisals { - Arc::new(RwLock::new(HashMap::new())) -} - -#[fixture] -fn rav_storage() -> RAVStorage { - Arc::new(RwLock::new(None)) -} - -#[fixture] -fn escrow_storage() -> EscrowStorage { - Arc::new(RwLock::new(HashMap::new())) -} - #[fixture] fn domain_separator() -> Eip712Domain { tap_eip712_domain(1, Address::from([0x11u8; 20])) @@ -92,25 +76,32 @@ fn executor_mock( domain_separator: Eip712Domain, allocation_ids: Vec
, sender_ids: Vec
, - receipt_storage: ReceiptStorage, - query_appraisal_storage: QueryAppraisals, - rav_storage: RAVStorage, - escrow_storage: EscrowStorage, ) -> ExecutorFixture { - let executor = ExecutorMock::new(rav_storage, receipt_storage.clone(), escrow_storage.clone()); + let escrow_storage = Arc::new(RwLock::new(HashMap::new())); + let rav_storage = Arc::new(RwLock::new(None)); + let query_appraisals = Arc::new(RwLock::new(HashMap::new())); + let receipt_storage = Arc::new(RwLock::new(HashMap::new())); + let timestamp_check = Arc::new(TimestampCheck::new(0)); + let executor = ExecutorMock::new( + rav_storage, + receipt_storage.clone(), + escrow_storage.clone(), + timestamp_check.clone(), + ); - let checks = get_full_list_of_checks( + let mut checks = get_full_list_of_checks( domain_separator, sender_ids.iter().cloned().collect(), Arc::new(RwLock::new(allocation_ids.iter().cloned().collect())), receipt_storage, - query_appraisal_storage.clone(), + query_appraisals.clone(), ); + checks.push(timestamp_check); ExecutorFixture { executor, escrow_storage, - query_appraisals: query_appraisal_storage, + query_appraisals, checks, } } @@ -135,14 +126,7 @@ async fn manager_verify_and_store_varying_initial_checks( .. } = executor_mock; // give receipt 5 second variance for min start time - let starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; - - let manager = Manager::new( - domain_separator.clone(), - executor, - checks.clone(), - starting_min_timestamp, - ); + let manager = Manager::new(domain_separator.clone(), executor, checks.clone()); let query_id = 1; let value = 20u128; @@ -152,8 +136,8 @@ async fn manager_verify_and_store_varying_initial_checks( &keys.0, ) .unwrap(); - query_appraisals.write().await.insert(query_id, value); - escrow_storage.write().await.insert(keys.1, 999999); + query_appraisals.write().unwrap().insert(query_id, value); + escrow_storage.write().unwrap().insert(keys.1, 999999); assert!(manager .verify_and_store_receipt(signed_receipt, query_id, &checks[range]) @@ -181,16 +165,9 @@ async fn manager_create_rav_request_all_valid_receipts( .. } = executor_mock; let initial_checks = &checks[range]; - // give receipt 5 second variance for min start time - let starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; - let manager = Manager::new( - domain_separator.clone(), - executor, - checks.clone(), - starting_min_timestamp, - ); - escrow_storage.write().await.insert(keys.1, 999999); + let manager = Manager::new(domain_separator.clone(), executor, checks.clone()); + escrow_storage.write().unwrap().insert(keys.1, 999999); let mut stored_signed_receipts = Vec::new(); for query_id in 0..10 { @@ -202,7 +179,7 @@ async fn manager_create_rav_request_all_valid_receipts( ) .unwrap(); stored_signed_receipts.push(signed_receipt.clone()); - query_appraisals.write().await.insert(query_id, value); + query_appraisals.write().unwrap().insert(query_id, value); assert!(manager .verify_and_store_receipt(signed_receipt, query_id, initial_checks) .await @@ -255,16 +232,10 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts( } = executor_mock; let initial_checks = &checks[range]; // give receipt 5 second variance for min start time - let starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; - let manager = Manager::new( - domain_separator.clone(), - executor, - checks.clone(), - starting_min_timestamp, - ); + let manager = Manager::new(domain_separator.clone(), executor, checks.clone()); - escrow_storage.write().await.insert(keys.1, 999999); + escrow_storage.write().unwrap().insert(keys.1, 999999); let mut stored_signed_receipts = Vec::new(); let mut expected_accumulated_value = 0; @@ -277,7 +248,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts( ) .unwrap(); stored_signed_receipts.push(signed_receipt.clone()); - query_appraisals.write().await.insert(query_id, value); + query_appraisals.write().unwrap().insert(query_id, value); assert!(manager .verify_and_store_receipt(signed_receipt, query_id, initial_checks) .await @@ -325,7 +296,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts( ) .unwrap(); stored_signed_receipts.push(signed_receipt.clone()); - query_appraisals.write().await.insert(query_id, value); + query_appraisals.write().unwrap().insert(query_id, value); assert!(manager .verify_and_store_receipt(signed_receipt, query_id, initial_checks) .await @@ -385,14 +356,9 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim // give receipt 5 second variance for min start time let starting_min_timestamp = get_current_timestamp_u64_ns().unwrap() - 500000000; - let manager = Manager::new( - domain_separator.clone(), - executor, - checks.clone(), - starting_min_timestamp, - ); + let manager = Manager::new(domain_separator.clone(), executor, checks.clone()); - escrow_storage.write().await.insert(keys.1, 999999); + escrow_storage.write().unwrap().insert(keys.1, 999999); let mut stored_signed_receipts = Vec::new(); let mut expected_accumulated_value = 0; @@ -402,7 +368,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim receipt.timestamp_ns = starting_min_timestamp + query_id + 1; let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &keys.0).unwrap(); stored_signed_receipts.push(signed_receipt.clone()); - query_appraisals.write().await.insert(query_id, value); + query_appraisals.write().unwrap().insert(query_id, value); assert!(manager .verify_and_store_receipt(signed_receipt, query_id, initial_checks) .await @@ -457,7 +423,7 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim receipt.timestamp_ns = starting_min_timestamp + query_id + 1; let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &keys.0).unwrap(); stored_signed_receipts.push(signed_receipt.clone()); - query_appraisals.write().await.insert(query_id, value); + query_appraisals.write().unwrap().insert(query_id, value); assert!(manager .verify_and_store_receipt(signed_receipt, query_id, initial_checks) .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 eb8c9fee..c339de74 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 @@ -1,19 +1,20 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use std::{collections::HashMap, str::FromStr, sync::Arc}; +use std::{ + collections::HashMap, + str::FromStr, + sync::{Arc, RwLock}, +}; use alloy_primitives::Address; use alloy_sol_types::Eip712Domain; use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer}; use rstest::*; -use tokio::sync::RwLock; use crate::{ - adapters::executor_mock::{ - EscrowStorage, ExecutorMock, QueryAppraisals, RAVStorage, ReceiptStorage, - }, - checks::{mock::get_full_list_of_checks, ReceiptCheck}, + adapters::executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals}, + checks::{mock::get_full_list_of_checks, ReceiptCheck, TimestampCheck}, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, tap_receipt::{Receipt, ReceiptAuditor, ReceiptCheckResults, ReceivedReceipt}, @@ -80,26 +81,6 @@ fn sender_ids() -> Vec
{ ] } -#[fixture] -fn receipt_storage() -> ReceiptStorage { - Arc::new(RwLock::new(HashMap::new())) -} - -#[fixture] -fn query_appraisal_storage() -> QueryAppraisals { - Arc::new(RwLock::new(HashMap::new())) -} - -#[fixture] -fn rav_storage() -> RAVStorage { - Arc::new(RwLock::new(None)) -} - -#[fixture] -fn escrow_storage() -> EscrowStorage { - Arc::new(RwLock::new(HashMap::new())) -} - #[fixture] fn domain_separator() -> Eip712Domain { tap_eip712_domain(1, Address::from([0x11u8; 20])) @@ -117,25 +98,32 @@ fn executor_mock( domain_separator: Eip712Domain, allocation_ids: Vec
, sender_ids: Vec
, - receipt_storage: ReceiptStorage, - query_appraisal_storage: QueryAppraisals, - rav_storage: RAVStorage, - escrow_storage: EscrowStorage, ) -> ExecutorFixture { - let executor = ExecutorMock::new(rav_storage, receipt_storage.clone(), escrow_storage.clone()); - - let checks = get_full_list_of_checks( + let escrow_storage = Arc::new(RwLock::new(HashMap::new())); + let rav_storage = Arc::new(RwLock::new(None)); + let receipt_storage = Arc::new(RwLock::new(HashMap::new())); + let query_appraisals = Arc::new(RwLock::new(HashMap::new())); + + let timestamp_check = Arc::new(TimestampCheck::new(0)); + let executor = ExecutorMock::new( + rav_storage, + receipt_storage.clone(), + escrow_storage.clone(), + timestamp_check.clone(), + ); + let mut checks = get_full_list_of_checks( domain_separator, sender_ids.iter().cloned().collect(), Arc::new(RwLock::new(allocation_ids.iter().cloned().collect())), receipt_storage, - query_appraisal_storage.clone(), + query_appraisals.clone(), ); + checks.push(timestamp_check); ExecutorFixture { executor, escrow_storage, - query_appraisals: query_appraisal_storage, + query_appraisals, checks, } } @@ -199,10 +187,13 @@ async fn partial_then_full_check_valid_receipt( // add escrow for sender escrow_storage .write() - .await + .unwrap() .insert(keys.1, query_value + 500); // appraise query - query_appraisals.write().await.insert(query_id, query_value); + query_appraisals + .write() + .unwrap() + .insert(query_id, query_value); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); @@ -258,10 +249,13 @@ async fn partial_then_finalize_valid_receipt( // add escrow for sender escrow_storage .write() - .await + .unwrap() .insert(keys.1, query_value + 500); // appraise query - query_appraisals.write().await.insert(query_id, query_value); + query_appraisals + .write() + .unwrap() + .insert(query_id, query_value); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); @@ -321,10 +315,13 @@ async fn standard_lifetime_valid_receipt( // add escrow for sender escrow_storage .write() - .await + .unwrap() .insert(keys.1, query_value + 500); // appraise query - query_appraisals.write().await.insert(query_id, query_value); + query_appraisals + .write() + .unwrap() + .insert(query_id, query_value); let received_receipt = ReceivedReceipt::new(signed_receipt, query_id, &checks); diff --git a/tap_integration_tests/tests/indexer_mock/mod.rs b/tap_integration_tests/tests/indexer_mock/mod.rs index 9b2d97d6..cc96559b 100644 --- a/tap_integration_tests/tests/indexer_mock/mod.rs +++ b/tap_integration_tests/tests/indexer_mock/mod.rs @@ -77,7 +77,6 @@ where domain_separator, executor, required_checks, - 0, )), initial_checks, receipt_count: Arc::new(AtomicU64::new(0)), diff --git a/tap_integration_tests/tests/showcase.rs b/tap_integration_tests/tests/showcase.rs index c7bc9409..61911669 100644 --- a/tap_integration_tests/tests/showcase.rs +++ b/tap_integration_tests/tests/showcase.rs @@ -9,7 +9,7 @@ use std::{ convert::TryInto, net::{SocketAddr, TcpListener}, str::FromStr, - sync::Arc, + sync::{Arc, RwLock}, }; use alloy_primitives::Address; @@ -21,14 +21,11 @@ use jsonrpsee::{ }; use rand::{rngs::StdRng, Rng, SeedableRng}; use rstest::*; -use tokio::sync::RwLock; use tap_aggregator::{jsonrpsee_helpers, server as agg_server}; use tap_core::{ - adapters::executor_mock::{ - EscrowStorage, ExecutorMock, QueryAppraisals, RAVStorage, ReceiptStorage, - }, - checks::{mock::get_full_list_of_checks, ReceiptCheck}, + adapters::executor_mock::{ExecutorMock, QueryAppraisals}, + checks::{mock::get_full_list_of_checks, ReceiptCheck, TimestampCheck}, eip_712_signed_message::EIP712SignedMessage, tap_eip712_domain, tap_manager::SignedRAV, @@ -156,11 +153,6 @@ fn available_escrow(query_price: &[u128], num_batches: u64) -> u128 { (num_batches as u128) * query_price.iter().sum::() } -#[fixture] -fn receipt_storage() -> ReceiptStorage { - Arc::new(RwLock::new(HashMap::new())) -} - #[fixture] fn query_appraisals(query_price: &[u128]) -> QueryAppraisals { Arc::new(RwLock::new( @@ -172,16 +164,6 @@ fn query_appraisals(query_price: &[u128]) -> QueryAppraisals { )) } -#[fixture] -fn rav_storage() -> RAVStorage { - Arc::new(RwLock::new(None)) -} - -#[fixture] -fn escrow_storage() -> EscrowStorage { - Arc::new(RwLock::new(HashMap::new())) -} - struct ExecutorFixture { executor: ExecutorMock, checks: Vec, @@ -192,20 +174,26 @@ fn executor( domain_separator: Eip712Domain, allocation_ids: Vec
, sender_ids: Vec
, - receipt_storage: ReceiptStorage, query_appraisals: QueryAppraisals, - rav_storage: RAVStorage, - escrow_storage: EscrowStorage, ) -> ExecutorFixture { - let executor = ExecutorMock::new(rav_storage, receipt_storage.clone(), escrow_storage.clone()); - - let checks = get_full_list_of_checks( + let receipt_storage = Arc::new(RwLock::new(HashMap::new())); + let escrow_storage = Arc::new(RwLock::new(HashMap::new())); + let rav_storage = Arc::new(RwLock::new(None)); + let timestamp_check = Arc::new(TimestampCheck::new(0)); + let executor = ExecutorMock::new( + rav_storage, + receipt_storage.clone(), + escrow_storage.clone(), + timestamp_check.clone(), + ); + let mut checks = get_full_list_of_checks( domain_separator, sender_ids.iter().cloned().collect(), Arc::new(RwLock::new(allocation_ids.iter().cloned().collect())), receipt_storage, query_appraisals, ); + checks.push(timestamp_check); ExecutorFixture { executor, checks } } @@ -880,7 +868,7 @@ async fn start_indexer_server( listener.local_addr()?.port() }; - executor.increase_escrow(sender_id, available_escrow).await; + executor.increase_escrow(sender_id, available_escrow); let aggregate_server_address = "http://".to_string() + &agg_server_addr.to_string(); let (server_handle, socket_addr) = indexer_mock::run_server( From 797d1783b4ae429ed280d96309f69fe9a59f732d Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Wed, 6 Mar 2024 10:41:28 -0300 Subject: [PATCH 31/32] chore: update futures dependency Signed-off-by: Gustavo Inacio --- tap_core/Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tap_core/Cargo.toml b/tap_core/Cargo.toml index ba0b35ac..3b9cece3 100644 --- a/tap_core/Cargo.toml +++ b/tap_core/Cargo.toml @@ -25,11 +25,10 @@ strum_macros = "0.24.3" async-trait = "0.1.72" tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } typetag = "0.2.14" -futures = "0.3.30" +futures = "0.3.17" [dev-dependencies] criterion = { version = "0.5", features = ["async_std"] } -futures = "0.3.17" [features] From c5b890a85c1df2a53935b020f820a48023307e79 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Thu, 7 Mar 2024 16:41:12 -0300 Subject: [PATCH 32/32] chore: remove unused comments Signed-off-by: Gustavo Inacio --- .../received_receipt/received_receipt_unit_test.rs | 8 -------- 1 file changed, 8 deletions(-) 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 c339de74..6d1a7034 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 @@ -182,8 +182,6 @@ async fn partial_then_full_check_valid_receipt( let query_id = 1; - // prepare adapters and storage to correctly validate receipt - // add escrow for sender escrow_storage .write() @@ -231,7 +229,6 @@ async fn partial_then_finalize_valid_receipt( query_appraisals, .. } = executor_mock; - // give receipt 5 second variance for min start time let receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); let query_value = 20u128; @@ -244,8 +241,6 @@ async fn partial_then_finalize_valid_receipt( let query_id = 1; - // prepare adapters and storage to correctly validate receipt - // add escrow for sender escrow_storage .write() @@ -297,7 +292,6 @@ async fn standard_lifetime_valid_receipt( query_appraisals, .. } = executor_mock; - // give receipt 5 second variance for min start time let receipt_auditor = ReceiptAuditor::new(domain_separator.clone(), executor); let query_value = 20u128; @@ -310,8 +304,6 @@ async fn standard_lifetime_valid_receipt( let query_id = 1; - // prepare adapters and storage to correctly validate receipt - // add escrow for sender escrow_storage .write()