Skip to content

add generic adapters #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ futures = "0.3"
reqwest = { version = "0.11", features = ["json"] }
config = "0.13.1"
blake2 = "0.9.2"
glob = "0.3"

[dev-dependencies]
mockall = "0.11.0"
Expand Down
5 changes: 3 additions & 2 deletions mithril-aggregator/config/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"network": "testnet",
"url_snapshot_manifest": "https://storage.googleapis.com/cardano-testnet/snapshots.json",
"snapshot_store_type": "local",
"snapshot_uploader_type": "local"
}
"snapshot_uploader_type": "local",
"pending_certificate_store_directory": "/tmp/mithril/cert_db"
}
3 changes: 2 additions & 1 deletion mithril-aggregator/config/testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"network": "testnet",
"url_snapshot_manifest": "https://storage.googleapis.com/cardano-testnet/snapshots.json",
"snapshot_store_type": "gcp",
"snapshot_uploader_type": "gcp"
"snapshot_uploader_type": "gcp",
"pending_certificate_store_directory": "./mithril/cert_db"
}
139 changes: 139 additions & 0 deletions mithril-aggregator/src/certificate_store/dumb_adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use super::{AdapterError, StoreAdapter};
use async_trait::async_trait;

pub struct DumbStoreAdapter<K, R> {
last_key: Option<K>,
last_certificate: Option<R>,
}

impl<K, R> DumbStoreAdapter<K, R> {
pub fn new() -> Self {
Self {
last_key: None,
last_certificate: None,
}
}
}

#[async_trait]
impl<K, R> StoreAdapter for DumbStoreAdapter<K, R>
where
R: Clone + Send + Sync,
K: PartialEq + Clone + Send + Sync,
{
type Key = K;
type Record = R;

async fn store_record(
&mut self,
key: &Self::Key,
record: &Self::Record,
) -> Result<(), AdapterError> {
let key = key.clone();
let record = record.clone();

self.last_key = Some(key);
self.last_certificate = Some(record);

Ok(())
}

async fn get_record(&self, key: &Self::Key) -> Result<Option<Self::Record>, AdapterError> {
if self.record_exists(key).await? {
Ok(self.last_certificate.as_ref().cloned())
} else {
Ok(None)
}
}

async fn record_exists(&self, key: &Self::Key) -> Result<bool, AdapterError> {
Ok(self.last_key.is_some() && self.last_key.as_ref().unwrap() == key)
}

async fn get_last_n_records(
&self,
how_many: usize,
) -> Result<Vec<(Self::Key, Self::Record)>, AdapterError> {
if how_many > 0 {
match &self.last_key {
Some(_key) => Ok(vec![(
self.last_key.as_ref().cloned().unwrap(),
self.last_certificate.as_ref().cloned().unwrap(),
)]),
None => Ok(Vec::new()),
}
} else {
Ok(Vec::new())
}
}
}

#[cfg(test)]
mod tests {

use super::*;

#[tokio::test]
async fn test_with_no_record_exists() {
let adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();

assert!(!adapter.record_exists(&1).await.unwrap());
}

#[tokio::test]
async fn test_with_no_record_get() {
let adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();

assert!(adapter.get_record(&1).await.unwrap().is_none());
}

#[tokio::test]
async fn test_write_record() {
let mut adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();

assert!(adapter
.store_record(&1, &"record".to_string())
.await
.is_ok());
assert_eq!(
"record".to_owned(),
adapter.get_record(&1).await.unwrap().unwrap()
);
}

#[tokio::test]
async fn test_list_with_no_record() {
let adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();

assert_eq!(0, adapter.get_last_n_records(10).await.unwrap().len());
}

#[tokio::test]
async fn test_list_with_records() {
let mut adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();
let _res = adapter
.store_record(&1, &"record".to_string())
.await
.unwrap();
let list = adapter.get_last_n_records(10).await.unwrap();

assert_eq!(1, list.len());

let (key, record) = &list[0];

assert_eq!(&1, key);
assert_eq!(&("record".to_owned()), record);
}

#[tokio::test]
async fn test_list_with_last_zero() {
let mut adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();
let _res = adapter
.store_record(&1, &"record".to_string())
.await
.unwrap();
let list = adapter.get_last_n_records(0).await.unwrap();

assert_eq!(0, list.len());
}
}
Loading