Skip to content

Commit 88041fd

Browse files
authored
Merge pull request #165 from semiotic-ai/result
refactor(core)!: make ChecksAdapter return Result
2 parents f079fc5 + 0983c27 commit 88041fd

File tree

4 files changed

+82
-33
lines changed

4 files changed

+82
-33
lines changed

tap_core/src/adapters/receipt_checks_adapter.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,40 @@ use ethereum_types::Address;
3030
3131
#[async_trait]
3232
pub trait ReceiptChecksAdapter {
33+
/// Defines the user-specified error type.
34+
///
35+
/// This error type should implement the `Error` and `Debug` traits from the standard library.
36+
/// Errors of this type are returned to the user when an operation fails.
37+
type AdapterError: std::error::Error + std::fmt::Debug + Send + Sync + 'static;
38+
3339
/// Checks if the given receipt is unique in the system.
3440
///
3541
/// This method should be implemented to verify the uniqueness of a given receipt in your system. Keep in mind that
3642
/// the receipt likely will be in storage when this check is performed so the receipt id should be used to check
3743
/// for uniqueness.
38-
async fn is_unique(&self, receipt: &EIP712SignedMessage<Receipt>, receipt_id: u64) -> bool;
44+
async fn is_unique(
45+
&self,
46+
receipt: &EIP712SignedMessage<Receipt>,
47+
receipt_id: u64,
48+
) -> Result<bool, Self::AdapterError>;
3949

4050
/// Verifies if the allocation ID is valid.
4151
///
4252
/// This method should be implemented to validate the given allocation ID is a valid allocation for the indexer. Valid is defined as
4353
/// an allocation ID that is owned by the indexer and still available for redeeming.
44-
async fn is_valid_allocation_id(&self, allocation_id: Address) -> bool;
54+
async fn is_valid_allocation_id(
55+
&self,
56+
allocation_id: Address,
57+
) -> Result<bool, Self::AdapterError>;
4558

4659
/// Confirms the value of the receipt is valid for the given query ID.
4760
///
4861
/// This method should be implemented to confirm the validity of the given value for a specific query ID.
49-
async fn is_valid_value(&self, value: u128, query_id: u64) -> bool;
62+
async fn is_valid_value(&self, value: u128, query_id: u64) -> Result<bool, Self::AdapterError>;
5063

5164
/// Confirms the gateway ID is valid.
5265
///
5366
/// This method should be implemented to validate the given gateway ID is one associated with a gateway the indexer considers valid.
5467
/// The provided gateway ID is the address of the gateway that is recovered from the signature of the receipt.
55-
async fn is_valid_gateway_id(&self, gateway_id: Address) -> bool;
68+
async fn is_valid_gateway_id(&self, gateway_id: Address) -> Result<bool, Self::AdapterError>;
5669
}

