diff --git a/.github/workflows/check-labels.yml b/.github/workflows/check-labels.yml new file mode 100644 index 0000000000000..ee03075176995 --- /dev/null +++ b/.github/workflows/check-labels.yml @@ -0,0 +1,16 @@ +name: Check labels + +on: + pull_request: + types: [labeled, opened, synchronize, unlabeled] + +jobs: + check-labels: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Check labels + run: bash ${{ github.workspace }}/.maintain/github/check_labels.sh + env: + GITHUB_PR: ${{ github.event.pull_request.number }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 69bf537be30b5..f2d8552cca78d 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ rls*.log frame/ddc-metrics-offchain-worker/src/tests/test_data/ddc.wasm frame/ddc-metrics-offchain-worker/src/tests/test_data/metadata.json .cargo-remote.toml +*.bin diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b643489d9009d..9cd755bc799be 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -39,6 +39,7 @@ variables: &default-vars CARGO_INCREMENTAL: 0 DOCKER_OS: "debian:stretch" ARCH: "x86_64" + CI_IMAGE: "paritytech/ci-linux:production" # FIXME set to release CARGO_UNLEASH_INSTALL_PARAMS: "--version 1.0.0-alpha.11" CARGO_UNLEASH_PKG_DEF: "--skip node node-* pallet-template pallet-example pallet-example-* subkey chain-spec-builder" @@ -60,7 +61,7 @@ default: interruptible: true .docker-env: &docker-env - image: paritytech/ci-linux:production + image: "${CI_IMAGE}" before_script: - rustup show - cargo --version @@ -84,9 +85,36 @@ default: - if: $CI_COMMIT_REF_NAME =~ /^[0-9]+$/ # PRs - if: $CI_COMMIT_REF_NAME =~ /^v[0-9]+\.[0-9]+.*$/ # i.e. v1.0, v2.1rc1 +.test-refs-no-trigger: &test-refs-no-trigger + rules: + - if: $CI_PIPELINE_SOURCE == "pipeline" + when: never + - if: $CI_PIPELINE_SOURCE == "web" + - if: $CI_PIPELINE_SOURCE == "schedule" + - if: $CI_COMMIT_REF_NAME == "master" + - if: $CI_COMMIT_REF_NAME == "tags" + - if: $CI_COMMIT_REF_NAME =~ /^[0-9]+$/ # PRs + - if: $CI_COMMIT_REF_NAME =~ /^v[0-9]+\.[0-9]+.*$/ # i.e. v1.0, v2.1rc1 + .build-refs: &build-refs rules: + # .publish-refs with manual on PRs + - if: $CI_PIPELINE_SOURCE == "pipeline" + when: never + - if: $CI_PIPELINE_SOURCE == "web" + - if: $CI_COMMIT_REF_NAME == "master" + - if: $CI_COMMIT_REF_NAME == "tags" + - if: $CI_COMMIT_REF_NAME =~ /^v[0-9]+\.[0-9]+.*$/ # i.e. v1.0, v2.1rc1 + - if: $CI_COMMIT_REF_NAME =~ /^[0-9]+$/ # PRs + when: manual + allow_failure: true + +.publish-refs: &publish-refs + rules: + - if: $CI_PIPELINE_SOURCE == "pipeline" + when: never - if: $CI_PIPELINE_SOURCE == "web" + - if: $CI_PIPELINE_SOURCE == "schedule" - if: $CI_COMMIT_REF_NAME == "master" - if: $CI_COMMIT_REF_NAME == "tags" - if: $CI_COMMIT_REF_NAME =~ /^v[0-9]+\.[0-9]+.*$/ # i.e. v1.0, v2.1rc1 @@ -145,6 +173,8 @@ test-dependency-rules: stage: check image: paritytech/tools:latest <<: *kubernetes-build + rules: + - if: $CI_COMMIT_REF_NAME =~ /^[0-9]+$/ # PRs script: - .maintain/ensure-deps.sh @@ -153,36 +183,28 @@ test-prometheus-alerting-rules: image: paritytech/tools:latest <<: *kubernetes-build rules: + - if: $CI_PIPELINE_SOURCE == "pipeline" + when: never - if: $CI_COMMIT_BRANCH changes: - .gitlab-ci.yml - .maintain/monitoring/**/* script: - promtool check rules .maintain/monitoring/alerting-rules/alerting-rules.yaml - - cat .maintain/monitoring/alerting-rules/alerting-rules.yaml | promtool test rules .maintain/monitoring/alerting-rules/alerting-rule-tests.yaml + - cat .maintain/monitoring/alerting-rules/alerting-rules.yaml | + promtool test rules .maintain/monitoring/alerting-rules/alerting-rule-tests.yaml #### stage: test -cargo-audit: - stage: test - <<: *docker-env - rules: - - if: $CI_COMMIT_REF_NAME =~ /^[0-9]+$/ # PRs - when: never - - if: $CI_PIPELINE_SOURCE == "web" - - if: $CI_PIPELINE_SOURCE == "schedule" - - if: $CI_COMMIT_REF_NAME == "master" - - if: $CI_COMMIT_REF_NAME =~ /^v[0-9]+\.[0-9]+.*$/ # i.e. v1.0, v2.1rc1 - script: - - cargo audit - allow_failure: true - cargo-deny: stage: test <<: *docker-env rules: - - if: $CI_COMMIT_MESSAGE =~ /skip-checks/ + - if: $CI_PIPELINE_SOURCE == "pipeline" when: never + - changes: + - "Cargo.lock" + - "**/Cargo.toml" - if: $CI_PIPELINE_SOURCE == "web" - if: $CI_PIPELINE_SOURCE == "schedule" - if: $CI_COMMIT_REF_NAME == "master" @@ -199,11 +221,13 @@ cargo-deny: when: always paths: - deny.log + # FIXME: Temorarily allow to fail. + allow_failure: true cargo-check-benches: stage: test <<: *docker-env - <<: *test-refs + <<: *test-refs-no-trigger script: - SKIP_WASM_BUILD=1 time cargo +nightly check --benches --all - cargo run --release -p node-bench -- ::node::import::native::sr25519::transfer_keep_alive::paritydb::small @@ -260,9 +284,8 @@ unleash-check: stage: test <<: *docker-env rules: - - if: $CI_COMMIT_MESSAGE =~ /skip-checks/ + - if: $CI_PIPELINE_SOURCE == "pipeline" when: never - # .test-refs - if: $CI_COMMIT_REF_NAME == "master" - if: $CI_COMMIT_REF_NAME == "tags" - if: $CI_COMMIT_REF_NAME =~ /^v[0-9]+\.[0-9]+.*$/ # i.e. v1.0, v2.1rc1 @@ -279,7 +302,7 @@ test-frame-examples-compile-to-wasm: <<: *default-vars # Enable debug assertions since we are running optimized builds for testing # but still want to have debug assertions. - RUSTFLAGS: -Cdebug-assertions=y + RUSTFLAGS: "-Cdebug-assertions=y" RUST_BACKTRACE: 1 script: - cd frame/example-offchain-worker/ @@ -331,7 +354,7 @@ test-full-crypto-feature: <<: *default-vars # Enable debug assertions since we are running optimized builds for testing # but still want to have debug assertions. - RUSTFLAGS: -Cdebug-assertions=y + RUSTFLAGS: "-Cdebug-assertions=y" RUST_BACKTRACE: 1 script: - cd primitives/core/ @@ -344,7 +367,7 @@ cargo-check-macos: stage: test # shell runner on mac ignores the image set in *docker-env <<: *docker-env - <<: *test-refs + <<: *test-refs-no-trigger script: - SKIP_WASM_BUILD=1 time cargo check --release - sccache -s @@ -365,7 +388,7 @@ check-polkadot-companion-status: check-polkadot-companion-build: stage: build <<: *docker-env - <<: *test-refs + <<: *test-refs-no-trigger needs: - job: test-linux-stable-int artifacts: false @@ -394,15 +417,7 @@ build-linux-substrate: &build-binary stage: build <<: *collect-artifacts <<: *docker-env - rules: - # .build-refs with manual on PRs - - if: $CI_PIPELINE_SOURCE == "web" - - if: $CI_COMMIT_REF_NAME == "master" - - if: $CI_COMMIT_REF_NAME == "tags" - - if: $CI_COMMIT_REF_NAME =~ /^v[0-9]+\.[0-9]+.*$/ # i.e. v1.0, v2.1rc1 - - if: $CI_COMMIT_REF_NAME =~ /^[0-9]+$/ # PRs - when: manual - allow_failure: true + <<: *build-refs needs: - job: test-linux-stable artifacts: false @@ -429,15 +444,7 @@ build-linux-subkey: &build-subkey stage: build <<: *collect-artifacts <<: *docker-env - rules: - # .build-refs with manual on PRs - - if: $CI_PIPELINE_SOURCE == "web" - - if: $CI_COMMIT_REF_NAME == "master" - - if: $CI_COMMIT_REF_NAME == "tags" - - if: $CI_COMMIT_REF_NAME =~ /^v[0-9]+\.[0-9]+.*$/ # i.e. v1.0, v2.1rc1 - - if: $CI_COMMIT_REF_NAME =~ /^[0-9]+$/ # PRs - when: manual - allow_failure: true + <<: *build-refs needs: - job: cargo-check-subkey artifacts: false @@ -461,34 +468,10 @@ build-macos-subkey: tags: - osx -build-rust-doc: - stage: build - <<: *docker-env - <<: *test-refs - needs: - - job: test-linux-stable - artifacts: false - variables: - <<: *default-vars - RUSTFLAGS: -Dwarnings - artifacts: - name: "${CI_JOB_NAME}_${CI_COMMIT_REF_NAME}-doc" - when: on_success - expire_in: 7 days - paths: - - ./crate-docs/ - script: - - rm -f ./crate-docs/index.html # use it as an indicator if the job succeeds - - SKIP_WASM_BUILD=1 RUSTDOCFLAGS="--html-in-header $(pwd)/.maintain/rustdoc-header.html" - time cargo +nightly doc --no-deps --workspace --all-features --verbose - - mv ./target/doc ./crate-docs - - echo "" > ./crate-docs/index.html - - sccache -s - #### stage: publish .build-push-docker-image: &build-push-docker-image - <<: *build-refs + <<: *publish-refs <<: *kubernetes-build image: quay.io/buildah/stable variables: &docker-build-vars @@ -521,8 +504,6 @@ build-rust-doc: publish-docker-substrate: stage: publish <<: *build-push-docker-image - # collect VERSION artifact here to pass it on to kubernetes - <<: *collect-artifacts needs: - job: build-linux-substrate artifacts: true @@ -530,8 +511,12 @@ publish-docker-substrate: <<: *docker-build-vars PRODUCT: substrate after_script: - # only VERSION information is needed for the deployment - - find ./artifacts/ -depth -not -name VERSION -type f -delete + - echo "VERSION=${VERSION}" >> build.env + artifacts: + reports: + # this artifact is used in trigger-simnet job + # https://docs.gitlab.com/ee/ci/multi_project_pipelines.html#with-variable-inheritance + dotenv: artifacts/substrate/build.env publish-docker-subkey: stage: publish @@ -545,7 +530,7 @@ publish-docker-subkey: publish-s3-release: stage: publish - <<: *build-refs + <<: *publish-refs <<: *kubernetes-build needs: - job: build-linux-substrate @@ -565,32 +550,6 @@ publish-s3-release: - aws s3 ls s3://${BUCKET}/${PREFIX}/latest/ --recursive --human-readable --summarize -publish-s3-doc: - stage: publish - image: paritytech/awscli:latest - allow_failure: true - needs: - - job: build-rust-doc - artifacts: true - - job: build-linux-substrate - artifacts: false - <<: *build-refs - <<: *kubernetes-build - variables: - GIT_STRATEGY: none - BUCKET: "releases.parity.io" - PREFIX: "substrate-rustdoc" - script: - - test -r ./crate-docs/index.html || ( - echo "./crate-docs/index.html not present, build:rust:doc:release job not complete"; - exit 1 - ) - - aws s3 sync --delete --size-only --only-show-errors - ./crate-docs/ s3://${BUCKET}/${PREFIX}/ - after_script: - - aws s3 ls s3://${BUCKET}/${PREFIX}/ - --human-readable --summarize - publish-draft-release: stage: publish image: paritytech/tools:latest @@ -635,6 +594,8 @@ deploy-prometheus-alerting-rules: - kubectl -n ${NAMESPACE} patch prometheusrule ${PROMETHEUSRULE} --type=merge --patch "$(sed 's/^/ /;1s/^/spec:\n/' ${RULES})" rules: + - if: $CI_PIPELINE_SOURCE == "pipeline" + when: never - if: $CI_COMMIT_REF_NAME == "master" changes: - .gitlab-ci.yml @@ -647,19 +608,7 @@ trigger-simnet: - if: $CI_COMMIT_REF_NAME == "master" && $CI_PIPELINE_SOURCE == "schedule" && $PIPELINE == "nightly" needs: - job: publish-docker-substrate - artifacts: false trigger: project: parity/simnet branch: master strategy: depend - -#### stage: .post - -check-labels: - stage: .post - image: paritytech/tools:latest - <<: *kubernetes-build - rules: - - if: $CI_COMMIT_REF_NAME =~ /^[0-9]+$/ # PRs - script: - - ./.maintain/gitlab/check_labels.sh diff --git a/.maintain/gitlab/lib.sh b/.maintain/common/lib.sh similarity index 89% rename from .maintain/gitlab/lib.sh rename to .maintain/common/lib.sh index 33477b52f5891..1d4be0ecc7296 100755 --- a/.maintain/gitlab/lib.sh +++ b/.maintain/common/lib.sh @@ -66,11 +66,17 @@ has_label(){ repo="$1" pr_id="$2" label="$3" + + # These will exist if the function is called in Gitlab. + # If the function's called in Github, we should have GITHUB_ACCESS_TOKEN set + # already. if [ -n "$GITHUB_RELEASE_TOKEN" ]; then - out=$(curl -H "Authorization: token $GITHUB_RELEASE_TOKEN" -s "$api_base/$repo/pulls/$pr_id") - else - out=$(curl -H "Authorization: token $GITHUB_PR_TOKEN" -s "$api_base/$repo/pulls/$pr_id") + GITHUB_TOKEN="$GITHUB_RELEASE_TOKEN" + elif [ -n "$GITHUB_PR_TOKEN" ]; then + GITHUB_TOKEN="$GITHUB_PR_TOKEN" fi + + out=$(curl -H "Authorization: token $GITHUB_TOKEN" -s "$api_base/$repo/pulls/$pr_id") [ -n "$(echo "$out" | tr -d '\r\n' | jq ".labels | .[] | select(.name==\"$label\")")" ] } diff --git a/.maintain/gitlab/check_labels.sh b/.maintain/github/check_labels.sh similarity index 76% rename from .maintain/gitlab/check_labels.sh rename to .maintain/github/check_labels.sh index 5ab099b38291c..75190db6683fa 100755 --- a/.maintain/gitlab/check_labels.sh +++ b/.maintain/github/check_labels.sh @@ -1,11 +1,14 @@ #!/usr/bin/env bash -#shellcheck source=lib.sh -source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/lib.sh" +#shellcheck source=../common/lib.sh +source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../common/lib.sh" + +repo="$GITHUB_REPOSITORY" +pr="$GITHUB_PR" ensure_labels() { for label in "$@"; do - if has_label 'paritytech/substrate' "$CI_COMMIT_BRANCH" "$label"; then + if has_label "$repo" "$pr" "$label"; then return 0 fi done @@ -27,7 +30,7 @@ criticality_labels=( 'C9-critical' ) -echo "[+] Checking release notes (B) labels for $CI_COMMIT_BRANCH" +echo "[+] Checking release notes (B) labels" if ensure_labels "${releasenotes_labels[@]}"; then echo "[+] Release notes label detected. All is well." else @@ -35,7 +38,7 @@ else exit 1 fi -echo "[+] Checking release criticality (C) labels for $CI_COMMIT_BRANCH" +echo "[+] Checking release criticality (C) labels" if ensure_labels "${criticality_labels[@]}"; then echo "[+] Release criticality label detected. All is well." else diff --git a/.maintain/gitlab/check_polkadot_companion_build.sh b/.maintain/gitlab/check_polkadot_companion_build.sh index 90354f809d4ac..bf8fbf5aaf410 100755 --- a/.maintain/gitlab/check_polkadot_companion_build.sh +++ b/.maintain/gitlab/check_polkadot_companion_build.sh @@ -85,9 +85,8 @@ else boldprint "this is not a pull request - building polkadot:master" fi -cd .. -diener --substrate --branch $CI_COMMIT_REF_NAME --git https://gitlab.parity.io/parity/substrate.git --path polkadot -cd polkadot +# Patch all Substrate crates in Polkadot +diener patch --crates-to-patch ../ --substrate # Test Polkadot pr or master branch with this Substrate commit. cargo update -p sp-io diff --git a/.maintain/gitlab/check_signed.sh b/.maintain/gitlab/check_signed.sh index 7c4cc47baba38..20d47c2304767 100755 --- a/.maintain/gitlab/check_signed.sh +++ b/.maintain/gitlab/check_signed.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash -# shellcheck source=lib.sh -source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/lib.sh" +# shellcheck source=../common/lib.sh +source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../common/lib.sh" version="$CI_COMMIT_TAG" diff --git a/.maintain/gitlab/generate_changelog.sh b/.maintain/gitlab/generate_changelog.sh index c13871f50ee49..a1190f2bf0bc6 100755 --- a/.maintain/gitlab/generate_changelog.sh +++ b/.maintain/gitlab/generate_changelog.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash -# shellcheck source=lib.sh -source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/lib.sh" +# shellcheck source=../common/lib.sh +source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../common/lib.sh" version="$2" last_version="$1" diff --git a/.maintain/gitlab/publish_draft_release.sh b/.maintain/gitlab/publish_draft_release.sh index c5813718a69f2..36ee0d63e78f9 100755 --- a/.maintain/gitlab/publish_draft_release.sh +++ b/.maintain/gitlab/publish_draft_release.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash -# shellcheck source=lib.sh -source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/lib.sh" +# shellcheck source=../common/lib.sh +source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../common/lib.sh" version="$CI_COMMIT_TAG" diff --git a/.maintain/monitoring/alerting-rules/alerting-rules.yaml b/.maintain/monitoring/alerting-rules/alerting-rules.yaml index cf00d7e2b90f1..5ee2376677677 100644 --- a/.maintain/monitoring/alerting-rules/alerting-rules.yaml +++ b/.maintain/monitoring/alerting-rules/alerting-rules.yaml @@ -108,6 +108,13 @@ groups: annotations: message: 'The node {{ $labels.instance }} has less than 3 peers for more than 15 minutes' + - alert: NoIncomingConnection + expr: increase(polkadot_sub_libp2p_incoming_connections_total[20m]) == 0 + labels: + severity: warning + annotations: + message: 'The node {{ $labels.instance }} has not received any new incoming + TCP connection in the past 20 minutes. Is it connected to the Internet?' ############################################################################## # System diff --git a/.maintain/monitoring/grafana-dashboards/README_dashboard.md b/.maintain/monitoring/grafana-dashboards/README_dashboard.md index 37bebc6f8eaae..e00b89449cfaf 100644 --- a/.maintain/monitoring/grafana-dashboards/README_dashboard.md +++ b/.maintain/monitoring/grafana-dashboards/README_dashboard.md @@ -5,10 +5,3 @@ Shared templated Grafana dashboards. To import the dashboards follow the [Grafana documentation](https://grafana.com/docs/grafana/latest/reference/export_import/). You can see an example setup [here](../../../.maintain/sentry-node). - -#### Required labels on Prometheus metrics - -- `instance` referring to a single scrape target (see [Prometheus docs for - details](https://prometheus.io/docs/concepts/jobs_instances/)). - -- `network` referring to the Blockchain network e.g. Kusama. diff --git a/.maintain/monitoring/grafana-dashboards/substrate-dashboard.json b/.maintain/monitoring/grafana-dashboards/substrate-dashboard.json deleted file mode 100644 index a61e8a49bade7..0000000000000 --- a/.maintain/monitoring/grafana-dashboards/substrate-dashboard.json +++ /dev/null @@ -1,1548 +0,0 @@ -{ - "annotations": { - "list": [ - { - "$$hashKey": "object:15", - "builtIn": 1, - "datasource": "-- Grafana --", - "enable": true, - "hide": true, - "iconColor": "rgba(0, 211, 255, 1)", - "name": "Annotations & Alerts", - "type": "dashboard" - } - ] - }, - "editable": true, - "gnetId": null, - "graphTooltip": 0, - "iteration": 1586424254170, - "links": [ - { - "icon": "external link", - "tags": [], - "targetBlank": true, - "title": "With love from ColmenaLabs", - "tooltip": "", - "type": "link", - "url": "https://colmenalabs.org" - }, - { - "icon": "external link", - "tags": [], - "targetBlank": true, - "title": "Polkastats.io", - "tooltip": "", - "type": "link", - "url": "https://polkastats.io" - } - ], - "panels": [ - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 0, - "y": 0 - }, - "hiddenSeries": false, - "id": 8, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "rate([[metric_namespace]]_block_height{status=\"finalized\",instance=\"[[instance]]\",network=\"[[network]]\"}[10m])/rate([[metric_namespace]]_block_height{status=\"finalized\",instance=\"[[instance]]\",network=\"[[network]]\"}[1m])", - "intervalFactor": 1, - "legendFormat": "rate[10m] / rate[1m]", - "refId": "A" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "Relative Block Production Speed", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 6, - "y": 0 - }, - "hiddenSeries": false, - "id": 15, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "[[metric_namespace]]_sub_libp2p_peers_count{instance=\"[[instance]]\",network=\"[[network]]\"}", - "legendFormat": "{{instance}}", - "refId": "A" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "Peers count", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "cacheTimeout": null, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 12, - "y": 0 - }, - "hiddenSeries": false, - "id": 17, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "links": [], - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pluginVersion": "6.4.1", - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "scalar([[metric_namespace]]_block_height{status=\"best\",instance=\"[[instance]]\",network=\"[[network]]\"})-scalar([[metric_namespace]]_block_height{status=\"finalized\",instance=\"[[instance]]\",network=\"[[network]]\"})", - "intervalFactor": 2, - "legendFormat": "[[hostname]]", - "refId": "A" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "Diff -> ( Best Block - Finalized )", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "cacheTimeout": null, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 18, - "y": 0 - }, - "hiddenSeries": false, - "id": 18, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "links": [], - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "rate([[metric_namespace]]_block_height{status=\"finalized\",instance=\"[[instance]]\",network=\"[[network]]\"}[10m])*60", - "intervalFactor": 10, - "legendFormat": "{{instance}} Blocks / minute", - "refId": "A" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "Block rate", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 0, - "y": 6 - }, - "hiddenSeries": false, - "id": 10, - "interval": "", - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "increase([[metric_namespace]]_block_height{instance=\"[[instance]]\",network=\"[[network]]\",status=~\"finalized|sync_target\"}[1m])", - "intervalFactor": 5, - "legendFormat": "{{status}}", - "refId": "A" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "Blocks Av per min", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 6, - "y": 6 - }, - "hiddenSeries": false, - "id": 14, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "[[metric_namespace]]_block_height{instance=\"[[instance]]\",network=\"[[network]]\",status=~\"finalized|sync_target\"}", - "legendFormat": "{{instance}} {{status}}", - "refId": "A" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "Block Finalized", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 12, - "y": 6 - }, - "hiddenSeries": false, - "id": 13, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "[[metric_namespace]]_block_height{status=\"best\",instance=\"[[instance]]\",network=\"[[network]]\"}", - "legendFormat": "{{instance}}", - "refId": "A" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "Block height", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": "Prometheus", - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 18, - "y": 6 - }, - "hiddenSeries": false, - "id": 20, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "data": "", - "expr": "[[metric_namespace]]_ready_transactions_number{instance=\"[[instance]]\",network=\"[[network]]\"}", - "hide": false, - "legendFormat": "{{instance}}", - "refId": "A", - "target": "txcount", - "type": "timeseries" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "TXs Count", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 6, - "y": 12 - }, - "hiddenSeries": false, - "id": 22, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "[[metric_namespace]]_sync_extra_justifications_active{instance=\"[[instance]]\",network=\"[[network]]\"}", - "legendFormat": "{{instance}} active", - "refId": "A" - }, - { - "expr": "[[metric_namespace]]_sync_extra_justifications_failed{instance=\"[[instance]]\",network=\"[[network]]\"}", - "legendFormat": "{{instance}} failed", - "refId": "B" - }, - { - "expr": "[[metric_namespace]]_sync_extra_justifications_importing{instance=\"[[instance]]\",network=\"[[network]]\"}", - "legendFormat": "{{instance}} importing", - "refId": "C" - }, - { - "expr": "[[metric_namespace]]_sync_extra_justifications_pending{instance=\"[[instance]]\",network=\"[[network]]\"}", - "legendFormat": "{{instance}} pending", - "refId": "D" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "Sync justifications", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 12, - "y": 12 - }, - "hiddenSeries": false, - "id": 24, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "[[metric_namespace]]_sub_libp2p_connections{instance=\"[[instance]]\",network=\"[[network]]\"}", - "hide": false, - "legendFormat": "{{instance}} connections", - "refId": "A" - }, - { - "expr": "[[metric_namespace]]_sub_libp2p_is_major_syncing{instance=\"[[instance]]\",network=\"[[network]]\"}", - "hide": false, - "legendFormat": "{{instance}} syncing", - "refId": "B" - }, - { - "expr": "[[metric_namespace]]_sub_libp2p_kbuckets_num_nodes{instance=\"[[instance]]\",network=\"[[network]]\"}", - "hide": false, - "legendFormat": "{{instance}} num_nodes", - "refId": "C" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "sub_libp2p", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 18, - "y": 12 - }, - "hiddenSeries": false, - "id": 26, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "[[metric_namespace]]_sub_libp2p_notifications_total{instance=\"[[instance]]\",network=\"[[network]]\",protocol=\"FRNK\",direction=\"in\"}", - "hide": false, - "legendFormat": "{{instance}} FRNK in", - "refId": "A" - }, - { - "expr": "[[metric_namespace]]_sub_libp2p_notifications_total{instance=\"[[instance]]\",network=\"[[network]]\",protocol=\"FRNK\",direction=\"out\"}", - "hide": false, - "legendFormat": "{{instance}} FRNK out", - "refId": "B" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "libp2p_notifications", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 0, - "y": 18 - }, - "hiddenSeries": false, - "id": 28, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "[[metric_namespace]]_cpu_usage_percentage{instance=\"[[instance]]\",network=\"[[network]]\"}", - "legendFormat": "{{instance}}", - "refId": "A" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "CPU usage %", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 6, - "y": 18 - }, - "hiddenSeries": false, - "id": 27, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "[[metric_namespace]]_memory_usage_bytes{instance=\"[[instance]]\",network=\"[[network]]\"}", - "legendFormat": "{{instance}} Mem bytes", - "refId": "A" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "Memory", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "decimals": 2, - "format": "bytes", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": false - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 12, - "y": 18 - }, - "hiddenSeries": false, - "id": 25, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "[[metric_namespace]]_sub_libp2p_network_per_sec_bytes", - "hide": false, - "legendFormat": "{{instance}}", - "refId": "A" - }, - { - "expr": "[[metric_namespace]]_sub_libp2p_notifications_total", - "hide": true, - "legendFormat": "{{instance}}", - "refId": "B" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "libp2p_network_per_sec_bytes", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - }, - { - "aliasColors": {}, - "bars": false, - "cacheTimeout": null, - "dashLength": 10, - "dashes": false, - "datasource": null, - "description": "", - "fill": 1, - "fillGradient": 0, - "gridPos": { - "h": 6, - "w": 6, - "x": 18, - "y": 18 - }, - "hiddenSeries": false, - "id": 29, - "legend": { - "avg": false, - "current": false, - "max": false, - "min": false, - "show": true, - "total": false, - "values": false - }, - "lines": true, - "linewidth": 1, - "links": [], - "nullPointMode": "null", - "options": { - "dataLinks": [] - }, - "percentage": false, - "pluginVersion": "6.5.2", - "pointradius": 2, - "points": false, - "renderer": "flot", - "seriesOverrides": [], - "spaceLength": 10, - "stack": false, - "steppedLine": false, - "targets": [ - { - "expr": "[[metric_namespace]]_sub_libp2p_notifications_total{instance=\"[[instance]]\",network=\"[[network]]\",protocol=\"dot1\",direction=\"in\"}", - "hide": false, - "legendFormat": "{{instance}} dot1 in", - "refId": "B" - }, - { - "expr": "[[metric_namespace]]_sub_libp2p_notifications_total{instance=\"[[instance]]\",network=\"[[network]]\",protocol=\"dot2\",direction=\"in\"}", - "hide": false, - "legendFormat": "{{instance}} dot2 in", - "refId": "C" - }, - { - "expr": "[[metric_namespace]]_sub_libp2p_notifications_total{instance=\"[[instance]]\",network=\"[[network]]\",protocol=\"dot2\",direction=\"out\"}", - "hide": false, - "legendFormat": "{{instance}} dot2 out", - "refId": "A" - } - ], - "thresholds": [], - "timeFrom": null, - "timeRegions": [], - "timeShift": null, - "title": "libp2p_notifications", - "tooltip": { - "shared": true, - "sort": 0, - "value_type": "individual" - }, - "type": "graph", - "xaxis": { - "buckets": null, - "mode": "time", - "name": null, - "show": true, - "values": [] - }, - "yaxes": [ - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - }, - { - "format": "short", - "label": null, - "logBase": 1, - "max": null, - "min": null, - "show": true - } - ], - "yaxis": { - "align": false, - "alignLevel": null - } - } - ], - "refresh": "5s", - "schemaVersion": 22, - "style": "dark", - "tags": [], - "templating": { - "list": [ - { - "allValue": null, - "current": { - "selected": true, - "text": "substrate", - "value": "substrate" - }, - "hide": 0, - "includeAll": false, - "label": null, - "multi": false, - "name": "metric_namespace", - "options": [ - { - "selected": true, - "text": "substrate", - "value": "substrate" - }, - { - "selected": false, - "text": "polkadot", - "value": "polkadot" - } - ], - "query": "substrate, polkadot", - "skipUrlSync": false, - "type": "custom" - }, - { - "allValue": null, - "current": { - "selected": true, - "text": "dev", - "value": "dev" - }, - "datasource": "Prometheus", - "definition": "label_values(network)", - "hide": 0, - "includeAll": false, - "index": -1, - "label": null, - "multi": false, - "name": "network", - "options": [], - "query": "label_values(network)", - "refresh": 1, - "regex": "", - "skipUrlSync": false, - "sort": 0, - "tagValuesQuery": "", - "tags": [], - "tagsQuery": "", - "type": "query", - "useTags": false - }, - { - "allValue": null, - "current": { - "selected": false, - "text": "validator-a:9615", - "value": "validator-a:9615" - }, - "datasource": "Prometheus", - "definition": "label_values(instance)", - "hide": 0, - "includeAll": false, - "index": -1, - "label": null, - "multi": false, - "name": "instance", - "options": [], - "query": "label_values(instance)", - "refresh": 1, - "regex": "", - "skipUrlSync": false, - "sort": 0, - "tagValuesQuery": "", - "tags": [], - "tagsQuery": "", - "type": "query", - "useTags": false - } - ] - }, - "time": { - "from": "now-1h", - "to": "now" - }, - "timepicker": { - "refresh_intervals": [ - "5s", - "10s", - "30s", - "1m", - "5m", - "15m", - "30m", - "1h", - "2h", - "1d" - ] - }, - "timezone": "", - "title": "Substrate Dashboard", - "uid": "ColmenaLabs", - "variables": { - "list": [] - }, - "version": 2 -} diff --git a/.maintain/monitoring/grafana-dashboards/substrate-networking.json b/.maintain/monitoring/grafana-dashboards/substrate-networking.json index d2abfd1cb864e..0b157e7205835 100644 --- a/.maintain/monitoring/grafana-dashboards/substrate-networking.json +++ b/.maintain/monitoring/grafana-dashboards/substrate-networking.json @@ -74,7 +74,7 @@ "gnetId": null, "graphTooltip": 0, "id": null, - "iteration": 1610462565248, + "iteration": 1613393276921, "links": [], "panels": [ { @@ -963,7 +963,8 @@ "steppedLine": false, "targets": [ { - "expr": "irate(${metric_namespace}_sub_libp2p_requests_out_started_total{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])", + "expr": "irate(${metric_namespace}_sub_libp2p_requests_out_success_total_sum{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m]) + on(instance) sum(irate(${metric_namespace}_sub_libp2p_requests_out_failure_total{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])) by (instance)", + "hide": false, "interval": "", "legendFormat": "{{instance}}", "refId": "A" @@ -989,6 +990,7 @@ }, "yaxes": [ { + "$$hashKey": "object:209", "format": "reqps", "label": null, "logBase": 1, @@ -997,6 +999,7 @@ "show": true }, { + "$$hashKey": "object:210", "format": "short", "label": null, "logBase": 1, @@ -1032,7 +1035,7 @@ "y": 51 }, "hiddenSeries": false, - "id": 151, + "id": 448, "legend": { "avg": false, "current": false, @@ -1060,9 +1063,11 @@ "steppedLine": false, "targets": [ { - "expr": "irate(${metric_namespace}_sub_libp2p_requests_in_success_total_count{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])", + "expr": "sum(irate(${metric_namespace}_sub_libp2p_requests_out_failure_total{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[$__rate_interval])) by (instance, reason)", + "hide": false, "interval": "", - "legendFormat": "{{instance}}", + "intervalFactor": 1, + "legendFormat": "{{reason}}", "refId": "A" } ], @@ -1070,7 +1075,7 @@ "timeFrom": null, "timeRegions": [], "timeShift": null, - "title": "Requests served per second", + "title": "Outbound requests failures", "tooltip": { "shared": true, "sort": 2, @@ -1086,6 +1091,7 @@ }, "yaxes": [ { + "$$hashKey": "object:209", "format": "reqps", "label": null, "logBase": 1, @@ -1094,6 +1100,7 @@ "show": true }, { + "$$hashKey": "object:210", "format": "short", "label": null, "logBase": 1, @@ -1227,7 +1234,7 @@ "y": 59 }, "hiddenSeries": false, - "id": 258, + "id": 257, "legend": { "avg": false, "current": false, @@ -1239,7 +1246,7 @@ }, "lines": true, "linewidth": 1, - "nullPointMode": "null", + "nullPointMode": "null as zero", "options": { "alertThreshold": true }, @@ -1255,7 +1262,8 @@ "steppedLine": false, "targets": [ { - "expr": "histogram_quantile(0.5, sum(rate(${metric_namespace}_sub_libp2p_requests_in_success_total_bucket{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])) by (instance, le))", + "expr": "histogram_quantile(0.99, sum(rate(${metric_namespace}_sub_libp2p_requests_out_success_total_bucket{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])) by (instance, le)) > 0", + "instant": false, "interval": "", "legendFormat": "{{instance}}", "refId": "A" @@ -1265,7 +1273,7 @@ "timeFrom": null, "timeRegions": [], "timeShift": null, - "title": "Median request serving time", + "title": "99th percentile request answer time", "tooltip": { "shared": true, "sort": 2, @@ -1324,7 +1332,7 @@ "y": 63 }, "hiddenSeries": false, - "id": 257, + "id": 151, "legend": { "avg": false, "current": false, @@ -1336,7 +1344,7 @@ }, "lines": true, "linewidth": 1, - "nullPointMode": "null as zero", + "nullPointMode": "null", "options": { "alertThreshold": true }, @@ -1352,8 +1360,7 @@ "steppedLine": false, "targets": [ { - "expr": "histogram_quantile(0.99, sum(rate(${metric_namespace}_sub_libp2p_requests_out_success_total_bucket{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])) by (instance, le)) > 0", - "instant": false, + "expr": "irate(${metric_namespace}_sub_libp2p_requests_in_success_total_count{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])", "interval": "", "legendFormat": "{{instance}}", "refId": "A" @@ -1363,7 +1370,7 @@ "timeFrom": null, "timeRegions": [], "timeShift": null, - "title": "99th percentile request answer time", + "title": "Requests served per second", "tooltip": { "shared": true, "sort": 2, @@ -1379,7 +1386,7 @@ }, "yaxes": [ { - "format": "s", + "format": "reqps", "label": null, "logBase": 1, "max": null, @@ -1422,7 +1429,7 @@ "y": 67 }, "hiddenSeries": false, - "id": 259, + "id": 449, "legend": { "avg": false, "current": false, @@ -1434,7 +1441,7 @@ }, "lines": true, "linewidth": 1, - "nullPointMode": "null as zero", + "nullPointMode": "null", "options": { "alertThreshold": true }, @@ -1450,9 +1457,11 @@ "steppedLine": false, "targets": [ { - "expr": "histogram_quantile(0.99, sum(rate(${metric_namespace}_sub_libp2p_requests_in_success_total_bucket{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])) by (instance, le))", + "expr": "sum(irate(${metric_namespace}_sub_libp2p_requests_in_failure_total{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[$__rate_interval])) by (instance, reason)", + "hide": false, "interval": "", - "legendFormat": "{{instance}}", + "intervalFactor": 1, + "legendFormat": "{{reason}}", "refId": "A" } ], @@ -1460,9 +1469,9 @@ "timeFrom": null, "timeRegions": [], "timeShift": null, - "title": "99th percentile request serving time", + "title": "Inbound requests failures", "tooltip": { - "shared": false, + "shared": true, "sort": 2, "value_type": "individual" }, @@ -1476,7 +1485,8 @@ }, "yaxes": [ { - "format": "s", + "$$hashKey": "object:209", + "format": "reqps", "label": null, "logBase": 1, "max": null, @@ -1484,6 +1494,7 @@ "show": true }, { + "$$hashKey": "object:210", "format": "short", "label": null, "logBase": 1, @@ -1519,7 +1530,7 @@ "y": 71 }, "hiddenSeries": false, - "id": 287, + "id": 258, "legend": { "avg": false, "current": false, @@ -1531,7 +1542,7 @@ }, "lines": true, "linewidth": 1, - "nullPointMode": "null as zero", + "nullPointMode": "null", "options": { "alertThreshold": true }, @@ -1547,10 +1558,9 @@ "steppedLine": false, "targets": [ { - "expr": "avg(irate(${metric_namespace}_sub_libp2p_requests_out_failure_total{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])) by (reason)", - "instant": false, + "expr": "histogram_quantile(0.5, sum(rate(${metric_namespace}_sub_libp2p_requests_in_success_total_bucket{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])) by (instance, le))", "interval": "", - "legendFormat": "{{reason}}", + "legendFormat": "{{instance}}", "refId": "A" } ], @@ -1558,7 +1568,7 @@ "timeFrom": null, "timeRegions": [], "timeShift": null, - "title": "Outgoing request failures per second", + "title": "Median request serving time", "tooltip": { "shared": true, "sort": 2, @@ -1574,7 +1584,7 @@ }, "yaxes": [ { - "format": "short", + "format": "s", "label": null, "logBase": 1, "max": null, @@ -1617,7 +1627,7 @@ "y": 75 }, "hiddenSeries": false, - "id": 286, + "id": 259, "legend": { "avg": false, "current": false, @@ -1645,10 +1655,9 @@ "steppedLine": false, "targets": [ { - "expr": "avg(irate(${metric_namespace}_sub_libp2p_requests_in_failure_total{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])) by (reason)", - "instant": false, + "expr": "histogram_quantile(0.99, sum(rate(${metric_namespace}_sub_libp2p_requests_in_success_total_bucket{instance=~\"${nodename}\", protocol=\"${request_protocol}\"}[5m])) by (instance, le))", "interval": "", - "legendFormat": "{{reason}}", + "legendFormat": "{{instance}}", "refId": "A" } ], @@ -1656,9 +1665,9 @@ "timeFrom": null, "timeRegions": [], "timeShift": null, - "title": "Ingoing request failures per second", + "title": "99th percentile request serving time", "tooltip": { - "shared": true, + "shared": false, "sort": 2, "value_type": "individual" }, @@ -1672,7 +1681,7 @@ }, "yaxes": [ { - "format": "short", + "format": "s", "label": null, "logBase": 1, "max": null, @@ -1845,7 +1854,7 @@ "lines": true, "linewidth": 1, "maxPerRow": 12, - "nullPointMode": "null as zero", + "nullPointMode": "connected", "options": { "alertThreshold": true }, @@ -1871,7 +1880,7 @@ "steppedLine": false, "targets": [ { - "expr": "avg by (direction) (irate(${metric_namespace}_sub_libp2p_notifications_sizes_count{instance=~\"${nodename}\", protocol=\"${notif_protocol}\"}[$__interval]))", + "expr": "avg by (direction) (irate(${metric_namespace}_sub_libp2p_notifications_sizes_count{instance=~\"${nodename}\", protocol=\"${notif_protocol}\"}[$__rate_interval]))", "interval": "", "legendFormat": "{{direction}}", "refId": "A" @@ -1958,7 +1967,7 @@ "lines": true, "linewidth": 1, "maxPerRow": 12, - "nullPointMode": "null as zero", + "nullPointMode": "connected", "options": { "alertThreshold": true }, @@ -1984,7 +1993,7 @@ "steppedLine": false, "targets": [ { - "expr": "avg(irate(${metric_namespace}_sub_libp2p_notifications_sizes_sum{instance=~\"${nodename}\", protocol=\"${notif_protocol}\"}[$__interval])) by (direction)", + "expr": "avg(irate(${metric_namespace}_sub_libp2p_notifications_sizes_sum{instance=~\"${nodename}\", protocol=\"${notif_protocol}\"}[$__rate_interval])) by (direction)", "instant": false, "interval": "", "legendFormat": "{{direction}}", @@ -2674,7 +2683,7 @@ "allValue": null, "current": {}, "datasource": "$data_source", - "definition": "${metric_namespace}_sub_libp2p_requests_out_started_total{instance=~\"${nodename}\"}", + "definition": "${metric_namespace}_sub_libp2p_requests_in_success_total_count{instance=~\"${nodename}\"}", "error": null, "hide": 2, "includeAll": true, @@ -2682,7 +2691,7 @@ "multi": false, "name": "request_protocol", "options": [], - "query": "${metric_namespace}_sub_libp2p_requests_out_started_total{instance=~\"${nodename}\"}", + "query": "${metric_namespace}_sub_libp2p_requests_in_success_total_count{instance=~\"${nodename}\"}", "refresh": 1, "regex": "/protocol=\"(.*?)\"/", "skipUrlSync": false, @@ -2707,6 +2716,7 @@ "name": "data_source", "options": [], "query": "prometheus", + "queryValue": "", "refresh": 1, "regex": "", "skipUrlSync": false, @@ -2756,5 +2766,5 @@ "timezone": "utc", "title": "Substrate Networking", "uid": "vKVuiD9Zk", - "version": 147 -} + "version": 154 +} \ No newline at end of file diff --git a/.maintain/monitoring/grafana-dashboards/substrate-service-tasks.json b/.maintain/monitoring/grafana-dashboards/substrate-service-tasks.json index a3db46ec6d2a9..944c9fb50c9bc 100644 --- a/.maintain/monitoring/grafana-dashboards/substrate-service-tasks.json +++ b/.maintain/monitoring/grafana-dashboards/substrate-service-tasks.json @@ -37,6 +37,7 @@ "annotations": { "list": [ { + "$$hashKey": "object:326", "builtIn": 1, "datasource": "-- Grafana --", "enable": true, @@ -48,6 +49,7 @@ "type": "dashboard" }, { + "$$hashKey": "object:327", "datasource": "$data_source", "enable": true, "expr": "increase(${metric_namespace}_tasks_ended_total{reason=\"panic\", instance=~\"${nodename}\"}[10m])", @@ -64,6 +66,7 @@ "type": "tags" }, { + "$$hashKey": "object:621", "datasource": "$data_source", "enable": true, "expr": "changes(${metric_namespace}_process_start_time_seconds{instance=~\"${nodename}\"}[10m])", @@ -81,7 +84,7 @@ "gnetId": null, "graphTooltip": 0, "id": null, - "iteration": 1610462629581, + "iteration": 1613393319015, "links": [], "panels": [ { @@ -164,7 +167,7 @@ }, "lines": false, "linewidth": 1, - "nullPointMode": "null as zero", + "nullPointMode": "null", "options": { "alertThreshold": true }, @@ -180,7 +183,7 @@ "steppedLine": false, "targets": [ { - "expr": "irate(${metric_namespace}_tasks_polling_duration_sum{instance=~\"${nodename}\"}[10m])", + "expr": "irate(${metric_namespace}_tasks_polling_duration_sum{instance=~\"${nodename}\"}[$__rate_interval])", "interval": "", "legendFormat": "{{task_name}}", "refId": "A" @@ -206,6 +209,7 @@ }, "yaxes": [ { + "$$hashKey": "object:2721", "format": "percentunit", "label": null, "logBase": 1, @@ -214,6 +218,7 @@ "show": true }, { + "$$hashKey": "object:2722", "format": "short", "label": null, "logBase": 1, @@ -266,7 +271,7 @@ }, "lines": true, "linewidth": 2, - "nullPointMode": "null", + "nullPointMode": "connected", "options": { "alertThreshold": true }, @@ -282,7 +287,7 @@ "steppedLine": true, "targets": [ { - "expr": "irate(${metric_namespace}_tasks_polling_duration_count{instance=~\"${nodename}\"}[10m])", + "expr": "irate(${metric_namespace}_tasks_polling_duration_count{instance=~\"${nodename}\"}[$__rate_interval])", "interval": "", "legendFormat": "{{task_name}}", "refId": "A" @@ -308,6 +313,7 @@ }, "yaxes": [ { + "$$hashKey": "object:2571", "format": "cps", "label": null, "logBase": 1, @@ -316,6 +322,7 @@ "show": true }, { + "$$hashKey": "object:2572", "format": "short", "label": null, "logBase": 1, @@ -382,7 +389,7 @@ "steppedLine": true, "targets": [ { - "expr": "irate(${metric_namespace}_tasks_spawned_total{instance=~\"${nodename}\"}[10m])", + "expr": "irate(${metric_namespace}_tasks_spawned_total{instance=~\"${nodename}\"}[$__rate_interval])", "interval": "", "legendFormat": "{{task_name}}", "refId": "A" @@ -408,6 +415,7 @@ }, "yaxes": [ { + "$$hashKey": "object:771", "format": "short", "label": null, "logBase": 10, @@ -416,6 +424,7 @@ "show": true }, { + "$$hashKey": "object:772", "format": "short", "label": null, "logBase": 1, @@ -466,7 +475,7 @@ }, "lines": true, "linewidth": 1, - "nullPointMode": "null as zero", + "nullPointMode": "connected", "options": { "alertThreshold": true }, @@ -508,6 +517,7 @@ }, "yaxes": [ { + "$$hashKey": "object:919", "format": "short", "label": null, "logBase": 10, @@ -516,6 +526,7 @@ "show": true }, { + "$$hashKey": "object:920", "format": "short", "label": null, "logBase": 1, @@ -585,7 +596,7 @@ "steppedLine": true, "targets": [ { - "expr": "irate(${metric_namespace}_tasks_polling_duration_bucket{instance=~\"${nodename}\", le=\"+Inf\"}[10m])\n - ignoring(le)\n irate(${metric_namespace}_tasks_polling_duration_bucket{instance=~\"${nodename}\", le=\"1.024\"}[10m]) > 0", + "expr": "irate(${metric_namespace}_tasks_polling_duration_bucket{instance=~\"${nodename}\", le=\"+Inf\"}[$__rate_interval])\n - ignoring(le)\n irate(${metric_namespace}_tasks_polling_duration_bucket{instance=~\"${nodename}\", le=\"1.024\"}[$__rate_interval]) > 0", "interval": "", "legendFormat": "{{task_name}}", "refId": "A" @@ -611,6 +622,7 @@ }, "yaxes": [ { + "$$hashKey": "object:3040", "decimals": null, "format": "cps", "label": "Calls to `Future::poll`/second", @@ -620,6 +632,7 @@ "show": true }, { + "$$hashKey": "object:3041", "format": "short", "label": null, "logBase": 1, @@ -683,7 +696,7 @@ }, "lines": true, "linewidth": 1, - "nullPointMode": "null", + "nullPointMode": "connected", "options": { "alertThreshold": true }, @@ -725,6 +738,7 @@ }, "yaxes": [ { + "$$hashKey": "object:626", "format": "short", "label": null, "logBase": 1, @@ -733,6 +747,7 @@ "show": true }, { + "$$hashKey": "object:627", "format": "short", "label": null, "logBase": 1, @@ -782,7 +797,7 @@ }, "lines": true, "linewidth": 1, - "nullPointMode": "null", + "nullPointMode": "connected", "options": { "alertThreshold": true }, @@ -798,7 +813,7 @@ "steppedLine": false, "targets": [ { - "expr": "irate(${metric_namespace}_unbounded_channel_len{instance=~\"${nodename}\", action = \"send\"}[10m])", + "expr": "irate(${metric_namespace}_unbounded_channel_len{instance=~\"${nodename}\", action = \"send\"}[$__rate_interval])", "interval": "", "legendFormat": "{{entity}}", "refId": "B" @@ -824,6 +839,7 @@ }, "yaxes": [ { + "$$hashKey": "object:626", "format": "cps", "label": null, "logBase": 1, @@ -832,6 +848,7 @@ "show": true }, { + "$$hashKey": "object:627", "format": "short", "label": null, "logBase": 1, @@ -938,5 +955,5 @@ "timezone": "utc", "title": "Substrate Service Tasks", "uid": "3LA6XNqZz", - "version": 59 -} + "version": 60 +} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 6736cc335e135..3cf86c1203e1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.15.0] +### Changed +- Updated Substrate to polkadot-v0.8.29 + ## [2.14.0] ### Changed - Updated Substrate to polkadot-v0.8.28 diff --git a/Cargo.lock b/Cargo.lock index 047ba162ec1a9..982d2f4786293 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,11 +14,11 @@ dependencies = [ [[package]] name = "addr2line" -version = "0.14.0" +version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c0929d69e78dd9bf5408269919fcbcaeb2e35e5d43e5815517cdc6a8e11a423" +checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7" dependencies = [ - "gimli 0.23.0", + "gimli", ] [[package]] @@ -57,7 +57,7 @@ dependencies = [ "aes", "block-cipher", "ghash", - "subtle 2.3.0", + "subtle 2.4.0", ] [[package]] @@ -83,9 +83,9 @@ dependencies = [ [[package]] name = "ahash" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6789e291be47ace86a60303502173d84af8327e3627ecf334356ee0f87a164c" +checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e" [[package]] name = "aho-corasick" @@ -136,9 +136,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.34" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7" +checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1" [[package]] name = "approx" @@ -197,9 +197,9 @@ dependencies = [ [[package]] name = "assert_cmd" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c88b9ca26f9c16ec830350d309397e74ee9abdfd8eb1f71cb6ecc71a3fc818da" +checksum = "3dc1679af9a1ab4bea16f228b05d18f8363f8327b1fa8db00d2760cfafc6b61e" dependencies = [ "doc-comment", "predicates", @@ -214,6 +214,16 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "695579f0f2520f3774bb40461e5adb066459d4e0af4d59d20175484fb8e9edf1" +[[package]] +name = "async-attributes" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "async-channel" version = "1.5.1" @@ -241,12 +251,15 @@ dependencies = [ [[package]] name = "async-global-executor" -version = "1.4.3" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73079b49cd26b8fd5a15f68fc7707fc78698dc2a3d61430f2a7a9430230dfa04" +checksum = "9586ec52317f36de58453159d48351bc244bc24ced3effc1fce22f3d48664af6" dependencies = [ + "async-channel", "async-executor", "async-io", + "async-mutex", + "blocking", "futures-lite", "num_cpus", "once_cell", @@ -272,6 +285,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "async-lock" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1996609732bde4a9988bc42125f55f2af5f3c36370e27c778d5191a4a1b63bfb" +dependencies = [ + "event-listener", +] + [[package]] name = "async-mutex" version = "1.4.0" @@ -281,17 +303,35 @@ dependencies = [ "event-listener", ] +[[package]] +name = "async-process" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8cea09c1fb10a317d1b5af8024eeba256d6554763e85ecd90ff8df31c7bbda" +dependencies = [ + "async-io", + "blocking", + "cfg-if 0.1.10", + "event-listener", + "futures-lite", + "once_cell", + "signal-hook", + "winapi 0.3.9", +] + [[package]] name = "async-std" -version = "1.7.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7e82538bc65a25dbdff70e4c5439d52f068048ab97cdea0acd73f131594caa1" +checksum = "d9f06685bad74e0570f5213741bea82158279a4103d988e57bfada11ad230341" dependencies = [ + "async-attributes", + "async-channel", "async-global-executor", "async-io", - "async-mutex", - "blocking", - "crossbeam-utils 0.8.0", + "async-lock", + "async-process", + "crossbeam-utils 0.8.1", "futures-channel", "futures-core", "futures-io", @@ -302,7 +342,7 @@ dependencies = [ "memchr", "num_cpus", "once_cell", - "pin-project-lite 0.1.11", + "pin-project-lite 0.2.4", "pin-utils", "slab", "wasm-bindgen-futures", @@ -316,9 +356,9 @@ checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0" [[package]] name = "async-trait" -version = "0.1.41" +version = "0.1.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b246867b8b3b6ae56035f1eb1ed557c1d8eae97f0d53696138a50fa0e3a3b8c0" +checksum = "8d3a45e77e34375a7923b1e8febb049bb011f064714a8e17a1a616fef01da13d" dependencies = [ "proc-macro2", "quote", @@ -335,7 +375,20 @@ dependencies = [ "futures-sink", "futures-util", "memchr", - "pin-project-lite 0.2.0", + "pin-project-lite 0.2.4", +] + +[[package]] +name = "asynchronous-codec" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0de5164e5edbf51c45fb8c2d9664ae1c095cce1b265ecf7569093c0d66ef690" +dependencies = [ + "bytes 1.0.1", + "futures-sink", + "futures-util", + "memchr", + "pin-project-lite 0.2.4", ] [[package]] @@ -344,7 +397,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3410529e8288c463bedb5930f82833bc0c90e5d2fe639a56582a4d09220b281" dependencies = [ - "autocfg 1.0.1", + "autocfg", ] [[package]] @@ -364,12 +417,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "autocfg" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" - [[package]] name = "autocfg" version = "1.0.1" @@ -378,18 +425,24 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "backtrace" -version = "0.3.54" +version = "0.3.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2baad346b2d4e94a24347adeee9c7a93f412ee94b9cc26e5b59dea23848e9f28" +checksum = "9d117600f438b1707d4e4ae15d3595657288f8235a0eb593e80ecc98ab34e1bc" dependencies = [ "addr2line", "cfg-if 1.0.0", "libc", "miniz_oxide", - "object 0.22.0", + "object 0.23.0", "rustc-demangle", ] +[[package]] +name = "base-x" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b" + [[package]] name = "base58" version = "0.1.0" @@ -450,12 +503,14 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "bitvec" -version = "0.17.4" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41262f11d771fd4a61aa3ce019fca363b4b6c282fca9da2a31186d3965a47a5c" +checksum = "f5011ffc90248764d7005b0e10c7294f5aa1bd87d9dd7248f4ad475b347c294d" dependencies = [ - "either", + "funty", "radium", + "tap", + "wyz", ] [[package]] @@ -490,6 +545,32 @@ dependencies = [ "constant_time_eq", ] +[[package]] +name = "blake2s_simd" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e461a7034e85b211a4acb57ee2e6730b32912b06c08cc242243c39fc21ae6a2" +dependencies = [ + "arrayref", + "arrayvec 0.5.2", + "constant_time_eq", +] + +[[package]] +name = "blake3" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9ff35b701f3914bdb8fad3368d822c766ef2858b2583198e41639b936f09d3f" +dependencies = [ + "arrayref", + "arrayvec 0.5.2", + "cc", + "cfg-if 0.1.10", + "constant_time_eq", + "crypto-mac 0.8.0", + "digest 0.9.0", +] + [[package]] name = "block-buffer" version = "0.7.3" @@ -579,15 +660,15 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.4.0" +version = "3.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" +checksum = "099e596ef14349721d9016f6b80dd3419ea1bf289ab9b44df8e4dfd3a005d5d9" [[package]] name = "byte-slice-cast" -version = "0.3.5" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0a5e3906bcbf133e33c1d4d95afc664ad37fbdb9f6568d8043e7ea8c27d93d3" +checksum = "65c1bf4a04a88c54f589125563643d773f3254b5c38571395e2b591c693bbc81" [[package]] name = "byte-tools" @@ -597,9 +678,9 @@ checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" [[package]] name = "byteorder" -version = "1.3.4" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" +checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" [[package]] name = "bytes" @@ -630,15 +711,26 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba" +[[package]] +name = "cargo-platform" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0226944a63d1bf35a3b5f948dd7c59e263db83695c9e8bffc4037de02e30f1d7" +dependencies = [ + "serde", +] + [[package]] name = "cargo_metadata" -version = "0.12.0" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5a5f7b42f606b7f23674f6f4d877628350682bc40687d3fae65679a58d55345" +checksum = "7714a157da7991e23d90686b9524b9e12e0407a108647f52e9328f4b3d51ac7f" dependencies = [ + "cargo-platform", "semver 0.11.0", + "semver-parser 0.10.2", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", ] [[package]] @@ -652,9 +744,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.62" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1770ced377336a88a67c473594ccc14eca6f4559217c34f64aac8f83d641b40" +checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48" dependencies = [ "jobserver", ] @@ -732,6 +824,17 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "cid" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d88f30b1e74e7063df5711496f3ee6e74a9735d62062242d70cddf77717f18e" +dependencies = [ + "multibase", + "multihash", + "unsigned-varint 0.5.1", +] + [[package]] name = "cipher" version = "0.2.5" @@ -785,15 +888,6 @@ dependencies = [ "bitflags", ] -[[package]] -name = "cloudabi" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4344512281c643ae7638bbabc3af17a11307803ec8f0fcad9fae512a8bf36467" -dependencies = [ - "bitflags", -] - [[package]] name = "concurrent-queue" version = "1.2.2" @@ -815,9 +909,9 @@ dependencies = [ [[package]] name = "const_fn" -version = "0.4.3" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c478836e029dcef17fb47c89023448c64f781a046e0300e257ad8225ae59afab" +checksum = "28b9d6de7f49e22cf97ad17fc4036ece69300032f45f78f30b4a4482cdc3f4a6" [[package]] name = "constant_time_eq" @@ -841,46 +935,62 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac" +[[package]] +name = "cpp_demangle" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44919ecaf6f99e8e737bc239408931c9a01e9a6c74814fee8242dd2506b65390" +dependencies = [ + "cfg-if 1.0.0", + "glob", +] + [[package]] name = "cpuid-bool" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" +[[package]] +name = "cpuid-bool" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcb25d077389e53838a8158c8e99174c5a9d902dee4904320db714f3c653ffba" + [[package]] name = "cranelift-bforest" -version = "0.66.0" +version = "0.69.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8dcc286b052ee24a1e5a222e7c1125e6010ad35b0f248709b9b3737a8fedcfdf" +checksum = "4066fd63b502d73eb8c5fa6bcab9c7962b05cd580f6b149ee83a8e730d8ce7fb" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.66.0" +version = "0.69.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d9badfe36176cb653506091693bc2bb1970c9bddfcd6ec7fac404f7eaec6f38" +checksum = "1a54e4beb833a3c873a18a8fe735d73d732044004c7539a072c8faa35ccb0c60" dependencies = [ "byteorder", "cranelift-bforest", "cranelift-codegen-meta", "cranelift-codegen-shared", "cranelift-entity", - "gimli 0.21.0", + "gimli", "log", "regalloc", "serde", - "smallvec 1.5.0", + "smallvec 1.6.1", "target-lexicon", "thiserror", ] [[package]] name = "cranelift-codegen-meta" -version = "0.66.0" +version = "0.69.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c3f460031861e4f4ad510be62b2ae50bba6cc886b598a36f9c0a970feab9598" +checksum = "c54cac7cacb443658d8f0ff36a3545822613fa202c946c0891897843bc933810" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -888,36 +998,36 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" -version = "0.66.0" +version = "0.69.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76ad12409e922e7697cd0bdc7dc26992f64a77c31880dfe5e3c7722f4710206d" +checksum = "a109760aff76788b2cdaeefad6875a73c2b450be13906524f6c2a81e05b8d83c" [[package]] name = "cranelift-entity" -version = "0.66.0" +version = "0.69.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d97cdc58972ea065d107872cfb9079f4c92ade78a8af85aaff519a65b5d13f71" +checksum = "3b044234aa32531f89a08b487630ddc6744696ec04c8123a1ad388de837f5de3" dependencies = [ "serde", ] [[package]] name = "cranelift-frontend" -version = "0.66.0" +version = "0.69.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ef419efb4f94ecc02e5d9fbcc910d2bb7f0040e2de570e63a454f883bc891d6" +checksum = "5452b3e4e97538ee5ef2cc071301c69a86c7adf2770916b9d04e9727096abd93" dependencies = [ "cranelift-codegen", "log", - "smallvec 1.5.0", + "smallvec 1.6.1", "target-lexicon", ] [[package]] name = "cranelift-native" -version = "0.66.0" +version = "0.69.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e69d44d59826eef6794066ac2c0f4ad3975f02d97030c60dbc04e3886adf36e" +checksum = "f68035c10b2e80f26cc29c32fa824380877f38483504c2a47b54e7da311caaf3" dependencies = [ "cranelift-codegen", "raw-cpuid", @@ -926,17 +1036,19 @@ dependencies = [ [[package]] name = "cranelift-wasm" -version = "0.66.0" +version = "0.69.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "979df666b1304624abe99738e9e0e7c7479ee5523ba4b8b237df9ff49996acbb" +checksum = "a530eb9d1c95b3309deb24c3d179d8b0ba5837ed98914a429787c395f614949d" dependencies = [ "cranelift-codegen", "cranelift-entity", "cranelift-frontend", + "itertools 0.9.0", "log", "serde", + "smallvec 1.6.1", "thiserror", - "wasmparser 0.59.0", + "wasmparser", ] [[package]] @@ -950,16 +1062,16 @@ dependencies = [ [[package]] name = "criterion" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70daa7ceec6cf143990669a04c7df13391d55fb27bd4079d252fca774ba244d8" +checksum = "ab327ed7354547cc2ef43cbe20ef68b988e70b4b593cbd66a2a61733123a3d23" dependencies = [ "atty", "cast", "clap", "criterion-plot", "csv", - "itertools 0.9.0", + "itertools 0.10.0", "lazy_static", "num-traits", "oorandom", @@ -969,7 +1081,7 @@ dependencies = [ "serde", "serde_cbor", "serde_derive", - "serde_json 1.0.59", + "serde_json 1.0.61", "tinytemplate", "walkdir", ] @@ -991,7 +1103,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" dependencies = [ "cfg-if 1.0.0", - "crossbeam-utils 0.8.0", + "crossbeam-utils 0.8.1", ] [[package]] @@ -1012,8 +1124,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" dependencies = [ "cfg-if 1.0.0", - "crossbeam-epoch 0.9.0", - "crossbeam-utils 0.8.0", + "crossbeam-epoch 0.9.1", + "crossbeam-utils 0.8.1", ] [[package]] @@ -1022,26 +1134,26 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" dependencies = [ - "autocfg 1.0.1", + "autocfg", "cfg-if 0.1.10", "crossbeam-utils 0.7.2", "lazy_static", "maybe-uninit", - "memoffset", + "memoffset 0.5.6", "scopeguard", ] [[package]] name = "crossbeam-epoch" -version = "0.9.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0f606a85340376eef0d6d8fec399e6d4a544d648386c6645eb6d0653b27d9f" +checksum = "a1aaa739f95311c2c7887a76863f500026092fb1dce0161dab577e559ef3569d" dependencies = [ "cfg-if 1.0.0", "const_fn", - "crossbeam-utils 0.8.0", + "crossbeam-utils 0.8.1", "lazy_static", - "memoffset", + "memoffset 0.6.1", "scopeguard", ] @@ -1062,20 +1174,19 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" dependencies = [ - "autocfg 1.0.1", + "autocfg", "cfg-if 0.1.10", "lazy_static", ] [[package]] name = "crossbeam-utils" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec91540d98355f690a86367e566ecad2e9e579f230230eb7c21398372be73ea5" +checksum = "02d96d1e189ef58269ebe5b97953da3274d83a93af647c2ddd6f9dab28cedb8d" dependencies = [ - "autocfg 1.0.1", + "autocfg", "cfg-if 1.0.0", - "const_fn", "lazy_static", ] @@ -1102,14 +1213,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" dependencies = [ "generic-array 0.14.4", - "subtle 2.3.0", + "subtle 2.4.0", ] [[package]] name = "csv" -version = "1.1.4" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc4666154fd004af3fd6f1da2e81a96fd5a81927fe8ddb6ecc79e2aa6e138b54" +checksum = "f9d58633299b24b515ac72a3f869f8b91306a3cec616a602843a383acd6f9e97" dependencies = [ "bstr", "csv-core", @@ -1138,9 +1249,9 @@ dependencies = [ [[package]] name = "ctor" -version = "0.1.16" +version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fbaabec2c953050352311293be5c6aba8e141ba19d6811862b232d6fd020484" +checksum = "10bcb9d7dcbf7002aaffbb53eac22906b64cdcc127971dcc387d8eb7c95d5560" dependencies = [ "quote", "syn", @@ -1159,35 +1270,55 @@ dependencies = [ [[package]] name = "curve25519-dalek" -version = "2.1.0" +version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d85653f070353a16313d0046f173f70d1aadd5b42600a14de626f0dfb3473a5" +checksum = "434e1720189a637d44fe464f4df1e6eb900b4835255b14354497c78af37d9bb8" dependencies = [ "byteorder", "digest 0.8.1", "rand_core 0.5.1", - "subtle 2.3.0", + "subtle 2.4.0", "zeroize", ] [[package]] name = "curve25519-dalek" -version = "3.0.0" +version = "3.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8492de420e9e60bc9a1d66e2dbb91825390b738a388606600663fc529b4b307" +checksum = "f627126b946c25a4638eec0ea634fc52506dea98db118aae985118ce7c3d723f" dependencies = [ "byteorder", "digest 0.9.0", "rand_core 0.5.1", - "subtle 2.3.0", + "subtle 2.4.0", "zeroize", ] [[package]] name = "data-encoding" -version = "2.3.1" +version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "993a608597367c6377b258c25d7120740f00ed23a2252b729b1932dd7866f908" +checksum = "3ee2393c4a91429dffb4bedf19f4d6abf27d8a732c8ce4980305d782e5426d57" + +[[package]] +name = "data-encoding-macro" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a94feec3d2ba66c0b6621bca8bc6f68415b1e5c69af3586fdd0af9fd9f29b17" +dependencies = [ + "data-encoding", + "data-encoding-macro-internal", +] + +[[package]] +name = "data-encoding-macro-internal" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f83e699727abca3c56e187945f303389590305ab2f0185ea445aa66e8d5f2a" +dependencies = [ + "data-encoding", + "syn", +] [[package]] name = "derive_more" @@ -1226,21 +1357,21 @@ dependencies = [ [[package]] name = "directories" -version = "2.0.2" +version = "3.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "551a778172a450d7fc12e629ca3b0428d00f6afa9a43da1b630d54604e97371c" +checksum = "f8fed639d60b58d0f53498ab13d26f621fd77569cc6edb031f4cc36a2ad9da0f" dependencies = [ - "cfg-if 0.1.10", "dirs-sys", ] [[package]] -name = "directories" -version = "3.0.1" +name = "directories-next" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8fed639d60b58d0f53498ab13d26f621fd77569cc6edb031f4cc36a2ad9da0f" +checksum = "339ee130d97a610ea5a5872d2bbb130fdf68884ff09d3028b81bec8a1ac23bbc" dependencies = [ - "dirs-sys", + "cfg-if 1.0.0", + "dirs-sys-next", ] [[package]] @@ -1250,7 +1381,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a" dependencies = [ "libc", - "redox_users", + "redox_users 0.3.5", + "winapi 0.3.9", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users 0.4.0", "winapi 0.3.9", ] @@ -1299,9 +1441,9 @@ dependencies = [ [[package]] name = "dyn-clone" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d55796afa1b20c2945ca8eabfc421839f2b766619209f1ede813cf2484f31804" +checksum = "ee2626afccd7561a06cf1367e2950c4718ea04565e20fb5029b6c7d8ad09abcf" [[package]] name = "ed25519" @@ -1318,11 +1460,11 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" dependencies = [ - "curve25519-dalek 3.0.0", + "curve25519-dalek 3.0.2", "ed25519", "rand 0.7.3", "serde", - "sha2 0.9.2", + "sha2 0.9.3", "zeroize", ] @@ -1354,12 +1496,12 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.5.13" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15b0a4d2e39f8420210be8b27eeda28029729e2fd4291019455016c348240c38" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" dependencies = [ "atty", - "humantime", + "humantime 1.3.0", "log", "regex", "termcolor", @@ -1367,12 +1509,12 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.7.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +checksum = "f26ecb66b4bdca6c1409b40fb255eefc2bd4f6d135dab3c3124f80ffa2a9661e" dependencies = [ "atty", - "humantime", + "humantime 2.1.0", "log", "regex", "termcolor", @@ -1386,9 +1528,9 @@ checksum = "68b91989ae21441195d7d9b9993a2f9295c7e1a8c96255d8b729accddc124797" [[package]] name = "erased-serde" -version = "0.3.12" +version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ca8b296792113e1500fd935ae487be6e00ce318952a6880555554824d6ebf38" +checksum = "0465971a8cc1fa2455c8465aaa377131e1f1cf4983280f474a13e68793aa770c" dependencies = [ "serde", ] @@ -1426,7 +1568,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e43f2f1833d64e33f15592464d6fdd70f349dda7b1a53088eb83cd94014008c5" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", ] [[package]] @@ -1493,18 +1635,18 @@ dependencies = [ [[package]] name = "finality-grandpa" -version = "0.12.3" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8feb87a63249689640ac9c011742c33139204e3c134293d3054022276869133b" +checksum = "c6447e2f8178843749e8c8003206def83ec124a7859475395777a28b5338647c" dependencies = [ "either", - "futures 0.3.9", - "futures-timer 2.0.2", + "futures 0.3.12", + "futures-timer 3.0.2", "log", "num-traits", "parity-scale-codec", - "parking_lot 0.9.0", - "rand 0.6.5", + "parking_lot 0.11.1", + "rand 0.8.3", ] [[package]] @@ -1514,7 +1656,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfcf0ed7fe52a17a03854ec54a9f76d6d84508d1c0e66bc1793301c73fc8493c" dependencies = [ "byteorder", - "rand 0.8.1", + "rand 0.8.3", "rustc-hex", "static_assertions", ] @@ -1527,9 +1669,9 @@ checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d" [[package]] name = "flate2" -version = "1.0.19" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7411863d55df97a419aa64cb4d2f167103ea9d767e2c54a1868b7ac3f6b47129" +checksum = "cd3aec53de10fe96d7d8c565eb17f2c687bb5518a2ec453b5b1252964526abe0" dependencies = [ "cfg-if 1.0.0", "crc32fast", @@ -1546,7 +1688,7 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "fork-tree" -version = "2.0.1" +version = "3.0.0" dependencies = [ "parity-scale-codec", ] @@ -1563,14 +1705,15 @@ dependencies = [ [[package]] name = "frame-benchmarking" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", "hex-literal", "linregress", "parity-scale-codec", - "paste 0.1.18", + "paste 1.0.4", + "serde", "sp-api", "sp-io", "sp-runtime", @@ -1581,7 +1724,7 @@ dependencies = [ [[package]] name = "frame-benchmarking-cli" -version = "2.0.1" +version = "3.0.0" dependencies = [ "Inflector", "chrono", @@ -1603,7 +1746,7 @@ dependencies = [ [[package]] name = "frame-executive" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -1623,7 +1766,7 @@ dependencies = [ [[package]] name = "frame-metadata" -version = "12.0.1" +version = "13.0.0" dependencies = [ "parity-scale-codec", "serde", @@ -1633,27 +1776,28 @@ dependencies = [ [[package]] name = "frame-support" -version = "2.0.1" +version = "3.0.0" dependencies = [ "bitflags", "frame-metadata", "frame-support-procedural", "frame-system", - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "log", "once_cell", "parity-scale-codec", "parity-util-mem", - "paste 0.1.18", + "paste 1.0.4", "pretty_assertions", "serde", - "smallvec 1.5.0", + "smallvec 1.6.1", "sp-api", "sp-arithmetic", "sp-core", "sp-inherents", "sp-io", "sp-runtime", + "sp-staking", "sp-state-machine", "sp-std", "sp-tracing", @@ -1662,7 +1806,7 @@ dependencies = [ [[package]] name = "frame-support-procedural" -version = "2.0.1" +version = "3.0.0" dependencies = [ "Inflector", "frame-support-procedural-tools", @@ -1673,7 +1817,7 @@ dependencies = [ [[package]] name = "frame-support-procedural-tools" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support-procedural-tools-derive", "proc-macro-crate", @@ -1684,7 +1828,7 @@ dependencies = [ [[package]] name = "frame-support-procedural-tools-derive" -version = "2.0.1" +version = "3.0.0" dependencies = [ "proc-macro2", "quote", @@ -1713,11 +1857,11 @@ dependencies = [ [[package]] name = "frame-system" -version = "2.0.1" +version = "3.0.0" dependencies = [ "criterion", "frame-support", - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "parity-scale-codec", "serde", "sp-core", @@ -1731,7 +1875,7 @@ dependencies = [ [[package]] name = "frame-system-benchmarking" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -1746,10 +1890,21 @@ dependencies = [ [[package]] name = "frame-system-rpc-runtime-api" -version = "2.0.1" +version = "3.0.0" +dependencies = [ + "parity-scale-codec", + "sp-api", +] + +[[package]] +name = "frame-try-runtime" +version = "0.9.0" dependencies = [ + "frame-support", "parity-scale-codec", "sp-api", + "sp-runtime", + "sp-std", ] [[package]] @@ -1792,6 +1947,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +[[package]] +name = "funty" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" + [[package]] name = "futures" version = "0.1.30" @@ -1800,9 +1961,9 @@ checksum = "4c7e4c2612746b0df8fed4ce0c69156021b704c9aefa360311c04e6e9e002eed" [[package]] name = "futures" -version = "0.3.9" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c70be434c505aee38639abccb918163b63158a4b4bb791b45b7023044bdc3c9c" +checksum = "da9052a1a50244d8d5aa9bf55cbc2fb6f357c86cc52e46c62ed390a7180cf150" dependencies = [ "futures-channel", "futures-core", @@ -1815,9 +1976,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.9" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f01c61843314e95f96cc9245702248733a3a3d744e43e2e755e3c7af8348a0a9" +checksum = "f2d31b7ec7efab6eefc7c57233bb10b847986139d88cc2f5a02a1ae6871a1846" dependencies = [ "futures-core", "futures-sink", @@ -1825,9 +1986,9 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.9" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db8d3b0917ff63a2a96173133c02818fac4a746b0a57569d3baca9ec0e945e08" +checksum = "79e5145dde8da7d1b3892dad07a9c98fc04bc39892b1ecc9692cf53e2b780a65" [[package]] name = "futures-cpupool" @@ -1846,20 +2007,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fdcef58a173af8148b182684c9f2d5250875adbcaff7b5794073894f9d8634a9" dependencies = [ "futures 0.1.30", - "futures 0.3.9", + "futures 0.3.12", "lazy_static", "log", "parking_lot 0.9.0", "pin-project 0.4.27", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", ] [[package]] name = "futures-executor" -version = "0.3.9" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ee9ca2f7eb4475772cf39dd1cd06208dce2670ad38f4d9c7262b3e15f127068" +checksum = "e9e59fdc009a4b3096bf94f740a0f2424c082521f20a9b08c5c07c48d90fd9b9" dependencies = [ "futures-core", "futures-task", @@ -1869,30 +2030,30 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.9" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e37c1a51b037b80922864b8eed90692c5cd8abd4c71ce49b77146caa47f3253b" +checksum = "28be053525281ad8259d47e4de5de657b25e7bac113458555bb4b70bc6870500" [[package]] name = "futures-lite" -version = "1.11.2" +version = "1.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e6c079abfac3ab269e2927ec048dabc89d009ebfdda6b8ee86624f30c689658" +checksum = "b4481d0cd0de1d204a4fa55e7d45f07b1d958abcb06714b3446438e2eff695fb" dependencies = [ "fastrand", "futures-core", "futures-io", "memchr", "parking", - "pin-project-lite 0.1.11", + "pin-project-lite 0.2.4", "waker-fn", ] [[package]] name = "futures-macro" -version = "0.3.9" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f8719ca0e1f3c5e34f3efe4570ef2c0610ca6da85ae7990d472e9cbfba13664" +checksum = "c287d25add322d9f9abdcdc5927ca398917996600182178774032e9f8258fedd" dependencies = [ "proc-macro-hack", "proc-macro2", @@ -1913,15 +2074,15 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.9" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6adabac1290109cfa089f79192fb6244ad2c3f1cc2281f3e1dd987592b71feb" +checksum = "caf5c69029bda2e743fddd0582d1083951d65cc9539aebf8812f36c3491342d6" [[package]] name = "futures-task" -version = "0.3.9" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92a0843a2ff66823a8f7c77bffe9a09be2b64e533562c412d63075643ec0038" +checksum = "13de07eb8ea81ae445aca7b69f5f7bf15d7bf4912d8ca37d6645c77ae8a58d86" dependencies = [ "once_cell", ] @@ -1944,9 +2105,9 @@ dependencies = [ [[package]] name = "futures-util" -version = "0.3.9" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "036a2107cdeb57f6d7322f1b6c363dad67cd63ca3b7d1b925bdf75bd5d96cda9" +checksum = "632a8cd0f2a4b3fdea1657f08bde063848c3bd00f9bbf6e256b8be78802e624b" dependencies = [ "futures 0.1.30", "futures-channel", @@ -1956,7 +2117,7 @@ dependencies = [ "futures-sink", "futures-task", "memchr", - "pin-project-lite 0.2.0", + "pin-project-lite 0.2.4", "pin-utils", "proc-macro-hack", "proc-macro-nested", @@ -1969,19 +2130,6 @@ version = "0.3.55" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" -[[package]] -name = "generator" -version = "0.6.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cdc09201b2e8ca1b19290cf7e65de2246b8e91fb6874279722189c4de7b94dc" -dependencies = [ - "cc", - "libc", - "log", - "rustc_version", - "winapi 0.3.9", -] - [[package]] name = "generic-array" version = "0.12.3" @@ -2012,11 +2160,12 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.1.15" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", + "js-sys", "libc", "wasi 0.9.0+wasi-snapshot-preview1", "wasm-bindgen", @@ -2024,9 +2173,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4060f4657be78b8e766215b02b18a2e862d83745545de804638e2b545e81aee6" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" dependencies = [ "cfg-if 1.0.0", "js-sys", @@ -2037,30 +2186,25 @@ dependencies = [ [[package]] name = "ghash" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6e27f0689a6e15944bdce7e45425efb87eaa8ab0c6e87f11d0987a9133e2531" +checksum = "97304e4cd182c3846f7575ced3890c53012ce534ad9114046b0a9e00bb30a375" dependencies = [ + "opaque-debug 0.3.0", "polyval", ] [[package]] name = "gimli" -version = "0.21.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcc8e0c9bce37868955864dbecd2b1ab2bdf967e6f28066d65aaac620444b65c" +checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" dependencies = [ "fallible-iterator", "indexmap", "stable_deref_trait", ] -[[package]] -name = "gimli" -version = "0.23.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" - [[package]] name = "glob" version = "0.3.0" @@ -2122,10 +2266,10 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http 0.2.1", + "http 0.2.3", "indexmap", "slab", - "tokio 0.2.23", + "tokio 0.2.25", "tokio-util", "tracing", "tracing-futures", @@ -2133,22 +2277,22 @@ dependencies = [ [[package]] name = "half" -version = "1.6.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d36fab90f82edc3c747f9d438e06cf0a491055896f2a279638bb5beed6c40177" +checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3" [[package]] name = "handlebars" -version = "3.5.1" +version = "3.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2764f9796c0ddca4b82c07f25dd2cb3db30b9a8f47940e78e1c883d9e95c3db9" +checksum = "964d0e99a61fe9b1b347389b77ebf8b7e1587b70293676aaca7d27e59b9073b2" dependencies = [ "log", "pest", "pest_derive", "quick-error 2.0.0", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", ] [[package]] @@ -2177,18 +2321,18 @@ dependencies = [ [[package]] name = "heck" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" +checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac" dependencies = [ "unicode-segmentation", ] [[package]] name = "hermit-abi" -version = "0.1.17" +version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" dependencies = [ "libc", ] @@ -2244,9 +2388,9 @@ dependencies = [ [[package]] name = "honggfuzz" -version = "0.5.51" +version = "0.5.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f085725a5828d7e959f014f624773094dfe20acc91be310ef106923c30594bc" +checksum = "ead88897bcad1c396806d6ccba260a0363e11da997472e9e19ab9889969083a2" dependencies = [ "arbitrary", "lazy_static", @@ -2266,11 +2410,11 @@ dependencies = [ [[package]] name = "http" -version = "0.2.1" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" +checksum = "7245cd7449cc792608c3c8a9eaf69bd4eabbabf802713748fd739c98b82f0747" dependencies = [ - "bytes 0.5.6", + "bytes 1.0.1", "fnv", "itoa", ] @@ -2294,7 +2438,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13d5ff830006f7646652e057693569bfe0d51760c0085a071769d142a205111b" dependencies = [ "bytes 0.5.6", - "http 0.2.1", + "http 0.2.3", ] [[package]] @@ -2318,6 +2462,12 @@ dependencies = [ "quick-error 1.2.3", ] +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.12.35" @@ -2359,14 +2509,14 @@ dependencies = [ "futures-core", "futures-util", "h2 0.2.7", - "http 0.2.1", + "http 0.2.3", "http-body 0.3.1", "httparse", "httpdate", "itoa", - "pin-project 1.0.2", + "pin-project 1.0.4", "socket2", - "tokio 0.2.23", + "tokio 0.2.25", "tower-service", "tracing", "want 0.3.0", @@ -2385,7 +2535,7 @@ dependencies = [ "log", "rustls 0.18.1", "rustls-native-certs", - "tokio 0.2.23", + "tokio 0.2.25", "tokio-rustls", "webpki", ] @@ -2435,12 +2585,12 @@ dependencies = [ [[package]] name = "if-watch" -version = "0.1.7" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16d7c5e361e6b05c882b4847dd98992534cebc6fcde7f4bc98225bcf10fd6d0d" +checksum = "97b8538953a3f0d0d3868f0a706eb4273535e10d72acb5c82c1c23ae48835c85" dependencies = [ "async-io", - "futures 0.3.9", + "futures 0.3.12", "futures-lite", "if-addrs", "ipnet", @@ -2451,9 +2601,9 @@ dependencies = [ [[package]] name = "impl-codec" -version = "0.4.2" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1be51a921b067b0eaca2fad532d9400041561aa922221cc65f95a85641c6bf53" +checksum = "df170efa359aebdd5cb7fe78edcc67107748e4737bdca8a8fb40d15ea7a877ed" dependencies = [ "parity-scale-codec", ] @@ -2469,20 +2619,9 @@ dependencies = [ [[package]] name = "impl-trait-for-tuples" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ef5550a42e3740a0e71f909d4c861056a284060af885ae7aa6242820f920d9d" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "impl-trait-for-tuples" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f65a8ecf74feeacdab8d38cb129e550ca871cccaa7d1921d8636ecd75534903" +checksum = "d5dacb10c5b3bb92d46ba347505a9041e676bb20ad220101326bffb0c93031ee" dependencies = [ "proc-macro2", "quote", @@ -2491,20 +2630,20 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55e2e4c765aa53a0424761bf9f41aa7a6ac1efa87238f59560640e27fca028f2" +checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b" dependencies = [ - "autocfg 1.0.1", + "autocfg", "hashbrown", "serde", ] [[package]] name = "instant" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb1fc4429a33e1f80d41dc9fea4d108a88bec1de8053878898ae448a0b52f613" +checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" dependencies = [ "cfg-if 1.0.0", "js-sys", @@ -2527,7 +2666,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64fa110ec7b8f493f416eed552740d10e7030ad5f63b2308f82c9608ec2df275" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "futures-timer 2.0.2", ] @@ -2554,27 +2693,27 @@ checksum = "47be2f14c678be2fdcab04ab1171db51b2762ce6f0a8ee87c8dd4a04ed216135" [[package]] name = "itertools" -version = "0.8.2" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484" +checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" dependencies = [ "either", ] [[package]] name = "itertools" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" +checksum = "37d572918e350e82412fe766d24b15e6682fb2ed2bbe018280caa810397cb319" dependencies = [ "either", ] [[package]] name = "itoa" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] name = "jobserver" @@ -2607,7 +2746,7 @@ dependencies = [ "jsonrpc-pubsub", "log", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "url 1.7.2", ] @@ -2621,7 +2760,7 @@ dependencies = [ "log", "serde", "serde_derive", - "serde_json 1.0.59", + "serde_json 1.0.61", ] [[package]] @@ -2755,19 +2894,19 @@ dependencies = [ [[package]] name = "kvdb" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92312348daade49976a6dc59263ad39ed54f840aacb5664874f7c9aa16e5f848" +checksum = "8891bd853eff90e33024195d79d578dc984c82f9e0715fcd2b525a0c19d52811" dependencies = [ "parity-util-mem", - "smallvec 1.5.0", + "smallvec 1.6.1", ] [[package]] name = "kvdb-memorydb" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "986052a8d16c692eaebe775391f9a3ac26714f3907132658500b601dec94c8c2" +checksum = "30a0da8e08caf08d384a620ec19bb6c9b85c84137248e202617fb91881f25912" dependencies = [ "kvdb", "parity-util-mem", @@ -2776,9 +2915,9 @@ dependencies = [ [[package]] name = "kvdb-rocksdb" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d92c36be64baba5ea549116ff0d7ffd445456a7be8aaee21ec05882b980cd11" +checksum = "34446c373ccc494c2124439281c198c7636ccdc2752c06722bbffd56d459c1e4" dependencies = [ "fs-swap", "kvdb", @@ -2789,16 +2928,16 @@ dependencies = [ "parking_lot 0.11.1", "regex", "rocksdb", - "smallvec 1.5.0", + "smallvec 1.6.1", ] [[package]] name = "kvdb-web" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7bfe11b3202691673766b1224c432996f6b8047db17ceb743675bef3404e714" +checksum = "eb1e98ba343d0b35f9009a8844cd2b87fa3192f7e79033ac05b00aeae0f3b0b5" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "js-sys", "kvdb", "kvdb-memorydb", @@ -2830,9 +2969,9 @@ checksum = "3576a87f2ba00f6f106fdfcd16db1d698d648a26ad8e0573cad8537c3c362d2a" [[package]] name = "libc" -version = "0.2.81" +version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" +checksum = "1cca32fa0182e8c0989459524dc356b8f2b5c10f1b9eb521b7d182c03cf8c5ff" [[package]] name = "libloading" @@ -2852,16 +2991,15 @@ checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" [[package]] name = "libp2p" -version = "0.34.0" +version = "0.35.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5133112ce42be9482f6a87be92a605dd6bbc9e93c297aee77d172ff06908f3a" +checksum = "adc225a49973cf9ab10d0cdd6a4b8f0cda299df9b760824bbb623f15f8f0c95a" dependencies = [ "atomic", "bytes 1.0.1", - "futures 0.3.9", + "futures 0.3.12", "lazy_static", "libp2p-core", - "libp2p-core-derive", "libp2p-deflate", "libp2p-dns", "libp2p-floodsub", @@ -2876,6 +3014,7 @@ dependencies = [ "libp2p-pnet", "libp2p-request-response", "libp2p-swarm", + "libp2p-swarm-derive", "libp2p-tcp", "libp2p-uds", "libp2p-wasm-ext", @@ -2883,23 +3022,23 @@ dependencies = [ "libp2p-yamux", "parity-multiaddr", "parking_lot 0.11.1", - "pin-project 1.0.2", - "smallvec 1.5.0", + "pin-project 1.0.4", + "smallvec 1.6.1", "wasm-timer", ] [[package]] name = "libp2p-core" -version = "0.27.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dad04d3cef6c1df366a6ab58c9cf8b06497699e335d83ac2174783946ff847d6" +checksum = "8a2d56aadc2c2bf22cd7797f86e56a65b5b3994a0136b65be3106938acae7a26" dependencies = [ "asn1_der", "bs58", "ed25519-dalek", "either", "fnv", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "lazy_static", "libsecp256k1", @@ -2908,38 +3047,28 @@ dependencies = [ "multistream-select", "parity-multiaddr", "parking_lot 0.11.1", - "pin-project 1.0.2", - "prost 0.7.0", + "pin-project 1.0.4", + "prost", "prost-build", "rand 0.7.3", "ring", "rw-stream-sink", - "sha2 0.9.2", - "smallvec 1.5.0", + "sha2 0.9.3", + "smallvec 1.6.1", "thiserror", - "unsigned-varint 0.6.0", + "unsigned-varint 0.7.0", "void", "zeroize", ] -[[package]] -name = "libp2p-core-derive" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4bc40943156e42138d22ed3c57ff0e1a147237742715937622a99b10fbe0156" -dependencies = [ - "quote", - "syn", -] - [[package]] name = "libp2p-deflate" -version = "0.27.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "935893c0e5b6ca6ef60d5225aab9182f97c8c5671df2fa9dee8f4ed72a90e6eb" +checksum = "6d42eed63305f0420736fa487f9acef720c4528bd7852a6a760f5ccde4813345" dependencies = [ "flate2", - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", ] @@ -2949,7 +3078,7 @@ version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5153b6db68fd4baa3b304e377db744dd8fea8ff4e4504509ee636abcde88d3e3" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "log", ] @@ -2962,39 +3091,39 @@ checksum = "b3c63dfa06581b24b1d12bf9815b43689a784424be217d6545c800c7c75a207f" dependencies = [ "cuckoofilter", "fnv", - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "libp2p-swarm", "log", - "prost 0.7.0", + "prost", "prost-build", "rand 0.7.3", - "smallvec 1.5.0", + "smallvec 1.6.1", ] [[package]] name = "libp2p-gossipsub" -version = "0.27.0" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12451ba9493e87c91baf2a6dffce9ddf1fbc807a0861532d7cf477954f8ebbee" +checksum = "502dc5fcbfec4aa1c63ef3f7307ffe20e90c1a1387bf23ed0bec087f2dde58a1" dependencies = [ - "asynchronous-codec", + "asynchronous-codec 0.6.0", "base64 0.13.0", "byteorder", "bytes 1.0.1", "fnv", - "futures 0.3.9", + "futures 0.3.12", "hex_fmt", "libp2p-core", "libp2p-swarm", "log", - "prost 0.7.0", + "prost", "prost-build", "rand 0.7.3", "regex", - "sha2 0.9.2", - "smallvec 1.5.0", - "unsigned-varint 0.6.0", + "sha2 0.9.3", + "smallvec 1.6.1", + "unsigned-varint 0.7.0", "wasm-timer", ] @@ -3004,79 +3133,79 @@ version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b40fb36a059b7a8cce1514bd8b546fa612e006c9937caa7f5950cb20021fe91e" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "libp2p-swarm", "log", - "prost 0.7.0", + "prost", "prost-build", - "smallvec 1.5.0", + "smallvec 1.6.1", "wasm-timer", ] [[package]] name = "libp2p-kad" -version = "0.28.0" +version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "456f5de8e283d7800ca848b9b9a4e2a578b790bd8ae582b885e831353cf0e5df" +checksum = "cf3da6c9acbcc05f93235d201d7d45ef4e8b88a45d8836f98becd8b4d443f066" dependencies = [ "arrayvec 0.5.2", - "asynchronous-codec", + "asynchronous-codec 0.6.0", "bytes 1.0.1", "either", "fnv", - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "libp2p-swarm", "log", - "prost 0.7.0", + "prost", "prost-build", "rand 0.7.3", - "sha2 0.9.2", - "smallvec 1.5.0", + "sha2 0.9.3", + "smallvec 1.6.1", "uint", - "unsigned-varint 0.6.0", + "unsigned-varint 0.7.0", "void", "wasm-timer", ] [[package]] name = "libp2p-mdns" -version = "0.28.0" +version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b974db63233fc0e199f4ede7794294aae285c96f4b6010f853eac4099ef08590" +checksum = "0e9e6374814d1b118d97ccabdfc975c8910bd16dc38a8bc058eeb08bf2080fe1" dependencies = [ "async-io", "data-encoding", "dns-parser", - "futures 0.3.9", + "futures 0.3.12", "if-watch", "lazy_static", "libp2p-core", "libp2p-swarm", "log", "rand 0.7.3", - "smallvec 1.5.0", + "smallvec 1.6.1", "socket2", "void", ] [[package]] name = "libp2p-mplex" -version = "0.27.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2705dc94b01ab9e3779b42a09bbf3712e637ed213e875c30face247291a85af0" +checksum = "350ce8b3923594aedabd5d6e3f875d058435052a29c3f32df378bc70d10be464" dependencies = [ - "asynchronous-codec", + "asynchronous-codec 0.6.0", "bytes 1.0.1", - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "log", "nohash-hasher", "parking_lot 0.11.1", "rand 0.7.3", - "smallvec 1.5.0", - "unsigned-varint 0.6.0", + "smallvec 1.6.1", + "unsigned-varint 0.7.0", ] [[package]] @@ -3086,15 +3215,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4aca322b52a0c5136142a7c3971446fb1e9964923a526c9cc6ef3b7c94e57778" dependencies = [ "bytes 1.0.1", - "curve25519-dalek 3.0.0", - "futures 0.3.9", + "curve25519-dalek 3.0.2", + "futures 0.3.12", "lazy_static", "libp2p-core", "log", - "prost 0.7.0", + "prost", "prost-build", "rand 0.7.3", - "sha2 0.9.2", + "sha2 0.9.3", "snow", "static_assertions", "x25519-dalek", @@ -3107,7 +3236,7 @@ version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f3813276d0708c8db0f500d8beda1bda9ad955723b9cb272c41f4727256f73c" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "libp2p-swarm", "log", @@ -3118,18 +3247,18 @@ dependencies = [ [[package]] name = "libp2p-plaintext" -version = "0.27.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48e8c1ec305c9949351925cdc7196b9570f4330477f5e47fbf5bb340b57e26ed" +checksum = "9d58defcadb646ae4b033e130b48d87410bf76394dc3335496cae99dac803e61" dependencies = [ - "asynchronous-codec", + "asynchronous-codec 0.6.0", "bytes 1.0.1", - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "log", - "prost 0.7.0", + "prost", "prost-build", - "unsigned-varint 0.6.0", + "unsigned-varint 0.7.0", "void", ] @@ -3139,9 +3268,9 @@ version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce3374f3b28162db9d3442c9347c4f14cb01e8290052615c7d341d40eae0599" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "log", - "pin-project 1.0.2", + "pin-project 1.0.4", "rand 0.7.3", "salsa20", "sha3", @@ -3149,48 +3278,58 @@ dependencies = [ [[package]] name = "libp2p-request-response" -version = "0.9.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d37637a4b33b5390322ccc068a33897d0aa541daf4fec99f6a7efbf37295346e" +checksum = "10e5552827c33d8326502682da73a0ba4bfa40c1b55b216af3c303f32169dd89" dependencies = [ "async-trait", "bytes 1.0.1", - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "libp2p-swarm", "log", "lru", "minicbor", "rand 0.7.3", - "smallvec 1.5.0", - "unsigned-varint 0.6.0", + "smallvec 1.6.1", + "unsigned-varint 0.7.0", "wasm-timer", ] [[package]] name = "libp2p-swarm" -version = "0.27.0" +version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22ea8c69839a0e593c8c6a24282cb234d48ac37be4153183f4914e00f5303e75" +checksum = "7955b973e1fd2bd61ffd43ce261c1223f61f4aacd5bae362a924993f9a25fd98" dependencies = [ "either", - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "log", "rand 0.7.3", - "smallvec 1.5.0", + "smallvec 1.6.1", "void", "wasm-timer", ] +[[package]] +name = "libp2p-swarm-derive" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c564ebaa36a64839f51eaddb0243aaaa29ce64affb56129193cc3248b72af273" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "libp2p-tcp" -version = "0.27.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dbd3d7076a478ac5a6aca55e74bdc250ac539b95de09b9d09915e0b8d01a6b2" +checksum = "88a5aef80e519a6cb8e2663605142f97baaaea1a252eecbf8756184765f7471b" dependencies = [ "async-io", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "if-watch", "ipnet", @@ -3207,7 +3346,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "80ac51ce419f60be966e02103c17f67ff5dc4422ba83ba54d251d6c62a4ed487" dependencies = [ "async-std", - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "log", ] @@ -3218,7 +3357,7 @@ version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6149c46cb76935c80bc8be6ec6e3ebd5f5e1679765a255fb34331d54610f15dd" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "js-sys", "libp2p-core", "parity-send-wrapper", @@ -3233,7 +3372,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3b1c6a3431045da8b925ed83384e4c5163e14b990572307fca9c507435d4d22" dependencies = [ "either", - "futures 0.3.9", + "futures 0.3.12", "futures-rustls", "libp2p-core", "log", @@ -3246,11 +3385,11 @@ dependencies = [ [[package]] name = "libp2p-yamux" -version = "0.30.0" +version = "0.30.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "490b8b27fc40fe35212df1b6a3d14bffaa4117cbff956fdc2892168a371102ad" +checksum = "4819358c542a86ff95f6ae691efb4b94ddaf477079b01a686f5705b79bfc232a" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "libp2p-core", "parking_lot 0.11.1", "thiserror", @@ -3281,7 +3420,7 @@ dependencies = [ "hmac-drbg", "rand 0.7.3", "sha2 0.8.2", - "subtle 2.3.0", + "subtle 2.4.0", "typenum", ] @@ -3298,9 +3437,9 @@ dependencies = [ [[package]] name = "linked-hash-map" -version = "0.5.3" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a" +checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" [[package]] name = "linked_hash_set" @@ -3359,31 +3498,19 @@ dependencies = [ [[package]] name = "log" -version = "0.4.11" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ - "cfg-if 0.1.10", -] - -[[package]] -name = "loom" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0e8460f2f2121162705187214720353c517b97bdfb3494c0b1e33d83ebe4bed" -dependencies = [ - "cfg-if 0.1.10", - "generator", - "scoped-tls", - "serde", - "serde_json 1.0.59", + "cfg-if 1.0.0", + "value-bag", ] [[package]] name = "lru" -version = "0.6.3" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3aae342b73d57ad0b8b364bd12584819f2c1fe9114285dfcf8b0722607671635" +checksum = "1f374d42cdfc1d7dbf3d3dec28afab2eb97ffbf43a3234d795b5986dbf4b90ba" dependencies = [ "hashbrown", ] @@ -3420,9 +3547,9 @@ checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" [[package]] name = "matrixmultiply" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4f7ec66360130972f34830bfad9ef05c6610a43938a467bcc9ab9369ab3478f" +checksum = "916806ba0031cd542105d916a97c8572e1fa6dd79c9c51e7eb43a09ec2dd84c1" dependencies = [ "rawpointer", ] @@ -3449,20 +3576,38 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "memmap2" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e73be3b7d04a0123e933fea1d50d126cc7196bbc0362c0ce426694f777194eee" +dependencies = [ + "libc", +] + +[[package]] +name = "memoffset" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043175f069eda7b85febe4a74abbaeff828d9f8b448515d3151a14a3542811aa" +dependencies = [ + "autocfg", +] + [[package]] name = "memoffset" -version = "0.5.6" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "043175f069eda7b85febe4a74abbaeff828d9f8b448515d3151a14a3542811aa" +checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87" dependencies = [ - "autocfg 1.0.1", + "autocfg", ] [[package]] name = "memory-db" -version = "0.25.0" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cbd2a22f201c03cc1706a727842490abfea17b7b53260358239828208daba3c" +checksum = "814bbecfc0451fc314eeea34f05bbcd5b98a7ad7af37faee088b86a1e633f1d4" dependencies = [ "hash-db", "hashbrown", @@ -3477,9 +3622,9 @@ checksum = "71d96e3f3c0b6325d8ccd83c33b28acb183edcb6c67938ba104ec546854b0882" [[package]] name = "merlin" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6feca46f4fa3443a01769d768727f10c10a20fdb65e52dc16a81f0c8269bb78" +checksum = "4e261cf0f8b3c42ded9f7d2bb59dea03aa52bc8a1cbc7482f9fc3fd1229d3b42" dependencies = [ "byteorder", "keccak", @@ -3489,18 +3634,18 @@ dependencies = [ [[package]] name = "minicbor" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0164190d1771b1458c3742075b057ed55d25cd9dfb930aade99315a1eb1fe12d" +checksum = "3265a9f5210bb726f81ef9c456ae0aff5321cd95748c0e71889b0e19d8f0332b" dependencies = [ "minicbor-derive", ] [[package]] name = "minicbor-derive" -version = "0.6.0" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e071b3159835ee91df62dbdbfdd7ec366b7ea77c838f43aff4acda6b61bcfb9" +checksum = "130b9455e28a3f308f6579671816a6f2621e2e0cbf55dc2f886345bef699481e" dependencies = [ "proc-macro2", "quote", @@ -3514,14 +3659,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f2d26ec3309788e423cfbf68ad1800f061638098d76a83681af979dc4eda19d" dependencies = [ "adler", - "autocfg 1.0.1", + "autocfg", ] [[package]] name = "mio" -version = "0.6.22" +version = "0.6.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430" +checksum = "4afd66f5b91bf2a3bc13fad0e21caedac168ca4c707504e75585648ae80e4cc4" dependencies = [ "cfg-if 0.1.10", "fuchsia-zircon", @@ -3530,7 +3675,7 @@ dependencies = [ "kernel32-sys", "libc", "log", - "miow 0.2.1", + "miow 0.2.2", "net2", "slab", "winapi 0.2.8", @@ -3573,9 +3718,9 @@ dependencies = [ [[package]] name = "miow" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" +checksum = "ebd808424166322d4a38da87083bfddd3ac4c131334ed55856112eb06d46944d" dependencies = [ "kernel32-sys", "net2", @@ -3599,24 +3744,39 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0debeb9fcf88823ea64d64e4a815ab1643f33127d995978e099942ce38f25238" +[[package]] +name = "multibase" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b78c60039650ff12e140ae867ef5299a58e19dded4d334c849dc7177083667e2" +dependencies = [ + "base-x", + "data-encoding", + "data-encoding-macro", +] + [[package]] name = "multihash" -version = "0.13.1" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb63389ee5fcd4df3f8727600f4a0c3df53c541f0ed4e8b50a9ae51a80fc1efe" +checksum = "4dac63698b887d2d929306ea48b63760431ff8a24fac40ddb22f9c7f49fb7cab" dependencies = [ + "blake2b_simd", + "blake2s_simd", + "blake3", "digest 0.9.0", "generic-array 0.14.4", "multihash-derive", - "sha2 0.9.2", + "sha2 0.9.3", + "sha3", "unsigned-varint 0.5.1", ] [[package]] name = "multihash-derive" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f5653449cd45d502a53480ee08d7a599e8f4893d2bacb33c63d65bc20af6c1a" +checksum = "85ee3c48cb9d9b275ad967a0e96715badc13c6029adb92f34fa17b9ff28fd81f" dependencies = [ "proc-macro-crate", "proc-macro-error", @@ -3639,10 +3799,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10ddc0eb0117736f19d556355464fc87efc8ad98b29e3fd84f02531eb6e90840" dependencies = [ "bytes 1.0.1", - "futures 0.3.9", + "futures 0.3.12", "log", - "pin-project 1.0.2", - "smallvec 1.5.0", + "pin-project 1.0.4", + "smallvec 1.6.1", "unsigned-varint 0.6.0", ] @@ -3685,9 +3845,9 @@ dependencies = [ [[package]] name = "net2" -version = "0.2.35" +version = "0.2.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ebc3ec692ed7c9a255596c67808dee269f64655d8baf7b4f0638e51ba1d6853" +checksum = "391630d12b68002ae1e25e8f974306474966550ad82dac6886fb8910c19568ae" dependencies = [ "cfg-if 0.1.10", "libc", @@ -3696,15 +3856,14 @@ dependencies = [ [[package]] name = "nix" -version = "0.17.0" +version = "0.19.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" +checksum = "b2ccba0cfe4fdf15982d1674c69b1fd80bad427d293849982668dfe454bd61f2" dependencies = [ "bitflags", "cc", - "cfg-if 0.1.10", + "cfg-if 1.0.0", "libc", - "void", ] [[package]] @@ -3713,7 +3872,7 @@ version = "0.8.0" dependencies = [ "derive_more", "fs_extra", - "futures 0.3.9", + "futures 0.3.12", "hash-db", "hex", "kvdb", @@ -3731,7 +3890,7 @@ dependencies = [ "sc-client-api", "sc-transaction-pool", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-consensus", "sp-core", "sp-inherents", @@ -3749,14 +3908,14 @@ dependencies = [ name = "node-browser-testing" version = "2.0.0" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "jsonrpc-core", "libp2p", "node-cli", "sc-rpc-api", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test", @@ -3767,10 +3926,11 @@ name = "node-cli" version = "2.0.0" dependencies = [ "assert_cmd", + "async-std", "frame-benchmarking-cli", "frame-support", "frame-system", - "futures 0.3.9", + "futures 0.3.12", "hex-literal", "log", "nix", @@ -3815,7 +3975,8 @@ dependencies = [ "sc-tracing", "sc-transaction-pool", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", + "soketto", "sp-authority-discovery", "sp-consensus", "sp-consensus-babe", @@ -3834,6 +3995,7 @@ dependencies = [ "substrate-build-script-utils", "substrate-frame-cli", "tempfile", + "try-runtime-cli", "wasm-bindgen", "wasm-bindgen-futures", ] @@ -3861,6 +4023,7 @@ dependencies = [ "parity-scale-codec", "sc-executor", "sp-application-crypto", + "sp-consensus-babe", "sp-core", "sp-externalities", "sp-io", @@ -3956,6 +4119,7 @@ dependencies = [ "frame-system", "frame-system-benchmarking", "frame-system-rpc-runtime-api", + "frame-try-runtime", "hex-literal", "node-primitives", "pallet-authority-discovery", @@ -3971,6 +4135,7 @@ dependencies = [ "pallet-contracts-rpc-runtime-api", "pallet-ddc-metrics-offchain-worker", "pallet-democracy", + "pallet-election-provider-multi-phase", "pallet-elections-phragmen", "pallet-erc20", "pallet-erc721", @@ -4028,7 +4193,7 @@ dependencies = [ "frame-support", "frame-system", "fs_extra", - "futures 0.3.9", + "futures 0.3.12", "log", "node-executor", "node-primitives", @@ -4092,7 +4257,7 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" dependencies = [ - "autocfg 1.0.1", + "autocfg", "num-integer", "num-traits", ] @@ -4103,7 +4268,7 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95" dependencies = [ - "autocfg 1.0.1", + "autocfg", "num-traits", ] @@ -4113,7 +4278,7 @@ version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" dependencies = [ - "autocfg 1.0.1", + "autocfg", "num-traits", ] @@ -4123,7 +4288,7 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" dependencies = [ - "autocfg 1.0.1", + "autocfg", "num-bigint", "num-integer", "num-traits", @@ -4135,7 +4300,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" dependencies = [ - "autocfg 1.0.1", + "autocfg", "libm", ] @@ -4151,26 +4316,19 @@ dependencies = [ [[package]] name = "object" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cbca9424c482ee628fa549d9c812e2cd22f1180b9222c9200fdfa6eb31aecb2" - -[[package]] -name = "object" -version = "0.20.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" +checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" dependencies = [ "crc32fast", "indexmap", - "wasmparser 0.57.0", ] [[package]] name = "object" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" +checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4" [[package]] name = "once_cell" @@ -4183,9 +4341,9 @@ dependencies = [ [[package]] name = "oorandom" -version = "11.1.2" +version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a170cebd8021a008ea92e4db85a72f80b35df514ec664b296fdcbb654eac0b2c" +checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" [[package]] name = "opaque-debug" @@ -4225,7 +4383,7 @@ dependencies = [ [[package]] name = "pallet-assets" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4241,7 +4399,7 @@ dependencies = [ [[package]] name = "pallet-atomic-swap" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -4256,7 +4414,7 @@ dependencies = [ [[package]] name = "pallet-aura" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -4269,7 +4427,6 @@ dependencies = [ "sp-application-crypto", "sp-consensus-aura", "sp-core", - "sp-inherents", "sp-io", "sp-runtime", "sp-std", @@ -4278,7 +4435,7 @@ dependencies = [ [[package]] name = "pallet-authority-discovery" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -4296,11 +4453,11 @@ dependencies = [ [[package]] name = "pallet-authorship" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "parity-scale-codec", "serde", "sp-authorship", @@ -4313,7 +4470,7 @@ dependencies = [ [[package]] name = "pallet-babe" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4331,7 +4488,7 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-vrf", "sp-core", - "sp-inherents", + "sp-election-providers", "sp-io", "sp-runtime", "sp-session", @@ -4342,7 +4499,7 @@ dependencies = [ [[package]] name = "pallet-balances" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4358,7 +4515,7 @@ dependencies = [ [[package]] name = "pallet-bounties" -version = "2.0.0" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4376,11 +4533,12 @@ dependencies = [ [[package]] name = "pallet-cere-ddc" -version = "4.0.0" +version = "4.1.0" dependencies = [ "frame-support", "frame-system", "parity-scale-codec", + "serde", "sp-core", "sp-io", "sp-runtime", @@ -4394,18 +4552,20 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "lite-json", "pallet-balances", "parity-scale-codec", "serde", "sp-core", "sp-io", + "sp-keystore", "sp-runtime", "sp-std", ] [[package]] name = "pallet-collective" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4436,11 +4596,11 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "parity-wasm 0.41.0", - "paste 1.0.3", + "paste 1.0.4", "pretty_assertions", "pwasm-utils 0.16.0", "rand 0.7.3", - "rand_pcg 0.2.1", + "rand_pcg", "serde", "sp-core", "sp-io", @@ -4481,7 +4641,7 @@ dependencies = [ "pallet-contracts-rpc-runtime-api", "parity-scale-codec", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-api", "sp-blockchain", "sp-core", @@ -4527,7 +4687,7 @@ dependencies = [ [[package]] name = "pallet-democracy" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4545,9 +4705,35 @@ dependencies = [ "substrate-test-utils", ] +[[package]] +name = "pallet-election-provider-multi-phase" +version = "3.0.0" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "hex-literal", + "pallet-balances", + "parity-scale-codec", + "parking_lot 0.11.1", + "paste 1.0.4", + "rand 0.7.3", + "serde", + "sp-arithmetic", + "sp-core", + "sp-election-providers", + "sp-io", + "sp-npos-elections", + "sp-runtime", + "sp-std", + "sp-tracing", + "static_assertions", + "substrate-test-utils", +] + [[package]] name = "pallet-elections" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -4639,6 +4825,7 @@ dependencies = [ "frame-support", "frame-system", "parity-scale-codec", + "serde", "sp-core", "sp-io", "sp-runtime", @@ -4648,7 +4835,7 @@ dependencies = [ [[package]] name = "pallet-grandpa" -version = "2.0.1" +version = "3.0.0" dependencies = [ "finality-grandpa", "frame-benchmarking", @@ -4665,6 +4852,7 @@ dependencies = [ "serde", "sp-application-crypto", "sp-core", + "sp-election-providers", "sp-finality-grandpa", "sp-io", "sp-keyring", @@ -4676,7 +4864,7 @@ dependencies = [ [[package]] name = "pallet-identity" -version = "2.0.1" +version = "3.0.0" dependencies = [ "enumflags2", "frame-benchmarking", @@ -4693,7 +4881,7 @@ dependencies = [ [[package]] name = "pallet-im-online" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4712,7 +4900,7 @@ dependencies = [ [[package]] name = "pallet-indices" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4729,13 +4917,14 @@ dependencies = [ [[package]] name = "pallet-lottery" -version = "2.0.0" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", "frame-system", "pallet-balances", "parity-scale-codec", + "serde", "sp-core", "sp-io", "sp-runtime", @@ -4744,7 +4933,7 @@ dependencies = [ [[package]] name = "pallet-membership" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -4758,14 +4947,15 @@ dependencies = [ [[package]] name = "pallet-mmr" -version = "2.0.1" +version = "3.0.0" dependencies = [ "ckb-merkle-mountain-range", - "env_logger 0.5.13", + "env_logger 0.8.2", "frame-benchmarking", "frame-support", "frame-system", "hex-literal", + "pallet-mmr-primitives", "parity-scale-codec", "serde", "sp-core", @@ -4774,9 +4964,24 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-mmr-primitives" +version = "3.0.0" +dependencies = [ + "frame-support", + "frame-system", + "hex-literal", + "parity-scale-codec", + "serde", + "sp-api", + "sp-core", + "sp-runtime", + "sp-std", +] + [[package]] name = "pallet-multisig" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4792,7 +4997,7 @@ dependencies = [ [[package]] name = "pallet-nicks" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -4821,7 +5026,7 @@ dependencies = [ [[package]] name = "pallet-offences" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -4837,7 +5042,7 @@ dependencies = [ [[package]] name = "pallet-offences-benchmarking" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4854,6 +5059,7 @@ dependencies = [ "parity-scale-codec", "serde", "sp-core", + "sp-election-providers", "sp-io", "sp-runtime", "sp-staking", @@ -4862,7 +5068,7 @@ dependencies = [ [[package]] name = "pallet-proxy" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4879,7 +5085,7 @@ dependencies = [ [[package]] name = "pallet-randomness-collective-flip" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -4894,7 +5100,7 @@ dependencies = [ [[package]] name = "pallet-recovery" -version = "2.0.1" +version = "3.0.0" dependencies = [ "enumflags2", "frame-support", @@ -4910,7 +5116,7 @@ dependencies = [ [[package]] name = "pallet-scheduler" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4926,7 +5132,7 @@ dependencies = [ [[package]] name = "pallet-scored-pool" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -4941,11 +5147,11 @@ dependencies = [ [[package]] name = "pallet-session" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", - "impl-trait-for-tuples 0.1.3", + "impl-trait-for-tuples", "lazy_static", "pallet-timestamp", "parity-scale-codec", @@ -4962,7 +5168,7 @@ dependencies = [ [[package]] name = "pallet-session-benchmarking" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -4976,6 +5182,7 @@ dependencies = [ "rand 0.7.3", "serde", "sp-core", + "sp-election-providers", "sp-io", "sp-runtime", "sp-session", @@ -4984,7 +5191,7 @@ dependencies = [ [[package]] name = "pallet-society" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -5000,7 +5207,7 @@ dependencies = [ [[package]] name = "pallet-staking" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -5017,6 +5224,7 @@ dependencies = [ "serde", "sp-application-crypto", "sp-core", + "sp-election-providers", "sp-io", "sp-npos-elections", "sp-runtime", @@ -5042,7 +5250,9 @@ dependencies = [ "pallet-staking-reward-curve", "pallet-timestamp", "parity-scale-codec", + "serde", "sp-core", + "sp-election-providers", "sp-io", "sp-npos-elections", "sp-runtime", @@ -5051,7 +5261,7 @@ dependencies = [ [[package]] name = "pallet-staking-reward-curve" -version = "2.0.1" +version = "3.0.0" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -5062,7 +5272,7 @@ dependencies = [ [[package]] name = "pallet-sudo" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", @@ -5076,12 +5286,12 @@ dependencies = [ [[package]] name = "pallet-timestamp" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", "frame-system", - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "parity-scale-codec", "serde", "sp-core", @@ -5094,7 +5304,7 @@ dependencies = [ [[package]] name = "pallet-tips" -version = "2.0.0" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -5112,15 +5322,15 @@ dependencies = [ [[package]] name = "pallet-transaction-payment" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", "pallet-balances", "parity-scale-codec", "serde", - "serde_json 1.0.59", - "smallvec 1.5.0", + "serde_json 1.0.61", + "smallvec 1.6.1", "sp-core", "sp-io", "sp-runtime", @@ -5130,7 +5340,7 @@ dependencies = [ [[package]] name = "pallet-transaction-payment-rpc" -version = "2.0.1" +version = "3.0.0" dependencies = [ "jsonrpc-core", "jsonrpc-core-client", @@ -5146,7 +5356,7 @@ dependencies = [ [[package]] name = "pallet-transaction-payment-rpc-runtime-api" -version = "2.0.1" +version = "3.0.0" dependencies = [ "pallet-transaction-payment", "parity-scale-codec", @@ -5156,12 +5366,12 @@ dependencies = [ [[package]] name = "pallet-treasury" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", "frame-system", - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "pallet-balances", "parity-scale-codec", "serde", @@ -5174,7 +5384,7 @@ dependencies = [ [[package]] name = "pallet-utility" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -5190,7 +5400,7 @@ dependencies = [ [[package]] name = "pallet-vesting" -version = "2.0.1" +version = "3.0.0" dependencies = [ "enumflags2", "frame-benchmarking", @@ -5209,23 +5419,25 @@ dependencies = [ [[package]] name = "parity-db" -version = "0.1.2" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00d595e372d119261593297debbe4193811a4dc811d2a1ccbb8caaa6666ad7ab" +checksum = "111e193c96758d476d272093a853882668da17489f76bf4361b8decae0b6c515" dependencies = [ "blake2-rfc", "crc32fast", + "hex", "libc", "log", - "memmap", - "parking_lot 0.10.2", + "memmap2", + "parking_lot 0.11.1", + "rand 0.8.3", ] [[package]] name = "parity-multiaddr" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bfda2e46fc5e14122649e2645645a81ee5844e0fb2e727ef560cc71a8b2d801" +checksum = "d2c6805f98667a3828afb2ec2c396a8d610497e8d546f5447188aae47c5a79ec" dependencies = [ "arrayref", "bs58", @@ -5235,15 +5447,15 @@ dependencies = [ "percent-encoding 2.1.0", "serde", "static_assertions", - "unsigned-varint 0.6.0", + "unsigned-varint 0.7.0", "url 2.2.0", ] [[package]] name = "parity-scale-codec" -version = "1.3.6" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79602888a81ace83e3d1d4b2873286c1f5f906c84db667594e8db8da3506c383" +checksum = "75c823fdae1bb5ff5708ee61a62697e6296175dc671710876871c853f48592b3" dependencies = [ "arrayvec 0.5.2", "bitvec", @@ -5254,9 +5466,9 @@ dependencies = [ [[package]] name = "parity-scale-codec-derive" -version = "1.2.2" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "198db82bb1c18fc00176004462dd809b2a6d851669550aa17af6dacd21ae0c14" +checksum = "9029e65297c7fd6d7013f0579e193ec2b34ae78eabca854c9417504ad8a2d214" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -5291,17 +5503,17 @@ dependencies = [ [[package]] name = "parity-util-mem" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f17f15cb05897127bf36a240085a1f0bbef7bce3024849eccf7f93f6171bc27" +checksum = "664a8c6b8e62d8f9f2f937e391982eb433ab285b4cd9545b342441e04a906e42" dependencies = [ "cfg-if 1.0.0", "hashbrown", - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "parity-util-mem-derive", "parking_lot 0.11.1", "primitive-types", - "smallvec 1.5.0", + "smallvec 1.6.1", "winapi 0.3.9", ] @@ -5384,7 +5596,7 @@ checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" dependencies = [ "instant", "lock_api 0.4.2", - "parking_lot_core 0.8.0", + "parking_lot_core 0.8.2", ] [[package]] @@ -5394,11 +5606,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" dependencies = [ "cfg-if 0.1.10", - "cloudabi 0.0.3", + "cloudabi", "libc", - "redox_syscall", + "redox_syscall 0.1.57", "rustc_version", - "smallvec 0.6.13", + "smallvec 0.6.14", "winapi 0.3.9", ] @@ -5409,25 +5621,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" dependencies = [ "cfg-if 0.1.10", - "cloudabi 0.0.3", + "cloudabi", "libc", - "redox_syscall", - "smallvec 1.5.0", + "redox_syscall 0.1.57", + "smallvec 1.6.1", "winapi 0.3.9", ] [[package]] name = "parking_lot_core" -version = "0.8.0" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c361aa727dd08437f2f1447be8b59a33b0edd15e0fcee698f935613d9efbca9b" +checksum = "9ccb628cad4f84851442432c60ad8e1f607e29752d0bf072cbd0baf28aa34272" dependencies = [ - "cfg-if 0.1.10", - "cloudabi 0.1.0", + "cfg-if 1.0.0", "instant", "libc", - "redox_syscall", - "smallvec 1.5.0", + "redox_syscall 0.1.57", + "smallvec 1.6.1", "winapi 0.3.9", ] @@ -5443,9 +5654,9 @@ dependencies = [ [[package]] name = "paste" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7151b083b0664ed58ed669fcdd92f01c3d2fdbf10af4931a301474950b52bfa9" +checksum = "c5d65c4d95931acda4498f675e332fcbdc9a06705cd07086c510e9b6009cd1c1" [[package]] name = "paste-impl" @@ -5563,11 +5774,11 @@ dependencies = [ [[package]] name = "pin-project" -version = "1.0.2" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ccc2237c2c489783abd8c4c80e5450fc0e98644555b1364da68cc29aa151ca7" +checksum = "95b70b68509f17aa2857863b6fa00bf21fc93674c7a8893de2f469f6aa7ca2f2" dependencies = [ - "pin-project-internal 1.0.2", + "pin-project-internal 1.0.4", ] [[package]] @@ -5583,9 +5794,9 @@ dependencies = [ [[package]] name = "pin-project-internal" -version = "1.0.2" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8e8d2bf0b23038a4424865103a4df472855692821aab4e4f5c3312d461d9e5f" +checksum = "caa25a6393f22ce819b0f50e0be89287292fda8d425be38ee0ca14c4931d9e71" dependencies = [ "proc-macro2", "quote", @@ -5600,9 +5811,9 @@ checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b" [[package]] name = "pin-project-lite" -version = "0.2.0" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b063f57ec186e6140e2b8b6921e5f1bd89c7356dda5b33acc5401203ca6131c" +checksum = "439697af366c49a6d0a010c56a0d97685bc140ce0d377b13a2ea2aa42d64a827" [[package]] name = "pin-utils" @@ -5618,22 +5829,38 @@ checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" [[package]] name = "platforms" -version = "0.2.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "feb3b2b1033b8a60b4da6ee470325f887758c95d5320f52f9ce0df055a55940e" +checksum = "989d43012e2ca1c4a02507c67282691a0a3207f9dc67cec596b43fe925b3d325" [[package]] name = "plotters" -version = "0.2.15" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d1685fbe7beba33de0330629da9d955ac75bd54f33d7b79f9a895590124f6bb" +checksum = "45ca0ae5f169d0917a7c7f5a9c1a3d3d9598f18f529dd2b8373ed988efea307a" dependencies = [ - "js-sys", "num-traits", + "plotters-backend", + "plotters-svg", "wasm-bindgen", "web-sys", ] +[[package]] +name = "plotters-backend" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b07fffcddc1cb3a1de753caa4e4df03b79922ba43cf882acc1bdd7e8df9f4590" + +[[package]] +name = "plotters-svg" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b38a02e23bd9604b842a812063aec4ef702b57989c37b655254bb61c471ad211" +dependencies = [ + "plotters-backend", +] + [[package]] name = "polling" version = "2.0.2" @@ -5649,20 +5876,22 @@ dependencies = [ [[package]] name = "poly1305" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22ce46de8e53ee414ca4d02bfefac75d8c12fba948b76622a40b4be34dfce980" +checksum = "4b7456bc1ad2d4cf82b3a016be4c2ac48daf11bf990c1603ebd447fe6f30fca8" dependencies = [ + "cpuid-bool 0.2.0", "universal-hash", ] [[package]] name = "polyval" -version = "0.4.1" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5884790f1ce3553ad55fec37b5aaac5882e0e845a2612df744d6c85c9bf046c" +checksum = "eebcc4aa140b9abd2bc40d9c3f7ccec842679cd79045ac3a7ac698c1a064b7cd" dependencies = [ - "cfg-if 0.1.10", + "cpuid-bool 0.2.0", + "opaque-debug 0.3.0", "universal-hash", ] @@ -5674,9 +5903,9 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" [[package]] name = "predicates" -version = "1.0.5" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96bfead12e90dccead362d62bb2c90a5f6fc4584963645bc7f71a735e0b0735a" +checksum = "eeb433456c1a57cc93554dea3ce40b4c19c4057e41c55d4a0f3d84ea71c325aa" dependencies = [ "difference", "predicates-core", @@ -5684,15 +5913,15 @@ dependencies = [ [[package]] name = "predicates-core" -version = "1.0.0" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06075c3a3e92559ff8929e7a280684489ea27fe44805174c3ebd9328dcb37178" +checksum = "57e35a3326b75e49aa85f5dc6ec15b41108cf5aee58eabb1f274dd18b73c2451" [[package]] name = "predicates-tree" -version = "1.0.0" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e63c4859013b38a76eca2414c64911fba30def9e3202ac461a2d22831220124" +checksum = "15f553275e5721409451eb85e15fd9a860a6e5ab4496eb215987502b5f5391f2" dependencies = [ "predicates-core", "treeline", @@ -5712,9 +5941,9 @@ dependencies = [ [[package]] name = "primitive-types" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3824ae2c5e27160113b9e029a10ec9e3f0237bad8029f69c7724393c9fdefd8" +checksum = "2415937401cb030a2a0a4d922483f945fa068f52a7dbb22ce0fe5f2b6f6adace" dependencies = [ "fixed-hash", "impl-codec", @@ -5763,9 +5992,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" [[package]] name = "proc-macro-nested" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" +checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" [[package]] name = "proc-macro2" @@ -5778,11 +6007,11 @@ dependencies = [ [[package]] name = "prometheus" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30d70cf4412832bcac9cffe27906f4a66e450d323525e977168c70d1b36120ae" +checksum = "c8425533e7122f0c3cc7a37e6244b16ad3a2cc32ae7ac6276e2a75da0d9c200d" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "fnv", "lazy_static", "parking_lot 0.11.1", @@ -5790,16 +6019,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "prost" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce49aefe0a6144a45de32927c77bd2859a5f7677b55f220ae5b744e87389c212" -dependencies = [ - "bytes 0.5.6", - "prost-derive 0.6.1", -] - [[package]] name = "prost" version = "0.7.0" @@ -5807,7 +6026,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e6984d2f1a23009bd270b8bb56d0926810a3d483f59c987d77969e9d8e840b2" dependencies = [ "bytes 1.0.1", - "prost-derive 0.7.0", + "prost-derive", ] [[package]] @@ -5822,7 +6041,7 @@ dependencies = [ "log", "multimap", "petgraph", - "prost 0.7.0", + "prost", "prost-types", "tempfile", "which 4.0.2", @@ -5830,38 +6049,34 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.6.1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "537aa19b95acde10a12fec4301466386f757403de4cd4e5b4fa78fb5ecb18f72" +checksum = "169a15f3008ecb5160cba7d37bcd690a7601b6d30cfb87a117d45e59d52af5d4" dependencies = [ "anyhow", - "itertools 0.8.2", + "itertools 0.9.0", "proc-macro2", "quote", "syn", ] [[package]] -name = "prost-derive" +name = "prost-types" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "169a15f3008ecb5160cba7d37bcd690a7601b6d30cfb87a117d45e59d52af5d4" +checksum = "b518d7cdd93dab1d1122cf07fa9a60771836c668dde9d9e2a139f957f0d9f1bb" dependencies = [ - "anyhow", - "itertools 0.9.0", - "proc-macro2", - "quote", - "syn", + "bytes 1.0.1", + "prost", ] [[package]] -name = "prost-types" -version = "0.7.0" +name = "psm" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b518d7cdd93dab1d1122cf07fa9a60771836c668dde9d9e2a139f957f0d9f1bb" +checksum = "3abf49e5417290756acfd26501536358560c4a5cc4a0934d390939acb3e7083a" dependencies = [ - "bytes 1.0.1", - "prost 0.7.0", + "cc", ] [[package]] @@ -5900,14 +6115,13 @@ checksum = "3ac73b1112776fc109b2e61909bc46c7e1bf0d7f690ffb1676553acce16d5cda" [[package]] name = "quickcheck" -version = "0.9.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a44883e74aa97ad63db83c4bf8ca490f02b2fc02f92575e720c8551e843c945f" +checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6" dependencies = [ - "env_logger 0.7.1", + "env_logger 0.8.2", "log", - "rand 0.7.3", - "rand_core 0.5.1", + "rand 0.8.3", ] [[package]] @@ -5923,18 +6137,18 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" dependencies = [ "proc-macro2", ] [[package]] name = "radium" -version = "0.3.0" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "def50a86306165861203e7f84ecffbbdfdea79f0e51039b33de1e952358c47ac" +checksum = "643f8f41a8ebc4c5dc4515c82bb8abd397b527fc20fd681b7c011c2aee5d44fb" [[package]] name = "rand" @@ -5959,58 +6173,30 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "rand" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" -dependencies = [ - "autocfg 0.1.7", - "libc", - "rand_chacha 0.1.1", - "rand_core 0.4.2", - "rand_hc 0.1.0", - "rand_isaac", - "rand_jitter", - "rand_os", - "rand_pcg 0.1.2", - "rand_xorshift", - "winapi 0.3.9", -] - [[package]] name = "rand" version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ - "getrandom 0.1.15", + "getrandom 0.1.16", "libc", "rand_chacha 0.2.2", "rand_core 0.5.1", "rand_hc 0.2.0", - "rand_pcg 0.2.1", + "rand_pcg", ] [[package]] name = "rand" -version = "0.8.1" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c24fcd450d3fa2b592732565aa4f17a27a61c65ece4726353e000939b0edee34" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" dependencies = [ "libc", "rand_chacha 0.3.0", "rand_core 0.6.1", -] - -[[package]] -name = "rand_chacha" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" -dependencies = [ - "autocfg 0.1.7", - "rand_core 0.3.1", + "rand_hc 0.3.0", ] [[package]] @@ -6054,7 +6240,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" dependencies = [ - "getrandom 0.1.15", + "getrandom 0.1.16", ] [[package]] @@ -6063,7 +6249,7 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5" dependencies = [ - "getrandom 0.2.1", + "getrandom 0.2.2", ] [[package]] @@ -6075,15 +6261,6 @@ dependencies = [ "rand 0.7.3", ] -[[package]] -name = "rand_hc" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" -dependencies = [ - "rand_core 0.3.1", -] - [[package]] name = "rand_hc" version = "0.2.0" @@ -6094,47 +6271,12 @@ dependencies = [ ] [[package]] -name = "rand_isaac" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "rand_jitter" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" -dependencies = [ - "libc", - "rand_core 0.4.2", - "winapi 0.3.9", -] - -[[package]] -name = "rand_os" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" -dependencies = [ - "cloudabi 0.0.3", - "fuchsia-cprng", - "libc", - "rand_core 0.4.2", - "rdrand", - "winapi 0.3.9", -] - -[[package]] -name = "rand_pcg" -version = "0.1.2" +name = "rand_hc" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" dependencies = [ - "autocfg 0.1.7", - "rand_core 0.4.2", + "rand_core 0.6.1", ] [[package]] @@ -6146,20 +6288,11 @@ dependencies = [ "rand_core 0.5.1", ] -[[package]] -name = "rand_xorshift" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" -dependencies = [ - "rand_core 0.3.1", -] - [[package]] name = "raw-cpuid" -version = "7.0.3" +version = "8.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4a349ca83373cfa5d6dbb66fd76e58b2cca08da71a5f6400de0a0a6a9bceeaf" +checksum = "1fdf7d9dbd43f3d81d94a49c1c3df73cc2b3827995147e6cf7f89d4ec5483e73" dependencies = [ "bitflags", "cc", @@ -6178,7 +6311,7 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" dependencies = [ - "autocfg 1.0.1", + "autocfg", "crossbeam-deque 0.8.0", "either", "rayon-core", @@ -6192,7 +6325,7 @@ checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" dependencies = [ "crossbeam-channel", "crossbeam-deque 0.8.0", - "crossbeam-utils 0.8.0", + "crossbeam-utils 0.8.1", "lazy_static", "num_cpus", ] @@ -6212,31 +6345,50 @@ version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" +[[package]] +name = "redox_syscall" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05ec8ca9416c5ea37062b502703cd7fcb207736bc294f6e0cf367ac6fc234570" +dependencies = [ + "bitflags", +] + [[package]] name = "redox_users" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d" dependencies = [ - "getrandom 0.1.15", - "redox_syscall", + "getrandom 0.1.16", + "redox_syscall 0.1.57", "rust-argon2", ] +[[package]] +name = "redox_users" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" +dependencies = [ + "getrandom 0.2.2", + "redox_syscall 0.2.4", +] + [[package]] name = "ref-cast" -version = "1.0.3" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e17626b2f4bcf35b84bf379072a66e28cfe5c3c6ae58b38e4914bb8891dabece" +checksum = "300f2a835d808734ee295d45007adacb9ebb29dd3ae2424acfa17930cae541da" dependencies = [ "ref-cast-impl", ] [[package]] name = "ref-cast-impl" -version = "1.0.3" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c523ccaed8ac4b0288948849a350b37d3035827413c458b6a40ddb614bb4f72" +checksum = "4c38e3aecd2b21cb3959637b883bb3714bc7e43f0268b9a29d3743ee3e55cdd2" dependencies = [ "proc-macro2", "quote", @@ -6245,20 +6397,20 @@ dependencies = [ [[package]] name = "regalloc" -version = "0.0.27" +version = "0.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9ba8aaf5fe7cf307c6dbdaeed85478961d29e25e3bee5169e11b92fa9f027a8" +checksum = "571f7f397d61c4755285cd37853fe8e03271c243424a907415909379659381c5" dependencies = [ "log", "rustc-hash", - "smallvec 1.5.0", + "smallvec 1.6.1", ] [[package]] name = "regex" -version = "1.4.2" +version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c" +checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" dependencies = [ "aho-corasick", "memchr", @@ -6278,9 +6430,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.21" +version = "0.6.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189" +checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581" [[package]] name = "region" @@ -6294,6 +6446,24 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "remote-externalities" +version = "0.9.0" +dependencies = [ + "async-std", + "bincode", + "env_logger 0.8.2", + "futures 0.1.30", + "hex-literal", + "jsonrpc-core-client", + "log", + "sc-rpc", + "sc-rpc-api", + "sp-core", + "sp-io", + "tokio 0.1.22", +] + [[package]] name = "remove_dir_all" version = "0.5.3" @@ -6311,9 +6481,9 @@ checksum = "53552c6c49e1e13f1a203ef0080ab3bbef0beb570a528993e83df057a9d9bba1" [[package]] name = "ring" -version = "0.16.16" +version = "0.16.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b72b84d47e8ec5a4f2872e8262b8f8256c5be1c938a7d6d3a867a3ba8f722f74" +checksum = "024a1e66fea74c66c66624ee5622a7ff0e4b73a13b4f5c326ddb50c708944226" dependencies = [ "cc", "libc", @@ -6336,9 +6506,9 @@ dependencies = [ [[package]] name = "rpassword" -version = "5.0.0" +version = "5.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d755237fc0f99d98641540e66abac8bc46a0652f19148ac9e21de2da06b326c9" +checksum = "ffc936cf8a7ea60c58f030fd36a612a48f440610214dc54bc36431f9ea0c3efb" dependencies = [ "libc", "winapi 0.3.9", @@ -6346,14 +6516,14 @@ dependencies = [ [[package]] name = "rust-argon2" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dab61250775933275e84053ac235621dfb739556d5c54a2f2e9313b7cf43a19" +checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb" dependencies = [ - "base64 0.12.3", + "base64 0.13.0", "blake2b_simd", "constant_time_eq", - "crossbeam-utils 0.7.2", + "crossbeam-utils 0.8.1", ] [[package]] @@ -6433,7 +6603,7 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4da5fcb054c46f5a5dff833b129285a93d3f0179531735e6c866e8cc307d2020" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "pin-project 0.4.27", "static_assertions", ] @@ -6473,24 +6643,24 @@ dependencies = [ [[package]] name = "sc-authority-discovery" -version = "0.8.1" +version = "0.9.0" dependencies = [ "async-trait", "derive_more", "either", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "libp2p", "log", "parity-scale-codec", - "prost 0.7.0", + "prost", "prost-build", "quickcheck", "rand 0.7.3", "sc-client-api", "sc-network", "sc-peerset", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-api", "sp-authority-discovery", "sp-blockchain", @@ -6504,9 +6674,9 @@ dependencies = [ [[package]] name = "sc-basic-authorship" -version = "0.8.1" +version = "0.9.0" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "log", "parity-scale-codec", @@ -6529,7 +6699,7 @@ dependencies = [ [[package]] name = "sc-block-builder" -version = "0.8.1" +version = "0.9.0" dependencies = [ "parity-scale-codec", "sc-client-api", @@ -6547,9 +6717,9 @@ dependencies = [ [[package]] name = "sc-chain-spec" -version = "2.0.1" +version = "3.0.0" dependencies = [ - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "parity-scale-codec", "sc-chain-spec-derive", "sc-consensus-babe", @@ -6558,7 +6728,7 @@ dependencies = [ "sc-network", "sc-telemetry", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-chain-spec", "sp-consensus-babe", "sp-core", @@ -6567,7 +6737,7 @@ dependencies = [ [[package]] name = "sc-chain-spec-derive" -version = "2.0.1" +version = "3.0.0" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -6577,11 +6747,11 @@ dependencies = [ [[package]] name = "sc-cli" -version = "0.8.1" +version = "0.9.0" dependencies = [ "chrono", "fdlimit", - "futures 0.3.9", + "futures 0.3.12", "hex", "libp2p", "log", @@ -6597,7 +6767,7 @@ dependencies = [ "sc-telemetry", "sc-tracing", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-blockchain", "sp-core", "sp-keyring", @@ -6610,16 +6780,16 @@ dependencies = [ "tempfile", "thiserror", "tiny-bip39", - "tokio 0.2.23", + "tokio 0.2.25", ] [[package]] name = "sc-client-api" -version = "2.0.1" +version = "3.0.0" dependencies = [ "derive_more", "fnv", - "futures 0.3.9", + "futures 0.3.12", "hash-db", "kvdb", "kvdb-memorydb", @@ -6652,7 +6822,7 @@ dependencies = [ [[package]] name = "sc-client-db" -version = "0.8.1" +version = "0.9.0" dependencies = [ "blake2-rfc", "hash-db", @@ -6686,7 +6856,7 @@ dependencies = [ [[package]] name = "sc-consensus" -version = "0.8.1" +version = "0.9.0" dependencies = [ "sc-client-api", "sp-blockchain", @@ -6696,12 +6866,12 @@ dependencies = [ [[package]] name = "sc-consensus-aura" -version = "0.8.1" +version = "0.9.0" dependencies = [ "derive_more", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", - "getrandom 0.2.1", + "getrandom 0.2.2", "log", "parity-scale-codec", "parking_lot 0.11.1", @@ -6720,6 +6890,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-aura", + "sp-consensus-slots", "sp-core", "sp-inherents", "sp-io", @@ -6736,11 +6907,11 @@ dependencies = [ [[package]] name = "sc-consensus-babe" -version = "0.8.1" +version = "0.9.0" dependencies = [ "derive_more", "fork-tree", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "log", "merlin", @@ -6772,6 +6943,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-babe", + "sp-consensus-slots", "sp-consensus-vrf", "sp-core", "sp-inherents", @@ -6790,10 +6962,10 @@ dependencies = [ [[package]] name = "sc-consensus-babe-rpc" -version = "0.8.1" +version = "0.9.0" dependencies = [ "derive_more", - "futures 0.3.9", + "futures 0.3.12", "jsonrpc-core", "jsonrpc-core-client", "jsonrpc-derive", @@ -6803,7 +6975,7 @@ dependencies = [ "sc-keystore", "sc-rpc-api", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-api", "sp-application-crypto", "sp-blockchain", @@ -6819,7 +6991,7 @@ dependencies = [ [[package]] name = "sc-consensus-epochs" -version = "0.8.1" +version = "0.9.0" dependencies = [ "fork-tree", "parity-scale-codec", @@ -6831,11 +7003,11 @@ dependencies = [ [[package]] name = "sc-consensus-manual-seal" -version = "0.8.1" +version = "0.9.0" dependencies = [ "assert_matches", "derive_more", - "futures 0.3.9", + "futures 0.3.12", "jsonrpc-core", "jsonrpc-core-client", "jsonrpc-derive", @@ -6852,6 +7024,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-babe", + "sp-consensus-slots", "sp-core", "sp-inherents", "sp-keyring", @@ -6863,15 +7036,15 @@ dependencies = [ "substrate-test-runtime-client", "substrate-test-runtime-transaction-pool", "tempfile", - "tokio 0.2.23", + "tokio 0.2.25", ] [[package]] name = "sc-consensus-pow" -version = "0.8.1" +version = "0.9.0" dependencies = [ "derive_more", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "log", "parity-scale-codec", @@ -6891,9 +7064,9 @@ dependencies = [ [[package]] name = "sc-consensus-slots" -version = "0.8.1" +version = "0.9.0" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "log", "parity-scale-codec", @@ -6917,7 +7090,7 @@ dependencies = [ [[package]] name = "sc-consensus-uncles" -version = "0.8.1" +version = "0.9.0" dependencies = [ "log", "sc-client-api", @@ -6930,7 +7103,7 @@ dependencies = [ [[package]] name = "sc-executor" -version = "0.8.1" +version = "0.9.0" dependencies = [ "assert_matches", "derive_more", @@ -6941,7 +7114,7 @@ dependencies = [ "parity-scale-codec", "parity-wasm 0.41.0", "parking_lot 0.11.1", - "paste 0.1.18", + "paste 1.0.4", "sc-executor-common", "sc-executor-wasmi", "sc-executor-wasmtime", @@ -6971,7 +7144,7 @@ dependencies = [ [[package]] name = "sc-executor-common" -version = "0.8.1" +version = "0.9.0" dependencies = [ "derive_more", "parity-scale-codec", @@ -6986,7 +7159,7 @@ dependencies = [ [[package]] name = "sc-executor-wasmi" -version = "0.8.1" +version = "0.9.0" dependencies = [ "log", "parity-scale-codec", @@ -7000,7 +7173,7 @@ dependencies = [ [[package]] name = "sc-executor-wasmtime" -version = "0.8.1" +version = "0.9.0" dependencies = [ "assert_matches", "log", @@ -7018,19 +7191,20 @@ dependencies = [ [[package]] name = "sc-finality-grandpa" -version = "0.8.1" +version = "0.9.0" dependencies = [ "assert_matches", "derive_more", + "dyn-clone", "finality-grandpa", "fork-tree", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "linked-hash-map", "log", "parity-scale-codec", "parking_lot 0.11.1", - "pin-project 0.4.27", + "pin-project 1.0.4", "rand 0.7.3", "sc-block-builder", "sc-client-api", @@ -7040,7 +7214,7 @@ dependencies = [ "sc-network-gossip", "sc-network-test", "sc-telemetry", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-api", "sp-application-crypto", "sp-arithmetic", @@ -7059,16 +7233,16 @@ dependencies = [ "substrate-prometheus-endpoint", "substrate-test-runtime-client", "tempfile", - "tokio 0.2.23", + "tokio 0.2.25", ] [[package]] name = "sc-finality-grandpa-rpc" -version = "0.8.1" +version = "0.9.0" dependencies = [ "derive_more", "finality-grandpa", - "futures 0.3.9", + "futures 0.3.12", "jsonrpc-core", "jsonrpc-core-client", "jsonrpc-derive", @@ -7082,7 +7256,7 @@ dependencies = [ "sc-network-test", "sc-rpc", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-blockchain", "sp-consensus", "sp-core", @@ -7094,15 +7268,15 @@ dependencies = [ [[package]] name = "sc-finality-grandpa-warp-sync" -version = "0.8.0" +version = "0.9.0" dependencies = [ "derive_more", - "futures 0.3.9", + "futures 0.3.12", "log", "num-traits", "parity-scale-codec", "parking_lot 0.11.1", - "prost 0.6.1", + "prost", "sc-client-api", "sc-finality-grandpa", "sc-network", @@ -7113,10 +7287,10 @@ dependencies = [ [[package]] name = "sc-informant" -version = "0.8.1" +version = "0.9.0" dependencies = [ "ansi_term 0.12.1", - "futures 0.3.9", + "futures 0.3.12", "log", "parity-util-mem", "sc-client-api", @@ -7130,27 +7304,27 @@ dependencies = [ [[package]] name = "sc-keystore" -version = "2.0.1" +version = "3.0.0" dependencies = [ "async-trait", "derive_more", - "futures 0.3.9", + "futures 0.3.12", "futures-util", "hex", "merlin", "parking_lot 0.11.1", "rand 0.7.3", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-application-crypto", "sp-core", "sp-keystore", - "subtle 2.3.0", + "subtle 2.4.0", "tempfile", ] [[package]] name = "sc-light" -version = "2.0.1" +version = "3.0.0" dependencies = [ "hash-db", "lazy_static", @@ -7168,21 +7342,22 @@ dependencies = [ [[package]] name = "sc-network" -version = "0.8.1" +version = "0.9.0" dependencies = [ "assert_matches", "async-std", "async-trait", - "asynchronous-codec", + "asynchronous-codec 0.5.0", "bitflags", "bs58", "bytes 1.0.1", + "cid", "derive_more", "either", "erased-serde", "fnv", "fork-tree", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "hex", "ip_network", @@ -7194,8 +7369,8 @@ dependencies = [ "nohash-hasher", "parity-scale-codec", "parking_lot 0.11.1", - "pin-project 0.4.27", - "prost 0.7.0", + "pin-project 1.0.4", + "prost", "prost-build", "quickcheck", "rand 0.7.3", @@ -7203,8 +7378,8 @@ dependencies = [ "sc-client-api", "sc-peerset", "serde", - "serde_json 1.0.59", - "smallvec 1.5.0", + "serde_json 1.0.61", + "smallvec 1.6.1", "sp-arithmetic", "sp-blockchain", "sp-consensus", @@ -7227,10 +7402,10 @@ dependencies = [ [[package]] name = "sc-network-gossip" -version = "0.8.1" +version = "0.9.0" dependencies = [ "async-std", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "libp2p", "log", @@ -7249,7 +7424,7 @@ name = "sc-network-test" version = "0.8.0" dependencies = [ "async-std", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "libp2p", "log", @@ -7273,11 +7448,11 @@ dependencies = [ [[package]] name = "sc-offchain" -version = "2.0.1" +version = "3.0.0" dependencies = [ "bytes 0.5.6", "fnv", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "hyper 0.13.9", "hyper-rustls", @@ -7303,25 +7478,25 @@ dependencies = [ "sp-utils", "substrate-test-runtime-client", "threadpool", - "tokio 0.2.23", + "tokio 0.2.25", ] [[package]] name = "sc-peerset" -version = "2.0.1" +version = "3.0.0" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "libp2p", "log", "rand 0.7.3", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-utils", "wasm-timer", ] [[package]] name = "sc-proposer-metrics" -version = "0.8.1" +version = "0.9.0" dependencies = [ "log", "substrate-prometheus-endpoint", @@ -7329,11 +7504,11 @@ dependencies = [ [[package]] name = "sc-rpc" -version = "2.0.1" +version = "3.0.0" dependencies = [ "assert_matches", "futures 0.1.30", - "futures 0.3.9", + "futures 0.3.12", "hash-db", "jsonrpc-core", "jsonrpc-pubsub", @@ -7350,7 +7525,7 @@ dependencies = [ "sc-rpc-api", "sc-tracing", "sc-transaction-pool", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-api", "sp-blockchain", "sp-chain-spec", @@ -7371,10 +7546,10 @@ dependencies = [ [[package]] name = "sc-rpc-api" -version = "0.8.1" +version = "0.9.0" dependencies = [ "derive_more", - "futures 0.3.9", + "futures 0.3.12", "jsonrpc-core", "jsonrpc-core-client", "jsonrpc-derive", @@ -7383,7 +7558,7 @@ dependencies = [ "parity-scale-codec", "parking_lot 0.11.1", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-chain-spec", "sp-core", "sp-rpc", @@ -7394,7 +7569,7 @@ dependencies = [ [[package]] name = "sc-rpc-server" -version = "2.0.1" +version = "3.0.0" dependencies = [ "futures 0.1.30", "jsonrpc-core", @@ -7404,7 +7579,7 @@ dependencies = [ "jsonrpc-ws-server", "log", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-runtime", "substrate-prometheus-endpoint", ] @@ -7425,13 +7600,13 @@ dependencies = [ [[package]] name = "sc-service" -version = "0.8.1" +version = "0.9.0" dependencies = [ "async-std", - "directories 3.0.1", + "directories", "exit-future", "futures 0.1.30", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "hash-db", "jsonrpc-core", @@ -7441,7 +7616,7 @@ dependencies = [ "parity-scale-codec", "parity-util-mem", "parking_lot 0.11.1", - "pin-project 0.4.27", + "pin-project 1.0.4", "rand 0.7.3", "sc-block-builder", "sc-chain-spec", @@ -7460,7 +7635,7 @@ dependencies = [ "sc-tracing", "sc-transaction-pool", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-api", "sp-application-crypto", "sp-block-builder", @@ -7486,9 +7661,11 @@ dependencies = [ "substrate-test-runtime-client", "tempfile", "thiserror", - "tokio 0.2.23", + "tokio 0.2.25", "tracing", "tracing-futures", + "tracing-log", + "tracing-subscriber", "wasm-timer", ] @@ -7498,7 +7675,7 @@ version = "2.0.0" dependencies = [ "fdlimit", "futures 0.1.30", - "futures 0.3.9", + "futures 0.3.12", "hex-literal", "log", "parity-scale-codec", @@ -7530,7 +7707,7 @@ dependencies = [ [[package]] name = "sc-state-db" -version = "0.8.1" +version = "0.9.0" dependencies = [ "log", "parity-scale-codec", @@ -7544,7 +7721,7 @@ dependencies = [ [[package]] name = "sc-sync-state-rpc" -version = "0.8.0" +version = "0.9.0" dependencies = [ "jsonrpc-core", "jsonrpc-core-client", @@ -7555,7 +7732,7 @@ dependencies = [ "sc-consensus-epochs", "sc-finality-grandpa", "sc-rpc-api", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-blockchain", "sp-runtime", "thiserror", @@ -7563,17 +7740,17 @@ dependencies = [ [[package]] name = "sc-telemetry" -version = "2.0.1" +version = "3.0.0" dependencies = [ "chrono", - "futures 0.3.9", + "futures 0.3.12", "libp2p", "log", "parking_lot 0.11.1", - "pin-project 0.4.27", + "pin-project 1.0.4", "rand 0.7.3", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-utils", "take_mut", "tracing", @@ -7584,7 +7761,7 @@ dependencies = [ [[package]] name = "sc-tracing" -version = "2.0.1" +version = "3.0.0" dependencies = [ "ansi_term 0.12.1", "atty", @@ -7598,7 +7775,7 @@ dependencies = [ "sc-telemetry", "sc-tracing-proc-macro", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-tracing", "thiserror", "tracing", @@ -7611,7 +7788,7 @@ dependencies = [ [[package]] name = "sc-tracing-proc-macro" -version = "2.0.0" +version = "3.0.0" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -7621,12 +7798,12 @@ dependencies = [ [[package]] name = "sc-transaction-graph" -version = "2.0.1" +version = "3.0.0" dependencies = [ "assert_matches", "criterion", "derive_more", - "futures 0.3.9", + "futures 0.3.12", "linked-hash-map", "log", "parity-scale-codec", @@ -7646,10 +7823,10 @@ dependencies = [ [[package]] name = "sc-transaction-pool" -version = "2.0.1" +version = "3.0.0" dependencies = [ "assert_matches", - "futures 0.3.9", + "futures 0.3.12", "futures-diagnose", "hex", "intervalier", @@ -7694,14 +7871,14 @@ checksum = "021b403afe70d81eea68f6ea12f6b3c9588e5d536a94c3bf80f15e7faa267862" dependencies = [ "arrayref", "arrayvec 0.5.2", - "curve25519-dalek 2.1.0", - "getrandom 0.1.15", + "curve25519-dalek 2.1.2", + "getrandom 0.1.16", "merlin", "rand 0.7.3", "rand_core 0.5.1", "serde", "sha2 0.8.2", - "subtle 2.3.0", + "subtle 2.4.0", "zeroize", ] @@ -7728,9 +7905,9 @@ dependencies = [ [[package]] name = "scroll_derive" -version = "0.10.4" +version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b12bd20b94c7cdfda8c7ba9b92ad0d9a56e3fa018c25fca83b51aa664c9b4c0d" +checksum = "aaaae8f38bb311444cfb7f1979af0bc9240d95795f75f9ceddf6a59b79ceffa0" dependencies = [ "proc-macro2", "quote", @@ -7803,7 +7980,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" dependencies = [ - "semver-parser 0.10.1", + "semver-parser 0.10.2", "serde", ] @@ -7815,9 +7992,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "semver-parser" -version = "0.10.1" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ef146c2ad5e5f4b037cd6ce2ebb775401729b19a82040c1beac9d36c7d1428" +checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" dependencies = [ "pest", ] @@ -7836,9 +8013,9 @@ checksum = "930c0acf610d3fdb5e2ab6213019aaa04e227ebe9547b0649ba599b16d788bd7" [[package]] name = "serde" -version = "1.0.119" +version = "1.0.123" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bdd36f49e35b61d49efd8aa7fc068fd295961fd2286d0b2ee9a4c7a14e99cc3" +checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" dependencies = [ "serde_derive", ] @@ -7855,9 +8032,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.119" +version = "1.0.123" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "552954ce79a059ddd5fd68c271592374bd15cab2274970380c000118aeffe1cd" +checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" dependencies = [ "proc-macro2", "quote", @@ -7876,9 +8053,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.59" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" +checksum = "4fceb2595057b6891a4ee808f70054bd2d12f0e97f1cbb78689b59f676df325a" dependencies = [ "itoa", "ryu", @@ -7905,7 +8082,7 @@ checksum = "ce3cdf1b5e620a498ee6f2a171885ac7e22f0e12089ec4b3d22b84921792507c" dependencies = [ "block-buffer 0.9.0", "cfg-if 1.0.0", - "cpuid-bool", + "cpuid-bool 0.1.2", "digest 0.9.0", "opaque-debug 0.3.0", ] @@ -7924,13 +8101,13 @@ dependencies = [ [[package]] name = "sha2" -version = "0.9.2" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e7aab86fe2149bad8c507606bdb3f4ef5e7b2380eb92350f56122cca72a42a8" +checksum = "fa827a14b29ab7f44778d14a88d3cb76e949c45083f7dbfa507d0cb699dc12de" dependencies = [ "block-buffer 0.9.0", "cfg-if 1.0.0", - "cpuid-bool", + "cpuid-bool 0.1.2", "digest 0.9.0", "opaque-debug 0.3.0", ] @@ -7949,12 +8126,11 @@ dependencies = [ [[package]] name = "sharded-slab" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b4921be914e16899a80adefb821f8ddb7974e3f1250223575a44ed994882127" +checksum = "79c719719ee05df97490f80a45acfc99e5a30ce98a1e4fb67aee422745ae14e3" dependencies = [ "lazy_static", - "loom", ] [[package]] @@ -7963,20 +8139,30 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" +[[package]] +name = "signal-hook" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e31d442c16f047a671b5a71e2161d6e68814012b7f5379d269ebd915fac2729" +dependencies = [ + "libc", + "signal-hook-registry", +] + [[package]] name = "signal-hook-registry" -version = "1.2.2" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce32ea0c6c56d5eacaeb814fbed9960547021d3edd010ded1425f180536b20ab" +checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6" dependencies = [ "libc", ] [[package]] name = "signature" -version = "1.2.2" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29f060a7d147e33490ec10da418795238fd7545bba241504d6b31a409f2e6210" +checksum = "0f0242b8e50dd9accdd56170e94ca1ebd223b098eb9c83539a6e367d0f36ae68" [[package]] name = "simba" @@ -7998,18 +8184,18 @@ checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" [[package]] name = "smallvec" -version = "0.6.13" +version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6" +checksum = "b97fcaeba89edba30f044a10c6a3cc39df9c3f17d7cd829dd1446cab35f890e0" dependencies = [ "maybe-uninit", ] [[package]] name = "smallvec" -version = "1.5.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7acad6f34eb9e8a259d3283d1e8c1d34d7415943d4895f65cc73813c7396fc85" +checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" [[package]] name = "snow" @@ -8024,20 +8210,19 @@ dependencies = [ "rand_core 0.5.1", "ring", "rustc_version", - "sha2 0.9.2", - "subtle 2.3.0", + "sha2 0.9.3", + "subtle 2.4.0", "x25519-dalek", ] [[package]] name = "socket2" -version = "0.3.17" +version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c29947abdee2a218277abeca306f25789c938e500ea5a9d4b12a5a504466902" +checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" dependencies = [ "cfg-if 1.0.0", "libc", - "redox_syscall", "winapi 0.3.9", ] @@ -8050,7 +8235,7 @@ dependencies = [ "base64 0.12.3", "bytes 0.5.6", "flate2", - "futures 0.3.9", + "futures 0.3.12", "httparse", "log", "rand 0.7.3", @@ -8059,7 +8244,7 @@ dependencies = [ [[package]] name = "sp-allocator" -version = "2.0.1" +version = "3.0.0" dependencies = [ "log", "sp-core", @@ -8070,7 +8255,7 @@ dependencies = [ [[package]] name = "sp-api" -version = "2.0.1" +version = "3.0.0" dependencies = [ "hash-db", "parity-scale-codec", @@ -8086,7 +8271,7 @@ dependencies = [ [[package]] name = "sp-api-proc-macro" -version = "2.0.1" +version = "3.0.0" dependencies = [ "blake2-rfc", "proc-macro-crate", @@ -8116,7 +8301,7 @@ dependencies = [ [[package]] name = "sp-application-crypto" -version = "2.0.1" +version = "3.0.0" dependencies = [ "parity-scale-codec", "serde", @@ -8139,7 +8324,7 @@ dependencies = [ [[package]] name = "sp-arithmetic" -version = "2.0.1" +version = "3.0.0" dependencies = [ "criterion", "integer-sqrt", @@ -8148,7 +8333,7 @@ dependencies = [ "primitive-types", "rand 0.7.3", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-debug-derive", "sp-std", ] @@ -8166,7 +8351,7 @@ dependencies = [ [[package]] name = "sp-authority-discovery" -version = "2.0.1" +version = "3.0.0" dependencies = [ "parity-scale-codec", "sp-api", @@ -8177,7 +8362,7 @@ dependencies = [ [[package]] name = "sp-authorship" -version = "2.0.1" +version = "3.0.0" dependencies = [ "parity-scale-codec", "sp-inherents", @@ -8187,7 +8372,7 @@ dependencies = [ [[package]] name = "sp-block-builder" -version = "2.0.1" +version = "3.0.0" dependencies = [ "parity-scale-codec", "sp-api", @@ -8198,9 +8383,9 @@ dependencies = [ [[package]] name = "sp-blockchain" -version = "2.0.1" +version = "3.0.0" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "log", "lru", "parity-scale-codec", @@ -8215,17 +8400,17 @@ dependencies = [ [[package]] name = "sp-chain-spec" -version = "2.0.1" +version = "3.0.0" dependencies = [ "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", ] [[package]] name = "sp-consensus" -version = "0.8.1" +version = "0.9.0" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", "libp2p", "log", @@ -8249,11 +8434,12 @@ dependencies = [ [[package]] name = "sp-consensus-aura" -version = "0.8.1" +version = "0.9.0" dependencies = [ "parity-scale-codec", "sp-api", "sp-application-crypto", + "sp-consensus-slots", "sp-inherents", "sp-runtime", "sp-std", @@ -8262,7 +8448,7 @@ dependencies = [ [[package]] name = "sp-consensus-babe" -version = "0.8.1" +version = "0.9.0" dependencies = [ "merlin", "parity-scale-codec", @@ -8281,7 +8467,7 @@ dependencies = [ [[package]] name = "sp-consensus-pow" -version = "0.8.1" +version = "0.9.0" dependencies = [ "parity-scale-codec", "sp-api", @@ -8292,15 +8478,16 @@ dependencies = [ [[package]] name = "sp-consensus-slots" -version = "0.8.1" +version = "0.9.0" dependencies = [ "parity-scale-codec", + "sp-arithmetic", "sp-runtime", ] [[package]] name = "sp-consensus-vrf" -version = "0.8.1" +version = "0.9.0" dependencies = [ "parity-scale-codec", "schnorrkel", @@ -8311,7 +8498,7 @@ dependencies = [ [[package]] name = "sp-core" -version = "2.0.1" +version = "3.0.0" dependencies = [ "base58", "blake2-rfc", @@ -8319,7 +8506,7 @@ dependencies = [ "criterion", "dyn-clonable", "ed25519-dalek", - "futures 0.3.9", + "futures 0.3.12", "hash-db", "hash256-std-hasher", "hex", @@ -8341,8 +8528,8 @@ dependencies = [ "schnorrkel", "secrecy", "serde", - "serde_json 1.0.59", - "sha2 0.9.2", + "serde_json 1.0.61", + "sha2 0.9.3", "sp-debug-derive", "sp-externalities", "sp-runtime-interface", @@ -8360,7 +8547,7 @@ dependencies = [ [[package]] name = "sp-database" -version = "2.0.1" +version = "3.0.0" dependencies = [ "kvdb", "parking_lot 0.11.1", @@ -8368,7 +8555,7 @@ dependencies = [ [[package]] name = "sp-debug-derive" -version = "2.0.1" +version = "3.0.0" dependencies = [ "proc-macro2", "quote", @@ -8377,7 +8564,7 @@ dependencies = [ [[package]] name = "sp-election-providers" -version = "2.0.0" +version = "3.0.0" dependencies = [ "parity-scale-codec", "sp-arithmetic", @@ -8388,7 +8575,7 @@ dependencies = [ [[package]] name = "sp-externalities" -version = "0.8.1" +version = "0.9.0" dependencies = [ "environmental", "parity-scale-codec", @@ -8398,7 +8585,7 @@ dependencies = [ [[package]] name = "sp-finality-grandpa" -version = "2.0.1" +version = "3.0.0" dependencies = [ "finality-grandpa", "log", @@ -8414,7 +8601,7 @@ dependencies = [ [[package]] name = "sp-inherents" -version = "2.0.1" +version = "3.0.0" dependencies = [ "parity-scale-codec", "parking_lot 0.11.1", @@ -8425,9 +8612,9 @@ dependencies = [ [[package]] name = "sp-io" -version = "2.0.1" +version = "3.0.0" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "hash-db", "libsecp256k1", "log", @@ -8448,7 +8635,7 @@ dependencies = [ [[package]] name = "sp-keyring" -version = "2.0.1" +version = "3.0.0" dependencies = [ "lazy_static", "sp-core", @@ -8458,11 +8645,11 @@ dependencies = [ [[package]] name = "sp-keystore" -version = "0.8.0" +version = "0.9.0" dependencies = [ "async-trait", "derive_more", - "futures 0.3.9", + "futures 0.3.12", "merlin", "parity-scale-codec", "parking_lot 0.11.1", @@ -8476,7 +8663,7 @@ dependencies = [ [[package]] name = "sp-npos-elections" -version = "2.0.1" +version = "3.0.0" dependencies = [ "parity-scale-codec", "rand 0.7.3", @@ -8491,7 +8678,7 @@ dependencies = [ [[package]] name = "sp-npos-elections-compact" -version = "2.0.1" +version = "3.0.0" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -8513,7 +8700,7 @@ dependencies = [ [[package]] name = "sp-offchain" -version = "2.0.1" +version = "3.0.0" dependencies = [ "sp-api", "sp-core", @@ -8523,34 +8710,34 @@ dependencies = [ [[package]] name = "sp-panic-handler" -version = "2.0.1" +version = "3.0.0" dependencies = [ "backtrace", ] [[package]] name = "sp-rpc" -version = "2.0.1" +version = "3.0.0" dependencies = [ "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-core", ] [[package]] name = "sp-runtime" -version = "2.0.1" +version = "3.0.0" dependencies = [ "either", "hash256-std-hasher", - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "log", "parity-scale-codec", "parity-util-mem", - "paste 0.1.18", + "paste 1.0.4", "rand 0.7.3", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-application-crypto", "sp-arithmetic", "sp-core", @@ -8561,9 +8748,9 @@ dependencies = [ [[package]] name = "sp-runtime-interface" -version = "2.0.1" +version = "3.0.0" dependencies = [ - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "parity-scale-codec", "primitive-types", "rustversion", @@ -8583,7 +8770,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface-proc-macro" -version = "2.0.1" +version = "3.0.0" dependencies = [ "Inflector", "proc-macro-crate", @@ -8632,7 +8819,7 @@ dependencies = [ [[package]] name = "sp-sandbox" -version = "0.8.1" +version = "0.9.0" dependencies = [ "assert_matches", "parity-scale-codec", @@ -8646,15 +8833,15 @@ dependencies = [ [[package]] name = "sp-serializer" -version = "2.0.1" +version = "3.0.0" dependencies = [ "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", ] [[package]] name = "sp-session" -version = "2.0.1" +version = "3.0.0" dependencies = [ "parity-scale-codec", "sp-api", @@ -8666,7 +8853,7 @@ dependencies = [ [[package]] name = "sp-staking" -version = "2.0.1" +version = "3.0.0" dependencies = [ "parity-scale-codec", "sp-runtime", @@ -8675,7 +8862,7 @@ dependencies = [ [[package]] name = "sp-state-machine" -version = "0.8.1" +version = "0.9.0" dependencies = [ "hash-db", "hex-literal", @@ -8685,7 +8872,7 @@ dependencies = [ "parking_lot 0.11.1", "pretty_assertions", "rand 0.7.3", - "smallvec 1.5.0", + "smallvec 1.6.1", "sp-core", "sp-externalities", "sp-panic-handler", @@ -8699,11 +8886,11 @@ dependencies = [ [[package]] name = "sp-std" -version = "2.0.1" +version = "3.0.0" [[package]] name = "sp-storage" -version = "2.0.1" +version = "3.0.0" dependencies = [ "impl-serde", "parity-scale-codec", @@ -8715,7 +8902,7 @@ dependencies = [ [[package]] name = "sp-tasks" -version = "2.0.0" +version = "3.0.0" dependencies = [ "log", "parity-scale-codec", @@ -8740,9 +8927,9 @@ dependencies = [ [[package]] name = "sp-timestamp" -version = "2.0.1" +version = "3.0.0" dependencies = [ - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "parity-scale-codec", "sp-api", "sp-inherents", @@ -8753,7 +8940,7 @@ dependencies = [ [[package]] name = "sp-tracing" -version = "2.0.1" +version = "3.0.0" dependencies = [ "log", "parity-scale-codec", @@ -8765,10 +8952,10 @@ dependencies = [ [[package]] name = "sp-transaction-pool" -version = "2.0.1" +version = "3.0.0" dependencies = [ "derive_more", - "futures 0.3.9", + "futures 0.3.12", "log", "parity-scale-codec", "serde", @@ -8780,7 +8967,7 @@ dependencies = [ [[package]] name = "sp-trie" -version = "2.0.1" +version = "3.0.0" dependencies = [ "criterion", "hash-db", @@ -8798,9 +8985,9 @@ dependencies = [ [[package]] name = "sp-utils" -version = "2.0.1" +version = "3.0.0" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "futures-core", "futures-timer 3.0.2", "lazy_static", @@ -8809,7 +8996,7 @@ dependencies = [ [[package]] name = "sp-version" -version = "2.0.1" +version = "3.0.0" dependencies = [ "impl-serde", "parity-scale-codec", @@ -8820,9 +9007,9 @@ dependencies = [ [[package]] name = "sp-wasm-interface" -version = "2.0.1" +version = "3.0.0" dependencies = [ - "impl-trait-for-tuples 0.2.0", + "impl-trait-for-tuples", "parity-scale-codec", "sp-std", "wasmi", @@ -8882,9 +9069,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "structopt" -version = "0.3.20" +version = "0.3.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "126d630294ec449fae0b16f964e35bf3c74f940da9dca17ee9b905f7b3112eb8" +checksum = "5277acd7ee46e63e5168a80734c9f6ee81b1367a7d8772a2d765df2a3705d28c" dependencies = [ "clap", "lazy_static", @@ -8893,9 +9080,9 @@ dependencies = [ [[package]] name = "structopt-derive" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65e51c492f9e23a220534971ff5afc14037289de430e3c83f9daf6a1b6ae91e8" +checksum = "5ba9cdfda491b814720b6b06e0cac513d922fc407582032e8706e9f137976f90" dependencies = [ "heck", "proc-macro-error", @@ -8906,18 +9093,18 @@ dependencies = [ [[package]] name = "strum" -version = "0.16.0" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6138f8f88a16d90134763314e3fc76fa3ed6a7db4725d6acf9a3ef95a3188d22" +checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c" dependencies = [ "strum_macros", ] [[package]] name = "strum_macros" -version = "0.16.0" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0054a7df764039a6cd8592b9de84be4bec368ff081d203a7d5371cbfa8e65c81" +checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149" dependencies = [ "heck", "proc-macro2", @@ -8948,14 +9135,14 @@ dependencies = [ [[package]] name = "substrate-browser-utils" -version = "0.8.1" +version = "0.9.0" dependencies = [ "chrono", "console_error_panic_hook", "futures 0.1.30", - "futures 0.3.9", + "futures 0.3.12", "futures-timer 3.0.2", - "getrandom 0.2.1", + "getrandom 0.2.2", "js-sys", "kvdb-web", "libp2p-wasm-ext", @@ -8974,14 +9161,14 @@ dependencies = [ [[package]] name = "substrate-build-script-utils" -version = "2.0.1" +version = "3.0.0" dependencies = [ "platforms", ] [[package]] name = "substrate-frame-cli" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-system", "sc-cli", @@ -8992,26 +9179,26 @@ dependencies = [ [[package]] name = "substrate-frame-rpc-support" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-support", "frame-system", - "futures 0.3.9", + "futures 0.3.12", "jsonrpc-client-transports", "jsonrpc-core", "parity-scale-codec", "sc-rpc-api", "serde", "sp-storage", - "tokio 0.2.23", + "tokio 0.2.25", ] [[package]] name = "substrate-frame-rpc-system" -version = "2.0.1" +version = "3.0.0" dependencies = [ "frame-system-rpc-runtime-api", - "futures 0.3.9", + "futures 0.3.12", "jsonrpc-core", "jsonrpc-core-client", "jsonrpc-derive", @@ -9033,7 +9220,7 @@ dependencies = [ [[package]] name = "substrate-prometheus-endpoint" -version = "0.8.1" +version = "0.9.0" dependencies = [ "async-std", "derive_more", @@ -9041,7 +9228,7 @@ dependencies = [ "hyper 0.13.9", "log", "prometheus", - "tokio 0.2.23", + "tokio 0.2.25", ] [[package]] @@ -9049,7 +9236,7 @@ name = "substrate-test-client" version = "2.0.1" dependencies = [ "futures 0.1.30", - "futures 0.3.9", + "futures 0.3.12", "hash-db", "hex", "parity-scale-codec", @@ -9060,7 +9247,7 @@ dependencies = [ "sc-light", "sc-service", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sp-blockchain", "sp-consensus", "sp-core", @@ -9074,7 +9261,7 @@ dependencies = [ name = "substrate-test-runtime" version = "2.0.0" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "frame-executive", "frame-support", "frame-system", @@ -9118,7 +9305,7 @@ dependencies = [ name = "substrate-test-runtime-client" version = "2.0.0" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "parity-scale-codec", "sc-block-builder", "sc-client-api", @@ -9139,7 +9326,7 @@ name = "substrate-test-runtime-transaction-pool" version = "2.0.0" dependencies = [ "derive_more", - "futures 0.3.9", + "futures 0.3.12", "parity-scale-codec", "parking_lot 0.11.1", "sc-transaction-graph", @@ -9151,18 +9338,18 @@ dependencies = [ [[package]] name = "substrate-test-utils" -version = "2.0.1" +version = "3.0.0" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "sc-service", "substrate-test-utils-derive", - "tokio 0.2.23", + "tokio 0.2.25", "trybuild", ] [[package]] name = "substrate-test-utils-derive" -version = "0.8.1" +version = "0.9.0" dependencies = [ "proc-macro-crate", "quote", @@ -9175,12 +9362,12 @@ version = "0.1.0" dependencies = [ "sc-service", "substrate-test-utils", - "tokio 0.2.23", + "tokio 0.2.25", ] [[package]] name = "substrate-wasm-builder" -version = "3.0.0" +version = "4.0.0" dependencies = [ "ansi_term 0.12.1", "atty", @@ -9200,15 +9387,15 @@ checksum = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" [[package]] name = "subtle" -version = "2.3.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "343f3f510c2915908f155e94f17220b19ccfacf2a64a2a5d8004f2c3e311e7fd" +checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" [[package]] name = "syn" -version = "1.0.58" +version = "1.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" dependencies = [ "proc-macro2", "quote", @@ -9233,31 +9420,37 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" +[[package]] +name = "tap" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36474e732d1affd3a6ed582781b3683df3d0563714c59c39591e8ff707cf078e" + [[package]] name = "target-lexicon" -version = "0.10.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab0e7238dcc7b40a7be719a25365910f6807bd864f4cce6b2e6b873658e2b19d" +checksum = "4ee5a98e506fb7231a304c3a1bd7c132a55016cf65001e0282480665870dfcb9" [[package]] name = "tempfile" -version = "3.1.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "libc", - "rand 0.7.3", - "redox_syscall", + "rand 0.8.3", + "redox_syscall 0.2.4", "remove_dir_all", "winapi 0.3.9", ] [[package]] name = "termcolor" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf11676eb135389f21fcda654382c4859bbfc1d2f36e4425a2f829bb41b1e20e" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" dependencies = [ "winapi-util", ] @@ -9295,18 +9488,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e9ae34b84616eedaaf1e9dd6026dbe00dcafa92aa0c8077cb69df1fcfe5e53e" +checksum = "76cc616c6abf8c8928e2fdcc0dbfab37175edd8fb49a4641066ad1364fdab146" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56" +checksum = "9be73a2caec27583d0046ef3796c3794f868a5bc813db689eed00c7631275cd1" dependencies = [ "proc-macro2", "quote", @@ -9315,11 +9508,11 @@ dependencies = [ [[package]] name = "thread_local" -version = "1.0.1" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd" dependencies = [ - "lazy_static", + "once_cell", ] [[package]] @@ -9354,7 +9547,7 @@ dependencies = [ "pbkdf2 0.4.0", "rand 0.7.3", "rustc-hash", - "sha2 0.9.2", + "sha2 0.9.3", "thiserror", "unicode-normalization", "zeroize", @@ -9371,19 +9564,19 @@ dependencies = [ [[package]] name = "tinytemplate" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d3dc76004a03cec1c5932bca4cdc2e39aaa798e3f82363dd94f9adf6098c12f" +checksum = "a2ada8616fad06a2d0c455adc530de4ef57605a8120cc65da9653e0e9623ca74" dependencies = [ "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", ] [[package]] name = "tinyvec" -version = "1.0.1" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b78a366903f506d2ad52ca8dc552102ffdd3e937ba8a227f024dc1d1eae28575" +checksum = "317cca572a0e89c3ce0ca1f1bdc9369547fe318a683418e42ac8f59d14701023" dependencies = [ "tinyvec_macros", ] @@ -9420,9 +9613,9 @@ dependencies = [ [[package]] name = "tokio" -version = "0.2.23" +version = "0.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6d7ad61edd59bfcc7e80dababf0f4aed2e6d5e0ba1659356ae889752dfc12ff" +checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092" dependencies = [ "bytes 0.5.6", "fnv", @@ -9556,7 +9749,7 @@ checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a" dependencies = [ "futures-core", "rustls 0.18.1", - "tokio 0.2.23", + "tokio 0.2.25", "webpki", ] @@ -9666,23 +9859,23 @@ dependencies = [ "futures-sink", "log", "pin-project-lite 0.1.11", - "tokio 0.2.23", + "tokio 0.2.25", ] [[package]] name = "toml" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" dependencies = [ "serde", ] [[package]] name = "tower-service" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" +checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" [[package]] name = "tracing" @@ -9692,7 +9885,7 @@ checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3" dependencies = [ "cfg-if 1.0.0", "log", - "pin-project-lite 0.2.0", + "pin-project-lite 0.2.4", "tracing-attributes", "tracing-core", ] @@ -9760,9 +9953,9 @@ dependencies = [ "matchers", "regex", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "sharded-slab", - "smallvec 1.5.0", + "smallvec 1.6.1", "thread_local", "tracing", "tracing-core", @@ -9778,9 +9971,9 @@ checksum = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41" [[package]] name = "trie-bench" -version = "0.26.0" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d03b477b8837fd2e6bd17df374e5de60959c54058208de98833347c02b778c" +checksum = "568257edb909a5c532b1f4ab38ee6b5dedfbf8775be6a55a29020513ebe3e072" dependencies = [ "criterion", "hash-db", @@ -9794,15 +9987,15 @@ dependencies = [ [[package]] name = "trie-db" -version = "0.22.2" +version = "0.22.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cc176c377eb24d652c9c69c832c832019011b6106182bf84276c66b66d5c9a6" +checksum = "ec051edf7f0fc9499a2cb0947652cab2148b9d7f61cee7605e312e9f970dacaf" dependencies = [ "hash-db", "hashbrown", "log", "rustc-hex", - "smallvec 1.5.0", + "smallvec 1.6.1", ] [[package]] @@ -9830,17 +10023,38 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" +[[package]] +name = "try-runtime-cli" +version = "0.9.0" +dependencies = [ + "frame-try-runtime", + "log", + "parity-scale-codec", + "remote-externalities", + "sc-cli", + "sc-client-api", + "sc-executor", + "sc-service", + "sp-api", + "sp-blockchain", + "sp-core", + "sp-externalities", + "sp-runtime", + "sp-state-machine", + "structopt", +] + [[package]] name = "trybuild" -version = "1.0.38" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17b06f8610494cbeb9a7665b398306f0109ab8708296d7f24b0bcd89178bb350" +checksum = "1c9594b802f041389d2baac680663573dde3103bb4a4926d61d6aba689465978" dependencies = [ "dissimilar", "glob", "lazy_static", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "termcolor", "toml", ] @@ -9909,9 +10123,9 @@ dependencies = [ [[package]] name = "unicode-segmentation" -version = "1.7.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db8716a166f290ff49dabc18b44aa407cb7c6dbe1aa0971b44b8a24b0ca35aae" +checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" [[package]] name = "unicode-width" @@ -9932,7 +10146,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8326b2c654932e3e4f9196e69d08fdf7cfd718e1dc6f66b347e6024a0c961402" dependencies = [ "generic-array 0.14.4", - "subtle 2.3.0", + "subtle 2.4.0", ] [[package]] @@ -9947,7 +10161,19 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35581ff83d4101e58b582e607120c7f5ffb17e632a980b1f38334d76b36908b2" dependencies = [ - "asynchronous-codec", + "asynchronous-codec 0.5.0", + "bytes 1.0.1", + "futures-io", + "futures-util", +] + +[[package]] +name = "unsigned-varint" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f8d425fafb8cd76bc3f22aace4af471d3156301d7508f2107e98fbeae10bc7f" +dependencies = [ + "asynchronous-codec 0.6.0", "bytes 1.0.1", "futures-io", "futures-util", @@ -9982,11 +10208,20 @@ dependencies = [ "percent-encoding 2.1.0", ] +[[package]] +name = "value-bag" +version = "1.0.0-alpha.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b676010e055c99033117c2343b33a40a30b91fecd6c49055ac9cd2d6c305ab1" +dependencies = [ + "ctor", +] + [[package]] name = "vcpkg" -version = "0.2.10" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" +checksum = "b00bca6106a5e23f3eee943593759b7fcddb00554332e856d990c893966879fb" [[package]] name = "vec-arena" @@ -10073,21 +10308,21 @@ checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" [[package]] name = "wasm-bindgen" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cd364751395ca0f68cafb17666eee36b63077fb5ecd972bbcd74c90c4bf736e" +checksum = "55c0f7123de74f0dab9b7d00fd614e7b19349cd1e2f5252bbe9b1754b59433be" dependencies = [ "cfg-if 1.0.0", "serde", - "serde_json 1.0.59", + "serde_json 1.0.61", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1114f89ab1f4106e5b55e688b828c0ab0ea593a1ea7c094b141b14cbaaec2d62" +checksum = "7bc45447f0d4573f3d65720f636bbcc3dd6ce920ed704670118650bcd47764c7" dependencies = [ "bumpalo", "lazy_static", @@ -10100,11 +10335,11 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.18" +version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da" +checksum = "1fe9756085a84584ee9457a002b7cdfe0bfff169f45d2591d8be1345a6780e35" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "js-sys", "wasm-bindgen", "web-sys", @@ -10112,9 +10347,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6ac8995ead1f084a8dea1e65f194d0973800c7f571f6edd70adf06ecf77084" +checksum = "3b8853882eef39593ad4174dd26fc9865a64e84026d223f63bb2c42affcbba2c" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -10122,9 +10357,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5a48c72f299d80557c7c62e37e7225369ecc0c963964059509fbafe917c7549" +checksum = "4133b5e7f2a531fa413b3a1695e925038a05a71cf67e87dafa295cb645a01385" dependencies = [ "proc-macro2", "quote", @@ -10135,15 +10370,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e7811dd7f9398f14cc76efd356f98f03aa30419dea46aa810d71e819fc97158" +checksum = "dd4945e4943ae02d15c13962b38a5b1e81eadd4b71214eee75af64a4d6a4fd64" [[package]] name = "wasm-bindgen-test" -version = "0.3.18" +version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34d1cdc8b98a557f24733d50a1199c4b0635e465eecba9c45b214544da197f64" +checksum = "0355fa0c1f9b792a09b6dcb6a8be24d51e71e6d74972f9eb4a44c4c004d24a25" dependencies = [ "console_error_panic_hook", "js-sys", @@ -10155,9 +10390,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.18" +version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8fb9c67be7439ee8ab1b7db502a49c05e51e2835b66796c705134d9b8e1a585" +checksum = "27e07b46b98024c2ba2f9e83a10c2ef0515f057f2da299c1762a2017de80438b" dependencies = [ "proc-macro2", "quote", @@ -10180,7 +10415,7 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be0ecb0db480561e9a7642b5d3e4187c128914e58aa84330b9493e3eb68c5e7f" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "js-sys", "parking_lot 0.11.1", "pin-utils", @@ -10215,33 +10450,31 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.57.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32fddd575d477c6e9702484139cf9f23dcd554b06d185ed0f56c857dd3a47aa6" - -[[package]] -name = "wasmparser" -version = "0.59.0" +version = "0.71.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a950e6a618f62147fd514ff445b2a0b53120d382751960797f85f058c7eda9b9" +checksum = "89a30c99437829ede826802bfcf28500cf58df00e66cb9114df98813bc145ff1" [[package]] name = "wasmtime" -version = "0.19.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd3c4f449382779ef6e0a7c3ec6752ae614e20a42e4100000c3efdc973100e2" +checksum = "7426055cb92bd9a1e9469b48154d8d6119cd8c498c8b70284e420342c05dc45d" dependencies = [ "anyhow", "backtrace", - "cfg-if 0.1.10", - "lazy_static", + "bincode", + "cfg-if 1.0.0", + "cpp_demangle", + "indexmap", "libc", "log", "region", "rustc-demangle", - "smallvec 1.5.0", + "serde", + "smallvec 1.6.1", "target-lexicon", - "wasmparser 0.59.0", + "wasmparser", + "wasmtime-cache", "wasmtime-environ", "wasmtime-jit", "wasmtime-profiling", @@ -10250,74 +10483,101 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "wasmtime-cache" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c01d9287e36921e46f5887a47007824ae5dbb9b7517a2d565660ab4471478709" +dependencies = [ + "anyhow", + "base64 0.13.0", + "bincode", + "directories-next", + "errno", + "file-per-thread-logger", + "libc", + "log", + "serde", + "sha2 0.9.3", + "toml", + "winapi 0.3.9", + "zstd", +] + +[[package]] +name = "wasmtime-cranelift" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4134ed3a4316cd0de0e546c6004850afe472b0fa3fcdc2f2c15f8d449562d962" +dependencies = [ + "cranelift-codegen", + "cranelift-entity", + "cranelift-frontend", + "cranelift-wasm", + "wasmtime-environ", +] + [[package]] name = "wasmtime-debug" -version = "0.19.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e634af9067a3af6cf2c7d33dc3b84767ddaf5d010ba68e80eecbcea73d4a349" +checksum = "e91fa931df6dd8af2b02606307674d3bad23f55473d5f4c809dddf7e4c4dc411" dependencies = [ "anyhow", - "gimli 0.21.0", + "gimli", "more-asserts", - "object 0.20.0", + "object 0.22.0", "target-lexicon", "thiserror", - "wasmparser 0.59.0", + "wasmparser", "wasmtime-environ", ] [[package]] name = "wasmtime-environ" -version = "0.19.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08f85619a94ee4034bd5bb87fc3dcf71fd2237b81c840809da1201061eec9ab3" +checksum = "a1098871dc3120aaf8190d79153e470658bb79f63ee9ca31716711e123c28220" dependencies = [ "anyhow", - "base64 0.12.3", - "bincode", - "cfg-if 0.1.10", + "cfg-if 1.0.0", "cranelift-codegen", "cranelift-entity", - "cranelift-frontend", "cranelift-wasm", - "directories 2.0.2", - "errno", - "file-per-thread-logger", + "gimli", "indexmap", - "libc", "log", "more-asserts", - "rayon", "serde", - "sha2 0.8.2", "thiserror", - "toml", - "wasmparser 0.59.0", - "winapi 0.3.9", - "zstd", + "wasmparser", ] [[package]] name = "wasmtime-jit" -version = "0.19.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e914c013c7a9f15f4e429d5431f2830fb8adb56e40567661b69c5ec1d645be23" +checksum = "738bfcd1561ede8bb174215776fd7d9a95d5f0a47ca3deabe0282c55f9a89f68" dependencies = [ + "addr2line", "anyhow", - "cfg-if 0.1.10", + "cfg-if 1.0.0", "cranelift-codegen", "cranelift-entity", "cranelift-frontend", "cranelift-native", "cranelift-wasm", - "gimli 0.21.0", + "gimli", "log", "more-asserts", - "object 0.20.0", + "object 0.22.0", + "rayon", "region", + "serde", "target-lexicon", "thiserror", - "wasmparser 0.59.0", + "wasmparser", + "wasmtime-cranelift", "wasmtime-debug", "wasmtime-environ", "wasmtime-obj", @@ -10328,13 +10588,13 @@ dependencies = [ [[package]] name = "wasmtime-obj" -version = "0.19.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e81d8e02e9bc9fe2da9b6d48bbc217f96e089f7df613f11a28a3958abc44641e" +checksum = "3e96d77f1801131c5e86d93e42a3cf8a35402107332c202c245c83f34888a906" dependencies = [ "anyhow", "more-asserts", - "object 0.20.0", + "object 0.22.0", "target-lexicon", "wasmtime-debug", "wasmtime-environ", @@ -10342,16 +10602,16 @@ dependencies = [ [[package]] name = "wasmtime-profiling" -version = "0.19.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e8d4d1af8dd5f7096cfcc89dd668d358e52980c38cce199643372ffd6590e27" +checksum = "60bb672c9d894776d7b9250dd9b4fe890f8760201ee4f53e5f2da772b6c4debb" dependencies = [ "anyhow", - "cfg-if 0.1.10", - "gimli 0.21.0", + "cfg-if 1.0.0", + "gimli", "lazy_static", "libc", - "object 0.19.0", + "object 0.22.0", "scroll", "serde", "target-lexicon", @@ -10361,19 +10621,20 @@ dependencies = [ [[package]] name = "wasmtime-runtime" -version = "0.19.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a25f140bbbaadb07c531cba99ce1a966dba216138dc1b2a0ddecec851a01a93" +checksum = "a978086740949eeedfefcee667b57a9e98d9a7fc0de382fcfa0da30369e3530d" dependencies = [ "backtrace", "cc", - "cfg-if 0.1.10", + "cfg-if 1.0.0", "indexmap", "lazy_static", "libc", "log", - "memoffset", + "memoffset 0.6.1", "more-asserts", + "psm", "region", "thiserror", "wasmtime-environ", @@ -10382,18 +10643,18 @@ dependencies = [ [[package]] name = "wast" -version = "27.0.0" +version = "32.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2c3ef5f6a72dffa44c24d5811123f704e18a1dbc83637d347b1852b41d3835c" +checksum = "c24a3ee360d01d60ed0a0f960ab76a6acce64348cdb0bf8699c2a866fad57c7c" dependencies = [ "leb128", ] [[package]] name = "wat" -version = "1.0.28" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "835cf59c907f67e2bbc20f50157e08f35006fe2a8444d8ec9f5683e22f937045" +checksum = "5e8f7f34773fa6318e8897283abf7941c1f250faae4e1a52f82df09c3bad7cce" dependencies = [ "wast", ] @@ -10410,9 +10671,9 @@ dependencies = [ [[package]] name = "webpki" -version = "0.21.3" +version = "0.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab146130f5f790d45f82aeeb09e55a256573373ec64409fc19a6fb82fb1032ae" +checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea" dependencies = [ "ring", "untrusted", @@ -10508,24 +10769,30 @@ dependencies = [ "winapi-build", ] +[[package]] +name = "wyz" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" + [[package]] name = "x25519-dalek" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc614d95359fd7afc321b66d2107ede58b246b844cf5d8a0adcca413e439f088" dependencies = [ - "curve25519-dalek 3.0.0", + "curve25519-dalek 3.0.2", "rand_core 0.5.1", "zeroize", ] [[package]] name = "yamux" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9aeb8c4043cac71c3c299dff107171c220d179492350ea198e109a414981b83c" +checksum = "1cc7bd8c983209ed5d527f44b01c41b7dc146fd960c61cf9e1d25399841dc271" dependencies = [ - "futures 0.3.9", + "futures 0.3.12", "log", "nohash-hasher", "parking_lot 0.11.1", @@ -10556,18 +10823,18 @@ dependencies = [ [[package]] name = "zstd" -version = "0.5.3+zstd.1.4.5" +version = "0.5.4+zstd.1.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01b32eaf771efa709e8308605bbf9319bf485dc1503179ec0469b611937c0cd8" +checksum = "69996ebdb1ba8b1517f61387a883857818a66c8a295f487b1ffd8fd9d2c82910" dependencies = [ "zstd-safe", ] [[package]] name = "zstd-safe" -version = "2.0.5+zstd.1.4.5" +version = "2.0.6+zstd.1.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cfb642e0d27f64729a639c52db457e0ae906e7bc6f5fe8f5c453230400f1055" +checksum = "98aa931fb69ecee256d44589d19754e61851ae4769bf963b385119b1cc37a49e" dependencies = [ "libc", "zstd-sys", @@ -10575,9 +10842,9 @@ dependencies = [ [[package]] name = "zstd-sys" -version = "1.4.17+zstd.1.4.5" +version = "1.4.18+zstd.1.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b89249644df056b522696b1bb9e7c18c87e8ffa3e2f0dc3b0155875d6498f01b" +checksum = "a1e6e8778706838f43f771d80d37787cb2fe06dafe89dd3aebaf6721b9eaec81" dependencies = [ "cc", "glob", diff --git a/Cargo.toml b/Cargo.toml index 8be3ddafbcff4..26e8362159e44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,9 @@ members = [ "frame/contracts/rpc", "frame/contracts/rpc/runtime-api", "frame/democracy", + "frame/try-runtime", "frame/elections", + "frame/election-provider-multi-phase", "frame/example", "frame/ddc-metrics-offchain-worker", "frame/example-parallel", @@ -82,6 +84,7 @@ members = [ "frame/lottery", "frame/membership", "frame/merkle-mountain-range", + "frame/merkle-mountain-range/primitives", "frame/metadata", "frame/multisig", "frame/nicks", @@ -182,7 +185,9 @@ members = [ "utils/build-script-utils", "utils/fork-tree", "utils/frame/benchmarking-cli", + "utils/frame/remote-externalities", "utils/frame/frame-utilities-cli", + "utils/frame/try-runtime/cli", "utils/frame/rpc/support", "utils/frame/rpc/system", "utils/prometheus", diff --git a/bin/node/bench/Cargo.toml b/bin/node/bench/Cargo.toml index 06d89ff7d0d55..728eb8d6093ce 100644 --- a/bin/node/bench/Cargo.toml +++ b/bin/node/bench/Cargo.toml @@ -13,31 +13,31 @@ log = "0.4.8" node-primitives = { version = "2.0.0", path = "../primitives" } node-testing = { version = "2.0.0", path = "../testing" } node-runtime = { version = "2.0.0", path = "../runtime" } -sc-cli = { version = "0.8.0", path = "../../../client/cli" } -sc-client-api = { version = "2.0.0", path = "../../../client/api/" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-state-machine = { version = "0.8.0", path = "../../../primitives/state-machine" } +sc-cli = { version = "0.9.0", path = "../../../client/cli" } +sc-client-api = { version = "3.0.0", path = "../../../client/api/" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-state-machine = { version = "0.9.0", path = "../../../primitives/state-machine" } serde = "1.0.101" serde_json = "1.0.41" structopt = "0.3" derive_more = "0.99.2" -kvdb = "0.8.0" -kvdb-rocksdb = "0.10.0" -sp-trie = { version = "2.0.0", path = "../../../primitives/trie" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -sp-transaction-pool = { version = "2.0.0", path = "../../../primitives/transaction-pool" } -sc-basic-authorship = { version = "0.8.0", path = "../../../client/basic-authorship" } -sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" } -sp-timestamp = { version = "2.0.0", default-features = false, path = "../../../primitives/timestamp" } -sp-tracing = { version = "2.0.0", path = "../../../primitives/tracing" } +kvdb = "0.9.0" +kvdb-rocksdb = "0.11.0" +sp-trie = { version = "3.0.0", path = "../../../primitives/trie" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sp-transaction-pool = { version = "3.0.0", path = "../../../primitives/transaction-pool" } +sc-basic-authorship = { version = "0.9.0", path = "../../../client/basic-authorship" } +sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } +sp-timestamp = { version = "3.0.0", default-features = false, path = "../../../primitives/timestamp" } +sp-tracing = { version = "3.0.0", path = "../../../primitives/tracing" } hash-db = "0.15.2" tempfile = "3.1.0" fs_extra = "1" hex = "0.4.0" rand = { version = "0.7.2", features = ["small_rng"] } lazy_static = "1.4.0" -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } -parity-db = { version = "0.1.2" } -sc-transaction-pool = { version = "2.0.0", path = "../../../client/transaction-pool" } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } +parity-db = { version = "0.2.2" } +sc-transaction-pool = { version = "3.0.0", path = "../../../client/transaction-pool" } futures = { version = "0.3.4", features = ["thread-pool"] } diff --git a/bin/node/browser-testing/Cargo.toml b/bin/node/browser-testing/Cargo.toml index e098ea3e64635..fe83cc65ba632 100644 --- a/bin/node/browser-testing/Cargo.toml +++ b/bin/node/browser-testing/Cargo.toml @@ -8,14 +8,14 @@ license = "Apache-2.0" [dependencies] futures-timer = "3.0.2" -libp2p = { version = "0.34.0", default-features = false } +libp2p = { version = "0.35.1", default-features = false } jsonrpc-core = "15.0.0" serde = "1.0.106" serde_json = "1.0.48" -wasm-bindgen = { version = "=0.2.69", features = ["serde-serialize"] } +wasm-bindgen = { version = "=0.2.70", features = ["serde-serialize"] } wasm-bindgen-futures = "0.4.18" wasm-bindgen-test = "0.3.18" futures = "0.3.9" -node-cli = { path = "../cli", default-features = false, features = ["browser"] , version = "2.0.0"} -sc-rpc-api = { path = "../../../client/rpc-api" , version = "0.8.0"} +node-cli = { path = "../cli", default-features = false, features = ["browser"], version = "2.0.0"} +sc-rpc-api = { path = "../../../client/rpc-api", version = "0.9.0"} diff --git a/bin/node/cli/Cargo.toml b/bin/node/cli/Cargo.toml index 923761f78703a..337c378951b41 100644 --- a/bin/node/cli/Cargo.toml +++ b/bin/node/cli/Cargo.toml @@ -34,7 +34,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] # third-party dependencies -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } serde = { version = "1.0.102", features = ["derive"] } futures = { version = "0.3.9", features = ["compat"] } hex-literal = "0.3.1" @@ -44,50 +44,50 @@ structopt = { version = "0.3.8", optional = true } parking_lot = "0.11.1" # primitives -sp-authority-discovery = { version = "2.0.0", path = "../../../primitives/authority-discovery" } -sp-consensus-babe = { version = "0.8.0", path = "../../../primitives/consensus/babe" } -grandpa-primitives = { version = "2.0.0", package = "sp-finality-grandpa", path = "../../../primitives/finality-grandpa" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-timestamp = { version = "2.0.0", default-features = false, path = "../../../primitives/timestamp" } -sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" } -sp-keyring = { version = "2.0.0", path = "../../../primitives/keyring" } -sp-keystore = { version = "0.8.0", path = "../../../primitives/keystore" } -sp-io = { version = "2.0.0", path = "../../../primitives/io" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -sp-transaction-pool = { version = "2.0.0", path = "../../../primitives/transaction-pool" } +sp-authority-discovery = { version = "3.0.0", path = "../../../primitives/authority-discovery" } +sp-consensus-babe = { version = "0.9.0", path = "../../../primitives/consensus/babe" } +grandpa-primitives = { version = "3.0.0", package = "sp-finality-grandpa", path = "../../../primitives/finality-grandpa" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-timestamp = { version = "3.0.0", default-features = false, path = "../../../primitives/timestamp" } +sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } +sp-keyring = { version = "3.0.0", path = "../../../primitives/keyring" } +sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" } +sp-io = { version = "3.0.0", path = "../../../primitives/io" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sp-transaction-pool = { version = "3.0.0", path = "../../../primitives/transaction-pool" } # client dependencies -sc-client-api = { version = "2.0.0", path = "../../../client/api" } -sc-chain-spec = { version = "2.0.0", path = "../../../client/chain-spec" } -sc-consensus = { version = "0.8.0", path = "../../../client/consensus/common" } -sc-transaction-pool = { version = "2.0.0", path = "../../../client/transaction-pool" } -sc-network = { version = "0.8.0", path = "../../../client/network" } -sc-consensus-slots = { version = "0.8.0", path = "../../../client/consensus/slots" } -sc-consensus-babe = { version = "0.8.0", path = "../../../client/consensus/babe" } -grandpa = { version = "0.8.0", package = "sc-finality-grandpa", path = "../../../client/finality-grandpa" } -sc-client-db = { version = "0.8.0", default-features = false, path = "../../../client/db" } -sc-offchain = { version = "2.0.0", path = "../../../client/offchain" } -sc-rpc = { version = "2.0.0", path = "../../../client/rpc" } -sc-basic-authorship = { version = "0.8.0", path = "../../../client/basic-authorship" } -sc-service = { version = "0.8.0", default-features = false, path = "../../../client/service" } -sc-tracing = { version = "2.0.0", path = "../../../client/tracing" } -sc-telemetry = { version = "2.0.0", path = "../../../client/telemetry" } -sc-authority-discovery = { version = "0.8.0", path = "../../../client/authority-discovery" } -sc-finality-grandpa-warp-sync = { version = "0.8.0", path = "../../../client/finality-grandpa-warp-sync", optional = true } +sc-client-api = { version = "3.0.0", path = "../../../client/api" } +sc-chain-spec = { version = "3.0.0", path = "../../../client/chain-spec" } +sc-consensus = { version = "0.9.0", path = "../../../client/consensus/common" } +sc-transaction-pool = { version = "3.0.0", path = "../../../client/transaction-pool" } +sc-network = { version = "0.9.0", path = "../../../client/network" } +sc-consensus-slots = { version = "0.9.0", path = "../../../client/consensus/slots" } +sc-consensus-babe = { version = "0.9.0", path = "../../../client/consensus/babe" } +grandpa = { version = "0.9.0", package = "sc-finality-grandpa", path = "../../../client/finality-grandpa" } +sc-client-db = { version = "0.9.0", default-features = false, path = "../../../client/db" } +sc-offchain = { version = "3.0.0", path = "../../../client/offchain" } +sc-rpc = { version = "3.0.0", path = "../../../client/rpc" } +sc-basic-authorship = { version = "0.9.0", path = "../../../client/basic-authorship" } +sc-service = { version = "0.9.0", default-features = false, path = "../../../client/service" } +sc-tracing = { version = "3.0.0", path = "../../../client/tracing" } +sc-telemetry = { version = "3.0.0", path = "../../../client/telemetry" } +sc-authority-discovery = { version = "0.9.0", path = "../../../client/authority-discovery" } +sc-finality-grandpa-warp-sync = { version = "0.9.0", path = "../../../client/finality-grandpa-warp-sync", optional = true } # frame dependencies -pallet-indices = { version = "2.0.0", path = "../../../frame/indices" } -pallet-timestamp = { version = "2.0.0", default-features = false, path = "../../../frame/timestamp" } +pallet-indices = { version = "3.0.0", path = "../../../frame/indices" } +pallet-timestamp = { version = "3.0.0", default-features = false, path = "../../../frame/timestamp" } pallet-contracts = { version = "2.0.0", path = "../../../frame/contracts" } -frame-system = { version = "2.0.0", path = "../../../frame/system" } -pallet-balances = { version = "2.0.0", path = "../../../frame/balances" } -pallet-transaction-payment = { version = "2.0.0", path = "../../../frame/transaction-payment" } -frame-support = { version = "2.0.0", default-features = false, path = "../../../frame/support" } -pallet-im-online = { version = "2.0.0", default-features = false, path = "../../../frame/im-online" } -pallet-authority-discovery = { version = "2.0.0", path = "../../../frame/authority-discovery" } -pallet-staking = { version = "2.0.0", path = "../../../frame/staking" } -pallet-grandpa = { version = "2.0.0", path = "../../../frame/grandpa" } +frame-system = { version = "3.0.0", path = "../../../frame/system" } +pallet-balances = { version = "3.0.0", path = "../../../frame/balances" } +pallet-transaction-payment = { version = "3.0.0", path = "../../../frame/transaction-payment" } +frame-support = { version = "3.0.0", default-features = false, path = "../../../frame/support" } +pallet-im-online = { version = "3.0.0", default-features = false, path = "../../../frame/im-online" } +pallet-authority-discovery = { version = "3.0.0", path = "../../../frame/authority-discovery" } +pallet-staking = { version = "3.0.0", path = "../../../frame/staking" } +pallet-grandpa = { version = "3.0.0", path = "../../../frame/grandpa" } # node-specific dependencies node-runtime = { version = "2.0.0", path = "../runtime" } @@ -96,44 +96,48 @@ node-primitives = { version = "2.0.0", path = "../primitives" } node-executor = { version = "2.0.0", path = "../executor" } # CLI-specific dependencies -sc-cli = { version = "0.8.0", optional = true, path = "../../../client/cli" } -frame-benchmarking-cli = { version = "2.0.0", optional = true, path = "../../../utils/frame/benchmarking-cli" } +sc-cli = { version = "0.9.0", optional = true, path = "../../../client/cli" } +frame-benchmarking-cli = { version = "3.0.0", optional = true, path = "../../../utils/frame/benchmarking-cli" } node-inspect = { version = "0.8.0", optional = true, path = "../inspect" } +try-runtime-cli = { version = "0.9.0", optional = true, path = "../../../utils/frame/try-runtime/cli" } # WASM-specific dependencies wasm-bindgen = { version = "0.2.57", optional = true } wasm-bindgen-futures = { version = "0.4.18", optional = true } -browser-utils = { package = "substrate-browser-utils", path = "../../../utils/browser", optional = true, version = "0.8.0"} +browser-utils = { package = "substrate-browser-utils", path = "../../../utils/browser", optional = true, version = "0.9.0"} [target.'cfg(target_arch="x86_64")'.dependencies] node-executor = { version = "2.0.0", path = "../executor", features = [ "wasmtime" ] } -sc-cli = { version = "0.8.0", optional = true, path = "../../../client/cli", features = [ "wasmtime" ] } -sc-service = { version = "0.8.0", default-features = false, path = "../../../client/service", features = [ "wasmtime" ] } -sp-trie = { version = "2.0.0", default-features = false, path = "../../../primitives/trie", features = ["memory-tracker"] } +sc-cli = { version = "0.9.0", optional = true, path = "../../../client/cli", features = [ "wasmtime" ] } +sc-service = { version = "0.9.0", default-features = false, path = "../../../client/service", features = [ "wasmtime" ] } +sp-trie = { version = "3.0.0", default-features = false, path = "../../../primitives/trie", features = ["memory-tracker"] } [dev-dependencies] -sc-keystore = { version = "2.0.0", path = "../../../client/keystore" } -sc-consensus = { version = "0.8.0", path = "../../../client/consensus/common" } -sc-consensus-babe = { version = "0.8.0", features = ["test-helpers"], path = "../../../client/consensus/babe" } -sc-consensus-epochs = { version = "0.8.0", path = "../../../client/consensus/epochs" } +sc-keystore = { version = "3.0.0", path = "../../../client/keystore" } +sc-consensus = { version = "0.9.0", path = "../../../client/consensus/common" } +sc-consensus-babe = { version = "0.9.0", features = ["test-helpers"], path = "../../../client/consensus/babe" } +sc-consensus-epochs = { version = "0.9.0", path = "../../../client/consensus/epochs" } sc-service-test = { version = "2.0.0", path = "../../../client/service/test" } futures = "0.3.9" tempfile = "3.1.0" assert_cmd = "1.0" -nix = "0.17" +nix = "0.19" serde_json = "1.0" regex = "1" -platforms = "0.2.1" +platforms = "1.1" +async-std = { version = "1.6.5", features = ["attributes"] } +soketto = "0.4.2" [build-dependencies] structopt = { version = "0.3.8", optional = true } node-inspect = { version = "0.8.0", optional = true, path = "../inspect" } -frame-benchmarking-cli = { version = "2.0.0", optional = true, path = "../../../utils/frame/benchmarking-cli" } -substrate-build-script-utils = { version = "2.0.0", optional = true, path = "../../../utils/build-script-utils" } -substrate-frame-cli = { version = "2.0.0", optional = true, path = "../../../utils/frame/frame-utilities-cli" } +frame-benchmarking-cli = { version = "3.0.0", optional = true, path = "../../../utils/frame/benchmarking-cli" } +substrate-build-script-utils = { version = "3.0.0", optional = true, path = "../../../utils/build-script-utils" } +substrate-frame-cli = { version = "3.0.0", optional = true, path = "../../../utils/frame/frame-utilities-cli" } +try-runtime-cli = { version = "0.9.0", optional = true, path = "../../../utils/frame/try-runtime/cli" } [build-dependencies.sc-cli] -version = "0.8.0" +version = "0.9.0" package = "sc-cli" path = "../../../client/cli" optional = true @@ -155,8 +159,15 @@ cli = [ "sc-finality-grandpa-warp-sync", "structopt", "substrate-build-script-utils", + "try-runtime-cli", ] runtime-benchmarks = [ "node-runtime/runtime-benchmarks", "frame-benchmarking-cli", ] +# Enable features that allow the runtime to be tried and debugged. Name might be subject to change +# in the near future. +try-runtime = [ + "node-runtime/try-runtime", + "try-runtime-cli", +] diff --git a/bin/node/cli/browser-demo/index.html b/bin/node/cli/browser-demo/index.html index 60acfde39f559..4a706906ab108 100644 --- a/bin/node/cli/browser-demo/index.html +++ b/bin/node/cli/browser-demo/index.html @@ -27,8 +27,8 @@ setInterval(() => { client - .rpcSend('{"method":"system_networkState","params":[],"id":1,"jsonrpc":"2.0"}') - .then((r) => log("Network state: " + r)); + .rpcSend('{"method":"system_localPeerId","params":[],"id":1,"jsonrpc":"2.0"}') + .then((r) => log("Local PeerId: " + r)); }, 20000); } diff --git a/bin/node/cli/src/cli.rs b/bin/node/cli/src/cli.rs index 63a07e00e2197..9b80a3e345290 100644 --- a/bin/node/cli/src/cli.rs +++ b/bin/node/cli/src/cli.rs @@ -47,6 +47,11 @@ pub enum Subcommand { #[structopt(name = "benchmark", about = "Benchmark runtime pallets.")] Benchmark(frame_benchmarking_cli::BenchmarkCmd), + /// Try some experimental command on the runtime. This includes migration and runtime-upgrade + /// testing. + #[cfg(feature = "try-runtime")] + TryRuntime(try_runtime_cli::TryRuntimeCmd), + /// Verify a signature for a message, provided on STDIN, with a given (public or secret) key. Verify(VerifyCmd), diff --git a/bin/node/cli/src/command.rs b/bin/node/cli/src/command.rs index 461930a613d97..d3689bdcd6743 100644 --- a/bin/node/cli/src/command.rs +++ b/bin/node/cli/src/command.rs @@ -149,5 +149,20 @@ pub fn run() -> Result<()> { Ok((cmd.run(client, backend), task_manager)) }) }, + #[cfg(feature = "try-runtime")] + Some(Subcommand::TryRuntime(cmd)) => { + let runner = cli.create_runner(cmd)?; + runner.async_run(|config| { + // we don't need any of the components of new_partial, just a runtime, or a task + // manager to do `async_run`. + let registry = config.prometheus_config.as_ref().map(|cfg| &cfg.registry); + let task_manager = sc_service::TaskManager::new( + config.task_executor.clone(), + registry, + ).unwrap(); + + Ok((cmd.run::(config), task_manager)) + }) + } } } diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index bef3f715b96fd..312a0226fc3d9 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -58,10 +58,9 @@ pub fn new_partial(config: &Configuration) -> Result, ), grandpa::SharedVoterState, - Option, ) >, ServiceError> { - let (client, backend, keystore_container, task_manager, telemetry_span) = + let (client, backend, keystore_container, task_manager) = sc_service::new_full_parts::(&config)?; let client = Arc::new(client); @@ -95,7 +94,7 @@ pub fn new_partial(config: &Configuration) -> Result Result Result<( Arc::Hash>>, Arc>> ), ServiceError> { - let (client, backend, keystore_container, mut task_manager, on_demand, telemetry_span) = + let (client, backend, keystore_container, mut task_manager, on_demand) = sc_service::new_light_parts::(&config)?; config.network.extra_sets.push(grandpa::grandpa_peers_set_config()); @@ -403,7 +405,7 @@ pub fn new_light_base(mut config: Configuration) -> Result<( client.clone(), select_chain.clone(), inherent_data_providers.clone(), - &task_manager.spawn_handle(), + &task_manager.spawn_essential_handle(), config.prometheus_registry(), sp_consensus::NeverCanAuthor, )?; @@ -435,6 +437,9 @@ pub fn new_light_base(mut config: Configuration) -> Result<( let rpc_extensions = node_rpc::create_light(light_deps); + let telemetry_span = TelemetrySpan::new(); + let _telemetry_span_entered = telemetry_span.enter(); + let (rpc_handlers, telemetry_connection_notifier) = sc_service::spawn_tasks(sc_service::SpawnTasksParams { on_demand: Some(on_demand), @@ -446,7 +451,7 @@ pub fn new_light_base(mut config: Configuration) -> Result<( config, backend, network_status_sinks, system_rpc_tx, network: network.clone(), task_manager: &mut task_manager, - telemetry_span, + telemetry_span: Some(telemetry_span.clone()), })?; Ok(( @@ -515,7 +520,7 @@ mod tests { let chain_spec = crate::chain_spec::tests::integration_test_config_with_single_authority(); // For the block factory - let mut slot_num = 1u64; + let mut slot = 1u64; // For the extrinsics factory let bob = Arc::new(AccountKeyring::Bob.pair()); @@ -576,7 +581,7 @@ mod tests { descendent_query(&*service.client()), &parent_hash, parent_number, - slot_num, + slot.into(), ).unwrap().unwrap(); let mut digest = Digest::::default(); @@ -584,9 +589,9 @@ mod tests { // even though there's only one authority some slots might be empty, // so we must keep trying the next slots until we can claim one. let babe_pre_digest = loop { - inherent_data.replace_data(sp_timestamp::INHERENT_IDENTIFIER, &(slot_num * SLOT_DURATION)); + inherent_data.replace_data(sp_timestamp::INHERENT_IDENTIFIER, &(slot * SLOT_DURATION)); if let Some(babe_pre_digest) = sc_consensus_babe::test_helpers::claim_slot( - slot_num, + slot.into(), &parent_header, &*service.client(), keystore.clone(), @@ -595,7 +600,7 @@ mod tests { break babe_pre_digest; } - slot_num += 1; + slot += 1; }; digest.push(::babe_pre_digest(babe_pre_digest)); @@ -620,13 +625,11 @@ mod tests { sp_consensus_babe::AuthorityId::ID, &alice.to_public_crypto_pair(), &to_sign, - ).unwrap() - .try_into() - .unwrap(); + ).unwrap().unwrap().try_into().unwrap(); let item = ::babe_seal( signature, ); - slot_num += 1; + slot += 1; let mut params = BlockImportParams::new(BlockOrigin::File, new_header); params.post_digests.push(item); diff --git a/bin/node/cli/tests/telemetry.rs b/bin/node/cli/tests/telemetry.rs new file mode 100644 index 0000000000000..a9515db319edb --- /dev/null +++ b/bin/node/cli/tests/telemetry.rs @@ -0,0 +1,102 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use assert_cmd::cargo::cargo_bin; +use nix::sys::signal::{kill, Signal::SIGINT}; +use nix::unistd::Pid; +use std::convert::TryInto; +use std::process; + +pub mod common; +pub mod websocket_server; + +#[async_std::test] +async fn telemetry_works() { + let config = websocket_server::Config { + capacity: 1, + max_frame_size: 1048 * 1024, + send_buffer_len: 32, + bind_address: "127.0.0.1:0".parse().unwrap(), + }; + let mut server = websocket_server::WsServer::new(config).await.unwrap(); + + let addr = server.local_addr().unwrap(); + + let server_task = async_std::task::spawn(async move { + loop { + use websocket_server::Event; + match server.next_event().await { + // New connection on the listener. + Event::ConnectionOpen { address } => { + println!("New connection from {:?}", address); + server.accept(); + } + + // Received a message from a connection. + Event::BinaryFrame { message, .. } => { + let json: serde_json::Value = serde_json::from_slice(&message).unwrap(); + let object = json + .as_object() + .unwrap() + .get("payload") + .unwrap() + .as_object() + .unwrap(); + if matches!(object.get("best"), Some(serde_json::Value::String(_))) { + break; + } + } + + Event::TextFrame { .. } => panic!("Got a TextFrame over the socket, this is a bug"), + + // Connection has been closed. + Event::ConnectionError { .. } => {} + } + } + }); + + let mut substrate = process::Command::new(cargo_bin("cere")); + + let mut substrate = substrate + .args(&["--dev", "--tmp", "--telemetry-url"]) + .arg(format!("ws://{} 10", addr)) + .stdout(process::Stdio::piped()) + .stderr(process::Stdio::piped()) + .stdin(process::Stdio::null()) + .spawn() + .unwrap(); + + server_task.await; + + assert!( + substrate.try_wait().unwrap().is_none(), + "the process should still be running" + ); + + // Stop the process + kill(Pid::from_raw(substrate.id().try_into().unwrap()), SIGINT).unwrap(); + assert!(common::wait_for(&mut substrate, 40) + .map(|x| x.success()) + .unwrap_or_default()); + + let output = substrate.wait_with_output().unwrap(); + + println!("{}", String::from_utf8(output.stdout).unwrap()); + eprintln!("{}", String::from_utf8(output.stderr).unwrap()); + assert!(output.status.success()); +} diff --git a/bin/node/cli/tests/websocket_server.rs b/bin/node/cli/tests/websocket_server.rs new file mode 100644 index 0000000000000..a8af1c3599521 --- /dev/null +++ b/bin/node/cli/tests/websocket_server.rs @@ -0,0 +1,281 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use async_std::net::{TcpListener, TcpStream}; +use core::pin::Pin; +use futures::prelude::*; +use soketto::handshake::{server::Response, Server}; +use std::{io, net::SocketAddr}; + +/// Configuration for a [`WsServer`]. +pub struct Config { + /// IP address to try to bind to. + pub bind_address: SocketAddr, + + /// Maximum size, in bytes, of a frame sent by the remote. + /// + /// Since the messages are entirely buffered before being returned, a maximum value is + /// necessary in order to prevent malicious clients from sending huge frames that would + /// occupy a lot of memory. + pub max_frame_size: usize, + + /// Number of pending messages to buffer up for sending before the socket is considered + /// unresponsive. + pub send_buffer_len: usize, + + /// Pre-allocated capacity for the list of connections. + pub capacity: usize, +} + +/// Identifier for a connection with regard to a [`WsServer`]. +/// +/// After a connection has been closed, its [`ConnectionId`] might be reused. +#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] +pub struct ConnectionId(u64); + +/// A WebSocket message. +pub enum Message { + Text(String), + Binary(Vec), +} + +/// WebSockets listening socket and list of open connections. +pub struct WsServer { + /// Value passed through [`Config::max_frame_size`]. + max_frame_size: usize, + + /// Endpoint for incoming TCP sockets. + listener: TcpListener, + + /// Pending incoming connection to accept. Accepted by calling [`WsServer::accept`]. + pending_incoming: Option, + + /// List of TCP connections that are currently negotiating the WebSocket handshake. + /// + /// The output can be an error if the handshake fails. + negotiating: stream::FuturesUnordered< + Pin< + Box< + dyn Future, Box>> + + Send, + >, + >, + >, + + /// List of streams of incoming messages for all connections. + incoming_messages: stream::SelectAll< + Pin>> + Send>>, + >, + + /// Tasks dedicated to closing sockets that have been rejected. + rejected_sockets: stream::FuturesUnordered + Send>>>, +} + +impl WsServer { + /// Try opening a TCP listening socket. + /// + /// Returns an error if the listening socket fails to open. + pub async fn new(config: Config) -> Result { + let listener = TcpListener::bind(config.bind_address).await?; + + Ok(WsServer { + max_frame_size: config.max_frame_size, + listener, + pending_incoming: None, + negotiating: stream::FuturesUnordered::new(), + incoming_messages: stream::SelectAll::new(), + rejected_sockets: stream::FuturesUnordered::new(), + }) + } + + /// Address of the local TCP listening socket, as provided by the operating system. + pub fn local_addr(&self) -> Result { + self.listener.local_addr() + } + + /// Accepts the pending connection. + /// + /// Either [`WsServer::accept`] or [`WsServer::reject`] must be called after a + /// [`Event::ConnectionOpen`] event is returned. + /// + /// # Panic + /// + /// Panics if no connection is pending. + /// + pub fn accept(&mut self) { + let pending_incoming = self.pending_incoming.take().expect("no pending socket"); + + self.negotiating.push(Box::pin(async move { + let mut server = Server::new(pending_incoming); + + let websocket_key = match server.receive_request().await { + Ok(req) => req.into_key(), + Err(err) => return Err(Box::new(err) as Box<_>), + }; + + match server + .send_response(&{ + Response::Accept { + key: &websocket_key, + protocol: None, + } + }) + .await + { + Ok(()) => {} + Err(err) => return Err(Box::new(err) as Box<_>), + }; + + Ok(server) + })); + } + + /// Reject the pending connection. + /// + /// Either [`WsServer::accept`] or [`WsServer::reject`] must be called after a + /// [`Event::ConnectionOpen`] event is returned. + /// + /// # Panic + /// + /// Panics if no connection is pending. + /// + pub fn reject(&mut self) { + let _ = self.pending_incoming.take().expect("no pending socket"); + } + + /// Returns the next event happening on the server. + pub async fn next_event(&mut self) -> Event { + loop { + futures::select! { + // Only try to fetch a new incoming connection if none is pending. + socket = { + let listener = &self.listener; + let has_pending = self.pending_incoming.is_some(); + async move { + if !has_pending { + listener.accept().await + } else { + loop { futures::pending!() } + } + } + }.fuse() => { + let (socket, address) = match socket { + Ok(s) => s, + Err(_) => continue, + }; + debug_assert!(self.pending_incoming.is_none()); + self.pending_incoming = Some(socket); + return Event::ConnectionOpen { address }; + }, + + result = self.negotiating.select_next_some() => { + let server = match result { + Ok(s) => s, + Err(error) => return Event::ConnectionError { + error, + }, + }; + + let (mut _sender, receiver) = { + let mut builder = server.into_builder(); + builder.set_max_frame_size(self.max_frame_size); + builder.set_max_message_size(self.max_frame_size); + builder.finish() + }; + + // Spawn a task dedicated to receiving messages from the socket. + self.incoming_messages.push({ + // Turn `receiver` into a stream of received packets. + let socket_packets = stream::unfold((receiver, Vec::new()), move |(mut receiver, mut buf)| async { + buf.clear(); + let ret = match receiver.receive_data(&mut buf).await { + Ok(soketto::Data::Text(len)) => String::from_utf8(buf[..len].to_vec()) + .map(Message::Text) + .map_err(|err| Box::new(err) as Box<_>), + Ok(soketto::Data::Binary(len)) => Ok(buf[..len].to_vec()) + .map(Message::Binary), + Err(err) => Err(Box::new(err) as Box<_>), + }; + Some((ret, (receiver, buf))) + }); + + Box::pin(socket_packets.map(move |msg| (msg))) + }); + }, + + result = self.incoming_messages.select_next_some() => { + let message = match result { + Ok(m) => m, + Err(error) => return Event::ConnectionError { + error, + }, + }; + + match message { + Message::Text(message) => { + return Event::TextFrame { + message, + } + } + Message::Binary(message) => { + return Event::BinaryFrame { + message, + } + } + } + }, + + _ = self.rejected_sockets.select_next_some() => { + } + } + } + } +} + +/// Event that has happened on a [`WsServer`]. +#[derive(Debug)] +pub enum Event { + /// A new TCP connection has arrived on the listening socket. + /// + /// The connection *must* be accepted or rejected using [`WsServer::accept`] or + /// [`WsServer::reject`]. + /// No other [`Event::ConnectionOpen`] event will be generated until the current pending + /// connection has been either accepted or rejected. + ConnectionOpen { + /// Address of the remote, as provided by the operating system. + address: SocketAddr, + }, + + /// An error has happened on a connection. The connection is now closed and its + /// [`ConnectionId`] is now invalid. + ConnectionError { error: Box }, + + /// A text frame has been received on a connection. + TextFrame { + /// Message sent by the remote. Its content is entirely decided by the client, and + /// nothing must be assumed about the validity of this message. + message: String, + }, + + /// A text frame has been received on a connection. + BinaryFrame { + /// Message sent by the remote. Its content is entirely decided by the client, and + /// nothing must be assumed about the validity of this message. + message: Vec, + }, +} diff --git a/bin/node/executor/Cargo.toml b/bin/node/executor/Cargo.toml index a2177ac9cd79d..7faca59cd48c6 100644 --- a/bin/node/executor/Cargo.toml +++ b/bin/node/executor/Cargo.toml @@ -12,35 +12,36 @@ repository = "https://github.com/paritytech/substrate/" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } node-primitives = { version = "2.0.0", path = "../primitives" } node-runtime = { version = "2.0.0", path = "../runtime" } -sc-executor = { version = "0.8.0", path = "../../../client/executor" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-keystore = { version = "0.8.0", path = "../../../primitives/keystore" } -sp-io = { version = "2.0.0", path = "../../../primitives/io" } -sp-state-machine = { version = "0.8.0", path = "../../../primitives/state-machine" } -sp-trie = { version = "2.0.0", path = "../../../primitives/trie" } +sc-executor = { version = "0.9.0", path = "../../../client/executor" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" } +sp-io = { version = "3.0.0", path = "../../../primitives/io" } +sp-state-machine = { version = "0.9.0", path = "../../../primitives/state-machine" } +sp-trie = { version = "3.0.0", path = "../../../primitives/trie" } trie-root = "0.16.0" -frame-benchmarking = { version = "2.0.0", path = "../../../frame/benchmarking" } +frame-benchmarking = { version = "3.0.0", path = "../../../frame/benchmarking" } [dev-dependencies] criterion = "0.3.0" -frame-support = { version = "2.0.0", path = "../../../frame/support" } -frame-system = { version = "2.0.0", path = "../../../frame/system" } +frame-support = { version = "3.0.0", path = "../../../frame/support" } +frame-system = { version = "3.0.0", path = "../../../frame/system" } node-testing = { version = "2.0.0", path = "../testing" } -pallet-balances = { version = "2.0.0", path = "../../../frame/balances" } +pallet-balances = { version = "3.0.0", path = "../../../frame/balances" } pallet-contracts = { version = "2.0.0", path = "../../../frame/contracts" } -pallet-grandpa = { version = "2.0.0", path = "../../../frame/grandpa" } -pallet-im-online = { version = "2.0.0", path = "../../../frame/im-online" } -pallet-indices = { version = "2.0.0", path = "../../../frame/indices" } -pallet-session = { version = "2.0.0", path = "../../../frame/session" } -pallet-timestamp = { version = "2.0.0", path = "../../../frame/timestamp" } -pallet-transaction-payment = { version = "2.0.0", path = "../../../frame/transaction-payment" } -pallet-treasury = { version = "2.0.0", path = "../../../frame/treasury" } -sp-application-crypto = { version = "2.0.0", path = "../../../primitives/application-crypto" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-externalities = { version = "0.8.0", path = "../../../primitives/externalities" } +pallet-grandpa = { version = "3.0.0", path = "../../../frame/grandpa" } +pallet-im-online = { version = "3.0.0", path = "../../../frame/im-online" } +pallet-indices = { version = "3.0.0", path = "../../../frame/indices" } +pallet-session = { version = "3.0.0", path = "../../../frame/session" } +pallet-timestamp = { version = "3.0.0", path = "../../../frame/timestamp" } +pallet-transaction-payment = { version = "3.0.0", path = "../../../frame/transaction-payment" } +pallet-treasury = { version = "3.0.0", path = "../../../frame/treasury" } +sp-application-crypto = { version = "3.0.0", path = "../../../primitives/application-crypto" } +sp-consensus-babe = { version = "0.9.0", path = "../../../primitives/consensus/babe" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-externalities = { version = "0.9.0", path = "../../../primitives/externalities" } substrate-test-client = { version = "2.0.0", path = "../../../test-utils/client" } wat = "1.0" diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index 3d5548c0cd790..3009a3b6f9f8e 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -17,7 +17,6 @@ use codec::{Encode, Decode, Joiner}; use frame_support::{ - StorageValue, StorageMap, traits::Currency, weights::{GetDispatchInfo, DispatchInfo, DispatchClass}, }; @@ -32,7 +31,7 @@ use frame_system::{self, EventRecord, Phase}; use node_runtime::{ Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances, System, TransactionPayment, Event, - constants::currency::*, + constants::{time::SLOT_DURATION, currency::*}, }; use node_primitives::{Balance, Hash}; use wat; @@ -76,6 +75,7 @@ fn set_heap_pages(ext: &mut E, heap_pages: u64) { } fn changes_trie_block() -> (Vec, Hash) { + let time = 42 * 1000; construct_block( &mut new_test_ext(compact_code_unwrap(), true), 1, @@ -83,13 +83,14 @@ fn changes_trie_block() -> (Vec, Hash) { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(pallet_timestamp::Call::set(42 * 1000)), + function: Call::Timestamp(pallet_timestamp::Call::set(time)), }, CheckedExtrinsic { signed: Some((alice(), signed_extra(0, 0))), function: Call::Balances(pallet_balances::Call::transfer(bob().into(), 69 * DOLLARS)), }, - ] + ], + (time / SLOT_DURATION).into(), ) } @@ -98,6 +99,7 @@ fn changes_trie_block() -> (Vec, Hash) { /// from block1's execution to block2 to derive the correct storage_root. fn blocks() -> ((Vec, Hash), (Vec, Hash)) { let mut t = new_test_ext(compact_code_unwrap(), false); + let time1 = 42 * 1000; let block1 = construct_block( &mut t, 1, @@ -105,14 +107,16 @@ fn blocks() -> ((Vec, Hash), (Vec, Hash)) { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(pallet_timestamp::Call::set(42 * 1000)), + function: Call::Timestamp(pallet_timestamp::Call::set(time1)), }, CheckedExtrinsic { signed: Some((alice(), signed_extra(0, 0))), function: Call::Balances(pallet_balances::Call::transfer(bob().into(), 69 * DOLLARS)), }, - ] + ], + (time1 / SLOT_DURATION).into(), ); + let time2 = 52 * 1000; let block2 = construct_block( &mut t, 2, @@ -120,7 +124,7 @@ fn blocks() -> ((Vec, Hash), (Vec, Hash)) { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(pallet_timestamp::Call::set(52 * 1000)), + function: Call::Timestamp(pallet_timestamp::Call::set(time2)), }, CheckedExtrinsic { signed: Some((bob(), signed_extra(0, 0))), @@ -130,12 +134,13 @@ fn blocks() -> ((Vec, Hash), (Vec, Hash)) { signed: Some((alice(), signed_extra(1, 0))), function: Call::Balances(pallet_balances::Call::transfer(bob().into(), 15 * DOLLARS)), } - ] + ], + (time2 / SLOT_DURATION).into(), ); // session change => consensus authorities change => authorities change digest item appears let digest = Header::decode(&mut &block2.0[..]).unwrap().digest; - assert_eq!(digest.logs().len(), 0); + assert_eq!(digest.logs().len(), 1 /* Just babe slot */); (block1, block2) } @@ -154,7 +159,8 @@ fn block_with_size(time: u64, nonce: u32, size: usize) -> (Vec, Hash) { signed: Some((alice(), signed_extra(nonce, 0))), function: Call::System(frame_system::Call::remark(vec![0; size])), } - ] + ], + (time * 1000 / SLOT_DURATION).into(), ) } @@ -336,7 +342,7 @@ fn full_native_block_import_works() { }, EventRecord { phase: Phase::ApplyExtrinsic(1), - event: Event::pallet_balances(pallet_balances::RawEvent::Transfer( + event: Event::pallet_balances(pallet_balances::Event::Transfer( alice().into(), bob().into(), 69 * DOLLARS, @@ -389,7 +395,7 @@ fn full_native_block_import_works() { EventRecord { phase: Phase::ApplyExtrinsic(1), event: Event::pallet_balances( - pallet_balances::RawEvent::Transfer( + pallet_balances::Event::Transfer( bob().into(), alice().into(), 5 * DOLLARS, @@ -412,7 +418,7 @@ fn full_native_block_import_works() { EventRecord { phase: Phase::ApplyExtrinsic(2), event: Event::pallet_balances( - pallet_balances::RawEvent::Transfer( + pallet_balances::Event::Transfer( alice().into(), bob().into(), 15 * DOLLARS, @@ -588,8 +594,9 @@ fn deploying_wasm_contract_should_work() { &[], ); - let subsistence = pallet_contracts::ConfigCache::::subsistence_threshold_uncached(); + let subsistence = pallet_contracts::Module::::subsistence_threshold(); + let time = 42 * 1000; let b = construct_block( &mut new_test_ext(compact_code_unwrap(), false), 1, @@ -597,28 +604,22 @@ fn deploying_wasm_contract_should_work() { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(pallet_timestamp::Call::set(42 * 1000)), + function: Call::Timestamp(pallet_timestamp::Call::set(time)), }, CheckedExtrinsic { signed: Some((charlie(), signed_extra(0, 0))), function: Call::Contracts( - pallet_contracts::Call::put_code::(transfer_code) - ), - }, - CheckedExtrinsic { - signed: Some((charlie(), signed_extra(1, 0))), - function: Call::Contracts( - pallet_contracts::Call::instantiate::( - 1 * DOLLARS + subsistence, + pallet_contracts::Call::instantiate_with_code::( + 1000 * DOLLARS + subsistence, 500_000_000, - transfer_ch, + transfer_code, Vec::new(), Vec::new(), ) ), }, CheckedExtrinsic { - signed: Some((charlie(), signed_extra(2, 0))), + signed: Some((charlie(), signed_extra(1, 0))), function: Call::Contracts( pallet_contracts::Call::call::( sp_runtime::MultiAddress::Id(addr.clone()), @@ -628,7 +629,8 @@ fn deploying_wasm_contract_should_work() { ) ), }, - ] + ], + (time / SLOT_DURATION).into(), ); let mut t = new_test_ext(compact_code_unwrap(), false); diff --git a/bin/node/executor/tests/common.rs b/bin/node/executor/tests/common.rs index b376ebc35bae8..635155b5d00b2 100644 --- a/bin/node/executor/tests/common.rs +++ b/bin/node/executor/tests/common.rs @@ -19,6 +19,7 @@ use codec::{Encode, Decode}; use frame_system::offchain::AppCrypto; use frame_support::Hashable; use sp_state_machine::TestExternalities as CoreTestExternalities; +use sp_consensus_babe::{BABE_ENGINE_ID, Slot, digests::{PreDigest, SecondaryPlainPreDigest}}; use sp_core::{ NeverNativeValue, NativeOrEncoded, crypto::KeyTypeId, @@ -29,6 +30,8 @@ use sp_runtime::{ ApplyExtrinsicResult, MultiSigner, MultiSignature, + Digest, + DigestItem, traits::{Header as HeaderT, BlakeTwo256}, }; use sc_executor::{NativeExecutor, WasmExecutionMethod}; @@ -99,7 +102,7 @@ pub fn executor() -> NativeExecutor { pub fn executor_call< R:Decode + Encode + PartialEq, - NC: FnOnce() -> std::result::Result + std::panic::UnwindSafe + NC: FnOnce() -> std::result::Result> + std::panic::UnwindSafe >( t: &mut TestExternalities, method: &str, @@ -145,6 +148,7 @@ pub fn construct_block( number: BlockNumber, parent_hash: Hash, extrinsics: Vec, + babe_slot: Slot, ) -> (Vec, Hash) { use sp_trie::{TrieConfiguration, trie_types::Layout}; @@ -162,7 +166,17 @@ pub fn construct_block( number, extrinsics_root, state_root: Default::default(), - digest: Default::default(), + digest: Digest { + logs: vec![ + DigestItem::PreRuntime( + BABE_ENGINE_ID, + PreDigest::SecondaryPlain(SecondaryPlainPreDigest { + slot: babe_slot, + authority_index: 42, + }).encode() + ), + ], + }, }; // execute the block to get the real header. diff --git a/bin/node/executor/tests/fees.rs b/bin/node/executor/tests/fees.rs index 9d83610b689de..90b28539f7bcd 100644 --- a/bin/node/executor/tests/fees.rs +++ b/bin/node/executor/tests/fees.rs @@ -17,7 +17,6 @@ use codec::{Encode, Joiner}; use frame_support::{ - StorageValue, traits::Currency, weights::{GetDispatchInfo, constants::ExtrinsicBaseWeight, IdentityFee, WeightToFeePolynomial}, }; @@ -26,7 +25,7 @@ use sp_runtime::{Perbill, FixedPointNumber}; use node_runtime::{ CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment, Multiplier, TransactionByteFee, - constants::currency::*, + constants::{time::SLOT_DURATION, currency::*}, }; use node_primitives::Balance; use node_testing::keyring::*; @@ -47,6 +46,7 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() { let mut tt = new_test_ext(compact_code_unwrap(), false); + let time1 = 42 * 1000; // big one in terms of weight. let block1 = construct_block( &mut tt, @@ -55,15 +55,17 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(pallet_timestamp::Call::set(42 * 1000)), + function: Call::Timestamp(pallet_timestamp::Call::set(time1)), }, CheckedExtrinsic { signed: Some((charlie(), signed_extra(0, 0))), function: Call::System(frame_system::Call::fill_block(Perbill::from_percent(60))), } - ] + ], + (time1 / SLOT_DURATION).into(), ); + let time2 = 52 * 1000; // small one in terms of weight. let block2 = construct_block( &mut tt, @@ -72,13 +74,14 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(pallet_timestamp::Call::set(52 * 1000)), + function: Call::Timestamp(pallet_timestamp::Call::set(time2)), }, CheckedExtrinsic { signed: Some((charlie(), signed_extra(1, 0))), function: Call::System(frame_system::Call::remark(vec![0; 1])), } - ] + ], + (time2 / SLOT_DURATION).into(), ); println!( @@ -220,7 +223,7 @@ fn block_weight_capacity_report() { let mut time = 10; let mut nonce: Index = 0; let mut block_number = 1; - let mut previous_hash: Hash = GENESIS_HASH.into(); + let mut previous_hash: node_primitives::Hash = GENESIS_HASH.into(); loop { let num_transfers = block_number * factor; @@ -239,7 +242,8 @@ fn block_weight_capacity_report() { &mut tt, block_number, previous_hash, - xts + xts, + (time * 1000 / SLOT_DURATION).into(), ); let len = block.0.len(); @@ -287,7 +291,7 @@ fn block_length_capacity_report() { let mut time = 10; let mut nonce: Index = 0; let mut block_number = 1; - let mut previous_hash: Hash = GENESIS_HASH.into(); + let mut previous_hash: node_primitives::Hash = GENESIS_HASH.into(); loop { // NOTE: this is super slow. Can probably be improved. @@ -304,7 +308,8 @@ fn block_length_capacity_report() { signed: Some((charlie(), signed_extra(nonce, 0))), function: Call::System(frame_system::Call::remark(vec![0u8; (block_number * factor) as usize])), }, - ] + ], + (time * 1000 / SLOT_DURATION).into(), ); let len = block.0.len(); diff --git a/bin/node/inspect/Cargo.toml b/bin/node/inspect/Cargo.toml index 7f94e15bb8fc8..3d89a68aed309 100644 --- a/bin/node/inspect/Cargo.toml +++ b/bin/node/inspect/Cargo.toml @@ -11,13 +11,13 @@ repository = "https://github.com/paritytech/substrate/" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } derive_more = "0.99" log = "0.4.8" -sc-cli = { version = "0.8.0", path = "../../../client/cli" } -sc-client-api = { version = "2.0.0", path = "../../../client/api" } -sc-service = { version = "0.8.0", default-features = false, path = "../../../client/service" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } +sc-cli = { version = "0.9.0", path = "../../../client/cli" } +sc-client-api = { version = "3.0.0", path = "../../../client/api" } +sc-service = { version = "0.9.0", default-features = false, path = "../../../client/service" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } structopt = "0.3.8" diff --git a/bin/node/primitives/Cargo.toml b/bin/node/primitives/Cargo.toml index db28472087fe2..043ec5ab21cec 100644 --- a/bin/node/primitives/Cargo.toml +++ b/bin/node/primitives/Cargo.toml @@ -11,14 +11,14 @@ repository = "https://github.com/paritytech/substrate/" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -frame-system = { version = "2.0.0", default-features = false, path = "../../../frame/system" } -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../../primitives/application-crypto" } -sp-core = { version = "2.0.0", default-features = false, path = "../../../primitives/core" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +frame-system = { version = "3.0.0", default-features = false, path = "../../../frame/system" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../../primitives/application-crypto" } +sp-core = { version = "3.0.0", default-features = false, path = "../../../primitives/core" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../primitives/runtime" } [dev-dependencies] -sp-serializer = { version = "2.0.0", path = "../../../primitives/serializer" } +sp-serializer = { version = "3.0.0", path = "../../../primitives/serializer" } pretty_assertions = "0.6.1" [features] diff --git a/bin/node/rpc-client/Cargo.toml b/bin/node/rpc-client/Cargo.toml index e88a18032698e..1d9819de24b6d 100644 --- a/bin/node/rpc-client/Cargo.toml +++ b/bin/node/rpc-client/Cargo.toml @@ -16,5 +16,5 @@ hyper = "~0.12.35" jsonrpc-core-client = { version = "15.1.0", default-features = false, features = ["http"] } log = "0.4.8" node-primitives = { version = "2.0.0", path = "../primitives" } -sp-tracing = { version = "2.0.0", path = "../../../primitives/tracing" } -sc-rpc = { version = "2.0.0", path = "../../../client/rpc" } +sp-tracing = { version = "3.0.0", path = "../../../primitives/tracing" } +sc-rpc = { version = "3.0.0", path = "../../../client/rpc" } diff --git a/bin/node/rpc/Cargo.toml b/bin/node/rpc/Cargo.toml index 10d7fe80d7ce9..3e0e77e030f10 100644 --- a/bin/node/rpc/Cargo.toml +++ b/bin/node/rpc/Cargo.toml @@ -15,24 +15,24 @@ jsonrpc-core = "15.1.0" node-primitives = { version = "2.0.0", path = "../primitives" } node-runtime = { version = "2.0.0", path = "../runtime" } pallet-contracts-rpc = { version = "0.8.0", path = "../../../frame/contracts/rpc/" } -pallet-transaction-payment-rpc = { version = "2.0.0", path = "../../../frame/transaction-payment/rpc/" } -sc-client-api = { version = "2.0.0", path = "../../../client/api" } -sc-consensus-babe = { version = "0.8.0", path = "../../../client/consensus/babe" } -sc-consensus-babe-rpc = { version = "0.8.0", path = "../../../client/consensus/babe/rpc" } -sc-consensus-epochs = { version = "0.8.0", path = "../../../client/consensus/epochs" } -sc-chain-spec = { version = "2.0.0", path = "../../../client/chain-spec" } -sc-finality-grandpa = { version = "0.8.0", path = "../../../client/finality-grandpa" } -sc-finality-grandpa-rpc = { version = "0.8.0", path = "../../../client/finality-grandpa/rpc" } -sc-keystore = { version = "2.0.0", path = "../../../client/keystore" } -sc-rpc-api = { version = "0.8.0", path = "../../../client/rpc-api" } -sc-rpc = { version = "2.0.0", path = "../../../client/rpc" } -sc-sync-state-rpc = { version = "0.8.0", path = "../../../client/sync-state-rpc" } -sp-api = { version = "2.0.0", path = "../../../primitives/api" } -sp-block-builder = { version = "2.0.0", path = "../../../primitives/block-builder" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-keystore = { version = "0.8.0", path = "../../../primitives/keystore" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -sp-consensus-babe = { version = "0.8.0", path = "../../../primitives/consensus/babe" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-transaction-pool = { version = "2.0.0", path = "../../../primitives/transaction-pool" } -substrate-frame-rpc-system = { version = "2.0.0", path = "../../../utils/frame/rpc/system" } +pallet-transaction-payment-rpc = { version = "3.0.0", path = "../../../frame/transaction-payment/rpc/" } +sc-client-api = { version = "3.0.0", path = "../../../client/api" } +sc-consensus-babe = { version = "0.9.0", path = "../../../client/consensus/babe" } +sc-consensus-babe-rpc = { version = "0.9.0", path = "../../../client/consensus/babe/rpc" } +sc-consensus-epochs = { version = "0.9.0", path = "../../../client/consensus/epochs" } +sc-chain-spec = { version = "3.0.0", path = "../../../client/chain-spec" } +sc-finality-grandpa = { version = "0.9.0", path = "../../../client/finality-grandpa" } +sc-finality-grandpa-rpc = { version = "0.9.0", path = "../../../client/finality-grandpa/rpc" } +sc-keystore = { version = "3.0.0", path = "../../../client/keystore" } +sc-rpc-api = { version = "0.9.0", path = "../../../client/rpc-api" } +sc-rpc = { version = "3.0.0", path = "../../../client/rpc" } +sc-sync-state-rpc = { version = "0.9.0", path = "../../../client/sync-state-rpc" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } +sp-block-builder = { version = "3.0.0", path = "../../../primitives/block-builder" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sp-consensus-babe = { version = "0.9.0", path = "../../../primitives/consensus/babe" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-transaction-pool = { version = "3.0.0", path = "../../../primitives/transaction-pool" } +substrate-frame-rpc-system = { version = "3.0.0", path = "../../../utils/frame/rpc/system" } diff --git a/bin/node/runtime/Cargo.toml b/bin/node/runtime/Cargo.toml index 0aa9044c4c6ff..27000656570d5 100644 --- a/bin/node/runtime/Cargo.toml +++ b/bin/node/runtime/Cargo.toml @@ -14,81 +14,82 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] # third-party dependencies -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } serde = { version = "1.0.102", optional = true } static_assertions = "1.1.0" hex-literal = { version = "0.3.1", optional = true } # primitives -sp-authority-discovery = { version = "2.0.0", default-features = false, path = "../../../primitives/authority-discovery" } -sp-consensus-babe = { version = "0.8.0", default-features = false, path = "../../../primitives/consensus/babe" } -sp-block-builder = { path = "../../../primitives/block-builder", default-features = false, version = "2.0.0"} -sp-inherents = { version = "2.0.0", default-features = false, path = "../../../primitives/inherents" } +sp-authority-discovery = { version = "3.0.0", default-features = false, path = "../../../primitives/authority-discovery" } +sp-consensus-babe = { version = "0.9.0", default-features = false, path = "../../../primitives/consensus/babe" } +sp-block-builder = { path = "../../../primitives/block-builder", default-features = false, version = "3.0.0"} +sp-inherents = { version = "3.0.0", default-features = false, path = "../../../primitives/inherents" } node-primitives = { version = "2.0.0", default-features = false, path = "../primitives" } -sp-offchain = { version = "2.0.0", default-features = false, path = "../../../primitives/offchain" } -sp-core = { version = "2.0.0", default-features = false, path = "../../../primitives/core" } -sp-io = { version = "2.0.0", default-features = false, path = "../../../primitives/io" } -sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } -sp-api = { version = "2.0.0", default-features = false, path = "../../../primitives/api" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } -sp-staking = { version = "2.0.0", default-features = false, path = "../../../primitives/staking" } -sp-keyring = { version = "2.0.0", optional = true, path = "../../../primitives/keyring" } -sp-session = { version = "2.0.0", default-features = false, path = "../../../primitives/session" } -sp-transaction-pool = { version = "2.0.0", default-features = false, path = "../../../primitives/transaction-pool" } -sp-version = { version = "2.0.0", default-features = false, path = "../../../primitives/version" } +sp-io = { version = "3.0.0", path = "../../../primitives/io", default-features = false } +sp-offchain = { version = "3.0.0", default-features = false, path = "../../../primitives/offchain" } +sp-core = { version = "3.0.0", default-features = false, path = "../../../primitives/core" } +sp-std = { version = "3.0.0", default-features = false, path = "../../../primitives/std" } +sp-api = { version = "3.0.0", default-features = false, path = "../../../primitives/api" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../primitives/runtime" } +sp-staking = { version = "3.0.0", default-features = false, path = "../../../primitives/staking" } +sp-keyring = { version = "3.0.0", optional = true, path = "../../../primitives/keyring" } +sp-session = { version = "3.0.0", default-features = false, path = "../../../primitives/session" } +sp-transaction-pool = { version = "3.0.0", default-features = false, path = "../../../primitives/transaction-pool" } +sp-version = { version = "3.0.0", default-features = false, path = "../../../primitives/version" } # frame dependencies -frame-executive = { version = "2.0.0", default-features = false, path = "../../../frame/executive" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../../../frame/benchmarking", optional = true } -frame-support = { version = "2.0.0", default-features = false, path = "../../../frame/support" } -frame-system = { version = "2.0.0", default-features = false, path = "../../../frame/system" } -frame-system-benchmarking = { version = "2.0.0", default-features = false, path = "../../../frame/system/benchmarking", optional = true } -frame-system-rpc-runtime-api = { version = "2.0.0", default-features = false, path = "../../../frame/system/rpc/runtime-api/" } -pallet-authority-discovery = { version = "2.0.0", default-features = false, path = "../../../frame/authority-discovery" } -pallet-authorship = { version = "2.0.0", default-features = false, path = "../../../frame/authorship" } -pallet-babe = { version = "2.0.0", default-features = false, path = "../../../frame/babe" } -pallet-balances = { version = "2.0.0", default-features = false, path = "../../../frame/balances" } -pallet-bounties = { version = "2.0.0", default-features = false, path = "../../../frame/bounties" } -pallet-collective = { version = "2.0.0", default-features = false, path = "../../../frame/collective" } +frame-executive = { version = "3.0.0", default-features = false, path = "../../../frame/executive" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../../../frame/benchmarking", optional = true } +frame-support = { version = "3.0.0", default-features = false, path = "../../../frame/support" } +frame-system = { version = "3.0.0", default-features = false, path = "../../../frame/system" } +frame-system-benchmarking = { version = "3.0.0", default-features = false, path = "../../../frame/system/benchmarking", optional = true } +frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/system/rpc/runtime-api/" } +frame-try-runtime = { version = "0.9.0", default-features = false, path = "../../../frame/try-runtime", optional = true } +pallet-authority-discovery = { version = "3.0.0", default-features = false, path = "../../../frame/authority-discovery" } +pallet-authorship = { version = "3.0.0", default-features = false, path = "../../../frame/authorship" } +pallet-babe = { version = "3.0.0", default-features = false, path = "../../../frame/babe" } +pallet-balances = { version = "3.0.0", default-features = false, path = "../../../frame/balances" } +pallet-bounties = { version = "3.0.0", default-features = false, path = "../../../frame/bounties" } +pallet-collective = { version = "3.0.0", default-features = false, path = "../../../frame/collective" } pallet-contracts = { version = "2.0.0", default-features = false, path = "../../../frame/contracts" } pallet-contracts-primitives = { version = "2.0.0", default-features = false, path = "../../../frame/contracts/common/" } pallet-contracts-rpc-runtime-api = { version = "0.8.0", default-features = false, path = "../../../frame/contracts/rpc/runtime-api/" } -pallet-democracy = { version = "2.0.0", default-features = false, path = "../../../frame/democracy" } +pallet-democracy = { version = "3.0.0", default-features = false, path = "../../../frame/democracy" } pallet-elections-phragmen = { version = "3.0.0", default-features = false, path = "../../../frame/elections-phragmen" } -pallet-grandpa = { version = "2.0.0", default-features = false, path = "../../../frame/grandpa" } -pallet-im-online = { version = "2.0.0", default-features = false, path = "../../../frame/im-online" } -pallet-indices = { version = "2.0.0", default-features = false, path = "../../../frame/indices" } -pallet-identity = { version = "2.0.0", default-features = false, path = "../../../frame/identity" } -pallet-membership = { version = "2.0.0", default-features = false, path = "../../../frame/membership" } -pallet-multisig = { version = "2.0.0", default-features = false, path = "../../../frame/multisig" } -pallet-offences = { version = "2.0.0", default-features = false, path = "../../../frame/offences" } -pallet-offences-benchmarking = { version = "2.0.0", path = "../../../frame/offences/benchmarking", default-features = false, optional = true } -pallet-proxy = { version = "2.0.0", default-features = false, path = "../../../frame/proxy" } -pallet-randomness-collective-flip = { version = "2.0.0", default-features = false, path = "../../../frame/randomness-collective-flip" } -pallet-recovery = { version = "2.0.0", default-features = false, path = "../../../frame/recovery" } -pallet-session = { version = "2.0.0", features = ["historical"], path = "../../../frame/session", default-features = false } -pallet-session-benchmarking = { version = "2.0.0", path = "../../../frame/session/benchmarking", default-features = false, optional = true } -pallet-staking = { version = "2.0.0", default-features = false, path = "../../../frame/staking" } -pallet-staking-reward-curve = { version = "2.0.0", default-features = false, path = "../../../frame/staking/reward-curve" } -pallet-scheduler = { version = "2.0.0", default-features = false, path = "../../../frame/scheduler" } -pallet-society = { version = "2.0.0", default-features = false, path = "../../../frame/society" } -pallet-sudo = { version = "2.0.0", default-features = false, path = "../../../frame/sudo" } -pallet-timestamp = { version = "2.0.0", default-features = false, path = "../../../frame/timestamp" } -pallet-tips = { version = "2.0.0", default-features = false, path = "../../../frame/tips" } -pallet-treasury = { version = "2.0.0", default-features = false, path = "../../../frame/treasury" } -pallet-utility = { version = "2.0.0", default-features = false, path = "../../../frame/utility" } -pallet-transaction-payment = { version = "2.0.0", default-features = false, path = "../../../frame/transaction-payment" } -pallet-transaction-payment-rpc-runtime-api = { version = "2.0.0", default-features = false, path = "../../../frame/transaction-payment/rpc/runtime-api/" } -pallet-vesting = { version = "2.0.0", default-features = false, path = "../../../frame/vesting" } -pallet-cere-ddc = { version = "4.0.0", default-features = false, path = "../../../frame/ddc-pallet" } +pallet-election-provider-multi-phase = { version = "3.0.0", default-features = false, path = "../../../frame/election-provider-multi-phase" } +pallet-grandpa = { version = "3.0.0", default-features = false, path = "../../../frame/grandpa" } +pallet-im-online = { version = "3.0.0", default-features = false, path = "../../../frame/im-online" } +pallet-indices = { version = "3.0.0", default-features = false, path = "../../../frame/indices" } +pallet-identity = { version = "3.0.0", default-features = false, path = "../../../frame/identity" } +pallet-membership = { version = "3.0.0", default-features = false, path = "../../../frame/membership" } +pallet-multisig = { version = "3.0.0", default-features = false, path = "../../../frame/multisig" } +pallet-offences = { version = "3.0.0", default-features = false, path = "../../../frame/offences" } +pallet-offences-benchmarking = { version = "3.0.0", path = "../../../frame/offences/benchmarking", default-features = false, optional = true } +pallet-proxy = { version = "3.0.0", default-features = false, path = "../../../frame/proxy" } +pallet-randomness-collective-flip = { version = "3.0.0", default-features = false, path = "../../../frame/randomness-collective-flip" } +pallet-recovery = { version = "3.0.0", default-features = false, path = "../../../frame/recovery" } +pallet-session = { version = "3.0.0", features = ["historical"], path = "../../../frame/session", default-features = false } +pallet-session-benchmarking = { version = "3.0.0", path = "../../../frame/session/benchmarking", default-features = false, optional = true } +pallet-staking = { version = "3.0.0", default-features = false, path = "../../../frame/staking" } +pallet-staking-reward-curve = { version = "3.0.0", default-features = false, path = "../../../frame/staking/reward-curve" } +pallet-scheduler = { version = "3.0.0", default-features = false, path = "../../../frame/scheduler" } +pallet-society = { version = "3.0.0", default-features = false, path = "../../../frame/society" } +pallet-sudo = { version = "3.0.0", default-features = false, path = "../../../frame/sudo" } +pallet-timestamp = { version = "3.0.0", default-features = false, path = "../../../frame/timestamp" } +pallet-tips = { version = "3.0.0", default-features = false, path = "../../../frame/tips" } +pallet-treasury = { version = "3.0.0", default-features = false, path = "../../../frame/treasury" } +pallet-utility = { version = "3.0.0", default-features = false, path = "../../../frame/utility" } +pallet-transaction-payment = { version = "3.0.0", default-features = false, path = "../../../frame/transaction-payment" } +pallet-transaction-payment-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/transaction-payment/rpc/runtime-api/" } +pallet-vesting = { version = "3.0.0", default-features = false, path = "../../../frame/vesting" } pallet-chainbridge = { version = "2.0.0", default-features = false, path = "../../../frame/chainbridge" } +pallet-cere-ddc = { version = "4.0.0", default-features = false, path = "../../../frame/ddc-pallet" } pallet-erc721 = { version = "2.0.0", default-features = false, path = "../../../frame/erc721" } pallet-erc20 = { version = "2.0.0", default-features = false, path = "../../../frame/erc20" } - -pallet-ddc-metrics-offchain-worker = { path = "../../../frame/ddc-metrics-offchain-worker", default-features = false } +pallet-ddc-metrics-offchain-worker = { version = "2.0.0", default-features = false, path = "../../../frame/ddc-metrics-offchain-worker" } [build-dependencies] -substrate-wasm-builder = { version = "3.0.0", path = "../../../utils/wasm-builder" } +substrate-wasm-builder = { version = "4.0.0", path = "../../../utils/wasm-builder" } [features] default = ["std"] @@ -143,6 +144,7 @@ std = [ "frame-benchmarking/std", "frame-system-rpc-runtime-api/std", "frame-system/std", + "pallet-election-provider-multi-phase/std", "pallet-timestamp/std", "pallet-tips/std", "pallet-transaction-payment-rpc-runtime-api/std", @@ -160,6 +162,7 @@ runtime-benchmarks = [ "frame-benchmarking", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", + "pallet-election-provider-multi-phase/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "pallet-babe/runtime-benchmarks", "pallet-balances/runtime-benchmarks", @@ -188,3 +191,7 @@ runtime-benchmarks = [ "frame-system-benchmarking", "hex-literal", ] +try-runtime = [ + "frame-executive/try-runtime", + "frame-try-runtime", +] diff --git a/bin/node/runtime/src/constants.rs b/bin/node/runtime/src/constants.rs index 2181a80b02b4e..840324f94fbd7 100644 --- a/bin/node/runtime/src/constants.rs +++ b/bin/node/runtime/src/constants.rs @@ -35,7 +35,7 @@ pub mod time { use node_primitives::{Moment, BlockNumber}; /// Since BABE is probabilistic this is the average expected block time that - /// we are targetting. Blocks will be produced at a minimum duration defined + /// we are targeting. Blocks will be produced at a minimum duration defined /// by `SLOT_DURATION`, but some slots will not be allocated to any /// authority and hence no block will be produced. We expect to have this /// block time on average following the defined slot duration and the value diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 69eb0053c3af0..050301cf2406f 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -28,7 +28,8 @@ use frame_support::{ construct_runtime, parameter_types, debug, RuntimeDebug, weights::{ Weight, IdentityFee, - constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND}, DispatchClass, + constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND}, + DispatchClass, }, traits::{ Currency, Imbalance, KeyOwnerProofSystem, OnUnbalanced, Randomness, LockIdentifier, @@ -53,14 +54,14 @@ pub use node_primitives::{AccountId, Signature}; use node_primitives::{AccountIndex, Balance, BlockNumber, Hash, Index, Moment}; use sp_api::impl_runtime_apis; use sp_runtime::{ - Permill, Perbill, Perquintill, Percent, ApplyExtrinsicResult, - impl_opaque_keys, generic, create_runtime_str, ModuleId, FixedPointNumber, + Permill, Perbill, Perquintill, Percent, ApplyExtrinsicResult, impl_opaque_keys, generic, + create_runtime_str, ModuleId, FixedPointNumber, }; use sp_runtime::curve::PiecewiseLinear; use sp_runtime::transaction_validity::{TransactionValidity, TransactionSource, TransactionPriority}; use sp_runtime::traits::{ - self, BlakeTwo256, Block as BlockT, StaticLookup, SaturatedConversion, - ConvertInto, OpaqueKeys, NumberFor, + self, BlakeTwo256, Block as BlockT, StaticLookup, SaturatedConversion, ConvertInto, OpaqueKeys, + NumberFor, }; use sp_version::RuntimeVersion; #[cfg(any(feature = "std", test))] @@ -74,7 +75,7 @@ pub use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment, Currency use pallet_session::{historical as pallet_session_historical}; use sp_inherents::{InherentData, CheckInherentsResult}; use static_assertions::const_assert; -use pallet_contracts::WeightInfo; +use pallet_contracts::weights::WeightInfo; pub use pallet_cere_ddc; pub use pallet_chainbridge; pub use pallet_ddc_metrics_offchain_worker; @@ -118,7 +119,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 283, + spec_version: 284, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 2, @@ -151,7 +152,7 @@ impl OnUnbalanced for DealWithFees { } } -/// We assume that ~10% of the block weight is consumed by `on_initalize` handlers. +/// We assume that ~10% of the block weight is consumed by `on_initialize` handlers. /// This is used to limit the maximal weight of a single extrinsic. const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10); /// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used @@ -325,6 +326,8 @@ impl pallet_scheduler::Config for Runtime { parameter_types! { pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS; pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK; + pub const ReportLongevity: u64 = + BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * EpochDuration::get(); } impl pallet_babe::Config for Runtime { @@ -345,7 +348,7 @@ impl pallet_babe::Config for Runtime { )>>::IdentificationTuple; type HandleEquivocation = - pallet_babe::EquivocationHandler; + pallet_babe::EquivocationHandler; type WeightInfo = (); } @@ -494,18 +497,56 @@ impl pallet_staking::Config for Runtime { type SessionInterface = Self; type RewardCurve = RewardCurve; type NextNewSession = Session; + type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator; type ElectionLookahead = ElectionLookahead; type Call = Call; type MaxIterations = MaxIterations; type MinSolutionScoreBump = MinSolutionScoreBump; - type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator; type UnsignedPriority = StakingUnsignedPriority; // The unsigned solution weight targeted by the OCW. We set it to the maximum possible value of // a single extrinsic. type OffchainSolutionWeightLimit = OffchainSolutionWeightLimit; + type ElectionProvider = ElectionProviderMultiPhase; type WeightInfo = pallet_staking::weights::SubstrateWeight; } +parameter_types! { + // phase durations. 1/4 of the last session for each. + pub const SignedPhase: u32 = EPOCH_DURATION_IN_BLOCKS / 4; + pub const UnsignedPhase: u32 = EPOCH_DURATION_IN_BLOCKS / 4; + + // fallback: no need to do on-chain phragmen initially. + pub const Fallback: pallet_election_provider_multi_phase::FallbackStrategy = + pallet_election_provider_multi_phase::FallbackStrategy::Nothing; + + pub SolutionImprovementThreshold: Perbill = Perbill::from_rational_approximation(1u32, 10_000); + + // miner configs + pub const MultiPhaseUnsignedPriority: TransactionPriority = StakingUnsignedPriority::get() - 1u64; + pub const MinerMaxIterations: u32 = 10; + pub MinerMaxWeight: Weight = RuntimeBlockWeights::get() + .get(DispatchClass::Normal) + .max_extrinsic.expect("Normal extrinsics have a weight limit configured; qed") + .saturating_sub(BlockExecutionWeight::get()); +} + +impl pallet_election_provider_multi_phase::Config for Runtime { + type Event = Event; + type Currency = Balances; + type SignedPhase = SignedPhase; + type UnsignedPhase = UnsignedPhase; + type SolutionImprovementThreshold = MinSolutionScoreBump; + type MinerMaxIterations = MinerMaxIterations; + type MinerMaxWeight = MinerMaxWeight; + type MinerTxPriority = MultiPhaseUnsignedPriority; + type DataProvider = Staking; + type OnChainAccuracy = Perbill; + type CompactSolution = pallet_staking::CompactAssignments; + type Fallback = Fallback; + type WeightInfo = pallet_election_provider_multi_phase::weights::SubstrateWeight; + type BenchmarkingConfig = (); +} + parameter_types! { pub const LaunchPeriod: BlockNumber = 1 * 24 * 60 * MINUTES; pub const VotingPeriod: BlockNumber = 1 * 24 * 60 * MINUTES; @@ -552,7 +593,7 @@ impl pallet_democracy::Config for Runtime { >; type BlacklistOrigin = EnsureRoot; // Any single technical committee member may veto a coming council proposal, however they can - // only do it once and it lasts only for the cooloff period. + // only do it once and it lasts only for the cool-off period. type VetoOrigin = pallet_collective::EnsureMember; type CooloffPeriod = CooloffPeriod; type PreimageByteDeposit = PreimageByteDeposit; @@ -738,6 +779,8 @@ parameter_types! { ::WeightInfo::on_initialize_per_queue_item(1) - ::WeightInfo::on_initialize_per_queue_item(0) )) / 5) as u32; + // Make it possible to upload ddc.wasm + pub MaxCodeSize: u32 = 160 * 1024; } impl pallet_contracts::Config for Runtime { @@ -760,6 +803,7 @@ impl pallet_contracts::Config for Runtime { type ChainExtension = (); type DeletionQueueDepth = DeletionQueueDepth; type DeletionWeightLimit = DeletionWeightLimit; + type MaxCodeSize = MaxCodeSize; } impl pallet_sudo::Config for Runtime { @@ -835,6 +879,7 @@ impl frame_system::offchain::SendTransactionTypes for Runtime where impl pallet_im_online::Config for Runtime { type AuthorityId = ImOnlineId; type Event = Event; + type ValidatorSet = Historical; type SessionDuration = SessionDuration; type ReportUnresponsiveness = Offences; type UnsignedPriority = ImOnlineUnsignedPriority; @@ -870,7 +915,7 @@ impl pallet_grandpa::Config for Runtime { )>>::IdentificationTuple; type HandleEquivocation = - pallet_grandpa::EquivocationHandler; + pallet_grandpa::EquivocationHandler; type WeightInfo = (); } @@ -1020,40 +1065,6 @@ impl pallet_ddc_metrics_offchain_worker::Config for Runtime { type Call = Call; } -pub struct CustomOnRuntimeUpgrade; -impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { - fn on_runtime_upgrade() -> frame_support::weights::Weight { - // Update scheduler origin usage - #[derive(Encode, Decode)] - #[allow(non_camel_case_types)] - pub enum OldOriginCaller { - system(frame_system::Origin), - pallet_collective_Instance1( - pallet_collective::Origin - ), - pallet_collective_Instance2( - pallet_collective::Origin - ), - } - - impl Into for OldOriginCaller { - fn into(self) -> OriginCaller { - match self { - OldOriginCaller::system(o) => OriginCaller::system(o), - OldOriginCaller::pallet_collective_Instance1(o) => - OriginCaller::pallet_collective_Instance1(o), - OldOriginCaller::pallet_collective_Instance2(o) => - OriginCaller::pallet_collective_Instance2(o), - } - } - } - - pallet_scheduler::Module::::migrate_origin::(); - - RuntimeBlockWeights::get().max_block - } -} - construct_runtime!( pub enum Runtime where Block = Block, @@ -1062,12 +1073,13 @@ construct_runtime!( { System: frame_system::{Module, Call, Config, Storage, Event}, Utility: pallet_utility::{Module, Call, Event}, - Babe: pallet_babe::{Module, Call, Storage, Config, Inherent, ValidateUnsigned}, + Babe: pallet_babe::{Module, Call, Storage, Config, ValidateUnsigned}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, Authorship: pallet_authorship::{Module, Call, Storage, Inherent}, Indices: pallet_indices::{Module, Call, Storage, Config, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, TransactionPayment: pallet_transaction_payment::{Module, Storage}, + ElectionProviderMultiPhase: pallet_election_provider_multi_phase::{Module, Call, Storage, Event, ValidateUnsigned}, Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, Session: pallet_session::{Module, Call, Storage, Event, Config}, Democracy: pallet_democracy::{Module, Call, Storage, Config, Event}, @@ -1138,21 +1150,9 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllModules, - PhragmenElectionDepositRuntimeUpgrade, + (), >; -pub struct PhragmenElectionDepositRuntimeUpgrade; -impl pallet_elections_phragmen::migrations_3_0_0::V2ToV3 for PhragmenElectionDepositRuntimeUpgrade { - type AccountId = AccountId; - type Balance = Balance; - type Module = Elections; -} -impl frame_support::traits::OnRuntimeUpgrade for PhragmenElectionDepositRuntimeUpgrade { - fn on_runtime_upgrade() -> frame_support::weights::Weight { - pallet_elections_phragmen::migrations_3_0_0::apply::(1 * CENTS, DOLLARS) - } -} - impl_runtime_apis! { impl sp_api::Core for Runtime { fn version() -> RuntimeVersion { @@ -1260,7 +1260,7 @@ impl_runtime_apis! { } } - fn current_epoch_start() -> sp_consensus_babe::SlotNumber { + fn current_epoch_start() -> sp_consensus_babe::Slot { Babe::current_epoch_start() } @@ -1273,7 +1273,7 @@ impl_runtime_apis! { } fn generate_key_ownership_proof( - _slot_number: sp_consensus_babe::SlotNumber, + _slot: sp_consensus_babe::Slot, authority_id: sp_consensus_babe::AuthorityId, ) -> Option { use codec::Encode; @@ -1359,15 +1359,24 @@ impl_runtime_apis! { } } + #[cfg(feature = "try-runtime")] + impl frame_try_runtime::TryRuntime for Runtime { + fn on_runtime_upgrade() -> Result<(Weight, Weight), sp_runtime::RuntimeString> { + frame_support::debug::RuntimeLogger::init(); + let weight = Executive::try_runtime_upgrade()?; + Ok((weight, RuntimeBlockWeights::get().max_block)) + } + } + #[cfg(feature = "runtime-benchmarks")] impl frame_benchmarking::Benchmark for Runtime { fn dispatch_benchmark( config: frame_benchmarking::BenchmarkConfig ) -> Result, sp_runtime::RuntimeString> { use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey}; - // Trying to add benchmarks directly to the Session Pallet caused cyclic dependency issues. - // To get around that, we separated the Session benchmarks into its own crate, which is why - // we need these two lines below. + // Trying to add benchmarks directly to the Session Pallet caused cyclic dependency + // issues. To get around that, we separated the Session benchmarks into its own crate, + // which is why we need these two lines below. use pallet_session_benchmarking::Module as SessionBench; use pallet_offences_benchmarking::Module as OffencesBench; use frame_system_benchmarking::Module as SystemBench; @@ -1401,6 +1410,7 @@ impl_runtime_apis! { add_benchmark!(params, batches, pallet_contracts, Contracts); add_benchmark!(params, batches, pallet_democracy, Democracy); add_benchmark!(params, batches, pallet_elections_phragmen, Elections); + add_benchmark!(params, batches, pallet_election_provider_multi_phase, ElectionProviderMultiPhase); add_benchmark!(params, batches, pallet_grandpa, Grandpa); add_benchmark!(params, batches, pallet_identity, Identity); add_benchmark!(params, batches, pallet_im_online, ImOnline); diff --git a/bin/node/testing/Cargo.toml b/bin/node/testing/Cargo.toml index f6cf92d77e8e8..95bc8abef6fc9 100644 --- a/bin/node/testing/Cargo.toml +++ b/bin/node/testing/Cargo.toml @@ -13,38 +13,38 @@ publish = true targets = ["x86_64-unknown-linux-gnu"] [dependencies] -pallet-balances = { version = "2.0.0", path = "../../../frame/balances" } -sc-service = { version = "0.8.0", features = ["test-helpers", "db"], path = "../../../client/service" } -sc-client-db = { version = "0.8.0", path = "../../../client/db/", features = ["kvdb-rocksdb", "parity-db"] } -sc-client-api = { version = "2.0.0", path = "../../../client/api/" } -codec = { package = "parity-scale-codec", version = "1.3.6" } +pallet-balances = { version = "3.0.0", path = "../../../frame/balances" } +sc-service = { version = "0.9.0", features = ["test-helpers", "db"], path = "../../../client/service" } +sc-client-db = { version = "0.9.0", path = "../../../client/db/", features = ["kvdb-rocksdb", "parity-db"] } +sc-client-api = { version = "3.0.0", path = "../../../client/api/" } +codec = { package = "parity-scale-codec", version = "2.0.0" } pallet-contracts = { version = "2.0.0", path = "../../../frame/contracts" } -pallet-grandpa = { version = "2.0.0", path = "../../../frame/grandpa" } -pallet-indices = { version = "2.0.0", path = "../../../frame/indices" } -sp-keyring = { version = "2.0.0", path = "../../../primitives/keyring" } +pallet-grandpa = { version = "3.0.0", path = "../../../frame/grandpa" } +pallet-indices = { version = "3.0.0", path = "../../../frame/indices" } +sp-keyring = { version = "3.0.0", path = "../../../primitives/keyring" } node-executor = { version = "2.0.0", path = "../executor" } node-primitives = { version = "2.0.0", path = "../primitives" } node-runtime = { version = "2.0.0", path = "../runtime" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-io = { version = "2.0.0", path = "../../../primitives/io" } -frame-support = { version = "2.0.0", path = "../../../frame/support" } -pallet-session = { version = "2.0.0", path = "../../../frame/session" } -pallet-society = { version = "2.0.0", path = "../../../frame/society" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -pallet-staking = { version = "2.0.0", path = "../../../frame/staking" } -sc-executor = { version = "0.8.0", path = "../../../client/executor", features = ["wasmtime"] } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -frame-system = { version = "2.0.0", path = "../../../frame/system" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-io = { version = "3.0.0", path = "../../../primitives/io" } +frame-support = { version = "3.0.0", path = "../../../frame/support" } +pallet-session = { version = "3.0.0", path = "../../../frame/session" } +pallet-society = { version = "3.0.0", path = "../../../frame/society" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +pallet-staking = { version = "3.0.0", path = "../../../frame/staking" } +sc-executor = { version = "0.9.0", path = "../../../client/executor", features = ["wasmtime"] } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +frame-system = { version = "3.0.0", path = "../../../frame/system" } substrate-test-client = { version = "2.0.0", path = "../../../test-utils/client" } -pallet-timestamp = { version = "2.0.0", path = "../../../frame/timestamp" } -pallet-transaction-payment = { version = "2.0.0", path = "../../../frame/transaction-payment" } -pallet-treasury = { version = "2.0.0", path = "../../../frame/treasury" } -sp-api = { version = "2.0.0", path = "../../../primitives/api" } -sp-timestamp = { version = "2.0.0", default-features = false, path = "../../../primitives/timestamp" } -sp-block-builder = { version = "2.0.0", path = "../../../primitives/block-builder" } -sc-block-builder = { version = "0.8.0", path = "../../../client/block-builder" } -sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } +pallet-timestamp = { version = "3.0.0", path = "../../../frame/timestamp" } +pallet-transaction-payment = { version = "3.0.0", path = "../../../frame/transaction-payment" } +pallet-treasury = { version = "3.0.0", path = "../../../frame/treasury" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } +sp-timestamp = { version = "3.0.0", default-features = false, path = "../../../primitives/timestamp" } +sp-block-builder = { version = "3.0.0", path = "../../../primitives/block-builder" } +sc-block-builder = { version = "0.9.0", path = "../../../client/block-builder" } +sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } log = "0.4.8" tempfile = "3.1.0" fs_extra = "1" @@ -52,4 +52,4 @@ futures = "0.3.1" [dev-dependencies] criterion = "0.3.0" -sc-cli = { version = "0.8.0", path = "../../../client/cli" } +sc-cli = { version = "0.9.0", path = "../../../client/cli" } diff --git a/bin/utils/chain-spec-builder/Cargo.toml b/bin/utils/chain-spec-builder/Cargo.toml index a57dadd26bda8..3c60d654db944 100644 --- a/bin/utils/chain-spec-builder/Cargo.toml +++ b/bin/utils/chain-spec-builder/Cargo.toml @@ -14,10 +14,10 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] ansi_term = "0.12.1" -sc-keystore = { version = "2.0.0", path = "../../../client/keystore" } -sc-chain-spec = { version = "2.0.0", path = "../../../client/chain-spec" } +sc-keystore = { version = "3.0.0", path = "../../../client/keystore" } +sc-chain-spec = { version = "3.0.0", path = "../../../client/chain-spec" } node-cli = { version = "2.0.0", path = "../../node/cli" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-keystore = { version = "0.8.0", path = "../../../primitives/keystore" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" } rand = "0.7.2" structopt = "0.3.8" diff --git a/bin/utils/subkey/Cargo.toml b/bin/utils/subkey/Cargo.toml index e445749c2c2ea..b0c71a4fc3327 100644 --- a/bin/utils/subkey/Cargo.toml +++ b/bin/utils/subkey/Cargo.toml @@ -8,13 +8,13 @@ homepage = "https://substrate.dev" repository = "https://github.com/paritytech/substrate/" readme = "README.md" +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + [[bin]] path = "src/main.rs" name = "subkey" -[package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] - [dependencies] -sc-cli = { version = "0.8.0", path = "../../../client/cli" } +sc-cli = { version = "0.9.0", path = "../../../client/cli" } structopt = "0.3.14" diff --git a/client/api/Cargo.toml b/client/api/Cargo.toml index 205d5a51cde34..637dae4a29abd 100644 --- a/client/api/Cargo.toml +++ b/client/api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-client-api" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -14,36 +14,36 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-consensus = { version = "0.8.0", path = "../../primitives/consensus/common" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } derive_more = "0.99.2" -sc-executor = { version = "0.8.0", path = "../executor" } -sp-externalities = { version = "0.8.0", path = "../../primitives/externalities" } +sc-executor = { version = "0.9.0", path = "../executor" } +sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" } fnv = "1.0.6" futures = "0.3.1" hash-db = { version = "0.15.2", default-features = false } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-inherents = { version = "2.0.0", default-features = false, path = "../../primitives/inherents" } -kvdb = "0.8.0" +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-inherents = { version = "3.0.0", default-features = false, path = "../../primitives/inherents" } +kvdb = "0.9.0" log = "0.4.8" parking_lot = "0.11.1" lazy_static = "1.4.0" -sp-database = { version = "2.0.0", path = "../../primitives/database" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-keystore = { version = "0.8.0", default-features = false, path = "../../primitives/keystore" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-version = { version = "2.0.0", default-features = false, path = "../../primitives/version" } -sp-api = { version = "2.0.0", path = "../../primitives/api" } -sp-utils = { version = "2.0.0", path = "../../primitives/utils" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine" } -sp-trie = { version = "2.0.0", path = "../../primitives/trie" } -sp-storage = { version = "2.0.0", path = "../../primitives/storage" } -sp-transaction-pool = { version = "2.0.0", path = "../../primitives/transaction-pool" } -prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0", path = "../../utils/prometheus" } +sp-database = { version = "3.0.0", path = "../../primitives/database" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-keystore = { version = "0.9.0", default-features = false, path = "../../primitives/keystore" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-version = { version = "3.0.0", default-features = false, path = "../../primitives/version" } +sp-api = { version = "3.0.0", path = "../../primitives/api" } +sp-utils = { version = "3.0.0", path = "../../primitives/utils" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" } +sp-trie = { version = "3.0.0", path = "../../primitives/trie" } +sp-storage = { version = "3.0.0", path = "../../primitives/storage" } +sp-transaction-pool = { version = "3.0.0", path = "../../primitives/transaction-pool" } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.9.0", path = "../../utils/prometheus" } [dev-dependencies] -kvdb-memorydb = "0.8.0" +kvdb-memorydb = "0.9.0" sp-test-primitives = { version = "2.0.0", path = "../../primitives/test-primitives" } substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" } thiserror = "1.0.21" diff --git a/client/api/src/call_executor.rs b/client/api/src/call_executor.rs index 5f1e0134a5caf..3b725bf8773a8 100644 --- a/client/api/src/call_executor.rs +++ b/client/api/src/call_executor.rs @@ -78,7 +78,7 @@ pub trait CallExecutor { Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >( &self, initialize_block_fn: IB, diff --git a/client/api/src/client.rs b/client/api/src/client.rs index 4dc2b6bb524e4..990a7908b62bb 100644 --- a/client/api/src/client.rs +++ b/client/api/src/client.rs @@ -95,6 +95,17 @@ pub trait BlockBackend { /// Get block hash by number. fn block_hash(&self, number: NumberFor) -> sp_blockchain::Result>; + + /// Get single extrinsic by hash. + fn extrinsic( + &self, + hash: &Block::Hash, + ) -> sp_blockchain::Result::Extrinsic>>; + + /// Check if extrinsic exists. + fn have_extrinsic(&self, hash: &Block::Hash) -> sp_blockchain::Result { + Ok(self.extrinsic(hash)?.is_some()) + } } /// Provide a list of potential uncle headers for a given block. diff --git a/client/api/src/execution_extensions.rs b/client/api/src/execution_extensions.rs index 68b412a0d778b..1b13f2c6cffd2 100644 --- a/client/api/src/execution_extensions.rs +++ b/client/api/src/execution_extensions.rs @@ -213,7 +213,7 @@ impl offchain::TransactionPool for TransactionPoolAdapter< let xt = match Block::Extrinsic::decode(&mut &*data) { Ok(xt) => xt, Err(e) => { - log::warn!("Unable to decode extrinsic: {:?}: {}", data, e.what()); + log::warn!("Unable to decode extrinsic: {:?}: {}", data, e); return Err(()); }, }; diff --git a/client/api/src/in_mem.rs b/client/api/src/in_mem.rs index cef52982f167b..b7060cf1d9b1b 100644 --- a/client/api/src/in_mem.rs +++ b/client/api/src/in_mem.rs @@ -386,6 +386,13 @@ impl blockchain::Backend for Blockchain { fn children(&self, _parent_hash: Block::Hash) -> sp_blockchain::Result> { unimplemented!() } + + fn extrinsic( + &self, + _hash: &Block::Hash, + ) -> sp_blockchain::Result::Extrinsic>> { + unimplemented!("Not supported by the in-mem backend.") + } } impl blockchain::ProvideCache for Blockchain { @@ -481,7 +488,6 @@ impl ProvideChtRoots for Blockchain { /// In-memory operation. pub struct BlockImportOperation { pending_block: Option>, - pending_cache: HashMap>, old_state: InMemoryBackend>, new_state: Option<> as StateBackend>>::Transaction>, aux: Vec<(Vec, Option>)>, @@ -513,9 +519,7 @@ impl backend::BlockImportOperation for BlockImportOperatio Ok(()) } - fn update_cache(&mut self, cache: HashMap>) { - self.pending_cache = cache; - } + fn update_cache(&mut self, _cache: HashMap>) {} fn update_db_storage( &mut self, @@ -630,7 +634,6 @@ impl backend::Backend for Backend where Block::Hash let old_state = self.state_at(BlockId::Hash(Default::default()))?; Ok(BlockImportOperation { pending_block: None, - pending_cache: Default::default(), old_state, new_state: None, aux: Default::default(), diff --git a/client/authority-discovery/Cargo.toml b/client/authority-discovery/Cargo.toml index d2ba2cf4152fc..4de6b5479066d 100644 --- a/client/authority-discovery/Cargo.toml +++ b/client/authority-discovery/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-authority-discovery" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" build = "build.rs" @@ -18,28 +18,28 @@ prost-build = "0.7" [dependencies] async-trait = "0.1" -codec = { package = "parity-scale-codec", default-features = false, version = "1.3.6" } +codec = { package = "parity-scale-codec", default-features = false, version = "2.0.0" } derive_more = "0.99.2" either = "1.5.3" futures = "0.3.9" futures-timer = "3.0.1" -libp2p = { version = "0.34.0", default-features = false, features = ["kad"] } +libp2p = { version = "0.35.1", default-features = false, features = ["kad"] } log = "0.4.8" -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.8.0"} +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.9.0"} prost = "0.7" rand = "0.7.2" -sc-client-api = { version = "2.0.0", path = "../api" } -sc-network = { version = "0.8.0", path = "../network" } +sc-client-api = { version = "3.0.0", path = "../api" } +sc-network = { version = "0.9.0", path = "../network" } serde_json = "1.0.41" -sp-authority-discovery = { version = "2.0.0", path = "../../primitives/authority-discovery" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-keystore = { version = "0.8.0", path = "../../primitives/keystore" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-api = { version = "2.0.0", path = "../../primitives/api" } +sp-authority-discovery = { version = "3.0.0", path = "../../primitives/authority-discovery" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-keystore = { version = "0.9.0", path = "../../primitives/keystore" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-api = { version = "3.0.0", path = "../../primitives/api" } [dev-dependencies] -quickcheck = "0.9.0" -sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } -sc-peerset = { version = "2.0.0", path = "../peerset" } +quickcheck = "1.0.3" +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } +sc-peerset = { version = "3.0.0", path = "../peerset" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client"} diff --git a/client/authority-discovery/src/lib.rs b/client/authority-discovery/src/lib.rs index 26d4396ca8830..818eb1beb3ffe 100644 --- a/client/authority-discovery/src/lib.rs +++ b/client/authority-discovery/src/lib.rs @@ -93,7 +93,7 @@ where Block: BlockT + Unpin + 'static, Network: NetworkProvider, Client: ProvideRuntimeApi + Send + Sync + 'static + HeaderBackend, - >::Api: AuthorityDiscoveryApi, + >::Api: AuthorityDiscoveryApi, DhtEventStream: Stream + Unpin, { new_worker_and_service_with_config( @@ -121,7 +121,7 @@ where Block: BlockT + Unpin + 'static, Network: NetworkProvider, Client: ProvideRuntimeApi + Send + Sync + 'static + HeaderBackend, - >::Api: AuthorityDiscoveryApi, + >::Api: AuthorityDiscoveryApi, DhtEventStream: Stream + Unpin, { let (to_worker, from_service) = mpsc::channel(0); diff --git a/client/authority-discovery/src/worker.rs b/client/authority-discovery/src/worker.rs index e47f42a445ee9..b1fb89669bf2e 100644 --- a/client/authority-discovery/src/worker.rs +++ b/client/authority-discovery/src/worker.rs @@ -132,7 +132,7 @@ where Network: NetworkProvider, Client: ProvideRuntimeApi + Send + Sync + 'static + HeaderBackend, >::Api: - AuthorityDiscoveryApi, + AuthorityDiscoveryApi, DhtEventStream: Stream + Unpin, { /// Construct a [`Worker`]. @@ -296,10 +296,10 @@ where for (sign_result, key) in signatures.into_iter().zip(keys) { let mut signed_addresses = vec![]; - // sign_with_all returns Result signature - // is generated for a public key that is supported. // Verify that all signatures exist for all provided keys. - let signature = sign_result.map_err(|_| Error::MissingSignature(key.clone()))?; + let signature = sign_result.ok() + .flatten() + .ok_or_else(|| Error::MissingSignature(key.clone()))?; schema::SignedAuthorityAddresses { addresses: serialized_addresses.clone(), signature, @@ -332,7 +332,7 @@ where .client .runtime_api() .authorities(&id) - .map_err(Error::CallingRuntime)? + .map_err(|e| Error::CallingRuntime(e.into()))? .into_iter() .filter(|id| !local_keys.contains(id.as_ref())) .collect(); @@ -546,7 +546,7 @@ where let id = BlockId::hash(client.info().best_hash); let authorities = client.runtime_api() .authorities(&id) - .map_err(Error::CallingRuntime)? + .map_err(|e| Error::CallingRuntime(e.into()))? .into_iter() .map(std::convert::Into::into) .collect::>(); diff --git a/client/authority-discovery/src/worker/addr_cache.rs b/client/authority-discovery/src/worker/addr_cache.rs index 1ad7f585e294b..13b259fbbb10d 100644 --- a/client/authority-discovery/src/worker/addr_cache.rs +++ b/client/authority-discovery/src/worker/addr_cache.rs @@ -113,7 +113,6 @@ mod tests { use libp2p::multihash::{self, Multihash}; use quickcheck::{Arbitrary, Gen, QuickCheck, TestResult}; - use rand::Rng; use sp_authority_discovery::{AuthorityId, AuthorityPair}; use sp_core::crypto::Pair; @@ -122,8 +121,8 @@ mod tests { struct TestAuthorityId(AuthorityId); impl Arbitrary for TestAuthorityId { - fn arbitrary(g: &mut G) -> Self { - let seed: [u8; 32] = g.gen(); + fn arbitrary(g: &mut Gen) -> Self { + let seed = (0..32).map(|_| u8::arbitrary(g)).collect::>(); TestAuthorityId(AuthorityPair::from_seed_slice(&seed).unwrap().public()) } } @@ -132,8 +131,8 @@ mod tests { struct TestMultiaddr(Multiaddr); impl Arbitrary for TestMultiaddr { - fn arbitrary(g: &mut G) -> Self { - let seed: [u8; 32] = g.gen(); + fn arbitrary(g: &mut Gen) -> Self { + let seed = (0..32).map(|_| u8::arbitrary(g)).collect::>(); let peer_id = PeerId::from_multihash( Multihash::wrap(multihash::Code::Sha2_256.into(), &seed).unwrap() ).unwrap(); diff --git a/client/authority-discovery/src/worker/tests.rs b/client/authority-discovery/src/worker/tests.rs index 20c4c937096a1..04f597aa26b03 100644 --- a/client/authority-discovery/src/worker/tests.rs +++ b/client/authority-discovery/src/worker/tests.rs @@ -100,8 +100,6 @@ pub(crate) struct RuntimeApi { sp_api::mock_impl_runtime_apis! { impl AuthorityDiscoveryApi for RuntimeApi { - type Error = sp_blockchain::Error; - fn authorities(&self) -> Vec { self.authorities.clone() } @@ -189,7 +187,7 @@ async fn build_dht_event( serialized_addresses.as_slice(), ) .await - .map_err(|_| Error::Signing) + .unwrap() .unwrap(); let mut signed_addresses = vec![]; @@ -197,9 +195,7 @@ async fn build_dht_event( addresses: serialized_addresses.clone(), signature, } - .encode(&mut signed_addresses) - .map_err(Error::EncodingProto) - .unwrap(); + .encode(&mut signed_addresses).unwrap(); let key = hash_authority_id(&public_key.to_raw_vec()); let value = signed_addresses; diff --git a/client/basic-authorship/Cargo.toml b/client/basic-authorship/Cargo.toml index c1df76253a46a..2047c85b0c872 100644 --- a/client/basic-authorship/Cargo.toml +++ b/client/basic-authorship/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-basic-authorship" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -13,24 +13,24 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } futures = "0.3.9" futures-timer = "3.0.1" log = "0.4.8" -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.8.0"} -sp-api = { version = "2.0.0", path = "../../primitives/api" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sc-client-api = { version = "2.0.0", path = "../api" } -sp-consensus = { version = "0.8.0", path = "../../primitives/consensus/common" } -sp-inherents = { version = "2.0.0", path = "../../primitives/inherents" } -sc-telemetry = { version = "2.0.0", path = "../telemetry" } -sp-transaction-pool = { version = "2.0.0", path = "../../primitives/transaction-pool" } -sc-block-builder = { version = "0.8.0", path = "../block-builder" } -sc-proposer-metrics = { version = "0.8.0", path = "../proposer-metrics" } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.9.0"} +sp-api = { version = "3.0.0", path = "../../primitives/api" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sc-client-api = { version = "3.0.0", path = "../api" } +sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sp-inherents = { version = "3.0.0", path = "../../primitives/inherents" } +sc-telemetry = { version = "3.0.0", path = "../telemetry" } +sp-transaction-pool = { version = "3.0.0", path = "../../primitives/transaction-pool" } +sc-block-builder = { version = "0.9.0", path = "../block-builder" } +sc-proposer-metrics = { version = "0.9.0", path = "../proposer-metrics" } [dev-dependencies] -sc-transaction-pool = { version = "2.0.0", path = "../transaction-pool" } +sc-transaction-pool = { version = "3.0.0", path = "../transaction-pool" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } parking_lot = "0.11.1" diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index 73e6156615288..067695e5a84da 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -99,7 +99,7 @@ impl ProposerFactory C: BlockBuilderProvider + HeaderBackend + ProvideRuntimeApi + Send + Sync + 'static, C::Api: ApiExt> - + BlockBuilderApi, + + BlockBuilderApi, { pub fn init_with_now( &mut self, @@ -138,7 +138,7 @@ impl sp_consensus::Environment for C: BlockBuilderProvider + HeaderBackend + ProvideRuntimeApi + Send + Sync + 'static, C::Api: ApiExt> - + BlockBuilderApi, + + BlockBuilderApi, { type CreateProposer = future::Ready>; type Proposer = Proposer; @@ -175,7 +175,7 @@ impl sp_consensus::Proposer for C: BlockBuilderProvider + HeaderBackend + ProvideRuntimeApi + Send + Sync + 'static, C::Api: ApiExt> - + BlockBuilderApi, + + BlockBuilderApi, { type Transaction = backend::TransactionFor; type Proposal = Pin Proposer C: BlockBuilderProvider + HeaderBackend + ProvideRuntimeApi + Send + Sync + 'static, C::Api: ApiExt> - + BlockBuilderApi, + + BlockBuilderApi, { async fn propose_with( self, diff --git a/client/block-builder/Cargo.toml b/client/block-builder/Cargo.toml index 64a82505a9fa4..dda5edde36db5 100644 --- a/client/block-builder/Cargo.toml +++ b/client/block-builder/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-block-builder" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -14,17 +14,17 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-api = { version = "2.0.0", path = "../../primitives/api" } -sp-consensus = { version = "0.8.0", path = "../../primitives/consensus/common" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-block-builder = { version = "2.0.0", path = "../../primitives/block-builder" } -sp-inherents = { version = "2.0.0", path = "../../primitives/inherents" } -sc-client-api = { version = "2.0.0", path = "../api" } -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } +sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-api = { version = "3.0.0", path = "../../primitives/api" } +sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-block-builder = { version = "3.0.0", path = "../../primitives/block-builder" } +sp-inherents = { version = "3.0.0", path = "../../primitives/inherents" } +sc-client-api = { version = "3.0.0", path = "../api" } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } [dev-dependencies] substrate-test-runtime-client = { path = "../../test-utils/runtime/client" } -sp-trie = { version = "2.0.0", path = "../../primitives/trie" } +sp-trie = { version = "3.0.0", path = "../../primitives/trie" } diff --git a/client/block-builder/src/lib.rs b/client/block-builder/src/lib.rs index 5a7e0277d9e8c..5f700da8914a3 100644 --- a/client/block-builder/src/lib.rs +++ b/client/block-builder/src/lib.rs @@ -35,8 +35,7 @@ use sp_runtime::{ use sp_blockchain::{ApplyExtrinsicFailed, Error}; use sp_core::ExecutionContext; use sp_api::{ - Core, ApiExt, ApiErrorFor, ApiRef, ProvideRuntimeApi, StorageChanges, StorageProof, - TransactionOutcome, + Core, ApiExt, ApiRef, ProvideRuntimeApi, StorageChanges, StorageProof, TransactionOutcome, }; use sp_consensus::RecordProof; @@ -106,8 +105,7 @@ impl<'a, Block, A, B> BlockBuilder<'a, Block, A, B> where Block: BlockT, A: ProvideRuntimeApi + 'a, - A::Api: BlockBuilderApi + - ApiExt>, + A::Api: BlockBuilderApi + ApiExt>, B: backend::Backend, { /// Create a new instance of builder based on the given `parent_hash` and `parent_number`. @@ -122,7 +120,7 @@ where record_proof: RecordProof, inherent_digests: DigestFor, backend: &'a B, - ) -> Result> { + ) -> Result { let header = <::Header as HeaderT>::new( parent_number + One::one(), Default::default(), @@ -155,7 +153,7 @@ where /// Push onto the block's list of extrinsics. /// /// This will ensure the extrinsic can be validly executed (by executing it). - pub fn push(&mut self, xt: ::Extrinsic) -> Result<(), ApiErrorFor> { + pub fn push(&mut self, xt: ::Extrinsic) -> Result<(), Error> { let block_id = &self.block_id; let extrinsics = &mut self.extrinsics; @@ -174,7 +172,7 @@ where Err(ApplyExtrinsicFailed::Validity(tx_validity).into()), ) }, - Err(e) => TransactionOutcome::Rollback(Err(e)), + Err(e) => TransactionOutcome::Rollback(Err(Error::from(e))), } }) } @@ -184,10 +182,7 @@ where /// Returns the build `Block`, the changes to the storage and an optional `StorageProof` /// supplied by `self.api`, combined as [`BuiltBlock`]. /// The storage proof will be `Some(_)` when proof recording was enabled. - pub fn build(mut self) -> Result< - BuiltBlock>, - ApiErrorFor - > { + pub fn build(mut self) -> Result>, Error> { let header = self.api.finalize_block_with_context( &self.block_id, ExecutionContext::BlockConstruction )?; @@ -227,7 +222,7 @@ where pub fn create_inherents( &mut self, inherent_data: sp_inherents::InherentData, - ) -> Result, ApiErrorFor> { + ) -> Result, Error> { let block_id = self.block_id; self.api.execute_in_transaction(move |api| { // `create_inherents` should not change any state, to ensure this we always rollback @@ -237,7 +232,7 @@ where ExecutionContext::BlockConstruction, inherent_data )) - }) + }).map_err(|e| Error::Application(Box::new(e))) } } diff --git a/client/chain-spec/Cargo.toml b/client/chain-spec/Cargo.toml index e7144d330c693..27850cc8400b3 100644 --- a/client/chain-spec/Cargo.toml +++ b/client/chain-spec/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-chain-spec" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -13,17 +13,17 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sc-chain-spec-derive = { version = "2.0.0", path = "./derive" } -impl-trait-for-tuples = "0.2.0" -sc-network = { version = "0.8.0", path = "../network" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } +sc-chain-spec-derive = { version = "3.0.0", path = "./derive" } +impl-trait-for-tuples = "0.2.1" +sc-network = { version = "0.9.0", path = "../network" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } serde = { version = "1.0.101", features = ["derive"] } serde_json = "1.0.41" -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-chain-spec = { version = "2.0.0", path = "../../primitives/chain-spec" } -sc-telemetry = { version = "2.0.0", path = "../telemetry" } -codec = { package = "parity-scale-codec", version = "1.3.6" } -sc-consensus-babe = { version = "0.8.0-rc6", path = "../consensus/babe" } -sp-consensus-babe = { version = "0.8.0-rc6", path = "../../primitives/consensus/babe" } -sc-consensus-epochs = { version = "0.8.0-rc6", path = "../consensus/epochs" } -sc-finality-grandpa = { version = "0.8.0-rc6", path = "../finality-grandpa" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-chain-spec = { version = "3.0.0", path = "../../primitives/chain-spec" } +sc-telemetry = { version = "3.0.0", path = "../telemetry" } +codec = { package = "parity-scale-codec", version = "2.0.0" } +sc-consensus-babe = { version = "0.9.0", path = "../consensus/babe" } +sp-consensus-babe = { version = "0.9.0", path = "../../primitives/consensus/babe" } +sc-consensus-epochs = { version = "0.9.0", path = "../consensus/epochs" } +sc-finality-grandpa = { version = "0.9.0", path = "../finality-grandpa" } diff --git a/client/chain-spec/derive/Cargo.toml b/client/chain-spec/derive/Cargo.toml index 09196c125b7dd..4f3484df31cba 100644 --- a/client/chain-spec/derive/Cargo.toml +++ b/client/chain-spec/derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-chain-spec-derive" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" diff --git a/client/cli/Cargo.toml b/client/cli/Cargo.toml index 17390a5f225c4..4617c2d790ada 100644 --- a/client/cli/Cargo.toml +++ b/client/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-cli" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Substrate CLI interface." edition = "2018" @@ -18,28 +18,28 @@ regex = "1.4.2" tokio = { version = "0.2.21", features = [ "signal", "rt-core", "rt-threaded", "blocking" ] } futures = "0.3.9" fdlimit = "0.2.1" -libp2p = "0.34.0" -parity-scale-codec = "1.3.6" +libp2p = "0.35.1" +parity-scale-codec = "2.0.0" hex = "0.4.2" rand = "0.7.3" tiny-bip39 = "0.8.0" serde_json = "1.0.41" -sc-keystore = { version = "2.0.0", path = "../keystore" } -sp-panic-handler = { version = "2.0.0", path = "../../primitives/panic-handler" } -sc-client-api = { version = "2.0.0", path = "../api" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sc-network = { version = "0.8.0", path = "../network" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-utils = { version = "2.0.0", path = "../../primitives/utils" } -sp-version = { version = "2.0.0", path = "../../primitives/version" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-keystore = { version = "0.8.0", path = "../../primitives/keystore" } -sc-service = { version = "0.8.0", default-features = false, path = "../service" } -sc-telemetry = { version = "2.0.0", path = "../telemetry" } -sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" } +sc-keystore = { version = "3.0.0", path = "../keystore" } +sp-panic-handler = { version = "3.0.0", path = "../../primitives/panic-handler" } +sc-client-api = { version = "3.0.0", path = "../api" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sc-network = { version = "0.9.0", path = "../network" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-utils = { version = "3.0.0", path = "../../primitives/utils" } +sp-version = { version = "3.0.0", path = "../../primitives/version" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-keystore = { version = "0.9.0", path = "../../primitives/keystore" } +sc-service = { version = "0.9.0", default-features = false, path = "../service" } +sc-telemetry = { version = "3.0.0", path = "../telemetry" } +sp-keyring = { version = "3.0.0", path = "../../primitives/keyring" } names = "0.11.0" structopt = "0.3.8" -sc-tracing = { version = "2.0.0", path = "../tracing" } +sc-tracing = { version = "3.0.0", path = "../tracing" } chrono = "0.4.10" serde = "1.0.111" thiserror = "1.0.21" diff --git a/client/cli/src/arg_enums.rs b/client/cli/src/arg_enums.rs index 2ebfa38925e23..4b1f197cf3ea5 100644 --- a/client/cli/src/arg_enums.rs +++ b/client/cli/src/arg_enums.rs @@ -165,18 +165,35 @@ impl Into for RpcMethods { } } -arg_enum! { - /// Database backend - #[allow(missing_docs)] - #[derive(Debug, Clone, Copy)] - pub enum Database { - // Facebooks RocksDB - RocksDb, - // ParityDb. https://github.com/paritytech/parity-db/ - ParityDb, +/// Database backend +#[derive(Debug, Clone, Copy)] +pub enum Database { + /// Facebooks RocksDB + RocksDb, + /// ParityDb. + ParityDb, +} + +impl std::str::FromStr for Database { + type Err = String; + + fn from_str(s: &str) -> Result { + if s.eq_ignore_ascii_case("rocksdb") { + Ok(Self::RocksDb) + } else if s.eq_ignore_ascii_case("paritydb-experimental") { + Ok(Self::ParityDb) + } else { + Err(format!("Unknwon variant `{}`, known variants: {:?}", s, Self::variants())) + } } } +impl Database { + /// Returns all the variants of this enum to be shown in the cli. + pub fn variants() -> &'static [&'static str] { + &["rocksdb", "paritydb-experimental"] + } +} arg_enum! { /// Whether off-chain workers are enabled. diff --git a/client/cli/src/commands/run_cmd.rs b/client/cli/src/commands/run_cmd.rs index bbb8d6f68d7f9..bb6f77819d7af 100644 --- a/client/cli/src/commands/run_cmd.rs +++ b/client/cli/src/commands/run_cmd.rs @@ -27,7 +27,7 @@ use crate::params::TransactionPoolParams; use crate::CliConfiguration; use regex::Regex; use sc_service::{ - config::{BasePath, MultiaddrWithPeerId, PrometheusConfig, TransactionPoolOptions}, + config::{BasePath, PrometheusConfig, TransactionPoolOptions}, ChainSpec, Role, }; use sc_telemetry::TelemetryEndpoints; @@ -43,33 +43,16 @@ pub struct RunCmd { /// participate in any consensus task that it can (e.g. depending on /// availability of local keys). #[structopt( - long = "validator", - conflicts_with_all = &[ "sentry" ] + long = "validator" )] pub validator: bool, - /// Enable sentry mode. - /// - /// The node will be started with the authority role and participate in - /// consensus tasks as an "observer", it will never actively participate - /// regardless of whether it could (e.g. keys are available locally). This - /// mode is useful as a secure proxy for validators (which would run - /// detached from the network), since we want this node to participate in - /// the full consensus protocols in order to have all needed consensus data - /// available to relay to private nodes. - #[structopt( - long = "sentry", - conflicts_with_all = &[ "validator", "light" ], - parse(try_from_str) - )] - pub sentry: Vec, - /// Disable GRANDPA voter when running in validator mode, otherwise disable the GRANDPA observer. #[structopt(long)] pub no_grandpa: bool, /// Experimental: Run in light client mode. - #[structopt(long = "light", conflicts_with = "sentry")] + #[structopt(long = "light")] pub light: bool, /// Listen to all RPC interfaces. @@ -245,17 +228,6 @@ pub struct RunCmd { #[structopt(long)] pub max_runtime_instances: Option, - /// Specify a list of sentry node public addresses. - /// - /// Can't be used with --public-addr as the sentry node would take precedence over the public address - /// specified there. - #[structopt( - long = "sentry-nodes", - value_name = "ADDR", - conflicts_with_all = &[ "sentry", "public-addr" ] - )] - pub sentry_nodes: Vec, - /// Run a temporary node. /// /// A temporary directory will be created to store the configuration and will be deleted @@ -366,13 +338,7 @@ impl CliConfiguration for RunCmd { Ok(if is_light { sc_service::Role::Light } else if is_authority { - sc_service::Role::Authority { - sentry_nodes: self.sentry_nodes.clone(), - } - } else if !self.sentry.is_empty() { - sc_service::Role::Sentry { - validators: self.sentry.clone(), - } + sc_service::Role::Authority } else { sc_service::Role::Full }) diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index ae43e8f334c6d..748e3b1012695 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -34,7 +34,7 @@ use sc_service::config::{ }; use sc_service::{ChainSpec, TracingReceiver, KeepBlocks, TransactionStorageMode}; use sc_telemetry::TelemetryHandle; -use sc_tracing::logging::GlobalLoggerBuilder; +use sc_tracing::logging::LoggerBuilder; use std::net::SocketAddr; use std::path::PathBuf; @@ -486,8 +486,14 @@ pub trait CliConfiguration: Sized { let node_key = self.node_key(&net_config_dir)?; let role = self.role(is_dev)?; let max_runtime_instances = self.max_runtime_instances()?.unwrap_or(8); - let is_validator = role.is_network_authority(); + let is_validator = role.is_authority(); let (keystore_remote, keystore) = self.keystore_config(&config_dir)?; + let telemetry_endpoints = telemetry_handle + .as_ref() + .and_then(|_| self.telemetry_endpoints(&chain_spec).transpose()) + .transpose()? + // Don't initialise telemetry if `telemetry_endpoints` == Some([]) + .filter(|x| !x.is_empty()); let unsafe_pruning = self .import_params() @@ -526,7 +532,7 @@ pub trait CliConfiguration: Sized { rpc_ws_max_connections: self.rpc_ws_max_connections()?, rpc_cors: self.rpc_cors(is_dev)?, prometheus_config: self.prometheus_config(DCV::prometheus_listen_port())?, - telemetry_endpoints: self.telemetry_endpoints(&chain_spec)?, + telemetry_endpoints, telemetry_external_transport: self.telemetry_external_transport()?, default_heap_pages: self.default_heap_pages()?, offchain_worker: self.offchain_worker(&role)?, @@ -576,7 +582,7 @@ pub trait CliConfiguration: Sized { fn init(&self) -> Result { sp_panic_handler::set(&C::support_url(), &C::impl_version()); - let mut logger = GlobalLoggerBuilder::new(self.log_filters()?); + let mut logger = LoggerBuilder::new(self.log_filters()?); logger.with_log_reloading(!self.is_log_filter_reloading_disabled()?); if let Some(transport) = self.telemetry_external_transport()? { diff --git a/client/cli/src/lib.rs b/client/cli/src/lib.rs index a4b0bd45727e2..602c53272ea59 100644 --- a/client/cli/src/lib.rs +++ b/client/cli/src/lib.rs @@ -38,7 +38,7 @@ pub use runner::*; pub use sc_service::{ChainSpec, Role}; use sc_service::{Configuration, TaskExecutor}; use sc_telemetry::TelemetryHandle; -pub use sc_tracing::logging::GlobalLoggerBuilder; +pub use sc_tracing::logging::LoggerBuilder; pub use sp_version::RuntimeVersion; use std::io::Write; pub use structopt; diff --git a/client/cli/src/params/database_params.rs b/client/cli/src/params/database_params.rs index 23d2adc07f9de..3d5aca10d5812 100644 --- a/client/cli/src/params/database_params.rs +++ b/client/cli/src/params/database_params.rs @@ -29,6 +29,7 @@ pub struct DatabaseParams { alias = "db", value_name = "DB", case_insensitive = true, + possible_values = &Database::variants(), )] pub database: Option, @@ -38,7 +39,7 @@ pub struct DatabaseParams { /// Enable storage chain mode /// - /// This changes the storage format for blocks bodys. + /// This changes the storage format for blocks bodies. /// If this is enabled, each transaction is stored separately in the /// transaction database column and is only referenced by hash /// in the block body column. diff --git a/client/cli/src/params/network_params.rs b/client/cli/src/params/network_params.rs index 0b53616b9ed13..f4a6e8d3982ba 100644 --- a/client/cli/src/params/network_params.rs +++ b/client/cli/src/params/network_params.rs @@ -36,10 +36,14 @@ pub struct NetworkParams { #[structopt(long = "reserved-nodes", value_name = "ADDR")] pub reserved_nodes: Vec, - /// Whether to only allow connections to/from reserved nodes. + /// Whether to only synchronize the chain with reserved nodes. /// - /// If you are a validator your node might still connect to other validator - /// nodes regardless of whether they are defined as reserved nodes. + /// Also disables automatic peer discovery. + /// + /// TCP connections might still be established with non-reserved nodes. + /// In particular, if you are a validator your node might still connect to other + /// validator nodes and collator nodes regardless of whether they are defined as + /// reserved nodes. #[structopt(long = "reserved-only")] pub reserved_only: bool, @@ -106,6 +110,10 @@ pub struct NetworkParams { /// security improvements. #[structopt(long)] pub kademlia_disjoint_query_paths: bool, + + /// Join the IPFS network and serve transactions over bitswap protocol. + #[structopt(long)] + pub ipfs_server: bool, } impl NetworkParams { @@ -173,9 +181,11 @@ impl NetworkParams { wasm_external_transport: None, }, max_parallel_downloads: self.max_parallel_downloads, + enable_dht_random_walk: !self.reserved_only, allow_non_globals_in_dht, kademlia_disjoint_query_paths: self.kademlia_disjoint_query_paths, yamux_window_size: None, + ipfs_server: self.ipfs_server, } } } diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index 467ca253531f1..987b8527e6fa5 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -46,10 +46,10 @@ impl PruningParams { // unless `unsafe_pruning` is set. Ok(match &self.pruning { Some(ref s) if s == "archive" => PruningMode::ArchiveAll, - None if role.is_network_authority() => PruningMode::ArchiveAll, + None if role.is_authority() => PruningMode::ArchiveAll, None => PruningMode::default(), Some(s) => { - if role.is_network_authority() && !unsafe_pruning { + if role.is_authority() && !unsafe_pruning { return Err(error::Error::Input( "Validators should run with state pruning disabled (i.e. archive). \ You can ignore this check with `--unsafe-pruning`." diff --git a/client/cli/src/runner.rs b/client/cli/src/runner.rs index 06676655581b1..61a7fe9b01454 100644 --- a/client/cli/src/runner.rs +++ b/client/cli/src/runner.rs @@ -239,7 +239,7 @@ impl Runner { /// Get a new [`TelemetryHandle`]. /// - /// This is used when you want to register a new telemetry for a Substrate node. + /// This is used when you want to register with the [`TelemetryWorker`]. pub fn telemetry_handle(&self) -> TelemetryHandle { self.telemetry_worker.handle() } diff --git a/client/consensus/aura/Cargo.toml b/client/consensus/aura/Cargo.toml index b6e1ba6bc10dd..1465119c81d08 100644 --- a/client/consensus/aura/Cargo.toml +++ b/client/consensus/aura/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-consensus-aura" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Aura consensus algorithm for substrate" edition = "2018" @@ -13,41 +13,42 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-application-crypto = { version = "2.0.0", path = "../../../primitives/application-crypto" } -sp-consensus-aura = { version = "0.8.0", path = "../../../primitives/consensus/aura" } -sp-block-builder = { version = "2.0.0", path = "../../../primitives/block-builder" } -sc-block-builder = { version = "0.8.0", path = "../../block-builder" } -sc-client-api = { version = "2.0.0", path = "../../api" } -codec = { package = "parity-scale-codec", version = "1.3.6" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } +sp-application-crypto = { version = "3.0.0", path = "../../../primitives/application-crypto" } +sp-consensus-aura = { version = "0.9.0", path = "../../../primitives/consensus/aura" } +sp-block-builder = { version = "3.0.0", path = "../../../primitives/block-builder" } +sc-block-builder = { version = "0.9.0", path = "../../block-builder" } +sc-client-api = { version = "3.0.0", path = "../../api" } +codec = { package = "parity-scale-codec", version = "2.0.0" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sp-consensus-slots = { version = "0.9.0", path = "../../../primitives/consensus/slots" } derive_more = "0.99.2" futures = "0.3.9" futures-timer = "3.0.1" -sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" } +sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } log = "0.4.8" parking_lot = "0.11.1" -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-io = { version = "2.0.0", path = "../../../primitives/io" } -sp-version = { version = "2.0.0", path = "../../../primitives/version" } -sc-consensus-slots = { version = "0.8.0", path = "../slots" } -sp-api = { version = "2.0.0", path = "../../../primitives/api" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-timestamp = { version = "2.0.0", path = "../../../primitives/timestamp" } -sp-keystore = { version = "0.8.0", path = "../../../primitives/keystore" } -sc-telemetry = { version = "2.0.0", path = "../../telemetry" } -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.8.0"} +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-io = { version = "3.0.0", path = "../../../primitives/io" } +sp-version = { version = "3.0.0", path = "../../../primitives/version" } +sc-consensus-slots = { version = "0.9.0", path = "../slots" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-timestamp = { version = "3.0.0", path = "../../../primitives/timestamp" } +sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" } +sc-telemetry = { version = "3.0.0", path = "../../telemetry" } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0"} # We enable it only for web-wasm check # See https://docs.rs/getrandom/0.2.1/getrandom/#webassembly-support getrandom = { version = "0.2", features = ["js"], optional = true } [dev-dependencies] -sp-keyring = { version = "2.0.0", path = "../../../primitives/keyring" } -sp-tracing = { version = "2.0.0", path = "../../../primitives/tracing" } -sc-executor = { version = "0.8.0", path = "../../executor" } -sc-keystore = { version = "2.0.0", path = "../../keystore" } -sc-network = { version = "0.8.0", path = "../../network" } +sp-keyring = { version = "3.0.0", path = "../../../primitives/keyring" } +sp-tracing = { version = "3.0.0", path = "../../../primitives/tracing" } +sc-executor = { version = "0.9.0", path = "../../executor" } +sc-keystore = { version = "3.0.0", path = "../../keystore" } +sc-network = { version = "0.9.0", path = "../../network" } sc-network-test = { version = "0.8.0", path = "../../network/test" } -sc-service = { version = "0.8.0", default-features = false, path = "../../service" } +sc-service = { version = "0.9.0", default-features = false, path = "../../service" } substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" } tempfile = "3.1.0" diff --git a/client/consensus/aura/src/digests.rs b/client/consensus/aura/src/digests.rs index fec412b62d1ea..bbf31136b0fcd 100644 --- a/client/consensus/aura/src/digests.rs +++ b/client/consensus/aura/src/digests.rs @@ -24,6 +24,7 @@ use sp_core::Pair; use sp_consensus_aura::AURA_ENGINE_ID; use sp_runtime::generic::{DigestItem, OpaqueDigestItemId}; +use sp_consensus_slots::Slot; use codec::{Encode, Codec}; use std::fmt::Debug; @@ -38,10 +39,10 @@ pub trait CompatibleDigestItem: Sized { fn as_aura_seal(&self) -> Option>; /// Construct a digest item which contains the slot number - fn aura_pre_digest(slot_num: u64) -> Self; + fn aura_pre_digest(slot: Slot) -> Self; /// If this item is an AuRa pre-digest, return the slot number - fn as_aura_pre_digest(&self) -> Option; + fn as_aura_pre_digest(&self) -> Option; } impl CompatibleDigestItem

for DigestItem where @@ -57,11 +58,11 @@ impl CompatibleDigestItem

for DigestItem where self.try_to(OpaqueDigestItemId::Seal(&AURA_ENGINE_ID)) } - fn aura_pre_digest(slot_num: u64) -> Self { - DigestItem::PreRuntime(AURA_ENGINE_ID, slot_num.encode()) + fn aura_pre_digest(slot: Slot) -> Self { + DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode()) } - fn as_aura_pre_digest(&self) -> Option { + fn as_aura_pre_digest(&self) -> Option { self.try_to(OpaqueDigestItemId::PreRuntime(&AURA_ENGINE_ID)) } } diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 84d3783927e54..29c4a40155166 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -43,11 +43,11 @@ use prometheus_endpoint::Registry; use codec::{Encode, Decode, Codec}; use sp_consensus::{ - self, BlockImport, Environment, Proposer, CanAuthorWith, ForkChoiceStrategy, BlockImportParams, - BlockOrigin, Error as ConsensusError, SelectChain, SlotData, BlockCheckParams, ImportResult -}; -use sp_consensus::import_queue::{ - Verifier, BasicQueue, DefaultImportQueue, BoxJustificationImport, + BlockImport, Environment, Proposer, CanAuthorWith, ForkChoiceStrategy, BlockImportParams, + BlockOrigin, Error as ConsensusError, SelectChain, SlotData, BlockCheckParams, ImportResult, + import_queue::{ + Verifier, BasicQueue, DefaultImportQueue, BoxJustificationImport, + }, }; use sc_client_api::{backend::AuxStore, BlockOf}; use sp_blockchain::{ @@ -57,10 +57,7 @@ use sp_blockchain::{ use sp_block_builder::BlockBuilder as BlockBuilderApi; use sp_core::crypto::Public; use sp_application_crypto::{AppKey, AppPublic}; -use sp_runtime::{ - generic::{BlockId, OpaqueDigestItemId}, - traits::NumberFor, Justification, -}; +use sp_runtime::{generic::{BlockId, OpaqueDigestItemId}, traits::NumberFor, Justification}; use sp_runtime::traits::{Block as BlockT, Header, DigestItemFor, Zero, Member}; use sp_api::ProvideRuntimeApi; use sp_core::crypto::Pair; @@ -75,6 +72,7 @@ use sc_consensus_slots::{ CheckedHeader, SlotInfo, SlotCompatible, StorageChanges, check_equivocation, BackoffAuthoringBlocksStrategy, }; +use sp_consensus_slots::Slot; use sp_api::ApiExt; @@ -100,16 +98,16 @@ pub fn slot_duration(client: &C) -> CResult where A: Codec, B: BlockT, C: AuxStore + ProvideRuntimeApi, - C::Api: AuraApi, + C::Api: AuraApi, { - SlotDuration::get_or_compute(client, |a, b| a.slot_duration(b)) + SlotDuration::get_or_compute(client, |a, b| a.slot_duration(b).map_err(Into::into)) } /// Get slot author for given block along with authorities. -fn slot_author(slot_num: u64, authorities: &[AuthorityId

]) -> Option<&AuthorityId

> { +fn slot_author(slot: Slot, authorities: &[AuthorityId

]) -> Option<&AuthorityId

> { if authorities.is_empty() { return None } - let idx = slot_num % (authorities.len() as u64); + let idx = *slot % (authorities.len() as u64); assert!( idx <= usize::max_value() as u64, "It is impossible to have a vector with length beyond the address space; qed", @@ -239,7 +237,7 @@ where fn epoch_data( &self, header: &B::Header, - _slot_number: u64, + _slot: Slot, ) -> Result { authorities(self.client.as_ref(), &BlockId::Hash(header.hash())) } @@ -251,14 +249,14 @@ where fn claim_slot( &self, _header: &B::Header, - slot_number: u64, + slot: Slot, epoch_data: &Self::EpochData, ) -> Option { - let expected_author = slot_author::

(slot_number, epoch_data); + let expected_author = slot_author::

(slot, epoch_data); expected_author.and_then(|p| { if SyncCryptoStore::has_keys( - &*self.keystore, - &[(p.to_raw_vec(), sp_application_crypto::key_types::AURA)], + &*self.keystore, + &[(p.to_raw_vec(), sp_application_crypto::key_types::AURA)], ) { Some(p.clone()) } else { @@ -269,11 +267,11 @@ where fn pre_digest_data( &self, - slot_number: u64, + slot: Slot, _claim: &Self::Claim, ) -> Vec> { vec![ - as CompatibleDigestItem

>::aura_pre_digest(slot_number), + as CompatibleDigestItem

>::aura_pre_digest(slot), ] } @@ -301,6 +299,9 @@ where header_hash.as_ref() ).map_err(|e| sp_consensus::Error::CannotSign( public.clone(), e.to_string(), + ))? + .ok_or_else(|| sp_consensus::Error::CannotSign( + public.clone(), "Could not find key in keystore.".into(), ))?; let signature = signature.clone().try_into() .map_err(|_| sp_consensus::Error::InvalidSignature( @@ -323,14 +324,14 @@ where self.force_authoring } - fn should_backoff(&self, slot_number: u64, chain_head: &B::Header) -> bool { + fn should_backoff(&self, slot: Slot, chain_head: &B::Header) -> bool { if let Some(ref strategy) = self.backoff_authoring_blocks { if let Ok(chain_head_slot) = find_pre_digest::(chain_head) { return strategy.should_backoff( *chain_head.number(), chain_head_slot, self.client.info().finalized_number, - slot_number, + slot, self.logging_target(), ); } @@ -363,9 +364,10 @@ where if let Some(slot_lenience) = sc_consensus_slots::slot_lenience_exponential(parent_slot, slot_info) { - debug!(target: "aura", + debug!( + target: "aura", "No block for {} slots. Applying linear lenience of {}s", - slot_info.number.saturating_sub(parent_slot + 1), + slot_info.slot.saturating_sub(parent_slot + 1), slot_lenience.as_secs(), ); @@ -401,7 +403,7 @@ enum Error { DataProvider(String), Runtime(String), #[display(fmt = "Slot number must increase: parent slot: {}, this slot: {}", _0, _1)] - SlotNumberMustIncrease(u64, u64), + SlotMustIncrease(Slot, Slot), #[display(fmt = "Parent ({}) of {} unavailable. Cannot import", _0, _1)] ParentUnavailable(B::Hash, B::Hash), } @@ -412,16 +414,16 @@ impl std::convert::From> for String { } } -fn find_pre_digest(header: &B::Header) -> Result> +fn find_pre_digest(header: &B::Header) -> Result> where DigestItemFor: CompatibleDigestItem

, P::Signature: Decode, P::Public: Encode + Decode + PartialEq + Clone, { if header.number().is_zero() { - return Ok(0); + return Ok(0.into()); } - let mut pre_digest: Option = None; + let mut pre_digest: Option = None; for log in header.digest().logs() { trace!(target: "aura", "Checking log {:?}", log); match (log.as_aura_pre_digest(), pre_digest.is_some()) { @@ -440,11 +442,11 @@ fn find_pre_digest(header: &B::Header) -> Result( client: &C, - slot_now: u64, + slot_now: Slot, mut header: B::Header, hash: B::Hash, authorities: &[AuthorityId

], -) -> Result)>, Error> where +) -> Result)>, Error> where DigestItemFor: CompatibleDigestItem

, P::Signature: Decode, C: sc_client_api::backend::AuxStore, @@ -459,15 +461,15 @@ fn check_header( aura_err(Error::HeaderBadSeal(hash)) })?; - let slot_num = find_pre_digest::(&header)?; + let slot = find_pre_digest::(&header)?; - if slot_num > slot_now { + if slot > slot_now { header.digest_mut().push(seal); - Ok(CheckedHeader::Deferred(header, slot_num)) + Ok(CheckedHeader::Deferred(header, slot)) } else { // check the signature is valid under the expected authority and // chain state. - let expected_author = match slot_author::

(slot_num, &authorities) { + let expected_author = match slot_author::

(slot, &authorities) { None => return Err(Error::SlotAuthorNotFound), Some(author) => author, }; @@ -478,19 +480,19 @@ fn check_header( if let Some(equivocation_proof) = check_equivocation( client, slot_now, - slot_num, + slot, &header, expected_author, ).map_err(Error::Client)? { info!( "Slot author is equivocating at slot {} with headers {:?} and {:?}", - slot_num, + slot, equivocation_proof.first_header.hash(), equivocation_proof.second_header.hash(), ); } - Ok(CheckedHeader::Checked(header, (slot_num, seal))) + Ok(CheckedHeader::Checked(header, (slot, seal))) } else { Err(Error::BadSignature(hash)) } @@ -516,7 +518,7 @@ impl AuraVerifier where inherent_data: InherentData, timestamp_now: u64, ) -> Result<(), Error> where - C: ProvideRuntimeApi, C::Api: BlockBuilderApi, + C: ProvideRuntimeApi, C::Api: BlockBuilderApi, CAW: CanAuthorWith, { const MAX_TIMESTAMP_DRIFT_SECS: u64 = 60; @@ -535,7 +537,7 @@ impl AuraVerifier where &block_id, block, inherent_data, - ).map_err(Error::Client)?; + ).map_err(|e| Error::Client(e.into()))?; if !inherent_res.ok() { inherent_res @@ -579,7 +581,7 @@ impl Verifier for AuraVerifier where sc_client_api::backend::AuxStore + ProvideCache + BlockOf, - C::Api: BlockBuilderApi + AuraApi> + ApiExt, + C::Api: BlockBuilderApi + AuraApi> + ApiExt, DigestItemFor: CompatibleDigestItem

, P: Pair + Send + Sync + 'static, P::Public: Send + Sync + Hash + Eq + Clone + Decode + Encode + Debug + 'static, @@ -614,18 +616,18 @@ impl Verifier for AuraVerifier where &authorities[..], ).map_err(|e| e.to_string())?; match checked_header { - CheckedHeader::Checked(pre_header, (slot_num, seal)) => { + CheckedHeader::Checked(pre_header, (slot, seal)) => { // if the body is passed through, we need to use the runtime // to check that the internally-set timestamp in the inherents // actually matches the slot set in the seal. if let Some(inner_body) = body.take() { - inherent_data.aura_replace_inherent_data(slot_num); + inherent_data.aura_replace_inherent_data(slot); let block = B::new(pre_header.clone(), inner_body); // skip the inherents verification if the runtime API is old. if self.client .runtime_api() - .has_api_with::, _>( + .has_api_with::, _>( &BlockId::Hash(parent_hash), |v| v >= 2, ) @@ -681,7 +683,7 @@ impl Verifier for AuraVerifier where } fn initialize_authorities_cache(client: &C) -> Result<(), ConsensusError> where - A: Codec, + A: Codec + Debug, B: BlockT, C: ProvideRuntimeApi + BlockOf + ProvideCache, C::Api: AuraApi, @@ -717,7 +719,7 @@ fn initialize_authorities_cache(client: &C) -> Result<(), ConsensusErro #[allow(deprecated)] fn authorities(client: &C, at: &BlockId) -> Result, ConsensusError> where - A: Codec, + A: Codec + Debug, B: BlockT, C: ProvideRuntimeApi + BlockOf + ProvideCache, C::Api: AuraApi, @@ -803,7 +805,7 @@ impl BlockImport for AuraBlockImport>, ) -> Result { let hash = block.post_hash(); - let slot_number = find_pre_digest::(&block.header) + let slot = find_pre_digest::(&block.header) .expect("valid Aura headers must contain a predigest; \ header has been already verified; qed"); @@ -819,10 +821,10 @@ impl BlockImport for AuraBlockImport::SlotNumberMustIncrease(parent_slot, slot_number) + Error::::SlotMustIncrease(parent_slot, slot) ).into()) ); } @@ -843,14 +845,14 @@ pub fn import_queue( can_author_with: CAW, ) -> Result, sp_consensus::Error> where B: BlockT, - C::Api: BlockBuilderApi + AuraApi> + ApiExt, + C::Api: BlockBuilderApi + AuraApi> + ApiExt, C: 'static + ProvideRuntimeApi + BlockOf + ProvideCache + Send + Sync + AuxStore + HeaderBackend, I: BlockImport> + Send + Sync + 'static, DigestItemFor: CompatibleDigestItem

, P: Pair + Send + Sync + 'static, P::Public: Clone + Eq + Send + Sync + Hash + Debug + Encode + Decode, P::Signature: Encode + Decode, - S: sp_core::traits::SpawnNamed, + S: sp_core::traits::SpawnEssentialNamed, CAW: CanAuthorWith + Send + Sync + 'static, { register_aura_inherent_data_provider(&inherent_data_providers, slot_duration.get())?; @@ -884,7 +886,7 @@ mod tests { use sc_client_api::BlockchainEvents; use sp_consensus_aura::sr25519::AuthorityPair; use sc_consensus_slots::{SimpleSlotWorker, BackoffAuthoringOnFinalizedHeadLagging}; - use std::task::Poll; + use std::{task::Poll, time::Instant}; use sc_block_builder::BlockBuilderProvider; use sp_runtime::traits::Header as _; use substrate_test_runtime_client::{TestClient, runtime::{Header, H256}}; @@ -1113,13 +1115,60 @@ mod tests { Default::default(), Default::default() ); - assert!(worker.claim_slot(&head, 0, &authorities).is_none()); - assert!(worker.claim_slot(&head, 1, &authorities).is_none()); - assert!(worker.claim_slot(&head, 2, &authorities).is_none()); - assert!(worker.claim_slot(&head, 3, &authorities).is_some()); - assert!(worker.claim_slot(&head, 4, &authorities).is_none()); - assert!(worker.claim_slot(&head, 5, &authorities).is_none()); - assert!(worker.claim_slot(&head, 6, &authorities).is_none()); - assert!(worker.claim_slot(&head, 7, &authorities).is_some()); + assert!(worker.claim_slot(&head, 0.into(), &authorities).is_none()); + assert!(worker.claim_slot(&head, 1.into(), &authorities).is_none()); + assert!(worker.claim_slot(&head, 2.into(), &authorities).is_none()); + assert!(worker.claim_slot(&head, 3.into(), &authorities).is_some()); + assert!(worker.claim_slot(&head, 4.into(), &authorities).is_none()); + assert!(worker.claim_slot(&head, 5.into(), &authorities).is_none()); + assert!(worker.claim_slot(&head, 6.into(), &authorities).is_none()); + assert!(worker.claim_slot(&head, 7.into(), &authorities).is_some()); + } + + #[test] + fn on_slot_returns_correct_block() { + let net = AuraTestNet::new(4); + + let keystore_path = tempfile::tempdir().expect("Creates keystore path"); + let keystore = LocalKeystore::open(keystore_path.path(), None) + .expect("Creates keystore."); + SyncCryptoStore::sr25519_generate_new( + &keystore, + AuthorityPair::ID, Some(&Keyring::Alice.to_seed()), + ).expect("Key should be created"); + + let net = Arc::new(Mutex::new(net)); + + let mut net = net.lock(); + let peer = net.peer(3); + let client = peer.client().as_full().expect("full clients are created").clone(); + let environ = DummyFactory(client.clone()); + + let mut worker = AuraWorker { + client: client.clone(), + block_import: Arc::new(Mutex::new(client.clone())), + env: environ, + keystore: keystore.into(), + sync_oracle: DummyOracle.clone(), + force_authoring: false, + backoff_authoring_blocks: Option::<()>::None, + _key_type: PhantomData::, + }; + + let head = client.header(&BlockId::Number(0)).unwrap().unwrap(); + + let res = futures::executor::block_on(worker.on_slot( + head, + SlotInfo { + slot: 0.into(), + timestamp: 0, + ends_at: Instant::now() + Duration::from_secs(100), + inherent_data: InherentData::new(), + duration: 1000, + }, + )).unwrap(); + + // The returned block should be imported and we should be able to get its header by now. + assert!(client.header(&BlockId::Hash(res.block.hash())).unwrap().is_some()); } } diff --git a/client/consensus/babe/Cargo.toml b/client/consensus/babe/Cargo.toml index 8104ca2047ca7..14d48fba1bb57 100644 --- a/client/consensus/babe/Cargo.toml +++ b/client/consensus/babe/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-consensus-babe" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "BABE consensus algorithm for substrate" edition = "2018" @@ -14,34 +14,35 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } -sp-consensus-babe = { version = "0.8.0", path = "../../../primitives/consensus/babe" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-application-crypto = { version = "2.0.0", path = "../../../primitives/application-crypto" } -sp-keystore = { version = "0.8.0", path = "../../../primitives/keystore" } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } +sp-consensus-babe = { version = "0.9.0", path = "../../../primitives/consensus/babe" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-application-crypto = { version = "3.0.0", path = "../../../primitives/application-crypto" } +sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" } num-bigint = "0.2.3" num-rational = "0.2.2" num-traits = "0.2.8" serde = { version = "1.0.104", features = ["derive"] } -sp-version = { version = "2.0.0", path = "../../../primitives/version" } -sp-io = { version = "2.0.0", path = "../../../primitives/io" } -sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" } -sp-timestamp = { version = "2.0.0", path = "../../../primitives/timestamp" } -sc-telemetry = { version = "2.0.0", path = "../../telemetry" } -sc-keystore = { version = "2.0.0", path = "../../keystore" } -sc-client-api = { version = "2.0.0", path = "../../api" } -sc-consensus-epochs = { version = "0.8.0", path = "../epochs" } -sp-api = { version = "2.0.0", path = "../../../primitives/api" } -sp-block-builder = { version = "2.0.0", path = "../../../primitives/block-builder" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -sp-consensus-vrf = { version = "0.8.0", path = "../../../primitives/consensus/vrf" } -sc-consensus-uncles = { version = "0.8.0", path = "../uncles" } -sc-consensus-slots = { version = "0.8.0", path = "../slots" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-utils = { version = "2.0.0", path = "../../../primitives/utils" } -fork-tree = { version = "2.0.0", path = "../../../utils/fork-tree" } -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.8.0"} +sp-version = { version = "3.0.0", path = "../../../primitives/version" } +sp-io = { version = "3.0.0", path = "../../../primitives/io" } +sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } +sp-timestamp = { version = "3.0.0", path = "../../../primitives/timestamp" } +sc-telemetry = { version = "3.0.0", path = "../../telemetry" } +sc-keystore = { version = "3.0.0", path = "../../keystore" } +sc-client-api = { version = "3.0.0", path = "../../api" } +sc-consensus-epochs = { version = "0.9.0", path = "../epochs" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } +sp-block-builder = { version = "3.0.0", path = "../../../primitives/block-builder" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sp-consensus-slots = { version = "0.9.0", path = "../../../primitives/consensus/slots" } +sp-consensus-vrf = { version = "0.9.0", path = "../../../primitives/consensus/vrf" } +sc-consensus-uncles = { version = "0.9.0", path = "../uncles" } +sc-consensus-slots = { version = "0.9.0", path = "../slots" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-utils = { version = "3.0.0", path = "../../../primitives/utils" } +fork-tree = { version = "3.0.0", path = "../../../utils/fork-tree" } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0"} futures = "0.3.9" futures-timer = "3.0.1" parking_lot = "0.11.1" @@ -54,14 +55,14 @@ derive_more = "0.99.2" retain_mut = "0.1.2" [dev-dependencies] -sp-keyring = { version = "2.0.0", path = "../../../primitives/keyring" } -sp-tracing = { version = "2.0.0", path = "../../../primitives/tracing" } -sc-executor = { version = "0.8.0", path = "../../executor" } -sc-network = { version = "0.8.0", path = "../../network" } +sp-keyring = { version = "3.0.0", path = "../../../primitives/keyring" } +sp-tracing = { version = "3.0.0", path = "../../../primitives/tracing" } +sc-executor = { version = "0.9.0", path = "../../executor" } +sc-network = { version = "0.9.0", path = "../../network" } sc-network-test = { version = "0.8.0", path = "../../network/test" } -sc-service = { version = "0.8.0", default-features = false, path = "../../service" } +sc-service = { version = "0.9.0", default-features = false, path = "../../service" } substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" } -sc-block-builder = { version = "0.8.0", path = "../../block-builder" } +sc-block-builder = { version = "0.9.0", path = "../../block-builder" } rand_chacha = "0.2.2" tempfile = "3.1.0" diff --git a/client/consensus/babe/rpc/Cargo.toml b/client/consensus/babe/rpc/Cargo.toml index adebccdfa7423..71a1205e3c7aa 100644 --- a/client/consensus/babe/rpc/Cargo.toml +++ b/client/consensus/babe/rpc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-consensus-babe-rpc" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "RPC extensions for the BABE consensus algorithm" edition = "2018" @@ -13,28 +13,28 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sc-consensus-babe = { version = "0.8.0", path = "../" } -sc-rpc-api = { version = "0.8.0", path = "../../../rpc-api" } +sc-consensus-babe = { version = "0.9.0", path = "../" } +sc-rpc-api = { version = "0.9.0", path = "../../../rpc-api" } jsonrpc-core = "15.1.0" jsonrpc-core-client = "15.1.0" jsonrpc-derive = "15.1.0" -sp-consensus-babe = { version = "0.8.0", path = "../../../../primitives/consensus/babe" } +sp-consensus-babe = { version = "0.9.0", path = "../../../../primitives/consensus/babe" } serde = { version = "1.0.104", features=["derive"] } -sp-blockchain = { version = "2.0.0", path = "../../../../primitives/blockchain" } -sp-runtime = { version = "2.0.0", path = "../../../../primitives/runtime" } -sc-consensus-epochs = { version = "0.8.0", path = "../../epochs" } +sp-blockchain = { version = "3.0.0", path = "../../../../primitives/blockchain" } +sp-runtime = { version = "3.0.0", path = "../../../../primitives/runtime" } +sc-consensus-epochs = { version = "0.9.0", path = "../../epochs" } futures = { version = "0.3.4", features = ["compat"] } derive_more = "0.99.2" -sp-api = { version = "2.0.0", path = "../../../../primitives/api" } -sp-consensus = { version = "0.8.0", path = "../../../../primitives/consensus/common" } -sp-core = { version = "2.0.0", path = "../../../../primitives/core" } -sp-application-crypto = { version = "2.0.0", path = "../../../../primitives/application-crypto" } -sp-keystore = { version = "0.8.0", path = "../../../../primitives/keystore" } +sp-api = { version = "3.0.0", path = "../../../../primitives/api" } +sp-consensus = { version = "0.9.0", path = "../../../../primitives/consensus/common" } +sp-core = { version = "3.0.0", path = "../../../../primitives/core" } +sp-application-crypto = { version = "3.0.0", path = "../../../../primitives/application-crypto" } +sp-keystore = { version = "0.9.0", path = "../../../../primitives/keystore" } [dev-dependencies] -sc-consensus = { version = "0.8.0", path = "../../../consensus/common" } +sc-consensus = { version = "0.9.0", path = "../../../consensus/common" } serde_json = "1.0.50" -sp-keyring = { version = "2.0.0", path = "../../../../primitives/keyring" } -sc-keystore = { version = "2.0.0", path = "../../../keystore" } +sp-keyring = { version = "3.0.0", path = "../../../../primitives/keyring" } +sc-keystore = { version = "3.0.0", path = "../../../keystore" } substrate-test-runtime-client = { version = "2.0.0", path = "../../../../test-utils/runtime/client" } tempfile = "3.1.0" diff --git a/client/consensus/babe/rpc/src/lib.rs b/client/consensus/babe/rpc/src/lib.rs index 4d5c091e0cbbd..ca14a764eece5 100644 --- a/client/consensus/babe/rpc/src/lib.rs +++ b/client/consensus/babe/rpc/src/lib.rs @@ -124,7 +124,13 @@ impl BabeApi for BabeRpcHandler .map_err(|err| { Error::StringError(format!("{:?}", err)) })?; - let epoch = epoch_data(&shared_epoch, &client, &babe_config, epoch_start, &select_chain)?; + let epoch = epoch_data( + &shared_epoch, + &client, + &babe_config, + *epoch_start, + &select_chain, + )?; let (epoch_start, epoch_end) = (epoch.start_slot(), epoch.end_slot()); let mut claims: HashMap = HashMap::new(); @@ -142,19 +148,19 @@ impl BabeApi for BabeRpcHandler .collect::>() }; - for slot_number in epoch_start..epoch_end { + for slot in *epoch_start..*epoch_end { if let Some((claim, key)) = - authorship::claim_slot_using_keys(slot_number, &epoch, &keystore, &keys) + authorship::claim_slot_using_keys(slot.into(), &epoch, &keystore, &keys) { match claim { PreDigest::Primary { .. } => { - claims.entry(key).or_default().primary.push(slot_number); + claims.entry(key).or_default().primary.push(slot); } PreDigest::SecondaryPlain { .. } => { - claims.entry(key).or_default().secondary.push(slot_number); + claims.entry(key).or_default().secondary.push(slot); } PreDigest::SecondaryVRF { .. } => { - claims.entry(key).or_default().secondary_vrf.push(slot_number); + claims.entry(key).or_default().secondary_vrf.push(slot.into()); }, }; } @@ -167,7 +173,7 @@ impl BabeApi for BabeRpcHandler } } -/// Holds information about the `slot_number`'s that can be claimed by a given key. +/// Holds information about the `slot`'s that can be claimed by a given key. #[derive(Default, Debug, Deserialize, Serialize)] pub struct EpochAuthorship { /// the array of primary slots that can be claimed @@ -197,12 +203,12 @@ impl From for jsonrpc_core::Error { } } -/// fetches the epoch data for a given slot_number. +/// fetches the epoch data for a given slot. fn epoch_data( epoch_changes: &SharedEpochChanges, client: &Arc, babe_config: &Config, - slot_number: u64, + slot: u64, select_chain: &SC, ) -> Result where @@ -215,7 +221,7 @@ fn epoch_data( descendent_query(&**client), &parent.hash(), parent.number().clone(), - slot_number, + slot.into(), |slot| Epoch::genesis(&babe_config, slot), ) .map_err(|e| Error::Consensus(ConsensusError::ChainLookup(format!("{:?}", e))))? diff --git a/client/consensus/babe/src/authorship.rs b/client/consensus/babe/src/authorship.rs index 90ad12c4558c8..cf75a4a43f23f 100644 --- a/client/consensus/babe/src/authorship.rs +++ b/client/consensus/babe/src/authorship.rs @@ -20,11 +20,8 @@ use sp_application_crypto::AppKey; use sp_consensus_babe::{ - BABE_VRF_PREFIX, - AuthorityId, BabeAuthorityWeight, - SlotNumber, - make_transcript, - make_transcript_data, + BABE_VRF_PREFIX, AuthorityId, BabeAuthorityWeight, make_transcript, make_transcript_data, + Slot, }; use sp_consensus_babe::digests::{ PreDigest, PrimaryPreDigest, SecondaryPlainPreDigest, SecondaryVRFPreDigest, @@ -106,7 +103,7 @@ pub(super) fn check_primary_threshold(inout: &VRFInOut, threshold: u128) -> bool /// authorities. This should always assign the slot to some authority unless the /// authorities list is empty. pub(super) fn secondary_slot_author( - slot_number: u64, + slot: Slot, authorities: &[(AuthorityId, BabeAuthorityWeight)], randomness: [u8; 32], ) -> Option<&AuthorityId> { @@ -114,7 +111,7 @@ pub(super) fn secondary_slot_author( return None; } - let rand = U256::from((randomness, slot_number).using_encoded(blake2_256)); + let rand = U256::from((randomness, slot).using_encoded(blake2_256)); let authorities_len = U256::from(authorities.len()); let idx = rand % authorities_len; @@ -130,7 +127,7 @@ pub(super) fn secondary_slot_author( /// pre-digest to use when authoring the block, or `None` if it is not our turn /// to propose. fn claim_secondary_slot( - slot_number: SlotNumber, + slot: Slot, epoch: &Epoch, keys: &[(AuthorityId, usize)], keystore: &SyncCryptoStorePtr, @@ -143,7 +140,7 @@ fn claim_secondary_slot( } let expected_author = super::authorship::secondary_slot_author( - slot_number, + slot, authorities, *randomness, )?; @@ -153,7 +150,7 @@ fn claim_secondary_slot( let pre_digest = if author_secondary_vrf { let transcript_data = super::authorship::make_transcript_data( randomness, - slot_number, + slot, *epoch_index, ); let result = SyncCryptoStore::sr25519_vrf_sign( @@ -162,9 +159,9 @@ fn claim_secondary_slot( authority_id.as_ref(), transcript_data, ); - if let Ok(signature) = result { + if let Ok(Some(signature)) = result { Some(PreDigest::SecondaryVRF(SecondaryVRFPreDigest { - slot_number, + slot, vrf_output: VRFOutput(signature.output), vrf_proof: VRFProof(signature.proof), authority_index: *authority_index as u32, @@ -174,7 +171,7 @@ fn claim_secondary_slot( } } else if SyncCryptoStore::has_keys(&**keystore, &[(authority_id.to_raw_vec(), AuthorityId::ID)]) { Some(PreDigest::SecondaryPlain(SecondaryPlainPreDigest { - slot_number, + slot, authority_index: *authority_index as u32, })) } else { @@ -195,7 +192,7 @@ fn claim_secondary_slot( /// secondary slots enabled for the given epoch, we will fallback to trying to /// claim a secondary slot. pub fn claim_slot( - slot_number: SlotNumber, + slot: Slot, epoch: &Epoch, keystore: &SyncCryptoStorePtr, ) -> Option<(PreDigest, AuthorityId)> { @@ -203,24 +200,24 @@ pub fn claim_slot( .enumerate() .map(|(index, a)| (a.0.clone(), index)) .collect::>(); - claim_slot_using_keys(slot_number, epoch, keystore, &authorities) + claim_slot_using_keys(slot, epoch, keystore, &authorities) } /// Like `claim_slot`, but allows passing an explicit set of key pairs. Useful if we intend /// to make repeated calls for different slots using the same key pairs. pub fn claim_slot_using_keys( - slot_number: SlotNumber, + slot: Slot, epoch: &Epoch, keystore: &SyncCryptoStorePtr, keys: &[(AuthorityId, usize)], ) -> Option<(PreDigest, AuthorityId)> { - claim_primary_slot(slot_number, epoch, epoch.config.c, keystore, &keys) + claim_primary_slot(slot, epoch, epoch.config.c, keystore, &keys) .or_else(|| { if epoch.config.allowed_slots.is_secondary_plain_slots_allowed() || epoch.config.allowed_slots.is_secondary_vrf_slots_allowed() { claim_secondary_slot( - slot_number, + slot, &epoch, keys, &keystore, @@ -237,7 +234,7 @@ pub fn claim_slot_using_keys( /// the VRF. If the VRF produces a value less than `threshold`, it is our turn, /// so it returns `Some(_)`. Otherwise, it returns `None`. fn claim_primary_slot( - slot_number: SlotNumber, + slot: Slot, epoch: &Epoch, c: (u64, u64), keystore: &SyncCryptoStorePtr, @@ -248,12 +245,12 @@ fn claim_primary_slot( for (authority_id, authority_index) in keys { let transcript = super::authorship::make_transcript( randomness, - slot_number, + slot, *epoch_index ); let transcript_data = super::authorship::make_transcript_data( randomness, - slot_number, + slot, *epoch_index ); // Compute the threshold we will use. @@ -268,7 +265,7 @@ fn claim_primary_slot( authority_id.as_ref(), transcript_data, ); - if let Ok(signature) = result { + if let Ok(Some(signature)) = result { let public = PublicKey::from_bytes(&authority_id.to_raw_vec()).ok()?; let inout = match signature.output.attach_input_hash(&public, transcript) { Ok(inout) => inout, @@ -276,7 +273,7 @@ fn claim_primary_slot( }; if super::authorship::check_primary_threshold(&inout, threshold) { let pre_digest = PreDigest::Primary(PrimaryPreDigest { - slot_number, + slot, vrf_output: VRFOutput(signature.output), vrf_proof: VRFProof(signature.proof), authority_index: *authority_index as u32, @@ -314,7 +311,7 @@ mod tests { let mut epoch = Epoch { epoch_index: 10, - start_slot: 0, + start_slot: 0.into(), duration: 20, authorities: authorities.clone(), randomness: Default::default(), @@ -324,9 +321,9 @@ mod tests { }, }; - assert!(claim_slot(10, &epoch, &keystore).is_none()); + assert!(claim_slot(10.into(), &epoch, &keystore).is_none()); epoch.authorities.push((valid_public_key.clone().into(), 10)); - assert_eq!(claim_slot(10, &epoch, &keystore).unwrap().1, valid_public_key.into()); + assert_eq!(claim_slot(10.into(), &epoch, &keystore).unwrap().1, valid_public_key.into()); } } diff --git a/client/consensus/babe/src/aux_schema.rs b/client/consensus/babe/src/aux_schema.rs index d399a12ea8a5b..7d5df77c92176 100644 --- a/client/consensus/babe/src/aux_schema.rs +++ b/client/consensus/babe/src/aux_schema.rs @@ -44,7 +44,7 @@ fn load_decode(backend: &B, key: &[u8]) -> ClientResult> T: Decode, { let corrupt = |e: codec::Error| { - ClientError::Backend(format!("BABE DB is corrupted. Decode error: {}", e.what())) + ClientError::Backend(format!("BABE DB is corrupted. Decode error: {}", e)) }; match backend.get_aux(key)? { None => Ok(None), @@ -151,7 +151,7 @@ mod test { #[test] fn load_decode_from_v0_epoch_changes() { let epoch = EpochV0 { - start_slot: 0, + start_slot: 0.into(), authorities: vec![], randomness: [0; 32], epoch_index: 1, @@ -195,8 +195,8 @@ mod test { .map(|(_, _, epoch)| epoch.clone()) .collect::>() == vec![PersistedEpochHeader::Regular(EpochHeader { - start_slot: 0, - end_slot: 100, + start_slot: 0.into(), + end_slot: 100.into(), })], ); // PersistedEpochHeader does not implement Debug, so we use assert! directly. diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index ea3ca29dad0e0..a8e533d2a83d4 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -66,10 +66,8 @@ #![forbid(unsafe_code)] #![warn(missing_docs)] pub use sp_consensus_babe::{ - BabeApi, ConsensusLog, BABE_ENGINE_ID, SlotNumber, - BabeEpochConfiguration, BabeGenesisConfiguration, - AuthorityId, AuthorityPair, AuthoritySignature, - BabeAuthorityWeight, VRF_OUTPUT_LENGTH, + BabeApi, ConsensusLog, BABE_ENGINE_ID, BabeEpochConfiguration, BabeGenesisConfiguration, + AuthorityId, AuthorityPair, AuthoritySignature, BabeAuthorityWeight, VRF_OUTPUT_LENGTH, digests::{ CompatibleDigestItem, NextEpochDescriptor, NextConfigDescriptor, PreDigest, PrimaryPreDigest, SecondaryPlainPreDigest, @@ -80,8 +78,7 @@ use std::{ collections::HashMap, sync::Arc, u64, pin::Pin, time::{Instant, Duration}, any::Any, borrow::Cow, convert::TryInto, }; -use sp_consensus::{ImportResult, CanAuthorWith}; -use sp_consensus::import_queue::BoxJustificationImport; +use sp_consensus::{ImportResult, CanAuthorWith, import_queue::BoxJustificationImport}; use sp_core::crypto::Public; use sp_application_crypto::AppKey; use sp_keystore::{SyncCryptoStorePtr, SyncCryptoStore}; @@ -94,16 +91,14 @@ use parking_lot::Mutex; use sp_inherents::{InherentDataProviders, InherentData}; use sc_telemetry::{telemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG}; use sp_consensus::{ - self, BlockImport, Environment, Proposer, BlockCheckParams, + BlockImport, Environment, Proposer, BlockCheckParams, ForkChoiceStrategy, BlockImportParams, BlockOrigin, Error as ConsensusError, - SelectChain, SlotData, + SelectChain, SlotData, import_queue::{Verifier, BasicQueue, DefaultImportQueue, CacheKeyId}, }; use sp_consensus_babe::inherents::BabeInherentData; use sp_timestamp::{TimestampInherentData, InherentType as TimestampInherent}; -use sp_consensus::import_queue::{Verifier, BasicQueue, DefaultImportQueue, CacheKeyId}; use sc_client_api::{ - backend::AuxStore, - BlockchainEvents, ProvideUncles, + backend::AuxStore, BlockchainEvents, ProvideUncles, }; use sp_block_builder::BlockBuilder as BlockBuilderApi; use futures::channel::mpsc::{channel, Sender, Receiver}; @@ -114,7 +109,7 @@ use log::{debug, info, log, trace, warn}; use prometheus_endpoint::Registry; use sc_consensus_slots::{ SlotInfo, SlotCompatible, StorageChanges, CheckedHeader, check_equivocation, - BackoffAuthoringBlocksStrategy, + BackoffAuthoringBlocksStrategy }; use sc_consensus_epochs::{ descendent_query, SharedEpochChanges, EpochChangesFor, Epoch as EpochT, ViableEpochDescriptor, @@ -126,6 +121,7 @@ use sp_blockchain::{ use schnorrkel::SignatureError; use codec::{Encode, Decode}; use sp_api::ApiExt; +use sp_consensus_slots::Slot; mod verification; mod migration; @@ -141,9 +137,9 @@ pub struct Epoch { /// The epoch index. pub epoch_index: u64, /// The starting slot of the epoch. - pub start_slot: SlotNumber, + pub start_slot: Slot, /// The duration of this epoch. - pub duration: SlotNumber, + pub duration: u64, /// The authorities and their weights. pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>, /// Randomness for this epoch. @@ -154,7 +150,7 @@ pub struct Epoch { impl EpochT for Epoch { type NextEpochDescriptor = (NextEpochDescriptor, BabeEpochConfiguration); - type SlotNumber = SlotNumber; + type Slot = Slot; fn increment( &self, @@ -170,11 +166,11 @@ impl EpochT for Epoch { } } - fn start_slot(&self) -> SlotNumber { + fn start_slot(&self) -> Slot { self.start_slot } - fn end_slot(&self) -> SlotNumber { + fn end_slot(&self) -> Slot { self.start_slot + self.duration } } @@ -184,11 +180,11 @@ impl Epoch { /// the first block, so that has to be provided. pub fn genesis( genesis_config: &BabeGenesisConfiguration, - slot_number: SlotNumber + slot: Slot, ) -> Epoch { Epoch { epoch_index: 0, - start_slot: slot_number, + start_slot: slot, duration: genesis_config.epoch_length, authorities: genesis_config.genesis_authorities.clone(), randomness: genesis_config.randomness, @@ -229,7 +225,7 @@ pub enum Error { ParentUnavailable(B::Hash, B::Hash), /// Slot number must increase #[display(fmt = "Slot number must increase: parent slot: {}, this slot: {}", _0, _1)] - SlotNumberMustIncrease(u64, u64), + SlotMustIncrease(Slot, Slot), /// Header has a bad seal #[display(fmt = "Header {:?} has a bad seal", _0)] HeaderBadSeal(B::Hash), @@ -262,7 +258,7 @@ pub enum Error { FetchParentHeader(sp_blockchain::Error), /// Expected epoch change to happen. #[display(fmt = "Expected epoch change to happen at {:?}, s{}", _0, _1)] - ExpectedEpochChange(B::Hash, u64), + ExpectedEpochChange(B::Hash, Slot), /// Unexpected config change. #[display(fmt = "Unexpected config change")] UnexpectedConfigChange, @@ -277,6 +273,8 @@ pub enum Error { CheckInherents(String), /// Client error Client(sp_blockchain::Error), + /// Runtime Api error. + RuntimeApi(sp_api::ApiError), /// Runtime error Runtime(sp_inherents::Error), /// Fork tree error @@ -314,14 +312,14 @@ impl Config { /// Either fetch the slot duration from disk or compute it from the genesis /// state. pub fn get_or_compute(client: &C) -> ClientResult where - C: AuxStore + ProvideRuntimeApi, C::Api: BabeApi, + C: AuxStore + ProvideRuntimeApi, C::Api: BabeApi, { trace!(target: "babe", "Getting slot duration"); match sc_consensus_slots::SlotDuration::get_or_compute(client, |a, b| { - let has_api_v1 = a.has_api_with::, _>( + let has_api_v1 = a.has_api_with::, _>( &b, |v| v == 1, )?; - let has_api_v2 = a.has_api_with::, _>( + let has_api_v2 = a.has_api_with::, _>( &b, |v| v == 2, )?; @@ -330,7 +328,7 @@ impl Config { Ok(a.configuration_before_version_2(b)?.into()) } } else if has_api_v2 { - a.configuration(b) + a.configuration(b).map_err(Into::into) } else { Err(sp_blockchain::Error::VersionInvalid( "Unsupported or invalid BabeApi version".to_string() @@ -471,7 +469,7 @@ pub fn start_babe(BabeParams { #[must_use] pub struct BabeWorker { inner: Pin + Send + 'static>>, - slot_notification_sinks: Arc, Epoch>)>>>>, + slot_notification_sinks: SlotNotificationSinks, } impl BabeWorker { @@ -479,7 +477,7 @@ impl BabeWorker { /// epoch descriptor. pub fn slot_notification_stream( &self - ) -> Receiver<(u64, ViableEpochDescriptor, Epoch>)> { + ) -> Receiver<(Slot, ViableEpochDescriptor, Epoch>)> { const CHANNEL_BUFFER_SIZE: usize = 1024; let (sink, stream) = channel(CHANNEL_BUFFER_SIZE); @@ -500,7 +498,9 @@ impl futures::Future for BabeWorker { } /// Slot notification sinks. -type SlotNotificationSinks = Arc::Hash, NumberFor, Epoch>)>>>>; +type SlotNotificationSinks = Arc< + Mutex::Hash, NumberFor, Epoch>)>>> +>; struct BabeSlotWorker { client: Arc, @@ -551,13 +551,13 @@ where fn epoch_data( &self, parent: &B::Header, - slot_number: u64, + slot: Slot, ) -> Result { self.epoch_changes.lock().epoch_descriptor_for_child_of( descendent_query(&*self.client), &parent.hash(), parent.number().clone(), - slot_number, + slot, ) .map_err(|e| ConsensusError::ChainLookup(format!("{:?}", e)))? .ok_or(sp_consensus::Error::InvalidAuthoritiesSet) @@ -572,12 +572,12 @@ where fn claim_slot( &self, _parent_header: &B::Header, - slot_number: SlotNumber, + slot: Slot, epoch_descriptor: &ViableEpochDescriptor, Epoch>, ) -> Option { - debug!(target: "babe", "Attempting to claim slot {}", slot_number); + debug!(target: "babe", "Attempting to claim slot {}", slot); let s = authorship::claim_slot( - slot_number, + slot, self.epoch_changes.lock().viable_epoch( &epoch_descriptor, |slot| Epoch::genesis(&self.config, slot) @@ -586,7 +586,7 @@ where ); if s.is_some() { - debug!(target: "babe", "Claimed slot {}", slot_number); + debug!(target: "babe", "Claimed slot {}", slot); } s @@ -595,12 +595,12 @@ where fn notify_slot( &self, _parent_header: &B::Header, - slot_number: SlotNumber, + slot: Slot, epoch_descriptor: &ViableEpochDescriptor, Epoch>, ) { self.slot_notification_sinks.lock() .retain_mut(|sink| { - match sink.try_send((slot_number, epoch_descriptor.clone())) { + match sink.try_send((slot, epoch_descriptor.clone())) { Ok(()) => true, Err(e) => { if e.is_full() { @@ -616,7 +616,7 @@ where fn pre_digest_data( &self, - _slot_number: u64, + _slot: Slot, claim: &Self::Claim, ) -> Vec> { vec![ @@ -649,6 +649,9 @@ where ) .map_err(|e| sp_consensus::Error::CannotSign( public.clone(), e.to_string(), + ))? + .ok_or_else(|| sp_consensus::Error::CannotSign( + public.clone(), "Could not find key in keystore.".into(), ))?; let signature: AuthoritySignature = signature.clone().try_into() .map_err(|_| sp_consensus::Error::InvalidSignature( @@ -673,16 +676,16 @@ where self.force_authoring } - fn should_backoff(&self, slot_number: u64, chain_head: &B::Header) -> bool { + fn should_backoff(&self, slot: Slot, chain_head: &B::Header) -> bool { if let Some(ref strategy) = self.backoff_authoring_blocks { if let Ok(chain_head_slot) = find_pre_digest::(chain_head) - .map(|digest| digest.slot_number()) + .map(|digest| digest.slot()) { return strategy.should_backoff( *chain_head.number(), chain_head_slot, self.client.info().finalized_number, - slot_number, + slot, self.logging_target(), ); } @@ -714,7 +717,7 @@ where let parent_slot = match find_pre_digest::(parent_head) { Err(_) => return Some(slot_remaining), - Ok(d) => d.slot_number(), + Ok(d) => d.slot(), }; if let Some(slot_lenience) = @@ -723,7 +726,7 @@ where debug!( target: "babe", "No block for {} slots. Applying exponential lenience of {}s", - slot_info.number.saturating_sub(parent_slot + 1), + slot_info.slot.saturating_sub(parent_slot + 1), slot_lenience.as_secs(), ); @@ -741,7 +744,7 @@ pub fn find_pre_digest(header: &B::Header) -> Result Result<(TimestampInherent, u64, std::time::Duration), sp_consensus::Error> { + ) -> Result<(TimestampInherent, Slot, std::time::Duration), sp_consensus::Error> { trace!(target: "babe", "extract timestamp"); data.timestamp_inherent_data() .and_then(|t| data.babe_inherent_data().map(|a| (t, a))) @@ -848,8 +851,7 @@ impl BabeVerifier + HeaderMetadata + ProvideRuntimeApi, - Client::Api: BlockBuilderApi - + BabeApi, + Client::Api: BlockBuilderApi + BabeApi, SelectChain: sp_consensus::SelectChain, CAW: CanAuthorWith, { @@ -873,7 +875,7 @@ where &block_id, block, inherent_data, - ).map_err(Error::Client)?; + ).map_err(Error::RuntimeApi)?; if !inherent_res.ok() { inherent_res @@ -888,8 +890,8 @@ where fn check_and_report_equivocation( &self, - slot_now: SlotNumber, - slot: SlotNumber, + slot_now: Slot, + slot: Slot, header: &Block::Header, author: &AuthorityId, origin: &BlockOrigin, @@ -936,7 +938,7 @@ where self.client .runtime_api() .generate_key_ownership_proof(block_id, slot, equivocation_proof.offender.clone()) - .map_err(Error::Client) + .map_err(Error::RuntimeApi) }; let parent_id = BlockId::Hash(*header.parent_hash()); @@ -959,7 +961,7 @@ where equivocation_proof, key_owner_proof, ) - .map_err(Error::Client)?; + .map_err(Error::RuntimeApi)?; info!(target: "babe", "Submitted equivocation report for author {:?}", author); @@ -973,7 +975,7 @@ where Block: BlockT, Client: HeaderMetadata + HeaderBackend + ProvideRuntimeApi + Send + Sync + AuxStore + ProvideCache, - Client::Api: BlockBuilderApi + BabeApi, + Client::Api: BlockBuilderApi + BabeApi, SelectChain: sp_consensus::SelectChain, CAW: CanAuthorWith + Send + Sync, { @@ -1014,7 +1016,7 @@ where descendent_query(&*self.client), &parent_hash, parent_header_metadata.number, - pre_digest.slot_number(), + pre_digest.slot(), ) .map_err(|e| Error::::ForkTree(Box::new(e)))? .ok_or_else(|| Error::::FetchEpoch(parent_hash))?; @@ -1036,14 +1038,14 @@ where CheckedHeader::Checked(pre_header, verified_info) => { let babe_pre_digest = verified_info.pre_digest.as_babe_pre_digest() .expect("check_header always returns a pre-digest digest item; qed"); - let slot_number = babe_pre_digest.slot_number(); + let slot = babe_pre_digest.slot(); // the header is valid but let's check if there was something else already // proposed at the same slot by the given author. if there was, we will // report the equivocation to the runtime. if let Err(err) = self.check_and_report_equivocation( slot_now, - slot_number, + slot, &header, &verified_info.author, &origin, @@ -1055,7 +1057,7 @@ where // to check that the internally-set timestamp in the inherents // actually matches the slot set in the seal. if let Some(inner_body) = body.take() { - inherent_data.babe_replace_inherent_data(slot_number); + inherent_data.babe_replace_inherent_data(slot); let block = Block::new(pre_header.clone(), inner_body); self.check_inherents( @@ -1185,7 +1187,7 @@ impl BlockImport for BabeBlockImport(&block.header) .expect("valid babe headers must contain a predigest; \ header has been already verified; qed"); - let slot_number = pre_digest.slot_number(); + let slot = pre_digest.slot(); let parent_hash = *block.header.parent_hash(); let parent_header = self.client.header(BlockId::Hash(parent_hash)) @@ -1195,15 +1197,15 @@ impl BlockImport for BabeBlockImport(&parent_header) - .map(|d| d.slot_number()) + .map(|d| d.slot()) .expect("parent is non-genesis; valid BABE headers contain a pre-digest; \ header has already been verified; qed"); // make sure that slot number is strictly increasing - if slot_number <= parent_slot { + if slot <= parent_slot { return Err( ConsensusError::ClientImport(babe_err( - Error::::SlotNumberMustIncrease(parent_slot, slot_number) + Error::::SlotMustIncrease(parent_slot, slot) ).into()) ); } @@ -1256,7 +1258,7 @@ impl BlockImport for BabeBlockImport { return Err( ConsensusError::ClientImport( - babe_err(Error::::ExpectedEpochChange(hash, slot_number)).into(), + babe_err(Error::::ExpectedEpochChange(hash, slot)).into(), ) ) }, @@ -1301,7 +1303,7 @@ impl BlockImport for BabeBlockImport= start slot {}).", viable_epoch.as_ref().epoch_index, hash, - slot_number, + slot, viable_epoch.as_ref().start_slot, ); @@ -1426,7 +1428,7 @@ fn prune_finalized( find_pre_digest::(&finalized_header) .expect("finalized header must be valid; \ valid blocks have a pre-digest; qed") - .slot_number() + .slot() }; epoch_changes.prune_finalized( @@ -1492,7 +1494,7 @@ pub fn import_queue( client: Arc, select_chain: SelectChain, inherent_data_providers: InherentDataProviders, - spawner: &impl sp_core::traits::SpawnNamed, + spawner: &impl sp_core::traits::SpawnEssentialNamed, registry: Option<&Registry>, can_author_with: CAW, ) -> ClientResult> where @@ -1500,7 +1502,7 @@ pub fn import_queue( + Send + Sync + 'static, Client: ProvideRuntimeApi + ProvideCache + Send + Sync + AuxStore + 'static, Client: HeaderBackend + HeaderMetadata, - Client::Api: BlockBuilderApi + BabeApi + ApiExt, + Client::Api: BlockBuilderApi + BabeApi + ApiExt, SelectChain: sp_consensus::SelectChain + 'static, CAW: CanAuthorWith + Send + Sync + 'static, { @@ -1533,7 +1535,7 @@ pub mod test_helpers { /// Try to claim the given slot and return a `BabePreDigest` if /// successful. pub fn claim_slot( - slot_number: u64, + slot: Slot, parent: &B::Header, client: &C, keystore: SyncCryptoStorePtr, @@ -1551,12 +1553,12 @@ pub mod test_helpers { descendent_query(client), &parent.hash(), parent.number().clone(), - slot_number, + slot, |slot| Epoch::genesis(&link.config, slot), ).unwrap().unwrap(); authorship::claim_slot( - slot_number, + slot, &epoch, &keystore, ).map(|(digest, _)| digest) diff --git a/client/consensus/babe/src/migration.rs b/client/consensus/babe/src/migration.rs index 2a5a8749cc3c1..fec73667da48d 100644 --- a/client/consensus/babe/src/migration.rs +++ b/client/consensus/babe/src/migration.rs @@ -1,9 +1,28 @@ +// This file is part of Substrate. + +// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + use codec::{Encode, Decode}; use sc_consensus_epochs::Epoch as EpochT; use crate::{ - Epoch, SlotNumber, AuthorityId, BabeAuthorityWeight, BabeGenesisConfiguration, + Epoch, AuthorityId, BabeAuthorityWeight, BabeGenesisConfiguration, BabeEpochConfiguration, VRF_OUTPUT_LENGTH, NextEpochDescriptor, }; +use sp_consensus_slots::Slot; /// BABE epoch information, version 0. #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] @@ -11,9 +30,9 @@ pub struct EpochV0 { /// The epoch index. pub epoch_index: u64, /// The starting slot of the epoch. - pub start_slot: SlotNumber, + pub start_slot: Slot, /// The duration of this epoch. - pub duration: SlotNumber, + pub duration: u64, /// The authorities and their weights. pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>, /// Randomness for this epoch. @@ -22,7 +41,7 @@ pub struct EpochV0 { impl EpochT for EpochV0 { type NextEpochDescriptor = NextEpochDescriptor; - type SlotNumber = SlotNumber; + type Slot = Slot; fn increment( &self, @@ -37,11 +56,11 @@ impl EpochT for EpochV0 { } } - fn start_slot(&self) -> SlotNumber { + fn start_slot(&self) -> Slot { self.start_slot } - fn end_slot(&self) -> SlotNumber { + fn end_slot(&self) -> Slot { self.start_slot + self.duration } } diff --git a/client/consensus/babe/src/tests.rs b/client/consensus/babe/src/tests.rs index 82d8f9de5af02..9d03a3266d615 100644 --- a/client/consensus/babe/src/tests.rs +++ b/client/consensus/babe/src/tests.rs @@ -28,13 +28,7 @@ use sp_keystore::{ SyncCryptoStore, vrf::make_transcript as transcript_from_data, }; -use sp_consensus_babe::{ - AuthorityPair, - SlotNumber, - AllowedSlots, - make_transcript, - make_transcript_data, -}; +use sp_consensus_babe::{AuthorityPair, Slot, AllowedSlots, make_transcript, make_transcript_data}; use sc_consensus_slots::BackoffAuthoringOnFinalizedHeadLagging; use sc_block_builder::{BlockBuilder, BlockBuilderProvider}; use sp_consensus::{ @@ -87,7 +81,7 @@ struct DummyProposer { factory: DummyFactory, parent_hash: Hash, parent_number: u64, - parent_slot: SlotNumber, + parent_slot: Slot, } impl Environment for DummyFactory { @@ -101,7 +95,7 @@ impl Environment for DummyFactory { let parent_slot = crate::find_pre_digest::(parent_header) .expect("parent header has a pre-digest") - .slot_number(); + .slot(); future::ready(Ok(DummyProposer { factory: self.clone(), @@ -137,7 +131,7 @@ impl DummyProposer { let this_slot = crate::find_pre_digest::(block.header()) .expect("baked block has valid pre-digest") - .slot_number(); + .slot(); // figure out if we should add a consensus digest, since the test runtime // doesn't. @@ -529,7 +523,7 @@ fn can_author_block() { let mut i = 0; let epoch = Epoch { - start_slot: 0, + start_slot: 0.into(), authorities: vec![(public.into(), 1)], randomness: [0; 32], epoch_index: 1, @@ -550,7 +544,7 @@ fn can_author_block() { }; // with secondary slots enabled it should never be empty - match claim_slot(i, &epoch, &keystore) { + match claim_slot(i.into(), &epoch, &keystore) { None => i += 1, Some(s) => debug!(target: "babe", "Authored block {:?}", s.0), } @@ -559,7 +553,7 @@ fn can_author_block() { // of times. config.allowed_slots = AllowedSlots::PrimarySlots; loop { - match claim_slot(i, &epoch, &keystore) { + match claim_slot(i.into(), &epoch, &keystore) { None => i += 1, Some(s) => { debug!(target: "babe", "Authored block {:?}", s.0); @@ -572,15 +566,15 @@ fn can_author_block() { // Propose and import a new BABE block on top of the given parent. fn propose_and_import_block( parent: &TestHeader, - slot_number: Option, + slot: Option, proposer_factory: &mut DummyFactory, block_import: &mut BoxBlockImport, ) -> sp_core::H256 { let mut proposer = futures::executor::block_on(proposer_factory.init(parent)).unwrap(); - let slot_number = slot_number.unwrap_or_else(|| { + let slot = slot.unwrap_or_else(|| { let parent_pre_digest = find_pre_digest::(parent).unwrap(); - parent_pre_digest.slot_number() + 1 + parent_pre_digest.slot() + 1 }); let pre_digest = sp_runtime::generic::Digest { @@ -588,7 +582,7 @@ fn propose_and_import_block( Item::babe_pre_digest( PreDigest::SecondaryPlain(SecondaryPlainPreDigest { authority_index: 0, - slot_number, + slot, }), ), ], @@ -602,7 +596,7 @@ fn propose_and_import_block( descendent_query(&*proposer_factory.client), &parent_hash, *parent.number(), - slot_number, + slot, ).unwrap().unwrap(); let seal = { @@ -660,19 +654,19 @@ fn importing_block_one_sets_genesis_epoch() { let block_hash = propose_and_import_block( &genesis_header, - Some(999), + Some(999.into()), &mut proposer_factory, &mut block_import, ); - let genesis_epoch = Epoch::genesis(&data.link.config, 999); + let genesis_epoch = Epoch::genesis(&data.link.config, 999.into()); let epoch_changes = data.link.epoch_changes.lock(); let epoch_for_second_block = epoch_changes.epoch_data_for_child_of( descendent_query(&*client), &block_hash, 1, - 1000, + 1000.into(), |slot| Epoch::genesis(&data.link.config, slot), ).unwrap().unwrap(); @@ -809,7 +803,7 @@ fn verify_slots_are_strictly_increasing() { // we should have no issue importing this block let b1 = propose_and_import_block( &genesis_header, - Some(999), + Some(999.into()), &mut proposer_factory, &mut block_import, ); @@ -820,7 +814,7 @@ fn verify_slots_are_strictly_increasing() { // we will panic due to the `PanickingBlockImport` defined above. propose_and_import_block( &b1, - Some(999), + Some(999.into()), &mut proposer_factory, &mut block_import, ); @@ -836,7 +830,7 @@ fn babe_transcript_generation_match() { .expect("Generates authority pair"); let epoch = Epoch { - start_slot: 0, + start_slot: 0.into(), authorities: vec![(public.into(), 1)], randomness: [0; 32], epoch_index: 1, @@ -847,8 +841,8 @@ fn babe_transcript_generation_match() { }, }; - let orig_transcript = make_transcript(&epoch.randomness.clone(), 1, epoch.epoch_index); - let new_transcript = make_transcript_data(&epoch.randomness, 1, epoch.epoch_index); + let orig_transcript = make_transcript(&epoch.randomness.clone(), 1.into(), epoch.epoch_index); + let new_transcript = make_transcript_data(&epoch.randomness, 1.into(), epoch.epoch_index); let test = |t: merlin::Transcript| -> [u8; 16] { let mut b = [0u8; 16]; diff --git a/client/consensus/babe/src/verification.rs b/client/consensus/babe/src/verification.rs index 5d657b8b97113..53dfd9ed10cee 100644 --- a/client/consensus/babe/src/verification.rs +++ b/client/consensus/babe/src/verification.rs @@ -19,12 +19,13 @@ //! Verification for BABE headers. use sp_runtime::{traits::Header, traits::DigestItemFor}; use sp_core::{Pair, Public}; -use sp_consensus_babe::{make_transcript, AuthoritySignature, SlotNumber, AuthorityPair, AuthorityId}; +use sp_consensus_babe::{make_transcript, AuthoritySignature, AuthorityPair, AuthorityId}; use sp_consensus_babe::digests::{ PreDigest, PrimaryPreDigest, SecondaryPlainPreDigest, SecondaryVRFPreDigest, CompatibleDigestItem }; use sc_consensus_slots::CheckedHeader; +use sp_consensus_slots::Slot; use log::{debug, trace}; use super::{find_pre_digest, babe_err, Epoch, BlockT, Error}; use super::authorship::{calculate_primary_threshold, check_primary_threshold, secondary_slot_author}; @@ -38,7 +39,7 @@ pub(super) struct VerificationParams<'a, B: 'a + BlockT> { /// work. pub(super) pre_digest: Option, /// The slot number of the current time. - pub(super) slot_now: SlotNumber, + pub(super) slot_now: Slot, /// Epoch descriptor of the epoch this block _should_ be under, if it's valid. pub(super) epoch: &'a Epoch, } @@ -83,9 +84,9 @@ pub(super) fn check_header( // and that's what we sign let pre_hash = header.hash(); - if pre_digest.slot_number() > slot_now { + if pre_digest.slot() > slot_now { header.digest_mut().push(seal); - return Ok(CheckedHeader::Deferred(header, pre_digest.slot_number())); + return Ok(CheckedHeader::Deferred(header, pre_digest.slot())); } let author = match authorities.get(pre_digest.authority_index() as usize) { @@ -98,7 +99,7 @@ pub(super) fn check_header( debug!(target: "babe", "Verifying primary block #{} at slot: {}", header.number(), - primary.slot_number, + primary.slot, ); check_primary_header::( @@ -113,7 +114,7 @@ pub(super) fn check_header( debug!(target: "babe", "Verifying secondary plain block #{} at slot: {}", header.number(), - secondary.slot_number, + secondary.slot, ); check_secondary_plain_header::( @@ -127,7 +128,7 @@ pub(super) fn check_header( debug!(target: "babe", "Verifying secondary VRF block #{} at slot: {}", header.number(), - secondary.slot_number, + secondary.slot, ); check_secondary_vrf_header::( @@ -173,7 +174,7 @@ fn check_primary_header( let (inout, _) = { let transcript = make_transcript( &epoch.randomness, - pre_digest.slot_number, + pre_digest.slot, epoch.epoch_index, ); @@ -213,7 +214,7 @@ fn check_secondary_plain_header( // check the signature is valid under the expected authority and // chain state. let expected_author = secondary_slot_author( - pre_digest.slot_number, + pre_digest.slot, &epoch.authorities, epoch.randomness, ).ok_or_else(|| Error::NoSecondaryAuthorExpected)?; @@ -241,7 +242,7 @@ fn check_secondary_vrf_header( // check the signature is valid under the expected authority and // chain state. let expected_author = secondary_slot_author( - pre_digest.slot_number, + pre_digest.slot, &epoch.authorities, epoch.randomness, ).ok_or_else(|| Error::NoSecondaryAuthorExpected)?; @@ -255,7 +256,7 @@ fn check_secondary_vrf_header( if AuthorityPair::verify(&signature, pre_hash.as_ref(), author) { let transcript = make_transcript( &epoch.randomness, - pre_digest.slot_number, + pre_digest.slot, epoch.epoch_index, ); diff --git a/client/consensus/common/Cargo.toml b/client/consensus/common/Cargo.toml index 6587553a73703..41c42866e7272 100644 --- a/client/consensus/common/Cargo.toml +++ b/client/consensus/common/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-consensus" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -13,7 +13,7 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sc-client-api = { version = "2.0.0", path = "../../api" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } +sc-client-api = { version = "3.0.0", path = "../../api" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } diff --git a/client/consensus/epochs/Cargo.toml b/client/consensus/epochs/Cargo.toml index 752280e3547dd..bebe6979e694e 100644 --- a/client/consensus/epochs/Cargo.toml +++ b/client/consensus/epochs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-consensus-epochs" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Generic epochs-based utilities for consensus" edition = "2018" @@ -13,9 +13,9 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } parking_lot = "0.11.1" -fork-tree = { version = "2.0.0", path = "../../../utils/fork-tree" } -sp-runtime = { path = "../../../primitives/runtime" , version = "2.0.0"} -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sc-client-api = { path = "../../api" , version = "2.0.0"} +fork-tree = { version = "3.0.0", path = "../../../utils/fork-tree" } +sp-runtime = { path = "../../../primitives/runtime" , version = "3.0.0"} +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sc-client-api = { path = "../../api" , version = "3.0.0"} diff --git a/client/consensus/epochs/src/lib.rs b/client/consensus/epochs/src/lib.rs index 76e8c8ed5419d..5c5ef446993a2 100644 --- a/client/consensus/epochs/src/lib.rs +++ b/client/consensus/epochs/src/lib.rs @@ -78,13 +78,13 @@ pub trait Epoch { /// Descriptor for the next epoch. type NextEpochDescriptor; /// Type of the slot number. - type SlotNumber: Ord + Copy; + type Slot: Ord + Copy; /// The starting slot of the epoch. - fn start_slot(&self) -> Self::SlotNumber; + fn start_slot(&self) -> Self::Slot; /// Produce the "end slot" of the epoch. This is NOT inclusive to the epoch, /// i.e. the slots covered by the epoch are `self.start_slot() .. self.end_slot()`. - fn end_slot(&self) -> Self::SlotNumber; + fn end_slot(&self) -> Self::Slot; /// Increment the epoch data, using the next epoch descriptor. fn increment(&self, descriptor: Self::NextEpochDescriptor) -> Self; } @@ -102,10 +102,10 @@ impl<'a, E: Epoch> From<&'a E> for EpochHeader { #[derive(Eq, PartialEq, Encode, Decode, Debug)] pub struct EpochHeader { /// The starting slot of the epoch. - pub start_slot: E::SlotNumber, + pub start_slot: E::Slot, /// The end slot of the epoch. This is NOT inclusive to the epoch, /// i.e. the slots covered by the epoch are `self.start_slot() .. self.end_slot()`. - pub end_slot: E::SlotNumber, + pub end_slot: E::Slot, } impl Clone for EpochHeader { @@ -215,14 +215,14 @@ impl ViableEpoch where #[derive(PartialEq, Eq, Clone, Debug)] pub enum ViableEpochDescriptor { /// The epoch is an unimported genesis, with given start slot number. - UnimportedGenesis(E::SlotNumber), + UnimportedGenesis(E::Slot), /// The epoch is signaled and has been imported, with given identifier and header. Signaled(EpochIdentifier, EpochHeader) } impl ViableEpochDescriptor { /// Start slot of the descriptor. - pub fn start_slot(&self) -> E::SlotNumber { + pub fn start_slot(&self) -> E::Slot { match self { Self::UnimportedGenesis(start_slot) => *start_slot, Self::Signaled(_, header) => header.start_slot, @@ -339,7 +339,7 @@ impl EpochChanges where /// Map the epoch changes from one storing data to a different one. pub fn map(self, mut f: F) -> EpochChanges where - B: Epoch, + B: Epoch, F: FnMut(&Hash, &Number, E) -> B, { EpochChanges { @@ -394,7 +394,7 @@ impl EpochChanges where descendent_of_builder: D, hash: &Hash, number: Number, - slot: E::SlotNumber, + slot: E::Slot, ) -> Result<(), fork_tree::Error> { let is_descendent_of = descendent_of_builder .build_is_descendent_of(None); @@ -445,11 +445,11 @@ impl EpochChanges where descriptor: &ViableEpochDescriptor, make_genesis: G, ) -> Option> where - G: FnOnce(E::SlotNumber) -> E + G: FnOnce(E::Slot) -> E { match descriptor { - ViableEpochDescriptor::UnimportedGenesis(slot_number) => { - Some(ViableEpoch::UnimportedGenesis(make_genesis(*slot_number))) + ViableEpochDescriptor::UnimportedGenesis(slot) => { + Some(ViableEpoch::UnimportedGenesis(make_genesis(*slot))) }, ViableEpochDescriptor::Signaled(identifier, _) => { self.epoch(&identifier).map(ViableEpoch::Signaled) @@ -479,11 +479,11 @@ impl EpochChanges where descriptor: &ViableEpochDescriptor, make_genesis: G, ) -> Option> where - G: FnOnce(E::SlotNumber) -> E + G: FnOnce(E::Slot) -> E { match descriptor { - ViableEpochDescriptor::UnimportedGenesis(slot_number) => { - Some(ViableEpoch::UnimportedGenesis(make_genesis(*slot_number))) + ViableEpochDescriptor::UnimportedGenesis(slot) => { + Some(ViableEpoch::UnimportedGenesis(make_genesis(*slot))) }, ViableEpochDescriptor::Signaled(identifier, _) => { self.epoch_mut(&identifier).map(ViableEpoch::Signaled) @@ -500,12 +500,12 @@ impl EpochChanges where descriptor: &ViableEpochDescriptor, make_genesis: G ) -> Option where - G: FnOnce(E::SlotNumber) -> E, + G: FnOnce(E::Slot) -> E, E: Clone, { match descriptor { - ViableEpochDescriptor::UnimportedGenesis(slot_number) => { - Some(make_genesis(*slot_number)) + ViableEpochDescriptor::UnimportedGenesis(slot) => { + Some(make_genesis(*slot)) }, ViableEpochDescriptor::Signaled(identifier, _) => { self.epoch(&identifier).cloned() @@ -523,17 +523,17 @@ impl EpochChanges where descendent_of_builder: D, parent_hash: &Hash, parent_number: Number, - slot_number: E::SlotNumber, + slot: E::Slot, make_genesis: G, ) -> Result, fork_tree::Error> where - G: FnOnce(E::SlotNumber) -> E, + G: FnOnce(E::Slot) -> E, E: Clone, { let descriptor = self.epoch_descriptor_for_child_of( descendent_of_builder, parent_hash, parent_number, - slot_number + slot )?; Ok(descriptor.and_then(|des| self.epoch_data(&des, make_genesis))) @@ -548,7 +548,7 @@ impl EpochChanges where descendent_of_builder: D, parent_hash: &Hash, parent_number: Number, - slot_number: E::SlotNumber, + slot: E::Slot, ) -> Result>, fork_tree::Error> { // find_node_where will give you the node in the fork-tree which is an ancestor // of the `parent_hash` by default. if the last epoch was signalled at the parent_hash, @@ -561,7 +561,7 @@ impl EpochChanges where if parent_number == Zero::zero() { // need to insert the genesis epoch. - return Ok(Some(ViableEpochDescriptor::UnimportedGenesis(slot_number))) + return Ok(Some(ViableEpochDescriptor::UnimportedGenesis(slot))) } // We want to find the deepest node in the tree which is an ancestor @@ -571,9 +571,9 @@ impl EpochChanges where // we need. let predicate = |epoch: &PersistedEpochHeader| match *epoch { PersistedEpochHeader::Genesis(ref epoch_0, _) => - epoch_0.start_slot <= slot_number, + epoch_0.start_slot <= slot, PersistedEpochHeader::Regular(ref epoch_n) => - epoch_n.start_slot <= slot_number, + epoch_n.start_slot <= slot, }; self.inner.find_node_where( @@ -588,7 +588,7 @@ impl EpochChanges where // and here we figure out which of the internal epochs // of a genesis node to use based on their start slot. PersistedEpochHeader::Genesis(ref epoch_0, ref epoch_1) => - if epoch_1.start_slot <= slot_number { + if epoch_1.start_slot <= slot { (EpochIdentifierPosition::Genesis1, epoch_1.clone()) } else { (EpochIdentifierPosition::Genesis0, epoch_0.clone()) @@ -695,17 +695,17 @@ mod tests { } type Hash = [u8; 1]; - type SlotNumber = u64; + type Slot = u64; #[derive(Debug, Clone, Eq, PartialEq)] struct Epoch { - start_slot: SlotNumber, - duration: SlotNumber, + start_slot: Slot, + duration: Slot, } impl EpochT for Epoch { type NextEpochDescriptor = (); - type SlotNumber = SlotNumber; + type Slot = Slot; fn increment(&self, _: ()) -> Self { Epoch { @@ -714,11 +714,11 @@ mod tests { } } - fn end_slot(&self) -> SlotNumber { + fn end_slot(&self) -> Slot { self.start_slot + self.duration } - fn start_slot(&self) -> SlotNumber { + fn start_slot(&self) -> Slot { self.start_slot } } @@ -748,8 +748,8 @@ mod tests { ).unwrap().unwrap(); match genesis_epoch { - ViableEpochDescriptor::UnimportedGenesis(slot_number) => { - assert_eq!(slot_number, 10101u64); + ViableEpochDescriptor::UnimportedGenesis(slot) => { + assert_eq!(slot, 10101u64); }, _ => panic!("should be unimported genesis"), }; @@ -762,8 +762,8 @@ mod tests { ).unwrap().unwrap(); match genesis_epoch_2 { - ViableEpochDescriptor::UnimportedGenesis(slot_number) => { - assert_eq!(slot_number, 10102u64); + ViableEpochDescriptor::UnimportedGenesis(slot) => { + assert_eq!(slot, 10102u64); }, _ => panic!("should be unimported genesis"), }; diff --git a/client/consensus/manual-seal/Cargo.toml b/client/consensus/manual-seal/Cargo.toml index b13cbc7b5590c..679fd5a3eb388 100644 --- a/client/consensus/manual-seal/Cargo.toml +++ b/client/consensus/manual-seal/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-consensus-manual-seal" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Manual sealing engine for Substrate" edition = "2018" @@ -20,32 +20,33 @@ jsonrpc-core-client = "15.1.0" jsonrpc-derive = "15.1.0" log = "0.4.8" parking_lot = "0.11.1" -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } serde = { version = "1.0", features=["derive"] } assert_matches = "1.3.0" -sc-client-api = { path = "../../api", version = "2.0.0" } -sc-consensus-babe = { path = "../../consensus/babe", version = "0.8.0" } -sc-consensus-epochs = { path = "../../consensus/epochs", version = "0.8.0" } -sp-consensus-babe = { path = "../../../primitives/consensus/babe", version = "0.8.0" } +sc-client-api = { path = "../../api", version = "3.0.0"} +sc-consensus-babe = { path = "../../consensus/babe", version = "0.9.0"} +sc-consensus-epochs = { path = "../../consensus/epochs", version = "0.9.0"} +sp-consensus-babe = { path = "../../../primitives/consensus/babe", version = "0.9.0"} -sc-transaction-pool = { path = "../../transaction-pool", version = "2.0.0" } -sp-blockchain = { path = "../../../primitives/blockchain", version = "2.0.0" } -sp-consensus = { package = "sp-consensus", path = "../../../primitives/consensus/common", version = "0.8.0" } -sp-inherents = { path = "../../../primitives/inherents", version = "2.0.0" } -sp-runtime = { path = "../../../primitives/runtime", version = "2.0.0" } -sp-core = { path = "../../../primitives/core", version = "2.0.0" } -sp-keystore = { path = "../../../primitives/keystore", version = "0.8.0" } -sp-keyring = { path = "../../../primitives/keyring", version = "2.0.0" } -sp-api = { path = "../../../primitives/api", version = "2.0.0" } -sp-transaction-pool = { path = "../../../primitives/transaction-pool", version = "2.0.0" } -sp-timestamp = { path = "../../../primitives/timestamp", version = "2.0.0" } +sc-transaction-pool = { path = "../../transaction-pool", version = "3.0.0"} +sp-blockchain = { path = "../../../primitives/blockchain", version = "3.0.0"} +sp-consensus = { path = "../../../primitives/consensus/common", version = "0.9.0"} +sp-consensus-slots = { path = "../../../primitives/consensus/slots", version = "0.9.0"} +sp-inherents = { path = "../../../primitives/inherents", version = "3.0.0"} +sp-runtime = { path = "../../../primitives/runtime", version = "3.0.0"} +sp-core = { path = "../../../primitives/core", version = "3.0.0"} +sp-keystore = { path = "../../../primitives/keystore", version = "0.9.0"} +sp-keyring = { path = "../../../primitives/keyring", version = "3.0.0"} +sp-api = { path = "../../../primitives/api", version = "3.0.0"} +sp-transaction-pool = { path = "../../../primitives/transaction-pool", version = "3.0.0"} +sp-timestamp = { path = "../../../primitives/timestamp", version = "3.0.0"} -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.8.0" } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0"} [dev-dependencies] tokio = { version = "0.2", features = ["rt-core", "macros"] } -sc-basic-authorship = { path = "../../basic-authorship", version = "0.8.0" } +sc-basic-authorship = { path = "../../basic-authorship", version = "0.9.0"} substrate-test-runtime-client = { path = "../../../test-utils/runtime/client", version = "2.0.0" } substrate-test-runtime-transaction-pool = { path = "../../../test-utils/runtime/transaction-pool", version = "2.0.0" } tempfile = "3.1.0" diff --git a/client/consensus/manual-seal/src/consensus/babe.rs b/client/consensus/manual-seal/src/consensus/babe.rs index 1566b647f2c01..247a8d9091a64 100644 --- a/client/consensus/manual-seal/src/consensus/babe.rs +++ b/client/consensus/manual-seal/src/consensus/babe.rs @@ -38,6 +38,7 @@ use sp_keystore::SyncCryptoStorePtr; use sp_api::{ProvideRuntimeApi, TransactionFor}; use sp_blockchain::{HeaderBackend, HeaderMetadata}; use sp_consensus::BlockImportParams; +use sp_consensus_slots::Slot; use sp_consensus_babe::{ BabeApi, inherents::BabeInherentData, ConsensusLog, BABE_ENGINE_ID, AuthorityId, digests::{PreDigest, SecondaryPlainPreDigest, NextEpochDescriptor}, BabeAuthorityWeight, @@ -72,7 +73,7 @@ impl BabeConsensusDataProvider where B: BlockT, C: AuxStore + HeaderBackend + ProvideRuntimeApi + HeaderMetadata, - C::Api: BabeApi, + C::Api: BabeApi, { pub fn new( client: Arc, @@ -100,14 +101,14 @@ impl BabeConsensusDataProvider }) } - fn epoch(&self, parent: &B::Header, slot_number: u64) -> Result { + fn epoch(&self, parent: &B::Header, slot: Slot) -> Result { let epoch_changes = self.epoch_changes.lock(); let epoch_descriptor = epoch_changes .epoch_descriptor_for_child_of( descendent_query(&*self.client), &parent.hash(), parent.number().clone(), - slot_number, + slot, ) .map_err(|e| Error::StringError(format!("failed to fetch epoch_descriptor: {}", e)))? .ok_or_else(|| sp_consensus::Error::InvalidAuthoritiesSet)?; @@ -130,16 +131,20 @@ impl ConsensusDataProvider for BabeConsensusDataProvider where B: BlockT, C: AuxStore + HeaderBackend + HeaderMetadata + ProvideRuntimeApi, - C::Api: BabeApi, + C::Api: BabeApi, { type Transaction = TransactionFor; fn create_digest(&self, parent: &B::Header, inherents: &InherentData) -> Result, Error> { - let slot_number = inherents.babe_inherent_data()?; - let epoch = self.epoch(parent, slot_number)?; + let slot = inherents.babe_inherent_data()?; + let epoch = self.epoch(parent, slot)?; // this is a dev node environment, we should always be able to claim a slot. - let logs = if let Some((predigest, _)) = authorship::claim_slot(slot_number, &epoch, &self.keystore) { + let logs = if let Some((predigest, _)) = authorship::claim_slot( + slot, + &epoch, + &self.keystore, + ) { vec![ as CompatibleDigestItem>::babe_pre_digest(predigest), ] @@ -147,7 +152,7 @@ impl ConsensusDataProvider for BabeConsensusDataProvider // well we couldn't claim a slot because this is an existing chain and we're not in the authorities. // we need to tell BabeBlockImport that the epoch has changed, and we put ourselves in the authorities. let predigest = PreDigest::SecondaryPlain(SecondaryPlainPreDigest { - slot_number, + slot, authority_index: 0_u32, }); @@ -157,7 +162,7 @@ impl ConsensusDataProvider for BabeConsensusDataProvider descendent_query(&*self.client), &parent.hash(), parent.number().clone(), - slot_number, + slot, ) .map_err(|e| Error::StringError(format!("failed to fetch epoch_descriptor: {}", e)))? .ok_or_else(|| sp_consensus::Error::InvalidAuthoritiesSet)?; @@ -194,21 +199,21 @@ impl ConsensusDataProvider for BabeConsensusDataProvider params: &mut BlockImportParams, inherents: &InherentData ) -> Result<(), Error> { - let slot_number = inherents.babe_inherent_data()?; + let slot = inherents.babe_inherent_data()?; let epoch_changes = self.epoch_changes.lock(); let mut epoch_descriptor = epoch_changes .epoch_descriptor_for_child_of( descendent_query(&*self.client), &parent.hash(), parent.number().clone(), - slot_number, + slot, ) .map_err(|e| Error::StringError(format!("failed to fetch epoch_descriptor: {}", e)))? .ok_or_else(|| sp_consensus::Error::InvalidAuthoritiesSet)?; // drop the lock drop(epoch_changes); // a quick check to see if we're in the authorities - let epoch = self.epoch(parent, slot_number)?; + let epoch = self.epoch(parent, slot)?; let (authority, _) = self.authorities.first().expect("authorities is non-emptyp; qed"); let has_authority = epoch.authorities.iter() .find(|(id, _)| *id == *authority) @@ -216,15 +221,15 @@ impl ConsensusDataProvider for BabeConsensusDataProvider if !has_authority { log::info!(target: "manual-seal", "authority not found"); - let slot_number = inherents.timestamp_inherent_data()? / self.config.slot_duration; + let slot = inherents.timestamp_inherent_data()? / self.config.slot_duration; // manually hard code epoch descriptor epoch_descriptor = match epoch_descriptor { ViableEpochDescriptor::Signaled(identifier, _header) => { ViableEpochDescriptor::Signaled( identifier, EpochHeader { - start_slot: slot_number, - end_slot: slot_number * self.config.epoch_length, + start_slot: slot.into(), + end_slot: (slot * self.config.epoch_length).into(), }, ) }, @@ -254,7 +259,7 @@ impl SlotTimestampProvider { where B: BlockT, C: AuxStore + HeaderBackend + ProvideRuntimeApi, - C::Api: BabeApi, + C::Api: BabeApi, { let slot_duration = Config::get_or_compute(&*client)?.slot_duration; let info = client.info(); @@ -263,9 +268,9 @@ impl SlotTimestampProvider { // otherwise we'd be producing blocks for older slots. let duration = if info.best_number != Zero::zero() { let header = client.header(BlockId::Hash(info.best_hash))?.unwrap(); - let slot_number = find_pre_digest::(&header).unwrap().slot_number(); + let slot = find_pre_digest::(&header).unwrap().slot(); // add the slot duration so there's no collision of slots - (slot_number * slot_duration) + slot_duration + (*slot * slot_duration) + slot_duration } else { // this is the first block, use the correct time. let now = SystemTime::now(); diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index 3ec68588573eb..320f196c1052c 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -73,7 +73,7 @@ impl Verifier for ManualSealVerifier { /// Instantiate the import queue for the manual seal consensus engine. pub fn import_queue( block_import: BoxBlockImport, - spawner: &impl sp_core::traits::SpawnNamed, + spawner: &impl sp_core::traits::SpawnEssentialNamed, registry: Option<&Registry>, ) -> BasicQueue where diff --git a/client/consensus/pow/Cargo.toml b/client/consensus/pow/Cargo.toml index b5112f9fa628f..8be43a8fa04bc 100644 --- a/client/consensus/pow/Cargo.toml +++ b/client/consensus/pow/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-consensus-pow" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "PoW consensus algorithm for substrate" edition = "2018" @@ -13,20 +13,20 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-api = { version = "2.0.0", path = "../../../primitives/api" } -sc-client-api = { version = "2.0.0", path = "../../api" } -sp-block-builder = { version = "2.0.0", path = "../../../primitives/block-builder" } -sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" } -sp-consensus-pow = { version = "0.8.0", path = "../../../primitives/consensus/pow" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } +sc-client-api = { version = "3.0.0", path = "../../api" } +sp-block-builder = { version = "3.0.0", path = "../../../primitives/block-builder" } +sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } +sp-consensus-pow = { version = "0.9.0", path = "../../../primitives/consensus/pow" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } log = "0.4.8" futures = { version = "0.3.1", features = ["compat"] } futures-timer = "3.0.1" parking_lot = "0.11.1" -sp-timestamp = { version = "2.0.0", path = "../../../primitives/timestamp" } +sp-timestamp = { version = "3.0.0", path = "../../../primitives/timestamp" } derive_more = "0.99.2" -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.8.0"} +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0"} diff --git a/client/consensus/pow/src/lib.rs b/client/consensus/pow/src/lib.rs index 975a6f17e795f..3c7f1a832d3cd 100644 --- a/client/consensus/pow/src/lib.rs +++ b/client/consensus/pow/src/lib.rs @@ -232,7 +232,7 @@ impl PowBlockImport wher I: BlockImport> + Send + Sync, I::Error: Into, C: ProvideRuntimeApi + Send + Sync + HeaderBackend + AuxStore + ProvideCache + BlockOf, - C::Api: BlockBuilderApi, + C::Api: BlockBuilderApi, Algorithm: PowAlgorithm, CAW: CanAuthorWith, { @@ -284,7 +284,7 @@ impl PowBlockImport wher &block_id, block, inherent_data, - ).map_err(Error::Client)?; + ).map_err(|e| Error::Client(e.into()))?; if !inherent_res.ok() { inherent_res @@ -314,7 +314,7 @@ impl BlockImport for PowBlockImport, S: SelectChain, C: ProvideRuntimeApi + Send + Sync + HeaderBackend + AuxStore + ProvideCache + BlockOf, - C::Api: BlockBuilderApi, + C::Api: BlockBuilderApi, Algorithm: PowAlgorithm, Algorithm::Difficulty: 'static, CAW: CanAuthorWith, @@ -505,7 +505,7 @@ pub fn import_queue( justification_import: Option>, algorithm: Algorithm, inherent_data_providers: InherentDataProviders, - spawner: &impl sp_core::traits::SpawnNamed, + spawner: &impl sp_core::traits::SpawnEssentialNamed, registry: Option<&Registry>, ) -> Result< PowImportQueue, diff --git a/client/consensus/slots/Cargo.toml b/client/consensus/slots/Cargo.toml index bdf28f35236b2..7ca413630e26e 100644 --- a/client/consensus/slots/Cargo.toml +++ b/client/consensus/slots/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-consensus-slots" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Generic slots-based utilities for consensus" edition = "2018" @@ -14,20 +14,20 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6" } -sc-client-api = { version = "2.0.0", path = "../../api" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-trie = { version = "2.0.0", path = "../../../primitives/trie" } -sp-application-crypto = { version = "2.0.0", path = "../../../primitives/application-crypto" } -sp-arithmetic = { version = "2.0.0", path = "../../../primitives/arithmetic" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-consensus-slots = { version = "0.8.0", path = "../../../primitives/consensus/slots" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-state-machine = { version = "0.8.0", path = "../../../primitives/state-machine" } -sp-api = { version = "2.0.0", path = "../../../primitives/api" } -sc-telemetry = { version = "2.0.0", path = "../../telemetry" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" } +codec = { package = "parity-scale-codec", version = "2.0.0" } +sc-client-api = { version = "3.0.0", path = "../../api" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-trie = { version = "3.0.0", path = "../../../primitives/trie" } +sp-application-crypto = { version = "3.0.0", path = "../../../primitives/application-crypto" } +sp-arithmetic = { version = "3.0.0", path = "../../../primitives/arithmetic" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-consensus-slots = { version = "0.9.0", path = "../../../primitives/consensus/slots" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-state-machine = { version = "0.9.0", path = "../../../primitives/state-machine" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } +sc-telemetry = { version = "3.0.0", path = "../../telemetry" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } futures = "0.3.9" futures-timer = "3.0.1" parking_lot = "0.11.1" diff --git a/client/consensus/slots/src/aux_schema.rs b/client/consensus/slots/src/aux_schema.rs index c8095f238ec8c..db94ec48855e4 100644 --- a/client/consensus/slots/src/aux_schema.rs +++ b/client/consensus/slots/src/aux_schema.rs @@ -21,7 +21,7 @@ use codec::{Encode, Decode}; use sc_client_api::backend::AuxStore; use sp_blockchain::{Result as ClientResult, Error as ClientError}; -use sp_consensus_slots::EquivocationProof; +use sp_consensus_slots::{EquivocationProof, Slot}; use sp_runtime::traits::Header; const SLOT_HEADER_MAP_KEY: &[u8] = b"slot_header_map"; @@ -41,7 +41,7 @@ fn load_decode(backend: &C, key: &[u8]) -> ClientResult> None => Ok(None), Some(t) => T::decode(&mut &t[..]) .map_err( - |e| ClientError::Backend(format!("Slots DB is corrupted. Decode error: {}", e.what())), + |e| ClientError::Backend(format!("Slots DB is corrupted. Decode error: {}", e)), ) .map(Some) } @@ -52,8 +52,8 @@ fn load_decode(backend: &C, key: &[u8]) -> ClientResult> /// Note: it detects equivocations only when slot_now - slot <= MAX_SLOT_CAPACITY. pub fn check_equivocation( backend: &C, - slot_now: u64, - slot: u64, + slot_now: Slot, + slot: Slot, header: &H, signer: &P, ) -> ClientResult>> @@ -63,7 +63,7 @@ pub fn check_equivocation( P: Clone + Encode + Decode + PartialEq, { // We don't check equivocations for old headers out of our capacity. - if slot_now.saturating_sub(slot) > MAX_SLOT_CAPACITY { + if slot_now.saturating_sub(*slot) > Slot::from(MAX_SLOT_CAPACITY) { return Ok(None); } @@ -77,7 +77,7 @@ pub fn check_equivocation( // Get first slot saved. let slot_header_start = SLOT_HEADER_START.to_vec(); - let first_saved_slot = load_decode::<_, u64>(backend, &slot_header_start[..])? + let first_saved_slot = load_decode::<_, Slot>(backend, &slot_header_start[..])? .unwrap_or(slot); if slot_now < first_saved_slot { @@ -92,7 +92,7 @@ pub fn check_equivocation( // 2) with different hash if header.hash() != prev_header.hash() { return Ok(Some(EquivocationProof { - slot_number: slot, + slot, offender: signer.clone(), first_header: prev_header.clone(), second_header: header.clone(), @@ -109,11 +109,11 @@ pub fn check_equivocation( let mut keys_to_delete = vec![]; let mut new_first_saved_slot = first_saved_slot; - if slot_now - first_saved_slot >= PRUNING_BOUND { + if *slot_now - *first_saved_slot >= PRUNING_BOUND { let prefix = SLOT_HEADER_MAP_KEY.to_vec(); new_first_saved_slot = slot_now.saturating_sub(MAX_SLOT_CAPACITY); - for s in first_saved_slot..new_first_saved_slot { + for s in u64::from(first_saved_slot)..new_first_saved_slot.into() { let mut p = prefix.clone(); s.using_encoded(|s| p.extend(s)); keys_to_delete.push(p); @@ -174,8 +174,8 @@ mod test { assert!( check_equivocation( &client, - 2, - 2, + 2.into(), + 2.into(), &header1, &public, ).unwrap().is_none(), @@ -184,8 +184,8 @@ mod test { assert!( check_equivocation( &client, - 3, - 2, + 3.into(), + 2.into(), &header1, &public, ).unwrap().is_none(), @@ -195,8 +195,8 @@ mod test { assert!( check_equivocation( &client, - 4, - 2, + 4.into(), + 2.into(), &header2, &public, ).unwrap().is_some(), @@ -206,8 +206,8 @@ mod test { assert!( check_equivocation( &client, - 5, - 4, + 5.into(), + 4.into(), &header3, &public, ).unwrap().is_none(), @@ -217,8 +217,8 @@ mod test { assert!( check_equivocation( &client, - PRUNING_BOUND + 2, - MAX_SLOT_CAPACITY + 4, + (PRUNING_BOUND + 2).into(), + (MAX_SLOT_CAPACITY + 4).into(), &header4, &public, ).unwrap().is_none(), @@ -228,8 +228,8 @@ mod test { assert!( check_equivocation( &client, - PRUNING_BOUND + 3, - MAX_SLOT_CAPACITY + 4, + (PRUNING_BOUND + 3).into(), + (MAX_SLOT_CAPACITY + 4).into(), &header5, &public, ).unwrap().is_some(), @@ -239,8 +239,8 @@ mod test { assert!( check_equivocation( &client, - PRUNING_BOUND + 4, - 4, + (PRUNING_BOUND + 4).into(), + 4.into(), &header6, &public, ).unwrap().is_none(), diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 93d3614584f8f..1df8378d514f3 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -41,6 +41,7 @@ use parking_lot::Mutex; use sp_api::{ProvideRuntimeApi, ApiRef}; use sp_arithmetic::traits::BaseArithmetic; use sp_consensus::{BlockImport, Proposer, SyncOracle, SelectChain, CanAuthorWith, SlotData, RecordProof}; +use sp_consensus_slots::Slot; use sp_inherents::{InherentData, InherentDataProviders}; use sp_runtime::{ generic::BlockId, @@ -70,14 +71,15 @@ pub struct SlotResult { /// The implementation should not make any assumptions of the slot being bound to the time or /// similar. The only valid assumption is that the slot number is always increasing. pub trait SlotWorker { - /// The type of the future that will be returned when a new slot is triggered. - type OnSlot: Future>>; - /// Called when a new slot is triggered. /// /// Returns a future that resolves to a [`SlotResult`] iff a block was successfully built in /// the slot. Otherwise `None` is returned. - fn on_slot(&mut self, chain_head: B::Header, slot_info: SlotInfo) -> Self::OnSlot; + fn on_slot( + &mut self, + chain_head: B::Header, + slot_info: SlotInfo, + ) -> Pin>> + Send>>; } /// A skeleton implementation for `SlotWorker` which tries to claim a slot at @@ -115,7 +117,7 @@ pub trait SimpleSlotWorker { fn epoch_data( &self, header: &B::Header, - slot_number: u64, + slot: Slot, ) -> Result; /// Returns the number of authorities given the epoch data. @@ -126,7 +128,7 @@ pub trait SimpleSlotWorker { fn claim_slot( &self, header: &B::Header, - slot_number: u64, + slot: Slot, epoch_data: &Self::EpochData, ) -> Option; @@ -135,14 +137,14 @@ pub trait SimpleSlotWorker { fn notify_slot( &self, _header: &B::Header, - _slot_number: u64, + _slot: Slot, _epoch_data: &Self::EpochData, ) {} /// Return the pre digest data to include in a block authored with the given claim. fn pre_digest_data( &self, - slot_number: u64, + slot: Slot, claim: &Self::Claim, ) -> Vec>; @@ -170,7 +172,7 @@ pub trait SimpleSlotWorker { /// /// An example strategy that back offs if the finalized head is lagging too much behind the tip /// is implemented by [`BackoffAuthoringOnFinalizedHeadLagging`]. - fn should_backoff(&self, _slot_number: u64, _chain_head: &B::Header) -> bool { + fn should_backoff(&self, _slot: Slot, _chain_head: &B::Header) -> bool { false } @@ -208,7 +210,7 @@ pub trait SimpleSlotWorker { where >::Proposal: Unpin + Send + 'static, { - let (timestamp, slot_number) = (slot_info.timestamp, slot_info.number); + let (timestamp, slot) = (slot_info.timestamp, slot_info.slot); let slot_remaining_duration = self.slot_remaining_duration(&slot_info); let proposing_remaining_duration = self.proposing_remaining_duration(&chain_head, &slot_info); @@ -218,7 +220,7 @@ pub trait SimpleSlotWorker { debug!( target: self.logging_target(), "Skipping proposal slot {} since there's no time left to propose", - slot_number, + slot, ); return Box::pin(future::ready(None)); @@ -227,7 +229,7 @@ pub trait SimpleSlotWorker { None => Box::new(future::pending()) as Box<_>, }; - let epoch_data = match self.epoch_data(&chain_head, slot_number) { + let epoch_data = match self.epoch_data(&chain_head, slot) { Ok(epoch_data) => epoch_data, Err(err) => { warn!("Unable to fetch epoch data at block {:?}: {:?}", chain_head.hash(), err); @@ -242,7 +244,7 @@ pub trait SimpleSlotWorker { } }; - self.notify_slot(&chain_head, slot_number, &epoch_data); + self.notify_slot(&chain_head, slot, &epoch_data); let authorities_len = self.authorities_len(&epoch_data); @@ -260,38 +262,43 @@ pub trait SimpleSlotWorker { return Box::pin(future::ready(None)); } - let claim = match self.claim_slot(&chain_head, slot_number, &epoch_data) { + let claim = match self.claim_slot(&chain_head, slot, &epoch_data) { None => return Box::pin(future::ready(None)), Some(claim) => claim, }; - if self.should_backoff(slot_number, &chain_head) { + if self.should_backoff(slot, &chain_head) { return Box::pin(future::ready(None)); } debug!( target: self.logging_target(), "Starting authorship at slot {}; timestamp = {}", - slot_number, + slot, timestamp, ); - telemetry!(CONSENSUS_DEBUG; "slots.starting_authorship"; - "slot_num" => slot_number, + telemetry!( + CONSENSUS_DEBUG; + "slots.starting_authorship"; + "slot_num" => *slot, "timestamp" => timestamp, ); let awaiting_proposer = self.proposer(&chain_head).map_err(move |err| { - warn!("Unable to author block in slot {:?}: {:?}", slot_number, err); + warn!("Unable to author block in slot {:?}: {:?}", slot, err); - telemetry!(CONSENSUS_WARN; "slots.unable_authoring_block"; - "slot" => slot_number, "err" => ?err + telemetry!( + CONSENSUS_WARN; + "slots.unable_authoring_block"; + "slot" => *slot, + "err" => ?err ); err }); - let logs = self.pre_digest_data(slot_number, &claim); + let logs = self.pre_digest_data(slot, &claim); // deadline our production to approx. the end of the slot let proposing = awaiting_proposer.and_then(move |proposer| proposer.propose( @@ -307,12 +314,14 @@ pub trait SimpleSlotWorker { futures::future::select(proposing, proposing_remaining).map(move |v| match v { Either::Left((b, _)) => b.map(|b| (b, claim)), Either::Right(_) => { - info!("⌛️ Discarding proposal for slot {}; block production took too long", slot_number); + info!("⌛️ Discarding proposal for slot {}; block production took too long", slot); // If the node was compiled with debug, tell the user to use release optimizations. #[cfg(build_type="debug")] info!("👉 Recompile your node in `--release` mode to mitigate this problem."); - telemetry!(CONSENSUS_INFO; "slots.discarding_proposal_took_too_long"; - "slot" => slot_number, + telemetry!( + CONSENSUS_INFO; + "slots.discarding_proposal_took_too_long"; + "slot" => *slot, ); Err(sp_consensus::Error::ClientImport("Timeout in the Slots proposer".into())) @@ -325,7 +334,7 @@ pub trait SimpleSlotWorker { proposal_work.and_then(move |(proposal, claim)| async move { let (block, storage_proof) = (proposal.block, proposal.proof); - let (header, body) = block.clone().deconstruct(); + let (header, body) = block.deconstruct(); let header_num = *header.number(); let header_hash = header.hash(); let parent_hash = *header.parent_hash(); @@ -333,7 +342,7 @@ pub trait SimpleSlotWorker { let block_import_params = block_import_params_maker( header, &header_hash, - body, + body.clone(), proposal.storage_changes, claim, epoch_data, @@ -352,6 +361,7 @@ pub trait SimpleSlotWorker { "hash_previously" => ?header_hash, ); + let header = block_import_params.post_header(); if let Err(err) = block_import.lock().import_block(block_import_params, Default::default()) { warn!( target: logging_target, @@ -367,7 +377,7 @@ pub trait SimpleSlotWorker { ); } - Ok(SlotResult { block, storage_proof }) + Ok(SlotResult { block: B::new(header, body), storage_proof }) }).then(|r| async move { r.map_err(|e| warn!(target: "slots", "Encountered consensus error: {:?}", e)).ok() }).boxed() @@ -375,9 +385,11 @@ pub trait SimpleSlotWorker { } impl> SlotWorker for T { - type OnSlot = Pin>> + Send>>; - - fn on_slot(&mut self, chain_head: B::Header, slot_info: SlotInfo) -> Self::OnSlot { + fn on_slot( + &mut self, + chain_head: B::Header, + slot_info: SlotInfo, + ) -> Pin>> + Send>> { SimpleSlotWorker::on_slot(self, chain_head, slot_info) } } @@ -388,7 +400,7 @@ pub trait SlotCompatible { fn extract_timestamp_and_slot( &self, inherent: &InherentData, - ) -> Result<(u64, u64, std::time::Duration), sp_consensus::Error>; + ) -> Result<(u64, Slot, std::time::Duration), sp_consensus::Error>; } /// Start a new slot worker. @@ -408,7 +420,6 @@ where B: BlockT, C: SelectChain, W: SlotWorker, - W::OnSlot: Unpin, SO: SyncOracle + Send, SC: SlotCompatible + Unpin, T: SlotData + Clone, @@ -429,12 +440,12 @@ where return Either::Right(future::ready(Ok(()))); } - let slot_num = slot_info.number; + let slot = slot_info.slot; let chain_head = match client.best_chain() { Ok(x) => x, Err(e) => { warn!(target: "slots", "Unable to author block in slot {}. \ - no best block header: {:?}", slot_num, e); + no best block header: {:?}", slot, e); return Either::Right(future::ready(Ok(()))); } }; @@ -444,7 +455,7 @@ where target: "slots", "Unable to author block in slot {},. `can_author_with` returned: {} \ Probably a node update is required!", - slot_num, + slot, err, ); Either::Right(future::ready(Ok(()))) @@ -465,7 +476,7 @@ where pub enum CheckedHeader { /// A header which has slot in the future. this is the full header (not stripped) /// and the slot in which it should be processed. - Deferred(H, u64), + Deferred(H, Slot), /// A header which is fully checked, including signature. This is the pre-header /// accompanied by the seal components. /// @@ -473,8 +484,6 @@ pub enum CheckedHeader { Checked(H, S), } - - #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] pub enum Error where T: Debug { @@ -561,7 +570,7 @@ impl SlotDuration { /// to parent. If the number of skipped slots is greated than 0 this method will apply /// an exponential backoff of at most `2^7 * slot_duration`, if no slots were skipped /// this method will return `None.` -pub fn slot_lenience_exponential(parent_slot: u64, slot_info: &SlotInfo) -> Option { +pub fn slot_lenience_exponential(parent_slot: Slot, slot_info: &SlotInfo) -> Option { // never give more than 2^this times the lenience. const BACKOFF_CAP: u64 = 7; @@ -574,7 +583,7 @@ pub fn slot_lenience_exponential(parent_slot: u64, slot_info: &SlotInfo) -> Opti // exponential back-off. // in normal cases we only attempt to issue blocks up to the end of the slot. // when the chain has been stalled for a few slots, we give more lenience. - let skipped_slots = slot_info.number.saturating_sub(parent_slot + 1); + let skipped_slots = *slot_info.slot.saturating_sub(parent_slot + 1); if skipped_slots == 0 { None @@ -590,7 +599,7 @@ pub fn slot_lenience_exponential(parent_slot: u64, slot_info: &SlotInfo) -> Opti /// to parent. If the number of skipped slots is greated than 0 this method will apply /// a linear backoff of at most `20 * slot_duration`, if no slots were skipped /// this method will return `None.` -pub fn slot_lenience_linear(parent_slot: u64, slot_info: &SlotInfo) -> Option { +pub fn slot_lenience_linear(parent_slot: Slot, slot_info: &SlotInfo) -> Option { // never give more than 20 times more lenience. const BACKOFF_CAP: u64 = 20; @@ -600,7 +609,7 @@ pub fn slot_lenience_linear(parent_slot: u64, slot_info: &SlotInfo) -> Option { fn should_backoff( &self, chain_head_number: N, - chain_head_slot: u64, + chain_head_slot: Slot, finalized_number: N, - slow_now: u64, + slow_now: Slot, logging_target: &str, ) -> bool; } @@ -663,9 +672,9 @@ where fn should_backoff( &self, chain_head_number: N, - chain_head_slot: u64, + chain_head_slot: Slot, finalized_number: N, - slot_now: u64, + slot_now: Slot, logging_target: &str, ) -> bool { // This should not happen, but we want to keep the previous behaviour if it does. @@ -683,7 +692,7 @@ where // If interval is nonzero we backoff if the current slot isn't far enough ahead of the chain // head. - if slot_now <= chain_head_slot + interval { + if *slot_now <= *chain_head_slot + interval { info!( target: logging_target, "Backing off claiming new slot for block authorship: finality is lagging.", @@ -699,9 +708,9 @@ impl BackoffAuthoringBlocksStrategy for () { fn should_backoff( &self, _chain_head_number: N, - _chain_head_slot: u64, + _chain_head_slot: Slot, _finalized_number: N, - _slot_now: u64, + _slot_now: Slot, _logging_target: &str, ) -> bool { false @@ -717,9 +726,9 @@ mod test { const SLOT_DURATION: Duration = Duration::from_millis(6000); - fn slot(n: u64) -> super::slots::SlotInfo { + fn slot(slot: u64) -> super::slots::SlotInfo { super::slots::SlotInfo { - number: n, + slot: slot.into(), duration: SLOT_DURATION.as_millis() as u64, timestamp: Default::default(), inherent_data: Default::default(), @@ -730,20 +739,20 @@ mod test { #[test] fn linear_slot_lenience() { // if no slots are skipped there should be no lenience - assert_eq!(super::slot_lenience_linear(1, &slot(2)), None); + assert_eq!(super::slot_lenience_linear(1.into(), &slot(2)), None); // otherwise the lenience is incremented linearly with // the number of skipped slots. for n in 3..=22 { assert_eq!( - super::slot_lenience_linear(1, &slot(n)), + super::slot_lenience_linear(1.into(), &slot(n)), Some(SLOT_DURATION * (n - 2) as u32), ); } // but we cap it to a maximum of 20 slots assert_eq!( - super::slot_lenience_linear(1, &slot(23)), + super::slot_lenience_linear(1.into(), &slot(23)), Some(SLOT_DURATION * 20), ); } @@ -751,24 +760,24 @@ mod test { #[test] fn exponential_slot_lenience() { // if no slots are skipped there should be no lenience - assert_eq!(super::slot_lenience_exponential(1, &slot(2)), None); + assert_eq!(super::slot_lenience_exponential(1.into(), &slot(2)), None); // otherwise the lenience is incremented exponentially every two slots for n in 3..=17 { assert_eq!( - super::slot_lenience_exponential(1, &slot(n)), + super::slot_lenience_exponential(1.into(), &slot(n)), Some(SLOT_DURATION * 2u32.pow((n / 2 - 1) as u32)), ); } // but we cap it to a maximum of 14 slots assert_eq!( - super::slot_lenience_exponential(1, &slot(18)), + super::slot_lenience_exponential(1.into(), &slot(18)), Some(SLOT_DURATION * 2u32.pow(7)), ); assert_eq!( - super::slot_lenience_exponential(1, &slot(19)), + super::slot_lenience_exponential(1.into(), &slot(19)), Some(SLOT_DURATION * 2u32.pow(7)), ); } @@ -808,7 +817,7 @@ mod test { let slot_now = 2; let should_backoff: Vec = (slot_now..1000) - .map(|s| strategy.should_backoff(head_number, head_slot, finalized_number, s, "slots")) + .map(|s| strategy.should_backoff(head_number, head_slot.into(), finalized_number, s.into(), "slots")) .collect(); // Should always be false, since the head isn't advancing @@ -833,9 +842,9 @@ mod test { .map(move |s| { let b = strategy.should_backoff( head_number, - head_slot, + head_slot.into(), finalized_number, - s, + s.into(), "slots", ); // Chain is still advancing (by someone else) @@ -872,7 +881,7 @@ mod test { let max_interval = strategy.max_interval; let should_backoff: Vec = (slot_now..200) - .map(|s| strategy.should_backoff(head_number, head_slot, finalized_number, s, "slots")) + .map(|s| strategy.should_backoff(head_number, head_slot.into(), finalized_number, s.into(), "slots")) .collect(); // Should backoff (true) until we are `max_interval` number of slots ahead of the chain @@ -900,9 +909,9 @@ mod test { >>::should_backoff( ¶m, head_state.head_number, - head_state.head_slot, + head_state.head_slot.into(), finalized_number, - head_state.slot_now, + head_state.slot_now.into(), "slots", ) }; @@ -972,9 +981,9 @@ mod test { >>::should_backoff( ¶m, head_state.head_number, - head_state.head_slot, + head_state.head_slot.into(), finalized_number, - head_state.slot_now, + head_state.slot_now.into(), "slots", ) }; @@ -1036,9 +1045,9 @@ mod test { >>::should_backoff( ¶m, head_state.head_number, - head_state.head_slot, + head_state.head_slot.into(), finalized_number, - head_state.slot_now, + head_state.slot_now.into(), "slots", ) }; diff --git a/client/consensus/slots/src/slots.rs b/client/consensus/slots/src/slots.rs index 0c93e16461ccb..d3bddccce0fad 100644 --- a/client/consensus/slots/src/slots.rs +++ b/client/consensus/slots/src/slots.rs @@ -20,7 +20,7 @@ //! //! This is used instead of `futures_timer::Interval` because it was unreliable. -use super::SlotCompatible; +use super::{SlotCompatible, Slot}; use sp_consensus::Error; use futures::{prelude::*, task::Context, task::Poll}; use sp_inherents::{InherentData, InherentDataProviders}; @@ -48,7 +48,7 @@ pub fn time_until_next(now: Duration, slot_duration: u64) -> Duration { /// Information about a slot. pub struct SlotInfo { /// The slot number. - pub number: u64, + pub slot: Slot, /// Current timestamp. pub timestamp: u64, /// The instant at which the slot ends. @@ -61,7 +61,7 @@ pub struct SlotInfo { /// A stream that returns every time there is a new slot. pub(crate) struct Slots { - last_slot: u64, + last_slot: Slot, slot_duration: u64, inner_delay: Option, inherent_data_providers: InherentDataProviders, @@ -76,7 +76,7 @@ impl Slots { timestamp_extractor: SC, ) -> Self { Slots { - last_slot: 0, + last_slot: 0.into(), slot_duration, inner_delay: None, inherent_data_providers, @@ -114,7 +114,7 @@ impl Stream for Slots { Err(err) => return Poll::Ready(Some(Err(sp_consensus::Error::InherentData(err)))), }; let result = self.timestamp_extractor.extract_timestamp_and_slot(&inherent_data); - let (timestamp, slot_num, offset) = match result { + let (timestamp, slot, offset) = match result { Ok(v) => v, Err(err) => return Poll::Ready(Some(Err(err))), }; @@ -125,11 +125,11 @@ impl Stream for Slots { self.inner_delay = Some(Delay::new(ends_in)); // never yield the same slot twice. - if slot_num > self.last_slot { - self.last_slot = slot_num; + if slot > self.last_slot { + self.last_slot = slot; break Poll::Ready(Some(Ok(SlotInfo { - number: slot_num, + slot, duration: self.slot_duration, timestamp, ends_at, diff --git a/client/consensus/uncles/Cargo.toml b/client/consensus/uncles/Cargo.toml index 0bdb25b1220ad..14a8c850562cb 100644 --- a/client/consensus/uncles/Cargo.toml +++ b/client/consensus/uncles/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-consensus-uncles" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Generic uncle inclusion utilities for consensus" edition = "2018" @@ -13,10 +13,10 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sc-client-api = { version = "2.0.0", path = "../../api" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-authorship = { version = "2.0.0", path = "../../../primitives/authorship" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" } +sc-client-api = { version = "3.0.0", path = "../../api" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-authorship = { version = "3.0.0", path = "../../../primitives/authorship" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } log = "0.4.8" diff --git a/client/db/Cargo.toml b/client/db/Cargo.toml index 23f6fa9b1f628..72c26fead1c1c 100644 --- a/client/db/Cargo.toml +++ b/client/db/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-client-db" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -15,35 +15,35 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] parking_lot = "0.11.1" log = "0.4.8" -kvdb = "0.8.0" -kvdb-rocksdb = { version = "0.10.0", optional = true } -kvdb-memorydb = "0.8.0" +kvdb = "0.9.0" +kvdb-rocksdb = { version = "0.11.0", optional = true } +kvdb-memorydb = "0.9.0" linked-hash-map = "0.5.2" hash-db = "0.15.2" -parity-util-mem = { version = "0.8.0", default-features = false, features = ["std"] } -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["std"] } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } blake2-rfc = "0.2.18" -sc-client-api = { version = "2.0.0", path = "../api" } -sp-arithmetic = { version = "2.0.0", path = "../../primitives/arithmetic" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine" } -sc-executor = { version = "0.8.0", path = "../executor" } -sc-state-db = { version = "0.8.0", path = "../state-db" } -sp-trie = { version = "2.0.0", path = "../../primitives/trie" } -sp-consensus = { version = "0.8.0", path = "../../primitives/consensus/common" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-database = { version = "2.0.0", path = "../../primitives/database" } -parity-db = { version = "0.1.2", optional = true } -prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0", path = "../../utils/prometheus" } +sc-client-api = { version = "3.0.0", path = "../api" } +sp-arithmetic = { version = "3.0.0", path = "../../primitives/arithmetic" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" } +sc-executor = { version = "0.9.0", path = "../executor" } +sc-state-db = { version = "0.9.0", path = "../state-db" } +sp-trie = { version = "3.0.0", path = "../../primitives/trie" } +sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-database = { version = "3.0.0", path = "../../primitives/database" } +parity-db = { version = "0.2.2", optional = true } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.9.0", path = "../../utils/prometheus" } [dev-dependencies] -sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" } -sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } +sp-keyring = { version = "3.0.0", path = "../../primitives/keyring" } +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } -quickcheck = "0.9" -kvdb-rocksdb = "0.10.0" +quickcheck = "1.0.3" +kvdb-rocksdb = "0.11.0" tempfile = "3" [features] diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index a976cbc2ce8d0..6654083939dae 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -448,20 +448,6 @@ impl BlockchainDb { header.digest().log(DigestItem::as_changes_trie_root) .cloned())) } - - fn extrinsic(&self, hash: &Block::Hash) -> ClientResult> { - match self.db.get(columns::TRANSACTION, hash.as_ref()) { - Some(ex) => { - match Decode::decode(&mut &ex[..]) { - Ok(ex) => Ok(Some(ex)), - Err(err) => Err(sp_blockchain::Error::Backend( - format!("Error decoding extrinsic {}: {}", hash, err) - )), - } - }, - None => Ok(None), - } - } } impl sc_client_api::blockchain::HeaderBackend for BlockchainDb { @@ -532,7 +518,7 @@ impl sc_client_api::blockchain::Backend for BlockchainDb::decode(&mut &body[..]) { Ok(hashes) => { let extrinsics: ClientResult> = hashes.into_iter().map( - |h| self.extrinsic(&h) .and_then(|maybe_ex| maybe_ex.ok_or_else( + |h| self.extrinsic(&h).and_then(|maybe_ex| maybe_ex.ok_or_else( || sp_blockchain::Error::Backend( format!("Missing transaction: {}", h)))) ).collect(); @@ -576,6 +562,24 @@ impl sc_client_api::blockchain::Backend for BlockchainDb ClientResult> { children::read_children(&*self.db, columns::META, meta_keys::CHILDREN_PREFIX, parent_hash) } + + fn extrinsic(&self, hash: &Block::Hash) -> ClientResult> { + match self.db.get(columns::TRANSACTION, hash.as_ref()) { + Some(ex) => { + match Decode::decode(&mut &ex[..]) { + Ok(ex) => Ok(Some(ex)), + Err(err) => Err(sp_blockchain::Error::Backend( + format!("Error decoding extrinsic {}: {}", hash, err) + )), + } + }, + None => Ok(None), + } + } + + fn have_extrinsic(&self, hash: &Block::Hash) -> ClientResult { + Ok(self.db.contains(columns::TRANSACTION, hash.as_ref())) + } } impl sc_client_api::blockchain::ProvideCache for BlockchainDb { diff --git a/client/db/src/storage_cache.rs b/client/db/src/storage_cache.rs index 317c637333d64..2dde8d5058220 100644 --- a/client/db/src/storage_cache.rs +++ b/client/db/src/storage_cache.rs @@ -1471,50 +1471,46 @@ mod qc { } impl Arbitrary for Action { - fn arbitrary(gen: &mut G) -> Self { - let path = gen.next_u32() as u8; - let mut buf = [0u8; 32]; + fn arbitrary(gen: &mut quickcheck::Gen) -> Self { + let path = u8::arbitrary(gen); + let buf = (0..32).map(|_| u8::arbitrary(gen)).collect::>(); match path { 0..=175 => { - gen.fill_bytes(&mut buf[..]); Action::Next { - hash: H256::from(&buf), + hash: H256::from_slice(&buf[..]), changes: { let mut set = Vec::new(); - for _ in 0..gen.next_u32()/(64*256*256*256) { - set.push((vec![gen.next_u32() as u8], Some(vec![gen.next_u32() as u8]))); + for _ in 0..::arbitrary(gen)/(64*256*256*256) { + set.push((vec![u8::arbitrary(gen)], Some(vec![u8::arbitrary(gen)]))); } set } } }, 176..=220 => { - gen.fill_bytes(&mut buf[..]); Action::Fork { - hash: H256::from(&buf), - depth: ((gen.next_u32() as u8) / 32) as usize, + hash: H256::from_slice(&buf[..]), + depth: ((u8::arbitrary(gen)) / 32) as usize, changes: { let mut set = Vec::new(); - for _ in 0..gen.next_u32()/(64*256*256*256) { - set.push((vec![gen.next_u32() as u8], Some(vec![gen.next_u32() as u8]))); + for _ in 0..::arbitrary(gen)/(64*256*256*256) { + set.push((vec![u8::arbitrary(gen)], Some(vec![u8::arbitrary(gen)]))); } set } } }, 221..=240 => { - gen.fill_bytes(&mut buf[..]); Action::ReorgWithImport { - hash: H256::from(&buf), - depth: ((gen.next_u32() as u8) / 32) as usize, // 0-7 + hash: H256::from_slice(&buf[..]), + depth: ((u8::arbitrary(gen)) / 32) as usize, // 0-7 } }, _ => { - gen.fill_bytes(&mut buf[..]); Action::FinalizationReorg { - fork_depth: ((gen.next_u32() as u8) / 32) as usize, // 0-7 - depth: ((gen.next_u32() as u8) / 64) as usize, // 0-3 + fork_depth: ((u8::arbitrary(gen)) / 32) as usize, // 0-7 + depth: ((u8::arbitrary(gen)) / 64) as usize, // 0-3 } }, } diff --git a/client/db/src/utils.rs b/client/db/src/utils.rs index baea6aab69fa0..cd9b2a6f56d41 100644 --- a/client/db/src/utils.rs +++ b/client/db/src/utils.rs @@ -401,7 +401,13 @@ pub fn read_meta(db: &dyn Database, col_header: u32) -> Result< } { let hash = header.hash(); - debug!("DB Opened blockchain db, fetched {} = {:?} ({})", desc, hash, header.number()); + debug!( + target: "db", + "Opened blockchain db, fetched {} = {:?} ({})", + desc, + hash, + header.number() + ); Ok((hash, *header.number())) } else { Ok((genesis_hash.clone(), Zero::zero())) diff --git a/client/executor/Cargo.toml b/client/executor/Cargo.toml index 1d0b17369b3ef..f38806013c981 100644 --- a/client/executor/Cargo.toml +++ b/client/executor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-executor" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -15,24 +15,25 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] derive_more = "0.99.2" -codec = { package = "parity-scale-codec", version = "1.3.6" } -sp-io = { version = "2.0.0", path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-tasks = { version = "2.0.0", path = "../../primitives/tasks" } -sp-trie = { version = "2.0.0", path = "../../primitives/trie" } -sp-serializer = { version = "2.0.0", path = "../../primitives/serializer" } -sp-version = { version = "2.0.0", path = "../../primitives/version" } -sp-panic-handler = { version = "2.0.0", path = "../../primitives/panic-handler" } +codec = { package = "parity-scale-codec", version = "2.0.0" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-tasks = { version = "3.0.0", path = "../../primitives/tasks" } +sp-trie = { version = "3.0.0", path = "../../primitives/trie" } +sp-serializer = { version = "3.0.0", path = "../../primitives/serializer" } +sp-version = { version = "3.0.0", path = "../../primitives/version" } +sp-panic-handler = { version = "3.0.0", path = "../../primitives/panic-handler" } wasmi = "0.6.2" parity-wasm = "0.41.0" lazy_static = "1.4.0" -sp-api = { version = "2.0.0", path = "../../primitives/api" } -sp-wasm-interface = { version = "2.0.0", path = "../../primitives/wasm-interface" } -sp-runtime-interface = { version = "2.0.0", path = "../../primitives/runtime-interface" } -sp-externalities = { version = "0.8.0", path = "../../primitives/externalities" } -sc-executor-common = { version = "0.8.0", path = "common" } -sc-executor-wasmi = { version = "0.8.0", path = "wasmi" } -sc-executor-wasmtime = { version = "0.8.0", path = "wasmtime", optional = true } +sp-api = { version = "3.0.0", path = "../../primitives/api" } +sp-wasm-interface = { version = "3.0.0", path = "../../primitives/wasm-interface" } +sp-runtime-interface = { version = "3.0.0", path = "../../primitives/runtime-interface" } +sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" } +sc-executor-common = { version = "0.9.0", path = "common" } +sc-executor-wasmi = { version = "0.9.0", path = "wasmi" } +sc-executor-wasmtime = { version = "0.9.0", path = "wasmtime", optional = true } +sc-tracing = { version = "3.0.0", path = "../tracing" } parking_lot = "0.11.1" log = "0.4.8" libsecp256k1 = "0.3.4" @@ -43,14 +44,13 @@ wat = "1.0" hex-literal = "0.3.1" sc-runtime-test = { version = "2.0.0", path = "runtime-test" } substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" } -sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine" } +sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } test-case = "2.2.1" -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } -sc-tracing = { version = "2.0.0", path = "../tracing" } tracing = "0.1.22" tracing-subscriber = "0.2.15" -paste = "0.1.6" +paste = "1.0" [features] default = [ "std" ] diff --git a/client/executor/common/Cargo.toml b/client/executor/common/Cargo.toml index a479f4e1f4ddb..7e13e37d33fbe 100644 --- a/client/executor/common/Cargo.toml +++ b/client/executor/common/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-executor-common" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -16,12 +16,12 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] derive_more = "0.99.2" parity-wasm = "0.41.0" -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } wasmi = "0.6.2" -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-allocator = { version = "2.0.0", path = "../../../primitives/allocator" } -sp-wasm-interface = { version = "2.0.0", path = "../../../primitives/wasm-interface" } -sp-serializer = { version = "2.0.0", path = "../../../primitives/serializer" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-allocator = { version = "3.0.0", path = "../../../primitives/allocator" } +sp-wasm-interface = { version = "3.0.0", path = "../../../primitives/wasm-interface" } +sp-serializer = { version = "3.0.0", path = "../../../primitives/serializer" } thiserror = "1.0.21" [features] diff --git a/client/executor/common/src/error.rs b/client/executor/common/src/error.rs index 0af148fd95809..96329d1680301 100644 --- a/client/executor/common/src/error.rs +++ b/client/executor/common/src/error.rs @@ -37,8 +37,8 @@ pub enum Error { #[error(transparent)] Wasmi(#[from] wasmi::Error), - #[error("API Error: {0}")] - ApiError(String), + #[error("Error calling api function: {0}")] + ApiError(Box), #[error("Method not found: '{0}'")] MethodNotFound(String), @@ -96,16 +96,16 @@ pub enum Error { #[error(transparent)] RuntimeConstruction(#[from] WasmError), - + #[error("Shared memory is not supported")] SharedMemUnsupported, - + #[error("Imported globals are not supported yet")] ImportedGlobalsUnsupported, - + #[error("initializer expression can have only up to 2 expressions in wasm 1.0")] InitializerHasTooManyExpressions, - + #[error("Invalid initializer expression provided {0}")] InvalidInitializerExpression(String), } diff --git a/client/executor/runtime-test/Cargo.toml b/client/executor/runtime-test/Cargo.toml index 1a898b92ca9ab..93ad463be16c3 100644 --- a/client/executor/runtime-test/Cargo.toml +++ b/client/executor/runtime-test/Cargo.toml @@ -13,16 +13,16 @@ repository = "https://github.com/paritytech/substrate/" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-allocator = { version = "2.0.0", default-features = false, path = "../../../primitives/allocator" } -sp-core = { version = "2.0.0", default-features = false, path = "../../../primitives/core" } -sp-io = { version = "2.0.0", default-features = false, path = "../../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } -sp-sandbox = { version = "0.8.0", default-features = false, path = "../../../primitives/sandbox" } -sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } -sp-tasks = { version = "2.0.0", default-features = false, path = "../../../primitives/tasks" } +sp-allocator = { version = "3.0.0", default-features = false, path = "../../../primitives/allocator" } +sp-core = { version = "3.0.0", default-features = false, path = "../../../primitives/core" } +sp-io = { version = "3.0.0", default-features = false, path = "../../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../primitives/runtime" } +sp-sandbox = { version = "0.9.0", default-features = false, path = "../../../primitives/sandbox" } +sp-std = { version = "3.0.0", default-features = false, path = "../../../primitives/std" } +sp-tasks = { version = "3.0.0", default-features = false, path = "../../../primitives/tasks" } [build-dependencies] -substrate-wasm-builder = { version = "3.0.0", path = "../../../utils/wasm-builder" } +substrate-wasm-builder = { version = "4.0.0", path = "../../../utils/wasm-builder" } [features] default = [ "std" ] diff --git a/client/executor/src/integration_tests/mod.rs b/client/executor/src/integration_tests/mod.rs index 1f14678c7a4db..b28e3ca2436b6 100644 --- a/client/executor/src/integration_tests/mod.rs +++ b/client/executor/src/integration_tests/mod.rs @@ -75,6 +75,7 @@ fn call_in_wasm( Some(1024), HostFunctions::host_functions(), 8, + None, ); executor.call_in_wasm( &wasm_binary_unwrap()[..], @@ -536,6 +537,7 @@ fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) { Some(17), // `17` is the initial number of pages compiled into the binary. HostFunctions::host_functions(), 8, + None, ); let err = executor.call_in_wasm( @@ -558,6 +560,7 @@ fn returns_mutable_static(wasm_method: WasmExecutionMethod) { &wasm_binary_unwrap()[..], HostFunctions::host_functions(), true, + None, ).expect("Creates runtime"); let instance = runtime.new_instance().unwrap(); @@ -591,6 +594,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) { &wasm_binary_unwrap()[..], HostFunctions::host_functions(), true, + None, ).expect("Creates runtime"); let instance = runtime.new_instance().unwrap(); @@ -611,6 +615,7 @@ fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) { &wasm_binary_unwrap()[..], HostFunctions::host_functions(), true, + None, ).expect("Creates runtime"); let instance = runtime.new_instance().unwrap(); @@ -634,6 +639,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) { Some(1024), HostFunctions::host_functions(), 8, + None, )); let code_hash = blake2_256(wasm_binary_unwrap()).to_vec(); let threads: Vec<_> = (0..8).map(|_| diff --git a/client/executor/src/lib.rs b/client/executor/src/lib.rs index ccb7aa1b445b2..c30015a86b20e 100644 --- a/client/executor/src/lib.rs +++ b/client/executor/src/lib.rs @@ -80,6 +80,7 @@ mod tests { Some(8), sp_io::SubstrateHostFunctions::host_functions(), 8, + None, ); let res = executor.call_in_wasm( &wasm_binary_unwrap()[..], diff --git a/client/executor/src/native_executor.rs b/client/executor/src/native_executor.rs index 766dada331cd1..42a7950593ccf 100644 --- a/client/executor/src/native_executor.rs +++ b/client/executor/src/native_executor.rs @@ -26,6 +26,7 @@ use std::{ panic::{UnwindSafe, AssertUnwindSafe}, result, sync::{Arc, atomic::{AtomicU64, Ordering}, mpsc}, + path::PathBuf, }; use sp_version::{NativeVersion, RuntimeVersion}; @@ -102,6 +103,9 @@ pub struct WasmExecutor { cache: Arc, /// The size of the instances cache. max_runtime_instances: usize, + /// The path to a directory which the executor can leverage for a file cache, e.g. put there + /// compiled artifacts. + cache_path: Option, } impl WasmExecutor { @@ -112,19 +116,30 @@ impl WasmExecutor { /// `method` - Method used to execute Wasm code. /// /// `default_heap_pages` - Number of 64KB pages to allocate for Wasm execution. - /// Defaults to `DEFAULT_HEAP_PAGES` if `None` is provided. + /// Defaults to `DEFAULT_HEAP_PAGES` if `None` is provided. + /// + /// `host_functions` - The set of host functions to be available for import provided by this + /// executor. + /// + /// `max_runtime_instances` - The number of runtime instances to keep in memory ready for reuse. + /// + /// `cache_path` - A path to a directory where the executor can place its files for purposes of + /// caching. This may be important in cases when there are many different modules with the + /// compiled execution method is used. pub fn new( method: WasmExecutionMethod, default_heap_pages: Option, host_functions: Vec<&'static dyn Function>, max_runtime_instances: usize, + cache_path: Option, ) -> Self { WasmExecutor { method, default_heap_pages: default_heap_pages.unwrap_or(DEFAULT_HEAP_PAGES), host_functions: Arc::new(host_functions), - cache: Arc::new(RuntimeCache::new(max_runtime_instances)), + cache: Arc::new(RuntimeCache::new(max_runtime_instances, cache_path.clone())), max_runtime_instances, + cache_path, } } @@ -210,6 +225,7 @@ impl sp_core::traits::CallInWasm for WasmExecutor { &wasm_code, self.host_functions.to_vec(), allow_missing_host_functions, + self.cache_path.as_deref(), ) .map_err(|e| format!("Failed to create module: {:?}", e))?; @@ -267,6 +283,7 @@ impl NativeExecutor { default_heap_pages, host_functions, max_runtime_instances, + None, ); NativeExecutor { @@ -439,7 +456,7 @@ impl CodeExecutor for NativeExecutor { fn call< R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result> + UnwindSafe, >( &self, ext: &mut dyn Externalities, @@ -497,7 +514,7 @@ impl CodeExecutor for NativeExecutor { let res = with_externalities_safe(&mut **ext, move || (call)()) .and_then(|r| r .map(NativeOrEncoded::Native) - .map_err(|s| Error::ApiError(s)) + .map_err(Error::ApiError) ); Ok(res) diff --git a/client/executor/src/wasm_runtime.rs b/client/executor/src/wasm_runtime.rs index a7d8b0ce2387e..351a2b5f40f00 100644 --- a/client/executor/src/wasm_runtime.rs +++ b/client/executor/src/wasm_runtime.rs @@ -28,6 +28,7 @@ use codec::Decode; use sp_core::traits::{Externalities, RuntimeCode, FetchRuntimeCode}; use sp_version::RuntimeVersion; use std::panic::AssertUnwindSafe; +use std::path::{Path, PathBuf}; use sc_executor_common::wasm_runtime::{WasmModule, WasmInstance}; use sp_wasm_interface::Function; @@ -152,14 +153,22 @@ pub struct RuntimeCache { runtimes: Mutex<[Option>; MAX_RUNTIMES]>, /// The size of the instances cache for each runtime. max_runtime_instances: usize, + cache_path: Option, } impl RuntimeCache { /// Creates a new instance of a runtimes cache. - pub fn new(max_runtime_instances: usize) -> RuntimeCache { + /// + /// `max_runtime_instances` specifies the number of runtime instances preserved in an in-memory + /// cache. + /// + /// `cache_path` allows to specify an optional directory where the executor can store files + /// for caching. + pub fn new(max_runtime_instances: usize, cache_path: Option) -> RuntimeCache { RuntimeCache { runtimes: Default::default(), max_runtime_instances, + cache_path, } } @@ -235,6 +244,7 @@ impl RuntimeCache { host_functions.into(), allow_missing_func_imports, self.max_runtime_instances, + self.cache_path.as_deref(), ); if let Err(ref err) = result { log::warn!(target: "wasm-runtime", "Cannot create a runtime: {:?}", err); @@ -271,22 +281,32 @@ pub fn create_wasm_runtime_with_code( code: &[u8], host_functions: Vec<&'static dyn Function>, allow_missing_func_imports: bool, + cache_path: Option<&Path>, ) -> Result, WasmError> { match wasm_method { - WasmExecutionMethod::Interpreted => + WasmExecutionMethod::Interpreted => { + // Wasmi doesn't have any need in a cache directory. + // + // We drop the cache_path here to silence warnings that cache_path is not used if compiling + // without the `wasmtime` flag. + drop(cache_path); + sc_executor_wasmi::create_runtime( code, heap_pages, host_functions, - allow_missing_func_imports - ).map(|runtime| -> Arc { Arc::new(runtime) }), + allow_missing_func_imports, + ) + .map(|runtime| -> Arc { Arc::new(runtime) }) + } #[cfg(feature = "wasmtime")] WasmExecutionMethod::Compiled => sc_executor_wasmtime::create_runtime( code, heap_pages, host_functions, - allow_missing_func_imports + allow_missing_func_imports, + cache_path, ).map(|runtime| -> Arc { Arc::new(runtime) }), } } @@ -319,6 +339,7 @@ fn create_versioned_wasm_runtime( host_functions: Vec<&'static dyn Function>, allow_missing_func_imports: bool, max_instances: usize, + cache_path: Option<&Path>, ) -> Result { #[cfg(not(target_os = "unknown"))] let time = std::time::Instant::now(); @@ -328,6 +349,7 @@ fn create_versioned_wasm_runtime( &code, host_functions, allow_missing_func_imports, + cache_path, )?; // Call to determine runtime version. @@ -392,7 +414,7 @@ mod tests { authoring_version: 1, spec_version: 1, impl_version: 1, - apis: sp_api::create_apis_vec!([(Core::::ID, 1)]), + apis: sp_api::create_apis_vec!([(Core::::ID, 1)]), }; let version = decode_version(&old_runtime_version.encode()).unwrap(); @@ -407,7 +429,7 @@ mod tests { authoring_version: 1, spec_version: 1, impl_version: 1, - apis: sp_api::create_apis_vec!([(Core::::ID, 3)]), + apis: sp_api::create_apis_vec!([(Core::::ID, 3)]), }; decode_version(&old_runtime_version.encode()).unwrap_err(); @@ -421,7 +443,7 @@ mod tests { authoring_version: 1, spec_version: 1, impl_version: 1, - apis: sp_api::create_apis_vec!([(Core::::ID, 3)]), + apis: sp_api::create_apis_vec!([(Core::::ID, 3)]), transaction_version: 3, }; diff --git a/client/executor/wasmi/Cargo.toml b/client/executor/wasmi/Cargo.toml index 38d1cf3072a1e..cfe9dd7108cf2 100644 --- a/client/executor/wasmi/Cargo.toml +++ b/client/executor/wasmi/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-executor-wasmi" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -16,9 +16,9 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] log = "0.4.8" wasmi = "0.6.2" -codec = { package = "parity-scale-codec", version = "1.3.6" } -sc-executor-common = { version = "0.8.0", path = "../common" } -sp-wasm-interface = { version = "2.0.0", path = "../../../primitives/wasm-interface" } -sp-runtime-interface = { version = "2.0.0", path = "../../../primitives/runtime-interface" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-allocator = { version = "2.0.0", path = "../../../primitives/allocator" } +codec = { package = "parity-scale-codec", version = "2.0.0" } +sc-executor-common = { version = "0.9.0", path = "../common" } +sp-wasm-interface = { version = "3.0.0", path = "../../../primitives/wasm-interface" } +sp-runtime-interface = { version = "3.0.0", path = "../../../primitives/runtime-interface" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-allocator = { version = "3.0.0", path = "../../../primitives/allocator" } diff --git a/client/executor/wasmtime/Cargo.toml b/client/executor/wasmtime/Cargo.toml index 071cbc66001d6..051b314e4498a 100644 --- a/client/executor/wasmtime/Cargo.toml +++ b/client/executor/wasmtime/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-executor-wasmtime" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -16,13 +16,13 @@ targets = ["x86_64-unknown-linux-gnu"] log = "0.4.8" scoped-tls = "1.0" parity-wasm = "0.41.0" -codec = { package = "parity-scale-codec", version = "1.3.6" } -sc-executor-common = { version = "0.8.0", path = "../common" } -sp-wasm-interface = { version = "2.0.0", path = "../../../primitives/wasm-interface" } -sp-runtime-interface = { version = "2.0.0", path = "../../../primitives/runtime-interface" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-allocator = { version = "2.0.0", path = "../../../primitives/allocator" } -wasmtime = "0.19" +codec = { package = "parity-scale-codec", version = "2.0.0" } +sc-executor-common = { version = "0.9.0", path = "../common" } +sp-wasm-interface = { version = "3.0.0", path = "../../../primitives/wasm-interface" } +sp-runtime-interface = { version = "3.0.0", path = "../../../primitives/runtime-interface" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-allocator = { version = "3.0.0", path = "../../../primitives/allocator" } +wasmtime = "0.22" pwasm-utils = "0.14.0" [dev-dependencies] diff --git a/client/executor/wasmtime/src/imports.rs b/client/executor/wasmtime/src/imports.rs index b5eaeae5e66cd..08cedd434e366 100644 --- a/client/executor/wasmtime/src/imports.rs +++ b/client/executor/wasmtime/src/imports.rs @@ -44,15 +44,17 @@ pub fn resolve_imports( let mut externs = vec![]; let mut memory_import_index = None; for import_ty in module.imports() { + let name = import_name(&import_ty)?; + if import_ty.module() != "env" { return Err(WasmError::Other(format!( "host doesn't provide any imports from non-env module: {}:{}", import_ty.module(), - import_ty.name() + name, ))); } - let resolved = match import_ty.name() { + let resolved = match name { "memory" => { memory_import_index = Some(externs.len()); resolve_memory_import(store, &import_ty, heap_pages)? @@ -72,6 +74,16 @@ pub fn resolve_imports( }) } +/// When the module linking proposal is supported the import's name can be `None`. +/// Because we are not using this proposal we could safely unwrap the name. +/// However, we opt for an error in order to avoid panics at all costs. +fn import_name<'a, 'b: 'a>(import: &'a ImportType<'b>) -> Result<&'a str, WasmError> { + let name = import.name().ok_or_else(|| + WasmError::Other("The module linking proposal is not supported.".to_owned()) + )?; + Ok(name) +} + fn resolve_memory_import( store: &Store, import_ty: &ImportType, @@ -83,7 +95,7 @@ fn resolve_memory_import( return Err(WasmError::Other(format!( "this import must be of memory type: {}:{}", import_ty.module(), - import_ty.name() + import_name(&import_ty)?, ))) } }; @@ -116,49 +128,46 @@ fn resolve_func_import( host_functions: &[&'static dyn Function], allow_missing_func_imports: bool, ) -> Result { + let name = import_name(&import_ty)?; + let func_ty = match import_ty.ty() { ExternType::Func(func_ty) => func_ty, _ => { return Err(WasmError::Other(format!( "host doesn't provide any non function imports besides 'memory': {}:{}", import_ty.module(), - import_ty.name() + name, ))); } }; let host_func = match host_functions .iter() - .find(|host_func| host_func.name() == import_ty.name()) + .find(|host_func| host_func.name() == name) { Some(host_func) => host_func, None if allow_missing_func_imports => { - return Ok(MissingHostFuncHandler::new(import_ty).into_extern(store, &func_ty)); + return Ok(MissingHostFuncHandler::new(import_ty)?.into_extern(store, &func_ty)); } None => { return Err(WasmError::Other(format!( "host doesn't provide such function: {}:{}", import_ty.module(), - import_ty.name() + name, ))); } }; - if !signature_matches(&func_ty, &wasmtime_func_sig(*host_func)) { + if &func_ty != &wasmtime_func_sig(*host_func) { return Err(WasmError::Other(format!( "signature mismatch for: {}:{}", import_ty.module(), - import_ty.name() + name, ))); } Ok(HostFuncHandler::new(*host_func).into_extern(store)) } -/// Returns `true` if `lhs` and `rhs` represent the same signature. -fn signature_matches(lhs: &wasmtime::FuncType, rhs: &wasmtime::FuncType) -> bool { - lhs.params() == rhs.params() && lhs.results() == rhs.results() -} - /// This structure implements `Callable` and acts as a bridge between wasmtime and /// substrate host functions. struct HostFuncHandler { @@ -243,11 +252,11 @@ struct MissingHostFuncHandler { } impl MissingHostFuncHandler { - fn new(import_ty: &ImportType) -> Self { - Self { + fn new(import_ty: &ImportType) -> Result { + Ok(Self { module: import_ty.module().to_string(), - name: import_ty.name().to_string(), - } + name: import_name(import_ty)?.to_string(), + }) } fn into_extern(self, store: &Store, func_ty: &FuncType) -> Extern { @@ -263,22 +272,17 @@ impl MissingHostFuncHandler { } fn wasmtime_func_sig(func: &dyn Function) -> wasmtime::FuncType { - let params = func - .signature() + let signature = func.signature(); + let params = signature .args .iter() .cloned() - .map(into_wasmtime_val_type) - .collect::>() - .into_boxed_slice(); - let results = func - .signature() + .map(into_wasmtime_val_type); + let results = signature .return_value .iter() .cloned() - .map(into_wasmtime_val_type) - .collect::>() - .into_boxed_slice(); + .map(into_wasmtime_val_type); wasmtime::FuncType::new(params, results) } diff --git a/client/executor/wasmtime/src/instance_wrapper.rs b/client/executor/wasmtime/src/instance_wrapper.rs index 2103ab9b7b98c..f0543a7ef9506 100644 --- a/client/executor/wasmtime/src/instance_wrapper.rs +++ b/client/executor/wasmtime/src/instance_wrapper.rs @@ -113,7 +113,7 @@ impl EntryPoint { ]) }, }) - .map(|results| + .map(|results| // the signature is checked to have i64 return type results[0].unwrap_i64() as u64 ) @@ -124,27 +124,28 @@ impl EntryPoint { } pub fn direct(func: wasmtime::Func) -> std::result::Result { - match (func.ty().params(), func.ty().results()) { - (&[wasmtime::ValType::I32, wasmtime::ValType::I32], &[wasmtime::ValType::I64]) => { - Ok(Self { func, call_type: EntryPointType::Direct }) - } - _ => { - Err("Invalid signature for direct entry point") - } + use wasmtime::ValType; + let entry_point = wasmtime::FuncType::new( + [ValType::I32, ValType::I32].iter().cloned(), + [ValType::I64].iter().cloned(), + ); + if func.ty() == entry_point { + Ok(Self { func, call_type: EntryPointType::Direct }) + } else { + Err("Invalid signature for direct entry point") } } pub fn wrapped(dispatcher: wasmtime::Func, func: u32) -> std::result::Result { - match (dispatcher.ty().params(), dispatcher.ty().results()) { - ( - &[wasmtime::ValType::I32, wasmtime::ValType::I32, wasmtime::ValType::I32], - &[wasmtime::ValType::I64], - ) => { - Ok(Self { func: dispatcher, call_type: EntryPointType::Wrapped(func) }) - }, - _ => { - Err("Invalid signature for wrapped entry point") - } + use wasmtime::ValType; + let entry_point = wasmtime::FuncType::new( + [ValType::I32, ValType::I32, ValType::I32].iter().cloned(), + [ValType::I64].iter().cloned(), + ); + if dispatcher.ty() == entry_point { + Ok(Self { func: dispatcher, call_type: EntryPointType::Wrapped(func) }) + } else { + Err("Invalid signature for wrapped entry point") } } } diff --git a/client/executor/wasmtime/src/runtime.rs b/client/executor/wasmtime/src/runtime.rs index a17a034918db7..64ad5a1f4e49f 100644 --- a/client/executor/wasmtime/src/runtime.rs +++ b/client/executor/wasmtime/src/runtime.rs @@ -25,6 +25,7 @@ use crate::state_holder; use std::rc::Rc; use std::sync::Arc; +use std::path::Path; use sc_executor_common::{ error::{Result, WasmError}, wasm_runtime::{WasmModule, WasmInstance, InvokeMethod}, @@ -119,20 +120,68 @@ impl WasmInstance for WasmtimeInstance { } } +/// Prepare a directory structure and a config file to enable wasmtime caching. +/// +/// In case of an error the caching will not be enabled. +fn setup_wasmtime_caching( + cache_path: &Path, + config: &mut Config, +) -> std::result::Result<(), String> { + use std::fs; + + let wasmtime_cache_root = cache_path.join("wasmtime"); + fs::create_dir_all(&wasmtime_cache_root) + .map_err(|err| format!("cannot create the dirs to cache: {:?}", err))?; + + // Canonicalize the path after creating the directories. + let wasmtime_cache_root = wasmtime_cache_root + .canonicalize() + .map_err(|err| format!("failed to canonicalize the path: {:?}", err))?; + + // Write the cache config file + let cache_config_path = wasmtime_cache_root.join("cache-config.toml"); + let config_content = format!( + "\ +[cache] +enabled = true +directory = \"{cache_dir}\" +", + cache_dir = wasmtime_cache_root.display() + ); + fs::write(&cache_config_path, config_content) + .map_err(|err| format!("cannot write the cache config: {:?}", err))?; + + config + .cache_config_load(cache_config_path) + .map_err(|err| format!("failed to parse the config: {:?}", err))?; + + Ok(()) +} + /// Create a new `WasmtimeRuntime` given the code. This function performs translation from Wasm to /// machine code, which can be computationally heavy. +/// +/// The `cache_path` designates where this executor implementation can put compiled artifacts. pub fn create_runtime( code: &[u8], heap_pages: u64, host_functions: Vec<&'static dyn Function>, allow_missing_func_imports: bool, + cache_path: Option<&Path>, ) -> std::result::Result { // Create the engine, store and finally the module from the given code. let mut config = Config::new(); config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); + if let Some(cache_path) = cache_path { + if let Err(reason) = setup_wasmtime_caching(cache_path, &mut config) { + log::warn!( + "failed to setup wasmtime cache. Performance may degrade significantly: {}.", + reason, + ); + } + } let engine = Engine::new(&config); - let module_wrapper = ModuleWrapper::new(&engine, code) .map_err(|e| WasmError::Other(format!("cannot create module: {}", e)))?; diff --git a/client/finality-grandpa-warp-sync/Cargo.toml b/client/finality-grandpa-warp-sync/Cargo.toml index 4f7ee0301f417..740c85940e77d 100644 --- a/client/finality-grandpa-warp-sync/Cargo.toml +++ b/client/finality-grandpa-warp-sync/Cargo.toml @@ -1,11 +1,10 @@ [package] description = "A request-response protocol for handling grandpa warp sync requests" name = "sc-finality-grandpa-warp-sync" -version = "0.8.0" +version = "0.9.0" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" authors = ["Parity Technologies "] edition = "2018" -publish = false homepage = "https://substrate.dev" repository = "https://github.com/paritytech/substrate/" @@ -13,16 +12,16 @@ repository = "https://github.com/paritytech/substrate/" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sc-network = { version = "0.8.0", path = "../network" } -sc-finality-grandpa = { version = "0.8.0", path = "../finality-grandpa" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sc-client-api = { version = "2.0.0", path = "../api" } -sc-service = { version = "0.8.0", path = "../service" } +sc-network = { version = "0.9.0", path = "../network" } +sc-finality-grandpa = { version = "0.9.0", path = "../finality-grandpa" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sc-client-api = { version = "3.0.0", path = "../api" } +sc-service = { version = "0.9.0", path = "../service" } futures = "0.3.8" log = "0.4.11" derive_more = "0.99.11" -codec = { package = "parity-scale-codec", version = "1.3.5" } -prost = "0.6.1" +codec = { package = "parity-scale-codec", version = "2.0.0" } +prost = "0.7" num-traits = "0.2.14" parking_lot = "0.11.1" diff --git a/client/finality-grandpa-warp-sync/src/lib.rs b/client/finality-grandpa-warp-sync/src/lib.rs index cae28173f09ee..e14bcfdd4f323 100644 --- a/client/finality-grandpa-warp-sync/src/lib.rs +++ b/client/finality-grandpa-warp-sync/src/lib.rs @@ -14,8 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . -//! Helper for handling (i.e. answering) grandpa warp sync requests from a remote peer via the -//! [`crate::request_responses::RequestResponsesBehaviour`]. +//! Helper for handling (i.e. answering) grandpa warp sync requests from a remote peer. use codec::Decode; use sc_network::config::{IncomingRequest, OutgoingResponse, ProtocolId, RequestResponseConfig}; @@ -86,9 +85,9 @@ struct Request { const WARP_SYNC_FRAGMENTS_LIMIT: usize = 100; /// Number of item with justification in warp sync cache. -/// This should be customizable, setting a low number -/// until then. -const WARP_SYNC_CACHE_SIZE: usize = 20; +/// This should be customizable, but setting it to the max number of fragments +/// we return seems like a good idea until then. +const WARP_SYNC_CACHE_SIZE: usize = WARP_SYNC_FRAGMENTS_LIMIT; /// Handler for incoming grandpa warp sync requests from a remote peer. pub struct GrandpaWarpSyncRequestHandler { diff --git a/client/finality-grandpa/Cargo.toml b/client/finality-grandpa/Cargo.toml index 1b410b32013a3..7ae5666c7bc84 100644 --- a/client/finality-grandpa/Cargo.toml +++ b/client/finality-grandpa/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-finality-grandpa" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -16,47 +16,47 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] derive_more = "0.99.2" -fork-tree = { version = "2.0.0", path = "../../utils/fork-tree" } +dyn-clone = "1.0" +fork-tree = { version = "3.0.0", path = "../../utils/fork-tree" } futures = "0.3.9" futures-timer = "3.0.1" log = "0.4.8" parking_lot = "0.11.1" rand = "0.7.2" -parity-scale-codec = { version = "1.3.6", features = ["derive"] } -sp-application-crypto = { version = "2.0.0", path = "../../primitives/application-crypto" } -sp-arithmetic = { version = "2.0.0", path = "../../primitives/arithmetic" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-utils = { version = "2.0.0", path = "../../primitives/utils" } -sp-consensus = { version = "0.8.0", path = "../../primitives/consensus/common" } -sc-consensus = { version = "0.8.0", path = "../consensus/common" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-keystore = { version = "0.8.0", path = "../../primitives/keystore" } -sp-api = { version = "2.0.0", path = "../../primitives/api" } -sc-telemetry = { version = "2.0.0", path = "../telemetry" } -sc-keystore = { version = "2.0.0", path = "../keystore" } +parity-scale-codec = { version = "2.0.0", features = ["derive"] } +sp-application-crypto = { version = "3.0.0", path = "../../primitives/application-crypto" } +sp-arithmetic = { version = "3.0.0", path = "../../primitives/arithmetic" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-utils = { version = "3.0.0", path = "../../primitives/utils" } +sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sc-consensus = { version = "0.9.0", path = "../consensus/common" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-keystore = { version = "0.9.0", path = "../../primitives/keystore" } +sp-api = { version = "3.0.0", path = "../../primitives/api" } +sc-telemetry = { version = "3.0.0", path = "../telemetry" } +sc-keystore = { version = "3.0.0", path = "../keystore" } serde_json = "1.0.41" -sc-client-api = { version = "2.0.0", path = "../api" } -sp-inherents = { version = "2.0.0", path = "../../primitives/inherents" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sc-network = { version = "0.8.0", path = "../network" } -sc-network-gossip = { version = "0.8.0", path = "../network-gossip" } -sp-finality-grandpa = { version = "2.0.0", path = "../../primitives/finality-grandpa" } -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.8.0"} -sc-block-builder = { version = "0.8.0", path = "../block-builder" } -finality-grandpa = { version = "0.12.3", features = ["derive-codec"] } -pin-project = "0.4.6" +sc-client-api = { version = "3.0.0", path = "../api" } +sp-inherents = { version = "3.0.0", path = "../../primitives/inherents" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sc-network = { version = "0.9.0", path = "../network" } +sc-network-gossip = { version = "0.9.0", path = "../network-gossip" } +sp-finality-grandpa = { version = "3.0.0", path = "../../primitives/finality-grandpa" } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.9.0"} +sc-block-builder = { version = "0.9.0", path = "../block-builder" } +finality-grandpa = { version = "0.14.0", features = ["derive-codec"] } +pin-project = "1.0.4" linked-hash-map = "0.5.2" [dev-dependencies] assert_matches = "1.3.0" -finality-grandpa = { version = "0.12.3", features = ["derive-codec", "test-helpers"] } -sc-network = { version = "0.8.0", path = "../network" } +finality-grandpa = { version = "0.14.0", features = ["derive-codec", "test-helpers"] } +sc-network = { version = "0.9.0", path = "../network" } sc-network-test = { version = "0.8.0", path = "../network/test" } -sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" } +sp-keyring = { version = "3.0.0", path = "../../primitives/keyring" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } -sp-consensus-babe = { version = "0.8.0", path = "../../primitives/consensus/babe" } -sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine" } -sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } +sp-consensus-babe = { version = "0.9.0", path = "../../primitives/consensus/babe" } +sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" } +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } tokio = { version = "0.2", features = ["rt-core"] } tempfile = "3.1.0" -sp-api = { version = "2.0.0", path = "../../primitives/api" } diff --git a/client/finality-grandpa/rpc/Cargo.toml b/client/finality-grandpa/rpc/Cargo.toml index 52f6b094a8dd9..ff5b4cafdae77 100644 --- a/client/finality-grandpa/rpc/Cargo.toml +++ b/client/finality-grandpa/rpc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-finality-grandpa-rpc" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "RPC extensions for the GRANDPA finality gadget" repository = "https://github.com/paritytech/substrate/" @@ -9,12 +9,12 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" readme = "README.md" [dependencies] -sc-finality-grandpa = { version = "0.8.0", path = "../" } -sc-rpc = { version = "2.0.0", path = "../../rpc" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -finality-grandpa = { version = "0.12.3", features = ["derive-codec"] } +sc-finality-grandpa = { version = "0.9.0", path = "../" } +sc-rpc = { version = "3.0.0", path = "../../rpc" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +finality-grandpa = { version = "0.14.0", features = ["derive-codec"] } jsonrpc-core = "15.1.0" jsonrpc-core-client = "15.1.0" jsonrpc-derive = "15.1.0" @@ -24,16 +24,16 @@ serde = { version = "1.0.105", features = ["derive"] } serde_json = "1.0.50" log = "0.4.8" derive_more = "0.99.2" -parity-scale-codec = { version = "1.3.6", features = ["derive"] } -sc-client-api = { version = "2.0.0", path = "../../api" } +parity-scale-codec = { version = "2.0.0", features = ["derive"] } +sc-client-api = { version = "3.0.0", path = "../../api" } [dev-dependencies] -sc-block-builder = { version = "0.8.0", path = "../../block-builder" } +sc-block-builder = { version = "0.9.0", path = "../../block-builder" } sc-network-test = { version = "0.8.0", path = "../../network/test" } -sc-rpc = { version = "2.0.0", path = "../../rpc", features = ["test-helpers"] } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-finality-grandpa = { version = "2.0.0", path = "../../../primitives/finality-grandpa" } -sp-keyring = { version = "2.0.0", path = "../../../primitives/keyring" } +sc-rpc = { version = "3.0.0", path = "../../rpc", features = ["test-helpers"] } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-finality-grandpa = { version = "3.0.0", path = "../../../primitives/finality-grandpa" } +sp-keyring = { version = "3.0.0", path = "../../../primitives/keyring" } substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" } lazy_static = "1.4" diff --git a/client/finality-grandpa/src/aux_schema.rs b/client/finality-grandpa/src/aux_schema.rs index a5092334b99f5..1ce3c7999f24c 100644 --- a/client/finality-grandpa/src/aux_schema.rs +++ b/client/finality-grandpa/src/aux_schema.rs @@ -143,7 +143,7 @@ pub(crate) fn load_decode( match backend.get_aux(key)? { None => Ok(None), Some(t) => T::decode(&mut &t[..]) - .map_err(|e| ClientError::Backend(format!("GRANDPA DB is corrupted: {}", e.what()))) + .map_err(|e| ClientError::Backend(format!("GRANDPA DB is corrupted: {}", e))) .map(Some) } } diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index c217218aecc4f..9f5582e5cea6b 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -370,7 +370,7 @@ pub(super) struct NeighborPacket { /// A versioned neighbor packet. #[derive(Debug, Encode, Decode)] pub(super) enum VersionedNeighborPacket { - #[codec(index = "1")] + #[codec(index = 1)] V1(NeighborPacket), } @@ -563,12 +563,10 @@ impl Peers { } fn authorities(&self) -> usize { - // Note that our sentry and our validator are neither authorities nor non-authorities. self.inner.iter().filter(|(_, info)| matches!(info.roles, ObservedRole::Authority)).count() } fn non_authorities(&self) -> usize { - // Note that our sentry and our validator are neither authorities nor non-authorities. self.inner .iter() .filter(|(_, info)| matches!(info.roles, ObservedRole::Full | ObservedRole::Light)) @@ -665,8 +663,7 @@ impl CatchUpConfig { match self { CatchUpConfig::Disabled => false, CatchUpConfig::Enabled { only_from_authorities, .. } => match peer.roles { - ObservedRole::Authority | ObservedRole::OurSentry | - ObservedRole::OurGuardedAuthority => true, + ObservedRole::Authority => true, _ => !only_from_authorities } } @@ -1158,7 +1155,6 @@ impl Inner { } match peer.roles { - ObservedRole::OurGuardedAuthority | ObservedRole::OurSentry => true, ObservedRole::Authority => { let authorities = self.peers.authorities(); @@ -1214,7 +1210,6 @@ impl Inner { }; match peer.roles { - ObservedRole::OurSentry | ObservedRole::OurGuardedAuthority => true, ObservedRole::Authority => { let authorities = self.peers.authorities(); @@ -1415,7 +1410,7 @@ impl GossipValidator { } Err(e) => { message_name = None; - debug!(target: "afg", "Error decoding message: {}", e.what()); + debug!(target: "afg", "Error decoding message: {}", e); telemetry!(CONSENSUS_DEBUG; "afg.err_decoding_msg"; "" => ""); let len = std::cmp::min(i32::max_value() as usize, data.len()) as i32; diff --git a/client/finality-grandpa/src/environment.rs b/client/finality-grandpa/src/environment.rs index 5e4203b2a40f6..7925a674c2983 100644 --- a/client/finality-grandpa/src/environment.rs +++ b/client/finality-grandpa/src/environment.rs @@ -480,7 +480,7 @@ where Block: BlockT, BE: Backend, C: crate::ClientForGrandpa, - C::Api: GrandpaApi, + C::Api: GrandpaApi, N: NetworkT, SC: SelectChain + 'static, { @@ -549,7 +549,7 @@ where authority_set.set_id, equivocation.offender().clone(), ) - .map_err(Error::Client)? + .map_err(Error::RuntimeApi)? { Some(proof) => proof, None => { @@ -571,7 +571,7 @@ where equivocation_proof, key_owner_proof, ) - .map_err(Error::Client)?; + .map_err(Error::RuntimeApi)?; Ok(()) } @@ -592,100 +592,6 @@ where fn ancestry(&self, base: Block::Hash, block: Block::Hash) -> Result, GrandpaError> { ancestry(&self.client, base, block) } - - fn best_chain_containing(&self, block: Block::Hash) -> Option<(Block::Hash, NumberFor)> { - // NOTE: when we finalize an authority set change through the sync protocol the voter is - // signaled asynchronously. therefore the voter could still vote in the next round - // before activating the new set. the `authority_set` is updated immediately thus we - // restrict the voter based on that. - if self.set_id != self.authority_set.set_id() { - return None; - } - - let base_header = match self.client.header(BlockId::Hash(block)).ok()? { - Some(h) => h, - None => { - debug!(target: "afg", "Encountered error finding best chain containing {:?}: couldn't find base block", block); - return None; - } - }; - - // we refuse to vote beyond the current limit number where transitions are scheduled to - // occur. - // once blocks are finalized that make that transition irrelevant or activate it, - // we will proceed onwards. most of the time there will be no pending transition. - // the limit, if any, is guaranteed to be higher than or equal to the given base number. - let limit = self.authority_set.current_limit(*base_header.number()); - debug!(target: "afg", "Finding best chain containing block {:?} with number limit {:?}", block, limit); - - match self.select_chain.finality_target(block, None) { - Ok(Some(best_hash)) => { - let best_header = self.client.header(BlockId::Hash(best_hash)).ok()? - .expect("Header known to exist after `finality_target` call; qed"); - - // check if our vote is currently being limited due to a pending change - let limit = limit.filter(|limit| limit < best_header.number()); - let target; - - let target_header = if let Some(target_number) = limit { - let mut target_header = best_header.clone(); - - // walk backwards until we find the target block - loop { - if *target_header.number() < target_number { - unreachable!( - "we are traversing backwards from a known block; \ - blocks are stored contiguously; \ - qed" - ); - } - - if *target_header.number() == target_number { - break; - } - - target_header = self.client.header(BlockId::Hash(*target_header.parent_hash())).ok()? - .expect("Header known to exist after `finality_target` call; qed"); - } - - target = target_header; - &target - } else { - // otherwise just use the given best as the target - &best_header - }; - - // restrict vote according to the given voting rule, if the - // voting rule doesn't restrict the vote then we keep the - // previous target. - // - // note that we pass the original `best_header`, i.e. before the - // authority set limit filter, which can be considered a - // mandatory/implicit voting rule. - // - // we also make sure that the restricted vote is higher than the - // round base (i.e. last finalized), otherwise the value - // returned by the given voting rule is ignored and the original - // target is used instead. - self.voting_rule - .restrict_vote(&*self.client, &base_header, &best_header, target_header) - .filter(|(_, restricted_number)| { - // we can only restrict votes within the interval [base, target] - restricted_number >= base_header.number() && - restricted_number < target_header.number() - }) - .or_else(|| Some((target_header.hash(), *target_header.number()))) - }, - Ok(None) => { - debug!(target: "afg", "Encountered error finding best chain containing {:?}: couldn't find target block", block); - None - } - Err(e) => { - debug!(target: "afg", "Encountered error finding best chain containing {:?}: {:?}", block, e); - None - } - } - } } @@ -726,13 +632,21 @@ where Block: 'static, B: Backend, C: crate::ClientForGrandpa + 'static, - C::Api: GrandpaApi, + C::Api: GrandpaApi, N: NetworkT + 'static + Send + Sync, SC: SelectChain + 'static, VR: VotingRule, NumberFor: BlockNumberOps, { type Timer = Pin> + Send + Sync>>; + type BestChain = Pin< + Box< + dyn Future)>, Self::Error>> + + Send + + Sync + >, + >; + type Id = AuthorityId; type Signature = AuthoritySignature; @@ -747,6 +661,119 @@ where type Error = CommandOrError>; + fn best_chain_containing(&self, block: Block::Hash) -> Self::BestChain { + let find_best_chain = || { + // NOTE: when we finalize an authority set change through the sync protocol the voter is + // signaled asynchronously. therefore the voter could still vote in the next round + // before activating the new set. the `authority_set` is updated immediately thus we + // restrict the voter based on that. + if self.set_id != self.authority_set.set_id() { + return None; + } + + let base_header = match self.client.header(BlockId::Hash(block)).ok()? { + Some(h) => h, + None => { + debug!(target: "afg", "Encountered error finding best chain containing {:?}: couldn't find base block", block); + return None; + } + }; + + // we refuse to vote beyond the current limit number where transitions are scheduled to + // occur. + // once blocks are finalized that make that transition irrelevant or activate it, + // we will proceed onwards. most of the time there will be no pending transition. + // the limit, if any, is guaranteed to be higher than or equal to the given base number. + let limit = self.authority_set.current_limit(*base_header.number()); + debug!(target: "afg", "Finding best chain containing block {:?} with number limit {:?}", block, limit); + + match self.select_chain.finality_target(block, None) { + Ok(Some(best_hash)) => { + let best_header = self + .client + .header(BlockId::Hash(best_hash)) + .ok()? + .expect("Header known to exist after `finality_target` call; qed"); + + // check if our vote is currently being limited due to a pending change + let limit = limit.filter(|limit| limit < best_header.number()); + + if let Some(target_number) = limit { + let mut target_header = best_header.clone(); + + // walk backwards until we find the target block + loop { + if *target_header.number() < target_number { + unreachable!( + "we are traversing backwards from a known block; \ + blocks are stored contiguously; \ + qed" + ); + } + + if *target_header.number() == target_number { + break; + } + + target_header = self + .client + .header(BlockId::Hash(*target_header.parent_hash())) + .ok()? + .expect("Header known to exist after `finality_target` call; qed"); + } + + Some((base_header, best_header, target_header)) + } else { + // otherwise just use the given best as the target + Some((base_header, best_header.clone(), best_header)) + } + } + Ok(None) => { + debug!(target: "afg", "Encountered error finding best chain containing {:?}: couldn't find target block", block); + None + } + Err(e) => { + debug!(target: "afg", "Encountered error finding best chain containing {:?}: {:?}", block, e); + None + } + } + }; + + if let Some((base_header, best_header, target_header)) = find_best_chain() { + // restrict vote according to the given voting rule, if the + // voting rule doesn't restrict the vote then we keep the + // previous target. + // + // note that we pass the original `best_header`, i.e. before the + // authority set limit filter, which can be considered a + // mandatory/implicit voting rule. + // + // we also make sure that the restricted vote is higher than the + // round base (i.e. last finalized), otherwise the value + // returned by the given voting rule is ignored and the original + // target is used instead. + let rule_fut = self.voting_rule.restrict_vote( + self.client.clone(), + &base_header, + &best_header, + &target_header, + ); + + Box::pin(async move { + Ok(rule_fut + .await + .filter(|(_, restricted_number)| { + // we can only restrict votes within the interval [base, target] + restricted_number >= base_header.number() + && restricted_number < target_header.number() + }) + .or_else(|| Some((target_header.hash(), *target_header.number())))) + }) + } else { + Box::pin(future::ok(None)) + } + } + fn round_data( &self, round: RoundNumber, diff --git a/client/finality-grandpa/src/finality_proof.rs b/client/finality-grandpa/src/finality_proof.rs index e17045349d452..e1e424472ff98 100644 --- a/client/finality-grandpa/src/finality_proof.rs +++ b/client/finality-grandpa/src/finality_proof.rs @@ -277,9 +277,9 @@ pub fn prove_warp_sync>( // This operation is a costy and only for the delay corner case. while index > Zero::zero() { index = index - One::one(); - if let Some((fragement, apply_block)) = get_warp_sync_proof_fragment(blockchain, index, &mut cache)? { + if let Some((fragment, apply_block)) = get_warp_sync_proof_fragment(blockchain, index, &mut cache)? { if last_apply.map(|next| &next > header.number()).unwrap_or(false) { - result.push(fragement); + result.push(fragment); last_apply = Some(apply_block); } else { break; @@ -289,7 +289,7 @@ pub fn prove_warp_sync>( let mut index = *header.number(); while index <= end_number { - if max_fragment_limit.map(|limit| result.len() <= limit).unwrap_or(false) { + if max_fragment_limit.map(|limit| result.len() >= limit).unwrap_or(false) { break; } @@ -305,7 +305,10 @@ pub fn prove_warp_sync>( index = index + One::one(); } - if result.last().as_ref().map(|head| head.header.number()) != Some(&end_number) { + let at_limit = max_fragment_limit.map(|limit| result.len() >= limit).unwrap_or(false); + + // add last finalized block if reached and not already included. + if !at_limit && result.last().as_ref().map(|head| head.header.number()) != Some(&end_number) { let header = blockchain.expect_header(end)?; if let Some(justification) = blockchain.justification(BlockId::Number(end_number.clone()))? { result.push(AuthoritySetProofFragment { @@ -328,7 +331,7 @@ fn get_warp_sync_proof_fragment>( ) -> sp_blockchain::Result, NumberFor)>> { if let Some(cache) = cache.as_mut() { if let Some(result) = cache.get_item(index) { - return Ok(result.clone()); + return Ok(result); } } @@ -541,11 +544,11 @@ impl BlockJustification for GrandpaJustification { + header_has_proof_fragment: std::collections::HashMap, cache: linked_hash_map::LinkedHashMap< Header::Number, - Option<(AuthoritySetProofFragment

, Header::Number)>, + (AuthoritySetProofFragment
, Header::Number), >, - headers_with_justification: usize, limit: usize, } @@ -553,8 +556,8 @@ impl WarpSyncFragmentCache
{ /// Instantiate a new cache for the warp sync prover. pub fn new(size: usize) -> Self { WarpSyncFragmentCache { + header_has_proof_fragment: Default::default(), cache: Default::default(), - headers_with_justification: 0, limit: size, } } @@ -564,31 +567,32 @@ impl WarpSyncFragmentCache
{ at: Header::Number, item: Option<(AuthoritySetProofFragment
, Header::Number)>, ) { - if self.cache.len() == self.limit { - self.pop_one(); - } - if item.is_some() { - // we do not check previous value as cached value is always supposed to - // be queried before calling 'new_item'. - self.headers_with_justification += 1; + self.header_has_proof_fragment.insert(at, item.is_some()); + + if let Some(item) = item { + if self.cache.len() == self.limit { + self.pop_one(); + } + + self.cache.insert(at, item); } - self.cache.insert(at, item); } fn pop_one(&mut self) { - while let Some(v) = self.cache.pop_front() { - if v.1.is_some() { - self.headers_with_justification -= 1; - break; - } + if let Some((header_number, _)) = self.cache.pop_front() { + self.header_has_proof_fragment.remove(&header_number); } } fn get_item( &mut self, block: Header::Number, - ) -> Option<&mut Option<(AuthoritySetProofFragment
, Header::Number)>> { - self.cache.get_refresh(&block) + ) -> Option, Header::Number)>> { + match self.header_has_proof_fragment.get(&block) { + Some(true) => Some(self.cache.get_refresh(&block).cloned()), + Some(false) => Some(None), + None => None + } } } diff --git a/client/finality-grandpa/src/import.rs b/client/finality-grandpa/src/import.rs index 2eef13d583600..d7b83b8032909 100644 --- a/client/finality-grandpa/src/import.rs +++ b/client/finality-grandpa/src/import.rs @@ -663,6 +663,7 @@ where Error::Safety(error) => ConsensusError::ClientImport(error), Error::Signing(error) => ConsensusError::ClientImport(error), Error::Timer(error) => ConsensusError::ClientImport(error.to_string()), + Error::RuntimeApi(error) => ConsensusError::ClientImport(error.to_string()), }); }, Ok(_) => { diff --git a/client/finality-grandpa/src/justification.rs b/client/finality-grandpa/src/justification.rs index 9429acff06d8c..eba909bad5ef6 100644 --- a/client/finality-grandpa/src/justification.rs +++ b/client/finality-grandpa/src/justification.rs @@ -217,8 +217,4 @@ impl finality_grandpa::Chain> for A Ok(route) } - - fn best_chain_containing(&self, _block: Block::Hash) -> Option<(Block::Hash, NumberFor)> { - None - } } diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index c7f7c8517b957..809e14e5c90b5 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -127,7 +127,8 @@ pub use notification::{GrandpaJustificationSender, GrandpaJustificationStream}; pub use import::GrandpaBlockImport; pub use justification::GrandpaJustification; pub use voting_rule::{ - BeforeBestBlockBy, ThreeQuartersOfTheUnfinalizedChain, VotingRule, VotingRulesBuilder + BeforeBestBlockBy, ThreeQuartersOfTheUnfinalizedChain, VotingRule, VotingRuleResult, + VotingRulesBuilder, }; pub use finality_grandpa::voter::report; pub use finality_proof::{prove_warp_sync, WarpSyncFragmentCache}; @@ -295,6 +296,8 @@ pub enum Error { Safety(String), /// A timer failed to fire. Timer(io::Error), + /// A runtime api request failed. + RuntimeApi(sp_api::ApiError), } impl From for Error { @@ -676,10 +679,10 @@ pub fn grandpa_peers_set_config() -> sc_network::config::NonDefaultSetConfig { // Notifications reach ~256kiB in size at the time of writing on Kusama and Polkadot. max_notification_size: 1024 * 1024, set_config: sc_network::config::SetConfig { - in_peers: 25, - out_peers: 25, + in_peers: 0, + out_peers: 0, reserved_nodes: Vec::new(), - non_reserved_mode: sc_network::config::NonReservedPeerMode::Accept, + non_reserved_mode: sc_network::config::NonReservedPeerMode::Deny, }, } } @@ -698,7 +701,7 @@ where NumberFor: BlockNumberOps, DigestFor: Encode, C: ClientForGrandpa + 'static, - C::Api: GrandpaApi, + C::Api: GrandpaApi, { let GrandpaParams { mut config, @@ -824,7 +827,7 @@ where Block: BlockT, B: Backend + 'static, C: ClientForGrandpa + 'static, - C::Api: GrandpaApi, + C::Api: GrandpaApi, N: NetworkT + Sync, NumberFor: BlockNumberOps, SC: SelectChain + 'static, @@ -1042,7 +1045,7 @@ where NumberFor: BlockNumberOps, SC: SelectChain + 'static, C: ClientForGrandpa + 'static, - C::Api: GrandpaApi, + C::Api: GrandpaApi, VR: VotingRule + Clone + 'static, { type Output = Result<(), Error>; diff --git a/client/finality-grandpa/src/observer.rs b/client/finality-grandpa/src/observer.rs index c9db917e1699a..3054a9df61c56 100644 --- a/client/finality-grandpa/src/observer.rs +++ b/client/finality-grandpa/src/observer.rs @@ -57,11 +57,6 @@ impl<'a, Block, Client> finality_grandpa::Chain> fn ancestry(&self, base: Block::Hash, block: Block::Hash) -> Result, GrandpaError> { environment::ancestry(&self.client, base, block) } - - fn best_chain_containing(&self, _block: Block::Hash) -> Option<(Block::Hash, NumberFor)> { - // only used by voter - None - } } fn grandpa_observer( diff --git a/client/finality-grandpa/src/tests.rs b/client/finality-grandpa/src/tests.rs index b94981838138a..921b49db61c25 100644 --- a/client/finality-grandpa/src/tests.rs +++ b/client/finality-grandpa/src/tests.rs @@ -174,8 +174,6 @@ impl ProvideRuntimeApi for TestApi { sp_api::mock_impl_runtime_apis! { impl GrandpaApi for RuntimeApi { - type Error = sp_blockchain::Error; - fn grandpa_authorities(&self) -> AuthorityList { self.inner.genesis_authorities.clone() } @@ -1357,7 +1355,7 @@ where #[test] fn grandpa_environment_respects_voting_rules() { - use finality_grandpa::Chain; + use finality_grandpa::voter::Environment; let peers = &[Ed25519Keyring::Alice]; let voters = make_ids(peers); @@ -1392,25 +1390,25 @@ fn grandpa_environment_respects_voting_rules() { // the unrestricted environment should just return the best block assert_eq!( - unrestricted_env.best_chain_containing( + futures::executor::block_on(unrestricted_env.best_chain_containing( peer.client().info().finalized_hash - ).unwrap().1, + )).unwrap().unwrap().1, 21, ); // both the other environments should return block 16, which is 3/4 of the // way in the unfinalized chain assert_eq!( - three_quarters_env.best_chain_containing( + futures::executor::block_on(three_quarters_env.best_chain_containing( peer.client().info().finalized_hash - ).unwrap().1, + )).unwrap().unwrap().1, 16, ); assert_eq!( - default_env.best_chain_containing( + futures::executor::block_on(default_env.best_chain_containing( peer.client().info().finalized_hash - ).unwrap().1, + )).unwrap().unwrap().1, 16, ); @@ -1419,18 +1417,18 @@ fn grandpa_environment_respects_voting_rules() { // the 3/4 environment should propose block 21 for voting assert_eq!( - three_quarters_env.best_chain_containing( + futures::executor::block_on(three_quarters_env.best_chain_containing( peer.client().info().finalized_hash - ).unwrap().1, + )).unwrap().unwrap().1, 21, ); // while the default environment will always still make sure we don't vote // on the best block (2 behind) assert_eq!( - default_env.best_chain_containing( + futures::executor::block_on(default_env.best_chain_containing( peer.client().info().finalized_hash - ).unwrap().1, + )).unwrap().unwrap().1, 19, ); @@ -1441,9 +1439,9 @@ fn grandpa_environment_respects_voting_rules() { // best block, there's a hard rule that we can't cast any votes lower than // the given base (#21). assert_eq!( - default_env.best_chain_containing( + futures::executor::block_on(default_env.best_chain_containing( peer.client().info().finalized_hash - ).unwrap().1, + )).unwrap().unwrap().1, 21, ); } diff --git a/client/finality-grandpa/src/voting_rule.rs b/client/finality-grandpa/src/voting_rule.rs index a861e792755fe..e7b74c3e3296e 100644 --- a/client/finality-grandpa/src/voting_rule.rs +++ b/client/finality-grandpa/src/voting_rule.rs @@ -22,14 +22,22 @@ //! restrictions that are taken into account by the GRANDPA environment when //! selecting a finality target to vote on. +use std::future::Future; use std::sync::Arc; +use std::pin::Pin; + +use dyn_clone::DynClone; use sc_client_api::blockchain::HeaderBackend; use sp_runtime::generic::BlockId; use sp_runtime::traits::{Block as BlockT, Header, NumberFor, One, Zero}; +/// A future returned by a `VotingRule` to restrict a given vote, if any restriction is necessary. +pub type VotingRuleResult = + Pin::Hash, NumberFor)>> + Send + Sync>>; + /// A trait for custom voting rules in GRANDPA. -pub trait VotingRule: Send + Sync where +pub trait VotingRule: DynClone + Send + Sync where Block: BlockT, B: HeaderBackend, { @@ -47,11 +55,11 @@ pub trait VotingRule: Send + Sync where /// execution of voting rules wherein `current_target <= best_target`. fn restrict_vote( &self, - backend: &B, + backend: Arc, base: &Block::Header, best_target: &Block::Header, current_target: &Block::Header, - ) -> Option<(Block::Hash, NumberFor)>; + ) -> VotingRuleResult; } impl VotingRule for () where @@ -60,12 +68,12 @@ impl VotingRule for () where { fn restrict_vote( &self, - _backend: &B, + _backend: Arc, _base: &Block::Header, _best_target: &Block::Header, _current_target: &Block::Header, - ) -> Option<(Block::Hash, NumberFor)> { - None + ) -> VotingRuleResult { + Box::pin(async { None }) } } @@ -80,15 +88,15 @@ impl VotingRule for BeforeBestBlockBy> wher { fn restrict_vote( &self, - backend: &B, + backend: Arc, _base: &Block::Header, best_target: &Block::Header, current_target: &Block::Header, - ) -> Option<(Block::Hash, NumberFor)> { + ) -> VotingRuleResult { use sp_arithmetic::traits::Saturating; if current_target.number().is_zero() { - return None; + return Box::pin(async { None }); } // find the target number restricted by this rule @@ -96,21 +104,24 @@ impl VotingRule for BeforeBestBlockBy> wher // our current target is already lower than this rule would restrict if target_number >= *current_target.number() { - return None; + return Box::pin(async { None }); } + let current_target = current_target.clone(); + // find the block at the given target height - find_target( - backend, - target_number, - current_target, - ) + Box::pin(std::future::ready(find_target( + &*backend, + target_number.clone(), + ¤t_target, + ))) } } /// A custom voting rule that limits votes towards 3/4 of the unfinalized chain, /// using the given `base` and `best_target` to figure where the 3/4 target /// should fall. +#[derive(Clone)] pub struct ThreeQuartersOfTheUnfinalizedChain; impl VotingRule for ThreeQuartersOfTheUnfinalizedChain where @@ -119,11 +130,11 @@ impl VotingRule for ThreeQuartersOfTheUnfinalizedChain where { fn restrict_vote( &self, - backend: &B, + backend: Arc, base: &Block::Header, best_target: &Block::Header, current_target: &Block::Header, - ) -> Option<(Block::Hash, NumberFor)> { + ) -> VotingRuleResult { // target a vote towards 3/4 of the unfinalized chain (rounding up) let target_number = { let two = NumberFor::::one() + One::one(); @@ -138,15 +149,15 @@ impl VotingRule for ThreeQuartersOfTheUnfinalizedChain where // our current target is already lower than this rule would restrict if target_number >= *current_target.number() { - return None; + return Box::pin(async { None }); } // find the block at the given target height - find_target( - backend, + Box::pin(std::future::ready(find_target( + &*backend, target_number, current_target, - ) + ))) } } @@ -195,37 +206,42 @@ impl Clone for VotingRules { impl VotingRule for VotingRules where Block: BlockT, - B: HeaderBackend, + B: HeaderBackend + 'static, { fn restrict_vote( &self, - backend: &B, + backend: Arc, base: &Block::Header, best_target: &Block::Header, current_target: &Block::Header, - ) -> Option<(Block::Hash, NumberFor)> { - let restricted_target = self.rules.iter().fold( - current_target.clone(), - |current_target, rule| { - rule.restrict_vote( - backend, - base, - best_target, - ¤t_target, - ) + ) -> VotingRuleResult { + let rules = self.rules.clone(); + let base = base.clone(); + let best_target = best_target.clone(); + let current_target = current_target.clone(); + + Box::pin(async move { + let mut restricted_target = current_target.clone(); + + for rule in rules.iter() { + if let Some(header) = rule + .restrict_vote(backend.clone(), &base, &best_target, &restricted_target) + .await .and_then(|(hash, _)| backend.header(BlockId::Hash(hash)).ok()) .and_then(std::convert::identity) - .unwrap_or(current_target) - }, - ); - - let restricted_hash = restricted_target.hash(); - - if restricted_hash != current_target.hash() { - Some((restricted_hash, *restricted_target.number())) - } else { - None - } + { + restricted_target = header; + } + } + + let restricted_hash = restricted_target.hash(); + + if restricted_hash != current_target.hash() { + Some((restricted_hash, *restricted_target.number())) + } else { + None + } + }) } } @@ -237,7 +253,7 @@ pub struct VotingRulesBuilder { impl Default for VotingRulesBuilder where Block: BlockT, - B: HeaderBackend, + B: HeaderBackend + 'static, { fn default() -> Self { VotingRulesBuilder::new() @@ -248,7 +264,7 @@ impl Default for VotingRulesBuilder where impl VotingRulesBuilder where Block: BlockT, - B: HeaderBackend, + B: HeaderBackend + 'static, { /// Return a new voting rule builder using the given backend. pub fn new() -> Self { @@ -285,14 +301,15 @@ impl VotingRulesBuilder where impl VotingRule for Box> where Block: BlockT, B: HeaderBackend, + Self: Clone, { fn restrict_vote( &self, - backend: &B, + backend: Arc, base: &Block::Header, best_target: &Block::Header, current_target: &Block::Header, - ) -> Option<(Block::Hash, NumberFor)> { + ) -> VotingRuleResult { (**self).restrict_vote(backend, base, best_target, current_target) } } diff --git a/client/informant/Cargo.toml b/client/informant/Cargo.toml index fe5ae3857f097..d552a123c3788 100644 --- a/client/informant/Cargo.toml +++ b/client/informant/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-informant" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Substrate informant." edition = "2018" @@ -16,11 +16,11 @@ targets = ["x86_64-unknown-linux-gnu"] ansi_term = "0.12.1" futures = "0.3.9" log = "0.4.8" -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } -sc-client-api = { version = "2.0.0", path = "../api" } -sc-network = { version = "0.8.0", path = "../network" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-utils = { version = "2.0.0", path = "../../primitives/utils" } -sp-transaction-pool = { version = "2.0.0", path = "../../primitives/transaction-pool" } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } +sc-client-api = { version = "3.0.0", path = "../api" } +sc-network = { version = "0.9.0", path = "../network" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-utils = { version = "3.0.0", path = "../../primitives/utils" } +sp-transaction-pool = { version = "3.0.0", path = "../../primitives/transaction-pool" } wasm-timer = "0.2" diff --git a/client/keystore/Cargo.toml b/client/keystore/Cargo.toml index 29cbfea3acfd8..fd9fd162e6179 100644 --- a/client/keystore/Cargo.toml +++ b/client/keystore/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-keystore" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -19,9 +19,9 @@ async-trait = "0.1.30" derive_more = "0.99.2" futures = "0.3.9" futures-util = "0.3.4" -sp-application-crypto = { version = "2.0.0", path = "../../primitives/application-crypto" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-keystore = { version = "0.8.0", path = "../../primitives/keystore" } +sp-application-crypto = { version = "3.0.0", path = "../../primitives/application-crypto" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-keystore = { version = "0.9.0", path = "../../primitives/keystore" } hex = "0.4.0" merlin = { version = "2.0", default-features = false } parking_lot = "0.11.1" diff --git a/client/keystore/src/lib.rs b/client/keystore/src/lib.rs index 9cad56efacfd6..38ab640d2e303 100644 --- a/client/keystore/src/lib.rs +++ b/client/keystore/src/lib.rs @@ -46,9 +46,6 @@ pub enum Error { /// Public key type is not supported #[display(fmt="Key crypto type is not supported")] KeyNotSupported(KeyTypeId), - /// Pair not found for public key and KeyTypeId - #[display(fmt="Pair not found for {} public key", "_0")] - PairNotFound(String), /// Keystore unavailable #[display(fmt="Keystore unavailable")] Unavailable, @@ -61,7 +58,6 @@ impl From for TraitError { fn from(error: Error) -> Self { match error { Error::KeyNotSupported(id) => TraitError::KeyNotSupported(id), - Error::PairNotFound(e) => TraitError::PairNotFound(e), Error::InvalidSeed | Error::InvalidPhrase | Error::InvalidPassword => { TraitError::ValidationError(error.to_string()) }, diff --git a/client/keystore/src/local.rs b/client/keystore/src/local.rs index 866a50ae4c93c..482ef407601dc 100644 --- a/client/keystore/src/local.rs +++ b/client/keystore/src/local.rs @@ -60,9 +60,9 @@ impl LocalKeystore { /// Get a key pair for the given public key. /// - /// This function is only available for a local keystore. If your application plans to work with - /// remote keystores, you do not want to depend on it. - pub fn key_pair(&self, public: &::Public) -> Result { + /// Returns `Ok(None)` if the key doesn't exist, `Ok(Some(_))` if the key exists and + /// `Err(_)` when something failed. + pub fn key_pair(&self, public: &::Public) -> Result> { self.0.read().key_pair::(public) } } @@ -130,7 +130,7 @@ impl CryptoStore for LocalKeystore { id: KeyTypeId, key: &CryptoTypePublicPair, msg: &[u8], - ) -> std::result::Result, TraitError> { + ) -> std::result::Result>, TraitError> { SyncCryptoStore::sign_with(self, id, key, msg) } @@ -139,7 +139,7 @@ impl CryptoStore for LocalKeystore { key_type: KeyTypeId, public: &sr25519::Public, transcript_data: VRFTranscriptData, - ) -> std::result::Result { + ) -> std::result::Result, TraitError> { SyncCryptoStore::sr25519_vrf_sign(self, key_type, public, transcript_data) } } @@ -175,28 +175,28 @@ impl SyncCryptoStore for LocalKeystore { id: KeyTypeId, key: &CryptoTypePublicPair, msg: &[u8], - ) -> std::result::Result, TraitError> { + ) -> std::result::Result>, TraitError> { match key.0 { ed25519::CRYPTO_ID => { let pub_key = ed25519::Public::from_slice(key.1.as_slice()); - let key_pair: ed25519::Pair = self.0.read() + let key_pair = self.0.read() .key_pair_by_type::(&pub_key, id) .map_err(|e| TraitError::from(e))?; - Ok(key_pair.sign(msg).encode()) + key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose() } sr25519::CRYPTO_ID => { let pub_key = sr25519::Public::from_slice(key.1.as_slice()); - let key_pair: sr25519::Pair = self.0.read() + let key_pair = self.0.read() .key_pair_by_type::(&pub_key, id) .map_err(|e| TraitError::from(e))?; - Ok(key_pair.sign(msg).encode()) + key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose() }, ecdsa::CRYPTO_ID => { let pub_key = ecdsa::Public::from_slice(key.1.as_slice()); - let key_pair: ecdsa::Pair = self.0.read() + let key_pair = self.0.read() .key_pair_by_type::(&pub_key, id) .map_err(|e| TraitError::from(e))?; - Ok(key_pair.sign(msg).encode()) + key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose() } _ => Err(TraitError::KeyNotSupported(id)) } @@ -232,7 +232,7 @@ impl SyncCryptoStore for LocalKeystore { .map(|k| ed25519::Public::from_slice(k.as_slice())) .collect() }) - .unwrap_or_default() + .unwrap_or_default() } fn ed25519_generate_new( @@ -278,7 +278,8 @@ impl SyncCryptoStore for LocalKeystore { } fn has_keys(&self, public_keys: &[(Vec, KeyTypeId)]) -> bool { - public_keys.iter().all(|(p, t)| self.0.read().key_phrase_by_type(&p, *t).is_ok()) + public_keys.iter() + .all(|(p, t)| self.0.read().key_phrase_by_type(&p, *t).ok().flatten().is_some()) } fn sr25519_vrf_sign( @@ -286,16 +287,19 @@ impl SyncCryptoStore for LocalKeystore { key_type: KeyTypeId, public: &Sr25519Public, transcript_data: VRFTranscriptData, - ) -> std::result::Result { + ) -> std::result::Result, TraitError> { let transcript = make_transcript(transcript_data); - let pair = self.0.read().key_pair_by_type::(public, key_type) - .map_err(|e| TraitError::PairNotFound(e.to_string()))?; + let pair = self.0.read().key_pair_by_type::(public, key_type)?; - let (inout, proof, _) = pair.as_ref().vrf_sign(transcript); - Ok(VRFSignature { - output: inout.to_output(), - proof, - }) + if let Some(pair) = pair { + let (inout, proof, _) = pair.as_ref().vrf_sign(transcript); + Ok(Some(VRFSignature { + output: inout.to_output(), + proof, + })) + } else { + Ok(None) + } } } @@ -411,36 +415,53 @@ impl KeystoreInner { } /// Get the key phrase for a given public key and key type. - fn key_phrase_by_type(&self, public: &[u8], key_type: KeyTypeId) -> Result { + fn key_phrase_by_type(&self, public: &[u8], key_type: KeyTypeId) -> Result> { if let Some(phrase) = self.get_additional_pair(public, key_type) { - return Ok(phrase.clone()) + return Ok(Some(phrase.clone())) } - let path = self.key_file_path(public, key_type).ok_or_else(|| Error::Unavailable)?; - let file = File::open(path)?; + let path = if let Some(path) = self.key_file_path(public, key_type) { + path + } else { + return Ok(None); + }; + + if path.exists() { + let file = File::open(path)?; - serde_json::from_reader(&file).map_err(Into::into) + serde_json::from_reader(&file).map_err(Into::into).map(Some) + } else { + Ok(None) + } } /// Get a key pair for the given public key and key type. - fn key_pair_by_type(&self, + fn key_pair_by_type( + &self, public: &Pair::Public, key_type: KeyTypeId, - ) -> Result { - let phrase = self.key_phrase_by_type(public.as_slice(), key_type)?; + ) -> Result> { + let phrase = if let Some(p) = self.key_phrase_by_type(public.as_slice(), key_type)? { + p + } else { + return Ok(None) + }; + let pair = Pair::from_string( &phrase, self.password(), ).map_err(|_| Error::InvalidPhrase)?; if &pair.public() == public { - Ok(pair) + Ok(Some(pair)) } else { Err(Error::InvalidPassword) } } - /// Returns the file path for the given public key and key type. + /// Get the file path for the given public key and key type. + /// + /// Returns `None` if the keystore only exists in-memory and there isn't any path to provide. fn key_file_path(&self, public: &[u8], key_type: KeyTypeId) -> Option { let mut buf = self.path.as_ref()?.clone(); let key_type = hex::encode(key_type.0); @@ -481,8 +502,12 @@ impl KeystoreInner { } /// Get a key pair for the given public key. - pub fn key_pair(&self, public: &::Public) -> Result { - self.key_pair_by_type::(IsWrappedBy::from_ref(public), Pair::ID).map(Into::into) + /// + /// Returns `Ok(None)` if the key doesn't exist, `Ok(Some(_))` if the key exists or `Err(_)` when + /// something failed. + pub fn key_pair(&self, public: &::Public) -> Result> { + self.key_pair_by_type::(IsWrappedBy::from_ref(public), Pair::ID) + .map(|v| v.map(Into::into)) } } @@ -531,13 +556,40 @@ mod tests { assert!(store.public_keys::().unwrap().is_empty()); let key: ed25519::AppPair = store.generate().unwrap(); - let key2: ed25519::AppPair = store.key_pair(&key.public()).unwrap(); + let key2: ed25519::AppPair = store.key_pair(&key.public()).unwrap().unwrap(); assert_eq!(key.public(), key2.public()); assert_eq!(store.public_keys::().unwrap()[0], key.public()); } + #[test] + fn has_keys_works() { + let temp_dir = TempDir::new().unwrap(); + let store = LocalKeystore::open(temp_dir.path(), None).unwrap(); + + let key: ed25519::AppPair = store.0.write().generate().unwrap(); + let key2 = ed25519::Pair::generate().0; + + assert!( + !SyncCryptoStore::has_keys(&store, &[(key2.public().to_vec(), ed25519::AppPublic::ID)]) + ); + + assert!( + !SyncCryptoStore::has_keys( + &store, + &[ + (key2.public().to_vec(), ed25519::AppPublic::ID), + (key.public().to_raw_vec(), ed25519::AppPublic::ID), + ], + ) + ); + + assert!( + SyncCryptoStore::has_keys(&store, &[(key.public().to_raw_vec(), ed25519::AppPublic::ID)]) + ); + } + #[test] fn test_insert_ephemeral_from_seed() { let temp_dir = TempDir::new().unwrap(); @@ -554,7 +606,7 @@ mod tests { drop(store); let store = KeystoreInner::open(temp_dir.path(), None).unwrap(); // Keys generated from seed should not be persisted! - assert!(store.key_pair::(&pair.public()).is_err()); + assert!(store.key_pair::(&pair.public()).unwrap().is_none()); } #[test] @@ -569,7 +621,7 @@ mod tests { let pair: ed25519::AppPair = store.generate().unwrap(); assert_eq!( pair.public(), - store.key_pair::(&pair.public()).unwrap().public(), + store.key_pair::(&pair.public()).unwrap().unwrap().public(), ); // Without the password the key should not be retrievable @@ -582,7 +634,7 @@ mod tests { ).unwrap(); assert_eq!( pair.public(), - store.key_pair::(&pair.public()).unwrap().public(), + store.key_pair::(&pair.public()).unwrap().unwrap().public(), ); } @@ -626,7 +678,7 @@ mod tests { let store_key_pair = store.key_pair_by_type::( &key_pair.public(), SR25519, - ).expect("Gets key pair from keystore"); + ).expect("Gets key pair from keystore").unwrap(); assert_eq!(key_pair.public(), store_key_pair.public()); } diff --git a/client/light/Cargo.toml b/client/light/Cargo.toml index 60d16ff0359ce..1b45dbf5c0c54 100644 --- a/client/light/Cargo.toml +++ b/client/light/Cargo.toml @@ -1,7 +1,7 @@ [package] description = "components for a light client" name = "sc-light" -version = "2.0.1" +version = "3.0.0" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" authors = ["Parity Technologies "] edition = "2018" @@ -14,15 +14,15 @@ readme = "README.md" parking_lot = "0.11.1" lazy_static = "1.4.0" hash-db = "0.15.2" -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-externalities = { version = "0.8.0", path = "../../primitives/externalities" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine" } -sc-client-api = { version = "2.0.0", path = "../api" } -sp-api = { version = "2.0.0", path = "../../primitives/api" } -codec = { package = "parity-scale-codec", version = "1.3.6" } -sc-executor = { version = "0.8.0", path = "../executor" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" } +sc-client-api = { version = "3.0.0", path = "../api" } +sp-api = { version = "3.0.0", path = "../../primitives/api" } +codec = { package = "parity-scale-codec", version = "2.0.0" } +sc-executor = { version = "0.9.0", path = "../executor" } [features] default = [] diff --git a/client/light/src/blockchain.rs b/client/light/src/blockchain.rs index f682e6e35b3d0..bcabc365676a5 100644 --- a/client/light/src/blockchain.rs +++ b/client/light/src/blockchain.rs @@ -128,6 +128,13 @@ impl BlockchainBackend for Blockchain where Block: BlockT, S fn children(&self, _parent_hash: Block::Hash) -> ClientResult> { Err(ClientError::NotAvailableOnLightClient) } + + fn extrinsic( + &self, + _hash: &Block::Hash, + ) -> ClientResult::Extrinsic>> { + Err(ClientError::NotAvailableOnLightClient) + } } impl, Block: BlockT> ProvideCache for Blockchain { diff --git a/client/light/src/call_executor.rs b/client/light/src/call_executor.rs index 8b403823b0eec..ae83807dc98f2 100644 --- a/client/light/src/call_executor.rs +++ b/client/light/src/call_executor.rs @@ -104,7 +104,7 @@ impl CallExecutor for Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >( &self, initialize_block_fn: IB, diff --git a/client/network-gossip/Cargo.toml b/client/network-gossip/Cargo.toml index b0120e306a52f..b5f3b754af030 100644 --- a/client/network-gossip/Cargo.toml +++ b/client/network-gossip/Cargo.toml @@ -1,7 +1,7 @@ [package] description = "Gossiping for the Substrate network protocol" name = "sc-network-gossip" -version = "0.8.1" +version = "0.9.0" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" authors = ["Parity Technologies "] edition = "2018" @@ -17,16 +17,16 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] futures = "0.3.9" futures-timer = "3.0.1" -libp2p = { version = "0.34.0", default-features = false } +libp2p = { version = "0.35.1", default-features = false } log = "0.4.8" -lru = "0.6.1" -prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0", path = "../../utils/prometheus" } -sc-network = { version = "0.8.0", path = "../network" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } +lru = "0.6.5" +prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.9.0", path = "../../utils/prometheus" } +sc-network = { version = "0.9.0", path = "../network" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } wasm-timer = "0.2" [dev-dependencies] async-std = "1.6.5" -quickcheck = "0.9.0" +quickcheck = "1.0.3" rand = "0.7.2" substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } diff --git a/client/network-gossip/src/bridge.rs b/client/network-gossip/src/bridge.rs index 15451ec3cd577..235ac98dc3968 100644 --- a/client/network-gossip/src/bridge.rs +++ b/client/network-gossip/src/bridge.rs @@ -303,7 +303,6 @@ mod tests { use crate::{ValidationResult, ValidatorContext}; use futures::{channel::mpsc::{unbounded, UnboundedSender}, executor::{block_on, block_on_stream}, future::poll_fn}; use quickcheck::{Arbitrary, Gen, QuickCheck}; - use rand::Rng; use sc_network::ObservedRole; use sp_runtime::{testing::H256, traits::{Block as BlockT}}; use std::borrow::Cow; @@ -469,12 +468,14 @@ mod tests { } impl Arbitrary for ChannelLengthAndTopic { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { + let possible_length = (0..100).collect::>(); + let possible_topics = (0..10).collect::>(); Self { - length: g.gen_range(0, 100), + length: *g.choose(&possible_length).unwrap(), // Make sure channel topics and message topics overlap by choosing a small // range. - topic: H256::from_low_u64_ne(g.gen_range(0, 10)), + topic: H256::from_low_u64_ne(*g.choose(&possible_topics).unwrap()), } } } @@ -485,11 +486,12 @@ mod tests { } impl Arbitrary for Message{ - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { + let possible_topics = (0..10).collect::>(); Self { // Make sure channel topics and message topics overlap by choosing a small // range. - topic: H256::from_low_u64_ne(g.gen_range(0, 10)), + topic: H256::from_low_u64_ne(*g.choose(&possible_topics).unwrap()), } } } diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index bf948ff4dd37d..3d8c33eae0f25 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -1,7 +1,7 @@ [package] description = "Substrate network protocol" name = "sc-network" -version = "0.8.1" +version = "0.9.0" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" authors = ["Parity Technologies "] edition = "2018" @@ -21,13 +21,14 @@ async-trait = "0.1" async-std = "1.6.5" bitflags = "1.2.0" bs58 = "0.4.0" +cid = "0.6.0" bytes = "1" -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } derive_more = "0.99.2" either = "1.5.3" erased-serde = "0.3.9" fnv = "1.0.6" -fork-tree = { version = "2.0.0", path = "../../utils/fork-tree" } +fork-tree = { version = "3.0.0", path = "../../utils/fork-tree" } futures = "0.3.9" futures-timer = "3.0.2" asynchronous-codec = "0.5" @@ -35,26 +36,26 @@ hex = "0.4.0" ip_network = "0.3.4" linked-hash-map = "0.5.2" linked_hash_set = "0.1.3" -lru = "0.6.3" +lru = "0.6.5" log = "0.4.8" nohash-hasher = "0.2.0" parking_lot = "0.11.1" -pin-project = "0.4.6" -prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0", path = "../../utils/prometheus" } +pin-project = "1.0.4" +prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.9.0", path = "../../utils/prometheus" } prost = "0.7" rand = "0.7.2" -sc-block-builder = { version = "0.8.0", path = "../block-builder" } -sc-client-api = { version = "2.0.0", path = "../api" } -sc-peerset = { version = "2.0.0", path = "../peerset" } +sc-block-builder = { version = "0.9.0", path = "../block-builder" } +sc-client-api = { version = "3.0.0", path = "../api" } +sc-peerset = { version = "3.0.0", path = "../peerset" } serde = { version = "1.0.101", features = ["derive"] } serde_json = "1.0.41" smallvec = "1.5.0" -sp-arithmetic = { version = "2.0.0", path = "../../primitives/arithmetic" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-consensus = { version = "0.8.0", path = "../../primitives/consensus/common" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-utils = { version = "2.0.0", path = "../../primitives/utils" } +sp-arithmetic = { version = "3.0.0", path = "../../primitives/arithmetic" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-utils = { version = "3.0.0", path = "../../primitives/utils" } thiserror = "1" unsigned-varint = { version = "0.6.0", features = ["futures", "asynchronous_codec"] } void = "1.0.2" @@ -62,18 +63,22 @@ wasm-timer = "0.2" zeroize = "1.2.0" [dependencies.libp2p] -version = "0.34.0" +version = "0.35.1" + +[target.'cfg(target_os = "unknown")'.dependencies.libp2p] +version = "0.35.1" default-features = false features = ["identify", "kad", "mdns", "mplex", "noise", "ping", "request-response", "tcp-async-io", "websocket", "yamux"] + [dev-dependencies] assert_matches = "1.3" -libp2p = { version = "0.34.0", default-features = false } -quickcheck = "0.9.0" +libp2p = { version = "0.35.1", default-features = false } +quickcheck = "1.0.3" rand = "0.7.2" -sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" } +sp-keyring = { version = "3.0.0", path = "../../primitives/keyring" } sp-test-primitives = { version = "2.0.0", path = "../../primitives/test-primitives" } -sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } tempfile = "3.1.0" diff --git a/client/network/build.rs b/client/network/build.rs index 2ccc72d99df96..0eea622e87574 100644 --- a/client/network/build.rs +++ b/client/network/build.rs @@ -1,6 +1,7 @@ const PROTOS: &[&str] = &[ "src/schema/api.v1.proto", - "src/schema/light.v1.proto" + "src/schema/light.v1.proto", + "src/schema/bitswap.v1.2.0.proto", ]; fn main() { diff --git a/client/network/src/behaviour.rs b/client/network/src/behaviour.rs index 7b1a35354c913..0eebd1713cc81 100644 --- a/client/network/src/behaviour.rs +++ b/client/network/src/behaviour.rs @@ -17,19 +17,23 @@ // along with this program. If not, see . use crate::{ - config::{ProtocolId, Role}, light_client_handler, peer_info, request_responses, + config::ProtocolId, + bitswap::Bitswap, discovery::{DiscoveryBehaviour, DiscoveryConfig, DiscoveryOut}, protocol::{message::Roles, CustomMessageOutcome, NotificationsSink, Protocol}, - ObservedRole, DhtEvent, ExHashT, + peer_info, request_responses, light_client_requests, + ObservedRole, DhtEvent, }; use bytes::Bytes; -use futures::channel::oneshot; +use futures::{channel::oneshot, stream::StreamExt}; use libp2p::NetworkBehaviour; use libp2p::core::{Multiaddr, PeerId, PublicKey}; use libp2p::identify::IdentifyInfo; use libp2p::kad::record; -use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters}; +use libp2p::swarm::{ + NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters, toggle::Toggle +}; use log::debug; use prost::Message; use sp_consensus::{BlockOrigin, import_queue::{IncomingBlock, Origin}}; @@ -44,31 +48,32 @@ use std::{ pub use crate::request_responses::{ ResponseFailure, InboundFailure, RequestFailure, OutboundFailure, RequestId, + IfDisconnected }; /// General behaviour of the network. Combines all protocols together. #[derive(NetworkBehaviour)] #[behaviour(out_event = "BehaviourOut", poll_method = "poll")] -pub struct Behaviour { +pub struct Behaviour { /// All the substrate-specific protocols. - substrate: Protocol, + substrate: Protocol, /// Periodically pings and identifies the nodes we are connected to, and store information in a /// cache. peer_info: peer_info::PeerInfoBehaviour, /// Discovers nodes of the network. discovery: DiscoveryBehaviour, + /// Bitswap server for blockchain data. + bitswap: Toggle>, /// Generic request-reponse protocols. request_responses: request_responses::RequestResponsesBehaviour, - /// Light client request handling. - light_client_handler: light_client_handler::LightClientHandler, /// Queue of events to produce for the outside. #[behaviour(ignore)] events: VecDeque>, - /// Role of our local node, as originally passed from the configuration. + /// Light client request handling. #[behaviour(ignore)] - role: Role, + light_client_request_sender: light_client_requests::sender::LightClientRequestSender, /// Protocol name used to send out block requests via /// [`request_responses::RequestResponsesBehaviour`]. @@ -167,17 +172,17 @@ pub enum BehaviourOut { Dht(DhtEvent, Duration), } -impl Behaviour { +impl Behaviour { /// Builds a new `Behaviour`. pub fn new( - substrate: Protocol, - role: Role, + substrate: Protocol, user_agent: String, local_public_key: PublicKey, - light_client_handler: light_client_handler::LightClientHandler, + light_client_request_sender: light_client_requests::sender::LightClientRequestSender, disco_config: DiscoveryConfig, - // Block request protocol config. block_request_protocol_config: request_responses::ProtocolConfig, + bitswap: Option>, + light_client_request_protocol_config: request_responses::ProtocolConfig, // All remaining request protocol configs. mut request_response_protocols: Vec, ) -> Result { @@ -185,15 +190,17 @@ impl Behaviour { let block_request_protocol_name = block_request_protocol_config.name.to_string(); request_response_protocols.push(block_request_protocol_config); + request_response_protocols.push(light_client_request_protocol_config); + Ok(Behaviour { substrate, peer_info: peer_info::PeerInfoBehaviour::new(user_agent, local_public_key), discovery: disco_config.finish(), + bitswap: bitswap.into(), request_responses: request_responses::RequestResponsesBehaviour::new(request_response_protocols.into_iter())?, - light_client_handler, + light_client_request_sender, events: VecDeque::new(), - role, block_request_protocol_name, }) @@ -243,17 +250,18 @@ impl Behaviour { protocol: &str, request: Vec, pending_response: oneshot::Sender, RequestFailure>>, + connect: IfDisconnected, ) { - self.request_responses.send_request(target, protocol, request, pending_response) + self.request_responses.send_request(target, protocol, request, pending_response, connect) } /// Returns a shared reference to the user protocol. - pub fn user_protocol(&self) -> &Protocol { + pub fn user_protocol(&self) -> &Protocol { &self.substrate } /// Returns a mutable reference to the user protocol. - pub fn user_protocol_mut(&mut self) -> &mut Protocol { + pub fn user_protocol_mut(&mut self) -> &mut Protocol { &mut self.substrate } @@ -268,20 +276,17 @@ impl Behaviour { } /// Issue a light client request. - pub fn light_client_request(&mut self, r: light_client_handler::Request) -> Result<(), light_client_handler::Error> { - self.light_client_handler.request(r) + pub fn light_client_request( + &mut self, + r: light_client_requests::sender::Request, + ) -> Result<(), light_client_requests::sender::SendRequestError> { + self.light_client_request_sender.request(r) } } -fn reported_roles_to_observed_role(local_role: &Role, remote: &PeerId, roles: Roles) -> ObservedRole { +fn reported_roles_to_observed_role(roles: Roles) -> ObservedRole { if roles.is_authority() { - match local_role { - Role::Authority { sentry_nodes } - if sentry_nodes.iter().any(|s| s.peer_id == *remote) => ObservedRole::OurSentry, - Role::Sentry { validators } - if validators.iter().any(|s| s.peer_id == *remote) => ObservedRole::OurGuardedAuthority, - _ => ObservedRole::Authority - } + ObservedRole::Authority } else if roles.is_full() { ObservedRole::Full } else { @@ -289,15 +294,15 @@ fn reported_roles_to_observed_role(local_role: &Role, remote: &PeerId, roles: Ro } } -impl NetworkBehaviourEventProcess for -Behaviour { +impl NetworkBehaviourEventProcess for +Behaviour { fn inject_event(&mut self, event: void::Void) { void::unreachable(event) } } -impl NetworkBehaviourEventProcess> for -Behaviour { +impl NetworkBehaviourEventProcess> for +Behaviour { fn inject_event(&mut self, event: CustomMessageOutcome) { match event { CustomMessageOutcome::BlockImport(origin, blocks) => @@ -316,15 +321,14 @@ Behaviour { } self.request_responses.send_request( - &target, &self.block_request_protocol_name, buf, pending_response, + &target, &self.block_request_protocol_name, buf, pending_response, IfDisconnected::ImmediateError, ); }, CustomMessageOutcome::NotificationStreamOpened { remote, protocol, roles, notifications_sink } => { - let role = reported_roles_to_observed_role(&self.role, &remote, roles); self.events.push_back(BehaviourOut::NotificationStreamOpened { remote, protocol, - role: role.clone(), + role: reported_roles_to_observed_role(roles), notifications_sink: notifications_sink.clone(), }); }, @@ -343,18 +347,22 @@ Behaviour { self.events.push_back(BehaviourOut::NotificationsReceived { remote, messages }); }, CustomMessageOutcome::PeerNewBest(peer_id, number) => { - self.light_client_handler.update_best_block(&peer_id, number); + self.light_client_request_sender.update_best_block(&peer_id, number); + } + CustomMessageOutcome::SyncConnected(peer_id) => { + self.light_client_request_sender.inject_connected(peer_id); + self.events.push_back(BehaviourOut::SyncConnected(peer_id)) + } + CustomMessageOutcome::SyncDisconnected(peer_id) => { + self.light_client_request_sender.inject_disconnected(peer_id); + self.events.push_back(BehaviourOut::SyncDisconnected(peer_id)) } - CustomMessageOutcome::SyncConnected(peer_id) => - self.events.push_back(BehaviourOut::SyncConnected(peer_id)), - CustomMessageOutcome::SyncDisconnected(peer_id) => - self.events.push_back(BehaviourOut::SyncDisconnected(peer_id)), CustomMessageOutcome::None => {} } } } -impl NetworkBehaviourEventProcess for Behaviour { +impl NetworkBehaviourEventProcess for Behaviour { fn inject_event(&mut self, event: request_responses::Event) { match event { request_responses::Event::InboundRequest { peer, protocol, result } => { @@ -378,8 +386,8 @@ impl NetworkBehaviourEventProcess NetworkBehaviourEventProcess - for Behaviour { +impl NetworkBehaviourEventProcess + for Behaviour { fn inject_event(&mut self, event: peer_info::PeerInfoEvent) { let peer_info::PeerInfoEvent::Identified { peer_id, @@ -408,8 +416,8 @@ impl NetworkBehaviourEventProcess NetworkBehaviourEventProcess - for Behaviour { +impl NetworkBehaviourEventProcess + for Behaviour { fn inject_event(&mut self, out: DiscoveryOut) { match out { DiscoveryOut::UnroutablePeer(_peer_id) => { @@ -442,8 +450,32 @@ impl NetworkBehaviourEventProcess } } -impl Behaviour { - fn poll(&mut self, _: &mut Context, _: &mut impl PollParameters) -> Poll>> { +impl Behaviour { + fn poll( + &mut self, + cx: &mut Context, + _: &mut impl PollParameters, + ) -> Poll>> { + use light_client_requests::sender::OutEvent; + while let Poll::Ready(Some(event)) = + self.light_client_request_sender.poll_next_unpin(cx) + { + match event { + OutEvent::SendRequest { + target, + request, + pending_response, + protocol_name, + } => self.request_responses.send_request( + &target, + &protocol_name, + request, + pending_response, + IfDisconnected::ImmediateError, + ), + } + } + if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)) } diff --git a/client/network/src/bitswap.rs b/client/network/src/bitswap.rs new file mode 100644 index 0000000000000..7129f3dbe07b1 --- /dev/null +++ b/client/network/src/bitswap.rs @@ -0,0 +1,338 @@ +// Copyright 2021 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Substrate. If not, see . + +//! Bitswap server for substrate. +//! +//! Allows querying transactions by hash over standard bitswap protocol +//! Only supports bitswap 1.2.0. +//! CID is expected to reference 256-bit Blake2b transaction hash. + +use std::collections::VecDeque; +use std::io; +use std::sync::Arc; +use std::task::{Context, Poll}; +use cid::Version; +use codec::Encode; +use core::pin::Pin; +use futures::Future; +use futures::io::{AsyncRead, AsyncWrite}; +use libp2p::core::{ + connection::ConnectionId, Multiaddr, PeerId, + upgrade, InboundUpgrade, OutboundUpgrade, UpgradeInfo, +}; +use libp2p::swarm::{ + NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, + ProtocolsHandler, IntoProtocolsHandler, OneShotHandler, +}; +use log::{error, debug, trace}; +use prost::Message; +use sp_runtime::traits::{Block as BlockT}; +use unsigned_varint::{encode as varint_encode}; +use crate::chain::Client; +use crate::schema::bitswap::{ + Message as BitswapMessage, + message::{wantlist::WantType, Block as MessageBlock, BlockPresenceType, BlockPresence}, +}; + +const LOG_TARGET: &str = "bitswap"; + +// Undocumented, but according to JS the bitswap messages have a max size of 512*1024 bytes +// https://github.com/ipfs/js-ipfs-bitswap/blob/ +// d8f80408aadab94c962f6b88f343eb9f39fa0fcc/src/decision-engine/index.js#L16 +// We set it to the same value as max substrate protocol message +const MAX_PACKET_SIZE: usize = 16 * 1024 * 1024; + +// Max number of queued responses before denying requests. +const MAX_RESPONSE_QUEUE: usize = 20; +// Max number of blocks per wantlist +const MAX_WANTED_BLOCKS: usize = 16; + +const PROTOCOL_NAME: &'static [u8] = b"/ipfs/bitswap/1.2.0"; + +type FutureResult = Pin> + Send>>; + +/// Bitswap protocol config +#[derive(Clone, Copy, Debug, Default)] +pub struct BitswapConfig; + +impl UpgradeInfo for BitswapConfig { + type Info = &'static [u8]; + type InfoIter = std::iter::Once; + + fn protocol_info(&self) -> Self::InfoIter { + std::iter::once(PROTOCOL_NAME) + } +} + +impl InboundUpgrade for BitswapConfig +where + TSocket: AsyncRead + AsyncWrite + Send + Unpin + 'static, +{ + type Output = BitswapMessage; + type Error = BitswapError; + type Future = FutureResult; + + fn upgrade_inbound(self, mut socket: TSocket, _info: Self::Info) -> Self::Future { + Box::pin(async move { + let packet = upgrade::read_one(&mut socket, MAX_PACKET_SIZE).await?; + let message: BitswapMessage = Message::decode(packet.as_slice())?; + Ok(message) + }) + } +} + +impl UpgradeInfo for BitswapMessage { + type Info = &'static [u8]; + type InfoIter = std::iter::Once; + + fn protocol_info(&self) -> Self::InfoIter { + std::iter::once(PROTOCOL_NAME) + } +} + +impl OutboundUpgrade for BitswapMessage +where + TSocket: AsyncRead + AsyncWrite + Send + Unpin + 'static, +{ + type Output = (); + type Error = io::Error; + type Future = FutureResult; + + fn upgrade_outbound(self, mut socket: TSocket, _info: Self::Info) -> Self::Future { + Box::pin(async move { + let mut data = Vec::with_capacity(self.encoded_len()); + self.encode(&mut data)?; + upgrade::write_one(&mut socket, data).await + }) + } +} + +/// Internal protocol handler event. +#[derive(Debug)] +pub enum HandlerEvent { + /// We received a `BitswapMessage` from a remote. + Request(BitswapMessage), + /// We successfully sent a `BitswapMessage`. + ResponseSent, +} + +impl From for HandlerEvent { + fn from(message: BitswapMessage) -> Self { + Self::Request(message) + } +} + +impl From<()> for HandlerEvent { + fn from(_: ()) -> Self { + Self::ResponseSent + } +} + +/// Prefix represents all metadata of a CID, without the actual content. +#[derive(PartialEq, Eq, Clone, Debug)] +struct Prefix { + /// The version of CID. + pub version: Version, + /// The codec of CID. + pub codec: u64, + /// The multihash type of CID. + pub mh_type: u64, + /// The multihash length of CID. + pub mh_len: u8, +} + +impl Prefix { + /// Convert the prefix to encoded bytes. + pub fn to_bytes(&self) -> Vec { + let mut res = Vec::with_capacity(4); + let mut buf = varint_encode::u64_buffer(); + let version = varint_encode::u64(self.version.into(), &mut buf); + res.extend_from_slice(version); + let mut buf = varint_encode::u64_buffer(); + let codec = varint_encode::u64(self.codec.into(), &mut buf); + res.extend_from_slice(codec); + let mut buf = varint_encode::u64_buffer(); + let mh_type = varint_encode::u64(self.mh_type.into(), &mut buf); + res.extend_from_slice(mh_type); + let mut buf = varint_encode::u64_buffer(); + let mh_len = varint_encode::u64(self.mh_len as u64, &mut buf); + res.extend_from_slice(mh_len); + res + } +} + +/// Network behaviour that handles sending and receiving IPFS blocks. +pub struct Bitswap { + client: Arc>, + ready_blocks: VecDeque<(PeerId, BitswapMessage)>, +} + +impl Bitswap { + /// Create a new instance of the bitswap protocol handler. + pub fn new(client: Arc>) -> Self { + Bitswap { + client, + ready_blocks: Default::default(), + } + } +} + +impl NetworkBehaviour for Bitswap { + type ProtocolsHandler = OneShotHandler; + type OutEvent = void::Void; + + fn new_handler(&mut self) -> Self::ProtocolsHandler { + Default::default() + } + + fn addresses_of_peer(&mut self, _peer: &PeerId) -> Vec { + Vec::new() + } + + fn inject_connected(&mut self, _peer: &PeerId) { + } + + fn inject_disconnected(&mut self, _peer: &PeerId) { + } + + fn inject_event(&mut self, peer: PeerId, _connection: ConnectionId, message: HandlerEvent) { + let request = match message { + HandlerEvent::ResponseSent => return, + HandlerEvent::Request(msg) => msg, + }; + trace!(target: LOG_TARGET, "Received request: {:?} from {}", request, peer); + if self.ready_blocks.len() > MAX_RESPONSE_QUEUE { + debug!(target: LOG_TARGET, "Ignored request: queue is full"); + return; + } + let mut response = BitswapMessage { + wantlist: None, + blocks: Default::default(), + payload: Default::default(), + block_presences: Default::default(), + pending_bytes: 0, + }; + let wantlist = match request.wantlist { + Some(wantlist) => wantlist, + None => { + debug!( + target: LOG_TARGET, + "Unexpected bitswap message from {}", + peer, + ); + return; + } + }; + if wantlist.entries.len() > MAX_WANTED_BLOCKS { + trace!(target: LOG_TARGET, "Ignored request: too many entries"); + return; + } + for entry in wantlist.entries { + let cid = match cid::Cid::read_bytes(entry.block.as_slice()) { + Ok(cid) => cid, + Err(e) => { + trace!(target: LOG_TARGET, "Bad CID {:?}: {:?}", entry.block, e); + continue; + } + }; + if cid.version() != cid::Version::V1 + || cid.hash().code() != u64::from(cid::multihash::Code::Blake2b256) + || cid.hash().size() != 32 + { + debug!(target: LOG_TARGET, "Ignoring unsupported CID {}: {}", peer, cid); + continue + } + let mut hash = B::Hash::default(); + hash.as_mut().copy_from_slice(&cid.hash().digest()[0..32]); + let extrinsic = match self.client.extrinsic(&hash) { + Ok(ex) => ex, + Err(e) => { + error!(target: LOG_TARGET, "Error retrieving extrinsic {}: {}", hash, e); + None + } + }; + match extrinsic { + Some(extrinsic) => { + trace!(target: LOG_TARGET, "Found CID {:?}, hash {:?}", cid, hash); + if entry.want_type == WantType::Block as i32 { + let prefix = Prefix { + version: cid.version(), + codec: cid.codec(), + mh_type: cid.hash().code(), + mh_len: cid.hash().size(), + }; + response.payload.push(MessageBlock { + prefix: prefix.to_bytes(), + data: extrinsic.encode(), + }); + } else { + response.block_presences.push(BlockPresence { + r#type: BlockPresenceType::Have as i32, + cid: cid.to_bytes(), + }); + } + }, + None => { + trace!(target: LOG_TARGET, "Missing CID {:?}, hash {:?}", cid, hash); + if entry.send_dont_have { + response.block_presences.push(BlockPresence { + r#type: BlockPresenceType::DontHave as i32, + cid: cid.to_bytes(), + }); + } + } + } + } + trace!(target: LOG_TARGET, "Response: {:?}", response); + self.ready_blocks.push_back((peer, response)); + } + + fn poll(&mut self, _ctx: &mut Context, _: &mut impl PollParameters) -> Poll< + NetworkBehaviourAction< + <::Handler as ProtocolsHandler>::InEvent, + Self::OutEvent, + >, + > { + if let Some((peer_id, message)) = self.ready_blocks.pop_front() { + return Poll::Ready(NetworkBehaviourAction::NotifyHandler { + peer_id: peer_id.clone(), + handler: NotifyHandler::Any, + event: message, + }) + } + Poll::Pending + } +} + +/// Bitswap protocol error. +#[derive(derive_more::Display, derive_more::From)] +pub enum BitswapError { + /// Protobuf decoding error. + #[display(fmt = "Failed to decode request: {}.", _0)] + DecodeProto(prost::DecodeError), + /// Protobuf encoding error. + #[display(fmt = "Failed to encode response: {}.", _0)] + EncodeProto(prost::EncodeError), + /// Client backend error. + Client(sp_blockchain::Error), + /// Error parsing CID + BadCid(cid::Error), + /// Packet read error. + Read(upgrade::ReadOneError), + /// Error sending response. + #[display(fmt = "Failed to send response.")] + SendResponse, +} diff --git a/client/network/src/block_request_handler.rs b/client/network/src/block_request_handler.rs index 1a6c09eff1303..8faa6a7f6c11b 100644 --- a/client/network/src/block_request_handler.rs +++ b/client/network/src/block_request_handler.rs @@ -39,7 +39,7 @@ const MAX_BLOCKS_IN_RESPONSE: usize = 128; const MAX_BODY_BYTES: usize = 8 * 1024 * 1024; /// Generates a [`ProtocolConfig`] for the block request protocol, refusing incoming requests. -pub fn generate_protocol_config(protocol_id: ProtocolId) -> ProtocolConfig { +pub fn generate_protocol_config(protocol_id: &ProtocolId) -> ProtocolConfig { ProtocolConfig { name: generate_protocol_name(protocol_id).into(), max_request_size: 1024 * 1024, @@ -50,7 +50,10 @@ pub fn generate_protocol_config(protocol_id: ProtocolId) -> ProtocolConfig { } /// Generate the block protocol name from chain specific protocol identifier. -fn generate_protocol_name(protocol_id: ProtocolId) -> String { +// +// Visibility `pub(crate)` to allow `crate::light_client_requests::sender` to generate block request +// protocol name and send block requests. +pub(crate) fn generate_protocol_name(protocol_id: &ProtocolId) -> String { let mut s = String::new(); s.push_str("/"); s.push_str(protocol_id.as_ref()); @@ -66,10 +69,10 @@ pub struct BlockRequestHandler { impl BlockRequestHandler { /// Create a new [`BlockRequestHandler`]. - pub fn new(protocol_id: ProtocolId, client: Arc>) -> (Self, ProtocolConfig) { + pub fn new(protocol_id: &ProtocolId, client: Arc>) -> (Self, ProtocolConfig) { // Rate of arrival multiplied with the waiting time in the queue equals the queue length. // - // An average Polkadot sentry node serves less than 5 requests per second. The 95th percentile + // An average Polkadot node serves less than 5 requests per second. The 95th percentile // serving a request is less than 2 second. Thus one would estimate the queue length to be // below 10. // @@ -82,6 +85,22 @@ impl BlockRequestHandler { (Self { client, request_receiver }, protocol_config) } + /// Run [`BlockRequestHandler`]. + pub async fn run(mut self) { + while let Some(request) = self.request_receiver.next().await { + let IncomingRequest { peer, payload, pending_response } = request; + + match self.handle_request(payload, pending_response) { + Ok(()) => debug!(target: LOG_TARGET, "Handled block request from {}.", peer), + Err(e) => debug!( + target: LOG_TARGET, + "Failed to handle block request from {}: {}", + peer, e, + ), + } + } + } + fn handle_request( &self, payload: Vec, @@ -186,22 +205,6 @@ impl BlockRequestHandler { reputation_changes: Vec::new(), }).map_err(|_| HandleRequestError::SendResponse) } - - /// Run [`BlockRequestHandler`]. - pub async fn run(mut self) { - while let Some(request) = self.request_receiver.next().await { - let IncomingRequest { peer, payload, pending_response } = request; - - match self.handle_request(payload, pending_response) { - Ok(()) => debug!(target: LOG_TARGET, "Handled block request from {}.", peer), - Err(e) => debug!( - target: LOG_TARGET, - "Failed to handle block request from {}: {}", - peer, e, - ), - } - } - } } #[derive(derive_more::Display, derive_more::From)] diff --git a/client/network/src/config.rs b/client/network/src/config.rs index 29d238c368a78..d6d4d9d7162f1 100644 --- a/client/network/src/config.rs +++ b/client/network/src/config.rs @@ -68,6 +68,9 @@ pub struct Params { /// default. pub executor: Option + Send>>) + Send>>, + /// How to spawn the background task dedicated to the transactions handler. + pub transactions_handler_executor: Box + Send>>) + Send>, + /// Network layer configuration. pub network_config: NetworkConfiguration, @@ -111,6 +114,14 @@ pub struct Params { /// [`block_request_handler::BlockRequestHandler::new`] allowing both outgoing and incoming /// requests. pub block_request_protocol_config: RequestResponseConfig, + + /// Request response configuration for the light client request protocol. + /// + /// Can be constructed either via [`light_client_requests::generate_protocol_config`] allowing + /// outgoing but not incoming requests, or constructed via + /// [`light_client_requests::handler::LightClientRequestHandler::new`] allowing both outgoing + /// and incoming requests. + pub light_client_request_protocol_config: RequestResponseConfig, } /// Role of the local node. @@ -120,18 +131,8 @@ pub enum Role { Full, /// Regular light node. Light, - /// Sentry node that guards an authority. Will be reported as "authority" on the wire protocol. - Sentry { - /// Address and identity of the validator nodes that we're guarding. - /// - /// The nodes will be granted some priviledged status. - validators: Vec, - }, /// Actual authority. - Authority { - /// List of public addresses and identities of our sentry nodes. - sentry_nodes: Vec, - } + Authority, } impl Role { @@ -139,12 +140,6 @@ impl Role { pub fn is_authority(&self) -> bool { matches!(self, Role::Authority { .. }) } - - /// True for `Role::Authority` and `Role::Sentry` since they're both - /// announced as having the authority role to the network. - pub fn is_network_authority(&self) -> bool { - matches!(self, Role::Authority { .. } | Role::Sentry { .. }) - } } impl fmt::Display for Role { @@ -152,7 +147,6 @@ impl fmt::Display for Role { match self { Role::Full => write!(f, "FULL"), Role::Light => write!(f, "LIGHT"), - Role::Sentry { .. } => write!(f, "SENTRY"), Role::Authority { .. } => write!(f, "AUTHORITY"), } } @@ -400,11 +394,20 @@ pub struct NetworkConfiguration { pub transport: TransportConfig, /// Maximum number of peers to ask the same blocks in parallel. pub max_parallel_downloads: u32, + + /// True if Kademlia random discovery should be enabled. + /// + /// If true, the node will automatically randomly walk the DHT in order to find new peers. + pub enable_dht_random_walk: bool, + /// Should we insert non-global addresses into the DHT? pub allow_non_globals_in_dht: bool, - /// Require iterative Kademlia DHT queries to use disjoint paths for increased resiliency in the - /// presence of potentially adversarial nodes. + + /// Require iterative Kademlia DHT queries to use disjoint paths for increased resiliency in + /// the presence of potentially adversarial nodes. pub kademlia_disjoint_query_paths: bool, + /// Enable serving block data over IPFS bitswap. + pub ipfs_server: bool, /// Size of Yamux receive window of all substreams. `None` for the default (256kiB). /// Any value less than 256kiB is invalid. @@ -453,9 +456,11 @@ impl NetworkConfiguration { wasm_external_transport: None, }, max_parallel_downloads: 5, + enable_dht_random_walk: true, allow_non_globals_in_dht: false, kademlia_disjoint_query_paths: false, yamux_window_size: None, + ipfs_server: false, } } diff --git a/client/network/src/discovery.rs b/client/network/src/discovery.rs index d9d28569ad30b..87b533ef77dc1 100644 --- a/client/network/src/discovery.rs +++ b/client/network/src/discovery.rs @@ -80,6 +80,7 @@ const MAX_KNOWN_EXTERNAL_ADDRESSES: usize = 32; pub struct DiscoveryConfig { local_peer_id: PeerId, user_defined: Vec<(PeerId, Multiaddr)>, + dht_random_walk: bool, allow_private_ipv4: bool, allow_non_globals_in_dht: bool, discovery_only_if_under_num: u64, @@ -94,6 +95,7 @@ impl DiscoveryConfig { DiscoveryConfig { local_peer_id: local_public_key.into_peer_id(), user_defined: Vec::new(), + dht_random_walk: true, allow_private_ipv4: true, allow_non_globals_in_dht: false, discovery_only_if_under_num: std::u64::MAX, @@ -118,6 +120,13 @@ impl DiscoveryConfig { self } + /// Whether the discovery behaviour should periodically perform a random + /// walk on the DHT to discover peers. + pub fn with_dht_random_walk(&mut self, value: bool) -> &mut Self { + self.dht_random_walk = value; + self + } + /// Should private IPv4 addresses be reported? pub fn allow_private_ipv4(&mut self, value: bool) -> &mut Self { self.allow_private_ipv4 = value; @@ -163,6 +172,7 @@ impl DiscoveryConfig { let DiscoveryConfig { local_peer_id, user_defined, + dht_random_walk, allow_private_ipv4, allow_non_globals_in_dht, discovery_only_if_under_num, @@ -197,7 +207,11 @@ impl DiscoveryConfig { DiscoveryBehaviour { user_defined, kademlias, - next_kad_random_query: Delay::new(Duration::new(0, 0)), + next_kad_random_query: if dht_random_walk { + Some(Delay::new(Duration::new(0, 0))) + } else { + None + }, duration_to_next_kad: Duration::from_secs(1), pending_events: VecDeque::new(), local_peer_id, @@ -229,8 +243,9 @@ pub struct DiscoveryBehaviour { /// Discovers nodes on the local network. #[cfg(not(target_os = "unknown"))] mdns: MdnsWrapper, - /// Stream that fires when we need to perform the next random Kademlia query. - next_kad_random_query: Delay, + /// Stream that fires when we need to perform the next random Kademlia query. `None` if + /// random walking is disabled. + next_kad_random_query: Option, /// After `next_kad_random_query` triggers, the next one triggers after this duration. duration_to_next_kad: Duration, /// Events to return in priority when polled. @@ -434,6 +449,8 @@ pub enum DiscoveryOut { ValuePutFailed(record::Key, Duration), /// Started a random Kademlia query for each DHT identified by the given `ProtocolId`s. + /// + /// Only happens if [`DiscoveryConfig::with_dht_random_walk`] has been configured to `true`. RandomKademliaStarted(Vec), } @@ -602,34 +619,36 @@ impl NetworkBehaviour for DiscoveryBehaviour { } // Poll the stream that fires when we need to start a random Kademlia query. - while let Poll::Ready(_) = self.next_kad_random_query.poll_unpin(cx) { - let actually_started = if self.num_connections < self.discovery_only_if_under_num { - let random_peer_id = PeerId::random(); - debug!(target: "sub-libp2p", - "Libp2p <= Starting random Kademlia request for {:?}", - random_peer_id); - for k in self.kademlias.values_mut() { - k.get_closest_peers(random_peer_id.clone()); + if let Some(next_kad_random_query) = self.next_kad_random_query.as_mut() { + while let Poll::Ready(_) = next_kad_random_query.poll_unpin(cx) { + let actually_started = if self.num_connections < self.discovery_only_if_under_num { + let random_peer_id = PeerId::random(); + debug!(target: "sub-libp2p", + "Libp2p <= Starting random Kademlia request for {:?}", + random_peer_id); + for k in self.kademlias.values_mut() { + k.get_closest_peers(random_peer_id.clone()); + } + true + } else { + debug!( + target: "sub-libp2p", + "Kademlia paused due to high number of connections ({})", + self.num_connections + ); + false + }; + + // Schedule the next random query with exponentially increasing delay, + // capped at 60 seconds. + *next_kad_random_query = Delay::new(self.duration_to_next_kad); + self.duration_to_next_kad = cmp::min(self.duration_to_next_kad * 2, + Duration::from_secs(60)); + + if actually_started { + let ev = DiscoveryOut::RandomKademliaStarted(self.kademlias.keys().cloned().collect()); + return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev)); } - true - } else { - debug!( - target: "sub-libp2p", - "Kademlia paused due to high number of connections ({})", - self.num_connections - ); - false - }; - - // Schedule the next random query with exponentially increasing delay, - // capped at 60 seconds. - self.next_kad_random_query = Delay::new(self.duration_to_next_kad); - self.duration_to_next_kad = cmp::min(self.duration_to_next_kad * 2, - Duration::from_secs(60)); - - if actually_started { - let ev = DiscoveryOut::RandomKademliaStarted(self.kademlias.keys().cloned().collect()); - return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev)); } } diff --git a/client/network/src/gossip/tests.rs b/client/network/src/gossip/tests.rs index e0941357e8441..c35159168d0fb 100644 --- a/client/network/src/gossip/tests.rs +++ b/client/network/src/gossip/tests.rs @@ -17,6 +17,7 @@ // along with this program. If not, see . use crate::block_request_handler::BlockRequestHandler; +use crate::light_client_requests::handler::LightClientRequestHandler; use crate::gossip::QueuedSender; use crate::{config, Event, NetworkService, NetworkWorker}; @@ -96,7 +97,16 @@ fn build_test_full_node(network_config: config::NetworkConfiguration) let block_request_protocol_config = { let (handler, protocol_config) = BlockRequestHandler::new( - protocol_id.clone(), + &protocol_id, + client.clone(), + ); + async_std::task::spawn(handler.run().boxed()); + protocol_config + }; + + let light_client_request_protocol_config = { + let (handler, protocol_config) = LightClientRequestHandler::new( + &protocol_id, client.clone(), ); async_std::task::spawn(handler.run().boxed()); @@ -106,6 +116,7 @@ fn build_test_full_node(network_config: config::NetworkConfiguration) let worker = NetworkWorker::new(config::Params { role: config::Role::Full, executor: None, + transactions_handler_executor: Box::new(|task| { async_std::task::spawn(task); }), network_config, chain: client.clone(), on_demand: None, @@ -117,6 +128,7 @@ fn build_test_full_node(network_config: config::NetworkConfiguration) ), metrics_registry: None, block_request_protocol_config, + light_client_request_protocol_config, }) .unwrap(); diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index ab7625ff9fe8a..556e71da23831 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -249,7 +249,6 @@ mod behaviour; mod chain; mod peer_info; mod discovery; -mod light_client_handler; mod on_demand_layer; mod protocol; mod request_responses; @@ -259,17 +258,20 @@ mod transport; mod utils; pub mod block_request_handler; +pub mod bitswap; +pub mod light_client_requests; pub mod config; pub mod error; pub mod gossip; pub mod network_state; +pub mod transactions; #[doc(inline)] pub use libp2p::{multiaddr, Multiaddr, PeerId}; pub use protocol::{event::{DhtEvent, Event, ObservedRole}, sync::SyncState, PeerInfo}; pub use service::{ NetworkService, NetworkWorker, RequestFailure, OutboundFailure, NotificationSender, - NotificationSenderReady, + NotificationSenderReady, IfDisconnected, }; pub use sc_peerset::ReputationChange; diff --git a/client/network/src/light_client_handler.rs b/client/network/src/light_client_handler.rs deleted file mode 100644 index 1062236e25eb3..0000000000000 --- a/client/network/src/light_client_handler.rs +++ /dev/null @@ -1,2061 +0,0 @@ -// This file is part of Substrate. - -// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -//! [`NetworkBehaviour`] implementation which handles light client requests. -//! -//! Every request is coming in on a separate connection substream which gets -//! closed after we have sent the response back. Requests and responses are -//! encoded as protocol buffers (cf. `api.v1.proto`). -//! -//! For every outgoing request we likewise open a separate substream. - -#![allow(unused)] - -use bytes::Bytes; -use codec::{self, Encode, Decode}; -use crate::{ - chain::Client, - config::ProtocolId, - protocol::message::{BlockAttributes, Direction, FromBlock}, - schema, -}; -use futures::{channel::oneshot, future::BoxFuture, prelude::*, stream::FuturesUnordered}; -use libp2p::{ - core::{ - ConnectedPoint, - Multiaddr, - PeerId, - connection::ConnectionId, - upgrade::{InboundUpgrade, ReadOneError, UpgradeInfo, Negotiated}, - upgrade::{OutboundUpgrade, read_one, write_one} - }, - swarm::{ - AddressRecord, - NegotiatedSubstream, - NetworkBehaviour, - NetworkBehaviourAction, - NotifyHandler, - OneShotHandler, - OneShotHandlerConfig, - PollParameters, - SubstreamProtocol, - } -}; -use nohash_hasher::IntMap; -use prost::Message; -use sc_client_api::{ - StorageProof, - light::{ - self, RemoteReadRequest, RemoteBodyRequest, ChangesProof, - RemoteCallRequest, RemoteChangesRequest, RemoteHeaderRequest, - } -}; -use sc_peerset::ReputationChange; -use sp_core::{ - storage::{ChildInfo, ChildType,StorageKey, PrefixedStorageKey}, - hexdisplay::HexDisplay, -}; -use smallvec::SmallVec; -use sp_blockchain::{Error as ClientError}; -use sp_runtime::{ - traits::{Block, Header, NumberFor, Zero}, - generic::BlockId, -}; -use std::{ - collections::{BTreeMap, VecDeque, HashMap}, - iter, - io, - sync::Arc, - time::Duration, - task::{Context, Poll} -}; -use void::Void; -use wasm_timer::Instant; - -/// Reputation change for a peer when a request timed out. -pub(crate) const TIMEOUT_REPUTATION_CHANGE: i32 = -(1 << 8); - -/// Configuration options for `LightClientHandler` behaviour. -#[derive(Debug, Clone)] -pub struct Config { - max_request_size: usize, - max_response_size: usize, - max_pending_requests: usize, - inactivity_timeout: Duration, - request_timeout: Duration, - light_protocol: Bytes, - block_protocol: Bytes, -} - -impl Config { - /// Create a fresh configuration with the following options: - /// - /// - max. request size = 1 MiB - /// - max. response size = 16 MiB - /// - max. pending requests = 128 - /// - inactivity timeout = 15s - /// - request timeout = 15s - pub fn new(id: &ProtocolId) -> Self { - let mut c = Config { - max_request_size: 1 * 1024 * 1024, - max_response_size: 16 * 1024 * 1024, - max_pending_requests: 128, - inactivity_timeout: Duration::from_secs(15), - request_timeout: Duration::from_secs(15), - light_protocol: Bytes::new(), - block_protocol: Bytes::new(), - }; - c.set_protocol(id); - c - } - - /// Limit the max. length in bytes of a request. - pub fn set_max_request_size(&mut self, v: usize) -> &mut Self { - self.max_request_size = v; - self - } - - /// Limit the max. length in bytes of a response. - pub fn set_max_response_size(&mut self, v: usize) -> &mut Self { - self.max_response_size = v; - self - } - - /// Limit the max. number of pending requests. - pub fn set_max_pending_requests(&mut self, v: usize) -> &mut Self { - self.max_pending_requests = v; - self - } - - /// Limit the max. duration the connection may remain inactive before closing it. - pub fn set_inactivity_timeout(&mut self, v: Duration) -> &mut Self { - self.inactivity_timeout = v; - self - } - - /// Limit the max. request duration. - pub fn set_request_timeout(&mut self, v: Duration) -> &mut Self { - self.request_timeout = v; - self - } - - /// Set protocol to use for upgrade negotiation. - pub fn set_protocol(&mut self, id: &ProtocolId) -> &mut Self { - let mut vl = Vec::new(); - vl.extend_from_slice(b"/"); - vl.extend_from_slice(id.as_ref().as_bytes()); - vl.extend_from_slice(b"/light/2"); - self.light_protocol = vl.into(); - - let mut vb = Vec::new(); - vb.extend_from_slice(b"/"); - vb.extend_from_slice(id.as_ref().as_bytes()); - vb.extend_from_slice(b"/sync/2"); - self.block_protocol = vb.into(); - - self - } -} - -/// Possible errors while handling light clients. -#[derive(Debug, thiserror::Error)] -pub enum Error { - /// There are currently too many pending request. - #[error("too many pending requests")] - TooManyRequests, - /// The response type does not correspond to the issued request. - #[error("unexpected response")] - UnexpectedResponse, - /// A bad request has been received. - #[error("bad request: {0}")] - BadRequest(&'static str), - /// The chain client errored. - #[error("client error: {0}")] - Client(#[from] ClientError), - /// Encoding or decoding of some data failed. - #[error("codec error: {0}")] - Codec(#[from] codec::Error), -} - -/// The possible light client requests we support. -/// -/// The associated `oneshot::Sender` will be used to convey the result of -/// their request back to them (cf. `Reply`). -// -// This is modeled after light_dispatch.rs's `RequestData` which is not -// used because we currently only support a subset of those. -#[derive(Debug)] -pub enum Request { - Body { - request: RemoteBodyRequest, - sender: oneshot::Sender, ClientError>> - }, - Header { - request: light::RemoteHeaderRequest, - sender: oneshot::Sender> - }, - Read { - request: light::RemoteReadRequest, - sender: oneshot::Sender, Option>>, ClientError>> - }, - ReadChild { - request: light::RemoteReadChildRequest, - sender: oneshot::Sender, Option>>, ClientError>> - }, - Call { - request: light::RemoteCallRequest, - sender: oneshot::Sender, ClientError>> - }, - Changes { - request: light::RemoteChangesRequest, - sender: oneshot::Sender, u32)>, ClientError>> - } -} - -/// The data to send back to the light client over the oneshot channel. -// -// It is unified here in order to be able to return it as a function -// result instead of delivering it to the client as a side effect of -// response processing. -#[derive(Debug)] -enum Reply { - VecU8(Vec), - VecNumberU32(Vec<(::Number, u32)>), - MapVecU8OptVecU8(HashMap, Option>>), - Header(B::Header), - Extrinsics(Vec), -} - -/// Augments a light client request with metadata. -#[derive(Debug)] -struct RequestWrapper { - /// Time when this value was created. - timestamp: Instant, - /// Remaining retries. - retries: usize, - /// The actual request. - request: Request, - /// The peer to send the request to, e.g. `PeerId`. - peer: P, - /// The connection to use for sending the request. - connection: Option, -} - -/// Information we have about some peer. -#[derive(Debug)] -struct PeerInfo { - connections: SmallVec<[(ConnectionId, Multiaddr); crate::MAX_CONNECTIONS_PER_PEER]>, - best_block: Option>, - status: PeerStatus, -} - -impl Default for PeerInfo { - fn default() -> Self { - PeerInfo { - connections: SmallVec::new(), - best_block: None, - status: PeerStatus::Idle, - } - } -} - -type RequestId = u64; - -/// A peer is either idle or busy processing a request from us. -#[derive(Debug, Clone, PartialEq, Eq)] -enum PeerStatus { - /// The peer is available. - Idle, - /// We wait for the peer to return us a response for the given request ID. - BusyWith(RequestId), -} - -/// The light client handler behaviour. -pub struct LightClientHandler { - /// This behaviour's configuration. - config: Config, - /// Blockchain client. - chain: Arc>, - /// Verifies that received responses are correct. - checker: Arc>, - /// Peer information (addresses, their best block, etc.) - peers: HashMap>, - /// Futures sending back response to remote clients. - responses: FuturesUnordered>, - /// Pending (local) requests. - pending_requests: VecDeque>, - /// Requests on their way to remote peers. - outstanding: IntMap>, - /// (Local) Request ID counter - next_request_id: RequestId, - /// Handle to use for reporting misbehaviour of peers. - peerset: sc_peerset::PeersetHandle, -} - -impl LightClientHandler -where - B: Block, -{ - /// Construct a new light client handler. - pub fn new( - cfg: Config, - chain: Arc>, - checker: Arc>, - peerset: sc_peerset::PeersetHandle, - ) -> Self { - LightClientHandler { - config: cfg, - chain, - checker, - peers: HashMap::new(), - responses: FuturesUnordered::new(), - pending_requests: VecDeque::new(), - outstanding: IntMap::default(), - next_request_id: 1, - peerset, - } - } - - /// We rely on external information about peers best blocks as we lack the - /// means to determine it ourselves. - pub fn update_best_block(&mut self, peer: &PeerId, num: NumberFor) { - if let Some(info) = self.peers.get_mut(peer) { - log::trace!("new best block for {:?}: {:?}", peer, num); - info.best_block = Some(num) - } - } - - /// Issue a new light client request. - pub fn request(&mut self, req: Request) -> Result<(), Error> { - if self.pending_requests.len() >= self.config.max_pending_requests { - return Err(Error::TooManyRequests) - } - let rw = RequestWrapper { - timestamp: Instant::now(), - retries: retries(&req), - request: req, - peer: (), // we do not know the peer yet - connection: None, - }; - self.pending_requests.push_back(rw); - Ok(()) - } - - fn next_request_id(&mut self) -> RequestId { - let id = self.next_request_id; - self.next_request_id += 1; - id - } - - /// Remove the given peer. - /// - /// If we have a request to this peer in flight, we move it back to - /// the pending requests queue. - fn remove_peer(&mut self, peer: &PeerId) { - if let Some(id) = self.outstanding.iter().find(|(_, rw)| &rw.peer == peer).map(|(k, _)| *k) { - let rw = self.outstanding.remove(&id).expect("key belongs to entry in this map"); - let rw = RequestWrapper { - timestamp: rw.timestamp, - retries: rw.retries, - request: rw.request, - peer: (), // need to find another peer - connection: None, - }; - self.pending_requests.push_back(rw); - } - self.peers.remove(peer); - } - - /// Prepares a request by selecting a suitable peer and connection to send it to. - /// - /// If there is currently no suitable peer for the request, the given request - /// is returned as `Err`. - fn prepare_request(&self, req: RequestWrapper) - -> Result<(PeerId, RequestWrapper), RequestWrapper> - { - let number = required_block(&req.request); - - let mut peer = None; - for (peer_id, peer_info) in self.peers.iter() { - if peer_info.status == PeerStatus::Idle { - match peer_info.best_block { - Some(n) => if n >= number { - peer = Some((peer_id, peer_info)); - break - }, - None => peer = Some((peer_id, peer_info)) - } - } - } - - if let Some((peer_id, peer_info)) = peer { - let connection = peer_info.connections.iter().next().map(|(id, _)| *id); - let rw = RequestWrapper { - timestamp: req.timestamp, - retries: req.retries, - request: req.request, - peer: peer_id.clone(), - connection, - }; - Ok((peer_id.clone(), rw)) - } else { - Err(req) - } - } - - /// Process a local request's response from remote. - /// - /// If successful, this will give us the actual, checked data we should be - /// sending back to the client, otherwise an error. - fn on_response - ( &mut self - , peer: &PeerId - , request: &Request - , response: Response - ) -> Result, Error> - { - log::trace!("response from {}", peer); - match response { - Response::Light(r) => self.on_response_light(peer, request, r), - Response::Block(r) => self.on_response_block(peer, request, r), - } - } - - fn on_response_light - ( &mut self - , peer: &PeerId - , request: &Request - , response: schema::v1::light::Response - ) -> Result, Error> - { - use schema::v1::light::response::Response; - match response.response { - Some(Response::RemoteCallResponse(response)) => - if let Request::Call { request , .. } = request { - let proof = Decode::decode(&mut response.proof.as_ref())?; - let reply = self.checker.check_execution_proof(request, proof)?; - Ok(Reply::VecU8(reply)) - } else { - Err(Error::UnexpectedResponse) - } - Some(Response::RemoteReadResponse(response)) => - match request { - Request::Read { request, .. } => { - let proof = Decode::decode(&mut response.proof.as_ref())?; - let reply = self.checker.check_read_proof(&request, proof)?; - Ok(Reply::MapVecU8OptVecU8(reply)) - } - Request::ReadChild { request, .. } => { - let proof = Decode::decode(&mut response.proof.as_ref())?; - let reply = self.checker.check_read_child_proof(&request, proof)?; - Ok(Reply::MapVecU8OptVecU8(reply)) - } - _ => Err(Error::UnexpectedResponse) - } - Some(Response::RemoteChangesResponse(response)) => - if let Request::Changes { request, .. } = request { - let max_block = Decode::decode(&mut response.max.as_ref())?; - let roots_proof = Decode::decode(&mut response.roots_proof.as_ref())?; - let roots = { - let mut r = BTreeMap::new(); - for pair in response.roots { - let k = Decode::decode(&mut pair.fst.as_ref())?; - let v = Decode::decode(&mut pair.snd.as_ref())?; - r.insert(k, v); - } - r - }; - let reply = self.checker.check_changes_proof(&request, light::ChangesProof { - max_block, - proof: response.proof, - roots, - roots_proof, - })?; - Ok(Reply::VecNumberU32(reply)) - } else { - Err(Error::UnexpectedResponse) - } - Some(Response::RemoteHeaderResponse(response)) => - if let Request::Header { request, .. } = request { - let header = - if response.header.is_empty() { - None - } else { - Some(Decode::decode(&mut response.header.as_ref())?) - }; - let proof = Decode::decode(&mut response.proof.as_ref())?; - let reply = self.checker.check_header_proof(&request, header, proof)?; - Ok(Reply::Header(reply)) - } else { - Err(Error::UnexpectedResponse) - } - None => Err(Error::UnexpectedResponse) - } - } - - fn on_response_block - ( &mut self - , peer: &PeerId - , request: &Request - , response: schema::v1::BlockResponse - ) -> Result, Error> - { - let request = if let Request::Body { request , .. } = &request { - request - } else { - return Err(Error::UnexpectedResponse); - }; - - let body: Vec<_> = match response.blocks.into_iter().next() { - Some(b) => b.body, - None => return Err(Error::UnexpectedResponse), - }; - - let body = body.into_iter() - .map(|mut extrinsic| B::Extrinsic::decode(&mut &extrinsic[..])) - .collect::>()?; - - let body = self.checker.check_body_proof(&request, body)?; - Ok(Reply::Extrinsics(body)) - } - - fn on_remote_call_request - ( &mut self - , peer: &PeerId - , request: &schema::v1::light::RemoteCallRequest - ) -> Result - { - log::trace!("remote call request from {} ({} at {:?})", - peer, - request.method, - request.block, - ); - - let block = Decode::decode(&mut request.block.as_ref())?; - - let proof = match self.chain.execution_proof(&BlockId::Hash(block), &request.method, &request.data) { - Ok((_, proof)) => proof, - Err(e) => { - log::trace!("remote call request from {} ({} at {:?}) failed with: {}", - peer, - request.method, - request.block, - e, - ); - StorageProof::empty() - } - }; - - let response = { - let r = schema::v1::light::RemoteCallResponse { proof: proof.encode() }; - schema::v1::light::response::Response::RemoteCallResponse(r) - }; - - Ok(schema::v1::light::Response { response: Some(response) }) - } - - fn on_remote_read_request - ( &mut self - , peer: &PeerId - , request: &schema::v1::light::RemoteReadRequest - ) -> Result - { - if request.keys.is_empty() { - log::debug!("invalid remote read request sent by {}", peer); - return Err(Error::BadRequest("remote read request without keys")) - } - - log::trace!("remote read request from {} ({} at {:?})", - peer, - fmt_keys(request.keys.first(), request.keys.last()), - request.block); - - let block = Decode::decode(&mut request.block.as_ref())?; - - let proof = match self.chain.read_proof(&BlockId::Hash(block), &mut request.keys.iter().map(AsRef::as_ref)) { - Ok(proof) => proof, - Err(error) => { - log::trace!("remote read request from {} ({} at {:?}) failed with: {}", - peer, - fmt_keys(request.keys.first(), request.keys.last()), - request.block, - error); - StorageProof::empty() - } - }; - - let response = { - let r = schema::v1::light::RemoteReadResponse { proof: proof.encode() }; - schema::v1::light::response::Response::RemoteReadResponse(r) - }; - - Ok(schema::v1::light::Response { response: Some(response) }) - } - - fn on_remote_read_child_request - ( &mut self - , peer: &PeerId - , request: &schema::v1::light::RemoteReadChildRequest - ) -> Result - { - if request.keys.is_empty() { - log::debug!("invalid remote child read request sent by {}", peer); - return Err(Error::BadRequest("remove read child request without keys")) - } - - log::trace!("remote read child request from {} ({} {} at {:?})", - peer, - HexDisplay::from(&request.storage_key), - fmt_keys(request.keys.first(), request.keys.last()), - request.block); - - let block = Decode::decode(&mut request.block.as_ref())?; - - let prefixed_key = PrefixedStorageKey::new_ref(&request.storage_key); - let child_info = match ChildType::from_prefixed_key(prefixed_key) { - Some((ChildType::ParentKeyId, storage_key)) => Ok(ChildInfo::new_default(storage_key)), - None => Err(sp_blockchain::Error::InvalidChildStorageKey), - }; - let proof = match child_info.and_then(|child_info| self.chain.read_child_proof( - &BlockId::Hash(block), - &child_info, - &mut request.keys.iter().map(AsRef::as_ref) - )) { - Ok(proof) => proof, - Err(error) => { - log::trace!("remote read child request from {} ({} {} at {:?}) failed with: {}", - peer, - HexDisplay::from(&request.storage_key), - fmt_keys(request.keys.first(), request.keys.last()), - request.block, - error); - StorageProof::empty() - } - }; - - let response = { - let r = schema::v1::light::RemoteReadResponse { proof: proof.encode() }; - schema::v1::light::response::Response::RemoteReadResponse(r) - }; - - Ok(schema::v1::light::Response { response: Some(response) }) - } - - fn on_remote_header_request - ( &mut self - , peer: &PeerId - , request: &schema::v1::light::RemoteHeaderRequest - ) -> Result - { - log::trace!("remote header proof request from {} ({:?})", peer, request.block); - - let block = Decode::decode(&mut request.block.as_ref())?; - let (header, proof) = match self.chain.header_proof(&BlockId::Number(block)) { - Ok((header, proof)) => (header.encode(), proof), - Err(error) => { - log::trace!("remote header proof request from {} ({:?}) failed with: {}", - peer, - request.block, - error); - (Default::default(), StorageProof::empty()) - } - }; - - let response = { - let r = schema::v1::light::RemoteHeaderResponse { header, proof: proof.encode() }; - schema::v1::light::response::Response::RemoteHeaderResponse(r) - }; - - Ok(schema::v1::light::Response { response: Some(response) }) - } - - fn on_remote_changes_request - ( &mut self - , peer: &PeerId - , request: &schema::v1::light::RemoteChangesRequest - ) -> Result - { - log::trace!("remote changes proof request from {} for key {} ({:?}..{:?})", - peer, - if !request.storage_key.is_empty() { - format!("{} : {}", HexDisplay::from(&request.storage_key), HexDisplay::from(&request.key)) - } else { - HexDisplay::from(&request.key).to_string() - }, - request.first, - request.last); - - let first = Decode::decode(&mut request.first.as_ref())?; - let last = Decode::decode(&mut request.last.as_ref())?; - let min = Decode::decode(&mut request.min.as_ref())?; - let max = Decode::decode(&mut request.max.as_ref())?; - let key = StorageKey(request.key.clone()); - let storage_key = if request.storage_key.is_empty() { - None - } else { - Some(PrefixedStorageKey::new_ref(&request.storage_key)) - }; - - let proof = match self.chain.key_changes_proof(first, last, min, max, storage_key, &key) { - Ok(proof) => proof, - Err(error) => { - log::trace!("remote changes proof request from {} for key {} ({:?}..{:?}) failed with: {}", - peer, - format!("{} : {}", HexDisplay::from(&request.storage_key), HexDisplay::from(&key.0)), - request.first, - request.last, - error); - - light::ChangesProof:: { - max_block: Zero::zero(), - proof: Vec::new(), - roots: BTreeMap::new(), - roots_proof: StorageProof::empty(), - } - } - }; - - let response = { - let r = schema::v1::light::RemoteChangesResponse { - max: proof.max_block.encode(), - proof: proof.proof, - roots: proof.roots.into_iter() - .map(|(k, v)| schema::v1::light::Pair { fst: k.encode(), snd: v.encode() }) - .collect(), - roots_proof: proof.roots_proof.encode(), - }; - schema::v1::light::response::Response::RemoteChangesResponse(r) - }; - - Ok(schema::v1::light::Response { response: Some(response) }) - } -} - -impl NetworkBehaviour for LightClientHandler -where - B: Block -{ - type ProtocolsHandler = OneShotHandler>; - type OutEvent = Void; - - fn new_handler(&mut self) -> Self::ProtocolsHandler { - let p = InboundProtocol { - max_request_size: self.config.max_request_size, - protocol: self.config.light_protocol.clone(), - }; - let mut cfg = OneShotHandlerConfig::default(); - cfg.keep_alive_timeout = self.config.inactivity_timeout; - OneShotHandler::new(SubstreamProtocol::new(p, ()), cfg) - } - - fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec { - self.peers.get(peer) - .map(|info| info.connections.iter().map(|(_, a)| a.clone()).collect()) - .unwrap_or_default() - } - - fn inject_connected(&mut self, peer: &PeerId) { - } - - fn inject_connection_established(&mut self, peer: &PeerId, conn: &ConnectionId, info: &ConnectedPoint) { - let peer_address = match info { - ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr.clone(), - ConnectedPoint::Dialer { address } => address.clone() - }; - - log::trace!("peer {} connected with address {}", peer, peer_address); - - let entry = self.peers.entry(peer.clone()).or_default(); - entry.connections.push((*conn, peer_address)); - } - - fn inject_disconnected(&mut self, peer: &PeerId) { - log::trace!("peer {} disconnected", peer); - self.remove_peer(peer) - } - - fn inject_connection_closed(&mut self, peer: &PeerId, conn: &ConnectionId, info: &ConnectedPoint) { - let peer_address = match info { - ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr, - ConnectedPoint::Dialer { address } => address - }; - - log::trace!("connection to peer {} closed: {}", peer, peer_address); - - if let Some(info) = self.peers.get_mut(peer) { - info.connections.retain(|(c, _)| c != conn) - } - - // Add any outstanding requests on the closed connection back to the - // pending requests. - if let Some(id) = self.outstanding.iter() - .find(|(_, rw)| &rw.peer == peer && rw.connection == Some(*conn)) // (*) - .map(|(id, _)| *id) - { - let rw = self.outstanding.remove(&id).expect("by (*)"); - let rw = RequestWrapper { - timestamp: rw.timestamp, - retries: rw.retries, - request: rw.request, - peer: (), // need to find another peer - connection: None, - }; - self.pending_requests.push_back(rw); - } - } - - fn inject_event(&mut self, peer: PeerId, conn: ConnectionId, event: Event) { - match event { - // An incoming request from remote has been received. - Event::Request(request, mut stream) => { - log::trace!("incoming request from {}", peer); - let result = match &request.request { - Some(schema::v1::light::request::Request::RemoteCallRequest(r)) => - self.on_remote_call_request(&peer, r), - Some(schema::v1::light::request::Request::RemoteReadRequest(r)) => - self.on_remote_read_request(&peer, r), - Some(schema::v1::light::request::Request::RemoteHeaderRequest(r)) => - self.on_remote_header_request(&peer, r), - Some(schema::v1::light::request::Request::RemoteReadChildRequest(r)) => - self.on_remote_read_child_request(&peer, r), - Some(schema::v1::light::request::Request::RemoteChangesRequest(r)) => - self.on_remote_changes_request(&peer, r), - None => { - log::debug!("ignoring request without request data from peer {}", peer); - return - } - }; - match result { - Ok(response) => { - log::trace!("enqueueing response for peer {}", peer); - let mut data = Vec::new(); - if let Err(e) = response.encode(&mut data) { - log::debug!("error encoding response for peer {}: {}", peer, e) - } else { - let future = async move { - if let Err(e) = write_one(&mut stream, data).await { - log::debug!("error writing response: {}", e) - } - }; - self.responses.push(future.boxed()) - } - } - Err(Error::BadRequest(_)) => { - self.remove_peer(&peer); - self.peerset.report_peer(peer, ReputationChange::new(-(1 << 12), "bad request")) - } - Err(e) => log::debug!("error handling request from peer {}: {}", peer, e) - } - } - // A response to one of our own requests has been received. - Event::Response(id, response) => { - if let Some(request) = self.outstanding.remove(&id) { - // We first just check if the response originates from the expected peer - // and connection. - if request.peer != peer { - log::debug!("Expected response from {} instead of {}.", request.peer, peer); - self.outstanding.insert(id, request); - self.remove_peer(&peer); - self.peerset.report_peer(peer, ReputationChange::new_fatal("response from unexpected peer")); - return - } - - if let Some(info) = self.peers.get_mut(&peer) { - if info.status != PeerStatus::BusyWith(id) { - // If we get here, something is wrong with our internal handling of peer - // status information. At any time, a single peer processes at most one - // request from us and its status should contain the request ID we are - // expecting a response for. If a peer would send us a response with a - // random ID, we should not have an entry for it with this peer ID in - // our `outstanding` map, so a malicious peer should not be able to get - // us here. It is our own fault and must be fixed! - panic!("unexpected peer status {:?} for {}", info.status, peer); - } - - info.status = PeerStatus::Idle; // Make peer available again. - - match self.on_response(&peer, &request.request, response) { - Ok(reply) => send_reply(Ok(reply), request.request), - Err(Error::UnexpectedResponse) => { - log::debug!("unexpected response {} from peer {}", id, peer); - self.remove_peer(&peer); - self.peerset.report_peer(peer, ReputationChange::new_fatal("unexpected response from peer")); - let rw = RequestWrapper { - timestamp: request.timestamp, - retries: request.retries, - request: request.request, - peer: (), - connection: None, - }; - self.pending_requests.push_back(rw); - } - Err(other) => { - log::debug!("error handling response {} from peer {}: {}", id, peer, other); - self.remove_peer(&peer); - self.peerset.report_peer(peer, ReputationChange::new_fatal("invalid response from peer")); - if request.retries > 0 { - let rw = RequestWrapper { - timestamp: request.timestamp, - retries: request.retries - 1, - request: request.request, - peer: (), - connection: None, - }; - self.pending_requests.push_back(rw) - } else { - send_reply(Err(ClientError::RemoteFetchFailed), request.request) - } - } - } - } else { - // If we get here, something is wrong with our internal handling of peers. - // We apparently have an entry in our `outstanding` map and the peer is the one we - // expected. So, if we can not find an entry for it in our peer information table, - // then these two collections are out of sync which must not happen and is a clear - // programmer error that must be fixed! - panic!("missing peer information for {}; response {}", peer, id); - } - } else { - log::debug!("unexpected response {} from peer {}", id, peer); - self.remove_peer(&peer); - self.peerset.report_peer(peer, ReputationChange::new_fatal("response from unexpected peer")); - } - } - } - } - - fn poll(&mut self, cx: &mut Context, _: &mut impl PollParameters) -> Poll> { - // Process response sending futures. - while let Poll::Ready(Some(_)) = self.responses.poll_next_unpin(cx) {} - - // If we have a pending request to send, try to find an available peer and send it. - let now = Instant::now(); - while let Some(mut request) = self.pending_requests.pop_front() { - if now > request.timestamp + self.config.request_timeout { - if request.retries == 0 { - send_reply(Err(ClientError::RemoteFetchFailed), request.request); - continue - } - request.timestamp = Instant::now(); - request.retries -= 1 - } - - - match self.prepare_request(request) { - Err(request) => { - self.pending_requests.push_front(request); - log::debug!("no peer available to send request to"); - break - } - Ok((peer, request)) => { - let request_bytes = match serialize_request(&request.request) { - Ok(bytes) => bytes, - Err(error) => { - log::debug!("failed to serialize request: {}", error); - send_reply(Err(ClientError::RemoteFetchFailed), request.request); - continue - } - }; - - let (expected, protocol) = match request.request { - Request::Body { .. } => - (ExpectedResponseTy::Block, self.config.block_protocol.clone()), - _ => - (ExpectedResponseTy::Light, self.config.light_protocol.clone()), - }; - - let peer_id = peer.clone(); - let handler = request.connection.map_or(NotifyHandler::Any, NotifyHandler::One); - - let request_id = self.next_request_id(); - if let Some(p) = self.peers.get_mut(&peer) { - p.status = PeerStatus::BusyWith(request_id); - } - self.outstanding.insert(request_id, request); - - let event = OutboundProtocol { - request_id, - request: request_bytes, - expected, - max_response_size: self.config.max_response_size, - protocol, - }; - - log::trace!("sending request {} to peer {}", request_id, peer_id); - - return Poll::Ready(NetworkBehaviourAction::NotifyHandler { - peer_id, - handler, - event, - }) - } - } - } - - // Look for ongoing requests that have timed out. - let mut expired = Vec::new(); - for (id, rw) in &self.outstanding { - if now > rw.timestamp + self.config.request_timeout { - log::debug!("request {} timed out", id); - expired.push(*id) - } - } - for id in expired { - if let Some(rw) = self.outstanding.remove(&id) { - self.remove_peer(&rw.peer); - self.peerset.report_peer(rw.peer.clone(), - ReputationChange::new(TIMEOUT_REPUTATION_CHANGE, "light request timeout")); - if rw.retries == 0 { - send_reply(Err(ClientError::RemoteFetchFailed), rw.request); - continue - } - let rw = RequestWrapper { - timestamp: Instant::now(), - retries: rw.retries - 1, - request: rw.request, - peer: (), - connection: None, - }; - self.pending_requests.push_back(rw) - } - } - - Poll::Pending - } -} - -fn required_block(request: &Request) -> NumberFor { - match request { - Request::Body { request, .. } => *request.header.number(), - Request::Header { request, .. } => request.block, - Request::Read { request, .. } => *request.header.number(), - Request::ReadChild { request, .. } => *request.header.number(), - Request::Call { request, .. } => *request.header.number(), - Request::Changes { request, .. } => request.max_block.0, - } -} - -fn retries(request: &Request) -> usize { - let rc = match request { - Request::Body { request, .. } => request.retry_count, - Request::Header { request, .. } => request.retry_count, - Request::Read { request, .. } => request.retry_count, - Request::ReadChild { request, .. } => request.retry_count, - Request::Call { request, .. } => request.retry_count, - Request::Changes { request, .. } => request.retry_count, - }; - rc.unwrap_or(0) -} - -fn serialize_request(request: &Request) -> Result, prost::EncodeError> { - let request = match request { - Request::Body { request, .. } => { - let rq = schema::v1::BlockRequest { - fields: BlockAttributes::BODY.to_be_u32(), - from_block: Some(schema::v1::block_request::FromBlock::Hash( - request.header.hash().encode(), - )), - to_block: Default::default(), - direction: schema::v1::Direction::Ascending as i32, - max_blocks: 1, - }; - - let mut buf = Vec::with_capacity(rq.encoded_len()); - rq.encode(&mut buf)?; - return Ok(buf); - } - Request::Header { request, .. } => { - let r = schema::v1::light::RemoteHeaderRequest { block: request.block.encode() }; - schema::v1::light::request::Request::RemoteHeaderRequest(r) - } - Request::Read { request, .. } => { - let r = schema::v1::light::RemoteReadRequest { - block: request.block.encode(), - keys: request.keys.clone(), - }; - schema::v1::light::request::Request::RemoteReadRequest(r) - } - Request::ReadChild { request, .. } => { - let r = schema::v1::light::RemoteReadChildRequest { - block: request.block.encode(), - storage_key: request.storage_key.clone().into_inner(), - keys: request.keys.clone(), - }; - schema::v1::light::request::Request::RemoteReadChildRequest(r) - } - Request::Call { request, .. } => { - let r = schema::v1::light::RemoteCallRequest { - block: request.block.encode(), - method: request.method.clone(), - data: request.call_data.clone(), - }; - schema::v1::light::request::Request::RemoteCallRequest(r) - } - Request::Changes { request, .. } => { - let r = schema::v1::light::RemoteChangesRequest { - first: request.first_block.1.encode(), - last: request.last_block.1.encode(), - min: request.tries_roots.1.encode(), - max: request.max_block.1.encode(), - storage_key: request.storage_key.clone().map(|s| s.into_inner()) - .unwrap_or_default(), - key: request.key.clone(), - }; - schema::v1::light::request::Request::RemoteChangesRequest(r) - } - }; - - let rq = schema::v1::light::Request { request: Some(request) }; - let mut buf = Vec::with_capacity(rq.encoded_len()); - rq.encode(&mut buf)?; - Ok(buf) -} - -fn send_reply(result: Result, ClientError>, request: Request) { - fn send(item: T, sender: oneshot::Sender) { - let _ = sender.send(item); // It is okay if the other end already hung up. - } - match request { - Request::Body { request, sender } => match result { - Err(e) => send(Err(e), sender), - Ok(Reply::Extrinsics(x)) => send(Ok(x), sender), - reply => log::error!("invalid reply for body request: {:?}, {:?}", reply, request), - } - Request::Header { request, sender } => match result { - Err(e) => send(Err(e), sender), - Ok(Reply::Header(x)) => send(Ok(x), sender), - reply => log::error!("invalid reply for header request: {:?}, {:?}", reply, request), - } - Request::Read { request, sender } => match result { - Err(e) => send(Err(e), sender), - Ok(Reply::MapVecU8OptVecU8(x)) => send(Ok(x), sender), - reply => log::error!("invalid reply for read request: {:?}, {:?}", reply, request), - } - Request::ReadChild { request, sender } => match result { - Err(e) => send(Err(e), sender), - Ok(Reply::MapVecU8OptVecU8(x)) => send(Ok(x), sender), - reply => log::error!("invalid reply for read child request: {:?}, {:?}", reply, request), - } - Request::Call { request, sender } => match result { - Err(e) => send(Err(e), sender), - Ok(Reply::VecU8(x)) => send(Ok(x), sender), - reply => log::error!("invalid reply for call request: {:?}, {:?}", reply, request), - } - Request::Changes { request, sender } => match result { - Err(e) => send(Err(e), sender), - Ok(Reply::VecNumberU32(x)) => send(Ok(x), sender), - reply => log::error!("invalid reply for changes request: {:?}, {:?}", reply, request), - } - } -} - -/// Output type of inbound and outbound substream upgrades. -#[derive(Debug)] -pub enum Event { - /// Incoming request from remote and substream to use for the response. - Request(schema::v1::light::Request, T), - /// Incoming response from remote. - Response(RequestId, Response), -} - -/// Incoming response from remote. -#[derive(Debug, Clone)] -pub enum Response { - /// Incoming light response from remote. - Light(schema::v1::light::Response), - /// Incoming block response from remote. - Block(schema::v1::BlockResponse), -} - -/// Substream upgrade protocol. -/// -/// Reads incoming requests from remote. -#[derive(Debug, Clone)] -pub struct InboundProtocol { - /// The max. request length in bytes. - max_request_size: usize, - /// The protocol to use for upgrade negotiation. - protocol: Bytes, -} - -impl UpgradeInfo for InboundProtocol { - type Info = Bytes; - type InfoIter = iter::Once; - - fn protocol_info(&self) -> Self::InfoIter { - iter::once(self.protocol.clone()) - } -} - -impl InboundUpgrade for InboundProtocol -where - T: AsyncRead + AsyncWrite + Unpin + Send + 'static -{ - type Output = Event; - type Error = ReadOneError; - type Future = BoxFuture<'static, Result>; - - fn upgrade_inbound(self, mut s: T, _: Self::Info) -> Self::Future { - let future = async move { - let vec = read_one(&mut s, self.max_request_size).await?; - match schema::v1::light::Request::decode(&vec[..]) { - Ok(r) => Ok(Event::Request(r, s)), - Err(e) => Err(ReadOneError::Io(io::Error::new(io::ErrorKind::Other, e))) - } - }; - future.boxed() - } -} - -/// Substream upgrade protocol. -/// -/// Sends a request to remote and awaits the response. -#[derive(Debug, Clone)] -pub struct OutboundProtocol { - /// The serialized protobuf request. - request: Vec, - /// Local identifier for the request. Used to associate it with a response. - request_id: RequestId, - /// Kind of response expected for this request. - expected: ExpectedResponseTy, - /// The max. response length in bytes. - max_response_size: usize, - /// The protocol to use for upgrade negotiation. - protocol: Bytes, -} - -/// Type of response expected from the remote for this request. -#[derive(Debug, Clone)] -enum ExpectedResponseTy { - Light, - Block, -} - -impl UpgradeInfo for OutboundProtocol { - type Info = Bytes; - type InfoIter = iter::Once; - - fn protocol_info(&self) -> Self::InfoIter { - iter::once(self.protocol.clone()) - } -} - -impl OutboundUpgrade for OutboundProtocol -where - T: AsyncRead + AsyncWrite + Unpin + Send + 'static -{ - type Output = Event; - type Error = ReadOneError; - type Future = BoxFuture<'static, Result>; - - fn upgrade_outbound(self, mut s: T, _: Self::Info) -> Self::Future { - let future = async move { - write_one(&mut s, &self.request).await?; - let vec = read_one(&mut s, self.max_response_size).await?; - - match self.expected { - ExpectedResponseTy::Light => { - schema::v1::light::Response::decode(&vec[..]) - .map(|r| Event::Response(self.request_id, Response::Light(r))) - .map_err(|e| { - ReadOneError::Io(io::Error::new(io::ErrorKind::Other, e)) - }) - }, - ExpectedResponseTy::Block => { - schema::v1::BlockResponse::decode(&vec[..]) - .map(|r| Event::Response(self.request_id, Response::Block(r))) - .map_err(|e| { - ReadOneError::Io(io::Error::new(io::ErrorKind::Other, e)) - }) - } - } - }; - future.boxed() - } -} - -fn fmt_keys(first: Option<&Vec>, last: Option<&Vec>) -> String { - if let (Some(first), Some(last)) = (first, last) { - if first == last { - HexDisplay::from(first).to_string() - } else { - format!("{}..{}", HexDisplay::from(first), HexDisplay::from(last)) - } - } else { - String::from("n/a") - } -} - -#[cfg(test)] -mod tests { - use super::*; - use async_std::task; - use assert_matches::assert_matches; - use codec::Encode; - use crate::{ - chain::Client, - config::ProtocolId, - schema, - }; - use futures::{channel::oneshot, prelude::*}; - use libp2p::{ - PeerId, - Multiaddr, - core::{ - ConnectedPoint, - connection::ConnectionId, - identity, - muxing::{StreamMuxerBox, SubstreamRef}, - transport::{Transport, Boxed, memory::MemoryTransport}, - upgrade - }, - noise::{self, Keypair, X25519, NoiseConfig}, - swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters}, - yamux - }; - use sc_client_api::{StorageProof, RemoteReadChildRequest, FetchChecker}; - use sp_blockchain::{Error as ClientError}; - use sp_core::storage::ChildInfo; - use std::{ - collections::{HashMap, HashSet}, - io, - iter::{self, FromIterator}, - pin::Pin, - sync::Arc, - task::{Context, Poll} - }; - use sp_runtime::{generic::Header, traits::{BlakeTwo256, Block as BlockT, NumberFor}}; - use super::{Event, LightClientHandler, Request, Response, OutboundProtocol, PeerStatus}; - use void::Void; - - type Block = sp_runtime::generic::Block, substrate_test_runtime::Extrinsic>; - type Handler = LightClientHandler; - type Swarm = libp2p::swarm::Swarm; - - fn empty_proof() -> Vec { - StorageProof::empty().encode() - } - - fn make_swarm(ok: bool, ps: sc_peerset::PeersetHandle, cf: super::Config) -> Swarm { - let client = Arc::new(substrate_test_runtime_client::new()); - let checker = Arc::new(DummyFetchChecker { ok, _mark: std::marker::PhantomData }); - let id_key = identity::Keypair::generate_ed25519(); - let dh_key = Keypair::::new().into_authentic(&id_key).unwrap(); - let local_peer = id_key.public().into_peer_id(); - let transport = MemoryTransport::default() - .upgrade(upgrade::Version::V1) - .authenticate(NoiseConfig::xx(dh_key).into_authenticated()) - .multiplex(yamux::YamuxConfig::default()) - .boxed(); - Swarm::new(transport, LightClientHandler::new(cf, client, checker, ps), local_peer) - } - - struct DummyFetchChecker { - ok: bool, - _mark: std::marker::PhantomData - } - - impl light::FetchChecker for DummyFetchChecker { - fn check_header_proof( - &self, - _request: &RemoteHeaderRequest, - header: Option, - _remote_proof: StorageProof, - ) -> Result { - match self.ok { - true if header.is_some() => Ok(header.unwrap()), - _ => Err(ClientError::Backend("Test error".into())), - } - } - - fn check_read_proof( - &self, - request: &RemoteReadRequest, - _: StorageProof, - ) -> Result, Option>>, ClientError> { - match self.ok { - true => Ok(request.keys - .iter() - .cloned() - .map(|k| (k, Some(vec![42]))) - .collect() - ), - false => Err(ClientError::Backend("Test error".into())), - } - } - - fn check_read_child_proof( - &self, - request: &RemoteReadChildRequest, - _: StorageProof, - ) -> Result, Option>>, ClientError> { - match self.ok { - true => Ok(request.keys - .iter() - .cloned() - .map(|k| (k, Some(vec![42]))) - .collect() - ), - false => Err(ClientError::Backend("Test error".into())), - } - } - - fn check_execution_proof( - &self, - _: &RemoteCallRequest, - _: StorageProof, - ) -> Result, ClientError> { - match self.ok { - true => Ok(vec![42]), - false => Err(ClientError::Backend("Test error".into())), - } - } - - fn check_changes_proof( - &self, - _: &RemoteChangesRequest, - _: ChangesProof - ) -> Result, u32)>, ClientError> { - match self.ok { - true => Ok(vec![(100u32.into(), 2)]), - false => Err(ClientError::Backend("Test error".into())), - } - } - - fn check_body_proof( - &self, - _: &RemoteBodyRequest, - body: Vec - ) -> Result, ClientError> { - match self.ok { - true => Ok(body), - false => Err(ClientError::Backend("Test error".into())), - } - } - } - - fn make_config() -> super::Config { - super::Config::new(&ProtocolId::from("foo")) - } - - fn dummy_header() -> sp_test_primitives::Header { - sp_test_primitives::Header { - parent_hash: Default::default(), - number: 0, - state_root: Default::default(), - extrinsics_root: Default::default(), - digest: Default::default(), - } - } - - struct EmptyPollParams(PeerId); - - impl PollParameters for EmptyPollParams { - type SupportedProtocolsIter = iter::Empty>; - type ListenedAddressesIter = iter::Empty; - type ExternalAddressesIter = iter::Empty; - - fn supported_protocols(&self) -> Self::SupportedProtocolsIter { - iter::empty() - } - - fn listened_addresses(&self) -> Self::ListenedAddressesIter { - iter::empty() - } - - fn external_addresses(&self) -> Self::ExternalAddressesIter { - iter::empty() - } - - fn local_peer_id(&self) -> &PeerId { - &self.0 - } - } - - fn peerset() -> (sc_peerset::Peerset, sc_peerset::PeersetHandle) { - let cfg = sc_peerset::SetConfig { - in_peers: 128, - out_peers: 128, - bootnodes: Default::default(), - reserved_only: false, - reserved_nodes: Default::default(), - }; - sc_peerset::Peerset::from_config(sc_peerset::PeersetConfig{ sets: vec![cfg] }) - } - - fn make_behaviour - ( ok: bool - , ps: sc_peerset::PeersetHandle - , cf: super::Config - ) -> LightClientHandler - { - let client = Arc::new(substrate_test_runtime_client::new()); - let checker = Arc::new(DummyFetchChecker { ok, _mark: std::marker::PhantomData }); - LightClientHandler::new(cf, client, checker, ps) - } - - fn empty_dialer() -> ConnectedPoint { - ConnectedPoint::Dialer { address: Multiaddr::empty() } - } - - fn poll(mut b: &mut LightClientHandler) -> Poll> { - let mut p = EmptyPollParams(PeerId::random()); - match future::poll_fn(|cx| Pin::new(&mut b).poll(cx, &mut p)).now_or_never() { - Some(a) => Poll::Ready(a), - None => Poll::Pending - } - } - - #[test] - fn disconnects_from_peer_if_told() { - let peer = PeerId::random(); - let pset = peerset(); - let mut behaviour = make_behaviour(true, pset.1, make_config()); - - behaviour.inject_connection_established(&peer, &ConnectionId::new(1), &empty_dialer()); - behaviour.inject_connected(&peer); - assert_eq!(1, behaviour.peers.len()); - - behaviour.inject_connection_closed(&peer, &ConnectionId::new(1), &empty_dialer()); - behaviour.inject_disconnected(&peer); - assert_eq!(0, behaviour.peers.len()) - } - - #[test] - fn disconnects_from_peer_if_request_times_out() { - let peer0 = PeerId::random(); - let peer1 = PeerId::random(); - let pset = peerset(); - let mut behaviour = make_behaviour(true, pset.1, make_config()); - - behaviour.inject_connection_established(&peer0, &ConnectionId::new(1), &empty_dialer()); - behaviour.inject_connected(&peer0); - behaviour.inject_connection_established(&peer1, &ConnectionId::new(2), &empty_dialer()); - behaviour.inject_connected(&peer1); - - // We now know about two peers. - assert_eq!(HashSet::from_iter(&[peer0.clone(), peer1.clone()]), behaviour.peers.keys().collect::>()); - - // No requests have been made yet. - assert!(behaviour.pending_requests.is_empty()); - assert!(behaviour.outstanding.is_empty()); - - // Issue our first request! - let chan = oneshot::channel(); - let request = light::RemoteCallRequest { - block: Default::default(), - header: dummy_header(), - method: "test".into(), - call_data: vec![], - retry_count: Some(1), - }; - behaviour.request(Request::Call { request, sender: chan.0 }).unwrap(); - assert_eq!(1, behaviour.pending_requests.len()); - - // The behaviour should now attempt to send the request. - assert_matches!(poll(&mut behaviour), Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, .. }) => { - assert!(peer_id == peer0 || peer_id == peer1) - }); - - // And we should have one busy peer. - assert!({ - let (idle, busy): (Vec<_>, Vec<_>) = - behaviour.peers.iter().partition(|(_, info)| info.status == PeerStatus::Idle); - - idle.len() == 1 && busy.len() == 1 - && (idle[0].0 == &peer0 || busy[0].0 == &peer0) - && (idle[0].0 == &peer1 || busy[0].0 == &peer1) - }); - - // No more pending requests, but one should be outstanding. - assert_eq!(0, behaviour.pending_requests.len()); - assert_eq!(1, behaviour.outstanding.len()); - - // We now set back the timestamp of the outstanding request to make it expire. - let request = behaviour.outstanding.values_mut().next().unwrap(); - request.timestamp -= make_config().request_timeout; - - // Make progress, but do not expect some action. - assert_matches!(poll(&mut behaviour), Poll::Pending); - - // The request should have timed out by now and the corresponding peer be removed. - assert_eq!(1, behaviour.peers.len()); - // Since we asked for one retry, the request should be back in the pending queue. - assert_eq!(1, behaviour.pending_requests.len()); - // No other request should be ongoing. - assert_eq!(0, behaviour.outstanding.len()); - } - - #[test] - fn disconnects_from_peer_on_incorrect_response() { - let peer = PeerId::random(); - let pset = peerset(); - let mut behaviour = make_behaviour(false, pset.1, make_config()); - // ^--- Making sure the response data check fails. - - let conn = ConnectionId::new(1); - behaviour.inject_connection_established(&peer, &conn, &empty_dialer()); - behaviour.inject_connected(&peer); - assert_eq!(1, behaviour.peers.len()); - - let chan = oneshot::channel(); - let request = light::RemoteCallRequest { - block: Default::default(), - header: dummy_header(), - method: "test".into(), - call_data: vec![], - retry_count: Some(1), - }; - behaviour.request(Request::Call { request, sender: chan.0 }).unwrap(); - - assert_eq!(1, behaviour.pending_requests.len()); - assert_eq!(0, behaviour.outstanding.len()); - poll(&mut behaviour); // Make progress - assert_eq!(0, behaviour.pending_requests.len()); - assert_eq!(1, behaviour.outstanding.len()); - - let request_id = *behaviour.outstanding.keys().next().unwrap(); - - let response = { - let r = schema::v1::light::RemoteCallResponse { proof: empty_proof() }; - schema::v1::light::Response { - response: Some(schema::v1::light::response::Response::RemoteCallResponse(r)), - } - }; - - behaviour.inject_event(peer.clone(), conn, Event::Response(request_id, Response::Light(response))); - assert!(behaviour.peers.is_empty()); - - poll(&mut behaviour); // More progress - - // The request should be back in the pending queue - assert_eq!(1, behaviour.pending_requests.len()); - assert_eq!(0, behaviour.outstanding.len()); - } - - #[test] - fn disconnects_from_peer_on_unexpected_response() { - let peer = PeerId::random(); - let pset = peerset(); - let mut behaviour = make_behaviour(true, pset.1, make_config()); - - let conn = ConnectionId::new(1); - behaviour.inject_connection_established(&peer, &conn, &empty_dialer()); - behaviour.inject_connected(&peer); - assert_eq!(1, behaviour.peers.len()); - assert_eq!(0, behaviour.pending_requests.len()); - assert_eq!(0, behaviour.outstanding.len()); - - // Some unsolicited response - let response = { - let r = schema::v1::light::RemoteCallResponse { proof: empty_proof() }; - schema::v1::light::Response { - response: Some(schema::v1::light::response::Response::RemoteCallResponse(r)), - } - }; - - behaviour.inject_event(peer.clone(), conn, Event::Response(2347895932, Response::Light(response))); - - assert!(behaviour.peers.is_empty()); - poll(&mut behaviour); - assert_eq!(0, behaviour.pending_requests.len()); - assert_eq!(0, behaviour.outstanding.len()); - } - - #[test] - fn disconnects_from_peer_on_wrong_response_type() { - let peer = PeerId::random(); - let pset = peerset(); - let mut behaviour = make_behaviour(true, pset.1, make_config()); - - let conn = ConnectionId::new(1); - behaviour.inject_connection_established(&peer, &conn, &empty_dialer()); - behaviour.inject_connected(&peer); - assert_eq!(1, behaviour.peers.len()); - - let chan = oneshot::channel(); - let request = light::RemoteCallRequest { - block: Default::default(), - header: dummy_header(), - method: "test".into(), - call_data: vec![], - retry_count: Some(1), - }; - behaviour.request(Request::Call { request, sender: chan.0 }).unwrap(); - - assert_eq!(1, behaviour.pending_requests.len()); - assert_eq!(0, behaviour.outstanding.len()); - poll(&mut behaviour); // Make progress - assert_eq!(0, behaviour.pending_requests.len()); - assert_eq!(1, behaviour.outstanding.len()); - - let request_id = *behaviour.outstanding.keys().next().unwrap(); - - let response = { - let r = schema::v1::light::RemoteReadResponse { proof: empty_proof() }; // Not a RemoteCallResponse! - schema::v1::light::Response { - response: Some(schema::v1::light::response::Response::RemoteReadResponse(r)), - } - }; - - behaviour.inject_event(peer.clone(), conn, Event::Response(request_id, Response::Light(response))); - assert!(behaviour.peers.is_empty()); - - poll(&mut behaviour); // More progress - - // The request should be back in the pending queue - assert_eq!(1, behaviour.pending_requests.len()); - assert_eq!(0, behaviour.outstanding.len()); - } - - #[test] - fn receives_remote_failure_after_retry_count_failures() { - let peer1 = PeerId::random(); - let peer2 = PeerId::random(); - let peer3 = PeerId::random(); - let peer4 = PeerId::random(); - let pset = peerset(); - let mut behaviour = make_behaviour(false, pset.1, make_config()); - // ^--- Making sure the response data check fails. - - let conn1 = ConnectionId::new(1); - behaviour.inject_connection_established(&peer1, &conn1, &empty_dialer()); - behaviour.inject_connected(&peer1); - let conn2 = ConnectionId::new(2); - behaviour.inject_connection_established(&peer2, &conn2, &empty_dialer()); - behaviour.inject_connected(&peer2); - let conn3 = ConnectionId::new(3); - behaviour.inject_connection_established(&peer3, &conn3, &empty_dialer()); - behaviour.inject_connected(&peer3); - let conn4 = ConnectionId::new(3); - behaviour.inject_connection_established(&peer4, &conn4, &empty_dialer()); - behaviour.inject_connected(&peer4); - assert_eq!(4, behaviour.peers.len()); - - let mut chan = oneshot::channel(); - let request = light::RemoteCallRequest { - block: Default::default(), - header: dummy_header(), - method: "test".into(), - call_data: vec![], - retry_count: Some(3), // Attempt up to three retries. - }; - behaviour.request(Request::Call { request, sender: chan.0 }).unwrap(); - - assert_eq!(1, behaviour.pending_requests.len()); - assert_eq!(0, behaviour.outstanding.len()); - assert_matches!(poll(&mut behaviour), Poll::Ready(NetworkBehaviourAction::NotifyHandler { .. })); - assert_eq!(0, behaviour.pending_requests.len()); - assert_eq!(1, behaviour.outstanding.len()); - - for i in 1 ..= 3 { - // Construct an invalid response - let request_id = *behaviour.outstanding.keys().next().unwrap(); - let responding_peer = behaviour.outstanding.values().next().unwrap().peer.clone(); - let response = { - let r = schema::v1::light::RemoteCallResponse { proof: empty_proof() }; - schema::v1::light::Response { - response: Some(schema::v1::light::response::Response::RemoteCallResponse(r)) - } - }; - let conn = ConnectionId::new(i); - behaviour.inject_event(responding_peer, conn, Event::Response(request_id, Response::Light(response.clone()))); - assert_matches!(poll(&mut behaviour), Poll::Ready(NetworkBehaviourAction::NotifyHandler { .. })); - assert_matches!(chan.1.try_recv(), Ok(None)) - } - // Final invalid response - let request_id = *behaviour.outstanding.keys().next().unwrap(); - let responding_peer = behaviour.outstanding.values().next().unwrap().peer.clone(); - let response = { - let r = schema::v1::light::RemoteCallResponse { proof: empty_proof() }; - schema::v1::light::Response { - response: Some(schema::v1::light::response::Response::RemoteCallResponse(r)), - } - }; - behaviour.inject_event(responding_peer, conn4, Event::Response(request_id, Response::Light(response))); - assert_matches!(poll(&mut behaviour), Poll::Pending); - assert_matches!(chan.1.try_recv(), Ok(Some(Err(ClientError::RemoteFetchFailed)))) - } - - fn issue_request(request: Request) { - let peer = PeerId::random(); - let pset = peerset(); - let mut behaviour = make_behaviour(true, pset.1, make_config()); - - let conn = ConnectionId::new(1); - behaviour.inject_connection_established(&peer, &conn, &empty_dialer()); - behaviour.inject_connected(&peer); - assert_eq!(1, behaviour.peers.len()); - - let response = match request { - Request::Body { .. } => unimplemented!(), - Request::Header{..} => { - let r = schema::v1::light::RemoteHeaderResponse { - header: dummy_header().encode(), - proof: empty_proof() - }; - schema::v1::light::Response { - response: Some(schema::v1::light::response::Response::RemoteHeaderResponse(r)), - } - } - Request::Read{..} => { - let r = schema::v1::light::RemoteReadResponse { proof: empty_proof() }; - schema::v1::light::Response { - response: Some(schema::v1::light::response::Response::RemoteReadResponse(r)), - } - } - Request::ReadChild{..} => { - let r = schema::v1::light::RemoteReadResponse { proof: empty_proof() }; - schema::v1::light::Response { - response: Some(schema::v1::light::response::Response::RemoteReadResponse(r)), - } - } - Request::Call{..} => { - let r = schema::v1::light::RemoteCallResponse { proof: empty_proof() }; - schema::v1::light::Response { - response: Some(schema::v1::light::response::Response::RemoteCallResponse(r)), - } - } - Request::Changes{..} => { - let r = schema::v1::light::RemoteChangesResponse { - max: iter::repeat(1).take(32).collect(), - proof: Vec::new(), - roots: Vec::new(), - roots_proof: empty_proof() - }; - schema::v1::light::Response { - response: Some(schema::v1::light::response::Response::RemoteChangesResponse(r)), - } - } - }; - - behaviour.request(request).unwrap(); - - assert_eq!(1, behaviour.pending_requests.len()); - assert_eq!(0, behaviour.outstanding.len()); - assert_matches!(poll(&mut behaviour), Poll::Ready(NetworkBehaviourAction::NotifyHandler { .. })); - assert_eq!(0, behaviour.pending_requests.len()); - assert_eq!(1, behaviour.outstanding.len()); - assert_eq!(1, *behaviour.outstanding.keys().next().unwrap()); - - behaviour.inject_event(peer.clone(), conn, Event::Response(1, Response::Light(response))); - - poll(&mut behaviour); - - assert_eq!(0, behaviour.pending_requests.len()); - assert_eq!(0, behaviour.outstanding.len()) - } - - #[test] - fn receives_remote_call_response() { - let mut chan = oneshot::channel(); - let request = light::RemoteCallRequest { - block: Default::default(), - header: dummy_header(), - method: "test".into(), - call_data: vec![], - retry_count: None, - }; - issue_request(Request::Call { request, sender: chan.0 }); - assert_matches!(chan.1.try_recv(), Ok(Some(Ok(_)))) - } - - #[test] - fn receives_remote_read_response() { - let mut chan = oneshot::channel(); - let request = light::RemoteReadRequest { - header: dummy_header(), - block: Default::default(), - keys: vec![b":key".to_vec()], - retry_count: None, - }; - issue_request(Request::Read { request, sender: chan.0 }); - assert_matches!(chan.1.try_recv(), Ok(Some(Ok(_)))) - } - - #[test] - fn receives_remote_read_child_response() { - let mut chan = oneshot::channel(); - let child_info = ChildInfo::new_default(&b":child_storage:default:sub"[..]); - let request = light::RemoteReadChildRequest { - header: dummy_header(), - block: Default::default(), - storage_key: child_info.prefixed_storage_key(), - keys: vec![b":key".to_vec()], - retry_count: None, - }; - issue_request(Request::ReadChild { request, sender: chan.0 }); - assert_matches!(chan.1.try_recv(), Ok(Some(Ok(_)))) - } - - #[test] - fn receives_remote_header_response() { - let mut chan = oneshot::channel(); - let request = light::RemoteHeaderRequest { - cht_root: Default::default(), - block: 1, - retry_count: None, - }; - issue_request(Request::Header { request, sender: chan.0 }); - assert_matches!(chan.1.try_recv(), Ok(Some(Ok(_)))) - } - - #[test] - fn receives_remote_changes_response() { - let mut chan = oneshot::channel(); - let request = light::RemoteChangesRequest { - changes_trie_configs: vec![sp_core::ChangesTrieConfigurationRange { - zero: (0, Default::default()), - end: None, - config: Some(sp_core::ChangesTrieConfiguration::new(4, 2)), - }], - first_block: (1, Default::default()), - last_block: (100, Default::default()), - max_block: (100, Default::default()), - tries_roots: (1, Default::default(), Vec::new()), - key: Vec::new(), - storage_key: None, - retry_count: None, - }; - issue_request(Request::Changes { request, sender: chan.0 }); - assert_matches!(chan.1.try_recv(), Ok(Some(Ok(_)))) - } - - fn send_receive(request: Request) { - // We start a swarm on the listening side which awaits incoming requests and answers them: - let local_pset = peerset(); - let local_listen_addr: libp2p::Multiaddr = libp2p::multiaddr::Protocol::Memory(rand::random()).into(); - let mut local_swarm = make_swarm(true, local_pset.1, make_config()); - Swarm::listen_on(&mut local_swarm, local_listen_addr.clone()).unwrap(); - - // We also start a swarm that makes requests and awaits responses: - let remote_pset = peerset(); - let mut remote_swarm = make_swarm(true, remote_pset.1, make_config()); - - // We now schedule a request, dial the remote and let the two swarm work it out: - remote_swarm.request(request).unwrap(); - Swarm::dial_addr(&mut remote_swarm, local_listen_addr).unwrap(); - - let future = { - let a = local_swarm.for_each(|_| future::ready(())); - let b = remote_swarm.for_each(|_| future::ready(())); - future::join(a, b).map(|_| ()) - }; - - task::spawn(future); - } - - #[test] - fn send_receive_call() { - let chan = oneshot::channel(); - let request = light::RemoteCallRequest { - block: Default::default(), - header: dummy_header(), - method: "test".into(), - call_data: vec![], - retry_count: None, - }; - send_receive(Request::Call { request, sender: chan.0 }); - assert_eq!(vec![42], task::block_on(chan.1).unwrap().unwrap()); - // ^--- from `DummyFetchChecker::check_execution_proof` - } - - #[test] - fn send_receive_read() { - let chan = oneshot::channel(); - let request = light::RemoteReadRequest { - header: dummy_header(), - block: Default::default(), - keys: vec![b":key".to_vec()], - retry_count: None - }; - send_receive(Request::Read { request, sender: chan.0 }); - assert_eq!(Some(vec![42]), task::block_on(chan.1).unwrap().unwrap().remove(&b":key"[..]).unwrap()); - // ^--- from `DummyFetchChecker::check_read_proof` - } - - #[test] - fn send_receive_read_child() { - let chan = oneshot::channel(); - let child_info = ChildInfo::new_default(&b":child_storage:default:sub"[..]); - let request = light::RemoteReadChildRequest { - header: dummy_header(), - block: Default::default(), - storage_key: child_info.prefixed_storage_key(), - keys: vec![b":key".to_vec()], - retry_count: None, - }; - send_receive(Request::ReadChild { request, sender: chan.0 }); - assert_eq!(Some(vec![42]), task::block_on(chan.1).unwrap().unwrap().remove(&b":key"[..]).unwrap()); - // ^--- from `DummyFetchChecker::check_read_child_proof` - } - - #[test] - fn send_receive_header() { - sp_tracing::try_init_simple(); - let chan = oneshot::channel(); - let request = light::RemoteHeaderRequest { - cht_root: Default::default(), - block: 1, - retry_count: None, - }; - send_receive(Request::Header { request, sender: chan.0 }); - // The remote does not know block 1: - assert_matches!(task::block_on(chan.1).unwrap(), Err(ClientError::RemoteFetchFailed)); - } - - #[test] - fn send_receive_changes() { - let chan = oneshot::channel(); - let request = light::RemoteChangesRequest { - changes_trie_configs: vec![sp_core::ChangesTrieConfigurationRange { - zero: (0, Default::default()), - end: None, - config: Some(sp_core::ChangesTrieConfiguration::new(4, 2)), - }], - first_block: (1, Default::default()), - last_block: (100, Default::default()), - max_block: (100, Default::default()), - tries_roots: (1, Default::default(), Vec::new()), - key: Vec::new(), - storage_key: None, - retry_count: None, - }; - send_receive(Request::Changes { request, sender: chan.0 }); - assert_eq!(vec![(100, 2)], task::block_on(chan.1).unwrap().unwrap()); - // ^--- from `DummyFetchChecker::check_changes_proof` - } - - #[test] - fn body_request_fields_encoded_properly() { - let (sender, _) = oneshot::channel(); - let serialized_request = serialize_request::(&Request::Body { - request: RemoteBodyRequest { - header: dummy_header(), - retry_count: None, - }, - sender, - }).unwrap(); - let deserialized_request = schema::v1::BlockRequest::decode(&serialized_request[..]).unwrap(); - assert!( - BlockAttributes::from_be_u32(deserialized_request.fields) - .unwrap() - .contains(BlockAttributes::BODY) - ); - } -} diff --git a/client/network/src/light_client_requests.rs b/client/network/src/light_client_requests.rs new file mode 100644 index 0000000000000..f859a35f45b24 --- /dev/null +++ b/client/network/src/light_client_requests.rs @@ -0,0 +1,334 @@ +// This file is part of Substrate. + +// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! Helpers for outgoing and incoming light client requests. + +/// For outgoing light client requests. +pub mod sender; +/// For incoming light client requests. +pub mod handler; + +use crate::config::ProtocolId; +use crate::request_responses::ProtocolConfig; + +use std::time::Duration; + +/// Generate the light client protocol name from chain specific protocol identifier. +fn generate_protocol_name(protocol_id: &ProtocolId) -> String { + let mut s = String::new(); + s.push_str("/"); + s.push_str(protocol_id.as_ref()); + s.push_str("/light/2"); + s +} + +/// Generates a [`ProtocolConfig`] for the light client request protocol, refusing incoming requests. +pub fn generate_protocol_config(protocol_id: &ProtocolId) -> ProtocolConfig { + ProtocolConfig { + name: generate_protocol_name(protocol_id).into(), + max_request_size: 1 * 1024 * 1024, + max_response_size: 16 * 1024 * 1024, + request_timeout: Duration::from_secs(15), + inbound_queue: None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::request_responses::IncomingRequest; + use crate::config::ProtocolId; + + use assert_matches::assert_matches; + use futures::executor::{block_on, LocalPool}; + use futures::task::Spawn; + use futures::{channel::oneshot, prelude::*}; + use libp2p::PeerId; + use sc_client_api::StorageProof; + use sc_client_api::light::{RemoteCallRequest, RemoteChangesRequest, RemoteHeaderRequest}; + use sc_client_api::light::{self, RemoteReadRequest, RemoteBodyRequest, ChangesProof}; + use sc_client_api::{FetchChecker, RemoteReadChildRequest}; + use sp_blockchain::Error as ClientError; + use sp_core::storage::ChildInfo; + use sp_runtime::generic::Header; + use sp_runtime::traits::{BlakeTwo256, Block as BlockT, NumberFor}; + use std::collections::HashMap; + use std::sync::Arc; + + pub struct DummyFetchChecker { + pub ok: bool, + pub _mark: std::marker::PhantomData, + } + + impl FetchChecker for DummyFetchChecker { + fn check_header_proof( + &self, + _request: &RemoteHeaderRequest, + header: Option, + _remote_proof: StorageProof, + ) -> Result { + match self.ok { + true if header.is_some() => Ok(header.unwrap()), + _ => Err(ClientError::Backend("Test error".into())), + } + } + + fn check_read_proof( + &self, + request: &RemoteReadRequest, + _: StorageProof, + ) -> Result, Option>>, ClientError> { + match self.ok { + true => Ok(request + .keys + .iter() + .cloned() + .map(|k| (k, Some(vec![42]))) + .collect()), + false => Err(ClientError::Backend("Test error".into())), + } + } + + fn check_read_child_proof( + &self, + request: &RemoteReadChildRequest, + _: StorageProof, + ) -> Result, Option>>, ClientError> { + match self.ok { + true => Ok(request + .keys + .iter() + .cloned() + .map(|k| (k, Some(vec![42]))) + .collect()), + false => Err(ClientError::Backend("Test error".into())), + } + } + + fn check_execution_proof( + &self, + _: &RemoteCallRequest, + _: StorageProof, + ) -> Result, ClientError> { + match self.ok { + true => Ok(vec![42]), + false => Err(ClientError::Backend("Test error".into())), + } + } + + fn check_changes_proof( + &self, + _: &RemoteChangesRequest, + _: ChangesProof, + ) -> Result, u32)>, ClientError> { + match self.ok { + true => Ok(vec![(100u32.into(), 2)]), + false => Err(ClientError::Backend("Test error".into())), + } + } + + fn check_body_proof( + &self, + _: &RemoteBodyRequest, + body: Vec, + ) -> Result, ClientError> { + match self.ok { + true => Ok(body), + false => Err(ClientError::Backend("Test error".into())), + } + } + } + + pub fn protocol_id() -> ProtocolId { + ProtocolId::from("test") + } + + pub fn peerset() -> (sc_peerset::Peerset, sc_peerset::PeersetHandle) { + let cfg = sc_peerset::SetConfig { + in_peers: 128, + out_peers: 128, + bootnodes: Default::default(), + reserved_only: false, + reserved_nodes: Default::default(), + }; + sc_peerset::Peerset::from_config(sc_peerset::PeersetConfig { sets: vec![cfg] }) + } + + pub fn dummy_header() -> sp_test_primitives::Header { + sp_test_primitives::Header { + parent_hash: Default::default(), + number: 0, + state_root: Default::default(), + extrinsics_root: Default::default(), + digest: Default::default(), + } + } + + type Block = + sp_runtime::generic::Block, substrate_test_runtime::Extrinsic>; + + fn send_receive(request: sender::Request, pool: &LocalPool) { + let client = Arc::new(substrate_test_runtime_client::new()); + let (handler, protocol_config) = handler::LightClientRequestHandler::new(&protocol_id(), client); + pool.spawner().spawn_obj(handler.run().boxed().into()).unwrap(); + + let (_peer_set, peer_set_handle) = peerset(); + let mut sender = sender::LightClientRequestSender::::new( + &protocol_id(), + Arc::new(crate::light_client_requests::tests::DummyFetchChecker { + ok: true, + _mark: std::marker::PhantomData, + }), + peer_set_handle, + ); + sender.inject_connected(PeerId::random()); + + sender.request(request).unwrap(); + let sender::OutEvent::SendRequest { pending_response, request, .. } = block_on(sender.next()).unwrap(); + let (tx, rx) = oneshot::channel(); + block_on(protocol_config.inbound_queue.unwrap().send(IncomingRequest { + peer: PeerId::random(), + payload: request, + pending_response: tx, + })).unwrap(); + pool.spawner().spawn_obj(async move { + pending_response.send(Ok(rx.await.unwrap().result.unwrap())).unwrap(); + }.boxed().into()).unwrap(); + + pool.spawner().spawn_obj(sender.for_each(|_| future::ready(())).boxed().into()).unwrap(); + } + + #[test] + fn send_receive_call() { + let chan = oneshot::channel(); + let request = light::RemoteCallRequest { + block: Default::default(), + header: dummy_header(), + method: "test".into(), + call_data: vec![], + retry_count: None, + }; + + let mut pool = LocalPool::new(); + send_receive(sender::Request::Call { + request, + sender: chan.0, + }, &pool); + assert_eq!(vec![42], pool.run_until(chan.1).unwrap().unwrap()); + // ^--- from `DummyFetchChecker::check_execution_proof` + } + + #[test] + fn send_receive_read() { + let chan = oneshot::channel(); + let request = light::RemoteReadRequest { + header: dummy_header(), + block: Default::default(), + keys: vec![b":key".to_vec()], + retry_count: None, + }; + let mut pool = LocalPool::new(); + send_receive(sender::Request::Read { + request, + sender: chan.0, + }, &pool); + assert_eq!( + Some(vec![42]), + pool.run_until(chan.1) + .unwrap() + .unwrap() + .remove(&b":key"[..]) + .unwrap() + ); + // ^--- from `DummyFetchChecker::check_read_proof` + } + + #[test] + fn send_receive_read_child() { + let chan = oneshot::channel(); + let child_info = ChildInfo::new_default(&b":child_storage:default:sub"[..]); + let request = light::RemoteReadChildRequest { + header: dummy_header(), + block: Default::default(), + storage_key: child_info.prefixed_storage_key(), + keys: vec![b":key".to_vec()], + retry_count: None, + }; + let mut pool = LocalPool::new(); + send_receive(sender::Request::ReadChild { + request, + sender: chan.0, + }, &pool); + assert_eq!( + Some(vec![42]), + pool.run_until(chan.1) + .unwrap() + .unwrap() + .remove(&b":key"[..]) + .unwrap() + ); + // ^--- from `DummyFetchChecker::check_read_child_proof` + } + + #[test] + fn send_receive_header() { + sp_tracing::try_init_simple(); + let chan = oneshot::channel(); + let request = light::RemoteHeaderRequest { + cht_root: Default::default(), + block: 1, + retry_count: None, + }; + let mut pool = LocalPool::new(); + send_receive(sender::Request::Header { + request, + sender: chan.0, + }, &pool); + // The remote does not know block 1: + assert_matches!( + pool.run_until(chan.1).unwrap(), + Err(ClientError::RemoteFetchFailed) + ); + } + + #[test] + fn send_receive_changes() { + let chan = oneshot::channel(); + let request = light::RemoteChangesRequest { + changes_trie_configs: vec![sp_core::ChangesTrieConfigurationRange { + zero: (0, Default::default()), + end: None, + config: Some(sp_core::ChangesTrieConfiguration::new(4, 2)), + }], + first_block: (1, Default::default()), + last_block: (100, Default::default()), + max_block: (100, Default::default()), + tries_roots: (1, Default::default(), Vec::new()), + key: Vec::new(), + storage_key: None, + retry_count: None, + }; + let mut pool = LocalPool::new(); + send_receive(sender::Request::Changes { + request, + sender: chan.0, + }, &pool); + assert_eq!(vec![(100, 2)], pool.run_until(chan.1).unwrap().unwrap()); + // ^--- from `DummyFetchChecker::check_changes_proof` + } +} diff --git a/client/network/src/light_client_requests/handler.rs b/client/network/src/light_client_requests/handler.rs new file mode 100644 index 0000000000000..08de99a0a5de4 --- /dev/null +++ b/client/network/src/light_client_requests/handler.rs @@ -0,0 +1,399 @@ +// This file is part of Substrate. + +// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! Helper for incoming light client requests. +//! +//! Handle (i.e. answer) incoming light client requests from a remote peer received via +//! [`crate::request_responses::RequestResponsesBehaviour`] with [`LightClientRequestHandler`]. + +use codec::{self, Encode, Decode}; +use crate::{ + chain::Client, + config::ProtocolId, + schema, + PeerId, +}; +use crate::request_responses::{IncomingRequest, OutgoingResponse, ProtocolConfig}; +use futures::{channel::mpsc, prelude::*}; +use prost::Message; +use sc_client_api::{ + StorageProof, + light +}; +use sc_peerset::ReputationChange; +use sp_core::{ + storage::{ChildInfo, ChildType,StorageKey, PrefixedStorageKey}, + hexdisplay::HexDisplay, +}; +use sp_runtime::{ + traits::{Block, Zero}, + generic::BlockId, +}; +use std::{ + collections::{BTreeMap}, + sync::Arc, +}; +use log::debug; + +const LOG_TARGET: &str = "light-client-request-handler"; + +/// Handler for incoming light client requests from a remote peer. +pub struct LightClientRequestHandler { + request_receiver: mpsc::Receiver, + /// Blockchain client. + client: Arc>, +} + +impl LightClientRequestHandler { + /// Create a new [`BlockRequestHandler`]. + pub fn new( + protocol_id: &ProtocolId, + client: Arc>, + ) -> (Self, ProtocolConfig) { + // For now due to lack of data on light client request handling in production systems, this + // value is chosen to match the block request limit. + let (tx, request_receiver) = mpsc::channel(20); + + let mut protocol_config = super::generate_protocol_config(protocol_id); + protocol_config.inbound_queue = Some(tx); + + (Self { client, request_receiver }, protocol_config) + } + + /// Run [`LightClientRequestHandler`]. + pub async fn run(mut self) { + while let Some(request) = self.request_receiver.next().await { + let IncomingRequest { peer, payload, pending_response } = request; + + match self.handle_request(peer, payload) { + Ok(response_data) => { + let response = OutgoingResponse { result: Ok(response_data), reputation_changes: Vec::new() }; + match pending_response.send(response) { + Ok(()) => debug!( + target: LOG_TARGET, + "Handled light client request from {}.", + peer, + ), + Err(_) => debug!( + target: LOG_TARGET, + "Failed to handle light client request from {}: {}", + peer, HandleRequestError::SendResponse, + ), + }; + } , + Err(e) => { + debug!( + target: LOG_TARGET, + "Failed to handle light client request from {}: {}", + peer, e, + ); + + let reputation_changes = match e { + HandleRequestError::BadRequest(_) => { + vec![ReputationChange::new(-(1 << 12), "bad request")] + } + _ => Vec::new(), + }; + + let response = OutgoingResponse { result: Err(()), reputation_changes }; + if pending_response.send(response).is_err() { + debug!( + target: LOG_TARGET, + "Failed to handle light client request from {}: {}", + peer, HandleRequestError::SendResponse, + ); + }; + }, + } + } + } + + + fn handle_request( + &mut self, + peer: PeerId, + payload: Vec, + ) -> Result, HandleRequestError> { + let request = schema::v1::light::Request::decode(&payload[..])?; + + let response = match &request.request { + Some(schema::v1::light::request::Request::RemoteCallRequest(r)) => + self.on_remote_call_request(&peer, r)?, + Some(schema::v1::light::request::Request::RemoteReadRequest(r)) => + self.on_remote_read_request(&peer, r)?, + Some(schema::v1::light::request::Request::RemoteHeaderRequest(r)) => + self.on_remote_header_request(&peer, r)?, + Some(schema::v1::light::request::Request::RemoteReadChildRequest(r)) => + self.on_remote_read_child_request(&peer, r)?, + Some(schema::v1::light::request::Request::RemoteChangesRequest(r)) => + self.on_remote_changes_request(&peer, r)?, + None => { + return Err(HandleRequestError::BadRequest("Remote request without request data.")); + } + }; + + let mut data = Vec::new(); + response.encode(&mut data)?; + + Ok(data) + } + + fn on_remote_call_request( + &mut self, + peer: &PeerId, + request: &schema::v1::light::RemoteCallRequest, + ) -> Result { + log::trace!( + "Remote call request from {} ({} at {:?}).", + peer, request.method, request.block, + ); + + let block = Decode::decode(&mut request.block.as_ref())?; + + let proof = match self.client.execution_proof( + &BlockId::Hash(block), + &request.method, &request.data, + ) { + Ok((_, proof)) => proof, + Err(e) => { + log::trace!( + "remote call request from {} ({} at {:?}) failed with: {}", + peer, request.method, request.block, e, + ); + StorageProof::empty() + } + }; + + let response = { + let r = schema::v1::light::RemoteCallResponse { proof: proof.encode() }; + schema::v1::light::response::Response::RemoteCallResponse(r) + }; + + Ok(schema::v1::light::Response { response: Some(response) }) + } + + fn on_remote_read_request( + &mut self, + peer: &PeerId, + request: &schema::v1::light::RemoteReadRequest, + ) -> Result { + if request.keys.is_empty() { + log::debug!("Invalid remote read request sent by {}.", peer); + return Err(HandleRequestError::BadRequest("Remote read request without keys.")) + } + + log::trace!( + "Remote read request from {} ({} at {:?}).", + peer, fmt_keys(request.keys.first(), request.keys.last()), request.block, + ); + + let block = Decode::decode(&mut request.block.as_ref())?; + + let proof = match self.client.read_proof( + &BlockId::Hash(block), + &mut request.keys.iter().map(AsRef::as_ref), + ) { + Ok(proof) => proof, + Err(error) => { + log::trace!( + "remote read request from {} ({} at {:?}) failed with: {}", + peer, fmt_keys(request.keys.first(), request.keys.last()), request.block, error, + ); + StorageProof::empty() + } + }; + + let response = { + let r = schema::v1::light::RemoteReadResponse { proof: proof.encode() }; + schema::v1::light::response::Response::RemoteReadResponse(r) + }; + + Ok(schema::v1::light::Response { response: Some(response) }) + } + + fn on_remote_read_child_request( + &mut self, + peer: &PeerId, + request: &schema::v1::light::RemoteReadChildRequest, + ) -> Result { + if request.keys.is_empty() { + log::debug!("Invalid remote child read request sent by {}.", peer); + return Err(HandleRequestError::BadRequest("Remove read child request without keys.")) + } + + log::trace!( + "Remote read child request from {} ({} {} at {:?}).", + peer, + HexDisplay::from(&request.storage_key), + fmt_keys(request.keys.first(), request.keys.last()), + request.block, + ); + + let block = Decode::decode(&mut request.block.as_ref())?; + + let prefixed_key = PrefixedStorageKey::new_ref(&request.storage_key); + let child_info = match ChildType::from_prefixed_key(prefixed_key) { + Some((ChildType::ParentKeyId, storage_key)) => Ok(ChildInfo::new_default(storage_key)), + None => Err(sp_blockchain::Error::InvalidChildStorageKey), + }; + let proof = match child_info.and_then(|child_info| self.client.read_child_proof( + &BlockId::Hash(block), + &child_info, + &mut request.keys.iter().map(AsRef::as_ref) + )) { + Ok(proof) => proof, + Err(error) => { + log::trace!( + "remote read child request from {} ({} {} at {:?}) failed with: {}", + peer, + HexDisplay::from(&request.storage_key), + fmt_keys(request.keys.first(), request.keys.last()), + request.block, + error, + ); + StorageProof::empty() + } + }; + + let response = { + let r = schema::v1::light::RemoteReadResponse { proof: proof.encode() }; + schema::v1::light::response::Response::RemoteReadResponse(r) + }; + + Ok(schema::v1::light::Response { response: Some(response) }) + } + + fn on_remote_header_request( + &mut self, + peer: &PeerId, + request: &schema::v1::light::RemoteHeaderRequest, + ) -> Result { + log::trace!("Remote header proof request from {} ({:?}).", peer, request.block); + + let block = Decode::decode(&mut request.block.as_ref())?; + let (header, proof) = match self.client.header_proof(&BlockId::Number(block)) { + Ok((header, proof)) => (header.encode(), proof), + Err(error) => { + log::trace!( + "Remote header proof request from {} ({:?}) failed with: {}.", + peer, request.block, error + ); + (Default::default(), StorageProof::empty()) + } + }; + + let response = { + let r = schema::v1::light::RemoteHeaderResponse { header, proof: proof.encode() }; + schema::v1::light::response::Response::RemoteHeaderResponse(r) + }; + + Ok(schema::v1::light::Response { response: Some(response) }) + } + + fn on_remote_changes_request( + &mut self, + peer: &PeerId, + request: &schema::v1::light::RemoteChangesRequest, + ) -> Result { + log::trace!( + "Remote changes proof request from {} for key {} ({:?}..{:?}).", + peer, + if !request.storage_key.is_empty() { + format!("{} : {}", HexDisplay::from(&request.storage_key), HexDisplay::from(&request.key)) + } else { + HexDisplay::from(&request.key).to_string() + }, + request.first, + request.last, + ); + + let first = Decode::decode(&mut request.first.as_ref())?; + let last = Decode::decode(&mut request.last.as_ref())?; + let min = Decode::decode(&mut request.min.as_ref())?; + let max = Decode::decode(&mut request.max.as_ref())?; + let key = StorageKey(request.key.clone()); + let storage_key = if request.storage_key.is_empty() { + None + } else { + Some(PrefixedStorageKey::new_ref(&request.storage_key)) + }; + + let proof = match self.client.key_changes_proof(first, last, min, max, storage_key, &key) { + Ok(proof) => proof, + Err(error) => { + log::trace!( + "Remote changes proof request from {} for key {} ({:?}..{:?}) failed with: {}.", + peer, + format!("{} : {}", HexDisplay::from(&request.storage_key), HexDisplay::from(&key.0)), + request.first, + request.last, + error, + ); + + light::ChangesProof:: { + max_block: Zero::zero(), + proof: Vec::new(), + roots: BTreeMap::new(), + roots_proof: StorageProof::empty(), + } + } + }; + + let response = { + let r = schema::v1::light::RemoteChangesResponse { + max: proof.max_block.encode(), + proof: proof.proof, + roots: proof.roots.into_iter() + .map(|(k, v)| schema::v1::light::Pair { fst: k.encode(), snd: v.encode() }) + .collect(), + roots_proof: proof.roots_proof.encode(), + }; + schema::v1::light::response::Response::RemoteChangesResponse(r) + }; + + Ok(schema::v1::light::Response { response: Some(response) }) + } +} + +#[derive(derive_more::Display, derive_more::From)] +enum HandleRequestError { + #[display(fmt = "Failed to decode request: {}.", _0)] + DecodeProto(prost::DecodeError), + #[display(fmt = "Failed to encode response: {}.", _0)] + EncodeProto(prost::EncodeError), + #[display(fmt = "Failed to send response.")] + SendResponse, + /// A bad request has been received. + #[display(fmt = "bad request: {}", _0)] + BadRequest(&'static str), + /// Encoding or decoding of some data failed. + #[display(fmt = "codec error: {}", _0)] + Codec(codec::Error), +} + +fn fmt_keys(first: Option<&Vec>, last: Option<&Vec>) -> String { + if let (Some(first), Some(last)) = (first, last) { + if first == last { + HexDisplay::from(first).to_string() + } else { + format!("{}..{}", HexDisplay::from(first), HexDisplay::from(last)) + } + } else { + String::from("n/a") + } +} diff --git a/client/network/src/light_client_requests/sender.rs b/client/network/src/light_client_requests/sender.rs new file mode 100644 index 0000000000000..652f465d6f250 --- /dev/null +++ b/client/network/src/light_client_requests/sender.rs @@ -0,0 +1,1343 @@ +// This file is part of Substrate. + +// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! Helper for outgoing light client requests. +//! +//! Call [`LightClientRequestSender::send_request`] to send out light client requests. It will: +//! +//! 1. Build the request. +//! +//! 2. Forward the request to [`crate::request_responses::RequestResponsesBehaviour`] via +//! [`OutEvent::SendRequest`]. +//! +//! 3. Wait for the response and forward the response via the [`oneshot::Sender`] provided earlier +//! with [`LightClientRequestSender::send_request`]. + +use codec::{self, Encode, Decode}; +use crate::{ + config::ProtocolId, + protocol::message::{BlockAttributes}, + schema, + PeerId, +}; +use crate::request_responses::{RequestFailure, OutboundFailure}; +use futures::{channel::{oneshot}, future::BoxFuture, prelude::*, stream::FuturesUnordered}; +use prost::Message; +use sc_client_api::{ + light::{ + self, RemoteBodyRequest, + } +}; +use sc_peerset::ReputationChange; +use sp_blockchain::{Error as ClientError}; +use sp_runtime::{ + traits::{Block, Header, NumberFor}, +}; +use std::{ + collections::{BTreeMap, VecDeque, HashMap}, + pin::Pin, + sync::Arc, + task::{Context, Poll}, +}; + +mod rep { + use super::*; + + /// Reputation change for a peer when a request timed out. + pub const TIMEOUT: ReputationChange = ReputationChange::new(-(1 << 8), "light client request timeout"); + /// Reputation change for a peer when a request is refused. + pub const REFUSED: ReputationChange = ReputationChange::new(-(1 << 8), "light client request refused"); +} + +/// Configuration options for [`LightClientRequestSender`]. +#[derive(Debug, Clone)] +struct Config { + max_pending_requests: usize, + light_protocol: String, + block_protocol: String, +} + +impl Config { + /// Create a new [`LightClientRequestSender`] configuration. + pub fn new(id: &ProtocolId) -> Self { + Config { + max_pending_requests: 128, + light_protocol: super::generate_protocol_name(id), + block_protocol: crate::block_request_handler::generate_protocol_name(id), + } + } +} + +/// State machine helping to send out light client requests. +pub struct LightClientRequestSender { + /// This behaviour's configuration. + config: Config, + /// Verifies that received responses are correct. + checker: Arc>, + /// Peer information (addresses, their best block, etc.) + peers: HashMap>, + /// Pending (local) requests. + pending_requests: VecDeque>, + /// Requests on their way to remote peers. + sent_requests: FuturesUnordered, Result, RequestFailure>, oneshot::Canceled>), + >>, + /// Handle to use for reporting misbehaviour of peers. + peerset: sc_peerset::PeersetHandle, +} + +/// Augments a pending light client request with metadata. +#[derive(Debug)] +struct PendingRequest { + /// Remaining attempts. + attempts_left: usize, + /// The actual request. + request: Request, +} + +impl PendingRequest { + fn new(req: Request) -> Self { + PendingRequest { + // Number of retries + one for the initial attempt. + attempts_left: req.retries() + 1, + request: req, + } + } + + fn into_sent(self, peer_id: PeerId) -> SentRequest { + SentRequest { + attempts_left: self.attempts_left, + request: self.request, + peer: peer_id, + } + } +} + +/// Augments a light client request with metadata that is currently being send to a remote. +#[derive(Debug)] +struct SentRequest { + /// Remaining attempts. + attempts_left: usize, + /// The actual request. + request: Request, + /// The peer that the request is send to. + peer: PeerId, +} + +impl SentRequest { + fn into_pending(self) -> PendingRequest { + PendingRequest { + attempts_left: self.attempts_left, + request: self.request, + } + } +} + +impl Unpin for LightClientRequestSender {} + +impl LightClientRequestSender +where + B: Block, +{ + /// Construct a new light client handler. + pub fn new( + id: &ProtocolId, + checker: Arc>, + peerset: sc_peerset::PeersetHandle, + ) -> Self { + LightClientRequestSender { + config: Config::new(id), + checker, + peers: Default::default(), + pending_requests: Default::default(), + sent_requests: Default::default(), + peerset, + } + } + + /// We rely on external information about peers best blocks as we lack the + /// means to determine it ourselves. + pub fn update_best_block(&mut self, peer: &PeerId, num: NumberFor) { + if let Some(info) = self.peers.get_mut(peer) { + log::trace!("new best block for {:?}: {:?}", peer, num); + info.best_block = Some(num) + } + } + + /// Issue a new light client request. + pub fn request(&mut self, req: Request) -> Result<(), SendRequestError> { + if self.pending_requests.len() >= self.config.max_pending_requests { + return Err(SendRequestError::TooManyRequests) + } + self.pending_requests.push_back(PendingRequest::new(req)); + Ok(()) + } + + /// Remove the given peer. + /// + /// In-flight requests to the given peer might fail and be retried. See + /// [`::poll_next`]. + fn remove_peer(&mut self, peer: PeerId) { + self.peers.remove(&peer); + } + + /// Process a local request's response from remote. + /// + /// If successful, this will give us the actual, checked data we should be + /// sending back to the client, otherwise an error. + fn on_response( + &mut self, + peer: PeerId, + request: &Request, + response: Response, + ) -> Result, Error> { + log::trace!("response from {}", peer); + match response { + Response::Light(r) => self.on_response_light(request, r), + Response::Block(r) => self.on_response_block(request, r), + } + } + + fn on_response_light( + &mut self, + request: &Request, + response: schema::v1::light::Response, + ) -> Result, Error> { + use schema::v1::light::response::Response; + match response.response { + Some(Response::RemoteCallResponse(response)) => + if let Request::Call { request , .. } = request { + let proof = Decode::decode(&mut response.proof.as_ref())?; + let reply = self.checker.check_execution_proof(request, proof)?; + Ok(Reply::VecU8(reply)) + } else { + Err(Error::UnexpectedResponse) + } + Some(Response::RemoteReadResponse(response)) => + match request { + Request::Read { request, .. } => { + let proof = Decode::decode(&mut response.proof.as_ref())?; + let reply = self.checker.check_read_proof(&request, proof)?; + Ok(Reply::MapVecU8OptVecU8(reply)) + } + Request::ReadChild { request, .. } => { + let proof = Decode::decode(&mut response.proof.as_ref())?; + let reply = self.checker.check_read_child_proof(&request, proof)?; + Ok(Reply::MapVecU8OptVecU8(reply)) + } + _ => Err(Error::UnexpectedResponse) + } + Some(Response::RemoteChangesResponse(response)) => + if let Request::Changes { request, .. } = request { + let max_block = Decode::decode(&mut response.max.as_ref())?; + let roots_proof = Decode::decode(&mut response.roots_proof.as_ref())?; + let roots = { + let mut r = BTreeMap::new(); + for pair in response.roots { + let k = Decode::decode(&mut pair.fst.as_ref())?; + let v = Decode::decode(&mut pair.snd.as_ref())?; + r.insert(k, v); + } + r + }; + let reply = self.checker.check_changes_proof(&request, light::ChangesProof { + max_block, + proof: response.proof, + roots, + roots_proof, + })?; + Ok(Reply::VecNumberU32(reply)) + } else { + Err(Error::UnexpectedResponse) + } + Some(Response::RemoteHeaderResponse(response)) => + if let Request::Header { request, .. } = request { + let header = + if response.header.is_empty() { + None + } else { + Some(Decode::decode(&mut response.header.as_ref())?) + }; + let proof = Decode::decode(&mut response.proof.as_ref())?; + let reply = self.checker.check_header_proof(&request, header, proof)?; + Ok(Reply::Header(reply)) + } else { + Err(Error::UnexpectedResponse) + } + None => Err(Error::UnexpectedResponse) + } + } + + fn on_response_block( + &mut self, + request: &Request, + response: schema::v1::BlockResponse, + ) -> Result, Error> { + let request = if let Request::Body { request , .. } = &request { + request + } else { + return Err(Error::UnexpectedResponse); + }; + + let body: Vec<_> = match response.blocks.into_iter().next() { + Some(b) => b.body, + None => return Err(Error::UnexpectedResponse), + }; + + let body = body.into_iter() + .map(|extrinsic| B::Extrinsic::decode(&mut &extrinsic[..])) + .collect::>()?; + + let body = self.checker.check_body_proof(&request, body)?; + Ok(Reply::Extrinsics(body)) + } + + /// Signal that the node is connected to the given peer. + pub fn inject_connected(&mut self, peer: PeerId) { + let prev_entry = self.peers.insert(peer, Default::default()); + debug_assert!( + prev_entry.is_none(), + "Expect `inject_connected` to be called for disconnected peer.", + ); + } + + /// Signal that the node disconnected from the given peer. + pub fn inject_disconnected(&mut self, peer: PeerId) { + self.remove_peer(peer) + } +} + + +impl Stream for LightClientRequestSender { + type Item = OutEvent; + + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { + // If we have received responses to previously sent requests, check them and pass them on. + while let Poll::Ready(Some((sent_request, request_result))) = self.sent_requests.poll_next_unpin(cx) { + if let Some(info) = self.peers.get_mut(&sent_request.peer) { + if info.status != PeerStatus::Busy { + // If we get here, something is wrong with our internal handling of peer status + // information. At any time, a single peer processes at most one request from + // us. A malicious peer should not be able to get us here. It is our own fault + // and must be fixed! + panic!("unexpected peer status {:?} for {}", info.status, sent_request.peer); + } + + info.status = PeerStatus::Idle; // Make peer available again. + } + + let request_result = match request_result { + Ok(r) => r, + Err(oneshot::Canceled) => { + log::debug!("Oneshot for request to peer {} was canceled.", sent_request.peer); + self.remove_peer(sent_request.peer); + self.peerset.report_peer(sent_request.peer, ReputationChange::new_fatal("no response from peer")); + self.pending_requests.push_back(sent_request.into_pending()); + continue; + } + }; + + let decoded_request_result = request_result.map(|response| { + if sent_request.request.is_block_request() { + schema::v1::BlockResponse::decode(&response[..]) + .map(|r| Response::Block(r)) + } else { + schema::v1::light::Response::decode(&response[..]) + .map(|r| Response::Light(r)) + } + }); + + let response = match decoded_request_result { + Ok(Ok(response)) => response, + Ok(Err(e)) => { + log::debug!("Failed to decode response from peer {}: {:?}.", sent_request.peer, e); + self.remove_peer(sent_request.peer); + self.peerset.report_peer(sent_request.peer, ReputationChange::new_fatal("invalid response from peer")); + self.pending_requests.push_back(sent_request.into_pending()); + continue; + }, + Err(e) => { + log::debug!("Request to peer {} failed with {:?}.", sent_request.peer, e); + + match e { + RequestFailure::NotConnected => { + self.remove_peer(sent_request.peer); + self.pending_requests.push_back(sent_request.into_pending()); + } + RequestFailure::UnknownProtocol => { + debug_assert!( + false, + "Light client and block request protocol should be known when \ + sending requests.", + ); + } + RequestFailure::Refused => { + self.remove_peer(sent_request.peer); + self.peerset.report_peer( + sent_request.peer, + rep::REFUSED, + ); + self.pending_requests.push_back(sent_request.into_pending()); + } + RequestFailure::Obsolete => { + debug_assert!( + false, + "Can not receive `RequestFailure::Obsolete` after dropping the \ + response receiver.", + ); + self.pending_requests.push_back(sent_request.into_pending()); + } + RequestFailure::Network(OutboundFailure::Timeout) => { + self.remove_peer(sent_request.peer); + self.peerset.report_peer( + sent_request.peer, + rep::TIMEOUT, + ); + self.pending_requests.push_back(sent_request.into_pending()); + }, + RequestFailure::Network(OutboundFailure::UnsupportedProtocols) => { + self.remove_peer(sent_request.peer); + self.peerset.report_peer( + sent_request.peer, + ReputationChange::new_fatal( + "peer does not support light client or block request protocol", + ), + ); + self.pending_requests.push_back(sent_request.into_pending()); + } + RequestFailure::Network(OutboundFailure::DialFailure) => { + self.remove_peer(sent_request.peer); + self.peerset.report_peer( + sent_request.peer, + ReputationChange::new_fatal( + "failed to dial peer", + ), + ); + self.pending_requests.push_back(sent_request.into_pending()); + } + RequestFailure::Network(OutboundFailure::ConnectionClosed) => { + self.remove_peer(sent_request.peer); + self.peerset.report_peer( + sent_request.peer, + ReputationChange::new_fatal( + "connection to peer closed", + ), + ); + self.pending_requests.push_back(sent_request.into_pending()); + } + } + + continue; + } + }; + + match self.on_response(sent_request.peer, &sent_request.request, response) { + Ok(reply) => sent_request.request.return_reply(Ok(reply)), + Err(Error::UnexpectedResponse) => { + log::debug!("Unexpected response from peer {}.", sent_request.peer); + self.remove_peer(sent_request.peer); + self.peerset.report_peer( + sent_request.peer, + ReputationChange::new_fatal( + "unexpected response from peer", + ), + ); + self.pending_requests.push_back(sent_request.into_pending()); + } + Err(other) => { + log::debug!("error handling response from peer {}: {}", sent_request.peer, other); + self.remove_peer(sent_request.peer); + self.peerset.report_peer( + sent_request.peer, + ReputationChange::new_fatal( + "invalid response from peer", + ), + ); + self.pending_requests.push_back(sent_request.into_pending()) + } + } + } + + // If we have a pending request to send, try to find an available peer and send it. + while let Some(mut pending_request) = self.pending_requests.pop_front() { + if pending_request.attempts_left == 0 { + pending_request.request.return_reply(Err(ClientError::RemoteFetchFailed)); + continue + } + + let protocol = if pending_request.request.is_block_request() { + self.config.block_protocol.clone() + } else { + self.config.light_protocol.clone() + }; + + // Out of all idle peers, find one who's best block is high enough, choose any idle peer + // if none exists. + let mut peer = None; + for (peer_id, peer_info) in self.peers.iter_mut() { + if peer_info.status == PeerStatus::Idle { + match peer_info.best_block { + Some(n) if n >= pending_request.request.required_block() => { + peer = Some((*peer_id, peer_info)); + break + }, + _ => peer = Some((*peer_id, peer_info)) + } + } + } + + // Break in case there is no idle peer. + let (peer_id, peer_info) = match peer { + Some((peer_id, peer_info)) => (peer_id, peer_info), + None => { + self.pending_requests.push_front(pending_request); + log::debug!("No peer available to send request to."); + + break; + } + }; + + let request_bytes = match pending_request.request.serialize_request() { + Ok(bytes) => bytes, + Err(error) => { + log::debug!("failed to serialize request: {}", error); + pending_request.request.return_reply(Err(ClientError::RemoteFetchFailed)); + continue + } + }; + + let (tx, rx) = oneshot::channel(); + + peer_info.status = PeerStatus::Busy; + + pending_request.attempts_left -= 1; + + self.sent_requests.push(async move { + (pending_request.into_sent(peer_id), rx.await) + }.boxed()); + + return Poll::Ready(Some(OutEvent::SendRequest { + target: peer_id, + request: request_bytes, + pending_response: tx, + protocol_name: protocol, + })); + } + + Poll::Pending + } +} + +/// Events returned by [`LightClientRequestSender`]. +#[derive(Debug)] +pub enum OutEvent { + /// Emit a request to be send out on the network e.g. via [`crate::request_responses`]. + SendRequest { + /// The remote peer to send the request to. + target: PeerId, + /// The encoded request. + request: Vec, + /// The [`onehsot::Sender`] channel to pass the response to. + pending_response: oneshot::Sender, RequestFailure>>, + /// The name of the protocol to use to send the request. + protocol_name: String, + } +} + +/// Incoming response from remote. +#[derive(Debug, Clone)] +pub enum Response { + /// Incoming light response from remote. + Light(schema::v1::light::Response), + /// Incoming block response from remote. + Block(schema::v1::BlockResponse), +} + +/// Error returned by [`LightClientRequestSender::request`]. +#[derive(Debug, derive_more::Display, derive_more::From)] +pub enum SendRequestError { + /// There are currently too many pending request. + #[display(fmt = "too many pending requests")] + TooManyRequests, +} + +/// Error type to propagate errors internally. +#[derive(Debug, derive_more::Display, derive_more::From)] +enum Error { + /// The response type does not correspond to the issued request. + #[display(fmt = "unexpected response")] + UnexpectedResponse, + /// Encoding or decoding of some data failed. + #[display(fmt = "codec error: {}", _0)] + Codec(codec::Error), + /// The chain client errored. + #[display(fmt = "client error: {}", _0)] + Client(ClientError), +} + +/// The data to send back to the light client over the oneshot channel. +// +// It is unified here in order to be able to return it as a function +// result instead of delivering it to the client as a side effect of +// response processing. +#[derive(Debug)] +enum Reply { + VecU8(Vec), + VecNumberU32(Vec<(::Number, u32)>), + MapVecU8OptVecU8(HashMap, Option>>), + Header(B::Header), + Extrinsics(Vec), +} + + +/// Information we have about some peer. +#[derive(Debug)] +struct PeerInfo { + best_block: Option>, + status: PeerStatus, +} + +impl Default for PeerInfo { + fn default() -> Self { + PeerInfo { + best_block: None, + status: PeerStatus::Idle, + } + } +} + +/// A peer is either idle or busy processing a request from us. +#[derive(Debug, Clone, PartialEq, Eq)] +enum PeerStatus { + /// The peer is available. + Idle, + /// We wait for the peer to return us a response for the given request ID. + Busy, +} + +/// The possible light client requests we support. +/// +/// The associated `oneshot::Sender` will be used to convey the result of +/// their request back to them (cf. `Reply`). +// +// This is modeled after light_dispatch.rs's `RequestData` which is not +// used because we currently only support a subset of those. +#[derive(Debug)] +pub enum Request { + /// Remote body request. + Body { + /// Request. + request: RemoteBodyRequest, + /// [`oneshot::Sender`] to return response. + sender: oneshot::Sender, ClientError>> + }, + /// Remote header request. + Header { + /// Request. + request: light::RemoteHeaderRequest, + /// [`oneshot::Sender`] to return response. + sender: oneshot::Sender> + }, + /// Remote read request. + Read { + /// Request. + request: light::RemoteReadRequest, + /// [`oneshot::Sender`] to return response. + sender: oneshot::Sender, Option>>, ClientError>> + }, + /// Remote read child request. + ReadChild { + /// Request. + request: light::RemoteReadChildRequest, + /// [`oneshot::Sender`] to return response. + sender: oneshot::Sender, Option>>, ClientError>> + }, + /// Remote call request. + Call { + /// Request. + request: light::RemoteCallRequest, + /// [`oneshot::Sender`] to return response. + sender: oneshot::Sender, ClientError>> + }, + /// Remote changes request. + Changes { + /// Request. + request: light::RemoteChangesRequest, + /// [`oneshot::Sender`] to return response. + sender: oneshot::Sender, u32)>, ClientError>> + } +} + +impl Request { + fn is_block_request(&self) -> bool { + matches!(self, Request::Body { .. }) + } + + fn required_block(&self) -> NumberFor { + match self { + Request::Body { request, .. } => *request.header.number(), + Request::Header { request, .. } => request.block, + Request::Read { request, .. } => *request.header.number(), + Request::ReadChild { request, .. } => *request.header.number(), + Request::Call { request, .. } => *request.header.number(), + Request::Changes { request, .. } => request.max_block.0, + } + } + + fn retries(&self) -> usize { + let rc = match self { + Request::Body { request, .. } => request.retry_count, + Request::Header { request, .. } => request.retry_count, + Request::Read { request, .. } => request.retry_count, + Request::ReadChild { request, .. } => request.retry_count, + Request::Call { request, .. } => request.retry_count, + Request::Changes { request, .. } => request.retry_count, + }; + rc.unwrap_or(0) + } + + fn serialize_request(&self) -> Result, prost::EncodeError> { + let request = match self { + Request::Body { request, .. } => { + let rq = schema::v1::BlockRequest { + fields: BlockAttributes::BODY.to_be_u32(), + from_block: Some(schema::v1::block_request::FromBlock::Hash( + request.header.hash().encode(), + )), + to_block: Default::default(), + direction: schema::v1::Direction::Ascending as i32, + max_blocks: 1, + }; + + let mut buf = Vec::with_capacity(rq.encoded_len()); + rq.encode(&mut buf)?; + return Ok(buf); + } + Request::Header { request, .. } => { + let r = schema::v1::light::RemoteHeaderRequest { block: request.block.encode() }; + schema::v1::light::request::Request::RemoteHeaderRequest(r) + } + Request::Read { request, .. } => { + let r = schema::v1::light::RemoteReadRequest { + block: request.block.encode(), + keys: request.keys.clone(), + }; + schema::v1::light::request::Request::RemoteReadRequest(r) + } + Request::ReadChild { request, .. } => { + let r = schema::v1::light::RemoteReadChildRequest { + block: request.block.encode(), + storage_key: request.storage_key.clone().into_inner(), + keys: request.keys.clone(), + }; + schema::v1::light::request::Request::RemoteReadChildRequest(r) + } + Request::Call { request, .. } => { + let r = schema::v1::light::RemoteCallRequest { + block: request.block.encode(), + method: request.method.clone(), + data: request.call_data.clone(), + }; + schema::v1::light::request::Request::RemoteCallRequest(r) + } + Request::Changes { request, .. } => { + let r = schema::v1::light::RemoteChangesRequest { + first: request.first_block.1.encode(), + last: request.last_block.1.encode(), + min: request.tries_roots.1.encode(), + max: request.max_block.1.encode(), + storage_key: request.storage_key.clone().map(|s| s.into_inner()) + .unwrap_or_default(), + key: request.key.clone(), + }; + schema::v1::light::request::Request::RemoteChangesRequest(r) + } + }; + + let rq = schema::v1::light::Request { request: Some(request) }; + let mut buf = Vec::with_capacity(rq.encoded_len()); + rq.encode(&mut buf)?; + Ok(buf) + } + + fn return_reply(self, result: Result, ClientError>) { + fn send(item: T, sender: oneshot::Sender) { + let _ = sender.send(item); // It is okay if the other end already hung up. + } + match self { + Request::Body { request, sender } => match result { + Err(e) => send(Err(e), sender), + Ok(Reply::Extrinsics(x)) => send(Ok(x), sender), + reply => log::error!("invalid reply for body request: {:?}, {:?}", reply, request), + } + Request::Header { request, sender } => match result { + Err(e) => send(Err(e), sender), + Ok(Reply::Header(x)) => send(Ok(x), sender), + reply => log::error!("invalid reply for header request: {:?}, {:?}", reply, request), + } + Request::Read { request, sender } => match result { + Err(e) => send(Err(e), sender), + Ok(Reply::MapVecU8OptVecU8(x)) => send(Ok(x), sender), + reply => log::error!("invalid reply for read request: {:?}, {:?}", reply, request), + } + Request::ReadChild { request, sender } => match result { + Err(e) => send(Err(e), sender), + Ok(Reply::MapVecU8OptVecU8(x)) => send(Ok(x), sender), + reply => log::error!("invalid reply for read child request: {:?}, {:?}", reply, request), + } + Request::Call { request, sender } => match result { + Err(e) => send(Err(e), sender), + Ok(Reply::VecU8(x)) => send(Ok(x), sender), + reply => log::error!("invalid reply for call request: {:?}, {:?}", reply, request), + } + Request::Changes { request, sender } => match result { + Err(e) => send(Err(e), sender), + Ok(Reply::VecNumberU32(x)) => send(Ok(x), sender), + reply => log::error!("invalid reply for changes request: {:?}, {:?}", reply, request), + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::light_client_requests::tests::{DummyFetchChecker, protocol_id, peerset, dummy_header}; + use crate::request_responses::OutboundFailure; + + use assert_matches::assert_matches; + use futures::channel::oneshot; + use futures::executor::block_on; + use futures::poll; + use sc_client_api::StorageProof; + use sp_core::storage::ChildInfo; + use sp_runtime::generic::Header; + use sp_runtime::traits::BlakeTwo256; + use std::collections::HashSet; + use std::iter::FromIterator; + + fn empty_proof() -> Vec { + StorageProof::empty().encode() + } + + #[test] + fn removes_peer_if_told() { + let peer = PeerId::random(); + let (_peer_set, peer_set_handle) = peerset(); + let mut sender = LightClientRequestSender::::new( + &protocol_id(), + Arc::new(DummyFetchChecker { + ok: true, + _mark: std::marker::PhantomData, + }), + peer_set_handle, + ); + + sender.inject_connected(peer); + assert_eq!(1, sender.peers.len()); + + sender.inject_disconnected(peer); + assert_eq!(0, sender.peers.len()); + } + + type Block = + sp_runtime::generic::Block, substrate_test_runtime::Extrinsic>; + + #[test] + fn body_request_fields_encoded_properly() { + let (sender, _receiver) = oneshot::channel(); + let request = Request::::Body { + request: RemoteBodyRequest { + header: dummy_header(), + retry_count: None, + }, + sender, + }; + let serialized_request = request.serialize_request().unwrap(); + let deserialized_request = schema::v1::BlockRequest::decode(&serialized_request[..]).unwrap(); + assert!(BlockAttributes::from_be_u32(deserialized_request.fields) + .unwrap() + .contains(BlockAttributes::BODY)); + } + + #[test] + fn disconnects_from_peer_if_request_times_out() { + let peer0 = PeerId::random(); + let peer1 = PeerId::random(); + + let (_peer_set, peer_set_handle) = peerset(); + let mut sender = LightClientRequestSender::::new( + &protocol_id(), + Arc::new(crate::light_client_requests::tests::DummyFetchChecker { + ok: true, + _mark: std::marker::PhantomData, + }), + peer_set_handle, + ); + + sender.inject_connected(peer0); + sender.inject_connected(peer1); + + assert_eq!( + HashSet::from_iter(&[peer0.clone(), peer1.clone()]), + sender.peers.keys().collect::>(), + "Expect knowledge of two peers." + ); + + assert!(sender.pending_requests.is_empty(), "Expect no pending request."); + assert!(sender.sent_requests.is_empty(), "Expect no sent request."); + + // Issue a request! + let chan = oneshot::channel(); + let request = light::RemoteCallRequest { + block: Default::default(), + header: dummy_header(), + method: "test".into(), + call_data: vec![], + retry_count: Some(1), + }; + sender.request(Request::Call { request, sender: chan.0 }).unwrap(); + assert_eq!(1, sender.pending_requests.len(), "Expect one pending request."); + + let OutEvent::SendRequest { target, pending_response, .. } = block_on(sender.next()).unwrap(); + assert!( + target == peer0 || target == peer1, + "Expect request to originate from known peer.", + ); + + // And we should have one busy peer. + assert!({ + let (idle, busy): (Vec<_>, Vec<_>) = sender + .peers + .iter() + .partition(|(_, info)| info.status == PeerStatus::Idle); + idle.len() == 1 + && busy.len() == 1 + && (idle[0].0 == &peer0 || busy[0].0 == &peer0) + && (idle[0].0 == &peer1 || busy[0].0 == &peer1) + }); + + assert_eq!(0, sender.pending_requests.len(), "Expect no pending request."); + assert_eq!(1, sender.sent_requests.len(), "Expect one request to be sent."); + + // Report first attempt as timed out. + pending_response.send(Err(RequestFailure::Network(OutboundFailure::Timeout))).unwrap(); + + // Expect a new request to be issued. + let OutEvent::SendRequest { pending_response, .. } = block_on(sender.next()).unwrap(); + + assert_eq!(1, sender.peers.len(), "Expect peer to be removed."); + assert_eq!(0, sender.pending_requests.len(), "Expect no request to be pending."); + assert_eq!(1, sender.sent_requests.len(), "Expect new request to be issued."); + + // Report second attempt as timed out. + pending_response.send(Err(RequestFailure::Network(OutboundFailure::Timeout))).unwrap(); + assert_matches!( + block_on(async { poll!(sender.next()) }), Poll::Pending, + "Expect sender to not issue another attempt.", + ); + assert_matches!( + block_on(chan.1).unwrap(), Err(ClientError::RemoteFetchFailed), + "Expect request failure to be reported.", + ); + assert_eq!(0, sender.peers.len(), "Expect no peer to be left"); + assert_eq!(0, sender.pending_requests.len(), "Expect no request to be pending."); + assert_eq!(0, sender.sent_requests.len(), "Expect no other request to be in progress."); + } + + #[test] + fn disconnects_from_peer_on_incorrect_response() { + let peer = PeerId::random(); + + let (_peer_set, peer_set_handle) = peerset(); + let mut sender = LightClientRequestSender::::new( + &protocol_id(), + Arc::new(crate::light_client_requests::tests::DummyFetchChecker { + ok: false, + // ^--- Making sure the response data check fails. + _mark: std::marker::PhantomData, + }), + peer_set_handle, + ); + + sender.inject_connected(peer); + assert_eq!(1, sender.peers.len(), "Expect one peer."); + + let chan = oneshot::channel(); + let request = light::RemoteCallRequest { + block: Default::default(), + header: dummy_header(), + method: "test".into(), + call_data: vec![], + retry_count: Some(1), + }; + sender + .request(Request::Call { + request, + sender: chan.0, + }) + .unwrap(); + + assert_eq!(1, sender.pending_requests.len(), "Expect one pending request."); + assert_eq!(0, sender.sent_requests.len(), "Expect zero sent requests."); + + let OutEvent::SendRequest { pending_response, .. } = block_on(sender.next()).unwrap(); + assert_eq!(0, sender.pending_requests.len(), "Expect zero pending requests."); + assert_eq!(1, sender.sent_requests.len(), "Expect one sent request."); + + let response = { + let r = schema::v1::light::RemoteCallResponse { + proof: empty_proof(), + }; + let response = schema::v1::light::Response { + response: Some(schema::v1::light::response::Response::RemoteCallResponse(r)), + }; + let mut data = Vec::new(); + response.encode(&mut data).unwrap(); + data + }; + + pending_response.send(Ok(response)).unwrap(); + + assert_matches!( + block_on(async { poll!(sender.next()) }), Poll::Pending, + "Expect sender to not issue another attempt, given that there is no peer left.", + ); + + assert!(sender.peers.is_empty(), "Expect no peers to be left."); + assert_eq!(1, sender.pending_requests.len(), "Expect request to be pending again."); + assert_eq!(0, sender.sent_requests.len(), "Expect no request to be sent."); + } + + #[test] + fn disconnects_from_peer_on_wrong_response_type() { + let peer = PeerId::random(); + let (_peer_set, peer_set_handle) = peerset(); + let mut sender = LightClientRequestSender::::new( + &protocol_id(), + Arc::new(crate::light_client_requests::tests::DummyFetchChecker { + ok: true, + _mark: std::marker::PhantomData, + }), + peer_set_handle, + ); + + sender.inject_connected(peer); + assert_eq!(1, sender.peers.len(), "Expect one peer."); + + let chan = oneshot::channel(); + let request = light::RemoteCallRequest { + block: Default::default(), + header: dummy_header(), + method: "test".into(), + call_data: vec![], + retry_count: Some(1), + }; + sender + .request(Request::Call { + request, + sender: chan.0, + }) + .unwrap(); + + assert_eq!(1, sender.pending_requests.len()); + assert_eq!(0, sender.sent_requests.len()); + let OutEvent::SendRequest { pending_response, .. } = block_on(sender.next()).unwrap(); + assert_eq!(0, sender.pending_requests.len(), "Expect zero pending requests."); + assert_eq!(1, sender.sent_requests.len(), "Expect one sent request."); + + let response = { + let r = schema::v1::light::RemoteReadResponse { + proof: empty_proof(), + }; // Not a RemoteCallResponse! + let response = schema::v1::light::Response { + response: Some(schema::v1::light::response::Response::RemoteReadResponse(r)), + }; + let mut data = Vec::new(); + response.encode(&mut data).unwrap(); + data + }; + + pending_response.send(Ok(response)).unwrap(); + assert_matches!( + block_on(async { poll!(sender.next()) }), Poll::Pending, + "Expect sender to not issue another attempt, given that there is no peer left.", + ); + + assert!(sender.peers.is_empty(), "Expect no peers to be left."); + assert_eq!(1, sender.pending_requests.len(), "Expect request to be pending again."); + assert_eq!(0, sender.sent_requests.len(), "Expect no request to be sent."); + } + + #[test] + fn receives_remote_failure_after_retry_count_failures() { + let peers = (0..4).map(|_| PeerId::random()).collect::>(); + + let (_peer_set, peer_set_handle) = peerset(); + let mut sender = LightClientRequestSender::::new( + &protocol_id(), + Arc::new(crate::light_client_requests::tests::DummyFetchChecker { + ok: false, + // ^--- Making sure the response data check fails. + _mark: std::marker::PhantomData, + }), + peer_set_handle, + ); + + for peer in &peers { + sender.inject_connected(*peer); + } + assert_eq!(4, sender.peers.len(), "Expect four peers."); + + let mut chan = oneshot::channel(); + let request = light::RemoteCallRequest { + block: Default::default(), + header: dummy_header(), + method: "test".into(), + call_data: vec![], + retry_count: Some(3), // Attempt up to three retries. + }; + sender + .request(Request::Call { + request, + sender: chan.0, + }) + .unwrap(); + + assert_eq!(1, sender.pending_requests.len()); + assert_eq!(0, sender.sent_requests.len()); + let mut pending_response = match block_on(sender.next()).unwrap() { + OutEvent::SendRequest { pending_response, .. } => Some(pending_response), + }; + assert_eq!(0, sender.pending_requests.len(), "Expect zero pending requests."); + assert_eq!(1, sender.sent_requests.len(), "Expect one sent request."); + + for (i, _peer) in peers.iter().enumerate() { + // Construct an invalid response + let response = { + let r = schema::v1::light::RemoteCallResponse { + proof: empty_proof(), + }; + let response = schema::v1::light::Response { + response: Some(schema::v1::light::response::Response::RemoteCallResponse(r)), + }; + let mut data = Vec::new(); + response.encode(&mut data).unwrap(); + data + }; + pending_response.take().unwrap().send(Ok(response)).unwrap(); + + if i < 3 { + pending_response = match block_on(sender.next()).unwrap() { + OutEvent::SendRequest { pending_response, .. } => Some(pending_response), + }; + assert_matches!(chan.1.try_recv(), Ok(None)) + } else { + // Last peer and last attempt. + assert_matches!( + block_on(async { poll!(sender.next()) }), Poll::Pending, + "Expect sender to not issue another attempt, given that there is no peer left.", + ); + assert_matches!( + chan.1.try_recv(), + Ok(Some(Err(ClientError::RemoteFetchFailed))) + ) + } + } + } + + fn issue_request(request: Request) { + let peer = PeerId::random(); + + let (_peer_set, peer_set_handle) = peerset(); + let mut sender = LightClientRequestSender::::new( + &protocol_id(), + Arc::new(crate::light_client_requests::tests::DummyFetchChecker { + ok: true, + _mark: std::marker::PhantomData, + }), + peer_set_handle, + ); + + sender.inject_connected(peer); + assert_eq!(1, sender.peers.len(), "Expect one peer."); + + let response = match request { + Request::Body { .. } => unimplemented!(), + Request::Header { .. } => { + let r = schema::v1::light::RemoteHeaderResponse { + header: dummy_header().encode(), + proof: empty_proof(), + }; + schema::v1::light::Response { + response: Some(schema::v1::light::response::Response::RemoteHeaderResponse( + r, + )), + } + } + Request::Read { .. } => { + let r = schema::v1::light::RemoteReadResponse { + proof: empty_proof(), + }; + schema::v1::light::Response { + response: Some(schema::v1::light::response::Response::RemoteReadResponse(r)), + } + } + Request::ReadChild { .. } => { + let r = schema::v1::light::RemoteReadResponse { + proof: empty_proof(), + }; + schema::v1::light::Response { + response: Some(schema::v1::light::response::Response::RemoteReadResponse(r)), + } + } + Request::Call { .. } => { + let r = schema::v1::light::RemoteCallResponse { + proof: empty_proof(), + }; + schema::v1::light::Response { + response: Some(schema::v1::light::response::Response::RemoteCallResponse(r)), + } + } + Request::Changes { .. } => { + let r = schema::v1::light::RemoteChangesResponse { + max: std::iter::repeat(1).take(32).collect(), + proof: Vec::new(), + roots: Vec::new(), + roots_proof: empty_proof(), + }; + schema::v1::light::Response { + response: Some(schema::v1::light::response::Response::RemoteChangesResponse(r)), + } + } + }; + + let response = { + let mut data = Vec::new(); + response.encode(&mut data).unwrap(); + data + }; + + sender.request(request).unwrap(); + + assert_eq!(1, sender.pending_requests.len()); + assert_eq!(0, sender.sent_requests.len()); + let OutEvent::SendRequest { pending_response, .. } = block_on(sender.next()).unwrap(); + assert_eq!(0, sender.pending_requests.len()); + assert_eq!(1, sender.sent_requests.len()); + + pending_response.send(Ok(response)).unwrap(); + assert_matches!( + block_on(async { poll!(sender.next()) }), Poll::Pending, + "Expect sender to not issue another attempt, given that there is no peer left.", + ); + + assert_eq!(0, sender.pending_requests.len()); + assert_eq!(0, sender.sent_requests.len()) + } + + #[test] + fn receives_remote_call_response() { + let mut chan = oneshot::channel(); + let request = light::RemoteCallRequest { + block: Default::default(), + header: dummy_header(), + method: "test".into(), + call_data: vec![], + retry_count: None, + }; + issue_request(Request::Call { + request, + sender: chan.0, + }); + assert_matches!(chan.1.try_recv(), Ok(Some(Ok(_)))) + } + + #[test] + fn receives_remote_read_response() { + let mut chan = oneshot::channel(); + let request = light::RemoteReadRequest { + header: dummy_header(), + block: Default::default(), + keys: vec![b":key".to_vec()], + retry_count: None, + }; + issue_request(Request::Read { + request, + sender: chan.0, + }); + assert_matches!(chan.1.try_recv(), Ok(Some(Ok(_)))) + } + + #[test] + fn receives_remote_read_child_response() { + let mut chan = oneshot::channel(); + let child_info = ChildInfo::new_default(&b":child_storage:default:sub"[..]); + let request = light::RemoteReadChildRequest { + header: dummy_header(), + block: Default::default(), + storage_key: child_info.prefixed_storage_key(), + keys: vec![b":key".to_vec()], + retry_count: None, + }; + issue_request(Request::ReadChild { + request, + sender: chan.0, + }); + assert_matches!(chan.1.try_recv(), Ok(Some(Ok(_)))) + } + + #[test] + fn receives_remote_header_response() { + let mut chan = oneshot::channel(); + let request = light::RemoteHeaderRequest { + cht_root: Default::default(), + block: 1, + retry_count: None, + }; + issue_request(Request::Header { + request, + sender: chan.0, + }); + assert_matches!(chan.1.try_recv(), Ok(Some(Ok(_)))) + } + + #[test] + fn receives_remote_changes_response() { + let mut chan = oneshot::channel(); + let request = light::RemoteChangesRequest { + changes_trie_configs: vec![sp_core::ChangesTrieConfigurationRange { + zero: (0, Default::default()), + end: None, + config: Some(sp_core::ChangesTrieConfiguration::new(4, 2)), + }], + first_block: (1, Default::default()), + last_block: (100, Default::default()), + max_block: (100, Default::default()), + tries_roots: (1, Default::default(), Vec::new()), + key: Vec::new(), + storage_key: None, + retry_count: None, + }; + issue_request(Request::Changes { + request, + sender: chan.0, + }); + assert_matches!(chan.1.try_recv(), Ok(Some(Ok(_)))) + } +} diff --git a/client/network/src/on_demand_layer.rs b/client/network/src/on_demand_layer.rs index 9ec1fb7508c3e..ef8076e8cbed7 100644 --- a/client/network/src/on_demand_layer.rs +++ b/client/network/src/on_demand_layer.rs @@ -18,7 +18,7 @@ //! On-demand requests service. -use crate::light_client_handler; +use crate::light_client_requests; use futures::{channel::oneshot, prelude::*}; use parking_lot::Mutex; @@ -45,10 +45,10 @@ pub struct OnDemand { /// Note that a better alternative would be to use a MPMC queue here, and add a `poll` method /// from the `OnDemand`. However there exists no popular implementation of MPMC channels in /// asynchronous Rust at the moment - requests_queue: Mutex>>>, + requests_queue: Mutex>>>, /// Sending side of `requests_queue`. - requests_send: TracingUnboundedSender>, + requests_send: TracingUnboundedSender>, } @@ -149,7 +149,7 @@ where /// If this function returns `None`, that means that the receiver has already been extracted in /// the past, and therefore that something already handles the requests. pub(crate) fn extract_receiver(&self) - -> Option>> + -> Option>> { self.requests_queue.lock().take() } @@ -170,7 +170,7 @@ where let (sender, receiver) = oneshot::channel(); let _ = self .requests_send - .unbounded_send(light_client_handler::Request::Header { request, sender }); + .unbounded_send(light_client_requests::sender::Request::Header { request, sender }); RemoteResponse { receiver } } @@ -178,7 +178,7 @@ where let (sender, receiver) = oneshot::channel(); let _ = self .requests_send - .unbounded_send(light_client_handler::Request::Read { request, sender }); + .unbounded_send(light_client_requests::sender::Request::Read { request, sender }); RemoteResponse { receiver } } @@ -189,7 +189,7 @@ where let (sender, receiver) = oneshot::channel(); let _ = self .requests_send - .unbounded_send(light_client_handler::Request::ReadChild { request, sender }); + .unbounded_send(light_client_requests::sender::Request::ReadChild { request, sender }); RemoteResponse { receiver } } @@ -197,7 +197,7 @@ where let (sender, receiver) = oneshot::channel(); let _ = self .requests_send - .unbounded_send(light_client_handler::Request::Call { request, sender }); + .unbounded_send(light_client_requests::sender::Request::Call { request, sender }); RemoteResponse { receiver } } @@ -208,7 +208,7 @@ where let (sender, receiver) = oneshot::channel(); let _ = self .requests_send - .unbounded_send(light_client_handler::Request::Changes { request, sender }); + .unbounded_send(light_client_requests::sender::Request::Changes { request, sender }); RemoteResponse { receiver } } @@ -216,7 +216,7 @@ where let (sender, receiver) = oneshot::channel(); let _ = self .requests_send - .unbounded_send(light_client_handler::Request::Body { request, sender }); + .unbounded_send(light_client_requests::sender::Request::Body { request, sender }); RemoteResponse { receiver } } } diff --git a/client/network/src/protocol.rs b/client/network/src/protocol.rs index e102552f0f675..e1a10b520ba9a 100644 --- a/client/network/src/protocol.rs +++ b/client/network/src/protocol.rs @@ -17,9 +17,8 @@ // along with this program. If not, see . use crate::{ - ExHashT, chain::Client, - config::{self, ProtocolId, TransactionPool, TransactionImportFuture, TransactionImport}, + config::{self, ProtocolId}, error, request_responses::RequestFailure, utils::{interval, LruHashSet}, @@ -27,7 +26,7 @@ use crate::{ use bytes::{Bytes, BytesMut}; use codec::{Decode, DecodeAll, Encode}; -use futures::{channel::oneshot, prelude::*, stream::FuturesUnordered}; +use futures::{channel::oneshot, prelude::*}; use generic_proto::{GenericProto, GenericProtoOut}; use libp2p::core::{ConnectedPoint, connection::{ConnectionId, ListenerId}}; use libp2p::request_response::OutboundFailure; @@ -37,10 +36,7 @@ use libp2p::{Multiaddr, PeerId}; use log::{log, Level, trace, debug, warn, error}; use message::{BlockAnnounce, Message}; use message::generic::{Message as GenericMessage, Roles}; -use prometheus_endpoint::{ - Registry, Gauge, Counter, GaugeVec, - PrometheusError, Opts, register, U64 -}; +use prometheus_endpoint::{Registry, Gauge, GaugeVec, PrometheusError, Opts, register, U64}; use prost::Message as _; use sp_consensus::{ BlockOrigin, @@ -55,7 +51,7 @@ use sp_arithmetic::traits::SaturatedConversion; use sync::{ChainSync, SyncState}; use std::borrow::Cow; use std::convert::TryFrom as _; -use std::collections::{HashMap, HashSet, VecDeque, hash_map::Entry}; +use std::collections::{HashMap, HashSet, VecDeque}; use std::sync::Arc; use std::{io, iter, num::NonZeroUsize, pin::Pin, task::Poll, time}; @@ -69,28 +65,16 @@ pub use generic_proto::{NotificationsSink, Ready, NotifsHandlerError}; /// Interval at which we perform time based maintenance const TICK_TIMEOUT: time::Duration = time::Duration::from_millis(1100); -/// Interval at which we propagate transactions; -const PROPAGATE_TIMEOUT: time::Duration = time::Duration::from_millis(2900); /// Maximum number of known block hashes to keep for a peer. const MAX_KNOWN_BLOCKS: usize = 1024; // ~32kb per peer + LruHashSet overhead -/// Maximum number of known transaction hashes to keep for a peer. -/// -/// This should be approx. 2 blocks full of transactions for the network to function properly. -const MAX_KNOWN_TRANSACTIONS: usize = 10240; // ~300kb per peer + overhead. - /// Maximum allowed size for a block announce. const MAX_BLOCK_ANNOUNCE_SIZE: u64 = 1024 * 1024; -/// Maximum allowed size for a transactions notification. -const MAX_TRANSACTIONS_SIZE: u64 = 16 * 1024 * 1024; /// Maximum size used for notifications in the block announce and transaction protocols. // Must be equal to `max(MAX_BLOCK_ANNOUNCE_SIZE, MAX_TRANSACTIONS_SIZE)`. pub(crate) const BLOCK_ANNOUNCES_TRANSACTIONS_SUBSTREAM_SIZE: u64 = 16 * 1024 * 1024; -/// Maximum number of transaction validation request we keep at any moment. -const MAX_PENDING_TRANSACTIONS: usize = 8192; - /// Current protocol version. pub(crate) const CURRENT_VERSION: u32 = 6; /// Lowest version we support @@ -98,11 +82,9 @@ pub(crate) const MIN_VERSION: u32 = 3; /// Identifier of the peerset for the block announces protocol. const HARDCODED_PEERSETS_SYNC: sc_peerset::SetId = sc_peerset::SetId::from(0); -/// Identifier of the peerset for the transactions protocol. -const HARDCODED_PEERSETS_TX: sc_peerset::SetId = sc_peerset::SetId::from(1); /// Number of hardcoded peersets (the constants right above). Any set whose identifier is equal or /// superior to this value corresponds to a user-defined protocol. -const NUM_HARDCODED_PEERSETS: usize = 2; +const NUM_HARDCODED_PEERSETS: usize = 1; /// When light node connects to the full node and the full node is behind light node /// for at least `LIGHT_MAXIMAL_BLOCKS_DIFFERENCE` blocks, we consider it not useful @@ -117,21 +99,8 @@ mod rep { pub const REFUSED: Rep = Rep::new(-(1 << 10), "Request refused"); /// Reputation change when we are a light client and a peer is behind us. pub const PEER_BEHIND_US_LIGHT: Rep = Rep::new(-(1 << 8), "Useless for a light peer"); - /// Reputation change when a peer sends us any transaction. - /// - /// This forces node to verify it, thus the negative value here. Once transaction is verified, - /// reputation change should be refunded with `ANY_TRANSACTION_REFUND` - pub const ANY_TRANSACTION: Rep = Rep::new(-(1 << 4), "Any transaction"); - /// Reputation change when a peer sends us any transaction that is not invalid. - pub const ANY_TRANSACTION_REFUND: Rep = Rep::new(1 << 4, "Any transaction (refund)"); - /// Reputation change when a peer sends us an transaction that we didn't know about. - pub const GOOD_TRANSACTION: Rep = Rep::new(1 << 7, "Good transaction"); - /// Reputation change when a peer sends us a bad transaction. - pub const BAD_TRANSACTION: Rep = Rep::new(-(1 << 12), "Bad transaction"); /// We received a message that failed to decode. pub const BAD_MESSAGE: Rep = Rep::new(-(1 << 12), "Bad message"); - /// We received an unexpected transaction packet. - pub const UNEXPECTED_TRANSACTIONS: Rep = Rep::new_fatal("Unexpected transactions packet"); /// Peer has different genesis. pub const GENESIS_MISMATCH: Rep = Rep::new_fatal("Genesis mismatch"); /// Peer is on unsupported protocol version. @@ -147,7 +116,6 @@ struct Metrics { queued_blocks: Gauge, fork_targets: Gauge, justifications: GaugeVec, - propagated_transactions: Counter, } impl Metrics { @@ -175,62 +143,27 @@ impl Metrics { )?; register(g, r)? }, - propagated_transactions: register(Counter::new( - "sync_propagated_transactions", - "Number of transactions propagated to at least one peer", - )?, r)?, }) } } -#[pin_project::pin_project] -struct PendingTransaction { - #[pin] - validation: TransactionImportFuture, - tx_hash: H, -} - -impl Future for PendingTransaction { - type Output = (H, TransactionImport); - - fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll { - let mut this = self.project(); - - if let Poll::Ready(import_result) = Pin::new(&mut this.validation).poll_unpin(cx) { - return Poll::Ready((this.tx_hash.clone(), import_result)); - } - - Poll::Pending - } -} - // Lock must always be taken in order declared here. -pub struct Protocol { +pub struct Protocol { /// Interval at which we call `tick`. tick_timeout: Pin + Send>>, - /// Interval at which we call `propagate_transactions`. - propagate_timeout: Pin + Send>>, /// Pending list of messages to return from `poll` as a priority. pending_messages: VecDeque>, - /// Pending transactions verification tasks. - pending_transactions: FuturesUnordered>, - /// As multiple peers can send us the same transaction, we group - /// these peers using the transaction hash while the transaction is - /// imported. This prevents that we import the same transaction - /// multiple times concurrently. - pending_transactions_peers: HashMap>, config: ProtocolConfig, genesis_hash: B::Hash, sync: ChainSync, // All connected peers - peers: HashMap>, + peers: HashMap>, chain: Arc>, /// List of nodes for which we perform additional logging because they are important for the /// user. important_peers: HashSet, /// Used to report reputation changes. peerset_handle: sc_peerset::PeersetHandle, - transaction_pool: Arc>, /// Handles opening the unique substream and sending and receiving raw messages. behaviour: GenericProto, /// List of notifications protocols that have been registered. @@ -245,15 +178,13 @@ pub struct Protocol { /// Peer information #[derive(Debug)] -struct Peer { +struct Peer { info: PeerInfo, /// Current block request, if any. Started by emitting [`CustomMessageOutcome::BlockRequest`]. block_request: Option<( message::BlockRequest, oneshot::Receiver, RequestFailure>>, )>, - /// Holds a set of transactions known to this peer. - known_transactions: LruHashSet, /// Holds a set of blocks known to this peer. known_blocks: LruHashSet, } @@ -336,18 +267,17 @@ fn build_status_message( Message::::Status(status).encode() } -impl Protocol { +impl Protocol { /// Create a new instance. pub fn new( config: ProtocolConfig, chain: Arc>, - transaction_pool: Arc>, protocol_id: ProtocolId, - config_role: &config::Role, network_config: &config::NetworkConfiguration, + notifications_protocols_handshakes: Vec>, block_announce_validator: Box + Send>, metrics_registry: Option<&Registry>, - ) -> error::Result<(Protocol, sc_peerset::PeersetHandle, Vec<(PeerId, Multiaddr)>)> { + ) -> error::Result<(Protocol, sc_peerset::PeersetHandle, Vec<(PeerId, Multiaddr)>)> { let info = chain.info(); let sync = ChainSync::new( config.roles, @@ -384,21 +314,6 @@ impl Protocol { let mut sets = Vec::with_capacity(NUM_HARDCODED_PEERSETS + network_config.extra_sets.len()); let mut default_sets_reserved = HashSet::new(); - match config_role { - config::Role::Sentry { validators } => { - for validator in validators { - default_sets_reserved.insert(validator.peer_id.clone()); - known_addresses.push((validator.peer_id.clone(), validator.multiaddr.clone())); - } - } - config::Role::Authority { sentry_nodes } => { - for sentry_node in sentry_nodes { - default_sets_reserved.insert(sentry_node.peer_id.clone()); - known_addresses.push((sentry_node.peer_id.clone(), sentry_node.multiaddr.clone())); - } - } - _ => {} - }; for reserved in network_config.default_peers_set.reserved_nodes.iter() { default_sets_reserved.insert(reserved.peer_id.clone()); known_addresses.push((reserved.peer_id.clone(), reserved.multiaddr.clone())); @@ -420,18 +335,6 @@ impl Protocol { == config::NonReservedPeerMode::Deny, }); - // Set number 1 is used for transactions. - // The `reserved_nodes` of this set are later kept in sync with the peers we connect - // to through set 0. - sets.push(sc_peerset::SetConfig { - in_peers: network_config.default_peers_set.in_peers, - out_peers: network_config.default_peers_set.out_peers, - bootnodes: Vec::new(), - reserved_nodes: default_sets_reserved, - reserved_only: network_config.default_peers_set.non_reserved_mode - == config::NonReservedPeerMode::Deny, - }); - for set_cfg in &network_config.extra_sets { let mut reserved_nodes = HashSet::new(); for reserved in set_cfg.set_config.reserved_nodes.iter() { @@ -456,14 +359,6 @@ impl Protocol { }) }; - let transactions_protocol: Cow<'static, str> = Cow::from({ - let mut proto = String::new(); - proto.push_str("/"); - proto.push_str(protocol_id.as_ref()); - proto.push_str("/transactions/1"); - proto - }); - let block_announces_protocol: Cow<'static, str> = Cow::from({ let mut proto = String::new(); proto.push_str("/"); @@ -474,7 +369,6 @@ impl Protocol { let behaviour = { let versions = &((MIN_VERSION as u8)..=(CURRENT_VERSION as u8)).collect::>(); - let handshake_message = Roles::from(config_role).encode(); let best_number = info.best_number; let best_hash = info.best_hash; @@ -493,12 +387,10 @@ impl Protocol { build_status_message::(&config, best_number, best_hash, genesis_hash), peerset, iter::once((block_announces_protocol, block_announces_handshake, MAX_BLOCK_ANNOUNCE_SIZE)) - .chain(iter::once((transactions_protocol, vec![], MAX_TRANSACTIONS_SIZE))) - .chain(network_config.extra_sets.iter().map(|s| ( - s.notifications_protocol.clone(), - handshake_message.clone(), - s.max_notification_size - ))), + .chain(network_config.extra_sets.iter() + .zip(notifications_protocols_handshakes) + .map(|(s, hs)| (s.notifications_protocol.clone(), hs, s.max_notification_size)) + ), ) }; @@ -509,17 +401,13 @@ impl Protocol { let protocol = Protocol { tick_timeout: Box::pin(interval(TICK_TIMEOUT)), - propagate_timeout: Box::pin(interval(PROPAGATE_TIMEOUT)), pending_messages: VecDeque::new(), - pending_transactions: FuturesUnordered::new(), - pending_transactions_peers: HashMap::new(), config, peers: HashMap::new(), chain, genesis_hash: info.genesis_hash, sync, important_peers, - transaction_pool, peerset_handle: peerset_handle.clone(), behaviour, notification_protocols: @@ -656,7 +544,7 @@ impl Protocol { "Couldn't decode packet sent by {}: {:?}: {}", who, data, - err.what(), + err, ); self.peerset_handle.report_peer(who, rep::BAD_MESSAGE); return CustomMessageOutcome::None; @@ -668,8 +556,8 @@ impl Protocol { debug!(target: "sub-libp2p", "Received unexpected Status"), GenericMessage::BlockAnnounce(announce) => self.push_block_announce_validation(who.clone(), announce), - GenericMessage::Transactions(m) => - self.on_transactions(who, m), + GenericMessage::Transactions(_) => + warn!(target: "sub-libp2p", "Received unexpected Transactions"), GenericMessage::BlockResponse(_) => warn!(target: "sub-libp2p", "Received unexpected BlockResponse"), GenericMessage::RemoteCallResponse(_) => @@ -706,7 +594,7 @@ impl Protocol { who: PeerId, request: message::BlockRequest, ) -> CustomMessageOutcome { - prepare_block_request::(&mut self.peers, who, request) + prepare_block_request::(&mut self.peers, who, request) } /// Called by peer when it is disconnecting. @@ -912,8 +800,6 @@ impl Protocol { best_number: status.best_number }, block_request: None, - known_transactions: LruHashSet::new(NonZeroUsize::new(MAX_KNOWN_TRANSACTIONS) - .expect("Constant is nonzero")), known_blocks: LruHashSet::new(NonZeroUsize::new(MAX_KNOWN_BLOCKS) .expect("Constant is nonzero")), }; @@ -944,144 +830,6 @@ impl Protocol { Ok(()) } - /// Called when peer sends us new transactions - fn on_transactions( - &mut self, - who: PeerId, - transactions: message::Transactions, - ) { - // sending transaction to light node is considered a bad behavior - if !self.config.roles.is_full() { - trace!(target: "sync", "Peer {} is trying to send transactions to the light node", who); - self.behaviour.disconnect_peer(&who, HARDCODED_PEERSETS_TX); - self.peerset_handle.report_peer(who, rep::UNEXPECTED_TRANSACTIONS); - return; - } - - // Accept transactions only when fully synced - if self.sync.status().state != SyncState::Idle { - trace!(target: "sync", "{} Ignoring transactions while syncing", who); - return; - } - - trace!(target: "sync", "Received {} transactions from {}", transactions.len(), who); - if let Some(ref mut peer) = self.peers.get_mut(&who) { - for t in transactions { - if self.pending_transactions.len() > MAX_PENDING_TRANSACTIONS { - debug!( - target: "sync", - "Ignoring any further transactions that exceed `MAX_PENDING_TRANSACTIONS`({}) limit", - MAX_PENDING_TRANSACTIONS, - ); - break; - } - - let hash = self.transaction_pool.hash_of(&t); - peer.known_transactions.insert(hash.clone()); - - self.peerset_handle.report_peer(who.clone(), rep::ANY_TRANSACTION); - - match self.pending_transactions_peers.entry(hash.clone()) { - Entry::Vacant(entry) => { - self.pending_transactions.push(PendingTransaction { - validation: self.transaction_pool.import(t), - tx_hash: hash, - }); - entry.insert(vec![who.clone()]); - }, - Entry::Occupied(mut entry) => { - entry.get_mut().push(who.clone()); - } - } - } - } - } - - fn on_handle_transaction_import(&mut self, who: PeerId, import: TransactionImport) { - match import { - TransactionImport::KnownGood => self.peerset_handle.report_peer(who, rep::ANY_TRANSACTION_REFUND), - TransactionImport::NewGood => self.peerset_handle.report_peer(who, rep::GOOD_TRANSACTION), - TransactionImport::Bad => self.peerset_handle.report_peer(who, rep::BAD_TRANSACTION), - TransactionImport::None => {}, - } - } - - /// Propagate one transaction. - pub fn propagate_transaction( - &mut self, - hash: &H, - ) { - debug!(target: "sync", "Propagating transaction [{:?}]", hash); - // Accept transactions only when fully synced - if self.sync.status().state != SyncState::Idle { - return; - } - if let Some(transaction) = self.transaction_pool.transaction(hash) { - let propagated_to = self.do_propagate_transactions(&[(hash.clone(), transaction)]); - self.transaction_pool.on_broadcasted(propagated_to); - } - } - - fn do_propagate_transactions( - &mut self, - transactions: &[(H, B::Extrinsic)], - ) -> HashMap> { - let mut propagated_to = HashMap::<_, Vec<_>>::new(); - let mut propagated_transactions = 0; - - for (who, peer) in self.peers.iter_mut() { - // never send transactions to the light node - if !peer.info.roles.is_full() { - continue; - } - - if !self.behaviour.is_open(who, HARDCODED_PEERSETS_TX) { - continue; - } - - let (hashes, to_send): (Vec<_>, Vec<_>) = transactions - .iter() - .filter(|&(ref hash, _)| peer.known_transactions.insert(hash.clone())) - .cloned() - .unzip(); - - propagated_transactions += hashes.len(); - - if !to_send.is_empty() { - for hash in hashes { - propagated_to - .entry(hash) - .or_default() - .push(who.to_base58()); - } - trace!(target: "sync", "Sending {} transactions to {}", to_send.len(), who); - self.behaviour.write_notification( - who, - HARDCODED_PEERSETS_TX, - to_send.encode() - ); - } - } - - if let Some(ref metrics) = self.metrics { - metrics.propagated_transactions.inc_by(propagated_transactions as _) - } - - propagated_to - } - - /// Call when we must propagate ready transactions to peers. - pub fn propagate_transactions(&mut self) { - debug!(target: "sync", "Propagating transactions"); - // Accept transactions only when fully synced - if self.sync.status().state != SyncState::Idle { - return; - } - let transactions = self.transaction_pool.transactions(); - let propagated_to = self.do_propagate_transactions(&transactions); - self.transaction_pool.on_broadcasted(propagated_to); - } - /// Make sure an important block is propagated to peers. /// /// In chain-based consensus, we often need to make sure non-best forks are @@ -1333,25 +1081,21 @@ impl Protocol { /// Set whether the syncing peers set is in reserved-only mode. pub fn set_reserved_only(&self, reserved_only: bool) { self.peerset_handle.set_reserved_only(HARDCODED_PEERSETS_SYNC, reserved_only); - self.peerset_handle.set_reserved_only(HARDCODED_PEERSETS_TX, reserved_only); } /// Removes a `PeerId` from the list of reserved peers for syncing purposes. pub fn remove_reserved_peer(&self, peer: PeerId) { self.peerset_handle.remove_reserved_peer(HARDCODED_PEERSETS_SYNC, peer.clone()); - self.peerset_handle.remove_reserved_peer(HARDCODED_PEERSETS_TX, peer); } /// Adds a `PeerId` to the list of reserved peers for syncing purposes. pub fn add_reserved_peer(&self, peer: PeerId) { self.peerset_handle.add_reserved_peer(HARDCODED_PEERSETS_SYNC, peer.clone()); - self.peerset_handle.add_reserved_peer(HARDCODED_PEERSETS_TX, peer); } /// Sets the list of reserved peers for syncing purposes. pub fn set_reserved_peers(&self, peers: HashSet) { self.peerset_handle.set_reserved_peers(HARDCODED_PEERSETS_SYNC, peers.clone()); - self.peerset_handle.set_reserved_peers(HARDCODED_PEERSETS_TX, peers); } /// Removes a `PeerId` from the list of reserved peers. @@ -1437,8 +1181,8 @@ impl Protocol { } } -fn prepare_block_request( - peers: &mut HashMap>, +fn prepare_block_request( + peers: &mut HashMap>, who: PeerId, request: message::BlockRequest, ) -> CustomMessageOutcome { @@ -1506,7 +1250,7 @@ pub enum CustomMessageOutcome { None, } -impl NetworkBehaviour for Protocol { +impl NetworkBehaviour for Protocol { type ProtocolsHandler = ::ProtocolsHandler; type OutEvent = CustomMessageOutcome; @@ -1635,10 +1379,6 @@ impl NetworkBehaviour for Protocol { self.tick(); } - while let Poll::Ready(Some(())) = self.propagate_timeout.poll_next_unpin(cx) { - self.propagate_transactions(); - } - for (id, request) in self.sync.block_requests() { let event = prepare_block_request(&mut self.peers, id.clone(), request); self.pending_messages.push_back(event); @@ -1647,13 +1387,6 @@ impl NetworkBehaviour for Protocol { let event = prepare_block_request(&mut self.peers, id, request); self.pending_messages.push_back(event); } - if let Poll::Ready(Some((tx_hash, result))) = self.pending_transactions.poll_next_unpin(cx) { - if let Some(peers) = self.pending_transactions_peers.remove(&tx_hash) { - peers.into_iter().for_each(|p| self.on_handle_transaction_import(p, result)); - } else { - warn!(target: "sub-libp2p", "Inconsistent state, no peers for pending transaction!"); - } - } // Check if there is any block announcement validation finished. while let Poll::Ready(result) = self.sync.poll_block_announce_validation(cx) { @@ -1697,11 +1430,6 @@ impl NetworkBehaviour for Protocol { }; if self.on_sync_peer_connected(peer_id.clone(), handshake).is_ok() { - // Set 1 is kept in sync with the connected peers of set 0. - self.peerset_handle.add_reserved_peer( - HARDCODED_PEERSETS_TX, - peer_id.clone() - ); CustomMessageOutcome::SyncConnected(peer_id) } else { CustomMessageOutcome::None @@ -1721,11 +1449,6 @@ impl NetworkBehaviour for Protocol { match as DecodeAll>::decode_all(&mut &received_handshake[..]) { Ok(handshake) => { if self.on_sync_peer_connected(peer_id.clone(), handshake).is_ok() { - // Set 1 is kept in sync with the connected peers of set 0. - self.peerset_handle.add_reserved_peer( - HARDCODED_PEERSETS_TX, - peer_id.clone() - ); CustomMessageOutcome::SyncConnected(peer_id) } else { CustomMessageOutcome::None @@ -1737,7 +1460,7 @@ impl NetworkBehaviour for Protocol { "Couldn't decode handshake sent by {}: {:?}: {} & {}", peer_id, received_handshake, - err.what(), + err, err2, ); self.peerset_handle.report_peer(peer_id, rep::BAD_MESSAGE); @@ -1747,19 +1470,28 @@ impl NetworkBehaviour for Protocol { } } - } else if set_id == HARDCODED_PEERSETS_TX { - // Nothing to do. - CustomMessageOutcome::None } else { - match message::Roles::decode_all(&received_handshake[..]) { - Ok(roles) => + match (message::Roles::decode_all(&received_handshake[..]), self.peers.get(&peer_id)) { + (Ok(roles), _) => CustomMessageOutcome::NotificationStreamOpened { remote: peer_id, protocol: self.notification_protocols[usize::from(set_id) - NUM_HARDCODED_PEERSETS].clone(), roles, notifications_sink, }, - Err(err) => { + (Err(_), Some(peer)) if received_handshake.is_empty() => { + // As a convenience, we allow opening substreams for "external" + // notification protocols with an empty handshake. This fetches the + // roles from the locally-known roles. + // TODO: remove this after https://github.com/paritytech/substrate/issues/5685 + CustomMessageOutcome::NotificationStreamOpened { + remote: peer_id, + protocol: self.notification_protocols[usize::from(set_id) - NUM_HARDCODED_PEERSETS].clone(), + roles: peer.info.roles, + notifications_sink, + } + }, + (Err(err), _) => { debug!(target: "sync", "Failed to parse remote handshake: {}", err); self.behaviour.disconnect_peer(&peer_id, set_id); self.peerset_handle.report_peer(peer_id, rep::BAD_MESSAGE); @@ -1769,7 +1501,7 @@ impl NetworkBehaviour for Protocol { } } GenericProtoOut::CustomProtocolReplaced { peer_id, notifications_sink, set_id } => { - if set_id == HARDCODED_PEERSETS_SYNC || set_id == HARDCODED_PEERSETS_TX { + if set_id == HARDCODED_PEERSETS_SYNC { CustomMessageOutcome::None } else { CustomMessageOutcome::NotificationStreamReplaced { @@ -1783,11 +1515,6 @@ impl NetworkBehaviour for Protocol { // Set number 0 is hardcoded the default set of peers we sync from. if set_id == HARDCODED_PEERSETS_SYNC { if self.on_sync_peer_disconnected(peer_id.clone()).is_ok() { - // Set 1 is kept in sync with the connected peers of set 0. - self.peerset_handle.remove_reserved_peer( - HARDCODED_PEERSETS_TX, - peer_id.clone() - ); CustomMessageOutcome::SyncDisconnected(peer_id) } else { log::debug!( @@ -1797,8 +1524,6 @@ impl NetworkBehaviour for Protocol { ); CustomMessageOutcome::None } - } else if set_id == HARDCODED_PEERSETS_TX { - CustomMessageOutcome::None } else { CustomMessageOutcome::NotificationStreamClosed { remote: peer_id, @@ -1831,20 +1556,10 @@ impl NetworkBehaviour for Protocol { CustomMessageOutcome::None } } - HARDCODED_PEERSETS_TX if self.peers.contains_key(&peer_id) => { - if let Ok(m) = as Decode>::decode( - &mut message.as_ref(), - ) { - self.on_transactions(peer_id, m); - } else { - warn!(target: "sub-libp2p", "Failed to decode transactions list"); - } - CustomMessageOutcome::None - } - HARDCODED_PEERSETS_SYNC | HARDCODED_PEERSETS_TX => { + HARDCODED_PEERSETS_SYNC => { debug!( target: "sync", - "Received sync or transaction for peer earlier refused by sync layer: {}", + "Received sync for peer earlier refused by sync layer: {}", peer_id ); CustomMessageOutcome::None diff --git a/client/network/src/protocol/event.rs b/client/network/src/protocol/event.rs index e20dbcb9ee279..fb2e3b33dd680 100644 --- a/client/network/src/protocol/event.rs +++ b/client/network/src/protocol/event.rs @@ -92,16 +92,16 @@ pub enum Event { /// Role that the peer sent to us during the handshake, with the addition of what our local node /// knows about that peer. +/// +/// > **Note**: This enum is different from the `Role` enum. The `Role` enum indicates what a +/// > node says about itself, while `ObservedRole` is a `Role` merged with the +/// > information known locally about that node. #[derive(Debug, Clone)] pub enum ObservedRole { /// Full node. Full, /// Light node. Light, - /// When we are a validator node, this is a sentry that protects us. - OurSentry, - /// When we are a sentry node, this is the authority we are protecting. - OurGuardedAuthority, /// Third-party authority. Authority, } diff --git a/client/network/src/protocol/message.rs b/client/network/src/protocol/message.rs index 3aa1e2cf34a78..ed2721032801c 100644 --- a/client/network/src/protocol/message.rs +++ b/client/network/src/protocol/message.rs @@ -95,7 +95,7 @@ impl BlockAttributes { } impl Encode for BlockAttributes { - fn encode_to(&self, dest: &mut T) { + fn encode_to(&self, dest: &mut T) { dest.push_byte(self.bits()) } } @@ -191,14 +191,13 @@ pub mod generic { match roles { crate::config::Role::Full => Roles::FULL, crate::config::Role::Light => Roles::LIGHT, - crate::config::Role::Sentry { .. } => Roles::AUTHORITY, crate::config::Role::Authority { .. } => Roles::AUTHORITY, } } } impl codec::Encode for Roles { - fn encode_to(&self, dest: &mut T) { + fn encode_to(&self, dest: &mut T) { dest.push_byte(self.bits()) } } @@ -282,7 +281,7 @@ pub mod generic { /// Batch of consensus protocol messages. // NOTE: index is incremented by 2 due to finality proof related // messages that were removed. - #[codec(index = "17")] + #[codec(index = 17)] ConsensusBatch(Vec), } @@ -402,7 +401,7 @@ pub mod generic { // This assumes that the packet contains nothing but the announcement message. // TODO: Get rid of it once protocol v4 is common. impl Encode for BlockAnnounce { - fn encode_to(&self, dest: &mut T) { + fn encode_to(&self, dest: &mut T) { self.header.encode_to(dest); if let Some(state) = &self.state { state.encode_to(dest); diff --git a/client/network/src/protocol/sync/extra_requests.rs b/client/network/src/protocol/sync/extra_requests.rs index d0fcfb777b8b0..3de79b3f48734 100644 --- a/client/network/src/protocol/sync/extra_requests.rs +++ b/client/network/src/protocol/sync/extra_requests.rs @@ -345,8 +345,7 @@ impl<'a, B: BlockT> Matcher<'a, B> { mod tests { use crate::protocol::sync::PeerSync; use sp_blockchain::Error as ClientError; - use quickcheck::{Arbitrary, Gen, QuickCheck, StdThreadGen}; - use rand::Rng; + use quickcheck::{Arbitrary, Gen, QuickCheck}; use std::collections::{HashMap, HashSet}; use super::*; use sp_test_primitives::{Block, BlockNumber, Hash}; @@ -373,7 +372,7 @@ mod tests { } } - QuickCheck::with_gen(StdThreadGen::new(19)) + QuickCheck::new() .quickcheck(property as fn(ArbitraryPeers)) } @@ -425,7 +424,7 @@ mod tests { previously_active == requests.pending_requests.iter().cloned().collect::>() } - QuickCheck::with_gen(StdThreadGen::new(19)) + QuickCheck::new() .quickcheck(property as fn(ArbitraryPeers) -> bool) } @@ -457,7 +456,7 @@ mod tests { } } - QuickCheck::with_gen(StdThreadGen::new(19)) + QuickCheck::new() .quickcheck(property as fn(ArbitraryPeers)) } @@ -527,11 +526,11 @@ mod tests { struct ArbitraryPeerSyncState(PeerSyncState); impl Arbitrary for ArbitraryPeerSyncState { - fn arbitrary(g: &mut G) -> Self { - let s = match g.gen::() % 4 { + fn arbitrary(g: &mut Gen) -> Self { + let s = match u8::arbitrary(g) % 4 { 0 => PeerSyncState::Available, // TODO: 1 => PeerSyncState::AncestorSearch(g.gen(), AncestorSearchState), - 1 => PeerSyncState::DownloadingNew(g.gen::()), + 1 => PeerSyncState::DownloadingNew(BlockNumber::arbitrary(g)), 2 => PeerSyncState::DownloadingStale(Hash::random()), _ => PeerSyncState::DownloadingJustification(Hash::random()), }; @@ -543,12 +542,12 @@ mod tests { struct ArbitraryPeerSync(PeerSync); impl Arbitrary for ArbitraryPeerSync { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { let ps = PeerSync { peer_id: PeerId::random(), - common_number: g.gen(), + common_number: u64::arbitrary(g), best_hash: Hash::random(), - best_number: g.gen(), + best_number: u64::arbitrary(g), state: ArbitraryPeerSyncState::arbitrary(g).0, }; ArbitraryPeerSync(ps) @@ -559,7 +558,7 @@ mod tests { struct ArbitraryPeers(HashMap>); impl Arbitrary for ArbitraryPeers { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { let mut peers = HashMap::with_capacity(g.size()); for _ in 0 .. g.size() { let ps = ArbitraryPeerSync::arbitrary(g).0; diff --git a/client/network/src/request_responses.rs b/client/network/src/request_responses.rs index 9170644c3f409..4d478ea7afd65 100644 --- a/client/network/src/request_responses.rs +++ b/client/network/src/request_responses.rs @@ -196,6 +196,25 @@ impl From<(Cow<'static, str>, RequestId)> for ProtocolRequestId { } } +/// When sending a request, what to do on a disconnected recipient. +pub enum IfDisconnected { + /// Try to connect to the peer. + TryConnect, + /// Just fail if the destination is not yet connected. + ImmediateError, +} + +/// Convenience functions for `IfDisconnected`. +impl IfDisconnected { + /// Shall we connect to a disconnected peer? + pub fn should_connect(self) -> bool { + match self { + Self::TryConnect => true, + Self::ImmediateError => false, + } + } +} + /// Implementation of `NetworkBehaviour` that provides support for request-response protocols. pub struct RequestResponsesBehaviour { /// The multiple sub-protocols, by name. @@ -269,22 +288,25 @@ impl RequestResponsesBehaviour { /// Initiates sending a request. /// - /// An error is returned if we are not connected to the target peer or if the protocol doesn't - /// match one that has been registered. + /// If there is no established connection to the target peer, the behavior is determined by the choice of `connect`. + /// + /// An error is returned if the protocol doesn't match one that has been registered. pub fn send_request( &mut self, target: &PeerId, protocol_name: &str, request: Vec, pending_response: oneshot::Sender, RequestFailure>>, + connect: IfDisconnected, ) { if let Some((protocol, _)) = self.protocols.get_mut(protocol_name) { - if protocol.is_connected(target) { + if protocol.is_connected(target) || connect.should_connect() { let request_id = protocol.send_request(target, request); - self.pending_requests.insert( + let prev_req_id = self.pending_requests.insert( (protocol_name.to_string().into(), request_id).into(), (Instant::now(), pending_response), ); + debug_assert!(prev_req_id.is_none(), "Expect request id to be unique."); } else { if pending_response.send(Err(RequestFailure::NotConnected)).is_err() { log::debug!( @@ -488,7 +510,6 @@ impl NetworkBehaviour for RequestResponsesBehaviour { return Poll::Ready(NetworkBehaviourAction::DialAddress { address }) } NetworkBehaviourAction::DialPeer { peer_id, condition } => { - log::error!("The request-response isn't supposed to start dialing peers"); return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition, @@ -948,6 +969,7 @@ mod tests { protocol_name, b"this is a request".to_vec(), sender, + IfDisconnected::ImmediateError, ); assert!(response_receiver.is_none()); response_receiver = Some(receiver); @@ -1036,6 +1058,7 @@ mod tests { protocol_name, b"this is a request".to_vec(), sender, + IfDisconnected::ImmediateError, ); assert!(response_receiver.is_none()); response_receiver = Some(receiver); @@ -1178,12 +1201,14 @@ mod tests { protocol_name_1, b"this is a request".to_vec(), sender_1, + IfDisconnected::ImmediateError, ); swarm_1.send_request( &peer_id, protocol_name_2, b"this is a request".to_vec(), sender_2, + IfDisconnected::ImmediateError, ); assert!(response_receivers.is_none()); response_receivers = Some((receiver_1, receiver_2)); diff --git a/client/network/src/schema.rs b/client/network/src/schema.rs index 5b9a70b0cd5d9..d4572fca7594c 100644 --- a/client/network/src/schema.rs +++ b/client/network/src/schema.rs @@ -24,3 +24,7 @@ pub mod v1 { include!(concat!(env!("OUT_DIR"), "/api.v1.light.rs")); } } + +pub mod bitswap { + include!(concat!(env!("OUT_DIR"), "/bitswap.message.rs")); +} diff --git a/client/network/src/schema/bitswap.v1.2.0.proto b/client/network/src/schema/bitswap.v1.2.0.proto new file mode 100644 index 0000000000000..a4138b516d63d --- /dev/null +++ b/client/network/src/schema/bitswap.v1.2.0.proto @@ -0,0 +1,43 @@ +syntax = "proto3"; + +package bitswap.message; + +message Message { + message Wantlist { + enum WantType { + Block = 0; + Have = 1; + } + + message Entry { + bytes block = 1; // the block cid (cidV0 in bitswap 1.0.0, cidV1 in bitswap 1.1.0) + int32 priority = 2; // the priority (normalized). default to 1 + bool cancel = 3; // whether this revokes an entry + WantType wantType = 4; // Note: defaults to enum 0, ie Block + bool sendDontHave = 5; // Note: defaults to false + } + + repeated Entry entries = 1; // a list of wantlist entries + bool full = 2; // whether this is the full wantlist. default to false + } + + message Block { + bytes prefix = 1; // CID prefix (cid version, multicodec and multihash prefix (type + length) + bytes data = 2; + } + + enum BlockPresenceType { + Have = 0; + DontHave = 1; + } + message BlockPresence { + bytes cid = 1; + BlockPresenceType type = 2; + } + + Wantlist wantlist = 1; + repeated bytes blocks = 2; // used to send Blocks in bitswap 1.0.0 + repeated Block payload = 3; // used to send Blocks in bitswap 1.1.0 + repeated BlockPresence blockPresences = 4; + int32 pendingBytes = 5; +} diff --git a/client/network/src/service.rs b/client/network/src/service.rs index 20968c1278893..74ce9316fc41c 100644 --- a/client/network/src/service.rs +++ b/client/network/src/service.rs @@ -30,7 +30,7 @@ use crate::{ ExHashT, NetworkStateInfo, NetworkStatus, behaviour::{self, Behaviour, BehaviourOut}, - config::{parse_str_addr, Params, Role, TransportConfig}, + config::{parse_str_addr, Params, TransportConfig}, DhtEvent, discovery::DiscoveryConfig, error::Error, @@ -38,9 +38,10 @@ use crate::{ NetworkState, NotConnectedPeer as NetworkStateNotConnectedPeer, Peer as NetworkStatePeer, }, on_demand_layer::AlwaysBadChecker, - light_client_handler, + light_client_requests, protocol::{ self, + message::generic::Roles, NotifsHandlerError, NotificationsSink, PeerInfo, @@ -49,8 +50,13 @@ use crate::{ event::Event, sync::SyncState, }, + transactions, transport, ReputationChange, + + bitswap::Bitswap, }; + +use codec::Encode as _; use futures::{channel::oneshot, prelude::*}; use libp2p::{PeerId, multiaddr, Multiaddr}; use libp2p::core::{ @@ -98,7 +104,7 @@ use std::{ task::Poll, }; -pub use behaviour::{ResponseFailure, InboundFailure, RequestFailure, OutboundFailure}; +pub use behaviour::{ResponseFailure, InboundFailure, RequestFailure, OutboundFailure, IfDisconnected}; mod metrics; mod out_events; @@ -139,7 +145,7 @@ impl NetworkWorker { /// Returns a `NetworkWorker` that implements `Future` and must be regularly polled in order /// for the network processing to advance. From it, you can extract a `NetworkService` using /// `worker.service()`. The `NetworkService` can be shared through the codebase. - pub fn new(params: Params) -> Result, Error> { + pub fn new(mut params: Params) -> Result, Error> { // Ensure the listen addresses are consistent with the transport. ensure_addresses_consistent_with_transport( params.network_config.listen_addresses.iter(), @@ -170,6 +176,11 @@ impl NetworkWorker { fs::create_dir_all(path)?; } + let transactions_handler_proto = transactions::TransactionsHandlerPrototype::new( + params.protocol_id.clone() + ); + params.network_config.extra_sets.insert(0, transactions_handler_proto.set_config()); + // Private and public keys configuration. let local_identity = params.network_config.node_key.clone().into_keypair()?; let local_public = local_identity.public(); @@ -180,16 +191,17 @@ impl NetworkWorker { local_peer_id.to_base58(), ); + let default_notif_handshake_message = Roles::from(¶ms.role).encode(); let (protocol, peerset_handle, mut known_addresses) = Protocol::new( protocol::ProtocolConfig { roles: From::from(¶ms.role), max_parallel_downloads: params.network_config.max_parallel_downloads, }, params.chain.clone(), - params.transaction_pool, params.protocol_id.clone(), - ¶ms.role, ¶ms.network_config, + iter::once(Vec::new()).chain((0..params.network_config.extra_sets.len() - 1) + .map(|_| default_notif_handshake_message.clone())).collect(), params.block_announce_validator, params.metrics_registry.as_ref(), )?; @@ -224,22 +236,6 @@ impl NetworkWorker { } )?; - // Print a message about the deprecation of sentry nodes. - let print_deprecated_message = match ¶ms.role { - Role::Sentry { .. } => true, - Role::Authority { sentry_nodes } if !sentry_nodes.is_empty() => true, - _ => false, - }; - if print_deprecated_message { - log::warn!( - "🙇 Sentry nodes are deprecated, and the `--sentry` and `--sentry-nodes` \ - CLI options will eventually be removed in a future version. The Substrate \ - and Polkadot networking protocol require validators to be \ - publicly-accessible. Please do not block access to your validator nodes. \ - For details, see https://github.com/paritytech/substrate/issues/6845." - ); - } - let checker = params.on_demand.as_ref() .map(|od| od.checker().clone()) .unwrap_or_else(|| Arc::new(AlwaysBadChecker)); @@ -248,17 +244,17 @@ impl NetworkWorker { let is_major_syncing = Arc::new(AtomicBool::new(false)); // Build the swarm. - let (mut swarm, bandwidth): (Swarm, _) = { + let client = params.chain.clone(); + let (mut swarm, bandwidth): (Swarm, _) = { let user_agent = format!( "{} ({})", params.network_config.client_version, params.network_config.node_name ); - let light_client_handler = { - let config = light_client_handler::Config::new(¶ms.protocol_id); - light_client_handler::LightClientHandler::new( - config, - params.chain, + + let light_client_request_sender = { + light_client_requests::sender::LightClientRequestSender::new( + ¶ms.protocol_id, checker, peerset_handle.clone(), ) @@ -269,6 +265,7 @@ impl NetworkWorker { config.with_user_defined(known_addresses); config.discovery_limit(u64::from(params.network_config.default_peers_set.out_peers) + 15); config.add_protocol(params.protocol_id.clone()); + config.with_dht_random_walk(params.network_config.enable_dht_random_walk); config.allow_non_globals_in_dht(params.network_config.allow_non_globals_in_dht); config.use_kademlia_disjoint_query_paths(params.network_config.kademlia_disjoint_query_paths); @@ -334,14 +331,16 @@ impl NetworkWorker { }; let behaviour = { + let bitswap = if params.network_config.ipfs_server { Some(Bitswap::new(client)) } else { None }; let result = Behaviour::new( protocol, - params.role, user_agent, local_public, - light_client_handler, + light_client_request_sender, discovery_config, params.block_request_protocol_config, + bitswap, + params.light_client_request_protocol_config, params.network_config.request_response_protocols, ); @@ -389,14 +388,14 @@ impl NetworkWorker { // Listen on multiaddresses. for addr in ¶ms.network_config.listen_addresses { - if let Err(err) = Swarm::::listen_on(&mut swarm, addr.clone()) { + if let Err(err) = Swarm::::listen_on(&mut swarm, addr.clone()) { warn!(target: "sub-libp2p", "Can't listen on {} because: {:?}", addr, err) } } // Add external addresses. for addr in ¶ms.network_config.public_addresses { - Swarm::::add_external_address(&mut swarm, addr.clone(), AddressScore::Infinite); + Swarm::::add_external_address(&mut swarm, addr.clone(), AddressScore::Infinite); } let external_addresses = Arc::new(Mutex::new(Vec::new())); @@ -416,6 +415,14 @@ impl NetworkWorker { _marker: PhantomData, }); + let (tx_handler, tx_handler_controller) = transactions_handler_proto.build( + service.clone(), + params.role, + params.transaction_pool, + params.metrics_registry.as_ref() + )?; + (params.transactions_handler_executor)(tx_handler.run().boxed()); + Ok(NetworkWorker { external_addresses, num_connected, @@ -427,6 +434,7 @@ impl NetworkWorker { light_client_rqs: params.on_demand.and_then(|od| od.extract_receiver()), event_streams: out_events::OutChannels::new(params.metrics_registry.as_ref())?, peers_notifications_sinks, + tx_handler_controller, metrics, boot_node_ids, }) @@ -518,14 +526,14 @@ impl NetworkWorker { /// Returns the local `PeerId`. pub fn local_peer_id(&self) -> &PeerId { - Swarm::::local_peer_id(&self.network_service) + Swarm::::local_peer_id(&self.network_service) } /// Returns the list of addresses we are listening on. /// /// Does **NOT** include a trailing `/p2p/` with our `PeerId`. pub fn listen_addresses(&self) -> impl Iterator { - Swarm::::listeners(&self.network_service) + Swarm::::listeners(&self.network_service) } /// Get network state. @@ -576,9 +584,9 @@ impl NetworkWorker { .collect() }; - let peer_id = Swarm::::local_peer_id(&swarm).to_base58(); - let listened_addresses = Swarm::::listeners(&swarm).cloned().collect(); - let external_addresses = Swarm::::external_addresses(&swarm) + let peer_id = Swarm::::local_peer_id(&swarm).to_base58(); + let listened_addresses = Swarm::::listeners(&swarm).cloned().collect(); + let external_addresses = Swarm::::external_addresses(&swarm) .map(|r| &r.addr) .cloned() .collect(); @@ -670,8 +678,8 @@ impl NetworkService { // Notification silently discarded, as documented. log::debug!( target: "sub-libp2p", - "Attempted to send notification on missing or closed substream: {:?}", - protocol, + "Attempted to send notification on missing or closed substream: {}, {:?}", + target, protocol, ); return; } @@ -811,9 +819,10 @@ impl NetworkService { /// notifications should remain the default ways of communicating information. For example, a /// peer can announce something through a notification, after which the recipient can obtain /// more information by performing a request. - /// As such, this function is meant to be called only with peers we are already connected to. - /// Calling this method with a `target` we are not connected to will *not* attempt to connect - /// to said peer. + /// As such, call this function with `IfDisconnected::ImmediateError` for `connect`. This way you + /// will get an error immediately for disconnected peers, instead of waiting for a potentially very + /// long connection attempt, which would suggest that something is wrong anyway, as you are + /// supposed to be connected because of the notification protocol. /// /// No limit or throttling of concurrent outbound requests per peer and protocol are enforced. /// Such restrictions, if desired, need to be enforced at the call site(s). @@ -825,15 +834,12 @@ impl NetworkService { &self, target: PeerId, protocol: impl Into>, - request: Vec + request: Vec, + connect: IfDisconnected, ) -> Result, RequestFailure> { let (tx, rx) = oneshot::channel(); - let _ = self.to_worker.unbounded_send(ServiceToWorkerMsg::Request { - target, - protocol: protocol.into(), - request, - pending_response: tx - }); + + self.start_request(target, protocol, request, tx, connect); match rx.await { Ok(v) => v, @@ -844,6 +850,32 @@ impl NetworkService { } } + /// Variation of `request` which starts a request whose response is delivered on a provided channel. + /// + /// Instead of blocking and waiting for a reply, this function returns immediately, sending + /// responses via the passed in sender. This alternative API exists to make it easier to + /// integrate with message passing APIs. + /// + /// Keep in mind that the connected receiver might receive a `Canceled` event in case of a + /// closing connection. This is expected behaviour. With `request` you would get a + /// `RequestFailure::Network(OutboundFailure::ConnectionClosed)` in that case. + pub fn start_request( + &self, + target: PeerId, + protocol: impl Into>, + request: Vec, + tx: oneshot::Sender, RequestFailure>>, + connect: IfDisconnected, + ) { + let _ = self.to_worker.unbounded_send(ServiceToWorkerMsg::Request { + target, + protocol: protocol.into(), + request, + pending_response: tx, + connect, + }); + } + /// You may call this when new transactons are imported by the transaction pool. /// /// All transactions will be fetched from the `TransactionPool` that was passed at @@ -1261,6 +1293,7 @@ enum ServiceToWorkerMsg { protocol: Cow<'static, str>, request: Vec, pending_response: oneshot::Sender, RequestFailure>>, + connect: IfDisconnected, }, DisconnectPeer(PeerId, Cow<'static, str>), NewBestBlockImported(B::Hash, NumberFor), @@ -1280,13 +1313,13 @@ pub struct NetworkWorker { /// The network service that can be extracted and shared through the codebase. service: Arc>, /// The *actual* network. - network_service: Swarm, + network_service: Swarm, /// The import queue that was passed at initialization. import_queue: Box>, /// Messages from the [`NetworkService`] that must be processed. from_service: TracingUnboundedReceiver>, /// Receiver for queries from the light client that must be processed. - light_client_rqs: Option>>, + light_client_rqs: Option>>, /// Senders for events that happen on the network. event_streams: out_events::OutChannels, /// Prometheus network metrics. @@ -1296,6 +1329,8 @@ pub struct NetworkWorker { /// For each peer and protocol combination, an object that allows sending notifications to /// that peer. Shared with the [`NetworkService`]. peers_notifications_sinks: Arc), NotificationsSink>>>, + /// Controller for the handler of incoming and outgoing transactions. + tx_handler_controller: transactions::TransactionsHandlerController, } impl Future for NetworkWorker { @@ -1312,10 +1347,14 @@ impl Future for NetworkWorker { // Check for new incoming light client requests. if let Some(light_client_rqs) = this.light_client_rqs.as_mut() { while let Poll::Ready(Some(rq)) = light_client_rqs.poll_next_unpin(cx) { - // This can error if there are too many queued requests already. - if this.network_service.light_client_request(rq).is_err() { - log::warn!("Couldn't start light client request: too many pending requests"); + let result = this.network_service.light_client_request(rq); + match result { + Ok(()) => {}, + Err(light_client_requests::sender::SendRequestError::TooManyRequests) => { + log::warn!("Couldn't start light client request: too many pending requests"); + } } + if let Some(metrics) = this.metrics.as_ref() { metrics.issued_light_requests.inc(); } @@ -1351,9 +1390,9 @@ impl Future for NetworkWorker { ServiceToWorkerMsg::RequestJustification(hash, number) => this.network_service.user_protocol_mut().request_justification(&hash, number), ServiceToWorkerMsg::PropagateTransaction(hash) => - this.network_service.user_protocol_mut().propagate_transaction(&hash), + this.tx_handler_controller.propagate_transaction(hash), ServiceToWorkerMsg::PropagateTransactions => - this.network_service.user_protocol_mut().propagate_transactions(), + this.tx_handler_controller.propagate_transactions(), ServiceToWorkerMsg::GetValue(key) => this.network_service.get_value(&key), ServiceToWorkerMsg::PutValue(key, value) => @@ -1380,8 +1419,8 @@ impl Future for NetworkWorker { this.network_service.user_protocol_mut().set_sync_fork_request(peer_ids, &hash, number), ServiceToWorkerMsg::EventStream(sender) => this.event_streams.push(sender), - ServiceToWorkerMsg::Request { target, protocol, request, pending_response } => { - this.network_service.send_request(&target, &protocol, request, pending_response); + ServiceToWorkerMsg::Request { target, protocol, request, pending_response, connect } => { + this.network_service.send_request(&target, &protocol, request, pending_response, connect); }, ServiceToWorkerMsg::DisconnectPeer(who, protocol_name) => this.network_service.user_protocol_mut().disconnect_peer(&who, &protocol_name), @@ -1732,7 +1771,7 @@ impl Future for NetworkWorker { // Update the variables shared with the `NetworkService`. this.num_connected.store(num_connected_peers, Ordering::Relaxed); { - let external_addresses = Swarm::::external_addresses(&this.network_service) + let external_addresses = Swarm::::external_addresses(&this.network_service) .map(|r| &r.addr) .cloned() .collect(); @@ -1744,6 +1783,8 @@ impl Future for NetworkWorker { SyncState::Downloading => true, }; + this.tx_handler_controller.set_gossip_enabled(!is_major_syncing); + this.is_major_syncing.store(is_major_syncing, Ordering::Relaxed); if let Some(metrics) = this.metrics.as_ref() { @@ -1775,14 +1816,14 @@ impl Unpin for NetworkWorker { } /// The libp2p swarm, customized for our needs. -type Swarm = libp2p::swarm::Swarm>; +type Swarm = libp2p::swarm::Swarm>; // Implementation of `import_queue::Link` trait using the available local variables. -struct NetworkLink<'a, B: BlockT, H: ExHashT> { - protocol: &'a mut Swarm, +struct NetworkLink<'a, B: BlockT> { + protocol: &'a mut Swarm, } -impl<'a, B: BlockT, H: ExHashT> Link for NetworkLink<'a, B, H> { +impl<'a, B: BlockT> Link for NetworkLink<'a, B> { fn blocks_processed( &mut self, imported: usize, diff --git a/client/network/src/service/tests.rs b/client/network/src/service/tests.rs index 8f16040aee3b1..defb9213a3493 100644 --- a/client/network/src/service/tests.rs +++ b/client/network/src/service/tests.rs @@ -18,6 +18,7 @@ use crate::{config, Event, NetworkService, NetworkWorker}; use crate::block_request_handler::BlockRequestHandler; +use crate::light_client_requests::handler::LightClientRequestHandler; use libp2p::PeerId; use futures::prelude::*; @@ -96,7 +97,16 @@ fn build_test_full_node(config: config::NetworkConfiguration) let block_request_protocol_config = { let (handler, protocol_config) = BlockRequestHandler::new( - protocol_id.clone(), + &protocol_id, + client.clone(), + ); + async_std::task::spawn(handler.run().boxed()); + protocol_config + }; + + let light_client_request_protocol_config = { + let (handler, protocol_config) = LightClientRequestHandler::new( + &protocol_id, client.clone(), ); async_std::task::spawn(handler.run().boxed()); @@ -106,6 +116,7 @@ fn build_test_full_node(config: config::NetworkConfiguration) let worker = NetworkWorker::new(config::Params { role: config::Role::Full, executor: None, + transactions_handler_executor: Box::new(|task| { async_std::task::spawn(task); }), network_config: config, chain: client.clone(), on_demand: None, @@ -117,6 +128,7 @@ fn build_test_full_node(config: config::NetworkConfiguration) ), metrics_registry: None, block_request_protocol_config, + light_client_request_protocol_config, }) .unwrap(); diff --git a/client/network/src/transactions.rs b/client/network/src/transactions.rs new file mode 100644 index 0000000000000..c011df1a9f8ea --- /dev/null +++ b/client/network/src/transactions.rs @@ -0,0 +1,486 @@ +// This file is part of Substrate. + +// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! Transactions handling to plug on top of the network service. +//! +//! Usage: +//! +//! - Use [`TransactionsHandlerPrototype::new`] to create a prototype. +//! - Pass the return value of [`TransactionsHandlerPrototype::set_config`] to the network +//! configuration as an extra peers set. +//! - Use [`TransactionsHandlerPrototype::build`] then [`TransactionsHandler::run`] to obtain a +//! `Future` that processes transactions. +//! + +use crate::{ + ExHashT, Event, ObservedRole, + config::{self, ProtocolId, TransactionPool, TransactionImportFuture, TransactionImport}, + error, protocol::message, service::NetworkService, utils::{interval, LruHashSet}, +}; + +use codec::{Decode, Encode}; +use futures::{channel::mpsc, prelude::*, stream::FuturesUnordered}; +use libp2p::{multiaddr, PeerId}; +use log::{trace, debug, warn}; +use prometheus_endpoint::{ + Registry, Counter, PrometheusError, register, U64 +}; +use sp_runtime::traits::Block as BlockT; +use std::borrow::Cow; +use std::collections::{HashMap, hash_map::Entry}; +use std::sync::{atomic::{AtomicBool, Ordering}, Arc}; +use std::{iter, num::NonZeroUsize, pin::Pin, task::Poll, time}; + +/// Interval at which we propagate transactions; +const PROPAGATE_TIMEOUT: time::Duration = time::Duration::from_millis(2900); + +/// Maximum number of known transaction hashes to keep for a peer. +/// +/// This should be approx. 2 blocks full of transactions for the network to function properly. +const MAX_KNOWN_TRANSACTIONS: usize = 10240; // ~300kb per peer + overhead. + +/// Maximum allowed size for a transactions notification. +const MAX_TRANSACTIONS_SIZE: u64 = 16 * 1024 * 1024; + +/// Maximum number of transaction validation request we keep at any moment. +const MAX_PENDING_TRANSACTIONS: usize = 8192; + +mod rep { + use sc_peerset::ReputationChange as Rep; + /// Reputation change when a peer sends us any transaction. + /// + /// This forces node to verify it, thus the negative value here. Once transaction is verified, + /// reputation change should be refunded with `ANY_TRANSACTION_REFUND` + pub const ANY_TRANSACTION: Rep = Rep::new(-(1 << 4), "Any transaction"); + /// Reputation change when a peer sends us any transaction that is not invalid. + pub const ANY_TRANSACTION_REFUND: Rep = Rep::new(1 << 4, "Any transaction (refund)"); + /// Reputation change when a peer sends us an transaction that we didn't know about. + pub const GOOD_TRANSACTION: Rep = Rep::new(1 << 7, "Good transaction"); + /// Reputation change when a peer sends us a bad transaction. + pub const BAD_TRANSACTION: Rep = Rep::new(-(1 << 12), "Bad transaction"); + /// We received an unexpected transaction packet. + pub const UNEXPECTED_TRANSACTIONS: Rep = Rep::new_fatal("Unexpected transactions packet"); +} + +struct Metrics { + propagated_transactions: Counter, +} + +impl Metrics { + fn register(r: &Registry) -> Result { + Ok(Metrics { + propagated_transactions: register(Counter::new( + "sync_propagated_transactions", + "Number of transactions propagated to at least one peer", + )?, r)?, + }) + } +} + +#[pin_project::pin_project] +struct PendingTransaction { + #[pin] + validation: TransactionImportFuture, + tx_hash: H, +} + +impl Future for PendingTransaction { + type Output = (H, TransactionImport); + + fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll { + let mut this = self.project(); + + if let Poll::Ready(import_result) = Pin::new(&mut this.validation).poll_unpin(cx) { + return Poll::Ready((this.tx_hash.clone(), import_result)); + } + + Poll::Pending + } +} + +/// Prototype for a [`TransactionsHandler`]. +pub struct TransactionsHandlerPrototype { + protocol_name: Cow<'static, str>, +} + +impl TransactionsHandlerPrototype { + /// Create a new instance. + pub fn new(protocol_id: ProtocolId) -> Self { + TransactionsHandlerPrototype { + protocol_name: Cow::from({ + let mut proto = String::new(); + proto.push_str("/"); + proto.push_str(protocol_id.as_ref()); + proto.push_str("/transactions/1"); + proto + }) + } + } + + /// Returns the configuration of the set to put in the network configuration. + pub fn set_config(&self) -> config::NonDefaultSetConfig { + config::NonDefaultSetConfig { + notifications_protocol: self.protocol_name.clone(), + max_notification_size: MAX_TRANSACTIONS_SIZE, + set_config: config::SetConfig { + in_peers: 0, + out_peers: 0, + reserved_nodes: Vec::new(), + non_reserved_mode: config::NonReservedPeerMode::Deny, + } + } + } + + /// Turns the prototype into the actual handler. Returns a controller that allows controlling + /// the behaviour of the handler while it's running. + /// + /// Important: the transactions handler is initially disabled and doesn't gossip transactions. + /// You must call [`TransactionsHandlerController::set_gossip_enabled`] to enable it. + pub fn build( + self, + service: Arc>, + local_role: config::Role, + transaction_pool: Arc>, + metrics_registry: Option<&Registry>, + ) -> error::Result<(TransactionsHandler, TransactionsHandlerController)> { + let event_stream = service.event_stream("transactions-handler").boxed(); + let (to_handler, from_controller) = mpsc::unbounded(); + let gossip_enabled = Arc::new(AtomicBool::new(false)); + + let handler = TransactionsHandler { + protocol_name: self.protocol_name, + propagate_timeout: Box::pin(interval(PROPAGATE_TIMEOUT)), + pending_transactions: FuturesUnordered::new(), + pending_transactions_peers: HashMap::new(), + gossip_enabled: gossip_enabled.clone(), + service, + event_stream, + peers: HashMap::new(), + transaction_pool, + local_role, + from_controller, + metrics: if let Some(r) = metrics_registry { + Some(Metrics::register(r)?) + } else { + None + }, + }; + + let controller = TransactionsHandlerController { + to_handler, + gossip_enabled, + }; + + Ok((handler, controller)) + } +} + +/// Controls the behaviour of a [`TransactionsHandler`] it is connected to. +pub struct TransactionsHandlerController { + to_handler: mpsc::UnboundedSender>, + gossip_enabled: Arc, +} + +impl TransactionsHandlerController { + /// Controls whether transactions are being gossiped on the network. + pub fn set_gossip_enabled(&mut self, enabled: bool) { + self.gossip_enabled.store(enabled, Ordering::Relaxed); + } + + /// You may call this when new transactions are imported by the transaction pool. + /// + /// All transactions will be fetched from the `TransactionPool` that was passed at + /// initialization as part of the configuration and propagated to peers. + pub fn propagate_transactions(&self) { + let _ = self.to_handler.unbounded_send(ToHandler::PropagateTransactions); + } + + /// You must call when new a transaction is imported by the transaction pool. + /// + /// This transaction will be fetched from the `TransactionPool` that was passed at + /// initialization as part of the configuration and propagated to peers. + pub fn propagate_transaction(&self, hash: H) { + let _ = self.to_handler.unbounded_send(ToHandler::PropagateTransaction(hash)); + } +} + +enum ToHandler { + PropagateTransactions, + PropagateTransaction(H), +} + +/// Handler for transactions. Call [`TransactionsHandler::run`] to start the processing. +pub struct TransactionsHandler { + protocol_name: Cow<'static, str>, + /// Interval at which we call `propagate_transactions`. + propagate_timeout: Pin + Send>>, + /// Pending transactions verification tasks. + pending_transactions: FuturesUnordered>, + /// As multiple peers can send us the same transaction, we group + /// these peers using the transaction hash while the transaction is + /// imported. This prevents that we import the same transaction + /// multiple times concurrently. + pending_transactions_peers: HashMap>, + /// Network service to use to send messages and manage peers. + service: Arc>, + /// Stream of networking events. + event_stream: Pin + Send>>, + // All connected peers + peers: HashMap>, + transaction_pool: Arc>, + gossip_enabled: Arc, + local_role: config::Role, + from_controller: mpsc::UnboundedReceiver>, + /// Prometheus metrics. + metrics: Option, +} + +/// Peer information +#[derive(Debug)] +struct Peer { + /// Holds a set of transactions known to this peer. + known_transactions: LruHashSet, + role: ObservedRole, +} + +impl TransactionsHandler { + /// Turns the [`TransactionsHandler`] into a future that should run forever and not be + /// interrupted. + pub async fn run(mut self) { + loop { + futures::select!{ + _ = self.propagate_timeout.next().fuse() => { + self.propagate_transactions(); + }, + (tx_hash, result) = self.pending_transactions.select_next_some() => { + if let Some(peers) = self.pending_transactions_peers.remove(&tx_hash) { + peers.into_iter().for_each(|p| self.on_handle_transaction_import(p, result)); + } else { + warn!(target: "sub-libp2p", "Inconsistent state, no peers for pending transaction!"); + } + }, + network_event = self.event_stream.next().fuse() => { + if let Some(network_event) = network_event { + self.handle_network_event(network_event).await; + } else { + // Networking has seemingly closed. Closing as well. + return; + } + }, + message = self.from_controller.select_next_some().fuse() => { + match message { + ToHandler::PropagateTransaction(hash) => self.propagate_transaction(&hash), + ToHandler::PropagateTransactions => self.propagate_transactions(), + } + }, + } + } + } + + async fn handle_network_event(&mut self, event: Event) { + match event { + Event::Dht(_) => {}, + Event::SyncConnected { remote } => { + let addr = iter::once(multiaddr::Protocol::P2p(remote.into())) + .collect::(); + let result = self.service.add_peers_to_reserved_set( + self.protocol_name.clone(), + iter::once(addr).collect() + ); + if let Err(err) = result { + log::error!(target: "sync", "Add reserved peer failed: {}", err); + } + }, + Event::SyncDisconnected { remote } => { + let addr = iter::once(multiaddr::Protocol::P2p(remote.into())) + .collect::(); + let result = self.service.remove_peers_from_reserved_set( + self.protocol_name.clone(), + iter::once(addr).collect() + ); + if let Err(err) = result { + log::error!(target: "sync", "Removing reserved peer failed: {}", err); + } + }, + + Event::NotificationStreamOpened { remote, protocol, role } if protocol == self.protocol_name => { + self.peers.insert(remote, Peer { + known_transactions: LruHashSet::new(NonZeroUsize::new(MAX_KNOWN_TRANSACTIONS) + .expect("Constant is nonzero")), + role, + }); + } + Event::NotificationStreamClosed { remote, protocol } if protocol == self.protocol_name => { + self.peers.remove(&remote); + } + + Event::NotificationsReceived { remote, messages } => { + for (protocol, message) in messages { + if protocol != self.protocol_name { + continue; + } + + if let Ok(m) = as Decode>::decode( + &mut message.as_ref(), + ) { + self.on_transactions(remote, m); + } else { + warn!(target: "sub-libp2p", "Failed to decode transactions list"); + } + } + }, + + // Not our concern. + Event::NotificationStreamOpened { .. } | Event::NotificationStreamClosed { .. } => {} + } + } + + /// Called when peer sends us new transactions + fn on_transactions( + &mut self, + who: PeerId, + transactions: message::Transactions, + ) { + // sending transaction to light node is considered a bad behavior + if matches!(self.local_role, config::Role::Light) { + trace!(target: "sync", "Peer {} is trying to send transactions to the light node", who); + self.service.disconnect_peer(who, self.protocol_name.clone()); + self.service.report_peer(who, rep::UNEXPECTED_TRANSACTIONS); + return; + } + + // Accept transactions only when enabled + if !self.gossip_enabled.load(Ordering::Relaxed) { + trace!(target: "sync", "{} Ignoring transactions while disabled", who); + return; + } + + trace!(target: "sync", "Received {} transactions from {}", transactions.len(), who); + if let Some(ref mut peer) = self.peers.get_mut(&who) { + for t in transactions { + if self.pending_transactions.len() > MAX_PENDING_TRANSACTIONS { + debug!( + target: "sync", + "Ignoring any further transactions that exceed `MAX_PENDING_TRANSACTIONS`({}) limit", + MAX_PENDING_TRANSACTIONS, + ); + break; + } + + let hash = self.transaction_pool.hash_of(&t); + peer.known_transactions.insert(hash.clone()); + + self.service.report_peer(who.clone(), rep::ANY_TRANSACTION); + + match self.pending_transactions_peers.entry(hash.clone()) { + Entry::Vacant(entry) => { + self.pending_transactions.push(PendingTransaction { + validation: self.transaction_pool.import(t), + tx_hash: hash, + }); + entry.insert(vec![who.clone()]); + }, + Entry::Occupied(mut entry) => { + entry.get_mut().push(who.clone()); + } + } + } + } + } + + fn on_handle_transaction_import(&mut self, who: PeerId, import: TransactionImport) { + match import { + TransactionImport::KnownGood => self.service.report_peer(who, rep::ANY_TRANSACTION_REFUND), + TransactionImport::NewGood => self.service.report_peer(who, rep::GOOD_TRANSACTION), + TransactionImport::Bad => self.service.report_peer(who, rep::BAD_TRANSACTION), + TransactionImport::None => {}, + } + } + + /// Propagate one transaction. + pub fn propagate_transaction( + &mut self, + hash: &H, + ) { + debug!(target: "sync", "Propagating transaction [{:?}]", hash); + // Accept transactions only when enabled + if !self.gossip_enabled.load(Ordering::Relaxed) { + return; + } + if let Some(transaction) = self.transaction_pool.transaction(hash) { + let propagated_to = self.do_propagate_transactions(&[(hash.clone(), transaction)]); + self.transaction_pool.on_broadcasted(propagated_to); + } + } + + fn do_propagate_transactions( + &mut self, + transactions: &[(H, B::Extrinsic)], + ) -> HashMap> { + let mut propagated_to = HashMap::<_, Vec<_>>::new(); + let mut propagated_transactions = 0; + + for (who, peer) in self.peers.iter_mut() { + // never send transactions to the light node + if matches!(peer.role, ObservedRole::Light) { + continue; + } + + let (hashes, to_send): (Vec<_>, Vec<_>) = transactions + .iter() + .filter(|&(ref hash, _)| peer.known_transactions.insert(hash.clone())) + .cloned() + .unzip(); + + propagated_transactions += hashes.len(); + + if !to_send.is_empty() { + for hash in hashes { + propagated_to + .entry(hash) + .or_default() + .push(who.to_base58()); + } + trace!(target: "sync", "Sending {} transactions to {}", to_send.len(), who); + self.service.write_notification( + who.clone(), + self.protocol_name.clone(), + to_send.encode() + ); + } + } + + if let Some(ref metrics) = self.metrics { + metrics.propagated_transactions.inc_by(propagated_transactions as _) + } + + propagated_to + } + + /// Call when we must propagate ready transactions to peers. + fn propagate_transactions(&mut self) { + // Accept transactions only when enabled + if !self.gossip_enabled.load(Ordering::Relaxed) { + return; + } + debug!(target: "sync", "Propagating transactions"); + let transactions = self.transaction_pool.transactions(); + let propagated_to = self.do_propagate_transactions(&transactions); + self.transaction_pool.on_broadcasted(propagated_to); + } +} diff --git a/client/network/test/Cargo.toml b/client/network/test/Cargo.toml index 20265f7680a9d..009315084cc32 100644 --- a/client/network/test/Cargo.toml +++ b/client/network/test/Cargo.toml @@ -14,23 +14,23 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] async-std = "1.6.5" -sc-network = { version = "0.8.0", path = "../" } +sc-network = { version = "0.9.0", path = "../" } log = "0.4.8" parking_lot = "0.11.1" futures = "0.3.9" futures-timer = "3.0.1" rand = "0.7.2" -libp2p = { version = "0.34.0", default-features = false } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -sc-consensus = { version = "0.8.0", path = "../../consensus/common" } -sc-client-api = { version = "2.0.0", path = "../../api" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sc-block-builder = { version = "0.8.0", path = "../../block-builder" } -sp-consensus-babe = { version = "0.8.0", path = "../../../primitives/consensus/babe" } +libp2p = { version = "0.35.1", default-features = false } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sc-consensus = { version = "0.9.0", path = "../../consensus/common" } +sc-client-api = { version = "3.0.0", path = "../../api" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sc-block-builder = { version = "0.9.0", path = "../../block-builder" } +sp-consensus-babe = { version = "0.9.0", path = "../../../primitives/consensus/babe" } substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" } substrate-test-runtime = { version = "2.0.0", path = "../../../test-utils/runtime" } tempfile = "3.1.0" -sp-tracing = { version = "2.0.0", path = "../../../primitives/tracing" } -sc-service = { version = "0.8.0", default-features = false, features = ["test-helpers"], path = "../../service" } +sp-tracing = { version = "3.0.0", path = "../../../primitives/tracing" } +sc-service = { version = "0.9.0", default-features = false, features = ["test-helpers"], path = "../../service" } diff --git a/client/network/test/src/lib.rs b/client/network/test/src/lib.rs index 786fddeed5554..6e2380b284783 100644 --- a/client/network/test/src/lib.rs +++ b/client/network/test/src/lib.rs @@ -30,6 +30,7 @@ use std::{ use libp2p::build_multiaddr; use log::trace; use sc_network::block_request_handler::{self, BlockRequestHandler}; +use sc_network::light_client_requests::{self, handler::LightClientRequestHandler}; use sp_blockchain::{ HeaderBackend, Result as ClientResult, well_known_cache_keys::{self, Id as CacheKeyId}, @@ -726,7 +727,13 @@ pub trait TestNetFactory: Sized { let protocol_id = ProtocolId::from("test-protocol-name"); let block_request_protocol_config = { - let (handler, protocol_config) = BlockRequestHandler::new(protocol_id.clone(), client.clone()); + let (handler, protocol_config) = BlockRequestHandler::new(&protocol_id, client.clone()); + self.spawn_task(handler.run().boxed()); + protocol_config + }; + + let light_client_request_protocol_config = { + let (handler, protocol_config) = LightClientRequestHandler::new(&protocol_id, client.clone()); self.spawn_task(handler.run().boxed()); protocol_config }; @@ -734,6 +741,7 @@ pub trait TestNetFactory: Sized { let network = NetworkWorker::new(sc_network::config::Params { role: Role::Full, executor: None, + transactions_handler_executor: Box::new(|task| { async_std::task::spawn(task); }), network_config, chain: client.clone(), on_demand: None, @@ -744,6 +752,7 @@ pub trait TestNetFactory: Sized { .unwrap_or_else(|| Box::new(DefaultBlockAnnounceValidator)), metrics_registry: None, block_request_protocol_config, + light_client_request_protocol_config, }).unwrap(); trace!(target: "test_network", "Peer identifier: {}", network.service().local_peer_id()); @@ -813,14 +822,17 @@ pub trait TestNetFactory: Sized { let protocol_id = ProtocolId::from("test-protocol-name"); - // Add block request handler. let block_request_protocol_config = block_request_handler::generate_protocol_config( - protocol_id.clone(), + &protocol_id, ); + let light_client_request_protocol_config = + light_client_requests::generate_protocol_config(&protocol_id); + let network = NetworkWorker::new(sc_network::config::Params { role: Role::Light, executor: None, + transactions_handler_executor: Box::new(|task| { async_std::task::spawn(task); }), network_config, chain: client.clone(), on_demand: None, @@ -830,6 +842,7 @@ pub trait TestNetFactory: Sized { block_announce_validator: Box::new(DefaultBlockAnnounceValidator), metrics_registry: None, block_request_protocol_config, + light_client_request_protocol_config, }).unwrap(); self.mut_peers(|peers| { diff --git a/client/offchain/Cargo.toml b/client/offchain/Cargo.toml index 7d0f01a0c7ed7..5671affb6fb75 100644 --- a/client/offchain/Cargo.toml +++ b/client/offchain/Cargo.toml @@ -1,7 +1,7 @@ [package] description = "Substrate offchain workers" name = "sc-offchain" -version = "2.0.1" +version = "3.0.0" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" authors = ["Parity Technologies "] edition = "2018" @@ -14,35 +14,35 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] bytes = "0.5" -sc-client-api = { version = "2.0.0", path = "../api" } -sp-api = { version = "2.0.0", path = "../../primitives/api" } +sc-client-api = { version = "3.0.0", path = "../api" } +sp-api = { version = "3.0.0", path = "../../primitives/api" } fnv = "1.0.6" futures = "0.3.9" futures-timer = "3.0.1" log = "0.4.8" threadpool = "1.7" num_cpus = "1.10" -sp-offchain = { version = "2.0.0", path = "../../primitives/offchain" } -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } +sp-offchain = { version = "3.0.0", path = "../../primitives/offchain" } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } parking_lot = "0.11.1" -sp-core = { version = "2.0.0", path = "../../primitives/core" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } rand = "0.7.2" -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-utils = { version = "2.0.0", path = "../../primitives/utils" } -sc-network = { version = "0.8.0", path = "../network" } -sc-keystore = { version = "2.0.0", path = "../keystore" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-utils = { version = "3.0.0", path = "../../primitives/utils" } +sc-network = { version = "0.9.0", path = "../network" } +sc-keystore = { version = "3.0.0", path = "../keystore" } [target.'cfg(not(target_os = "unknown"))'.dependencies] hyper = "0.13.9" hyper-rustls = "0.21.0" [dev-dependencies] -sc-client-db = { version = "0.8.0", default-features = true, path = "../db" } -sc-block-builder = { version = "0.8.0", path = "../block-builder" } -sc-transaction-pool = { version = "2.0.0", path = "../transaction-pool" } -sp-transaction-pool = { version = "2.0.0", path = "../../primitives/transaction-pool" } -sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } -sp-consensus = { version = "0.8.1", path = "../../primitives/consensus/common" } +sc-client-db = { version = "0.9.0", default-features = true, path = "../db" } +sc-block-builder = { version = "0.9.0", path = "../block-builder" } +sc-transaction-pool = { version = "3.0.0", path = "../transaction-pool" } +sp-transaction-pool = { version = "3.0.0", path = "../../primitives/transaction-pool" } +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } +sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } tokio = "0.2" lazy_static = "1.4.0" diff --git a/client/offchain/src/lib.rs b/client/offchain/src/lib.rs index b82f89cb95d67..f456efb755dcb 100644 --- a/client/offchain/src/lib.rs +++ b/client/offchain/src/lib.rs @@ -132,10 +132,10 @@ impl OffchainWorkers< ) -> impl Future { let runtime = self.client.runtime_api(); let at = BlockId::hash(header.hash()); - let has_api_v1 = runtime.has_api_with::, _>( + let has_api_v1 = runtime.has_api_with::, _>( &at, |v| v == 1 ); - let has_api_v2 = runtime.has_api_with::, _>( + let has_api_v2 = runtime.has_api_with::, _>( &at, |v| v == 2 ); let version = match (has_api_v1, has_api_v2) { diff --git a/client/peerset/Cargo.toml b/client/peerset/Cargo.toml index 1ebb6bde52a65..536ec6b681753 100644 --- a/client/peerset/Cargo.toml +++ b/client/peerset/Cargo.toml @@ -3,7 +3,7 @@ description = "Connectivity manager based on reputation" homepage = "http://parity.io" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" name = "sc-peerset" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" repository = "https://github.com/paritytech/substrate/" @@ -16,8 +16,8 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] futures = "0.3.9" -libp2p = { version = "0.34.0", default-features = false } -sp-utils = { version = "2.0.0", path = "../../primitives/utils"} +libp2p = { version = "0.35.1", default-features = false } +sp-utils = { version = "3.0.0", path = "../../primitives/utils"} log = "0.4.8" serde_json = "1.0.41" wasm-timer = "0.2" diff --git a/client/proposer-metrics/Cargo.toml b/client/proposer-metrics/Cargo.toml index 29a5701bc9e4d..ffe5045461f77 100644 --- a/client/proposer-metrics/Cargo.toml +++ b/client/proposer-metrics/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-proposer-metrics" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -14,4 +14,4 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] log = "0.4.8" -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.8.0"} +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.9.0"} diff --git a/client/rpc-api/Cargo.toml b/client/rpc-api/Cargo.toml index 546deb1283c57..d213decdbc779 100644 --- a/client/rpc-api/Cargo.toml +++ b/client/rpc-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-rpc-api" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -13,7 +13,7 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } derive_more = "0.99.2" futures = { version = "0.3.1", features = ["compat"] } jsonrpc-core = "15.1.0" @@ -22,11 +22,11 @@ jsonrpc-derive = "15.1.0" jsonrpc-pubsub = "15.1.0" log = "0.4.8" parking_lot = "0.11.1" -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-version = { version = "2.0.0", path = "../../primitives/version" } -sp-runtime = { path = "../../primitives/runtime" , version = "2.0.0"} -sp-chain-spec = { path = "../../primitives/chain-spec" , version = "2.0.0"} +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-version = { version = "3.0.0", path = "../../primitives/version" } +sp-runtime = { path = "../../primitives/runtime" , version = "3.0.0"} +sp-chain-spec = { path = "../../primitives/chain-spec" , version = "3.0.0"} serde = { version = "1.0.101", features = ["derive"] } serde_json = "1.0.41" -sp-transaction-pool = { version = "2.0.0", path = "../../primitives/transaction-pool" } -sp-rpc = { version = "2.0.0", path = "../../primitives/rpc" } +sp-transaction-pool = { version = "3.0.0", path = "../../primitives/transaction-pool" } +sp-rpc = { version = "3.0.0", path = "../../primitives/rpc" } diff --git a/client/rpc-api/src/system/helpers.rs b/client/rpc-api/src/system/helpers.rs index b2b793a8ee400..c2fc807471f38 100644 --- a/client/rpc-api/src/system/helpers.rs +++ b/client/rpc-api/src/system/helpers.rs @@ -82,8 +82,6 @@ pub enum NodeRole { LightClient, /// The node is an authority Authority, - /// The node is a sentry - Sentry, } /// The state of the syncing of the node. diff --git a/client/rpc-api/src/system/mod.rs b/client/rpc-api/src/system/mod.rs index 2cf22b9802993..2e8a7aa12633b 100644 --- a/client/rpc-api/src/system/mod.rs +++ b/client/rpc-api/src/system/mod.rs @@ -79,9 +79,11 @@ pub trait SystemApi { /// Returns current state of the network. /// - /// **Warning**: This API is not stable. - // TODO: make this stable and move structs https://github.com/paritytech/substrate/issues/1890 - #[rpc(name = "system_networkState", returns = "jsonrpc_core::Value")] + /// **Warning**: This API is not stable. Please do not programmatically interpret its output, + /// as its format might change at any time. + // TODO: the future of this call is uncertain: https://github.com/paritytech/substrate/issues/1890 + // https://github.com/paritytech/substrate/issues/5541 + #[rpc(name = "system_unstable_networkState", returns = "jsonrpc_core::Value")] fn system_network_state(&self) -> Compat>>; diff --git a/client/rpc-servers/Cargo.toml b/client/rpc-servers/Cargo.toml index 0ee186923e8f1..95c3e4194cd51 100644 --- a/client/rpc-servers/Cargo.toml +++ b/client/rpc-servers/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-rpc-server" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -17,10 +17,10 @@ futures = "0.1.6" jsonrpc-core = "15.1.0" pubsub = { package = "jsonrpc-pubsub", version = "15.1.0" } log = "0.4.8" -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.8.0"} +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.9.0"} serde = "1.0.101" serde_json = "1.0.41" -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } [target.'cfg(not(target_os = "unknown"))'.dependencies] http = { package = "jsonrpc-http-server", version = "15.1.0" } diff --git a/client/rpc/Cargo.toml b/client/rpc/Cargo.toml index bab436c93a7f2..203bb0e525d8b 100644 --- a/client/rpc/Cargo.toml +++ b/client/rpc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-rpc" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -13,31 +13,31 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sc-rpc-api = { version = "0.8.0", path = "../rpc-api" } -sc-client-api = { version = "2.0.0", path = "../api" } -sp-api = { version = "2.0.0", path = "../../primitives/api" } -codec = { package = "parity-scale-codec", version = "1.3.6" } +sc-rpc-api = { version = "0.9.0", path = "../rpc-api" } +sc-client-api = { version = "3.0.0", path = "../api" } +sp-api = { version = "3.0.0", path = "../../primitives/api" } +codec = { package = "parity-scale-codec", version = "2.0.0" } futures = { version = "0.3.1", features = ["compat"] } jsonrpc-pubsub = "15.1.0" log = "0.4.8" -sp-core = { version = "2.0.0", path = "../../primitives/core" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } rpc = { package = "jsonrpc-core", version = "15.1.0" } -sp-version = { version = "2.0.0", path = "../../primitives/version" } +sp-version = { version = "3.0.0", path = "../../primitives/version" } serde_json = "1.0.41" -sp-session = { version = "2.0.0", path = "../../primitives/session" } -sp-offchain = { version = "2.0.0", path = "../../primitives/offchain" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-utils = { version = "2.0.0", path = "../../primitives/utils" } -sp-rpc = { version = "2.0.0", path = "../../primitives/rpc" } -sp-keystore = { version = "0.8.0", path = "../../primitives/keystore" } -sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine" } -sp-chain-spec = { version = "2.0.0", path = "../../primitives/chain-spec" } -sc-executor = { version = "0.8.0", path = "../executor" } -sc-block-builder = { version = "0.8.0", path = "../block-builder" } -sc-keystore = { version = "2.0.0", path = "../keystore" } -sp-transaction-pool = { version = "2.0.0", path = "../../primitives/transaction-pool" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sc-tracing = { version = "2.0.0", path = "../tracing" } +sp-session = { version = "3.0.0", path = "../../primitives/session" } +sp-offchain = { version = "3.0.0", path = "../../primitives/offchain" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-utils = { version = "3.0.0", path = "../../primitives/utils" } +sp-rpc = { version = "3.0.0", path = "../../primitives/rpc" } +sp-keystore = { version = "0.9.0", path = "../../primitives/keystore" } +sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" } +sp-chain-spec = { version = "3.0.0", path = "../../primitives/chain-spec" } +sc-executor = { version = "0.9.0", path = "../executor" } +sc-block-builder = { version = "0.9.0", path = "../block-builder" } +sc-keystore = { version = "3.0.0", path = "../keystore" } +sp-transaction-pool = { version = "3.0.0", path = "../../primitives/transaction-pool" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sc-tracing = { version = "3.0.0", path = "../tracing" } hash-db = { version = "0.15.2", default-features = false } parking_lot = "0.11.1" lazy_static = { version = "1.4.0", optional = true } @@ -46,12 +46,12 @@ lazy_static = { version = "1.4.0", optional = true } assert_matches = "1.3.0" futures01 = { package = "futures", version = "0.1.29" } lazy_static = "1.4.0" -sc-network = { version = "0.8.0", path = "../network" } -sp-io = { version = "2.0.0", path = "../../primitives/io" } +sc-network = { version = "0.9.0", path = "../network" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } tokio = "0.1.22" -sc-transaction-pool = { version = "2.0.0", path = "../transaction-pool" } -sc-cli = { version = "0.8.0", path = "../cli" } +sc-transaction-pool = { version = "3.0.0", path = "../transaction-pool" } +sc-cli = { version = "0.9.0", path = "../cli" } [features] test-helpers = ["lazy_static"] diff --git a/client/rpc/src/author/mod.rs b/client/rpc/src/author/mod.rs index 7cd980544503b..4181206fdd0a7 100644 --- a/client/rpc/src/author/mod.rs +++ b/client/rpc/src/author/mod.rs @@ -24,12 +24,9 @@ mod tests; use std::{sync::Arc, convert::TryInto}; use log::warn; -use sp_blockchain::{Error as ClientError, HeaderBackend}; +use sp_blockchain::HeaderBackend; -use rpc::futures::{ - Sink, Future, - future::result, -}; +use rpc::futures::{Sink, Future, future::result}; use futures::{StreamExt as _, compat::Compat}; use futures::future::{ready, FutureExt, TryFutureExt}; use sc_rpc_api::DenyUnsafe; @@ -93,7 +90,7 @@ impl AuthorApi, BlockHash

> for Author where P: TransactionPool + Sync + Send + 'static, Client: HeaderBackend + ProvideRuntimeApi + Send + Sync + 'static, - Client::Api: SessionKeys, + Client::Api: SessionKeys, { type Metadata = crate::Metadata; diff --git a/client/rpc/src/state/mod.rs b/client/rpc/src/state/mod.rs index 52a4ed1d753b5..a3d83ae250d0e 100644 --- a/client/rpc/src/state/mod.rs +++ b/client/rpc/src/state/mod.rs @@ -178,9 +178,8 @@ pub fn new_full( BE: Backend + 'static, Client: ExecutorProvider + StorageProvider + ProofProvider + HeaderBackend + HeaderMetadata + BlockchainEvents - + CallApiAt - + ProvideRuntimeApi + Send + Sync + 'static, - Client::Api: Metadata, + + CallApiAt + ProvideRuntimeApi + Send + Sync + 'static, + Client::Api: Metadata, { let child_backend = Box::new( self::state_full::FullState::new(client.clone(), subscriptions.clone()) diff --git a/client/rpc/src/state/state_full.rs b/client/rpc/src/state/state_full.rs index 8d93d445b08cb..a55903484adc2 100644 --- a/client/rpc/src/state/state_full.rs +++ b/client/rpc/src/state/state_full.rs @@ -223,9 +223,9 @@ impl StateBackend for FullState + 'static, Client: ExecutorProvider + StorageProvider + ProofProvider + HeaderBackend + HeaderMetadata + BlockchainEvents - + CallApiAt + ProvideRuntimeApi + + CallApiAt + ProvideRuntimeApi + Send + Sync + 'static, - Client::Api: Metadata, + Client::Api: Metadata, { fn call( &self, @@ -344,17 +344,23 @@ impl StateBackend for FullState) -> FutureResult { Box::new(result( self.block_or_best(block) + .map_err(client_err) .and_then(|block| - self.client.runtime_api().metadata(&BlockId::Hash(block)).map(Into::into) - ) - .map_err(client_err))) + self.client.runtime_api().metadata(&BlockId::Hash(block)) + .map(Into::into) + .map_err(|e| Error::Client(Box::new(e)))) + )) } fn runtime_version(&self, block: Option) -> FutureResult { Box::new(result( self.block_or_best(block) - .and_then(|block| self.client.runtime_version_at(&BlockId::Hash(block))) - .map_err(client_err))) + .map_err(client_err) + .and_then(|block| + self.client.runtime_version_at(&BlockId::Hash(block)) + .map_err(|e| Error::Client(Box::new(e))) + ) + )) } fn query_storage( @@ -432,7 +438,7 @@ impl StateBackend for FullState ChildStateBackend for FullState + 'static, Client: ExecutorProvider + StorageProvider + HeaderBackend + HeaderMetadata + BlockchainEvents - + CallApiAt + ProvideRuntimeApi + + CallApiAt + ProvideRuntimeApi + Send + Sync + 'static, - Client::Api: Metadata, + Client::Api: Metadata, { fn storage_keys( &self, diff --git a/client/rpc/src/state/state_light.rs b/client/rpc/src/state/state_light.rs index c1294dd27b08f..c8c921345877c 100644 --- a/client/rpc/src/state/state_light.rs +++ b/client/rpc/src/state/state_light.rs @@ -590,7 +590,7 @@ fn runtime_version>( ) .then(|version| ready(version.and_then(|version| Decode::decode(&mut &version.0[..]) - .map_err(|e| client_err(ClientError::VersionInvalid(e.what().into()))) + .map_err(|e| client_err(ClientError::VersionInvalid(e.to_string()))) ))) } diff --git a/client/rpc/src/system/tests.rs b/client/rpc/src/system/tests.rs index 89676acae26bf..c196403501035 100644 --- a/client/rpc/src/system/tests.rs +++ b/client/rpc/src/system/tests.rs @@ -344,7 +344,7 @@ fn test_add_reset_log_filter() { // Enter log generation / filter reload if std::env::var("TEST_LOG_FILTER").is_ok() { - sc_tracing::logging::GlobalLoggerBuilder::new("test_before_add=debug").init().unwrap(); + sc_tracing::logging::LoggerBuilder::new("test_before_add=debug").init().unwrap(); for line in std::io::stdin().lock().lines() { let line = line.expect("Failed to read bytes"); if line.contains("add_reload") { diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index 95ce02e195f1e..c6119695ace71 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-service" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -36,48 +36,48 @@ log = "0.4.11" futures-timer = "3.0.1" wasm-timer = "0.2" exit-future = "0.2.0" -pin-project = "0.4.8" +pin-project = "1.0.4" hash-db = "0.15.2" serde = "1.0.101" serde_json = "1.0.41" -sc-keystore = { version = "2.0.0", path = "../keystore" } -sp-io = { version = "2.0.0", path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-trie = { version = "2.0.0", path = "../../primitives/trie" } -sp-externalities = { version = "0.8.0", path = "../../primitives/externalities" } -sp-utils = { version = "2.0.0", path = "../../primitives/utils" } -sp-version = { version = "2.0.0", path = "../../primitives/version" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-keystore = { version = "0.8.0", path = "../../primitives/keystore" } -sp-session = { version = "2.0.0", path = "../../primitives/session" } -sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine" } -sp-application-crypto = { version = "2.0.0", path = "../../primitives/application-crypto" } -sp-consensus = { version = "0.8.0", path = "../../primitives/consensus/common" } -sp-inherents = { version = "2.0.0", path = "../../primitives/inherents" } -sc-network = { version = "0.8.0", path = "../network" } -sc-chain-spec = { version = "2.0.0", path = "../chain-spec" } -sc-light = { version = "2.0.0", path = "../light" } -sc-client-api = { version = "2.0.0", path = "../api" } -sp-api = { version = "2.0.0", path = "../../primitives/api" } -sc-client-db = { version = "0.8.0", default-features = false, path = "../db" } -codec = { package = "parity-scale-codec", version = "1.3.6" } -sc-executor = { version = "0.8.0", path = "../executor" } -sc-transaction-pool = { version = "2.0.0", path = "../transaction-pool" } -sp-transaction-pool = { version = "2.0.0", path = "../../primitives/transaction-pool" } -sc-rpc-server = { version = "2.0.0", path = "../rpc-servers" } -sc-rpc = { version = "2.0.0", path = "../rpc" } -sc-block-builder = { version = "0.8.0", path = "../block-builder" } -sp-block-builder = { version = "2.0.0", path = "../../primitives/block-builder" } -sc-informant = { version = "0.8.0", path = "../informant" } -sc-telemetry = { version = "2.0.0", path = "../telemetry" } -sc-offchain = { version = "2.0.0", path = "../offchain" } -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.8.0"} -sc-tracing = { version = "2.0.0", path = "../tracing" } -sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } +sc-keystore = { version = "3.0.0", path = "../keystore" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-trie = { version = "3.0.0", path = "../../primitives/trie" } +sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" } +sp-utils = { version = "3.0.0", path = "../../primitives/utils" } +sp-version = { version = "3.0.0", path = "../../primitives/version" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-keystore = { version = "0.9.0", path = "../../primitives/keystore" } +sp-session = { version = "3.0.0", path = "../../primitives/session" } +sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" } +sp-application-crypto = { version = "3.0.0", path = "../../primitives/application-crypto" } +sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sp-inherents = { version = "3.0.0", path = "../../primitives/inherents" } +sc-network = { version = "0.9.0", path = "../network" } +sc-chain-spec = { version = "3.0.0", path = "../chain-spec" } +sc-light = { version = "3.0.0", path = "../light" } +sc-client-api = { version = "3.0.0", path = "../api" } +sp-api = { version = "3.0.0", path = "../../primitives/api" } +sc-client-db = { version = "0.9.0", default-features = false, path = "../db" } +codec = { package = "parity-scale-codec", version = "2.0.0" } +sc-executor = { version = "0.9.0", path = "../executor" } +sc-transaction-pool = { version = "3.0.0", path = "../transaction-pool" } +sp-transaction-pool = { version = "3.0.0", path = "../../primitives/transaction-pool" } +sc-rpc-server = { version = "3.0.0", path = "../rpc-servers" } +sc-rpc = { version = "3.0.0", path = "../rpc" } +sc-block-builder = { version = "0.9.0", path = "../block-builder" } +sp-block-builder = { version = "3.0.0", path = "../../primitives/block-builder" } +sc-informant = { version = "0.9.0", path = "../informant" } +sc-telemetry = { version = "3.0.0", path = "../telemetry" } +sc-offchain = { version = "3.0.0", path = "../offchain" } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.9.0"} +sc-tracing = { version = "3.0.0", path = "../tracing" } +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } tracing = "0.1.22" tracing-futures = { version = "0.2.4" } -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } [target.'cfg(not(target_os = "unknown"))'.dependencies] tempfile = "3.1.0" @@ -86,8 +86,10 @@ directories = "3.0.1" [dev-dependencies] substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime/" } -sp-consensus-babe = { version = "0.8.0", path = "../../primitives/consensus/babe" } -grandpa = { version = "0.8.0", package = "sc-finality-grandpa", path = "../finality-grandpa" } -grandpa-primitives = { version = "2.0.0", package = "sp-finality-grandpa", path = "../../primitives/finality-grandpa" } -tokio = { version = "0.2", default-features = false } +sp-consensus-babe = { version = "0.9.0", path = "../../primitives/consensus/babe" } +grandpa = { version = "0.9.0", package = "sc-finality-grandpa", path = "../finality-grandpa" } +grandpa-primitives = { version = "3.0.0", package = "sp-finality-grandpa", path = "../../primitives/finality-grandpa" } +tokio = { version = "0.2.25", default-features = false } async-std = { version = "1.6.5", default-features = false } +tracing-subscriber = "0.2.15" +tracing-log = "0.1.1" diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 3dc716b4e1c9e..103e499a589db 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -43,6 +43,7 @@ use log::{info, warn}; use sc_network::config::{Role, OnDemand}; use sc_network::NetworkService; use sc_network::block_request_handler::{self, BlockRequestHandler}; +use sc_network::light_client_requests::{self, handler::LightClientRequestHandler}; use sp_runtime::generic::BlockId; use sp_runtime::traits::{ Block as BlockT, HashFor, Zero, BlockIdTo, @@ -184,7 +185,6 @@ type TFullParts = ( Arc>, KeystoreContainer, TaskManager, - Option, ); type TLightParts = ( @@ -193,7 +193,6 @@ type TLightParts = ( KeystoreContainer, TaskManager, Arc>, - Option, ); /// Light client backend type with a specific hash type. @@ -308,14 +307,9 @@ pub fn new_full_parts( { let keystore_container = KeystoreContainer::new(&config.keystore)?; - let telemetry_span = if config.telemetry_endpoints.is_some() { - Some(TelemetrySpan::new()) - } else { - None - }; let task_manager = { let registry = config.prometheus_config.as_ref().map(|cfg| &cfg.registry); - TaskManager::new(config.task_executor.clone(), registry, telemetry_span.clone())? + TaskManager::new(config.task_executor.clone(), registry)? }; let executor = NativeExecutor::::new( @@ -371,7 +365,6 @@ pub fn new_full_parts( backend, keystore_container, task_manager, - telemetry_span, )) } @@ -383,14 +376,9 @@ pub fn new_light_parts( TExecDisp: NativeExecutionDispatch + 'static, { let keystore_container = KeystoreContainer::new(&config.keystore)?; - let telemetry_span = if config.telemetry_endpoints.is_some() { - Some(TelemetrySpan::new()) - } else { - None - }; let task_manager = { let registry = config.prometheus_config.as_ref().map(|cfg| &cfg.registry); - TaskManager::new(config.task_executor.clone(), registry, telemetry_span.clone())? + TaskManager::new(config.task_executor.clone(), registry)? }; let executor = NativeExecutor::::new( @@ -429,7 +417,7 @@ pub fn new_light_parts( config.prometheus_config.as_ref().map(|config| config.registry.clone()), )?); - Ok((client, backend, keystore_container, task_manager, on_demand, telemetry_span)) + Ok((client, backend, keystore_container, task_manager, on_demand)) } /// Create an instance of db-backed client. @@ -481,8 +469,6 @@ pub fn new_client( pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> { /// The service configuration. pub config: Configuration, - /// Telemetry span, if any. - pub telemetry_span: Option, /// A shared client returned by `new_full_parts`/`new_light_parts`. pub client: Arc, /// A shared backend returned by `new_full_parts`/`new_light_parts`. @@ -506,6 +492,10 @@ pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> { pub network_status_sinks: NetworkStatusSinks, /// A Sender for RPC requests. pub system_rpc_tx: TracingUnboundedSender>, + /// Telemetry span. + /// + /// This span needs to be entered **before** calling [`spawn_tasks()`]. + pub telemetry_span: Option, } /// Build a shared offchain workers instance. @@ -557,14 +547,13 @@ pub fn spawn_tasks( TCl: ProvideRuntimeApi + HeaderMetadata + Chain + BlockBackend + BlockIdTo + ProofProvider + HeaderBackend + BlockchainEvents + ExecutorProvider + UsageProvider + - StorageProvider + CallApiAt + + StorageProvider + CallApiAt + Send + 'static, >::Api: sp_api::Metadata + sc_offchain::OffchainWorkerApi + sp_transaction_pool::runtime_api::TaggedTransactionQueue + sp_session::SessionKeys + - sp_api::ApiErrorExt + sp_api::ApiExt, TBl: BlockT, TBackend: 'static + sc_client_api::backend::Backend + Send, @@ -575,7 +564,6 @@ pub fn spawn_tasks( let SpawnTasksParams { mut config, task_manager, - telemetry_span, client, on_demand, backend, @@ -586,6 +574,7 @@ pub fn spawn_tasks( network, network_status_sinks, system_rpc_tx, + telemetry_span, } = params; let chain_info = client.usage_info().chain; @@ -594,15 +583,14 @@ pub fn spawn_tasks( client.clone(), &BlockId::Hash(chain_info.best_hash), config.dev_key_seed.clone().map(|s| vec![s]).unwrap_or_default(), - )?; + ).map_err(|e| Error::Application(Box::new(e)))?; - let telemetry_connection_notifier = telemetry_span - .and_then(|span| init_telemetry( - &mut config, - span, - network.clone(), - client.clone(), - )); + let telemetry_connection_notifier = init_telemetry( + &mut config, + telemetry_span, + network.clone(), + client.clone(), + ); info!("📦 Highest known block at #{}", chain_info.best_number); @@ -700,11 +688,12 @@ async fn transaction_notifications( fn init_telemetry>( config: &mut Configuration, - telemetry_span: TelemetrySpan, + telemetry_span: Option, network: Arc::Hash>>, client: Arc, ) -> Option { - let endpoints = config.telemetry_endpoints()?.clone(); + let telemetry_span = telemetry_span?; + let endpoints = config.telemetry_endpoints.clone()?; let genesis_hash = client.block_hash(Zero::zero()).ok().flatten().unwrap_or_default(); let connection_message = ConnectionMessage { name: config.network.node_name.to_owned(), @@ -747,14 +736,14 @@ fn gen_handler( TBl: BlockT, TCl: ProvideRuntimeApi + BlockchainEvents + HeaderBackend + HeaderMetadata + ExecutorProvider + - CallApiAt + ProofProvider + + CallApiAt + ProofProvider + StorageProvider + BlockBackend + Send + Sync + 'static, TExPool: MaintainedTransactionPool::Hash> + 'static, TBackend: sc_client_api::backend::Backend + 'static, TRpc: sc_rpc::RpcExtension, >::Api: sp_session::SessionKeys + - sp_api::Metadata, + sp_api::Metadata, { use sc_rpc::{chain, state, author, system, offchain}; @@ -888,11 +877,11 @@ pub fn build_network( let block_request_protocol_config = { if matches!(config.role, Role::Light) { // Allow outgoing requests but deny incoming requests. - block_request_handler::generate_protocol_config(protocol_id.clone()) + block_request_handler::generate_protocol_config(&protocol_id) } else { // Allow both outgoing and incoming requests. let (handler, protocol_config) = BlockRequestHandler::new( - protocol_id.clone(), + &protocol_id, client.clone(), ); spawn_handle.spawn("block_request_handler", handler.run()); @@ -900,6 +889,21 @@ pub fn build_network( } }; + let light_client_request_protocol_config = { + if matches!(config.role, Role::Light) { + // Allow outgoing requests but deny incoming requests. + light_client_requests::generate_protocol_config(&protocol_id) + } else { + // Allow both outgoing and incoming requests. + let (handler, protocol_config) = LightClientRequestHandler::new( + &protocol_id, + client.clone(), + ); + spawn_handle.spawn("light_client_request_handler", handler.run()); + protocol_config + } + }; + let network_params = sc_network::config::Params { role: config.role.clone(), executor: { @@ -908,6 +912,12 @@ pub fn build_network( spawn_handle.spawn("libp2p-node", fut); })) }, + transactions_handler_executor: { + let spawn_handle = Clone::clone(&spawn_handle); + Box::new(move |fut| { + spawn_handle.spawn("network-transactions-handler", fut); + }) + }, network_config: config.network.clone(), chain: client.clone(), on_demand: on_demand, @@ -917,6 +927,7 @@ pub fn build_network( block_announce_validator, metrics_registry: config.prometheus_config.as_ref().map(|config| config.registry.clone()), block_request_protocol_config, + light_client_request_protocol_config, }; let has_bootnodes = !network_params.network_config.boot_nodes.is_empty(); diff --git a/client/service/src/client/call_executor.rs b/client/service/src/client/call_executor.rs index cc196f67a37aa..8c7ca645b0ffe 100644 --- a/client/service/src/client/call_executor.rs +++ b/client/service/src/client/call_executor.rs @@ -161,7 +161,7 @@ where Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >( &self, initialize_block_fn: IB, @@ -226,7 +226,10 @@ where ); // TODO: https://github.com/paritytech/substrate/issues/4455 // .with_storage_transaction_cache(storage_transaction_cache.as_mut().map(|c| &mut **c)) - state_machine.execute_using_consensus_failure_handler(execution_manager, native_call) + state_machine.execute_using_consensus_failure_handler( + execution_manager, + native_call.map(|n| || (n)().map_err(|e| Box::new(e) as Box<_>)), + ) }, None => { let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state); @@ -245,7 +248,10 @@ where &runtime_code, self.spawn_handle.clone(), ).with_storage_transaction_cache(storage_transaction_cache.as_mut().map(|c| &mut **c)); - state_machine.execute_using_consensus_failure_handler(execution_manager, native_call) + state_machine.execute_using_consensus_failure_handler( + execution_manager, + native_call.map(|n| || (n)().map_err(|e| Box::new(e) as Box<_>)), + ) } }.map_err(Into::into) } diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index f337452e9dc84..b1ff0678ee9a4 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -604,7 +604,7 @@ impl Client where new_cache: HashMap>, ) -> sp_blockchain::Result where Self: ProvideRuntimeApi, - >::Api: CoreApi + + >::Api: CoreApi + ApiExt, { let BlockImportParams { @@ -696,7 +696,7 @@ impl Client where import_existing: bool, ) -> sp_blockchain::Result where Self: ProvideRuntimeApi, - >::Api: CoreApi + + >::Api: CoreApi + ApiExt, { let parent_hash = import_headers.post().parent_hash().clone(); @@ -838,7 +838,7 @@ impl Client where ) -> sp_blockchain::Result> where Self: ProvideRuntimeApi, - >::Api: CoreApi + + >::Api: CoreApi + ApiExt, { let parent_hash = import_block.header.parent_hash(); @@ -1272,7 +1272,7 @@ impl BlockBuilderProvider for Client + ProvideRuntimeApi, >::Api: ApiExt> - + BlockBuilderApi, + + BlockBuilderApi, { fn new_block_at>( &self, @@ -1628,18 +1628,17 @@ impl CallApiAt for Client where E: CallExecutor + Send + Sync, Block: BlockT, { - type Error = Error; type StateBackend = B::State; fn call_api_at< 'a, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, - C: CoreApi, + NC: FnOnce() -> result::Result + UnwindSafe, + C: CoreApi, >( &self, params: CallApiAtParams<'a, Block, C, NC, B::State>, - ) -> sp_blockchain::Result> { + ) -> Result, sp_api::ApiError> { let core_api = params.core_api; let at = params.at; @@ -1649,7 +1648,9 @@ impl CallApiAt for Client where ); self.executor.contextual_call::<_, fn(_,_) -> _,_,_>( - || core_api.initialize_block(at, &self.prepare_environment_block(at)?), + || core_api + .initialize_block(at, &self.prepare_environment_block(at)?) + .map_err(Error::RuntimeApiError), at, params.function, ¶ms.arguments, @@ -1660,11 +1661,14 @@ impl CallApiAt for Client where params.native_call, params.recorder, Some(extensions), - ) + ).map_err(Into::into) } - fn runtime_version_at(&self, at: &BlockId) -> sp_blockchain::Result { - self.runtime_version_at(at) + fn runtime_version_at( + &self, + at: &BlockId, + ) -> Result { + self.runtime_version_at(at).map_err(Into::into) } } @@ -1676,7 +1680,7 @@ impl sp_consensus::BlockImport for &Client + Send + Sync, Block: BlockT, Client: ProvideRuntimeApi, - as ProvideRuntimeApi>::Api: CoreApi + + as ProvideRuntimeApi>::Api: CoreApi + ApiExt, { type Error = ConsensusError; @@ -1776,7 +1780,7 @@ impl sp_consensus::BlockImport for Client + Send + Sync, Block: BlockT, Self: ProvideRuntimeApi, - >::Api: CoreApi + + >::Api: CoreApi + ApiExt, { type Error = ConsensusError; @@ -1919,6 +1923,14 @@ impl BlockBackend for Client fn block_hash(&self, number: NumberFor) -> sp_blockchain::Result> { self.backend.blockchain().hash(number) } + + fn extrinsic(&self, hash: &Block::Hash) -> sp_blockchain::Result> { + self.backend.blockchain().extrinsic(hash) + } + + fn have_extrinsic(&self, hash: &Block::Hash) -> sp_blockchain::Result { + self.backend.blockchain().have_extrinsic(hash) + } } impl backend::AuxStore for Client @@ -1927,7 +1939,7 @@ impl backend::AuxStore for Client E: CallExecutor, Block: BlockT, Self: ProvideRuntimeApi, - >::Api: CoreApi, + >::Api: CoreApi, { /// Insert auxiliary data into key-value store. fn insert_aux< @@ -1957,7 +1969,7 @@ impl backend::AuxStore for &Client E: CallExecutor, Block: BlockT, Client: ProvideRuntimeApi, - as ProvideRuntimeApi>::Api: CoreApi, + as ProvideRuntimeApi>::Api: CoreApi, { fn insert_aux< 'a, diff --git a/client/service/src/config.rs b/client/service/src/config.rs index 74d15cb3fb922..4f0d426bdba42 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -207,19 +207,6 @@ impl Configuration { self.prometheus_config.as_ref().map(|config| &config.registry) } - /// Returns the telemetry endpoints if any and if the telemetry handle exists. - pub(crate) fn telemetry_endpoints(&self) -> Option<&TelemetryEndpoints> { - if self.telemetry_handle.is_none() { - return None; - } - - match self.telemetry_endpoints.as_ref() { - // Don't initialise telemetry if `telemetry_endpoints` == Some([]) - Some(endpoints) if !endpoints.is_empty() => Some(endpoints), - _ => None, - } - } - /// Returns the network protocol id from the chain spec, or the default. pub fn protocol_id(&self) -> sc_network::config::ProtocolId { let protocol_id_full = match self.chain_spec.protocol_id() { diff --git a/client/service/src/error.rs b/client/service/src/error.rs index 31c3cea4ef43b..caa54700da916 100644 --- a/client/service/src/error.rs +++ b/client/service/src/error.rs @@ -33,13 +33,13 @@ pub type Result = std::result::Result; pub enum Error { #[error(transparent)] Client(#[from] sp_blockchain::Error), - + #[error(transparent)] Io(#[from] std::io::Error), - + #[error(transparent)] Consensus(#[from] sp_consensus::Error), - + #[error(transparent)] Network(#[from] sc_network::error::Error), diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index 4880b8cffdafe..39bad8f2f36ef 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -309,7 +309,6 @@ async fn build_network_future< Role::Authority { .. } => NodeRole::Authority, Role::Light => NodeRole::LightClient, Role::Full => NodeRole::Full, - Role::Sentry { .. } => NodeRole::Sentry, }; let _ = sender.send(vec![node_role]); diff --git a/client/service/src/metrics.rs b/client/service/src/metrics.rs index 446cce952741d..4fbfa4d77f08f 100644 --- a/client/service/src/metrics.rs +++ b/client/service/src/metrics.rs @@ -135,7 +135,6 @@ impl MetricsService { let role_bits = match config.role { Role::Full => 1u64, Role::Light => 2u64, - Role::Sentry { .. } => 3u64, Role::Authority { .. } => 4u64, }; diff --git a/client/service/src/task_manager/mod.rs b/client/service/src/task_manager/mod.rs index 4d9e16d900327..c7254f1f894de 100644 --- a/client/service/src/task_manager/mod.rs +++ b/client/service/src/task_manager/mod.rs @@ -34,7 +34,6 @@ use prometheus_endpoint::{ use sp_utils::mpsc::{TracingUnboundedSender, TracingUnboundedReceiver, tracing_unbounded}; use tracing_futures::Instrument; use crate::{config::{TaskExecutor, TaskType, JoinFuture}, Error}; -use sc_telemetry::TelemetrySpan; mod prometheus_future; #[cfg(test)] @@ -47,7 +46,6 @@ pub struct SpawnTaskHandle { executor: TaskExecutor, metrics: Option, task_notifier: TracingUnboundedSender, - telemetry_span: Option, } impl SpawnTaskHandle { @@ -91,10 +89,7 @@ impl SpawnTaskHandle { metrics.tasks_ended.with_label_values(&[name, "finished"]).inc_by(0); } - let telemetry_span = self.telemetry_span.clone(); let future = async move { - let _telemetry_entered = telemetry_span.as_ref().map(|x| x.enter()); - if let Some(metrics) = metrics { // Add some wrappers around `task`. let task = { @@ -127,7 +122,8 @@ impl SpawnTaskHandle { } }; - let join_handle = self.executor.spawn(Box::pin(future.in_current_span()), task_type); + let join_handle = self.executor.spawn(future.in_current_span().boxed(), task_type); + let mut task_notifier = self.task_notifier.clone(); self.executor.spawn( Box::pin(async move { @@ -154,6 +150,7 @@ impl sp_core::traits::SpawnNamed for SpawnTaskHandle { /// task spawned through it fails. The service should be on the receiver side /// and will shut itself down whenever it receives any message, i.e. an /// essential task has failed. +#[derive(Clone)] pub struct SpawnEssentialTaskHandle { essential_failed_tx: TracingUnboundedSender<()>, inner: SpawnTaskHandle, @@ -207,6 +204,16 @@ impl SpawnEssentialTaskHandle { } } +impl sp_core::traits::SpawnEssentialNamed for SpawnEssentialTaskHandle { + fn spawn_essential_blocking(&self, name: &'static str, future: BoxFuture<'static, ()>) { + self.spawn_blocking(name, future); + } + + fn spawn_essential(&self, name: &'static str, future: BoxFuture<'static, ()>) { + self.spawn(name, future); + } +} + /// Helper struct to manage background/async tasks in Service. pub struct TaskManager { /// A future that resolves when the service has exited, this is useful to @@ -233,17 +240,14 @@ pub struct TaskManager { /// terminates and gracefully shutdown. Also ends the parent `future()` if a child's essential /// task fails. children: Vec, - /// A telemetry handle used to enter the telemetry span when a task is spawned. - telemetry_span: Option, } impl TaskManager { /// If a Prometheus registry is passed, it will be used to report statistics about the /// service tasks. - pub(super) fn new( + pub fn new( executor: TaskExecutor, prometheus_registry: Option<&Registry>, - telemetry_span: Option, ) -> Result { let (signal, on_exit) = exit_future::signal(); @@ -272,7 +276,6 @@ impl TaskManager { task_notifier, completion_future, children: Vec::new(), - telemetry_span, }) } @@ -283,7 +286,6 @@ impl TaskManager { executor: self.executor.clone(), metrics: self.metrics.clone(), task_notifier: self.task_notifier.clone(), - telemetry_span: self.telemetry_span.clone(), } } diff --git a/client/service/src/task_manager/tests.rs b/client/service/src/task_manager/tests.rs index f0ede1fc389a5..762348ba9fa5d 100644 --- a/client/service/src/task_manager/tests.rs +++ b/client/service/src/task_manager/tests.rs @@ -20,9 +20,14 @@ use crate::config::TaskExecutor; use crate::task_manager::TaskManager; use futures::{future::FutureExt, pin_mut, select}; use parking_lot::Mutex; -use std::any::Any; -use std::sync::Arc; -use std::time::Duration; +use sc_telemetry::TelemetrySpan; +use std::{any::Any, env, sync::Arc, time::Duration}; +use tracing::{event::Event, span::Id, subscriber::Subscriber}; +use tracing_subscriber::{ + layer::{Context, SubscriberExt}, + registry::LookupSpan, + Layer, +}; #[derive(Clone, Debug)] struct DropTester(Arc>); @@ -82,7 +87,7 @@ async fn run_background_task_blocking(duration: Duration, _keep_alive: impl Any) } fn new_task_manager(task_executor: TaskExecutor) -> TaskManager { - TaskManager::new(task_executor, None, None).unwrap() + TaskManager::new(task_executor, None).unwrap() } #[test] @@ -312,3 +317,94 @@ fn ensure_task_manager_future_continues_when_childs_not_essential_task_fails() { runtime.block_on(task_manager.clean_shutdown()); assert_eq!(drop_tester, 0); } + +struct TestLayer { + spans_found: Arc>>>, +} + +impl Layer for TestLayer +where + S: Subscriber + for<'a> LookupSpan<'a>, +{ + fn on_event(&self, _: &Event<'_>, ctx: Context) { + let mut spans_found = self.spans_found.lock(); + + if spans_found.is_some() { + panic!("on_event called multiple times"); + } + + *spans_found = Some(ctx.scope().map(|x| x.id()).collect()); + } +} + +fn setup_subscriber() -> ( + impl Subscriber + for<'a> LookupSpan<'a>, + Arc>>>, +) { + let spans_found = Arc::new(Mutex::new(Default::default())); + let layer = TestLayer { + spans_found: spans_found.clone(), + }; + let subscriber = tracing_subscriber::fmt().finish().with(layer); + (subscriber, spans_found) +} + +/// This is not an actual test, it is used by the `telemetry_span_is_forwarded_to_task` test. +/// The given test will call the test executable and only execute this one test that +/// test that the telemetry span and the prefix span are forwarded correctly. This needs to be done +/// in a separate process to avoid interfering with the other tests. +#[test] +fn subprocess_telemetry_span_is_forwarded_to_task() { + if env::var("SUBPROCESS_TEST").is_err() { + return; + } + + let (subscriber, spans_found) = setup_subscriber(); + tracing_log::LogTracer::init().unwrap(); + let _sub_guard = tracing::subscriber::set_global_default(subscriber); + + let mut runtime = tokio::runtime::Runtime::new().unwrap(); + + let prefix_span = tracing::info_span!("prefix"); + let _enter_prefix_span = prefix_span.enter(); + + let telemetry_span = TelemetrySpan::new(); + let _enter_telemetry_span = telemetry_span.enter(); + + let handle = runtime.handle().clone(); + let task_executor = TaskExecutor::from(move |fut, _| handle.spawn(fut).map(|_| ())); + let task_manager = new_task_manager(task_executor); + + let (sender, receiver) = futures::channel::oneshot::channel(); + + task_manager.spawn_handle().spawn( + "log-something", + async move { + log::info!("boo!"); + sender.send(()).unwrap(); + } + .boxed(), + ); + + runtime.block_on(receiver).unwrap(); + runtime.block_on(task_manager.clean_shutdown()); + + let spans = spans_found.lock().take().unwrap(); + assert_eq!(2, spans.len()); + + assert_eq!(spans[0], prefix_span.id().unwrap()); + assert_eq!(spans[1], telemetry_span.span().id().unwrap()); +} + +#[test] +fn telemetry_span_is_forwarded_to_task() { + let executable = env::current_exe().unwrap(); + let output = std::process::Command::new(executable) + .env("SUBPROCESS_TEST", "1") + .args(&["--nocapture", "subprocess_telemetry_span_is_forwarded_to_task"]) + .output() + .unwrap(); + println!("{}", String::from_utf8(output.stdout).unwrap()); + eprintln!("{}", String::from_utf8(output.stderr).unwrap()); + assert!(output.status.success()); +} diff --git a/client/service/test/Cargo.toml b/client/service/test/Cargo.toml index 8fcd09b8298dd..e55320d6c5fb7 100644 --- a/client/service/test/Cargo.toml +++ b/client/service/test/Cargo.toml @@ -19,26 +19,26 @@ futures01 = { package = "futures", version = "0.1.29" } log = "0.4.8" fdlimit = "0.2.1" parking_lot = "0.11.1" -sc-light = { version = "2.0.0", path = "../../light" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-api = { version = "2.0.0", path = "../../../primitives/api" } -sp-state-machine = { version = "0.8.0", path = "../../../primitives/state-machine" } -sp-externalities = { version = "0.8.0", path = "../../../primitives/externalities" } -sp-trie = { version = "2.0.0", path = "../../../primitives/trie" } -sp-storage = { version = "2.0.0", path = "../../../primitives/storage" } -sc-client-db = { version = "0.8.0", default-features = false, path = "../../db" } +sc-light = { version = "3.0.0", path = "../../light" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } +sp-state-machine = { version = "0.9.0", path = "../../../primitives/state-machine" } +sp-externalities = { version = "0.9.0", path = "../../../primitives/externalities" } +sp-trie = { version = "3.0.0", path = "../../../primitives/trie" } +sp-storage = { version = "3.0.0", path = "../../../primitives/storage" } +sc-client-db = { version = "0.9.0", default-features = false, path = "../../db" } futures = { version = "0.3.1", features = ["compat"] } -sc-service = { version = "0.8.0", default-features = false, features = ["test-helpers"], path = "../../service" } -sc-network = { version = "0.8.0", path = "../../network" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-transaction-pool = { version = "2.0.0", path = "../../../primitives/transaction-pool" } +sc-service = { version = "0.9.0", default-features = false, features = ["test-helpers"], path = "../../service" } +sc-network = { version = "0.9.0", path = "../../network" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-transaction-pool = { version = "3.0.0", path = "../../../primitives/transaction-pool" } substrate-test-runtime = { version = "2.0.0", path = "../../../test-utils/runtime" } substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" } -sc-client-api = { version = "2.0.0", path = "../../api" } -sc-block-builder = { version = "0.8.0", path = "../../block-builder" } -sc-executor = { version = "0.8.0", path = "../../executor" } -sp-panic-handler = { version = "2.0.0", path = "../../../primitives/panic-handler" } -parity-scale-codec = "1.3.6" -sp-tracing = { version = "2.0.0", path = "../../../primitives/tracing" } +sc-client-api = { version = "3.0.0", path = "../../api" } +sc-block-builder = { version = "0.9.0", path = "../../block-builder" } +sc-executor = { version = "0.9.0", path = "../../executor" } +sp-panic-handler = { version = "3.0.0", path = "../../../primitives/panic-handler" } +parity-scale-codec = "2.0.0" +sp-tracing = { version = "3.0.0", path = "../../../primitives/tracing" } diff --git a/client/service/test/src/client/light.rs b/client/service/test/src/client/light.rs index b6287741fdf34..3b20f163871f1 100644 --- a/client/service/test/src/client/light.rs +++ b/client/service/test/src/client/light.rs @@ -215,7 +215,7 @@ impl CallExecutor for DummyCallExecutor { Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> Result + UnwindSafe, + NC: FnOnce() -> Result + UnwindSafe, >( &self, _initialize_block_fn: IB, diff --git a/client/service/test/src/client/mod.rs b/client/service/test/src/client/mod.rs index 7498289c7be1a..66b6aae12c2f9 100644 --- a/client/service/test/src/client/mod.rs +++ b/client/service/test/src/client/mod.rs @@ -1327,7 +1327,9 @@ fn doesnt_import_blocks_that_revert_finality() { let import_err = client.import(BlockOrigin::Own, b3).err().unwrap(); let expected_err = ConsensusError::ClientImport( - sp_blockchain::Error::NotInFinalizedChain.to_string() + sp_blockchain::Error::RuntimeApiError( + sp_api::ApiError::Application(Box::new(sp_blockchain::Error::NotInFinalizedChain)) + ).to_string() ); assert_eq!( diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index f1d5c6a86b06e..6c99f83d4c517 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -336,7 +336,7 @@ impl TestNet where let node_config = node_config( self.nodes, &self.chain_spec, - Role::Authority { sentry_nodes: Vec::new() }, + Role::Authority, task_executor.clone(), Some(key), self.base_port, diff --git a/client/state-db/Cargo.toml b/client/state-db/Cargo.toml index 26939b769b8a7..d61dd7fc125a1 100644 --- a/client/state-db/Cargo.toml +++ b/client/state-db/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-state-db" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -16,8 +16,8 @@ targets = ["x86_64-unknown-linux-gnu"] thiserror = "1.0.21" parking_lot = "0.11.1" log = "0.4.11" -sc-client-api = { version = "2.0.0", path = "../api" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } +sc-client-api = { version = "3.0.0", path = "../api" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } parity-util-mem-derive = "0.1.0" diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 8fd02ee17b996..1f73f3cca35e9 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -24,12 +24,12 @@ //! Canonicalization window tracks a tree of blocks identified by header hash. The in-memory //! overlay allows to get any node that was inserted in any of the blocks within the window. //! The tree is journaled to the backing database and rebuilt on startup. -//! Canonicalization function selects one root from the top of the tree and discards all other roots and -//! their subtrees. +//! Canonicalization function selects one root from the top of the tree and discards all other roots +//! and their subtrees. //! //! # Pruning. -//! See `RefWindow` for pruning algorithm details. `StateDb` prunes on each canonicalization until pruning -//! constraints are satisfied. +//! See `RefWindow` for pruning algorithm details. `StateDb` prunes on each canonicalization until +//! pruning constraints are satisfied. mod noncanonical; mod pruning; @@ -107,7 +107,7 @@ impl fmt::Debug for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Error::Db(e) => e.fmt(f), - Error::Decoding(e) => write!(f, "Error decoding sliceable value: {}", e.what()), + Error::Decoding(e) => write!(f, "Error decoding sliceable value: {}", e), Error::InvalidBlock => write!(f, "Trying to canonicalize invalid block"), Error::InvalidBlockNumber => write!(f, "Trying to insert block with invalid number"), Error::InvalidParent => write!(f, "Trying to insert block with unknown parent"), diff --git a/client/sync-state-rpc/Cargo.toml b/client/sync-state-rpc/Cargo.toml index 81204365d0821..3ec48ac9ec570 100644 --- a/client/sync-state-rpc/Cargo.toml +++ b/client/sync-state-rpc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-sync-state-rpc" -version = "0.8.0" +version = "0.9.0" authors = ["Parity Technologies "] description = "A RPC handler to create sync states for light clients." edition = "2018" @@ -17,12 +17,12 @@ thiserror = "1.0.21" jsonrpc-core = "15.0" jsonrpc-core-client = "15.0" jsonrpc-derive = "15.0" -sc-chain-spec = { version = "2.0.0", path = "../chain-spec" } -sc-client-api = { version = "2.0.0", path = "../api" } -sc-consensus-babe = { version = "0.8.0", path = "../consensus/babe" } -sc-consensus-epochs = { version = "0.8.0", path = "../consensus/epochs" } -sc-finality-grandpa = { version = "0.8.0", path = "../finality-grandpa" } -sc-rpc-api = { version = "0.8.0", path = "../rpc-api" } +sc-chain-spec = { version = "3.0.0", path = "../chain-spec" } +sc-client-api = { version = "3.0.0", path = "../api" } +sc-consensus-babe = { version = "0.9.0", path = "../consensus/babe" } +sc-consensus-epochs = { version = "0.9.0", path = "../consensus/epochs" } +sc-finality-grandpa = { version = "0.9.0", path = "../finality-grandpa" } +sc-rpc-api = { version = "0.9.0", path = "../rpc-api" } serde_json = "1.0.58" -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } diff --git a/client/telemetry/Cargo.toml b/client/telemetry/Cargo.toml index f6e249c786a9c..0d29fbca6f9b9 100644 --- a/client/telemetry/Cargo.toml +++ b/client/telemetry/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-telemetry" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] description = "Telemetry utils" edition = "2018" @@ -18,9 +18,9 @@ targets = ["x86_64-unknown-linux-gnu"] parking_lot = "0.11.1" futures = "0.3.9" wasm-timer = "0.2.5" -libp2p = { version = "0.34.0", default-features = false, features = ["dns", "tcp-async-io", "wasm-ext", "websocket"] } +libp2p = { version = "0.35.1", default-features = false, features = ["dns", "tcp-async-io", "wasm-ext", "websocket"] } log = "0.4.8" -pin-project = "0.4.6" +pin-project = "1.0.4" rand = "0.7.2" serde = { version = "1.0.101", features = ["derive"] } take_mut = "0.2.2" @@ -28,5 +28,5 @@ void = "1.0.2" tracing = "0.1.10" tracing-subscriber = "0.2.13" serde_json = "1.0.41" -sp-utils = { version = "2.0.0", path = "../../primitives/utils" } +sp-utils = { version = "3.0.0", path = "../../primitives/utils" } chrono = "0.4.19" diff --git a/client/telemetry/README.md b/client/telemetry/README.md index a6b7b654508ae..2e3e19bd2f628 100644 --- a/client/telemetry/README.md +++ b/client/telemetry/README.md @@ -1,21 +1,21 @@ # sc-telemetry -Substrate's client telemetry is a part of substrate that allows logging telemetry information -with a [Polkadot telemetry](https://github.com/paritytech/substrate-telemetry). +Substrate's client telemetry is a part of substrate that allows ingesting telemetry data +with for example [Polkadot telemetry](https://github.com/paritytech/substrate-telemetry). -It works using Tokio's [tracing](https://github.com/tokio-rs/tracing/). The telemetry -information uses tracing's logging to report the telemetry which is then retrieved by a -tracing's `Layer`. This layer will then send the data through an asynchronous channel and to a -background task called [`TelemetryWorker`] which will send the information to the telemetry -server. +It works using Tokio's [tracing](https://github.com/tokio-rs/tracing/) library. The telemetry +information uses tracing's logging to report the telemetry data which is then retrieved by a +tracing `Layer`. This layer will then send the data through an asynchronous channel to a +background task called [`TelemetryWorker`] which will send the information to the configured +remote telemetry servers. -If multiple substrate nodes are running, it uses a tracing's `Span` to identify which substrate -node is reporting the telemetry. Every task spawned using sc-service's `TaskManager` -automatically inherit this span. +If multiple substrate nodes are running in the same process, it uses a `tracing::Span` to +identify which substrate node is reporting the telemetry. Every task spawned using sc-service's +`TaskManager` automatically inherit this span. -Substrate's nodes initialize/register to the [`TelemetryWorker`] using a [`TelemetryHandle`]. +Substrate's nodes initialize/register with the [`TelemetryWorker`] using a [`TelemetryHandle`]. This handle can be cloned and passed around. It uses an asynchronous channel to communicate with -the running [`TelemetryWorker`] dedicated to registration. Registering a telemetry can happen at -any point in time during the execution. +the running [`TelemetryWorker`] dedicated to registration. Registering can happen at any point +in time during the process execution. License: GPL-3.0-or-later WITH Classpath-exception-2.0 diff --git a/client/telemetry/src/endpoints.rs b/client/telemetry/src/endpoints.rs index 7d0338fb18e3c..fe4fa23974a64 100644 --- a/client/telemetry/src/endpoints.rs +++ b/client/telemetry/src/endpoints.rs @@ -25,7 +25,8 @@ use serde::{Deserialize, Deserializer, Serialize}; /// The URL string can be either a URL or a multiaddress. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] pub struct TelemetryEndpoints( - #[serde(deserialize_with = "url_or_multiaddr_deser")] pub(crate) Vec<(Multiaddr, u8)>, + #[serde(deserialize_with = "url_or_multiaddr_deser")] + pub(crate) Vec<(Multiaddr, u8)>, ); /// Custom deserializer for TelemetryEndpoints, used to convert urls or multiaddr to multiaddr. diff --git a/client/telemetry/src/layer.rs b/client/telemetry/src/layer.rs index eb5eee197770b..0ce3f97620da9 100644 --- a/client/telemetry/src/layer.rs +++ b/client/telemetry/src/layer.rs @@ -35,7 +35,7 @@ pub struct TelemetryLayer(Mutex>); impl TelemetryLayer { /// Create a new [`TelemetryLayer`] and [`TelemetryWorker`]. /// - /// If not provided, the `buffer_size` will be 16 by default. + /// The `buffer_size` defaults to 16. /// /// The [`ExtTransport`] is used in WASM contexts where we need some binding between the /// networking provided by the operating system or environment and libp2p. diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 6a4533bb7bc40..b398ee86de4ec 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -16,23 +16,23 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Substrate's client telemetry is a part of substrate that allows logging telemetry information -//! with a [Polkadot telemetry](https://github.com/paritytech/substrate-telemetry). +//! Substrate's client telemetry is a part of substrate that allows ingesting telemetry data +//! with for example [Polkadot telemetry](https://github.com/paritytech/substrate-telemetry). //! -//! It works using Tokio's [tracing](https://github.com/tokio-rs/tracing/). The telemetry -//! information uses tracing's logging to report the telemetry which is then retrieved by a -//! tracing's `Layer`. This layer will then send the data through an asynchronous channel and to a -//! background task called [`TelemetryWorker`] which will send the information to the telemetry -//! server. +//! It works using Tokio's [tracing](https://github.com/tokio-rs/tracing/) library. The telemetry +//! information uses tracing's logging to report the telemetry data which is then retrieved by a +//! tracing `Layer`. This layer will then send the data through an asynchronous channel to a +//! background task called [`TelemetryWorker`] which will send the information to the configured +//! remote telemetry servers. //! -//! If multiple substrate nodes are running, it uses a tracing's `Span` to identify which substrate -//! node is reporting the telemetry. Every task spawned using sc-service's `TaskManager` -//! automatically inherit this span. +//! If multiple substrate nodes are running in the same process, it uses a `tracing::Span` to +//! identify which substrate node is reporting the telemetry. Every task spawned using sc-service's +//! `TaskManager` automatically inherit this span. //! -//! Substrate's nodes initialize/register to the [`TelemetryWorker`] using a [`TelemetryHandle`]. +//! Substrate's nodes initialize/register with the [`TelemetryWorker`] using a [`TelemetryHandle`]. //! This handle can be cloned and passed around. It uses an asynchronous channel to communicate with -//! the running [`TelemetryWorker`] dedicated to registration. Registering a telemetry can happen at -//! any point in time during the execution. +//! the running [`TelemetryWorker`] dedicated to registration. Registering can happen at any point +//! in time during the process execution. #![warn(missing_docs)] @@ -86,7 +86,12 @@ impl TelemetrySpan { /// Constructs a new [`TelemetrySpan`]. pub fn new() -> Self { - Self(tracing::info_span!(TELEMETRY_LOG_SPAN)) + Self(tracing::error_span!(TELEMETRY_LOG_SPAN)) + } + + /// Return a clone of the underlying `tracing::Span` instance. + pub fn span(&self) -> tracing::Span { + self.0.clone() } } @@ -115,7 +120,7 @@ pub struct ConnectionMessage { /// Telemetry worker. /// -/// It should be ran as a background task using the [`TelemetryWorker::run`] method. This method +/// It should run as a background task using the [`TelemetryWorker::run`] method. This method /// will consume the object and any further attempts of initializing a new telemetry through its /// handle will fail (without being fatal). #[derive(Debug)] @@ -143,7 +148,7 @@ impl TelemetryWorker { /// Get a new [`TelemetryHandle`]. /// - /// This is used when you want to register a new telemetry for a Substrate node. + /// This is used when you want to register with the [`TelemetryWorker`]. pub fn handle(&self) -> TelemetryHandle { TelemetryHandle { message_sender: self.register_sender.clone(), @@ -225,6 +230,11 @@ impl TelemetryWorker { }; for (addr, verbosity) in endpoints { + log::trace!( + target: "telemetry", + "Initializing telemetry for: {:?}", + addr, + ); node_map .entry(id.clone()) .or_default() diff --git a/client/tracing/Cargo.toml b/client/tracing/Cargo.toml index 6a49c92b0f873..34aa9d9d4e7f0 100644 --- a/client/tracing/Cargo.toml +++ b/client/tracing/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-tracing" -version = "2.0.1" +version = "3.0.0" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" authors = ["Parity Technologies "] edition = "2018" @@ -29,9 +29,9 @@ tracing = "0.1.22" tracing-core = "0.1.17" tracing-log = "0.1.1" tracing-subscriber = "0.2.15" -sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } -sc-telemetry = { version = "2.0.0", path = "../telemetry" } -sc-tracing-proc-macro = { version = "2.0.0", path = "./proc-macro" } +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } +sc-telemetry = { version = "3.0.0", path = "../telemetry" } +sc-tracing-proc-macro = { version = "3.0.0", path = "./proc-macro" } [target.'cfg(target_os = "unknown")'.dependencies] wasm-bindgen = "0.2.67" diff --git a/client/tracing/proc-macro/Cargo.toml b/client/tracing/proc-macro/Cargo.toml index e2f4cf14435ba..ac06dc45a9c40 100644 --- a/client/tracing/proc-macro/Cargo.toml +++ b/client/tracing/proc-macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-tracing-proc-macro" -version = "2.0.0" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/client/tracing/src/lib.rs b/client/tracing/src/lib.rs index ebec8f2a8716a..2b0044a6f25b0 100644 --- a/client/tracing/src/lib.rs +++ b/client/tracing/src/lib.rs @@ -440,12 +440,11 @@ mod tests { } } - type TestSubscriber = tracing_subscriber::layer::Layered< - ProfilingLayer, - tracing_subscriber::fmt::Subscriber - >; - - fn setup_subscriber() -> (TestSubscriber, Arc>>, Arc>>) { + fn setup_subscriber() -> ( + impl tracing::Subscriber + Send + Sync, + Arc>>, + Arc>> + ) { let spans = Arc::new(Mutex::new(Vec::new())); let events = Arc::new(Mutex::new(Vec::new())); let handler = TestTraceHandler { @@ -456,7 +455,7 @@ mod tests { Box::new(handler), "test_target", ); - let subscriber = tracing_subscriber::fmt().finish().with(layer); + let subscriber = tracing_subscriber::fmt().with_writer(std::io::sink).finish().with(layer); (subscriber, spans, events) } @@ -560,64 +559,76 @@ mod tests { #[test] fn test_parent_id_with_threads() { - use std::sync::mpsc; - use std::thread; - - let (sub, spans, events) = setup_subscriber(); - let _sub_guard = tracing::subscriber::set_global_default(sub); - let span1 = tracing::info_span!(target: "test_target", "test_span1"); - let _guard1 = span1.enter(); - - let (tx, rx) = mpsc::channel(); - let handle = thread::spawn(move || { - let span2 = tracing::info_span!(target: "test_target", "test_span2"); - let _guard2 = span2.enter(); - // emit event - tracing::event!(target: "test_target", tracing::Level::INFO, "test_event1"); - for msg in rx.recv() { - if msg == false { - break; + use std::{sync::mpsc, thread}; + + if std::env::var("RUN_TEST_PARENT_ID_WITH_THREADS").is_err() { + let executable = std::env::current_exe().unwrap(); + let mut command = std::process::Command::new(executable); + + let res = command + .env("RUN_TEST_PARENT_ID_WITH_THREADS", "1") + .args(&["--nocapture", "test_parent_id_with_threads"]) + .output() + .unwrap() + .status; + assert!(res.success()); + } else { + let (sub, spans, events) = setup_subscriber(); + let _sub_guard = tracing::subscriber::set_global_default(sub); + let span1 = tracing::info_span!(target: "test_target", "test_span1"); + let _guard1 = span1.enter(); + + let (tx, rx) = mpsc::channel(); + let handle = thread::spawn(move || { + let span2 = tracing::info_span!(target: "test_target", "test_span2"); + let _guard2 = span2.enter(); + // emit event + tracing::event!(target: "test_target", tracing::Level::INFO, "test_event1"); + for msg in rx.recv() { + if msg == false { + break; + } } - } - // gard2 and span2 dropped / exited - }); + // gard2 and span2 dropped / exited + }); - // wait for Event to be dispatched and stored - while events.lock().is_empty() { - thread::sleep(Duration::from_millis(1)); - } + // wait for Event to be dispatched and stored + while events.lock().is_empty() { + thread::sleep(Duration::from_millis(1)); + } - // emit new event (will be second item in Vec) while span2 still active in other thread - tracing::event!(target: "test_target", tracing::Level::INFO, "test_event2"); + // emit new event (will be second item in Vec) while span2 still active in other thread + tracing::event!(target: "test_target", tracing::Level::INFO, "test_event2"); - // stop thread and drop span - let _ = tx.send(false); - let _ = handle.join(); + // stop thread and drop span + let _ = tx.send(false); + let _ = handle.join(); - // wait for Span to be dispatched and stored - while spans.lock().is_empty() { - thread::sleep(Duration::from_millis(1)); + // wait for Span to be dispatched and stored + while spans.lock().is_empty() { + thread::sleep(Duration::from_millis(1)); + } + let span2 = spans.lock().remove(0); + let event1 = events.lock().remove(0); + drop(_guard1); + drop(span1); + + // emit event with no parent + tracing::event!(target: "test_target", tracing::Level::INFO, "test_event3"); + + let span1 = spans.lock().remove(0); + let event2 = events.lock().remove(0); + + assert_eq!(event1.values.string_values.get("message").unwrap(), "test_event1"); + assert_eq!(event2.values.string_values.get("message").unwrap(), "test_event2"); + assert!(span1.parent_id.is_none()); + assert!(span2.parent_id.is_none()); + assert_eq!(span2.id, event1.parent_id.unwrap()); + assert_eq!(span1.id, event2.parent_id.unwrap()); + assert_ne!(span2.id, span1.id); + + let event3 = events.lock().remove(0); + assert!(event3.parent_id.is_none()); } - let span2 = spans.lock().remove(0); - let event1 = events.lock().remove(0); - drop(_guard1); - drop(span1); - - // emit event with no parent - tracing::event!(target: "test_target", tracing::Level::INFO, "test_event3"); - - let span1 = spans.lock().remove(0); - let event2 = events.lock().remove(0); - - assert_eq!(event1.values.string_values.get("message").unwrap(), "test_event1"); - assert_eq!(event2.values.string_values.get("message").unwrap(), "test_event2"); - assert!(span1.parent_id.is_none()); - assert!(span2.parent_id.is_none()); - assert_eq!(span2.id, event1.parent_id.unwrap()); - assert_eq!(span1.id, event2.parent_id.unwrap()); - assert_ne!(span2.id, span1.id); - - let event3 = events.lock().remove(0); - assert!(event3.parent_id.is_none()); } } diff --git a/client/tracing/src/logging/directives.rs b/client/tracing/src/logging/directives.rs index b108566bf2bce..39dee2b061f0a 100644 --- a/client/tracing/src/logging/directives.rs +++ b/client/tracing/src/logging/directives.rs @@ -114,7 +114,7 @@ pub(crate) fn set_reload_handle(handle: Handle) { let _ = FILTER_RELOAD_HANDLE.set(handle); } -// The layered Subscriber as built up in `init_logger()`. +// The layered Subscriber as built up in `LoggerBuilder::init()`. // Used in the reload `Handle`. type SCSubscriber< N = tracing_fmt::format::DefaultFields, diff --git a/client/tracing/src/logging/layers/prefix_layer.rs b/client/tracing/src/logging/layers/prefix_layer.rs index 6aa7e6d436e1a..0c8f25c24100c 100644 --- a/client/tracing/src/logging/layers/prefix_layer.rs +++ b/client/tracing/src/logging/layers/prefix_layer.rs @@ -33,9 +33,18 @@ where S: Subscriber + for<'a> LookupSpan<'a>, { fn new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { - let span = ctx - .span(id) - .expect("new_span has been called for this span; qed"); + let span = match ctx.span(id) { + Some(span) => span, + None => { + // this shouldn't happen! + debug_assert!( + false, + "newly created span with ID {:?} did not exist in the registry; this is a bug!", + id + ); + return; + } + }; if span.name() != PREFIX_LOG_SPAN { return; diff --git a/client/tracing/src/logging/mod.rs b/client/tracing/src/logging/mod.rs index ca4f74194bcc6..5674b50cb98e2 100644 --- a/client/tracing/src/logging/mod.rs +++ b/client/tracing/src/logging/mod.rs @@ -38,7 +38,7 @@ use tracing_subscriber::{ format, FormatEvent, FormatFields, Formatter, Layer as FmtLayer, MakeWriter, SubscriberBuilder, }, - layer::{self, SubscriberExt}, + layer::{self, SubscriberExt}, filter::LevelFilter, registry::LookupSpan, EnvFilter, FmtSubscriber, Layer, Registry, }; @@ -53,21 +53,15 @@ pub type Result = std::result::Result; #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] #[non_exhaustive] +#[error(transparent)] pub enum Error { - #[error(transparent)] IoError(#[from] io::Error), - - #[error(transparent)] SetGlobalDefaultError(#[from] tracing::subscriber::SetGlobalDefaultError), - - #[error(transparent)] DirectiveParseError(#[from] tracing_subscriber::filter::ParseError), - - #[error(transparent)] SetLoggerError(#[from] tracing_log::log_tracer::SetLoggerError), } -macro_rules! disable_log_reloading { +macro_rules! enable_log_reloading { ($builder:expr) => {{ let builder = $builder.with_filter_reloading(); let handle = builder.reload_handle(); @@ -77,9 +71,9 @@ macro_rules! disable_log_reloading { } /// Common implementation to get the subscriber. -fn get_subscriber_internal( - pattern: &str, - max_level: Option, +fn prepare_subscriber( + directives: &str, + profiling_targets: Option<&str>, force_colors: Option, telemetry_buffer_size: Option, telemetry_external_transport: Option, @@ -130,22 +124,28 @@ where } } - if pattern != "" { - // We're not sure if log or tracing is available at this moment, so silently ignore the - // parse error. - env_filter = parse_user_directives(env_filter, pattern)?; + if directives != "" { + env_filter = parse_user_directives(env_filter, directives)?; + } + + if let Some(profiling_targets) = profiling_targets { + env_filter = parse_user_directives(env_filter, profiling_targets)?; + env_filter = env_filter + .add_directive( + parse_default_directive("sc_tracing=trace").expect("provided directive is valid") + ); } let max_level_hint = Layer::::max_level_hint(&env_filter); - let max_level = max_level.unwrap_or_else(|| match max_level_hint { - Some(tracing_subscriber::filter::LevelFilter::INFO) | None => log::LevelFilter::Info, - Some(tracing_subscriber::filter::LevelFilter::TRACE) => log::LevelFilter::Trace, - Some(tracing_subscriber::filter::LevelFilter::WARN) => log::LevelFilter::Warn, - Some(tracing_subscriber::filter::LevelFilter::ERROR) => log::LevelFilter::Error, - Some(tracing_subscriber::filter::LevelFilter::DEBUG) => log::LevelFilter::Debug, - Some(tracing_subscriber::filter::LevelFilter::OFF) => log::LevelFilter::Off, - }); + let max_level = match max_level_hint { + Some(LevelFilter::INFO) | None => log::LevelFilter::Info, + Some(LevelFilter::TRACE) => log::LevelFilter::Trace, + Some(LevelFilter::WARN) => log::LevelFilter::Warn, + Some(LevelFilter::ERROR) => log::LevelFilter::Error, + Some(LevelFilter::DEBUG) => log::LevelFilter::Debug, + Some(LevelFilter::OFF) => log::LevelFilter::Off, + }; tracing_log::LogTracer::builder() .with_max_level(max_level) @@ -196,24 +196,24 @@ where } /// A builder that is used to initialize the global logger. -pub struct GlobalLoggerBuilder { - pattern: String, +pub struct LoggerBuilder { + directives: String, profiling: Option<(crate::TracingReceiver, String)>, telemetry_buffer_size: Option, telemetry_external_transport: Option, - disable_log_reloading: bool, + log_reloading: bool, force_colors: Option, } -impl GlobalLoggerBuilder { - /// Create a new [`GlobalLoggerBuilder`] which can be used to initialize the global logger. - pub fn new>(pattern: S) -> Self { +impl LoggerBuilder { + /// Create a new [`LoggerBuilder`] which can be used to initialize the global logger. + pub fn new>(directives: S) -> Self { Self { - pattern: pattern.into(), + directives: directives.into(), profiling: None, telemetry_buffer_size: None, telemetry_external_transport: None, - disable_log_reloading: false, + log_reloading: true, force_colors: None, } } @@ -230,7 +230,7 @@ impl GlobalLoggerBuilder { /// Wether or not to disable log reloading. pub fn with_log_reloading(&mut self, enabled: bool) -> &mut Self { - self.disable_log_reloading = !enabled; + self.log_reloading = enabled; self } @@ -257,17 +257,14 @@ impl GlobalLoggerBuilder { /// This sets various global logging and tracing instances and thus may only be called once. pub fn init(self) -> Result { if let Some((tracing_receiver, profiling_targets)) = self.profiling { - // If profiling is activated, we require `trace` logging. - let max_level = Some(log::LevelFilter::Trace); - - if self.disable_log_reloading { - let (subscriber, telemetry_worker) = get_subscriber_internal( - &format!("{},{},sc_tracing=trace", self.pattern, profiling_targets), - max_level, + if self.log_reloading { + let (subscriber, telemetry_worker) = prepare_subscriber( + &self.directives, + Some(&profiling_targets), self.force_colors, self.telemetry_buffer_size, self.telemetry_external_transport, - |builder| builder, + |builder| enable_log_reloading!(builder), )?; let profiling = crate::ProfilingLayer::new(tracing_receiver, &profiling_targets); @@ -275,13 +272,13 @@ impl GlobalLoggerBuilder { Ok(telemetry_worker) } else { - let (subscriber, telemetry_worker) = get_subscriber_internal( - &format!("{},{},sc_tracing=trace", self.pattern, profiling_targets), - max_level, + let (subscriber, telemetry_worker) = prepare_subscriber( + &self.directives, + Some(&profiling_targets), self.force_colors, self.telemetry_buffer_size, self.telemetry_external_transport, - |builder| disable_log_reloading!(builder), + |builder| builder, )?; let profiling = crate::ProfilingLayer::new(tracing_receiver, &profiling_targets); @@ -290,27 +287,27 @@ impl GlobalLoggerBuilder { Ok(telemetry_worker) } } else { - if self.disable_log_reloading { - let (subscriber, telemetry_worker) = get_subscriber_internal( - &self.pattern, + if self.log_reloading { + let (subscriber, telemetry_worker) = prepare_subscriber( + &self.directives, None, self.force_colors, self.telemetry_buffer_size, self.telemetry_external_transport, - |builder| builder, + |builder| enable_log_reloading!(builder), )?; tracing::subscriber::set_global_default(subscriber)?; Ok(telemetry_worker) } else { - let (subscriber, telemetry_worker) = get_subscriber_internal( - &self.pattern, + let (subscriber, telemetry_worker) = prepare_subscriber( + &self.directives, None, self.force_colors, self.telemetry_buffer_size, self.telemetry_external_transport, - |builder| disable_log_reloading!(builder), + |builder| builder, )?; tracing::subscriber::set_global_default(subscriber)?; @@ -331,61 +328,57 @@ mod tests { const EXPECTED_LOG_MESSAGE: &'static str = "yeah logging works as expected"; const EXPECTED_NODE_NAME: &'static str = "THE_NODE"; - fn init_logger(pattern: &str) { - let _ = GlobalLoggerBuilder::new(pattern).init().unwrap(); - } - - fn run_in_process(test_name: &str) { - if env::var("RUN_IN_PROCESS").is_err() { - let status = Command::new(env::current_exe().unwrap()) - .arg(test_name) - .env("RUN_IN_PROCESS", "true") - .status() - .unwrap(); - assert!(status.success(), "process did not ended successfully"); - std::process::exit(0); - } + fn init_logger(directives: &str) { + let _ = LoggerBuilder::new(directives).init().unwrap(); } #[test] fn test_logger_filters() { - run_in_process("test_logger_filters"); - - let test_pattern = "afg=debug,sync=trace,client=warn,telemetry,something-with-dash=error"; - init_logger(&test_pattern); - - tracing::dispatcher::get_default(|dispatcher| { - let test_filter = |target, level| { - struct DummyCallSite; - impl Callsite for DummyCallSite { - fn set_interest(&self, _: Interest) {} - fn metadata(&self) -> &Metadata<'_> { - unreachable!(); + if env::var("RUN_TEST_LOGGER_FILTERS").is_ok() { + let test_directives = "afg=debug,sync=trace,client=warn,telemetry,something-with-dash=error"; + init_logger(&test_directives); + + tracing::dispatcher::get_default(|dispatcher| { + let test_filter = |target, level| { + struct DummyCallSite; + impl Callsite for DummyCallSite { + fn set_interest(&self, _: Interest) {} + fn metadata(&self) -> &Metadata<'_> { + unreachable!(); + } } - } - - let metadata = tracing::metadata!( - name: "", - target: target, - level: level, - fields: &[], - callsite: &DummyCallSite, - kind: Kind::SPAN, - ); - - dispatcher.enabled(&metadata) - }; - - assert!(test_filter("afg", Level::INFO)); - assert!(test_filter("afg", Level::DEBUG)); - assert!(!test_filter("afg", Level::TRACE)); - - assert!(test_filter("sync", Level::TRACE)); - assert!(test_filter("client", Level::WARN)); - - assert!(test_filter("telemetry", Level::TRACE)); - assert!(test_filter("something-with-dash", Level::ERROR)); - }); + + let metadata = tracing::metadata!( + name: "", + target: target, + level: level, + fields: &[], + callsite: &DummyCallSite, + kind: Kind::SPAN, + ); + + dispatcher.enabled(&metadata) + }; + + assert!(test_filter("afg", Level::INFO)); + assert!(test_filter("afg", Level::DEBUG)); + assert!(!test_filter("afg", Level::TRACE)); + + assert!(test_filter("sync", Level::TRACE)); + assert!(test_filter("client", Level::WARN)); + + assert!(test_filter("telemetry", Level::TRACE)); + assert!(test_filter("something-with-dash", Level::ERROR)); + }); + } else { + let status = Command::new(env::current_exe().unwrap()) + .arg("test_logger_filters") + .env("RUN_TEST_LOGGER_FILTERS", "1") + .output() + .unwrap() + .status; + assert!(status.success()); + } } /// This test ensures that using dash (`-`) in the target name in logs and directives actually @@ -410,8 +403,8 @@ mod tests { #[test] fn log_something_with_dash_target_name() { if env::var("ENABLE_LOGGING").is_ok() { - let test_pattern = "test-target=info"; - let _guard = init_logger(&test_pattern); + let test_directives = "test-target=info"; + let _guard = init_logger(&test_directives); log::info!(target: "test-target", "{}", EXPECTED_LOG_MESSAGE); } @@ -506,11 +499,18 @@ mod tests { let output = command.output().unwrap(); - String::from_utf8(output.stderr).unwrap() + dbg!(String::from_utf8(output.stderr)).unwrap() } if env::var("PRINT_MAX_LOG_LEVEL").is_ok() { - init_logger(&env::var("TRACING_TARGETS").unwrap_or_default()); + let mut builder = LoggerBuilder::new(""); + + if let Ok(targets) = env::var("TRACING_TARGETS") { + builder.with_profiling(crate::TracingReceiver::Log, targets); + } + + builder.init().unwrap(); + eprint!("MAX_LOG_LEVEL={:?}", log::max_level()); } else { assert_eq!("MAX_LOG_LEVEL=Info", run_test(None, None)); diff --git a/client/transaction-pool/Cargo.toml b/client/transaction-pool/Cargo.toml index e68e39f5542dd..d457d709d1222 100644 --- a/client/transaction-pool/Cargo.toml +++ b/client/transaction-pool/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-transaction-pool" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -13,31 +13,31 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } thiserror = "1.0.21" futures = { version = "0.3.1", features = ["compat"] } futures-diagnose = "1.0" intervalier = "0.4.0" log = "0.4.8" -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } parking_lot = "0.11.1" -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.8.0"} -sc-client-api = { version = "2.0.0", path = "../api" } -sc-transaction-graph = { version = "2.0.0", path = "./graph" } -sp-api = { version = "2.0.0", path = "../../primitives/api" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } -sp-transaction-pool = { version = "2.0.0", path = "../../primitives/transaction-pool" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-utils = { version = "2.0.0", path = "../../primitives/utils" } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.9.0"} +sc-client-api = { version = "3.0.0", path = "../api" } +sc-transaction-graph = { version = "3.0.0", path = "./graph" } +sp-api = { version = "3.0.0", path = "../../primitives/api" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } +sp-transaction-pool = { version = "3.0.0", path = "../../primitives/transaction-pool" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-utils = { version = "3.0.0", path = "../../primitives/utils" } wasm-timer = "0.2" [dev-dependencies] assert_matches = "1.3.0" hex = "0.4" -sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" } -sp-consensus = { version = "0.8.0", path = "../../primitives/consensus/common" } +sp-keyring = { version = "3.0.0", path = "../../primitives/keyring" } +sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } substrate-test-runtime-transaction-pool = { version = "2.0.0", path = "../../test-utils/runtime/transaction-pool" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } -sc-block-builder = { version = "0.8.0", path = "../block-builder" } +sc-block-builder = { version = "0.9.0", path = "../block-builder" } diff --git a/client/transaction-pool/graph/Cargo.toml b/client/transaction-pool/graph/Cargo.toml index f6143f8837bfd..7ed455f9370c5 100644 --- a/client/transaction-pool/graph/Cargo.toml +++ b/client/transaction-pool/graph/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sc-transaction-graph" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" @@ -20,18 +20,18 @@ log = "0.4.8" parking_lot = "0.11.1" serde = { version = "1.0.101", features = ["derive"] } wasm-timer = "0.2" -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-utils = { version = "2.0.0", path = "../../../primitives/utils" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-transaction-pool = { version = "2.0.0", path = "../../../primitives/transaction-pool" } -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-utils = { version = "3.0.0", path = "../../../primitives/utils" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-transaction-pool = { version = "3.0.0", path = "../../../primitives/transaction-pool" } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } linked-hash-map = "0.5.2" retain_mut = "0.1.2" [dev-dependencies] assert_matches = "1.3.0" -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } substrate-test-runtime = { version = "2.0.0", path = "../../../test-utils/runtime" } criterion = "0.3" diff --git a/client/transaction-pool/src/api.rs b/client/transaction-pool/src/api.rs index fc14a5a0cba64..2ebf038844fab 100644 --- a/client/transaction-pool/src/api.rs +++ b/client/transaction-pool/src/api.rs @@ -84,7 +84,6 @@ where Client: ProvideRuntimeApi + BlockBackend + BlockIdTo, Client: Send + Sync + 'static, Client::Api: TaggedTransactionQueue, - sp_api::ApiErrorFor: Send + std::fmt::Display, { type Block = Block; type Error = error::Error; @@ -166,14 +165,13 @@ where Client: ProvideRuntimeApi + BlockBackend + BlockIdTo, Client: Send + Sync + 'static, Client::Api: TaggedTransactionQueue, - sp_api::ApiErrorFor: Send + std::fmt::Display, { sp_tracing::within_span!(sp_tracing::Level::TRACE, "validate_transaction"; { let runtime_api = client.runtime_api(); let has_v2 = sp_tracing::within_span! { sp_tracing::Level::TRACE, "check_version"; runtime_api - .has_api_with::, _>(&at, |v| v >= 2) + .has_api_with::, _>(&at, |v| v >= 2) .unwrap_or_default() }; @@ -198,7 +196,6 @@ where Client: ProvideRuntimeApi + BlockBackend + BlockIdTo, Client: Send + Sync + 'static, Client::Api: TaggedTransactionQueue, - sp_api::ApiErrorFor: Send + std::fmt::Display, { /// Validates a transaction by calling into the runtime, same as /// `validate_transaction` but blocks the current thread when performing diff --git a/client/transaction-pool/src/lib.rs b/client/transaction-pool/src/lib.rs index 32525065b9798..b6f19ba376861 100644 --- a/client/transaction-pool/src/lib.rs +++ b/client/transaction-pool/src/lib.rs @@ -360,7 +360,6 @@ where + sp_runtime::traits::BlockIdTo, Client: sc_client_api::ExecutorProvider + Send + Sync + 'static, Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue, - sp_api::ApiErrorFor: Send + std::fmt::Display, { /// Create new basic transaction pool for a full node with the provided api. pub fn new_full( @@ -391,7 +390,6 @@ where + sp_runtime::traits::BlockIdTo, Client: Send + Sync + 'static, Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue, - sp_api::ApiErrorFor: Send + std::fmt::Display, { type Block = Block; type Hash = sc_transaction_graph::ExtrinsicHash>; diff --git a/docs/Upgrading-2.0-to-3.0.md b/docs/Upgrading-2.0-to-3.0.md new file mode 100644 index 0000000000000..bc4a15eb15f27 --- /dev/null +++ b/docs/Upgrading-2.0-to-3.0.md @@ -0,0 +1,1120 @@ +# Upgrading from Substrate 2.0 to 3.0 + +An incomplete guide. + +## Refreshing the node-template + +Not much has changed on the top and API level for developing Substrate betweeen 2.0 and 3.0. If you've made only small changes to the node-template, we recommend to do the following - it is easiest and quickest path forward: +1. take a diff between 2.0 and your changes +2. store that diff +3. remove everything, copy over the 3.0 node-template +4. try re-applying your diff, manually, a hunk at a time. + +## In-Depth guide on the changes + +If you've made significant changes or diverted from the node-template a lot, starting out with that is probably not helping. For that case, we'll take a look at all changes between 2.0 and 3.0 to the fully-implemented node and explain them one by one, so you can follow up, what needs to be changing for your node. + +_Note_: Of course, step 1 is to upgrade your `Cargo.toml`'s to use the latest version of Substrate and all dependencies. + +We'll be taking the diff from 2.0.1 to 3.0.0 on `bin/node` as the baseline of what has changed between these two versions in terms of adapting ones code base. We will not be covering the changes made on the tests and bench-marking as they are mostly reactions to the other changes. + +### Versions upgrade + +First and foremost you have to upgrade the version pf the dependencies of course, that's `0.8.x -> 0.9.0` and `2.0.x -> 3.0.0` for all `sc-`, `sp-`, `frame-`, and `pallet-` coming from Parity. Further more this release also upgraded its own dependencies, most notably, we are now using `parity-scale-codec 2.0`, `parking_lot 0.11` and `substrate-wasm-builder 3.0.0` (as build dependency). All other dependency upgrades should resolve automatically or are just internal. However you might see some error that another dependency/type you have as a dependency and one of our upgraded crates don't match up, if so please check the version of said dependency - we've probably ugraded it. + +### WASM-Builder + +The new version of wasm-builder has gotten a bit smarter and a lot faster (you should definitly switch). Once you've upgraded the dependency, in most cases you just have to remove the now obsolete `with_wasm_builder_from_crates_or_path`-function and you are good to go: + +```diff: rust +--- a/bin/node/runtime/build.rs ++++ b/bin/node/runtime/build.rs +@@ -15,12 +15,11 @@ + // See the License for the specific language governing permissions and + // limitations under the License. + +-use wasm_builder_runner::WasmBuilder; ++use substrate_wasm_builder::WasmBuilder; + + fn main() { + WasmBuilder::new() + .with_current_project() +- .with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder") + .export_heap_base() + .import_memory() + .build() +``` + +### Runtime + +#### FRAME 2.0 + +The new FRAME 2.0 macros are a lot nicer to use and easier to read. While we were on that change though, we also cleaned up some mainly internal names and traits. The old `macro`'s still work and also produce the new structure, however, when plugging all that together as a Runtime, there's some things we have to adapt now: + +##### `::Trait for Runtime` becomes `::Config for Runtime` + +The most visible and significant change is that the macros no longer generate the `$pallet::Trait` but now a much more aptly named `$pallet::Config`. Thus, we need to rename all `::Trait for Runtime` into`::Config for Runtime`, e.g. for the `sudo` pallet we must do: + +```diff +-impl pallet_sudo::Trait for Runtime { ++impl pallet_sudo::Config for Runtime { +``` + +The same goes for all `` and alike, which simply becomes ``. + +#### SS58 Prefix is now a runtime param + + +Since [#7810](https://github.com/paritytech/substrate/pull/7810) we don't define the ss58 prefix in the chainspec anymore but moved it into the runtime. Namely, `frame_system` now needs a new `SS58Prefix`, which in substrate node we have defined for ourselves as: `pub const SS58Prefix: u8 = 42;`. Use your own chain-specific value there. + +#### Weight Definition + +`type WeightInfo` has changed and instead on `weights::pallet_$name::WeightInfo` is now bound to the Runtime as `pallet_$name::weights::SubstrateWeight`. As a result we have to the change the type definitions everywhere in our Runtime accordingly: + +```diff +- type WeightInfo = weights::pallet_$name::WeightInfo; ++ type WeightInfo = pallet_$name::weights::SubstrateWeight; +``` + +e.g. +```diff +- type WeightInfo = weights::pallet_collective::WeightInfo; ++ type WeightInfo = pallet_collective::weights::SubstrateWeight; +``` +and + +```diff +- type WeightInfo = weights::pallet_proxy::WeightInfo; ++ type WeightInfo = pallet_proxy::weights::SubstrateWeight; +``` + +And update the overall definition for weights on frame and a few related types and runtime parameters: + +```diff= + +-const AVERAGE_ON_INITIALIZE_WEIGHT: Perbill = Perbill::from_percent(10); ++/// We assume that ~10% of the block weight is consumed by `on_initalize` handlers. ++/// This is used to limit the maximal weight of a single extrinsic. ++const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10); ++/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used ++/// by Operational extrinsics. ++const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); ++/// We allow for 2 seconds of compute with a 6 second average block time. ++const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND; ++ + parameter_types! { + pub const BlockHashCount: BlockNumber = 2400; +- /// We allow for 2 seconds of compute with a 6 second average block time. +- pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND; +- pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75); +- /// Assume 10% of weight for average on_initialize calls. +- pub MaximumExtrinsicWeight: Weight = +- AvailableBlockRatio::get().saturating_sub(AVERAGE_ON_INITIALIZE_WEIGHT) +- * MaximumBlockWeight::get(); +- pub const MaximumBlockLength: u32 = 5 * 1024 * 1024; + pub const Version: RuntimeVersion = VERSION; +-} +- +-const_assert!(AvailableBlockRatio::get().deconstruct() >= AVERAGE_ON_INITIALIZE_WEIGHT.deconstruct()); +- +-impl frame_system::Trait for Runtime { ++ pub RuntimeBlockLength: BlockLength = ++ BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO); ++ pub RuntimeBlockWeights: BlockWeights = BlockWeights::builder() ++ .base_block(BlockExecutionWeight::get()) ++ .for_class(DispatchClass::all(), |weights| { ++ weights.base_extrinsic = ExtrinsicBaseWeight::get(); ++ }) ++ .for_class(DispatchClass::Normal, |weights| { ++ weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT); ++ }) ++ .for_class(DispatchClass::Operational, |weights| { ++ weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT); ++ // Operational transactions have some extra reserved space, so that they ++ // are included even if block reached `MAXIMUM_BLOCK_WEIGHT`. ++ weights.reserved = Some( ++ MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT ++ ); ++ }) ++ .avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO) ++ .build_or_panic(); ++} ++ ++const_assert!(NORMAL_DISPATCH_RATIO.deconstruct() >= AVERAGE_ON_INITIALIZE_RATIO.deconstruct()); ++ ++impl frame_system::Config for Runtime { + type BaseCallFilter = (); ++ type BlockWeights = RuntimeBlockWeights; ++ type BlockLength = RuntimeBlockLength; ++ type DbWeight = RocksDbWeight; + type Origin = Origin; + type Call = Call; + type Index = Index; +@@ -171,25 +198,19 @@ impl frame_system::Trait for Runtime { + type Header = generic::Header; + type Event = Event; + type BlockHashCount = BlockHashCount; +- type MaximumBlockWeight = MaximumBlockWeight; +- type DbWeight = RocksDbWeight; +- type BlockExecutionWeight = BlockExecutionWeight; +- type ExtrinsicBaseWeight = ExtrinsicBaseWeight; +- type MaximumExtrinsicWeight = MaximumExtrinsicWeight; +- type MaximumBlockLength = MaximumBlockLength; +- type AvailableBlockRatio = AvailableBlockRatio; + type Version = Version; + type PalletInfo = PalletInfo; + type AccountData = pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); +- type SystemWeightInfo = weights::frame_system::WeightInfo; ++ type SystemWeightInfo = frame_system::weights::SubstrateWeight; +``` + +#### Pallets: + +##### Assets + +The assets pallet has seen a variety of changes: +- [Features needed for reserve-backed stablecoins #7152 ](https://github.com/paritytech/substrate/pull/7152) +- [Freeze Assets and Asset Metadata #7346 ](https://github.com/paritytech/substrate/pull/7346) +- [Introduces account existence providers reference counting #7363 ]((https://github.com/paritytech/substrate/pull/7363)) + +have all altered the feature set and changed the concepts. However, it has some of the best documentation and explains the current state very well. If you are using the assets pallet and need to upgrade from an earlier version, we recommend you use the current docs to guide your way! + +##### Contracts + +As noted in the changelog, the `contracts`-pallet is still undergoing massive changes and is not yet part of this release. We are expecting for it to be released a few weeks after. If your chain is dependent on this pallet, we recommend to wait until it has been released as the currently released version is not compatible with FRAME 2.0. + +#### (changes) Treasury + +As mentioned above, Bounties, Tips and Lottery have been extracted out of treasury into their own pallets - removing these options here. Secondly we must now specify the `BurnDestination` and `SpendFunds`, which now go the `Bounties`. + +```diff +- type Tippers = Elections; +- type TipCountdown = TipCountdown; +- type TipFindersFee = TipFindersFee; +- type TipReportDepositBase = TipReportDepositBase; +- type DataDepositPerByte = DataDepositPerByte; + type Event = Event; + type OnSlash = (); + type ProposalBond = ProposalBond; + type ProposalBondMinimum = ProposalBondMinimum; + type SpendPeriod = SpendPeriod; + type Burn = Burn; ++ type BurnDestination = (); ++ type SpendFunds = Bounties; +``` + +Factoring out Bounties and Tips means most of these definitions have now moved there, while the parameter types can be left as they were: + +###### 🆕 Bounties + +```rust= +impl pallet_bounties::Config for Runtime { + type Event = Event; + type BountyDepositBase = BountyDepositBase; + type BountyDepositPayoutDelay = BountyDepositPayoutDelay; + type BountyUpdatePeriod = BountyUpdatePeriod; + type BountyCuratorDeposit = BountyCuratorDeposit; + type BountyValueMinimum = BountyValueMinimum; + type DataDepositPerByte = DataDepositPerByte; + type MaximumReasonLength = MaximumReasonLength; + type WeightInfo = pallet_bounties::weights::SubstrateWeight; + } +``` + +###### 🆕 Tips + +```rust= +impl pallet_tips::Config for Runtime { + type Event = Event; + type DataDepositPerByte = DataDepositPerByte; + type MaximumReasonLength = MaximumReasonLength; + type Tippers = Elections; + type TipCountdown = TipCountdown; + type TipFindersFee = TipFindersFee; + type TipReportDepositBase = TipReportDepositBase; + type WeightInfo = pallet_tips::weights::SubstrateWeight; + } +``` + +#### `FinalityTracker` removed + +Finality Tracker has been removed in favor of a different approach to handle the issue in GRANDPA, [see #7228 for details](https://github.com/paritytech/substrate/pull/7228). With latest GRANDPA this is not needed anymore and can be removed without worry. + +#### (changes) Elections Phragmen + +The pallet has been moved to a new system in which the exact amount of deposit for each voter, candidate, member, or runner-up is now deposited on-chain. Moreover, the concept of a `defunct_voter` is removed, since votes now have adequet deposit associated with them. A number of configuration parameters has changed to reflect this, as shown below: + +```diff= + parameter_types! { + pub const CandidacyBond: Balance = 10 * DOLLARS; +- pub const VotingBond: Balance = 1 * DOLLARS; ++ // 1 storage item created, key size is 32 bytes, value size is 16+16. ++ pub const VotingBondBase: Balance = deposit(1, 64); ++ // additional data per vote is 32 bytes (account id). ++ pub const VotingBondFactor: Balance = deposit(0, 32); + pub const TermDuration: BlockNumber = 7 * DAYS; + pub const DesiredMembers: u32 = 13; + pub const DesiredRunnersUp: u32 = 7; + +@@ -559,16 +600,16 @@ impl pallet_elections_phragmen::Trait for Runtime { + // NOTE: this implies that council's genesis members cannot be set directly and must come from + // this module. + type InitializeMembers = Council; +- type CurrencyToVote = CurrencyToVoteHandler; ++ type CurrencyToVote = U128CurrencyToVote; + type CandidacyBond = CandidacyBond; +- type VotingBond = VotingBond; ++ type VotingBondBase = VotingBondBase; ++ type VotingBondFactor = VotingBondFactor; + type LoserCandidate = (); +- type BadReport = (); + type KickedMember = (); + type DesiredMembers = DesiredMembers; + type DesiredRunnersUp = DesiredRunnersUp; + type TermDuration = TermDuration; + ``` + + **This upgrade requires storage [migration](https://github.com/paritytech/substrate/blob/master/frame/elections-phragmen/src/migrations_3_0_0.rs)**. Further details can be found in the [pallet-specific changelog](https://github.com/paritytech/substrate/blob/master/frame/elections-phragmen/CHANGELOG.md#security). + +#### (changes) Democracy + +Democracy brings three new settings with this release, all to allow for better influx- and spam-control. Namely these allow to specify the maximum number of proposals at a time, who can blacklist and who can cancel proposals. This diff acts as a good starting point: + +```diff= +@@ -508,6 +537,14 @@ impl pallet_democracy::Trait for Runtime { + type FastTrackVotingPeriod = FastTrackVotingPeriod; + // To cancel a proposal which has been passed, 2/3 of the council must agree to it. + type CancellationOrigin = pallet_collective::EnsureProportionAtLeast<_2, _3, AccountId, CouncilCollective>; ++ // To cancel a proposal before it has been passed, the technical committee must be unanimous or ++ // Root must agree. ++ type CancelProposalOrigin = EnsureOneOf< ++ AccountId, ++ EnsureRoot, ++ pallet_collective::EnsureProportionAtLeast<_1, _1, AccountId, TechnicalCollective>, ++ >; ++ type BlacklistOrigin = EnsureRoot; + // Any single technical committee member may veto a coming council proposal, however they can + // only do it once and it lasts only for the cooloff period. + type VetoOrigin = pallet_collective::EnsureMember; +@@ -518,7 +555,8 @@ impl pallet_democracy::Trait for Runtime { + type Scheduler = Scheduler; + type PalletsOrigin = OriginCaller; + type MaxVotes = MaxVotes; ++ type MaxProposals = MaxProposals; + } +``` + +---- + +### Primitives + +The shared primitives define the API between Client and Runtime. Usually, you don't have to touch nor directly interact with them, unless you created your own client or frame-less runtime. Therefore we'd expect you to understand whether you are effected by changes and how to update your code yourself. + +---- + +### Client + +#### CLI + +A few minor things have changed in the `cli` (compared to 2.0.1): + +1. we've [replaced the newly added `BuildSyncSpec` subcommand with an RPC API](https://github.com/paritytech/substrate/commit/65cc9af9b8df8d36928f6144ee7474cefbd70454#diff-c57da6fbeff8c46ce15f55ea42fedaa5a4684d79578006ce4af01ae04fd6b8f8) in an on-going effort to make light-client-support smoother, see below +2. we've [removed double accounts from our chainspec-builder](https://github.com/paritytech/substrate/commit/31499cd29ed30df932fb71b7459796f7160d0272) +3. we [don't fallback to `--chain flaming-fir` anymore](https://github.com/paritytech/substrate/commit/13cdf1c8cd2ee62d411f82b64dc7eba860c9c6c6), if no chain is given our substrate-node will error. +4. [the `subkey`-integration has seen a fix to the `insert`-command](https://github.com/paritytech/substrate/commit/54bde60cfd2c544c54e9e8623b6b8725b99557f8) that requires you to now add the `&cli` as a param. + ```diff= + --- a/bin/node/cli/src/command.rs + +++ b/bin/node/cli/src/command.rs + @@ -92,7 +97,7 @@ pub fn run() -> Result<()> { + You can enable it with `--features runtime-benchmarks`.".into()) + } + } + - Some(Subcommand::Key(cmd)) => cmd.run(), + + Some(Subcommand::Key(cmd)) => cmd.run(&cli), + Some(Subcommand::Sign(cmd)) => cmd.run(), + Some(Subcommand::Verify(cmd)) => cmd.run(), + Some(Subcommand::Vanity(cmd)) => cmd.run(), + ``` + + +#### Service Builder Upgrades + +##### Light client support + +As said, we've added a new optional RPC service for improved light client support. For that to work, we need to pass the `chain_spec` and give access to the `AuxStore` to our `rpc`: + + +```diff= + +--- a/bin/node/rpc/src/lib.rs ++++ b/bin/node/rpc/src/lib.rs +@@ -49,6 +49,7 @@ use sp_consensus::SelectChain; + use sp_consensus_babe::BabeApi; + use sc_rpc::SubscriptionTaskExecutor; + use sp_transaction_pool::TransactionPool; ++use sc_client_api::AuxStore; + + /// Light client extra dependencies. + pub struct LightDeps { +@@ -94,6 +95,8 @@ pub struct FullDeps { + pub pool: Arc

, + /// The SelectChain Strategy + pub select_chain: SC, ++ /// A copy of the chain spec. ++ pub chain_spec: Box, + /// Whether to deny unsafe calls + pub deny_unsafe: DenyUnsafe, + /// BABE specific dependencies. +@@ -109,9 +112,8 @@ pub type IoHandler = jsonrpc_core::IoHandler; + pub fn create_full( + deps: FullDeps, + ) -> jsonrpc_core::IoHandler where +- C: ProvideRuntimeApi, +- C: HeaderBackend + HeaderMetadata + 'static, +- C: Send + Sync + 'static, ++ C: ProvideRuntimeApi + HeaderBackend + AuxStore + ++ HeaderMetadata + Sync + Send + 'static, + C::Api: substrate_frame_rpc_system::AccountNonceApi, + C::Api: pallet_contracts_rpc::ContractsRuntimeApi, + C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi, +@@ -131,6 +133,7 @@ pub fn create_full( + client, + pool, + select_chain, ++ chain_spec, + deny_unsafe, + babe, + grandpa, +@@ -164,8 +167,8 @@ pub fn create_full( + io.extend_with( + sc_consensus_babe_rpc::BabeApi::to_delegate( + BabeRpcHandler::new( +- client, +- shared_epoch_changes, ++ client.clone(), ++ shared_epoch_changes.clone(), + keystore, + babe_config, + select_chain, +@@ -176,7 +179,7 @@ pub fn create_full( + io.extend_with( + sc_finality_grandpa_rpc::GrandpaApi::to_delegate( + GrandpaRpcHandler::new( +- shared_authority_set, ++ shared_authority_set.clone(), + shared_voter_state, + justification_stream, + subscription_executor, + +``` + +and add the new service: + +```diff= +--- a/bin/node/rpc/src/lib.rs ++++ b/bin/node/rpc/src/lib.rs +@@ -185,6 +188,18 @@ pub fn create_full( + ) + ); + ++ io.extend_with( ++ sc_sync_state_rpc::SyncStateRpcApi::to_delegate( ++ sc_sync_state_rpc::SyncStateRpcHandler::new( ++ chain_spec, ++ client, ++ shared_authority_set, ++ shared_epoch_changes, ++ deny_unsafe, ++ ) ++ ) ++ ); ++ + io + } +``` + +##### Telemetry + +The telemetry subsystem has seen a few fixes and refactorings to allow for a more flexible handling, in particular in regards to parachains. Most notably `sc_service::spawn_tasks` now returns the `telemetry_connection_notifier` as the second member of the tuple, (`let (_rpc_handlers, telemetry_connection_notifier) = sc_service::spawn_tasks(`), which should be passed to `telemetry_on_connect` of `new_full_base` now: `telemetry_on_connect: telemetry_connection_notifier.map(|x| x.on_connect_stream()),` (see the service-section below for a full diff). + +On the browser-side, this complicates setup a tiny bit, yet not terribly. Instead of `init_console_log`, we now use `init_logging_and_telemetry` and need to make sure we spawn the runner for its handleat the end (the other changes are formatting and cosmetics): + +```diff +--- a/bin/node/cli/src/browser.rs ++++ b/bin/node/cli/src/browser.rs +@@ -21,9 +21,8 @@ use log::info; + use wasm_bindgen::prelude::*; + use browser_utils::{ + Client, +- browser_configuration, set_console_error_panic_hook, init_console_log, ++ browser_configuration, init_logging_and_telemetry, set_console_error_panic_hook, + }; +-use std::str::FromStr; + + /// Starts the client. + #[wasm_bindgen] +@@ -33,29 +32,38 @@ pub async fn start_client(chain_spec: Option, log_level: String) -> Resu + .map_err(|err| JsValue::from_str(&err.to_string())) + } + +-async fn start_inner(chain_spec: Option, log_level: String) -> Result> { ++async fn start_inner( ++ chain_spec: Option, ++ log_directives: String, ++) -> Result> { + set_console_error_panic_hook(); +- init_console_log(log::Level::from_str(&log_level)?)?; ++ let telemetry_worker = init_logging_and_telemetry(&log_directives)?; + let chain_spec = match chain_spec { + Some(chain_spec) => ChainSpec::from_json_bytes(chain_spec.as_bytes().to_vec()) + .map_err(|e| format!("{:?}", e))?, + None => crate::chain_spec::development_config(), + }; + +- let config = browser_configuration(chain_spec).await?; ++ let telemetry_handle = telemetry_worker.handle(); ++ let config = browser_configuration( ++ chain_spec, ++ Some(telemetry_handle), ++ ).await?; + + info!("Substrate browser node"); + info!("✌️ version {}", config.impl_version); +- info!("❤️ by Parity Technologies, 2017-2020"); ++ info!("❤️ by Parity Technologies, 2017-2021"); + info!("📋 Chain specification: {}", config.chain_spec.name()); +- info!("🏷 Node name: {}", config.network.node_name); ++ info!("🏷 Node name: {}", config.network.node_name); + info!("👤 Role: {:?}", config.role); + + // Create the service. This is the most heavy initialization step. + let (task_manager, rpc_handlers) = + crate::service::new_light_base(config) +- .map(|(components, rpc_handlers, _, _, _)| (components, rpc_handlers)) ++ .map(|(components, rpc_handlers, _, _, _, _)| (components, rpc_handlers)) + .map_err(|e| format!("{:?}", e))?; + ++ task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); ++ + Ok(browser_utils::start_client(task_manager, rpc_handlers)) + } + ``` + +##### Async & Remote Keystore support + +In order to allow for remote-keystores, the keystore-subsystem has been reworked to support async operations and generally refactored to not provide the keys itself but only sign on request. This allows for remote-keystore to never hand out keys and thus to operate any substrate-based node in a manner without ever having the private keys in the local system memory. + +There are some operations, however, that the keystore must be local for performance reasons and for which a remote keystore won't work (in particular around parachains). As such, the keystore has both a slot for remote but also always a local instance, where some operations hard bind to the local variant, while most subsystems just ask the generic keystore which prefers a remote signer if given. To reflect this change, `sc_service::new_full_parts` now returns a `KeystoreContainer` rather than the keystore, and the other subsystems (e.g. `sc_service::PartialComponents`) expect to be given that. + +###### on RPC: + +This has most visible changes for the rpc, where we are switching from the previous `KeyStorePtr` to the new `SyncCryptoStorePtr`: + +```diff + +--- a/bin/node/rpc/src/lib.rs ++++ b/bin/node/rpc/src/lib.rs +@@ -32,6 +32,7 @@ + + use std::sync::Arc; + ++use sp_keystore::SyncCryptoStorePtr; + use node_primitives::{Block, BlockNumber, AccountId, Index, Balance, Hash}; + use sc_consensus_babe::{Config, Epoch}; + use sc_consensus_babe_rpc::BabeRpcHandler; +@@ -40,7 +41,6 @@ use sc_finality_grandpa::{ + SharedVoterState, SharedAuthoritySet, FinalityProofProvider, GrandpaJustificationStream + }; + use sc_finality_grandpa_rpc::GrandpaRpcHandler; +-use sc_keystore::KeyStorePtr; + pub use sc_rpc_api::DenyUnsafe; + use sp_api::ProvideRuntimeApi; + use sp_block_builder::BlockBuilder; + pub struct LightDeps { +@@ -69,7 +70,7 @@ pub struct BabeDeps { + /// BABE pending epoch changes. + pub shared_epoch_changes: SharedEpochChanges, + /// The keystore that manages the keys of the node. +- pub keystore: KeyStorePtr, ++ pub keystore: SyncCryptoStorePtr, + } + +``` + +##### GRANDPA + +As already in the changelog, a few things significant things have changed in regards to GRANDPA: the finality tracker has been replaced, an RPC command has been added and WARP-sync-support for faster light client startup has been implemented. All this means we have to do a few changes to our GRANDPA setup procedures in the client. + +First and foremost, grandpa internalised a few aspects, and thus `new_partial` doesn't expect a tuple but only the `grandpa::SharedVoterState` as input now, and unpacking that again later is not needed anymore either. On the opposite side `grandpa::FinalityProofProvider::new_for_service` now requires the `Some(shared_authority_set)` to be passed as a new third parameter. This set also becomes relevant when adding warp-sync-support, which is added as an extra-protocol-layer to the networking as: +```diff= + ++ config.network.extra_sets.push(grandpa::grandpa_peers_set_config()); ++ ++ #[cfg(feature = "cli")] ++ config.network.request_response_protocols.push(sc_finality_grandpa_warp_sync::request_response_config_for_chain( ++ &config, task_manager.spawn_handle(), backend.clone(), ++ )); +``` + +As these changes pull through the enitrety of `cli/src/service.rs`, we recommend looking at the final diff below for guidance. + +##### In a nutshell + +Altogether this accumulates to the following diff for `node/cli/src/service.rs`. If you want these features and have modified your chain you should probably try to apply these patches: + + +```diff= +--- a/bin/node/cli/src/service.rs ++++ b/bin/node/cli/src/service.rs +@@ -22,11 +22,10 @@ + + use std::sync::Arc; + use sc_consensus_babe; +-use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider}; + use node_primitives::Block; + use node_runtime::RuntimeApi; + use sc_service::{ +- config::{Role, Configuration}, error::{Error as ServiceError}, ++ config::{Configuration}, error::{Error as ServiceError}, + RpcHandlers, TaskManager, + }; + use sp_inherents::InherentDataProviders; +@@ -34,8 +33,8 @@ use sc_network::{Event, NetworkService}; + use sp_runtime::traits::Block as BlockT; + use futures::prelude::*; + use sc_client_api::{ExecutorProvider, RemoteBackend}; +-use sp_core::traits::BareCryptoStorePtr; + use node_executor::Executor; ++use sc_telemetry::TelemetryConnectionNotifier; + + type FullClient = sc_service::TFullClient; + type FullBackend = sc_service::TFullBackend; +@@ -58,13 +57,10 @@ pub fn new_partial(config: &Configuration) -> Result, + sc_consensus_babe::BabeLink, + ), +- ( +- grandpa::SharedVoterState, +- Arc>, +- ), ++ grandpa::SharedVoterState, + ) + >, ServiceError> { +- let (client, backend, keystore, task_manager) = ++ let (client, backend, keystore_container, task_manager) = + sc_service::new_full_parts::(&config)?; + let client = Arc::new(client); + +@@ -94,7 +90,6 @@ pub fn new_partial(config: &Configuration) -> Result Result Result Result, + &sc_consensus_babe::BabeLink, + ) + ) -> Result { + let sc_service::PartialComponents { +- client, backend, mut task_manager, import_queue, keystore, select_chain, transaction_pool, ++ client, ++ backend, ++ mut task_manager, ++ import_queue, ++ keystore_container, ++ select_chain, ++ transaction_pool, + inherent_data_providers, + other: (rpc_extensions_builder, import_setup, rpc_setup), + } = new_partial(&config)?; + +- let (shared_voter_state, finality_proof_provider) = rpc_setup; ++ let shared_voter_state = rpc_setup; ++ ++ config.network.extra_sets.push(grandpa::grandpa_peers_set_config()); ++ ++ #[cfg(feature = "cli")] ++ config.network.request_response_protocols.push(sc_finality_grandpa_warp_sync::request_response_config_for_chain( ++ &config, task_manager.spawn_handle(), backend.clone(), ++ )); + + let (network, network_status_sinks, system_rpc_tx, network_starter) = + sc_service::build_network(sc_service::BuildNetworkParams { +@@ -191,8 +209,6 @@ pub fn new_full_base( + import_queue, + on_demand: None, + block_announce_validator_builder: None, +- finality_proof_request_builder: None, +- finality_proof_provider: Some(finality_proof_provider.clone()), + })?; + + if config.offchain_worker.enabled { +@@ -203,26 +219,28 @@ pub fn new_full_base( + + let role = config.role.clone(); + let force_authoring = config.force_authoring; ++ let backoff_authoring_blocks = ++ Some(sc_consensus_slots::BackoffAuthoringOnFinalizedHeadLagging::default()); + let name = config.network.node_name.clone(); + let enable_grandpa = !config.disable_grandpa; + let prometheus_registry = config.prometheus_registry().cloned(); +- let telemetry_connection_sinks = sc_service::TelemetryConnectionSinks::default(); + +- sc_service::spawn_tasks(sc_service::SpawnTasksParams { +- config, +- backend: backend.clone(), +- client: client.clone(), +- keystore: keystore.clone(), +- network: network.clone(), +- rpc_extensions_builder: Box::new(rpc_extensions_builder), +- transaction_pool: transaction_pool.clone(), +- task_manager: &mut task_manager, +- on_demand: None, +- remote_blockchain: None, +- telemetry_connection_sinks: telemetry_connection_sinks.clone(), +- network_status_sinks: network_status_sinks.clone(), +- system_rpc_tx, +- })?; ++ let (_rpc_handlers, telemetry_connection_notifier) = sc_service::spawn_tasks( ++ sc_service::SpawnTasksParams { ++ config, ++ backend: backend.clone(), ++ client: client.clone(), ++ keystore: keystore_container.sync_keystore(), ++ network: network.clone(), ++ rpc_extensions_builder: Box::new(rpc_extensions_builder), ++ transaction_pool: transaction_pool.clone(), ++ task_manager: &mut task_manager, ++ on_demand: None, ++ remote_blockchain: None, ++ network_status_sinks: network_status_sinks.clone(), ++ system_rpc_tx, ++ }, ++ )?; + + let (block_import, grandpa_link, babe_link) = import_setup; + +@@ -230,6 +248,7 @@ pub fn new_full_base( + + if let sc_service::config::Role::Authority { .. } = &role { + let proposer = sc_basic_authorship::ProposerFactory::new( ++ task_manager.spawn_handle(), + client.clone(), + transaction_pool.clone(), + prometheus_registry.as_ref(), +@@ -239,7 +258,7 @@ pub fn new_full_base( + sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()); + + let babe_config = sc_consensus_babe::BabeParams { +- keystore: keystore.clone(), ++ keystore: keystore_container.sync_keystore(), + client: client.clone(), + select_chain, + env: proposer, +@@ -247,6 +266,7 @@ pub fn new_full_base( + sync_oracle: network.clone(), + inherent_data_providers: inherent_data_providers.clone(), + force_authoring, ++ backoff_authoring_blocks, + babe_link, + can_author_with, + }; +@@ -256,42 +276,30 @@ pub fn new_full_base( + } + + // Spawn authority discovery module. +- if matches!(role, Role::Authority{..} | Role::Sentry {..}) { +- let (sentries, authority_discovery_role) = match role { +- sc_service::config::Role::Authority { ref sentry_nodes } => ( +- sentry_nodes.clone(), +- sc_authority_discovery::Role::Authority ( +- keystore.clone(), +- ), +- ), +- sc_service::config::Role::Sentry {..} => ( +- vec![], +- sc_authority_discovery::Role::Sentry, +- ), +- _ => unreachable!("Due to outer matches! constraint; qed.") +- }; +- ++ if role.is_authority() { ++ let authority_discovery_role = sc_authority_discovery::Role::PublishAndDiscover( ++ keystore_container.keystore(), ++ ); + let dht_event_stream = network.event_stream("authority-discovery") + .filter_map(|e| async move { match e { + Event::Dht(e) => Some(e), + _ => None, +- }}).boxed(); ++ }}); + let (authority_discovery_worker, _service) = sc_authority_discovery::new_worker_and_service( + client.clone(), + network.clone(), +- sentries, +- dht_event_stream, ++ Box::pin(dht_event_stream), + authority_discovery_role, + prometheus_registry.clone(), + ); + +- task_manager.spawn_handle().spawn("authority-discovery-worker", authority_discovery_worker); ++ task_manager.spawn_handle().spawn("authority-discovery-worker", authority_discovery_worker.run()); + } + + // if the node isn't actively participating in consensus then it doesn't + // need a keystore, regardless of which protocol we use below. + let keystore = if role.is_authority() { +- Some(keystore as BareCryptoStorePtr) ++ Some(keystore_container.sync_keystore()) + } else { + None + }; +@@ -317,8 +325,7 @@ pub fn new_full_base( + config, + link: grandpa_link, + network: network.clone(), +- inherent_data_providers: inherent_data_providers.clone(), +- telemetry_on_connect: Some(telemetry_connection_sinks.on_connect_stream()), ++ telemetry_on_connect: telemetry_connection_notifier.map(|x| x.on_connect_stream()), + voting_rule: grandpa::VotingRulesBuilder::default().build(), + prometheus_registry, + shared_voter_state, +@@ -330,17 +337,15 @@ pub fn new_full_base( + "grandpa-voter", + grandpa::run_grandpa_voter(grandpa_config)? + ); +- } else { +- grandpa::setup_disabled_grandpa( +- client.clone(), +- &inherent_data_providers, +- network.clone(), +- )?; + } + + network_starter.start_network(); + Ok(NewFullBase { +- task_manager, inherent_data_providers, client, network, network_status_sinks, ++ task_manager, ++ inherent_data_providers, ++ client, ++ network, ++ network_status_sinks, + transaction_pool, + }) + } +@@ -353,14 +358,16 @@ pub fn new_full(config: Configuration) + }) + } + +-pub fn new_light_base(config: Configuration) -> Result<( +- TaskManager, RpcHandlers, Arc, ++pub fn new_light_base(mut config: Configuration) -> Result<( ++ TaskManager, RpcHandlers, Option, Arc, + Arc::Hash>>, + Arc>> + ), ServiceError> { +- let (client, backend, keystore, mut task_manager, on_demand) = ++ let (client, backend, keystore_container, mut task_manager, on_demand) = + sc_service::new_light_parts::(&config)?; + ++ config.network.extra_sets.push(grandpa::grandpa_peers_set_config()); ++ + let select_chain = sc_consensus::LongestChain::new(backend.clone()); + + let transaction_pool = Arc::new(sc_transaction_pool::BasicPool::new_light( +@@ -371,14 +378,12 @@ pub fn new_light_base(config: Configuration) -> Result<( + on_demand.clone(), + )); + +- let grandpa_block_import = grandpa::light_block_import( +- client.clone(), backend.clone(), &(client.clone() as Arc<_>), +- Arc::new(on_demand.checker().clone()), ++ let (grandpa_block_import, _) = grandpa::block_import( ++ client.clone(), ++ &(client.clone() as Arc<_>), ++ select_chain.clone(), + )?; +- +- let finality_proof_import = grandpa_block_import.clone(); +- let finality_proof_request_builder = +- finality_proof_import.create_finality_proof_request_builder(); ++ let justification_import = grandpa_block_import.clone(); + + let (babe_block_import, babe_link) = sc_consensus_babe::block_import( + sc_consensus_babe::Config::get_or_compute(&*client)?, +@@ -391,8 +396,7 @@ pub fn new_light_base(config: Configuration) -> Result<( + let import_queue = sc_consensus_babe::import_queue( + babe_link, + babe_block_import, +- None, +- Some(Box::new(finality_proof_import)), ++ Some(Box::new(justification_import)), + client.clone(), + select_chain.clone(), + inherent_data_providers.clone(), +@@ -401,9 +405,6 @@ pub fn new_light_base(config: Configuration) -> Result<( + sp_consensus::NeverCanAuthor, + )?; + +- let finality_proof_provider = +- GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone()); +- + let (network, network_status_sinks, system_rpc_tx, network_starter) = + sc_service::build_network(sc_service::BuildNetworkParams { + config: &config, +@@ -413,8 +414,6 @@ pub fn new_light_base(config: Configuration) -> Result<( + import_queue, + on_demand: Some(on_demand.clone()), + block_announce_validator_builder: None, +- finality_proof_request_builder: Some(finality_proof_request_builder), +- finality_proof_provider: Some(finality_proof_provider), + })?; + network_starter.start_network(); + +@@ -433,32 +432,39 @@ pub fn new_light_base(config: Configuration) -> Result<( + + let rpc_extensions = node_rpc::create_light(light_deps); + +- let rpc_handlers = ++ let (rpc_handlers, telemetry_connection_notifier) = + sc_service::spawn_tasks(sc_service::SpawnTasksParams { + on_demand: Some(on_demand), + remote_blockchain: Some(backend.remote_blockchain()), + rpc_extensions_builder: Box::new(sc_service::NoopRpcExtensionBuilder(rpc_extensions)), + client: client.clone(), + transaction_pool: transaction_pool.clone(), +- config, keystore, backend, network_status_sinks, system_rpc_tx, ++ keystore: keystore_container.sync_keystore(), ++ config, backend, network_status_sinks, system_rpc_tx, + network: network.clone(), +- telemetry_connection_sinks: sc_service::TelemetryConnectionSinks::default(), + task_manager: &mut task_manager, + })?; + +- Ok((task_manager, rpc_handlers, client, network, transaction_pool)) ++ Ok(( ++ task_manager, ++ rpc_handlers, ++ telemetry_connection_notifier, ++ client, ++ network, ++ transaction_pool, ++ )) + } + + /// Builds a new service for a light client. + pub fn new_light(config: Configuration) -> Result { +- new_light_base(config).map(|(task_manager, _, _, _, _)| { ++ new_light_base(config).map(|(task_manager, _, _, _, _, _)| { + task_manager + }) + } + + #[cfg(test)] + mod tests { +- use std::{sync::Arc, borrow::Cow, any::Any}; ++ use std::{sync::Arc, borrow::Cow, any::Any, convert::TryInto}; + use sc_consensus_babe::{CompatibleDigestItem, BabeIntermediate, INTERMEDIATE_KEY}; + use sc_consensus_epochs::descendent_query; + use sp_consensus::{ +@@ -469,20 +475,25 @@ mod tests { + use node_runtime::{BalancesCall, Call, UncheckedExtrinsic, Address}; + use node_runtime::constants::{currency::CENTS, time::SLOT_DURATION}; + use codec::Encode; +- use sp_core::{crypto::Pair as CryptoPair, H256}; ++ use sp_core::{ ++ crypto::Pair as CryptoPair, ++ H256, ++ Public ++ }; ++ use sp_keystore::{SyncCryptoStorePtr, SyncCryptoStore}; + use sp_runtime::{ + generic::{BlockId, Era, Digest, SignedPayload}, + traits::{Block as BlockT, Header as HeaderT}, + traits::Verify, + }; + use sp_timestamp; +- use sp_finality_tracker; + use sp_keyring::AccountKeyring; + use sc_service_test::TestNetNode; + use crate::service::{new_full_base, new_light_base, NewFullBase}; +- use sp_runtime::traits::IdentifyAccount; ++ use sp_runtime::{key_types::BABE, traits::IdentifyAccount, RuntimeAppPublic}; + use sp_transaction_pool::{MaintainedTransactionPool, ChainEvent}; + use sc_client_api::BlockBackend; ++ use sc_keystore::LocalKeystore; + + type AccountPublic = ::Signer; + +@@ -492,15 +503,15 @@ mod tests { + #[ignore] + fn test_sync() { + let keystore_path = tempfile::tempdir().expect("Creates keystore path"); +- let keystore = sc_keystore::Store::open(keystore_path.path(), None) +- .expect("Creates keystore"); +- let alice = keystore.write().insert_ephemeral_from_seed::("//Alice") +- .expect("Creates authority pair"); ++ let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::open(keystore_path.path(), None) ++ .expect("Creates keystore")); ++ let alice: sp_consensus_babe::AuthorityId = SyncCryptoStore::sr25519_generate_new(&*keystore, BABE, Some("//Alice")) ++ .expect("Creates authority pair").into(); + + let chain_spec = crate::chain_spec::tests::integration_test_config_with_single_authority(); + + // For the block factory +- let mut slot_num = 1u64; ++ let mut slot = 1u64; + + // For the extrinsics factory + let bob = Arc::new(AccountKeyring::Bob.pair()); +@@ -528,14 +539,13 @@ mod tests { + Ok((node, (inherent_data_providers, setup_handles.unwrap()))) + }, + |config| { +- let (keep_alive, _, client, network, transaction_pool) = new_light_base(config)?; ++ let (keep_alive, _, _, client, network, transaction_pool) = new_light_base(config)?; + Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool)) + }, + |service, &mut (ref inherent_data_providers, (ref mut block_import, ref babe_link))| { + let mut inherent_data = inherent_data_providers + .create_inherent_data() + .expect("Creates inherent data."); +- inherent_data.replace_data(sp_finality_tracker::INHERENT_IDENTIFIER, &1u64); + + let parent_id = BlockId::number(service.client().chain_info().best_number); + let parent_header = service.client().header(&parent_id).unwrap().unwrap(); +@@ -552,6 +562,7 @@ mod tests { + ); + + let mut proposer_factory = sc_basic_authorship::ProposerFactory::new( ++ service.spawn_handle(), + service.client(), + service.transaction_pool(), + None, +@@ -561,7 +572,7 @@ mod tests { + descendent_query(&*service.client()), + &parent_hash, + parent_number, +- slot_num, ++ slot.into(), + ).unwrap().unwrap(); + + let mut digest = Digest::::default(); +@@ -569,18 +580,18 @@ mod tests { + // even though there's only one authority some slots might be empty, + // so we must keep trying the next slots until we can claim one. + let babe_pre_digest = loop { +- inherent_data.replace_data(sp_timestamp::INHERENT_IDENTIFIER, &(slot_num * SLOT_DURATION)); ++ inherent_data.replace_data(sp_timestamp::INHERENT_IDENTIFIER, &(slot * SLOT_DURATION)); + if let Some(babe_pre_digest) = sc_consensus_babe::test_helpers::claim_slot( +- slot_num, ++ slot.into(), + &parent_header, + &*service.client(), +- &keystore, ++ keystore.clone(), + &babe_link, + ) { + break babe_pre_digest; + } + +- slot_num += 1; ++ slot += 1; + }; + + digest.push(::babe_pre_digest(babe_pre_digest)); +@@ -600,11 +611,18 @@ mod tests { + // sign the pre-sealed hash of the block and then + // add it to a digest item. + let to_sign = pre_hash.encode(); +- let signature = alice.sign(&to_sign[..]); ++ let signature = SyncCryptoStore::sign_with( ++ &*keystore, ++ sp_consensus_babe::AuthorityId::ID, ++ &alice.to_public_crypto_pair(), ++ &to_sign, ++ ).unwrap() ++ .try_into() ++ .unwrap(); + let item = ::babe_seal( +- signature.into(), ++ signature, + ); +- slot_num += 1; ++ slot += 1; + + let mut params = BlockImportParams::new(BlockOrigin::File, new_header); + params.post_digests.push(item); +@@ -679,7 +697,7 @@ mod tests { + Ok(sc_service_test::TestNetComponents::new(task_manager, client, network, transaction_pool)) + }, + |config| { +- let (keep_alive, _, client, network, transaction_pool) = new_light_base(config)?; ++ let (keep_alive, _, _, client, network, transaction_pool) = new_light_base(config)?; + Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool)) + }, + vec![ +``` diff --git a/frame/assets/Cargo.toml b/frame/assets/Cargo.toml index 4dddd1c59cdea..67fa0af3d63be 100644 --- a/frame/assets/Cargo.toml +++ b/frame/assets/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-assets" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,21 +14,21 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } # Needed for various traits. In our case, `OnFinalize`. -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } # Needed for type-safe access to storage DB. -frame-support = { version = "2.0.0", default-features = false, path = "../support" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } # `system` module provides us with all sorts of useful stuff and macros depend on it being around. -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-std = { version = "2.0.0", path = "../../primitives/std" } -sp-io = { version = "2.0.0", path = "../../primitives/io" } -pallet-balances = { version = "2.0.0", path = "../balances" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-std = { version = "3.0.0", path = "../../primitives/std" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/assets/src/benchmarking.rs b/frame/assets/src/benchmarking.rs index 90b6f65b39890..86a0c48e7973a 100644 --- a/frame/assets/src/benchmarking.rs +++ b/frame/assets/src/benchmarking.rs @@ -20,7 +20,8 @@ use super::*; use sp_runtime::traits::Bounded; use frame_system::RawOrigin as SystemOrigin; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; +use frame_support::traits::Get; use crate::Module as Assets; @@ -79,7 +80,7 @@ benchmarks! { T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, 1, 1u32.into()) verify { - assert_last_event::(RawEvent::Created(Default::default(), caller.clone(), caller).into()); + assert_last_event::(Event::Created(Default::default(), caller.clone(), caller).into()); } force_create { @@ -87,7 +88,7 @@ benchmarks! { let caller_lookup = T::Lookup::unlookup(caller.clone()); }: _(SystemOrigin::Root, Default::default(), caller_lookup, 1, 1u32.into()) verify { - assert_last_event::(RawEvent::ForceCreated(Default::default(), caller).into()); + assert_last_event::(Event::ForceCreated(Default::default(), caller).into()); } destroy { @@ -96,7 +97,7 @@ benchmarks! { add_zombies::(caller.clone(), z); }: _(SystemOrigin::Signed(caller), Default::default(), 10_000) verify { - assert_last_event::(RawEvent::Destroyed(Default::default()).into()); + assert_last_event::(Event::Destroyed(Default::default()).into()); } force_destroy { @@ -105,7 +106,7 @@ benchmarks! { add_zombies::(caller.clone(), z); }: _(SystemOrigin::Root, Default::default(), 10_000) verify { - assert_last_event::(RawEvent::Destroyed(Default::default()).into()); + assert_last_event::(Event::Destroyed(Default::default()).into()); } mint { @@ -113,7 +114,7 @@ benchmarks! { let amount = T::Balance::from(100u32); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, amount) verify { - assert_last_event::(RawEvent::Issued(Default::default(), caller, amount).into()); + assert_last_event::(Event::Issued(Default::default(), caller, amount).into()); } burn { @@ -121,7 +122,7 @@ benchmarks! { let (caller, caller_lookup) = create_default_minted_asset::(10, amount); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, amount) verify { - assert_last_event::(RawEvent::Burned(Default::default(), caller, amount).into()); + assert_last_event::(Event::Burned(Default::default(), caller, amount).into()); } transfer { @@ -131,7 +132,7 @@ benchmarks! { let target_lookup = T::Lookup::unlookup(target.clone()); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), target_lookup, amount) verify { - assert_last_event::(RawEvent::Transferred(Default::default(), caller, target, amount).into()); + assert_last_event::(Event::Transferred(Default::default(), caller, target, amount).into()); } force_transfer { @@ -141,14 +142,16 @@ benchmarks! { let target_lookup = T::Lookup::unlookup(target.clone()); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, target_lookup, amount) verify { - assert_last_event::(RawEvent::ForceTransferred(Default::default(), caller, target, amount).into()); + assert_last_event::( + Event::ForceTransferred(Default::default(), caller, target, amount).into() + ); } freeze { let (caller, caller_lookup) = create_default_minted_asset::(10, 100u32.into()); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup) verify { - assert_last_event::(RawEvent::Frozen(Default::default(), caller).into()); + assert_last_event::(Event::Frozen(Default::default(), caller).into()); } thaw { @@ -160,14 +163,14 @@ benchmarks! { )?; }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup) verify { - assert_last_event::(RawEvent::Thawed(Default::default(), caller).into()); + assert_last_event::(Event::Thawed(Default::default(), caller).into()); } freeze_asset { let (caller, caller_lookup) = create_default_minted_asset::(10, 100u32.into()); }: _(SystemOrigin::Signed(caller.clone()), Default::default()) verify { - assert_last_event::(RawEvent::AssetFrozen(Default::default()).into()); + assert_last_event::(Event::AssetFrozen(Default::default()).into()); } thaw_asset { @@ -178,7 +181,7 @@ benchmarks! { )?; }: _(SystemOrigin::Signed(caller.clone()), Default::default()) verify { - assert_last_event::(RawEvent::AssetThawed(Default::default()).into()); + assert_last_event::(Event::AssetThawed(Default::default()).into()); } transfer_ownership { @@ -187,7 +190,7 @@ benchmarks! { let target_lookup = T::Lookup::unlookup(target.clone()); }: _(SystemOrigin::Signed(caller), Default::default(), target_lookup) verify { - assert_last_event::(RawEvent::OwnerChanged(Default::default(), target).into()); + assert_last_event::(Event::OwnerChanged(Default::default(), target).into()); } set_team { @@ -197,7 +200,7 @@ benchmarks! { let target2 = T::Lookup::unlookup(account("target", 2, SEED)); }: _(SystemOrigin::Signed(caller), Default::default(), target0.clone(), target1.clone(), target2.clone()) verify { - assert_last_event::(RawEvent::TeamChanged( + assert_last_event::(Event::TeamChanged( Default::default(), account("target", 0, SEED), account("target", 1, SEED), @@ -211,7 +214,7 @@ benchmarks! { T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); }: _(SystemOrigin::Signed(caller), Default::default(), max_zombies) verify { - assert_last_event::(RawEvent::MaxZombiesChanged(Default::default(), max_zombies).into()); + assert_last_event::(Event::MaxZombiesChanged(Default::default(), max_zombies).into()); } set_metadata { @@ -226,124 +229,8 @@ benchmarks! { T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); }: _(SystemOrigin::Signed(caller), Default::default(), name.clone(), symbol.clone(), decimals) verify { - assert_last_event::(RawEvent::MetadataSet(Default::default(), name, symbol, decimals).into()); + assert_last_event::(Event::MetadataSet(Default::default(), name, symbol, decimals).into()); } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - - #[test] - fn create() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_create::().is_ok()); - }); - } - - #[test] - fn force_create() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_force_create::().is_ok()); - }); - } - - #[test] - fn destroy() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_destroy::().is_ok()); - }); - } - - #[test] - fn force_destroy() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_force_destroy::().is_ok()); - }); - } - - #[test] - fn mint() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_mint::().is_ok()); - }); - } - - #[test] - fn burn() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_burn::().is_ok()); - }); - } - - #[test] - fn transfer() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_transfer::().is_ok()); - }); - } - - #[test] - fn force_transfer() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_force_transfer::().is_ok()); - }); - } - - #[test] - fn freeze() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_freeze::().is_ok()); - }); - } - - #[test] - fn thaw() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_thaw::().is_ok()); - }); - } - - #[test] - fn freeze_asset() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_freeze_asset::().is_ok()); - }); - } - - #[test] - fn thaw_asset() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_thaw_asset::().is_ok()); - }); - } - - #[test] - fn transfer_ownership() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_transfer_ownership::().is_ok()); - }); - } - - #[test] - fn set_team() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_set_team::().is_ok()); - }); - } - - #[test] - fn set_max_zombies() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_set_max_zombies::().is_ok()); - }); - } - - #[test] - fn set_metadata() { - new_test_ext().execute_with(|| { - assert!(test_benchmark_set_metadata::().is_ok()); - }); - } -} +impl_benchmark_test_suite!(Assets, crate::tests::new_test_ext(), crate::tests::Test); diff --git a/frame/assets/src/lib.rs b/frame/assets/src/lib.rs index 099361eceb95c..7b04ea11bafed 100644 --- a/frame/assets/src/lib.rs +++ b/frame/assets/src/lib.rs @@ -29,9 +29,9 @@ //! * Asset Freezing //! * Asset Destruction (Burning) //! -//! To use it in your runtime, you need to implement the assets [`Config`](./trait.Config.html). +//! To use it in your runtime, you need to implement the assets [`Config`]. //! -//! The supported dispatchable functions are documented in the [`Call`](./enum.Call.html) enum. +//! The supported dispatchable functions are documented in the [`Call`] enum. //! //! ### Terminology //! @@ -114,214 +114,81 @@ mod benchmarking; pub mod weights; use sp_std::{fmt::Debug, prelude::*}; -use sp_runtime::{RuntimeDebug, traits::{ - Member, AtLeast32BitUnsigned, Zero, StaticLookup, Saturating, CheckedSub, CheckedAdd -}}; +use sp_runtime::{ + RuntimeDebug, + traits::{ + AtLeast32BitUnsigned, Zero, StaticLookup, Saturating, CheckedSub, CheckedAdd, + } +}; use codec::{Encode, Decode, HasCompact}; -use frame_support::{Parameter, decl_module, decl_event, decl_storage, decl_error, ensure, - traits::{Currency, ReservableCurrency, EnsureOrigin, Get, BalanceStatus::Reserved}, - dispatch::{DispatchResult, DispatchError}, +use frame_support::{ + ensure, + traits::{Currency, ReservableCurrency, BalanceStatus::Reserved}, + dispatch::DispatchError, }; -use frame_system::ensure_signed; pub use weights::WeightInfo; -type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; - -/// The module configuration trait. -pub trait Config: frame_system::Config { - /// The overarching event type. - type Event: From> + Into<::Event>; +pub use pallet::*; - /// The units in which we record balances. - type Balance: Member + Parameter + AtLeast32BitUnsigned + Default + Copy; - - /// The arithmetic type of asset identifier. - type AssetId: Member + Parameter + Default + Copy + HasCompact; +type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; - /// The currency mechanism. - type Currency: ReservableCurrency; +#[frame_support::pallet] +pub mod pallet { + use frame_support::{ + dispatch::DispatchResultWithPostInfo, + pallet_prelude::*, + }; + use frame_system::pallet_prelude::*; + use super::*; - /// The origin which may forcibly create or destroy an asset. - type ForceOrigin: EnsureOrigin; + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); - /// The basic amount of funds that must be reserved when creating a new asset class. - type AssetDepositBase: Get>; + #[pallet::config] + /// The module configuration trait. + pub trait Config: frame_system::Config { + /// The overarching event type. + type Event: From> + IsType<::Event>; - /// The additional funds that must be reserved for every zombie account that an asset class - /// supports. - type AssetDepositPerZombie: Get>; + /// The units in which we record balances. + type Balance: Member + Parameter + AtLeast32BitUnsigned + Default + Copy; - /// The maximum length of a name or symbol stored on-chain. - type StringLimit: Get; + /// The arithmetic type of asset identifier. + type AssetId: Member + Parameter + Default + Copy + HasCompact; - /// The basic amount of funds that must be reserved when adding metadata to your asset. - type MetadataDepositBase: Get>; + /// The currency mechanism. + type Currency: ReservableCurrency; - /// The additional funds that must be reserved for the number of bytes you store in your - /// metadata. - type MetadataDepositPerByte: Get>; + /// The origin which may forcibly create or destroy an asset. + type ForceOrigin: EnsureOrigin; - /// Weight information for extrinsics in this pallet. - type WeightInfo: WeightInfo; -} + /// The basic amount of funds that must be reserved when creating a new asset class. + type AssetDepositBase: Get>; -#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug)] -pub struct AssetDetails< - Balance: Encode + Decode + Clone + Debug + Eq + PartialEq, - AccountId: Encode + Decode + Clone + Debug + Eq + PartialEq, - DepositBalance: Encode + Decode + Clone + Debug + Eq + PartialEq, -> { - /// Can change `owner`, `issuer`, `freezer` and `admin` accounts. - owner: AccountId, - /// Can mint tokens. - issuer: AccountId, - /// Can thaw tokens, force transfers and burn tokens from any account. - admin: AccountId, - /// Can freeze tokens. - freezer: AccountId, - /// The total supply across all accounts. - supply: Balance, - /// The balance deposited for this asset. - /// - /// This pays for the data stored here together with any virtual accounts. - deposit: DepositBalance, - /// The number of balance-holding accounts that this asset may have, excluding those that were - /// created when they had a system-level ED. - max_zombies: u32, - /// The ED for virtual accounts. - min_balance: Balance, - /// The current number of zombie accounts. - zombies: u32, - /// The total number of accounts. - accounts: u32, - /// Whether the asset is frozen for permissionless transfers. - is_frozen: bool, -} + /// The additional funds that must be reserved for every zombie account that an asset class + /// supports. + type AssetDepositPerZombie: Get>; -#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default)] -pub struct AssetBalance< - Balance: Encode + Decode + Clone + Debug + Eq + PartialEq, -> { - /// The balance. - balance: Balance, - /// Whether the account is frozen. - is_frozen: bool, - /// Whether the account is a zombie. If not, then it has a reference. - is_zombie: bool, -} + /// The maximum length of a name or symbol stored on-chain. + type StringLimit: Get; -#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default)] -pub struct AssetMetadata { - /// The balance deposited for this metadata. - /// - /// This pays for the data stored in this struct. - deposit: DepositBalance, - /// The user friendly name of this asset. Limited in length by `StringLimit`. - name: Vec, - /// The ticker symbol for this asset. Limited in length by `StringLimit`. - symbol: Vec, - /// The number of decimals this asset uses to represent one unit. - decimals: u8, -} + /// The basic amount of funds that must be reserved when adding metadata to your asset. + type MetadataDepositBase: Get>; -decl_storage! { - trait Store for Module as Assets { - /// Details of an asset. - Asset: map hasher(blake2_128_concat) T::AssetId => Option, - >>; - - /// The number of units of assets held by any given account. - Account: double_map - hasher(blake2_128_concat) T::AssetId, - hasher(blake2_128_concat) T::AccountId - => AssetBalance; - - /// Metadata of an asset. - Metadata: map hasher(blake2_128_concat) T::AssetId => AssetMetadata>; - } -} + /// The additional funds that must be reserved for the number of bytes you store in your + /// metadata. + type MetadataDepositPerByte: Get>; -decl_event! { - pub enum Event where - ::AccountId, - ::Balance, - ::AssetId, - { - /// Some asset class was created. \[asset_id, creator, owner\] - Created(AssetId, AccountId, AccountId), - /// Some assets were issued. \[asset_id, owner, total_supply\] - Issued(AssetId, AccountId, Balance), - /// Some assets were transferred. \[asset_id, from, to, amount\] - Transferred(AssetId, AccountId, AccountId, Balance), - /// Some assets were destroyed. \[asset_id, owner, balance\] - Burned(AssetId, AccountId, Balance), - /// The management team changed \[asset_id, issuer, admin, freezer\] - TeamChanged(AssetId, AccountId, AccountId, AccountId), - /// The owner changed \[asset_id, owner\] - OwnerChanged(AssetId, AccountId), - /// Some assets was transferred by an admin. \[asset_id, from, to, amount\] - ForceTransferred(AssetId, AccountId, AccountId, Balance), - /// Some account `who` was frozen. \[asset_id, who\] - Frozen(AssetId, AccountId), - /// Some account `who` was thawed. \[asset_id, who\] - Thawed(AssetId, AccountId), - /// Some asset `asset_id` was frozen. \[asset_id\] - AssetFrozen(AssetId), - /// Some asset `asset_id` was thawed. \[asset_id\] - AssetThawed(AssetId), - /// An asset class was destroyed. - Destroyed(AssetId), - /// Some asset class was force-created. \[asset_id, owner\] - ForceCreated(AssetId, AccountId), - /// The maximum amount of zombies allowed has changed. \[asset_id, max_zombies\] - MaxZombiesChanged(AssetId, u32), - /// New metadata has been set for an asset. \[asset_id, name, symbol, decimals\] - MetadataSet(AssetId, Vec, Vec, u8), + /// Weight information for extrinsics in this pallet. + type WeightInfo: WeightInfo; } -} -decl_error! { - pub enum Error for Module { - /// Transfer amount should be non-zero. - AmountZero, - /// Account balance must be greater than or equal to the transfer amount. - BalanceLow, - /// Balance should be non-zero. - BalanceZero, - /// The signing account has no permission to do the operation. - NoPermission, - /// The given asset ID is unknown. - Unknown, - /// The origin account is frozen. - Frozen, - /// The asset ID is already taken. - InUse, - /// Too many zombie accounts in use. - TooManyZombies, - /// Attempt to destroy an asset class when non-zombie, reference-bearing accounts exist. - RefsLeft, - /// Invalid witness data given. - BadWitness, - /// Minimum balance should be non-zero. - MinBalanceZero, - /// A mint operation lead to an overflow. - Overflow, - /// Some internal state is broken. - BadState, - /// Invalid metadata given. - BadMetadata, - } -} - -decl_module! { - pub struct Module for enum Call where origin: T::Origin { - type Error = Error; - - fn deposit_event() = default; + #[pallet::hooks] + impl Hooks> for Pallet {} + #[pallet::call] + impl Pallet { /// Issue a new class of fungible assets from a public origin. /// /// This new asset class has no assets initially. @@ -345,13 +212,14 @@ decl_module! { /// Emits `Created` event when successful. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::create()] - fn create(origin, - #[compact] id: T::AssetId, + #[pallet::weight(T::WeightInfo::create())] + pub(super) fn create( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, admin: ::Source, max_zombies: u32, min_balance: T::Balance, - ) { + ) -> DispatchResultWithPostInfo { let owner = ensure_signed(origin)?; let admin = T::Lookup::lookup(admin)?; @@ -376,7 +244,8 @@ decl_module! { accounts: Zero::zero(), is_frozen: false, }); - Self::deposit_event(RawEvent::Created(id, owner, admin)); + Self::deposit_event(Event::Created(id, owner, admin)); + Ok(().into()) } /// Issue a new class of fungible assets from a privileged origin. @@ -400,13 +269,14 @@ decl_module! { /// Emits `ForceCreated` event when successful. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::force_create()] - fn force_create(origin, - #[compact] id: T::AssetId, + #[pallet::weight(T::WeightInfo::force_create())] + pub(super) fn force_create( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, owner: ::Source, - #[compact] max_zombies: u32, - #[compact] min_balance: T::Balance, - ) { + #[pallet::compact] max_zombies: u32, + #[pallet::compact] min_balance: T::Balance, + ) -> DispatchResultWithPostInfo { T::ForceOrigin::ensure_origin(origin)?; let owner = T::Lookup::lookup(owner)?; @@ -426,7 +296,8 @@ decl_module! { accounts: Zero::zero(), is_frozen: false, }); - Self::deposit_event(RawEvent::ForceCreated(id, owner)); + Self::deposit_event(Event::ForceCreated(id, owner)); + Ok(().into()) } /// Destroy a class of fungible assets owned by the sender. @@ -439,11 +310,12 @@ decl_module! { /// Emits `Destroyed` event when successful. /// /// Weight: `O(z)` where `z` is the number of zombie accounts. - #[weight = T::WeightInfo::destroy(*zombies_witness)] - fn destroy(origin, - #[compact] id: T::AssetId, - #[compact] zombies_witness: u32, - ) -> DispatchResult { + #[pallet::weight(T::WeightInfo::destroy(*zombies_witness))] + pub(super) fn destroy( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, + #[pallet::compact] zombies_witness: u32, + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; Asset::::try_mutate_exists(id, |maybe_details| { @@ -457,8 +329,8 @@ decl_module! { *maybe_details = None; Account::::remove_prefix(&id); - Self::deposit_event(RawEvent::Destroyed(id)); - Ok(()) + Self::deposit_event(Event::Destroyed(id)); + Ok(().into()) }) } @@ -472,11 +344,12 @@ decl_module! { /// Emits `Destroyed` event when successful. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::force_destroy(*zombies_witness)] - fn force_destroy(origin, - #[compact] id: T::AssetId, - #[compact] zombies_witness: u32, - ) -> DispatchResult { + #[pallet::weight(T::WeightInfo::force_destroy(*zombies_witness))] + pub(super) fn force_destroy( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, + #[pallet::compact] zombies_witness: u32, + ) -> DispatchResultWithPostInfo { T::ForceOrigin::ensure_origin(origin)?; Asset::::try_mutate_exists(id, |maybe_details| { @@ -489,8 +362,8 @@ decl_module! { *maybe_details = None; Account::::remove_prefix(&id); - Self::deposit_event(RawEvent::Destroyed(id)); - Ok(()) + Self::deposit_event(Event::Destroyed(id)); + Ok(().into()) }) } @@ -506,12 +379,13 @@ decl_module! { /// /// Weight: `O(1)` /// Modes: Pre-existing balance of `beneficiary`; Account pre-existence of `beneficiary`. - #[weight = T::WeightInfo::mint()] - fn mint(origin, - #[compact] id: T::AssetId, + #[pallet::weight(T::WeightInfo::mint())] + pub(super) fn mint( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, beneficiary: ::Source, - #[compact] amount: T::Balance - ) -> DispatchResult { + #[pallet::compact] amount: T::Balance + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; let beneficiary = T::Lookup::lookup(beneficiary)?; @@ -521,17 +395,17 @@ decl_module! { ensure!(&origin == &details.issuer, Error::::NoPermission); details.supply = details.supply.checked_add(&amount).ok_or(Error::::Overflow)?; - Account::::try_mutate(id, &beneficiary, |t| -> DispatchResult { + Account::::try_mutate(id, &beneficiary, |t| -> DispatchResultWithPostInfo { let new_balance = t.balance.saturating_add(amount); ensure!(new_balance >= details.min_balance, Error::::BalanceLow); if t.balance.is_zero() { t.is_zombie = Self::new_account(&beneficiary, details)?; } t.balance = new_balance; - Ok(()) + Ok(().into()) })?; - Self::deposit_event(RawEvent::Issued(id, beneficiary, amount)); - Ok(()) + Self::deposit_event(Event::Issued(id, beneficiary, amount)); + Ok(().into()) }) } @@ -550,12 +424,13 @@ decl_module! { /// /// Weight: `O(1)` /// Modes: Post-existence of `who`; Pre & post Zombie-status of `who`. - #[weight = T::WeightInfo::burn()] - fn burn(origin, - #[compact] id: T::AssetId, + #[pallet::weight(T::WeightInfo::burn())] + pub(super) fn burn( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, who: ::Source, - #[compact] amount: T::Balance - ) -> DispatchResult { + #[pallet::compact] amount: T::Balance + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; let who = T::Lookup::lookup(who)?; @@ -583,8 +458,8 @@ decl_module! { d.supply = d.supply.saturating_sub(burned); - Self::deposit_event(RawEvent::Burned(id, who, burned)); - Ok(()) + Self::deposit_event(Event::Burned(id, who, burned)); + Ok(().into()) }) } @@ -606,12 +481,13 @@ decl_module! { /// Weight: `O(1)` /// Modes: Pre-existence of `target`; Post-existence of sender; Prior & post zombie-status /// of sender; Account pre-existence of `target`. - #[weight = T::WeightInfo::transfer()] - fn transfer(origin, - #[compact] id: T::AssetId, + #[pallet::weight(T::WeightInfo::transfer())] + pub(super) fn transfer( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, target: ::Source, - #[compact] amount: T::Balance - ) -> DispatchResult { + #[pallet::compact] amount: T::Balance + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; ensure!(!amount.is_zero(), Error::::AmountZero); @@ -626,7 +502,7 @@ decl_module! { ensure!(!details.is_frozen, Error::::Frozen); if dest == origin { - return Ok(()) + return Ok(().into()) } let mut amount = amount; @@ -635,14 +511,14 @@ decl_module! { origin_account.balance = Zero::zero(); } - Account::::try_mutate(id, &dest, |a| -> DispatchResult { + Account::::try_mutate(id, &dest, |a| -> DispatchResultWithPostInfo { let new_balance = a.balance.saturating_add(amount); ensure!(new_balance >= details.min_balance, Error::::BalanceLow); if a.balance.is_zero() { a.is_zombie = Self::new_account(&dest, details)?; } a.balance = new_balance; - Ok(()) + Ok(().into()) })?; match origin_account.balance.is_zero() { @@ -656,8 +532,8 @@ decl_module! { } } - Self::deposit_event(RawEvent::Transferred(id, origin, dest, amount)); - Ok(()) + Self::deposit_event(Event::Transferred(id, origin, dest, amount)); + Ok(().into()) }) } @@ -680,13 +556,14 @@ decl_module! { /// Weight: `O(1)` /// Modes: Pre-existence of `dest`; Post-existence of `source`; Prior & post zombie-status /// of `source`; Account pre-existence of `dest`. - #[weight = T::WeightInfo::force_transfer()] - fn force_transfer(origin, - #[compact] id: T::AssetId, + #[pallet::weight(T::WeightInfo::force_transfer())] + pub(super) fn force_transfer( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, source: ::Source, dest: ::Source, - #[compact] amount: T::Balance, - ) -> DispatchResult { + #[pallet::compact] amount: T::Balance, + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; let source = T::Lookup::lookup(source)?; @@ -696,7 +573,7 @@ decl_module! { let dest = T::Lookup::lookup(dest)?; if dest == source { - return Ok(()) + return Ok(().into()) } Asset::::try_mutate(id, |maybe_details| { @@ -709,14 +586,14 @@ decl_module! { source_account.balance = Zero::zero(); } - Account::::try_mutate(id, &dest, |a| -> DispatchResult { + Account::::try_mutate(id, &dest, |a| -> DispatchResultWithPostInfo { let new_balance = a.balance.saturating_add(amount); ensure!(new_balance >= details.min_balance, Error::::BalanceLow); if a.balance.is_zero() { a.is_zombie = Self::new_account(&dest, details)?; } a.balance = new_balance; - Ok(()) + Ok(().into()) })?; match source_account.balance.is_zero() { @@ -730,8 +607,8 @@ decl_module! { } } - Self::deposit_event(RawEvent::ForceTransferred(id, source, dest, amount)); - Ok(()) + Self::deposit_event(Event::ForceTransferred(id, source, dest, amount)); + Ok(().into()) }) } @@ -745,8 +622,12 @@ decl_module! { /// Emits `Frozen`. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::freeze()] - fn freeze(origin, #[compact] id: T::AssetId, who: ::Source) { + #[pallet::weight(T::WeightInfo::freeze())] + pub(super) fn freeze( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, + who: ::Source + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; let d = Asset::::get(id).ok_or(Error::::Unknown)?; @@ -757,6 +638,7 @@ decl_module! { Account::::mutate(id, &who, |a| a.is_frozen = true); Self::deposit_event(Event::::Frozen(id, who)); + Ok(().into()) } /// Allow unprivileged transfers from an account again. @@ -769,8 +651,13 @@ decl_module! { /// Emits `Thawed`. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::thaw()] - fn thaw(origin, #[compact] id: T::AssetId, who: ::Source) { + #[pallet::weight(T::WeightInfo::thaw())] + pub(super) fn thaw( + origin: OriginFor, + #[pallet::compact] + id: T::AssetId, + who: ::Source + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; let details = Asset::::get(id).ok_or(Error::::Unknown)?; @@ -781,6 +668,7 @@ decl_module! { Account::::mutate(id, &who, |a| a.is_frozen = false); Self::deposit_event(Event::::Thawed(id, who)); + Ok(().into()) } /// Disallow further unprivileged transfers for the asset class. @@ -792,8 +680,11 @@ decl_module! { /// Emits `Frozen`. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::freeze_asset()] - fn freeze_asset(origin, #[compact] id: T::AssetId) -> DispatchResult { + #[pallet::weight(T::WeightInfo::freeze_asset())] + pub(super) fn freeze_asset( + origin: OriginFor, + #[pallet::compact] id: T::AssetId + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; Asset::::try_mutate(id, |maybe_details| { @@ -803,7 +694,7 @@ decl_module! { d.is_frozen = true; Self::deposit_event(Event::::AssetFrozen(id)); - Ok(()) + Ok(().into()) }) } @@ -816,8 +707,11 @@ decl_module! { /// Emits `Thawed`. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::thaw_asset()] - fn thaw_asset(origin, #[compact] id: T::AssetId) -> DispatchResult { + #[pallet::weight(T::WeightInfo::thaw_asset())] + pub(super) fn thaw_asset( + origin: OriginFor, + #[pallet::compact] id: T::AssetId + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; Asset::::try_mutate(id, |maybe_details| { @@ -827,7 +721,7 @@ decl_module! { d.is_frozen = false; Self::deposit_event(Event::::AssetThawed(id)); - Ok(()) + Ok(().into()) }) } @@ -841,26 +735,27 @@ decl_module! { /// Emits `OwnerChanged`. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::transfer_ownership()] - fn transfer_ownership(origin, - #[compact] id: T::AssetId, + #[pallet::weight(T::WeightInfo::transfer_ownership())] + pub(super) fn transfer_ownership( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, owner: ::Source, - ) -> DispatchResult { + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; let owner = T::Lookup::lookup(owner)?; Asset::::try_mutate(id, |maybe_details| { let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; ensure!(&origin == &details.owner, Error::::NoPermission); - if details.owner == owner { return Ok(()) } + if details.owner == owner { return Ok(().into()) } // Move the deposit to the new owner. T::Currency::repatriate_reserved(&details.owner, &owner, details.deposit, Reserved)?; details.owner = owner.clone(); - Self::deposit_event(RawEvent::OwnerChanged(id, owner)); - Ok(()) + Self::deposit_event(Event::OwnerChanged(id, owner)); + Ok(().into()) }) } @@ -876,13 +771,14 @@ decl_module! { /// Emits `TeamChanged`. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::set_team()] - fn set_team(origin, - #[compact] id: T::AssetId, + #[pallet::weight(T::WeightInfo::set_team())] + pub(super) fn set_team( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, issuer: ::Source, admin: ::Source, freezer: ::Source, - ) -> DispatchResult { + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; let issuer = T::Lookup::lookup(issuer)?; let admin = T::Lookup::lookup(admin)?; @@ -896,8 +792,8 @@ decl_module! { details.admin = admin.clone(); details.freezer = freezer.clone(); - Self::deposit_event(RawEvent::TeamChanged(id, issuer, admin, freezer)); - Ok(()) + Self::deposit_event(Event::TeamChanged(id, issuer, admin, freezer)); + Ok(().into()) }) } @@ -915,11 +811,12 @@ decl_module! { /// Emits `MaxZombiesChanged`. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::set_max_zombies()] - fn set_max_zombies(origin, - #[compact] id: T::AssetId, - #[compact] max_zombies: u32, - ) -> DispatchResult { + #[pallet::weight(T::WeightInfo::set_max_zombies())] + pub(super) fn set_max_zombies( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, + #[pallet::compact] max_zombies: u32, + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; Asset::::try_mutate(id, |maybe_details| { @@ -939,8 +836,8 @@ decl_module! { details.max_zombies = max_zombies; - Self::deposit_event(RawEvent::MaxZombiesChanged(id, max_zombies)); - Ok(()) + Self::deposit_event(Event::MaxZombiesChanged(id, max_zombies)); + Ok(().into()) }) } @@ -964,13 +861,14 @@ decl_module! { /// Emits `MaxZombiesChanged`. /// /// Weight: `O(1)` - #[weight = T::WeightInfo::set_metadata(name.len() as u32, symbol.len() as u32)] - fn set_metadata(origin, - #[compact] id: T::AssetId, + #[pallet::weight(T::WeightInfo::set_metadata(name.len() as u32, symbol.len() as u32))] + pub(super) fn set_metadata( + origin: OriginFor, + #[pallet::compact] id: T::AssetId, name: Vec, symbol: Vec, decimals: u8, - ) -> DispatchResult { + ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; ensure!(name.len() <= T::StringLimit::get() as usize, Error::::BadMetadata); @@ -1009,15 +907,175 @@ decl_module! { }) } - Self::deposit_event(RawEvent::MetadataSet(id, name, symbol, decimals)); - Ok(()) + Self::deposit_event(Event::MetadataSet(id, name, symbol, decimals)); + Ok(().into()) }) } + + } + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + #[pallet::metadata(T::AccountId = "AccountId", T::Balance = "Balance", T::AssetId = "AssetId")] + pub enum Event { + /// Some asset class was created. \[asset_id, creator, owner\] + Created(T::AssetId, T::AccountId, T::AccountId), + /// Some assets were issued. \[asset_id, owner, total_supply\] + Issued(T::AssetId, T::AccountId, T::Balance), + /// Some assets were transferred. \[asset_id, from, to, amount\] + Transferred(T::AssetId, T::AccountId, T::AccountId, T::Balance), + /// Some assets were destroyed. \[asset_id, owner, balance\] + Burned(T::AssetId, T::AccountId, T::Balance), + /// The management team changed \[asset_id, issuer, admin, freezer\] + TeamChanged(T::AssetId, T::AccountId, T::AccountId, T::AccountId), + /// The owner changed \[asset_id, owner\] + OwnerChanged(T::AssetId, T::AccountId), + /// Some assets was transferred by an admin. \[asset_id, from, to, amount\] + ForceTransferred(T::AssetId, T::AccountId, T::AccountId, T::Balance), + /// Some account `who` was frozen. \[asset_id, who\] + Frozen(T::AssetId, T::AccountId), + /// Some account `who` was thawed. \[asset_id, who\] + Thawed(T::AssetId, T::AccountId), + /// Some asset `asset_id` was frozen. \[asset_id\] + AssetFrozen(T::AssetId), + /// Some asset `asset_id` was thawed. \[asset_id\] + AssetThawed(T::AssetId), + /// An asset class was destroyed. + Destroyed(T::AssetId), + /// Some asset class was force-created. \[asset_id, owner\] + ForceCreated(T::AssetId, T::AccountId), + /// The maximum amount of zombies allowed has changed. \[asset_id, max_zombies\] + MaxZombiesChanged(T::AssetId, u32), + /// New metadata has been set for an asset. \[asset_id, name, symbol, decimals\] + MetadataSet(T::AssetId, Vec, Vec, u8), + } + + #[deprecated(note = "use `Event` instead")] + pub type RawEvent = Event; + + #[pallet::error] + pub enum Error { + /// Transfer amount should be non-zero. + AmountZero, + /// Account balance must be greater than or equal to the transfer amount. + BalanceLow, + /// Balance should be non-zero. + BalanceZero, + /// The signing account has no permission to do the operation. + NoPermission, + /// The given asset ID is unknown. + Unknown, + /// The origin account is frozen. + Frozen, + /// The asset ID is already taken. + InUse, + /// Too many zombie accounts in use. + TooManyZombies, + /// Attempt to destroy an asset class when non-zombie, reference-bearing accounts exist. + RefsLeft, + /// Invalid witness data given. + BadWitness, + /// Minimum balance should be non-zero. + MinBalanceZero, + /// A mint operation lead to an overflow. + Overflow, + /// Some internal state is broken. + BadState, + /// Invalid metadata given. + BadMetadata, } + + #[pallet::storage] + /// Details of an asset. + pub(super) type Asset = StorageMap< + _, + Blake2_128Concat, + T::AssetId, + AssetDetails> + >; + #[pallet::storage] + /// The number of units of assets held by any given account. + pub(super) type Account = StorageDoubleMap< + _, + Blake2_128Concat, + T::AssetId, + Blake2_128Concat, + T::AccountId, + AssetBalance, + ValueQuery + >; + #[pallet::storage] + /// Metadata of an asset. + pub(super) type Metadata = StorageMap< + _, + Blake2_128Concat, + T::AssetId, + AssetMetadata>, + ValueQuery + >; +} + +#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug)] +pub struct AssetDetails< + Balance: Encode + Decode + Clone + Debug + Eq + PartialEq, + AccountId: Encode + Decode + Clone + Debug + Eq + PartialEq, + DepositBalance: Encode + Decode + Clone + Debug + Eq + PartialEq, +> { + /// Can change `owner`, `issuer`, `freezer` and `admin` accounts. + owner: AccountId, + /// Can mint tokens. + issuer: AccountId, + /// Can thaw tokens, force transfers and burn tokens from any account. + admin: AccountId, + /// Can freeze tokens. + freezer: AccountId, + /// The total supply across all accounts. + supply: Balance, + /// The balance deposited for this asset. + /// + /// This pays for the data stored here together with any virtual accounts. + deposit: DepositBalance, + /// The number of balance-holding accounts that this asset may have, excluding those that were + /// created when they had a system-level ED. + max_zombies: u32, + /// The ED for virtual accounts. + min_balance: Balance, + /// The current number of zombie accounts. + zombies: u32, + /// The total number of accounts. + accounts: u32, + /// Whether the asset is frozen for permissionless transfers. + is_frozen: bool, +} + +#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default)] +pub struct AssetBalance< + Balance: Encode + Decode + Clone + Debug + Eq + PartialEq, +> { + /// The balance. + balance: Balance, + /// Whether the account is frozen. + is_frozen: bool, + /// Whether the account is a zombie. If not, then it has a reference. + is_zombie: bool, +} + +#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default)] +pub struct AssetMetadata { + /// The balance deposited for this metadata. + /// + /// This pays for the data stored in this struct. + deposit: DepositBalance, + /// The user friendly name of this asset. Limited in length by `StringLimit`. + name: Vec, + /// The ticker symbol for this asset. Limited in length by `StringLimit`. + symbol: Vec, + /// The number of decimals this asset uses to represent one unit. + decimals: u8, } // The main implementation block for the module. -impl Module { +impl Pallet { // Public immutables /// Get the asset `id` balance of `who`. diff --git a/frame/atomic-swap/Cargo.toml b/frame/atomic-swap/Cargo.toml index 55d8de86582ac..99ce41f39939a 100644 --- a/frame/atomic-swap/Cargo.toml +++ b/frame/atomic-swap/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-atomic-swap" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,16 +14,16 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } [dev-dependencies] -pallet-balances = { version = "2.0.0", path = "../balances" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/aura/Cargo.toml b/frame/aura/Cargo.toml index cc8e678fb559b..80ea164cf0f5f 100644 --- a/frame/aura/Cargo.toml +++ b/frame/aura/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-aura" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,23 +13,21 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../primitives/application-crypto" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-inherents = { version = "2.0.0", default-features = false, path = "../../primitives/inherents" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../primitives/application-crypto" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } serde = { version = "1.0.101", optional = true } -pallet-session = { version = "2.0.0", default-features = false, path = "../session" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -sp-consensus-aura = { version = "0.8.0", path = "../../primitives/consensus/aura", default-features = false } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-timestamp = { version = "2.0.0", default-features = false, path = "../../primitives/timestamp" } -pallet-timestamp = { version = "2.0.0", default-features = false, path = "../timestamp" } - +pallet-session = { version = "3.0.0", default-features = false, path = "../session" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +sp-consensus-aura = { version = "0.9.0", path = "../../primitives/consensus/aura", default-features = false } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-timestamp = { version = "3.0.0", default-features = false, path = "../../primitives/timestamp" } +pallet-timestamp = { version = "3.0.0", default-features = false, path = "../timestamp" } [dev-dependencies] -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-io ={ version = "2.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-io ={ version = "3.0.0", path = "../../primitives/io" } lazy_static = "1.4.0" parking_lot = "0.11.1" @@ -38,7 +36,6 @@ default = ["std"] std = [ "sp-application-crypto/std", "codec/std", - "sp-inherents/std", "sp-std/std", "serde", "sp-runtime/std", diff --git a/frame/aura/src/lib.rs b/frame/aura/src/lib.rs index 42a40cc40d9c4..db639a4499beb 100644 --- a/frame/aura/src/lib.rs +++ b/frame/aura/src/lib.rs @@ -34,62 +34,94 @@ //! //! - [Timestamp](../pallet_timestamp/index.html): The Timestamp module is used in Aura to track //! consensus rounds (via `slots`). -//! -//! ## References -//! -//! If you're interested in hacking on this module, it is useful to understand the interaction with -//! `substrate/primitives/inherents/src/lib.rs` and, specifically, the required implementation of -//! [`ProvideInherent`](../sp_inherents/trait.ProvideInherent.html) and -//! [`ProvideInherentData`](../sp_inherents/trait.ProvideInherentData.html) to create and check inherents. #![cfg_attr(not(feature = "std"), no_std)] -use pallet_timestamp; - -use sp_std::{result, prelude::*}; +use sp_std::prelude::*; use codec::{Encode, Decode}; -use frame_support::{ - decl_storage, decl_module, Parameter, traits::{Get, FindAuthor}, - ConsensusEngineId, -}; +use frame_support::{Parameter, traits::{Get, FindAuthor, OneSessionHandler}, ConsensusEngineId}; use sp_runtime::{ RuntimeAppPublic, traits::{SaturatedConversion, Saturating, Zero, Member, IsMember}, generic::DigestItem, }; use sp_timestamp::OnTimestampSet; -use sp_inherents::{InherentIdentifier, InherentData, ProvideInherent, MakeFatalError}; -use sp_consensus_aura::{ - AURA_ENGINE_ID, ConsensusLog, AuthorityIndex, - inherents::{INHERENT_IDENTIFIER, AuraInherentData}, -}; +use sp_consensus_aura::{AURA_ENGINE_ID, ConsensusLog, AuthorityIndex, Slot}; mod mock; mod tests; +pub mod migrations; -pub trait Config: pallet_timestamp::Config { - /// The identifier type for an authority. - type AuthorityId: Member + Parameter + RuntimeAppPublic + Default; -} +pub use pallet::*; -decl_storage! { - trait Store for Module as Aura { - /// The last timestamp. - LastTimestamp get(fn last): T::Moment; +#[frame_support::pallet] +pub mod pallet { + use super::*; + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; - /// The current authorities - pub Authorities get(fn authorities): Vec; + #[pallet::config] + pub trait Config: pallet_timestamp::Config + frame_system::Config { + /// The identifier type for an authority. + type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + MaybeSerializeDeserialize; } - add_extra_genesis { - config(authorities): Vec; - build(|config| Module::::initialize_authorities(&config.authorities)) + + #[pallet::pallet] + pub struct Pallet(sp_std::marker::PhantomData); + + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_initialize(_: T::BlockNumber) -> Weight { + if let Some(new_slot) = Self::current_slot_from_digests() { + let current_slot = CurrentSlot::::get(); + + assert!(current_slot < new_slot, "Slot must increase"); + CurrentSlot::::put(new_slot); + + // TODO [#3398] Generate offence report for all authorities that skipped their slots. + + T::DbWeight::get().reads_writes(2, 1) + } else { + T::DbWeight::get().reads(1) + } + } } -} -decl_module! { - pub struct Module for enum Call where origin: T::Origin { } + #[pallet::call] + impl Pallet {} + + /// The current authority set. + #[pallet::storage] + #[pallet::getter(fn authorities)] + pub(super) type Authorities = StorageValue<_, Vec, ValueQuery>; + + /// The current slot of this block. + /// + /// This will be set in `on_initialize`. + #[pallet::storage] + #[pallet::getter(fn current_slot)] + pub(super) type CurrentSlot = StorageValue<_, Slot, ValueQuery>; + + #[pallet::genesis_config] + pub struct GenesisConfig { + pub authorities: Vec, + } + + #[cfg(feature = "std")] + impl Default for GenesisConfig { + fn default() -> Self { + Self { authorities: Vec::new() } + } + } + + #[pallet::genesis_build] + impl GenesisBuild for GenesisConfig { + fn build(&self) { + Pallet::::initialize_authorities(&self.authorities); + } + } } -impl Module { +impl Pallet { fn change_authorities(new: Vec) { >::put(&new); @@ -106,13 +138,33 @@ impl Module { >::put(authorities); } } + + /// Get the current slot from the pre-runtime digests. + fn current_slot_from_digests() -> Option { + let digest = frame_system::Pallet::::digest(); + let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime()); + for (id, mut data) in pre_runtime_digests { + if id == AURA_ENGINE_ID { + return Slot::decode(&mut data).ok(); + } + } + + None + } + + /// Determine the Aura slot-duration based on the Timestamp module configuration. + pub fn slot_duration() -> T::Moment { + // we double the minimum block-period so each author can always propose within + // the majority of its slot. + ::MinimumPeriod::get().saturating_mul(2u32.into()) + } } -impl sp_runtime::BoundToRuntimeAppPublic for Module { +impl sp_runtime::BoundToRuntimeAppPublic for Pallet { type Public = T::AuthorityId; } -impl pallet_session::OneSessionHandler for Module { +impl OneSessionHandler for Pallet { type Key = T::AuthorityId; fn on_genesis_session<'a, I: 'a>(validators: I) @@ -128,7 +180,7 @@ impl pallet_session::OneSessionHandler for Module { // instant changes if changed { let next_authorities = validators.map(|(_, k)| k).collect::>(); - let last_authorities = >::authorities(); + let last_authorities = Self::authorities(); if next_authorities != last_authorities { Self::change_authorities(next_authorities); } @@ -145,16 +197,15 @@ impl pallet_session::OneSessionHandler for Module { } } -impl FindAuthor for Module { +impl FindAuthor for Pallet { fn find_author<'a, I>(digests: I) -> Option where I: 'a + IntoIterator { for (id, mut data) in digests.into_iter() { if id == AURA_ENGINE_ID { - if let Ok(slot_num) = u64::decode(&mut data) { - let author_index = slot_num % Self::authorities().len() as u64; - return Some(author_index as u32) - } + let slot = Slot::decode(&mut data).ok()?; + let author_index = *slot % Self::authorities().len() as u64; + return Some(author_index as u32) } } @@ -175,15 +226,15 @@ impl> FindAuthor { let i = Inner::find_author(digests)?; - let validators = >::authorities(); + let validators = >::authorities(); validators.get(i as usize).map(|k| k.clone()) } } /// Find the authority ID of the Aura authority who authored the current block. -pub type AuraAuthorId = FindAccountFromAuthorIndex>; +pub type AuraAuthorId = FindAccountFromAuthorIndex>; -impl IsMember for Module { +impl IsMember for Pallet { fn is_member(authority_id: &T::AuthorityId) -> bool { Self::authorities() .iter() @@ -191,63 +242,14 @@ impl IsMember for Module { } } -impl Module { - /// Determine the Aura slot-duration based on the Timestamp module configuration. - pub fn slot_duration() -> T::Moment { - // we double the minimum block-period so each author can always propose within - // the majority of its slot. - ::MinimumPeriod::get().saturating_mul(2u32.into()) - } - - fn on_timestamp_set(now: T::Moment, slot_duration: T::Moment) { - let last = Self::last(); - ::LastTimestamp::put(now); - - if last.is_zero() { - return; - } - - assert!(!slot_duration.is_zero(), "Aura slot duration cannot be zero."); - - let last_slot = last / slot_duration; - let cur_slot = now / slot_duration; - - assert!(last_slot < cur_slot, "Only one block may be authored per slot."); - - // TODO [#3398] Generate offence report for all authorities that skipped their slots. - } -} - -impl OnTimestampSet for Module { +impl OnTimestampSet for Pallet { fn on_timestamp_set(moment: T::Moment) { - Self::on_timestamp_set(moment, Self::slot_duration()) - } -} - -impl ProvideInherent for Module { - type Call = pallet_timestamp::Call; - type Error = MakeFatalError; - const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER; - - fn create_inherent(_: &InherentData) -> Option { - None - } - - /// Verify the validity of the inherent using the timestamp. - fn check_inherent(call: &Self::Call, data: &InherentData) -> result::Result<(), Self::Error> { - let timestamp = match call { - pallet_timestamp::Call::set(ref timestamp) => timestamp.clone(), - _ => return Ok(()), - }; - - let timestamp_based_slot = timestamp / Self::slot_duration(); + let slot_duration = Self::slot_duration(); + assert!(!slot_duration.is_zero(), "Aura slot duration cannot be zero."); - let seal_slot = data.aura_inherent_data()?.saturated_into(); + let timestamp_slot = moment / slot_duration; + let timestamp_slot = Slot::from(timestamp_slot.saturated_into::()); - if timestamp_based_slot == seal_slot { - Ok(()) - } else { - Err(sp_inherents::Error::from("timestamp set in block doesn't match slot in seal").into()) - } + assert!(CurrentSlot::::get() == timestamp_slot, "Timestamp slot must match `CurrentSlot`"); } } diff --git a/frame/aura/src/migrations.rs b/frame/aura/src/migrations.rs new file mode 100644 index 0000000000000..038c5b3f3f18b --- /dev/null +++ b/frame/aura/src/migrations.rs @@ -0,0 +1,43 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Migrations for the AURA pallet. + +use frame_support::{traits::Get, weights::Weight, pallet_prelude::*}; + +struct __LastTimestamp(sp_std::marker::PhantomData); +impl frame_support::traits::StorageInstance for __LastTimestamp { + fn pallet_prefix() -> &'static str { T::PalletPrefix::get() } + const STORAGE_PREFIX: &'static str = "LastTimestamp"; +} + +type LastTimestamp = StorageValue<__LastTimestamp, (), ValueQuery>; + +pub trait RemoveLastTimestamp: super::Config { + type PalletPrefix: Get<&'static str>; +} + +/// Remove the `LastTimestamp` storage value. +/// +/// This storage value was removed and replaced by `CurrentSlot`. As we only remove this storage +/// value, it is safe to call this method multiple times. +/// +/// This migration requires a type `T` that implements [`RemoveLastTimestamp`]. +pub fn remove_last_timestamp() -> Weight { + LastTimestamp::::kill(); + T::DbWeight::get().writes(1) +} diff --git a/frame/aura/src/mock.rs b/frame/aura/src/mock.rs index c7c439393de9b..a5ef12f5935f1 100644 --- a/frame/aura/src/mock.rs +++ b/frame/aura/src/mock.rs @@ -21,12 +21,8 @@ use crate as pallet_aura; use sp_consensus_aura::ed25519::AuthorityId; -use sp_runtime::{ - traits::IdentityLookup, - testing::{Header, UintAuthorityId}, -}; -use frame_support::parameter_types; -use sp_io; +use sp_runtime::{traits::IdentityLookup, testing::{Header, UintAuthorityId}}; +use frame_support::{parameter_types, traits::GenesisBuild}; use sp_core::H256; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; @@ -40,7 +36,7 @@ frame_support::construct_runtime!( { System: frame_system::{Module, Call, Config, Storage, Event}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, - Aura: pallet_aura::{Module, Call, Storage, Config, Inherent}, + Aura: pallet_aura::{Module, Call, Storage, Config}, } ); diff --git a/frame/aura/src/tests.rs b/frame/aura/src/tests.rs index b198308282c48..18e14e802bd32 100644 --- a/frame/aura/src/tests.rs +++ b/frame/aura/src/tests.rs @@ -24,7 +24,7 @@ use crate::mock::{Aura, new_test_ext}; #[test] fn initial_values() { new_test_ext(vec![0, 1, 2, 3]).execute_with(|| { - assert_eq!(Aura::last(), 0u64); + assert_eq!(Aura::current_slot(), 0u64); assert_eq!(Aura::authorities().len(), 4); }); } diff --git a/frame/authority-discovery/Cargo.toml b/frame/authority-discovery/Cargo.toml index 3538d8a5f81ca..43a09b01fd45d 100644 --- a/frame/authority-discovery/Cargo.toml +++ b/frame/authority-discovery/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-authority-discovery" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,20 +13,20 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-authority-discovery = { version = "2.0.0", default-features = false, path = "../../primitives/authority-discovery" } -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../primitives/application-crypto" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +sp-authority-discovery = { version = "3.0.0", default-features = false, path = "../../primitives/authority-discovery" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../primitives/application-crypto" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } serde = { version = "1.0.101", optional = true } -pallet-session = { version = "2.0.0", features = ["historical" ], path = "../session", default-features = false } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +pallet-session = { version = "3.0.0", features = ["historical" ], path = "../session", default-features = false } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-io = { version = "2.0.0", path = "../../primitives/io" } -sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } +sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } [features] default = ["std"] diff --git a/frame/authority-discovery/src/lib.rs b/frame/authority-discovery/src/lib.rs index 219219b9957bd..cc3f41f59ed89 100644 --- a/frame/authority-discovery/src/lib.rs +++ b/frame/authority-discovery/src/lib.rs @@ -24,7 +24,7 @@ #![cfg_attr(not(feature = "std"), no_std)] use sp_std::prelude::*; -use frame_support::{decl_module, decl_storage}; +use frame_support::{decl_module, decl_storage, traits::OneSessionHandler}; use sp_authority_discovery::AuthorityId; /// The module's config trait. @@ -85,7 +85,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = AuthorityId; } -impl pallet_session::OneSessionHandler for Module { +impl OneSessionHandler for Module { type Key = AuthorityId; fn on_genesis_session<'a, I: 'a>(authorities: I) @@ -265,7 +265,7 @@ mod tests { let mut externalities = TestExternalities::new(t); externalities.execute_with(|| { - use pallet_session::OneSessionHandler; + use frame_support::traits::OneSessionHandler; AuthorityDiscovery::on_genesis_session( first_authorities.iter().map(|id| (id, id.clone())) diff --git a/frame/authorship/Cargo.toml b/frame/authorship/Cargo.toml index 64e3fb12b0d4e..ab48fbec8f50a 100644 --- a/frame/authorship/Cargo.toml +++ b/frame/authorship/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-authorship" -version = "2.0.1" +version = "3.0.0" description = "Block and Uncle Author tracking for the FRAME" authors = ["Parity Technologies "] edition = "2018" @@ -13,18 +13,18 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-inherents = { version = "2.0.0", default-features = false, path = "../../primitives/inherents" } -sp-authorship = { version = "2.0.0", default-features = false, path = "../../primitives/authorship" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -impl-trait-for-tuples = "0.2.0" +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-inherents = { version = "3.0.0", default-features = false, path = "../../primitives/inherents" } +sp-authorship = { version = "3.0.0", default-features = false, path = "../../primitives/authorship" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +impl-trait-for-tuples = "0.2.1" [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-io ={ version = "2.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-io ={ version = "3.0.0", path = "../../primitives/io" } serde = { version = "1.0.101" } [features] diff --git a/frame/babe/Cargo.toml b/frame/babe/Cargo.toml index 13ac2e4034c9f..f0d9021426351 100644 --- a/frame/babe/Cargo.toml +++ b/frame/babe/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-babe" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,32 +13,31 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-authorship = { version = "2.0.0", default-features = false, path = "../authorship" } -pallet-session = { version = "2.0.0", default-features = false, path = "../session" } -pallet-timestamp = { version = "2.0.0", default-features = false, path = "../timestamp" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +pallet-authorship = { version = "3.0.0", default-features = false, path = "../authorship" } +pallet-session = { version = "3.0.0", default-features = false, path = "../session" } +pallet-timestamp = { version = "3.0.0", default-features = false, path = "../timestamp" } serde = { version = "1.0.101", optional = true } -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../primitives/application-crypto" } -sp-consensus-babe = { version = "0.8.0", default-features = false, path = "../../primitives/consensus/babe" } -sp-consensus-vrf = { version = "0.8.0", default-features = false, path = "../../primitives/consensus/vrf" } -sp-inherents = { version = "2.0.0", default-features = false, path = "../../primitives/inherents" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } -sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-timestamp = { version = "2.0.0", default-features = false, path = "../../primitives/timestamp" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../primitives/application-crypto" } +sp-consensus-babe = { version = "0.9.0", default-features = false, path = "../../primitives/consensus/babe" } +sp-consensus-vrf = { version = "0.9.0", default-features = false, path = "../../primitives/consensus/vrf" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-session = { version = "3.0.0", default-features = false, path = "../../primitives/session" } +sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-timestamp = { version = "3.0.0", default-features = false, path = "../../primitives/timestamp" } [dev-dependencies] -frame-benchmarking = { version = "2.0.0", path = "../benchmarking" } -pallet-balances = { version = "2.0.0", path = "../balances" } -pallet-offences = { version = "2.0.0", path = "../offences" } -pallet-staking = { version = "2.0.0", path = "../staking" } -pallet-staking-reward-curve = { version = "2.0.0", path = "../staking/reward-curve" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-offences = { version = "3.0.0", path = "../offences" } +pallet-staking = { version = "3.0.0", path = "../staking" } +pallet-staking-reward-curve = { version = "3.0.0", path = "../staking/reward-curve" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-election-providers = { version = "3.0.0", path = "../../primitives/election-providers" } [features] default = ["std"] @@ -54,7 +53,6 @@ std = [ "sp-application-crypto/std", "sp-consensus-babe/std", "sp-consensus-vrf/std", - "sp-inherents/std", "sp-io/std", "sp-runtime/std", "sp-session/std", diff --git a/frame/babe/src/equivocation.rs b/frame/babe/src/equivocation.rs index e7053f5ac0fed..b7275d04734ea 100644 --- a/frame/babe/src/equivocation.rs +++ b/frame/babe/src/equivocation.rs @@ -35,8 +35,11 @@ //! definition. //! -use frame_support::{debug, traits::KeyOwnerProofSystem}; -use sp_consensus_babe::{EquivocationProof, SlotNumber}; +use frame_support::{ + debug, + traits::{Get, KeyOwnerProofSystem}, +}; +use sp_consensus_babe::{EquivocationProof, Slot}; use sp_runtime::transaction_validity::{ InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity, TransactionValidityError, ValidTransaction, @@ -56,6 +59,10 @@ use crate::{Call, Module, Config}; /// reporter), and also for creating and submitting equivocation report /// extrinsics (useful only in offchain context). pub trait HandleEquivocation { + /// The longevity, in blocks, that the equivocation report is valid for. When using the staking + /// pallet this should be equal to the bonding duration (in blocks, not eras). + type ReportLongevity: Get; + /// Report an offence proved by the given reporters. fn report_offence( reporters: Vec, @@ -63,7 +70,7 @@ pub trait HandleEquivocation { ) -> Result<(), OffenceError>; /// Returns true if all of the offenders at the given time slot have already been reported. - fn is_known_offence(offenders: &[T::KeyOwnerIdentification], time_slot: &SlotNumber) -> bool; + fn is_known_offence(offenders: &[T::KeyOwnerIdentification], time_slot: &Slot) -> bool; /// Create and dispatch an equivocation report extrinsic. fn submit_unsigned_equivocation_report( @@ -76,6 +83,8 @@ pub trait HandleEquivocation { } impl HandleEquivocation for () { + type ReportLongevity = (); + fn report_offence( _reporters: Vec, _offence: BabeEquivocationOffence, @@ -83,7 +92,7 @@ impl HandleEquivocation for () { Ok(()) } - fn is_known_offence(_offenders: &[T::KeyOwnerIdentification], _time_slot: &SlotNumber) -> bool { + fn is_known_offence(_offenders: &[T::KeyOwnerIdentification], _time_slot: &Slot) -> bool { true } @@ -103,11 +112,11 @@ impl HandleEquivocation for () { /// using existing subsystems that are part of frame (type bounds described /// below) and will dispatch to them directly, it's only purpose is to wire all /// subsystems together. -pub struct EquivocationHandler { - _phantom: sp_std::marker::PhantomData<(I, R)>, +pub struct EquivocationHandler { + _phantom: sp_std::marker::PhantomData<(I, R, L)>, } -impl Default for EquivocationHandler { +impl Default for EquivocationHandler { fn default() -> Self { Self { _phantom: Default::default(), @@ -115,7 +124,7 @@ impl Default for EquivocationHandler { } } -impl HandleEquivocation for EquivocationHandler +impl HandleEquivocation for EquivocationHandler where // We use the authorship pallet to fetch the current block author and use // `offchain::SendTransactionTypes` for unsigned extrinsic creation and @@ -128,7 +137,12 @@ where T::KeyOwnerIdentification, BabeEquivocationOffence, >, + // The longevity (in blocks) that the equivocation report is valid for. When using the staking + // pallet this should be the bonding duration. + L: Get, { + type ReportLongevity = L; + fn report_offence( reporters: Vec, offence: BabeEquivocationOffence, @@ -136,7 +150,7 @@ where R::report_offence(reporters, offence) } - fn is_known_offence(offenders: &[T::KeyOwnerIdentification], time_slot: &SlotNumber) -> bool { + fn is_known_offence(offenders: &[T::KeyOwnerIdentification], time_slot: &Slot) -> bool { R::is_known_offence(offenders, time_slot) } @@ -167,7 +181,7 @@ where impl frame_support::unsigned::ValidateUnsigned for Module { type Call = Call; fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity { - if let Call::report_equivocation_unsigned(equivocation_proof, _) = call { + if let Call::report_equivocation_unsigned(equivocation_proof, key_owner_proof) = call { // discard equivocation report not coming from the local node match source { TransactionSource::Local | TransactionSource::InBlock => { /* allowed */ } @@ -181,14 +195,20 @@ impl frame_support::unsigned::ValidateUnsigned for Module { } } + // check report staleness + is_known_offence::(equivocation_proof, key_owner_proof)?; + + let longevity = >::ReportLongevity::get(); + ValidTransaction::with_tag_prefix("BabeEquivocation") // We assign the maximum priority for any equivocation report. .priority(TransactionPriority::max_value()) // Only one equivocation report for the same offender at the same slot. .and_provides(( equivocation_proof.offender.clone(), - equivocation_proof.slot_number, + *equivocation_proof.slot, )) + .longevity(longevity) // We don't propagate this. This can never be included on a remote node. .propagate(false) .build() @@ -199,39 +219,41 @@ impl frame_support::unsigned::ValidateUnsigned for Module { fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> { if let Call::report_equivocation_unsigned(equivocation_proof, key_owner_proof) = call { - // check the membership proof to extract the offender's id - let key = ( - sp_consensus_babe::KEY_TYPE, - equivocation_proof.offender.clone(), - ); - - let offender = T::KeyOwnerProofSystem::check_proof(key, key_owner_proof.clone()) - .ok_or(InvalidTransaction::BadProof)?; - - // check if the offence has already been reported, - // and if so then we can discard the report. - let is_known_offence = T::HandleEquivocation::is_known_offence( - &[offender], - &equivocation_proof.slot_number, - ); - - if is_known_offence { - Err(InvalidTransaction::Stale.into()) - } else { - Ok(()) - } + is_known_offence::(equivocation_proof, key_owner_proof) } else { Err(InvalidTransaction::Call.into()) } } } +fn is_known_offence( + equivocation_proof: &EquivocationProof, + key_owner_proof: &T::KeyOwnerProof, +) -> Result<(), TransactionValidityError> { + // check the membership proof to extract the offender's id + let key = ( + sp_consensus_babe::KEY_TYPE, + equivocation_proof.offender.clone(), + ); + + let offender = T::KeyOwnerProofSystem::check_proof(key, key_owner_proof.clone()) + .ok_or(InvalidTransaction::BadProof)?; + + // check if the offence has already been reported, + // and if so then we can discard the report. + if T::HandleEquivocation::is_known_offence(&[offender], &equivocation_proof.slot) { + Err(InvalidTransaction::Stale.into()) + } else { + Ok(()) + } +} + /// A BABE equivocation offence report. /// /// When a validator released two or more blocks at the same slot. pub struct BabeEquivocationOffence { - /// A babe slot number in which this incident happened. - pub slot: SlotNumber, + /// A babe slot in which this incident happened. + pub slot: Slot, /// The session index in which the incident happened. pub session_index: SessionIndex, /// The size of the validator set at the time of the offence. @@ -244,7 +266,7 @@ impl Offence for BabeEquivocationOffence { const ID: Kind = *b"babe:equivocatio"; - type TimeSlot = SlotNumber; + type TimeSlot = Slot; fn offenders(&self) -> Vec { vec![self.offender.clone()] diff --git a/frame/babe/src/lib.rs b/frame/babe/src/lib.rs index d604bfd57d1a3..2794d5b247421 100644 --- a/frame/babe/src/lib.rs +++ b/frame/babe/src/lib.rs @@ -25,7 +25,7 @@ use codec::{Decode, Encode}; use frame_support::{ decl_error, decl_module, decl_storage, dispatch::DispatchResultWithPostInfo, - traits::{FindAuthor, Get, KeyOwnerProofSystem, Randomness as RandomnessT}, + traits::{FindAuthor, Get, KeyOwnerProofSystem, OneSessionHandler, Randomness as RandomnessT}, weights::{Pays, Weight}, Parameter, }; @@ -33,20 +33,18 @@ use frame_system::{ensure_none, ensure_signed}; use sp_application_crypto::Public; use sp_runtime::{ generic::DigestItem, - traits::{Hash, IsMember, One, SaturatedConversion, Saturating}, + traits::{Hash, IsMember, One, SaturatedConversion, Saturating, Zero}, ConsensusEngineId, KeyTypeId, }; use sp_session::{GetSessionNumber, GetValidatorCount}; -use sp_std::{prelude::*, result}; +use sp_std::prelude::*; use sp_timestamp::OnTimestampSet; use sp_consensus_babe::{ digests::{NextConfigDescriptor, NextEpochDescriptor, PreDigest}, - inherents::{BabeInherentData, INHERENT_IDENTIFIER}, - BabeAuthorityWeight, ConsensusLog, Epoch, EquivocationProof, SlotNumber, BABE_ENGINE_ID, + BabeAuthorityWeight, ConsensusLog, Epoch, EquivocationProof, Slot, BABE_ENGINE_ID, }; use sp_consensus_vrf::schnorrkel; -use sp_inherents::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent}; pub use sp_consensus_babe::{AuthorityId, PUBLIC_KEY_LENGTH, RANDOMNESS_LENGTH, VRF_OUTPUT_LENGTH}; @@ -66,7 +64,7 @@ pub trait Config: pallet_timestamp::Config { /// The amount of time, in slots, that each epoch should last. /// NOTE: Currently it is not possible to change the epoch duration after /// the chain has started. Attempting to do so will brick block production. - type EpochDuration: Get; + type EpochDuration: Get; /// The expected average block time at which BABE should be creating /// blocks. Since BABE is probabilistic it is not trivial to figure out @@ -168,10 +166,10 @@ decl_storage! { /// The slot at which the first epoch actually started. This is 0 /// until the first block of the chain. - pub GenesisSlot get(fn genesis_slot): u64; + pub GenesisSlot get(fn genesis_slot): Slot; /// Current slot number. - pub CurrentSlot get(fn current_slot): u64; + pub CurrentSlot get(fn current_slot): Slot; /// The epoch randomness for the *current* epoch. /// @@ -403,7 +401,7 @@ impl Module { // so we don't rotate the epoch. now != One::one() && { let diff = CurrentSlot::get().saturating_sub(Self::current_epoch_start()); - diff >= T::EpochDuration::get() + *diff >= T::EpochDuration::get() } } @@ -415,16 +413,18 @@ impl Module { /// In other word, this is only accurate if no slots are missed. Given missed slots, the slot /// number will grow while the block number will not. Hence, the result can be interpreted as an /// upper bound. - // -------------- IMPORTANT NOTE -------------- + // + // ## IMPORTANT NOTE + // // This implementation is linked to how [`should_epoch_change`] is working. This might need to // be updated accordingly, if the underlying mechanics of slot and epochs change. // - // WEIGHT NOTE: This function is tied to the weight of `EstimateNextSessionRotation`. If you update - // this function, you must also update the corresponding weight. + // WEIGHT NOTE: This function is tied to the weight of `EstimateNextSessionRotation`. If you + // update this function, you must also update the corresponding weight. pub fn next_expected_epoch_change(now: T::BlockNumber) -> Option { let next_slot = Self::current_epoch_start().saturating_add(T::EpochDuration::get()); next_slot - .checked_sub(CurrentSlot::get()) + .checked_sub(*CurrentSlot::get()) .map(|slots_remaining| { // This is a best effort guess. Drifts in the slot/block ratio will cause errors here. let blocks_remaining: T::BlockNumber = slots_remaining.saturated_into(); @@ -490,10 +490,10 @@ impl Module { } } - // finds the start slot of the current epoch. only guaranteed to - // give correct results after `do_initialize` of the first block - // in the chain (as its result is based off of `GenesisSlot`). - pub fn current_epoch_start() -> SlotNumber { + /// Finds the start slot of the current epoch. only guaranteed to + /// give correct results after `do_initialize` of the first block + /// in the chain (as its result is based off of `GenesisSlot`). + pub fn current_epoch_start() -> Slot { Self::epoch_start(EpochIndex::get()) } @@ -525,7 +525,7 @@ impl Module { } } - fn epoch_start(epoch_index: u64) -> SlotNumber { + fn epoch_start(epoch_index: u64) -> Slot { // (epoch_index * epoch_duration) + genesis_slot const PROOF: &str = "slot number is u64; it should relate in some way to wall clock time; \ @@ -535,7 +535,7 @@ impl Module { .checked_mul(T::EpochDuration::get()) .expect(PROOF); - epoch_start.checked_add(GenesisSlot::get()).expect(PROOF) + epoch_start.checked_add(*GenesisSlot::get()).expect(PROOF).into() } fn deposit_consensus(new: U) { @@ -583,9 +583,9 @@ impl Module { // on the first non-zero block (i.e. block #1) // this is where the first epoch (epoch #0) actually starts. // we need to adjust internal storage accordingly. - if GenesisSlot::get() == 0 { - GenesisSlot::put(digest.slot_number()); - debug_assert_ne!(GenesisSlot::get(), 0); + if *GenesisSlot::get() == 0 { + GenesisSlot::put(digest.slot()); + debug_assert_ne!(*GenesisSlot::get(), 0); // deposit a log because this is the first block in epoch #0 // we use the same values as genesis because we haven't collected any @@ -599,11 +599,11 @@ impl Module { } // the slot number of the current block being initialized - let current_slot = digest.slot_number(); + let current_slot = digest.slot(); // how many slots were skipped between current and last block let lateness = current_slot.saturating_sub(CurrentSlot::get() + 1); - let lateness = T::BlockNumber::from(lateness as u32); + let lateness = T::BlockNumber::from(*lateness as u32); Lateness::::put(lateness); CurrentSlot::put(current_slot); @@ -684,7 +684,7 @@ impl Module { key_owner_proof: T::KeyOwnerProof, ) -> DispatchResultWithPostInfo { let offender = equivocation_proof.offender.clone(); - let slot_number = equivocation_proof.slot_number; + let slot = equivocation_proof.slot; // validate the equivocation proof if !sp_consensus_babe::check_equivocation_proof(equivocation_proof) { @@ -694,7 +694,7 @@ impl Module { let validator_set_count = key_owner_proof.validator_count(); let session_index = key_owner_proof.session(); - let epoch_index = (slot_number.saturating_sub(GenesisSlot::get()) / T::EpochDuration::get()) + let epoch_index = (*slot.saturating_sub(GenesisSlot::get()) / T::EpochDuration::get()) .saturated_into::(); // check that the slot number is consistent with the session index @@ -709,7 +709,7 @@ impl Module { .ok_or(Error::::InvalidKeyOwnershipProof)?; let offence = BabeEquivocationOffence { - slot: slot_number, + slot, validator_set_count, offender, session_index, @@ -744,10 +744,22 @@ impl Module { } impl OnTimestampSet for Module { - fn on_timestamp_set(_moment: T::Moment) { } + fn on_timestamp_set(moment: T::Moment) { + let slot_duration = Self::slot_duration(); + assert!(!slot_duration.is_zero(), "Babe slot duration cannot be zero."); + + let timestamp_slot = moment / slot_duration; + let timestamp_slot = Slot::from(timestamp_slot.saturated_into::()); + + assert!(CurrentSlot::get() == timestamp_slot, "Timestamp slot must match `CurrentSlot`"); + } } impl frame_support::traits::EstimateNextSessionRotation for Module { + fn average_session_length() -> T::BlockNumber { + T::EpochDuration::get().saturated_into() + } + fn estimate_next_session_rotation(now: T::BlockNumber) -> Option { Self::next_expected_epoch_change(now) } @@ -769,7 +781,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = AuthorityId; } -impl pallet_session::OneSessionHandler for Module { +impl OneSessionHandler for Module { type Key = AuthorityId; fn on_genesis_session<'a, I: 'a>(validators: I) @@ -818,29 +830,3 @@ fn compute_randomness( sp_io::hashing::blake2_256(&s) } - -impl ProvideInherent for Module { - type Call = pallet_timestamp::Call; - type Error = MakeFatalError; - const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER; - - fn create_inherent(_: &InherentData) -> Option { - None - } - - fn check_inherent(call: &Self::Call, data: &InherentData) -> result::Result<(), Self::Error> { - let timestamp = match call { - pallet_timestamp::Call::set(ref timestamp) => timestamp.clone(), - _ => return Ok(()), - }; - - let timestamp_based_slot = (timestamp / Self::slot_duration()).saturated_into::(); - let seal_slot = data.babe_inherent_data()?; - - if timestamp_based_slot == seal_slot { - Ok(()) - } else { - Err(sp_inherents::Error::from("timestamp set in block doesn't match slot in seal").into()) - } - } -} diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index 3198930ea6ccb..412f13f6a2df8 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -33,10 +33,11 @@ use frame_support::{ }; use sp_io; use sp_core::{H256, U256, crypto::{IsWrappedBy, KeyTypeId, Pair}}; -use sp_consensus_babe::{AuthorityId, AuthorityPair, SlotNumber}; +use sp_consensus_babe::{AuthorityId, AuthorityPair, Slot}; use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof}; use sp_staking::SessionIndex; use pallet_staking::EraIndex; +use sp_election_providers::onchain; use pallet_session::historical as pallet_session_historical; type DummyValidatorId = u64; @@ -54,7 +55,7 @@ frame_support::construct_runtime!( Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Historical: pallet_session_historical::{Module}, Offences: pallet_offences::{Module, Call, Storage, Event}, - Babe: pallet_babe::{Module, Call, Storage, Config, Inherent, ValidateUnsigned}, + Babe: pallet_babe::{Module, Call, Storage, Config, ValidateUnsigned}, Staking: pallet_staking::{Module, Call, Storage, Config, Event}, Session: pallet_session::{Module, Call, Storage, Event, Config}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, @@ -63,8 +64,6 @@ frame_support::construct_runtime!( parameter_types! { pub const BlockHashCount: u64 = 250; - pub const EpochDuration: u64 = 3; - pub const ExpectedBlockTime: u64 = 1; pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(16); pub BlockWeights: frame_system::limits::BlockWeights = frame_system::limits::BlockWeights::simple_max(1024); @@ -185,6 +184,13 @@ parameter_types! { pub const StakingUnsignedPriority: u64 = u64::max_value() / 2; } +impl onchain::Config for Test { + type AccountId = ::AccountId; + type BlockNumber = ::BlockNumber; + type Accuracy = Perbill; + type DataProvider = Staking; +} + impl pallet_staking::Config for Test { type RewardRemainder = (); type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote; @@ -207,6 +213,7 @@ impl pallet_staking::Config for Test { type MaxIterations = (); type MinSolutionScoreBump = (); type OffchainSolutionWeightLimit = (); + type ElectionProvider = onchain::OnChainSequentialPhragmen; type WeightInfo = (); } @@ -222,6 +229,13 @@ impl pallet_offences::Config for Test { type WeightSoftLimit = OffencesWeightSoftLimit; } +parameter_types! { + pub const EpochDuration: u64 = 3; + pub const ExpectedBlockTime: u64 = 1; + pub const ReportLongevity: u64 = + BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * EpochDuration::get(); +} + impl Config for Test { type EpochDuration = EpochDuration; type ExpectedBlockTime = ExpectedBlockTime; @@ -237,7 +251,9 @@ impl Config for Test { AuthorityId, )>>::IdentificationTuple; - type HandleEquivocation = super::EquivocationHandler; + type HandleEquivocation = + super::EquivocationHandler; + type WeightInfo = (); } @@ -255,14 +271,14 @@ pub fn go_to_block(n: u64, s: u64) { System::parent_hash() }; - let pre_digest = make_secondary_plain_pre_digest(0, s); + let pre_digest = make_secondary_plain_pre_digest(0, s.into()); System::initialize(&n, &parent_hash, &pre_digest, InitKind::Full); System::set_block_number(n); Timestamp::set_timestamp(n); if s > 1 { - CurrentSlot::put(s); + CurrentSlot::put(Slot::from(s)); } System::on_initialize(n); @@ -272,8 +288,8 @@ pub fn go_to_block(n: u64, s: u64) { /// Slots will grow accordingly to blocks pub fn progress_to_block(n: u64) { - let mut slot = Babe::current_slot() + 1; - for i in System::block_number()+1..=n { + let mut slot = u64::from(Babe::current_slot()) + 1; + for i in System::block_number() + 1 ..= n { go_to_block(i, slot); slot += 1; } @@ -294,14 +310,14 @@ pub fn start_era(era_index: EraIndex) { pub fn make_primary_pre_digest( authority_index: sp_consensus_babe::AuthorityIndex, - slot_number: sp_consensus_babe::SlotNumber, + slot: sp_consensus_babe::Slot, vrf_output: VRFOutput, vrf_proof: VRFProof, ) -> Digest { let digest_data = sp_consensus_babe::digests::PreDigest::Primary( sp_consensus_babe::digests::PrimaryPreDigest { authority_index, - slot_number, + slot, vrf_output, vrf_proof, } @@ -312,12 +328,12 @@ pub fn make_primary_pre_digest( pub fn make_secondary_plain_pre_digest( authority_index: sp_consensus_babe::AuthorityIndex, - slot_number: sp_consensus_babe::SlotNumber, + slot: sp_consensus_babe::Slot, ) -> Digest { let digest_data = sp_consensus_babe::digests::PreDigest::SecondaryPlain( sp_consensus_babe::digests::SecondaryPlainPreDigest { authority_index, - slot_number, + slot, } ); let log = DigestItem::PreRuntime(sp_consensus_babe::BABE_ENGINE_ID, digest_data.encode()); @@ -326,14 +342,14 @@ pub fn make_secondary_plain_pre_digest( pub fn make_secondary_vrf_pre_digest( authority_index: sp_consensus_babe::AuthorityIndex, - slot_number: sp_consensus_babe::SlotNumber, + slot: sp_consensus_babe::Slot, vrf_output: VRFOutput, vrf_proof: VRFProof, ) -> Digest { let digest_data = sp_consensus_babe::digests::PreDigest::SecondaryVRF( sp_consensus_babe::digests::SecondaryVRFPreDigest { authority_index, - slot_number, + slot, vrf_output, vrf_proof, } @@ -343,11 +359,11 @@ pub fn make_secondary_vrf_pre_digest( } pub fn make_vrf_output( - slot_number: u64, + slot: Slot, pair: &sp_consensus_babe::AuthorityPair ) -> (VRFOutput, VRFProof, [u8; 32]) { let pair = sp_core::sr25519::Pair::from_ref(pair).as_ref(); - let transcript = sp_consensus_babe::make_transcript(&Babe::randomness(), slot_number, 0); + let transcript = sp_consensus_babe::make_transcript(&Babe::randomness(), slot, 0); let vrf_inout = pair.vrf_sign(transcript); let vrf_randomness: sp_consensus_vrf::schnorrkel::Randomness = vrf_inout.0 .make_bytes::<[u8; 32]>(&sp_consensus_babe::BABE_VRF_INOUT_CONTEXT); @@ -435,7 +451,7 @@ pub fn new_test_ext_raw_authorities(authorities: Vec) -> sp_io::Tes pub fn generate_equivocation_proof( offender_authority_index: u32, offender_authority_pair: &AuthorityPair, - slot_number: SlotNumber, + slot: Slot, ) -> sp_consensus_babe::EquivocationProof

{ use sp_consensus_babe::digests::CompatibleDigestItem; @@ -444,7 +460,7 @@ pub fn generate_equivocation_proof( let make_header = || { let parent_hash = System::parent_hash(); - let pre_digest = make_secondary_plain_pre_digest(offender_authority_index, slot_number); + let pre_digest = make_secondary_plain_pre_digest(offender_authority_index, slot); System::initialize(¤t_block, &parent_hash, &pre_digest, InitKind::Full); System::set_block_number(current_block); Timestamp::set_timestamp(current_block); @@ -469,10 +485,10 @@ pub fn generate_equivocation_proof( seal_header(&mut h2); // restore previous runtime state - go_to_block(current_block, current_slot); + go_to_block(current_block, *current_slot); sp_consensus_babe::EquivocationProof { - slot_number, + slot, offender: offender_authority_pair.public(), first_header: h1, second_header: h2, diff --git a/frame/babe/src/tests.rs b/frame/babe/src/tests.rs index 23e8bc765c804..8576389af31ff 100644 --- a/frame/babe/src/tests.rs +++ b/frame/babe/src/tests.rs @@ -25,7 +25,7 @@ use frame_support::{ }; use mock::*; use pallet_session::ShouldEndSession; -use sp_consensus_babe::AllowedSlots; +use sp_consensus_babe::{AllowedSlots, Slot}; use sp_core::crypto::Pair; const EMPTY_RANDOMNESS: [u8; 32] = [ @@ -62,7 +62,7 @@ fn first_block_epoch_zero_start() { let (pairs, mut ext) = new_test_ext_with_pairs(4); ext.execute_with(|| { - let genesis_slot = 100; + let genesis_slot = Slot::from(100); let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]); let first_vrf = vrf_output; @@ -73,7 +73,7 @@ fn first_block_epoch_zero_start() { vrf_proof, ); - assert_eq!(Babe::genesis_slot(), 0); + assert_eq!(Babe::genesis_slot(), Slot::from(0)); System::initialize( &1, &Default::default(), @@ -120,7 +120,7 @@ fn author_vrf_output_for_primary() { let (pairs, mut ext) = new_test_ext_with_pairs(1); ext.execute_with(|| { - let genesis_slot = 10; + let genesis_slot = Slot::from(10); let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]); let primary_pre_digest = make_primary_pre_digest(0, genesis_slot, vrf_output, vrf_proof); @@ -146,7 +146,7 @@ fn author_vrf_output_for_secondary_vrf() { let (pairs, mut ext) = new_test_ext_with_pairs(1); ext.execute_with(|| { - let genesis_slot = 10; + let genesis_slot = Slot::from(10); let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]); let secondary_vrf_pre_digest = make_secondary_vrf_pre_digest(0, genesis_slot, vrf_output, vrf_proof); @@ -170,7 +170,7 @@ fn author_vrf_output_for_secondary_vrf() { #[test] fn no_author_vrf_output_for_secondary_plain() { new_test_ext(1).execute_with(|| { - let genesis_slot = 10; + let genesis_slot = Slot::from(10); let secondary_plain_pre_digest = make_secondary_plain_pre_digest(0, genesis_slot); System::initialize( @@ -205,17 +205,17 @@ fn can_predict_next_epoch_change() { assert_eq!(::EpochDuration::get(), 3); // this sets the genesis slot to 6; go_to_block(1, 6); - assert_eq!(Babe::genesis_slot(), 6); - assert_eq!(Babe::current_slot(), 6); + assert_eq!(*Babe::genesis_slot(), 6); + assert_eq!(*Babe::current_slot(), 6); assert_eq!(Babe::epoch_index(), 0); progress_to_block(5); assert_eq!(Babe::epoch_index(), 5 / 3); - assert_eq!(Babe::current_slot(), 10); + assert_eq!(*Babe::current_slot(), 10); // next epoch change will be at - assert_eq!(Babe::current_epoch_start(), 9); // next change will be 12, 2 slots from now + assert_eq!(*Babe::current_epoch_start(), 9); // next change will be 12, 2 slots from now assert_eq!(Babe::next_expected_epoch_change(System::block_number()), Some(5 + 2)); }) } @@ -226,8 +226,8 @@ fn can_enact_next_config() { assert_eq!(::EpochDuration::get(), 3); // this sets the genesis slot to 6; go_to_block(1, 6); - assert_eq!(Babe::genesis_slot(), 6); - assert_eq!(Babe::current_slot(), 6); + assert_eq!(*Babe::genesis_slot(), 6); + assert_eq!(*Babe::current_slot(), 6); assert_eq!(Babe::epoch_index(), 0); go_to_block(2, 7); @@ -269,12 +269,12 @@ fn can_fetch_current_and_next_epoch_data() { let current_epoch = Babe::current_epoch(); assert_eq!(current_epoch.epoch_index, 3); - assert_eq!(current_epoch.start_slot, 10); + assert_eq!(*current_epoch.start_slot, 10); assert_eq!(current_epoch.authorities.len(), 5); let next_epoch = Babe::next_epoch(); assert_eq!(next_epoch.epoch_index, 4); - assert_eq!(next_epoch.start_slot, 13); + assert_eq!(*next_epoch.start_slot, 13); assert_eq!(next_epoch.authorities.len(), 5); // the on-chain randomness should always change across epochs @@ -572,7 +572,7 @@ fn report_equivocation_invalid_equivocation_proof() { &offending_authority_pair, CurrentSlot::get(), ); - equivocation_proof.slot_number = 0; + equivocation_proof.slot = Slot::from(0); assert_invalid_equivocation(equivocation_proof.clone()); // different slot numbers in headers @@ -611,8 +611,8 @@ fn report_equivocation_invalid_equivocation_proof() { #[test] fn report_equivocation_validate_unsigned_prevents_duplicates() { use sp_runtime::transaction_validity::{ - InvalidTransaction, TransactionLongevity, TransactionPriority, TransactionSource, - TransactionValidity, ValidTransaction, + InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity, + ValidTransaction, }; let (pairs, mut ext) = new_test_ext_with_pairs(3); @@ -664,7 +664,7 @@ fn report_equivocation_validate_unsigned_prevents_duplicates() { priority: TransactionPriority::max_value(), requires: vec![], provides: vec![("BabeEquivocation", tx_tag).encode()], - longevity: TransactionLongevity::max_value(), + longevity: ReportLongevity::get(), propagate: false, }) ); @@ -676,7 +676,16 @@ fn report_equivocation_validate_unsigned_prevents_duplicates() { Babe::report_equivocation_unsigned(Origin::none(), equivocation_proof, key_owner_proof) .unwrap(); - // the report should now be considered stale and the transaction is invalid + // the report should now be considered stale and the transaction is invalid. + // the check for staleness should be done on both `validate_unsigned` and on `pre_dispatch` + assert_err!( + ::validate_unsigned( + TransactionSource::Local, + &inner, + ), + InvalidTransaction::Stale, + ); + assert_err!( ::pre_dispatch(&inner), InvalidTransaction::Stale, diff --git a/frame/balances/Cargo.toml b/frame/balances/Cargo.toml index 82f0e3f6b0750..39b7fda77fef7 100644 --- a/frame/balances/Cargo.toml +++ b/frame/balances/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-balances" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,17 +14,17 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] -sp-io = { version = "2.0.0", path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -pallet-transaction-payment = { version = "2.0.0", path = "../transaction-payment" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-transaction-payment = { version = "3.0.0", path = "../transaction-payment" } [features] default = ["std"] diff --git a/frame/balances/src/benchmarking.rs b/frame/balances/src/benchmarking.rs index 53cf273d850de..14732b44b4fc2 100644 --- a/frame/balances/src/benchmarking.rs +++ b/frame/balances/src/benchmarking.rs @@ -22,7 +22,7 @@ use super::*; use frame_system::RawOrigin; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use sp_runtime::traits::Bounded; use crate::Module as Balances; @@ -144,51 +144,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests_composite::{ExtBuilder, Test}; - use frame_support::assert_ok; - - #[test] - fn transfer() { - ExtBuilder::default().build().execute_with(|| { - assert_ok!(test_benchmark_transfer::()); - }); - } - - #[test] - fn transfer_best_case() { - ExtBuilder::default().build().execute_with(|| { - assert_ok!(test_benchmark_transfer_best_case::()); - }); - } - - #[test] - fn transfer_keep_alive() { - ExtBuilder::default().build().execute_with(|| { - assert_ok!(test_benchmark_transfer_keep_alive::()); - }); - } - - #[test] - fn transfer_set_balance_creating() { - ExtBuilder::default().build().execute_with(|| { - assert_ok!(test_benchmark_set_balance_creating::()); - }); - } - - #[test] - fn transfer_set_balance_killing() { - ExtBuilder::default().build().execute_with(|| { - assert_ok!(test_benchmark_set_balance_killing::()); - }); - } - - #[test] - fn force_transfer() { - ExtBuilder::default().build().execute_with(|| { - assert_ok!(test_benchmark_force_transfer::()); - }); - } -} +impl_benchmark_test_suite!( + Balances, + crate::tests_composite::ExtBuilder::default().build(), + crate::tests_composite::Test, +); diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index ef069455bbabe..cc82497293c8c 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -15,17 +15,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! # Balances Module +//! # Balances Pallet //! -//! The Balances module provides functionality for handling accounts and balances. +//! The Balances pallet provides functionality for handling accounts and balances. //! -//! - [`balances::Config`](./trait.Config.html) -//! - [`Call`](./enum.Call.html) -//! - [`Module`](./struct.Module.html) +//! - [`Config`] +//! - [`Call`] +//! - [`Pallet`] //! //! ## Overview //! -//! The Balances module provides functions for: +//! The Balances pallet provides functions for: //! //! - Getting and setting free balances. //! - Retrieving total, reserved and unreserved balances. @@ -43,7 +43,7 @@ //! fall below this, then the account is said to be dead; and it loses its functionality as well as any //! prior history and all information on it is removed from the chain's state. //! No account should ever have a total balance that is strictly between 0 and the existential -//! deposit (exclusive). If this ever happens, it indicates either a bug in this module or an +//! deposit (exclusive). If this ever happens, it indicates either a bug in this pallet or an //! erroneous raw mutation of storage. //! //! - **Total Issuance:** The total number of units in existence in a system. @@ -67,20 +67,18 @@ //! //! ### Implementations //! -//! The Balances module provides implementations for the following traits. If these traits provide the functionality -//! that you need, then you can avoid coupling with the Balances module. +//! The Balances pallet provides implementations for the following traits. If these traits provide the functionality +//! that you need, then you can avoid coupling with the Balances pallet. //! -//! - [`Currency`](../frame_support/traits/trait.Currency.html): Functions for dealing with a +//! - [`Currency`](frame_support::traits::Currency): Functions for dealing with a //! fungible assets system. -//! - [`ReservableCurrency`](../frame_support/traits/trait.ReservableCurrency.html): +//! - [`ReservableCurrency`](frame_support::traits::ReservableCurrency): //! Functions for dealing with assets that can be reserved from an account. -//! - [`LockableCurrency`](../frame_support/traits/trait.LockableCurrency.html): Functions for +//! - [`LockableCurrency`](frame_support::traits::LockableCurrency): Functions for //! dealing with accounts that allow liquidity restrictions. -//! - [`Imbalance`](../frame_support/traits/trait.Imbalance.html): Functions for handling +//! - [`Imbalance`](frame_support::traits::Imbalance): Functions for handling //! imbalances between total issuance in the system and account balances. Must be used when a function //! creates new funds (e.g. a reward) or destroys some funds (e.g. a system fee). -//! - [`IsDeadAccount`](../frame_support/traits/trait.IsDeadAccount.html): Determiner to say whether a -//! given account is unused. //! //! ## Interface //! @@ -91,11 +89,11 @@ //! //! ## Usage //! -//! The following examples show how to use the Balances module in your custom module. +//! The following examples show how to use the Balances pallet in your custom pallet. //! //! ### Examples from the FRAME //! -//! The Contract module uses the `Currency` trait to handle gas payment, and its types inherit from `Currency`: +//! The Contract pallet uses the `Currency` trait to handle gas payment, and its types inherit from `Currency`: //! //! ``` //! use frame_support::traits::Currency; @@ -109,7 +107,7 @@ //! # fn main() {} //! ``` //! -//! The Staking module uses the `LockableCurrency` trait to lock a stash account's funds: +//! The Staking pallet uses the `LockableCurrency` trait to lock a stash account's funds: //! //! ``` //! use frame_support::traits::{WithdrawReasons, LockableCurrency}; @@ -141,7 +139,7 @@ //! //! ## Genesis config //! -//! The Balances module depends on the [`GenesisConfig`](./struct.GenesisConfig.html). +//! The Balances pallet depends on the [`GenesisConfig`]. //! //! ## Assumptions //! @@ -153,6 +151,7 @@ mod tests; mod tests_local; mod tests_composite; +mod tests_reentrancy; mod benchmarking; pub mod weights; @@ -160,7 +159,7 @@ use sp_std::prelude::*; use sp_std::{cmp, result, mem, fmt::Debug, ops::BitOr}; use codec::{Codec, Encode, Decode}; use frame_support::{ - StorageValue, Parameter, decl_event, decl_storage, decl_module, decl_error, ensure, + ensure, traits::{ Currency, OnUnbalanced, TryDrop, StoredMap, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement, @@ -168,98 +167,236 @@ use frame_support::{ ExistenceRequirement::AllowDeath, BalanceStatus as Status, } }; +#[cfg(feature = "std")] +use frame_support::traits::GenesisBuild; use sp_runtime::{ RuntimeDebug, DispatchResult, DispatchError, traits::{ - Zero, AtLeast32BitUnsigned, StaticLookup, Member, CheckedAdd, CheckedSub, + Zero, AtLeast32BitUnsigned, StaticLookup, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Saturating, Bounded, StoredMapError, }, }; -use frame_system::{self as system, ensure_signed, ensure_root}; +use frame_system as system; pub use self::imbalances::{PositiveImbalance, NegativeImbalance}; pub use weights::WeightInfo; -pub trait Subtrait: frame_system::Config { - /// The balance of an account. - type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + - MaybeSerializeDeserialize + Debug; +pub use pallet::*; - /// The minimum amount required to keep an account open. - type ExistentialDeposit: Get; +#[frame_support::pallet] +pub mod pallet { + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use super::*; - /// The means of storing the balances of an account. - type AccountStore: StoredMap>; + #[pallet::config] + pub trait Config: frame_system::Config { + /// The balance of an account. + type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + + MaybeSerializeDeserialize + Debug; - /// Weight information for the extrinsics in this pallet. - type WeightInfo: WeightInfo; + /// Handler for the unbalanced reduction when removing a dust account. + type DustRemoval: OnUnbalanced>; - /// The maximum number of locks that should exist on an account. - /// Not strictly enforced, but used for weight estimation. - type MaxLocks: Get; -} + /// The overarching event type. + type Event: From> + IsType<::Event>; -pub trait Config: frame_system::Config { - /// The balance of an account. - type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + - MaybeSerializeDeserialize + Debug; + /// The minimum amount required to keep an account open. + #[pallet::constant] + type ExistentialDeposit: Get; - /// Handler for the unbalanced reduction when removing a dust account. - type DustRemoval: OnUnbalanced>; + /// The means of storing the balances of an account. + type AccountStore: StoredMap>; - /// The overarching event type. - type Event: From> + Into<::Event>; + /// Weight information for extrinsics in this pallet. + type WeightInfo: WeightInfo; - /// The minimum amount required to keep an account open. - type ExistentialDeposit: Get; + /// The maximum number of locks that should exist on an account. + /// Not strictly enforced, but used for weight estimation. + type MaxLocks: Get; + } - /// The means of storing the balances of an account. - type AccountStore: StoredMap>; + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(PhantomData<(T, I)>); - /// Weight information for extrinsics in this pallet. - type WeightInfo: WeightInfo; + #[pallet::hooks] + impl, I: 'static> Hooks> for Pallet { + } - /// The maximum number of locks that should exist on an account. - /// Not strictly enforced, but used for weight estimation. - type MaxLocks: Get; -} + #[pallet::call] + impl, I: 'static> Pallet { + /// Transfer some liquid free balance to another account. + /// + /// `transfer` will set the `FreeBalance` of the sender and receiver. + /// It will decrease the total issuance of the system by the `TransferFee`. + /// If the sender's account is below the existential deposit as a result + /// of the transfer, the account will be reaped. + /// + /// The dispatch origin for this call must be `Signed` by the transactor. + /// + /// # + /// - Dependent on arguments but not critical, given proper implementations for + /// input config types. See related functions below. + /// - It contains a limited number of reads and writes internally and no complex computation. + /// + /// Related functions: + /// + /// - `ensure_can_withdraw` is always called internally but has a bounded complexity. + /// - Transferring balances to accounts that did not exist before will cause + /// `T::OnNewAccount::on_new_account` to be called. + /// - Removing enough funds from an account will trigger `T::DustRemoval::on_unbalanced`. + /// - `transfer_keep_alive` works the same way as `transfer`, but has an additional + /// check that the transfer will not kill the origin account. + /// --------------------------------- + /// - Base Weight: 73.64 µs, worst case scenario (account created, account removed) + /// - DB Weight: 1 Read and 1 Write to destination account + /// - Origin account is already in memory, so no DB operations for them. + /// # + #[pallet::weight(T::WeightInfo::transfer())] + pub fn transfer( + origin: OriginFor, + dest: ::Source, + #[pallet::compact] value: T::Balance, + ) -> DispatchResultWithPostInfo { + let transactor = ensure_signed(origin)?; + let dest = T::Lookup::lookup(dest)?; + >::transfer(&transactor, &dest, value, ExistenceRequirement::AllowDeath)?; + Ok(().into()) + } -impl, I: Instance> Subtrait for T { - type Balance = T::Balance; - type ExistentialDeposit = T::ExistentialDeposit; - type AccountStore = T::AccountStore; - type WeightInfo = >::WeightInfo; - type MaxLocks = T::MaxLocks; -} + /// Set the balances of a given account. + /// + /// This will alter `FreeBalance` and `ReservedBalance` in storage. it will + /// also decrease the total issuance of the system (`TotalIssuance`). + /// If the new free or reserved balance is below the existential deposit, + /// it will reset the account nonce (`frame_system::AccountNonce`). + /// + /// The dispatch origin for this call is `root`. + /// + /// # + /// - Independent of the arguments. + /// - Contains a limited number of reads and writes. + /// --------------------- + /// - Base Weight: + /// - Creating: 27.56 µs + /// - Killing: 35.11 µs + /// - DB Weight: 1 Read, 1 Write to `who` + /// # + #[pallet::weight( + T::WeightInfo::set_balance_creating() // Creates a new account. + .max(T::WeightInfo::set_balance_killing()) // Kills an existing account. + )] + pub(super) fn set_balance( + origin: OriginFor, + who: ::Source, + #[pallet::compact] new_free: T::Balance, + #[pallet::compact] new_reserved: T::Balance, + ) -> DispatchResultWithPostInfo { + ensure_root(origin)?; + let who = T::Lookup::lookup(who)?; + let existential_deposit = T::ExistentialDeposit::get(); -decl_event!( - pub enum Event where - ::AccountId, - >::Balance - { + let wipeout = new_free + new_reserved < existential_deposit; + let new_free = if wipeout { Zero::zero() } else { new_free }; + let new_reserved = if wipeout { Zero::zero() } else { new_reserved }; + + let (free, reserved) = Self::mutate_account(&who, |account| { + if new_free > account.free { + mem::drop(PositiveImbalance::::new(new_free - account.free)); + } else if new_free < account.free { + mem::drop(NegativeImbalance::::new(account.free - new_free)); + } + + if new_reserved > account.reserved { + mem::drop(PositiveImbalance::::new(new_reserved - account.reserved)); + } else if new_reserved < account.reserved { + mem::drop(NegativeImbalance::::new(account.reserved - new_reserved)); + } + + account.free = new_free; + account.reserved = new_reserved; + + (account.free, account.reserved) + })?; + Self::deposit_event(Event::BalanceSet(who, free, reserved)); + Ok(().into()) + } + + /// Exactly as `transfer`, except the origin must be root and the source account may be + /// specified. + /// # + /// - Same as transfer, but additional read and write because the source account is + /// not assumed to be in the overlay. + /// # + #[pallet::weight(T::WeightInfo::force_transfer())] + pub fn force_transfer( + origin: OriginFor, + source: ::Source, + dest: ::Source, + #[pallet::compact] value: T::Balance, + ) -> DispatchResultWithPostInfo { + ensure_root(origin)?; + let source = T::Lookup::lookup(source)?; + let dest = T::Lookup::lookup(dest)?; + >::transfer(&source, &dest, value, ExistenceRequirement::AllowDeath)?; + Ok(().into()) + } + + /// Same as the [`transfer`] call, but with a check that the transfer will not kill the + /// origin account. + /// + /// 99% of the time you want [`transfer`] instead. + /// + /// [`transfer`]: struct.Pallet.html#method.transfer + /// # + /// - Cheaper than transfer because account cannot be killed. + /// - Base Weight: 51.4 µs + /// - DB Weight: 1 Read and 1 Write to dest (sender is in overlay already) + /// # + #[pallet::weight(T::WeightInfo::transfer_keep_alive())] + pub fn transfer_keep_alive( + origin: OriginFor, + dest: ::Source, + #[pallet::compact] value: T::Balance, + ) -> DispatchResultWithPostInfo { + let transactor = ensure_signed(origin)?; + let dest = T::Lookup::lookup(dest)?; + >::transfer(&transactor, &dest, value, KeepAlive)?; + Ok(().into()) + } + } + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + #[pallet::metadata(T::AccountId = "AccountId", T::Balance = "Balance")] + pub enum Event, I: 'static = ()> { /// An account was created with some free balance. \[account, free_balance\] - Endowed(AccountId, Balance), + Endowed(T::AccountId, T::Balance), /// An account was removed whose balance was non-zero but below ExistentialDeposit, /// resulting in an outright loss. \[account, balance\] - DustLost(AccountId, Balance), + DustLost(T::AccountId, T::Balance), /// Transfer succeeded. \[from, to, value\] - Transfer(AccountId, AccountId, Balance), + Transfer(T::AccountId, T::AccountId, T::Balance), /// A balance was set by root. \[who, free, reserved\] - BalanceSet(AccountId, Balance, Balance), + BalanceSet(T::AccountId, T::Balance, T::Balance), /// Some amount was deposited (e.g. for transaction fees). \[who, deposit\] - Deposit(AccountId, Balance), + Deposit(T::AccountId, T::Balance), /// Some balance was reserved (moved from free to reserved). \[who, value\] - Reserved(AccountId, Balance), + Reserved(T::AccountId, T::Balance), /// Some balance was unreserved (moved from reserved to free). \[who, value\] - Unreserved(AccountId, Balance), + Unreserved(T::AccountId, T::Balance), /// Some balance was moved from the reserve of the first account to the second account. /// Final argument indicates the destination balance type. /// \[from, to, balance, destination_status\] - ReserveRepatriated(AccountId, AccountId, Balance, Status), + ReserveRepatriated(T::AccountId, T::AccountId, T::Balance, Status), } -); -decl_error! { - pub enum Error for Module, I: Instance> { + /// Old name generated by `decl_event`. + #[deprecated(note = "use `Event` instead")] + pub type RawEvent = Event; + + #[pallet::error] + pub enum Error { /// Vesting balance too high to send value VestingBalance, /// Account liquidity restrictions prevent withdrawal @@ -277,6 +414,107 @@ decl_error! { /// Beneficiary account must pre-exist DeadAccount, } + + /// The total units issued in the system. + #[pallet::storage] + #[pallet::getter(fn total_issuance)] + pub type TotalIssuance, I: 'static = ()> = StorageValue<_, T::Balance, ValueQuery>; + + /// The balance of an account. + /// + /// NOTE: This is only used in the case that this pallet is used to store balances. + #[pallet::storage] + pub type Account, I: 'static = ()> = StorageMap< + _, + Blake2_128Concat, + T::AccountId, + AccountData, + ValueQuery + >; + + /// Any liquidity locks on some account balances. + /// NOTE: Should only be accessed when setting, changing and freeing a lock. + #[pallet::storage] + #[pallet::getter(fn locks)] + pub type Locks, I: 'static = ()> = StorageMap< + _, + Blake2_128Concat, + T::AccountId, + Vec>, + ValueQuery + >; + + /// Storage version of the pallet. + /// + /// This is set to v2.0.0 for new networks. + #[pallet::storage] + pub(super) type StorageVersion, I: 'static = ()> = StorageValue< + _, + Releases, + ValueQuery + >; + + #[pallet::genesis_config] + pub struct GenesisConfig, I: 'static = ()> { + pub balances: Vec<(T::AccountId, T::Balance)>, + } + + #[cfg(feature = "std")] + impl, I: 'static> Default for GenesisConfig { + fn default() -> Self { + Self { + balances: Default::default(), + } + } + } + + #[pallet::genesis_build] + impl, I: 'static> GenesisBuild for GenesisConfig { + fn build(&self) { + let total = self.balances + .iter() + .fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n); + >::put(total); + + >::put(Releases::V2_0_0); + + for (_, balance) in &self.balances { + assert!( + *balance >= >::ExistentialDeposit::get(), + "the balance of any account should always be at least the existential deposit.", + ) + } + + // ensure no duplicates exist. + let endowed_accounts = self.balances.iter().map(|(x, _)| x).cloned().collect::>(); + + assert!(endowed_accounts.len() == self.balances.len(), "duplicate balances in genesis."); + + for &(ref who, free) in self.balances.iter() { + assert!(T::AccountStore::insert(who, AccountData { free, ..Default::default() }).is_ok()); + } + } + } +} + +#[cfg(feature = "std")] +impl, I: 'static> GenesisConfig { + /// Direct implementation of `GenesisBuild::build_storage`. + /// + /// Kept in order not to break dependency. + pub fn build_storage(&self) -> Result { + >::build_storage(self) + } + + /// Direct implementation of `GenesisBuild::assimilate_storage`. + /// + /// Kept in order not to break dependency. + pub fn assimilate_storage( + &self, + storage: &mut sp_runtime::Storage + ) -> Result<(), String> { + >::assimilate_storage(self, storage) + } } /// Simplified reasons for withdrawing balance. @@ -381,199 +619,18 @@ impl Default for Releases { } } -decl_storage! { - trait Store for Module, I: Instance=DefaultInstance> as Balances { - /// The total units issued in the system. - pub TotalIssuance get(fn total_issuance) build(|config: &GenesisConfig| { - config.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n) - }): T::Balance; - - /// The balance of an account. - /// - /// NOTE: This is only used in the case that this module is used to store balances. - pub Account: map hasher(blake2_128_concat) T::AccountId => AccountData; - - /// Any liquidity locks on some account balances. - /// NOTE: Should only be accessed when setting, changing and freeing a lock. - pub Locks get(fn locks): map hasher(blake2_128_concat) T::AccountId => Vec>; +pub struct DustCleaner, I: 'static = ()>(Option<(T::AccountId, NegativeImbalance)>); - /// Storage version of the pallet. - /// - /// This is set to v2.0.0 for new networks. - StorageVersion build(|_: &GenesisConfig| Releases::V2_0_0): Releases; - } - add_extra_genesis { - config(balances): Vec<(T::AccountId, T::Balance)>; - // ^^ begin, length, amount liquid at genesis - build(|config: &GenesisConfig| { - for (_, balance) in &config.balances { - assert!( - *balance >= >::ExistentialDeposit::get(), - "the balance of any account should always be at least the existential deposit.", - ) - } - - // ensure no duplicates exist. - let endowed_accounts = config.balances.iter().map(|(x, _)| x).cloned().collect::>(); - - assert!(endowed_accounts.len() == config.balances.len(), "duplicate balances in genesis."); - - for &(ref who, free) in config.balances.iter() { - assert!(T::AccountStore::insert(who, AccountData { free, .. Default::default() }).is_ok()); - } - }); - } -} - -decl_module! { - pub struct Module, I: Instance = DefaultInstance> for enum Call where origin: T::Origin { - type Error = Error; - - /// The minimum amount required to keep an account open. - const ExistentialDeposit: T::Balance = T::ExistentialDeposit::get(); - - fn deposit_event() = default; - - /// Transfer some liquid free balance to another account. - /// - /// `transfer` will set the `FreeBalance` of the sender and receiver. - /// It will decrease the total issuance of the system by the `TransferFee`. - /// If the sender's account is below the existential deposit as a result - /// of the transfer, the account will be reaped. - /// - /// The dispatch origin for this call must be `Signed` by the transactor. - /// - /// # - /// - Dependent on arguments but not critical, given proper implementations for - /// input config types. See related functions below. - /// - It contains a limited number of reads and writes internally and no complex computation. - /// - /// Related functions: - /// - /// - `ensure_can_withdraw` is always called internally but has a bounded complexity. - /// - Transferring balances to accounts that did not exist before will cause - /// `T::OnNewAccount::on_new_account` to be called. - /// - Removing enough funds from an account will trigger `T::DustRemoval::on_unbalanced`. - /// - `transfer_keep_alive` works the same way as `transfer`, but has an additional - /// check that the transfer will not kill the origin account. - /// --------------------------------- - /// - Base Weight: 73.64 µs, worst case scenario (account created, account removed) - /// - DB Weight: 1 Read and 1 Write to destination account - /// - Origin account is already in memory, so no DB operations for them. - /// # - #[weight = T::WeightInfo::transfer()] - pub fn transfer( - origin, - dest: ::Source, - #[compact] value: T::Balance - ) { - let transactor = ensure_signed(origin)?; - let dest = T::Lookup::lookup(dest)?; - >::transfer(&transactor, &dest, value, ExistenceRequirement::AllowDeath)?; - } - - /// Set the balances of a given account. - /// - /// This will alter `FreeBalance` and `ReservedBalance` in storage. it will - /// also decrease the total issuance of the system (`TotalIssuance`). - /// If the new free or reserved balance is below the existential deposit, - /// it will reset the account nonce (`frame_system::AccountNonce`). - /// - /// The dispatch origin for this call is `root`. - /// - /// # - /// - Independent of the arguments. - /// - Contains a limited number of reads and writes. - /// --------------------- - /// - Base Weight: - /// - Creating: 27.56 µs - /// - Killing: 35.11 µs - /// - DB Weight: 1 Read, 1 Write to `who` - /// # - #[weight = T::WeightInfo::set_balance_creating() // Creates a new account. - .max(T::WeightInfo::set_balance_killing()) // Kills an existing account. - ] - fn set_balance( - origin, - who: ::Source, - #[compact] new_free: T::Balance, - #[compact] new_reserved: T::Balance - ) { - ensure_root(origin)?; - let who = T::Lookup::lookup(who)?; - let existential_deposit = T::ExistentialDeposit::get(); - - let wipeout = new_free + new_reserved < existential_deposit; - let new_free = if wipeout { Zero::zero() } else { new_free }; - let new_reserved = if wipeout { Zero::zero() } else { new_reserved }; - - let (free, reserved) = Self::mutate_account(&who, |account| { - if new_free > account.free { - mem::drop(PositiveImbalance::::new(new_free - account.free)); - } else if new_free < account.free { - mem::drop(NegativeImbalance::::new(account.free - new_free)); - } - - if new_reserved > account.reserved { - mem::drop(PositiveImbalance::::new(new_reserved - account.reserved)); - } else if new_reserved < account.reserved { - mem::drop(NegativeImbalance::::new(account.reserved - new_reserved)); - } - - account.free = new_free; - account.reserved = new_reserved; - - (account.free, account.reserved) - })?; - Self::deposit_event(RawEvent::BalanceSet(who, free, reserved)); - } - - /// Exactly as `transfer`, except the origin must be root and the source account may be - /// specified. - /// # - /// - Same as transfer, but additional read and write because the source account is - /// not assumed to be in the overlay. - /// # - #[weight = T::WeightInfo::force_transfer()] - pub fn force_transfer( - origin, - source: ::Source, - dest: ::Source, - #[compact] value: T::Balance - ) { - ensure_root(origin)?; - let source = T::Lookup::lookup(source)?; - let dest = T::Lookup::lookup(dest)?; - >::transfer(&source, &dest, value, ExistenceRequirement::AllowDeath)?; - } - - /// Same as the [`transfer`] call, but with a check that the transfer will not kill the - /// origin account. - /// - /// 99% of the time you want [`transfer`] instead. - /// - /// [`transfer`]: struct.Module.html#method.transfer - /// # - /// - Cheaper than transfer because account cannot be killed. - /// - Base Weight: 51.4 µs - /// - DB Weight: 1 Read and 1 Write to dest (sender is in overlay already) - /// # - #[weight = T::WeightInfo::transfer_keep_alive()] - pub fn transfer_keep_alive( - origin, - dest: ::Source, - #[compact] value: T::Balance - ) { - let transactor = ensure_signed(origin)?; - let dest = T::Lookup::lookup(dest)?; - >::transfer(&transactor, &dest, value, KeepAlive)?; +impl, I: 'static> Drop for DustCleaner { + fn drop(&mut self) { + if let Some((who, dust)) = self.0.take() { + Module::::deposit_event(Event::DustLost(who, dust.peek())); + T::DustRemoval::on_unbalanced(dust); } } } -impl, I: Instance> Module { - // PRIVATE MUTABLES - +impl, I: 'static> Pallet { /// Get the free balance of an account. pub fn free_balance(who: impl sp_std::borrow::Borrow) -> T::Balance { Self::account(who.borrow()).free @@ -601,25 +658,27 @@ impl, I: Instance> Module { T::AccountStore::get(&who) } - /// Places the `free` and `reserved` parts of `new` into `account`. Also does any steps needed - /// after mutating an account. This includes DustRemoval unbalancing, in the case than the `new` - /// account's total balance is non-zero but below ED. + /// Handles any steps needed after mutating an account. + /// + /// This includes DustRemoval unbalancing, in the case than the `new` account's total balance + /// is non-zero but below ED. /// - /// Returns the final free balance, iff the account was previously of total balance zero, known - /// as its "endowment". + /// Returns two values: + /// - `Some` containing the the `new` account, iff the account has sufficient balance. + /// - `Some` containing the dust to be dropped, iff some dust should be dropped. fn post_mutation( - who: &T::AccountId, + _who: &T::AccountId, new: AccountData, - ) -> Option> { + ) -> (Option>, Option>) { let total = new.total(); if total < T::ExistentialDeposit::get() { - if !total.is_zero() { - T::DustRemoval::on_unbalanced(NegativeImbalance::new(total)); - Self::deposit_event(RawEvent::DustLost(who.clone(), total)); + if total.is_zero() { + (None, None) + } else { + (None, Some(NegativeImbalance::new(total))) } - None } else { - Some(new) + (Some(new), None) } } @@ -651,19 +710,46 @@ impl, I: Instance> Module { who: &T::AccountId, f: impl FnOnce(&mut AccountData, bool) -> Result ) -> Result { - T::AccountStore::try_mutate_exists(who, |maybe_account| { + Self::try_mutate_account_with_dust(who, f) + .map(|(result, dust_cleaner)| { + drop(dust_cleaner); + result + }) + } + + /// Mutate an account to some new value, or delete it entirely with `None`. Will enforce + /// `ExistentialDeposit` law, annulling the account as needed. This will do nothing if the + /// result of `f` is an `Err`. + /// + /// It returns both the result from the closure, and an optional `DustCleaner` instance which + /// should be dropped once it is known that all nested mutates that could affect storage items + /// what the dust handler touches have completed. + /// + /// NOTE: Doesn't do any preparatory work for creating a new account, so should only be used + /// when it is known that the account already exists. + /// + /// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that + /// the caller will do this. + fn try_mutate_account_with_dust>( + who: &T::AccountId, + f: impl FnOnce(&mut AccountData, bool) -> Result + ) -> Result<(R, DustCleaner), E> { + let result = T::AccountStore::try_mutate_exists(who, |maybe_account| { let is_new = maybe_account.is_none(); let mut account = maybe_account.take().unwrap_or_default(); f(&mut account, is_new).map(move |result| { let maybe_endowed = if is_new { Some(account.free) } else { None }; - *maybe_account = Self::post_mutation(who, account); - (maybe_endowed, result) + let maybe_account_maybe_dust = Self::post_mutation(who, account); + *maybe_account = maybe_account_maybe_dust.0; + (maybe_endowed, maybe_account_maybe_dust.1, result) }) - }).map(|(maybe_endowed, result)| { + }); + result.map(|(maybe_endowed, maybe_dust, result)| { if let Some(endowed) = maybe_endowed { - Self::deposit_event(RawEvent::Endowed(who.clone(), endowed)); + Self::deposit_event(Event::Endowed(who.clone(), endowed)); } - result + let dust_cleaner = DustCleaner(maybe_dust.map(|dust| (who.clone(), dust))); + (result, dust_cleaner) }) } @@ -695,12 +781,12 @@ impl, I: Instance> Module { if existed { // TODO: use Locks::::hashed_key // https://github.com/paritytech/substrate/issues/4969 - system::Module::::dec_consumers(who); + system::Pallet::::dec_consumers(who); } } else { Locks::::insert(who, locks); if !existed { - if system::Module::::inc_consumers(who).is_err() { + if system::Pallet::::inc_consumers(who).is_err() { // No providers for the locks. This is impossible under normal circumstances // since the funds that are under the lock will themselves be stored in the // account and therefore will need a reference. @@ -718,8 +804,8 @@ impl, I: Instance> Module { // of the inner member. mod imbalances { use super::{ - result, DefaultInstance, Imbalance, Config, Zero, Instance, Saturating, - StorageValue, TryDrop, RuntimeDebug, + result, Imbalance, Config, Zero, Saturating, + TryDrop, RuntimeDebug, }; use sp_std::mem; @@ -727,9 +813,9 @@ mod imbalances { /// funds have been created without any equal and opposite accounting. #[must_use] #[derive(RuntimeDebug, PartialEq, Eq)] - pub struct PositiveImbalance, I: Instance=DefaultInstance>(T::Balance); + pub struct PositiveImbalance, I: 'static = ()>(T::Balance); - impl, I: Instance> PositiveImbalance { + impl, I: 'static> PositiveImbalance { /// Create a new positive imbalance from a balance. pub fn new(amount: T::Balance) -> Self { PositiveImbalance(amount) @@ -740,22 +826,22 @@ mod imbalances { /// funds have been destroyed without any equal and opposite accounting. #[must_use] #[derive(RuntimeDebug, PartialEq, Eq)] - pub struct NegativeImbalance, I: Instance=DefaultInstance>(T::Balance); + pub struct NegativeImbalance, I: 'static = ()>(T::Balance); - impl, I: Instance> NegativeImbalance { + impl, I: 'static> NegativeImbalance { /// Create a new negative imbalance from a balance. pub fn new(amount: T::Balance) -> Self { NegativeImbalance(amount) } } - impl, I: Instance> TryDrop for PositiveImbalance { + impl, I: 'static> TryDrop for PositiveImbalance { fn try_drop(self) -> result::Result<(), Self> { self.drop_zero() } } - impl, I: Instance> Imbalance for PositiveImbalance { + impl, I: 'static> Imbalance for PositiveImbalance { type Opposite = NegativeImbalance; fn zero() -> Self { @@ -800,13 +886,13 @@ mod imbalances { } } - impl, I: Instance> TryDrop for NegativeImbalance { + impl, I: 'static> TryDrop for NegativeImbalance { fn try_drop(self) -> result::Result<(), Self> { self.drop_zero() } } - impl, I: Instance> Imbalance for NegativeImbalance { + impl, I: 'static> Imbalance for NegativeImbalance { type Opposite = PositiveImbalance; fn zero() -> Self { @@ -851,7 +937,7 @@ mod imbalances { } } - impl, I: Instance> Drop for PositiveImbalance { + impl, I: 'static> Drop for PositiveImbalance { /// Basic drop handler will just square up the total issuance. fn drop(&mut self) { >::mutate( @@ -860,7 +946,7 @@ mod imbalances { } } - impl, I: Instance> Drop for NegativeImbalance { + impl, I: 'static> Drop for NegativeImbalance { /// Basic drop handler will just square up the total issuance. fn drop(&mut self) { >::mutate( @@ -870,7 +956,7 @@ mod imbalances { } } -impl, I: Instance> Currency for Module where +impl, I: 'static> Currency for Pallet where T::Balance: MaybeSerializeDeserialize + Debug { type Balance = T::Balance; @@ -932,7 +1018,7 @@ impl, I: Instance> Currency for Module where // // # // Despite iterating over a list of locks, they are limited by the number of - // lock IDs, which means the number of runtime modules that intend to use and create locks. + // lock IDs, which means the number of runtime pallets that intend to use and create locks. // # fn ensure_can_withdraw( who: &T::AccountId, @@ -956,37 +1042,43 @@ impl, I: Instance> Currency for Module where ) -> DispatchResult { if value.is_zero() || transactor == dest { return Ok(()) } - Self::try_mutate_account(dest, |to_account, _| -> DispatchResult { - Self::try_mutate_account(transactor, |from_account, _| -> DispatchResult { - from_account.free = from_account.free.checked_sub(&value) - .ok_or(Error::::InsufficientBalance)?; - - // NOTE: total stake being stored in the same type means that this could never overflow - // but better to be safe than sorry. - to_account.free = to_account.free.checked_add(&value).ok_or(Error::::Overflow)?; - - let ed = T::ExistentialDeposit::get(); - ensure!(to_account.total() >= ed, Error::::ExistentialDeposit); - - Self::ensure_can_withdraw( + Self::try_mutate_account_with_dust( + dest, + |to_account, _| -> Result, DispatchError> { + Self::try_mutate_account_with_dust( transactor, - value, - WithdrawReasons::TRANSFER, - from_account.free, - ).map_err(|_| Error::::LiquidityRestrictions)?; - - // TODO: This is over-conservative. There may now be other providers, and this module - // may not even be a provider. - let allow_death = existence_requirement == ExistenceRequirement::AllowDeath; - let allow_death = allow_death && !system::Module::::is_provider_required(transactor); - ensure!(allow_death || from_account.free >= ed, Error::::KeepAlive); - - Ok(()) - }) - })?; + |from_account, _| -> DispatchResult { + from_account.free = from_account.free.checked_sub(&value) + .ok_or(Error::::InsufficientBalance)?; + + // NOTE: total stake being stored in the same type means that this could never overflow + // but better to be safe than sorry. + to_account.free = to_account.free.checked_add(&value).ok_or(Error::::Overflow)?; + + let ed = T::ExistentialDeposit::get(); + ensure!(to_account.total() >= ed, Error::::ExistentialDeposit); + + Self::ensure_can_withdraw( + transactor, + value, + WithdrawReasons::TRANSFER, + from_account.free, + ).map_err(|_| Error::::LiquidityRestrictions)?; + + // TODO: This is over-conservative. There may now be other providers, and this pallet + // may not even be a provider. + let allow_death = existence_requirement == ExistenceRequirement::AllowDeath; + let allow_death = allow_death && !system::Pallet::::is_provider_required(transactor); + ensure!(allow_death || from_account.total() >= ed, Error::::KeepAlive); + + Ok(()) + } + ).map(|(_, maybe_dust_cleaner)| maybe_dust_cleaner) + } + )?; // Emit transfer event. - Self::deposit_event(RawEvent::Transfer(transactor.clone(), dest.clone(), value)); + Self::deposit_event(Event::Transfer(transactor.clone(), dest.clone(), value)); Ok(()) } @@ -1156,7 +1248,7 @@ impl, I: Instance> Currency for Module where } } -impl, I: Instance> ReservableCurrency for Module where +impl, I: 'static> ReservableCurrency for Pallet where T::Balance: MaybeSerializeDeserialize + Debug { /// Check if `who` can reserve `value` from their free balance. @@ -1187,7 +1279,7 @@ impl, I: Instance> ReservableCurrency for Module, I: Instance> ReservableCurrency for Module, I: Instance> ReservableCurrency for Module Result { - ensure!(!is_new, Error::::DeadAccount); - Self::try_mutate_account(slashed, |from_account, _| -> Result { - let actual = cmp::min(from_account.reserved, value); - match status { - Status::Free => to_account.free = to_account.free.checked_add(&actual).ok_or(Error::::Overflow)?, - Status::Reserved => to_account.reserved = to_account.reserved.checked_add(&actual).ok_or(Error::::Overflow)?, - } - from_account.reserved -= actual; - Ok(actual) - }) - })?; + let ((actual, _maybe_one_dust), _maybe_other_dust) = Self::try_mutate_account_with_dust( + beneficiary, + |to_account, is_new| -> Result<(Self::Balance, DustCleaner), DispatchError> { + ensure!(!is_new, Error::::DeadAccount); + Self::try_mutate_account_with_dust( + slashed, + |from_account, _| -> Result { + let actual = cmp::min(from_account.reserved, value); + match status { + Status::Free => to_account.free = to_account.free + .checked_add(&actual) + .ok_or(Error::::Overflow)?, + Status::Reserved => to_account.reserved = to_account.reserved + .checked_add(&actual) + .ok_or(Error::::Overflow)?, + } + from_account.reserved -= actual; + Ok(actual) + } + ) + } + )?; - Self::deposit_event(RawEvent::ReserveRepatriated(slashed.clone(), beneficiary.clone(), actual, status)); + Self::deposit_event(Event::ReserveRepatriated(slashed.clone(), beneficiary.clone(), actual, status)); Ok(value - actual) } } -impl, I: Instance> LockableCurrency for Module +impl, I: 'static> LockableCurrency for Pallet where T::Balance: MaybeSerializeDeserialize + Debug { diff --git a/frame/balances/src/tests.rs b/frame/balances/src/tests.rs index de7ccc6d239f9..776cda140efb8 100644 --- a/frame/balances/src/tests.rs +++ b/frame/balances/src/tests.rs @@ -26,7 +26,7 @@ macro_rules! decl_tests { use crate::*; use sp_runtime::{FixedPointNumber, traits::{SignedExtension, BadOrigin}}; use frame_support::{ - assert_noop, assert_storage_noop, assert_ok, assert_err, + assert_noop, assert_storage_noop, assert_ok, assert_err, StorageValue, traits::{ LockableCurrency, LockIdentifier, WithdrawReasons, Currency, ReservableCurrency, ExistenceRequirement::AllowDeath @@ -469,7 +469,7 @@ macro_rules! decl_tests { assert_ok!(Balances::repatriate_reserved(&1, &2, 41, Status::Free), 0); assert_eq!( last_event(), - Event::pallet_balances(RawEvent::ReserveRepatriated(1, 2, 41, Status::Free)), + Event::pallet_balances(crate::Event::ReserveRepatriated(1, 2, 41, Status::Free)), ); assert_eq!(Balances::reserved_balance(1), 69); assert_eq!(Balances::free_balance(1), 0); @@ -688,7 +688,7 @@ macro_rules! decl_tests { assert_eq!( last_event(), - Event::pallet_balances(RawEvent::Reserved(1, 10)), + Event::pallet_balances(crate::Event::Reserved(1, 10)), ); System::set_block_number(3); @@ -696,7 +696,7 @@ macro_rules! decl_tests { assert_eq!( last_event(), - Event::pallet_balances(RawEvent::Unreserved(1, 5)), + Event::pallet_balances(crate::Event::Unreserved(1, 5)), ); System::set_block_number(4); @@ -705,7 +705,7 @@ macro_rules! decl_tests { // should only unreserve 5 assert_eq!( last_event(), - Event::pallet_balances(RawEvent::Unreserved(1, 5)), + Event::pallet_balances(crate::Event::Unreserved(1, 5)), ); }); } @@ -722,8 +722,8 @@ macro_rules! decl_tests { events(), [ Event::frame_system(system::Event::NewAccount(1)), - Event::pallet_balances(RawEvent::Endowed(1, 100)), - Event::pallet_balances(RawEvent::BalanceSet(1, 100, 0)), + Event::pallet_balances(crate::Event::Endowed(1, 100)), + Event::pallet_balances(crate::Event::BalanceSet(1, 100, 0)), ] ); @@ -732,8 +732,8 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::pallet_balances(RawEvent::DustLost(1, 99)), - Event::frame_system(system::Event::KilledAccount(1)) + Event::frame_system(system::Event::KilledAccount(1)), + Event::pallet_balances(crate::Event::DustLost(1, 99)), ] ); }); @@ -751,8 +751,8 @@ macro_rules! decl_tests { events(), [ Event::frame_system(system::Event::NewAccount(1)), - Event::pallet_balances(RawEvent::Endowed(1, 100)), - Event::pallet_balances(RawEvent::BalanceSet(1, 100, 0)), + Event::pallet_balances(crate::Event::Endowed(1, 100)), + Event::pallet_balances(crate::Event::BalanceSet(1, 100, 0)), ] ); @@ -961,5 +961,18 @@ macro_rules! decl_tests { assert_storage_noop!(assert_eq!(Balances::slash(&1337, 42).1, 42)); }); } + + #[test] + fn transfer_keep_alive_all_free_succeed() { + <$ext_builder>::default() + .existential_deposit(100) + .build() + .execute_with(|| { + assert_ok!(Balances::set_balance(Origin::root(), 1, 100, 100)); + assert_ok!(Balances::transfer_keep_alive(Some(1).into(), 2, 100)); + assert_eq!(Balances::total_balance(&1), 100); + assert_eq!(Balances::total_balance(&2), 100); + }); + } } } diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index a072d2954bec1..02088e88b98ec 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -168,9 +168,9 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!( events(), [ - Event::frame_system(frame_system::Event::NewAccount(1)), - Event::pallet_balances(RawEvent::Endowed(1, 100)), - Event::pallet_balances(RawEvent::BalanceSet(1, 100, 0)), + Event::frame_system(system::Event::NewAccount(1)), + Event::pallet_balances(crate::Event::Endowed(1, 100)), + Event::pallet_balances(crate::Event::BalanceSet(1, 100, 0)), ] ); @@ -184,8 +184,8 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!( events(), [ - Event::pallet_balances(RawEvent::DustLost(1, 1)), - Event::frame_system(frame_system::Event::KilledAccount(1)) + Event::frame_system(system::Event::KilledAccount(1)), + Event::pallet_balances(crate::Event::DustLost(1, 1)), ] ); }); diff --git a/frame/balances/src/tests_reentrancy.rs b/frame/balances/src/tests_reentrancy.rs new file mode 100644 index 0000000000000..020c514b6317c --- /dev/null +++ b/frame/balances/src/tests_reentrancy.rs @@ -0,0 +1,310 @@ +// This file is part of Substrate. + +// Copyright (C) 2018-2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Test setup for potential reentracy and lost updates of nested mutations. + +#![cfg(test)] + +use sp_runtime::{ + traits::IdentityLookup, + testing::Header, +}; +use sp_core::H256; +use sp_io; +use frame_support::parameter_types; +use frame_support::traits::StorageMapShim; +use frame_support::weights::{IdentityFee}; +use crate::{ + self as pallet_balances, + Module, Config, +}; +use pallet_transaction_payment::CurrencyAdapter; + +use crate::*; +use frame_support::{ + assert_ok, + traits::{ + Currency, ReservableCurrency, + } +}; +use frame_system::RawOrigin; + +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +fn last_event() -> Event { + system::Module::::events().pop().expect("Event expected").event +} + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + } +); + +parameter_types! { + pub const BlockHashCount: u64 = 250; + pub BlockWeights: frame_system::limits::BlockWeights = + frame_system::limits::BlockWeights::simple_max(1024); + pub static ExistentialDeposit: u64 = 0; +} +impl frame_system::Config for Test { + type BaseCallFilter = (); + type BlockWeights = BlockWeights; + type BlockLength = (); + type DbWeight = (); + type Origin = Origin; + type Index = u64; + type BlockNumber = u64; + type Call = Call; + type Hash = H256; + type Hashing = ::sp_runtime::traits::BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type Header = Header; + type Event = Event; + type BlockHashCount = BlockHashCount; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = (); +} +parameter_types! { + pub const TransactionByteFee: u64 = 1; +} +impl pallet_transaction_payment::Config for Test { + type OnChargeTransaction = CurrencyAdapter, ()>; + type TransactionByteFee = TransactionByteFee; + type WeightToFee = IdentityFee; + type FeeMultiplierUpdate = (); +} + +pub struct OnDustRemoval; +impl OnUnbalanced> for OnDustRemoval { + fn on_nonzero_unbalanced(amount: NegativeImbalance) { + let _ = Balances::resolve_into_existing(&1, amount); + } +} +parameter_types! { + pub const MaxLocks: u32 = 50; +} +impl Config for Test { + type Balance = u64; + type DustRemoval = OnDustRemoval; + type Event = Event; + type ExistentialDeposit = ExistentialDeposit; + type AccountStore = StorageMapShim< + super::Account, + system::Provider, + u64, + super::AccountData, + >; + type MaxLocks = MaxLocks; + type WeightInfo = (); +} + +pub struct ExtBuilder { + existential_deposit: u64, +} +impl Default for ExtBuilder { + fn default() -> Self { + Self { + existential_deposit: 1, + } + } +} +impl ExtBuilder { + + pub fn existential_deposit(mut self, existential_deposit: u64) -> Self { + self.existential_deposit = existential_deposit; + self + } + + pub fn set_associated_consts(&self) { + EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit); + } + + pub fn build(self) -> sp_io::TestExternalities { + self.set_associated_consts(); + let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); + pallet_balances::GenesisConfig:: { + balances: vec![], + }.assimilate_storage(&mut t).unwrap(); + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| System::set_block_number(1)); + ext + } +} + +#[test] +fn transfer_dust_removal_tst1_should_work() { + ExtBuilder::default() + .existential_deposit(100) + .build() + .execute_with(|| { + // Verification of reentrancy in dust removal + assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 1000, 0)); + assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 2, 500, 0)); + + // In this transaction, account 2 free balance + // drops below existential balance + // and dust balance is removed from account 2 + assert_ok!(Balances::transfer(RawOrigin::Signed(2).into(), 3, 450)); + + // As expected dust balance is removed. + assert_eq!(Balances::free_balance(&2), 0); + + // As expected beneficiary account 3 + // received the transfered fund. + assert_eq!(Balances::free_balance(&3), 450); + + // Dust balance is deposited to account 1 + // during the process of dust removal. + assert_eq!(Balances::free_balance(&1), 1050); + + // Verify the events + // Number of events expected is 8 + assert_eq!(System::events().len(), 11); + + assert!( + System::events().iter().any( + |er| + er.event == Event::pallet_balances( + crate::Event::Transfer(2, 3, 450), + ), + ), + ); + + assert!( + System::events().iter().any( + |er| + er.event == Event::pallet_balances( + crate::Event::DustLost(2, 50) + ), + ), + ); + } + ); +} + +#[test] +fn transfer_dust_removal_tst2_should_work() { + ExtBuilder::default() + .existential_deposit(100) + .build() + .execute_with(|| { + // Verification of reentrancy in dust removal + assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 1000, 0)); + assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 2, 500, 0)); + + // In this transaction, account 2 free balance + // drops below existential balance + // and dust balance is removed from account 2 + assert_ok!(Balances::transfer(RawOrigin::Signed(2).into(), 1, 450)); + + // As expected dust balance is removed. + assert_eq!(Balances::free_balance(&2), 0); + + // Dust balance is deposited to account 1 + // during the process of dust removal. + assert_eq!(Balances::free_balance(&1), 1500); + + // Verify the events + // Number of events expected is 8 + assert_eq!(System::events().len(), 9); + + assert!( + System::events().iter().any( + |er| + er.event == Event::pallet_balances( + crate::Event::Transfer(2, 1, 450), + ), + ), + ); + + assert!( + System::events().iter().any( + |er| + er.event == Event::pallet_balances( + crate::Event::DustLost(2, 50), + ), + ), + ); + } + ); +} + +#[test] +fn repatriating_reserved_balance_dust_removal_should_work() { + ExtBuilder::default() + .existential_deposit(100) + .build() + .execute_with(|| { + // Verification of reentrancy in dust removal + assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 1000, 0)); + assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 2, 500, 0)); + + // Reserve a value on account 2, + // Such that free balance is lower than + // Exestintial deposit. + assert_ok!(Balances::reserve(&2, 450)); + + // Transfer of reserved fund from slashed account 2 to + // beneficiary account 1 + assert_ok!(Balances::repatriate_reserved(&2, &1, 450, Status::Free), 0); + + // Since free balance of account 2 is lower than + // existential deposit, dust amount is + // removed from the account 2 + assert_eq!(Balances::reserved_balance(2), 0); + assert_eq!(Balances::free_balance(2), 0); + + // account 1 is credited with reserved amount + // together with dust balance during dust + // removal. + assert_eq!(Balances::reserved_balance(1), 0); + assert_eq!(Balances::free_balance(1), 1500); + + // Verify the events + // Number of events expected is 10 + assert_eq!(System::events().len(), 10); + + assert!( + System::events().iter().any( + |er| + er.event == Event::pallet_balances( + crate::Event::ReserveRepatriated(2, 1, 450, Status::Free), + ), + ), + ); + + assert_eq!( + last_event(), + Event::pallet_balances(crate::Event::DustLost(2, 50)), + ); + + } + ); +} diff --git a/frame/balances/src/weights.rs b/frame/balances/src/weights.rs index 2b69c9c11d599..463ac7dd35c07 100644 --- a/frame/balances/src/weights.rs +++ b/frame/balances/src/weights.rs @@ -18,7 +18,7 @@ //! Autogenerated weights for pallet_balances //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0 -//! DATE: 2021-01-06, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] +//! DATE: 2021-01-06, STEPS: \[50, \], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: diff --git a/frame/benchmarking/Cargo.toml b/frame/benchmarking/Cargo.toml index e045f259be77e..41ab9efeced02 100644 --- a/frame/benchmarking/Cargo.toml +++ b/frame/benchmarking/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-benchmarking" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,19 +14,20 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] linregress = { version = "0.4.0", optional = true } -paste = "0.1" -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -sp-api = { version = "2.0.0", path = "../../primitives/api", default-features = false } -sp-runtime-interface = { version = "2.0.0", path = "../../primitives/runtime-interface", default-features = false } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime", default-features = false } -sp-std = { version = "2.0.0", path = "../../primitives/std", default-features = false } -sp-io = { version = "2.0.0", path = "../../primitives/io", default-features = false } -sp-storage = { version = "2.0.0", path = "../../primitives/storage", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +paste = "1.0" +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +sp-api = { version = "3.0.0", path = "../../primitives/api", default-features = false } +sp-runtime-interface = { version = "3.0.0", path = "../../primitives/runtime-interface", default-features = false } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime", default-features = false } +sp-std = { version = "3.0.0", path = "../../primitives/std", default-features = false } +sp-io = { version = "3.0.0", path = "../../primitives/io", default-features = false } +sp-storage = { version = "3.0.0", path = "../../primitives/storage", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] hex-literal = "0.3.1" +serde = "1.0.101" [features] default = [ "std" ] diff --git a/frame/benchmarking/src/lib.rs b/frame/benchmarking/src/lib.rs index d2cba9cc70973..94803b88b93f0 100644 --- a/frame/benchmarking/src/lib.rs +++ b/frame/benchmarking/src/lib.rs @@ -199,12 +199,12 @@ macro_rules! benchmarks_iter { { $( $where_clause:tt )* } ( $( $names:tt )* ) ( $( $names_extra:tt )* ) - where_clause { where $( $where_ty:ty: $where_bound:path ),* $(,)? } + where_clause { where $( $where_bound:tt )* } $( $rest:tt )* ) => { $crate::benchmarks_iter! { { $( $instance)? } - { $( $where_ty: $where_bound ),* } + { $( $where_bound )* } ( $( $names )* ) ( $( $names_extra )* ) $( $rest )* @@ -830,6 +830,31 @@ macro_rules! impl_benchmark { return Ok(results); } } + + /// Test a particular benchmark by name. + /// + /// This isn't called `test_benchmark_by_name` just in case some end-user eventually + /// writes a benchmark, itself called `by_name`; the function would be shadowed in + /// that case. + /// + /// This is generally intended to be used by child test modules such as those created + /// by the `impl_benchmark_test_suite` macro. However, it is not an error if a pallet + /// author chooses not to implement benchmarks. + #[cfg(test)] + #[allow(unused)] + fn test_bench_by_name(name: &[u8]) -> Result<(), &'static str> + where + T: Config + frame_system::Config, $( $where_clause )* + { + let name = sp_std::str::from_utf8(name) + .map_err(|_| "`name` is not a valid utf8 string!")?; + match name { + $( stringify!($name) => { + $crate::paste::paste! { [< test_benchmark_ $name >]::() } + } )* + _ => Err("Could not find test for requested benchmark."), + } + } }; } @@ -903,6 +928,239 @@ macro_rules! impl_benchmark_test { }; } +/// This creates a test suite which runs the module's benchmarks. +/// +/// When called in `pallet_example` as +/// +/// ```rust,ignore +/// impl_benchmark_test_suite!(Module, crate::tests::new_test_ext(), crate::tests::Test); +/// ``` +/// +/// It expands to the equivalent of: +/// +/// ```rust,ignore +/// #[cfg(test)] +/// mod tests { +/// use super::*; +/// use crate::tests::{new_test_ext, Test}; +/// use frame_support::assert_ok; +/// +/// #[test] +/// fn test_benchmarks() { +/// new_test_ext().execute_with(|| { +/// assert_ok!(test_benchmark_accumulate_dummy::()); +/// assert_ok!(test_benchmark_set_dummy::()); +/// assert_ok!(test_benchmark_another_set_dummy::()); +/// assert_ok!(test_benchmark_sort_vector::()); +/// }); +/// } +/// } +/// ``` +/// +/// ## Arguments +/// +/// The first argument, `module`, must be the path to this crate's module. +/// +/// The second argument, `new_test_ext`, must be a function call which returns either a +/// `sp_io::TestExternalities`, or some other type with a similar interface. +/// +/// Note that this function call is _not_ evaluated at compile time, but is instead copied textually +/// into each appropriate invocation site. +/// +/// The third argument, `test`, must be the path to the runtime. The item to which this must refer +/// will generally take the form: +/// +/// ```rust,ignore +/// frame_support::construct_runtime!( +/// pub enum Test where ... +/// { ... } +/// ); +/// ``` +/// +/// There is an optional fourth argument, with keyword syntax: `benchmarks_path = path_to_benchmarks_invocation`. +/// In the typical case in which this macro is in the same module as the `benchmarks!` invocation, +/// you don't need to supply this. However, if the `impl_benchmark_test_suite!` invocation is in a +/// different module than the `benchmarks!` invocation, then you should provide the path to the +/// module containing the `benchmarks!` invocation: +/// +/// ```rust,ignore +/// mod benches { +/// benchmarks!{ +/// ... +/// } +/// } +/// +/// mod tests { +/// // because of macro syntax limitations, neither Module nor benches can be paths, but both have +/// // to be idents in the scope of `impl_benchmark_test_suite`. +/// use crate::{benches, Module}; +/// +/// impl_benchmark_test_suite!(Module, new_test_ext(), Test, benchmarks_path = benches); +/// +/// // new_test_ext and the Test item are defined later in this module +/// } +/// ``` +/// +/// There is an optional fifth argument, with keyword syntax: `extra = true` or `extra = false`. +/// By default, this generates a test suite which iterates over all benchmarks, including those +/// marked with the `#[extra]` annotation. Setting `extra = false` excludes those. +/// +/// There is an optional sixth argument, with keyword syntax: `exec_name = custom_exec_name`. +/// By default, this macro uses `execute_with` for this parameter. This argument, if set, is subject +/// to these restrictions: +/// +/// - It must be the name of a method applied to the output of the `new_test_ext` argument. +/// - That method must have a signature capable of receiving a single argument of the form `impl FnOnce()`. +/// +// ## Notes (not for rustdoc) +// +// The biggest challenge for this macro is communicating the actual test functions to be run. We +// can't just build an array of function pointers to each test function and iterate over it, because +// the test functions are parameterized by the `Test` type. That's incompatible with +// monomorphization: if it were legal, then even if the compiler detected and monomorphized the +// functions into only the types of the callers, which implementation would the function pointer +// point to? There would need to be some kind of syntax for selecting the destination of the pointer +// according to a generic argument, and in general it would be a huge mess and not worth it. +// +// Instead, we're going to steal a trick from `fn run_benchmark`: generate a function which is +// itself parametrized by `Test`, which accepts a `&[u8]` parameter containing the name of the +// benchmark, and dispatches based on that to the appropriate real test implementation. Then, we can +// just iterate over the `Benchmarking::benchmarks` list to run the actual implementations. +#[macro_export] +macro_rules! impl_benchmark_test_suite { + // user might or might not have set some keyword arguments; set the defaults + // + // The weird syntax indicates that `rest` comes only after a comma, which is otherwise optional + ( + $bench_module:ident, + $new_test_ext:expr, + $test:path + $(, $( $rest:tt )* )? + ) => { + impl_benchmark_test_suite!( + @selected: + $bench_module, + $new_test_ext, + $test, + benchmarks_path = super, + extra = true, + exec_name = execute_with, + @user: + $( $( $rest )* )? + ); + }; + // pick off the benchmarks_path keyword argument + ( + @selected: + $bench_module:ident, + $new_test_ext:expr, + $test:path, + benchmarks_path = $old:ident, + extra = $extra:expr, + exec_name = $exec_name:ident, + @user: + benchmarks_path = $benchmarks_path:ident + $(, $( $rest:tt )* )? + ) => { + impl_benchmark_test_suite!( + @selected: + $bench_module, + $new_test_ext, + $test, + benchmarks_path = $benchmarks_path, + extra = $extra, + exec_name = $exec_name, + @user: + $( $( $rest )* )? + ); + }; + // pick off the extra keyword argument + ( + @selected: + $bench_module:ident, + $new_test_ext:expr, + $test:path, + benchmarks_path = $benchmarks_path:ident, + extra = $old:expr, + exec_name = $exec_name:ident, + @user: + extra = $extra:expr + $(, $( $rest:tt )* )? + ) => { + impl_benchmark_test_suite!( + @selected: + $bench_module, + $new_test_ext, + $test, + benchmarks_path = $benchmarks_path, + extra = $extra, + exec_name = $exec_name, + @user: + $( $( $rest )* )? + ); + }; + // pick off the exec_name keyword argument + ( + @selected: + $bench_module:ident, + $new_test_ext:expr, + $test:path, + benchmarks_path = $benchmarks_path:ident, + extra = $extra:expr, + exec_name = $old:ident, + @user: + exec_name = $exec_name:ident + $(, $( $rest:tt )* )? + ) => { + impl_benchmark_test_suite!( + @selected: + $bench_module, + $new_test_ext, + $test, + benchmarks_path = $benchmarks_path, + extra = $extra, + exec_name = $exec_name, + @user: + $( $( $rest )* )? + ); + }; + // all options set; nothing else in user-provided keyword arguments + ( + @selected: + $bench_module:ident, + $new_test_ext:expr, + $test:path, + benchmarks_path = $path_to_benchmarks_invocation:ident, + extra = $extra:expr, + exec_name = $exec_name:ident, + @user: + $(,)? + ) => { + #[cfg(test)] + mod benchmark_tests { + use $path_to_benchmarks_invocation::test_bench_by_name; + use super::$bench_module; + + #[test] + fn test_benchmarks() { + $new_test_ext.$exec_name(|| { + use $crate::Benchmarking; + + let mut anything_failed = false; + println!("failing benchmark tests:"); + for benchmark_name in $bench_module::<$test>::benchmarks($extra) { + if let Err(err) = std::panic::catch_unwind(|| test_bench_by_name::<$test>(benchmark_name)) { + println!("{}: {:?}", String::from_utf8_lossy(benchmark_name), err); + anything_failed = true; + } + } + assert!(!anything_failed); + }); + } + } + }; +} + /// show error message and debugging info for the case of an error happening /// during a benchmark pub fn show_benchmark_debug_info( @@ -1031,7 +1289,7 @@ macro_rules! add_benchmark { *repeat, whitelist, *verify, - ).map_err(|e| { + ).map_err(|e| { $crate::show_benchmark_debug_info( instance_string, benchmark, @@ -1058,7 +1316,7 @@ macro_rules! add_benchmark { *repeat, whitelist, *verify, - ).map_err(|e| { + ).map_err(|e| { $crate::show_benchmark_debug_info( instance_string, benchmark, diff --git a/frame/benchmarking/src/tests.rs b/frame/benchmarking/src/tests.rs index 2a2daaffbadc1..8431f3e46c277 100644 --- a/frame/benchmarking/src/tests.rs +++ b/frame/benchmarking/src/tests.rs @@ -21,59 +21,65 @@ use super::*; use sp_std::prelude::*; -use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::{H256, Header}}; -use frame_support::{ - dispatch::DispatchResult, - decl_module, decl_storage, impl_outer_origin, assert_ok, assert_err, ensure, - parameter_types, pallet_prelude::Get, -}; -use frame_system::{RawOrigin, ensure_signed, ensure_none}; - -decl_storage! { - trait Store for Module as Test where - ::OtherEvent: Into<::Event> - { - Value get(fn value): Option; - } -} +use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::{H256, Header}, BuildStorage}; +use frame_support::parameter_types; -decl_module! { - pub struct Module for enum Call where - origin: T::Origin, ::OtherEvent: Into<::Event> - { - #[weight = 0] - fn set_value(origin, n: u32) -> DispatchResult { - let _sender = ensure_signed(origin)?; - Value::put(n); - Ok(()) +mod pallet_test { + use frame_support::pallet_prelude::Get; + + frame_support::decl_storage! { + trait Store for Module as Test where + ::OtherEvent: Into<::Event> + { + pub Value get(fn value): Option; } + } - #[weight = 0] - fn dummy(origin, _n: u32) -> DispatchResult { - let _sender = ensure_none(origin)?; - Ok(()) + frame_support::decl_module! { + pub struct Module for enum Call where + origin: T::Origin, ::OtherEvent: Into<::Event> + { + #[weight = 0] + fn set_value(origin, n: u32) -> frame_support::dispatch::DispatchResult { + let _sender = frame_system::ensure_signed(origin)?; + Value::put(n); + Ok(()) + } + + #[weight = 0] + fn dummy(origin, _n: u32) -> frame_support::dispatch::DispatchResult { + let _sender = frame_system::ensure_none(origin)?; + Ok(()) + } } } -} -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} + pub trait OtherConfig { + type OtherEvent; + } -pub trait OtherConfig { - type OtherEvent; + pub trait Config: frame_system::Config + OtherConfig + where Self::OtherEvent: Into<::Event> + { + type Event; + type LowerBound: Get; + type UpperBound: Get; + } } -pub trait Config: frame_system::Config + OtherConfig - where Self::OtherEvent: Into<::Event> -{ - type Event; - type LowerBound: Get; - type UpperBound: Get; -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; -#[derive(Clone, Eq, PartialEq)] -pub struct Test; +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + TestPallet: pallet_test::{Module, Call, Storage}, + } +); impl frame_system::Config for Test { type BaseCallFilter = (); @@ -84,15 +90,15 @@ impl frame_system::Config for Test { type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = (); + type Call = Call; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -105,163 +111,178 @@ parameter_types!{ pub const UpperBound: u32 = 100; } -impl Config for Test { - type Event = (); +impl pallet_test::Config for Test { + type Event = Event; type LowerBound = LowerBound; type UpperBound = UpperBound; } -impl OtherConfig for Test { - type OtherEvent = (); +impl pallet_test::OtherConfig for Test { + type OtherEvent = Event; } fn new_test_ext() -> sp_io::TestExternalities { - frame_system::GenesisConfig::default().build_storage::().unwrap().into() + GenesisConfig::default().build_storage().unwrap().into() } -benchmarks!{ - where_clause { where ::OtherEvent: Into<::Event> } - - set_value { - let b in 1 .. 1000; - let caller = account::("caller", 0, 0); - }: _ (RawOrigin::Signed(caller), b.into()) - verify { - assert_eq!(Value::get(), Some(b)); - } - - other_name { - let b in 1 .. 1000; - }: dummy (RawOrigin::None, b.into()) - - sort_vector { - let x in 1 .. 10000; - let mut m = Vec::::new(); - for i in (0..x).rev() { - m.push(i); +mod benchmarks { + use sp_std::prelude::*; + use frame_system::RawOrigin; + use super::{Test, pallet_test::{self, Value}, new_test_ext}; + use frame_support::{assert_ok, assert_err, ensure, traits::Get, StorageValue}; + use crate::{BenchmarkingSetup, BenchmarkParameter, account}; + + // Additional used internally by the benchmark macro. + use super::pallet_test::{Call, Config, Module}; + + crate::benchmarks!{ + where_clause { + where + ::OtherEvent: Into<::Event> + Clone, + ::Event: Clone, } - }: { - m.sort(); - } verify { - ensure!(m[0] == 0, "You forgot to sort!") - } - bad_origin { - let b in 1 .. 1000; - let caller = account::("caller", 0, 0); - }: dummy (RawOrigin::Signed(caller), b.into()) - - bad_verify { - let x in 1 .. 10000; - let mut m = Vec::::new(); - for i in (0..x).rev() { - m.push(i); + set_value { + let b in 1 .. 1000; + let caller = account::("caller", 0, 0); + }: _ (RawOrigin::Signed(caller), b.into()) + verify { + assert_eq!(Value::get(), Some(b)); } - }: { } - verify { - ensure!(m[0] == 0, "You forgot to sort!") - } - no_components { - let caller = account::("caller", 0, 0); - }: set_value(RawOrigin::Signed(caller), 0) - - variable_components { - let b in ( T::LowerBound::get() ) .. T::UpperBound::get(); - }: dummy (RawOrigin::None, b.into()) -} - -#[test] -fn benchmarks_macro_works() { - // Check benchmark creation for `set_value`. - let selected = SelectedBenchmark::set_value; - - let components = >::components(&selected); - assert_eq!(components, vec![(BenchmarkParameter::b, 1, 1000)]); + other_name { + let b in 1 .. 1000; + }: dummy (RawOrigin::None, b.into()) + + sort_vector { + let x in 1 .. 10000; + let mut m = Vec::::new(); + for i in (0..x).rev() { + m.push(i); + } + }: { + m.sort(); + } verify { + ensure!(m[0] == 0, "You forgot to sort!") + } - let closure = >::instance( - &selected, - &[(BenchmarkParameter::b, 1)], - true, - ).expect("failed to create closure"); + bad_origin { + let b in 1 .. 1000; + let caller = account::("caller", 0, 0); + }: dummy (RawOrigin::Signed(caller), b.into()) + + bad_verify { + let x in 1 .. 10000; + let mut m = Vec::::new(); + for i in (0..x).rev() { + m.push(i); + } + }: { } + verify { + ensure!(m[0] == 0, "You forgot to sort!") + } - new_test_ext().execute_with(|| { - assert_ok!(closure()); - }); -} + no_components { + let caller = account::("caller", 0, 0); + }: set_value(RawOrigin::Signed(caller), 0) -#[test] -fn benchmarks_macro_rename_works() { - // Check benchmark creation for `other_dummy`. - let selected = SelectedBenchmark::other_name; - let components = >::components(&selected); - assert_eq!(components, vec![(BenchmarkParameter::b, 1, 1000)]); + variable_components { + let b in ( T::LowerBound::get() ) .. T::UpperBound::get(); + }: dummy (RawOrigin::None, b.into()) + } - let closure = >::instance( - &selected, - &[(BenchmarkParameter::b, 1)], - true, - ).expect("failed to create closure"); + #[test] + fn benchmarks_macro_works() { + // Check benchmark creation for `set_value`. + let selected = SelectedBenchmark::set_value; - new_test_ext().execute_with(|| { - assert_ok!(closure()); - }); -} + let components = >::components(&selected); + assert_eq!(components, vec![(BenchmarkParameter::b, 1, 1000)]); -#[test] -fn benchmarks_macro_works_for_non_dispatchable() { - let selected = SelectedBenchmark::sort_vector; + let closure = >::instance( + &selected, + &[(BenchmarkParameter::b, 1)], + true, + ).expect("failed to create closure"); - let components = >::components(&selected); - assert_eq!(components, vec![(BenchmarkParameter::x, 1, 10000)]); + new_test_ext().execute_with(|| { + assert_ok!(closure()); + }); + } - let closure = >::instance( - &selected, - &[(BenchmarkParameter::x, 1)], - true, - ).expect("failed to create closure"); + #[test] + fn benchmarks_macro_rename_works() { + // Check benchmark creation for `other_dummy`. + let selected = SelectedBenchmark::other_name; + let components = >::components(&selected); + assert_eq!(components, vec![(BenchmarkParameter::b, 1, 1000)]); + + let closure = >::instance( + &selected, + &[(BenchmarkParameter::b, 1)], + true, + ).expect("failed to create closure"); + + new_test_ext().execute_with(|| { + assert_ok!(closure()); + }); + } - assert_ok!(closure()); -} + #[test] + fn benchmarks_macro_works_for_non_dispatchable() { + let selected = SelectedBenchmark::sort_vector; -#[test] -fn benchmarks_macro_verify_works() { - // Check postcondition for benchmark `set_value` is valid. - let selected = SelectedBenchmark::set_value; + let components = >::components(&selected); + assert_eq!(components, vec![(BenchmarkParameter::x, 1, 10000)]); - let closure = >::instance( - &selected, - &[(BenchmarkParameter::b, 1)], - true, - ).expect("failed to create closure"); + let closure = >::instance( + &selected, + &[(BenchmarkParameter::x, 1)], + true, + ).expect("failed to create closure"); - new_test_ext().execute_with(|| { assert_ok!(closure()); - }); - - // Check postcondition for benchmark `bad_verify` is invalid. - let selected = SelectedBenchmark::bad_verify; - - let closure = >::instance( - &selected, - &[(BenchmarkParameter::x, 10000)], - true, - ).expect("failed to create closure"); + } - new_test_ext().execute_with(|| { - assert_err!(closure(), "You forgot to sort!"); - }); -} + #[test] + fn benchmarks_macro_verify_works() { + // Check postcondition for benchmark `set_value` is valid. + let selected = SelectedBenchmark::set_value; + + let closure = >::instance( + &selected, + &[(BenchmarkParameter::b, 1)], + true, + ).expect("failed to create closure"); + + new_test_ext().execute_with(|| { + assert_ok!(closure()); + }); + + // Check postcondition for benchmark `bad_verify` is invalid. + let selected = SelectedBenchmark::bad_verify; + + let closure = >::instance( + &selected, + &[(BenchmarkParameter::x, 10000)], + true, + ).expect("failed to create closure"); + + new_test_ext().execute_with(|| { + assert_err!(closure(), "You forgot to sort!"); + }); + } -#[test] -fn benchmarks_generate_unit_tests() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_set_value::()); - assert_ok!(test_benchmark_other_name::()); - assert_ok!(test_benchmark_sort_vector::()); - assert_err!(test_benchmark_bad_origin::(), "Bad origin"); - assert_err!(test_benchmark_bad_verify::(), "You forgot to sort!"); - assert_ok!(test_benchmark_no_components::()); - assert_ok!(test_benchmark_variable_components::()); - }); + #[test] + fn benchmarks_generate_unit_tests() { + new_test_ext().execute_with(|| { + assert_ok!(test_benchmark_set_value::()); + assert_ok!(test_benchmark_other_name::()); + assert_ok!(test_benchmark_sort_vector::()); + assert_err!(test_benchmark_bad_origin::(), "Bad origin"); + assert_err!(test_benchmark_bad_verify::(), "You forgot to sort!"); + assert_ok!(test_benchmark_no_components::()); + assert_ok!(test_benchmark_variable_components::()); + }); + } } diff --git a/frame/bounties/Cargo.toml b/frame/bounties/Cargo.toml index 0e37e3b9d4a57..ec4f1b94cd625 100644 --- a/frame/bounties/Cargo.toml +++ b/frame/bounties/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-bounties" -version = "2.0.0" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,20 +14,20 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-treasury = { version = "2.0.0", default-features = false, path = "../treasury" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +pallet-treasury = { version = "3.0.0", default-features = false, path = "../treasury" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-io ={ version = "2.0.0", path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-storage = { version = "2.0.0", path = "../../primitives/storage" } -pallet-balances = { version = "2.0.0", path = "../balances" } +sp-io ={ version = "3.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-storage = { version = "3.0.0", path = "../../primitives/storage" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/bounties/src/benchmarking.rs b/frame/bounties/src/benchmarking.rs index f6fc11ad0bf06..632f951f05e19 100644 --- a/frame/bounties/src/benchmarking.rs +++ b/frame/bounties/src/benchmarking.rs @@ -23,7 +23,7 @@ use super::*; use sp_runtime::traits::Bounded; use frame_system::{EventRecord, RawOrigin}; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use frame_support::traits::OnInitialize; use crate::Module as Bounties; @@ -220,26 +220,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_propose_bounty::()); - assert_ok!(test_benchmark_approve_bounty::()); - assert_ok!(test_benchmark_propose_curator::()); - assert_ok!(test_benchmark_unassign_curator::()); - assert_ok!(test_benchmark_accept_curator::()); - assert_ok!(test_benchmark_award_bounty::()); - assert_ok!(test_benchmark_claim_bounty::()); - assert_ok!(test_benchmark_close_bounty_proposed::()); - assert_ok!(test_benchmark_close_bounty_active::()); - assert_ok!(test_benchmark_extend_bounty_expiry::()); - assert_ok!(test_benchmark_spend_funds::()); - }); - } -} +impl_benchmark_test_suite!( + Bounties, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/bounties/src/tests.rs b/frame/bounties/src/tests.rs index 2f503f39b94bf..cbff502daa65e 100644 --- a/frame/bounties/src/tests.rs +++ b/frame/bounties/src/tests.rs @@ -19,12 +19,12 @@ #![cfg(test)] +use crate as pallet_bounties; use super::*; use std::cell::RefCell; use frame_support::{ - assert_noop, assert_ok, impl_outer_origin, parameter_types, weights::Weight, - impl_outer_event, traits::{OnInitialize} + assert_noop, assert_ok, parameter_types, weights::Weight, traits::OnInitialize }; use sp_core::H256; @@ -34,32 +34,29 @@ use sp_runtime::{ traits::{BlakeTwo256, IdentityLookup, BadOrigin}, }; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} - -mod bounties { - // Re-export needed for `impl_outer_event!`. - pub use crate::*; -} - -impl_outer_event! { - pub enum Event for Test { - system, - pallet_balances, - pallet_treasury, - bounties, +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Bounties: pallet_bounties::{Module, Call, Storage, Event}, + Treasury: pallet_treasury::{Module, Call, Storage, Config, Event}, } -} +); -#[derive(Clone, Eq, PartialEq)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub const MaximumBlockWeight: Weight = 1024; pub const MaximumBlockLength: u32 = 2 * 1024; pub const AvailableBlockRatio: Perbill = Perbill::one(); } + impl frame_system::Config for Test { type BaseCallFilter = (); type BlockWeights = (); @@ -68,7 +65,7 @@ impl frame_system::Config for Test { type Origin = Origin; type Index = u64; type BlockNumber = u64; - type Call = (); + type Call = Call; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = u128; // u64 is not enough to hold bytes used to generate bounty account @@ -77,7 +74,7 @@ impl frame_system::Config for Test { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -142,10 +139,8 @@ impl Config for Test { type MaximumReasonLength = MaximumReasonLength; type WeightInfo = (); } -type System = frame_system::Module; -type Balances = pallet_balances::Module; -type Treasury = pallet_treasury::Module; -type Bounties = Module; + +type TreasuryError = pallet_treasury::Error::; pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); @@ -160,7 +155,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities { fn last_event() -> RawEvent { System::events().into_iter().map(|r| r.event) .filter_map(|e| { - if let Event::bounties(inner) = e { Some(inner) } else { None } + if let Event::pallet_bounties(inner) = e { Some(inner) } else { None } }) .last() .unwrap() @@ -206,7 +201,7 @@ fn spend_proposal_fails_when_proposer_poor() { new_test_ext().execute_with(|| { assert_noop!( Treasury::propose_spend(Origin::signed(2), 100, 3), - Error::::InsufficientProposersBalance, + TreasuryError::InsufficientProposersBalance, ); }); } @@ -259,21 +254,22 @@ fn reject_already_rejected_spend_proposal_fails() { assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3)); assert_ok!(Treasury::reject_proposal(Origin::root(), 0)); - assert_noop!(Treasury::reject_proposal(Origin::root(), 0), Error::::InvalidIndex); + assert_noop!(Treasury::reject_proposal(Origin::root(), 0), TreasuryError::InvalidIndex); }); } #[test] fn reject_non_existent_spend_proposal_fails() { new_test_ext().execute_with(|| { - assert_noop!(Treasury::reject_proposal(Origin::root(), 0), Error::::InvalidIndex); + assert_noop!(Treasury::reject_proposal(Origin::root(), 0), + pallet_treasury::Error::::InvalidIndex); }); } #[test] fn accept_non_existent_spend_proposal_fails() { new_test_ext().execute_with(|| { - assert_noop!(Treasury::approve_proposal(Origin::root(), 0), Error::::InvalidIndex); + assert_noop!(Treasury::approve_proposal(Origin::root(), 0), TreasuryError::InvalidIndex); }); } @@ -284,7 +280,7 @@ fn accept_already_rejected_spend_proposal_fails() { assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3)); assert_ok!(Treasury::reject_proposal(Origin::root(), 0)); - assert_noop!(Treasury::approve_proposal(Origin::root(), 0), Error::::InvalidIndex); + assert_noop!(Treasury::approve_proposal(Origin::root(), 0), TreasuryError::InvalidIndex); }); } diff --git a/frame/chainbridge/Cargo.toml b/frame/chainbridge/Cargo.toml index fd0d982783d91..6ad966b2a8ee1 100644 --- a/frame/chainbridge/Cargo.toml +++ b/frame/chainbridge/Cargo.toml @@ -13,17 +13,19 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-balances = { version = "2.0.0", default-features = false, path = "../balances" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core", default-features = false } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +lite-json = { version = "0.1", default-features = false } +sp-keystore = { version = "0.9.0", path = "../../primitives/keystore", optional = true } +pallet-balances = { version = "3.0.0", default-features = false, path = "../balances" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [features] default = ["std"] diff --git a/frame/chainbridge/src/mock.rs b/frame/chainbridge/src/mock.rs index 4b44be36f55ba..0880dbbfb560f 100644 --- a/frame/chainbridge/src/mock.rs +++ b/frame/chainbridge/src/mock.rs @@ -40,7 +40,7 @@ impl frame_system::Config for Test { type DbWeight = (); type Version = (); // type ModuleToIndex = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; // type MaxLocks = MaxLocks; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); @@ -80,8 +80,8 @@ impl Config for Test { type ProposalLifetime = ProposalLifetime; } -pub type Block = sp_runtime::generic::Block; -pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic; +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; frame_support::construct_runtime!( pub enum Test where diff --git a/frame/collective/Cargo.toml b/frame/collective/Cargo.toml index 47f8414ef4bb6..0c58f41640100 100644 --- a/frame/collective/Cargo.toml +++ b/frame/collective/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-collective" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,18 +14,18 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] hex-literal = "0.3.1" -pallet-balances = { version = "2.0.0", path = "../balances" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/collective/src/benchmarking.rs b/frame/collective/src/benchmarking.rs index bff7dad59d891..1afdd14b1ad38 100644 --- a/frame/collective/src/benchmarking.rs +++ b/frame/collective/src/benchmarking.rs @@ -21,7 +21,12 @@ use super::*; use frame_system::RawOrigin as SystemOrigin; use frame_system::EventRecord; -use frame_benchmarking::{benchmarks_instance, account, whitelisted_caller}; +use frame_benchmarking::{ + benchmarks_instance, + account, + whitelisted_caller, + impl_benchmark_test_suite, +}; use sp_runtime::traits::Bounded; use sp_std::mem::size_of; @@ -42,7 +47,6 @@ fn assert_last_event, I: Instance>(generic_event: >: } benchmarks_instance! { - set_members { let m in 1 .. T::MaxMembers::get(); let n in 1 .. T::MaxMembers::get(); @@ -634,79 +638,8 @@ benchmarks_instance! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn set_members() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_set_members::()); - }); - } - - #[test] - fn execute() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_execute::()); - }); - } - - #[test] - fn propose_execute() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_propose_execute::()); - }); - } - - #[test] - fn propose_proposed() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_propose_proposed::()); - }); - } - - #[test] - fn vote() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_vote::()); - }); - } - - #[test] - fn close_early_disapproved() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_close_early_disapproved::()); - }); - } - - #[test] - fn close_early_approved() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_close_early_approved::()); - }); - } - - #[test] - fn close_disapproved() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_close_disapproved::()); - }); - } - - #[test] - fn close_approved() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_close_approved::()); - }); - } - - #[test] - fn disapprove_proposal() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_disapprove_proposal::()); - }); - } -} +impl_benchmark_test_suite!( + Collective, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/collective/src/lib.rs b/frame/collective/src/lib.rs index ead9135aaa19f..50beb8607d61d 100644 --- a/frame/collective/src/lib.rs +++ b/frame/collective/src/lib.rs @@ -995,7 +995,7 @@ mod tests { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); diff --git a/frame/contracts/Cargo.toml b/frame/contracts/Cargo.toml index 710258037e7aa..c5ba615504c6b 100644 --- a/frame/contracts/Cargo.toml +++ b/frame/contracts/Cargo.toml @@ -16,20 +16,20 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } pallet-contracts-primitives = { version = "2.0.0", default-features = false, path = "common" } pallet-contracts-proc-macro = { version = "0.1.0", path = "proc-macro" } parity-wasm = { version = "0.41.0", default-features = false } pwasm-utils = { version = "0.16", default-features = false } serde = { version = "1.0.101", optional = true, features = ["derive"] } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-sandbox = { version = "0.8.0", default-features = false, path = "../../primitives/sandbox" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-sandbox = { version = "0.9.0", default-features = false, path = "../../primitives/sandbox" } wasmi-validation = { version = "0.3.0", default-features = false } # Only used in benchmarking to generate random contract code @@ -39,9 +39,9 @@ rand_pcg = { version = "0.2.1", optional = true } [dev-dependencies] assert_matches = "1.3.0" hex-literal = "0.3.1" -pallet-balances = { version = "2.0.0", path = "../balances" } -pallet-timestamp = { version = "2.0.0", path = "../timestamp" } -pallet-randomness-collective-flip = { version = "2.0.0", path = "../randomness-collective-flip" } +pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-timestamp = { version = "3.0.0", path = "../timestamp" } +pallet-randomness-collective-flip = { version = "3.0.0", path = "../randomness-collective-flip" } paste = "1.0" pretty_assertions = "0.6.1" wat = "1.0" diff --git a/frame/contracts/README.md b/frame/contracts/README.md index 4252bfc1d8434..8397d2f6bf005 100644 --- a/frame/contracts/README.md +++ b/frame/contracts/README.md @@ -42,23 +42,19 @@ fails, A can decide how to handle that failure, either proceeding or reverting A ### Dispatchable functions -* `put_code` - Stores the given binary Wasm code into the chain's storage and returns its `code_hash`. -* `instantiate` - Deploys a new contract from the given `code_hash`, optionally transferring some balance. -This instantiates a new smart contract account and calls its contract deploy handler to -initialize the contract. -* `call` - Makes a call to an account, optionally transferring some balance. +Those are documented in the reference documentation of the `Module`. ## Usage The Contract module is a work in progress. The following examples show how this Contract module can be used to instantiate and call contracts. -* [`ink`](https://github.com/paritytech/ink) is +- [`ink`](https://github.com/paritytech/ink) is an [`eDSL`](https://wiki.haskell.org/Embedded_domain_specific_language) that enables writing WebAssembly based smart contracts in the Rust programming language. This is a work in progress. ## Related Modules -* [Balances](https://docs.rs/pallet-balances/latest/pallet_balances/) +- [Balances](https://docs.rs/pallet-balances/latest/pallet_balances/) License: Apache-2.0 diff --git a/frame/contracts/common/Cargo.toml b/frame/contracts/common/Cargo.toml index ff5f38637765d..f385a7ae9f2ff 100644 --- a/frame/contracts/common/Cargo.toml +++ b/frame/contracts/common/Cargo.toml @@ -16,9 +16,9 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] # This crate should not rely on any of the frame primitives. bitflags = "1.0" -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../primitives/runtime" } [features] default = ["std"] diff --git a/frame/contracts/fixtures/instantiate_return_code.wat b/frame/contracts/fixtures/instantiate_return_code.wat index cead1f1c9fa40..544489329cfad 100644 --- a/frame/contracts/fixtures/instantiate_return_code.wat +++ b/frame/contracts/fixtures/instantiate_return_code.wat @@ -10,8 +10,8 @@ (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) (import "env" "memory" (memory 1 1)) - ;; [0, 8) 100 balance - (data (i32.const 0) "\64\00\00\00\00\00\00\00") + ;; [0, 8) 10_000 balance + (data (i32.const 0) "\10\27\00\00\00\00\00\00") ;; [8, 12) here we store the return code of the transfer diff --git a/frame/contracts/rpc/Cargo.toml b/frame/contracts/rpc/Cargo.toml index 39c3b373c8cf8..06c3c7d243e0f 100644 --- a/frame/contracts/rpc/Cargo.toml +++ b/frame/contracts/rpc/Cargo.toml @@ -14,16 +14,16 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } jsonrpc-core = "15.1.0" jsonrpc-core-client = "15.1.0" jsonrpc-derive = "15.1.0" -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-rpc = { version = "2.0.0", path = "../../../primitives/rpc" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-rpc = { version = "3.0.0", path = "../../../primitives/rpc" } serde = { version = "1.0.101", features = ["derive"] } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-api = { version = "2.0.0", path = "../../../primitives/api" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } pallet-contracts-primitives = { version = "2.0.0", path = "../common" } pallet-contracts-rpc-runtime-api = { version = "0.8.0", path = "./runtime-api" } diff --git a/frame/contracts/rpc/runtime-api/Cargo.toml b/frame/contracts/rpc/runtime-api/Cargo.toml index fe1cb91b84535..0794fee292842 100644 --- a/frame/contracts/rpc/runtime-api/Cargo.toml +++ b/frame/contracts/rpc/runtime-api/Cargo.toml @@ -14,10 +14,10 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-api = { version = "2.0.0", default-features = false, path = "../../../../primitives/api" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../../primitives/runtime" } +sp-api = { version = "3.0.0", default-features = false, path = "../../../../primitives/api" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../../primitives/runtime" } pallet-contracts-primitives = { version = "2.0.0", default-features = false, path = "../../common" } [features] diff --git a/frame/contracts/src/benchmarking/code.rs b/frame/contracts/src/benchmarking/code.rs index 88e8b265a57e1..64d2a0cf011d9 100644 --- a/frame/contracts/src/benchmarking/code.rs +++ b/frame/contracts/src/benchmarking/code.rs @@ -27,12 +27,14 @@ use crate::Config; use crate::Module as Contracts; -use parity_wasm::elements::{Instruction, Instructions, FuncBody, ValueType, BlockType}; +use parity_wasm::elements::{ + Instruction, Instructions, FuncBody, ValueType, BlockType, Section, CustomSection, +}; use pwasm_utils::stack_height::inject_limiter; use sp_core::crypto::UncheckedFrom; use sp_runtime::traits::Hash; use sp_sandbox::{EnvironmentDefinitionBuilder, Memory}; -use sp_std::{prelude::*, convert::TryFrom}; +use sp_std::{prelude::*, convert::TryFrom, borrow::ToOwned}; /// Pass to `create_code` in order to create a compiled `WasmModule`. /// @@ -66,6 +68,10 @@ pub struct ModuleDefinition { pub inject_stack_metering: bool, /// Create a table containing function pointers. pub table: Option, + /// Create a section named "dummy" of the specified size. This is useful in order to + /// benchmark the overhead of loading and storing codes of specified sizes. The dummy + /// section only contributes to the size of the contract but does not affect execution. + pub dummy_section: u32, } pub struct TableSegment { @@ -103,7 +109,7 @@ pub struct ImportedFunction { pub return_type: Option, } -/// A wasm module ready to be put on chain with `put_code`. +/// A wasm module ready to be put on chain. #[derive(Clone)] pub struct WasmModule { pub code: Vec, @@ -204,6 +210,15 @@ where .build(); } + // Add the dummy section + if def.dummy_section > 0 { + contract = contract.with_section( + Section::Custom( + CustomSection::new("dummy".to_owned(), vec![42; def.dummy_section as usize]) + ) + ); + } + let mut code = contract.build(); // Inject stack height metering @@ -235,26 +250,27 @@ where ModuleDefinition::default().into() } - /// Same as `dummy` but with maximum sized linear memory. - pub fn dummy_with_mem() -> Self { + /// Same as `dummy` but with maximum sized linear memory and a dummy section of specified size. + pub fn dummy_with_bytes(dummy_bytes: u32) -> Self { ModuleDefinition { memory: Some(ImportedMemory::max::()), + dummy_section: dummy_bytes, .. Default::default() } .into() } /// Creates a wasm module of `target_bytes` size. Used to benchmark the performance of - /// `put_code` for different sizes of wasm modules. The generated module maximizes + /// `instantiate_with_code` for different sizes of wasm modules. The generated module maximizes /// instrumentation runtime by nesting blocks as deeply as possible given the byte budget. pub fn sized(target_bytes: u32) -> Self { use parity_wasm::elements::Instruction::{If, I32Const, Return, End}; - // Base size of a contract is 47 bytes and each expansion adds 6 bytes. + // Base size of a contract is 63 bytes and each expansion adds 6 bytes. // We do one expansion less to account for the code section and function body // size fields inside the binary wasm module representation which are leb128 encoded // and therefore grow in size when the contract grows. We are not allowed to overshoot - // because of the maximum code size that is enforced by `put_code`. - let expansions = (target_bytes.saturating_sub(47) / 6).saturating_sub(1); + // because of the maximum code size that is enforced by `instantiate_with_code`. + let expansions = (target_bytes.saturating_sub(63) / 6).saturating_sub(1); const EXPANSION: [Instruction; 4] = [ I32Const(0), If(BlockType::NoResult), @@ -263,6 +279,7 @@ where ]; ModuleDefinition { call_body: Some(body::repeated(expansions, &EXPANSION)), + memory: Some(ImportedMemory::max::()), .. Default::default() } .into() diff --git a/frame/contracts/src/benchmarking/mod.rs b/frame/contracts/src/benchmarking/mod.rs index 649d091880328..d01a2bce2c27b 100644 --- a/frame/contracts/src/benchmarking/mod.rs +++ b/frame/contracts/src/benchmarking/mod.rs @@ -36,12 +36,13 @@ use self::{ }, sandbox::Sandbox, }; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use frame_system::{Module as System, RawOrigin}; use parity_wasm::elements::{Instruction, ValueType, BlockType}; use sp_runtime::traits::{Hash, Bounded, Zero}; use sp_std::{default::Default, convert::{TryInto}, vec::Vec, vec}; use pallet_contracts_primitives::RentProjection; +use frame_support::weights::Weight; /// How many batches we do per API benchmark. const API_BENCHMARK_BATCHES: u32 = 20; @@ -137,7 +138,7 @@ where // same block number. System::::set_block_number(1u32.into()); - Contracts::::put_code_raw(module.code)?; + Contracts::::store_code_raw(module.code)?; Contracts::::instantiate( RawOrigin::Signed(caller.clone()).into(), endowment, @@ -198,7 +199,7 @@ where /// Get the block number when this contract will be evicted. Returns an error when /// the rent collection won't happen because the contract has to much endowment. fn eviction_at(&self) -> Result { - let projection = Rent::::compute_projection(&self.account_id) + let projection = Rent::>::compute_projection(&self.account_id) .map_err(|_| "Invalid acc for rent")?; match projection { RentProjection::EvictionAt(at) => Ok(at), @@ -250,7 +251,7 @@ where /// Evict this contract. fn evict(&mut self) -> Result<(), &'static str> { self.set_block_num_for_eviction()?; - Rent::::try_eviction(&self.contract.account_id, Zero::zero())?; + Rent::>::try_eviction(&self.contract.account_id, Zero::zero())?; self.contract.ensure_tombstone() } } @@ -304,6 +305,19 @@ benchmarks! { Storage::::process_deletion_queue_batch(Weight::max_value()) } + // This benchmarks the additional weight that is charged when a contract is executed the + // first time after a new schedule was deployed: For every new schedule a contract needs + // to re-run the instrumentation once. + instrument { + let c in 0 .. T::MaxCodeSize::get() / 1024; + let WasmModule { code, hash, .. } = WasmModule::::sized(c * 1024); + Contracts::::store_code_raw(code)?; + let mut module = PrefabWasmModule::from_storage_noinstr(hash)?; + let schedule = Contracts::::current_schedule(); + }: { + Contracts::::reinstrument_module(&mut module, &schedule)?; + } + // This extrinsic is pretty much constant as it is only a simple setter. update_schedule { let schedule = Schedule { @@ -314,33 +328,50 @@ benchmarks! { // This constructs a contract that is maximal expensive to instrument. // It creates a maximum number of metering blocks per byte. - // `n`: Size of the code in kilobytes. - put_code { - let n in 0 .. Contracts::::current_schedule().limits.code_size / 1024; + // The size of the salt influences the runtime because is is hashed in order to + // determine the contract address. + // `c`: Size of the code in kilobytes. + // `s`: Size of the salt in kilobytes. + // + // # Note + // + // We cannot let `c` grow to the maximum code size because the code is not allowed + // to be larger than the maximum size **after instrumentation**. + instantiate_with_code { + let c in 0 .. Perbill::from_percent(50).mul_ceil(T::MaxCodeSize::get() / 1024); + let s in 0 .. code::max_pages::() * 64; + let salt = vec![42u8; (s * 1024) as usize]; + let endowment = caller_funding::() / 3u32.into(); let caller = whitelisted_caller(); T::Currency::make_free_balance_be(&caller, caller_funding::()); - let module = WasmModule::::sized(n * 1024); - let origin = RawOrigin::Signed(caller); - }: _(origin, module.code) + let WasmModule { code, hash, .. } = WasmModule::::sized(c * 1024); + let origin = RawOrigin::Signed(caller.clone()); + let addr = Contracts::::contract_address(&caller, &hash, &salt); + }: _(origin, endowment, Weight::max_value(), code, vec![], salt) + verify { + // endowment was removed from the caller + assert_eq!(T::Currency::free_balance(&caller), caller_funding::() - endowment); + // contract has the full endowment because no rent collection happended + assert_eq!(T::Currency::free_balance(&addr), endowment); + // instantiate should leave a alive contract + Contract::::address_alive_info(&addr)?; + } // Instantiate uses a dummy contract constructor to measure the overhead of the instantiate. - // The size of the input data influences the runtime because it is hashed in order to determine - // the contract address. - // `n`: Size of the data passed to constructor in kilobytes. + // `c`: Size of the code in kilobytes. // `s`: Size of the salt in kilobytes. instantiate { - let n in 0 .. code::max_pages::() * 64; + let c in 0 .. T::MaxCodeSize::get() / 1024; let s in 0 .. code::max_pages::() * 64; - let data = vec![42u8; (n * 1024) as usize]; let salt = vec![42u8; (s * 1024) as usize]; let endowment = caller_funding::() / 3u32.into(); let caller = whitelisted_caller(); T::Currency::make_free_balance_be(&caller, caller_funding::()); - let WasmModule { code, hash, .. } = WasmModule::::dummy_with_mem(); + let WasmModule { code, hash, .. } = WasmModule::::dummy_with_bytes(c * 1024); let origin = RawOrigin::Signed(caller.clone()); let addr = Contracts::::contract_address(&caller, &hash, &salt); - Contracts::::put_code_raw(code)?; - }: _(origin, endowment, Weight::max_value(), hash, data, salt) + Contracts::::store_code_raw(code)?; + }: _(origin, endowment, Weight::max_value(), hash, vec![], salt) verify { // endowment was removed from the caller assert_eq!(T::Currency::free_balance(&caller), caller_funding::() - endowment); @@ -355,10 +386,12 @@ benchmarks! { // won't call `seal_input` in its constructor to copy the data to contract memory. // The dummy contract used here does not do this. The costs for the data copy is billed as // part of `seal_input`. + // `c`: Size of the code in kilobytes. call { + let c in 0 .. T::MaxCodeSize::get() / 1024; let data = vec![42u8; 1024]; let instance = Contract::::with_caller( - whitelisted_caller(), WasmModule::dummy_with_mem(), vec![], Endow::CollectRent + whitelisted_caller(), WasmModule::dummy_with_bytes(c * 1024), vec![], Endow::CollectRent )?; let value = T::Currency::minimum_balance() * 100u32.into(); let origin = RawOrigin::Signed(instance.caller.clone()); @@ -386,9 +419,11 @@ benchmarks! { // will be distributed over multiple blocks using a scheduler. Otherwise there is // no incentive to remove large contracts when the removal is more expensive than // the reward for removing them. + // `c`: Size of the code of the contract that should be evicted. claim_surcharge { + let c in 0 .. T::MaxCodeSize::get() / 1024; let instance = Contract::::with_caller( - whitelisted_caller(), WasmModule::dummy(), vec![], Endow::CollectRent + whitelisted_caller(), WasmModule::dummy_with_bytes(c * 1024), vec![], Endow::CollectRent )?; let origin = RawOrigin::Signed(instance.caller.clone()); let account_id = instance.account_id.clone(); @@ -684,6 +719,42 @@ benchmarks! { } } + seal_terminate_per_code_kb { + let c in 0 .. T::MaxCodeSize::get() / 1024; + let beneficiary = account::("beneficiary", 0, 0); + let beneficiary_bytes = beneficiary.encode(); + let beneficiary_len = beneficiary_bytes.len(); + let code = WasmModule::::from(ModuleDefinition { + memory: Some(ImportedMemory::max::()), + imported_functions: vec![ImportedFunction { + name: "seal_terminate", + params: vec![ValueType::I32, ValueType::I32], + return_type: None, + }], + data_segments: vec![ + DataSegment { + offset: 0, + value: beneficiary_bytes, + }, + ], + call_body: Some(body::repeated(1, &[ + Instruction::I32Const(0), // beneficiary_ptr + Instruction::I32Const(beneficiary_len as i32), // beneficiary_len + Instruction::Call(0), + ])), + dummy_section: c * 1024, + .. Default::default() + }); + let instance = Contract::::new(code, vec![], Endow::Max)?; + let origin = RawOrigin::Signed(instance.caller.clone()); + assert_eq!(T::Currency::total_balance(&beneficiary), 0u32.into()); + assert_eq!(T::Currency::total_balance(&instance.account_id), Endow::max::()); + }: call(origin, instance.addr, 0u32.into(), Weight::max_value(), vec![]) + verify { + assert_eq!(T::Currency::total_balance(&instance.account_id), 0u32.into()); + assert_eq!(T::Currency::total_balance(&beneficiary), Endow::max::()); + } + seal_restore_to { let r in 0 .. 1; @@ -762,9 +833,16 @@ benchmarks! { } } - seal_restore_to_per_delta { + // `c`: Code size of caller contract + // `t`: Code size of tombstone contract + // `d`: Number of supplied delta keys + seal_restore_to_per_code_kb_delta { + let c in 0 .. T::MaxCodeSize::get() / 1024; + let t in 0 .. T::MaxCodeSize::get() / 1024; let d in 0 .. API_BENCHMARK_BATCHES; - let mut tombstone = ContractWithStorage::::new(0, 0)?; + let mut tombstone = ContractWithStorage::::with_code( + WasmModule::::dummy_with_bytes(t * 1024), 0, 0 + )?; tombstone.evict()?; let delta = create_storage::(d * API_BENCHMARK_BATCH_SIZE, T::MaxValueSize::get())?; @@ -827,6 +905,7 @@ benchmarks! { Instruction::Call(0), Instruction::End, ])), + dummy_section: c * 1024, .. Default::default() }); @@ -1169,7 +1248,7 @@ benchmarks! { .collect::>(); let account_len = accounts.get(0).map(|i| i.encode().len()).unwrap_or(0); let account_bytes = accounts.iter().flat_map(|x| x.encode()).collect(); - let value = ConfigCache::::subsistence_threshold_uncached(); + let value = Contracts::::subsistence_threshold(); assert!(value > 0u32.into()); let value_bytes = value.encode(); let value_len = value_bytes.len(); @@ -1215,7 +1294,7 @@ benchmarks! { // We call unique accounts. seal_call { let r in 0 .. API_BENCHMARK_BATCHES; - let dummy_code = WasmModule::::dummy_with_mem(); + let dummy_code = WasmModule::::dummy_with_bytes(0); let callees = (0..r * API_BENCHMARK_BATCH_SIZE) .map(|i| Contract::with_index(i + 1, dummy_code.clone(), vec![], Endow::Max)) .collect::, _>>()?; @@ -1270,7 +1349,8 @@ benchmarks! { let origin = RawOrigin::Signed(instance.caller.clone()); }: call(origin, instance.addr, 0u32.into(), Weight::max_value(), vec![]) - seal_call_per_transfer_input_output_kb { + seal_call_per_code_transfer_input_output_kb { + let c in 0 .. T::MaxCodeSize::get() / 1024; let t in 0 .. 1; let i in 0 .. code::max_pages::() * 64; let o in 0 .. (code::max_pages::() - 1) * 64; @@ -1292,6 +1372,7 @@ benchmarks! { Instruction::Call(0), Instruction::End, ])), + dummy_section: c * 1024, .. Default::default() }); let callees = (0..API_BENCHMARK_BATCH_SIZE) @@ -1369,7 +1450,7 @@ benchmarks! { ])), .. Default::default() }); - Contracts::::put_code_raw(code.code)?; + Contracts::::store_code_raw(code.code)?; Ok(code.hash) }) .collect::, &'static str>>()?; @@ -1465,7 +1546,8 @@ benchmarks! { } } - seal_instantiate_per_input_output_salt_kb { + seal_instantiate_per_code_input_output_salt_kb { + let c in 0 .. T::MaxCodeSize::get() / 1024; let i in 0 .. (code::max_pages::() - 1) * 64; let o in 0 .. (code::max_pages::() - 1) * 64; let s in 0 .. (code::max_pages::() - 1) * 64; @@ -1487,12 +1569,13 @@ benchmarks! { Instruction::Call(0), Instruction::End, ])), + dummy_section: c * 1024, .. Default::default() }); let hash = callee_code.hash.clone(); let hash_bytes = callee_code.hash.encode(); let hash_len = hash_bytes.len(); - Contracts::::put_code_raw(callee_code.code)?; + Contracts::::store_code_raw(callee_code.code)?; let inputs = (0..API_BENCHMARK_BATCH_SIZE).map(|x| x.encode()).collect::>(); let input_len = inputs.get(0).map(|x| x.len()).unwrap_or(0); let input_bytes = inputs.iter().cloned().flatten().collect::>(); @@ -2430,127 +2513,8 @@ benchmarks! { }: {} } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{ExtBuilder, Test}; - use frame_support::assert_ok; - use paste::paste; - - macro_rules! create_test { - ($name:ident) => { - #[test] - fn $name() { - ExtBuilder::default().build().execute_with(|| { - assert_ok!(paste!{ - []::() - }); - }); - } - } - } - - create_test!(on_initialize); - create_test!(on_initialize_per_trie_key); - create_test!(on_initialize_per_queue_item); - - create_test!(update_schedule); - create_test!(put_code); - create_test!(instantiate); - create_test!(call); - create_test!(claim_surcharge); - - create_test!(seal_caller); - create_test!(seal_address); - create_test!(seal_gas_left); - create_test!(seal_balance); - create_test!(seal_value_transferred); - create_test!(seal_minimum_balance); - create_test!(seal_tombstone_deposit); - create_test!(seal_rent_allowance); - create_test!(seal_block_number); - create_test!(seal_now); - create_test!(seal_weight_to_fee); - create_test!(seal_gas); - create_test!(seal_input); - create_test!(seal_input_per_kb); - create_test!(seal_return); - create_test!(seal_return_per_kb); - create_test!(seal_terminate); - create_test!(seal_restore_to); - create_test!(seal_restore_to_per_delta); - create_test!(seal_random); - create_test!(seal_deposit_event); - create_test!(seal_deposit_event_per_topic_and_kb); - create_test!(seal_set_rent_allowance); - create_test!(seal_set_storage); - create_test!(seal_set_storage_per_kb); - create_test!(seal_get_storage); - create_test!(seal_get_storage_per_kb); - create_test!(seal_transfer); - create_test!(seal_call); - create_test!(seal_call_per_transfer_input_output_kb); - create_test!(seal_instantiate); - create_test!(seal_instantiate_per_input_output_salt_kb); - create_test!(seal_clear_storage); - create_test!(seal_hash_sha2_256); - create_test!(seal_hash_sha2_256_per_kb); - create_test!(seal_hash_keccak_256); - create_test!(seal_hash_keccak_256_per_kb); - create_test!(seal_hash_blake2_256); - create_test!(seal_hash_blake2_256_per_kb); - create_test!(seal_hash_blake2_128); - create_test!(seal_hash_blake2_128_per_kb); - - create_test!(instr_i64const); - create_test!(instr_i64load); - create_test!(instr_i64store); - create_test!(instr_select); - create_test!(instr_if); - create_test!(instr_br); - create_test!(instr_br_if); - create_test!(instr_br_table); - create_test!(instr_br_table_per_entry); - create_test!(instr_call); - create_test!(instr_call_indirect); - create_test!(instr_call_indirect_per_param); - create_test!(instr_local_get); - create_test!(instr_local_set); - create_test!(instr_local_tee); - create_test!(instr_global_get); - create_test!(instr_global_set); - create_test!(instr_memory_current); - create_test!(instr_memory_grow); - create_test!(instr_i64clz); - create_test!(instr_i64ctz); - create_test!(instr_i64popcnt); - create_test!(instr_i64eqz); - create_test!(instr_i64extendsi32); - create_test!(instr_i64extendui32); - create_test!(instr_i32wrapi64); - create_test!(instr_i64eq); - create_test!(instr_i64ne); - create_test!(instr_i64lts); - create_test!(instr_i64ltu); - create_test!(instr_i64gts); - create_test!(instr_i64gtu); - create_test!(instr_i64les); - create_test!(instr_i64leu); - create_test!(instr_i64ges); - create_test!(instr_i64geu); - create_test!(instr_i64add); - create_test!(instr_i64sub); - create_test!(instr_i64mul); - create_test!(instr_i64divs); - create_test!(instr_i64divu); - create_test!(instr_i64rems); - create_test!(instr_i64remu); - create_test!(instr_i64and); - create_test!(instr_i64or); - create_test!(instr_i64xor); - create_test!(instr_i64shl); - create_test!(instr_i64shrs); - create_test!(instr_i64shru); - create_test!(instr_i64rotl); - create_test!(instr_i64rotr); -} +impl_benchmark_test_suite!( + Contracts, + crate::tests::ExtBuilder::default().build(), + crate::tests::Test, +); diff --git a/frame/contracts/src/chain_extension.rs b/frame/contracts/src/chain_extension.rs index 662cfb2053e6e..dc6e9771775ca 100644 --- a/frame/contracts/src/chain_extension.rs +++ b/frame/contracts/src/chain_extension.rs @@ -18,7 +18,7 @@ //! A mechanism for runtime authors to augment the functionality of contracts. //! //! The runtime is able to call into any contract and retrieve the result using -//! [`bare_call`](crate::Module::bare_call). This already allows customization of runtime +//! [`bare_call`](crate::Pallet::bare_call). This already allows customization of runtime //! behaviour by user generated code (contracts). However, often it is more straightforward //! to allow the reverse behaviour: The contract calls into the runtime. We call the latter //! one a "chain extension" because it allows the chain to extend the set of functions that are @@ -37,7 +37,7 @@ //! [`charge_weight`](Environment::charge_weight) function must be called **before** //! carrying out any action that causes the consumption of the chargeable weight. //! It cannot be overstated how delicate of a process the creation of a chain extension -//! is. Check whether using [`bare_call`](crate::Module::bare_call) suffices for the +//! is. Check whether using [`bare_call`](crate::Pallet::bare_call) suffices for the //! use case at hand. //! //! # Benchmarking @@ -63,7 +63,7 @@ use sp_std::{ pub use frame_system::Config as SysConfig; pub use pallet_contracts_primitives::ReturnFlags; pub use sp_core::crypto::UncheckedFrom; -pub use crate::exec::Ext; +pub use crate::{Config, exec::Ext}; pub use state::Init as InitState; /// Result that returns a [`DispatchError`] on error. @@ -74,7 +74,7 @@ pub type Result = sp_std::result::Result; /// In order to create a custom chain extension this trait must be implemented and supplied /// to the pallet contracts configuration trait as the associated type of the same name. /// Consult the [module documentation](self) for a general explanation of chain extensions. -pub trait ChainExtension { +pub trait ChainExtension { /// Call the chain extension logic. /// /// This is the only function that needs to be implemented in order to write a @@ -88,11 +88,12 @@ pub trait ChainExtension { /// /// # Return /// - /// In case of `Err` the contract execution is immediatly suspended and the passed error + /// In case of `Err` the contract execution is immediately suspended and the passed error /// is returned to the caller. Otherwise the value of [`RetVal`] determines the exit /// behaviour. - fn call(func_id: u32, env: Environment) -> Result + fn call(func_id: u32, env: Environment) -> Result where + E: Ext, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>; /// Determines whether chain extensions are enabled for this chain. @@ -108,9 +109,10 @@ pub trait ChainExtension { } /// Implementation that indicates that no chain extension is available. -impl ChainExtension for () { - fn call(_func_id: u32, mut _env: Environment) -> Result +impl ChainExtension for () { + fn call(_func_id: u32, mut _env: Environment) -> Result where + E: Ext, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>, { // Never called since [`Self::enabled()`] is set to `false`. Because we want to @@ -326,7 +328,7 @@ where /// /// If the contract supplied buffer is smaller than the passed `buffer` an `Err` is returned. /// If `allow_skip` is set to true the contract is allowed to skip the copying of the buffer - /// by supplying the guard value of [`u32::max_value()`] as `out_ptr`. The + /// by supplying the guard value of `u32::max_value()` as `out_ptr`. The /// `weight_per_byte` is only charged when the write actually happens and is not skipped or /// failed due to a too small output buffer. pub fn write( diff --git a/frame/contracts/src/exec.rs b/frame/contracts/src/exec.rs index 05eaf52c1bc9b..745384a8674bc 100644 --- a/frame/contracts/src/exec.rs +++ b/frame/contracts/src/exec.rs @@ -16,18 +16,21 @@ // limitations under the License. use crate::{ - CodeHash, ConfigCache, Event, RawEvent, Config, Module as Contracts, + CodeHash, Event, Config, Module as Contracts, TrieId, BalanceOf, ContractInfo, gas::GasMeter, rent::Rent, storage::{self, Storage}, - Error, ContractInfoOf + Error, ContractInfoOf, Schedule, }; use sp_core::crypto::UncheckedFrom; -use sp_std::prelude::*; +use sp_std::{ + prelude::*, + marker::PhantomData, +}; use sp_runtime::traits::{Bounded, Zero, Convert, Saturating}; use frame_support::{ - dispatch::DispatchResult, - traits::{ExistenceRequirement, Currency, Time, Randomness}, + dispatch::{DispatchResult, DispatchError}, + traits::{ExistenceRequirement, Currency, Time, Randomness, Get}, weights::Weight, - ensure, StorageMap, + ensure, }; use pallet_contracts_primitives::{ErrorOrigin, ExecError, ExecReturnValue, ExecResult, ReturnFlags}; @@ -54,7 +57,11 @@ pub enum TransactorKind { /// /// This interface is specialized to an account of the executing code, so all /// operations are implicitly performed on that account. -pub trait Ext { +/// +/// # Note +/// +/// This trait is sealed and cannot be implemented by downstream crates. +pub trait Ext: sealing::Sealed { type T: Config; /// Returns the storage entry of the executing account by the given `key`. @@ -69,16 +76,21 @@ pub trait Ext { /// Instantiate a contract from the given code. /// + /// Returns the original code size of the called contract. /// The newly created account will be associated with `code`. `value` specifies the amount of value /// transferred from this to the newly created account (also known as endowment). + /// + /// # Return Value + /// + /// Result<(AccountId, ExecReturnValue, CodeSize), (ExecError, CodeSize)> fn instantiate( &mut self, - code: &CodeHash, + code: CodeHash, value: BalanceOf, gas_meter: &mut GasMeter, input_data: Vec, salt: &[u8], - ) -> Result<(AccountIdOf, ExecReturnValue), ExecError>; + ) -> Result<(AccountIdOf, ExecReturnValue, u32), (ExecError, u32)>; /// Transfer some amount of funds into the specified account. fn transfer( @@ -89,24 +101,35 @@ pub trait Ext { /// Transfer all funds to `beneficiary` and delete the contract. /// + /// Returns the original code size of the terminated contract. /// Since this function removes the self contract eagerly, if succeeded, no further actions should /// be performed on this `Ext` instance. /// /// This function will fail if the same contract is present on the contract /// call stack. + /// + /// # Return Value + /// + /// Result fn terminate( &mut self, beneficiary: &AccountIdOf, - ) -> DispatchResult; + ) -> Result; /// Call (possibly transferring some amount of funds) into the specified account. + /// + /// Returns the original code size of the called contract. + /// + /// # Return Value + /// + /// Result<(ExecReturnValue, CodeSize), (ExecError, CodeSize)> fn call( &mut self, to: &AccountIdOf, value: BalanceOf, gas_meter: &mut GasMeter, input_data: Vec, - ) -> ExecResult; + ) -> Result<(ExecReturnValue, u32), (ExecError, u32)>; /// Restores the given destination contract sacrificing the current one. /// @@ -115,13 +138,17 @@ pub trait Ext { /// /// This function will fail if the same contract is present /// on the contract call stack. + /// + /// # Return Value + /// + /// Result<(CallerCodeSize, DestCodeSize), (DispatchError, CallerCodeSize, DestCodesize)> fn restore_to( &mut self, dest: AccountIdOf, code_hash: CodeHash, rent_allowance: BalanceOf, delta: Vec, - ) -> DispatchResult; + ) -> Result<(u32, u32), (DispatchError, u32, u32)>; /// Returns a reference to the account id of the caller. fn caller(&self) -> &AccountIdOf; @@ -168,159 +195,208 @@ pub trait Ext { /// Returns the price for the specified amount of weight. fn get_weight_price(&self, weight: Weight) -> BalanceOf; + + /// Get a reference to the schedule used by the current call. + fn schedule(&self) -> &Schedule; } -/// Loader is a companion of the `Vm` trait. It loads an appropriate abstract -/// executable to be executed by an accompanying `Vm` implementation. -pub trait Loader { - type Executable; - - /// Load the initializer portion of the code specified by the `code_hash`. This - /// executable is called upon instantiation. - fn load_init(&self, code_hash: &CodeHash) -> Result; - /// Load the main portion of the code specified by the `code_hash`. This executable - /// is called for each call to a contract. - fn load_main(&self, code_hash: &CodeHash) -> Result; +/// Describes the different functions that can be exported by an [`Executable`]. +pub enum ExportedFunction { + /// The constructor function which is executed on deployment of a contract. + Constructor, + /// The function which is executed when a contract is called. + Call, } -/// A trait that represent a virtual machine. +/// A trait that represents something that can be executed. /// -/// You can view a virtual machine as something that takes code, an input data buffer, -/// queries it and/or performs actions on the given `Ext` and optionally -/// returns an output data buffer. The type of code depends on the particular virtual machine. -/// -/// Execution of code can end by either implicit termination (that is, reached the end of -/// executable), explicit termination via returning a buffer or termination due to a trap. -pub trait Vm { - type Executable; +/// In the on-chain environment this would be represented by a wasm module. This trait exists in +/// order to be able to mock the wasm logic for testing. +pub trait Executable: Sized { + /// Load the executable from storage. + fn from_storage( + code_hash: CodeHash, + schedule: &Schedule, + gas_meter: &mut GasMeter, + ) -> Result; + + /// Load the module from storage without re-instrumenting it. + /// + /// A code module is re-instrumented on-load when it was originally instrumented with + /// an older schedule. This skips this step for cases where the code storage is + /// queried for purposes other than execution. + fn from_storage_noinstr(code_hash: CodeHash) -> Result; + + /// Decrements the refcount by one and deletes the code if it drops to zero. + fn drop_from_storage(self); + /// Increment the refcount by one. Fails if the code does not exist on-chain. + /// + /// Returns the size of the original code. + fn add_user(code_hash: CodeHash) -> Result; + + /// Decrement the refcount by one and remove the code when it drops to zero. + /// + /// Returns the size of the original code. + fn remove_user(code_hash: CodeHash) -> u32; + + /// Execute the specified exported function and return the result. + /// + /// When the specified function is `Constructor` the executable is stored and its + /// refcount incremented. + /// + /// # Note + /// + /// This functions expects to be executed in a storage transaction that rolls back + /// all of its emitted storage changes. fn execute>( - &self, - exec: &Self::Executable, + self, ext: E, + function: &ExportedFunction, input_data: Vec, gas_meter: &mut GasMeter, ) -> ExecResult; + + /// The code hash of the executable. + fn code_hash(&self) -> &CodeHash; + + /// The storage that is occupied by the instrumented executable and its pristine source. + /// + /// The returned size is already divided by the number of users who share the code. + /// + /// # Note + /// + /// This works with the current in-memory value of refcount. When calling any contract + /// without refetching this from storage the result can be inaccurate as it might be + /// working with a stale value. Usually this inaccuracy is tolerable. + fn occupied_storage(&self) -> u32; + + /// Size of the instrumented code in bytes. + fn code_len(&self) -> u32; } -pub struct ExecutionContext<'a, T: Config + 'a, V, L> { - pub caller: Option<&'a ExecutionContext<'a, T, V, L>>, +pub struct ExecutionContext<'a, T: Config + 'a, E> { + pub caller: Option<&'a ExecutionContext<'a, T, E>>, pub self_account: T::AccountId, pub self_trie_id: Option, pub depth: usize, - pub config: &'a ConfigCache, - pub vm: &'a V, - pub loader: &'a L, + pub schedule: &'a Schedule, pub timestamp: MomentOf, pub block_number: T::BlockNumber, + _phantom: PhantomData, } -impl<'a, T, E, V, L> ExecutionContext<'a, T, V, L> +impl<'a, T, E> ExecutionContext<'a, T, E> where T: Config, T::AccountId: UncheckedFrom + AsRef<[u8]>, - L: Loader, - V: Vm, + E: Executable, { /// Create the top level execution context. /// /// The specified `origin` address will be used as `sender` for. The `origin` must be a regular /// account (not a contract). - pub fn top_level(origin: T::AccountId, cfg: &'a ConfigCache, vm: &'a V, loader: &'a L) -> Self { + pub fn top_level(origin: T::AccountId, schedule: &'a Schedule) -> Self { ExecutionContext { caller: None, self_trie_id: None, self_account: origin, depth: 0, - config: &cfg, - vm: &vm, - loader: &loader, + schedule, timestamp: T::Time::now(), block_number: >::block_number(), + _phantom: Default::default(), } } fn nested<'b, 'c: 'b>(&'c self, dest: T::AccountId, trie_id: TrieId) - -> ExecutionContext<'b, T, V, L> + -> ExecutionContext<'b, T, E> { ExecutionContext { caller: Some(self), self_trie_id: Some(trie_id), self_account: dest, depth: self.depth + 1, - config: self.config, - vm: self.vm, - loader: self.loader, + schedule: self.schedule, timestamp: self.timestamp.clone(), block_number: self.block_number.clone(), + _phantom: Default::default(), } } /// Make a call to the specified address, optionally transferring some funds. + /// + /// # Return Value + /// + /// Result<(ExecReturnValue, CodeSize), (ExecError, CodeSize)> pub fn call( &mut self, dest: T::AccountId, value: BalanceOf, gas_meter: &mut GasMeter, input_data: Vec, - ) -> ExecResult { - if self.depth == self.config.max_depth as usize { - Err(Error::::MaxCallDepthReached)? + ) -> Result<(ExecReturnValue, u32), (ExecError, u32)> { + if self.depth == T::MaxDepth::get() as usize { + return Err((Error::::MaxCallDepthReached.into(), 0)); } + let contract = >::get(&dest) + .and_then(|contract| contract.get_alive()) + .ok_or((Error::::NotCallable.into(), 0))?; + + let executable = E::from_storage(contract.code_hash, &self.schedule, gas_meter) + .map_err(|e| (e.into(), 0))?; + let code_len = executable.code_len(); + // This charges the rent and denies access to a contract that is in need of // eviction by returning `None`. We cannot evict eagerly here because those // changes would be rolled back in case this contract is called by another // contract. // See: https://github.com/paritytech/substrate/issues/6439#issuecomment-648754324 - let contract = if let Ok(Some(ContractInfo::Alive(info))) = Rent::::charge(&dest) { - info - } else { - Err(Error::::NotCallable)? - }; + let contract = Rent::::charge(&dest, contract, executable.occupied_storage()) + .map_err(|e| (e.into(), code_len))? + .ok_or((Error::::NotCallable.into(), code_len))?; let transactor_kind = self.transactor_kind(); let caller = self.self_account.clone(); - self.with_nested_context(dest.clone(), contract.trie_id.clone(), |nested| { + let result = self.with_nested_context(dest.clone(), contract.trie_id.clone(), |nested| { if value > BalanceOf::::zero() { - transfer( + transfer::( TransferCause::Call, transactor_kind, &caller, &dest, value, - nested, )? } - let executable = nested.loader.load_main(&contract.code_hash) - .map_err(|_| Error::::CodeNotFound)?; - let output = nested.vm.execute( - &executable, + let output = executable.execute( nested.new_call_context(caller, value), + &ExportedFunction::Call, input_data, gas_meter, ).map_err(|e| ExecError { error: e.error, origin: ErrorOrigin::Callee })?; Ok(output) - }) + }).map_err(|e| (e, code_len))?; + Ok((result, code_len)) } pub fn instantiate( &mut self, endowment: BalanceOf, gas_meter: &mut GasMeter, - code_hash: &CodeHash, + executable: E, input_data: Vec, salt: &[u8], ) -> Result<(T::AccountId, ExecReturnValue), ExecError> { - if self.depth == self.config.max_depth as usize { + if self.depth == T::MaxDepth::get() as usize { Err(Error::::MaxCallDepthReached)? } let transactor_kind = self.transactor_kind(); let caller = self.self_account.clone(); - let dest = Contracts::::contract_address(&caller, code_hash, salt); + let dest = Contracts::::contract_address(&caller, executable.code_hash(), salt); let output = frame_support::storage::with_transaction(|| { // Generate the trie id in a new transaction to only increment the counter on success. @@ -333,42 +409,48 @@ where .self_trie_id .clone() .expect("the nested context always has to have self_trie_id"), - code_hash.clone() + executable.code_hash().clone() )?; // Send funds unconditionally here. If the `endowment` is below existential_deposit // then error will be returned here. - transfer( + transfer::( TransferCause::Instantiate, transactor_kind, &caller, &dest, endowment, - nested, )?; - let executable = nested.loader.load_init(&code_hash) - .map_err(|_| Error::::CodeNotFound)?; - let output = nested.vm - .execute( - &executable, - nested.new_call_context(caller.clone(), endowment), - input_data, - gas_meter, - ).map_err(|e| ExecError { error: e.error, origin: ErrorOrigin::Callee })?; - + // Cache the value before calling into the constructor because that + // consumes the value. If the constructor creates additional contracts using + // the same code hash we still charge the "1 block rent" as if they weren't + // spawned. This is OK as overcharging is always safe. + let occupied_storage = executable.occupied_storage(); + + let output = executable.execute( + nested.new_call_context(caller.clone(), endowment), + &ExportedFunction::Constructor, + input_data, + gas_meter, + ).map_err(|e| ExecError { error: e.error, origin: ErrorOrigin::Callee })?; + + // We need to re-fetch the contract because changes are written to storage + // eagerly during execution. + let contract = >::get(&dest) + .and_then(|contract| contract.get_alive()) + .ok_or(Error::::NotCallable)?; // Collect the rent for the first block to prevent the creation of very large // contracts that never intended to pay for even one block. // This also makes sure that it is above the subsistence threshold // in order to keep up the guarantuee that we always leave a tombstone behind // with the exception of a contract that called `seal_terminate`. - Rent::::charge(&dest)? - .and_then(|c| c.get_alive()) - .ok_or_else(|| Error::::NewContractNotFunded)?; + Rent::::charge(&dest, contract, occupied_storage)? + .ok_or(Error::::NewContractNotFunded)?; // Deposit an instantiation event. - deposit_event::(vec![], RawEvent::Instantiated(caller.clone(), dest.clone())); + deposit_event::(vec![], Event::Instantiated(caller.clone(), dest.clone())); Ok(output) }); @@ -387,7 +469,7 @@ where &'b mut self, caller: T::AccountId, value: BalanceOf, - ) -> CallContext<'b, 'a, T, V, L> { + ) -> CallContext<'b, 'a, T, E> { let timestamp = self.timestamp.clone(); let block_number = self.block_number.clone(); CallContext { @@ -396,13 +478,14 @@ where value_transferred: value, timestamp, block_number, + _phantom: Default::default(), } } /// Execute the given closure within a nested execution context. fn with_nested_context(&mut self, dest: T::AccountId, trie_id: TrieId, func: F) -> ExecResult - where F: FnOnce(&mut ExecutionContext) -> ExecResult + where F: FnOnce(&mut ExecutionContext) -> ExecResult { use frame_support::storage::TransactionOutcome::*; let mut nested = self.nested(dest, trie_id); @@ -447,13 +530,12 @@ enum TransferCause { /// is specified as `Terminate`. Otherwise, any transfer that would bring the sender below the /// subsistence threshold (for contracts) or the existential deposit (for plain accounts) /// results in an error. -fn transfer<'a, T: Config, V: Vm, L: Loader>( +fn transfer( cause: TransferCause, origin: TransactorKind, transactor: &T::AccountId, dest: &T::AccountId, value: BalanceOf, - ctx: &mut ExecutionContext<'a, T, V, L>, ) -> DispatchResult where T::AccountId: UncheckedFrom + AsRef<[u8]>, @@ -468,7 +550,7 @@ where (_, Contract) => { ensure!( T::Currency::total_balance(transactor).saturating_sub(value) >= - ctx.config.subsistence_threshold(), + Contracts::::subsistence_threshold(), Error::::BelowSubsistenceThreshold, ); ExistenceRequirement::KeepAlive @@ -493,20 +575,20 @@ where /// implies that the control won't be returned to the contract anymore, but there is still some code /// on the path of the return from that call context. Therefore, care must be taken in these /// situations. -struct CallContext<'a, 'b: 'a, T: Config + 'b, V: Vm + 'b, L: Loader> { - ctx: &'a mut ExecutionContext<'b, T, V, L>, +struct CallContext<'a, 'b: 'a, T: Config + 'b, E> { + ctx: &'a mut ExecutionContext<'b, T, E>, caller: T::AccountId, value_transferred: BalanceOf, timestamp: MomentOf, block_number: T::BlockNumber, + _phantom: PhantomData, } -impl<'a, 'b: 'a, T, E, V, L> Ext for CallContext<'a, 'b, T, V, L> +impl<'a, 'b: 'a, T, E> Ext for CallContext<'a, 'b, T, E> where T: Config + 'b, T::AccountId: UncheckedFrom + AsRef<[u8]>, - V: Vm, - L: Loader, + E: Executable, { type T = T; @@ -537,13 +619,18 @@ where fn instantiate( &mut self, - code_hash: &CodeHash, + code_hash: CodeHash, endowment: BalanceOf, gas_meter: &mut GasMeter, input_data: Vec, salt: &[u8], - ) -> Result<(AccountIdOf, ExecReturnValue), ExecError> { - self.ctx.instantiate(endowment, gas_meter, code_hash, input_data, salt) + ) -> Result<(AccountIdOf, ExecReturnValue, u32), (ExecError, u32)> { + let executable = E::from_storage(code_hash, &self.ctx.schedule, gas_meter) + .map_err(|e| (e.into(), 0))?; + let code_len = executable.code_len(); + self.ctx.instantiate(endowment, gas_meter, executable, input_data, salt) + .map(|r| (r.0, r.1, code_len)) + .map_err(|e| (e, code_len)) } fn transfer( @@ -551,38 +638,38 @@ where to: &T::AccountId, value: BalanceOf, ) -> DispatchResult { - transfer( + transfer::( TransferCause::Call, TransactorKind::Contract, &self.ctx.self_account.clone(), to, value, - self.ctx, ) } fn terminate( &mut self, beneficiary: &AccountIdOf, - ) -> DispatchResult { + ) -> Result { let self_id = self.ctx.self_account.clone(); let value = T::Currency::free_balance(&self_id); if let Some(caller_ctx) = self.ctx.caller { if caller_ctx.is_live(&self_id) { - return Err(Error::::ReentranceDenied.into()); + return Err((Error::::ReentranceDenied.into(), 0)); } } - transfer( + transfer::( TransferCause::Terminate, TransactorKind::Contract, &self_id, beneficiary, value, - self.ctx, - )?; + ).map_err(|e| (e, 0))?; if let Some(ContractInfo::Alive(info)) = ContractInfoOf::::take(&self_id) { - Storage::::queue_trie_for_deletion(&info)?; - Ok(()) + Storage::::queue_trie_for_deletion(&info).map_err(|e| (e, 0))?; + let code_len = E::remove_user(info.code_hash); + Contracts::::deposit_event(Event::Terminated(self_id, beneficiary.clone())); + Ok(code_len) } else { panic!( "this function is only invoked by in the context of a contract;\ @@ -598,7 +685,7 @@ where value: BalanceOf, gas_meter: &mut GasMeter, input_data: Vec, - ) -> ExecResult { + ) -> Result<(ExecReturnValue, u32), (ExecError, u32)> { self.ctx.call(to.clone(), value, gas_meter, input_data) } @@ -608,14 +695,14 @@ where code_hash: CodeHash, rent_allowance: BalanceOf, delta: Vec, - ) -> DispatchResult { + ) -> Result<(u32, u32), (DispatchError, u32, u32)> { if let Some(caller_ctx) = self.ctx.caller { if caller_ctx.is_live(&self.ctx.self_account) { - return Err(Error::::ReentranceDenied.into()); + return Err((Error::::ReentranceDenied.into(), 0, 0)); } } - let result = Rent::::restore_to( + let result = Rent::::restore_to( self.ctx.self_account.clone(), dest.clone(), code_hash.clone(), @@ -625,7 +712,7 @@ where if let Ok(_) = result { deposit_event::( vec![], - RawEvent::Restored( + Event::Restored( self.ctx.self_account.clone(), dest, code_hash, @@ -661,17 +748,17 @@ where } fn minimum_balance(&self) -> BalanceOf { - self.ctx.config.existential_deposit + T::Currency::minimum_balance() } fn tombstone_deposit(&self) -> BalanceOf { - self.ctx.config.tombstone_deposit + T::TombstoneDeposit::get() } fn deposit_event(&mut self, topics: Vec, data: Vec) { deposit_event::( topics, - RawEvent::ContractExecution(self.ctx.self_account.clone(), data) + Event::ContractEmitted(self.ctx.self_account.clone(), data) ); } @@ -694,12 +781,16 @@ where fn block_number(&self) -> T::BlockNumber { self.block_number } fn max_value_size(&self) -> u32 { - self.ctx.config.max_value_size + T::MaxValueSize::get() } fn get_weight_price(&self, weight: Weight) -> BalanceOf { T::WeightPrice::convert(weight) } + + fn schedule(&self) -> &Schedule { + &self.ctx.schedule + } } fn deposit_event( @@ -712,6 +803,20 @@ fn deposit_event( ) } +mod sealing { + use super::*; + + pub trait Sealed {} + + impl<'a, 'b: 'a, T: Config, E> Sealed for CallContext<'a, 'b, T, E> {} + + #[cfg(test)] + impl Sealed for crate::wasm::MockExt {} + + #[cfg(test)] + impl Sealed for &mut crate::wasm::MockExt {} +} + /// These tests exercise the executive layer. /// /// In these tests the VM/loader are mocked. Instead of dealing with wasm bytecode they use simple closures. @@ -719,30 +824,33 @@ fn deposit_event( /// wasm VM code. #[cfg(test)] mod tests { - use super::{ - BalanceOf, Event, ExecResult, ExecutionContext, Ext, Loader, - RawEvent, Vm, ReturnFlags, ExecError, ErrorOrigin, AccountIdOf, - }; + use super::*; use crate::{ - gas::GasMeter, tests::{ExtBuilder, Test, MetaEvent}, - exec::ExecReturnValue, CodeHash, ConfigCache, - gas::Gas, + gas::GasMeter, tests::{ExtBuilder, Test, Event as MetaEvent}, storage::Storage, - tests::{ALICE, BOB, CHARLIE}, - Error, + tests::{ + ALICE, BOB, CHARLIE, + test_utils::{place_contract, set_balance, get_balance}, + }, + Error, Weight, }; - use crate::tests::test_utils::{place_contract, set_balance, get_balance}; use sp_runtime::DispatchError; use assert_matches::assert_matches; - use std::{cell::RefCell, collections::HashMap, marker::PhantomData, rc::Rc}; + use std::{cell::RefCell, collections::HashMap, rc::Rc}; + + type MockContext<'a> = ExecutionContext<'a, Test, MockExecutable>; + + const GAS_LIMIT: Weight = 10_000_000_000; - const GAS_LIMIT: Gas = 10_000_000_000; + thread_local! { + static LOADER: RefCell = RefCell::new(MockLoader::default()); + } fn events() -> Vec> { >::events() .into_iter() .filter_map(|meta| match meta.event { - MetaEvent::contracts(contract_event) => Some(contract_event), + MetaEvent::pallet_contracts(contract_event) => Some(contract_event), _ => None, }) .collect() @@ -755,80 +863,79 @@ mod tests { } #[derive(Clone)] - struct MockExecutable<'a>(Rc ExecResult + 'a>); - - impl<'a> MockExecutable<'a> { - fn new(f: impl Fn(MockCtx) -> ExecResult + 'a) -> Self { - MockExecutable(Rc::new(f)) - } - } + struct MockExecutable(Rc ExecResult + 'static>, CodeHash); - struct MockLoader<'a> { - map: HashMap, MockExecutable<'a>>, + #[derive(Default)] + struct MockLoader { + map: HashMap, MockExecutable>, counter: u64, } - impl<'a> MockLoader<'a> { - fn empty() -> Self { - MockLoader { - map: HashMap::new(), - counter: 0, - } - } - - fn insert(&mut self, f: impl Fn(MockCtx) -> ExecResult + 'a) -> CodeHash { - // Generate code hashes as monotonically increasing values. - let code_hash = ::Hash::from_low_u64_be(self.counter); - - self.counter += 1; - self.map.insert(code_hash, MockExecutable::new(f)); - code_hash + impl MockLoader { + fn insert(f: impl Fn(MockCtx) -> ExecResult + 'static) -> CodeHash { + LOADER.with(|loader| { + let mut loader = loader.borrow_mut(); + // Generate code hashes as monotonically increasing values. + let hash = ::Hash::from_low_u64_be(loader.counter); + loader.counter += 1; + loader.map.insert(hash, MockExecutable (Rc::new(f), hash.clone())); + hash + }) } } - struct MockVm<'a> { - _marker: PhantomData<&'a ()>, - } + impl Executable for MockExecutable { + fn from_storage( + code_hash: CodeHash, + _schedule: &Schedule, + _gas_meter: &mut GasMeter, + ) -> Result { + Self::from_storage_noinstr(code_hash) + } - impl<'a> MockVm<'a> { - fn new() -> Self { - MockVm { _marker: PhantomData } + fn from_storage_noinstr(code_hash: CodeHash) -> Result { + LOADER.with(|loader| { + loader.borrow_mut() + .map + .get(&code_hash) + .cloned() + .ok_or(Error::::CodeNotFound.into()) + }) } - } - impl<'a> Loader for MockLoader<'a> { - type Executable = MockExecutable<'a>; + fn drop_from_storage(self) {} - fn load_init(&self, code_hash: &CodeHash) -> Result { - self.map - .get(code_hash) - .cloned() - .ok_or_else(|| "code not found") - } - fn load_main(&self, code_hash: &CodeHash) -> Result { - self.map - .get(code_hash) - .cloned() - .ok_or_else(|| "code not found") + fn add_user(_code_hash: CodeHash) -> Result { + Ok(0) } - } - impl<'a> Vm for MockVm<'a> { - type Executable = MockExecutable<'a>; + fn remove_user(_code_hash: CodeHash) -> u32 { 0 } fn execute>( - &self, - exec: &MockExecutable, + self, mut ext: E, + _function: &ExportedFunction, input_data: Vec, gas_meter: &mut GasMeter, ) -> ExecResult { - (exec.0)(MockCtx { + (self.0)(MockCtx { ext: &mut ext, input_data, gas_meter, }) } + + fn code_hash(&self) -> &CodeHash { + &self.1 + } + + fn occupied_storage(&self) -> u32 { + 0 + } + + fn code_len(&self) -> u32 { + 0 + } } fn exec_success() -> ExecResult { @@ -837,32 +944,29 @@ mod tests { #[test] fn it_works() { + thread_local! { + static TEST_DATA: RefCell> = RefCell::new(vec![0]); + } + let value = Default::default(); let mut gas_meter = GasMeter::::new(GAS_LIMIT); - let data = vec![]; - - let vm = MockVm::new(); - - let test_data = Rc::new(RefCell::new(vec![0usize])); - - let mut loader = MockLoader::empty(); - let exec_ch = loader.insert(|_ctx| { - test_data.borrow_mut().push(1); + let exec_ch = MockLoader::insert(|_ctx| { + TEST_DATA.with(|data| data.borrow_mut().push(1)); exec_success() }); ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); place_contract(&BOB, exec_ch); assert_matches!( - ctx.call(BOB, value, &mut gas_meter, data), + ctx.call(BOB, value, &mut gas_meter, vec![]), Ok(_) ); }); - assert_eq!(&*test_data.borrow(), &vec![0, 1]); + TEST_DATA.with(|data| assert_eq!(*data.borrow(), vec![0, 1])); } #[test] @@ -872,22 +976,16 @@ mod tests { let origin = ALICE; let dest = BOB; - let vm = MockVm::new(); - let loader = MockLoader::empty(); - ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(origin.clone(), &cfg, &vm, &loader); set_balance(&origin, 100); set_balance(&dest, 0); - super::transfer( + super::transfer::( super::TransferCause::Call, super::TransactorKind::PlainAccount, &origin, &dest, 55, - &mut ctx, ).unwrap(); assert_eq!(get_balance(&origin), 45); @@ -902,15 +1000,13 @@ mod tests { let origin = ALICE; let dest = BOB; - let vm = MockVm::new(); - let mut loader = MockLoader::empty(); - let return_ch = loader.insert( + let return_ch = MockLoader::insert( |_| Ok(ExecReturnValue { flags: ReturnFlags::REVERT, data: Vec::new() }) ); ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(origin.clone(), &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(origin.clone(), &schedule); place_contract(&BOB, return_ch); set_balance(&origin, 100); let balance = get_balance(&dest); @@ -922,7 +1018,7 @@ mod tests { vec![], ).unwrap(); - assert!(!output.is_success()); + assert!(!output.0.is_success()); assert_eq!(get_balance(&origin), 100); // the rent is still charged @@ -937,21 +1033,15 @@ mod tests { let origin = ALICE; let dest = BOB; - let vm = MockVm::new(); - let loader = MockLoader::empty(); - ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(origin.clone(), &cfg, &vm, &loader); set_balance(&origin, 0); - let result = super::transfer( + let result = super::transfer::( super::TransferCause::Call, super::TransactorKind::PlainAccount, &origin, &dest, 100, - &mut ctx, ); assert_eq!( @@ -969,16 +1059,13 @@ mod tests { // is returned from the execution context. let origin = ALICE; let dest = BOB; - - let vm = MockVm::new(); - let mut loader = MockLoader::empty(); - let return_ch = loader.insert( + let return_ch = MockLoader::insert( |_| Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: vec![1, 2, 3, 4] }) ); ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(origin, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(origin, &schedule); place_contract(&BOB, return_ch); let result = ctx.call( @@ -989,8 +1076,8 @@ mod tests { ); let output = result.unwrap(); - assert!(output.is_success()); - assert_eq!(output.data, vec![1, 2, 3, 4]); + assert!(output.0.is_success()); + assert_eq!(output.0.data, vec![1, 2, 3, 4]); }); } @@ -1000,16 +1087,13 @@ mod tests { // is returned from the execution context. let origin = ALICE; let dest = BOB; - - let vm = MockVm::new(); - let mut loader = MockLoader::empty(); - let return_ch = loader.insert( + let return_ch = MockLoader::insert( |_| Ok(ExecReturnValue { flags: ReturnFlags::REVERT, data: vec![1, 2, 3, 4] }) ); ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(origin, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(origin, &schedule); place_contract(&BOB, return_ch); let result = ctx.call( @@ -1020,24 +1104,22 @@ mod tests { ); let output = result.unwrap(); - assert!(!output.is_success()); - assert_eq!(output.data, vec![1, 2, 3, 4]); + assert!(!output.0.is_success()); + assert_eq!(output.0.data, vec![1, 2, 3, 4]); }); } #[test] fn input_data_to_call() { - let vm = MockVm::new(); - let mut loader = MockLoader::empty(); - let input_data_ch = loader.insert(|ctx| { + let input_data_ch = MockLoader::insert(|ctx| { assert_eq!(ctx.input_data, &[1, 2, 3, 4]); exec_success() }); // This one tests passing the input data into a contract via call. ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); place_contract(&BOB, input_data_ch); let result = ctx.call( @@ -1052,24 +1134,27 @@ mod tests { #[test] fn input_data_to_instantiate() { - let vm = MockVm::new(); - let mut loader = MockLoader::empty(); - let input_data_ch = loader.insert(|ctx| { + let input_data_ch = MockLoader::insert(|ctx| { assert_eq!(ctx.input_data, &[1, 2, 3, 4]); exec_success() }); // This one tests passing the input data into a contract via instantiate. ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let subsistence = Contracts::::subsistence_threshold(); + let mut ctx = MockContext::top_level(ALICE, &schedule); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = MockExecutable::from_storage( + input_data_ch, &schedule, &mut gas_meter + ).unwrap(); - set_balance(&ALICE, cfg.subsistence_threshold() * 10); + set_balance(&ALICE, subsistence * 10); let result = ctx.instantiate( - cfg.subsistence_threshold() * 3, - &mut GasMeter::::new(GAS_LIMIT), - &input_data_ch, + subsistence * 3, + &mut gas_meter, + executable, vec![1, 2, 3, 4], &[], ); @@ -1081,35 +1166,36 @@ mod tests { fn max_depth() { // This test verifies that when we reach the maximal depth creation of an // yet another context fails. + thread_local! { + static REACHED_BOTTOM: RefCell = RefCell::new(false); + } let value = Default::default(); - let reached_bottom = RefCell::new(false); - - let vm = MockVm::new(); - let mut loader = MockLoader::empty(); - let recurse_ch = loader.insert(|ctx| { + let recurse_ch = MockLoader::insert(|ctx| { // Try to call into yourself. let r = ctx.ext.call(&BOB, 0, ctx.gas_meter, vec![]); - let mut reached_bottom = reached_bottom.borrow_mut(); - if !*reached_bottom { - // We are first time here, it means we just reached bottom. - // Verify that we've got proper error and set `reached_bottom`. - assert_eq!( - r, - Err(Error::::MaxCallDepthReached.into()) - ); - *reached_bottom = true; - } else { - // We just unwinding stack here. - assert_matches!(r, Ok(_)); - } + REACHED_BOTTOM.with(|reached_bottom| { + let mut reached_bottom = reached_bottom.borrow_mut(); + if !*reached_bottom { + // We are first time here, it means we just reached bottom. + // Verify that we've got proper error and set `reached_bottom`. + assert_eq!( + r, + Err((Error::::MaxCallDepthReached.into(), 0)) + ); + *reached_bottom = true; + } else { + // We just unwinding stack here. + assert_matches!(r, Ok(_)); + } + }); exec_success() }); ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); set_balance(&BOB, 1); place_contract(&BOB, recurse_ch); @@ -1129,15 +1215,16 @@ mod tests { let origin = ALICE; let dest = BOB; - let vm = MockVm::new(); - - let witnessed_caller_bob = RefCell::new(None::>); - let witnessed_caller_charlie = RefCell::new(None::>); + thread_local! { + static WITNESSED_CALLER_BOB: RefCell>> = RefCell::new(None); + static WITNESSED_CALLER_CHARLIE: RefCell>> = RefCell::new(None); + } - let mut loader = MockLoader::empty(); - let bob_ch = loader.insert(|ctx| { + let bob_ch = MockLoader::insert(|ctx| { // Record the caller for bob. - *witnessed_caller_bob.borrow_mut() = Some(ctx.ext.caller().clone()); + WITNESSED_CALLER_BOB.with(|caller| + *caller.borrow_mut() = Some(ctx.ext.caller().clone()) + ); // Call into CHARLIE contract. assert_matches!( @@ -1146,16 +1233,17 @@ mod tests { ); exec_success() }); - let charlie_ch = loader.insert(|ctx| { + let charlie_ch = MockLoader::insert(|ctx| { // Record the caller for charlie. - *witnessed_caller_charlie.borrow_mut() = Some(ctx.ext.caller().clone()); + WITNESSED_CALLER_CHARLIE.with(|caller| + *caller.borrow_mut() = Some(ctx.ext.caller().clone()) + ); exec_success() }); ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - - let mut ctx = ExecutionContext::top_level(origin.clone(), &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(origin.clone(), &schedule); place_contract(&dest, bob_ch); place_contract(&CHARLIE, charlie_ch); @@ -1169,16 +1257,13 @@ mod tests { assert_matches!(result, Ok(_)); }); - assert_eq!(&*witnessed_caller_bob.borrow(), &Some(origin)); - assert_eq!(&*witnessed_caller_charlie.borrow(), &Some(dest)); + WITNESSED_CALLER_BOB.with(|caller| assert_eq!(*caller.borrow(), Some(origin))); + WITNESSED_CALLER_CHARLIE.with(|caller| assert_eq!(*caller.borrow(), Some(dest))); } #[test] fn address_returns_proper_values() { - let vm = MockVm::new(); - - let mut loader = MockLoader::empty(); - let bob_ch = loader.insert(|ctx| { + let bob_ch = MockLoader::insert(|ctx| { // Verify that address matches BOB. assert_eq!(*ctx.ext.address(), BOB); @@ -1189,14 +1274,14 @@ mod tests { ); exec_success() }); - let charlie_ch = loader.insert(|ctx| { + let charlie_ch = MockLoader::insert(|ctx| { assert_eq!(*ctx.ext.address(), CHARLIE); exec_success() }); ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); place_contract(&BOB, bob_ch); place_contract(&CHARLIE, charlie_ch); @@ -1213,20 +1298,21 @@ mod tests { #[test] fn refuse_instantiate_with_value_below_existential_deposit() { - let vm = MockVm::new(); - - let mut loader = MockLoader::empty(); - let dummy_ch = loader.insert(|_| exec_success()); + let dummy_ch = MockLoader::insert(|_| exec_success()); ExtBuilder::default().existential_deposit(15).build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = MockExecutable::from_storage( + dummy_ch, &schedule, &mut gas_meter + ).unwrap(); assert_matches!( ctx.instantiate( 0, // <- zero endowment - &mut GasMeter::::new(GAS_LIMIT), - &dummy_ch, + &mut gas_meter, + executable, vec![], &[], ), @@ -1237,23 +1323,24 @@ mod tests { #[test] fn instantiation_work_with_success_output() { - let vm = MockVm::new(); - - let mut loader = MockLoader::empty(); - let dummy_ch = loader.insert( + let dummy_ch = MockLoader::insert( |_| Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: vec![80, 65, 83, 83] }) ); ExtBuilder::default().existential_deposit(15).build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = MockExecutable::from_storage( + dummy_ch, &schedule, &mut gas_meter + ).unwrap(); set_balance(&ALICE, 1000); let instantiated_contract_address = assert_matches!( ctx.instantiate( 100, - &mut GasMeter::::new(GAS_LIMIT), - &dummy_ch, + &mut gas_meter, + executable, vec![], &[], ), @@ -1264,30 +1351,31 @@ mod tests { // there are instantiation event. assert_eq!(Storage::::code_hash(&instantiated_contract_address).unwrap(), dummy_ch); assert_eq!(&events(), &[ - RawEvent::Instantiated(ALICE, instantiated_contract_address) + Event::Instantiated(ALICE, instantiated_contract_address) ]); }); } #[test] fn instantiation_fails_with_failing_output() { - let vm = MockVm::new(); - - let mut loader = MockLoader::empty(); - let dummy_ch = loader.insert( + let dummy_ch = MockLoader::insert( |_| Ok(ExecReturnValue { flags: ReturnFlags::REVERT, data: vec![70, 65, 73, 76] }) ); ExtBuilder::default().existential_deposit(15).build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = MockExecutable::from_storage( + dummy_ch, &schedule, &mut gas_meter + ).unwrap(); set_balance(&ALICE, 1000); let instantiated_contract_address = assert_matches!( ctx.instantiate( 100, - &mut GasMeter::::new(GAS_LIMIT), - &dummy_ch, + &mut gas_meter, + executable, vec![], &[], ), @@ -1302,19 +1390,16 @@ mod tests { #[test] fn instantiation_from_contract() { - let vm = MockVm::new(); - - let mut loader = MockLoader::empty(); - let dummy_ch = loader.insert(|_| exec_success()); + let dummy_ch = MockLoader::insert(|_| exec_success()); let instantiated_contract_address = Rc::new(RefCell::new(None::>)); - let instantiator_ch = loader.insert({ + let instantiator_ch = MockLoader::insert({ let dummy_ch = dummy_ch.clone(); let instantiated_contract_address = Rc::clone(&instantiated_contract_address); move |ctx| { // Instantiate a contract and save it's address in `instantiated_contract_address`. - let (address, output) = ctx.ext.instantiate( - &dummy_ch, - ConfigCache::::subsistence_threshold_uncached() * 3, + let (address, output, _) = ctx.ext.instantiate( + dummy_ch, + Contracts::::subsistence_threshold() * 3, ctx.gas_meter, vec![], &[48, 49, 50], @@ -1326,9 +1411,9 @@ mod tests { }); ExtBuilder::default().existential_deposit(15).build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); - set_balance(&ALICE, cfg.subsistence_threshold() * 100); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); + set_balance(&ALICE, Contracts::::subsistence_threshold() * 100); place_contract(&BOB, instantiator_ch); assert_matches!( @@ -1342,35 +1427,32 @@ mod tests { // there are instantiation event. assert_eq!(Storage::::code_hash(&instantiated_contract_address).unwrap(), dummy_ch); assert_eq!(&events(), &[ - RawEvent::Instantiated(BOB, instantiated_contract_address) + Event::Instantiated(BOB, instantiated_contract_address) ]); }); } #[test] fn instantiation_traps() { - let vm = MockVm::new(); - - let mut loader = MockLoader::empty(); - let dummy_ch = loader.insert( + let dummy_ch = MockLoader::insert( |_| Err("It's a trap!".into()) ); - let instantiator_ch = loader.insert({ + let instantiator_ch = MockLoader::insert({ let dummy_ch = dummy_ch.clone(); move |ctx| { // Instantiate a contract and save it's address in `instantiated_contract_address`. assert_matches!( ctx.ext.instantiate( - &dummy_ch, + dummy_ch, 15u64, ctx.gas_meter, vec![], &[], ), - Err(ExecError { + Err((ExecError { error: DispatchError::Other("It's a trap!"), origin: ErrorOrigin::Callee, - }) + }, 0)) ); exec_success() @@ -1378,8 +1460,8 @@ mod tests { }); ExtBuilder::default().existential_deposit(15).build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); set_balance(&ALICE, 1000); set_balance(&BOB, 100); place_contract(&BOB, instantiator_ch); @@ -1397,11 +1479,7 @@ mod tests { #[test] fn termination_from_instantiate_fails() { - let vm = MockVm::new(); - - let mut loader = MockLoader::empty(); - - let terminate_ch = loader.insert(|ctx| { + let terminate_ch = MockLoader::insert(|ctx| { ctx.ext.terminate(&ALICE).unwrap(); exec_success() }); @@ -1410,19 +1488,23 @@ mod tests { .existential_deposit(15) .build() .execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = MockExecutable::from_storage( + terminate_ch, &schedule, &mut gas_meter + ).unwrap(); set_balance(&ALICE, 1000); assert_eq!( ctx.instantiate( 100, - &mut GasMeter::::new(GAS_LIMIT), - &terminate_ch, + &mut gas_meter, + executable, vec![], &[], ), - Err(Error::::NewContractNotFunded.into()) + Err(Error::::NotCallable.into()) ); assert_eq!( @@ -1434,10 +1516,9 @@ mod tests { #[test] fn rent_allowance() { - let vm = MockVm::new(); - let mut loader = MockLoader::empty(); - let rent_allowance_ch = loader.insert(|ctx| { - let allowance = ConfigCache::::subsistence_threshold_uncached() * 3; + let rent_allowance_ch = MockLoader::insert(|ctx| { + let subsistence = Contracts::::subsistence_threshold(); + let allowance = subsistence * 3; assert_eq!(ctx.ext.rent_allowance(), >::max_value()); ctx.ext.set_rent_allowance(allowance); assert_eq!(ctx.ext.rent_allowance(), allowance); @@ -1445,14 +1526,19 @@ mod tests { }); ExtBuilder::default().build().execute_with(|| { - let cfg = ConfigCache::preload(); - let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader); - set_balance(&ALICE, cfg.subsistence_threshold() * 10); + let subsistence = Contracts::::subsistence_threshold(); + let schedule = Contracts::current_schedule(); + let mut ctx = MockContext::top_level(ALICE, &schedule); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = MockExecutable::from_storage( + rent_allowance_ch, &schedule, &mut gas_meter + ).unwrap(); + set_balance(&ALICE, subsistence * 10); let result = ctx.instantiate( - cfg.subsistence_threshold() * 5, - &mut GasMeter::::new(GAS_LIMIT), - &rent_allowance_ch, + subsistence * 5, + &mut gas_meter, + executable, vec![], &[], ); diff --git a/frame/contracts/src/gas.rs b/frame/contracts/src/gas.rs index 9bb6185e558ac..80e608b217bd3 100644 --- a/frame/contracts/src/gas.rs +++ b/frame/contracts/src/gas.rs @@ -15,41 +15,26 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::Config; +use crate::{Config, Error}; use sp_std::marker::PhantomData; use sp_runtime::traits::Zero; -use frame_support::dispatch::{ - DispatchResultWithPostInfo, PostDispatchInfo, DispatchErrorWithPostInfo, +use frame_support::{ + dispatch::{ + DispatchResultWithPostInfo, PostDispatchInfo, DispatchErrorWithPostInfo, DispatchError, + }, + weights::Weight, }; use pallet_contracts_primitives::ExecError; +use sp_core::crypto::UncheckedFrom; #[cfg(test)] use std::{any::Any, fmt::Debug}; -// Gas is essentially the same as weight. It is a 1 to 1 correspondence. -pub type Gas = frame_support::weights::Weight; - -#[must_use] #[derive(Debug, PartialEq, Eq)] -pub enum GasMeterResult { - Proceed(ChargedAmount), - OutOfGas, -} - -impl GasMeterResult { - pub fn is_out_of_gas(&self) -> bool { - match *self { - GasMeterResult::OutOfGas => true, - GasMeterResult::Proceed(_) => false, - } - } -} - -#[derive(Debug, PartialEq, Eq)] -pub struct ChargedAmount(Gas); +pub struct ChargedAmount(Weight); impl ChargedAmount { - pub fn amount(&self) -> Gas { + pub fn amount(&self) -> Weight { self.0 } } @@ -84,7 +69,7 @@ pub trait Token: Copy + Clone + TestAuxiliaries { /// That said, implementors of this function still can run into overflows /// while calculating the amount. In this case it is ok to use saturating operations /// since on overflow they will return `max_value` which should consume all gas. - fn calculate_amount(&self, metadata: &Self::Metadata) -> Gas; + fn calculate_amount(&self, metadata: &Self::Metadata) -> Weight; } /// A wrapper around a type-erased trait object of what used to be a `Token`. @@ -95,15 +80,19 @@ pub struct ErasedToken { } pub struct GasMeter { - gas_limit: Gas, + gas_limit: Weight, /// Amount of gas left from initial gas limit. Can reach zero. - gas_left: Gas, + gas_left: Weight, _phantom: PhantomData, #[cfg(test)] tokens: Vec, } -impl GasMeter { - pub fn new(gas_limit: Gas) -> Self { + +impl GasMeter +where + T::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]> +{ + pub fn new(gas_limit: Weight) -> Self { GasMeter { gas_limit, gas_left: gas_limit, @@ -127,7 +116,7 @@ impl GasMeter { &mut self, metadata: &Tok::Metadata, token: Tok, - ) -> GasMeterResult { + ) -> Result { #[cfg(test)] { // Unconditionally add the token to the storage. @@ -148,11 +137,25 @@ impl GasMeter { self.gas_left = new_value.unwrap_or_else(Zero::zero); match new_value { - Some(_) => GasMeterResult::Proceed(ChargedAmount(amount)), - None => GasMeterResult::OutOfGas, + Some(_) => Ok(ChargedAmount(amount)), + None => Err(Error::::OutOfGas.into()), } } + /// Adjust a previously charged amount down to its actual amount. + /// + /// This is when a maximum a priori amount was charged and then should be partially + /// refunded to match the actual amount. + pub fn adjust_gas>( + &mut self, + charged_amount: ChargedAmount, + metadata: &Tok::Metadata, + token: Tok, + ) { + let adjustment = charged_amount.0.saturating_sub(token.calculate_amount(metadata)); + self.gas_left = self.gas_left.saturating_add(adjustment).min(self.gas_limit); + } + /// Refund previously charged gas back to the gas meter. /// /// This can be used if a gas worst case estimation must be charged before @@ -171,7 +174,7 @@ impl GasMeter { /// All unused gas in the nested gas meter is returned to this gas meter. pub fn with_nested>) -> R>( &mut self, - amount: Gas, + amount: Weight, f: F, ) -> R { // NOTE that it is ok to allocate all available gas since it still ensured @@ -191,22 +194,25 @@ impl GasMeter { } /// Returns how much gas was used. - pub fn gas_spent(&self) -> Gas { + pub fn gas_spent(&self) -> Weight { self.gas_limit - self.gas_left } /// Returns how much gas left from the initial budget. - pub fn gas_left(&self) -> Gas { + pub fn gas_left(&self) -> Weight { self.gas_left } /// Turn this GasMeter into a DispatchResult that contains the actually used gas. - pub fn into_dispatch_result(self, result: Result) -> DispatchResultWithPostInfo + pub fn into_dispatch_result( + self, result: Result, + base_weight: Weight, + ) -> DispatchResultWithPostInfo where E: Into, { let post_info = PostDispatchInfo { - actual_weight: Some(self.gas_spent()), + actual_weight: Some(self.gas_spent().saturating_add(base_weight)), pays_fee: Default::default(), }; @@ -221,49 +227,48 @@ impl GasMeter { } } -/// A simple utility macro that helps to match against a -/// list of tokens. -#[macro_export] -macro_rules! match_tokens { - ($tokens_iter:ident,) => { - }; - ($tokens_iter:ident, $x:expr, $($rest:tt)*) => { - { - let next = ($tokens_iter).next().unwrap(); - let pattern = $x; - - // Note that we don't specify the type name directly in this macro, - // we only have some expression $x of some type. At the same time, we - // have an iterator of Box and to downcast we need to specify - // the type which we want downcast to. - // - // So what we do is we assign `_pattern_typed_next_ref` to a variable which has - // the required type. - // - // Then we make `_pattern_typed_next_ref = token.downcast_ref()`. This makes - // rustc infer the type `T` (in `downcast_ref`) to be the same as in $x. - - let mut _pattern_typed_next_ref = &pattern; - _pattern_typed_next_ref = match next.token.downcast_ref() { - Some(p) => { - assert_eq!(p, &pattern); - p - } - None => { - panic!("expected type {} got {}", stringify!($x), next.description); - } - }; - } - - match_tokens!($tokens_iter, $($rest)*); - }; -} - #[cfg(test)] mod tests { use super::{GasMeter, Token}; use crate::tests::Test; + /// A simple utility macro that helps to match against a + /// list of tokens. + macro_rules! match_tokens { + ($tokens_iter:ident,) => { + }; + ($tokens_iter:ident, $x:expr, $($rest:tt)*) => { + { + let next = ($tokens_iter).next().unwrap(); + let pattern = $x; + + // Note that we don't specify the type name directly in this macro, + // we only have some expression $x of some type. At the same time, we + // have an iterator of Box and to downcast we need to specify + // the type which we want downcast to. + // + // So what we do is we assign `_pattern_typed_next_ref` to a variable which has + // the required type. + // + // Then we make `_pattern_typed_next_ref = token.downcast_ref()`. This makes + // rustc infer the type `T` (in `downcast_ref`) to be the same as in $x. + + let mut _pattern_typed_next_ref = &pattern; + _pattern_typed_next_ref = match next.token.downcast_ref() { + Some(p) => { + assert_eq!(p, &pattern); + p + } + None => { + panic!("expected type {} got {}", stringify!($x), next.description); + } + }; + } + + match_tokens!($tokens_iter, $($rest)*); + }; + } + /// A trivial token that charges the specified number of gas units. #[derive(Copy, Clone, PartialEq, Eq, Debug)] struct SimpleToken(u64); @@ -300,7 +305,7 @@ mod tests { let result = gas_meter .charge(&MultiplierTokenMetadata { multiplier: 3 }, MultiplierToken(10)); - assert!(!result.is_out_of_gas()); + assert!(!result.is_err()); assert_eq!(gas_meter.gas_left(), 49_970); } @@ -308,10 +313,10 @@ mod tests { #[test] fn tracing() { let mut gas_meter = GasMeter::::new(50000); - assert!(!gas_meter.charge(&(), SimpleToken(1)).is_out_of_gas()); + assert!(!gas_meter.charge(&(), SimpleToken(1)).is_err()); assert!(!gas_meter .charge(&MultiplierTokenMetadata { multiplier: 3 }, MultiplierToken(10)) - .is_out_of_gas()); + .is_err()); let mut tokens = gas_meter.tokens()[0..2].iter(); match_tokens!(tokens, SimpleToken(1), MultiplierToken(10),); @@ -321,7 +326,7 @@ mod tests { #[test] fn refuse_to_execute_anything_if_zero() { let mut gas_meter = GasMeter::::new(0); - assert!(gas_meter.charge(&(), SimpleToken(1)).is_out_of_gas()); + assert!(gas_meter.charge(&(), SimpleToken(1)).is_err()); } // Make sure that if the gas meter is charged by exceeding amount then not only an error @@ -334,10 +339,10 @@ mod tests { let mut gas_meter = GasMeter::::new(200); // The first charge is should lead to OOG. - assert!(gas_meter.charge(&(), SimpleToken(300)).is_out_of_gas()); + assert!(gas_meter.charge(&(), SimpleToken(300)).is_err()); // The gas meter is emptied at this moment, so this should also fail. - assert!(gas_meter.charge(&(), SimpleToken(1)).is_out_of_gas()); + assert!(gas_meter.charge(&(), SimpleToken(1)).is_err()); } @@ -346,6 +351,6 @@ mod tests { #[test] fn charge_exact_amount() { let mut gas_meter = GasMeter::::new(25); - assert!(!gas_meter.charge(&(), SimpleToken(25)).is_out_of_gas()); + assert!(!gas_meter.charge(&(), SimpleToken(25)).is_err()); } } diff --git a/frame/contracts/src/lib.rs b/frame/contracts/src/lib.rs index fee56b15a6911..1f21a59e61584 100644 --- a/frame/contracts/src/lib.rs +++ b/frame/contracts/src/lib.rs @@ -59,10 +59,11 @@ //! //! ### Dispatchable functions //! -//! * `put_code` - Stores the given binary Wasm code into the chain's storage and returns its `code_hash`. -//! * `instantiate` - Deploys a new contract from the given `code_hash`, optionally transferring some balance. -//! This instantiates a new smart contract account and calls its contract deploy handler to -//! initialize the contract. +//! * `instantiate_with_code` - Deploys a new contract from the supplied wasm binary, optionally transferring +//! some balance. This instantiates a new smart contract account and calls its contract deploy +//! handler to initialize the contract. +//! * `instantiate` - The same as `instantiate_with_code` but instead of uploading new code an +//! existing `code_hash` is supplied. //! * `call` - Makes a call to an account, optionally transferring some balance. //! //! ## Usage @@ -79,7 +80,7 @@ //! * [Balances](../pallet_balances/index.html) #![cfg_attr(not(feature = "std"), no_std)] -#![cfg_attr(feature = "runtime-benchmarks", recursion_limit="256")] +#![cfg_attr(feature = "runtime-benchmarks", recursion_limit="512")] #[macro_use] mod gas; @@ -97,361 +98,80 @@ pub mod weights; mod tests; pub use crate::{ - gas::{Gas, GasMeter}, - wasm::ReturnCode as RuntimeReturnCode, - weights::WeightInfo, + wasm::PrefabWasmModule, schedule::{Schedule, HostFnWeights, InstructionWeights, Limits}, + pallet::*, }; use crate::{ - exec::ExecutionContext, - wasm::{WasmLoader, WasmVm}, + gas::GasMeter, + exec::{ExecutionContext, Executable}, rent::Rent, - storage::Storage, + storage::{Storage, DeletedContract}, + weights::WeightInfo, }; use sp_core::crypto::UncheckedFrom; use sp_std::{prelude::*, marker::PhantomData, fmt::Debug}; use codec::{Codec, Encode, Decode}; use sp_runtime::{ traits::{ - Hash, StaticLookup, Zero, MaybeSerializeDeserialize, Member, Convert, Saturating, + Hash, StaticLookup, MaybeSerializeDeserialize, Member, Convert, Saturating, Zero, }, RuntimeDebug, Perbill, }; use frame_support::{ - decl_module, decl_event, decl_storage, decl_error, ensure, storage::child::ChildInfo, - dispatch::{DispatchResult, DispatchResultWithPostInfo}, traits::{OnUnbalanced, Currency, Get, Time, Randomness}, - weights::Pays, + weights::{Weight, PostDispatchInfo, WithPostDispatchInfo}, }; -use frame_system::{ensure_signed, ensure_root, Module as System}; +use frame_system::Module as System; use pallet_contracts_primitives::{ - RentProjectionResult, GetStorageResult, ContractAccessError, ContractExecResult, ExecResult, + RentProjectionResult, GetStorageResult, ContractAccessError, ContractExecResult, }; -use frame_support::weights::Weight; pub type CodeHash = ::Hash; pub type TrieId = Vec; - -/// Information for managing an account and its sub trie abstraction. -/// This is the required info to cache for an account -#[derive(Encode, Decode, RuntimeDebug)] -pub enum ContractInfo { - Alive(AliveContractInfo), - Tombstone(TombstoneContractInfo), -} - -impl ContractInfo { - /// If contract is alive then return some alive info - pub fn get_alive(self) -> Option> { - if let ContractInfo::Alive(alive) = self { - Some(alive) - } else { - None - } - } - /// If contract is alive then return some reference to alive info - pub fn as_alive(&self) -> Option<&AliveContractInfo> { - if let ContractInfo::Alive(ref alive) = self { - Some(alive) - } else { - None - } - } - /// If contract is alive then return some mutable reference to alive info - pub fn as_alive_mut(&mut self) -> Option<&mut AliveContractInfo> { - if let ContractInfo::Alive(ref mut alive) = self { - Some(alive) - } else { - None - } - } - - /// If contract is tombstone then return some tombstone info - pub fn get_tombstone(self) -> Option> { - if let ContractInfo::Tombstone(tombstone) = self { - Some(tombstone) - } else { - None - } - } - /// If contract is tombstone then return some reference to tombstone info - pub fn as_tombstone(&self) -> Option<&TombstoneContractInfo> { - if let ContractInfo::Tombstone(ref tombstone) = self { - Some(tombstone) - } else { - None - } - } - /// If contract is tombstone then return some mutable reference to tombstone info - pub fn as_tombstone_mut(&mut self) -> Option<&mut TombstoneContractInfo> { - if let ContractInfo::Tombstone(ref mut tombstone) = self { - Some(tombstone) - } else { - None - } - } -} - +pub type BalanceOf = + <::Currency as Currency<::AccountId>>::Balance; +pub type NegativeImbalanceOf = + <::Currency as Currency<::AccountId>>::NegativeImbalance; pub type AliveContractInfo = RawAliveContractInfo, BalanceOf, ::BlockNumber>; - -/// Information for managing an account and its sub trie abstraction. -/// This is the required info to cache for an account. -#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)] -pub struct RawAliveContractInfo { - /// Unique ID for the subtree encoded as a bytes vector. - pub trie_id: TrieId, - /// The total number of bytes used by this contract. - /// - /// It is a sum of each key-value pair stored by this contract. - pub storage_size: u32, - /// The total number of key-value pairs in storage of this contract. - pub pair_count: u32, - /// The code associated with a given account. - pub code_hash: CodeHash, - /// Pay rent at most up to this value. - pub rent_allowance: Balance, - /// The amount of rent that was payed by the contract over its whole lifetime. - /// - /// A restored contract starts with a value of zero just like a new contract. - pub rent_payed: Balance, - /// Last block rent has been payed. - pub deduct_block: BlockNumber, - /// Last block child storage has been written. - pub last_write: Option, -} - -impl RawAliveContractInfo { - /// Associated child trie unique id is built from the hash part of the trie id. - pub fn child_trie_info(&self) -> ChildInfo { - child_trie_info(&self.trie_id[..]) - } -} - -/// Associated child trie unique id is built from the hash part of the trie id. -pub(crate) fn child_trie_info(trie_id: &[u8]) -> ChildInfo { - ChildInfo::new_default(trie_id) -} - pub type TombstoneContractInfo = RawTombstoneContractInfo<::Hash, ::Hashing>; -#[derive(Encode, Decode, PartialEq, Eq, RuntimeDebug)] -pub struct RawTombstoneContractInfo(H, PhantomData); - -impl RawTombstoneContractInfo -where - H: Member + MaybeSerializeDeserialize+ Debug - + AsRef<[u8]> + AsMut<[u8]> + Copy + Default - + sp_std::hash::Hash + Codec, - Hasher: Hash, -{ - fn new(storage_root: &[u8], code_hash: H) -> Self { - let mut buf = Vec::new(); - storage_root.using_encoded(|encoded| buf.extend_from_slice(encoded)); - buf.extend_from_slice(code_hash.as_ref()); - RawTombstoneContractInfo(::hash(&buf[..]), PhantomData) - } -} - -impl From> for ContractInfo { - fn from(alive_info: AliveContractInfo) -> Self { - Self::Alive(alive_info) - } -} - -pub type BalanceOf = - <::Currency as Currency<::AccountId>>::Balance; -pub type NegativeImbalanceOf = - <::Currency as Currency<::AccountId>>::NegativeImbalance; +#[frame_support::pallet] +pub mod pallet { + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use super::*; + #[pallet::config] pub trait Config: frame_system::Config { - type Time: Time; - type Randomness: Randomness; - - /// The currency in which fees are paid and contract balances are held. - type Currency: Currency; - - /// The overarching event type. - type Event: From> + Into<::Event>; - - /// Handler for rent payments. - type RentPayment: OnUnbalanced>; - - /// Number of block delay an extrinsic claim surcharge has. - /// - /// When claim surcharge is called by an extrinsic the rent is checked - /// for current_block - delay - type SignedClaimHandicap: Get; - - /// The minimum amount required to generate a tombstone. - type TombstoneDeposit: Get>; - - /// The balance every contract needs to deposit to stay alive indefinitely. - /// - /// This is different from the [`Self::TombstoneDeposit`] because this only needs to be - /// deposited while the contract is alive. Costs for additional storage are added to - /// this base cost. - /// - /// This is a simple way to ensure that contracts with empty storage eventually get deleted by - /// making them pay rent. This creates an incentive to remove them early in order to save rent. - type DepositPerContract: Get>; - - /// The balance a contract needs to deposit per storage byte to stay alive indefinitely. - /// - /// Let's suppose the deposit is 1,000 BU (balance units)/byte and the rent is 1 BU/byte/day, - /// then a contract with 1,000,000 BU that uses 1,000 bytes of storage would pay no rent. - /// But if the balance reduced to 500,000 BU and the storage stayed the same at 1,000, - /// then it would pay 500 BU/day. - type DepositPerStorageByte: Get>; - - /// The balance a contract needs to deposit per storage item to stay alive indefinitely. - /// - /// It works the same as [`Self::DepositPerStorageByte`] but for storage items. - type DepositPerStorageItem: Get>; - - /// The fraction of the deposit that should be used as rent per block. - /// - /// When a contract hasn't enough balance deposited to stay alive indefinitely it needs - /// to pay per block for the storage it consumes that is not covered by the deposit. - /// This determines how high this rent payment is per block as a fraction of the deposit. - type RentFraction: Get; - - /// Reward that is received by the party whose touch has led - /// to removal of a contract. - type SurchargeReward: Get>; - - /// The maximum nesting level of a call/instantiate stack. - type MaxDepth: Get; - - /// The maximum size of a storage value and event payload in bytes. - type MaxValueSize: Get; + /// The time implementation used to supply timestamps to conntracts through `seal_now`. + type Time: Time; - /// Used to answer contracts's queries regarding the current weight price. This is **not** - /// used to calculate the actual fee and is only for informational purposes. - type WeightPrice: Convert>; + /// The generator used to supply randomness to contracts through `seal_random`. + type Randomness: Randomness; - /// Describes the weights of the dispatchables of this module and is also used to - /// construct a default cost schedule. - type WeightInfo: WeightInfo; + /// The currency in which fees are paid and contract balances are held. + type Currency: Currency; - /// Type that allows the runtime authors to add new host functions for a contract to call. - type ChainExtension: chain_extension::ChainExtension; - - /// The maximum number of tries that can be queued for deletion. - type DeletionQueueDepth: Get; - - /// The maximum amount of weight that can be consumed per block for lazy trie removal. - type DeletionWeightLimit: Get; -} - -decl_error! { - /// Error for the contracts module. - pub enum Error for Module - where - T::AccountId: UncheckedFrom, - T::AccountId: AsRef<[u8]>, - { - /// A new schedule must have a greater version than the current one. - InvalidScheduleVersion, - /// An origin must be signed or inherent and auxiliary sender only provided on inherent. - InvalidSurchargeClaim, - /// Cannot restore from nonexisting or tombstone contract. - InvalidSourceContract, - /// Cannot restore to nonexisting or alive contract. - InvalidDestinationContract, - /// Tombstones don't match. - InvalidTombstone, - /// An origin TrieId written in the current block. - InvalidContractOrigin, - /// The executed contract exhausted its gas limit. - OutOfGas, - /// The output buffer supplied to a contract API call was too small. - OutputBufferTooSmall, - /// Performing the requested transfer would have brought the contract below - /// the subsistence threshold. No transfer is allowed to do this in order to allow - /// for a tombstone to be created. Use `seal_terminate` to remove a contract without - /// leaving a tombstone behind. - BelowSubsistenceThreshold, - /// The newly created contract is below the subsistence threshold after executing - /// its contructor. No contracts are allowed to exist below that threshold. - NewContractNotFunded, - /// Performing the requested transfer failed for a reason originating in the - /// chosen currency implementation of the runtime. Most probably the balance is - /// too low or locks are placed on it. - TransferFailed, - /// Performing a call was denied because the calling depth reached the limit - /// of what is specified in the schedule. - MaxCallDepthReached, - /// The contract that was called is either no contract at all (a plain account) - /// or is a tombstone. - NotCallable, - /// The code supplied to `put_code` exceeds the limit specified in the current schedule. - CodeTooLarge, - /// No code could be found at the supplied code hash. - CodeNotFound, - /// A buffer outside of sandbox memory was passed to a contract API function. - OutOfBounds, - /// Input passed to a contract API function failed to decode as expected type. - DecodingFailed, - /// Contract trapped during execution. - ContractTrapped, - /// The size defined in `T::MaxValueSize` was exceeded. - ValueTooLarge, - /// The action performed is not allowed while the contract performing it is already - /// on the call stack. Those actions are contract self destruction and restoration - /// of a tombstone. - ReentranceDenied, - /// `seal_input` was called twice from the same contract execution context. - InputAlreadyRead, - /// The subject passed to `seal_random` exceeds the limit. - RandomSubjectTooLong, - /// The amount of topics passed to `seal_deposit_events` exceeds the limit. - TooManyTopics, - /// The topics passed to `seal_deposit_events` contains at least one duplicate. - DuplicateTopics, - /// The chain does not provide a chain extension. Calling the chain extension results - /// in this error. Note that this usually shouldn't happen as deploying such contracts - /// is rejected. - NoChainExtension, - /// Removal of a contract failed because the deletion queue is full. - /// - /// This can happen when either calling [`Module::claim_surcharge`] or `seal_terminate`. - /// The queue is filled by deleting contracts and emptied by a fixed amount each block. - /// Trying again during another block is the only way to resolve this issue. - DeletionQueueFull, - /// A contract could not be evicted because it has enough balance to pay rent. - /// - /// This can be returned from [`Module::claim_surcharge`] because the target - /// contract has enough balance to pay for its rent. - ContractNotEvictable, - /// A storage modification exhausted the 32bit type that holds the storage size. - /// - /// This can either happen when the accumulated storage in bytes is too large or - /// when number of storage items is too large. - StorageExhausted, - } -} + /// The overarching event type. + type Event: From> + IsType<::Event>; -decl_module! { - /// Contracts module. - pub struct Module for enum Call - where - origin: T::Origin, - T::AccountId: UncheckedFrom, - T::AccountId: AsRef<[u8]>, - { - type Error = Error; + /// Handler for rent payments. + type RentPayment: OnUnbalanced>; /// Number of block delay an extrinsic claim surcharge has. /// /// When claim surcharge is called by an extrinsic the rent is checked /// for current_block - delay - const SignedClaimHandicap: T::BlockNumber = T::SignedClaimHandicap::get(); + #[pallet::constant] + type SignedClaimHandicap: Get; /// The minimum amount required to generate a tombstone. - const TombstoneDeposit: BalanceOf = T::TombstoneDeposit::get(); + #[pallet::constant] + type TombstoneDeposit: Get>; /// The balance every contract needs to deposit to stay alive indefinitely. /// @@ -461,7 +181,8 @@ decl_module! { /// /// This is a simple way to ensure that contracts with empty storage eventually get deleted by /// making them pay rent. This creates an incentive to remove them early in order to save rent. - const DepositPerContract: BalanceOf = T::DepositPerContract::get(); + #[pallet::constant] + type DepositPerContract: Get>; /// The balance a contract needs to deposit per storage byte to stay alive indefinitely. /// @@ -469,40 +190,73 @@ decl_module! { /// then a contract with 1,000,000 BU that uses 1,000 bytes of storage would pay no rent. /// But if the balance reduced to 500,000 BU and the storage stayed the same at 1,000, /// then it would pay 500 BU/day. - const DepositPerStorageByte: BalanceOf = T::DepositPerStorageByte::get(); + #[pallet::constant] + type DepositPerStorageByte: Get>; /// The balance a contract needs to deposit per storage item to stay alive indefinitely. /// /// It works the same as [`Self::DepositPerStorageByte`] but for storage items. - const DepositPerStorageItem: BalanceOf = T::DepositPerStorageItem::get(); + #[pallet::constant] + type DepositPerStorageItem: Get>; /// The fraction of the deposit that should be used as rent per block. /// /// When a contract hasn't enough balance deposited to stay alive indefinitely it needs /// to pay per block for the storage it consumes that is not covered by the deposit. /// This determines how high this rent payment is per block as a fraction of the deposit. - const RentFraction: Perbill = T::RentFraction::get(); + #[pallet::constant] + type RentFraction: Get; /// Reward that is received by the party whose touch has led /// to removal of a contract. - const SurchargeReward: BalanceOf = T::SurchargeReward::get(); + #[pallet::constant] + type SurchargeReward: Get>; + + /// The maximum nesting level of a call/instantiate stack. + #[pallet::constant] + type MaxDepth: Get; + + /// The maximum size of a storage value and event payload in bytes. + #[pallet::constant] + type MaxValueSize: Get; + + /// Used to answer contracts's queries regarding the current weight price. This is **not** + /// used to calculate the actual fee and is only for informational purposes. + type WeightPrice: Convert>; - /// The maximum nesting level of a call/instantiate stack. A reasonable default - /// value is 100. - const MaxDepth: u32 = T::MaxDepth::get(); + /// Describes the weights of the dispatchables of this module and is also used to + /// construct a default cost schedule. + type WeightInfo: WeightInfo; - /// The maximum size of a storage value in bytes. A reasonable default is 16 KiB. - const MaxValueSize: u32 = T::MaxValueSize::get(); + /// Type that allows the runtime authors to add new host functions for a contract to call. + type ChainExtension: chain_extension::ChainExtension; /// The maximum number of tries that can be queued for deletion. - const DeletionQueueDepth: u32 = T::DeletionQueueDepth::get(); + #[pallet::constant] + type DeletionQueueDepth: Get; /// The maximum amount of weight that can be consumed per block for lazy trie removal. - const DeletionWeightLimit: Weight = T::DeletionWeightLimit::get(); + #[pallet::constant] + type DeletionWeightLimit: Get; + + /// The maximum length of a contract code in bytes. This limit applies to the instrumented + /// version of the code. Therefore `instantiate_with_code` can fail even when supplying + /// a wasm binary below this maximum size. + #[pallet::constant] + type MaxCodeSize: Get; + } - fn deposit_event() = default; + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(PhantomData); - fn on_initialize() -> Weight { + #[pallet::hooks] + impl Hooks> for Pallet + where + T::AccountId: UncheckedFrom, + T::AccountId: AsRef<[u8]>, + { + fn on_initialize(_block: T::BlockNumber) -> Weight { // We do not want to go above the block limit and rather avoid lazy deletion // in that case. This should only happen on runtime upgrades. let weight_limit = T::BlockWeights::get().max_block @@ -511,38 +265,29 @@ decl_module! { Storage::::process_deletion_queue_batch(weight_limit) .saturating_add(T::WeightInfo::on_initialize()) } + } + #[pallet::call] + impl Pallet + where + T::AccountId: UncheckedFrom, + T::AccountId: AsRef<[u8]>, + { /// Updates the schedule for metering contracts. /// /// The schedule must have a greater version than the stored schedule. - #[weight = T::WeightInfo::update_schedule()] - pub fn update_schedule(origin, schedule: Schedule) -> DispatchResult { + #[pallet::weight(T::WeightInfo::update_schedule())] + pub fn update_schedule( + origin: OriginFor, + schedule: Schedule + ) -> DispatchResultWithPostInfo { ensure_root(origin)?; if >::current_schedule().version >= schedule.version { Err(Error::::InvalidScheduleVersion)? } - - Self::deposit_event(RawEvent::ScheduleUpdated(schedule.version)); + Self::deposit_event(Event::ScheduleUpdated(schedule.version)); CurrentSchedule::put(schedule); - - Ok(()) - } - - /// Stores the given binary Wasm code into the chain's storage and returns its `codehash`. - /// You can instantiate contracts only with stored code. - #[weight = T::WeightInfo::put_code(code.len() as u32 / 1024)] - pub fn put_code( - origin, - code: Vec - ) -> DispatchResult { - ensure_signed(origin)?; - let schedule = >::current_schedule(); - ensure!(code.len() as u32 <= schedule.limits.code_size, Error::::CodeTooLarge); - let result = wasm::save_code::(code, &schedule); - if let Ok(code_hash) = result { - Self::deposit_event(RawEvent::CodeStored(code_hash)); - } - result.map(|_| ()).map_err(Into::into) + Ok(().into()) } /// Makes a call to an account, optionally transferring some balance. @@ -552,59 +297,108 @@ decl_module! { /// * If the account is a regular account, any value will be transferred. /// * If no account exists and the call value is not less than `existential_deposit`, /// a regular account will be created and any value will be transferred. - #[weight = T::WeightInfo::call().saturating_add(*gas_limit)] + #[pallet::weight(T::WeightInfo::call(T::MaxCodeSize::get() / 1024).saturating_add(*gas_limit))] pub fn call( - origin, + origin: OriginFor, dest: ::Source, - #[compact] value: BalanceOf, - #[compact] gas_limit: Gas, + #[pallet::compact] value: BalanceOf, + #[pallet::compact] gas_limit: Weight, data: Vec ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; let dest = T::Lookup::lookup(dest)?; let mut gas_meter = GasMeter::new(gas_limit); - - let result = Self::execute_wasm(origin, &mut gas_meter, |ctx, gas_meter| { - ctx.call(dest, value, gas_meter, data) - }); - gas_meter.into_dispatch_result(result) + let schedule = >::current_schedule(); + let mut ctx = ExecutionContext::>::top_level(origin, &schedule); + let (result, code_len) = match ctx.call(dest, value, &mut gas_meter, data) { + Ok((output, len)) => (Ok(output), len), + Err((err, len)) => (Err(err), len), + }; + gas_meter.into_dispatch_result(result, T::WeightInfo::call(code_len / 1024)) } - /// Instantiates a new contract from the `code_hash` generated by `put_code`, - /// optionally transferring some balance. + /// Instantiates a new contract from the supplied `code` optionally transferring + /// some balance. + /// + /// This is the only function that can deploy new code to the chain. + /// + /// # Parameters /// - /// The supplied `salt` is used for contract address deriviation. See `fn contract_address`. + /// * `endowment`: The balance to transfer from the `origin` to the newly created contract. + /// * `gas_limit`: The gas limit enforced when executing the constructor. + /// * `code`: The contract code to deploy in raw bytes. + /// * `data`: The input data to pass to the contract constructor. + /// * `salt`: Used for the address derivation. See [`Self::contract_address`]. /// /// Instantiation is executed as follows: /// + /// - The supplied `code` is instrumented, deployed, and a `code_hash` is created for that code. + /// - If the `code_hash` already exists on the chain the underlying `code` will be shared. /// - The destination address is computed based on the sender, code_hash and the salt. /// - The smart-contract account is created at the computed address. - /// - The `ctor_code` is executed in the context of the newly-created account. Buffer returned - /// after the execution is saved as the `code` of the account. That code will be invoked - /// upon any call received by this account. - /// - The contract is initialized. - #[weight = - T::WeightInfo::instantiate( - data.len() as u32 / 1024, + /// - The `endowment` is transferred to the new account. + /// - The `deploy` function is executed in the context of the newly-created account. + #[pallet::weight( + T::WeightInfo::instantiate_with_code( + code.len() as u32 / 1024, salt.len() as u32 / 1024, - ).saturating_add(*gas_limit) - ] + ) + .saturating_add(*gas_limit) + )] + pub fn instantiate_with_code( + origin: OriginFor, + #[pallet::compact] endowment: BalanceOf, + #[pallet::compact] gas_limit: Weight, + code: Vec, + data: Vec, + salt: Vec, + ) -> DispatchResultWithPostInfo { + let origin = ensure_signed(origin)?; + let code_len = code.len() as u32; + ensure!(code_len <= T::MaxCodeSize::get(), Error::::CodeTooLarge); + let mut gas_meter = GasMeter::new(gas_limit); + let schedule = >::current_schedule(); + let executable = PrefabWasmModule::from_code(code, &schedule)?; + let code_len = executable.code_len(); + ensure!(code_len <= T::MaxCodeSize::get(), Error::::CodeTooLarge); + let mut ctx = ExecutionContext::>::top_level(origin, &schedule); + let result = ctx.instantiate(endowment, &mut gas_meter, executable, data, &salt) + .map(|(_address, output)| output); + gas_meter.into_dispatch_result( + result, + T::WeightInfo::instantiate_with_code(code_len / 1024, salt.len() as u32 / 1024) + ) + } + + /// Instantiates a contract from a previously deployed wasm binary. + /// + /// This function is identical to [`Self::instantiate_with_code`] but without the + /// code deployment step. Instead, the `code_hash` of an on-chain deployed wasm binary + /// must be supplied. + #[pallet::weight( + T::WeightInfo::instantiate(T::MaxCodeSize::get() / 1024, salt.len() as u32 / 1024) + .saturating_add(*gas_limit) + )] pub fn instantiate( - origin, - #[compact] endowment: BalanceOf, - #[compact] gas_limit: Gas, + origin: OriginFor, + #[pallet::compact] endowment: BalanceOf, + #[pallet::compact] gas_limit: Weight, code_hash: CodeHash, data: Vec, salt: Vec, ) -> DispatchResultWithPostInfo { let origin = ensure_signed(origin)?; let mut gas_meter = GasMeter::new(gas_limit); - - let result = Self::execute_wasm(origin, &mut gas_meter, |ctx, gas_meter| { - ctx.instantiate(endowment, gas_meter, &code_hash, data, &salt) - .map(|(_address, output)| output) - }); - gas_meter.into_dispatch_result(result) + let schedule = >::current_schedule(); + let executable = PrefabWasmModule::from_storage(code_hash, &schedule, &mut gas_meter)?; + let mut ctx = ExecutionContext::>::top_level(origin, &schedule); + let code_len = executable.code_len(); + let result = ctx.instantiate(endowment, &mut gas_meter, executable, data, &salt) + .map(|(_address, output)| output); + gas_meter.into_dispatch_result( + result, + T::WeightInfo::instantiate(code_len / 1024, salt.len() as u32 / 1024), + ) } /// Allows block producers to claim a small reward for evicting a contract. If a block @@ -616,9 +410,9 @@ decl_module! { /// /// If contract is not evicted as a result of this call, [`Error::ContractNotEvictable`] /// is returned and the sender is not eligible for the reward. - #[weight = T::WeightInfo::claim_surcharge()] + #[pallet::weight(T::WeightInfo::claim_surcharge(T::MaxCodeSize::get() / 1024))] pub fn claim_surcharge( - origin, + origin: OriginFor, dest: T::AccountId, aux_sender: Option ) -> DispatchResultWithPostInfo { @@ -643,21 +437,226 @@ decl_module! { }; // If poking the contract has lead to eviction of the contract, give out the rewards. - if let Some(rent_payed) = Rent::::try_eviction(&dest, handicap)? { - T::Currency::deposit_into_existing( - &rewarded, - T::SurchargeReward::get().min(rent_payed), - ) - .map(|_| Pays::No.into()) - .map_err(Into::into) - } else { - Err(Error::::ContractNotEvictable.into()) + match Rent::>::try_eviction(&dest, handicap)? { + (Some(rent_payed), code_len) => { + T::Currency::deposit_into_existing( + &rewarded, + T::SurchargeReward::get().min(rent_payed), + ) + .map(|_| PostDispatchInfo { + actual_weight: Some(T::WeightInfo::claim_surcharge(code_len / 1024)), + pays_fee: Pays::No, + }) + .map_err(Into::into) + } + (None, code_len) => Err(Error::::ContractNotEvictable.with_weight( + T::WeightInfo::claim_surcharge(code_len / 1024) + )), + } + } + } + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + #[pallet::metadata(T::AccountId = "AccountId", T::Hash = "Hash", BalanceOf = "Balance")] + pub enum Event { + /// Contract deployed by address at the specified address. \[deployer, contract\] + Instantiated(T::AccountId, T::AccountId), + + /// Contract has been evicted and is now in tombstone state. \[contract\] + Evicted(T::AccountId), + + /// Contract has been terminated without leaving a tombstone. + /// \[contract, beneficiary\] + /// + /// # Params + /// + /// - `contract`: The contract that was terminated. + /// - `beneficiary`: The account that received the contracts remaining balance. + /// + /// # Note + /// + /// The only way for a contract to be removed without a tombstone and emitting + /// this event is by calling `seal_terminate`. + Terminated(T::AccountId, T::AccountId), + + /// Restoration of a contract has been successful. + /// \[restorer, dest, code_hash, rent_allowance\] + /// + /// # Params + /// + /// - `restorer`: Account ID of the restoring contract. + /// - `dest`: Account ID of the restored contract. + /// - `code_hash`: Code hash of the restored contract. + /// - `rent_allowance`: Rent allowance of the restored contract. + Restored(T::AccountId, T::AccountId, T::Hash, BalanceOf), + + /// Code with the specified hash has been stored. \[code_hash\] + CodeStored(T::Hash), + + /// Triggered when the current schedule is updated. + /// \[version\] + /// + /// # Params + /// + /// - `version`: The version of the newly set schedule. + ScheduleUpdated(u32), + + /// A custom event emitted by the contract. + /// \[contract, data\] + /// + /// # Params + /// + /// - `contract`: The contract that emitted the event. + /// - `data`: Data supplied by the contract. Metadata generated during contract + /// compilation is needed to decode it. + ContractEmitted(T::AccountId, Vec), + + /// A code with the specified hash was removed. + /// \[code_hash\] + /// + /// This happens when the last contract that uses this code hash was removed or evicted. + CodeRemoved(T::Hash), + } + + #[pallet::error] + pub enum Error { + /// A new schedule must have a greater version than the current one. + InvalidScheduleVersion, + /// An origin must be signed or inherent and auxiliary sender only provided on inherent. + InvalidSurchargeClaim, + /// Cannot restore from nonexisting or tombstone contract. + InvalidSourceContract, + /// Cannot restore to nonexisting or alive contract. + InvalidDestinationContract, + /// Tombstones don't match. + InvalidTombstone, + /// An origin TrieId written in the current block. + InvalidContractOrigin, + /// The executed contract exhausted its gas limit. + OutOfGas, + /// The output buffer supplied to a contract API call was too small. + OutputBufferTooSmall, + /// Performing the requested transfer would have brought the contract below + /// the subsistence threshold. No transfer is allowed to do this in order to allow + /// for a tombstone to be created. Use `seal_terminate` to remove a contract without + /// leaving a tombstone behind. + BelowSubsistenceThreshold, + /// The newly created contract is below the subsistence threshold after executing + /// its contructor. No contracts are allowed to exist below that threshold. + NewContractNotFunded, + /// Performing the requested transfer failed for a reason originating in the + /// chosen currency implementation of the runtime. Most probably the balance is + /// too low or locks are placed on it. + TransferFailed, + /// Performing a call was denied because the calling depth reached the limit + /// of what is specified in the schedule. + MaxCallDepthReached, + /// The contract that was called is either no contract at all (a plain account) + /// or is a tombstone. + NotCallable, + /// The code supplied to `instantiate_with_code` exceeds the limit specified in the + /// current schedule. + CodeTooLarge, + /// No code could be found at the supplied code hash. + CodeNotFound, + /// A buffer outside of sandbox memory was passed to a contract API function. + OutOfBounds, + /// Input passed to a contract API function failed to decode as expected type. + DecodingFailed, + /// Contract trapped during execution. + ContractTrapped, + /// The size defined in `T::MaxValueSize` was exceeded. + ValueTooLarge, + /// The action performed is not allowed while the contract performing it is already + /// on the call stack. Those actions are contract self destruction and restoration + /// of a tombstone. + ReentranceDenied, + /// `seal_input` was called twice from the same contract execution context. + InputAlreadyRead, + /// The subject passed to `seal_random` exceeds the limit. + RandomSubjectTooLong, + /// The amount of topics passed to `seal_deposit_events` exceeds the limit. + TooManyTopics, + /// The topics passed to `seal_deposit_events` contains at least one duplicate. + DuplicateTopics, + /// The chain does not provide a chain extension. Calling the chain extension results + /// in this error. Note that this usually shouldn't happen as deploying such contracts + /// is rejected. + NoChainExtension, + /// Removal of a contract failed because the deletion queue is full. + /// + /// This can happen when either calling [`Pallet::claim_surcharge`] or `seal_terminate`. + /// The queue is filled by deleting contracts and emptied by a fixed amount each block. + /// Trying again during another block is the only way to resolve this issue. + DeletionQueueFull, + /// A contract could not be evicted because it has enough balance to pay rent. + /// + /// This can be returned from [`Pallet::claim_surcharge`] because the target + /// contract has enough balance to pay for its rent. + ContractNotEvictable, + /// A storage modification exhausted the 32bit type that holds the storage size. + /// + /// This can either happen when the accumulated storage in bytes is too large or + /// when number of storage items is too large. + StorageExhausted, + /// A contract with the same AccountId already exists. + DuplicateContract, + } + + /// Current cost schedule for contracts. + #[pallet::storage] + #[pallet::getter(fn current_schedule)] + pub(super) type CurrentSchedule = StorageValue<_, Schedule, ValueQuery>; + + /// A mapping from an original code hash to the original code, untouched by instrumentation. + #[pallet::storage] + pub type PristineCode = StorageMap<_, Identity, CodeHash, Vec>; + + /// A mapping between an original code hash and instrumented wasm code, ready for execution. + #[pallet::storage] + pub type CodeStorage = StorageMap<_, Identity, CodeHash, PrefabWasmModule>; + + /// The subtrie counter. + #[pallet::storage] + pub type AccountCounter = StorageValue<_, u64, ValueQuery>; + + /// The code associated with a given account. + /// + /// TWOX-NOTE: SAFE since `AccountId` is a secure hash. + #[pallet::storage] + pub type ContractInfoOf = StorageMap<_, Twox64Concat, T::AccountId, ContractInfo>; + + /// Evicted contracts that await child trie deletion. + /// + /// Child trie deletion is a heavy operation depending on the amount of storage items + /// stored in said trie. Therefore this operation is performed lazily in `on_initialize`. + #[pallet::storage] + pub type DeletionQueue = StorageValue<_, Vec, ValueQuery>; + + #[pallet::genesis_config] + pub struct GenesisConfig { + #[doc = "Current cost schedule for contracts."] + pub current_schedule: Schedule, + } + + #[cfg(feature = "std")] + impl Default for GenesisConfig { + fn default() -> Self { + Self { + current_schedule: Default::default(), } } } + + #[pallet::genesis_build] + impl GenesisBuild for GenesisConfig { + fn build(&self) { + >::put(&self.current_schedule); + } + } } -/// Public APIs provided by the contracts module. impl Module where T::AccountId: UncheckedFrom + AsRef<[u8]>, @@ -667,21 +666,21 @@ where /// This function is similar to `Self::call`, but doesn't perform any address lookups and better /// suitable for calling directly from Rust. /// - /// It returns the exection result and the amount of used weight. + /// It returns the execution result and the amount of used weight. pub fn bare_call( origin: T::AccountId, dest: T::AccountId, value: BalanceOf, - gas_limit: Gas, + gas_limit: Weight, input_data: Vec, ) -> ContractExecResult { let mut gas_meter = GasMeter::new(gas_limit); - let exec_result = Self::execute_wasm(origin, &mut gas_meter, |ctx, gas_meter| { - ctx.call(dest, value, gas_meter, input_data) - }); + let schedule = >::current_schedule(); + let mut ctx = ExecutionContext::>::top_level(origin, &schedule); + let result = ctx.call(dest, value, &mut gas_meter, input_data); let gas_consumed = gas_meter.gas_spent(); ContractExecResult { - exec_result, + exec_result: result.map(|r| r.0).map_err(|r| r.0), gas_consumed, } } @@ -697,23 +696,17 @@ where Ok(maybe_value) } + /// Query how many blocks the contract stays alive given that the amount endowment + /// and consumed storage does not change. pub fn rent_projection(address: T::AccountId) -> RentProjectionResult { - Rent::::compute_projection(&address) - } - - /// Put code for benchmarks which does not check or instrument the code. - #[cfg(feature = "runtime-benchmarks")] - pub fn put_code_raw(code: Vec) -> DispatchResult { - let schedule = >::current_schedule(); - let result = wasm::save_code_raw::(code, &schedule); - result.map(|_| ()).map_err(Into::into) + Rent::>::compute_projection(&address) } /// Determine the address of a contract, /// - /// This is the address generation function used by contract instantation. Its result + /// This is the address generation function used by contract instantiation. Its result /// is only dependend on its inputs. It can therefore be used to reliably predict the - /// address of a contract. This is akin to the formular of eth's CRATE2 opcode. There + /// address of a contract. This is akin to the formular of eth's CREATE2 opcode. There /// is no CREATE equivalent because CREATE2 is strictly more powerful. /// /// Formula: `hash(deploying_address ++ code_hash ++ salt)` @@ -730,135 +723,154 @@ where .collect(); UncheckedFrom::unchecked_from(T::Hashing::hash(&buf)) } -} -impl Module -where - T::AccountId: UncheckedFrom + AsRef<[u8]>, -{ - fn execute_wasm( - origin: T::AccountId, - gas_meter: &mut GasMeter, - func: impl FnOnce(&mut ExecutionContext, WasmLoader>, &mut GasMeter) -> ExecResult, - ) -> ExecResult { - let cfg = ConfigCache::preload(); - let vm = WasmVm::new(&cfg.schedule); - let loader = WasmLoader::new(&cfg.schedule); - let mut ctx = ExecutionContext::top_level(origin, &cfg, &vm, &loader); - func(&mut ctx, gas_meter) + /// Subsistence threshold is the extension of the minimum balance (aka existential deposit) + /// by the tombstone deposit, required for leaving a tombstone. + /// + /// Rent or any contract initiated balance transfer mechanism cannot make the balance lower + /// than the subsistence threshold in order to guarantee that a tombstone is created. + /// + /// The only way to completely kill a contract without a tombstone is calling `seal_terminate`. + pub fn subsistence_threshold() -> BalanceOf { + T::Currency::minimum_balance().saturating_add(T::TombstoneDeposit::get()) } -} - -decl_event! { - pub enum Event - where - Balance = BalanceOf, - ::AccountId, - ::Hash - { - /// Contract deployed by address at the specified address. \[owner, contract\] - Instantiated(AccountId, AccountId), - /// Contract has been evicted and is now in tombstone state. - /// \[contract, tombstone\] - /// - /// # Params - /// - /// - `contract`: `AccountId`: The account ID of the evicted contract. - /// - `tombstone`: `bool`: True if the evicted contract left behind a tombstone. - Evicted(AccountId, bool), + /// Store code for benchmarks which does not check nor instrument the code. + #[cfg(feature = "runtime-benchmarks")] + fn store_code_raw(code: Vec) -> frame_support::dispatch::DispatchResult { + let schedule = >::current_schedule(); + PrefabWasmModule::store_code_unchecked(code, &schedule)?; + Ok(()) + } - /// Restoration for a contract has been successful. - /// \[donor, dest, code_hash, rent_allowance\] - /// - /// # Params - /// - /// - `donor`: `AccountId`: Account ID of the restoring contract - /// - `dest`: `AccountId`: Account ID of the restored contract - /// - `code_hash`: `Hash`: Code hash of the restored contract - /// - `rent_allowance: `Balance`: Rent allowance of the restored contract - Restored(AccountId, AccountId, Hash, Balance), + /// This exists so that benchmarks can determine the weight of running an instrumentation. + #[cfg(feature = "runtime-benchmarks")] + fn reinstrument_module( + module: &mut PrefabWasmModule, + schedule: &Schedule + ) -> frame_support::dispatch::DispatchResult { + self::wasm::reinstrument(module, schedule) + } +} - /// Code with the specified hash has been stored. - /// \[code_hash\] - CodeStored(Hash), +/// Information for managing an account and its sub trie abstraction. +/// This is the required info to cache for an account +#[derive(Encode, Decode, RuntimeDebug)] +pub enum ContractInfo { + Alive(AliveContractInfo), + Tombstone(TombstoneContractInfo), +} - /// Triggered when the current \[schedule\] is updated. - ScheduleUpdated(u32), +impl ContractInfo { + /// If contract is alive then return some alive info + pub fn get_alive(self) -> Option> { + if let ContractInfo::Alive(alive) = self { + Some(alive) + } else { + None + } + } + /// If contract is alive then return some reference to alive info + pub fn as_alive(&self) -> Option<&AliveContractInfo> { + if let ContractInfo::Alive(ref alive) = self { + Some(alive) + } else { + None + } + } + /// If contract is alive then return some mutable reference to alive info + pub fn as_alive_mut(&mut self) -> Option<&mut AliveContractInfo> { + if let ContractInfo::Alive(ref mut alive) = self { + Some(alive) + } else { + None + } + } - /// An event deposited upon execution of a contract from the account. - /// \[account, data\] - ContractExecution(AccountId, Vec), + /// If contract is tombstone then return some tombstone info + pub fn get_tombstone(self) -> Option> { + if let ContractInfo::Tombstone(tombstone) = self { + Some(tombstone) + } else { + None + } + } + /// If contract is tombstone then return some reference to tombstone info + pub fn as_tombstone(&self) -> Option<&TombstoneContractInfo> { + if let ContractInfo::Tombstone(ref tombstone) = self { + Some(tombstone) + } else { + None + } + } + /// If contract is tombstone then return some mutable reference to tombstone info + pub fn as_tombstone_mut(&mut self) -> Option<&mut TombstoneContractInfo> { + if let ContractInfo::Tombstone(ref mut tombstone) = self { + Some(tombstone) + } else { + None + } } } -decl_storage! { - trait Store for Module as Contracts - where - T::AccountId: UncheckedFrom + AsRef<[u8]> - { - /// Current cost schedule for contracts. - CurrentSchedule get(fn current_schedule) config(): Schedule = Default::default(); - /// A mapping from an original code hash to the original code, untouched by instrumentation. - pub PristineCode: map hasher(identity) CodeHash => Option>; - /// A mapping between an original code hash and instrumented wasm code, ready for execution. - pub CodeStorage: map hasher(identity) CodeHash => Option; - /// The subtrie counter. - pub AccountCounter: u64 = 0; - /// The code associated with a given account. - /// - /// TWOX-NOTE: SAFE since `AccountId` is a secure hash. - pub ContractInfoOf: map hasher(twox_64_concat) T::AccountId => Option>; - /// Evicted contracts that await child trie deletion. - /// - /// Child trie deletion is a heavy operation depending on the amount of storage items - /// stored in said trie. Therefore this operation is performed lazily in `on_initialize`. - pub DeletionQueue: Vec; +/// Information for managing an account and its sub trie abstraction. +/// This is the required info to cache for an account. +#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)] +pub struct RawAliveContractInfo { + /// Unique ID for the subtree encoded as a bytes vector. + pub trie_id: TrieId, + /// The total number of bytes used by this contract. + /// + /// It is a sum of each key-value pair stored by this contract. + pub storage_size: u32, + /// The total number of key-value pairs in storage of this contract. + pub pair_count: u32, + /// The code associated with a given account. + pub code_hash: CodeHash, + /// Pay rent at most up to this value. + pub rent_allowance: Balance, + /// The amount of rent that was payed by the contract over its whole lifetime. + /// + /// A restored contract starts with a value of zero just like a new contract. + pub rent_payed: Balance, + /// Last block rent has been payed. + pub deduct_block: BlockNumber, + /// Last block child storage has been written. + pub last_write: Option, +} + +impl RawAliveContractInfo { + /// Associated child trie unique id is built from the hash part of the trie id. + pub fn child_trie_info(&self) -> ChildInfo { + child_trie_info(&self.trie_id[..]) } } -/// In-memory cache of configuration values. -/// -/// We assume that these values can't be changed in the -/// course of transaction execution. -pub struct ConfigCache { - pub schedule: Schedule, - pub existential_deposit: BalanceOf, - pub tombstone_deposit: BalanceOf, - pub max_depth: u32, - pub max_value_size: u32, +/// Associated child trie unique id is built from the hash part of the trie id. +pub(crate) fn child_trie_info(trie_id: &[u8]) -> ChildInfo { + ChildInfo::new_default(trie_id) } -impl ConfigCache +#[derive(Encode, Decode, PartialEq, Eq, RuntimeDebug)] +pub struct RawTombstoneContractInfo(H, PhantomData); + +impl RawTombstoneContractInfo where - T::AccountId: UncheckedFrom + AsRef<[u8]> + H: Member + MaybeSerializeDeserialize+ Debug + + AsRef<[u8]> + AsMut<[u8]> + Copy + Default + + sp_std::hash::Hash + Codec, + Hasher: Hash, { - fn preload() -> ConfigCache { - ConfigCache { - schedule: >::current_schedule(), - existential_deposit: T::Currency::minimum_balance(), - tombstone_deposit: T::TombstoneDeposit::get(), - max_depth: T::MaxDepth::get(), - max_value_size: T::MaxValueSize::get(), - } - } - - /// Subsistence threshold is the extension of the minimum balance (aka existential deposit) by the - /// tombstone deposit, required for leaving a tombstone. - /// - /// Rent or any contract initiated balance transfer mechanism cannot make the balance lower - /// than the subsistence threshold in order to guarantee that a tombstone is created. - /// - /// The only way to completely kill a contract without a tombstone is calling `seal_terminate`. - pub fn subsistence_threshold(&self) -> BalanceOf { - self.existential_deposit.saturating_add(self.tombstone_deposit) + fn new(storage_root: &[u8], code_hash: H) -> Self { + let mut buf = Vec::new(); + storage_root.using_encoded(|encoded| buf.extend_from_slice(encoded)); + buf.extend_from_slice(code_hash.as_ref()); + RawTombstoneContractInfo(::hash(&buf[..]), PhantomData) } +} - /// The same as `subsistence_threshold` but without the need for a preloaded instance. - /// - /// This is for cases where this value is needed in rent calculation rather than - /// during contract execution. - pub fn subsistence_threshold_uncached() -> BalanceOf { - T::Currency::minimum_balance().saturating_add(T::TombstoneDeposit::get()) +impl From> for ContractInfo { + fn from(alive_info: AliveContractInfo) -> Self { + Self::Alive(alive_info) } } diff --git a/frame/contracts/src/rent.rs b/frame/contracts/src/rent.rs index 0bf229d494696..85b8eff989319 100644 --- a/frame/contracts/src/rent.rs +++ b/frame/contracts/src/rent.rs @@ -18,15 +18,15 @@ //! A module responsible for computing the right amount of weight and charging it. use crate::{ - AliveContractInfo, BalanceOf, ContractInfo, ContractInfoOf, Module, RawEvent, - TombstoneContractInfo, Config, CodeHash, ConfigCache, Error, - storage::Storage, + AliveContractInfo, BalanceOf, ContractInfo, ContractInfoOf, Module, Event, + TombstoneContractInfo, Config, CodeHash, Error, + storage::Storage, wasm::PrefabWasmModule, exec::Executable, }; use sp_std::prelude::*; use sp_io::hashing::blake2_256; use sp_core::crypto::UncheckedFrom; use frame_support::{ - debug, StorageMap, + debug, storage::child, traits::{Currency, ExistenceRequirement, Get, OnUnbalanced, WithdrawReasons}, }; @@ -86,12 +86,13 @@ enum Verdict { Charge { amount: OutstandingAmount }, } -pub struct Rent(sp_std::marker::PhantomData); +pub struct Rent(sp_std::marker::PhantomData<(T, E)>); -impl Rent +impl Rent where T: Config, - T::AccountId: UncheckedFrom + AsRef<[u8]> + T::AccountId: UncheckedFrom + AsRef<[u8]>, + E: Executable, { /// Returns a fee charged per block from the contract. /// @@ -99,10 +100,11 @@ where /// then the fee can drop to zero. fn compute_fee_per_block( free_balance: &BalanceOf, - contract: &AliveContractInfo + contract: &AliveContractInfo, + code_size_share: u32, ) -> BalanceOf { let uncovered_by_balance = T::DepositPerStorageByte::get() - .saturating_mul(contract.storage_size.into()) + .saturating_mul(contract.storage_size.saturating_add(code_size_share).into()) .saturating_add( T::DepositPerStorageItem::get() .saturating_mul(contract.pair_count.into()) @@ -123,7 +125,7 @@ where free_balance: &BalanceOf, contract: &AliveContractInfo, ) -> Option> { - let subsistence_threshold = ConfigCache::::subsistence_threshold_uncached(); + let subsistence_threshold = Module::::subsistence_threshold(); // Reserved balance contributes towards the subsistence threshold to stay consistent // with the existential deposit where the reserved balance is also counted. if *total_balance < subsistence_threshold { @@ -148,6 +150,7 @@ where current_block_number: T::BlockNumber, handicap: T::BlockNumber, contract: &AliveContractInfo, + code_size: u32, ) -> Verdict { // How much block has passed since the last deduction for the contract. let blocks_passed = { @@ -164,7 +167,7 @@ where let free_balance = T::Currency::free_balance(account); // An amount of funds to charge per block for storage taken up by the contract. - let fee_per_block = Self::compute_fee_per_block(&free_balance, contract); + let fee_per_block = Self::compute_fee_per_block(&free_balance, contract, code_size); if fee_per_block.is_zero() { // The rent deposit offset reduced the fee to 0. This means that the contract // gets the rent for free. @@ -228,19 +231,22 @@ where /// Enacts the given verdict and returns the updated `ContractInfo`. /// /// `alive_contract_info` should be from the same address as `account`. + /// + /// # Note + /// + /// if `evictable_code` is `None` an `Evict` verdict will not be enacted. This is for + /// when calling this function during a `call` where access to the soon to be evicted + /// contract should be denied but storage should be left unmodified. fn enact_verdict( account: &T::AccountId, alive_contract_info: AliveContractInfo, current_block_number: T::BlockNumber, verdict: Verdict, - allow_eviction: bool, - ) -> Result>, DispatchError> { - match verdict { - Verdict::Exempt => return Ok(Some(ContractInfo::Alive(alive_contract_info))), - Verdict::Evict { amount: _ } if !allow_eviction => { - Ok(None) - } - Verdict::Evict { amount } => { + evictable_code: Option>, + ) -> Result>, DispatchError> { + match (verdict, evictable_code) { + (Verdict::Exempt, _) => return Ok(Some(alive_contract_info)), + (Verdict::Evict { amount }, Some(code)) => { // We need to remove the trie first because it is the only operation // that can fail and this function is called without a storage // transaction when called through `claim_surcharge`. @@ -261,19 +267,23 @@ where ); let tombstone_info = ContractInfo::Tombstone(tombstone); >::insert(account, &tombstone_info); - >::deposit_event(RawEvent::Evicted(account.clone(), true)); - Ok(Some(tombstone_info)) + code.drop_from_storage(); + >::deposit_event(Event::Evicted(account.clone())); + Ok(None) + } + (Verdict::Evict { amount: _ }, None) => { + Ok(None) } - Verdict::Charge { amount } => { - let contract_info = ContractInfo::Alive(AliveContractInfo:: { + (Verdict::Charge { amount }, _) => { + let contract = ContractInfo::Alive(AliveContractInfo:: { rent_allowance: alive_contract_info.rent_allowance - amount.peek(), deduct_block: current_block_number, rent_payed: alive_contract_info.rent_payed.saturating_add(amount.peek()), ..alive_contract_info }); - >::insert(account, &contract_info); + >::insert(account, &contract); amount.withdraw(account); - Ok(Some(contract_info)) + Ok(Some(contract.get_alive().expect("We just constructed it as alive. qed"))) } } } @@ -283,21 +293,20 @@ where /// This functions does **not** evict the contract. It returns `None` in case the /// contract is in need of eviction. [`try_eviction`] must /// be called to perform the eviction. - pub fn charge(account: &T::AccountId) -> Result>, DispatchError> { - let contract_info = >::get(account); - let alive_contract_info = match contract_info { - None | Some(ContractInfo::Tombstone(_)) => return Ok(contract_info), - Some(ContractInfo::Alive(contract)) => contract, - }; - + pub fn charge( + account: &T::AccountId, + contract: AliveContractInfo, + code_size: u32, + ) -> Result>, DispatchError> { let current_block_number = >::block_number(); let verdict = Self::consider_case( account, current_block_number, Zero::zero(), - &alive_contract_info, + &contract, + code_size, ); - Self::enact_verdict(account, alive_contract_info, current_block_number, verdict, false) + Self::enact_verdict(account, contract, current_block_number, verdict, None) } /// Process a report that a contract under the given address should be evicted. @@ -316,18 +325,21 @@ where pub fn try_eviction( account: &T::AccountId, handicap: T::BlockNumber, - ) -> Result>, DispatchError> { + ) -> Result<(Option>, u32), DispatchError> { let contract = >::get(account); let contract = match contract { - None | Some(ContractInfo::Tombstone(_)) => return Ok(None), + None | Some(ContractInfo::Tombstone(_)) => return Ok((None, 0)), Some(ContractInfo::Alive(contract)) => contract, }; + let module = PrefabWasmModule::::from_storage_noinstr(contract.code_hash)?; + let code_len = module.code_len(); let current_block_number = >::block_number(); let verdict = Self::consider_case( account, current_block_number, handicap, &contract, + module.occupied_storage(), ); // Enact the verdict only if the contract gets removed. @@ -339,10 +351,12 @@ where .map(|a| a.peek()) .unwrap_or_else(|| >::zero()) .saturating_add(contract.rent_payed); - Self::enact_verdict(account, contract, current_block_number, verdict, true)?; - Ok(Some(rent_payed)) + Self::enact_verdict( + account, contract, current_block_number, verdict, Some(module), + )?; + Ok((Some(rent_payed), code_len)) } - _ => Ok(None), + _ => Ok((None, code_len)), } } @@ -367,26 +381,33 @@ where None | Some(ContractInfo::Tombstone(_)) => return Err(IsTombstone), Some(ContractInfo::Alive(contract)) => contract, }; + let module = PrefabWasmModule::from_storage_noinstr(alive_contract_info.code_hash) + .map_err(|_| IsTombstone)?; + let code_size = module.occupied_storage(); let current_block_number = >::block_number(); let verdict = Self::consider_case( account, current_block_number, Zero::zero(), &alive_contract_info, + code_size, + ); + let new_contract_info = Self::enact_verdict( + account, alive_contract_info, current_block_number, verdict, Some(module), ); - let new_contract_info = - Self::enact_verdict(account, alive_contract_info, current_block_number, verdict, false); // Check what happened after enaction of the verdict. let alive_contract_info = match new_contract_info.map_err(|_| IsTombstone)? { - None | Some(ContractInfo::Tombstone(_)) => return Err(IsTombstone), - Some(ContractInfo::Alive(contract)) => contract, + None => return Err(IsTombstone), + Some(contract) => contract, }; // Compute how much would the fee per block be with the *updated* balance. let total_balance = T::Currency::total_balance(account); let free_balance = T::Currency::free_balance(account); - let fee_per_block = Self::compute_fee_per_block(&free_balance, &alive_contract_info); + let fee_per_block = Self::compute_fee_per_block( + &free_balance, &alive_contract_info, code_size, + ); if fee_per_block.is_zero() { return Ok(RentProjection::NoEviction); } @@ -418,6 +439,7 @@ where /// Restores the destination account using the origin as prototype. /// /// The restoration will be performed iff: + /// - the supplied code_hash does still exist on-chain /// - origin exists and is alive, /// - the origin's storage is not written in the current block /// - the restored account has tombstone @@ -426,28 +448,32 @@ where /// Upon succesful restoration, `origin` will be destroyed, all its funds are transferred to /// the restored account. The restored account will inherit the last write block and its last /// deduct block will be set to the current block. + /// + /// # Return Value + /// + /// Result<(CallerCodeSize, DestCodeSize), (DispatchError, CallerCodeSize, DestCodesize)> pub fn restore_to( origin: T::AccountId, dest: T::AccountId, code_hash: CodeHash, rent_allowance: BalanceOf, delta: Vec, - ) -> Result<(), DispatchError> { + ) -> Result<(u32, u32), (DispatchError, u32, u32)> { let mut origin_contract = >::get(&origin) .and_then(|c| c.get_alive()) - .ok_or(Error::::InvalidSourceContract)?; + .ok_or((Error::::InvalidSourceContract.into(), 0, 0))?; let child_trie_info = origin_contract.child_trie_info(); let current_block = >::block_number(); if origin_contract.last_write == Some(current_block) { - return Err(Error::::InvalidContractOrigin.into()); + return Err((Error::::InvalidContractOrigin.into(), 0, 0)); } let dest_tombstone = >::get(&dest) .and_then(|c| c.get_tombstone()) - .ok_or(Error::::InvalidDestinationContract)?; + .ok_or((Error::::InvalidDestinationContract.into(), 0, 0))?; let last_write = if !delta.is_empty() { Some(current_block) @@ -455,6 +481,9 @@ where origin_contract.last_write }; + // Fails if the code hash does not exist on chain + let caller_code_len = E::add_user(code_hash).map_err(|e| (e, 0, 0))?; + // We are allowed to eagerly modify storage even though the function can // fail later due to tombstones not matching. This is because the restoration // is always called from a contract and therefore in a storage transaction. @@ -477,12 +506,13 @@ where ); if tombstone != dest_tombstone { - return Err(Error::::InvalidTombstone.into()); + return Err((Error::::InvalidTombstone.into(), caller_code_len, 0)); } origin_contract.storage_size -= bytes_taken; >::remove(&origin); + let tombstone_code_len = E::remove_user(origin_contract.code_hash); >::insert(&dest, ContractInfo::Alive(AliveContractInfo:: { trie_id: origin_contract.trie_id, storage_size: origin_contract.storage_size, @@ -498,6 +528,6 @@ where T::Currency::make_free_balance_be(&origin, >::zero()); T::Currency::deposit_creating(&dest, origin_free_balance); - Ok(()) + Ok((caller_code_len, tombstone_code_len)) } } diff --git a/frame/contracts/src/schedule.rs b/frame/contracts/src/schedule.rs index 63e3f3c285898..c86134bc415d1 100644 --- a/frame/contracts/src/schedule.rs +++ b/frame/contracts/src/schedule.rs @@ -104,10 +104,6 @@ pub struct Limits { /// The maximum length of a subject in bytes used for PRNG generation. pub subject_len: u32, - - /// The maximum length of a contract code in bytes. This limit applies to the uninstrumented - /// and pristine form of the code as supplied to `put_code`. - pub code_size: u32, } impl Limits { @@ -250,9 +246,18 @@ pub struct HostFnWeights { /// Weight of calling `seal_terminate`. pub terminate: Weight, + /// Weight per byte of the terminated contract. + pub terminate_per_code_byte: Weight, + /// Weight of calling `seal_restore_to`. pub restore_to: Weight, + /// Weight per byte of the restoring contract. + pub restore_to_per_caller_code_byte: Weight, + + /// Weight per byte of the restored contract. + pub restore_to_per_tombstone_code_byte: Weight, + /// Weight per delta key supplied to `seal_restore_to`. pub restore_to_per_delta: Weight, @@ -292,6 +297,9 @@ pub struct HostFnWeights { /// Weight of calling `seal_call`. pub call: Weight, + /// Weight per byte of the called contract. + pub call_per_code_byte: Weight, + /// Weight surcharge that is claimed if `seal_call` does a balance transfer. pub call_transfer_surcharge: Weight, @@ -304,6 +312,9 @@ pub struct HostFnWeights { /// Weight of calling `seal_instantiate`. pub instantiate: Weight, + /// Weight per byte of the instantiated contract. + pub instantiate_per_code_byte: Weight, + /// Weight per input byte supplied to `seal_instantiate`. pub instantiate_per_input_byte: Weight, @@ -443,7 +454,6 @@ impl Default for Limits { table_size: 4096, br_table_size: 256, subject_len: 32, - code_size: 512 * 1024, } } } @@ -528,8 +538,11 @@ impl Default for HostFnWeights { r#return: cost!(seal_return), return_per_byte: cost_byte!(seal_return_per_kb), terminate: cost!(seal_terminate), + terminate_per_code_byte: cost_byte!(seal_terminate_per_code_kb), restore_to: cost!(seal_restore_to), - restore_to_per_delta: cost_batched!(seal_restore_to_per_delta), + restore_to_per_caller_code_byte: cost_byte_args!(seal_restore_to_per_code_kb_delta, 1, 0, 0), + restore_to_per_tombstone_code_byte: cost_byte_args!(seal_restore_to_per_code_kb_delta, 0, 1, 0), + restore_to_per_delta: cost_batched_args!(seal_restore_to_per_code_kb_delta, 0, 0, 1), random: cost_batched!(seal_random), deposit_event: cost_batched!(seal_deposit_event), deposit_event_per_topic: cost_batched_args!(seal_deposit_event_per_topic_and_kb, 1, 0), @@ -542,13 +555,15 @@ impl Default for HostFnWeights { get_storage_per_byte: cost_byte_batched!(seal_get_storage_per_kb), transfer: cost_batched!(seal_transfer), call: cost_batched!(seal_call), - call_transfer_surcharge: cost_batched_args!(seal_call_per_transfer_input_output_kb, 1, 0, 0), - call_per_input_byte: cost_byte_batched_args!(seal_call_per_transfer_input_output_kb, 0, 1, 0), - call_per_output_byte: cost_byte_batched_args!(seal_call_per_transfer_input_output_kb, 0, 0, 1), + call_per_code_byte: cost_byte_batched_args!(seal_call_per_code_transfer_input_output_kb, 1, 0, 0, 0), + call_transfer_surcharge: cost_batched_args!(seal_call_per_code_transfer_input_output_kb, 0, 1, 0, 0), + call_per_input_byte: cost_byte_batched_args!(seal_call_per_code_transfer_input_output_kb, 0, 0, 1, 0), + call_per_output_byte: cost_byte_batched_args!(seal_call_per_code_transfer_input_output_kb, 0, 0, 0, 1), instantiate: cost_batched!(seal_instantiate), - instantiate_per_input_byte: cost_byte_batched_args!(seal_instantiate_per_input_output_salt_kb, 1, 0, 0), - instantiate_per_output_byte: cost_byte_batched_args!(seal_instantiate_per_input_output_salt_kb, 0, 1, 0), - instantiate_per_salt_byte: cost_byte_batched_args!(seal_instantiate_per_input_output_salt_kb, 0, 0, 1), + instantiate_per_code_byte: cost_byte_batched_args!(seal_instantiate_per_code_input_output_salt_kb, 1, 0, 0, 0), + instantiate_per_input_byte: cost_byte_batched_args!(seal_instantiate_per_code_input_output_salt_kb, 0, 1, 0, 0), + instantiate_per_output_byte: cost_byte_batched_args!(seal_instantiate_per_code_input_output_salt_kb, 0, 0, 1, 0), + instantiate_per_salt_byte: cost_byte_batched_args!(seal_instantiate_per_code_input_output_salt_kb, 0, 0, 0, 1), hash_sha2_256: cost_batched!(seal_hash_sha2_256), hash_sha2_256_per_byte: cost_byte_batched!(seal_hash_sha2_256_per_kb), hash_keccak_256: cost_batched!(seal_hash_keccak_256), diff --git a/frame/contracts/src/storage.rs b/frame/contracts/src/storage.rs index 030f62fc4088e..5fb603b334a63 100644 --- a/frame/contracts/src/storage.rs +++ b/frame/contracts/src/storage.rs @@ -31,9 +31,8 @@ use sp_runtime::traits::{Bounded, Saturating, Zero}; use sp_core::crypto::UncheckedFrom; use frame_support::{ dispatch::DispatchResult, - StorageMap, debug, - storage::{child::{self, KillOutcome}, StorageValue}, + storage::child::{self, KillChildStorageResult}, traits::Get, weights::Weight, }; @@ -164,29 +163,28 @@ where account: &AccountIdOf, trie_id: TrieId, ch: CodeHash, - ) -> Result<(), &'static str> { - >::mutate(account, |maybe_contract_info| { - if maybe_contract_info.is_some() { - return Err("Alive contract or tombstone already exists"); + ) -> DispatchResult { + >::try_mutate(account, |existing| { + if existing.is_some() { + return Err(Error::::DuplicateContract.into()); } - *maybe_contract_info = Some( - AliveContractInfo:: { - code_hash: ch, - storage_size: 0, - trie_id, - deduct_block: - // We want to charge rent for the first block in advance. Therefore we - // treat the contract as if it was created in the last block and then - // charge rent for it during instantation. - >::block_number().saturating_sub(1u32.into()), - rent_allowance: >::max_value(), - rent_payed: >::zero(), - pair_count: 0, - last_write: None, - } - .into(), - ); + let contract = AliveContractInfo:: { + code_hash: ch, + storage_size: 0, + trie_id, + deduct_block: + // We want to charge rent for the first block in advance. Therefore we + // treat the contract as if it was created in the last block and then + // charge rent for it during instantiation. + >::block_number().saturating_sub(1u32.into()), + rent_allowance: >::max_value(), + rent_payed: >::zero(), + pair_count: 0, + last_write: None, + }; + + *existing = Some(contract.into()); Ok(()) }) @@ -197,10 +195,10 @@ where /// You must make sure that the contract is also removed or converted into a tombstone /// when queuing the trie for deletion. pub fn queue_trie_for_deletion(contract: &AliveContractInfo) -> DispatchResult { - if DeletionQueue::decode_len().unwrap_or(0) >= T::DeletionQueueDepth::get() as usize { + if >::decode_len().unwrap_or(0) >= T::DeletionQueueDepth::get() as usize { Err(Error::::DeletionQueueFull.into()) } else { - DeletionQueue::append(DeletedContract { + >::append(DeletedContract { pair_count: contract.pair_count, trie_id: contract.trie_id.clone(), }); @@ -235,7 +233,7 @@ where /// It returns the amount of weight used for that task or `None` when no weight was used /// apart from the base weight. pub fn process_deletion_queue_batch(weight_limit: Weight) -> Weight { - let queue_len = DeletionQueue::decode_len().unwrap_or(0); + let queue_len = >::decode_len().unwrap_or(0); if queue_len == 0 { return weight_limit; } @@ -252,7 +250,7 @@ where return weight_limit; } - let mut queue = DeletionQueue::get(); + let mut queue = >::get(); while !queue.is_empty() && remaining_key_budget > 0 { // Cannot panic due to loop condition @@ -271,20 +269,20 @@ where let removed = queue.swap_remove(0); match outcome { // This should not happen as our budget was large enough to remove all keys. - KillOutcome::SomeRemaining => { + KillChildStorageResult::SomeRemaining(_) => { debug::error!( "After deletion keys are remaining in this child trie: {:?}", removed.trie_id, ); }, - KillOutcome::AllRemoved => (), + KillChildStorageResult::AllRemoved(_) => (), } } remaining_key_budget = remaining_key_budget .saturating_sub(remaining_key_budget.min(pair_count)); } - DeletionQueue::put(queue); + >::put(queue); weight_limit.saturating_sub(weight_per_key.saturating_mul(remaining_key_budget as Weight)) } @@ -294,7 +292,7 @@ where use sp_runtime::traits::Hash; // Note that skipping a value due to error is not an issue here. // We only need uniqueness, not sequence. - let new_seed = AccountCounter::mutate(|v| { + let new_seed = >::mutate(|v| { *v = v.wrapping_add(1); *v }); @@ -323,6 +321,6 @@ where trie_id: vec![], }) .collect(); - DeletionQueue::put(queue); + >::put(queue); } } diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 96bcf99bf8e81..3fa806799e95c 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -16,14 +16,16 @@ // limitations under the License. use crate::{ - BalanceOf, ContractInfo, ContractInfoOf, GenesisConfig, Module, - RawAliveContractInfo, RawEvent, Config, Schedule, gas::Gas, - Error, ConfigCache, RuntimeReturnCode, storage::Storage, + BalanceOf, ContractInfo, ContractInfoOf, Module, + RawAliveContractInfo, Config, Schedule, + Error, storage::Storage, chain_extension::{ Result as ExtensionResult, Environment, ChainExtension, Ext, SysConfig, RetVal, UncheckedFrom, InitState, ReturnFlags, }, - exec::AccountIdOf, + exec::{AccountIdOf, Executable}, wasm::PrefabWasmModule, + weights::WeightInfo, + wasm::ReturnCode as RuntimeReturnCode, }; use assert_matches::assert_matches; use codec::Encode; @@ -34,52 +36,45 @@ use sp_runtime::{ }; use sp_io::hashing::blake2_256; use frame_support::{ - assert_ok, assert_err, assert_err_ignore_postinfo, impl_outer_dispatch, impl_outer_event, - impl_outer_origin, parameter_types, StorageMap, assert_storage_noop, - traits::{Currency, ReservableCurrency, OnInitialize}, + assert_ok, assert_err, assert_err_ignore_postinfo, + parameter_types, assert_storage_noop, + traits::{Currency, ReservableCurrency, OnInitialize, GenesisBuild}, weights::{Weight, PostDispatchInfo, DispatchClass, constants::WEIGHT_PER_SECOND}, dispatch::DispatchErrorWithPostInfo, storage::child, }; use frame_system::{self as system, EventRecord, Phase}; +use pretty_assertions::assert_eq; -mod contracts { - // Re-export contents of the root. This basically - // needs to give a name for the current crate. - // This hack is required for `impl_outer_event!`. - pub use super::super::*; - pub use frame_support::impl_outer_event; -} +use crate as pallet_contracts; -use pallet_balances as balances; +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; -impl_outer_event! { - pub enum MetaEvent for Test { - system, - balances, - contracts, - } -} -impl_outer_origin! { - pub enum Origin for Test where system = frame_system { } -} -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - balances::Balances, - contracts::Contracts, +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, + Randomness: pallet_randomness_collective_flip::{Module, Call, Storage}, + Contracts: pallet_contracts::{Module, Call, Config, Storage, Event}, } -} +); #[macro_use] pub mod test_utils { use super::{Test, Balances}; use crate::{ - ConfigCache, ContractInfoOf, CodeHash, storage::Storage, exec::{StorageKey, AccountIdOf}, + Module as Contracts, }; - use frame_support::{StorageMap, traits::Currency}; + use frame_support::traits::Currency; pub fn set_storage(addr: &AccountIdOf, key: &StorageKey, value: Option>) { let contract_info = >::get(&addr).unwrap().get_alive().unwrap(); @@ -91,8 +86,8 @@ pub mod test_utils { } pub fn place_contract(address: &AccountIdOf, code_hash: CodeHash) { let trie_id = Storage::::generate_trie_id(address); - set_balance(address, ConfigCache::::subsistence_threshold_uncached() * 10); - Storage::::place_contract(&address, trie_id, code_hash).unwrap() + set_balance(address, Contracts::::subsistence_threshold() * 10); + Storage::::place_contract(&address, trie_id, code_hash).unwrap(); } pub fn set_balance(who: &AccountIdOf, amount: u64) { let imbalance = Balances::deposit_creating(who, amount); @@ -107,6 +102,14 @@ pub mod test_utils { assert_eq!(u32::from_le_bytes($x.data[..].try_into().unwrap()), $y as u32); }} } + macro_rules! assert_refcount { + ( $code_hash:expr , $should:expr $(,)? ) => {{ + let is = crate::CodeStorage::::get($code_hash) + .map(|m| m.refcount()) + .unwrap_or(0); + assert_eq!(is, $should); + }} + } } thread_local! { @@ -143,9 +146,10 @@ impl Default for TestExtension { } } -impl ChainExtension for TestExtension { - fn call(func_id: u32, env: Environment) -> ExtensionResult +impl ChainExtension for TestExtension { + fn call(func_id: u32, env: Environment) -> ExtensionResult where + E: Ext, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>, { match func_id { @@ -188,8 +192,6 @@ impl ChainExtension for TestExtension { } } -#[derive(Clone, Eq, PartialEq, Debug)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -210,10 +212,10 @@ impl frame_system::Config for Test { type AccountId = AccountId32; type Lookup = IdentityLookup; type Header = Header; - type Event = MetaEvent; + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -223,7 +225,7 @@ impl frame_system::Config for Test { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; - type Event = MetaEvent; + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -250,6 +252,7 @@ parameter_types! { pub const MaxValueSize: u32 = 16_384; pub const DeletionQueueDepth: u32 = 1024; pub const DeletionWeightLimit: Weight = 500_000_000_000; + pub const MaxCodeSize: u32 = 2 * 1024; } parameter_types! { @@ -266,7 +269,7 @@ impl Config for Test { type Time = Timestamp; type Randomness = Randomness; type Currency = Balances; - type Event = MetaEvent; + type Event = Event; type RentPayment = (); type SignedClaimHandicap = SignedClaimHandicap; type TombstoneDeposit = TombstoneDeposit; @@ -282,20 +285,15 @@ impl Config for Test { type ChainExtension = TestExtension; type DeletionQueueDepth = DeletionQueueDepth; type DeletionWeightLimit = DeletionWeightLimit; + type MaxCodeSize = MaxCodeSize; } -type Balances = pallet_balances::Module; -type Timestamp = pallet_timestamp::Module; -type Contracts = Module; -type System = frame_system::Module; -type Randomness = pallet_randomness_collective_flip::Module; - pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]); pub const BOB: AccountId32 = AccountId32::new([2u8; 32]); pub const CHARLIE: AccountId32 = AccountId32::new([3u8; 32]); pub const DJANGO: AccountId32 = AccountId32::new([4u8; 32]); -const GAS_LIMIT: Gas = 10_000_000_000; +const GAS_LIMIT: Weight = 10_000_000_000; pub struct ExtBuilder { existential_deposit: u64, @@ -321,7 +319,7 @@ impl ExtBuilder { pallet_balances::GenesisConfig:: { balances: vec![], }.assimilate_storage(&mut t).unwrap(); - GenesisConfig { + pallet_contracts::GenesisConfig { current_schedule: Schedule:: { enable_println: true, ..Default::default() @@ -351,12 +349,12 @@ where // Perform a call to a plain account. // The actual transfer fails because we can only call contracts. -// Then we check that no gas was used because the base costs for calling are either charged -// as part of the `call` extrinsic or by `seal_call`. +// Then we check that at least the base costs where charged (no runtime gas costs.) #[test] fn calling_plain_account_fails() { ExtBuilder::default().build().execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 100_000_000); + let base_cost = <::WeightInfo as WeightInfo>::call(0); assert_eq!( Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, Vec::new()), @@ -364,7 +362,7 @@ fn calling_plain_account_fails() { DispatchErrorWithPostInfo { error: Error::::NotCallable.into(), post_info: PostDispatchInfo { - actual_weight: Some(0), + actual_weight: Some(base_cost), pays_fee: Default::default(), }, } @@ -457,70 +455,68 @@ fn instantiate_and_call_and_deposit_event() { .build() .execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); + let subsistence = Module::::subsistence_threshold(); // Check at the end to get hash on error easily - let creation = Contracts::instantiate( + let creation = Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], ); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); - pretty_assertions::assert_eq!(System::events(), vec![ + assert_eq!(System::events(), vec![ EventRecord { phase: Phase::Initialization, - event: MetaEvent::system(frame_system::Event::NewAccount(ALICE.clone())), + event: Event::frame_system(frame_system::Event::NewAccount(ALICE.clone())), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances( - pallet_balances::RawEvent::Endowed(ALICE, 1_000_000) + event: Event::pallet_balances( + pallet_balances::Event::Endowed(ALICE, 1_000_000) ), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())), + event: Event::frame_system(frame_system::Event::NewAccount(addr.clone())), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::system(frame_system::Event::NewAccount(addr.clone())), + event: Event::pallet_balances( + pallet_balances::Event::Endowed(addr.clone(), subsistence * 100) + ), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances( - pallet_balances::RawEvent::Endowed(addr.clone(), subsistence * 3) + event: Event::pallet_balances( + pallet_balances::Event::Transfer(ALICE, addr.clone(), subsistence * 100) ), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances( - pallet_balances::RawEvent::Transfer(ALICE, addr.clone(), subsistence * 3) - ), + event: Event::pallet_contracts(crate::Event::CodeStored(code_hash.into())), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::contracts( - RawEvent::ContractExecution(addr.clone(), vec![1, 2, 3, 4]) + event: Event::pallet_contracts( + crate::Event::ContractEmitted(addr.clone(), vec![1, 2, 3, 4]) ), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::contracts(RawEvent::Instantiated(ALICE, addr.clone())), + event: Event::pallet_contracts(crate::Event::Instantiated(ALICE, addr.clone())), topics: vec![], - } + }, ]); assert_ok!(creation); @@ -538,31 +534,16 @@ fn deposit_event_max_value_limit() { .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 30_000, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], )); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); - // The instantation deducted the rent for one block immediatly - let first_rent = ::RentFraction::get() - // base_deposit - free_balance - .mul_ceil(80_000 - 30_000) - // blocks to rent - * 1; - - // Check creation - let bob_contract = ContractInfoOf::::get(addr.clone()) - .unwrap() - .get_alive() - .unwrap(); - assert_eq!(bob_contract.rent_allowance, >::max_value() - first_rent); - // Call contract with allowed storage value. assert_ok!(Contracts::call( Origin::signed(ALICE), @@ -589,6 +570,7 @@ fn deposit_event_max_value_limit() { #[test] fn run_out_of_gas() { let (wasm, code_hash) = compile_module::("run_out_of_gas").unwrap(); + let subsistence = Module::::subsistence_threshold(); ExtBuilder::default() .existential_deposit(50) @@ -596,13 +578,11 @@ fn run_out_of_gas() { .execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); - - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), - 100, + 100 * subsistence, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], )); @@ -635,43 +615,6 @@ mod call { pub fn null() -> Vec { 3u32.to_le_bytes().to_vec() } } -/// Test correspondence of set_rent code and its hash. -/// Also test that encoded extrinsic in code correspond to the correct transfer -#[test] -fn test_set_rent_code_and_hash() { - let (wasm, code_hash) = compile_module::("set_rent").unwrap(); - - ExtBuilder::default() - .existential_deposit(50) - .build() - .execute_with(|| { - let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); - - // If you ever need to update the wasm source this test will fail - // and will show you the actual hash. - assert_eq!(System::events(), vec![ - EventRecord { - phase: Phase::Initialization, - event: MetaEvent::system(frame_system::Event::NewAccount(ALICE)), - topics: vec![], - }, - EventRecord { - phase: Phase::Initialization, - event: MetaEvent::balances(pallet_balances::RawEvent::Endowed( - ALICE, 1_000_000 - )), - topics: vec![], - }, - EventRecord { - phase: Phase::Initialization, - event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())), - topics: vec![], - }, - ]); - }); -} - #[test] fn storage_size() { let (wasm, code_hash) = compile_module::("set_rent").unwrap(); @@ -683,13 +626,13 @@ fn storage_size() { .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 30_000, GAS_LIMIT, - code_hash.into(), - ::Balance::from(1_000u32).encode(), // rent allowance + wasm, + // rent_allowance + ::Balance::from(10_000u32).encode(), vec![], )); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); @@ -756,12 +699,11 @@ fn empty_kv_pairs() { .build() .execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 30_000, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], )); @@ -794,6 +736,8 @@ fn initialize_block(number: u64) { #[test] fn deduct_blocks() { let (wasm, code_hash) = compile_module::("set_rent").unwrap(); + let endowment: BalanceOf = 100_000; + let allowance: BalanceOf = 70_000; ExtBuilder::default() .existential_deposit(50) @@ -801,27 +745,33 @@ fn deduct_blocks() { .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), - 30_000, - GAS_LIMIT, code_hash.into(), - ::Balance::from(1_000u32).encode(), // rent allowance + endowment, + GAS_LIMIT, + wasm, + allowance.encode(), vec![], )); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); - ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); + let contract = ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); + let code_len: BalanceOf = + PrefabWasmModule::::from_storage_noinstr(contract.code_hash) + .unwrap() + .occupied_storage() + .into(); - // The instantation deducted the rent for one block immediatly + // The instantiation deducted the rent for one block immediately let rent0 = ::RentFraction::get() - // base_deposit + deploy_set_storage (4 bytes in 1 item) - free_balance - .mul_ceil(80_000 + 40_000 + 10_000 - 30_000) + // (base_deposit(8) + bytes in storage(4) + size of code) * byte_price + // + 1 storage item (10_000) - free_balance + .mul_ceil((8 + 4 + code_len) * 10_000 + 10_000 - endowment) // blocks to rent * 1; - let bob_contract = ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); - assert_eq!(bob_contract.rent_allowance, 1_000 - rent0); - assert_eq!(bob_contract.deduct_block, 1); - assert_eq!(Balances::free_balance(&addr), 30_000 - rent0); + assert!(rent0 > 0); + assert_eq!(contract.rent_allowance, allowance - rent0); + assert_eq!(contract.deduct_block, 1); + assert_eq!(Balances::free_balance(&addr), endowment - rent0); // Advance 4 blocks initialize_block(5); @@ -833,17 +783,15 @@ fn deduct_blocks() { // Check result let rent = ::RentFraction::get() - // base_deposit + deploy_set_storage (4 bytes in 1 item) - free_balance - .mul_ceil(80_000 + 40_000 + 10_000 - (30_000 - rent0)) - // blocks to rent + .mul_ceil((8 + 4 + code_len) * 10_000 + 10_000 - (endowment - rent0)) * 4; - let bob_contract = ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); - assert_eq!(bob_contract.rent_allowance, 1_000 - rent0 - rent); - assert_eq!(bob_contract.deduct_block, 5); - assert_eq!(Balances::free_balance(&addr), 30_000 - rent0 - rent); + let contract = ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); + assert_eq!(contract.rent_allowance, allowance - rent0 - rent); + assert_eq!(contract.deduct_block, 5); + assert_eq!(Balances::free_balance(&addr), endowment - rent0 - rent); - // Advance 7 blocks more - initialize_block(12); + // Advance 2 blocks more + initialize_block(7); // Trigger rent through call assert_ok!( @@ -852,23 +800,21 @@ fn deduct_blocks() { // Check result let rent_2 = ::RentFraction::get() - // base_deposit + deploy_set_storage (4 bytes in 1 item) - free_balance - .mul_ceil(80_000 + 40_000 + 10_000 - (30_000 - rent0 - rent)) - // blocks to rent - * 7; - let bob_contract = ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); - assert_eq!(bob_contract.rent_allowance, 1_000 - rent0 - rent - rent_2); - assert_eq!(bob_contract.deduct_block, 12); - assert_eq!(Balances::free_balance(&addr), 30_000 - rent0 - rent - rent_2); + .mul_ceil((8 + 4 + code_len) * 10_000 + 10_000 - (endowment - rent0 - rent)) + * 2; + let contract = ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); + assert_eq!(contract.rent_allowance, allowance - rent0 - rent - rent_2); + assert_eq!(contract.deduct_block, 7); + assert_eq!(Balances::free_balance(&addr), endowment - rent0 - rent - rent_2); // Second call on same block should have no effect on rent assert_ok!( Contracts::call(Origin::signed(ALICE), addr.clone(), 0, GAS_LIMIT, call::null()) ); - let bob_contract = ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); - assert_eq!(bob_contract.rent_allowance, 1_000 - rent0 - rent - rent_2); - assert_eq!(bob_contract.deduct_block, 12); - assert_eq!(Balances::free_balance(&addr), 30_000 - rent0 - rent - rent_2) + let contract = ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); + assert_eq!(contract.rent_allowance, allowance - rent0 - rent - rent_2); + assert_eq!(contract.deduct_block, 7); + assert_eq!(Balances::free_balance(&addr), endowment - rent0 - rent - rent_2) }); } @@ -885,16 +831,16 @@ fn signed_claim_surcharge_contract_removals() { #[test] fn claim_surcharge_malus() { // Test surcharge malus for inherent - claim_surcharge(27, |addr| Contracts::claim_surcharge(Origin::none(), addr, Some(ALICE)).is_ok(), true); - claim_surcharge(26, |addr| Contracts::claim_surcharge(Origin::none(), addr, Some(ALICE)).is_ok(), true); - claim_surcharge(25, |addr| Contracts::claim_surcharge(Origin::none(), addr, Some(ALICE)).is_ok(), true); - claim_surcharge(24, |addr| Contracts::claim_surcharge(Origin::none(), addr, Some(ALICE)).is_ok(), false); + claim_surcharge(9, |addr| Contracts::claim_surcharge(Origin::none(), addr, Some(ALICE)).is_ok(), true); + claim_surcharge(8, |addr| Contracts::claim_surcharge(Origin::none(), addr, Some(ALICE)).is_ok(), true); + claim_surcharge(7, |addr| Contracts::claim_surcharge(Origin::none(), addr, Some(ALICE)).is_ok(), true); + claim_surcharge(6, |addr| Contracts::claim_surcharge(Origin::none(), addr, Some(ALICE)).is_ok(), false); // Test surcharge malus for signed - claim_surcharge(27, |addr| Contracts::claim_surcharge(Origin::signed(ALICE), addr, None).is_ok(), true); - claim_surcharge(26, |addr| Contracts::claim_surcharge(Origin::signed(ALICE), addr, None).is_ok(), false); - claim_surcharge(25, |addr| Contracts::claim_surcharge(Origin::signed(ALICE), addr, None).is_ok(), false); - claim_surcharge(24, |addr| Contracts::claim_surcharge(Origin::signed(ALICE), addr, None).is_ok(), false); + claim_surcharge(9, |addr| Contracts::claim_surcharge(Origin::signed(ALICE), addr, None).is_ok(), true); + claim_surcharge(8, |addr| Contracts::claim_surcharge(Origin::signed(ALICE), addr, None).is_ok(), false); + claim_surcharge(7, |addr| Contracts::claim_surcharge(Origin::signed(ALICE), addr, None).is_ok(), false); + claim_surcharge(6, |addr| Contracts::claim_surcharge(Origin::signed(ALICE), addr, None).is_ok(), false); } /// Claim surcharge with the given trigger_call at the given blocks. @@ -908,12 +854,12 @@ fn claim_surcharge(blocks: u64, trigger_call: impl Fn(AccountIdOf) -> bool .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), - 30_000, - GAS_LIMIT, code_hash.into(), - ::Balance::from(1_000u32).encode(), // rent allowance + 100_000, + GAS_LIMIT, + wasm, + ::Balance::from(30_000u32).encode(), // rent allowance vec![], )); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); @@ -947,12 +893,12 @@ fn removals(trigger_call: impl Fn(AccountIdOf) -> bool) { .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm.clone())); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), - 500, - GAS_LIMIT, code_hash.into(), - ::Balance::from(1_000u32).encode(), // rent allowance + 70_000, + GAS_LIMIT, + wasm.clone(), + ::Balance::from(100_000u32).encode(), // rent allowance vec![], )); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); @@ -960,7 +906,7 @@ fn removals(trigger_call: impl Fn(AccountIdOf) -> bool) { .unwrap().get_alive().unwrap().rent_allowance; let balance = Balances::free_balance(&addr); - let subsistence_threshold = ConfigCache::::subsistence_threshold_uncached(); + let subsistence_threshold = Module::::subsistence_threshold(); // Trigger rent must have no effect assert!(!trigger_call(addr.clone())); @@ -994,13 +940,12 @@ fn removals(trigger_call: impl Fn(AccountIdOf) -> bool) { .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm.clone())); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), - 30_000, + 100_000, GAS_LIMIT, - code_hash.into(), - ::Balance::from(1000u32).encode(), // rent allowance + wasm.clone(), + ::Balance::from(70_000u32).encode(), // rent allowance vec![], )); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); @@ -1030,7 +975,7 @@ fn removals(trigger_call: impl Fn(AccountIdOf) -> bool) { .get_tombstone() .is_some()); // Balance should be initial balance - initial rent_allowance - assert_eq!(Balances::free_balance(&addr), 29000); + assert_eq!(Balances::free_balance(&addr), 30_000); // Advance blocks initialize_block(20); @@ -1041,7 +986,7 @@ fn removals(trigger_call: impl Fn(AccountIdOf) -> bool) { .unwrap() .get_tombstone() .is_some()); - assert_eq!(Balances::free_balance(&addr), 29000); + assert_eq!(Balances::free_balance(&addr), 30_000); }); // Balance reached and inferior to subsistence threshold @@ -1050,15 +995,14 @@ fn removals(trigger_call: impl Fn(AccountIdOf) -> bool) { .build() .execute_with(|| { // Create - let _ = Balances::deposit_creating(&ALICE, 1_000_000); - let subsistence_threshold = ConfigCache::::subsistence_threshold_uncached(); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm.clone())); - assert_ok!(Contracts::instantiate( + let subsistence_threshold = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, subsistence_threshold * 1000); + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence_threshold * 3, + subsistence_threshold * 100, GAS_LIMIT, - code_hash.into(), - ::Balance::from(1_000u32).encode(), // rent allowance + wasm, + (subsistence_threshold * 100).encode(), // rent allowance vec![], )); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); @@ -1081,7 +1025,7 @@ fn removals(trigger_call: impl Fn(AccountIdOf) -> bool) { balance, ); - // Make contract have exactly the subsitence threshold + // Make contract have exactly the subsistence threshold Balances::make_free_balance_be(&addr, subsistence_threshold); assert_eq!(Balances::free_balance(&addr), subsistence_threshold); @@ -1114,12 +1058,13 @@ fn call_removed_contract() { .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm.clone())); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 30_000, - GAS_LIMIT, code_hash.into(), - ::Balance::from(1_000u32).encode(), // rent allowance + GAS_LIMIT, + wasm, + // rent allowance + ::Balance::from(10_000u32).encode(), vec![], )); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); @@ -1162,27 +1107,29 @@ fn default_rent_allowance_on_instantiate() { .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 30_000, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], )); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); + let contract = ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); + let code_len: BalanceOf = + PrefabWasmModule::::from_storage_noinstr(contract.code_hash) + .unwrap() + .occupied_storage() + .into(); - // The instantation deducted the rent for one block immediatly + // The instantiation deducted the rent for one block immediately let first_rent = ::RentFraction::get() - // base_deposit - free_balance - .mul_ceil(80_000 - 30_000) + // (base_deposit(8) + code_len) * byte_price - free_balance + .mul_ceil((8 + code_len) * 10_000 - 30_000) // blocks to rent * 1; - - // Check creation - let bob_contract = ContractInfoOf::::get(&addr).unwrap().get_alive().unwrap(); - assert_eq!(bob_contract.rent_allowance, >::max_value() - first_rent); + assert_eq!(contract.rent_allowance, >::max_value() - first_rent); // Advance blocks initialize_block(5); @@ -1193,84 +1140,160 @@ fn default_rent_allowance_on_instantiate() { ); // Check contract is still alive - let bob_contract = ContractInfoOf::::get(&addr).unwrap().get_alive(); - assert!(bob_contract.is_some()) + let contract = ContractInfoOf::::get(&addr).unwrap().get_alive(); + assert!(contract.is_some()) }); } #[test] fn restorations_dirty_storage_and_different_storage() { - restoration(true, true); + restoration(true, true, false); } #[test] fn restorations_dirty_storage() { - restoration(false, true); + restoration(false, true, false); } #[test] fn restoration_different_storage() { - restoration(true, false); + restoration(true, false, false); +} + +#[test] +fn restoration_code_evicted() { + restoration(false, false, true); } #[test] fn restoration_success() { - restoration(false, false); + restoration(false, false, false); } -fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: bool) { +fn restoration( + test_different_storage: bool, + test_restore_to_with_dirty_storage: bool, + test_code_evicted: bool +) { let (set_rent_wasm, set_rent_code_hash) = compile_module::("set_rent").unwrap(); let (restoration_wasm, restoration_code_hash) = compile_module::("restoration").unwrap(); + let allowance: ::Balance = 10_000; ExtBuilder::default() .existential_deposit(50) .build() .execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), restoration_wasm)); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), set_rent_wasm)); - // If you ever need to update the wasm source this test will fail - // and will show you the actual hash. - assert_eq!(System::events(), vec![ + // Create an account with address `BOB` with code `CODE_SET_RENT`. + // The input parameter sets the rent allowance to 0. + assert_ok!(Contracts::instantiate_with_code( + Origin::signed(ALICE), + 30_000, + GAS_LIMIT, + set_rent_wasm.clone(), + allowance.encode(), + vec![], + )); + let addr_bob = Contracts::contract_address(&ALICE, &set_rent_code_hash, &[]); + + let mut events = vec![ + EventRecord { + phase: Phase::Initialization, + event: Event::frame_system(frame_system::Event::NewAccount(ALICE)), + topics: vec![], + }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::system(frame_system::Event::NewAccount(ALICE)), + event: Event::pallet_balances( + pallet_balances::Event::Endowed(ALICE, 1_000_000) + ), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(ALICE, 1_000_000)), + event: Event::frame_system(frame_system::Event::NewAccount(addr_bob.clone())), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::contracts(RawEvent::CodeStored(restoration_code_hash.into())), + event: Event::pallet_balances( + pallet_balances::Event::Endowed(addr_bob.clone(), 30_000) + ), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::contracts(RawEvent::CodeStored(set_rent_code_hash.into())), + event: Event::pallet_balances( + pallet_balances::Event::Transfer(ALICE, addr_bob.clone(), 30_000) + ), topics: vec![], }, - ]); + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_contracts( + crate::Event::CodeStored(set_rent_code_hash.into()) + ), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_contracts( + crate::Event::Instantiated(ALICE, addr_bob.clone()) + ), + topics: vec![], + }, + ]; - // Create an account with address `BOB` with code `CODE_SET_RENT`. - // The input parameter sets the rent allowance to 0. - assert_ok!(Contracts::instantiate( - Origin::signed(ALICE), - 30_000, - GAS_LIMIT, - set_rent_code_hash.into(), - ::Balance::from(1_000u32).encode(), - vec![], - )); - let addr_bob = Contracts::contract_address(&ALICE, &set_rent_code_hash, &[]); + // Create another contract from the same code in order to increment the codes + // refcounter so that it stays on chain. + if !test_code_evicted { + assert_ok!(Contracts::instantiate_with_code( + Origin::signed(ALICE), + 20_000, + GAS_LIMIT, + set_rent_wasm, + allowance.encode(), + vec![1], + )); + assert_refcount!(set_rent_code_hash, 2); + let addr_dummy = Contracts::contract_address(&ALICE, &set_rent_code_hash, &[1]); + events.extend([ + EventRecord { + phase: Phase::Initialization, + event: Event::frame_system(frame_system::Event::NewAccount(addr_dummy.clone())), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_balances( + pallet_balances::Event::Endowed(addr_dummy.clone(), 20_000) + ), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_balances( + pallet_balances::Event::Transfer(ALICE, addr_dummy.clone(), 20_000) + ), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_contracts( + crate::Event::Instantiated(ALICE, addr_dummy.clone()) + ), + topics: vec![], + }, + ].iter().cloned()); + } + + assert_eq!(System::events(), events); // Check if `BOB` was created successfully and that the rent allowance is below what // we specified as the first rent was already collected. let bob_contract = ContractInfoOf::::get(&addr_bob).unwrap().get_alive().unwrap(); - assert!(bob_contract.rent_allowance < 5_000); + assert!(bob_contract.rent_allowance < allowance); if test_different_storage { assert_ok!(Contracts::call( @@ -1296,26 +1319,22 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: assert!(ContractInfoOf::::get(&addr_bob).unwrap().get_alive().is_some()); assert_ok!(Contracts::claim_surcharge(Origin::none(), addr_bob.clone(), Some(ALICE))); assert!(ContractInfoOf::::get(&addr_bob).unwrap().get_tombstone().is_some()); - assert_eq!(System::events(), vec![ - EventRecord { - phase: Phase::Initialization, - event: MetaEvent::contracts( - RawEvent::Evicted(addr_bob.clone(), true) - ), - topics: vec![], - }, - ]); + if test_code_evicted { + assert_refcount!(set_rent_code_hash, 0); + } else { + assert_refcount!(set_rent_code_hash, 1); + } // Create another account with the address `DJANGO` with `CODE_RESTORATION`. // // Note that we can't use `ALICE` for creating `DJANGO` so we create yet another // account `CHARLIE` and create `DJANGO` with it. let _ = Balances::deposit_creating(&CHARLIE, 1_000_000); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(CHARLIE), 30_000, GAS_LIMIT, - restoration_code_hash.into(), + restoration_wasm, vec![], vec![], )); @@ -1357,7 +1376,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: key }; - if test_different_storage || test_restore_to_with_dirty_storage { + if test_different_storage || test_restore_to_with_dirty_storage || test_code_evicted { // Parametrization of the test imply restoration failure. Check that `DJANGO` aka // restoration contract is still in place and also that `BOB` doesn't exist. let result = perform_the_restoration(); @@ -1371,61 +1390,83 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: Storage::::read(&django_trie_id, &delta_key), Some(vec![40, 0, 0, 0]), ); - match (test_different_storage, test_restore_to_with_dirty_storage) { - (true, false) => { + match ( + test_different_storage, + test_restore_to_with_dirty_storage, + test_code_evicted + ) { + (true, false, false) => { assert_err_ignore_postinfo!( result, Error::::InvalidTombstone, ); assert_eq!(System::events(), vec![]); } - (_, true) => { + (_, true, false) => { assert_err_ignore_postinfo!( result, Error::::InvalidContractOrigin, ); - pretty_assertions::assert_eq!(System::events(), vec![ + assert_eq!(System::events(), vec![ EventRecord { phase: Phase::Initialization, - event: MetaEvent::contracts(RawEvent::Evicted(addr_bob, true)), + event: Event::pallet_contracts(crate::Event::Evicted(addr_bob)), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::system(frame_system::Event::NewAccount(CHARLIE)), + event: Event::frame_system(frame_system::Event::NewAccount(CHARLIE)), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(CHARLIE, 1_000_000)), + event: Event::pallet_balances(pallet_balances::Event::Endowed(CHARLIE, 1_000_000)), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::system(frame_system::Event::NewAccount(addr_django.clone())), + event: Event::frame_system(frame_system::Event::NewAccount(addr_django.clone())), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(addr_django.clone(), 30_000)), + event: Event::pallet_balances(pallet_balances::Event::Endowed(addr_django.clone(), 30_000)), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances( - pallet_balances::RawEvent::Transfer(CHARLIE, addr_django.clone(), 30_000) + event: Event::pallet_balances( + pallet_balances::Event::Transfer(CHARLIE, addr_django.clone(), 30_000) ), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::contracts(RawEvent::Instantiated(CHARLIE, addr_django.clone())), + event: Event::pallet_contracts( + crate::Event::CodeStored(restoration_code_hash) + ), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_contracts( + crate::Event::Instantiated(CHARLIE, addr_django.clone()) + ), topics: vec![], }, + ]); - } + }, + (false, false, true) => { + assert_err_ignore_postinfo!( + result, Error::::CodeNotFound, + ); + assert_refcount!(set_rent_code_hash, 0); + assert_eq!(System::events(), vec![]); + }, _ => unreachable!(), } } else { assert_ok!(perform_the_restoration()); + assert_refcount!(set_rent_code_hash, 2); // Here we expect that the restoration is succeeded. Check that the restoration // contract `DJANGO` ceased to exist and that `BOB` returned back. @@ -1440,13 +1481,20 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: assert_eq!(System::events(), vec![ EventRecord { phase: Phase::Initialization, - event: MetaEvent::system(system::Event::KilledAccount(addr_django.clone())), + event: Event::pallet_contracts(crate::Event::CodeRemoved(restoration_code_hash)), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: Event::frame_system(system::Event::KilledAccount(addr_django.clone())), topics: vec![], }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::contracts( - RawEvent::Restored(addr_django, addr_bob, bob_contract.code_hash, 50) + event: Event::pallet_contracts( + crate::Event::Restored( + addr_django, addr_bob, bob_contract.code_hash, 50 + ) ), topics: vec![], }, @@ -1465,12 +1513,11 @@ fn storage_max_value_limit() { .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 30_000, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], )); @@ -1511,24 +1558,28 @@ fn deploy_and_call_other_contract() { .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), callee_wasm)); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), caller_wasm)); - - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 100_000, GAS_LIMIT, - caller_code_hash.into(), + caller_wasm, vec![], vec![], )); - let addr = Contracts::contract_address(&ALICE, &caller_code_hash, &[]); + assert_ok!(Contracts::instantiate_with_code( + Origin::signed(ALICE), + 100_000, + GAS_LIMIT, + callee_wasm, + 0u32.to_le_bytes().encode(), + vec![42], + )); // Call BOB contract, which attempts to instantiate and call the callee contract and // makes various assertions on the results from those calls. assert_ok!(Contracts::call( Origin::signed(ALICE), - addr, + Contracts::contract_address(&ALICE, &caller_code_hash, &[]), 0, GAS_LIMIT, callee_code_hash.as_ref().to_vec(), @@ -1544,14 +1595,13 @@ fn cannot_self_destruct_through_draning() { .build() .execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); // Instantiate the BOB contract. - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 100_000, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], )); @@ -1585,14 +1635,13 @@ fn cannot_self_destruct_while_live() { .build() .execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); // Instantiate the BOB contract. - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 100_000, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], )); @@ -1633,14 +1682,14 @@ fn self_destruct_works() { .build() .execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); + let _ = Balances::deposit_creating(&DJANGO, 1_000_000); // Instantiate the BOB contract. - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 100_000, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], )); @@ -1652,6 +1701,9 @@ fn self_destruct_works() { Some(ContractInfo::Alive(_)) ); + // Drop all previous events + initialize_block(2); + // Call BOB without input data which triggers termination. assert_matches!( Contracts::call( @@ -1664,11 +1716,41 @@ fn self_destruct_works() { Ok(_) ); + pretty_assertions::assert_eq!(System::events(), vec![ + EventRecord { + phase: Phase::Initialization, + event: Event::frame_system( + frame_system::Event::KilledAccount(addr.clone()) + ), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_balances( + pallet_balances::Event::Transfer(addr.clone(), DJANGO, 93_654) + ), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_contracts(crate::Event::CodeRemoved(code_hash)), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_contracts( + crate::Event::Terminated(addr.clone(), DJANGO) + ), + topics: vec![], + }, + ]); + // Check that account is gone assert!(ContractInfoOf::::get(&addr).is_none()); // check that the beneficiary (django) got remaining balance - assert_eq!(Balances::free_balance(DJANGO), 100_000); + // some rent was deducted before termination + assert_eq!(Balances::free_balance(DJANGO), 1_093_654); }); } @@ -1685,16 +1767,22 @@ fn destroy_contract_and_transfer_funds() { .execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), callee_wasm)); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), caller_wasm)); + assert_ok!(Contracts::instantiate_with_code( + Origin::signed(ALICE), + 200_000, + GAS_LIMIT, + callee_wasm, + vec![], + vec![42] + )); // This deploys the BOB contract, which in turn deploys the CHARLIE contract during // construction. - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 200_000, GAS_LIMIT, - caller_code_hash.into(), + caller_wasm, callee_code_hash.as_ref().to_vec(), vec![], )); @@ -1725,25 +1813,24 @@ fn destroy_contract_and_transfer_funds() { #[test] fn cannot_self_destruct_in_constructor() { - let (wasm, code_hash) = compile_module::("self_destructing_constructor").unwrap(); + let (wasm, _) = compile_module::("self_destructing_constructor").unwrap(); ExtBuilder::default() .existential_deposit(50) .build() .execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); // Fail to instantiate the BOB because the contructor calls seal_terminate. assert_err_ignore_postinfo!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), 100_000, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], ), - Error::::NewContractNotFunded, + Error::::NotCallable, ); }); } @@ -1757,14 +1844,13 @@ fn crypto_hashes() { .build() .execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); // Instantiate the CRYPTO_HASHES contract. - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 100_000, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], )); @@ -1808,16 +1894,15 @@ fn crypto_hashes() { fn transfer_return_code() { let (wasm, code_hash) = compile_module::("transfer_return_code").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - code_hash.into(), + wasm, vec![], vec![], ), @@ -1856,18 +1941,16 @@ fn call_return_code() { let (caller_code, caller_hash) = compile_module::("call_return_code").unwrap(); let (callee_code, callee_hash) = compile_module::("ok_trap_revert").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); - let _ = Balances::deposit_creating(&CHARLIE, 10 * subsistence); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), caller_code)); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), callee_code)); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); + let _ = Balances::deposit_creating(&CHARLIE, 1000 * subsistence); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - caller_hash.into(), + caller_code, vec![0], vec![], ), @@ -1886,11 +1969,11 @@ fn call_return_code() { assert_return_code!(result, RuntimeReturnCode::NotCallable); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(CHARLIE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - callee_hash.into(), + callee_code, vec![0], vec![], ), @@ -1951,19 +2034,28 @@ fn instantiate_return_code() { let (caller_code, caller_hash) = compile_module::("instantiate_return_code").unwrap(); let (callee_code, callee_hash) = compile_module::("ok_trap_revert").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); - let _ = Balances::deposit_creating(&CHARLIE, 10 * subsistence); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), caller_code)); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), callee_code)); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); + let _ = Balances::deposit_creating(&CHARLIE, 1000 * subsistence); let callee_hash = callee_hash.as_ref().to_vec(); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( + Origin::signed(ALICE), + subsistence * 100, + GAS_LIMIT, + callee_code, + vec![], + vec![], + ), + ); + + assert_ok!( + Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - caller_hash.into(), + caller_code, vec![], vec![], ), @@ -1977,26 +2069,26 @@ fn instantiate_return_code() { addr.clone(), 0, GAS_LIMIT, - vec![0; 33], + callee_hash.clone(), ).exec_result.unwrap(); assert_return_code!(result, RuntimeReturnCode::BelowSubsistenceThreshold); // Contract has enough total balance in order to not go below the subsistence - // threshold when transfering 100 balance but this balance is reserved so + // threshold when transfering the balance but this balance is reserved so // the transfer still fails but with another return code. - Balances::make_free_balance_be(&addr, subsistence + 100); - Balances::reserve(&addr, subsistence + 100).unwrap(); + Balances::make_free_balance_be(&addr, subsistence + 10_000); + Balances::reserve(&addr, subsistence + 10_000).unwrap(); let result = Contracts::bare_call( ALICE, addr.clone(), 0, GAS_LIMIT, - vec![0; 33], + callee_hash.clone(), ).exec_result.unwrap(); assert_return_code!(result, RuntimeReturnCode::TransferFailed); // Contract has enough balance but the passed code hash is invalid - Balances::make_free_balance_be(&addr, subsistence + 1000); + Balances::make_free_balance_be(&addr, subsistence + 10_000); let result = Contracts::bare_call( ALICE, addr.clone(), @@ -2033,12 +2125,19 @@ fn instantiate_return_code() { fn disabled_chain_extension_wont_deploy() { let (code, _hash) = compile_module::("chain_extension").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); TestExtension::disable(); - assert_eq!( - Contracts::put_code(Origin::signed(ALICE), code), - Err("module uses chain extensions but chain extensions are disabled".into()), + assert_err_ignore_postinfo!( + Contracts::instantiate_with_code( + Origin::signed(ALICE), + 3 * subsistence, + GAS_LIMIT, + code, + vec![], + vec![], + ), + "module uses chain extensions but chain extensions are disabled", ); }); } @@ -2047,21 +2146,20 @@ fn disabled_chain_extension_wont_deploy() { fn disabled_chain_extension_errors_on_call() { let (code, hash) = compile_module::("chain_extension").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), code)); - TestExtension::disable(); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - hash.into(), + code, vec![], vec![], ), ); let addr = Contracts::contract_address(&ALICE, &hash, &[]); + TestExtension::disable(); assert_err_ignore_postinfo!( Contracts::call( Origin::signed(ALICE), @@ -2079,15 +2177,14 @@ fn disabled_chain_extension_errors_on_call() { fn chain_extension_works() { let (code, hash) = compile_module::("chain_extension").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), code)); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - hash.into(), + code, vec![], vec![], ), @@ -2149,16 +2246,15 @@ fn chain_extension_works() { fn lazy_removal_works() { let (code, hash) = compile_module::("self_destruct").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), code)); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - hash.into(), + code, vec![], vec![], ), @@ -2210,16 +2306,15 @@ fn lazy_removal_partial_remove_works() { let mut ext = ExtBuilder::default().existential_deposit(50).build(); let trie = ext.execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), code)); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - hash.into(), + code, vec![], vec![], ), @@ -2292,16 +2387,15 @@ fn lazy_removal_partial_remove_works() { fn lazy_removal_does_no_run_on_full_block() { let (code, hash) = compile_module::("self_destruct").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), code)); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - hash.into(), + code, vec![], vec![], ), @@ -2354,7 +2448,7 @@ fn lazy_removal_does_no_run_on_full_block() { // Run the lazy removal without any limit so that all keys would be removed if there // had been some weight left in the block. let weight_used = Contracts::on_initialize(Weight::max_value()); - let base = <::WeightInfo as crate::WeightInfo>::on_initialize(); + let base = <::WeightInfo as WeightInfo>::on_initialize(); assert_eq!(weight_used, base); // All the keys are still in place @@ -2377,16 +2471,15 @@ fn lazy_removal_does_no_run_on_full_block() { fn lazy_removal_does_not_use_all_weight() { let (code, hash) = compile_module::("self_destruct").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), code)); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - hash.into(), + code, vec![], vec![], ), @@ -2448,16 +2541,15 @@ fn lazy_removal_does_not_use_all_weight() { fn deletion_queue_full() { let (code, hash) = compile_module::("self_destruct").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { - let subsistence = ConfigCache::::subsistence_threshold_uncached(); - let _ = Balances::deposit_creating(&ALICE, 10 * subsistence); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), code)); + let subsistence = Module::::subsistence_threshold(); + let _ = Balances::deposit_creating(&ALICE, 1000 * subsistence); assert_ok!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), - subsistence * 3, + subsistence * 100, GAS_LIMIT, - hash.into(), + code, vec![], vec![], ), @@ -2501,7 +2593,7 @@ fn deletion_queue_full() { fn not_deployed_if_endowment_too_low_for_first_rent() { let (wasm, code_hash) = compile_module::("set_rent").unwrap(); - // The instantation deducted the rent for one block immediatly + // The instantiation deducted the rent for one block immediately let first_rent = ::RentFraction::get() // base_deposit + deploy_set_storage (4 bytes in 1 item) - free_balance .mul_ceil(80_000u32 + 40_000 + 10_000 - 30_000) @@ -2511,12 +2603,12 @@ fn not_deployed_if_endowment_too_low_for_first_rent() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); assert_storage_noop!(assert_err_ignore_postinfo!( - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(ALICE), 30_000, - GAS_LIMIT, code_hash.into(), + GAS_LIMIT, + wasm, (BalanceOf::::from(first_rent) - BalanceOf::::from(1u32)) .encode(), // rent allowance vec![], @@ -2533,12 +2625,12 @@ fn surcharge_reward_is_capped() { let (wasm, code_hash) = compile_module::("set_rent").unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm)); - assert_ok!(Contracts::instantiate( + assert_ok!(Contracts::instantiate_with_code( Origin::signed(ALICE), 30_000, - GAS_LIMIT, code_hash.into(), - >::from(1_000u32).encode(), // rent allowance + GAS_LIMIT, + wasm, + >::from(10_000u32).encode(), // rent allowance vec![], )); let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); @@ -2546,7 +2638,7 @@ fn surcharge_reward_is_capped() { let balance = Balances::free_balance(&ALICE); let reward = ::SurchargeReward::get(); - // some rent should have payed due to instantation + // some rent should have payed due to instantiation assert_ne!(contract.rent_payed, 0); // the reward should be parameterized sufficiently high to make this test useful @@ -2569,3 +2661,141 @@ fn surcharge_reward_is_capped() { assert!(Balances::free_balance(&ALICE) < balance + reward); }); } + +#[test] +fn refcounter() { + let (wasm, code_hash) = compile_module::("self_destruct").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = Balances::deposit_creating(&ALICE, 1_000_000); + let subsistence = Module::::subsistence_threshold(); + + // Create two contracts with the same code and check that they do in fact share it. + assert_ok!(Contracts::instantiate_with_code( + Origin::signed(ALICE), + subsistence * 100, + GAS_LIMIT, + wasm.clone(), + vec![], + vec![0], + )); + assert_ok!(Contracts::instantiate_with_code( + Origin::signed(ALICE), + subsistence * 100, + GAS_LIMIT, + wasm.clone(), + vec![], + vec![1], + )); + assert_refcount!(code_hash, 2); + + // Sharing should also work with the usual instantiate call + assert_ok!(Contracts::instantiate( + Origin::signed(ALICE), + subsistence * 100, + GAS_LIMIT, + code_hash, + vec![], + vec![2], + )); + assert_refcount!(code_hash, 3); + + // addresses of all three existing contracts + let addr0 = Contracts::contract_address(&ALICE, &code_hash, &[0]); + let addr1 = Contracts::contract_address(&ALICE, &code_hash, &[1]); + let addr2 = Contracts::contract_address(&ALICE, &code_hash, &[2]); + + // Terminating one contract should decrement the refcount + assert_ok!(Contracts::call( + Origin::signed(ALICE), + addr0, + 0, + GAS_LIMIT, + vec![], + )); + assert_refcount!(code_hash, 2); + + // make remaining contracts eligible for eviction + initialize_block(40); + + // remove one of them + assert_ok!(Contracts::claim_surcharge(Origin::none(), addr1, Some(ALICE))); + assert_refcount!(code_hash, 1); + + // Pristine code should still be there + crate::PristineCode::::get(code_hash).unwrap(); + + // remove the last contract + assert_ok!(Contracts::claim_surcharge(Origin::none(), addr2, Some(ALICE))); + assert_refcount!(code_hash, 0); + + // all code should be gone + assert_matches!(crate::PristineCode::::get(code_hash), None); + assert_matches!(crate::CodeStorage::::get(code_hash), None); + }); +} + + +#[test] +fn reinstrument_does_charge() { + let (wasm, code_hash) = compile_module::("return_with_data").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = Balances::deposit_creating(&ALICE, 1_000_000); + let subsistence = Module::::subsistence_threshold(); + let zero = 0u32.to_le_bytes().encode(); + let code_len = wasm.len() as u32; + + assert_ok!(Contracts::instantiate_with_code( + Origin::signed(ALICE), + subsistence * 100, + GAS_LIMIT, + wasm, + zero.clone(), + vec![], + )); + + let addr = Contracts::contract_address(&ALICE, &code_hash, &[]); + + // Call the contract two times without reinstrument + + let result0 = Contracts::bare_call( + ALICE, + addr.clone(), + 0, + GAS_LIMIT, + zero.clone(), + ); + assert!(result0.exec_result.unwrap().is_success()); + + let result1 = Contracts::bare_call( + ALICE, + addr.clone(), + 0, + GAS_LIMIT, + zero.clone(), + ); + assert!(result1.exec_result.unwrap().is_success()); + + // They should match because both where called with the same schedule. + assert_eq!(result0.gas_consumed, result1.gas_consumed); + + // Update the schedule version but keep the rest the same + crate::CurrentSchedule::mutate(|old: &mut Schedule| { + old.version += 1; + }); + + // This call should trigger reinstrumentation + let result2 = Contracts::bare_call( + ALICE, + addr.clone(), + 0, + GAS_LIMIT, + zero.clone(), + ); + assert!(result2.exec_result.unwrap().is_success()); + assert!(result2.gas_consumed > result1.gas_consumed); + assert_eq!( + result2.gas_consumed, + result1.gas_consumed + ::WeightInfo::instrument(code_len / 1024), + ); + }); +} diff --git a/frame/contracts/src/wasm/code_cache.rs b/frame/contracts/src/wasm/code_cache.rs index 3150ee4b7bde7..0b2512f17f594 100644 --- a/frame/contracts/src/wasm/code_cache.rs +++ b/frame/contracts/src/wasm/code_cache.rs @@ -27,46 +27,92 @@ //! this guarantees that every instrumented contract code in cache cannot have the version equal to the current one. //! Thus, before executing a contract it should be reinstrument with new schedule. -use crate::wasm::{prepare, runtime::Env, PrefabWasmModule}; -use crate::{CodeHash, CodeStorage, PristineCode, Schedule, Config}; -use sp_std::prelude::*; -use sp_runtime::traits::Hash; +use crate::{ + CodeHash, CodeStorage, PristineCode, Schedule, Config, Error, Weight, + wasm::{prepare, PrefabWasmModule}, Module as Contracts, Event, + gas::{GasMeter, Token}, + weights::WeightInfo, +}; use sp_core::crypto::UncheckedFrom; -use frame_support::StorageMap; +use frame_support::dispatch::DispatchError; +#[cfg(feature = "runtime-benchmarks")] +pub use self::private::reinstrument as reinstrument; -/// Put code in the storage. The hash of code is used as a key and is returned -/// as a result of this function. +/// Put the instrumented module in storage. /// -/// This function instruments the given code and caches it in the storage. -pub fn save( - original_code: Vec, - schedule: &Schedule, -) -> Result, &'static str> where T::AccountId: UncheckedFrom + AsRef<[u8]> { - let prefab_module = prepare::prepare_contract::(&original_code, schedule)?; - let code_hash = T::Hashing::hash(&original_code); - - >::insert(code_hash, prefab_module); - >::insert(code_hash, original_code); - - Ok(code_hash) +/// Increments the refcount of the in-storage `prefab_module` if it already exists in storage +/// under the specified `code_hash`. +pub fn store(mut prefab_module: PrefabWasmModule) +where + T::AccountId: UncheckedFrom + AsRef<[u8]> +{ + let code_hash = sp_std::mem::take(&mut prefab_module.code_hash); + + // original_code is only `Some` if the contract was instantiated from a new code + // but `None` if it was loaded from storage. + if let Some(code) = prefab_module.original_code.take() { + >::insert(&code_hash, code); + } + >::mutate(&code_hash, |existing| { + match existing { + Some(module) => increment_64(&mut module.refcount), + None => { + *existing = Some(prefab_module); + Contracts::::deposit_event(Event::CodeStored(code_hash)) + } + } + }); } -/// Version of `save` to be used in runtime benchmarks. -// -/// This version neither checks nor instruments the passed in code. This is useful -/// when code needs to be benchmarked without the injected instrumentation. -#[cfg(feature = "runtime-benchmarks")] -pub fn save_raw( - original_code: Vec, - schedule: &Schedule, -) -> Result, &'static str> where T::AccountId: UncheckedFrom + AsRef<[u8]> { - let prefab_module = prepare::benchmarking::prepare_contract::(&original_code, schedule)?; - let code_hash = T::Hashing::hash(&original_code); +/// Decrement the refcount and store. +/// +/// Removes the code instead of storing it when the refcount drops to zero. +pub fn store_decremented(mut prefab_module: PrefabWasmModule) +where + T::AccountId: UncheckedFrom + AsRef<[u8]> +{ + prefab_module.refcount = prefab_module.refcount.saturating_sub(1); + if prefab_module.refcount > 0 { + >::insert(prefab_module.code_hash, prefab_module); + } else { + >::remove(prefab_module.code_hash); + finish_removal::(prefab_module.code_hash); + } +} - >::insert(code_hash, prefab_module); - >::insert(code_hash, original_code); +/// Increment the refcount of a code in-storage by one. +pub fn increment_refcount(code_hash: CodeHash) -> Result +where + T::AccountId: UncheckedFrom + AsRef<[u8]> +{ + >::mutate(code_hash, |existing| { + if let Some(module) = existing { + increment_64(&mut module.refcount); + Ok(module.original_code_len) + } else { + Err(Error::::CodeNotFound.into()) + } + }) +} - Ok(code_hash) +/// Decrement the refcount of a code in-storage by one and remove the code when it drops to zero. +pub fn decrement_refcount(code_hash: CodeHash) -> u32 +where + T::AccountId: UncheckedFrom + AsRef<[u8]> +{ + >::mutate_exists(code_hash, |existing| { + if let Some(module) = existing { + let code_len = module.original_code_len; + module.refcount = module.refcount.saturating_sub(1); + if module.refcount == 0 { + *existing = None; + finish_removal::(code_hash); + } + code_len + } else { + 0 + } + }) } /// Load code with the given code hash. @@ -75,21 +121,82 @@ pub fn save_raw( /// the current one given as an argument, then this function will perform /// re-instrumentation and update the cache in the storage. pub fn load( - code_hash: &CodeHash, - schedule: &Schedule, -) -> Result where T::AccountId: UncheckedFrom + AsRef<[u8]> { - let mut prefab_module = - >::get(code_hash).ok_or_else(|| "code is not found")?; - - if prefab_module.schedule_version < schedule.version { - // The current schedule version is greater than the version of the one cached - // in the storage. - // - // We need to re-instrument the code with the latest schedule here. - let original_code = - >::get(code_hash).ok_or_else(|| "pristine code is not found")?; - prefab_module = prepare::prepare_contract::(&original_code, schedule)?; - >::insert(&code_hash, &prefab_module); + code_hash: CodeHash, + reinstrument: Option<(&Schedule, &mut GasMeter)>, +) -> Result, DispatchError> +where + T::AccountId: UncheckedFrom + AsRef<[u8]> +{ + let mut prefab_module = >::get(code_hash) + .ok_or_else(|| Error::::CodeNotFound)?; + prefab_module.code_hash = code_hash; + + if let Some((schedule, gas_meter)) = reinstrument { + if prefab_module.schedule_version < schedule.version { + // The current schedule version is greater than the version of the one cached + // in the storage. + // + // We need to re-instrument the code with the latest schedule here. + gas_meter.charge(&(), InstrumentToken(prefab_module.original_code_len))?; + private::reinstrument(&mut prefab_module, schedule)?; + } } Ok(prefab_module) } + +mod private { + use super::*; + + /// Instruments the passed prefab wasm module with the supplied schedule. + pub fn reinstrument( + prefab_module: &mut PrefabWasmModule, + schedule: &Schedule, + ) -> Result<(), DispatchError> + where + T::AccountId: UncheckedFrom + AsRef<[u8]> + { + let original_code = >::get(&prefab_module.code_hash) + .ok_or_else(|| Error::::CodeNotFound)?; + prefab_module.code = prepare::reinstrument_contract::(original_code, schedule)?; + prefab_module.schedule_version = schedule.version; + >::insert(&prefab_module.code_hash, &*prefab_module); + Ok(()) + } +} + +/// Finish removal of a code by deleting the pristine code and emitting an event. +fn finish_removal(code_hash: CodeHash) +where + T::AccountId: UncheckedFrom + AsRef<[u8]> +{ + >::remove(code_hash); + Contracts::::deposit_event(Event::CodeRemoved(code_hash)) +} + +/// Increment the refcount panicking if it should ever overflow (which will not happen). +/// +/// We try hard to be infallible here because otherwise more storage transactions would be +/// necessary to account for failures in storing code for an already instantiated contract. +fn increment_64(refcount: &mut u64) { + *refcount = refcount.checked_add(1).expect(" + refcount is 64bit. Generating this overflow would require to store + _at least_ 18 exabyte of data assuming that a contract consumes only + one byte of data. Any node would run out of storage space before hitting + this overflow. + qed + "); +} + +/// Token to be supplied to the gas meter which charges the weight needed for reinstrumenting +/// a contract of the specified size in bytes. +#[cfg_attr(test, derive(Debug, PartialEq, Eq))] +#[derive(Clone, Copy)] +struct InstrumentToken(u32); + +impl Token for InstrumentToken { + type Metadata = (); + + fn calculate_amount(&self, _metadata: &Self::Metadata) -> Weight { + T::WeightInfo::instrument(self.0 / 1024) + } +} diff --git a/frame/contracts/src/wasm/env_def/macros.rs b/frame/contracts/src/wasm/env_def/macros.rs index dbb6705e9722d..3c10d3225e430 100644 --- a/frame/contracts/src/wasm/env_def/macros.rs +++ b/frame/contracts/src/wasm/env_def/macros.rs @@ -20,13 +20,11 @@ //! //! Most likely you should use `define_env` macro. -#[macro_export] macro_rules! convert_args { () => (vec![]); ( $( $t:ty ),* ) => ( vec![ $( { use $crate::wasm::env_def::ConvertibleToWasm; <$t>::VALUE_TYPE }, )* ] ); } -#[macro_export] macro_rules! gen_signature { ( ( $( $params: ty ),* ) ) => ( { @@ -43,7 +41,6 @@ macro_rules! gen_signature { ); } -#[macro_export] macro_rules! gen_signature_dispatch { ( $needle_name:ident, @@ -102,7 +99,6 @@ where f } -#[macro_export] macro_rules! unmarshall_then_body_then_marshall { ( $args_iter:ident, $ctx:ident, ( $( $names:ident : $params:ty ),* ) -> $returns:ty => $body:tt ) => ({ let body = $crate::wasm::env_def::macros::constrain_closure::< @@ -128,7 +124,6 @@ macro_rules! unmarshall_then_body_then_marshall { }) } -#[macro_export] macro_rules! define_func { ( < E: $seal_ty:tt > $name:ident ( $ctx: ident $(, $names:ident : $params:ty)*) $(-> $returns:ty)* => $body:tt ) => { fn $name< E: $seal_ty >( @@ -152,7 +147,6 @@ macro_rules! define_func { }; } -#[macro_export] macro_rules! register_func { ( $reg_cb:ident, < E: $seal_ty:tt > ; ) => {}; @@ -215,9 +209,9 @@ mod tests { use sp_runtime::traits::Zero; use sp_sandbox::{ReturnValue, Value}; use crate::{ + Weight, wasm::{Runtime, runtime::TrapReason, tests::MockExt}, exec::Ext, - gas::Gas, }; struct TestRuntime { @@ -282,7 +276,7 @@ mod tests { #[test] fn macro_define_func() { define_func!( seal_gas (_ctx, amount: u32) => { - let amount = Gas::from(amount); + let amount = Weight::from(amount); if !amount.is_zero() { Ok(()) } else { @@ -334,7 +328,7 @@ mod tests { define_env!(Env, , seal_gas( _ctx, amount: u32 ) => { - let amount = Gas::from(amount); + let amount = Weight::from(amount); if !amount.is_zero() { Ok(()) } else { diff --git a/frame/contracts/src/wasm/env_def/mod.rs b/frame/contracts/src/wasm/env_def/mod.rs index 0d9ceeee02373..997ec29e028d5 100644 --- a/frame/contracts/src/wasm/env_def/mod.rs +++ b/frame/contracts/src/wasm/env_def/mod.rs @@ -22,7 +22,7 @@ use sp_sandbox::Value; use parity_wasm::elements::{FunctionType, ValueType}; #[macro_use] -pub(crate) mod macros; +pub mod macros; pub trait ConvertibleToWasm: Sized { const VALUE_TYPE: ValueType; @@ -67,13 +67,13 @@ impl ConvertibleToWasm for u64 { } } -pub(crate) type HostFunc = +pub type HostFunc = fn( &mut Runtime, &[sp_sandbox::Value] ) -> Result; -pub(crate) trait FunctionImplProvider { +pub trait FunctionImplProvider { fn impls)>(f: &mut F); } diff --git a/frame/contracts/src/wasm/mod.rs b/frame/contracts/src/wasm/mod.rs index 45c927dfaa4b8..9001e2b8e92d7 100644 --- a/frame/contracts/src/wasm/mod.rs +++ b/frame/contracts/src/wasm/mod.rs @@ -18,114 +18,168 @@ //! This module provides a means for executing contracts //! represented in wasm. +#[macro_use] +mod env_def; +mod code_cache; +mod prepare; +mod runtime; + use crate::{ CodeHash, Schedule, Config, wasm::env_def::FunctionImplProvider, - exec::Ext, + exec::{Ext, Executable, ExportedFunction}, gas::GasMeter, }; use sp_std::prelude::*; use sp_core::crypto::UncheckedFrom; use codec::{Encode, Decode}; - -#[macro_use] -mod env_def; -mod code_cache; -mod prepare; -mod runtime; - -use self::code_cache::load as load_code; +use frame_support::dispatch::DispatchError; use pallet_contracts_primitives::ExecResult; - -pub use self::code_cache::save as save_code; -#[cfg(feature = "runtime-benchmarks")] -pub use self::code_cache::save_raw as save_code_raw; pub use self::runtime::{ReturnCode, Runtime, RuntimeToken}; +#[cfg(feature = "runtime-benchmarks")] +pub use self::code_cache::reinstrument; +#[cfg(test)] +pub use tests::MockExt; /// A prepared wasm module ready for execution. +/// +/// # Note +/// +/// This data structure is mostly immutable once created and stored. The exceptions that +/// can be changed by calling a contract are `refcount`, `schedule_version` and `code`. +/// `refcount` can change when a contract instantiates a new contract or self terminates. +/// `schedule_version` and `code` when a contract with an outdated instrumention is called. +/// Therefore one must be careful when holding any in-memory representation of this type while +/// calling into a contract as those fields can get out of date. #[derive(Clone, Encode, Decode)] -pub struct PrefabWasmModule { +pub struct PrefabWasmModule { /// Version of the schedule with which the code was instrumented. #[codec(compact)] schedule_version: u32, + /// Initial memory size of a contract's sandbox. #[codec(compact)] initial: u32, + /// The maximum memory size of a contract's sandbox. #[codec(compact)] maximum: u32, + /// The number of alive contracts that use this as their contract code. + /// + /// If this number drops to zero this module is removed from storage. + #[codec(compact)] + refcount: u64, /// This field is reserved for future evolution of format. /// - /// Basically, for now this field will be serialized as `None`. In the future - /// we would be able to extend this structure with. + /// For now this field is serialized as `None`. In the future we are able to change the + /// type parameter to a new struct that contains the fields that we want to add. + /// That new struct would also contain a reserved field for its future extensions. + /// This works because in SCALE `None` is encoded independently from the type parameter + /// of the option. _reserved: Option<()>, /// Code instrumented with the latest schedule. code: Vec, + /// The size of the uninstrumented code. + /// + /// We cache this value here in order to avoid the need to pull the pristine code + /// from storage when we only need its length for rent calculations. + original_code_len: u32, + /// The uninstrumented, pristine version of the code. + /// + /// It is not stored because the pristine code has its own storage item. The value + /// is only `Some` when this module was created from an `original_code` and `None` if + /// it was loaded from storage. + #[codec(skip)] + original_code: Option>, + /// The code hash of the stored code which is defined as the hash over the `original_code`. + /// + /// As the map key there is no need to store the hash in the value, too. It is set manually + /// when loading the module from storage. + #[codec(skip)] + code_hash: CodeHash, } -/// Wasm executable loaded by `WasmLoader` and executed by `WasmVm`. -pub struct WasmExecutable { - entrypoint_name: &'static str, - prefab_module: PrefabWasmModule, -} - -/// Loader which fetches `WasmExecutable` from the code cache. -pub struct WasmLoader<'a, T: Config> { - schedule: &'a Schedule, -} - -impl<'a, T: Config> WasmLoader<'a, T> where T::AccountId: UncheckedFrom + AsRef<[u8]> { - pub fn new(schedule: &'a Schedule) -> Self { - WasmLoader { schedule } +impl ExportedFunction { + /// The wasm export name for the function. + fn identifier(&self) -> &str { + match self { + Self::Constructor => "deploy", + Self::Call => "call", + } } } -impl<'a, T: Config> crate::exec::Loader for WasmLoader<'a, T> +impl PrefabWasmModule where T::AccountId: UncheckedFrom + AsRef<[u8]> { - type Executable = WasmExecutable; - - fn load_init(&self, code_hash: &CodeHash) -> Result { - let prefab_module = load_code::(code_hash, self.schedule)?; - Ok(WasmExecutable { - entrypoint_name: "deploy", - prefab_module, - }) - } - fn load_main(&self, code_hash: &CodeHash) -> Result { - let prefab_module = load_code::(code_hash, self.schedule)?; - Ok(WasmExecutable { - entrypoint_name: "call", - prefab_module, - }) + /// Create the module by checking and instrumenting `original_code`. + pub fn from_code( + original_code: Vec, + schedule: &Schedule + ) -> Result { + prepare::prepare_contract(original_code, schedule).map_err(Into::into) } -} -/// Implementation of `Vm` that takes `WasmExecutable` and executes it. -pub struct WasmVm<'a, T: Config> where T::AccountId: UncheckedFrom + AsRef<[u8]> { - schedule: &'a Schedule, -} + /// Create and store the module without checking nor instrumenting the passed code. + /// + /// # Note + /// + /// This is useful for benchmarking where we don't want instrumentation to skew + /// our results. + #[cfg(feature = "runtime-benchmarks")] + pub fn store_code_unchecked( + original_code: Vec, + schedule: &Schedule + ) -> Result<(), DispatchError> { + let executable = prepare::benchmarking::prepare_contract(original_code, schedule) + .map_err::(Into::into)?; + code_cache::store(executable); + Ok(()) + } -impl<'a, T: Config> WasmVm<'a, T> where T::AccountId: UncheckedFrom + AsRef<[u8]> { - pub fn new(schedule: &'a Schedule) -> Self { - WasmVm { schedule } + /// Return the refcount of the module. + #[cfg(test)] + pub fn refcount(&self) -> u64 { + self.refcount } } -impl<'a, T: Config> crate::exec::Vm for WasmVm<'a, T> +impl Executable for PrefabWasmModule where T::AccountId: UncheckedFrom + AsRef<[u8]> { - type Executable = WasmExecutable; + fn from_storage( + code_hash: CodeHash, + schedule: &Schedule, + gas_meter: &mut GasMeter, + ) -> Result { + code_cache::load(code_hash, Some((schedule, gas_meter))) + } + + fn from_storage_noinstr(code_hash: CodeHash) -> Result { + code_cache::load(code_hash, None) + } + + fn drop_from_storage(self) { + code_cache::store_decremented(self); + } + + fn add_user(code_hash: CodeHash) -> Result { + code_cache::increment_refcount::(code_hash) + } + + fn remove_user(code_hash: CodeHash) -> u32 { + code_cache::decrement_refcount::(code_hash) + } fn execute>( - &self, - exec: &WasmExecutable, + self, mut ext: E, + function: &ExportedFunction, input_data: Vec, gas_meter: &mut GasMeter, ) -> ExecResult { let memory = - sp_sandbox::Memory::new(exec.prefab_module.initial, Some(exec.prefab_module.maximum)) + sp_sandbox::Memory::new(self.initial, Some(self.maximum)) .unwrap_or_else(|_| { // unlike `.expect`, explicit panic preserves the source location. // Needed as we can't use `RUST_BACKTRACE` in here. @@ -145,17 +199,38 @@ where let mut runtime = Runtime::new( &mut ext, input_data, - &self.schedule, memory, gas_meter, ); + // We store before executing so that the code hash is available in the constructor. + let code = self.code.clone(); + if let &ExportedFunction::Constructor = function { + code_cache::store(self) + } + // Instantiate the instance from the instrumented module code and invoke the contract // entrypoint. - let result = sp_sandbox::Instance::new(&exec.prefab_module.code, &imports, &mut runtime) - .and_then(|mut instance| instance.invoke(exec.entrypoint_name, &[], &mut runtime)); + let result = sp_sandbox::Instance::new(&code, &imports, &mut runtime) + .and_then(|mut instance| instance.invoke(function.identifier(), &[], &mut runtime)); + runtime.to_execution_result(result) } + + fn code_hash(&self) -> &CodeHash { + &self.code_hash + } + + fn occupied_storage(&self) -> u32 { + // We disregard the size of the struct itself as the size is completely + // dominated by the code size. + let len = self.original_code_len.saturating_add(self.code.len() as u32); + len.checked_div(self.refcount as u32).unwrap_or(len) + } + + fn code_len(&self) -> u32 { + self.code.len() as u32 + } } #[cfg(test)] @@ -163,10 +238,9 @@ mod tests { use super::*; use crate::{ CodeHash, BalanceOf, Error, Module as Contracts, - exec::{Ext, StorageKey, AccountIdOf}, - gas::{Gas, GasMeter}, + exec::{Ext, StorageKey, AccountIdOf, Executable}, + gas::GasMeter, tests::{Test, Call, ALICE, BOB}, - wasm::prepare::prepare_contract, }; use std::collections::HashMap; use sp_core::H256; @@ -176,7 +250,7 @@ mod tests { use assert_matches::assert_matches; use pallet_contracts_primitives::{ExecReturnValue, ReturnFlags, ExecError, ErrorOrigin}; - const GAS_LIMIT: Gas = 10_000_000_000; + const GAS_LIMIT: Weight = 10_000_000_000; #[derive(Debug, PartialEq, Eq)] struct DispatchEntry(Call); @@ -220,6 +294,7 @@ mod tests { restores: Vec, // (topics, data) events: Vec<(Vec, Vec)>, + schedule: Schedule, } impl Ext for MockExt { @@ -234,12 +309,12 @@ mod tests { } fn instantiate( &mut self, - code_hash: &CodeHash, + code_hash: CodeHash, endowment: u64, gas_meter: &mut GasMeter, data: Vec, salt: &[u8], - ) -> Result<(AccountIdOf, ExecReturnValue), ExecError> { + ) -> Result<(AccountIdOf, ExecReturnValue, u32), (ExecError, u32)> { self.instantiates.push(InstantiateEntry { code_hash: code_hash.clone(), endowment, @@ -248,11 +323,12 @@ mod tests { salt: salt.to_vec(), }); Ok(( - Contracts::::contract_address(&ALICE, code_hash, salt), + Contracts::::contract_address(&ALICE, &code_hash, salt), ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new(), }, + 0, )) } fn transfer( @@ -273,7 +349,7 @@ mod tests { value: u64, _gas_meter: &mut GasMeter, data: Vec, - ) -> ExecResult { + ) -> Result<(ExecReturnValue, u32), (ExecError, u32)> { self.transfers.push(TransferEntry { to: to.clone(), value, @@ -281,16 +357,16 @@ mod tests { }); // Assume for now that it was just a plain transfer. // TODO: Add tests for different call outcomes. - Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }) + Ok((ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }, 0)) } fn terminate( &mut self, beneficiary: &AccountIdOf, - ) -> Result<(), DispatchError> { + ) -> Result { self.terminations.push(TerminationEntry { beneficiary: beneficiary.clone(), }); - Ok(()) + Ok(0) } fn restore_to( &mut self, @@ -298,14 +374,14 @@ mod tests { code_hash: H256, rent_allowance: u64, delta: Vec, - ) -> Result<(), DispatchError> { + ) -> Result<(u32, u32), (DispatchError, u32, u32)> { self.restores.push(RestoreEntry { dest, code_hash, rent_allowance, delta, }); - Ok(()) + Ok((0, 0)) } fn caller(&self) -> &AccountIdOf { &ALICE @@ -355,6 +431,10 @@ mod tests { fn get_weight_price(&self, weight: Weight) -> BalanceOf { BalanceOf::::from(1312_u32).saturating_mul(weight.into()) } + + fn schedule(&self) -> &Schedule { + &self.schedule + } } impl Ext for &mut MockExt { @@ -368,12 +448,12 @@ mod tests { } fn instantiate( &mut self, - code: &CodeHash, + code: CodeHash, value: u64, gas_meter: &mut GasMeter, input_data: Vec, salt: &[u8], - ) -> Result<(AccountIdOf, ExecReturnValue), ExecError> { + ) -> Result<(AccountIdOf, ExecReturnValue, u32), (ExecError, u32)> { (**self).instantiate(code, value, gas_meter, input_data, salt) } fn transfer( @@ -386,7 +466,7 @@ mod tests { fn terminate( &mut self, beneficiary: &AccountIdOf, - ) -> Result<(), DispatchError> { + ) -> Result { (**self).terminate(beneficiary) } fn call( @@ -395,7 +475,7 @@ mod tests { value: u64, gas_meter: &mut GasMeter, input_data: Vec, - ) -> ExecResult { + ) -> Result<(ExecReturnValue, u32), (ExecError, u32)> { (**self).call(to, value, gas_meter, input_data) } fn restore_to( @@ -404,7 +484,7 @@ mod tests { code_hash: H256, rent_allowance: u64, delta: Vec, - ) -> Result<(), DispatchError> { + ) -> Result<(u32, u32), (DispatchError, u32, u32)> { (**self).restore_to( dest, code_hash, @@ -454,6 +534,9 @@ mod tests { fn get_weight_price(&self, weight: Weight) -> BalanceOf { (**self).get_weight_price(weight) } + fn schedule(&self) -> &Schedule { + (**self).schedule() + } } fn execute( @@ -466,23 +549,10 @@ mod tests { ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]> { - use crate::exec::Vm; - let wasm = wat::parse_str(wat).unwrap(); let schedule = crate::Schedule::default(); - let prefab_module = - prepare_contract::(&wasm, &schedule).unwrap(); - - let exec = WasmExecutable { - // Use a "call" convention. - entrypoint_name: "call", - prefab_module, - }; - - let cfg = Default::default(); - let vm = WasmVm::new(&cfg); - - vm.execute(&exec, ext, input_data, gas_meter) + let executable = PrefabWasmModule::::from_code(wasm, &schedule).unwrap(); + executable.execute(ext, &ExportedFunction::Call, input_data, gas_meter) } const CODE_TRANSFER: &str = r#" @@ -1134,7 +1204,7 @@ mod tests { &mut gas_meter, ).unwrap(); - let gas_left = Gas::decode(&mut output.data.as_slice()).unwrap(); + let gas_left = Weight::decode(&mut output.data.as_slice()).unwrap(); assert!(gas_left < GAS_LIMIT, "gas_left must be less than initial"); assert!(gas_left > gas_meter.gas_left(), "gas_left must be greater than final"); } diff --git a/frame/contracts/src/wasm/prepare.rs b/frame/contracts/src/wasm/prepare.rs index e03eb3d39bc11..caf6ef88c1ba0 100644 --- a/frame/contracts/src/wasm/prepare.rs +++ b/frame/contracts/src/wasm/prepare.rs @@ -25,7 +25,7 @@ use crate::{ wasm::{PrefabWasmModule, env_def::ImportSatisfyCheck}, }; use parity_wasm::elements::{self, Internal, External, MemoryType, Type, ValueType}; -use pwasm_utils; +use sp_runtime::traits::Hash; use sp_std::prelude::*; /// Currently, all imported functions must be located inside this module. We might support @@ -407,22 +407,11 @@ fn get_memory_limits(module: Option<&MemoryType>, schedule: &Schedule } } -/// Loads the given module given in `original_code`, performs some checks on it and -/// does some preprocessing. -/// -/// The checks are: -/// -/// - provided code is a valid wasm module. -/// - the module doesn't define an internal memory instance, -/// - imported memory (if any) doesn't reserve more memory than permitted by the `schedule`, -/// - all imported functions from the external environment matches defined by `env` module, -/// -/// The preprocessing includes injecting code for gas metering and metering the height of stack. -pub fn prepare_contract( +fn check_and_instrument( original_code: &[u8], schedule: &Schedule, -) -> Result { - let mut contract_module = ContractModule::new(original_code, schedule)?; +) -> Result<(Vec, (u32, u32)), &'static str> { + let contract_module = ContractModule::new(&original_code, schedule)?; contract_module.scan_exports()?; contract_module.ensure_no_internal_memory()?; contract_module.ensure_table_size_limit(schedule.limits.table_size)?; @@ -438,19 +427,65 @@ pub fn prepare_contract( schedule )?; - contract_module = contract_module + let code = contract_module .inject_gas_metering()? - .inject_stack_height_metering()?; + .inject_stack_height_metering()? + .into_wasm_code()?; + Ok((code, memory_limits)) +} + +fn do_preparation( + original_code: Vec, + schedule: &Schedule, +) -> Result, &'static str> { + let (code, (initial, maximum)) = check_and_instrument::( + original_code.as_ref(), + schedule, + )?; Ok(PrefabWasmModule { schedule_version: schedule.version, - initial: memory_limits.0, - maximum: memory_limits.1, + initial, + maximum, _reserved: None, - code: contract_module.into_wasm_code()?, + code, + original_code_len: original_code.len() as u32, + refcount: 1, + code_hash: T::Hashing::hash(&original_code), + original_code: Some(original_code), }) } +/// Loads the given module given in `original_code`, performs some checks on it and +/// does some preprocessing. +/// +/// The checks are: +/// +/// - provided code is a valid wasm module. +/// - the module doesn't define an internal memory instance, +/// - imported memory (if any) doesn't reserve more memory than permitted by the `schedule`, +/// - all imported functions from the external environment matches defined by `env` module, +/// +/// The preprocessing includes injecting code for gas metering and metering the height of stack. +pub fn prepare_contract( + original_code: Vec, + schedule: &Schedule, +) -> Result, &'static str> { + do_preparation::(original_code, schedule) +} + +/// The same as [`prepare_contract`] but without constructing a new [`PrefabWasmModule`] +/// +/// # Note +/// +/// Use this when an existing contract should be re-instrumented with a newer schedule version. +pub fn reinstrument_contract( + original_code: Vec, + schedule: &Schedule, +) -> Result, &'static str> { + Ok(check_and_instrument::(&original_code, schedule)?.0) +} + /// Alternate (possibly unsafe) preparation functions used only for benchmarking. /// /// For benchmarking we need to construct special contracts that might not pass our @@ -459,9 +494,7 @@ pub fn prepare_contract( /// in production code. #[cfg(feature = "runtime-benchmarks")] pub mod benchmarking { - use super::{ - Config, ContractModule, PrefabWasmModule, ImportSatisfyCheck, Schedule, get_memory_limits - }; + use super::*; use parity_wasm::elements::FunctionType; impl ImportSatisfyCheck for () { @@ -471,10 +504,10 @@ pub mod benchmarking { } /// Prepare function that neither checks nor instruments the passed in code. - pub fn prepare_contract(original_code: &[u8], schedule: &Schedule) - -> Result + pub fn prepare_contract(original_code: Vec, schedule: &Schedule) + -> Result, &'static str> { - let contract_module = ContractModule::new(original_code, schedule)?; + let contract_module = ContractModule::new(&original_code, schedule)?; let memory_limits = get_memory_limits(contract_module.scan_imports::<()>(&[])?, schedule)?; Ok(PrefabWasmModule { schedule_version: schedule.version, @@ -482,6 +515,10 @@ pub mod benchmarking { maximum: memory_limits.1, _reserved: None, code: contract_module.into_wasm_code()?, + original_code_len: original_code.len() as u32, + refcount: 1, + code_hash: T::Hashing::hash(&original_code), + original_code: Some(original_code), }) } } @@ -493,7 +530,7 @@ mod tests { use std::fmt; use assert_matches::assert_matches; - impl fmt::Debug for PrefabWasmModule { + impl fmt::Debug for PrefabWasmModule { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "PreparedContract {{ .. }}") } @@ -534,7 +571,7 @@ mod tests { }, .. Default::default() }; - let r = prepare_contract::(wasm.as_ref(), &schedule); + let r = do_preparation::(wasm, &schedule); assert_matches!(r, $($expected)*); } }; @@ -945,7 +982,7 @@ mod tests { ).unwrap(); let mut schedule = Schedule::default(); schedule.enable_println = true; - let r = prepare_contract::(wasm.as_ref(), &schedule); + let r = do_preparation::(wasm, &schedule); assert_matches!(r, Ok(_)); } } diff --git a/frame/contracts/src/wasm/runtime.rs b/frame/contracts/src/wasm/runtime.rs index 6b459f05193c9..e0f7626b95a9f 100644 --- a/frame/contracts/src/wasm/runtime.rs +++ b/frame/contracts/src/wasm/runtime.rs @@ -18,13 +18,13 @@ //! Environment definition of the wasm smart-contract runtime. use crate::{ - HostFnWeights, Schedule, Config, CodeHash, BalanceOf, Error, + HostFnWeights, Config, CodeHash, BalanceOf, Error, exec::{Ext, StorageKey, TopicOf}, - gas::{Gas, GasMeter, Token, GasMeterResult, ChargedAmount}, + gas::{GasMeter, Token, ChargedAmount}, wasm::env_def::ConvertibleToWasm, }; use parity_wasm::elements::ValueType; -use frame_support::{dispatch::DispatchError, ensure}; +use frame_support::{dispatch::DispatchError, ensure, traits::Get, weights::Weight}; use sp_std::prelude::*; use codec::{Decode, DecodeAll, Encode}; use sp_runtime::traits::SaturatedConversion; @@ -38,6 +38,13 @@ use sp_io::hashing::{ use pallet_contracts_primitives::{ExecResult, ExecReturnValue, ReturnFlags, ExecError}; /// Every error that can be returned to a contract when it calls any of the host functions. +/// +/// # Note +/// +/// This enum can be extended in the future: New codes can be added but existing codes +/// will not be changed or removed. This means that any contract **must not** exhaustively +/// match return codes. Instead, contracts should prepare for unknown variants and deal with +/// those errors gracefuly in order to be forward compatible. #[repr(u32)] pub enum ReturnCode { /// API call successful. @@ -158,8 +165,12 @@ pub enum RuntimeToken { Return(u32), /// Weight of calling `seal_terminate`. Terminate, + /// Weight that is added to `seal_terminate` for every byte of the terminated contract. + TerminateSurchargeCodeSize(u32), /// Weight of calling `seal_restore_to` per number of supplied delta entries. RestoreTo(u32), + /// Weight that is added to `seal_restore_to` for the involved code sizes. + RestoreToSurchargeCodeSize{caller_code: u32, tombstone_code: u32}, /// Weight of calling `seal_random`. It includes the weight for copying the subject. Random, /// Weight of calling `seal_reposit_event` with the given number of topics and event size. @@ -178,6 +189,8 @@ pub enum RuntimeToken { Transfer, /// Weight of calling `seal_call` for the given input size. CallBase(u32), + /// Weight that is added to `seal_call` for every byte of the called contract. + CallSurchargeCodeSize(u32), /// Weight of the transfer performed during a call. CallSurchargeTransfer, /// Weight of output received through `seal_call` for the given size. @@ -186,6 +199,8 @@ pub enum RuntimeToken { /// This includes the transfer as an instantiate without a value will always be below /// the existential deposit and is disregarded as corner case. InstantiateBase{input_data_len: u32, salt_len: u32}, + /// Weight that is added to `seal_instantiate` for every byte of the instantiated contract. + InstantiateSurchargeCodeSize(u32), /// Weight of output received through `seal_instantiate` for the given size. InstantiateCopyOut(u32), /// Weight of calling `seal_hash_sha_256` for the given input size. @@ -208,7 +223,7 @@ where { type Metadata = HostFnWeights; - fn calculate_amount(&self, s: &Self::Metadata) -> Gas { + fn calculate_amount(&self, s: &Self::Metadata) -> Weight { use self::RuntimeToken::*; match *self { MeteringBlock(amount) => s.gas.saturating_add(amount.into()), @@ -228,8 +243,13 @@ where Return(len) => s.r#return .saturating_add(s.return_per_byte.saturating_mul(len.into())), Terminate => s.terminate, + TerminateSurchargeCodeSize(len) => s.terminate_per_code_byte.saturating_mul(len.into()), RestoreTo(delta) => s.restore_to .saturating_add(s.restore_to_per_delta.saturating_mul(delta.into())), + RestoreToSurchargeCodeSize{caller_code, tombstone_code} => + s.restore_to_per_caller_code_byte.saturating_mul(caller_code.into()).saturating_add( + s.restore_to_per_tombstone_code_byte.saturating_mul(tombstone_code.into()) + ), Random => s.random, DepositEvent{num_topic, len} => s.deposit_event .saturating_add(s.deposit_event_per_topic.saturating_mul(num_topic.into())) @@ -243,11 +263,14 @@ where Transfer => s.transfer, CallBase(len) => s.call .saturating_add(s.call_per_input_byte.saturating_mul(len.into())), + CallSurchargeCodeSize(len) => s.call_per_code_byte.saturating_mul(len.into()), CallSurchargeTransfer => s.call_transfer_surcharge, CallCopyOut(len) => s.call_per_output_byte.saturating_mul(len.into()), InstantiateBase{input_data_len, salt_len} => s.instantiate .saturating_add(s.instantiate_per_input_byte.saturating_mul(input_data_len.into())) .saturating_add(s.instantiate_per_salt_byte.saturating_mul(salt_len.into())), + InstantiateSurchargeCodeSize(len) => + s.instantiate_per_code_byte.saturating_mul(len.into()), InstantiateCopyOut(len) => s.instantiate_per_output_byte .saturating_mul(len.into()), HashSha256(len) => s.hash_sha2_256 @@ -293,7 +316,6 @@ fn has_duplicates>(items: &mut Vec) -> bool { pub struct Runtime<'a, E: Ext + 'a> { ext: &'a mut E, input_data: Option>, - schedule: &'a Schedule, memory: sp_sandbox::Memory, gas_meter: &'a mut GasMeter, trap_reason: Option, @@ -308,14 +330,12 @@ where pub fn new( ext: &'a mut E, input_data: Vec, - schedule: &'a Schedule, memory: sp_sandbox::Memory, gas_meter: &'a mut GasMeter, ) -> Self { Runtime { ext, input_data: Some(input_data), - schedule, memory, gas_meter, trap_reason: None, @@ -404,10 +424,19 @@ where where Tok: Token>, { - match self.gas_meter.charge(&self.schedule.host_fn_weights, token) { - GasMeterResult::Proceed(amount) => Ok(amount), - GasMeterResult::OutOfGas => Err(Error::::OutOfGas.into()) - } + self.gas_meter.charge(&self.ext.schedule().host_fn_weights, token) + } + + /// Correct previously charged gas amount. + pub fn adjust_gas(&mut self, charged_amount: ChargedAmount, adjusted_amount: Tok) + where + Tok: Token>, + { + self.gas_meter.adjust_gas( + charged_amount, + &self.ext.schedule().host_fn_weights, + adjusted_amount, + ); } /// Read designated chunk from the sandbox memory. @@ -418,7 +447,7 @@ where pub fn read_sandbox_memory(&self, ptr: u32, len: u32) -> Result, DispatchError> { - ensure!(len <= self.schedule.limits.max_memory_size(), Error::::OutOfBounds); + ensure!(len <= self.ext.schedule().limits.max_memory_size(), Error::::OutOfBounds); let mut buf = vec![0u8; len as usize]; self.memory.get(ptr, buf.as_mut_slice()) .map_err(|_| Error::::OutOfBounds)?; @@ -770,11 +799,12 @@ define_env!(Env, , ctx.read_sandbox_memory_as(callee_ptr, callee_len)?; let value: BalanceOf<::T> = ctx.read_sandbox_memory_as(value_ptr, value_len)?; let input_data = ctx.read_sandbox_memory(input_data_ptr, input_data_len)?; - if value > 0u32.into() { ctx.charge_gas(RuntimeToken::CallSurchargeTransfer)?; } - + let charged = ctx.charge_gas( + RuntimeToken::CallSurchargeCodeSize(::MaxCodeSize::get()) + )?; let nested_gas_limit = if gas == 0 { ctx.gas_meter.gas_left() } else { @@ -792,16 +822,20 @@ define_env!(Env, , ) } // there is not enough gas to allocate for the nested call. - None => Err(Error::<::T>::OutOfGas.into()), + None => Err((Error::<::T>::OutOfGas.into(), 0)), } }); - - if let Ok(output) = &call_outcome { + let code_len = match &call_outcome { + Ok((_, len)) => len, + Err((_, len)) => len, + }; + ctx.adjust_gas(charged, RuntimeToken::CallSurchargeCodeSize(*code_len)); + if let Ok((output, _)) = &call_outcome { ctx.write_sandbox_output(output_ptr, output_len_ptr, &output.data, true, |len| { Some(RuntimeToken::CallCopyOut(len)) })?; } - Ok(Runtime::::exec_into_return_code(call_outcome)?) + Ok(Runtime::::exec_into_return_code(call_outcome.map(|r| r.0).map_err(|r| r.0))?) }, // Instantiate a contract with the specified code hash. @@ -871,7 +905,9 @@ define_env!(Env, , let value: BalanceOf<::T> = ctx.read_sandbox_memory_as(value_ptr, value_len)?; let input_data = ctx.read_sandbox_memory(input_data_ptr, input_data_len)?; let salt = ctx.read_sandbox_memory(salt_ptr, salt_len)?; - + let charged = ctx.charge_gas( + RuntimeToken::InstantiateSurchargeCodeSize(::MaxCodeSize::get()) + )?; let nested_gas_limit = if gas == 0 { ctx.gas_meter.gas_left() } else { @@ -882,7 +918,7 @@ define_env!(Env, , match nested_meter { Some(nested_meter) => { ext.instantiate( - &code_hash, + code_hash, value, nested_meter, input_data, @@ -890,10 +926,15 @@ define_env!(Env, , ) } // there is not enough gas to allocate for the nested call. - None => Err(Error::<::T>::OutOfGas.into()), + None => Err((Error::<::T>::OutOfGas.into(), 0)), } }); - if let Ok((address, output)) = &instantiate_outcome { + let code_len = match &instantiate_outcome { + Ok((_, _, code_len)) => code_len, + Err((_, code_len)) => code_len, + }; + ctx.adjust_gas(charged, RuntimeToken::InstantiateSurchargeCodeSize(*code_len)); + if let Ok((address, output, _)) = &instantiate_outcome { if !output.flags.contains(ReturnFlags::REVERT) { ctx.write_sandbox_output( address_ptr, address_len_ptr, &address.encode(), true, already_charged, @@ -903,7 +944,9 @@ define_env!(Env, , Some(RuntimeToken::InstantiateCopyOut(len)) })?; } - Ok(Runtime::::exec_into_return_code(instantiate_outcome.map(|(_id, retval)| retval))?) + Ok(Runtime::::exec_into_return_code( + instantiate_outcome.map(|(_, retval, _)| retval).map_err(|(err, _)| err) + )?) }, // Remove the calling account and transfer remaining balance. @@ -931,14 +974,32 @@ define_env!(Env, , let beneficiary: <::T as frame_system::Config>::AccountId = ctx.read_sandbox_memory_as(beneficiary_ptr, beneficiary_len)?; - ctx.ext.terminate(&beneficiary)?; + let charged = ctx.charge_gas( + RuntimeToken::TerminateSurchargeCodeSize(::MaxCodeSize::get()) + )?; + let (result, code_len) = match ctx.ext.terminate(&beneficiary) { + Ok(len) => (Ok(()), len), + Err((err, len)) => (Err(err), len), + }; + ctx.adjust_gas(charged, RuntimeToken::TerminateSurchargeCodeSize(code_len)); + result?; Err(TrapReason::Termination) }, - seal_input(ctx, buf_ptr: u32, buf_len_ptr: u32) => { + // Stores the input passed by the caller into the supplied buffer. + // + // The value is stored to linear memory at the address pointed to by `out_ptr`. + // `out_len_ptr` must point to a u32 value that describes the available space at + // `out_ptr`. This call overwrites it with the size of the value. If the available + // space at `out_ptr` is less than the size of the value a trap is triggered. + // + // # Note + // + // This function can only be called once. Calling it multiple times will trigger a trap. + seal_input(ctx, out_ptr: u32, out_len_ptr: u32) => { ctx.charge_gas(RuntimeToken::InputBase)?; if let Some(input) = ctx.input_data.take() { - ctx.write_sandbox_output(buf_ptr, buf_len_ptr, &input, false, |len| { + ctx.write_sandbox_output(out_ptr, out_len_ptr, &input, false, |len| { Some(RuntimeToken::InputCopyOut(len)) })?; Ok(()) @@ -1077,7 +1138,7 @@ define_env!(Env, , // The data is encoded as T::Hash. seal_random(ctx, subject_ptr: u32, subject_len: u32, out_ptr: u32, out_len_ptr: u32) => { ctx.charge_gas(RuntimeToken::Random)?; - if subject_len > ctx.schedule.limits.subject_len { + if subject_len > ctx.ext.schedule().limits.subject_len { Err(Error::::RandomSubjectTooLong)?; } let subject_buf = ctx.read_sandbox_memory(subject_ptr, subject_len)?; @@ -1138,25 +1199,30 @@ define_env!(Env, , // the caller contract and restore the destination contract and set the specified `rent_allowance`. // All caller's funds are transfered to the destination. // - // If there is no tombstone at the destination address, the hashes don't match or this contract - // instance is already present on the contract call stack, a trap is generated. + // The tombstone hash is derived as `hash(code_hash, storage_root_hash)`. In order to match + // this hash to its own hash the restorer must make its storage equal to the one of the + // evicted destination contract. In order to allow for additional storage items in the + // restoring contract a delta can be specified to this function. All keys specified as + // delta are disregarded when calculating the storage root hash. // - // Otherwise, the destination contract is restored. This function is diverging and stops execution - // even on success. + // On success, the destination contract is restored. This function is diverging and + // stops execution even on success. // - // `dest_ptr`, `dest_len` - the pointer and the length of a buffer that encodes `T::AccountId` - // with the address of the to be restored contract. - // `code_hash_ptr`, `code_hash_len` - the pointer and the length of a buffer that encodes - // a code hash of the to be restored contract. - // `rent_allowance_ptr`, `rent_allowance_len` - the pointer and the length of a buffer that - // encodes the rent allowance that must be set in the case of successful restoration. - // `delta_ptr` is the pointer to the start of a buffer that has `delta_count` storage keys - // laid out sequentially. + // - `dest_ptr`, `dest_len` - the pointer and the length of a buffer that encodes `T::AccountId` + // with the address of the to be restored contract. + // - `code_hash_ptr`, `code_hash_len` - the pointer and the length of a buffer that encodes + // a code hash of the to be restored contract. + // - `rent_allowance_ptr`, `rent_allowance_len` - the pointer and the length of a buffer that + // encodes the rent allowance that must be set in the case of successful restoration. + // - `delta_ptr` is the pointer to the start of a buffer that has `delta_count` storage keys + // laid out sequentially. // // # Traps // - // - Tombstone hashes do not match - // - Calling cantract is live i.e is already on the call stack. + // - There is no tombstone at the destination address. + // - Tombstone hashes do not match. + // - The calling contract is already present on the call stack. + // - The supplied code_hash does not exist on-chain. seal_restore_to( ctx, dest_ptr: u32, @@ -1183,7 +1249,7 @@ define_env!(Env, , // allocator can handle. ensure!( delta_count - .saturating_mul(KEY_SIZE as u32) <= ctx.schedule.limits.max_memory_size(), + .saturating_mul(KEY_SIZE as u32) <= ctx.ext.schedule().limits.max_memory_size(), Error::::OutOfBounds, ); let mut delta = vec![[0; KEY_SIZE]; delta_count as usize]; @@ -1201,7 +1267,22 @@ define_env!(Env, , delta }; - ctx.ext.restore_to(dest, code_hash, rent_allowance, delta)?; + let max_len = ::MaxCodeSize::get(); + let charged = ctx.charge_gas(RuntimeToken::RestoreToSurchargeCodeSize { + caller_code: max_len, + tombstone_code: max_len, + })?; + let (result, caller_code, tombstone_code) = match ctx.ext.restore_to( + dest, code_hash, rent_allowance, delta + ) { + Ok((code, tomb)) => (Ok(()), code, tomb), + Err((err, code, tomb)) => (Err(err), code, tomb), + }; + ctx.adjust_gas(charged, RuntimeToken::RestoreToSurchargeCodeSize { + caller_code, + tombstone_code, + }); + result?; Err(TrapReason::Restoration) }, @@ -1231,7 +1312,7 @@ define_env!(Env, , }; // If there are more than `event_topics`, then trap. - if topics.len() > ctx.schedule.limits.event_topics as usize { + if topics.len() > ctx.ext.schedule().limits.event_topics as usize { Err(Error::::TooManyTopics)?; } diff --git a/frame/contracts/src/weights.rs b/frame/contracts/src/weights.rs index 60d229101816e..905ccf8cb5a2f 100644 --- a/frame/contracts/src/weights.rs +++ b/frame/contracts/src/weights.rs @@ -17,8 +17,8 @@ //! Autogenerated weights for pallet_contracts //! -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.1 -//! DATE: 2021-01-12, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 +//! DATE: 2021-02-18, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -47,11 +47,12 @@ pub trait WeightInfo { fn on_initialize() -> Weight; fn on_initialize_per_trie_key(k: u32, ) -> Weight; fn on_initialize_per_queue_item(q: u32, ) -> Weight; + fn instrument(c: u32, ) -> Weight; fn update_schedule() -> Weight; - fn put_code(n: u32, ) -> Weight; - fn instantiate(n: u32, s: u32, ) -> Weight; - fn call() -> Weight; - fn claim_surcharge() -> Weight; + fn instantiate_with_code(c: u32, s: u32, ) -> Weight; + fn instantiate(c: u32, s: u32, ) -> Weight; + fn call(c: u32, ) -> Weight; + fn claim_surcharge(c: u32, ) -> Weight; fn seal_caller(r: u32, ) -> Weight; fn seal_address(r: u32, ) -> Weight; fn seal_gas_left(r: u32, ) -> Weight; @@ -69,8 +70,9 @@ pub trait WeightInfo { fn seal_return(r: u32, ) -> Weight; fn seal_return_per_kb(n: u32, ) -> Weight; fn seal_terminate(r: u32, ) -> Weight; + fn seal_terminate_per_code_kb(c: u32, ) -> Weight; fn seal_restore_to(r: u32, ) -> Weight; - fn seal_restore_to_per_delta(d: u32, ) -> Weight; + fn seal_restore_to_per_code_kb_delta(c: u32, t: u32, d: u32, ) -> Weight; fn seal_random(r: u32, ) -> Weight; fn seal_deposit_event(r: u32, ) -> Weight; fn seal_deposit_event_per_topic_and_kb(t: u32, n: u32, ) -> Weight; @@ -82,9 +84,9 @@ pub trait WeightInfo { fn seal_get_storage_per_kb(n: u32, ) -> Weight; fn seal_transfer(r: u32, ) -> Weight; fn seal_call(r: u32, ) -> Weight; - fn seal_call_per_transfer_input_output_kb(t: u32, i: u32, o: u32, ) -> Weight; + fn seal_call_per_code_transfer_input_output_kb(c: u32, t: u32, i: u32, o: u32, ) -> Weight; fn seal_instantiate(r: u32, ) -> Weight; - fn seal_instantiate_per_input_output_salt_kb(i: u32, o: u32, s: u32, ) -> Weight; + fn seal_instantiate_per_code_input_output_salt_kb(c: u32, i: u32, o: u32, s: u32, ) -> Weight; fn seal_hash_sha2_256(r: u32, ) -> Weight; fn seal_hash_sha2_256_per_kb(n: u32, ) -> Weight; fn seal_hash_keccak_256(r: u32, ) -> Weight; @@ -150,247 +152,271 @@ pub trait WeightInfo { pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { fn on_initialize() -> Weight { - (3_659_000 as Weight) + (3_733_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) } fn on_initialize_per_trie_key(k: u32, ) -> Weight { - (40_731_000 as Weight) - // Standard Error: 4_000 - .saturating_add((2_317_000 as Weight).saturating_mul(k as Weight)) + (49_569_000 as Weight) + // Standard Error: 5_000 + .saturating_add((2_295_000 as Weight).saturating_mul(k as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(k as Weight))) } fn on_initialize_per_queue_item(q: u32, ) -> Weight { - (384_459_000 as Weight) - // Standard Error: 45_000 - .saturating_add((146_401_000 as Weight).saturating_mul(q as Weight)) + (358_064_000 as Weight) + // Standard Error: 143_000 + .saturating_add((140_992_000 as Weight).saturating_mul(q as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } - fn update_schedule() -> Weight { - (27_803_000 as Weight) + fn instrument(c: u32, ) -> Weight { + (44_198_000 as Weight) + // Standard Error: 188_000 + .saturating_add((125_833_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } - fn put_code(n: u32, ) -> Weight { - (0 as Weight) - // Standard Error: 208_000 - .saturating_add((110_774_000 as Weight).saturating_mul(n as Weight)) + fn update_schedule() -> Weight { + (29_190_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) } - fn instantiate(n: u32, s: u32, ) -> Weight { - (175_290_000 as Weight) - // Standard Error: 1_000 - .saturating_add((3_000 as Weight).saturating_mul(n as Weight)) + fn instantiate_with_code(c: u32, s: u32, ) -> Weight { + (180_015_000 as Weight) + // Standard Error: 197_000 + .saturating_add((167_480_000 as Weight).saturating_mul(c as Weight)) + // Standard Error: 12_000 + .saturating_add((2_581_000 as Weight).saturating_mul(s as Weight)) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) + .saturating_add(T::DbWeight::get().writes(5 as Weight)) + } + fn instantiate(c: u32, s: u32, ) -> Weight { + (180_996_000 as Weight) + // Standard Error: 14_000 + .saturating_add((8_684_000 as Weight).saturating_mul(c as Weight)) // Standard Error: 1_000 - .saturating_add((2_244_000 as Weight).saturating_mul(s as Weight)) + .saturating_add((2_518_000 as Weight).saturating_mul(s as Weight)) .saturating_add(T::DbWeight::get().reads(6 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } - fn call() -> Weight { - (161_225_000 as Weight) + fn call(c: u32, ) -> Weight { + (184_326_000 as Weight) + // Standard Error: 2_000 + .saturating_add((3_920_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } - fn claim_surcharge() -> Weight { - (283_759_000 as Weight) - .saturating_add(T::DbWeight::get().reads(4 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + fn claim_surcharge(c: u32, ) -> Weight { + (303_270_000 as Weight) + // Standard Error: 5_000 + .saturating_add((5_108_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(5 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn seal_caller(r: u32, ) -> Weight { - (118_373_000 as Weight) - // Standard Error: 337_000 - .saturating_add((250_358_000 as Weight).saturating_mul(r as Weight)) + (128_965_000 as Weight) + // Standard Error: 130_000 + .saturating_add((270_123_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_address(r: u32, ) -> Weight { - (125_126_000 as Weight) - // Standard Error: 127_000 - .saturating_add((248_900_000 as Weight).saturating_mul(r as Weight)) + (137_748_000 as Weight) + // Standard Error: 184_000 + .saturating_add((270_103_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_gas_left(r: u32, ) -> Weight { - (127_087_000 as Weight) - // Standard Error: 145_000 - .saturating_add((243_311_000 as Weight).saturating_mul(r as Weight)) + (118_784_000 as Weight) + // Standard Error: 234_000 + .saturating_add((264_467_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_balance(r: u32, ) -> Weight { - (123_879_000 as Weight) - // Standard Error: 227_000 - .saturating_add((521_306_000 as Weight).saturating_mul(r as Weight)) + (146_072_000 as Weight) + // Standard Error: 207_000 + .saturating_add((573_282_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_value_transferred(r: u32, ) -> Weight { - (121_348_000 as Weight) - // Standard Error: 125_000 - .saturating_add((244_379_000 as Weight).saturating_mul(r as Weight)) + (133_857_000 as Weight) + // Standard Error: 151_000 + .saturating_add((263_110_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_minimum_balance(r: u32, ) -> Weight { - (120_680_000 as Weight) - // Standard Error: 107_000 - .saturating_add((244_096_000 as Weight).saturating_mul(r as Weight)) + (130_447_000 as Weight) + // Standard Error: 125_000 + .saturating_add((265_565_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_tombstone_deposit(r: u32, ) -> Weight { - (117_310_000 as Weight) - // Standard Error: 130_000 - .saturating_add((245_096_000 as Weight).saturating_mul(r as Weight)) + (116_232_000 as Weight) + // Standard Error: 327_000 + .saturating_add((265_728_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_rent_allowance(r: u32, ) -> Weight { - (131_643_000 as Weight) - // Standard Error: 171_000 - .saturating_add((554_208_000 as Weight).saturating_mul(r as Weight)) + (175_561_000 as Weight) + // Standard Error: 292_000 + .saturating_add((604_373_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_block_number(r: u32, ) -> Weight { - (117_553_000 as Weight) - // Standard Error: 128_000 - .saturating_add((244_494_000 as Weight).saturating_mul(r as Weight)) + (133_961_000 as Weight) + // Standard Error: 150_000 + .saturating_add((262_329_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_now(r: u32, ) -> Weight { - (123_184_000 as Weight) - // Standard Error: 116_000 - .saturating_add((244_414_000 as Weight).saturating_mul(r as Weight)) + (128_662_000 as Weight) + // Standard Error: 150_000 + .saturating_add((263_234_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_weight_to_fee(r: u32, ) -> Weight { - (132_846_000 as Weight) - // Standard Error: 189_000 - .saturating_add((482_450_000 as Weight).saturating_mul(r as Weight)) + (142_580_000 as Weight) + // Standard Error: 205_000 + .saturating_add((505_378_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(6 as Weight)) } fn seal_gas(r: u32, ) -> Weight { - (113_681_000 as Weight) - // Standard Error: 116_000 - .saturating_add((120_711_000 as Weight).saturating_mul(r as Weight)) + (116_346_000 as Weight) + // Standard Error: 86_000 + .saturating_add((124_599_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_input(r: u32, ) -> Weight { - (118_826_000 as Weight) - // Standard Error: 89_000 - .saturating_add((6_650_000 as Weight).saturating_mul(r as Weight)) + (124_679_000 as Weight) + // Standard Error: 81_000 + .saturating_add((7_310_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_input_per_kb(n: u32, ) -> Weight { - (132_497_000 as Weight) + (136_069_000 as Weight) // Standard Error: 0 - .saturating_add((278_000 as Weight).saturating_mul(n as Weight)) + .saturating_add((274_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_return(r: u32, ) -> Weight { - (112_447_000 as Weight) - // Standard Error: 73_000 - .saturating_add((4_398_000 as Weight).saturating_mul(r as Weight)) + (118_807_000 as Weight) + // Standard Error: 66_000 + .saturating_add((4_740_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_return_per_kb(n: u32, ) -> Weight { - (120_288_000 as Weight) + (127_702_000 as Weight) // Standard Error: 0 - .saturating_add((787_000 as Weight).saturating_mul(n as Weight)) + .saturating_add((784_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_terminate(r: u32, ) -> Weight { - (118_973_000 as Weight) - // Standard Error: 124_000 - .saturating_add((75_967_000 as Weight).saturating_mul(r as Weight)) + (124_847_000 as Weight) + // Standard Error: 87_000 + .saturating_add((107_679_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes((4 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes((5 as Weight).saturating_mul(r as Weight))) + } + fn seal_terminate_per_code_kb(c: u32, ) -> Weight { + (237_115_000 as Weight) + // Standard Error: 6_000 + .saturating_add((8_556_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(7 as Weight)) + .saturating_add(T::DbWeight::get().writes(5 as Weight)) } fn seal_restore_to(r: u32, ) -> Weight { - (207_295_000 as Weight) - // Standard Error: 385_000 - .saturating_add((103_584_000 as Weight).saturating_mul(r as Weight)) + (217_959_000 as Weight) + // Standard Error: 455_000 + .saturating_add((134_528_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) - .saturating_add(T::DbWeight::get().reads((3 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes((4 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().reads((4 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes((6 as Weight).saturating_mul(r as Weight))) } - fn seal_restore_to_per_delta(d: u32, ) -> Weight { + fn seal_restore_to_per_code_kb_delta(c: u32, t: u32, d: u32, ) -> Weight { (0 as Weight) - // Standard Error: 2_349_000 - .saturating_add((3_693_440_000 as Weight).saturating_mul(d as Weight)) - .saturating_add(T::DbWeight::get().reads(7 as Weight)) + // Standard Error: 151_000 + .saturating_add((9_061_000 as Weight).saturating_mul(c as Weight)) + // Standard Error: 151_000 + .saturating_add((4_807_000 as Weight).saturating_mul(t as Weight)) + // Standard Error: 1_331_000 + .saturating_add((3_736_196_000 as Weight).saturating_mul(d as Weight)) + .saturating_add(T::DbWeight::get().reads(8 as Weight)) .saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(d as Weight))) - .saturating_add(T::DbWeight::get().writes(5 as Weight)) + .saturating_add(T::DbWeight::get().writes(7 as Weight)) .saturating_add(T::DbWeight::get().writes((100 as Weight).saturating_mul(d as Weight))) } fn seal_random(r: u32, ) -> Weight { - (166_160_000 as Weight) - // Standard Error: 237_000 - .saturating_add((594_474_000 as Weight).saturating_mul(r as Weight)) + (134_143_000 as Weight) + // Standard Error: 233_000 + .saturating_add((643_555_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(6 as Weight)) } fn seal_deposit_event(r: u32, ) -> Weight { - (145_170_000 as Weight) - // Standard Error: 397_000 - .saturating_add((859_096_000 as Weight).saturating_mul(r as Weight)) + (142_838_000 as Weight) + // Standard Error: 367_000 + .saturating_add((937_126_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_deposit_event_per_topic_and_kb(t: u32, n: u32, ) -> Weight { - (1_128_905_000 as Weight) - // Standard Error: 4_299_000 - .saturating_add((559_485_000 as Weight).saturating_mul(t as Weight)) - // Standard Error: 847_000 - .saturating_add((253_404_000 as Weight).saturating_mul(n as Weight)) + (1_210_711_000 as Weight) + // Standard Error: 2_124_000 + .saturating_add((594_541_000 as Weight).saturating_mul(t as Weight)) + // Standard Error: 418_000 + .saturating_add((251_068_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(t as Weight))) .saturating_add(T::DbWeight::get().writes((100 as Weight).saturating_mul(t as Weight))) } fn seal_set_rent_allowance(r: u32, ) -> Weight { - (127_849_000 as Weight) + (144_533_000 as Weight) // Standard Error: 220_000 - .saturating_add((628_543_000 as Weight).saturating_mul(r as Weight)) + .saturating_add((714_590_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn seal_set_storage(r: u32, ) -> Weight { - (0 as Weight) - // Standard Error: 45_695_000 - .saturating_add((17_015_513_000 as Weight).saturating_mul(r as Weight)) + (406_366_000 as Weight) + // Standard Error: 3_533_000 + .saturating_add((16_167_082_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) .saturating_add(T::DbWeight::get().writes((100 as Weight).saturating_mul(r as Weight))) } fn seal_set_storage_per_kb(n: u32, ) -> Weight { - (1_632_351_000 as Weight) - // Standard Error: 399_000 - .saturating_add((73_694_000 as Weight).saturating_mul(n as Weight)) + (1_739_590_000 as Weight) + // Standard Error: 390_000 + .saturating_add((74_815_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn seal_clear_storage(r: u32, ) -> Weight { (0 as Weight) - // Standard Error: 2_632_000 - .saturating_add((2_148_012_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 2_284_000 + .saturating_add((2_281_347_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) .saturating_add(T::DbWeight::get().writes((100 as Weight).saturating_mul(r as Weight))) } fn seal_get_storage(r: u32, ) -> Weight { - (48_127_000 as Weight) - // Standard Error: 1_123_000 - .saturating_add((906_947_000 as Weight).saturating_mul(r as Weight)) + (81_889_000 as Weight) + // Standard Error: 1_171_000 + .saturating_add((930_704_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(r as Weight))) } fn seal_get_storage_per_kb(n: u32, ) -> Weight { - (676_986_000 as Weight) - // Standard Error: 307_000 - .saturating_add((153_667_000 as Weight).saturating_mul(n as Weight)) + (709_323_000 as Weight) + // Standard Error: 391_000 + .saturating_add((155_689_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(6 as Weight)) } fn seal_transfer(r: u32, ) -> Weight { - (36_730_000 as Weight) - // Standard Error: 1_966_000 - .saturating_add((3_972_101_000 as Weight).saturating_mul(r as Weight)) + (0 as Weight) + // Standard Error: 1_846_000 + .saturating_add((5_566_275_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -398,591 +424,619 @@ impl WeightInfo for SubstrateWeight { } fn seal_call(r: u32, ) -> Weight { (0 as Weight) - // Standard Error: 10_776_000 - .saturating_add((9_860_978_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 4_823_000 + .saturating_add((10_461_861_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().reads((200 as Weight).saturating_mul(r as Weight))) } - fn seal_call_per_transfer_input_output_kb(t: u32, i: u32, o: u32, ) -> Weight { - (9_838_971_000 as Weight) - // Standard Error: 112_906_000 - .saturating_add((3_413_715_000 as Weight).saturating_mul(t as Weight)) - // Standard Error: 40_000 - .saturating_add((60_054_000 as Weight).saturating_mul(i as Weight)) - // Standard Error: 43_000 - .saturating_add((82_629_000 as Weight).saturating_mul(o as Weight)) + fn seal_call_per_code_transfer_input_output_kb(c: u32, t: u32, i: u32, o: u32, ) -> Weight { + (9_686_594_000 as Weight) + // Standard Error: 473_000 + .saturating_add((393_132_000 as Weight).saturating_mul(c as Weight)) + // Standard Error: 197_094_000 + .saturating_add((4_957_181_000 as Weight).saturating_mul(t as Weight)) + // Standard Error: 62_000 + .saturating_add((59_974_000 as Weight).saturating_mul(i as Weight)) + // Standard Error: 66_000 + .saturating_add((83_027_000 as Weight).saturating_mul(o as Weight)) .saturating_add(T::DbWeight::get().reads(206 as Weight)) .saturating_add(T::DbWeight::get().writes((101 as Weight).saturating_mul(t as Weight))) } fn seal_instantiate(r: u32, ) -> Weight { (0 as Weight) - // Standard Error: 36_803_000 - .saturating_add((18_211_156_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 34_133_000 + .saturating_add((21_407_630_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().reads((300 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(2 as Weight)) - .saturating_add(T::DbWeight::get().writes((200 as Weight).saturating_mul(r as Weight))) - } - fn seal_instantiate_per_input_output_salt_kb(i: u32, o: u32, s: u32, ) -> Weight { - (15_975_563_000 as Weight) - // Standard Error: 167_000 - .saturating_add((60_759_000 as Weight).saturating_mul(i as Weight)) - // Standard Error: 167_000 - .saturating_add((83_681_000 as Weight).saturating_mul(o as Weight)) - // Standard Error: 167_000 - .saturating_add((284_260_000 as Weight).saturating_mul(s as Weight)) + .saturating_add(T::DbWeight::get().writes((300 as Weight).saturating_mul(r as Weight))) + } + fn seal_instantiate_per_code_input_output_salt_kb(c: u32, i: u32, o: u32, s: u32, ) -> Weight { + (9_705_322_000 as Weight) + // Standard Error: 674_000 + .saturating_add((879_118_000 as Weight).saturating_mul(c as Weight)) + // Standard Error: 95_000 + .saturating_add((63_025_000 as Weight).saturating_mul(i as Weight)) + // Standard Error: 95_000 + .saturating_add((87_633_000 as Weight).saturating_mul(o as Weight)) + // Standard Error: 95_000 + .saturating_add((311_987_000 as Weight).saturating_mul(s as Weight)) .saturating_add(T::DbWeight::get().reads(207 as Weight)) - .saturating_add(T::DbWeight::get().writes(202 as Weight)) + .saturating_add(T::DbWeight::get().writes(203 as Weight)) } fn seal_hash_sha2_256(r: u32, ) -> Weight { - (120_795_000 as Weight) - // Standard Error: 115_000 - .saturating_add((226_658_000 as Weight).saturating_mul(r as Weight)) + (125_486_000 as Weight) + // Standard Error: 266_000 + .saturating_add((240_913_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_hash_sha2_256_per_kb(n: u32, ) -> Weight { - (731_640_000 as Weight) - // Standard Error: 56_000 - .saturating_add((430_102_000 as Weight).saturating_mul(n as Weight)) + (636_153_000 as Weight) + // Standard Error: 47_000 + .saturating_add((429_541_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_hash_keccak_256(r: u32, ) -> Weight { - (121_490_000 as Weight) - // Standard Error: 144_000 - .saturating_add((242_726_000 as Weight).saturating_mul(r as Weight)) + (131_768_000 as Weight) + // Standard Error: 176_000 + .saturating_add((256_946_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_hash_keccak_256_per_kb(n: u32, ) -> Weight { - (624_029_000 as Weight) - // Standard Error: 36_000 - .saturating_add((344_476_000 as Weight).saturating_mul(n as Weight)) + (647_777_000 as Weight) + // Standard Error: 29_000 + .saturating_add((344_145_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_hash_blake2_256(r: u32, ) -> Weight { - (120_959_000 as Weight) - // Standard Error: 103_000 - .saturating_add((215_519_000 as Weight).saturating_mul(r as Weight)) + (130_042_000 as Weight) + // Standard Error: 158_000 + .saturating_add((225_474_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_hash_blake2_256_per_kb(n: u32, ) -> Weight { - (713_448_000 as Weight) - // Standard Error: 47_000 - .saturating_add((160_493_000 as Weight).saturating_mul(n as Weight)) + (638_275_000 as Weight) + // Standard Error: 30_000 + .saturating_add((159_832_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_hash_blake2_128(r: u32, ) -> Weight { - (122_428_000 as Weight) - // Standard Error: 111_000 - .saturating_add((213_863_000 as Weight).saturating_mul(r as Weight)) + (126_632_000 as Weight) + // Standard Error: 143_000 + .saturating_add((225_612_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn seal_hash_blake2_128_per_kb(n: u32, ) -> Weight { - (757_838_000 as Weight) - // Standard Error: 47_000 - .saturating_add((160_245_000 as Weight).saturating_mul(n as Weight)) + (656_936_000 as Weight) + // Standard Error: 35_000 + .saturating_add((159_763_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) } fn instr_i64const(r: u32, ) -> Weight { - (24_075_000 as Weight) - // Standard Error: 18_000 - .saturating_add((3_122_000 as Weight).saturating_mul(r as Weight)) + (25_205_000 as Weight) + // Standard Error: 26_000 + .saturating_add((3_311_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64load(r: u32, ) -> Weight { - (26_406_000 as Weight) - // Standard Error: 31_000 - .saturating_add((159_539_000 as Weight).saturating_mul(r as Weight)) + (27_394_000 as Weight) + // Standard Error: 28_000 + .saturating_add((159_123_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64store(r: u32, ) -> Weight { - (26_266_000 as Weight) - // Standard Error: 3_229_000 - .saturating_add((238_726_000 as Weight).saturating_mul(r as Weight)) + (27_398_000 as Weight) + // Standard Error: 57_000 + .saturating_add((229_775_000 as Weight).saturating_mul(r as Weight)) } fn instr_select(r: u32, ) -> Weight { - (27_469_000 as Weight) - // Standard Error: 592_000 - .saturating_add((10_423_000 as Weight).saturating_mul(r as Weight)) + (25_212_000 as Weight) + // Standard Error: 22_000 + .saturating_add((12_291_000 as Weight).saturating_mul(r as Weight)) } fn instr_if(r: u32, ) -> Weight { - (24_627_000 as Weight) - // Standard Error: 29_000 - .saturating_add((11_999_000 as Weight).saturating_mul(r as Weight)) + (25_116_000 as Weight) + // Standard Error: 16_000 + .saturating_add((12_146_000 as Weight).saturating_mul(r as Weight)) } fn instr_br(r: u32, ) -> Weight { - (24_008_000 as Weight) - // Standard Error: 22_000 - .saturating_add((6_614_000 as Weight).saturating_mul(r as Weight)) + (25_119_000 as Weight) + // Standard Error: 19_000 + .saturating_add((6_608_000 as Weight).saturating_mul(r as Weight)) } fn instr_br_if(r: u32, ) -> Weight { - (24_040_000 as Weight) - // Standard Error: 20_000 - .saturating_add((14_190_000 as Weight).saturating_mul(r as Weight)) + (25_146_000 as Weight) + // Standard Error: 23_000 + .saturating_add((14_017_000 as Weight).saturating_mul(r as Weight)) } fn instr_br_table(r: u32, ) -> Weight { - (23_997_000 as Weight) - // Standard Error: 24_000 - .saturating_add((15_529_000 as Weight).saturating_mul(r as Weight)) + (25_192_000 as Weight) + // Standard Error: 21_000 + .saturating_add((15_460_000 as Weight).saturating_mul(r as Weight)) } fn instr_br_table_per_entry(e: u32, ) -> Weight { - (36_890_000 as Weight) + (37_079_000 as Weight) // Standard Error: 1_000 - .saturating_add((112_000 as Weight).saturating_mul(e as Weight)) + .saturating_add((160_000 as Weight).saturating_mul(e as Weight)) } fn instr_call(r: u32, ) -> Weight { - (24_266_000 as Weight) - // Standard Error: 198_000 - .saturating_add((99_702_000 as Weight).saturating_mul(r as Weight)) + (25_599_000 as Weight) + // Standard Error: 201_000 + .saturating_add((99_705_000 as Weight).saturating_mul(r as Weight)) } fn instr_call_indirect(r: u32, ) -> Weight { - (31_901_000 as Weight) - // Standard Error: 322_000 - .saturating_add((197_671_000 as Weight).saturating_mul(r as Weight)) + (33_236_000 as Weight) + // Standard Error: 368_000 + .saturating_add((199_753_000 as Weight).saturating_mul(r as Weight)) } fn instr_call_indirect_per_param(p: u32, ) -> Weight { - (239_803_000 as Weight) - // Standard Error: 5_000 - .saturating_add((3_474_000 as Weight).saturating_mul(p as Weight)) + (247_488_000 as Weight) + // Standard Error: 6_000 + .saturating_add((3_374_000 as Weight).saturating_mul(p as Weight)) } fn instr_local_get(r: u32, ) -> Weight { - (41_697_000 as Weight) - // Standard Error: 15_000 - .saturating_add((3_225_000 as Weight).saturating_mul(r as Weight)) + (44_133_000 as Weight) + // Standard Error: 20_000 + .saturating_add((3_235_000 as Weight).saturating_mul(r as Weight)) } fn instr_local_set(r: u32, ) -> Weight { - (41_698_000 as Weight) - // Standard Error: 13_000 - .saturating_add((3_458_000 as Weight).saturating_mul(r as Weight)) + (44_107_000 as Weight) + // Standard Error: 20_000 + .saturating_add((3_486_000 as Weight).saturating_mul(r as Weight)) } fn instr_local_tee(r: u32, ) -> Weight { - (41_715_000 as Weight) - // Standard Error: 19_000 - .saturating_add((4_684_000 as Weight).saturating_mul(r as Weight)) + (44_116_000 as Weight) + // Standard Error: 23_000 + .saturating_add((4_757_000 as Weight).saturating_mul(r as Weight)) } fn instr_global_get(r: u32, ) -> Weight { - (27_751_000 as Weight) - // Standard Error: 20_000 - .saturating_add((7_980_000 as Weight).saturating_mul(r as Weight)) + (28_712_000 as Weight) + // Standard Error: 29_000 + .saturating_add((7_659_000 as Weight).saturating_mul(r as Weight)) } fn instr_global_set(r: u32, ) -> Weight { - (27_632_000 as Weight) - // Standard Error: 21_000 - .saturating_add((12_050_000 as Weight).saturating_mul(r as Weight)) + (28_624_000 as Weight) + // Standard Error: 25_000 + .saturating_add((11_841_000 as Weight).saturating_mul(r as Weight)) } fn instr_memory_current(r: u32, ) -> Weight { - (26_302_000 as Weight) - // Standard Error: 25_000 - .saturating_add((3_480_000 as Weight).saturating_mul(r as Weight)) + (27_445_000 as Weight) + // Standard Error: 18_000 + .saturating_add((3_487_000 as Weight).saturating_mul(r as Weight)) } fn instr_memory_grow(r: u32, ) -> Weight { - (24_695_000 as Weight) - // Standard Error: 3_876_000 - .saturating_add((2_324_806_000 as Weight).saturating_mul(r as Weight)) + (26_016_000 as Weight) + // Standard Error: 4_230_000 + .saturating_add((2_300_044_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64clz(r: u32, ) -> Weight { - (24_043_000 as Weight) - // Standard Error: 13_000 - .saturating_add((5_187_000 as Weight).saturating_mul(r as Weight)) + (25_227_000 as Weight) + // Standard Error: 29_000 + .saturating_add((5_341_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64ctz(r: u32, ) -> Weight { - (24_040_000 as Weight) - // Standard Error: 14_000 - .saturating_add((5_077_000 as Weight).saturating_mul(r as Weight)) + (25_163_000 as Weight) + // Standard Error: 26_000 + .saturating_add((5_355_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64popcnt(r: u32, ) -> Weight { - (23_995_000 as Weight) - // Standard Error: 18_000 - .saturating_add((5_801_000 as Weight).saturating_mul(r as Weight)) + (25_204_000 as Weight) + // Standard Error: 29_000 + .saturating_add((5_930_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64eqz(r: u32, ) -> Weight { - (24_010_000 as Weight) - // Standard Error: 12_000 - .saturating_add((5_221_000 as Weight).saturating_mul(r as Weight)) + (25_177_000 as Weight) + // Standard Error: 21_000 + .saturating_add((5_457_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64extendsi32(r: u32, ) -> Weight { - (24_073_000 as Weight) - // Standard Error: 18_000 - .saturating_add((5_205_000 as Weight).saturating_mul(r as Weight)) + (25_206_000 as Weight) + // Standard Error: 19_000 + .saturating_add((5_229_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64extendui32(r: u32, ) -> Weight { - (23_993_000 as Weight) + (25_165_000 as Weight) // Standard Error: 17_000 - .saturating_add((5_079_000 as Weight).saturating_mul(r as Weight)) + .saturating_add((5_301_000 as Weight).saturating_mul(r as Weight)) } fn instr_i32wrapi64(r: u32, ) -> Weight { - (24_008_000 as Weight) - // Standard Error: 16_000 - .saturating_add((5_077_000 as Weight).saturating_mul(r as Weight)) + (25_184_000 as Weight) + // Standard Error: 28_000 + .saturating_add((5_356_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64eq(r: u32, ) -> Weight { - (23_991_000 as Weight) - // Standard Error: 17_000 - .saturating_add((7_248_000 as Weight).saturating_mul(r as Weight)) + (25_195_000 as Weight) + // Standard Error: 48_000 + .saturating_add((7_406_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64ne(r: u32, ) -> Weight { - (23_983_000 as Weight) - // Standard Error: 21_000 + (25_192_000 as Weight) + // Standard Error: 19_000 .saturating_add((7_303_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64lts(r: u32, ) -> Weight { - (23_991_000 as Weight) - // Standard Error: 21_000 - .saturating_add((7_106_000 as Weight).saturating_mul(r as Weight)) + (25_165_000 as Weight) + // Standard Error: 34_000 + .saturating_add((7_247_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64ltu(r: u32, ) -> Weight { - (24_062_000 as Weight) - // Standard Error: 25_000 - .saturating_add((7_168_000 as Weight).saturating_mul(r as Weight)) + (25_152_000 as Weight) + // Standard Error: 46_000 + .saturating_add((7_464_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64gts(r: u32, ) -> Weight { - (24_028_000 as Weight) - // Standard Error: 26_000 - .saturating_add((7_130_000 as Weight).saturating_mul(r as Weight)) + (25_140_000 as Weight) + // Standard Error: 27_000 + .saturating_add((7_308_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64gtu(r: u32, ) -> Weight { - (23_998_000 as Weight) - // Standard Error: 18_000 - .saturating_add((7_279_000 as Weight).saturating_mul(r as Weight)) + (25_723_000 as Weight) + // Standard Error: 29_000 + .saturating_add((6_846_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64les(r: u32, ) -> Weight { - (24_010_000 as Weight) - // Standard Error: 19_000 - .saturating_add((7_114_000 as Weight).saturating_mul(r as Weight)) + (25_201_000 as Weight) + // Standard Error: 20_000 + .saturating_add((7_226_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64leu(r: u32, ) -> Weight { - (24_003_000 as Weight) - // Standard Error: 13_000 - .saturating_add((7_052_000 as Weight).saturating_mul(r as Weight)) + (25_192_000 as Weight) + // Standard Error: 24_000 + .saturating_add((7_143_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64ges(r: u32, ) -> Weight { - (23_948_000 as Weight) - // Standard Error: 15_000 - .saturating_add((7_236_000 as Weight).saturating_mul(r as Weight)) + (25_146_000 as Weight) + // Standard Error: 37_000 + .saturating_add((7_451_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64geu(r: u32, ) -> Weight { - (24_042_000 as Weight) - // Standard Error: 19_000 - .saturating_add((7_223_000 as Weight).saturating_mul(r as Weight)) + (25_193_000 as Weight) + // Standard Error: 30_000 + .saturating_add((7_391_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64add(r: u32, ) -> Weight { - (23_965_000 as Weight) - // Standard Error: 37_000 - .saturating_add((7_261_000 as Weight).saturating_mul(r as Weight)) + (25_192_000 as Weight) + // Standard Error: 30_000 + .saturating_add((7_214_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64sub(r: u32, ) -> Weight { - (24_023_000 as Weight) - // Standard Error: 26_000 - .saturating_add((7_170_000 as Weight).saturating_mul(r as Weight)) + (25_221_000 as Weight) + // Standard Error: 34_000 + .saturating_add((7_168_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64mul(r: u32, ) -> Weight { - (24_057_000 as Weight) - // Standard Error: 17_000 - .saturating_add((7_050_000 as Weight).saturating_mul(r as Weight)) + (25_221_000 as Weight) + // Standard Error: 28_000 + .saturating_add((7_200_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64divs(r: u32, ) -> Weight { - (24_038_000 as Weight) - // Standard Error: 15_000 - .saturating_add((12_934_000 as Weight).saturating_mul(r as Weight)) + (25_229_000 as Weight) + // Standard Error: 32_000 + .saturating_add((13_066_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64divu(r: u32, ) -> Weight { - (23_992_000 as Weight) - // Standard Error: 15_000 - .saturating_add((12_055_000 as Weight).saturating_mul(r as Weight)) + (25_210_000 as Weight) + // Standard Error: 28_000 + .saturating_add((12_314_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64rems(r: u32, ) -> Weight { - (24_082_000 as Weight) - // Standard Error: 18_000 - .saturating_add((12_898_000 as Weight).saturating_mul(r as Weight)) + (25_186_000 as Weight) + // Standard Error: 24_000 + .saturating_add((13_055_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64remu(r: u32, ) -> Weight { - (24_025_000 as Weight) - // Standard Error: 13_000 - .saturating_add((12_178_000 as Weight).saturating_mul(r as Weight)) + (25_162_000 as Weight) + // Standard Error: 25_000 + .saturating_add((12_327_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64and(r: u32, ) -> Weight { - (23_984_000 as Weight) - // Standard Error: 18_000 - .saturating_add((7_214_000 as Weight).saturating_mul(r as Weight)) + (25_191_000 as Weight) + // Standard Error: 24_000 + .saturating_add((7_153_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64or(r: u32, ) -> Weight { - (24_012_000 as Weight) - // Standard Error: 16_000 - .saturating_add((7_183_000 as Weight).saturating_mul(r as Weight)) + (25_184_000 as Weight) + // Standard Error: 23_000 + .saturating_add((7_120_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64xor(r: u32, ) -> Weight { - (24_001_000 as Weight) - // Standard Error: 18_000 - .saturating_add((7_122_000 as Weight).saturating_mul(r as Weight)) + (25_129_000 as Weight) + // Standard Error: 31_000 + .saturating_add((7_247_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64shl(r: u32, ) -> Weight { - (23_973_000 as Weight) - // Standard Error: 13_000 - .saturating_add((7_251_000 as Weight).saturating_mul(r as Weight)) + (25_156_000 as Weight) + // Standard Error: 16_000 + .saturating_add((7_333_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64shrs(r: u32, ) -> Weight { - (23_969_000 as Weight) - // Standard Error: 14_000 - .saturating_add((7_289_000 as Weight).saturating_mul(r as Weight)) + (25_159_000 as Weight) + // Standard Error: 25_000 + .saturating_add((7_415_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64shru(r: u32, ) -> Weight { - (24_008_000 as Weight) - // Standard Error: 15_000 - .saturating_add((7_292_000 as Weight).saturating_mul(r as Weight)) + (25_181_000 as Weight) + // Standard Error: 25_000 + .saturating_add((7_265_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64rotl(r: u32, ) -> Weight { - (24_010_000 as Weight) - // Standard Error: 21_000 - .saturating_add((7_305_000 as Weight).saturating_mul(r as Weight)) + (25_165_000 as Weight) + // Standard Error: 17_000 + .saturating_add((7_443_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64rotr(r: u32, ) -> Weight { - (24_001_000 as Weight) - // Standard Error: 22_000 - .saturating_add((7_299_000 as Weight).saturating_mul(r as Weight)) + (25_103_000 as Weight) + // Standard Error: 44_000 + .saturating_add((7_463_000 as Weight).saturating_mul(r as Weight)) } } // For backwards compatibility and tests impl WeightInfo for () { fn on_initialize() -> Weight { - (3_659_000 as Weight) + (3_733_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) } fn on_initialize_per_trie_key(k: u32, ) -> Weight { - (40_731_000 as Weight) - // Standard Error: 4_000 - .saturating_add((2_317_000 as Weight).saturating_mul(k as Weight)) + (49_569_000 as Weight) + // Standard Error: 5_000 + .saturating_add((2_295_000 as Weight).saturating_mul(k as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(k as Weight))) } fn on_initialize_per_queue_item(q: u32, ) -> Weight { - (384_459_000 as Weight) - // Standard Error: 45_000 - .saturating_add((146_401_000 as Weight).saturating_mul(q as Weight)) + (358_064_000 as Weight) + // Standard Error: 143_000 + .saturating_add((140_992_000 as Weight).saturating_mul(q as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } - fn update_schedule() -> Weight { - (27_803_000 as Weight) + fn instrument(c: u32, ) -> Weight { + (44_198_000 as Weight) + // Standard Error: 188_000 + .saturating_add((125_833_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } - fn put_code(n: u32, ) -> Weight { - (0 as Weight) - // Standard Error: 208_000 - .saturating_add((110_774_000 as Weight).saturating_mul(n as Weight)) + fn update_schedule() -> Weight { + (29_190_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) - .saturating_add(RocksDbWeight::get().writes(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } - fn instantiate(n: u32, s: u32, ) -> Weight { - (175_290_000 as Weight) - // Standard Error: 1_000 - .saturating_add((3_000 as Weight).saturating_mul(n as Weight)) + fn instantiate_with_code(c: u32, s: u32, ) -> Weight { + (180_015_000 as Weight) + // Standard Error: 197_000 + .saturating_add((167_480_000 as Weight).saturating_mul(c as Weight)) + // Standard Error: 12_000 + .saturating_add((2_581_000 as Weight).saturating_mul(s as Weight)) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) + .saturating_add(RocksDbWeight::get().writes(5 as Weight)) + } + fn instantiate(c: u32, s: u32, ) -> Weight { + (180_996_000 as Weight) + // Standard Error: 14_000 + .saturating_add((8_684_000 as Weight).saturating_mul(c as Weight)) // Standard Error: 1_000 - .saturating_add((2_244_000 as Weight).saturating_mul(s as Weight)) + .saturating_add((2_518_000 as Weight).saturating_mul(s as Weight)) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) + .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } - fn call() -> Weight { - (161_225_000 as Weight) + fn call(c: u32, ) -> Weight { + (184_326_000 as Weight) + // Standard Error: 2_000 + .saturating_add((3_920_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } - fn claim_surcharge() -> Weight { - (283_759_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(4 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) + fn claim_surcharge(c: u32, ) -> Weight { + (303_270_000 as Weight) + // Standard Error: 5_000 + .saturating_add((5_108_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn seal_caller(r: u32, ) -> Weight { - (118_373_000 as Weight) - // Standard Error: 337_000 - .saturating_add((250_358_000 as Weight).saturating_mul(r as Weight)) + (128_965_000 as Weight) + // Standard Error: 130_000 + .saturating_add((270_123_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_address(r: u32, ) -> Weight { - (125_126_000 as Weight) - // Standard Error: 127_000 - .saturating_add((248_900_000 as Weight).saturating_mul(r as Weight)) + (137_748_000 as Weight) + // Standard Error: 184_000 + .saturating_add((270_103_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_gas_left(r: u32, ) -> Weight { - (127_087_000 as Weight) - // Standard Error: 145_000 - .saturating_add((243_311_000 as Weight).saturating_mul(r as Weight)) + (118_784_000 as Weight) + // Standard Error: 234_000 + .saturating_add((264_467_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_balance(r: u32, ) -> Weight { - (123_879_000 as Weight) - // Standard Error: 227_000 - .saturating_add((521_306_000 as Weight).saturating_mul(r as Weight)) + (146_072_000 as Weight) + // Standard Error: 207_000 + .saturating_add((573_282_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_value_transferred(r: u32, ) -> Weight { - (121_348_000 as Weight) - // Standard Error: 125_000 - .saturating_add((244_379_000 as Weight).saturating_mul(r as Weight)) + (133_857_000 as Weight) + // Standard Error: 151_000 + .saturating_add((263_110_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_minimum_balance(r: u32, ) -> Weight { - (120_680_000 as Weight) - // Standard Error: 107_000 - .saturating_add((244_096_000 as Weight).saturating_mul(r as Weight)) + (130_447_000 as Weight) + // Standard Error: 125_000 + .saturating_add((265_565_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_tombstone_deposit(r: u32, ) -> Weight { - (117_310_000 as Weight) - // Standard Error: 130_000 - .saturating_add((245_096_000 as Weight).saturating_mul(r as Weight)) + (116_232_000 as Weight) + // Standard Error: 327_000 + .saturating_add((265_728_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_rent_allowance(r: u32, ) -> Weight { - (131_643_000 as Weight) - // Standard Error: 171_000 - .saturating_add((554_208_000 as Weight).saturating_mul(r as Weight)) + (175_561_000 as Weight) + // Standard Error: 292_000 + .saturating_add((604_373_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_block_number(r: u32, ) -> Weight { - (117_553_000 as Weight) - // Standard Error: 128_000 - .saturating_add((244_494_000 as Weight).saturating_mul(r as Weight)) + (133_961_000 as Weight) + // Standard Error: 150_000 + .saturating_add((262_329_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_now(r: u32, ) -> Weight { - (123_184_000 as Weight) - // Standard Error: 116_000 - .saturating_add((244_414_000 as Weight).saturating_mul(r as Weight)) + (128_662_000 as Weight) + // Standard Error: 150_000 + .saturating_add((263_234_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_weight_to_fee(r: u32, ) -> Weight { - (132_846_000 as Weight) - // Standard Error: 189_000 - .saturating_add((482_450_000 as Weight).saturating_mul(r as Weight)) + (142_580_000 as Weight) + // Standard Error: 205_000 + .saturating_add((505_378_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) } fn seal_gas(r: u32, ) -> Weight { - (113_681_000 as Weight) - // Standard Error: 116_000 - .saturating_add((120_711_000 as Weight).saturating_mul(r as Weight)) + (116_346_000 as Weight) + // Standard Error: 86_000 + .saturating_add((124_599_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_input(r: u32, ) -> Weight { - (118_826_000 as Weight) - // Standard Error: 89_000 - .saturating_add((6_650_000 as Weight).saturating_mul(r as Weight)) + (124_679_000 as Weight) + // Standard Error: 81_000 + .saturating_add((7_310_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_input_per_kb(n: u32, ) -> Weight { - (132_497_000 as Weight) + (136_069_000 as Weight) // Standard Error: 0 - .saturating_add((278_000 as Weight).saturating_mul(n as Weight)) + .saturating_add((274_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_return(r: u32, ) -> Weight { - (112_447_000 as Weight) - // Standard Error: 73_000 - .saturating_add((4_398_000 as Weight).saturating_mul(r as Weight)) + (118_807_000 as Weight) + // Standard Error: 66_000 + .saturating_add((4_740_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_return_per_kb(n: u32, ) -> Weight { - (120_288_000 as Weight) + (127_702_000 as Weight) // Standard Error: 0 - .saturating_add((787_000 as Weight).saturating_mul(n as Weight)) + .saturating_add((784_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_terminate(r: u32, ) -> Weight { - (118_973_000 as Weight) - // Standard Error: 124_000 - .saturating_add((75_967_000 as Weight).saturating_mul(r as Weight)) + (124_847_000 as Weight) + // Standard Error: 87_000 + .saturating_add((107_679_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(RocksDbWeight::get().writes((4 as Weight).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().writes((5 as Weight).saturating_mul(r as Weight))) + } + fn seal_terminate_per_code_kb(c: u32, ) -> Weight { + (237_115_000 as Weight) + // Standard Error: 6_000 + .saturating_add((8_556_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(RocksDbWeight::get().reads(7 as Weight)) + .saturating_add(RocksDbWeight::get().writes(5 as Weight)) } fn seal_restore_to(r: u32, ) -> Weight { - (207_295_000 as Weight) - // Standard Error: 385_000 - .saturating_add((103_584_000 as Weight).saturating_mul(r as Weight)) + (217_959_000 as Weight) + // Standard Error: 455_000 + .saturating_add((134_528_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) - .saturating_add(RocksDbWeight::get().reads((3 as Weight).saturating_mul(r as Weight))) - .saturating_add(RocksDbWeight::get().writes((4 as Weight).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().reads((4 as Weight).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().writes((6 as Weight).saturating_mul(r as Weight))) } - fn seal_restore_to_per_delta(d: u32, ) -> Weight { + fn seal_restore_to_per_code_kb_delta(c: u32, t: u32, d: u32, ) -> Weight { (0 as Weight) - // Standard Error: 2_349_000 - .saturating_add((3_693_440_000 as Weight).saturating_mul(d as Weight)) - .saturating_add(RocksDbWeight::get().reads(7 as Weight)) + // Standard Error: 151_000 + .saturating_add((9_061_000 as Weight).saturating_mul(c as Weight)) + // Standard Error: 151_000 + .saturating_add((4_807_000 as Weight).saturating_mul(t as Weight)) + // Standard Error: 1_331_000 + .saturating_add((3_736_196_000 as Weight).saturating_mul(d as Weight)) + .saturating_add(RocksDbWeight::get().reads(8 as Weight)) .saturating_add(RocksDbWeight::get().reads((100 as Weight).saturating_mul(d as Weight))) - .saturating_add(RocksDbWeight::get().writes(5 as Weight)) + .saturating_add(RocksDbWeight::get().writes(7 as Weight)) .saturating_add(RocksDbWeight::get().writes((100 as Weight).saturating_mul(d as Weight))) } fn seal_random(r: u32, ) -> Weight { - (166_160_000 as Weight) - // Standard Error: 237_000 - .saturating_add((594_474_000 as Weight).saturating_mul(r as Weight)) + (134_143_000 as Weight) + // Standard Error: 233_000 + .saturating_add((643_555_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) } fn seal_deposit_event(r: u32, ) -> Weight { - (145_170_000 as Weight) - // Standard Error: 397_000 - .saturating_add((859_096_000 as Weight).saturating_mul(r as Weight)) + (142_838_000 as Weight) + // Standard Error: 367_000 + .saturating_add((937_126_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_deposit_event_per_topic_and_kb(t: u32, n: u32, ) -> Weight { - (1_128_905_000 as Weight) - // Standard Error: 4_299_000 - .saturating_add((559_485_000 as Weight).saturating_mul(t as Weight)) - // Standard Error: 847_000 - .saturating_add((253_404_000 as Weight).saturating_mul(n as Weight)) + (1_210_711_000 as Weight) + // Standard Error: 2_124_000 + .saturating_add((594_541_000 as Weight).saturating_mul(t as Weight)) + // Standard Error: 418_000 + .saturating_add((251_068_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().reads((100 as Weight).saturating_mul(t as Weight))) .saturating_add(RocksDbWeight::get().writes((100 as Weight).saturating_mul(t as Weight))) } fn seal_set_rent_allowance(r: u32, ) -> Weight { - (127_849_000 as Weight) + (144_533_000 as Weight) // Standard Error: 220_000 - .saturating_add((628_543_000 as Weight).saturating_mul(r as Weight)) + .saturating_add((714_590_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn seal_set_storage(r: u32, ) -> Weight { - (0 as Weight) - // Standard Error: 45_695_000 - .saturating_add((17_015_513_000 as Weight).saturating_mul(r as Weight)) + (406_366_000 as Weight) + // Standard Error: 3_533_000 + .saturating_add((16_167_082_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().reads((100 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) .saturating_add(RocksDbWeight::get().writes((100 as Weight).saturating_mul(r as Weight))) } fn seal_set_storage_per_kb(n: u32, ) -> Weight { - (1_632_351_000 as Weight) - // Standard Error: 399_000 - .saturating_add((73_694_000 as Weight).saturating_mul(n as Weight)) + (1_739_590_000 as Weight) + // Standard Error: 390_000 + .saturating_add((74_815_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } fn seal_clear_storage(r: u32, ) -> Weight { (0 as Weight) - // Standard Error: 2_632_000 - .saturating_add((2_148_012_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 2_284_000 + .saturating_add((2_281_347_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().reads((100 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) .saturating_add(RocksDbWeight::get().writes((100 as Weight).saturating_mul(r as Weight))) } fn seal_get_storage(r: u32, ) -> Weight { - (48_127_000 as Weight) - // Standard Error: 1_123_000 - .saturating_add((906_947_000 as Weight).saturating_mul(r as Weight)) + (81_889_000 as Weight) + // Standard Error: 1_171_000 + .saturating_add((930_704_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().reads((100 as Weight).saturating_mul(r as Weight))) } fn seal_get_storage_per_kb(n: u32, ) -> Weight { - (676_986_000 as Weight) - // Standard Error: 307_000 - .saturating_add((153_667_000 as Weight).saturating_mul(n as Weight)) + (709_323_000 as Weight) + // Standard Error: 391_000 + .saturating_add((155_689_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) } fn seal_transfer(r: u32, ) -> Weight { - (36_730_000 as Weight) - // Standard Error: 1_966_000 - .saturating_add((3_972_101_000 as Weight).saturating_mul(r as Weight)) + (0 as Weight) + // Standard Error: 1_846_000 + .saturating_add((5_566_275_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().reads((100 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) @@ -990,343 +1044,347 @@ impl WeightInfo for () { } fn seal_call(r: u32, ) -> Weight { (0 as Weight) - // Standard Error: 10_776_000 - .saturating_add((9_860_978_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 4_823_000 + .saturating_add((10_461_861_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().reads((200 as Weight).saturating_mul(r as Weight))) } - fn seal_call_per_transfer_input_output_kb(t: u32, i: u32, o: u32, ) -> Weight { - (9_838_971_000 as Weight) - // Standard Error: 112_906_000 - .saturating_add((3_413_715_000 as Weight).saturating_mul(t as Weight)) - // Standard Error: 40_000 - .saturating_add((60_054_000 as Weight).saturating_mul(i as Weight)) - // Standard Error: 43_000 - .saturating_add((82_629_000 as Weight).saturating_mul(o as Weight)) + fn seal_call_per_code_transfer_input_output_kb(c: u32, t: u32, i: u32, o: u32, ) -> Weight { + (9_686_594_000 as Weight) + // Standard Error: 473_000 + .saturating_add((393_132_000 as Weight).saturating_mul(c as Weight)) + // Standard Error: 197_094_000 + .saturating_add((4_957_181_000 as Weight).saturating_mul(t as Weight)) + // Standard Error: 62_000 + .saturating_add((59_974_000 as Weight).saturating_mul(i as Weight)) + // Standard Error: 66_000 + .saturating_add((83_027_000 as Weight).saturating_mul(o as Weight)) .saturating_add(RocksDbWeight::get().reads(206 as Weight)) .saturating_add(RocksDbWeight::get().writes((101 as Weight).saturating_mul(t as Weight))) } fn seal_instantiate(r: u32, ) -> Weight { (0 as Weight) - // Standard Error: 36_803_000 - .saturating_add((18_211_156_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 34_133_000 + .saturating_add((21_407_630_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().reads((300 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) - .saturating_add(RocksDbWeight::get().writes((200 as Weight).saturating_mul(r as Weight))) - } - fn seal_instantiate_per_input_output_salt_kb(i: u32, o: u32, s: u32, ) -> Weight { - (15_975_563_000 as Weight) - // Standard Error: 167_000 - .saturating_add((60_759_000 as Weight).saturating_mul(i as Weight)) - // Standard Error: 167_000 - .saturating_add((83_681_000 as Weight).saturating_mul(o as Weight)) - // Standard Error: 167_000 - .saturating_add((284_260_000 as Weight).saturating_mul(s as Weight)) + .saturating_add(RocksDbWeight::get().writes((300 as Weight).saturating_mul(r as Weight))) + } + fn seal_instantiate_per_code_input_output_salt_kb(c: u32, i: u32, o: u32, s: u32, ) -> Weight { + (9_705_322_000 as Weight) + // Standard Error: 674_000 + .saturating_add((879_118_000 as Weight).saturating_mul(c as Weight)) + // Standard Error: 95_000 + .saturating_add((63_025_000 as Weight).saturating_mul(i as Weight)) + // Standard Error: 95_000 + .saturating_add((87_633_000 as Weight).saturating_mul(o as Weight)) + // Standard Error: 95_000 + .saturating_add((311_987_000 as Weight).saturating_mul(s as Weight)) .saturating_add(RocksDbWeight::get().reads(207 as Weight)) - .saturating_add(RocksDbWeight::get().writes(202 as Weight)) + .saturating_add(RocksDbWeight::get().writes(203 as Weight)) } fn seal_hash_sha2_256(r: u32, ) -> Weight { - (120_795_000 as Weight) - // Standard Error: 115_000 - .saturating_add((226_658_000 as Weight).saturating_mul(r as Weight)) + (125_486_000 as Weight) + // Standard Error: 266_000 + .saturating_add((240_913_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_hash_sha2_256_per_kb(n: u32, ) -> Weight { - (731_640_000 as Weight) - // Standard Error: 56_000 - .saturating_add((430_102_000 as Weight).saturating_mul(n as Weight)) + (636_153_000 as Weight) + // Standard Error: 47_000 + .saturating_add((429_541_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_hash_keccak_256(r: u32, ) -> Weight { - (121_490_000 as Weight) - // Standard Error: 144_000 - .saturating_add((242_726_000 as Weight).saturating_mul(r as Weight)) + (131_768_000 as Weight) + // Standard Error: 176_000 + .saturating_add((256_946_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_hash_keccak_256_per_kb(n: u32, ) -> Weight { - (624_029_000 as Weight) - // Standard Error: 36_000 - .saturating_add((344_476_000 as Weight).saturating_mul(n as Weight)) + (647_777_000 as Weight) + // Standard Error: 29_000 + .saturating_add((344_145_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_hash_blake2_256(r: u32, ) -> Weight { - (120_959_000 as Weight) - // Standard Error: 103_000 - .saturating_add((215_519_000 as Weight).saturating_mul(r as Weight)) + (130_042_000 as Weight) + // Standard Error: 158_000 + .saturating_add((225_474_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_hash_blake2_256_per_kb(n: u32, ) -> Weight { - (713_448_000 as Weight) - // Standard Error: 47_000 - .saturating_add((160_493_000 as Weight).saturating_mul(n as Weight)) + (638_275_000 as Weight) + // Standard Error: 30_000 + .saturating_add((159_832_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_hash_blake2_128(r: u32, ) -> Weight { - (122_428_000 as Weight) - // Standard Error: 111_000 - .saturating_add((213_863_000 as Weight).saturating_mul(r as Weight)) + (126_632_000 as Weight) + // Standard Error: 143_000 + .saturating_add((225_612_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn seal_hash_blake2_128_per_kb(n: u32, ) -> Weight { - (757_838_000 as Weight) - // Standard Error: 47_000 - .saturating_add((160_245_000 as Weight).saturating_mul(n as Weight)) + (656_936_000 as Weight) + // Standard Error: 35_000 + .saturating_add((159_763_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) } fn instr_i64const(r: u32, ) -> Weight { - (24_075_000 as Weight) - // Standard Error: 18_000 - .saturating_add((3_122_000 as Weight).saturating_mul(r as Weight)) + (25_205_000 as Weight) + // Standard Error: 26_000 + .saturating_add((3_311_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64load(r: u32, ) -> Weight { - (26_406_000 as Weight) - // Standard Error: 31_000 - .saturating_add((159_539_000 as Weight).saturating_mul(r as Weight)) + (27_394_000 as Weight) + // Standard Error: 28_000 + .saturating_add((159_123_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64store(r: u32, ) -> Weight { - (26_266_000 as Weight) - // Standard Error: 3_229_000 - .saturating_add((238_726_000 as Weight).saturating_mul(r as Weight)) + (27_398_000 as Weight) + // Standard Error: 57_000 + .saturating_add((229_775_000 as Weight).saturating_mul(r as Weight)) } fn instr_select(r: u32, ) -> Weight { - (27_469_000 as Weight) - // Standard Error: 592_000 - .saturating_add((10_423_000 as Weight).saturating_mul(r as Weight)) + (25_212_000 as Weight) + // Standard Error: 22_000 + .saturating_add((12_291_000 as Weight).saturating_mul(r as Weight)) } fn instr_if(r: u32, ) -> Weight { - (24_627_000 as Weight) - // Standard Error: 29_000 - .saturating_add((11_999_000 as Weight).saturating_mul(r as Weight)) + (25_116_000 as Weight) + // Standard Error: 16_000 + .saturating_add((12_146_000 as Weight).saturating_mul(r as Weight)) } fn instr_br(r: u32, ) -> Weight { - (24_008_000 as Weight) - // Standard Error: 22_000 - .saturating_add((6_614_000 as Weight).saturating_mul(r as Weight)) + (25_119_000 as Weight) + // Standard Error: 19_000 + .saturating_add((6_608_000 as Weight).saturating_mul(r as Weight)) } fn instr_br_if(r: u32, ) -> Weight { - (24_040_000 as Weight) - // Standard Error: 20_000 - .saturating_add((14_190_000 as Weight).saturating_mul(r as Weight)) + (25_146_000 as Weight) + // Standard Error: 23_000 + .saturating_add((14_017_000 as Weight).saturating_mul(r as Weight)) } fn instr_br_table(r: u32, ) -> Weight { - (23_997_000 as Weight) - // Standard Error: 24_000 - .saturating_add((15_529_000 as Weight).saturating_mul(r as Weight)) + (25_192_000 as Weight) + // Standard Error: 21_000 + .saturating_add((15_460_000 as Weight).saturating_mul(r as Weight)) } fn instr_br_table_per_entry(e: u32, ) -> Weight { - (36_890_000 as Weight) + (37_079_000 as Weight) // Standard Error: 1_000 - .saturating_add((112_000 as Weight).saturating_mul(e as Weight)) + .saturating_add((160_000 as Weight).saturating_mul(e as Weight)) } fn instr_call(r: u32, ) -> Weight { - (24_266_000 as Weight) - // Standard Error: 198_000 - .saturating_add((99_702_000 as Weight).saturating_mul(r as Weight)) + (25_599_000 as Weight) + // Standard Error: 201_000 + .saturating_add((99_705_000 as Weight).saturating_mul(r as Weight)) } fn instr_call_indirect(r: u32, ) -> Weight { - (31_901_000 as Weight) - // Standard Error: 322_000 - .saturating_add((197_671_000 as Weight).saturating_mul(r as Weight)) + (33_236_000 as Weight) + // Standard Error: 368_000 + .saturating_add((199_753_000 as Weight).saturating_mul(r as Weight)) } fn instr_call_indirect_per_param(p: u32, ) -> Weight { - (239_803_000 as Weight) - // Standard Error: 5_000 - .saturating_add((3_474_000 as Weight).saturating_mul(p as Weight)) + (247_488_000 as Weight) + // Standard Error: 6_000 + .saturating_add((3_374_000 as Weight).saturating_mul(p as Weight)) } fn instr_local_get(r: u32, ) -> Weight { - (41_697_000 as Weight) - // Standard Error: 15_000 - .saturating_add((3_225_000 as Weight).saturating_mul(r as Weight)) + (44_133_000 as Weight) + // Standard Error: 20_000 + .saturating_add((3_235_000 as Weight).saturating_mul(r as Weight)) } fn instr_local_set(r: u32, ) -> Weight { - (41_698_000 as Weight) - // Standard Error: 13_000 - .saturating_add((3_458_000 as Weight).saturating_mul(r as Weight)) + (44_107_000 as Weight) + // Standard Error: 20_000 + .saturating_add((3_486_000 as Weight).saturating_mul(r as Weight)) } fn instr_local_tee(r: u32, ) -> Weight { - (41_715_000 as Weight) - // Standard Error: 19_000 - .saturating_add((4_684_000 as Weight).saturating_mul(r as Weight)) + (44_116_000 as Weight) + // Standard Error: 23_000 + .saturating_add((4_757_000 as Weight).saturating_mul(r as Weight)) } fn instr_global_get(r: u32, ) -> Weight { - (27_751_000 as Weight) - // Standard Error: 20_000 - .saturating_add((7_980_000 as Weight).saturating_mul(r as Weight)) + (28_712_000 as Weight) + // Standard Error: 29_000 + .saturating_add((7_659_000 as Weight).saturating_mul(r as Weight)) } fn instr_global_set(r: u32, ) -> Weight { - (27_632_000 as Weight) - // Standard Error: 21_000 - .saturating_add((12_050_000 as Weight).saturating_mul(r as Weight)) + (28_624_000 as Weight) + // Standard Error: 25_000 + .saturating_add((11_841_000 as Weight).saturating_mul(r as Weight)) } fn instr_memory_current(r: u32, ) -> Weight { - (26_302_000 as Weight) - // Standard Error: 25_000 - .saturating_add((3_480_000 as Weight).saturating_mul(r as Weight)) + (27_445_000 as Weight) + // Standard Error: 18_000 + .saturating_add((3_487_000 as Weight).saturating_mul(r as Weight)) } fn instr_memory_grow(r: u32, ) -> Weight { - (24_695_000 as Weight) - // Standard Error: 3_876_000 - .saturating_add((2_324_806_000 as Weight).saturating_mul(r as Weight)) + (26_016_000 as Weight) + // Standard Error: 4_230_000 + .saturating_add((2_300_044_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64clz(r: u32, ) -> Weight { - (24_043_000 as Weight) - // Standard Error: 13_000 - .saturating_add((5_187_000 as Weight).saturating_mul(r as Weight)) + (25_227_000 as Weight) + // Standard Error: 29_000 + .saturating_add((5_341_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64ctz(r: u32, ) -> Weight { - (24_040_000 as Weight) - // Standard Error: 14_000 - .saturating_add((5_077_000 as Weight).saturating_mul(r as Weight)) + (25_163_000 as Weight) + // Standard Error: 26_000 + .saturating_add((5_355_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64popcnt(r: u32, ) -> Weight { - (23_995_000 as Weight) - // Standard Error: 18_000 - .saturating_add((5_801_000 as Weight).saturating_mul(r as Weight)) + (25_204_000 as Weight) + // Standard Error: 29_000 + .saturating_add((5_930_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64eqz(r: u32, ) -> Weight { - (24_010_000 as Weight) - // Standard Error: 12_000 - .saturating_add((5_221_000 as Weight).saturating_mul(r as Weight)) + (25_177_000 as Weight) + // Standard Error: 21_000 + .saturating_add((5_457_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64extendsi32(r: u32, ) -> Weight { - (24_073_000 as Weight) - // Standard Error: 18_000 - .saturating_add((5_205_000 as Weight).saturating_mul(r as Weight)) + (25_206_000 as Weight) + // Standard Error: 19_000 + .saturating_add((5_229_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64extendui32(r: u32, ) -> Weight { - (23_993_000 as Weight) + (25_165_000 as Weight) // Standard Error: 17_000 - .saturating_add((5_079_000 as Weight).saturating_mul(r as Weight)) + .saturating_add((5_301_000 as Weight).saturating_mul(r as Weight)) } fn instr_i32wrapi64(r: u32, ) -> Weight { - (24_008_000 as Weight) - // Standard Error: 16_000 - .saturating_add((5_077_000 as Weight).saturating_mul(r as Weight)) + (25_184_000 as Weight) + // Standard Error: 28_000 + .saturating_add((5_356_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64eq(r: u32, ) -> Weight { - (23_991_000 as Weight) - // Standard Error: 17_000 - .saturating_add((7_248_000 as Weight).saturating_mul(r as Weight)) + (25_195_000 as Weight) + // Standard Error: 48_000 + .saturating_add((7_406_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64ne(r: u32, ) -> Weight { - (23_983_000 as Weight) - // Standard Error: 21_000 + (25_192_000 as Weight) + // Standard Error: 19_000 .saturating_add((7_303_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64lts(r: u32, ) -> Weight { - (23_991_000 as Weight) - // Standard Error: 21_000 - .saturating_add((7_106_000 as Weight).saturating_mul(r as Weight)) + (25_165_000 as Weight) + // Standard Error: 34_000 + .saturating_add((7_247_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64ltu(r: u32, ) -> Weight { - (24_062_000 as Weight) - // Standard Error: 25_000 - .saturating_add((7_168_000 as Weight).saturating_mul(r as Weight)) + (25_152_000 as Weight) + // Standard Error: 46_000 + .saturating_add((7_464_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64gts(r: u32, ) -> Weight { - (24_028_000 as Weight) - // Standard Error: 26_000 - .saturating_add((7_130_000 as Weight).saturating_mul(r as Weight)) + (25_140_000 as Weight) + // Standard Error: 27_000 + .saturating_add((7_308_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64gtu(r: u32, ) -> Weight { - (23_998_000 as Weight) - // Standard Error: 18_000 - .saturating_add((7_279_000 as Weight).saturating_mul(r as Weight)) + (25_723_000 as Weight) + // Standard Error: 29_000 + .saturating_add((6_846_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64les(r: u32, ) -> Weight { - (24_010_000 as Weight) - // Standard Error: 19_000 - .saturating_add((7_114_000 as Weight).saturating_mul(r as Weight)) + (25_201_000 as Weight) + // Standard Error: 20_000 + .saturating_add((7_226_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64leu(r: u32, ) -> Weight { - (24_003_000 as Weight) - // Standard Error: 13_000 - .saturating_add((7_052_000 as Weight).saturating_mul(r as Weight)) + (25_192_000 as Weight) + // Standard Error: 24_000 + .saturating_add((7_143_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64ges(r: u32, ) -> Weight { - (23_948_000 as Weight) - // Standard Error: 15_000 - .saturating_add((7_236_000 as Weight).saturating_mul(r as Weight)) + (25_146_000 as Weight) + // Standard Error: 37_000 + .saturating_add((7_451_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64geu(r: u32, ) -> Weight { - (24_042_000 as Weight) - // Standard Error: 19_000 - .saturating_add((7_223_000 as Weight).saturating_mul(r as Weight)) + (25_193_000 as Weight) + // Standard Error: 30_000 + .saturating_add((7_391_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64add(r: u32, ) -> Weight { - (23_965_000 as Weight) - // Standard Error: 37_000 - .saturating_add((7_261_000 as Weight).saturating_mul(r as Weight)) + (25_192_000 as Weight) + // Standard Error: 30_000 + .saturating_add((7_214_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64sub(r: u32, ) -> Weight { - (24_023_000 as Weight) - // Standard Error: 26_000 - .saturating_add((7_170_000 as Weight).saturating_mul(r as Weight)) + (25_221_000 as Weight) + // Standard Error: 34_000 + .saturating_add((7_168_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64mul(r: u32, ) -> Weight { - (24_057_000 as Weight) - // Standard Error: 17_000 - .saturating_add((7_050_000 as Weight).saturating_mul(r as Weight)) + (25_221_000 as Weight) + // Standard Error: 28_000 + .saturating_add((7_200_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64divs(r: u32, ) -> Weight { - (24_038_000 as Weight) - // Standard Error: 15_000 - .saturating_add((12_934_000 as Weight).saturating_mul(r as Weight)) + (25_229_000 as Weight) + // Standard Error: 32_000 + .saturating_add((13_066_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64divu(r: u32, ) -> Weight { - (23_992_000 as Weight) - // Standard Error: 15_000 - .saturating_add((12_055_000 as Weight).saturating_mul(r as Weight)) + (25_210_000 as Weight) + // Standard Error: 28_000 + .saturating_add((12_314_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64rems(r: u32, ) -> Weight { - (24_082_000 as Weight) - // Standard Error: 18_000 - .saturating_add((12_898_000 as Weight).saturating_mul(r as Weight)) + (25_186_000 as Weight) + // Standard Error: 24_000 + .saturating_add((13_055_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64remu(r: u32, ) -> Weight { - (24_025_000 as Weight) - // Standard Error: 13_000 - .saturating_add((12_178_000 as Weight).saturating_mul(r as Weight)) + (25_162_000 as Weight) + // Standard Error: 25_000 + .saturating_add((12_327_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64and(r: u32, ) -> Weight { - (23_984_000 as Weight) - // Standard Error: 18_000 - .saturating_add((7_214_000 as Weight).saturating_mul(r as Weight)) + (25_191_000 as Weight) + // Standard Error: 24_000 + .saturating_add((7_153_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64or(r: u32, ) -> Weight { - (24_012_000 as Weight) - // Standard Error: 16_000 - .saturating_add((7_183_000 as Weight).saturating_mul(r as Weight)) + (25_184_000 as Weight) + // Standard Error: 23_000 + .saturating_add((7_120_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64xor(r: u32, ) -> Weight { - (24_001_000 as Weight) - // Standard Error: 18_000 - .saturating_add((7_122_000 as Weight).saturating_mul(r as Weight)) + (25_129_000 as Weight) + // Standard Error: 31_000 + .saturating_add((7_247_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64shl(r: u32, ) -> Weight { - (23_973_000 as Weight) - // Standard Error: 13_000 - .saturating_add((7_251_000 as Weight).saturating_mul(r as Weight)) + (25_156_000 as Weight) + // Standard Error: 16_000 + .saturating_add((7_333_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64shrs(r: u32, ) -> Weight { - (23_969_000 as Weight) - // Standard Error: 14_000 - .saturating_add((7_289_000 as Weight).saturating_mul(r as Weight)) + (25_159_000 as Weight) + // Standard Error: 25_000 + .saturating_add((7_415_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64shru(r: u32, ) -> Weight { - (24_008_000 as Weight) - // Standard Error: 15_000 - .saturating_add((7_292_000 as Weight).saturating_mul(r as Weight)) + (25_181_000 as Weight) + // Standard Error: 25_000 + .saturating_add((7_265_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64rotl(r: u32, ) -> Weight { - (24_010_000 as Weight) - // Standard Error: 21_000 - .saturating_add((7_305_000 as Weight).saturating_mul(r as Weight)) + (25_165_000 as Weight) + // Standard Error: 17_000 + .saturating_add((7_443_000 as Weight).saturating_mul(r as Weight)) } fn instr_i64rotr(r: u32, ) -> Weight { - (24_001_000 as Weight) - // Standard Error: 22_000 - .saturating_add((7_299_000 as Weight).saturating_mul(r as Weight)) + (25_103_000 as Weight) + // Standard Error: 44_000 + .saturating_add((7_463_000 as Weight).saturating_mul(r as Weight)) } } diff --git a/frame/ddc-metrics-offchain-worker/Cargo.toml b/frame/ddc-metrics-offchain-worker/Cargo.toml index 4e6d34b7995d3..ed24e493961a2 100644 --- a/frame/ddc-metrics-offchain-worker/Cargo.toml +++ b/frame/ddc-metrics-offchain-worker/Cargo.toml @@ -13,15 +13,15 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["full"] } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["full"] } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } serde = { version = "1.0.101", optional = true } -sp-keystore = { version = "0.8.0", default-features = false, path = "../../primitives/keystore", optional = true } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +sp-keystore = { version = "0.9.0", default-features = false, path = "../../primitives/keystore", optional = true } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } pallet-contracts = { version = '2.0.0', default-features = false, path = "../contracts" } lite-json = { version = "0.1", default-features = false } alt_serde = { version = "1", default-features = false, features = ["derive"] } @@ -48,7 +48,7 @@ std = [ ] [dev-dependencies] -pallet-balances = { version = "2.0.0", path = "../balances" } -pallet-timestamp = { version = "2.0.0", path = "../timestamp" } -pallet-randomness-collective-flip = { version = "2.0.0", path = "../randomness-collective-flip" } +pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-timestamp = { version = "3.0.0", path = "../timestamp" } +pallet-randomness-collective-flip = { version = "3.0.0", path = "../randomness-collective-flip" } pretty_assertions = "0.6.1" diff --git a/frame/ddc-metrics-offchain-worker/src/tests/mod.rs b/frame/ddc-metrics-offchain-worker/src/tests/mod.rs index 1eb11d30157d4..dda5fc820bfa6 100644 --- a/frame/ddc-metrics-offchain-worker/src/tests/mod.rs +++ b/frame/ddc-metrics-offchain-worker/src/tests/mod.rs @@ -1,6 +1,5 @@ -use frame_support::traits::{Currency, OffchainWorker}; +use frame_support::traits::{Currency, OffchainWorker, GenesisBuild}; use frame_system::Config as FSC; -use pallet_contracts::Gas; use pallet_contracts::{self as contracts, Config as CC}; use sp_core::{ offchain::{testing, OffchainExt, Timestamp as OCWTimestamp, TransactionPoolExt} @@ -20,6 +19,7 @@ use crate::{ }; use codec::Encode; use hex_literal::hex; +use frame_support::weights::Weight; use sp_core::bytes::from_hex; mod test_runtime; @@ -369,14 +369,13 @@ fn deploy_contract() -> AccountId { // Deploy the contract. //let endowment = contracts::Config::::subsistence_threshold_uncached(); - const GAS_LIMIT: Gas = 100_000_000_000; + const GAS_LIMIT: Weight = 100_000_000_000; const ENDOWMENT: Balance = 100_000_000_000; - Contracts::put_code(Origin::signed(alice.clone()), wasm.to_vec()).unwrap(); - Contracts::instantiate( + Contracts::instantiate_with_code( Origin::signed(alice.clone()), ENDOWMENT, GAS_LIMIT, - wasm_hash.into(), + wasm.to_vec(), contract_args.clone(), vec![] ) diff --git a/frame/ddc-metrics-offchain-worker/src/tests/test_runtime.rs b/frame/ddc-metrics-offchain-worker/src/tests/test_runtime.rs index 72657818eaaba..3eadb6abcfbdb 100644 --- a/frame/ddc-metrics-offchain-worker/src/tests/test_runtime.rs +++ b/frame/ddc-metrics-offchain-worker/src/tests/test_runtime.rs @@ -2,7 +2,7 @@ // // Inspired from pos-network-node/frame/contracts/src/tests.rs -use crate::*; +use crate::{*, self as pallet_ddc_metrics_offchain_worker}; use codec::{Decode, Encode}; use frame_support::{ @@ -41,42 +41,24 @@ mod example_offchain_worker { pub use frame_support::impl_outer_event; } -// Macro hack: Give names to the modules. -pub type Balances = balances::Module; -pub type Timestamp = pallet_timestamp::Module; -pub type Contracts = contracts::Module; -pub type System = frame_system::Module; -pub type Randomness = pallet_randomness_collective_flip::Module; - -pub type DdcMetricsOffchainWorker = Module; - -// Macros based on the names above. Not Rust syntax. -impl_outer_event! { - pub enum MetaEvent for Test { - system, - balances, - contracts, - example_offchain_worker, - } -} - -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} - -impl_outer_dispatch! { - pub enum MetaCall for Test where origin: Origin { - balances::Balances, - contracts::Contracts, - example_offchain_worker::DdcMetricsOffchainWorker, +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Contracts: contracts::{Module, Call, Config, Storage, Event}, + Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, + Randomness: pallet_randomness_collective_flip::{Module, Call, Storage}, + DdcMetricsOffchainWorker: pallet_ddc_metrics_offchain_worker::{Module, Call, Event}, } -} +); -// For testing the module, we construct most of a mock runtime. This means -// first constructing a configuration type (`Test`) which `impl`s each of the -// configuration traits of modules we want to use. -#[derive(Clone, Eq, PartialEq, Encode, Decode)] -pub struct Test; parameter_types! { pub const BlockHashCount: BlockNumber = 250; pub const MaximumBlockWeight: Weight = 1024; @@ -91,17 +73,17 @@ impl frame_system::Config for Test { type Index = u64; type BlockNumber = BlockNumber; type Hash = H256; - type Call = MetaCall; + type Call = Call; type Hashing = BlakeTwo256; type AccountId = AccountId; // u64; // sp_core::sr25519::Public; type Lookup = IdentityLookup; type Header = generic::Header; - type Event = MetaEvent; + type Event = Event; type BlockHashCount = BlockHashCount; type DbWeight = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -112,7 +94,7 @@ impl frame_system::Config for Test { impl pallet_balances::Config for Test { type Balance = Balance; type DustRemoval = (); - type Event = MetaEvent; + type Event = Event; type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; type WeightInfo = (); @@ -149,6 +131,7 @@ parameter_types! { pub const SurchargeReward: Balance = 150; pub const MaxDepth: u32 = 100; pub const MaxValueSize: u32 = 16_384; + pub MaxCodeSize: u32 = 160 * 1024; } // Contracts for Test Runtime. @@ -159,7 +142,7 @@ impl contracts::Config for Test { type Time = Timestamp; type Randomness = Randomness; type Currency = Balances; - type Event = MetaEvent; + type Event = Event; type RentPayment = (); type SignedClaimHandicap = SignedClaimHandicap; type TombstoneDeposit = TombstoneDeposit; @@ -175,6 +158,7 @@ impl contracts::Config for Test { type ChainExtension = (); type DeletionQueueDepth = (); type DeletionWeightLimit = (); + type MaxCodeSize = MaxCodeSize; } parameter_types! { @@ -193,7 +177,7 @@ use frame_system::offchain::{ AppCrypto, CreateSignedTransaction, SendTransactionTypes, SigningTypes, }; -pub type Extrinsic = TestXt; +pub type Extrinsic = TestXt; impl SigningTypes for Test { type Public = ::Signer; @@ -202,22 +186,22 @@ impl SigningTypes for Test { impl SendTransactionTypes for Test where - MetaCall: From, + Call: From, { - type OverarchingCall = MetaCall; + type OverarchingCall = Call; type Extrinsic = Extrinsic; } impl CreateSignedTransaction for Test where - MetaCall: From, + Call: From, { fn create_transaction>( - call: MetaCall, + call: Call, _public: ::Signer, _account: AccountId, nonce: u64, - ) -> Option<(MetaCall, ::SignaturePayload)> { + ) -> Option<(Call, ::SignaturePayload)> { Some((call, (nonce, ()))) } } @@ -231,6 +215,6 @@ impl Config for Test { type AuthorityId = crypto::TestAuthId; - type Event = MetaEvent; - type Call = MetaCall; + type Event = Event; + type Call = Call; } diff --git a/frame/ddc-pallet b/frame/ddc-pallet index c995d5f429099..828ffb37473a7 160000 --- a/frame/ddc-pallet +++ b/frame/ddc-pallet @@ -1 +1 @@ -Subproject commit c995d5f429099b3261ebb670f91f32298e8cdb33 +Subproject commit 828ffb37473a7ed8d3ead8828d0c65cfd23e0386 diff --git a/frame/democracy/Cargo.toml b/frame/democracy/Cargo.toml index a633829225459..2e675dd25188e 100644 --- a/frame/democracy/Cargo.toml +++ b/frame/democracy/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-democracy" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,20 +14,20 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -pallet-balances = { version = "2.0.0", path = "../balances" } -pallet-scheduler = { version = "2.0.0", path = "../scheduler" } -sp-storage = { version = "2.0.0", path = "../../primitives/storage" } -substrate-test-utils = { version = "2.0.0", path = "../../test-utils" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-scheduler = { version = "3.0.0", path = "../scheduler" } +sp-storage = { version = "3.0.0", path = "../../primitives/storage" } +substrate-test-utils = { version = "3.0.0", path = "../../test-utils" } hex-literal = "0.3.1" [features] diff --git a/frame/democracy/src/benchmarking.rs b/frame/democracy/src/benchmarking.rs index c66ce20dab87c..57447944d22a7 100644 --- a/frame/democracy/src/benchmarking.rs +++ b/frame/democracy/src/benchmarking.rs @@ -19,7 +19,7 @@ use super::*; -use frame_benchmarking::{benchmarks, account, whitelist_account}; +use frame_benchmarking::{benchmarks, account, whitelist_account, impl_benchmark_test_suite}; use frame_support::{ IterableStorageMap, traits::{Currency, Get, EnsureOrigin, OnInitialize, UnfilteredDispatchable, schedule::DispatchTime}, @@ -781,44 +781,9 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_propose::()); - assert_ok!(test_benchmark_second::()); - assert_ok!(test_benchmark_vote_new::()); - assert_ok!(test_benchmark_vote_existing::()); - assert_ok!(test_benchmark_emergency_cancel::()); - assert_ok!(test_benchmark_external_propose::()); - assert_ok!(test_benchmark_external_propose_majority::()); - assert_ok!(test_benchmark_external_propose_default::()); - assert_ok!(test_benchmark_fast_track::()); - assert_ok!(test_benchmark_veto_external::()); - assert_ok!(test_benchmark_cancel_referendum::()); - assert_ok!(test_benchmark_cancel_queued::()); - assert_ok!(test_benchmark_on_initialize_external::()); - assert_ok!(test_benchmark_on_initialize_public::()); - assert_ok!(test_benchmark_on_initialize_base::()); - assert_ok!(test_benchmark_delegate::()); - assert_ok!(test_benchmark_undelegate::()); - assert_ok!(test_benchmark_clear_public_proposals::()); - assert_ok!(test_benchmark_note_preimage::()); - assert_ok!(test_benchmark_note_imminent_preimage::()); - assert_ok!(test_benchmark_reap_preimage::()); - assert_ok!(test_benchmark_unlock_remove::()); - assert_ok!(test_benchmark_unlock_set::()); - assert_ok!(test_benchmark_remove_vote::()); - assert_ok!(test_benchmark_remove_other_vote::()); - assert_ok!(test_benchmark_enact_proposal_execute::()); - assert_ok!(test_benchmark_enact_proposal_slash::()); - assert_ok!(test_benchmark_blacklist::()); - assert_ok!(test_benchmark_cancel_proposal::()); - }); - } -} + +impl_benchmark_test_suite!( + Democracy, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/democracy/src/tests.rs b/frame/democracy/src/tests.rs index 5927f1dcdd85f..99f413b389284 100644 --- a/frame/democracy/src/tests.rs +++ b/frame/democracy/src/tests.rs @@ -17,11 +17,12 @@ //! The crate's tests. +use crate as pallet_democracy; use super::*; use codec::Encode; use frame_support::{ - impl_outer_origin, impl_outer_dispatch, assert_noop, assert_ok, parameter_types, - impl_outer_event, ord_parameter_types, traits::{Contains, OnInitialize, Filter}, + assert_noop, assert_ok, parameter_types, ord_parameter_types, + traits::{Contains, OnInitialize, Filter}, weights::Weight, }; use sp_core::H256; @@ -50,30 +51,21 @@ const BIG_NAY: Vote = Vote { aye: false, conviction: Conviction::Locked1x }; const MAX_PROPOSALS: u32 = 100; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} - -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - frame_system::System, - pallet_balances::Balances, - democracy::Democracy, +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Scheduler: pallet_scheduler::{Module, Call, Storage, Config, Event}, + Democracy: pallet_democracy::{Module, Call, Storage, Config, Event}, } -} - -mod democracy { - pub use crate::Event; -} - -impl_outer_event! { - pub enum Event for Test { - system, - pallet_balances, - pallet_scheduler, - democracy, - } -} +); // Test that a fitlered call can be dispatched. pub struct BaseFilter; @@ -83,9 +75,6 @@ impl Filter for BaseFilter { } } -// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. -#[derive(Clone, Eq, PartialEq, Debug)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -108,7 +97,7 @@ impl frame_system::Config for Test { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -169,7 +158,7 @@ impl Contains for OneToFive { fn add(_m: &u64) {} } -impl super::Config for Test { +impl Config for Test { type Proposal = Call; type Event = Event; type Currency = pallet_balances::Module; @@ -204,7 +193,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities { pallet_balances::GenesisConfig::{ balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)], }.assimilate_storage(&mut t).unwrap(); - GenesisConfig::default().assimilate_storage(&mut t).unwrap(); + pallet_democracy::GenesisConfig::default().assimilate_storage(&mut t).unwrap(); let mut ext = sp_io::TestExternalities::new(t); ext.execute_with(|| System::set_block_number(1)); ext @@ -216,11 +205,6 @@ pub fn new_test_ext_execute_with_cond(execute: impl FnOnce(bool) -> () + Clone) new_test_ext().execute_with(|| execute(true)); } -type System = frame_system::Module; -type Balances = pallet_balances::Module; -type Scheduler = pallet_scheduler::Module; -type Democracy = Module; - #[test] fn params_should_work() { new_test_ext().execute_with(|| { diff --git a/frame/democracy/src/vote.rs b/frame/democracy/src/vote.rs index fdf13b944d62e..5adc76f4ae00b 100644 --- a/frame/democracy/src/vote.rs +++ b/frame/democracy/src/vote.rs @@ -30,7 +30,7 @@ pub struct Vote { } impl Encode for Vote { - fn encode_to(&self, output: &mut T) { + fn encode_to(&self, output: &mut T) { output.push_byte(u8::from(self.conviction) | if self.aye { 0b1000_0000 } else { 0 }); } } diff --git a/frame/election-provider-multi-phase/Cargo.toml b/frame/election-provider-multi-phase/Cargo.toml new file mode 100644 index 0000000000000..e52093ce1354e --- /dev/null +++ b/frame/election-provider-multi-phase/Cargo.toml @@ -0,0 +1,66 @@ +[package] +name = "pallet-election-provider-multi-phase" +version = "3.0.0" +authors = ["Parity Technologies "] +edition = "2018" +license = "Apache-2.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" +description = "PALLET two phase election providers" +readme = "README.md" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +static_assertions = "1.1.0" +serde = { version = "1.0.101", optional = true } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } + +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } + +sp-io ={ version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-npos-elections = { version = "3.0.0", default-features = false, path = "../../primitives/npos-elections" } +sp-arithmetic = { version = "3.0.0", default-features = false, path = "../../primitives/arithmetic" } +sp-election-providers = { version = "3.0.0", default-features = false, path = "../../primitives/election-providers" } + +# Optional imports for benchmarking +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } +rand = { version = "0.7.3", default-features = false, optional = true, features = ["alloc", "small_rng"] } + +[dev-dependencies] +paste = "1.0.3" +parking_lot = "0.11.0" +rand = { version = "0.7.3" } +hex-literal = "0.3.1" +substrate-test-utils = { version = "3.0.0", path = "../../test-utils" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } +sp-election-providers = { version = "3.0.0", features = ["runtime-benchmarks"], path = "../../primitives/election-providers" } +pallet-balances = { version = "3.0.0", path = "../balances" } +frame-benchmarking = { path = "../benchmarking" } + +[features] +default = ["std"] +std = [ + "serde", + "codec/std", + + "frame-support/std", + "frame-system/std", + + "sp-io/std", + "sp-std/std", + "sp-runtime/std", + "sp-npos-elections/std", + "sp-arithmetic/std", + "sp-election-providers/std", +] +runtime-benchmarks = [ + "frame-benchmarking", + "rand", +] diff --git a/frame/election-provider-multi-phase/src/benchmarking.rs b/frame/election-provider-multi-phase/src/benchmarking.rs new file mode 100644 index 0000000000000..74db28c6e3929 --- /dev/null +++ b/frame/election-provider-multi-phase/src/benchmarking.rs @@ -0,0 +1,282 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Two phase election pallet benchmarking. + +use super::*; +use crate::Module as MultiPhase; + +pub use frame_benchmarking::{account, benchmarks, whitelist_account, whitelisted_caller}; +use frame_support::{assert_ok, traits::OnInitialize}; +use frame_system::RawOrigin; +use rand::{prelude::SliceRandom, rngs::SmallRng, SeedableRng}; +use sp_election_providers::Assignment; +use sp_arithmetic::traits::One; +use sp_runtime::InnerOf; +use sp_std::convert::TryInto; + +const SEED: u32 = 0; + +/// Creates a **valid** solution with exactly the given size. +/// +/// The snapshot is also created internally. +fn solution_with_size( + size: SolutionOrSnapshotSize, + active_voters_count: u32, + desired_targets: u32, +) -> RawSolution> { + assert!(size.targets >= desired_targets, "must have enough targets"); + assert!( + size.targets >= (>::LIMIT * 2) as u32, + "must have enough targets for unique votes." + ); + assert!(size.voters >= active_voters_count, "must have enough voters"); + assert!( + (>::LIMIT as u32) < desired_targets, + "must have enough winners to give them votes." + ); + + let ed: VoteWeight = T::Currency::minimum_balance().saturated_into::(); + let stake: VoteWeight = ed.max(One::one()).saturating_mul(100); + + // first generates random targets. + let targets: Vec = + (0..size.targets).map(|i| account("Targets", i, SEED)).collect(); + + let mut rng = SmallRng::seed_from_u64(999u64); + + // decide who are the winners. + let winners = targets + .as_slice() + .choose_multiple(&mut rng, desired_targets as usize) + .cloned() + .collect::>(); + + // first generate active voters who must vote for a subset of winners. + let active_voters = (0..active_voters_count) + .map(|i| { + // chose a random subset of winners. + let winner_votes = winners + .as_slice() + .choose_multiple(&mut rng, >::LIMIT) + .cloned() + .collect::>(); + let voter = account::("Voter", i, SEED); + (voter, stake, winner_votes) + }) + .collect::>(); + + // rest of the voters. They can only vote for non-winners. + let non_winners = + targets.iter().filter(|t| !winners.contains(t)).cloned().collect::>(); + let rest_voters = (active_voters_count..size.voters) + .map(|i| { + let votes = (&non_winners) + .choose_multiple(&mut rng, >::LIMIT) + .cloned() + .collect::>(); + let voter = account::("Voter", i, SEED); + (voter, stake, votes) + }) + .collect::>(); + + let mut all_voters = active_voters.clone(); + all_voters.extend(rest_voters); + all_voters.shuffle(&mut rng); + + assert_eq!(active_voters.len() as u32, active_voters_count); + assert_eq!(all_voters.len() as u32, size.voters); + assert_eq!(winners.len() as u32, desired_targets); + + >::put(SolutionOrSnapshotSize { + voters: all_voters.len() as u32, + targets: targets.len() as u32, + }); + >::put(desired_targets); + >::put(RoundSnapshot { voters: all_voters.clone(), targets: targets.clone() }); + + // write the snapshot to staking or whoever is the data provider. + T::DataProvider::put_snapshot(all_voters.clone(), targets.clone()); + + let cache = helpers::generate_voter_cache::(&all_voters); + let stake_of = helpers::stake_of_fn::(&all_voters, &cache); + let voter_index = helpers::voter_index_fn::(&cache); + let target_index = helpers::target_index_fn_linear::(&targets); + let voter_at = helpers::voter_at_fn::(&all_voters); + let target_at = helpers::target_at_fn::(&targets); + + let assignments = active_voters + .iter() + .map(|(voter, _stake, votes)| { + let percent_per_edge: InnerOf> = + (100 / votes.len()).try_into().unwrap_or_else(|_| panic!("failed to convert")); + Assignment { + who: voter.clone(), + distribution: votes + .iter() + .map(|t| (t.clone(), >::from_percent(percent_per_edge))) + .collect::>(), + } + }) + .collect::>(); + + let compact = + >::from_assignment(assignments, &voter_index, &target_index).unwrap(); + let score = compact.clone().score(&winners, stake_of, voter_at, target_at).unwrap(); + let round = >::round(); + RawSolution { compact, score, round } +} + +benchmarks! { + on_initialize_nothing { + assert!(>::current_phase().is_off()); + }: { + >::on_initialize(1u32.into()); + } verify { + assert!(>::current_phase().is_off()); + } + + on_initialize_open_signed { + // NOTE: this benchmark currently doesn't have any components because the length of a db + // read/write is not captured. Otherwise, it is quite influenced by how much data + // `T::ElectionDataProvider` is reading and passing on. + assert!(>::snapshot().is_none()); + assert!(>::current_phase().is_off()); + }: { + >::on_initialize_open_signed(); + } verify { + assert!(>::snapshot().is_some()); + assert!(>::current_phase().is_signed()); + } + + on_initialize_open_unsigned_with_snapshot { + assert!(>::snapshot().is_none()); + assert!(>::current_phase().is_off()); + }: { + >::on_initialize_open_unsigned(true, true, 1u32.into()); + } verify { + assert!(>::snapshot().is_some()); + assert!(>::current_phase().is_unsigned()); + } + + on_initialize_open_unsigned_without_snapshot { + // need to assume signed phase was open before + >::on_initialize_open_signed(); + assert!(>::snapshot().is_some()); + assert!(>::current_phase().is_signed()); + }: { + >::on_initialize_open_unsigned(false, true, 1u32.into()); + } verify { + assert!(>::snapshot().is_some()); + assert!(>::current_phase().is_unsigned()); + } + + #[extra] + create_snapshot { + assert!(>::snapshot().is_none()); + }: { + >::create_snapshot() + } verify { + assert!(>::snapshot().is_some()); + } + + submit_unsigned { + // number of votes in snapshot. + let v in (T::BenchmarkingConfig::VOTERS[0]) .. T::BenchmarkingConfig::VOTERS[1]; + // number of targets in snapshot. + let t in (T::BenchmarkingConfig::TARGETS[0]) .. T::BenchmarkingConfig::TARGETS[1]; + // number of assignments, i.e. compact.len(). This means the active nominators, thus must be + // a subset of `v` component. + let a in (T::BenchmarkingConfig::ACTIVE_VOTERS[0]) .. T::BenchmarkingConfig::ACTIVE_VOTERS[1]; + // number of desired targets. Must be a subset of `t` component. + let d in (T::BenchmarkingConfig::DESIRED_TARGETS[0]) .. T::BenchmarkingConfig::DESIRED_TARGETS[1]; + + let witness = SolutionOrSnapshotSize { voters: v, targets: t }; + let raw_solution = solution_with_size::(witness, a, d); + + assert!(>::queued_solution().is_none()); + >::put(Phase::Unsigned((true, 1u32.into()))); + + // encode the most significant storage item that needs to be decoded in the dispatch. + let encoded_snapshot = >::snapshot().unwrap().encode(); + let encoded_call = >::submit_unsigned(raw_solution.clone(), witness).encode(); + }: { + assert_ok!(>::submit_unsigned(RawOrigin::None.into(), raw_solution, witness)); + let _decoded_snap = as Decode>::decode(&mut &*encoded_snapshot).unwrap(); + let _decoded_call = as Decode>::decode(&mut &*encoded_call).unwrap(); + } verify { + assert!(>::queued_solution().is_some()); + } + + // This is checking a valid solution. The worse case is indeed a valid solution. + feasibility_check { + // number of votes in snapshot. + let v in (T::BenchmarkingConfig::VOTERS[0]) .. T::BenchmarkingConfig::VOTERS[1]; + // number of targets in snapshot. + let t in (T::BenchmarkingConfig::TARGETS[0]) .. T::BenchmarkingConfig::TARGETS[1]; + // number of assignments, i.e. compact.len(). This means the active nominators, thus must be + // a subset of `v` component. + let a in (T::BenchmarkingConfig::ACTIVE_VOTERS[0]) .. T::BenchmarkingConfig::ACTIVE_VOTERS[1]; + // number of desired targets. Must be a subset of `t` component. + let d in (T::BenchmarkingConfig::DESIRED_TARGETS[0]) .. T::BenchmarkingConfig::DESIRED_TARGETS[1]; + + let size = SolutionOrSnapshotSize { voters: v, targets: t }; + let raw_solution = solution_with_size::(size, a, d); + + assert_eq!(raw_solution.compact.voter_count() as u32, a); + assert_eq!(raw_solution.compact.unique_targets().len() as u32, d); + + // encode the most significant storage item that needs to be decoded in the dispatch. + let encoded_snapshot = >::snapshot().unwrap().encode(); + }: { + assert_ok!(>::feasibility_check(raw_solution, ElectionCompute::Unsigned)); + let _decoded_snap = as Decode>::decode(&mut &*encoded_snapshot).unwrap(); + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::mock::*; + + #[test] + fn test_benchmarks() { + ExtBuilder::default().build_and_execute(|| { + assert_ok!(test_benchmark_feasibility_check::()); + }); + + ExtBuilder::default().build_and_execute(|| { + assert_ok!(test_benchmark_submit_unsigned::()); + }); + + ExtBuilder::default().build_and_execute(|| { + assert_ok!(test_benchmark_on_initialize_open_unsigned_with_snapshot::()); + }); + + ExtBuilder::default().build_and_execute(|| { + assert_ok!(test_benchmark_on_initialize_open_unsigned_without_snapshot::()); + }); + + ExtBuilder::default().build_and_execute(|| { + assert_ok!(test_benchmark_on_initialize_nothing::()); + }); + + ExtBuilder::default().build_and_execute(|| { + assert_ok!(test_benchmark_create_snapshot::()); + }); + } +} diff --git a/frame/election-provider-multi-phase/src/helpers.rs b/frame/election-provider-multi-phase/src/helpers.rs new file mode 100644 index 0000000000000..be074594e6603 --- /dev/null +++ b/frame/election-provider-multi-phase/src/helpers.rs @@ -0,0 +1,159 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Some helper functions/macros for this crate. + +use super::{Config, VoteWeight, CompactVoterIndexOf, CompactTargetIndexOf}; +use sp_std::{collections::btree_map::BTreeMap, convert::TryInto, boxed::Box, prelude::*}; + +#[macro_export] +macro_rules! log { + ($level:tt, $patter:expr $(, $values:expr)* $(,)?) => { + frame_support::debug::$level!( + target: $crate::LOG_TARGET, + concat!("🗳 ", $patter) $(, $values)* + ) + }; +} + +/// Generate a btree-map cache of the voters and their indices. +/// +/// This can be used to efficiently build index getter closures. +pub fn generate_voter_cache( + snapshot: &Vec<(T::AccountId, VoteWeight, Vec)>, +) -> BTreeMap { + let mut cache: BTreeMap = BTreeMap::new(); + snapshot.iter().enumerate().for_each(|(i, (x, _, _))| { + let _existed = cache.insert(x.clone(), i); + // if a duplicate exists, we only consider the last one. Defensive only, should never + // happen. + debug_assert!(_existed.is_none()); + }); + + cache +} + +/// Create a function the returns the index a voter in the snapshot. +/// +/// The returning index type is the same as the one defined in [`T::CompactSolution::Voter`]. +/// +/// ## Warning +/// +/// The snapshot must be the same is the one used to create `cache`. +pub fn voter_index_fn( + cache: &BTreeMap, +) -> Box Option> + '_> { + Box::new(move |who| { + cache.get(who).and_then(|i| >>::try_into(*i).ok()) + }) +} + +/// Same as [`voter_index_fn`], but the returning index is converted into usize, if possible. +/// +/// ## Warning +/// +/// The snapshot must be the same is the one used to create `cache`. +pub fn voter_index_fn_usize( + cache: &BTreeMap, +) -> Box Option + '_> { + Box::new(move |who| cache.get(who).cloned()) +} + +/// A non-optimized, linear version of [`voter_index_fn`] that does not need a cache and does a +/// linear search. +/// +/// ## Warning +/// +/// Not meant to be used in production. +pub fn voter_index_fn_linear( + snapshot: &Vec<(T::AccountId, VoteWeight, Vec)>, +) -> Box Option> + '_> { + Box::new(move |who| { + snapshot + .iter() + .position(|(x, _, _)| x == who) + .and_then(|i| >>::try_into(i).ok()) + }) +} + +/// Create a function the returns the index a targets in the snapshot. +/// +/// The returning index type is the same as the one defined in [`T::CompactSolution::Target`]. +pub fn target_index_fn_linear( + snapshot: &Vec, +) -> Box Option> + '_> { + Box::new(move |who| { + snapshot + .iter() + .position(|x| x == who) + .and_then(|i| >>::try_into(i).ok()) + }) +} + +/// Create a function that can map a voter index ([`CompactVoterIndexOf`]) to the actual voter +/// account using a linearly indexible snapshot. +pub fn voter_at_fn( + snapshot: &Vec<(T::AccountId, VoteWeight, Vec)>, +) -> Box) -> Option + '_> { + Box::new(move |i| { + as TryInto>::try_into(i) + .ok() + .and_then(|i| snapshot.get(i).map(|(x, _, _)| x).cloned()) + }) +} + +/// Create a function that can map a target index ([`CompactTargetIndexOf`]) to the actual target +/// account using a linearly indexible snapshot. +pub fn target_at_fn( + snapshot: &Vec, +) -> Box) -> Option + '_> { + Box::new(move |i| { + as TryInto>::try_into(i) + .ok() + .and_then(|i| snapshot.get(i).cloned()) + }) +} + +/// Create a function to get the stake of a voter. +/// +/// This is not optimized and uses a linear search. +pub fn stake_of_fn_linear( + snapshot: &Vec<(T::AccountId, VoteWeight, Vec)>, +) -> Box VoteWeight + '_> { + Box::new(move |who| { + snapshot.iter().find(|(x, _, _)| x == who).map(|(_, x, _)| *x).unwrap_or_default() + }) +} + +/// Create a function to get the stake of a voter. +/// +/// ## Warning +/// +/// The cache need must be derived from the same snapshot. Zero is returned if a voter is +/// non-existent. +pub fn stake_of_fn<'a, T: Config>( + snapshot: &'a Vec<(T::AccountId, VoteWeight, Vec)>, + cache: &'a BTreeMap, +) -> Box VoteWeight + 'a> { + Box::new(move |who| { + if let Some(index) = cache.get(who) { + snapshot.get(*index).map(|(_, x, _)| x).cloned().unwrap_or_default() + } else { + 0 + } + }) +} diff --git a/frame/election-provider-multi-phase/src/lib.rs b/frame/election-provider-multi-phase/src/lib.rs new file mode 100644 index 0000000000000..c4a5e0fa6936a --- /dev/null +++ b/frame/election-provider-multi-phase/src/lib.rs @@ -0,0 +1,1457 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! # Multi phase, offchain election provider pallet. +//! +//! Currently, this election-provider has two distinct phases (see [`Phase`]), **signed** and +//! **unsigned**. +//! +//! ## Phases +//! +//! The timeline of pallet is as follows. At each block, +//! [`sp_election_providers::ElectionDataProvider::next_election_prediction`] is used to estimate +//! the time remaining to the next call to [`sp_election_providers::ElectionProvider::elect`]. Based +//! on this, a phase is chosen. The timeline is as follows. +//! +//! ```ignore +//! elect() +//! + <--T::SignedPhase--> + <--T::UnsignedPhase--> + +//! +-------------------------------------------------------------------+ +//! Phase::Off + Phase::Signed + Phase::Unsigned + +//! ``` +//! +//! Note that the unsigned phase starts [`pallet::Config::UnsignedPhase`] blocks before the +//! `next_election_prediction`, but only ends when a call to [`ElectionProvider::elect`] happens. If +//! no `elect` happens, the signed phase is extended. +//! +//! > Given this, it is rather important for the user of this pallet to ensure it always terminates +//! election via `elect` before requesting a new one. +//! +//! Each of the phases can be disabled by essentially setting their length to zero. If both phases +//! have length zero, then the pallet essentially runs only the fallback strategy, denoted by +//! [`Config::FallbackStrategy`]. +//! ### Signed Phase +//! +//! In the signed phase, solutions (of type [`RawSolution`]) are submitted and queued on chain. A +//! deposit is reserved, based on the size of the solution, for the cost of keeping this solution +//! on-chain for a number of blocks, and the potential weight of the solution upon being checked. A +//! maximum of [`pallet::Config::MaxSignedSubmissions`] solutions are stored. The queue is always +//! sorted based on score (worse to best). +//! +//! Upon arrival of a new solution: +//! +//! 1. If the queue is not full, it is stored in the appropriate sorted index. +//! 2. If the queue is full but the submitted solution is better than one of the queued ones, the +//! worse solution is discarded, the bond of the outgoing solution is returned, and the new +//! solution is stored in the correct index. +//! 3. If the queue is full and the solution is not an improvement compared to any of the queued +//! ones, it is instantly rejected and no additional bond is reserved. +//! +//! A signed solution cannot be reversed, taken back, updated, or retracted. In other words, the +//! origin can not bail out in any way, if their solution is queued. +//! +//! Upon the end of the signed phase, the solutions are examined from best to worse (i.e. `pop()`ed +//! until drained). Each solution undergoes an expensive [`Pallet::feasibility_check`], which +//! ensures the score claimed by this score was correct, and it is valid based on the election data +//! (i.e. votes and candidates). At each step, if the current best solution passes the feasibility +//! check, it is considered to be the best one. The sender of the origin is rewarded, and the rest +//! of the queued solutions get their deposit back and are discarded, without being checked. +//! +//! The following example covers all of the cases at the end of the signed phase: +//! +//! ```ignore +//! Queue +//! +-------------------------------+ +//! |Solution(score=20, valid=false)| +--> Slashed +//! +-------------------------------+ +//! |Solution(score=15, valid=true )| +--> Rewarded, Saved +//! +-------------------------------+ +//! |Solution(score=10, valid=true )| +--> Discarded +//! +-------------------------------+ +//! |Solution(score=05, valid=false)| +--> Discarded +//! +-------------------------------+ +//! | None | +//! +-------------------------------+ +//! ``` +//! +//! Note that both of the bottom solutions end up being discarded and get their deposit back, +//! despite one of them being *invalid*. +//! +//! ## Unsigned Phase +//! +//! The unsigned phase will always follow the signed phase, with the specified duration. In this +//! phase, only validator nodes can submit solutions. A validator node who has offchain workers +//! enabled will start to mine a solution in this phase and submits it back to the chain as an +//! unsigned transaction, thus the name _unsigned_ phase. This unsigned transaction can never be +//! valid if propagated, and it acts similar to an inherent. +//! +//! Validators will only submit solutions if the one that they have computed is sufficiently better +//! than the best queued one (see [`pallet::Config::SolutionImprovementThreshold`]) and will limit +//! the weigh of the solution to [`pallet::Config::MinerMaxWeight`]. +//! +//! The unsigned phase can be made passive depending on how the previous signed phase went, by +//! setting the first inner value of [`Phase`] to `false`. For now, the signed phase is always +//! active. +//! +//! ### Fallback +//! +//! If we reach the end of both phases (i.e. call to [`ElectionProvider::elect`] happens) and no +//! good solution is queued, then the fallback strategy [`pallet::Config::Fallback`] is used to +//! determine what needs to be done. The on-chain election is slow, and contains no balancing or +//! reduction post-processing. See [`onchain::OnChainSequentialPhragmen`]. The +//! [`FallbackStrategy::Nothing`] should probably only be used for testing, and returns an error. +//! +//! ## Feasible Solution (correct solution) +//! +//! All submissions must undergo a feasibility check. Signed solutions are checked on by one at the +//! end of the signed phase, and the unsigned solutions are checked on the spot. A feasible solution +//! is as follows: +//! +//! 0. **all** of the used indices must be correct. +//! 1. present *exactly* correct number of winners. +//! 2. any assignment is checked to match with [`RoundSnapshot::voters`]. +//! 3. the claimed score is valid, based on the fixed point arithmetic accuracy. +//! +//! ## Accuracy +//! +//! The accuracy of the election is configured via two trait parameters. namely, +//! [`OnChainAccuracyOf`] dictates the accuracy used to compute the on-chain fallback election and +//! [`CompactAccuracyOf`] is the accuracy that the submitted solutions must adhere to. +//! +//! Note that both accuracies are of great importance. The offchain solution should be as small as +//! possible, reducing solutions size/weight. The on-chain solution can use more space for accuracy, +//! but should still be fast to prevent massively large blocks in case of a fallback. +//! +//! ## Error types +//! +//! This pallet provides a verbose error system to ease future debugging and debugging. The +//! overall hierarchy of errors is as follows: +//! +//! 1. [`pallet::Error`]: These are the errors that can be returned in the dispatchables of the +//! pallet, either signed or unsigned. Since decomposition with nested enums is not possible +//! here, they are prefixed with the logical sub-system to which they belong. +//! 2. [`ElectionError`]: These are the errors that can be generated while the pallet is doing +//! something in automatic scenarios, such as `offchain_worker` or `on_initialize`. These errors +//! are helpful for logging and are thus nested as: +//! - [`ElectionError::Miner`]: wraps a [`unsigned::MinerError`]. +//! - [`ElectionError::Feasibility`]: wraps a [`FeasibilityError`]. +//! - [`ElectionError::OnChainFallback`]: wraps a [`sp_election_providers::onchain::Error`]. +//! +//! Note that there could be an overlap between these sub-errors. For example, A +//! `SnapshotUnavailable` can happen in both miner and feasibility check phase. +//! +//! ## Future Plans +//! +//! **Challenge Phase**. We plan adding a third phase to the pallet, called the challenge phase. +//! This is phase in which no further solutions are processed, and the current best solution might +//! be challenged by anyone (signed or unsigned). The main plan here is to enforce the solution to +//! be PJR. Checking PJR on-chain is quite expensive, yet proving that a solution is **not** PJR is +//! rather cheap. If a queued solution is challenged: +//! +//! 1. We must surely slash whoever submitted that solution (might be a challenge for unsigned +//! solutions). +//! 2. It is probably fine to fallback to the on-chain election, as we expect this to happen rarely. +//! +//! **Bailing out**. The functionality of bailing out of a queued solution is nice. A miner can +//! submit a solution as soon as they _think_ it is high probability feasible, and do the checks +//! afterwards, and remove their solution (for a small cost of probably just transaction fees, or a +//! portion of the bond). +//! +//! **Conditionally open unsigned phase**: Currently, the unsigned phase is always opened. This is +//! useful because an honest validation will run our OCW code, which should be good enough to trump +//! a mediocre or malicious signed submission (assuming in the absence of honest signed bots). If an +//! when the signed submissions are checked against an absolute measure (e.g. PJR), then we can only +//! open the unsigned phase in extreme conditions (i.e. "not good signed solution received") to +//! spare some work in the validators +//! +//! **Allow smaller solutions and build up**: For now we only allow solutions that are exactly +//! [`DesiredTargets`], no more, no less. Over time, we can change this to a [min, max] where any +//! solution within this range is acceptable, where bigger solutions are prioritized. +//! +//! **Recursive Fallback**: Currently, the fallback is a separate enum. A different and fancier way +//! of doing this would be to have the fallback be another +//! [`sp_election_providers::ElectionProvider`]. In this case, this pallet can even have the +//! on-chain election provider as fallback, or special _noop_ fallback that simply returns an error, +//! thus replicating [`FallbackStrategy::Nothing`]. In this case, we won't need the additional +//! config OnChainAccuracy either. +//! +//! **Score based on (byte) size**: We should always prioritize small solutions over bigger ones, if +//! there is a tie. Even more harsh should be to enforce the bound of the `reduce` algorithm. +//! +//! **Offchain resubmit**: Essentially port https://github.com/paritytech/substrate/pull/7976 to +//! this pallet as well. The `OFFCHAIN_REPEAT` also needs to become an adjustable parameter of the +//! pallet. +//! +//! **Make the number of nominators configurable from the runtime**. Remove `sp_npos_elections` +//! dependency from staking and the compact solution type. It should be generated at runtime, there +//! it should be encoded how many votes each nominators have. Essentially translate +//! https://github.com/paritytech/substrate/pull/7929 to this pallet. + +#![cfg_attr(not(feature = "std"), no_std)] + +use codec::{Decode, Encode}; +use frame_support::{ + dispatch::DispatchResultWithPostInfo, + ensure, + traits::{Currency, Get, ReservableCurrency}, + weights::Weight, +}; +use frame_system::{ensure_none, offchain::SendTransactionTypes}; +use sp_election_providers::{ElectionDataProvider, ElectionProvider, onchain}; +use sp_npos_elections::{ + assignment_ratio_to_staked_normalized, is_score_better, CompactSolution, ElectionScore, + EvaluateSupport, PerThing128, Supports, VoteWeight, +}; +use sp_runtime::{ + transaction_validity::{ + InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity, + TransactionValidityError, ValidTransaction, + }, + DispatchError, PerThing, Perbill, RuntimeDebug, SaturatedConversion, +}; +use sp_std::prelude::*; +use sp_arithmetic::{ + UpperOf, + traits::{Zero, CheckedAdd}, +}; + +#[cfg(any(feature = "runtime-benchmarks", test))] +mod benchmarking; +#[cfg(test)] +mod mock; +#[macro_use] +pub mod helpers; + +const LOG_TARGET: &'static str = "runtime::election-provider"; + +pub mod unsigned; +pub mod weights; + +/// The weight declaration of the pallet. +pub use weights::WeightInfo; + +/// The compact solution type used by this crate. +pub type CompactOf = ::CompactSolution; + +/// The voter index. Derived from [`CompactOf`]. +pub type CompactVoterIndexOf = as CompactSolution>::Voter; +/// The target index. Derived from [`CompactOf`]. +pub type CompactTargetIndexOf = as CompactSolution>::Target; +/// The accuracy of the election, when submitted from offchain. Derived from [`CompactOf`]. +pub type CompactAccuracyOf = as CompactSolution>::Accuracy; +/// The accuracy of the election, when computed on-chain. Equal to [`Config::OnChainAccuracy`]. +pub type OnChainAccuracyOf = ::OnChainAccuracy; + +/// Wrapper type that implements the configurations needed for the on-chain backup. +struct OnChainConfig(sp_std::marker::PhantomData); +impl onchain::Config for OnChainConfig { + type AccountId = T::AccountId; + type BlockNumber = T::BlockNumber; + type Accuracy = T::OnChainAccuracy; + type DataProvider = T::DataProvider; +} + +/// Configuration for the benchmarks of the pallet. +pub trait BenchmarkingConfig { + /// Range of voters. + const VOTERS: [u32; 2]; + /// Range of targets. + const TARGETS: [u32; 2]; + /// Range of active voters. + const ACTIVE_VOTERS: [u32; 2]; + /// Range of desired targets. + const DESIRED_TARGETS: [u32; 2]; +} + +impl BenchmarkingConfig for () { + const VOTERS: [u32; 2] = [4000, 6000]; + const TARGETS: [u32; 2] = [1000, 1600]; + const ACTIVE_VOTERS: [u32; 2] = [1000, 3000]; + const DESIRED_TARGETS: [u32; 2] = [400, 800]; +} + +/// Current phase of the pallet. +#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, RuntimeDebug)] +pub enum Phase { + /// Nothing, the election is not happening. + Off, + /// Signed phase is open. + Signed, + /// Unsigned phase. First element is whether it is open or not, second the starting block + /// number. + Unsigned((bool, Bn)), +} + +impl Default for Phase { + fn default() -> Self { + Phase::Off + } +} + +impl Phase { + /// Weather the phase is signed or not. + pub fn is_signed(&self) -> bool { + matches!(self, Phase::Signed) + } + + /// Weather the phase is unsigned or not. + pub fn is_unsigned(&self) -> bool { + matches!(self, Phase::Unsigned(_)) + } + + /// Weather the phase is unsigned and open or not, with specific start. + pub fn is_unsigned_open_at(&self, at: Bn) -> bool { + matches!(self, Phase::Unsigned((true, real)) if *real == at) + } + + /// Weather the phase is unsigned and open or not. + pub fn is_unsigned_open(&self) -> bool { + matches!(self, Phase::Unsigned((true, _))) + } + + /// Weather the phase is off or not. + pub fn is_off(&self) -> bool { + matches!(self, Phase::Off) + } +} + +/// A configuration for the pallet to indicate what should happen in the case of a fallback i.e. +/// reaching a call to `elect` with no good solution. +#[cfg_attr(test, derive(Clone))] +pub enum FallbackStrategy { + /// Run a on-chain sequential phragmen. + /// + /// This might burn the chain for a few minutes due to a stall, but is generally a safe + /// approach to maintain a sensible validator set. + OnChain, + /// Nothing. Return an error. + Nothing, +} + +/// The type of `Computation` that provided this election data. +#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, RuntimeDebug)] +pub enum ElectionCompute { + /// Election was computed on-chain. + OnChain, + /// Election was computed with a signed submission. + Signed, + /// Election was computed with an unsigned submission. + Unsigned, +} + +impl Default for ElectionCompute { + fn default() -> Self { + ElectionCompute::OnChain + } +} + +/// A raw, unchecked solution. +/// +/// This is what will get submitted to the chain. +/// +/// Such a solution should never become effective in anyway before being checked by the +/// [`Pallet::feasibility_check`] +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)] +pub struct RawSolution { + /// Compact election edges. + compact: C, + /// The _claimed_ score of the solution. + score: ElectionScore, + /// The round at which this solution should be submitted. + round: u32, +} + +impl Default for RawSolution { + fn default() -> Self { + // Round 0 is always invalid, only set this to 1. + Self { round: 1, compact: Default::default(), score: Default::default() } + } +} + +/// A checked solution, ready to be enacted. +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, Default)] +pub struct ReadySolution { + /// The final supports of the solution. + /// + /// This is target-major vector, storing each winners, total backing, and each individual + /// backer. + supports: Supports, + /// The score of the solution. + /// + /// This is needed to potentially challenge the solution. + score: ElectionScore, + /// How this election was computed. + compute: ElectionCompute, +} + +/// A snapshot of all the data that is needed for en entire round. They are provided by +/// [`ElectionDataProvider`] and are kept around until the round is finished. +/// +/// These are stored together because they are often accessed together. +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, Default)] +pub struct RoundSnapshot { + /// All of the voters. + pub voters: Vec<(A, VoteWeight, Vec)>, + /// All of the targets. + pub targets: Vec, +} + +/// Encodes the length of a solution or a snapshot. +/// +/// This is stored automatically on-chain, and it contains the **size of the entire snapshot**. +/// This is also used in dispatchables as weight witness data and should **only contain the size of +/// the presented solution**, not the entire snapshot. +#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, RuntimeDebug, Default)] +pub struct SolutionOrSnapshotSize { + /// The length of voters. + #[codec(compact)] + voters: u32, + /// The length of targets. + #[codec(compact)] + targets: u32, +} + +/// Internal errors of the pallet. +/// +/// Note that this is different from [`pallet::Error`]. +#[derive(Debug, Eq, PartialEq)] +pub enum ElectionError { + /// An error happened in the feasibility check sub-system. + Feasibility(FeasibilityError), + /// An error in the miner (offchain) sub-system. + Miner(unsigned::MinerError), + /// An error in the on-chain fallback. + OnChainFallback(onchain::Error), + /// No fallback is configured. This is a special case. + NoFallbackConfigured, +} + +impl From for ElectionError { + fn from(e: onchain::Error) -> Self { + ElectionError::OnChainFallback(e) + } +} + +impl From for ElectionError { + fn from(e: FeasibilityError) -> Self { + ElectionError::Feasibility(e) + } +} + +impl From for ElectionError { + fn from(e: unsigned::MinerError) -> Self { + ElectionError::Miner(e) + } +} + +/// Errors that can happen in the feasibility check. +#[derive(Debug, Eq, PartialEq)] +pub enum FeasibilityError { + /// Wrong number of winners presented. + WrongWinnerCount, + /// The snapshot is not available. + /// + /// Kinda defensive: The pallet should technically never attempt to do a feasibility check when + /// no snapshot is present. + SnapshotUnavailable, + /// Internal error from the election crate. + NposElection(sp_npos_elections::Error), + /// A vote is invalid. + InvalidVote, + /// A voter is invalid. + InvalidVoter, + /// A winner is invalid. + InvalidWinner, + /// The given score was invalid. + InvalidScore, + /// The provided round is incorrect. + InvalidRound, +} + +impl From for FeasibilityError { + fn from(e: sp_npos_elections::Error) -> Self { + FeasibilityError::NposElection(e) + } +} + +pub use pallet::*; +#[frame_support::pallet] +pub mod pallet { + use super::*; + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + + #[pallet::config] + pub trait Config: frame_system::Config + SendTransactionTypes> { + type Event: From> + IsType<::Event>; + + /// Currency type. + type Currency: ReservableCurrency + Currency; + + /// Duration of the unsigned phase. + #[pallet::constant] + type UnsignedPhase: Get; + /// Duration of the signed phase. + #[pallet::constant] + type SignedPhase: Get; + + /// The minimum amount of improvement to the solution score that defines a solution as + /// "better" (in any phase). + #[pallet::constant] + type SolutionImprovementThreshold: Get; + + /// The priority of the unsigned transaction submitted in the unsigned-phase + type MinerTxPriority: Get; + /// Maximum number of iteration of balancing that will be executed in the embedded miner of + /// the pallet. + type MinerMaxIterations: Get; + /// Maximum weight that the miner should consume. + /// + /// The miner will ensure that the total weight of the unsigned solution will not exceed + /// this values, based on [`WeightInfo::submit_unsigned`]. + type MinerMaxWeight: Get; + + /// Something that will provide the election data. + type DataProvider: ElectionDataProvider; + + /// The compact solution type + type CompactSolution: codec::Codec + + Default + + PartialEq + + Eq + + Clone + + sp_std::fmt::Debug + + CompactSolution; + + /// Accuracy used for fallback on-chain election. + type OnChainAccuracy: PerThing128; + + /// Configuration for the fallback + type Fallback: Get; + + /// The configuration of benchmarking. + type BenchmarkingConfig: BenchmarkingConfig; + + /// The weight of the pallet. + type WeightInfo: WeightInfo; + } + + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_initialize(now: T::BlockNumber) -> Weight { + let next_election = T::DataProvider::next_election_prediction(now).max(now); + + let signed_deadline = T::SignedPhase::get() + T::UnsignedPhase::get(); + let unsigned_deadline = T::UnsignedPhase::get(); + + let remaining = next_election - now; + let current_phase = Self::current_phase(); + + match current_phase { + Phase::Off if remaining <= signed_deadline && remaining > unsigned_deadline => { + Self::on_initialize_open_signed(); + log!(info, "Starting signed phase at #{:?} , round {}.", now, Self::round()); + T::WeightInfo::on_initialize_open_signed() + } + Phase::Signed | Phase::Off + if remaining <= unsigned_deadline && remaining > 0u32.into() => + { + let (need_snapshot, enabled, additional) = if current_phase == Phase::Signed { + // followed by a signed phase: close the signed phase, no need for snapshot. + // TWO_PHASE_NOTE: later on once we have signed phase, this should return + // something else. + (false, true, Weight::zero()) + } else { + // no signed phase: create a new snapshot, definitely `enable` the unsigned + // phase. + (true, true, Weight::zero()) + }; + + Self::on_initialize_open_unsigned(need_snapshot, enabled, now); + log!(info, "Starting unsigned phase({}) at #{:?}.", enabled, now); + + let base_weight = if need_snapshot { + T::WeightInfo::on_initialize_open_unsigned_with_snapshot() + } else { + T::WeightInfo::on_initialize_open_unsigned_without_snapshot() + }; + base_weight.saturating_add(additional) + } + _ => T::WeightInfo::on_initialize_nothing(), + } + } + + fn offchain_worker(n: T::BlockNumber) { + // We only run the OCW in the first block of the unsigned phase. + if Self::current_phase().is_unsigned_open_at(n) { + match Self::try_acquire_offchain_lock(n) { + Ok(_) => { + let outcome = Self::mine_check_and_submit().map_err(ElectionError::from); + log!(info, "miner exeuction done: {:?}", outcome); + } + Err(why) => log!(warn, "denied offchain worker: {:?}", why), + } + } + } + + fn integrity_test() { + use sp_std::mem::size_of; + // The index type of both voters and targets need to be smaller than that of usize (very + // unlikely to be the case, but anyhow). + assert!(size_of::>() <= size_of::()); + assert!(size_of::>() <= size_of::()); + + // ---------------------------- + // based on the requirements of [`sp_npos_elections::Assignment::try_normalize`]. + let max_vote: usize = as CompactSolution>::LIMIT; + + // 1. Maximum sum of [ChainAccuracy; 16] must fit into `UpperOf`.. + let maximum_chain_accuracy: Vec>> = (0..max_vote) + .map(|_| { + >>::from( + >::one().deconstruct(), + ) + }) + .collect(); + let _: UpperOf> = maximum_chain_accuracy + .iter() + .fold(Zero::zero(), |acc, x| acc.checked_add(x).unwrap()); + + // 2. Maximum sum of [CompactAccuracy; 16] must fit into `UpperOf`. + let maximum_chain_accuracy: Vec>> = (0..max_vote) + .map(|_| { + >>::from( + >::one().deconstruct(), + ) + }) + .collect(); + let _: UpperOf> = maximum_chain_accuracy + .iter() + .fold(Zero::zero(), |acc, x| acc.checked_add(x).unwrap()); + } + } + + #[pallet::call] + impl Pallet { + /// Submit a solution for the unsigned phase. + /// + /// The dispatch origin fo this call must be __none__. + /// + /// This submission is checked on the fly. Moreover, this unsigned solution is only + /// validated when submitted to the pool from the **local** node. Effectively, this means + /// that only active validators can submit this transaction when authoring a block (similar + /// to an inherent). + /// + /// To prevent any incorrect solution (and thus wasted time/weight), this transaction will + /// panic if the solution submitted by the validator is invalid in any way, effectively + /// putting their authoring reward at risk. + /// + /// No deposit or reward is associated with this submission. + #[pallet::weight(T::WeightInfo::submit_unsigned( + witness.voters, + witness.targets, + solution.compact.voter_count() as u32, + solution.compact.unique_targets().len() as u32 + ))] + pub fn submit_unsigned( + origin: OriginFor, + solution: RawSolution>, + witness: SolutionOrSnapshotSize, + ) -> DispatchResultWithPostInfo { + ensure_none(origin)?; + let error_message = "Invalid unsigned submission must produce invalid block and \ + deprive validator from their authoring reward."; + + // Check score being an improvement, phase, and desired targets. + Self::unsigned_pre_dispatch_checks(&solution).expect(error_message); + + // ensure witness was correct. + let SolutionOrSnapshotSize { voters, targets } = + Self::snapshot_metadata().expect(error_message); + + // NOTE: we are asserting, not `ensure`ing -- we want to panic here. + assert!(voters as u32 == witness.voters, error_message); + assert!(targets as u32 == witness.targets, error_message); + + let ready = + Self::feasibility_check(solution, ElectionCompute::Unsigned).expect(error_message); + + // store the newly received solution. + log!(info, "queued unsigned solution with score {:?}", ready.score); + >::put(ready); + Self::deposit_event(Event::SolutionStored(ElectionCompute::Unsigned)); + + Ok(None.into()) + } + } + + #[pallet::event] + #[pallet::metadata(::AccountId = "AccountId")] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + /// A solution was stored with the given compute. + /// + /// If the solution is signed, this means that it hasn't yet been processed. If the + /// solution is unsigned, this means that it has also been processed. + SolutionStored(ElectionCompute), + /// The election has been finalized, with `Some` of the given computation, or else if the + /// election failed, `None`. + ElectionFinalized(Option), + /// An account has been rewarded for their signed submission being finalized. + Rewarded(::AccountId), + /// An account has been slashed for submitting an invalid signed submission. + Slashed(::AccountId), + /// The signed phase of the given round has started. + SignedPhaseStarted(u32), + /// The unsigned phase of the given round has started. + UnsignedPhaseStarted(u32), + } + + /// Error of the pallet that can be returned in response to dispatches. + #[pallet::error] + pub enum Error { + /// Submission was too early. + PreDispatchEarlySubmission, + /// Wrong number of winners presented. + PreDispatchWrongWinnerCount, + /// Submission was too weak, score-wise. + PreDispatchWeakSubmission, + } + + #[pallet::origin] + pub struct Origin(PhantomData); + #[pallet::validate_unsigned] + impl ValidateUnsigned for Pallet { + type Call = Call; + fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity { + if let Call::submit_unsigned(solution, _) = call { + // discard solution not coming from the local OCW. + match source { + TransactionSource::Local | TransactionSource::InBlock => { /* allowed */ } + _ => { + return InvalidTransaction::Call.into(); + } + } + + let _ = Self::unsigned_pre_dispatch_checks(solution) + .map_err(|err| { + log!(error, "unsigned transaction validation failed due to {:?}", err); + err + }) + .map_err(dispatch_error_to_invalid)?; + + ValidTransaction::with_tag_prefix("OffchainElection") + // The higher the score[0], the better a solution is. + .priority( + T::MinerTxPriority::get().saturating_add( + solution.score[0].saturated_into() + ), + ) + // used to deduplicate unsigned solutions: each validator should produce one + // solution per round at most, and solutions are not propagate. + .and_provides(solution.round) + // transaction should stay in the pool for the duration of the unsigned phase. + .longevity(T::UnsignedPhase::get().saturated_into::()) + // We don't propagate this. This can never be validated at a remote node. + .propagate(false) + .build() + } else { + InvalidTransaction::Call.into() + } + } + + fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> { + if let Call::submit_unsigned(solution, _) = call { + Self::unsigned_pre_dispatch_checks(solution) + .map_err(dispatch_error_to_invalid) + .map_err(Into::into) + } else { + Err(InvalidTransaction::Call.into()) + } + } + } + + #[pallet::type_value] + pub fn DefaultForRound() -> u32 { + 1 + } + + /// Internal counter for the number of rounds. + /// + /// This is useful for de-duplication of transactions submitted to the pool, and general + /// diagnostics of the pallet. + /// + /// This is merely incremented once per every time that an upstream `elect` is called. + #[pallet::storage] + #[pallet::getter(fn round)] + pub type Round = StorageValue<_, u32, ValueQuery, DefaultForRound>; + + /// Current phase. + #[pallet::storage] + #[pallet::getter(fn current_phase)] + pub type CurrentPhase = StorageValue<_, Phase, ValueQuery>; + + /// Current best solution, signed or unsigned, queued to be returned upon `elect`. + #[pallet::storage] + #[pallet::getter(fn queued_solution)] + pub type QueuedSolution = StorageValue<_, ReadySolution>; + + /// Snapshot data of the round. + /// + /// This is created at the beginning of the signed phase and cleared upon calling `elect`. + #[pallet::storage] + #[pallet::getter(fn snapshot)] + pub type Snapshot = StorageValue<_, RoundSnapshot>; + + /// Desired number of targets to elect for this round. + /// + /// Only exists when [`Snapshot`] is present. + #[pallet::storage] + #[pallet::getter(fn desired_targets)] + pub type DesiredTargets = StorageValue<_, u32>; + + /// The metadata of the [`RoundSnapshot`] + /// + /// Only exists when [`Snapshot`] is present. + #[pallet::storage] + #[pallet::getter(fn snapshot_metadata)] + pub type SnapshotMetadata = StorageValue<_, SolutionOrSnapshotSize>; + + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(PhantomData); +} + +impl Pallet { + /// Logic for `::on_initialize` when signed phase is being opened. + /// + /// This is decoupled for easy weight calculation. + pub(crate) fn on_initialize_open_signed() { + >::put(Phase::Signed); + Self::create_snapshot(); + Self::deposit_event(Event::SignedPhaseStarted(Self::round())); + } + + /// Logic for `>::on_initialize` when unsigned phase is being opened. + /// + /// This is decoupled for easy weight calculation. Note that the default weight benchmark of + /// this function will assume an empty signed queue for `finalize_signed_phase`. + pub(crate) fn on_initialize_open_unsigned( + need_snapshot: bool, + enabled: bool, + now: T::BlockNumber, + ) { + if need_snapshot { + // if not being followed by a signed phase, then create the snapshots. + debug_assert!(Self::snapshot().is_none()); + Self::create_snapshot(); + } + + >::put(Phase::Unsigned((enabled, now))); + Self::deposit_event(Event::UnsignedPhaseStarted(Self::round())); + } + + /// Creates the snapshot. Writes new data to: + /// + /// 1. [`SnapshotMetadata`] + /// 2. [`RoundSnapshot`] + /// 3. [`DesiredTargets`] + pub(crate) fn create_snapshot() { + // if any of them don't exist, create all of them. This is a bit conservative. + let targets = T::DataProvider::targets(); + let voters = T::DataProvider::voters(); + let desired_targets = T::DataProvider::desired_targets(); + + >::put(SolutionOrSnapshotSize { + voters: voters.len() as u32, + targets: targets.len() as u32, + }); + >::put(desired_targets); + >::put(RoundSnapshot { voters, targets }); + } + + /// Kill everything created by [`Pallet::create_snapshot`]. + pub(crate) fn kill_snapshot() { + >::kill(); + >::kill(); + >::kill(); + } + + /// Checks the feasibility of a solution. + fn feasibility_check( + solution: RawSolution>, + compute: ElectionCompute, + ) -> Result, FeasibilityError> { + let RawSolution { compact, score, round } = solution; + + // first, check round. + ensure!(Self::round() == round, FeasibilityError::InvalidRound); + + // winners are not directly encoded in the solution. + let winners = compact.unique_targets(); + + let desired_targets = + Self::desired_targets().ok_or(FeasibilityError::SnapshotUnavailable)?; + + // NOTE: this is a bit of duplicate, but we keep it around for veracity. The unsigned path + // already checked this in `unsigned_per_dispatch_checks`. The signed path *could* check it + // upon arrival, thus we would then remove it here. Given overlay it is cheap anyhow + ensure!(winners.len() as u32 == desired_targets, FeasibilityError::WrongWinnerCount); + + // read the entire snapshot. + let RoundSnapshot { voters: snapshot_voters, targets: snapshot_targets } = + Self::snapshot().ok_or(FeasibilityError::SnapshotUnavailable)?; + + // ----- Start building. First, we need some closures. + let cache = helpers::generate_voter_cache::(&snapshot_voters); + let voter_at = helpers::voter_at_fn::(&snapshot_voters); + let target_at = helpers::target_at_fn::(&snapshot_targets); + let voter_index = helpers::voter_index_fn_usize::(&cache); + + // first, make sure that all the winners are sane. + // OPTIMIZATION: we could first build the assignments, and then extract the winners directly + // from that, as that would eliminate a little bit of duplicate work. For now, we keep them + // separate: First extract winners separately from compact, and then assignments. This is + // also better, because we can reject solutions that don't meet `desired_targets` early on. + let winners = winners + .into_iter() + .map(|i| target_at(i).ok_or(FeasibilityError::InvalidWinner)) + .collect::, FeasibilityError>>()?; + + // Then convert compact -> assignment. This will fail if any of the indices are gibberish. + let assignments = compact + .into_assignment(voter_at, target_at) + .map_err::(Into::into)?; + + // Ensure that assignments is correct. + let _ = assignments + .iter() + .map(|ref assignment| { + // check that assignment.who is actually a voter (defensive-only). + // NOTE: while using the index map from `voter_index` is better than a blind linear + // search, this *still* has room for optimization. Note that we had the index when + // we did `compact -> assignment` and we lost it. Ideal is to keep the index around. + + // defensive-only: must exist in the snapshot. + let snapshot_index = + voter_index(&assignment.who).ok_or(FeasibilityError::InvalidVoter)?; + // defensive-only: index comes from the snapshot, must exist. + let (_voter, _stake, targets) = + snapshot_voters.get(snapshot_index).ok_or(FeasibilityError::InvalidVoter)?; + + // check that all of the targets are valid based on the snapshot. + if assignment.distribution.iter().any(|(d, _)| !targets.contains(d)) { + return Err(FeasibilityError::InvalidVote); + } + Ok(()) + }) + .collect::>()?; + + // ----- Start building support. First, we need one more closure. + let stake_of = helpers::stake_of_fn::(&snapshot_voters, &cache); + + // This might fail if the normalization fails. Very unlikely. See `integrity_test`. + let staked_assignments = assignment_ratio_to_staked_normalized(assignments, stake_of) + .map_err::(Into::into)?; + + // This might fail if one of the voter edges is pointing to a non-winner, which is not + // really possible anymore because all the winners come from the same `compact`. + let supports = sp_npos_elections::to_supports(&winners, &staked_assignments) + .map_err::(Into::into)?; + + // Finally, check that the claimed score was indeed correct. + let known_score = (&supports).evaluate(); + ensure!(known_score == score, FeasibilityError::InvalidScore); + + Ok(ReadySolution { supports, compute, score }) + } + + /// Perform the tasks to be done after a new `elect` has been triggered: + /// + /// 1. Increment round. + /// 2. Change phase to [`Phase::Off`] + /// 3. Clear all snapshot data. + fn post_elect() { + // inc round + >::mutate(|r| *r = *r + 1); + + // change phase + >::put(Phase::Off); + + // kill snapshots + Self::kill_snapshot(); + } + + /// On-chain fallback of election. + fn onchain_fallback() -> Result, ElectionError> { + > as ElectionProvider< + T::AccountId, + T::BlockNumber, + >>::elect() + .map_err(Into::into) + } + + fn do_elect() -> Result, ElectionError> { + >::take() + .map_or_else( + || match T::Fallback::get() { + FallbackStrategy::OnChain => Self::onchain_fallback() + .map(|r| (r, ElectionCompute::OnChain)) + .map_err(Into::into), + FallbackStrategy::Nothing => Err(ElectionError::NoFallbackConfigured), + }, + |ReadySolution { supports, compute, .. }| Ok((supports, compute)), + ) + .map(|(supports, compute)| { + Self::deposit_event(Event::ElectionFinalized(Some(compute))); + log!(info, "Finalized election round with compute {:?}.", compute); + supports + }) + .map_err(|err| { + Self::deposit_event(Event::ElectionFinalized(None)); + log!(warn, "Failed to finalize election round. reason {:?}", err); + err + }) + } +} + +impl ElectionProvider for Pallet { + type Error = ElectionError; + type DataProvider = T::DataProvider; + + fn elect() -> Result, Self::Error> { + let outcome = Self::do_elect(); + Self::post_elect(); + outcome + } +} + +/// convert a DispatchError to a custom InvalidTransaction with the inner code being the error +/// number. +pub fn dispatch_error_to_invalid(error: DispatchError) -> InvalidTransaction { + let error_number = match error { + DispatchError::Module { error, .. } => error, + _ => 0, + }; + InvalidTransaction::Custom(error_number) +} + +#[cfg(test)] +mod feasibility_check { + //! All of the tests here should be dedicated to only testing the feasibility check and nothing + //! more. The best way to audit and review these tests is to try and come up with a solution + //! that is invalid, but gets through the system as valid. + + use super::{mock::*, *}; + + const COMPUTE: ElectionCompute = ElectionCompute::OnChain; + + #[test] + fn snapshot_is_there() { + ExtBuilder::default().build_and_execute(|| { + roll_to(::get() - ::get() - ::get()); + assert!(MultiPhase::current_phase().is_signed()); + let solution = raw_solution(); + + // for whatever reason it might be: + >::kill(); + + assert_noop!( + MultiPhase::feasibility_check(solution, COMPUTE), + FeasibilityError::SnapshotUnavailable + ); + }) + } + + #[test] + fn round() { + ExtBuilder::default().build_and_execute(|| { + roll_to(::get() - ::get() - ::get()); + assert!(MultiPhase::current_phase().is_signed()); + + let mut solution = raw_solution(); + solution.round += 1; + assert_noop!( + MultiPhase::feasibility_check(solution, COMPUTE), + FeasibilityError::InvalidRound + ); + }) + } + + #[test] + fn desired_targets() { + ExtBuilder::default().desired_targets(8).build_and_execute(|| { + roll_to(::get() - ::get() - ::get()); + assert!(MultiPhase::current_phase().is_signed()); + + let solution = raw_solution(); + + assert_eq!(solution.compact.unique_targets().len(), 4); + assert_eq!(MultiPhase::desired_targets().unwrap(), 8); + + assert_noop!( + MultiPhase::feasibility_check(solution, COMPUTE), + FeasibilityError::WrongWinnerCount, + ); + }) + } + + #[test] + fn winner_indices() { + ExtBuilder::default().desired_targets(2).build_and_execute(|| { + roll_to(::get() - ::get() - ::get()); + assert!(MultiPhase::current_phase().is_signed()); + + let mut solution = raw_solution(); + assert_eq!(MultiPhase::snapshot().unwrap().targets.len(), 4); + // ----------------------------------------------------^^ valid range is [0..3]. + + // swap all votes from 3 to 4. This will ensure that the number of unique winners + // will still be 4, but one of the indices will be gibberish. Requirement is to make + // sure 3 a winner, which we don't do here. + solution + .compact + .votes1 + .iter_mut() + .filter(|(_, t)| *t == 3u16) + .for_each(|(_, t)| *t += 1); + solution.compact.votes2.iter_mut().for_each(|(_, (t0, _), t1)| { + if *t0 == 3u16 { + *t0 += 1 + }; + if *t1 == 3u16 { + *t1 += 1 + }; + }); + assert_noop!( + MultiPhase::feasibility_check(solution, COMPUTE), + FeasibilityError::InvalidWinner + ); + }) + } + + #[test] + fn voter_indices() { + // should be caught in `compact.into_assignment`. + ExtBuilder::default().desired_targets(2).build_and_execute(|| { + roll_to(::get() - ::get() - ::get()); + assert!(MultiPhase::current_phase().is_signed()); + + let mut solution = raw_solution(); + assert_eq!(MultiPhase::snapshot().unwrap().voters.len(), 8); + // ----------------------------------------------------^^ valid range is [0..7]. + + // check that there is a index 7 in votes1, and flip to 8. + assert!( + solution + .compact + .votes1 + .iter_mut() + .filter(|(v, _)| *v == 7u32) + .map(|(v, _)| *v = 8) + .count() > 0 + ); + assert_noop!( + MultiPhase::feasibility_check(solution, COMPUTE), + FeasibilityError::NposElection(sp_npos_elections::Error::CompactInvalidIndex), + ); + }) + } + + #[test] + fn voter_votes() { + ExtBuilder::default().desired_targets(2).build_and_execute(|| { + roll_to(::get() - ::get() - ::get()); + assert!(MultiPhase::current_phase().is_signed()); + + let mut solution = raw_solution(); + assert_eq!(MultiPhase::snapshot().unwrap().voters.len(), 8); + // ----------------------------------------------------^^ valid range is [0..7]. + + // first, check that voter at index 7 (40) actually voted for 3 (40) -- this is self + // vote. Then, change the vote to 2 (30). + assert_eq!( + solution + .compact + .votes1 + .iter_mut() + .filter(|(v, t)| *v == 7 && *t == 3) + .map(|(_, t)| *t = 2) + .count(), + 1, + ); + assert_noop!( + MultiPhase::feasibility_check(solution, COMPUTE), + FeasibilityError::InvalidVote, + ); + }) + } + + #[test] + fn score() { + ExtBuilder::default().desired_targets(2).build_and_execute(|| { + roll_to(::get() - ::get() - ::get()); + assert!(MultiPhase::current_phase().is_signed()); + + let mut solution = raw_solution(); + assert_eq!(MultiPhase::snapshot().unwrap().voters.len(), 8); + + // simply faff with the score. + solution.score[0] += 1; + + assert_noop!( + MultiPhase::feasibility_check(solution, COMPUTE), + FeasibilityError::InvalidScore, + ); + }) + } +} + +#[cfg(test)] +mod tests { + use super::{mock::*, Event, *}; + use sp_election_providers::ElectionProvider; + use sp_npos_elections::Support; + + #[test] + fn phase_rotation_works() { + ExtBuilder::default().build_and_execute(|| { + // 0 ------- 15 ------- 25 ------- 30 ------- ------- 45 ------- 55 ------- 60 + // | | | | + // Signed Unsigned Signed Unsigned + + assert_eq!(System::block_number(), 0); + assert_eq!(MultiPhase::current_phase(), Phase::Off); + assert_eq!(MultiPhase::round(), 1); + + roll_to(4); + assert_eq!(MultiPhase::current_phase(), Phase::Off); + assert!(MultiPhase::snapshot().is_none()); + assert_eq!(MultiPhase::round(), 1); + + roll_to(15); + assert_eq!(MultiPhase::current_phase(), Phase::Signed); + assert_eq!(multi_phase_events(), vec![Event::SignedPhaseStarted(1)]); + assert!(MultiPhase::snapshot().is_some()); + assert_eq!(MultiPhase::round(), 1); + + roll_to(24); + assert_eq!(MultiPhase::current_phase(), Phase::Signed); + assert!(MultiPhase::snapshot().is_some()); + assert_eq!(MultiPhase::round(), 1); + + roll_to(25); + assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25))); + assert_eq!( + multi_phase_events(), + vec![Event::SignedPhaseStarted(1), Event::UnsignedPhaseStarted(1)], + ); + assert!(MultiPhase::snapshot().is_some()); + + roll_to(29); + assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25))); + assert!(MultiPhase::snapshot().is_some()); + + roll_to(30); + assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25))); + assert!(MultiPhase::snapshot().is_some()); + + // we close when upstream tells us to elect. + roll_to(32); + assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25))); + assert!(MultiPhase::snapshot().is_some()); + + MultiPhase::elect().unwrap(); + + assert!(MultiPhase::current_phase().is_off()); + assert!(MultiPhase::snapshot().is_none()); + assert_eq!(MultiPhase::round(), 2); + + roll_to(44); + assert!(MultiPhase::current_phase().is_off()); + + roll_to(45); + assert!(MultiPhase::current_phase().is_signed()); + + roll_to(55); + assert!(MultiPhase::current_phase().is_unsigned_open_at(55)); + }) + } + + #[test] + fn signed_phase_void() { + ExtBuilder::default().phases(0, 10).build_and_execute(|| { + roll_to(15); + assert!(MultiPhase::current_phase().is_off()); + + roll_to(19); + assert!(MultiPhase::current_phase().is_off()); + + roll_to(20); + assert!(MultiPhase::current_phase().is_unsigned_open_at(20)); + assert!(MultiPhase::snapshot().is_some()); + + roll_to(30); + assert!(MultiPhase::current_phase().is_unsigned_open_at(20)); + + MultiPhase::elect().unwrap(); + + assert!(MultiPhase::current_phase().is_off()); + assert!(MultiPhase::snapshot().is_none()); + }); + } + + #[test] + fn unsigned_phase_void() { + ExtBuilder::default().phases(10, 0).build_and_execute(|| { + roll_to(15); + assert!(MultiPhase::current_phase().is_off()); + + roll_to(19); + assert!(MultiPhase::current_phase().is_off()); + + roll_to(20); + assert!(MultiPhase::current_phase().is_signed()); + assert!(MultiPhase::snapshot().is_some()); + + roll_to(30); + assert!(MultiPhase::current_phase().is_signed()); + + let _ = MultiPhase::elect().unwrap(); + + assert!(MultiPhase::current_phase().is_off()); + assert!(MultiPhase::snapshot().is_none()); + }); + } + + #[test] + fn both_phases_void() { + ExtBuilder::default().phases(0, 0).build_and_execute(|| { + roll_to(15); + assert!(MultiPhase::current_phase().is_off()); + + roll_to(19); + assert!(MultiPhase::current_phase().is_off()); + + roll_to(20); + assert!(MultiPhase::current_phase().is_off()); + + roll_to(30); + assert!(MultiPhase::current_phase().is_off()); + + // this module is now only capable of doing on-chain backup. + let _ = MultiPhase::elect().unwrap(); + + assert!(MultiPhase::current_phase().is_off()); + }); + } + + #[test] + fn early_termination() { + // an early termination in the signed phase, with no queued solution. + ExtBuilder::default().build_and_execute(|| { + // signed phase started at block 15 and will end at 25. + roll_to(14); + assert_eq!(MultiPhase::current_phase(), Phase::Off); + + roll_to(15); + assert_eq!(multi_phase_events(), vec![Event::SignedPhaseStarted(1)]); + assert_eq!(MultiPhase::current_phase(), Phase::Signed); + assert_eq!(MultiPhase::round(), 1); + + // an unexpected call to elect. + roll_to(20); + MultiPhase::elect().unwrap(); + + // we surely can't have any feasible solutions. This will cause an on-chain election. + assert_eq!( + multi_phase_events(), + vec![ + Event::SignedPhaseStarted(1), + Event::ElectionFinalized(Some(ElectionCompute::OnChain)) + ], + ); + // all storage items must be cleared. + assert_eq!(MultiPhase::round(), 2); + assert!(MultiPhase::snapshot().is_none()); + assert!(MultiPhase::snapshot_metadata().is_none()); + assert!(MultiPhase::desired_targets().is_none()); + assert!(MultiPhase::queued_solution().is_none()); + }) + } + + #[test] + fn fallback_strategy_works() { + ExtBuilder::default().fallabck(FallbackStrategy::OnChain).build_and_execute(|| { + roll_to(15); + assert_eq!(MultiPhase::current_phase(), Phase::Signed); + + roll_to(25); + assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25))); + + // zilch solutions thus far. + let supports = MultiPhase::elect().unwrap(); + + assert_eq!( + supports, + vec![ + (30, Support { total: 40, voters: vec![(2, 5), (4, 5), (30, 30)] }), + (40, Support { total: 60, voters: vec![(2, 5), (3, 10), (4, 5), (40, 40)] }) + ] + ) + }); + + ExtBuilder::default().fallabck(FallbackStrategy::Nothing).build_and_execute(|| { + roll_to(15); + assert_eq!(MultiPhase::current_phase(), Phase::Signed); + + roll_to(25); + assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25))); + + // zilch solutions thus far. + assert_eq!(MultiPhase::elect().unwrap_err(), ElectionError::NoFallbackConfigured); + }) + } + + #[test] + fn number_of_voters_allowed_2sec_block() { + // Just a rough estimate with the substrate weights. + assert!(!MockWeightInfo::get()); + + let all_voters: u32 = 10_000; + let all_targets: u32 = 5_000; + let desired: u32 = 1_000; + let weight_with = |active| { + ::WeightInfo::submit_unsigned( + all_voters, + all_targets, + active, + desired, + ) + }; + + let mut active = 1; + while weight_with(active) + <= ::BlockWeights::get().max_block + || active == all_voters + { + active += 1; + } + + println!("can support {} voters to yield a weight of {}", active, weight_with(active)); + } +} diff --git a/frame/election-provider-multi-phase/src/mock.rs b/frame/election-provider-multi-phase/src/mock.rs new file mode 100644 index 0000000000000..eb38a4cd52e95 --- /dev/null +++ b/frame/election-provider-multi-phase/src/mock.rs @@ -0,0 +1,381 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::*; +use crate as multi_phase; +pub use frame_support::{assert_noop, assert_ok}; +use frame_support::{ + parameter_types, + traits::{Hooks}, + weights::Weight, +}; +use parking_lot::RwLock; +use sp_core::{ + offchain::{ + testing::{PoolState, TestOffchainExt, TestTransactionPoolExt}, + OffchainExt, TransactionPoolExt, + }, + H256, +}; +use sp_election_providers::ElectionDataProvider; +use sp_npos_elections::{ + assignment_ratio_to_staked_normalized, seq_phragmen, to_supports, to_without_backing, + CompactSolution, ElectionResult, EvaluateSupport, +}; +use sp_runtime::{ + testing::Header, + traits::{BlakeTwo256, IdentityLookup}, + PerU16, +}; +use std::sync::Arc; + +pub type Block = sp_runtime::generic::Block; +pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic; + +frame_support::construct_runtime!( + pub enum Runtime where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic + { + System: frame_system::{Module, Call, Event, Config}, + Balances: pallet_balances::{Module, Call, Event, Config}, + MultiPhase: multi_phase::{Module, Call, Event}, + } +); + +pub(crate) type Balance = u64; +pub(crate) type AccountId = u64; + +sp_npos_elections::generate_solution_type!( + #[compact] + pub struct TestCompact::(16) +); + +/// All events of this pallet. +pub(crate) fn multi_phase_events() -> Vec> { + System::events() + .into_iter() + .map(|r| r.event) + .filter_map(|e| if let Event::multi_phase(inner) = e { Some(inner) } else { None }) + .collect::>() +} + +/// To from `now` to block `n`. +pub fn roll_to(n: u64) { + let now = System::block_number(); + for i in now + 1..=n { + System::set_block_number(i); + MultiPhase::on_initialize(i); + } +} + +pub fn roll_to_with_ocw(n: u64) { + let now = System::block_number(); + for i in now + 1..=n { + System::set_block_number(i); + MultiPhase::on_initialize(i); + MultiPhase::offchain_worker(i); + } +} + +/// Spit out a verifiable raw solution. +/// +/// This is a good example of what an offchain miner would do. +pub fn raw_solution() -> RawSolution> { + let RoundSnapshot { voters, targets } = MultiPhase::snapshot().unwrap(); + let desired_targets = MultiPhase::desired_targets().unwrap(); + + // closures + let cache = helpers::generate_voter_cache::(&voters); + let voter_index = helpers::voter_index_fn_linear::(&voters); + let target_index = helpers::target_index_fn_linear::(&targets); + let stake_of = helpers::stake_of_fn::(&voters, &cache); + + let ElectionResult { winners, assignments } = seq_phragmen::<_, CompactAccuracyOf>( + desired_targets as usize, + targets.clone(), + voters.clone(), + None, + ) + .unwrap(); + + let winners = to_without_backing(winners); + + let score = { + let staked = assignment_ratio_to_staked_normalized(assignments.clone(), &stake_of).unwrap(); + to_supports(&winners, &staked).unwrap().evaluate() + }; + let compact = + >::from_assignment(assignments, &voter_index, &target_index).unwrap(); + + let round = MultiPhase::round(); + RawSolution { compact, score, round } +} + +pub fn witness() -> SolutionOrSnapshotSize { + MultiPhase::snapshot() + .map(|snap| SolutionOrSnapshotSize { + voters: snap.voters.len() as u32, + targets: snap.targets.len() as u32, + }) + .unwrap_or_default() +} + +impl frame_system::Config for Runtime { + type SS58Prefix = (); + type BaseCallFilter = (); + type Origin = Origin; + type Index = u64; + type BlockNumber = u64; + type Call = Call; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type Header = Header; + type Event = Event; + type BlockHashCount = (); + type DbWeight = (); + type BlockLength = (); + type BlockWeights = BlockWeights; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); +} + +const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); +parameter_types! { + pub const ExistentialDeposit: u64 = 1; + pub BlockWeights: frame_system::limits::BlockWeights = frame_system::limits::BlockWeights + ::with_sensible_defaults(2 * frame_support::weights::constants::WEIGHT_PER_SECOND, NORMAL_DISPATCH_RATIO); +} + +impl pallet_balances::Config for Runtime { + type Balance = Balance; + type Event = Event; + type DustRemoval = (); + type ExistentialDeposit = ExistentialDeposit; + type AccountStore = System; + type MaxLocks = (); + type WeightInfo = (); +} + +parameter_types! { + pub static Targets: Vec = vec![10, 20, 30, 40]; + pub static Voters: Vec<(AccountId, VoteWeight, Vec)> = vec![ + (1, 10, vec![10, 20]), + (2, 10, vec![30, 40]), + (3, 10, vec![40]), + (4, 10, vec![10, 20, 30, 40]), + // self votes. + (10, 10, vec![10]), + (20, 20, vec![20]), + (30, 30, vec![30]), + (40, 40, vec![40]), + ]; + + pub static Fallback: FallbackStrategy = FallbackStrategy::OnChain; + pub static DesiredTargets: u32 = 2; + pub static SignedPhase: u64 = 10; + pub static UnsignedPhase: u64 = 5; + pub static MaxSignedSubmissions: u32 = 5; + + pub static MinerMaxIterations: u32 = 5; + pub static MinerTxPriority: u64 = 100; + pub static SolutionImprovementThreshold: Perbill = Perbill::zero(); + pub static MinerMaxWeight: Weight = BlockWeights::get().max_block; + pub static MockWeightInfo: bool = false; + + + pub static EpochLength: u64 = 30; +} + +// Hopefully this won't be too much of a hassle to maintain. +pub struct DualMockWeightInfo; +impl multi_phase::weights::WeightInfo for DualMockWeightInfo { + fn on_initialize_nothing() -> Weight { + if MockWeightInfo::get() { + Zero::zero() + } else { + <() as multi_phase::weights::WeightInfo>::on_initialize_nothing() + } + } + fn on_initialize_open_signed() -> Weight { + if MockWeightInfo::get() { + Zero::zero() + } else { + <() as multi_phase::weights::WeightInfo>::on_initialize_open_signed() + } + } + fn on_initialize_open_unsigned_with_snapshot() -> Weight { + if MockWeightInfo::get() { + Zero::zero() + } else { + <() as multi_phase::weights::WeightInfo>::on_initialize_open_unsigned_with_snapshot() + } + } + fn on_initialize_open_unsigned_without_snapshot() -> Weight { + if MockWeightInfo::get() { + Zero::zero() + } else { + <() as multi_phase::weights::WeightInfo>::on_initialize_open_unsigned_without_snapshot() + } + } + fn submit_unsigned(v: u32, t: u32, a: u32, d: u32) -> Weight { + if MockWeightInfo::get() { + // 10 base + // 5 per edge. + (10 as Weight).saturating_add((5 as Weight).saturating_mul(a as Weight)) + } else { + <() as multi_phase::weights::WeightInfo>::submit_unsigned(v, t, a, d) + } + } + fn feasibility_check(v: u32, t: u32, a: u32, d: u32) -> Weight { + if MockWeightInfo::get() { + // 10 base + // 5 per edge. + (10 as Weight).saturating_add((5 as Weight).saturating_mul(a as Weight)) + } else { + <() as multi_phase::weights::WeightInfo>::feasibility_check(v, t, a, d) + } + } +} + +impl crate::Config for Runtime { + type Event = Event; + type Currency = Balances; + type SignedPhase = SignedPhase; + type UnsignedPhase = UnsignedPhase; + type SolutionImprovementThreshold = SolutionImprovementThreshold; + type MinerMaxIterations = MinerMaxIterations; + type MinerMaxWeight = MinerMaxWeight; + type MinerTxPriority = MinerTxPriority; + type DataProvider = StakingMock; + type WeightInfo = DualMockWeightInfo; + type BenchmarkingConfig = (); + type OnChainAccuracy = Perbill; + type Fallback = Fallback; + type CompactSolution = TestCompact; +} + +impl frame_system::offchain::SendTransactionTypes for Runtime +where + Call: From, +{ + type OverarchingCall = Call; + type Extrinsic = Extrinsic; +} + +pub type Extrinsic = sp_runtime::testing::TestXt; + +#[derive(Default)] +pub struct ExtBuilder {} + +pub struct StakingMock; +impl ElectionDataProvider for StakingMock { + fn targets() -> Vec { + Targets::get() + } + fn voters() -> Vec<(AccountId, VoteWeight, Vec)> { + Voters::get() + } + fn desired_targets() -> u32 { + DesiredTargets::get() + } + fn next_election_prediction(now: u64) -> u64 { + now + EpochLength::get() - now % EpochLength::get() + } +} + +impl ExtBuilder { + pub fn miner_tx_priority(self, p: u64) -> Self { + ::set(p); + self + } + pub fn solution_improvement_threshold(self, p: Perbill) -> Self { + ::set(p); + self + } + pub fn phases(self, signed: u64, unsigned: u64) -> Self { + ::set(signed); + ::set(unsigned); + self + } + pub fn fallabck(self, fallback: FallbackStrategy) -> Self { + ::set(fallback); + self + } + pub fn miner_weight(self, weight: Weight) -> Self { + ::set(weight); + self + } + pub fn mock_weight_info(self, mock: bool) -> Self { + ::set(mock); + self + } + pub fn desired_targets(self, t: u32) -> Self { + ::set(t); + self + } + pub fn add_voter(self, who: AccountId, stake: Balance, targets: Vec) -> Self { + VOTERS.with(|v| v.borrow_mut().push((who, stake, targets))); + self + } + pub fn build(self) -> sp_io::TestExternalities { + sp_tracing::try_init_simple(); + let mut storage = + frame_system::GenesisConfig::default().build_storage::().unwrap(); + + let _ = pallet_balances::GenesisConfig:: { + balances: vec![ + // bunch of account for submitting stuff only. + (99, 100), + (999, 100), + (9999, 100), + ], + } + .assimilate_storage(&mut storage); + + sp_io::TestExternalities::from(storage) + } + + pub fn build_offchainify( + self, + iters: u32, + ) -> (sp_io::TestExternalities, Arc>) { + let mut ext = self.build(); + let (offchain, offchain_state) = TestOffchainExt::new(); + let (pool, pool_state) = TestTransactionPoolExt::new(); + + let mut seed = [0_u8; 32]; + seed[0..4].copy_from_slice(&iters.to_le_bytes()); + offchain_state.write().seed = seed; + + ext.register_extension(OffchainExt::new(offchain)); + ext.register_extension(TransactionPoolExt::new(pool)); + + (ext, pool_state) + } + + pub fn build_and_execute(self, test: impl FnOnce() -> ()) { + self.build().execute_with(test) + } +} diff --git a/frame/election-provider-multi-phase/src/unsigned.rs b/frame/election-provider-multi-phase/src/unsigned.rs new file mode 100644 index 0000000000000..2039e5d9f0754 --- /dev/null +++ b/frame/election-provider-multi-phase/src/unsigned.rs @@ -0,0 +1,873 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! The unsigned phase implementation. + +use crate::*; +use frame_support::dispatch::DispatchResult; +use frame_system::offchain::SubmitTransaction; +use sp_npos_elections::{ + seq_phragmen, CompactSolution, ElectionResult, assignment_ratio_to_staked_normalized, + assignment_staked_to_ratio_normalized, +}; +use sp_runtime::{offchain::storage::StorageValueRef, traits::TrailingZeroInput}; +use sp_std::cmp::Ordering; + +/// Storage key used to store the persistent offchain worker status. +pub(crate) const OFFCHAIN_HEAD_DB: &[u8] = b"parity/multi-phase-unsigned-election"; + +/// The repeat threshold of the offchain worker. This means we won't run the offchain worker twice +/// within a window of 5 blocks. +pub(crate) const OFFCHAIN_REPEAT: u32 = 5; + +#[derive(Debug, Eq, PartialEq)] +pub enum MinerError { + /// An internal error in the NPoS elections crate. + NposElections(sp_npos_elections::Error), + /// Snapshot data was unavailable unexpectedly. + SnapshotUnAvailable, + /// Submitting a transaction to the pool failed. + PoolSubmissionFailed, + /// The pre-dispatch checks failed for the mined solution. + PreDispatchChecksFailed, + /// The solution generated from the miner is not feasible. + Feasibility(FeasibilityError), +} + +impl From for MinerError { + fn from(e: sp_npos_elections::Error) -> Self { + MinerError::NposElections(e) + } +} + +impl From for MinerError { + fn from(e: FeasibilityError) -> Self { + MinerError::Feasibility(e) + } +} + +impl Pallet { + /// Mine a new solution, and submit it back to the chain as an unsigned transaction. + pub fn mine_check_and_submit() -> Result<(), MinerError> { + let iters = Self::get_balancing_iters(); + // get the solution, with a load of checks to ensure if submitted, IT IS ABSOLUTELY VALID. + let (raw_solution, witness) = Self::mine_and_check(iters)?; + + let call = Call::submit_unsigned(raw_solution, witness).into(); + SubmitTransaction::>::submit_unsigned_transaction(call) + .map_err(|_| MinerError::PoolSubmissionFailed) + } + + /// Mine a new npos solution, with all the relevant checks to make sure that it will be accepted + /// to the chain. + /// + /// If you want an unchecked solution, use [`Pallet::mine_solution`]. + /// If you want a checked solution and submit it at the same time, use + /// [`Pallet::mine_check_and_submit`]. + pub fn mine_and_check( + iters: usize, + ) -> Result<(RawSolution>, SolutionOrSnapshotSize), MinerError> { + let (raw_solution, witness) = Self::mine_solution(iters)?; + + // ensure that this will pass the pre-dispatch checks + Self::unsigned_pre_dispatch_checks(&raw_solution).map_err(|e| { + log!(warn, "pre-dispatch-checks failed for mined solution: {:?}", e); + MinerError::PreDispatchChecksFailed + })?; + + // ensure that this is a feasible solution + let _ = Self::feasibility_check(raw_solution.clone(), ElectionCompute::Unsigned).map_err( + |e| { + log!(warn, "feasibility-check failed for mined solution: {:?}", e); + MinerError::from(e) + }, + )?; + + Ok((raw_solution, witness)) + } + + /// Mine a new npos solution. + pub fn mine_solution( + iters: usize, + ) -> Result<(RawSolution>, SolutionOrSnapshotSize), MinerError> { + let RoundSnapshot { voters, targets } = + Self::snapshot().ok_or(MinerError::SnapshotUnAvailable)?; + let desired_targets = Self::desired_targets().ok_or(MinerError::SnapshotUnAvailable)?; + + seq_phragmen::<_, CompactAccuracyOf>( + desired_targets as usize, + targets, + voters, + Some((iters, 0)), + ) + .map_err(Into::into) + .and_then(Self::prepare_election_result) + } + + /// Convert a raw solution from [`sp_npos_elections::ElectionResult`] to [`RawSolution`], which + /// is ready to be submitted to the chain. + /// + /// Will always reduce the solution as well. + pub fn prepare_election_result( + election_result: ElectionResult>, + ) -> Result<(RawSolution>, SolutionOrSnapshotSize), MinerError> { + // NOTE: This code path is generally not optimized as it is run offchain. Could use some at + // some point though. + + // storage items. Note: we have already read this from storage, they must be in cache. + let RoundSnapshot { voters, targets } = + Self::snapshot().ok_or(MinerError::SnapshotUnAvailable)?; + let desired_targets = Self::desired_targets().ok_or(MinerError::SnapshotUnAvailable)?; + + // closures. + let cache = helpers::generate_voter_cache::(&voters); + let voter_index = helpers::voter_index_fn::(&cache); + let target_index = helpers::target_index_fn_linear::(&targets); + let voter_at = helpers::voter_at_fn::(&voters); + let target_at = helpers::target_at_fn::(&targets); + let stake_of = helpers::stake_of_fn::(&voters, &cache); + + let ElectionResult { assignments, winners } = election_result; + + // convert to staked and reduce. + let mut staked = assignment_ratio_to_staked_normalized(assignments, &stake_of) + .map_err::(Into::into)?; + sp_npos_elections::reduce(&mut staked); + + // convert back to ration and make compact. + let ratio = assignment_staked_to_ratio_normalized(staked)?; + let compact = >::from_assignment(ratio, &voter_index, &target_index)?; + + let size = + SolutionOrSnapshotSize { voters: voters.len() as u32, targets: targets.len() as u32 }; + let maximum_allowed_voters = Self::maximum_voter_for_weight::( + desired_targets, + size, + T::MinerMaxWeight::get(), + ); + log!( + debug, + "miner: current compact solution voters = {}, maximum_allowed = {}", + compact.voter_count(), + maximum_allowed_voters, + ); + let compact = Self::trim_compact(maximum_allowed_voters, compact, &voter_index)?; + + // re-calc score. + let winners = sp_npos_elections::to_without_backing(winners); + let score = compact.clone().score(&winners, stake_of, voter_at, target_at)?; + + let round = Self::round(); + Ok((RawSolution { compact, score, round }, size)) + } + + /// Get a random number of iterations to run the balancing in the OCW. + /// + /// Uses the offchain seed to generate a random number, maxed with + /// [`Config::MinerMaxIterations`]. + pub fn get_balancing_iters() -> usize { + match T::MinerMaxIterations::get() { + 0 => 0, + max @ _ => { + let seed = sp_io::offchain::random_seed(); + let random = ::decode(&mut TrailingZeroInput::new(seed.as_ref())) + .expect("input is padded with zeroes; qed") + % max.saturating_add(1); + random as usize + } + } + } + + /// Greedily reduce the size of the a solution to fit into the block, w.r.t. weight. + /// + /// The weight of the solution is foremost a function of the number of voters (i.e. + /// `compact.len()`). Aside from this, the other components of the weight are invariant. The + /// number of winners shall not be changed (otherwise the solution is invalid) and the + /// `ElectionSize` is merely a representation of the total number of stakers. + /// + /// Thus, we reside to stripping away some voters. This means only changing the `compact` + /// struct. + /// + /// Note that the solution is already computed, and the winners are elected based on the merit + /// of the entire stake in the system. Nonetheless, some of the voters will be removed further + /// down the line. + /// + /// Indeed, the score must be computed **after** this step. If this step reduces the score too + /// much or remove a winner, then the solution must be discarded **after** this step. + pub fn trim_compact( + maximum_allowed_voters: u32, + mut compact: CompactOf, + voter_index: FN, + ) -> Result, MinerError> + where + for<'r> FN: Fn(&'r T::AccountId) -> Option>, + { + match compact.voter_count().checked_sub(maximum_allowed_voters as usize) { + Some(to_remove) if to_remove > 0 => { + // grab all voters and sort them by least stake. + let RoundSnapshot { voters, .. } = + Self::snapshot().ok_or(MinerError::SnapshotUnAvailable)?; + let mut voters_sorted = voters + .into_iter() + .map(|(who, stake, _)| (who.clone(), stake)) + .collect::>(); + voters_sorted.sort_by_key(|(_, y)| *y); + + // start removing from the least stake. Iterate until we know enough have been + // removed. + let mut removed = 0; + for (maybe_index, _stake) in + voters_sorted.iter().map(|(who, stake)| (voter_index(&who), stake)) + { + let index = maybe_index.ok_or(MinerError::SnapshotUnAvailable)?; + if compact.remove_voter(index) { + removed += 1 + } + + if removed >= to_remove { + break; + } + } + + Ok(compact) + } + _ => { + // nada, return as-is + Ok(compact) + } + } + } + + /// Find the maximum `len` that a compact can have in order to fit into the block weight. + /// + /// This only returns a value between zero and `size.nominators`. + pub fn maximum_voter_for_weight( + desired_winners: u32, + size: SolutionOrSnapshotSize, + max_weight: Weight, + ) -> u32 { + if size.voters < 1 { + return size.voters; + } + + let max_voters = size.voters.max(1); + let mut voters = max_voters; + + // helper closures. + let weight_with = |active_voters: u32| -> Weight { + W::submit_unsigned(size.voters, size.targets, active_voters, desired_winners) + }; + + let next_voters = |current_weight: Weight, voters: u32, step: u32| -> Result { + match current_weight.cmp(&max_weight) { + Ordering::Less => { + let next_voters = voters.checked_add(step); + match next_voters { + Some(voters) if voters < max_voters => Ok(voters), + _ => Err(()), + } + } + Ordering::Greater => voters.checked_sub(step).ok_or(()), + Ordering::Equal => Ok(voters), + } + }; + + // First binary-search the right amount of voters + let mut step = voters / 2; + let mut current_weight = weight_with(voters); + while step > 0 { + match next_voters(current_weight, voters, step) { + // proceed with the binary search + Ok(next) if next != voters => { + voters = next; + } + // we are out of bounds, break out of the loop. + Err(()) => { + break; + } + // we found the right value - early exit the function. + Ok(next) => return next, + } + step = step / 2; + current_weight = weight_with(voters); + } + + // Time to finish. We might have reduced less than expected due to rounding error. Increase + // one last time if we have any room left, the reduce until we are sure we are below limit. + while voters + 1 <= max_voters && weight_with(voters + 1) < max_weight { + voters += 1; + } + while voters.checked_sub(1).is_some() && weight_with(voters) > max_weight { + voters -= 1; + } + + debug_assert!( + weight_with(voters.min(size.voters)) <= max_weight, + "weight_with({}) <= {}", + voters.min(size.voters), + max_weight, + ); + voters.min(size.voters) + } + + /// Checks if an execution of the offchain worker is permitted at the given block number, or + /// not. + /// + /// This essentially makes sure that we don't run on previous blocks in case of a re-org, and we + /// don't run twice within a window of length [`OFFCHAIN_REPEAT`]. + /// + /// Returns `Ok(())` if offchain worker should happen, `Err(reason)` otherwise. + pub(crate) fn try_acquire_offchain_lock(now: T::BlockNumber) -> Result<(), &'static str> { + let storage = StorageValueRef::persistent(&OFFCHAIN_HEAD_DB); + let threshold = T::BlockNumber::from(OFFCHAIN_REPEAT); + + let mutate_stat = + storage.mutate::<_, &'static str, _>(|maybe_head: Option>| { + match maybe_head { + Some(Some(head)) if now < head => Err("fork."), + Some(Some(head)) if now >= head && now <= head + threshold => { + Err("recently executed.") + } + Some(Some(head)) if now > head + threshold => { + // we can run again now. Write the new head. + Ok(now) + } + _ => { + // value doesn't exists. Probably this node just booted up. Write, and run + Ok(now) + } + } + }); + + match mutate_stat { + // all good + Ok(Ok(_)) => Ok(()), + // failed to write. + Ok(Err(_)) => Err("failed to write to offchain db."), + // fork etc. + Err(why) => Err(why), + } + } + + /// Do the basics checks that MUST happen during the validation and pre-dispatch of an unsigned + /// transaction. + /// + /// Can optionally also be called during dispatch, if needed. + /// + /// NOTE: Ideally, these tests should move more and more outside of this and more to the miner's + /// code, so that we do less and less storage reads here. + pub(crate) fn unsigned_pre_dispatch_checks( + solution: &RawSolution>, + ) -> DispatchResult { + // ensure solution is timely. Don't panic yet. This is a cheap check. + ensure!(Self::current_phase().is_unsigned_open(), Error::::PreDispatchEarlySubmission); + + // ensure correct number of winners. + ensure!( + Self::desired_targets().unwrap_or_default() + == solution.compact.unique_targets().len() as u32, + Error::::PreDispatchWrongWinnerCount, + ); + + // ensure score is being improved. Panic henceforth. + ensure!( + Self::queued_solution().map_or(true, |q: ReadySolution<_>| is_score_better::( + solution.score, + q.score, + T::SolutionImprovementThreshold::get() + )), + Error::::PreDispatchWeakSubmission, + ); + + Ok(()) + } +} + +#[cfg(test)] +mod max_weight { + #![allow(unused_variables)] + use super::{mock::*, *}; + + struct TestWeight; + impl crate::weights::WeightInfo for TestWeight { + fn on_initialize_nothing() -> Weight { + unreachable!() + } + fn on_initialize_open_signed() -> Weight { + unreachable!() + } + fn on_initialize_open_unsigned_with_snapshot() -> Weight { + unreachable!() + } + fn on_initialize_open_unsigned_without_snapshot() -> Weight { + unreachable!() + } + fn submit_unsigned(v: u32, t: u32, a: u32, d: u32) -> Weight { + (0 * v + 0 * t + 1000 * a + 0 * d) as Weight + } + fn feasibility_check(v: u32, _t: u32, a: u32, d: u32) -> Weight { + unreachable!() + } + } + + #[test] + fn find_max_voter_binary_search_works() { + let w = SolutionOrSnapshotSize { voters: 10, targets: 0 }; + + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 0), 0); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1), 0); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 999), 0); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1000), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1001), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1990), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1999), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2000), 2); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2001), 2); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2010), 2); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2990), 2); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2999), 2); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 3000), 3); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 3333), 3); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 5500), 5); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 7777), 7); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 9999), 9); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 10_000), 10); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 10_999), 10); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 11_000), 10); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 22_000), 10); + + let w = SolutionOrSnapshotSize { voters: 1, targets: 0 }; + + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 0), 0); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1), 0); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 999), 0); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1000), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1001), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1990), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1999), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2000), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2001), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2010), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 3333), 1); + + let w = SolutionOrSnapshotSize { voters: 2, targets: 0 }; + + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 0), 0); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1), 0); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 999), 0); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1000), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1001), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 1999), 1); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2000), 2); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2001), 2); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 2010), 2); + assert_eq!(MultiPhase::maximum_voter_for_weight::(0, w, 3333), 2); + } +} + +#[cfg(test)] +mod tests { + use super::{ + mock::{Origin, *}, + Call, *, + }; + use frame_support::{dispatch::Dispatchable, traits::OffchainWorker}; + use mock::Call as OuterCall; + use sp_election_providers::Assignment; + use sp_runtime::{traits::ValidateUnsigned, PerU16}; + + #[test] + fn validate_unsigned_retracts_wrong_phase() { + ExtBuilder::default().desired_targets(0).build_and_execute(|| { + let solution = RawSolution:: { score: [5, 0, 0], ..Default::default() }; + let call = Call::submit_unsigned(solution.clone(), witness()); + + // initial + assert_eq!(MultiPhase::current_phase(), Phase::Off); + assert!(matches!( + ::validate_unsigned(TransactionSource::Local, &call) + .unwrap_err(), + TransactionValidityError::Invalid(InvalidTransaction::Custom(0)) + )); + assert!(matches!( + ::pre_dispatch(&call).unwrap_err(), + TransactionValidityError::Invalid(InvalidTransaction::Custom(0)) + )); + + // signed + roll_to(15); + assert_eq!(MultiPhase::current_phase(), Phase::Signed); + assert!(matches!( + ::validate_unsigned(TransactionSource::Local, &call) + .unwrap_err(), + TransactionValidityError::Invalid(InvalidTransaction::Custom(0)) + )); + assert!(matches!( + ::pre_dispatch(&call).unwrap_err(), + TransactionValidityError::Invalid(InvalidTransaction::Custom(0)) + )); + + // unsigned + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + + assert!(::validate_unsigned( + TransactionSource::Local, + &call + ) + .is_ok()); + assert!(::pre_dispatch(&call).is_ok()); + + // unsigned -- but not enabled. + >::put(Phase::Unsigned((false, 25))); + assert!(MultiPhase::current_phase().is_unsigned()); + assert!(matches!( + ::validate_unsigned(TransactionSource::Local, &call) + .unwrap_err(), + TransactionValidityError::Invalid(InvalidTransaction::Custom(0)) + )); + assert!(matches!( + ::pre_dispatch(&call).unwrap_err(), + TransactionValidityError::Invalid(InvalidTransaction::Custom(0)) + )); + }) + } + + #[test] + fn validate_unsigned_retracts_low_score() { + ExtBuilder::default().desired_targets(0).build_and_execute(|| { + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + + let solution = RawSolution:: { score: [5, 0, 0], ..Default::default() }; + let call = Call::submit_unsigned(solution.clone(), witness()); + + // initial + assert!(::validate_unsigned( + TransactionSource::Local, + &call + ) + .is_ok()); + assert!(::pre_dispatch(&call).is_ok()); + + // set a better score + let ready = ReadySolution { score: [10, 0, 0], ..Default::default() }; + >::put(ready); + + // won't work anymore. + assert!(matches!( + ::validate_unsigned( + TransactionSource::Local, + &call + ) + .unwrap_err(), + TransactionValidityError::Invalid(InvalidTransaction::Custom(2)) + )); + assert!(matches!( + ::pre_dispatch(&call).unwrap_err(), + TransactionValidityError::Invalid(InvalidTransaction::Custom(2)) + )); + }) + } + + #[test] + fn validate_unsigned_retracts_incorrect_winner_count() { + ExtBuilder::default().desired_targets(1).build_and_execute(|| { + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + + let solution = RawSolution:: { score: [5, 0, 0], ..Default::default() }; + let call = Call::submit_unsigned(solution.clone(), witness()); + assert_eq!(solution.compact.unique_targets().len(), 0); + + // won't work anymore. + assert!(matches!( + ::validate_unsigned( + TransactionSource::Local, + &call + ) + .unwrap_err(), + TransactionValidityError::Invalid(InvalidTransaction::Custom(1)) + )); + }) + } + + #[test] + fn priority_is_set() { + ExtBuilder::default().miner_tx_priority(20).desired_targets(0).build_and_execute(|| { + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + + let solution = RawSolution:: { score: [5, 0, 0], ..Default::default() }; + let call = Call::submit_unsigned(solution.clone(), witness()); + + assert_eq!( + ::validate_unsigned( + TransactionSource::Local, + &call + ) + .unwrap() + .priority, + 25 + ); + }) + } + + #[test] + #[should_panic(expected = "Invalid unsigned submission must produce invalid block and \ + deprive validator from their authoring reward.: \ + DispatchError::Module { index: 2, error: 1, message: \ + Some(\"PreDispatchWrongWinnerCount\") }")] + fn unfeasible_solution_panics() { + ExtBuilder::default().build_and_execute(|| { + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + + // This is in itself an invalid BS solution. + let solution = RawSolution:: { score: [5, 0, 0], ..Default::default() }; + let call = Call::submit_unsigned(solution.clone(), witness()); + let outer_call: OuterCall = call.into(); + let _ = outer_call.dispatch(Origin::none()); + }) + } + + #[test] + #[should_panic(expected = "Invalid unsigned submission must produce invalid block and \ + deprive validator from their authoring reward.")] + fn wrong_witness_panics() { + ExtBuilder::default().build_and_execute(|| { + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + + // This solution is unfeasible as well, but we won't even get there. + let solution = RawSolution:: { score: [5, 0, 0], ..Default::default() }; + + let mut correct_witness = witness(); + correct_witness.voters += 1; + correct_witness.targets -= 1; + let call = Call::submit_unsigned(solution.clone(), correct_witness); + let outer_call: OuterCall = call.into(); + let _ = outer_call.dispatch(Origin::none()); + }) + } + + #[test] + fn miner_works() { + ExtBuilder::default().build_and_execute(|| { + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + + // ensure we have snapshots in place. + assert!(MultiPhase::snapshot().is_some()); + assert_eq!(MultiPhase::desired_targets().unwrap(), 2); + + // mine seq_phragmen solution with 2 iters. + let (solution, witness) = MultiPhase::mine_solution(2).unwrap(); + + // ensure this solution is valid. + assert!(MultiPhase::queued_solution().is_none()); + assert_ok!(MultiPhase::submit_unsigned(Origin::none(), solution, witness)); + assert!(MultiPhase::queued_solution().is_some()); + }) + } + + #[test] + fn miner_trims_weight() { + ExtBuilder::default().miner_weight(100).mock_weight_info(true).build_and_execute(|| { + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + + let (solution, witness) = MultiPhase::mine_solution(2).unwrap(); + let solution_weight = ::WeightInfo::submit_unsigned( + witness.voters, + witness.targets, + solution.compact.voter_count() as u32, + solution.compact.unique_targets().len() as u32, + ); + // default solution will have 5 edges (5 * 5 + 10) + assert_eq!(solution_weight, 35); + assert_eq!(solution.compact.voter_count(), 5); + + // now reduce the max weight + ::set(25); + + let (solution, witness) = MultiPhase::mine_solution(2).unwrap(); + let solution_weight = ::WeightInfo::submit_unsigned( + witness.voters, + witness.targets, + solution.compact.voter_count() as u32, + solution.compact.unique_targets().len() as u32, + ); + // default solution will have 5 edges (5 * 5 + 10) + assert_eq!(solution_weight, 25); + assert_eq!(solution.compact.voter_count(), 3); + }) + } + + #[test] + fn miner_will_not_submit_if_not_enough_winners() { + let (mut ext, _) = ExtBuilder::default().desired_targets(8).build_offchainify(0); + ext.execute_with(|| { + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + + // mine seq_phragmen solution with 2 iters. + assert_eq!( + MultiPhase::mine_check_and_submit().unwrap_err(), + MinerError::PreDispatchChecksFailed, + ); + }) + } + + #[test] + fn unsigned_per_dispatch_checks_can_only_submit_threshold_better() { + ExtBuilder::default() + .desired_targets(1) + .add_voter(7, 2, vec![10]) + .add_voter(8, 5, vec![10]) + .solution_improvement_threshold(Perbill::from_percent(50)) + .build_and_execute(|| { + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + assert_eq!(MultiPhase::desired_targets().unwrap(), 1); + + // an initial solution + let result = ElectionResult { + // note: This second element of backing stake is not important here. + winners: vec![(10, 10)], + assignments: vec![Assignment { + who: 10, + distribution: vec![(10, PerU16::one())], + }], + }; + let (solution, witness) = MultiPhase::prepare_election_result(result).unwrap(); + assert_ok!(MultiPhase::unsigned_pre_dispatch_checks(&solution)); + assert_ok!(MultiPhase::submit_unsigned(Origin::none(), solution, witness)); + assert_eq!(MultiPhase::queued_solution().unwrap().score[0], 10); + + // trial 1: a solution who's score is only 2, i.e. 20% better in the first element. + let result = ElectionResult { + winners: vec![(10, 12)], + assignments: vec![ + Assignment { who: 10, distribution: vec![(10, PerU16::one())] }, + Assignment { + who: 7, + // note: this percent doesn't even matter, in compact it is 100%. + distribution: vec![(10, PerU16::one())], + }, + ], + }; + let (solution, _) = MultiPhase::prepare_election_result(result).unwrap(); + // 12 is not 50% more than 10 + assert_eq!(solution.score[0], 12); + assert_noop!( + MultiPhase::unsigned_pre_dispatch_checks(&solution), + Error::::PreDispatchWeakSubmission, + ); + // submitting this will actually panic. + + // trial 2: a solution who's score is only 7, i.e. 70% better in the first element. + let result = ElectionResult { + winners: vec![(10, 12)], + assignments: vec![ + Assignment { who: 10, distribution: vec![(10, PerU16::one())] }, + Assignment { who: 7, distribution: vec![(10, PerU16::one())] }, + Assignment { + who: 8, + // note: this percent doesn't even matter, in compact it is 100%. + distribution: vec![(10, PerU16::one())], + }, + ], + }; + let (solution, witness) = MultiPhase::prepare_election_result(result).unwrap(); + assert_eq!(solution.score[0], 17); + + // and it is fine + assert_ok!(MultiPhase::unsigned_pre_dispatch_checks(&solution)); + assert_ok!(MultiPhase::submit_unsigned(Origin::none(), solution, witness)); + }) + } + + #[test] + fn ocw_check_prevent_duplicate() { + let (mut ext, _) = ExtBuilder::default().build_offchainify(0); + ext.execute_with(|| { + roll_to(25); + assert!(MultiPhase::current_phase().is_unsigned()); + + // first execution -- okay. + assert!(MultiPhase::try_acquire_offchain_lock(25).is_ok()); + + // next block: rejected. + assert!(MultiPhase::try_acquire_offchain_lock(26).is_err()); + + // allowed after `OFFCHAIN_REPEAT` + assert!(MultiPhase::try_acquire_offchain_lock((26 + OFFCHAIN_REPEAT).into()).is_ok()); + + // a fork like situation: re-execute last 3. + assert!( + MultiPhase::try_acquire_offchain_lock((26 + OFFCHAIN_REPEAT - 3).into()).is_err() + ); + assert!( + MultiPhase::try_acquire_offchain_lock((26 + OFFCHAIN_REPEAT - 2).into()).is_err() + ); + assert!( + MultiPhase::try_acquire_offchain_lock((26 + OFFCHAIN_REPEAT - 1).into()).is_err() + ); + }) + } + + #[test] + fn ocw_only_runs_when_signed_open_now() { + let (mut ext, pool) = ExtBuilder::default().build_offchainify(0); + ext.execute_with(|| { + roll_to(25); + assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25))); + + // we must clear the offchain storage to ensure the offchain execution check doesn't get + // in the way. + let mut storage = StorageValueRef::persistent(&OFFCHAIN_HEAD_DB); + + MultiPhase::offchain_worker(24); + assert!(pool.read().transactions.len().is_zero()); + storage.clear(); + + MultiPhase::offchain_worker(26); + assert!(pool.read().transactions.len().is_zero()); + storage.clear(); + + // submits! + MultiPhase::offchain_worker(25); + assert!(!pool.read().transactions.len().is_zero()); + }) + } + + #[test] + fn ocw_can_submit_to_pool() { + let (mut ext, pool) = ExtBuilder::default().build_offchainify(0); + ext.execute_with(|| { + roll_to_with_ocw(25); + assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25))); + // OCW must have submitted now + + let encoded = pool.read().transactions[0].clone(); + let extrinsic: Extrinsic = Decode::decode(&mut &*encoded).unwrap(); + let call = extrinsic.call; + assert!(matches!(call, OuterCall::MultiPhase(Call::submit_unsigned(_, _)))); + }) + } +} diff --git a/frame/election-provider-multi-phase/src/weights.rs b/frame/election-provider-multi-phase/src/weights.rs new file mode 100644 index 0000000000000..cbdc5b39bf3ee --- /dev/null +++ b/frame/election-provider-multi-phase/src/weights.rs @@ -0,0 +1,150 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Autogenerated weights for pallet_election_provider_multi_phase +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 +//! DATE: 2021-02-12, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 + +// Executed Command: +// target/release/substrate +// benchmark +// --chain=dev +// --steps=50 +// --repeat=20 +// --pallet=pallet_election_provider_multi_phase +// --extrinsic=* +// --execution=wasm +// --wasm-execution=compiled +// --heap-pages=4096 +// --output=./frame/election-provider-multi-phase/src/weights.rs +// --template=./.maintain/frame-weight-template.hbs + + +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use sp_std::marker::PhantomData; + +/// Weight functions needed for pallet_election_provider_multi_phase. +pub trait WeightInfo { + fn on_initialize_nothing() -> Weight; + fn on_initialize_open_signed() -> Weight; + fn on_initialize_open_unsigned_with_snapshot() -> Weight; + fn on_initialize_open_unsigned_without_snapshot() -> Weight; + fn submit_unsigned(v: u32, t: u32, a: u32, d: u32, ) -> Weight; + fn feasibility_check(v: u32, t: u32, a: u32, d: u32, ) -> Weight; +} + +/// Weights for pallet_election_provider_multi_phase using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + fn on_initialize_nothing() -> Weight { + (23_401_000 as Weight) + .saturating_add(T::DbWeight::get().reads(7 as Weight)) + } + fn on_initialize_open_signed() -> Weight { + (79_260_000 as Weight) + .saturating_add(T::DbWeight::get().reads(7 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) + } + fn on_initialize_open_unsigned_with_snapshot() -> Weight { + (77_745_000 as Weight) + .saturating_add(T::DbWeight::get().reads(7 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) + } + fn on_initialize_open_unsigned_without_snapshot() -> Weight { + (21_764_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + fn submit_unsigned(v: u32, t: u32, a: u32, d: u32, ) -> Weight { + (0 as Weight) + // Standard Error: 23_000 + .saturating_add((4_171_000 as Weight).saturating_mul(v as Weight)) + // Standard Error: 78_000 + .saturating_add((229_000 as Weight).saturating_mul(t as Weight)) + // Standard Error: 23_000 + .saturating_add((13_661_000 as Weight).saturating_mul(a as Weight)) + // Standard Error: 117_000 + .saturating_add((4_499_000 as Weight).saturating_mul(d as Weight)) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + fn feasibility_check(v: u32, t: u32, a: u32, d: u32, ) -> Weight { + (0 as Weight) + // Standard Error: 12_000 + .saturating_add((4_232_000 as Weight).saturating_mul(v as Weight)) + // Standard Error: 42_000 + .saturating_add((636_000 as Weight).saturating_mul(t as Weight)) + // Standard Error: 12_000 + .saturating_add((10_294_000 as Weight).saturating_mul(a as Weight)) + // Standard Error: 64_000 + .saturating_add((4_428_000 as Weight).saturating_mul(d as Weight)) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + } +} + +// For backwards compatibility and tests +impl WeightInfo for () { + fn on_initialize_nothing() -> Weight { + (23_401_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(7 as Weight)) + } + fn on_initialize_open_signed() -> Weight { + (79_260_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(7 as Weight)) + .saturating_add(RocksDbWeight::get().writes(4 as Weight)) + } + fn on_initialize_open_unsigned_with_snapshot() -> Weight { + (77_745_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(7 as Weight)) + .saturating_add(RocksDbWeight::get().writes(4 as Weight)) + } + fn on_initialize_open_unsigned_without_snapshot() -> Weight { + (21_764_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(1 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } + fn submit_unsigned(v: u32, t: u32, a: u32, d: u32, ) -> Weight { + (0 as Weight) + // Standard Error: 23_000 + .saturating_add((4_171_000 as Weight).saturating_mul(v as Weight)) + // Standard Error: 78_000 + .saturating_add((229_000 as Weight).saturating_mul(t as Weight)) + // Standard Error: 23_000 + .saturating_add((13_661_000 as Weight).saturating_mul(a as Weight)) + // Standard Error: 117_000 + .saturating_add((4_499_000 as Weight).saturating_mul(d as Weight)) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } + fn feasibility_check(v: u32, t: u32, a: u32, d: u32, ) -> Weight { + (0 as Weight) + // Standard Error: 12_000 + .saturating_add((4_232_000 as Weight).saturating_mul(v as Weight)) + // Standard Error: 42_000 + .saturating_add((636_000 as Weight).saturating_mul(t as Weight)) + // Standard Error: 12_000 + .saturating_add((10_294_000 as Weight).saturating_mul(a as Weight)) + // Standard Error: 64_000 + .saturating_add((4_428_000 as Weight).saturating_mul(d as Weight)) + .saturating_add(RocksDbWeight::get().reads(3 as Weight)) + } +} diff --git a/frame/elections-phragmen/Cargo.toml b/frame/elections-phragmen/Cargo.toml index 2571dff7c8904..bdb301c73ec36 100644 --- a/frame/elections-phragmen/Cargo.toml +++ b/frame/elections-phragmen/Cargo.toml @@ -13,21 +13,21 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } serde = { version = "1.0.101", optional = true } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-npos-elections = { version = "2.0.0", default-features = false, path = "../../primitives/npos-elections" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-npos-elections = { version = "3.0.0", default-features = false, path = "../../primitives/npos-elections" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-io = { version = "2.0.0", path = "../../primitives/io" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } hex-literal = "0.3.1" -pallet-balances = { version = "2.0.0", path = "../balances" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -substrate-test-utils = { version = "2.0.0", path = "../../test-utils" } +pallet-balances = { version = "3.0.0", path = "../balances" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +substrate-test-utils = { version = "3.0.0", path = "../../test-utils" } [features] default = ["std"] diff --git a/frame/elections-phragmen/src/benchmarking.rs b/frame/elections-phragmen/src/benchmarking.rs index 511d2751a5d77..cfdcd80207958 100644 --- a/frame/elections-phragmen/src/benchmarking.rs +++ b/frame/elections-phragmen/src/benchmarking.rs @@ -22,7 +22,7 @@ use super::*; use frame_system::RawOrigin; -use frame_benchmarking::{benchmarks, account}; +use frame_benchmarking::{benchmarks, account, impl_benchmark_test_suite}; use frame_support::traits::OnInitialize; use crate::Module as Elections; @@ -536,84 +536,9 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{ExtBuilder, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks_elections_phragmen() { - ExtBuilder::default() - .desired_members(13) - .desired_runners_up(7) - .build_and_execute(|| { - assert_ok!(test_benchmark_vote_equal::()); - }); - - ExtBuilder::default() - .desired_members(13) - .desired_runners_up(7) - .build_and_execute(|| { - assert_ok!(test_benchmark_vote_more::()); - }); - - ExtBuilder::default() - .desired_members(13) - .desired_runners_up(7) - .build_and_execute(|| { - assert_ok!(test_benchmark_vote_less::()); - }); - - ExtBuilder::default() - .desired_members(13) - .desired_runners_up(7) - .build_and_execute(|| { - assert_ok!(test_benchmark_remove_voter::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_submit_candidacy::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_renounce_candidacy_candidate::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_renounce_candidacy_runners_up::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_renounce_candidacy_members::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_remove_member_without_replacement::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_remove_member_with_replacement::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_clean_defunct_voters::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_election_phragmen::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_election_phragmen::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_election_phragmen_c_e::()); - }); - - ExtBuilder::default().desired_members(13).desired_runners_up(7).build_and_execute(|| { - assert_ok!(test_benchmark_election_phragmen_v::()); - }); - } -} +impl_benchmark_test_suite!( + Elections, + crate::tests::ExtBuilder::default().desired_members(13).desired_runners_up(7), + crate::tests::Test, + exec_name = build_and_execute, +); diff --git a/frame/elections-phragmen/src/lib.rs b/frame/elections-phragmen/src/lib.rs index d566975e2e7a9..057e9f181c7af 100644 --- a/frame/elections-phragmen/src/lib.rs +++ b/frame/elections-phragmen/src/lib.rs @@ -1074,7 +1074,7 @@ mod tests { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); diff --git a/frame/elections/Cargo.toml b/frame/elections/Cargo.toml index 90e69ea212754..a13c6d7567f06 100644 --- a/frame/elections/Cargo.toml +++ b/frame/elections/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-elections" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,17 +14,17 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] hex-literal = "0.3.1" -pallet-balances = { version = "2.0.0", path = "../balances" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/elections/src/mock.rs b/frame/elections/src/mock.rs index b386542b2b3db..7c9bc9bfaf8b0 100644 --- a/frame/elections/src/mock.rs +++ b/frame/elections/src/mock.rs @@ -52,7 +52,7 @@ impl frame_system::Config for Test { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); diff --git a/frame/erc20/Cargo.toml b/frame/erc20/Cargo.toml index f08830761f01e..a1f8e5e5439fc 100644 --- a/frame/erc20/Cargo.toml +++ b/frame/erc20/Cargo.toml @@ -14,19 +14,19 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-balances = { version = "2.0.0", default-features = false, path = "../balances" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core", default-features = false } -sp-arithmetic = { version = "2.0.0", path = "../../primitives/arithmetic", default-features = false } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +pallet-balances = { version = "3.0.0", default-features = false, path = "../balances" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core", default-features = false } +sp-arithmetic = { version = "3.0.0", path = "../../primitives/arithmetic", default-features = false } pallet-chainbridge = { version = "2.0.0", default-features = false, path = "../chainbridge" } pallet-erc721 = { version = "2.0.0", default-features = false, path = "../erc721" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [features] default = ["std"] diff --git a/frame/erc721/Cargo.toml b/frame/erc721/Cargo.toml index 53d2e98f017e3..10b4ae40ceaec 100644 --- a/frame/erc721/Cargo.toml +++ b/frame/erc721/Cargo.toml @@ -14,17 +14,17 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-balances = { version = "2.0.0", default-features = false, path = "../balances" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core", default-features = false } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +pallet-balances = { version = "3.0.0", default-features = false, path = "../balances" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core", default-features = false } pallet-chainbridge = { version = "2.0.0", default-features = false, path = "../chainbridge" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [features] default = ["std"] diff --git a/frame/erc721/src/mock.rs b/frame/erc721/src/mock.rs index 603df31e84289..d646faa531d61 100644 --- a/frame/erc721/src/mock.rs +++ b/frame/erc721/src/mock.rs @@ -38,7 +38,7 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type DbWeight = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); diff --git a/frame/example-parallel/Cargo.toml b/frame/example-parallel/Cargo.toml index ee816d963be98..b2f28887cec0b 100644 --- a/frame/example-parallel/Cargo.toml +++ b/frame/example-parallel/Cargo.toml @@ -12,14 +12,17 @@ description = "FRAME example pallet using runtime worker threads" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-tasks = { version = "2.0.0", default-features = false, path = "../../primitives/tasks" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-tasks = { version = "3.0.0", default-features = false, path = "../../primitives/tasks" } + +[dev-dependencies] +serde = { version = "1.0.101" } [features] default = ["std"] diff --git a/frame/example-parallel/src/lib.rs b/frame/example-parallel/src/lib.rs index c83a722be127f..e777100c6f54b 100644 --- a/frame/example-parallel/src/lib.rs +++ b/frame/example-parallel/src/lib.rs @@ -22,10 +22,6 @@ #![cfg_attr(not(feature = "std"), no_std)] -use frame_system::ensure_signed; -use frame_support::{ - dispatch::DispatchResult, decl_module, decl_storage, decl_event, -}; use sp_runtime::RuntimeDebug; use codec::{Encode, Decode}; @@ -34,33 +30,71 @@ use sp_std::vec::Vec; #[cfg(test)] mod tests; -pub trait Config: frame_system::Config { - /// The overarching event type. - type Event: From + Into<::Event>; - /// The overarching dispatch call type. - type Call: From>; -} +pub use pallet::*; -decl_storage! { - trait Store for Module as ExampleOffchainWorker { - /// A vector of current participants - /// - /// To enlist someone to participate, signed payload should be - /// sent to `enlist`. - Participants get(fn participants): Vec>; +#[frame_support::pallet] +pub mod pallet { + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use super::*; - /// Current event id to enlist participants to. - CurrentEventId get(fn get_current_event_id): Vec; + #[pallet::config] + pub trait Config: frame_system::Config { + /// The overarching dispatch call type. + type Call: From>; } -} -decl_event!( - /// Events generated by the module. - pub enum Event { - /// When new event is drafted. - NewEventDrafted(Vec), + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); + + #[pallet::hooks] + impl Hooks> for Pallet {} + + /// A public part of the pallet. + #[pallet::call] + impl Pallet { + /// Get the new event running. + #[pallet::weight(0)] + pub fn run_event(origin: OriginFor, id: Vec) -> DispatchResultWithPostInfo { + let _ = ensure_signed(origin)?; + >::kill(); + >::mutate(move |event_id| *event_id = id); + Ok(().into()) + } + + /// Submit list of participants to the current event. + /// + /// The example utilizes parallel execution by checking half of the + /// signatures in spawned task. + #[pallet::weight(0)] + pub fn enlist_participants(origin: OriginFor, participants: Vec) + -> DispatchResultWithPostInfo + { + let _ = ensure_signed(origin)?; + + if validate_participants_parallel(&>::get(), &participants[..]) { + for participant in participants { + >::append(participant.account); + } + } + Ok(().into()) + } } -); + + /// A vector of current participants + /// + /// To enlist someone to participate, signed payload should be + /// sent to `enlist`. + #[pallet::storage] + #[pallet::getter(fn participants)] + pub(super) type Participants = StorageValue<_, Vec>, ValueQuery>; + + /// Current event id to enlist participants to. + #[pallet::storage] + #[pallet::getter(fn get_current_event_id)] + pub(super) type CurrentEventId = StorageValue<_, Vec, ValueQuery>; +} /// Request to enlist participant. #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)] @@ -85,40 +119,6 @@ impl EnlistedParticipant { } } -decl_module! { - /// A public part of the pallet. - pub struct Module for enum Call where origin: T::Origin { - fn deposit_event() = default; - - /// Get the new event running. - #[weight = 0] - pub fn run_event(origin, id: Vec) -> DispatchResult { - let _ = ensure_signed(origin)?; - Participants::kill(); - CurrentEventId::mutate(move |event_id| *event_id = id); - Ok(()) - } - - /// Submit list of participants to the current event. - /// - /// The example utilizes parallel execution by checking half of the - /// signatures in spawned task. - #[weight = 0] - pub fn enlist_participants(origin, participants: Vec) - -> DispatchResult - { - let _ = ensure_signed(origin)?; - - if validate_participants_parallel(&CurrentEventId::get(), &participants[..]) { - for participant in participants { - Participants::append(participant.account); - } - } - Ok(()) - } - } -} - fn validate_participants_parallel(event_id: &[u8], participants: &[EnlistedParticipant]) -> bool { fn spawn_verify(data: Vec) -> Vec { diff --git a/frame/example-parallel/src/tests.rs b/frame/example-parallel/src/tests.rs index 4623de196df88..da2892c67d42a 100644 --- a/frame/example-parallel/src/tests.rs +++ b/frame/example-parallel/src/tests.rs @@ -15,23 +15,29 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::*; +use crate::{self as pallet_example_parallel, *}; -use codec::{Encode, Decode}; -use frame_support::{impl_outer_origin, parameter_types}; +use frame_support::parameter_types; use sp_core::H256; use sp_runtime::{ - Perbill, - testing::{Header}, + Perbill, testing::Header, traits::{BlakeTwo256, IdentityLookup}, }; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Example: pallet_example_parallel::{Module, Call, Storage}, + } +); -#[derive(Clone, Eq, PartialEq, Encode, Decode)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub const AvailableBlockRatio: Perbill = Perbill::one(); @@ -40,8 +46,8 @@ parameter_types! { impl frame_system::Config for Test { type BaseCallFilter = (); type Origin = Origin; - type Call = (); - type PalletInfo = (); + type Call = Call; + type PalletInfo = PalletInfo; type Index = u64; type BlockNumber = u64; type Hash = H256; @@ -49,7 +55,7 @@ impl frame_system::Config for Test { type AccountId = sp_core::sr25519::Public; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type DbWeight = (); type BlockWeights = (); @@ -69,12 +75,9 @@ parameter_types! { } impl Config for Test { - type Event = (); - type Call = Call; + type Call = Call; } -type Example = Module; - #[test] fn it_can_enlist() { use sp_core::Pair; diff --git a/frame/example/Cargo.toml b/frame/example/Cargo.toml index e805a27a96b82..c6dfc018b3f52 100644 --- a/frame/example/Cargo.toml +++ b/frame/example/Cargo.toml @@ -14,18 +14,18 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-balances = { version = "2.0.0", default-features = false, path = "../balances" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +pallet-balances = { version = "3.0.0", default-features = false, path = "../balances" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core", default-features = false } +sp-core = { version = "3.0.0", path = "../../primitives/core", default-features = false } [features] default = ["std"] diff --git a/frame/example/src/lib.rs b/frame/example/src/lib.rs index 05526d2c7a29e..b4ae35c5508a9 100644 --- a/frame/example/src/lib.rs +++ b/frame/example/src/lib.rs @@ -63,9 +63,9 @@ //! // Include the following links that shows what trait needs to be implemented to use the pallet //! // and the supported dispatchables that are documented in the Call enum. //! -//! - \[`::Config`](./trait.Config.html) -//! - \[`Call`](./enum.Call.html) -//! - \[`Module`](./struct.Module.html) +//! - \[`Config`] +//! - \[`Call`] +//! - \[`Pallet`] //! //! \## Overview //! @@ -257,11 +257,11 @@ use sp_std::marker::PhantomData; use frame_support::{ - dispatch::DispatchResult, decl_module, decl_storage, decl_event, traits::IsSubType, + dispatch::DispatchResult, traits::IsSubType, weights::{DispatchClass, ClassifyDispatch, WeighData, Weight, PaysFee, Pays}, }; use sp_std::prelude::*; -use frame_system::{ensure_signed, ensure_root}; +use frame_system::{ensure_signed}; use codec::{Encode, Decode}; use sp_runtime::{ traits::{ @@ -278,7 +278,7 @@ use sp_runtime::{ // The `WeightData` trait has access to the arguments of the dispatch that it wants to assign a // weight to. Nonetheless, the trait itself can not make any assumptions about what the generic type // of the arguments (`T`) is. Based on our needs, we could replace `T` with a more concrete type -// while implementing the trait. The `decl_module!` expects whatever implements `WeighData` to +// while implementing the trait. The `pallet::weight` expects whatever implements `WeighData` to // replace `T` with a tuple of the dispatch arguments. This is exactly how we will craft the // implementation below. // @@ -315,111 +315,97 @@ impl PaysFee<(&BalanceOf,)> for WeightForSetDummy /// A type alias for the balance type from this pallet's point of view. type BalanceOf = ::Balance; -/// Our pallet's configuration trait. All our types and constants go in here. If the -/// pallet is dependent on specific other pallets, then their configuration traits -/// should be added to our implied traits list. -/// -/// `frame_system::Config` should always be included in our implied traits. -pub trait Config: pallet_balances::Config { - /// The overarching event type. - type Event: From> + Into<::Event>; -} +// Re-export pallet items so that they can be accessed from the crate namespace. +pub use pallet::*; -decl_storage! { - // A macro for the Storage trait, and its implementation, for this pallet. - // This allows for type-safe usage of the Substrate storage database, so you can - // keep things around between blocks. - // - // It is important to update your storage name so that your pallet's - // storage items are isolated from other pallets. - // ---------------------------------vvvvvvv - trait Store for Module as Example { - // Any storage declarations of the form: - // `pub? Name get(fn getter_name)? [config()|config(myname)] [build(|_| {...})] : (= )?;` - // where `` is either: - // - `Type` (a basic value item); or - // - `map hasher(HasherKind) KeyType => ValueType` (a map item). - // - // Note that there are two optional modifiers for the storage type declaration. - // - `Foo: Option`: - // - `Foo::put(1); Foo::get()` returns `Some(1)`; - // - `Foo::kill(); Foo::get()` returns `None`. - // - `Foo: u32`: - // - `Foo::put(1); Foo::get()` returns `1`; - // - `Foo::kill(); Foo::get()` returns `0` (u32::default()). - // e.g. Foo: u32; - // e.g. pub Bar get(fn bar): map hasher(blake2_128_concat) T::AccountId => Vec<(T::Balance, u64)>; - // - // For basic value items, you'll get a type which implements - // `frame_support::StorageValue`. For map items, you'll get a type which - // implements `frame_support::StorageMap`. +// Definition of the pallet logic, to be aggregated at runtime definition through +// `construct_runtime`. +#[frame_support::pallet] +pub mod pallet { + // Import various types used to declare pallet in scope. + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use super::*; + + /// Our pallet's configuration trait. All our types and constants go in here. If the + /// pallet is dependent on specific other pallets, then their configuration traits + /// should be added to our implied traits list. + /// + /// `frame_system::Config` should always be included. + #[pallet::config] + pub trait Config: pallet_balances::Config + frame_system::Config { + /// The overarching event type. + type Event: From> + IsType<::Event>; + } + + // Simple declaration of the `Pallet` type. It is placeholder we use to implement traits and + // method. + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); + + // Pallet implements [`Hooks`] trait to define some logic to execute in some context. + #[pallet::hooks] + impl Hooks> for Pallet { + // `on_initialize` is executed at the beginning of the block before any extrinsic are + // dispatched. // - // If they have a getter (`get(getter_name)`), then your pallet will come - // equipped with `fn getter_name() -> Type` for basic value items or - // `fn getter_name(key: KeyType) -> ValueType` for map items. - Dummy get(fn dummy) config(): Option; + // This function must return the weight consumed by `on_initialize` and `on_finalize`. + fn on_initialize(_n: T::BlockNumber) -> Weight { + // Anything that needs to be done at the start of the block. + // We don't do anything here. - // A map that has enumerable entries. - Bar get(fn bar) config(): map hasher(blake2_128_concat) T::AccountId => T::Balance; + 0 + } - // this one uses the default, we'll demonstrate the usage of 'mutate' API. - Foo get(fn foo) config(): T::Balance; - } -} + // `on_finalize` is executed at the end of block after all extrinsic are dispatched. + fn on_finalize(_n: T::BlockNumber) { + // We just kill our dummy storage item. + >::kill(); + } -decl_event!( - /// Events are a simple means of reporting specific conditions and - /// circumstances that have happened that users, Dapps and/or chain explorers would find - /// interesting and otherwise difficult to detect. - pub enum Event where B = ::Balance { - // Just a normal `enum`, here's a dummy event to ensure it compiles. - /// Dummy event, just here so there's a generic type that's used. - Dummy(B), + // A runtime code run after every block and have access to extended set of APIs. + // + // For instance you can generate extrinsics for the upcoming produced block. + fn offchain_worker(_n: T::BlockNumber) { + // We don't do anything here. + // but we could dispatch extrinsic (transaction/unsigned/inherent) using + // sp_io::submit_extrinsic + } } -); -// The module declaration. This states the entry points that we handle. The -// macro takes care of the marshalling of arguments and dispatch. -// -// Anyone can have these functions execute by signing and submitting -// an extrinsic. Ensure that calls into each of these execute in a time, memory and -// using storage space proportional to any costs paid for by the caller or otherwise the -// difficulty of forcing the call to happen. -// -// Generally you'll want to split these into three groups: -// - Public calls that are signed by an external account. -// - Root calls that are allowed to be made only by the governance system. -// - Unsigned calls that can be of two kinds: -// * "Inherent extrinsics" that are opinions generally held by the block -// authors that build child blocks. -// * Unsigned Transactions that are of intrinsic recognizable utility to the -// network, and are validated by the runtime. -// -// Information about where this dispatch initiated from is provided as the first argument -// "origin". As such functions must always look like: -// -// `fn foo(origin, bar: Bar, baz: Baz) -> Result;` -// -// The `Result` is required as part of the syntax (and expands to the conventional dispatch -// result of `Result<(), &'static str>`). -// -// When you come to `impl` them later in the pallet, you must specify the full type for `origin`: -// -// `fn foo(origin: T::Origin, bar: Bar, baz: Baz) { ... }` -// -// There are three entries in the `frame_system::Origin` enum that correspond -// to the above bullets: `::Signed(AccountId)`, `::Root` and `::None`. You should always match -// against them as the first thing you do in your function. There are three convenience calls -// in system that do the matching for you and return a convenient result: `ensure_signed`, -// `ensure_root` and `ensure_none`. -decl_module! { - // Simple declaration of the `Module` type. Lets the macro know what its working on. - pub struct Module for enum Call where origin: T::Origin { - /// Deposit one of this pallet's events by using the default implementation. - /// It is also possible to provide a custom implementation. - /// For non-generic events, the generic parameter just needs to be dropped, so that it - /// looks like: `fn deposit_event() = default;`. - fn deposit_event() = default; + // The call declaration. This states the entry points that we handle. The + // macro takes care of the marshalling of arguments and dispatch. + // + // Anyone can have these functions execute by signing and submitting + // an extrinsic. Ensure that calls into each of these execute in a time, memory and + // using storage space proportional to any costs paid for by the caller or otherwise the + // difficulty of forcing the call to happen. + // + // Generally you'll want to split these into three groups: + // - Public calls that are signed by an external account. + // - Root calls that are allowed to be made only by the governance system. + // - Unsigned calls that can be of two kinds: + // * "Inherent extrinsics" that are opinions generally held by the block + // authors that build child blocks. + // * Unsigned Transactions that are of intrinsic recognizable utility to the + // network, and are validated by the runtime. + // + // Information about where this dispatch initiated from is provided as the first argument + // "origin". As such functions must always look like: + // + // `fn foo(origin: OriginFor, bar: Bar, baz: Baz) -> DispatchResultWithPostInfo { ... }` + // + // The `DispatchResultWithPostInfo` is required as part of the syntax (and can be found at + // `pallet_prelude::DispatchResultWithPostInfo`). + // + // There are three entries in the `frame_system::Origin` enum that correspond + // to the above bullets: `::Signed(AccountId)`, `::Root` and `::None`. You should always match + // against them as the first thing you do in your function. There are three convenience calls + // in system that do the matching for you and return a convenient result: `ensure_signed`, + // `ensure_root` and `ensure_none`. + #[pallet::call] + impl Pallet { /// This is your public interface. Be extremely careful. /// This is just a simple example of how to interact with the pallet from the external /// world. @@ -458,18 +444,22 @@ decl_module! { // // If you don't respect these rules, it is likely that your chain will be attackable. // - // Each transaction can define an optional `#[weight]` attribute to convey a set of static + // Each transaction must define a `#[pallet::weight(..)]` attribute to convey a set of static // information about its dispatch. FRAME System and FRAME Executive pallet then use this // information to properly execute the transaction, whilst keeping the total load of the // chain in a moderate rate. // - // The _right-hand-side_ value of the `#[weight]` attribute can be any type that implements - // a set of traits, namely [`WeighData`] and [`ClassifyDispatch`]. The former conveys the - // weight (a numeric representation of pure execution time and difficulty) of the - // transaction and the latter demonstrates the [`DispatchClass`] of the call. A higher - // weight means a larger transaction (less of which can be placed in a single block). - #[weight = 0] - fn accumulate_dummy(origin, increase_by: T::Balance) -> DispatchResult { + // The parenthesized value of the `#[pallet::weight(..)]` attribute can be any type that + // implements a set of traits, namely [`WeighData`] and [`ClassifyDispatch`]. + // The former conveys the weight (a numeric representation of pure execution time and + // difficulty) of the transaction and the latter demonstrates the [`DispatchClass`] of the + // call. A higher weight means a larger transaction (less of which can be placed in a + // single block). + #[pallet::weight(0)] + pub(super) fn accumulate_dummy( + origin: OriginFor, + increase_by: T::Balance + ) -> DispatchResultWithPostInfo { // This is a public call, so we ensure that the origin is some signed account. let _sender = ensure_signed(origin)?; @@ -493,10 +483,10 @@ decl_module! { }); // Let's deposit an event to let the outside world know this happened. - Self::deposit_event(RawEvent::Dummy(increase_by)); + Self::deposit_event(Event::Dummy(increase_by)); - // All good. - Ok(()) + // All good, no refund. + Ok(().into()) } /// A privileged call; in this case it resets our dummy value to something new. @@ -506,39 +496,92 @@ decl_module! { // calls to be executed - we don't need to care why. Because it's privileged, we can // assume it's a one-off operation and substantial processing/storage/memory can be used // without worrying about gameability or attack scenarios. - // If you do not specify `Result` explicitly as return value, it will be added automatically - // for you and `Ok(())` will be returned. - #[weight = WeightForSetDummy::(>::from(100u32))] - fn set_dummy(origin, #[compact] new_value: T::Balance) { + #[pallet::weight(WeightForSetDummy::(>::from(100u32)))] + fn set_dummy( + origin: OriginFor, + #[pallet::compact] new_value: T::Balance, + ) -> DispatchResultWithPostInfo { ensure_root(origin)?; // Put the new value into storage. >::put(new_value); + + // All good, no refund. + Ok(().into()) } + } - // The signature could also look like: `fn on_initialize()`. - // This function could also very well have a weight annotation, similar to any other. The - // only difference is that it mut be returned, not annotated. - fn on_initialize(_n: T::BlockNumber) -> Weight { - // Anything that needs to be done at the start of the block. - // We don't do anything here. + /// Events are a simple means of reporting specific conditions and + /// circumstances that have happened that users, Dapps and/or chain explorers would find + /// interesting and otherwise difficult to detect. + #[pallet::event] + /// This attribute generate the function `deposit_event` to deposit one of this pallet event, + /// it is optional, it is also possible to provide a custom implementation. + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + // Just a normal `enum`, here's a dummy event to ensure it compiles. + /// Dummy event, just here so there's a generic type that's used. + Dummy(BalanceOf), + } - 0 - } + // pallet::storage attributes allow for type-safe usage of the Substrate storage database, + // so you can keep things around between blocks. + // + // Any storage must be one of `StorageValue`, `StorageMap` or `StorageDoubleMap`. + // The first generic holds the prefix to use and is generated by the macro. + // The query kind is either `OptionQuery` (the default) or `ValueQuery`. + // - for `type Foo = StorageValue<_, u32, OptionQuery>`: + // - `Foo::put(1); Foo::get()` returns `Some(1)`; + // - `Foo::kill(); Foo::get()` returns `None`. + // - for `type Foo = StorageValue<_, u32, ValueQuery>`: + // - `Foo::put(1); Foo::get()` returns `1`; + // - `Foo::kill(); Foo::get()` returns `0` (u32::default()). + #[pallet::storage] + // The getter attribute generate a function on `Pallet` placeholder: + // `fn getter_name() -> Type` for basic value items or + // `fn getter_name(key: KeyType) -> ValueType` for map items. + #[pallet::getter(fn dummy)] + pub(super) type Dummy = StorageValue<_, T::Balance>; + + // A map that has enumerable entries. + #[pallet::storage] + #[pallet::getter(fn bar)] + pub(super) type Bar = StorageMap<_, Blake2_128Concat, T::AccountId, T::Balance, ValueQuery>; + + // this one uses the query kind: `ValueQuery`, we'll demonstrate the usage of 'mutate' API. + #[pallet::storage] + #[pallet::getter(fn foo)] + pub(super) type Foo = StorageValue<_, T::Balance, ValueQuery>; + + + // The genesis config type. + #[pallet::genesis_config] + pub struct GenesisConfig { + pub dummy: T::Balance, + pub bar: Vec<(T::AccountId, T::Balance)>, + pub foo: T::Balance, + } - // The signature could also look like: `fn on_finalize()` - fn on_finalize(_n: T::BlockNumber) { - // Anything that needs to be done at the end of the block. - // We just kill our dummy storage item. - >::kill(); + // The default value for the genesis config type. + #[cfg(feature = "std")] + impl Default for GenesisConfig { + fn default() -> Self { + Self { + dummy: Default::default(), + bar: Default::default(), + foo: Default::default(), + } } + } - // A runtime code run after every block and have access to extended set of APIs. - // - // For instance you can generate extrinsics for the upcoming produced block. - fn offchain_worker(_n: T::BlockNumber) { - // We don't do anything here. - // but we could dispatch extrinsic (transaction/unsigned/inherent) using - // sp_io::submit_extrinsic + // The build of genesis for the pallet. + #[pallet::genesis_build] + impl GenesisBuild for GenesisConfig { + fn build(&self) { + >::put(&self.dummy); + for (a, b) in &self.bar { + >::insert(a, b); + } + >::put(&self.foo); } } } @@ -548,7 +591,7 @@ decl_module! { // - Public interface. These are functions that are `pub` and generally fall into inspector // functions that do not write to storage and operation functions that do. // - Private functions. These are your usual private utilities unavailable to other pallets. -impl Module { +impl Pallet { // Add public immutables and private mutables. #[allow(dead_code)] fn accumulate_foo(origin: T::Origin, increase_by: T::Balance) -> DispatchResult { @@ -577,7 +620,8 @@ impl Module { // // Note that a signed extension can also indicate that a particular data must be present in the // _signing payload_ of a transaction by providing an implementation for the `additional_signed` -// method. This example will not cover this type of extension. See `CheckRuntime` in FRAME System +// method. This example will not cover this type of extension. See `CheckSpecVersion` in +// [FRAME System](https://github.com/paritytech/substrate/tree/master/frame/system#signed-extensions) // for an example. // // Using the extension, you can add some hooks to the life cycle of each transaction. Note that by @@ -651,7 +695,7 @@ where #[cfg(feature = "runtime-benchmarks")] mod benchmarking { use super::*; - use frame_benchmarking::{benchmarks, account}; + use frame_benchmarking::{benchmarks, account, impl_benchmark_test_suite}; use frame_system::RawOrigin; benchmarks!{ @@ -683,22 +727,7 @@ mod benchmarking { } } - #[cfg(test)] - mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_accumulate_dummy::()); - assert_ok!(test_benchmark_set_dummy::()); - assert_ok!(test_benchmark_another_set_dummy::()); - assert_ok!(test_benchmark_sort_vector::()); - }); - } - } + impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test); } #[cfg(test)] @@ -706,32 +735,35 @@ mod tests { use super::*; use frame_support::{ - assert_ok, impl_outer_origin, parameter_types, impl_outer_dispatch, + assert_ok, parameter_types, weights::{DispatchInfo, GetDispatchInfo}, traits::{OnInitialize, OnFinalize} }; use sp_core::H256; // The testing primitives are very useful for avoiding having to work with signatures // or public keys. `u64` is used as the `AccountId` and no `Signature`s are required. use sp_runtime::{ - testing::Header, + testing::Header, BuildStorage, traits::{BlakeTwo256, IdentityLookup}, }; - - impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} - } - - impl_outer_dispatch! { - pub enum OuterCall for Test where origin: Origin { - self::Example, + // Reexport crate as its pallet name for construct_runtime. + use crate as pallet_example; + + type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; + type Block = frame_system::mocking::MockBlock; + + // For testing the pallet, we construct a mock runtime. + frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Example: pallet_example::{Module, Call, Storage, Config, Event}, } - } + ); - // For testing the pallet, we construct most of a mock runtime. This means - // first constructing a configuration type (`Test`) which `impl`s each of the - // configuration traits of pallets we want to use. - #[derive(Clone, Eq, PartialEq)] - pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -746,15 +778,15 @@ mod tests { type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = OuterCall; + type Call = Call; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -768,29 +800,29 @@ mod tests { type MaxLocks = (); type Balance = u64; type DustRemoval = (); - type Event = (); + type Event = Event; type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; type WeightInfo = (); } impl Config for Test { - type Event = (); + type Event = Event; } - type System = frame_system::Module; - type Example = Module; // This function basically just builds a genesis storage key/value store according to // our desired mockup. pub fn new_test_ext() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); - // We use default for brevity, but you can configure as desired if needed. - pallet_balances::GenesisConfig::::default().assimilate_storage(&mut t).unwrap(); - GenesisConfig::{ - dummy: 42, - // we configure the map with (key, value) pairs. - bar: vec![(1, 2), (2, 3)], - foo: 24, - }.assimilate_storage(&mut t).unwrap(); + let t = GenesisConfig { + // We use default for brevity, but you can configure as desired if needed. + frame_system: Some(Default::default()), + pallet_balances: Some(Default::default()), + pallet_example: Some(pallet_example::GenesisConfig { + dummy: 42, + // we configure the map with (key, value) pairs. + bar: vec![(1, 2), (2, 3)], + foo: 24, + }), + }.build_storage().unwrap(); t.into() } @@ -827,7 +859,7 @@ mod tests { #[test] fn signed_ext_watch_dummy_works() { new_test_ext().execute_with(|| { - let call = >::set_dummy(10).into(); + let call = >::set_dummy(10).into(); let info = DispatchInfo::default(); assert_eq!( @@ -846,13 +878,13 @@ mod tests { #[test] fn weights_work() { // must have a defined weight. - let default_call = >::accumulate_dummy(10); + let default_call = >::accumulate_dummy(10); let info = default_call.get_dispatch_info(); // aka. `let info = as GetDispatchInfo>::get_dispatch_info(&default_call);` assert_eq!(info.weight, 0); // must have a custom weight of `100 * arg = 2000` - let custom_call = >::set_dummy(20); + let custom_call = >::set_dummy(20); let info = custom_call.get_dispatch_info(); assert_eq!(info.weight, 2000); } diff --git a/frame/executive/Cargo.toml b/frame/executive/Cargo.toml index 3f9820b5f3f57..7ef00e7ff71c8 100644 --- a/frame/executive/Cargo.toml +++ b/frame/executive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-executive" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,24 +13,24 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } serde = { version = "1.0.101", optional = true } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-tracing = { version = "2.0.0", default-features = false, path = "../../primitives/tracing" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-tracing = { version = "3.0.0", default-features = false, path = "../../primitives/tracing" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } [dev-dependencies] hex-literal = "0.3.1" -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-io ={ version = "2.0.0", path = "../../primitives/io" } -pallet-indices = { version = "2.0.0", path = "../indices" } -pallet-balances = { version = "2.0.0", path = "../balances" } -pallet-transaction-payment = { version = "2.0.0", path = "../transaction-payment" } -sp-version = { version = "2.0.0", path = "../../primitives/version" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-io ={ version = "3.0.0", path = "../../primitives/io" } +pallet-indices = { version = "3.0.0", path = "../indices" } +pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-transaction-payment = { version = "3.0.0", path = "../transaction-payment" } +sp-version = { version = "3.0.0", path = "../../primitives/version" } [features] default = ["std"] @@ -47,3 +47,6 @@ std = [ "sp-tracing/std", "sp-std/std", ] +try-runtime = [ + "frame-support/try-runtime" +] diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index df1ae17df613f..924adea95fd01 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -44,7 +44,8 @@ //! //! ## Usage //! -//! The default Substrate node template declares the [`Executive`](./struct.Executive.html) type in its library. +//! The default Substrate node template declares the [`Executive`](./struct.Executive.html) type in +//! its library. //! //! ### Example //! @@ -185,26 +186,58 @@ where } impl< - System: frame_system::Config, - Block: traits::Block, - Context: Default, - UnsignedValidator, - AllModules: - OnRuntimeUpgrade + - OnInitialize + - OnFinalize + - OffchainWorker, - COnRuntimeUpgrade: OnRuntimeUpgrade, -> Executive + System: frame_system::Config, + Block: traits::Block
, + Context: Default, + UnsignedValidator, + AllModules: OnRuntimeUpgrade + + OnInitialize + + OnFinalize + + OffchainWorker, + COnRuntimeUpgrade: OnRuntimeUpgrade, + > Executive where Block::Extrinsic: Checkable + Codec, - CheckedOf: - Applyable + - GetDispatchInfo, - CallOf: Dispatchable, + CheckedOf: Applyable + GetDispatchInfo, + CallOf: + Dispatchable, OriginOf: From>, - UnsignedValidator: ValidateUnsigned>, + UnsignedValidator: ValidateUnsigned>, { + /// Execute all `OnRuntimeUpgrade` of this runtime, and return the aggregate weight. + pub fn execute_on_runtime_upgrade() -> frame_support::weights::Weight { + let mut weight = 0; + weight = weight.saturating_add( + as OnRuntimeUpgrade>::on_runtime_upgrade(), + ); + weight = weight.saturating_add(COnRuntimeUpgrade::on_runtime_upgrade()); + weight = weight.saturating_add(::on_runtime_upgrade()); + + weight + } + + /// Execute all `OnRuntimeUpgrade` of this runtime, including the pre and post migration checks. + /// + /// This should only be used for testing. + #[cfg(feature = "try-runtime")] + pub fn try_runtime_upgrade() -> Result { + < + (frame_system::Module::, COnRuntimeUpgrade, AllModules) + as + OnRuntimeUpgrade + >::pre_upgrade()?; + + let weight = Self::execute_on_runtime_upgrade(); + + < + (frame_system::Module::, COnRuntimeUpgrade, AllModules) + as + OnRuntimeUpgrade + >::post_upgrade()?; + + Ok(weight) + } + /// Start the execution of a particular block. pub fn initialize_block(header: &System::Header) { sp_io::init_tracing(); @@ -234,10 +267,7 @@ where ) { let mut weight = 0; if Self::runtime_upgraded() { - // System is not part of `AllModules`, so we need to call this manually. - weight = weight.saturating_add( as OnRuntimeUpgrade>::on_runtime_upgrade()); - weight = weight.saturating_add(COnRuntimeUpgrade::on_runtime_upgrade()); - weight = weight.saturating_add(::on_runtime_upgrade()); + weight = weight.saturating_add(Self::execute_on_runtime_upgrade()); } >::initialize( block_number, @@ -320,7 +350,7 @@ where ) { extrinsics.into_iter().for_each(|e| if let Err(e) = Self::apply_extrinsic(e) { let err: &'static str = e.into(); - panic!(err) + panic!("{}", err) }); // post-extrinsics book-keeping diff --git a/frame/grandpa/Cargo.toml b/frame/grandpa/Cargo.toml index c6a76de23e454..39207e10f8f32 100644 --- a/frame/grandpa/Cargo.toml +++ b/frame/grandpa/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-grandpa" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,30 +14,31 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../primitives/application-crypto" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-finality-grandpa = { version = "2.0.0", default-features = false, path = "../../primitives/finality-grandpa" } -sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-authorship = { version = "2.0.0", default-features = false, path = "../authorship" } -pallet-session = { version = "2.0.0", default-features = false, path = "../session" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../primitives/application-crypto" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-finality-grandpa = { version = "3.0.0", default-features = false, path = "../../primitives/finality-grandpa" } +sp-session = { version = "3.0.0", default-features = false, path = "../../primitives/session" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +pallet-authorship = { version = "3.0.0", default-features = false, path = "../authorship" } +pallet-session = { version = "3.0.0", default-features = false, path = "../session" } [dev-dependencies] -frame-benchmarking = { version = "2.0.0", path = "../benchmarking" } -grandpa = { package = "finality-grandpa", version = "0.12.3", features = ["derive-codec"] } -sp-io = { version = "2.0.0", path = "../../primitives/io" } -sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" } -pallet-balances = { version = "2.0.0", path = "../balances" } -pallet-offences = { version = "2.0.0", path = "../offences" } -pallet-staking = { version = "2.0.0", path = "../staking" } -pallet-staking-reward-curve = { version = "2.0.0", path = "../staking/reward-curve" } -pallet-timestamp = { version = "2.0.0", path = "../timestamp" } +frame-benchmarking = { version = "3.0.0", path = "../benchmarking" } +grandpa = { package = "finality-grandpa", version = "0.14.0", features = ["derive-codec"] } +sp-io = { version = "3.0.0", path = "../../primitives/io" } +sp-keyring = { version = "3.0.0", path = "../../primitives/keyring" } +pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-offences = { version = "3.0.0", path = "../offences" } +pallet-staking = { version = "3.0.0", path = "../staking" } +pallet-staking-reward-curve = { version = "3.0.0", path = "../staking/reward-curve" } +pallet-timestamp = { version = "3.0.0", path = "../timestamp" } +sp-election-providers = { version = "3.0.0", path = "../../primitives/election-providers" } [features] default = ["std"] diff --git a/frame/grandpa/src/equivocation.rs b/frame/grandpa/src/equivocation.rs index 593ebf6ba650a..b8bff59d3920c 100644 --- a/frame/grandpa/src/equivocation.rs +++ b/frame/grandpa/src/equivocation.rs @@ -40,7 +40,10 @@ use sp_std::prelude::*; use codec::{self as codec, Decode, Encode}; -use frame_support::{debug, traits::KeyOwnerProofSystem}; +use frame_support::{ + debug, + traits::{Get, KeyOwnerProofSystem}, +}; use sp_finality_grandpa::{EquivocationProof, RoundNumber, SetId}; use sp_runtime::{ transaction_validity::{ @@ -64,6 +67,10 @@ pub trait HandleEquivocation { /// The offence type used for reporting offences on valid equivocation reports. type Offence: GrandpaOffence; + /// The longevity, in blocks, that the equivocation report is valid for. When using the staking + /// pallet this should be equal to the bonding duration (in blocks, not eras). + type ReportLongevity: Get; + /// Report an offence proved by the given reporters. fn report_offence( reporters: Vec, @@ -88,6 +95,7 @@ pub trait HandleEquivocation { impl HandleEquivocation for () { type Offence = GrandpaEquivocationOffence; + type ReportLongevity = (); fn report_offence( _reporters: Vec, @@ -119,11 +127,11 @@ impl HandleEquivocation for () { /// using existing subsystems that are part of frame (type bounds described /// below) and will dispatch to them directly, it's only purpose is to wire all /// subsystems together. -pub struct EquivocationHandler> { - _phantom: sp_std::marker::PhantomData<(I, R, O)>, +pub struct EquivocationHandler> { + _phantom: sp_std::marker::PhantomData<(I, R, L, O)>, } -impl Default for EquivocationHandler { +impl Default for EquivocationHandler { fn default() -> Self { Self { _phantom: Default::default(), @@ -131,7 +139,7 @@ impl Default for EquivocationHandler { } } -impl HandleEquivocation for EquivocationHandler +impl HandleEquivocation for EquivocationHandler where // We use the authorship pallet to fetch the current block author and use // `offchain::SendTransactionTypes` for unsigned extrinsic creation and @@ -140,10 +148,14 @@ where // A system for reporting offences after valid equivocation reports are // processed. R: ReportOffence, + // The longevity (in blocks) that the equivocation report is valid for. When using the staking + // pallet this should be the bonding duration. + L: Get, // The offence type that should be used when reporting. O: GrandpaOffence, { type Offence = O; + type ReportLongevity = L; fn report_offence(reporters: Vec, offence: O) -> Result<(), OffenceError> { R::report_offence(reporters, offence) @@ -190,7 +202,7 @@ pub struct GrandpaTimeSlot { impl frame_support::unsigned::ValidateUnsigned for Module { type Call = Call; fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity { - if let Call::report_equivocation_unsigned(equivocation_proof, _) = call { + if let Call::report_equivocation_unsigned(equivocation_proof, key_owner_proof) = call { // discard equivocation report not coming from the local node match source { TransactionSource::Local | TransactionSource::InBlock => { /* allowed */ } @@ -204,6 +216,11 @@ impl frame_support::unsigned::ValidateUnsigned for Module { } } + // check report staleness + is_known_offence::(equivocation_proof, key_owner_proof)?; + + let longevity = >::ReportLongevity::get(); + ValidTransaction::with_tag_prefix("GrandpaEquivocation") // We assign the maximum priority for any equivocation report. .priority(TransactionPriority::max_value()) @@ -213,6 +230,7 @@ impl frame_support::unsigned::ValidateUnsigned for Module { equivocation_proof.set_id(), equivocation_proof.round(), )) + .longevity(longevity) // We don't propagate this. This can never be included on a remote node. .propagate(false) .build() @@ -223,36 +241,42 @@ impl frame_support::unsigned::ValidateUnsigned for Module { fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> { if let Call::report_equivocation_unsigned(equivocation_proof, key_owner_proof) = call { - // check the membership proof to extract the offender's id - let key = ( - sp_finality_grandpa::KEY_TYPE, - equivocation_proof.offender().clone(), - ); - - let offender = T::KeyOwnerProofSystem::check_proof(key, key_owner_proof.clone()) - .ok_or(InvalidTransaction::BadProof)?; - - // check if the offence has already been reported, - // and if so then we can discard the report. - let time_slot = - >::Offence::new_time_slot( - equivocation_proof.set_id(), - equivocation_proof.round(), - ); - - let is_known_offence = T::HandleEquivocation::is_known_offence(&[offender], &time_slot); - - if is_known_offence { - Err(InvalidTransaction::Stale.into()) - } else { - Ok(()) - } + is_known_offence::(equivocation_proof, key_owner_proof) } else { Err(InvalidTransaction::Call.into()) } } } +fn is_known_offence( + equivocation_proof: &EquivocationProof, + key_owner_proof: &T::KeyOwnerProof, +) -> Result<(), TransactionValidityError> { + // check the membership proof to extract the offender's id + let key = ( + sp_finality_grandpa::KEY_TYPE, + equivocation_proof.offender().clone(), + ); + + let offender = T::KeyOwnerProofSystem::check_proof(key, key_owner_proof.clone()) + .ok_or(InvalidTransaction::BadProof)?; + + // check if the offence has already been reported, + // and if so then we can discard the report. + let time_slot = >::Offence::new_time_slot( + equivocation_proof.set_id(), + equivocation_proof.round(), + ); + + let is_known_offence = T::HandleEquivocation::is_known_offence(&[offender], &time_slot); + + if is_known_offence { + Err(InvalidTransaction::Stale.into()) + } else { + Ok(()) + } +} + /// A grandpa equivocation offence report. #[allow(dead_code)] pub struct GrandpaEquivocationOffence { diff --git a/frame/grandpa/src/lib.rs b/frame/grandpa/src/lib.rs index 078acbaa57561..b68624df7b5dc 100644 --- a/frame/grandpa/src/lib.rs +++ b/frame/grandpa/src/lib.rs @@ -41,7 +41,7 @@ use fg_primitives::{ }; use frame_support::{ decl_error, decl_event, decl_module, decl_storage, dispatch::DispatchResultWithPostInfo, - storage, traits::KeyOwnerProofSystem, weights::{Pays, Weight}, Parameter, + storage, traits::{OneSessionHandler, KeyOwnerProofSystem}, weights::{Pays, Weight}, Parameter, }; use frame_system::{ensure_none, ensure_root, ensure_signed}; use sp_runtime::{ @@ -587,7 +587,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = AuthorityId; } -impl pallet_session::OneSessionHandler for Module +impl OneSessionHandler for Module where T: pallet_session::Config { type Key = AuthorityId; diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index fd7230fd9ceb0..0a24a2344547e 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -19,18 +19,17 @@ #![cfg(test)] -use crate::{AuthorityId, AuthorityList, ConsensusLog, Module, Config}; +use crate::{AuthorityId, AuthorityList, ConsensusLog, Config, self as pallet_grandpa}; use ::grandpa as finality_grandpa; use codec::Encode; use frame_support::{ - impl_outer_dispatch, impl_outer_event, impl_outer_origin, parameter_types, + parameter_types, traits::{KeyOwnerProofSystem, OnFinalize, OnInitialize}, weights::Weight, }; use pallet_staking::EraIndex; use sp_core::{crypto::KeyTypeId, H256}; use sp_finality_grandpa::{RoundNumber, SetId, GRANDPA_ENGINE_ID}; -use sp_io; use sp_keyring::Ed25519Keyring; use sp_runtime::{ curve::PiecewiseLinear, @@ -40,17 +39,28 @@ use sp_runtime::{ DigestItem, Perbill, }; use sp_staking::SessionIndex; - -impl_outer_origin! { - pub enum Origin for Test {} -} - -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - pallet_grandpa::Grandpa, - pallet_staking::Staking, +use pallet_session::historical as pallet_session_historical; +use sp_election_providers::onchain; + +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, + Session: pallet_session::{Module, Call, Storage, Event, Config}, + Grandpa: pallet_grandpa::{Module, Call, Storage, Config, Event, ValidateUnsigned}, + Offences: pallet_offences::{Module, Call, Storage, Event}, + Historical: pallet_session_historical::{Module}, } -} +); impl_opaque_keys! { pub struct TestSessionKeys { @@ -58,20 +68,6 @@ impl_opaque_keys! { } } -impl_outer_event! { - pub enum TestEvent for Test { - frame_system, - pallet_balances, - pallet_grandpa, - pallet_offences, - pallet_session, - pallet_staking, - } -} - -#[derive(Clone, Eq, PartialEq)] -pub struct Test; - parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -92,10 +88,10 @@ impl frame_system::Config for Test { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = TestEvent; + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -119,7 +115,7 @@ parameter_types! { /// Custom `SessionHandler` since we use `TestSessionKeys` as `Keys`. impl pallet_session::Config for Test { - type Event = TestEvent; + type Event = Event; type ValidatorId = u64; type ValidatorIdOf = pallet_staking::StashOf; type ShouldEndSession = pallet_session::PeriodicSessions; @@ -155,7 +151,7 @@ impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u128; type DustRemoval = (); - type Event = TestEvent; + type Event = Event; type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; type WeightInfo = (); @@ -194,10 +190,17 @@ parameter_types! { pub const StakingUnsignedPriority: u64 = u64::max_value() / 2; } +impl onchain::Config for Test { + type AccountId = ::AccountId; + type BlockNumber = ::BlockNumber; + type Accuracy = Perbill; + type DataProvider = Staking; +} + impl pallet_staking::Config for Test { type RewardRemainder = (); type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote; - type Event = TestEvent; + type Event = Event; type Currency = Balances; type Slash = (); type Reward = (); @@ -216,6 +219,7 @@ impl pallet_staking::Config for Test { type MaxIterations = (); type MinSolutionScoreBump = (); type OffchainSolutionWeightLimit = (); + type ElectionProvider = onchain::OnChainSequentialPhragmen; type WeightInfo = (); } @@ -224,14 +228,19 @@ parameter_types! { } impl pallet_offences::Config for Test { - type Event = TestEvent; + type Event = Event; type IdentificationTuple = pallet_session::historical::IdentificationTuple; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; } +parameter_types! { + pub const ReportLongevity: u64 = + BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * Period::get(); +} + impl Config for Test { - type Event = TestEvent; + type Event = Event; type Call = Call; type KeyOwnerProofSystem = Historical; @@ -244,24 +253,12 @@ impl Config for Test { AuthorityId, )>>::IdentificationTuple; - type HandleEquivocation = super::EquivocationHandler; + type HandleEquivocation = + super::EquivocationHandler; type WeightInfo = (); } -mod pallet_grandpa { - pub use crate::Event; -} - -pub type Balances = pallet_balances::Module; -pub type Historical = pallet_session::historical::Module; -pub type Offences = pallet_offences::Module; -pub type Session = pallet_session::Module; -pub type Staking = pallet_staking::Module; -pub type System = frame_system::Module; -pub type Timestamp = pallet_timestamp::Module; -pub type Grandpa = Module; - pub fn grandpa_log(log: ConsensusLog) -> DigestItem { DigestItem::Consensus(GRANDPA_ENGINE_ID, log.encode()) } diff --git a/frame/grandpa/src/tests.rs b/frame/grandpa/src/tests.rs index 0e2a458a3dfe1..50462d33472a9 100644 --- a/frame/grandpa/src/tests.rs +++ b/frame/grandpa/src/tests.rs @@ -19,17 +19,16 @@ #![cfg(test)] -use super::{Call, *}; +use super::{Call, Event, *}; use crate::mock::*; use codec::{Decode, Encode}; use fg_primitives::ScheduledChange; use frame_support::{ assert_err, assert_ok, - traits::{Currency, OnFinalize}, + traits::{Currency, OnFinalize, OneSessionHandler}, weights::{GetDispatchInfo, Pays}, }; use frame_system::{EventRecord, Phase}; -use pallet_session::OneSessionHandler; use sp_core::H256; use sp_keyring::Ed25519Keyring; use sp_runtime::testing::Digest; @@ -707,8 +706,8 @@ fn report_equivocation_invalid_equivocation_proof() { #[test] fn report_equivocation_validate_unsigned_prevents_duplicates() { use sp_runtime::transaction_validity::{ - InvalidTransaction, TransactionLongevity, TransactionPriority, TransactionSource, - TransactionValidity, ValidTransaction, + InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity, + ValidTransaction, }; let authorities = test_authorities(); @@ -763,7 +762,7 @@ fn report_equivocation_validate_unsigned_prevents_duplicates() { priority: TransactionPriority::max_value(), requires: vec![], provides: vec![("GrandpaEquivocation", tx_tag).encode()], - longevity: TransactionLongevity::max_value(), + longevity: ReportLongevity::get(), propagate: false, }) ); @@ -776,6 +775,15 @@ fn report_equivocation_validate_unsigned_prevents_duplicates() { .unwrap(); // the report should now be considered stale and the transaction is invalid + // the check for staleness should be done on both `validate_unsigned` and on `pre_dispatch` + assert_err!( + ::validate_unsigned( + TransactionSource::Local, + &call, + ), + InvalidTransaction::Stale, + ); + assert_err!( ::pre_dispatch(&call), InvalidTransaction::Stale, diff --git a/frame/identity/Cargo.toml b/frame/identity/Cargo.toml index 124ac4f006448..3fd0c30a0f83b 100644 --- a/frame/identity/Cargo.toml +++ b/frame/identity/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-identity" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,18 +14,18 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } enumflags2 = { version = "0.6.2" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -pallet-balances = { version = "2.0.0", path = "../balances" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/identity/src/benchmarking.rs b/frame/identity/src/benchmarking.rs index e916bdfa50461..645b3817d6ec9 100644 --- a/frame/identity/src/benchmarking.rs +++ b/frame/identity/src/benchmarking.rs @@ -22,7 +22,7 @@ use super::*; use frame_system::{EventRecord, RawOrigin}; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use sp_runtime::traits::Bounded; use crate::Module as Identity; @@ -403,31 +403,8 @@ benchmarks! { } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_add_registrar::()); - assert_ok!(test_benchmark_set_identity::()); - assert_ok!(test_benchmark_set_subs_new::()); - assert_ok!(test_benchmark_set_subs_old::()); - assert_ok!(test_benchmark_clear_identity::()); - assert_ok!(test_benchmark_request_judgement::()); - assert_ok!(test_benchmark_cancel_request::()); - assert_ok!(test_benchmark_set_fee::()); - assert_ok!(test_benchmark_set_account_id::()); - assert_ok!(test_benchmark_set_fields::()); - assert_ok!(test_benchmark_provide_judgement::()); - assert_ok!(test_benchmark_kill_identity::()); - assert_ok!(test_benchmark_add_sub::()); - assert_ok!(test_benchmark_rename_sub::()); - assert_ok!(test_benchmark_remove_sub::()); - assert_ok!(test_benchmark_quit_sub::()); - }); - } -} +impl_benchmark_test_suite!( + Identity, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/identity/src/tests.rs b/frame/identity/src/tests.rs index 0ac3c93a75b01..230079a21ea0d 100644 --- a/frame/identity/src/tests.rs +++ b/frame/identity/src/tests.rs @@ -18,24 +18,31 @@ // Tests for Identity Pallet use super::*; +use crate as pallet_identity; use sp_runtime::traits::BadOrigin; -use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, parameter_types, - ord_parameter_types, -}; +use frame_support::{assert_ok, assert_noop, parameter_types, ord_parameter_types}; use sp_core::H256; use frame_system::{EnsureSignedBy, EnsureOneOf, EnsureRoot}; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, }; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Identity: pallet_identity::{Module, Call, Storage, Event}, + } +); -#[derive(Clone, Eq, PartialEq)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -49,16 +56,16 @@ impl frame_system::Config for Test { type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = (); + type Call = Call; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type DbWeight = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -70,7 +77,7 @@ parameter_types! { } impl pallet_balances::Config for Test { type Balance = u64; - type Event = (); + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -100,7 +107,7 @@ type EnsureTwoOrRoot = EnsureOneOf< EnsureSignedBy >; impl Config for Test { - type Event = (); + type Event = Event; type Currency = Balances; type Slashed = (); type BasicDeposit = BasicDeposit; @@ -113,9 +120,6 @@ impl Config for Test { type ForceOrigin = EnsureTwoOrRoot; type WeightInfo = (); } -type System = frame_system::Module; -type Balances = pallet_balances::Module; -type Identity = Module; pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index 95948c86de490..bde041c43764a 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-im-online" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,23 +13,25 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../primitives/application-crypto" } -pallet-authorship = { version = "2.0.0", default-features = false, path = "../authorship" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../primitives/application-crypto" } +pallet-authorship = { version = "3.0.0", default-features = false, path = "../authorship" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } serde = { version = "1.0.101", optional = true } -pallet-session = { version = "2.0.0", default-features = false, path = "../session" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } + +[dev-dependencies] +pallet-session = { version = "3.0.0", path = "../session" } [features] -default = ["std", "pallet-session/historical"] +default = ["std"] std = [ "sp-application-crypto/std", "pallet-authorship/std", @@ -37,7 +39,6 @@ std = [ "sp-core/std", "sp-std/std", "serde", - "pallet-session/std", "sp-io/std", "sp-runtime/std", "sp-staking/std", diff --git a/frame/im-online/src/benchmarking.rs b/frame/im-online/src/benchmarking.rs index ef7f66307a99d..287a2c6fd3a73 100644 --- a/frame/im-online/src/benchmarking.rs +++ b/frame/im-online/src/benchmarking.rs @@ -22,7 +22,7 @@ use super::*; use frame_system::RawOrigin; -use frame_benchmarking::benchmarks; +use frame_benchmarking::{benchmarks, impl_benchmark_test_suite}; use sp_core::OpaquePeerId; use sp_core::offchain::OpaqueMultiaddr; use sp_runtime::traits::{ValidateUnsigned, Zero}; @@ -91,18 +91,9 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::mock::{new_test_ext, Runtime}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_heartbeat::()); - assert_ok!(test_benchmark_validate_unsigned::()); - assert_ok!(test_benchmark_validate_unsigned_and_then_heartbeat::()); - }); - } -} + +impl_benchmark_test_suite!( + ImOnline, + crate::mock::new_test_ext(), + crate::mock::Runtime, +); diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 71ee25d779bdd..bd597acfb1ed5 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -79,7 +79,6 @@ use codec::{Encode, Decode}; use sp_core::offchain::OpaqueNetworkState; use sp_std::prelude::*; use sp_std::convert::TryInto; -use pallet_session::historical::IdentificationTuple; use sp_runtime::{ offchain::storage::StorageValueRef, RuntimeDebug, @@ -95,7 +94,7 @@ use sp_staking::{ }; use frame_support::{ decl_module, decl_event, decl_storage, Parameter, debug, decl_error, - traits::Get, + traits::{Get, ValidatorSet, ValidatorSetWithIdentification, OneSessionHandler}, }; use frame_system::ensure_none; use frame_system::offchain::{ @@ -227,7 +226,19 @@ pub struct Heartbeat pub validators_len: u32, } -pub trait Config: SendTransactionTypes> + pallet_session::historical::Config { +/// A type for representing the validator id in a session. +pub type ValidatorId = < + ::ValidatorSet as ValidatorSet<::AccountId> +>::ValidatorId; + +/// A tuple of (ValidatorId, Identification) where `Identification` is the full identification of `ValidatorId`. +pub type IdentificationTuple = ( + ValidatorId, + <::ValidatorSet as + ValidatorSetWithIdentification<::AccountId>>::Identification, +); + +pub trait Config: SendTransactionTypes> + frame_system::Config { /// The identifier type for an authority. type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + Ord; @@ -242,6 +253,9 @@ pub trait Config: SendTransactionTypes> + pallet_session::historical: /// there is a chance the authority will produce a block and they won't be necessary. type SessionDuration: Get; + /// A type for retrieving the validators supposed to be online in a session. + type ValidatorSet: ValidatorSetWithIdentification; + /// A type that gives us the ability to submit unresponsiveness offence reports. type ReportUnresponsiveness: ReportOffence< @@ -293,10 +307,10 @@ decl_storage! { double_map hasher(twox_64_concat) SessionIndex, hasher(twox_64_concat) AuthIndex => Option>; - /// For each session index, we keep a mapping of `T::ValidatorId` to the + /// For each session index, we keep a mapping of `ValidatorId` to the /// number of blocks authored by the given authority. AuthoredBlocks get(fn authored_blocks): - double_map hasher(twox_64_concat) SessionIndex, hasher(twox_64_concat) T::ValidatorId + double_map hasher(twox_64_concat) SessionIndex, hasher(twox_64_concat) ValidatorId => u32; } add_extra_genesis { @@ -345,7 +359,7 @@ decl_module! { ) { ensure_none(origin)?; - let current_session = >::current_index(); + let current_session = T::ValidatorSet::session_index(); let exists = ::contains_key( ¤t_session, &heartbeat.authority_index @@ -397,12 +411,15 @@ type OffchainResult = Result::B /// Keep track of number of authored blocks per authority, uncles are counted as /// well since they're a valid proof of being online. -impl pallet_authorship::EventHandler for Module { - fn note_author(author: T::ValidatorId) { +impl< + T: Config + pallet_authorship::Config, +> pallet_authorship::EventHandler, T::BlockNumber> for Module +{ + fn note_author(author: ValidatorId) { Self::note_authorship(author); } - fn note_uncle(author: T::ValidatorId, _age: T::BlockNumber) { + fn note_uncle(author: ValidatorId, _age: T::BlockNumber) { Self::note_authorship(author); } } @@ -413,7 +430,7 @@ impl Module { /// authored at least one block, during the current session. Otherwise /// `false`. pub fn is_online(authority_index: AuthIndex) -> bool { - let current_validators = >::validators(); + let current_validators = T::ValidatorSet::validators(); if authority_index >= current_validators.len() as u32 { return false; @@ -424,8 +441,8 @@ impl Module { Self::is_online_aux(authority_index, authority) } - fn is_online_aux(authority_index: AuthIndex, authority: &T::ValidatorId) -> bool { - let current_session = >::current_index(); + fn is_online_aux(authority_index: AuthIndex, authority: &ValidatorId) -> bool { + let current_session = T::ValidatorSet::session_index(); ::contains_key(¤t_session, &authority_index) || >::get( @@ -437,13 +454,13 @@ impl Module { /// Returns `true` if a heartbeat has been received for the authority at `authority_index` in /// the authorities series, during the current session. Otherwise `false`. pub fn received_heartbeat_in_current_session(authority_index: AuthIndex) -> bool { - let current_session = >::current_index(); + let current_session = T::ValidatorSet::session_index(); ::contains_key(¤t_session, &authority_index) } /// Note that the given authority has authored a block in the current session. - fn note_authorship(author: T::ValidatorId) { - let current_session = >::current_index(); + fn note_authorship(author: ValidatorId) { + let current_session = T::ValidatorSet::session_index(); >::mutate( ¤t_session, @@ -460,8 +477,8 @@ impl Module { return Err(OffchainErr::TooEarly(heartbeat_after)) } - let session_index = >::current_index(); - let validators_len = >::validators().len() as u32; + let session_index = T::ValidatorSet::session_index(); + let validators_len = Keys::::decode_len().unwrap_or_default() as u32; Ok(Self::local_authority_keys() .map(move |(authority_index, key)| @@ -614,7 +631,7 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { type Public = T::AuthorityId; } -impl pallet_session::OneSessionHandler for Module { +impl OneSessionHandler for Module { type Key = T::AuthorityId; fn on_genesis_session<'a, I: 'a>(validators: I) @@ -639,22 +656,24 @@ impl pallet_session::OneSessionHandler for Module { } fn on_before_session_ending() { - let session_index = >::current_index(); + let session_index = T::ValidatorSet::session_index(); let keys = Keys::::get(); - let current_validators = >::validators(); + let current_validators = T::ValidatorSet::validators(); let offenders = current_validators.into_iter().enumerate() .filter(|(index, id)| !Self::is_online_aux(*index as u32, id) ).filter_map(|(_, id)| - T::FullIdentificationOf::convert(id.clone()).map(|full_id| (id, full_id)) + >::IdentificationOf::convert( + id.clone() + ).map(|full_id| (id, full_id)) ).collect::>>(); // Remove all received heartbeats and number of authored blocks from the // current session, they have already been processed and won't be needed // anymore. - ::remove_prefix(&>::current_index()); - >::remove_prefix(&>::current_index()); + ::remove_prefix(&T::ValidatorSet::session_index()); + >::remove_prefix(&T::ValidatorSet::session_index()); if offenders.is_empty() { Self::deposit_event(RawEvent::AllGood); @@ -691,7 +710,7 @@ impl frame_support::unsigned::ValidateUnsigned for Module { } // check if session index from heartbeat is recent - let current_session = >::current_index(); + let current_session = T::ValidatorSet::session_index(); if heartbeat.session_index != current_session { return InvalidTransaction::Stale.into(); } diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index 624014cd55f78..1b80f5b12dedb 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -21,23 +21,31 @@ use std::cell::RefCell; -use crate::{Module, Config}; +use crate::Config; use sp_runtime::Perbill; use sp_staking::{SessionIndex, offence::{ReportOffence, OffenceError}}; use sp_runtime::testing::{Header, UintAuthorityId, TestXt}; use sp_runtime::traits::{IdentityLookup, BlakeTwo256, ConvertInto}; use sp_core::H256; -use frame_support::{impl_outer_origin, impl_outer_dispatch, parameter_types}; - -impl_outer_origin!{ - pub enum Origin for Runtime {} -} - -impl_outer_dispatch! { - pub enum Call for Runtime where origin: Origin { - imonline::ImOnline, +use frame_support::parameter_types; +use crate as imonline; +use pallet_session::historical as pallet_session_historical; + +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Runtime where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Session: pallet_session::{Module, Call, Storage, Event, Config}, + ImOnline: imonline::{Module, Call, Storage, Config, Event}, + Historical: pallet_session_historical::{Module}, } -} +); thread_local! { pub static VALIDATORS: RefCell>> = RefCell::new(Some(vec![ @@ -99,9 +107,6 @@ pub fn new_test_ext() -> sp_io::TestExternalities { t.into() } -#[derive(Clone, PartialEq, Eq, Debug)] -pub struct Runtime; - parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -122,10 +127,10 @@ impl frame_system::Config for Runtime { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -149,7 +154,7 @@ impl pallet_session::Config for Runtime { type ValidatorId = u64; type ValidatorIdOf = ConvertInto; type Keys = UintAuthorityId; - type Event = (); + type Event = Event; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type NextSessionRotation = pallet_session::PeriodicSessions; type WeightInfo = (); @@ -177,8 +182,9 @@ parameter_types! { impl Config for Runtime { type AuthorityId = UintAuthorityId; - type Event = (); + type Event = Event; type ReportUnresponsiveness = OffenceHandler; + type ValidatorSet = Historical; type SessionDuration = Period; type UnsignedPriority = UnsignedPriority; type WeightInfo = (); @@ -191,11 +197,6 @@ impl frame_system::offchain::SendTransactionTypes for Runt type Extrinsic = Extrinsic; } -/// Im Online module. -pub type ImOnline = Module; -pub type System = frame_system::Module; -pub type Session = pallet_session::Module; - pub fn advance_session() { let now = System::block_number().max(1); System::set_block_number(now + 1); diff --git a/frame/indices/Cargo.toml b/frame/indices/Cargo.toml index afe315cfaa6b3..cde3cdeeecba5 100644 --- a/frame/indices/Cargo.toml +++ b/frame/indices/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-indices" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,19 +14,19 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-keyring = { version = "2.0.0", optional = true, path = "../../primitives/keyring" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-keyring = { version = "3.0.0", optional = true, path = "../../primitives/keyring" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -pallet-balances = { version = "2.0.0", path = "../balances" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/indices/src/benchmarking.rs b/frame/indices/src/benchmarking.rs index f83e05ee9c627..6ea39e9ccc23e 100644 --- a/frame/indices/src/benchmarking.rs +++ b/frame/indices/src/benchmarking.rs @@ -21,7 +21,7 @@ use super::*; use frame_system::RawOrigin; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use sp_runtime::traits::Bounded; use crate::Module as Indices; @@ -93,20 +93,9 @@ benchmarks! { // TODO in another PR: lookup and unlookup trait weights (not critical) } -#[cfg(test)] -mod tests { - use super::*; - use crate::mock::{new_test_ext, Test}; - use frame_support::assert_ok; - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_claim::()); - assert_ok!(test_benchmark_transfer::()); - assert_ok!(test_benchmark_free::()); - assert_ok!(test_benchmark_force_transfer::()); - assert_ok!(test_benchmark_freeze::()); - }); - } -} +impl_benchmark_test_suite!( + Indices, + crate::mock::new_test_ext(), + crate::mock::Test, +); diff --git a/frame/lottery/Cargo.toml b/frame/lottery/Cargo.toml index b223625c87a02..05bb7e385f5dd 100644 --- a/frame/lottery/Cargo.toml +++ b/frame/lottery/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-lottery" -version = "2.0.0" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,18 +13,19 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -pallet-balances = { version = "2.0.0", path = "../balances" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-io = { version = "2.0.0", path = "../../primitives/io" } +pallet-balances = { version = "3.0.0", path = "../balances" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } +serde = { version = "1.0.101" } [features] default = ["std"] diff --git a/frame/lottery/src/benchmarking.rs b/frame/lottery/src/benchmarking.rs index b9b0d7fd0002c..a2b8946ecc49a 100644 --- a/frame/lottery/src/benchmarking.rs +++ b/frame/lottery/src/benchmarking.rs @@ -23,7 +23,7 @@ use super::*; use frame_system::RawOrigin; use frame_support::traits::{OnInitialize, UnfilteredDispatchable}; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use sp_runtime::traits::{Bounded, Zero}; use crate::Module as Lottery; @@ -170,21 +170,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::mock::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_buy_ticket::()); - assert_ok!(test_benchmark_set_calls::()); - assert_ok!(test_benchmark_start_lottery::()); - assert_ok!(test_benchmark_stop_repeat::()); - assert_ok!(test_benchmark_on_initialize_end::()); - assert_ok!(test_benchmark_on_initialize_repeat::()); - }); - } -} +impl_benchmark_test_suite!( + Lottery, + crate::mock::new_test_ext(), + crate::mock::Test, +); diff --git a/frame/lottery/src/mock.rs b/frame/lottery/src/mock.rs index 0f25e9fc7facc..ea73ee190e6dd 100644 --- a/frame/lottery/src/mock.rs +++ b/frame/lottery/src/mock.rs @@ -18,9 +18,10 @@ //! Test utilities use super::*; +use crate as pallet_lottery; use frame_support::{ - impl_outer_origin, impl_outer_dispatch, parameter_types, + parameter_types, traits::{OnInitialize, OnFinalize, TestRandomness}, }; use sp_core::H256; @@ -31,19 +32,21 @@ use sp_runtime::{ }; use frame_system::EnsureRoot; -impl_outer_origin! { - pub enum Origin for Test {} -} - -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - frame_system::System, - pallet_balances::Balances, +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Lottery: pallet_lottery::{Module, Call, Storage, Event}, } -} +); -#[derive(Clone, Eq, PartialEq)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub const MaximumBlockWeight: u32 = 1024; @@ -65,10 +68,10 @@ impl frame_system::Config for Test { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -83,7 +86,7 @@ parameter_types! { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; - type Event = (); + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -101,7 +104,7 @@ impl Config for Test { type Call = Call; type Currency = Balances; type Randomness = TestRandomness; - type Event = (); + type Event = Event; type ManagerOrigin = EnsureRoot; type MaxCalls = MaxCalls; type ValidateCall = Lottery; @@ -109,10 +112,6 @@ impl Config for Test { type WeightInfo = (); } -pub type Lottery = Module; -pub type System = frame_system::Module; -pub type Balances = pallet_balances::Module; - pub type SystemCall = frame_system::Call; pub type BalancesCall = pallet_balances::Call; diff --git a/frame/membership/Cargo.toml b/frame/membership/Cargo.toml index f1ce20df17ed3..98987e6fe901b 100644 --- a/frame/membership/Cargo.toml +++ b/frame/membership/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-membership" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,15 +14,15 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } [features] default = ["std"] diff --git a/frame/membership/src/lib.rs b/frame/membership/src/lib.rs index a43a5b4089f19..f080938095445 100644 --- a/frame/membership/src/lib.rs +++ b/frame/membership/src/lib.rs @@ -277,21 +277,27 @@ impl, I: Instance> Contains for Module { #[cfg(test)] mod tests { use super::*; + use crate as pallet_membership; - use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, parameter_types, - ord_parameter_types - }; + use frame_support::{assert_ok, assert_noop, parameter_types, ord_parameter_types}; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup, BadOrigin}, testing::Header}; use frame_system::EnsureSignedBy; - impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} - } + type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; + type Block = frame_system::mocking::MockBlock; + + frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Membership: pallet_membership::{Module, Call, Storage, Config, Event}, + } + ); - #[derive(Clone, Eq, PartialEq)] - pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -308,15 +314,15 @@ mod tests { type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = (); + type Call = Call; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -356,7 +362,7 @@ mod tests { } impl Config for Test { - type Event = (); + type Event = Event; type AddOrigin = EnsureSignedBy; type RemoveOrigin = EnsureSignedBy; type SwapOrigin = EnsureSignedBy; @@ -366,12 +372,10 @@ mod tests { type MembershipChanged = TestChangeMembers; } - type Membership = Module; - fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); // We use default for brevity, but you can configure as desired if needed. - GenesisConfig::{ + pallet_membership::GenesisConfig::{ members: vec![10, 20, 30], .. Default::default() }.assimilate_storage(&mut t).unwrap(); diff --git a/frame/merkle-mountain-range/Cargo.toml b/frame/merkle-mountain-range/Cargo.toml index 096333680c6ad..eea3845ae16d3 100644 --- a/frame/merkle-mountain-range/Cargo.toml +++ b/frame/merkle-mountain-range/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-mmr" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -12,19 +12,20 @@ description = "FRAME Merkle Mountain Range pallet." targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } mmr-lib = { package = "ckb-merkle-mountain-range", default-features = false, version = "0.3.1" } +pallet-mmr-primitives = { version = "3.0.0", default-features = false, path = "./primitives" } serde = { version = "1.0.101", optional = true } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } [dev-dependencies] -env_logger = "0.5" +env_logger = "0.8" hex-literal = "0.3" [features] @@ -35,6 +36,7 @@ std = [ "frame-support/std", "frame-system/std", "mmr-lib/std", + "pallet-mmr-primitives/std", "serde", "sp-core/std", "sp-io/std", diff --git a/frame/merkle-mountain-range/primitives/Cargo.toml b/frame/merkle-mountain-range/primitives/Cargo.toml new file mode 100644 index 0000000000000..be0a8bdc3a2bb --- /dev/null +++ b/frame/merkle-mountain-range/primitives/Cargo.toml @@ -0,0 +1,38 @@ +[package] +name = "pallet-mmr-primitives" +version = "3.0.0" +authors = ["Parity Technologies "] +edition = "2018" +license = "Apache-2.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" +description = "FRAME Merkle Mountain Range primitives." + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../../system" } +serde = { version = "1.0.101", optional = true, features = ["derive"] } +sp-api = { version = "3.0.0", default-features = false, path = "../../../primitives/api" } +sp-core = { version = "3.0.0", default-features = false, path = "../../../primitives/core" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../../primitives/std" } + +[dev-dependencies] +hex-literal = "0.3" + +[features] +default = ["std"] +std = [ + "codec/std", + "frame-support/std", + "frame-system/std", + "serde", + "sp-api/std", + "sp-core/std", + "sp-runtime/std", + "sp-std/std", +] diff --git a/frame/merkle-mountain-range/src/primitives.rs b/frame/merkle-mountain-range/primitives/src/lib.rs similarity index 70% rename from frame/merkle-mountain-range/src/primitives.rs rename to frame/merkle-mountain-range/primitives/src/lib.rs index 4d13a32c89f81..d57f8565b6081 100644 --- a/frame/merkle-mountain-range/src/primitives.rs +++ b/frame/merkle-mountain-range/primitives/src/lib.rs @@ -17,8 +17,11 @@ //! Merkle Mountain Range primitive types. -use frame_support::RuntimeDebug; -use sp_runtime::traits; +#![cfg_attr(not(feature = "std"), no_std)] +#![warn(missing_docs)] + +use frame_support::{RuntimeDebug, debug}; +use sp_runtime::traits::{self, Saturating, One}; use sp_std::fmt; #[cfg(not(feature = "std"))] use sp_std::prelude::Vec; @@ -26,7 +29,7 @@ use sp_std::prelude::Vec; /// A provider of the MMR's leaf data. pub trait LeafDataProvider { /// A type that should end up in the leaf of MMR. - type LeafData: FullLeaf; + type LeafData: FullLeaf + codec::Decode; /// The method to return leaf data that should be placed /// in the leaf node appended MMR at this block. @@ -47,14 +50,21 @@ impl LeafDataProvider for () { /// The most common use case for MMRs is to store historical block hashes, /// so that any point in time in the future we can receive a proof about some past /// blocks without using excessive on-chain storage. -/// Hence we implement the [LeafDataProvider] for [frame_system::Module], since the +/// +/// Hence we implement the [LeafDataProvider] for [frame_system::Module]. Since the /// current block hash is not available (since the block is not finished yet), -/// we use the `parent_hash` here. +/// we use the `parent_hash` here along with parent block number. impl LeafDataProvider for frame_system::Module { - type LeafData = ::Hash; + type LeafData = ( + ::BlockNumber, + ::Hash + ); fn leaf_data() -> Self::LeafData { - Self::parent_hash() + ( + Self::block_number().saturating_sub(One::one()), + Self::parent_hash() + ) } } @@ -70,7 +80,7 @@ impl OnNewRoot for () { } /// A full leaf content stored in the offchain-db. -pub trait FullLeaf: Clone + PartialEq + fmt::Debug + codec::Decode { +pub trait FullLeaf: Clone + PartialEq + fmt::Debug { /// Encode the leaf either in it's full or compact form. /// /// NOTE the encoding returned here MUST be `Decode`able into `FullLeaf`. @@ -117,7 +127,7 @@ mod encoding { } impl codec::Encode for DataOrHash { - fn encode_to(&self, dest: &mut T) { + fn encode_to(&self, dest: &mut T) { match self { Self::Data(l) => l.using_encoded( |data| Either::<&[u8], &H::Output>::Left(data).encode_to(dest), false @@ -127,7 +137,7 @@ mod encoding { } } - impl codec::Decode for DataOrHash { + impl codec::Decode for DataOrHash { fn decode(value: &mut I) -> Result { let decoded: Either, H::Output> = Either::decode(value)?; Ok(match decoded { @@ -164,6 +174,7 @@ impl DataOrHash { /// you don't care about with their hashes. #[derive(RuntimeDebug, Clone, PartialEq)] pub struct Compact { + /// Internal tuple representation. pub tuple: T, _hash: sp_std::marker::PhantomData, } @@ -177,6 +188,7 @@ impl sp_std::ops::Deref for Compact { } impl Compact { + /// Create a new [Compact] wrapper for a tuple. pub fn new(tuple: T) -> Self { Self { tuple, _hash: Default::default() } } @@ -274,15 +286,114 @@ pub struct Proof { pub items: Vec, } +/// Merkle Mountain Range operation error. +#[derive(RuntimeDebug, codec::Encode, codec::Decode, PartialEq, Eq)] +pub enum Error { + /// Error while pushing new node. + Push, + /// Error getting the new root. + GetRoot, + /// Error commiting changes. + Commit, + /// Error during proof generation. + GenerateProof, + /// Proof verification error. + Verify, + /// Leaf not found in the storage. + LeafNotFound, +} + +impl Error { + #![allow(unused_variables)] + /// Consume given error `e` with `self` and generate a native log entry with error details. + pub fn log_error(self, e: impl fmt::Debug) -> Self { + debug::native::error!("[{:?}] MMR error: {:?}", self, e); + self + } + + /// Consume given error `e` with `self` and generate a native log entry with error details. + pub fn log_debug(self, e: impl fmt::Debug) -> Self { + debug::native::debug!("[{:?}] MMR error: {:?}", self, e); + self + } +} + +/// A helper type to allow using arbitrary SCALE-encoded leaf data in the RuntimeApi. +/// +/// The point is to be able to verify MMR proofs from external MMRs, where we don't +/// know the exact leaf type, but it's enough for us to have it SCALE-encoded. +/// +/// Note the leaf type should be encoded in its compact form when passed through this type. +/// See [FullLeaf] documentation for details. +/// +/// This type does not implement SCALE encoding/decoding on purpose to avoid confusion, +/// it would have to be SCALE-compatible with the concrete leaf type, but due to SCALE limitations +/// it's not possible to know how many bytes the encoding of concrete leaf type uses. +#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[derive(RuntimeDebug, Clone, PartialEq)] +pub struct OpaqueLeaf( + /// Raw bytes of the leaf type encoded in its compact form. + /// + /// NOTE it DOES NOT include length prefix (like `Vec` encoding would). + #[cfg_attr(feature = "std", serde(with = "sp_core::bytes"))] + pub Vec +); + +impl OpaqueLeaf { + /// Convert a concrete MMR leaf into an opaque type. + pub fn from_leaf(leaf: &T) -> Self { + let encoded_leaf = leaf.using_encoded(|d| d.to_vec(), true); + OpaqueLeaf::from_encoded_leaf(encoded_leaf) + } + + /// Create a `OpaqueLeaf` given raw bytes of compact-encoded leaf. + pub fn from_encoded_leaf(encoded_leaf: Vec) -> Self { + OpaqueLeaf(encoded_leaf) + } +} + +impl FullLeaf for OpaqueLeaf { + fn using_encoded R>(&self, f: F, _compact: bool) -> R { + f(&self.0) + } +} + +sp_api::decl_runtime_apis! { + /// API to interact with MMR pallet. + pub trait MmrApi { + /// Generate MMR proof for a leaf under given index. + fn generate_proof(leaf_index: u64) -> Result<(Leaf, Proof), Error>; + + /// Verify MMR proof against on-chain MMR. + /// + /// Note this function will use on-chain MMR root hash and check if the proof + /// matches the hash. + /// See [Self::verify_proof_stateless] for a stateless verifier. + fn verify_proof(leaf: Leaf, proof: Proof) -> Result<(), Error>; + + /// Verify MMR proof against given root hash. + /// + /// Note this function does not require any on-chain storage - the + /// proof is verified against given MMR root hash. + /// + /// The leaf data is expected to be encoded in it's compact form. + fn verify_proof_stateless(root: Hash, leaf: Vec, proof: Proof) + -> Result<(), Error>; + } +} #[cfg(test)] mod tests { use super::*; use codec::Decode; - use crate::tests::hex; + use sp_core::H256; use sp_runtime::traits::Keccak256; + pub(crate) fn hex(s: &str) -> H256 { + s.parse().unwrap() + } + type Test = DataOrHash; type TestCompact = Compact; type TestProof = Proof<::Output>; @@ -412,4 +523,35 @@ mod tests { assert_eq!(decoded_compact, vec![Ok(d.clone()), Ok(d.clone())]); } + + #[test] + fn opaque_leaves_should_be_scale_compatible_with_concrete_ones() { + // given + let a = Test::Data("Hello World!".into()); + let b = Test::Data("".into()); + + let c: TestCompact = Compact::new((a.clone(), b.clone())); + let d: TestCompact = Compact::new(( + Test::Hash(a.hash()), + Test::Hash(b.hash()), + )); + let cases = vec![c, d.clone()]; + + let encoded_compact = cases + .iter() + .map(|c| c.using_encoded(|x| x.to_vec(), true)) + .map(OpaqueLeaf::from_encoded_leaf) + .collect::>(); + + let opaque = cases + .iter() + .map(OpaqueLeaf::from_leaf) + .collect::>(); + + // then + assert_eq!( + encoded_compact, + opaque, + ); + } } diff --git a/frame/merkle-mountain-range/src/benchmarking.rs b/frame/merkle-mountain-range/src/benchmarking.rs index e6b3cf7f2172c..750a140382b9d 100644 --- a/frame/merkle-mountain-range/src/benchmarking.rs +++ b/frame/merkle-mountain-range/src/benchmarking.rs @@ -21,7 +21,7 @@ use crate::*; use frame_support::traits::OnInitialize; -use frame_benchmarking::benchmarks; +use frame_benchmarking::{benchmarks, impl_benchmark_test_suite}; use sp_std::prelude::*; benchmarks! { @@ -38,17 +38,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::mock::*; - use crate::tests::new_test_ext; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_on_initialize::()); - }) - } -} +impl_benchmark_test_suite!( + Module, + crate::tests::new_test_ext(), + crate::mock::Test, +); diff --git a/frame/merkle-mountain-range/src/lib.rs b/frame/merkle-mountain-range/src/lib.rs index 85e448fd3a17c..b137be7b53c1a 100644 --- a/frame/merkle-mountain-range/src/lib.rs +++ b/frame/merkle-mountain-range/src/lib.rs @@ -73,7 +73,7 @@ mod mock; #[cfg(test)] mod tests; -pub mod primitives; +pub use pallet_mmr_primitives as primitives; pub trait WeightInfo { fn on_initialize(peaks: u64) -> Weight; @@ -118,6 +118,9 @@ pub trait Config: frame_system::Config { /// [LeafDataProvider](primitives::LeafDataProvider)s can be composed into tuples to put /// multiple elements into the tree. In such a case it might be worth using [primitives::Compact] /// to make MMR proof for one element of the tuple leaner. + /// + /// Note that the leaf at each block MUST be unique. You may want to include a block hash or block + /// number as an easiest way to ensure that. type LeafData: primitives::LeafDataProvider; /// A hook to act on the new MMR root. @@ -182,6 +185,28 @@ type LeafOf = <>::LeafData as primitives::LeafDataProvider> /// Hashing used for the pallet. pub(crate) type HashingOf = >::Hashing; +/// Stateless MMR proof verification. +/// +/// This function can be used to verify received MMR proof (`proof`) +/// for given leaf data (`leaf`) against a known MMR root hash (`root`). +/// +/// The verification does not require any storage access. +pub fn verify_leaf_proof( + root: H::Output, + leaf: mmr::Node, + proof: primitives::Proof, +) -> Result<(), primitives::Error> where + H: traits::Hash, + L: primitives::FullLeaf, +{ + let is_valid = mmr::verify_leaf_proof::(root, leaf, proof)?; + if is_valid { + Ok(()) + } else { + Err(primitives::Error::Verify.log_debug(("The proof is incorrect.", root))) + } +} + impl, I: Instance> Module { fn offchain_key(pos: u64) -> sp_std::prelude::Vec { (T::INDEXING_PREFIX, pos).encode() @@ -195,7 +220,7 @@ impl, I: Instance> Module { /// It may return an error or panic if used incorrectly. pub fn generate_proof(leaf_index: u64) -> Result< (LeafOf, primitives::Proof<>::Hash>), - mmr::Error, + primitives::Error, > { let mmr: ModuleMmr = mmr::Mmr::new(Self::mmr_leaves()); mmr.generate_proof(leaf_index) @@ -210,12 +235,12 @@ impl, I: Instance> Module { pub fn verify_leaf( leaf: LeafOf, proof: primitives::Proof<>::Hash>, - ) -> Result<(), mmr::Error> { + ) -> Result<(), primitives::Error> { if proof.leaf_count > Self::mmr_leaves() || proof.leaf_count == 0 || proof.items.len() as u32 > mmr::utils::NodesUtils::new(proof.leaf_count).depth() { - return Err(mmr::Error::Verify.log_debug( + return Err(primitives::Error::Verify.log_debug( "The proof has incorrect number of leaves or proof items." )); } @@ -225,7 +250,7 @@ impl, I: Instance> Module { if is_valid { Ok(()) } else { - Err(mmr::Error::Verify.log_debug("The proof is incorrect.")) + Err(primitives::Error::Verify.log_debug("The proof is incorrect.")) } } } diff --git a/frame/merkle-mountain-range/src/mmr/mmr.rs b/frame/merkle-mountain-range/src/mmr/mmr.rs index 10762d98d7e01..a3d373bfd2e9e 100644 --- a/frame/merkle-mountain-range/src/mmr/mmr.rs +++ b/frame/merkle-mountain-range/src/mmr/mmr.rs @@ -22,12 +22,35 @@ use crate::{ storage::{Storage, OffchainStorage, RuntimeStorage}, utils::NodesUtils, }, - primitives, + primitives::{self, Error}, }; -use frame_support::{debug, RuntimeDebug}; -use sp_std::fmt; #[cfg(not(feature = "std"))] -use sp_std::{vec, prelude::Vec}; +use sp_std::vec; + +/// Stateless verification of the leaf proof. +pub fn verify_leaf_proof( + root: H::Output, + leaf: Node, + proof: primitives::Proof, +) -> Result where + H: sp_runtime::traits::Hash, + L: primitives::FullLeaf, +{ + let size = NodesUtils::new(proof.leaf_count).size(); + let leaf_position = mmr_lib::leaf_index_to_pos(proof.leaf_index); + + let p = mmr_lib::MerkleProof::< + Node, + Hasher, + >::new( + size, + proof.items.into_iter().map(Node::Hash).collect(), + ); + p.verify( + Node::Hash(root), + vec![(leaf_position, leaf)], + ).map_err(|e| Error::Verify.log_debug(e)) +} /// A wrapper around a MMR library to expose limited functionality. /// @@ -123,7 +146,7 @@ impl Mmr where impl Mmr where T: Config, I: Instance, - L: primitives::FullLeaf, + L: primitives::FullLeaf + codec::Decode, { /// Generate a proof for given leaf index. /// @@ -151,36 +174,3 @@ impl Mmr where } } -/// Merkle Mountain Range operation error. -#[derive(RuntimeDebug)] -#[cfg_attr(test, derive(PartialEq, Eq))] -pub enum Error { - /// Error while pushing new node. - Push, - /// Error getting the new root. - GetRoot, - /// Error commiting changes. - Commit, - /// Error during proof generation. - GenerateProof, - /// Proof verification error. - Verify, - /// Leaf not found in the storage. - LeafNotFound, -} - -impl Error { - /// Consume given error `e` with `self` and generate a native log entry with error details. - pub(crate) fn log_error(self, e: impl fmt::Debug) -> Self { - debug::native::error!("[{:?}] MMR error: {:?}", self, e); - self - } - - /// Consume given error `e` with `self` and generate a native log entry with error details. - pub(crate) fn log_debug(self, e: impl fmt::Debug) -> Self { - debug::native::debug!("[{:?}] MMR error: {:?}", self, e); - self - } - -} - diff --git a/frame/merkle-mountain-range/src/mmr/mod.rs b/frame/merkle-mountain-range/src/mmr/mod.rs index 38833af6f2f85..e705b247067e5 100644 --- a/frame/merkle-mountain-range/src/mmr/mod.rs +++ b/frame/merkle-mountain-range/src/mmr/mod.rs @@ -22,7 +22,7 @@ mod mmr; use crate::primitives::FullLeaf; use sp_runtime::traits; -pub use self::mmr::{Mmr, Error}; +pub use self::mmr::{Mmr, verify_leaf_proof}; /// Node type for runtime `T`. pub type NodeOf = Node<>::Hashing, L>; diff --git a/frame/merkle-mountain-range/src/mmr/storage.rs b/frame/merkle-mountain-range/src/mmr/storage.rs index c8390e27047c5..0bff53f2fb057 100644 --- a/frame/merkle-mountain-range/src/mmr/storage.rs +++ b/frame/merkle-mountain-range/src/mmr/storage.rs @@ -57,7 +57,7 @@ impl Default for Storage { impl mmr_lib::MMRStore> for Storage where T: Config, I: Instance, - L: primitives::FullLeaf, + L: primitives::FullLeaf + codec::Decode, { fn get_elem(&self, pos: u64) -> mmr_lib::Result>> { let key = Module::::offchain_key(pos); diff --git a/frame/merkle-mountain-range/src/mock.rs b/frame/merkle-mountain-range/src/mock.rs index 153aecdbd3136..0adb0294d5080 100644 --- a/frame/merkle-mountain-range/src/mock.rs +++ b/frame/merkle-mountain-range/src/mock.rs @@ -16,12 +16,11 @@ // limitations under the License. use crate::*; -use crate::primitives::{LeafDataProvider, Compact}; +use crate as pallet_mmr; use codec::{Encode, Decode}; -use frame_support::{ - impl_outer_origin, parameter_types, -}; +use frame_support::parameter_types; +use pallet_mmr_primitives::{LeafDataProvider, Compact}; use sp_core::H256; use sp_runtime::{ testing::Header, @@ -32,19 +31,27 @@ use sp_runtime::{ use sp_std::cell::RefCell; use sp_std::prelude::*; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + MMR: pallet_mmr::{Module, Call, Storage}, + } +); -#[derive(Clone, Eq, PartialEq, Encode, Decode)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; } impl frame_system::Config for Test { type BaseCallFilter = (); type Origin = Origin; - type Call = (); + type Call = Call; type Index = u64; type BlockNumber = u64; type Hash = H256; @@ -52,13 +59,13 @@ impl frame_system::Config for Test { type AccountId = sp_core::sr25519::Public; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type DbWeight = (); type BlockWeights = (); type BlockLength = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -102,5 +109,3 @@ impl LeafDataProvider for LeafData { LEAF_DATA.with(|r| r.borrow().clone()) } } - -pub(crate) type MMR = Module; diff --git a/frame/merkle-mountain-range/src/tests.rs b/frame/merkle-mountain-range/src/tests.rs index c279e42a8c239..63e4ec2257066 100644 --- a/frame/merkle-mountain-range/src/tests.rs +++ b/frame/merkle-mountain-range/src/tests.rs @@ -17,7 +17,6 @@ use crate::*; use crate::mock::*; -use crate::primitives::{Proof, Compact}; use frame_support::traits::OnInitialize; use sp_core::{ @@ -27,6 +26,7 @@ use sp_core::{ OffchainExt, }, }; +use pallet_mmr_primitives::{Proof, Compact}; pub(crate) fn new_test_ext() -> sp_io::TestExternalities { frame_system::GenesisConfig::default().build_storage::().unwrap().into() @@ -55,12 +55,14 @@ pub(crate) fn hex(s: &str) -> H256 { s.parse().unwrap() } +type BlockNumber = ::BlockNumber; + fn decode_node(v: Vec) -> mmr::Node< ::Hashing, - (H256, LeafData), + ((BlockNumber, H256), LeafData), > { use crate::primitives::DataOrHash; - type A = DataOrHash::<::Hashing, H256>; + type A = DataOrHash::<::Hashing, (BlockNumber, H256)>; type B = DataOrHash::<::Hashing, LeafData>; type Node = mmr::Node<::Hashing, (A, B)>; let tuple: Node = codec::Decode::decode(&mut &v[..]).unwrap(); @@ -97,10 +99,10 @@ fn should_start_empty() { // then assert_eq!(crate::NumberOfLeaves::::get(), 1); assert_eq!(crate::Nodes::::get(0), - Some(hex("da5e6d0616e05c6a6348605a37ca33493fc1a15ad1e6a405ee05c17843fdafed"))); + Some(hex("4320435e8c3318562dba60116bdbcc0b82ffcecb9bb39aae3300cfda3ad0b8b0"))); assert_eq!( crate::RootHash::::get(), - hex("da5e6d0616e05c6a6348605a37ca33493fc1a15ad1e6a405ee05c17843fdafed") + hex("4320435e8c3318562dba60116bdbcc0b82ffcecb9bb39aae3300cfda3ad0b8b0") ); assert!(weight != 0); }); @@ -117,32 +119,34 @@ fn should_append_to_mmr_when_on_initialize_is_called() { // then assert_eq!(crate::NumberOfLeaves::::get(), 2); - assert_eq!(crate::Nodes::::get(0), - Some(hex("da5e6d0616e05c6a6348605a37ca33493fc1a15ad1e6a405ee05c17843fdafed"))); - assert_eq!(crate::Nodes::::get(1), - Some(hex("ff5d891b28463a3440e1b650984685efdf260e482cb3807d53c49090841e755f"))); - assert_eq!(crate::Nodes::::get(2), - Some(hex("bc54778fab79f586f007bd408dca2c4aa07959b27d1f2c8f4f2549d1fcfac8f8"))); - assert_eq!(crate::Nodes::::get(3), None); - assert_eq!( + assert_eq!(( + crate::Nodes::::get(0), + crate::Nodes::::get(1), + crate::Nodes::::get(2), + crate::Nodes::::get(3), crate::RootHash::::get(), - hex("bc54778fab79f586f007bd408dca2c4aa07959b27d1f2c8f4f2549d1fcfac8f8") - ); + ), ( + Some(hex("4320435e8c3318562dba60116bdbcc0b82ffcecb9bb39aae3300cfda3ad0b8b0")), + Some(hex("ad4cbc033833612ccd4626d5f023b9dfc50a35e838514dd1f3c86f8506728705")), + Some(hex("672c04a9cd05a644789d769daa552d35d8de7c33129f8a7cbf49e595234c4854")), + None, + hex("672c04a9cd05a644789d769daa552d35d8de7c33129f8a7cbf49e595234c4854"), + )); }); // make sure the leaves end up in the offchain DB ext.persist_offchain_overlay(); let offchain_db = ext.offchain_db(); assert_eq!(offchain_db.get(&MMR::offchain_key(0)).map(decode_node), Some(mmr::Node::Data(( - H256::repeat_byte(1), + (0, H256::repeat_byte(1)), LeafData::new(1), )))); assert_eq!(offchain_db.get(&MMR::offchain_key(1)).map(decode_node), Some(mmr::Node::Data(( - H256::repeat_byte(2), + (1, H256::repeat_byte(2)), LeafData::new(2), )))); assert_eq!(offchain_db.get(&MMR::offchain_key(2)).map(decode_node), Some(mmr::Node::Hash( - hex("bc54778fab79f586f007bd408dca2c4aa07959b27d1f2c8f4f2549d1fcfac8f8") + hex("672c04a9cd05a644789d769daa552d35d8de7c33129f8a7cbf49e595234c4854") ))); assert_eq!(offchain_db.get(&MMR::offchain_key(3)), None); } @@ -156,14 +160,15 @@ fn should_construct_larger_mmr_correctly() { // then assert_eq!(crate::NumberOfLeaves::::get(), 7); - assert_eq!(crate::Nodes::::get(0), - Some(hex("da5e6d0616e05c6a6348605a37ca33493fc1a15ad1e6a405ee05c17843fdafed"))); - assert_eq!(crate::Nodes::::get(10), - Some(hex("af3327deed0515c8d1902c9b5cd375942d42f388f3bfe3d1cd6e1b86f9cc456c"))); - assert_eq!( + assert_eq!(( + crate::Nodes::::get(0), + crate::Nodes::::get(10), crate::RootHash::::get(), - hex("fc4f9042bd2f73feb26f3fc42db834c5f1943fa20070ddf106c486a478a0d561") - ); + ), ( + Some(hex("4320435e8c3318562dba60116bdbcc0b82ffcecb9bb39aae3300cfda3ad0b8b0")), + Some(hex("611c2174c6164952a66d985cfe1ec1a623794393e3acff96b136d198f37a648c")), + hex("e45e25259f7930626431347fa4dd9aae7ac83b4966126d425ca70ab343709d2c"), + )); }); } @@ -187,38 +192,38 @@ fn should_generate_proofs_correctly() { // then assert_eq!(proofs[0], (Compact::new(( - H256::repeat_byte(1).into(), + (0, H256::repeat_byte(1)).into(), LeafData::new(1).into(), )), Proof { leaf_index: 0, leaf_count: 7, items: vec![ - hex("ff5d891b28463a3440e1b650984685efdf260e482cb3807d53c49090841e755f"), - hex("00b0046bd2d63fcb760cf50a262448bb2bbf9a264b0b0950d8744044edf00dc3"), - hex("16de0900b57bf359a0733674ebfbba0f494e95a8391b4bfeae850019399f3ec0"), + hex("ad4cbc033833612ccd4626d5f023b9dfc50a35e838514dd1f3c86f8506728705"), + hex("cb24f4614ad5b2a5430344c99545b421d9af83c46fd632d70a332200884b4d46"), + hex("dca421199bdcc55bb773c6b6967e8d16675de69062b52285ca63685241fdf626"), ], })); assert_eq!(proofs[4], (Compact::new(( - H256::repeat_byte(5).into(), + (4, H256::repeat_byte(5)).into(), LeafData::new(5).into(), )), Proof { leaf_index: 4, leaf_count: 7, items: vec![ - hex("e53ee36ba6c068b1a6cfef7862fed5005df55615e1c9fa6eeefe08329ac4b94b"), - hex("c09d4a008a0f1ef37860bef33ec3088ccd94268c0bfba7ff1b3c2a1075b0eb92"), - hex("af3327deed0515c8d1902c9b5cd375942d42f388f3bfe3d1cd6e1b86f9cc456c"), + hex("ae88a0825da50e953e7a359c55fe13c8015e48d03d301b8bdfc9193874da9252"), + hex("8ed25570209d8f753d02df07c1884ddb36a3d9d4770e4608b188322151c657fe"), + hex("611c2174c6164952a66d985cfe1ec1a623794393e3acff96b136d198f37a648c"), ], })); assert_eq!(proofs[6], (Compact::new(( - H256::repeat_byte(7).into(), + (6, H256::repeat_byte(7)).into(), LeafData::new(7).into(), )), Proof { leaf_index: 6, leaf_count: 7, items: vec![ - hex("e53ee36ba6c068b1a6cfef7862fed5005df55615e1c9fa6eeefe08329ac4b94b"), - hex("dad09f50b41822fc5ecadc25b08c3a61531d4d60e962a5aa0b6998fad5c37c5e"), + hex("ae88a0825da50e953e7a359c55fe13c8015e48d03d301b8bdfc9193874da9252"), + hex("7e4316ae2ebf7c3b6821cb3a46ca8b7a4f9351a9b40fcf014bb0a4fd8e8f29da"), ], })); }); @@ -253,6 +258,30 @@ fn should_verify() { }); } +#[test] +fn verification_should_be_stateless() { + let _ = env_logger::try_init(); + + // Start off with chain initialisation and storing indexing data off-chain + // (MMR Leafs) + let mut ext = new_test_ext(); + ext.execute_with(|| init_chain(7)); + ext.persist_offchain_overlay(); + + // Try to generate proof now. This requires the offchain extensions to be present + // to retrieve full leaf data. + register_offchain_ext(&mut ext); + let (leaf, proof5) = ext.execute_with(|| { + // when + crate::Module::::generate_proof(5).unwrap() + }); + let root = ext.execute_with(|| crate::Module::::mmr_root_hash()); + + // Verify proof without relying on any on-chain data. + let leaf = crate::primitives::DataOrHash::Data(leaf); + assert_eq!(crate::verify_leaf_proof::<::Hashing, _>(root, leaf, proof5), Ok(())); +} + #[test] fn should_verify_on_the_next_block_since_there_is_no_pruning_yet() { let _ = env_logger::try_init(); diff --git a/frame/metadata/Cargo.toml b/frame/metadata/Cargo.toml index c809b3d1fcbd4..cede8a836123d 100644 --- a/frame/metadata/Cargo.toml +++ b/frame/metadata/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-metadata" -version = "12.0.1" +version = "13.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,10 +13,10 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } serde = { version = "1.0.101", optional = true, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } [features] default = ["std"] diff --git a/frame/metadata/src/lib.rs b/frame/metadata/src/lib.rs index 8e6b8b6bd796d..a63da82ca00db 100644 --- a/frame/metadata/src/lib.rs +++ b/frame/metadata/src/lib.rs @@ -53,7 +53,7 @@ pub enum DecodeDifferent where B: 'static, O: 'static { } impl Encode for DecodeDifferent where B: Encode + 'static, O: Encode + 'static { - fn encode_to(&self, dest: &mut W) { + fn encode_to(&self, dest: &mut W) { match self { DecodeDifferent::Encode(b) => b.encode_to(dest), DecodeDifferent::Decoded(o) => o.encode_to(dest), @@ -139,7 +139,7 @@ pub struct FunctionArgumentMetadata { pub struct FnEncode(pub fn() -> E) where E: Encode + 'static; impl Encode for FnEncode { - fn encode_to(&self, dest: &mut W) { + fn encode_to(&self, dest: &mut W) { self.0().encode_to(dest); } } @@ -238,7 +238,7 @@ pub struct DefaultByteGetter(pub &'static dyn DefaultByte); pub type ByteGetter = DecodeDifferent>; impl Encode for DefaultByteGetter { - fn encode_to(&self, dest: &mut W) { + fn encode_to(&self, dest: &mut W) { self.0.default_byte().encode_to(dest) } } @@ -374,7 +374,7 @@ pub enum RuntimeMetadata { pub enum RuntimeMetadataDeprecated { } impl Encode for RuntimeMetadataDeprecated { - fn encode_to(&self, _dest: &mut W) {} + fn encode_to(&self, _dest: &mut W) {} } impl codec::EncodeLike for RuntimeMetadataDeprecated {} diff --git a/frame/multisig/Cargo.toml b/frame/multisig/Cargo.toml index 70412fa6de0ad..e8d625138371e 100644 --- a/frame/multisig/Cargo.toml +++ b/frame/multisig/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-multisig" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,19 +14,19 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -pallet-balances = { version = "2.0.0", path = "../balances" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/multisig/src/benchmarking.rs b/frame/multisig/src/benchmarking.rs index 748223072b99b..b530a96396024 100644 --- a/frame/multisig/src/benchmarking.rs +++ b/frame/multisig/src/benchmarking.rs @@ -21,7 +21,7 @@ use super::*; use frame_system::RawOrigin; -use frame_benchmarking::{benchmarks, account}; +use frame_benchmarking::{benchmarks, account, impl_benchmark_test_suite}; use sp_runtime::traits::Bounded; use core::convert::TryInto; @@ -298,25 +298,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_as_multi_threshold_1::()); - assert_ok!(test_benchmark_as_multi_create::()); - assert_ok!(test_benchmark_as_multi_create_store::()); - assert_ok!(test_benchmark_as_multi_approve::()); - assert_ok!(test_benchmark_as_multi_approve_store::()); - assert_ok!(test_benchmark_as_multi_complete::()); - assert_ok!(test_benchmark_approve_as_multi_create::()); - assert_ok!(test_benchmark_approve_as_multi_approve::()); - assert_ok!(test_benchmark_approve_as_multi_complete::()); - assert_ok!(test_benchmark_cancel_as_multi::()); - }); - } -} +impl_benchmark_test_suite!( + Multisig, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/multisig/src/lib.rs b/frame/multisig/src/lib.rs index a015f291bc716..aa72d2d1ad3ca 100644 --- a/frame/multisig/src/lib.rs +++ b/frame/multisig/src/lib.rs @@ -160,7 +160,7 @@ decl_error! { /// A timepoint was given, yet no multisig operation is underway. UnexpectedTimepoint, /// The maximum weight information provided was too low. - WeightTooLow, + MaxWeightTooLow, /// The data to be stored is already stored. AlreadyStored, } @@ -503,7 +503,7 @@ impl Module { if let Some((call, call_len)) = maybe_approved_call { // verify weight - ensure!(call.get_dispatch_info().weight <= max_weight, Error::::WeightTooLow); + ensure!(call.get_dispatch_info().weight <= max_weight, Error::::MaxWeightTooLow); // Clean up storage before executing call to avoid an possibility of reentrancy // attack. diff --git a/frame/multisig/src/tests.rs b/frame/multisig/src/tests.rs index d16b0ad495568..a3f47a26e6422 100644 --- a/frame/multisig/src/tests.rs +++ b/frame/multisig/src/tests.rs @@ -22,37 +22,27 @@ use super::*; use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch, - impl_outer_event, traits::Filter, + assert_ok, assert_noop, parameter_types, traits::Filter, }; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header}; -use crate as multisig; - -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} - -impl_outer_event! { - pub enum TestEvent for Test { - system, - pallet_balances, - multisig, +use crate as pallet_multisig; + +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Multisig: pallet_multisig::{Module, Call, Storage, Event}, } -} -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - frame_system::System, - pallet_balances::Balances, - multisig::Multisig, - } -} +); -// For testing the pallet, we construct most of a mock runtime. This means -// first constructing a configuration type (`Test`) which `impl`s each of the -// configuration traits of pallets we want to use. -#[derive(Clone, Eq, PartialEq)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -72,10 +62,10 @@ impl frame_system::Config for Test { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = TestEvent; + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -88,7 +78,7 @@ parameter_types! { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; - type Event = TestEvent; + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -111,7 +101,7 @@ impl Filter for TestBaseCallFilter { } } impl Config for Test { - type Event = TestEvent; + type Event = Event; type Call = Call; type Currency = Balances; type DepositBase = DepositBase; @@ -119,9 +109,6 @@ impl Config for Test { type MaxSignatories = MaxSignatories; type WeightInfo = (); } -type System = frame_system::Module; -type Balances = pallet_balances::Module; -type Multisig = Module; use pallet_balances::Call as BalancesCall; use pallet_balances::Error as BalancesError; @@ -136,11 +123,11 @@ pub fn new_test_ext() -> sp_io::TestExternalities { ext } -fn last_event() -> TestEvent { +fn last_event() -> Event { system::Module::::events().pop().map(|e| e.event).expect("Event expected") } -fn expect_event>(e: E) { +fn expect_event>(e: E) { assert_eq!(last_event(), e.into()); } @@ -544,7 +531,7 @@ fn weight_check_works() { assert_noop!( Multisig::as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), data, false, 0), - Error::::WeightTooLow, + Error::::MaxWeightTooLow, ); }); } diff --git a/frame/nicks/Cargo.toml b/frame/nicks/Cargo.toml index 3e1ddf897d34c..611f492b81f24 100644 --- a/frame/nicks/Cargo.toml +++ b/frame/nicks/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-nicks" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,16 +14,16 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -pallet-balances = { version = "2.0.0", path = "../balances" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/nicks/src/lib.rs b/frame/nicks/src/lib.rs index 983be4056d0c3..681a45626fbca 100644 --- a/frame/nicks/src/lib.rs +++ b/frame/nicks/src/lib.rs @@ -239,23 +239,30 @@ decl_module! { #[cfg(test)] mod tests { use super::*; + use crate as pallet_nicks; - use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, parameter_types, - ord_parameter_types - }; + use frame_support::{assert_ok, assert_noop, parameter_types, ord_parameter_types}; use sp_core::H256; use frame_system::EnsureSignedBy; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup, BadOrigin}, }; - impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} - } + type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; + type Block = frame_system::mocking::MockBlock; + + frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Nicks: pallet_nicks::{Module, Call, Storage, Event}, + } + ); - #[derive(Clone, Eq, PartialEq)] - pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -270,15 +277,15 @@ mod tests { type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = (); + type Call = Call; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -291,7 +298,7 @@ mod tests { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; - type Event = (); + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -306,7 +313,7 @@ mod tests { pub const One: u64 = 1; } impl Config for Test { - type Event = (); + type Event = Event; type Currency = Balances; type ReservationFee = ReservationFee; type Slashed = (); @@ -314,9 +321,6 @@ mod tests { type MinLength = MinLength; type MaxLength = MaxLength; } - type System = frame_system::Module; - type Balances = pallet_balances::Module; - type Nicks = Module; fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); diff --git a/frame/node-authorization/Cargo.toml b/frame/node-authorization/Cargo.toml index d78ffd13fd579..db77f25c18871 100644 --- a/frame/node-authorization/Cargo.toml +++ b/frame/node-authorization/Cargo.toml @@ -13,13 +13,13 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } [features] default = ["std"] diff --git a/frame/node-authorization/src/lib.rs b/frame/node-authorization/src/lib.rs index 79b1d6e74c303..f1f70e9eacd4b 100644 --- a/frame/node-authorization/src/lib.rs +++ b/frame/node-authorization/src/lib.rs @@ -431,21 +431,28 @@ impl Module { #[cfg(test)] mod tests { use super::*; + use crate as pallet_node_authorization; - use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, - parameter_types, ord_parameter_types, - }; + use frame_support::{assert_ok, assert_noop, parameter_types, ord_parameter_types}; use frame_system::EnsureSignedBy; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup, BadOrigin}, testing::Header}; - impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} - } - - #[derive(Clone, Eq, PartialEq)] - pub struct Test; + type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; + type Block = frame_system::mocking::MockBlock; + + frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + NodeAuthorization: pallet_node_authorization::{ + Module, Call, Storage, Config, Event, + }, + } + ); parameter_types! { pub const BlockHashCount: u64 = 250; @@ -459,15 +466,15 @@ mod tests { type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = (); + type Call = Call; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -486,7 +493,7 @@ mod tests { pub const MaxPeerIdLength: u32 = 2; } impl Config for Test { - type Event = (); + type Event = Event; type MaxWellKnownNodes = MaxWellKnownNodes; type MaxPeerIdLength = MaxPeerIdLength; type AddOrigin = EnsureSignedBy; @@ -496,15 +503,13 @@ mod tests { type WeightInfo = (); } - type NodeAuthorization = Module; - fn test_node(id: u8) -> PeerId { PeerId(vec![id]) } fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); - GenesisConfig:: { + pallet_node_authorization::GenesisConfig:: { nodes: vec![(test_node(10), 10), (test_node(20), 20), (test_node(30), 30)], }.assimilate_storage(&mut t).unwrap(); t.into() diff --git a/frame/offences/Cargo.toml b/frame/offences/Cargo.toml index 2860e3ef8ee2f..3232d5f3ae5dd 100644 --- a/frame/offences/Cargo.toml +++ b/frame/offences/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-offences" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,18 +13,18 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -pallet-balances = { version = "2.0.0", default-features = false, path = "../balances" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +pallet-balances = { version = "3.0.0", default-features = false, path = "../balances" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } serde = { version = "1.0.101", optional = true } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] -sp-io = { version = "2.0.0", path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } [features] default = ["std"] diff --git a/frame/offences/benchmarking/Cargo.toml b/frame/offences/benchmarking/Cargo.toml index 80492288d74bf..2378be45d6814 100644 --- a/frame/offences/benchmarking/Cargo.toml +++ b/frame/offences/benchmarking/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-offences-benchmarking" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,27 +13,28 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../../benchmarking" } -frame-support = { version = "2.0.0", default-features = false, path = "../../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../../system" } -pallet-babe = { version = "2.0.0", default-features = false, path = "../../babe" } -pallet-balances = { version = "2.0.0", default-features = false, path = "../../balances" } -pallet-grandpa = { version = "2.0.0", default-features = false, path = "../../grandpa" } -pallet-im-online = { version = "2.0.0", default-features = false, path = "../../im-online" } -pallet-offences = { version = "2.0.0", default-features = false, features = ["runtime-benchmarks"], path = "../../offences" } -pallet-session = { version = "2.0.0", default-features = false, path = "../../session" } -pallet-staking = { version = "2.0.0", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } -sp-staking = { version = "2.0.0", default-features = false, path = "../../../primitives/staking" } -sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../../benchmarking" } +frame-support = { version = "3.0.0", default-features = false, path = "../../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../../system" } +pallet-babe = { version = "3.0.0", default-features = false, path = "../../babe" } +pallet-balances = { version = "3.0.0", default-features = false, path = "../../balances" } +pallet-grandpa = { version = "3.0.0", default-features = false, path = "../../grandpa" } +pallet-im-online = { version = "3.0.0", default-features = false, path = "../../im-online" } +pallet-offences = { version = "3.0.0", default-features = false, features = ["runtime-benchmarks"], path = "../../offences" } +pallet-session = { version = "3.0.0", default-features = false, path = "../../session" } +pallet-staking = { version = "3.0.0", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../primitives/runtime" } +sp-staking = { version = "3.0.0", default-features = false, path = "../../../primitives/staking" } +sp-std = { version = "3.0.0", default-features = false, path = "../../../primitives/std" } +sp-election-providers = { version = "3.0.0", default-features = false, path = "../../../primitives/election-providers" } [dev-dependencies] -pallet-staking-reward-curve = { version = "2.0.0", path = "../../staking/reward-curve" } -pallet-timestamp = { version = "2.0.0", path = "../../timestamp" } +pallet-staking-reward-curve = { version = "3.0.0", path = "../../staking/reward-curve" } +pallet-timestamp = { version = "3.0.0", path = "../../timestamp" } serde = { version = "1.0.101" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-io = { version = "2.0.0", path = "../../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-io = { version = "3.0.0", path = "../../../primitives/io" } [features] default = ["std"] @@ -50,6 +51,7 @@ std = [ "pallet-staking/std", "sp-runtime/std", "sp-staking/std", + "sp-election-providers/std", "sp-std/std", "codec/std", ] diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 1151bfea4807b..a14e4cf5d29ef 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -25,13 +25,13 @@ use sp_std::prelude::*; use sp_std::vec; use frame_system::{RawOrigin, Module as System, Config as SystemConfig}; -use frame_benchmarking::{benchmarks, account}; -use frame_support::traits::{Currency, OnInitialize}; +use frame_benchmarking::{benchmarks, account, impl_benchmark_test_suite}; +use frame_support::traits::{Currency, OnInitialize, ValidatorSet, ValidatorSetWithIdentification}; use sp_runtime::{Perbill, traits::{Convert, StaticLookup, Saturating, UniqueSaturatedInto}}; use sp_staking::offence::{ReportOffence, Offence, OffenceDetails}; -use pallet_balances::{Config as BalancesConfig}; +use pallet_balances::Config as BalancesConfig; use pallet_babe::BabeEquivocationOffence; use pallet_grandpa::{GrandpaEquivocationOffence, GrandpaTimeSlot}; use pallet_im_online::{Config as ImOnlineConfig, Module as ImOnline, UnresponsivenessOffence}; @@ -176,6 +176,34 @@ fn make_offenders(num_offenders: u32, num_nominators: u32) -> Result< Ok((id_tuples, offenders)) } +fn make_offenders_im_online(num_offenders: u32, num_nominators: u32) -> Result< + (Vec>, Vec>), + &'static str +> { + Staking::::new_session(0); + + let mut offenders = vec![]; + for i in 0 .. num_offenders { + let offender = create_offender::(i + 1, num_nominators)?; + offenders.push(offender); + } + + Staking::::start_session(0); + + let id_tuples = offenders.iter() + .map(|offender| < + ::ValidatorSet as ValidatorSet + >::ValidatorIdOf::convert(offender.controller.clone()) + .expect("failed to get validator id from account id")) + .map(|validator_id| < + ::ValidatorSet as ValidatorSetWithIdentification + >::IdentificationOf::convert(validator_id.clone()) + .map(|full_id| (validator_id, full_id)) + .expect("failed to convert validator id to full identification")) + .collect::>>(); + Ok((id_tuples, offenders)) +} + #[cfg(test)] fn check_events::Event>>(expected: I) { let events = System::::events() .into_iter() @@ -220,7 +248,7 @@ benchmarks! { // make sure reporters actually get rewarded Staking::::set_slash_reward_fraction(Perbill::one()); - let (offenders, raw_offenders) = make_offenders::(o, n)?; + let (offenders, raw_offenders) = make_offenders_im_online::(o, n)?; let keys = ImOnline::::keys(); let validator_set_count = keys.len() as u32; @@ -331,7 +359,7 @@ benchmarks! { let keys = ImOnline::::keys(); let offence = BabeEquivocationOffence { - slot: 0, + slot: 0u64.into(), session_index: 0, validator_set_count: keys.len() as u32, offender: T::convert(offenders.pop().unwrap()), @@ -392,19 +420,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::mock::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_report_offence_im_online::()); - assert_ok!(test_benchmark_report_offence_grandpa::()); - assert_ok!(test_benchmark_report_offence_babe::()); - assert_ok!(test_benchmark_on_initialize::()); - }); - } -} +impl_benchmark_test_suite!( + Module, + crate::mock::new_test_ext(), + crate::mock::Test, +); diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index 6ebb9f19e6ae9..124e6b13b77ac 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -29,7 +29,8 @@ use sp_runtime::{ traits::IdentityLookup, testing::{Header, UintAuthorityId}, }; - +use sp_election_providers::onchain; +use pallet_session::historical as pallet_session_historical; type AccountId = u64; type AccountIndex = u32; @@ -58,7 +59,7 @@ impl frame_system::Config for Test { type Event = Event; type BlockHashCount = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -130,6 +131,7 @@ impl pallet_session::Config for Test { type DisabledValidatorsThreshold = (); type WeightInfo = (); } + pallet_staking_reward_curve::build! { const I_NPOS: sp_runtime::curve::PiecewiseLinear<'static> = curve!( min_inflation: 0_025_000, @@ -147,6 +149,13 @@ parameter_types! { pub type Extrinsic = sp_runtime::testing::TestXt; +impl onchain::Config for Test { + type AccountId = AccountId; + type BlockNumber = BlockNumber; + type Accuracy = Perbill; + type DataProvider = Staking; +} + impl pallet_staking::Config for Test { type Currency = Balances; type UnixTime = pallet_timestamp::Module; @@ -169,12 +178,14 @@ impl pallet_staking::Config for Test { type MaxIterations = (); type MinSolutionScoreBump = (); type OffchainSolutionWeightLimit = (); + type ElectionProvider = onchain::OnChainSequentialPhragmen; type WeightInfo = (); } impl pallet_im_online::Config for Test { type AuthorityId = UintAuthorityId; type Event = Event; + type ValidatorSet = Historical; type SessionDuration = Period; type ReportUnresponsiveness = Offences; type UnsignedPriority = (); @@ -214,6 +225,7 @@ frame_support::construct_runtime!( Session: pallet_session::{Module, Call, Storage, Event, Config}, ImOnline: pallet_im_online::{Module, Call, Storage, Event, ValidateUnsigned, Config}, Offences: pallet_offences::{Module, Call, Storage, Event}, + Historical: pallet_session_historical::{Module}, } ); diff --git a/frame/offences/src/mock.rs b/frame/offences/src/mock.rs index 042c0501094ca..c47a9cf943c18 100644 --- a/frame/offences/src/mock.rs +++ b/frame/offences/src/mock.rs @@ -20,7 +20,7 @@ #![cfg(test)] use std::cell::RefCell; -use crate::{Module, Config}; +use crate::Config; use codec::Encode; use sp_runtime::Perbill; use sp_staking::{ @@ -31,14 +31,10 @@ use sp_runtime::testing::Header; use sp_runtime::traits::{IdentityLookup, BlakeTwo256}; use sp_core::H256; use frame_support::{ - impl_outer_origin, impl_outer_event, parameter_types, StorageMap, StorageDoubleMap, + parameter_types, StorageMap, StorageDoubleMap, weights::{Weight, constants::{WEIGHT_PER_SECOND, RocksDbWeight}}, }; -use frame_system as system; - -impl_outer_origin!{ - pub enum Origin for Runtime {} -} +use crate as offences; pub struct OnOffenceHandler; @@ -86,9 +82,20 @@ pub fn set_offence_weight(new: Weight) { OFFENCE_WEIGHT.with(|w| *w.borrow_mut() = new); } -// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. -#[derive(Clone, PartialEq, Eq, Debug)] -pub struct Runtime; +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Runtime where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Offences: offences::{Module, Call, Storage, Event}, + } +); + parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -102,16 +109,16 @@ impl frame_system::Config for Runtime { type Origin = Origin; type Index = u64; type BlockNumber = u64; - type Call = (); + type Call = Call; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = TestEvent; + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -125,23 +132,12 @@ parameter_types! { } impl Config for Runtime { - type Event = TestEvent; + type Event = Event; type IdentificationTuple = u64; type OnOffenceHandler = OnOffenceHandler; type WeightSoftLimit = OffencesWeightSoftLimit; } -mod offences { - pub use crate::Event; -} - -impl_outer_event! { - pub enum TestEvent for Runtime { - system, - offences, - } -} - pub fn new_test_ext() -> sp_io::TestExternalities { let t = frame_system::GenesisConfig::default().build_storage::().unwrap(); let mut ext = sp_io::TestExternalities::new(t); @@ -149,10 +145,6 @@ pub fn new_test_ext() -> sp_io::TestExternalities { ext } -/// Offences module. -pub type Offences = Module; -pub type System = frame_system::Module; - pub const KIND: [u8; 16] = *b"test_report_1234"; /// Returns all offence details for the specific `kind` happened at the specific time slot. diff --git a/frame/offences/src/tests.rs b/frame/offences/src/tests.rs index a33ba96447a48..2b7c500dfa2d9 100644 --- a/frame/offences/src/tests.rs +++ b/frame/offences/src/tests.rs @@ -21,7 +21,7 @@ use super::*; use crate::mock::{ - Offences, System, Offence, TestEvent, KIND, new_test_ext, with_on_offence_fractions, + Offences, System, Offence, Event, KIND, new_test_ext, with_on_offence_fractions, offence_reports, set_can_report, set_offence_weight, }; use sp_runtime::Perbill; @@ -132,7 +132,7 @@ fn should_deposit_event() { System::events(), vec![EventRecord { phase: Phase::Initialization, - event: TestEvent::offences(crate::Event::Offence(KIND, time_slot.encode(), true)), + event: Event::offences(crate::Event::Offence(KIND, time_slot.encode(), true)), topics: vec![], }] ); @@ -167,7 +167,7 @@ fn doesnt_deposit_event_for_dups() { System::events(), vec![EventRecord { phase: Phase::Initialization, - event: TestEvent::offences(crate::Event::Offence(KIND, time_slot.encode(), true)), + event: Event::offences(crate::Event::Offence(KIND, time_slot.encode(), true)), topics: vec![], }] ); @@ -304,7 +304,7 @@ fn should_queue_and_resubmit_rejected_offence() { System::events(), vec![EventRecord { phase: Phase::Initialization, - event: TestEvent::offences(crate::Event::Offence(KIND, 42u128.encode(), false)), + event: Event::offences(crate::Event::Offence(KIND, 42u128.encode(), false)), topics: vec![], }] ); diff --git a/frame/proxy/Cargo.toml b/frame/proxy/Cargo.toml index da3d50ab22345..9490364abd879 100644 --- a/frame/proxy/Cargo.toml +++ b/frame/proxy/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-proxy" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,20 +14,20 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -pallet-balances = { version = "2.0.0", path = "../balances" } -pallet-utility = { version = "2.0.0", path = "../utility" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-utility = { version = "3.0.0", path = "../utility" } [features] default = ["std"] diff --git a/frame/proxy/src/benchmarking.rs b/frame/proxy/src/benchmarking.rs index 29c2e475c64ff..130c980011871 100644 --- a/frame/proxy/src/benchmarking.rs +++ b/frame/proxy/src/benchmarking.rs @@ -21,7 +21,7 @@ use super::*; use frame_system::{RawOrigin, EventRecord}; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use sp_runtime::traits::Bounded; use crate::Module as Proxy; @@ -251,25 +251,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_proxy::()); - assert_ok!(test_benchmark_proxy_announced::()); - assert_ok!(test_benchmark_remove_announcement::()); - assert_ok!(test_benchmark_reject_announcement::()); - assert_ok!(test_benchmark_announce::()); - assert_ok!(test_benchmark_add_proxy::()); - assert_ok!(test_benchmark_remove_proxy::()); - assert_ok!(test_benchmark_remove_proxies::()); - assert_ok!(test_benchmark_anonymous::()); - assert_ok!(test_benchmark_kill_anonymous::()); - }); - } -} +impl_benchmark_test_suite!( + Proxy, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/proxy/src/tests.rs b/frame/proxy/src/tests.rs index 3c417647c2cd1..b31ef1dfdb2fe 100644 --- a/frame/proxy/src/tests.rs +++ b/frame/proxy/src/tests.rs @@ -22,39 +22,29 @@ use super::*; use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch, - impl_outer_event, RuntimeDebug, dispatch::DispatchError, traits::Filter, + assert_ok, assert_noop, parameter_types, RuntimeDebug, dispatch::DispatchError, traits::Filter, }; use codec::{Encode, Decode}; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header}; use crate as proxy; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} -impl_outer_event! { - pub enum TestEvent for Test { - system, - pallet_balances, - proxy, - pallet_utility, - } -} -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - frame_system::System, - pallet_balances::Balances, - proxy::Proxy, - pallet_utility::Utility, +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Proxy: proxy::{Module, Call, Storage, Event}, + Utility: pallet_utility::{Module, Call, Event}, } -} +); -// For testing the pallet, we construct most of a mock runtime. This means -// first constructing a configuration type (`Test`) which `impl`s each of the -// configuration traits of pallets we want to use. -#[derive(Clone, Eq, PartialEq)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -74,10 +64,10 @@ impl frame_system::Config for Test { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = TestEvent; + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -90,14 +80,14 @@ parameter_types! { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; - type Event = TestEvent; + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; type WeightInfo = (); } impl pallet_utility::Config for Test { - type Event = TestEvent; + type Event = Event; type Call = Call; type WeightInfo = (); } @@ -140,7 +130,7 @@ impl Filter for BaseFilter { } } impl Config for Test { - type Event = TestEvent; + type Event = Event; type Call = Call; type Currency = Balances; type ProxyType = ProxyType; @@ -154,11 +144,6 @@ impl Config for Test { type AnnouncementDepositFactor = AnnouncementDepositFactor; } -type System = frame_system::Module; -type Balances = pallet_balances::Module; -type Utility = pallet_utility::Module; -type Proxy = Module; - use frame_system::Call as SystemCall; use pallet_balances::Call as BalancesCall; use pallet_balances::Error as BalancesError; @@ -177,19 +162,19 @@ pub fn new_test_ext() -> sp_io::TestExternalities { ext } -fn last_event() -> TestEvent { +fn last_event() -> Event { system::Module::::events().pop().expect("Event expected").event } -fn expect_event>(e: E) { +fn expect_event>(e: E) { assert_eq!(last_event(), e.into()); } -fn last_events(n: usize) -> Vec { +fn last_events(n: usize) -> Vec { system::Module::::events().into_iter().rev().take(n).rev().map(|e| e.event).collect() } -fn expect_events(e: Vec) { +fn expect_events(e: Vec) { assert_eq!(last_events(e.len()), e); } diff --git a/frame/randomness-collective-flip/Cargo.toml b/frame/randomness-collective-flip/Cargo.toml index 9d2683a8a8f85..285326ef1e9a6 100644 --- a/frame/randomness-collective-flip/Cargo.toml +++ b/frame/randomness-collective-flip/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-randomness-collective-flip" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,15 +14,15 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] safe-mix = { version = "1.0", default-features = false } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-io = { version = "2.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } serde = { version = "1.0.101" } [features] diff --git a/frame/recovery/Cargo.toml b/frame/recovery/Cargo.toml index a0d4c0f14df5d..80450db0bd39b 100644 --- a/frame/recovery/Cargo.toml +++ b/frame/recovery/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-recovery" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,17 +14,17 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } enumflags2 = { version = "0.6.2" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -pallet-balances = { version = "2.0.0", path = "../balances" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/scheduler/Cargo.toml b/frame/scheduler/Cargo.toml index 7d21b125e6e65..eef287d86771b 100644 --- a/frame/scheduler/Cargo.toml +++ b/frame/scheduler/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-scheduler" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Unlicense" @@ -11,18 +11,18 @@ readme = "README.md" [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core", default-features = false } -substrate-test-utils = { version = "2.0.0", path = "../../test-utils" } +sp-core = { version = "3.0.0", path = "../../primitives/core", default-features = false } +substrate-test-utils = { version = "3.0.0", path = "../../test-utils" } [features] default = ["std"] diff --git a/frame/scheduler/src/benchmarking.rs b/frame/scheduler/src/benchmarking.rs index defc334ba7368..37ccb900a824a 100644 --- a/frame/scheduler/src/benchmarking.rs +++ b/frame/scheduler/src/benchmarking.rs @@ -23,7 +23,7 @@ use super::*; use sp_std::{vec, prelude::*}; use frame_system::RawOrigin; use frame_support::{ensure, traits::OnInitialize}; -use frame_benchmarking::benchmarks; +use frame_benchmarking::{benchmarks, impl_benchmark_test_suite}; use crate::Module as Scheduler; use frame_system::Module as System; @@ -141,20 +141,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_schedule::()); - assert_ok!(test_benchmark_cancel::()); - assert_ok!(test_benchmark_schedule_named::()); - assert_ok!(test_benchmark_cancel_named::()); - assert_ok!(test_benchmark_on_initialize::()); - }); - } -} +impl_benchmark_test_suite!( + Scheduler, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/scored-pool/Cargo.toml b/frame/scored-pool/Cargo.toml index 6c9bceb32e007..e5e71dba68881 100644 --- a/frame/scored-pool/Cargo.toml +++ b/frame/scored-pool/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-scored-pool" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,17 +13,17 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } serde = { version = "1.0.101", optional = true } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] -pallet-balances = { version = "2.0.0", path = "../balances" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } [features] default = ["std"] diff --git a/frame/session/Cargo.toml b/frame/session/Cargo.toml index 3a9f4609a2e25..5b8fe6e2d137e 100644 --- a/frame/session/Cargo.toml +++ b/frame/session/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-session" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,21 +14,21 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } -sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-timestamp = { version = "2.0.0", default-features = false, path = "../timestamp" } -sp-trie = { version = "2.0.0", optional = true, default-features = false, path = "../../primitives/trie" } -impl-trait-for-tuples = "0.1" +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-session = { version = "3.0.0", default-features = false, path = "../../primitives/session" } +sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +pallet-timestamp = { version = "3.0.0", default-features = false, path = "../timestamp" } +sp-trie = { version = "3.0.0", optional = true, default-features = false, path = "../../primitives/trie" } +impl-trait-for-tuples = "0.2.1" [dev-dependencies] -sp-application-crypto = { version = "2.0.0", path = "../../primitives/application-crypto" } +sp-application-crypto = { version = "3.0.0", path = "../../primitives/application-crypto" } lazy_static = "1.4.0" [features] diff --git a/frame/session/benchmarking/Cargo.toml b/frame/session/benchmarking/Cargo.toml index fc3099e1b95cb..e4db81c4b3bc4 100644 --- a/frame/session/benchmarking/Cargo.toml +++ b/frame/session/benchmarking/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-session-benchmarking" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,30 +13,32 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } -sp-session = { version = "2.0.0", default-features = false, path = "../../../primitives/session" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } -frame-system = { version = "2.0.0", default-features = false, path = "../../system" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../../benchmarking" } -frame-support = { version = "2.0.0", default-features = false, path = "../../support" } -pallet-staking = { version = "2.0.0", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" } -pallet-session = { version = "2.0.0", default-features = false, path = "../../session" } +sp-session = { version = "3.0.0", default-features = false, path = "../../../primitives/session" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../../primitives/std" } +frame-system = { version = "3.0.0", default-features = false, path = "../../system" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../../benchmarking" } +frame-support = { version = "3.0.0", default-features = false, path = "../../support" } +pallet-staking = { version = "3.0.0", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" } +pallet-session = { version = "3.0.0", default-features = false, path = "../../session" } rand = { version = "0.7.2", default-features = false } [dev-dependencies] serde = { version = "1.0.101" } -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -pallet-staking-reward-curve = { version = "2.0.0", path = "../../staking/reward-curve" } -sp-io ={ version = "2.0.0", path = "../../../primitives/io" } -pallet-timestamp = { version = "2.0.0", path = "../../timestamp" } -pallet-balances = { version = "2.0.0", path = "../../balances" } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +pallet-staking-reward-curve = { version = "3.0.0", path = "../../staking/reward-curve" } +sp-io ={ version = "3.0.0", path = "../../../primitives/io" } +pallet-timestamp = { version = "3.0.0", path = "../../timestamp" } +pallet-balances = { version = "3.0.0", path = "../../balances" } +sp-election-providers = { version = "3.0.0", path = "../../../primitives/election-providers" } [features] default = ["std"] std = [ "sp-std/std", "sp-session/std", + "sp-election-providers/std", "sp-runtime/std", "frame-system/std", "frame-benchmarking/std", diff --git a/frame/session/benchmarking/src/lib.rs b/frame/session/benchmarking/src/lib.rs index 06dfa3da34943..8546800ee4fdc 100644 --- a/frame/session/benchmarking/src/lib.rs +++ b/frame/session/benchmarking/src/lib.rs @@ -25,7 +25,7 @@ mod mock; use sp_std::prelude::*; use sp_std::vec; -use frame_benchmarking::benchmarks; +use frame_benchmarking::{benchmarks, impl_benchmark_test_suite}; use frame_support::{ codec::Decode, storage::StorageValue, @@ -169,17 +169,9 @@ fn check_membership_proof_setup( (key, Historical::::prove(key).unwrap()) } -#[cfg(test)] -mod tests { - use super::*; - use crate::mock::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_set_keys::()); - assert_ok!(test_benchmark_purge_keys::()); - }); - } -} +impl_benchmark_test_suite!( + Module, + crate::mock::new_test_ext(), + crate::mock::Test, + extra = false, +); diff --git a/frame/session/benchmarking/src/mock.rs b/frame/session/benchmarking/src/mock.rs index 711cde8e8ecf5..0eba5452b28d0 100644 --- a/frame/session/benchmarking/src/mock.rs +++ b/frame/session/benchmarking/src/mock.rs @@ -20,30 +20,29 @@ #![cfg(test)] use sp_runtime::traits::IdentityLookup; -use frame_support::{impl_outer_origin, impl_outer_dispatch, parameter_types}; +use sp_election_providers::onchain; +use frame_support::parameter_types; type AccountId = u64; type AccountIndex = u32; type BlockNumber = u64; type Balance = u64; -type System = frame_system::Module; -type Balances = pallet_balances::Module; -type Staking = pallet_staking::Module; -type Session = pallet_session::Module; - -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} - -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - pallet_staking::Staking, +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, + Session: pallet_session::{Module, Call, Storage, Event, Config}, } -} - -#[derive(Clone, Eq, PartialEq, Debug)] -pub struct Test; +); impl frame_system::Config for Test { type BaseCallFilter = (); @@ -59,10 +58,10 @@ impl frame_system::Config for Test { type AccountId = AccountId; type Lookup = IdentityLookup; type Header = sp_runtime::testing::Header; - type Event = (); + type Event = Event; type BlockHashCount = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -75,7 +74,7 @@ parameter_types! { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = Balance; - type Event = (); + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -123,7 +122,7 @@ impl pallet_session::Config for Test { type ShouldEndSession = pallet_session::PeriodicSessions<(), ()>; type NextSessionRotation = pallet_session::PeriodicSessions<(), ()>; type SessionHandler = TestSessionHandler; - type Event = (); + type Event = Event; type ValidatorId = AccountId; type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); @@ -147,19 +146,27 @@ parameter_types! { pub type Extrinsic = sp_runtime::testing::TestXt; -impl frame_system::offchain::SendTransactionTypes for Test where +impl frame_system::offchain::SendTransactionTypes for Test +where Call: From, { type OverarchingCall = Call; type Extrinsic = Extrinsic; } +impl onchain::Config for Test { + type AccountId = AccountId; + type BlockNumber = BlockNumber; + type Accuracy = sp_runtime::Perbill; + type DataProvider = Staking; +} + impl pallet_staking::Config for Test { type Currency = Balances; type UnixTime = pallet_timestamp::Module; type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote; type RewardRemainder = (); - type Event = (); + type Event = Event; type Slash = (); type Reward = (); type SessionsPerEra = (); @@ -176,6 +183,7 @@ impl pallet_staking::Config for Test { type MaxIterations = (); type MinSolutionScoreBump = (); type OffchainSolutionWeightLimit = (); + type ElectionProvider = onchain::OnChainSequentialPhragmen; type WeightInfo = (); } diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index 48f32af7b474c..9b4d2704cf456 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -31,8 +31,10 @@ use codec::{Encode, Decode}; use sp_runtime::KeyTypeId; use sp_runtime::traits::{Convert, OpaqueKeys}; use sp_session::{MembershipProof, ValidatorCount}; -use frame_support::{decl_module, decl_storage}; -use frame_support::{Parameter, print}; +use frame_support::{ + decl_module, decl_storage, Parameter, print, + traits::{ValidatorSet, ValidatorSetWithIdentification}, +}; use sp_trie::{MemoryDB, Trie, TrieMut, Recorder, EMPTY_PREFIX}; use sp_trie::trie_types::{TrieDBMut, TrieDB}; use super::{SessionIndex, Module as SessionModule}; @@ -102,6 +104,24 @@ impl Module { } } +impl ValidatorSet for Module { + type ValidatorId = T::ValidatorId; + type ValidatorIdOf = T::ValidatorIdOf; + + fn session_index() -> sp_staking::SessionIndex { + super::Module::::current_index() + } + + fn validators() -> Vec { + super::Module::::validators() + } +} + +impl ValidatorSetWithIdentification for Module { + type Identification = T::FullIdentification; + type IdentificationOf = T::FullIdentificationOf; +} + /// Specialization of the crate-level `SessionManager` which returns the set of full identification /// when creating a new session. pub trait SessionManager: crate::SessionManager { diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 74105ade15f17..d95d99389f732 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -116,13 +116,14 @@ pub mod weights; use sp_std::{prelude::*, marker::PhantomData, ops::{Sub, Rem}}; use codec::Decode; -use sp_runtime::{KeyTypeId, Perbill, RuntimeAppPublic, BoundToRuntimeAppPublic}; +use sp_runtime::{KeyTypeId, Perbill, RuntimeAppPublic}; use sp_runtime::traits::{Convert, Zero, Member, OpaqueKeys, Saturating}; use sp_staking::SessionIndex; use frame_support::{ ensure, decl_module, decl_event, decl_storage, decl_error, ConsensusEngineId, Parameter, traits::{ Get, FindAuthor, ValidatorRegistration, EstimateNextSessionRotation, EstimateNextNewSession, + OneSessionHandler, ValidatorSet, }, dispatch::{self, DispatchResult, DispatchError}, weights::Weight, @@ -168,11 +169,13 @@ impl< Some(if now > offset { let block_after_last_session = (now.clone() - offset) % period.clone(); if block_after_last_session > Zero::zero() { - now.saturating_add( - period.saturating_sub(block_after_last_session) - ) + now.saturating_add(period.saturating_sub(block_after_last_session)) } else { - now + // this branch happens when the session is already rotated or will rotate in this + // block (depending on being called before or after `session::on_initialize`). Here, + // we assume the latter, namely that this is called after `session::on_initialize`, + // and thus we add period to it as well. + now + period } } else { offset @@ -186,6 +189,10 @@ impl< // reasonable to come back here and properly calculate the weight of this function. 0 } + + fn average_session_length() -> BlockNumber { + Period::get() + } } /// A trait for managing creation of new validator set. @@ -256,45 +263,9 @@ pub trait SessionHandler { fn on_disabled(validator_index: usize); } -/// A session handler for specific key type. -pub trait OneSessionHandler: BoundToRuntimeAppPublic { - /// The key type expected. - type Key: Decode + Default + RuntimeAppPublic; - - fn on_genesis_session<'a, I: 'a>(validators: I) - where I: Iterator, ValidatorId: 'a; - - /// Session set has changed; act appropriately. Note that this can be called - /// before initialization of your module. - /// - /// `changed` is true when at least one of the session keys - /// or the underlying economic identities/distribution behind one the - /// session keys has changed, false otherwise. - /// - /// The `validators` are the validators of the incoming session, and `queued_validators` - /// will follow. - fn on_new_session<'a, I: 'a>( - changed: bool, - validators: I, - queued_validators: I, - ) where I: Iterator, ValidatorId: 'a; - - - /// A notification for end of the session. - /// - /// Note it is triggered before any `SessionManager::end_session` handlers, - /// so we can still affect the validator set. - fn on_before_session_ending() {} - - /// A validator got disabled. Act accordingly until a new session begins. - fn on_disabled(_validator_index: usize); -} - #[impl_trait_for_tuples::impl_for_tuples(1, 30)] -#[tuple_types_no_default_trait_bound] +#[tuple_types_custom_trait_bound(OneSessionHandler)] impl SessionHandler for Tuple { - for_tuples!( where #( Tuple: OneSessionHandler )* ); - for_tuples!( const KEY_TYPE_IDS: &'static [KeyTypeId] = &[ #( ::ID ),* ]; ); @@ -830,6 +801,19 @@ impl Module { } } +impl ValidatorSet for Module { + type ValidatorId = T::ValidatorId; + type ValidatorIdOf = T::ValidatorIdOf; + + fn session_index() -> sp_staking::SessionIndex { + Module::::current_index() + } + + fn validators() -> Vec { + Module::::validators() + } +} + /// Wraps the author-scraping logic for consensus engines that can recover /// the canonical index of an author. This then transforms it into the /// registering account-ID of that session key index. @@ -855,6 +839,10 @@ impl EstimateNextNewSession for Module { T::NextSessionRotation::estimate_next_session_rotation(now) } + fn average_session_length() -> T::BlockNumber { + T::NextSessionRotation::average_session_length() + } + fn weight(now: T::BlockNumber) -> Weight { T::NextSessionRotation::weight(now) } diff --git a/frame/session/src/tests.rs b/frame/session/src/tests.rs index 7c1d3c9dcdd24..b2e086aed90c5 100644 --- a/frame/session/src/tests.rs +++ b/frame/session/src/tests.rs @@ -18,6 +18,7 @@ // Tests for the Session Pallet use super::*; +use codec::Decode; use frame_support::{traits::OnInitialize, assert_ok}; use sp_core::crypto::key_types::DUMMY; use sp_runtime::testing::UintAuthorityId; @@ -274,7 +275,7 @@ fn periodic_session_works() { } assert!(P::should_end_session(13u64)); - assert_eq!(P::estimate_next_session_rotation(13u64).unwrap(), 13); + assert_eq!(P::estimate_next_session_rotation(13u64).unwrap(), 23); assert!(!P::should_end_session(14u64)); assert_eq!(P::estimate_next_session_rotation(14u64).unwrap(), 23); diff --git a/frame/society/Cargo.toml b/frame/society/Cargo.toml index fce6ebe51bb30..5ddebeb9f5791 100644 --- a/frame/society/Cargo.toml +++ b/frame/society/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-society" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,17 +14,17 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } rand_chacha = { version = "0.2", default-features = false } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-io ={ version = "2.0.0", path = "../../primitives/io" } -pallet-balances = { version = "2.0.0", path = "../balances" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-io ={ version = "3.0.0", path = "../../primitives/io" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/society/src/mock.rs b/frame/society/src/mock.rs index 8c39a0bc3ea59..4b1bb21dd18db 100644 --- a/frame/society/src/mock.rs +++ b/frame/society/src/mock.rs @@ -83,7 +83,7 @@ impl frame_system::Config for Test { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type OnNewAccount = (); type OnKilledAccount = (); type AccountData = pallet_balances::AccountData; diff --git a/frame/staking/Cargo.toml b/frame/staking/Cargo.toml index 7c2fc21fde54e..11de7e63ea940 100644 --- a/frame/staking/Cargo.toml +++ b/frame/staking/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-staking" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,31 +15,34 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] static_assertions = "1.1.0" serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-npos-elections = { version = "2.0.0", default-features = false, path = "../../primitives/npos-elections" } -sp-io ={ version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-staking = { version = "2.0.0", default-features = false, path = "../../primitives/staking" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-session = { version = "2.0.0", default-features = false, features = ["historical"], path = "../session" } -pallet-authorship = { version = "2.0.0", default-features = false, path = "../authorship" } -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../primitives/application-crypto" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +# TWO_PHASE_NOTE:: ideally we should be able to get rid of this. +sp-npos-elections = { version = "3.0.0", default-features = false, path = "../../primitives/npos-elections" } +sp-io ={ version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +pallet-session = { version = "3.0.0", default-features = false, features = ["historical"], path = "../session" } +pallet-authorship = { version = "3.0.0", default-features = false, path = "../authorship" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../primitives/application-crypto" } +sp-election-providers = { version = "3.0.0", default-features = false, path = "../../primitives/election-providers" } # Optional imports for benchmarking -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } rand_chacha = { version = "0.2", default-features = false, optional = true } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-storage = { version = "2.0.0", path = "../../primitives/storage" } -sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" } -pallet-balances = { version = "2.0.0", path = "../balances" } -pallet-timestamp = { version = "2.0.0", path = "../timestamp" } -pallet-staking-reward-curve = { version = "2.0.0", path = "../staking/reward-curve" } -substrate-test-utils = { version = "2.0.0", path = "../../test-utils" } -frame-benchmarking = { version = "2.0.0", path = "../benchmarking" } +sp-storage = { version = "3.0.0", path = "../../primitives/storage" } +sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-timestamp = { version = "3.0.0", path = "../timestamp" } +pallet-staking-reward-curve = { version = "3.0.0", path = "../staking/reward-curve" } +substrate-test-utils = { version = "3.0.0", path = "../../test-utils" } +frame-benchmarking = { version = "3.0.0", path = "../benchmarking" } +sp-election-providers = { version = "3.0.0", features = ["runtime-benchmarks"], path = "../../primitives/election-providers" } rand_chacha = { version = "0.2" } parking_lot = "0.11.1" hex = "0.4" @@ -59,8 +62,10 @@ std = [ "frame-system/std", "pallet-authorship/std", "sp-application-crypto/std", + "sp-election-providers/std", ] runtime-benchmarks = [ "frame-benchmarking", + "sp-election-providers/runtime-benchmarks", "rand_chacha", ] diff --git a/frame/staking/fuzzer/Cargo.toml b/frame/staking/fuzzer/Cargo.toml index db65e347d8e2a..84758c6bf65ce 100644 --- a/frame/staking/fuzzer/Cargo.toml +++ b/frame/staking/fuzzer/Cargo.toml @@ -14,20 +14,27 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] honggfuzz = "0.5" -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -pallet-staking = { version = "2.0.0", path = "..", features = ["runtime-benchmarks"] } -pallet-staking-reward-curve = { version = "2.0.0", path = "../reward-curve" } -pallet-session = { version = "2.0.0", path = "../../session" } -pallet-indices = { version = "2.0.0", path = "../../indices" } -pallet-balances = { version = "2.0.0", path = "../../balances" } -pallet-timestamp = { version = "2.0.0", path = "../../timestamp" } -frame-system = { version = "2.0.0", path = "../../system" } -frame-support = { version = "2.0.0", path = "../../support" } -sp-std = { version = "2.0.0", path = "../../../primitives/std" } -sp-io ={ version = "2.0.0", path = "../../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-npos-elections = { version = "2.0.0", path = "../../../primitives/npos-elections" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +pallet-staking = { version = "3.0.0", path = "..", features = ["runtime-benchmarks"] } +pallet-staking-reward-curve = { version = "3.0.0", path = "../reward-curve" } +pallet-session = { version = "3.0.0", path = "../../session" } +pallet-indices = { version = "3.0.0", path = "../../indices" } +pallet-balances = { version = "3.0.0", path = "../../balances" } +pallet-timestamp = { version = "3.0.0", path = "../../timestamp" } +frame-system = { version = "3.0.0", path = "../../system" } +frame-support = { version = "3.0.0", path = "../../support" } +sp-std = { version = "3.0.0", path = "../../../primitives/std" } +sp-io ={ version = "3.0.0", path = "../../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-npos-elections = { version = "3.0.0", path = "../../../primitives/npos-elections" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-election-providers = { version = "3.0.0", path = "../../../primitives/election-providers" } +serde = "1.0.101" + +[features] +# Note feature std is required so that impl_opaque_keys derive serde. +default = ["std"] +std = [] [[bin]] name = "submit_solution" diff --git a/frame/staking/fuzzer/src/mock.rs b/frame/staking/fuzzer/src/mock.rs index 75e67fa36518a..05d001d23858e 100644 --- a/frame/staking/fuzzer/src/mock.rs +++ b/frame/staking/fuzzer/src/mock.rs @@ -17,31 +17,29 @@ //! Mock file for staking fuzzing. -use frame_support::{impl_outer_origin, impl_outer_dispatch, parameter_types}; +use frame_support::parameter_types; type AccountId = u64; type AccountIndex = u32; type BlockNumber = u64; type Balance = u64; -pub type System = frame_system::Module; -pub type Balances = pallet_balances::Module; -pub type Staking = pallet_staking::Module; -pub type Indices = pallet_indices::Module; -pub type Session = pallet_session::Module; - -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} - -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - staking::Staking, +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, + Indices: pallet_indices::{Module, Call, Storage, Config, Event}, + Session: pallet_session::{Module, Call, Storage, Event, Config}, } -} - -#[derive(Clone, Eq, PartialEq, Debug)] -pub struct Test; +); impl frame_system::Config for Test { type BaseCallFilter = (); @@ -57,10 +55,10 @@ impl frame_system::Config for Test { type AccountId = AccountId; type Lookup = Indices; type Header = sp_runtime::testing::Header; - type Event = (); + type Event = Event; type BlockHashCount = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -73,7 +71,7 @@ parameter_types! { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = Balance; - type Event = (); + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -81,7 +79,7 @@ impl pallet_balances::Config for Test { } impl pallet_indices::Config for Test { type AccountIndex = AccountIndex; - type Event = (); + type Event = Event; type Currency = Balances; type Deposit = (); type WeightInfo = (); @@ -127,7 +125,7 @@ impl pallet_session::Config for Test { type ShouldEndSession = pallet_session::PeriodicSessions<(), ()>; type NextSessionRotation = pallet_session::PeriodicSessions<(), ()>; type SessionHandler = TestSessionHandler; - type Event = (); + type Event = Event; type ValidatorId = AccountId; type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); @@ -151,19 +149,30 @@ parameter_types! { pub type Extrinsic = sp_runtime::testing::TestXt; -impl frame_system::offchain::SendTransactionTypes for Test where +impl frame_system::offchain::SendTransactionTypes for Test +where Call: From, { type OverarchingCall = Call; type Extrinsic = Extrinsic; } +pub struct MockElectionProvider; +impl sp_election_providers::ElectionProvider for MockElectionProvider { + type Error = (); + type DataProvider = pallet_staking::Module; + + fn elect() -> Result, Self::Error> { + Err(()) + } +} + impl pallet_staking::Config for Test { type Currency = Balances; type UnixTime = pallet_timestamp::Module; type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote; type RewardRemainder = (); - type Event = (); + type Event = Event; type Slash = (); type Reward = (); type SessionsPerEra = (); @@ -181,4 +190,5 @@ impl pallet_staking::Config for Test { type UnsignedPriority = (); type OffchainSolutionWeightLimit = (); type WeightInfo = (); + type ElectionProvider = MockElectionProvider; } diff --git a/frame/staking/fuzzer/src/submit_solution.rs b/frame/staking/fuzzer/src/submit_solution.rs index d94ee49b96db4..b661a83a1bdd0 100644 --- a/frame/staking/fuzzer/src/submit_solution.rs +++ b/frame/staking/fuzzer/src/submit_solution.rs @@ -164,7 +164,7 @@ fn main() { assert_eq!( call.dispatch_bypass_filter(origin.into()).unwrap_err().error, DispatchError::Module { - index: 0, + index: 2, error: 16, message: Some("OffchainElectionWeakSubmission"), }, diff --git a/frame/staking/reward-curve/Cargo.toml b/frame/staking/reward-curve/Cargo.toml index 9d2564522cd92..8713f5e1001cc 100644 --- a/frame/staking/reward-curve/Cargo.toml +++ b/frame/staking/reward-curve/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-staking-reward-curve" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -21,4 +21,4 @@ proc-macro2 = "1.0.6" proc-macro-crate = "0.1.4" [dev-dependencies] -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } diff --git a/frame/staking/reward-curve/src/lib.rs b/frame/staking/reward-curve/src/lib.rs index 3a8d625e83575..2e1bc1f1859d7 100644 --- a/frame/staking/reward-curve/src/lib.rs +++ b/frame/staking/reward-curve/src/lib.rs @@ -353,13 +353,13 @@ fn generate_piecewise_linear(points: Vec<(u32, u32)>) -> TokenStream2 { .unwrap_or(1_000_000_000); for (x, y) in points { - let error = || panic!(format!( + let error = || panic!( "Generated reward curve approximation doesn't fit into [0, 1] -> [0, 1] \ because of point: x = {:07} per million y = {:07} per million", x, y - )); + ); let x_perbill = x.checked_mul(1_000).unwrap_or_else(error); let y_perbill = y.checked_mul(1_000).unwrap_or_else(error); @@ -420,14 +420,14 @@ fn generate_test_module(input: &INposInput) -> TokenStream2 { / float_res as u64 ) as u32; if err > #precision { - panic!(format!("\n\ + panic!("\n\ Generated reward curve approximation differ from real one:\n\t\ for i = {} and base = {}, f(i/base) * base = {},\n\t\ but approximation = {},\n\t\ err = {:07} millionth,\n\t\ try increase the number of segment: {} or the test_error: {}.\n", i, base, float_res, int_res, err, #max_piece_count, #precision - )); + ); } } } diff --git a/frame/staking/src/benchmarking.rs b/frame/staking/src/benchmarking.rs index beddc326b5109..ecaa9889b5fb2 100644 --- a/frame/staking/src/benchmarking.rs +++ b/frame/staking/src/benchmarking.rs @@ -24,7 +24,13 @@ use testing_utils::*; use sp_npos_elections::CompactSolution; use sp_runtime::traits::One; use frame_system::RawOrigin; -pub use frame_benchmarking::{benchmarks, account, whitelisted_caller, whitelist_account}; +pub use frame_benchmarking::{ + benchmarks, + account, + whitelisted_caller, + whitelist_account, + impl_benchmark_test_suite, +}; const SEED: u32 = 0; const MAX_SPANS: u32 = 100; const MAX_VALIDATORS: u32 = 1000; @@ -861,40 +867,6 @@ mod tests { }); } - #[test] - fn test_benchmarks() { - ExtBuilder::default().has_stakers(true).build().execute_with(|| { - assert_ok!(test_benchmark_bond::()); - assert_ok!(test_benchmark_bond_extra::()); - assert_ok!(test_benchmark_unbond::()); - assert_ok!(test_benchmark_withdraw_unbonded_update::()); - assert_ok!(test_benchmark_withdraw_unbonded_kill::()); - assert_ok!(test_benchmark_validate::()); - assert_ok!(test_benchmark_kick::()); - assert_ok!(test_benchmark_nominate::()); - assert_ok!(test_benchmark_chill::()); - assert_ok!(test_benchmark_set_payee::()); - assert_ok!(test_benchmark_set_controller::()); - assert_ok!(test_benchmark_set_validator_count::()); - assert_ok!(test_benchmark_force_no_eras::()); - assert_ok!(test_benchmark_force_new_era::()); - assert_ok!(test_benchmark_force_new_era_always::()); - assert_ok!(test_benchmark_set_invulnerables::()); - assert_ok!(test_benchmark_force_unstake::()); - assert_ok!(test_benchmark_cancel_deferred_slash::()); - assert_ok!(test_benchmark_payout_stakers_dead_controller::()); - assert_ok!(test_benchmark_payout_stakers_alive_staked::()); - assert_ok!(test_benchmark_rebond::()); - assert_ok!(test_benchmark_set_history_depth::()); - assert_ok!(test_benchmark_reap_stash::()); - assert_ok!(test_benchmark_new_era::()); - assert_ok!(test_benchmark_do_slash::()); - assert_ok!(test_benchmark_payout_all::()); - // only run one of them to same time on the CI. ignore the other two. - assert_ok!(test_benchmark_submit_solution_initial::()); - }); - } - #[test] #[ignore] fn test_benchmarks_offchain() { @@ -905,3 +877,9 @@ mod tests { } } + +impl_benchmark_test_suite!( + Staking, + crate::mock::ExtBuilder::default().has_stakers(true).build(), + crate::mock::Test, +); diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 3ea66e937e83c..a74b2d55233ec 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -328,15 +328,13 @@ use frame_system::{ }; use sp_npos_elections::{ ExtendedBalance, Assignment, ElectionScore, ElectionResult as PrimitiveElectionResult, - to_support_map, EvaluateSupport, seq_phragmen, generate_solution_type, is_score_better, - SupportMap, VoteWeight, CompactSolution, PerThing128, + to_supports, EvaluateSupport, seq_phragmen, generate_solution_type, is_score_better, Supports, + VoteWeight, CompactSolution, PerThing128, }; +use sp_election_providers::ElectionProvider; pub use weights::WeightInfo; const STAKING_ID: LockIdentifier = *b"staking "; -pub const MAX_UNLOCKING_CHUNKS: usize = 32; -pub const MAX_NOMINATIONS: usize = ::LIMIT; - pub(crate) const LOG_TARGET: &'static str = "staking"; // syntactic sugar for logging. @@ -345,7 +343,7 @@ macro_rules! log { ($level:tt, $patter:expr $(, $values:expr)* $(,)?) => { frame_support::debug::$level!( target: crate::LOG_TARGET, - $patter $(, $values)* + concat!("💸 ", $patter) $(, $values)* ) }; } @@ -365,6 +363,10 @@ static_assertions::const_assert!(size_of::() <= size_of::() /// Maximum number of stakers that can be stored in a snapshot. pub(crate) const MAX_VALIDATORS: usize = ValidatorIndex::max_value() as usize; pub(crate) const MAX_NOMINATORS: usize = NominatorIndex::max_value() as usize; +pub const MAX_NOMINATIONS: usize = + ::LIMIT; + +pub const MAX_UNLOCKING_CHUNKS: usize = 32; /// Counter for the number of eras that have passed. pub type EraIndex = u32; @@ -388,10 +390,12 @@ pub type OffchainAccuracy = PerU16; pub type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; -type PositiveImbalanceOf = - <::Currency as Currency<::AccountId>>::PositiveImbalance; -type NegativeImbalanceOf = - <::Currency as Currency<::AccountId>>::NegativeImbalance; +type PositiveImbalanceOf = <::Currency as Currency< + ::AccountId, +>>::PositiveImbalance; +type NegativeImbalanceOf = <::Currency as Currency< + ::AccountId, +>>::NegativeImbalance; /// Information regarding the active era (era in used in session). #[derive(Encode, Decode, RuntimeDebug)] @@ -778,7 +782,7 @@ impl SessionInterface<::AccountId> for T w pub trait Config: frame_system::Config + SendTransactionTypes> { /// The staking balance. - type Currency: LockableCurrency; + type Currency: LockableCurrency; /// Time used for computing era duration. /// @@ -793,6 +797,14 @@ pub trait Config: frame_system::Config + SendTransactionTypes> { /// [`BalanceOf`]. type CurrencyToVote: CurrencyToVote>; + /// Something that provides the election functionality. + type ElectionProvider: sp_election_providers::ElectionProvider< + Self::AccountId, + Self::BlockNumber, + // we only accept an election provider that has staking as data provider. + DataProvider = Module, + >; + /// Tokens have been minted and are unused for validator-reward. /// See [Era payout](./index.html#era-payout). type RewardRemainder: OnUnbalanced>; @@ -889,7 +901,9 @@ pub enum Forcing { } impl Default for Forcing { - fn default() -> Self { Forcing::NotForcing } + fn default() -> Self { + Forcing::NotForcing + } } // A value placed in storage that represents the current version of the Staking storage. This value @@ -1066,28 +1080,45 @@ decl_storage! { /// The earliest era for which we have a pending, unapplied slash. EarliestUnappliedSlash: Option; + /// The last planned session scheduled by the session pallet. + /// + /// This is basically in sync with the call to [`SessionManager::new_session`]. + pub CurrentPlannedSession get(fn current_planned_session): SessionIndex; + /// Snapshot of validators at the beginning of the current election window. This should only /// have a value when [`EraElectionStatus`] == `ElectionStatus::Open(_)`. + /// + /// TWO_PHASE_NOTE: should be removed once we switch to multi-phase. pub SnapshotValidators get(fn snapshot_validators): Option>; /// Snapshot of nominators at the beginning of the current election window. This should only /// have a value when [`EraElectionStatus`] == `ElectionStatus::Open(_)`. + /// + /// TWO_PHASE_NOTE: should be removed once we switch to multi-phase. pub SnapshotNominators get(fn snapshot_nominators): Option>; /// The next validator set. At the end of an era, if this is available (potentially from the /// result of an offchain worker), it is immediately used. Otherwise, the on-chain election /// is executed. + /// + /// TWO_PHASE_NOTE: should be removed once we switch to multi-phase. pub QueuedElected get(fn queued_elected): Option>>; /// The score of the current [`QueuedElected`]. + /// + /// TWO_PHASE_NOTE: should be removed once we switch to multi-phase. pub QueuedScore get(fn queued_score): Option; /// Flag to control the execution of the offchain election. When `Open(_)`, we accept /// solutions to be submitted. + /// + /// TWO_PHASE_NOTE: should be removed once we switch to multi-phase. pub EraElectionStatus get(fn era_election_status): ElectionStatus; /// True if the current **planned** session is final. Note that this does not take era /// forcing into account. + /// + /// TWO_PHASE_NOTE: should be removed once we switch to multi-phase. pub IsCurrentSessionFinal get(fn is_current_session_final): bool = false; /// True if network has been upgraded to this version. @@ -1345,14 +1376,14 @@ decl_module! { ElectionStatus::::Open(now) ); add_weight(0, 1, 0); - log!(info, "💸 Election window is Open({:?}). Snapshot created", now); + log!(info, "Election window is Open({:?}). Snapshot created", now); } else { - log!(warn, "💸 Failed to create snapshot at {:?}.", now); + log!(warn, "Failed to create snapshot at {:?}.", now); } } } } else { - log!(warn, "💸 Estimating next session change failed."); + log!(warn, "Estimating next session change failed."); } add_weight(0, 0, T::NextNewSession::weight(now)) } @@ -1367,16 +1398,15 @@ decl_module! { /// to open. If so, it runs the offchain worker code. fn offchain_worker(now: T::BlockNumber) { use offchain_election::{set_check_offchain_execution_status, compute_offchain_election}; - if Self::era_election_status().is_open_at(now) { let offchain_status = set_check_offchain_execution_status::(now); if let Err(why) = offchain_status { - log!(warn, "💸 skipping offchain worker in open election window due to [{}]", why); + log!(warn, "skipping offchain worker in open election window due to [{}]", why); } else { if let Err(e) = compute_offchain_election::() { - log!(error, "💸 Error in election offchain worker: {:?}", e); + log!(error, "Error in election offchain worker: {:?}", e); } else { - log!(debug, "💸 Executed offchain worker thread without errors."); + log!(debug, "Executed offchain worker thread without errors."); } } } @@ -1638,7 +1668,7 @@ decl_module! { ledger = ledger.consolidate_unlocked(current_era) } - let post_info_weight = if ledger.unlocking.is_empty() && ledger.active <= T::Currency::minimum_balance() { + let post_info_weight = if ledger.unlocking.is_empty() && ledger.active < T::Currency::minimum_balance() { // This account must have called `unbond()` with some value that caused the active // portion to fall below existential deposit + will have no more unlocking chunks // left. We can now safely remove all staking-related information. @@ -2267,7 +2297,10 @@ impl Module { } /// Internal impl of [`Self::slashable_balance_of`] that returns [`VoteWeight`]. - pub fn slashable_balance_of_vote_weight(stash: &T::AccountId, issuance: BalanceOf) -> VoteWeight { + pub fn slashable_balance_of_vote_weight( + stash: &T::AccountId, + issuance: BalanceOf, + ) -> VoteWeight { T::CurrencyToVote::to_vote(Self::slashable_balance_of(stash), issuance) } @@ -2306,7 +2339,7 @@ impl Module { { log!( warn, - "💸 Snapshot size too big [{} <> {}][{} <> {}].", + "Snapshot size too big [{} <> {}][{} <> {}].", num_validators, MAX_VALIDATORS, num_nominators, @@ -2330,10 +2363,7 @@ impl Module { >::kill(); } - fn do_payout_stakers( - validator_stash: T::AccountId, - era: EraIndex, - ) -> DispatchResult { + fn do_payout_stakers(validator_stash: T::AccountId, era: EraIndex) -> DispatchResult { // Validate input data let current_era = CurrentEra::get().ok_or(Error::::InvalidEraToReward)?; ensure!(era <= current_era, Error::::InvalidEraToReward); @@ -2626,7 +2656,7 @@ impl Module { validator_at, ).map_err(|e| { // log the error since it is not propagated into the runtime error. - log!(warn, "💸 un-compacting solution failed due to {:?}", e); + log!(warn, "un-compacting solution failed due to {:?}", e); Error::::OffchainElectionBogusCompact })?; @@ -2641,7 +2671,7 @@ impl Module { // all of the indices must map to either a validator or a nominator. If this is ever // not the case, then the locking system of staking is most likely faulty, or we // have bigger problems. - log!(error, "💸 detected an error in the staking locking and snapshot."); + log!(error, "detected an error in the staking locking and snapshot."); // abort. return Err(Error::::OffchainElectionBogusNominator.into()); } @@ -2690,7 +2720,7 @@ impl Module { ); // build the support map thereof in order to evaluate. - let supports = to_support_map::(&winners, &staked_assignments) + let supports = to_supports(&winners, &staked_assignments) .map_err(|_| Error::::OffchainElectionBogusEdge)?; // Check if the score is the same as the claimed one. @@ -2698,10 +2728,11 @@ impl Module { ensure!(submitted_score == claimed_score, Error::::OffchainElectionBogusScore); // At last, alles Ok. Exposures and store the result. - let exposures = Self::collect_exposure(supports); + let exposures = Self::collect_exposures(supports); log!( info, - "💸 A better solution (with compute {:?} and score {:?}) has been validated and stored on chain.", + "A better solution (with compute {:?} and score {:?}) has been validated and stored \ + on chain.", compute, submitted_score, ); @@ -2834,6 +2865,8 @@ impl Module { // Set staking information for new era. let maybe_new_validators = Self::select_and_update_validators(current_era); + // TWO_PHASE_NOTE: use this later on. + let _unused_new_validators = Self::enact_election(current_era); maybe_new_validators } @@ -2901,7 +2934,7 @@ impl Module { log!( info, - "💸 new validator set of size {:?} has been elected via {:?} for era {:?}", + "new validator set of size {:?} has been elected via {:?} for staring era {:?}", elected_stashes.len(), compute, current_era, @@ -2950,20 +2983,20 @@ impl Module { Self::slashable_balance_of_fn(), ); - let supports = to_support_map::( + let supports = to_supports( &elected_stashes, &staked_assignments, ) .map_err(|_| log!( error, - "💸 on-chain phragmen is failing due to a problem in the result. This must be a bug." + "on-chain phragmen is failing due to a problem in the result. This must be a bug." ) ) .ok()?; // collect exposures - let exposures = Self::collect_exposure(supports); + let exposures = Self::collect_exposures(supports); // In order to keep the property required by `on_session_ending` that we must return the // new validator set even if it's the same as the old, as long as any underlying @@ -3025,7 +3058,7 @@ impl Module { // If we don't have enough candidates, nothing to do. log!( warn, - "💸 Chain does not have enough staking candidates to operate. Era {:?}.", + "chain does not have enough staking candidates to operate. Era {:?}.", Self::current_era() ); None @@ -3041,9 +3074,10 @@ impl Module { } } - /// Consume a set of [`Supports`] from [`sp_npos_elections`] and collect them into a [`Exposure`] - fn collect_exposure( - supports: SupportMap, + /// Consume a set of [`Supports`] from [`sp_npos_elections`] and collect them into a + /// [`Exposure`]. + fn collect_exposures( + supports: Supports, ) -> Vec<(T::AccountId, Exposure>)> { let total_issuance = T::Currency::total_issuance(); let to_currency = |e: ExtendedBalance| T::CurrencyToVote::to_currency(e, total_issuance); @@ -3075,12 +3109,86 @@ impl Module { }).collect::)>>() } + /// Process the output of the election. + /// + /// This ensures enough validators have been elected, converts all supports to exposures and + /// writes them to the associated storage. + /// + /// Returns `Err(())` if less than [`MinimumValidatorCount`] validators have been elected, `Ok` + /// otherwise. + // TWO_PHASE_NOTE: remove the dead code. + #[allow(dead_code)] + pub fn process_election( + flat_supports: sp_npos_elections::Supports, + current_era: EraIndex, + ) -> Result, ()> { + let exposures = Self::collect_exposures(flat_supports); + let elected_stashes = exposures.iter().cloned().map(|(x, _)| x).collect::>(); + + if (elected_stashes.len() as u32) <= Self::minimum_validator_count() { + log!( + warn, + "chain does not have enough staking candidates to operate for era {:?}", + current_era, + ); + return Err(()); + } + + // Populate Stakers and write slot stake. + let mut total_stake: BalanceOf = Zero::zero(); + exposures.into_iter().for_each(|(stash, exposure)| { + total_stake = total_stake.saturating_add(exposure.total); + >::insert(current_era, &stash, &exposure); + + let mut exposure_clipped = exposure; + let clipped_max_len = T::MaxNominatorRewardedPerValidator::get() as usize; + if exposure_clipped.others.len() > clipped_max_len { + exposure_clipped.others.sort_by(|a, b| a.value.cmp(&b.value).reverse()); + exposure_clipped.others.truncate(clipped_max_len); + } + >::insert(¤t_era, &stash, exposure_clipped); + }); + + // Insert current era staking information + >::insert(¤t_era, total_stake); + + // collect the pref of all winners + for stash in &elected_stashes { + let pref = Self::validators(stash); + >::insert(¤t_era, stash, pref); + } + + // emit event + // TWO_PHASE_NOTE: remove the inner value. + Self::deposit_event(RawEvent::StakingElection(ElectionCompute::Signed)); + + log!( + info, + "new validator set of size {:?} has been processed for era {:?}", + elected_stashes.len(), + current_era, + ); + + Ok(elected_stashes) + } + + /// Enact and process the election using the `ElectionProvider` type. + /// + /// This will also process the election, as noted in [`process_election`]. + fn enact_election(_current_era: EraIndex) -> Option> { + let _outcome = T::ElectionProvider::elect().map(|_| ()); + log!(debug, "Experimental election provider outputted {:?}", _outcome); + // TWO_PHASE_NOTE: This code path shall not return anything for now. Later on, redirect the + // results to `process_election`. + None + } + /// Remove all associated data of a stash account from the staking system. /// /// Assumes storage is upgraded before calling. /// /// This is called: - /// - after a `withdraw_unbond()` call that frees all of a stash's bonded balance. + /// - after a `withdraw_unbonded()` call that frees all of a stash's bonded balance. /// - through `reap_stash()` if the balance has fallen to zero (through slashing). fn kill_stash(stash: &T::AccountId, num_slashing_spans: u32) -> DispatchResult { let controller = >::get(stash).ok_or(Error::::NotStash)?; @@ -3167,7 +3275,11 @@ impl Module { } #[cfg(feature = "runtime-benchmarks")] - pub fn add_era_stakers(current_era: EraIndex, controller: T::AccountId, exposure: Exposure>) { + pub fn add_era_stakers( + current_era: EraIndex, + controller: T::AccountId, + exposure: Exposure>, + ) { >::insert(¤t_era, &controller, &exposure); } @@ -3180,6 +3292,109 @@ impl Module { pub fn set_slash_reward_fraction(fraction: Perbill) { SlashRewardFraction::put(fraction); } + + /// Get all of the voters that are eligible for the npos election. + /// + /// This will use all on-chain nominators, and all the validators will inject a self vote. + /// + /// ### Slashing + /// + /// All nominations that have been submitted before the last non-zero slash of the validator are + /// auto-chilled. + /// + /// Note that this is VERY expensive. Use with care. + pub fn get_npos_voters() -> Vec<(T::AccountId, VoteWeight, Vec)> { + let weight_of = Self::slashable_balance_of_fn(); + let mut all_voters = Vec::new(); + + for (validator, _) in >::iter() { + // append self vote + let self_vote = (validator.clone(), weight_of(&validator), vec![validator.clone()]); + all_voters.push(self_vote); + } + + for (nominator, nominations) in >::iter() { + let Nominations { submitted_in, mut targets, suppressed: _ } = nominations; + + // Filter out nomination targets which were nominated before the most recent + // slashing span. + targets.retain(|stash| { + Self::slashing_spans(&stash) + .map_or(true, |spans| submitted_in >= spans.last_nonzero_slash()) + }); + + let vote_weight = weight_of(&nominator); + all_voters.push((nominator, vote_weight, targets)) + } + + all_voters + } + + pub fn get_npos_targets() -> Vec { + >::iter().map(|(v, _)| v).collect::>() + } +} + +impl sp_election_providers::ElectionDataProvider + for Module +{ + fn desired_targets() -> u32 { + Self::validator_count() + } + + fn voters() -> Vec<(T::AccountId, VoteWeight, Vec)> { + Self::get_npos_voters() + } + + fn targets() -> Vec { + Self::get_npos_targets() + } + + fn next_election_prediction(now: T::BlockNumber) -> T::BlockNumber { + let current_era = Self::current_era().unwrap_or(0); + let current_session = Self::current_planned_session(); + let current_era_start_session_index = + Self::eras_start_session_index(current_era).unwrap_or(0); + let era_length = current_session + .saturating_sub(current_era_start_session_index) + .min(T::SessionsPerEra::get()); + + let session_length = T::NextNewSession::average_session_length(); + + let until_this_session_end = T::NextNewSession::estimate_next_new_session(now) + .unwrap_or_default() + .saturating_sub(now); + + let sessions_left: T::BlockNumber = T::SessionsPerEra::get() + .saturating_sub(era_length) + // one session is computed in this_session_end. + .saturating_sub(1) + .into(); + + now.saturating_add( + until_this_session_end.saturating_add(sessions_left.saturating_mul(session_length)), + ) + } + + #[cfg(any(feature = "runtime-benchmarks", test))] + fn put_snapshot( + voters: Vec<(T::AccountId, VoteWeight, Vec)>, + targets: Vec, + ) { + targets.into_iter().for_each(|v| { + >::insert( + v, + ValidatorPrefs { commission: Perbill::zero(), blocked: false }, + ); + }); + + voters.into_iter().for_each(|(v, _s, t)| { + >::insert( + v, + Nominations { targets: t, submitted_in: 0, suppressed: false }, + ); + }); + } } /// In this implementation `new_session(session)` must be called before `end_session(session-1)` @@ -3195,6 +3410,7 @@ impl pallet_session::SessionManager for Module { >::block_number(), new_index ); + CurrentPlannedSession::put(new_index); Self::new_session(new_index) } fn start_session(start_index: SessionIndex) { @@ -3217,10 +3433,12 @@ impl pallet_session::SessionManager for Module { } } -impl historical::SessionManager>> for Module { - fn new_session(new_index: SessionIndex) - -> Option>)>> - { +impl historical::SessionManager>> + for Module +{ + fn new_session( + new_index: SessionIndex, + ) -> Option>)>> { >::new_session(new_index).map(|validators| { let current_era = Self::current_era() // Must be some as a new era has been created. @@ -3245,8 +3463,8 @@ impl historical::SessionManager pallet_authorship::EventHandler for Module - where - T: Config + pallet_authorship::Config + pallet_session::Config +where + T: Config + pallet_authorship::Config + pallet_session::Config, { fn note_author(author: T::AccountId) { Self::reward_by_ids(vec![(author, 20)]) @@ -3289,9 +3507,10 @@ impl Convert } /// This is intended to be used with `FilterHistoricalOffences`. -impl +impl OnOffenceHandler, Weight> -for Module where + for Module +where T: pallet_session::Config::AccountId>, T: pallet_session::historical::Config< FullIdentification = Exposure<::AccountId, BalanceOf>, @@ -3305,12 +3524,15 @@ for Module where >, { fn on_offence( - offenders: &[OffenceDetails>], + offenders: &[OffenceDetails< + T::AccountId, + pallet_session::historical::IdentificationTuple, + >], slash_fraction: &[Perbill], slash_session: SessionIndex, ) -> Result { if !Self::can_report() { - return Err(()) + return Err(()); } let reward_proportion = SlashRewardFraction::get(); @@ -3421,6 +3643,7 @@ for Module where } fn can_report() -> bool { + // TWO_PHASE_NOTE: we can get rid of this API Self::era_election_status().is_closed() } } @@ -3431,7 +3654,8 @@ pub struct FilterHistoricalOffences { } impl ReportOffence - for FilterHistoricalOffences, R> where + for FilterHistoricalOffences, R> +where T: Config, R: ReportOffence, O: Offence, @@ -3488,7 +3712,7 @@ impl frame_support::unsigned::ValidateUnsigned for Module { return invalid.into(); } - log!(debug, "💸 validateUnsigned succeeded for a solution at era {}.", era); + log!(debug, "validateUnsigned succeeded for a solution at era {}.", era); ValidTransaction::with_tag_prefix("StakingOffchain") // The higher the score[0], the better a solution is. diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 5c3261414d2bc..0d6701c48b894 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -21,14 +21,14 @@ use crate::*; use crate as staking; use frame_support::{ assert_ok, parameter_types, - traits::{Currency, FindAuthor, Get, OnFinalize, OnInitialize}, + traits::{Currency, FindAuthor, Get, OnFinalize, OnInitialize, OneSessionHandler}, weights::{constants::RocksDbWeight, Weight}, IterableStorageMap, StorageDoubleMap, StorageMap, StorageValue, }; use sp_core::H256; use sp_io; use sp_npos_elections::{ - to_support_map, EvaluateSupport, reduce, ExtendedBalance, StakedAssignment, ElectionScore, + to_supports, reduce, ExtendedBalance, StakedAssignment, ElectionScore, EvaluateSupport, }; use sp_runtime::{ curve::PiecewiseLinear, @@ -37,6 +37,7 @@ use sp_runtime::{ }; use sp_staking::offence::{OffenceDetails, OnOffenceHandler}; use std::{cell::RefCell, collections::HashSet}; +use sp_election_providers::onchain; pub const INIT_TIMESTAMP: u64 = 30_000; pub const BLOCK_TIME: u64 = 1000; @@ -53,7 +54,7 @@ thread_local! { /// Another session handler struct to test on_disabled. pub struct OtherSessionHandler; -impl pallet_session::OneSessionHandler for OtherSessionHandler { +impl OneSessionHandler for OtherSessionHandler { type Key = UintAuthorityId; fn on_genesis_session<'a, I: 'a>(_: I) @@ -239,6 +240,12 @@ impl OnUnbalanced> for RewardRemainderMock { } } +impl onchain::Config for Test { + type AccountId = AccountId; + type BlockNumber = BlockNumber; + type Accuracy = Perbill; + type DataProvider = Staking; +} impl Config for Test { type Currency = Balances; type UnixTime = Timestamp; @@ -261,6 +268,7 @@ impl Config for Test { type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator; type UnsignedPriority = UnsignedPriority; type OffchainSolutionWeightLimit = OffchainSolutionWeightLimit; + type ElectionProvider = onchain::OnChainSequentialPhragmen; type WeightInfo = (); } @@ -760,7 +768,7 @@ pub(crate) fn add_slash(who: &AccountId) { on_offence_now( &[ OffenceDetails { - offender: (who.clone(), Staking::eras_stakers(Staking::active_era().unwrap().index, who.clone())), + offender: (who.clone(), Staking::eras_stakers(active_era(), who.clone())), reporters: vec![], }, ], @@ -841,7 +849,7 @@ pub(crate) fn horrible_npos_solution( let score = { let (_, _, better_score) = prepare_submission_with(true, true, 0, |_| {}); - let support = to_support_map::(&winners, &staked_assignment).unwrap(); + let support = to_supports::(&winners, &staked_assignment).unwrap(); let score = support.evaluate(); assert!(sp_npos_elections::is_score_better::( @@ -941,7 +949,7 @@ pub(crate) fn prepare_submission_with( Staking::slashable_balance_of_fn(), ); - let support_map = to_support_map::( + let support_map = to_supports( winners.as_slice(), staked.as_slice(), ).unwrap(); @@ -962,9 +970,8 @@ pub(crate) fn prepare_submission_with( /// Make all validator and nominator request their payment pub(crate) fn make_all_reward_payment(era: EraIndex) { - let validators_with_reward = ErasRewardPoints::::get(era).individual.keys() - .cloned() - .collect::>(); + let validators_with_reward = + ErasRewardPoints::::get(era).individual.keys().cloned().collect::>(); // reward validators for validator_controller in validators_with_reward.iter().filter_map(Staking::bonded) { @@ -988,10 +995,10 @@ macro_rules! assert_session_era { $session, ); assert_eq!( - Staking::active_era().unwrap().index, + Staking::current_era().unwrap(), $era, - "wrong active era {} != {}", - Staking::active_era().unwrap().index, + "wrong current era {} != {}", + Staking::current_era().unwrap(), $era, ); }; diff --git a/frame/staking/src/offchain_election.rs b/frame/staking/src/offchain_election.rs index 4f80d75086e7e..8398c2022fc3f 100644 --- a/frame/staking/src/offchain_election.rs +++ b/frame/staking/src/offchain_election.rs @@ -25,7 +25,7 @@ use codec::Decode; use frame_support::{traits::Get, weights::Weight, IterableStorageMap}; use frame_system::offchain::SubmitTransaction; use sp_npos_elections::{ - to_support_map, EvaluateSupport, reduce, Assignment, ElectionResult, ElectionScore, + to_supports, EvaluateSupport, reduce, Assignment, ElectionResult, ElectionScore, ExtendedBalance, CompactSolution, }; use sp_runtime::{ @@ -127,7 +127,7 @@ pub(crate) fn compute_offchain_election() -> Result<(), OffchainElect crate::log!( info, - "💸 prepared a seq-phragmen solution with {} balancing iterations and score {:?}", + "prepared a seq-phragmen solution with {} balancing iterations and score {:?}", iters, score, ); @@ -284,7 +284,7 @@ where if compact.remove_voter(index) { crate::log!( trace, - "💸 removed a voter at index {} with stake {:?} from compact to reduce the size", + "removed a voter at index {} with stake {:?} from compact to reduce the size", index, _stake, ); @@ -297,19 +297,17 @@ where } crate::log!( - warn, - "💸 {} nominators out of {} had to be removed from compact solution due to size limits.", - removed, - compact.voter_count() + removed, - ); + warn, + "{} nominators out of {} had to be removed from compact solution due to size \ + limits.", + removed, + compact.voter_count() + removed, + ); Ok(compact) } _ => { // nada, return as-is - crate::log!( - info, - "💸 Compact solution did not get trimmed due to block weight limits.", - ); + crate::log!(info, "Compact solution did not get trimmed due to block weight limits.",); Ok(compact) } } @@ -390,13 +388,16 @@ pub fn prepare_submission( let maximum_allowed_voters = maximum_compact_len::(winners.len() as u32, size, maximum_weight); - crate::log!(debug, "💸 Maximum weight = {:?} // current weight = {:?} // maximum voters = {:?} // current votes = {:?}", + crate::log!( + debug, + "Maximum weight = {:?} // current weight = {:?} // maximum voters = {:?} // current votes \ + = {:?}", maximum_weight, T::WeightInfo::submit_solution_better( - size.validators.into(), - size.nominators.into(), - compact.voter_count() as u32, - winners.len() as u32, + size.validators.into(), + size.nominators.into(), + compact.voter_count() as u32, + winners.len() as u32, ), maximum_allowed_voters, compact.voter_count(), @@ -415,7 +416,7 @@ pub fn prepare_submission( >::slashable_balance_of_fn(), ); - let support_map = to_support_map::(&winners, &staked) + let support_map = to_supports::(&winners, &staked) .map_err(|_| OffchainElectionError::ElectionFailed)?; support_map.evaluate() }; diff --git a/frame/staking/src/testing_utils.rs b/frame/staking/src/testing_utils.rs index a30c0136550b4..f6ee89704d8d2 100644 --- a/frame/staking/src/testing_utils.rs +++ b/frame/staking/src/testing_utils.rs @@ -247,7 +247,7 @@ pub fn get_weak_solution( ); let support_map = - to_support_map::(winners.as_slice(), staked.as_slice()).unwrap(); + to_supports::(winners.as_slice(), staked.as_slice()).unwrap(); support_map.evaluate() }; diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 1f5e2a48888a5..529cd7b87cabf 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -1833,6 +1833,7 @@ fn bond_with_duplicate_vote_should_be_ignored_by_npos_election() { } assert_ok!(Staking::bond(Origin::signed(1), 2, 1000, RewardDestination::Controller)); + // 11 should not be elected. All of these count as ONE vote. assert_ok!(Staking::nominate(Origin::signed(2), vec![11, 11, 11, 21, 31,])); assert_ok!(Staking::bond(Origin::signed(3), 4, 1000, RewardDestination::Controller)); @@ -1886,7 +1887,6 @@ fn bond_with_duplicate_vote_should_be_ignored_by_npos_election_elected() { assert_ok!(Staking::nominate(Origin::signed(4), vec![21, 31])); // winners should be 21 and 31. Otherwise this election is taking duplicates into account. - let sp_npos_elections::ElectionResult { winners, assignments, @@ -2029,7 +2029,7 @@ fn reward_from_authorship_event_handler_works() { fn add_reward_points_fns_works() { ExtBuilder::default().build_and_execute(|| { // Not mandatory but must be coherent with rewards - assert_eq!(Session::validators(), vec![21, 11]); + assert_eq_uvec!(Session::validators(), vec![21, 11]); >::reward_by_ids(vec![ (21, 1), @@ -3048,7 +3048,7 @@ mod offchain_election { assert_eq!(Staking::era_election_status(), ElectionStatus::Open(37)); run_to_block(40); - assert_session_era!(4, 0); + assert_session_era!(4, 1); assert_eq!(Staking::era_election_status(), ElectionStatus::Closed); assert!(Staking::snapshot_nominators().is_none()); assert!(Staking::snapshot_validators().is_none()); @@ -3066,7 +3066,7 @@ mod offchain_election { assert!(Staking::snapshot_validators().is_some()); run_to_block(90); - assert_session_era!(9, 1); + assert_session_era!(9, 2); assert_eq!(Staking::era_election_status(), ElectionStatus::Closed); assert!(Staking::snapshot_nominators().is_none()); assert!(Staking::snapshot_validators().is_none()); @@ -4978,3 +4978,129 @@ fn cannot_bond_extra_to_lower_than_ed() { ); }) } + +#[test] +fn do_not_die_when_active_is_ed() { + let ed = 10; + ExtBuilder::default() + .existential_deposit(ed) + .build_and_execute(|| { + // initial stuff. + assert_eq!( + Staking::ledger(&20).unwrap(), + StakingLedger { + stash: 21, + total: 1000, + active: 1000, + unlocking: vec![], + claimed_rewards: vec![] + } + ); + + // unbond all of it except ed. + assert_ok!(Staking::unbond(Origin::signed(20), 1000 - ed)); + start_active_era(3); + assert_ok!(Staking::withdraw_unbonded(Origin::signed(20), 100)); + + // initial stuff. + assert_eq!( + Staking::ledger(&20).unwrap(), + StakingLedger { + stash: 21, + total: ed, + active: ed, + unlocking: vec![], + claimed_rewards: vec![] + } + ); + }) +} + +mod election_data_provider { + use super::*; + use sp_election_providers::ElectionDataProvider; + + #[test] + fn voters_include_self_vote() { + ExtBuilder::default().nominate(false).build().execute_with(|| { + assert!(>::iter().map(|(x, _)| x).all(|v| Staking::voters() + .into_iter() + .find(|(w, _, t)| { v == *w && t[0] == *w }) + .is_some())) + }) + } + + #[test] + fn voters_exclude_slashed() { + ExtBuilder::default().build().execute_with(|| { + assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21]); + assert_eq!( + >::voters() + .iter() + .find(|x| x.0 == 101) + .unwrap() + .2, + vec![11, 21] + ); + + start_active_era(1); + add_slash(&11); + + // 11 is gone. + start_active_era(2); + assert_eq!( + >::voters() + .iter() + .find(|x| x.0 == 101) + .unwrap() + .2, + vec![21] + ); + + // resubmit and it is back + assert_ok!(Staking::nominate(Origin::signed(100), vec![11, 21])); + assert_eq!( + >::voters() + .iter() + .find(|x| x.0 == 101) + .unwrap() + .2, + vec![11, 21] + ); + }) + } + + #[test] + fn estimate_next_election_works() { + ExtBuilder::default().session_per_era(5).period(5).build().execute_with(|| { + // first session is always length 0. + for b in 1..20 { + run_to_block(b); + assert_eq!(Staking::next_election_prediction(System::block_number()), 20); + } + + // election + run_to_block(20); + assert_eq!(Staking::next_election_prediction(System::block_number()), 45); + assert_eq!(staking_events().len(), 1); + assert_eq!( + *staking_events().last().unwrap(), + RawEvent::StakingElection(ElectionCompute::OnChain) + ); + + for b in 21..45 { + run_to_block(b); + assert_eq!(Staking::next_election_prediction(System::block_number()), 45); + } + + // election + run_to_block(45); + assert_eq!(Staking::next_election_prediction(System::block_number()), 70); + assert_eq!(staking_events().len(), 3); + assert_eq!( + *staking_events().last().unwrap(), + RawEvent::StakingElection(ElectionCompute::OnChain) + ); + }) + } +} diff --git a/frame/staking/src/weights.rs b/frame/staking/src/weights.rs index b70563ccf41b3..c7b7edad5518a 100644 --- a/frame/staking/src/weights.rs +++ b/frame/staking/src/weights.rs @@ -17,8 +17,8 @@ //! Autogenerated weights for pallet_staking //! -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.1 -//! DATE: 2021-01-19, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 +//! DATE: 2021-02-13, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -75,171 +75,171 @@ pub trait WeightInfo { pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { fn bond() -> Weight { - (76_281_000 as Weight) + (81_642_000 as Weight) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn bond_extra() -> Weight { - (62_062_000 as Weight) + (66_025_000 as Weight) .saturating_add(T::DbWeight::get().reads(4 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn unbond() -> Weight { - (57_195_000 as Weight) + (60_810_000 as Weight) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_update(s: u32, ) -> Weight { - (58_043_000 as Weight) + (61_537_000 as Weight) // Standard Error: 1_000 - .saturating_add((52_000 as Weight).saturating_mul(s as Weight)) + .saturating_add((60_000 as Weight).saturating_mul(s as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_kill(s: u32, ) -> Weight { - (89_920_000 as Weight) - // Standard Error: 3_000 - .saturating_add((2_526_000 as Weight).saturating_mul(s as Weight)) + (95_741_000 as Weight) + // Standard Error: 1_000 + .saturating_add((2_754_000 as Weight).saturating_mul(s as Weight)) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn validate() -> Weight { - (20_228_000 as Weight) + (21_009_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn kick(k: u32, ) -> Weight { - (31_066_000 as Weight) - // Standard Error: 11_000 - .saturating_add((17_754_000 as Weight).saturating_mul(k as Weight)) + (31_832_000 as Weight) + // Standard Error: 15_000 + .saturating_add((19_418_000 as Weight).saturating_mul(k as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(k as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(k as Weight))) } fn nominate(n: u32, ) -> Weight { - (33_494_000 as Weight) - // Standard Error: 23_000 - .saturating_add((5_253_000 as Weight).saturating_mul(n as Weight)) + (34_304_000 as Weight) + // Standard Error: 20_000 + .saturating_add((5_643_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(4 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(n as Weight))) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn chill() -> Weight { - (19_396_000 as Weight) + (20_103_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn set_payee() -> Weight { - (13_449_000 as Weight) + (13_858_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_controller() -> Weight { - (29_184_000 as Weight) + (30_269_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn set_validator_count() -> Weight { - (2_266_000 as Weight) + (2_444_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn force_no_eras() -> Weight { - (2_462_000 as Weight) + (2_766_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn force_new_era() -> Weight { - (2_483_000 as Weight) + (2_724_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn force_new_era_always() -> Weight { - (2_495_000 as Weight) + (2_702_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_invulnerables(v: u32, ) -> Weight { - (2_712_000 as Weight) + (2_914_000 as Weight) // Standard Error: 0 - .saturating_add((9_000 as Weight).saturating_mul(v as Weight)) + .saturating_add((35_000 as Weight).saturating_mul(v as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn force_unstake(s: u32, ) -> Weight { - (60_508_000 as Weight) - // Standard Error: 1_000 - .saturating_add((2_525_000 as Weight).saturating_mul(s as Weight)) + (64_032_000 as Weight) + // Standard Error: 2_000 + .saturating_add((2_787_000 as Weight).saturating_mul(s as Weight)) .saturating_add(T::DbWeight::get().reads(4 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn cancel_deferred_slash(s: u32, ) -> Weight { - (5_886_772_000 as Weight) - // Standard Error: 393_000 - .saturating_add((34_849_000 as Weight).saturating_mul(s as Weight)) + (5_903_394_000 as Weight) + // Standard Error: 391_000 + .saturating_add((34_834_000 as Weight).saturating_mul(s as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn payout_stakers_dead_controller(n: u32, ) -> Weight { - (127_627_000 as Weight) - // Standard Error: 27_000 - .saturating_add((49_354_000 as Weight).saturating_mul(n as Weight)) + (141_724_000 as Weight) + // Standard Error: 24_000 + .saturating_add((53_018_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(11 as Weight)) .saturating_add(T::DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) .saturating_add(T::DbWeight::get().writes(2 as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(n as Weight))) } fn payout_stakers_alive_staked(n: u32, ) -> Weight { - (156_838_000 as Weight) - // Standard Error: 24_000 - .saturating_add((62_653_000 as Weight).saturating_mul(n as Weight)) + (159_994_000 as Weight) + // Standard Error: 28_000 + .saturating_add((67_746_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(12 as Weight)) .saturating_add(T::DbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) .saturating_add(T::DbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } fn rebond(l: u32, ) -> Weight { - (40_110_000 as Weight) + (42_177_000 as Weight) // Standard Error: 1_000 - .saturating_add((78_000 as Weight).saturating_mul(l as Weight)) + .saturating_add((82_000 as Weight).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(4 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn set_history_depth(e: u32, ) -> Weight { (0 as Weight) - // Standard Error: 70_000 - .saturating_add((32_883_000 as Weight).saturating_mul(e as Weight)) + // Standard Error: 65_000 + .saturating_add((34_151_000 as Weight).saturating_mul(e as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) .saturating_add(T::DbWeight::get().writes((7 as Weight).saturating_mul(e as Weight))) } fn reap_stash(s: u32, ) -> Weight { - (64_605_000 as Weight) - // Standard Error: 1_000 - .saturating_add((2_506_000 as Weight).saturating_mul(s as Weight)) + (68_377_000 as Weight) + // Standard Error: 0 + .saturating_add((2_757_000 as Weight).saturating_mul(s as Weight)) .saturating_add(T::DbWeight::get().reads(4 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn new_era(v: u32, n: u32, ) -> Weight { (0 as Weight) - // Standard Error: 926_000 - .saturating_add((548_212_000 as Weight).saturating_mul(v as Weight)) - // Standard Error: 46_000 - .saturating_add((78_343_000 as Weight).saturating_mul(n as Weight)) - .saturating_add(T::DbWeight::get().reads(7 as Weight)) + // Standard Error: 908_000 + .saturating_add((588_562_000 as Weight).saturating_mul(v as Weight)) + // Standard Error: 45_000 + .saturating_add((83_485_000 as Weight).saturating_mul(n as Weight)) + .saturating_add(T::DbWeight::get().reads(9 as Weight)) .saturating_add(T::DbWeight::get().reads((4 as Weight).saturating_mul(v as Weight))) .saturating_add(T::DbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) - .saturating_add(T::DbWeight::get().writes(8 as Weight)) + .saturating_add(T::DbWeight::get().writes(13 as Weight)) .saturating_add(T::DbWeight::get().writes((3 as Weight).saturating_mul(v as Weight))) } fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { (0 as Weight) - // Standard Error: 48_000 - .saturating_add((937_000 as Weight).saturating_mul(v as Weight)) - // Standard Error: 19_000 - .saturating_add((657_000 as Weight).saturating_mul(n as Weight)) - // Standard Error: 48_000 - .saturating_add((70_669_000 as Weight).saturating_mul(a as Weight)) - // Standard Error: 101_000 - .saturating_add((7_658_000 as Weight).saturating_mul(w as Weight)) + // Standard Error: 52_000 + .saturating_add((750_000 as Weight).saturating_mul(v as Weight)) + // Standard Error: 20_000 + .saturating_add((556_000 as Weight).saturating_mul(n as Weight)) + // Standard Error: 52_000 + .saturating_add((76_201_000 as Weight).saturating_mul(a as Weight)) + // Standard Error: 108_000 + .saturating_add((7_271_000 as Weight).saturating_mul(w as Weight)) .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().reads((4 as Weight).saturating_mul(a as Weight))) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) @@ -250,171 +250,171 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { fn bond() -> Weight { - (76_281_000 as Weight) + (81_642_000 as Weight) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn bond_extra() -> Weight { - (62_062_000 as Weight) + (66_025_000 as Weight) .saturating_add(RocksDbWeight::get().reads(4 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } fn unbond() -> Weight { - (57_195_000 as Weight) + (60_810_000 as Weight) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_update(s: u32, ) -> Weight { - (58_043_000 as Weight) + (61_537_000 as Weight) // Standard Error: 1_000 - .saturating_add((52_000 as Weight).saturating_mul(s as Weight)) + .saturating_add((60_000 as Weight).saturating_mul(s as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn withdraw_unbonded_kill(s: u32, ) -> Weight { - (89_920_000 as Weight) - // Standard Error: 3_000 - .saturating_add((2_526_000 as Weight).saturating_mul(s as Weight)) + (95_741_000 as Weight) + // Standard Error: 1_000 + .saturating_add((2_754_000 as Weight).saturating_mul(s as Weight)) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(8 as Weight)) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn validate() -> Weight { - (20_228_000 as Weight) + (21_009_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } fn kick(k: u32, ) -> Weight { - (31_066_000 as Weight) - // Standard Error: 11_000 - .saturating_add((17_754_000 as Weight).saturating_mul(k as Weight)) + (31_832_000 as Weight) + // Standard Error: 15_000 + .saturating_add((19_418_000 as Weight).saturating_mul(k as Weight)) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(k as Weight))) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(k as Weight))) } fn nominate(n: u32, ) -> Weight { - (33_494_000 as Weight) - // Standard Error: 23_000 - .saturating_add((5_253_000 as Weight).saturating_mul(n as Weight)) + (34_304_000 as Weight) + // Standard Error: 20_000 + .saturating_add((5_643_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(4 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(n as Weight))) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } fn chill() -> Weight { - (19_396_000 as Weight) + (20_103_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } fn set_payee() -> Weight { - (13_449_000 as Weight) + (13_858_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_controller() -> Weight { - (29_184_000 as Weight) + (30_269_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn set_validator_count() -> Weight { - (2_266_000 as Weight) + (2_444_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn force_no_eras() -> Weight { - (2_462_000 as Weight) + (2_766_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn force_new_era() -> Weight { - (2_483_000 as Weight) + (2_724_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn force_new_era_always() -> Weight { - (2_495_000 as Weight) + (2_702_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_invulnerables(v: u32, ) -> Weight { - (2_712_000 as Weight) + (2_914_000 as Weight) // Standard Error: 0 - .saturating_add((9_000 as Weight).saturating_mul(v as Weight)) + .saturating_add((35_000 as Weight).saturating_mul(v as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn force_unstake(s: u32, ) -> Weight { - (60_508_000 as Weight) - // Standard Error: 1_000 - .saturating_add((2_525_000 as Weight).saturating_mul(s as Weight)) + (64_032_000 as Weight) + // Standard Error: 2_000 + .saturating_add((2_787_000 as Weight).saturating_mul(s as Weight)) .saturating_add(RocksDbWeight::get().reads(4 as Weight)) .saturating_add(RocksDbWeight::get().writes(8 as Weight)) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn cancel_deferred_slash(s: u32, ) -> Weight { - (5_886_772_000 as Weight) - // Standard Error: 393_000 - .saturating_add((34_849_000 as Weight).saturating_mul(s as Weight)) + (5_903_394_000 as Weight) + // Standard Error: 391_000 + .saturating_add((34_834_000 as Weight).saturating_mul(s as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn payout_stakers_dead_controller(n: u32, ) -> Weight { - (127_627_000 as Weight) - // Standard Error: 27_000 - .saturating_add((49_354_000 as Weight).saturating_mul(n as Weight)) + (141_724_000 as Weight) + // Standard Error: 24_000 + .saturating_add((53_018_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(11 as Weight)) .saturating_add(RocksDbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(n as Weight))) } fn payout_stakers_alive_staked(n: u32, ) -> Weight { - (156_838_000 as Weight) - // Standard Error: 24_000 - .saturating_add((62_653_000 as Weight).saturating_mul(n as Weight)) + (159_994_000 as Weight) + // Standard Error: 28_000 + .saturating_add((67_746_000 as Weight).saturating_mul(n as Weight)) .saturating_add(RocksDbWeight::get().reads(12 as Weight)) .saturating_add(RocksDbWeight::get().reads((5 as Weight).saturating_mul(n as Weight))) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) .saturating_add(RocksDbWeight::get().writes((3 as Weight).saturating_mul(n as Weight))) } fn rebond(l: u32, ) -> Weight { - (40_110_000 as Weight) + (42_177_000 as Weight) // Standard Error: 1_000 - .saturating_add((78_000 as Weight).saturating_mul(l as Weight)) + .saturating_add((82_000 as Weight).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(4 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn set_history_depth(e: u32, ) -> Weight { (0 as Weight) - // Standard Error: 70_000 - .saturating_add((32_883_000 as Weight).saturating_mul(e as Weight)) + // Standard Error: 65_000 + .saturating_add((34_151_000 as Weight).saturating_mul(e as Weight)) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) .saturating_add(RocksDbWeight::get().writes((7 as Weight).saturating_mul(e as Weight))) } fn reap_stash(s: u32, ) -> Weight { - (64_605_000 as Weight) - // Standard Error: 1_000 - .saturating_add((2_506_000 as Weight).saturating_mul(s as Weight)) + (68_377_000 as Weight) + // Standard Error: 0 + .saturating_add((2_757_000 as Weight).saturating_mul(s as Weight)) .saturating_add(RocksDbWeight::get().reads(4 as Weight)) .saturating_add(RocksDbWeight::get().writes(8 as Weight)) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(s as Weight))) } fn new_era(v: u32, n: u32, ) -> Weight { (0 as Weight) - // Standard Error: 926_000 - .saturating_add((548_212_000 as Weight).saturating_mul(v as Weight)) - // Standard Error: 46_000 - .saturating_add((78_343_000 as Weight).saturating_mul(n as Weight)) - .saturating_add(RocksDbWeight::get().reads(7 as Weight)) + // Standard Error: 908_000 + .saturating_add((588_562_000 as Weight).saturating_mul(v as Weight)) + // Standard Error: 45_000 + .saturating_add((83_485_000 as Weight).saturating_mul(n as Weight)) + .saturating_add(RocksDbWeight::get().reads(9 as Weight)) .saturating_add(RocksDbWeight::get().reads((4 as Weight).saturating_mul(v as Weight))) .saturating_add(RocksDbWeight::get().reads((3 as Weight).saturating_mul(n as Weight))) - .saturating_add(RocksDbWeight::get().writes(8 as Weight)) + .saturating_add(RocksDbWeight::get().writes(13 as Weight)) .saturating_add(RocksDbWeight::get().writes((3 as Weight).saturating_mul(v as Weight))) } fn submit_solution_better(v: u32, n: u32, a: u32, w: u32, ) -> Weight { (0 as Weight) - // Standard Error: 48_000 - .saturating_add((937_000 as Weight).saturating_mul(v as Weight)) - // Standard Error: 19_000 - .saturating_add((657_000 as Weight).saturating_mul(n as Weight)) - // Standard Error: 48_000 - .saturating_add((70_669_000 as Weight).saturating_mul(a as Weight)) - // Standard Error: 101_000 - .saturating_add((7_658_000 as Weight).saturating_mul(w as Weight)) + // Standard Error: 52_000 + .saturating_add((750_000 as Weight).saturating_mul(v as Weight)) + // Standard Error: 20_000 + .saturating_add((556_000 as Weight).saturating_mul(n as Weight)) + // Standard Error: 52_000 + .saturating_add((76_201_000 as Weight).saturating_mul(a as Weight)) + // Standard Error: 108_000 + .saturating_add((7_271_000 as Weight).saturating_mul(w as Weight)) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().reads((4 as Weight).saturating_mul(a as Weight))) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(w as Weight))) diff --git a/frame/sudo/Cargo.toml b/frame/sudo/Cargo.toml index a566cd2a9f061..ed19d2e16535a 100644 --- a/frame/sudo/Cargo.toml +++ b/frame/sudo/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-sudo" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,15 +14,15 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } [features] default = ["std"] diff --git a/frame/support/Cargo.toml b/frame/support/Cargo.toml index 9353dc6e121d1..b77907721be22 100644 --- a/frame/support/Cargo.toml +++ b/frame/support/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-support" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,29 +15,30 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] log = "0.4" serde = { version = "1.0.101", optional = true, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -frame-metadata = { version = "12.0.0", default-features = false, path = "../metadata" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-tracing = { version = "2.0.0", default-features = false, path = "../../primitives/tracing" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-arithmetic = { version = "2.0.0", default-features = false, path = "../../primitives/arithmetic" } -sp-inherents = { version = "2.0.0", default-features = false, path = "../../primitives/inherents" } -frame-support-procedural = { version = "2.0.1", default-features = false, path = "./procedural" } -paste = "0.1.6" +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +frame-metadata = { version = "13.0.0", default-features = false, path = "../metadata" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-tracing = { version = "3.0.0", default-features = false, path = "../../primitives/tracing" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-arithmetic = { version = "3.0.0", default-features = false, path = "../../primitives/arithmetic" } +sp-inherents = { version = "3.0.0", default-features = false, path = "../../primitives/inherents" } +sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } +frame-support-procedural = { version = "3.0.0", default-features = false, path = "./procedural" } +paste = "1.0" once_cell = { version = "1", default-features = false, optional = true } -sp-state-machine = { version = "0.8.0", optional = true, path = "../../primitives/state-machine" } +sp-state-machine = { version = "0.9.0", optional = true, path = "../../primitives/state-machine" } bitflags = "1.2" -impl-trait-for-tuples = "0.2.0" +impl-trait-for-tuples = "0.2.1" smallvec = "1.4.1" [dev-dependencies] pretty_assertions = "0.6.1" -frame-system = { version = "2.0.0", path = "../system" } -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } +frame-system = { version = "3.0.0", path = "../system" } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } -sp-api = { version = "2.0.0", default-features = false, path = "../../primitives/api" } +sp-api = { version = "3.0.0", default-features = false, path = "../../primitives/api" } [features] default = ["std"] @@ -52,9 +53,11 @@ std = [ "sp-arithmetic/std", "frame-metadata/std", "sp-inherents/std", + "sp-staking/std", "sp-state-machine", "frame-support-procedural/std", ] nightly = [] strict = [] runtime-benchmarks = [] +try-runtime = [] diff --git a/frame/support/procedural/Cargo.toml b/frame/support/procedural/Cargo.toml index 3d829afb0ca3f..4a00a24e3849d 100644 --- a/frame/support/procedural/Cargo.toml +++ b/frame/support/procedural/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-support-procedural" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"] proc-macro = true [dependencies] -frame-support-procedural-tools = { version = "2.0.0", path = "./tools" } +frame-support-procedural-tools = { version = "3.0.0", path = "./tools" } proc-macro2 = "1.0.6" quote = "1.0.3" Inflector = "0.11.4" diff --git a/frame/support/procedural/src/construct_runtime/mod.rs b/frame/support/procedural/src/construct_runtime/mod.rs index 4644c217cfdd9..abd68e4425d89 100644 --- a/frame/support/procedural/src/construct_runtime/mod.rs +++ b/frame/support/procedural/src/construct_runtime/mod.rs @@ -348,7 +348,7 @@ fn decl_outer_dispatch<'a>( .map(|module_declaration| { let module = &module_declaration.module; let name = &module_declaration.name; - let index = module_declaration.index.to_string(); + let index = module_declaration.index; quote!(#[codec(index = #index)] #module::#name) }); @@ -381,14 +381,14 @@ fn decl_outer_origin<'a>( ); return Err(syn::Error::new(module_declaration.name.span(), msg)); } - let index = module_declaration.index.to_string(); + let index = module_declaration.index; let tokens = quote!(#[codec(index = #index)] #module #instance #generics,); modules_tokens.extend(tokens); } } let system_name = &system_module.module; - let system_index = system_module.index.to_string(); + let system_index = system_module.index; Ok(quote!( #scrate::impl_outer_origin! { @@ -422,7 +422,7 @@ fn decl_outer_event<'a>( return Err(syn::Error::new(module_declaration.name.span(), msg)); } - let index = module_declaration.index.to_string(); + let index = module_declaration.index; let tokens = quote!(#[codec(index = #index)] #module #instance #generics,); modules_tokens.extend(tokens); } @@ -470,8 +470,11 @@ fn decl_all_modules<'a>( quote!( #types - type AllModules = ( #all_modules ); - type AllModulesWithSystem = ( #all_modules_with_system ); + /// All pallets included in the runtime as a nested tuple of types. + /// Excludes the System pallet. + pub type AllModules = ( #all_modules ); + /// All pallets included in the runtime as a nested tuple of types. + pub type AllModulesWithSystem = ( #all_modules_with_system ); ) } diff --git a/frame/support/procedural/src/pallet/expand/pallet_struct.rs b/frame/support/procedural/src/pallet/expand/pallet_struct.rs index aff7af4afb5e2..6e456695d9a45 100644 --- a/frame/support/procedural/src/pallet/expand/pallet_struct.rs +++ b/frame/support/procedural/src/pallet/expand/pallet_struct.rs @@ -22,6 +22,7 @@ use crate::pallet::Def; /// * Implement OnGenesis on Pallet /// * Implement ModuleErrorMetadata on Pallet /// * declare Module type alias for construct_runtime +/// * replace the first field type of `struct Pallet` with `PhantomData` if it is `_` pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { let frame_support = &def.frame_support; let frame_system = &def.frame_system; @@ -41,6 +42,15 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { } }; + // If the first field type is `_` then we replace with `PhantomData` + if let Some(field) = pallet_item.fields.iter_mut().next() { + if field.ty == syn::parse_quote!(_) { + field.ty = syn::parse_quote!( + #frame_support::sp_std::marker::PhantomData<(#type_use_gen)> + ); + } + } + pallet_item.attrs.push(syn::parse_quote!( #[derive( #frame_support::CloneNoBound, diff --git a/frame/support/procedural/tools/Cargo.toml b/frame/support/procedural/tools/Cargo.toml index 0c8b9249b5cac..4165cb32c3a5b 100644 --- a/frame/support/procedural/tools/Cargo.toml +++ b/frame/support/procedural/tools/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-support-procedural-tools" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -12,7 +12,7 @@ description = "Proc macro helpers for procedural macros" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -frame-support-procedural-tools-derive = { version = "2.0.0", path = "./derive" } +frame-support-procedural-tools-derive = { version = "3.0.0", path = "./derive" } proc-macro2 = "1.0.6" quote = "1.0.3" syn = { version = "1.0.58", features = ["full", "visit"] } diff --git a/frame/support/procedural/tools/derive/Cargo.toml b/frame/support/procedural/tools/derive/Cargo.toml index 0ec72f1388e03..c377680af16f4 100644 --- a/frame/support/procedural/tools/derive/Cargo.toml +++ b/frame/support/procedural/tools/derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-support-procedural-tools-derive" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index 03cda0e4d40e0..7927ccd014bd5 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -2409,7 +2409,7 @@ mod tests { use crate::weights::{DispatchInfo, DispatchClass, Pays, RuntimeDbWeight}; use crate::traits::{ CallMetadata, GetCallMetadata, GetCallName, OnInitialize, OnFinalize, OnRuntimeUpgrade, - IntegrityTest, Get, + IntegrityTest, Get, PalletInfo, }; pub trait Config: system::Config + Sized where Self::AccountId: From { } @@ -2562,13 +2562,32 @@ mod tests { } } + impl PalletInfo for TraitImpl { + fn index() -> Option { + let type_id = sp_std::any::TypeId::of::

(); + if type_id == sp_std::any::TypeId::of::() { + return Some(0) + } + + None + } + fn name() -> Option<&'static str> { + let type_id = sp_std::any::TypeId::of::

(); + if type_id == sp_std::any::TypeId::of::() { + return Some("Test") + } + + None + } + } + impl system::Config for TraitImpl { type Origin = OuterOrigin; type AccountId = u32; type Call = OuterCall; type BaseCallFilter = (); type BlockNumber = u32; - type PalletInfo = (); + type PalletInfo = Self; type DbWeight = (); } diff --git a/frame/support/src/event.rs b/frame/support/src/event.rs index b55f5d7e0b2ae..eb666b6f028ab 100644 --- a/frame/support/src/event.rs +++ b/frame/support/src/event.rs @@ -713,7 +713,7 @@ mod tests { pub enum TestEventSystemRenamed for TestRuntime2 { system_renamed, event_module, - #[codec(index = "5")] event_module2, + #[codec(index = 5)] event_module2, event_module3, } } @@ -729,7 +729,7 @@ mod tests { impl system::Config for TestRuntime { type Origin = u32; type BlockNumber = u32; - type PalletInfo = (); + type PalletInfo = crate::tests::PanicPalletInfo; type DbWeight = (); } @@ -744,14 +744,14 @@ mod tests { impl system_renamed::Config for TestRuntime2 { type Origin = u32; type BlockNumber = u32; - type PalletInfo = (); + type PalletInfo = crate::tests::PanicPalletInfo; type DbWeight = (); } impl system::Config for TestRuntime2 { type Origin = u32; type BlockNumber = u32; - type PalletInfo = (); + type PalletInfo = crate::tests::PanicPalletInfo; type DbWeight = (); } diff --git a/frame/support/src/inherent.rs b/frame/support/src/inherent.rs index 430075d603f2a..3c201dff29c22 100644 --- a/frame/support/src/inherent.rs +++ b/frame/support/src/inherent.rs @@ -218,6 +218,10 @@ mod tests { fn create_inherent(_: &InherentData) -> Option { Some(CallTest2::Something) } + + fn is_inherent_required(_: &InherentData) -> Result, Self::Error> { + Ok(Some(().into())) + } } type Block = testing::Block; @@ -260,14 +264,30 @@ mod tests { fn check_inherents_works() { let block = Block::new( Header::new_from_number(1), - vec![Extrinsic { function: Call::Test(CallTest::Something) }], + vec![ + Extrinsic { function: Call::Test2(CallTest2::Something) }, + Extrinsic { function: Call::Test(CallTest::Something) }, + ], ); assert!(InherentData::new().check_extrinsics(&block).ok()); let block = Block::new( Header::new_from_number(1), - vec![Extrinsic { function: Call::Test(CallTest::SomethingElse) }], + vec![ + Extrinsic { function: Call::Test2(CallTest2::Something) }, + Extrinsic { function: Call::Test(CallTest::SomethingElse) }, + ], + ); + + assert!(InherentData::new().check_extrinsics(&block).fatal_error()); + } + + #[test] + fn required_inherents_enforced() { + let block = Block::new( + Header::new_from_number(1), + vec![Extrinsic { function: Call::Test(CallTest::Something) }], ); assert!(InherentData::new().check_extrinsics(&block).fatal_error()); diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index 08852a7f3c1fe..e7af1ccab68fd 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -571,7 +571,7 @@ macro_rules! assert_ok { pub use serde::{Serialize, Deserialize}; #[cfg(test)] -mod tests { +pub mod tests { use super::*; use codec::{Codec, EncodeLike}; use frame_metadata::{ @@ -581,6 +581,18 @@ mod tests { use sp_std::{marker::PhantomData, result}; use sp_io::TestExternalities; + /// A PalletInfo implementation which just panics. + pub struct PanicPalletInfo; + + impl crate::traits::PalletInfo for PanicPalletInfo { + fn index() -> Option { + unimplemented!("PanicPalletInfo mustn't be triggered by tests"); + } + fn name() -> Option<&'static str> { + unimplemented!("PanicPalletInfo mustn't be triggered by tests"); + } + } + pub trait Config: 'static { type BlockNumber: Codec + EncodeLike + Default; type Origin; @@ -625,7 +637,7 @@ mod tests { impl Config for Test { type BlockNumber = u32; type Origin = u32; - type PalletInfo = (); + type PalletInfo = PanicPalletInfo; type DbWeight = (); } @@ -1129,7 +1141,7 @@ pub mod pallet_prelude { /// Item must be defined as followed: /// ```ignore /// #[pallet::pallet] -/// pub struct Pallet(PhantomData); +/// pub struct Pallet(_); /// ``` /// I.e. a regular struct definition named `Pallet`, with generic T and no where clause. /// @@ -1138,7 +1150,7 @@ pub mod pallet_prelude { /// ```ignore /// #[pallet::pallet] /// #[pallet::generate_store(pub(super) trait Store)] -/// pub struct Pallet(PhantomData); +/// pub struct Pallet(_); /// ``` /// More precisely the store trait contains an associated type for each storage. It is implemented /// for `Pallet` allowing to access the storage from pallet struct. @@ -1157,6 +1169,7 @@ pub mod pallet_prelude { /// frame_support::RuntimeDebugNoBound, /// )] /// ``` +/// and replace the type `_` by `PhantomData`. /// /// It implements on pallet: /// * [`traits::GetPalletVersion`] @@ -1294,7 +1307,7 @@ pub mod pallet_prelude { /// ```ignore /// #[pallet::event] /// #[pallet::metadata($SomeType = "$Metadata", $SomeOtherType = "$Metadata", ..)] // Optional -/// #[pallet::generate_deposit($visbility fn deposit_event)] // Optional +/// #[pallet::generate_deposit($visibility fn deposit_event)] // Optional /// pub enum Event<$some_generic> $optional_where_clause { /// /// Some doc /// $SomeName($SomeType, $YetanotherType, ...), @@ -1325,7 +1338,7 @@ pub mod pallet_prelude { /// ``` /// will write in event variant metadata `"SpecialU32"` and `"T::AccountId"`. /// -/// The attribute `#[pallet::generate_deposit($visbility fn deposit_event)]` generate a helper +/// The attribute `#[pallet::generate_deposit($visibility fn deposit_event)]` generate a helper /// function on `Pallet` to deposit event. /// /// NOTE: For instantiable pallet, event must be generic over T and I. @@ -1590,7 +1603,7 @@ pub mod pallet_prelude { /// // Define the pallet struct placeholder, various pallet function are implemented on it. /// #[pallet::pallet] /// #[pallet::generate_store(pub(super) trait Store)] -/// pub struct Pallet(PhantomData); +/// pub struct Pallet(_); /// /// // Implement the pallet hooks. /// #[pallet::hooks] @@ -1874,11 +1887,15 @@ pub mod pallet_prelude { /// /// ## Upgrade guidelines: /// -/// 1. export metadata of the pallet for later checks -/// 2. generate the template upgrade for the pallet provided by decl_storage with environment -/// variable `PRINT_PALLET_UPGRADE`: `PRINT_PALLET_UPGRADE=1 cargo check -p my_pallet` -/// This template can be used as information it contains all information for storages, genesis -/// config and genesis build. +/// 1. Export the metadata of the pallet for later checks +/// - run your node with the pallet active +/// - query the metadata using the `state_getMetadata` RPC and curl, or use +/// `subsee -p > meta.json` +/// 2. generate the template upgrade for the pallet provided by decl_storage +/// with environment variable `PRINT_PALLET_UPGRADE`: +/// `PRINT_PALLET_UPGRADE=1 cargo check -p my_pallet` This template can be +/// used as information it contains all information for storages, genesis +/// config and genesis build. /// 3. reorganize pallet to have trait `Config`, `decl_*` macros, `ValidateUnsigned`, /// `ProvideInherent`, `Origin` all together in one file. Suggested order: /// * Config, @@ -1904,7 +1921,7 @@ pub mod pallet_prelude { /// #[pallet::generate_store($visibility_of_trait_store trait Store)] /// // NOTE: if the visibility of trait store is private but you want to make it available /// // in super, then use `pub(super)` or `pub(crate)` to make it available in crate. -/// pub struct Pallet(PhantomData); +/// pub struct Pallet(_); /// // pub struct Pallet(PhantomData); // for instantiable pallet /// } /// ``` @@ -1925,7 +1942,7 @@ pub mod pallet_prelude { /// impl Pallet { /// } /// ``` -/// and write inside all the call in decl_module with a few changes in the signature: +/// and write inside all the calls in decl_module with a few changes in the signature: /// - origin must now be written completely, e.g. `origin: OriginFor` /// - result type must be `DispatchResultWithPostInfo`, you need to write it and also you might /// need to put `Ok(().into())` at the end or the function. @@ -1989,10 +2006,10 @@ pub mod pallet_prelude { /// implementation. /// /// 10. **migrate origin**: move the origin to the pallet module under `#[pallet::origin]` -/// 11. **migrate validate_unsigned**: move the ValidateUnsigned implementation to the pallet +/// 11. **migrate validate_unsigned**: move the `ValidateUnsigned` implementation to the pallet /// module under `#[pallet::validate_unsigned]` -/// 12. **migrate provide_inherent**: move the ValidateUnsigned implementation to the pallet -/// module under `#[pallet::provide_inherent]` +/// 12. **migrate provide_inherent**: move the `ProvideInherent` implementation to the pallet +/// module under `#[pallet::inherent]` /// 13. rename the usage of `Module` to `Pallet` inside the crate. /// 14. migration is done, now double check migration with the checking migration guidelines. /// @@ -2017,9 +2034,9 @@ pub mod pallet_prelude { /// * `add_extra_genesis` fields are converted to `GenesisConfig` field with their correct /// default if specified /// * `add_extra_genesis` build is written into `GenesisBuild::build` -/// * storage items defined with [`pallet`] use the name of the pallet provided by [`PalletInfo::name`] -/// as `pallet_prefix` (in `decl_storage`, storage items used the `pallet_prefix` given as input of -/// `decl_storage` with the syntax `as Example`). +/// * storage items defined with [`pallet`] use the name of the pallet provided by +/// [`traits::PalletInfo::name`] as `pallet_prefix` (in `decl_storage`, storage items used the +/// `pallet_prefix` given as input of `decl_storage` with the syntax `as Example`). /// Thus a runtime using the pallet must be careful with this change. /// To handle this change: /// * either ensure that the name of the pallet given to `construct_runtime!` is the same diff --git a/frame/support/src/metadata.rs b/frame/support/src/metadata.rs index a60481933701b..2edaba1cb47e9 100644 --- a/frame/support/src/metadata.rs +++ b/frame/support/src/metadata.rs @@ -43,10 +43,14 @@ pub use frame_metadata::{ ///# } ///# use module0 as module1; ///# use module0 as module2; +///# impl frame_support::traits::PalletInfo for Runtime { +///# fn index() -> Option { unimplemented!() } +///# fn name() -> Option<&'static str> { unimplemented!() } +///# } ///# impl module0::Config for Runtime { ///# type Origin = u32; ///# type BlockNumber = u32; -///# type PalletInfo = (); +///# type PalletInfo = Self; ///# type DbWeight = (); ///# } ///# @@ -414,6 +418,37 @@ mod tests { #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct TestRuntime; + impl crate::traits::PalletInfo for TestRuntime { + fn index() -> Option { + let type_id = sp_std::any::TypeId::of::

(); + if type_id == sp_std::any::TypeId::of::>() { + return Some(0) + } + if type_id == sp_std::any::TypeId::of::() { + return Some(1) + } + if type_id == sp_std::any::TypeId::of::() { + return Some(2) + } + + None + } + fn name() -> Option<&'static str> { + let type_id = sp_std::any::TypeId::of::

(); + if type_id == sp_std::any::TypeId::of::>() { + return Some("System") + } + if type_id == sp_std::any::TypeId::of::() { + return Some("EventModule") + } + if type_id == sp_std::any::TypeId::of::() { + return Some("EventModule2") + } + + None + } + } + impl_outer_event! { pub enum TestEvent for TestRuntime { system, @@ -451,7 +486,7 @@ mod tests { type AccountId = u32; type BlockNumber = u32; type SomeValue = SystemValue; - type PalletInfo = (); + type PalletInfo = Self; type DbWeight = (); type Call = Call; } diff --git a/frame/support/src/origin.rs b/frame/support/src/origin.rs index c17c617b86b78..19b24fb84bb1a 100644 --- a/frame/support/src/origin.rs +++ b/frame/support/src/origin.rs @@ -478,9 +478,9 @@ mod tests { ); impl_outer_origin!( - pub enum OriginIndices for TestRuntime where system = frame_system, system_index = "11" { + pub enum OriginIndices for TestRuntime where system = frame_system, system_index = 11 { origin_with_generic, - #[codec(index = "10")] origin_without_generic, + #[codec(index = 10)] origin_without_generic, } ); diff --git a/frame/support/src/storage/child.rs b/frame/support/src/storage/child.rs index c1885fc074307..66cc7d74fe7db 100644 --- a/frame/support/src/storage/child.rs +++ b/frame/support/src/storage/child.rs @@ -24,14 +24,7 @@ use crate::sp_std::prelude::*; use codec::{Codec, Encode, Decode}; pub use sp_core::storage::{ChildInfo, ChildType}; - -/// The outcome of calling [`kill_storage`]. -pub enum KillOutcome { - /// No key remains in the child trie. - AllRemoved, - /// At least one key still resides in the child trie due to the supplied limit. - SomeRemaining, -} +pub use crate::sp_io::KillChildStorageResult; /// Return the value of the item in storage under `key`, or `None` if there is no explicit entry. pub fn get( @@ -177,16 +170,12 @@ pub fn exists( pub fn kill_storage( child_info: &ChildInfo, limit: Option, -) -> KillOutcome { - let all_removed = match child_info.child_type() { +) -> KillChildStorageResult { + match child_info.child_type() { ChildType::ParentKeyId => sp_io::default_child_storage::storage_kill( child_info.storage_key(), limit ), - }; - match all_removed { - true => KillOutcome::AllRemoved, - false => KillOutcome::SomeRemaining, } } diff --git a/frame/support/src/storage/generator/double_map.rs b/frame/support/src/storage/generator/double_map.rs index e5ee7ec45b13e..7e1a2456e4536 100644 --- a/frame/support/src/storage/generator/double_map.rs +++ b/frame/support/src/storage/generator/double_map.rs @@ -383,7 +383,7 @@ impl< iterator } - fn translate Option>(f: F) { + fn translate Option>(mut f: F) { let prefix = G::prefix_hash(); let mut previous_key = prefix.clone(); while let Some(next) = sp_io::storage::next_key(&previous_key) diff --git a/frame/support/src/storage/generator/map.rs b/frame/support/src/storage/generator/map.rs index 198fad08dc731..7f6eb2a518f57 100644 --- a/frame/support/src/storage/generator/map.rs +++ b/frame/support/src/storage/generator/map.rs @@ -162,7 +162,7 @@ impl< iterator } - fn translate Option>(f: F) { + fn translate Option>(mut f: F) { let prefix = G::prefix_hash(); let mut previous_key = prefix.clone(); while let Some(next) = sp_io::storage::next_key(&previous_key) diff --git a/frame/support/src/storage/generator/mod.rs b/frame/support/src/storage/generator/mod.rs index a9e5665c544d2..fc2a21ff72517 100644 --- a/frame/support/src/storage/generator/mod.rs +++ b/frame/support/src/storage/generator/mod.rs @@ -52,7 +52,7 @@ mod tests { impl Config for Runtime { type Origin = u32; type BlockNumber = u32; - type PalletInfo = (); + type PalletInfo = crate::tests::PanicPalletInfo; type DbWeight = (); } diff --git a/frame/support/src/storage/mod.rs b/frame/support/src/storage/mod.rs index dbb1062c24639..93cf7c6639064 100644 --- a/frame/support/src/storage/mod.rs +++ b/frame/support/src/storage/mod.rs @@ -315,7 +315,7 @@ pub trait IterableStorageMap: StorageMap { /// By returning `None` from `f` for an element, you'll remove it from the map. /// /// NOTE: If a value fail to decode because storage is corrupted then it is skipped. - fn translate Option>(f: F); + fn translate Option>(f: F); } /// A strongly-typed double map in storage whose secondary keys and values can be iterated over. @@ -352,7 +352,7 @@ pub trait IterableStorageDoubleMap< /// By returning `None` from `f` for an element, you'll remove it from the map. /// /// NOTE: If a value fail to decode because storage is corrupted then it is skipped. - fn translate Option>(f: F); + fn translate Option>(f: F); } /// An implementation of a map with a two keys. @@ -614,7 +614,7 @@ pub trait StoragePrefixedMap { /// # Usage /// /// This would typically be called inside the module implementation of on_runtime_upgrade. - fn translate_values Option>(f: F) { + fn translate_values Option>(mut f: F) { let prefix = Self::final_prefix(); let mut previous_key = prefix.clone().to_vec(); while let Some(next) = sp_io::storage::next_key(&previous_key) diff --git a/frame/support/src/storage/types/double_map.rs b/frame/support/src/storage/types/double_map.rs index 93f40b660f7b8..f0b5f66eff058 100644 --- a/frame/support/src/storage/types/double_map.rs +++ b/frame/support/src/storage/types/double_map.rs @@ -326,7 +326,7 @@ where /// # Usage /// /// This would typically be called inside the module implementation of on_runtime_upgrade. - pub fn translate_values Option>(f: F) { + pub fn translate_values Option>(f: F) { >::translate_values(f) } } @@ -379,7 +379,7 @@ where /// By returning `None` from `f` for an element, you'll remove it from the map. /// /// NOTE: If a value fail to decode because storage is corrupted then it is skipped. - pub fn translate Option>(f: F) { + pub fn translate Option>(f: F) { >::translate(f) } } diff --git a/frame/support/src/storage/types/map.rs b/frame/support/src/storage/types/map.rs index 5c236e7f6b598..4af28a77cf2b6 100644 --- a/frame/support/src/storage/types/map.rs +++ b/frame/support/src/storage/types/map.rs @@ -249,7 +249,7 @@ where /// # Usage /// /// This would typically be called inside the module implementation of on_runtime_upgrade. - pub fn translate_values Option>(f: F) { + pub fn translate_values Option>(f: F) { >::translate_values(f) } } @@ -283,7 +283,7 @@ where /// By returning `None` from `f` for an element, you'll remove it from the map. /// /// NOTE: If a value fail to decode because storage is corrupted then it is skipped. - pub fn translate Option>(f: F) { + pub fn translate Option>(f: F) { >::translate(f) } } diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index 2888abc306b3d..4114afa973cb6 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -23,13 +23,15 @@ use sp_std::{prelude::*, result, marker::PhantomData, ops::Div, fmt::Debug}; use codec::{FullCodec, Codec, Encode, Decode, EncodeLike}; use sp_core::u32_trait::Value as U32; use sp_runtime::{ - RuntimeDebug, ConsensusEngineId, DispatchResult, DispatchError, + RuntimeAppPublic, RuntimeDebug, BoundToRuntimeAppPublic, + ConsensusEngineId, DispatchResult, DispatchError, traits::{ - MaybeSerializeDeserialize, AtLeast32Bit, Saturating, TrailingZeroInput, Bounded, Zero, - BadOrigin, AtLeast32BitUnsigned, UniqueSaturatedFrom, UniqueSaturatedInto, - SaturatedConversion, StoredMapError, + MaybeSerializeDeserialize, AtLeast32Bit, Saturating, TrailingZeroInput, Bounded, Zero, + BadOrigin, AtLeast32BitUnsigned, Convert, UniqueSaturatedFrom, UniqueSaturatedInto, + SaturatedConversion, StoredMapError, }, }; +use sp_staking::SessionIndex; use crate::dispatch::Parameter; use crate::storage::StorageMap; use crate::weights::Weight; @@ -40,6 +42,67 @@ use impl_trait_for_tuples::impl_for_tuples; #[doc(hidden)] pub use sp_std::{mem::{swap, take}, cell::RefCell, vec::Vec, boxed::Box}; +/// A trait for online node inspection in a session. +/// +/// Something that can give information about the current validator set. +pub trait ValidatorSet { + /// Type for representing validator id in a session. + type ValidatorId: Parameter; + /// A type for converting `AccountId` to `ValidatorId`. + type ValidatorIdOf: Convert>; + + /// Returns current session index. + fn session_index() -> SessionIndex; + + /// Returns the active set of validators. + fn validators() -> Vec; +} + +/// [`ValidatorSet`] combined with an identification. +pub trait ValidatorSetWithIdentification: ValidatorSet { + /// Full identification of `ValidatorId`. + type Identification: Parameter; + /// A type for converting `ValidatorId` to `Identification`. + type IdentificationOf: Convert>; +} + +/// A session handler for specific key type. +pub trait OneSessionHandler: BoundToRuntimeAppPublic { + /// The key type expected. + type Key: Decode + Default + RuntimeAppPublic; + + /// The given validator set will be used for the genesis session. + /// It is guaranteed that the given validator set will also be used + /// for the second session, therefore the first call to `on_new_session` + /// should provide the same validator set. + fn on_genesis_session<'a, I: 'a>(validators: I) + where I: Iterator, ValidatorId: 'a; + + /// Session set has changed; act appropriately. Note that this can be called + /// before initialization of your module. + /// + /// `changed` is true when at least one of the session keys + /// or the underlying economic identities/distribution behind one the + /// session keys has changed, false otherwise. + /// + /// The `validators` are the validators of the incoming session, and `queued_validators` + /// will follow. + fn on_new_session<'a, I: 'a>( + changed: bool, + validators: I, + queued_validators: I, + ) where I: Iterator, ValidatorId: 'a; + + /// A notification for end of the session. + /// + /// Note it is triggered before any `SessionManager::end_session` handlers, + /// so we can still affect the validator set. + fn on_before_session_ending() {} + + /// A validator got disabled. Act accordingly until a new session begins. + fn on_disabled(_validator_index: usize); +} + /// Simple trait for providing a filter over a reference to some type. pub trait Filter { /// Determine if a given value should be allowed through the filter (returns `true`) or not. @@ -427,10 +490,16 @@ impl< } } -/// Something that can estimate at which block the next session rotation will happen. This should -/// be the same logical unit that dictates `ShouldEndSession` to the session module. No Assumptions -/// are made about the scheduling of the sessions. +/// Something that can estimate at which block the next session rotation will happen. +/// +/// This should be the same logical unit that dictates `ShouldEndSession` to the session module. No +/// Assumptions are made about the scheduling of the sessions. pub trait EstimateNextSessionRotation { + /// Return the average length of a session. + /// + /// This may or may not be accurate. + fn average_session_length() -> BlockNumber; + /// Return the block number at which the next session rotation is estimated to happen. /// /// None should be returned if the estimation fails to come to an answer @@ -440,7 +509,11 @@ pub trait EstimateNextSessionRotation { fn weight(now: BlockNumber) -> Weight; } -impl EstimateNextSessionRotation for () { +impl EstimateNextSessionRotation for () { + fn average_session_length() -> BlockNumber { + Default::default() + } + fn estimate_next_session_rotation(_: BlockNumber) -> Option { Default::default() } @@ -450,9 +523,15 @@ impl EstimateNextSessionRotation for () { } } -/// Something that can estimate at which block the next `new_session` will be triggered. This must -/// always be implemented by the session module. +/// Something that can estimate at which block the next `new_session` will be triggered. +/// +/// This must always be implemented by the session module. pub trait EstimateNextNewSession { + /// Return the average length of a session. + /// + /// This may or may not be accurate. + fn average_session_length() -> BlockNumber; + /// Return the block number at which the next new session is estimated to happen. fn estimate_next_new_session(now: BlockNumber) -> Option; @@ -460,7 +539,11 @@ pub trait EstimateNextNewSession { fn weight(now: BlockNumber) -> Weight; } -impl EstimateNextNewSession for () { +impl EstimateNextNewSession for () { + fn average_session_length() -> BlockNumber { + Default::default() + } + fn estimate_next_new_session(_: BlockNumber) -> Option { Default::default() } @@ -1396,11 +1479,6 @@ pub trait PalletInfo { fn name() -> Option<&'static str>; } -impl PalletInfo for () { - fn index() -> Option { Some(0) } - fn name() -> Option<&'static str> { Some("test") } -} - /// The function and pallet name of the Call. #[derive(Clone, Eq, PartialEq, Default, RuntimeDebug)] pub struct CallMetadata { @@ -1489,7 +1567,25 @@ pub trait OnRuntimeUpgrade { /// block local data are not accessible. /// /// Return the non-negotiable weight consumed for runtime upgrade. - fn on_runtime_upgrade() -> crate::weights::Weight { 0 } + fn on_runtime_upgrade() -> crate::weights::Weight { + 0 + } + + /// Execute some pre-checks prior to a runtime upgrade. + /// + /// This hook is never meant to be executed on-chain but is meant to be used by testing tools. + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result<(), &'static str> { + Ok(()) + } + + /// Execute some post-checks after a runtime upgrade. + /// + /// This hook is never meant to be executed on-chain but is meant to be used by testing tools. + #[cfg(feature = "try-runtime")] + fn post_upgrade() -> Result<(), &'static str> { + Ok(()) + } } #[impl_for_tuples(30)] @@ -1499,6 +1595,20 @@ impl OnRuntimeUpgrade for Tuple { for_tuples!( #( weight = weight.saturating_add(Tuple::on_runtime_upgrade()); )* ); weight } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result<(), &'static str> { + let mut result = Ok(()); + for_tuples!( #( result = result.and(Tuple::pre_upgrade()); )* ); + result + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade() -> Result<(), &'static str> { + let mut result = Ok(()); + for_tuples!( #( result = result.and(Tuple::post_upgrade()); )* ); + result + } } /// Off-chain computation trait. diff --git a/frame/support/src/weights.rs b/frame/support/src/weights.rs index 32dc9e1f2529f..abd54994bc9e8 100644 --- a/frame/support/src/weights.rs +++ b/frame/support/src/weights.rs @@ -841,7 +841,7 @@ mod tests { type BlockNumber = u32; type Balance = u32; type DbWeight = DbWeight; - type PalletInfo = (); + type PalletInfo = crate::tests::PanicPalletInfo; } decl_module! { diff --git a/frame/support/test/Cargo.toml b/frame/support/test/Cargo.toml index ef66bd1902155..67cf668f7f4c9 100644 --- a/frame/support/test/Cargo.toml +++ b/frame/support/test/Cargo.toml @@ -13,19 +13,19 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", default-features = false, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-io = { version = "2.0.0", path = "../../../primitives/io", default-features = false } -sp-state-machine = { version = "0.8.0", optional = true, path = "../../../primitives/state-machine" } -frame-support = { version = "2.0.0", default-features = false, path = "../" } -sp-inherents = { version = "2.0.0", default-features = false, path = "../../../primitives/inherents" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } -sp-core = { version = "2.0.0", default-features = false, path = "../../../primitives/core" } -sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-io = { version = "3.0.0", path = "../../../primitives/io", default-features = false } +sp-state-machine = { version = "0.9.0", optional = true, path = "../../../primitives/state-machine" } +frame-support = { version = "3.0.0", default-features = false, path = "../" } +sp-inherents = { version = "3.0.0", default-features = false, path = "../../../primitives/inherents" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../primitives/runtime" } +sp-core = { version = "3.0.0", default-features = false, path = "../../../primitives/core" } +sp-std = { version = "3.0.0", default-features = false, path = "../../../primitives/std" } trybuild = "1.0.38" pretty_assertions = "0.6.1" rustversion = "1.0.0" -frame-metadata = { version = "12.0.0", default-features = false, path = "../../metadata" } -frame-system = { version = "2.0.0", default-features = false, path = "../../system" } +frame-metadata = { version = "13.0.0", default-features = false, path = "../../metadata" } +frame-system = { version = "3.0.0", default-features = false, path = "../../system" } [features] default = ["std"] diff --git a/frame/support/test/src/lib.rs b/frame/support/test/src/lib.rs index d837056fe6ab6..4b1510bf81f4d 100644 --- a/frame/support/test/src/lib.rs +++ b/frame/support/test/src/lib.rs @@ -41,3 +41,15 @@ frame_support::decl_module! { /// Some test module pub struct Module for enum Call where origin: T::Origin, system=self {} } + +/// A PalletInfo implementation which just panics. +pub struct PanicPalletInfo; + +impl frame_support::traits::PalletInfo for PanicPalletInfo { + fn index() -> Option { + unimplemented!("PanicPalletInfo mustn't be triggered by tests"); + } + fn name() -> Option<&'static str> { + unimplemented!("PanicPalletInfo mustn't be triggered by tests"); + } +} diff --git a/frame/support/test/tests/decl_storage.rs b/frame/support/test/tests/decl_storage.rs index 99697393785fe..a2690b1379db5 100644 --- a/frame/support/test/tests/decl_storage.rs +++ b/frame/support/test/tests/decl_storage.rs @@ -84,7 +84,7 @@ mod tests { impl frame_support_test::Config for TraitImpl { type Origin = u32; type BlockNumber = u32; - type PalletInfo = (); + type PalletInfo = frame_support_test::PanicPalletInfo; type DbWeight = (); } @@ -441,7 +441,7 @@ mod test2 { impl frame_support_test::Config for TraitImpl { type Origin = u32; type BlockNumber = u32; - type PalletInfo = (); + type PalletInfo = frame_support_test::PanicPalletInfo; type DbWeight = (); } @@ -469,7 +469,7 @@ mod test3 { impl frame_support_test::Config for TraitImpl { type Origin = u32; type BlockNumber = u32; - type PalletInfo = (); + type PalletInfo = frame_support_test::PanicPalletInfo; type DbWeight = (); } @@ -514,7 +514,7 @@ mod test_append_and_len { impl frame_support_test::Config for Test { type Origin = u32; type BlockNumber = u32; - type PalletInfo = (); + type PalletInfo = frame_support_test::PanicPalletInfo; type DbWeight = (); } diff --git a/frame/support/test/tests/genesisconfig.rs b/frame/support/test/tests/genesisconfig.rs index dd98fca8c9538..a30b021d13e51 100644 --- a/frame/support/test/tests/genesisconfig.rs +++ b/frame/support/test/tests/genesisconfig.rs @@ -32,7 +32,7 @@ struct Test; impl frame_support_test::Config for Test { type BlockNumber = u32; type Origin = (); - type PalletInfo = (); + type PalletInfo = frame_support_test::PanicPalletInfo; type DbWeight = (); } diff --git a/frame/support/test/tests/instance.rs b/frame/support/test/tests/instance.rs index dc6c41564a753..f7d79b7d4bf6e 100644 --- a/frame/support/test/tests/instance.rs +++ b/frame/support/test/tests/instance.rs @@ -253,7 +253,7 @@ impl system::Config for Runtime { type BlockNumber = BlockNumber; type AccountId = AccountId; type Event = Event; - type PalletInfo = (); + type PalletInfo = PalletInfo; type Call = Call; type DbWeight = (); } diff --git a/frame/support/test/tests/issue2219.rs b/frame/support/test/tests/issue2219.rs index adabb2d597928..4eacca9daca01 100644 --- a/frame/support/test/tests/issue2219.rs +++ b/frame/support/test/tests/issue2219.rs @@ -164,7 +164,7 @@ impl system::Config for Runtime { type BlockNumber = BlockNumber; type AccountId = AccountId; type Event = Event; - type PalletInfo = (); + type PalletInfo = PalletInfo; type Call = Call; type DbWeight = (); } diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index 431377a70ee3e..8e0bacb9aa4af 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -100,7 +100,7 @@ pub mod pallet { #[pallet::pallet] #[pallet::generate_store(pub(crate) trait Store)] - pub struct Pallet(PhantomData); + pub struct Pallet(_); #[pallet::hooks] impl Hooks> for Pallet @@ -290,7 +290,7 @@ pub mod pallet2 { #[pallet::pallet] #[pallet::generate_store(pub(crate) trait Store)] - pub struct Pallet(PhantomData); + pub struct Pallet(_); #[pallet::hooks] impl Hooks> for Pallet diff --git a/frame/support/test/tests/pallet_compatibility.rs b/frame/support/test/tests/pallet_compatibility.rs index 66d013441362a..5b9001e0475fe 100644 --- a/frame/support/test/tests/pallet_compatibility.rs +++ b/frame/support/test/tests/pallet_compatibility.rs @@ -106,7 +106,7 @@ pub mod pallet { } #[pallet::pallet] - pub struct Pallet(PhantomData); + pub struct Pallet(_); #[pallet::hooks] impl Hooks for Pallet { diff --git a/frame/support/test/tests/pallet_ui/hooks_invalid_item.rs b/frame/support/test/tests/pallet_ui/hooks_invalid_item.rs index fae12f133b6a0..7c66b3e6cecc1 100644 --- a/frame/support/test/tests/pallet_ui/hooks_invalid_item.rs +++ b/frame/support/test/tests/pallet_ui/hooks_invalid_item.rs @@ -1,12 +1,12 @@ #[frame_support::pallet] mod pallet { - use frame_support::pallet_prelude::{Hooks, PhantomData}; + use frame_support::pallet_prelude::Hooks; #[pallet::config] pub trait Config: frame_system::Config {} #[pallet::pallet] - pub struct Pallet(PhantomData); + pub struct Pallet(_); #[pallet::hooks] impl Hooks for Pallet {} diff --git a/frame/support/test/tests/pallet_ui/type_value_error_in_block.rs b/frame/support/test/tests/pallet_ui/type_value_error_in_block.rs index 1a1c451ac39fc..a13e1c7c5c2d2 100644 --- a/frame/support/test/tests/pallet_ui/type_value_error_in_block.rs +++ b/frame/support/test/tests/pallet_ui/type_value_error_in_block.rs @@ -1,13 +1,13 @@ #[frame_support::pallet] mod pallet { - use frame_support::pallet_prelude::{Hooks, PhantomData}; + use frame_support::pallet_prelude::Hooks; use frame_system::pallet_prelude::BlockNumberFor; #[pallet::config] pub trait Config: frame_system::Config {} #[pallet::pallet] - pub struct Pallet(PhantomData); + pub struct Pallet(_); #[pallet::hooks] impl Hooks> for Pallet {} diff --git a/frame/support/test/tests/pallet_ui/type_value_forgotten_where_clause.rs b/frame/support/test/tests/pallet_ui/type_value_forgotten_where_clause.rs index 9c0662e3f77cb..b04d8b894676d 100644 --- a/frame/support/test/tests/pallet_ui/type_value_forgotten_where_clause.rs +++ b/frame/support/test/tests/pallet_ui/type_value_forgotten_where_clause.rs @@ -1,6 +1,6 @@ #[frame_support::pallet] mod pallet { - use frame_support::pallet_prelude::{Hooks, PhantomData}; + use frame_support::pallet_prelude::Hooks; use frame_system::pallet_prelude::BlockNumberFor; #[pallet::config] @@ -9,7 +9,7 @@ mod pallet { {} #[pallet::pallet] - pub struct Pallet(PhantomData); + pub struct Pallet(_); #[pallet::hooks] impl Hooks> for Pallet diff --git a/frame/support/test/tests/pallet_ui/type_value_invalid_item.rs b/frame/support/test/tests/pallet_ui/type_value_invalid_item.rs index 476a4a8e1e783..1b6c975b09ed1 100644 --- a/frame/support/test/tests/pallet_ui/type_value_invalid_item.rs +++ b/frame/support/test/tests/pallet_ui/type_value_invalid_item.rs @@ -7,7 +7,7 @@ mod pallet { pub trait Config: frame_system::Config {} #[pallet::pallet] - pub struct Pallet(PhantomData); + pub struct Pallet(_); #[pallet::hooks] impl Hooks> for Pallet {} diff --git a/frame/support/test/tests/pallet_ui/type_value_no_return.rs b/frame/support/test/tests/pallet_ui/type_value_no_return.rs index eb13436cac7cc..82eb3b17d0393 100644 --- a/frame/support/test/tests/pallet_ui/type_value_no_return.rs +++ b/frame/support/test/tests/pallet_ui/type_value_no_return.rs @@ -7,7 +7,7 @@ mod pallet { pub trait Config: frame_system::Config {} #[pallet::pallet] - pub struct Pallet(PhantomData); + pub struct Pallet(_); #[pallet::hooks] impl Hooks> for Pallet {} diff --git a/frame/support/test/tests/pallet_version.rs b/frame/support/test/tests/pallet_version.rs index a86a876b48a5d..4cc93d395db2a 100644 --- a/frame/support/test/tests/pallet_version.rs +++ b/frame/support/test/tests/pallet_version.rs @@ -86,7 +86,7 @@ mod pallet3 { } #[pallet::pallet] - pub struct Pallet(PhantomData); + pub struct Pallet(_); #[pallet::hooks] impl Hooks> for Pallet { diff --git a/frame/support/test/tests/pallet_with_name_trait_is_valid.rs b/frame/support/test/tests/pallet_with_name_trait_is_valid.rs index 07be02c838e55..246bbe8fd52d1 100644 --- a/frame/support/test/tests/pallet_with_name_trait_is_valid.rs +++ b/frame/support/test/tests/pallet_with_name_trait_is_valid.rs @@ -135,7 +135,7 @@ mod tests { type BlockWeights = (); type BlockLength = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); diff --git a/frame/support/test/tests/storage_transaction.rs b/frame/support/test/tests/storage_transaction.rs index ee6ce5869e176..b518c60e957c6 100644 --- a/frame/support/test/tests/storage_transaction.rs +++ b/frame/support/test/tests/storage_transaction.rs @@ -53,7 +53,7 @@ struct Runtime; impl frame_support_test::Config for Runtime { type Origin = u32; type BlockNumber = u32; - type PalletInfo = (); + type PalletInfo = frame_support_test::PanicPalletInfo; type DbWeight = (); } diff --git a/frame/system/Cargo.toml b/frame/system/Cargo.toml index 49f3056aff2ff..c4530e9dfd09f 100644 --- a/frame/system/Cargo.toml +++ b/frame/system/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-system" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,18 +14,18 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", path = "../../primitives/io", default-features = false } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-version = { version = "2.0.0", default-features = false, path = "../../primitives/version" } -frame-support = { version = "2.0.1", default-features = false, path = "../support" } -impl-trait-for-tuples = "0.2.0" +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", path = "../../primitives/io", default-features = false } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-version = { version = "3.0.0", default-features = false, path = "../../primitives/version" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +impl-trait-for-tuples = "0.2.1" [dev-dependencies] criterion = "0.3.3" -sp-externalities = { version = "0.8.0", path = "../../primitives/externalities" } +sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } [features] diff --git a/frame/system/benches/bench.rs b/frame/system/benches/bench.rs index 5bebeaf932b90..6ed3d456826c2 100644 --- a/frame/system/benches/bench.rs +++ b/frame/system/benches/bench.rs @@ -17,7 +17,7 @@ use criterion::{Criterion, criterion_group, criterion_main, black_box}; use frame_system as system; -use frame_support::{decl_module, decl_event, impl_outer_origin, impl_outer_event}; +use frame_support::{decl_module, decl_event}; use sp_core::H256; use sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header}; @@ -41,16 +41,19 @@ mod module { ); } -impl_outer_origin!{ - pub enum Origin for Runtime {} -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; -impl_outer_event! { - pub enum Event for Runtime { - system, - module, +frame_support::construct_runtime!( + pub enum Runtime where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Module: module::{Module, Call, Event}, } -} +); frame_support::parameter_types! { pub const BlockHashCount: u64 = 250; @@ -63,8 +66,6 @@ frame_support::parameter_types! { 4 * 1024 * 1024, Perbill::from_percent(75), ); } -#[derive(Clone, Eq, PartialEq)] -pub struct Runtime; impl system::Config for Runtime { type BaseCallFilter = (); type BlockWeights = (); @@ -73,7 +74,7 @@ impl system::Config for Runtime { type Origin = Origin; type Index = u64; type BlockNumber = u64; - type Call = (); + type Call = Call; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = u64; @@ -82,7 +83,7 @@ impl system::Config for Runtime { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); diff --git a/frame/system/benchmarking/Cargo.toml b/frame/system/benchmarking/Cargo.toml index e164a0d62e0fb..ddf52c96effe6 100644 --- a/frame/system/benchmarking/Cargo.toml +++ b/frame/system/benchmarking/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-system-benchmarking" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,17 +13,17 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -sp-std = { version = "2.0.0", default-features = false, path = "../../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../primitives/runtime" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../../benchmarking" } -frame-system = { version = "2.0.0", default-features = false, path = "../../system" } -frame-support = { version = "2.0.0", default-features = false, path = "../../support" } -sp-core = { version = "2.0.0", default-features = false, path = "../../../primitives/core" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +sp-std = { version = "3.0.0", default-features = false, path = "../../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../primitives/runtime" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../../benchmarking" } +frame-system = { version = "3.0.0", default-features = false, path = "../../system" } +frame-support = { version = "3.0.0", default-features = false, path = "../../support" } +sp-core = { version = "3.0.0", default-features = false, path = "../../../primitives/core" } [dev-dependencies] serde = { version = "1.0.101" } -sp-io ={ version = "2.0.0", path = "../../../primitives/io" } +sp-io ={ version = "3.0.0", path = "../../../primitives/io" } [features] default = ["std"] diff --git a/frame/system/benchmarking/src/lib.rs b/frame/system/benchmarking/src/lib.rs index 9ff749950ab5e..a23ea07df0ea9 100644 --- a/frame/system/benchmarking/src/lib.rs +++ b/frame/system/benchmarking/src/lib.rs @@ -24,7 +24,7 @@ use sp_std::vec; use sp_std::prelude::*; use sp_core::{ChangesTrieConfiguration, storage::well_known_keys}; use sp_runtime::traits::Hash; -use frame_benchmarking::{benchmarks, whitelisted_caller}; +use frame_benchmarking::{benchmarks, whitelisted_caller, impl_benchmark_test_suite}; use frame_support::{ storage, traits::Get, @@ -138,22 +138,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::mock::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_remark::()); - assert_ok!(test_benchmark_set_heap_pages::()); - assert_ok!(test_benchmark_set_code_without_checks::()); - assert_ok!(test_benchmark_set_changes_trie_config::()); - assert_ok!(test_benchmark_set_storage::()); - assert_ok!(test_benchmark_kill_storage::()); - assert_ok!(test_benchmark_kill_prefix::()); - }); - } -} +impl_benchmark_test_suite!( + Module, + crate::mock::new_test_ext(), + crate::mock::Test, +); diff --git a/frame/system/benchmarking/src/mock.rs b/frame/system/benchmarking/src/mock.rs index 87f9113a49311..edc5dfebbd106 100644 --- a/frame/system/benchmarking/src/mock.rs +++ b/frame/system/benchmarking/src/mock.rs @@ -20,35 +20,23 @@ #![cfg(test)] use sp_runtime::traits::IdentityLookup; -use frame_support::{ - impl_outer_origin, - dispatch::{Dispatchable, DispatchInfo, PostDispatchInfo}, -}; type AccountId = u64; type AccountIndex = u32; type BlockNumber = u64; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} - -#[derive(Debug, codec::Encode, codec::Decode)] -pub struct Call; +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; -impl Dispatchable for Call { - type Origin = (); - type Config = (); - type Info = DispatchInfo; - type PostInfo = PostDispatchInfo; - fn dispatch(self, _origin: Self::Origin) - -> sp_runtime::DispatchResultWithInfo { - panic!("Do not use dummy implementation for dispatch."); +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, } -} - -#[derive(Clone, Eq, PartialEq, Debug)] -pub struct Test; +); impl frame_system::Config for Test { type BaseCallFilter = (); @@ -64,10 +52,10 @@ impl frame_system::Config for Test { type AccountId = AccountId; type Lookup = IdentityLookup; type Header = sp_runtime::testing::Header; - type Event = (); + type Event = Event; type BlockHashCount = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); diff --git a/frame/system/rpc/runtime-api/Cargo.toml b/frame/system/rpc/runtime-api/Cargo.toml index 4820df10fe16d..56619d59ddcad 100644 --- a/frame/system/rpc/runtime-api/Cargo.toml +++ b/frame/system/rpc/runtime-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-system-rpc-runtime-api" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,8 +13,8 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-api = { version = "2.0.0", default-features = false, path = "../../../../primitives/api" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } +sp-api = { version = "3.0.0", default-features = false, path = "../../../../primitives/api" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } [features] default = ["std"] diff --git a/frame/system/src/extensions/check_mortality.rs b/frame/system/src/extensions/check_mortality.rs index f1951baba5d50..1e8eb32a3d3c2 100644 --- a/frame/system/src/extensions/check_mortality.rs +++ b/frame/system/src/extensions/check_mortality.rs @@ -110,7 +110,7 @@ mod tests { let normal = DispatchInfo { weight: 100, class: DispatchClass::Normal, pays_fee: Pays::Yes }; let len = 0_usize; let ext = ( - crate::CheckWeight::::default(), + crate::CheckWeight::::new(), CheckMortality::::from(Era::mortal(16, 256)), ); System::set_block_number(17); diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 87a636b37f1ca..e521a082a91ca 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -77,7 +77,7 @@ use sp_runtime::{ traits::{ self, CheckEqual, AtLeast32Bit, Zero, Lookup, LookupError, SimpleBitOps, Hash, Member, MaybeDisplay, BadOrigin, - MaybeSerialize, MaybeSerializeDeserialize, MaybeMallocSizeOf, StaticLookup, One, Bounded, + MaybeSerializeDeserialize, MaybeMallocSizeOf, StaticLookup, One, Bounded, Dispatchable, AtLeast32BitUnsigned, Saturating, StoredMapError, }, offchain::storage_lock::BlockNumberProvider, @@ -174,7 +174,7 @@ pub mod pallet { /// Account index (aka nonce) type. This stores the number of previous transactions associated /// with a sender account. type Index: - Parameter + Member + MaybeSerialize + Debug + Default + MaybeDisplay + AtLeast32Bit + Parameter + Member + MaybeSerializeDeserialize + Debug + Default + MaybeDisplay + AtLeast32Bit + Copy; /// The block number type used by the runtime. @@ -257,7 +257,7 @@ pub mod pallet { #[pallet::pallet] #[pallet::generate_store(pub (super) trait Store)] - pub struct Pallet(PhantomData); + pub struct Pallet(_); #[pallet::hooks] impl Hooks> for Pallet { diff --git a/frame/system/src/limits.rs b/frame/system/src/limits.rs index c24d671cdd7ab..49a458224020c 100644 --- a/frame/system/src/limits.rs +++ b/frame/system/src/limits.rs @@ -129,6 +129,7 @@ pub struct WeightsPerClass { /// `on_initialize` pallet callbacks are invoked and their cost is added before any extrinsic /// is executed. This cost is tracked as `Mandatory` dispatch class. /// +/// ```text,ignore /// | | `max_block` | | /// | | | | /// | | | | @@ -139,12 +140,15 @@ pub struct WeightsPerClass { /// ||\_ Mandatory /// |\__ Operational /// \___ Normal +/// ``` /// /// The remaining capacity can be used to dispatch extrinsics. Note that each dispatch class /// is being tracked separately, but the sum can't exceed `max_block` (except for `reserved`). /// Below you can see a picture representing full block with 3 extrinsics (two `Operational` and /// one `Normal`). Each class has it's own limit `max_total`, but also the sum cannot exceed /// `max_block` value. +/// +/// ```text,ignore /// -- `Mandatory` limit (unlimited) /// | # | | | /// | # | `Ext3` | - - `Operational` limit @@ -153,6 +157,7 @@ pub struct WeightsPerClass { /// | #| `on_initialize` | ##| /// | #| `base_block` |###| /// |NOM| |NOM| +/// ``` /// /// It should be obvious now that it's possible for one class to reach it's limit (say `Normal`), /// while the block has still capacity to process more transactions (`max_block` not reached, @@ -164,6 +169,8 @@ pub struct WeightsPerClass { /// full. For instance one might want to prevent high-priority `Normal` transactions from pushing /// out lower-priority `Operational` transactions. In such cases you might add a `reserved` capacity /// for given class. +/// +/// ```test,ignore /// _ /// # \ /// # `Ext8` - `reserved` @@ -175,6 +182,7 @@ pub struct WeightsPerClass { /// | #| `on_initialize` |###| /// | #| `base_block` |###| /// |NOM| |NOM| +/// ``` /// /// In the above example, `Ext4-6` fill up the block almost up to `max_block`. `Ext7` would not fit /// if there wasn't the extra `reserved` space for `Operational` transactions. Note that `max_total` diff --git a/frame/system/src/mock.rs b/frame/system/src/mock.rs index d67f00917fd00..2b31929b5da81 100644 --- a/frame/system/src/mock.rs +++ b/frame/system/src/mock.rs @@ -15,24 +15,27 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::*; +use crate::{self as frame_system, *}; use sp_std::cell::RefCell; use sp_core::H256; use sp_runtime::{ traits::{BlakeTwo256, IdentityLookup}, - testing::Header, -}; -use frame_support::{ - impl_outer_origin, parameter_types, - weights::PostDispatchInfo, + testing::Header, BuildStorage, }; +use frame_support::parameter_types; -impl_outer_origin! { - pub enum Origin for Test where system = super {} -} +type UncheckedExtrinsic = mocking::MockUncheckedExtrinsic; +type Block = mocking::MockBlock; -#[derive(Clone, Eq, PartialEq, Debug, Default)] -pub struct Test; +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + } +); const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); const MAX_BLOCK_WEIGHT: Weight = 1024; @@ -81,20 +84,6 @@ impl OnKilledAccount for RecordKilled { fn on_killed_account(who: &u64) { KILLED.with(|r| r.borrow_mut().push(*who)) } } -#[derive(Debug, codec::Encode, codec::Decode)] -pub struct Call; - -impl Dispatchable for Call { - type Origin = Origin; - type Config = (); - type Info = DispatchInfo; - type PostInfo = PostDispatchInfo; - fn dispatch(self, _origin: Self::Origin) - -> sp_runtime::DispatchResultWithInfo { - panic!("Do not use dummy implementation for dispatch."); - } -} - impl Config for Test { type BaseCallFilter = (); type BlockWeights = RuntimeBlockWeights; @@ -108,11 +97,11 @@ impl Config for Test { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = Event; + type Event = Event; type BlockHashCount = BlockHashCount; type DbWeight = DbWeight; type Version = Version; - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = u32; type OnNewAccount = (); type OnKilledAccount = RecordKilled; @@ -120,14 +109,15 @@ impl Config for Test { type SS58Prefix = (); } -pub type System = Module; -pub type SysEvent = ::Event; +pub type SysEvent = frame_system::Event; -pub const CALL: &::Call = &Call; +/// A simple call, which one doesn't matter. +pub const CALL: &::Call = &Call::System(frame_system::Call::set_heap_pages(0u64)); /// Create new externalities for `System` module tests. pub fn new_test_ext() -> sp_io::TestExternalities { - let mut ext: sp_io::TestExternalities = GenesisConfig::default().build_storage::().unwrap().into(); + let mut ext: sp_io::TestExternalities = GenesisConfig::default() + .build_storage().unwrap().into(); // Add to each test the initial weight of a block ext.execute_with(|| System::register_extra_weight_unchecked( ::BlockWeights::get().base_block, diff --git a/frame/system/src/offchain.rs b/frame/system/src/offchain.rs index 05a5882ee3982..f2f446913c477 100644 --- a/frame/system/src/offchain.rs +++ b/frame/system/src/offchain.rs @@ -637,7 +637,7 @@ pub trait SignedPayload: Encode { mod tests { use super::*; use codec::Decode; - use crate::mock::{Test as TestRuntime, Call}; + use crate::mock::{Test as TestRuntime, Call, CALL}; use sp_core::offchain::{testing, TransactionPoolExt}; use sp_runtime::testing::{UintAuthorityId, TestSignature, TestXt}; @@ -708,7 +708,7 @@ mod tests { public: account.public.clone() }, |_payload, _signature| { - Call + CALL.clone() } ); @@ -749,7 +749,7 @@ mod tests { public: account.public.clone() }, |_payload, _signature| { - Call + CALL.clone() } ); @@ -787,7 +787,7 @@ mod tests { public: account.public.clone() }, |_payload, _signature| { - Call + CALL.clone() } ); @@ -827,7 +827,7 @@ mod tests { public: account.public.clone() }, |_payload, _signature| { - Call + CALL.clone() } ); diff --git a/frame/system/src/tests.rs b/frame/system/src/tests.rs index d1992a14e06aa..ca17edcf4b22a 100644 --- a/frame/system/src/tests.rs +++ b/frame/system/src/tests.rs @@ -71,7 +71,7 @@ fn deposit_event_should_work() { vec![ EventRecord { phase: Phase::Finalization, - event: SysEvent::CodeUpdated, + event: SysEvent::CodeUpdated.into(), topics: vec![], } ] @@ -99,17 +99,17 @@ fn deposit_event_should_work() { vec![ EventRecord { phase: Phase::Initialization, - event: SysEvent::NewAccount(32), + event: SysEvent::NewAccount(32).into(), topics: vec![], }, EventRecord { phase: Phase::ApplyExtrinsic(0), - event: SysEvent::KilledAccount(42), + event: SysEvent::KilledAccount(42).into(), topics: vec![] }, EventRecord { phase: Phase::ApplyExtrinsic(0), - event: SysEvent::ExtrinsicSuccess(Default::default()), + event: SysEvent::ExtrinsicSuccess(Default::default()).into(), topics: vec![] }, EventRecord { @@ -117,12 +117,12 @@ fn deposit_event_should_work() { event: SysEvent::ExtrinsicFailed( DispatchError::BadOrigin.into(), Default::default() - ), + ).into(), topics: vec![] }, EventRecord { phase: Phase::Finalization, - event: SysEvent::NewAccount(3), + event: SysEvent::NewAccount(3).into(), topics: vec![] }, ] @@ -173,7 +173,7 @@ fn deposit_event_uses_actual_weight() { weight: 300, .. Default::default() }, - ), + ).into(), topics: vec![] }, EventRecord { @@ -183,7 +183,7 @@ fn deposit_event_uses_actual_weight() { weight: 1000, .. Default::default() }, - ), + ).into(), topics: vec![] }, EventRecord { @@ -193,7 +193,7 @@ fn deposit_event_uses_actual_weight() { weight: 1000, .. Default::default() }, - ), + ).into(), topics: vec![] }, EventRecord { @@ -204,7 +204,7 @@ fn deposit_event_uses_actual_weight() { weight: 999, .. Default::default() }, - ), + ).into(), topics: vec![] }, ] @@ -232,9 +232,9 @@ fn deposit_event_topics() { ]; // We deposit a few events with different sets of topics. - System::deposit_event_indexed(&topics[0..3], SysEvent::NewAccount(1)); - System::deposit_event_indexed(&topics[0..1], SysEvent::NewAccount(2)); - System::deposit_event_indexed(&topics[1..2], SysEvent::NewAccount(3)); + System::deposit_event_indexed(&topics[0..3], SysEvent::NewAccount(1).into()); + System::deposit_event_indexed(&topics[0..1], SysEvent::NewAccount(2).into()); + System::deposit_event_indexed(&topics[1..2], SysEvent::NewAccount(3).into()); System::finalize(); @@ -244,17 +244,17 @@ fn deposit_event_topics() { vec![ EventRecord { phase: Phase::Finalization, - event: SysEvent::NewAccount(1), + event: SysEvent::NewAccount(1).into(), topics: topics[0..3].to_vec(), }, EventRecord { phase: Phase::Finalization, - event: SysEvent::NewAccount(2), + event: SysEvent::NewAccount(2).into(), topics: topics[0..1].to_vec(), }, EventRecord { phase: Phase::Finalization, - event: SysEvent::NewAccount(3), + event: SysEvent::NewAccount(3).into(), topics: topics[1..2].to_vec(), } ] @@ -375,7 +375,7 @@ fn set_code_with_real_wasm_blob() { System::events(), vec![EventRecord { phase: Phase::Initialization, - event: SysEvent::CodeUpdated, + event: SysEvent::CodeUpdated.into(), topics: vec![], }], ); diff --git a/frame/system/src/weights.rs b/frame/system/src/weights.rs index f28e90b34c38b..823e4b7d1e0db 100644 --- a/frame/system/src/weights.rs +++ b/frame/system/src/weights.rs @@ -1,6 +1,6 @@ // This file is part of Substrate. -// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd. +// Copyright (C) 2021 Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,9 +15,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Weights for frame_system -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0 -//! DATE: 2020-10-28, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] +//! Autogenerated weights for frame_system +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 +//! DATE: 2021-02-23, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -49,74 +50,73 @@ pub trait WeightInfo { fn set_storage(i: u32, ) -> Weight; fn kill_storage(i: u32, ) -> Weight; fn kill_prefix(p: u32, ) -> Weight; - fn suicide() -> Weight; } /// Weights for frame_system using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { fn remark(_b: u32, ) -> Weight { - (1_973_000 as Weight) + (1_279_000 as Weight) } fn set_heap_pages() -> Weight { - (2_816_000 as Weight) + (2_167_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_changes_trie_config() -> Weight { - (11_539_000 as Weight) + (10_117_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn set_storage(i: u32, ) -> Weight { (0 as Weight) - .saturating_add((833_000 as Weight).saturating_mul(i as Weight)) + // Standard Error: 0 + .saturating_add((608_000 as Weight).saturating_mul(i as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(i as Weight))) } fn kill_storage(i: u32, ) -> Weight { - (2_131_000 as Weight) - .saturating_add((597_000 as Weight).saturating_mul(i as Weight)) + (3_199_000 as Weight) + // Standard Error: 0 + .saturating_add((450_000 as Weight).saturating_mul(i as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(i as Weight))) } fn kill_prefix(p: u32, ) -> Weight { - (11_844_000 as Weight) - .saturating_add((857_000 as Weight).saturating_mul(p as Weight)) + (8_966_000 as Weight) + // Standard Error: 1_000 + .saturating_add((845_000 as Weight).saturating_mul(p as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(p as Weight))) } - fn suicide() -> Weight { - (37_209_000 as Weight) - } } // For backwards compatibility and tests impl WeightInfo for () { fn remark(_b: u32, ) -> Weight { - (1_973_000 as Weight) + (1_279_000 as Weight) } fn set_heap_pages() -> Weight { - (2_816_000 as Weight) + (2_167_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_changes_trie_config() -> Weight { - (11_539_000 as Weight) + (10_117_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } fn set_storage(i: u32, ) -> Weight { (0 as Weight) - .saturating_add((833_000 as Weight).saturating_mul(i as Weight)) + // Standard Error: 0 + .saturating_add((608_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(i as Weight))) } fn kill_storage(i: u32, ) -> Weight { - (2_131_000 as Weight) - .saturating_add((597_000 as Weight).saturating_mul(i as Weight)) + (3_199_000 as Weight) + // Standard Error: 0 + .saturating_add((450_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(i as Weight))) } fn kill_prefix(p: u32, ) -> Weight { - (11_844_000 as Weight) - .saturating_add((857_000 as Weight).saturating_mul(p as Weight)) + (8_966_000 as Weight) + // Standard Error: 1_000 + .saturating_add((845_000 as Weight).saturating_mul(p as Weight)) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(p as Weight))) } - fn suicide() -> Weight { - (37_209_000 as Weight) - } } diff --git a/frame/timestamp/Cargo.toml b/frame/timestamp/Cargo.toml index 79d8e30935f77..f4f7bbda0f885 100644 --- a/frame/timestamp/Cargo.toml +++ b/frame/timestamp/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-timestamp" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -16,20 +16,20 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io", optional = true } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-inherents = { version = "2.0.0", default-features = false, path = "../../primitives/inherents" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-timestamp = { version = "2.0.0", default-features = false, path = "../../primitives/timestamp" } -impl-trait-for-tuples = "0.2.0" +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io", optional = true } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-inherents = { version = "3.0.0", default-features = false, path = "../../primitives/inherents" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-timestamp = { version = "3.0.0", default-features = false, path = "../../primitives/timestamp" } +impl-trait-for-tuples = "0.2.1" [dev-dependencies] -sp-io ={ version = "2.0.0", path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } +sp-io ={ version = "3.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } [features] default = ["std"] diff --git a/frame/timestamp/src/benchmarking.rs b/frame/timestamp/src/benchmarking.rs index 024e6967826cd..57b8ce2d1b703 100644 --- a/frame/timestamp/src/benchmarking.rs +++ b/frame/timestamp/src/benchmarking.rs @@ -23,7 +23,7 @@ use super::*; use sp_std::prelude::*; use frame_system::RawOrigin; use frame_support::{ensure, traits::OnFinalize}; -use frame_benchmarking::{benchmarks, TrackedStorageKey}; +use frame_benchmarking::{benchmarks, TrackedStorageKey, impl_benchmark_test_suite}; use crate::Module as Timestamp; @@ -33,7 +33,7 @@ benchmarks! { set { let t = MAX_TIME; // Ignore write to `DidUpdate` since it transient. - let did_update_key = crate::DidUpdate::hashed_key().to_vec(); + let did_update_key = crate::DidUpdate::::hashed_key().to_vec(); frame_benchmarking::benchmarking::add_to_whitelist(TrackedStorageKey { key: did_update_key, has_been_read: false, @@ -47,27 +47,18 @@ benchmarks! { on_finalize { let t = MAX_TIME; Timestamp::::set(RawOrigin::None.into(), t.into())?; - ensure!(DidUpdate::exists(), "Time was not set."); + ensure!(DidUpdate::::exists(), "Time was not set."); // Ignore read/write to `DidUpdate` since it is transient. - let did_update_key = crate::DidUpdate::hashed_key().to_vec(); + let did_update_key = crate::DidUpdate::::hashed_key().to_vec(); frame_benchmarking::benchmarking::add_to_whitelist(did_update_key.into()); }: { Timestamp::::on_finalize(t.into()); } verify { - ensure!(!DidUpdate::exists(), "Time was not removed."); + ensure!(!DidUpdate::::exists(), "Time was not removed."); } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_set::()); - assert_ok!(test_benchmark_on_finalize::()); - }); - } -} +impl_benchmark_test_suite!( + Timestamp, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/timestamp/src/lib.rs b/frame/timestamp/src/lib.rs index ae7ba4814694b..86ca0c11a70c8 100644 --- a/frame/timestamp/src/lib.rs +++ b/frame/timestamp/src/lib.rs @@ -15,23 +15,23 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! # Timestamp Module +//! # Timestamp Pallet //! -//! The Timestamp module provides functionality to get and set the on-chain time. +//! The Timestamp pallet provides functionality to get and set the on-chain time. //! //! - [`timestamp::Config`](./trait.Config.html) //! - [`Call`](./enum.Call.html) -//! - [`Module`](./struct.Module.html) +//! - [`Pallet`](./struct.Pallet.html) //! //! ## Overview //! -//! The Timestamp module allows the validators to set and validate a timestamp with each block. +//! The Timestamp pallet allows the validators to set and validate a timestamp with each block. //! //! It uses inherents for timestamp data, which is provided by the block author and validated/verified //! by other validators. The timestamp can be set only once per block and must be set each block. //! There could be a constraint on how much time must pass before setting the new timestamp. //! -//! **NOTE:** The Timestamp module is the recommended way to query the on-chain time instead of using +//! **NOTE:** The Timestamp pallet is the recommended way to query the on-chain time instead of using //! an approach based on block numbers. The block number based time measurement can cause issues //! because of cumulative calculation errors and hence should be avoided. //! @@ -52,11 +52,11 @@ //! //! ## Usage //! -//! The following example shows how to use the Timestamp module in your custom module to query the current timestamp. +//! The following example shows how to use the Timestamp pallet in your custom pallet to query the current timestamp. //! //! ### Prerequisites //! -//! Import the Timestamp module into your custom module and derive the module configuration +//! Import the Timestamp pallet into your custom pallet and derive the pallet configuration //! trait from the timestamp trait. //! //! ### Get current timestamp @@ -83,10 +83,10 @@ //! //! ### Example from the FRAME //! -//! The [Session module](https://github.com/paritytech/substrate/blob/master/frame/session/src/lib.rs) uses -//! the Timestamp module for session management. +//! The [Session pallet](https://github.com/paritytech/substrate/blob/master/frame/session/src/lib.rs) uses +//! the Timestamp pallet for session management. //! -//! ## Related Modules +//! ## Related Pallets //! //! * [Session](../pallet_session/index.html) @@ -96,54 +96,83 @@ mod benchmarking; pub mod weights; use sp_std::{result, cmp}; -use sp_inherents::{ProvideInherent, InherentData, InherentIdentifier}; +use sp_inherents::InherentData; #[cfg(feature = "std")] use frame_support::debug; -use frame_support::{ - Parameter, decl_storage, decl_module, - traits::{Time, UnixTime, Get}, - weights::{DispatchClass, Weight}, -}; +use frame_support::traits::{Time, UnixTime}; use sp_runtime::{ RuntimeString, traits::{ AtLeast32Bit, Zero, SaturatedConversion, Scale, } }; -use frame_system::ensure_none; use sp_timestamp::{ InherentError, INHERENT_IDENTIFIER, InherentType, OnTimestampSet, }; pub use weights::WeightInfo; -/// The module configuration trait -pub trait Config: frame_system::Config { - /// Type used for expressing timestamp. - type Moment: Parameter + Default + AtLeast32Bit - + Scale + Copy; +pub use pallet::*; - /// Something which can be notified when the timestamp is set. Set this to `()` if not needed. - type OnTimestampSet: OnTimestampSet; +#[frame_support::pallet] +pub mod pallet { + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use super::*; - /// The minimum period between blocks. Beware that this is different to the *expected* period - /// that the block production apparatus provides. Your chosen consensus system will generally - /// work with this to determine a sensible block time. e.g. For Aura, it will be double this - /// period on default settings. - type MinimumPeriod: Get; + /// The pallet configuration trait + #[pallet::config] + pub trait Config: frame_system::Config { + /// Type used for expressing timestamp. + type Moment: Parameter + Default + AtLeast32Bit + + Scale + Copy; - /// Weight information for extrinsics in this pallet. - type WeightInfo: WeightInfo; -} + /// Something which can be notified when the timestamp is set. Set this to `()` if not needed. + type OnTimestampSet: OnTimestampSet; -decl_module! { - pub struct Module for enum Call where origin: T::Origin { /// The minimum period between blocks. Beware that this is different to the *expected* period /// that the block production apparatus provides. Your chosen consensus system will generally /// work with this to determine a sensible block time. e.g. For Aura, it will be double this /// period on default settings. - const MinimumPeriod: T::Moment = T::MinimumPeriod::get(); + #[pallet::constant] + type MinimumPeriod: Get; + + /// Weight information for extrinsics in this pallet. + type WeightInfo: WeightInfo; + } + + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(PhantomData); + + /// Current time for the current block. + #[pallet::storage] + #[pallet::getter(fn now)] + pub type Now = StorageValue<_, T::Moment, ValueQuery>; + + /// Did the timestamp get updated in this block? + #[pallet::storage] + pub(super) type DidUpdate = StorageValue<_, bool, ValueQuery>; + + #[pallet::hooks] + impl Hooks> for Pallet { + /// dummy `on_initialize` to return the weight used in `on_finalize`. + fn on_initialize(_n: BlockNumberFor) -> Weight { + // weight of `on_finalize` + T::WeightInfo::on_finalize() + } + + /// # + /// - `O(1)` + /// - 1 storage deletion (codec `O(1)`). + /// # + fn on_finalize(_n: BlockNumberFor) { + assert!(DidUpdate::::take(), "Timestamp must be updated once in the block"); + } + } + #[pallet::call] + impl Pallet { /// Set the current time. /// /// This call should be invoked exactly once per block. It will panic at the finalization @@ -159,51 +188,65 @@ decl_module! { /// - 1 storage read and 1 storage mutation (codec `O(1)`). (because of `DidUpdate::take` in `on_finalize`) /// - 1 event handler `on_timestamp_set`. Must be `O(1)`. /// # - #[weight = ( + #[pallet::weight(( T::WeightInfo::set(), DispatchClass::Mandatory - )] - fn set(origin, #[compact] now: T::Moment) { + ))] + pub(super) fn set(origin: OriginFor, #[pallet::compact] now: T::Moment) -> DispatchResultWithPostInfo { ensure_none(origin)?; - assert!(!::DidUpdate::exists(), "Timestamp must be updated only once in the block"); + assert!(!DidUpdate::::exists(), "Timestamp must be updated only once in the block"); let prev = Self::now(); assert!( prev.is_zero() || now >= prev + T::MinimumPeriod::get(), "Timestamp must increment by at least between sequential blocks" ); - ::Now::put(now); - ::DidUpdate::put(true); + Now::::put(now); + DidUpdate::::put(true); >::on_timestamp_set(now); - } - /// dummy `on_initialize` to return the weight used in `on_finalize`. - fn on_initialize() -> Weight { - // weight of `on_finalize` - T::WeightInfo::on_finalize() + Ok(().into()) } + } - /// # - /// - `O(1)` - /// - 1 storage deletion (codec `O(1)`). - /// # - fn on_finalize() { - assert!(::DidUpdate::take(), "Timestamp must be updated once in the block"); + #[pallet::inherent] + impl ProvideInherent for Pallet { + type Call = Call; + type Error = InherentError; + const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER; + + fn create_inherent(data: &InherentData) -> Option { + let data: T::Moment = extract_inherent_data(data) + .expect("Gets and decodes timestamp inherent data") + .saturated_into(); + + let next_time = cmp::max(data, Self::now() + T::MinimumPeriod::get()); + Some(Call::set(next_time.into())) } - } -} -decl_storage! { - trait Store for Module as Timestamp { - /// Current time for the current block. - pub Now get(fn now): T::Moment; + fn check_inherent(call: &Self::Call, data: &InherentData) -> result::Result<(), Self::Error> { + const MAX_TIMESTAMP_DRIFT_MILLIS: u64 = 30 * 1000; - /// Did the timestamp get updated in this block? - DidUpdate: bool; + let t: u64 = match call { + Call::set(ref t) => t.clone().saturated_into::(), + _ => return Ok(()), + }; + + let data = extract_inherent_data(data).map_err(|e| InherentError::Other(e))?; + + let minimum = (Self::now() + T::MinimumPeriod::get()).saturated_into::(); + if t > data + MAX_TIMESTAMP_DRIFT_MILLIS { + Err(InherentError::Other("Timestamp too far in future to accept".into())) + } else if t < minimum { + Err(InherentError::ValidAtTimestamp(minimum)) + } else { + Ok(()) + } + } } } -impl Module { +impl Pallet { /// Get the current time for the current block. /// /// NOTE: if this function is called prior to setting the timestamp, @@ -215,7 +258,7 @@ impl Module { /// Set the timestamp to something in particular. Only used for tests. #[cfg(feature = "std")] pub fn set_timestamp(now: T::Moment) { - ::Now::put(now); + Now::::put(now); } } @@ -225,42 +268,7 @@ fn extract_inherent_data(data: &InherentData) -> Result ProvideInherent for Module { - type Call = Call; - type Error = InherentError; - const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER; - - fn create_inherent(data: &InherentData) -> Option { - let data: T::Moment = extract_inherent_data(data) - .expect("Gets and decodes timestamp inherent data") - .saturated_into(); - - let next_time = cmp::max(data, Self::now() + T::MinimumPeriod::get()); - Some(Call::set(next_time.into())) - } - - fn check_inherent(call: &Self::Call, data: &InherentData) -> result::Result<(), Self::Error> { - const MAX_TIMESTAMP_DRIFT_MILLIS: u64 = 30 * 1000; - - let t: u64 = match call { - Call::set(ref t) => t.clone().saturated_into::(), - _ => return Ok(()), - }; - - let data = extract_inherent_data(data).map_err(|e| InherentError::Other(e))?; - - let minimum = (Self::now() + T::MinimumPeriod::get()).saturated_into::(); - if t > data + MAX_TIMESTAMP_DRIFT_MILLIS { - Err(InherentError::Other("Timestamp too far in future to accept".into())) - } else if t < minimum { - Err(InherentError::ValidAtTimestamp(minimum)) - } else { - Ok(()) - } - } -} - -impl Time for Module { +impl Time for Pallet { type Moment = T::Moment; /// Before the first set of now with inherent the value returned is zero. @@ -272,7 +280,7 @@ impl Time for Module { /// Before the timestamp inherent is applied, it returns the time of previous block. /// /// On genesis the time returned is not valid. -impl UnixTime for Module { +impl UnixTime for Pallet { fn now() -> core::time::Duration { // now is duration since unix epoch in millisecond as documented in // `sp_timestamp::InherentDataProvider`. diff --git a/frame/tips/Cargo.toml b/frame/tips/Cargo.toml index 0ce81a6d5d1b8..92af65ce07650 100644 --- a/frame/tips/Cargo.toml +++ b/frame/tips/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-tips" -version = "2.0.0" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,20 +14,20 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-treasury = { version = "2.0.0", default-features = false, path = "../treasury" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +pallet-treasury = { version = "3.0.0", default-features = false, path = "../treasury" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-io ={ version = "2.0.0", path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-storage = { version = "2.0.0", path = "../../primitives/storage" } -pallet-balances = { version = "2.0.0", path = "../balances" } +sp-io ={ version = "3.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-storage = { version = "3.0.0", path = "../../primitives/storage" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/tips/src/benchmarking.rs b/frame/tips/src/benchmarking.rs index e05afc0b2ab20..e6a0284d82307 100644 --- a/frame/tips/src/benchmarking.rs +++ b/frame/tips/src/benchmarking.rs @@ -22,7 +22,7 @@ use super::*; use frame_system::RawOrigin; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use sp_runtime::{traits::{Saturating}}; use crate::Module as TipsMod; @@ -193,21 +193,8 @@ benchmarks! { }: _(RawOrigin::Root, hash) } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_report_awesome::()); - assert_ok!(test_benchmark_retract_tip::()); - assert_ok!(test_benchmark_tip_new::()); - assert_ok!(test_benchmark_tip::()); - assert_ok!(test_benchmark_close_tip::()); - assert_ok!(test_benchmark_slash_tip::()); - }); - } -} +impl_benchmark_test_suite!( + TipsMod, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/transaction-payment/Cargo.toml b/frame/transaction-payment/Cargo.toml index 1f64ae03995bc..7a713ab1cfbd1 100644 --- a/frame/transaction-payment/Cargo.toml +++ b/frame/transaction-payment/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-transaction-payment" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,20 +13,20 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } serde = { version = "1.0.101", optional = true } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } smallvec = "1.4.1" -sp-io = { version = "2.0.0", path = "../../primitives/io", default-features = false } -sp-core = { version = "2.0.0", path = "../../primitives/core", default-features = false } +sp-io = { version = "3.0.0", path = "../../primitives/io", default-features = false } +sp-core = { version = "3.0.0", path = "../../primitives/core", default-features = false } [dev-dependencies] serde_json = "1.0.41" -pallet-balances = { version = "2.0.0", path = "../balances" } -sp-storage = { version = "2.0.0", path = "../../primitives/storage" } +pallet-balances = { version = "3.0.0", path = "../balances" } +sp-storage = { version = "3.0.0", path = "../../primitives/storage" } [features] default = ["std"] diff --git a/frame/transaction-payment/rpc/Cargo.toml b/frame/transaction-payment/rpc/Cargo.toml index 410827d0efb5b..102f91dcc2c08 100644 --- a/frame/transaction-payment/rpc/Cargo.toml +++ b/frame/transaction-payment/rpc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-transaction-payment-rpc" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,13 +13,13 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } jsonrpc-core = "15.1.0" jsonrpc-core-client = "15.1.0" jsonrpc-derive = "15.1.0" -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sp-rpc = { version = "2.0.0", path = "../../../primitives/rpc" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-api = { version = "2.0.0", path = "../../../primitives/api" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -pallet-transaction-payment-rpc-runtime-api = { version = "2.0.0", path = "./runtime-api" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sp-rpc = { version = "3.0.0", path = "../../../primitives/rpc" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +pallet-transaction-payment-rpc-runtime-api = { version = "3.0.0", path = "./runtime-api" } diff --git a/frame/transaction-payment/rpc/runtime-api/Cargo.toml b/frame/transaction-payment/rpc/runtime-api/Cargo.toml index 64c082b420c93..fede9f9dd0267 100644 --- a/frame/transaction-payment/rpc/runtime-api/Cargo.toml +++ b/frame/transaction-payment/rpc/runtime-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-transaction-payment-rpc-runtime-api" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,10 +13,10 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-api = { version = "2.0.0", default-features = false, path = "../../../../primitives/api" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../../../primitives/runtime" } -pallet-transaction-payment = { version = "2.0.0", default-features = false, path = "../../../transaction-payment" } +sp-api = { version = "3.0.0", default-features = false, path = "../../../../primitives/api" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../../../primitives/runtime" } +pallet-transaction-payment = { version = "3.0.0", default-features = false, path = "../../../transaction-payment" } [features] default = ["std"] diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 5f907fb91b99e..709a8f69a487d 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -681,7 +681,7 @@ mod tests { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -1156,7 +1156,7 @@ mod tests { assert_eq!(Balances::free_balance(2), 0); // Transfer Event assert!(System::events().iter().any(|event| { - event.event == Event::pallet_balances(pallet_balances::RawEvent::Transfer(2, 3, 80)) + event.event == Event::pallet_balances(pallet_balances::Event::Transfer(2, 3, 80)) })); // Killed Event assert!(System::events().iter().any(|event| { diff --git a/frame/treasury/Cargo.toml b/frame/treasury/Cargo.toml index 07b22002ee384..461dc91223946 100644 --- a/frame/treasury/Cargo.toml +++ b/frame/treasury/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-treasury" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,20 +14,20 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -pallet-balances = { version = "2.0.0", default-features = false, path = "../balances" } -impl-trait-for-tuples = "0.2.0" +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +pallet-balances = { version = "3.0.0", default-features = false, path = "../balances" } +impl-trait-for-tuples = "0.2.1" -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-io ={ version = "2.0.0", path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-storage = { version = "2.0.0", path = "../../primitives/storage" } +sp-io ={ version = "3.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-storage = { version = "3.0.0", path = "../../primitives/storage" } [features] default = ["std"] diff --git a/frame/treasury/src/benchmarking.rs b/frame/treasury/src/benchmarking.rs index 9cb214420ca43..119516fe2741a 100644 --- a/frame/treasury/src/benchmarking.rs +++ b/frame/treasury/src/benchmarking.rs @@ -22,7 +22,7 @@ use super::*; use frame_system::RawOrigin; -use frame_benchmarking::{benchmarks_instance, account}; +use frame_benchmarking::{benchmarks_instance, account, impl_benchmark_test_suite}; use frame_support::traits::OnInitialize; use crate::Module as Treasury; @@ -66,7 +66,7 @@ fn setup_pot_account, I: Instance>() { } benchmarks_instance! { - + propose_spend { let (caller, value, beneficiary_lookup) = setup_proposal::(SEED); // Whitelist caller account from further DB operations. @@ -103,19 +103,8 @@ benchmarks_instance! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_propose_spend::()); - assert_ok!(test_benchmark_reject_proposal::()); - assert_ok!(test_benchmark_approve_proposal::()); - assert_ok!(test_benchmark_on_initialize_proposals::()); - }); - } -} +impl_benchmark_test_suite!( + Treasury, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/try-runtime/Cargo.toml b/frame/try-runtime/Cargo.toml new file mode 100644 index 0000000000000..9c1919d380b82 --- /dev/null +++ b/frame/try-runtime/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "frame-try-runtime" +version = "0.9.0" +authors = ["Parity Technologies "] +edition = "2018" +license = "Apache-2.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" +description = "FRAME pallet for democracy" +readme = "README.md" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } + +sp-api = { version = "3.0.0", path = "../../primitives/api", default-features = false } +sp-std = { version = "3.0.0", path = "../../primitives/std" , default-features = false } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" , default-features = false } + +frame-support = { version = "3.0.0", path = "../support", default-features = false } + +[features] +default = [ "std" ] +std = [ + "sp-api/std", + "sp-std/std", + "sp-runtime/std", + "frame-support/std", +] diff --git a/frame/try-runtime/src/lib.rs b/frame/try-runtime/src/lib.rs new file mode 100644 index 0000000000000..dcd3a47878237 --- /dev/null +++ b/frame/try-runtime/src/lib.rs @@ -0,0 +1,37 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Supporting types for try-runtime, testing and dry-running commands. + +#![cfg_attr(not(feature = "std"), no_std)] + +use sp_std::prelude::*; +use frame_support::weights::Weight; + +sp_api::decl_runtime_apis! { + /// Runtime api for testing the execution of a runtime upgrade. + pub trait TryRuntime { + /// dry-run runtime upgrades, returning the total weight consumed. + /// + /// This should do EXACTLY the same operations as the runtime would have done in the case of + /// a runtime upgrade (e.g. pallet ordering must be the same) + /// + /// Returns the consumed weight of the migration in case of a successful one, combined with + /// the total allowed block weight of the runtime. + fn on_runtime_upgrade() -> Result<(Weight, Weight), sp_runtime::RuntimeString>; + } +} diff --git a/frame/utility/Cargo.toml b/frame/utility/Cargo.toml index ea8dc1ac015c7..edb930231e176 100644 --- a/frame/utility/Cargo.toml +++ b/frame/utility/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-utility" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,19 +14,19 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-core = { version = "2.0.0", path = "../../primitives/core" } -pallet-balances = { version = "2.0.0", path = "../balances" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] diff --git a/frame/utility/src/benchmarking.rs b/frame/utility/src/benchmarking.rs index 24de60215799f..79fb569c77a5c 100644 --- a/frame/utility/src/benchmarking.rs +++ b/frame/utility/src/benchmarking.rs @@ -21,7 +21,7 @@ use super::*; use frame_system::{RawOrigin, EventRecord}; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; const SEED: u32 = 0; @@ -69,18 +69,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{new_test_ext, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_batch::()); - assert_ok!(test_benchmark_as_derivative::()); - assert_ok!(test_benchmark_batch_all::()); - }); - } -} +impl_benchmark_test_suite!( + Module, + crate::tests::new_test_ext(), + crate::tests::Test, +); diff --git a/frame/utility/src/tests.rs b/frame/utility/src/tests.rs index b14f958bd6f8d..af31bbe96cbc4 100644 --- a/frame/utility/src/tests.rs +++ b/frame/utility/src/tests.rs @@ -104,7 +104,7 @@ impl frame_system::Config for Test { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); diff --git a/frame/vesting/Cargo.toml b/frame/vesting/Cargo.toml index a151219501559..dc42fbcbab108 100644 --- a/frame/vesting/Cargo.toml +++ b/frame/vesting/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-vesting" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,19 +14,19 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } enumflags2 = { version = "0.6.2" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -frame-support = { version = "2.0.0", default-features = false, path = "../support" } -frame-system = { version = "2.0.0", default-features = false, path = "../system" } -frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-io = { version = "2.0.0", path = "../../primitives/io" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -pallet-balances = { version = "2.0.0", path = "../balances" } -sp-storage = { version = "2.0.0", path = "../../primitives/storage" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-balances = { version = "3.0.0", path = "../balances" } +sp-storage = { version = "3.0.0", path = "../../primitives/storage" } hex-literal = "0.3.1" [features] diff --git a/frame/vesting/src/benchmarking.rs b/frame/vesting/src/benchmarking.rs index f65011050422b..937f2b033d847 100644 --- a/frame/vesting/src/benchmarking.rs +++ b/frame/vesting/src/benchmarking.rs @@ -22,7 +22,7 @@ use super::*; use frame_system::{RawOrigin, Module as System}; -use frame_benchmarking::{benchmarks, account, whitelisted_caller}; +use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use sp_runtime::traits::Bounded; use crate::Module as Vesting; @@ -224,21 +224,8 @@ benchmarks! { } } -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::{ExtBuilder, Test}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - ExtBuilder::default().existential_deposit(256).build().execute_with(|| { - assert_ok!(test_benchmark_vest_locked::()); - assert_ok!(test_benchmark_vest_unlocked::()); - assert_ok!(test_benchmark_vest_other_locked::()); - assert_ok!(test_benchmark_vest_other_unlocked::()); - assert_ok!(test_benchmark_vested_transfer::()); - assert_ok!(test_benchmark_force_vested_transfer::()); - }); - } -} +impl_benchmark_test_suite!( + Vesting, + crate::tests::ExtBuilder::default().existential_deposit(256).build(), + crate::tests::Test, +); diff --git a/primitives/allocator/Cargo.toml b/primitives/allocator/Cargo.toml index 4fef71db75407..1c38cbbb9c26e 100644 --- a/primitives/allocator/Cargo.toml +++ b/primitives/allocator/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-allocator" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,9 +14,9 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-std = { version = "2.0.0", path = "../std", default-features = false } -sp-core = { version = "2.0.0", path = "../core", default-features = false } -sp-wasm-interface = { version = "2.0.0", path = "../wasm-interface", default-features = false } +sp-std = { version = "3.0.0", path = "../std", default-features = false } +sp-core = { version = "3.0.0", path = "../core", default-features = false } +sp-wasm-interface = { version = "3.0.0", path = "../wasm-interface", default-features = false } log = { version = "0.4.11", optional = true } thiserror = { version = "1.0.21", optional = true } diff --git a/primitives/api/Cargo.toml b/primitives/api/Cargo.toml index c1effc523fcb6..20987035ef2f0 100644 --- a/primitives/api/Cargo.toml +++ b/primitives/api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-api" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,13 +13,13 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -sp-api-proc-macro = { version = "2.0.0", path = "proc-macro" } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } -sp-version = { version = "2.0.0", default-features = false, path = "../version" } -sp-state-machine = { version = "0.8.0", optional = true, path = "../state-machine" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +sp-api-proc-macro = { version = "3.0.0", path = "proc-macro" } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } +sp-version = { version = "3.0.0", default-features = false, path = "../version" } +sp-state-machine = { version = "0.9.0", optional = true, path = "../state-machine" } hash-db = { version = "0.15.2", optional = true } thiserror = { version = "1.0.21", optional = true } diff --git a/primitives/api/proc-macro/Cargo.toml b/primitives/api/proc-macro/Cargo.toml index 98ca45081c1e3..450ce64b2b6c1 100644 --- a/primitives/api/proc-macro/Cargo.toml +++ b/primitives/api/proc-macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-api-proc-macro" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/primitives/api/proc-macro/src/decl_runtime_apis.rs b/primitives/api/proc-macro/src/decl_runtime_apis.rs index 62ea5ed32b5b1..9fd5baba877dc 100644 --- a/primitives/api/proc-macro/src/decl_runtime_apis.rs +++ b/primitives/api/proc-macro/src/decl_runtime_apis.rs @@ -187,14 +187,15 @@ fn generate_native_call_generators(decl: &ItemTrait) -> Result { result.push(quote!( #[cfg(any(feature = "std", test))] fn convert_between_block_types - ( - input: &I, error_desc: &'static str, - ) -> std::result::Result + #crate_::ApiError>( + input: &I, + map_error: F, + ) -> std::result::Result { ::decode_with_depth_limit( #crate_::MAX_EXTRINSIC_DEPTH, &mut &#crate_::Encode::encode(input)[..], - ).map_err(|e| format!("{} {}", error_desc, e.what())) + ).map_err(map_error) } )); @@ -202,19 +203,26 @@ fn generate_native_call_generators(decl: &ItemTrait) -> Result { for fn_ in fns { let params = extract_parameter_names_types_and_borrows(&fn_, AllowSelfRefInParameters::No)?; let trait_fn_name = &fn_.ident; + let function_name_str = fn_.ident.to_string(); let fn_name = generate_native_call_generator_fn_name(&fn_.ident); let output = return_type_replace_block_with_node_block(fn_.output.clone()); let output_ty = return_type_extract_type(&output); - let output = quote!( std::result::Result<#output_ty, String> ); + let output = quote!( std::result::Result<#output_ty, #crate_::ApiError> ); // Every type that is using the `Block` generic parameter, we need to encode/decode, // to make it compatible between the runtime/node. let conversions = params.iter().filter(|v| type_is_using_block(&v.1)).map(|(n, t, _)| { - let name_str = format!( - "Could not convert parameter `{}` between node and runtime:", quote!(#n) - ); + let param_name = quote!(#n).to_string(); + quote!( - let #n: #t = convert_between_block_types(&#n, #name_str)?; + let #n: #t = convert_between_block_types( + &#n, + |e| #crate_::ApiError::FailedToConvertParameter { + function: #function_name_str, + parameter: #param_name, + error: e, + }, + )?; ) }); // Same as for the input types, we need to check if we also need to convert the output, @@ -223,7 +231,10 @@ fn generate_native_call_generators(decl: &ItemTrait) -> Result { quote!( convert_between_block_types( &res, - "Could not convert return value from runtime to node!" + |e| #crate_::ApiError::FailedToConvertReturnValue { + function: #function_name_str, + error: e, + }, ) ) } else { @@ -399,10 +410,10 @@ fn generate_call_api_at_calls(decl: &ItemTrait) -> Result { #[cfg(any(feature = "std", test))] pub fn #fn_name< R: #crate_::Encode + #crate_::Decode + PartialEq, - NC: FnOnce() -> std::result::Result + std::panic::UnwindSafe, + NC: FnOnce() -> std::result::Result + std::panic::UnwindSafe, Block: #crate_::BlockT, T: #crate_::CallApiAt, - C: #crate_::Core, + C: #crate_::Core, >( call_runtime_at: &T, core_api: &C, @@ -416,7 +427,7 @@ fn generate_call_api_at_calls(decl: &ItemTrait) -> Result { native_call: Option, context: #crate_::ExecutionContext, recorder: &Option<#crate_::ProofRecorder>, - ) -> std::result::Result<#crate_::NativeOrEncoded, T::Error> { + ) -> std::result::Result<#crate_::NativeOrEncoded, #crate_::ApiError> { let version = call_runtime_at.runtime_version_at(at)?; use #crate_::InitializeBlock; let initialize_block = if #skip_initialize_block { @@ -621,7 +632,7 @@ impl<'a> ToClientSideDecl<'a> { context: #crate_::ExecutionContext, params: Option<( #( #param_types ),* )>, params_encoded: Vec, - ) -> std::result::Result<#crate_::NativeOrEncoded<#ret_type>, Self::Error>; + ) -> std::result::Result<#crate_::NativeOrEncoded<#ret_type>, #crate_::ApiError>; } ) } @@ -647,7 +658,7 @@ impl<'a> ToClientSideDecl<'a> { let params2 = params.clone(); let ret_type = return_type_extract_type(&method.sig.output); - fold_fn_decl_for_client_side(&mut method.sig, &self.block_id); + fold_fn_decl_for_client_side(&mut method.sig, &self.block_id, &self.crate_); let name_impl = generate_method_runtime_api_impl_name(&self.trait_, &method.sig.ident); let crate_ = self.crate_; @@ -705,7 +716,12 @@ impl<'a> ToClientSideDecl<'a> { }, #crate_::NativeOrEncoded::Encoded(r) => { <#ret_type as #crate_::Decode>::decode(&mut &r[..]) - .map_err(|err| { #crate_::ApiError::new(#function_name, err).into() }) + .map_err(|err| + #crate_::ApiError::FailedToDecodeReturnValue { + function: #function_name, + error: err, + } + ) } } ) @@ -728,12 +744,10 @@ impl<'a> Fold for ToClientSideDecl<'a> { if is_core_trait { // Add all the supertraits we want to have for `Core`. - let crate_ = &self.crate_; input.supertraits = parse_quote!( 'static + Send + Sync - + #crate_::ApiErrorExt ); } else { // Add the `Core` runtime api as super trait. @@ -803,12 +817,12 @@ fn generate_runtime_info_impl(trait_: &ItemTrait, version: u64) -> TokenStream { let bounds = &t.bounds; quote! { #ident #colon_token #bounds } - }).chain(std::iter::once(quote! { __Sr_Api_Error__ })); + }); let ty_generics = trait_.generics.type_params().map(|t| { let ident = &t.ident; quote! { #ident } - }).chain(std::iter::once(quote! { Error = __Sr_Api_Error__ })); + }); quote!( #[cfg(any(feature = "std", test))] diff --git a/primitives/api/proc-macro/src/impl_runtime_apis.rs b/primitives/api/proc-macro/src/impl_runtime_apis.rs index 8a057383efaaf..51bbe1c73ac88 100644 --- a/primitives/api/proc-macro/src/impl_runtime_apis.rs +++ b/primitives/api/proc-macro/src/impl_runtime_apis.rs @@ -86,7 +86,7 @@ fn generate_impl_call( &#input, ) { Ok(res) => res, - Err(e) => panic!("Bad input data provided to {}: {}", #fn_name_str, e.what()), + Err(e) => panic!("Bad input data provided to {}: {}", #fn_name_str, e), }; #[allow(deprecated)] @@ -233,16 +233,6 @@ fn generate_runtime_api_base_structures() -> Result { C::StateBackend: #crate_::StateBackend<#crate_::HashFor>, {} - #[cfg(any(feature = "std", test))] - impl> #crate_::ApiErrorExt - for RuntimeApiImpl - where - // Rust bug: https://github.com/rust-lang/rust/issues/24159 - C::StateBackend: #crate_::StateBackend<#crate_::HashFor>, - { - type Error = C::Error; - } - #[cfg(any(feature = "std", test))] impl> #crate_::ApiExt for RuntimeApiImpl @@ -269,16 +259,20 @@ fn generate_runtime_api_base_structures() -> Result { fn has_api( &self, at: &#crate_::BlockId, - ) -> std::result::Result where Self: Sized { - self.call.runtime_version_at(at).map(|v| v.has_api_with(&A::ID, |v| v == A::VERSION)) + ) -> std::result::Result where Self: Sized { + self.call + .runtime_version_at(at) + .map(|v| v.has_api_with(&A::ID, |v| v == A::VERSION)) } fn has_api_with bool>( &self, at: &#crate_::BlockId, pred: P, - ) -> std::result::Result where Self: Sized { - self.call.runtime_version_at(at).map(|v| v.has_api_with(&A::ID, pred)) + ) -> std::result::Result where Self: Sized { + self.call + .runtime_version_at(at) + .map(|v| v.has_api_with(&A::ID, pred)) } fn record_proof(&mut self) { @@ -306,7 +300,7 @@ fn generate_runtime_api_base_structures() -> Result { >>, parent_hash: Block::Hash, ) -> std::result::Result< - #crate_::StorageChanges, + #crate_::StorageChanges, String > where Self: Sized { self.initialized_block.borrow_mut().take(); @@ -513,7 +507,7 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> { // Generate the correct return type. input.sig.output = parse_quote!( - -> std::result::Result<#crate_::NativeOrEncoded<#ret_type>, RuntimeApiImplCall::Error> + -> std::result::Result<#crate_::NativeOrEncoded<#ret_type>, #crate_::ApiError> ); // Generate the new method implementation that calls into the runtime. @@ -554,7 +548,7 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> { ) }; - let mut input = fold::fold_impl_item_method(self, input); + let mut input = fold::fold_impl_item_method(self, input); // We need to set the block, after we modified the rest of the ast, otherwise we would // modify our generated block as well. input.block = block; diff --git a/primitives/api/proc-macro/src/mock_impl_runtime_apis.rs b/primitives/api/proc-macro/src/mock_impl_runtime_apis.rs index c6ff98c0f1dc5..62a03a59baacd 100644 --- a/primitives/api/proc-macro/src/mock_impl_runtime_apis.rs +++ b/primitives/api/proc-macro/src/mock_impl_runtime_apis.rs @@ -27,7 +27,7 @@ use proc_macro2::{Span, TokenStream}; use quote::{quote, quote_spanned}; use syn::{ - spanned::Spanned, parse_macro_input, Ident, Type, ItemImpl, ImplItem, TypePath, parse_quote, + spanned::Spanned, parse_macro_input, Ident, Type, ItemImpl, TypePath, parse_quote, parse::{Parse, ParseStream, Result, Error}, fold::{self, Fold}, Attribute, Pat, }; @@ -61,29 +61,14 @@ impl Parse for RuntimeApiImpls { } } -/// Implement the `ApiExt` trait, `ApiErrorExt` trait and the `Core` runtime api. +/// Implement the `ApiExt` trait and the `Core` runtime api. fn implement_common_api_traits( - error_type: Option, block_type: TypePath, self_ty: Type, ) -> Result { let crate_ = generate_crate_access(HIDDEN_INCLUDES_ID); - let error_type = error_type - .map(|e| quote!(#e)) - .unwrap_or_else(|| quote!( #crate_::ApiError ) ); - - // Quote using the span from `error_type` to generate nice error messages when the type is - // not implementing a trait or similar. - let api_error_ext = quote_spanned! { error_type.span() => - impl #crate_::ApiErrorExt for #self_ty { - type Error = #error_type; - } - }; - Ok(quote!( - #api_error_ext - impl #crate_::ApiExt<#block_type> for #self_ty { type StateBackend = #crate_::InMemoryBackend<#crate_::HashFor<#block_type>>; @@ -97,7 +82,7 @@ fn implement_common_api_traits( fn has_api( &self, _: &#crate_::BlockId<#block_type>, - ) -> std::result::Result where Self: Sized { + ) -> std::result::Result where Self: Sized { Ok(true) } @@ -105,7 +90,7 @@ fn implement_common_api_traits( &self, _: &#crate_::BlockId<#block_type>, pred: P, - ) -> std::result::Result where Self: Sized { + ) -> std::result::Result where Self: Sized { Ok(pred(A::VERSION)) } @@ -140,7 +125,7 @@ fn implement_common_api_traits( _: #crate_::ExecutionContext, _: Option<()>, _: Vec, - ) -> std::result::Result<#crate_::NativeOrEncoded<#crate_::RuntimeVersion>, #error_type> { + ) -> std::result::Result<#crate_::NativeOrEncoded<#crate_::RuntimeVersion>, #crate_::ApiError> { unimplemented!("Not required for testing!") } @@ -150,7 +135,7 @@ fn implement_common_api_traits( _: #crate_::ExecutionContext, _: Option<#block_type>, _: Vec, - ) -> std::result::Result<#crate_::NativeOrEncoded<()>, #error_type> { + ) -> std::result::Result<#crate_::NativeOrEncoded<()>, #crate_::ApiError> { unimplemented!("Not required for testing!") } @@ -160,7 +145,7 @@ fn implement_common_api_traits( _: #crate_::ExecutionContext, _: Option<&<#block_type as #crate_::BlockT>::Header>, _: Vec, - ) -> std::result::Result<#crate_::NativeOrEncoded<()>, #error_type> { + ) -> std::result::Result<#crate_::NativeOrEncoded<()>, #crate_::ApiError> { unimplemented!("Not required for testing!") } } @@ -230,9 +215,6 @@ struct FoldRuntimeApiImpl<'a> { block_type: &'a TypePath, /// The identifier of the trait being implemented. impl_trait: &'a Ident, - /// Stores the error type that is being found in the trait implementation as associated type - /// with the name `Error`. - error_type: &'a mut Option, } impl<'a> Fold for FoldRuntimeApiImpl<'a> { @@ -300,7 +282,7 @@ impl<'a> Fold for FoldRuntimeApiImpl<'a> { // Generate the correct return type. input.sig.output = parse_quote!( - -> std::result::Result<#crate_::NativeOrEncoded<#ret_type>, Self::Error> + -> std::result::Result<#crate_::NativeOrEncoded<#ret_type>, #crate_::ApiError> ); } @@ -336,51 +318,12 @@ impl<'a> Fold for FoldRuntimeApiImpl<'a> { input.block = block; input } - - fn fold_impl_item(&mut self, input: ImplItem) -> ImplItem { - match input { - ImplItem::Type(ty) => { - if ty.ident == "Error" { - if let Some(error_type) = self.error_type { - if *error_type != ty.ty { - let mut error = Error::new( - ty.span(), - "Error type can not change between runtime apis", - ); - let error_first = Error::new( - error_type.span(), - "First error type was declared here." - ); - - error.combine(error_first); - - ImplItem::Verbatim(error.to_compile_error()) - } else { - ImplItem::Verbatim(Default::default()) - } - } else { - *self.error_type = Some(ty.ty); - ImplItem::Verbatim(Default::default()) - } - } else { - let error = Error::new( - ty.span(), - "Only associated type with name `Error` is allowed", - ); - ImplItem::Verbatim(error.to_compile_error()) - } - }, - o => fold::fold_impl_item(self, o), - } - } } /// Result of [`generate_runtime_api_impls`]. struct GeneratedRuntimeApiImpls { /// All the runtime api implementations. impls: TokenStream, - /// The error type that should be used by the runtime apis. - error_type: Option, /// The block type that is being used by the runtime apis. block_type: TypePath, /// The type the traits are implemented for. @@ -393,7 +336,6 @@ struct GeneratedRuntimeApiImpls { /// extracts the error type, self type and the block type. fn generate_runtime_api_impls(impls: &[ItemImpl]) -> Result { let mut result = Vec::with_capacity(impls.len()); - let mut error_type = None; let mut global_block_type: Option = None; let mut self_ty: Option> = None; @@ -451,7 +393,6 @@ fn generate_runtime_api_impls(impls: &[ItemImpl]) -> Result Result proc_macro fn mock_impl_runtime_apis_impl_inner(api_impls: &[ItemImpl]) -> Result { let hidden_includes = generate_hidden_includes(HIDDEN_INCLUDES_ID); - let GeneratedRuntimeApiImpls { impls, error_type, block_type, self_ty } = + let GeneratedRuntimeApiImpls { impls, block_type, self_ty } = generate_runtime_api_impls(api_impls)?; - let api_traits = implement_common_api_traits(error_type, block_type, self_ty)?; + let api_traits = implement_common_api_traits(block_type, self_ty)?; Ok(quote!( #hidden_includes diff --git a/primitives/api/proc-macro/src/utils.rs b/primitives/api/proc-macro/src/utils.rs index dbe7c723af0b6..a7a6d352058c9 100644 --- a/primitives/api/proc-macro/src/utils.rs +++ b/primitives/api/proc-macro/src/utils.rs @@ -99,6 +99,7 @@ pub fn replace_wild_card_parameter_names(input: &mut Signature) { pub fn fold_fn_decl_for_client_side( input: &mut Signature, block_id: &TokenStream, + crate_: &TokenStream, ) { replace_wild_card_parameter_names(input); @@ -109,7 +110,7 @@ pub fn fold_fn_decl_for_client_side( // Wrap the output in a `Result` input.output = { let ty = return_type_extract_type(&input.output); - parse_quote!( -> std::result::Result<#ty, Self::Error> ) + parse_quote!( -> std::result::Result<#ty, #crate_::ApiError> ) }; } diff --git a/primitives/api/src/lib.rs b/primitives/api/src/lib.rs index 8ce447c0d3667..592b20b62a77d 100644 --- a/primitives/api/src/lib.rs +++ b/primitives/api/src/lib.rs @@ -67,7 +67,7 @@ pub use sp_std::{slice, mem}; #[cfg(feature = "std")] use sp_std::result; #[doc(hidden)] -pub use codec::{Encode, Decode, DecodeLimit}; +pub use codec::{Encode, Decode, DecodeLimit, self}; use sp_core::OpaqueMetadata; #[cfg(feature = "std")] use std::{panic::UnwindSafe, cell::RefCell}; @@ -246,8 +246,8 @@ pub use sp_api_proc_macro::impl_runtime_apis; /// and the error type can be specified as associated type. If no error type is specified [`String`] /// is used as error type. /// -/// Besides implementing the given traits, the [`Core`](sp_api::Core), [`ApiExt`](sp_api::ApiExt) -/// and [`ApiErrorExt`](sp_api::ApiErrorExt) are implemented automatically. +/// Besides implementing the given traits, the [`Core`](sp_api::Core) and [`ApiExt`](sp_api::ApiExt) +/// are implemented automatically. /// /// # Example /// @@ -284,11 +284,6 @@ pub use sp_api_proc_macro::impl_runtime_apis; /// } /// /// impl BlockBuilder for MockApi { -/// /// Sets the error type that is being used by the mock implementation. -/// /// The error type is used by all runtime apis. It is only required to -/// /// be specified in one trait implementation. -/// type Error = sp_api::ApiError; -/// /// fn build_block() -> Block { /// unimplemented!("Not Required in tests") /// } @@ -331,15 +326,14 @@ pub use sp_api_proc_macro::impl_runtime_apis; /// /// sp_api::mock_impl_runtime_apis! { /// impl Balance for MockApi { -/// type Error = sp_api::ApiError; /// #[advanced] -/// fn get_balance(&self, at: &BlockId) -> Result, Self::Error> { +/// fn get_balance(&self, at: &BlockId) -> Result, sp_api::ApiError> { /// println!("Being called at: {}", at); /// /// Ok(self.balance.into()) /// } /// #[advanced] -/// fn set_balance(at: &BlockId, val: u64) -> Result, Self::Error> { +/// fn set_balance(at: &BlockId, val: u64) -> Result, sp_api::ApiError> { /// if let BlockId::Number(1) = at { /// println!("Being called to set balance to: {}", val); /// } @@ -393,46 +387,35 @@ pub trait ConstructRuntimeApi> { } /// An error describing which API call failed. -#[cfg_attr(feature = "std", derive(Debug, thiserror::Error, Eq, PartialEq))] -#[cfg_attr(feature = "std", error("Failed to execute API call {tag}"))] -#[cfg(feature = "std")] -pub struct ApiError { - tag: &'static str, - #[source] - error: codec::Error, -} - #[cfg(feature = "std")] -impl From<(&'static str, codec::Error)> for ApiError { - fn from((tag, error): (&'static str, codec::Error)) -> Self { - Self { - tag, - error, - } - } -} - -#[cfg(feature = "std")] -impl ApiError { - pub fn new(tag: &'static str, error: codec::Error) -> Self { - Self { - tag, - error, - } - } -} - -/// Extends the runtime api traits with an associated error type. This trait is given as super -/// trait to every runtime api trait. -#[cfg(feature = "std")] -pub trait ApiErrorExt { - /// Error type used by the runtime apis. - type Error: std::fmt::Debug + From; +#[derive(Debug, thiserror::Error)] +pub enum ApiError { + #[error("Failed to decode return value of {function}")] + FailedToDecodeReturnValue { + function: &'static str, + #[source] + error: codec::Error, + }, + #[error("Failed to convert return value from runtime to node of {function}")] + FailedToConvertReturnValue { + function: &'static str, + #[source] + error: codec::Error, + }, + #[error("Failed to convert parameter `{parameter}` from node to runtime of {function}")] + FailedToConvertParameter { + function: &'static str, + parameter: &'static str, + #[source] + error: codec::Error, + }, + #[error(transparent)] + Application(#[from] Box), } /// Extends the runtime api implementation with some common functionality. #[cfg(feature = "std")] -pub trait ApiExt: ApiErrorExt { +pub trait ApiExt { /// The state backend that is used to store the block states. type StateBackend: StateBackend>; @@ -450,14 +433,14 @@ pub trait ApiExt: ApiErrorExt { fn has_api( &self, at: &BlockId, - ) -> Result where Self: Sized; + ) -> Result where Self: Sized; /// Check if the given api is implemented and the version passes a predicate. fn has_api_with bool>( &self, at: &BlockId, pred: P, - ) -> Result where Self: Sized; + ) -> Result where Self: Sized; /// Start recording all accessed trie nodes for generating proofs. fn record_proof(&mut self); @@ -478,7 +461,10 @@ pub trait ApiExt: ApiErrorExt { backend: &Self::StateBackend, changes_trie_state: Option<&ChangesTrieState, NumberFor>>, parent_hash: Block::Hash, - ) -> Result, String> where Self: Sized; + ) -> Result< + StorageChanges, + String + > where Self: Sized; } /// Before calling any runtime api function, the runtime need to be initialized @@ -533,9 +519,6 @@ pub struct CallApiAtParams<'a, Block: BlockT, C, NC, Backend: StateBackend { - /// Error type used by the implementation. - type Error: std::fmt::Debug + From; - /// The state backend that is used to store the block states. type StateBackend: StateBackend>; @@ -544,15 +527,18 @@ pub trait CallApiAt { fn call_api_at< 'a, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, - C: Core, + NC: FnOnce() -> result::Result + UnwindSafe, + C: Core, >( &self, params: CallApiAtParams<'a, Block, C, NC, Self::StateBackend>, - ) -> Result, Self::Error>; + ) -> Result, ApiError>; /// Returns the runtime version at the given block. - fn runtime_version_at(&self, at: &BlockId) -> Result; + fn runtime_version_at( + &self, + at: &BlockId, + ) -> Result; } /// Auxiliary wrapper that holds an api instance and binds it to the given lifetime. @@ -605,10 +591,6 @@ pub trait RuntimeApiInfo { const VERSION: u32; } -/// Extracts the `Api::Error` for a type that provides a runtime api. -#[cfg(feature = "std")] -pub type ApiErrorFor = <>::Api as ApiErrorExt>::Error; - #[derive(codec::Encode, codec::Decode)] pub struct OldRuntimeVersion { pub spec_name: RuntimeString, diff --git a/primitives/api/test/Cargo.toml b/primitives/api/test/Cargo.toml index 310840d1ca9c7..e8f06aaf20e1b 100644 --- a/primitives/api/test/Cargo.toml +++ b/primitives/api/test/Cargo.toml @@ -12,22 +12,22 @@ repository = "https://github.com/paritytech/substrate/" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-api = { version = "2.0.0", path = "../" } +sp-api = { version = "3.0.0", path = "../" } substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" } -sp-version = { version = "2.0.0", path = "../../version" } -sp-runtime = { version = "2.0.0", path = "../../runtime" } -sp-blockchain = { version = "2.0.0", path = "../../blockchain" } -sp-consensus = { version = "0.8.0", path = "../../consensus/common" } -sc-block-builder = { version = "0.8.0", path = "../../../client/block-builder" } -codec = { package = "parity-scale-codec", version = "1.3.6" } -sp-state-machine = { version = "0.8.0", path = "../../state-machine" } +sp-version = { version = "3.0.0", path = "../../version" } +sp-runtime = { version = "3.0.0", path = "../../runtime" } +sp-blockchain = { version = "3.0.0", path = "../../blockchain" } +sp-consensus = { version = "0.9.0", path = "../../consensus/common" } +sc-block-builder = { version = "0.9.0", path = "../../../client/block-builder" } +codec = { package = "parity-scale-codec", version = "2.0.0" } +sp-state-machine = { version = "0.9.0", path = "../../state-machine" } trybuild = "1.0.38" rustversion = "1.0.0" [dev-dependencies] criterion = "0.3.0" substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" } -sp-core = { version = "2.0.0", path = "../../core" } +sp-core = { version = "3.0.0", path = "../../core" } [[bench]] name = "bench" diff --git a/primitives/api/test/tests/decl_and_impl.rs b/primitives/api/test/tests/decl_and_impl.rs index 134ee5085658a..1f7ccf2712d63 100644 --- a/primitives/api/test/tests/decl_and_impl.rs +++ b/primitives/api/test/tests/decl_and_impl.rs @@ -23,7 +23,6 @@ use sp_api::{ use sp_runtime::{traits::{GetNodeBlockType, Block as BlockT}, generic::BlockId}; use sp_core::NativeOrEncoded; use substrate_test_runtime_client::runtime::Block; -use sp_blockchain::Result; /// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType` /// trait are done by the `construct_runtime!` macro in a real runtime. @@ -105,7 +104,7 @@ mock_impl_runtime_apis! { #[advanced] fn same_name(_: &BlockId) -> - std::result::Result< + Result< NativeOrEncoded<()>, ApiError > @@ -115,7 +114,7 @@ mock_impl_runtime_apis! { #[advanced] fn wild_card(at: &BlockId, _: u32) -> - std::result::Result< + Result< NativeOrEncoded<()>, ApiError > @@ -124,7 +123,7 @@ mock_impl_runtime_apis! { // yeah Ok(().into()) } else { - Err(ApiError::new("MockApi", codec::Error::from("Ohh noooo"))) + Err((Box::from("Test error") as Box).into()) } } } @@ -143,33 +142,33 @@ type TestClient = substrate_test_runtime_client::client::Client< #[test] fn test_client_side_function_signature() { - let _test: fn(&RuntimeApiImpl, &BlockId, u64) -> Result<()> = + let _test: fn(&RuntimeApiImpl, &BlockId, u64) -> Result<(), ApiError> = RuntimeApiImpl::::test; let _something_with_block: - fn(&RuntimeApiImpl, &BlockId, Block) -> Result = + fn(&RuntimeApiImpl, &BlockId, Block) -> Result = RuntimeApiImpl::::something_with_block; #[allow(deprecated)] let _same_name_before_version_2: - fn(&RuntimeApiImpl, &BlockId) -> Result = + fn(&RuntimeApiImpl, &BlockId) -> Result = RuntimeApiImpl::::same_name_before_version_2; } #[test] fn check_runtime_api_info() { - assert_eq!(&Api::::ID, &runtime_decl_for_Api::ID); - assert_eq!(Api::::VERSION, runtime_decl_for_Api::VERSION); - assert_eq!(Api::::VERSION, 1); + assert_eq!(&Api::::ID, &runtime_decl_for_Api::ID); + assert_eq!(Api::::VERSION, runtime_decl_for_Api::VERSION); + assert_eq!(Api::::VERSION, 1); assert_eq!( - ApiWithCustomVersion::::VERSION, + ApiWithCustomVersion::::VERSION, runtime_decl_for_ApiWithCustomVersion::VERSION, ); assert_eq!( - &ApiWithCustomVersion::::ID, + &ApiWithCustomVersion::::ID, &runtime_decl_for_ApiWithCustomVersion::ID, ); - assert_eq!(ApiWithCustomVersion::::VERSION, 2); + assert_eq!(ApiWithCustomVersion::::VERSION, 2); } fn check_runtime_api_versions_contains() { @@ -178,9 +177,9 @@ fn check_runtime_api_versions_contains() { #[test] fn check_runtime_api_versions() { - check_runtime_api_versions_contains::>(); - check_runtime_api_versions_contains::>(); - check_runtime_api_versions_contains::>(); + check_runtime_api_versions_contains::>(); + check_runtime_api_versions_contains::>(); + check_runtime_api_versions_contains::>(); } #[test] @@ -188,9 +187,9 @@ fn mock_runtime_api_has_api() { let mock = MockApi { block: None }; assert!( - mock.has_api::>(&BlockId::Number(0)).unwrap(), + mock.has_api::>(&BlockId::Number(0)).unwrap(), ); - assert!(mock.has_api::>(&BlockId::Number(0)).unwrap()); + assert!(mock.has_api::>(&BlockId::Number(0)).unwrap()); } #[test] @@ -209,7 +208,7 @@ fn mock_runtime_api_works_with_advanced() { Api::::same_name(&mock, &BlockId::Number(0)).unwrap(); mock.wild_card(&BlockId::Number(1337), 1).unwrap(); assert_eq!( - ApiError::new("MockApi", ::codec::Error::from("Ohh noooo")), - mock.wild_card(&BlockId::Number(1336), 1).unwrap_err() + "Test error".to_string(), + mock.wild_card(&BlockId::Number(1336), 1).unwrap_err().to_string(), ); } diff --git a/primitives/api/test/tests/runtime_calls.rs b/primitives/api/test/tests/runtime_calls.rs index ec1a86d8379fc..94f419b1c44d5 100644 --- a/primitives/api/test/tests/runtime_calls.rs +++ b/primitives/api/test/tests/runtime_calls.rs @@ -50,10 +50,7 @@ fn calling_wasm_runtime_function() { } #[test] -#[should_panic( - expected = - "Could not convert parameter `param` between node and runtime: DecodeFails always fails" -)] +#[should_panic(expected = "FailedToConvertParameter { function: \"fail_convert_parameter\"")] fn calling_native_runtime_function_with_non_decodable_parameter() { let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible).build(); let runtime_api = client.runtime_api(); @@ -62,7 +59,7 @@ fn calling_native_runtime_function_with_non_decodable_parameter() { } #[test] -#[should_panic(expected = "Could not convert return value from runtime to node!")] +#[should_panic(expected = "FailedToConvertReturnValue { function: \"fail_convert_return_value\"")] fn calling_native_runtime_function_with_non_decodable_return_value() { let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible).build(); let runtime_api = client.runtime_api(); diff --git a/primitives/api/test/tests/ui/mock_only_error_associated_type.rs b/primitives/api/test/tests/ui/mock_only_error_associated_type.rs deleted file mode 100644 index bbd3c71c94017..0000000000000 --- a/primitives/api/test/tests/ui/mock_only_error_associated_type.rs +++ /dev/null @@ -1,19 +0,0 @@ -use substrate_test_runtime_client::runtime::Block; - -sp_api::decl_runtime_apis! { - pub trait Api { - fn test(data: u64); - } -} - -struct MockApi; - -sp_api::mock_impl_runtime_apis! { - impl Api for MockApi { - type OtherData = u32; - - fn test(data: u64) {} - } -} - -fn main() {} diff --git a/primitives/api/test/tests/ui/mock_only_error_associated_type.stderr b/primitives/api/test/tests/ui/mock_only_error_associated_type.stderr deleted file mode 100644 index beced70413bb0..0000000000000 --- a/primitives/api/test/tests/ui/mock_only_error_associated_type.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: Only associated type with name `Error` is allowed - --> $DIR/mock_only_error_associated_type.rs:13:3 - | -13 | type OtherData = u32; - | ^^^^ diff --git a/primitives/api/test/tests/ui/mock_only_one_error_type.rs b/primitives/api/test/tests/ui/mock_only_one_error_type.rs deleted file mode 100644 index 1c3f13dbb9bf1..0000000000000 --- a/primitives/api/test/tests/ui/mock_only_one_error_type.rs +++ /dev/null @@ -1,29 +0,0 @@ -use substrate_test_runtime_client::runtime::Block; - -sp_api::decl_runtime_apis! { - pub trait Api { - fn test(data: u64); - } - - pub trait Api2 { - fn test(data: u64); - } -} - -struct MockApi; - -sp_api::mock_impl_runtime_apis! { - impl Api for MockApi { - type Error = u32; - - fn test(data: u64) {} - } - - impl Api2 for MockApi { - type Error = u64; - - fn test(data: u64) {} - } -} - -fn main() {} diff --git a/primitives/api/test/tests/ui/mock_only_one_error_type.stderr b/primitives/api/test/tests/ui/mock_only_one_error_type.stderr deleted file mode 100644 index ab5b90af3ad1b..0000000000000 --- a/primitives/api/test/tests/ui/mock_only_one_error_type.stderr +++ /dev/null @@ -1,29 +0,0 @@ -error: Error type can not change between runtime apis - --> $DIR/mock_only_one_error_type.rs:23:3 - | -23 | type Error = u64; - | ^^^^ - -error: First error type was declared here. - --> $DIR/mock_only_one_error_type.rs:17:16 - | -17 | type Error = u32; - | ^^^ - -error[E0277]: the trait bound `u32: From` is not satisfied - --> $DIR/mock_only_one_error_type.rs:17:16 - | -17 | type Error = u32; - | ^^^ the trait `From` is not implemented for `u32` - | - ::: $WORKSPACE/primitives/api/src/lib.rs - | - | type Error: std::fmt::Debug + From; - | -------------- required by this bound in `sp_api_hidden_includes_DECL_RUNTIME_APIS::sp_api::ApiErrorExt::Error` - | - = help: the following implementations were found: - > - > - > - > - and 18 others diff --git a/primitives/application-crypto/Cargo.toml b/primitives/application-crypto/Cargo.toml index 8791ce4174bbf..fff289e9a1d86 100644 --- a/primitives/application-crypto/Cargo.toml +++ b/primitives/application-crypto/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-application-crypto" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" description = "Provides facilities for generating application specific crypto wrapper types." @@ -15,11 +15,11 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-core = { version = "2.0.0", default-features = false, path = "../core" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } serde = { version = "1.0.101", optional = true, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-io = { version = "2.0.0", default-features = false, path = "../io" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-io = { version = "3.0.0", default-features = false, path = "../io" } [features] default = [ "std" ] diff --git a/primitives/application-crypto/test/Cargo.toml b/primitives/application-crypto/test/Cargo.toml index f132e04deaa08..92a2ea8f3b8c8 100644 --- a/primitives/application-crypto/test/Cargo.toml +++ b/primitives/application-crypto/test/Cargo.toml @@ -13,9 +13,9 @@ repository = "https://github.com/paritytech/substrate/" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-core = { version = "2.0.0", default-features = false, path = "../../core" } -sp-keystore = { version = "0.8.0", path = "../../keystore", default-features = false } +sp-core = { version = "3.0.0", default-features = false, path = "../../core" } +sp-keystore = { version = "0.9.0", path = "../../keystore", default-features = false } substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" } -sp-runtime = { version = "2.0.0", path = "../../runtime" } -sp-api = { version = "2.0.0", path = "../../api" } -sp-application-crypto = { version = "2.0.0", path = "../" } +sp-runtime = { version = "3.0.0", path = "../../runtime" } +sp-api = { version = "3.0.0", path = "../../api" } +sp-application-crypto = { version = "3.0.0", path = "../" } diff --git a/primitives/arithmetic/Cargo.toml b/primitives/arithmetic/Cargo.toml index 5f951d8d248d1..76751cdee81bd 100644 --- a/primitives/arithmetic/Cargo.toml +++ b/primitives/arithmetic/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-arithmetic" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,18 +15,18 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } integer-sqrt = "0.1.2" num-traits = { version = "0.2.8", default-features = false } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } serde = { version = "1.0.101", optional = true, features = ["derive"] } -sp-debug-derive = { version = "2.0.0", default-features = false, path = "../debug-derive" } +sp-debug-derive = { version = "3.0.0", default-features = false, path = "../debug-derive" } [dev-dependencies] rand = "0.7.2" criterion = "0.3" serde_json = "1.0" -primitive-types = "0.8.0" +primitive-types = "0.9.0" [features] default = ["std"] diff --git a/primitives/arithmetic/fuzzer/Cargo.toml b/primitives/arithmetic/fuzzer/Cargo.toml index 74b9d782ef89d..2666dde9016a8 100644 --- a/primitives/arithmetic/fuzzer/Cargo.toml +++ b/primitives/arithmetic/fuzzer/Cargo.toml @@ -14,9 +14,9 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-arithmetic = { version = "2.0.0", path = ".." } +sp-arithmetic = { version = "3.0.0", path = ".." } honggfuzz = "0.5.49" -primitive-types = "0.8.0" +primitive-types = "0.9.0" num-bigint = "0.2" num-traits = "0.2" diff --git a/primitives/arithmetic/src/per_things.rs b/primitives/arithmetic/src/per_things.rs index 5c86e55c2f4e8..caaa4c33cd431 100644 --- a/primitives/arithmetic/src/per_things.rs +++ b/primitives/arithmetic/src/per_things.rs @@ -338,9 +338,9 @@ macro_rules! implement_per_thing { fn encode_as(&self) -> &Self::As { &self.0 } - fn decode_from(x: Self::As) -> Self { + fn decode_from(x: Self::As) -> Result { // Saturates if `x` is more than `$max` internally. - Self::from_parts(x) + Ok(Self::from_parts(x)) } } diff --git a/primitives/authority-discovery/Cargo.toml b/primitives/authority-discovery/Cargo.toml index 917f3eb024ae0..a32b13ca728d8 100644 --- a/primitives/authority-discovery/Cargo.toml +++ b/primitives/authority-discovery/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-authority-discovery" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] description = "Authority discovery primitives" edition = "2018" @@ -13,11 +13,11 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../application-crypto" } -codec = { package = "parity-scale-codec", default-features = false, version = "1.3.6" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-api = { version = "2.0.0", default-features = false, path = "../api" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../application-crypto" } +codec = { package = "parity-scale-codec", default-features = false, version = "2.0.0" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-api = { version = "3.0.0", default-features = false, path = "../api" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } [features] default = ["std"] diff --git a/primitives/authorship/Cargo.toml b/primitives/authorship/Cargo.toml index a63f75467ebf3..5455902fddc34 100644 --- a/primitives/authorship/Cargo.toml +++ b/primitives/authorship/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-authorship" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] description = "Authorship primitives" edition = "2018" @@ -13,10 +13,10 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-inherents = { version = "2.0.0", default-features = false, path = "../inherents" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +sp-inherents = { version = "3.0.0", default-features = false, path = "../inherents" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } [features] default = [ "std" ] diff --git a/primitives/block-builder/Cargo.toml b/primitives/block-builder/Cargo.toml index 5c6dad5ab7676..6081e872786ef 100644 --- a/primitives/block-builder/Cargo.toml +++ b/primitives/block-builder/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-block-builder" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,11 +13,11 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } -sp-api = { version = "2.0.0", default-features = false, path = "../api" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -sp-inherents = { version = "2.0.0", default-features = false, path = "../inherents" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } +sp-api = { version = "3.0.0", default-features = false, path = "../api" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +sp-inherents = { version = "3.0.0", default-features = false, path = "../inherents" } [features] default = [ "std" ] diff --git a/primitives/blockchain/Cargo.toml b/primitives/blockchain/Cargo.toml index 7d2d64de85e7a..c37686c0df73c 100644 --- a/primitives/blockchain/Cargo.toml +++ b/primitives/blockchain/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-blockchain" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,13 +15,13 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] log = "0.4.11" -lru = "0.6.1" +lru = "0.6.5" parking_lot = "0.11.1" thiserror = "1.0.21" futures = "0.3.9" -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-consensus = { version = "0.8.0", path = "../consensus/common" } -sp-runtime = { version = "2.0.0", path = "../runtime" } -sp-state-machine = { version = "0.8.0", path = "../state-machine" } -sp-database = { version = "2.0.0", path = "../database" } -sp-api = { version = "2.0.0", path = "../api" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-consensus = { version = "0.9.0", path = "../consensus/common" } +sp-runtime = { version = "3.0.0", path = "../runtime" } +sp-state-machine = { version = "0.9.0", path = "../state-machine" } +sp-database = { version = "3.0.0", path = "../database" } +sp-api = { version = "3.0.0", path = "../api" } diff --git a/primitives/blockchain/src/backend.rs b/primitives/blockchain/src/backend.rs index b50545b1a20af..b5efcfb02198a 100644 --- a/primitives/blockchain/src/backend.rs +++ b/primitives/blockchain/src/backend.rs @@ -215,6 +215,17 @@ pub trait Backend: HeaderBackend + HeaderMetadata Result::Extrinsic>>; + + /// Check if extrinsic exists. + fn have_extrinsic(&self, hash: &Block::Hash) -> Result { + Ok(self.extrinsic(hash)?.is_some()) + } } /// Provides access to the optional cache. diff --git a/primitives/blockchain/src/error.rs b/primitives/blockchain/src/error.rs index 6ed5fe1b335f3..58d08d06f049e 100644 --- a/primitives/blockchain/src/error.rs +++ b/primitives/blockchain/src/error.rs @@ -114,8 +114,8 @@ pub enum Error { #[error("Error decoding call result of {0}")] CallResultDecode(&'static str, #[source] CodecError), - #[error(transparent)] - RuntimeApiCodecError(#[from] ApiError), + #[error("Error at calling runtime api: {0}")] + RuntimeApiError(#[from] ApiError), #[error("Runtime :code missing in storage")] RuntimeCodeMissing, @@ -153,7 +153,6 @@ pub enum Error { #[error("Failed to get header for hash {0}")] MissingHeader(String), - #[error("State Database error: {0}")] StateDatabase(String), @@ -183,6 +182,15 @@ impl From> for Error { } } +impl From for ApiError { + fn from(err: Error) -> ApiError { + match err { + Error::RuntimeApiError(err) => err, + e => ApiError::Application(Box::new(e)), + } + } +} + impl Error { /// Chain a blockchain error. pub fn from_blockchain(e: Box) -> Self { diff --git a/primitives/chain-spec/Cargo.toml b/primitives/chain-spec/Cargo.toml index 52747dca94c91..ec3e731bb0e95 100644 --- a/primitives/chain-spec/Cargo.toml +++ b/primitives/chain-spec/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-chain-spec" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/primitives/consensus/aura/Cargo.toml b/primitives/consensus/aura/Cargo.toml index eed368e5c1d1c..100c323024952 100644 --- a/primitives/consensus/aura/Cargo.toml +++ b/primitives/consensus/aura/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-consensus-aura" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Primitives for Aura consensus" edition = "2018" @@ -13,13 +13,14 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../application-crypto" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -sp-std = { version = "2.0.0", default-features = false, path = "../../std" } -sp-api = { version = "2.0.0", default-features = false, path = "../../api" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../runtime" } -sp-inherents = { version = "2.0.0", default-features = false, path = "../../inherents" } -sp-timestamp = { version = "2.0.0", default-features = false, path = "../../timestamp" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../application-crypto" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +sp-std = { version = "3.0.0", default-features = false, path = "../../std" } +sp-api = { version = "3.0.0", default-features = false, path = "../../api" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../runtime" } +sp-inherents = { version = "3.0.0", default-features = false, path = "../../inherents" } +sp-timestamp = { version = "3.0.0", default-features = false, path = "../../timestamp" } +sp-consensus-slots = { version = "0.9.0", default-features = false, path = "../slots" } [features] default = ["std"] diff --git a/primitives/consensus/aura/src/inherents.rs b/primitives/consensus/aura/src/inherents.rs index e92775c501afe..35f686d93450c 100644 --- a/primitives/consensus/aura/src/inherents.rs +++ b/primitives/consensus/aura/src/inherents.rs @@ -26,7 +26,7 @@ use sp_inherents::{InherentDataProviders, ProvideInherentData}; pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"auraslot"; /// The type of the Aura inherent. -pub type InherentType = u64; +pub type InherentType = sp_consensus_slots::Slot; /// Auxiliary trait to extract Aura inherent data. pub trait AuraInherentData { @@ -48,6 +48,7 @@ impl AuraInherentData for InherentData { } /// Provides the slot duration inherent data for `Aura`. +// TODO: Remove in the future. https://github.com/paritytech/substrate/issues/8029 #[cfg(feature = "std")] pub struct InherentDataProvider { slot_duration: u64, @@ -87,8 +88,8 @@ impl ProvideInherentData for InherentDataProvider { use sp_timestamp::TimestampInherentData; let timestamp = inherent_data.timestamp_inherent_data()?; - let slot_num = timestamp / self.slot_duration; - inherent_data.put_data(INHERENT_IDENTIFIER, &slot_num) + let slot = timestamp / self.slot_duration; + inherent_data.put_data(INHERENT_IDENTIFIER, &slot) } fn error_to_string(&self, error: &[u8]) -> Option { diff --git a/primitives/consensus/aura/src/lib.rs b/primitives/consensus/aura/src/lib.rs index f3de26da90d38..95630fa7b5c6b 100644 --- a/primitives/consensus/aura/src/lib.rs +++ b/primitives/consensus/aura/src/lib.rs @@ -61,6 +61,8 @@ pub mod ed25519 { pub type AuthorityId = app_ed25519::Public; } +pub use sp_consensus_slots::Slot; + /// The `ConsensusEngineId` of AuRa. pub const AURA_ENGINE_ID: ConsensusEngineId = [b'a', b'u', b'r', b'a']; @@ -71,10 +73,10 @@ pub type AuthorityIndex = u32; #[derive(Decode, Encode)] pub enum ConsensusLog { /// The authorities have changed. - #[codec(index = "1")] + #[codec(index = 1)] AuthoritiesChange(Vec), /// Disable the authority with given index. - #[codec(index = "2")] + #[codec(index = 2)] OnDisabled(AuthorityIndex), } diff --git a/primitives/consensus/babe/Cargo.toml b/primitives/consensus/babe/Cargo.toml index 65321d324a695..fb02014eeef51 100644 --- a/primitives/consensus/babe/Cargo.toml +++ b/primitives/consensus/babe/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-consensus-babe" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Primitives for BABE consensus" edition = "2018" @@ -13,19 +13,19 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../application-crypto" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../application-crypto" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } merlin = { version = "2.0", default-features = false } -sp-std = { version = "2.0.0", default-features = false, path = "../../std" } -sp-api = { version = "2.0.0", default-features = false, path = "../../api" } -sp-consensus = { version = "0.8.0", optional = true, path = "../common" } -sp-consensus-slots = { version = "0.8.0", default-features = false, path = "../slots" } -sp-consensus-vrf = { version = "0.8.0", path = "../vrf", default-features = false } -sp-core = { version = "2.0.0", default-features = false, path = "../../core" } -sp-inherents = { version = "2.0.0", default-features = false, path = "../../inherents" } -sp-keystore = { version = "0.8.0", default-features = false, path = "../../keystore", optional = true } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../runtime" } -sp-timestamp = { version = "2.0.0", default-features = false, path = "../../timestamp" } +sp-std = { version = "3.0.0", default-features = false, path = "../../std" } +sp-api = { version = "3.0.0", default-features = false, path = "../../api" } +sp-consensus = { version = "0.9.0", optional = true, path = "../common" } +sp-consensus-slots = { version = "0.9.0", default-features = false, path = "../slots" } +sp-consensus-vrf = { version = "0.9.0", path = "../vrf", default-features = false } +sp-core = { version = "3.0.0", default-features = false, path = "../../core" } +sp-inherents = { version = "3.0.0", default-features = false, path = "../../inherents" } +sp-keystore = { version = "0.9.0", default-features = false, path = "../../keystore", optional = true } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../runtime" } +sp-timestamp = { version = "3.0.0", default-features = false, path = "../../timestamp" } [features] default = ["std"] diff --git a/primitives/consensus/babe/src/digests.rs b/primitives/consensus/babe/src/digests.rs index eeea747179a56..5a89e1fbc015f 100644 --- a/primitives/consensus/babe/src/digests.rs +++ b/primitives/consensus/babe/src/digests.rs @@ -19,7 +19,7 @@ use super::{ AllowedSlots, AuthorityId, AuthorityIndex, AuthoritySignature, BabeAuthorityWeight, - BabeEpochConfiguration, SlotNumber, BABE_ENGINE_ID, + BabeEpochConfiguration, Slot, BABE_ENGINE_ID, }; use codec::{Codec, Decode, Encode}; use sp_std::vec::Vec; @@ -32,8 +32,8 @@ use sp_consensus_vrf::schnorrkel::{Randomness, VRFOutput, VRFProof}; pub struct PrimaryPreDigest { /// Authority index pub authority_index: super::AuthorityIndex, - /// Slot number - pub slot_number: SlotNumber, + /// Slot + pub slot: Slot, /// VRF output pub vrf_output: VRFOutput, /// VRF proof @@ -50,8 +50,8 @@ pub struct SecondaryPlainPreDigest { /// it makes things easier for higher-level users of the chain data to /// be aware of the author of a secondary-slot block. pub authority_index: super::AuthorityIndex, - /// Slot number - pub slot_number: SlotNumber, + /// Slot + pub slot: Slot, } /// BABE secondary deterministic slot assignment with VRF outputs. @@ -59,8 +59,8 @@ pub struct SecondaryPlainPreDigest { pub struct SecondaryVRFPreDigest { /// Authority index pub authority_index: super::AuthorityIndex, - /// Slot number - pub slot_number: SlotNumber, + /// Slot + pub slot: Slot, /// VRF output pub vrf_output: VRFOutput, /// VRF proof @@ -73,13 +73,13 @@ pub struct SecondaryVRFPreDigest { #[derive(Clone, RuntimeDebug, Encode, Decode)] pub enum PreDigest { /// A primary VRF-based slot assignment. - #[codec(index = "1")] + #[codec(index = 1)] Primary(PrimaryPreDigest), /// A secondary deterministic slot assignment. - #[codec(index = "2")] + #[codec(index = 2)] SecondaryPlain(SecondaryPlainPreDigest), /// A secondary deterministic slot assignment with VRF outputs. - #[codec(index = "3")] + #[codec(index = 3)] SecondaryVRF(SecondaryVRFPreDigest), } @@ -93,12 +93,12 @@ impl PreDigest { } } - /// Returns the slot number of the pre digest. - pub fn slot_number(&self) -> SlotNumber { + /// Returns the slot of the pre digest. + pub fn slot(&self) -> Slot { match self { - PreDigest::Primary(primary) => primary.slot_number, - PreDigest::SecondaryPlain(secondary) => secondary.slot_number, - PreDigest::SecondaryVRF(secondary) => secondary.slot_number, + PreDigest::Primary(primary) => primary.slot, + PreDigest::SecondaryPlain(secondary) => secondary.slot, + PreDigest::SecondaryVRF(secondary) => secondary.slot, } } @@ -137,7 +137,7 @@ pub struct NextEpochDescriptor { #[derive(Decode, Encode, PartialEq, Eq, Clone, RuntimeDebug)] pub enum NextConfigDescriptor { /// Version 1. - #[codec(index = "1")] + #[codec(index = 1)] V1 { /// Value of `c` in `BabeEpochConfiguration`. c: (u64, u64), diff --git a/primitives/consensus/babe/src/inherents.rs b/primitives/consensus/babe/src/inherents.rs index 98104385c70f7..2f1a716114c5f 100644 --- a/primitives/consensus/babe/src/inherents.rs +++ b/primitives/consensus/babe/src/inherents.rs @@ -31,7 +31,7 @@ use sp_std::result::Result; pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"babeslot"; /// The type of the BABE inherent. -pub type InherentType = u64; +pub type InherentType = sp_consensus_slots::Slot; /// Auxiliary trait to extract BABE inherent data. pub trait BabeInherentData { /// Get BABE inherent data. @@ -52,6 +52,7 @@ impl BabeInherentData for InherentData { } /// Provides the slot duration inherent data for BABE. +// TODO: Remove in the future. https://github.com/paritytech/substrate/issues/8029 #[cfg(feature = "std")] pub struct InherentDataProvider { slot_duration: u64, @@ -82,8 +83,8 @@ impl ProvideInherentData for InherentDataProvider { fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> { let timestamp = inherent_data.timestamp_inherent_data()?; - let slot_number = timestamp / self.slot_duration; - inherent_data.put_data(INHERENT_IDENTIFIER, &slot_number) + let slot = timestamp / self.slot_duration; + inherent_data.put_data(INHERENT_IDENTIFIER, &slot) } fn error_to_string(&self, error: &[u8]) -> Option { diff --git a/primitives/consensus/babe/src/lib.rs b/primitives/consensus/babe/src/lib.rs index 6ecc21ab7a11e..6987796c114a0 100644 --- a/primitives/consensus/babe/src/lib.rs +++ b/primitives/consensus/babe/src/lib.rs @@ -76,8 +76,7 @@ pub const MEDIAN_ALGORITHM_CARDINALITY: usize = 1200; // arbitrary suggestion by /// The index of an authority. pub type AuthorityIndex = u32; -/// A slot number. -pub use sp_consensus_slots::SlotNumber; +pub use sp_consensus_slots::Slot; /// An equivocation proof for multiple block authorships on the same slot (i.e. double vote). pub type EquivocationProof = sp_consensus_slots::EquivocationProof; @@ -93,11 +92,11 @@ pub type BabeBlockWeight = u32; /// Make a VRF transcript from given randomness, slot number and epoch. pub fn make_transcript( randomness: &Randomness, - slot_number: u64, + slot: Slot, epoch: u64, ) -> Transcript { let mut transcript = Transcript::new(&BABE_ENGINE_ID); - transcript.append_u64(b"slot number", slot_number); + transcript.append_u64(b"slot number", *slot); transcript.append_u64(b"current epoch", epoch); transcript.append_message(b"chain randomness", &randomness[..]); transcript @@ -107,13 +106,13 @@ pub fn make_transcript( #[cfg(feature = "std")] pub fn make_transcript_data( randomness: &Randomness, - slot_number: u64, + slot: Slot, epoch: u64, ) -> VRFTranscriptData { VRFTranscriptData { label: &BABE_ENGINE_ID, items: vec![ - ("slot number", VRFTranscriptValue::U64(slot_number)), + ("slot number", VRFTranscriptValue::U64(*slot)), ("current epoch", VRFTranscriptValue::U64(epoch)), ("chain randomness", VRFTranscriptValue::Bytes(randomness.to_vec())), ] @@ -126,14 +125,14 @@ pub enum ConsensusLog { /// The epoch has changed. This provides information about the _next_ /// epoch - information about the _current_ epoch (i.e. the one we've just /// entered) should already be available earlier in the chain. - #[codec(index = "1")] + #[codec(index = 1)] NextEpochData(NextEpochDescriptor), /// Disable the authority with given index. - #[codec(index = "2")] + #[codec(index = 2)] OnDisabled(AuthorityIndex), /// The epoch has changed, and the epoch after the current one will /// enact different epoch configurations. - #[codec(index = "3")] + #[codec(index = 3)] NextConfigData(NextConfigDescriptor), } @@ -147,7 +146,7 @@ pub struct BabeGenesisConfigurationV1 { pub slot_duration: u64, /// The duration of epochs in slots. - pub epoch_length: SlotNumber, + pub epoch_length: u64, /// A constant value that is used in the threshold calculation formula. /// Expressed as a rational where the first member of the tuple is the @@ -195,7 +194,7 @@ pub struct BabeGenesisConfiguration { pub slot_duration: u64, /// The duration of epochs in slots. - pub epoch_length: SlotNumber, + pub epoch_length: u64, /// A constant value that is used in the threshold calculation formula. /// Expressed as a rational where the first member of the tuple is the @@ -303,8 +302,8 @@ where // both headers must be targetting the same slot and it must // be the same as the one in the proof. - if proof.slot_number != first_pre_digest.slot_number() || - first_pre_digest.slot_number() != second_pre_digest.slot_number() + if proof.slot != first_pre_digest.slot() || + first_pre_digest.slot() != second_pre_digest.slot() { return None; } @@ -356,9 +355,9 @@ pub struct Epoch { /// The epoch index. pub epoch_index: u64, /// The starting slot of the epoch. - pub start_slot: SlotNumber, + pub start_slot: Slot, /// The duration of this epoch. - pub duration: SlotNumber, + pub duration: u64, /// The authorities and their weights. pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>, /// Randomness for this epoch. @@ -376,8 +375,8 @@ sp_api::decl_runtime_apis! { #[changed_in(2)] fn configuration() -> BabeGenesisConfigurationV1; - /// Returns the slot number that started the current epoch. - fn current_epoch_start() -> SlotNumber; + /// Returns the slot that started the current epoch. + fn current_epoch_start() -> Slot; /// Returns information regarding the current epoch. fn current_epoch() -> Epoch; @@ -391,14 +390,14 @@ sp_api::decl_runtime_apis! { /// session historical module to prove that a given authority key is /// tied to a given staking identity during a specific session. Proofs /// of key ownership are necessary for submitting equivocation reports. - /// NOTE: even though the API takes a `slot_number` as parameter the current + /// NOTE: even though the API takes a `slot` as parameter the current /// implementations ignores this parameter and instead relies on this /// method being called at the correct block height, i.e. any point at /// which the epoch for the given slot is live on-chain. Future /// implementations will instead use indexed data through an offchain /// worker, not requiring older states to be available. fn generate_key_ownership_proof( - slot_number: SlotNumber, + slot: Slot, authority_id: AuthorityId, ) -> Option; diff --git a/primitives/consensus/common/Cargo.toml b/primitives/consensus/common/Cargo.toml index a9d2d92998a69..8c5ae968158a2 100644 --- a/primitives/consensus/common/Cargo.toml +++ b/primitives/consensus/common/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-consensus" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -16,23 +16,23 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] thiserror = "1.0.21" -libp2p = { version = "0.34.0", default-features = false } +libp2p = { version = "0.35.1", default-features = false } log = "0.4.8" -sp-core = { path= "../../core", version = "2.0.0"} -sp-inherents = { version = "2.0.0", path = "../../inherents" } -sp-state-machine = { version = "0.8.0", path = "../../state-machine" } +sp-core = { path= "../../core", version = "3.0.0"} +sp-inherents = { version = "3.0.0", path = "../../inherents" } +sp-state-machine = { version = "0.9.0", path = "../../state-machine" } futures = { version = "0.3.1", features = ["thread-pool"] } futures-timer = "3.0.1" -sp-std = { version = "2.0.0", path = "../../std" } -sp-version = { version = "2.0.0", path = "../../version" } -sp-runtime = { version = "2.0.0", path = "../../runtime" } -sp-utils = { version = "2.0.0", path = "../../utils" } -sp-trie = { version = "2.0.0", path = "../../trie" } -sp-api = { version = "2.0.0", path = "../../api" } -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } +sp-std = { version = "3.0.0", path = "../../std" } +sp-version = { version = "3.0.0", path = "../../version" } +sp-runtime = { version = "3.0.0", path = "../../runtime" } +sp-utils = { version = "3.0.0", path = "../../utils" } +sp-trie = { version = "3.0.0", path = "../../trie" } +sp-api = { version = "3.0.0", path = "../../api" } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } parking_lot = "0.11.1" serde = { version = "1.0", features = ["derive"] } -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.8.0"} +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0"} wasm-timer = "0.2.5" [dev-dependencies] diff --git a/primitives/consensus/common/src/block_import.rs b/primitives/consensus/common/src/block_import.rs index 41b5f391f65ca..00f84501dbb32 100644 --- a/primitives/consensus/common/src/block_import.rs +++ b/primitives/consensus/common/src/block_import.rs @@ -193,16 +193,21 @@ impl BlockImportParams { if let Some(hash) = self.post_hash { hash } else { - if self.post_digests.is_empty() { - self.header.hash() - } else { - let mut hdr = self.header.clone(); - for digest_item in &self.post_digests { - hdr.digest_mut().push(digest_item.clone()); - } - - hdr.hash() + self.post_header().hash() + } + } + + /// Get the post header. + pub fn post_header(&self) -> Block::Header { + if self.post_digests.is_empty() { + self.header.clone() + } else { + let mut hdr = self.header.clone(); + for digest_item in &self.post_digests { + hdr.digest_mut().push(digest_item.clone()); } + + hdr } } diff --git a/primitives/consensus/common/src/import_queue/basic_queue.rs b/primitives/consensus/common/src/import_queue/basic_queue.rs index 541c1ff0f4ed7..f1b42e1460e59 100644 --- a/primitives/consensus/common/src/import_queue/basic_queue.rs +++ b/primitives/consensus/common/src/import_queue/basic_queue.rs @@ -62,7 +62,7 @@ impl BasicQueue { verifier: V, block_import: BoxBlockImport, justification_import: Option>, - spawner: &impl sp_core::traits::SpawnNamed, + spawner: &impl sp_core::traits::SpawnEssentialNamed, prometheus_registry: Option<&Registry>, ) -> Self { let (result_sender, result_port) = buffered_link::buffered_link(); @@ -83,7 +83,7 @@ impl BasicQueue { metrics, ); - spawner.spawn_blocking("basic-block-import-worker", future.boxed()); + spawner.spawn_essential_blocking("basic-block-import-worker", future.boxed()); Self { justification_sender, @@ -164,7 +164,13 @@ async fn block_import_process( loop { let worker_messages::ImportBlocks(origin, blocks) = match block_import_receiver.next().await { Some(blocks) => blocks, - None => return, + None => { + log::debug!( + target: "block-import", + "Stopping block import because the import channel was closed!", + ); + return + }, }; let res = import_many_blocks( @@ -236,6 +242,10 @@ impl BlockImportWorker { // If the results sender is closed, that means that the import queue is shutting // down and we should end this future. if worker.result_sender.is_closed() { + log::debug!( + target: "block-import", + "Stopping block import because result channel was closed!", + ); return; } @@ -244,7 +254,13 @@ impl BlockImportWorker { match justification { Some(ImportJustification(who, hash, number, justification)) => worker.import_justification(who, hash, number, justification), - None => return, + None => { + log::debug!( + target: "block-import", + "Stopping block import because justification channel was closed!", + ); + return + }, } } diff --git a/primitives/consensus/pow/Cargo.toml b/primitives/consensus/pow/Cargo.toml index 15b37d6690ba1..850f0efe47ed3 100644 --- a/primitives/consensus/pow/Cargo.toml +++ b/primitives/consensus/pow/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-consensus-pow" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Primitives for Aura consensus" edition = "2018" @@ -13,11 +13,11 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-api = { version = "2.0.0", default-features = false, path = "../../api" } -sp-std = { version = "2.0.0", default-features = false, path = "../../std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../runtime" } -sp-core = { version = "2.0.0", default-features = false, path = "../../core" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +sp-api = { version = "3.0.0", default-features = false, path = "../../api" } +sp-std = { version = "3.0.0", default-features = false, path = "../../std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../runtime" } +sp-core = { version = "3.0.0", default-features = false, path = "../../core" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } [features] default = ["std"] diff --git a/primitives/consensus/slots/Cargo.toml b/primitives/consensus/slots/Cargo.toml index 11f81628b38a8..46dbaca1a6ad4 100644 --- a/primitives/consensus/slots/Cargo.toml +++ b/primitives/consensus/slots/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-consensus-slots" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Primitives for slots-based consensus" edition = "2018" @@ -13,12 +13,14 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../runtime" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../runtime" } +sp-arithmetic = { version = "3.0.0", default-features = false, path = "../../arithmetic" } [features] default = ["std"] std = [ "codec/std", "sp-runtime/std", + "sp-arithmetic/std", ] diff --git a/primitives/consensus/slots/src/lib.rs b/primitives/consensus/slots/src/lib.rs index 52df467c2910b..545d18af1f9be 100644 --- a/primitives/consensus/slots/src/lib.rs +++ b/primitives/consensus/slots/src/lib.rs @@ -21,8 +21,76 @@ use codec::{Decode, Encode}; -/// A slot number. -pub type SlotNumber = u64; +/// Unit type wrapper that represents a slot. +#[derive(Debug, Encode, Decode, Eq, Clone, Copy, Default, Ord)] +pub struct Slot(u64); + +impl core::ops::Deref for Slot { + type Target = u64; + + fn deref(&self) -> &u64 { + &self.0 + } +} + +impl core::ops::Add for Slot { + type Output = Self; + + fn add(self, other: Self) -> Self { + Self(self.0 + other.0) + } +} + +impl core::ops::Add for Slot { + type Output = Self; + + fn add(self, other: u64) -> Self { + Self(self.0 + other) + } +} + +impl + Copy> core::cmp::PartialEq for Slot { + fn eq(&self, eq: &T) -> bool { + self.0 == (*eq).into() + } +} + +impl + Copy> core::cmp::PartialOrd for Slot { + fn partial_cmp(&self, other: &T) -> Option { + self.0.partial_cmp(&(*other).into()) + } +} + +impl Slot { + /// Saturating addition. + pub fn saturating_add>(self, rhs: T) -> Self { + Self(self.0.saturating_add(rhs.into())) + } + + /// Saturating subtraction. + pub fn saturating_sub>(self, rhs: T) -> Self { + Self(self.0.saturating_sub(rhs.into())) + } +} + +#[cfg(feature = "std")] +impl std::fmt::Display for Slot { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl From for Slot { + fn from(slot: u64) -> Slot { + Slot(slot) + } +} + +impl From for u64 { + fn from(slot: Slot) -> u64 { + slot.0 + } +} /// Represents an equivocation proof. An equivocation happens when a validator /// produces more than one block on the same slot. The proof of equivocation @@ -32,8 +100,8 @@ pub type SlotNumber = u64; pub struct EquivocationProof { /// Returns the authority id of the equivocator. pub offender: Id, - /// The slot number at which the equivocation happened. - pub slot_number: SlotNumber, + /// The slot at which the equivocation happened. + pub slot: Slot, /// The first header involved in the equivocation. pub first_header: Header, /// The second header involved in the equivocation. diff --git a/primitives/consensus/vrf/Cargo.toml b/primitives/consensus/vrf/Cargo.toml index 58daab488c39b..15a9318cd4461 100644 --- a/primitives/consensus/vrf/Cargo.toml +++ b/primitives/consensus/vrf/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-consensus-vrf" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Primitives for VRF based consensus" edition = "2018" @@ -13,11 +13,11 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { version = "1.0.0", package = "parity-scale-codec", default-features = false } +codec = { version = "2.0.0", package = "parity-scale-codec", default-features = false } schnorrkel = { version = "0.9.1", features = ["preaudit_deprecated", "u64_backend"], default-features = false } -sp-std = { version = "2.0.0", path = "../../std", default-features = false } -sp-core = { version = "2.0.0", path = "../../core", default-features = false } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../runtime" } +sp-std = { version = "3.0.0", path = "../../std", default-features = false } +sp-core = { version = "3.0.0", path = "../../core", default-features = false } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../runtime" } [features] default = ["std"] diff --git a/primitives/core/Cargo.toml b/primitives/core/Cargo.toml index 2b27161b07510..3d9cf1287e051 100644 --- a/primitives/core/Cargo.toml +++ b/primitives/core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-core" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,12 +13,12 @@ documentation = "https://docs.rs/sp-core" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } log = { version = "0.4.11", default-features = false } serde = { version = "1.0.101", optional = true, features = ["derive"] } byteorder = { version = "1.3.2", default-features = false } -primitive-types = { version = "0.8.0", default-features = false, features = ["codec"] } +primitive-types = { version = "0.9.0", default-features = false, features = ["codec"] } impl-serde = { version = "0.3.0", optional = true } wasmi = { version = "0.6.2", optional = true } hash-db = { version = "0.15.2", default-features = false } @@ -33,10 +33,10 @@ zeroize = { version = "1.2.0", default-features = false } secrecy = { version = "0.7.0", default-features = false } lazy_static = { version = "1.4.0", default-features = false, optional = true } parking_lot = { version = "0.11.1", optional = true } -sp-debug-derive = { version = "2.0.0", path = "../debug-derive" } -sp-externalities = { version = "0.8.0", optional = true, path = "../externalities" } -sp-storage = { version = "2.0.0", default-features = false, path = "../storage" } -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } +sp-debug-derive = { version = "3.0.0", path = "../debug-derive" } +sp-externalities = { version = "0.9.0", optional = true, path = "../externalities" } +sp-storage = { version = "3.0.0", default-features = false, path = "../storage" } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } futures = { version = "0.3.1", optional = true } dyn-clonable = { version = "0.9.0", optional = true } thiserror = { version = "1.0.21", optional = true } @@ -52,10 +52,10 @@ twox-hash = { version = "1.5.0", default-features = false, optional = true } libsecp256k1 = { version = "0.3.2", default-features = false, features = ["hmac"], optional = true } merlin = { version = "2.0", default-features = false, optional = true } -sp-runtime-interface = { version = "2.0.0", default-features = false, path = "../runtime-interface" } +sp-runtime-interface = { version = "3.0.0", default-features = false, path = "../runtime-interface" } [dev-dependencies] -sp-serializer = { version = "2.0.0", path = "../serializer" } +sp-serializer = { version = "3.0.0", path = "../serializer" } pretty_assertions = "0.6.1" hex-literal = "0.3.1" rand = "0.7.2" diff --git a/primitives/core/src/crypto.rs b/primitives/core/src/crypto.rs index 2cc2d703c6177..0e5aca8f7ce1b 100644 --- a/primitives/core/src/crypto.rs +++ b/primitives/core/src/crypto.rs @@ -210,7 +210,7 @@ pub enum PublicError { BadBase58, /// Bad length. BadLength, - /// Unknown version. + /// Unknown identifier for the encoding. UnknownVersion, /// Invalid checksum. InvalidChecksum, @@ -218,11 +218,22 @@ pub enum PublicError { InvalidFormat, /// Invalid derivation path. InvalidPath, + /// Disallowed SS58 Address Format for this datatype. + FormatNotAllowed, } /// Key that can be encoded to/from SS58. +/// +/// See +/// for information on the codec. #[cfg(feature = "full_crypto")] pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default { + /// A format filterer, can be used to ensure that `from_ss58check` family only decode for + /// allowed identifiers. By default just refuses the two reserved identifiers. + fn format_is_allowed(f: Ss58AddressFormat) -> bool { + !matches!(f, Ss58AddressFormat::Reserved46 | Ss58AddressFormat::Reserved47) + } + /// Some if the string is a properly encoded SS58Check address. #[cfg(feature = "std")] fn from_ss58check(s: &str) -> Result { @@ -233,25 +244,46 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default { _ => Err(PublicError::UnknownVersion), }) } + /// Some if the string is a properly encoded SS58Check address. #[cfg(feature = "std")] fn from_ss58check_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { + const CHECKSUM_LEN: usize = 2; let mut res = Self::default(); - let len = res.as_mut().len(); - let d = s.from_base58().map_err(|_| PublicError::BadBase58)?; // failure here would be invalid encoding. - if d.len() != len + 3 { - // Invalid length. - return Err(PublicError::BadLength); - } - let ver = d[0].try_into().map_err(|_: ()| PublicError::UnknownVersion)?; - if d[len + 1..len + 3] != ss58hash(&d[0..len + 1]).as_bytes()[0..2] { + // Must decode to our type. + let body_len = res.as_mut().len(); + + let data = s.from_base58().map_err(|_| PublicError::BadBase58)?; + if data.len() < 2 { return Err(PublicError::BadLength); } + let (prefix_len, ident) = match data[0] { + 0..=63 => (1, data[0] as u16), + 64..=127 => { + // weird bit manipulation owing to the combination of LE encoding and missing two bits + // from the left. + // d[0] d[1] are: 01aaaaaa bbcccccc + // they make the LE-encoded 16-bit value: aaaaaabb 00cccccc + // so the lower byte is formed of aaaaaabb and the higher byte is 00cccccc + let lower = (data[0] << 2) | (data[1] >> 6); + let upper = data[1] & 0b00111111; + (2, (lower as u16) | ((upper as u16) << 8)) + } + _ => Err(PublicError::UnknownVersion)?, + }; + if data.len() != prefix_len + body_len + CHECKSUM_LEN { return Err(PublicError::BadLength) } + let format = ident.try_into().map_err(|_: ()| PublicError::UnknownVersion)?; + if !Self::format_is_allowed(format) { return Err(PublicError::FormatNotAllowed) } + + let hash = ss58hash(&data[0..body_len + prefix_len]); + let checksum = &hash.as_bytes()[0..CHECKSUM_LEN]; + if data[body_len + prefix_len..body_len + prefix_len + CHECKSUM_LEN] != *checksum { // Invalid checksum. return Err(PublicError::InvalidChecksum); } - res.as_mut().copy_from_slice(&d[1..len + 1]); - Ok((res, ver)) + res.as_mut().copy_from_slice(&data[prefix_len..body_len + prefix_len]); + Ok((res, format)) } + /// Some if the string is a properly encoded SS58Check address, optionally with /// a derivation path following. #[cfg(feature = "std")] @@ -267,7 +299,20 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default { /// Return the ss58-check string for this key. #[cfg(feature = "std")] fn to_ss58check_with_version(&self, version: Ss58AddressFormat) -> String { - let mut v = vec![version.into()]; + // We mask out the upper two bits of the ident - SS58 Prefix currently only supports 14-bits + let ident: u16 = u16::from(version) & 0b00111111_11111111; + let mut v = match ident { + 0..=63 => vec![ident as u8], + 64..=16_383 => { + // upper six bits of the lower byte(!) + let first = ((ident & 0b00000000_11111100) as u8) >> 2; + // lower two bits of the lower byte in the high pos, + // lower bits of the upper byte in the low pos + let second = ((ident >> 8) as u8) | ((ident & 0b00000000_00000011) as u8) << 6; + vec![first | 0b01000000, second] + } + _ => unreachable!("masked out the upper two bits; qed"), + }; v.extend(self.as_ref()); let r = ss58hash(&v); v.extend(&r.as_bytes()[0..2]); @@ -321,8 +366,8 @@ macro_rules! ss58_address_format { #[derive(Copy, Clone, PartialEq, Eq, crate::RuntimeDebug)] pub enum Ss58AddressFormat { $(#[doc = $desc] $identifier),*, - /// Use a manually provided numeric value. - Custom(u8), + /// Use a manually provided numeric value as a standard identifier + Custom(u16), } #[cfg(feature = "std")] @@ -363,8 +408,16 @@ macro_rules! ss58_address_format { } } - impl From for u8 { - fn from(x: Ss58AddressFormat) -> u8 { + impl TryFrom for Ss58AddressFormat { + type Error = (); + + fn try_from(x: u8) -> Result { + Ss58AddressFormat::try_from(x as u16) + } + } + + impl From for u16 { + fn from(x: Ss58AddressFormat) -> u16 { match x { $(Ss58AddressFormat::$identifier => $number),*, Ss58AddressFormat::Custom(n) => n, @@ -372,22 +425,13 @@ macro_rules! ss58_address_format { } } - impl TryFrom for Ss58AddressFormat { + impl TryFrom for Ss58AddressFormat { type Error = (); - fn try_from(x: u8) -> Result { + fn try_from(x: u16) -> Result { match x { $($number => Ok(Ss58AddressFormat::$identifier)),*, - _ => { - #[cfg(feature = "std")] - match Ss58AddressFormat::default() { - Ss58AddressFormat::Custom(n) if n == x => Ok(Ss58AddressFormat::Custom(x)), - _ => Err(()), - } - - #[cfg(not(feature = "std"))] - Err(()) - }, + _ => Ok(Ss58AddressFormat::Custom(x)), } } } @@ -403,7 +447,7 @@ macro_rules! ss58_address_format { fn try_from(x: &'a str) -> Result { match x { $($name => Ok(Ss58AddressFormat::$identifier)),*, - a => a.parse::().map(Ss58AddressFormat::Custom).map_err(|_| ParseError), + a => a.parse::().map(Ss58AddressFormat::Custom).map_err(|_| ParseError), } } } @@ -444,12 +488,12 @@ macro_rules! ss58_address_format { ss58_address_format!( PolkadotAccount => (0, "polkadot", "Polkadot Relay-chain, standard account (*25519).") - Reserved1 => - (1, "reserved1", "Reserved for future use (1).") + BareSr25519 => + (1, "sr25519", "Bare 32-bit Schnorr/Ristretto 25519 (S/R 25519) key.") KusamaAccount => (2, "kusama", "Kusama Relay-chain, standard account (*25519).") - Reserved3 => - (3, "reserved3", "Reserved for future use (3).") + BareEd25519 => + (3, "ed25519", "Bare 32-bit Edwards Ed25519 key.") KatalChainAccount => (4, "katalchain", "Katal Chain, standard account (*25519).") PlasmAccount => @@ -501,7 +545,7 @@ ss58_address_format!( SubsocialAccount => (28, "subsocial", "Subsocial network, standard account (*25519).") DhiwayAccount => - (29, "cord", "Dhiway CORD network, standard account (*25519).") + (29, "cord", "Dhiway CORD network, standard account (*25519).") PhalaAccount => (30, "phala", "Phala Network, standard account (*25519).") LitentryAccount => @@ -510,6 +554,8 @@ ss58_address_format!( (32, "robonomics", "Any Robonomics network standard account (*25519).") DataHighwayAccount => (33, "datahighway", "DataHighway mainnet, standard account (*25519).") + AresAccount => + (34, "ares", "Ares Protocol, standard account (*25519).") ValiuAccount => (35, "vln", "Valiu Liquidity Network mainnet, standard account (*25519).") CentrifugeAccount => @@ -522,8 +568,8 @@ ss58_address_format!( (41, "poli", "Polimec Chain mainnet, standard account (*25519).") SubstrateAccount => (42, "substrate", "Any Substrate network, standard account (*25519).") - Reserved43 => - (43, "reserved43", "Reserved for future use (43).") + BareSecp256k1 => + (43, "secp256k1", "Bare ECDSA SECP256k1 key.") ChainXAccount => (44, "chainx", "ChainX mainnet, standard account (*25519).") UniartsAccount => @@ -532,7 +578,13 @@ ss58_address_format!( (46, "reserved46", "Reserved for future use (46).") Reserved47 => (47, "reserved47", "Reserved for future use (47).") - // Note: 48 and above are reserved. + NeatcoinAccount => + (48, "neatcoin", "Neatcoin mainnet, standard account (*25519).") + AventusAccount => + (65, "aventus", "Aventus Chain mainnet, standard account (*25519).") + CrustAccount => + (66, "crust", "Crust Network, standard account (*25519).") + // Note: 16384 and above are reserved. ); /// Set the default "version" (actually, this is a bit of a misnomer and the version byte is @@ -545,14 +597,20 @@ pub fn set_default_ss58_version(version: Ss58AddressFormat) { *DEFAULT_VERSION.lock() = version } +#[cfg(feature = "std")] +lazy_static::lazy_static! { + static ref SS58_REGEX: Regex = Regex::new(r"^(?P[\w\d ]+)?(?P(//?[^/]+)*)$") + .expect("constructed from known-good static value; qed"); + static ref SECRET_PHRASE_REGEX: Regex = Regex::new(r"^(?P[\d\w ]+)?(?P(//?[^/]+)*)(///(?P.*))?$") + .expect("constructed from known-good static value; qed"); + static ref JUNCTION_REGEX: Regex = Regex::new(r"/(/?[^/]+)") + .expect("constructed from known-good static value; qed"); +} + #[cfg(feature = "std")] impl + AsRef<[u8]> + Default + Derive> Ss58Codec for T { fn from_string(s: &str) -> Result { - let re = Regex::new(r"^(?P[\w\d ]+)?(?P(//?[^/]+)*)$") - .expect("constructed from known-good static value; qed"); - let cap = re.captures(s).ok_or(PublicError::InvalidFormat)?; - let re_junction = Regex::new(r"/(/?[^/]+)") - .expect("constructed from known-good static value; qed"); + let cap = SS58_REGEX.captures(s).ok_or(PublicError::InvalidFormat)?; let s = cap.name("ss58") .map(|r| r.as_str()) .unwrap_or(DEV_ADDRESS); @@ -571,7 +629,7 @@ impl + AsRef<[u8]> + Default + Derive> Ss58Codec for T { if cap["path"].is_empty() { Ok(addr) } else { - let path = re_junction.captures_iter(&cap["path"]) + let path = JUNCTION_REGEX.captures_iter(&cap["path"]) .map(|f| DeriveJunction::from(&f[1])); addr.derive(path) .ok_or(PublicError::InvalidPath) @@ -579,11 +637,7 @@ impl + AsRef<[u8]> + Default + Derive> Ss58Codec for T { } fn from_string_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { - let re = Regex::new(r"^(?P[\w\d ]+)?(?P(//?[^/]+)*)$") - .expect("constructed from known-good static value; qed"); - let cap = re.captures(s).ok_or(PublicError::InvalidFormat)?; - let re_junction = Regex::new(r"/(/?[^/]+)") - .expect("constructed from known-good static value; qed"); + let cap = SS58_REGEX.captures(s).ok_or(PublicError::InvalidFormat)?; let (addr, v) = Self::from_ss58check_with_version( cap.name("ss58") .map(|r| r.as_str()) @@ -592,7 +646,7 @@ impl + AsRef<[u8]> + Default + Derive> Ss58Codec for T { if cap["path"].is_empty() { Ok((addr, v)) } else { - let path = re_junction.captures_iter(&cap["path"]) + let path = JUNCTION_REGEX.captures_iter(&cap["path"]) .map(|f| DeriveJunction::from(&f[1])); addr.derive(path) .ok_or(PublicError::InvalidPath) @@ -949,13 +1003,9 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static { fn from_string_with_seed(s: &str, password_override: Option<&str>) -> Result<(Self, Option), SecretStringError> { - let re = Regex::new(r"^(?P[\d\w ]+)?(?P(//?[^/]+)*)(///(?P.*))?$") - .expect("constructed from known-good static value; qed"); - let cap = re.captures(s).ok_or(SecretStringError::InvalidFormat)?; + let cap = SECRET_PHRASE_REGEX.captures(s).ok_or(SecretStringError::InvalidFormat)?; - let re_junction = Regex::new(r"/(/?[^/]+)") - .expect("constructed from known-good static value; qed"); - let path = re_junction.captures_iter(&cap["path"]) + let path = JUNCTION_REGEX.captures_iter(&cap["path"]) .map(|f| DeriveJunction::from(&f[1])); let phrase = cap.name("phrase").map(|r| r.as_str()).unwrap_or(DEV_PHRASE); diff --git a/primitives/core/src/ecdsa.rs b/primitives/core/src/ecdsa.rs index 0f654f816c472..fc9b16beedd13 100644 --- a/primitives/core/src/ecdsa.rs +++ b/primitives/core/src/ecdsa.rs @@ -554,6 +554,7 @@ mod test { use hex_literal::hex; use crate::crypto::{DEV_PHRASE, set_default_ss58_version}; use serde_json; + use crate::crypto::PublicError; #[test] fn default_phrase_should_be_used() { @@ -676,6 +677,34 @@ mod test { assert_eq!(cmp, public); } + #[test] + fn ss58check_format_check_works() { + use crate::crypto::Ss58AddressFormat; + let pair = Pair::from_seed(b"12345678901234567890123456789012"); + let public = pair.public(); + let format = Ss58AddressFormat::Reserved46; + let s = public.to_ss58check_with_version(format); + assert_eq!(Public::from_ss58check_with_version(&s), Err(PublicError::FormatNotAllowed)); + } + + #[test] + fn ss58check_full_roundtrip_works() { + use crate::crypto::Ss58AddressFormat; + let pair = Pair::from_seed(b"12345678901234567890123456789012"); + let public = pair.public(); + let format = Ss58AddressFormat::PolkadotAccount; + let s = public.to_ss58check_with_version(format); + let (k, f) = Public::from_ss58check_with_version(&s).unwrap(); + assert_eq!(k, public); + assert_eq!(f, format); + + let format = Ss58AddressFormat::Custom(64); + let s = public.to_ss58check_with_version(format); + let (k, f) = Public::from_ss58check_with_version(&s).unwrap(); + assert_eq!(k, public); + assert_eq!(f, format); + } + #[test] fn ss58check_custom_format_works() { // We need to run this test in its own process to not interfere with other tests running in @@ -685,10 +714,12 @@ mod test { // temp save default format version let default_format = Ss58AddressFormat::default(); // set current ss58 version is custom "200" `Ss58AddressFormat::Custom(200)` + set_default_ss58_version(Ss58AddressFormat::Custom(200)); // custom addr encoded by version 200 - let addr = "2X64kMNEWAW5KLZMSKcGKEc96MyuaRsRUku7vomuYxKgqjVCRj"; + let addr = "4pbsSkWcBaYoFHrKJZp5fDVUKbqSYD9dhZZGvpp3vQ5ysVs5ybV"; Public::from_ss58check(&addr).unwrap(); + set_default_ss58_version(default_format); // set current ss58 version to default version let addr = "KWAfgC2aRG5UVD6CpbPQXCx4YZZUhvWqqAJE6qcYc9Rtr6g5C"; diff --git a/primitives/core/src/hashing.rs b/primitives/core/src/hashing.rs index 0b67d33235ae3..ac0eedef6967d 100644 --- a/primitives/core/src/hashing.rs +++ b/primitives/core/src/hashing.rs @@ -18,7 +18,7 @@ //! Hashing functions. //! //! This module is gated by `full-crypto` feature. If you intend to use any of the functions -//! defined here within your runtime, you should most likely rather use [sp_io::hashing] instead, +//! defined here within your runtime, you should most likely rather use `sp_io::hashing` instead, //! unless you know what you're doing. Using `sp_io` will be more performant, since instead of //! computing the hash in WASM it delegates that computation to the host client. diff --git a/primitives/core/src/hexdisplay.rs b/primitives/core/src/hexdisplay.rs index 304b665a72c9b..e590eec0e5aec 100644 --- a/primitives/core/src/hexdisplay.rs +++ b/primitives/core/src/hexdisplay.rs @@ -71,6 +71,12 @@ impl AsBytesRef for sp_std::vec::Vec { fn as_bytes_ref(&self) -> &[u8] { &self } } +impl AsBytesRef for sp_storage::StorageKey { + fn as_bytes_ref(&self) -> &[u8] { + self.as_ref() + } +} + macro_rules! impl_non_endians { ( $( $t:ty ),* ) => { $( impl AsBytesRef for $t { diff --git a/primitives/core/src/sandbox.rs b/primitives/core/src/sandbox.rs index 330ea7eb92e11..a15a7af418313 100644 --- a/primitives/core/src/sandbox.rs +++ b/primitives/core/src/sandbox.rs @@ -31,12 +31,12 @@ pub struct HostError; pub enum ExternEntity { /// Function that is specified by an index in a default table of /// a module that creates the sandbox. - #[codec(index = "1")] + #[codec(index = 1)] Function(u32), /// Linear memory that is specified by some identifier returned by sandbox /// module upon creation new sandboxed memory. - #[codec(index = "2")] + #[codec(index = 2)] Memory(u32), } diff --git a/primitives/core/src/testing.rs b/primitives/core/src/testing.rs index 1506abb77f9c1..b33f518c32ee0 100644 --- a/primitives/core/src/testing.rs +++ b/primitives/core/src/testing.rs @@ -152,3 +152,13 @@ impl crate::traits::SpawnNamed for TaskExecutor { self.0.spawn_ok(future); } } + +#[cfg(feature = "std")] +impl crate::traits::SpawnEssentialNamed for TaskExecutor { + fn spawn_essential_blocking(&self, _: &'static str, future: futures::future::BoxFuture<'static, ()>) { + self.0.spawn_ok(future); + } + fn spawn_essential(&self, _: &'static str, future: futures::future::BoxFuture<'static, ()>) { + self.0.spawn_ok(future); + } +} diff --git a/primitives/core/src/traits.rs b/primitives/core/src/traits.rs index 8488a1873cacf..90f8060f9a565 100644 --- a/primitives/core/src/traits.rs +++ b/primitives/core/src/traits.rs @@ -34,7 +34,7 @@ pub trait CodeExecutor: Sized + Send + Sync + CallInWasm + Clone + 'static { /// or an execution error) together with a `bool`, which is true if native execution was used. fn call< R: codec::Codec + PartialEq, - NC: FnOnce() -> Result + UnwindSafe, + NC: FnOnce() -> Result> + UnwindSafe, >( &self, ext: &mut dyn Externalities, @@ -205,7 +205,7 @@ sp_externalities::decl_extension! { pub struct RuntimeSpawnExt(Box); } -/// Something that can spawn futures (blocking and non-blocking) with an assigned name. +/// Something that can spawn tasks (blocking and non-blocking) with an assigned name. #[dyn_clonable::clonable] pub trait SpawnNamed: Clone + Send + Sync { /// Spawn the given blocking future. @@ -227,3 +227,28 @@ impl SpawnNamed for Box { (**self).spawn(name, future) } } + +/// Something that can spawn essential tasks (blocking and non-blocking) with an assigned name. +/// +/// Essential tasks are special tasks that should take down the node when they end. +#[dyn_clonable::clonable] +pub trait SpawnEssentialNamed: Clone + Send + Sync { + /// Spawn the given blocking future. + /// + /// The given `name` is used to identify the future in tracing. + fn spawn_essential_blocking(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>); + /// Spawn the given non-blocking future. + /// + /// The given `name` is used to identify the future in tracing. + fn spawn_essential(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>); +} + +impl SpawnEssentialNamed for Box { + fn spawn_essential_blocking(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>) { + (**self).spawn_essential_blocking(name, future) + } + + fn spawn_essential(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>) { + (**self).spawn_essential(name, future) + } +} diff --git a/primitives/database/Cargo.toml b/primitives/database/Cargo.toml index 728396aea74cb..4062ba292352f 100644 --- a/primitives/database/Cargo.toml +++ b/primitives/database/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-database" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -12,4 +12,4 @@ readme = "README.md" [dependencies] parking_lot = "0.11.1" -kvdb = "0.8.0" +kvdb = "0.9.0" diff --git a/primitives/database/src/lib.rs b/primitives/database/src/lib.rs index 94fe16ce01db5..7107ea25c02c0 100644 --- a/primitives/database/src/lib.rs +++ b/primitives/database/src/lib.rs @@ -115,6 +115,11 @@ pub trait Database: Send + Sync { /// `key` is not currently in the database. fn get(&self, col: ColumnId, key: &[u8]) -> Option>; + /// Check if the value exists in the database without retrieving it. + fn contains(&self, col: ColumnId, key: &[u8]) -> bool { + self.get(col, key).is_some() + } + /// Call `f` with the value previously stored against `key`. /// /// This may be faster than `get` since it doesn't allocate. diff --git a/primitives/debug-derive/Cargo.toml b/primitives/debug-derive/Cargo.toml index f72842b19615e..0d3ba805100c4 100644 --- a/primitives/debug-derive/Cargo.toml +++ b/primitives/debug-derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-debug-derive" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/primitives/debug-derive/src/lib.rs b/primitives/debug-derive/src/lib.rs index 74907b13874a3..ebfbd614d9c8d 100644 --- a/primitives/debug-derive/src/lib.rs +++ b/primitives/debug-derive/src/lib.rs @@ -27,9 +27,9 @@ //! //! ```rust //! #[derive(sp_debug_derive::RuntimeDebug)] -//! struct MyStruct; +//! struct MyStruct; //! -//! assert_eq!(format!("{:?}", MyStruct), "MyStruct"); +//! assert_eq!(format!("{:?}", MyStruct), "MyStruct"); //! ``` mod impls; diff --git a/primitives/election-providers/Cargo.toml b/primitives/election-providers/Cargo.toml index f017a37637202..cf12dce8098d7 100644 --- a/primitives/election-providers/Cargo.toml +++ b/primitives/election-providers/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-election-providers" -version = "2.0.0" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,14 +13,14 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.1", default-features = false, path = "../std" } -sp-arithmetic = { version = "2.0.1", default-features = false, path = "../arithmetic" } -sp-npos-elections = { version = "2.0.1", default-features = false, path = "../npos-elections" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-arithmetic = { version = "3.0.0", default-features = false, path = "../arithmetic" } +sp-npos-elections = { version = "3.0.0", default-features = false, path = "../npos-elections" } [dev-dependencies] -sp-npos-elections = { version = "2.0.1", path = "../npos-elections" } -sp-runtime = { version = "2.0.1", path = "../runtime" } +sp-npos-elections = { version = "3.0.0", path = "../npos-elections" } +sp-runtime = { version = "3.0.0", path = "../runtime" } [features] default = ["std"] diff --git a/primitives/externalities/Cargo.toml b/primitives/externalities/Cargo.toml index f1990e89d757b..05de1837dc1d5 100644 --- a/primitives/externalities/Cargo.toml +++ b/primitives/externalities/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-externalities" -version = "0.8.1" +version = "0.9.0" license = "Apache-2.0" authors = ["Parity Technologies "] edition = "2018" @@ -14,10 +14,10 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-storage = { version = "2.0.0", path = "../storage", default-features = false } -sp-std = { version = "2.0.0", path = "../std", default-features = false } +sp-storage = { version = "3.0.0", path = "../storage", default-features = false } +sp-std = { version = "3.0.0", path = "../std", default-features = false } environmental = { version = "1.1.2", default-features = false } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } [features] default = ["std"] diff --git a/primitives/externalities/src/lib.rs b/primitives/externalities/src/lib.rs index a10ce32bdc855..3ee37f5e31b93 100644 --- a/primitives/externalities/src/lib.rs +++ b/primitives/externalities/src/lib.rs @@ -139,15 +139,16 @@ pub trait Externalities: ExtensionStore { /// Clear an entire child storage. /// /// Deletes all keys from the overlay and up to `limit` keys from the backend. No - /// limit is applied if `limit` is `None`. Returns `true` if the child trie was + /// limit is applied if `limit` is `None`. Returned boolean is `true` if the child trie was /// removed completely and `false` if there are remaining keys after the function - /// returns. + /// returns. Returned `u32` is the number of keys that was removed at the end of the + /// operation. /// /// # Note /// /// An implementation is free to delete more keys than the specified limit as long as /// it is able to do that in constant time. - fn kill_child_storage(&mut self, child_info: &ChildInfo, limit: Option) -> bool; + fn kill_child_storage(&mut self, child_info: &ChildInfo, limit: Option) -> (bool, u32); /// Clear storage entries which keys are start with the given prefix. fn clear_prefix(&mut self, prefix: &[u8]); diff --git a/primitives/finality-grandpa/Cargo.toml b/primitives/finality-grandpa/Cargo.toml index f96196bdb190c..95aa65c930f78 100644 --- a/primitives/finality-grandpa/Cargo.toml +++ b/primitives/finality-grandpa/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-finality-grandpa" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,16 +15,16 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -grandpa = { package = "finality-grandpa", version = "0.12.3", default-features = false, features = ["derive-codec"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +grandpa = { package = "finality-grandpa", version = "0.14.0", default-features = false, features = ["derive-codec"] } log = { version = "0.4.8", optional = true } serde = { version = "1.0.101", optional = true, features = ["derive"] } -sp-api = { version = "2.0.0", default-features = false, path = "../api" } -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../application-crypto" } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } -sp-keystore = { version = "0.8.0", default-features = false, path = "../keystore", optional = true } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } +sp-api = { version = "3.0.0", default-features = false, path = "../api" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../application-crypto" } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } +sp-keystore = { version = "0.9.0", default-features = false, path = "../keystore", optional = true } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } [features] default = ["std"] diff --git a/primitives/finality-grandpa/src/lib.rs b/primitives/finality-grandpa/src/lib.rs index 5a5468aff5608..5b393bd1d80e6 100644 --- a/primitives/finality-grandpa/src/lib.rs +++ b/primitives/finality-grandpa/src/lib.rs @@ -102,7 +102,7 @@ pub enum ConsensusLog { /// This should be a pure function: i.e. as long as the runtime can interpret /// the digest type it should return the same result regardless of the current /// state. - #[codec(index = "1")] + #[codec(index = 1)] ScheduledChange(ScheduledChange), /// Force an authority set change. /// @@ -118,18 +118,18 @@ pub enum ConsensusLog { /// This should be a pure function: i.e. as long as the runtime can interpret /// the digest type it should return the same result regardless of the current /// state. - #[codec(index = "2")] + #[codec(index = 2)] ForcedChange(N, ScheduledChange), /// Note that the authority with given index is disabled until the next change. - #[codec(index = "3")] + #[codec(index = 3)] OnDisabled(AuthorityIndex), /// A signal to pause the current authority set after the given delay. /// After finalizing the block at _delay_ the authorities should stop voting. - #[codec(index = "4")] + #[codec(index = 4)] Pause(N), /// A signal to resume the current authority set after the given delay. /// After authoring the block at _delay_ the authorities should resume voting. - #[codec(index = "5")] + #[codec(index = 5)] Resume(N), } @@ -400,7 +400,7 @@ where AuthorityId::ID, &public.to_public_crypto_pair(), &encoded[..], - ).ok()?.try_into().ok()?; + ).ok().flatten()?.try_into().ok()?; Some(grandpa::SignedMessage { message, diff --git a/primitives/inherents/Cargo.toml b/primitives/inherents/Cargo.toml index f73bd97bf4b0e..c0e74c0fb99fd 100644 --- a/primitives/inherents/Cargo.toml +++ b/primitives/inherents/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-inherents" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -16,9 +16,9 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] parking_lot = { version = "0.11.1", optional = true } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } thiserror = { version = "1.0.21", optional = true } [features] diff --git a/primitives/inherents/src/lib.rs b/primitives/inherents/src/lib.rs index 8adf44cbc418c..0110db5680a18 100644 --- a/primitives/inherents/src/lib.rs +++ b/primitives/inherents/src/lib.rs @@ -398,9 +398,11 @@ impl IsFatalError for MakeFatalError { } } -/// A module that provides an inherent and may also verifies it. +/// A pallet that provides or verifies an inherent extrinsic. +/// +/// The pallet may provide the inherent, verify an inherent, or both provide and verify. pub trait ProvideInherent { - /// The call type of the module. + /// The call type of the pallet. type Call; /// The error returned by `check_inherent`. type Error: codec::Encode + IsFatalError; @@ -410,13 +412,27 @@ pub trait ProvideInherent { /// Create an inherent out of the given `InherentData`. fn create_inherent(data: &InherentData) -> Option; - /// If `Some`, indicates that an inherent is required. Check will return the inner error if no - /// inherent is found. If `Err`, indicates that the check failed and further operations should - /// be aborted. + /// Determines whether this inherent is required in this block. + /// + /// - `Ok(None)` indicates that this inherent is not required in this block. The default + /// implementation returns this. + /// + /// - `Ok(Some(e))` indicates that this inherent is required in this block. The + /// `impl_outer_inherent!`, will call this function from its `check_extrinsics`. + /// If the inherent is not present, it will return `e`. + /// + /// - `Err(_)` indicates that this function failed and further operations should be aborted. + /// + /// CAUTION: This check has a bug when used in pallets that also provide unsigned transactions. + /// See for details. fn is_inherent_required(_: &InherentData) -> Result, Self::Error> { Ok(None) } - /// Check the given inherent if it is valid. - /// Checking the inherent is optional and can be omitted. + /// Check whether the given inherent is valid. Checking the inherent is optional and can be + /// omitted by using the default implementation. + /// + /// When checking an inherent, the first parameter represents the inherent that is actually + /// included in the block by its author. Whereas the second parameter represents the inherent + /// data that the verifying node calculates. fn check_inherent(_: &Self::Call, _: &InherentData) -> Result<(), Self::Error> { Ok(()) } diff --git a/primitives/io/Cargo.toml b/primitives/io/Cargo.toml index 06d9878f6bebc..9bd3d83a74ee9 100644 --- a/primitives/io/Cargo.toml +++ b/primitives/io/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-io" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,18 +15,18 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } hash-db = { version = "0.15.2", default-features = false } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } -sp-keystore = { version = "0.8.0", default-features = false, optional = true, path = "../keystore" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } +sp-keystore = { version = "0.9.0", default-features = false, optional = true, path = "../keystore" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } libsecp256k1 = { version = "0.3.4", optional = true } -sp-state-machine = { version = "0.8.0", optional = true, path = "../state-machine" } -sp-wasm-interface = { version = "2.0.0", path = "../wasm-interface", default-features = false } -sp-runtime-interface = { version = "2.0.0", default-features = false, path = "../runtime-interface" } -sp-trie = { version = "2.0.0", optional = true, path = "../trie" } -sp-externalities = { version = "0.8.0", optional = true, path = "../externalities" } -sp-tracing = { version = "2.0.0", default-features = false, path = "../tracing" } +sp-state-machine = { version = "0.9.0", optional = true, path = "../state-machine" } +sp-wasm-interface = { version = "3.0.0", path = "../wasm-interface", default-features = false } +sp-runtime-interface = { version = "3.0.0", default-features = false, path = "../runtime-interface" } +sp-trie = { version = "3.0.0", optional = true, path = "../trie" } +sp-externalities = { version = "0.9.0", optional = true, path = "../externalities" } +sp-tracing = { version = "3.0.0", default-features = false, path = "../tracing" } log = { version = "0.4.8", optional = true } futures = { version = "0.3.1", features = ["thread-pool"], optional = true } parking_lot = { version = "0.11.1", optional = true } diff --git a/primitives/io/src/lib.rs b/primitives/io/src/lib.rs index ad993bb2a6c1d..59d8d90202299 100644 --- a/primitives/io/src/lib.rs +++ b/primitives/io/src/lib.rs @@ -57,7 +57,7 @@ use sp_core::{ use sp_trie::{TrieConfiguration, trie_types::Layout}; use sp_runtime_interface::{runtime_interface, Pointer}; -use sp_runtime_interface::pass_by::PassBy; +use sp_runtime_interface::pass_by::{PassBy, PassByCodec}; use codec::{Encode, Decode}; @@ -81,6 +81,16 @@ pub enum EcdsaVerifyError { BadSignature, } +/// The outcome of calling [`kill_storage`]. Returned value is the number of storage items +/// removed from the trie from making the `kill_storage` call. +#[derive(PassByCodec, Encode, Decode)] +pub enum KillChildStorageResult { + /// No key remains in the child trie. + AllRemoved(u32), + /// At least one key still resides in the child trie due to the supplied limit. + SomeRemaining(u32), +} + /// Interface for accessing the storage from within the runtime. #[runtime_interface] pub trait Storage { @@ -290,7 +300,7 @@ pub trait DefaultChildStorage { /// The limit can be used to partially delete a child trie in case it is too large /// to delete in one go (block). /// - /// It returns false iff some keys are remaining in + /// It returns a boolean false iff some keys are remaining in /// the child trie after the functions returns. /// /// # Note @@ -307,7 +317,41 @@ pub trait DefaultChildStorage { #[version(2)] fn storage_kill(&mut self, storage_key: &[u8], limit: Option) -> bool { let child_info = ChildInfo::new_default(storage_key); - self.kill_child_storage(&child_info, limit) + let (all_removed, _num_removed) = self.kill_child_storage(&child_info, limit); + all_removed + } + + /// Clear a child storage key. + /// + /// Deletes all keys from the overlay and up to `limit` keys from the backend if + /// it is set to `Some`. No limit is applied when `limit` is set to `None`. + /// + /// The limit can be used to partially delete a child trie in case it is too large + /// to delete in one go (block). + /// + /// It returns a boolean false iff some keys are remaining in + /// the child trie after the functions returns. Also returns a `u32` with + /// the number of keys removed from the process. + /// + /// # Note + /// + /// Please note that keys that are residing in the overlay for that child trie when + /// issuing this call are all deleted without counting towards the `limit`. Only keys + /// written during the current block are part of the overlay. Deleting with a `limit` + /// mostly makes sense with an empty overlay for that child trie. + /// + /// Calling this function multiple times per block for the same `storage_key` does + /// not make much sense because it is not cumulative when called inside the same block. + /// Use this function to distribute the deletion of a single child trie across multiple + /// blocks. + #[version(3)] + fn storage_kill(&mut self, storage_key: &[u8], limit: Option) -> KillChildStorageResult { + let child_info = ChildInfo::new_default(storage_key); + let (all_removed, num_removed) = self.kill_child_storage(&child_info, limit); + match all_removed { + true => KillChildStorageResult::AllRemoved(num_removed), + false => KillChildStorageResult::SomeRemaining(num_removed), + } } /// Check a child storage key. @@ -474,8 +518,9 @@ pub trait Crypto { let keystore = &***self.extension::() .expect("No `keystore` associated for the current context!"); SyncCryptoStore::sign_with(keystore, id, &pub_key.into(), msg) - .map(|sig| ed25519::Signature::from_slice(sig.as_slice())) .ok() + .flatten() + .map(|sig| ed25519::Signature::from_slice(sig.as_slice())) } /// Verify `ed25519` signature. @@ -600,8 +645,9 @@ pub trait Crypto { let keystore = &***self.extension::() .expect("No `keystore` associated for the current context!"); SyncCryptoStore::sign_with(keystore, id, &pub_key.into(), msg) - .map(|sig| sr25519::Signature::from_slice(sig.as_slice())) .ok() + .flatten() + .map(|sig| sr25519::Signature::from_slice(sig.as_slice())) } /// Verify an `sr25519` signature. @@ -646,8 +692,9 @@ pub trait Crypto { let keystore = &***self.extension::() .expect("No `keystore` associated for the current context!"); SyncCryptoStore::sign_with(keystore, id, &pub_key.into(), msg) - .map(|sig| ecdsa::Signature::from_slice(sig.as_slice())) .ok() + .flatten() + .map(|sig| ecdsa::Signature::from_slice(sig.as_slice())) } /// Verify `ecdsa` signature. diff --git a/primitives/keyring/Cargo.toml b/primitives/keyring/Cargo.toml index e3e927f70bb82..ee71687f1ef7c 100644 --- a/primitives/keyring/Cargo.toml +++ b/primitives/keyring/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-keyring" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-core = { version = "2.0.0", path = "../core" } -sp-runtime = { version = "2.0.0", path = "../runtime" } +sp-core = { version = "3.0.0", path = "../core" } +sp-runtime = { version = "3.0.0", path = "../runtime" } lazy_static = "1.4.0" -strum = { version = "0.16.0", features = ["derive"] } +strum = { version = "0.20.0", features = ["derive"] } diff --git a/primitives/keystore/Cargo.toml b/primitives/keystore/Cargo.toml index 7fb6b4b93fc21..81404ce344a21 100644 --- a/primitives/keystore/Cargo.toml +++ b/primitives/keystore/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-keystore" -version = "0.8.0" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,14 +15,14 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] async-trait = "0.1.30" derive_more = "0.99.2" -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } futures = { version = "0.3.1" } schnorrkel = { version = "0.9.1", features = ["preaudit_deprecated", "u64_backend"], default-features = false } merlin = { version = "2.0", default-features = false } parking_lot = { version = "0.11.1", default-features = false } serde = { version = "1.0", optional = true} -sp-core = { version = "2.0.0", path = "../core" } -sp-externalities = { version = "0.8.0", path = "../externalities", default-features = false } +sp-core = { version = "3.0.0", path = "../core" } +sp-externalities = { version = "0.9.0", path = "../externalities", default-features = false } [dev-dependencies] rand = "0.7.2" diff --git a/primitives/keystore/src/lib.rs b/primitives/keystore/src/lib.rs index f42f6dd7122d0..2fda3a48c5da2 100644 --- a/primitives/keystore/src/lib.rs +++ b/primitives/keystore/src/lib.rs @@ -34,9 +34,6 @@ pub enum Error { /// Public key type is not supported #[display(fmt="Key not supported: {:?}", _0)] KeyNotSupported(KeyTypeId), - /// Pair not found for public key and KeyTypeId - #[display(fmt="Pair was not found: {}", _0)] - PairNotFound(String), /// Validation error #[display(fmt="Validation error: {}", _0)] ValidationError(String), @@ -125,37 +122,39 @@ pub trait CryptoStore: Send + Sync { /// Signs a message with the private key that matches /// the public key passed. /// - /// Returns the SCALE encoded signature if key is found & supported, - /// an error otherwise. + /// Returns the SCALE encoded signature if key is found and supported, `None` if the key doesn't + /// exist or an error when something failed. async fn sign_with( &self, id: KeyTypeId, key: &CryptoTypePublicPair, msg: &[u8], - ) -> Result, Error>; + ) -> Result>, Error>; /// Sign with any key /// /// Given a list of public keys, find the first supported key and /// sign the provided message with that key. /// - /// Returns a tuple of the used key and the SCALE encoded signature. + /// Returns a tuple of the used key and the SCALE encoded signature or `None` if no key could + /// be found to sign. async fn sign_with_any( &self, id: KeyTypeId, keys: Vec, msg: &[u8] - ) -> Result<(CryptoTypePublicPair, Vec), Error> { + ) -> Result)>, Error> { if keys.len() == 1 { - return self.sign_with(id, &keys[0], msg).await.map(|s| (keys[0].clone(), s)); + return Ok(self.sign_with(id, &keys[0], msg).await?.map(|s| (keys[0].clone(), s))); } else { for k in self.supported_keys(id, keys).await? { - if let Ok(sign) = self.sign_with(id, &k, msg).await { - return Ok((k, sign)); + if let Ok(Some(sign)) = self.sign_with(id, &k, msg).await { + return Ok(Some((k, sign))); } } } - Err(Error::KeyNotSupported(id)) + + Ok(None) } /// Sign with all keys @@ -164,13 +163,13 @@ pub trait CryptoStore: Send + Sync { /// each key given that the key is supported. /// /// Returns a list of `Result`s each representing the SCALE encoded - /// signature of each key or a Error for non-supported keys. + /// signature of each key, `None` if the key doesn't exist or a error when something failed. async fn sign_with_all( &self, id: KeyTypeId, keys: Vec, msg: &[u8], - ) -> Result, Error>>, ()> { + ) -> Result>, Error>>, ()> { let futs = keys.iter() .map(|k| self.sign_with(id, k, msg)); @@ -187,16 +186,14 @@ pub trait CryptoStore: Send + Sync { /// Namely, VRFOutput and VRFProof which are returned /// inside the `VRFSignature` container struct. /// - /// This function will return an error in the cases where - /// the public key and key type provided do not match a private - /// key in the keystore. Or, in the context of remote signing - /// an error could be a network one. + /// This function will return `None` if the given `key_type` and `public` combination + /// doesn't exist in the keystore or an `Err` when something failed. async fn sr25519_vrf_sign( &self, key_type: KeyTypeId, public: &sr25519::Public, transcript_data: VRFTranscriptData, - ) -> Result; + ) -> Result, Error>; } /// Sync version of the CryptoStore @@ -285,37 +282,41 @@ pub trait SyncCryptoStore: CryptoStore + Send + Sync { /// Signs a message with the private key that matches /// the public key passed. /// - /// Returns the SCALE encoded signature if key is found & supported, - /// an error otherwise. + /// Returns the SCALE encoded signature if key is found and supported, `None` if the key doesn't + /// exist or an error when something failed. fn sign_with( &self, id: KeyTypeId, key: &CryptoTypePublicPair, msg: &[u8], - ) -> Result, Error>; + ) -> Result>, Error>; /// Sign with any key /// /// Given a list of public keys, find the first supported key and /// sign the provided message with that key. /// - /// Returns a tuple of the used key and the SCALE encoded signature. + /// Returns a tuple of the used key and the SCALE encoded signature or `None` if no key could + /// be found to sign. fn sign_with_any( &self, id: KeyTypeId, keys: Vec, msg: &[u8] - ) -> Result<(CryptoTypePublicPair, Vec), Error> { + ) -> Result)>, Error> { if keys.len() == 1 { - return SyncCryptoStore::sign_with(self, id, &keys[0], msg).map(|s| (keys[0].clone(), s)); + return Ok( + SyncCryptoStore::sign_with(self, id, &keys[0], msg)?.map(|s| (keys[0].clone(), s)), + ) } else { for k in SyncCryptoStore::supported_keys(self, id, keys)? { - if let Ok(sign) = SyncCryptoStore::sign_with(self, id, &k, msg) { - return Ok((k, sign)); + if let Ok(Some(sign)) = SyncCryptoStore::sign_with(self, id, &k, msg) { + return Ok(Some((k, sign))); } } } - Err(Error::KeyNotSupported(id)) + + Ok(None) } /// Sign with all keys @@ -324,13 +325,13 @@ pub trait SyncCryptoStore: CryptoStore + Send + Sync { /// each key given that the key is supported. /// /// Returns a list of `Result`s each representing the SCALE encoded - /// signature of each key or a Error for non-supported keys. + /// signature of each key, `None` if the key doesn't exist or an error when something failed. fn sign_with_all( &self, id: KeyTypeId, keys: Vec, msg: &[u8], - ) -> Result, Error>>, ()>{ + ) -> Result>, Error>>, ()> { Ok(keys.iter().map(|k| SyncCryptoStore::sign_with(self, id, k, msg)).collect()) } @@ -344,16 +345,14 @@ pub trait SyncCryptoStore: CryptoStore + Send + Sync { /// Namely, VRFOutput and VRFProof which are returned /// inside the `VRFSignature` container struct. /// - /// This function will return an error in the cases where - /// the public key and key type provided do not match a private - /// key in the keystore. Or, in the context of remote signing - /// an error could be a network one. + /// This function will return `None` if the given `key_type` and `public` combination + /// doesn't exist in the keystore or an `Err` when something failed. fn sr25519_vrf_sign( &self, key_type: KeyTypeId, public: &sr25519::Public, transcript_data: VRFTranscriptData, - ) -> Result; + ) -> Result, Error>; } /// A pointer to a keystore. diff --git a/primitives/keystore/src/testing.rs b/primitives/keystore/src/testing.rs index 702e2bbc857d7..caee7178e094f 100644 --- a/primitives/keystore/src/testing.rs +++ b/primitives/keystore/src/testing.rs @@ -132,7 +132,7 @@ impl CryptoStore for KeyStore { id: KeyTypeId, key: &CryptoTypePublicPair, msg: &[u8], - ) -> Result, Error> { + ) -> Result>, Error> { SyncCryptoStore::sign_with(self, id, key, msg) } @@ -141,7 +141,7 @@ impl CryptoStore for KeyStore { key_type: KeyTypeId, public: &sr25519::Public, transcript_data: VRFTranscriptData, - ) -> Result { + ) -> Result, Error> { SyncCryptoStore::sr25519_vrf_sign(self, key_type, public, transcript_data) } } @@ -280,27 +280,27 @@ impl SyncCryptoStore for KeyStore { id: KeyTypeId, key: &CryptoTypePublicPair, msg: &[u8], - ) -> Result, Error> { + ) -> Result>, Error> { use codec::Encode; match key.0 { ed25519::CRYPTO_ID => { - let key_pair: ed25519::Pair = self - .ed25519_key_pair(id, &ed25519::Public::from_slice(key.1.as_slice())) - .ok_or_else(|| Error::PairNotFound("ed25519".to_owned()))?; - return Ok(key_pair.sign(msg).encode()); + let key_pair = self + .ed25519_key_pair(id, &ed25519::Public::from_slice(key.1.as_slice())); + + key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose() } sr25519::CRYPTO_ID => { - let key_pair: sr25519::Pair = self - .sr25519_key_pair(id, &sr25519::Public::from_slice(key.1.as_slice())) - .ok_or_else(|| Error::PairNotFound("sr25519".to_owned()))?; - return Ok(key_pair.sign(msg).encode()); + let key_pair = self + .sr25519_key_pair(id, &sr25519::Public::from_slice(key.1.as_slice())); + + key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose() } ecdsa::CRYPTO_ID => { - let key_pair: ecdsa::Pair = self - .ecdsa_key_pair(id, &ecdsa::Public::from_slice(key.1.as_slice())) - .ok_or_else(|| Error::PairNotFound("ecdsa".to_owned()))?; - return Ok(key_pair.sign(msg).encode()); + let key_pair = self + .ecdsa_key_pair(id, &ecdsa::Public::from_slice(key.1.as_slice())); + + key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose() } _ => Err(Error::KeyNotSupported(id)) } @@ -311,15 +311,19 @@ impl SyncCryptoStore for KeyStore { key_type: KeyTypeId, public: &sr25519::Public, transcript_data: VRFTranscriptData, - ) -> Result { + ) -> Result, Error> { let transcript = make_transcript(transcript_data); - let pair = self.sr25519_key_pair(key_type, public) - .ok_or_else(|| Error::PairNotFound("Not found".to_owned()))?; + let pair = if let Some(k) = self.sr25519_key_pair(key_type, public) { + k + } else { + return Ok(None) + }; + let (inout, proof, _) = pair.as_ref().vrf_sign(transcript); - Ok(VRFSignature { + Ok(Some(VRFSignature { output: inout.to_output(), proof, - }) + })) } } @@ -394,7 +398,7 @@ mod tests { &key_pair.public(), transcript_data.clone(), ); - assert!(result.is_err()); + assert!(result.unwrap().is_none()); SyncCryptoStore::insert_unknown( &store, @@ -410,6 +414,6 @@ mod tests { transcript_data, ); - assert!(result.is_ok()); + assert!(result.unwrap().is_some()); } } diff --git a/primitives/npos-elections/Cargo.toml b/primitives/npos-elections/Cargo.toml index 82ce6b005a954..79d46743cd758 100644 --- a/primitives/npos-elections/Cargo.toml +++ b/primitives/npos-elections/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-npos-elections" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,17 +13,17 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } serde = { version = "1.0.101", optional = true, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-npos-elections-compact = { version = "2.0.0", path = "./compact" } -sp-arithmetic = { version = "2.0.0", default-features = false, path = "../arithmetic" } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-npos-elections-compact = { version = "3.0.0", path = "./compact" } +sp-arithmetic = { version = "3.0.0", default-features = false, path = "../arithmetic" } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } [dev-dependencies] -substrate-test-utils = { version = "2.0.0", path = "../../test-utils" } +substrate-test-utils = { version = "3.0.0", path = "../../test-utils" } rand = "0.7.3" -sp-runtime = { version = "2.0.0", path = "../runtime" } +sp-runtime = { version = "3.0.0", path = "../runtime" } [features] default = ["std"] diff --git a/primitives/npos-elections/compact/Cargo.toml b/primitives/npos-elections/compact/Cargo.toml index 7383dd67d5939..57cb6dc1c4f22 100644 --- a/primitives/npos-elections/compact/Cargo.toml +++ b/primitives/npos-elections/compact/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-npos-elections-compact" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/primitives/npos-elections/fuzzer/Cargo.toml b/primitives/npos-elections/fuzzer/Cargo.toml index a1fa4a2f4ca40..bac8a165f3947 100644 --- a/primitives/npos-elections/fuzzer/Cargo.toml +++ b/primitives/npos-elections/fuzzer/Cargo.toml @@ -14,12 +14,12 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-npos-elections = { version = "2.0.0", path = ".." } -sp-std = { version = "2.0.0", path = "../../std" } -sp-runtime = { version = "2.0.0", path = "../../runtime" } +sp-npos-elections = { version = "3.0.0", path = ".." } +sp-std = { version = "3.0.0", path = "../../std" } +sp-runtime = { version = "3.0.0", path = "../../runtime" } honggfuzz = "0.5" rand = { version = "0.7.3", features = ["std", "small_rng"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } [[bin]] name = "reduce" diff --git a/primitives/offchain/Cargo.toml b/primitives/offchain/Cargo.toml index 6678ac32ea67b..1e3d0a34b26b1 100644 --- a/primitives/offchain/Cargo.toml +++ b/primitives/offchain/Cargo.toml @@ -1,7 +1,7 @@ [package] description = "Substrate offchain workers primitives" name = "sp-offchain" -version = "2.0.1" +version = "3.0.0" license = "Apache-2.0" authors = ["Parity Technologies "] edition = "2018" @@ -13,12 +13,12 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-core = { version = "2.0.0", default-features = false, path = "../core" } -sp-api = { version = "2.0.0", default-features = false, path = "../api" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } +sp-api = { version = "3.0.0", default-features = false, path = "../api" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } [dev-dependencies] -sp-state-machine = { version = "0.8.0", default-features = false, path = "../state-machine" } +sp-state-machine = { version = "0.9.0", default-features = false, path = "../state-machine" } [features] default = ["std"] diff --git a/primitives/panic-handler/Cargo.toml b/primitives/panic-handler/Cargo.toml index 5ec47423c0149..ad03baca24ebb 100644 --- a/primitives/panic-handler/Cargo.toml +++ b/primitives/panic-handler/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-panic-handler" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/primitives/rpc/Cargo.toml b/primitives/rpc/Cargo.toml index 3644894362781..de7e2bd882e73 100644 --- a/primitives/rpc/Cargo.toml +++ b/primitives/rpc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-rpc" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", features = ["derive"] } -sp-core = { version = "2.0.0", path = "../core" } +sp-core = { version = "3.0.0", path = "../core" } [dev-dependencies] serde_json = "1.0.41" diff --git a/primitives/runtime-interface/Cargo.toml b/primitives/runtime-interface/Cargo.toml index bbf02578848e4..c4eb084f685c4 100644 --- a/primitives/runtime-interface/Cargo.toml +++ b/primitives/runtime-interface/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-runtime-interface" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,22 +14,22 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-wasm-interface = { version = "2.0.0", path = "../wasm-interface", default-features = false } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-tracing = { version = "2.0.0", default-features = false, path = "../tracing" } -sp-runtime-interface-proc-macro = { version = "2.0.0", path = "proc-macro" } -sp-externalities = { version = "0.8.0", optional = true, path = "../externalities" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } +sp-wasm-interface = { version = "3.0.0", path = "../wasm-interface", default-features = false } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-tracing = { version = "3.0.0", default-features = false, path = "../tracing" } +sp-runtime-interface-proc-macro = { version = "3.0.0", path = "proc-macro" } +sp-externalities = { version = "0.9.0", optional = true, path = "../externalities" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } static_assertions = "1.0.0" -primitive-types = { version = "0.8.0", default-features = false } -sp-storage = { version = "2.0.0", default-features = false, path = "../storage" } -impl-trait-for-tuples = "0.2.0" +primitive-types = { version = "0.9.0", default-features = false } +sp-storage = { version = "3.0.0", default-features = false, path = "../storage" } +impl-trait-for-tuples = "0.2.1" [dev-dependencies] sp-runtime-interface-test-wasm = { version = "2.0.0", path = "test-wasm" } -sp-state-machine = { version = "0.8.0", path = "../state-machine" } -sp-core = { version = "2.0.0", path = "../core" } -sp-io = { version = "2.0.0", path = "../io" } +sp-state-machine = { version = "0.9.0", path = "../state-machine" } +sp-core = { version = "3.0.0", path = "../core" } +sp-io = { version = "3.0.0", path = "../io" } rustversion = "1.0.0" trybuild = "1.0.38" diff --git a/primitives/runtime-interface/proc-macro/Cargo.toml b/primitives/runtime-interface/proc-macro/Cargo.toml index a63247758c3a0..51732ac631810 100644 --- a/primitives/runtime-interface/proc-macro/Cargo.toml +++ b/primitives/runtime-interface/proc-macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-runtime-interface-proc-macro" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/primitives/runtime-interface/src/pass_by.rs b/primitives/runtime-interface/src/pass_by.rs index e2a9b4ed42749..69485a1a2873f 100644 --- a/primitives/runtime-interface/src/pass_by.rs +++ b/primitives/runtime-interface/src/pass_by.rs @@ -238,7 +238,7 @@ impl PassByImpl for Codec { let (ptr, len) = unpack_ptr_and_len(arg); let vec = context.read_memory(Pointer::new(ptr), len)?; T::decode(&mut &vec[..]) - .map_err(|e| format!("Could not decode value from wasm: {}", e.what())) + .map_err(|e| format!("Could not decode value from wasm: {}", e)) } } diff --git a/primitives/runtime-interface/test-wasm-deprecated/Cargo.toml b/primitives/runtime-interface/test-wasm-deprecated/Cargo.toml index eba557de5dbab..91febf68ed285 100644 --- a/primitives/runtime-interface/test-wasm-deprecated/Cargo.toml +++ b/primitives/runtime-interface/test-wasm-deprecated/Cargo.toml @@ -13,13 +13,13 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-runtime-interface = { version = "2.0.0", default-features = false, path = "../" } -sp-std = { version = "2.0.0", default-features = false, path = "../../std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../io" } -sp-core = { version = "2.0.0", default-features = false, path = "../../core" } +sp-runtime-interface = { version = "3.0.0", default-features = false, path = "../" } +sp-std = { version = "3.0.0", default-features = false, path = "../../std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../io" } +sp-core = { version = "3.0.0", default-features = false, path = "../../core" } [build-dependencies] -substrate-wasm-builder = { version = "3.0.0", path = "../../../utils/wasm-builder" } +substrate-wasm-builder = { version = "4.0.0", path = "../../../utils/wasm-builder" } [features] default = [ "std" ] diff --git a/primitives/runtime-interface/test-wasm/Cargo.toml b/primitives/runtime-interface/test-wasm/Cargo.toml index 3cf36f95145e6..d0a61c5b920f4 100644 --- a/primitives/runtime-interface/test-wasm/Cargo.toml +++ b/primitives/runtime-interface/test-wasm/Cargo.toml @@ -13,13 +13,13 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-runtime-interface = { version = "2.0.0", default-features = false, path = "../" } -sp-std = { version = "2.0.0", default-features = false, path = "../../std" } -sp-io = { version = "2.0.0", default-features = false, path = "../../io" } -sp-core = { version = "2.0.0", default-features = false, path = "../../core" } +sp-runtime-interface = { version = "3.0.0", default-features = false, path = "../" } +sp-std = { version = "3.0.0", default-features = false, path = "../../std" } +sp-io = { version = "3.0.0", default-features = false, path = "../../io" } +sp-core = { version = "3.0.0", default-features = false, path = "../../core" } [build-dependencies] -substrate-wasm-builder = { version = "3.0.0", path = "../../../utils/wasm-builder" } +substrate-wasm-builder = { version = "4.0.0", path = "../../../utils/wasm-builder" } [features] default = [ "std" ] diff --git a/primitives/runtime-interface/test/Cargo.toml b/primitives/runtime-interface/test/Cargo.toml index fb000166ac5b0..f25183f021227 100644 --- a/primitives/runtime-interface/test/Cargo.toml +++ b/primitives/runtime-interface/test/Cargo.toml @@ -12,13 +12,13 @@ repository = "https://github.com/paritytech/substrate/" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-runtime-interface = { version = "2.0.0", path = "../" } -sc-executor = { version = "0.8.0", path = "../../../client/executor" } +sp-runtime-interface = { version = "3.0.0", path = "../" } +sc-executor = { version = "0.9.0", path = "../../../client/executor" } sp-runtime-interface-test-wasm = { version = "2.0.0", path = "../test-wasm" } sp-runtime-interface-test-wasm-deprecated = { version = "2.0.0", path = "../test-wasm-deprecated" } -sp-state-machine = { version = "0.8.0", path = "../../state-machine" } -sp-runtime = { version = "2.0.0", path = "../../runtime" } -sp-core = { version = "2.0.0", path = "../../core" } -sp-io = { version = "2.0.0", path = "../../io" } +sp-state-machine = { version = "0.9.0", path = "../../state-machine" } +sp-runtime = { version = "3.0.0", path = "../../runtime" } +sp-core = { version = "3.0.0", path = "../../core" } +sp-io = { version = "3.0.0", path = "../../io" } tracing = "0.1.22" tracing-core = "0.1.17" diff --git a/primitives/runtime-interface/test/src/lib.rs b/primitives/runtime-interface/test/src/lib.rs index 75aebf1caef73..4426997663485 100644 --- a/primitives/runtime-interface/test/src/lib.rs +++ b/primitives/runtime-interface/test/src/lib.rs @@ -44,6 +44,7 @@ fn call_wasm_method_with_result( Some(8), host_functions, 8, + None, ); executor.call_in_wasm( binary, diff --git a/primitives/runtime/Cargo.toml b/primitives/runtime/Cargo.toml index 9ce6a95c0c876..0e4f6168ba118 100644 --- a/primitives/runtime/Cargo.toml +++ b/primitives/runtime/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-runtime" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -16,24 +16,24 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", optional = true, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../application-crypto" } -sp-arithmetic = { version = "2.0.0", default-features = false, path = "../arithmetic" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-io = { version = "2.0.0", default-features = false, path = "../io" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../application-crypto" } +sp-arithmetic = { version = "3.0.0", default-features = false, path = "../arithmetic" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-io = { version = "3.0.0", default-features = false, path = "../io" } log = { version = "0.4.8", optional = true } -paste = "0.1.6" +paste = "1.0" rand = { version = "0.7.2", optional = true } -impl-trait-for-tuples = "0.2.0" -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } +impl-trait-for-tuples = "0.2.1" +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } hash256-std-hasher = { version = "0.15.2", default-features = false } either = { version = "1.5", default-features = false } [dev-dependencies] serde_json = "1.0.41" rand = "0.7.2" -sp-state-machine = { version = "0.8.0", path = "../state-machine" } +sp-state-machine = { version = "0.9.0", path = "../state-machine" } [features] bench = [] diff --git a/primitives/runtime/src/generic/era.rs b/primitives/runtime/src/generic/era.rs index 381c34ef419dc..5bee170048b5f 100644 --- a/primitives/runtime/src/generic/era.rs +++ b/primitives/runtime/src/generic/era.rs @@ -107,13 +107,13 @@ impl Era { } impl Encode for Era { - fn encode_to(&self, output: &mut T) { + fn encode_to(&self, output: &mut T) { match self { Era::Immortal => output.push_byte(0), Era::Mortal(period, phase) => { let quantize_factor = (*period as u64 >> 12).max(1); let encoded = (period.trailing_zeros() - 1).max(1).min(15) as u16 | ((phase / quantize_factor) << 4) as u16; - output.push(&encoded); + encoded.encode_to(output); } } } diff --git a/primitives/runtime/src/generic/header.rs b/primitives/runtime/src/generic/header.rs index 09f473e7d8192..62f9908fbe58d 100644 --- a/primitives/runtime/src/generic/header.rs +++ b/primitives/runtime/src/generic/header.rs @@ -106,12 +106,12 @@ impl Encode for Header where Hash: HashT, Hash::Output: Encode, { - fn encode_to(&self, dest: &mut T) { - dest.push(&self.parent_hash); - dest.push(&<<::Type as EncodeAsRef<_>>::RefType>::from(&self.number)); - dest.push(&self.state_root); - dest.push(&self.extrinsics_root); - dest.push(&self.digest); + fn encode_to(&self, dest: &mut T) { + self.parent_hash.encode_to(dest); + <<::Type as EncodeAsRef<_>>::RefType>::from(&self.number).encode_to(dest); + self.state_root.encode_to(dest); + self.extrinsics_root.encode_to(dest); + self.digest.encode_to(dest); } } diff --git a/primitives/runtime/src/testing.rs b/primitives/runtime/src/testing.rs index 3e72c25af9e95..b6d2641f01083 100644 --- a/primitives/runtime/src/testing.rs +++ b/primitives/runtime/src/testing.rs @@ -247,7 +247,7 @@ impl<'a, Xt> Deserialize<'a> for Block where Block: Decode { fn deserialize>(de: D) -> Result { let r = >::deserialize(de)?; Decode::decode(&mut &r[..]) - .map_err(|e| DeError::custom(format!("Invalid value passed into decode: {}", e.what()))) + .map_err(|e| DeError::custom(format!("Invalid value passed into decode: {}", e))) } } diff --git a/primitives/sandbox/Cargo.toml b/primitives/sandbox/Cargo.toml index 5ec8c203b54d7..9efe5cde7a426 100755 --- a/primitives/sandbox/Cargo.toml +++ b/primitives/sandbox/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-sandbox" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,11 +14,11 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] wasmi = { version = "0.6.2", optional = true } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-io = { version = "2.0.0", default-features = false, path = "../io" } -sp-wasm-interface = { version = "2.0.0", default-features = false, path = "../wasm-interface" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-io = { version = "3.0.0", default-features = false, path = "../io" } +sp-wasm-interface = { version = "3.0.0", default-features = false, path = "../wasm-interface" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } [dev-dependencies] wat = "1.0" diff --git a/primitives/serializer/Cargo.toml b/primitives/serializer/Cargo.toml index 670d8736400bf..51b53b43a40b7 100644 --- a/primitives/serializer/Cargo.toml +++ b/primitives/serializer/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-serializer" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/primitives/session/Cargo.toml b/primitives/session/Cargo.toml index d47a8062ef1a9..c04b271bc0370 100644 --- a/primitives/session/Cargo.toml +++ b/primitives/session/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-session" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,12 +13,12 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-api = { version = "2.0.0", default-features = false, path = "../api" } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-staking = { version = "2.0.0", default-features = false, path = "../staking" } -sp-runtime = { version = "2.0.0", optional = true, path = "../runtime" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-api = { version = "3.0.0", default-features = false, path = "../api" } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-staking = { version = "3.0.0", default-features = false, path = "../staking" } +sp-runtime = { version = "3.0.0", optional = true, path = "../runtime" } [features] default = [ "std" ] diff --git a/primitives/session/src/lib.rs b/primitives/session/src/lib.rs index 8000c23dd4311..9f63d64d414b0 100644 --- a/primitives/session/src/lib.rs +++ b/primitives/session/src/lib.rs @@ -113,7 +113,7 @@ pub fn generate_initial_session_keys( client: std::sync::Arc, at: &BlockId, seeds: Vec, -) -> Result<(), sp_api::ApiErrorFor> +) -> Result<(), sp_api::ApiError> where Block: BlockT, T: ProvideRuntimeApi, diff --git a/primitives/staking/Cargo.toml b/primitives/staking/Cargo.toml index fbe4b30f00b8a..cf2347082a885 100644 --- a/primitives/staking/Cargo.toml +++ b/primitives/staking/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-staking" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,9 +13,9 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } [features] default = ["std"] diff --git a/primitives/state-machine/Cargo.toml b/primitives/state-machine/Cargo.toml index c594c27fc7a24..9db850cfe0b96 100644 --- a/primitives/state-machine/Cargo.toml +++ b/primitives/state-machine/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-state-machine" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Substrate State Machine" edition = "2018" @@ -20,19 +20,19 @@ parking_lot = { version = "0.11.1", optional = true } hash-db = { version = "0.15.2", default-features = false } trie-db = { version = "0.22.2", default-features = false } trie-root = { version = "0.16.0", default-features = false } -sp-trie = { version = "2.0.0", path = "../trie", default-features = false } -sp-core = { version = "2.0.0", path = "../core", default-features = false } -sp-panic-handler = { version = "2.0.0", path = "../panic-handler", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } +sp-trie = { version = "3.0.0", path = "../trie", default-features = false } +sp-core = { version = "3.0.0", path = "../core", default-features = false } +sp-panic-handler = { version = "3.0.0", path = "../panic-handler", optional = true } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } num-traits = { version = "0.2.8", default-features = false } rand = { version = "0.7.2", optional = true } -sp-externalities = { version = "0.8.0", path = "../externalities", default-features = false } +sp-externalities = { version = "0.9.0", path = "../externalities", default-features = false } smallvec = "1.4.1" -sp-std = { version = "2.0.0", default-features = false, path = "../std" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } [dev-dependencies] hex-literal = "0.3.1" -sp-runtime = { version = "2.0.0", path = "../runtime" } +sp-runtime = { version = "3.0.0", path = "../runtime" } pretty_assertions = "0.6.1" [features] diff --git a/primitives/state-machine/src/basic.rs b/primitives/state-machine/src/basic.rs index 3b265208136ac..dda8f523b77f9 100644 --- a/primitives/state-machine/src/basic.rs +++ b/primitives/state-machine/src/basic.rs @@ -211,9 +211,9 @@ impl Externalities for BasicExternalities { &mut self, child_info: &ChildInfo, _limit: Option, - ) -> bool { - self.inner.children_default.remove(child_info.storage_key()); - true + ) -> (bool, u32) { + let num_removed = self.inner.children_default.remove(child_info.storage_key()).map(|c| c.data.len()).unwrap_or(0); + (true, num_removed as u32) } fn clear_prefix(&mut self, prefix: &[u8]) { @@ -411,6 +411,29 @@ mod tests { assert_eq!(ext.child_storage(child_info, b"doe"), None); } + #[test] + fn kill_child_storage_returns_num_elements_removed() { + let child_info = ChildInfo::new_default(b"storage_key"); + let child_info = &child_info; + let mut ext = BasicExternalities::new(Storage { + top: Default::default(), + children_default: map![ + child_info.storage_key().to_vec() => StorageChild { + data: map![ + b"doe".to_vec() => b"reindeer".to_vec(), + b"dog".to_vec() => b"puppy".to_vec(), + b"hello".to_vec() => b"world".to_vec(), + ], + child_info: child_info.to_owned(), + } + ] + }); + + + let res = ext.kill_child_storage(child_info, None); + assert_eq!(res, (true, 3)); + } + #[test] fn basic_externalities_is_empty() { // Make sure no values are set by default in `BasicExternalities`. diff --git a/primitives/state-machine/src/changes_trie/input.rs b/primitives/state-machine/src/changes_trie/input.rs index 3702eefb99648..85a8de0b78d81 100644 --- a/primitives/state-machine/src/changes_trie/input.rs +++ b/primitives/state-machine/src/changes_trie/input.rs @@ -123,7 +123,7 @@ impl ExtrinsicIndex { } impl Encode for ExtrinsicIndex { - fn encode_to(&self, dest: &mut W) { + fn encode_to(&self, dest: &mut W) { dest.push_byte(1); self.block.encode_to(dest); self.key.encode_to(dest); @@ -142,7 +142,7 @@ impl DigestIndex { impl Encode for DigestIndex { - fn encode_to(&self, dest: &mut W) { + fn encode_to(&self, dest: &mut W) { dest.push_byte(2); self.block.encode_to(dest); self.key.encode_to(dest); @@ -158,7 +158,7 @@ impl ChildIndex { } impl Encode for ChildIndex { - fn encode_to(&self, dest: &mut W) { + fn encode_to(&self, dest: &mut W) { dest.push_byte(3); self.block.encode_to(dest); self.storage_key.encode_to(dest); diff --git a/primitives/state-machine/src/ext.rs b/primitives/state-machine/src/ext.rs index 1e64cd74bc1b3..7907cda6fb4e7 100644 --- a/primitives/state-machine/src/ext.rs +++ b/primitives/state-machine/src/ext.rs @@ -391,7 +391,7 @@ where &mut self, child_info: &ChildInfo, limit: Option, - ) -> bool { + ) -> (bool, u32) { trace!(target: "state", "{:04x}: KillChild({})", self.id, HexDisplay::from(&child_info.storage_key()), @@ -399,9 +399,9 @@ where let _guard = guard(); self.mark_dirty(); self.overlay.clear_child_storage(child_info); + let mut num_deleted: u32 = 0; if let Some(limit) = limit { - let mut num_deleted: u32 = 0; let mut all_deleted = true; self.backend.apply_to_child_keys_while(child_info, |key| { if num_deleted == limit { @@ -417,13 +417,14 @@ where self.overlay.set_child_storage(child_info, key.to_vec(), None); true }); - all_deleted + (all_deleted, num_deleted) } else { self.backend.apply_to_child_keys_while(child_info, |key| { + num_deleted = num_deleted.saturating_add(1); self.overlay.set_child_storage(child_info, key.to_vec(), None); true }); - true + (true, num_deleted) } } @@ -575,27 +576,41 @@ where #[cfg(feature = "std")] fn storage_changes_root(&mut self, parent_hash: &[u8]) -> Result>, ()> { let _guard = guard(); - let root = self.overlay.changes_trie_root( - self.backend, - self.changes_trie_state.as_ref(), - Decode::decode(&mut &parent_hash[..]).map_err(|e| - trace!( - target: "state", - "Failed to decode changes root parent hash: {}", - e, - ) - )?, - true, - self.storage_transaction_cache, - ); + if let Some(ref root) = self.storage_transaction_cache.changes_trie_transaction_storage_root { + trace!( + target: "state", + "{:04x}: ChangesRoot({})(cached) {:?}", + self.id, + HexDisplay::from(&parent_hash), + root, + ); - trace!(target: "state", "{:04x}: ChangesRoot({}) {:?}", - self.id, - HexDisplay::from(&parent_hash), - root, - ); + Ok(Some(root.encode())) + } else { + let root = self.overlay.changes_trie_root( + self.backend, + self.changes_trie_state.as_ref(), + Decode::decode(&mut &parent_hash[..]).map_err(|e| + trace!( + target: "state", + "Failed to decode changes root parent hash: {}", + e, + ) + )?, + true, + self.storage_transaction_cache, + ); - root.map(|r| r.map(|o| o.encode())) + trace!( + target: "state", + "{:04x}: ChangesRoot({}) {:?}", + self.id, + HexDisplay::from(&parent_hash), + root, + ); + + root.map(|r| r.map(|o| o.encode())) + } } fn storage_start_transaction(&mut self) { diff --git a/primitives/state-machine/src/lib.rs b/primitives/state-machine/src/lib.rs index 31d4eacc4e586..0167633d48070 100644 --- a/primitives/state-machine/src/lib.rs +++ b/primitives/state-machine/src/lib.rs @@ -392,7 +392,7 @@ mod execution { bool, ) where R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result> + UnwindSafe, { let mut cache = StorageTransactionCache::default(); @@ -449,7 +449,7 @@ mod execution { ) -> CallResult where R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result> + UnwindSafe, Handler: FnOnce( CallResult, CallResult, @@ -485,7 +485,7 @@ mod execution { ) -> CallResult where R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result> + UnwindSafe, { self.overlay.start_transaction(); let (result, was_native) = self.execute_aux( @@ -522,7 +522,7 @@ mod execution { ) -> Result, Box> where R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result> + UnwindSafe, Handler: FnOnce( CallResult, CallResult, @@ -869,7 +869,7 @@ mod tests { map, traits::{Externalities, RuntimeCode}, testing::TaskExecutor, }; use sp_runtime::traits::BlakeTwo256; - use std::{result, collections::HashMap}; + use std::{result, collections::HashMap, panic::UnwindSafe}; use codec::Decode; use sp_core::{ storage::ChildInfo, NativeOrEncoded, NeverNativeValue, @@ -891,7 +891,7 @@ mod tests { fn call< R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result, + NC: FnOnce() -> result::Result> + UnwindSafe, >( &self, ext: &mut dyn Externalities, @@ -1159,7 +1159,7 @@ mod tests { changes_trie::disabled_state::<_, u64>(), None, ); - assert_eq!(ext.kill_child_storage(&child_info, Some(2)), false); + assert_eq!(ext.kill_child_storage(&child_info, Some(2)), (false, 2)); } assert_eq!( @@ -1199,12 +1199,14 @@ mod tests { changes_trie::disabled_state::<_, u64>(), None, ); - assert_eq!(ext.kill_child_storage(&child_info, Some(0)), false); - assert_eq!(ext.kill_child_storage(&child_info, Some(1)), false); - assert_eq!(ext.kill_child_storage(&child_info, Some(2)), false); - assert_eq!(ext.kill_child_storage(&child_info, Some(3)), false); - assert_eq!(ext.kill_child_storage(&child_info, Some(4)), true); - assert_eq!(ext.kill_child_storage(&child_info, Some(5)), true); + assert_eq!(ext.kill_child_storage(&child_info, Some(0)), (false, 0)); + assert_eq!(ext.kill_child_storage(&child_info, Some(1)), (false, 1)); + assert_eq!(ext.kill_child_storage(&child_info, Some(2)), (false, 2)); + assert_eq!(ext.kill_child_storage(&child_info, Some(3)), (false, 3)); + assert_eq!(ext.kill_child_storage(&child_info, Some(4)), (true, 4)); + // Only 4 items to remove + assert_eq!(ext.kill_child_storage(&child_info, Some(5)), (true, 4)); + assert_eq!(ext.kill_child_storage(&child_info, None), (true, 4)); } #[test] diff --git a/primitives/state-machine/src/overlayed_changes/mod.rs b/primitives/state-machine/src/overlayed_changes/mod.rs index b529c0ebfaeeb..285bf2a73a148 100644 --- a/primitives/state-machine/src/overlayed_changes/mod.rs +++ b/primitives/state-machine/src/overlayed_changes/mod.rs @@ -370,7 +370,7 @@ impl OverlayedChanges { /// transaction was open. Any transaction must be closed by either `rollback_transaction` or /// `commit_transaction` before this overlay can be converted into storage changes. /// - /// Changes made without any open transaction are committed immediatly. + /// Changes made without any open transaction are committed immediately. pub fn start_transaction(&mut self) { self.top.start_transaction(); for (_, (changeset, _)) in self.children.iter_mut() { diff --git a/primitives/state-machine/src/read_only.rs b/primitives/state-machine/src/read_only.rs index dee7c9e337cda..296520900c952 100644 --- a/primitives/state-machine/src/read_only.rs +++ b/primitives/state-machine/src/read_only.rs @@ -132,7 +132,7 @@ impl<'a, H: Hasher, B: 'a + Backend> Externalities for ReadOnlyExternalities< &mut self, _child_info: &ChildInfo, _limit: Option, - ) -> bool { + ) -> (bool, u32) { unimplemented!("kill_child_storage is not supported in ReadOnlyExternalities") } diff --git a/primitives/state-machine/src/testing.rs b/primitives/state-machine/src/testing.rs index a6f9d06824642..f4b0cb6592ce2 100644 --- a/primitives/state-machine/src/testing.rs +++ b/primitives/state-machine/src/testing.rs @@ -48,20 +48,22 @@ pub struct TestExternalities where H::Out: codec::Codec + Ord, { + /// The overlay changed storage. overlay: OverlayedChanges, offchain_db: TestPersistentOffchainDB, - storage_transaction_cache: StorageTransactionCache< - as Backend>::Transaction, H, N - >, - backend: InMemoryBackend, + storage_transaction_cache: + StorageTransactionCache< as Backend>::Transaction, H, N>, + /// Storage backend. + pub backend: InMemoryBackend, changes_trie_config: Option, changes_trie_storage: ChangesTrieInMemoryStorage, - extensions: Extensions, + /// Extensions. + pub extensions: Extensions, } impl TestExternalities - where - H::Out: Ord + 'static + codec::Codec +where + H::Out: Ord + 'static + codec::Codec, { /// Get externalities implementation. pub fn ext(&mut self) -> Ext> { @@ -324,7 +326,7 @@ mod tests { { let mut ext = ext.ext(); - assert!(!ext.kill_child_storage(&child_info, Some(2)), "Should not delete all keys"); + assert!(!ext.kill_child_storage(&child_info, Some(2)).0, "Should not delete all keys"); assert!(ext.child_storage(&child_info, &b"doe"[..]).is_none()); assert!(ext.child_storage(&child_info, &b"dog"[..]).is_none()); diff --git a/primitives/std/Cargo.toml b/primitives/std/Cargo.toml index 91edfc9732639..bafa1ea7ef414 100644 --- a/primitives/std/Cargo.toml +++ b/primitives/std/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-std" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/primitives/storage/Cargo.toml b/primitives/storage/Cargo.toml index b025b5a106715..7a984d9205690 100644 --- a/primitives/storage/Cargo.toml +++ b/primitives/storage/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-storage" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" description = "Storage related primitives" @@ -14,12 +14,12 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-std = { version = "2.0.0", default-features = false, path = "../std" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } serde = { version = "1.0.101", optional = true, features = ["derive"] } impl-serde = { version = "0.3.1", optional = true } ref-cast = "1.0.0" -sp-debug-derive = { version = "2.0.0", path = "../debug-derive" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +sp-debug-derive = { version = "3.0.0", path = "../debug-derive" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } [features] default = [ "std" ] diff --git a/primitives/storage/src/lib.rs b/primitives/storage/src/lib.rs index 1e9f9766072e4..1016b73eb1e3b 100644 --- a/primitives/storage/src/lib.rs +++ b/primitives/storage/src/lib.rs @@ -31,10 +31,15 @@ use codec::{Encode, Decode}; #[derive(PartialEq, Eq, RuntimeDebug)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash, PartialOrd, Ord, Clone))] pub struct StorageKey( - #[cfg_attr(feature = "std", serde(with="impl_serde::serialize"))] - pub Vec, + #[cfg_attr(feature = "std", serde(with = "impl_serde::serialize"))] pub Vec, ); +impl AsRef<[u8]> for StorageKey { + fn as_ref(&self) -> &[u8] { + self.0.as_ref() + } +} + /// Storage key with read/write tracking information. #[derive(PartialEq, Eq, RuntimeDebug, Clone, Encode, Decode)] #[cfg_attr(feature = "std", derive(Hash, PartialOrd, Ord))] diff --git a/primitives/tasks/Cargo.toml b/primitives/tasks/Cargo.toml index 7ad5e6dd51395..0a361b6c8dbbc 100644 --- a/primitives/tasks/Cargo.toml +++ b/primitives/tasks/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-tasks" -version = "2.0.0" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,14 +15,14 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] log = { version = "0.4.8", optional = true } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } -sp-externalities = { version = "0.8.0", optional = true, path = "../externalities" } -sp-io = { version = "2.0.0", default-features = false, path = "../io" } -sp-runtime-interface = { version = "2.0.0", default-features = false, path = "../runtime-interface" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } +sp-externalities = { version = "0.9.0", optional = true, path = "../externalities" } +sp-io = { version = "3.0.0", default-features = false, path = "../io" } +sp-runtime-interface = { version = "3.0.0", default-features = false, path = "../runtime-interface" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } [dev-dependencies] -codec = { package = "parity-scale-codec", default-features = false, version = "1.3.6" } +codec = { package = "parity-scale-codec", default-features = false, version = "2.0.0" } [features] default = ["std"] diff --git a/primitives/tasks/src/async_externalities.rs b/primitives/tasks/src/async_externalities.rs index 249222ec71c3d..5d99ca4368d0b 100644 --- a/primitives/tasks/src/async_externalities.rs +++ b/primitives/tasks/src/async_externalities.rs @@ -119,7 +119,7 @@ impl Externalities for AsyncExternalities { &mut self, _child_info: &ChildInfo, _limit: Option, - ) -> bool { + ) -> (bool, u32) { panic!("`kill_child_storage`: should not be used in async externalities!") } diff --git a/primitives/test-primitives/Cargo.toml b/primitives/test-primitives/Cargo.toml index 1bfb793610b64..fbf29db96fa46 100644 --- a/primitives/test-primitives/Cargo.toml +++ b/primitives/test-primitives/Cargo.toml @@ -12,12 +12,12 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../application-crypto" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../application-crypto" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } serde = { version = "1.0.101", optional = true, features = ["derive"] } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } [features] default = [ diff --git a/primitives/timestamp/Cargo.toml b/primitives/timestamp/Cargo.toml index de1271b0dd027..53fb37d4deb43 100644 --- a/primitives/timestamp/Cargo.toml +++ b/primitives/timestamp/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-timestamp" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,12 +13,12 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-api = { version = "2.0.0", default-features = false, path = "../api" } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-inherents = { version = "2.0.0", default-features = false, path = "../inherents" } -impl-trait-for-tuples = "0.2.0" +sp-api = { version = "3.0.0", default-features = false, path = "../api" } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-inherents = { version = "3.0.0", default-features = false, path = "../inherents" } +impl-trait-for-tuples = "0.2.1" wasm-timer = { version = "0.2", optional = true } [features] diff --git a/primitives/tracing/Cargo.toml b/primitives/tracing/Cargo.toml index 31527b204963f..13804353eca76 100644 --- a/primitives/tracing/Cargo.toml +++ b/primitives/tracing/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-tracing" -version = "2.0.1" +version = "3.0.0" license = "Apache-2.0" authors = ["Parity Technologies "] edition = "2018" @@ -18,8 +18,8 @@ features = ["with-tracing"] targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"] [dependencies] -sp-std = { version = "2.0.0", path = "../std", default-features = false} -codec = { version = "1.3.1", package = "parity-scale-codec", default-features = false, features = ["derive"]} +sp-std = { version = "3.0.0", path = "../std", default-features = false} +codec = { version = "2.0.0", package = "parity-scale-codec", default-features = false, features = ["derive"]} tracing = { version = "0.1.22", default-features = false } tracing-core = { version = "0.1.17", default-features = false } log = { version = "0.4.8", optional = true } diff --git a/primitives/transaction-pool/Cargo.toml b/primitives/transaction-pool/Cargo.toml index 675987e3a127c..d431e444d457e 100644 --- a/primitives/transaction-pool/Cargo.toml +++ b/primitives/transaction-pool/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-transaction-pool" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,14 +15,14 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] thiserror = { version = "1.0.21", optional = true } -codec = { package = "parity-scale-codec", version = "1.3.6", optional = true } +codec = { package = "parity-scale-codec", version = "2.0.0", optional = true } derive_more = { version = "0.99.11", optional = true } futures = { version = "0.3.1", optional = true } log = { version = "0.4.8", optional = true } serde = { version = "1.0.101", features = ["derive"], optional = true} -sp-api = { version = "2.0.0", default-features = false, path = "../api" } -sp-blockchain = { version = "2.0.0", optional = true, path = "../blockchain" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } +sp-api = { version = "3.0.0", default-features = false, path = "../api" } +sp-blockchain = { version = "3.0.0", optional = true, path = "../blockchain" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } [features] default = [ "std" ] diff --git a/primitives/trie/Cargo.toml b/primitives/trie/Cargo.toml index 4392f01d222aa..4396550a48a8f 100644 --- a/primitives/trie/Cargo.toml +++ b/primitives/trie/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-trie" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] description = "Patricia trie stuff using a parity-scale-codec node format" repository = "https://github.com/paritytech/substrate/" @@ -18,20 +18,20 @@ name = "bench" harness = false [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } hash-db = { version = "0.15.2", default-features = false } trie-db = { version = "0.22.2", default-features = false } trie-root = { version = "0.16.0", default-features = false } -memory-db = { version = "0.25.0", default-features = false } -sp-core = { version = "2.0.0", default-features = false, path = "../core" } +memory-db = { version = "0.26.0", default-features = false } +sp-core = { version = "3.0.0", default-features = false, path = "../core" } [dev-dependencies] -trie-bench = "0.26.0" +trie-bench = "0.27.0" trie-standardmap = "0.15.2" criterion = "0.3.3" hex-literal = "0.3.1" -sp-runtime = { version = "2.0.0", path = "../runtime" } +sp-runtime = { version = "3.0.0", path = "../runtime" } [features] default = ["std"] diff --git a/primitives/trie/src/error.rs b/primitives/trie/src/error.rs index 453f74afeb818..8e1d9b974ffd5 100644 --- a/primitives/trie/src/error.rs +++ b/primitives/trie/src/error.rs @@ -49,7 +49,7 @@ impl StdError for Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Error::Decode(e) => write!(f, "Decode error: {}", e.what()), + Error::Decode(e) => write!(f, "Decode error: {}", e), Error::BadFormat => write!(f, "Bad format"), } } diff --git a/primitives/trie/src/node_header.rs b/primitives/trie/src/node_header.rs index 14a998903d697..0fdf6fefbd0bc 100644 --- a/primitives/trie/src/node_header.rs +++ b/primitives/trie/src/node_header.rs @@ -38,7 +38,7 @@ pub(crate) enum NodeKind { } impl Encode for NodeHeader { - fn encode_to(&self, output: &mut T) { + fn encode_to(&self, output: &mut T) { match self { NodeHeader::Null => output.push_byte(trie_constants::EMPTY_TRIE), NodeHeader::Branch(true, nibble_count) => @@ -99,7 +99,7 @@ pub(crate) fn size_and_prefix_iterator(size: usize, prefix: u8) -> impl Iterator } /// Encodes size and prefix to a stream output. -fn encode_size_and_prefix(size: usize, prefix: u8, out: &mut impl Output) { +fn encode_size_and_prefix(size: usize, prefix: u8, out: &mut W) { for b in size_and_prefix_iterator(size, prefix) { out.push_byte(b) } diff --git a/primitives/utils/Cargo.toml b/primitives/utils/Cargo.toml index b42c92abad922..7669cee346d0b 100644 --- a/primitives/utils/Cargo.toml +++ b/primitives/utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-utils" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,7 +13,7 @@ readme = "README.md" futures = "0.3.9" futures-core = "0.3.4" lazy_static = "1.4.0" -prometheus = { version = "0.10.0", default-features = false } +prometheus = { version = "0.11.0", default-features = false } futures-timer = "3.0.2" [features] diff --git a/primitives/version/Cargo.toml b/primitives/version/Cargo.toml index 113639434d5bc..bfb9a742c8689 100644 --- a/primitives/version/Cargo.toml +++ b/primitives/version/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-version" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -17,9 +17,9 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] impl-serde = { version = "0.3.1", optional = true } serde = { version = "1.0.101", optional = true, features = ["derive"] } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -sp-std = { version = "2.0.0", default-features = false, path = "../std" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-std = { version = "3.0.0", default-features = false, path = "../std" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" } [features] default = ["std"] diff --git a/primitives/wasm-interface/Cargo.toml b/primitives/wasm-interface/Cargo.toml index 32c283a8527fa..1721df4a86685 100644 --- a/primitives/wasm-interface/Cargo.toml +++ b/primitives/wasm-interface/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-wasm-interface" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -15,9 +15,9 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] wasmi = { version = "0.6.2", optional = true } -impl-trait-for-tuples = "0.2.0" -sp-std = { version = "2.0.0", path = "../std", default-features = false } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } +impl-trait-for-tuples = "0.2.1" +sp-std = { version = "3.0.0", path = "../std", default-features = false } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } [features] default = [ "std" ] diff --git a/ss58-registry.json b/ss58-registry.json index 4501571fa322b..d65485daeb19d 100644 --- a/ss58-registry.json +++ b/ss58-registry.json @@ -21,8 +21,8 @@ }, { "prefix": 1, - "network": "reserved1", - "displayName": "This prefix is reserved.", + "network": null, + "displayName": "Bare 32-bit Schnorr/Ristretto (S/R 25519) public key.", "symbols": null, "decimals": null, "standardAccount": null, @@ -39,8 +39,8 @@ }, { "prefix": 3, - "network": "reserved3", - "displayName": "This prefix is reserved.", + "network": null, + "displayName": "Bare 32-bit Ed25519 public key.", "symbols": null, "decimals": null, "standardAccount": null, @@ -316,6 +316,15 @@ "standardAccount": "*25519", "website": null }, + { + "prefix": 34, + "network": "ares", + "displayName": "Ares Protocol", + "symbols": ["ARES"], + "decimals": [12], + "standardAccount": "*25519", + "website": "https://www.aresprotocol.com/" + }, { "prefix": 35, "network": "vln", @@ -390,8 +399,8 @@ }, { "prefix": 43, - "network": "reserved43", - "displayName": "This prefix is reserved.", + "network": null, + "displayName": "Bare 32-bit ECDSA SECP-256k1 public key.", "symbols": null, "decimals": null, "standardAccount": null, @@ -435,12 +444,30 @@ }, { "prefix": 48, - "network": "reserved48", - "displayName": "All prefixes 48 and higher are reserved and cannot be allocated.", - "symbols": null, - "decimals": null, - "standardAccount": null, - "website": null + "network": "neatcoin", + "displayName": "Neatcoin Mainnet", + "symbols": ["NEAT"], + "decimals": [12], + "standardAccount": "*25519", + "website": "https://neatcoin.org" + }, + { + "prefix": 65, + "network": "aventus", + "displayName": "AvN Mainnet", + "symbols": ["AVT"], + "decimals": [18], + "standardAccount": "*25519", + "website": "https://aventus.io" + }, + { + "prefix": 66, + "network": "crust", + "displayName": "Crust Network", + "symbols": ["CRU"], + "decimals": [12], + "standardAccount": "*25519", + "website": "https://crust.network" } ] } diff --git a/test-utils/Cargo.toml b/test-utils/Cargo.toml index 1b15c34401feb..0a8849fe98a72 100644 --- a/test-utils/Cargo.toml +++ b/test-utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-test-utils" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,9 +13,9 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] futures = { version = "0.3.1", features = ["compat"] } -substrate-test-utils-derive = { version = "0.8.0", path = "./derive" } +substrate-test-utils-derive = { version = "0.9.0", path = "./derive" } tokio = { version = "0.2.13", features = ["macros"] } [dev-dependencies] -sc-service = { version = "0.8.0", path = "../client/service" } +sc-service = { version = "0.9.0", path = "../client/service" } trybuild = { version = "1.0.38", features = [ "diff" ] } diff --git a/test-utils/client/Cargo.toml b/test-utils/client/Cargo.toml index fad66c5a6708d..1f62e32ddf596 100644 --- a/test-utils/client/Cargo.toml +++ b/test-utils/client/Cargo.toml @@ -12,23 +12,23 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } futures = "0.3.9" futures01 = { package = "futures", version = "0.1.29" } hash-db = "0.15.2" hex = "0.4" serde = "1.0.55" serde_json = "1.0.55" -sc-client-api = { version = "2.0.0", path = "../../client/api" } -sc-client-db = { version = "0.8.0", features = ["test-helpers"], path = "../../client/db" } -sc-consensus = { version = "0.8.0", path = "../../client/consensus/common" } -sc-executor = { version = "0.8.0", path = "../../client/executor" } -sc-light = { version = "2.0.0", path = "../../client/light" } -sc-service = { version = "0.8.0", default-features = false, features = ["test-helpers"], path = "../../client/service" } -sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" } -sp-consensus = { version = "0.8.0", path = "../../primitives/consensus/common" } -sp-core = { version = "2.0.0", path = "../../primitives/core" } -sp-keystore = { version = "0.8.0", path = "../../primitives/keystore" } -sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" } -sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } -sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine" } +sc-client-api = { version = "3.0.0", path = "../../client/api" } +sc-client-db = { version = "0.9.0", features = ["test-helpers"], path = "../../client/db" } +sc-consensus = { version = "0.9.0", path = "../../client/consensus/common" } +sc-executor = { version = "0.9.0", path = "../../client/executor" } +sc-light = { version = "3.0.0", path = "../../client/light" } +sc-service = { version = "0.9.0", default-features = false, features = ["test-helpers"], path = "../../client/service" } +sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } +sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-keystore = { version = "0.9.0", path = "../../primitives/keystore" } +sp-keyring = { version = "3.0.0", path = "../../primitives/keyring" } +sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } +sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" } diff --git a/test-utils/derive/Cargo.toml b/test-utils/derive/Cargo.toml index 1fb1db555f490..8f9a37f8dba6c 100644 --- a/test-utils/derive/Cargo.toml +++ b/test-utils/derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-test-utils-derive" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/test-utils/runtime/Cargo.toml b/test-utils/runtime/Cargo.toml index c99ec9a05e7c4..1a841ac0755a4 100644 --- a/test-utils/runtime/Cargo.toml +++ b/test-utils/runtime/Cargo.toml @@ -13,50 +13,50 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sp-application-crypto = { version = "2.0.0", default-features = false, path = "../../primitives/application-crypto" } -sp-consensus-aura = { version = "0.8.0", default-features = false, path = "../../primitives/consensus/aura" } -sp-consensus-babe = { version = "0.8.0", default-features = false, path = "../../primitives/consensus/babe" } -sp-block-builder = { version = "2.0.0", default-features = false, path = "../../primitives/block-builder" } -codec = { package = "parity-scale-codec", version = "1.3.6", default-features = false, features = ["derive"] } -frame-executive = { version = "2.0.0", default-features = false, path = "../../frame/executive" } -sp-inherents = { version = "2.0.0", default-features = false, path = "../../primitives/inherents" } -sp-keyring = { version = "2.0.0", optional = true, path = "../../primitives/keyring" } -memory-db = { version = "0.25.0", default-features = false } -sp-offchain = { path = "../../primitives/offchain", default-features = false, version = "2.0.0"} -sp-core = { version = "2.0.0", default-features = false, path = "../../primitives/core" } -sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } -sp-runtime-interface = { path = "../../primitives/runtime-interface", default-features = false, version = "2.0.0"} -sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } -frame-support = { version = "2.0.0", default-features = false, path = "../../frame/support" } -sp-version = { version = "2.0.0", default-features = false, path = "../../primitives/version" } -sp-session = { version = "2.0.0", default-features = false, path = "../../primitives/session" } -sp-api = { version = "2.0.0", default-features = false, path = "../../primitives/api" } -sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } -pallet-babe = { version = "2.0.0", default-features = false, path = "../../frame/babe" } -frame-system = { version = "2.0.0", default-features = false, path = "../../frame/system" } -frame-system-rpc-runtime-api = { version = "2.0.0", default-features = false, path = "../../frame/system/rpc/runtime-api" } -pallet-timestamp = { version = "2.0.0", default-features = false, path = "../../frame/timestamp" } -sp-finality-grandpa = { version = "2.0.0", default-features = false, path = "../../primitives/finality-grandpa" } -sp-trie = { version = "2.0.0", default-features = false, path = "../../primitives/trie" } -sp-transaction-pool = { version = "2.0.0", default-features = false, path = "../../primitives/transaction-pool" } +sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../primitives/application-crypto" } +sp-consensus-aura = { version = "0.9.0", default-features = false, path = "../../primitives/consensus/aura" } +sp-consensus-babe = { version = "0.9.0", default-features = false, path = "../../primitives/consensus/babe" } +sp-block-builder = { version = "3.0.0", default-features = false, path = "../../primitives/block-builder" } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +frame-executive = { version = "3.0.0", default-features = false, path = "../../frame/executive" } +sp-inherents = { version = "3.0.0", default-features = false, path = "../../primitives/inherents" } +sp-keyring = { version = "3.0.0", optional = true, path = "../../primitives/keyring" } +memory-db = { version = "0.26.0", default-features = false } +sp-offchain = { path = "../../primitives/offchain", default-features = false, version = "3.0.0"} +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-runtime-interface = { path = "../../primitives/runtime-interface", default-features = false, version = "3.0.0"} +sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" } +frame-support = { version = "3.0.0", default-features = false, path = "../../frame/support" } +sp-version = { version = "3.0.0", default-features = false, path = "../../primitives/version" } +sp-session = { version = "3.0.0", default-features = false, path = "../../primitives/session" } +sp-api = { version = "3.0.0", default-features = false, path = "../../primitives/api" } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +pallet-babe = { version = "3.0.0", default-features = false, path = "../../frame/babe" } +frame-system = { version = "3.0.0", default-features = false, path = "../../frame/system" } +frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../frame/system/rpc/runtime-api" } +pallet-timestamp = { version = "3.0.0", default-features = false, path = "../../frame/timestamp" } +sp-finality-grandpa = { version = "3.0.0", default-features = false, path = "../../primitives/finality-grandpa" } +sp-trie = { version = "3.0.0", default-features = false, path = "../../primitives/trie" } +sp-transaction-pool = { version = "3.0.0", default-features = false, path = "../../primitives/transaction-pool" } trie-db = { version = "0.22.2", default-features = false } -parity-util-mem = { version = "0.8.0", default-features = false, features = ["primitive-types"] } -sc-service = { version = "0.8.0", default-features = false, optional = true, features = ["test-helpers"], path = "../../client/service" } -sp-state-machine = { version = "0.8.0", default-features = false, path = "../../primitives/state-machine" } -sp-externalities = { version = "0.8.0", default-features = false, path = "../../primitives/externalities" } +parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } +sc-service = { version = "0.9.0", default-features = false, optional = true, features = ["test-helpers"], path = "../../client/service" } +sp-state-machine = { version = "0.9.0", default-features = false, path = "../../primitives/state-machine" } +sp-externalities = { version = "0.9.0", default-features = false, path = "../../primitives/externalities" } # 3rd party -cfg-if = "0.1.10" +cfg-if = "1.0" log = { version = "0.4.8", optional = true } serde = { version = "1.0.101", optional = true, features = ["derive"] } [dev-dependencies] -sc-block-builder = { version = "0.8.0", path = "../../client/block-builder" } -sc-executor = { version = "0.8.0", path = "../../client/executor" } +sc-block-builder = { version = "0.9.0", path = "../../client/block-builder" } +sc-executor = { version = "0.9.0", path = "../../client/executor" } substrate-test-runtime-client = { version = "2.0.0", path = "./client" } [build-dependencies] -substrate-wasm-builder = { version = "3.0.0", path = "../../utils/wasm-builder" } +substrate-wasm-builder = { version = "4.0.0", path = "../../utils/wasm-builder" } [features] default = [ diff --git a/test-utils/runtime/client/Cargo.toml b/test-utils/runtime/client/Cargo.toml index 2540e29c8b0e2..0c822f0cdff84 100644 --- a/test-utils/runtime/client/Cargo.toml +++ b/test-utils/runtime/client/Cargo.toml @@ -12,17 +12,17 @@ publish = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sc-light = { version = "2.0.0", path = "../../../client/light" } -sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" } -sc-block-builder = { version = "0.8.0", path = "../../../client/block-builder" } +sc-light = { version = "3.0.0", path = "../../../client/light" } +sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sc-block-builder = { version = "0.9.0", path = "../../../client/block-builder" } substrate-test-client = { version = "2.0.0", path = "../../client" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } substrate-test-runtime = { version = "2.0.0", path = "../../runtime" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-api = { version = "2.0.0", path = "../../../primitives/api" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -codec = { package = "parity-scale-codec", version = "1.3.6" } -sc-client-api = { version = "2.0.0", path = "../../../client/api" } -sc-consensus = { version = "0.8.0", path = "../../../client/consensus/common" } -sc-service = { version = "0.8.0", default-features = false, path = "../../../client/service" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +codec = { package = "parity-scale-codec", version = "2.0.0" } +sc-client-api = { version = "3.0.0", path = "../../../client/api" } +sc-consensus = { version = "0.9.0", path = "../../../client/consensus/common" } +sc-service = { version = "0.9.0", default-features = false, path = "../../../client/service" } futures = "0.3.9" diff --git a/test-utils/runtime/client/src/block_builder_ext.rs b/test-utils/runtime/client/src/block_builder_ext.rs index 9dc27c64143f4..bb0f2d400bfc2 100644 --- a/test-utils/runtime/client/src/block_builder_ext.rs +++ b/test-utils/runtime/client/src/block_builder_ext.rs @@ -43,7 +43,7 @@ pub trait BlockBuilderExt { impl<'a, A, B> BlockBuilderExt for sc_block_builder::BlockBuilder<'a, substrate_test_runtime::Block, A, B> where A: ProvideRuntimeApi + 'a, - A::Api: BlockBuilderApi + + A::Api: BlockBuilderApi + ApiExt< substrate_test_runtime::Block, StateBackend = backend::StateBackendFor diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 3dc514953b669..b349d1266b031 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -61,7 +61,7 @@ use sp_inherents::{CheckInherentsResult, InherentData}; use cfg_if::cfg_if; // Ensure Babe and Aura use the same crypto to simplify things a bit. -pub use sp_consensus_babe::{AuthorityId, SlotNumber, AllowedSlots}; +pub use sp_consensus_babe::{AuthorityId, Slot, AllowedSlots}; pub type AuraId = sp_consensus_aura::sr25519::AuthorityId; @@ -433,6 +433,37 @@ impl From> for Event { } } +impl frame_support::traits::PalletInfo for Runtime { + fn index() -> Option { + let type_id = sp_std::any::TypeId::of::

(); + if type_id == sp_std::any::TypeId::of::>() { + return Some(0) + } + if type_id == sp_std::any::TypeId::of::>() { + return Some(1) + } + if type_id == sp_std::any::TypeId::of::>() { + return Some(2) + } + + None + } + fn name() -> Option<&'static str> { + let type_id = sp_std::any::TypeId::of::

(); + if type_id == sp_std::any::TypeId::of::>() { + return Some("System") + } + if type_id == sp_std::any::TypeId::of::>() { + return Some("Timestamp") + } + if type_id == sp_std::any::TypeId::of::>() { + return Some("Babe") + } + + None + } +} + parameter_types! { pub const BlockHashCount: BlockNumber = 2400; pub const MinimumPeriod: u64 = 5; @@ -463,7 +494,7 @@ impl frame_system::Config for Runtime { type BlockHashCount = BlockHashCount; type DbWeight = (); type Version = (); - type PalletInfo = (); + type PalletInfo = Self; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -739,7 +770,7 @@ cfg_if! { } } - fn current_epoch_start() -> sp_consensus_babe::SlotNumber { + fn current_epoch_start() -> Slot { >::current_epoch_start() } @@ -761,7 +792,7 @@ cfg_if! { } fn generate_key_ownership_proof( - _slot_number: sp_consensus_babe::SlotNumber, + _slot: sp_consensus_babe::Slot, _authority_id: sp_consensus_babe::AuthorityId, ) -> Option { None @@ -998,7 +1029,7 @@ cfg_if! { } } - fn current_epoch_start() -> sp_consensus_babe::SlotNumber { + fn current_epoch_start() -> Slot { >::current_epoch_start() } @@ -1020,7 +1051,7 @@ cfg_if! { } fn generate_key_ownership_proof( - _slot_number: sp_consensus_babe::SlotNumber, + _slot: sp_consensus_babe::Slot, _authority_id: sp_consensus_babe::AuthorityId, ) -> Option { None diff --git a/test-utils/runtime/transaction-pool/Cargo.toml b/test-utils/runtime/transaction-pool/Cargo.toml index 7fbea1e3c0edb..6e4e6524c3699 100644 --- a/test-utils/runtime/transaction-pool/Cargo.toml +++ b/test-utils/runtime/transaction-pool/Cargo.toml @@ -14,10 +14,10 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] substrate-test-runtime-client = { version = "2.0.0", path = "../client" } parking_lot = "0.11.1" -codec = { package = "parity-scale-codec", version = "1.3.6" } -sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-transaction-pool = { version = "2.0.0", path = "../../../primitives/transaction-pool" } -sc-transaction-graph = { version = "2.0.0", path = "../../../client/transaction-pool/graph" } +codec = { package = "parity-scale-codec", version = "2.0.0" } +sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-transaction-pool = { version = "3.0.0", path = "../../../primitives/transaction-pool" } +sc-transaction-graph = { version = "3.0.0", path = "../../../client/transaction-pool/graph" } futures = { version = "0.3.1", features = ["compat"] } derive_more = "0.99.2" diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index a2e83fe7b0bf3..b3a0f322a639f 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -72,7 +72,7 @@ macro_rules! assert_eq_uvec { macro_rules! __assert_eq_uvec { ( $x:expr, $y:expr ) => { $x.iter().for_each(|e| { - if !$y.contains(e) { panic!(format!("vectors not equal: {:?} != {:?}", $x, $y)); } + if !$y.contains(e) { panic!("vectors not equal: {:?} != {:?}", $x, $y); } }); } } diff --git a/test-utils/test-crate/Cargo.toml b/test-utils/test-crate/Cargo.toml index 4e1273b25c993..846b14fe07744 100644 --- a/test-utils/test-crate/Cargo.toml +++ b/test-utils/test-crate/Cargo.toml @@ -13,5 +13,5 @@ targets = ["x86_64-unknown-linux-gnu"] [dev-dependencies] tokio = { version = "0.2.13", features = ["macros"] } -test-utils = { version = "2.0.0", path = "..", package = "substrate-test-utils" } -sc-service = { version = "0.8.0", path = "../../client/service" } +test-utils = { version = "3.0.0", path = "..", package = "substrate-test-utils" } +sc-service = { version = "0.9.0", path = "../../client/service" } diff --git a/utils/browser/Cargo.toml b/utils/browser/Cargo.toml index 3137c2698ec33..d7051620fcfd9 100644 --- a/utils/browser/Cargo.toml +++ b/utils/browser/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-browser-utils" -version = "0.8.1" +version = "0.9.0" authors = ["Parity Technologies "] description = "Utilities for creating a browser light-client." edition = "2018" @@ -21,14 +21,14 @@ console_error_panic_hook = "0.1.6" js-sys = "0.3.34" wasm-bindgen = "0.2.57" wasm-bindgen-futures = "0.4.18" -kvdb-web = "0.8.0" -sp-database = { version = "2.0.0", path = "../../primitives/database" } -sc-informant = { version = "0.8.0", path = "../../client/informant" } -sc-service = { version = "0.8.0", path = "../../client/service", default-features = false } -sc-network = { path = "../../client/network", version = "0.8.0"} -sc-chain-spec = { path = "../../client/chain-spec", version = "2.0.0"} -sc-telemetry = { path = "../../client/telemetry", version = "2.0.0"} -sc-tracing = { path = "../../client/tracing", version = "2.0.0"} +kvdb-web = "0.9.0" +sp-database = { version = "3.0.0", path = "../../primitives/database" } +sc-informant = { version = "0.9.0", path = "../../client/informant" } +sc-service = { version = "0.9.0", path = "../../client/service", default-features = false } +sc-network = { path = "../../client/network", version = "0.9.0"} +sc-chain-spec = { path = "../../client/chain-spec", version = "3.0.0"} +sc-telemetry = { path = "../../client/telemetry", version = "3.0.0"} +sc-tracing = { path = "../../client/tracing", version = "3.0.0"} # Imported just for the `wasm-bindgen` feature getrandom = { version = "0.2", features = ["js"] } diff --git a/utils/browser/src/lib.rs b/utils/browser/src/lib.rs index 5e1e8db316688..b72f2e973b682 100644 --- a/utils/browser/src/lib.rs +++ b/utils/browser/src/lib.rs @@ -25,7 +25,7 @@ use sc_service::{ KeepBlocks, TransactionStorageMode, }; use sc_telemetry::TelemetryHandle; -use sc_tracing::logging::GlobalLoggerBuilder; +use sc_tracing::logging::LoggerBuilder; use wasm_bindgen::prelude::*; use futures::{ prelude::*, channel::{oneshot, mpsc}, compat::*, future::{ready, ok, select} @@ -41,7 +41,7 @@ pub fn init_logging_and_telemetry( pattern: &str, ) -> Result { let transport = ExtTransport::new(ffi::websocket_transport()); - let mut logger = GlobalLoggerBuilder::new(pattern); + let mut logger = LoggerBuilder::new(pattern); logger.with_transport(transport); logger.init() } diff --git a/utils/build-script-utils/Cargo.toml b/utils/build-script-utils/Cargo.toml index f82ee7487d9fa..fbef70db93bfd 100644 --- a/utils/build-script-utils/Cargo.toml +++ b/utils/build-script-utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-build-script-utils" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,4 +13,4 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -platforms = "0.2.1" +platforms = "1.1" diff --git a/utils/fork-tree/Cargo.toml b/utils/fork-tree/Cargo.toml index 292d1a83b7e58..11c269bc3cba8 100644 --- a/utils/fork-tree/Cargo.toml +++ b/utils/fork-tree/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fork-tree" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -14,4 +14,4 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.6", features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } diff --git a/utils/frame/benchmarking-cli/Cargo.toml b/utils/frame/benchmarking-cli/Cargo.toml index 717224f787f51..c810bd4d57d79 100644 --- a/utils/frame/benchmarking-cli/Cargo.toml +++ b/utils/frame/benchmarking-cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frame-benchmarking-cli" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,17 +13,17 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -frame-benchmarking = { version = "2.0.0", path = "../../../frame/benchmarking" } -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sc-service = { version = "0.8.0", default-features = false, path = "../../../client/service" } -sc-cli = { version = "0.8.0", path = "../../../client/cli" } -sc-client-db = { version = "0.8.0", path = "../../../client/db" } -sc-executor = { version = "0.8.0", path = "../../../client/executor" } -sp-externalities = { version = "0.8.0", path = "../../../primitives/externalities" } -sp-keystore = { version = "0.8.0", path = "../../../primitives/keystore" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } -sp-state-machine = { version = "0.8.0", path = "../../../primitives/state-machine" } -codec = { version = "1.3.1", package = "parity-scale-codec" } +frame-benchmarking = { version = "3.0.0", path = "../../../frame/benchmarking" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sc-service = { version = "0.9.0", default-features = false, path = "../../../client/service" } +sc-cli = { version = "0.9.0", path = "../../../client/cli" } +sc-client-db = { version = "0.9.0", path = "../../../client/db" } +sc-executor = { version = "0.9.0", path = "../../../client/executor" } +sp-externalities = { version = "0.9.0", path = "../../../primitives/externalities" } +sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-state-machine = { version = "0.9.0", path = "../../../primitives/state-machine" } +codec = { version = "2.0.0", package = "parity-scale-codec" } structopt = "0.3.8" chrono = "0.4" serde = "1.0.116" diff --git a/utils/frame/frame-utilities-cli/Cargo.toml b/utils/frame/frame-utilities-cli/Cargo.toml index 4f0030b021820..cb37119edf0b2 100644 --- a/utils/frame/frame-utilities-cli/Cargo.toml +++ b/utils/frame/frame-utilities-cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-frame-cli" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -11,11 +11,11 @@ documentation = "https://docs.rs/substrate-frame-cli" readme = "README.md" [dependencies] -sp-core = { version = "2.0.0", path = "../../../primitives/core" } -sc-cli = { version = "0.8.0", path = "../../../client/cli" } -sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } +sc-cli = { version = "0.9.0", path = "../../../client/cli" } +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } structopt = "0.3.8" -frame-system = { version = "2.0.0", path = "../../../frame/system" } +frame-system = { version = "3.0.0", path = "../../../frame/system" } [dev-dependencies] diff --git a/utils/frame/remote-externalities/Cargo.toml b/utils/frame/remote-externalities/Cargo.toml new file mode 100644 index 0000000000000..41a3b26217860 --- /dev/null +++ b/utils/frame/remote-externalities/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "remote-externalities" +version = "0.9.0" +authors = ["Parity Technologies "] +edition = "2018" +license = "Apache-2.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" +description = "An externalities provided environemnt that can load itself from remote nodes or cache files" +readme = "README.md" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +jsonrpc-core-client = { version = "15.1.0", features = ["http"] } +sc-rpc-api = { version = "0.9.0", path = "../../../client/rpc-api" } +sc-rpc = { version = "3.0.0", path = "../../../client/rpc" } +futures = "0.1.29" + +hex-literal = "0.3.1" +env_logger = "0.8.2" +log = "0.4.11" +bincode = "1.3.1" +tokio = "0.1.22" + +sp-io = { version = "3.0.0", path = "../../../primitives/io" } +sp-core = { version = "3.0.0", path = "../../../primitives/core" } + +[dev-dependencies] +async-std = { version = "1.6.5", features = ["attributes"] } + +[features] +remote-test = [] diff --git a/utils/frame/remote-externalities/proxy_test b/utils/frame/remote-externalities/proxy_test new file mode 100644 index 0000000000000..adb93f5ba270c Binary files /dev/null and b/utils/frame/remote-externalities/proxy_test differ diff --git a/utils/frame/remote-externalities/src/lib.rs b/utils/frame/remote-externalities/src/lib.rs new file mode 100644 index 0000000000000..6c8b49c7c85d5 --- /dev/null +++ b/utils/frame/remote-externalities/src/lib.rs @@ -0,0 +1,454 @@ +// This file is part of Substrate. + +// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! # Remote Externalities +//! +//! An equivalent of `sp_io::TestExternalities` that can load its state from a remote substrate +//! based chain, or a local cache file. +//! +//! #### Runtime to Test Against +//! +//! While not absolutely necessary, you most likely need a `Runtime` equivalent in your test setup +//! through which you can infer storage types. There are two options here: +//! +//! 1. Build a mock runtime, similar how to you would build one in a pallet test (see example +//! below). The very important point here is that this mock needs to hold real values for types +//! that matter for you, based on the chain of interest. Some typical ones are: +//! +//! - `sp_runtime::AccountId32` as `AccountId`. +//! - `u32` as `BlockNumber`. +//! - `u128` as Balance. +//! +//! Once you have your `Runtime`, you can use it for storage type resolution and do things like +//! `>::storage_getter()` or `>::get()`. +//! +//! 2. Or, you can use a real runtime. +//! +//! ### Example +//! +//! With a test runtime +//! +//! ```ignore +//! use remote_externalities::Builder; +//! +//! #[derive(Clone, Eq, PartialEq, Debug, Default)] +//! pub struct TestRuntime; +//! +//! use frame_system as system; +//! impl_outer_origin! { +//! pub enum Origin for TestRuntime {} +//! } +//! +//! impl frame_system::Config for TestRuntime { +//! .. +//! // we only care about these two for now. The rest can be mock. The block number type of +//! // kusama is u32. +//! type BlockNumber = u32; +//! type Header = Header; +//! .. +//! } +//! +//! #[test] +//! fn test_runtime_works() { +//! let hash: Hash = +//! hex!["f9a4ce984129569f63edc01b1c13374779f9384f1befd39931ffdcc83acf63a7"].into(); +//! let parent: Hash = +//! hex!["540922e96a8fcaf945ed23c6f09c3e189bd88504ec945cc2171deaebeaf2f37e"].into(); +//! Builder::new() +//! .at(hash) +//! .module("System") +//! .build() +//! .execute_with(|| { +//! assert_eq!( +//! // note: the hash corresponds to 3098546. We can check only the parent. +//! // https://polkascan.io/kusama/block/3098546 +//! >::block_hash(3098545u32), +//! parent, +//! ) +//! }); +//! } +//! ``` +//! +//! Or with the real kusama runtime. +//! +//! ```ignore +//! use remote_externalities::Builder; +//! use kusama_runtime::Runtime; +//! +//! #[test] +//! fn test_runtime_works() { +//! let hash: Hash = +//! hex!["f9a4ce984129569f63edc01b1c13374779f9384f1befd39931ffdcc83acf63a7"].into(); +//! Builder::new() +//! .at(hash) +//! .module("Staking") +//! .build() +//! .execute_with(|| assert_eq!(>::validator_count(), 400)); +//! } +//! ``` + +use std::{ + fs, + path::{Path, PathBuf}, +}; +use log::*; +use sp_core::{hashing::twox_128}; +pub use sp_io::TestExternalities; +use sp_core::{ + hexdisplay::HexDisplay, + storage::{StorageKey, StorageData}, +}; +use futures::future::Future; + +type KeyPair = (StorageKey, StorageData); +type Number = u32; +type Hash = sp_core::H256; +// TODO: make these two generic. + +const LOG_TARGET: &'static str = "remote-ext"; + +/// The execution mode. +#[derive(Clone)] +pub enum Mode { + /// Online. + Online(OnlineConfig), + /// Offline. Uses a cached file and needs not any client config. + Offline(OfflineConfig), +} + +/// configuration of the online execution. +/// +/// A cache config must be present. +#[derive(Clone)] +pub struct OfflineConfig { + /// The configuration of the cache file to use. It must be present. + pub cache: CacheConfig, +} + +/// Configuration of the online execution. +/// +/// A cache config may be present and will be written to in that case. +#[derive(Clone)] +pub struct OnlineConfig { + /// The HTTP uri to use. + pub uri: String, + /// The block number at which to connect. Will be latest finalized head if not provided. + pub at: Option, + /// An optional cache file to WRITE to, not for reading. Not cached if set to `None`. + pub cache: Option, + /// The modules to scrape. If empty, entire chain state will be scraped. + pub modules: Vec, +} + +impl Default for OnlineConfig { + fn default() -> Self { + Self { + uri: "http://localhost:9933".into(), + at: None, + cache: None, + modules: Default::default(), + } + } +} + +/// Configuration of the cache. +#[derive(Clone)] +pub struct CacheConfig { + // TODO: I could mix these two into one filed, but I think separate is better bc one can be + // configurable while one not. + /// File name. + pub name: String, + /// Base directory. + pub directory: String, +} + +impl Default for CacheConfig { + fn default() -> Self { + Self { name: "CACHE".into(), directory: ".".into() } + } +} + +impl CacheConfig { + fn path(&self) -> PathBuf { + Path::new(&self.directory).join(self.name.clone()) + } +} + +/// Builder for remote-externalities. +pub struct Builder { + inject: Vec, + mode: Mode, + chain: String, +} + +impl Default for Builder { + fn default() -> Self { + Self { + inject: Default::default(), + mode: Mode::Online(OnlineConfig { + at: None, + uri: "http://localhost:9933".into(), + cache: None, + modules: Default::default(), + }), + chain: "UNSET".into(), + } + } +} + +// Mode methods +impl Builder { + fn as_online(&self) -> &OnlineConfig { + match &self.mode { + Mode::Online(config) => &config, + _ => panic!("Unexpected mode: Online"), + } + } + + fn as_online_mut(&mut self) -> &mut OnlineConfig { + match &mut self.mode { + Mode::Online(config) => config, + _ => panic!("Unexpected mode: Online"), + } + } +} + +// RPC methods +impl Builder { + async fn rpc_get_head(&self) -> Hash { + let mut rt = tokio::runtime::Runtime::new().expect("Unable to create a runtime"); + let uri = self.as_online().uri.clone(); + rt.block_on::<_, _, ()>(futures::lazy(move || { + trace!(target: LOG_TARGET, "rpc: finalized_head"); + let client: sc_rpc_api::chain::ChainClient = + jsonrpc_core_client::transports::http::connect(&uri).wait().unwrap(); + Ok(client.finalized_head().wait().unwrap()) + })) + .unwrap() + } + + /// Relay the request to `state_getPairs` rpc endpoint. + /// + /// Note that this is an unsafe RPC. + async fn rpc_get_pairs(&self, prefix: StorageKey, at: Hash) -> Vec { + let mut rt = tokio::runtime::Runtime::new().expect("Unable to create a runtime"); + let uri = self.as_online().uri.clone(); + rt.block_on::<_, _, ()>(futures::lazy(move || { + trace!(target: LOG_TARGET, "rpc: storage_pairs: {:?} / {:?}", prefix, at); + let client: sc_rpc_api::state::StateClient = + jsonrpc_core_client::transports::http::connect(&uri).wait().unwrap(); + Ok(client.storage_pairs(prefix, Some(at)).wait().unwrap()) + })) + .unwrap() + } + + /// Get the chain name. + async fn chain_name(&self) -> String { + let mut rt = tokio::runtime::Runtime::new().expect("Unable to create a runtime"); + let uri = self.as_online().uri.clone(); + rt.block_on::<_, _, ()>(futures::lazy(move || { + trace!(target: LOG_TARGET, "rpc: system_chain"); + let client: sc_rpc_api::system::SystemClient<(), ()> = + jsonrpc_core_client::transports::http::connect(&uri).wait().unwrap(); + Ok(client.system_chain().wait().unwrap()) + })) + .unwrap() + } +} + +// Internal methods +impl Builder { + /// Save the given data as cache. + fn save_cache(&self, data: &[KeyPair], path: &Path) { + let bdata = bincode::serialize(data).unwrap(); + info!(target: LOG_TARGET, "writing to cache file {:?}", path); + fs::write(path, bdata).unwrap(); + } + + /// initialize `Self` from cache. Panics if the file does not exist. + fn load_cache(&self, path: &Path) -> Vec { + info!(target: LOG_TARGET, "scraping keypairs from cache {:?}", path,); + let bytes = fs::read(path).unwrap(); + bincode::deserialize(&bytes[..]).unwrap() + } + + /// Build `Self` from a network node denoted by `uri`. + async fn load_remote(&self) -> Vec { + let config = self.as_online(); + let at = self.as_online().at.unwrap().clone(); + info!(target: LOG_TARGET, "scraping keypairs from remote node {} @ {:?}", config.uri, at); + + let keys_and_values = if config.modules.len() > 0 { + let mut filtered_kv = vec![]; + for f in config.modules.iter() { + let hashed_prefix = StorageKey(twox_128(f.as_bytes()).to_vec()); + let module_kv = self.rpc_get_pairs(hashed_prefix.clone(), at).await; + info!( + target: LOG_TARGET, + "downloaded data for module {} (count: {} / prefix: {:?}).", + f, + module_kv.len(), + HexDisplay::from(&hashed_prefix), + ); + filtered_kv.extend(module_kv); + } + filtered_kv + } else { + info!(target: LOG_TARGET, "downloading data for all modules."); + self.rpc_get_pairs(StorageKey(vec![]), at).await.into_iter().collect::>() + }; + + keys_and_values + } + + async fn init_remote_client(&mut self) { + self.as_online_mut().at = Some(self.rpc_get_head().await); + self.chain = self.chain_name().await; + } + + async fn pre_build(mut self) -> Vec { + let mut base_kv = match self.mode.clone() { + Mode::Offline(config) => self.load_cache(&config.cache.path()), + Mode::Online(config) => { + self.init_remote_client().await; + let kp = self.load_remote().await; + if let Some(c) = config.cache { + self.save_cache(&kp, &c.path()); + } + kp + } + }; + + info!( + target: LOG_TARGET, + "extending externalities with {} manually injected keys", + self.inject.len() + ); + base_kv.extend(self.inject.clone()); + base_kv + } +} + +// Public methods +impl Builder { + /// Create a new builder. + pub fn new() -> Self { + Default::default() + } + + /// Inject a manual list of key and values to the storage. + pub fn inject(mut self, injections: &[KeyPair]) -> Self { + for i in injections { + self.inject.push(i.clone()); + } + self + } + + /// Configure a cache to be used. + pub fn mode(mut self, mode: Mode) -> Self { + self.mode = mode; + self + } + + /// Build the test externalities. + pub async fn build(self) -> TestExternalities { + let kv = self.pre_build().await; + let mut ext = TestExternalities::new_empty(); + + info!(target: LOG_TARGET, "injecting a total of {} keys", kv.len()); + for (k, v) in kv { + let (k, v) = (k.0, v.0); + ext.insert(k, v); + } + ext + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn init_logger() { + let _ = env_logger::Builder::from_default_env() + .format_module_path(false) + .format_level(true) + .try_init(); + } + + #[async_std::test] + #[cfg(feature = "remote-test")] + async fn can_build_one_pallet() { + init_logger(); + Builder::new() + .mode(Mode::Online(OnlineConfig { + modules: vec!["Proxy".into()], + ..Default::default() + })) + .build() + .await + .execute_with(|| {}); + } + + #[async_std::test] + async fn can_load_cache() { + init_logger(); + Builder::new() + .mode(Mode::Offline(OfflineConfig { + cache: CacheConfig { name: "proxy_test".into(), ..Default::default() }, + })) + .build() + .await + .execute_with(|| {}); + } + + #[async_std::test] + #[cfg(feature = "remote-test")] + async fn can_create_cache() { + init_logger(); + Builder::new() + .mode(Mode::Online(OnlineConfig { + cache: Some(CacheConfig { + name: "test_cache_to_remove.bin".into(), + ..Default::default() + }), + ..Default::default() + })) + .build() + .await + .execute_with(|| {}); + + let to_delete = std::fs::read_dir(CacheConfig::default().directory) + .unwrap() + .into_iter() + .map(|d| d.unwrap()) + .filter(|p| p.path().extension().unwrap_or_default() == "bin") + .collect::>(); + + assert!(to_delete.len() > 0); + + for d in to_delete { + std::fs::remove_file(d.path()).unwrap(); + } + } + + #[async_std::test] + #[cfg(feature = "remote-test")] + async fn can_build_all() { + init_logger(); + Builder::new().build().await.execute_with(|| {}); + } +} diff --git a/utils/frame/rpc/support/Cargo.toml b/utils/frame/rpc/support/Cargo.toml index b9ee76b846e0d..ca3705b499a22 100644 --- a/utils/frame/rpc/support/Cargo.toml +++ b/utils/frame/rpc/support/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-frame-rpc-support" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies ", "Andrew Dirksen "] edition = "2018" license = "Apache-2.0" @@ -15,12 +15,12 @@ targets = ["x86_64-unknown-linux-gnu"] futures = { version = "0.3.0", features = ["compat"] } jsonrpc-client-transports = { version = "15.1.0", default-features = false, features = ["http"] } jsonrpc-core = "15.1.0" -codec = { package = "parity-scale-codec", version = "1.3.6" } +codec = { package = "parity-scale-codec", version = "2.0.0" } serde = "1" -frame-support = { version = "2.0.0", path = "../../../../frame/support" } -sp-storage = { version = "2.0.0", path = "../../../../primitives/storage" } -sc-rpc-api = { version = "0.8.0", path = "../../../../client/rpc-api" } +frame-support = { version = "3.0.0", path = "../../../../frame/support" } +sp-storage = { version = "3.0.0", path = "../../../../primitives/storage" } +sc-rpc-api = { version = "0.9.0", path = "../../../../client/rpc-api" } [dev-dependencies] -frame-system = { version = "2.0.0", path = "../../../../frame/system" } +frame-system = { version = "3.0.0", path = "../../../../frame/system" } tokio = "0.2" diff --git a/utils/frame/rpc/system/Cargo.toml b/utils/frame/rpc/system/Cargo.toml index 03016462cbeac..ea8d97a82ad34 100644 --- a/utils/frame/rpc/system/Cargo.toml +++ b/utils/frame/rpc/system/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-frame-rpc-system" -version = "2.0.1" +version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" @@ -13,24 +13,24 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -sc-client-api = { version = "2.0.0", path = "../../../../client/api" } -codec = { package = "parity-scale-codec", version = "1.3.6" } +sc-client-api = { version = "3.0.0", path = "../../../../client/api" } +codec = { package = "parity-scale-codec", version = "2.0.0" } futures = { version = "0.3.4", features = ["compat"] } jsonrpc-core = "15.1.0" jsonrpc-core-client = "15.1.0" jsonrpc-derive = "15.1.0" log = "0.4.8" serde = { version = "1.0.101", features = ["derive"] } -sp-runtime = { version = "2.0.0", path = "../../../../primitives/runtime" } -sp-api = { version = "2.0.0", path = "../../../../primitives/api" } -frame-system-rpc-runtime-api = { version = "2.0.0", path = "../../../../frame/system/rpc/runtime-api" } -sp-core = { version = "2.0.0", path = "../../../../primitives/core" } -sp-blockchain = { version = "2.0.0", path = "../../../../primitives/blockchain" } -sp-transaction-pool = { version = "2.0.0", path = "../../../../primitives/transaction-pool" } -sp-block-builder = { version = "2.0.0", path = "../../../../primitives/block-builder" } -sc-rpc-api = { version = "0.8.0", path = "../../../../client/rpc-api" } +sp-runtime = { version = "3.0.0", path = "../../../../primitives/runtime" } +sp-api = { version = "3.0.0", path = "../../../../primitives/api" } +frame-system-rpc-runtime-api = { version = "3.0.0", path = "../../../../frame/system/rpc/runtime-api" } +sp-core = { version = "3.0.0", path = "../../../../primitives/core" } +sp-blockchain = { version = "3.0.0", path = "../../../../primitives/blockchain" } +sp-transaction-pool = { version = "3.0.0", path = "../../../../primitives/transaction-pool" } +sp-block-builder = { version = "3.0.0", path = "../../../../primitives/block-builder" } +sc-rpc-api = { version = "0.9.0", path = "../../../../client/rpc-api" } [dev-dependencies] substrate-test-runtime-client = { version = "2.0.0", path = "../../../../test-utils/runtime/client" } -sp-tracing = { version = "2.0.0", path = "../../../../primitives/tracing" } -sc-transaction-pool = { version = "2.0.0", path = "../../../../client/transaction-pool" } +sp-tracing = { version = "3.0.0", path = "../../../../primitives/tracing" } +sc-transaction-pool = { version = "3.0.0", path = "../../../../client/transaction-pool" } diff --git a/utils/frame/try-runtime/cli/Cargo.toml b/utils/frame/try-runtime/cli/Cargo.toml new file mode 100644 index 0000000000000..592d0a5b99d27 --- /dev/null +++ b/utils/frame/try-runtime/cli/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "try-runtime-cli" +version = "0.9.0" +authors = ["Parity Technologies "] +edition = "2018" +license = "Apache-2.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" +description = "Cli command runtime testing and dry-running" +readme = "README.md" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +log = "0.4.8" +parity-scale-codec = { version = "2.0.0" } + +sc-service = { version = "0.9.0", default-features = false, path = "../../../../client/service" } +sc-cli = { version = "0.9.0", path = "../../../../client/cli" } +sc-executor = { path = "../../../../client/executor" } +sc-client-api = { version = "3.0.0", path = "../../../../client/api" } +structopt = "0.3.8" +sp-state-machine = { version = "0.9.0", path = "../../../../primitives/state-machine" } +sp-api = { version = "3.0.0", path = "../../../../primitives/api" } +sp-blockchain = { version = "3.0.0", path = "../../../../primitives/blockchain" } +sp-runtime = { version = "3.0.0", path = "../../../../primitives/runtime" } +sp-externalities = { version = "0.9.0", path = "../../../../primitives/externalities" } +sp-core = { version = "3.0.0", path = "../../../../primitives/core" } +frame-try-runtime = { version = "0.9.0", path = "../../../../frame/try-runtime" } + +remote-externalities = { path = "../../remote-externalities" } diff --git a/utils/frame/try-runtime/cli/src/lib.rs b/utils/frame/try-runtime/cli/src/lib.rs new file mode 100644 index 0000000000000..92526379f471e --- /dev/null +++ b/utils/frame/try-runtime/cli/src/lib.rs @@ -0,0 +1,178 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! `Structopt`-ready struct for `try-runtime`. + +use parity_scale_codec::Decode; +use std::{fmt::Debug, str::FromStr}; +use sc_service::Configuration; +use sc_cli::{CliConfiguration, ExecutionStrategy, WasmExecutionMethod}; +use sc_executor::NativeExecutor; +use sc_service::NativeExecutionDispatch; +use sp_state_machine::StateMachine; +use sp_runtime::traits::{Block as BlockT, NumberFor}; +use sp_core::storage::{StorageData, StorageKey, well_known_keys}; + +/// Various commands to try out the new runtime, over configurable states. +/// +/// For now this only assumes running the `on_runtime_upgrade` hooks. +#[derive(Debug, structopt::StructOpt)] +pub struct TryRuntimeCmd { + /// The shared parameters + #[allow(missing_docs)] + #[structopt(flatten)] + pub shared_params: sc_cli::SharedParams, + + /// The state to use to run the migration. Should be a valid FILE or HTTP URI. + #[structopt(short, long, default_value = "http://localhost:9933")] + pub state: State, + + /// The execution strategy that should be used for benchmarks + #[structopt( + long = "execution", + value_name = "STRATEGY", + possible_values = &ExecutionStrategy::variants(), + case_insensitive = true, + default_value = "Native", + )] + pub execution: ExecutionStrategy, + + /// Method for executing Wasm runtime code. + #[structopt( + long = "wasm-execution", + value_name = "METHOD", + possible_values = &WasmExecutionMethod::enabled_variants(), + case_insensitive = true, + default_value = "Interpreted" + )] + pub wasm_method: WasmExecutionMethod, +} + +/// The state to use for a migration dry-run. +#[derive(Debug)] +pub enum State { + /// A snapshot. Inner value is a file path. + Snap(String), + + /// A live chain. Inner value is the HTTP uri. + Live(String), +} + +impl FromStr for State { + type Err = &'static str; + fn from_str(s: &str) -> Result { + match s.get(..7) { + // could use Url crate as well, but lets keep it simple for now. + Some("http://") => Ok(State::Live(s.to_string())), + Some("file://") => s + .split("//") + .collect::>() + .get(1) + .map(|s| State::Snap(s.to_string())) + .ok_or("invalid file URI"), + _ => Err("invalid format. Must be a valid HTTP or File URI"), + } + } +} + +impl TryRuntimeCmd { + pub async fn run(&self, config: Configuration) -> sc_cli::Result<()> + where + B: BlockT, + ExecDispatch: NativeExecutionDispatch + 'static, + { + let spec = config.chain_spec; + let genesis_storage = spec.build_storage()?; + + let code = StorageData( + genesis_storage + .top + .get(well_known_keys::CODE) + .expect("code key must exist in genesis storage; qed") + .to_vec(), + ); + let code_key = StorageKey(well_known_keys::CODE.to_vec()); + + let wasm_method = self.wasm_method; + let execution = self.execution; + + let mut changes = Default::default(); + // don't really care about these -- use the default values. + let max_runtime_instances = config.max_runtime_instances; + let heap_pages = config.default_heap_pages; + let executor = NativeExecutor::::new( + wasm_method.into(), + heap_pages, + max_runtime_instances, + ); + + let ext = { + use remote_externalities::{Builder, Mode, CacheConfig, OfflineConfig, OnlineConfig}; + let builder = match &self.state { + State::Snap(file_path) => Builder::new().mode(Mode::Offline(OfflineConfig { + cache: CacheConfig { name: file_path.into(), ..Default::default() }, + })), + State::Live(http_uri) => Builder::new().mode(Mode::Online(OnlineConfig { + uri: http_uri.into(), + ..Default::default() + })), + }; + + // inject the code into this ext. + builder.inject(&[(code_key, code)]).build().await + }; + + let encoded_result = StateMachine::<_, _, NumberFor, _>::new( + &ext.backend, + None, + &mut changes, + &executor, + "TryRuntime_on_runtime_upgrade", + &[], + ext.extensions, + &sp_state_machine::backend::BackendRuntimeCode::new(&ext.backend) + .runtime_code()?, + sp_core::testing::TaskExecutor::new(), + ) + .execute(execution.into()) + .map_err(|e| format!("failed to execute 'TryRuntime_on_runtime_upgrade' due to {:?}", e))?; + + let (weight, total_weight) = <(u64, u64) as Decode>::decode(&mut &*encoded_result) + .map_err(|e| format!("failed to decode output due to {:?}", e))?; + log::info!( + "try-runtime executed without errors. Consumed weight = {}, total weight = {} ({})", + weight, + total_weight, + weight as f64 / total_weight as f64 + ); + + Ok(()) + } +} + +impl CliConfiguration for TryRuntimeCmd { + fn shared_params(&self) -> &sc_cli::SharedParams { + &self.shared_params + } + + fn chain_id(&self, _is_dev: bool) -> sc_cli::Result { + Ok(match self.shared_params.chain { + Some(ref chain) => chain.clone(), + None => "dev".into(), + }) + } +} diff --git a/utils/prometheus/Cargo.toml b/utils/prometheus/Cargo.toml index 19df4fb8059dd..a7f90e831620b 100644 --- a/utils/prometheus/Cargo.toml +++ b/utils/prometheus/Cargo.toml @@ -1,7 +1,7 @@ [package] description = "Endpoint to expose Prometheus metrics" name = "substrate-prometheus-endpoint" -version = "0.8.1" +version = "0.9.0" license = "Apache-2.0" authors = ["Parity Technologies "] edition = "2018" @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] log = "0.4.8" -prometheus = { version = "0.10.0", default-features = false } +prometheus = { version = "0.11.0", default-features = false } futures-util = { version = "0.3.1", default-features = false, features = ["io"] } derive_more = "0.99" diff --git a/utils/wasm-builder/Cargo.toml b/utils/wasm-builder/Cargo.toml index 199e26b509e2e..c9d165ce8a140 100644 --- a/utils/wasm-builder/Cargo.toml +++ b/utils/wasm-builder/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-wasm-builder" -version = "3.0.0" +version = "4.0.0" authors = ["Parity Technologies "] description = "Utility for building WASM binaries" edition = "2018"