diff --git a/Cargo.lock b/Cargo.lock index 31b7f3f027..b6f30ad63c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,8 @@ +[[package]] +name = "adler32" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "aes-ctr" version = "0.3.0" @@ -212,6 +217,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "build-info" version = "0.8.0-pre" +[[package]] +name = "build_const" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "byte-tools" version = "0.3.1" @@ -355,8 +365,11 @@ version = "0.8.0-pre" dependencies = [ "ckb-core 0.8.0-pre", "ckb-pow 0.8.0-pre", + "includedir 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "includedir_codegen 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "phf 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -764,6 +777,22 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "crc" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "build_const 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "crc32fast" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "criterion" version = "0.2.10" @@ -1059,6 +1088,17 @@ dependencies = [ "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "flate2" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", + "miniz-sys 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "miniz_oxide_c_api 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "fnv" version = "1.0.6" @@ -1253,6 +1293,25 @@ dependencies = [ "unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "includedir" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "flate2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "phf 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "includedir_codegen" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "flate2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_codegen 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "indexmap" version = "1.0.2" @@ -1510,6 +1569,34 @@ name = "merkle-cbt" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "miniz-sys" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "miniz_oxide" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "miniz_oxide_c_api" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", + "miniz_oxide 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mio" version = "0.6.16" @@ -1751,6 +1838,40 @@ name = "percent-encoding" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "phf" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "phf_codegen" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "phf_generator 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "phf_generator" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "phf_shared" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "pkg-config" version = "0.3.14" @@ -2236,6 +2357,11 @@ dependencies = [ "opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "siphasher" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "siphasher" version = "0.3.0" @@ -2987,6 +3113,7 @@ dependencies = [ ] [metadata] +"checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c" "checksum aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d2e5b0458ea3beae0d1d8c0f3946564f8e10f90646cf78c06b4351052058d1ee" "checksum aes-soft 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cfd7e7ae3f9a1fb5c03b389fc6bb9a51400d0c13053f0dca698c832bfd893a0d" "checksum aesni 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f70a6b5f971e473091ab7cfb5ffac6cde81666c4556751d8d5620ead8abf100" @@ -3011,6 +3138,7 @@ dependencies = [ "checksum block-padding 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d75255892aeb580d3c566f213a2b6fdc1c66667839f45719ee1d30ebf2aea591" "checksum bloom-filters 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b233192e97c6e528c071bd83180c5d4f35846b9aff4670ea8e05e3ba0daa5e30" "checksum bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0de79cfb98e7aa9988188784d8664b4b5dad6eaaa0863b91d9a4ed871d4f7a42" +"checksum build_const 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39092a32794787acd8525ee150305ff051b0aa6cc2abaf193924f5ab05425f39" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" @@ -3026,6 +3154,8 @@ dependencies = [ "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum config 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e82d07fac0a5eeaa9d959b5194d01bb66e414665f547416958d2b430f8f4852" "checksum console 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2bf3720d3f3fc30b721ef1ae54e13af3264af4af39dc476a8de56a6ee1e2184b" +"checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb" +"checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" "checksum criterion 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "1c6e5ee5b9652d4f851418c448af105642e1f99e9a2741a8ff45c0d2c911b1e0" "checksum criterion-plot 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4107e4a5abb94267e0149922b8ff49dc70a87cc202820fdbfc0d3e1edbdc4b16" "checksum crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0f0ed1a4de2235cabda8558ff5840bffb97fcb64c97827f354a451307df5f72b" @@ -3058,6 +3188,7 @@ dependencies = [ "checksum faster-hex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2ea2e4ecc921ec2cbb8b10f0d400fe448554779d2ac5e0bfbb065836d9d8483a" "checksum faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b8cccaafb5aae8c282692e5590f341925edea6c696e8715ff0d973320b2646" "checksum flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea0c34f669be9911826facafe996adfda978aeee67285a13556869e2d8b8331f" +"checksum flate2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f87e68aa82b2de08a6e037f1385455759df6e445a8df5e005b4297191dbf18aa" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" @@ -3078,6 +3209,8 @@ dependencies = [ "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" "checksum hyper 0.12.25 (registry+https://github.com/rust-lang/crates.io-index)" = "7d5b6658b016965ae301fa995306db965c93677880ea70765a84235a96eae896" "checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" +"checksum includedir 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e97402f770a519ebea51b27131c3b6558cfd2375aff21294bad806bad91bf0b6" +"checksum includedir_codegen 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "af7d542be113fd84855692fb536c16cc4c09527724d1dca8953047d71cccadef" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" "checksum indicatif 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2c60da1c9abea75996b70a931bba6c750730399005b61ccd853cee50ef3d0d0c" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" @@ -3106,6 +3239,9 @@ dependencies = [ "checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum merkle-cbt 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d589b5a7ca642540e7ccfbca3bcd0aa18693eb9287e2a6b17c79b1d062d52863" +"checksum miniz-sys 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0300eafb20369952951699b68243ab4334f4b10a88f411c221d444b36c40e649" +"checksum miniz_oxide 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c468f2369f07d651a5d0bb2c9079f8488a66d5466efe42d0c5c6466edcb7f71e" +"checksum miniz_oxide_c_api 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b7fe927a42e3807ef71defb191dc87d4e24479b221e67015fe38ae2b7b447bab" "checksum mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "71646331f2619b1026cc302f87a2b8b648d5c6dd6937846a16cc8ce0f347f432" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" @@ -3130,6 +3266,10 @@ dependencies = [ "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" "checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" +"checksum phf 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "b3da44b85f8e8dfaec21adae67f95d93244b2ecf6ad2a692320598dcc8e6dd18" +"checksum phf_codegen 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "b03e85129e324ad4166b06b2c7491ae27fe3ec353af72e72cd1654c7225d517e" +"checksum phf_generator 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662" +"checksum phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0" "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum plain 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" "checksum proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2c725b36c99df7af7bf9324e9c999b9e37d92c8f8caf106d82e1d7953218d2d8" @@ -3187,6 +3327,7 @@ dependencies = [ "checksum serde_test 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "110b3dbdf8607ec493c22d5d947753282f3bae73c0f56d322af1e8c78e4c23d5" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" "checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" +"checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" "checksum siphasher 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9913c75df657d84a03fa689c016b0bb2863ff0b497b26a8d6e9703f8d5df03a8" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be" diff --git a/nodes_template/default.toml b/nodes_template/default.toml index 23724b22c9..9188f08c1b 100644 --- a/nodes_template/default.toml +++ b/nodes_template/default.toml @@ -4,7 +4,7 @@ data_dir = "default" path = "default/db" [chain] -spec = "spec/dev.toml" +spec = { Local = "spec/dev.toml" } [logger] file = "ckb.log" diff --git a/nodes_template/miner.toml b/nodes_template/miner.toml index dd7896bf66..dceaca40ff 100644 --- a/nodes_template/miner.toml +++ b/nodes_template/miner.toml @@ -1,5 +1,5 @@ data_dir = "default" -chain = "spec/dev.toml" +chain = { Local = "spec/dev.toml" } rpc_url = "http://127.0.0.1:8114/" cycles_limit = 100000000 bytes_limit = 10000000 diff --git a/spec/Cargo.toml b/spec/Cargo.toml index d16b6f77d2..215e318bb6 100644 --- a/spec/Cargo.toml +++ b/spec/Cargo.toml @@ -4,6 +4,8 @@ version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" +build = "build.rs" +include = ["/chainspecs"] [dependencies] serde = "1.0" @@ -13,3 +15,8 @@ numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heap numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] } ckb-core = { path = "../core" } ckb-pow = { path = "../pow" } +includedir = "0.5.0" +phf = "0.7.21" + +[build-dependencies] +includedir_codegen = "0.5.0" diff --git a/spec/build.rs b/spec/build.rs new file mode 100644 index 0000000000..69b7c92dbb --- /dev/null +++ b/spec/build.rs @@ -0,0 +1,8 @@ +use includedir_codegen::Compression; + +fn main() { + includedir_codegen::start("FILES") + .dir("chainspecs", Compression::Gzip) + .build("chainspecs.rs") + .unwrap(); +} diff --git a/spec/chainspecs/testnet/cells/secp256k1_blake2b_lock b/spec/chainspecs/testnet/cells/secp256k1_blake2b_lock new file mode 100755 index 0000000000..2c0f2e8934 Binary files /dev/null and b/spec/chainspecs/testnet/cells/secp256k1_blake2b_lock differ diff --git a/spec/chainspecs/testnet/testnet.toml b/spec/chainspecs/testnet/testnet.toml new file mode 100644 index 0000000000..09354d9402 --- /dev/null +++ b/spec/chainspecs/testnet/testnet.toml @@ -0,0 +1,36 @@ +name = "ckb_testnet" + +[genesis] +version = 0 +parent_hash = "0x0000000000000000000000000000000000000000000000000000000000000000" +timestamp = 0 +txs_commit = "0x0000000000000000000000000000000000000000000000000000000000000000" +txs_proposal = "0x0000000000000000000000000000000000000000000000000000000000000000" +difficulty = "0x100" +cellbase_id = "0x0000000000000000000000000000000000000000000000000000000000000000" +uncles_hash = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[genesis.seal] +nonce = 0 +proof = [0] + +[params] +initial_block_reward = 50000 +max_block_cycles = 100000000 + +[pow] +func = "Cuckoo" + +[pow.params] +# the 2-log of the graph size, which is the size in bits of the node +# identifiers +edge_bits = 15 + +# length of the cycle to be found, must be an even number, a minimum of 12 is +# recommended +cycle_length = 12 + +# An array list paths to system cell files, which is absolute or relative to +# the directory containing this config file. +[[system_cells]] +path = "cells/secp256k1_blake2b_lock" diff --git a/spec/src/lib.rs b/spec/src/lib.rs index 403b626fe9..480782a80e 100644 --- a/spec/src/lib.rs +++ b/spec/src/lib.rs @@ -5,6 +5,9 @@ //! In order to run a chain different to the official public one, //! with a config file specifying chain = "path" under [ckb]. +// Shields clippy errors in generated chainspecs.rs file. +#![allow(clippy::unreadable_literal)] + use crate::consensus::Consensus; use ckb_core::block::BlockBuilder; use ckb_core::header::HeaderBuilder; @@ -18,11 +21,66 @@ use serde_derive::Deserialize; use std::error::Error; use std::fs::File; use std::io::Read; -use std::path::{Path, PathBuf}; +use std::path::{Display, Path, PathBuf}; use std::sync::Arc; pub mod consensus; +include!(concat!(env!("OUT_DIR"), "/chainspecs.rs")); + +#[derive(Clone, Debug, PartialEq, Eq, Deserialize)] +pub enum SpecPath { + Testnet, + Local(PathBuf), +} + +impl SpecPath { + pub fn display(&self) -> Display { + match self { + SpecPath::Testnet => Path::new("Testnet").display(), + SpecPath::Local(path) => path.display(), + } + } + + pub fn expand_path>(&self, base: P) -> Self { + match self { + SpecPath::Testnet => SpecPath::Testnet, + SpecPath::Local(path) => { + if path.is_relative() { + SpecPath::Local(base.as_ref().join(path)) + } else { + SpecPath::Local(path.to_path_buf()) + } + } + } + } + + fn path(&self) -> PathBuf { + match self { + SpecPath::Testnet => PathBuf::from("testnet/testnet.toml"), + SpecPath::Local(path) => PathBuf::from(path), + } + } + + fn load_file>(&self, path: P) -> Result, Box> { + match self { + SpecPath::Testnet => { + let s = path.as_ref().to_str().expect("chain spec path"); + Ok(FILES + .get(&format!("chainspecs/{}", s)) + .expect("hardcoded spec") + .to_vec()) + } + SpecPath::Local(_) => { + let mut file = File::open(&path)?; + let mut data = Vec::new(); + file.read_to_end(&mut data)?; + Ok(data) + } + } + } +} + #[derive(Clone, PartialEq, Eq, Debug, Deserialize)] pub struct ChainSpec { pub name: String, @@ -64,12 +122,11 @@ pub struct SystemCell { pub(self) fn build_system_cell_transaction( cells: &[SystemCell], + spec_path: &SpecPath, ) -> Result> { let mut outputs = Vec::new(); for system_cell in cells { - let mut file = File::open(&system_cell.path)?; - let mut data = Vec::new(); - file.read_to_end(&mut data)?; + let data = spec_path.load_file(&system_cell.path)?; // TODO: we should provide a proper lock script here so system cells // can be updated. @@ -81,10 +138,12 @@ pub(self) fn build_system_cell_transaction( } impl ChainSpec { - pub fn read_from_file>(path: P) -> Result> { - let config_str = std::fs::read_to_string(path.as_ref())?; + pub fn read_from_file(spec_path: &SpecPath) -> Result> { + let config_bytes = spec_path.load_file(spec_path.path())?; + let config_str = String::from_utf8(config_bytes)?; let mut spec: Self = toml::from_str(&config_str)?; - spec.resolve_paths(path.as_ref().parent().expect("chain spec path resolve")); + spec.resolve_paths(spec_path.path().parent().expect("chain spec path resolve")); + Ok(spec) } @@ -92,7 +151,7 @@ impl ChainSpec { self.pow.engine() } - pub fn to_consensus(&self) -> Result> { + pub fn to_consensus(&self, spec_path: &SpecPath) -> Result> { let header = HeaderBuilder::default() .version(self.genesis.version) .parent_hash(self.genesis.parent_hash.clone()) @@ -107,7 +166,10 @@ impl ChainSpec { .build(); let genesis_block = BlockBuilder::default() - .commit_transaction(build_system_cell_transaction(&self.system_cells)?) + .commit_transaction(build_system_cell_transaction( + &self.system_cells, + &spec_path, + )?) .header(header) .build(); @@ -142,9 +204,9 @@ pub mod test { .join("../nodes_template/spec/dev.toml") .display() ); - let dev = ChainSpec::read_from_file( + let dev = ChainSpec::read_from_file(&SpecPath::Local( Path::new(env!("CARGO_MANIFEST_DIR")).join("../nodes_template/spec/dev.toml"), - ); + )); assert!(dev.is_ok(), format!("{:?}", dev)); for cell in &dev.unwrap().system_cells { assert!(cell.path.exists()); @@ -153,12 +215,18 @@ pub mod test { #[test] fn always_success_type_hash() { + let spec_path = SpecPath::Local( + Path::new(env!("CARGO_MANIFEST_DIR")).join("../nodes_template/spec/dev.toml"), + ); let always_success_path = Path::new(env!("CARGO_MANIFEST_DIR")) .join("../nodes_template/spec/cells/always_success"); - let tx = build_system_cell_transaction(&[SystemCell { - path: always_success_path, - }]) + let tx = build_system_cell_transaction( + &[SystemCell { + path: always_success_path, + }], + &spec_path, + ) .unwrap(); // Tx and Output hash will be used in some test cases directly, assert here for convenience @@ -179,4 +247,22 @@ pub mod test { "9a9a6bdbc38d4905eace1822f85237e3a1e238bb3f277aa7b7c8903441123510" ); } + + #[test] + fn test_testnet_chain_spec_load() { + let spec_path = SpecPath::Testnet; + let result = ChainSpec::read_from_file(&spec_path); + assert!(result.is_ok(), format!("{:?}", result)); + let chain_spec = result.unwrap(); + + let result = build_system_cell_transaction(&chain_spec.system_cells, &spec_path); + assert!(result.is_ok(), format!("{:?}", result)); + let tx = result.unwrap(); + + let data_hash = tx.outputs()[0].data_hash(); + assert_eq!( + format!("{:x}", data_hash), + "fe1cf5a297023a3c5282ecd9b0ca88d6736424d75fbe4dcf47a7c8b303e4d339" + ); + } } diff --git a/src/cli/export.rs b/src/cli/export.rs index effd7d6b3e..be2bc9a1ae 100644 --- a/src/cli/export.rs +++ b/src/cli/export.rs @@ -10,7 +10,12 @@ pub fn export(setup: &Setup, matches: &ArgMatches) { let target = value_t!(matches.value_of("target"), String).unwrap_or_else(|e| e.exit()); let shared = SharedBuilder::>::default() - .consensus(setup.chain_spec.to_consensus().unwrap()) + .consensus( + setup + .chain_spec + .to_consensus(&setup.configs.chain.spec) + .unwrap(), + ) .db(&setup.configs.db) .build(); Export::new(shared, format, target.into()) diff --git a/src/cli/import.rs b/src/cli/import.rs index 9dd30e6894..c4c12fca24 100644 --- a/src/cli/import.rs +++ b/src/cli/import.rs @@ -12,7 +12,12 @@ pub fn import(setup: &Setup, matches: &ArgMatches) { let source = value_t!(matches.value_of("source"), String).unwrap_or_else(|e| e.exit()); let shared = SharedBuilder::>::default() - .consensus(setup.chain_spec.to_consensus().unwrap()) + .consensus( + setup + .chain_spec + .to_consensus(&setup.configs.chain.spec) + .unwrap(), + ) .db(&setup.configs.db) .build(); diff --git a/src/cli/miner.rs b/src/cli/miner.rs index 588f2b179a..9daec0ba00 100644 --- a/src/cli/miner.rs +++ b/src/cli/miner.rs @@ -1,5 +1,5 @@ use crate::helper::{require_path_exists, to_absolute_path}; -use ckb_chain_spec::ChainSpec; +use ckb_chain_spec::{ChainSpec, SpecPath}; use ckb_miner::{Client, Miner, MinerConfig}; use ckb_util::Mutex; use clap::ArgMatches; @@ -19,15 +19,13 @@ struct Config { pub logger: LogConfig, #[serde(flatten)] pub miner: MinerConfig, - pub chain: PathBuf, + pub chain: SpecPath, pub data_dir: PathBuf, } impl Config { fn resolve_paths(&mut self, base: &Path) { - if self.chain.is_relative() { - self.chain = base.join(&self.chain); - } + self.chain = self.chain.expand_path(base); if self.data_dir.is_relative() { self.data_dir = base.join(&self.data_dir); diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index 43839d0f2a..b21f09154a 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -20,7 +20,10 @@ use numext_fixed_hash::H256; use std::sync::Arc; pub fn run(setup: Setup) { - let consensus = setup.chain_spec.to_consensus().unwrap(); + let consensus = setup + .chain_spec + .to_consensus(&setup.configs.chain.spec) + .unwrap(); let shared = SharedBuilder::>::default() .consensus(consensus) diff --git a/src/setup.rs b/src/setup.rs index 035343912d..6b8a366263 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -1,5 +1,5 @@ use crate::helper::{require_path_exists, to_absolute_path}; -use ckb_chain_spec::ChainSpec; +use ckb_chain_spec::{ChainSpec, SpecPath}; use ckb_db::DBConfig; use ckb_miner::BlockAssemblerConfig; use ckb_network::NetworkConfig; @@ -25,7 +25,7 @@ pub struct Setup { #[derive(Clone, Debug, Deserialize)] pub struct ChainConfig { - pub spec: PathBuf, + pub spec: SpecPath, } #[derive(Clone, Debug, Deserialize)] @@ -103,9 +103,7 @@ impl Configs { if self.data_dir.is_relative() { self.data_dir = base.join(&self.data_dir); } - if self.chain.spec.is_relative() { - self.chain.spec = base.join(&self.chain.spec); - } + self.chain.spec = self.chain.spec.expand_path(base); if self.db.path.is_relative() { self.db.path = base.join(&self.db.path); } @@ -243,7 +241,7 @@ pub mod test { let test_config = format!( r#" [chain] - spec = "{}" + spec = {{ Local = "{}" }} "#, chain_spec_path.to_str().unwrap() ); @@ -256,4 +254,26 @@ pub mod test { assert!(setup.is_ok()); assert_eq!(setup.unwrap().chain_spec.name, "ckb_test_custom"); } + + #[test] + fn test_testnet_chain_spec_with_config() { + let tmp_dir = tempfile::Builder::new() + .prefix("test_testnet_chain_spec_with_config") + .tempdir() + .unwrap(); + + let test_config = r#" + [chain] + spec = "Testnet" + "#; + + let config_path = tmp_dir.path().join("config.toml"); + write_file(&config_path, &test_config); + + let setup = override_default_config_file(&config_path); + assert!(setup.is_ok()); + let setup = setup.unwrap(); + assert_eq!(setup.configs.chain.spec, SpecPath::Testnet); + assert_eq!(setup.chain_spec.name, "ckb_testnet"); + } } diff --git a/test/fixtures/nodes_template/default.toml b/test/fixtures/nodes_template/default.toml index 2b7c7aa77b..7f2fcd904e 100644 --- a/test/fixtures/nodes_template/default.toml +++ b/test/fixtures/nodes_template/default.toml @@ -4,7 +4,7 @@ data_dir = "default" path = "default/db" [chain] -spec = "spec/integration_test.toml" +spec = { Local = "spec/integration_test.toml" } [logger] file = "ckb.log"