diff --git a/tap_core/src/manager/tap_manager.rs b/tap_core/src/manager/tap_manager.rs index 33727e5..75da565 100644 --- a/tap_core/src/manager/tap_manager.rs +++ b/tap_core/src/manager/tap_manager.rs @@ -1,8 +1,6 @@ // Copyright 2023-, Semiotic AI, Inc. // SPDX-License-Identifier: Apache-2.0 -use std::marker::PhantomData; - use alloy::{dyn_abi::Eip712Domain, sol_types::SolStruct}; use tap_receipt::rav::Aggregate; @@ -20,7 +18,7 @@ use crate::{ Error, }; -pub struct Manager { +pub struct Manager { /// Context that implements adapters context: E, @@ -30,11 +28,9 @@ pub struct Manager { /// Struct responsible for doing checks for receipt. Ownership stays with manager allowing manager /// to update configuration ( like minimum timestamp ). domain_separator: Eip712Domain, - - _phantom: PhantomData, } -impl Manager { +impl Manager { /// Creates new manager with provided `adapters`, any receipts received by this manager /// will complete all `required_checks` before being accepted or declined from RAV. /// `starting_min_timestamp` will be used as min timestamp until the first RAV request is created. @@ -48,27 +44,40 @@ impl Manager { context, domain_separator, checks: checks.into(), - _phantom: PhantomData, } } -} -impl Manager -where - E: RavStore + SignatureChecker, - Rav: SolStruct + PartialEq + Sync + std::fmt::Debug, -{ + async fn get_previous_rav( + &self, + ) -> Result>, Error> + where + E: RavRead, + { + let previous_rav = self + .context + .last_rav() + .await + .map_err(|err| Error::AdapterError { + source_error: anyhow::Error::new(err), + })?; + Ok(previous_rav) + } + /// Verify `signed_rav` matches all values on `expected_rav`, and that `signed_rav` has a valid signer. /// /// # Errors /// /// 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: Rav, signed_rav: Eip712SignedMessage, - ) -> std::result::Result<(), Error> { + ) -> std::result::Result<(), Error> + where + E: RavStore + SignatureChecker, + Rav: SolStruct + PartialEq + Sync + std::fmt::Debug, + { self.context .check_signature(&signed_rav, &self.domain_separator) .await?; @@ -91,27 +100,10 @@ where } } -impl Manager +impl Manager where - E: RavRead, - Rav: SolStruct, -{ - async fn get_previous_rav(&self) -> Result>, Error> { - let previous_rav = self - .context - .last_rav() - .await - .map_err(|err| Error::AdapterError { - source_error: anyhow::Error::new(err), - })?; - Ok(previous_rav) - } -} - -impl Manager -where - E: ReceiptRead + SignatureChecker, - Rcpt: WithUniqueId + WithValueAndTimestamp + Sync, + E: ReceiptRead, + Rcpt: WithUniqueId + WithValueAndTimestamp, { async fn collect_receipts( &self, @@ -168,14 +160,7 @@ where Ok((checked_receipts, failed_receipts)) } -} -impl Manager -where - E: ReceiptRead + RavRead + SignatureChecker, - Rav: SolStruct + WithValueAndTimestamp + Clone + Aggregate, - Rcpt: WithUniqueId + WithValueAndTimestamp + Sync, -{ /// 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 @@ -191,12 +176,16 @@ where /// previous RAV is greater than the min timestamp. Caused by timestamp /// buffer being too large, or requests coming too soon. /// - pub async fn create_rav_request( + pub async fn create_rav_request( &self, ctx: &Context, timestamp_buffer_ns: u64, receipts_limit: Option, - ) -> Result, Error> { + ) -> Result, Error> + where + E: RavRead, + Rav: SolStruct + WithValueAndTimestamp + Clone + Aggregate, + { let previous_rav = self.get_previous_rav().await?; let min_timestamp_ns = previous_rav .as_ref() @@ -218,10 +207,9 @@ where } } -impl Manager +impl Manager where - E: ReceiptDelete + RavRead, - Rav: SolStruct + WithValueAndTimestamp, + E: ReceiptDelete, { /// Removes obsolete receipts from storage. Obsolete receipts are receipts /// that are older than the last RAV, and therefore already aggregated into the RAV. @@ -233,7 +221,11 @@ where /// Returns [`Error::AdapterError`] if there are any errors while retrieving /// last RAV or removing receipts /// - pub async fn remove_obsolete_receipts(&self) -> Result<(), Error> { + pub async fn remove_obsolete_receipts(&self) -> Result<(), Error> + where + E: RavRead, + Rav: SolStruct + WithValueAndTimestamp, + { match self.get_previous_rav().await? { Some(last_rav) => { self.context @@ -249,7 +241,7 @@ where } } -impl Manager +impl Manager where E: ReceiptStore, { diff --git a/tap_core/tests/manager_test.rs b/tap_core/tests/manager_test.rs index 8bd6ef6..e14c9ea 100644 --- a/tap_core/tests/manager_test.rs +++ b/tap_core/tests/manager_test.rs @@ -128,8 +128,7 @@ async fn manager_verify_and_store_varying_initial_checks( signer, .. } = context; - let manager = - Manager::<_, _, ReceiptAggregateVoucher>::new(domain_separator.clone(), context, checks); + let manager = Manager::new(domain_separator.clone(), context, checks); let value = 20u128; let signed_receipt = Eip712SignedMessage::new( diff --git a/tap_integration_tests/tests/indexer_mock.rs b/tap_integration_tests/tests/indexer_mock.rs index 98d8659..7538ff8 100644 --- a/tap_integration_tests/tests/indexer_mock.rs +++ b/tap_integration_tests/tests/indexer_mock.rs @@ -44,9 +44,9 @@ pub trait Rpc { /// threshold is a limit to which receipt_count can increment, after reaching which RAV request is triggered. /// aggregator_client is an HTTP client used for making JSON-RPC requests to another server. pub struct RpcManager { - manager: Arc>, // Manager object reference counted with an Arc - receipt_count: Arc, // Thread-safe atomic counter for receipts - threshold: u64, // The count at which a RAV request will be triggered + manager: Arc>, // Manager object reference counted with an Arc + 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 } @@ -66,7 +66,7 @@ where aggregate_server_api_version: String, ) -> Result { Ok(Self { - manager: Arc::new(Manager::::new( + manager: Arc::new(Manager::::new( domain_separator, context, required_checks, @@ -184,7 +184,7 @@ where // request_rav function creates a request for aggregate receipts (RAV), sends it to another server and verifies the result. async fn request_rav( - manager: &Arc>, + manager: &Arc>, 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,