Skip to content

Commit

Permalink
implemented fetching of setup public settings for contributor, update…
Browse files Browse the repository at this point in the history
…d tokio to 1.6
  • Loading branch information
ibaryshnikov committed May 14, 2021
1 parent 48e84aa commit 2ca8c58
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 65 deletions.
232 changes: 208 additions & 24 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ members = [
"setup2",
"setup-utils",
]
exclude = [
"setup1-shared",
]

[profile.release]
opt-level = 3
Expand Down
5 changes: 3 additions & 2 deletions setup1-contributor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ path = "src/main.rs"
phase1 = { path = "../phase1" }
phase1-cli = { path = "../phase1-cli" }
phase1-coordinator = { path = "../phase1-coordinator", features = ["operator"]}
setup1-shared = { version = "0.1", path = "../setup1-shared" }
setup-utils = { path = "../setup-utils" }

snarkos-toolkit = { git = "https://github.com/AleoHQ/snarkOS", rev = "6357695", package = "snarkos-toolkit", default-features = false }
Expand All @@ -35,14 +36,14 @@ indicatif = { version = "0.15.0" }
lazy_static = { version = "1.4.0" }
panic-control = {version = "0.1.4" }
rand = { version = "0.7" }
reqwest = { version = "0.10", features = [ "blocking", "stream", "json" ] }
reqwest = { version = "0.11", features = [ "blocking", "stream", "json" ] }
secrecy = { version = "0.7" }
serde = { version = "1.0", features = [ "derive" ] }
serde_derive = { version = "1.0" }
serde_json = { version = "1.0" }
structopt = { version = "0.3" }
thiserror = { version = "1.0" }
tokio = { version = "0.2", features = [ "full" ] }
tokio = { version = "1.6", features = [ "macros", "rt-multi-thread" ] }
tracing = { version = "0.1" }
tracing-appender = { version = "0.1.1" }
tracing-subscriber = { version = "0.2" }
Expand Down
11 changes: 1 addition & 10 deletions setup1-contributor/src/cli/commands/contribute.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::utils::{environment_variants, parse_environment, UploadMode};
use crate::utils::UploadMode;

use clap::AppSettings;
use phase1_coordinator::environment::Environment;
use secrecy::SecretString;
use structopt::StructOpt;
use url::Url;
Expand Down Expand Up @@ -32,14 +31,6 @@ pub struct ContributeOptions {
#[structopt(long)]
pub passphrase: Option<SecretString>,

/// Specify the contribution environment.
#[structopt(
rename_all = "screaming-snake-case",
possible_values = environment_variants(),
parse(try_from_str = parse_environment),
)]
pub environment: Environment,

/// Specify the URL of the ceremony coordinator.
#[structopt(rename_all = "screaming-snake-case")]
pub coordinator_api_url: Url,
Expand Down
52 changes: 38 additions & 14 deletions setup1-contributor/src/commands/contribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use phase1_coordinator::{
environment::Environment,
objects::{Chunk, Participant, Round},
};
use setup1_shared::structures::PublicSettings;
use setup_utils::calculate_hash;
use snarkos_toolkit::account::{Address, PrivateKey, ViewKey};
use zexe_algebra::{Bls12_377, PairingEngine, BW6_761};
Expand All @@ -45,7 +46,7 @@ use std::{
str::FromStr,
sync::{Arc, RwLock},
};
use tokio::time::{delay_for, Instant};
use tokio::time::{sleep, Instant};
use tracing::{debug, error, info, warn};
use url::Url;

Expand Down Expand Up @@ -107,7 +108,7 @@ pub struct Contribute {
}

impl Contribute {
pub fn new(opts: &ContributeOptions, private_key: &[u8]) -> Result<Self> {
pub fn new(opts: &ContributeOptions, environment: &Environment, private_key: &[u8]) -> Result<Self> {
let private_key = PrivateKey::from_str(std::str::from_utf8(&private_key)?)?;

// TODO (raychu86): Pass in pipelining options from the CLI.
Expand All @@ -117,7 +118,7 @@ impl Contribute {
participant_id: Address::from(&private_key)?.to_string(),
private_key: private_key.to_string(),
upload_mode: opts.upload_mode.clone(),
environment: opts.environment.clone(),
environment: environment.clone(),

challenge_filename: CHALLENGE_FILENAME.to_string(),
challenge_hash_filename: CHALLENGE_HASH_FILENAME.to_string(),
Expand Down Expand Up @@ -178,7 +179,7 @@ impl Contribute {
progress_bar.set_message(&format!("Could not update status: {}", e.to_string().trim()));
}
}
delay_for(delay_poll_ceremony_duration).await;
sleep(delay_poll_ceremony_duration).await;
}
});

