diff --git a/Cargo.lock b/Cargo.lock index 6c3bff7a6af..0462ce4432a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3022,7 +3022,6 @@ dependencies = [ "anyhow", "chrono", "derive_more", - "json_comments", "near-config-utils", "near-crypto", "near-o11y", @@ -3849,7 +3848,6 @@ dependencies = [ "hyper", "hyper-tls", "indicatif", - "json_comments", "near-actix-test-utils", "near-chain", "near-chain-configs", diff --git a/core/chain-configs/Cargo.toml b/core/chain-configs/Cargo.toml index 1b394148686..31954c486dd 100644 --- a/core/chain-configs/Cargo.toml +++ b/core/chain-configs/Cargo.toml @@ -14,7 +14,6 @@ description = "This crate provides typed interfaces to the NEAR Genesis and Chai anyhow.workspace = true chrono.workspace = true derive_more.workspace = true -json_comments.workspace = true num-rational.workspace = true once_cell.workspace = true serde.workspace = true diff --git a/core/chain-configs/src/genesis_config.rs b/core/chain-configs/src/genesis_config.rs index 37c0493beab..7a3e3a49e4c 100644 --- a/core/chain-configs/src/genesis_config.rs +++ b/core/chain-configs/src/genesis_config.rs @@ -261,7 +261,11 @@ impl GenesisConfig { /// /// It panics if the contents cannot be parsed from JSON to the GenesisConfig structure. pub fn from_json(value: &str) -> Self { - serde_json::from_str(value).expect("Failed to deserialize the genesis config.") + let json_str_without_comments: String = + near_config_utils::strip_comments_from_json_str(&value.to_string()) + .expect("Failed to strip comments from genesis config."); + serde_json::from_str(&json_str_without_comments) + .expect("Failed to deserialize the genesis config.") } /// Reads GenesisConfig from a JSON file. @@ -270,11 +274,12 @@ impl GenesisConfig { /// GenesisConfig structure. pub fn from_file>(path: P) -> anyhow::Result { let mut file = File::open(path).with_context(|| "Could not open genesis config file.")?; - let mut content = String::new(); - file.read_to_string(&mut content)?; - let content_without_comments: String = near_config_utils::strip_comments_from_str(&content)?; - let genesis_config: GenesisConfig = serde_json::from_str(&content_without_comments) - .with_context(|| "Failed to deserialize the genesis records.")?; + let mut json_str = String::new(); + file.read_to_string(&mut json_str)?; + let json_str_without_comments: String = + near_config_utils::strip_comments_from_json_str(&json_str)?; + let genesis_config: GenesisConfig = serde_json::from_str(&json_str_without_comments) + .with_context(|| "Failed to deserialize the genesis config.")?; Ok(genesis_config) } @@ -315,8 +320,15 @@ impl GenesisRecords { /// It panics if file cannot be open or read, or the contents cannot be parsed from JSON to the /// GenesisConfig structure. pub fn from_file>(path: P) -> Self { - let reader = BufReader::new(File::open(path).expect("Could not open genesis config file.")); - serde_json::from_reader(reader).expect("Failed to deserialize the genesis records.") + let mut file = File::open(path).expect("Failed to open genesis config file."); + let mut json_str = String::new(); + file.read_to_string(&mut json_str) + .expect("Failed to read the genesis config file to string. "); + let json_str_without_comments: String = + near_config_utils::strip_comments_from_json_str(&json_str) + .expect("Failed to strip comments from Genesis config file."); + serde_json::from_str(&json_str_without_comments) + .expect("Failed to deserialize the genesis records.") } /// Writes GenesisRecords to the file. @@ -400,7 +412,8 @@ pub fn stream_records_from_file( reader: impl Read, mut callback: impl FnMut(StateRecord), ) -> serde_json::Result<()> { - let mut deserializer = serde_json::Deserializer::from_reader(reader); + let reader_without_comments = near_config_utils::strip_comments_from_json_reader(reader); + let mut deserializer = serde_json::Deserializer::from_reader(reader_without_comments); let records_processor = RecordsProcessor { sink: &mut callback }; deserializer.deserialize_any(records_processor) } @@ -452,9 +465,14 @@ impl Genesis { /// Reads Genesis from a single file. pub fn from_file>(path: P, genesis_validation: GenesisValidationMode) -> Self { - let reader = BufReader::new(File::open(path).expect("Could not open genesis config file.")); - let genesis: Genesis = - serde_json::from_reader(reader).expect("Failed to deserialize the genesis records."); + let mut file = File::open(path).expect("Could not open genesis config file."); + let mut json_str = String::new(); + file.read_to_string(&mut json_str).expect("Failed to read genesis config file to string. "); + let json_str_without_comments: String = + near_config_utils::strip_comments_from_json_str(&json_str) + .expect("Failed to strip comments from Genesis config file."); + let genesis: Genesis = serde_json::from_str(&json_str_without_comments) + .expect("Failed to deserialize the genesis records."); // As serde skips the `records_file` field, we can assume that `Genesis` has `records` and // doesn't have `records_file`. Self::new_validated(genesis.config, genesis.records, genesis_validation) diff --git a/core/crypto/src/key_file.rs b/core/crypto/src/key_file.rs index bffa1632ffb..2e2182d124d 100644 --- a/core/crypto/src/key_file.rs +++ b/core/crypto/src/key_file.rs @@ -1,8 +1,8 @@ +use serde::{Deserialize, Serialize}; use std::fs::File; use std::io; use std::io::{Read, Write}; use std::path::Path; -use serde::{Deserialize, Serialize}; use crate::{PublicKey, SecretKey}; use near_account_id::AccountId; @@ -38,11 +38,12 @@ impl KeyFile { pub fn from_file(path: &Path) -> io::Result { let mut file = File::open(path)?; - let mut content = String::new(); - file.read_to_string(&mut content)?; - let content_without_comments: String = near_config_utils::strip_comments_from_str(&content)?; + let mut json_config_str = String::new(); + file.read_to_string(&mut json_config_str)?; + let json_str_without_comments: String = + near_config_utils::strip_comments_from_json_str(&json_config_str)?; - Ok(serde_json::from_str(&content_without_comments)?) + Ok(serde_json::from_str(&json_str_without_comments)?) } } diff --git a/nearcore/Cargo.toml b/nearcore/Cargo.toml index 3955c2cc4ce..bc070ec74f4 100644 --- a/nearcore/Cargo.toml +++ b/nearcore/Cargo.toml @@ -20,7 +20,6 @@ futures.workspace = true hyper-tls.workspace = true hyper.workspace = true indicatif.workspace = true -json_comments.workspace = true num-rational.workspace = true once_cell.workspace = true rand.workspace = true diff --git a/nearcore/src/config.rs b/nearcore/src/config.rs index f06eca15fd8..681777f3fac 100644 --- a/nearcore/src/config.rs +++ b/nearcore/src/config.rs @@ -395,12 +395,12 @@ impl Default for Config { impl Config { pub fn from_file(path: &Path) -> anyhow::Result { - let contents = std::fs::read_to_string(path) + let json_str = std::fs::read_to_string(path) .with_context(|| format!("Failed to read config from {}", path.display()))?; let mut unrecognised_fields = Vec::new(); - let contents_without_comments = near_config_utils::strip_comments_from_str(&contents)?; + let json_str_without_comments = near_config_utils::strip_comments_from_json_str(&json_str)?; let config: Config = serde_ignored::deserialize( - &mut serde_json::Deserializer::from_str(&contents_without_comments), + &mut serde_json::Deserializer::from_str(&json_str_without_comments), |field| { let field = field.to_string(); // TODO(mina86): Remove this deprecation notice some time by the @@ -1300,12 +1300,12 @@ struct NodeKeyFile { impl NodeKeyFile { fn from_file(path: &Path) -> std::io::Result { let mut file = File::open(path)?; - let mut content = String::new(); - file.read_to_string(&mut content)?; - - let content_without_comments = near_config_utils::strip_comments_from_str(&content)?; + let mut json_str = String::new(); + file.read_to_string(&mut json_str)?; - Ok(serde_json::from_str(&content_without_comments)?) + let json_str_without_comments = near_config_utils::strip_comments_from_json_str(&json_str)?; + + Ok(serde_json::from_str(&json_str_without_comments)?) } } diff --git a/nearcore/src/dyn_config.rs b/nearcore/src/dyn_config.rs index eca1f4a877d..8875462ab3f 100644 --- a/nearcore/src/dyn_config.rs +++ b/nearcore/src/dyn_config.rs @@ -59,20 +59,21 @@ where for<'a> T: Deserialize<'a>, { match std::fs::read_to_string(path) { - Ok(config_str) => { - match near_config_utils::strip_comments_from_str(&config_str) { - Ok(content_without_comments) => { - match serde_json::from_str::(&content_without_comments) { - Ok(config) => { - tracing::info!(target: "neard", config=?config, "Changing the config {path:?}."); - return Ok(Some(config)); - }, - Err(err) => Err(UpdateableConfigLoaderError::Parse { file: path.to_path_buf(), err }), + Ok(config_str) => match near_config_utils::strip_comments_from_json_str(&config_str) { + Ok(config_str_without_comments) => { + match serde_json::from_str::(&config_str_without_comments) { + Ok(config) => { + tracing::info!(target: "neard", config=?config, "Changing the config {path:?}."); + return Ok(Some(config)); } - }, - Err(err) => Err(UpdateableConfigLoaderError::OpenAndRead { file: path.to_path_buf(), err }), + Err(err) => { + Err(UpdateableConfigLoaderError::Parse { file: path.to_path_buf(), err }) + } + } + } + Err(err) => { + Err(UpdateableConfigLoaderError::OpenAndRead { file: path.to_path_buf(), err }) } - }, Err(err) => match err.kind() { std::io::ErrorKind::NotFound => { diff --git a/nearcore/src/lib.rs b/nearcore/src/lib.rs index b40a94b2f3d..53670181842 100644 --- a/nearcore/src/lib.rs +++ b/nearcore/src/lib.rs @@ -404,4 +404,4 @@ pub fn recompress_storage(home_dir: &Path, opts: RecompressOpts) -> anyhow::Resu info!(target: "recompress", dest = %dst_path.display(), "Database recompressed"); Ok(()) -} \ No newline at end of file +} diff --git a/utils/config/src/lib.rs b/utils/config/src/lib.rs index de8257aea7c..8bdac440614 100644 --- a/utils/config/src/lib.rs +++ b/utils/config/src/lib.rs @@ -2,8 +2,12 @@ use std::io::Read; use json_comments::StripComments; -pub fn strip_comments_from_str(input: &String) -> std::io::Result { +pub fn strip_comments_from_json_str(json_str: &String) -> std::io::Result { let mut content_without_comments = String::new(); - StripComments::new(input.as_bytes()).read_to_string(&mut content_without_comments)?; + StripComments::new(json_str.as_bytes()).read_to_string(&mut content_without_comments)?; Ok(content_without_comments) -} \ No newline at end of file +} + +pub fn strip_comments_from_json_reader(reader: impl Read) -> impl Read { + StripComments::new(reader) +}