Skip to content

Commit

Permalink
Merge pull request #116 from semiotic-ai/fix_aggregator_timestamp_check
Browse files Browse the repository at this point in the history
Fix aggregator timestamp check
  • Loading branch information
aasseman committed Jun 23, 2023
2 parents 053c46f + 0eca077 commit c57f6d7
Showing 1 changed file with 41 additions and 49 deletions.
90 changes: 41 additions & 49 deletions tap_aggregator/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn check_receipt_timestamps(
if let Some(previous_rav) = &previous_rav {
for receipt in receipts.iter() {
let receipt = &receipt.message;
if previous_rav.message.timestamp_ns > receipt.timestamp_ns {
if previous_rav.message.timestamp_ns >= receipt.timestamp_ns {
return Err(tap_core::Error::InvalidCheckError {
check_string: "Receipt timestamp is less or equal then previous rav timestamp"
.into(),
Expand All @@ -117,10 +117,7 @@ fn check_receipt_timestamps(

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

use ethers_core::types::Address;
use ethers_signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer};
Expand Down Expand Up @@ -194,71 +191,66 @@ mod tests {
#[rstest]
#[tokio::test]
/// Test that a receipt with a timestamp greater then the rav timestamp passes
async fn check_receipt_timestamps_ok(
keys: (LocalWallet, Address),
allocation_ids: Vec<Address>,
) {
let time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as u64;
async fn check_receipt_timestamps(keys: (LocalWallet, Address), allocation_ids: Vec<Address>) {
// Create receipts with consecutive timestamps
let receipt_timestamp_range = 10..20;
let mut receipts = Vec::new();
for i in receipt_timestamp_range.clone() {
receipts.push(
EIP712SignedMessage::new(
Receipt {
allocation_id: allocation_ids[0],
timestamp_ns: i,
nonce: 0,
value: 42,
},
&keys.0,
)
.await
.unwrap(),
);
}

// Create rav
// Create rav with max_timestamp below the receipts timestamps
let rav = EIP712SignedMessage::new(
tap_core::receipt_aggregate_voucher::ReceiptAggregateVoucher {
allocation_id: allocation_ids[0],
timestamp_ns: time,
timestamp_ns: receipt_timestamp_range.clone().min().unwrap() - 1,
value_aggregate: 42,
},
&keys.0,
)
.await
.unwrap();
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_ok());

let mut receipts = Vec::new();
receipts.push(
EIP712SignedMessage::new(Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0)
.await
.unwrap(),
);

aggregator::check_receipt_timestamps(&receipts, Some(&rav)).unwrap();
}

#[rstest]
#[tokio::test]
/// Test that a receipt with a timestamp less then the rav timestamp fails
async fn check_receipt_timestamps_fail(
keys: (LocalWallet, Address),
allocation_ids: Vec<Address>,
) {
let mut receipts = Vec::new();
receipts.push(
EIP712SignedMessage::new(Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0)
.await
.unwrap(),
);

let time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos() as u64;

// Create rav
// Create rav with max_timestamp equal to the lowest receipt timestamp
// Aggregation should fail
let rav = EIP712SignedMessage::new(
tap_core::receipt_aggregate_voucher::ReceiptAggregateVoucher {
allocation_id: allocation_ids[0],
timestamp_ns: time,
timestamp_ns: receipt_timestamp_range.clone().min().unwrap(),
value_aggregate: 42,
},
&keys.0,
)
.await
.unwrap();
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_err());

let res = aggregator::check_receipt_timestamps(&receipts, Some(&rav));

assert!(res.is_err());
// Create rav with max_timestamp above highest receipt timestamp
// Aggregation should fail
let rav = EIP712SignedMessage::new(
tap_core::receipt_aggregate_voucher::ReceiptAggregateVoucher {
allocation_id: allocation_ids[0],
timestamp_ns: receipt_timestamp_range.clone().max().unwrap() + 1,
value_aggregate: 42,
},
&keys.0,
)
.await
.unwrap();
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_err());
}

#[rstest]
Expand Down

1 comment on commit c57f6d7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

80.21%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
tap_aggregator/src
   aggregator.rs92.16%100%100%90.48%40–43, 53–56
   api_versioning.rs86.21%100%76.47%90.24%15–16
   error_codes.rs0%100%0%0%13, 27
   jsonrpsee_helpers.rs43.90%100%33.33%48.28%44, 51–57, 9
   lib.rs100%100%100%100%
   main.rs3.70%100%6.67%2.56%13, 17–18, 22, 26–27, 31–32, 36–37, 41–42, 45–47, 50–57, 60–65, 68–72, 74
   server.rs86.09%100%100%82.22%101, 113–117, 137, 69–77
tap_core/src
   eip_712_signed_message.rs83.78%100%80%85.19%58
   error.rs50%100%50%50%
   lib.rs83.33%100%87.50%81.25%26
   receipt_aggregate_voucher.rs92.50%100%87.50%93.75%23
tap_core/src/adapters/test
   collateral_adapter_mock.rs88.33%100%75%90.38%18, 44–45
   collateral_adapter_test.rs100%100%100%100%
   rav_storage_adapter_mock.rs92%100%71.43%95.35%30
   rav_storage_adapter_test.rs100%100%100%100%
   receipt_checks_adapter_mock.rs97.83%100%100%97.50%61
   receipt_checks_adapter_test.rs95.83%100%100%95%59
   receipt_storage_adapter_mock.rs71.78%100%57.14%74.14%111, 130, 134–136, 148, 163–177, 29, 43, 57, 75, 93
   receipt_storage_adapter_test.rs100%100%100%100%
tap_core/src/tap_manager
   manager.rs82.50%100%61.54%84.35%107, 126–129, 136, 185, 200–203, 209, 95
   rav_request.rs0%100%0%0%9
tap_core/src/tap_manager/test
   manager_test.rs100%100%100%100%
tap_core/src/tap_receipt
   mod.rs46.34%100%45.45%46.67%49–59
   receipt.rs88.46%100%87.50%88.89%19
   receipt_auditor.rs84.83%100%83.33%84.96%103–105, 115, 139, 161–163, 65, 78–80, 87–90
   received_receipt.rs83.71%100%78.05%84.59%119–121, 131, 215–217, 230–232, 238–240, 247–249, 251–253, 282–284, 319, 333–335, 340, 349–350, 359–360, 382, 396–398

Please sign in to comment.