diff --git a/.github/actions/ci_prologue/ci_prologue.sh b/.github/actions/ci_prologue/ci_prologue.sh index 5868183bb9..5adbfcd48c 100755 --- a/.github/actions/ci_prologue/ci_prologue.sh +++ b/.github/actions/ci_prologue/ci_prologue.sh @@ -43,7 +43,7 @@ fun_pasing_message(){ if [ $? -eq 0 ]; then job_run_list=`echo "${MESSAGE}"| grep "ci-runs-only" | awk -F ':' '{print $2}'` else - job_run_list=" [ quick_checks,unit_tests,integration_tests,benchmarks,linters,wasm_build,cargo_deny,aarch64_build ] " + job_run_list=" [ quick_checks,unit_tests,integration_tests,benchmarks,linters,cargo_deny,aarch64_build ] " fi echo "job_run_list is ""$job_run_list" #parsing runs os @@ -103,7 +103,7 @@ if [[ $GITHUB_EVENT_NAME == "pull_request" ]];then else runs_on=" [ ubuntu,macos,windows ] " fun_run_os "$runs_on" - job_run_list=" [ quick_checks,unit_tests,integration_tests,benchmarks,linters,wasm_build,cargo_deny,aarch64_build ] " + job_run_list=" [ quick_checks,unit_tests,integration_tests,benchmarks,linters,cargo_deny,aarch64_build ] " fun_jobs "$job_run_list" if [[ "$GITHUB_REPOSITORY" == "nervosnetwork/ckb" ]];then echo "linux_runner_label=self-hosted-ci-ubuntu-20.04" >> $GITHUB_OUTPUT diff --git a/.github/workflows/ci_wasm_build_ubuntu.yaml b/.github/workflows/ci_wasm_build_ubuntu.yaml deleted file mode 100644 index 22fddfd1b7..0000000000 --- a/.github/workflows/ci_wasm_build_ubuntu.yaml +++ /dev/null @@ -1,68 +0,0 @@ -name: ci_wasm_build_ubuntu -concurrency: - group: ci_wasm_build_ubuntu-${{ github.ref }} - cancel-in-progress: true -on: - pull_request: - types: [ opened, synchronize, reopened ] - push: - branches: - - master - - develop - - 'rc/*' - merge_group: {} - -env: - CARGO_TERM_COLOR: always - RUST_BACKTRACE: full - RUSTFLAGS: -D warnings -jobs: - prologue: - name: prologue - if: | - github.event_name != 'push' || - ( github.event_name == 'push' && - ( github.ref == 'refs/heads/master' || - (github.ref == 'refs/heads/develop' && startsWith(github.event.head_commit.message, 'Merge pull request #')) || - startsWith(github.ref, 'refs/heads/rc/') - ) - ) || (github.repository_owner != 'nervosnetwork') - runs-on: ubuntu-20.04 - outputs: - os_skip: ${{ steps.prologue.outputs.os_skip }} - job_skip: ${{ steps.prologue.outputs.job_skip }} - linux_runner_label: ${{ steps.prologue.outputs.linux_runner_label }} - steps: - - uses: actions/checkout@v3 - - name: prologue - id: prologue - uses: ./.github/actions/ci_prologue - with: - GITHUB_EVENT_NAME: ${{ github.event_name }} - COMMIT_MESSAGE: "${{github.event.head_commit.message}}" - PR_COMMONS_BODY: "${{ github.event.pull_request.body }}" - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - LABELS: "${{ toJson(github.event.pull_request.labels.*.name) }}" - GITHUB_REPOSITORY: ${{ github.repository }} - GITHUB_WORKFLOW: ${{ github.workflow }} - ci_wasm_build_ubuntu: - name: ci_wasm_build_ubuntu - needs: prologue - runs-on: ${{ needs.prologue.outputs.linux_runner_label }} - steps: - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: 1.71.1 - - run: sudo apt-get update && sudo apt-get install -y pkg-config clang - - uses: actions/checkout@v3 - - name: ci_wasm_build_ubuntu - run: | - if [[ ${{ needs.prologue.outputs.os_skip }} == run ]] && [[ ${{ needs.prologue.outputs.job_skip }} == run ]];then - devtools/ci/ci_main.sh - else - echo "skip job" - exit 0 - fi - env: - RUNNER_LABEL: ${{ needs.prologue.outputs.runner_label }} diff --git a/Cargo.lock b/Cargo.lock index 3e3b8cda75..83b7b4d522 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -179,9 +179,9 @@ dependencies = [ [[package]] name = "base64" -version = "0.21.4" +version = "0.21.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ba43ea6f343b788c8764558649e08df62f86c6ef251fdaeb1ffd010a9ae50a2" +checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9" [[package]] name = "bindgen" @@ -341,6 +341,33 @@ dependencies = [ "serde", ] +[[package]] +name = "cacache" +version = "12.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "142316461ed3a3dfcba10417317472da5bfd0461e4d276bf7c07b330766d9490" +dependencies = [ + "digest 0.10.7", + "either", + "futures", + "hex", + "libc", + "memmap2", + "miette", + "reflink-copy", + "serde", + "serde_derive", + "serde_json", + "sha1", + "sha2", + "ssri", + "tempfile", + "thiserror", + "tokio", + "tokio-stream", + "walkdir", +] + [[package]] name = "cast" version = "0.3.0" @@ -618,19 +645,21 @@ dependencies = [ name = "ckb-chain-spec" version = "0.113.0-pre" dependencies = [ + "cacache", "ckb-constant", "ckb-crypto", "ckb-dao-utils", "ckb-error", "ckb-hash", "ckb-jsonrpc-types", + "ckb-logger", "ckb-pow", "ckb-rational", "ckb-resource", "ckb-traits", "ckb-types", - "ckb-util", "serde", + "tempfile", "toml", ] @@ -1080,7 +1109,7 @@ dependencies = [ "tempfile", "tentacle", "tokio", - "tokio-util 0.7.9", + "tokio-util 0.7.10", "trust-dns-resolver", ] @@ -1351,7 +1380,7 @@ dependencies = [ "once_cell", "rand 0.8.5", "tokio", - "tokio-util 0.7.9", + "tokio-util 0.7.10", ] [[package]] @@ -1493,7 +1522,7 @@ dependencies = [ "slab", "tempfile", "tokio", - "tokio-util 0.7.9", + "tokio-util 0.7.10", ] [[package]] @@ -1627,18 +1656,18 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.6" +version = "4.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956" +checksum = "ac495e00dcec98c83465d5ad66c5c4fabd652fd6686e7c6269b117e729a6f17b" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" -version = "4.4.6" +version = "4.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45" +checksum = "c77ed9a32a62e6ca27175d00d29d05ca32e396ea1eb5fb01d8256b669cec7663" dependencies = [ "anstream", "anstyle", @@ -1649,9 +1678,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.5.1" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" [[package]] name = "colorchoice" @@ -1674,23 +1703,21 @@ dependencies = [ [[package]] name = "const-random" -version = "0.1.15" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "368a7a772ead6ce7e1de82bfb04c485f3db8ec744f72925af5735e29a22cc18e" +checksum = "11df32a13d7892ec42d51d3d175faba5211ffe13ed25d4fb348ac9e9ce835593" dependencies = [ "const-random-macro", - "proc-macro-hack", ] [[package]] name = "const-random-macro" -version = "0.1.15" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d7d6ab3c3a2282db210df5f02c4dab6e0a7057af0fb7ebd4070f30fe05c0ddb" +checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" dependencies = [ "getrandom 0.2.10", "once_cell", - "proc-macro-hack", "tiny-keccak", ] @@ -1988,9 +2015,9 @@ dependencies = [ [[package]] name = "dlv-list" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8aead04dc46b5f263c25721cf25c9e595951d15055f8063f92392fa0d7f64cf4" +checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f" dependencies = [ "const-random", ] @@ -2432,7 +2459,7 @@ dependencies = [ "indexmap 1.9.3", "slab", "tokio", - "tokio-util 0.7.9", + "tokio-util 0.7.10", "tracing", ] @@ -3036,6 +3063,15 @@ version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +[[package]] +name = "memmap2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327" +dependencies = [ + "libc", +] + [[package]] name = "memoffset" version = "0.9.0" @@ -3054,6 +3090,29 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "miette" +version = "5.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59bb584eaeeab6bd0226ccf3509a69d7936d148cf3d036ad350abe35e8c6856e" +dependencies = [ + "miette-derive", + "once_cell", + "thiserror", + "unicode-width", +] + +[[package]] +name = "miette-derive" +version = "5.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49e7bc1560b95a3c4a25d03de42fe76ca718ab92d1a22a55b9b4cf67b3ae635c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", +] + [[package]] name = "mime" version = "0.3.17" @@ -3096,9 +3155,9 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.8" +version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" +checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0" dependencies = [ "libc", "wasi 0.11.0+wasi-snapshot-preview1", @@ -3407,7 +3466,7 @@ dependencies = [ "mio 0.6.23", "mio-extras", "rand 0.7.3", - "sha-1", + "sha-1 0.8.2", "slab", "url", ] @@ -3663,12 +3722,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "proc-macro-hack" -version = "0.5.20+deprecated" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" - [[package]] name = "proc-macro2" version = "1.0.69" @@ -3903,6 +3956,17 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "reflink-copy" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f97f7665e51f23760e9e4949d454a4782c76ef954acaeec9d1b0f48a58e4529e" +dependencies = [ + "cfg-if 1.0.0", + "rustix", + "windows", +] + [[package]] name = "regex" version = "1.10.2" @@ -4335,6 +4399,28 @@ dependencies = [ "opaque-debug 0.2.3", ] +[[package]] +name = "sha-1" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5058ada175748e33390e40e872bd0fe59a19f265d0158daa551c5a88a76009c" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.10.7", +] + +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.10.7", +] + [[package]] name = "sha2" version = "0.10.8" @@ -4452,6 +4538,23 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" +[[package]] +name = "ssri" +version = "9.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7a2b3c2bc9693bcb40870c4e9b5bf0d79f9cb46273321bf855ec513e919082" +dependencies = [ + "base64", + "digest 0.10.7", + "hex", + "miette", + "serde", + "sha-1 0.10.1", + "sha2", + "thiserror", + "xxhash-rust", +] + [[package]] name = "static_assertions" version = "1.1.0" @@ -4528,7 +4631,7 @@ dependencies = [ "tentacle-secio", "thiserror", "tokio", - "tokio-util 0.7.9", + "tokio-util 0.7.10", "tokio-yamux", "wasm-bindgen", "wasm-bindgen-futures", @@ -4570,7 +4673,7 @@ dependencies = [ "secp256k1", "sha2", "tokio", - "tokio-util 0.7.9", + "tokio-util 0.7.10", "unsigned-varint", "x25519-dalek", ] @@ -4727,7 +4830,7 @@ dependencies = [ "backtrace", "bytes 1.5.0", "libc", - "mio 0.8.8", + "mio 0.8.9", "num_cpus", "parking_lot 0.12.1", "pin-project-lite", @@ -4785,9 +4888,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.9" +version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d68074620f57a0b21594d9735eb2e98ab38b17f80d3fcb189fca266771ca60d" +checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" dependencies = [ "bytes 1.5.0", "futures-core", @@ -4808,7 +4911,7 @@ dependencies = [ "log", "nohash-hasher", "tokio", - "tokio-util 0.7.9", + "tokio-util 0.7.10", ] [[package]] @@ -5213,6 +5316,25 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.51.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" +dependencies = [ + "windows-core", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-core" +version = "0.51.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.45.0" @@ -5400,6 +5522,12 @@ dependencies = [ "xml-rs", ] +[[package]] +name = "xxhash-rust" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9828b178da53440fa9c766a3d2f73f7cf5d0ac1fe3980c1e5018d899fd19e07b" + [[package]] name = "yansi" version = "0.5.1" diff --git a/Makefile b/Makefile index dd834bb408..13f5f15557 100644 --- a/Makefile +++ b/Makefile @@ -67,10 +67,6 @@ cov: cov-install-tools ## Run code coverage. RUSTFLAGS="-Zinstrument-coverage" LLVM_PROFILE_FILE="${COV_PROFRAW_DIR}/ckb-cov-%p-%m.profraw" cargo +nightly-2022-03-22 test --all GRCOV_OUTPUT=lcov-unit-test.info make cov-collect-data -.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 setup-ckb-test: diff --git a/deny.toml b/deny.toml index bde89806b7..5bd9e904b0 100644 --- a/deny.toml +++ b/deny.toml @@ -19,6 +19,7 @@ allow = [ # https://softwareengineering.stackexchange.com/questions/317944/can-i-include-code-licensed-under-mpl-within-a-project-under-mit-license "MPL-2.0", "Unicode-DFS-2016", + "BSL-1.0", ] copyleft = "deny" default = "deny" diff --git a/devtools/ci/ci_main.sh b/devtools/ci/ci_main.sh index 25623ed7f9..75d5db62e8 100755 --- a/devtools/ci/ci_main.sh +++ b/devtools/ci/ci_main.sh @@ -61,11 +61,6 @@ case $GITHUB_WORKFLOW in make check-dirty-hashes-toml devtools/ci/check-cyclic-dependencies.py ;; - ci_wasm_build*) - echo "ci_WASM_build" - rustup target add wasm32-unknown-unknown - make wasm-build-test - ;; ci_aarch64_build*) echo "ci_aarch64_build" sudo apt-get install -y gcc-multilib diff --git a/rpc/src/tests/examples.rs b/rpc/src/tests/examples.rs index 514529d4fd..ef03381e54 100644 --- a/rpc/src/tests/examples.rs +++ b/rpc/src/tests/examples.rs @@ -20,6 +20,7 @@ use ckb_types::{ capacity_bytes, BlockBuilder, Capacity, EpochNumberWithFraction, Ratio, TransactionBuilder, TransactionView, }, + global::DATA_DIR, h256, packed::{AlertBuilder, CellDep, CellInput, CellOutputBuilder, OutPoint, RawAlertBuilder}, prelude::*, @@ -130,11 +131,13 @@ fn setup_rpc_test_suite(height: u64) -> RpcTestSuite { // Start network services let temp_dir = tempfile::tempdir().expect("create tempdir failed"); + DATA_DIR + .set(temp_dir.path().join("data")) + .expect("DATA_DIR set only once"); - let temp_path = temp_dir.path().to_path_buf(); let network_controller = { let network_config = NetworkConfig { - path: temp_path, + path: temp_dir.path().join("network").to_path_buf(), ping_interval_secs: 1, ping_timeout_secs: 1, connect_outbound_interval_secs: 1, diff --git a/rpc/src/tests/mod.rs b/rpc/src/tests/mod.rs index 56ae906413..78ead1f75b 100644 --- a/rpc/src/tests/mod.rs +++ b/rpc/src/tests/mod.rs @@ -15,6 +15,7 @@ use ckb_types::{ cell::resolve_transaction, BlockBuilder, BlockView, HeaderView, TransactionBuilder, TransactionView, }, + global::DATA_DIR, h256, packed::{CellInput, OutPoint}, prelude::*, @@ -225,13 +226,15 @@ fn setup(consensus: Consensus) -> RpcTestSuite { let chain_controller = ChainService::new(shared.clone(), pack.take_proposal_table()).start::<&str>(None); - // Start network services - let tmp_dir = tempfile::tempdir().expect("create tempdir failed"); + let tmp_dir = tempfile::tempdir().expect("create tmp_dir failed"); + DATA_DIR + .set(tmp_dir.path().join("data")) + .expect("DATA_DIR set only once"); - let tmp_path = tmp_dir.path().to_path_buf(); + // Start network services let network_controller = { let network_config = NetworkConfig { - path: tmp_path, + path: tmp_dir.path().join("network").to_path_buf(), ping_interval_secs: 1, ping_timeout_secs: 1, connect_outbound_interval_secs: 1, diff --git a/spec/Cargo.toml b/spec/Cargo.toml index 574a57f152..7ab8cfd478 100644 --- a/spec/Cargo.toml +++ b/spec/Cargo.toml @@ -11,6 +11,7 @@ repository = "https://github.com/nervosnetwork/ckb" [dependencies] serde = { version = "1.0", features = ["derive"] } toml = "0.5" +cacache = { version = "12.0.0", default-features = false, features = ["tokio-runtime", "mmap"] } ckb-constant = { path = "../util/constant", version = "= 0.113.0-pre" } ckb-types = { path = "../util/types", version = "= 0.113.0-pre" } ckb-pow = { path = "../pow", version = "= 0.113.0-pre" } @@ -22,4 +23,8 @@ ckb-crypto = { path = "../util/crypto", version = "= 0.113.0-pre"} ckb-hash = { path = "../util/hash", version = "= 0.113.0-pre"} ckb-error = { path = "../error", version = "= 0.113.0-pre" } ckb-traits = { path = "../traits", version = "= 0.113.0-pre" } -ckb-util = { path = "../util", version = "= 0.113.0-pre" } +ckb-logger = {path = "../util/logger", version = "= 0.113.0-pre"} + + +[dev-dependencies] +tempfile.workspace = true diff --git a/spec/src/tests/versionbits.rs b/spec/src/tests/versionbits.rs index 9a092a68ba..bcbb3aea57 100644 --- a/spec/src/tests/versionbits.rs +++ b/spec/src/tests/versionbits.rs @@ -12,6 +12,7 @@ use ckb_types::{ capacity_bytes, BlockBuilder, BlockView, Capacity, EpochExt, EpochNumber, HeaderView, TransactionBuilder, TransactionView, Version, }, + global::DATA_DIR, packed::{Byte32, Bytes, CellbaseWitness}, prelude::*, utilities::DIFF_TWO, @@ -191,6 +192,11 @@ impl MockChain { #[test] fn test_versionbits_active() { + let tmp_dir = tempfile::tempdir().expect("create tmp_dir failed"); + DATA_DIR + .set(tmp_dir.path().join("data")) + .expect("DATA_DIR set only once"); + let cellbase = TransactionBuilder::default() .witness(Bytes::default()) .build(); @@ -294,6 +300,11 @@ fn test_versionbits_active() { #[test] fn test_versionbits_failed() { + let tmp_dir = tempfile::tempdir().expect("create tmp_dir failed"); + DATA_DIR + .set(tmp_dir.path().join("data")) + .expect("DATA_DIR set only once"); + let cellbase = TransactionBuilder::default() .witness(Bytes::default()) .build(); diff --git a/spec/src/versionbits/mod.rs b/spec/src/versionbits/mod.rs index 7acc709a33..5c62a9834a 100644 --- a/spec/src/versionbits/mod.rs +++ b/spec/src/versionbits/mod.rs @@ -1,17 +1,19 @@ -//! Versionbits 9 defines a finite-state-machine to deploy a softfork in multiple stages. +//! Versionbits defines a finite-state-machine to deploy a softfork in multiple stages. //! mod convert; use crate::consensus::Consensus; +use ckb_logger::error; +use ckb_types::global::DATA_DIR; use ckb_types::{ core::{EpochExt, EpochNumber, HeaderView, Ratio, TransactionView, Version}, packed::{Byte32, CellbaseWitnessReader}, prelude::*, }; -use ckb_util::Mutex; -use std::collections::{hash_map, HashMap}; +use std::fmt; use std::sync::Arc; +use std::{collections::HashMap, path::PathBuf}; /// What bits to set in version for versionbits blocks pub const VERSIONBITS_TOP_BITS: Version = 0x00000000; @@ -20,24 +22,40 @@ pub const VERSIONBITS_TOP_MASK: Version = 0xE0000000; /// Total bits available for versionbits pub const VERSIONBITS_NUM_BITS: u32 = 29; +const PATH_PREFIX: &str = "softfork"; + /// RFC0043 defines a finite-state-machine to deploy a soft fork in multiple stages. /// State transitions happen during epoch if conditions are met /// In case of reorg, transitions can go backward. Without transition, state is /// inherited between epochs. All blocks of a epoch share the same state. #[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[repr(u8)] pub enum ThresholdState { /// First state that each softfork starts. /// The 0 epoch is by definition in this state for each deployment. - Defined, + Defined = 0, /// For epochs past the `start` epoch. - Started, + Started = 1, /// For one epoch after the first epoch period with STARTED epochs of /// which at least `threshold` has the associated bit set in `version`. - LockedIn, + LockedIn = 2, /// For all epochs after the LOCKED_IN epoch. - Active, + Active = 3, /// For one epoch period past the `timeout_epoch`, if LOCKED_IN was not reached. - Failed, + Failed = 4, +} + +impl ThresholdState { + fn from_u8(value: u8) -> ThresholdState { + match value { + 0 => ThresholdState::Defined, + 1 => ThresholdState::Started, + 2 => ThresholdState::LockedIn, + 3 => ThresholdState::Active, + 4 => ThresholdState::Failed, + _ => panic!("Unknown value: {}", value), + } + } } /// This is useful for testing, as it means tests don't need to deal with the activation @@ -63,6 +81,12 @@ pub enum DeploymentPos { LightClient, } +impl fmt::Display for DeploymentPos { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} + /// VersionbitsIndexer pub trait VersionbitsIndexer { /// Gets epoch index by block hash @@ -114,7 +138,49 @@ pub struct Deployment { pub threshold: Ratio, } -type Cache = Mutex>; +/// Signal state cache +/// +/// Persistent signal state cache, mmap-based cacache +#[derive(Clone, Debug)] +pub struct Cache { + path: PathBuf, +} + +impl Cache { + /// Reads the entire contents of a cache file synchronously into a bytes vector, + /// looking the data up by key. + pub fn get(&self, key: &Byte32) -> Option { + match cacache::read_sync(&self.path, Self::encode_key(key)) { + Ok(bytes) => Some(Self::decode_value(bytes)), + Err(cacache::Error::EntryNotFound(_path, _key)) => None, + Err(err) => { + error!("cacache read_sync failed {:?}", err); + None + } + } + } + + /// Writes data to the cache synchronously + pub fn insert(&self, key: &Byte32, value: ThresholdState) { + if let Err(e) = + cacache::write_sync(&self.path, Self::encode_key(key), Self::encode_value(value)) + { + error!("cacache write_sync failed {:?}", e); + } + } + + fn decode_value(value: Vec) -> ThresholdState { + ThresholdState::from_u8(value[0]) + } + + fn encode_key(key: &Byte32) -> String { + format!("{}", key) + } + + fn encode_value(value: ThresholdState) -> Vec { + vec![value as u8] + } +} /// RFC0000 allows multiple soft forks to be deployed in parallel. We cache /// per-epoch state for every one of them. */ @@ -126,8 +192,17 @@ pub struct VersionbitsCache { impl VersionbitsCache { /// Construct new VersionbitsCache instance from deployments pub fn new<'a>(deployments: impl Iterator) -> Self { + let default_dir = PathBuf::new(); + let data_dir = DATA_DIR.get().unwrap_or(&default_dir); let caches: HashMap<_, _> = deployments - .map(|pos| (*pos, Mutex::new(HashMap::new()))) + .map(|pos| { + ( + *pos, + Cache { + path: data_dir.join(PATH_PREFIX).join(pos.to_string()), + }, + ) + }) .collect(); VersionbitsCache { caches: Arc::new(caches), @@ -194,25 +269,20 @@ pub trait VersionbitsConditionChecker { let target = epoch_number.saturating_sub((epoch_number + 1) % period); let mut epoch_ext = indexer.ancestor_epoch(&start_index, target)?; - let mut g_cache = cache.lock(); let mut to_compute = Vec::new(); let mut state = loop { let epoch_index = epoch_ext.last_block_hash_in_previous_epoch(); - match g_cache.entry(epoch_index.clone()) { - hash_map::Entry::Occupied(entry) => { - break *entry.get(); - } - hash_map::Entry::Vacant(entry) => { - // The genesis is by definition defined. - if epoch_ext.is_genesis() || epoch_ext.number() < start { - entry.insert(ThresholdState::Defined); - break ThresholdState::Defined; - } - let next_epoch_ext = indexer - .ancestor_epoch(&epoch_index, epoch_ext.number().saturating_sub(period))?; - to_compute.push(epoch_ext); - epoch_ext = next_epoch_ext; + if let Some(value) = cache.get(&epoch_index) { + break value; + } else { + if epoch_ext.is_genesis() || epoch_ext.number() < start { + cache.insert(&epoch_index, ThresholdState::Defined); + break ThresholdState::Defined; } + let next_epoch_ext = indexer + .ancestor_epoch(&epoch_index, epoch_ext.number().saturating_sub(period))?; + to_compute.push(epoch_ext); + epoch_ext = next_epoch_ext; } }; @@ -268,7 +338,7 @@ pub trait VersionbitsConditionChecker { } } state = next_state; - g_cache.insert(epoch_ext.last_block_hash_in_previous_epoch(), state); + cache.insert(&epoch_ext.last_block_hash_in_previous_epoch(), state); } Some(state) @@ -301,16 +371,14 @@ pub trait VersionbitsConditionChecker { let mut epoch_ext = indexer.ancestor_epoch(&index, period_start)?; let mut epoch_index = epoch_ext.last_block_hash_in_previous_epoch(); - let g_cache = cache.lock(); while let Some(prev_epoch_ext) = indexer.ancestor_epoch(&epoch_index, epoch_ext.number().saturating_sub(period)) { epoch_ext = prev_epoch_ext; epoch_index = epoch_ext.last_block_hash_in_previous_epoch(); - - if let Some(state) = g_cache.get(&epoch_index) { - if state != &init_state { + if let Some(state) = cache.get(&epoch_index) { + if state != init_state { break; } } else { diff --git a/util/app-config/src/app_config.rs b/util/app-config/src/app_config.rs index 627df65f38..476ed1a4e4 100644 --- a/util/app-config/src/app_config.rs +++ b/util/app-config/src/app_config.rs @@ -4,6 +4,7 @@ //! we must put nested config struct in the tail to make it serializable, //! details +use ckb_types::global::DATA_DIR; use path_clean::PathClean; use std::fs; use std::path::{Path, PathBuf}; @@ -274,6 +275,10 @@ impl CKBAppConfig { self.data_dir = canonicalize_data_dir(self.data_dir, root_dir); + DATA_DIR + .set(self.data_dir.clone()) + .expect("DATA_DIR is empty"); + self.db.adjust(root_dir, &self.data_dir, "db"); self.ancient = mkdir(path_specified_or_else(&self.ancient, || { self.data_dir.join("ancient") diff --git a/util/types/src/core/mod.rs b/util/types/src/core/mod.rs index 1e7342d57c..ae8602a577 100644 --- a/util/types/src/core/mod.rs +++ b/util/types/src/core/mod.rs @@ -27,6 +27,7 @@ mod fee_rate; mod reward; mod transaction_meta; mod views; + pub use advanced_builders::{BlockBuilder, HeaderBuilder, TransactionBuilder}; pub use blockchain::DepType; pub use extras::{BlockExt, EpochExt, EpochNumberWithFraction, TransactionInfo}; diff --git a/util/types/src/global.rs b/util/types/src/global.rs new file mode 100644 index 0000000000..9c02d6aac9 --- /dev/null +++ b/util/types/src/global.rs @@ -0,0 +1,7 @@ +//! Global data, initialized in the launch phase. + +use once_cell::sync::OnceCell; +use std::path::PathBuf; + +/// ckb data directory path, located under root/data, initialized during the launch phase +pub static DATA_DIR: OnceCell = OnceCell::new(); diff --git a/util/types/src/lib.rs b/util/types/src/lib.rs index 3e823f91e3..6b559dd614 100644 --- a/util/types/src/lib.rs +++ b/util/types/src/lib.rs @@ -11,6 +11,7 @@ pub use molecule::{self, error}; pub use numext_fixed_uint::{u256, U128, U256}; pub mod core; +pub mod global; pub mod constants; mod conversion; diff --git a/wasm-build-test/Cargo.toml b/wasm-build-test/Cargo.toml deleted file mode 100644 index c39aaaa78b..0000000000 --- a/wasm-build-test/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "ckb-wasm-test" -version = "0.113.0-pre" -license = "MIT" -authors = ["Nervos Core Dev "] -edition = "2021" -description = "Testsuite to ensure some crates work with diffent features." -homepage = "https://github.com/nervosnetwork/ckb" -repository = "https://github.com/nervosnetwork/ckb" - -[dependencies] -ckb-types = { path = "../util/types", version = "= 0.113.0-pre" } -ckb-script = { path = "../script", version = "= 0.113.0-pre", default-features = false, features = [] } - -# Prevent this from interfering with workspaces -[workspace] -members = ["."] diff --git a/wasm-build-test/src/lib.rs b/wasm-build-test/src/lib.rs deleted file mode 100644 index b30dde965c..0000000000 --- a/wasm-build-test/src/lib.rs +++ /dev/null @@ -1,4 +0,0 @@ -// We just need a dummy package to test building dependencies, those -// lines here prevent errors from devtools/ci/check-cargotoml.sh -extern crate ckb_script; -extern crate ckb_types;