Skip to content
This repository has been archived by the owner on Feb 15, 2021. It is now read-only.

Commit

Permalink
Migrate witness_generator crate to the new configs
Browse files Browse the repository at this point in the history
  • Loading branch information
popzxc committed Dec 18, 2020
1 parent 70eaf0a commit 98bdc5e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
6 changes: 3 additions & 3 deletions core/bin/prover/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use zksync_circuit::{
circuit::ZkSyncCircuit,
witness::{deposit::DepositWitness, utils::WitnessBuilder, Witness},
};
use zksync_config::ConfigurationOptions;
use zksync_config::configs::ChainConfig;
use zksync_crypto::{
circuit::{account::CircuitAccount, CircuitAccountTree},
proof::EncodedProofPlonk,
Expand All @@ -33,7 +33,7 @@ fn prover_sends_heartbeat_requests_and_exits_on_stop_signal() {
// - BabyProver sends `working_on` requests (heartbeat) over api client
// - BabyProver stops running upon receiving data over stop channel

let block_size_chunks = ConfigurationOptions::from_env().available_block_chunk_sizes[0];
let block_size_chunks = ChainConfig::from_env().circuit.supported_block_chunks_sizes[0];

// Create a channel to notify on provers exit.
let (done_tx, _done_rx) = mpsc::channel();
Expand Down Expand Up @@ -148,7 +148,7 @@ fn new_test_data_for_prover() -> ProverData {
witness_accum.add_operation_with_pubdata(deposit_operations, pub_data_from_witness);
witness_accum.extend_pubdata_with_noops(smallest_block_size_for_chunks(
DepositOp::CHUNKS,
&ConfigurationOptions::from_env().available_block_chunk_sizes,
&ChainConfig::from_env().circuit.supported_block_chunks_sizes,
));
witness_accum.collect_fees(&Vec::new());
witness_accum.calculate_pubdata_commitment();
Expand Down
6 changes: 3 additions & 3 deletions core/bin/zksync_prometheus_exporter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::{channel::mpsc, executor::block_on, SinkExt, StreamExt};
use std::cell::RefCell;
use zksync_config::ConfigurationOptions;
use zksync_config::configs::ApiConfig;
use zksync_prometheus_exporter::run_prometheus_exporter;
use zksync_storage::ConnectionPool;

Expand All @@ -23,10 +23,10 @@ async fn main() -> anyhow::Result<()> {
}

let connection_pool = ConnectionPool::new(Some(PROMETHEUS_EXPORTER_CONNECTION_POOL_SIZE));
let config_options = ConfigurationOptions::from_env();
let api_options = ApiConfig::from_env();

let (prometheus_handle, counter_handle) =
run_prometheus_exporter(connection_pool, config_options.prometheus_export_port);
run_prometheus_exporter(connection_pool, api_options.prometheus.port);

tokio::select! {
_ = async { prometheus_handle.await } => {
Expand Down
22 changes: 13 additions & 9 deletions core/bin/zksync_witness_generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use jsonwebtoken::errors::Error as JwtError;
use jsonwebtoken::{decode, DecodingKey, Validation};
use serde::{Deserialize, Serialize};
// Workspace deps
use zksync_config::ProverOptions;
use zksync_config::configs::ZkSyncConfig;
use zksync_prover_utils::api::{BlockToProveRes, ProverReq, PublishReq, WorkingOnReq};
use zksync_storage::ConnectionPool;
use zksync_types::BlockNumber;
Expand Down Expand Up @@ -304,8 +304,12 @@ async fn required_replicas(
pub fn run_prover_server(
connection_pool: zksync_storage::ConnectionPool,
panic_notify: mpsc::Sender<bool>,
prover_options: ProverOptions,
config: ZkSyncConfig,
) {
let witness_generator_opts = config.prover.witness_generator;
let core_opts = config.prover.core;
let prover_api_opts = config.api.prover;

thread::Builder::new()
.name("prover_server".to_string())
.spawn(move || {
Expand All @@ -329,26 +333,26 @@ pub fn run_prover_server(
};

// Start pool maintainer threads.
for offset in 0..prover_options.witness_generators {
for offset in 0..witness_generator_opts.witness_generators {
let start_block = (last_verified_block + offset + 1) as u32;
let block_step = prover_options.witness_generators as u32;
let block_step = witness_generator_opts.witness_generators as u32;
log::info!(
"Starting witness generator ({},{})",
start_block,
block_step
);
let pool_maintainer = witness_generator::WitnessGenerator::new(
connection_pool.clone(),
prover_options.prepare_data_interval,
witness_generator_opts.prepare_data_interval(),
start_block,
block_step,
);
pool_maintainer.start(panic_notify.clone());
}
// Start HTTP server.
let secret_auth = prover_options.secret_auth.clone();
let gone_timeout = prover_options.gone_timeout;
let idle_provers = prover_options.idle_provers;
let secret_auth = prover_api_opts.secret_auth.clone();
let gone_timeout = core_opts.gone_timeout();
let idle_provers = core_opts.idle_provers;
HttpServer::new(move || {
let app_state = AppState::new(
secret_auth.clone(),
Expand Down Expand Up @@ -385,7 +389,7 @@ pub fn run_prover_server(
web::post().to(required_replicas),
)
})
.bind(&prover_options.prover_server_address)
.bind(&prover_api_opts.url)
.expect("failed to bind")
.run()
.await
Expand Down
6 changes: 3 additions & 3 deletions core/bin/zksync_witness_generator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::{channel::mpsc, executor::block_on, SinkExt, StreamExt};
use std::cell::RefCell;
use zksync_config::ProverOptions;
use zksync_config::configs::ZkSyncConfig;
use zksync_storage::ConnectionPool;
use zksync_witness_generator::run_prover_server;

Expand All @@ -23,9 +23,9 @@ async fn main() -> anyhow::Result<()> {
}

let connection_pool = ConnectionPool::new(Some(WITNESS_GENERATOR_CONNECTION_POOL_SIZE));
let prover_options = ProverOptions::from_env();
let config = ZkSyncConfig::from_env();

run_prover_server(connection_pool, stop_signal_sender, prover_options);
run_prover_server(connection_pool, stop_signal_sender, config);

stop_signal_receiver.next().await;

Expand Down
36 changes: 25 additions & 11 deletions core/bin/zksync_witness_generator/tests/prover_server.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Built-in deps
use std::{net, str::FromStr, thread, time, time::Duration};
use std::{thread, time, time::Duration};
// External deps
use futures::channel::mpsc;
use zksync_crypto::pairing::ff::{Field, PrimeField};
// Workspace deps
use num::BigUint;
use zksync_circuit::witness::{deposit::DepositWitness, Witness};
use zksync_config::{ConfigurationOptions, ProverOptions};
use zksync_config::configs::ZkSyncConfig;
use zksync_crypto::{params::total_tokens, proof::EncodedProofPlonk};
use zksync_prover::{client, ApiClient};
use zksync_types::{block::Block, Address};
Expand All @@ -25,11 +25,14 @@ async fn spawn_server(prover_timeout: time::Duration, rounds_interval: time::Dur
// TODO: make single server spawn for all tests (ZKS-99).
let bind_to = "127.0.0.1:8088";

let mut prover_options = ProverOptions::from_env();
prover_options.prepare_data_interval = rounds_interval;
prover_options.gone_timeout = prover_timeout;
prover_options.prover_server_address = net::SocketAddr::from_str(bind_to).unwrap();
prover_options.secret_auth = CORRECT_PROVER_SECRET_AUTH.to_string();
let mut prover_options = ZkSyncConfig::from_env();
prover_options
.prover
.witness_generator
.prepare_data_interval = rounds_interval.as_millis() as u64;
prover_options.prover.core.gone_timeout = prover_timeout.as_millis() as u64;
prover_options.api.prover.url = bind_to.into();
prover_options.api.prover.secret_auth = CORRECT_PROVER_SECRET_AUTH.to_string();

let conn_pool = connect_to_db().await;
let (tx, _rx) = mpsc::channel(1);
Expand All @@ -40,6 +43,17 @@ async fn spawn_server(prover_timeout: time::Duration, rounds_interval: time::Dur
bind_to.to_string()
}

fn supported_block_sizes() -> Vec<usize> {
ZkSyncConfig::from_env()
.chain
.circuit
.supported_block_chunks_sizes
}

fn smallest_block_size() -> usize {
supported_block_sizes()[0]
}

#[test]
#[should_panic]
fn client_with_empty_worker_name_panics() {
Expand All @@ -54,7 +68,7 @@ fn client_with_empty_worker_name_panics() {
#[tokio::test]
#[cfg_attr(not(feature = "db_test"), ignore)]
async fn client_with_incorrect_secret_auth() {
let block_size_chunks = ConfigurationOptions::from_env().available_block_chunk_sizes[0];
let block_size_chunks = smallest_block_size();
let addr = spawn_server(Duration::from_secs(1), Duration::from_secs(1)).await;
let client = client::ApiClient::new(
&format!("http://{}", &addr).parse().unwrap(),
Expand All @@ -76,7 +90,7 @@ async fn client_with_incorrect_secret_auth() {
#[tokio::test]
#[cfg_attr(not(feature = "db_test"), ignore)]
async fn api_client_register_start_and_stop_of_prover() {
let block_size_chunks = ConfigurationOptions::from_env().available_block_chunk_sizes[0];
let block_size_chunks = smallest_block_size();
let addr = spawn_server(time::Duration::from_secs(1), time::Duration::from_secs(1)).await;
let client = client::ApiClient::new(
&format!("http://{}", &addr).parse().unwrap(),
Expand Down Expand Up @@ -116,7 +130,7 @@ async fn api_client_simple_simulation() {

let addr = spawn_server(prover_timeout, rounds_interval).await;

let block_size_chunks = ConfigurationOptions::from_env().available_block_chunk_sizes[0];
let block_size_chunks = smallest_block_size();
let client = client::ApiClient::new(
&format!("http://{}", &addr).parse().unwrap(),
"foo",
Expand Down Expand Up @@ -290,7 +304,7 @@ pub async fn test_operation_and_wanted_prover_data(
validator_account_id,
ops,
(0, 1),
&ConfigurationOptions::from_env().available_block_chunk_sizes,
&supported_block_sizes(),
1_000_000.into(),
1_500_000.into(),
);
Expand Down

0 comments on commit 98bdc5e

Please sign in to comment.