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

Implement CardanoTransactions proof generation route in aggregator #1473

Merged
merged 17 commits into from Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
[package]
name = "mithril-aggregator"
version = "0.4.29"
version = "0.4.30"
description = "A Mithril Aggregator server"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
64 changes: 64 additions & 0 deletions mithril-aggregator/src/database/provider/cardano_transaction.rs
Expand Up @@ -13,6 +13,8 @@ use async_trait::async_trait;
use sqlite::{Row, Value};
use std::{iter::repeat, sync::Arc};

use crate::services::TransactionsRetriever;

/// Cardano Transaction record is the representation of a cardano transaction.
#[derive(Debug, PartialEq, Clone)]
pub struct CardanoTransactionRecord {
Expand All @@ -36,6 +38,16 @@ impl From<CardanoTransaction> for CardanoTransactionRecord {
}
}

impl From<CardanoTransactionRecord> for CardanoTransaction {
fn from(other: CardanoTransactionRecord) -> CardanoTransaction {
CardanoTransaction {
transaction_hash: other.transaction_hash,
block_number: other.block_number,
immutable_file_number: other.immutable_file_number,
}
}
}

impl SqLiteEntity for CardanoTransactionRecord {
fn hydrate(row: Row) -> Result<Self, HydrationError>
where
Expand Down Expand Up @@ -186,6 +198,15 @@ impl CardanoTransactionRepository {
Self { connection }
}

/// Return all the [CardanoTransactionRecord]s in the database.
pub async fn get_all_transactions(&self) -> StdResult<Vec<CardanoTransactionRecord>> {
let provider = CardanoTransactionProvider::new(&self.connection);
let filters = WhereCondition::default();
let transactions = provider.find(filters)?;

Ok(transactions.collect())
}

/// Return the [CardanoTransactionRecord] for the given transaction hash.
pub async fn get_transaction(
&self,
Expand Down Expand Up @@ -242,6 +263,17 @@ impl TransactionStore for CardanoTransactionRepository {
}
}

#[async_trait]
impl TransactionsRetriever for CardanoTransactionRepository {
async fn get_all(&self) -> StdResult<Vec<CardanoTransaction>> {
self.get_all_transactions().await.map(|v| {
v.into_iter()
.map(|record| record.into())
.collect::<Vec<CardanoTransaction>>()
})
}
}

#[cfg(test)]
mod tests {
use mithril_common::sqlite::SourceAlias;
Expand Down Expand Up @@ -463,6 +495,38 @@ mod tests {
);
}

#[tokio::test]
async fn repository_store_transactions_and_get_all_stored_transactions() {
let connection = get_connection().await;
let repository = CardanoTransactionRepository::new(connection.clone());

let cardano_transactions = vec![
CardanoTransaction {
transaction_hash: "tx-hash-123".to_string(),
block_number: 10,
immutable_file_number: 99,
},
CardanoTransaction {
transaction_hash: "tx-hash-456".to_string(),
block_number: 11,
immutable_file_number: 100,
},
];
repository
.store_transactions(&cardano_transactions)
.await
.unwrap();

let transactions_result = repository.get_all().await.unwrap();
let transactions_expected = cardano_transactions
.iter()
.rev()
.cloned()
.collect::<Vec<_>>();

assert_eq!(transactions_expected, transactions_result);
}

#[tokio::test]
async fn repository_store_transactions_doesnt_erase_existing_data() {
let connection = get_connection().await;
Expand Down
50 changes: 47 additions & 3 deletions mithril-aggregator/src/dependency_injection/builder.rs
Expand Up @@ -59,8 +59,9 @@ use crate::{
http_server::routes::router,
services::{
CertifierService, MessageService, MithrilCertifierService, MithrilEpochService,
MithrilMessageService, MithrilSignedEntityService, MithrilStakeDistributionService,
MithrilTickerService, SignedEntityService, StakeDistributionService, TickerService,
MithrilMessageService, MithrilProverService, MithrilSignedEntityService,
MithrilStakeDistributionService, MithrilTickerService, ProverService, SignedEntityService,
StakeDistributionService, TickerService,
},
tools::{CExplorerSignerRetriever, GcpFileUploader, GenesisToolsDependency, SignersImporter},
AggregatorConfig, AggregatorRunner, AggregatorRuntime, CertificatePendingStore,
Expand Down Expand Up @@ -130,6 +131,9 @@ pub struct DependenciesBuilder {
/// Beacon provider service.
pub beacon_provider: Option<Arc<dyn BeaconProvider>>,

/// Cardano transactions repository.
pub transaction_repository: Option<Arc<CardanoTransactionRepository>>,

/// Cardano transactions store.
pub transaction_store: Option<Arc<dyn TransactionStore>>,

Expand Down Expand Up @@ -207,6 +211,9 @@ pub struct DependenciesBuilder {

/// HTTP Message service
pub message_service: Option<Arc<dyn MessageService>>,

/// Prover service
pub prover_service: Option<Arc<dyn ProverService>>,
}

impl DependenciesBuilder {
Expand All @@ -228,6 +235,7 @@ impl DependenciesBuilder {
chain_observer: None,
beacon_provider: None,
transaction_parser: None,
transaction_repository: None,
transaction_store: None,
immutable_digester: None,
immutable_file_observer: None,
Expand All @@ -252,6 +260,7 @@ impl DependenciesBuilder {
epoch_service: None,
signed_entity_storer: None,
message_service: None,
prover_service: None,
}
}

Expand Down Expand Up @@ -677,14 +686,31 @@ impl DependenciesBuilder {
self.create_logger().await
}

async fn build_transaction_store(&mut self) -> Result<Arc<dyn TransactionStore>> {
async fn build_transaction_repository(&mut self) -> Result<Arc<CardanoTransactionRepository>> {
let transaction_store = CardanoTransactionRepository::new(
self.get_sqlite_connection_cardano_transaction().await?,
);

Ok(Arc::new(transaction_store))
}

/// Transaction repository.
pub async fn get_transaction_repository(
&mut self,
) -> Result<Arc<CardanoTransactionRepository>> {
if self.transaction_repository.is_none() {
self.transaction_repository = Some(self.build_transaction_repository().await?);
}

Ok(self.transaction_repository.as_ref().cloned().unwrap())
}

async fn build_transaction_store(&mut self) -> Result<Arc<dyn TransactionStore>> {
let transaction_store = self.get_transaction_repository().await?;

Ok(transaction_store as Arc<dyn TransactionStore>)
}

/// Transaction store.
pub async fn get_transaction_store(&mut self) -> Result<Arc<dyn TransactionStore>> {
if self.transaction_store.is_none() {
Expand Down Expand Up @@ -1171,6 +1197,7 @@ impl DependenciesBuilder {
message_service: self.get_message_service().await?,
transaction_parser: self.get_transaction_parser().await?,
transaction_store: self.get_transaction_store().await?,
prover_service: self.get_prover_service().await?,
};

Ok(dependency_manager)
Expand Down Expand Up @@ -1374,6 +1401,23 @@ impl DependenciesBuilder {
Ok(self.message_service.as_ref().cloned().unwrap())
}

/// build Prover service
pub async fn build_prover_service(&mut self) -> Result<Arc<dyn ProverService>> {
let transaction_retriever = self.get_transaction_repository().await?;
let service = MithrilProverService::new(transaction_retriever);

Ok(Arc::new(service))
}

/// [ProverService] service
pub async fn get_prover_service(&mut self) -> Result<Arc<dyn ProverService>> {
if self.prover_service.is_none() {
self.prover_service = Some(self.build_prover_service().await?);
}

Ok(self.prover_service.as_ref().cloned().unwrap())
}

/// Remove the dependencies builder from memory to release Arc instances.
pub async fn vanish(self) {
self.drop_sqlite_connections().await;
Expand Down
8 changes: 7 additions & 1 deletion mithril-aggregator/src/dependency_injection/containers.rs
Expand Up @@ -22,7 +22,10 @@ use crate::{
database::provider::{CertificateRepository, SignedEntityStorer, SignerGetter, StakePoolStore},
event_store::{EventMessage, TransmitterService},
multi_signer::MultiSigner,
services::{CertifierService, SignedEntityService, StakeDistributionService, TickerService},
services::{
CertifierService, ProverService, SignedEntityService, StakeDistributionService,
TickerService,
},
signer_registerer::SignerRecorder,
snapshot_uploaders::SnapshotUploader,
CertificatePendingStore, ProtocolParametersStorer, SignerRegisterer,
Expand Down Expand Up @@ -151,6 +154,9 @@ pub struct DependencyContainer {

/// HTTP message service
pub message_service: Arc<dyn MessageService>,

/// Prover service
pub prover_service: Arc<dyn ProverService>,
}

#[doc(hidden)]
Expand Down
11 changes: 10 additions & 1 deletion mithril-aggregator/src/http_server/routes/middlewares.rs
Expand Up @@ -2,7 +2,9 @@ use crate::{
database::provider::SignerGetter,
dependency_injection::EpochServiceWrapper,
event_store::{EventMessage, TransmitterService},
services::{CertifierService, MessageService, SignedEntityService, TickerService},
services::{
CertifierService, MessageService, ProverService, SignedEntityService, TickerService,
},
CertificatePendingStore, Configuration, DependencyContainer, SignerRegisterer,
VerificationKeyStorer,
};
Expand Down Expand Up @@ -102,3 +104,10 @@ pub fn with_http_message_service(
) -> impl Filter<Extract = (Arc<dyn MessageService>,), Error = Infallible> + Clone {
warp::any().map(move || dependency_manager.message_service.clone())
}

/// With Prover service
pub fn with_prover_service(
dependency_manager: Arc<DependencyContainer>,
) -> impl Filter<Extract = (Arc<dyn ProverService>,), Error = Infallible> + Clone {
warp::any().map(move || dependency_manager.prover_service.clone())
}
1 change: 1 addition & 0 deletions mithril-aggregator/src/http_server/routes/mod.rs
Expand Up @@ -2,6 +2,7 @@ mod artifact_routes;
mod certificate_routes;
mod epoch_routes;
mod middlewares;
mod proof_routes;
pub(crate) mod reply;
mod root_routes;
pub mod router;
Expand Down