Skip to content

Commit c57f6d7

Browse files
authored
Merge pull request #116 from semiotic-ai/fix_aggregator_timestamp_check
Fix aggregator timestamp check
2 parents 053c46f + 0eca077 commit c57f6d7

File tree

1 file changed

+41
-49
lines changed

1 file changed

+41
-49
lines changed

tap_aggregator/src/aggregator.rs

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn check_receipt_timestamps(
102102
if let Some(previous_rav) = &previous_rav {
103103
for receipt in receipts.iter() {
104104
let receipt = &receipt.message;
105-
if previous_rav.message.timestamp_ns > receipt.timestamp_ns {
105+
if previous_rav.message.timestamp_ns >= receipt.timestamp_ns {
106106
return Err(tap_core::Error::InvalidCheckError {
107107
check_string: "Receipt timestamp is less or equal then previous rav timestamp"
108108
.into(),
@@ -117,10 +117,7 @@ fn check_receipt_timestamps(
117117

118118
#[cfg(test)]
119119
mod tests {
120-
use std::{
121-
str::FromStr,
122-
time::{SystemTime, UNIX_EPOCH},
123-
};
120+
use std::str::FromStr;
124121

125122
use ethers_core::types::Address;
126123
use ethers_signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer};
@@ -194,71 +191,66 @@ mod tests {
194191
#[rstest]
195192
#[tokio::test]
196193
/// Test that a receipt with a timestamp greater then the rav timestamp passes
197-
async fn check_receipt_timestamps_ok(
198-
keys: (LocalWallet, Address),
199-
allocation_ids: Vec<Address>,
200-
) {
201-
let time = SystemTime::now()
202-
.duration_since(UNIX_EPOCH)
203-
.unwrap()
204-
.as_millis() as u64;
194+
async fn check_receipt_timestamps(keys: (LocalWallet, Address), allocation_ids: Vec<Address>) {
195+
// Create receipts with consecutive timestamps
196+
let receipt_timestamp_range = 10..20;
197+
let mut receipts = Vec::new();
198+
for i in receipt_timestamp_range.clone() {
199+
receipts.push(
200+
EIP712SignedMessage::new(
201+
Receipt {
202+
allocation_id: allocation_ids[0],
203+
timestamp_ns: i,
204+
nonce: 0,
205+
value: 42,
206+
},
207+
&keys.0,
208+
)
209+
.await
210+
.unwrap(),
211+
);
212+
}
205213

206-
// Create rav
214+
// Create rav with max_timestamp below the receipts timestamps
207215
let rav = EIP712SignedMessage::new(
208216
tap_core::receipt_aggregate_voucher::ReceiptAggregateVoucher {
209217
allocation_id: allocation_ids[0],
210-
timestamp_ns: time,
218+
timestamp_ns: receipt_timestamp_range.clone().min().unwrap() - 1,
211219
value_aggregate: 42,
212220
},
213221
&keys.0,
214222
)
215223
.await
216224
.unwrap();
225+
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_ok());
217226

218-
let mut receipts = Vec::new();
219-
receipts.push(
220-
EIP712SignedMessage::new(Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0)
221-
.await
222-
.unwrap(),
223-
);
224-
225-
aggregator::check_receipt_timestamps(&receipts, Some(&rav)).unwrap();
226-
}
227-
228-
#[rstest]
229-
#[tokio::test]
230-
/// Test that a receipt with a timestamp less then the rav timestamp fails
231-
async fn check_receipt_timestamps_fail(
232-
keys: (LocalWallet, Address),
233-
allocation_ids: Vec<Address>,
234-
) {
235-
let mut receipts = Vec::new();
236-
receipts.push(
237-
EIP712SignedMessage::new(Receipt::new(allocation_ids[0], 42).unwrap(), &keys.0)
238-
.await
239-
.unwrap(),
240-
);
241-
242-
let time = SystemTime::now()
243-
.duration_since(UNIX_EPOCH)
244-
.unwrap()
245-
.as_nanos() as u64;
246-
247-
// Create rav
227+
// Create rav with max_timestamp equal to the lowest receipt timestamp
228+
// Aggregation should fail
248229
let rav = EIP712SignedMessage::new(
249230
tap_core::receipt_aggregate_voucher::ReceiptAggregateVoucher {
250231
allocation_id: allocation_ids[0],
251-
timestamp_ns: time,
232+
timestamp_ns: receipt_timestamp_range.clone().min().unwrap(),
252233
value_aggregate: 42,
253234
},
254235
&keys.0,
255236
)
256237
.await
257238
.unwrap();
239+
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_err());
258240

259-
let res = aggregator::check_receipt_timestamps(&receipts, Some(&rav));
260-
261-
assert!(res.is_err());
241+
// Create rav with max_timestamp above highest receipt timestamp
242+
// Aggregation should fail
243+
let rav = EIP712SignedMessage::new(
244+
tap_core::receipt_aggregate_voucher::ReceiptAggregateVoucher {
245+
allocation_id: allocation_ids[0],
246+
timestamp_ns: receipt_timestamp_range.clone().max().unwrap() + 1,
247+
value_aggregate: 42,
248+
},
249+
&keys.0,
250+
)
251+
.await
252+
.unwrap();
253+
assert!(aggregator::check_receipt_timestamps(&receipts, Some(&rav)).is_err());
262254
}
263255

264256
#[rstest]

0 commit comments

Comments
 (0)