From b77355274e0fa83aac87edbbab7bba8a59e699ac Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Fri, 16 Aug 2024 10:06:27 -0300 Subject: [PATCH] test: add recoverable check test Signed-off-by: Gustavo Inacio --- tap_core/tests/manager_test.rs | 84 ++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/tap_core/tests/manager_test.rs b/tap_core/tests/manager_test.rs index 397dea4..516d382 100644 --- a/tap_core/tests/manager_test.rs +++ b/tap_core/tests/manager_test.rs @@ -3,11 +3,12 @@ use std::{ collections::HashMap, str::FromStr, - sync::{Arc, RwLock}, + sync::{atomic::AtomicBool, Arc, RwLock}, time::{SystemTime, UNIX_EPOCH}, }; use alloy::{dyn_abi::Eip712Domain, primitives::Address, signers::local::PrivateKeySigner}; +use anyhow::anyhow; use rstest::*; fn get_current_timestamp_u64_ns() -> anyhow::Result { @@ -24,8 +25,9 @@ use tap_core::{ }, rav::ReceiptAggregateVoucher, receipt::{ - checks::{CheckList, StatefulTimestampCheck}, - Receipt, + checks::{Check, CheckError, CheckList, StatefulTimestampCheck}, + state::Checking, + Receipt, ReceiptWithState, }, signed_message::EIP712SignedMessage, tap_eip712_domain, @@ -530,3 +532,79 @@ async fn manager_create_rav_and_ignore_invalid_receipts( //Rav Value corresponds only to value of one receipt assert_eq!(expected_rav.valueAggregate, 20); } + +#[rstest] +#[tokio::test] +async fn test_recoverable_checks( + allocation_ids: Vec
, + domain_separator: Eip712Domain, + context: ContextFixture, +) { + struct RecoverableCheck(Arc); + + #[async_trait::async_trait] + impl Check for RecoverableCheck { + async fn check(&self, receipt: &ReceiptWithState) -> Result<(), CheckError> { + // we want to fail only if nonce is 5 and if is create rav step + if self.0.load(std::sync::atomic::Ordering::SeqCst) + && receipt.signed_receipt().message.nonce == 5 + { + Err(CheckError::Recoverable(anyhow!("Recoverable error"))) + } else { + Ok(()) + } + } + } + + let ContextFixture { + context, + checks, + escrow_storage, + signer, + .. + } = context; + + let is_create_rav = Arc::new(AtomicBool::new(false)); + + let mut checks: Vec> = checks.iter().cloned().collect(); + checks.push(Arc::new(RecoverableCheck(is_create_rav.clone()))); + + let manager = Manager::new( + domain_separator.clone(), + context.clone(), + CheckList::new(checks), + ); + + escrow_storage + .write() + .unwrap() + .insert(signer.address(), 999999); + + let mut stored_signed_receipts = Vec::new(); + for i in 0..10 { + let receipt = Receipt { + allocation_id: allocation_ids[0], + timestamp_ns: i + 1, + nonce: i, + value: 20u128, + }; + let signed_receipt = EIP712SignedMessage::new(&domain_separator, receipt, &signer).unwrap(); + stored_signed_receipts.push(signed_receipt.clone()); + manager + .verify_and_store_receipt(signed_receipt) + .await + .unwrap(); + } + + is_create_rav.store(true, std::sync::atomic::Ordering::SeqCst); + + let rav_request = manager.create_rav_request(0, None).await; + + assert_eq!( + rav_request.expect_err("Didn't fail").to_string(), + tap_core::Error::ReceiptError(tap_core::receipt::ReceiptError::RecoverableCheckError( + "Recoverable error".to_string() + )) + .to_string() + ); +}