Expand Down Expand Up @@ -219,11 +220,11 @@ impl Contribute {
}
}

delay_for(delay_duration).await;
sleep(delay_duration).await;
}
});
futures.push(join_handle);
delay_for(Duration::seconds(DELAY_WAIT_FOR_PIPELINE_SECS).to_std()?).await;
sleep(Duration::seconds(DELAY_WAIT_FOR_PIPELINE_SECS).to_std()?).await;
}

let cloned = self.clone();
Expand All @@ -232,7 +233,7 @@ impl Contribute {
// of the other tasks.
std::thread::spawn(move || {
let auth_rng = &mut rand::rngs::OsRng;
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
loop {
tracing::info!("Performing heartbeat.");
if let Err(error) = runtime.block_on(cloned.heartbeat(auth_rng)) {
Expand Down Expand Up @@ -311,7 +312,7 @@ impl Contribute {
return Ok(());
}
}
delay_for(Duration::seconds(DELAY_WAIT_FOR_PIPELINE_SECS).to_std()?).await;
sleep(Duration::seconds(DELAY_WAIT_FOR_PIPELINE_SECS).to_std()?).await;
}
}

Expand Down Expand Up @@ -385,7 +386,7 @@ impl Contribute {
loop {
match self.move_task_from_lane_to_lane(from, to, task)? {
true => return Ok(()),
false => delay_for(Duration::seconds(DELAY_WAIT_FOR_PIPELINE_SECS).to_std()?).await,
false => sleep(Duration::seconds(DELAY_WAIT_FOR_PIPELINE_SECS).to_std()?).await,
}
}
}
Expand All @@ -394,7 +395,7 @@ impl Contribute {
loop {
match self.add_task_to_download_lane(task)? {
true => return Ok(()),
false => delay_for(Duration::seconds(DELAY_WAIT_FOR_PIPELINE_SECS).to_std()?).await,
false => sleep(Duration::seconds(DELAY_WAIT_FOR_PIPELINE_SECS).to_std()?).await,
}
}
}
Expand Down Expand Up @@ -458,7 +459,7 @@ impl Contribute {
remove_file_if_exists(&self.response_hash_filename)?;
return Ok(());
} else {
tokio::time::delay_for(Duration::seconds(DELAY_POLL_CEREMONY_SECS).to_std()?).await;
tokio::time::sleep(Duration::seconds(DELAY_POLL_CEREMONY_SECS).to_std()?).await;
continue;
}
}
Expand Down Expand Up @@ -876,7 +877,29 @@ fn read_keys<P: AsRef<Path>>(keys_path: P, passphrase: Option<SecretString>) ->
Ok((aleo_seed, aleo_private_key))
}

