-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathreceipt_max_val_check.rs
124 lines (109 loc) · 3.74 KB
/
receipt_max_val_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
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0
use anyhow::anyhow;
pub struct ReceiptMaxValueCheck {
receipt_max_value: u128,
}
use tap_core::receipt::{
checks::{Check, CheckError, CheckResult},
WithValueAndTimestamp,
};
use crate::tap::{CheckingReceipt, TapReceipt};
impl ReceiptMaxValueCheck {
pub fn new(receipt_max_value: u128) -> Self {
Self { receipt_max_value }
}
}
#[async_trait::async_trait]
impl Check<TapReceipt> for ReceiptMaxValueCheck {
async fn check(
&self,
_: &tap_core::receipt::Context,
receipt: &CheckingReceipt,
) -> CheckResult {
let receipt_value = receipt.signed_receipt().value();
if receipt_value < self.receipt_max_value {
Ok(())
} else {
Err(CheckError::Failed(anyhow!(
"Receipt value `{}` is higher than the limit set by the user",
receipt_value
)))
}
}
}
#[cfg(test)]
mod tests {
use std::time::{Duration, SystemTime};
use tap_core::{
receipt::{checks::Check, Context},
signed_message::Eip712SignedMessage,
tap_eip712_domain,
};
use tap_graph::Receipt;
use thegraph_core::alloy::{
primitives::{address, Address},
signers::local::{coins_bip39::English, MnemonicBuilder, PrivateKeySigner},
};
use super::*;
use crate::tap::{CheckingReceipt, Eip712Domain};
fn create_signed_receipt_with_custom_value(value: u128) -> CheckingReceipt {
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 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 value: u128 = value;
let nonce: u64 = 10;
let receipt = Eip712SignedMessage::new(
&eip712_domain_separator,
Receipt {
allocation_id: address!("abababababababababababababababababababab"),
nonce,
timestamp_ns,
value,
},
&wallet,
)
.unwrap();
CheckingReceipt::new(TapReceipt::V1(receipt))
}
const RECEIPT_LIMIT: u128 = 10;
#[tokio::test]
async fn test_receipt_lower_than_limit() {
let signed_receipt = create_signed_receipt_with_custom_value(RECEIPT_LIMIT - 1);
let timestamp_check = ReceiptMaxValueCheck::new(RECEIPT_LIMIT);
assert!(timestamp_check
.check(&Context::new(), &signed_receipt)
.await
.is_ok());
}
#[tokio::test]
async fn test_receipt_higher_than_limit() {
let signed_receipt = create_signed_receipt_with_custom_value(RECEIPT_LIMIT + 1);
let timestamp_check = ReceiptMaxValueCheck::new(RECEIPT_LIMIT);
assert!(timestamp_check
.check(&Context::new(), &signed_receipt)
.await
.is_err());
}
#[tokio::test]
async fn test_receipt_same_as_limit() {
let signed_receipt = create_signed_receipt_with_custom_value(RECEIPT_LIMIT);
let timestamp_check = ReceiptMaxValueCheck::new(RECEIPT_LIMIT);
assert!(timestamp_check
.check(&Context::new(), &signed_receipt)
.await
.is_err());
}
}