Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover more unit tests on rav/receipts #228

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions tap_core/src/receipt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,84 @@ impl CheckBatch for UniqueCheck {
(checking, failed)
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
use std::time::Duration;
use std::time::SystemTime;

use alloy_primitives::Address;
use alloy_sol_types::eip712_domain;
use alloy_sol_types::Eip712Domain;

use ethers::signers::coins_bip39::English;
use ethers::signers::{LocalWallet, MnemonicBuilder};

use crate::receipt::Receipt;
use crate::signed_message::EIP712SignedMessage;

use super::*;

fn create_signed_receipt_with_custom_value(value: u128) -> ReceiptWithState<Checking> {
let index: u32 = 0;
let wallet: LocalWallet = MnemonicBuilder::<English>::default()
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
.index(index)
.unwrap()
.build()
.unwrap();
let eip712_domain_separator: Eip712Domain = eip712_domain! {
name: "TAP",
version: "1",
chain_id: 1,
verifying_contract: Address:: from([0x11u8; 20]),
};

let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Time went backwards")
.as_nanos()
+ Duration::from_secs(33).as_nanos();
let timestamp_ns = timestamp as u64;

let value: u128 = value;
let nonce: u64 = 10;
let receipt = EIP712SignedMessage::new(
&eip712_domain_separator,
Receipt {
allocation_id: Address::from_str("0xabababababababababababababababababababab")
.unwrap(),
nonce,
timestamp_ns,
value,
},
&wallet,
)
.unwrap();
ReceiptWithState::<Checking>::new(receipt)
}

#[tokio::test]
async fn test_receipt_uniqueness_check() {
aasseman marked this conversation as resolved.
Show resolved Hide resolved
let signed_receipt = create_signed_receipt_with_custom_value(10);
let signed_receipt_2 = create_signed_receipt_with_custom_value(15);
let signed_receipt_copy = signed_receipt.clone();
let receipts_batch = vec![signed_receipt, signed_receipt_2, signed_receipt_copy];
let (valid_receipts, invalid_receipts) = UniqueCheck.check_batch(receipts_batch);
assert_eq!(valid_receipts.len(), 2);
assert_eq!(invalid_receipts.len(), 1);
}

#[tokio::test]
async fn test_receipt_timestamp_check() {
aasseman marked this conversation as resolved.
Show resolved Hide resolved
let signed_receipt = create_signed_receipt_with_custom_value(10);
let signed_receipt_2 = create_signed_receipt_with_custom_value(15);
let receipts_batch = vec![signed_receipt.clone(), signed_receipt_2];
let min_time_stamp = signed_receipt.signed_receipt.message.timestamp_ns + 1;
let (valid_receipts, invalid_receipts) =
TimestampCheck(min_time_stamp).check_batch(receipts_batch);
assert_eq!(valid_receipts.len(), 1);
assert_eq!(invalid_receipts.len(), 1);
}
}
106 changes: 106 additions & 0 deletions tap_core/tests/manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use tap_core::{
},
Manager,
},
rav::ReceiptAggregateVoucher,
receipt::{
checks::{CheckList, StatefulTimestampCheck},
Receipt,
Expand Down Expand Up @@ -204,6 +205,68 @@ async fn manager_create_rav_request_all_valid_receipts(
.is_ok());
}

#[rstest]
#[tokio::test]
async fn deny_rav_due_to_wrong_value(
keys: (LocalWallet, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
context: ContextFixture,
) {
let ContextFixture {
context,
checks,
query_appraisals,
escrow_storage,
..
} = context;
let manager = Manager::new(domain_separator.clone(), context, checks);
escrow_storage.write().unwrap().insert(keys.1, 999999);

let mut stored_signed_receipts = Vec::new();
for _ in 0..10 {
let value = 20u128;
let signed_receipt = EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], value).unwrap(),
&keys.0,
)
.unwrap();
let query_id = signed_receipt.unique_hash();
stored_signed_receipts.push(signed_receipt.clone());
query_appraisals.write().unwrap().insert(query_id, value);
let _ = manager.verify_and_store_receipt(signed_receipt).await;
aasseman marked this conversation as resolved.
Show resolved Hide resolved
}
let rav_request_result = manager.create_rav_request(0, None).await;

let rav_request = rav_request_result.unwrap();
aasseman marked this conversation as resolved.
Show resolved Hide resolved

// Create more receipts to create a manual rav with different value
let mut receipts = Vec::new();
for value in 50..60 {
receipts.push(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], value).unwrap(),
&keys.0,
)
.unwrap(),
);
}

let signed_rav_with_wrong_aggregate = EIP712SignedMessage::new(
&domain_separator,
ReceiptAggregateVoucher::aggregate_receipts(allocation_ids[0], &receipts, None).unwrap(),
&keys.0,
)
.unwrap();

assert!(manager
.verify_and_store_rav(rav_request.expected_rav, signed_rav_with_wrong_aggregate)
.await
.is_err());
aasseman marked this conversation as resolved.
Show resolved Hide resolved
}

#[rstest]
#[tokio::test]
async fn manager_create_multiple_rav_requests_all_valid_receipts(
Expand Down Expand Up @@ -453,3 +516,46 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim
.await
.is_ok());
}

#[rstest]
#[tokio::test]
async fn manager_create_rav_and_ignore_invalid_receipts(
keys: (LocalWallet, Address),
allocation_ids: Vec<Address>,
domain_separator: Eip712Domain,
context: ContextFixture,
) {
let ContextFixture {
context,
checks,
escrow_storage,
..
} = context;

let manager = Manager::new(domain_separator.clone(), context.clone(), checks);

escrow_storage.write().unwrap().insert(keys.1, 999999);

let mut stored_signed_receipts = Vec::new();
//Forcing all receipts but one to be invalid by making all the same
for _ in 0..10 {
let value = 20u128;
let mut receipt = Receipt::new(allocation_ids[0], value).unwrap();
receipt.nonce = 1;
receipt.timestamp_ns = 1;
aasseman marked this conversation as resolved.
Show resolved Hide resolved
let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &keys.0).unwrap();
stored_signed_receipts.push(signed_receipt.clone());
let _ = manager.verify_and_store_receipt(signed_receipt).await;
aasseman marked this conversation as resolved.
Show resolved Hide resolved
}

let rav_request_result = manager.create_rav_request(0, None).await;
assert!(rav_request_result.is_ok());

let rav_request = rav_request_result.unwrap();
aasseman marked this conversation as resolved.
Show resolved Hide resolved

assert!(rav_request.valid_receipts.len() < stored_signed_receipts.len());
aasseman marked this conversation as resolved.
Show resolved Hide resolved
// All receipts but one being invalid
assert_eq!(rav_request.invalid_receipts.len(), 9);
//Rav Value corresponds only to value of one receipt
assert_eq!(rav_request.expected_rav.valueAggregate, 20);
}
Loading