Skip to content

Commit

Permalink
feat: expected_rav to become result<rav,error>
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvdr committed Aug 2, 2024
1 parent cf90347 commit 6735e1a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 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>,
}
41 changes: 26 additions & 15 deletions tap_core/tests/manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,13 @@ 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(), &keys.0)
EIP712SignedMessage::new(&domain_separator, expected_rav.clone(), &keys.0)
.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 @@ -288,19 +290,21 @@ 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_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(), &keys.0)
EIP712SignedMessage::new(&domain_separator, expected_rav.clone(), &keys.0)
.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 @@ -334,19 +338,21 @@ 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_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(), &keys.0)
EIP712SignedMessage::new(&domain_separator, expected_rav.clone(), &keys.0)
.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 @@ -408,22 +414,24 @@ 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_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(),
expected_rav_1.clone(),
&keys.0,
)
.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 @@ -468,22 +476,24 @@ 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_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(),
expected_rav_2.clone(),
&keys.0,
)
.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 @@ -525,10 +535,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);
}

0 comments on commit 6735e1a

Please sign in to comment.