openzeppelin_relayer/repositories/
signer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! This module defines an in-memory repository for managing signer models.
//! It provides asynchronous CRUD operations and supports pagination.
//! The repository is thread-safe, using a `Mutex` to protect access to the underlying data store.
use crate::{
    models::{RepositoryError, SignerRepoModel},
    repositories::*,
};
use async_trait::async_trait;
use eyre::Result;
use std::collections::HashMap;
use tokio::sync::{Mutex, MutexGuard};

#[derive(Debug)]
pub struct InMemorySignerRepository {
    store: Mutex<HashMap<String, SignerRepoModel>>,
}

#[allow(dead_code)]
impl InMemorySignerRepository {
    pub fn new() -> Self {
        Self {
            store: Mutex::new(HashMap::new()),
        }
    }

    async fn acquire_lock<T>(lock: &Mutex<T>) -> Result<MutexGuard<T>, RepositoryError> {
        Ok(lock.lock().await)
    }
}

impl Default for InMemorySignerRepository {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Repository<SignerRepoModel, String> for InMemorySignerRepository {
    async fn create(&self, signer: SignerRepoModel) -> Result<SignerRepoModel, RepositoryError> {
        let mut store: MutexGuard<'_, HashMap<String, SignerRepoModel>> =
            Self::acquire_lock(&self.store).await?;
        if store.contains_key(&signer.id) {
            return Err(RepositoryError::ConstraintViolation(format!(
                "Signer with ID {} already exists",
                signer.id
            )));
        }
        store.insert(signer.id.clone(), signer.clone());
        Ok(signer)
    }

    async fn get_by_id(&self, id: String) -> Result<SignerRepoModel, RepositoryError> {
        let store: MutexGuard<'_, HashMap<String, SignerRepoModel>> =
            Self::acquire_lock(&self.store).await?;
        match store.get(&id) {
            Some(signer) => Ok(signer.clone()),
            None => Err(RepositoryError::NotFound(format!(
                "Signer with ID {} not found",
                id
            ))),
        }
    }

    #[allow(clippy::map_entry)]
    async fn update(
        &self,
        _id: String,
        _relayer: SignerRepoModel,
    ) -> Result<SignerRepoModel, RepositoryError> {
        Err(RepositoryError::NotSupported("Not supported".to_string()))
    }

    async fn delete_by_id(&self, _id: String) -> Result<(), RepositoryError> {
        Err(RepositoryError::NotSupported("Not supported".to_string()))
    }

    async fn list_all(&self) -> Result<Vec<SignerRepoModel>, RepositoryError> {
        let store: MutexGuard<'_, HashMap<String, SignerRepoModel>> =
            Self::acquire_lock(&self.store).await?;
        let signers: Vec<SignerRepoModel> = store.values().cloned().collect();
        Ok(signers)
    }

    async fn list_paginated(
        &self,
        query: PaginationQuery,
    ) -> Result<PaginatedResult<SignerRepoModel>, RepositoryError> {
        let total = self.count().await?;
        let start = ((query.page - 1) * query.per_page) as usize;
        let items: Vec<SignerRepoModel> = self
            .store
            .lock()
            .await
            .values()
            .skip(start)
            .take(query.per_page as usize)
            .cloned()
            .collect();

        Ok(PaginatedResult {
            items,
            total: total as u64,
            page: query.page,
            per_page: query.per_page,
        })
    }

    async fn count(&self) -> Result<usize, RepositoryError> {
        let store: MutexGuard<'_, HashMap<String, SignerRepoModel>> =
            Self::acquire_lock(&self.store).await?;
        let length = store.len();
        Ok(length)
    }
}

#[cfg(test)]
mod tests {
    use secrets::SecretVec;

    use crate::models::{LocalSignerConfig, SignerConfig};

    use super::*;

    fn create_test_signer(id: String) -> SignerRepoModel {
        SignerRepoModel {
            id: id.clone(),
            config: SignerConfig::Local(LocalSignerConfig {
                raw_key: SecretVec::zero(0),
            }),
        }
    }

    #[actix_web::test]
    async fn test_new_repository_is_empty() {
        let repo = InMemorySignerRepository::new();
        assert_eq!(repo.count().await.unwrap(), 0);
    }

    #[actix_web::test]
    async fn test_add_signer() {
        let repo = InMemorySignerRepository::new();
        let signer = create_test_signer("test".to_string());

        repo.create(signer.clone()).await.unwrap();
        assert_eq!(repo.count().await.unwrap(), 1);

        let stored = repo.get_by_id("test".to_string()).await.unwrap();
        assert_eq!(stored.id, signer.id);
    }

    #[actix_web::test]
    async fn test_update_signer() {
        let repo = InMemorySignerRepository::new();
        let signer = create_test_signer("test".to_string());

        let result = repo.update("test".to_string(), signer).await;
        assert!(matches!(result, Err(RepositoryError::NotSupported(_))));
    }

    #[actix_web::test]
    async fn test_list_signers() {
        let repo = InMemorySignerRepository::new();
        let signer1 = create_test_signer("test".to_string());
        let signer2 = create_test_signer("test2".to_string());

        repo.create(signer1.clone()).await.unwrap();
        repo.create(signer2).await.unwrap();

        let signers = repo.list_all().await.unwrap();
        assert_eq!(signers.len(), 2);
    }

    #[actix_web::test]
    async fn test_update_nonexistent_signer() {
        let repo = InMemorySignerRepository::new();
        let signer = create_test_signer("test".to_string());

        let result = repo.update("test2".to_string(), signer).await;
        assert!(matches!(result, Err(RepositoryError::NotSupported(_))));
    }

    #[actix_web::test]
    async fn test_get_nonexistent_relayer() {
        let repo = InMemorySignerRepository::new();

        let result = repo.get_by_id("test".to_string()).await;
        assert!(matches!(result, Err(RepositoryError::NotFound(_))));
    }
}