pub async fn start_contributor(opts: ContributeOptions) {
async fn request_coordinator_public_settings(coordinator_url: &Url) -> anyhow::Result<PublicSettings> {
let settings_endpoint_url = coordinator_url.join("/v1/contributor/settings")?;
let client = reqwest::Client::new();
let bytes = client.post(settings_endpoint_url).send().await?.bytes().await?;
PublicSettings::decode(&bytes.to_vec())
.map_err(|e| anyhow::anyhow!("Error decoding coordinator PublicSettings: {}", e))
}

pub async fn contribute_subcommand(opts: &ContributeOptions) -> anyhow::Result<()> {
let public_settings = request_coordinator_public_settings(&opts.coordinator_api_url)
.await
.map_err(|e| {
tracing::error!("Can't get the coordinator public settings, got error: {}", e);
e
})?;

let environment = crate::utils::environment_by_setup_kind(&public_settings.setup);

start_contributor(opts, &environment).await;
Ok(())
}

async fn start_contributor(opts: &ContributeOptions, environment: &Environment) {
// Initialize tracing logger. Stored to `aleo-setup.log`.
let appender = tracing_appender::rolling::never(".", "aleo-setup.log");
let (non_blocking, _guard) = tracing_appender::non_blocking(appender);
Expand All @@ -888,10 +911,11 @@ pub async fn start_contributor(opts: ContributeOptions) {

*SEED.write().expect("Should have been able to write seed") = Some(Arc::new(seed));

let curve_kind = opts.environment.parameters().curve();
let curve_kind = environment.parameters().curve();

// Initialize the contributor.
let contribute = Contribute::new(&opts, private_key.expose_secret()).expect("Unable to initialize a contributor");
let contribute =
Contribute::new(opts, environment, private_key.expose_secret()).expect("Unable to initialize a contributor");

// Run the contributor.
let contribution = match curve_kind {
Expand Down
4 changes: 2 additions & 2 deletions setup1-contributor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use setup1_contributor::{
cli::{Command, Options},
commands::{generate_keys, start_contributor},
commands::{contribute_subcommand, generate_keys},
};

use i18n_embed::{DesktopLanguageRequester, LanguageRequester};
Expand All @@ -18,7 +18,7 @@ async fn main() -> anyhow::Result<()> {
match opts.subcommand {
Command::Generate(generate_opts) => generate_keys(generate_opts),
Command::Contribute(contribute_opts) => {
start_contributor(contribute_opts).await;
contribute_subcommand(&contribute_opts).await?;
}
}

Expand Down
21 changes: 8 additions & 13 deletions setup1-contributor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use phase1_coordinator::{
environment::{Development, Environment, Parameters, Production},
objects::{ContributionFileSignature, ContributionState},
};
use setup1_shared::structures::SetupKind;
use snarkos_toolkit::account::{Address, PrivateKey, ViewKey};
use zexe_algebra::PairingEngine;

Expand Down Expand Up @@ -196,19 +197,13 @@ fn universal_environment() -> Environment {
Production::from(Parameters::AleoUniversal).into()
}

/// Available environment command line argument variants.
pub fn environment_variants() -> &'static [&'static str] {
&["development", "inner", "outer", "universal"]
}

/// Parse [Environment] from command line argument string.
pub fn parse_environment(s: &str) -> Result<Environment, String> {
match s {
"development" => Ok(development_environment()),
"inner" => Ok(inner_environment()),
"outer" => Ok(outer_environment()),
"universal" => Ok(universal_environment()),
unexpected => Err(format!("Could not parse {:?} as EnvirnomentArg", unexpected)),
/// Returns the [Environment] settings based on a setup kind
pub fn environment_by_setup_kind(kind: &SetupKind) -> Environment {
match kind {
SetupKind::Development => development_environment(),
SetupKind::Inner => inner_environment(),
SetupKind::Outer => outer_environment(),
SetupKind::Universal => universal_environment(),
}
}

Expand Down
87 changes: 87 additions & 0 deletions setup1-shared/Cargo.lock

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

9 changes: 9 additions & 0 deletions setup1-shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "setup1-shared"
version = "0.1.0"
authors = ["The Aleo Team <[email protected]>"]
edition = "2018"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
25 changes: 25 additions & 0 deletions setup1-shared/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Shared structures and functions for Aleo setup 1

## Description

There are some data structures which we reuse in coordinator,
contributor and verifier. For example, public settings of a verifier.
This package is a home of such structures. It may as well help
to specify a communication protocol a bit better, for example
which format of message encoding we are using, and to contain
the encode/decode functions for the shared structures.

## Usage

Check the doc comments provided by the data structures and helper functions.
Must be used with compatible versions of **serde** and **serde_json**.

## Error types

Right now the errors in encode/decode functions are the same as returned
by **serde_json**

## Workspace and lockfile

Since it's a library, it doesn't have a lockfile. There's no benefit in including it
in a workspace either, because it is not supposed to be built alone.
1 change: 1 addition & 0 deletions setup1-shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod structures;
Loading

0 comments on commit 2ca8c58

Please sign in to comment.