Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: explicitly specify bundled or file system #939

Merged
merged 2 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 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 ckb-bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn run_app(version: Version) -> Result<(), ExitCode> {
subcommand::cli::secp256k1_lock(sub_matches)
}
(cli::CMD_HASHES, Some(sub_matches)) => {
subcommand::cli::hashes(Setup::locator_from_matches(&matches)?, sub_matches)
subcommand::cli::hashes(Setup::root_dir_from_matches(&matches)?, sub_matches)
}
_ => unreachable!(),
};
Expand Down
32 changes: 15 additions & 17 deletions ckb-bin/src/subcommand/cli/hashes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ckb_app_config::{cli, CKBAppConfig, ExitCode};
use ckb_chain_spec::ChainSpec;
use ckb_core::script::Script;
use ckb_resource::{Resource, ResourceLocator, CKB_CONFIG_FILE_NAME};
use ckb_resource::Resource;
use clap::ArgMatches;
use numext_fixed_hash::H256;
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -48,7 +48,7 @@ impl TryFrom<ChainSpec> for SpecHashes {
let cells_hashes = spec
.genesis
.system_cells
.resources
.files
.iter()
.zip(cellbase.outputs().iter().skip(1))
.enumerate()
Expand All @@ -72,30 +72,28 @@ impl TryFrom<ChainSpec> for SpecHashes {
}
}

pub fn hashes<'m>(locator: ResourceLocator, matches: &ArgMatches<'m>) -> Result<(), ExitCode> {
pub fn hashes<'m>(root_dir: PathBuf, matches: &ArgMatches<'m>) -> Result<(), ExitCode> {
let mut specs = BTreeMap::<String, SpecHashes>::new();

if matches.is_present(cli::ARG_BUNDLED) {
println!("# Generated by: ckb cli hashes -b");
let resource = Resource::Bundled(CKB_CONFIG_FILE_NAME.to_string());
let dev =
ChainSpec::resolve_relative_to(&locator, PathBuf::from("specs/dev.toml"), &resource)
.map_err(to_config_error)?;
let dev = ChainSpec::load_from(&Resource::bundled("specs/dev.toml".to_string()))
.map_err(to_config_error)?;
specs.insert(dev.name.clone(), dev.try_into()?);

let testnet = ChainSpec::resolve_relative_to(
&locator,
PathBuf::from("specs/testnet.toml"),
&resource,
)
.map_err(to_config_error)?;
let testnet = ChainSpec::load_from(&Resource::bundled("specs/testnet.toml".to_string()))
.map_err(to_config_error)?;
specs.insert(testnet.name.clone(), testnet.try_into()?);
} else {
println!("# Generated by: ckb cli hashes");
let resource = locator.ckb();
let config: CKBAppConfig = toml::from_slice(&resource.get()?)?;
let chain_spec = ChainSpec::resolve_relative_to(&locator, config.chain.spec, &resource)
.map_err(to_config_error)?;
let mut resource = Resource::ckb_config(&root_dir);
if !resource.exists() {
resource = Resource::bundled_ckb_config();
}

let mut config: CKBAppConfig = toml::from_slice(&resource.get()?)?;
config.chain.spec.absolutize(&root_dir);
let chain_spec = ChainSpec::load_from(&config.chain.spec).map_err(to_config_error)?;
specs.insert(chain_spec.name.clone(), chain_spec.try_into()?);
}

Expand Down
14 changes: 7 additions & 7 deletions ckb-bin/src/subcommand/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ckb_app_config::{ExitCode, InitArgs};
use ckb_resource::{
TemplateContext, AVAILABLE_SPECS, CKB_CONFIG_FILE_NAME, DEFAULT_SPEC, MINER_CONFIG_FILE_NAME,
SPEC_DEV_FILE_NAME,
Resource, TemplateContext, AVAILABLE_SPECS, CKB_CONFIG_FILE_NAME, DEFAULT_SPEC,
MINER_CONFIG_FILE_NAME, SPEC_DEV_FILE_NAME,
};
use ckb_script::Runner;

Expand All @@ -23,7 +23,7 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
runner: &runner,
};

