Skip to content

Commit

Permalink
refactor!: use typestate for receivedreceipt
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <gustavo@semiotic.ai>
  • Loading branch information
gusinacio committed Dec 19, 2023
1 parent e9770f9 commit 43d615d
Show file tree
Hide file tree
Showing 12 changed files with 460 additions and 433 deletions.
2 changes: 1 addition & 1 deletion tap_core/src/adapters/mock/receipt_checks_adapter_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl ReceiptChecksAdapter for ReceiptChecksAdapterMock {
Ok(receipt_storage
.iter()
.all(|(stored_receipt_id, stored_receipt)| {
(stored_receipt.signed_receipt.message != receipt.message)
(stored_receipt.signed_receipt().message != receipt.message)
|| *stored_receipt_id == receipt_id
}))
}
Expand Down
18 changes: 10 additions & 8 deletions tap_core/src/adapters/mock/receipt_storage_adapter_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use async_trait::async_trait;
use tokio::sync::RwLock;

use crate::{
adapters::receipt_storage_adapter::{safe_truncate_receipts, ReceiptRead, ReceiptStore},
adapters::receipt_storage_adapter::{
safe_truncate_receipts, ReceiptRead, ReceiptStore, StoredReceipt,
},
tap_receipt::ReceivedReceipt,
};

Expand Down Expand Up @@ -44,15 +46,15 @@ impl ReceiptStorageAdapterMock {
Ok(receipt_storage
.iter()
.filter(|(_, rx_receipt)| {
rx_receipt.signed_receipt.message.timestamp_ns == timestamp_ns
rx_receipt.signed_receipt().message.timestamp_ns == timestamp_ns
})
.map(|(&id, rx_receipt)| (id, rx_receipt.clone()))
.collect())
}
pub async fn retrieve_receipts_upto_timestamp(
&self,
timestamp_ns: u64,
) -> Result<Vec<(u64, ReceivedReceipt)>, AdapterErrorMock> {
) -> Result<Vec<StoredReceipt>, AdapterErrorMock> {
self.retrieve_receipts_in_timestamp_range(..=timestamp_ns, None)
.await
}
Expand Down Expand Up @@ -117,7 +119,7 @@ impl ReceiptStore for ReceiptStorageAdapterMock {
) -> Result<(), Self::AdapterError> {
let mut receipt_storage = self.receipt_storage.write().await;
receipt_storage.retain(|_, rx_receipt| {
!timestamp_ns.contains(&rx_receipt.signed_receipt.message.timestamp_ns)
!timestamp_ns.contains(&rx_receipt.signed_receipt().message.timestamp_ns)
});
Ok(())
}
Expand All @@ -130,22 +132,22 @@ impl ReceiptRead for ReceiptStorageAdapterMock {
&self,
timestamp_range_ns: R,
limit: Option<u64>,
) -> Result<Vec<(u64, ReceivedReceipt)>, Self::AdapterError> {
) -> Result<Vec<StoredReceipt>, Self::AdapterError> {
let receipt_storage = self.receipt_storage.read().await;
let mut receipts_in_range: Vec<(u64, ReceivedReceipt)> = receipt_storage
.iter()
.filter(|(_, rx_receipt)| {
timestamp_range_ns.contains(&rx_receipt.signed_receipt.message.timestamp_ns)
timestamp_range_ns.contains(&rx_receipt.signed_receipt().message.timestamp_ns)
})
.map(|(&id, rx_receipt)| (id, rx_receipt.clone()))
.collect();

if limit.is_some_and(|limit| receipts_in_range.len() > limit as usize) {
safe_truncate_receipts(&mut receipts_in_range, limit.unwrap());

Ok(receipts_in_range)
Ok(receipts_in_range.into_iter().map(|r| r.into()).collect())
} else {
Ok(receipts_in_range)
Ok(receipts_in_range.into_iter().map(|r| r.into()).collect())
}
}
}
24 changes: 19 additions & 5 deletions tap_core/src/adapters/receipt_storage_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,22 @@ pub trait ReceiptRead {
&self,
timestamp_range_ns: R,
limit: Option<u64>,
) -> Result<Vec<(u64, ReceivedReceipt)>, Self::AdapterError>;
) -> Result<Vec<StoredReceipt>, Self::AdapterError>;
}

pub struct StoredReceipt {
pub receipt_id: u64,
pub receipt: ReceivedReceipt,
}

impl From<(u64, ReceivedReceipt)> for StoredReceipt {
fn from((receipt_id, receipt): (u64, ReceivedReceipt)) -> Self {
Self {
receipt_id,
receipt,
}
}
}
/// See [`ReceiptStorageAdapter::retrieve_receipts_in_timestamp_range()`] for details.
///
/// WARNING: Will sort the receipts by timestamp using
Expand All @@ -130,18 +143,19 @@ pub fn safe_truncate_receipts(receipts: &mut Vec<(u64, ReceivedReceipt)>, limit:
return;
}

receipts.sort_unstable_by_key(|(_, rx_receipt)| rx_receipt.signed_receipt.message.timestamp_ns);
receipts
.sort_unstable_by_key(|(_, rx_receipt)| rx_receipt.signed_receipt().message.timestamp_ns);

// This one will be the last timestamp in `receipts` after naive truncation
let last_timestamp = receipts[limit as usize - 1]
.1
.signed_receipt
.signed_receipt()
.message
.timestamp_ns;
// This one is the timestamp that comes just after the one above
let after_last_timestamp = receipts[limit as usize]
.1
.signed_receipt
.signed_receipt()
.message
.timestamp_ns;