tap_core/src/adapters/test/receipt_checks_adapter_mock.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use std::{
88

99
use async_trait::async_trait;
1010
use ethereum_types::Address;
11+
use thiserror::Error;
1112
use tokio::sync::RwLock;
1213

1314
use crate::{
1415
adapters::receipt_checks_adapter::ReceiptChecksAdapter,
1516
eip_712_signed_message::EIP712SignedMessage,
16-
tap_receipt::{Receipt, ReceivedReceipt},
17+
tap_receipt::{Receipt, ReceiptError, ReceivedReceipt},
1718
};
1819

1920
pub struct ReceiptChecksAdapterMock {
@@ -23,6 +24,20 @@ pub struct ReceiptChecksAdapterMock {
2324
gateway_ids: Arc<RwLock<HashSet<Address>>>,
2425
}
2526

27+
#[derive(Debug, Error)]
28+
pub enum AdapterErrorMock {
29+
#[error("something went wrong: {error}")]
30+
AdapterError { error: String },
31+
}
32+
33+
impl From<AdapterErrorMock> for ReceiptError {
34+
fn from(val: AdapterErrorMock) -> Self {
35+
ReceiptError::CheckFailedToComplete {
36+
source_error_message: val.to_string(),
37+
}
38+
}
39+
}
40+
2641
impl ReceiptChecksAdapterMock {
2742
pub fn new(
2843
receipt_storage: Arc<RwLock<HashMap<u64, ReceivedReceipt>>>,
@@ -41,33 +56,42 @@ impl ReceiptChecksAdapterMock {
4156

4257
#[async_trait]
4358
impl ReceiptChecksAdapter for ReceiptChecksAdapterMock {
44-
async fn is_unique(&self, receipt: &EIP712SignedMessage<Receipt>, receipt_id: u64) -> bool {
59+
type AdapterError = AdapterErrorMock;
60+
61+
async fn is_unique(
62+
&self,
63+
receipt: &EIP712SignedMessage<Receipt>,
64+
receipt_id: u64,
65+
) -> Result<bool, Self::AdapterError> {
4566
let receipt_storage = self.receipt_storage.read().await;
46-
receipt_storage
67+
Ok(receipt_storage
4768
.iter()
4869
.all(|(stored_receipt_id, stored_receipt)| {
4970
(stored_receipt.signed_receipt.message != receipt.message)
5071
|| *stored_receipt_id == receipt_id
51-
})
72+
}))
5273
}
5374

54-
async fn is_valid_allocation_id(&self, allocation_id: Address) -> bool {
75+
async fn is_valid_allocation_id(
76+
&self,
77+
allocation_id: Address,
78+
) -> Result<bool, Self::AdapterError> {
5579
let allocation_ids = self.allocation_ids.read().await;
56-
allocation_ids.contains(&allocation_id)
80+
Ok(allocation_ids.contains(&allocation_id))
5781
}
5882

59-
async fn is_valid_value(&self, value: u128, query_id: u64) -> bool {
83+
async fn is_valid_value(&self, value: u128, query_id: u64) -> Result<bool, Self::AdapterError> {
6084
let query_appraisals = self.query_appraisals.read().await;
6185
let appraised_value = query_appraisals.get(&query_id).unwrap();
6286

6387
if value != *appraised_value {
64-
return false;
88+
return Ok(false);
6589
}
66-
true
90+
Ok(true)
6791
}
6892

69-
async fn is_valid_gateway_id(&self, gateway_id: Address) -> bool {
93+
async fn is_valid_gateway_id(&self, gateway_id: Address) -> Result<bool, Self::AdapterError> {
7094
let gateway_ids = self.gateway_ids.read().await;
71-
gateway_ids.contains(&gateway_id)
95+
Ok(gateway_ids.contains(&gateway_id))
7296
}
7397
}

tap_core/src/adapters/test/receipt_checks_adapter_test.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,22 @@ mod receipt_checks_adapter_unit_test {
9898
.await
9999
.insert(unique_receipt_id, new_receipt.1.clone());
100100

101-
assert!(
102-
receipt_checks_adapter
103-
.is_unique(&new_receipt.1.signed_receipt, unique_receipt_id)
104-
.await
105-
);
106-
assert!(
107-
receipt_checks_adapter
108-
.is_valid_allocation_id(new_receipt.1.signed_receipt.message.allocation_id)
109-
.await
110-
);
101+
assert!(receipt_checks_adapter
102+
.is_unique(&new_receipt.1.signed_receipt, unique_receipt_id)
103+
.await
104+
.unwrap());
105+
assert!(receipt_checks_adapter
106+
.is_valid_allocation_id(new_receipt.1.signed_receipt.message.allocation_id)
107+
.await
108+
.unwrap());
111109
// TODO: Add check when gateway_id is available from received receipt (issue: #56)
112110
// assert!(receipt_checks_adapter.is_valid_gateway_id(gateway_id));
113-
assert!(
114-
receipt_checks_adapter
115-
.is_valid_value(
116-
new_receipt.1.signed_receipt.message.value,
117-
new_receipt.1.query_id
118-
)
119-
.await
120-
);
111+
assert!(receipt_checks_adapter
112+
.is_valid_value(
113+
new_receipt.1.signed_receipt.message.value,
114+
new_receipt.1.query_id
115+
)
116+
.await
117+
.unwrap());
121118
}
122119
}

tap_core/src/tap_receipt/receipt_auditor.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
6363
.receipt_checks_adapter
6464
.is_unique(signed_receipt, receipt_id)
6565
.await
66+
.map_err(|e| ReceiptError::CheckFailedToComplete {
67+
source_error_message: e.to_string(),
68+
})?
6669
{
6770
return Err(ReceiptError::NonUniqueReceipt);
6871
}
@@ -77,6 +80,9 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
7780
.receipt_checks_adapter
7881
.is_valid_allocation_id(signed_receipt.message.allocation_id)
7982
.await
83+
.map_err(|e| ReceiptError::CheckFailedToComplete {
84+
source_error_message: e.to_string(),
85+
})?
8086
{
8187
return Err(ReceiptError::InvalidAllocationID {
8288
received_allocation_id: signed_receipt.message.allocation_id,
@@ -107,6 +113,9 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
107113
.receipt_checks_adapter
108114
.is_valid_value(signed_receipt.message.value, query_id)
109115
.await
116+
.map_err(|e| ReceiptError::CheckFailedToComplete {
117+
source_error_message: e.to_string(),
118+
})?
110119
{
111120
return Err(ReceiptError::InvalidValue {
112121
received_value: signed_receipt.message.value,
@@ -129,6 +138,9 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
129138
.receipt_checks_adapter
130139
.is_valid_gateway_id(receipt_signer_address)
131140
.await
141+
.map_err(|e| ReceiptError::CheckFailedToComplete {
142+
source_error_message: e.to_string(),
143+
})?
132144
{
133145
return Err(ReceiptError::InvalidSignature {
134146
source_error_message: format!(
@@ -171,6 +183,9 @@ impl<EA: EscrowAdapter, RCA: ReceiptChecksAdapter> ReceiptAuditor<EA, RCA> {
171183
.receipt_checks_adapter
172184
.is_valid_gateway_id(rav_signer_address)
173185
.await
186+
.map_err(|err| Error::AdapterError {
187+
source_error: anyhow::Error::new(err),
188+
})?
174189
{
175190
return Err(Error::InvalidRecoveredSigner {
176191
address: rav_signer_address,

0 commit comments

Comments
 (0)