|
1 | 1 | use async_trait::async_trait;
|
2 | 2 | use chrono::{DateTime, Utc};
|
3 | 3 | use sqlite::{Connection, Value};
|
4 |
| -use std::{ |
5 |
| - collections::{BTreeMap, HashMap}, |
6 |
| - sync::Arc, |
7 |
| -}; |
| 4 | +use std::{collections::HashMap, sync::Arc}; |
8 | 5 | use tokio::sync::Mutex;
|
9 | 6 |
|
10 |
| -use crate::VerificationKeyStorer; |
11 |
| -use mithril_common::entities::Signer; |
12 |
| -use mithril_common::store::StoreError; |
13 | 7 | use mithril_common::{
|
14 | 8 | crypto_helper::KESPeriod,
|
15 | 9 | entities::{
|
16 | 10 | Epoch, HexEncodedOpCert, HexEncodedVerificationKey, HexEncodedVerificationKeySignature,
|
17 |
| - PartyId, SignerWithStake, Stake, |
| 11 | + PartyId, Signer, SignerWithStake, Stake, |
18 | 12 | },
|
19 | 13 | sqlite::{
|
20 | 14 | EntityCursor, HydrationError, Projection, Provider, SourceAlias, SqLiteEntity,
|
21 | 15 | WhereCondition,
|
22 | 16 | },
|
23 |
| - store::adapter::{AdapterError, StoreAdapter}, |
| 17 | + store::{adapter::AdapterError, StoreError}, |
24 | 18 | StdError,
|
25 | 19 | };
|
26 | 20 |
|
| 21 | +use crate::VerificationKeyStorer; |
| 22 | + |
27 | 23 | /// SignerRegistration record is the representation of a stored signer_registration.
|
28 | 24 | #[derive(Debug, PartialEq, Clone)]
|
29 | 25 | pub struct SignerRegistrationRecord {
|
@@ -469,141 +465,9 @@ impl VerificationKeyStorer for SignerRegistrationStore {
|
469 | 465 | }
|
470 | 466 | }
|
471 | 467 |
|
472 |
| -/// Service to deal with signer_registration (read & write). |
473 |
| -pub struct SignerRegistrationStoreAdapter { |
474 |
| - connection: Arc<Mutex<Connection>>, |
475 |
| -} |
476 |
| - |
477 |
| -impl SignerRegistrationStoreAdapter { |
478 |
| - /// Create a new SignerRegistrationStoreAdapter service |
479 |
| - pub fn new(connection: Arc<Mutex<Connection>>) -> Self { |
480 |
| - Self { connection } |
481 |
| - } |
482 |
| -} |
483 |
| - |
484 |
| -#[async_trait] |
485 |
| -impl StoreAdapter for SignerRegistrationStoreAdapter { |
486 |
| - type Key = Epoch; |
487 |
| - type Record = HashMap<PartyId, SignerWithStake>; |
488 |
| - |
489 |
| - async fn store_record( |
490 |
| - &mut self, |
491 |
| - key: &Self::Key, |
492 |
| - record: &Self::Record, |
493 |
| - ) -> Result<(), AdapterError> { |
494 |
| - let connection = &*self.connection.lock().await; |
495 |
| - let provider = InsertOrReplaceSignerRegistrationRecordProvider::new(connection); |
496 |
| - connection |
497 |
| - .execute("begin transaction") |
498 |
| - .map_err(|e| AdapterError::QueryError(e.into()))?; |
499 |
| - |
500 |
| - for signer_with_stake in record.values() { |
501 |
| - let _signer_registration_record = provider |
502 |
| - .persist(SignerRegistrationRecord::from_signer_with_stake( |
503 |
| - signer_with_stake.to_owned(), |
504 |
| - *key, |
505 |
| - )) |
506 |
| - .map_err(|e| AdapterError::GeneralError(format!("{e}")))?; |
507 |
| - } |
508 |
| - |
509 |
| - connection |
510 |
| - .execute("commit transaction") |
511 |
| - .map_err(|e| AdapterError::QueryError(e.into()))?; |
512 |
| - |
513 |
| - Ok(()) |
514 |
| - } |
515 |
| - |
516 |
| - async fn get_record(&self, key: &Self::Key) -> Result<Option<Self::Record>, AdapterError> { |
517 |
| - let connection = &*self.connection.lock().await; |
518 |
| - let provider = SignerRegistrationRecordProvider::new(connection); |
519 |
| - let cursor = provider |
520 |
| - .get_by_epoch(key) |
521 |
| - .map_err(|e| AdapterError::GeneralError(format!("{e}")))?; |
522 |
| - let mut signer_with_stakes = HashMap::new(); |
523 |
| - for signer_registration_record in cursor { |
524 |
| - signer_with_stakes.insert( |
525 |
| - signer_registration_record.signer_id.to_string(), |
526 |
| - signer_registration_record.into(), |
527 |
| - ); |
528 |
| - } |
529 |
| - if signer_with_stakes.is_empty() { |
530 |
| - Ok(None) |
531 |
| - } else { |
532 |
| - Ok(Some(signer_with_stakes)) |
533 |
| - } |
534 |
| - } |
535 |
| - |
536 |
| - async fn record_exists(&self, key: &Self::Key) -> Result<bool, AdapterError> { |
537 |
| - Ok(self.get_record(key).await?.is_some()) |
538 |
| - } |
539 |
| - |
540 |
| - async fn get_last_n_records( |
541 |
| - &self, |
542 |
| - how_many: usize, |
543 |
| - ) -> Result<Vec<(Self::Key, Self::Record)>, AdapterError> { |
544 |
| - let connection = &*self.connection.lock().await; |
545 |
| - let provider = SignerRegistrationRecordProvider::new(connection); |
546 |
| - let cursor = provider |
547 |
| - .get_all() |
548 |
| - .map_err(|e| AdapterError::GeneralError(format!("{e}")))? |
549 |
| - .collect::<Vec<_>>() |
550 |
| - .into_iter() |
551 |
| - .rev(); |
552 |
| - let signer_with_stake_by_epoch: BTreeMap<Self::Key, Self::Record> = cursor.fold( |
553 |
| - BTreeMap::<Self::Key, Self::Record>::new(), |
554 |
| - |mut acc, signer_registration_record| { |
555 |
| - let epoch = signer_registration_record.epoch_setting_id; |
556 |
| - let mut signer_with_stakes: Self::Record = |
557 |
| - if let Some(signer_with_stakes) = acc.get_mut(&epoch) { |
558 |
| - signer_with_stakes.to_owned() |
559 |
| - } else { |
560 |
| - HashMap::new() |
561 |
| - }; |
562 |
| - signer_with_stakes.insert( |
563 |
| - signer_registration_record.signer_id.to_string(), |
564 |
| - signer_registration_record.into(), |
565 |
| - ); |
566 |
| - acc.insert(epoch, signer_with_stakes); |
567 |
| - acc |
568 |
| - }, |
569 |
| - ); |
570 |
| - Ok(signer_with_stake_by_epoch |
571 |
| - .into_iter() |
572 |
| - .rev() |
573 |
| - .take(how_many) |
574 |
| - .collect()) |
575 |
| - } |
576 |
| - |
577 |
| - async fn remove(&mut self, key: &Self::Key) -> Result<Option<Self::Record>, AdapterError> { |
578 |
| - let connection = &*self.connection.lock().await; |
579 |
| - let provider = DeleteSignerRegistrationRecordProvider::new(connection); |
580 |
| - let cursor = provider |
581 |
| - .delete(*key) |
582 |
| - .map_err(|e| AdapterError::GeneralError(format!("{e}")))?; |
583 |
| - let mut signer_with_stakes = HashMap::new(); |
584 |
| - for signer_registration_record in cursor { |
585 |
| - signer_with_stakes.insert( |
586 |
| - signer_registration_record.signer_id.to_string(), |
587 |
| - signer_registration_record.into(), |
588 |
| - ); |
589 |
| - } |
590 |
| - |
591 |
| - if signer_with_stakes.is_empty() { |
592 |
| - Ok(None) |
593 |
| - } else { |
594 |
| - Ok(Some(signer_with_stakes)) |
595 |
| - } |
596 |
| - } |
597 |
| - |
598 |
| - async fn get_iter(&self) -> Result<Box<dyn Iterator<Item = Self::Record> + '_>, AdapterError> { |
599 |
| - let records = self.get_last_n_records(usize::MAX).await?; |
600 |
| - Ok(Box::new(records.into_iter().map(|(_k, v)| v))) |
601 |
| - } |
602 |
| -} |
603 |
| - |
604 | 468 | #[cfg(test)]
|
605 | 469 | mod tests {
|
606 |
| - use std::collections::{BTreeMap, HashMap}; |
| 470 | + use std::collections::HashMap; |
607 | 471 |
|
608 | 472 | use mithril_common::test_utils::MithrilFixtureBuilder;
|
609 | 473 |
|
@@ -939,66 +803,6 @@ mod tests {
|
939 | 803 | }
|
940 | 804 | }
|
941 | 805 |
|
942 |
| - #[tokio::test] |
943 |
| - async fn test_store_adapter() { |
944 |
| - let fixture = MithrilFixtureBuilder::default().with_signers(5).build(); |
945 |
| - let signer_with_stakes = fixture.signers_with_stake(); |
946 |
| - let signer_with_stakes_by_epoch: Vec<(Epoch, HashMap<PartyId, SignerWithStake>)> = (0..5) |
947 |
| - .map(|e| { |
948 |
| - ( |
949 |
| - Epoch(e), |
950 |
| - signer_with_stakes |
951 |
| - .clone() |
952 |
| - .into_iter() |
953 |
| - .map(|s| (s.party_id.to_owned(), s)) |
954 |
| - .collect(), |
955 |
| - ) |
956 |
| - }) |
957 |
| - .collect(); |
958 |
| - |
959 |
| - let connection = Connection::open(":memory:").unwrap(); |
960 |
| - setup_signer_registration_db(&connection, Vec::new()).unwrap(); |
961 |
| - |
962 |
| - let mut signer_registration_store_adapter = |
963 |
| - SignerRegistrationStoreAdapter::new(Arc::new(Mutex::new(connection))); |
964 |
| - |
965 |
| - for (epoch, signer_with_stakes) in &signer_with_stakes_by_epoch { |
966 |
| - assert!(signer_registration_store_adapter |
967 |
| - .store_record(epoch, signer_with_stakes) |
968 |
| - .await |
969 |
| - .is_ok()); |
970 |
| - } |
971 |
| - |
972 |
| - for (epoch, signer_with_stakes) in &signer_with_stakes_by_epoch { |
973 |
| - assert!(signer_registration_store_adapter |
974 |
| - .record_exists(epoch) |
975 |
| - .await |
976 |
| - .unwrap()); |
977 |
| - assert_eq!( |
978 |
| - Some(signer_with_stakes.to_owned()), |
979 |
| - signer_registration_store_adapter |
980 |
| - .get_record(epoch) |
981 |
| - .await |
982 |
| - .unwrap() |
983 |
| - ); |
984 |
| - } |
985 |
| - assert_eq!( |
986 |
| - signer_with_stakes_by_epoch |
987 |
| - .clone() |
988 |
| - .into_iter() |
989 |
| - .map(|(k, v)| (k, BTreeMap::from_iter(v.into_iter()))) |
990 |
| - .collect::<Vec<(Epoch, BTreeMap<PartyId, SignerWithStake>)>>(), |
991 |
| - signer_registration_store_adapter |
992 |
| - .get_last_n_records(signer_with_stakes_by_epoch.len()) |
993 |
| - .await |
994 |
| - .unwrap() |
995 |
| - .into_iter() |
996 |
| - .rev() |
997 |
| - .map(|(k, v)| (k, BTreeMap::from_iter(v.into_iter()))) |
998 |
| - .collect::<Vec<(Epoch, BTreeMap<PartyId, SignerWithStake>)>>() |
999 |
| - ) |
1000 |
| - } |
1001 |
| - |
1002 | 806 | pub fn init_signer_registration_store(
|
1003 | 807 | initial_data: Vec<(Epoch, HashMap<PartyId, SignerWithStake>)>,
|
1004 | 808 | retention_limit: Option<usize>,
|
|
0 commit comments