Skip to content

Commit 9089edd

Browse files
committed
json adapter to work with beacons
1 parent 35516cb commit 9089edd

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

mithril-aggregator/src/certificate_store/jsonfile_store_adapter.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ use std::{
99

1010
use glob::glob;
1111
use serde::{de::DeserializeOwned, Serialize};
12-
use serde_json::json;
1312

1413
use super::{AdapterError, StoreAdapter};
1514

16-
struct JsonFileStoreAdapter<K, V> {
15+
pub struct JsonFileStoreAdapter<K, V> {
1716
dirpath: PathBuf,
1817
key: PhantomData<K>,
1918
value: PhantomData<V>,
@@ -24,12 +23,6 @@ where
2423
K: Hash + PartialEq + Serialize + DeserializeOwned,
2524
V: Serialize + DeserializeOwned,
2625
{
27-
fn create_dir(dirpath: &PathBuf) -> Result<(), AdapterError> {
28-
std::fs::create_dir_all(dirpath)
29-
.map_err(|e| AdapterError::InitializationError(e.into()))?;
30-
31-
Ok(())
32-
}
3326
pub fn new(dirpath: PathBuf) -> Result<Self, AdapterError> {
3427
if !dirpath.exists() {
3528
Self::create_dir(&dirpath)?;
@@ -42,12 +35,18 @@ where
4235
})
4336
}
4437

38+
fn create_dir(dirpath: &PathBuf) -> Result<(), AdapterError> {
39+
fs::create_dir_all(dirpath).map_err(|e| AdapterError::InitializationError(e.into()))?;
40+
41+
Ok(())
42+
}
43+
4544
fn get_hash_from_key(&self, key: &K) -> String {
4645
let mut hasher = DefaultHasher::new();
4746
key.hash(&mut hasher);
4847
let checksum = hasher.finish();
4948

50-
format!("{:X}", checksum)
49+
format!("{:0>16X}", checksum)
5150
}
5251

5352
fn get_filename_from_key(&self, key: &K) -> PathBuf {
@@ -117,6 +116,7 @@ where
117116
)
118117
}
119118
}
119+
120120
impl<K, V> StoreAdapter for JsonFileStoreAdapter<K, V>
121121
where
122122
K: Hash + PartialEq + Serialize + DeserializeOwned,
@@ -191,7 +191,7 @@ where
191191

192192
#[cfg(test)]
193193
mod tests {
194-
use std::io::Read;
194+
use serde_json::json;
195195
use std::time::Duration;
196196

197197
use super::*;
@@ -274,6 +274,7 @@ mod tests {
274274

275275
assert!(adapter.store_record(&1, &record).is_ok());
276276
assert_eq!(record, adapter.get_record(&1).unwrap().unwrap());
277+
rmdir(dir);
277278
}
278279

279280
#[test]
@@ -285,5 +286,6 @@ mod tests {
285286

286287
assert!(adapter.store_record(&1, &record).is_ok());
287288
assert_eq!(record, adapter.get_record(&1).unwrap().unwrap());
289+
rmdir(dir);
288290
}
289291
}

mithril-common/src/entities.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
66
pub type ImmutableFileNumber = u64;
77

88
/// Beacon represents a point in the Cardano chain at which a Mithril certificate should be produced
9-
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
9+
#[derive(Clone, Debug, PartialEq, Hash, Default, Serialize, Deserialize)]
1010
pub struct Beacon {
1111
/// Cardano network
1212
#[serde(rename = "network")]

0 commit comments

Comments
 (0)