Skip to content

Commit

Permalink
feat: add general config and secrets opts to snapshot creator (#2471)
Browse files Browse the repository at this point in the history
## What ❔

<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->
This PR adds `--config-path` and `--secrets-path` options to
`/core/bin/snapshots_creator`. Allowing the binary to source
configurations from files.

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->
Needed in order to run the command within `zk_toolbox`.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [x] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
manuelmauro authored Jul 24, 2024
1 parent e02b411 commit 0f475c9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions core/bin/snapshots_creator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ zksync_env_config.workspace = true
zksync_types.workspace = true
zksync_object_store.workspace = true
zksync_vlog.workspace = true
zksync_core_leftovers.workspace = true

anyhow.workspace = true
structopt.workspace = true
tokio = { workspace = true, features = ["full"] }
tracing.workspace = true
futures.workspace = true
Expand Down
51 changes: 35 additions & 16 deletions core/bin/snapshots_creator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
//! at a time).
use anyhow::Context as _;
use structopt::StructOpt;
use tokio::{sync::watch, task::JoinHandle};
use zksync_config::{
configs::{DatabaseSecrets, ObservabilityConfig, PrometheusConfig},
SnapshotsCreatorConfig,
};
use zksync_config::configs::PrometheusConfig;
use zksync_core_leftovers::temp_config_store::{load_database_secrets, load_general_config};
use zksync_dal::{ConnectionPool, Core};
use zksync_env_config::{object_store::SnapshotsObjectStoreConfig, FromEnv};
use zksync_object_store::ObjectStoreFactory;
use zksync_vlog::prometheus::PrometheusExporterConfig;

Expand All @@ -28,9 +26,9 @@ mod metrics;
mod tests;

async fn maybe_enable_prometheus_metrics(
prometheus_config: Option<PrometheusConfig>,
stop_receiver: watch::Receiver<bool>,
) -> anyhow::Result<Option<JoinHandle<anyhow::Result<()>>>> {
let prometheus_config = PrometheusConfig::from_env().ok();
match prometheus_config.map(|c| (c.gateway_endpoint(), c.push_interval())) {
Some((Some(gateway_endpoint), push_interval)) => {
tracing::info!("Starting prometheus exporter with gateway {gateway_endpoint:?} and push_interval {push_interval:?}");
Expand All @@ -49,18 +47,36 @@ async fn maybe_enable_prometheus_metrics(
/// Minimum number of storage log chunks to produce.
const MIN_CHUNK_COUNT: u64 = 10;

#[derive(StructOpt)]
#[structopt(name = "ZKsync snapshot creator", author = "Matter Labs")]
struct Opt {
/// Path to the configuration file.
#[structopt(long)]
config_path: Option<std::path::PathBuf>,

/// Path to the secrets file.
#[structopt(long)]
secrets_path: Option<std::path::PathBuf>,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let (stop_sender, stop_receiver) = watch::channel(false);

let observability_config =
ObservabilityConfig::from_env().context("ObservabilityConfig::from_env()")?;
let opt = Opt::from_args();
let general_config = load_general_config(opt.config_path).context("general config")?;
let database_secrets = load_database_secrets(opt.secrets_path).context("database secrets")?;

let observability_config = general_config
.observability
.context("observability config")?;
let log_format: zksync_vlog::LogFormat = observability_config
.log_format
.parse()
.context("Invalid log format")?;

let prometheus_exporter_task = maybe_enable_prometheus_metrics(stop_receiver).await?;
let prometheus_exporter_task =
maybe_enable_prometheus_metrics(general_config.prometheus_config, stop_receiver).await?;
let mut builder = zksync_vlog::ObservabilityBuilder::new().with_log_format(log_format);
if let Some(sentry_url) = observability_config.sentry_url {
builder = builder
Expand All @@ -71,16 +87,19 @@ async fn main() -> anyhow::Result<()> {
let _guard = builder.build();
tracing::info!("Starting snapshots creator");

let object_store_config =
SnapshotsObjectStoreConfig::from_env().context("SnapshotsObjectStoreConfig::from_env()")?;
let blob_store = ObjectStoreFactory::new(object_store_config.0)
let creator_config = general_config
.snapshot_creator
.context("snapshot creator config")?;

let object_store_config = creator_config
.clone()
.object_store
.context("snapshot creator object storage config")?;

let blob_store = ObjectStoreFactory::new(object_store_config)
.create_store()
.await?;

let database_secrets = DatabaseSecrets::from_env().context("DatabaseSecrets")?;
let creator_config =
SnapshotsCreatorConfig::from_env().context("SnapshotsCreatorConfig::from_env")?;

let replica_pool = ConnectionPool::<Core>::builder(
database_secrets.replica_url()?,
creator_config.concurrent_queries_count,
Expand Down
1 change: 1 addition & 0 deletions core/bin/snapshots_creator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
};

use rand::{thread_rng, Rng};
use zksync_config::SnapshotsCreatorConfig;
use zksync_dal::{Connection, CoreDal};
use zksync_object_store::{MockObjectStore, ObjectStore};
use zksync_types::{
Expand Down
8 changes: 6 additions & 2 deletions core/lib/env_config/src/snapshots_creator.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use zksync_config::SnapshotsCreatorConfig;

use crate::{envy_load, FromEnv};
use crate::{envy_load, object_store::SnapshotsObjectStoreConfig, FromEnv};

impl FromEnv for SnapshotsCreatorConfig {
fn from_env() -> anyhow::Result<Self> {
envy_load("snapshots_creator", "SNAPSHOTS_CREATOR_")
let mut snapshot_creator: SnapshotsCreatorConfig =
envy_load("snapshots_creator", "SNAPSHOTS_CREATOR_")?;

snapshot_creator.object_store = SnapshotsObjectStoreConfig::from_env().map(|a| a.0).ok();
Ok(snapshot_creator)
}
}
5 changes: 2 additions & 3 deletions docs/guides/external-node/09_decentralization.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ On the gossipnet, the data integrity will be protected by the BFT (byzantine fau
> current implementation it may take a couple of hours and gets faster the more nodes you add to the
> `gossip_static_outbound` list (see below). We are working to remove this inconvenience.

> [!NOTE]
>
> The minimal supported server version for this is [24.11.0](https://github.com/matter-labs/zksync-era/releases/tag/core-v24.11.0)
> The minimal supported server version for this is
> [24.11.0](https://github.com/matter-labs/zksync-era/releases/tag/core-v24.11.0)
### Generating secrets

Expand Down

0 comments on commit 0f475c9

Please sign in to comment.