Skip to content

Commit

Permalink
feat(config): Simplify config and data dir parsing (#19)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Command line arguments and some config options and chan spec options have been
changed. It may break scripts and integration tests that depends on the
command line interface.

- Command line argument `--config|-c` is now a top option, which should be
  specified before sub-command.
- Remove command line arguments `--data-dir` and `--chain`. They should be
  configured via the config file.
- Add a new config option `data_dir`, which configures the path of data
  directory.
- Option `ckb.chain` now only accepts a path.
- Option `system_cells` in chain spec now only accepts paths.

The path `data_dir` and `ckb.chain` can be an absolute path, or relative to
the directory containing the config file.

The paths of `system_cells` in chain spec can be absolute, or relative to the
directory containing the chain spec file.

If `--config|-c` is omitted, the app tries `ckb.json`, `nodes/default.json` in
order.

It is recommended to use `nodes/default.json` for development, which can be
created by copying `nodes/default.example.json`. The default config saves data
in `nodes/default/`, and loads chains spec `nodes/spec/dev.json`.

Following locations are obsoleted data directories, which can be removed on
development machines:

- Linux
  - `$XDG_DATA_HOME/ckb`
  - `$XDG_DATA_HOME/NervosDev`
  - `$HOME/.local/share/ckb`
  - `$HOME/.local/share/NervosDev`
- macOS
  - `$HOME/Library/Application Support/ckb`
  - `$HOME/Library/Application Support/NervosDev`
  • Loading branch information
doitian authored Nov 26, 2018
1 parent f74dbbc commit b4fdc29
Show file tree
Hide file tree
Showing 21 changed files with 229 additions and 469 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ indent_style = space
indent_size = 4
tab_width = 4

[*.json]
indent_style = space
indent_size = 4
tab_width = 4

[Makefile]
indent_style = tab
indent_size = 4
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ tags
# vagrant file
.vagrant

# nervos runtime folder
.nervos
# runtime folder
/nodes/
42 changes: 0 additions & 42 deletions Cargo.lock

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

36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,23 @@ cargo test -p ckb-chain

### Start Node

Create the defualt runtime directory:

```shell
cp -r nodes_template/ nodes
```

Use the config file to start the node

```shell
target/release/ckb run
```

blockchain data is located in
It searches config file `ckb.json`, `nodes/default.json` in the shell
working directory in that order. Alternatively, the argument `-c` can specify
the config file used to start the node.

Platform | Value | Example
-------- | ------|---------
Linux | $XDG_DATA_HOME/ckb or $HOME/.local/share/ckb | /home/alice/.local/share/ckb
macOS | $HOME/Library/Application Support/ckb | /Users/Alice/Library/Application Support/ckb
The default config file saves data in `nodes/default/`.

### Send Transaction via RPC

Expand All @@ -130,19 +137,26 @@ curl -d '{"id": 2, "jsonrpc": "2.0", "method":"send_transaction","params": [{"ve

### Advanced

Run multiple nodes in different `data-dir`:
Run multiple nodes in different data directories.

Create the config file for new nodes, for example:

```shell
target/release/ckb run --data-dir=/tmp/node1
target/release/ckb run --data-dir=/tmp/node2
cp nodes/default.json nodes/node2.json
```

Update `data_dir` configuration in config file to a different directory.

```
"data_dir": "node2"
```

The file `config.json` in `data-dir` overrides default configurations. Start with the default one:
Then start the new node using the new config file

```shell
cp src/config/default.json /tmp/node1/config.json
target/release/ckb -c nodes/node2.json run
```

The option `ckb.chain` configures the chain spec. It accepts a pre-defined spec or the path to a customized spec JSON file. The directory `spec/res` 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 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 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.
10 changes: 5 additions & 5 deletions src/config/default.json → nodes_template/default.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"data_dir": "default",
"ckb": {
"chain": "dev"
"chain": "spec/dev.json"
},
"logger": {
"file": "ckb.log",
"filter": "info",
"color": true
},
"network": {
"listen_addresses": [
"/ip4/0.0.0.0/tcp/8115"
],
"listen_addresses": ["/ip4/0.0.0.0/tcp/8115"],
"boot_nodes": [],
"reserved_nodes": [],
"only_reserved_peers": false,
Expand All @@ -37,6 +36,7 @@
"max_tx": 1024,
"max_prop": 1024,
"new_transactions_threshold": 8,
"type_hash": "0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff"
"type_hash":
"0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff"
}
}
File renamed without changes.
File renamed without changes.
9 changes: 4 additions & 5 deletions spec/res/dev.json → nodes_template/spec/dev.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"__comments__": {
"system_cells": [
"When loading dev.yaml, we will modify the path here to simplify loading,",
"but if you are copying this file elsewhere, you will need to provide full path ",
"or relative path to where ckb is executing."
"path to cells files",
"which is absolute or relative to the directory containing this config file."
],

"edge_bits": [
Expand Down Expand Up @@ -36,8 +35,8 @@
"initial_block_reward": 50000
},
"system_cells": [
{"path": "verify"},
{"path": "always_success"}
{"path": "cells/verify"},
{"path": "cells/always_success"}
],
"pow": {
"Cuckoo": {
Expand Down
5 changes: 4 additions & 1 deletion pool/src/tests/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use ckb_shared::store::ChainKVStore;
use ckb_time::now_ms;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::sync::Arc;
use std::time;
use txs_pool::pool::TransactionPoolService;
Expand Down Expand Up @@ -596,7 +597,9 @@ fn test_transaction_with_capacity(
// Since the main point here is to test pool functionality, not scripting
// behavior, we use a dummy script here that always passes in testing
fn create_valid_script() -> Script {
let mut file = File::open("../spec/res/cells/always_success").unwrap();
let mut file = File::open(
Path::new(env!("CARGO_MANIFEST_DIR")).join("../nodes_template/spec/cells/always_success"),
).unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

Expand Down
27 changes: 20 additions & 7 deletions script/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,23 @@ mod tests {
use hash::sha3_256;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;

fn open_cell_verify() -> File {
File::open(
Path::new(env!("CARGO_MANIFEST_DIR")).join("../nodes_template/spec/cells/verify"),
).unwrap()
}
fn open_cell_always_success() -> File {
File::open(
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../nodes_template/spec/cells/always_success"),
).unwrap()
}

#[test]
fn check_signature() {
let mut file = File::open("../spec/res/cells/verify").unwrap();
let mut file = open_cell_verify();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

Expand Down Expand Up @@ -190,7 +203,7 @@ mod tests {

#[test]
fn check_invalid_signature() {
let mut file = File::open("../spec/res/cells/verify").unwrap();
let mut file = open_cell_verify();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

Expand Down Expand Up @@ -235,7 +248,7 @@ mod tests {

#[test]
fn check_valid_dep_reference() {
let mut file = File::open("../spec/res/cells/verify").unwrap();
let mut file = open_cell_verify();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

Expand Down Expand Up @@ -285,7 +298,7 @@ mod tests {

#[test]
fn check_invalid_dep_reference() {
let mut file = File::open("../spec/res/cells/verify").unwrap();
let mut file = open_cell_verify();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

Expand Down Expand Up @@ -331,7 +344,7 @@ mod tests {
}

fn create_always_success_script() -> Script {
let mut file = File::open("../spec/res/cells/always_success").unwrap();
let mut file = open_cell_always_success();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

Expand All @@ -340,7 +353,7 @@ mod tests {

#[test]
fn check_output_contract() {
let mut file = File::open("../spec/res/cells/verify").unwrap();
let mut file = open_cell_verify();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

Expand Down Expand Up @@ -387,7 +400,7 @@ mod tests {

#[test]
fn check_invalid_output_contract() {
let mut file = File::open("../spec/res/cells/verify").unwrap();
let mut file = open_cell_verify();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

Expand Down
Loading

0 comments on commit b4fdc29

Please sign in to comment.