let exported = args.locator.exported();
let exported = Resource::exported_in(&args.root_dir);
if !args.force && exported {
eprintln!("Config files already exists, use --force to overwrite.");
return Err(ExitCode::Failure);
Expand All @@ -36,17 +36,17 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
} else {
"Reinitialized"
},
args.locator.root_dir().display()
args.root_dir.display()
);

println!("create {}", CKB_CONFIG_FILE_NAME);
args.locator.export_ckb(&context)?;
Resource::bundled_ckb_config().export(&context, &args.root_dir)?;
println!("create {}", MINER_CONFIG_FILE_NAME);
args.locator.export_miner(&context)?;
Resource::bundled_miner_config().export(&context, &args.root_dir)?;

if args.chain == DEFAULT_SPEC {
println!("create {}", SPEC_DEV_FILE_NAME);
args.locator.export(SPEC_DEV_FILE_NAME, &context)?;
Resource::bundled(SPEC_DEV_FILE_NAME.to_string()).export(&context, &args.root_dir)?;
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions db/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde_derive::Deserialize;
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

#[derive(Clone, Debug, Deserialize, Default)]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct DBConfig {
#[serde(default)]
pub path: PathBuf,
Expand Down
4 changes: 0 additions & 4 deletions docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ specs/cells/secp256k1_sighash_all
Then `ckb.toml` refers `dev.toml` as `specs/dev.toml`, while
`specs/dev.toml` refers `secp256k1_sighash_all` as `cells/secp256k1_sighash_all`.

For security reason, there is a limitation of the file reference. The bundled
file can only refer to bundled files, while a file located in the file system
can either refer to another file in the file system or a bundled one.

## How to Change Config

First export the bundled config files into current directory using subcommand `init`.
Expand Down
2 changes: 2 additions & 0 deletions resource/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ include = ["/specs", "/ckb.toml", "/ckb-miner.toml"]
phf = "0.7.21"
includedir = "0.5.0"
tempfile = "3.0"
serde = "1.0"
serde_derive = "1.0"

[build-dependencies]
includedir_codegen = "0.5.0"
Expand Down
10 changes: 5 additions & 5 deletions resource/ckb-miner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ data_dir = "data"

[chain]
# Choose the kind of chains to run, possible values:
# - specs/dev.toml
# - specs/testnet.toml
spec = "specs/dev.toml" # {{
# testnet => spec = "specs/testnet.toml"
# integration => spec = "specs/integration.toml"
# - { file = "specs/dev.toml" }
# - { bundled = "specs/testnet.toml" }
spec = { file = "specs/dev.toml" } # {{
# testnet => spec = { bundled = "specs/testnet.toml" }
# integration => spec = { file = "specs/integration.toml" }
# }}

[logger]
Expand Down
10 changes: 5 additions & 5 deletions resource/ckb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ data_dir = "data"

[chain]
# Choose the kind of chains to run, possible values:
# - specs/dev.toml
# - specs/testnet.toml
spec = "specs/dev.toml" # {{
# testnet => spec = "specs/testnet.toml"
# integration => spec = "specs/integration.toml"
# - { file = "specs/dev.toml" }
# - { bundled = "specs/testnet.toml" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bundled is hard to understand in config, can we use some intention related name like builtin.

spec = { file = "specs/dev.toml" } # {{
# testnet => spec = { bundled = "specs/testnet.toml" }
# integration => spec = { file = "specs/integration.toml" }
# }}

[logger]
Expand Down
2 changes: 1 addition & 1 deletion resource/specs/dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ args = []
# the directory containing this config file.
[genesis.system_cells]
files = [
"cells/secp256k1_blake160_sighash_all"
{ bundled = "specs/cells/secp256k1_blake160_sighash_all" }
]

[genesis.system_cells.lock]
Expand Down
2 changes: 1 addition & 1 deletion resource/specs/testnet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ args = []
# the directory containing this config file.
[genesis.system_cells]
files = [
"cells/secp256k1_blake160_sighash_all"
{ bundled = "specs/cells/secp256k1_blake160_sighash_all" }
]

[genesis.system_cells.lock]
Expand Down
Loading