Skip to content

Commit 9fd4beb

Browse files
authored
refactor!: relax manager constraints for ravs (#275)
* refactor!: relax manager constraints for ravs Signed-off-by: Gustavo Inacio <[email protected]> * feat: add custom aggregation error Signed-off-by: Gustavo Inacio <[email protected]> --------- Signed-off-by: Gustavo Inacio <[email protected]>
1 parent df70b82 commit 9fd4beb

File tree

5 files changed

+53
-58
lines changed

5 files changed

+53
-58
lines changed

tap_core/src/manager/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@
6262
//! # #[tokio::main]
6363
//! # async fn main() {
6464
//! # use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner};
65-
//! # use tap_graph::{Receipt, SignedReceipt, ReceiptAggregateVoucher};
65+
//! # use tap_graph::{Receipt, SignedReceipt};
6666
//! # use tap_core::signed_message::Eip712SignedMessage;
6767
//! # let domain_separator = Eip712Domain::default();
6868
//! # let wallet = PrivateKeySigner::random();
6969
//! # let message = Receipt::new(Address::from([0x11u8; 20]), 100).unwrap();
7070
//!
7171
//! let receipt = Eip712SignedMessage::new(&domain_separator, message, &wallet).unwrap();
7272
//!
73-
//! let manager = Manager::<_, _, ReceiptAggregateVoucher>::new(domain_separator, MyContext, CheckList::empty());
73+
//! let manager = Manager::new(domain_separator, MyContext, CheckList::empty());
7474
//! manager.verify_and_store_receipt(&Context::new(), receipt).await.unwrap()
7575
//! # }
7676
//! ```

tap_core/src/manager/tap_manager.rs

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

4-
use std::marker::PhantomData;
5-
64
use alloy::{dyn_abi::Eip712Domain, sol_types::SolStruct};
75
use tap_receipt::rav::Aggregate;
86

@@ -20,7 +18,7 @@ use crate::{
2018
Error,
2119
};
2220

23-
pub struct Manager<E, Rcpt, Rav> {
21+
pub struct Manager<E, Rcpt> {
2422
/// Context that implements adapters
2523
context: E,
2624

@@ -30,11 +28,9 @@ pub struct Manager<E, Rcpt, Rav> {
3028
/// Struct responsible for doing checks for receipt. Ownership stays with manager allowing manager
3129
/// to update configuration ( like minimum timestamp ).
3230
domain_separator: Eip712Domain,
33-
34-
_phantom: PhantomData<Rav>,
3531
}
3632

37-
impl<E, Rcpt, Rav> Manager<E, Rcpt, Rav> {
33+
impl<E, Rcpt> Manager<E, Rcpt> {
3834
/// Creates new manager with provided `adapters`, any receipts received by this manager
3935
/// will complete all `required_checks` before being accepted or declined from RAV.
4036
/// `starting_min_timestamp` will be used as min timestamp until the first RAV request is created.
@@ -48,27 +44,40 @@ impl<E, Rcpt, Rav> Manager<E, Rcpt, Rav> {
4844
context,
4945
domain_separator,
5046
checks: checks.into(),
51-
_phantom: PhantomData,
5247
}
5348
}
54-
}
5549

56-
impl<E, Rcpt, Rav> Manager<E, Rcpt, Rav>
57-
where
58-
E: RavStore<Rav> + SignatureChecker,
59-
Rav: SolStruct + PartialEq<Rav> + Sync + std::fmt::Debug,
60-
{
50+
async fn get_previous_rav<Rav: SolStruct>(
51+
&self,
52+
) -> Result<Option<Eip712SignedMessage<Rav>>, Error>
53+
where
54+
E: RavRead<Rav>,
55+
{
56+
let previous_rav = self
57+
.context
58+
.last_rav()
59+
.await
60+
.map_err(|err| Error::AdapterError {
61+
source_error: anyhow::Error::new(err),
62+
})?;
63+
Ok(previous_rav)
64+
}
65+
6166
/// Verify `signed_rav` matches all values on `expected_rav`, and that `signed_rav` has a valid signer.
6267
///
6368
/// # Errors
6469
///
6570
/// Returns [`Error::AdapterError`] if there are any errors while storing RAV
6671
///
67-
pub async fn verify_and_store_rav(
72+
pub async fn verify_and_store_rav<Rav>(
6873
&self,
6974
expected_rav: Rav,
7075
signed_rav: Eip712SignedMessage<Rav>,
71-
) -> std::result::Result<(), Error> {
76+
) -> std::result::Result<(), Error>
77+
where
78+
E: RavStore<Rav> + SignatureChecker,
79+
Rav: SolStruct + PartialEq<Rav> + Sync + std::fmt::Debug,
80+
{
7281
self.context
7382
.check_signature(&signed_rav, &self.domain_separator)
7483
.await?;
@@ -91,27 +100,10 @@ where
91100
}
92101
}
93102

94-
impl<E, Rcpt, Rav> Manager<E, Rcpt, Rav>
103+
impl<E, Rcpt> Manager<E, Rcpt>
95104
where
96-
E: RavRead<Rav>,
97-
Rav: SolStruct,
98-
{
99-
async fn get_previous_rav(&self) -> Result<Option<Eip712SignedMessage<Rav>>, Error> {
100-
let previous_rav = self
101-
.context
102-
.last_rav()
103-
.await
104-
.map_err(|err| Error::AdapterError {
105-
source_error: anyhow::Error::new(err),
106-
})?;
107-
Ok(previous_rav)
108-
}
109-
}
110-
111-
impl<E, Rcpt, Rav> Manager<E, Rcpt, Rav>
112-
where
113-
E: ReceiptRead<Rcpt> + SignatureChecker,
114-
Rcpt: WithUniqueId + WithValueAndTimestamp + Sync,
105+
E: ReceiptRead<Rcpt>,
106+
Rcpt: WithUniqueId + WithValueAndTimestamp,
115107
{
116108
async fn collect_receipts(
117109
&self,
@@ -168,14 +160,7 @@ where
168160

169161
Ok((checked_receipts, failed_receipts))
170162
}
171-
}
172163

173-
impl<E, Rcpt, Rav> Manager<E, Rcpt, Rav>
174-
where
175-
E: ReceiptRead<Rcpt> + RavRead<Rav> + SignatureChecker,
176-
Rav: SolStruct + WithValueAndTimestamp + Clone + Aggregate<Rcpt>,
177-
Rcpt: WithUniqueId + WithValueAndTimestamp + Sync,
178-
{
179164
/// Completes remaining checks on all receipts up to
180165
/// (current time - `timestamp_buffer_ns`). Returns them in two lists
181166
/// (valid receipts and invalid receipts) along with the expected RAV that
@@ -191,12 +176,16 @@ where
191176
/// previous RAV is greater than the min timestamp. Caused by timestamp
192177
/// buffer being too large, or requests coming too soon.
193178
///
194-
pub async fn create_rav_request(
179+
pub async fn create_rav_request<Rav>(
195180
&self,
196181
ctx: &Context,
197182
timestamp_buffer_ns: u64,
198183
receipts_limit: Option<u64>,
199-
) -> Result<RavRequest<Rcpt, Rav>, Error> {
184+
) -> Result<RavRequest<Rcpt, Rav>, Error>
185+
where
186+
E: RavRead<Rav>,
187+
Rav: SolStruct + WithValueAndTimestamp + Clone + Aggregate<Rcpt>,
188+
{
200189
let previous_rav = self.get_previous_rav().await?;
201190
let min_timestamp_ns = previous_rav
202191
.as_ref()
@@ -218,10 +207,9 @@ where
218207
}
219208
}
220209

221-
impl<E, Rcpt, Rav> Manager<E, Rcpt, Rav>
210+
impl<E, Rcpt> Manager<E, Rcpt>
222211
where
223-
E: ReceiptDelete + RavRead<Rav>,
224-
Rav: SolStruct + WithValueAndTimestamp,
212+
E: ReceiptDelete,
225213
{
226214
/// Removes obsolete receipts from storage. Obsolete receipts are receipts
227215
/// that are older than the last RAV, and therefore already aggregated into the RAV.
@@ -233,7 +221,11 @@ where
233221
/// Returns [`Error::AdapterError`] if there are any errors while retrieving
234222
/// last RAV or removing receipts
235223
///
236-
pub async fn remove_obsolete_receipts(&self) -> Result<(), Error> {
224+
pub async fn remove_obsolete_receipts<Rav>(&self) -> Result<(), Error>
225+
where
226+
E: RavRead<Rav>,
227+
Rav: SolStruct + WithValueAndTimestamp,
228+
{
237229
match self.get_previous_rav().await? {
238230
Some(last_rav) => {
239231
self.context
@@ -249,7 +241,7 @@ where
249241
}
250242
}
251243

252-
impl<E, Rcpt, Rav> Manager<E, Rcpt, Rav>
244+
impl<E, Rcpt> Manager<E, Rcpt>
253245
where
254246
E: ReceiptStore<Rcpt>,
255247
{

tap_core/tests/manager_test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ async fn manager_verify_and_store_varying_initial_checks(
128128
signer,
129129
..
130130
} = context;
131-
let manager =
132-
Manager::<_, _, ReceiptAggregateVoucher>::new(domain_separator.clone(), context, checks);
131+
let manager = Manager::new(domain_separator.clone(), context, checks);
133132

134133
let value = 20u128;
135134
let signed_receipt = Eip712SignedMessage::new(

tap_integration_tests/tests/indexer_mock.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ pub trait Rpc {
4444
/// threshold is a limit to which receipt_count can increment, after reaching which RAV request is triggered.
4545
/// aggregator_client is an HTTP client used for making JSON-RPC requests to another server.
4646
pub struct RpcManager<E> {
47-
manager: Arc<Manager<E, SignedReceipt, ReceiptAggregateVoucher>>, // Manager object reference counted with an Arc
48-
receipt_count: Arc<AtomicU64>, // Thread-safe atomic counter for receipts
49-
threshold: u64, // The count at which a RAV request will be triggered
47+
manager: Arc<Manager<E, SignedReceipt>>, // Manager object reference counted with an Arc
48+
receipt_count: Arc<AtomicU64>, // Thread-safe atomic counter for receipts
49+
threshold: u64, // The count at which a RAV request will be triggered
5050
aggregator_client: (HttpClient, String), // HTTP client for sending requests to the aggregator server
5151
}
5252

@@ -66,7 +66,7 @@ where
6666
aggregate_server_api_version: String,
6767
) -> Result<Self> {
6868
Ok(Self {
69-
manager: Arc::new(Manager::<E, SignedReceipt, ReceiptAggregateVoucher>::new(
69+
manager: Arc::new(Manager::<E, SignedReceipt>::new(
7070
domain_separator,
7171
context,
7272
required_checks,
@@ -184,7 +184,7 @@ where
184184

185185
// request_rav function creates a request for aggregate receipts (RAV), sends it to another server and verifies the result.
186186
async fn request_rav<E>(
187-
manager: &Arc<Manager<E, SignedReceipt, ReceiptAggregateVoucher>>,
187+
manager: &Arc<Manager<E, SignedReceipt>>,
188188
time_stamp_buffer: u64, // Buffer for timestamping, see tap_core for details
189189
aggregator_client: &(HttpClient, String), // HttpClient for making requests to the tap_aggregator server
190190
threshold: usize,

tap_receipt/src/rav.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ pub enum AggregationError {
2626
/// Error when no valid receipts are found for a RAV request
2727
#[error("Failed to produce rav request, no valid receipts")]
2828
NoValidReceiptsForRavRequest,
29+
30+
/// Other user-defined error
31+
#[error(transparent)]
32+
Other(anyhow::Error),
2933
}

0 commit comments

Comments
 (0)