Skip to content

Commit

Permalink
feat: ckb init --import-spec <file>
Browse files Browse the repository at this point in the history
Allow copying the spec file in `ckb init` from `<file>`. Specially, when `<file>` is "-", read the spec file from stdin encoded in base64.
  • Loading branch information
doitian committed Oct 10, 2019
1 parent a0d5b1e commit 2477920
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 32 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.

1 change: 1 addition & 0 deletions ckb-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ ckb-build-info = { path = "../util/build-info" }
ckb-verification = { path = "../verification" }
faster-hex = "0.4"
ckb-db = { path = "../db" }
base64 = "0.10.1"
54 changes: 40 additions & 14 deletions ckb-bin/src/subcommand/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs;
use std::io::{self, Read};
use std::path::PathBuf;

use crate::helper::prompt;
Expand Down Expand Up @@ -166,15 +167,6 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
}
};

let context = TemplateContext {
spec: &args.chain,
rpc_port: &args.rpc_port,
p2p_port: &args.p2p_port,
log_to_file: args.log_to_file,
log_to_stdout: args.log_to_stdout,
block_assembler: &block_assembler,
};

println!(
"{} CKB directory in {}",
if !exported {
Expand All @@ -185,15 +177,49 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
args.root_dir.display()
);

println!("create {}", CKB_CONFIG_FILE_NAME);
Resource::bundled_ckb_config().export(&context, &args.root_dir)?;
println!("create {}", MINER_CONFIG_FILE_NAME);
Resource::bundled_miner_config().export(&context, &args.root_dir)?;
let mut context = TemplateContext {
spec: &args.chain,
rpc_port: &args.rpc_port,
p2p_port: &args.p2p_port,
log_to_file: args.log_to_file,
log_to_stdout: args.log_to_stdout,
block_assembler: &block_assembler,
spec_source: "bundled",
};

if args.chain == DEFAULT_SPEC {
if let Some(spec_file) = args.import_spec {
context.spec_source = "file";

let specs_dir = args.root_dir.join("specs");
fs::create_dir_all(&specs_dir)?;
let target_file = specs_dir.join(format!("{}.toml", args.chain));

if spec_file == "-" {
println!("create specs/{}.toml from stdin", args.chain);
let mut encoded_content = String::new();
io::stdin().read_to_string(&mut encoded_content)?;
let spec_content = base64::decode_config(
&encoded_content.trim(),
base64::STANDARD.decode_allow_trailing_bits(true),
)
.map_err(|err| {
eprintln!("stdin must be encoded in base64: {}", err);
ExitCode::Failure
})?;
fs::write(target_file, spec_content)?;
} else {
println!("cp {} specs/{}.toml", spec_file, args.chain);
fs::copy(spec_file, target_file)?;
}
} else if args.chain == DEFAULT_SPEC {
println!("create {}", SPEC_DEV_FILE_NAME);
Resource::bundled(SPEC_DEV_FILE_NAME.to_string()).export(&context, &args.root_dir)?;
}

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

Ok(())
}
4 changes: 2 additions & 2 deletions resource/ckb-miner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ data_dir = "data"
# - { file = "specs/dev.toml" }
# - { bundled = "specs/testnet.toml" }
spec = { file = "specs/dev.toml" } # {{
# testnet => spec = { bundled = "specs/testnet.toml" }
# staging => spec = { bundled = "specs/staging.toml" }
# testnet => spec = { {spec_source} = "specs/testnet.toml" }
# staging => spec = { {spec_source} = "specs/staging.toml" }
# integration => spec = { file = "specs/integration.toml" }
# }}

Expand Down
4 changes: 2 additions & 2 deletions resource/ckb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ data_dir = "data"
# - { file = "specs/dev.toml" }
# - { bundled = "specs/testnet.toml" }
spec = { file = "specs/dev.toml" } # {{
# testnet => spec = { bundled = "specs/testnet.toml" }
# staging => spec = { bundled = "specs/staging.toml" }
# testnet => spec = { {spec_source} = "specs/testnet.toml" }
# staging => spec = { {spec_source} = "specs/staging.toml" }
# integration => spec = { file = "specs/integration.toml" }
# }}

Expand Down
1 change: 1 addition & 0 deletions resource/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ mod tests {
log_to_file: true,
log_to_stdout: true,
block_assembler: "",
spec_source: "bundled",
};
Resource::bundled_ckb_config()
.export(&context, root_dir.path())
Expand Down
2 changes: 2 additions & 0 deletions resource/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Template<T>(T);

pub struct TemplateContext<'a> {
pub spec: &'a str,
pub spec_source: &'a str,
pub rpc_port: &'a str,
pub p2p_port: &'a str,
pub log_to_file: bool,
Expand All @@ -38,6 +39,7 @@ fn writeln<W: io::Write>(w: &mut W, s: &str, context: &TemplateContext) -> io::R
.replace("{log_to_file}", &format!("{}", context.log_to_file))
.replace("{log_to_stdout}", &format!("{}", context.log_to_stdout))
.replace("{block_assembler}", context.block_assembler)
.replace("{spec_source}", context.spec_source)
)
}

