Skip to content

Commit 2e99fb3

Browse files
authored
refactor: Refactor error variants and usage of them (#112)
* refactor: error variants and usage of them Signed-off-by: Ozgur Akkurt <[email protected]> * refactor: Refactor error variants and usage of them Signed-off-by: Ozgur Akkurt <[email protected]> --------- Signed-off-by: Ozgur Akkurt <[email protected]>
1 parent 96cdf24 commit 2e99fb3

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

tap_aggregator/src/aggregator.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,19 @@ pub async fn check_and_aggregate_receipts(
3636
// Get the allocation id from the first receipt, return error if there are no receipts
3737
let allocation_id = match receipts.get(0) {
3838
Some(receipt) => receipt.message.allocation_id,
39-
None => {
40-
return Err(tap_core::Error::InvalidCheckError {
41-
check_string: "No receipts".into(),
42-
}
43-
.into())
44-
}
39+
None => return Err(tap_core::Error::NoValidReceiptsForRAVRequest.into()),
4540
};
4641

4742
// Check that the receipts all have the same allocation id
4843
check_allocation_id(receipts, allocation_id)?;
4944

5045
// Check that the rav has the correct allocation id
5146
if let Some(previous_rav) = &previous_rav {
52-
if previous_rav.message.allocation_id != allocation_id {
53-
return Err(tap_core::Error::InvalidCheckError {
54-
check_string: "Previous rav allocation id does not match receipts".into(),
47+
let prev_id = previous_rav.message.allocation_id;
48+
if prev_id != allocation_id {
49+
return Err(tap_core::Error::RavAllocationIdMismatch {
50+
prev_id: format!("{prev_id:#X}"),
51+
new_id: format!("{allocation_id:#X}"),
5552
}
5653
.into());
5754
}
@@ -71,10 +68,7 @@ fn check_allocation_id(
7168
for receipt in receipts.iter() {
7269
let receipt = &receipt.message;
7370
if receipt.allocation_id != allocation_id {
74-
return Err(tap_core::Error::InvalidCheckError {
75-
check_string: "Receipts allocation id is not uniform".into(),
76-
}
77-
.into());
71+
return Err(tap_core::Error::RavAllocationIdNotUniform.into());
7872
}
7973
}
8074
Ok(())
@@ -84,13 +78,9 @@ fn check_signatures_unique(receipts: &[EIP712SignedMessage<Receipt>]) -> Result<
8478
let mut receipt_signatures: hash_set::HashSet<Signature> = hash_set::HashSet::new();
8579
for receipt in receipts.iter() {
8680
let signature = receipt.signature;
87-
if receipt_signatures.contains(&signature) {
88-
return Err(tap_core::Error::InvalidCheckError {
89-
check_string: "Duplicate receipt signature".into(),
90-
}
91-
.into());
81+
if !receipt_signatures.insert(signature) {
82+
return Err(tap_core::Error::DuplicateReceiptSignature(signature.to_string()).into());
9283
}
93-
receipt_signatures.insert(signature);
9484
}
9585
Ok(())
9686
}
@@ -103,9 +93,9 @@ fn check_receipt_timestamps(
10393
for receipt in receipts.iter() {
10494
let receipt = &receipt.message;
10595
if previous_rav.message.timestamp_ns >= receipt.timestamp_ns {
106-
return Err(tap_core::Error::InvalidCheckError {
107-
check_string: "Receipt timestamp is less or equal then previous rav timestamp"
108-
.into(),
96+
return Err(tap_core::Error::ReceiptTimestampLowerThanRav {
97+
rav_ts: previous_rav.message.timestamp_ns,
98+
receipt_ts: receipt.timestamp_ns,
10999
}
110100
.into());
111101
}

tap_core/src/error.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum Error {
1717
#[error("Failed to encode to EIP712 hash:\n{source_error_message}")]
1818
EIP712EncodeError { source_error_message: String },
1919
#[error(
20-
"Unexpected check: {check_string}, only checks provided in initial checklist are valid"
20+
"Unexpected check: \"{check_string}\". Only checks provided in initial checklist are valid"
2121
)]
2222
InvalidCheckError { check_string: String },
2323
#[error("The requested action is invalid for current receipt state: {state}")]
@@ -39,6 +39,16 @@ pub enum Error {
3939
AdapterError { source_error_message: String },
4040
#[error("Failed to produce rav request, no valid receipts")]
4141
NoValidReceiptsForRAVRequest,
42+
#[error("Previous RAV allocation id ({prev_id}) doesn't match the allocation id from the new receipt ({new_id}).")]
43+
RavAllocationIdMismatch { prev_id: String, new_id: String },
44+
#[error("All receipts should have the same allocation id, but they don't")]
45+
RavAllocationIdNotUniform,
46+
#[error("Duplicate receipt signature: {0}")]
47+
DuplicateReceiptSignature(String),
48+
#[error(
49+
"Receipt timestamp ({receipt_ts}) is less or equal than previous rav timestamp ({rav_ts})"
50+
)]
51+
ReceiptTimestampLowerThanRav { rav_ts: u64, receipt_ts: u64 },
4252
#[error("Timestamp range error: min_timestamp_ns: {min_timestamp_ns}, max_timestamp_ns: {max_timestamp_ns}. Adjust timestamp buffer.")]
4353
TimestampRangeError {
4454
min_timestamp_ns: u64,

0 commit comments

Comments
 (0)