Expand All @@ -152,7 +166,7 @@ pub fn safe_truncate_receipts(receipts: &mut Vec<(u64, ReceivedReceipt)>, limit:
// remove all the receipts with the same timestamp as the last one, because
// otherwise we would leave behind part of the receipts for that timestamp.
receipts.retain(|(_, rx_receipt)| {
rx_receipt.signed_receipt.message.timestamp_ns != last_timestamp
rx_receipt.signed_receipt().message.timestamp_ns != last_timestamp
});
}
}
10 changes: 5 additions & 5 deletions tap_core/src/adapters/test/receipt_checks_adapter_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod receipt_checks_adapter_unit_test {
receipt_checks_adapter_mock::ReceiptChecksAdapterMock,
},
eip_712_signed_message::EIP712SignedMessage,
tap_receipt::{get_full_list_of_checks, Receipt, ReceivedReceipt},
tap_receipt::{get_full_list_of_checks, Receipt, ReceiptWithState, ReceivedReceipt},
};

#[fixture]
Expand Down Expand Up @@ -65,7 +65,7 @@ mod receipt_checks_adapter_unit_test {
async move {
(
id,
ReceivedReceipt::new(
ReceiptWithState::new(
EIP712SignedMessage::new(
domain_separator,
Receipt::new(allocation_ids[0], value).unwrap(),
Expand All @@ -75,7 +75,7 @@ mod receipt_checks_adapter_unit_test {
.unwrap(),
id,
&get_full_list_of_checks(),
),
).into_stateful(),
)
}
})
Expand All @@ -96,7 +96,7 @@ mod receipt_checks_adapter_unit_test {

let new_receipt = (
10u64,
ReceivedReceipt::new(
ReceiptWithState::new(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_ids[0], value).unwrap(),
Expand All @@ -113,7 +113,7 @@ mod receipt_checks_adapter_unit_test {
receipt_storage
.write()
.await
.insert(unique_receipt_id, new_receipt.1.clone());
.insert(unique_receipt_id, new_receipt.1.clone().into_stateful());

assert!(receipt_checks_adapter
.is_unique(&new_receipt.1.signed_receipt, unique_receipt_id)
Expand Down
22 changes: 13 additions & 9 deletions tap_core/src/adapters/test/receipt_storage_adapter_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ mod receipt_storage_adapter_unit_test {
receipt_storage_adapter::ReceiptStore,
receipt_storage_adapter_mock::ReceiptStorageAdapterMock,
};
use crate::tap_receipt::ReceivedReceipt;
use crate::{
eip_712_signed_message::EIP712SignedMessage, tap_receipt::get_full_list_of_checks,
tap_receipt::Receipt, tap_receipt::ReceivedReceipt,
tap_receipt::Receipt, tap_receipt::ReceiptWithState,
};

#[fixture]
Expand Down Expand Up @@ -52,7 +53,7 @@ mod receipt_storage_adapter_unit_test {
// Create receipts
let query_id = 10u64;
let value = 100u128;
let received_receipt = ReceivedReceipt::new(
let received_receipt = ReceiptWithState::new(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, value).unwrap(),
Expand All @@ -64,7 +65,9 @@ mod receipt_storage_adapter_unit_test {
&get_full_list_of_checks(),
);

let receipt_store_result = receipt_adapter.store_receipt(received_receipt).await;
let receipt_store_result = receipt_adapter
.store_receipt(received_receipt.into_stateful())
.await;
assert!(receipt_store_result.is_ok());
let receipt_id = receipt_store_result.unwrap();

Expand Down Expand Up @@ -114,7 +117,7 @@ mod receipt_storage_adapter_unit_test {
// Create receipts
let mut received_receipts = Vec::new();
for (query_id, value) in (50..60).enumerate() {
received_receipts.push(ReceivedReceipt::new(
received_receipts.push(ReceiptWithState::new(
EIP712SignedMessage::new(
&domain_separator,
Receipt::new(allocation_id, value).unwrap(),
Expand All @@ -131,7 +134,7 @@ mod receipt_storage_adapter_unit_test {
for received_receipt in received_receipts {
receipt_ids.push(
receipt_adapter
.store_receipt(received_receipt.clone())
.store_receipt(received_receipt.clone().into_stateful())
.await
.unwrap(),
);
Expand Down Expand Up @@ -207,7 +210,7 @@ mod receipt_storage_adapter_unit_test {
// The contents of the receipt only need to be unique for this test (so we can check)
receipts_orig.push((
i as u64,
ReceivedReceipt::new(
ReceiptWithState::new(
EIP712SignedMessage::new(
&domain_separator,
Receipt {
Expand All @@ -222,7 +225,8 @@ mod receipt_storage_adapter_unit_test {
.unwrap(),
i as u64, // Will use that to check the IDs
&get_full_list_of_checks(),
),
)
.into_stateful(),
));
}

Expand All @@ -241,12 +245,12 @@ mod receipt_storage_adapter_unit_test {
for (elem_trun, expected_timestamp) in receipts_truncated.iter().zip(expected.iter()) {
// Check timestamps
assert_eq!(
elem_trun.1.signed_receipt.message.timestamp_ns,
elem_trun.1.signed_receipt().message.timestamp_ns,
*expected_timestamp
);

// Check that the IDs are fine
assert_eq!(elem_trun.0, elem_trun.1.query_id);
assert_eq!(elem_trun.0, elem_trun.1.query_id());
}
}
}
Loading

0 comments on commit 43d615d

Please sign in to comment.