Skip to content

Commit

Permalink
Merge #2756
Browse files Browse the repository at this point in the history
2756: feat(hardfork): ckb2021 hardfork features (vm related part) r=doitian,quake a=yangby-cryptape

### Changes

- New hardfork Features

  -  [CKB-RFCs PR 236: RFC: ckb vm version1 changes](nervosnetwork/rfcs#236)

  -  [CKB-RFCs PR 237: RFC: vm syscalls 2](nervosnetwork/rfcs#237)

  -  [CKB-RFCs PR 238: CKB VM version selection](nervosnetwork/rfcs#238)

- All changes are followed to the latest RFCs, except:

  - Add a "edition" field to all configuration files: CKB chain specification or app configurations.

#### CKB Chain Edition

- If no `edition` field, the default edition is `"2019"`.

-  After this PR, the default files created by `ckb init` will be edition `"2021"` files.

   But the public chains will still use the edition `"2019"` CKB chain specification as the built-in spec.

   At present, there are only to public chains: "mainnet" and "testnet".

- The edition `"2019"` doesn't support hardfork parameters.

- How to upgrade from edition `"2019"` to `"2021"`?

  Modify your spec file (default is `specs/dev.toml`), `ckb.toml` and `ckb-miner.toml`:

    - Update all `hash_type = "data"` to `hash_type.kind = "data"` and `hash_type.vm_version = 0`.

    - Update all `hash_type = "type"` to `hash_type.kind = "type"`.

    - Insert `edition = "2021"` into the head of the files.

  After upgrade the edition of chain specification,  the first time to run the CKB, you have to use `--overwrite-spec` parameter. The details of this parameter can be found with `ckb run --help`.

### BREAKING CHANGES

- The field `hash_type` in `Script` is changed from a `String` to an `Object` for all JSON RPC methods.

  The details can be found in the latest RFCs.

- The argument `--ba-hash-type` for `ckb init` is split into two arguments: `--ba-hash-type-kind` and `--ba-hash-type-vm-version`.

  More details can be found with `ckb-release init --help`.

Co-authored-by: mohanson <[email protected]>
Co-authored-by: Boyu Yang <[email protected]>
  • Loading branch information
3 people authored Jul 1, 2021
2 parents abc3a7f + 09c7975 commit 79e47fb
Show file tree
Hide file tree
Showing 83 changed files with 2,792 additions and 482 deletions.
279 changes: 117 additions & 162 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cov: ## Run code coverage.

.PHONY: wasm-build-test
wasm-build-test: ## Build core packages for wasm target
cp -f Cargo.lock wasm-build-test/
cd wasm-build-test && cargo build --target=wasm32-unknown-unknown

.PHONY: setup-ckb-test
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/benchmarks/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ lazy_static! {
let script = Script::new_builder()
.code_hash(CellOutput::calc_data_hash(&data))
.args(Bytes::from(PUBKEY_HASH.as_bytes()).pack())
.hash_type(ScriptHashType::Data.into())
.hash_type(ScriptHashType::Data(0).into())
.build();

(cell, data, script)
Expand Down
2 changes: 1 addition & 1 deletion chain/src/tests/block_assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn start_chain(consensus: Option<Consensus>) -> (ChainController, Shared) {
let config = BlockAssemblerConfig {
code_hash: h256!("0x0"),
args: Default::default(),
hash_type: ScriptHashType::Data,
hash_type: ScriptHashType::Data { vm_version: 0 },
message: Default::default(),
};
let (shared, mut pack) = builder
Expand Down
4 changes: 2 additions & 2 deletions chain/src/tests/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ fn finalize_reward() {
let bob = ScriptBuilder::default()
.args(bob_args)
.code_hash(always_success_script.code_hash())
.hash_type(ScriptHashType::Data.into())
.hash_type(ScriptHashType::Data(0).into())
.build();

let alice_args: packed::Bytes = Bytes::from(b"a11ce".to_vec()).pack();
let alice = ScriptBuilder::default()
.args(alice_args)
.code_hash(always_success_script.code_hash())
.hash_type(ScriptHashType::Data.into())
.hash_type(ScriptHashType::Data(0).into())
.build();

for i in 1..23 {
Expand Down
2 changes: 1 addition & 1 deletion chain/src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub(crate) fn start_chain(consensus: Option<Consensus>) -> (ChainController, Sha
let config = BlockAssemblerConfig {
code_hash: h256!("0x0"),
args: Default::default(),
hash_type: ScriptHashType::Data,
hash_type: ScriptHashType::Data { vm_version: 0 },
message: Default::default(),
};

Expand Down
83 changes: 62 additions & 21 deletions ckb-bin/src/subcommand/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::{self, Read};
use crate::helper::prompt;
use ckb_app_config::{cli, AppConfig, ExitCode, InitArgs};
use ckb_chain_spec::ChainSpec;
use ckb_jsonrpc_types::ScriptHashType;
use ckb_jsonrpc_types::{ScriptHashTypeKind, VmVersion};
use ckb_resource::{
Resource, TemplateContext, AVAILABLE_SPECS, CKB_CONFIG_FILE_NAME, DB_OPTIONS_FILE_NAME,
MINER_CONFIG_FILE_NAME, SPEC_DEV_FILE_NAME,
Expand Down Expand Up @@ -48,7 +48,6 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
let in_block_assembler_code_hash = prompt("code hash: ");
let in_args = prompt("args: ");
let in_hash_type = prompt("hash_type: ");
let in_message = prompt("message: ");

args.block_assembler_code_hash = Some(in_block_assembler_code_hash.trim().to_string());

Expand All @@ -58,12 +57,29 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
.map(|s| s.to_string())
.collect::<Vec<String>>();

args.block_assembler_message = Some(in_message.trim().to_string());
args.block_assembler_hash_type_kind =
match serde_plain::from_str::<ScriptHashTypeKind>(in_hash_type.trim()).ok() {
Some(hash_type) => hash_type,
None => {
eprintln!("Invalid block assembler hash type");
return Err(ExitCode::Failure);
}
};

match serde_plain::from_str::<ScriptHashType>(in_hash_type.trim()).ok() {
Some(hash_type) => args.block_assembler_hash_type = hash_type,
None => eprintln!("Invalid block assembler hash type"),
if args.block_assembler_hash_type_kind == ScriptHashTypeKind::Data {
let in_vm_version = prompt("vm_version: ");
args.block_assembler_hash_type_vm_version =
match serde_plain::from_str::<VmVersion>(in_vm_version.trim()).ok() {
Some(vm_version) => Some(vm_version),
None => {
eprintln!("Invalid block assembler vm version");
return Err(ExitCode::Failure);
}
};
}

let in_message = prompt("message: ");
args.block_assembler_message = Some(in_message.trim().to_string());
}

// Try to find the default secp256k1 from bundled chain spec.
Expand All @@ -90,11 +106,11 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
let block_assembler = match block_assembler_code_hash {
Some(hash) => {
if let Some(default_code_hash) = &default_code_hash_option {
if ScriptHashType::Type != args.block_assembler_hash_type {
if ScriptHashTypeKind::Type != args.block_assembler_hash_type_kind {
eprintln!(
"WARN: the default lock should use hash type `{}`, you are using `{}`.\n\
It will require `ckb run --ba-advanced` to enable this block assembler",
DEFAULT_LOCK_SCRIPT_HASH_TYPE, args.block_assembler_hash_type
DEFAULT_LOCK_SCRIPT_HASH_TYPE, args.block_assembler_hash_type_kind
);
} else if *default_code_hash != *hash {
eprintln!(
Expand All @@ -111,18 +127,43 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
);
}
}
format!(
"[block_assembler]\n\
code_hash = \"{}\"\n\
args = \"{}\"\n\
hash_type = \"{}\"\n\
message = \"{}\"",
hash,
args.block_assembler_args.join("\", \""),
serde_plain::to_string(&args.block_assembler_hash_type).unwrap(),
args.block_assembler_message
.unwrap_or_else(|| "0x".to_string()),
)
if let Some(vm_version) = &args.block_assembler_hash_type_vm_version {
if ScriptHashTypeKind::Type == args.block_assembler_hash_type_kind {
eprintln!("VM version is not allowed for hash-type \"type\".");
return Err(ExitCode::Failure);
}
format!(
"[block_assembler]\n\
code_hash = \"{}\"\n\
args = \"{}\"\n\
hash_type.kind = \"{}\"\n\
hash_type.vm_version = {}\n\
message = \"{}\"",
hash,
args.block_assembler_args.join("\", \""),
args.block_assembler_hash_type_kind,
vm_version,
args.block_assembler_message
.unwrap_or_else(|| "0x".to_string()),
)
} else {
if ScriptHashTypeKind::Data == args.block_assembler_hash_type_kind {
eprintln!("VM version should be provided for hash-type \"data\".");
return Err(ExitCode::Failure);
}
format!(
"[block_assembler]\n\
code_hash = \"{}\"\n\
args = \"{}\"\n\
hash_type.kind = \"{}\"\n\
message = \"{}\"",
hash,
args.block_assembler_args.join("\", \""),
args.block_assembler_hash_type_kind,
args.block_assembler_message
.unwrap_or_else(|| "0x".to_string()),
)
}
}
None => {
eprintln!("WARN: mining feature is disabled because of lacking the block assembler config options");
Expand All @@ -131,7 +172,7 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
# [block_assembler]\n\
# code_hash = \"{}\"\n\
# args = \"ckb-cli util blake2b --prefix-160 <compressed-pubkey>\"\n\
# hash_type = \"{}\"\n\
# hash_type.kind = \"{}\"\n\
# message = \"A 0x-prefixed hex string\"",
default_code_hash_option.unwrap_or_default(),
DEFAULT_LOCK_SCRIPT_HASH_TYPE,
Expand Down
2 changes: 1 addition & 1 deletion ckb-bin/src/subcommand/list_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn list_hashes(root_dir: PathBuf, matches: &ArgMatches<'_>) -> Result<(), Ex
resource = Resource::bundled_ckb_config();
}

let mut config: CKBAppConfig = toml::from_slice(&resource.get()?)?;
let mut config = CKBAppConfig::load_from_slice(&resource.get()?)?;
config.chain.spec.absolutize(&root_dir);
let chain_spec = ChainSpec::load_from(&config.chain.spec).map_err(to_config_error)?;
let spec_name = chain_spec.name.clone();
Expand Down
3 changes: 2 additions & 1 deletion devtools/doc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,8 @@ def handle_endtag(self, tag):
if self.variant_parser is not None:
self.variant_parser.handle_endtag(tag)
if self.variant_parser.completed():
self.variants.append((self.next_variant, self.variant_parser))
if self.next_variant not in [v[0] for v in self.variants]:
self.variants.append((self.next_variant, self.variant_parser))
self.next_variant = None
self.variant_parser = None

Expand Down
2 changes: 1 addition & 1 deletion docs/hashes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ index = 1

# Spec: ckb_dev
[ckb_dev]
spec_hash = "0xa8d7ab92196638cd091d49aa76849f6f102d8082eb374f089477d9c50a4bd2f1"
spec_hash = "0xc8dc9b41a93c64daf608231fd2061623c5cd73b10d6deb4ed22390324e358cc8"
genesis = "0x823b2ff5785b12da8b1363cac9a5cbe566d8b715a4311441b119c39a0367488c"
cellbase = "0xa563884b3686078ec7e7677a5f86449b15cf2693f3c1241766c6996f206cc541"

Expand Down
6 changes: 6 additions & 0 deletions resource/ckb-miner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
# staging => # Config generated by `ckb init --chain staging`
# }}

# Choose the edition, possible values:
# - "2019"
# - "2021"
# The default is "2019", the latest is "2021".
edition = "2021"

data_dir = "data"

[chain]
Expand Down
6 changes: 6 additions & 0 deletions resource/ckb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
# staging => # Config generated by `ckb init --chain staging`
# }}

# Choose the edition, possible values:
# - "2019"
# - "2021"
# The default is "2019", the latest is "2021".
edition = "2021"

data_dir = "data"

[chain]
Expand Down
28 changes: 22 additions & 6 deletions resource/specs/dev.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
name = "ckb_dev"

# Choose the edition, possible values:
# - "2019"
# - "2021"
# The default is "2019", the latest is "2021".
edition = "2021"

[genesis]
version = 0
parent_hash = "0x0000000000000000000000000000000000000000000000000000000000000000"
Expand All @@ -16,7 +22,8 @@ message = "ckb_dev" # {{
[genesis.genesis_cell.lock]
code_hash = "0x0000000000000000000000000000000000000000000000000000000000000000"
args = "0x"
hash_type = "data"
hash_type.kind = "data"
hash_type.vm_version = 0

# An array list paths to system cell files, which is absolute or relative to
# the directory containing this config file.
Expand All @@ -40,7 +47,8 @@ capacity = 100_000_0000_0000
[genesis.system_cells_lock]
code_hash = "0x0000000000000000000000000000000000000000000000000000000000000000"
args = "0x"
hash_type = "data"
hash_type.kind = "data"
hash_type.vm_version = 0

# Dep group cells
[[genesis.dep_groups]]
Expand All @@ -60,28 +68,29 @@ files = [
[genesis.bootstrap_lock]
code_hash = "0x0000000000000000000000000000000000000000000000000000000000000000"
args = "0x"
hash_type = "type"
hash_type.kind = "type"

# Burn
[[genesis.issued_cells]]
capacity = 8_400_000_000_00000000
lock.code_hash = "0x0000000000000000000000000000000000000000000000000000000000000000"
lock.args = "0x62e907b15cbf27d5425399ebf6f0fb50ebb88f18"
lock.hash_type = "data"
lock.hash_type.kind = "data"
lock.hash_type.vm_version = 0

# issue for random generated private key: d00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc
[[genesis.issued_cells]]
capacity = 20_000_000_000_00000000
lock.code_hash = "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"
lock.args = "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7"
lock.hash_type = "type"
lock.hash_type.kind = "type"

# issue for random generated private key: 63d86723e08f0f813a36ce6aa123bb2289d90680ae1e99d4de8cdb334553f24d
[[genesis.issued_cells]]
capacity = 5_198_735_037_00000000
lock.code_hash = "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"
lock.args = "0x470dcdc5e44064909650113a274b3b36aecb6dc7"
lock.hash_type = "type"
lock.hash_type.kind = "type"

[params]
initial_primary_epoch_reward = 1_917_808_21917808
Expand All @@ -95,5 +104,12 @@ genesis_epoch_length = 1000
# Keep difficulty be permanent if the pow is Dummy. (default: false)
permanent_difficulty_in_dummy = true

[params.hardfork]
rfc_pr_0221 = 0
rfc_pr_0222 = 0
rfc_pr_0223 = 0
rfc_pr_0224 = 0
rfc_pr_0237 = 0

[pow]
func = "Dummy"
Loading

0 comments on commit 79e47fb

Please sign in to comment.