Skip to content

Commit

Permalink
add strip comments for reader and apply strip comments on more functi…
Browse files Browse the repository at this point in the history
…ons for genesis config
  • Loading branch information
ppca committed Feb 1, 2023
1 parent bd1eb8a commit 9765f8e
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 45 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion core/chain-configs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 30 additions & 12 deletions core/chain-configs/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -270,11 +274,12 @@ impl GenesisConfig {
/// GenesisConfig structure.
pub fn from_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
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)
}

Expand Down Expand Up @@ -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<P: AsRef<Path>>(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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -452,9 +465,14 @@ impl Genesis {

/// Reads Genesis from a single file.
pub fn from_file<P: AsRef<Path>>(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)
Expand Down
11 changes: 6 additions & 5 deletions core/crypto/src/key_file.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,11 +38,12 @@ impl KeyFile {

pub fn from_file(path: &Path) -> io::Result<Self> {
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)?)
}
}

Expand Down
1 change: 0 additions & 1 deletion nearcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,12 @@ impl Default for Config {

impl Config {
pub fn from_file(path: &Path) -> anyhow::Result<Self> {
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
Expand Down Expand Up @@ -1300,12 +1300,12 @@ struct NodeKeyFile {
impl NodeKeyFile {
fn from_file(path: &Path) -> std::io::Result<Self> {
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)?)
}
}

Expand Down
25 changes: 13 additions & 12 deletions nearcore/src/dyn_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>(&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::<T>(&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 => {
Expand Down
2 changes: 1 addition & 1 deletion nearcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
10 changes: 7 additions & 3 deletions utils/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use std::io::Read;

use json_comments::StripComments;

pub fn strip_comments_from_str(input: &String) -> std::io::Result<String> {
pub fn strip_comments_from_json_str(json_str: &String) -> std::io::Result<String> {
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)
}
}

pub fn strip_comments_from_json_reader(reader: impl Read) -> impl Read {
StripComments::new(reader)
}

0 comments on commit 9765f8e

Please sign in to comment.