Skip to content
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

add signable builder in signer #891

Merged
merged 3 commits into from
May 2, 2023
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
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-aggregator"
version = "0.3.9"
version = "0.3.10"
description = "A Mithril Aggregator server"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
11 changes: 6 additions & 5 deletions mithril-aggregator/src/dependency_injection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use mithril_common::{
adapters::{EraReaderAdapterBuilder, EraReaderDummyAdapter},
EraChecker, EraMarker, EraReader, EraReaderAdapter, SupportedEra,
},
signable_builder::CardanoImmutableFilesFullSignableBuilder,
store::adapter::{MemoryAdapter, SQLiteAdapter, StoreAdapter},
BeaconProvider, BeaconProviderImpl,
};
Expand All @@ -42,9 +43,7 @@ use crate::{
},
event_store::{EventMessage, EventStore, TransmitterService},
http_server::routes::router,
signable_builder::{
ImmutableSignableBuilder, MithrilStakeDistributionSignableBuilder, SignableBuilderService,
},
signable_builder::{MithrilStakeDistributionSignableBuilder, SignableBuilderService},
signer_registerer::SignerRecorder,
stake_distribution_service::{MithrilStakeDistributionService, StakeDistributionService},
ticker_service::{MithrilTickerService, TickerService},
Expand Down Expand Up @@ -877,8 +876,10 @@ impl DependenciesBuilder {
let multi_signer = self.get_multi_signer().await?;
let mithril_stake_distribution_builder =
MithrilStakeDistributionSignableBuilder::new(multi_signer);
let immutable_signable_builder =
ImmutableSignableBuilder::new(self.get_immutable_digester().await?);
let immutable_signable_builder = CardanoImmutableFilesFullSignableBuilder::new(
self.get_immutable_digester().await?,
self.get_logger().await?,
);
let signable_builder_service = Arc::new(SignableBuilderService::new(
mithril_stake_distribution_builder,
immutable_signable_builder,
Expand Down
2 changes: 1 addition & 1 deletion mithril-aggregator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub use crate::configuration::{
};
pub use crate::multi_signer::{MultiSigner, MultiSignerImpl, ProtocolError};
pub use crate::signable_builder::{
ImmutableSignableBuilder, MithrilStakeDistributionSignableBuilder, SignableBuilderService,
MithrilStakeDistributionSignableBuilder, SignableBuilderService,
};
pub use crate::snapshot_stores::{LocalSnapshotStore, SnapshotStore};
pub use command_args::MainOpts;
Expand Down
2 changes: 0 additions & 2 deletions mithril-aggregator/src/signable_builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! The module used for building signables

mod immutable_signable_builder;
mod mithril_stake_distribution;
mod signable_builder_service;

pub use immutable_signable_builder::*;
pub use mithril_stake_distribution::*;
pub use signable_builder_service::*;
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ use std::sync::Arc;

use mithril_common::{
entities::SignedEntityType,
signable_builder::{Signable, SignableBuilder},
signable_builder::{CardanoImmutableFilesFullSignableBuilder, Signable, SignableBuilder},
StdResult,
};

use super::{ImmutableSignableBuilder, MithrilStakeDistributionSignableBuilder};
use super::MithrilStakeDistributionSignableBuilder;

/// SignableBuilder Service
pub struct SignableBuilderService {
mithril_stake_distribution_builder: MithrilStakeDistributionSignableBuilder,
immutable_signable_builder: ImmutableSignableBuilder,
immutable_signable_builder: CardanoImmutableFilesFullSignableBuilder,
}

impl SignableBuilderService {
/// SignableBuilderService factory
pub fn new(
mithril_stake_distribution_builder: MithrilStakeDistributionSignableBuilder,
immutable_signable_builder: ImmutableSignableBuilder,
immutable_signable_builder: CardanoImmutableFilesFullSignableBuilder,
) -> Self {
Self {
mithril_stake_distribution_builder,
Expand Down
2 changes: 1 addition & 1 deletion mithril-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-common"
version = "0.2.45"
version = "0.2.46"
authors = { workspace = true }
edition = { workspace = true }
documentation = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion mithril-common/src/digesters/cache/json_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ mod tests {

#[tokio::test]
async fn reset_clear_existing_values() {
let file = get_test_dir("store_erase_existing_values").join("immutable-cache-store.json");
let file = get_test_dir("reset_clear_existing_values").join("immutable-cache-store.json");
let provider = JsonImmutableFileDigestCacheProvider::new(&file);
let values_to_store = vec![
("0.chunk".to_string(), "digest 0".to_string()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
use std::sync::Arc;

use async_trait::async_trait;
use mithril_common::{
use crate::{
digesters::ImmutableDigester,
entities::{Beacon, ProtocolMessage, ProtocolMessagePartKey},
signable_builder::SignableBuilder,
StdResult,
};
use async_trait::async_trait;
use slog::{debug, info, Logger};

/// A [SignableBuilder] for cardano immutable files.
pub struct ImmutableSignableBuilder {
/// This structure is responsible of calculating the message for Cardano immutable files snapshots.
pub struct CardanoImmutableFilesFullSignableBuilder {
immutable_digester: Arc<dyn ImmutableDigester>,
logger: Logger,
}

impl ImmutableSignableBuilder {
impl CardanoImmutableFilesFullSignableBuilder {
/// Constructor
pub fn new(immutable_digester: Arc<dyn ImmutableDigester>) -> Self {
Self { immutable_digester }
pub fn new(immutable_digester: Arc<dyn ImmutableDigester>, logger: Logger) -> Self {
Self {
immutable_digester,
logger,
}
}
}

#[async_trait]
impl SignableBuilder<Beacon, ProtocolMessage> for ImmutableSignableBuilder {
impl SignableBuilder<Beacon, ProtocolMessage> for CardanoImmutableFilesFullSignableBuilder {
async fn compute_signable(&self, beacon: Beacon) -> StdResult<ProtocolMessage> {
debug!(self.logger, "SignableBuilder::compute_signable({beacon:?})");
let digest = self.immutable_digester.compute_digest(&beacon).await?;
info!(self.logger, "SignableBuilder: digest = '{digest}'.");
let mut protocol_message = ProtocolMessage::new();
protocol_message.set_message_part(ProtocolMessagePartKey::SnapshotDigest, digest);

Expand All @@ -35,9 +42,10 @@ impl SignableBuilder<Beacon, ProtocolMessage> for ImmutableSignableBuilder {
mod tests {
use super::*;

use crate::digesters::{ImmutableDigester, ImmutableDigesterError};
use crate::entities::Beacon;
use async_trait::async_trait;
use mithril_common::digesters::{ImmutableDigester, ImmutableDigesterError};
use mithril_common::entities::Beacon;
use slog::Drain;

#[derive(Default)]
pub struct ImmutableDigesterImpl;
Expand All @@ -48,10 +56,18 @@ mod tests {
Ok(format!("immutable {}", beacon.immutable_file_number))
}
}

fn create_logger() -> slog::Logger {
let decorator = slog_term::PlainDecorator::new(slog_term::TestStdoutWriter);
let drain = slog_term::CompactFormat::new(decorator).build().fuse();
let drain = slog_async::Async::new(drain).build().fuse();
slog::Logger::root(Arc::new(drain), slog::o!())
}
#[tokio::test]
async fn compute_signable() {
let digester = ImmutableDigesterImpl::default();
let signable_builder = ImmutableSignableBuilder::new(Arc::new(digester));
let signable_builder =
CardanoImmutableFilesFullSignableBuilder::new(Arc::new(digester), create_logger());
let protocol_message = signable_builder
.compute_signable(Beacon::default())
.await
Expand Down
2 changes: 2 additions & 0 deletions mithril-common/src/signable_builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! The module used for building signables

mod cardano_immutable_full_signable_builder;
mod dummy_signable;
mod interface;

pub use cardano_immutable_full_signable_builder::*;
pub use dummy_signable::*;
pub use interface::*;
2 changes: 1 addition & 1 deletion mithril-signer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-signer"
version = "0.2.36"
version = "0.2.37"
description = "A Mithril Signer"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion mithril-signer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ pub use message_adapters::{
};
pub use protocol_initializer_store::{ProtocolInitializerStore, ProtocolInitializerStorer};
pub use runtime::*;
pub use signable_builder::SignableBuilderService;
pub use signable_builder::*;
pub use single_signer::*;
11 changes: 6 additions & 5 deletions mithril-signer/src/runtime/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ mod tests {
adapters::{EraReaderAdapterType, EraReaderBootstrapAdapter},
EraChecker, EraReader,
},
signable_builder::DummySignableBuilder,
signable_builder::CardanoImmutableFilesFullSignableBuilder,
store::{
adapter::{DumbStoreAdapter, MemoryAdapter},
StakeStore, StakeStorer,
Expand Down Expand Up @@ -533,16 +533,17 @@ mod tests {
));

let api_version_provider = Arc::new(APIVersionProvider::new(era_checker.clone()));
let digester = Arc::new(DumbImmutableDigester::new(DIGESTER_RESULT, true));
let signable_builder =
CardanoImmutableFilesFullSignableBuilder::new(digester.clone(), slog_scope::logger());

let dummy_signable_builder = DummySignableBuilder::new();
let signable_builder_service =
Arc::new(SignableBuilderService::new(dummy_signable_builder));
let signable_builder_service = Arc::new(SignableBuilderService::new(signable_builder));

SignerServices {
stake_store: Arc::new(StakeStore::new(Box::new(DumbStoreAdapter::new()), None)),
certificate_handler: Arc::new(DumbCertificateHandler::default()),
chain_observer,
digester: Arc::new(DumbImmutableDigester::new(DIGESTER_RESULT, true)),
digester,
single_signer: Arc::new(MithrilSingleSigner::new("1".to_string())),
beacon_provider,
protocol_initializer_store: Arc::new(ProtocolInitializerStore::new(
Expand Down
11 changes: 6 additions & 5 deletions mithril-signer/src/runtime/signer_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use mithril_common::{
},
digesters::{CardanoImmutableDigester, ImmutableDigester, ImmutableFileSystemObserver},
era::{EraChecker, EraReader},
signable_builder::DummySignableBuilder,
signable_builder::CardanoImmutableFilesFullSignableBuilder,
store::{adapter::SQLiteAdapter, StakeStore},
BeaconProvider, BeaconProviderImpl, StdError,
};

use crate::{
certificate_handler::CertificateHandler, signable_builder::SignableBuilderService,
single_signer::SingleSigner, CertificateHandlerHTTPClient, Configuration, MithrilSingleSigner,
ProtocolInitializerStore, ProtocolInitializerStorer,
certificate_handler::CertificateHandler, single_signer::SingleSigner,
CertificateHandlerHTTPClient, Configuration, MithrilSingleSigner, ProtocolInitializerStore,
ProtocolInitializerStorer, SignableBuilderService,
};

type StakeStoreService = Arc<StakeStore>;
Expand Down Expand Up @@ -201,7 +201,8 @@ impl<'a> ServiceBuilder for ProductionServiceBuilder<'a> {
api_version_provider.clone(),
));

let dummy_signable_builder = DummySignableBuilder::new();
let dummy_signable_builder =
CardanoImmutableFilesFullSignableBuilder::new(digester.clone(), slog_scope::logger());
let signable_builder_service =
Arc::new(SignableBuilderService::new(dummy_signable_builder));

Expand Down
14 changes: 7 additions & 7 deletions mithril-signer/src/signable_builder/signable_builder_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ use std::sync::Arc;

use mithril_common::{
entities::SignedEntityType,
signable_builder::{DummyBeacon, DummySignableBuilder, Signable, SignableBuilder},
signable_builder::{CardanoImmutableFilesFullSignableBuilder, Signable, SignableBuilder},
StdResult,
};

/// SignableBuilder Service
// TODO: temporary implementation
pub struct SignableBuilderService {
dummy_signable_builder: DummySignableBuilder,
immutable_signable_builder: CardanoImmutableFilesFullSignableBuilder,
}

impl SignableBuilderService {
/// SignableBuilderService factory
pub fn new(dummy_signable_builder: DummySignableBuilder) -> Self {
pub fn new(immutable_signable_builder: CardanoImmutableFilesFullSignableBuilder) -> Self {
Self {
dummy_signable_builder,
immutable_signable_builder,
}
}
}
Expand All @@ -28,9 +28,9 @@ impl SignableBuilderService {
signed_entity_type: SignedEntityType,
) -> StdResult<Arc<dyn Signable>> {
let signable: Arc<dyn Signable> = match signed_entity_type {
SignedEntityType::MithrilStakeDistribution(e) => Arc::new(
self.dummy_signable_builder
.compute_signable(DummyBeacon { epoch: e })
SignedEntityType::CardanoImmutableFilesFull(beacon) => Arc::new(
self.immutable_signable_builder
.compute_signable(beacon)
.await?,
),
_ => todo!(),
Expand Down
8 changes: 4 additions & 4 deletions mithril-signer/tests/test_extensions/state_machine_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use mithril_common::era::{
EraChecker, EraReader,
};
use mithril_common::era::{EraMarker, SupportedEra};
use mithril_common::signable_builder::DummySignableBuilder;
use mithril_common::signable_builder::CardanoImmutableFilesFullSignableBuilder;
use mithril_common::BeaconProvider;
use slog::Drain;
use slog_scope::debug;
Expand Down Expand Up @@ -150,9 +150,9 @@ impl StateMachineTester {

let api_version_provider = Arc::new(APIVersionProvider::new(era_checker.clone()));

let dummy_signable_builder = DummySignableBuilder::new();
let signable_builder_service =
Arc::new(SignableBuilderService::new(dummy_signable_builder));
let signable_builder =
CardanoImmutableFilesFullSignableBuilder::new(digester.clone(), slog_scope::logger());
let signable_builder_service = Arc::new(SignableBuilderService::new(signable_builder));

let services = SignerServices {
certificate_handler: certificate_handler.clone(),
Expand Down