-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtimestamp_check.rs
142 lines (129 loc) · 4.72 KB
/
timestamp_check.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0
use std::time::{Duration, SystemTime};
use anyhow::anyhow;
pub struct TimestampCheck {
timestamp_error_tolerance: Duration,
}
use tap_core::receipt::{
checks::{Check, CheckError, CheckResult},
state::Checking,
ReceiptWithState,
};
impl TimestampCheck {
pub fn new(timestamp_error_tolerance: Duration) -> Self {
Self {
timestamp_error_tolerance,
}
}
}
#[async_trait::async_trait]
impl Check for TimestampCheck {
async fn check(
&self,
_: &tap_core::receipt::Context,
receipt: &ReceiptWithState<Checking>,
) -> CheckResult {
let timestamp_now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.map_err(|e| CheckError::Failed(e.into()))?;
let min_timestamp = timestamp_now - self.timestamp_error_tolerance;
let max_timestamp = timestamp_now + self.timestamp_error_tolerance;
let receipt_timestamp = Duration::from_nanos(receipt.signed_receipt().message.timestamp_ns);
if receipt_timestamp < max_timestamp && receipt_timestamp > min_timestamp {
Ok(())
} else {
Err(CheckError::Failed(anyhow!(
"Receipt timestamp `{}` is outside of current system time +/- timestamp_error_tolerance",
receipt_timestamp.as_secs()
)))
}
}
}
#[cfg(test)]
mod tests {
use std::time::{Duration, SystemTime};
use tap_core::{
receipt::{checks::Check, state::Checking, Context, Receipt, ReceiptWithState},
signed_message::EIP712SignedMessage,
tap_eip712_domain,
};
use thegraph_core::alloy::{
primitives::{address, Address},
signers::local::{coins_bip39::English, MnemonicBuilder, PrivateKeySigner},
};
use super::TimestampCheck;
use crate::tap::Eip712Domain;
fn create_signed_receipt_with_custom_timestamp(
timestamp_ns: u64,
) -> ReceiptWithState<Checking> {
let index: u32 = 0;
let wallet: PrivateKeySigner = 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 =
tap_eip712_domain(1, Address::from([0x11u8; 20]));
let value: u128 = 1234;
let nonce: u64 = 10;
let receipt = EIP712SignedMessage::new(
&eip712_domain_separator,
Receipt {
allocation_id: address!("abababababababababababababababababababab"),
nonce,
timestamp_ns,
value,
},
&wallet,
)
.unwrap();
ReceiptWithState::<Checking>::new(receipt)
}
#[tokio::test]
async fn test_timestamp_inside_tolerance() {
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Time went backwards")
.as_nanos()
+ Duration::from_secs(15).as_nanos();
let timestamp_ns = timestamp as u64;
let signed_receipt = create_signed_receipt_with_custom_timestamp(timestamp_ns);
let timestamp_check = TimestampCheck::new(Duration::from_secs(30));
assert!(timestamp_check
.check(&Context::new(), &signed_receipt)
.await
.is_ok());
}
#[tokio::test]
async fn test_timestamp_less_than_tolerance() {
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 signed_receipt = create_signed_receipt_with_custom_timestamp(timestamp_ns);
let timestamp_check = TimestampCheck::new(Duration::from_secs(30));
assert!(timestamp_check
.check(&Context::new(), &signed_receipt)
.await
.is_err());
}
#[tokio::test]
async fn test_timestamp_more_than_tolerance() {
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 signed_receipt = create_signed_receipt_with_custom_timestamp(timestamp_ns);
let timestamp_check = TimestampCheck::new(Duration::from_secs(30));
assert!(timestamp_check
.check(&Context::new(), &signed_receipt)
.await
.is_err());
}
}