From 4b87df3a74d79390c25799c694fbfe80df4a9ad3 Mon Sep 17 00:00:00 2001 From: ian Date: Thu, 7 Mar 2019 11:36:05 +0800 Subject: [PATCH] feat: use toml for miner and chain spec (#311) BREAKING CHANGE: nodes need to copy and use new toml configs and remove/backup old json config files. --- Cargo.lock | 4 +- Cargo.toml | 2 +- README.md | 16 ++--- nodes_template/default.json | 53 ---------------- nodes_template/default.toml | 48 ++++++++++++++ nodes_template/miner.json | 18 ------ nodes_template/miner.toml | 15 +++++ nodes_template/spec/dev.json | 47 -------------- nodes_template/spec/dev.toml | 36 +++++++++++ pow/src/lib.rs | 1 + spec/Cargo.toml | 6 +- spec/src/lib.rs | 8 +-- src/cli/args.rs | 4 +- src/cli/miner.rs | 7 +-- src/setup.rs | 119 ++++++++++++++++++----------------- 15 files changed, 180 insertions(+), 204 deletions(-) delete mode 100644 nodes_template/default.json create mode 100644 nodes_template/default.toml delete mode 100644 nodes_template/miner.json create mode 100644 nodes_template/miner.toml delete mode 100644 nodes_template/spec/dev.json create mode 100644 nodes_template/spec/dev.toml diff --git a/Cargo.lock b/Cargo.lock index 00ca92d455..b2bbe5038b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -316,8 +316,8 @@ dependencies = [ "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -372,7 +372,7 @@ dependencies = [ "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 5df2389ec2..8b701b9537 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ build = "build.rs" clap = { version = "2" } serde = "1.0" serde_derive = "1.0" +toml = "0.4" log = "0.4" crossbeam-channel = "0.3" config-tool = { package= "config", version = "0.9" } @@ -31,7 +32,6 @@ numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heap dir = { path = "util/dir" } ctrlc = { version = "3.1", features = ["termination"] } ckb-sync = { path = "sync"} -serde_json = "1.0" crypto = { path = "util/crypto"} ckb-instrument = { path = "util/instrument", features = ["progress_bar"] } hash = { path = "util/hash"} diff --git a/README.md b/README.md index 153788a694..011a41753e 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ Use the config file to start the node target/release/ckb run ``` -It searches config file `ckb.json`, `nodes/default.json` in the shell +It searches config file `ckb.toml`, `nodes/default.toml` in the shell working directory in that order. Alternatively, the argument `-c` can specify the config file used to start the node. @@ -141,27 +141,21 @@ Run multiple nodes in different data directories. Create the config file for new nodes, for example: ```shell -cp nodes/default.json nodes/node2.json +cp nodes/default.toml nodes/node2.toml ``` Update `data_dir` configuration in config file to a different directory. ``` -"data_dir": "node2" -``` - -or using a simple shell command to modify the config file: - -```shell -ex -sc '%s/"data_dir": "default"/"data_dir": "node2"/|x' nodes/node2.json +data_dir = "node2" ``` Then start the new node using the new config file ```shell -target/release/ckb run -c nodes/node2.json +target/release/ckb run -c nodes/node2.toml ``` -The option `ckb.chain` configures the chain spec. It accepts a path to the spec JSON file. The directory `nodes_template/spec` has all the pre-defined specs. Please note that nodes with different chain specs may fail to connect with each other. +The option `ckb.chain` configures the chain spec. It accepts a path to the spec toml file. The directory `nodes_template/spec` has all the pre-defined specs. Please note that nodes with different chain specs may fail to connect with each other. The chain spec can switch between different PoW engines. Wiki has the [instructions](https://github.com/nervosnetwork/ckb/wiki/PoW-Engines) about how to configure it. diff --git a/nodes_template/default.json b/nodes_template/default.json deleted file mode 100644 index eb19d24aef..0000000000 --- a/nodes_template/default.json +++ /dev/null @@ -1,53 +0,0 @@ -{ - "__comments__": { - "rpc modules": [ - "List of API modules", - ["Net", "Pool", "Miner", "Chain", "Trace"] - ], - "rpc max_request_body_size": "Default is 10MiB = 10 * 1024 * 1024" - }, - - "data_dir": "default", - "db": { - "path": "default/db" - }, - "chain": { - "spec": "spec/dev.json" - }, - "logger": { - "file": "ckb.log", - "filter": "info", - "color": true - }, - "network": { - "listen_addresses": ["/ip4/0.0.0.0/tcp/8115"], - "bootnodes": [], - "reserved_nodes": [], - "only_reserved_peers": false, - "min_peers": 4, - "max_peers": 8, - "secret_file": "secret", - "nodes_file": "nodes.json" - }, - "rpc": { - "listen_address": "0.0.0.0:8114", - "modules": ["Net", "Pool", "Miner", "Chain"], - "max_request_body_size": 10485760 - }, - "sync": { - "verification_level": "Full", - "orphan_block_limit": 1024 - }, - "pool": { - "max_pool_size": 10000, - "max_orphan_size": 10000, - "max_proposal_size": 10000, - "max_cache_size": 1000, - "max_pending_size": 10000, - "trace": 100 - }, - "block_assembler": { - "type_hash": "0x0da2fe99fe549e082d4ed483c2e968a89ea8d11aabf5d79e5cbf06522de6e674" - }, - "txs_verify_cache_size": 100000 -} diff --git a/nodes_template/default.toml b/nodes_template/default.toml new file mode 100644 index 0000000000..0ff21a12e7 --- /dev/null +++ b/nodes_template/default.toml @@ -0,0 +1,48 @@ +data_dir = "default" +txs_verify_cache_size = 100000 + +[db] +path = "default/db" + +[chain] +spec = "spec/dev.toml" + +[logger] +file = "ckb.log" +filter = "info" +color = true + +[network] +listen_addresses = ["/ip4/0.0.0.0/tcp/8115"] +bootnodes = [] +reserved_nodes = [] +only_reserved_peers = false +min_peers = 4 +max_peers = 8 +secret_file = "secret" +nodes_file = "nodes.toml" + +[rpc] +listen_address = "0.0.0.0:8114" + +# Default is 10MiB = 10 * 1024 * 1024 +max_request_body_size = 10485760 + +# List of API modules: ["Net", "Pool", "Miner", "Chain", "Trace"] +modules = ["Net", "Pool", "Miner", "Chain"] + +[sync] +verification_level = "Full" +orphan_block_limit = 1024 + +[pool] +max_pool_size = 10000 +max_orphan_size = 10000 +max_proposal_size = 10000 +max_cache_size = 1000 +max_pending_size = 10000 +trace = 100 + +[block_assembler] +type_hash = "0x0da2fe99fe549e082d4ed483c2e968a89ea8d11aabf5d79e5cbf06522de6e674" + diff --git a/nodes_template/miner.json b/nodes_template/miner.json deleted file mode 100644 index 70a032ecc4..0000000000 --- a/nodes_template/miner.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "__comments__": { - "poll_interval": "poll block template interval, unit: millisecond" - }, - "logger": { - "file": "miner.log", - "filter": "info", - "color": true - }, - "data_dir": "default", - "chain": "spec/dev.json", - "rpc_url": "http://127.0.0.1:8114/", - "cycles_limit": 100000000, - "bytes_limit": 10000000, - "max_version": 0, - "poll_interval": 1000, - "block_on_submit": true -} diff --git a/nodes_template/miner.toml b/nodes_template/miner.toml new file mode 100644 index 0000000000..dd7896bf66 --- /dev/null +++ b/nodes_template/miner.toml @@ -0,0 +1,15 @@ +data_dir = "default" +chain = "spec/dev.toml" +rpc_url = "http://127.0.0.1:8114/" +cycles_limit = 100000000 +bytes_limit = 10000000 +max_version = 0 +block_on_submit = true + +# block template polling interval in milliseconds +poll_interval = 1000 + +[logger] +file = "miner.log" +filter = "info" +color = true diff --git a/nodes_template/spec/dev.json b/nodes_template/spec/dev.json deleted file mode 100644 index 76549dd2c5..0000000000 --- a/nodes_template/spec/dev.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "__comments__": { - "system_cells": [ - "path to cells files", - "which is absolute or relative to the directory containing this config file." - ], - - "edge_bits": [ - "the 2-log of the graph size", - "which is the size in bits of the node identifiers" - ], - - "cycle_length": [ - "length of the cycle to be found, must be an even number", - "a minimum of 12 is recommended" - ] - }, - - "name": "ckb", - "genesis": { - "seal": { - "nonce": 0, - "proof": [0] - }, - "version": 0, - "parent_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "timestamp": 0, - "txs_commit": "0x0000000000000000000000000000000000000000000000000000000000000000", - "txs_proposal": "0x0000000000000000000000000000000000000000000000000000000000000000", - "difficulty": "0x100", - "cellbase_id": "0x0000000000000000000000000000000000000000000000000000000000000000", - "uncles_hash": "0x0000000000000000000000000000000000000000000000000000000000000000" - }, - "params": { - "initial_block_reward": 50000, - "max_block_cycles": 100000000 - }, - "system_cells": [ - {"path": "cells/always_success"} - ], - "pow": { - "Cuckoo": { - "edge_bits": 15, - "cycle_length": 12 - } - } -} diff --git a/nodes_template/spec/dev.toml b/nodes_template/spec/dev.toml new file mode 100644 index 0000000000..3d6e09b7b8 --- /dev/null +++ b/nodes_template/spec/dev.toml @@ -0,0 +1,36 @@ +name = "ckb" + +[genesis] +version = 0 +parent_hash = "0x0000000000000000000000000000000000000000000000000000000000000000" +timestamp = 0 +txs_commit = "0x0000000000000000000000000000000000000000000000000000000000000000" +txs_proposal = "0x0000000000000000000000000000000000000000000000000000000000000000" +difficulty = "0x100" +cellbase_id = "0x0000000000000000000000000000000000000000000000000000000000000000" +uncles_hash = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[genesis.seal] +nonce = 0 +proof = [0] + +[params] +initial_block_reward = 50000 +max_block_cycles = 100000000 + +[pow] +func = "Cuckoo" + +[pow.params] +# the 2-log of the graph size, which is the size in bits of the node +# identifiers +edge_bits = 15 + +# length of the cycle to be found, must be an even number, a minimum of 12 is +# recommended +cycle_length = 12 + +# An array list paths to system cell files, which is absolute or relative to +# the directory containing this config file. +[[system_cells]] +path = "cells/always_success" diff --git a/pow/src/lib.rs b/pow/src/lib.rs index 30a9bac7e6..7ed27de94e 100644 --- a/pow/src/lib.rs +++ b/pow/src/lib.rs @@ -16,6 +16,7 @@ pub use crate::cuckoo::{Cuckoo, CuckooEngine, CuckooParams}; pub use crate::dummy::DummyPowEngine; #[derive(Clone, Deserialize, Eq, PartialEq, Hash, Debug)] +#[serde(tag = "func", content = "params")] pub enum Pow { Dummy, Clicker, diff --git a/spec/Cargo.toml b/spec/Cargo.toml index 9ed8b29365..2bb8232e3b 100644 --- a/spec/Cargo.toml +++ b/spec/Cargo.toml @@ -6,12 +6,12 @@ authors = ["Nervos Core Dev "] edition = "2018" [dependencies] -serde_json = "1.0" +serde = "1.0" +serde_derive = "1.0" +toml = "0.4" numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] } numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] } ckb-core = { path = "../core" } -serde_derive = "1.0" -serde = "1.0" ckb-pow = { path = "../pow" } ckb-protocol = { path = "../protocol" } flatbuffers = "0.5.0" diff --git a/spec/src/lib.rs b/spec/src/lib.rs index 99c04d9ac4..0771e786b2 100644 --- a/spec/src/lib.rs +++ b/spec/src/lib.rs @@ -93,8 +93,8 @@ fn build_system_cell_transaction(cells: &[SystemCell]) -> Result>(path: P) -> Result> { - let file = File::open(path.as_ref())?; - let mut spec: Self = serde_json::from_reader(file)?; + let config_str = std::fs::read_to_string(path.as_ref())?; + let mut spec: Self = toml::from_str(&config_str)?; spec.resolve_paths(path.as_ref().parent().unwrap()); Ok(spec) } @@ -150,11 +150,11 @@ pub mod test { println!( "{:?}", Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../nodes_template/spec/dev.json") + .join("../nodes_template/spec/dev.toml") .display() ); let dev = ChainSpec::read_from_file( - Path::new(env!("CARGO_MANIFEST_DIR")).join("../nodes_template/spec/dev.json"), + Path::new(env!("CARGO_MANIFEST_DIR")).join("../nodes_template/spec/dev.toml"), ); assert!(dev.is_ok(), format!("{:?}", dev)); for cell in &dev.unwrap().system_cells { diff --git a/src/cli/args.rs b/src/cli/args.rs index bd50a0cc0d..933822bbc7 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -2,8 +2,8 @@ use build_info::{get_version, Version}; use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; -const CKB_CONFIG_HELP: &str = "Specify the configuration file PATH. Tries ckb.json, nodes/default.json in working directory when omitted."; -const MINER_CONFIG_HELP: &str = "Specify the configuration file PATH. Tries miner.json, nodes/miner.json in working directory when omitted."; +const CKB_CONFIG_HELP: &str = "Specify the configuration file PATH. Tries ckb.toml, nodes/default.toml in working directory when omitted."; +const MINER_CONFIG_HELP: &str = "Specify the configuration file PATH. Tries miner.toml, nodes/miner.toml in working directory when omitted."; pub fn get_matches() -> ArgMatches<'static> { let version = get_version!(); diff --git a/src/cli/miner.rs b/src/cli/miner.rs index 82420bb66e..768336a802 100644 --- a/src/cli/miner.rs +++ b/src/cli/miner.rs @@ -8,12 +8,11 @@ use dir::Directories; use logger::{self, Config as LogConfig}; use serde_derive::Deserialize; use std::error::Error; -use std::fs::File; use std::path::{Path, PathBuf}; use std::sync::Arc; use std::thread; -const DEFAULT_CONFIG_PATHS: &[&str] = &["miner.json", "nodes/miner.json"]; +const DEFAULT_CONFIG_PATHS: &[&str] = &["miner.toml", "nodes/miner.toml"]; #[derive(Clone, Debug, Deserialize)] struct Config { @@ -42,8 +41,8 @@ impl Config { } pub fn read_from_file>(path: P) -> Result> { - let file = File::open(path.as_ref())?; - let mut config: Self = serde_json::from_reader(file)?; + let config_str = std::fs::read_to_string(path.as_ref())?; + let mut config: Self = toml::from_str(&config_str)?; config.resolve_paths(path.as_ref().parent().unwrap()); Ok(config) } diff --git a/src/setup.rs b/src/setup.rs index b00cb5f209..3e2c2dba4a 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -7,14 +7,14 @@ use ckb_pool::txs_pool::PoolConfig; use ckb_rpc::Config as RpcConfig; use ckb_sync::Config as SyncConfig; use clap::ArgMatches; -use config_tool::{Config as ConfigTool, File}; +use config_tool::{Config as ConfigTool, ConfigError, File}; use dir::Directories; use logger::Config as LogConfig; use serde_derive::Deserialize; use std::error::Error; use std::path::{Path, PathBuf}; -const DEFAULT_CONFIG_PATHS: &[&str] = &["ckb.json", "nodes/default.json"]; +const DEFAULT_CONFIG_PATHS: &[&str] = &["ckb.toml", "nodes/default.toml"]; #[derive(Clone, Debug)] pub struct Setup { @@ -77,7 +77,13 @@ impl Setup { Some(dirs.join("network").to_string_lossy().to_string()); } - let chain_spec = ChainSpec::read_from_file(&configs.chain.spec)?; + let chain_spec = ChainSpec::read_from_file(&configs.chain.spec).map_err(|e| { + Box::new(ConfigError::Message(format!( + "invalid chain spec {}, {}", + configs.chain.spec.display(), + e + ))) + })?; Ok(Setup { configs, @@ -124,7 +130,7 @@ pub mod test { fn override_default_config_file>(config_path: &T) -> Result> { let mut config_tool = ConfigTool::new(); let default_config_path = - Path::new(env!("CARGO_MANIFEST_DIR")).join("nodes_template/default.json"); + Path::new(env!("CARGO_MANIFEST_DIR")).join("nodes_template/default.toml"); config_tool.merge(ConfigFile::from(default_config_path.as_path()))?; config_tool.merge(ConfigFile::from(config_path.as_ref()))?; @@ -142,37 +148,38 @@ pub mod test { fn test_chain_spec() -> &'static str { r#" - { - "name": "ckb_test_custom", - "genesis": { - "seal": { - "nonce": 233, - "proof": [2, 3, 3] - }, - "version": 0, - "parent_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "timestamp": 0, - "txs_commit": "0x0000000000000000000000000000000000000000000000000000000000000000", - "txs_proposal": "0x0000000000000000000000000000000000000000000000000000000000000000", - "difficulty": "0x233", - "cellbase_id": "0x0000000000000000000000000000000000000000000000000000000000000000", - "uncles_hash": "0x0000000000000000000000000000000000000000000000000000000000000000" - }, - "params": { - "initial_block_reward": 233, - "max_block_cycles": 100000000 - }, - "system_cells": [ - {"path": "verify"}, - {"path": "always_success"} - ], - "pow": { - "Cuckoo": { - "edge_bits": 29, - "cycle_length": 42 - } - } - } + name = "ckb_test_custom" + + [genesis] + version = 0 + parent_hash = "0x0000000000000000000000000000000000000000000000000000000000000000" + timestamp = 0 + txs_commit = "0x0000000000000000000000000000000000000000000000000000000000000000" + txs_proposal = "0x0000000000000000000000000000000000000000000000000000000000000000" + difficulty = "0x233" + cellbase_id = "0x0000000000000000000000000000000000000000000000000000000000000000" + uncles_hash = "0x0000000000000000000000000000000000000000000000000000000000000000" + + [genesis.seal] + nonce = 233 + proof = [2, 3, 3] + + [params] + initial_block_reward = 233 + max_block_cycles = 100000000 + + [pow] + func = "Cuckoo" + + [pow.params] + edge_bits = 29 + cycle_length = 42 + + [[system_cells]] + path = "verify" + + [[system_cells]] + path = "always_success" "# } @@ -183,12 +190,11 @@ pub mod test { .tempdir() .unwrap(); - let test_conifg = r#"{ - "network": { - "listen_addresses": ["/ip4/1.1.1.1/tcp/1"] - } - }"#; - let config_path = tmp_dir.path().join("config.json"); + let test_conifg = r#" + [network] + listen_addresses = ["/ip4/1.1.1.1/tcp/1"] + "#; + let config_path = tmp_dir.path().join("config.toml"); write_file(&config_path, test_conifg); let setup = override_default_config_file(&config_path); assert!(setup.is_ok()); @@ -205,15 +211,12 @@ pub mod test { .tempdir() .unwrap(); - let test_conifg = r#"{ - "db": { - "options": { - "disable_auto_compactions": "true", - "paranoid_file_checks": "true" - } - } - }"#; - let config_path = tmp_dir.path().join("config.json"); + let test_conifg = r#" + [db.options] + disable_auto_compactions = "true" + paranoid_file_checks = "true" + "#; + let config_path = tmp_dir.path().join("config.toml"); write_file(&config_path, test_conifg); let setup = override_default_config_file(&config_path).unwrap(); let options: Vec<(&str, &str)> = setup @@ -239,19 +242,17 @@ pub mod test { .tempdir() .unwrap(); - let chain_spec_path = tmp_dir.path().join("ckb_test_custom.json"); - let test_conifg = format!( + let chain_spec_path = tmp_dir.path().join("ckb_test_custom.toml"); + let test_config = format!( r#" - {{ - "chain": {{ - "spec": "{}" - }} - }}"#, + [chain] + spec = "{}" + "#, chain_spec_path.to_str().unwrap() ); - let config_path = tmp_dir.path().join("config.json"); - write_file(&config_path, &test_conifg); + let config_path = tmp_dir.path().join("config.toml"); + write_file(&config_path, &test_config); write_file(&chain_spec_path, test_chain_spec()); let setup = override_default_config_file(&config_path);