Skip to content

Commit 25a3316

Browse files
authored
refactor!: manager generic over receipt (#266)
* refactor!: manager generic over receipt Signed-off-by: Gustavo Inacio <[email protected]> * refactor!: rename RAVRequest with RavRequest Signed-off-by: Gustavo Inacio <[email protected]> --------- Signed-off-by: Gustavo Inacio <[email protected]>
1 parent e5511ff commit 25a3316

File tree

18 files changed

+240
-162
lines changed

18 files changed

+240
-162
lines changed

tap_aggregator/src/aggregator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn check_and_aggregate_receipts(
4444
// Get the allocation id from the first receipt, return error if there are no receipts
4545
let allocation_id = match receipts.first() {
4646
Some(receipt) => receipt.message.allocation_id,
47-
None => return Err(tap_core::Error::NoValidReceiptsForRAVRequest.into()),
47+
None => return Err(tap_core::Error::NoValidReceiptsForRavRequest.into()),
4848
};
4949

5050
// Check that the receipts all have the same allocation id

tap_core/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub enum Error {
4646
AdapterError { source_error: anyhow::Error },
4747
/// Error when no valid receipts are found for a RAV request
4848
#[error("Failed to produce rav request, no valid receipts")]
49-
NoValidReceiptsForRAVRequest,
49+
NoValidReceiptsForRavRequest,
5050

5151
/// Error when the previous RAV allocation id does not match the allocation id from the new receipt
5252
#[error("Previous RAV allocation id ({prev_id}) doesn't match the allocation id from the new receipt ({new_id}).")]

tap_core/src/manager/adapters/receipt.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use async_trait::async_trait;
77

88
use crate::receipt::{
99
state::{Checking, ReceiptState},
10-
ReceiptWithState,
10+
ReceiptWithState, WithValueAndTimestamp,
1111
};
1212

1313
/// Stores receipts in the storage.
@@ -16,7 +16,7 @@ use crate::receipt::{
1616
///
1717
/// For example code see [crate::manager::context::memory::ReceiptStorage]
1818
#[async_trait]
19-
pub trait ReceiptStore {
19+
pub trait ReceiptStore<Rcpt> {
2020
/// Defines the user-specified error type.
2121
///
2222
/// This error type should implement the `Error` and `Debug` traits from the standard library.
@@ -29,7 +29,7 @@ pub trait ReceiptStore {
2929
/// this process should be captured and returned as an `AdapterError`.
3030
async fn store_receipt(
3131
&self,
32-
receipt: ReceiptWithState<Checking>,
32+
receipt: ReceiptWithState<Checking, Rcpt>,
3333
) -> Result<u64, Self::AdapterError>;
3434
}
3535

@@ -62,7 +62,7 @@ pub trait ReceiptDelete {
6262
///
6363
/// For example code see [crate::manager::context::memory::ReceiptStorage]
6464
#[async_trait]
65-
pub trait ReceiptRead {
65+
pub trait ReceiptRead<Rcpt> {
6666
/// Defines the user-specified error type.
6767
///
6868
/// This error type should implement the `Error` and `Debug` traits from
@@ -92,15 +92,15 @@ pub trait ReceiptRead {
9292
&self,
9393
timestamp_range_ns: R,
9494
limit: Option<u64>,
95-
) -> Result<Vec<ReceiptWithState<Checking>>, Self::AdapterError>;
95+
) -> Result<Vec<ReceiptWithState<Checking, Rcpt>>, Self::AdapterError>;
9696
}
9797

9898
/// See [`ReceiptRead::retrieve_receipts_in_timestamp_range()`] for details.
9999
///
100100
/// WARNING: Will sort the receipts by timestamp using
101101
/// [vec::sort_unstable](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort_unstable).
102-
pub fn safe_truncate_receipts<T: ReceiptState>(
103-
receipts: &mut Vec<ReceiptWithState<T>>,
102+
pub fn safe_truncate_receipts<T: ReceiptState, Rcpt: WithValueAndTimestamp>(
103+
receipts: &mut Vec<ReceiptWithState<T, Rcpt>>,
104104
limit: u64,
105105
) {
106106
if receipts.len() <= limit as usize {
@@ -110,27 +110,19 @@ pub fn safe_truncate_receipts<T: ReceiptState>(
110110
return;
111111
}
112112

113-
receipts.sort_unstable_by_key(|rx_receipt| rx_receipt.signed_receipt().message.timestamp_ns);
113+
receipts.sort_unstable_by_key(|rx_receipt| rx_receipt.signed_receipt().timestamp_ns());
114114

115115
// This one will be the last timestamp in `receipts` after naive truncation
116-
let last_timestamp = receipts[limit as usize - 1]
117-
.signed_receipt()
118-
.message
119-
.timestamp_ns;
116+
let last_timestamp = receipts[limit as usize - 1].signed_receipt().timestamp_ns();
120117
// This one is the timestamp that comes just after the one above
121-
let after_last_timestamp = receipts[limit as usize]
122-
.signed_receipt()
123-
.message
124-
.timestamp_ns;
118+
let after_last_timestamp = receipts[limit as usize].signed_receipt().timestamp_ns();
125119

126120
receipts.truncate(limit as usize);
127121

128122
if last_timestamp == after_last_timestamp {
129123
// If the last timestamp is the same as the one that came after it, we need to
130124
// remove all the receipts with the same timestamp as the last one, because
131125
// otherwise we would leave behind part of the receipts for that timestamp.
132-
receipts.retain(|rx_receipt| {
133-
rx_receipt.signed_receipt().message.timestamp_ns != last_timestamp
134-
});
126+
receipts.retain(|rx_receipt| rx_receipt.signed_receipt().timestamp_ns() != last_timestamp);
135127
}
136128
}

tap_core/src/manager/context/memory.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ use async_trait::async_trait;
1818
use crate::{
1919
manager::adapters::*,
2020
rav::SignedRAV,
21-
receipt::{checks::StatefulTimestampCheck, state::Checking, ReceiptWithState},
21+
receipt::{checks::StatefulTimestampCheck, state::Checking, ReceiptWithState, SignedReceipt},
2222
signed_message::MessageId,
2323
};
2424

2525
pub type EscrowStorage = Arc<RwLock<HashMap<Address, u128>>>;
2626
pub type QueryAppraisals = Arc<RwLock<HashMap<MessageId, u128>>>;
27-
pub type ReceiptStorage = Arc<RwLock<HashMap<u64, ReceiptWithState<Checking>>>>;
27+
pub type ReceiptStorage = Arc<RwLock<HashMap<u64, ReceiptWithState<Checking, SignedReceipt>>>>;
2828
pub type RAVStorage = Arc<RwLock<Option<SignedRAV>>>;
2929

3030
use thiserror::Error;
@@ -71,7 +71,7 @@ impl InMemoryContext {
7171
pub async fn retrieve_receipt_by_id(
7272
&self,
7373
receipt_id: u64,
74-
) -> Result<ReceiptWithState<Checking>, InMemoryError> {
74+
) -> Result<ReceiptWithState<Checking, SignedReceipt>, InMemoryError> {
7575
let receipt_storage = self.receipt_storage.read().unwrap();
7676

7777
receipt_storage
@@ -85,7 +85,7 @@ impl InMemoryContext {
8585
pub async fn retrieve_receipts_by_timestamp(
8686
&self,
8787
timestamp_ns: u64,
88-
) -> Result<Vec<(u64, ReceiptWithState<Checking>)>, InMemoryError> {
88+
) -> Result<Vec<(u64, ReceiptWithState<Checking, SignedReceipt>)>, InMemoryError> {
8989
let receipt_storage = self.receipt_storage.read().unwrap();
9090
Ok(receipt_storage
9191
.iter()
@@ -99,7 +99,7 @@ impl InMemoryContext {
9999
pub async fn retrieve_receipts_upto_timestamp(
100100
&self,
101101
timestamp_ns: u64,
102-
) -> Result<Vec<ReceiptWithState<Checking>>, InMemoryError> {
102+
) -> Result<Vec<ReceiptWithState<Checking, SignedReceipt>>, InMemoryError> {
103103
self.retrieve_receipts_in_timestamp_range(..=timestamp_ns, None)
104104
.await
105105
}
@@ -147,12 +147,12 @@ impl RAVRead for InMemoryContext {
147147
}
148148

149149
#[async_trait]
150-
impl ReceiptStore for InMemoryContext {
150+
impl ReceiptStore<SignedReceipt> for InMemoryContext {
151151
type AdapterError = InMemoryError;
152152

153153
async fn store_receipt(
154154
&self,
155-
receipt: ReceiptWithState<Checking>,
155+
receipt: ReceiptWithState<Checking, SignedReceipt>,
156156
) -> Result<u64, Self::AdapterError> {
157157
let mut id_pointer = self.unique_id.write().unwrap();
158158
let id_previous = *id_pointer;
@@ -179,15 +179,15 @@ impl ReceiptDelete for InMemoryContext {
179179
}
180180
}
181181
#[async_trait]
182-
impl ReceiptRead for InMemoryContext {
182+
impl ReceiptRead<SignedReceipt> for InMemoryContext {
183183
type AdapterError = InMemoryError;
184184
async fn retrieve_receipts_in_timestamp_range<R: RangeBounds<u64> + std::marker::Send>(
185185
&self,
186186
timestamp_range_ns: R,
187187
limit: Option<u64>,
188-
) -> Result<Vec<ReceiptWithState<Checking>>, Self::AdapterError> {
188+
) -> Result<Vec<ReceiptWithState<Checking, SignedReceipt>>, Self::AdapterError> {
189189
let receipt_storage = self.receipt_storage.read().unwrap();
190-
let mut receipts_in_range: Vec<ReceiptWithState<Checking>> = receipt_storage
190+
let mut receipts_in_range: Vec<ReceiptWithState<Checking, SignedReceipt>> = receipt_storage
191191
.iter()
192192
.filter(|(_, rx_receipt)| {
193193
timestamp_range_ns.contains(&rx_receipt.signed_receipt().message.timestamp_ns)
@@ -264,7 +264,7 @@ pub mod checks {
264264
receipt::{
265265
checks::{Check, CheckError, CheckResult, ReceiptCheck},
266266
state::Checking,
267-
Context, ReceiptError, ReceiptWithState,
267+
Context, ReceiptError, ReceiptWithState, SignedReceipt,
268268
},
269269
signed_message::MessageId,
270270
};
@@ -274,7 +274,7 @@ pub mod checks {
274274
valid_signers: HashSet<Address>,
275275
allocation_ids: Arc<RwLock<HashSet<Address>>>,
276276
_query_appraisals: Arc<RwLock<HashMap<MessageId, u128>>>,
277-
) -> Vec<ReceiptCheck> {
277+
) -> Vec<ReceiptCheck<SignedReceipt>> {
278278
vec![
279279
// Arc::new(UniqueCheck ),
280280
// Arc::new(ValueCheck { query_appraisals }),
@@ -291,8 +291,12 @@ pub mod checks {
291291
}
292292

293293
#[async_trait::async_trait]
294-
impl Check for AllocationIdCheck {
295-
async fn check(&self, _: &Context, receipt: &ReceiptWithState<Checking>) -> CheckResult {
294+
impl Check<SignedReceipt> for AllocationIdCheck {
295+
async fn check(
296+
&self,
297+
_: &Context,
298+
receipt: &ReceiptWithState<Checking, SignedReceipt>,
299+
) -> CheckResult {
296300
let received_allocation_id = receipt.signed_receipt().message.allocation_id;
297301
if self
298302
.allocation_ids
@@ -318,8 +322,12 @@ pub mod checks {
318322
}
319323

320324
#[async_trait::async_trait]
321-
impl Check for SignatureCheck {
322-
async fn check(&self, _: &Context, receipt: &ReceiptWithState<Checking>) -> CheckResult {
325+
impl Check<SignedReceipt> for SignatureCheck {
326+
async fn check(
327+
&self,
328+
_: &Context,
329+
receipt: &ReceiptWithState<Checking, SignedReceipt>,
330+
) -> CheckResult {
323331
let recovered_address = receipt
324332
.signed_receipt()
325333
.recover_signer(&self.domain_separator)

tap_core/src/manager/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@
5151
//! struct MyContext;
5252
//!
5353
//! #[async_trait]
54-
//! impl ReceiptStore for MyContext {
54+
//! impl<T: Send + 'static> ReceiptStore<T> for MyContext {
5555
//! type AdapterError = ReceiptError;
5656
//!
57-
//! async fn store_receipt(&self, receipt: ReceiptWithState<Checking>) -> Result<u64, Self::AdapterError> {
57+
//! async fn store_receipt(&self, receipt: ReceiptWithState<Checking, T>) -> Result<u64, Self::AdapterError> {
5858
//! // ...
5959
//! # Ok(0)
6060
//! }

0 commit comments

Comments
 (0)