Expand Down
14 changes: 0 additions & 14 deletions spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ impl Default for SatoshiGift {
#[derive(Debug)]
pub enum SpecLoadError {
FileNotFound,
ChainNameNotAllowed(String),
GenesisMismatch { expect: H256, actual: H256 },
}

Expand All @@ -156,10 +155,6 @@ impl SpecLoadError {
Box::new(SpecLoadError::FileNotFound)
}

fn chain_name_not_allowed(name: String) -> Box<Self> {
Box::new(SpecLoadError::ChainNameNotAllowed(name))
}

fn genesis_mismatch(expect: H256, actual: H256) -> Box<Self> {
Box::new(SpecLoadError::GenesisMismatch { expect, actual })
}
Expand All @@ -171,11 +166,6 @@ impl fmt::Display for SpecLoadError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SpecLoadError::FileNotFound => write!(f, "ChainSpec: file not found"),
SpecLoadError::ChainNameNotAllowed(name) => write!(
f,
"ChainSpec: name not allowed, expect ckb_dev, actual {}",
name
),
SpecLoadError::GenesisMismatch { expect, actual } => write!(
f,
"ChainSpec: genesis hash mismatch, expect {:#x}, actual {:#x}",
Expand All @@ -192,10 +182,6 @@ impl ChainSpec {
}
let config_bytes = resource.get()?;
let mut spec: ChainSpec = toml::from_slice(&config_bytes)?;
if !(resource.is_bundled() || spec.name == "ckb_dev" || spec.name == "ckb_integration_test")
{
return Err(SpecLoadError::chain_name_not_allowed(spec.name.clone()));
}

if let Some(parent) = resource.parent() {
for r in spec.genesis.system_cells.iter_mut() {
Expand Down
5 changes: 5 additions & 0 deletions util/app-config/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ mod tests {
log_to_file: true,
log_to_stdout: true,
block_assembler: "",
spec_source: "bundled",
};
{
Resource::bundled_ckb_config()
Expand Down Expand Up @@ -306,6 +307,7 @@ mod tests {
log_to_file: false,
log_to_stdout: true,
block_assembler: "",
spec_source: "bundled",
};
{
Resource::bundled_ckb_config()
Expand Down Expand Up @@ -341,6 +343,7 @@ mod tests {
log_to_file: true,
log_to_stdout: true,
block_assembler: "",
spec_source: "bundled",
};
{
Resource::bundled_ckb_config()
Expand Down Expand Up @@ -387,6 +390,7 @@ mod tests {
log_to_file: true,
log_to_stdout: true,
block_assembler: "",
spec_source: "bundled",
};
{
Resource::bundled_ckb_config()
Expand Down Expand Up @@ -431,6 +435,7 @@ mod tests {
log_to_file: true,
log_to_stdout: true,
block_assembler: "",
spec_source: "bundled",
};
{
Resource::bundled_ckb_config()
Expand Down
1 change: 1 addition & 0 deletions util/app-config/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct InitArgs {
pub block_assembler_args: Vec<String>,
pub block_assembler_hash_type: ScriptHashType,
pub block_assembler_message: Option<String>,
pub import_spec: Option<String>,
}

pub struct ResetDataArgs {
Expand Down
10 changes: 10 additions & 0 deletions util/app-config/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub const ARG_DATA: &str = "data";
pub const ARG_LIST_CHAINS: &str = "list-chains";
pub const ARG_INTERACTIVE: &str = "interactive";
pub const ARG_CHAIN: &str = "chain";
pub const ARG_IMPORT_SPEC: &str = "import-spec";
pub const ARG_P2P_PORT: &str = "p2p-port";
pub const ARG_RPC_PORT: &str = "rpc-port";
pub const ARG_FORCE: &str = "force";
Expand Down Expand Up @@ -301,6 +302,15 @@ fn init() -> App<'static, 'static> {
.default_value(DEFAULT_SPEC)
.help("Initializes CKB direcotry for <chain>"),
)
.arg(
Arg::with_name(ARG_IMPORT_SPEC)
.long(ARG_IMPORT_SPEC)
.takes_value(true)
.help(
"Uses the specifiec file as chain spec. Specially, \
The dash \"-\" denotes importing the spec from stdin encoded in base64",
),
)
.arg(
Arg::with_name(ARG_LOG_TO)
.long(ARG_LOG_TO)
Expand Down
3 changes: 3 additions & 0 deletions util/app-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ impl Setup {
.unwrap();
let block_assembler_message = matches.value_of(cli::ARG_BA_MESSAGE).map(str::to_string);

let import_spec = matches.value_of(cli::ARG_IMPORT_SPEC).map(str::to_string);

Ok(InitArgs {
interactive,
root_dir,
Expand All @@ -240,6 +242,7 @@ impl Setup {
block_assembler_args,
block_assembler_hash_type,
block_assembler_message,
import_spec,
})
}

Expand Down

0 comments on commit 2477920

Please sign in to comment.