Skip to content

Commit 0874697

Browse files
authored
Merge pull request #924 from input-output-hk/jpraynaud/893-implement-artifact-routes
Implement Artifact routes
2 parents 54e8cf9 + 9dd318e commit 0874697

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1191
-475
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-aggregator"
3-
version = "0.3.18"
3+
version = "0.3.19"
44
description = "A Mithril Aggregator server"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-aggregator/src/artifact_builder/artifact_builder_service.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use slog_scope::info;
44
use std::sync::Arc;
55

66
use mithril_common::{
7-
entities::{Beacon, Certificate, Epoch, SignedEntityType, Snapshot},
7+
entities::{Beacon, Certificate, Epoch, MithrilStakeDistribution, SignedEntityType, Snapshot},
88
signable_builder::Artifact,
99
StdResult,
1010
};
@@ -14,8 +14,6 @@ use crate::{
1414
database::provider::{SignedEntityRecord, SignedEntityStorer},
1515
};
1616

17-
use super::MithrilStakeDistribution;
18-
1917
#[cfg(test)]
2018
use mockall::automock;
2119

@@ -123,8 +121,11 @@ mod tests {
123121
async fn build_mithril_stake_distribution_artifact_when_given_mithril_stake_distribution_entity_type(
124122
) {
125123
let signers_with_stake = fake_data::signers_with_stakes(5);
126-
let mithril_stake_distribution_expected =
127-
MithrilStakeDistribution::new(Epoch(1), signers_with_stake);
124+
let mithril_stake_distribution_expected = MithrilStakeDistribution::new(
125+
Epoch(1),
126+
signers_with_stake,
127+
"certificate-123".to_string(),
128+
);
128129
let mithril_stake_distribution_clone = mithril_stake_distribution_expected.clone();
129130

130131
let mock_signed_entity_storer = MockSignedEntityStorer::new();

mithril-aggregator/src/artifact_builder/cardano_immutable_files_full.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ mod tests {
157157
#[tokio::test]
158158
async fn should_compute_valid_artifact() {
159159
let beacon = fake_data::beacon();
160-
let certificate = fake_data::certificate("cert-123".to_string());
160+
let certificate = fake_data::certificate("certificate-123".to_string());
161161
let snapshot_digest = certificate
162162
.protocol_message
163163
.get_message_part(&ProtocolMessagePartKey::SnapshotDigest)

mithril-aggregator/src/artifact_builder/mithril_stake_distribution.rs

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,14 @@
11
use async_trait::async_trait;
2-
use serde::{Deserialize, Serialize};
3-
use sha2::{Digest, Sha256};
42
use std::sync::Arc;
53
use tokio::sync::RwLock;
64

75
use super::ArtifactBuilder;
86
use crate::MultiSigner;
97
use mithril_common::{
10-
entities::{Certificate, Epoch, SignerWithStake},
11-
signable_builder::Artifact,
8+
entities::{Certificate, Epoch, MithrilStakeDistribution},
129
StdResult,
1310
};
1411

15-
/// Mithril Stake Distribution
16-
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
17-
pub struct MithrilStakeDistribution {
18-
epoch: Epoch,
19-
signers_with_stake: Vec<SignerWithStake>,
20-
hash: String,
21-
}
22-
23-
impl MithrilStakeDistribution {
24-
/// MithrilStakeDistribution artifact factory
25-
pub fn new(epoch: Epoch, signers_with_stake: Vec<SignerWithStake>) -> Self {
26-
let mut signers_with_stake_sorted = signers_with_stake;
27-
signers_with_stake_sorted.sort();
28-
let mut mithril_stake_distribution = Self {
29-
epoch,
30-
signers_with_stake: signers_with_stake_sorted,
31-
hash: "".to_string(),
32-
};
33-
mithril_stake_distribution.hash = mithril_stake_distribution.compute_hash();
34-
mithril_stake_distribution
35-
}
36-
37-
fn compute_hash(&self) -> String {
38-
let mut hasher = Sha256::new();
39-
hasher.update(self.epoch.0.to_be_bytes());
40-
for signer_with_stake in &self.signers_with_stake {
41-
hasher.update(signer_with_stake.compute_hash().as_bytes());
42-
}
43-
hex::encode(hasher.finalize())
44-
}
45-
}
46-
47-
#[typetag::serde]
48-
impl Artifact for MithrilStakeDistribution {
49-
fn get_id(&self) -> String {
50-
self.hash.clone()
51-
}
52-
}
53-
5412
/// A [MithrilStakeDistributionArtifact] builder
5513
pub struct MithrilStakeDistributionArtifactBuilder {
5614
multi_signer: Arc<RwLock<dyn MultiSigner>>,
@@ -68,12 +26,13 @@ impl ArtifactBuilder<Epoch, MithrilStakeDistribution> for MithrilStakeDistributi
6826
async fn compute_artifact(
6927
&self,
7028
beacon: Epoch,
71-
_certificate: &Certificate,
29+
certificate: &Certificate,
7230
) -> StdResult<MithrilStakeDistribution> {
7331
let multi_signer = self.multi_signer.read().await;
7432
Ok(MithrilStakeDistribution::new(
7533
beacon,
7634
multi_signer.get_next_signers_with_stake().await?,
35+
certificate.hash.to_owned(),
7736
))
7837
}
7938
}
@@ -90,7 +49,7 @@ mod tests {
9049
async fn should_compute_valid_artifact() {
9150
let signers_with_stake = fake_data::signers_with_stakes(5);
9251
let signers_with_stake_clone = signers_with_stake.clone();
93-
let certificate = fake_data::certificate("cert-123".to_string());
52+
let certificate = fake_data::certificate("certificate-123".to_string());
9453
let mut mock_multi_signer = MockMultiSigner::new();
9554
mock_multi_signer
9655
.expect_get_next_signers_with_stake()
@@ -101,7 +60,11 @@ mod tests {
10160
.compute_artifact(Epoch(1), &certificate)
10261
.await
10362
.unwrap();
104-
let artifact_expected = MithrilStakeDistribution::new(Epoch(1), signers_with_stake);
63+
let artifact_expected = MithrilStakeDistribution::new(
64+
Epoch(1),
65+
signers_with_stake,
66+
"certificate-123".to_string(),
67+
);
10568
assert_eq!(artifact_expected, artifact);
10669
}
10770

@@ -110,18 +73,33 @@ mod tests {
11073
let signers_with_stake = fake_data::signers_with_stakes(5);
11174

11275
assert_eq!(
113-
MithrilStakeDistribution::new(Epoch(1), signers_with_stake.clone()),
114-
MithrilStakeDistribution::new(Epoch(1), signers_with_stake.into_iter().rev().collect())
76+
MithrilStakeDistribution::new(
77+
Epoch(1),
78+
signers_with_stake.clone(),
79+
"certificate-123".to_string()
80+
),
81+
MithrilStakeDistribution::new(
82+
Epoch(1),
83+
signers_with_stake.into_iter().rev().collect(),
84+
"certificate-123".to_string()
85+
)
11586
);
11687
}
11788

11889
#[test]
11990
fn hash_value_doesnt_change_if_signers_order_change() {
12091
let signers_with_stake = fake_data::signers_with_stakes(5);
12192

122-
let sd = MithrilStakeDistribution::new(Epoch(1), signers_with_stake.clone());
123-
let sd2 =
124-
MithrilStakeDistribution::new(Epoch(1), signers_with_stake.into_iter().rev().collect());
93+
let sd = MithrilStakeDistribution::new(
94+
Epoch(1),
95+
signers_with_stake.clone(),
96+
"certificate-123".to_string(),
97+
);
98+
let sd2 = MithrilStakeDistribution::new(
99+
Epoch(1),
100+
signers_with_stake.into_iter().rev().collect(),
101+
"certificate-123".to_string(),
102+
);
125103

126104
assert_eq!(sd.hash, sd2.hash);
127105
}

mithril-aggregator/src/configuration.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ use mithril_common::CardanoNetwork;
1313
use crate::tools::GcpFileUploader;
1414
use crate::{LocalSnapshotUploader, RemoteSnapshotUploader, SnapshotUploader};
1515

16-
// TODO: 'LIST_SNAPSHOTS_MAX_ITEMS' keep as const or in config, or add a parameter to `list_snapshots`?
17-
pub const LIST_SNAPSHOTS_MAX_ITEMS: usize = 20;
18-
1916
/// Different kinds of execution environments
2017
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
2118
pub enum ExecutionEnvironment {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,13 @@ mod tests {
743743
fn get_certificate_record_by_certificate_id() {
744744
let connection = Connection::open(":memory:").unwrap();
745745
let provider = CertificateRecordProvider::new(&connection);
746-
let condition = provider.condition_by_certificate_id("cert-123").unwrap();
746+
let condition = provider
747+
.condition_by_certificate_id("certificate-123")
748+
.unwrap();
747749
let (filter, values) = condition.expand();
748750

749751
assert_eq!("certificate_id = ?1".to_string(), filter);
750-
assert_eq!(vec![Value::String("cert-123".to_string())], values);
752+
assert_eq!(vec![Value::String("certificate-123".to_string())], values);
751753
}
752754

753755
#[test]

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@ pub trait SignedEntityStorer: Sync + Send {
264264
&self,
265265
signed_entity_id: String,
266266
) -> StdResult<Option<SignedEntityRecord>>;
267+
268+
/// Get last signed entities by signed entity type
269+
async fn get_last_signed_entities_by_type(
270+
&self,
271+
signed_entity_type: &SignedEntityType,
272+
total: usize,
273+
) -> StdResult<Vec<SignedEntityRecord>>;
267274
}
268275

269276
/// Service to deal with signed_entity (read & write).
@@ -301,6 +308,21 @@ impl SignedEntityStorer for SignedEntityStoreAdapter {
301308

302309
Ok(signed_entity)
303310
}
311+
312+
async fn get_last_signed_entities_by_type(
313+
&self,
314+
signed_entity_type: &SignedEntityType,
315+
total: usize,
316+
) -> StdResult<Vec<SignedEntityRecord>> {
317+
let connection = &*self.connection.lock().await;
318+
let provider = SignedEntityRecordProvider::new(connection);
319+
let cursor = provider
320+
.get_by_signed_entity_type(signed_entity_type.to_owned())
321+
.map_err(|e| AdapterError::GeneralError(format!("{e}")))?;
322+
let signed_entities: Vec<SignedEntityRecord> = cursor.take(total).collect();
323+
324+
Ok(signed_entities)
325+
}
304326
}
305327

306328
// TODO: this StoreAdapter implementation is temporary and concerns only the snapshots for the CardanoImmutableFilesFull signed entity type

mithril-aggregator/src/dependency.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use sqlite::Connection;
1616
use std::{collections::HashMap, sync::Arc};
1717
use tokio::sync::{Mutex, RwLock};
1818

19+
use crate::event_store::EventMessage;
1920
use crate::{
2021
artifact_builder::ArtifactBuilderService,
2122
certifier_service::CertifierService,
@@ -27,7 +28,6 @@ use crate::{
2728
SignerRegisterer, SignerRegistrationRoundOpener, Snapshotter, VerificationKeyStore,
2829
VerificationKeyStorer,
2930
};
30-
use crate::{event_store::EventMessage, snapshot_stores::SnapshotStore};
3131
use crate::{event_store::TransmitterService, multi_signer::MultiSigner};
3232
use crate::{
3333
snapshot_uploaders::SnapshotUploader, stake_distribution_service::StakeDistributionService,
@@ -50,9 +50,6 @@ pub struct DependencyManager {
5050
/// It shall be a private dependency.
5151
pub stake_store: Arc<StakePoolStore>,
5252

53-
/// Snapshot store.
54-
pub snapshot_store: Arc<dyn SnapshotStore>,
55-
5653
/// Snapshot uploader service.
5754
pub snapshot_uploader: Arc<dyn SnapshotUploader>,
5855

mithril-aggregator/src/dependency_injection/builder.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{
4141
MithrilArtifactBuilderService, MithrilStakeDistributionArtifactBuilder,
4242
},
4343
certifier_service::{CertifierService, MithrilCertifierService},
44-
configuration::{ExecutionEnvironment, LIST_SNAPSHOTS_MAX_ITEMS},
44+
configuration::ExecutionEnvironment,
4545
database::provider::{
4646
CertificateRepository, CertificateStoreAdapter, EpochSettingStore, OpenMessageRepository,
4747
SignedEntityStoreAdapter, SignedEntityStorer, SignerRegistrationStoreAdapter, SignerStore,
@@ -55,10 +55,9 @@ use crate::{
5555
tools::{GcpFileUploader, GenesisToolsDependency},
5656
AggregatorConfig, AggregatorRunner, AggregatorRuntime, CertificatePendingStore,
5757
CertificateStore, Configuration, DependencyManager, DumbSnapshotUploader, DumbSnapshotter,
58-
GzipSnapshotter, LocalSnapshotStore, LocalSnapshotUploader, MithrilSignerRegisterer,
59-
MultiSigner, MultiSignerImpl, ProtocolParametersStore, ProtocolParametersStorer,
60-
RemoteSnapshotUploader, SnapshotStore, SnapshotUploader, SnapshotUploaderType, Snapshotter,
61-
VerificationKeyStore,
58+
GzipSnapshotter, LocalSnapshotUploader, MithrilSignerRegisterer, MultiSigner, MultiSignerImpl,
59+
ProtocolParametersStore, ProtocolParametersStorer, RemoteSnapshotUploader, SnapshotUploader,
60+
SnapshotUploaderType, Snapshotter, VerificationKeyStore,
6261
};
6362

6463
use super::{DependenciesBuilderError, Result};
@@ -87,9 +86,6 @@ pub struct DependenciesBuilder {
8786
/// It shall be a private dependency.
8887
pub stake_store: Option<Arc<StakePoolStore>>,
8988

90-
/// Snapshot store.
91-
pub snapshot_store: Option<Arc<dyn SnapshotStore>>,
92-
9389
/// Snapshot uploader service.
9490
pub snapshot_uploader: Option<Arc<dyn SnapshotUploader>>,
9591

@@ -191,7 +187,6 @@ impl DependenciesBuilder {
191187
configuration,
192188
sqlite_connection: None,
193189
stake_store: None,
194-
snapshot_store: None,
195190
snapshot_uploader: None,
196191
multi_signer: None,
197192
certificate_pending_store: None,
@@ -291,24 +286,6 @@ impl DependenciesBuilder {
291286
Ok(self.stake_store.as_ref().cloned().unwrap())
292287
}
293288

294-
async fn build_snapshot_store(&mut self) -> Result<Arc<dyn SnapshotStore>> {
295-
Ok(Arc::new(LocalSnapshotStore::new(
296-
Box::new(SignedEntityStoreAdapter::new(
297-
self.get_sqlite_connection().await?,
298-
)),
299-
LIST_SNAPSHOTS_MAX_ITEMS,
300-
)))
301-
}
302-
303-
/// Return a configured [SnapshotStore]
304-
pub async fn get_snapshot_store(&mut self) -> Result<Arc<dyn SnapshotStore>> {
305-
if self.snapshot_store.is_none() {
306-
self.snapshot_store = Some(self.build_snapshot_store().await?);
307-
}
308-
309-
Ok(self.snapshot_store.as_ref().cloned().unwrap())
310-
}
311-
312289
async fn build_snapshot_uploader(&mut self) -> Result<Arc<dyn SnapshotUploader>> {
313290
if self.configuration.environment == ExecutionEnvironment::Production {
314291
match self.configuration.snapshot_uploader_type {
@@ -960,7 +937,6 @@ impl DependenciesBuilder {
960937
config: self.configuration.clone(),
961938
sqlite_connection: self.get_sqlite_connection().await?,
962939
stake_store: self.get_stake_store().await?,
963-
snapshot_store: self.get_snapshot_store().await?,
964940
snapshot_uploader: self.get_snapshot_uploader().await?,
965941
multi_signer: self.get_multi_signer().await?,
966942
certificate_pending_store: self.get_certificate_pending_store().await?,

0 commit comments

Comments
 (0)