|
1 | 1 | use std::sync::Arc;
|
2 | 2 |
|
| 3 | +use async_trait::async_trait; |
| 4 | +use chrono::Utc; |
3 | 5 | use sqlite::{Connection, Value};
|
4 | 6 |
|
5 | 7 | use mithril_common::sqlite::{
|
@@ -216,18 +218,71 @@ impl<'conn> Provider<'conn> for UpdateSignerRecordProvider<'conn> {
|
216 | 218 | }
|
217 | 219 | }
|
218 | 220 |
|
| 221 | +/// Signer recorder trait |
| 222 | +#[async_trait] |
| 223 | +pub trait SignerRecorder { |
| 224 | + /// Record signer_id |
| 225 | + async fn record_signer_id(&self, signer_id: String) -> Result<(), StdError>; |
| 226 | + |
| 227 | + /// Record pool ticker by id |
| 228 | + async fn record_signer_pool_ticker( |
| 229 | + &self, |
| 230 | + signer_id: String, |
| 231 | + pool_ticker: Option<String>, |
| 232 | + ) -> Result<(), StdError>; |
| 233 | +} |
| 234 | + |
219 | 235 | /// Service to deal with signer (read & write).
|
220 |
| -pub struct SignerStoreAdapter { |
| 236 | +pub struct SignerStore { |
221 | 237 | connection: Arc<Mutex<Connection>>,
|
222 | 238 | }
|
223 | 239 |
|
224 |
| -impl SignerStoreAdapter { |
225 |
| - /// Create a new SignerStoreAdapter service |
| 240 | +impl SignerStore { |
| 241 | + /// Create a new SignerStore service |
226 | 242 | pub fn new(connection: Arc<Mutex<Connection>>) -> Self {
|
227 | 243 | Self { connection }
|
228 | 244 | }
|
229 | 245 | }
|
230 | 246 |
|
| 247 | +#[async_trait] |
| 248 | +impl SignerRecorder for SignerStore { |
| 249 | + async fn record_signer_id(&self, signer_id: String) -> Result<(), StdError> { |
| 250 | + let connection = &*self.connection.lock().await; |
| 251 | + let provider = InsertSignerRecordProvider::new(connection); |
| 252 | + let created_at = format!("{:?}", Utc::now()); |
| 253 | + let updated_at = created_at.clone(); |
| 254 | + let signer_record = SignerRecord { |
| 255 | + signer_id, |
| 256 | + pool_ticker: None, |
| 257 | + created_at, |
| 258 | + updated_at, |
| 259 | + }; |
| 260 | + provider.persist(signer_record)?; |
| 261 | + |
| 262 | + Ok(()) |
| 263 | + } |
| 264 | + |
| 265 | + async fn record_signer_pool_ticker( |
| 266 | + &self, |
| 267 | + signer_id: String, |
| 268 | + pool_ticker: Option<String>, |
| 269 | + ) -> Result<(), StdError> { |
| 270 | + let connection = &*self.connection.lock().await; |
| 271 | + let provider = UpdateSignerRecordProvider::new(connection); |
| 272 | + let created_at = format!("{:?}", Utc::now()); |
| 273 | + let updated_at = created_at.clone(); |
| 274 | + let signer_record = SignerRecord { |
| 275 | + signer_id, |
| 276 | + pool_ticker, |
| 277 | + created_at, |
| 278 | + updated_at, |
| 279 | + }; |
| 280 | + provider.persist(signer_record)?; |
| 281 | + |
| 282 | + Ok(()) |
| 283 | + } |
| 284 | +} |
| 285 | + |
231 | 286 | #[cfg(test)]
|
232 | 287 | mod tests {
|
233 | 288 |
|
@@ -443,4 +498,44 @@ mod tests {
|
443 | 498 | assert_eq!(signer_record_copy, signer_record_saved);
|
444 | 499 | }
|
445 | 500 | }
|
| 501 | + |
| 502 | + #[tokio::test] |
| 503 | + async fn test_signer_recorder() { |
| 504 | + let signer_records_fake = fake_signer_records(5); |
| 505 | + |
| 506 | + let connection = Connection::open(":memory:").unwrap(); |
| 507 | + setup_signer_db(&connection, Vec::new()).unwrap(); |
| 508 | + |
| 509 | + let connection = Arc::new(Mutex::new(connection)); |
| 510 | + let store_recorder = SignerStore::new(connection.clone()); |
| 511 | + |
| 512 | + for signer_record in signer_records_fake.clone() { |
| 513 | + store_recorder |
| 514 | + .record_signer_id(signer_record.signer_id.clone()) |
| 515 | + .await |
| 516 | + .expect("record_signer_id should not fail"); |
| 517 | + let connection = &*connection.lock().await; |
| 518 | + let provider = SignerRecordProvider::new(connection); |
| 519 | + let signer_records_stored: Vec<SignerRecord> = provider |
| 520 | + .get_by_signer_id(signer_record.signer_id) |
| 521 | + .unwrap() |
| 522 | + .collect::<Vec<_>>(); |
| 523 | + assert_eq!(1, signer_records_stored.len()); |
| 524 | + } |
| 525 | + |
| 526 | + for signer_record in signer_records_fake { |
| 527 | + let pool_ticker = Some(format!("new-pool-{}", signer_record.signer_id)); |
| 528 | + store_recorder |
| 529 | + .record_signer_pool_ticker(signer_record.signer_id.clone(), pool_ticker.clone()) |
| 530 | + .await |
| 531 | + .expect("record_signer_pool_ticker should not fail"); |
| 532 | + let connection = &*connection.lock().await; |
| 533 | + let provider = SignerRecordProvider::new(connection); |
| 534 | + let signer_records_stored: Vec<SignerRecord> = provider |
| 535 | + .get_by_signer_id(signer_record.signer_id) |
| 536 | + .unwrap() |
| 537 | + .collect::<Vec<_>>(); |
| 538 | + assert_eq!(pool_ticker, signer_records_stored[0].to_owned().pool_ticker); |
| 539 | + } |
| 540 | + } |
446 | 541 | }
|
0 commit comments