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

feat(cli): Implemented the download config method to the init command #4427

Merged
merged 13 commits into from
Jul 2, 2021
Merged
12 changes: 9 additions & 3 deletions chain/indexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ pub struct InitConfigArgs {
/// Genesis file to use when initializing testnet (including downloading)
pub genesis: Option<String>,
/// Download the verified NEAR genesis file automatically.
pub download: bool,
/// Specify a custom download URL for the genesis-file.
pub should_download_genesis: bool,
/// Specify a custom download URL for the genesis file.
pub download_genesis_url: Option<String>,
/// Download the verified NEAR config file automtically.
pub should_download_config: bool,
/// Specify a custom download URL for the config file.
pub download_config_url: Option<String>,
/// Specify a custom max_gas_burnt_view limit.
pub max_gas_burnt_view: Option<Gas>,
}
Expand Down Expand Up @@ -136,8 +140,10 @@ pub fn indexer_init_configs(dir: &std::path::PathBuf, params: InitConfigArgs) {
params.num_shards,
params.fast,
params.genesis.as_deref(),
params.download,
params.should_download_genesis,
params.download_genesis_url.as_deref(),
params.should_download_config,
params.download_config_url.as_deref(),
params.max_gas_burnt_view,
)
}
53 changes: 48 additions & 5 deletions nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,10 @@ pub fn init_configs(
num_shards: NumShards,
fast: bool,
genesis: Option<&str>,
download: bool,
should_download_genesis: bool,
chefsale marked this conversation as resolved.
Show resolved Hide resolved
download_genesis_url: Option<&str>,
should_download_config: bool,
download_config_url: Option<&str>,
max_gas_burnt_view: Option<Gas>,
) {
fs::create_dir_all(dir).expect("Failed to create directory");
Expand All @@ -801,13 +803,25 @@ pub fn init_configs(
let genesis_config = GenesisConfig::from_file(&dir.join(config.genesis_file));
panic!("Found existing config in {} with chain-id = {}. Use unsafe_reset_all to clear the folder.", dir.to_str().unwrap(), genesis_config.chain_id);
}

let mut config = Config::default();
let chain_id = chain_id
.and_then(|c| if c.is_empty() { None } else { Some(c.to_string()) })
.unwrap_or_else(random_chain_id);
let mut config = Config::default();

if let Some(url) = download_config_url {
download_config(&url.to_string(), &dir.join(CONFIG_FILENAME));
config = Config::from_file(&dir.join(CONFIG_FILENAME));
} else if should_download_config {
let url = get_config_url(&chain_id);
download_config(&url, &dir.join(CONFIG_FILENAME));
config = Config::from_file(&dir.join(CONFIG_FILENAME));
}

if max_gas_burnt_view.is_some() {
config.max_gas_burnt_view = max_gas_burnt_view;
}

match chain_id.as_ref() {
"mainnet" => {
if test_seed.is_some() {
Expand All @@ -816,7 +830,6 @@ pub fn init_configs(
config.telemetry.endpoints.push(MAINNET_TELEMETRY_URL.to_string());
config.write_to_file(&dir.join(CONFIG_FILENAME));

// TODO: add download genesis for mainnet
let genesis: Genesis = serde_json::from_slice(*MAINNET_GENESIS_JSON)
.expect("Failed to deserialize MainNet genesis");
if let Some(account_id) = account_id {
Expand Down Expand Up @@ -850,7 +863,7 @@ pub fn init_configs(

if let Some(url) = download_genesis_url {
download_genesis(&url.to_string(), &genesis_path);
} else if download {
} else if should_download_genesis {
let url = get_genesis_url(&chain_id);
download_genesis(&url, &genesis_path);
} else {
Expand Down Expand Up @@ -1058,9 +1071,15 @@ pub fn get_genesis_url(chain_id: &String) -> String {
)
}

pub fn get_config_url(chain_id: &String) -> String {
format!(
"https://s3-us-west-1.amazonaws.com/build.nearprotocol.com/nearcore-deploy/{}/config.json",
chain_id,
)
}

pub fn download_genesis(url: &String, path: &PathBuf) {
info!(target: "near", "Downloading genesis file from: {} ...", url);

let url = url.clone();
let path = path.clone();

Expand All @@ -1077,6 +1096,30 @@ pub fn download_genesis(url: &String, path: &PathBuf) {
.await
.expect("Genesis file is bigger than 10GB. Please make the limit higher.");

std::fs::write(&path, &body).expect("Failed to create / write a genesis file.");

info!(target: "near", "Saved the genesis file to: {} ...", path.as_path().display());
});
}

pub fn download_config(url: &String, path: &PathBuf) {
chefsale marked this conversation as resolved.
Show resolved Hide resolved
info!(target: "near", "Downloading config file from: {} ...", url);
let url = url.clone();
let path = path.clone();

actix::System::new().block_on(async move {
let client = awc::Client::new();
let mut response =
client.get(url).send().await.expect("Unable to download the config file");

// IMPORTANT: limit specifies the maximum size of the genesis
chefsale marked this conversation as resolved.
Show resolved Hide resolved
// In case where the config is bigger than the specified limit Overflow Error is thrown
let body = response
.body()
.limit(10_000)
chefsale marked this conversation as resolved.
Show resolved Hide resolved
.await
.expect("Config file is bigger than 10MB. Please make the limit higher.");

std::fs::write(&path, &body).expect("Failed to create / write a config file.");

info!(target: "near", "Saved the config file to: {} ...", path.as_path().display());
Expand Down
10 changes: 9 additions & 1 deletion neard/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ pub(super) struct InitCmd {
/// Download the verified NEAR genesis file automatically.
#[clap(long)]
download_genesis: bool,
/// Download the verified NEAR config file automatically.
#[clap(long)]
download_config: bool,
/// Makes block production fast (TESTING ONLY).
#[clap(long)]
fast: bool,
Expand All @@ -111,9 +114,12 @@ pub(super) struct InitCmd {
/// Chain ID, by default creates new random.
#[clap(long)]
chain_id: Option<String>,
/// Specify a custom download URL for the genesis-file.
/// Specify a custom download URL for the genesis file.
#[clap(long)]
download_genesis_url: Option<String>,
/// Specify a custom download URL for the config file.
#[clap(long)]
download_config_url: Option<String>,
/// Genesis file to use when initializing testnet (including downloading).
#[clap(long)]
genesis: Option<String>,
Expand Down Expand Up @@ -149,6 +155,8 @@ impl InitCmd {
self.genesis.as_deref(),
self.download_genesis,
self.download_genesis_url.as_deref(),
self.download_config,
self.download_config_url.as_deref(),
self.max_gas_burnt_view,
);
}
Expand Down
12 changes: 10 additions & 2 deletions tools/indexer/example/src/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ pub(crate) struct InitConfigArgs {
pub genesis: Option<String>,
#[clap(short, long)]
/// Download the verified NEAR genesis file automatically.
pub download: bool,
pub should_download_genesis: bool,
/// Specify a custom download URL for the genesis-file.
#[clap(long)]
pub download_genesis_url: Option<String>,
#[clap(short, long)]
chefsale marked this conversation as resolved.
Show resolved Hide resolved
/// Download the verified NEAR config file automatically.
pub should_download_config: bool,
/// Specify a custom download URL for the config file.
#[clap(long)]
pub download_config_url: Option<String>,
/// Specify a custom max_gas_burnt_view limit.
#[clap(long)]
pub max_gas_burnt_view: Option<Gas>,
Expand All @@ -74,8 +80,10 @@ impl From<InitConfigArgs> for near_indexer::InitConfigArgs {
num_shards: config_args.num_shards,
fast: config_args.fast,
genesis: config_args.genesis,
download: config_args.download,
should_download_genesis: config_args.should_download_genesis,
download_genesis_url: config_args.download_genesis_url,
should_download_config: config_args.should_download_config,
download_config_url: config_args.download_config_url,
max_gas_burnt_view: config_args.max_gas_burnt_view,
}
}
Expand Down