Skip to content

Commit

Permalink
feat(Prover CLI): Configuration (with flag) (#1861)
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) -->

Adds the possibility to configure the DB URL. For now, we use a flag
`--db-url` for passing it. If not passed it defaults to
`postgres://postgres:notsecurepassword@localhost/prover_local`.

In the future, we should handle this differently, with an independent
command `config` to set one or multiple configurations into a
`.pli-config` file.

This also removes the need for the CLI to be run with `zk f`.

## 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. -->

We need to offer the Prover CLI users to provide the DB URL to which
they want to work. In addition, we want to get rid of the need for `zk
f` to run the app.

## 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).
- [ ] 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`.
- [x] Spellcheck has been run via `zk spellcheck`.
- [x] Linkcheck has been run via `zk linkcheck`.

---------

Co-authored-by: Joaquin Carletti <56092489+ColoCarletti@users.noreply.github.com>
Co-authored-by: EmilLuta <EmilLuta@users.noreply.github.com>
  • Loading branch information
3 people authored May 8, 2024
1 parent 59bb851 commit 620c880
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
21 changes: 18 additions & 3 deletions prover/prover_cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::{command, Parser, Subcommand};
use clap::{command, Args, Parser, Subcommand};
use zksync_types::url::SensitiveUrl;

use crate::commands::{self, get_file_info};

Expand All @@ -9,6 +10,20 @@ pub const VERSION_STRING: &str = env!("CARGO_PKG_VERSION");
struct ProverCLI {
#[command(subcommand)]
command: ProverCommand,
#[clap(flatten)]
config: ProverCLIConfig,
}

// Note: This is a temporary solution for the configuration of the CLI. In the
// future, we should have an `config` command to set the configuration in a
// `.config` file.
#[derive(Args)]
pub struct ProverCLIConfig {
#[clap(
long,
default_value = "postgres://postgres:notsecurepassword@localhost/prover_local"
)]
pub db_url: SensitiveUrl,
}

#[derive(Subcommand)]
Expand All @@ -19,10 +34,10 @@ enum ProverCommand {
}

pub async fn start() -> anyhow::Result<()> {
let ProverCLI { command } = ProverCLI::parse();
let ProverCLI { command, config } = ProverCLI::parse();
match command {
ProverCommand::FileInfo(args) => get_file_info::run(args).await?,
ProverCommand::Status(cmd) => cmd.run().await?,
ProverCommand::Status(cmd) => cmd.run(config).await?,
};

Ok(())
Expand Down
25 changes: 13 additions & 12 deletions prover/prover_cli/src/commands/status/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use anyhow::{ensure, Context as _};
use clap::Args as ClapArgs;
use prover_dal::{Connection, ConnectionPool, Prover, ProverDal};
use zksync_types::{
basic_fri_types::AggregationRound, prover_dal::WitnessJobStatus, L1BatchNumber,
basic_fri_types::AggregationRound, prover_dal::WitnessJobStatus, url::SensitiveUrl,
L1BatchNumber,
};

use super::utils::{AggregationRoundInfo, BatchData, Task, TaskStatus};
use crate::commands::status::utils::postgres_config;
use crate::cli::ProverCLIConfig;

#[derive(ClapArgs)]
pub struct Args {
Expand All @@ -16,13 +17,13 @@ pub struct Args {
verbose: bool,
}

pub(crate) async fn run(args: Args) -> anyhow::Result<()> {
pub(crate) async fn run(args: Args, config: ProverCLIConfig) -> anyhow::Result<()> {
ensure!(
!args.batches.is_empty(),
"At least one batch number should be provided"
);

let batches_data = get_batches_data(args.batches).await?;
let batches_data = get_batches_data(args.batches, config.db_url).await?;

for batch_data in batches_data {
println!("{batch_data:?}");
Expand All @@ -31,14 +32,14 @@ pub(crate) async fn run(args: Args) -> anyhow::Result<()> {
Ok(())
}

async fn get_batches_data(batches: Vec<L1BatchNumber>) -> anyhow::Result<Vec<BatchData>> {
let config = postgres_config()?;

let prover_connection_pool =
ConnectionPool::<Prover>::builder(config.prover_url()?, config.max_connections()?)
.build()
.await
.context("failed to build a prover_connection_pool")?;
async fn get_batches_data(
batches: Vec<L1BatchNumber>,
db_url: SensitiveUrl,
) -> anyhow::Result<Vec<BatchData>> {
let prover_connection_pool = ConnectionPool::<Prover>::singleton(db_url)
.build()
.await
.context("failed to build a prover_connection_pool")?;

let mut conn = prover_connection_pool
.connection()
Expand Down
6 changes: 4 additions & 2 deletions prover/prover_cli/src/commands/status/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clap::Subcommand;

use crate::cli::ProverCLIConfig;

pub(crate) mod batch;
mod utils;

Expand All @@ -9,9 +11,9 @@ pub enum StatusCommand {
}

impl StatusCommand {
pub(crate) async fn run(self) -> anyhow::Result<()> {
pub(crate) async fn run(self, config: ProverCLIConfig) -> anyhow::Result<()> {
match self {
StatusCommand::Batch(args) => batch::run(args).await,
StatusCommand::Batch(args) => batch::run(args, config).await,
}
}
}
6 changes: 0 additions & 6 deletions prover/prover_cli/src/commands/status/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ use std::fmt::Debug;

use colored::*;
use strum::{Display, EnumString};
use zksync_config::PostgresConfig;
use zksync_env_config::FromEnv;
use zksync_types::{
basic_fri_types::AggregationRound,
prover_dal::{ProofCompressionJobStatus, ProverJobFriInfo, ProverJobStatus, WitnessJobStatus},
L1BatchNumber,
};

pub fn postgres_config() -> anyhow::Result<PostgresConfig> {
PostgresConfig::from_env()
}

/// Represents the proving data of a batch.
pub struct BatchData {
/// The number of the batch.
Expand Down

0 comments on commit 620c880

Please sign in to comment.