Skip to content

Commit 4094a14

Browse files
committed
Remove SignerRegistrationStoreAdapter
It was superseed by the `SignerRegistrationStore`.
1 parent 58a0815 commit 4094a14

File tree

2 files changed

+17
-212
lines changed

2 files changed

+17
-212
lines changed

mithril-aggregator/src/database/provider/signer_registration.rs

Lines changed: 6 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
use async_trait::async_trait;
22
use chrono::{DateTime, Utc};
33
use sqlite::{Connection, Value};
4-
use std::{
5-
collections::{BTreeMap, HashMap},
6-
sync::Arc,
7-
};
4+
use std::{collections::HashMap, sync::Arc};
85
use tokio::sync::Mutex;
96

10-
use crate::VerificationKeyStorer;
11-
use mithril_common::entities::Signer;
12-
use mithril_common::store::StoreError;
137
use mithril_common::{
148
crypto_helper::KESPeriod,
159
entities::{
1610
Epoch, HexEncodedOpCert, HexEncodedVerificationKey, HexEncodedVerificationKeySignature,
17-
PartyId, SignerWithStake, Stake,
11+
PartyId, Signer, SignerWithStake, Stake,
1812
},
1913
sqlite::{
2014
EntityCursor, HydrationError, Projection, Provider, SourceAlias, SqLiteEntity,
2115
WhereCondition,
2216
},
23-
store::adapter::{AdapterError, StoreAdapter},
17+
store::{adapter::AdapterError, StoreError},
2418
StdError,
2519
};
2620

21+
use crate::VerificationKeyStorer;
22+
2723
/// SignerRegistration record is the representation of a stored signer_registration.
2824
#[derive(Debug, PartialEq, Clone)]
2925
pub struct SignerRegistrationRecord {
@@ -469,141 +465,9 @@ impl VerificationKeyStorer for SignerRegistrationStore {
469465
}
470466
}
471467

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-
604468
#[cfg(test)]
605469
mod tests {
606-
use std::collections::{BTreeMap, HashMap};
470+
use std::collections::HashMap;
607471

608472
use mithril_common::test_utils::MithrilFixtureBuilder;
609473

@@ -939,66 +803,6 @@ mod tests {
939803
}
940804
}
941805

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-
1002806
pub fn init_signer_registration_store(
1003807
initial_data: Vec<(Epoch, HashMap<PartyId, SignerWithStake>)>,
1004808
retention_limit: Option<usize>,

mithril-aggregator/src/dependency_injection/builder.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ use crate::{
4141
},
4242
certifier_service::{CertifierService, MithrilCertifierService},
4343
configuration::ExecutionEnvironment,
44-
database::provider::{
45-
CertificateRepository, CertificateStoreAdapter, EpochSettingStore, OpenMessageRepository,
46-
SignedEntityStoreAdapter, SignedEntityStorer, SignerRegistrationStoreAdapter, SignerStore,
47-
SingleSignatureRepository, StakePoolStore,
44+
database::{
45+
provider::SignerRegistrationStore,
46+
provider::{
47+
CertificateRepository, CertificateStoreAdapter, EpochSettingStore,
48+
OpenMessageRepository, SignedEntityStoreAdapter, SignedEntityStorer, SignerStore,
49+
SingleSignatureRepository, StakePoolStore,
50+
},
4851
},
4952
event_store::{EventMessage, EventStore, TransmitterService},
5053
http_server::routes::router,
@@ -57,7 +60,7 @@ use crate::{
5760
CertificateStore, Configuration, DependencyManager, DumbSnapshotUploader, DumbSnapshotter,
5861
GzipSnapshotter, LocalSnapshotUploader, MithrilSignerRegisterer, MultiSigner, MultiSignerImpl,
5962
ProtocolParametersStore, ProtocolParametersStorer, RemoteSnapshotUploader, SnapshotUploader,
60-
SnapshotUploaderType, Snapshotter, VerificationKeyStore, VerificationKeyStorer,
63+
SnapshotUploaderType, Snapshotter, VerificationKeyStorer,
6164
};
6265

6366
use super::{DependenciesBuilderError, Result};
@@ -399,11 +402,9 @@ impl DependenciesBuilder {
399402
}
400403

401404
async fn build_verification_key_store(&mut self) -> Result<Arc<dyn VerificationKeyStorer>> {
402-
Ok(Arc::new(VerificationKeyStore::new(
403-
Box::new(SignerRegistrationStoreAdapter::new(
404-
self.get_sqlite_connection().await?,
405-
)),
406-
self.configuration.store_retention_limit,
405+
Ok(Arc::new(SignerRegistrationStore::new(
406+
self.get_sqlite_connection().await?,
407+
self.configuration.store_retention_limit.map(|l| l as u64),
407408
)))
408409
}
409410

0 commit comments

Comments
 (0)