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

Use a unique name for the cloud location of digests artifacts Cardano database #2314

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 4 additions & 2 deletions mithril-aggregator/src/artifact_builder/cardano_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl ArtifactBuilder<CardanoDbBeacon, CardanoDatabaseSnapshot> for CardanoDataba
.immutable_builder
.upload(beacon.immutable_file_number)
.await?;
let digest_locations = self.digest_builder.upload().await?;
let digest_locations = self.digest_builder.upload(&beacon).await?;

let locations = ArtifactsLocations {
ancillary: ancillary_locations,
Expand Down Expand Up @@ -176,6 +176,7 @@ mod tests {
let test_dir = get_test_directory("should_compute_valid_artifact");

let beacon = CardanoDbBeacon::new(123, 3);
let network = CardanoNetwork::DevNet(123);
let immutable_trio_file_size = 777;
let ledger_file_size = 6666;
let volatile_file_size = 99;
Expand Down Expand Up @@ -204,7 +205,7 @@ mod tests {
AncillaryArtifactBuilder::new(
vec![Arc::new(ancillary_uploader)],
snapshotter.clone(),
CardanoNetwork::DevNet(123),
network,
CompressionAlgorithm::Gzip,
TestLogger::stdout(),
)
Expand Down Expand Up @@ -245,6 +246,7 @@ mod tests {
DigestArtifactBuilder::new(
SanitizedUrlWithTrailingSlash::parse("http://aggregator_uri").unwrap(),
vec![],
network,
test_dir.join("digests"),
Arc::new(immutable_file_digest_mapper),
TestLogger::stdout(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use std::{
use anyhow::Context;
use async_trait::async_trait;
use mithril_common::{
entities::DigestLocation, logging::LoggerExtensions,
messages::CardanoDatabaseDigestListItemMessage, StdResult,
entities::{CardanoDbBeacon, DigestLocation},
logging::LoggerExtensions,
messages::CardanoDatabaseDigestListItemMessage,
CardanoNetwork, StdResult,
};
use slog::{error, Logger};

Expand Down Expand Up @@ -60,6 +62,8 @@ pub struct DigestArtifactBuilder {
/// Uploaders
uploaders: Vec<Arc<dyn DigestFileUploader>>,

network: CardanoNetwork,

digests_dir: PathBuf,

immutable_file_digest_mapper: Arc<dyn ImmutableFileDigestMapper>,
Expand All @@ -72,21 +76,23 @@ impl DigestArtifactBuilder {
pub fn new(
aggregator_url_prefix: SanitizedUrlWithTrailingSlash,
uploaders: Vec<Arc<dyn DigestFileUploader>>,
network: CardanoNetwork,
digests_dir: PathBuf,
immutable_file_digest_mapper: Arc<dyn ImmutableFileDigestMapper>,
logger: Logger,
) -> StdResult<Self> {
Ok(Self {
aggregator_url_prefix,
uploaders,
network,
digests_dir,
immutable_file_digest_mapper,
logger: logger.new_with_component_name::<Self>(),
})
}

pub async fn upload(&self) -> StdResult<Vec<DigestLocation>> {
let digest_path = self.create_digest_file().await?;
pub async fn upload(&self, beacon: &CardanoDbBeacon) -> StdResult<Vec<DigestLocation>> {
let digest_path = self.create_digest_file(beacon).await?;

let locations = self.upload_digest_file(&digest_path).await;
fs::remove_file(&digest_path).with_context(|| {
Expand All @@ -96,7 +102,7 @@ impl DigestArtifactBuilder {
locations
}

async fn create_digest_file(&self) -> StdResult<PathBuf> {
async fn create_digest_file(&self, beacon: &CardanoDbBeacon) -> StdResult<PathBuf> {
let immutable_file_digest_map = self
.immutable_file_digest_mapper
.get_immutable_file_digest_map()
Expand All @@ -110,7 +116,8 @@ impl DigestArtifactBuilder {
)
.collect::<Vec<_>>();

let digests_file_path = DigestArtifactBuilder::get_digests_file_path(&self.digests_dir);
let digests_file_path =
DigestArtifactBuilder::get_digests_file_path(&self.digests_dir, &self.network, beacon);

if let Some(digests_dir) = digests_file_path.parent() {
fs::create_dir_all(digests_dir).with_context(|| {
Expand Down Expand Up @@ -160,8 +167,16 @@ impl DigestArtifactBuilder {
})
}

fn get_digests_file_path<P: AsRef<Path>>(digests_dir: P) -> PathBuf {
digests_dir.as_ref().join("digests.json")
fn get_digests_file_path<P: AsRef<Path>>(
digests_dir: P,
network: &CardanoNetwork,
beacon: &CardanoDbBeacon,
) -> PathBuf {
let filename = format!(
"{}-e{}-i{}.digests.json",
network, *beacon.epoch, beacon.immutable_file_number
);
digests_dir.as_ref().join(filename)
}
}

Expand All @@ -174,6 +189,7 @@ mod tests {
};
use anyhow::anyhow;
use mithril_common::{
entities::CardanoDbBeacon,
messages::{CardanoDatabaseDigestListItemMessage, CardanoDatabaseDigestListMessage},
test_utils::{assert_equivalent, TempDir},
};
Expand Down Expand Up @@ -214,13 +230,14 @@ mod tests {
let builder = DigestArtifactBuilder::new(
SanitizedUrlWithTrailingSlash::parse("https://aggregator/").unwrap(),
vec![],
CardanoNetwork::DevNet(123),
temp_dir,
Arc::new(immutable_file_digest_mapper),
TestLogger::stdout(),
)
.unwrap();

let locations = builder.upload().await.unwrap();
let locations = builder.upload(&CardanoDbBeacon::new(4, 123)).await.unwrap();
assert_eq!(
vec!(DigestLocation::Aggregator {
uri: "https://aggregator/artifact/cardano-database/digests".to_string()
Expand All @@ -243,6 +260,7 @@ mod tests {
let builder = DigestArtifactBuilder::new(
SanitizedUrlWithTrailingSlash::parse("https://aggregator/").unwrap(),
vec![Arc::new(uploader)],
CardanoNetwork::DevNet(123),
PathBuf::from("/tmp/whatever"),
Arc::new(MockImmutableFileDigestMapper::new()),
TestLogger::file(&log_path),
Expand All @@ -263,6 +281,7 @@ mod tests {
let builder = DigestArtifactBuilder::new(
SanitizedUrlWithTrailingSlash::parse("https://aggregator/").unwrap(),
vec![Arc::new(uploader)],
CardanoNetwork::DevNet(123),
PathBuf::from("/tmp/whatever"),
Arc::new(MockImmutableFileDigestMapper::new()),
TestLogger::stdout(),
Expand Down Expand Up @@ -292,6 +311,7 @@ mod tests {
let builder = DigestArtifactBuilder::new(
SanitizedUrlWithTrailingSlash::parse("https://aggregator/").unwrap(),
uploaders,
CardanoNetwork::DevNet(123),
PathBuf::from("/tmp/whatever"),
Arc::new(MockImmutableFileDigestMapper::new()),
TestLogger::stdout(),
Expand Down Expand Up @@ -327,6 +347,7 @@ mod tests {
let builder = DigestArtifactBuilder::new(
SanitizedUrlWithTrailingSlash::parse("https://aggregator/").unwrap(),
uploaders,
CardanoNetwork::DevNet(123),
PathBuf::from("/tmp/whatever"),
Arc::new(MockImmutableFileDigestMapper::new()),
TestLogger::stdout(),
Expand Down Expand Up @@ -373,13 +394,17 @@ mod tests {
let builder = DigestArtifactBuilder::new(
SanitizedUrlWithTrailingSlash::parse("https://aggregator/").unwrap(),
vec![],
CardanoNetwork::DevNet(123),
temp_dir,
Arc::new(immutable_file_digest_mapper),
TestLogger::stdout(),
)
.unwrap();

let digest_file = builder.create_digest_file().await.unwrap();
let digest_file = builder
.create_digest_file(&CardanoDbBeacon::new(4, 123))
.await
.unwrap();

let file_content = read_to_string(digest_file).unwrap();
let digest_content: CardanoDatabaseDigestListMessage =
Expand Down Expand Up @@ -408,7 +433,10 @@ mod tests {

let mut digest_file_uploader = MockDigestFileUploader::new();

let digest_file = DigestArtifactBuilder::get_digests_file_path(&digests_dir);
let beacon = CardanoDbBeacon::new(3, 456);
let network = CardanoNetwork::DevNet(24);
let digest_file =
DigestArtifactBuilder::get_digests_file_path(&digests_dir, &network, &beacon);

let digest_file_clone = digest_file.clone();
digest_file_uploader
Expand All @@ -424,14 +452,31 @@ mod tests {
let builder = DigestArtifactBuilder::new(
SanitizedUrlWithTrailingSlash::parse("https://aggregator/").unwrap(),
vec![Arc::new(digest_file_uploader)],
network,
digests_dir,
Arc::new(immutable_file_digest_mapper),
TestLogger::stdout(),
)
.unwrap();

let _locations = builder.upload().await.unwrap();
let _locations = builder.upload(&beacon).await.unwrap();

assert!(!digest_file.exists());
}

#[tokio::test]
async fn get_digest_file_path_include_beacon_information() {
let digests_dir =
TempDir::create("digests", "get_digest_file_path_include_beacon_information");

let beacon = CardanoDbBeacon::new(5, 456);
let network = CardanoNetwork::MainNet;
let digest_file =
DigestArtifactBuilder::get_digests_file_path(&digests_dir, &network, &beacon);

assert_eq!(
digest_file,
digests_dir.join("mainnet-e5-i456.digests.json")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ impl DependenciesBuilder {
let digest_builder = Arc::new(DigestArtifactBuilder::new(
self.configuration.get_server_url()?,
self.build_cardano_database_digests_uploaders()?,
self.configuration.get_network()?,
snapshot_dir.join("pending_cardano_database_digests"),
self.get_immutable_file_digest_mapper().await?,
self.root_logger(),
Expand Down
2 changes: 1 addition & 1 deletion mithril-infra/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ variable "google_storage_bucket_max_age" {
variable "google_storage_bucket_prefix_with_lifecyle_rule" {
type = list(any)
description = "The prefix of the object in the storage bucket to apply the lifecycle rule"
default = ["cardano-immutable-files-full", "cardano-database/ancillary"]
default = ["cardano-immutable-files-full", "cardano-database/ancillary", "cardano-database/digests"]
}

locals {
Expand Down
Loading