Skip to content

Commit 341d0ab

Browse files
authored
Merge pull request #109 from semiotic-ai/105-tap_manager-always-requesting-rav-using-all-receipts-even-when-some-receipts-included-in-previous-rav
105 tap manager always requesting rav using all receipts even when some receipts included in previous rav
2 parents 31d5a15 + 0a8c6ad commit 341d0ab

File tree

8 files changed

+253
-32
lines changed

8 files changed

+253
-32
lines changed

CONTRIBUTING.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,33 @@ Depending on how large the project is, you may want to outsource the questioning
6666
> ```
6767
> Developer Certificate of Origin
6868
> Version 1.1
69-
>
69+
>
7070
> Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
71-
>
71+
>
7272
> Everyone is permitted to copy and distribute verbatim copies of this
7373
> license document, but changing it is not allowed.
74-
>
75-
>
74+
>
75+
>
7676
> Developer's Certificate of Origin 1.1
77-
>
77+
>
7878
> By making a contribution to this project, I certify that:
79-
>
79+
>
8080
> (a) The contribution was created in whole or in part by me and I
8181
> have the right to submit it under the open source license
8282
> indicated in the file; or
83-
>
83+
>
8484
> (b) The contribution is based upon previous work that, to the best
8585
> of my knowledge, is covered under an appropriate open source
8686
> license and I have the right under that license to submit that
8787
> work with modifications, whether created in whole or in part
8888
> by me, under the same open source license (unless I am
8989
> permitted to submit under a different license), as indicated
9090
> in the file; or
91-
>
91+
>
9292
> (c) The contribution was provided directly to me by some other
9393
> person who certified (a), (b) or (c) and I have not modified
9494
> it.
95-
>
95+
>
9696
> (d) I understand and agree that this project and the contribution
9797
> are public and that a record of the contribution (including all
9898
> personal information I submit with it, including my sign-off) is
@@ -146,7 +146,7 @@ Once it's filed:
146146
147147
### Suggesting Enhancements
148148
149-
This section guides you through submitting an enhancement suggestion for H2S2, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions.
149+
This section guides you through submitting an enhancement suggestion for TAP, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions.
150150
151151
<!-- omit in toc -->
152152
#### Before Submitting an Enhancement
@@ -165,7 +165,7 @@ Enhancement suggestions are tracked as [GitHub issues](https://github.com/semiot
165165
- Provide a **step-by-step description of the suggested enhancement** in as many details as possible.
166166
- **Describe the current behavior** and **explain which behavior you expected to see instead** and why. At this point you can also tell which alternatives do not work for you.
167167
- You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to. You can use [this tool](https://www.cockos.com/licecap/) to record GIFs on macOS and Windows, and [this tool](https://github.com/colinkeenan/silentcast) or [this tool](https://github.com/GNOME/byzanz) on Linux. <!-- this should only be included if the project has a GUI -->
168-
- **Explain why this enhancement would be useful** to most H2S2 users. You may also want to point out the other projects that solved it better and which could serve as inspiration.
168+
- **Explain why this enhancement would be useful** to most TAP users. You may also want to point out the other projects that solved it better and which could serve as inspiration.
169169
170170
<!-- You might want to create an issue template for enhancement suggestions that can be used as a guide and that defines the structure of the information to be included. If you do so, reference it here in the description. -->
171171

tap_core/src/adapters/receipt_storage_adapter.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2023-, Semiotic AI, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use std::ops::Range;
5+
46
use crate::tap_receipt::ReceivedReceipt;
57

68
pub trait ReceiptStorageAdapter {
@@ -20,11 +22,19 @@ pub trait ReceiptStorageAdapter {
2022
&self,
2123
timestamp_ns: u64,
2224
) -> Result<Vec<(u64, ReceivedReceipt)>, Self::AdapterError>;
25+
fn retrieve_receipts_in_timestamp_range(
26+
&self,
27+
timestamp_range_ns: Range<u64>,
28+
) -> Result<Vec<(u64, ReceivedReceipt)>, Self::AdapterError>;
2329
fn update_receipt_by_id(
2430
&mut self,
2531
receipt_id: u64,
2632
receipt: ReceivedReceipt,
2733
) -> Result<(), Self::AdapterError>;
2834
fn remove_receipt_by_id(&mut self, receipt_id: u64) -> Result<(), Self::AdapterError>;
2935
fn remove_receipts_by_ids(&mut self, receipt_ids: &[u64]) -> Result<(), Self::AdapterError>;
36+
fn remove_receipts_in_timestamp_range(
37+
&mut self,
38+
timestamp_ns: Range<u64>,
39+
) -> Result<(), Self::AdapterError>;
3040
}

tap_core/src/adapters/test/receipt_storage_adapter_mock.rs

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use std::{
55
collections::HashMap,
6+
ops::Range,
67
sync::{Arc, RwLock},
78
};
89

@@ -35,7 +36,12 @@ impl ReceiptStorageAdapter for ReceiptStorageAdapterMock {
3536
type AdapterError = AdpaterErrorMock;
3637
fn store_receipt(&mut self, receipt: ReceivedReceipt) -> Result<u64, Self::AdapterError> {
3738
let id = self.unique_id;
38-
let mut receipt_storage = self.receipt_storage.write().unwrap();
39+
let mut receipt_storage =
40+
self.receipt_storage
41+
.write()
42+
.map_err(|e| Self::AdapterError::AdapterError {
43+
error: e.to_string(),
44+
})?;
3945
receipt_storage.insert(id, receipt);
4046
self.unique_id += 1;
4147
Ok(id)
@@ -44,7 +50,12 @@ impl ReceiptStorageAdapter for ReceiptStorageAdapterMock {
4450
&self,
4551
receipt_id: u64,
4652
) -> Result<ReceivedReceipt, Self::AdapterError> {
47-
let receipt_storage = self.receipt_storage.read().unwrap();
53+
let receipt_storage =
54+
self.receipt_storage
55+
.read()
56+
.map_err(|e| Self::AdapterError::AdapterError {
57+
error: e.to_string(),
58+
})?;
4859

4960
receipt_storage
5061
.get(&receipt_id)
@@ -57,7 +68,12 @@ impl ReceiptStorageAdapter for ReceiptStorageAdapterMock {
5768
&self,
5869
timestamp_ns: u64,
5970
) -> Result<Vec<(u64, ReceivedReceipt)>, Self::AdapterError> {
60-
let receipt_storage = self.receipt_storage.read().unwrap();
71+
let receipt_storage =
72+
self.receipt_storage
73+
.read()
74+
.map_err(|e| Self::AdapterError::AdapterError {
75+
error: e.to_string(),
76+
})?;
6177
Ok(receipt_storage
6278
.iter()
6379
.filter(|(_, rx_receipt)| {
@@ -70,7 +86,12 @@ impl ReceiptStorageAdapter for ReceiptStorageAdapterMock {
7086
&self,
7187
timestamp_ns: u64,
7288
) -> Result<Vec<(u64, ReceivedReceipt)>, Self::AdapterError> {
73-
let receipt_storage = self.receipt_storage.read().unwrap();
89+
let receipt_storage =
90+
self.receipt_storage
91+
.read()
92+
.map_err(|e| Self::AdapterError::AdapterError {
93+
error: e.to_string(),
94+
})?;
7495
Ok(receipt_storage
7596
.iter()
7697
.filter(|(_, rx_receipt)| {
@@ -79,12 +100,35 @@ impl ReceiptStorageAdapter for ReceiptStorageAdapterMock {
79100
.map(|(&id, rx_receipt)| (id, rx_receipt.clone()))
80101
.collect())
81102
}
103+
fn retrieve_receipts_in_timestamp_range(
104+
&self,
105+
timestamp_range_ns: Range<u64>,
106+
) -> Result<Vec<(u64, ReceivedReceipt)>, Self::AdapterError> {
107+
let receipt_storage =
108+
self.receipt_storage
109+
.read()
110+
.map_err(|e| Self::AdapterError::AdapterError {
111+
error: e.to_string(),
112+
})?;
113+
Ok(receipt_storage
114+
.iter()
115+
.filter(|(_, rx_receipt)| {
116+
timestamp_range_ns.contains(&rx_receipt.signed_receipt.message.timestamp_ns)
117+
})
118+
.map(|(&id, rx_receipt)| (id, rx_receipt.clone()))
119+
.collect())
120+
}
82121
fn update_receipt_by_id(
83122
&mut self,
84123
receipt_id: u64,
85124
receipt: ReceivedReceipt,
86125
) -> Result<(), Self::AdapterError> {
87-
let mut receipt_storage = self.receipt_storage.write().unwrap();
126+
let mut receipt_storage =
127+
self.receipt_storage
128+
.write()
129+
.map_err(|e| Self::AdapterError::AdapterError {
130+
error: e.to_string(),
131+
})?;
88132

89133
if !receipt_storage.contains_key(&receipt_id) {
90134
return Err(AdpaterErrorMock::AdapterError {
@@ -97,7 +141,12 @@ impl ReceiptStorageAdapter for ReceiptStorageAdapterMock {
97141
Ok(())
98142
}
99143
fn remove_receipt_by_id(&mut self, receipt_id: u64) -> Result<(), Self::AdapterError> {
100-
let mut receipt_storage = self.receipt_storage.write().unwrap();
144+
let mut receipt_storage =
145+
self.receipt_storage
146+
.write()
147+
.map_err(|e| Self::AdapterError::AdapterError {
148+
error: e.to_string(),
149+
})?;
101150
receipt_storage
102151
.remove(&receipt_id)
103152
.map(|_| ())
@@ -111,4 +160,19 @@ impl ReceiptStorageAdapter for ReceiptStorageAdapterMock {
111160
}
112161
Ok(())
113162
}
163+
fn remove_receipts_in_timestamp_range(
164+
&mut self,
165+
timestamp_ns: Range<u64>,
166+
) -> Result<(), Self::AdapterError> {
167+
let mut receipt_storage =
168+
self.receipt_storage
169+
.write()
170+
.map_err(|e| Self::AdapterError::AdapterError {
171+
error: e.to_string(),
172+
})?;
173+
receipt_storage.retain(|_, rx_receipt| {
174+
!timestamp_ns.contains(&rx_receipt.signed_receipt.message.timestamp_ns)
175+
});
176+
Ok(())
177+
}
114178
}

tap_core/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ pub enum Error {
3939
AdapterError { source_error_message: String },
4040
#[error("Failed to produce rav request, no valid receipts")]
4141
NoValidReceiptsForRAVRequest,
42+
#[error("Timestamp range error: min_timestamp_ns: {min_timestamp_ns}, max_timestamp_ns: {max_timestamp_ns}. Adjust timestamp buffer.")]
43+
TimestampRangeError {
44+
min_timestamp_ns: u64,
45+
max_timestamp_ns: u64,
46+
},
4247
}
4348

4449
pub type Result<T> = StdResult<T, Error>;

tap_core/src/tap_manager/manager.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,15 @@ impl<
129129
});
130130
}
131131

132-
self.rav_storage_adapter
132+
let rav_id = self
133+
.rav_storage_adapter
133134
.store_rav(signed_rav)
134135
.map_err(|err| Error::AdapterError {
135136
source_error_message: err.to_string(),
136137
})?;
137138

139+
self.current_rav_id = Some(rav_id);
140+
138141
Ok(())
139142
}
140143

@@ -146,18 +149,26 @@ impl<
146149
///
147150
/// Returns [`Error::AdapterError`] if unable to fetch previous RAV or if unable to fetch previous receipts
148151
///
152+
/// Returns [`Error::TimestampRangeError`] if the max timestamp of the previous RAV is greater than the min timestamp. Caused by timestamp buffer being too large, or requests coming too soon.
153+
///
149154
pub fn create_rav_request(&mut self, timestamp_buffer_ns: u64) -> Result<RAVRequest, Error> {
150155
let previous_rav = self.get_previous_rav()?;
156+
let min_timestamp_ns = previous_rav
157+
.as_ref()
158+
.map(|rav| rav.message.timestamp_ns + 1)
159+
.unwrap_or(0);
151160

152-
let (valid_receipts, invalid_receipts) = self.collect_receipts(timestamp_buffer_ns)?;
161+
let (valid_receipts, invalid_receipts) =
162+
self.collect_receipts(timestamp_buffer_ns, min_timestamp_ns)?;
153163

154-
let expected_rav = Self::generate_expected_rav(&valid_receipts, previous_rav)?;
164+
let expected_rav = Self::generate_expected_rav(&valid_receipts, previous_rav.clone())?;
155165

156166
self.receipt_auditor
157167
.update_min_timestamp_ns(expected_rav.timestamp_ns + 1);
158168

159169
Ok(RAVRequest {
160170
valid_receipts,
171+
previous_rav,
161172
invalid_receipts,
162173
expected_rav,
163174
})
@@ -181,11 +192,19 @@ impl<
181192
fn collect_receipts(
182193
&mut self,
183194
timestamp_buffer_ns: u64,
195+
min_timestamp_ns: u64,
184196
) -> Result<(Vec<SignedReceipt>, Vec<SignedReceipt>), Error> {
185-
let cutoff_timestamp = crate::get_current_timestamp_u64_ns()? - timestamp_buffer_ns;
197+
let max_timestamp_ns = crate::get_current_timestamp_u64_ns()? - timestamp_buffer_ns;
198+
199+
if min_timestamp_ns > max_timestamp_ns {
200+
return Err(Error::TimestampRangeError {
201+
min_timestamp_ns,
202+
max_timestamp_ns,
203+
});
204+
}
186205
let received_receipts = self
187206
.receipt_storage_adapter
188-
.retrieve_receipts_upto_timestamp(cutoff_timestamp)
207+
.retrieve_receipts_in_timestamp_range(min_timestamp_ns..max_timestamp_ns)
189208
.map_err(|err| Error::AdapterError {
190209
source_error_message: err.to_string(),
191210
})?;

tap_core/src/tap_manager/rav_request.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
use serde::{Deserialize, Serialize};
55

6-
use super::SignedReceipt;
6+
use super::{SignedRAV, SignedReceipt};
77
use crate::receipt_aggregate_voucher::ReceiptAggregateVoucher;
88

99
#[derive(Debug, Serialize, Deserialize, Clone)]
1010

1111
pub struct RAVRequest {
1212
pub valid_receipts: Vec<SignedReceipt>,
13+
pub previous_rav: Option<SignedRAV>,
1314
pub invalid_receipts: Vec<SignedReceipt>,
1415
pub expected_rav: ReceiptAggregateVoucher,
1516
}

0 commit comments

Comments
 (0)