//! # The Chain Specification //! //! By default, when simply running CKB, CKB will connect to the official public Nervos network. //! //! In order to run a chain different to the official public one, //! with a config file specifying `spec = { file = "" }` under `[chain]`. //! // Because the limitation of toml library, // we must put nested config struct in the tail to make it serializable, // details https://docs.rs/toml/0.5.0/toml/ser/index.html use crate::consensus::{ Consensus, ConsensusBuilder, SATOSHI_CELL_OCCUPIED_RATIO, SATOSHI_PUBKEY_HASH, TESTNET_ACTIVATION_THRESHOLD, TYPE_ID_CODE_HASH, build_genesis_dao_data, build_genesis_epoch_ext, }; use crate::versionbits::{ActiveMode, Deployment, DeploymentPos, VERSIONBITS_NUM_BITS}; use ckb_constant::hardfork::{mainnet, testnet}; use ckb_crypto::secp::Privkey; use ckb_hash::{blake2b_256, new_blake2b}; use ckb_jsonrpc_types::{JsonBytes, Script}; use ckb_pow::{Pow, PowEngine}; use ckb_resource::{ CODE_HASH_DAO, CODE_HASH_SECP256K1_BLAKE160_MULTISIG_ALL, CODE_HASH_SECP256K1_BLAKE160_SIGHASH_ALL, CODE_HASH_SECP256K1_DATA, Resource, }; use ckb_types::{ H160, H256, U128, bytes::Bytes, core::{ BlockBuilder, BlockNumber, BlockView, Capacity, Cycle, EpochNumber, EpochNumberWithFraction, Ratio, ScriptHashType, TransactionBuilder, TransactionView, capacity_bytes, hardfork::HardForks, }, h256, packed, prelude::*, }; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::error::Error; use std::fmt; use std::sync::Arc; pub use error::SpecError; pub use hardfork::HardForkConfig; pub mod consensus; mod error; mod hardfork; pub mod versionbits; #[cfg(test)] mod tests; // Just a random secp256k1 secret key for dep group input cell's lock const SPECIAL_CELL_PRIVKEY: H256 = h256!("0xd0c5c1e2d5af8b6ced3c0800937f996c1fa38c29186cade0cd8b5a73c97aaca3"); /// The output index of SECP256K1/blake160 script in the genesis no.0 transaction pub const OUTPUT_INDEX_SECP256K1_BLAKE160_SIGHASH_ALL: u64 = 1; /// The output index of DAO script in the genesis no.0 transaction pub const OUTPUT_INDEX_DAO: u64 = 2; /// The output data index of SECP256K1 in the genesis no.0 transaction pub const OUTPUT_INDEX_SECP256K1_DATA: u64 = 3; /// The output index of SECP256K1/multisig script in the genesis no.0 transaction pub const OUTPUT_INDEX_SECP256K1_BLAKE160_MULTISIG_ALL: u64 = 4; /// The CKB block chain specification #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct ChainSpec { /// The spec name, also used identify network pub name: String, /// The genesis block information pub genesis: Genesis, /// The block chain parameters #[serde(default)] pub params: Params, /// The block chain pow pub pow: Pow, #[serde(skip)] /// Hash of blake2b_256 spec content bytes, used for check consistency between database and config pub hash: packed::Byte32, } /// The default_params mod defines the default parameters for CKB Mainnet pub mod default_params { use crate::consensus::{ CELLBASE_MATURITY, DEFAULT_EPOCH_DURATION_TARGET, DEFAULT_ORPHAN_RATE_TARGET, DEFAULT_PRIMARY_EPOCH_REWARD_HALVING_INTERVAL, DEFAULT_SECONDARY_EPOCH_REWARD, GENESIS_EPOCH_LENGTH, INITIAL_PRIMARY_EPOCH_REWARD, MAX_BLOCK_BYTES, MAX_BLOCK_CYCLES, MAX_BLOCK_PROPOSALS_LIMIT, STARTING_BLOCK_LIMITING_DAO_WITHDRAWING_LOCK, }; use ckb_types::core::{Capacity, Cycle, EpochNumber}; /// The default initial_primary_epoch_reward /// /// Apply to [`initial_primary_epoch_reward`](../consensus/struct.Consensus.html#structfield.initial_primary_epoch_reward) pub fn initial_primary_epoch_reward() -> Capacity { INITIAL_PRIMARY_EPOCH_REWARD } /// The default secondary_epoch_reward /// /// Apply to [`secondary_epoch_reward`](../consensus/struct.Consensus.html#structfield.secondary_epoch_reward) pub fn secondary_epoch_reward() -> Capacity { DEFAULT_SECONDARY_EPOCH_REWARD } /// The default max_block_cycles /// /// Apply to [`max_block_cycles`](../consensus/struct.Consensus.html#structfield.max_block_cycles) pub fn max_block_cycles() -> Cycle { MAX_BLOCK_CYCLES } /// The default max_block_bytes /// /// Apply to [`max_block_bytes`](../consensus/struct.Consensus.html#structfield.max_block_bytes) pub fn max_block_bytes() -> u64 { MAX_BLOCK_BYTES } /// The default cellbase_maturity /// /// Apply to [`cellbase_maturity`](../consensus/struct.Consensus.html#structfield.cellbase_maturity) pub fn cellbase_maturity() -> u64 { CELLBASE_MATURITY.full_value() } /// The default primary_epoch_reward_halving_interval /// /// Apply to [`primary_epoch_reward_halving_interval`](../consensus/struct.Consensus.html#structfield.primary_epoch_reward_halving_interval) pub fn primary_epoch_reward_halving_interval() -> EpochNumber { DEFAULT_PRIMARY_EPOCH_REWARD_HALVING_INTERVAL } /// The default epoch_duration /// /// Apply to [`epoch_duration_target`](../consensus/struct.Consensus.html#structfield.epoch_duration_target) pub fn epoch_duration_target() -> u64 { DEFAULT_EPOCH_DURATION_TARGET } /// The default genesis_epoch_length /// /// Apply to [`genesis_epoch_length`](../consensus/struct.Consensus.html#structfield.genesis_epoch_length) pub fn genesis_epoch_length() -> u64 { GENESIS_EPOCH_LENGTH } /// The default max_block_proposals_limit /// /// Apply to [`max_block_proposals_limit`](../consensus/struct.Consensus.html#structfield.max_block_proposals_limit) pub fn max_block_proposals_limit() -> u64 { MAX_BLOCK_PROPOSALS_LIMIT } /// The default permanent_difficulty_in_dummy /// /// Apply to [`permanent_difficulty_in_dummy`](../consensus/struct.Consensus.html#structfield.permanent_difficulty_in_dummy) pub fn permanent_difficulty_in_dummy() -> bool { false } /// The default orphan_rate_target /// /// Apply to [`orphan_rate_target`](../consensus/struct.Consensus.html#structfield.orphan_rate_target) pub fn orphan_rate_target() -> (u32, u32) { DEFAULT_ORPHAN_RATE_TARGET } /// The default starting_block_limiting_dao_withdrawing_lock /// /// Apply to [`starting_block_limiting_dao_withdrawing_lock`](../consensus/struct.Consensus.html#structfield.starting_block_limiting_dao_withdrawing_lock) pub fn starting_block_limiting_dao_withdrawing_lock() -> u64 { STARTING_BLOCK_LIMITING_DAO_WITHDRAWING_LOCK } } /// Parameters for CKB block chain #[derive(Default, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Params { /// The initial_primary_epoch_reward /// /// See [`initial_primary_epoch_reward`](consensus/struct.Consensus.html#structfield.initial_primary_epoch_reward) #[serde(skip_serializing_if = "Option::is_none")] pub initial_primary_epoch_reward: Option, /// The secondary_epoch_reward /// /// See [`secondary_epoch_reward`](consensus/struct.Consensus.html#structfield.secondary_epoch_reward) #[serde(skip_serializing_if = "Option::is_none")] pub secondary_epoch_reward: Option, /// The max_block_cycles /// /// See [`max_block_cycles`](consensus/struct.Consensus.html#structfield.max_block_cycles) #[serde(skip_serializing_if = "Option::is_none")] pub max_block_cycles: Option, /// The max_block_bytes /// /// See [`max_block_bytes`](consensus/struct.Consensus.html#structfield.max_block_bytes) #[serde(skip_serializing_if = "Option::is_none")] pub max_block_bytes: Option, /// The cellbase_maturity /// /// See [`cellbase_maturity`](consensus/struct.Consensus.html#structfield.cellbase_maturity) #[serde(skip_serializing_if = "Option::is_none")] pub cellbase_maturity: Option, /// The primary_epoch_reward_halving_interval /// /// See [`primary_epoch_reward_halving_interval`](consensus/struct.Consensus.html#structfield.primary_epoch_reward_halving_interval) #[serde(skip_serializing_if = "Option::is_none")] pub primary_epoch_reward_halving_interval: Option, /// The epoch_duration_target /// /// See [`epoch_duration_target`](consensus/struct.Consensus.html#structfield.epoch_duration_target) #[serde(skip_serializing_if = "Option::is_none")] pub epoch_duration_target: Option, /// The genesis_epoch_length /// /// See [`genesis_epoch_length`](consensus/struct.Consensus.html#structfield.genesis_epoch_length) #[serde(skip_serializing_if = "Option::is_none")] pub genesis_epoch_length: Option, /// The permanent_difficulty_in_dummy /// /// See [`permanent_difficulty_in_dummy`](consensus/struct.Consensus.html#structfield.permanent_difficulty_in_dummy) #[serde(skip_serializing_if = "Option::is_none")] pub permanent_difficulty_in_dummy: Option, /// The max_block_proposals_limit /// /// See [`max_block_proposals_limit`](consensus/struct.Consensus.html#structfield.max_block_proposals_limit) #[serde(skip_serializing_if = "Option::is_none")] pub max_block_proposals_limit: Option, /// The orphan_rate_target /// /// See [`orphan_rate_target`](consensus/struct.Consensus.html#structfield.orphan_rate_target) #[serde(skip_serializing_if = "Option::is_none")] pub orphan_rate_target: Option<(u32, u32)>, /// The starting_block_limiting_dao_withdrawing_lock. /// /// See [`starting_block_limiting_dao_withdrawing_lock`](consensus/struct.Consensus.html#structfield.starting_block_limiting_dao_withdrawing_lock) #[serde(skip_serializing_if = "Option::is_none")] pub starting_block_limiting_dao_withdrawing_lock: Option, /// The parameters for hard fork features. /// /// See [`hardfork_switch`](consensus/struct.Consensus.html#structfield.hardfork_switch) #[serde(skip_serializing_if = "Option::is_none")] pub hardfork: Option, } impl Params { /// Return the `initial_primary_epoch_reward`, otherwise if None, returns the default value pub fn initial_primary_epoch_reward(&self) -> Capacity { self.initial_primary_epoch_reward .unwrap_or_else(default_params::initial_primary_epoch_reward) } /// Return the `secondary_epoch_reward`, otherwise if None, returns the default value pub fn secondary_epoch_reward(&self) -> Capacity { self.secondary_epoch_reward .unwrap_or_else(default_params::secondary_epoch_reward) } /// Return the `max_block_cycles`, otherwise if None, returns the default value pub fn max_block_cycles(&self) -> Cycle { self.max_block_cycles .unwrap_or_else(default_params::max_block_cycles) } /// Return the `max_block_bytes`, otherwise if None, returns the default value pub fn max_block_bytes(&self) -> u64 { self.max_block_bytes .unwrap_or_else(default_params::max_block_bytes) } /// Return the `cellbase_maturity`, otherwise if None, returns the default value pub fn cellbase_maturity(&self) -> u64 { self.cellbase_maturity .unwrap_or_else(default_params::cellbase_maturity) } /// Return the `primary_epoch_reward_halving_interval`, otherwise if None, returns the default value pub fn primary_epoch_reward_halving_interval(&self) -> EpochNumber { self.primary_epoch_reward_halving_interval .unwrap_or_else(default_params::primary_epoch_reward_halving_interval) } /// Return the `permanent_difficulty_in_dummy`, otherwise if None, returns the default value pub fn permanent_difficulty_in_dummy(&self) -> bool { self.permanent_difficulty_in_dummy .unwrap_or_else(default_params::permanent_difficulty_in_dummy) } /// Return the `epoch_duration_target`, otherwise if None, returns the default value pub fn epoch_duration_target(&self) -> u64 { self.epoch_duration_target .unwrap_or_else(default_params::epoch_duration_target) } /// Return the `genesis_epoch_length`, otherwise if None, returns the default value pub fn genesis_epoch_length(&self) -> BlockNumber { self.genesis_epoch_length .unwrap_or_else(default_params::genesis_epoch_length) } /// Return the `max_block_proposals_limit`, otherwise if None, returns the default value pub fn max_block_proposals_limit(&self) -> BlockNumber { self.max_block_proposals_limit .unwrap_or_else(default_params::max_block_proposals_limit) } /// Return the `orphan_rate_target`, otherwise if None, returns the default value pub fn orphan_rate_target(&self) -> (u32, u32) { self.orphan_rate_target .unwrap_or_else(default_params::orphan_rate_target) } /// Return the `starting_block_limiting_dao_withdrawing_lock`, otherwise if None, returns the default value pub fn starting_block_limiting_dao_withdrawing_lock(&self) -> u64 { self.starting_block_limiting_dao_withdrawing_lock .unwrap_or_else(default_params::starting_block_limiting_dao_withdrawing_lock) } } /// The genesis information /// Load from config file. #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Genesis { /// The genesis block version pub version: u32, /// The genesis block parent_hash pub parent_hash: H256, /// The genesis block timestamp pub timestamp: u64, /// The genesis block compact_target pub compact_target: u32, /// The genesis block uncles_hash pub uncles_hash: H256, /// The genesis block hash /// /// If hash is provided, it will be used to check whether match with actual calculated hash pub hash: Option, /// The genesis block nonce pub nonce: U128, /// The genesis block issued_cells /// /// Initial token supply pub issued_cells: Vec, /// The genesis cell /// /// The genesis cell contains a message for identity pub genesis_cell: GenesisCell, /// The system cells /// /// The initial system cells, such SECP256K1/blake160, DAO. pub system_cells: Vec, /// The system cells' lock pub system_cells_lock: Script, /// For block 1~11, the reward target is genesis block. /// Genesis block must have the lock serialized in the cellbase witness, which is set to `bootstrap_lock`. pub bootstrap_lock: Script, /// The genesis dep_groups file resource /// /// see detail [dep-group](https://github.com/nervosnetwork/rfcs/blob/f639fa8b30b5568b895449b7ab3ef4ad40ca077a/rfcs/0022-transaction-structure/0022-transaction-structure.md#dep-group) pub dep_groups: Vec, /// The burned 25% of Nervos CKBytes in genesis block #[serde(default)] pub satoshi_gift: SatoshiGift, } /// The system cell information #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct SystemCell { // NOTE: must put `create_type_id` before `file` otherwise this struct can not serialize /// whether crate type script pub create_type_id: bool, /// Overwrite the cell capacity. Set to None to use the minimal capacity. pub capacity: Option, /// The file resource pub file: Resource, } /// The genesis cell information #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct GenesisCell { /// The genesis cell message pub message: String, /// The genesis cell lock pub lock: Script, } /// Initial token supply cell #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct IssuedCell { /// The cell capacity pub capacity: Capacity, /// The cell data, can be ignored in spec pub data: Option, /// The cell type script #[serde(rename = "type")] pub type_: Option