Skip to content

Commit 483a712

Browse files
authored
Cover more unit tests on rav/receipts (#228)
* test: Cover more unit tests on rav/receipts Signed-off-by: Carlos V <[email protected]> * test: Simplified tests Signed-off-by: Carlos V <[email protected]> * test: remove unnecesary value Signed-off-by: Carlos V <[email protected]> --------- Signed-off-by: Carlos V <[email protected]>
1 parent c179dfe commit 483a712

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

tap_core/src/receipt/checks.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,84 @@ impl CheckBatch for UniqueCheck {
182182
(checking, failed)
183183
}
184184
}
185+
186+
#[cfg(test)]
187+
mod tests {
188+
use std::str::FromStr;
189+
use std::time::Duration;
190+
use std::time::SystemTime;
191+
192+
use alloy_primitives::Address;
193+
use alloy_sol_types::eip712_domain;
194+
use alloy_sol_types::Eip712Domain;
195+
196+
use ethers::signers::coins_bip39::English;
197+
use ethers::signers::{LocalWallet, MnemonicBuilder};
198+
199+
use crate::receipt::Receipt;
200+
use crate::signed_message::EIP712SignedMessage;
201+
202+
use super::*;
203+
204+
fn create_signed_receipt_with_custom_value(value: u128) -> ReceiptWithState<Checking> {
205+
let index: u32 = 0;
206+
let wallet: LocalWallet = MnemonicBuilder::<English>::default()
207+
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
208+
.index(index)
209+
.unwrap()
210+
.build()
211+
.unwrap();
212+
let eip712_domain_separator: Eip712Domain = eip712_domain! {
213+
name: "TAP",
214+
version: "1",
215+
chain_id: 1,
216+
verifying_contract: Address:: from([0x11u8; 20]),
217+
};
218+
219+
let timestamp = SystemTime::now()
220+
.duration_since(SystemTime::UNIX_EPOCH)
221+
.expect("Time went backwards")
222+
.as_nanos()
223+
+ Duration::from_secs(33).as_nanos();
224+
let timestamp_ns = timestamp as u64;
225+
226+
let value: u128 = value;
227+
let nonce: u64 = 10;
228+
let receipt = EIP712SignedMessage::new(
229+
&eip712_domain_separator,
230+
Receipt {
231+
allocation_id: Address::from_str("0xabababababababababababababababababababab")
232+
.unwrap(),
233+
nonce,
234+
timestamp_ns,
235+
value,
236+
},
237+
&wallet,
238+
)
239+
.unwrap();
240+
ReceiptWithState::<Checking>::new(receipt)
241+
}
242+
243+
#[tokio::test]
244+
async fn test_receipt_uniqueness_check() {
245+
let signed_receipt = create_signed_receipt_with_custom_value(10);
246+
let signed_receipt_2 = create_signed_receipt_with_custom_value(15);
247+
let signed_receipt_copy = signed_receipt.clone();
248+
let receipts_batch = vec![signed_receipt, signed_receipt_2, signed_receipt_copy];
249+
let (valid_receipts, invalid_receipts) = UniqueCheck.check_batch(receipts_batch);
250+
assert_eq!(valid_receipts.len(), 2);
251+
assert_eq!(invalid_receipts.len(), 1);
252+
}
253+
254+
#[tokio::test]
255+
async fn test_receipt_timestamp_check() {
256+
let signed_receipt = create_signed_receipt_with_custom_value(10);
257+
let signed_receipt_2 = create_signed_receipt_with_custom_value(15);
258+
let receipts_batch = vec![signed_receipt.clone(), signed_receipt_2];
259+
let min_time_stamp = signed_receipt.signed_receipt.message.timestamp_ns + 1;
260+
let (valid_receipts, invalid_receipts) =
261+
TimestampCheck(min_time_stamp).check_batch(receipts_batch);
262+
assert_eq!(valid_receipts.len(), 1);
263+
assert_eq!(invalid_receipts.len(), 1);
264+
}
265+
}

tap_core/tests/manager_test.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use tap_core::{
2424
},
2525
Manager,
2626
},
27+
rav::ReceiptAggregateVoucher,
2728
receipt::{
2829
checks::{CheckList, StatefulTimestampCheck},
2930
Receipt,
@@ -204,6 +205,39 @@ async fn manager_create_rav_request_all_valid_receipts(
204205
.is_ok());
205206
}
206207

208+
#[rstest]
209+
#[tokio::test]
210+
async fn deny_rav_due_to_wrong_value(
211+
keys: (LocalWallet, Address),
212+
domain_separator: Eip712Domain,
213+
context: ContextFixture,
214+
) {
215+
let ContextFixture {
216+
context, checks, ..
217+
} = context;
218+
let manager = Manager::new(domain_separator.clone(), context, checks);
219+
220+
let rav = ReceiptAggregateVoucher {
221+
allocationId: Address::from_str("0xabababababababababababababababababababab").unwrap(),
222+
timestampNs: 1232442,
223+
valueAggregate: 20u128,
224+
};
225+
226+
let rav_wrong_value = ReceiptAggregateVoucher {
227+
allocationId: Address::from_str("0xabababababababababababababababababababab").unwrap(),
228+
timestampNs: 1232442,
229+
valueAggregate: 10u128,
230+
};
231+
232+
let signed_rav_with_wrong_aggregate =
233+
EIP712SignedMessage::new(&domain_separator, rav_wrong_value, &keys.0).unwrap();
234+
235+
assert!(manager
236+
.verify_and_store_rav(rav, signed_rav_with_wrong_aggregate)
237+
.await
238+
.is_err());
239+
}
240+
207241
#[rstest]
208242
#[tokio::test]
209243
async fn manager_create_multiple_rav_requests_all_valid_receipts(
@@ -453,3 +487,48 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim
453487
.await
454488
.is_ok());
455489
}
490+
491+
#[rstest]
492+
#[tokio::test]
493+
async fn manager_create_rav_and_ignore_invalid_receipts(
494+
keys: (LocalWallet, Address),
495+
allocation_ids: Vec<Address>,
496+
domain_separator: Eip712Domain,
497+
context: ContextFixture,
498+
) {
499+
let ContextFixture {
500+
context,
501+
checks,
502+
escrow_storage,
503+
..
504+
} = context;
505+
506+
let manager = Manager::new(domain_separator.clone(), context.clone(), checks);
507+
508+
escrow_storage.write().unwrap().insert(keys.1, 999999);
509+
510+
let mut stored_signed_receipts = Vec::new();
511+
//Forcing all receipts but one to be invalid by making all the same
512+
for _ in 0..10 {
513+
let receipt = Receipt {
514+
allocation_id: allocation_ids[0],
515+
timestamp_ns: 1,
516+
nonce: 1,
517+
value: 20u128,
518+
};
519+
let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &keys.0).unwrap();
520+
stored_signed_receipts.push(signed_receipt.clone());
521+
manager
522+
.verify_and_store_receipt(signed_receipt)
523+
.await
524+
.unwrap();
525+
}
526+
527+
let rav_request = manager.create_rav_request(0, None).await.unwrap();
528+
529+
assert_eq!(rav_request.valid_receipts.len(), 1);
530+
// All receipts but one being invalid
531+
assert_eq!(rav_request.invalid_receipts.len(), 9);
532+
//Rav Value corresponds only to value of one receipt
533+
assert_eq!(rav_request.expected_rav.valueAggregate, 20);
534+
}

0 commit comments

Comments
 (0)