Skip to content

Commit 86ab07d

Browse files
authored
Merge pull request #875 from input-output-hk/jpraynaud/868-artifact-restorer-trait
Implement Artifact Restorer
2 parents ab9d05b + 8a1f4f6 commit 86ab07d

File tree

7 files changed

+117
-12
lines changed

7 files changed

+117
-12
lines changed

mithril-aggregator/src/artifact_builder/artifact_builder_service.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ use std::sync::Arc;
22

33
use mithril_common::{
44
entities::{Certificate, SignedEntityType},
5-
signable_builder::DummyBeacon,
5+
signable_builder::{Artifact, DummyBeacon},
66
StdResult,
77
};
88

9-
use crate::artifact_builder::{Artifact, DummyArtifactBuilder};
10-
11-
use super::ArtifactBuilder;
9+
use crate::artifact_builder::{ArtifactBuilder, DummyArtifactBuilder};
1210

1311
/// ArtifactBuilder Service
1412
// TODO: temporary implementation

mithril-aggregator/src/artifact_builder/dummy_artifact.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
use std::marker::PhantomData;
22

33
use async_trait::async_trait;
4-
use mithril_common::{entities::Certificate, signable_builder::DummyBeacon, StdResult};
4+
use mithril_common::{
5+
entities::Certificate,
6+
signable_builder::{Artifact, DummyBeacon},
7+
StdResult,
8+
};
59
use serde::{Deserialize, Serialize};
610

7-
use crate::artifact_builder::{Artifact, ArtifactBuilder};
11+
use crate::artifact_builder::ArtifactBuilder;
812

913
/// Dummy artifact
1014
#[derive(Serialize, Deserialize, PartialEq, Debug)]

mithril-aggregator/src/artifact_builder/interface.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use async_trait::async_trait;
2-
use mithril_common::{entities::Certificate, signable_builder::Beacon, StdResult};
3-
use serde::{Deserialize, Serialize};
4-
use std::fmt::Debug;
5-
6-
/// Artifact is a trait for types that represent signed artifacts
7-
pub trait Artifact<'a>: Serialize + Deserialize<'a> + PartialEq + Debug {}
2+
use mithril_common::{
3+
entities::Certificate,
4+
signable_builder::{Artifact, Beacon},
5+
StdResult,
6+
};
87

98
/// ArtifactBuilder is trait for building an artifact
109
#[async_trait]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::marker::PhantomData;
2+
3+
use async_trait::async_trait;
4+
use mithril_common::{
5+
entities::Certificate,
6+
signable_builder::{DummyBeacon, DummySignable},
7+
StdResult,
8+
};
9+
use serde::{Deserialize, Serialize};
10+
11+
use crate::artifact_builder::{Artifact, ArtifactRestorer};
12+
13+
/// Dummy artifact
14+
#[derive(Serialize, Deserialize, PartialEq, Debug)]
15+
pub struct DummyArtifact<'a> {
16+
message: String,
17+
beacon: DummyBeacon,
18+
phantom: PhantomData<&'a DummyBeacon>,
19+
}
20+
21+
impl<'a> DummyArtifact<'a> {
22+
/// Dummy artifact factory
23+
pub fn new(message: String, beacon: DummyBeacon) -> Self {
24+
Self {
25+
message,
26+
beacon,
27+
phantom: PhantomData,
28+
}
29+
}
30+
}
31+
32+
impl<'a> Artifact<'a> for DummyArtifact<'a> {}
33+
34+
/// A [DummyArtifact] restorer
35+
pub struct DummyArtifactRestorer {}
36+
37+
impl DummyArtifactRestorer {
38+
/// Dummy artifact builder restorer
39+
pub fn new() -> Self {
40+
Self {}
41+
}
42+
}
43+
44+
impl Default for DummyArtifactRestorer {
45+
fn default() -> Self {
46+
Self::new()
47+
}
48+
}
49+
50+
#[async_trait]
51+
impl<'a> ArtifactRestorer<'a, DummyBeacon, DummyArtifact<'a>> for DummyArtifactRestorer {
52+
async fn compute_artifact(
53+
&'a self,
54+
beacon: DummyBeacon,
55+
certificate: &Certificate,
56+
) -> StdResult<DummyArtifact> {
57+
Ok(DummyArtifact::new(
58+
format!("certificate id is {}", certificate.hash),
59+
beacon,
60+
))
61+
}
62+
63+
/// Compute a signable from an artifact
64+
async fn compute_signable(&self, artifact: DummyArtifact) -> StdResult<DummySignable> {
65+
Ok(DummySignable::new(artifact.message, artifact.beacon))
66+
}
67+
68+
/// Restore an artifact
69+
async fn restore_artifact(&'a self, artifact: DummyArtifact) -> StdResult<()> {
70+
Ok(())
71+
}
72+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use async_trait::async_trait;
2+
use mithril_common::{entities::Certificate, signable_builder::Beacon, StdResult};
3+
use serde::{Deserialize, Serialize};
4+
use std::fmt::Debug;
5+
6+
/// ArtifactRestorer is trait for restoring an artifact
7+
#[async_trait]
8+
trait ArtifactRestorer<'a, V, W>
9+
where
10+
V: Signable,
11+
W: Artifact<'a>,
12+
{
13+
/// Compute a signable from an artifact
14+
async fn compute_signable(&self, artifact: W) -> StdResult<V>;
15+
16+
/// Restore an artifact
17+
async fn restore_artifact(&'a self, artifact: W) -> StdResult<()>;
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! The module used for building artifact
2+
3+
mod artifact_builder_service;
4+
mod dummy_artifact;
5+
mod interface;
6+
7+
pub use artifact_builder_service::*;
8+
pub use dummy_artifact::*;
9+
pub use interface::*;

mithril-common/src/signable_builder/interface.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use async_trait::async_trait;
2+
use serde::{Deserialize, Serialize};
3+
use std::fmt::Debug;
24

35
use crate::{entities::ProtocolMessage, StdResult};
46

@@ -11,6 +13,9 @@ pub trait Signable: Send + Sync {
1113
fn compute_protocol_message(&self) -> StdResult<ProtocolMessage>;
1214
}
1315

16+
/// Artifact is a trait for types that represent signed artifacts
17+
pub trait Artifact<'a>: Serialize + Deserialize<'a> + PartialEq + Debug {}
18+
1419
/// SignableBuilder is trait for building a signable for a beacon
1520
#[async_trait]
1621
pub trait SignableBuilder<U, V>

0 commit comments

Comments
 (0)