Skip to content

Commit

Permalink
feat: use toml for miner and chain spec (#311)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: nodes need to copy and use new toml configs and
remove/backup old json config files.
  • Loading branch information
doitian authored Mar 7, 2019
1 parent f9e7f97 commit 4b87df3
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 204 deletions.
4 changes: 2 additions & 2 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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"}
Expand Down
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
53 changes: 0 additions & 53 deletions nodes_template/default.json

This file was deleted.

48 changes: 48 additions & 0 deletions nodes_template/default.toml
Original file line number Diff line number Diff line change
@@ -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"

18 changes: 0 additions & 18 deletions nodes_template/miner.json

This file was deleted.

15 changes: 15 additions & 0 deletions nodes_template/miner.toml
Original file line number Diff line number Diff line change
@@ -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
47 changes: 0 additions & 47 deletions nodes_template/spec/dev.json

This file was deleted.

36 changes: 36 additions & 0 deletions nodes_template/spec/dev.toml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions pow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ authors = ["Nervos Core Dev <[email protected]>"]
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"
8 changes: 4 additions & 4 deletions spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ fn build_system_cell_transaction(cells: &[SystemCell]) -> Result<Transaction, Bo

impl ChainSpec {
pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<ChainSpec, Box<Error>> {
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)
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down
7 changes: 3 additions & 4 deletions src/cli/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -42,8 +41,8 @@ impl Config {
}

pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Config, Box<Error>> {
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)
}
Expand Down
Loading

0 comments on commit 4b87df3

Please sign in to comment.