diff --git a/.github/workflows/060_generate_benchmark_weights.yml b/.github/workflows/060_generate_benchmark_weights.yml new file mode 100644 index 000000000..872ae8712 --- /dev/null +++ b/.github/workflows/060_generate_benchmark_weights.yml @@ -0,0 +1,74 @@ +name: Generate benchmark weights + +on: + workflow_dispatch: + +jobs: + generate-benchmark-weights: + runs-on: [self-hosted, tfchainrunner01] + container: + image: threefolddev/tfchain:4 + env: + DEBIAN_FRONTEND: noninteractive + PATH: /root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin + RUSTUP_HOME: /root/.rustup + CARGO_HOME: /root/.cargo + + steps: + - name: Fail if branch is main + if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/development' + run: | + echo "This workflow should not be triggered with workflow_dispatch on main branch" + exit 1 + + - name: Checkout the repo + uses: actions/checkout@v3 + + - name: Chown user + run: | + chown -R $USER:$USER $GITHUB_WORKSPACE + + - name: Cache build + uses: actions/cache@v3 + timeout-minutes: 6 + continue-on-error: true + with: + path: | + /root/.cargo/bin/ + /root/.cargo/registry/index/ + /root/.cargo/registry/cache/ + /root/.cargo/git/db/ + substrate-node/target/ + key: ${{ runner.os }}-tfchain-build-cache-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-tfchain-build-cache + + - name: Build + run: | + cd substrate-node + cargo build --profile=production --features runtime-benchmarks + + - name: Run benchmarking + run: | + cd substrate-node + for weights_rs_file in ./pallets/*/src/weights.rs + do + rm $weights_rs_file + pal_name=$(awk -F'pallets/|/src' '{print $2}' <<< $weights_rs_file) + ./target/production/tfchain benchmark pallet \ + --chain=dev \ + --wasm-execution=compiled \ + --pallet="$pal_name" \ + --extrinsic="*" \ + --steps=50 \ + --repeat=20 \ + --heap-pages=409 \ + --output ./pallets/"$pal_name"/src/weights.rs \ + --template ./.maintain/frame-weight-template.hbs + done + + - name: Commit & Push changes + uses: actions-js/push@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + message: 'chore(pallets): update benchmark `weights.rs` files ${date}' + branch: ${{ github.ref_name }} diff --git a/.github/workflows/080_check_benchmark_weights.yml b/.github/workflows/080_check_benchmark_weights.yml new file mode 100644 index 000000000..06ba6edf4 --- /dev/null +++ b/.github/workflows/080_check_benchmark_weights.yml @@ -0,0 +1,77 @@ +name: Check benchmark weights + +on: + push: + branches-ignore: + - development + paths: + - '**.rs' + - 'substrate-node/pallets/**' + +jobs: + check-benchmark-weights: + runs-on: ubuntu-22.04 + + steps: + - name: Checkout the repo + uses: actions/checkout@v3 + + - name: Get all rust files that have changed in pallets + id: pallets-changed-rust-files + uses: tj-actions/changed-files@v42 + with: + files: | + substrate-node/pallets/**/src/*.rs + + - name: Get all pallets with changes in src dir + id: pallets-changed-src-dir + uses: tj-actions/changed-files@v42 + with: + dir_names: "true" + files: | + substrate-node/pallets/**/src/*.rs + + - name: List all changed files + env: + ALL_CHANGED_FILES: ${{ steps.pallets-changed-rust-files.outputs.all_changed_files }} + run: | + for file in ${ALL_CHANGED_FILES}; do + echo "$file file was changed" + done + + - name: List all changed dir + env: + ALL_CHANGED_DIR: ${{ steps.pallets-changed-src-dir.outputs.all_changed_files }} + run: | + for dir in ${ALL_CHANGED_DIR}; do + echo "$dir has changes" + done + + - name: Run benchmarking + env: + ALL_CHANGED_PALLETS_SRC_DIR: ${{ steps.pallets-changed-src-dir.outputs.all_changed_files }} + ALL_CHANGED_PALLETS_FILES: ${{ steps.pallets-changed-rust-files.outputs.all_changed_files }} + run: | + count=0 + for pallet_src_dir in ${ALL_CHANGED_PALLETS_SRC_DIR}; do + echo "pallet src dir: $pallet_src_dir" + weights_file="$pallet_src_dir"/weights.rs + echo "weights file: $weights_file" + updated_weights=false + for changed_file in ${ALL_CHANGED_PALLETS_FILES}; do + if [ "$changed_file" = "$weights_file" ]; then + updated_weights=true + break + fi + done + if [ "$updated_weights" = false ] ; then + let "count=count+1" + fi + done + if [ "$count" -gt 0 ]; then + echo "Found changes on src rust file(s) for $count pallet(s) and respective weights.rs file(s) was not updated." + echo "Make sure to generate these files again if pallet logic has changed by running generate_benchmark_weights workflow on branch." + exit 1 + else + echo "Found changes on src rust file(s) and respective weights.rs file(s) was updated." + fi diff --git a/substrate-node/.maintain/benchmark.sh b/substrate-node/.maintain/benchmark.sh index 73abcbca1..dda20655c 100644 --- a/substrate-node/.maintain/benchmark.sh +++ b/substrate-node/.maintain/benchmark.sh @@ -3,37 +3,37 @@ echo "starting benchmark" echo "generating pallet-burning" -cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_burning --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-burning/src/weights.rs --template ./frame-weight-template.hbs" +cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_burning --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-burning/src/weights.rs --template ./frame-weight-template.hbs" $cmd echo "generating pallet-dao" -cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_dao --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-dao/src/weights.rs --template ./frame-weight-template.hbs" +cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_dao --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-dao/src/weights.rs --template ./frame-weight-template.hbs" $cmd echo "generating pallet-kvstore" -cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_kvstore --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-kvstore/src/weights.rs --template ./frame-weight-template.hbs" +cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_kvstore --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-kvstore/src/weights.rs --template ./frame-weight-template.hbs" $cmd echo "generating pallet-smart-contract" -cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_smart_contract --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-smart-contract/src/weights.rs --template ./frame-weight-template.hbs" +cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_smart_contract --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-smart-contract/src/weights.rs --template ./frame-weight-template.hbs" $cmd echo "generating pallet-tfgrid weights" -cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tfgrid --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-tfgrid/src/weights.rs --template ./frame-weight-template.hbs" +cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tfgrid --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-tfgrid/src/weights.rs --template ./frame-weight-template.hbs" $cmd echo "generating pallet-tft-bridge" -cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tft_bridge --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-tft-bridge/src/weights.rs --template ./frame-weight-template.hbs" +cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tft_bridge --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-tft-bridge/src/weights.rs --template ./frame-weight-template.hbs" $cmd echo "generating pallet-tft-price" -cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tft_price --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-tft-price/src/weights.rs --template ./frame-weight-template.hbs" +cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_tft_price --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-tft-price/src/weights.rs --template ./frame-weight-template.hbs" $cmd echo "generating pallet-validator" -cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_validator --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/pallet-validator/src/weights.rs --template ./frame-weight-template.hbs" +cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=pallet_validator --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/pallet-validator/src/weights.rs --template ./frame-weight-template.hbs" $cmd echo "generating substrate-validator-set" -cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=validatorset --extrinsic="*" --steps=50 --repeat=20 --execution=wasm --heap-pages=409 --output ../pallets/substrate-validator-set/src/weights.rs --template ./frame-weight-template.hbs" +cmd="../target/release/tfchain benchmark pallet --chain=dev --pallet=substrate_validator_set --extrinsic="*" --steps=50 --repeat=20 --heap-pages=409 --output ../pallets/substrate-validator-set/src/weights.rs --template ./frame-weight-template.hbs" $cmd \ No newline at end of file diff --git a/substrate-node/Cargo.toml b/substrate-node/Cargo.toml index 1d5bcbc0d..cabf4190c 100644 --- a/substrate-node/Cargo.toml +++ b/substrate-node/Cargo.toml @@ -131,3 +131,13 @@ sp-timestamp = {git = "https://github.com/paritytech/substrate", branch = "polka substrate-build-script-utils = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0"} substrate-frame-rpc-system = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0"} substrate-prometheus-endpoint = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0"} + +[profile.production] +inherits = "release" + +# Sacrifice compile speed for execution speed by using optimization flags: + +# https://doc.rust-lang.org/rustc/linker-plugin-lto.html +lto = "fat" +# https://doc.rust-lang.org/rustc/codegen-options/index.html#codegen-units +codegen-units = 1 \ No newline at end of file diff --git a/substrate-node/pallets/pallet-runtime-upgrade/src/lib.rs b/substrate-node/pallets/pallet-runtime-upgrade/src/lib.rs index f18903ec6..7d29ea90b 100644 --- a/substrate-node/pallets/pallet-runtime-upgrade/src/lib.rs +++ b/substrate-node/pallets/pallet-runtime-upgrade/src/lib.rs @@ -6,6 +6,7 @@ pub use pallet::*; pub mod pallet { use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; + use frame_system::weights::WeightInfo; use sp_std::vec::Vec; #[pallet::pallet] @@ -16,13 +17,14 @@ pub mod pallet { pub trait Config: frame_system::Config { /// Origin for runtime upgrades type SetCodeOrigin: EnsureOrigin; + type WeightInfo: WeightInfo; } #[pallet::call] impl Pallet { #[pallet::call_index(0)] // Give same weight as set_code() wrapped extrinsic from frame_system - #[pallet::weight((T::BlockWeights::get().base_block, DispatchClass::Operational))] + #[pallet::weight((::WeightInfo::set_code(), DispatchClass::Operational))] pub fn set_code(origin: OriginFor, code: Vec) -> DispatchResultWithPostInfo { T::SetCodeOrigin::ensure_origin(origin)?; frame_system::Pallet::::set_code(frame_system::RawOrigin::Root.into(), code)?; diff --git a/substrate-node/runtime/src/lib.rs b/substrate-node/runtime/src/lib.rs index adcec67b0..8f7f96efa 100644 --- a/substrate-node/runtime/src/lib.rs +++ b/substrate-node/runtime/src/lib.rs @@ -673,6 +673,7 @@ type EnsureRootOrCouncilApproval = EitherOfDiverse< impl pallet_runtime_upgrade::Config for Runtime { type SetCodeOrigin = EnsureRootOrCouncilApproval; + type WeightInfo = (); } pub struct AuraAccountAdapter; @@ -805,7 +806,7 @@ mod benches { [pallet_burning, BurningModule] [pallet_dao, Dao] [pallet_kvstore, TFKVStore] - [validatorset, ValidatorSet] + [substrate_validator_set, ValidatorSet] [pallet_validator, Validator] [pallet_tft_bridge, TFTBridgeModule] // Substrate