Skip to content

Commit e5511ff

Browse files
authored
refactor!: remove unused escrow handler methods (#264)
* refactor!: remove unused escrow handler methods Signed-off-by: Gustavo Inacio <[email protected]> * refactor: rename escrow handler to signature checker Signed-off-by: Gustavo Inacio <[email protected]> * style: cargo fmt Signed-off-by: Gustavo Inacio <[email protected]> --------- Signed-off-by: Gustavo Inacio <[email protected]>
1 parent 6dda863 commit e5511ff

File tree

11 files changed

+78
-263
lines changed

11 files changed

+78
-263
lines changed

tap_core/src/manager/adapters/escrow.rs

Lines changed: 0 additions & 87 deletions
This file was deleted.

tap_core/src/manager/adapters/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
//! allows for easy integration with various storage solutions and verification
99
//! procedures, thereby making the library adaptable to a wide range of use cases.
1010
11-
mod escrow;
1211
mod rav;
1312
mod receipt;
13+
mod signature;
1414

15-
pub use escrow::EscrowHandler;
1615
pub use rav::*;
1716
pub use receipt::*;
17+
pub use signature::SignatureChecker;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2023-, Semiotic AI, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use alloy::{dyn_abi::Eip712Domain, primitives::Address, sol_types::SolStruct};
5+
use async_trait::async_trait;
6+
7+
use crate::{signed_message::EIP712SignedMessage, Error};
8+
9+
/// Manages the escrow operations
10+
///
11+
/// # Example
12+
///
13+
/// For example code see [crate::manager::context::memory::EscrowStorage]
14+
15+
#[async_trait]
16+
pub trait SignatureChecker: Send + Sync {
17+
/// Defines the user-specified error type.
18+
///
19+
/// This error type should implement the `Error` and `Debug` traits from
20+
/// the standard library.
21+
/// Errors of this type are returned to the user when an operation fails.
22+
type AdapterError: std::error::Error + std::fmt::Debug + Send + Sync + 'static;
23+
24+
/// Verifies the signer of the receipt
25+
async fn verify_signer(&self, signer_address: Address) -> Result<bool, Self::AdapterError>;
26+
27+
/// Checks if the signed message has a sender signature
28+
async fn check_signature<T: SolStruct + Sync>(
29+
&self,
30+
signed_message: &EIP712SignedMessage<T>,
31+
domain_separator: &Eip712Domain,
32+
) -> Result<(), Error> {
33+
let recovered_address = signed_message.recover_signer(domain_separator)?;
34+
if self
35+
.verify_signer(recovered_address)
36+
.await
37+
.map_err(|e| Error::FailedToVerifySigner(e.to_string()))?
38+
{
39+
Ok(())
40+
} else {
41+
Err(Error::InvalidRecoveredSigner {
42+
address: recovered_address,
43+
})
44+
}
45+
}
46+
}

tap_core/src/manager/context/memory.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,8 @@ impl InMemoryContext {
241241
}
242242

243243
#[async_trait]
244-
impl EscrowHandler for InMemoryContext {
244+
impl SignatureChecker for InMemoryContext {
245245
type AdapterError = InMemoryError;
246-
async fn get_available_escrow(&self, sender_id: Address) -> Result<u128, Self::AdapterError> {
247-
self.escrow(sender_id)
248-
}
249-
async fn subtract_escrow(
250-
&self,
251-
sender_id: Address,
252-
value: u128,
253-
) -> Result<(), Self::AdapterError> {
254-
self.reduce_escrow(sender_id, value)
255-
}
256246

257247
async fn verify_signer(&self, signer_address: Address) -> Result<bool, Self::AdapterError> {
258248
Ok(self

tap_core/src/manager/tap_manager.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
use alloy::dyn_abi::Eip712Domain;
55

6-
use super::adapters::{EscrowHandler, RAVRead, RAVStore, ReceiptDelete, ReceiptRead, ReceiptStore};
6+
use super::adapters::{
7+
RAVRead, RAVStore, ReceiptDelete, ReceiptRead, ReceiptStore, SignatureChecker,
8+
};
79
use crate::{
810
rav::{RAVRequest, ReceiptAggregateVoucher, SignedRAV},
911
receipt::{
1012
checks::{CheckBatch, CheckList, TimestampCheck, UniqueCheck},
11-
state::{Failed, Reserved},
13+
state::{Checked, Failed},
1214
Context, ReceiptError, ReceiptWithState, SignedReceipt,
1315
},
1416
Error,
@@ -42,7 +44,7 @@ impl<E> Manager<E> {
4244

4345
impl<E> Manager<E>
4446
where
45-
E: RAVStore + EscrowHandler,
47+
E: RAVStore + SignatureChecker,
4648
{
4749
/// Verify `signed_rav` matches all values on `expected_rav`, and that `signed_rav` has a valid signer.
4850
///
@@ -56,7 +58,7 @@ where
5658
signed_rav: SignedRAV,
5759
) -> std::result::Result<(), Error> {
5860
self.context
59-
.check_rav_signature(&signed_rav, &self.domain_separator)
61+
.check_signature(&signed_rav, &self.domain_separator)
6062
.await?;
6163

6264
if signed_rav.message != expected_rav {
@@ -95,7 +97,7 @@ where
9597

9698
impl<E> Manager<E>
9799
where
98-
E: ReceiptRead + EscrowHandler,
100+
E: ReceiptRead + SignatureChecker,
99101
{
100102
async fn collect_receipts(
101103
&self,
@@ -105,7 +107,7 @@ where
105107
limit: Option<u64>,
106108
) -> Result<
107109
(
108-
Vec<ReceiptWithState<Reserved>>,
110+
Vec<ReceiptWithState<Checked>>,
109111
Vec<ReceiptWithState<Failed>>,
110112
),
111113
Error,
@@ -126,9 +128,8 @@ where
126128
source_error: anyhow::Error::new(err),
127129
})?;
128130

129-
let mut awaiting_reserve_receipts = vec![];
131+
let mut checked_receipts = vec![];
130132
let mut failed_receipts = vec![];
131-
let mut reserved_receipts = vec![];
132133

133134
// check for timestamp
134135
let (checking_receipts, already_failed) =
@@ -146,27 +147,18 @@ where
146147
.map_err(|e| Error::ReceiptError(ReceiptError::RetryableCheck(e)))?;
147148

148149
match receipt {
149-
Ok(checked) => awaiting_reserve_receipts.push(checked),
150-
Err(failed) => failed_receipts.push(failed),
151-
}
152-
}
153-
for checked in awaiting_reserve_receipts {
154-
match checked
155-
.check_and_reserve_escrow(&self.context, &self.domain_separator)
156-
.await
157-
{
158-
Ok(reserved) => reserved_receipts.push(reserved),
150+
Ok(checked) => checked_receipts.push(checked),
159151
Err(failed) => failed_receipts.push(failed),
160152
}
161153
}
162154

163-
Ok((reserved_receipts, failed_receipts))
155+
Ok((checked_receipts, failed_receipts))
164156
}
165157
}
166158

167159
impl<E> Manager<E>
168160
where
169-
E: ReceiptRead + RAVRead + EscrowHandler,
161+
E: ReceiptRead + RAVRead + SignatureChecker,
170162
{
171163
/// Completes remaining checks on all receipts up to
172164
/// (current time - `timestamp_buffer_ns`). Returns them in two lists
@@ -210,7 +202,7 @@ where
210202
}
211203

212204
fn generate_expected_rav(
213-
receipts: &[ReceiptWithState<Reserved>],
205+
receipts: &[ReceiptWithState<Checked>],
214206
previous_rav: Option<SignedRAV>,
215207
) -> Result<ReceiptAggregateVoucher, Error> {
216208
if receipts.is_empty() {

tap_core/src/rav/request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::{
55
rav::{ReceiptAggregateVoucher, SignedRAV},
66
receipt::{
7-
state::{Failed, Reserved},
7+
state::{Checked, Failed},
88
ReceiptWithState,
99
},
1010
Error,
@@ -14,7 +14,7 @@ use crate::{
1414
#[derive(Debug)]
1515
pub struct RAVRequest {
1616
/// List of checked and reserved receipts to aggregate
17-
pub valid_receipts: Vec<ReceiptWithState<Reserved>>,
17+
pub valid_receipts: Vec<ReceiptWithState<Checked>>,
1818
/// Optional previous RAV to aggregate with
1919
pub previous_rav: Option<SignedRAV>,
2020
/// List of failed receipt used to log invalid receipts

tap_core/src/receipt/received_receipt.rs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@
1313
//! This module is useful for managing and tracking the state of received receipts, as well as
1414
//! their progress through various checks and stages of inclusion in RAV requests and received RAVs.
1515
16-
use alloy::dyn_abi::Eip712Domain;
17-
1816
use super::{checks::CheckError, Context, Receipt, ReceiptError, ReceiptResult, SignedReceipt};
1917
use crate::{
20-
manager::adapters::EscrowHandler,
2118
receipt::{
2219
checks::ReceiptCheck,
23-
state::{AwaitingReserve, Checking, Failed, ReceiptState, Reserved},
20+
state::{Checked, Checking, Failed, ReceiptState},
2421
},
2522
signed_message::EIP712SignedMessage,
2623
};
@@ -50,30 +47,6 @@ where
5047
pub(crate) _state: S,
5148
}
5249

53-
impl ReceiptWithState<AwaitingReserve> {
54-
/// Perform the checks implemented by the context and reserve escrow if
55-
/// all checks pass
56-
///
57-
/// Returns a [`ReceiptWithState<Reserved>`] if successful, otherwise
58-
/// returns a [`ReceiptWithState<Failed>`]
59-
pub async fn check_and_reserve_escrow<E>(
60-
self,
61-
context: &E,
62-
domain_separator: &Eip712Domain,
63-
) -> ResultReceipt<Reserved>
64-
where
65-
E: EscrowHandler,
66-
{
67-
match context
68-
.check_and_reserve_escrow(&self, domain_separator)
69-
.await
70-
{
71-
Ok(_) => Ok(self.perform_state_changes(Reserved)),
72-
Err(e) => Err(self.perform_state_error(e)),
73-
}
74-
}
75-
}
76-
7750
impl ReceiptWithState<Checking> {
7851
/// Creates a new `ReceiptWithState` in the `Checking` state
7952
pub fn new(signed_receipt: SignedReceipt) -> ReceiptWithState<Checking> {
@@ -115,14 +88,14 @@ impl ReceiptWithState<Checking> {
11588
mut self,
11689
ctx: &Context,
11790
checks: &[ReceiptCheck],
118-
) -> Result<ResultReceipt<AwaitingReserve>, String> {
91+
) -> Result<ResultReceipt<Checked>, String> {
11992
let all_checks_passed = self.perform_checks(ctx, checks).await;
12093
if let Err(ReceiptError::RetryableCheck(e)) = all_checks_passed {
12194
Err(e.to_string())
12295
} else if let Err(e) = all_checks_passed {
12396
Ok(Err(self.perform_state_error(e)))
12497
} else {
125-
let checked = self.perform_state_changes(AwaitingReserve);
98+
let checked = self.perform_state_changes(Checked);
12699
Ok(Ok(checked))
127100
}
128101
}

tap_core/src/receipt/state.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,12 @@ pub struct Failed {
2121
pub error: ReceiptError,
2222
}
2323

24-
/// AwaitingReserve state represents a receipt that has passed all checks
25-
/// and is awaiting escrow reservation.
26-
#[derive(Debug, Clone)]
27-
pub struct AwaitingReserve;
28-
2924
/// Reserved state represents a receipt that has successfully reserved escrow.
3025
#[derive(Debug, Clone)]
31-
pub struct Reserved;
26+
pub struct Checked;
3227

3328
/// Trait for the different states a receipt can be in.
3429
pub trait ReceiptState {}
3530
impl ReceiptState for Checking {}
36-
impl ReceiptState for AwaitingReserve {}
37-
impl ReceiptState for Reserved {}
31+
impl ReceiptState for Checked {}
3832
impl ReceiptState for Failed {}

0 commit comments

Comments
 (0)