openzeppelin_relayer/services/transaction_counter/
mod.rs

1//! This module provides a service for managing transaction counters.
2//!
3//! The `TransactionCounterService` struct offers methods to get, increment,
4//! decrement, and set transaction counts associated with a specific relayer
5//! and address. It uses an in-memory store to keep track of these counts.
6use std::sync::Arc;
7
8use crate::repositories::{TransactionCounterError, TransactionCounterTrait};
9use async_trait::async_trait;
10
11#[cfg(test)]
12use mockall::automock;
13
14#[derive(Clone)]
15pub struct TransactionCounterService<T: TransactionCounterTrait + Send + Sync> {
16    relayer_id: String,
17    address: String,
18    store: Arc<T>,
19}
20
21impl<T: TransactionCounterTrait + Send + Sync> TransactionCounterService<T> {
22    pub fn new(relayer_id: String, address: String, store: Arc<T>) -> Self {
23        Self {
24            relayer_id,
25            address,
26            store,
27        }
28    }
29}
30
31#[async_trait]
32#[cfg_attr(test, automock)]
33pub trait TransactionCounterServiceTrait: Send + Sync {
34    async fn get(&self) -> Result<Option<u64>, TransactionCounterError>;
35    async fn get_and_increment(&self) -> Result<u64, TransactionCounterError>;
36    async fn decrement(&self) -> Result<u64, TransactionCounterError>;
37    async fn set(&self, value: u64) -> Result<(), TransactionCounterError>;
38}
39
40#[async_trait]
41#[allow(dead_code)]
42impl<T: TransactionCounterTrait + Send + Sync> TransactionCounterServiceTrait
43    for TransactionCounterService<T>
44{
45    async fn get(&self) -> Result<Option<u64>, TransactionCounterError> {
46        self.store.get(&self.relayer_id, &self.address)
47    }
48
49    async fn get_and_increment(&self) -> Result<u64, TransactionCounterError> {
50        self.store
51            .get_and_increment(&self.relayer_id, &self.address)
52    }
53
54    async fn decrement(&self) -> Result<u64, TransactionCounterError> {
55        self.store.decrement(&self.relayer_id, &self.address)
56    }
57
58    async fn set(&self, value: u64) -> Result<(), TransactionCounterError> {
59        self.store.set(&self.relayer_id, &self.address, value)
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use crate::repositories::InMemoryTransactionCounter;
67
68    #[tokio::test]
69    async fn test_transaction_counter() {
70        let store = Arc::new(InMemoryTransactionCounter::default());
71        let service =
72            TransactionCounterService::new("relayer_id".to_string(), "address".to_string(), store);
73
74        assert_eq!(service.get().await.unwrap(), None);
75        assert_eq!(service.get_and_increment().await.unwrap(), 0);
76        assert_eq!(service.get_and_increment().await.unwrap(), 1);
77        assert_eq!(service.decrement().await.unwrap(), 1);
78        assert!(service.set(10).await.is_ok());
79        assert_eq!(service.get().await.unwrap(), Some(10));
80    }
81}