Skip to content

Commit

Permalink
Merge pull request #232 from semiotic-ai/invalid-receipts
Browse files Browse the repository at this point in the history
obtain invalid receipts
  • Loading branch information
carlosvdr authored Aug 9, 2024
2 parents b5f2135 + b9445f7 commit eb8447e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 43 deletions.
2 changes: 1 addition & 1 deletion tap_core/src/manager/tap_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ where
.collect_receipts(timestamp_buffer_ns, min_timestamp_ns, receipts_limit)
.await?;

let expected_rav = Self::generate_expected_rav(&valid_receipts, previous_rav.clone())?;
let expected_rav = Self::generate_expected_rav(&valid_receipts, previous_rav.clone());

Ok(RAVRequest {
valid_receipts,
Expand Down
3 changes: 2 additions & 1 deletion tap_core/src/rav/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::receipt::state::Reserved;
use crate::Error;
use crate::{
rav::{ReceiptAggregateVoucher, SignedRAV},
receipt::{state::Failed, ReceiptWithState},
Expand All @@ -17,5 +18,5 @@ pub struct RAVRequest {
/// List of failed receipt used to log invalid receipts
pub invalid_receipts: Vec<ReceiptWithState<Failed>>,
/// Expected RAV to be created
pub expected_rav: ReceiptAggregateVoucher,
pub expected_rav: Result<ReceiptAggregateVoucher, Error>,
}
68 changes: 28 additions & 40 deletions tap_core/tests/manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,12 @@ async fn manager_create_rav_request_all_valid_receipts(
// no failing
assert_eq!(rav_request.invalid_receipts.len(), 0);

let expected_rav = rav_request.expected_rav.unwrap();

let signed_rav =
EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &signer)
.unwrap();
EIP712SignedMessage::new(&domain_separator, expected_rav.clone(), &signer).unwrap();
assert!(manager
.verify_and_store_rav(rav_request.expected_rav, signed_rav)
.verify_and_store_rav(expected_rav, signed_rav)
.await
.is_ok());
}
Expand Down Expand Up @@ -292,19 +293,17 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts(
);
// no receipts failing
assert_eq!(rav_request.invalid_receipts.len(), 0);

let expected_rav = rav_request.expected_rav.unwrap();
// accumulated value is correct
assert_eq!(
rav_request.expected_rav.valueAggregate,
expected_accumulated_value
);
assert_eq!(expected_rav.valueAggregate, expected_accumulated_value);
// no previous rav
assert!(rav_request.previous_rav.is_none());

let signed_rav =
EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &signer)
.unwrap();
EIP712SignedMessage::new(&domain_separator, expected_rav.clone(), &signer).unwrap();
assert!(manager
.verify_and_store_rav(rav_request.expected_rav, signed_rav)
.verify_and_store_rav(expected_rav, signed_rav)
.await
.is_ok());

Expand Down Expand Up @@ -338,19 +337,17 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts(
);
// no receipts failing
assert_eq!(rav_request.invalid_receipts.len(), 0);

let expected_rav = rav_request.expected_rav.unwrap();
// accumulated value is correct
assert_eq!(
rav_request.expected_rav.valueAggregate,
expected_accumulated_value
);
assert_eq!(expected_rav.valueAggregate, expected_accumulated_value);
// Verify there is a previous rav
assert!(rav_request.previous_rav.is_some());

let signed_rav =
EIP712SignedMessage::new(&domain_separator, rav_request.expected_rav.clone(), &signer)
.unwrap();
EIP712SignedMessage::new(&domain_separator, expected_rav.clone(), &signer).unwrap();
assert!(manager
.verify_and_store_rav(rav_request.expected_rav, signed_rav)
.verify_and_store_rav(expected_rav, signed_rav)
.await
.is_ok());
}
Expand Down Expand Up @@ -415,22 +412,17 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim
);
// no receipts failing
assert_eq!(rav_request_1.invalid_receipts.len(), 0);

let expected_rav_1 = rav_request_1.expected_rav.unwrap();
// accumulated value is correct
assert_eq!(
rav_request_1.expected_rav.valueAggregate,
expected_accumulated_value
);
assert_eq!(expected_rav_1.valueAggregate, expected_accumulated_value);
// no previous rav
assert!(rav_request_1.previous_rav.is_none());

let signed_rav_1 = EIP712SignedMessage::new(
&domain_separator,
rav_request_1.expected_rav.clone(),
&signer,
)
.unwrap();
let signed_rav_1 =
EIP712SignedMessage::new(&domain_separator, expected_rav_1.clone(), &signer).unwrap();
assert!(manager
.verify_and_store_rav(rav_request_1.expected_rav, signed_rav_1)
.verify_and_store_rav(expected_rav_1, signed_rav_1)
.await
.is_ok());

Expand Down Expand Up @@ -475,22 +467,17 @@ async fn manager_create_multiple_rav_requests_all_valid_receipts_consecutive_tim
);
// no receipts failing
assert_eq!(rav_request_2.invalid_receipts.len(), 0);

let expected_rav_2 = rav_request_2.expected_rav.unwrap();
// accumulated value is correct
assert_eq!(
rav_request_2.expected_rav.valueAggregate,
expected_accumulated_value
);
assert_eq!(expected_rav_2.valueAggregate, expected_accumulated_value);
// Verify there is a previous rav
assert!(rav_request_2.previous_rav.is_some());

let signed_rav_2 = EIP712SignedMessage::new(
&domain_separator,
rav_request_2.expected_rav.clone(),
&signer,
)
.unwrap();
let signed_rav_2 =
EIP712SignedMessage::new(&domain_separator, expected_rav_2.clone(), &signer).unwrap();
assert!(manager
.verify_and_store_rav(rav_request_2.expected_rav, signed_rav_2)
.verify_and_store_rav(expected_rav_2, signed_rav_2)
.await
.is_ok());
}
Expand Down Expand Up @@ -535,10 +522,11 @@ async fn manager_create_rav_and_ignore_invalid_receipts(
}

let rav_request = manager.create_rav_request(0, None).await.unwrap();
let expected_rav = rav_request.expected_rav.unwrap();

assert_eq!(rav_request.valid_receipts.len(), 1);
// 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);
assert_eq!(expected_rav.valueAggregate, 20);
}
2 changes: 1 addition & 1 deletion tap_integration_tests/tests/indexer_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ where
.request("aggregate_receipts", params)
.await?;
manager
.verify_and_store_rav(rav_request.expected_rav, remote_rav_result.data)
.verify_and_store_rav(rav_request.expected_rav?, remote_rav_result.data)
.await?;

// For these tests, we expect every receipt to be valid, i.e. there should be no invalid receipts, nor any missing receipts (less than the expected threshold).
Expand Down

0 comments on commit eb8447e

Please sign in to comment.