Skip to content

Commit 66ec313

Browse files
authored
Merge pull request #214 from semiotic-ai/gusinacio/tap-agent-integration
Gusinacio/tap agent integration
2 parents b577d19 + 2c1bd89 commit 66ec313

File tree

17 files changed

+302
-682
lines changed

17 files changed

+302
-682
lines changed

tap_core/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ strum = "0.24.1"
2424
strum_macros = "0.24.3"
2525
async-trait = "0.1.72"
2626
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
27-
typetag = "0.2.14"
28-
futures = "0.3.17"
2927

3028
[dev-dependencies]
3129
criterion = { version = "0.5", features = ["async_std"] }

tap_core/src/adapters/escrow_adapter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ pub trait EscrowAdapter {
5353
sender_id: Address,
5454
value: u128,
5555
) -> Result<(), Self::AdapterError>;
56+
57+
async fn verify_signer(&self, signer_address: Address) -> Result<bool, Self::AdapterError>;
5658
}

tap_core/src/adapters/mock/executor_mock.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
use crate::adapters::escrow_adapter::EscrowAdapter;
55
use crate::adapters::receipt_storage_adapter::{
6-
safe_truncate_receipts, ReceiptRead, ReceiptStore, StoredReceipt,
6+
safe_truncate_receipts, ReceiptDelete, ReceiptRead, ReceiptStore, StoredReceipt,
77
};
88
use crate::checks::TimestampCheck;
9+
use crate::eip_712_signed_message::MessageId;
910
use crate::tap_receipt::ReceivedReceipt;
1011
use crate::{
1112
adapters::rav_storage_adapter::{RAVRead, RAVStore},
@@ -18,7 +19,7 @@ use std::sync::RwLock;
1819
use std::{collections::HashMap, sync::Arc};
1920

2021
pub type EscrowStorage = Arc<RwLock<HashMap<Address, u128>>>;
21-
pub type QueryAppraisals = Arc<RwLock<HashMap<u64, u128>>>;
22+
pub type QueryAppraisals = Arc<RwLock<HashMap<MessageId, u128>>>;
2223
pub type ReceiptStorage = Arc<RwLock<HashMap<u64, ReceivedReceipt>>>;
2324
pub type RAVStorage = Arc<RwLock<Option<SignedRAV>>>;
2425

@@ -36,10 +37,9 @@ pub struct ExecutorMock {
3637
rav_storage: RAVStorage,
3738
receipt_storage: ReceiptStorage,
3839
unique_id: Arc<RwLock<u64>>,
39-
4040
sender_escrow_storage: EscrowStorage,
41-
4241
timestamp_check: Arc<TimestampCheck>,
42+
sender_address: Option<Address>,
4343
}
4444

4545
impl ExecutorMock {
@@ -55,9 +55,15 @@ impl ExecutorMock {
5555
unique_id: Arc::new(RwLock::new(0)),
5656
sender_escrow_storage,
5757
timestamp_check,
58+
sender_address: None,
5859
}
5960
}
6061

62+
pub fn with_sender_address(mut self, sender_address: Address) -> Self {
63+
self.sender_address = Some(sender_address);
64+
self
65+
}
66+
6167
pub async fn retrieve_receipt_by_id(
6268
&self,
6369
receipt_id: u64,
@@ -139,6 +145,7 @@ impl RAVRead for ExecutorMock {
139145
#[async_trait]
140146
impl ReceiptStore for ExecutorMock {
141147
type AdapterError = AdapterErrorMock;
148+
142149
async fn store_receipt(&self, receipt: ReceivedReceipt) -> Result<u64, Self::AdapterError> {
143150
let mut id_pointer = self.unique_id.write().unwrap();
144151
let id_previous = *id_pointer;
@@ -147,23 +154,12 @@ impl ReceiptStore for ExecutorMock {
147154
*id_pointer += 1;
148155
Ok(id_previous)
149156
}
150-
async fn update_receipt_by_id(
151-
&self,
152-
receipt_id: u64,
153-
receipt: ReceivedReceipt,
154-
) -> Result<(), Self::AdapterError> {
155-
let mut receipt_storage = self.receipt_storage.write().unwrap();
157+
}
156158

157-
if !receipt_storage.contains_key(&receipt_id) {
158-
return Err(AdapterErrorMock::AdapterError {
159-
error: "Invalid receipt_id".to_owned(),
160-
});
161-
};
159+
#[async_trait]
160+
impl ReceiptDelete for ExecutorMock {
161+
type AdapterError = AdapterErrorMock;
162162

163-
receipt_storage.insert(receipt_id, receipt);
164-
*self.unique_id.write().unwrap() += 1;
165-
Ok(())
166-
}
167163
async fn remove_receipts_in_timestamp_range<R: RangeBounds<u64> + std::marker::Send>(
168164
&self,
169165
timestamp_ns: R,
@@ -175,7 +171,6 @@ impl ReceiptStore for ExecutorMock {
175171
Ok(())
176172
}
177173
}
178-
179174
#[async_trait]
180175
impl ReceiptRead for ExecutorMock {
181176
type AdapterError = AdapterErrorMock;
@@ -251,4 +246,11 @@ impl EscrowAdapter for ExecutorMock {
251246
) -> Result<(), Self::AdapterError> {
252247
self.reduce_escrow(sender_id, value)
253248
}
249+
250+
async fn verify_signer(&self, signer_address: Address) -> Result<bool, Self::AdapterError> {
251+
Ok(self
252+
.sender_address
253+
.map(|sender| signer_address == sender)
254+
.unwrap_or(false))
255+
}
254256
}

tap_core/src/adapters/receipt_storage_adapter.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,15 @@ pub trait ReceiptStore {
4747
/// It returns a unique receipt_id associated with the stored receipt. Any errors that occur during
4848
/// this process should be captured and returned as an `AdapterError`.
4949
async fn store_receipt(&self, receipt: ReceivedReceipt) -> Result<u64, Self::AdapterError>;
50+
}
5051

51-
/// Updates a specific `ReceivedReceipt` identified by a unique receipt_id.
52+
#[async_trait]
53+
pub trait ReceiptDelete {
54+
/// Defines the user-specified error type.
5255
///
53-
/// This method should be implemented to update a specific `ReceivedReceipt` identified by a unique
54-
/// receipt_id in your storage system. Any errors that occur during this process should be captured
55-
/// and returned as an `AdapterError`.
56-
async fn update_receipt_by_id(
57-
&self,
58-
receipt_id: u64,
59-
receipt: ReceivedReceipt,
60-
) -> Result<(), Self::AdapterError>;
61-
56+
/// This error type should implement the `Error` and `Debug` traits from the standard library.
57+
/// Errors of this type are returned to the user when an operation fails.
58+
type AdapterError: std::error::Error + std::fmt::Debug + Send + Sync + 'static;
6259
/// Removes all `ReceivedReceipts` within a specific timestamp range from the storage.
6360
///
6461
/// This method should be implemented to remove all `ReceivedReceipts` within a specific timestamp

tap_core/src/adapters/test/receipt_storage_adapter_test.rs

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
mod receipt_storage_adapter_unit_test {
66
use rand::seq::SliceRandom;
77
use rand::thread_rng;
8-
use std::collections::{HashMap, HashSet};
8+
use std::collections::HashMap;
99
use std::str::FromStr;
1010
use std::sync::{Arc, RwLock};
1111

1212
use crate::checks::TimestampCheck;
1313
use crate::{
1414
adapters::{executor_mock::ExecutorMock, receipt_storage_adapter::ReceiptStore},
15-
checks::{mock::get_full_list_of_checks, ReceiptCheck},
1615
eip_712_signed_message::EIP712SignedMessage,
1716
tap_eip712_domain,
1817
tap_receipt::{Receipt, ReceivedReceipt},
@@ -28,45 +27,24 @@ mod receipt_storage_adapter_unit_test {
2827
tap_eip712_domain(1, Address::from([0x11u8; 20]))
2928
}
3029

31-
struct ExecutorFixture {
32-
executor: ExecutorMock,
33-
checks: Vec<ReceiptCheck>,
34-
}
35-
3630
#[fixture]
37-
fn executor_mock(domain_separator: Eip712Domain) -> ExecutorFixture {
31+
fn executor() -> ExecutorMock {
3832
let escrow_storage = Arc::new(RwLock::new(HashMap::new()));
3933
let rav_storage = Arc::new(RwLock::new(None));
40-
let query_appraisals = Arc::new(RwLock::new(HashMap::new()));
4134
let receipt_storage = Arc::new(RwLock::new(HashMap::new()));
4235

4336
let timestamp_check = Arc::new(TimestampCheck::new(0));
44-
let executor = ExecutorMock::new(
37+
ExecutorMock::new(
4538
rav_storage,
4639
receipt_storage.clone(),
4740
escrow_storage.clone(),
4841
timestamp_check.clone(),
49-
);
50-
let mut checks = get_full_list_of_checks(
51-
domain_separator,
52-
HashSet::new(),
53-
Arc::new(RwLock::new(HashSet::new())),
54-
receipt_storage,
55-
query_appraisals.clone(),
56-
);
57-
checks.push(timestamp_check);
58-
59-
ExecutorFixture { executor, checks }
42+
)
6043
}
6144

6245
#[rstest]
6346
#[tokio::test]
64-
async fn receipt_adapter_test(domain_separator: Eip712Domain, executor_mock: ExecutorFixture) {
65-
let ExecutorFixture {
66-
mut executor,
67-
checks,
68-
} = executor_mock;
69-
47+
async fn receipt_adapter_test(domain_separator: Eip712Domain, mut executor: ExecutorMock) {
7048
let wallet: LocalWallet = MnemonicBuilder::<English>::default()
7149
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
7250
.build()
@@ -76,7 +54,6 @@ mod receipt_storage_adapter_unit_test {
7654
Address::from_str("0xabababababababababababababababababababab").unwrap();
7755

7856
// Create receipts
79-
let query_id = 10u64;
8057
let value = 100u128;
8158
let received_receipt = ReceivedReceipt::new(
8259
EIP712SignedMessage::new(
@@ -85,8 +62,6 @@ mod receipt_storage_adapter_unit_test {
8562
&wallet,
8663
)
8764
.unwrap(),
88-
query_id,
89-
&checks,
9065
);
9166

9267
let receipt_store_result = executor.store_receipt(received_receipt).await;
@@ -114,13 +89,8 @@ mod receipt_storage_adapter_unit_test {
11489
#[tokio::test]
11590
async fn multi_receipt_adapter_test(
11691
domain_separator: Eip712Domain,
117-
executor_mock: ExecutorFixture,
92+
mut executor: ExecutorMock,
11893
) {
119-
let ExecutorFixture {
120-
mut executor,
121-
checks,
122-
} = executor_mock;
123-
12494
let wallet: LocalWallet = MnemonicBuilder::<English>::default()
12595
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
12696
.build()
@@ -131,28 +101,21 @@ mod receipt_storage_adapter_unit_test {
131101

132102
// Create receipts
133103
let mut received_receipts = Vec::new();
134-
for (query_id, value) in (50..60).enumerate() {
104+
for value in 50..60 {
135105
received_receipts.push(ReceivedReceipt::new(
136106
EIP712SignedMessage::new(
137107
&domain_separator,
138108
Receipt::new(allocation_id, value).unwrap(),
139109
&wallet,
140110
)
141111
.unwrap(),
142-
query_id as u64,
143-
&checks,
144112
));
145113
}
146114
let mut receipt_ids = Vec::new();
147115
let mut receipt_timestamps = Vec::new();
148116
for received_receipt in received_receipts {
149-
receipt_ids.push(
150-
executor
151-
.store_receipt(received_receipt.clone())
152-
.await
153-
.unwrap(),
154-
);
155-
receipt_timestamps.push(received_receipt.signed_receipt().message.timestamp_ns)
117+
receipt_timestamps.push(received_receipt.signed_receipt().message.timestamp_ns);
118+
receipt_ids.push(executor.store_receipt(received_receipt).await.unwrap());
156119
}
157120

158121
// Retreive receipts with timestamp
@@ -205,7 +168,6 @@ mod receipt_storage_adapter_unit_test {
205168
#[test]
206169
fn safe_truncate_receipts_test(
207170
domain_separator: Eip712Domain,
208-
executor_mock: ExecutorFixture,
209171
#[case] input: Vec<u64>,
210172
#[case] limit: u64,
211173
#[case] expected: Vec<u64>,
@@ -214,7 +176,6 @@ mod receipt_storage_adapter_unit_test {
214176
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
215177
.build()
216178
.unwrap();
217-
let checks = executor_mock.checks;
218179

219180
// Vec of (id, receipt)
220181
let mut receipts_orig: Vec<(u64, ReceivedReceipt)> = Vec::new();
@@ -235,13 +196,11 @@ mod receipt_storage_adapter_unit_test {
235196
&wallet,
236197
)
237198
.unwrap(),
238-
i as u64, // Will use that to check the IDs
239-
&checks,
240199
),
241200
));
242201
}
243202

244-
let mut receipts_truncated = receipts_orig.clone();
203+
let mut receipts_truncated = receipts_orig;
245204

246205
// shuffle the input receipts
247206
receipts_truncated.shuffle(&mut thread_rng());
@@ -259,9 +218,6 @@ mod receipt_storage_adapter_unit_test {
259218
elem_trun.1.signed_receipt().message.timestamp_ns,
260219
*expected_timestamp
261220
);
262-
263-
// Check that the IDs are fine
264-
assert_eq!(elem_trun.0, elem_trun.1.query_id());
265221
}
266222
}
267223
}

0 commit comments

Comments
 (0)