Skip to content

Commit

Permalink
Merge pull request #200 from semiotic-ai/gusinacio/executor-refactor
Browse files Browse the repository at this point in the history
refactor!: replace adapter by single executor
  • Loading branch information
gusinacio authored Jan 2, 2024
2 parents d841a44 + c1a6ec9 commit bd6c82e
Show file tree
Hide file tree
Showing 11 changed files with 632 additions and 356 deletions.
2 changes: 1 addition & 1 deletion tap_aggregator/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn check_and_aggregate_receipts(
check_receipt_timestamps(receipts, previous_rav.as_ref())?;

// Get the allocation id from the first receipt, return error if there are no receipts
let allocation_id = match receipts.get(0) {
let allocation_id = match receipts.first() {
Some(receipt) => receipt.message.allocation_id,
None => return Err(tap_core::Error::NoValidReceiptsForRAVRequest.into()),
};
Expand Down
2 changes: 2 additions & 0 deletions tap_core/src/adapters/mock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright 2023-, Semiotic AI, Inc.
// 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_checks_adapter_mock;
pub mod receipt_storage_adapter_mock;
143 changes: 143 additions & 0 deletions tap_core/src/adapters/mock/auditor_executor_mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright 2023-, Semiotic AI, Inc.
// SPDX-License-Identifier: Apache-2.0

use super::{escrow_adapter_mock::AdpaterErrorMock, receipt_checks_adapter_mock::AdapterErrorMock};
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 alloy_primitives::Address;
use async_trait::async_trait;
use std::{
collections::{HashMap, HashSet},
sync::Arc,
};
use tokio::sync::RwLock;

#[derive(Clone)]
pub struct AuditorExecutorMock {
receipt_storage: Arc<RwLock<HashMap<u64, ReceivedReceipt>>>,

sender_escrow_storage: Arc<RwLock<HashMap<Address, u128>>>,

query_appraisals: Arc<RwLock<HashMap<u64, u128>>>,
allocation_ids: Arc<RwLock<HashSet<Address>>>,
sender_ids: Arc<RwLock<HashSet<Address>>>,
}

impl AuditorExecutorMock {
pub fn new(
receipt_storage: Arc<RwLock<HashMap<u64, ReceivedReceipt>>>,
sender_escrow_storage: Arc<RwLock<HashMap<Address, u128>>>,
query_appraisals: Arc<RwLock<HashMap<u64, u128>>>,
allocation_ids: Arc<RwLock<HashSet<Address>>>,
sender_ids: Arc<RwLock<HashSet<Address>>>,
) -> Self {
AuditorExecutorMock {
receipt_storage,
sender_escrow_storage,
allocation_ids,
sender_ids,
query_appraisals,
}
}
}

impl AuditorExecutorMock {
pub async fn escrow(&self, sender_id: Address) -> Result<u128, AdpaterErrorMock> {
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 AuditorExecutorMock {
type AdapterError = AdpaterErrorMock;
async fn get_available_escrow(&self, sender_id: Address) -> Result<u128, Self::AdapterError> {
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
}
}

#[async_trait]
impl ReceiptChecksAdapter for AuditorExecutorMock {
type AdapterError = AdapterErrorMock;

async fn is_unique(
&self,
receipt: &EIP712SignedMessage<Receipt>,
receipt_id: u64,
) -> Result<bool, Self::AdapterError> {
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<bool, Self::AdapterError> {
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<bool, Self::AdapterError> {
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<bool, Self::AdapterError> {
let sender_ids = self.sender_ids.read().await;
Ok(sender_ids.contains(&sender_id))
}
}
Loading

0 comments on commit bd6c82e

Please sign in to comment.