Skip to content

Commit

Permalink
refactor!: remove stateful checks
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <gustavo@semiotic.ai>
  • Loading branch information
gusinacio committed Mar 8, 2024
1 parent 3045f61 commit 1044f53
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 427 deletions.
1 change: 0 additions & 1 deletion tap_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ 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"
futures = "0.3.17"

[dev-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion tap_core/src/adapters/mock/executor_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::adapters::receipt_storage_adapter::{
safe_truncate_receipts, ReceiptDelete, ReceiptRead, ReceiptStore, StoredReceipt,
};
use crate::checks::TimestampCheck;
use crate::eip_712_signed_message::MessageId;
use crate::tap_receipt::ReceivedReceipt;
use crate::{
adapters::rav_storage_adapter::{RAVRead, RAVStore},
Expand All @@ -18,7 +19,7 @@ use std::sync::RwLock;
use std::{collections::HashMap, sync::Arc};

pub type EscrowStorage = Arc<RwLock<HashMap<Address, u128>>>;
pub type QueryAppraisals = Arc<RwLock<HashMap<u64, u128>>>;
pub type QueryAppraisals = Arc<RwLock<HashMap<MessageId, u128>>>;
pub type ReceiptStorage = Arc<RwLock<HashMap<u64, ReceivedReceipt>>>;
pub type RAVStorage = Arc<RwLock<Option<SignedRAV>>>;

Expand Down
64 changes: 10 additions & 54 deletions tap_core/src/adapters/test/receipt_storage_adapter_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
mod receipt_storage_adapter_unit_test {
use rand::seq::SliceRandom;
use rand::thread_rng;
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::str::FromStr;
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},
eip_712_signed_message::EIP712SignedMessage,
tap_eip712_domain,
tap_receipt::{Receipt, ReceivedReceipt},
Expand All @@ -28,45 +27,24 @@ mod receipt_storage_adapter_unit_test {
tap_eip712_domain(1, Address::from([0x11u8; 20]))
}

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

#[fixture]
fn executor_mock(domain_separator: Eip712Domain) -> ExecutorFixture {
fn executor() -> ExecutorMock {
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(
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())),
receipt_storage,
query_appraisals.clone(),
);
checks.push(timestamp_check);

ExecutorFixture { executor, checks }
)
}

#[rstest]
#[tokio::test]
async fn receipt_adapter_test(domain_separator: Eip712Domain, executor_mock: ExecutorFixture) {
let ExecutorFixture {
mut executor,
checks,
} = executor_mock;

async fn receipt_adapter_test(domain_separator: Eip712Domain, mut executor: ExecutorMock) {
let wallet: LocalWallet = MnemonicBuilder::<English>::default()
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
.build()
Expand All @@ -76,7 +54,6 @@ mod receipt_storage_adapter_unit_test {
Address::from_str("0xabababababababababababababababababababab").unwrap();

// Create receipts
let query_id = 10u64;
let value = 100u128;
let received_receipt = ReceivedReceipt::new(
EIP712SignedMessage::new(
Expand All @@ -85,8 +62,6 @@ mod receipt_storage_adapter_unit_test {
&wallet,
)
.unwrap(),
query_id,
&checks,
);

let receipt_store_result = executor.store_receipt(received_receipt).await;
Expand Down Expand Up @@ -114,13 +89,8 @@ mod receipt_storage_adapter_unit_test {
#[tokio::test]
async fn multi_receipt_adapter_test(
domain_separator: Eip712Domain,
executor_mock: ExecutorFixture,
mut executor: ExecutorMock,
) {
let ExecutorFixture {
mut executor,
checks,
} = executor_mock;

let wallet: LocalWallet = MnemonicBuilder::<English>::default()
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
.build()
Expand All @@ -131,28 +101,21 @@ mod receipt_storage_adapter_unit_test {

// Create receipts
let mut received_receipts = Vec::new();
for (query_id, value) in (50..60).enumerate() {
for value in 50..60 {
received_receipts.push(ReceivedReceipt::new(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, value).unwrap(),
&wallet,
)
.unwrap(),
query_id as u64,
&checks,
));
}
let mut receipt_ids = Vec::new();
let mut receipt_timestamps = Vec::new();
for received_receipt in received_receipts {
receipt_ids.push(
executor
.store_receipt(received_receipt.clone())
.await
.unwrap(),
);
receipt_timestamps.push(received_receipt.signed_receipt().message.timestamp_ns)
receipt_timestamps.push(received_receipt.signed_receipt().message.timestamp_ns);
receipt_ids.push(executor.store_receipt(received_receipt).await.unwrap());
}

// Retreive receipts with timestamp
Expand Down Expand Up @@ -205,7 +168,6 @@ mod receipt_storage_adapter_unit_test {
#[test]
fn safe_truncate_receipts_test(
domain_separator: Eip712Domain,
executor_mock: ExecutorFixture,
#[case] input: Vec<u64>,
#[case] limit: u64,
#[case] expected: Vec<u64>,
Expand All @@ -214,7 +176,6 @@ 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();
Expand All @@ -235,13 +196,11 @@ mod receipt_storage_adapter_unit_test {
&wallet,
)
.unwrap(),
i as u64, // Will use that to check the IDs
&checks,
),
));
}

let mut receipts_truncated = receipts_orig.clone();
let mut receipts_truncated = receipts_orig;

// shuffle the input receipts
receipts_truncated.shuffle(&mut thread_rng());
Expand All @@ -259,9 +218,6 @@ mod receipt_storage_adapter_unit_test {
elem_trun.1.signed_receipt().message.timestamp_ns,
*expected_timestamp
);

// Check that the IDs are fine
assert_eq!(elem_trun.0, elem_trun.1.query_id());
}
}
}
Loading

0 comments on commit 1044f53

Please sign in to comment.