Skip to content

Commit

Permalink
strip comments out from json for genesis_config.json, config.json, va…
Browse files Browse the repository at this point in the history
…lidator_key.json and node_key.json
  • Loading branch information
ppca committed Jan 30, 2023
1 parent f5ab615 commit 435aeb4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion core/chain-configs/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl GenesisConfig {
pub fn from_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
let file = File::open(path).with_context(|| "Could not open genesis config file.")?;
let reader = BufReader::new(file);
// Strip the comments from the input (use `as_bytes()` to get a `Read`).
// Strip the comments from the input
let stripped = StripComments::new(reader);
let genesis_config: GenesisConfig = serde_json::from_reader(stripped)
.with_context(|| "Failed to deserialize the genesis records.")?;
Expand Down
1 change: 1 addition & 0 deletions core/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ c2-chacha.workspace = true
curve25519-dalek.workspace = true
derive_more.workspace = true
ed25519-dalek.workspace = true
json_comments.workspace = true
near-account-id = { path = "../account-id" }
once_cell.workspace = true
primitive-types.workspace = true
Expand Down
8 changes: 6 additions & 2 deletions core/crypto/src/key_file.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::fs::File;
use std::io;
use std::io::Write;
use std::io::{Read, Write};
use std::path::Path;

use json_comments::StripComments;
use serde::{Deserialize, Serialize};

use crate::{PublicKey, SecretKey};
Expand Down Expand Up @@ -40,7 +41,10 @@ impl KeyFile {

pub fn from_file(path: &Path) -> io::Result<Self> {
let content = std::fs::read_to_string(path)?;
Ok(serde_json::from_str(&content)?)
let mut content_without_comments = String::new();
StripComments::new(content.as_bytes()).read_to_string(&mut content_without_comments)?;

Ok(serde_json::from_str(&content_without_comments)?)
}
}

Expand Down
14 changes: 11 additions & 3 deletions nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,7 @@ impl Config {
let contents = std::fs::read_to_string(path)
.with_context(|| format!("Failed to read config from {}", path.display()))?;
let mut unrecognised_fields = Vec::new();
let mut contents_without_comments = String::new();
StripComments::new(contents.as_bytes()).read_to_string(&mut contents_without_comments).unwrap();
let contents_without_comments = strip_comments_from_str(&contents)?;
let config: Config = serde_ignored::deserialize(
&mut serde_json::Deserializer::from_str(&contents_without_comments),
|field| {
Expand Down Expand Up @@ -1304,7 +1303,10 @@ impl NodeKeyFile {
let mut file = File::open(path)?;
let mut content = String::new();
file.read_to_string(&mut content)?;
Ok(serde_json::from_str(&content)?)

let content_without_comments = strip_comments_from_str(&content)?;

Ok(serde_json::from_str(&content_without_comments)?)
}
}

Expand All @@ -1324,6 +1326,12 @@ impl From<NodeKeyFile> for KeyFile {
}
}

fn strip_comments_from_str(input: &String) -> std::io::Result<String> {
let mut content_without_comments = String::new();
StripComments::new(input.as_bytes()).read_to_string(&mut content_without_comments)?;
Ok(content_without_comments)
}

pub fn load_config(
dir: &Path,
genesis_validation: GenesisValidationMode,
Expand Down
2 changes: 1 addition & 1 deletion nearcore/src/dyn_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
{
match std::fs::read_to_string(path) {
Ok(config_str) => {
// Strip the comments from the input (use `as_bytes()` to get a `Read`).
// Strip the comments from the input
let stripped = StripComments::new(config_str.as_bytes());

match serde_json::from_reader(stripped) {
Expand Down

0 comments on commit 435aeb4

Please sign in to comment.