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: Update neard init to specify config file type to download #12072

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions chain/indexer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![doc = include_str!("../README.md")]

use anyhow::Context;
use nearcore::config::DownloadConfigType;
use std::str::FromStr;
use tokio::sync::mpsc;

use near_chain_configs::GenesisValidationMode;
Expand Down Expand Up @@ -43,7 +45,7 @@ pub struct InitConfigArgs {
/// Specify a custom download URL for the records file.
pub download_records_url: Option<String>,
/// Download the verified NEAR config file automatically.
pub download_config: bool,
pub download_config: Option<String>,
tayfunelmas marked this conversation as resolved.
Show resolved Hide resolved
/// Specify a custom download URL for the config file.
pub download_config_url: Option<String>,
/// Specify the boot nodes to bootstrap the network
Expand Down Expand Up @@ -157,6 +159,11 @@ pub fn indexer_init_configs(
dir: &std::path::PathBuf,
params: InitConfigArgs,
) -> Result<(), anyhow::Error> {
let download_config_type = if let Some(config_type) = params.download_config.as_deref() {
Some(DownloadConfigType::from_str(config_type)?)
} else {
None
};
init_configs(
dir,
params.chain_id,
Expand All @@ -168,7 +175,7 @@ pub fn indexer_init_configs(
params.download_genesis,
params.download_genesis_url.as_deref(),
params.download_records_url.as_deref(),
params.download_config,
download_config_type,
params.download_config_url.as_deref(),
params.boot_nodes.as_deref(),
params.max_gas_burnt_view,
Expand Down
53 changes: 44 additions & 9 deletions nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,41 @@ fn set_block_production_delay(chain_id: &str, fast: bool, config: &mut Config) {
}
}

/// Type of the configuration to download.
pub enum DownloadConfigType {
/// Validator node configuration.
Validator,
/// Non-validator RPC node configuration.
Rpc,
tayfunelmas marked this conversation as resolved.
Show resolved Hide resolved
/// Non-validator archival node configuration.
Archival,
}

impl ToString for DownloadConfigType {
fn to_string(&self) -> String {
match self {
DownloadConfigType::Validator => "validator".to_string(),
DownloadConfigType::Rpc => "rpc".to_string(),
DownloadConfigType::Archival => "archival".to_string(),
}
}
}

impl FromStr for DownloadConfigType {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim().to_lowercase().as_str() {
"validator" => Ok(DownloadConfigType::Validator),
"rpc" => Ok(DownloadConfigType::Rpc),
"archival" => Ok(DownloadConfigType::Archival),
_ => anyhow::bail!(
"Flag download_config must be one of the following: validator, rpc, archival"
),
}
}
}

/// Initializes Genesis, client Config, node and validator keys, and stores in the specified folder.
///
/// This method supports the following use cases:
Expand All @@ -811,7 +846,7 @@ pub fn init_configs(
should_download_genesis: bool,
download_genesis_url: Option<&str>,
download_records_url: Option<&str>,
should_download_config: bool,
download_config_type: Option<DownloadConfigType>,
download_config_url: Option<&str>,
boot_nodes: Option<&str>,
max_gas_burnt_view: Option<Gas>,
Expand Down Expand Up @@ -853,8 +888,8 @@ pub fn init_configs(
download_config(url, &dir.join(CONFIG_FILENAME))
.context(format!("Failed to download the config file from {}", url))?;
config = Config::from_file(&dir.join(CONFIG_FILENAME))?;
} else if should_download_config {
let url = get_config_url(&chain_id);
} else if let Some(config_type) = download_config_type {
let url = get_config_url(&chain_id, config_type);
download_config(&url, &dir.join(CONFIG_FILENAME))
.context(format!("Failed to download the config file from {}", url))?;
config = Config::from_file(&dir.join(CONFIG_FILENAME))?;
Expand Down Expand Up @@ -1328,10 +1363,10 @@ pub fn get_records_url(chain_id: &str) -> String {
)
}

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

Expand Down Expand Up @@ -1544,7 +1579,7 @@ mod tests {
false,
None,
None,
false,
None,
None,
None,
None,
Expand Down Expand Up @@ -1602,7 +1637,7 @@ mod tests {
false,
None,
None,
false,
None,
None,
None,
None,
Expand Down Expand Up @@ -1635,7 +1670,7 @@ mod tests {
false,
None,
None,
false,
None,
None,
None,
None,
Expand Down
16 changes: 13 additions & 3 deletions neard/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ use near_state_viewer::StateViewerSubCommand;
use near_store::db::RocksDB;
use near_store::Mode;
use near_undo_block::cli::UndoBlockCommand;
use nearcore::config::DownloadConfigType;
use serde_json::Value;
use std::fs::File;
use std::io::BufReader;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::broadcast;
use tokio::sync::broadcast::Receiver;
Expand Down Expand Up @@ -259,8 +261,10 @@ pub(super) struct InitCmd {
#[clap(long)]
download_genesis: bool,
/// Download the verified NEAR config file automatically.
#[clap(long)]
download_config: bool,
/// Can be one of "validator", "rpc", and "archival".
/// If flag is present with no value, defaults to "validator".
#[clap(long, default_missing_value = "validator", num_args(0..=1))]
download_config: Option<String>,
/// Makes block production fast (TESTING ONLY).
#[clap(long)]
fast: bool,
Expand Down Expand Up @@ -347,6 +351,12 @@ impl InitCmd {
check_release_build(chain)
}

let download_config_type = if let Some(config_type) = self.download_config.as_deref() {
Some(DownloadConfigType::from_str(config_type)?)
} else {
None
};

nearcore::init_configs(
home_dir,
self.chain_id,
Expand All @@ -358,7 +368,7 @@ impl InitCmd {
self.download_genesis,
self.download_genesis_url.as_deref(),
self.download_records_url.as_deref(),
self.download_config,
download_config_type,
self.download_config_url.as_deref(),
self.boot_nodes.as_deref(),
self.max_gas_burnt_view,
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime-params-estimator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn run_estimation(cli_args: CliArgs) -> anyhow::Result<Option<CostTable>> {
false,
None,
None,
false,
None,
None,
None,
None,
Expand Down
3 changes: 2 additions & 1 deletion tools/chainsync-loadtest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use near_o11y::tracing::{error, info};
use near_primitives::block::GenesisId;
use near_primitives::hash::CryptoHash;
use nearcore::config;
use nearcore::config::DownloadConfigType;
use nearcore::config::NearConfig;
use network::Network;
use openssl_probe;
Expand Down Expand Up @@ -59,7 +60,7 @@ pub fn start_with_config(config: NearConfig, qps_limit: u32) -> anyhow::Result<A
fn download_configs(chain_id: &str, dir: &std::path::Path) -> anyhow::Result<NearConfig> {
// Always fetch the config.
std::fs::create_dir_all(dir)?;
let url = config::get_config_url(chain_id);
let url = config::get_config_url(chain_id, DownloadConfigType::Rpc);
let config_path = &dir.join(config::CONFIG_FILENAME);
config::download_config(&url, config_path)?;
let config = config::Config::from_file(config_path)?;
Expand Down
9 changes: 8 additions & 1 deletion tools/indexer/example/src/configs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use near_indexer::near_primitives::types::Gas;

/// Indexer uses the RPC configuration type.
const DOWNLOAD_CONFIG_TYPE: &str = "rpc";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khorolets please check here. simply I do not change --download-config flag, it is still boolean, but I map it to rpc config if it is specified.


/// NEAR Indexer Example
/// Watches for stream of blocks from the chain
#[derive(clap::Parser, Debug)]
Expand Down Expand Up @@ -76,7 +79,11 @@ impl From<InitConfigArgs> for near_indexer::InitConfigArgs {
download_genesis: config_args.download_genesis,
download_genesis_url: config_args.download_genesis_url,
download_records_url: config_args.download_records_url,
download_config: config_args.download_config,
download_config: if config_args.download_config {
Some(DOWNLOAD_CONFIG_TYPE.to_string())
} else {
None
},
download_config_url: config_args.download_config_url,
boot_nodes: config_args.boot_nodes,
max_gas_burnt_view: config_args.max_gas_burnt_view,
Expand Down
Loading