Skip to content

Commit

Permalink
refactor: create a checks type to move checks more easily
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <[email protected]>
  • Loading branch information
gusinacio committed Mar 7, 2024
1 parent 0839f09 commit 3d14126
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
21 changes: 20 additions & 1 deletion tap_core/src/checks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@
// SPDX-License-Identifier: Apache-2.0

use crate::tap_receipt::{Checking, ReceiptError, ReceiptWithState};
use std::sync::{Arc, RwLock};
use std::{
ops::Deref,
sync::{Arc, RwLock},
};

pub type ReceiptCheck = Arc<dyn Check + Sync + Send>;

pub type CheckResult = anyhow::Result<()>;

pub struct Checks(Arc<[ReceiptCheck]>);

impl Checks {
pub fn new(checks: Vec<ReceiptCheck>) -> Self {
Self(checks.into())
}
}

impl Deref for Checks {
type Target = [ReceiptCheck];

fn deref(&self) -> &Self::Target {
self.0.as_ref()
}
}

#[async_trait::async_trait]
pub trait Check {
async fn check(&self, receipt: &ReceiptWithState<Checking>) -> CheckResult;
Expand Down
10 changes: 3 additions & 7 deletions tap_core/src/tap_manager/manager.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,7 +12,7 @@ use crate::{
rav_storage_adapter::{RAVRead, RAVStore},
receipt_storage_adapter::{ReceiptDelete, ReceiptRead, ReceiptStore},
},
checks::ReceiptCheck,
checks::Checks,
receipt_aggregate_voucher::ReceiptAggregateVoucher,
tap_receipt::{
CategorizedReceiptsWithState, Failed, ReceiptAuditor, ReceiptWithId, ReceiptWithState,
Expand All @@ -28,10 +26,8 @@ pub struct Manager<E> {
executor: E,

/// Checks that must be completed for each receipt before being confirmed or denied for rav request
checks: Arc<[ReceiptCheck]>,
checks: Checks,

// /// Checks that must be completed for each receipt before being confirmed or denied for rav request
// finalize_checks: Arc<[ReceiptCheck]>,
/// Struct responsible for doing checks for receipt. Ownership stays with manager allowing manager
/// to update configuration ( like minimum timestamp ).
receipt_auditor: ReceiptAuditor<E>,
Expand All @@ -48,7 +44,7 @@ where
pub fn new(
domain_separator: Eip712Domain,
executor: E,
initial_checks: impl Into<Arc<[ReceiptCheck]>>,
initial_checks: impl Into<Checks>,
// finalize_checks: impl Into<Arc<[ReceiptCheck]>>,
) -> Self {
let receipt_auditor = ReceiptAuditor::new(domain_separator, executor.clone());
Expand Down
5 changes: 3 additions & 2 deletions tap_core/src/tap_manager/test/manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
executor_mock::{EscrowStorage, ExecutorMock, QueryAppraisals},
receipt_storage_adapter::ReceiptRead,
},
checks::{mock::get_full_list_of_checks, ReceiptCheck, TimestampCheck},
checks::{mock::get_full_list_of_checks, Checks, TimestampCheck},
eip_712_signed_message::EIP712SignedMessage,
get_current_timestamp_u64_ns, tap_eip712_domain,
tap_receipt::Receipt,
Expand Down Expand Up @@ -65,7 +65,7 @@ struct ExecutorFixture {
executor: ExecutorMock,
escrow_storage: EscrowStorage,
query_appraisals: QueryAppraisals,
checks: Vec<ReceiptCheck>,
checks: Checks,
}

#[fixture]
Expand Down Expand Up @@ -93,6 +93,7 @@ fn executor_mock(
query_appraisals.clone(),
);
checks.push(timestamp_check);
let checks = Checks::new(checks);

ExecutorFixture {
executor,
Expand Down
12 changes: 6 additions & 6 deletions tap_integration_tests/tests/indexer_mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tap_core::{
rav_storage_adapter::{RAVRead, RAVStore},
receipt_storage_adapter::{ReceiptRead, ReceiptStore},
},
checks::ReceiptCheck,
checks::Checks,
tap_manager::{Manager, SignedRAV, SignedReceipt},
};
/// Rpc trait represents a JSON-RPC server that has a single async method `request`.
Expand Down Expand Up @@ -64,7 +64,7 @@ where
pub fn new(
domain_separator: Eip712Domain,
executor: E,
required_checks: Vec<ReceiptCheck>,
required_checks: Checks,
threshold: u64,
sender_id: Address,
aggregate_server_address: String,
Expand Down Expand Up @@ -142,11 +142,11 @@ pub async fn run_server<E>(
port: u16, // Port on which the server will listen
domain_separator: Eip712Domain, // EIP712 domain separator
executor: E, // Executor instance
required_checks: Vec<ReceiptCheck>, // Vector of required checks to be performed on each request
threshold: u64, // The count at which a RAV request will be triggered
aggregate_server_address: String, // Address of the aggregator server
required_checks: Checks, // Vector of required checks to be performed on each request
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
sender_id: Address, // The sender address
) -> Result<(ServerHandle, std::net::SocketAddr)>
where
E: ReceiptStore
Expand Down
8 changes: 5 additions & 3 deletions tap_integration_tests/tests/showcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rstest::*;
use tap_aggregator::{jsonrpsee_helpers, server as agg_server};
use tap_core::{
adapters::executor_mock::{ExecutorMock, QueryAppraisals},
checks::{mock::get_full_list_of_checks, ReceiptCheck, TimestampCheck},
checks::{mock::get_full_list_of_checks, Checks, TimestampCheck},
eip_712_signed_message::{EIP712SignedMessage, MessageId},
tap_eip712_domain,
tap_manager::SignedRAV,
Expand Down Expand Up @@ -167,7 +167,7 @@ fn query_appraisals(query_price: &[u128]) -> QueryAppraisals {

struct ExecutorFixture {
executor: ExecutorMock,
checks: Vec<ReceiptCheck>,
checks: Checks,
}

#[fixture]
Expand Down Expand Up @@ -195,6 +195,8 @@ fn executor(
);
checks.push(timestamp_check);

let checks = Checks::new(checks);

ExecutorFixture { executor, checks }
}

Expand Down Expand Up @@ -854,7 +856,7 @@ async fn start_indexer_server(
mut executor: ExecutorMock,
sender_id: Address,
available_escrow: u128,
required_checks: Vec<ReceiptCheck>,
required_checks: Checks,
receipt_threshold: u64,
agg_server_addr: SocketAddr,
) -> Result<(ServerHandle, SocketAddr)> {
Expand Down

0 comments on commit 3d14126

Please sign in to comment.