diff --git a/Cargo.lock b/Cargo.lock index 2922adec4..e8d8ec223 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "Inflector" @@ -978,6 +978,12 @@ dependencies = [ "semver 0.6.0", ] +[[package]] +name = "build-print" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a2128d00b7061b82b72844a351e80acd29e05afc60e9261e2ac90dca9ecc2ac" + [[package]] name = "bumpalo" version = "3.16.0" @@ -2962,7 +2968,7 @@ dependencies = [ "derive-syn-parse", "docify", "expander", - "frame-support-procedural-tools 13.0.0", + "frame-support-procedural-tools", "itertools 0.11.0", "macro_magic", "proc-macro-warning 1.0.2", @@ -2972,42 +2978,18 @@ dependencies = [ "syn 2.0.87", ] -[[package]] -name = "frame-support-procedural-tools" -version = "10.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3363df38464c47a73eb521a4f648bfcc7537a82d70347ef8af3f73b6d019e910" -dependencies = [ - "frame-support-procedural-tools-derive 11.0.0", - "proc-macro-crate 3.2.0", - "proc-macro2", - "quote", - "syn 2.0.87", -] - [[package]] name = "frame-support-procedural-tools" version = "13.0.0" source = "git+https://github.com/paritytech/polkadot-sdk.git?tag=polkadot-stable2409#87971b3e92721bdf10bf40b410eaae779d494ca0" dependencies = [ - "frame-support-procedural-tools-derive 12.0.0", + "frame-support-procedural-tools-derive", "proc-macro-crate 3.2.0", "proc-macro2", "quote", "syn 2.0.87", ] -[[package]] -name = "frame-support-procedural-tools-derive" -version = "11.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68672b9ec6fe72d259d3879dc212c5e42e977588cdac830c76f54d9f492aeb58" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.87", -] - [[package]] name = "frame-support-procedural-tools-derive" version = "12.0.0" @@ -5577,7 +5559,6 @@ dependencies = [ "pallet-transaction-payment", "pallet-transaction-payment-rpc", "pallet-transaction-payment-rpc-runtime-api", - "parity-scale-codec", "sc-basic-authorship", "sc-chain-spec", "sc-chain-spec-derive", @@ -5586,7 +5567,6 @@ dependencies = [ "sc-consensus", "sc-consensus-aura", "sc-consensus-grandpa", - "sc-consensus-grandpa-rpc", "sc-consensus-manual-seal", "sc-consensus-slots", "sc-executor", @@ -5624,6 +5604,7 @@ dependencies = [ "substrate-prometheus-endpoint", "subtensor-custom-rpc", "subtensor-custom-rpc-runtime-api", + "subtensor-linting", "thiserror", ] @@ -5664,7 +5645,6 @@ dependencies = [ "pallet-evm-precompile-sha3fips", "pallet-evm-precompile-simple", "pallet-grandpa", - "pallet-hotfix-sufficients", "pallet-insecure-randomness-collective-flip", "pallet-membership", "pallet-multisig", @@ -5702,6 +5682,7 @@ dependencies = [ "sp-version", "substrate-wasm-builder", "subtensor-custom-rpc-runtime-api", + "subtensor-linting", "subtensor-macros", "tle", "w3f-bls", @@ -6029,6 +6010,7 @@ dependencies = [ "sp-tracing 17.0.1", "sp-weights", "substrate-fixed", + "subtensor-linting", "subtensor-macros", ] @@ -6104,6 +6086,7 @@ dependencies = [ "sp-io", "sp-runtime", "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk.git?tag=polkadot-stable2409)", + "subtensor-linting", "subtensor-macros", ] @@ -6122,6 +6105,7 @@ dependencies = [ "sp-io", "sp-runtime", "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk.git?tag=polkadot-stable2409)", + "subtensor-linting", "subtensor-macros", ] @@ -6279,21 +6263,6 @@ dependencies = [ "sp-staking", ] -[[package]] -name = "pallet-hotfix-sufficients" -version = "1.0.0" -source = "git+https://github.com/gztensor/frontier?rev=b8e3025#b8e3025aa30ea65144372bd68d26090c0f31bea2" -dependencies = [ - "frame-benchmarking", - "frame-support", - "frame-system", - "pallet-evm", - "parity-scale-codec", - "scale-info", - "sp-core", - "sp-runtime", -] - [[package]] name = "pallet-insecure-randomness-collective-flip" version = "26.0.0" @@ -6382,6 +6351,7 @@ dependencies = [ "sp-io", "sp-runtime", "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk.git?tag=polkadot-stable2409)", + "subtensor-linting", "subtensor-macros", ] @@ -6481,6 +6451,7 @@ dependencies = [ "sp-tracing 17.0.1", "sp-version", "substrate-fixed", + "subtensor-linting", "subtensor-macros", "tle", "w3f-bls", @@ -7195,7 +7166,7 @@ dependencies = [ "cfg-expr", "derive-syn-parse", "expander", - "frame-support-procedural-tools 10.0.0", + "frame-support-procedural-tools", "itertools 0.10.5", "macro_magic", "proc-macro-warning 1.0.2", @@ -8369,26 +8340,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "sc-consensus-grandpa-rpc" -version = "0.30.0" -source = "git+https://github.com/paritytech/polkadot-sdk.git?tag=polkadot-stable2409#87971b3e92721bdf10bf40b410eaae779d494ca0" -dependencies = [ - "finality-grandpa", - "futures", - "jsonrpsee", - "log", - "parity-scale-codec", - "sc-client-api", - "sc-consensus-grandpa", - "sc-rpc", - "serde", - "sp-blockchain", - "sp-core", - "sp-runtime", - "thiserror", -] - [[package]] name = "sc-consensus-manual-seal" version = "0.46.0" @@ -10895,15 +10846,16 @@ dependencies = [ ] [[package]] -name = "subtensor" +name = "subtensor-code-coverage" version = "0.1.0" dependencies = [ - "node-subtensor", - "node-subtensor-runtime", + "build-print", + "frame-support", + "frame-system", "proc-macro2", + "procedural-fork", "quote", "rayon", - "subtensor-linting", "syn 2.0.87", "walkdir", ] @@ -10937,10 +10889,13 @@ dependencies = [ name = "subtensor-linting" version = "0.1.0" dependencies = [ + "build-print", "proc-macro2", "procedural-fork", "quote", + "rayon", "syn 2.0.87", + "walkdir", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index cdcd6643d..aae05d81b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,44 +1,30 @@ -[package] -name = "subtensor" -version = "0.1.0" -description = "Implementation of the bittensor blockchain" +[workspace] +default-members = ["node"] +members = [ + "node", + "pallets/admin-utils", + "pallets/collective", + "pallets/commitments", + "pallets/registry", + "pallets/subtensor", + "pallets/subtensor/rpc", + "pallets/subtensor/runtime-api", + "runtime", + "support/code-coverage", + "support/linting", + "support/macros", + "support/procedural-fork", + "support/tools", +] +resolver = "2" + +[workspace.package] authors = ["Substrate DevHub "] homepage = "https://substrate.io/" edition = "2021" -license = "Unlicense" publish = false repository = "https://github.com/opentensor/subtensor" -[dependencies] -node-subtensor = { path = "node", version = "4.0.0-dev" } -node-subtensor-runtime = { path = "runtime", version = "4.0.0-dev" } - -[build-dependencies] -subtensor-linting = { path = "support/linting", version = "0.1.0" } -syn.workspace = true -quote.workspace = true -proc-macro2.workspace = true -walkdir.workspace = true -rayon = "1.10" - -[workspace] -members = [ - "node", - "pallets/commitments", - "pallets/subtensor", - "pallets/admin-utils", - "pallets/collective", - "pallets/registry", - "runtime", - "support/tools", - "support/macros", - "support/linting", - "support/procedural-fork", - "pallets/drand", -] -exclude = ["support/procedural-fork"] -resolver = "2" - [workspace.lints.clippy] indexing-slicing = "deny" arithmetic-side-effects = "deny" @@ -47,11 +33,31 @@ unwrap-used = "deny" manual_inspect = "allow" [workspace.dependencies] +# local crates +node-subtensor = { path = "node" } +node-subtensor-runtime = { path = "runtime" } +pallet-admin-utils = { path = "pallets/admin-utils", default-features = false } +pallet-collective = { path = "pallets/collective", default-features = false } +pallet-commitments = { path = "pallets/commitments", default-features = false } +pallet-registry = { path = "pallets/registry", default-features = false } +pallet-subtensor = { path = "pallets/subtensor", default-features = false } +procedural-fork = { path = "support/procedural-fork" } +subtensor-code-coverage = { path = "support/code-coverage" } +subtensor-custom-rpc = { path = "pallets/subtensor/rpc", default-features = false } +subtensor-custom-rpc-runtime-api = { path = "pallets/subtensor/runtime-api", default-features = false } +subtensor-linting = { path = "support/linting" } +subtensor-macros = { path = "support/macros" } +subtensor-tools = { path = "support/tools" } + +# non-local async-trait = "0.1" +rayon = "1" +build-print = "0.1" cargo-husky = { version = "1", default-features = false } clap = "4.5.4" -codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive"] } -ed25519-dalek = { version = "2.1.0", default-features = false, features = ["alloc"] } +ed25519-dalek = { version = "2.1.0", default-features = false, features = [ + "alloc", +] } enumflags2 = "0.7.9" futures = "0.3.30" hex = { version = "0.4", default-features = false } @@ -62,7 +68,9 @@ memmap2 = "0.9.4" ndarray = { version = "0.15.6", default-features = false } parity-util-mem = "0.12.0" rand = "0.8.5" -scale-codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = [ + "derive", +] } scale-info = { version = "2.11.2", default-features = false } serde = { version = "1.0.214", default-features = false } serde-tuple-vec-map = { version = "1.0.1", default-features = false } @@ -71,25 +79,24 @@ serde_json = { version = "1.0.121", default-features = false } serde_with = { version = "=2.0.0", default-features = false } smallvec = "1.13.2" litep2p = { git = "https://github.com/paritytech/litep2p", tag = "v0.7.0" } -syn = { version = "2.0.87", features = [ - "full", - "visit-mut", - "visit", - "extra-traits", - "parsing", +syn = { version = "2", features = [ + "full", + "visit-mut", + "visit", + "extra-traits", + "parsing", ] } quote = "1" proc-macro2 = { version = "1", features = ["span-locations"] } thiserror = "1.0" walkdir = "2" -subtensor-macros = { path = "support/macros" } - frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false } frame-benchmarking-cli = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409" } frame-executive = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false } frame-metadata-hash-extension = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false } frame-support = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false } +frame-support-procedural-tools = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false } frame-system = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false } frame-system-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false } frame-system-rpc-runtime-api = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false } @@ -195,21 +202,34 @@ pallet-hotfix-sufficients = { git = "https://github.com/gztensor/frontier", rev #DRAND pallet-drand = { path = "pallets/drand", default-features = false } -sp-crypto-ec-utils = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", features = ["bls12-381"] } -getrandom = { version = "0.2.15", features = ["custom"], default-features = false } -sp-keystore = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false } +sp-crypto-ec-utils = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", features = [ + "bls12-381", +] } +getrandom = { version = "0.2.15", features = [ + "custom", +], default-features = false } +sp-keystore = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false } w3f-bls = { version = "=0.1.3", default-features = false } -ark-crypto-primitives = { version = "0.4.0", default-features = false, features = [ "r1cs", "snark" ] } -ark-scale = { version = "0.0.11", default-features = false, features = ["hazmat"] } +ark-crypto-primitives = { version = "0.4.0", default-features = false, features = [ + "r1cs", + "snark", +] } +ark-scale = { version = "0.0.11", default-features = false, features = [ + "hazmat", +] } sp-ark-bls12-381 = { git = "https://github.com/paritytech/substrate-curves", default-features = false } -ark-bls12-381 = { version = "0.4.0", features = ["curve"], default-features = false } -ark-serialize = { version = "0.4.0", features = [ "derive" ], default-features = false } +ark-bls12-381 = { version = "0.4.0", features = [ + "curve", +], default-features = false } +ark-serialize = { version = "0.4.0", features = [ + "derive", +], default-features = false } ark-ff = { version = "0.4.0", default-features = false } ark-ec = { version = "0.4.0", default-features = false } ark-std = { version = "0.4.0", default-features = false } anyhow = "1.0.81" sha2 = { version = "0.10.8", default-features = false } -rand_chacha = { version = "0.3.1", default-features = false } +rand_chacha = { version = "0.3.1", default-features = false } tle = { git = "https://github.com/ideal-lab5/timelock", rev = "5416406cfd32799e31e1795393d4916894de4468", default-features = false } frame-metadata = "16" @@ -224,15 +244,3 @@ opt-level = 3 inherits = "release" lto = true codegen-units = 1 - -[features] -default = [] -try-runtime = [ - "node-subtensor/try-runtime", - "node-subtensor-runtime/try-runtime", -] -runtime-benchmarks = [ - "node-subtensor/runtime-benchmarks", - "node-subtensor-runtime/runtime-benchmarks", -] -metadata-hash = ["node-subtensor-runtime/metadata-hash"] diff --git a/build.rs b/build.rs deleted file mode 100644 index 1abd7456b..000000000 --- a/build.rs +++ /dev/null @@ -1,101 +0,0 @@ -use rayon::prelude::*; -use std::{ - env, fs, - path::{Path, PathBuf}, - str::FromStr, - sync::mpsc::channel, -}; -use walkdir::WalkDir; - -use subtensor_linting::*; - -fn main() { - // need to list all rust directories here - println!("cargo:rerun-if-changed=pallets"); - println!("cargo:rerun-if-changed=node"); - println!("cargo:rerun-if-changed=runtime"); - println!("cargo:rerun-if-changed=lints"); - println!("cargo:rerun-if-changed=build.rs"); - println!("cargo:rerun-if-changed=src"); - println!("cargo:rerun-if-changed=support"); - // Get the root directory of the workspace - let workspace_root = env::var("CARGO_MANIFEST_DIR").unwrap(); - let workspace_root = Path::new(&workspace_root); - - // Collect all Rust source files in the workspace - let rust_files = collect_rust_files(workspace_root); - - // Channel used to communicate errors back to the main thread from the parallel processing - // as we process each Rust file - let (tx, rx) = channel(); - - // Parse each rust file with syn and run the linting suite on it in parallel - rust_files.par_iter().for_each_with(tx.clone(), |tx, file| { - let Ok(content) = fs::read_to_string(file) else { - return; - }; - let Ok(parsed_tokens) = proc_macro2::TokenStream::from_str(&content) else { - return; - }; - let Ok(parsed_file) = syn::parse2::(parsed_tokens) else { - return; - }; - - let track_lint = |result: Result| { - let Err(errors) = result else { - return; - }; - let relative_path = file.strip_prefix(workspace_root).unwrap_or(file.as_path()); - for error in errors { - let loc = error.span().start(); - let file_path = relative_path.display(); - // note that spans can't go across thread boundaries without losing their location - // info so we we serialize here and send a String - tx.send(format!( - "cargo:warning={}:{}:{}: {}", - file_path, loc.line, loc.column, error, - )) - .unwrap(); - } - }; - - track_lint(ForbidAsPrimitiveConversion::lint(&parsed_file)); - track_lint(ForbidKeysRemoveCall::lint(&parsed_file)); - track_lint(RequireFreezeStruct::lint(&parsed_file)); - track_lint(RequireExplicitPalletIndex::lint(&parsed_file)); - }); - - // Collect and print all errors after the parallel processing is done - drop(tx); // Close the sending end of the channel - - for error in rx { - println!("{error}"); - } -} - -/// Recursively collects all Rust files in the given directory -fn collect_rust_files(dir: &Path) -> Vec { - let mut rust_files = Vec::new(); - - for entry in WalkDir::new(dir) { - let Ok(entry) = entry else { - continue; - }; - let path = entry.path(); - - // Skip any path that contains "target" directory - if path - .components() - .any(|component| component.as_os_str() == "target") - || path.ends_with("build.rs") - { - continue; - } - - if path.is_file() && path.extension().and_then(|ext| ext.to_str()) == Some("rs") { - rust_files.push(path.to_path_buf()); - } - } - - rust_files -} diff --git a/node/Cargo.toml b/node/Cargo.toml index 3d14b7496..40693e6ae 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -23,7 +23,6 @@ name = "node-subtensor" async-trait = { workspace = true } clap = { workspace = true, features = ["derive"] } futures = { workspace = true, features = ["thread-pool"] } -scale-codec = { workspace = true } serde = { workspace = true, features = ["derive"] } # Storage import @@ -45,7 +44,6 @@ sp-consensus-aura = { workspace = true } sp-consensus = { workspace = true } sc-consensus = { workspace = true } sc-consensus-grandpa = { workspace = true } -sc-consensus-grandpa-rpc = { workspace = true } sp-consensus-grandpa = { workspace = true } sc-chain-spec-derive = { workspace = true } sc-chain-spec = { workspace = true } @@ -62,7 +60,7 @@ sp-session = { workspace = true } frame-metadata-hash-extension = { workspace = true } frame-system = { workspace = true } pallet-transaction-payment = { workspace = true } -pallet-commitments = { path = "../pallets/commitments" } +pallet-commitments = { workspace = true } pallet-drand = { workspace = true } sp-crypto-ec-utils = { workspace = true } sp-keystore = { workspace = true, default-features = false } @@ -104,12 +102,13 @@ fp-consensus = { workspace = true } thiserror = { workspace = true } # Local Dependencies -node-subtensor-runtime = { path = "../runtime" } -subtensor-custom-rpc = { path = "../pallets/subtensor/rpc" } -subtensor-custom-rpc-runtime-api = { path = "../pallets/subtensor/runtime-api" } +node-subtensor-runtime = { workspace = true } +subtensor-custom-rpc = { workspace = true } +subtensor-custom-rpc-runtime-api = { workspace = true } [build-dependencies] substrate-build-script-utils = { workspace = true } +subtensor-linting = {workspace = true} [features] default = ["rocksdb", "sql", "txpool"] diff --git a/node/build.rs b/node/build.rs index f9d839f9b..aba66d859 100644 --- a/node/build.rs +++ b/node/build.rs @@ -2,6 +2,8 @@ use substrate_build_script_utils::{generate_cargo_keys, rerun_if_git_head_change fn main() { generate_cargo_keys(); - rerun_if_git_head_changed(); + + println!("cargo:rerun-if-changed=src"); + subtensor_linting::walk_src(); } diff --git a/pallets/admin-utils/Cargo.toml b/pallets/admin-utils/Cargo.toml index f7dfedbbe..3597cd7db 100644 --- a/pallets/admin-utils/Cargo.toml +++ b/pallets/admin-utils/Cargo.toml @@ -17,16 +17,14 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] subtensor-macros.workspace = true -codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ - "derive", -] } +codec = { workspace = true } scale-info = { workspace = true, features = ["derive"] } frame-benchmarking = { workspace = true, optional = true } frame-support = { workspace = true } frame-system = { workspace = true } sp-runtime = { workspace = true } log = { workspace = true } -pallet-subtensor = { version = "4.0.0-dev", default-features = false, path = "../subtensor" } +pallet-subtensor = { workspace = true } sp-weights = { workspace = true } substrate-fixed = { workspace = true } pallet-drand = { workspace = true, default-features = false } @@ -41,6 +39,9 @@ pallet-balances = { workspace = true, features = ["std"] } pallet-scheduler = { workspace = true } sp-std = { workspace = true } +[build-dependencies] +subtensor-linting = { workspace = true } + [features] default = ["std"] std = [ diff --git a/pallets/admin-utils/build.rs b/pallets/admin-utils/build.rs new file mode 100644 index 000000000..c49f3a720 --- /dev/null +++ b/pallets/admin-utils/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo:rerun-if-changed=src"); + subtensor_linting::walk_src(); +} diff --git a/pallets/collective/Cargo.toml b/pallets/collective/Cargo.toml index cf311f404..0a31f197e 100644 --- a/pallets/collective/Cargo.toml +++ b/pallets/collective/Cargo.toml @@ -17,9 +17,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] subtensor-macros.workspace = true -codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = [ - "derive", -] } +codec = { workspace = true } log = { workspace = true } scale-info = { workspace = true, features = ["derive"] } frame-benchmarking = { workspace = true, optional = true } @@ -30,6 +28,9 @@ sp-io = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } +[build-dependencies] +subtensor-linting = { workspace = true } + [features] default = ["std"] std = [ @@ -53,5 +54,5 @@ runtime-benchmarks = [ try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", - "sp-runtime/try-runtime" + "sp-runtime/try-runtime", ] diff --git a/pallets/collective/build.rs b/pallets/collective/build.rs new file mode 100644 index 000000000..c49f3a720 --- /dev/null +++ b/pallets/collective/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo:rerun-if-changed=src"); + subtensor_linting::walk_src(); +} diff --git a/pallets/commitments/Cargo.toml b/pallets/commitments/Cargo.toml index 1cff429d5..a1e2b87c4 100644 --- a/pallets/commitments/Cargo.toml +++ b/pallets/commitments/Cargo.toml @@ -17,10 +17,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] subtensor-macros.workspace = true -codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ - "derive", - "max-encoded-len", -] } +codec = { workspace = true, features = ["derive", "max-encoded-len"] } scale-info = { workspace = true, features = ["derive"] } frame-benchmarking = { workspace = true, optional = true } frame-support = { workspace = true } @@ -34,6 +31,9 @@ sp-core = { workspace = true } sp-io = { workspace = true } pallet-balances = { workspace = true } +[build-dependencies] +subtensor-linting = { workspace = true } + [features] default = ["std"] std = [ @@ -47,18 +47,18 @@ std = [ "enumflags2/std", "pallet-balances/std", "sp-core/std", - "sp-io/std" + "sp-io/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", "sp-runtime/runtime-benchmarks", - "pallet-balances/runtime-benchmarks" + "pallet-balances/runtime-benchmarks", ] try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", "pallet-balances/try-runtime", - "sp-runtime/try-runtime" + "sp-runtime/try-runtime", ] diff --git a/pallets/commitments/build.rs b/pallets/commitments/build.rs new file mode 100644 index 000000000..c49f3a720 --- /dev/null +++ b/pallets/commitments/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo:rerun-if-changed=src"); + subtensor_linting::walk_src(); +} diff --git a/pallets/registry/Cargo.toml b/pallets/registry/Cargo.toml index e6aca55f6..5884a0f80 100644 --- a/pallets/registry/Cargo.toml +++ b/pallets/registry/Cargo.toml @@ -17,10 +17,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] subtensor-macros.workspace = true -codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ - "derive", - "max-encoded-len", -] } +codec = { workspace = true, features = ["derive", "max-encoded-len"] } scale-info = { workspace = true, features = ["derive"] } frame-benchmarking = { workspace = true, optional = true } frame-support = { workspace = true } @@ -33,6 +30,9 @@ enumflags2 = { workspace = true } sp-core = { workspace = true } sp-io = { workspace = true } +[build-dependencies] +subtensor-linting = { workspace = true } + [features] default = ["std"] std = [ @@ -45,7 +45,7 @@ std = [ "sp-runtime/std", "enumflags2/std", "sp-core/std", - "sp-io/std" + "sp-io/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", @@ -56,5 +56,5 @@ runtime-benchmarks = [ try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", - "sp-runtime/try-runtime" + "sp-runtime/try-runtime", ] diff --git a/pallets/registry/build.rs b/pallets/registry/build.rs new file mode 100644 index 000000000..c49f3a720 --- /dev/null +++ b/pallets/registry/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo:rerun-if-changed=src"); + subtensor_linting::walk_src(); +} diff --git a/pallets/subtensor/Cargo.toml b/pallets/subtensor/Cargo.toml index da4cac3be..d4018de77 100644 --- a/pallets/subtensor/Cargo.toml +++ b/pallets/subtensor/Cargo.toml @@ -17,9 +17,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] subtensor-macros.workspace = true -codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ - "derive", -] } +codec = { workspace = true } sp-core = { workspace = true } pallet-balances = { workspace = true } scale-info = { workspace = true, features = ["derive"] } @@ -41,17 +39,20 @@ pallet-utility = { workspace = true } ndarray = { workspace = true } hex = { workspace = true } -pallet-collective = { version = "4.0.0-dev", default-features = false, path = "../collective" } -pallet-drand = { path = "../drand", default-features = false } +pallet-collective = { workspace = true } +pallet-drand = { workspace = true } pallet-membership = { workspace = true } -hex-literal = { workspace = true } -num-traits = { version = "0.2.19", default-features = false, features = ["libm"] } +num-traits = { version = "0.2.19", default-features = false, features = [ + "libm", +] } tle = { workspace = true, default-features = false } ark-bls12-381 = { workspace = true, default-features = false } ark-serialize = { workspace = true, default-features = false } w3f-bls = { workspace = true, default-features = false } sha2 = { workspace = true } rand_chacha = { workspace = true } +# Used for sudo decentralization +hex-literal = { workspace = true } [dev-dependencies] pallet-balances = { workspace = true, features = ["std"] } @@ -65,6 +66,9 @@ sp-core = { workspace = true } sp-std = { workspace = true } pallet-preimage = { workspace = true } +[build-dependencies] +subtensor-linting = { workspace = true } + [features] default = ["std"] std = [ @@ -104,19 +108,6 @@ std = [ "rand_chacha/std", "sha2/std" ] -runtime-benchmarks = [ - "frame-benchmarking/runtime-benchmarks", - "frame-support/runtime-benchmarks", - "frame-system/runtime-benchmarks", - "pallet-balances/runtime-benchmarks", - "pallet-membership/runtime-benchmarks", - "pallet-utility/runtime-benchmarks", - "sp-runtime/runtime-benchmarks", - "pallet-collective/runtime-benchmarks", - "pallet-preimage/runtime-benchmarks", - "pallet-scheduler/runtime-benchmarks", - "pallet-drand/runtime-benchmarks" -] try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", @@ -130,5 +121,20 @@ try-runtime = [ "pallet-collective/try-runtime", "pallet-drand/try-runtime" ] + +code-coverage = [] +runtime-benchmarks = [ + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "pallet-balances/runtime-benchmarks", + "pallet-membership/runtime-benchmarks", + "pallet-utility/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", + "pallet-collective/runtime-benchmarks", + "pallet-preimage/runtime-benchmarks", + "pallet-scheduler/runtime-benchmarks", + "pallet-drand/runtime-benchmarks", +] pow-faucet = [] fast-blocks = [] diff --git a/pallets/subtensor/build.rs b/pallets/subtensor/build.rs new file mode 100644 index 000000000..c49f3a720 --- /dev/null +++ b/pallets/subtensor/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo:rerun-if-changed=src"); + subtensor_linting::walk_src(); +} diff --git a/pallets/subtensor/src/benchmarks.rs b/pallets/subtensor/src/benchmarking.rs similarity index 100% rename from pallets/subtensor/src/benchmarks.rs rename to pallets/subtensor/src/benchmarking.rs diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 8f9703d36..5f69a1b2d 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -31,7 +31,7 @@ use sp_std::marker::PhantomData; // ============================ // ==== Benchmark Imports ===== // ============================ -mod benchmarks; +mod benchmarking; // ========================= // ==== Pallet Imports ===== diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index cdb8fd52f..c02d56641 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -22,17 +22,15 @@ path = "src/spec_version.rs" [dependencies] ed25519-dalek = { workspace = true, default-features = false, features = ["alloc"] } subtensor-macros.workspace = true -subtensor-custom-rpc-runtime-api = { path = "../pallets/subtensor/runtime-api", default-features = false } +subtensor-custom-rpc-runtime-api = { workspace = true } smallvec = { workspace = true } log = { workspace = true } -codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ - "derive", -] } +codec = { workspace = true } scale-info = { workspace = true, features = ["derive"] } serde_json = { workspace = true, features = ["alloc"] } pallet-aura = { workspace = true } pallet-balances = { workspace = true } -pallet-subtensor = { default-features = false, path = "../pallets/subtensor" } +pallet-subtensor = { workspace = true } frame-support = { workspace = true } pallet-grandpa = { workspace = true } pallet-insecure-randomness-collective-flip = { workspace = true } @@ -60,10 +58,10 @@ sp-version = { workspace = true } # Temporary sudo pallet-sudo = { workspace = true } -pallet-admin-utils = { default-features = false, path = "../pallets/admin-utils" } +pallet-admin-utils = { workspace = true } # Used for sudo decentralization -pallet-collective = { default-features = false, path = "../pallets/collective" } +pallet-collective = { workspace = true } pallet-membership = { workspace = true } # Multisig @@ -89,10 +87,10 @@ frame-benchmarking = { workspace = true, optional = true } frame-system-benchmarking = { workspace = true, optional = true } # Identity registry pallet for registering project info -pallet-registry = { default-features = false, path = "../pallets/registry" } +pallet-registry = { workspace = true } # Metadata commitment pallet -pallet-commitments = { default-features = false, path = "../pallets/commitments" } +pallet-commitments = { workspace = true } # Frontier fp-evm = { workspace = true } @@ -108,7 +106,6 @@ pallet-evm-chain-id = { workspace = true } pallet-evm-precompile-modexp = { workspace = true } pallet-evm-precompile-sha3fips = { workspace = true } pallet-evm-precompile-simple = { workspace = true } -pallet-hotfix-sufficients = { workspace = true } fp-account = { workspace = true } #drand @@ -128,6 +125,7 @@ sp-tracing = { workspace = true } [build-dependencies] substrate-wasm-builder = { workspace = true, optional = true } +subtensor-linting = { workspace = true } [features] default = ["std"] @@ -200,7 +198,6 @@ std = [ "pallet-evm-precompile-modexp/std", "pallet-evm-precompile-sha3fips/std", "pallet-evm-precompile-simple/std", - "pallet-hotfix-sufficients/std", "fp-account/std", "pallet-drand/std", "getrandom/std", @@ -237,7 +234,6 @@ runtime-benchmarks = [ # EVM + Frontier "pallet-ethereum/runtime-benchmarks", "pallet-evm/runtime-benchmarks", - "pallet-hotfix-sufficients/runtime-benchmarks", "pallet-drand/runtime-benchmarks" ] try-runtime = [ @@ -249,7 +245,6 @@ try-runtime = [ "pallet-sudo/try-runtime", "pallet-balances/try-runtime", "pallet-grandpa/try-runtime", - "pallet-hotfix-sufficients/try-runtime", "pallet-insecure-randomness-collective-flip/try-runtime", "pallet-timestamp/try-runtime", "pallet-transaction-payment/try-runtime", diff --git a/runtime/build.rs b/runtime/build.rs index c0fa0405b..15271d2b9 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -16,4 +16,7 @@ fn main() { .enable_metadata_hash("TAO", 9) .build(); } + + println!("cargo:rerun-if-changed=src"); + subtensor_linting::walk_src(); } diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 6b376d88e..000000000 --- a/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -//! This crate exists for linting in CI. Look inside build.rs for reference. diff --git a/support/code-coverage/Cargo.toml b/support/code-coverage/Cargo.toml new file mode 100644 index 000000000..fa4baaed5 --- /dev/null +++ b/support/code-coverage/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "subtensor-code-coverage" +version = "0.1.0" +edition = "2021" + +[dependencies] +build-print.workspace = true +syn.workspace = true +quote.workspace = true +proc-macro2.workspace = true +procedural-fork.workspace = true +walkdir.workspace = true +rayon.workspace = true + +[dev-dependencies] +frame-system.workspace = true +frame-support.workspace = true + +[lints] +workspace = true diff --git a/support/code-coverage/src/lib.rs b/support/code-coverage/src/lib.rs new file mode 100644 index 000000000..e08f02bcc --- /dev/null +++ b/support/code-coverage/src/lib.rs @@ -0,0 +1,620 @@ +#![allow(clippy::unwrap_used)] +#![allow(clippy::arithmetic_side_effects)] + +use build_print::*; +use proc_macro2::TokenStream as TokenStream2; +use procedural_fork::exports::pallet::parse::Def; +use quote::ToTokens; +use rayon::{ + iter::{IntoParallelRefIterator, ParallelIterator}, + slice::ParallelSliceMut, +}; +use std::{ + collections::{HashMap, HashSet}, + ffi::OsStr, + fs, + path::{Path, PathBuf}, + str::FromStr, +}; +use syn::{visit::Visit, Attribute, File, Ident, ItemFn, ItemMod}; +use walkdir::WalkDir; + +/// Code coverage information for a pallet +#[derive(Default, Debug, PartialEq, Eq, Clone)] +pub struct PalletInfo { + pub path: PathBuf, + pub pallet_name: String, + pub methods: Vec, +} + +/// Recursively collects all Rust files in the given directory +pub fn collect_rust_files(dir: &Path) -> Vec { + let mut rust_files = Vec::new(); + + for entry in WalkDir::new(dir) { + let Ok(entry) = entry else { + continue; + }; + let path = entry.path(); + + // Skip any path that contains "target" directory + if path.components().any(|component| { + component.as_os_str() == "target" || component.as_os_str() == "procedural-fork" + }) || path.ends_with("build.rs") + { + continue; + } + + if path.is_file() && path.extension().and_then(|ext| ext.to_str()) == Some("rs") { + rust_files.push(path.to_path_buf()); + } + } + + rust_files +} + +pub fn analyze_files(rust_files: &[PathBuf], workspace_root: &Path) -> Vec { + custom_println!("[code-coverage]", cyan, "generating coverage report..."); + let start = std::time::Instant::now(); + custom_println!( + "[code-coverage]", + cyan, + "searching {} rust files for pallets in parallel...", + rust_files.len() + ); + let infos = rust_files + .par_iter() + .map(|path| { + if path.display().to_string().contains("test") { + return Vec::new(); + } + analyze_file(path, workspace_root) + }) + .reduce(Vec::new, |mut acc, mut infos| { + acc.append(&mut infos); + acc + }); + + custom_println!( + "[code-coverage]", + cyan, + "searching {} rust files for tests in parallel...", + rust_files.len() + ); + let tests = find_tests(rust_files); + let benchmarks = find_benchmarks(rust_files); + + let methods = infos + .iter() + .flat_map(|i| i.methods.iter()) + .collect::>(); + + custom_println!("[code-coverage]", green, "found {} tests", tests.len()); + custom_println!( + "[code-coverage]", + green, + "found {} benchmarks", + benchmarks.len() + ); + + custom_println!( + "[code-coverage]", + green, + "found {} unique calls across {} pallets", + methods.len(), + infos.len(), + ); + + custom_println!("[code-coverage]", cyan, "compiling statistics..."); + + let mut coverage: HashMap = HashMap::new(); + // this takes about 6ms serially so better to keep serial for now + for method in &methods { + coverage + .entry(method.strip_prefix("sudo_").unwrap_or(method).to_string()) + .or_insert(0); + } + for test in &tests { + for method in &test.method_calls { + let method = method.strip_prefix("sudo_").unwrap_or(method); + let Some(count) = coverage.get_mut(method) else { + continue; + }; + *count += 1; + } + } + // if a call is in a benchmark, we can consider it tested since a benchmark test is + // auto-generated + for benchmark in &benchmarks { + for call in &benchmark.calls { + let call = call.strip_prefix("sudo_").unwrap_or(call); + let Some(count) = coverage.get_mut(call) else { + continue; + }; + *count += 1; + } + } + let mut coverage = coverage.into_iter().collect::>(); + coverage.par_sort_by_key(|(_, v)| *v); + + let (covered, uncovered) = coverage.iter().partition::, _>(|(_, v)| *v > 0); + + custom_println!( + "[code-coverage]", + cyan, + " total covered: {}", + covered.len() + ); + custom_println!( + "[code-coverage]", + cyan, + " total uncovered: {}", + uncovered.len() + ); + custom_println!( + "[code-coverage]", + cyan, + " total coverage: {:.2}%", + covered.len() as f64 / methods.len() as f64 * 100.0 + ); + + let finish = std::time::Instant::now(); + custom_println!( + "[code-coverage]", + green, + "coverage report generated in {:?}", + finish - start + ); + infos +} + +fn analyze_file(path: &Path, root_path: &Path) -> Vec { + let Ok(content) = fs::read_to_string(path) else { + return Vec::new(); + }; + let Ok(parsed_tokens) = TokenStream2::from_str(&content) else { + return Vec::new(); + }; + let Ok(file) = syn::parse2::(parsed_tokens) else { + return Vec::new(); + }; + let mut infos = Vec::new(); + PalletVisitor::for_each_pallet(&file, path, root_path, |_item_mod, pallet: &Def| { + custom_println!( + "[code-coverage]", + green, + "parsed pallet '{}' ({})", + extract_pallet_name(path).unwrap_or("unknown".to_string()), + strip_common_suffix("/src/lib.rs".as_ref(), strip_common_prefix(root_path, path)) + .display(), + ); + let mut info = PalletInfo { + path: path.to_path_buf(), + pallet_name: extract_pallet_name(path).unwrap_or("pallet".to_string()), + ..Default::default() + }; + + // collect all Call methods + if let Some(call) = &pallet.call { + info.methods + .append(&mut call.methods.iter().map(|m| m.name.to_string()).collect()); + } + + infos.push(info); + }); + infos +} + +#[derive(Default, Debug, Clone, PartialEq, Eq)] +pub struct TestInfo { + pub path: PathBuf, + pub name: String, + pub method_calls: HashSet, +} + +/// Finds all tests in the given set of rust files, using a parallel map-reduce. +pub fn find_tests(rust_files: &[PathBuf]) -> Vec { + rust_files + .par_iter() + .map(|path| { + let Ok(content) = fs::read_to_string(path) else { + return Vec::new(); + }; + let Ok(file) = syn::parse_file(&content) else { + return Vec::new(); + }; + let mut visitor = TestVisitor { tests: Vec::new() }; + visitor.visit_file(&file); + visitor + .tests + .into_iter() + .map(|f| { + let mut method_calls = HashSet::new(); + let mut visitor = MethodCallVisitor { + method_calls: &mut method_calls, + }; + visitor.visit_item_fn(&f); + TestInfo { + path: path.clone(), + name: f.sig.ident.to_string(), + method_calls, + } + }) + .collect() + }) + .reduce(Vec::new, |mut acc, mut infos| { + acc.append(&mut infos); + acc + }) +} + +#[derive(Default, Debug, Clone, PartialEq, Eq)] +pub struct BenchmarkInfo { + pub calls: HashSet, +} + +/// Finds all benchmarks in the given set of rust files, using a parallel map-reduce +pub fn find_benchmarks(rust_files: &[PathBuf]) -> Vec { + rust_files + .par_iter() + .map(|path| { + let Ok(content) = fs::read_to_string(path) else { + return Vec::new(); + }; + let Ok(file) = syn::parse_file(&content) else { + return Vec::new(); + }; + let mut visitor = BenchmarkVisitor { + benchmarks: Vec::new(), + }; + visitor.visit_file(&file); + visitor + .benchmarks + .into_iter() + .map(|f| { + let mut calls = HashSet::new(); + let mut visitor = CallVisitor { calls: &mut calls }; + visitor.visit_item_fn(&f); + BenchmarkInfo { calls } + }) + .collect() + }) + .reduce(Vec::new, |mut acc, mut infos| { + acc.append(&mut infos); + acc + }) +} + +pub struct CallVisitor<'a> { + pub calls: &'a mut HashSet, +} + +impl<'ast> Visit<'ast> for CallVisitor<'_> { + fn visit_expr_call(&mut self, i: &'ast syn::ExprCall) { + if let syn::Expr::Path(expr) = &*i.func { + if let Some(seg) = expr.path.segments.last() { + self.calls.insert(seg.ident.to_string()); + } + } + syn::visit::visit_expr_call(self, i); + } +} + +pub struct MethodCallVisitor<'a> { + pub method_calls: &'a mut HashSet, +} + +impl<'ast> Visit<'ast> for MethodCallVisitor<'_> { + fn visit_expr_method_call(&mut self, i: &'ast syn::ExprMethodCall) { + self.method_calls.insert(i.method.to_string()); + syn::visit::visit_expr_method_call(self, i); + } + + fn visit_expr_path(&mut self, i: &'ast syn::ExprPath) { + self.method_calls.insert( + i.path + .segments + .last() + .expect("at least one element is expected in path") + .ident + .to_string(), + ); + syn::visit::visit_expr_path(self, i); + } +} + +pub struct TestVisitor { + pub tests: Vec, +} + +impl<'ast> Visit<'ast> for TestVisitor { + fn visit_item_fn(&mut self, item_fn: &'ast ItemFn) { + if item_fn.attrs.iter().any(|attr| { + let Some(seg) = attr.path().segments.last() else { + return false; + }; + seg.ident == "test" || seg.ident == "should_panic" + }) { + self.tests.push(item_fn.clone()); + } + syn::visit::visit_item_fn(self, item_fn); + } +} + +pub struct BenchmarkVisitor { + pub benchmarks: Vec, +} + +impl<'ast> Visit<'ast> for BenchmarkVisitor { + fn visit_item_fn(&mut self, item_fn: &'ast ItemFn) { + if item_fn.attrs.iter().any(|attr| { + let Some(seg) = attr.path().segments.last() else { + return false; + }; + seg.ident == "benchmark" + }) { + self.benchmarks.push(item_fn.clone()); + } + syn::visit::visit_item_fn(self, item_fn); + } +} + +/// Tries to parse a pallet from a module +pub fn try_parse_pallet(item_mod: &ItemMod, file_path: &Path, root_path: &Path) -> Option { + // skip non-inline modules + let mut item_mod = item_mod.clone(); + let (_, ref mut content) = item_mod.content.as_mut()?; + + // skip non-pallet modules + if item_mod.ident != "pallet" { + return None; + } + + let mut section_announced = false; + + // manually import foreign sections defined by the `#[import_section]` attribute + for attr in item_mod.attrs.iter() { + if attr + .meta + .path() + .segments + .last() + .expect("at least one element is expected in path") + .ident + != "import_section" + { + continue; + } + + // Extract the section name from the attribute's args + let Ok(inner_path) = attr.parse_args::() else { + continue; + }; + let section_name = &inner_path + .segments + .last() + .expect("at least one element is expected in path") + .ident; + + if !section_announced { + custom_println!( + "[code-coverage]", + cyan, + "importing pallet sections for '{}' ({})...", + extract_pallet_name(file_path).unwrap_or("unknown".to_string()), + strip_common_suffix( + "/src/lib.rs".as_ref(), + strip_common_prefix(root_path, file_path) + ) + .display(), + ); + section_announced = true; + } + + if let Some((section_mod, section_path)) = + find_matching_pallet_section(file_path, section_name) + { + let Some((_, mut section_content)) = section_mod.content else { + continue; + }; + content.append(&mut section_content); + custom_println!( + "[code-coverage]", + cyan, + "└ imported '{}' ({})", + section_name, + strip_common_suffix( + "/src/lib.rs".as_ref(), + strip_common_prefix(file_path, §ion_path) + ) + .display() + ); + } else { + custom_println!( + "[code-coverage]", + red, + "could not find matching section for: '{}'", + section_name, + ); + } + } + + if let Ok(pallet) = Def::try_from(item_mod.clone(), false) { + Some(pallet) + } else if let Ok(pallet) = Def::try_from(item_mod.clone(), true) { + Some(pallet) + } else { + let err = match Def::try_from(item_mod.clone(), false) { + Err(e) => e, + Ok(_) => unreachable!(), + }; + custom_println!( + "[code-coverage]", + red, + "unable to parse pallet in {}:", + file_path.display() + ); + custom_println!("[code-coverage]", red, "{}", err); + None + } +} + +fn find_matching_pallet_section( + src_path: &Path, + section_name: &Ident, +) -> Option<(ItemMod, PathBuf)> { + let base_path = src_path.parent()?; + let rust_files = WalkDir::new( + base_path + .parent() + .expect("the base path is not a top level directory"), + ) + .into_iter() + .filter_map(Result::ok) + .filter(|e| { + e.path() != src_path && e.path().is_file() && e.path().extension() == Some(OsStr::new("rs")) + }) + .map(|e| e.path().to_path_buf()) + .collect::>(); + let section_name = section_name.to_string().trim().to_string(); + rust_files + .par_iter() + .find_map_any(|path| { + if path.display().to_string().contains("test") { + return None; + } + let Ok(content) = fs::read_to_string(path) else { + return None; + }; + let Ok(file) = syn::parse_file(&content) else { + return None; + }; + for item in file.items { + let syn::Item::Mod(item_mod) = item else { + continue; + }; + if item_mod.ident != section_name { + continue; + } + if item_mod.attrs.iter().any(is_pallet_section) { + // can't move ItemMod across thread boundaries + return Some((item_mod.to_token_stream().to_string(), path.to_path_buf())); + } + } + None + }) + .map(|(s, p)| (syn::parse_str::(&s).unwrap(), p)) // can't move ItemMod across thread boundaries +} + +fn is_pallet_section(attr: &Attribute) -> bool { + attr.meta.path().segments.last().unwrap().ident != "pallet_section" +} + +/// A visitor that collects pallets from a file/module +#[derive(Default)] +pub struct PalletVisitor { + pub pallets: Vec<(ItemMod, Def)>, + pub file_path: PathBuf, + pub workspace_root_path: PathBuf, +} + +impl PalletVisitor { + pub fn for_each_pallet(file: &File, file_path: &Path, root_path: &Path, mut f: F) -> Self + where + F: FnMut(&ItemMod, &Def), + { + let mut visitor = PalletVisitor { + pallets: Vec::new(), + file_path: file_path.to_path_buf(), + workspace_root_path: root_path.to_path_buf(), + }; + visitor.visit_file(file); + for (item_mod, pallet) in &visitor.pallets { + f(item_mod, pallet); + } + visitor + } +} + +impl<'ast> Visit<'ast> for PalletVisitor { + fn visit_item_mod(&mut self, item_mod: &'ast ItemMod) { + if let Some(pallet) = try_parse_pallet(item_mod, &self.file_path, &self.workspace_root_path) + { + self.pallets.push((item_mod.clone(), pallet)); + } + syn::visit::visit_item_mod(self, item_mod); + } +} + +/// Extracts the pallet name from a path +pub fn extract_pallet_name(path: &Path) -> Option { + // Try to get the parent directory, then the directory name + path.parent()? + .parent()? // Go up one level to the "pallets" directory + .file_name() // Get the directory name "subtensor" + .and_then(|os_str| os_str.to_str()) // Convert OsStr to &str + .map(|s| s.to_string()) // Convert &str to String +} + +/// Strips the longest common prefix from two paths (i.e. base is allowed to have more +/// components that are not shared with target and these are ignored) +pub fn strip_common_prefix<'a>(base: &'a Path, target: &'a Path) -> &'a Path { + let mut base_components = base.components(); + let mut target_components = target.components(); + let mut common_length = 0; + + // Find the longest common prefix + while let (Some(bc), Some(tc)) = (base_components.next(), target_components.next()) { + if bc == tc { + common_length += 1; + } else { + break; + } + } + + // Create a Path that skips the common prefix + let mut remaining = target; + for _ in 0..common_length { + remaining = remaining + .strip_prefix(remaining.components().next().unwrap()) + .unwrap_or(remaining); + } + + remaining +} + +/// Strips the longest common suffix from two paths (i.e. base is allowed to have more +/// leading components that are not shared with target and these are ignored) +pub fn strip_common_suffix<'a>(base: &'a Path, target: &'a Path) -> &'a Path { + let base_components: Vec<_> = base.components().collect(); + let target_components: Vec<_> = target.components().collect(); + + let mut common_suffix_length = 0; + + // Reverse iterate over both paths to find the longest common suffix + for (bc, tc) in base_components + .iter() + .rev() + .zip(target_components.iter().rev()) + { + if bc == tc { + common_suffix_length += 1; + } else { + break; + } + } + + // If there is no common suffix, return target verbatim + if common_suffix_length == 0 { + return target; + } + + // Create a new path without the common suffix + let mut remaining = target; + + for _ in 0..common_suffix_length { + remaining = remaining.parent().unwrap_or(target); + } + + remaining +} diff --git a/support/code-coverage/src/main.rs b/support/code-coverage/src/main.rs new file mode 100644 index 000000000..9fc3266fa --- /dev/null +++ b/support/code-coverage/src/main.rs @@ -0,0 +1,14 @@ +use std::env; +use std::path::PathBuf; + +fn main() { + // should we instead provide an argument to set the directory? + let source_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR should be set"); + let mut source_dir = PathBuf::from(&source_dir); + source_dir.pop(); + source_dir.pop(); + let rust_files = subtensor_code_coverage::collect_rust_files(source_dir.as_path()); + + // Generate code coverage report + subtensor_code_coverage::analyze_files(&rust_files, source_dir.as_path()); +} diff --git a/support/linting/Cargo.toml b/support/linting/Cargo.toml index 4378ca9dd..725588f2d 100644 --- a/support/linting/Cargo.toml +++ b/support/linting/Cargo.toml @@ -4,10 +4,13 @@ version = "0.1.0" edition = "2021" [dependencies] -syn.workspace = true -quote.workspace = true +build-print.workspace = true proc-macro2.workspace = true -procedural-fork = { version = "1.10.0-rc3", path = "../procedural-fork" } +procedural-fork.workspace = true +quote.workspace = true +rayon.workspace = true +syn.workspace = true +walkdir.workspace = true [lints] workspace = true diff --git a/support/linting/src/disallow_v1_benchmarks.rs b/support/linting/src/disallow_v1_benchmarks.rs new file mode 100644 index 000000000..593a361f2 --- /dev/null +++ b/support/linting/src/disallow_v1_benchmarks.rs @@ -0,0 +1,72 @@ +use super::*; +use syn::{spanned::Spanned, visit::Visit}; + +pub struct DisallowV1Benchmarks; + +impl Lint for DisallowV1Benchmarks { + fn lint(source: &syn::File) -> Result { + let mut visitor = ExprMacroVisitor::new(); + visitor.visit_file(source); + + if !visitor.errors.is_empty() { + return Err(visitor.errors); + } + + Ok(()) + } +} + +struct ExprMacroVisitor { + errors: Vec, +} + +impl<'ast> syn::visit::Visit<'ast> for ExprMacroVisitor { + fn visit_expr_macro(&mut self, i: &'ast syn::ExprMacro) { + let Some(segment) = i.mac.path.segments.last() else { + return; + }; + + if segment.ident == "benchmarks" { + self.errors.push(syn::Error::new( + segment.span(), + "V1 benchmark syntax is disallowed!".to_owned(), + )); + } + } +} + +impl ExprMacroVisitor { + fn new() -> Self { + Self { errors: Vec::new() } + } +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + use quote::quote; + + fn lint_macro(input: proc_macro2::TokenStream) -> Result { + let expr_macro: syn::ExprMacro = syn::parse2(input).unwrap(); + let mut visitor = ExprMacroVisitor::new(); + visitor.visit_expr_macro(&expr_macro); + if !visitor.errors.is_empty() { + return Err(visitor.errors); + } + Ok(()) + } + + #[test] + fn test_disallow_benchmarks_v1() { + let input = quote! { + benchmarks! { + benchmark_test { + + }: _(RawOrigin::Root) + } + }; + + lint_macro(input).unwrap_err(); + } +} diff --git a/support/linting/src/lib.rs b/support/linting/src/lib.rs index a65466e6a..2621d5cc1 100644 --- a/support/linting/src/lib.rs +++ b/support/linting/src/lib.rs @@ -1,11 +1,13 @@ pub mod lint; pub use lint::*; +mod disallow_v1_benchmarks; mod forbid_as_primitive; mod forbid_keys_remove; mod pallet_index; mod require_freeze_struct; +pub use disallow_v1_benchmarks::DisallowV1Benchmarks; pub use forbid_as_primitive::ForbidAsPrimitiveConversion; pub use forbid_keys_remove::ForbidKeysRemoveCall; pub use pallet_index::RequireExplicitPalletIndex; diff --git a/support/linting/src/lint.rs b/support/linting/src/lint.rs index fdf35ae7c..4b607a0bd 100644 --- a/support/linting/src/lint.rs +++ b/support/linting/src/lint.rs @@ -1,5 +1,18 @@ +use std::env; +use std::path::{Path, PathBuf}; +use std::sync::mpsc; +use std::{fs, str::FromStr}; + use proc_macro2::TokenTree; -use syn::{Attribute, File, Meta, MetaList, Path}; +use syn::{Attribute, File, Meta, MetaList, Path as SynPath}; + +use rayon::prelude::*; +use walkdir::WalkDir; + +use crate::{ + DisallowV1Benchmarks, ForbidAsPrimitiveConversion, RequireExplicitPalletIndex, + RequireFreezeStruct, +}; pub type Result = core::result::Result<(), Vec>; @@ -18,7 +31,7 @@ pub fn is_allowed(attibutes: &[Attribute]) -> bool { let Attribute { meta: Meta::List(MetaList { - path: Path { segments: attr, .. }, + path: SynPath { segments: attr, .. }, tokens: attr_args, .. }), @@ -38,3 +51,82 @@ pub fn is_allowed(attibutes: &[Attribute]) -> bool { }) }) } + +pub fn walk_src() { + let source_dir = + env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is expected to be set"); + let source_dir = Path::new(&source_dir); + + let rust_files = collect_rust_files(source_dir); + + let (tx, rx) = mpsc::channel(); + + // Parse each rust file with syn and run the linting suite on it in parallel + rust_files.par_iter().for_each_with(tx.clone(), |tx, file| { + let Ok(content) = fs::read_to_string(file) else { + return; + }; + let Ok(parsed_tokens) = proc_macro2::TokenStream::from_str(&content) else { + return; + }; + let Ok(parsed_file) = syn::parse2::(parsed_tokens) else { + return; + }; + + let track_lint = |result: Result| { + let Err(errors) = result else { + return; + }; + let relative_path = file.strip_prefix(source_dir).unwrap_or(file.as_path()); + for error in errors { + let loc = error.span().start(); + let file_path = relative_path.display(); + // note that spans can't go across thread boundaries so we serialize here and send a String + tx.send(format!( + "cargo:warning={}:{}:{}: {}", + file_path, loc.line, loc.column, error, + )) + .expect("sending via unbound channel should work"); + } + }; + + track_lint(DisallowV1Benchmarks::lint(&parsed_file)); + track_lint(ForbidAsPrimitiveConversion::lint(&parsed_file)); + track_lint(RequireFreezeStruct::lint(&parsed_file)); + track_lint(RequireExplicitPalletIndex::lint(&parsed_file)); + }); + + // Collect and print all errors after the parallel processing is done + drop(tx); // Close the sending end of the channel + + for error in rx { + println!("{error}"); + } +} + +// copied from the `code-coverage` crate, to not introduce dependency +fn collect_rust_files(dir: &Path) -> Vec { + let mut rust_files = Vec::new(); + + for entry in WalkDir::new(dir) { + let Ok(entry) = entry else { + continue; + }; + let path = entry.path(); + + // Skip any path that contains "target" directory + if path + .components() + .any(|component| component.as_os_str() == "target") + || path.ends_with("build.rs") + { + continue; + } + + if path.is_file() && path.extension().and_then(|ext| ext.to_str()) == Some("rs") { + rust_files.push(path.to_path_buf()); + } + } + + rust_files +} diff --git a/support/procedural-fork/Cargo.toml b/support/procedural-fork/Cargo.toml index 503c81f1f..be58492dc 100644 --- a/support/procedural-fork/Cargo.toml +++ b/support/procedural-fork/Cargo.toml @@ -15,10 +15,11 @@ proc-macro2.workspace = true quote.workspace = true syn.workspace = true macro_magic = { version = "0.5", features = ["proc_support"] } -frame-support-procedural-tools = { version = "10.0.0" } +frame-support-procedural-tools.workspace = true proc-macro-warning = { version = "1", default-features = false } expander = "2" sp-crypto-hashing = { default-features = false, version = "0.1.0" } +regex = "1" [dev-dependencies] regex = "1" diff --git a/support/procedural-fork/src/benchmark.rs b/support/procedural-fork/src/benchmark.rs index 0eb3c330a..045454dd9 100644 --- a/support/procedural-fork/src/benchmark.rs +++ b/support/procedural-fork/src/benchmark.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/call.rs b/support/procedural-fork/src/construct_runtime/expand/call.rs index cc467c31d..aeab0afa6 100644 --- a/support/procedural-fork/src/construct_runtime/expand/call.rs +++ b/support/procedural-fork/src/construct_runtime/expand/call.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/composite_helper.rs b/support/procedural-fork/src/construct_runtime/expand/composite_helper.rs index be6b2f085..dea809ac7 100644 --- a/support/procedural-fork/src/construct_runtime/expand/composite_helper.rs +++ b/support/procedural-fork/src/construct_runtime/expand/composite_helper.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/config.rs b/support/procedural-fork/src/construct_runtime/expand/config.rs index ff715e584..e70506779 100644 --- a/support/procedural-fork/src/construct_runtime/expand/config.rs +++ b/support/procedural-fork/src/construct_runtime/expand/config.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/freeze_reason.rs b/support/procedural-fork/src/construct_runtime/expand/freeze_reason.rs index 131c919ef..797263433 100644 --- a/support/procedural-fork/src/construct_runtime/expand/freeze_reason.rs +++ b/support/procedural-fork/src/construct_runtime/expand/freeze_reason.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/hold_reason.rs b/support/procedural-fork/src/construct_runtime/expand/hold_reason.rs index 58870a321..17ee803f9 100644 --- a/support/procedural-fork/src/construct_runtime/expand/hold_reason.rs +++ b/support/procedural-fork/src/construct_runtime/expand/hold_reason.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/inherent.rs b/support/procedural-fork/src/construct_runtime/expand/inherent.rs index 9705f9703..2467c0a74 100644 --- a/support/procedural-fork/src/construct_runtime/expand/inherent.rs +++ b/support/procedural-fork/src/construct_runtime/expand/inherent.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/lock_id.rs b/support/procedural-fork/src/construct_runtime/expand/lock_id.rs index 67c2fb933..ac4bafd7e 100644 --- a/support/procedural-fork/src/construct_runtime/expand/lock_id.rs +++ b/support/procedural-fork/src/construct_runtime/expand/lock_id.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/metadata.rs b/support/procedural-fork/src/construct_runtime/expand/metadata.rs index 9f3d9cd4a..004dc350b 100644 --- a/support/procedural-fork/src/construct_runtime/expand/metadata.rs +++ b/support/procedural-fork/src/construct_runtime/expand/metadata.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/mod.rs b/support/procedural-fork/src/construct_runtime/expand/mod.rs index 88f9a3c6e..eff0cccc1 100644 --- a/support/procedural-fork/src/construct_runtime/expand/mod.rs +++ b/support/procedural-fork/src/construct_runtime/expand/mod.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/origin.rs b/support/procedural-fork/src/construct_runtime/expand/origin.rs index 58c8adec5..0d1f9dd73 100644 --- a/support/procedural-fork/src/construct_runtime/expand/origin.rs +++ b/support/procedural-fork/src/construct_runtime/expand/origin.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/outer_enums.rs b/support/procedural-fork/src/construct_runtime/expand/outer_enums.rs index 28e39c7a2..48a29a086 100644 --- a/support/procedural-fork/src/construct_runtime/expand/outer_enums.rs +++ b/support/procedural-fork/src/construct_runtime/expand/outer_enums.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/slash_reason.rs b/support/procedural-fork/src/construct_runtime/expand/slash_reason.rs index 0695d8102..50cba8d73 100644 --- a/support/procedural-fork/src/construct_runtime/expand/slash_reason.rs +++ b/support/procedural-fork/src/construct_runtime/expand/slash_reason.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/task.rs b/support/procedural-fork/src/construct_runtime/expand/task.rs index 94a5f52bb..fdfc4d268 100644 --- a/support/procedural-fork/src/construct_runtime/expand/task.rs +++ b/support/procedural-fork/src/construct_runtime/expand/task.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/expand/unsigned.rs b/support/procedural-fork/src/construct_runtime/expand/unsigned.rs index 109f7081c..ad81c9c08 100644 --- a/support/procedural-fork/src/construct_runtime/expand/unsigned.rs +++ b/support/procedural-fork/src/construct_runtime/expand/unsigned.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/mod.rs b/support/procedural-fork/src/construct_runtime/mod.rs index 9bc271fdc..f38a47631 100644 --- a/support/procedural-fork/src/construct_runtime/mod.rs +++ b/support/procedural-fork/src/construct_runtime/mod.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/construct_runtime/parse.rs b/support/procedural-fork/src/construct_runtime/parse.rs index e5e60b3ff..09ca2d351 100644 --- a/support/procedural-fork/src/construct_runtime/parse.rs +++ b/support/procedural-fork/src/construct_runtime/parse.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/crate_version.rs b/support/procedural-fork/src/crate_version.rs index 63e7c7279..f65b4cbd5 100644 --- a/support/procedural-fork/src/crate_version.rs +++ b/support/procedural-fork/src/crate_version.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/derive_impl.rs b/support/procedural-fork/src/derive_impl.rs index e91f9c534..866be5bab 100644 --- a/support/procedural-fork/src/derive_impl.rs +++ b/support/procedural-fork/src/derive_impl.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. @@ -247,6 +248,7 @@ pub fn derive_impl( } #[test] +#[ignore] fn test_derive_impl_attr_args_parsing() { parse2::(quote!( some::path::TestDefaultConfig as some::path::DefaultConfig @@ -264,6 +266,7 @@ fn test_derive_impl_attr_args_parsing() { } #[test] +#[ignore] fn test_runtime_type_with_doc() { trait TestTrait { type Test; @@ -286,6 +289,7 @@ fn test_runtime_type_with_doc() { } #[test] +#[ignore] fn test_disambiguation_path() { let foreign_impl: ItemImpl = parse_quote!(impl SomeTrait for SomeType {}); let default_impl_path: Path = parse_quote!(SomeScope::SomeType); diff --git a/support/procedural-fork/src/dummy_part_checker.rs b/support/procedural-fork/src/dummy_part_checker.rs index 6bed541d1..a226c8f21 100644 --- a/support/procedural-fork/src/dummy_part_checker.rs +++ b/support/procedural-fork/src/dummy_part_checker.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/dynamic_params.rs b/support/procedural-fork/src/dynamic_params.rs index e1f9f626c..1dd96c258 100644 --- a/support/procedural-fork/src/dynamic_params.rs +++ b/support/procedural-fork/src/dynamic_params.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/key_prefix.rs b/support/procedural-fork/src/key_prefix.rs index aea60ce3b..51f7af1fe 100644 --- a/support/procedural-fork/src/key_prefix.rs +++ b/support/procedural-fork/src/key_prefix.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/lib.rs b/support/procedural-fork/src/lib.rs index c7f1472b2..7564514cd 100644 --- a/support/procedural-fork/src/lib.rs +++ b/support/procedural-fork/src/lib.rs @@ -12,63 +12,50 @@ #![recursion_limit = "512"] #![allow(warnings)] #![allow(clippy::all)] +#![ignore] // ensure procedural-fork tests are not run extern crate proc_macro; -#[cfg(not(test))] mod benchmark; -#[cfg(not(test))] mod construct_runtime; -#[cfg(not(test))] mod crate_version; -#[cfg(not(test))] mod derive_impl; -#[cfg(not(test))] mod dummy_part_checker; -#[cfg(not(test))] mod dynamic_params; -#[cfg(not(test))] mod key_prefix; -#[cfg(not(test))] mod match_and_insert; -#[cfg(not(test))] mod no_bound; -#[cfg(not(test))] mod pallet; -#[cfg(not(test))] mod pallet_error; -#[cfg(not(test))] mod runtime; -#[cfg(not(test))] mod storage_alias; -#[cfg(not(test))] mod transactional; -#[cfg(not(test))] mod tt_macro; -#[cfg(not(test))] -use std::{cell::RefCell, str::FromStr}; -#[cfg(not(test))] +use std::{ + cell::RefCell, + env::{set_var, var}, + path::PathBuf, + str::FromStr, + sync::Mutex, +}; + pub(crate) const INHERENT_INSTANCE_NAME: &str = "__InherentHiddenInstance"; /// The number of module instances supported by the runtime, starting at index 1, /// and up to `NUMBER_OF_INSTANCE`. -#[cfg(not(test))] pub(crate) const NUMBER_OF_INSTANCE: u8 = 16; thread_local! { /// A global counter, can be used to generate a relatively unique identifier. - #[cfg(not(test))] static COUNTER: RefCell = const { RefCell::new(Counter(0)) }; } /// Counter to generate a relatively unique identifier for macros. This is necessary because /// declarative macros gets hoisted to the crate root, which shares the namespace with other pallets /// containing the very same macros. -#[cfg(not(test))] struct Counter(u64); -#[cfg(not(test))] impl Counter { fn inc(&mut self) -> u64 { let ret = self.0; @@ -80,7 +67,6 @@ impl Counter { /// Get the value from the given environment variable set by cargo. /// /// The value is parsed into the requested destination type. -#[cfg(not(test))] fn get_cargo_env_var(version_env: &str) -> std::result::Result { let version = std::env::var(version_env) .unwrap_or_else(|_| panic!("`{}` is always set by cargo; qed", version_env)); @@ -90,13 +76,32 @@ fn get_cargo_env_var(version_env: &str) -> std::result::Result String { format!("CounterFor{}", prefix) } -#[cfg(not(test))] +/// Improvement on [`exports::simulate_manifest_dir`] that allows for an arbitrary return type +pub fn simulate_manifest_dir(path: P, closure: F) -> R +where + P: AsRef, + F: FnOnce() -> R + std::panic::UnwindSafe, +{ + static MANIFEST_DIR_LOCK: Mutex<()> = Mutex::new(()); + let guard = MANIFEST_DIR_LOCK.lock().unwrap(); + let orig = PathBuf::from( + var("CARGO_MANIFEST_DIR").expect("failed to read ENV var `CARGO_MANIFEST_DIR`"), + ); + set_var("CARGO_MANIFEST_DIR", orig.join(path.as_ref())); + let result = std::panic::catch_unwind(closure); + set_var("CARGO_MANIFEST_DIR", &orig); + drop(guard); + result.unwrap() +} + +#[cfg(not(doc))] pub mod exports { + pub use crate::pallet::parse::tests::simulate_manifest_dir; + pub mod benchmark { pub use crate::benchmark::*; } diff --git a/support/procedural-fork/src/match_and_insert.rs b/support/procedural-fork/src/match_and_insert.rs index a80b6e95f..475136c13 100644 --- a/support/procedural-fork/src/match_and_insert.rs +++ b/support/procedural-fork/src/match_and_insert.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/no_bound/clone.rs b/support/procedural-fork/src/no_bound/clone.rs index 17039bdc8..8071e82d9 100644 --- a/support/procedural-fork/src/no_bound/clone.rs +++ b/support/procedural-fork/src/no_bound/clone.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/no_bound/debug.rs b/support/procedural-fork/src/no_bound/debug.rs index 8034bb5ec..3a171f1a0 100644 --- a/support/procedural-fork/src/no_bound/debug.rs +++ b/support/procedural-fork/src/no_bound/debug.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/no_bound/default.rs b/support/procedural-fork/src/no_bound/default.rs index 3f896da35..108ceef84 100644 --- a/support/procedural-fork/src/no_bound/default.rs +++ b/support/procedural-fork/src/no_bound/default.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/no_bound/mod.rs b/support/procedural-fork/src/no_bound/mod.rs index 9e0377dda..f33bf0911 100644 --- a/support/procedural-fork/src/no_bound/mod.rs +++ b/support/procedural-fork/src/no_bound/mod.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/no_bound/ord.rs b/support/procedural-fork/src/no_bound/ord.rs index 20f30eb9d..4b700280a 100644 --- a/support/procedural-fork/src/no_bound/ord.rs +++ b/support/procedural-fork/src/no_bound/ord.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/no_bound/partial_eq.rs b/support/procedural-fork/src/no_bound/partial_eq.rs index 8833f6e5f..0b5b3a788 100644 --- a/support/procedural-fork/src/no_bound/partial_eq.rs +++ b/support/procedural-fork/src/no_bound/partial_eq.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/no_bound/partial_ord.rs b/support/procedural-fork/src/no_bound/partial_ord.rs index c73199d4e..dfd33872d 100644 --- a/support/procedural-fork/src/no_bound/partial_ord.rs +++ b/support/procedural-fork/src/no_bound/partial_ord.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/call.rs b/support/procedural-fork/src/pallet/expand/call.rs index a39e81fd1..cd6536fcf 100644 --- a/support/procedural-fork/src/pallet/expand/call.rs +++ b/support/procedural-fork/src/pallet/expand/call.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/composite.rs b/support/procedural-fork/src/pallet/expand/composite.rs index 49c0ad675..aacf70bf6 100644 --- a/support/procedural-fork/src/pallet/expand/composite.rs +++ b/support/procedural-fork/src/pallet/expand/composite.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/config.rs b/support/procedural-fork/src/pallet/expand/config.rs index 836c74ae7..56811626c 100644 --- a/support/procedural-fork/src/pallet/expand/config.rs +++ b/support/procedural-fork/src/pallet/expand/config.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/constants.rs b/support/procedural-fork/src/pallet/expand/constants.rs index 19862a8a6..74b71b97a 100644 --- a/support/procedural-fork/src/pallet/expand/constants.rs +++ b/support/procedural-fork/src/pallet/expand/constants.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/doc_only.rs b/support/procedural-fork/src/pallet/expand/doc_only.rs index 3e60e9a9b..87579bcde 100644 --- a/support/procedural-fork/src/pallet/expand/doc_only.rs +++ b/support/procedural-fork/src/pallet/expand/doc_only.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/documentation.rs b/support/procedural-fork/src/pallet/expand/documentation.rs index 62b2e8b8b..6d6abdcf1 100644 --- a/support/procedural-fork/src/pallet/expand/documentation.rs +++ b/support/procedural-fork/src/pallet/expand/documentation.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/error.rs b/support/procedural-fork/src/pallet/expand/error.rs index 1b76034ef..66d156220 100644 --- a/support/procedural-fork/src/pallet/expand/error.rs +++ b/support/procedural-fork/src/pallet/expand/error.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/event.rs b/support/procedural-fork/src/pallet/expand/event.rs index 931dcd95a..eb7d5b123 100644 --- a/support/procedural-fork/src/pallet/expand/event.rs +++ b/support/procedural-fork/src/pallet/expand/event.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/genesis_build.rs b/support/procedural-fork/src/pallet/expand/genesis_build.rs index c6089550d..42d413ddc 100644 --- a/support/procedural-fork/src/pallet/expand/genesis_build.rs +++ b/support/procedural-fork/src/pallet/expand/genesis_build.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/genesis_config.rs b/support/procedural-fork/src/pallet/expand/genesis_config.rs index e171e2468..a5deb70d6 100644 --- a/support/procedural-fork/src/pallet/expand/genesis_config.rs +++ b/support/procedural-fork/src/pallet/expand/genesis_config.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/hooks.rs b/support/procedural-fork/src/pallet/expand/hooks.rs index 8ff0e8f30..6f1bb2ab2 100644 --- a/support/procedural-fork/src/pallet/expand/hooks.rs +++ b/support/procedural-fork/src/pallet/expand/hooks.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/inherent.rs b/support/procedural-fork/src/pallet/expand/inherent.rs index 0a80d672a..b685b071f 100644 --- a/support/procedural-fork/src/pallet/expand/inherent.rs +++ b/support/procedural-fork/src/pallet/expand/inherent.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/instances.rs b/support/procedural-fork/src/pallet/expand/instances.rs index 12423409c..f0515fd14 100644 --- a/support/procedural-fork/src/pallet/expand/instances.rs +++ b/support/procedural-fork/src/pallet/expand/instances.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/mod.rs b/support/procedural-fork/src/pallet/expand/mod.rs index ff4423f85..4b07a83fb 100644 --- a/support/procedural-fork/src/pallet/expand/mod.rs +++ b/support/procedural-fork/src/pallet/expand/mod.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/origin.rs b/support/procedural-fork/src/pallet/expand/origin.rs index 167445ad6..e1470d4d8 100644 --- a/support/procedural-fork/src/pallet/expand/origin.rs +++ b/support/procedural-fork/src/pallet/expand/origin.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/pallet_struct.rs b/support/procedural-fork/src/pallet/expand/pallet_struct.rs index 64e5d533c..5b1b152c6 100644 --- a/support/procedural-fork/src/pallet/expand/pallet_struct.rs +++ b/support/procedural-fork/src/pallet/expand/pallet_struct.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/storage.rs b/support/procedural-fork/src/pallet/expand/storage.rs index 95b046670..efd8b3570 100644 --- a/support/procedural-fork/src/pallet/expand/storage.rs +++ b/support/procedural-fork/src/pallet/expand/storage.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/tasks.rs b/support/procedural-fork/src/pallet/expand/tasks.rs index 8a0bd2252..c1eb88d86 100644 --- a/support/procedural-fork/src/pallet/expand/tasks.rs +++ b/support/procedural-fork/src/pallet/expand/tasks.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] //! Contains logic for expanding task-related items. // This file is part of Substrate. diff --git a/support/procedural-fork/src/pallet/expand/tt_default_parts.rs b/support/procedural-fork/src/pallet/expand/tt_default_parts.rs index 8e7dc39d8..7d924a565 100644 --- a/support/procedural-fork/src/pallet/expand/tt_default_parts.rs +++ b/support/procedural-fork/src/pallet/expand/tt_default_parts.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/type_value.rs b/support/procedural-fork/src/pallet/expand/type_value.rs index 84db3e431..b38a2b5ea 100644 --- a/support/procedural-fork/src/pallet/expand/type_value.rs +++ b/support/procedural-fork/src/pallet/expand/type_value.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/validate_unsigned.rs b/support/procedural-fork/src/pallet/expand/validate_unsigned.rs index 28c78a1c6..532fd4d07 100644 --- a/support/procedural-fork/src/pallet/expand/validate_unsigned.rs +++ b/support/procedural-fork/src/pallet/expand/validate_unsigned.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/expand/warnings.rs b/support/procedural-fork/src/pallet/expand/warnings.rs index 3d71b83af..e75a2389d 100644 --- a/support/procedural-fork/src/pallet/expand/warnings.rs +++ b/support/procedural-fork/src/pallet/expand/warnings.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/mod.rs b/support/procedural-fork/src/pallet/mod.rs index d3796662f..21f08fc79 100644 --- a/support/procedural-fork/src/pallet/mod.rs +++ b/support/procedural-fork/src/pallet/mod.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/call.rs b/support/procedural-fork/src/pallet/parse/call.rs index 865c63473..cbe509c44 100644 --- a/support/procedural-fork/src/pallet/parse/call.rs +++ b/support/procedural-fork/src/pallet/parse/call.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/composite.rs b/support/procedural-fork/src/pallet/parse/composite.rs index 239b4fd4b..f971fd234 100644 --- a/support/procedural-fork/src/pallet/parse/composite.rs +++ b/support/procedural-fork/src/pallet/parse/composite.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/config.rs b/support/procedural-fork/src/pallet/parse/config.rs index 95b4143b6..0f9e388c2 100644 --- a/support/procedural-fork/src/pallet/parse/config.rs +++ b/support/procedural-fork/src/pallet/parse/config.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/error.rs b/support/procedural-fork/src/pallet/parse/error.rs index 7aab5732b..37840bfba 100644 --- a/support/procedural-fork/src/pallet/parse/error.rs +++ b/support/procedural-fork/src/pallet/parse/error.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/event.rs b/support/procedural-fork/src/pallet/parse/event.rs index 6102dd31f..280440cde 100644 --- a/support/procedural-fork/src/pallet/parse/event.rs +++ b/support/procedural-fork/src/pallet/parse/event.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/extra_constants.rs b/support/procedural-fork/src/pallet/parse/extra_constants.rs index 431fcf677..5457565bf 100644 --- a/support/procedural-fork/src/pallet/parse/extra_constants.rs +++ b/support/procedural-fork/src/pallet/parse/extra_constants.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/genesis_build.rs b/support/procedural-fork/src/pallet/parse/genesis_build.rs index 936c929af..7e0999d40 100644 --- a/support/procedural-fork/src/pallet/parse/genesis_build.rs +++ b/support/procedural-fork/src/pallet/parse/genesis_build.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/genesis_config.rs b/support/procedural-fork/src/pallet/parse/genesis_config.rs index 1c52345eb..90defee72 100644 --- a/support/procedural-fork/src/pallet/parse/genesis_config.rs +++ b/support/procedural-fork/src/pallet/parse/genesis_config.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/helper.rs b/support/procedural-fork/src/pallet/parse/helper.rs index 1105046c2..061e786b0 100644 --- a/support/procedural-fork/src/pallet/parse/helper.rs +++ b/support/procedural-fork/src/pallet/parse/helper.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/hooks.rs b/support/procedural-fork/src/pallet/parse/hooks.rs index bca1a3383..b625ad724 100644 --- a/support/procedural-fork/src/pallet/parse/hooks.rs +++ b/support/procedural-fork/src/pallet/parse/hooks.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/inherent.rs b/support/procedural-fork/src/pallet/parse/inherent.rs index 911de2ffe..f05eb71e9 100644 --- a/support/procedural-fork/src/pallet/parse/inherent.rs +++ b/support/procedural-fork/src/pallet/parse/inherent.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/mod.rs b/support/procedural-fork/src/pallet/parse/mod.rs index 69f921733..38384dcfd 100644 --- a/support/procedural-fork/src/pallet/parse/mod.rs +++ b/support/procedural-fork/src/pallet/parse/mod.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. @@ -37,7 +38,6 @@ pub mod tasks; pub mod type_value; pub mod validate_unsigned; -#[cfg(test)] pub mod tests; use composite::{keyword::CompositeKeyword, CompositeDef}; diff --git a/support/procedural-fork/src/pallet/parse/origin.rs b/support/procedural-fork/src/pallet/parse/origin.rs index 8232719d6..2be9db85e 100644 --- a/support/procedural-fork/src/pallet/parse/origin.rs +++ b/support/procedural-fork/src/pallet/parse/origin.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/pallet_struct.rs b/support/procedural-fork/src/pallet/parse/pallet_struct.rs index 320cf01fa..389d3eccf 100644 --- a/support/procedural-fork/src/pallet/parse/pallet_struct.rs +++ b/support/procedural-fork/src/pallet/parse/pallet_struct.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/storage.rs b/support/procedural-fork/src/pallet/parse/storage.rs index 64a5e685b..dcc88f629 100644 --- a/support/procedural-fork/src/pallet/parse/storage.rs +++ b/support/procedural-fork/src/pallet/parse/storage.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. @@ -718,11 +719,11 @@ fn process_generics( "CountedStorageNMap" => StorageKind::CountedNMap, found => { let msg = format!( - "Invalid pallet::storage, expected ident: `StorageValue` or \ + "Invalid pallet::storage, expected ident: `StorageValue` or \ `StorageMap` or `CountedStorageMap` or `StorageDoubleMap` or `StorageNMap` or `CountedStorageNMap` \ in order to expand metadata, found `{}`.", - found, - ); + found, + ); return Err(syn::Error::new(segment.ident.span(), msg)); } }; diff --git a/support/procedural-fork/src/pallet/parse/tasks.rs b/support/procedural-fork/src/pallet/parse/tasks.rs index 2a8d14826..524fdc404 100644 --- a/support/procedural-fork/src/pallet/parse/tasks.rs +++ b/support/procedural-fork/src/pallet/parse/tasks.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. @@ -595,6 +596,7 @@ fn partition_task_attrs(item: &ImplItemFn) -> (Vec, Vec(quote!(#[pallet::task_list(Something::iter())])).unwrap(); parse2::(quote!(#[pallet::task_list(Numbers::::iter_keys())])).unwrap(); @@ -610,6 +612,7 @@ fn test_parse_task_list_() { } #[test] +#[ignore] fn test_parse_task_index() { parse2::(quote!(#[pallet::task_index(3)])).unwrap(); parse2::(quote!(#[pallet::task_index(0)])).unwrap(); @@ -629,6 +632,7 @@ fn test_parse_task_index() { } #[test] +#[ignore] fn test_parse_task_condition() { parse2::(quote!(#[pallet::task_condition(|x| x.is_some())])).unwrap(); parse2::(quote!(#[pallet::task_condition(|_x| some_expr())])).unwrap(); @@ -637,6 +641,7 @@ fn test_parse_task_condition() { } #[test] +#[ignore] fn test_parse_tasks_attr() { parse2::(quote!(#[pallet::tasks_experimental])).unwrap(); assert_parse_error_matches!( @@ -658,6 +663,7 @@ fn test_parse_tasks_attr() { } #[test] +#[ignore] fn test_parse_tasks_def_basic() { simulate_manifest_dir("../../examples/basic", || { let parsed = parse2::(quote! { @@ -684,6 +690,7 @@ fn test_parse_tasks_def_basic() { } #[test] +#[ignore] fn test_parse_tasks_def_basic_increment_decrement() { simulate_manifest_dir("../../examples/basic", || { let parsed = parse2::(quote! { @@ -736,6 +743,7 @@ fn test_parse_tasks_def_basic_increment_decrement() { } #[test] +#[ignore] fn test_parse_tasks_def_duplicate_index() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( @@ -765,6 +773,7 @@ fn test_parse_tasks_def_duplicate_index() { } #[test] +#[ignore] fn test_parse_tasks_def_missing_task_list() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( @@ -784,6 +793,7 @@ fn test_parse_tasks_def_missing_task_list() { } #[test] +#[ignore] fn test_parse_tasks_def_missing_task_condition() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( @@ -803,6 +813,7 @@ fn test_parse_tasks_def_missing_task_condition() { } #[test] +#[ignore] fn test_parse_tasks_def_missing_task_index() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( @@ -822,6 +833,7 @@ fn test_parse_tasks_def_missing_task_index() { } #[test] +#[ignore] fn test_parse_tasks_def_missing_task_weight() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( @@ -842,6 +854,7 @@ fn test_parse_tasks_def_missing_task_weight() { } #[test] +#[ignore] fn test_parse_tasks_def_unexpected_extra_task_list_attr() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( @@ -864,6 +877,7 @@ fn test_parse_tasks_def_unexpected_extra_task_list_attr() { } #[test] +#[ignore] fn test_parse_tasks_def_unexpected_extra_task_condition_attr() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( @@ -886,6 +900,7 @@ fn test_parse_tasks_def_unexpected_extra_task_condition_attr() { } #[test] +#[ignore] fn test_parse_tasks_def_unexpected_extra_task_index_attr() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( @@ -908,6 +923,7 @@ fn test_parse_tasks_def_unexpected_extra_task_index_attr() { } #[test] +#[ignore] fn test_parse_tasks_def_extra_tasks_attribute() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( @@ -922,6 +938,7 @@ fn test_parse_tasks_def_extra_tasks_attribute() { } #[test] +#[ignore] fn test_parse_task_enum_def_basic() { simulate_manifest_dir("../../examples/basic", || { parse2::(quote! { @@ -936,6 +953,7 @@ fn test_parse_task_enum_def_basic() { } #[test] +#[ignore] fn test_parse_task_enum_def_non_task_name() { simulate_manifest_dir("../../examples/basic", || { parse2::(quote! { @@ -949,6 +967,7 @@ fn test_parse_task_enum_def_non_task_name() { } #[test] +#[ignore] fn test_parse_task_enum_def_missing_attr_allowed() { simulate_manifest_dir("../../examples/basic", || { parse2::(quote! { @@ -962,6 +981,7 @@ fn test_parse_task_enum_def_missing_attr_allowed() { } #[test] +#[ignore] fn test_parse_task_enum_def_missing_attr_alternate_name_allowed() { simulate_manifest_dir("../../examples/basic", || { parse2::(quote! { @@ -974,6 +994,7 @@ fn test_parse_task_enum_def_missing_attr_alternate_name_allowed() { } #[test] +#[ignore] fn test_parse_task_enum_def_wrong_attr() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( @@ -990,6 +1011,7 @@ fn test_parse_task_enum_def_wrong_attr() { } #[test] +#[ignore] fn test_parse_task_enum_def_wrong_item() { simulate_manifest_dir("../../examples/basic", || { assert_parse_error_matches!( diff --git a/support/procedural-fork/src/pallet/parse/tests/mod.rs b/support/procedural-fork/src/pallet/parse/tests/mod.rs index fd7dc2dbe..631afcb0a 100644 --- a/support/procedural-fork/src/pallet/parse/tests/mod.rs +++ b/support/procedural-fork/src/pallet/parse/tests/mod.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. @@ -223,6 +224,7 @@ pub fn simulate_manifest_dir, F: FnOnce() + std::panic mod tasks; #[test] +#[ignore] fn test_parse_minimal_pallet() { assert_pallet_parses! { #[manifest_dir("../../examples/basic")] @@ -238,6 +240,7 @@ fn test_parse_minimal_pallet() { } #[test] +#[ignore] fn test_parse_pallet_missing_pallet() { assert_pallet_parse_error! { #[manifest_dir("../../examples/basic")] @@ -251,6 +254,7 @@ fn test_parse_pallet_missing_pallet() { } #[test] +#[ignore] fn test_parse_pallet_missing_config() { assert_pallet_parse_error! { #[manifest_dir("../../examples/basic")] diff --git a/support/procedural-fork/src/pallet/parse/tests/tasks.rs b/support/procedural-fork/src/pallet/parse/tests/tasks.rs index 0097ed047..0694eb3ff 100644 --- a/support/procedural-fork/src/pallet/parse/tests/tasks.rs +++ b/support/procedural-fork/src/pallet/parse/tests/tasks.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. @@ -18,6 +19,7 @@ use syn::parse_quote; #[test] +#[ignore] fn test_parse_pallet_with_task_enum_missing_impl() { assert_pallet_parse_error! { #[manifest_dir("../../examples/basic")] @@ -39,6 +41,7 @@ fn test_parse_pallet_with_task_enum_missing_impl() { } #[test] +#[ignore] fn test_parse_pallet_with_task_enum_wrong_attribute() { assert_pallet_parse_error! { #[manifest_dir("../../examples/basic")] @@ -66,6 +69,7 @@ fn test_parse_pallet_with_task_enum_wrong_attribute() { } #[test] +#[ignore] fn test_parse_pallet_missing_task_enum() { assert_pallet_parses! { #[manifest_dir("../../examples/basic")] @@ -88,6 +92,7 @@ fn test_parse_pallet_missing_task_enum() { } #[test] +#[ignore] fn test_parse_pallet_task_list_in_wrong_place() { assert_pallet_parse_error! { #[manifest_dir("../../examples/basic")] @@ -113,6 +118,7 @@ fn test_parse_pallet_task_list_in_wrong_place() { } #[test] +#[ignore] fn test_parse_pallet_manual_tasks_impl_without_manual_tasks_enum() { assert_pallet_parse_error! { #[manifest_dir("../../examples/basic")] @@ -141,6 +147,7 @@ fn test_parse_pallet_manual_tasks_impl_without_manual_tasks_enum() { } #[test] +#[ignore] fn test_parse_pallet_manual_task_enum_non_manual_impl() { assert_pallet_parses! { #[manifest_dir("../../examples/basic")] @@ -166,6 +173,7 @@ fn test_parse_pallet_manual_task_enum_non_manual_impl() { } #[test] +#[ignore] fn test_parse_pallet_non_manual_task_enum_manual_impl() { assert_pallet_parses! { #[manifest_dir("../../examples/basic")] @@ -191,6 +199,7 @@ fn test_parse_pallet_non_manual_task_enum_manual_impl() { } #[test] +#[ignore] fn test_parse_pallet_manual_task_enum_manual_impl() { assert_pallet_parses! { #[manifest_dir("../../examples/basic")] @@ -215,6 +224,7 @@ fn test_parse_pallet_manual_task_enum_manual_impl() { } #[test] +#[ignore] fn test_parse_pallet_manual_task_enum_mismatch_ident() { assert_pallet_parses! { #[manifest_dir("../../examples/basic")] diff --git a/support/procedural-fork/src/pallet/parse/type_value.rs b/support/procedural-fork/src/pallet/parse/type_value.rs index 1054fd74c..9a086fe56 100644 --- a/support/procedural-fork/src/pallet/parse/type_value.rs +++ b/support/procedural-fork/src/pallet/parse/type_value.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet/parse/validate_unsigned.rs b/support/procedural-fork/src/pallet/parse/validate_unsigned.rs index 3fcbe09e8..3c8b3885d 100644 --- a/support/procedural-fork/src/pallet/parse/validate_unsigned.rs +++ b/support/procedural-fork/src/pallet/parse/validate_unsigned.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/pallet_error.rs b/support/procedural-fork/src/pallet_error.rs index bdf8330cd..4c58d064e 100644 --- a/support/procedural-fork/src/pallet_error.rs +++ b/support/procedural-fork/src/pallet_error.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/runtime/expand/mod.rs b/support/procedural-fork/src/runtime/expand/mod.rs index a1a6d4d07..d70c054db 100644 --- a/support/procedural-fork/src/runtime/expand/mod.rs +++ b/support/procedural-fork/src/runtime/expand/mod.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/runtime/mod.rs b/support/procedural-fork/src/runtime/mod.rs index a96b21cd1..20b06b61c 100644 --- a/support/procedural-fork/src/runtime/mod.rs +++ b/support/procedural-fork/src/runtime/mod.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/runtime/parse/helper.rs b/support/procedural-fork/src/runtime/parse/helper.rs index 17e362410..da5ae2761 100644 --- a/support/procedural-fork/src/runtime/parse/helper.rs +++ b/support/procedural-fork/src/runtime/parse/helper.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/runtime/parse/mod.rs b/support/procedural-fork/src/runtime/parse/mod.rs index 494ab2c53..85b23ba9c 100644 --- a/support/procedural-fork/src/runtime/parse/mod.rs +++ b/support/procedural-fork/src/runtime/parse/mod.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. @@ -260,14 +261,14 @@ impl Def { syn::Error::new(item_span, "Missing Runtime. Please add a struct inside the module and annotate it with `#[runtime::runtime]`" ) - })?, - pallets, - runtime_types: runtime_types.ok_or_else(|| { - syn::Error::new(item_span, + })?, + pallets, + runtime_types: runtime_types.ok_or_else(|| { + syn::Error::new(item_span, "Missing Runtime Types. Please annotate the runtime struct with `#[runtime::derive]`" ) - })?, - }; + })?, + }; Ok(def) } diff --git a/support/procedural-fork/src/runtime/parse/pallet.rs b/support/procedural-fork/src/runtime/parse/pallet.rs index 591c05930..576873434 100644 --- a/support/procedural-fork/src/runtime/parse/pallet.rs +++ b/support/procedural-fork/src/runtime/parse/pallet.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/runtime/parse/pallet_decl.rs b/support/procedural-fork/src/runtime/parse/pallet_decl.rs index fab826eee..0b6fc65b5 100644 --- a/support/procedural-fork/src/runtime/parse/pallet_decl.rs +++ b/support/procedural-fork/src/runtime/parse/pallet_decl.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/runtime/parse/runtime_struct.rs b/support/procedural-fork/src/runtime/parse/runtime_struct.rs index 82c6470d7..2788d2d9e 100644 --- a/support/procedural-fork/src/runtime/parse/runtime_struct.rs +++ b/support/procedural-fork/src/runtime/parse/runtime_struct.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/runtime/parse/runtime_types.rs b/support/procedural-fork/src/runtime/parse/runtime_types.rs index 4d8c8358c..da25cdeed 100644 --- a/support/procedural-fork/src/runtime/parse/runtime_types.rs +++ b/support/procedural-fork/src/runtime/parse/runtime_types.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/storage_alias.rs b/support/procedural-fork/src/storage_alias.rs index 7099239f9..be2dd6ac2 100644 --- a/support/procedural-fork/src/storage_alias.rs +++ b/support/procedural-fork/src/storage_alias.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/transactional.rs b/support/procedural-fork/src/transactional.rs index 73a841d9b..d034ef132 100644 --- a/support/procedural-fork/src/transactional.rs +++ b/support/procedural-fork/src/transactional.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/src/tt_macro.rs b/support/procedural-fork/src/tt_macro.rs index 3f280013f..651828a06 100644 --- a/support/procedural-fork/src/tt_macro.rs +++ b/support/procedural-fork/src/tt_macro.rs @@ -1,3 +1,4 @@ +#![cfg(not(doc))] // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. diff --git a/support/procedural-fork/update.sh b/support/procedural-fork/update.sh index 0cb933b33..ed21c2db3 100755 --- a/support/procedural-fork/update.sh +++ b/support/procedural-fork/update.sh @@ -13,6 +13,7 @@ TMP_DIR=$(mktemp -d) # Define source and destination directories SRC_DIR="substrate/frame/support/procedural/src" DEST_DIR="$(pwd)/src" # Absolute path to `src` directory of procedural-fork +PARENT_DIR="$(dirname "$DEST_DIR")" # Get the parent directory of DEST_DIR # Check if DEST_DIR exists if [ ! -d "$DEST_DIR" ]; then @@ -36,11 +37,35 @@ git sparse-checkout set "$SRC_DIR" echo "Contents of $TMP_DIR/$SRC_DIR after sparse-checkout:" ls -l "$TMP_DIR/$SRC_DIR" || { echo "Error: Sparse checkout failed, $SRC_DIR not found."; rm -rf "$TMP_DIR"; exit 1; } -# Copy all files from `src` except `lib.rs` to the destination folder +# Copy all files from `src` except `$DEST_DIR/lib.rs` to the destination folder echo "Copying files to $DEST_DIR ..." rsync -a --exclude='lib.rs' "$TMP_DIR/$SRC_DIR/" "$DEST_DIR/" +# Prepend only `#![cfg(not(doc))]` to the top of each Rust file, except the `lib.rs` in $DEST_DIR +find "$DEST_DIR" -name '*.rs' -not -path "$DEST_DIR/lib.rs" | while read -r file; do + echo "Prepending configuration to $file ..." + # Use awk to prepend only `#![cfg(not(doc))]` + awk 'BEGIN {print "#![cfg(not(doc))]"} {print}' "$file" > "$file.tmp" && mv "$file.tmp" "$file" +done + +# Remove all `#[cfg(test)]` lines from `pallet/parse/mod.rs` +MOD_RS="$DEST_DIR/pallet/parse/mod.rs" +if [ -f "$MOD_RS" ]; then + echo "Removing #[cfg(test)] from $MOD_RS ..." + grep -v '#\[cfg(test)\]' "$MOD_RS" > "$MOD_RS.tmp" && mv "$MOD_RS.tmp" "$MOD_RS" +fi + +# Replace all #[test] with #[test]\n#[ignore] using awk +echo "Replacing #[test] with #[ignore] ..." +find "$DEST_DIR" -name '*.rs' | while read -r file; do + awk '{if ($0 == "#[test]") print $0 "\n#[ignore]"; else print $0}' "$file" > "$file.tmp" && mv "$file.tmp" "$file" +done + +# Change directory to the parent of $DEST_DIR to run cargo fmt +echo "Changing directory to $PARENT_DIR and running cargo fmt --all ..." +cd "$PARENT_DIR" && cargo fmt --all + # Clean up the temporary directory rm -rf "$TMP_DIR" -echo "Update completed successfully." +echo "Update and formatting completed successfully."