diff --git a/.github/mergify.yml b/.github/mergify.yml index b464c9aba87..91cd8881237 100644 --- a/.github/mergify.yml +++ b/.github/mergify.yml @@ -52,22 +52,24 @@ pull_request_rules: message: Approvals have been dismissed because the PR was updated after the `send-it` label was applied. changes_requested: false - # - name: Approve trivial maintainer PRs - # conditions: - # - base=master - # - label=trivial - # - author=@libp2p/rust-libp2p-maintainers - # actions: - # review: + - name: Approve trivial maintainer PRs + conditions: + - base=master + - label=trivial + - author=@libp2p/rust-libp2p-maintainers + actions: + review: + type: APPROVE - # - name: Approve dependabot PRs of semver-compatible updates - # conditions: - # - author=dependabot[bot] - # - or: - # - title~=bump [^\s]+ from ([1-9]+)\..+ to \1\. # For major >= 1 versions, only approve updates with the same major version. - # - title~=bump [^\s]+ from 0\.([\d]+)\..+ to 0\.\1\. # For major == 0 versions, only approve updates with the same minor version. - # actions: - # review: + - name: Approve dependabot PRs of semver-compatible updates + conditions: + - author=dependabot[bot] + - or: + - title~=bump [^\s]+ from ([1-9]+)\..+ to \1\. # For major >= 1 versions, only approve updates with the same major version. + - title~=bump [^\s]+ from 0\.([\d]+)\..+ to 0\.\1\. # For major == 0 versions, only approve updates with the same minor version. + actions: + review: + type: APPROVE queue_rules: - name: default diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index f724d2fd7de..90e8b2cda53 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -4,7 +4,7 @@ Please write a summary of your changes and why you made them. This section will appear as the commit message after merging. Please craft it accordingly. -For a quick primer on good commit mesages, check out this blog post: https://cbea.ms/git-commit/ +For a quick primer on good commit messages, check out this blog post: https://cbea.ms/git-commit/ Please include any relevant issues in here, for example: diff --git a/.github/workflows/cache-factory.yml b/.github/workflows/cache-factory.yml index 8c49b335f1b..7623b56f450 100644 --- a/.github/workflows/cache-factory.yml +++ b/.github/workflows/cache-factory.yml @@ -22,7 +22,7 @@ jobs: - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: shared-key: stable-cache diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 24a211966a8..daee569d047 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,11 +36,11 @@ jobs: with: fetch-depth: 0 - - uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0 + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: shared-key: stable-cache save-if: false @@ -57,20 +57,20 @@ jobs: cargo metadata --format-version=1 --no-deps | \ jq -e -r '.packages[] | select(.name == "'"$CRATE"'") | .dependencies | all(.name != "libp2p")' - - uses: taiki-e/cache-cargo-install-action@924d49e0af41f449f0ad549559bc608ee4653562 # v1 + - uses: taiki-e/cache-cargo-install-action@v2 with: tool: tomlq - name: Extract version from manifest run: | CRATE_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -e -r '.packages[] | select(.name == "'"$CRATE"'") | .version') - + echo "CRATE_VERSION=$CRATE_VERSION" >> $GITHUB_ENV - name: Enforce version in `workspace.dependencies` matches latest version if: env.CRATE != 'libp2p' run: | - SPECIFIED_VERSION=$(tomlq "workspace.dependencies.$CRATE.version" --file ./Cargo.toml) + SPECIFIED_VERSION=$(tq "workspace.dependencies.$CRATE.version" --file ./Cargo.toml) echo "Package version: $CRATE_VERSION"; echo "Specified version: $SPECIFIED_VERSION"; @@ -107,8 +107,6 @@ jobs: wasm_tests: name: Run all WASM tests runs-on: ubuntu-latest - env: - CHROMEDRIVER_VERSION: '114.0.5735.90' steps: - uses: actions/checkout@v4 @@ -116,19 +114,12 @@ jobs: with: target: wasm32-unknown-unknown - - uses: taiki-e/cache-cargo-install-action@v1 + - uses: taiki-e/cache-cargo-install-action@v2 with: tool: wasm-pack@0.12.0 - - name: Install Google Chrome - run: | - curl -o /tmp/google-chrome-stable_amd64.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROMEDRIVER_VERSION}-1_amd64.deb - sudo dpkg -i /tmp/google-chrome-stable_amd64.deb - - name: Install chromedriver uses: nanasess/setup-chromedriver@v2 - with: - chromedriver-version: ${{ env.CHROMEDRIVER_VERSION }} - name: Run all tests run: ./wasm-tests/run-all.sh @@ -156,9 +147,9 @@ jobs: with: target: ${{ matrix.target }} - - uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0 + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: key: ${{ matrix.target }} save-if: ${{ github.ref == 'refs/heads/master' }} @@ -181,9 +172,9 @@ jobs: with: toolchain: ${{ env.MSRV }} - - uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0 + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: save-if: ${{ github.ref == 'refs/heads/master' }} @@ -202,9 +193,9 @@ jobs: - uses: dtolnay/rust-toolchain@stable - - uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0 + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: key: ${{ matrix.features }} save-if: ${{ github.ref == 'refs/heads/master' }} @@ -219,9 +210,9 @@ jobs: - uses: dtolnay/rust-toolchain@stable - - uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0 + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: save-if: ${{ github.ref == 'refs/heads/master' }} @@ -234,7 +225,7 @@ jobs: fail-fast: false matrix: rust-version: [ - 1.75.0, # current stable + 1.80.0, # current stable beta, ] steps: @@ -245,9 +236,9 @@ jobs: toolchain: ${{ matrix.rust-version }} components: clippy - - uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0 + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: save-if: ${{ github.ref == 'refs/heads/master' }} @@ -261,9 +252,9 @@ jobs: - uses: dtolnay/rust-toolchain@stable - - uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0 + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: save-if: ${{ github.ref == 'refs/heads/master' }} @@ -280,9 +271,9 @@ jobs: - uses: dtolnay/rust-toolchain@stable - - uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0 + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: shared-key: stable-cache save-if: false @@ -295,7 +286,7 @@ jobs: cargo check --manifest-path "$toml"; done - - uses: taiki-e/cache-cargo-install-action@v1 + - uses: taiki-e/cache-cargo-install-action@v2 with: tool: wasm-pack@0.12.0 @@ -317,9 +308,9 @@ jobs: RUSTFLAGS: '' steps: - uses: actions/checkout@v4 - - run: wget -q -O- https://github.com/obi1kenobi/cargo-semver-checks/releases/download/v0.27.0/cargo-semver-checks-x86_64-unknown-linux-gnu.tar.gz | tar -xz -C ~/.cargo/bin + - run: wget -q -O- https://github.com/obi1kenobi/cargo-semver-checks/releases/download/v0.33.0/cargo-semver-checks-x86_64-unknown-linux-gnu.tar.gz | tar -xz -C ~/.cargo/bin shell: bash - - uses: obi1kenobi/cargo-semver-checks-action@48f4ef7da6d907d69d18249e0ba79aa98c61b9db # v2 + - uses: obi1kenobi/cargo-semver-checks-action@7272cc2caa468d3e009a2b0a9cc366839348237b # v2.6 rustfmt: runs-on: ubuntu-latest @@ -330,7 +321,7 @@ jobs: with: components: rustfmt - - uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0 + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - name: Check formatting run: cargo fmt -- --check @@ -342,7 +333,7 @@ jobs: - uses: dtolnay/rust-toolchain@stable - - uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0 + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - name: Ensure `full` feature contains all features run: | @@ -374,12 +365,12 @@ jobs: steps: - uses: actions/checkout@v4 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 - run: cargo install --version 0.10.0 pb-rs --locked - name: Glob match - uses: tj-actions/glob@v21 + uses: tj-actions/glob@v22 id: glob with: files: | @@ -400,13 +391,13 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 - run: cargo metadata --locked --format-version=1 > /dev/null cargo-deny: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: EmbarkStudios/cargo-deny-action@v1 + - uses: EmbarkStudios/cargo-deny-action@v2 with: command: check advisories bans licenses sources diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index b9cd82897c2..5cbfc20d69d 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -6,7 +6,6 @@ on: - 'master' tags: - 'libp2p-server-**' - pull_request: jobs: server: @@ -30,15 +29,10 @@ jobs: images: ghcr.io/${{ github.repository }}-server - name: Build and push - uses: docker/build-push-action@v5 + uses: docker/build-push-action@v6 with: context: . file: ./misc/server/Dockerfile - push: ${{ ! github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]' }} # Only push image if we have the required permissions, i.e. not running from a fork - cache-from: ${{ ! github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]' && type=s3,mode=max,bucket=libp2p-by-tf-aws-bootstrap,region=us-east-1,prefix=buildCache,name=rust-libp2p-server }} - cache-to: ${{ ! github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]' && type=s3,mode=max,bucket=libp2p-by-tf-aws-bootstrap,region=us-east-1,prefix=buildCache,name=rust-libp2p-server }} + push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - env: - AWS_ACCESS_KEY_ID: ${{ vars.TEST_PLANS_BUILD_CACHE_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.TEST_PLANS_BUILD_CACHE_KEY }} diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index b2a761fd8c1..e2bac78c006 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -17,7 +17,6 @@ jobs: run: cargo +nightly doc --no-deps --workspace -F full env: RUSTDOCFLAGS: "--cfg docsrs" - RUSTFLAGS: "--cfg docsrs" - name: Add index file run: | mkdir host-docs diff --git a/.github/workflows/interop-test.yml b/.github/workflows/interop-test.yml index f3950897089..57d0f1a692d 100644 --- a/.github/workflows/interop-test.yml +++ b/.github/workflows/interop-test.yml @@ -1,6 +1,5 @@ name: Interoperability Testing on: - pull_request: push: branches: - "master" @@ -12,6 +11,7 @@ concurrency: jobs: run-transport-interop: name: Run transport interoperability tests + if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository runs-on: ${{ fromJSON(github.repository == 'libp2p/rust-libp2p' && '["self-hosted", "linux", "x64", "4xlarge"]' || '"ubuntu-latest"') }} strategy: matrix: @@ -24,8 +24,9 @@ jobs: - name: Build ${{ matrix.flavour }} image run: ./scripts/build-interop-image.sh env: - AWS_ACCESS_KEY_ID: ${{ vars.TEST_PLANS_BUILD_CACHE_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.TEST_PLANS_BUILD_CACHE_KEY }} + AWS_BUCKET_NAME: ${{ vars.S3_LIBP2P_BUILD_CACHE_BUCKET_NAME }} + AWS_ACCESS_KEY_ID: ${{ vars.S3_LIBP2P_BUILD_CACHE_AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_LIBP2P_BUILD_CACHE_AWS_SECRET_ACCESS_KEY }} FLAVOUR: ${{ matrix.flavour }} - name: Run ${{ matrix.flavour }} tests @@ -33,12 +34,13 @@ jobs: with: test-filter: ${{ matrix.flavour }}-rust-libp2p-head extra-versions: ${{ github.workspace }}/interop-tests/${{ matrix.flavour }}-ping-version.json - s3-cache-bucket: libp2p-by-tf-aws-bootstrap - s3-access-key-id: ${{ vars.TEST_PLANS_BUILD_CACHE_KEY_ID }} - s3-secret-access-key: ${{ secrets.TEST_PLANS_BUILD_CACHE_KEY }} + s3-cache-bucket: ${{ vars.S3_LIBP2P_BUILD_CACHE_BUCKET_NAME }} + s3-access-key-id: ${{ vars.S3_LIBP2P_BUILD_CACHE_AWS_ACCESS_KEY_ID }} + s3-secret-access-key: ${{ secrets.S3_LIBP2P_BUILD_CACHE_AWS_SECRET_ACCESS_KEY }} worker-count: 16 run-holepunching-interop: name: Run hole-punch interoperability tests + if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository runs-on: ${{ fromJSON(github.repository == 'libp2p/rust-libp2p' && '["self-hosted", "linux", "x64", "4xlarge"]' || '"ubuntu-latest"') }} steps: - uses: actions/checkout@v4 @@ -50,7 +52,7 @@ jobs: with: test-filter: rust-libp2p-head extra-versions: ${{ github.workspace }}/hole-punching-tests/version.json - s3-cache-bucket: libp2p-by-tf-aws-bootstrap - s3-access-key-id: ${{ vars.TEST_PLANS_BUILD_CACHE_KEY_ID }} - s3-secret-access-key: ${{ secrets.TEST_PLANS_BUILD_CACHE_KEY }} + s3-cache-bucket: ${{ vars.S3_LIBP2P_BUILD_CACHE_BUCKET_NAME }} + s3-access-key-id: ${{ vars.S3_LIBP2P_BUILD_CACHE_AWS_ACCESS_KEY_ID }} + s3-secret-access-key: ${{ secrets.S3_LIBP2P_BUILD_CACHE_AWS_SECRET_ACCESS_KEY }} worker-count: 16 diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c3466708c9..4fccb9a489e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ## Main APIs - [`libp2p-core` CHANGELOG](core/CHANGELOG.md) +- [`libp2p-identity` CHANGELOG](identity/CHANGELOG.md) - [`libp2p-swarm` CHANGELOG](swarm/CHANGELOG.md) - [`libp2p-swarm-derive` CHANGELOG](swarm-derive/CHANGELOG.md) @@ -13,7 +14,6 @@ - [`libp2p-floodsub` CHANGELOG](protocols/floodsub/CHANGELOG.md) - [`libp2p-gossipsub` CHANGELOG](protocols/gossipsub/CHANGELOG.md) - [`libp2p-identify` CHANGELOG](protocols/identify/CHANGELOG.md) -- [`libp2p-identity` CHANGELOG](protocols/identity/CHANGELOG.md) - [`libp2p-kad` CHANGELOG](protocols/kad/CHANGELOG.md) - [`libp2p-mdns` CHANGELOG](protocols/mdns/CHANGELOG.md) - [`libp2p-ping` CHANGELOG](protocols/ping/CHANGELOG.md) diff --git a/Cargo.lock b/Cargo.lock index 52913f9796e..9b8db54e855 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,15 +64,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "aho-corasick" -version = "0.7.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" -dependencies = [ - "memchr", -] - [[package]] name = "aho-corasick" version = "1.0.2" @@ -144,9 +135,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.80" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ad32ce52e4161730f7098c077cd2ed6229b5804ccf99e5366be1ab72a98b4e1" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" [[package]] name = "arbitrary" @@ -178,8 +169,24 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f6fd5ddaf0351dff5b8da21b2fb4ff8e08ddd02857f0bf69c47639106c0fff0" dependencies = [ - "asn1-rs-derive", - "asn1-rs-impl", + "asn1-rs-derive 0.4.0", + "asn1-rs-impl 0.1.0", + "displaydoc", + "nom", + "num-traits", + "rusticata-macros", + "thiserror", + "time", +] + +[[package]] +name = "asn1-rs" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ad1373757efa0f70ec53939aabc7152e1591cb485208052993070ac8d2429d" +dependencies = [ + "asn1-rs-derive 0.5.0", + "asn1-rs-impl 0.2.0", "displaydoc", "nom", "num-traits", @@ -197,7 +204,19 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", +] + +[[package]] +name = "asn1-rs-derive" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7378575ff571966e99a744addeff0bff98b8ada0dedf1956d59e634db95eaac1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", + "synstructure 0.13.1", ] [[package]] @@ -211,6 +230,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "asn1-rs-impl" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b18050c2cd6fe86c3a76584ef5e0baf286d038cda203eb6223df2cc413565f7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "asn1_der" version = "0.7.6" @@ -301,9 +331,9 @@ dependencies = [ [[package]] name = "async-io" -version = "2.3.2" +version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcccb0f599cfa2f8ace422d3555572f47424da5648a4382a9dd0310ff8210884" +checksum = "0d6baa8f0178795da0e71bc42c9e5d13261aac7ee549853162e66a241ba17964" dependencies = [ "async-lock 3.1.0", "cfg-if", @@ -408,7 +438,29 @@ dependencies = [ "futures-util", "hickory-resolver", "pin-utils", - "socket2 0.5.6", + "socket2 0.5.7", +] + +[[package]] +name = "async-stream" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51" +dependencies = [ + "async-stream-impl", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-stream-impl" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", ] [[package]] @@ -419,13 +471,13 @@ checksum = "ecc7ab41815b3c653ccd2978ec3255c81349336702dfdf62ee6f7069b12a3aae" [[package]] name = "async-trait" -version = "0.1.77" +version = "0.1.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" +checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] @@ -477,47 +529,36 @@ dependencies = [ ] [[package]] -name = "axum" -version = "0.6.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" +name = "autonatv2" +version = "0.1.0" dependencies = [ - "async-trait", - "axum-core 0.3.4", - "bitflags 1.3.2", - "bytes", - "futures-util", - "http 0.2.9", - "http-body 0.4.5", - "hyper 0.14.27", - "itoa", - "matchit", - "memchr", - "mime", - "percent-encoding", - "pin-project-lite", - "rustversion", - "serde", - "sync_wrapper", - "tower", - "tower-layer", - "tower-service", + "cfg-if", + "clap", + "libp2p", + "opentelemetry 0.21.0", + "opentelemetry-jaeger", + "opentelemetry_sdk 0.21.2", + "rand 0.8.5", + "tokio", + "tracing", + "tracing-opentelemetry 0.22.0", + "tracing-subscriber", ] [[package]] name = "axum" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1236b4b292f6c4d6dc34604bb5120d85c3fe1d1aa596bd5cc52ca054d13e7b9e" +checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf" dependencies = [ "async-trait", - "axum-core 0.4.3", + "axum-core", "bytes", "futures-util", - "http 1.0.0", - "http-body 1.0.0", + "http 1.1.0", + "http-body", "http-body-util", - "hyper 1.1.0", + "hyper", "hyper-util", "itoa", "matchit", @@ -530,7 +571,7 @@ dependencies = [ "serde_json", "serde_path_to_error", "serde_urlencoded", - "sync_wrapper", + "sync_wrapper 1.0.0", "tokio", "tower", "tower-layer", @@ -538,23 +579,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "axum-core" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "759fa577a247914fd3f7f76d62972792636412fbfd634cd452f6a385a74d2d2c" -dependencies = [ - "async-trait", - "bytes", - "futures-util", - "http 0.2.9", - "http-body 0.4.5", - "mime", - "rustversion", - "tower-layer", - "tower-service", -] - [[package]] name = "axum-core" version = "0.4.3" @@ -564,13 +588,13 @@ dependencies = [ "async-trait", "bytes", "futures-util", - "http 1.0.0", - "http-body 1.0.0", + "http 1.1.0", + "http-body", "http-body-util", "mime", "pin-project-lite", "rustversion", - "sync_wrapper", + "sync_wrapper 0.1.2", "tower-layer", "tower-service", "tracing", @@ -617,9 +641,9 @@ checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" [[package]] name = "base64" -version = "0.22.0" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "base64ct" @@ -627,15 +651,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" -[[package]] -name = "basic-toml" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2db21524cad41c5591204d22d75e1970a2d1f71060214ca931dc7d5afe2c14e5" -dependencies = [ - "serde", -] - [[package]] name = "bimap" version = "0.6.3" @@ -719,7 +734,7 @@ name = "browser-webrtc-example" version = "0.1.0" dependencies = [ "anyhow", - "axum 0.7.4", + "axum", "futures", "js-sys", "libp2p", @@ -742,9 +757,9 @@ dependencies = [ [[package]] name = "bs58" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f5353f36341f7451062466f0b755b96ac3a9547e4d7f6b70d603fc721a7d7896" +checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4" dependencies = [ "tinyvec", ] @@ -773,9 +788,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.5.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" dependencies = [ "serde", ] @@ -907,9 +922,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.16" +version = "4.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58e54881c004cec7895b0068a0a954cd5d62da01aef83fa35b1e594497bf5445" +checksum = "a9689a29b593160de5bc4aacab7b5d54fb52231de70122626c178e6a368994c7" dependencies = [ "clap_builder", "clap_derive", @@ -917,9 +932,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.16" +version = "4.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59cb82d7f531603d2fd1f507441cdd35184fa81beff7bd489570de7f773460bb" +checksum = "2e5387378c84f6faa26890ebf9f0a92989f8873d4d380467bcd0d8d8620424df" dependencies = [ "anstream", "anstyle", @@ -929,21 +944,21 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.4.7" +version = "4.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" dependencies = [ - "heck", + "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] name = "clap_lex" -version = "0.6.0" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" [[package]] name = "colorchoice" @@ -1050,6 +1065,7 @@ dependencies = [ "ciborium", "clap", "criterion-plot", + "futures", "is-terminal", "itertools", "num-traits", @@ -1062,6 +1078,7 @@ dependencies = [ "serde_derive", "serde_json", "tinytemplate", + "tokio", "walkdir", ] @@ -1077,11 +1094,10 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.5.8" +version = "0.5.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" dependencies = [ - "cfg-if", "crossbeam-utils", ] @@ -1111,12 +1127,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.16" +version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" -dependencies = [ - "cfg-if", -] +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" [[package]] name = "crunchy" @@ -1179,16 +1192,15 @@ dependencies = [ [[package]] name = "curve25519-dalek" -version = "4.1.2" +version = "4.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a677b8922c94e01bdbb12126b0bc852f00447528dee1782229af9c720c3f348" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" dependencies = [ "cfg-if", "cpufeatures", "curve25519-dalek-derive", "digest 0.10.7", "fiat-crypto", - "platforms", "rustc_version", "subtle", "zeroize", @@ -1202,14 +1214,14 @@ checksum = "83fdaf97f4804dcebfa5862639bc9ce4121e82140bec2a987ac5140294865b5b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] name = "data-encoding" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" [[package]] name = "data-encoding-macro" @@ -1262,7 +1274,21 @@ version = "8.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbd676fbbab537128ef0278adb5576cf363cff6aa22a7b24effe97347cfab61e" dependencies = [ - "asn1-rs", + "asn1-rs 0.5.2", + "displaydoc", + "nom", + "num-bigint", + "num-traits", + "rusticata-macros", +] + +[[package]] +name = "der-parser" +version = "9.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cd0a5c643689626bec213c4d8bd4d96acc8ffdb4ad4bb6bc16abf27d5f4b553" +dependencies = [ + "asn1-rs 0.6.1", "displaydoc", "nom", "num-bigint", @@ -1270,6 +1296,15 @@ dependencies = [ "rusticata-macros", ] +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", +] + [[package]] name = "digest" version = "0.9.0" @@ -1319,7 +1354,7 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] @@ -1381,15 +1416,15 @@ dependencies = [ [[package]] name = "either" -version = "1.9.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" [[package]] name = "elliptic-curve" -version = "0.13.5" +version = "0.13.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "968405c8fdc9b3bf4df0a6638858cc0b52462836ab6b1c87377785dd09cf1c0b" +checksum = "b5e6043086bf7973472e0c7dff2142ea0b680d30e18d9cc40f267efbf222bd47" dependencies = [ "base16ct", "crypto-bigint", @@ -1421,10 +1456,10 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ffccbb6966c05b32ef8fbac435df276c4ae4d3dc55a8cd0eb9745e6c12f546a" dependencies = [ - "heck", + "heck 0.4.1", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] @@ -1585,9 +1620,9 @@ dependencies = [ [[package]] name = "futures-bounded" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1e2774cc104e198ef3d3e1ff4ab40f86fa3245d6cb6a3a46174f21463cee173" +checksum = "91f328e7fb845fc832912fb6a34f40cf6d1888c92f974d1893a54e97b5ff542e" dependencies = [ "futures-timer", "futures-util", @@ -1660,17 +1695,18 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] name = "futures-rustls" -version = "0.24.0" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35bd3cf68c183738046838e300353e4716c674dc5e56890de4826801a6622a28" +checksum = "a8f2f12607f92c69b12ed746fabf9ca4f5c482cba46679c1a75b874ed7c26adb" dependencies = [ "futures-io", - "rustls", + "rustls 0.23.11", + "rustls-pki-types", ] [[package]] @@ -1760,9 +1796,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.12" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ "cfg-if", "js-sys", @@ -1795,11 +1831,11 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] name = "globset" -version = "0.4.10" +version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "029d74589adefde59de1a0c4f4732695c32805624aec7b68d91503d4dba79afc" +checksum = "1391ab1f92ffcc08911957149833e682aa3fe252b9f45f966d2ef972274c97df" dependencies = [ - "aho-corasick 0.7.20", + "aho-corasick", "bstr", "fnv", "log", @@ -1831,35 +1867,16 @@ dependencies = [ [[package]] name = "h2" -version = "0.3.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9" -dependencies = [ - "bytes", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http 0.2.9", - "indexmap 2.2.1", - "slab", - "tokio", - "tokio-util", - "tracing", -] - -[[package]] -name = "h2" -version = "0.4.2" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31d030e59af851932b72ceebadf4a2b5986dba4c3b99dd2493f8273a0f151943" +checksum = "816ec7294445779408f36fe57bc5b7fc1cf59664059096c65f905c1c61f58069" dependencies = [ "bytes", "fnv", "futures-core", "futures-sink", "futures-util", - "http 1.0.0", + "http 1.1.0", "indexmap 2.2.1", "slab", "tokio", @@ -1895,6 +1912,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.2" @@ -1921,9 +1944,9 @@ checksum = "b07f60793ff0a4d9cef0f18e63b5357e06209987153a64648c972c1e5aff336f" [[package]] name = "hickory-proto" -version = "0.24.0" +version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "091a6fbccf4860009355e3efc52ff4acf37a63489aad7435372d44ceeb6fbbcf" +checksum = "07698b8420e2f0d6447a436ba999ec85d8fbf2a398bbd737b82cac4a2e96e512" dependencies = [ "async-trait", "cfg-if", @@ -1936,7 +1959,7 @@ dependencies = [ "ipnet", "once_cell", "rand 0.8.5", - "socket2 0.5.6", + "socket2 0.5.7", "thiserror", "tinyvec", "tokio", @@ -1946,9 +1969,9 @@ dependencies = [ [[package]] name = "hickory-resolver" -version = "0.24.0" +version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35b8f021164e6a984c9030023544c57789c51760065cd510572fedcfb04164e8" +checksum = "28757f23aa75c98f254cf0405e6d8c25b831b32921b050a66692427679b1f243" dependencies = [ "cfg-if", "futures-util", @@ -2044,26 +2067,15 @@ dependencies = [ [[package]] name = "http" -version = "1.0.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" dependencies = [ "bytes", "fnv", "itoa", ] -[[package]] -name = "http-body" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" -dependencies = [ - "bytes", - "http 0.2.9", - "pin-project-lite", -] - [[package]] name = "http-body" version = "1.0.0" @@ -2071,7 +2083,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" dependencies = [ "bytes", - "http 1.0.0", + "http 1.1.0", ] [[package]] @@ -2082,8 +2094,8 @@ checksum = "41cb79eb393015dadd30fc252023adb0b2400a0caee0fa2a077e6e21a551e840" dependencies = [ "bytes", "futures-util", - "http 1.0.0", - "http-body 1.0.0", + "http 1.1.0", + "http-body", "pin-project-lite", ] @@ -2113,101 +2125,87 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hyper" -version = "0.14.27" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb1cfd654a8219eaef89881fdb3bb3b1cdc5fa75ded05d6933b2b382e395468" +checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" dependencies = [ "bytes", "futures-channel", - "futures-core", "futures-util", - "h2 0.3.24", - "http 0.2.9", - "http-body 0.4.5", + "h2", + "http 1.1.0", + "http-body", "httparse", "httpdate", "itoa", "pin-project-lite", - "socket2 0.4.9", + "smallvec", "tokio", - "tower-service", - "tracing", "want", ] -[[package]] -name = "hyper" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb5aa53871fc917b1a9ed87b683a5d86db645e23acb32c2e0785a353e522fb75" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "h2 0.4.2", - "http 1.0.0", - "http-body 1.0.0", - "httparse", - "httpdate", - "itoa", - "pin-project-lite", - "tokio", -] - [[package]] name = "hyper-rustls" -version = "0.24.2" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" +checksum = "a0bea761b46ae2b24eb4aef630d8d1c398157b6fc29e6350ecf090a0b70c952c" dependencies = [ "futures-util", - "http 0.2.9", - "hyper 0.14.27", - "rustls", + "http 1.1.0", + "hyper", + "hyper-util", + "rustls 0.22.4", + "rustls-pki-types", "tokio", "tokio-rustls", + "tower-service", ] [[package]] name = "hyper-timeout" -version = "0.4.1" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1" +checksum = "3203a961e5c83b6f5498933e78b6b263e208c197b63e9c6c53cc82ffd3f63793" dependencies = [ - "hyper 0.14.27", + "hyper", + "hyper-util", "pin-project-lite", "tokio", - "tokio-io-timeout", + "tower-service", ] [[package]] name = "hyper-tls" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" dependencies = [ "bytes", - "hyper 0.14.27", + "http-body-util", + "hyper", + "hyper-util", "native-tls", "tokio", "tokio-native-tls", + "tower-service", ] [[package]] name = "hyper-util" -version = "0.1.2" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdea9aac0dbe5a9240d68cfd9501e2db94222c6dc06843e06640b9e07f0fdc67" +checksum = "41296eb09f183ac68eec06e03cdbea2e759633d4067b2f6552fc2e009bcad08b" dependencies = [ "bytes", "futures-channel", "futures-util", - "http 1.0.0", - "http-body 1.0.0", - "hyper 1.1.0", + "http 1.1.0", + "http-body", + "hyper", "pin-project-lite", - "socket2 0.5.6", + "socket2 0.5.7", "tokio", + "tower-service", "tracing", ] @@ -2215,10 +2213,9 @@ dependencies = [ name = "identify-example" version = "0.1.0" dependencies = [ - "async-std", - "async-trait", "futures", "libp2p", + "tokio", "tracing", "tracing-subscriber", ] @@ -2259,7 +2256,7 @@ version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6b0422c86d7ce0e97169cc42e04ae643caf278874a7a3c87b8150a220dc7e1e" dependencies = [ - "async-io 2.3.2", + "async-io 2.3.3", "core-foundation", "fnv", "futures", @@ -2268,23 +2265,25 @@ dependencies = [ "log", "rtnetlink", "smol", - "system-configuration 0.5.1", + "system-configuration", "tokio", - "windows", + "windows 0.51.1", ] [[package]] name = "igd-next" -version = "0.14.3" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "064d90fec10d541084e7b39ead8875a5a80d9114a2b18791565253bae25f49e4" +checksum = "76b0d7d4541def58a37bf8efc559683f21edce7c82f0d866c93ac21f7e098f93" dependencies = [ "async-trait", "attohttpc", "bytes", "futures", - "http 0.2.9", - "hyper 0.14.27", + "http 1.1.0", + "http-body-util", + "hyper", + "hyper-util", "log", "rand 0.8.5", "tokio", @@ -2324,16 +2323,19 @@ dependencies = [ [[package]] name = "instant" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" dependencies = [ "cfg-if", - "js-sys", - "wasm-bindgen", - "web-sys", ] +[[package]] +name = "integer-encoding" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bb03732005da905c88227371639bf1ad885cc712789c011c31c5fb3ab3ccf02" + [[package]] name = "interceptor" version = "0.10.0" @@ -2350,7 +2352,7 @@ dependencies = [ "tokio", "waitgroup", "webrtc-srtp", - "webrtc-util", + "webrtc-util 0.8.1", ] [[package]] @@ -2358,12 +2360,11 @@ name = "interop-tests" version = "0.1.0" dependencies = [ "anyhow", - "axum 0.7.4", + "axum", "console_error_panic_hook", "either", "futures", "futures-timer", - "instant", "libp2p", "libp2p-mplex", "libp2p-noise", @@ -2385,6 +2386,7 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "wasm-logger", + "web-time 1.1.0", ] [[package]] @@ -2404,10 +2406,10 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b58db92f96b720de98181bbbe63c831e87005ab460c1bf306eb2622b4707997f" dependencies = [ - "socket2 0.5.6", + "socket2 0.5.7", "widestring", "windows-sys 0.48.0", - "winreg", + "winreg 0.50.0", ] [[package]] @@ -2472,9 +2474,9 @@ checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "js-sys" -version = "0.3.69" +version = "0.3.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" dependencies = [ "wasm-bindgen", ] @@ -2492,7 +2494,7 @@ dependencies = [ name = "keygen" version = "0.1.0" dependencies = [ - "base64 0.21.7", + "base64 0.22.1", "clap", "libp2p-core", "libp2p-identity", @@ -2518,13 +2520,13 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.153" +version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "libp2p" -version = "0.53.2" +version = "0.54.1" dependencies = [ "async-std", "async-trait", @@ -2533,8 +2535,7 @@ dependencies = [ "either", "futures", "futures-timer", - "getrandom 0.2.12", - "instant", + "getrandom 0.2.15", "libp2p-allow-block-list", "libp2p-autonat", "libp2p-connection-limits", @@ -2577,7 +2578,7 @@ dependencies = [ [[package]] name = "libp2p-allow-block-list" -version = "0.3.0" +version = "0.4.1" dependencies = [ "async-std", "libp2p-core", @@ -2590,15 +2591,18 @@ dependencies = [ [[package]] name = "libp2p-autonat" -version = "0.12.0" +version = "0.13.1" dependencies = [ "async-std", "async-trait", "asynchronous-codec", + "bytes", + "either", "futures", + "futures-bounded", "futures-timer", - "instant", "libp2p-core", + "libp2p-identify", "libp2p-identity", "libp2p-request-response", "libp2p-swarm", @@ -2606,13 +2610,18 @@ dependencies = [ "quick-protobuf", "quick-protobuf-codec", "rand 0.8.5", + "rand_core 0.6.4", + "thiserror", + "tokio", "tracing", "tracing-subscriber", + "void", + "web-time 1.1.0", ] [[package]] name = "libp2p-connection-limits" -version = "0.3.1" +version = "0.4.0" dependencies = [ "async-std", "libp2p-core", @@ -2629,14 +2638,13 @@ dependencies = [ [[package]] name = "libp2p-core" -version = "0.41.2" +version = "0.42.0" dependencies = [ "async-std", "either", "fnv", "futures", "futures-timer", - "instant", "libp2p-identity", "libp2p-mplex", "libp2p-noise", @@ -2656,11 +2664,12 @@ dependencies = [ "tracing", "unsigned-varint 0.8.0", "void", + "web-time 1.1.0", ] [[package]] name = "libp2p-dcutr" -version = "0.11.0" +version = "0.12.0" dependencies = [ "async-std", "asynchronous-codec", @@ -2669,7 +2678,6 @@ dependencies = [ "futures", "futures-bounded", "futures-timer", - "instant", "libp2p-core", "libp2p-dns", "libp2p-identify", @@ -2690,11 +2698,12 @@ dependencies = [ "tracing", "tracing-subscriber", "void", + "web-time 1.1.0", ] [[package]] name = "libp2p-dns" -version = "0.41.1" +version = "0.42.0" dependencies = [ "async-std", "async-std-resolver", @@ -2712,7 +2721,7 @@ dependencies = [ [[package]] name = "libp2p-floodsub" -version = "0.44.0" +version = "0.45.0" dependencies = [ "asynchronous-codec", "bytes", @@ -2732,21 +2741,20 @@ dependencies = [ [[package]] name = "libp2p-gossipsub" -version = "0.46.1" +version = "0.47.1" dependencies = [ "async-std", "asynchronous-codec", - "base64 0.21.7", + "base64 0.22.1", "byteorder", "bytes", "either", "fnv", "futures", "futures-ticker", - "getrandom 0.2.12", + "getrandom 0.2.15", "hex", "hex_fmt", - "instant", "libp2p-core", "libp2p-identity", "libp2p-noise", @@ -2765,11 +2773,12 @@ dependencies = [ "tracing", "tracing-subscriber", "void", + "web-time 1.1.0", ] [[package]] name = "libp2p-identify" -version = "0.44.2" +version = "0.45.1" dependencies = [ "async-std", "asynchronous-codec", @@ -2793,10 +2802,10 @@ dependencies = [ [[package]] name = "libp2p-identity" -version = "0.2.8" +version = "0.2.9" dependencies = [ "asn1_der", - "base64 0.21.7", + "base64 0.22.1", "bs58", "criterion", "ed25519-dalek", @@ -2808,7 +2817,7 @@ dependencies = [ "quick-protobuf", "quickcheck-ext", "rand 0.8.5", - "ring 0.17.5", + "ring 0.17.8", "rmp-serde", "sec1", "serde", @@ -2822,7 +2831,7 @@ dependencies = [ [[package]] name = "libp2p-kad" -version = "0.45.4" +version = "0.47.0" dependencies = [ "arrayvec", "async-std", @@ -2833,7 +2842,6 @@ dependencies = [ "futures", "futures-bounded", "futures-timer", - "instant", "libp2p-core", "libp2p-identify", "libp2p-identity", @@ -2853,13 +2861,14 @@ dependencies = [ "tracing-subscriber", "uint", "void", + "web-time 1.1.0", ] [[package]] name = "libp2p-mdns" -version = "0.45.1" +version = "0.46.0" dependencies = [ - "async-io 2.3.2", + "async-io 2.3.3", "async-std", "data-encoding", "futures", @@ -2874,7 +2883,7 @@ dependencies = [ "libp2p-yamux", "rand 0.8.5", "smallvec", - "socket2 0.5.6", + "socket2 0.5.7", "tokio", "tracing", "tracing-subscriber", @@ -2883,7 +2892,7 @@ dependencies = [ [[package]] name = "libp2p-memory-connection-limits" -version = "0.2.0" +version = "0.3.0" dependencies = [ "async-std", "libp2p-core", @@ -2901,10 +2910,9 @@ dependencies = [ [[package]] name = "libp2p-metrics" -version = "0.14.1" +version = "0.15.0" dependencies = [ "futures", - "instant", "libp2p-core", "libp2p-dcutr", "libp2p-gossipsub", @@ -2916,11 +2924,12 @@ dependencies = [ "libp2p-swarm", "pin-project", "prometheus-client", + "web-time 1.1.0", ] [[package]] name = "libp2p-mplex" -version = "0.41.0" +version = "0.42.0" dependencies = [ "async-std", "asynchronous-codec", @@ -2955,7 +2964,7 @@ dependencies = [ [[package]] name = "libp2p-noise" -version = "0.44.0" +version = "0.45.0" dependencies = [ "asynchronous-codec", "bytes", @@ -2982,14 +2991,13 @@ dependencies = [ [[package]] name = "libp2p-perf" -version = "0.3.0" +version = "0.4.0" dependencies = [ "anyhow", "clap", "futures", "futures-bounded", "futures-timer", - "instant", "libp2p", "libp2p-core", "libp2p-dns", @@ -3008,17 +3016,17 @@ dependencies = [ "tracing", "tracing-subscriber", "void", + "web-time 1.1.0", ] [[package]] name = "libp2p-ping" -version = "0.44.0" +version = "0.45.0" dependencies = [ "async-std", "either", "futures", "futures-timer", - "instant", "libp2p-core", "libp2p-identity", "libp2p-swarm", @@ -3028,11 +3036,12 @@ dependencies = [ "tracing", "tracing-subscriber", "void", + "web-time 1.1.0", ] [[package]] name = "libp2p-plaintext" -version = "0.41.0" +version = "0.42.0" dependencies = [ "asynchronous-codec", "bytes", @@ -3050,7 +3059,7 @@ dependencies = [ [[package]] name = "libp2p-pnet" -version = "0.24.0" +version = "0.25.0" dependencies = [ "futures", "libp2p-core", @@ -3071,7 +3080,7 @@ dependencies = [ [[package]] name = "libp2p-quic" -version = "0.10.2" +version = "0.11.1" dependencies = [ "async-std", "bytes", @@ -3089,9 +3098,9 @@ dependencies = [ "quickcheck", "quinn", "rand 0.8.5", - "ring 0.16.20", - "rustls", - "socket2 0.5.6", + "ring 0.17.8", + "rustls 0.23.11", + "socket2 0.5.7", "thiserror", "tokio", "tracing", @@ -3100,7 +3109,7 @@ dependencies = [ [[package]] name = "libp2p-relay" -version = "0.17.1" +version = "0.18.0" dependencies = [ "asynchronous-codec", "bytes", @@ -3108,7 +3117,6 @@ dependencies = [ "futures", "futures-bounded", "futures-timer", - "instant", "libp2p-core", "libp2p-identity", "libp2p-ping", @@ -3125,18 +3133,18 @@ dependencies = [ "tracing", "tracing-subscriber", "void", + "web-time 1.1.0", ] [[package]] name = "libp2p-rendezvous" -version = "0.14.0" +version = "0.15.0" dependencies = [ "async-trait", "asynchronous-codec", "bimap", "futures", "futures-timer", - "instant", "libp2p-core", "libp2p-identify", "libp2p-identity", @@ -3155,11 +3163,12 @@ dependencies = [ "tracing", "tracing-subscriber", "void", + "web-time 1.1.0", ] [[package]] name = "libp2p-request-response" -version = "0.26.2" +version = "0.27.0" dependencies = [ "anyhow", "async-std", @@ -3169,7 +3178,6 @@ dependencies = [ "futures-bounded", "futures-timer", "futures_ringbuf", - "instant", "libp2p-core", "libp2p-identity", "libp2p-noise", @@ -3184,17 +3192,18 @@ dependencies = [ "tracing", "tracing-subscriber", "void", + "web-time 1.1.0", ] [[package]] name = "libp2p-server" -version = "0.12.7" +version = "0.12.8" dependencies = [ - "base64 0.21.7", + "axum", + "base64 0.22.1", "clap", "futures", "futures-timer", - "hyper 0.14.27", "libp2p", "prometheus-client", "serde", @@ -3208,7 +3217,7 @@ dependencies = [ [[package]] name = "libp2p-stream" -version = "0.1.0-alpha.1" +version = "0.2.0-alpha" dependencies = [ "futures", "libp2p-core", @@ -3224,15 +3233,15 @@ dependencies = [ [[package]] name = "libp2p-swarm" -version = "0.44.2" +version = "0.45.2" dependencies = [ "async-std", + "criterion", "either", "fnv", "futures", "futures-timer", - "getrandom 0.2.12", - "instant", + "getrandom 0.2.15", "libp2p-core", "libp2p-identify", "libp2p-identity", @@ -3254,21 +3263,22 @@ dependencies = [ "trybuild", "void", "wasm-bindgen-futures", + "web-time 1.1.0", ] [[package]] name = "libp2p-swarm-derive" -version = "0.34.3" +version = "0.35.0" dependencies = [ - "heck", + "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] name = "libp2p-swarm-test" -version = "0.3.0" +version = "0.4.1" dependencies = [ "async-trait", "futures", @@ -3285,9 +3295,9 @@ dependencies = [ [[package]] name = "libp2p-tcp" -version = "0.41.0" +version = "0.42.0" dependencies = [ - "async-io 2.3.2", + "async-io 2.3.3", "async-std", "futures", "futures-timer", @@ -3295,7 +3305,7 @@ dependencies = [ "libc", "libp2p-core", "libp2p-identity", - "socket2 0.5.6", + "socket2 0.5.7", "tokio", "tracing", "tracing-subscriber", @@ -3303,7 +3313,7 @@ dependencies = [ [[package]] name = "libp2p-tls" -version = "0.3.0" +version = "0.5.0" dependencies = [ "futures", "futures-rustls", @@ -3314,18 +3324,18 @@ dependencies = [ "libp2p-swarm", "libp2p-yamux", "rcgen", - "ring 0.16.20", - "rustls", - "rustls-webpki", + "ring 0.17.8", + "rustls 0.23.11", + "rustls-webpki 0.101.7", "thiserror", "tokio", - "x509-parser", + "x509-parser 0.16.0", "yasna", ] [[package]] name = "libp2p-uds" -version = "0.40.0" +version = "0.41.0" dependencies = [ "async-std", "futures", @@ -3337,7 +3347,7 @@ dependencies = [ [[package]] name = "libp2p-upnp" -version = "0.2.1" +version = "0.3.1" dependencies = [ "futures", "futures-timer", @@ -3351,7 +3361,7 @@ dependencies = [ [[package]] name = "libp2p-webrtc" -version = "0.7.1-alpha" +version = "0.8.0-alpha" dependencies = [ "async-trait", "bytes", @@ -3368,7 +3378,7 @@ dependencies = [ "rand 0.8.5", "rcgen", "serde", - "stun", + "stun 0.6.0", "thiserror", "tinytemplate", "tokio", @@ -3380,7 +3390,7 @@ dependencies = [ [[package]] name = "libp2p-webrtc-utils" -version = "0.2.0" +version = "0.3.0" dependencies = [ "asynchronous-codec", "bytes", @@ -3402,11 +3412,11 @@ dependencies = [ [[package]] name = "libp2p-webrtc-websys" -version = "0.3.0-alpha" +version = "0.4.0-alpha.2" dependencies = [ "bytes", "futures", - "getrandom 0.2.12", + "getrandom 0.2.15", "hex", "js-sys", "libp2p-core", @@ -3422,7 +3432,7 @@ dependencies = [ [[package]] name = "libp2p-websocket" -version = "0.43.0" +version = "0.44.0" dependencies = [ "async-std", "either", @@ -3437,14 +3447,15 @@ dependencies = [ "rcgen", "rw-stream-sink", "soketto", + "thiserror", "tracing", "url", - "webpki-roots", + "webpki-roots 0.25.2", ] [[package]] name = "libp2p-websocket-websys" -version = "0.3.1" +version = "0.4.0" dependencies = [ "bytes", "futures", @@ -3463,7 +3474,7 @@ dependencies = [ [[package]] name = "libp2p-webtransport-websys" -version = "0.2.0" +version = "0.4.0" dependencies = [ "futures", "js-sys", @@ -3483,7 +3494,7 @@ dependencies = [ [[package]] name = "libp2p-yamux" -version = "0.45.1" +version = "0.46.0" dependencies = [ "async-std", "either", @@ -3493,7 +3504,7 @@ dependencies = [ "thiserror", "tracing", "yamux 0.12.1", - "yamux 0.13.1", + "yamux 0.13.3", ] [[package]] @@ -3581,11 +3592,19 @@ dependencies = [ "value-bag", ] +[[package]] +name = "logging" +version = "0.0.0" +dependencies = [ + "tracing", + "tracing-subscriber", +] + [[package]] name = "lru" -version = "0.12.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db2c024b41519440580066ba82aab04092b333e09066a5eb86c7c4890df31f22" +checksum = "d3262e75e648fce39813cb56ac41f3c3e3f65217ebf3844d818d1f9398cfb0dc" dependencies = [ "hashbrown 0.14.3", ] @@ -3667,16 +3686,16 @@ dependencies = [ name = "metrics-example" version = "0.1.0" dependencies = [ + "axum", "futures", - "hyper 0.14.27", "libp2p", - "opentelemetry", + "opentelemetry 0.25.0", "opentelemetry-otlp", - "opentelemetry_api", + "opentelemetry_sdk 0.25.0", "prometheus-client", "tokio", "tracing", - "tracing-opentelemetry", + "tracing-opentelemetry 0.26.0", "tracing-subscriber", ] @@ -3696,6 +3715,16 @@ dependencies = [ "unicase", ] +[[package]] +name = "minicov" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c71e683cd655513b99affab7d317deb690528255a0d5f717f1024093c12b169" +dependencies = [ + "cc", + "walkdir", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -3939,6 +3968,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "num-integer" version = "0.1.45" @@ -3951,9 +3986,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] @@ -3983,7 +4018,16 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9bedf36ffb6ba96c2eb7144ef6270557b52e54b20c0a8e1eb2ff99a6c6959bff" dependencies = [ - "asn1-rs", + "asn1-rs 0.5.2", +] + +[[package]] +name = "oid-registry" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c958dd45046245b9c3c2547369bb634eb461670b2e7e0de552905801a648d1d" +dependencies = [ + "asn1-rs 0.6.1", ] [[package]] @@ -4006,9 +4050,9 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "openssl" -version = "0.10.60" +version = "0.10.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79a4c6c3a2b158f7f8f2a2fc5a969fa3a068df6fc9dbb4a43845436e3af7c800" +checksum = "9529f4786b70a3e8c61e11179af17ab6188ad8d0ded78c5529441ed39d4bd9c1" dependencies = [ "bitflags 2.4.1", "cfg-if", @@ -4027,7 +4071,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] @@ -4038,9 +4082,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.96" +version = "0.9.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3812c071ba60da8b5677cc12bcb1d42989a65553772897a7e0355545a819838f" +checksum = "7f9e8deee91df40a943c71b917e5874b951d32a802526c85721ce3b776c929d6" dependencies = [ "cc", "libc", @@ -4050,27 +4094,62 @@ dependencies = [ [[package]] name = "opentelemetry" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e32339a5dc40459130b3bd269e9892439f55b33e772d2a9d402a789baaf4e8a" +dependencies = [ + "futures-core", + "futures-sink", + "indexmap 2.2.1", + "js-sys", + "once_cell", + "pin-project-lite", + "thiserror", + "urlencoding", +] + +[[package]] +name = "opentelemetry" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "803801d3d3b71cd026851a53f974ea03df3d179cb758b260136a6c9e22e196af" +dependencies = [ + "futures-core", + "futures-sink", + "js-sys", + "once_cell", + "pin-project-lite", + "thiserror", +] + +[[package]] +name = "opentelemetry-jaeger" version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9591d937bc0e6d2feb6f71a559540ab300ea49955229c347a517a28d27784c54" +checksum = "e617c66fd588e40e0dbbd66932fdc87393095b125d4459b1a3a10feb1712f8a1" dependencies = [ - "opentelemetry_api", - "opentelemetry_sdk", + "async-trait", + "futures-core", + "futures-util", + "opentelemetry 0.21.0", + "opentelemetry-semantic-conventions", + "opentelemetry_sdk 0.21.2", + "thrift", + "tokio", ] [[package]] name = "opentelemetry-otlp" -version = "0.13.0" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e5e5a5c4135864099f3faafbe939eb4d7f9b80ebf68a8448da961b32a7c1275" +checksum = "596b1719b3cab83addb20bcbffdf21575279d9436d9ccccfe651a3bf0ab5ab06" dependencies = [ "async-trait", "futures-core", - "http 0.2.9", + "http 1.1.0", + "opentelemetry 0.25.0", "opentelemetry-proto", - "opentelemetry-semantic-conventions", - "opentelemetry_api", - "opentelemetry_sdk", + "opentelemetry_sdk 0.25.0", "prost", "thiserror", "tokio", @@ -4079,58 +4158,62 @@ dependencies = [ [[package]] name = "opentelemetry-proto" -version = "0.3.0" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1e3f814aa9f8c905d0ee4bde026afd3b2577a97c10e1699912e3e44f0c4cbeb" +checksum = "2c43620e8f93359eb7e627a3b16ee92d8585774986f24f2ab010817426c5ce61" dependencies = [ - "opentelemetry_api", - "opentelemetry_sdk", + "opentelemetry 0.25.0", + "opentelemetry_sdk 0.25.0", "prost", "tonic", ] [[package]] name = "opentelemetry-semantic-conventions" -version = "0.12.0" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73c9f9340ad135068800e7f1b24e9e09ed9e7143f5bf8518ded3d3ec69789269" +checksum = "f5774f1ef1f982ef2a447f6ee04ec383981a3ab99c8e77a1a7b30182e65bbc84" dependencies = [ - "opentelemetry", + "opentelemetry 0.21.0", ] [[package]] -name = "opentelemetry_api" -version = "0.20.0" +name = "opentelemetry_sdk" +version = "0.21.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a81f725323db1b1206ca3da8bb19874bbd3f57c3bcd59471bfb04525b265b9b" +checksum = "2f16aec8a98a457a52664d69e0091bac3a0abd18ead9b641cb00202ba4e0efe4" dependencies = [ + "async-trait", + "crossbeam-channel", "futures-channel", + "futures-executor", "futures-util", - "indexmap 1.9.3", - "js-sys", + "glob", "once_cell", - "pin-project-lite", + "opentelemetry 0.21.0", + "ordered-float 4.2.0", + "percent-encoding", + "rand 0.8.5", "thiserror", - "urlencoding", + "tokio", + "tokio-stream", ] [[package]] name = "opentelemetry_sdk" -version = "0.20.0" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa8e705a0612d48139799fcbaba0d4a90f06277153e43dd2bdc16c6f0edd8026" +checksum = "e0da0d6b47a3dbc6e9c9e36a0520e25cf943e046843818faaa3f87365a548c82" dependencies = [ "async-trait", - "crossbeam-channel", "futures-channel", "futures-executor", "futures-util", + "glob", "once_cell", - "opentelemetry_api", - "ordered-float", + "opentelemetry 0.25.0", "percent-encoding", "rand 0.8.5", - "regex", "serde_json", "thiserror", "tokio", @@ -4139,9 +4222,18 @@ dependencies = [ [[package]] name = "ordered-float" -version = "3.9.2" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1e1c390732d15f1d48471625cd92d154e66db2c56645e29a9cd26f4699f72dc" +checksum = "68f19d67e5a2795c94e73e0bb1cc1a7edeb2e28efd39e2e1c9b7a40c1108b11c" +dependencies = [ + "num-traits", +] + +[[package]] +name = "ordered-float" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a76df7075c7d4d01fdcb46c912dd17fba5b60c78ea480b475f2b6ab6f666584e" dependencies = [ "num-traits", ] @@ -4184,9 +4276,9 @@ checksum = "14f2252c834a40ed9bb5422029649578e63aa341ac401f74e719dd1afda8394e" [[package]] name = "parking_lot" -version = "0.12.1" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" dependencies = [ "lock_api", "parking_lot_core", @@ -4238,29 +4330,29 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pin-project" -version = "1.1.4" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0302c4a0442c456bd56f841aee5c3bfd17967563f6fadc9ceb9f9c23cf3807e0" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.1.4" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "266c042b60c9c76b8d53061e52b2e0d1116abc57cefc8c5cd671619a56ac3690" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] name = "pin-project-lite" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" [[package]] name = "pin-utils" @@ -4295,12 +4387,6 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" -[[package]] -name = "platforms" -version = "3.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3d7ddaed09e0eb771a79ab0fd64609ba0afb0a8366421957936ad14cbd13630" - [[package]] name = "plotters" version = "0.3.5" @@ -4382,6 +4468,18 @@ dependencies = [ "universal-hash", ] +[[package]] +name = "portable-atomic" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -4397,35 +4495,11 @@ dependencies = [ "elliptic-curve", ] -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro2" -version = "1.0.78" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" dependencies = [ "unicode-ident", ] @@ -4450,14 +4524,14 @@ checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] name = "prost" -version = "0.11.9" +version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd" +checksum = "7b0487d90e047de87f984913713b85c601c05609aad5b0df4b4573fbf69aa13f" dependencies = [ "bytes", "prost-derive", @@ -4465,15 +4539,15 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.11.9" +version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4" +checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" dependencies = [ "anyhow", "itertools", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.66", ] [[package]] @@ -4526,19 +4600,19 @@ dependencies = [ [[package]] name = "quinn" -version = "0.10.2" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cc2c5017e4b43d5995dcea317bc46c1e09404c0a9664d2908f7f02dfe943d75" +checksum = "e4ceeeeabace7857413798eb1ffa1e9c905a9946a57d81fb69b4b71c4d8eb3ad" dependencies = [ - "async-io 1.13.0", + "async-io 2.3.3", "async-std", "bytes", "futures-io", "pin-project-lite", "quinn-proto", "quinn-udp", - "rustc-hash", - "rustls", + "rustc-hash 1.1.0", + "rustls 0.23.11", "thiserror", "tokio", "tracing", @@ -4546,15 +4620,15 @@ dependencies = [ [[package]] name = "quinn-proto" -version = "0.10.5" +version = "0.11.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c78e758510582acc40acb90458401172d41f1016f8c9dde89e49677afb7eec1" +checksum = "fadfaed2cd7f389d0161bb73eeb07b7b78f8691047a6f3e73caaeae55310a4a6" dependencies = [ "bytes", "rand 0.8.5", - "ring 0.16.20", - "rustc-hash", - "rustls", + "ring 0.17.8", + "rustc-hash 2.0.0", + "rustls 0.23.11", "slab", "thiserror", "tinyvec", @@ -4563,22 +4637,22 @@ dependencies = [ [[package]] name = "quinn-udp" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6df19e284d93757a9fb91d63672f7741b129246a669db09d1c0063071debc0c0" +checksum = "cb7ad7bc932e4968523fa7d9c320ee135ff779de720e9350fee8728838551764" dependencies = [ - "bytes", "libc", - "socket2 0.5.6", + "once_cell", + "socket2 0.5.7", "tracing", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -4642,7 +4716,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", ] [[package]] @@ -4656,9 +4730,9 @@ dependencies = [ [[package]] name = "rayon" -version = "1.7.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" dependencies = [ "either", "rayon-core", @@ -4666,14 +4740,12 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.11.0" +version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" dependencies = [ - "crossbeam-channel", "crossbeam-deque", "crossbeam-utils", - "num_cpus", ] [[package]] @@ -4685,15 +4757,15 @@ dependencies = [ "pem", "ring 0.16.20", "time", - "x509-parser", + "x509-parser 0.15.1", "yasna", ] [[package]] name = "redis" -version = "0.23.3" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f49cdc0bb3f412bf8e7d1bd90fe1d9eb10bc5c399ba90973c14662a27b3f8ba" +checksum = "c580d9cbbe1d1b479e8d67cf9daf6a62c957e6846048408b80b43ac3f6af84cd" dependencies = [ "async-trait", "bytes", @@ -4732,18 +4804,18 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", "redox_syscall 0.2.16", "thiserror", ] [[package]] name = "regex" -version = "1.10.3" +version = "1.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" dependencies = [ - "aho-corasick 1.0.2", + "aho-corasick", "memchr", "regex-automata 0.4.4", "regex-syntax 0.8.2", @@ -4764,7 +4836,7 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b7fa1134405e2ec9353fd416b17f8dacd46c473d7d3fd1cf202706a14eb792a" dependencies = [ - "aho-corasick 1.0.2", + "aho-corasick", "memchr", "regex-syntax 0.8.2", ] @@ -4785,11 +4857,10 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" name = "relay-server-example" version = "0.1.0" dependencies = [ - "async-std", - "async-trait", "clap", "futures", "libp2p", + "tokio", "tracing", "tracing-subscriber", ] @@ -4798,8 +4869,6 @@ dependencies = [ name = "rendezvous-example" version = "0.1.0" dependencies = [ - "async-std", - "async-trait", "futures", "libp2p", "tokio", @@ -4809,21 +4878,23 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.25" +version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eea5a9eb898d3783f17c6407670e3592fd174cb81a10e51d4c37f49450b9946" +checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10" dependencies = [ - "base64 0.21.7", + "base64 0.22.1", "bytes", "encoding_rs", "futures-core", "futures-util", - "h2 0.3.24", - "http 0.2.9", - "http-body 0.4.5", - "hyper 0.14.27", + "h2", + "http 1.1.0", + "http-body", + "http-body-util", + "hyper", "hyper-rustls", "hyper-tls", + "hyper-util", "ipnet", "js-sys", "log", @@ -4832,13 +4903,14 @@ dependencies = [ "once_cell", "percent-encoding", "pin-project-lite", - "rustls", + "rustls 0.22.4", "rustls-pemfile", + "rustls-pki-types", "serde", "serde_json", "serde_urlencoded", - "sync_wrapper", - "system-configuration 0.6.0", + "sync_wrapper 0.1.2", + "system-configuration", "tokio", "tokio-native-tls", "tokio-rustls", @@ -4847,8 +4919,8 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "web-sys", - "webpki-roots", - "winreg", + "webpki-roots 0.26.1", + "winreg 0.52.0", ] [[package]] @@ -4888,16 +4960,17 @@ dependencies = [ [[package]] name = "ring" -version = "0.17.5" +version = "0.17.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb0205304757e5d899b9c2e448b867ffd03ae7f988002e47cd24954391394d0b" +checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" dependencies = [ "cc", - "getrandom 0.2.12", + "cfg-if", + "getrandom 0.2.15", "libc", "spin 0.9.8", "untrusted 0.9.0", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -4911,9 +4984,9 @@ dependencies = [ [[package]] name = "rmp" -version = "0.8.12" +version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f9860a6cc38ed1da53456442089b4dfa35e7cedaa326df63017af88385e6b20" +checksum = "228ed7c16fa39782c3b3468e974aec2795e9089153cd08ee2e9aefb3613334c4" dependencies = [ "byteorder", "num-traits", @@ -4922,9 +4995,9 @@ dependencies = [ [[package]] name = "rmp-serde" -version = "1.1.2" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bffea85eea980d8a74453e5d02a8d93028f3c34725de143085a844ebe953258a" +checksum = "52e599a477cf9840e92f2cde9a7189e67b42c57532749bf90aea6ec10facd4db" dependencies = [ "byteorder", "rmp", @@ -4939,7 +5012,7 @@ checksum = "3677908cadfbecb4cc1da9a56a32524fae4ebdfa7c2ea93886e1b1e846488cb9" dependencies = [ "bytes", "thiserror", - "webrtc-util", + "webrtc-util 0.8.1", ] [[package]] @@ -4968,14 +5041,14 @@ dependencies = [ "rand 0.8.5", "serde", "thiserror", - "webrtc-util", + "webrtc-util 0.8.1", ] [[package]] name = "rust-embed" -version = "8.3.0" +version = "8.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb78f46d0066053d16d4ca7b898e9343bc3530f71c61d5ad84cd404ada068745" +checksum = "19549741604902eb99a7ed0ee177a0663ee1eda51a29f71401f166e47e77806a" dependencies = [ "rust-embed-impl", "rust-embed-utils", @@ -4984,23 +5057,23 @@ dependencies = [ [[package]] name = "rust-embed-impl" -version = "8.3.0" +version = "8.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b91ac2a3c6c0520a3fb3dd89321177c3c692937c4eb21893378219da10c44fc8" +checksum = "cb9f96e283ec64401f30d3df8ee2aaeb2561f34c824381efa24a35f79bf40ee4" dependencies = [ "proc-macro2", "quote", "rust-embed-utils", "shellexpand", - "syn 2.0.52", + "syn 2.0.66", "walkdir", ] [[package]] name = "rust-embed-utils" -version = "8.3.0" +version = "8.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f69089032567ffff4eada41c573fc43ff466c7db7c5688b2e7969584345581" +checksum = "38c74a686185620830701348de757fd36bef4aa9680fd23c49fc539ddcc1af32" dependencies = [ "globset", "sha2 0.10.8", @@ -5019,6 +5092,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc-hash" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" + [[package]] name = "rustc_version" version = "0.4.0" @@ -5066,32 +5145,78 @@ dependencies = [ [[package]] name = "rustls" -version = "0.21.9" +version = "0.21.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "629648aced5775d558af50b2b4c7b02983a04b312126d45eeead26e7caa498b9" +checksum = "7fecbfb7b1444f477b345853b1fce097a2c6fb637b2bfb87e6bc5db0f043fae4" dependencies = [ "log", - "ring 0.17.5", - "rustls-webpki", + "ring 0.17.8", + "rustls-webpki 0.101.7", "sct", ] +[[package]] +name = "rustls" +version = "0.22.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4ef73721ac7bcd79b2b315da7779d8fc09718c6b3d2d1b2d94850eb8c18432" +dependencies = [ + "log", + "ring 0.17.8", + "rustls-pki-types", + "rustls-webpki 0.102.5", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls" +version = "0.23.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4828ea528154ae444e5a642dbb7d5623354030dc9822b83fd9bb79683c7399d0" +dependencies = [ + "once_cell", + "ring 0.17.8", + "rustls-pki-types", + "rustls-webpki 0.102.5", + "subtle", + "zeroize", +] + [[package]] name = "rustls-pemfile" -version = "1.0.3" +version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2" +checksum = "29993a25686778eb88d4189742cd713c9bce943bc54251a33509dc63cbacf73d" dependencies = [ - "base64 0.21.7", + "base64 0.22.1", + "rustls-pki-types", ] +[[package]] +name = "rustls-pki-types" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d" + [[package]] name = "rustls-webpki" version = "0.101.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765" dependencies = [ - "ring 0.17.5", + "ring 0.17.8", + "untrusted 0.9.0", +] + +[[package]] +name = "rustls-webpki" +version = "0.102.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a6fccd794a42c2c105b513a2f62bc3fd8f3ba57a4593677ceb0bd035164d78" +dependencies = [ + "ring 0.17.8", + "rustls-pki-types", "untrusted 0.9.0", ] @@ -5238,29 +5363,29 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.197" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] name = "serde_json" -version = "1.0.114" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" dependencies = [ "indexmap 2.2.1", "itoa", @@ -5280,13 +5405,22 @@ dependencies = [ [[package]] name = "serde_repr" -version = "0.1.18" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b2e6b945e9d3df726b65d6ee24060aff8e3533d431f677a9695db04eff9dfdb" +checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", +] + +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", ] [[package]] @@ -5404,9 +5538,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.13.1" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "smol" @@ -5445,7 +5579,7 @@ dependencies = [ "chacha20poly1305", "curve25519-dalek", "rand_core 0.6.4", - "ring 0.17.5", + "ring 0.17.8", "rustc_version", "sha2 0.10.8", "subtle", @@ -5463,9 +5597,9 @@ dependencies = [ [[package]] name = "socket2" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" dependencies = [ "libc", "windows-sys 0.52.0", @@ -5477,7 +5611,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37468c595637c10857701c990f93a40ce0e357cedb0953d1c26c8d8027f9bb53" dependencies = [ - "base64 0.22.0", + "base64 0.22.1", "bytes", "futures", "httparse", @@ -5539,30 +5673,30 @@ dependencies = [ [[package]] name = "strsim" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" [[package]] name = "strum" -version = "0.25.0" +version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" +checksum = "5d8cec3501a5194c432b2b7976db6b7d10ec95c253208b45f83f7136aa985e29" dependencies = [ "strum_macros", ] [[package]] name = "strum_macros" -version = "0.25.3" +version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23dc1fa9ac9c169a78ba62f0b841814b7abae11bdd047b9c58f893439e309ea0" +checksum = "c6cf59daf282c0a494ba14fd21610a0325f9f90ec9d1231dea26bcb1d696c946" dependencies = [ - "heck", + "heck 0.4.1", "proc-macro2", "quote", "rustversion", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] @@ -5576,12 +5710,31 @@ dependencies = [ "lazy_static", "md-5", "rand 0.8.5", - "ring 0.17.5", + "ring 0.17.8", + "subtle", + "thiserror", + "tokio", + "url", + "webrtc-util 0.8.1", +] + +[[package]] +name = "stun" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28fad383a1cc63ae141e84e48eaef44a1063e9d9e55bcb8f51a99b886486e01b" +dependencies = [ + "base64 0.21.7", + "crc", + "lazy_static", + "md-5", + "rand 0.8.5", + "ring 0.17.8", "subtle", "thiserror", "tokio", "url", - "webrtc-util", + "webrtc-util 0.9.0", ] [[package]] @@ -5612,9 +5765,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.52" +version = "2.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" dependencies = [ "proc-macro2", "quote", @@ -5627,6 +5780,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "sync_wrapper" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384595c11a4e2969895cad5a8c4029115f5ab956a9e5ef4de79d11a426e5f20c" + [[package]] name = "synstructure" version = "0.12.6" @@ -5639,11 +5798,22 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "sysinfo" -version = "0.29.11" +version = "0.30.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd727fc423c2060f6c92d9534cef765c65a6ed3f428a03d7def74a8c4348e666" +checksum = "732ffa00f53e6b2af46208fba5718d9662a421049204e156328b66791ffa15ae" dependencies = [ "cfg-if", "core-foundation-sys", @@ -5651,7 +5821,7 @@ dependencies = [ "ntapi", "once_cell", "rayon", - "winapi", + "windows 0.52.0", ] [[package]] @@ -5662,18 +5832,7 @@ checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" dependencies = [ "bitflags 1.3.2", "core-foundation", - "system-configuration-sys 0.5.0", -] - -[[package]] -name = "system-configuration" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "658bc6ee10a9b4fcf576e9b0819d95ec16f4d2c02d39fd83ac1c8789785c4a42" -dependencies = [ - "bitflags 2.4.1", - "core-foundation", - "system-configuration-sys 0.6.0", + "system-configuration-sys", ] [[package]] @@ -5686,16 +5845,6 @@ dependencies = [ "libc", ] -[[package]] -name = "system-configuration-sys" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" -dependencies = [ - "core-foundation-sys", - "libc", -] - [[package]] name = "tempfile" version = "3.10.1" @@ -5719,14 +5868,14 @@ dependencies = [ [[package]] name = "thirtyfour" -version = "0.32.0-rc.10" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcc8d557d4ac49c0d0bf13722bf7414506ef50b28ac40894400dcd0b8ed25c4d" +checksum = "0641fa1353dd7b7864a7a7782e495433de18a9096bc91b5a2d5838ac209c2fa8" dependencies = [ "async-trait", - "base64 0.21.7", + "base64 0.22.1", "futures", - "http 1.0.0", + "http 1.1.0", "indexmap 2.2.1", "parking_lot", "paste", @@ -5745,34 +5894,33 @@ dependencies = [ [[package]] name = "thirtyfour-macros" -version = "0.1.1" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cae91d1c7c61ec65817f1064954640ee350a50ae6548ff9a1bdd2489d6ffbb0" +checksum = "b72d056365e368fc57a56d0cec9e41b02fb4a3474a61c8735262b1cfebe67425" dependencies = [ - "proc-macro-error", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.66", ] [[package]] name = "thiserror" -version = "1.0.57" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.57" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] @@ -5785,13 +5933,38 @@ dependencies = [ "once_cell", ] +[[package]] +name = "threadpool" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa" +dependencies = [ + "num_cpus", +] + +[[package]] +name = "thrift" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e54bc85fc7faa8bc175c4bab5b92ba8d9a3ce893d0e9f42cc455c8ab16a9e09" +dependencies = [ + "byteorder", + "integer-encoding", + "log", + "ordered-float 2.10.1", + "threadpool", +] + [[package]] name = "time" -version = "0.3.23" +version = "0.3.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59e399c068f43a5d116fedaf73b203fa4f9c519f17e2b34f63221d3792f81446" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ + "deranged", "itoa", + "num-conv", + "powerfmt", "serde", "time-core", "time-macros", @@ -5799,16 +5972,17 @@ dependencies = [ [[package]] name = "time-core" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.10" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96ba15a897f3c86766b757e5ac7221554c6750054d74d5b28844fce5fb36a6c4" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" dependencies = [ + "num-conv", "time-core", ] @@ -5839,9 +6013,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.36.0" +version = "1.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" +checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" dependencies = [ "backtrace", "bytes", @@ -5851,30 +6025,20 @@ dependencies = [ "parking_lot", "pin-project-lite", "signal-hook-registry", - "socket2 0.5.6", + "socket2 0.5.7", "tokio-macros", "windows-sys 0.48.0", ] -[[package]] -name = "tokio-io-timeout" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30b74022ada614a1b4834de765f9bb43877f910cc8ce4be40e89042c9223a8bf" -dependencies = [ - "pin-project-lite", - "tokio", -] - [[package]] name = "tokio-macros" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] @@ -5889,19 +6053,20 @@ dependencies = [ [[package]] name = "tokio-rustls" -version = "0.24.1" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" +checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f" dependencies = [ - "rustls", + "rustls 0.22.4", + "rustls-pki-types", "tokio", ] [[package]] name = "tokio-stream" -version = "0.1.14" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842" +checksum = "4f4e6ce100d0eb49a2734f8c0812bcd324cf357d21810932c5df6b96ef2b86f1" dependencies = [ "futures-core", "pin-project-lite", @@ -5910,9 +6075,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.10" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" +checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" dependencies = [ "bytes", "futures-core", @@ -5920,29 +6085,64 @@ dependencies = [ "futures-sink", "pin-project-lite", "tokio", - "tracing", +] + +[[package]] +name = "toml" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af06656561d28735e9c1cd63dfd57132c8155426aa6af24f36a00a351f88c48e" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18769cd1cec395d70860ceb4d932812a0b4d06b1a4bb336745a4d21b9496e992" +dependencies = [ + "indexmap 2.2.1", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", ] [[package]] name = "tonic" -version = "0.9.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3082666a3a6433f7f511c7192923fa1fe07c69332d3c6a2e6bb040b569199d5a" +checksum = "877c5b330756d856ffcc4553ab34a5684481ade925ecc54bcd1bf02b1d0d4d52" dependencies = [ + "async-stream", "async-trait", - "axum 0.6.20", - "base64 0.21.7", + "axum", + "base64 0.22.1", "bytes", - "futures-core", - "futures-util", - "h2 0.3.24", - "http 0.2.9", - "http-body 0.4.5", - "hyper 0.14.27", + "h2", + "http 1.1.0", + "http-body", + "http-body-util", + "hyper", "hyper-timeout", + "hyper-util", "percent-encoding", "pin-project", "prost", + "socket2 0.5.7", "tokio", "tokio-stream", "tower", @@ -5980,8 +6180,8 @@ dependencies = [ "bitflags 2.4.1", "bytes", "futures-util", - "http 1.0.0", - "http-body 1.0.0", + "http 1.1.0", + "http-body", "http-body-util", "http-range-header", "httpdate", @@ -6028,7 +6228,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] @@ -6043,40 +6243,49 @@ dependencies = [ [[package]] name = "tracing-log" -version = "0.1.3" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" dependencies = [ - "lazy_static", "log", + "once_cell", "tracing-core", ] [[package]] -name = "tracing-log" -version = "0.2.0" +name = "tracing-opentelemetry" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +checksum = "c67ac25c5407e7b961fafc6f7e9aa5958fd297aada2d20fa2ae1737357e55596" dependencies = [ - "log", + "js-sys", "once_cell", + "opentelemetry 0.21.0", + "opentelemetry_sdk 0.21.2", + "smallvec", + "tracing", "tracing-core", + "tracing-log", + "tracing-subscriber", + "web-time 0.2.4", ] [[package]] name = "tracing-opentelemetry" -version = "0.21.0" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75327c6b667828ddc28f5e3f169036cb793c3f588d83bf0f262a7f062ffed3c8" +checksum = "5eabc56d23707ad55ba2a0750fc24767125d5a0f51993ba41ad2c441cc7b8dea" dependencies = [ + "js-sys", "once_cell", - "opentelemetry", - "opentelemetry_sdk", + "opentelemetry 0.25.0", + "opentelemetry_sdk 0.25.0", "smallvec", "tracing", "tracing-core", - "tracing-log 0.1.3", + "tracing-log", "tracing-subscriber", + "web-time 1.1.0", ] [[package]] @@ -6094,7 +6303,7 @@ dependencies = [ "thread_local", "tracing", "tracing-core", - "tracing-log 0.2.0", + "tracing-log", ] [[package]] @@ -6116,17 +6325,16 @@ checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" [[package]] name = "trybuild" -version = "1.0.89" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a9d3ba662913483d6722303f619e75ea10b7855b0f8e0d72799cf8621bb488f" +checksum = "33a5f13f11071020bb12de7a16b925d2d58636175c20c11dc5f96cb64bb6c9b3" dependencies = [ - "basic-toml", "glob", - "once_cell", "serde", "serde_derive", "serde_json", "termcolor", + "toml", ] [[package]] @@ -6142,10 +6350,10 @@ dependencies = [ "md-5", "rand 0.8.5", "ring 0.16.20", - "stun", + "stun 0.5.1", "thiserror", "tokio", - "webrtc-util", + "webrtc-util 0.8.1", ] [[package]] @@ -6252,9 +6460,9 @@ dependencies = [ [[package]] name = "url" -version = "2.5.0" +version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" dependencies = [ "form_urlencoded", "idna 0.5.0", @@ -6279,7 +6487,7 @@ version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", ] [[package]] @@ -6360,34 +6568,35 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" dependencies = [ "cfg-if", + "once_cell", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.42" +version = "0.4.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +checksum = "61e9300f63a621e96ed275155c108eb6f843b6a26d053f122ab69724559dc8ed" dependencies = [ "cfg-if", "js-sys", @@ -6397,9 +6606,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -6407,31 +6616,32 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" [[package]] name = "wasm-bindgen-test" -version = "0.3.41" +version = "0.3.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "143ddeb4f833e2ed0d252e618986e18bfc7b0e52f2d28d77d05b2f045dd8eb61" +checksum = "68497a05fb21143a08a7d24fc81763384a3072ee43c44e86aad1744d6adef9d9" dependencies = [ "console_error_panic_hook", "js-sys", + "minicov", "scoped-tls", "wasm-bindgen", "wasm-bindgen-futures", @@ -6440,13 +6650,13 @@ dependencies = [ [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.41" +version = "0.3.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5211b7550606857312bba1d978a8ec75692eae187becc5e680444fffc5e6f89" +checksum = "4b8220be1fa9e4c889b30fd207d4906657e7e90b12e0e6b0c8b8d8709f5de021" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] @@ -6462,9 +6672,29 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.68" +version = "0.3.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96565907687f7aceb35bc5fc03770a8a0471d82e479f25832f54a0e3f4b28446" +checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa30049b1c872b72c89866d458eae9f20380ab280ffd1b1e18df2d3e2d98cfe0" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" dependencies = [ "js-sys", "wasm-bindgen", @@ -6476,6 +6706,15 @@ version = "0.25.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14247bb57be4f377dfb94c72830b8ce8fc6beac03cf4bf7b9732eadd414123fc" +[[package]] +name = "webpki-roots" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3de34ae270483955a94f4b21bdaaeb83d508bb84a01435f393818edb0012009" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "webrtc" version = "0.9.0" @@ -6497,13 +6736,13 @@ dependencies = [ "ring 0.16.20", "rtcp", "rtp", - "rustls", + "rustls 0.21.11", "sdp", "serde", "serde_json", "sha2 0.10.8", "smol_str", - "stun", + "stun 0.5.1", "thiserror", "time", "tokio", @@ -6517,7 +6756,7 @@ dependencies = [ "webrtc-media", "webrtc-sctp", "webrtc-srtp", - "webrtc-util", + "webrtc-util 0.8.1", ] [[package]] @@ -6531,7 +6770,7 @@ dependencies = [ "thiserror", "tokio", "webrtc-sctp", - "webrtc-util", + "webrtc-util 0.8.1", ] [[package]] @@ -6547,7 +6786,7 @@ dependencies = [ "byteorder", "cbc", "ccm", - "der-parser", + "der-parser 8.2.0", "hkdf", "hmac 0.12.1", "log", @@ -6558,7 +6797,7 @@ dependencies = [ "rand_core 0.6.4", "rcgen", "ring 0.16.20", - "rustls", + "rustls 0.21.11", "sec1", "serde", "sha1", @@ -6566,9 +6805,9 @@ dependencies = [ "subtle", "thiserror", "tokio", - "webrtc-util", + "webrtc-util 0.8.1", "x25519-dalek", - "x509-parser", + "x509-parser 0.15.1", ] [[package]] @@ -6584,7 +6823,7 @@ dependencies = [ "rand 0.8.5", "serde", "serde_json", - "stun", + "stun 0.5.1", "thiserror", "tokio", "turn", @@ -6592,7 +6831,7 @@ dependencies = [ "uuid", "waitgroup", "webrtc-mdns", - "webrtc-util", + "webrtc-util 0.8.1", ] [[package]] @@ -6602,10 +6841,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62bebbd40e7f8b630a0f1a74783dbfff1edfc0ccaae891c4689891156a8c4d8c" dependencies = [ "log", - "socket2 0.5.6", + "socket2 0.5.7", "thiserror", "tokio", - "webrtc-util", + "webrtc-util 0.8.1", ] [[package]] @@ -6635,7 +6874,7 @@ dependencies = [ "rand 0.8.5", "thiserror", "tokio", - "webrtc-util", + "webrtc-util 0.8.1", ] [[package]] @@ -6658,7 +6897,7 @@ dependencies = [ "subtle", "thiserror", "tokio", - "webrtc-util", + "webrtc-util 0.8.1", ] [[package]] @@ -6681,12 +6920,33 @@ dependencies = [ "winapi", ] +[[package]] +name = "webrtc-util" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc8d9bc631768958ed97b8d68b5d301e63054ae90b09083d43e2fefb939fd77e" +dependencies = [ + "async-trait", + "bitflags 1.3.2", + "bytes", + "ipnet", + "lazy_static", + "libc", + "log", + "nix 0.26.4", + "portable-atomic", + "rand 0.8.5", + "thiserror", + "tokio", + "winapi", +] + [[package]] name = "webtransport-tests" version = "0.1.0" dependencies = [ "futures", - "getrandom 0.2.12", + "getrandom 0.2.15", "libp2p-core", "libp2p-identity", "libp2p-noise", @@ -6742,10 +7002,20 @@ version = "0.51.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" dependencies = [ - "windows-core", + "windows-core 0.51.1", "windows-targets 0.48.5", ] +[[package]] +name = "windows" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" +dependencies = [ + "windows-core 0.52.0", + "windows-targets 0.52.0", +] + [[package]] name = "windows-core" version = "0.51.1" @@ -6755,6 +7025,15 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.0", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -6887,6 +7166,15 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +[[package]] +name = "winnow" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.50.0" @@ -6897,6 +7185,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "winreg" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + [[package]] name = "x25519-dalek" version = "2.0.1" @@ -6915,18 +7213,35 @@ version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7069fba5b66b9193bd2c5d3d4ff12b839118f6bcbef5328efafafb5395cf63da" dependencies = [ - "asn1-rs", + "asn1-rs 0.5.2", "data-encoding", - "der-parser", + "der-parser 8.2.0", "lazy_static", "nom", - "oid-registry", + "oid-registry 0.6.1", "ring 0.16.20", "rusticata-macros", "thiserror", "time", ] +[[package]] +name = "x509-parser" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcbc162f30700d6f3f82a24bf7cc62ffe7caea42c0b2cba8bf7f3ae50cf51f69" +dependencies = [ + "asn1-rs 0.6.1", + "data-encoding", + "der-parser 9.0.0", + "lazy_static", + "nom", + "oid-registry 0.7.0", + "rusticata-macros", + "thiserror", + "time", +] + [[package]] name = "xml-rs" version = "0.8.17" @@ -6959,18 +7274,18 @@ dependencies = [ [[package]] name = "yamux" -version = "0.13.1" +version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1d0148b89300047e72994bee99ecdabd15a9166a7b70c8b8c37c314dcc9002" +checksum = "a31b5e376a8b012bee9c423acdbb835fc34d45001cfa3106236a624e4b738028" dependencies = [ "futures", - "instant", "log", "nohash-hasher", "parking_lot", "pin-project", "rand 0.8.5", "static_assertions", + "web-time 1.1.0", ] [[package]] @@ -6999,14 +7314,14 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] [[package]] name = "zeroize" -version = "1.7.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" dependencies = [ "zeroize_derive", ] @@ -7019,5 +7334,5 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.66", ] diff --git a/Cargo.toml b/Cargo.toml index b6508a3d3f2..6763624a76b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "core", "examples/autonat", + "examples/autonatv2", "examples/browser-webrtc", "examples/chat", "examples/dcutr", @@ -30,6 +31,7 @@ members = [ "misc/rw-stream-sink", "misc/server", "misc/webrtc-utils", + "misc/logging", "muxers/mplex", "muxers/test-harness", "muxers/yamux", @@ -68,53 +70,53 @@ members = [ resolver = "2" [workspace.package] -rust-version = "1.73.0" +rust-version = "1.75.0" [workspace.dependencies] asynchronous-codec = { version = "0.7.0" } -futures-bounded = { version = "0.2.3" } -libp2p = { version = "0.53.2", path = "libp2p" } -libp2p-allow-block-list = { version = "0.3.0", path = "misc/allow-block-list" } -libp2p-autonat = { version = "0.12.0", path = "protocols/autonat" } -libp2p-connection-limits = { version = "0.3.1", path = "misc/connection-limits" } -libp2p-core = { version = "0.41.2", path = "core" } -libp2p-dcutr = { version = "0.11.0", path = "protocols/dcutr" } -libp2p-dns = { version = "0.41.1", path = "transports/dns" } -libp2p-floodsub = { version = "0.44.0", path = "protocols/floodsub" } -libp2p-gossipsub = { version = "0.46.1", path = "protocols/gossipsub" } -libp2p-identify = { version = "0.44.2", path = "protocols/identify" } -libp2p-identity = { version = "0.2.8" } -libp2p-kad = { version = "0.45.4", path = "protocols/kad" } -libp2p-mdns = { version = "0.45.1", path = "protocols/mdns" } -libp2p-memory-connection-limits = { version = "0.2.0", path = "misc/memory-connection-limits" } -libp2p-metrics = { version = "0.14.1", path = "misc/metrics" } -libp2p-mplex = { version = "0.41.0", path = "muxers/mplex" } -libp2p-muxer-test-harness = { path = "muxers/test-harness" } -libp2p-noise = { version = "0.44.0", path = "transports/noise" } -libp2p-perf = { version = "0.3.0", path = "protocols/perf" } -libp2p-ping = { version = "0.44.0", path = "protocols/ping" } -libp2p-plaintext = { version = "0.41.0", path = "transports/plaintext" } -libp2p-pnet = { version = "0.24.0", path = "transports/pnet" } -libp2p-quic = { version = "0.10.2", path = "transports/quic" } -libp2p-relay = { version = "0.17.1", path = "protocols/relay" } -libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" } -libp2p-request-response = { version = "0.26.2", path = "protocols/request-response" } -libp2p-server = { version = "0.12.7", path = "misc/server" } -libp2p-stream = { version = "0.1.0-alpha.1", path = "protocols/stream" } -libp2p-swarm = { version = "0.44.2", path = "swarm" } -libp2p-swarm-derive = { version = "=0.34.3", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required. -libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" } -libp2p-tcp = { version = "0.41.0", path = "transports/tcp" } -libp2p-tls = { version = "0.3.0", path = "transports/tls" } -libp2p-uds = { version = "0.40.0", path = "transports/uds" } -libp2p-upnp = { version = "0.2.1", path = "protocols/upnp" } -libp2p-webrtc = { version = "0.7.1-alpha", path = "transports/webrtc" } -libp2p-webrtc-utils = { version = "0.2.0", path = "misc/webrtc-utils" } -libp2p-webrtc-websys = { version = "0.3.0-alpha", path = "transports/webrtc-websys" } -libp2p-websocket = { version = "0.43.0", path = "transports/websocket" } -libp2p-websocket-websys = { version = "0.3.1", path = "transports/websocket-websys" } -libp2p-webtransport-websys = { version = "0.2.0", path = "transports/webtransport-websys" } -libp2p-yamux = { version = "0.45.1", path = "muxers/yamux" } +futures-bounded = { version = "0.2.4" } +futures-rustls = { version = "0.26.0", default-features = false } +libp2p = { version = "0.54.1", path = "libp2p" } +libp2p-allow-block-list = { version = "0.4.1", path = "misc/allow-block-list" } +libp2p-autonat = { version = "0.13.1", path = "protocols/autonat" } +libp2p-connection-limits = { version = "0.4.0", path = "misc/connection-limits" } +libp2p-core = { version = "0.42.0", path = "core" } +libp2p-dcutr = { version = "0.12.0", path = "protocols/dcutr" } +libp2p-dns = { version = "0.42.0", path = "transports/dns" } +libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" } +libp2p-gossipsub = { version = "0.47.1", path = "protocols/gossipsub" } +libp2p-identify = { version = "0.45.1", path = "protocols/identify" } +libp2p-identity = { version = "0.2.9" } +libp2p-kad = { version = "0.47.0", path = "protocols/kad" } +libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" } +libp2p-memory-connection-limits = { version = "0.3.0", path = "misc/memory-connection-limits" } +libp2p-metrics = { version = "0.15.0", path = "misc/metrics" } +libp2p-mplex = { version = "0.42.0", path = "muxers/mplex" } +libp2p-noise = { version = "0.45.0", path = "transports/noise" } +libp2p-perf = { version = "0.4.0", path = "protocols/perf" } +libp2p-ping = { version = "0.45.0", path = "protocols/ping" } +libp2p-plaintext = { version = "0.42.0", path = "transports/plaintext" } +libp2p-pnet = { version = "0.25.0", path = "transports/pnet" } +libp2p-quic = { version = "0.11.1", path = "transports/quic" } +libp2p-relay = { version = "0.18.0", path = "protocols/relay" } +libp2p-rendezvous = { version = "0.15.0", path = "protocols/rendezvous" } +libp2p-request-response = { version = "0.27.0", path = "protocols/request-response" } +libp2p-server = { version = "0.12.8", path = "misc/server" } +libp2p-stream = { version = "0.2.0-alpha", path = "protocols/stream" } +libp2p-swarm = { version = "0.45.2", path = "swarm" } +libp2p-swarm-derive = { version = "=0.35.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required. +libp2p-swarm-test = { version = "0.4.1", path = "swarm-test" } +libp2p-tcp = { version = "0.42.0", path = "transports/tcp" } +libp2p-tls = { version = "0.5.0", path = "transports/tls" } +libp2p-uds = { version = "0.41.0", path = "transports/uds" } +libp2p-upnp = { version = "0.3.1", path = "protocols/upnp" } +libp2p-webrtc = { version = "0.8.0-alpha", path = "transports/webrtc" } +libp2p-webrtc-utils = { version = "0.3.0", path = "misc/webrtc-utils" } +libp2p-webrtc-websys = { version = "0.4.0-alpha.2", path = "transports/webrtc-websys" } +libp2p-websocket = { version = "0.44.0", path = "transports/websocket" } +libp2p-websocket-websys = { version = "0.4.0", path = "transports/websocket-websys" } +libp2p-webtransport-websys = { version = "0.4.0", path = "transports/webtransport-websys" } +libp2p-yamux = { version = "0.46.0", path = "muxers/yamux" } multiaddr = "0.18.1" multihash = "0.19.1" multistream-select = { version = "0.13.0", path = "misc/multistream-select" } @@ -123,6 +125,13 @@ quick-protobuf-codec = { version = "0.3.1", path = "misc/quick-protobuf-codec" } quickcheck = { package = "quickcheck-ext", path = "misc/quickcheck-ext" } rw-stream-sink = { version = "0.4.0", path = "misc/rw-stream-sink" } unsigned-varint = { version = "0.8.0" } +tokio = { version = "1.38", default-features = false } +tracing = "0.1.37" +tracing-subscriber = "0.3" +futures = "0.3.30" +web-time = "1.1.0" +ring = "0.17.8" +rcgen = "0.11.3" [patch.crates-io] @@ -136,11 +145,15 @@ libp2p-identity = { path = "identity" } [workspace.lints] rust.unreachable_pub = "warn" clippy.used_underscore_binding = "warn" -clippy.pedantic = "allow" +clippy.pedantic = { level = "allow", priority = -1 } clippy.type_complexity = "allow" clippy.unnecessary_wraps = "warn" clippy.manual_let_else = "warn" clippy.dbg_macro = "warn" [workspace.metadata.release] -pre-release-hook = ["/bin/sh", '-c', '/bin/sh $WORKSPACE_ROOT/scripts/add-changelog-header.sh'] # Nested use of shell to expand variables. +pre-release-hook = [ + "/bin/sh", + '-c', + '/bin/sh $WORKSPACE_ROOT/scripts/add-changelog-header.sh', +] # Nested use of shell to expand variables. diff --git a/FUNDING.json b/FUNDING.json new file mode 100644 index 00000000000..cce3fb3fe4c --- /dev/null +++ b/FUNDING.json @@ -0,0 +1,5 @@ +{ + "opRetro": { + "projectId": "0xdf1bb03d08808e2d789f5eac8462bdc560f1bb5b0877f0cf8c66ab53a0bc2f5c" + } +} diff --git a/README.md b/README.md index 4d72626e413..d818c6ba7b4 100644 --- a/README.md +++ b/README.md @@ -78,8 +78,8 @@ Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md). (In alphabetical order.) +- Guillaume Michel ([@guillaumemichel](https://github.com/guillaumemichel)) - João Oliveira ([@jxs](https://github.com/jxs)) -- Thomas Eizinger ([@thomaseizinger](https://github.com/thomaseizinger)) ## Notable users @@ -102,3 +102,4 @@ Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md). - [Substrate](https://github.com/paritytech/substrate) - Framework for blockchain innovation, used by [Polkadot](https://www.parity.io/technologies/polkadot/). - [Taple](https://github.com/opencanarias/taple-core) - Sustainable DLT for asset and process traceability by [OpenCanarias](https://www.opencanarias.com/en/). +- [Ceylon](https://github.com/ceylonai/ceylon) - A Multi-Agent System (MAS) Development Framework. diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index a7cd7fd46b4..5ed4b4e181d 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -1,3 +1,18 @@ +## 0.42.0 + +- Update `Transport::dial` function signature with a `DialOpts` param and remove `Transport::dial_as_listener`: + - `DialOpts` struct contains `PortUse` and `Endpoint`, + - `PortUse` allows controling port allocation of new connections (defaults to `PortUse::Reuse`) - + - Add `port_use` field to `ConnectedPoint` + - Set `endpoint` field in `DialOpts` to `Endpoint::Listener` to dial as a listener +- Remove `Transport::address_translation` and relocate functionality to `libp2p_swarm` + +See [PR 4568]. + +## 0.41.3 +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + ## 0.41.2 - Implement `std::fmt::Display` on `ListenerId`. @@ -304,7 +319,7 @@ - Add `From<&PublicKey> for PeerId` (see [PR 2145]). -- Remove `TInEvent` and `TOutEvent` trait paramters on most public types. +- Remove `TInEvent` and `TOutEvent` trait parameters on most public types. `TInEvent` and `TOutEvent` are implied through `THandler` and thus superflucious. Both are removed in favor of a derivation through `THandler` (see [PR 2183]). diff --git a/core/Cargo.toml b/core/Cargo.toml index 054627bed38..8a083276e7f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-core" edition = "2021" rust-version = { workspace = true } description = "Core traits and structs of libp2p" -version = "0.41.2" +version = "0.42.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -11,32 +11,32 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] -either = "1.9" +either = "1.11" fnv = "1.0" -futures = { version = "0.3.30", features = ["executor", "thread-pool"] } +futures = { workspace = true, features = ["executor", "thread-pool"] } futures-timer = "3" -instant = "0.1.12" +web-time = { workspace = true } libp2p-identity = { workspace = true, features = ["peerid", "ed25519"] } multiaddr = { workspace = true } multihash = { workspace = true } multistream-select = { workspace = true } once_cell = "1.19.0" -parking_lot = "0.12.0" -pin-project = "1.1.4" +parking_lot = "0.12.3" +pin-project = "1.1.5" quick-protobuf = "0.8" rand = "0.8" rw-stream-sink = { workspace = true } serde = { version = "1", optional = true, features = ["derive"] } -smallvec = "1.13.1" +smallvec = "1.13.2" thiserror = "1.0" -tracing = "0.1.37" +tracing = { workspace = true } unsigned-varint = { workspace = true } void = "1" [dev-dependencies] async-std = { version = "1.6.2", features = ["attributes"] } -libp2p-mplex = { path = "../muxers/mplex" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing. -libp2p-noise = { path = "../transports/noise" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing. +libp2p-mplex = { path = "../muxers/mplex" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing. +libp2p-noise = { path = "../transports/noise" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing. multihash = { workspace = true, features = ["arb"] } quickcheck = { workspace = true } libp2p-identity = { workspace = true, features = ["ed25519", "rand"] } @@ -48,8 +48,6 @@ serde = ["multihash/serde-codec", "dep:serde", "libp2p-identity/serde"] # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/core/src/connection.rs b/core/src/connection.rs index ff613c5cce0..bb6639842c9 100644 --- a/core/src/connection.rs +++ b/core/src/connection.rs @@ -18,7 +18,10 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::multiaddr::{Multiaddr, Protocol}; +use crate::{ + multiaddr::{Multiaddr, Protocol}, + transport::PortUse, +}; /// The endpoint roles associated with a peer-to-peer communication channel. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] @@ -80,6 +83,13 @@ pub enum ConnectedPoint { /// connection as a dialer and one peer dial the other and upgrade the /// connection _as a listener_ overriding its role. role_override: Endpoint, + /// Whether the port for the outgoing connection was reused from a listener + /// or a new port was allocated. This is useful for address translation. + /// + /// The port use is implemented on a best-effort basis. It is not guaranteed + /// that [`PortUse::Reuse`] actually reused a port. A good example is the case + /// where there is no listener available to reuse a port from. + port_use: PortUse, }, /// We received the node. Listener { @@ -133,6 +143,7 @@ impl ConnectedPoint { ConnectedPoint::Dialer { address, role_override: _, + port_use: _, } => address, ConnectedPoint::Listener { local_addr, .. } => local_addr, } diff --git a/core/src/either.rs b/core/src/either.rs index 3f79b2b37a9..2593174290c 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -19,6 +19,7 @@ // DEALINGS IN THE SOFTWARE. use crate::muxing::StreamMuxerEvent; +use crate::transport::DialOpts; use crate::{ muxing::StreamMuxer, transport::{ListenerId, Transport, TransportError, TransportEvent}, @@ -172,48 +173,23 @@ where } } - fn dial(&mut self, addr: Multiaddr) -> Result> { - use TransportError::*; - match self { - Either::Left(a) => match a.dial(addr) { - Ok(connec) => Ok(EitherFuture::First(connec)), - Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), - Err(Other(err)) => Err(Other(Either::Left(err))), - }, - Either::Right(b) => match b.dial(addr) { - Ok(connec) => Ok(EitherFuture::Second(connec)), - Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), - Err(Other(err)) => Err(Other(Either::Right(err))), - }, - } - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, - ) -> Result> - where - Self: Sized, - { + opts: DialOpts, + ) -> Result> { use TransportError::*; match self { - Either::Left(a) => match a.dial_as_listener(addr) { + Either::Left(a) => match a.dial(addr, opts) { Ok(connec) => Ok(EitherFuture::First(connec)), Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), Err(Other(err)) => Err(Other(Either::Left(err))), }, - Either::Right(b) => match b.dial_as_listener(addr) { + Either::Right(b) => match b.dial(addr, opts) { Ok(connec) => Ok(EitherFuture::Second(connec)), Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), Err(Other(err)) => Err(Other(Either::Right(err))), }, } } - - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - match self { - Either::Left(a) => a.address_translation(server, observed), - Either::Right(b) => b.address_translation(server, observed), - } - } } diff --git a/core/src/lib.rs b/core/src/lib.rs index abb83481d6c..a42f56773df 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -45,8 +45,6 @@ mod proto { pub use multiaddr; pub type Negotiated = multistream_select::Negotiated; -mod translation; - pub mod connection; pub mod either; pub mod muxing; @@ -56,12 +54,12 @@ pub mod transport; pub mod upgrade; pub use connection::{ConnectedPoint, Endpoint}; +pub use libp2p_identity::PeerId; pub use multiaddr::Multiaddr; pub use multihash; pub use muxing::StreamMuxer; pub use peer_record::PeerRecord; pub use signed_envelope::SignedEnvelope; -pub use translation::address_translation; pub use transport::Transport; pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; diff --git a/core/src/peer_record.rs b/core/src/peer_record.rs index 2fd6a39ef2b..ac488338cc6 100644 --- a/core/src/peer_record.rs +++ b/core/src/peer_record.rs @@ -1,11 +1,10 @@ use crate::signed_envelope::SignedEnvelope; use crate::{proto, signed_envelope, DecodeError, Multiaddr}; -use instant::SystemTime; use libp2p_identity::Keypair; use libp2p_identity::PeerId; use libp2p_identity::SigningError; use quick_protobuf::{BytesReader, Writer}; -use std::convert::TryInto; +use web_time::SystemTime; const PAYLOAD_TYPE: &str = "/libp2p/routing-state-record"; const DOMAIN_SEP: &str = "libp2p-routing-state"; diff --git a/core/src/transport.rs b/core/src/transport.rs index 22e7a0532fa..28ce2dbf650 100644 --- a/core/src/transport.rs +++ b/core/src/transport.rs @@ -48,7 +48,7 @@ pub mod upgrade; mod boxed; mod optional; -use crate::ConnectedPoint; +use crate::{ConnectedPoint, Endpoint}; pub use self::boxed::Boxed; pub use self::choice::OrTransport; @@ -58,6 +58,30 @@ pub use self::upgrade::Upgrade; static NEXT_LISTENER_ID: AtomicUsize = AtomicUsize::new(1); +/// The port use policy for a new connection. +#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash)] +pub enum PortUse { + /// Always allocate a new port for the dial. + New, + /// Best effor reusing of an existing port. + /// + /// If there is no listener present that can be used to dial, a new port is allocated. + #[default] + Reuse, +} + +/// Options to customize the behaviour during dialing. +#[derive(Debug, Copy, Clone)] +pub struct DialOpts { + /// The endpoint establishing a new connection. + /// + /// When attempting a hole-punch, both parties simultaneously "dial" each other but one party has to be the "listener" on the final connection. + /// This option specifies the role of this node in the final connection. + pub role: Endpoint, + /// The port use policy for a new connection. + pub port_use: PortUse, +} + /// A transport provides connection-oriented communication between two peers /// through ordered streams of data (i.e. connections). /// @@ -129,16 +153,10 @@ pub trait Transport { /// /// If [`TransportError::MultiaddrNotSupported`] is returned, it may be desirable to /// try an alternative [`Transport`], if available. - fn dial(&mut self, addr: Multiaddr) -> Result>; - - /// As [`Transport::dial`] but has the local node act as a listener on the outgoing connection. - /// - /// This option is needed for NAT and firewall hole punching. - /// - /// See [`ConnectedPoint::Dialer`] for related option. - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + opts: DialOpts, ) -> Result>; /// Poll for [`TransportEvent`]s. @@ -157,24 +175,6 @@ pub trait Transport { cx: &mut Context<'_>, ) -> Poll>; - /// Performs a transport-specific mapping of an address `observed` by a remote onto a - /// local `listen` address to yield an address for the local node that may be reachable - /// for other peers. - /// - /// This is relevant for transports where Network Address Translation (NAT) can occur - /// so that e.g. the peer is observed at a different IP than the IP of the local - /// listening address. See also [`address_translation`][crate::address_translation]. - /// - /// Within [`libp2p::Swarm`]() this is - /// used when extending the listening addresses of the local peer with external addresses - /// observed by remote peers. - /// On transports where this is not relevant (i.e. no NATs are present) `None` should be - /// returned for the sake of de-duplication. - /// - /// Note: if the listen or observed address is not a valid address of this transport, - /// `None` should be returned as well. - fn address_translation(&self, listen: &Multiaddr, observed: &Multiaddr) -> Option; - /// Boxes the transport, including custom transport errors. fn boxed(self) -> boxed::Boxed where @@ -256,7 +256,7 @@ impl ListenerId { } } -impl std::fmt::Display for ListenerId { +impl fmt::Display for ListenerId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } @@ -311,7 +311,7 @@ pub enum TransportEvent { impl TransportEvent { /// In case this [`TransportEvent`] is an upgrade, apply the given function - /// to the upgrade and produce another transport event based the the function's result. + /// to the upgrade and produce another transport event based the function's result. pub fn map_upgrade(self, map: impl FnOnce(TUpgr) -> U) -> TransportEvent { match self { TransportEvent::Incoming { @@ -530,7 +530,7 @@ pub enum TransportError { } impl TransportError { - /// Applies a function to the the error in [`TransportError::Other`]. + /// Applies a function to the error in [`TransportError::Other`]. pub fn map(self, map: impl FnOnce(TErr) -> TNewErr) -> TransportError { match self { TransportError::MultiaddrNotSupported(addr) => { diff --git a/core/src/transport/and_then.rs b/core/src/transport/and_then.rs index 6e0c7e32067..e85703f77fb 100644 --- a/core/src/transport/and_then.rs +++ b/core/src/transport/and_then.rs @@ -19,8 +19,8 @@ // DEALINGS IN THE SOFTWARE. use crate::{ - connection::{ConnectedPoint, Endpoint}, - transport::{ListenerId, Transport, TransportError, TransportEvent}, + connection::ConnectedPoint, + transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent}, }; use either::Either; use futures::prelude::*; @@ -68,32 +68,14 @@ where self.transport.remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { - let dialed_fut = self - .transport - .dial(addr.clone()) - .map_err(|err| err.map(Either::Left))?; - let future = AndThenFuture { - inner: Either::Left(Box::pin(dialed_fut)), - args: Some(( - self.fun.clone(), - ConnectedPoint::Dialer { - address: addr, - role_override: Endpoint::Dialer, - }, - )), - _marker: PhantomPinned, - }; - Ok(future) - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + opts: DialOpts, ) -> Result> { let dialed_fut = self .transport - .dial_as_listener(addr.clone()) + .dial(addr.clone(), opts) .map_err(|err| err.map(Either::Left))?; let future = AndThenFuture { inner: Either::Left(Box::pin(dialed_fut)), @@ -101,7 +83,8 @@ where self.fun.clone(), ConnectedPoint::Dialer { address: addr, - role_override: Endpoint::Listener, + role_override: opts.role, + port_use: opts.port_use, }, )), _marker: PhantomPinned, @@ -109,10 +92,6 @@ where Ok(future) } - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.transport.address_translation(server, observed) - } - fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/core/src/transport/boxed.rs b/core/src/transport/boxed.rs index 1cede676c8e..596ab262221 100644 --- a/core/src/transport/boxed.rs +++ b/core/src/transport/boxed.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::transport::{ListenerId, Transport, TransportError, TransportEvent}; +use crate::transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent}; use futures::{prelude::*, stream::FusedStream}; use multiaddr::Multiaddr; use std::{ @@ -58,9 +58,11 @@ trait Abstract { addr: Multiaddr, ) -> Result<(), TransportError>; fn remove_listener(&mut self, id: ListenerId) -> bool; - fn dial(&mut self, addr: Multiaddr) -> Result, TransportError>; - fn dial_as_listener(&mut self, addr: Multiaddr) -> Result, TransportError>; - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option; + fn dial( + &mut self, + addr: Multiaddr, + opts: DialOpts, + ) -> Result, TransportError>; fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -86,24 +88,17 @@ where Transport::remove_listener(self, id) } - fn dial(&mut self, addr: Multiaddr) -> Result, TransportError> { - let fut = Transport::dial(self, addr) - .map(|r| r.map_err(box_err)) - .map_err(|e| e.map(box_err))?; - Ok(Box::pin(fut) as Dial<_>) - } - - fn dial_as_listener(&mut self, addr: Multiaddr) -> Result, TransportError> { - let fut = Transport::dial_as_listener(self, addr) + fn dial( + &mut self, + addr: Multiaddr, + opts: DialOpts, + ) -> Result, TransportError> { + let fut = Transport::dial(self, addr, opts) .map(|r| r.map_err(box_err)) .map_err(|e| e.map(box_err))?; Ok(Box::pin(fut) as Dial<_>) } - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - Transport::address_translation(self, server, observed) - } - fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -143,19 +138,12 @@ impl Transport for Boxed { self.inner.remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { - self.inner.dial(addr) - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + opts: DialOpts, ) -> Result> { - self.inner.dial_as_listener(addr) - } - - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.inner.address_translation(server, observed) + self.inner.dial(addr, opts) } fn poll( diff --git a/core/src/transport/choice.rs b/core/src/transport/choice.rs index aa3acfc3231..4339f6bba71 100644 --- a/core/src/transport/choice.rs +++ b/core/src/transport/choice.rs @@ -19,7 +19,7 @@ // DEALINGS IN THE SOFTWARE. use crate::either::EitherFuture; -use crate::transport::{ListenerId, Transport, TransportError, TransportEvent}; +use crate::transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent}; use either::Either; use futures::future; use multiaddr::Multiaddr; @@ -92,19 +92,23 @@ where self.0.remove_listener(id) || self.1.remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + opts: DialOpts, + ) -> Result> { tracing::trace!( address=%addr, - "Attempting to dial address using {}", + "Attempting to dial using {}", std::any::type_name::() ); - let addr = match self.0.dial(addr) { + let addr = match self.0.dial(addr, opts) { Ok(connec) => return Ok(EitherFuture::First(connec)), Err(TransportError::MultiaddrNotSupported(addr)) => { tracing::debug!( address=%addr, - "Failed to dial address using {}", - std::any::type_name::() + "Failed to dial using {}", + std::any::type_name::(), ); addr } @@ -115,16 +119,16 @@ where tracing::trace!( address=%addr, - "Attempting to dial address using {}", + "Attempting to dial {}", std::any::type_name::() ); - let addr = match self.1.dial(addr) { + let addr = match self.1.dial(addr, opts) { Ok(connec) => return Ok(EitherFuture::Second(connec)), Err(TransportError::MultiaddrNotSupported(addr)) => { tracing::debug!( address=%addr, - "Failed to dial address using {}", - std::any::type_name::() + "Failed to dial using {}", + std::any::type_name::(), ); addr } @@ -136,37 +140,6 @@ where Err(TransportError::MultiaddrNotSupported(addr)) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - let addr = match self.0.dial_as_listener(addr) { - Ok(connec) => return Ok(EitherFuture::First(connec)), - Err(TransportError::MultiaddrNotSupported(addr)) => addr, - Err(TransportError::Other(err)) => { - return Err(TransportError::Other(Either::Left(err))) - } - }; - - let addr = match self.1.dial_as_listener(addr) { - Ok(connec) => return Ok(EitherFuture::Second(connec)), - Err(TransportError::MultiaddrNotSupported(addr)) => addr, - Err(TransportError::Other(err)) => { - return Err(TransportError::Other(Either::Right(err))) - } - }; - - Err(TransportError::MultiaddrNotSupported(addr)) - } - - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - if let Some(addr) = self.0.address_translation(server, observed) { - Some(addr) - } else { - self.1.address_translation(server, observed) - } - } - fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/core/src/transport/dummy.rs b/core/src/transport/dummy.rs index 951d1039328..72558d34a79 100644 --- a/core/src/transport/dummy.rs +++ b/core/src/transport/dummy.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::transport::{ListenerId, Transport, TransportError, TransportEvent}; +use crate::transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent}; use crate::Multiaddr; use futures::{prelude::*, task::Context, task::Poll}; use std::{fmt, io, marker::PhantomData, pin::Pin}; @@ -71,21 +71,14 @@ impl Transport for DummyTransport { false } - fn dial(&mut self, addr: Multiaddr) -> Result> { - Err(TransportError::MultiaddrNotSupported(addr)) - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + _opts: DialOpts, ) -> Result> { Err(TransportError::MultiaddrNotSupported(addr)) } - fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option { - None - } - fn poll( self: Pin<&mut Self>, _: &mut Context<'_>, @@ -94,7 +87,7 @@ impl Transport for DummyTransport { } } -/// Implementation of `AsyncRead` and `AsyncWrite`. Not meant to be instanciated. +/// Implementation of `AsyncRead` and `AsyncWrite`. Not meant to be instantiated. pub struct DummyStream(()); impl fmt::Debug for DummyStream { diff --git a/core/src/transport/global_only.rs b/core/src/transport/global_only.rs index 0671b0e9984..83774f37004 100644 --- a/core/src/transport/global_only.rs +++ b/core/src/transport/global_only.rs @@ -20,7 +20,7 @@ use crate::{ multiaddr::{Multiaddr, Protocol}, - transport::{ListenerId, TransportError, TransportEvent}, + transport::{DialOpts, ListenerId, TransportError, TransportEvent}, }; use std::{ pin::Pin, @@ -287,47 +287,25 @@ impl crate::Transport for Transport { self.inner.remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { - match addr.iter().next() { - Some(Protocol::Ip4(a)) => { - if !ipv4_global::is_global(a) { - tracing::debug!(ip=%a, "Not dialing non global IP address"); - return Err(TransportError::MultiaddrNotSupported(addr)); - } - self.inner.dial(addr) - } - Some(Protocol::Ip6(a)) => { - if !ipv6_global::is_global(a) { - tracing::debug!(ip=%a, "Not dialing non global IP address"); - return Err(TransportError::MultiaddrNotSupported(addr)); - } - self.inner.dial(addr) - } - _ => { - tracing::debug!(address=%addr, "Not dialing unsupported Multiaddress"); - Err(TransportError::MultiaddrNotSupported(addr)) - } - } - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + opts: DialOpts, ) -> Result> { match addr.iter().next() { Some(Protocol::Ip4(a)) => { if !ipv4_global::is_global(a) { - tracing::debug!(ip=?a, "Not dialing non global IP address"); + tracing::debug!(ip=%a, "Not dialing non global IP address"); return Err(TransportError::MultiaddrNotSupported(addr)); } - self.inner.dial_as_listener(addr) + self.inner.dial(addr, opts) } Some(Protocol::Ip6(a)) => { if !ipv6_global::is_global(a) { - tracing::debug!(ip=?a, "Not dialing non global IP address"); + tracing::debug!(ip=%a, "Not dialing non global IP address"); return Err(TransportError::MultiaddrNotSupported(addr)); } - self.inner.dial_as_listener(addr) + self.inner.dial(addr, opts) } _ => { tracing::debug!(address=%addr, "Not dialing unsupported Multiaddress"); @@ -336,10 +314,6 @@ impl crate::Transport for Transport { } } - fn address_translation(&self, listen: &Multiaddr, observed: &Multiaddr) -> Option { - self.inner.address_translation(listen, observed) - } - fn poll( mut self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/core/src/transport/map.rs b/core/src/transport/map.rs index 553f3e6338d..9aab84ba8b1 100644 --- a/core/src/transport/map.rs +++ b/core/src/transport/map.rs @@ -18,8 +18,9 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +use crate::transport::DialOpts; use crate::{ - connection::{ConnectedPoint, Endpoint}, + connection::ConnectedPoint, transport::{Transport, TransportError, TransportEvent}, }; use futures::prelude::*; @@ -73,26 +74,16 @@ where self.transport.remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { - let future = self.transport.dial(addr.clone())?; - let p = ConnectedPoint::Dialer { - address: addr, - role_override: Endpoint::Dialer, - }; - Ok(MapFuture { - inner: future, - args: Some((self.fun.clone(), p)), - }) - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + opts: DialOpts, ) -> Result> { - let future = self.transport.dial_as_listener(addr.clone())?; + let future = self.transport.dial(addr.clone(), opts)?; let p = ConnectedPoint::Dialer { address: addr, - role_override: Endpoint::Listener, + role_override: opts.role, + port_use: opts.port_use, }; Ok(MapFuture { inner: future, @@ -100,10 +91,6 @@ where }) } - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.transport.address_translation(server, observed) - } - fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/core/src/transport/map_err.rs b/core/src/transport/map_err.rs index 56e1ebf2929..5d44af9af2e 100644 --- a/core/src/transport/map_err.rs +++ b/core/src/transport/map_err.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::transport::{ListenerId, Transport, TransportError, TransportEvent}; +use crate::transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent}; use futures::prelude::*; use multiaddr::Multiaddr; use std::{error, pin::Pin, task::Context, task::Poll}; @@ -65,23 +65,13 @@ where self.transport.remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { - let map = self.map.clone(); - match self.transport.dial(addr) { - Ok(future) => Ok(MapErrDial { - inner: future, - map: Some(map), - }), - Err(err) => Err(err.map(map)), - } - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + opts: DialOpts, ) -> Result> { let map = self.map.clone(); - match self.transport.dial_as_listener(addr) { + match self.transport.dial(addr, opts) { Ok(future) => Ok(MapErrDial { inner: future, map: Some(map), @@ -90,10 +80,6 @@ where } } - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.transport.address_translation(server, observed) - } - fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/core/src/transport/memory.rs b/core/src/transport/memory.rs index bf88215dd43..85680265e8b 100644 --- a/core/src/transport/memory.rs +++ b/core/src/transport/memory.rs @@ -18,15 +18,9 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::transport::{ListenerId, Transport, TransportError, TransportEvent}; +use crate::transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent}; use fnv::FnvHashMap; -use futures::{ - channel::mpsc, - future::{self, Ready}, - prelude::*, - task::Context, - task::Poll, -}; +use futures::{channel::mpsc, future::Ready, prelude::*, task::Context, task::Poll}; use multiaddr::{Multiaddr, Protocol}; use once_cell::sync::Lazy; use parking_lot::Mutex; @@ -214,7 +208,11 @@ impl Transport for MemoryTransport { } } - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + _opts: DialOpts, + ) -> Result> { let port = if let Ok(port) = parse_memory_addr(&addr) { if let Some(port) = NonZeroU64::new(port) { port @@ -228,17 +226,6 @@ impl Transport for MemoryTransport { DialFuture::new(port).ok_or(TransportError::Other(MemoryTransportError::Unreachable)) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - self.dial(addr) - } - - fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option { - None - } - fn poll( mut self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -411,6 +398,8 @@ impl Drop for Chan { #[cfg(test)] mod tests { + use crate::{transport::PortUse, Endpoint}; + use super::*; #[test] @@ -495,7 +484,13 @@ mod tests { fn port_not_in_use() { let mut transport = MemoryTransport::default(); assert!(transport - .dial("/memory/810172461024613".parse().unwrap()) + .dial( + "/memory/810172461024613".parse().unwrap(), + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::New + } + ) .is_err()); transport .listen_on( @@ -504,7 +499,13 @@ mod tests { ) .unwrap(); assert!(transport - .dial("/memory/810172461024613".parse().unwrap()) + .dial( + "/memory/810172461024613".parse().unwrap(), + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::New + } + ) .is_ok()); } @@ -571,7 +572,17 @@ mod tests { let mut t2 = MemoryTransport::default(); let dialer = async move { - let mut socket = t2.dial(cloned_t1_addr).unwrap().await.unwrap(); + let mut socket = t2 + .dial( + cloned_t1_addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::New, + }, + ) + .unwrap() + .await + .unwrap(); socket.write_all(&msg).await.unwrap(); }; @@ -607,7 +618,13 @@ mod tests { let dialer = async move { MemoryTransport::default() - .dial(listener_addr_cloned) + .dial( + listener_addr_cloned, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::New, + }, + ) .unwrap() .await .unwrap(); @@ -658,7 +675,13 @@ mod tests { let dialer = async move { let chan = MemoryTransport::default() - .dial(listener_addr_cloned) + .dial( + listener_addr_cloned, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::New, + }, + ) .unwrap() .await .unwrap(); diff --git a/core/src/transport/optional.rs b/core/src/transport/optional.rs index 839f55a4000..f18bfa441b0 100644 --- a/core/src/transport/optional.rs +++ b/core/src/transport/optional.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::transport::{ListenerId, Transport, TransportError, TransportEvent}; +use crate::transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent}; use multiaddr::Multiaddr; use std::{pin::Pin, task::Context, task::Poll}; @@ -80,33 +80,18 @@ where } } - fn dial(&mut self, addr: Multiaddr) -> Result> { - if let Some(inner) = self.0.as_mut() { - inner.dial(addr) - } else { - Err(TransportError::MultiaddrNotSupported(addr)) - } - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + opts: DialOpts, ) -> Result> { if let Some(inner) = self.0.as_mut() { - inner.dial_as_listener(addr) + inner.dial(addr, opts) } else { Err(TransportError::MultiaddrNotSupported(addr)) } } - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - if let Some(inner) = &self.0 { - inner.address_translation(server, observed) - } else { - None - } - } - fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/core/src/transport/timeout.rs b/core/src/transport/timeout.rs index 0e8ab3f5201..830ed099629 100644 --- a/core/src/transport/timeout.rs +++ b/core/src/transport/timeout.rs @@ -24,6 +24,7 @@ //! underlying `Transport`. // TODO: add example +use crate::transport::DialOpts; use crate::{ transport::{ListenerId, TransportError, TransportEvent}, Multiaddr, Transport, @@ -99,24 +100,14 @@ where self.inner.remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { - let dial = self - .inner - .dial(addr) - .map_err(|err| err.map(TransportTimeoutError::Other))?; - Ok(Timeout { - inner: dial, - timer: Delay::new(self.outgoing_timeout), - }) - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + opts: DialOpts, ) -> Result> { let dial = self .inner - .dial_as_listener(addr) + .dial(addr, opts) .map_err(|err| err.map(TransportTimeoutError::Other))?; Ok(Timeout { inner: dial, @@ -124,10 +115,6 @@ where }) } - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.inner.address_translation(server, observed) - } - fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/core/src/transport/upgrade.rs b/core/src/transport/upgrade.rs index 9626312b5fb..66b9e7509af 100644 --- a/core/src/transport/upgrade.rs +++ b/core/src/transport/upgrade.rs @@ -22,6 +22,7 @@ pub use crate::upgrade::Version; +use crate::transport::DialOpts; use crate::{ connection::ConnectedPoint, muxing::{StreamMuxer, StreamMuxerBox}, @@ -335,21 +336,18 @@ where type ListenerUpgrade = T::ListenerUpgrade; type Dial = T::Dial; - fn dial(&mut self, addr: Multiaddr) -> Result> { - self.0.dial(addr) + fn dial( + &mut self, + addr: Multiaddr, + opts: DialOpts, + ) -> Result> { + self.0.dial(addr, opts) } fn remove_listener(&mut self, id: ListenerId) -> bool { self.0.remove_listener(id) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - self.0.dial_as_listener(addr) - } - fn listen_on( &mut self, id: ListenerId, @@ -358,10 +356,6 @@ where self.0.listen_on(id, addr) } - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.0.address_translation(server, observed) - } - fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -404,10 +398,14 @@ where type ListenerUpgrade = ListenerUpgradeFuture; type Dial = DialUpgradeFuture; - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + opts: DialOpts, + ) -> Result> { let future = self .inner - .dial(addr) + .dial(addr, opts) .map_err(|err| err.map(TransportUpgradeError::Transport))?; Ok(DialUpgradeFuture { future: Box::pin(future), @@ -419,20 +417,6 @@ where self.inner.remove_listener(id) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - let future = self - .inner - .dial_as_listener(addr) - .map_err(|err| err.map(TransportUpgradeError::Transport))?; - Ok(DialUpgradeFuture { - future: Box::pin(future), - upgrade: future::Either::Left(Some(self.upgrade.clone())), - }) - } - fn listen_on( &mut self, id: ListenerId, @@ -443,10 +427,6 @@ where .map_err(|err| err.map(TransportUpgradeError::Transport)) } - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.inner.address_translation(server, observed) - } - fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/core/src/upgrade.rs b/core/src/upgrade.rs index 69561fbebd8..7a1fd3724d0 100644 --- a/core/src/upgrade.rs +++ b/core/src/upgrade.rs @@ -52,9 +52,9 @@ //! can be used by the user to control the behaviour of the protocol. //! //! > **Note**: You can use the `apply_inbound` or `apply_outbound` methods to try upgrade a -//! connection or substream. However if you use the recommended `Swarm` or -//! `ConnectionHandler` APIs, the upgrade is automatically handled for you and you don't -//! need to use these methods. +//! > connection or substream. However if you use the recommended `Swarm` or +//! > `ConnectionHandler` APIs, the upgrade is automatically handled for you and you don't +//! > need to use these methods. //! mod apply; diff --git a/core/src/upgrade/apply.rs b/core/src/upgrade/apply.rs index 15cb0348cf3..f84aaaac9fa 100644 --- a/core/src/upgrade/apply.rs +++ b/core/src/upgrade/apply.rs @@ -21,7 +21,7 @@ use crate::upgrade::{InboundConnectionUpgrade, OutboundConnectionUpgrade, UpgradeError}; use crate::{connection::ConnectedPoint, Negotiated}; use futures::{future::Either, prelude::*}; -use multistream_select::{self, DialerSelectFuture, ListenerSelectFuture}; +use multistream_select::{DialerSelectFuture, ListenerSelectFuture}; use std::{mem, pin::Pin, task::Context, task::Poll}; pub(crate) use multistream_select::Version; diff --git a/core/src/upgrade/ready.rs b/core/src/upgrade/ready.rs index 323f1f73f32..7e235902651 100644 --- a/core/src/upgrade/ready.rs +++ b/core/src/upgrade/ready.rs @@ -31,7 +31,7 @@ pub struct ReadyUpgrade

{ } impl

ReadyUpgrade

{ - pub fn new(protocol_name: P) -> Self { + pub const fn new(protocol_name: P) -> Self { Self { protocol_name } } } diff --git a/core/tests/transport_upgrade.rs b/core/tests/transport_upgrade.rs index a8872051618..d8bec6f2b59 100644 --- a/core/tests/transport_upgrade.rs +++ b/core/tests/transport_upgrade.rs @@ -19,10 +19,11 @@ // DEALINGS IN THE SOFTWARE. use futures::prelude::*; -use libp2p_core::transport::{ListenerId, MemoryTransport, Transport}; +use libp2p_core::transport::{DialOpts, ListenerId, MemoryTransport, PortUse, Transport}; use libp2p_core::upgrade::{ self, InboundConnectionUpgrade, OutboundConnectionUpgrade, UpgradeInfo, }; +use libp2p_core::Endpoint; use libp2p_identity as identity; use libp2p_mplex::MplexConfig; use libp2p_noise as noise; @@ -121,7 +122,17 @@ fn upgrade_pipeline() { }; let client = async move { - let (peer, _mplex) = dialer_transport.dial(listen_addr2).unwrap().await.unwrap(); + let (peer, _mplex) = dialer_transport + .dial( + listen_addr2, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::New, + }, + ) + .unwrap() + .await + .unwrap(); assert_eq!(peer, listener_id); }; diff --git a/deny.toml b/deny.toml index 6634887128e..5be86107edf 100644 --- a/deny.toml +++ b/deny.toml @@ -2,20 +2,14 @@ # More documentation for the advisories section can be found here: # https://embarkstudios.github.io/cargo-deny/checks/advisories/cfg.html [advisories] +# Version of the advisory config. See https://github.com/EmbarkStudios/cargo-deny/pull/611 +version = 2 # The path where the advisory database is cloned/fetched into db-path = "~/cargo/advisory-db" # The url of the advisory database to use -db-urls = [ "https://github.com/rustsec/advisory-db" ] -# The lint level for security vulnerabilities -vulnerability = "deny" -# The lint level for unmaintained crates -unmaintained = "warn" +db-urls = ["https://github.com/rustsec/advisory-db"] # The lint level for crates that have been yanked from their source registry yanked = "warn" -# The lint level for crates with security notices. Note that as of -# 2019-12-17 there are no security notice advisories in -# https://github.com/rustsec/advisory-db -notice = "warn" # A list of advisory IDs to ignore. Note that ignored advisories will still # output a note when they are encountered. ignore = [ @@ -35,35 +29,21 @@ ignore = [ # More documentation for the licenses section can be found here: # https://embarkstudios.github.io/cargo-deny/checks/licenses/cfg.html [licenses] -# The lint level for crates which do not have a detectable license -unlicensed = "deny" -# List of explictly allowed licenses +# Version of the license config. See https://github.com/EmbarkStudios/cargo-deny/pull/611 +version = 2 +# List of explicitly allowed licenses # See https://spdx.org/licenses/ for list of possible licenses -# [possible values: any SPDX 3.7 short identifier (+ optional exception)]. +# [possible values: any SPDX 3.11 short identifier (+ optional exception)]. allow = [ + "Apache-2.0 WITH LLVM-exception", "Apache-2.0", "BSD-2-Clause", + "BSD-3-Clause", + "ISC", "MIT", + "MPL-2.0", "Unlicense", ] -# List of explictly disallowed licenses -# See https://spdx.org/licenses/ for list of possible licenses -# [possible values: any SPDX 3.7 short identifier (+ optional exception)]. -deny = [] -# Lint level for licenses considered copyleft -copyleft = "allow" -# Blanket approval or denial for OSI-approved or FSF Free/Libre licenses -# * both - The license will be approved if it is both OSI-approved *AND* FSF -# * either - The license will be approved if it is either OSI-approved *OR* FSF -# * osi-only - The license will be approved if is OSI-approved *AND NOT* FSF -# * fsf-only - The license will be approved if is FSF *AND NOT* OSI-approved -# * neither - This predicate is ignored and the default lint level is used -allow-osi-fsf-free = "both" -# Lint level used when no other predicates are matched -# 1. License isn't in the allow or deny lists -# 2. License isn't copyleft -# 3. License isn't OSI/FSF, or allow-osi-fsf-free = "neither" -default = "deny" # The confidence threshold for detecting a license from license text. # The higher the value, the more closely the license text must be to the # canonical license text of a valid SPDX license file. @@ -76,7 +56,7 @@ exceptions = [ # https://www.openssl.org/blog/blog/2017/03/22/license/ # ring crate is ISC & MIT { allow = ["ISC", "MIT", "OpenSSL"], name = "ring" }, - # libp2p is not re-distributing unicode tables data by itself + # libp2p is not re-distributing unicode tables data by itself { allow = ["MIT", "Apache-2.0", "Unicode-DFS-2016"], name = "unicode-ident" }, ] diff --git a/docs/maintainer-handbook.md b/docs/maintainer-handbook.md index 6d36f6fe77c..0b090901216 100644 --- a/docs/maintainer-handbook.md +++ b/docs/maintainer-handbook.md @@ -31,7 +31,7 @@ This will have mergify approve your PR, thus fulfilling all requirements to auto Our CI checks that each crate which is modified gets a changelog entry. Whilst this is a good default safety-wise, it creates a lot of false-positives for changes that are internal and don't need a changelog entry. -For PRs that in the categories `chore`, `deps`, `refactor` and `docs`, this check is disabled automatically. +For PRs in the categories `chore`, `deps`, `refactor` and `docs`, this check is disabled automatically. Any other PR needs to explicitly disable this check if desired by applying the `internal-change` label. ## Dependencies diff --git a/examples/autonat/Cargo.toml b/examples/autonat/Cargo.toml index 642e621c257..010b76623e0 100644 --- a/examples/autonat/Cargo.toml +++ b/examples/autonat/Cargo.toml @@ -3,18 +3,18 @@ name = "autonat-example" version = "0.1.0" edition = "2021" publish = false -license = "MIT" +license = "MIT or Apache-2.0" [package.metadata.release] release = false [dependencies] -tokio = { version = "1.36", features = ["full"] } -clap = { version = "4.4.16", features = ["derive"] } -futures = "0.3.30" +tokio = { workspace = true, features = ["full"] } +clap = { version = "4.5.6", features = ["derive"] } +futures = { workspace = true } libp2p = { path = "../../libp2p", features = ["tokio", "tcp", "noise", "yamux", "autonat", "identify", "macros"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/autonat/src/bin/autonat_client.rs b/examples/autonat/src/bin/autonat_client.rs index 3fb25aa6222..def66c4823b 100644 --- a/examples/autonat/src/bin/autonat_client.rs +++ b/examples/autonat/src/bin/autonat_client.rs @@ -110,22 +110,3 @@ impl Behaviour { } } } - -#[derive(Debug)] -#[allow(clippy::large_enum_variant)] -enum Event { - AutoNat(autonat::Event), - Identify(identify::Event), -} - -impl From for Event { - fn from(v: identify::Event) -> Self { - Self::Identify(v) - } -} - -impl From for Event { - fn from(v: autonat::Event) -> Self { - Self::AutoNat(v) - } -} diff --git a/examples/autonat/src/bin/autonat_server.rs b/examples/autonat/src/bin/autonat_server.rs index 44a53f0d17f..389cc0fa26f 100644 --- a/examples/autonat/src/bin/autonat_server.rs +++ b/examples/autonat/src/bin/autonat_server.rs @@ -94,22 +94,3 @@ impl Behaviour { } } } - -#[derive(Debug)] -#[allow(clippy::large_enum_variant)] -enum Event { - AutoNat(autonat::Event), - Identify(identify::Event), -} - -impl From for Event { - fn from(v: identify::Event) -> Self { - Self::Identify(v) - } -} - -impl From for Event { - fn from(v: autonat::Event) -> Self { - Self::AutoNat(v) - } -} diff --git a/examples/autonatv2/Cargo.toml b/examples/autonatv2/Cargo.toml new file mode 100644 index 00000000000..6c862ee22e4 --- /dev/null +++ b/examples/autonatv2/Cargo.toml @@ -0,0 +1,38 @@ +[package] +name = "autonatv2" +version = "0.1.0" +edition = "2021" +publish = false +license = "MIT or Apache-2.0" + +[package.metadata.release] +release = false + +[[bin]] +name = "autonatv2_client" + +[[bin]] +name = "autonatv2_server" + +[dependencies] +libp2p = { workspace = true, features = ["macros", "tokio", "tcp", "noise", "yamux", "autonat", "identify", "dns", "quic"] } +clap = { version = "4.4.18", features = ["derive"] } +tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread"] } +tracing = "0.1.40" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } +rand = "0.8.5" +opentelemetry = { version = "0.21.0", optional = true } +opentelemetry_sdk = { version = "0.21.1", optional = true, features = ["rt-tokio"] } +tracing-opentelemetry = { version = "0.22.0", optional = true } +opentelemetry-jaeger = { version = "0.20.0", optional = true, features = ["rt-tokio"] } +cfg-if = "1.0.0" + +[features] +jaeger = ["opentelemetry", "opentelemetry_sdk", "tracing-opentelemetry", "opentelemetry-jaeger"] +opentelemetry = ["dep:opentelemetry"] +opentelemetry_sdk = ["dep:opentelemetry_sdk"] +tracing-opentelemetry = ["dep:tracing-opentelemetry"] +opentelemetry-jaeger = ["dep:opentelemetry-jaeger"] + +[lints] +workspace = true diff --git a/examples/autonatv2/Dockerfile b/examples/autonatv2/Dockerfile new file mode 100644 index 00000000000..6bc92e4d11b --- /dev/null +++ b/examples/autonatv2/Dockerfile @@ -0,0 +1,20 @@ +FROM rust:1.81-alpine as builder + +RUN apk add musl-dev + +WORKDIR /workspace +COPY . . +RUN --mount=type=cache,target=./target \ + --mount=type=cache,target=/usr/local/cargo/registry \ + cargo build --release --package autonatv2 --bin autonatv2_server -F jaeger + +RUN --mount=type=cache,target=./target \ + mv ./target/release/autonatv2_server /usr/local/bin/autonatv2_server + +FROM alpine:latest + +COPY --from=builder /usr/local/bin/autonatv2_server /app/autonatv2_server + +EXPOSE 4884 + +ENTRYPOINT [ "/app/autonatv2_server", "-l", "4884" ] diff --git a/examples/autonatv2/docker-compose.yml b/examples/autonatv2/docker-compose.yml new file mode 100644 index 00000000000..75f44e7e6f9 --- /dev/null +++ b/examples/autonatv2/docker-compose.yml @@ -0,0 +1,16 @@ +version: '3' + +services: + autonatv2: + build: + context: ../.. + dockerfile: examples/autonatv2/Dockerfile + ports: + - 4884:4884 + jaeger: + image: jaegertracing/all-in-one + ports: + - 6831:6831/udp + - 6832:6832/udp + - 16686:16686 + - 14268:14268 diff --git a/examples/autonatv2/src/bin/autonatv2_client.rs b/examples/autonatv2/src/bin/autonatv2_client.rs new file mode 100644 index 00000000000..de902514dd8 --- /dev/null +++ b/examples/autonatv2/src/bin/autonatv2_client.rs @@ -0,0 +1,111 @@ +use std::{error::Error, net::Ipv4Addr, time::Duration}; + +use clap::Parser; +use libp2p::{ + autonat, + futures::StreamExt, + identify, identity, + multiaddr::Protocol, + noise, + swarm::{dial_opts::DialOpts, NetworkBehaviour, SwarmEvent}, + tcp, yamux, Multiaddr, SwarmBuilder, +}; +use rand::rngs::OsRng; +use tracing_subscriber::EnvFilter; + +#[derive(Debug, Parser)] +#[clap(name = "libp2p autonatv2 client")] +struct Opt { + /// Port where the client will listen for incoming connections. + #[clap(short = 'p', long, default_value_t = 0)] + listen_port: u16, + + /// Address of the server where want to connect to. + #[clap(short = 'a', long)] + server_address: Multiaddr, + + /// Probe interval in seconds. + #[clap(short = 't', long, default_value = "2")] + probe_interval: u64, +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); + + let opt = Opt::parse(); + + let mut swarm = SwarmBuilder::with_new_identity() + .with_tokio() + .with_tcp( + tcp::Config::default(), + noise::Config::new, + yamux::Config::default, + )? + .with_quic() + .with_dns()? + .with_behaviour(|key| Behaviour::new(key.public(), opt.probe_interval))? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(10))) + .build(); + + swarm.listen_on( + Multiaddr::empty() + .with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED)) + .with(Protocol::Tcp(opt.listen_port)), + )?; + + swarm.dial( + DialOpts::unknown_peer_id() + .address(opt.server_address) + .build(), + )?; + + loop { + match swarm.select_next_some().await { + SwarmEvent::NewListenAddr { address, .. } => { + println!("Listening on {address:?}"); + } + SwarmEvent::Behaviour(BehaviourEvent::Autonat(autonat::v2::client::Event { + server, + tested_addr, + bytes_sent, + result: Ok(()), + })) => { + println!("Tested {tested_addr} with {server}. Sent {bytes_sent} bytes for verification. Everything Ok and verified."); + } + SwarmEvent::Behaviour(BehaviourEvent::Autonat(autonat::v2::client::Event { + server, + tested_addr, + bytes_sent, + result: Err(e), + })) => { + println!("Tested {tested_addr} with {server}. Sent {bytes_sent} bytes for verification. Failed with {e:?}."); + } + SwarmEvent::ExternalAddrConfirmed { address } => { + println!("External address confirmed: {address}"); + } + _ => {} + } + } +} + +#[derive(NetworkBehaviour)] +pub struct Behaviour { + autonat: autonat::v2::client::Behaviour, + identify: identify::Behaviour, +} + +impl Behaviour { + pub fn new(key: identity::PublicKey, probe_interval: u64) -> Self { + Self { + autonat: autonat::v2::client::Behaviour::new( + OsRng, + autonat::v2::client::Config::default() + .with_probe_interval(Duration::from_secs(probe_interval)), + ), + identify: identify::Behaviour::new(identify::Config::new("/ipfs/0.1.0".into(), key)), + } + } +} diff --git a/examples/autonatv2/src/bin/autonatv2_server.rs b/examples/autonatv2/src/bin/autonatv2_server.rs new file mode 100644 index 00000000000..849ed3b3b0a --- /dev/null +++ b/examples/autonatv2/src/bin/autonatv2_server.rs @@ -0,0 +1,87 @@ +use std::{error::Error, net::Ipv4Addr, time::Duration}; + +use cfg_if::cfg_if; +use clap::Parser; +use libp2p::{ + autonat, + futures::StreamExt, + identify, identity, + multiaddr::Protocol, + noise, + swarm::{NetworkBehaviour, SwarmEvent}, + tcp, yamux, Multiaddr, SwarmBuilder, +}; +use rand::rngs::OsRng; + +#[derive(Debug, Parser)] +#[clap(name = "libp2p autonatv2 server")] +struct Opt { + #[clap(short, long, default_value_t = 0)] + listen_port: u16, +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + cfg_if! { + if #[cfg(feature = "jaeger")] { + use tracing_subscriber::layer::SubscriberExt; + use opentelemetry_sdk::runtime::Tokio; + let tracer = opentelemetry_jaeger::new_agent_pipeline() + .with_endpoint("jaeger:6831") + .with_service_name("autonatv2") + .install_batch(Tokio)?; + let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); + let subscriber = tracing_subscriber::Registry::default() + .with(telemetry); + } else { + let subscriber = tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .finish(); + } + } + tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); + + let opt = Opt::parse(); + + let mut swarm = SwarmBuilder::with_new_identity() + .with_tokio() + .with_tcp( + tcp::Config::default(), + noise::Config::new, + yamux::Config::default, + )? + .with_quic() + .with_dns()? + .with_behaviour(|key| Behaviour::new(key.public()))? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) + .build(); + + swarm.listen_on( + Multiaddr::empty() + .with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED)) + .with(Protocol::Tcp(opt.listen_port)), + )?; + + loop { + match swarm.select_next_some().await { + SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"), + SwarmEvent::Behaviour(event) => println!("{event:?}"), + e => println!("{e:?}"), + } + } +} + +#[derive(NetworkBehaviour)] +pub struct Behaviour { + autonat: autonat::v2::server::Behaviour, + identify: identify::Behaviour, +} + +impl Behaviour { + pub fn new(key: identity::PublicKey) -> Self { + Self { + autonat: autonat::v2::server::Behaviour::new(OsRng), + identify: identify::Behaviour::new(identify::Config::new("/ipfs/0.1.0".into(), key)), + } + } +} diff --git a/examples/browser-webrtc/Cargo.toml b/examples/browser-webrtc/Cargo.toml index 322ae2b8348..c3630d805fb 100644 --- a/examples/browser-webrtc/Cargo.toml +++ b/examples/browser-webrtc/Cargo.toml @@ -16,25 +16,25 @@ release = false crate-type = ["cdylib"] [dependencies] -anyhow = "1.0.80" -futures = "0.3.30" +anyhow = "1.0.86" +futures = { workspace = true } rand = "0.8" -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -axum = "0.7.4" +axum = "0.7.5" libp2p = { path = "../../libp2p", features = [ "ed25519", "macros", "ping", "tokio"] } libp2p-webrtc = { workspace = true, features = ["tokio"] } -rust-embed = { version = "8.3.0", features = ["include-exclude", "interpolate-folder-path"] } -tokio = { version = "1.36", features = ["macros", "net", "rt", "signal"] } +rust-embed = { version = "8.4.0", features = ["include-exclude", "interpolate-folder-path"] } +tokio = { workspace = true, features = ["macros", "net", "rt", "signal"] } tokio-util = { version = "0.7", features = ["compat"] } tower = "0.4" tower-http = { version = "0.5.2", features = ["cors"] } mime_guess = "2.0.4" [target.'cfg(target_arch = "wasm32")'.dependencies] -js-sys = "0.3.67" +js-sys = "0.3.69" libp2p = { path = "../../libp2p", features = [ "ed25519", "macros", "ping", "wasm-bindgen"] } libp2p-webrtc-websys = { workspace = true } tracing-wasm = "0.2.1" diff --git a/examples/chat/Cargo.toml b/examples/chat/Cargo.toml index a16c930e5b3..a1d32956825 100644 --- a/examples/chat/Cargo.toml +++ b/examples/chat/Cargo.toml @@ -9,12 +9,12 @@ license = "MIT" release = false [dependencies] -tokio = { version = "1.36", features = ["full"] } +tokio = { workspace = true, features = ["full"] } async-trait = "0.1" -futures = "0.3.30" +futures = { workspace = true } libp2p = { path = "../../libp2p", features = [ "tokio", "gossipsub", "mdns", "noise", "macros", "tcp", "yamux", "quic"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/chat/README.md b/examples/chat/README.md index 96bad137b07..2dbf585100b 100644 --- a/examples/chat/README.md +++ b/examples/chat/README.md @@ -11,7 +11,7 @@ It showcases how peers can connect, discover each other using mDNS, and engage i ``` 2. Mutual mDNS discovery may take a few seconds. When each peer does discover the other -it will print a message like: + it will print a message like: ```sh mDNS discovered a new peer: {peerId} ``` diff --git a/examples/dcutr/Cargo.toml b/examples/dcutr/Cargo.toml index 8443e9a59c7..c1b4bbc6e7e 100644 --- a/examples/dcutr/Cargo.toml +++ b/examples/dcutr/Cargo.toml @@ -9,14 +9,14 @@ license = "MIT" release = false [dependencies] -clap = { version = "4.4.16", features = ["derive"] } -futures = "0.3.30" +clap = { version = "4.5.6", features = ["derive"] } +futures = { workspace = true } futures-timer = "3.0" libp2p = { path = "../../libp2p", features = [ "dns", "dcutr", "identify", "macros", "noise", "ping", "quic", "relay", "rendezvous", "tcp", "tokio", "yamux"] } log = "0.4" -tokio = { version = "1.36", features = ["macros", "net", "rt", "signal"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tokio = { workspace = true, features = ["macros", "net", "rt", "signal"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/dcutr/src/main.rs b/examples/dcutr/src/main.rs index 51df670f8a7..630d4b2b1f3 100644 --- a/examples/dcutr/src/main.rs +++ b/examples/dcutr/src/main.rs @@ -89,7 +89,7 @@ async fn main() -> Result<(), Box> { libp2p::SwarmBuilder::with_existing_identity(generate_ed25519(opts.secret_key_seed)) .with_tokio() .with_tcp( - tcp::Config::default().port_reuse(true).nodelay(true), + tcp::Config::default().nodelay(true), noise::Config::new, yamux::Config::default, )? diff --git a/examples/distributed-key-value-store/Cargo.toml b/examples/distributed-key-value-store/Cargo.toml index a7efe3c0697..9c2e2bce5c9 100644 --- a/examples/distributed-key-value-store/Cargo.toml +++ b/examples/distributed-key-value-store/Cargo.toml @@ -11,10 +11,10 @@ release = false [dependencies] async-std = { version = "1.12", features = ["attributes"] } async-trait = "0.1" -futures = "0.3.30" +futures = { workspace = true } libp2p = { path = "../../libp2p", features = [ "async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/distributed-key-value-store/README.md b/examples/distributed-key-value-store/README.md index 6065609cfdf..4a44e0c5427 100644 --- a/examples/distributed-key-value-store/README.md +++ b/examples/distributed-key-value-store/README.md @@ -9,10 +9,10 @@ This example showcases a basic distributed key-value store implemented using **l 1. Open two terminal windows, type `cargo run` and press Enter. 2. In terminal one, type `PUT my-key my-value` and press Enter. -This command will store the value `my-value` with the key `my-key` in the distributed key-value store. + This command will store the value `my-value` with the key `my-key` in the distributed key-value store. 3. In terminal two, type `GET my-key` and press Enter. -This command will retrieve the value associated with the key `my-key` from the key-value store. + This command will retrieve the value associated with the key `my-key` from the key-value store. 4. To exit, press `Ctrl-c` in each terminal window to gracefully close the instances. @@ -22,13 +22,13 @@ This command will retrieve the value associated with the key `my-key` from the k You can also use provider records instead of key-value records in the distributed store. 1. Open two terminal windows and start two instances of the key-value store. -If your local network supports mDNS, the instances will automatically connect. + If your local network supports mDNS, the instances will automatically connect. 2. In terminal one, type `PUT_PROVIDER my-key` and press Enter. -This command will register the peer as a provider for the key `my-key` in the distributed key-value store. + This command will register the peer as a provider for the key `my-key` in the distributed key-value store. 3. In terminal two, type `GET_PROVIDERS my-key` and press Enter. -This command will retrieve the list of providers for the key `my-key` from the key-value store. + This command will retrieve the list of providers for the key `my-key` from the key-value store. 4. To exit, press `Ctrl-c` in each terminal window to gracefully close the instances. diff --git a/examples/file-sharing/Cargo.toml b/examples/file-sharing/Cargo.toml index 8d20a702718..7cbb96cc7ed 100644 --- a/examples/file-sharing/Cargo.toml +++ b/examples/file-sharing/Cargo.toml @@ -10,12 +10,12 @@ release = false [dependencies] serde = { version = "1.0", features = ["derive"] } -tokio = { version = "1.36.0", features = ["full"] } -clap = { version = "4.4.16", features = ["derive"] } -futures = "0.3.30" +tokio = { workspace = true, features = ["full"] } +clap = { version = "4.5.6", features = ["derive"] } +futures = { workspace = true } libp2p = { path = "../../libp2p", features = [ "tokio", "cbor", "dns", "kad", "noise", "macros", "request-response", "tcp", "websocket", "yamux"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } void = "1.0.2" [lints] diff --git a/examples/file-sharing/README.md b/examples/file-sharing/README.md index 2a2b3ec5317..5424026bb12 100644 --- a/examples/file-sharing/README.md +++ b/examples/file-sharing/README.md @@ -12,28 +12,28 @@ Retrievers can locate and retrieve files by their names from any node in the net Let's understand the flow of the file sharing process: - **File Providers**: Nodes A and B serve as file providers. -Each node offers a specific file: file FA for node A and file FB for node B. -To make their files available, they advertise themselves as providers on the DHT using `libp2p-kad`. -This enables other nodes in the network to discover and retrieve their files. + Each node offers a specific file: file FA for node A and file FB for node B. + To make their files available, they advertise themselves as providers on the DHT using `libp2p-kad`. + This enables other nodes in the network to discover and retrieve their files. - **File Retrievers**: Node C acts as a file retriever. -It wants to retrieve either file FA or FB. -Using `libp2p-kad`, it can locate the providers for these files on the DHT without being directly connected to them. -Node C connects to the corresponding provider node and requests the file content using `libp2p-request-response`. + It wants to retrieve either file FA or FB. + Using `libp2p-kad`, it can locate the providers for these files on the DHT without being directly connected to them. + Node C connects to the corresponding provider node and requests the file content using `libp2p-request-response`. - **DHT and Network Connectivity**: The DHT (Distributed Hash Table) plays a crucial role in the file sharing process. -It allows nodes to store and discover information about file providers. -Nodes in the network are interconnected via the DHT, enabling efficient file discovery and retrieval. + It allows nodes to store and discover information about file providers. + Nodes in the network are interconnected via the DHT, enabling efficient file discovery and retrieval. ## Architectural Properties The File Sharing application has the following architectural properties: - **Clean and Clonable Interface**: The application provides a clean and clonable async/await interface, allowing users to interact with the network layer seamlessly. -The `Client` module encapsulates the necessary functionality for network communication. + The `Client` module encapsulates the necessary functionality for network communication. - **Efficient Network Handling**: The application operates with a single task that drives the network layer. -This design choice ensures efficient network communication without the need for locks or complex synchronization mechanisms. + This design choice ensures efficient network communication without the need for locks or complex synchronization mechanisms. ## Usage diff --git a/examples/identify/Cargo.toml b/examples/identify/Cargo.toml index 2dcc780ac22..8d12699afa7 100644 --- a/examples/identify/Cargo.toml +++ b/examples/identify/Cargo.toml @@ -9,12 +9,11 @@ license = "MIT" release = false [dependencies] -async-std = { version = "1.12", features = ["attributes"] } -async-trait = "0.1" -futures = "0.3.30" -libp2p = { path = "../../libp2p", features = ["async-std", "dns", "dcutr", "identify", "macros", "noise", "ping", "relay", "rendezvous", "tcp", "tokio","yamux"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tokio = { version = "1.37.0", features = ["full"] } +futures = { workspace = true } +libp2p = { path = "../../libp2p", features = ["identify", "noise", "tcp", "tokio", "yamux"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/identify/src/main.rs b/examples/identify/src/main.rs index 916317a5a43..22474061da6 100644 --- a/examples/identify/src/main.rs +++ b/examples/identify/src/main.rs @@ -25,14 +25,14 @@ use libp2p::{core::multiaddr::Multiaddr, identify, noise, swarm::SwarmEvent, tcp use std::{error::Error, time::Duration}; use tracing_subscriber::EnvFilter; -#[async_std::main] +#[tokio::main] async fn main() -> Result<(), Box> { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) .try_init(); let mut swarm = libp2p::SwarmBuilder::with_new_identity() - .with_async_std() + .with_tokio() .with_tcp( tcp::Config::default(), noise::Config::new, diff --git a/examples/ipfs-kad/Cargo.toml b/examples/ipfs-kad/Cargo.toml index 5581728794e..115c604269f 100644 --- a/examples/ipfs-kad/Cargo.toml +++ b/examples/ipfs-kad/Cargo.toml @@ -9,15 +9,15 @@ license = "MIT" release = false [dependencies] -tokio = { version = "1.36", features = ["rt-multi-thread", "macros"] } +tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } async-trait = "0.1" -clap = { version = "4.4.16", features = ["derive"] } +clap = { version = "4.5.6", features = ["derive"] } env_logger = "0.10" -futures = "0.3.30" -anyhow = "1.0.80" +futures = { workspace = true } +anyhow = "1.0.86" libp2p = { path = "../../libp2p", features = [ "tokio", "dns", "kad", "noise", "tcp", "yamux", "rsa"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/ipfs-kad/README.md b/examples/ipfs-kad/README.md index ccefb6b1d9f..05556c89382 100644 --- a/examples/ipfs-kad/README.md +++ b/examples/ipfs-kad/README.md @@ -11,7 +11,7 @@ By running this example, users can gain a better understanding of how the Kademl The example code demonstrates how to perform Kademlia queries on the IPFS network using the Rust P2P Library. -### Getting closes peers +### Getting closest peers By specifying a peer ID as a parameter, the code will search for the closest peers to the given peer ID. @@ -87,5 +87,5 @@ Failed to insert the PK record ## Conclusion In conclusion, this example provides a practical demonstration of using the Rust P2P Library to interact with the Kademlia protocol on the IPFS network. -By examining the code and running the example, users can gain insights into the inner workings of Kademlia and how it performs various basic actions like getting the closes peers or inserting records into the DHT. +By examining the code and running the example, users can gain insights into the inner workings of Kademlia and how it performs various basic actions like getting the closest peers or inserting records into the DHT. This knowledge can be valuable when developing peer-to-peer applications or understanding decentralized networks. diff --git a/examples/ipfs-private/Cargo.toml b/examples/ipfs-private/Cargo.toml index daa68cd8f5a..0813dba56e0 100644 --- a/examples/ipfs-private/Cargo.toml +++ b/examples/ipfs-private/Cargo.toml @@ -9,13 +9,13 @@ license = "MIT" release = false [dependencies] -tokio = { version = "1.36", features = ["rt-multi-thread", "macros", "io-std"] } +tokio = { workspace = true, features = ["rt-multi-thread", "macros", "io-std"] } async-trait = "0.1" -either = "1.9" -futures = "0.3.30" +either = "1.12" +futures = { workspace = true } libp2p = { path = "../../libp2p", features = [ "tokio", "gossipsub", "dns", "identify", "kad", "macros", "noise", "ping", "pnet", "tcp", "websocket", "yamux"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/ipfs-private/README.md b/examples/ipfs-private/README.md index bc3b12a7ac8..ae7fc42c9c4 100644 --- a/examples/ipfs-private/README.md +++ b/examples/ipfs-private/README.md @@ -24,11 +24,11 @@ To run the example, follow these steps: 2. Once the example is running, you can interact with the IPFS node using the following commands: - **Pubsub (Gossipsub):** You can use the gossipsub protocol to send and receive messages on the "chat" topic. - To send a message, type it in the console and press Enter. - The message will be broadcasted to other connected nodes using gossipsub. + To send a message, type it in the console and press Enter. + The message will be broadcasted to other connected nodes using gossipsub. - **Ping:** You can ping other connected nodes to test network connectivity. - The example will display the round-trip time (RTT) for successful pings or indicate if a timeout occurs. + The example will display the round-trip time (RTT) for successful pings or indicate if a timeout occurs. ## Conclusion diff --git a/examples/metrics/Cargo.toml b/examples/metrics/Cargo.toml index 39412d29aea..129b1abb1f3 100644 --- a/examples/metrics/Cargo.toml +++ b/examples/metrics/Cargo.toml @@ -9,17 +9,17 @@ license = "MIT" release = false [dependencies] -futures = "0.3.30" -hyper = { version = "0.14", features = ["server", "tcp", "http1"] } +futures = { workspace = true } +axum = "0.7" libp2p = { path = "../../libp2p", features = ["tokio", "metrics", "ping", "noise", "identify", "tcp", "yamux", "macros"] } -opentelemetry = { version = "0.20.0", features = ["rt-tokio", "metrics"] } -opentelemetry-otlp = { version = "0.13.0", features = ["metrics"]} -opentelemetry_api = "0.20.0" +opentelemetry = { version = "0.25.0", features = ["metrics"] } +opentelemetry-otlp = { version = "0.25.0", features = ["metrics"] } +opentelemetry_sdk = { version = "0.25.0", features = ["rt-tokio", "metrics"] } prometheus-client = { workspace = true } -tokio = { version = "1", features = ["full"] } -tracing = "0.1.37" -tracing-opentelemetry = "0.21.0" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tokio = { workspace = true, features = ["full"] } +tracing = { workspace = true } +tracing-opentelemetry = "0.26.0" +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/metrics/src/http_service.rs b/examples/metrics/src/http_service.rs index 8c77d724ea3..4a9c9785bb3 100644 --- a/examples/metrics/src/http_service.rs +++ b/examples/metrics/src/http_service.rs @@ -18,109 +18,60 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use hyper::http::StatusCode; -use hyper::service::Service; -use hyper::{Body, Method, Request, Response, Server}; +use axum::extract::State; +use axum::http::StatusCode; +use axum::response::IntoResponse; +use axum::routing::get; +use axum::Router; use prometheus_client::encoding::text::encode; use prometheus_client::registry::Registry; -use std::future::Future; -use std::pin::Pin; +use std::net::SocketAddr; use std::sync::{Arc, Mutex}; -use std::task::{Context, Poll}; +use tokio::net::TcpListener; const METRICS_CONTENT_TYPE: &str = "application/openmetrics-text;charset=utf-8;version=1.0.0"; pub(crate) async fn metrics_server(registry: Registry) -> Result<(), std::io::Error> { // Serve on localhost. - let addr = ([127, 0, 0, 1], 0).into(); - - let server = Server::bind(&addr).serve(MakeMetricService::new(registry)); - tracing::info!(metrics_server=%format!("http://{}/metrics", server.local_addr())); - if let Err(e) = server.await { - tracing::error!("server error: {}", e); - } + let addr: SocketAddr = ([127, 0, 0, 1], 0).into(); + let service = MetricService::new(registry); + let server = Router::new() + .route("/metrics", get(respond_with_metrics)) + .with_state(service); + let tcp_listener = TcpListener::bind(addr).await?; + let local_addr = tcp_listener.local_addr()?; + tracing::info!(metrics_server=%format!("http://{}/metrics", local_addr)); + axum::serve(tcp_listener, server.into_make_service()).await?; Ok(()) } +#[derive(Clone)] pub(crate) struct MetricService { reg: Arc>, } -type SharedRegistry = Arc>; - -impl MetricService { - fn get_reg(&mut self) -> SharedRegistry { - Arc::clone(&self.reg) - } - fn respond_with_metrics(&mut self) -> Response { - let mut response: Response = Response::default(); - - response.headers_mut().insert( - hyper::header::CONTENT_TYPE, - METRICS_CONTENT_TYPE.try_into().unwrap(), - ); - - let reg = self.get_reg(); - encode(&mut response.body_mut(), ®.lock().unwrap()).unwrap(); - - *response.status_mut() = StatusCode::OK; - - response - } - fn respond_with_404_not_found(&mut self) -> Response { - Response::builder() - .status(StatusCode::NOT_FOUND) - .body("Not found try localhost:[port]/metrics".to_string()) - .unwrap() - } -} - -impl Service> for MetricService { - type Response = Response; - type Error = hyper::Error; - type Future = Pin> + Send>>; - - fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { - Poll::Ready(Ok(())) - } +async fn respond_with_metrics(state: State) -> impl IntoResponse { + let mut sink = String::new(); + let reg = state.get_reg(); + encode(&mut sink, ®.lock().unwrap()).unwrap(); - fn call(&mut self, req: Request) -> Self::Future { - let req_path = req.uri().path(); - let req_method = req.method(); - let resp = if (req_method == Method::GET) && (req_path == "/metrics") { - // Encode and serve metrics from registry. - self.respond_with_metrics() - } else { - self.respond_with_404_not_found() - }; - Box::pin(async { Ok(resp) }) - } + ( + StatusCode::OK, + [(axum::http::header::CONTENT_TYPE, METRICS_CONTENT_TYPE)], + sink, + ) } -pub(crate) struct MakeMetricService { - reg: SharedRegistry, -} +type SharedRegistry = Arc>; -impl MakeMetricService { - pub(crate) fn new(registry: Registry) -> MakeMetricService { - MakeMetricService { +impl MetricService { + fn new(registry: Registry) -> Self { + Self { reg: Arc::new(Mutex::new(registry)), } } -} - -impl Service for MakeMetricService { - type Response = MetricService; - type Error = hyper::Error; - type Future = Pin> + Send>>; - - fn poll_ready(&mut self, _: &mut Context) -> Poll> { - Poll::Ready(Ok(())) - } - fn call(&mut self, _: T) -> Self::Future { - let reg = self.reg.clone(); - let fut = async move { Ok(MetricService { reg }) }; - Box::pin(fut) + fn get_reg(&self) -> SharedRegistry { + Arc::clone(&self.reg) } } diff --git a/examples/metrics/src/main.rs b/examples/metrics/src/main.rs index 3ab6815cb32..1755c769053 100644 --- a/examples/metrics/src/main.rs +++ b/examples/metrics/src/main.rs @@ -25,8 +25,7 @@ use libp2p::core::Multiaddr; use libp2p::metrics::{Metrics, Recorder}; use libp2p::swarm::{NetworkBehaviour, SwarmEvent}; use libp2p::{identify, identity, noise, ping, tcp, yamux}; -use opentelemetry::sdk; -use opentelemetry_api::KeyValue; +use opentelemetry::{trace::TracerProvider, KeyValue}; use prometheus_client::registry::Registry; use std::error::Error; use std::time::Duration; @@ -67,6 +66,13 @@ async fn main() -> Result<(), Box> { loop { match swarm.select_next_some().await { + SwarmEvent::NewListenAddr { address, .. } => { + tracing::info!( + "Local node is listening on\n {}/p2p/{}", + address, + swarm.local_peer_id() + ); + } SwarmEvent::Behaviour(BehaviourEvent::Ping(ping_event)) => { tracing::info!(?ping_event); metrics.record(&ping_event); @@ -84,25 +90,22 @@ async fn main() -> Result<(), Box> { } fn setup_tracing() -> Result<(), Box> { - let tracer = opentelemetry_otlp::new_pipeline() + let provider = opentelemetry_otlp::new_pipeline() .tracing() .with_exporter(opentelemetry_otlp::new_exporter().tonic()) - .with_trace_config( - sdk::trace::Config::default().with_resource(sdk::Resource::new(vec![KeyValue::new( - "service.name", - "libp2p", - )])), - ) - .install_batch(opentelemetry::runtime::Tokio)?; + .with_trace_config(opentelemetry_sdk::trace::Config::default().with_resource( + opentelemetry_sdk::Resource::new(vec![KeyValue::new("service.name", "libp2p")]), + )) + .install_batch(opentelemetry_sdk::runtime::Tokio)?; tracing_subscriber::registry() .with(tracing_subscriber::fmt::layer().with_filter(EnvFilter::from_default_env())) .with( tracing_opentelemetry::layer() - .with_tracer(tracer) + .with_tracer(provider.tracer("libp2p-subscriber")) .with_filter(EnvFilter::from_default_env()), ) - .try_init()?; + .init(); Ok(()) } diff --git a/examples/ping/Cargo.toml b/examples/ping/Cargo.toml index db47e4e2d8e..633f043de56 100644 --- a/examples/ping/Cargo.toml +++ b/examples/ping/Cargo.toml @@ -9,11 +9,11 @@ license = "MIT" release = false [dependencies] -futures = "0.3.30" +futures = { workspace = true } libp2p = { path = "../../libp2p", features = ["noise", "ping", "tcp", "tokio", "yamux"] } -tokio = { version = "1.36.0", features = ["full"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tokio = { workspace = true, features = ["full"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/relay-server/Cargo.toml b/examples/relay-server/Cargo.toml index 65c7c707087..7385cf6c033 100644 --- a/examples/relay-server/Cargo.toml +++ b/examples/relay-server/Cargo.toml @@ -9,13 +9,12 @@ license = "MIT" release = false [dependencies] -clap = { version = "4.4.16", features = ["derive"] } -async-std = { version = "1.12", features = ["attributes"] } -async-trait = "0.1" -futures = "0.3.30" -libp2p = { path = "../../libp2p", features = [ "async-std", "noise", "macros", "ping", "tcp", "identify", "yamux", "relay", "quic"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +clap = { version = "4.5.6", features = ["derive"] } +tokio = { version = "1.37.0", features = ["full"] } +futures = { workspace = true } +libp2p = { path = "../../libp2p", features = ["tokio", "noise", "macros", "ping", "tcp", "identify", "yamux", "relay", "quic"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/relay-server/README.md b/examples/relay-server/README.md index ae2ab3fdfd8..cd8f0340a5a 100644 --- a/examples/relay-server/README.md +++ b/examples/relay-server/README.md @@ -16,7 +16,7 @@ To run the example, follow these steps: Replace `` with a seed value used to generate a deterministic peer ID for the relay node. 2. The relay node will start listening for incoming connections. -It will print the listening address once it is ready. + It will print the listening address once it is ready. 3. Connect other **libp2p** nodes to the relay node by specifying the relay's listening address as one of the bootstrap nodes in their configuration. diff --git a/examples/relay-server/src/main.rs b/examples/relay-server/src/main.rs index bf5817454f8..46a122d0717 100644 --- a/examples/relay-server/src/main.rs +++ b/examples/relay-server/src/main.rs @@ -22,8 +22,7 @@ #![doc = include_str!("../README.md")] use clap::Parser; -use futures::executor::block_on; -use futures::stream::StreamExt; +use futures::StreamExt; use libp2p::{ core::multiaddr::Protocol, core::Multiaddr, @@ -35,7 +34,8 @@ use std::error::Error; use std::net::{Ipv4Addr, Ipv6Addr}; use tracing_subscriber::EnvFilter; -fn main() -> Result<(), Box> { +#[tokio::main] +async fn main() -> Result<(), Box> { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) .try_init(); @@ -46,7 +46,7 @@ fn main() -> Result<(), Box> { let local_key: identity::Keypair = generate_ed25519(opt.secret_key_seed); let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key) - .with_async_std() + .with_tokio() .with_tcp( tcp::Config::default(), noise::Config::new, @@ -81,27 +81,25 @@ fn main() -> Result<(), Box> { .with(Protocol::QuicV1); swarm.listen_on(listen_addr_quic)?; - block_on(async { - loop { - match swarm.next().await.expect("Infinite Stream.") { - SwarmEvent::Behaviour(event) => { - if let BehaviourEvent::Identify(identify::Event::Received { - info: identify::Info { observed_addr, .. }, - .. - }) = &event - { - swarm.add_external_address(observed_addr.clone()); - } - - println!("{event:?}") + loop { + match swarm.next().await.expect("Infinite Stream.") { + SwarmEvent::Behaviour(event) => { + if let BehaviourEvent::Identify(identify::Event::Received { + info: identify::Info { observed_addr, .. }, + .. + }) = &event + { + swarm.add_external_address(observed_addr.clone()); } - SwarmEvent::NewListenAddr { address, .. } => { - println!("Listening on {address:?}"); - } - _ => {} + + println!("{event:?}") + } + SwarmEvent::NewListenAddr { address, .. } => { + println!("Listening on {address:?}"); } + _ => {} } - }) + } } #[derive(NetworkBehaviour)] diff --git a/examples/rendezvous/Cargo.toml b/examples/rendezvous/Cargo.toml index edd5b8031a4..4eea38616f4 100644 --- a/examples/rendezvous/Cargo.toml +++ b/examples/rendezvous/Cargo.toml @@ -9,13 +9,11 @@ license = "MIT" release = false [dependencies] -async-std = { version = "1.12", features = ["attributes"] } -async-trait = "0.1" -futures = "0.3.30" -libp2p = { path = "../../libp2p", features = [ "async-std", "identify", "macros", "noise", "ping", "rendezvous", "tcp", "tokio", "yamux"] } -tokio = { version = "1.36", features = ["rt-multi-thread", "macros", "time"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +futures = { workspace = true } +libp2p = { path = "../../libp2p", features = ["identify", "macros", "noise", "ping", "rendezvous", "tcp", "tokio", "yamux"] } +tokio = { workspace = true, features = ["rt-multi-thread", "macros", "time"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/rendezvous/src/bin/rzv-identify.rs b/examples/rendezvous/src/bin/rzv-identify.rs index 1d545592829..ff637aa6f49 100644 --- a/examples/rendezvous/src/bin/rzv-identify.rs +++ b/examples/rendezvous/src/bin/rzv-identify.rs @@ -76,8 +76,12 @@ async fn main() { } // once `/identify` did its job, we know our external address and can register SwarmEvent::Behaviour(MyBehaviourEvent::Identify(identify::Event::Received { + info, .. })) => { + // Register our external address. Needs to be done explicitly + // for this case, as it's a local address. + swarm.add_external_address(info.observed_addr); if let Err(error) = swarm.behaviour_mut().rendezvous.register( rendezvous::Namespace::from_static("rendezvous"), rendezvous_point, diff --git a/examples/stream/Cargo.toml b/examples/stream/Cargo.toml index 37f84e6ed62..ba31d4d9e13 100644 --- a/examples/stream/Cargo.toml +++ b/examples/stream/Cargo.toml @@ -10,13 +10,13 @@ release = false [dependencies] anyhow = "1" -futures = "0.3.29" +futures = { workspace = true } libp2p = { path = "../../libp2p", features = [ "tokio", "quic"] } -libp2p-stream = { path = "../../protocols/stream", version = "0.1.0-alpha" } +libp2p-stream = { path = "../../protocols/stream", version = "0.2.0-alpha" } rand = "0.8" -tokio = { version = "1.36", features = ["full"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tokio = { workspace = true, features = ["full"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/examples/stream/README.md b/examples/stream/README.md index 8437a5ea21e..3b0325519f7 100644 --- a/examples/stream/README.md +++ b/examples/stream/README.md @@ -23,7 +23,7 @@ To run the example, follow these steps: cargo run --bin stream-example --

``` -3. Both terminals should now continuosly print messages. +3. Both terminals should now continuously print messages. ## Conclusion diff --git a/examples/upnp/Cargo.toml b/examples/upnp/Cargo.toml index db9825c8742..ae292e0a48d 100644 --- a/examples/upnp/Cargo.toml +++ b/examples/upnp/Cargo.toml @@ -9,10 +9,10 @@ license = "MIT" release = false [dependencies] -tokio = { version = "1", features = ["rt-multi-thread", "macros"] } -futures = "0.3.30" +tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } +futures = { workspace = true } libp2p = { path = "../../libp2p", features = ["tokio", "dns", "macros", "noise", "ping", "tcp", "yamux", "upnp"] } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/hole-punching-tests/Cargo.toml b/hole-punching-tests/Cargo.toml index c8e6cde98e0..79728f9535c 100644 --- a/hole-punching-tests/Cargo.toml +++ b/hole-punching-tests/Cargo.toml @@ -8,11 +8,11 @@ license = "MIT" [dependencies] anyhow = "1" env_logger = "0.10.2" -futures = "0.3.30" +futures = { workspace = true } libp2p = { path = "../libp2p", features = ["tokio", "dcutr", "identify", "macros", "noise", "ping", "relay", "tcp", "yamux", "quic"] } -tracing = "0.1.37" -redis = { version = "0.23.0", default-features = false, features = ["tokio-comp"] } -tokio = { version = "1.36.0", features = ["full"] } -serde = { version = "1.0.197", features = ["derive"] } -serde_json = "1.0.114" -either = "1.9.0" +tracing = { workspace = true } +redis = { version = "0.24.0", default-features = false, features = ["tokio-comp"] } +tokio = { workspace = true, features = ["full"] } +serde = { version = "1.0.203", features = ["derive"] } +serde_json = "1.0.117" +either = "1.12.0" diff --git a/hole-punching-tests/Dockerfile b/hole-punching-tests/Dockerfile index 864f058799e..403cc301fc6 100644 --- a/hole-punching-tests/Dockerfile +++ b/hole-punching-tests/Dockerfile @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM rust:1.73.0 as builder +FROM rust:1.81.0 as builder # Run with access to the target cache to speed up builds WORKDIR /workspace diff --git a/hole-punching-tests/src/main.rs b/hole-punching-tests/src/main.rs index 4f81cd65480..02229e16262 100644 --- a/hole-punching-tests/src/main.rs +++ b/hole-punching-tests/src/main.rs @@ -64,7 +64,7 @@ async fn main() -> Result<()> { let mut swarm = libp2p::SwarmBuilder::with_new_identity() .with_tokio() .with_tcp( - tcp::Config::new().port_reuse(true).nodelay(true), + tcp::Config::new().nodelay(true), noise::Config::new, yamux::Config::default, )? @@ -294,9 +294,7 @@ impl RedisClient { tracing::debug!("Pushing {key}={value} to redis"); - self.inner.rpush(key, value).await?; - - Ok(()) + self.inner.rpush(key, value).await.map_err(Into::into) } async fn pop(&mut self, key: &str) -> Result @@ -308,7 +306,7 @@ impl RedisClient { let value = self .inner - .blpop::<_, HashMap>(key, 0) + .blpop::<_, HashMap>(key, 0.0) .await? .remove(key) .with_context(|| format!("Failed to get value for {key} from redis"))? diff --git a/identity/CHANGELOG.md b/identity/CHANGELOG.md index 004943ce195..9670a843130 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.2.9 + +- Add `rand` feature gate to ecdsa methods requiring a random number generator. + See [PR 5212](https://github.com/libp2p/rust-libp2p/pull/5212). + ## 0.2.8 - Bump `ring` to `0.17.5. diff --git a/identity/Cargo.toml b/identity/Cargo.toml index 920b9a990e4..cb0b8cb000e 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-identity" -version = "0.2.8" +version = "0.2.9" edition = "2021" description = "Data structures and algorithms for identifying peers in libp2p." rust-version = "1.73.0" # MUST NOT inherit from workspace because we don't want to publish breaking changes to `libp2p-identity`. @@ -13,13 +13,13 @@ categories = ["cryptography"] [dependencies] asn1_der = { version = "0.7.6", optional = true } -bs58 = { version = "0.5.0", optional = true } +bs58 = { version = "0.5.1", optional = true } ed25519-dalek = { version = "2.1", optional = true } hkdf = { version = "0.12.4", optional = true } libsecp256k1 = { version = "0.7.0", optional = true } -tracing = "0.1.37" +tracing = { workspace = true } multihash = { version = "0.19.1", optional = true } -p256 = { version = "0.13", default-features = false, features = [ "ecdsa", "std", "pem"], optional = true } +p256 = { version = "0.13", default-features = false, features = ["ecdsa", "std", "pem"], optional = true } quick-protobuf = "0.8.1" rand = { version = "0.8", optional = true } sec1 = { version = "0.7", default-features = false, optional = true } @@ -27,10 +27,10 @@ serde = { version = "1", optional = true, features = ["derive"] } sha2 = { version = "0.10.8", optional = true } thiserror = { version = "1.0", optional = true } void = { version = "1.0", optional = true } -zeroize = { version = "1.7", optional = true } +zeroize = { version = "1.8", optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -ring = { version = "0.17.5", features = [ "alloc", "std"], default-features = false, optional = true } +ring = { workspace = true, features = ["alloc", "std"], optional = true } [features] secp256k1 = ["dep:libsecp256k1", "dep:asn1_der", "dep:sha2", "dep:hkdf", "dep:zeroize"] @@ -42,9 +42,9 @@ rand = ["dep:rand", "ed25519-dalek?/rand_core"] [dev-dependencies] quickcheck = { workspace = true } -base64 = "0.21.7" +base64 = "0.22.1" serde_json = "1.0" -rmp-serde = "1.1" +rmp-serde = "1.3" criterion = "0.5" hex-literal = "0.4.1" @@ -56,8 +56,6 @@ harness = false # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/identity/src/ecdsa.rs b/identity/src/ecdsa.rs index 2f1a286d46d..65cbe885b86 100644 --- a/identity/src/ecdsa.rs +++ b/identity/src/ecdsa.rs @@ -94,6 +94,7 @@ pub struct SecretKey(SigningKey); impl SecretKey { /// Generate a new random ECDSA secret key. + #[cfg(feature = "rand")] pub fn generate() -> SecretKey { SecretKey(SigningKey::random(&mut rand::thread_rng())) } diff --git a/identity/src/ed25519.rs b/identity/src/ed25519.rs index 529a4dddea1..d77c44547d6 100644 --- a/identity/src/ed25519.rs +++ b/identity/src/ed25519.rs @@ -25,7 +25,6 @@ use core::cmp; use core::fmt; use core::hash; use ed25519_dalek::{self as ed25519, Signer as _, Verifier as _}; -use std::convert::TryFrom; use zeroize::Zeroize; /// An Ed25519 keypair. diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index bdfb68c0091..f1e8a7c2142 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -24,32 +24,30 @@ feature = "ed25519", feature = "rsa" ))] -use crate::error::OtherVariantError; -use crate::error::{DecodingError, SigningError}; +#[cfg(feature = "ed25519")] +use crate::ed25519; #[cfg(any( feature = "ecdsa", feature = "secp256k1", feature = "ed25519", feature = "rsa" ))] -use crate::proto; +use crate::error::OtherVariantError; +use crate::error::{DecodingError, SigningError}; #[cfg(any( feature = "ecdsa", feature = "secp256k1", feature = "ed25519", feature = "rsa" ))] -use quick_protobuf::{BytesReader, Writer}; +use crate::proto; #[cfg(any( feature = "ecdsa", feature = "secp256k1", feature = "ed25519", feature = "rsa" ))] -use std::convert::TryFrom; - -#[cfg(feature = "ed25519")] -use crate::ed25519; +use quick_protobuf::{BytesReader, Writer}; #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] use crate::rsa; diff --git a/identity/src/lib.rs b/identity/src/lib.rs index c78e68d1652..4f4313e8f17 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -34,7 +34,7 @@ //! All key types have functions to enable conversion to/from their binary representations. #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] - +#![allow(unreachable_pub)] #[cfg(any( feature = "ecdsa", feature = "secp256k1", @@ -42,7 +42,6 @@ feature = "rsa" ))] mod proto { - #![allow(unreachable_pub)] include!("generated/mod.rs"); pub(crate) use self::keys_proto::*; } diff --git a/identity/src/peer_id.rs b/identity/src/peer_id.rs index 1d85fe66ffa..7b3f799f612 100644 --- a/identity/src/peer_id.rs +++ b/identity/src/peer_id.rs @@ -21,7 +21,7 @@ #[cfg(feature = "rand")] use rand::Rng; use sha2::Digest as _; -use std::{convert::TryFrom, fmt, str::FromStr}; +use std::{fmt, str::FromStr}; use thiserror::Error; /// Local type-alias for multihash. diff --git a/identity/src/secp256k1.rs b/identity/src/secp256k1.rs index 5e1fda2933b..a6e9e923268 100644 --- a/identity/src/secp256k1.rs +++ b/identity/src/secp256k1.rs @@ -214,7 +214,7 @@ impl PublicKey { self.0.serialize() } - /// Decode a public key from a byte slice in the the format produced + /// Decode a public key from a byte slice in the format produced /// by `encode`. pub fn try_from_bytes(k: &[u8]) -> Result { libsecp256k1::PublicKey::parse_slice(k, Some(libsecp256k1::PublicKeyFormat::Compressed)) diff --git a/interop-tests/Cargo.toml b/interop-tests/Cargo.toml index 410d3dbce2b..0eb32bb4975 100644 --- a/interop-tests/Cargo.toml +++ b/interop-tests/Cargo.toml @@ -13,12 +13,12 @@ crate-type = ["cdylib", "rlib"] [dependencies] anyhow = "1" -either = "1.9.0" -futures = "0.3.30" +either = "1.11.0" +futures = { workspace = true } rand = "0.8.5" serde = { version = "1", features = ["derive"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] axum = "0.7" @@ -28,16 +28,16 @@ libp2p-noise = { workspace = true } libp2p-tls = { workspace = true } libp2p-webrtc = { workspace = true, features = ["tokio"] } mime_guess = "2.0" -redis = { version = "0.23.3", default-features = false, features = [ +redis = { version = "0.24.0", default-features = false, features = [ "tokio-comp", ] } -rust-embed = "8.3" +rust-embed = "8.4" serde_json = "1" -thirtyfour = "=0.32.0-rc.10" # https://github.com/stevepryde/thirtyfour/issues/169 -tokio = { version = "1.36.0", features = ["full"] } +thirtyfour = "=0.32.0" # https://github.com/stevepryde/thirtyfour/issues/169 +tokio = { workspace = true, features = ["full"] } tower-http = { version = "0.5", features = ["cors", "fs", "trace"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [target.'cfg(target_arch = "wasm32")'.dependencies] libp2p = { path = "../libp2p", features = [ "ping", "macros", "webtransport-websys", "wasm-bindgen", "identify", "websocket-websys", "yamux", "noise"] } @@ -46,8 +46,8 @@ libp2p-webrtc-websys = { workspace = true } wasm-bindgen = { version = "0.2" } wasm-bindgen-futures = { version = "0.4" } wasm-logger = { version = "0.2.0" } -instant = "0.1.12" -reqwest = { version = "0.11", features = ["json"] } +web-time = { workspace = true } +reqwest = { version = "0.12", features = ["json"] } console_error_panic_hook = { version = "0.1.7" } futures-timer = "3.0.3" diff --git a/interop-tests/Dockerfile.chromium b/interop-tests/Dockerfile.chromium index 5ec46e313aa..86edbc5b9d2 100644 --- a/interop-tests/Dockerfile.chromium +++ b/interop-tests/Dockerfile.chromium @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM rust:1.73.0 as chef +FROM rust:1.81 as chef RUN rustup target add wasm32-unknown-unknown RUN wget -q -O- https://github.com/rustwasm/wasm-pack/releases/download/v0.12.1/wasm-pack-v0.12.1-x86_64-unknown-linux-musl.tar.gz | tar -zx -C /usr/local/bin --strip-components 1 --wildcards "wasm-pack-*/wasm-pack" RUN wget -q -O- https://github.com/WebAssembly/binaryen/releases/download/version_115/binaryen-version_115-x86_64-linux.tar.gz | tar -zx -C /usr/local/bin --strip-components 2 --wildcards "binaryen-version_*/bin/wasm-opt" @@ -20,7 +20,7 @@ COPY . . RUN wasm-pack build --target web interop-tests RUN RUSTFLAGS='-C target-feature=+crt-static' cargo build --release --package interop-tests --target x86_64-unknown-linux-gnu --bin wasm_ping -FROM selenium/standalone-chrome:115.0 +FROM selenium/standalone-chrome:125.0 COPY --from=builder /app/target/x86_64-unknown-linux-gnu/release/wasm_ping /usr/local/bin/testplan ENV RUST_BACKTRACE=1 ENTRYPOINT ["testplan"] diff --git a/interop-tests/Dockerfile.native b/interop-tests/Dockerfile.native index 91e6cf8893e..499c73437fc 100644 --- a/interop-tests/Dockerfile.native +++ b/interop-tests/Dockerfile.native @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM lukemathwalker/cargo-chef:0.1.62-rust-1.73.0 as chef +FROM lukemathwalker/cargo-chef:0.1.67-rust-bullseye as chef WORKDIR /app FROM chef AS planner diff --git a/interop-tests/src/arch.rs b/interop-tests/src/arch.rs index 52000f90a86..df36f8e5baf 100644 --- a/interop-tests/src/arch.rs +++ b/interop-tests/src/arch.rs @@ -174,13 +174,12 @@ pub(crate) mod native { pub(crate) async fn blpop(&self, key: &str, timeout: u64) -> Result> { let mut conn = self.0.get_async_connection().await?; - Ok(conn.blpop(key, timeout as usize).await?) + Ok(conn.blpop(key, timeout as f64).await?) } pub(crate) async fn rpush(&self, key: &str, value: String) -> Result<()> { let mut conn = self.0.get_async_connection().await?; - conn.rpush(key, value).await?; - Ok(()) + conn.rpush(key, value).await.map_err(Into::into) } } } @@ -199,7 +198,7 @@ pub(crate) mod wasm { use crate::{BlpopRequest, Muxer, SecProtocol, Transport}; - pub(crate) type Instant = instant::Instant; + pub(crate) type Instant = web_time::Instant; pub(crate) fn init_logger() { console_error_panic_hook::set_once(); @@ -246,7 +245,7 @@ pub(crate) mod wasm { .with_behaviour(behaviour_constructor)? .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), - format!("/ip4/{ip}/tcp/0/wss"), + format!("/ip4/{ip}/tcp/0/tls/ws"), ), (Transport::Ws, Some(SecProtocol::Noise), Some(Muxer::Yamux)) => ( libp2p::SwarmBuilder::with_new_identity() @@ -263,7 +262,7 @@ pub(crate) mod wasm { .with_behaviour(behaviour_constructor)? .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), - format!("/ip4/{ip}/tcp/0/wss"), + format!("/ip4/{ip}/tcp/0/tls/ws"), ), (Transport::WebRtcDirect, None, None) => ( libp2p::SwarmBuilder::with_new_identity() diff --git a/interop-tests/src/bin/wasm_ping.rs b/interop-tests/src/bin/wasm_ping.rs index e1bb2ea49fb..0d697a0e2a3 100644 --- a/interop-tests/src/bin/wasm_ping.rs +++ b/interop-tests/src/bin/wasm_ping.rs @@ -130,6 +130,8 @@ async fn open_in_browser() -> Result<(Child, WebDriver)> { // run a webdriver client let mut caps = DesiredCapabilities::chrome(); caps.set_headless()?; + caps.set_disable_dev_shm_usage()?; + caps.set_no_sandbox()?; let driver = WebDriver::new("http://localhost:45782", caps).await?; // go to the wasm test service driver.goto(format!("http://{BIND_ADDR}")).await?; @@ -149,7 +151,7 @@ async fn redis_blpop( StatusCode::INTERNAL_SERVER_ERROR })?; let res = conn - .blpop(&request.key, request.timeout as usize) + .blpop(&request.key, request.timeout as f64) .await .map_err(|e| { tracing::warn!( diff --git a/libp2p/CHANGELOG.md b/libp2p/CHANGELOG.md index 80b32c35643..72a624786d4 100644 --- a/libp2p/CHANGELOG.md +++ b/libp2p/CHANGELOG.md @@ -1,3 +1,34 @@ +## 0.54.1 + +- Update individual crates. + - Update to [`libp2p-metrics` `0.15.0`](misc/metrics/CHANGELOG.md#0150). + +## 0.54.0 + +- Update individual crates. + - Update to [`libp2p-kad` `v0.46.0`](protocols/kad/CHANGELOG.md#0460). + - Update to [`libp2p-identify` `v0.45.0`](protocols/identify/CHANGELOG.md#0450). + +- Raise MSRV to 1.73. + See [PR 5266](https://github.com/libp2p/rust-libp2p/pull/5266). + +- Implement refactored `Transport`. + See [PR 4568]. + +- Move `address_translation` from `libp2p-core` to `libp2p-swarm` and `libp2p-identify`. To now get + address translation behaviour, i.e. discovery of extern address (candidates) based on connecting + to other peers, one needs to use `libp2p-identify` now. This pertains to you if your nodes can be + behind NATs and they aren't aware of their true external address. + See [PR 4568]. + +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + +- Remove redundant async signature from builder methods. + See [PR 5468](https://github.com/libp2p/rust-libp2p/pull/5468). + +[PR 4568]: https://github.com/libp2p/rust-libp2p/pull/4568 + ## 0.53.2 - Allow `SwarmBuilder::with_bandwidth_metrics` after `SwarmBuilder::with_websocket`. @@ -1185,7 +1216,7 @@ must not be skipped! - Merged `PeriodicPing` and `PingListen` into one `Ping` behaviour. - `Floodsub` now generates `FloodsubEvent`s instead of direct floodsub messages. - Added `ProtocolsHandler::connection_keep_alive`. If all the handlers return `false`, then the connection to the remote node will automatically be gracefully closed after a few seconds. -- The crate now successfuly compiles for the `wasm32-unknown-unknown` target. +- The crate now successfully compiles for the `wasm32-unknown-unknown` target. - Updated `ring` to version 0.13. - Updated `secp256k1` to version 0.12. - The enum returned by `RawSwarm::peer()` can now return `LocalNode`. This makes it impossible to accidentally attempt to dial the local node. diff --git a/libp2p/Cargo.toml b/libp2p/Cargo.toml index 9dc9667be10..b1017f5958c 100644 --- a/libp2p/Cargo.toml +++ b/libp2p/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p" edition = "2021" rust-version = { workspace = true } description = "Peer-to-peer networking library" -version = "0.53.2" +version = "0.54.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -82,7 +82,7 @@ tcp = ["dep:libp2p-tcp"] tls = ["dep:libp2p-tls"] tokio = [ "libp2p-swarm/tokio", "libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio", "libp2p-quic?/tokio", "libp2p-upnp?/tokio"] uds = ["dep:libp2p-uds"] -wasm-bindgen = [ "futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js", "libp2p-swarm/wasm-bindgen", "libp2p-gossipsub?/wasm-bindgen",] +wasm-bindgen = [ "futures-timer/wasm-bindgen", "getrandom/js", "libp2p-swarm/wasm-bindgen", "libp2p-gossipsub?/wasm-bindgen"] websocket-websys = ["dep:libp2p-websocket-websys"] websocket = ["dep:libp2p-websocket"] webtransport-websys = ["dep:libp2p-webtransport-websys"] @@ -92,10 +92,9 @@ upnp = ["dep:libp2p-upnp"] [dependencies] bytes = "1" either = "1.9.0" -futures = "0.3.26" +futures = { workspace = true } futures-timer = "3.0.2" # Explicit dependency to be used in `wasm-bindgen` feature getrandom = "0.2.3" # Explicit dependency to be used in `wasm-bindgen` feature -instant = "0.1.12" # Explicit dependency to be used in `wasm-bindgen` feature # TODO feature flag? rw-stream-sink = { workspace = true } @@ -140,19 +139,17 @@ libp2p-websocket = { workspace = true, optional = true } async-std = { version = "1.6.2", features = ["attributes"] } async-trait = "0.1" clap = { version = "4.1.6", features = ["derive"] } -tokio = { version = "1.15", features = [ "io-util", "io-std", "macros", "rt", "rt-multi-thread"] } +tokio = { workspace = true, features = [ "io-util", "io-std", "macros", "rt", "rt-multi-thread"] } libp2p-mplex = { workspace = true } libp2p-noise = { workspace = true } libp2p-tcp = { workspace = true, features = ["tokio"] } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/libp2p/src/bandwidth.rs b/libp2p/src/bandwidth.rs index b84cbb7e27b..8931c5c4166 100644 --- a/libp2p/src/bandwidth.rs +++ b/libp2p/src/bandwidth.rs @@ -154,7 +154,7 @@ impl AsyncRead for InstrumentedStream { let this = self.project(); let num_bytes = ready!(this.inner.poll_read(cx, buf))?; this.sinks.inbound.fetch_add( - u64::try_from(num_bytes).unwrap_or(u64::max_value()), + u64::try_from(num_bytes).unwrap_or(u64::MAX), Ordering::Relaxed, ); Poll::Ready(Ok(num_bytes)) @@ -168,7 +168,7 @@ impl AsyncRead for InstrumentedStream { let this = self.project(); let num_bytes = ready!(this.inner.poll_read_vectored(cx, bufs))?; this.sinks.inbound.fetch_add( - u64::try_from(num_bytes).unwrap_or(u64::max_value()), + u64::try_from(num_bytes).unwrap_or(u64::MAX), Ordering::Relaxed, ); Poll::Ready(Ok(num_bytes)) @@ -184,7 +184,7 @@ impl AsyncWrite for InstrumentedStream { let this = self.project(); let num_bytes = ready!(this.inner.poll_write(cx, buf))?; this.sinks.outbound.fetch_add( - u64::try_from(num_bytes).unwrap_or(u64::max_value()), + u64::try_from(num_bytes).unwrap_or(u64::MAX), Ordering::Relaxed, ); Poll::Ready(Ok(num_bytes)) @@ -198,7 +198,7 @@ impl AsyncWrite for InstrumentedStream { let this = self.project(); let num_bytes = ready!(this.inner.poll_write_vectored(cx, bufs))?; this.sinks.outbound.fetch_add( - u64::try_from(num_bytes).unwrap_or(u64::max_value()), + u64::try_from(num_bytes).unwrap_or(u64::MAX), Ordering::Relaxed, ); Poll::Ready(Ok(num_bytes)) diff --git a/libp2p/src/builder.rs b/libp2p/src/builder.rs index c96c20d470a..de003314cca 100644 --- a/libp2p/src/builder.rs +++ b/libp2p/src/builder.rs @@ -575,7 +575,7 @@ mod tests { #[test] #[cfg(all(feature = "tokio", feature = "quic"))] - fn quic_bandwidth_metrics() -> Result<(), Box> { + fn quic_bandwidth_metrics() { let _ = SwarmBuilder::with_new_identity() .with_tokio() .with_quic() @@ -583,8 +583,6 @@ mod tests { .with_behaviour(|_| libp2p_swarm::dummy::Behaviour) .unwrap() .build(); - - Ok(()) } #[test] diff --git a/libp2p/src/builder/phase/dns.rs b/libp2p/src/builder/phase/dns.rs index 135f6c57b19..638064d58bb 100644 --- a/libp2p/src/builder/phase/dns.rs +++ b/libp2p/src/builder/phase/dns.rs @@ -8,8 +8,7 @@ pub struct DnsPhase { #[cfg(all(not(target_arch = "wasm32"), feature = "async-std", feature = "dns"))] impl SwarmBuilder> { - // TODO: Remove `async` - pub async fn with_dns( + pub fn with_dns( self, ) -> Result< SwarmBuilder< diff --git a/libp2p/src/builder/phase/other_transport.rs b/libp2p/src/builder/phase/other_transport.rs index b0d56cd92d2..e04621b2e3f 100644 --- a/libp2p/src/builder/phase/other_transport.rs +++ b/libp2p/src/builder/phase/other_transport.rs @@ -73,7 +73,7 @@ impl impl SwarmBuilder> { - pub async fn with_dns( + pub fn with_dns( self, ) -> Result< SwarmBuilder< @@ -82,7 +82,7 @@ impl >, std::io::Error, > { - self.without_any_other_transports().with_dns().await + self.without_any_other_transports().with_dns() } } #[cfg(all(not(target_arch = "wasm32"), feature = "tokio", feature = "dns"))] diff --git a/libp2p/src/builder/phase/provider.rs b/libp2p/src/builder/phase/provider.rs index 32321442689..2a9154cda74 100644 --- a/libp2p/src/builder/phase/provider.rs +++ b/libp2p/src/builder/phase/provider.rs @@ -1,46 +1,60 @@ #[allow(unused_imports)] use super::*; - use crate::SwarmBuilder; +use std::marker::PhantomData; +/// Represents the phase where a provider is not yet specified. +/// This is a marker type used in the type-state pattern to ensure compile-time checks of the builder's state. +pub enum NoProviderSpecified {} + +// Define enums for each of the possible runtime environments. These are used as markers in the type-state pattern, +// allowing compile-time checks for the appropriate environment configuration. + +#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))] +/// Represents the AsyncStd runtime environment. +pub enum AsyncStd {} + +#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))] +/// Represents the Tokio runtime environment. +pub enum Tokio {} + +#[cfg(feature = "wasm-bindgen")] +/// Represents the WasmBindgen environment for WebAssembly. +pub enum WasmBindgen {} +/// Represents a phase in the SwarmBuilder where a provider has been chosen but not yet specified. pub struct ProviderPhase {} impl SwarmBuilder { + /// Configures the SwarmBuilder to use the AsyncStd runtime. + /// This method is only available when compiling for non-Wasm targets with the `async-std` feature enabled. #[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))] pub fn with_async_std(self) -> SwarmBuilder { SwarmBuilder { keypair: self.keypair, - phantom: std::marker::PhantomData, + phantom: PhantomData, phase: TcpPhase {}, } } + /// Configures the SwarmBuilder to use the Tokio runtime. + /// This method is only available when compiling for non-Wasm targets with the `tokio` feature enabled #[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))] pub fn with_tokio(self) -> SwarmBuilder { SwarmBuilder { keypair: self.keypair, - phantom: std::marker::PhantomData, + phantom: PhantomData, phase: TcpPhase {}, } } + /// Configures the SwarmBuilder for WebAssembly using WasmBindgen. + /// This method is available when the `wasm-bindgen` feature is enabled. #[cfg(feature = "wasm-bindgen")] pub fn with_wasm_bindgen(self) -> SwarmBuilder { SwarmBuilder { keypair: self.keypair, - phantom: std::marker::PhantomData, + phantom: PhantomData, phase: TcpPhase {}, } } } - -pub enum NoProviderSpecified {} - -#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))] -pub enum AsyncStd {} - -#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))] -pub enum Tokio {} - -#[cfg(feature = "wasm-bindgen")] -pub enum WasmBindgen {} diff --git a/libp2p/src/builder/phase/quic.rs b/libp2p/src/builder/phase/quic.rs index 885b16e2e03..e030e9493bb 100644 --- a/libp2p/src/builder/phase/quic.rs +++ b/libp2p/src/builder/phase/quic.rs @@ -150,7 +150,7 @@ impl SwarmBuilder SwarmBuilder> { - pub async fn with_dns( + pub fn with_dns( self, ) -> Result< SwarmBuilder< @@ -162,7 +162,6 @@ impl SwarmBuilder Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity(); //! @@ -89,7 +91,7 @@ //! ## Transport //! //! Next up we need to construct a transport. Each transport in libp2p provides encrypted streams. -//! E.g. combining TCP to establish connections, TLS to encrypt these connections and Yamux to run +//! E.g. combining TCP to establish connections, NOISE to encrypt these connections and Yamux to run //! one or more streams on a connection. Another libp2p transport is QUIC, providing encrypted //! streams out-of-the-box. We will stick to TCP for now. Each of these implement the [`Transport`] //! trait. @@ -97,17 +99,20 @@ //! ```rust //! use std::error::Error; //! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, tcp, yamux}; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )?; //! //! Ok(()) @@ -137,20 +142,22 @@ //! With the above in mind, let's extend our example, creating a [`ping::Behaviour`](crate::ping::Behaviour) at the end: //! //! ```rust -//! use libp2p::ping; -//! use tracing_subscriber::EnvFilter; //! use std::error::Error; +//! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, ping, tcp, yamux}; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())?; //! @@ -166,20 +173,22 @@ //! to the [`Transport`] as well as events from the [`Transport`] to the [`NetworkBehaviour`]. //! //! ```rust -//! use libp2p::ping; //! use std::error::Error; //! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, ping, tcp, yamux}; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? //! .build(); @@ -199,24 +208,25 @@ //! Thus, without any other behaviour in place, we would not be able to observe the pings. //! //! ```rust -//! use libp2p::ping; -//! use std::error::Error; -//! use std::time::Duration; +//! use std::{error::Error, time::Duration}; //! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, ping, tcp, yamux}; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? -//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) // Allows us to observe pings indefinitely. +//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) //! .build(); //! //! Ok(()) @@ -250,24 +260,25 @@ //! remote peer. //! //! ```rust -//! use libp2p::{ping, Multiaddr}; -//! use std::error::Error; -//! use std::time::Duration; +//! use std::{error::Error, time::Duration}; //! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, ping, tcp, yamux, Multiaddr}; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? -//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) // Allows us to observe pings indefinitely.. +//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) //! .build(); //! //! // Tell the swarm to listen on all interfaces and a random, OS-assigned @@ -293,26 +304,26 @@ //! outgoing connection in case we specify an address on the CLI. //! //! ```no_run -//! use futures::prelude::*; -//! use libp2p::swarm::SwarmEvent; -//! use libp2p::{ping, Multiaddr}; -//! use std::error::Error; -//! use std::time::Duration; +//! use std::{error::Error, time::Duration}; //! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, ping, tcp, yamux, Multiaddr, swarm::SwarmEvent}; +//! use futures::prelude::*; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? -//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) // Allows us to observe pings indefinitely. +//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) //! .build(); //! //! // Tell the swarm to listen on all interfaces and a random, OS-assigned diff --git a/misc/allow-block-list/CHANGELOG.md b/misc/allow-block-list/CHANGELOG.md index 7778e924886..3cda0603ee4 100644 --- a/misc/allow-block-list/CHANGELOG.md +++ b/misc/allow-block-list/CHANGELOG.md @@ -1,3 +1,13 @@ +## 0.4.1 + +- Add getters & setters for the allowed/blocked peers. + Return a `bool` for every "insert/remove" function, informing if a change was performed. + See [PR 5572](https://github.com/libp2p/rust-libp2p/pull/5572). + +## 0.4.0 + + + ## 0.3.0 diff --git a/misc/allow-block-list/Cargo.toml b/misc/allow-block-list/Cargo.toml index c620e7f4a2b..1ff0ccff906 100644 --- a/misc/allow-block-list/Cargo.toml +++ b/misc/allow-block-list/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-allow-block-list" edition = "2021" rust-version = { workspace = true } description = "Allow/block list connection management for libp2p." -version = "0.3.0" +version = "0.4.1" license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" keywords = ["peer-to-peer", "libp2p", "networking"] diff --git a/misc/allow-block-list/src/lib.rs b/misc/allow-block-list/src/lib.rs index c1d31433db1..56de29d1985 100644 --- a/misc/allow-block-list/src/lib.rs +++ b/misc/allow-block-list/src/lib.rs @@ -61,6 +61,7 @@ //! # } //! ``` +use libp2p_core::transport::PortUse; use libp2p_core::{Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::{ @@ -93,44 +94,74 @@ pub struct BlockedPeers { } impl Behaviour { + /// Peers that are currently allowed. + pub fn allowed_peers(&self) -> &HashSet { + &self.state.peers + } + /// Allow connections to the given peer. - pub fn allow_peer(&mut self, peer: PeerId) { - self.state.peers.insert(peer); - if let Some(waker) = self.waker.take() { - waker.wake() + /// + /// Returns whether the peer was newly inserted. Does nothing if the peer was already present in the set. + pub fn allow_peer(&mut self, peer: PeerId) -> bool { + let inserted = self.state.peers.insert(peer); + if inserted { + if let Some(waker) = self.waker.take() { + waker.wake() + } } + inserted } /// Disallow connections to the given peer. /// /// All active connections to this peer will be closed immediately. - pub fn disallow_peer(&mut self, peer: PeerId) { - self.state.peers.remove(&peer); - self.close_connections.push_back(peer); - if let Some(waker) = self.waker.take() { - waker.wake() + /// + /// Returns whether the peer was present in the set. Does nothing if the peer was not present in the set. + pub fn disallow_peer(&mut self, peer: PeerId) -> bool { + let removed = self.state.peers.remove(&peer); + if removed { + self.close_connections.push_back(peer); + if let Some(waker) = self.waker.take() { + waker.wake() + } } + removed } } impl Behaviour { + /// Peers that are currently blocked. + pub fn blocked_peers(&self) -> &HashSet { + &self.state.peers + } + /// Block connections to a given peer. /// /// All active connections to this peer will be closed immediately. - pub fn block_peer(&mut self, peer: PeerId) { - self.state.peers.insert(peer); - self.close_connections.push_back(peer); - if let Some(waker) = self.waker.take() { - waker.wake() + /// + /// Returns whether the peer was newly inserted. Does nothing if the peer was already present in the set. + pub fn block_peer(&mut self, peer: PeerId) -> bool { + let inserted = self.state.peers.insert(peer); + if inserted { + self.close_connections.push_back(peer); + if let Some(waker) = self.waker.take() { + waker.wake() + } } + inserted } /// Unblock connections to a given peer. - pub fn unblock_peer(&mut self, peer: PeerId) { - self.state.peers.remove(&peer); - if let Some(waker) = self.waker.take() { - waker.wake() + /// + /// Returns whether the peer was present in the set. Does nothing if the peer was not present in the set. + pub fn unblock_peer(&mut self, peer: PeerId) -> bool { + let removed = self.state.peers.remove(&peer); + if removed { + if let Some(waker) = self.waker.take() { + waker.wake() + } } + removed } } @@ -225,6 +256,7 @@ where peer: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { self.state.enforce(&peer)?; @@ -239,6 +271,8 @@ where _: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } diff --git a/misc/connection-limits/CHANGELOG.md b/misc/connection-limits/CHANGELOG.md index 4654281a83e..db88e99ffa7 100644 --- a/misc/connection-limits/CHANGELOG.md +++ b/misc/connection-limits/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.4.0 + + + ## 0.3.1 - Add function to mutate `ConnectionLimits`. diff --git a/misc/connection-limits/Cargo.toml b/misc/connection-limits/Cargo.toml index 8ecb0005cb1..56fe97f984b 100644 --- a/misc/connection-limits/Cargo.toml +++ b/misc/connection-limits/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-connection-limits" edition = "2021" rust-version = { workspace = true } description = "Connection limits for libp2p." -version = "0.3.1" +version = "0.4.0" license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" keywords = ["peer-to-peer", "libp2p", "networking"] diff --git a/misc/connection-limits/src/lib.rs b/misc/connection-limits/src/lib.rs index dbe68a8ad11..05a9b639f26 100644 --- a/misc/connection-limits/src/lib.rs +++ b/misc/connection-limits/src/lib.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr}; +use libp2p_core::{transport::PortUse, ConnectedPoint, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::{ behaviour::{ConnectionEstablished, DialFailure, ListenFailure}, @@ -278,6 +278,7 @@ impl NetworkBehaviour for Behaviour { peer: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { self.pending_outbound_connections.remove(&connection_id); @@ -354,6 +355,8 @@ impl NetworkBehaviour for Behaviour { _: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -569,6 +572,7 @@ mod tests { _peer: PeerId, _addr: &Multiaddr, _role_override: Endpoint, + _port_use: PortUse, ) -> Result, ConnectionDenied> { Err(ConnectionDenied::new(std::io::Error::new( std::io::ErrorKind::Other, @@ -584,6 +588,8 @@ mod tests { _connection_id: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } diff --git a/misc/keygen/Cargo.toml b/misc/keygen/Cargo.toml index 9fe9e926d76..003993a512c 100644 --- a/misc/keygen/Cargo.toml +++ b/misc/keygen/Cargo.toml @@ -13,12 +13,12 @@ publish = false release = false [dependencies] -clap = { version = "4.4.16", features = ["derive"] } +clap = { version = "4.5.6", features = ["derive"] } zeroize = "1" -serde = { version = "1.0.197", features = ["derive"] } -serde_json = "1.0.114" +serde = { version = "1.0.203", features = ["derive"] } +serde_json = "1.0.117" libp2p-core = { workspace = true } -base64 = "0.21.7" +base64 = "0.22.1" libp2p-identity = { workspace = true } [lints] diff --git a/misc/logging/Cargo.toml b/misc/logging/Cargo.toml new file mode 100644 index 00000000000..a9671c3f637 --- /dev/null +++ b/misc/logging/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "logging" +version = "0.0.0" +edition = "2021" +authors = ["drHuangMHT "] +license = "MIT" +repository = "https://github.com/libp2p/rust-libp2p" +publish = false + +[package.metadata.release] +release = false + +[dependencies] +tracing = "0.1.37" +tracing-subscriber = {version = "0.3", features = ["env-filter"]} + +[lints] +workspace = true diff --git a/misc/logging/src/lib.rs b/misc/logging/src/lib.rs new file mode 100644 index 00000000000..646adbeb8e1 --- /dev/null +++ b/misc/logging/src/lib.rs @@ -0,0 +1,8 @@ +use tracing_subscriber::EnvFilter; + +pub fn with_env_filter(){ + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .with_writer(||std::io::stderr()) + .try_init(); +} \ No newline at end of file diff --git a/misc/memory-connection-limits/CHANGELOG.md b/misc/memory-connection-limits/CHANGELOG.md index fc598872d50..9e580c5a1d2 100644 --- a/misc/memory-connection-limits/CHANGELOG.md +++ b/misc/memory-connection-limits/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.3.0 + + + ## 0.2.0 diff --git a/misc/memory-connection-limits/Cargo.toml b/misc/memory-connection-limits/Cargo.toml index ae6bb386373..f56ed33d5ad 100644 --- a/misc/memory-connection-limits/Cargo.toml +++ b/misc/memory-connection-limits/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-memory-connection-limits" edition = "2021" rust-version = { workspace = true } description = "Memory usage based connection limits for libp2p." -version = "0.2.0" +version = "0.3.0" license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" keywords = ["peer-to-peer", "libp2p", "networking"] @@ -14,8 +14,8 @@ memory-stats = { version = "1", features = ["always_use_statm"] } libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true, features = ["peerid"] } -sysinfo = "0.29" -tracing = "0.1.37" +sysinfo = "0.30" +tracing = { workspace = true } void = "1" [dev-dependencies] diff --git a/misc/memory-connection-limits/src/lib.rs b/misc/memory-connection-limits/src/lib.rs index ac911654979..757ff770487 100644 --- a/misc/memory-connection-limits/src/lib.rs +++ b/misc/memory-connection-limits/src/lib.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use libp2p_core::{Endpoint, Multiaddr}; +use libp2p_core::{transport::PortUse, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::{ dummy, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler, THandlerInEvent, @@ -31,6 +31,7 @@ use std::{ task::{Context, Poll}, time::{Duration, Instant}, }; +use sysinfo::MemoryRefreshKind; /// A [`NetworkBehaviour`] that enforces a set of memory usage based limits. /// @@ -90,10 +91,12 @@ impl Behaviour { /// /// New inbound and outbound connections will be denied when the threshold is reached. pub fn with_max_percentage(percentage: f64) -> Self { - use sysinfo::{RefreshKind, SystemExt}; + use sysinfo::{RefreshKind, System}; - let system_memory_bytes = - sysinfo::System::new_with_specifics(RefreshKind::new().with_memory()).total_memory(); + let system_memory_bytes = System::new_with_specifics( + RefreshKind::new().with_memory(MemoryRefreshKind::new().with_ram()), + ) + .total_memory(); Self::with_max_bytes((system_memory_bytes as f64 * percentage).round() as usize) } @@ -174,6 +177,7 @@ impl NetworkBehaviour for Behaviour { _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(dummy::ConnectionHandler) } @@ -186,6 +190,8 @@ impl NetworkBehaviour for Behaviour { _: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } diff --git a/misc/memory-connection-limits/tests/max_percentage.rs b/misc/memory-connection-limits/tests/max_percentage.rs index daee20703ee..bfb1b504af5 100644 --- a/misc/memory-connection-limits/tests/max_percentage.rs +++ b/misc/memory-connection-limits/tests/max_percentage.rs @@ -24,7 +24,7 @@ use libp2p_core::Multiaddr; use libp2p_identity::PeerId; use libp2p_memory_connection_limits::*; use std::time::Duration; -use sysinfo::{RefreshKind, SystemExt}; +use sysinfo::{MemoryRefreshKind, RefreshKind}; use util::*; use libp2p_swarm::{ @@ -36,7 +36,9 @@ use libp2p_swarm_test::SwarmExt; #[test] fn max_percentage() { const CONNECTION_LIMIT: usize = 20; - let system_info = sysinfo::System::new_with_specifics(RefreshKind::new().with_memory()); + let system_info = sysinfo::System::new_with_specifics( + RefreshKind::new().with_memory(MemoryRefreshKind::new().with_ram()), + ); let mut network = Swarm::new_ephemeral(|_| TestBehaviour { connection_limits: Behaviour::with_max_percentage(0.1), diff --git a/misc/memory-connection-limits/tests/util.rs b/misc/memory-connection-limits/tests/util/mod.rs similarity index 95% rename from misc/memory-connection-limits/tests/util.rs rename to misc/memory-connection-limits/tests/util/mod.rs index f40ce319929..01e8cd9f655 100644 --- a/misc/memory-connection-limits/tests/util.rs +++ b/misc/memory-connection-limits/tests/util/mod.rs @@ -20,7 +20,7 @@ use std::task::{Context, Poll}; -use libp2p_core::{Endpoint, Multiaddr}; +use libp2p_core::{transport::PortUse, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::{ dummy, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler, THandlerInEvent, @@ -102,6 +102,7 @@ impl NetworkBehaviour _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { self.handle_established(); Ok(dummy::ConnectionHandler) @@ -115,6 +116,8 @@ impl NetworkBehaviour _: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } diff --git a/misc/metrics/CHANGELOG.md b/misc/metrics/CHANGELOG.md index 67c304680db..bd109c42811 100644 --- a/misc/metrics/CHANGELOG.md +++ b/misc/metrics/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.15.0 +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + ## 0.14.1 - Add `BandwidthTransport`, wrapping an existing `Transport`, exposing Prometheus bandwidth metrics. diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 38c4777d4a6..0b7a3c93b2f 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-metrics" edition = "2021" rust-version = { workspace = true } description = "Metrics for libp2p" -version = "0.14.1" +version = "0.15.0" authors = ["Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -19,8 +19,8 @@ ping = ["libp2p-ping"] relay = ["libp2p-relay"] [dependencies] -futures = "0.3.30" -instant = "0.1.12" +futures = { workspace = true } +web-time = { workspace = true } libp2p-core = { workspace = true } libp2p-dcutr = { workspace = true, optional = true } libp2p-gossipsub = { workspace = true, optional = true } @@ -30,7 +30,7 @@ libp2p-kad = { workspace = true, optional = true } libp2p-ping = { workspace = true, optional = true } libp2p-relay = { workspace = true, optional = true } libp2p-swarm = { workspace = true } -pin-project = "1.1.4" +pin-project = "1.1.5" prometheus-client = { workspace = true } [dev-dependencies] @@ -40,8 +40,6 @@ libp2p-identity = { workspace = true, features = ["rand"] } # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustc-args = ["--cfg", "docsrs"] -rustdoc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/misc/metrics/src/bandwidth.rs b/misc/metrics/src/bandwidth.rs index 2792e00612c..8a0f54e5b65 100644 --- a/misc/metrics/src/bandwidth.rs +++ b/misc/metrics/src/bandwidth.rs @@ -7,7 +7,7 @@ use futures::{ }; use libp2p_core::{ muxing::{StreamMuxer, StreamMuxerEvent}, - transport::{ListenerId, TransportError, TransportEvent}, + transport::{DialOpts, ListenerId, TransportError, TransportEvent}, Multiaddr, }; use libp2p_identity::PeerId; @@ -84,33 +84,20 @@ where self.transport.remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { - let metrics = ConnectionMetrics::from_family_and_addr(&self.metrics, &addr); - Ok(self - .transport - .dial(addr.clone())? - .map_ok(Box::new(|(peer_id, stream_muxer)| { - (peer_id, Muxer::new(stream_muxer, metrics)) - }))) - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + dial_opts: DialOpts, ) -> Result> { let metrics = ConnectionMetrics::from_family_and_addr(&self.metrics, &addr); Ok(self .transport - .dial_as_listener(addr.clone())? + .dial(addr.clone(), dial_opts)? .map_ok(Box::new(|(peer_id, stream_muxer)| { (peer_id, Muxer::new(stream_muxer, metrics)) }))) } - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.transport.address_translation(server, observed) - } - fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -255,7 +242,7 @@ impl AsyncRead for InstrumentedStream { let num_bytes = ready!(this.inner.poll_read(cx, buf))?; this.metrics .inbound - .inc_by(u64::try_from(num_bytes).unwrap_or(u64::max_value())); + .inc_by(u64::try_from(num_bytes).unwrap_or(u64::MAX)); Poll::Ready(Ok(num_bytes)) } @@ -268,7 +255,7 @@ impl AsyncRead for InstrumentedStream { let num_bytes = ready!(this.inner.poll_read_vectored(cx, bufs))?; this.metrics .inbound - .inc_by(u64::try_from(num_bytes).unwrap_or(u64::max_value())); + .inc_by(u64::try_from(num_bytes).unwrap_or(u64::MAX)); Poll::Ready(Ok(num_bytes)) } } @@ -283,7 +270,7 @@ impl AsyncWrite for InstrumentedStream { let num_bytes = ready!(this.inner.poll_write(cx, buf))?; this.metrics .outbound - .inc_by(u64::try_from(num_bytes).unwrap_or(u64::max_value())); + .inc_by(u64::try_from(num_bytes).unwrap_or(u64::MAX)); Poll::Ready(Ok(num_bytes)) } @@ -296,7 +283,7 @@ impl AsyncWrite for InstrumentedStream { let num_bytes = ready!(this.inner.poll_write_vectored(cx, bufs))?; this.metrics .outbound - .inc_by(u64::try_from(num_bytes).unwrap_or(u64::max_value())); + .inc_by(u64::try_from(num_bytes).unwrap_or(u64::MAX)); Poll::Ready(Ok(num_bytes)) } diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index b1d4e9f0c89..03ac3f9634e 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -22,7 +22,7 @@ use crate::protocol_stack; use libp2p_identity::PeerId; use libp2p_swarm::StreamProtocol; use prometheus_client::collector::Collector; -use prometheus_client::encoding::{DescriptorEncoder, EncodeLabelSet, EncodeMetric}; +use prometheus_client::encoding::{DescriptorEncoder, EncodeMetric}; use prometheus_client::metrics::counter::Counter; use prometheus_client::metrics::gauge::ConstGauge; use prometheus_client::metrics::MetricType; @@ -138,11 +138,6 @@ impl super::Recorder> for Metrics { } } -#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)] -struct AddressLabels { - protocols: String, -} - #[derive(Default, Debug, Clone)] struct Peers(Arc>>); diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index 59e8c0bfa6a..57760df79a1 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -23,5 +23,11 @@ mod tests { let protocol_stack = as_string(&ma); assert_eq!(protocol_stack, "/ip6/tcp/wss/p2p"); + + let ma = Multiaddr::try_from("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/tls/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC").expect("testbad"); + + let protocol_stack = as_string(&ma); + + assert_eq!(protocol_stack, "/ip6/tcp/tls/ws/p2p"); } } diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index ad83401f316..51c0a0af253 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -22,13 +22,13 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; use crate::protocol_stack; -use instant::Instant; use libp2p_swarm::{ConnectionId, DialError, SwarmEvent}; use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue}; use prometheus_client::metrics::counter::Counter; use prometheus_client::metrics::family::Family; use prometheus_client::metrics::histogram::{exponential_buckets, Histogram}; use prometheus_client::registry::{Registry, Unit}; +use web_time::Instant; pub(crate) struct Metrics { connections_incoming: Family, diff --git a/misc/multistream-select/Cargo.toml b/misc/multistream-select/Cargo.toml index b6287505ae4..1bbe3642477 100644 --- a/misc/multistream-select/Cargo.toml +++ b/misc/multistream-select/Cargo.toml @@ -12,10 +12,10 @@ categories = ["network-programming", "asynchronous"] [dependencies] bytes = "1" -futures = "0.3" -tracing = "0.1.37" -pin-project = "1.1.4" -smallvec = "1.13.1" +futures = { workspace = true } +tracing = { workspace = true } +pin-project = "1.1.5" +smallvec = "1.13.2" unsigned-varint = { workspace = true } [dev-dependencies] @@ -24,14 +24,12 @@ futures_ringbuf = "0.4.0" quickcheck = { workspace = true } rand = "0.8" rw-stream-sink = { workspace = true } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/misc/multistream-select/src/length_delimited.rs b/misc/multistream-select/src/length_delimited.rs index 6515d00c717..3a7988d0548 100644 --- a/misc/multistream-select/src/length_delimited.rs +++ b/misc/multistream-select/src/length_delimited.rs @@ -25,7 +25,6 @@ use std::{ io, pin::Pin, task::{Context, Poll}, - u16, }; const MAX_LEN_BYTES: u16 = 2; diff --git a/misc/multistream-select/src/listener_select.rs b/misc/multistream-select/src/listener_select.rs index 21c507096e2..b4236310a1d 100644 --- a/misc/multistream-select/src/listener_select.rs +++ b/misc/multistream-select/src/listener_select.rs @@ -28,7 +28,6 @@ use futures::prelude::*; use smallvec::SmallVec; use std::{ convert::TryFrom as _, - iter::FromIterator, mem, pin::Pin, task::{Context, Poll}, diff --git a/misc/multistream-select/src/protocol.rs b/misc/multistream-select/src/protocol.rs index aef9f16c4cf..92b6acedaeb 100644 --- a/misc/multistream-select/src/protocol.rs +++ b/misc/multistream-select/src/protocol.rs @@ -31,7 +31,6 @@ use crate::Version; use bytes::{BufMut, Bytes, BytesMut}; use futures::{io::IoSlice, prelude::*, ready}; use std::{ - convert::TryFrom, error::Error, fmt, io, pin::Pin, @@ -137,24 +136,21 @@ pub(crate) enum Message { impl Message { /// Encodes a `Message` into its byte representation. - fn encode(&self, dest: &mut BytesMut) -> Result<(), ProtocolError> { + fn encode(&self, dest: &mut BytesMut) { match self { Message::Header(HeaderLine::V1) => { dest.reserve(MSG_MULTISTREAM_1_0.len()); dest.put(MSG_MULTISTREAM_1_0); - Ok(()) } Message::Protocol(p) => { let len = p.as_ref().len() + 1; // + 1 for \n dest.reserve(len); dest.put(p.0.as_ref()); dest.put_u8(b'\n'); - Ok(()) } Message::ListProtocols => { dest.reserve(MSG_LS.len()); dest.put(MSG_LS); - Ok(()) } Message::Protocols(ps) => { let mut buf = uvi::encode::usize_buffer(); @@ -167,12 +163,10 @@ impl Message { encoded.push(b'\n'); dest.reserve(encoded.len()); dest.put(encoded.as_ref()); - Ok(()) } Message::NotAvailable => { dest.reserve(MSG_PROTOCOL_NA.len()); dest.put(MSG_PROTOCOL_NA); - Ok(()) } } } @@ -289,7 +283,7 @@ where fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> { let mut buf = BytesMut::new(); - item.encode(&mut buf)?; + item.encode(&mut buf); self.project() .inner .start_send(buf.freeze()) @@ -500,8 +494,7 @@ mod tests { fn encode_decode_message() { fn prop(msg: Message) { let mut buf = BytesMut::new(); - msg.encode(&mut buf) - .unwrap_or_else(|_| panic!("Encoding message failed: {msg:?}")); + msg.encode(&mut buf); match Message::decode(buf.freeze()) { Ok(m) => assert_eq!(m, msg), Err(e) => panic!("Decoding failed: {e:?}"), diff --git a/misc/quick-protobuf-codec/Cargo.toml b/misc/quick-protobuf-codec/Cargo.toml index bc07b86b427..985479059a2 100644 --- a/misc/quick-protobuf-codec/Cargo.toml +++ b/misc/quick-protobuf-codec/Cargo.toml @@ -19,7 +19,7 @@ quick-protobuf = "0.8" [dev-dependencies] criterion = "0.5.1" -futures = "0.3.30" +futures = { workspace = true } quickcheck = { workspace = true } [[bench]] @@ -30,8 +30,6 @@ harness = false # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/misc/quick-protobuf-codec/src/lib.rs b/misc/quick-protobuf-codec/src/lib.rs index c50b1264af6..32cee8eccac 100644 --- a/misc/quick-protobuf-codec/src/lib.rs +++ b/misc/quick-protobuf-codec/src/lib.rs @@ -12,6 +12,7 @@ mod generated; pub use generated::test as proto; /// [`Codec`] implements [`Encoder`] and [`Decoder`], uses [`unsigned_varint`] +/// /// to prefix messages with their length and uses [`quick_protobuf`] and a provided /// `struct` implementing [`MessageRead`] and [`MessageWrite`] to do the encoding. pub struct Codec { @@ -182,7 +183,6 @@ impl From for io::Error { #[cfg(test)] mod tests { use super::*; - use crate::proto; use asynchronous_codec::FramedRead; use futures::io::Cursor; use futures::{FutureExt, StreamExt}; diff --git a/misc/quickcheck-ext/src/lib.rs b/misc/quickcheck-ext/src/lib.rs index a3b9ce26e6e..4ada7e73ba1 100644 --- a/misc/quickcheck-ext/src/lib.rs +++ b/misc/quickcheck-ext/src/lib.rs @@ -9,7 +9,7 @@ pub trait GenRange { fn gen_range(&mut self, _range: Range) -> T; fn gen_index(&mut self, ubound: usize) -> usize { - if ubound <= (core::u32::MAX as usize) { + if ubound <= (u32::MAX as usize) { self.gen_range(0..ubound as u32) as usize } else { self.gen_range(0..ubound) diff --git a/misc/rw-stream-sink/Cargo.toml b/misc/rw-stream-sink/Cargo.toml index f8f103bd6d9..20fa2fa23fa 100644 --- a/misc/rw-stream-sink/Cargo.toml +++ b/misc/rw-stream-sink/Cargo.toml @@ -11,8 +11,8 @@ keywords = ["networking"] categories = ["network-programming", "asynchronous"] [dependencies] -futures = "0.3.30" -pin-project = "1.1.4" +futures = { workspace = true } +pin-project = "1.1.5" static_assertions = "1" [dev-dependencies] @@ -22,8 +22,6 @@ async-std = "1.0" # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/misc/rw-stream-sink/src/lib.rs b/misc/rw-stream-sink/src/lib.rs index ee05196bfb4..f10e683ad33 100644 --- a/misc/rw-stream-sink/src/lib.rs +++ b/misc/rw-stream-sink/src/lib.rs @@ -117,7 +117,7 @@ where mod tests { use super::RwStreamSink; use async_std::task; - use futures::{channel::mpsc, prelude::*, stream}; + use futures::{channel::mpsc, prelude::*}; use std::{ pin::Pin, task::{Context, Poll}, diff --git a/misc/server/CHANGELOG.md b/misc/server/CHANGELOG.md index 254ab1d92be..fe48de0f553 100644 --- a/misc/server/CHANGELOG.md +++ b/misc/server/CHANGELOG.md @@ -1,9 +1,18 @@ +## 0.12.8 + +### Changed + +- Remove deprecated [`libp2p-lookup`](https://github.com/mxinden/libp2p-lookup) from Dockerfile. + See [PR 5610](https://github.com/libp2p/rust-libp2p/pull/5610). + ## 0.12.7 ### Changed - Use periodic and automatic bootstrap of Kademlia. See [PR 4838](https://github.com/libp2p/rust-libp2p/pull/4838). +- Update to [`libp2p-identify` `v0.45.0`](protocols/identify/CHANGELOG.md#0450). + See [PR 4981](https://github.com/libp2p/rust-libp2p/pull/4981). ## 0.12.6 @@ -29,6 +38,7 @@ ## 0.12.3 ### Changed + - Add libp2p-lookup to Dockerfile to enable healthchecks. ### Fixed @@ -40,14 +50,18 @@ [PR 4467]: https://github.com/libp2p/rust-libp2p/pull/4467 ## 0.12.2 + ### Fixed + - Adhere to `--metrics-path` flag and listen on `0.0.0.0:8888` (default IPFS metrics port). [PR 4392] [PR 4392]: https://github.com/libp2p/rust-libp2p/pull/4392 ## 0.12.1 + ### Changed + - Move to tokio and hyper. See [PR 4311]. - Move to distroless Docker base image. @@ -56,39 +70,57 @@ [PR 4311]: https://github.com/libp2p/rust-libp2p/pull/4311 ## 0.8.0 + ### Changed + - Remove mplex support. ## 0.7.0 + ### Changed + - Update to libp2p v0.47.0. ## 0.6.0 - 2022-05-05 + ### Changed + - Update to libp2p v0.44.0. ## 0.5.4 - 2022-01-11 + ### Changed + - Pull latest autonat changes. ## 0.5.3 - 2021-12-25 + ### Changed + - Update dependencies. - Pull in autonat fixes. ## 0.5.2 - 2021-12-20 + ### Added + - Add support for libp2p autonat protocol via `--enable-autonat`. ## 0.5.1 - 2021-12-20 + ### Fixed + - Update dependencies. - Fix typo in command line flag `--enable-kademlia`. ## 0.5.0 - 2021-11-18 + ### Changed + - Disable Kademlia protocol by default. ## 0.4.0 - 2021-11-18 + ### Fixed + - Update dependencies. diff --git a/misc/server/Cargo.toml b/misc/server/Cargo.toml index 3f46de701e2..0954e2f38d8 100644 --- a/misc/server/Cargo.toml +++ b/misc/server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-server" -version = "0.12.7" +version = "0.12.8" authors = ["Max Inden "] edition = "2021" repository = "https://github.com/libp2p/rust-libp2p" @@ -11,19 +11,35 @@ license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -base64 = "0.21" -clap = { version = "4.4.16", features = ["derive"] } -futures = "0.3" +base64 = "0.22" +clap = { version = "4.5.6", features = ["derive"] } +futures = { workspace = true } futures-timer = "3" -hyper = { version = "0.14", features = ["server", "tcp", "http1"] } -libp2p = { workspace = true, features = ["autonat", "dns", "tokio", "noise", "tcp", "yamux", "identify", "kad", "ping", "relay", "metrics", "rsa", "macros", "quic", "websocket"] } +axum = "0.7" +libp2p = { workspace = true, features = [ + "autonat", + "dns", + "tokio", + "noise", + "tcp", + "yamux", + "identify", + "kad", + "ping", + "relay", + "metrics", + "rsa", + "macros", + "quic", + "websocket", +] } prometheus-client = { workspace = true } -serde = "1.0.197" +serde = "1.0.203" serde_derive = "1.0.125" serde_json = "1.0" -tokio = { version = "1", features = ["rt-multi-thread", "macros"] } -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } zeroize = "1" [lints] diff --git a/misc/server/Dockerfile b/misc/server/Dockerfile index 9d2742f97e8..12a8982eb3f 100644 --- a/misc/server/Dockerfile +++ b/misc/server/Dockerfile @@ -1,7 +1,6 @@ # syntax=docker/dockerfile:1.5-labs -FROM rust:1.73.0 as chef +FROM rust:1.81.0 as chef RUN wget -q -O- https://github.com/LukeMathWalker/cargo-chef/releases/download/v0.1.62/cargo-chef-x86_64-unknown-linux-gnu.tar.gz | tar -zx -C /usr/local/bin -RUN cargo install --locked --root /usr/local libp2p-lookup --version 0.6.4 WORKDIR /app FROM chef AS planner @@ -17,5 +16,4 @@ COPY . . RUN cargo build --release --package libp2p-server FROM gcr.io/distroless/cc -COPY --from=builder /usr/local/bin/libp2p-server /usr/local/bin/libp2p-lookup /usr/local/bin/ CMD ["libp2p-server"] diff --git a/misc/server/README.md b/misc/server/README.md index 0da1bd8abd9..f9a5d65124a 100644 --- a/misc/server/README.md +++ b/misc/server/README.md @@ -25,7 +25,6 @@ Options: -h, --help Print help ``` - ``` cargo run -- --config ~/.ipfs/config @@ -33,9 +32,3 @@ Local peer id: PeerId("12D3KooWSa1YEeQVSwvoqAMhwjKQ6kqZQckhWPb3RWEGV3sZGU6Z") Listening on "/ip4/127.0.0.1/udp/4001/quic" [...] ``` - -The Docker container includes [libp2-lookup](https://github.com/mxinden/libp2p-lookup/) to enable adding a proper healthcheck for container startup, e.g. - -``` shell -docker run --health-cmd 'libp2p-lookup direct --address /ip4/127.0.0.1/tcp/4001/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa' /home/ipfs/.ipfs:/ipfs ghcr.io/libp2p/rust-libp2p-server --config /ipfs/config -``` diff --git a/misc/server/src/http_service.rs b/misc/server/src/http_service.rs index 7905933fbf5..cee1aa96e28 100644 --- a/misc/server/src/http_service.rs +++ b/misc/server/src/http_service.rs @@ -18,115 +18,62 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use hyper::http::StatusCode; -use hyper::service::Service; -use hyper::{Body, Method, Request, Response, Server}; +use axum::extract::State; +use axum::http::StatusCode; +use axum::response::IntoResponse; +use axum::routing::get; +use axum::Router; use prometheus_client::encoding::text::encode; use prometheus_client::registry::Registry; -use std::future::Future; -use std::pin::Pin; +use std::net::SocketAddr; use std::sync::{Arc, Mutex}; -use std::task::{Context, Poll}; +use tokio::net::TcpListener; const METRICS_CONTENT_TYPE: &str = "application/openmetrics-text;charset=utf-8;version=1.0.0"; pub(crate) async fn metrics_server( registry: Registry, metrics_path: String, -) -> Result<(), hyper::Error> { +) -> Result<(), std::io::Error> { // Serve on localhost. - let addr = ([0, 0, 0, 0], 8888).into(); - - let server = Server::bind(&addr).serve(MakeMetricService::new(registry, metrics_path.clone())); - tracing::info!(metrics_server=%format!("http://{}{}", server.local_addr(), metrics_path)); - server.await?; + let addr: SocketAddr = ([0, 0, 0, 0], 8888).into(); + let service = MetricService::new(registry); + let server = Router::new() + .route(&metrics_path, get(respond_with_metrics)) + .with_state(service); + let tcp_listener = TcpListener::bind(addr).await?; + let local_addr = tcp_listener.local_addr()?; + tracing::info!(metrics_server=%format!("http://{}{}", local_addr, metrics_path)); + axum::serve(tcp_listener, server.into_make_service()).await?; Ok(()) } -pub(crate) struct MetricService { - reg: Arc>, - metrics_path: String, -} -type SharedRegistry = Arc>; +async fn respond_with_metrics(state: State) -> impl IntoResponse { + let mut sink = String::new(); + let reg = state.get_reg(); + encode(&mut sink, ®.lock().unwrap()).unwrap(); -impl MetricService { - fn get_reg(&mut self) -> SharedRegistry { - Arc::clone(&self.reg) - } - fn respond_with_metrics(&mut self) -> Response { - let mut response: Response = Response::default(); - - response.headers_mut().insert( - hyper::header::CONTENT_TYPE, - METRICS_CONTENT_TYPE.try_into().unwrap(), - ); - - let reg = self.get_reg(); - encode(&mut response.body_mut(), ®.lock().unwrap()).unwrap(); - - *response.status_mut() = StatusCode::OK; - - response - } - fn respond_with_404_not_found(&mut self) -> Response { - Response::builder() - .status(StatusCode::NOT_FOUND) - .body(format!( - "Not found try localhost:[port]/{}", - self.metrics_path - )) - .unwrap() - } + ( + StatusCode::OK, + [(axum::http::header::CONTENT_TYPE, METRICS_CONTENT_TYPE)], + sink, + ) } -impl Service> for MetricService { - type Response = Response; - type Error = hyper::Error; - type Future = Pin> + Send>>; - - fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { - Poll::Ready(Ok(())) - } - - fn call(&mut self, req: Request) -> Self::Future { - let req_path = req.uri().path(); - let req_method = req.method(); - let resp = if (req_method == Method::GET) && (req_path == self.metrics_path) { - // Encode and serve metrics from registry. - self.respond_with_metrics() - } else { - self.respond_with_404_not_found() - }; - Box::pin(async { Ok(resp) }) - } +#[derive(Clone)] +pub(crate) struct MetricService { + reg: Arc>, } -pub(crate) struct MakeMetricService { - reg: SharedRegistry, - metrics_path: String, -} +type SharedRegistry = Arc>; -impl MakeMetricService { - pub(crate) fn new(registry: Registry, metrics_path: String) -> MakeMetricService { - MakeMetricService { - reg: Arc::new(Mutex::new(registry)), - metrics_path, +impl MetricService { + fn new(reg: Registry) -> Self { + Self { + reg: Arc::new(Mutex::new(reg)), } } -} - -impl Service for MakeMetricService { - type Response = MetricService; - type Error = hyper::Error; - type Future = Pin> + Send>>; - fn poll_ready(&mut self, _: &mut Context) -> Poll> { - Poll::Ready(Ok(())) - } - - fn call(&mut self, _: T) -> Self::Future { - let reg = self.reg.clone(); - let metrics_path = self.metrics_path.clone(); - let fut = async move { Ok(MetricService { reg, metrics_path }) }; - Box::pin(fut) + fn get_reg(&self) -> SharedRegistry { + Arc::clone(&self.reg) } } diff --git a/misc/server/src/main.rs b/misc/server/src/main.rs index 6dfa035f3b1..820921beaed 100644 --- a/misc/server/src/main.rs +++ b/misc/server/src/main.rs @@ -71,7 +71,7 @@ async fn main() -> Result<(), Box> { let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_keypair) .with_tokio() .with_tcp( - tcp::Config::default().port_reuse(true).nodelay(true), + tcp::Config::default().nodelay(true), noise::Config::new, yamux::Config::default, )? @@ -138,6 +138,7 @@ async fn main() -> Result<(), Box> { protocols, .. }, + .. } = e { if protocols.iter().any(|p| *p == kad::PROTOCOL_NAME) { diff --git a/misc/webrtc-utils/CHANGELOG.md b/misc/webrtc-utils/CHANGELOG.md index 6949113a377..3bb31610fa1 100644 --- a/misc/webrtc-utils/CHANGELOG.md +++ b/misc/webrtc-utils/CHANGELOG.md @@ -1,3 +1,12 @@ +## 0.3.0 + + + +## 0.2.1 + +- Fix end of stream handling when buffer is empty or not present. + See [PR 5439](https://github.com/libp2p/rust-libp2p/pull/5439). + ## 0.2.0 - Update to latest version of `libp2p-noise`. diff --git a/misc/webrtc-utils/Cargo.toml b/misc/webrtc-utils/Cargo.toml index 7173dedae7b..88f576f12d9 100644 --- a/misc/webrtc-utils/Cargo.toml +++ b/misc/webrtc-utils/Cargo.toml @@ -7,13 +7,13 @@ license = "MIT" name = "libp2p-webrtc-utils" repository = "https://github.com/libp2p/rust-libp2p" rust-version = { workspace = true } -version = "0.2.0" +version = "0.3.0" publish = true [dependencies] asynchronous-codec = { workspace = true } bytes = "1" -futures = "0.3" +futures = { workspace = true } hex = "0.4" libp2p-core = { workspace = true } libp2p-identity = { workspace = true } @@ -25,7 +25,7 @@ serde = { version = "1.0", features = ["derive"] } sha2 = "0.10.8" thiserror = "1" tinytemplate = "1.2" -tracing = "0.1.37" +tracing = { workspace = true } [dev-dependencies] hex-literal = "0.4" diff --git a/misc/webrtc-utils/src/stream.rs b/misc/webrtc-utils/src/stream.rs index 0e1496eb640..17f746a92a1 100644 --- a/misc/webrtc-utils/src/stream.rs +++ b/misc/webrtc-utils/src/stream.rs @@ -146,8 +146,14 @@ where } debug_assert!(read_buffer.is_empty()); - if let Some(message) = message { - *read_buffer = message.into(); + match message { + Some(msg) if !msg.is_empty() => { + *read_buffer = msg.into(); + } + _ => { + tracing::debug!("poll_read buffer is empty, received None"); + return Poll::Ready(Ok(0)); + } } } None => { diff --git a/muxers/mplex/CHANGELOG.md b/muxers/mplex/CHANGELOG.md index 48ab616e131..f0c2c0353da 100644 --- a/muxers/mplex/CHANGELOG.md +++ b/muxers/mplex/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.42.0 + + + ## 0.41.0 - Migrate to `{In,Out}boundConnectionUpgrade` traits. diff --git a/muxers/mplex/Cargo.toml b/muxers/mplex/Cargo.toml index 7cb4befa22b..7f887c8b3b8 100644 --- a/muxers/mplex/Cargo.toml +++ b/muxers/mplex/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-mplex" edition = "2021" rust-version = { workspace = true } description = "Mplex multiplexing protocol for libp2p" -version = "0.41.0" +version = "0.42.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -12,38 +12,36 @@ categories = ["network-programming", "asynchronous"] [dependencies] bytes = "1" -futures = "0.3.30" +futures = { workspace = true } asynchronous-codec = { workspace = true } libp2p-core = { workspace = true } libp2p-identity = { workspace = true } nohash-hasher = "0.2" parking_lot = "0.12" rand = "0.8" -smallvec = "1.13.1" -tracing = "0.1.37" +smallvec = "1.13.2" +tracing = { workspace = true } unsigned-varint = { workspace = true, features = ["asynchronous_codec"] } [dev-dependencies] async-std = { version = "1.7.0", features = ["attributes"] } criterion = "0.5" -futures = "0.3" +futures = { workspace = true } libp2p-identity = { workspace = true, features = ["rand"] } libp2p-muxer-test-harness = { path = "../test-harness" } libp2p-plaintext = { workspace = true } libp2p-tcp = { workspace = true, features = ["async-io"] } quickcheck = { workspace = true } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [[bench]] name = "split_send_size" harness = false -# Passing arguments to the docsrs builder in order to properly document cfg's. +# Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/muxers/mplex/benches/split_send_size.rs b/muxers/mplex/benches/split_send_size.rs index 0125d49dcef..44eafa884ac 100644 --- a/muxers/mplex/benches/split_send_size.rs +++ b/muxers/mplex/benches/split_send_size.rs @@ -28,6 +28,7 @@ use futures::prelude::*; use futures::{channel::oneshot, future::join}; use libp2p_core::muxing::StreamMuxerExt; use libp2p_core::transport::ListenerId; +use libp2p_core::Endpoint; use libp2p_core::{multiaddr::multiaddr, muxing, transport, upgrade, Multiaddr, Transport}; use libp2p_identity as identity; use libp2p_identity::PeerId; @@ -146,7 +147,17 @@ fn run( // Spawn and block on the sender, i.e. until all data is sent. let sender = async move { let addr = addr_receiver.await.unwrap(); - let (_peer, mut conn) = sender_trans.dial(addr).unwrap().await.unwrap(); + let (_peer, mut conn) = sender_trans + .dial( + addr, + transport::DialOpts { + role: Endpoint::Dialer, + port_use: transport::PortUse::Reuse, + }, + ) + .unwrap() + .await + .unwrap(); // Just calling `poll_outbound` without `poll` is fine here because mplex makes progress through all `poll_` functions. It is hacky though. let mut stream = poll_fn(|cx| conn.poll_outbound_unpin(cx)).await.unwrap(); let mut off = 0; diff --git a/muxers/mplex/src/lib.rs b/muxers/mplex/src/lib.rs index c67e0e3baec..17ca9ad46f6 100644 --- a/muxers/mplex/src/lib.rs +++ b/muxers/mplex/src/lib.rs @@ -30,7 +30,7 @@ pub use config::{MaxBufferBehaviour, MplexConfig}; use bytes::Bytes; use codec::LocalStreamId; -use futures::{future, prelude::*, ready}; +use futures::{prelude::*, ready}; use libp2p_core::muxing::{StreamMuxer, StreamMuxerEvent}; use libp2p_core::upgrade::{InboundConnectionUpgrade, OutboundConnectionUpgrade, UpgradeInfo}; use parking_lot::Mutex; diff --git a/muxers/test-harness/Cargo.toml b/muxers/test-harness/Cargo.toml index bfe1e61b9b6..f4632437ce6 100644 --- a/muxers/test-harness/Cargo.toml +++ b/muxers/test-harness/Cargo.toml @@ -12,10 +12,10 @@ release = false [dependencies] libp2p-core = { workspace = true } -futures = "0.3.30" +futures = { workspace = true } futures-timer = "3.0.3" futures_ringbuf = "0.4.0" -tracing = "0.1.37" +tracing = { workspace = true } [lints] workspace = true diff --git a/muxers/yamux/CHANGELOG.md b/muxers/yamux/CHANGELOG.md index de608b195f8..855b3a33773 100644 --- a/muxers/yamux/CHANGELOG.md +++ b/muxers/yamux/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.46.0 + + + +## 0.45.2 + +- Update `yamux` to version `v0.13.3`.` + ## 0.45.1 - Deprecate `WindowUpdateMode::on_receive`. diff --git a/muxers/yamux/Cargo.toml b/muxers/yamux/Cargo.toml index 14a5c0fe145..0c52eca3fd4 100644 --- a/muxers/yamux/Cargo.toml +++ b/muxers/yamux/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-yamux" edition = "2021" rust-version = { workspace = true } description = "Yamux multiplexing protocol for libp2p" -version = "0.45.1" +version = "0.46.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -12,12 +12,12 @@ categories = ["network-programming", "asynchronous"] [dependencies] either = "1" -futures = "0.3.30" +futures = { workspace = true } libp2p-core = { workspace = true } thiserror = "1.0" yamux012 = { version = "0.12.1", package = "yamux" } -yamux013 = { version = "0.13.1", package = "yamux" } -tracing = "0.1.37" +yamux013 = { version = "0.13.3", package = "yamux" } +tracing = { workspace = true } [dev-dependencies] async-std = { version = "1.7.0", features = ["attributes"] } @@ -27,8 +27,6 @@ libp2p-muxer-test-harness = { path = "../test-harness" } # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/muxers/yamux/src/lib.rs b/muxers/yamux/src/lib.rs index 2b5eb52a11e..bcfeb62fccf 100644 --- a/muxers/yamux/src/lib.rs +++ b/muxers/yamux/src/lib.rs @@ -23,7 +23,7 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] use either::Either; -use futures::{future, prelude::*, ready}; +use futures::{prelude::*, ready}; use libp2p_core::muxing::{StreamMuxer, StreamMuxerEvent}; use libp2p_core::upgrade::{InboundConnectionUpgrade, OutboundConnectionUpgrade, UpgradeInfo}; use std::collections::VecDeque; diff --git a/protocols/autonat/CHANGELOG.md b/protocols/autonat/CHANGELOG.md index 1259dd01fd4..f1aeda6ac18 100644 --- a/protocols/autonat/CHANGELOG.md +++ b/protocols/autonat/CHANGELOG.md @@ -1,3 +1,25 @@ +## 0.13.1 +- Verify that an incoming AutoNAT dial comes from a connected peer. See [PR 5597](https://github.com/libp2p/rust-libp2p/pull/5597). + +## 0.13.0 + +- Due to the refactor of `Transport` it's no longer required to create a seperate transport for +AutoNAT where port reuse is disabled. This information is now passed by the behaviour. + See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568). +- Introduce the new AutoNATv2 protocol. + It's split into a client and a server part, represented in their respective modules + Features: + - The server now always dials back over a newly allocated port. + This more accurately reflects the reachability state for other peers and avoids accidental hole punching. + - The server can now test addresses different from the observed address (i.e., the connection to the server was made through a `p2p-circuit`). To mitigate against DDoS attacks, the client has to send more data to the server than the dial-back costs. + See [PR 5526](https://github.com/libp2p/rust-libp2p/pull/5526). + + + +## 0.12.1 +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + ## 0.12.0 - Remove `Clone`, `PartialEq` and `Eq` implementations on `Event` and its sub-structs. diff --git a/protocols/autonat/Cargo.toml b/protocols/autonat/Cargo.toml index fce64ad0c12..0c0e757641d 100644 --- a/protocols/autonat/Cargo.toml +++ b/protocols/autonat/Cargo.toml @@ -3,39 +3,52 @@ name = "libp2p-autonat" edition = "2021" rust-version = { workspace = true } description = "NAT and firewall detection for libp2p" -authors = ["David Craven ", "Elena Frank "] -version = "0.12.0" +version = "0.13.1" +authors = ["David Craven ", "Elena Frank ", "Hannes Furmans "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] + [dependencies] -async-trait = "0.1" -futures = "0.3" +async-trait = { version = "0.1", optional = true } +asynchronous-codec = { workspace = true } +bytes = { version = "1", optional = true } +either = { version = "1.9.0", optional = true } +futures = { workspace = true } +futures-bounded = { workspace = true, optional = true } futures-timer = "3.0" -instant = "0.1" +web-time = { workspace = true, optional = true } libp2p-core = { workspace = true } -libp2p-swarm = { workspace = true } -libp2p-request-response = { workspace = true } libp2p-identity = { workspace = true } +libp2p-request-response = { workspace = true, optional = true } +libp2p-swarm = { workspace = true } quick-protobuf = "0.8" -rand = "0.8" -tracing = "0.1.37" +tracing = { workspace = true } quick-protobuf-codec = { workspace = true } -asynchronous-codec = { workspace = true } +rand = "0.8" +rand_core = { version = "0.6", optional = true } +thiserror = { version = "1.0.52", optional = true } +void = { version = "1", optional = true } [dev-dependencies] +tokio = { version = "1", features = ["macros", "rt", "sync"]} async-std = { version = "1.10", features = ["attributes"] } libp2p-swarm-test = { path = "../../swarm-test" } tracing-subscriber = { version = "0.3", features = ["env-filter"] } +libp2p-identify = { workspace = true } +libp2p-swarm = { workspace = true, features = ["macros"]} + +[features] +default = ["v1", "v2"] +v1 = ["dep:libp2p-request-response", "dep:web-time", "dep:async-trait"] +v2 = ["dep:bytes", "dep:either", "dep:futures-bounded", "dep:thiserror", "dep:void", "dep:rand_core"] # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/autonat/src/lib.rs b/protocols/autonat/src/lib.rs index 10c87b1e984..e49eaadcb83 100644 --- a/protocols/autonat/src/lib.rs +++ b/protocols/autonat/src/lib.rs @@ -1,41 +1,10 @@ -// Copyright 2021 Protocol Labs. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. +#![cfg_attr(docsrs, feature(doc_auto_cfg))] -//! Implementation of the [AutoNAT](https://github.com/libp2p/specs/blob/master/autonat/README.md) protocol. +#[cfg(feature = "v1")] +pub mod v1; -#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +#[cfg(feature = "v2")] +pub mod v2; -mod behaviour; -mod protocol; - -pub use self::{ - behaviour::{ - Behaviour, Config, Event, InboundProbeError, InboundProbeEvent, NatStatus, - OutboundProbeError, OutboundProbeEvent, ProbeId, - }, - protocol::{ResponseError, DEFAULT_PROTOCOL_NAME}, -}; -pub use libp2p_request_response::{InboundFailure, OutboundFailure}; - -mod proto { - #![allow(unreachable_pub)] - include!("generated/mod.rs"); - pub(crate) use self::structs::{mod_Message::*, Message}; -} +#[cfg(feature = "v1")] +pub use v1::*; diff --git a/protocols/autonat/src/v1.rs b/protocols/autonat/src/v1.rs new file mode 100644 index 00000000000..c60e4805f40 --- /dev/null +++ b/protocols/autonat/src/v1.rs @@ -0,0 +1,45 @@ +// Copyright 2021 Protocol Labs. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +//! Implementation of the [AutoNAT](https://github.com/libp2p/specs/blob/master/autonat/README.md) protocol. +//! +//! ## Eventual Deprecation +//! This version of the protocol will eventually be deprecated in favor of [v2](crate::v2). +//! We recommend using v2 for new projects. + +#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] + +pub(crate) mod behaviour; +pub(crate) mod protocol; + +pub use self::{ + behaviour::{ + Behaviour, Config, Event, InboundProbeError, InboundProbeEvent, NatStatus, + OutboundProbeError, OutboundProbeEvent, ProbeId, + }, + protocol::{ResponseError, DEFAULT_PROTOCOL_NAME}, +}; +pub use libp2p_request_response::{InboundFailure, OutboundFailure}; + +pub(crate) mod proto { + #![allow(unreachable_pub)] + include!("v1/generated/mod.rs"); + pub(crate) use self::structs::{mod_Message::*, Message}; +} diff --git a/protocols/autonat/src/behaviour.rs b/protocols/autonat/src/v1/behaviour.rs similarity index 98% rename from protocols/autonat/src/behaviour.rs rename to protocols/autonat/src/v1/behaviour.rs index a770e61e88a..7a717baed8d 100644 --- a/protocols/autonat/src/behaviour.rs +++ b/protocols/autonat/src/v1/behaviour.rs @@ -28,7 +28,7 @@ pub use as_client::{OutboundProbeError, OutboundProbeEvent}; use as_server::AsServer; pub use as_server::{InboundProbeError, InboundProbeEvent}; use futures_timer::Delay; -use instant::Instant; +use libp2p_core::transport::PortUse; use libp2p_core::{multiaddr::Protocol, ConnectedPoint, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_request_response::{ @@ -45,6 +45,7 @@ use std::{ task::{Context, Poll}, time::Duration, }; +use web_time::Instant; /// Config for the [`Behaviour`]. #[derive(Debug, Clone, PartialEq, Eq)] @@ -338,6 +339,7 @@ impl Behaviour { ConnectedPoint::Dialer { address, role_override: Endpoint::Dialer, + port_use: _, } => { if let Some(event) = self.as_server().on_outbound_connection(&peer, address) { self.pending_actions @@ -347,6 +349,7 @@ impl Behaviour { ConnectedPoint::Dialer { address: _, role_override: Endpoint::Listener, + port_use: _, } => { // Outgoing connection was dialed as a listener. In other words outgoing connection // was dialed as part of a hole punch. `libp2p-autonat` never attempts to hole @@ -512,9 +515,15 @@ impl NetworkBehaviour for Behaviour { peer: PeerId, addr: &Multiaddr, role_override: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied> { - self.inner - .handle_established_outbound_connection(connection_id, peer, addr, role_override) + self.inner.handle_established_outbound_connection( + connection_id, + peer, + addr, + role_override, + port_use, + ) } fn on_swarm_event(&mut self, event: FromSwarm) { diff --git a/protocols/autonat/src/behaviour/as_client.rs b/protocols/autonat/src/v1/behaviour/as_client.rs similarity index 99% rename from protocols/autonat/src/behaviour/as_client.rs rename to protocols/autonat/src/v1/behaviour/as_client.rs index 668f3b93719..8960163ccb3 100644 --- a/protocols/autonat/src/behaviour/as_client.rs +++ b/protocols/autonat/src/v1/behaviour/as_client.rs @@ -26,7 +26,6 @@ use super::{ }; use futures::FutureExt; use futures_timer::Delay; -use instant::Instant; use libp2p_core::Multiaddr; use libp2p_identity::PeerId; use libp2p_request_response::{self as request_response, OutboundFailure, OutboundRequestId}; @@ -37,6 +36,7 @@ use std::{ task::{Context, Poll}, time::Duration, }; +use web_time::Instant; /// Outbound probe failed or was aborted. #[derive(Debug)] diff --git a/protocols/autonat/src/behaviour/as_server.rs b/protocols/autonat/src/v1/behaviour/as_server.rs similarity index 95% rename from protocols/autonat/src/behaviour/as_server.rs rename to protocols/autonat/src/v1/behaviour/as_server.rs index 878fd713dda..1289bd53d24 100644 --- a/protocols/autonat/src/behaviour/as_server.rs +++ b/protocols/autonat/src/v1/behaviour/as_server.rs @@ -17,12 +17,10 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. - use super::{ Action, AutoNatCodec, Config, DialRequest, DialResponse, Event, HandleInnerEvent, ProbeId, ResponseError, }; -use instant::Instant; use libp2p_core::{multiaddr::Protocol, Multiaddr}; use libp2p_identity::PeerId; use libp2p_request_response::{ @@ -36,6 +34,7 @@ use std::{ collections::{HashMap, HashSet, VecDeque}, num::NonZeroU8, }; +use web_time::Instant; /// Inbound probe failed. #[derive(Debug)] @@ -108,6 +107,21 @@ impl<'a> HandleInnerEvent for AsServer<'a> { }, } => { let probe_id = self.probe_id.next(); + if !self.connected.contains_key(&peer) { + tracing::debug!( + %peer, + "Reject inbound dial request from peer since it is not connected" + ); + + return VecDeque::from([ToSwarm::GenerateEvent(Event::InboundProbe( + InboundProbeEvent::Error { + probe_id, + peer, + error: InboundProbeError::Response(ResponseError::DialRefused), + }, + ))]); + } + match self.resolve_inbound_request(peer, request) { Ok(addrs) => { tracing::debug!( @@ -135,6 +149,7 @@ impl<'a> HandleInnerEvent for AsServer<'a> { NonZeroU8::new(1).expect("1 > 0"), ) .addresses(addrs) + .allocate_new_port() .build(), }, ]) diff --git a/protocols/autonat/src/generated/mod.rs b/protocols/autonat/src/v1/generated/mod.rs similarity index 100% rename from protocols/autonat/src/generated/mod.rs rename to protocols/autonat/src/v1/generated/mod.rs diff --git a/protocols/autonat/src/generated/structs.proto b/protocols/autonat/src/v1/generated/structs.proto similarity index 100% rename from protocols/autonat/src/generated/structs.proto rename to protocols/autonat/src/v1/generated/structs.proto diff --git a/protocols/autonat/src/generated/structs.rs b/protocols/autonat/src/v1/generated/structs.rs similarity index 100% rename from protocols/autonat/src/generated/structs.rs rename to protocols/autonat/src/v1/generated/structs.rs diff --git a/protocols/autonat/src/protocol.rs b/protocols/autonat/src/v1/protocol.rs similarity index 97% rename from protocols/autonat/src/protocol.rs rename to protocols/autonat/src/v1/protocol.rs index c1862058400..2ce538fddf4 100644 --- a/protocols/autonat/src/protocol.rs +++ b/protocols/autonat/src/v1/protocol.rs @@ -27,7 +27,7 @@ use libp2p_core::Multiaddr; use libp2p_identity::PeerId; use libp2p_request_response::{self as request_response}; use libp2p_swarm::StreamProtocol; -use std::{convert::TryFrom, io}; +use std::io; /// The protocol name used for negotiating with multistream-select. pub const DEFAULT_PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/libp2p/autonat/1.0.0"); @@ -121,12 +121,8 @@ impl DialRequest { } let peer_id_result = msg.dial.and_then(|dial| { - dial.peer.and_then(|peer_info| { - let Some(peer_id) = peer_info.id else { - return None; - }; - Some((peer_id, peer_info.addrs)) - }) + dial.peer + .and_then(|peer_info| peer_info.id.map(|peer_id| (peer_id, peer_info.addrs))) }); let (peer_id, addrs) = peer_id_result diff --git a/protocols/autonat/src/v2.rs b/protocols/autonat/src/v2.rs new file mode 100644 index 00000000000..cdc807ea303 --- /dev/null +++ b/protocols/autonat/src/v2.rs @@ -0,0 +1,37 @@ +//! The second version of the autonat protocol. +//! +//! The implementation follows the [libp2p spec](https://github.com/libp2p/specs/blob/03718ef0f2dea4a756a85ba716ee33f97e4a6d6c/autonat/autonat-v2.md). +//! +//! The new version fixes the issues of the first version: +//! - The server now always dials back over a newly allocated port. This greatly reduces the risk of +//! false positives that often occurred in the first version, when the clinet-server connection +//! occurred over a hole-punched port. +//! - The server protects against DoS attacks by requiring the client to send more data to the +//! server then the dial back puts on the client, thus making the protocol unatractive for an +//! attacker. +//! +//! The protocol is seperated into two parts: +//! - The client part, which is implemented in the `client` module. (The client is the party that +//! wants to check if it is reachable from the outside.) +//! - The server part, which is implemented in the `server` module. (The server is the party +//! performing reachability checks on behalf of the client.) +//! +//! The two can be used together. + +use libp2p_swarm::StreamProtocol; + +pub mod client; +pub(crate) mod protocol; +pub mod server; + +pub(crate) mod generated { + #![allow(unreachable_pub)] + include!("v2/generated/mod.rs"); +} + +pub(crate) const DIAL_REQUEST_PROTOCOL: StreamProtocol = + StreamProtocol::new("/libp2p/autonat/2/dial-request"); +pub(crate) const DIAL_BACK_PROTOCOL: StreamProtocol = + StreamProtocol::new("/libp2p/autonat/2/dial-back"); + +type Nonce = u64; diff --git a/protocols/autonat/src/v2/client.rs b/protocols/autonat/src/v2/client.rs new file mode 100644 index 00000000000..d3272512f35 --- /dev/null +++ b/protocols/autonat/src/v2/client.rs @@ -0,0 +1,5 @@ +mod behaviour; +mod handler; + +pub use behaviour::Event; +pub use behaviour::{Behaviour, Config}; diff --git a/protocols/autonat/src/v2/client/behaviour.rs b/protocols/autonat/src/v2/client/behaviour.rs new file mode 100644 index 00000000000..97509c05443 --- /dev/null +++ b/protocols/autonat/src/v2/client/behaviour.rs @@ -0,0 +1,439 @@ +use std::{ + collections::{HashMap, VecDeque}, + task::{Context, Poll}, + time::Duration, +}; + +use either::Either; +use futures::FutureExt; +use futures_timer::Delay; +use libp2p_core::{transport::PortUse, Endpoint, Multiaddr}; +use libp2p_identity::PeerId; +use libp2p_swarm::{ + behaviour::ConnectionEstablished, ConnectionClosed, ConnectionDenied, ConnectionHandler, + ConnectionId, FromSwarm, NetworkBehaviour, NewExternalAddrCandidate, NotifyHandler, ToSwarm, +}; +use rand::prelude::*; +use rand_core::OsRng; +use std::fmt::{Debug, Display, Formatter}; + +use crate::v2::{protocol::DialRequest, Nonce}; + +use super::handler::{ + dial_back::{self, IncomingNonce}, + dial_request, +}; + +#[derive(Debug, Clone, Copy)] +pub struct Config { + /// How many candidates we will test at most. + pub(crate) max_candidates: usize, + + /// The interval at which we will attempt to confirm candidates as external addresses. + pub(crate) probe_interval: Duration, +} + +impl Config { + pub fn with_max_candidates(self, max_candidates: usize) -> Self { + Self { + max_candidates, + ..self + } + } + + pub fn with_probe_interval(self, probe_interval: Duration) -> Self { + Self { + probe_interval, + ..self + } + } +} + +impl Default for Config { + fn default() -> Self { + Self { + max_candidates: 10, + probe_interval: Duration::from_secs(5), + } + } +} + +pub struct Behaviour +where + R: RngCore + 'static, +{ + rng: R, + config: Config, + pending_events: VecDeque< + ToSwarm< + ::ToSwarm, + <::ConnectionHandler as ConnectionHandler>::FromBehaviour, + >, + >, + address_candidates: HashMap, + next_tick: Delay, + peer_info: HashMap, +} + +impl NetworkBehaviour for Behaviour +where + R: RngCore + 'static, +{ + type ConnectionHandler = Either; + + type ToSwarm = Event; + + fn handle_established_inbound_connection( + &mut self, + _: ConnectionId, + _: PeerId, + _: &Multiaddr, + _: &Multiaddr, + ) -> Result<::ConnectionHandler, ConnectionDenied> { + Ok(Either::Right(dial_back::Handler::new())) + } + + fn handle_established_outbound_connection( + &mut self, + _: ConnectionId, + _: PeerId, + _: &Multiaddr, + _: Endpoint, + _: PortUse, + ) -> Result<::ConnectionHandler, ConnectionDenied> { + Ok(Either::Left(dial_request::Handler::new())) + } + + fn on_swarm_event(&mut self, event: FromSwarm) { + match event { + FromSwarm::NewExternalAddrCandidate(NewExternalAddrCandidate { addr }) => { + self.address_candidates + .entry(addr.clone()) + .or_default() + .score += 1; + } + FromSwarm::ConnectionEstablished(ConnectionEstablished { + peer_id, + connection_id, + endpoint: _, + .. + }) => { + self.peer_info.insert( + connection_id, + ConnectionInfo { + peer_id, + supports_autonat: false, + }, + ); + } + FromSwarm::ConnectionClosed(ConnectionClosed { + peer_id, + connection_id, + .. + }) => { + let info = self + .peer_info + .remove(&connection_id) + .expect("inconsistent state"); + + if info.supports_autonat { + tracing::debug!(%peer_id, "Disconnected from AutoNAT server"); + } + } + _ => {} + } + } + + fn on_connection_handler_event( + &mut self, + peer_id: PeerId, + connection_id: ConnectionId, + event: ::ToBehaviour, + ) { + let (nonce, outcome) = match event { + Either::Right(IncomingNonce { nonce, sender }) => { + let Some((_, info)) = self + .address_candidates + .iter_mut() + .find(|(_, info)| info.is_pending_with_nonce(nonce)) + else { + let _ = sender.send(Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!("Received unexpected nonce: {nonce} from {peer_id}"), + ))); + return; + }; + + info.status = TestStatus::Received(nonce); + tracing::debug!(%peer_id, %nonce, "Successful dial-back"); + + let _ = sender.send(Ok(())); + + return; + } + Either::Left(dial_request::ToBehaviour::PeerHasServerSupport) => { + self.peer_info + .get_mut(&connection_id) + .expect("inconsistent state") + .supports_autonat = true; + return; + } + Either::Left(dial_request::ToBehaviour::TestOutcome { nonce, outcome }) => { + (nonce, outcome) + } + }; + + let ((tested_addr, bytes_sent), result) = match outcome { + Ok(address) => { + let received_dial_back = self + .address_candidates + .iter_mut() + .any(|(_, info)| info.is_received_with_nonce(nonce)); + + if !received_dial_back { + tracing::warn!( + %peer_id, + %nonce, + "Server reported reachbility but we never received a dial-back" + ); + return; + } + + self.pending_events + .push_back(ToSwarm::ExternalAddrConfirmed(address.0.clone())); + + (address, Ok(())) + } + Err(dial_request::Error::UnsupportedProtocol) => { + self.peer_info + .get_mut(&connection_id) + .expect("inconsistent state") + .supports_autonat = false; + + self.reset_status_to(nonce, TestStatus::Untested); // Reset so it will be tried again. + + return; + } + Err(dial_request::Error::Io(e)) => { + tracing::debug!( + %peer_id, + %nonce, + "Failed to complete AutoNAT probe: {e}" + ); + + self.reset_status_to(nonce, TestStatus::Untested); // Reset so it will be tried again. + + return; + } + Err(dial_request::Error::AddressNotReachable { + address, + bytes_sent, + error, + }) => { + self.reset_status_to(nonce, TestStatus::Failed); + + ((address, bytes_sent), Err(error)) + } + }; + + self.pending_events.push_back(ToSwarm::GenerateEvent(Event { + tested_addr, + bytes_sent, + server: peer_id, + result: result.map_err(|e| Error { inner: e }), + })); + } + + fn poll( + &mut self, + cx: &mut Context<'_>, + ) -> Poll::FromBehaviour>> + { + loop { + if let Some(event) = self.pending_events.pop_front() { + return Poll::Ready(event); + } + + if self.next_tick.poll_unpin(cx).is_ready() { + self.next_tick.reset(self.config.probe_interval); + + self.issue_dial_requests_for_untested_candidates(); + continue; + } + + return Poll::Pending; + } + } +} + +impl Behaviour +where + R: RngCore + 'static, +{ + pub fn new(rng: R, config: Config) -> Self { + Self { + rng, + next_tick: Delay::new(config.probe_interval), + config, + pending_events: VecDeque::new(), + address_candidates: HashMap::new(), + peer_info: HashMap::new(), + } + } + + /// Issues dial requests to random AutoNAT servers for the most frequently reported, untested candidates. + /// + /// In the current implementation, we only send a single address to each AutoNAT server. + /// This spreads our candidates out across all servers we are connected to which should give us pretty fast feedback on all of them. + fn issue_dial_requests_for_untested_candidates(&mut self) { + for addr in self.untested_candidates() { + let Some((conn_id, peer_id)) = self.random_autonat_server() else { + tracing::debug!("Not connected to any AutoNAT servers"); + return; + }; + + let nonce = self.rng.gen(); + self.address_candidates + .get_mut(&addr) + .expect("only emit candidates") + .status = TestStatus::Pending(nonce); + + self.pending_events.push_back(ToSwarm::NotifyHandler { + peer_id, + handler: NotifyHandler::One(conn_id), + event: Either::Left(DialRequest { + nonce, + addrs: vec![addr], + }), + }); + } + } + + /// Returns all untested candidates, sorted by the frequency they were reported at. + /// + /// More frequently reported candidates are considered to more likely be external addresses and thus tested first. + fn untested_candidates(&self) -> impl Iterator { + let mut entries = self + .address_candidates + .iter() + .filter(|(_, info)| info.status == TestStatus::Untested) + .map(|(addr, count)| (addr.clone(), *count)) + .collect::>(); + + entries.sort_unstable_by_key(|(_, info)| info.score); + + if entries.is_empty() { + tracing::debug!("No untested address candidates"); + } + + entries + .into_iter() + .rev() // `sort_unstable` is ascending + .take(self.config.max_candidates) + .map(|(addr, _)| addr) + } + + /// Chooses an active connection to one of our peers that reported support for the [`DIAL_REQUEST_PROTOCOL`](crate::v2::DIAL_REQUEST_PROTOCOL) protocol. + fn random_autonat_server(&mut self) -> Option<(ConnectionId, PeerId)> { + let (conn_id, info) = self + .peer_info + .iter() + .filter(|(_, info)| info.supports_autonat) + .choose(&mut self.rng)?; + + Some((*conn_id, info.peer_id)) + } + + fn reset_status_to(&mut self, nonce: Nonce, new_status: TestStatus) { + let Some((_, info)) = self + .address_candidates + .iter_mut() + .find(|(_, i)| i.is_pending_with_nonce(nonce) || i.is_received_with_nonce(nonce)) + else { + return; + }; + + info.status = new_status; + } + + // FIXME: We don't want test-only APIs in our public API. + #[doc(hidden)] + pub fn validate_addr(&mut self, addr: &Multiaddr) { + if let Some(info) = self.address_candidates.get_mut(addr) { + info.status = TestStatus::Received(self.rng.next_u64()); + } + } +} + +impl Default for Behaviour { + fn default() -> Self { + Self::new(OsRng, Config::default()) + } +} + +pub struct Error { + pub(crate) inner: dial_request::DialBackError, +} + +impl Display for Error { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + Display::fmt(&self.inner, f) + } +} + +impl Debug for Error { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + Debug::fmt(&self.inner, f) + } +} + +#[derive(Debug)] +pub struct Event { + /// The address that was selected for testing. + pub tested_addr: Multiaddr, + /// The amount of data that was sent to the server. + /// Is 0 if it wasn't necessary to send any data. + /// Otherwise it's a number between 30.000 and 100.000. + pub bytes_sent: usize, + /// The peer id of the server that was selected for testing. + pub server: PeerId, + /// The result of the test. If the test was successful, this is `Ok(())`. + /// Otherwise it's an error. + pub result: Result<(), Error>, +} + +struct ConnectionInfo { + peer_id: PeerId, + supports_autonat: bool, +} + +#[derive(Copy, Clone, Default)] +struct AddressInfo { + score: usize, + status: TestStatus, +} + +impl AddressInfo { + fn is_pending_with_nonce(&self, nonce: Nonce) -> bool { + match self.status { + TestStatus::Pending(c) => c == nonce, + _ => false, + } + } + + fn is_received_with_nonce(&self, nonce: Nonce) -> bool { + match self.status { + TestStatus::Received(c) => c == nonce, + _ => false, + } + } +} + +#[derive(Clone, Copy, Default, PartialEq)] +enum TestStatus { + #[default] + Untested, + Pending(Nonce), + Failed, + Received(Nonce), +} diff --git a/protocols/autonat/src/v2/client/handler.rs b/protocols/autonat/src/v2/client/handler.rs new file mode 100644 index 00000000000..e526c2fb44c --- /dev/null +++ b/protocols/autonat/src/v2/client/handler.rs @@ -0,0 +1,2 @@ +pub(crate) mod dial_back; +pub(crate) mod dial_request; diff --git a/protocols/autonat/src/v2/client/handler/dial_back.rs b/protocols/autonat/src/v2/client/handler/dial_back.rs new file mode 100644 index 00000000000..98a41a82504 --- /dev/null +++ b/protocols/autonat/src/v2/client/handler/dial_back.rs @@ -0,0 +1,143 @@ +use std::{ + io, + task::{Context, Poll}, + time::Duration, +}; + +use futures::channel::oneshot; +use futures_bounded::StreamSet; +use libp2p_core::upgrade::{DeniedUpgrade, ReadyUpgrade}; +use libp2p_swarm::{ + handler::{ConnectionEvent, FullyNegotiatedInbound, ListenUpgradeError}, + ConnectionHandler, ConnectionHandlerEvent, StreamProtocol, SubstreamProtocol, +}; +use void::Void; + +use crate::v2::{protocol, Nonce, DIAL_BACK_PROTOCOL}; + +pub struct Handler { + inbound: StreamSet>, +} + +impl Handler { + pub(crate) fn new() -> Self { + Self { + inbound: StreamSet::new(Duration::from_secs(5), 2), + } + } +} + +impl ConnectionHandler for Handler { + type FromBehaviour = Void; + type ToBehaviour = IncomingNonce; + type InboundProtocol = ReadyUpgrade; + type OutboundProtocol = DeniedUpgrade; + type InboundOpenInfo = (); + type OutboundOpenInfo = (); + + fn listen_protocol(&self) -> SubstreamProtocol { + SubstreamProtocol::new(ReadyUpgrade::new(DIAL_BACK_PROTOCOL), ()) + } + + fn poll( + &mut self, + cx: &mut Context<'_>, + ) -> Poll< + ConnectionHandlerEvent, + > { + loop { + match self.inbound.poll_next_unpin(cx) { + Poll::Pending => return Poll::Pending, + Poll::Ready(None) => continue, + Poll::Ready(Some(Err(err))) => { + tracing::debug!("Stream timed out: {err}"); + continue; + } + Poll::Ready(Some(Ok(Err(err)))) => { + tracing::debug!("Dial back handler failed with: {err:?}"); + continue; + } + Poll::Ready(Some(Ok(Ok(incoming_nonce)))) => { + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(incoming_nonce)); + } + } + } + } + + fn on_behaviour_event(&mut self, _event: Self::FromBehaviour) {} + + fn on_connection_event( + &mut self, + event: ConnectionEvent< + Self::InboundProtocol, + Self::OutboundProtocol, + Self::InboundOpenInfo, + Self::OutboundOpenInfo, + >, + ) { + match event { + ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { + protocol, .. + }) => { + if self.inbound.try_push(perform_dial_back(protocol)).is_err() { + tracing::warn!("Dial back request dropped, too many requests in flight"); + } + } + // TODO: remove when Rust 1.82 is MSRVprotocols/autonat/src/v2/client/handler/dial_back.rs + #[allow(unreachable_patterns)] + ConnectionEvent::ListenUpgradeError(ListenUpgradeError { error, .. }) => { + void::unreachable(error); + } + _ => {} + } + } +} + +struct State { + stream: libp2p_swarm::Stream, + oneshot: Option>>, +} + +#[derive(Debug)] +pub struct IncomingNonce { + pub nonce: Nonce, + pub sender: oneshot::Sender>, +} + +fn perform_dial_back( + stream: libp2p_swarm::Stream, +) -> impl futures::Stream> { + let state = State { + stream, + oneshot: None, + }; + futures::stream::unfold(state, |mut state| async move { + if let Some(ref mut receiver) = state.oneshot { + match receiver.await { + Ok(Ok(())) => {} + Ok(Err(e)) => return Some((Err(e), state)), + Err(_) => { + return Some(( + Err(io::Error::new(io::ErrorKind::Other, "Sender got cancelled")), + state, + )); + } + } + if let Err(e) = protocol::dial_back_response(&mut state.stream).await { + return Some((Err(e), state)); + } + return None; + } + + let nonce = match protocol::recv_dial_back(&mut state.stream).await { + Ok(nonce) => nonce, + Err(err) => { + return Some((Err(err), state)); + } + }; + + let (sender, receiver) = oneshot::channel(); + state.oneshot = Some(receiver); + Some((Ok(IncomingNonce { nonce, sender }), state)) + }) +} diff --git a/protocols/autonat/src/v2/client/handler/dial_request.rs b/protocols/autonat/src/v2/client/handler/dial_request.rs new file mode 100644 index 00000000000..85ad176ec30 --- /dev/null +++ b/protocols/autonat/src/v2/client/handler/dial_request.rs @@ -0,0 +1,345 @@ +use futures::{channel::oneshot, AsyncWrite}; +use futures_bounded::FuturesMap; +use libp2p_core::{ + upgrade::{DeniedUpgrade, ReadyUpgrade}, + Multiaddr, +}; + +use libp2p_swarm::{ + handler::{ + ConnectionEvent, DialUpgradeError, FullyNegotiatedOutbound, OutboundUpgradeSend, + ProtocolsChange, + }, + ConnectionHandler, ConnectionHandlerEvent, Stream, StreamProtocol, StreamUpgradeError, + SubstreamProtocol, +}; +use std::{ + collections::VecDeque, + io, + iter::{once, repeat}, + task::{Context, Poll}, + time::Duration, +}; + +use crate::v2::{ + generated::structs::{mod_DialResponse::ResponseStatus, DialStatus}, + protocol::{ + Coder, DialDataRequest, DialDataResponse, DialRequest, Response, + DATA_FIELD_LEN_UPPER_BOUND, DATA_LEN_LOWER_BOUND, DATA_LEN_UPPER_BOUND, + }, + Nonce, DIAL_REQUEST_PROTOCOL, +}; + +#[derive(Debug)] +pub enum ToBehaviour { + TestOutcome { + nonce: Nonce, + outcome: Result<(Multiaddr, usize), Error>, + }, + PeerHasServerSupport, +} + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("Address is not reachable: {error}")] + AddressNotReachable { + address: Multiaddr, + bytes_sent: usize, + error: DialBackError, + }, + #[error("Peer does not support AutoNAT dial-request protocol")] + UnsupportedProtocol, + #[error("IO error: {0}")] + Io(io::Error), +} + +impl From for Error { + fn from(value: io::Error) -> Self { + Self::Io(value) + } +} + +#[derive(thiserror::Error, Debug)] +pub enum DialBackError { + #[error("server failed to establish a connection")] + NoConnection, + #[error("dial back stream failed")] + StreamFailed, +} + +pub struct Handler { + queued_events: VecDeque< + ConnectionHandlerEvent< + ::OutboundProtocol, + ::OutboundOpenInfo, + ::ToBehaviour, + >, + >, + outbound: FuturesMap>, + queued_streams: VecDeque< + oneshot::Sender< + Result< + Stream, + StreamUpgradeError< as OutboundUpgradeSend>::Error>, + >, + >, + >, +} + +impl Handler { + pub(crate) fn new() -> Self { + Self { + queued_events: VecDeque::new(), + outbound: FuturesMap::new(Duration::from_secs(10), 10), + queued_streams: VecDeque::default(), + } + } + + fn perform_request(&mut self, req: DialRequest) { + let (tx, rx) = oneshot::channel(); + self.queued_streams.push_back(tx); + self.queued_events + .push_back(ConnectionHandlerEvent::OutboundSubstreamRequest { + protocol: SubstreamProtocol::new(ReadyUpgrade::new(DIAL_REQUEST_PROTOCOL), ()), + }); + if self + .outbound + .try_push(req.nonce, start_stream_handle(req, rx)) + .is_err() + { + tracing::debug!("Dial request dropped, too many requests in flight"); + } + } +} + +impl ConnectionHandler for Handler { + type FromBehaviour = DialRequest; + type ToBehaviour = ToBehaviour; + type InboundProtocol = DeniedUpgrade; + type OutboundProtocol = ReadyUpgrade; + type InboundOpenInfo = (); + type OutboundOpenInfo = (); + + fn listen_protocol(&self) -> SubstreamProtocol { + SubstreamProtocol::new(DeniedUpgrade, ()) + } + + fn poll( + &mut self, + cx: &mut Context<'_>, + ) -> Poll< + ConnectionHandlerEvent, + > { + if let Some(event) = self.queued_events.pop_front() { + return Poll::Ready(event); + } + + match self.outbound.poll_unpin(cx) { + Poll::Ready((nonce, Ok(outcome))) => { + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + ToBehaviour::TestOutcome { nonce, outcome }, + )) + } + Poll::Ready((nonce, Err(_))) => { + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + ToBehaviour::TestOutcome { + nonce, + outcome: Err(Error::Io(io::ErrorKind::TimedOut.into())), + }, + )); + } + Poll::Pending => {} + } + + Poll::Pending + } + + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { + self.perform_request(event); + } + + fn on_connection_event( + &mut self, + event: ConnectionEvent< + Self::InboundProtocol, + Self::OutboundProtocol, + Self::InboundOpenInfo, + Self::OutboundOpenInfo, + >, + ) { + match event { + ConnectionEvent::DialUpgradeError(DialUpgradeError { error, .. }) => { + tracing::debug!("Dial request failed: {}", error); + match self.queued_streams.pop_front() { + Some(stream_tx) => { + let _ = stream_tx.send(Err(error)); + } + None => { + tracing::warn!( + "Opened unexpected substream without a pending dial request" + ); + } + } + } + ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { + protocol, .. + }) => match self.queued_streams.pop_front() { + Some(stream_tx) => { + if stream_tx.send(Ok(protocol)).is_err() { + tracing::debug!("Failed to send stream to dead handler"); + } + } + None => { + tracing::warn!("Opened unexpected substream without a pending dial request"); + } + }, + ConnectionEvent::RemoteProtocolsChange(ProtocolsChange::Added(mut added)) => { + if added.any(|p| p.as_ref() == DIAL_REQUEST_PROTOCOL) { + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + ToBehaviour::PeerHasServerSupport, + )); + } + } + _ => {} + } + } +} + +async fn start_stream_handle( + req: DialRequest, + stream_recv: oneshot::Receiver>>, +) -> Result<(Multiaddr, usize), Error> { + let stream = stream_recv + .await + .map_err(|_| io::Error::from(io::ErrorKind::BrokenPipe))? + .map_err(|e| match e { + StreamUpgradeError::NegotiationFailed => Error::UnsupportedProtocol, + StreamUpgradeError::Timeout => Error::Io(io::ErrorKind::TimedOut.into()), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] + StreamUpgradeError::Apply(v) => void::unreachable(v), + StreamUpgradeError::Io(e) => Error::Io(e), + })?; + + let mut coder = Coder::new(stream); + coder.send(req.clone()).await?; + + let (res, bytes_sent) = match coder.next().await? { + Response::Data(DialDataRequest { + addr_idx, + num_bytes, + }) => { + if addr_idx >= req.addrs.len() { + return Err(Error::Io(io::Error::new( + io::ErrorKind::InvalidInput, + "address index out of bounds", + ))); + } + if !(DATA_LEN_LOWER_BOUND..=DATA_LEN_UPPER_BOUND).contains(&num_bytes) { + return Err(Error::Io(io::Error::new( + io::ErrorKind::InvalidInput, + "requested bytes out of bounds", + ))); + } + + send_aap_data(&mut coder, num_bytes).await?; + + let Response::Dial(dial_response) = coder.next().await? else { + return Err(Error::Io(io::Error::new( + io::ErrorKind::InvalidInput, + "expected message", + ))); + }; + + (dial_response, num_bytes) + } + Response::Dial(dial_response) => (dial_response, 0), + }; + match coder.close().await { + Ok(_) => {} + Err(err) => { + if err.kind() == io::ErrorKind::ConnectionReset { + // The AutoNAT server may have already closed the stream (this is normal because the probe is finished), in this case we have this error: + // Err(Custom { kind: ConnectionReset, error: Stopped(0) }) + // so we silently ignore this error + } else { + return Err(err.into()); + } + } + } + + match res.status { + ResponseStatus::E_REQUEST_REJECTED => { + return Err(Error::Io(io::Error::new( + io::ErrorKind::Other, + "server rejected request", + ))) + } + ResponseStatus::E_DIAL_REFUSED => { + return Err(Error::Io(io::Error::new( + io::ErrorKind::Other, + "server refused dial", + ))) + } + ResponseStatus::E_INTERNAL_ERROR => { + return Err(Error::Io(io::Error::new( + io::ErrorKind::Other, + "server encountered internal error", + ))) + } + ResponseStatus::OK => {} + } + + let tested_address = req + .addrs + .get(res.addr_idx) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "address index out of bounds"))? + .clone(); + + match res.dial_status { + DialStatus::UNUSED => { + return Err(Error::Io(io::Error::new( + io::ErrorKind::InvalidInput, + "unexpected message", + ))) + } + DialStatus::E_DIAL_ERROR => { + return Err(Error::AddressNotReachable { + address: tested_address, + bytes_sent, + error: DialBackError::NoConnection, + }) + } + DialStatus::E_DIAL_BACK_ERROR => { + return Err(Error::AddressNotReachable { + address: tested_address, + bytes_sent, + error: DialBackError::StreamFailed, + }) + } + DialStatus::OK => {} + } + + Ok((tested_address, bytes_sent)) +} + +async fn send_aap_data(stream: &mut Coder, num_bytes: usize) -> io::Result<()> +where + I: AsyncWrite + Unpin, +{ + let count_full = num_bytes / DATA_FIELD_LEN_UPPER_BOUND; + let partial_len = num_bytes % DATA_FIELD_LEN_UPPER_BOUND; + for req in repeat(DATA_FIELD_LEN_UPPER_BOUND) + .take(count_full) + .chain(once(partial_len)) + .filter(|e| *e > 0) + .map(|data_count| { + DialDataResponse::new(data_count).expect("data count is unexpectedly too big") + }) + { + stream.send(req).await?; + } + + Ok(()) +} diff --git a/protocols/autonat/src/v2/generated/mod.rs b/protocols/autonat/src/v2/generated/mod.rs new file mode 100644 index 00000000000..e52c5a80bc0 --- /dev/null +++ b/protocols/autonat/src/v2/generated/mod.rs @@ -0,0 +1,2 @@ +// Automatically generated mod.rs +pub mod structs; diff --git a/protocols/autonat/src/v2/generated/structs.proto b/protocols/autonat/src/v2/generated/structs.proto new file mode 100644 index 00000000000..d298f43d047 --- /dev/null +++ b/protocols/autonat/src/v2/generated/structs.proto @@ -0,0 +1,54 @@ +syntax = "proto3"; + +package structs; + +message Message { + oneof msg { + DialRequest dialRequest = 1; + DialResponse dialResponse = 2; + DialDataRequest dialDataRequest = 3; + DialDataResponse dialDataResponse = 4; + } +} + +message DialRequest { + repeated bytes addrs = 1; + fixed64 nonce = 2; +} + +message DialDataRequest { + uint32 addrIdx = 1; + uint64 numBytes = 2; +} + +enum DialStatus { + UNUSED = 0; + E_DIAL_ERROR = 100; + E_DIAL_BACK_ERROR = 101; + OK = 200; +} + +message DialResponse { + enum ResponseStatus { + E_INTERNAL_ERROR = 0; + E_REQUEST_REJECTED = 100; + E_DIAL_REFUSED = 101; + OK = 200; + } + + ResponseStatus status = 1; + uint32 addrIdx = 2; + DialStatus dialStatus = 3; +} + +message DialDataResponse { bytes data = 1; } + +message DialBack { fixed64 nonce = 1; } + +message DialBackResponse { + enum DialBackStatus { + OK = 0; + } + + DialBackStatus status = 1; +} diff --git a/protocols/autonat/src/v2/generated/structs.rs b/protocols/autonat/src/v2/generated/structs.rs new file mode 100644 index 00000000000..e188adb8a42 --- /dev/null +++ b/protocols/autonat/src/v2/generated/structs.rs @@ -0,0 +1,403 @@ +// Automatically generated rust module for 'structs.proto' file + +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(unused_imports)] +#![allow(unknown_lints)] +#![allow(clippy::all)] +#![cfg_attr(rustfmt, rustfmt_skip)] + + +use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; +use quick_protobuf::sizeofs::*; +use super::*; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum DialStatus { + UNUSED = 0, + E_DIAL_ERROR = 100, + E_DIAL_BACK_ERROR = 101, + OK = 200, +} + +impl Default for DialStatus { + fn default() -> Self { + DialStatus::UNUSED + } +} + +impl From for DialStatus { + fn from(i: i32) -> Self { + match i { + 0 => DialStatus::UNUSED, + 100 => DialStatus::E_DIAL_ERROR, + 101 => DialStatus::E_DIAL_BACK_ERROR, + 200 => DialStatus::OK, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for DialStatus { + fn from(s: &'a str) -> Self { + match s { + "UNUSED" => DialStatus::UNUSED, + "E_DIAL_ERROR" => DialStatus::E_DIAL_ERROR, + "E_DIAL_BACK_ERROR" => DialStatus::E_DIAL_BACK_ERROR, + "OK" => DialStatus::OK, + _ => Self::default(), + } + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Message { + pub msg: structs::mod_Message::OneOfmsg, +} + +impl<'a> MessageRead<'a> for Message { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.msg = structs::mod_Message::OneOfmsg::dialRequest(r.read_message::(bytes)?), + Ok(18) => msg.msg = structs::mod_Message::OneOfmsg::dialResponse(r.read_message::(bytes)?), + Ok(26) => msg.msg = structs::mod_Message::OneOfmsg::dialDataRequest(r.read_message::(bytes)?), + Ok(34) => msg.msg = structs::mod_Message::OneOfmsg::dialDataResponse(r.read_message::(bytes)?), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for Message { + fn get_size(&self) -> usize { + 0 + + match self.msg { + structs::mod_Message::OneOfmsg::dialRequest(ref m) => 1 + sizeof_len((m).get_size()), + structs::mod_Message::OneOfmsg::dialResponse(ref m) => 1 + sizeof_len((m).get_size()), + structs::mod_Message::OneOfmsg::dialDataRequest(ref m) => 1 + sizeof_len((m).get_size()), + structs::mod_Message::OneOfmsg::dialDataResponse(ref m) => 1 + sizeof_len((m).get_size()), + structs::mod_Message::OneOfmsg::None => 0, + } } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + match self.msg { structs::mod_Message::OneOfmsg::dialRequest(ref m) => { w.write_with_tag(10, |w| w.write_message(m))? }, + structs::mod_Message::OneOfmsg::dialResponse(ref m) => { w.write_with_tag(18, |w| w.write_message(m))? }, + structs::mod_Message::OneOfmsg::dialDataRequest(ref m) => { w.write_with_tag(26, |w| w.write_message(m))? }, + structs::mod_Message::OneOfmsg::dialDataResponse(ref m) => { w.write_with_tag(34, |w| w.write_message(m))? }, + structs::mod_Message::OneOfmsg::None => {}, + } Ok(()) + } +} + +pub mod mod_Message { + +use super::*; + +#[derive(Debug, PartialEq, Clone)] +pub enum OneOfmsg { + dialRequest(structs::DialRequest), + dialResponse(structs::DialResponse), + dialDataRequest(structs::DialDataRequest), + dialDataResponse(structs::DialDataResponse), + None, +} + +impl Default for OneOfmsg { + fn default() -> Self { + OneOfmsg::None + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct DialRequest { + pub addrs: Vec>, + pub nonce: u64, +} + +impl<'a> MessageRead<'a> for DialRequest { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()), + Ok(17) => msg.nonce = r.read_fixed64(bytes)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for DialRequest { + fn get_size(&self) -> usize { + 0 + + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::() + + if self.nonce == 0u64 { 0 } else { 1 + 8 } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + for s in &self.addrs { w.write_with_tag(10, |w| w.write_bytes(&**s))?; } + if self.nonce != 0u64 { w.write_with_tag(17, |w| w.write_fixed64(*&self.nonce))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct DialDataRequest { + pub addrIdx: u32, + pub numBytes: u64, +} + +impl<'a> MessageRead<'a> for DialDataRequest { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.addrIdx = r.read_uint32(bytes)?, + Ok(16) => msg.numBytes = r.read_uint64(bytes)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for DialDataRequest { + fn get_size(&self) -> usize { + 0 + + if self.addrIdx == 0u32 { 0 } else { 1 + sizeof_varint(*(&self.addrIdx) as u64) } + + if self.numBytes == 0u64 { 0 } else { 1 + sizeof_varint(*(&self.numBytes) as u64) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.addrIdx != 0u32 { w.write_with_tag(8, |w| w.write_uint32(*&self.addrIdx))?; } + if self.numBytes != 0u64 { w.write_with_tag(16, |w| w.write_uint64(*&self.numBytes))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct DialResponse { + pub status: structs::mod_DialResponse::ResponseStatus, + pub addrIdx: u32, + pub dialStatus: structs::DialStatus, +} + +impl<'a> MessageRead<'a> for DialResponse { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.status = r.read_enum(bytes)?, + Ok(16) => msg.addrIdx = r.read_uint32(bytes)?, + Ok(24) => msg.dialStatus = r.read_enum(bytes)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for DialResponse { + fn get_size(&self) -> usize { + 0 + + if self.status == structs::mod_DialResponse::ResponseStatus::E_INTERNAL_ERROR { 0 } else { 1 + sizeof_varint(*(&self.status) as u64) } + + if self.addrIdx == 0u32 { 0 } else { 1 + sizeof_varint(*(&self.addrIdx) as u64) } + + if self.dialStatus == structs::DialStatus::UNUSED { 0 } else { 1 + sizeof_varint(*(&self.dialStatus) as u64) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.status != structs::mod_DialResponse::ResponseStatus::E_INTERNAL_ERROR { w.write_with_tag(8, |w| w.write_enum(*&self.status as i32))?; } + if self.addrIdx != 0u32 { w.write_with_tag(16, |w| w.write_uint32(*&self.addrIdx))?; } + if self.dialStatus != structs::DialStatus::UNUSED { w.write_with_tag(24, |w| w.write_enum(*&self.dialStatus as i32))?; } + Ok(()) + } +} + +pub mod mod_DialResponse { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum ResponseStatus { + E_INTERNAL_ERROR = 0, + E_REQUEST_REJECTED = 100, + E_DIAL_REFUSED = 101, + OK = 200, +} + +impl Default for ResponseStatus { + fn default() -> Self { + ResponseStatus::E_INTERNAL_ERROR + } +} + +impl From for ResponseStatus { + fn from(i: i32) -> Self { + match i { + 0 => ResponseStatus::E_INTERNAL_ERROR, + 100 => ResponseStatus::E_REQUEST_REJECTED, + 101 => ResponseStatus::E_DIAL_REFUSED, + 200 => ResponseStatus::OK, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for ResponseStatus { + fn from(s: &'a str) -> Self { + match s { + "E_INTERNAL_ERROR" => ResponseStatus::E_INTERNAL_ERROR, + "E_REQUEST_REJECTED" => ResponseStatus::E_REQUEST_REJECTED, + "E_DIAL_REFUSED" => ResponseStatus::E_DIAL_REFUSED, + "OK" => ResponseStatus::OK, + _ => Self::default(), + } + } +} + +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct DialDataResponse { + pub data: Vec, +} + +impl<'a> MessageRead<'a> for DialDataResponse { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(10) => msg.data = r.read_bytes(bytes)?.to_owned(), + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for DialDataResponse { + fn get_size(&self) -> usize { + 0 + + if self.data.is_empty() { 0 } else { 1 + sizeof_len((&self.data).len()) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if !self.data.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.data))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct DialBack { + pub nonce: u64, +} + +impl<'a> MessageRead<'a> for DialBack { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(9) => msg.nonce = r.read_fixed64(bytes)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for DialBack { + fn get_size(&self) -> usize { + 0 + + if self.nonce == 0u64 { 0 } else { 1 + 8 } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.nonce != 0u64 { w.write_with_tag(9, |w| w.write_fixed64(*&self.nonce))?; } + Ok(()) + } +} + +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Debug, Default, PartialEq, Clone)] +pub struct DialBackResponse { + pub status: structs::mod_DialBackResponse::DialBackStatus, +} + +impl<'a> MessageRead<'a> for DialBackResponse { + fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result { + let mut msg = Self::default(); + while !r.is_eof() { + match r.next_tag(bytes) { + Ok(8) => msg.status = r.read_enum(bytes)?, + Ok(t) => { r.read_unknown(bytes, t)?; } + Err(e) => return Err(e), + } + } + Ok(msg) + } +} + +impl MessageWrite for DialBackResponse { + fn get_size(&self) -> usize { + 0 + + if self.status == structs::mod_DialBackResponse::DialBackStatus::OK { 0 } else { 1 + sizeof_varint(*(&self.status) as u64) } + } + + fn write_message(&self, w: &mut Writer) -> Result<()> { + if self.status != structs::mod_DialBackResponse::DialBackStatus::OK { w.write_with_tag(8, |w| w.write_enum(*&self.status as i32))?; } + Ok(()) + } +} + +pub mod mod_DialBackResponse { + + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum DialBackStatus { + OK = 0, +} + +impl Default for DialBackStatus { + fn default() -> Self { + DialBackStatus::OK + } +} + +impl From for DialBackStatus { + fn from(i: i32) -> Self { + match i { + 0 => DialBackStatus::OK, + _ => Self::default(), + } + } +} + +impl<'a> From<&'a str> for DialBackStatus { + fn from(s: &'a str) -> Self { + match s { + "OK" => DialBackStatus::OK, + _ => Self::default(), + } + } +} + +} + diff --git a/protocols/autonat/src/v2/protocol.rs b/protocols/autonat/src/v2/protocol.rs new file mode 100644 index 00000000000..4077fd65f5d --- /dev/null +++ b/protocols/autonat/src/v2/protocol.rs @@ -0,0 +1,337 @@ +// change to quick-protobuf-codec + +use std::io; +use std::io::ErrorKind; + +use asynchronous_codec::{Framed, FramedRead, FramedWrite}; + +use futures::{AsyncRead, AsyncWrite, SinkExt, StreamExt}; +use libp2p_core::Multiaddr; + +use quick_protobuf_codec::Codec; +use rand::Rng; + +use crate::v2::{generated::structs as proto, Nonce}; + +const REQUEST_MAX_SIZE: usize = 4104; +pub(super) const DATA_LEN_LOWER_BOUND: usize = 30_000u32 as usize; +pub(super) const DATA_LEN_UPPER_BOUND: usize = 100_000u32 as usize; +pub(super) const DATA_FIELD_LEN_UPPER_BOUND: usize = 4096; + +fn new_io_invalid_data_err(msg: impl Into) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, msg.into()) +} + +pub(crate) struct Coder { + inner: Framed>, +} + +impl Coder +where + I: AsyncWrite + AsyncRead + Unpin, +{ + pub(crate) fn new(io: I) -> Self { + Self { + inner: Framed::new(io, Codec::new(REQUEST_MAX_SIZE)), + } + } + pub(crate) async fn close(mut self) -> io::Result<()> { + self.inner.close().await?; + Ok(()) + } +} + +impl Coder +where + I: AsyncRead + Unpin, +{ + pub(crate) async fn next(&mut self) -> io::Result + where + proto::Message: TryInto, + io::Error: From, + { + Ok(self.next_msg().await?.try_into()?) + } + + async fn next_msg(&mut self) -> io::Result { + self.inner + .next() + .await + .ok_or(io::Error::new( + ErrorKind::UnexpectedEof, + "no request to read", + ))? + .map_err(|e| io::Error::new(ErrorKind::InvalidData, e)) + } +} + +impl Coder +where + I: AsyncWrite + Unpin, +{ + pub(crate) async fn send(&mut self, msg: M) -> io::Result<()> + where + M: Into, + { + self.inner.send(msg.into()).await?; + Ok(()) + } +} + +#[derive(Debug, Clone, PartialEq)] +pub(crate) enum Request { + Dial(DialRequest), + Data(DialDataResponse), +} + +impl From for proto::Message { + fn from(val: DialRequest) -> Self { + let addrs = val.addrs.iter().map(|e| e.to_vec()).collect(); + let nonce = val.nonce; + + proto::Message { + msg: proto::mod_Message::OneOfmsg::dialRequest(proto::DialRequest { addrs, nonce }), + } + } +} + +impl From for proto::Message { + fn from(val: DialDataResponse) -> Self { + debug_assert!( + val.data_count <= DATA_FIELD_LEN_UPPER_BOUND, + "data_count too large" + ); + proto::Message { + msg: proto::mod_Message::OneOfmsg::dialDataResponse(proto::DialDataResponse { + data: vec![0; val.data_count], // One could use Cow::Borrowed here, but it will require a modification of the generated code and that will fail the CI + }), + } + } +} + +#[derive(Debug, Clone, PartialEq)] +pub struct DialRequest { + pub(crate) addrs: Vec, + pub(crate) nonce: u64, +} + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct DialDataResponse { + data_count: usize, +} + +impl DialDataResponse { + pub(crate) fn new(data_count: usize) -> Option { + if data_count <= DATA_FIELD_LEN_UPPER_BOUND { + Some(Self { data_count }) + } else { + None + } + } + + pub(crate) fn get_data_count(&self) -> usize { + self.data_count + } +} + +impl TryFrom for Request { + type Error = io::Error; + + fn try_from(msg: proto::Message) -> Result { + match msg.msg { + proto::mod_Message::OneOfmsg::dialRequest(proto::DialRequest { addrs, nonce }) => { + let addrs = addrs + .into_iter() + .map(|e| e.to_vec()) + .map(|e| { + Multiaddr::try_from(e).map_err(|err| { + new_io_invalid_data_err(format!("invalid multiaddr: {}", err)) + }) + }) + .collect::, io::Error>>()?; + Ok(Self::Dial(DialRequest { addrs, nonce })) + } + proto::mod_Message::OneOfmsg::dialDataResponse(proto::DialDataResponse { data }) => { + let data_count = data.len(); + Ok(Self::Data(DialDataResponse { data_count })) + } + _ => Err(new_io_invalid_data_err( + "expected dialResponse or dialDataRequest", + )), + } + } +} + +#[derive(Debug, Clone)] +pub(crate) enum Response { + Dial(DialResponse), + Data(DialDataRequest), +} + +#[derive(Debug, Clone)] +pub(crate) struct DialDataRequest { + pub(crate) addr_idx: usize, + pub(crate) num_bytes: usize, +} + +#[derive(Debug, Clone)] +pub(crate) struct DialResponse { + pub(crate) status: proto::mod_DialResponse::ResponseStatus, + pub(crate) addr_idx: usize, + pub(crate) dial_status: proto::DialStatus, +} + +impl TryFrom for Response { + type Error = io::Error; + + fn try_from(msg: proto::Message) -> Result { + match msg.msg { + proto::mod_Message::OneOfmsg::dialResponse(proto::DialResponse { + status, + addrIdx, + dialStatus, + }) => Ok(Response::Dial(DialResponse { + status, + addr_idx: addrIdx as usize, + dial_status: dialStatus, + })), + proto::mod_Message::OneOfmsg::dialDataRequest(proto::DialDataRequest { + addrIdx, + numBytes, + }) => Ok(Self::Data(DialDataRequest { + addr_idx: addrIdx as usize, + num_bytes: numBytes as usize, + })), + _ => Err(new_io_invalid_data_err( + "invalid message type, expected dialResponse or dialDataRequest", + )), + } + } +} + +impl From for proto::Message { + fn from(val: Response) -> Self { + match val { + Response::Dial(DialResponse { + status, + addr_idx, + dial_status, + }) => proto::Message { + msg: proto::mod_Message::OneOfmsg::dialResponse(proto::DialResponse { + status, + addrIdx: addr_idx as u32, + dialStatus: dial_status, + }), + }, + Response::Data(DialDataRequest { + addr_idx, + num_bytes, + }) => proto::Message { + msg: proto::mod_Message::OneOfmsg::dialDataRequest(proto::DialDataRequest { + addrIdx: addr_idx as u32, + numBytes: num_bytes as u64, + }), + }, + } + } +} + +impl DialDataRequest { + pub(crate) fn from_rng(addr_idx: usize, mut rng: R) -> Self { + let num_bytes = rng.gen_range(DATA_LEN_LOWER_BOUND..=DATA_LEN_UPPER_BOUND); + Self { + addr_idx, + num_bytes, + } + } +} + +const DIAL_BACK_MAX_SIZE: usize = 10; + +pub(crate) async fn dial_back(stream: impl AsyncWrite + Unpin, nonce: Nonce) -> io::Result<()> { + let msg = proto::DialBack { nonce }; + let mut framed = FramedWrite::new(stream, Codec::::new(DIAL_BACK_MAX_SIZE)); + + framed + .send(msg) + .await + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + + Ok(()) +} + +pub(crate) async fn recv_dial_back(stream: impl AsyncRead + Unpin) -> io::Result { + let framed = &mut FramedRead::new(stream, Codec::::new(DIAL_BACK_MAX_SIZE)); + let proto::DialBack { nonce } = framed + .next() + .await + .ok_or(io::Error::from(io::ErrorKind::UnexpectedEof))??; + Ok(nonce) +} + +pub(crate) async fn dial_back_response(stream: impl AsyncWrite + Unpin) -> io::Result<()> { + let msg = proto::DialBackResponse { + status: proto::mod_DialBackResponse::DialBackStatus::OK, + }; + let mut framed = FramedWrite::new( + stream, + Codec::::new(DIAL_BACK_MAX_SIZE), + ); + framed + .send(msg) + .await + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + + Ok(()) +} + +pub(crate) async fn recv_dial_back_response( + stream: impl AsyncRead + AsyncWrite + Unpin, +) -> io::Result<()> { + let framed = &mut FramedRead::new( + stream, + Codec::::new(DIAL_BACK_MAX_SIZE), + ); + let proto::DialBackResponse { status } = framed + .next() + .await + .ok_or(io::Error::from(io::ErrorKind::UnexpectedEof))??; + + if proto::mod_DialBackResponse::DialBackStatus::OK == status { + Ok(()) + } else { + Err(io::Error::new( + io::ErrorKind::InvalidData, + "invalid dial back response", + )) + } +} + +#[cfg(test)] +mod tests { + use crate::v2::generated::structs::{ + mod_Message::OneOfmsg, DialDataResponse as GenDialDataResponse, Message, + }; + + #[test] + fn message_correct_max_size() { + let message_bytes = quick_protobuf::serialize_into_vec(&Message { + msg: OneOfmsg::dialDataResponse(GenDialDataResponse { + data: vec![0; 4096], + }), + }) + .unwrap(); + assert_eq!(message_bytes.len(), super::REQUEST_MAX_SIZE); + } + + #[test] + fn dial_back_correct_size() { + let dial_back = super::proto::DialBack { nonce: 0 }; + let buf = quick_protobuf::serialize_into_vec(&dial_back).unwrap(); + assert!(buf.len() <= super::DIAL_BACK_MAX_SIZE); + + let dial_back_max_nonce = super::proto::DialBack { nonce: u64::MAX }; + let buf = quick_protobuf::serialize_into_vec(&dial_back_max_nonce).unwrap(); + assert!(buf.len() <= super::DIAL_BACK_MAX_SIZE); + } +} diff --git a/protocols/autonat/src/v2/server.rs b/protocols/autonat/src/v2/server.rs new file mode 100644 index 00000000000..25819307784 --- /dev/null +++ b/protocols/autonat/src/v2/server.rs @@ -0,0 +1,5 @@ +mod behaviour; +mod handler; + +pub use behaviour::Behaviour; +pub use behaviour::Event; diff --git a/protocols/autonat/src/v2/server/behaviour.rs b/protocols/autonat/src/v2/server/behaviour.rs new file mode 100644 index 00000000000..9264c728fe4 --- /dev/null +++ b/protocols/autonat/src/v2/server/behaviour.rs @@ -0,0 +1,158 @@ +use std::{ + collections::{HashMap, VecDeque}, + io, + task::{Context, Poll}, +}; + +use crate::v2::server::handler::dial_request::DialBackStatus; +use either::Either; +use libp2p_core::{transport::PortUse, Endpoint, Multiaddr}; +use libp2p_identity::PeerId; +use libp2p_swarm::dial_opts::PeerCondition; +use libp2p_swarm::{ + dial_opts::DialOpts, dummy, ConnectionDenied, ConnectionHandler, ConnectionId, DialFailure, + FromSwarm, NetworkBehaviour, ToSwarm, +}; +use rand_core::{OsRng, RngCore}; + +use crate::v2::server::handler::{ + dial_back, + dial_request::{self, DialBackCommand}, + Handler, +}; + +pub struct Behaviour +where + R: Clone + Send + RngCore + 'static, +{ + dialing_dial_back: HashMap, + pending_events: VecDeque< + ToSwarm< + ::ToSwarm, + <::ConnectionHandler as ConnectionHandler>::FromBehaviour, + >, + >, + rng: R, +} + +impl Default for Behaviour { + fn default() -> Self { + Self::new(OsRng) + } +} + +impl Behaviour +where + R: RngCore + Send + Clone + 'static, +{ + pub fn new(rng: R) -> Self { + Self { + dialing_dial_back: HashMap::new(), + pending_events: VecDeque::new(), + rng, + } + } +} + +impl NetworkBehaviour for Behaviour +where + R: RngCore + Send + Clone + 'static, +{ + type ConnectionHandler = Handler; + + type ToSwarm = Event; + + fn handle_established_inbound_connection( + &mut self, + _connection_id: ConnectionId, + peer: PeerId, + _local_addr: &Multiaddr, + remote_addr: &Multiaddr, + ) -> Result<::ConnectionHandler, ConnectionDenied> { + Ok(Either::Right(dial_request::Handler::new( + peer, + remote_addr.clone(), + self.rng.clone(), + ))) + } + + fn handle_established_outbound_connection( + &mut self, + connection_id: ConnectionId, + _peer: PeerId, + _addr: &Multiaddr, + _role_override: Endpoint, + _port_use: PortUse, + ) -> Result<::ConnectionHandler, ConnectionDenied> { + Ok(match self.dialing_dial_back.remove(&connection_id) { + Some(cmd) => Either::Left(Either::Left(dial_back::Handler::new(cmd))), + None => Either::Left(Either::Right(dummy::ConnectionHandler)), + }) + } + + fn on_swarm_event(&mut self, event: FromSwarm) { + if let FromSwarm::DialFailure(DialFailure { connection_id, .. }) = event { + if let Some(DialBackCommand { back_channel, .. }) = + self.dialing_dial_back.remove(&connection_id) + { + let dial_back_status = DialBackStatus::DialErr; + let _ = back_channel.send(Err(dial_back_status)); + } + } + } + + fn on_connection_handler_event( + &mut self, + peer_id: PeerId, + _connection_id: ConnectionId, + event: as ConnectionHandler>::ToBehaviour, + ) { + match event { + Either::Left(Either::Left(Ok(_))) => {} + Either::Left(Either::Left(Err(e))) => { + tracing::debug!("dial back error: {e:?}"); + } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] + Either::Left(Either::Right(v)) => void::unreachable(v), + Either::Right(Either::Left(cmd)) => { + let addr = cmd.addr.clone(); + let opts = DialOpts::peer_id(peer_id) + .addresses(Vec::from([addr])) + .condition(PeerCondition::Always) + .allocate_new_port() + .build(); + let conn_id = opts.connection_id(); + self.dialing_dial_back.insert(conn_id, cmd); + self.pending_events.push_back(ToSwarm::Dial { opts }); + } + Either::Right(Either::Right(status_update)) => self + .pending_events + .push_back(ToSwarm::GenerateEvent(status_update)), + } + } + + fn poll( + &mut self, + _cx: &mut Context<'_>, + ) -> Poll as ConnectionHandler>::FromBehaviour>> { + if let Some(event) = self.pending_events.pop_front() { + return Poll::Ready(event); + } + Poll::Pending + } +} + +#[derive(Debug)] +pub struct Event { + /// All address that were submitted for testing. + pub all_addrs: Vec, + /// The address that was eventually tested. + pub tested_addr: Multiaddr, + /// The peer id of the client that submitted addresses for testing. + pub client: PeerId, + /// The amount of data that was requested by the server and was transmitted. + pub data_amount: usize, + /// The result of the test. + pub result: Result<(), io::Error>, +} diff --git a/protocols/autonat/src/v2/server/handler.rs b/protocols/autonat/src/v2/server/handler.rs new file mode 100644 index 00000000000..ffdad69c86f --- /dev/null +++ b/protocols/autonat/src/v2/server/handler.rs @@ -0,0 +1,8 @@ +use either::Either; +use libp2p_swarm::dummy; + +pub(crate) mod dial_back; +pub(crate) mod dial_request; + +pub(crate) type Handler = + Either, dial_request::Handler>; diff --git a/protocols/autonat/src/v2/server/handler/dial_back.rs b/protocols/autonat/src/v2/server/handler/dial_back.rs new file mode 100644 index 00000000000..3cacd4ff32b --- /dev/null +++ b/protocols/autonat/src/v2/server/handler/dial_back.rs @@ -0,0 +1,140 @@ +use std::{ + convert::identity, + io, + task::{Context, Poll}, + time::Duration, +}; + +use futures::{AsyncRead, AsyncWrite}; +use futures_bounded::FuturesSet; +use libp2p_core::upgrade::{DeniedUpgrade, ReadyUpgrade}; +use libp2p_swarm::{ + handler::{ConnectionEvent, DialUpgradeError, FullyNegotiatedOutbound}, + ConnectionHandler, ConnectionHandlerEvent, StreamProtocol, StreamUpgradeError, + SubstreamProtocol, +}; + +use crate::v2::{ + protocol::{dial_back, recv_dial_back_response}, + DIAL_BACK_PROTOCOL, +}; + +use super::dial_request::{DialBackCommand, DialBackStatus as DialBackRes}; + +pub(crate) type ToBehaviour = io::Result<()>; + +pub struct Handler { + pending_nonce: Option, + requested_substream_nonce: Option, + outbound: FuturesSet, +} + +impl Handler { + pub(crate) fn new(cmd: DialBackCommand) -> Self { + Self { + pending_nonce: Some(cmd), + requested_substream_nonce: None, + outbound: FuturesSet::new(Duration::from_secs(10), 5), + } + } +} + +impl ConnectionHandler for Handler { + type FromBehaviour = (); + type ToBehaviour = ToBehaviour; + type InboundProtocol = DeniedUpgrade; + type OutboundProtocol = ReadyUpgrade; + type InboundOpenInfo = (); + type OutboundOpenInfo = (); + + fn listen_protocol(&self) -> SubstreamProtocol { + SubstreamProtocol::new(DeniedUpgrade, ()) + } + + fn poll( + &mut self, + cx: &mut Context<'_>, + ) -> Poll< + ConnectionHandlerEvent, + > { + if let Poll::Ready(result) = self.outbound.poll_unpin(cx) { + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + result + .map_err(|timeout| io::Error::new(io::ErrorKind::TimedOut, timeout)) + .and_then(identity), + )); + } + if let Some(cmd) = self.pending_nonce.take() { + self.requested_substream_nonce = Some(cmd); + return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { + protocol: SubstreamProtocol::new(ReadyUpgrade::new(DIAL_BACK_PROTOCOL), ()), + }); + } + Poll::Pending + } + + fn on_behaviour_event(&mut self, _event: Self::FromBehaviour) {} + + fn on_connection_event( + &mut self, + event: ConnectionEvent< + Self::InboundProtocol, + Self::OutboundProtocol, + Self::InboundOpenInfo, + Self::OutboundOpenInfo, + >, + ) { + match event { + ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { + protocol, .. + }) => { + if let Some(cmd) = self.requested_substream_nonce.take() { + if self + .outbound + .try_push(perform_dial_back(protocol, cmd)) + .is_err() + { + tracing::warn!("Dial back dropped, too many requests in flight"); + } + } else { + tracing::warn!("received dial back substream without nonce"); + } + } + ConnectionEvent::DialUpgradeError(DialUpgradeError { + error: StreamUpgradeError::NegotiationFailed | StreamUpgradeError::Timeout, + .. + }) => { + if let Some(cmd) = self.requested_substream_nonce.take() { + let _ = cmd.back_channel.send(Err(DialBackRes::DialBackErr)); + } + } + _ => {} + } + } +} + +async fn perform_dial_back( + mut stream: impl AsyncRead + AsyncWrite + Unpin, + DialBackCommand { + nonce, + back_channel, + .. + }: DialBackCommand, +) -> io::Result<()> { + let res = dial_back(&mut stream, nonce) + .await + .map_err(|_| DialBackRes::DialBackErr) + .map(|_| ()); + + let res = match res { + Ok(()) => recv_dial_back_response(stream) + .await + .map_err(|_| DialBackRes::DialBackErr) + .map(|_| ()), + Err(e) => Err(e), + }; + back_channel + .send(res) + .map_err(|_| io::Error::new(io::ErrorKind::Other, "send error"))?; + Ok(()) +} diff --git a/protocols/autonat/src/v2/server/handler/dial_request.rs b/protocols/autonat/src/v2/server/handler/dial_request.rs new file mode 100644 index 00000000000..14ddb153416 --- /dev/null +++ b/protocols/autonat/src/v2/server/handler/dial_request.rs @@ -0,0 +1,334 @@ +use std::{ + io, + task::{Context, Poll}, + time::Duration, +}; + +use either::Either; +use futures::{ + channel::{mpsc, oneshot}, + AsyncRead, AsyncWrite, SinkExt, StreamExt, +}; +use futures_bounded::FuturesSet; +use libp2p_core::{ + upgrade::{DeniedUpgrade, ReadyUpgrade}, + Multiaddr, +}; +use libp2p_identity::PeerId; +use libp2p_swarm::{ + handler::{ConnectionEvent, FullyNegotiatedInbound, ListenUpgradeError}, + ConnectionHandler, ConnectionHandlerEvent, StreamProtocol, SubstreamProtocol, +}; +use rand_core::RngCore; + +use crate::v2::{ + generated::structs::{mod_DialResponse::ResponseStatus, DialStatus}, + protocol::{Coder, DialDataRequest, DialRequest, DialResponse, Request, Response}, + server::behaviour::Event, + Nonce, DIAL_REQUEST_PROTOCOL, +}; + +#[derive(Debug, PartialEq)] +pub(crate) enum DialBackStatus { + /// Failure during dial + DialErr, + /// Failure during dial back + DialBackErr, +} + +#[derive(Debug)] +pub struct DialBackCommand { + pub(crate) addr: Multiaddr, + pub(crate) nonce: Nonce, + pub(crate) back_channel: oneshot::Sender>, +} + +pub struct Handler { + client_id: PeerId, + observed_multiaddr: Multiaddr, + dial_back_cmd_sender: mpsc::Sender, + dial_back_cmd_receiver: mpsc::Receiver, + inbound: FuturesSet, + rng: R, +} + +impl Handler +where + R: RngCore, +{ + pub(crate) fn new(client_id: PeerId, observed_multiaddr: Multiaddr, rng: R) -> Self { + let (dial_back_cmd_sender, dial_back_cmd_receiver) = mpsc::channel(10); + Self { + client_id, + observed_multiaddr, + dial_back_cmd_sender, + dial_back_cmd_receiver, + inbound: FuturesSet::new(Duration::from_secs(10), 10), + rng, + } + } +} + +impl ConnectionHandler for Handler +where + R: RngCore + Send + Clone + 'static, +{ + type FromBehaviour = void::Void; + type ToBehaviour = Either; + type InboundProtocol = ReadyUpgrade; + type OutboundProtocol = DeniedUpgrade; + type InboundOpenInfo = (); + type OutboundOpenInfo = (); + + fn listen_protocol(&self) -> SubstreamProtocol { + SubstreamProtocol::new(ReadyUpgrade::new(DIAL_REQUEST_PROTOCOL), ()) + } + + fn poll( + &mut self, + cx: &mut Context<'_>, + ) -> Poll< + ConnectionHandlerEvent, + > { + loop { + match self.inbound.poll_unpin(cx) { + Poll::Ready(Ok(event)) => { + if let Err(e) = &event.result { + tracing::warn!("inbound request handle failed: {:?}", e); + } + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Either::Right( + event, + ))); + } + Poll::Ready(Err(e)) => { + tracing::warn!("inbound request handle timed out {e:?}"); + } + Poll::Pending => break, + } + } + if let Poll::Ready(Some(cmd)) = self.dial_back_cmd_receiver.poll_next_unpin(cx) { + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Either::Left(cmd))); + } + Poll::Pending + } + + fn on_behaviour_event(&mut self, _event: Self::FromBehaviour) {} + + fn on_connection_event( + &mut self, + event: ConnectionEvent< + Self::InboundProtocol, + Self::OutboundProtocol, + Self::InboundOpenInfo, + Self::OutboundOpenInfo, + >, + ) { + match event { + ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { + protocol, .. + }) => { + if self + .inbound + .try_push(handle_request( + protocol, + self.observed_multiaddr.clone(), + self.client_id, + self.dial_back_cmd_sender.clone(), + self.rng.clone(), + )) + .is_err() + { + tracing::warn!( + "failed to push inbound request handler, too many requests in flight" + ); + } + } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] + ConnectionEvent::ListenUpgradeError(ListenUpgradeError { error, .. }) => { + tracing::debug!("inbound request failed: {:?}", error); + } + _ => {} + } + } +} + +enum HandleFail { + InternalError(usize), + RequestRejected, + DialRefused, + DialBack { + idx: usize, + result: Result<(), DialBackStatus>, + }, +} + +impl From for DialResponse { + fn from(value: HandleFail) -> Self { + match value { + HandleFail::InternalError(addr_idx) => Self { + status: ResponseStatus::E_INTERNAL_ERROR, + addr_idx, + dial_status: DialStatus::UNUSED, + }, + HandleFail::RequestRejected => Self { + status: ResponseStatus::E_REQUEST_REJECTED, + addr_idx: 0, + dial_status: DialStatus::UNUSED, + }, + HandleFail::DialRefused => Self { + status: ResponseStatus::E_DIAL_REFUSED, + addr_idx: 0, + dial_status: DialStatus::UNUSED, + }, + HandleFail::DialBack { idx, result } => Self { + status: ResponseStatus::OK, + addr_idx: idx, + dial_status: match result { + Err(DialBackStatus::DialErr) => DialStatus::E_DIAL_ERROR, + Err(DialBackStatus::DialBackErr) => DialStatus::E_DIAL_BACK_ERROR, + Ok(()) => DialStatus::OK, + }, + }, + } + } +} + +async fn handle_request( + stream: impl AsyncRead + AsyncWrite + Unpin, + observed_multiaddr: Multiaddr, + client: PeerId, + dial_back_cmd_sender: mpsc::Sender, + rng: impl RngCore, +) -> Event { + let mut coder = Coder::new(stream); + let mut all_addrs = Vec::new(); + let mut tested_addr_opt = None; + let mut data_amount = 0; + let response = handle_request_internal( + &mut coder, + observed_multiaddr.clone(), + dial_back_cmd_sender, + rng, + &mut all_addrs, + &mut tested_addr_opt, + &mut data_amount, + ) + .await + .unwrap_or_else(|e| e.into()); + let Some(tested_addr) = tested_addr_opt else { + return Event { + all_addrs, + tested_addr: observed_multiaddr, + client, + data_amount, + result: Err(io::Error::new( + io::ErrorKind::Other, + "client is not conformint to protocol. the tested address is not the observed address", + )), + }; + }; + if let Err(e) = coder.send(Response::Dial(response)).await { + return Event { + all_addrs, + tested_addr, + client, + data_amount, + result: Err(e), + }; + } + if let Err(e) = coder.close().await { + return Event { + all_addrs, + tested_addr, + client, + data_amount, + result: Err(e), + }; + } + Event { + all_addrs, + tested_addr, + client, + data_amount, + result: Ok(()), + } +} + +async fn handle_request_internal( + coder: &mut Coder, + observed_multiaddr: Multiaddr, + dial_back_cmd_sender: mpsc::Sender, + mut rng: impl RngCore, + all_addrs: &mut Vec, + tested_addrs: &mut Option, + data_amount: &mut usize, +) -> Result +where + I: AsyncRead + AsyncWrite + Unpin, +{ + let DialRequest { mut addrs, nonce } = match coder + .next() + .await + .map_err(|_| HandleFail::InternalError(0))? + { + Request::Dial(dial_request) => dial_request, + Request::Data(_) => { + return Err(HandleFail::RequestRejected); + } + }; + all_addrs.clone_from(&addrs); + let idx = 0; + let addr = addrs.pop().ok_or(HandleFail::DialRefused)?; + *tested_addrs = Some(addr.clone()); + *data_amount = 0; + if addr != observed_multiaddr { + let dial_data_request = DialDataRequest::from_rng(idx, &mut rng); + let mut rem_data = dial_data_request.num_bytes; + coder + .send(Response::Data(dial_data_request)) + .await + .map_err(|_| HandleFail::InternalError(idx))?; + while rem_data > 0 { + let data_count = match coder + .next() + .await + .map_err(|_e| HandleFail::InternalError(idx))? + { + Request::Dial(_) => { + return Err(HandleFail::RequestRejected); + } + Request::Data(dial_data_response) => dial_data_response.get_data_count(), + }; + rem_data = rem_data.saturating_sub(data_count); + *data_amount += data_count; + } + } + let (back_channel, rx) = oneshot::channel(); + let dial_back_cmd = DialBackCommand { + addr, + nonce, + back_channel, + }; + dial_back_cmd_sender + .clone() + .send(dial_back_cmd) + .await + .map_err(|_| HandleFail::DialBack { + idx, + result: Err(DialBackStatus::DialErr), + })?; + + let dial_back = rx.await.map_err(|_e| HandleFail::InternalError(idx))?; + if let Err(err) = dial_back { + return Err(HandleFail::DialBack { + idx, + result: Err(err), + }); + } + Ok(DialResponse { + status: ResponseStatus::OK, + addr_idx: idx, + dial_status: DialStatus::OK, + }) +} diff --git a/protocols/autonat/tests/autonatv2.rs b/protocols/autonat/tests/autonatv2.rs new file mode 100644 index 00000000000..f22a2e51470 --- /dev/null +++ b/protocols/autonat/tests/autonatv2.rs @@ -0,0 +1,563 @@ +use libp2p_autonat::v2::client::{self, Config}; +use libp2p_autonat::v2::server; +use libp2p_core::multiaddr::Protocol; +use libp2p_core::transport::TransportError; +use libp2p_core::Multiaddr; +use libp2p_swarm::{ + DialError, FromSwarm, NetworkBehaviour, NewExternalAddrCandidate, Swarm, SwarmEvent, +}; +use libp2p_swarm_test::SwarmExt; +use rand_core::OsRng; +use std::sync::Arc; +use std::time::Duration; +use tokio::sync::oneshot; +use tracing_subscriber::EnvFilter; + +#[tokio::test] +async fn confirm_successful() { + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); + let (mut alice, mut bob) = start_and_connect().await; + + let cor_server_peer = *alice.local_peer_id(); + let cor_client_peer = *bob.local_peer_id(); + let bob_tcp_listeners = Arc::new(tcp_listeners(&bob)); + let alice_bob_tcp_listeners = bob_tcp_listeners.clone(); + + let alice_task = async { + let (dialed_peer_id, dialed_connection_id) = alice + .wait(|event| match event { + SwarmEvent::Dialing { + peer_id, + connection_id, + .. + } => peer_id.map(|peer_id| (peer_id, connection_id)), + _ => None, + }) + .await; + + assert_eq!(dialed_peer_id, cor_client_peer); + + let _ = alice + .wait(|event| match event { + SwarmEvent::ConnectionEstablished { + peer_id, + connection_id, + .. + } if peer_id == dialed_peer_id + && peer_id == cor_client_peer + && connection_id == dialed_connection_id => + { + Some(()) + } + _ => None, + }) + .await; + + let server::Event { + all_addrs, + tested_addr, + client, + data_amount, + result, + } = alice + .wait(|event| match event { + SwarmEvent::Behaviour(CombinedServerEvent::Autonat(status_update)) => { + Some(status_update) + } + _ => None, + }) + .await; + + assert_eq!(tested_addr, bob_tcp_listeners.first().cloned().unwrap()); + assert_eq!(data_amount, 0); + assert_eq!(client, cor_client_peer); + assert_eq!(&all_addrs[..], &bob_tcp_listeners[..]); + assert!(result.is_ok(), "Result: {result:?}"); + }; + + let bob_task = async { + bob.wait(|event| match event { + SwarmEvent::NewExternalAddrCandidate { address } => Some(address), + _ => None, + }) + .await; + let incoming_conn_id = bob + .wait(|event| match event { + SwarmEvent::IncomingConnection { connection_id, .. } => Some(connection_id), + _ => None, + }) + .await; + + let _ = bob + .wait(|event| match event { + SwarmEvent::ConnectionEstablished { + connection_id, + peer_id, + .. + } if incoming_conn_id == connection_id && peer_id == cor_server_peer => Some(()), + _ => None, + }) + .await; + + let client::Event { + tested_addr, + bytes_sent, + server, + result, + } = bob + .wait(|event| match event { + SwarmEvent::Behaviour(CombinedClientEvent::Autonat(status_update)) => { + Some(status_update) + } + _ => None, + }) + .await; + assert_eq!( + tested_addr, + alice_bob_tcp_listeners.first().cloned().unwrap() + ); + assert_eq!(bytes_sent, 0); + assert_eq!(server, cor_server_peer); + assert!(result.is_ok(), "Result is {result:?}"); + }; + + tokio::join!(alice_task, bob_task); +} + +#[tokio::test] +async fn dial_back_to_unsupported_protocol() { + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); + let (mut alice, mut bob) = bootstrap().await; + + let alice_peer_id = *alice.local_peer_id(); + + let test_addr: Multiaddr = "/ip4/127.0.0.1/udp/1234/quic/webtransport".parse().unwrap(); + let bob_test_addr = test_addr.clone(); + bob.behaviour_mut() + .autonat + .on_swarm_event(FromSwarm::NewExternalAddrCandidate( + NewExternalAddrCandidate { addr: &test_addr }, + )); + + let (bob_done_tx, bob_done_rx) = oneshot::channel(); + + let alice_task = async { + let (alice_dialing_peer, alice_conn_id) = alice + .wait(|event| match event { + SwarmEvent::Dialing { + peer_id, + connection_id, + } => peer_id.map(|e| (e, connection_id)), + _ => None, + }) + .await; + let mut outgoing_conn_error = alice + .wait(|event| match event { + SwarmEvent::OutgoingConnectionError { + connection_id, + peer_id: Some(peer_id), + error: DialError::Transport(transport_errs), + } if connection_id == alice_conn_id && alice_dialing_peer == peer_id => { + Some(transport_errs) + } + _ => None, + }) + .await; + if let Some((multiaddr, TransportError::MultiaddrNotSupported(not_supported_addr))) = + outgoing_conn_error.pop() + { + assert_eq!( + multiaddr, + test_addr.clone().with_p2p(alice_dialing_peer).unwrap() + ); + assert_eq!(not_supported_addr, multiaddr,); + } else { + panic!("Peers are empty"); + } + assert_eq!(outgoing_conn_error.len(), 0); + let data_amount = alice + .wait(|event| match event { + SwarmEvent::Behaviour(CombinedServerEvent::Autonat(server::Event { + all_addrs, + tested_addr, + client, + data_amount, + result: Ok(()), + })) if all_addrs == vec![test_addr.clone()] + && tested_addr == test_addr.clone() + && client == alice_dialing_peer => + { + Some(data_amount) + } + _ => None, + }) + .await; + + let handler = tokio::spawn(async move { + alice.loop_on_next().await; + }); + let _ = bob_done_rx.await; + handler.abort(); + data_amount + }; + + let bob_task = async { + let data_amount = bob + .wait(|event| match event { + SwarmEvent::Behaviour(CombinedClientEvent::Autonat(client::Event { + tested_addr, + bytes_sent, + server, + result: Err(_), + })) if server == alice_peer_id && tested_addr == bob_test_addr => Some(bytes_sent), + _ => None, + }) + .await; + bob_done_tx.send(()).unwrap(); + data_amount + }; + let (alice_amount, bob_amount) = tokio::join!(alice_task, bob_task); + assert_eq!(alice_amount, bob_amount); +} + +#[tokio::test] +async fn dial_back_to_non_libp2p() { + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); + let (mut alice, mut bob) = bootstrap().await; + let alice_peer_id = *alice.local_peer_id(); + + let addr_str = "/ip6/::1/tcp/1000"; + let addr: Multiaddr = addr_str.parse().unwrap(); + let bob_addr = addr.clone(); + bob.behaviour_mut() + .autonat + .on_swarm_event(FromSwarm::NewExternalAddrCandidate( + NewExternalAddrCandidate { addr: &addr }, + )); + + let alice_task = async { + let (alice_dialing_peer, alice_conn_id) = alice + .wait(|event| match event { + SwarmEvent::Dialing { + peer_id, + connection_id, + } => peer_id.map(|p| (p, connection_id)), + _ => None, + }) + .await; + let mut outgoing_conn_error = alice + .wait(|event| match event { + SwarmEvent::OutgoingConnectionError { + connection_id, + peer_id: Some(peer_id), + error: DialError::Transport(peers), + } if connection_id == alice_conn_id && peer_id == alice_dialing_peer => Some(peers), + _ => None, + }) + .await; + + if let Some((multiaddr, TransportError::Other(o))) = outgoing_conn_error.pop() { + assert_eq!( + multiaddr, + addr.clone().with_p2p(alice_dialing_peer).unwrap() + ); + let error_string = o.to_string(); + assert!( + error_string.contains("Connection refused"), + "Correct error string: {error_string} for {addr_str}" + ); + } else { + panic!("No outgoing connection errors"); + } + + alice + .wait(|event| match event { + SwarmEvent::Behaviour(CombinedServerEvent::Autonat(server::Event { + all_addrs, + tested_addr, + client, + data_amount, + result: Ok(()), + })) if all_addrs == vec![addr.clone()] + && tested_addr == addr + && alice_dialing_peer == client => + { + Some(data_amount) + } + _ => None, + }) + .await + }; + let bob_task = async { + bob.wait(|event| match event { + SwarmEvent::Behaviour(CombinedClientEvent::Autonat(client::Event { + tested_addr, + bytes_sent, + server, + result: Err(_), + })) if tested_addr == bob_addr && server == alice_peer_id => Some(bytes_sent), + _ => None, + }) + .await + }; + + let (alice_bytes_sent, bob_bytes_sent) = tokio::join!(alice_task, bob_task); + assert_eq!(alice_bytes_sent, bob_bytes_sent); + bob.behaviour_mut().autonat.validate_addr(&addr); +} + +#[tokio::test] +async fn dial_back_to_not_supporting() { + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); + + let (mut alice, mut bob) = bootstrap().await; + let alice_peer_id = *alice.local_peer_id(); + + let (bob_done_tx, bob_done_rx) = oneshot::channel(); + + let hannes = new_dummy().await; + let hannes_peer_id = *hannes.local_peer_id(); + let unreachable_address = hannes.external_addresses().next().unwrap().clone(); + let bob_unreachable_address = unreachable_address.clone(); + bob.behaviour_mut() + .autonat + .on_swarm_event(FromSwarm::NewExternalAddrCandidate( + NewExternalAddrCandidate { + addr: &unreachable_address, + }, + )); + + let handler = tokio::spawn(async { hannes.loop_on_next().await }); + + let alice_task = async { + let (alice_dialing_peer, alice_conn_id) = alice + .wait(|event| match event { + SwarmEvent::Dialing { + peer_id, + connection_id, + } => peer_id.map(|p| (p, connection_id)), + _ => None, + }) + .await; + alice + .wait(|event| match event { + SwarmEvent::OutgoingConnectionError { + connection_id, + peer_id: Some(peer_id), + error: DialError::WrongPeerId { obtained, .. }, + } if connection_id == alice_conn_id + && peer_id == alice_dialing_peer + && obtained == hannes_peer_id => + { + Some(()) + } + _ => None, + }) + .await; + + let data_amount = alice + .wait(|event| match event { + SwarmEvent::Behaviour(CombinedServerEvent::Autonat(server::Event { + all_addrs, + tested_addr, + client, + data_amount, + result: Ok(()), + })) if all_addrs == vec![unreachable_address.clone()] + && tested_addr == unreachable_address + && alice_dialing_peer == client => + { + Some(data_amount) + } + _ => None, + }) + .await; + tokio::select! { + _ = bob_done_rx => { + data_amount + } + _ = alice.loop_on_next() => { + unreachable!(); + } + } + }; + + let bob_task = async { + let bytes_sent = bob + .wait(|event| match event { + SwarmEvent::Behaviour(CombinedClientEvent::Autonat(client::Event { + tested_addr, + bytes_sent, + server, + result: Err(_), + })) if tested_addr == bob_unreachable_address && server == alice_peer_id => { + Some(bytes_sent) + } + _ => None, + }) + .await; + bob_done_tx.send(()).unwrap(); + bytes_sent + }; + + let (alice_bytes_sent, bob_bytes_sent) = tokio::join!(alice_task, bob_task); + assert_eq!(alice_bytes_sent, bob_bytes_sent); + handler.abort(); +} + +async fn new_server() -> Swarm { + let mut node = Swarm::new_ephemeral(|identity| CombinedServer { + autonat: libp2p_autonat::v2::server::Behaviour::default(), + identify: libp2p_identify::Behaviour::new(libp2p_identify::Config::new( + "/libp2p-test/1.0.0".into(), + identity.public().clone(), + )), + }); + node.listen().with_tcp_addr_external().await; + + node +} + +async fn new_client() -> Swarm { + let mut node = Swarm::new_ephemeral(|identity| CombinedClient { + autonat: libp2p_autonat::v2::client::Behaviour::new( + OsRng, + Config::default().with_probe_interval(Duration::from_millis(100)), + ), + identify: libp2p_identify::Behaviour::new(libp2p_identify::Config::new( + "/libp2p-test/1.0.0".into(), + identity.public().clone(), + )), + }); + node.listen().await; + node +} + +#[derive(libp2p_swarm::NetworkBehaviour)] +#[behaviour(prelude = "libp2p_swarm::derive_prelude")] +struct CombinedServer { + autonat: libp2p_autonat::v2::server::Behaviour, + identify: libp2p_identify::Behaviour, +} + +#[derive(libp2p_swarm::NetworkBehaviour)] +#[behaviour(prelude = "libp2p_swarm::derive_prelude")] +struct CombinedClient { + autonat: libp2p_autonat::v2::client::Behaviour, + identify: libp2p_identify::Behaviour, +} + +async fn new_dummy() -> Swarm { + let mut node = Swarm::new_ephemeral(|identity| { + libp2p_identify::Behaviour::new(libp2p_identify::Config::new( + "/libp2p-test/1.0.0".into(), + identity.public().clone(), + )) + }); + node.listen().with_tcp_addr_external().await; + node +} + +async fn start_and_connect() -> (Swarm, Swarm) { + let mut alice = new_server().await; + let mut bob = new_client().await; + + bob.connect(&mut alice).await; + (alice, bob) +} + +async fn bootstrap() -> (Swarm, Swarm) { + let (mut alice, mut bob) = start_and_connect().await; + + let cor_server_peer = *alice.local_peer_id(); + let cor_client_peer = *bob.local_peer_id(); + + let alice_task = async { + let (dialed_peer_id, dialed_connection_id) = alice + .wait(|event| match event { + SwarmEvent::Dialing { + peer_id, + connection_id, + .. + } => peer_id.map(|peer_id| (peer_id, connection_id)), + _ => None, + }) + .await; + + let _ = alice + .wait(|event| match event { + SwarmEvent::ConnectionEstablished { + peer_id, + connection_id, + .. + } if peer_id == dialed_peer_id + && peer_id == cor_client_peer + && connection_id == dialed_connection_id => + { + Some(()) + } + _ => None, + }) + .await; + + alice + .wait(|event| match event { + SwarmEvent::Behaviour(CombinedServerEvent::Autonat(_)) => Some(()), + _ => None, + }) + .await; + }; + + let bob_task = async { + bob.wait(|event| match event { + SwarmEvent::NewExternalAddrCandidate { address } => Some(address), + _ => None, + }) + .await; + let incoming_conn_id = bob + .wait(|event| match event { + SwarmEvent::IncomingConnection { connection_id, .. } => Some(connection_id), + _ => None, + }) + .await; + + let _ = bob + .wait(|event| match event { + SwarmEvent::ConnectionEstablished { + connection_id, + peer_id, + .. + } if incoming_conn_id == connection_id && peer_id == cor_server_peer => Some(()), + _ => None, + }) + .await; + + bob.wait(|event| match event { + SwarmEvent::Behaviour(CombinedClientEvent::Autonat(_)) => Some(()), + _ => None, + }) + .await; + }; + + tokio::join!(alice_task, bob_task); + (alice, bob) +} + +fn tcp_listeners(swarm: &Swarm) -> Vec { + swarm + .listeners() + .filter(|addr| { + addr.iter() + .any(|protocol| matches!(protocol, Protocol::Tcp(_))) + }) + .cloned() + .collect::>() +} diff --git a/protocols/autonat/tests/test_server.rs b/protocols/autonat/tests/test_server.rs index b0610ef59a4..fd97b1a9132 100644 --- a/protocols/autonat/tests/test_server.rs +++ b/protocols/autonat/tests/test_server.rs @@ -92,6 +92,7 @@ async fn test_dial_back() { ConnectedPoint::Dialer { address, role_override: Endpoint::Dialer, + .. }, num_established, concurrent_dial_errors, @@ -300,6 +301,7 @@ async fn test_dial_multiple_addr() { ConnectedPoint::Dialer { address, role_override: Endpoint::Dialer, + .. }, concurrent_dial_errors, .. diff --git a/protocols/dcutr/CHANGELOG.md b/protocols/dcutr/CHANGELOG.md index d3857373658..0ddc4aa1148 100644 --- a/protocols/dcutr/CHANGELOG.md +++ b/protocols/dcutr/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.12.0 + + + +## 0.11.1 +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + ## 0.11.0 - Add `ConnectionId` to `Event::DirectConnectionUpgradeSucceeded` and `Event::DirectConnectionUpgradeFailed`. diff --git a/protocols/dcutr/Cargo.toml b/protocols/dcutr/Cargo.toml index 8444dc70756..6b1d04f82f5 100644 --- a/protocols/dcutr/Cargo.toml +++ b/protocols/dcutr/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-dcutr" edition = "2021" rust-version = { workspace = true } description = "Direct connection upgrade through relay" -version = "0.11.0" +version = "0.12.0" authors = ["Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -12,24 +12,24 @@ categories = ["network-programming", "asynchronous"] [dependencies] asynchronous-codec = { workspace = true } -either = "1.9.0" -futures = "0.3.30" +either = "1.11.0" +futures = { workspace = true } futures-timer = "3.0" -instant = "0.1.12" +web-time = { workspace = true } libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true } quick-protobuf = "0.8" quick-protobuf-codec = { workspace = true } thiserror = "1.0" -tracing = "0.1.37" +tracing = { workspace = true } void = "1" -lru = "0.12.2" +lru = "0.12.3" futures-bounded = { workspace = true } [dev-dependencies] async-std = { version = "1.12.0", features = ["attributes"] } -clap = { version = "4.4.16", features = ["derive"] } +clap = { version = "4.5.6", features = ["derive"] } libp2p-dns = { workspace = true, features = ["async-std"] } libp2p-identify = { workspace = true } libp2p-noise = { workspace = true } @@ -41,14 +41,12 @@ libp2p-swarm-test = { path = "../../swarm-test" } libp2p-tcp = { workspace = true, features = ["async-io"] } libp2p-yamux = { workspace = true } rand = "0.8" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/dcutr/src/behaviour.rs b/protocols/dcutr/src/behaviour.rs index 3742eb512f5..babd56bd28e 100644 --- a/protocols/dcutr/src/behaviour.rs +++ b/protocols/dcutr/src/behaviour.rs @@ -24,6 +24,7 @@ use crate::{handler, protocol}; use either::Either; use libp2p_core::connection::ConnectedPoint; use libp2p_core::multiaddr::Protocol; +use libp2p_core::transport::PortUse; use libp2p_core::{Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::behaviour::{ConnectionClosed, DialFailure, FromSwarm}; @@ -191,9 +192,9 @@ impl NetworkBehaviour for Behaviour { .insert(connection_id); assert!( - self.direct_to_relayed_connections - .get(&connection_id) - .is_none(), + !self + .direct_to_relayed_connections + .contains_key(&connection_id), "state mismatch" ); @@ -206,12 +207,14 @@ impl NetworkBehaviour for Behaviour { peer: PeerId, addr: &Multiaddr, role_override: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied> { if is_relayed(addr) { return Ok(Either::Left(handler::relayed::Handler::new( ConnectedPoint::Dialer { address: addr.clone(), role_override, + port_use, }, self.observed_addresses(), ))); // TODO: We could make two `handler::relayed::Handler` here, one inbound one outbound. @@ -311,6 +314,8 @@ impl NetworkBehaviour for Behaviour { .or_default() += 1; self.queued_events.push_back(ToSwarm::Dial { opts }); } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right(never) => void::unreachable(never), }; } diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index eba58f89313..72af9fec264 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -115,6 +115,8 @@ impl Handler { self.attempts += 1; } // A connection listener denies all incoming substreams, thus none can ever be fully negotiated. + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] future::Either::Right(output) => void::unreachable(output), } } @@ -153,6 +155,8 @@ impl Handler { ::InboundProtocol, >, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(error.into_inner()); } @@ -164,6 +168,8 @@ impl Handler { >, ) { let error = match error { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(v) => void::unreachable(v), StreamUpgradeError::NegotiationFailed => outbound::Error::Unsupported, StreamUpgradeError::Io(e) => outbound::Error::Io(e), @@ -298,6 +304,8 @@ impl ConnectionHandler for Handler { ConnectionEvent::FullyNegotiatedOutbound(fully_negotiated_outbound) => { self.on_fully_negotiated_outbound(fully_negotiated_outbound) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => { self.on_listen_upgrade_error(listen_upgrade_error) } diff --git a/protocols/dcutr/src/protocol/inbound.rs b/protocols/dcutr/src/protocol/inbound.rs index b8f90daf3a1..005d8394f5e 100644 --- a/protocols/dcutr/src/protocol/inbound.rs +++ b/protocols/dcutr/src/protocol/inbound.rs @@ -23,7 +23,6 @@ use asynchronous_codec::Framed; use futures::prelude::*; use libp2p_core::{multiaddr::Protocol, Multiaddr}; use libp2p_swarm::Stream; -use std::convert::TryFrom; use std::io; use thiserror::Error; diff --git a/protocols/dcutr/src/protocol/outbound.rs b/protocols/dcutr/src/protocol/outbound.rs index d9cb60a01f6..8639ff4f053 100644 --- a/protocols/dcutr/src/protocol/outbound.rs +++ b/protocols/dcutr/src/protocol/outbound.rs @@ -23,12 +23,11 @@ use crate::PROTOCOL_NAME; use asynchronous_codec::Framed; use futures::prelude::*; use futures_timer::Delay; -use instant::Instant; use libp2p_core::{multiaddr::Protocol, Multiaddr}; use libp2p_swarm::Stream; -use std::convert::TryFrom; use std::io; use thiserror::Error; +use web_time::Instant; pub(crate) async fn handshake( stream: Stream, diff --git a/protocols/floodsub/CHANGELOG.md b/protocols/floodsub/CHANGELOG.md index 8e3cb70ddf1..4192e0ea58d 100644 --- a/protocols/floodsub/CHANGELOG.md +++ b/protocols/floodsub/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.45.0 + + + ## 0.44.0 - Change publish to require `data: impl Into` to internally avoid any costly cloning / allocation. diff --git a/protocols/floodsub/Cargo.toml b/protocols/floodsub/Cargo.toml index 06277273a90..18d77e99e9c 100644 --- a/protocols/floodsub/Cargo.toml +++ b/protocols/floodsub/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-floodsub" edition = "2021" rust-version = { workspace = true } description = "Floodsub protocol for libp2p" -version = "0.44.0" +version = "0.45.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -14,24 +14,22 @@ categories = ["network-programming", "asynchronous"] asynchronous-codec = { workspace = true } cuckoofilter = "0.5.0" fnv = "1.0" -bytes = "1.5" -futures = "0.3.30" +bytes = "1.6" +futures = { workspace = true } libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true } quick-protobuf = "0.8" quick-protobuf-codec = { workspace = true } rand = "0.8" -smallvec = "1.13.1" -thiserror = "1.0.57" -tracing = "0.1.37" +smallvec = "1.13.2" +thiserror = "1.0.61" +tracing = { workspace = true } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/floodsub/src/layer.rs b/protocols/floodsub/src/layer.rs index 35711408a8d..1a70d2213b2 100644 --- a/protocols/floodsub/src/layer.rs +++ b/protocols/floodsub/src/layer.rs @@ -27,6 +27,7 @@ use crate::FloodsubConfig; use bytes::Bytes; use cuckoofilter::{CuckooError, CuckooFilter}; use fnv::FnvHashSet; +use libp2p_core::transport::PortUse; use libp2p_core::{Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm}; @@ -346,6 +347,7 @@ impl NetworkBehaviour for Floodsub { _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(Default::default()) } diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index 5ff4cfa27d6..c47a9f40f66 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -1,7 +1,22 @@ +## 0.47.1 + +- Attempt to publish to at least mesh_n peers when flood publish is disabled. + See [PR 5578](https://github.com/libp2p/rust-libp2p/pull/5578). + +## 0.47.0 + + +- Add ConnectionError to FromSwarm::ConnectionClosed. + See [PR 5485](https://github.com/libp2p/rust-libp2p/pull/5485). + +## 0.46.2 +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + ## 0.46.1 - Deprecate `Rpc` in preparation for removing it from the public API because it is an internal type. - See [PR 4833](https://github.com/libp2p/rust-libp2p/pull/4833). + See [PR 4833](https://github.com/libp2p/rust-libp2p/pull/4833). ## 0.46.0 diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index f44d3f97fa0..665f757fcb3 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-gossipsub" edition = "2021" rust-version = { workspace = true } description = "Gossipsub protocol for libp2p" -version = "0.46.1" +version = "0.47.1" authors = ["Age Manning "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -11,31 +11,31 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [features] -wasm-bindgen = ["getrandom/js", "instant/wasm-bindgen"] +wasm-bindgen = ["getrandom/js"] [dependencies] asynchronous-codec = { workspace = true } -base64 = "0.21.7" +base64 = "0.22.1" byteorder = "1.5.0" -bytes = "1.5" -either = "1.9" +bytes = "1.6" +either = "1.11" fnv = "1.0.7" -futures = "0.3.30" +futures = { workspace = true } futures-ticker = "0.0.3" -getrandom = "0.2.12" +getrandom = "0.2.15" hex_fmt = "0.3.0" -instant = "0.1.12" +web-time = { workspace = true } libp2p-core = { workspace = true } libp2p-identity = { workspace = true, features = ["rand"] } libp2p-swarm = { workspace = true } quick-protobuf = "0.8" quick-protobuf-codec = { workspace = true } rand = "0.8" -regex = "1.10.3" +regex = "1.10.5" serde = { version = "1", optional = true, features = ["derive"] } sha2 = "0.10.8" -smallvec = "1.13.1" -tracing = "0.1.37" +smallvec = "1.13.2" +tracing = { workspace = true } void = "1.0.2" # Metrics dependencies @@ -49,14 +49,12 @@ libp2p-yamux = { workspace = true } libp2p-noise = { workspace = true } libp2p-swarm-test = { path = "../../swarm-test" } quickcheck = { workspace = true } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/gossipsub/src/backoff.rs b/protocols/gossipsub/src/backoff.rs index b4a40b91a74..b24da318582 100644 --- a/protocols/gossipsub/src/backoff.rs +++ b/protocols/gossipsub/src/backoff.rs @@ -20,13 +20,13 @@ //! Data structure for efficiently storing known back-off's when pruning peers. use crate::topic::TopicHash; -use instant::Instant; use libp2p_identity::PeerId; use std::collections::{ hash_map::{Entry, HashMap}, HashSet, }; use std::time::Duration; +use web_time::Instant; #[derive(Copy, Clone)] struct HeartbeatIndex(usize); diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 24a32de4cc7..16adb555a44 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -34,8 +34,9 @@ use futures_ticker::Ticker; use prometheus_client::registry::Registry; use rand::{seq::SliceRandom, thread_rng}; -use instant::Instant; -use libp2p_core::{multiaddr::Protocol::Ip4, multiaddr::Protocol::Ip6, Endpoint, Multiaddr}; +use libp2p_core::{ + multiaddr::Protocol::Ip4, multiaddr::Protocol::Ip6, transport::PortUse, Endpoint, Multiaddr, +}; use libp2p_identity::Keypair; use libp2p_identity::PeerId; use libp2p_swarm::{ @@ -44,6 +45,7 @@ use libp2p_swarm::{ ConnectionDenied, ConnectionId, NetworkBehaviour, NotifyHandler, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm, }; +use web_time::{Instant, SystemTime}; use crate::backoff::BackoffStorage; use crate::config::{Config, ValidationMode}; @@ -64,7 +66,6 @@ use crate::types::{ use crate::types::{PeerConnections, PeerKind, RpcOut}; use crate::{rpc_proto::proto, TopicScoreParams}; use crate::{PublishError, SubscriptionError, ValidationError}; -use instant::SystemTime; use quick_protobuf::{MessageWrite, Writer}; use std::{cmp::Ordering::Equal, fmt::Debug}; @@ -258,12 +259,6 @@ pub struct Behaviour { /// the set of [`ConnectionId`]s. connected_peers: HashMap, - /// A map of all connected peers - A map of topic hash to a list of gossipsub peer Ids. - topic_peers: HashMap>, - - /// A map of all connected peers to their subscribed topics. - peer_topics: HashMap>, - /// A set of all explicit peers. These are peers that remain connected and we unconditionally /// forward messages to, outside of the scoring system. explicit_peers: HashSet, @@ -442,8 +437,6 @@ where control_pool: HashMap::new(), publish_config: privacy.into(), duplicate_cache: DuplicateCache::new(config.duplicate_cache_time()), - topic_peers: HashMap::new(), - peer_topics: HashMap::new(), explicit_peers: HashSet::new(), blacklisted_peers: HashSet::new(), mesh: HashMap::new(), @@ -500,9 +493,9 @@ where /// Lists all known peers and their associated subscribed topics. pub fn all_peers(&self) -> impl Iterator)> { - self.peer_topics + self.connected_peers .iter() - .map(|(peer_id, topic_set)| (peer_id, topic_set.iter().collect())) + .map(|(peer_id, peer)| (peer_id, peer.topics.iter().collect())) } /// Lists all known peers and their associated protocol. @@ -528,13 +521,13 @@ where return Err(SubscriptionError::NotAllowed); } - if self.mesh.get(&topic_hash).is_some() { + if self.mesh.contains_key(&topic_hash) { tracing::debug!(%topic, "Topic is already in the mesh"); return Ok(false); } // send subscription request to all peers - for peer in self.peer_topics.keys().copied().collect::>() { + for peer in self.connected_peers.keys().copied().collect::>() { tracing::debug!(%peer, "Sending SUBSCRIBE to peer"); let event = RpcOut::Subscribe(topic_hash.clone()); self.send_message(peer, event); @@ -550,18 +543,19 @@ where /// Unsubscribes from a topic. /// /// Returns [`Ok(true)`] if we were subscribed to this topic. + #[allow(clippy::unnecessary_wraps)] pub fn unsubscribe(&mut self, topic: &Topic) -> Result { tracing::debug!(%topic, "Unsubscribing from topic"); let topic_hash = topic.hash(); - if self.mesh.get(&topic_hash).is_none() { + if !self.mesh.contains_key(&topic_hash) { tracing::debug!(topic=%topic_hash, "Already unsubscribed from topic"); // we are not subscribed return Ok(false); } // announce to all peers - for peer in self.peer_topics.keys().copied().collect::>() { + for peer in self.connected_peers.keys().copied().collect::>() { tracing::debug!(%peer, "Sending UNSUBSCRIBE to peer"); let event = RpcOut::Unsubscribe(topic_hash.clone()); self.send_message(peer, event); @@ -619,84 +613,103 @@ where let topic_hash = raw_message.topic.clone(); + let mut peers_on_topic = self + .connected_peers + .iter() + .filter(|(_, p)| p.topics.contains(&topic_hash)) + .map(|(peer_id, _)| peer_id) + .peekable(); + + if peers_on_topic.peek().is_none() { + return Err(PublishError::InsufficientPeers); + } + let mut recipient_peers = HashSet::new(); - if let Some(set) = self.topic_peers.get(&topic_hash) { - if self.config.flood_publish() { - // Forward to all peers above score and all explicit peers - recipient_peers.extend(set.iter().filter(|p| { - self.explicit_peers.contains(*p) - || !self.score_below_threshold(p, |ts| ts.publish_threshold).0 - })); - } else { - match self.mesh.get(&raw_message.topic) { - // Mesh peers - Some(mesh_peers) => { - recipient_peers.extend(mesh_peers); + if self.config.flood_publish() { + // Forward to all peers above score and all explicit peers + recipient_peers.extend(peers_on_topic.filter(|p| { + self.explicit_peers.contains(*p) + || !self.score_below_threshold(p, |ts| ts.publish_threshold).0 + })); + } else { + match self.mesh.get(&topic_hash) { + // Mesh peers + Some(mesh_peers) => { + // We have a mesh set. We want to make sure to publish to at least `mesh_n` + // peers (if possible). + let needed_extra_peers = self.config.mesh_n().saturating_sub(mesh_peers.len()); + + if needed_extra_peers > 0 { + // We don't have `mesh_n` peers in our mesh, we will randomly select extras + // and publish to them. + + // Get a random set of peers that are appropriate to send messages too. + let peer_list = get_random_peers( + &self.connected_peers, + &topic_hash, + needed_extra_peers, + |peer| { + !mesh_peers.contains(peer) + && !self.explicit_peers.contains(peer) + && !self + .score_below_threshold(peer, |pst| pst.publish_threshold) + .0 + }, + ); + recipient_peers.extend(peer_list); } - // Gossipsub peers - None => { - tracing::debug!(topic=%topic_hash, "Topic not in the mesh"); - // If we have fanout peers add them to the map. - if self.fanout.contains_key(&topic_hash) { - for peer in self.fanout.get(&topic_hash).expect("Topic must exist") { - recipient_peers.insert(*peer); - } - } else { - // We have no fanout peers, select mesh_n of them and add them to the fanout - let mesh_n = self.config.mesh_n(); - let new_peers = get_random_peers( - &self.topic_peers, - &self.connected_peers, - &topic_hash, - mesh_n, - { - |p| { - !self.explicit_peers.contains(p) - && !self - .score_below_threshold(p, |pst| { - pst.publish_threshold - }) - .0 - } - }, - ); - // Add the new peers to the fanout and recipient peers - self.fanout.insert(topic_hash.clone(), new_peers.clone()); - for peer in new_peers { - tracing::debug!(%peer, "Peer added to fanout"); - recipient_peers.insert(peer); - } + + recipient_peers.extend(mesh_peers); + } + // Gossipsub peers + None => { + tracing::debug!(topic=%topic_hash, "Topic not in the mesh"); + // If we have fanout peers add them to the map. + if self.fanout.contains_key(&topic_hash) { + for peer in self.fanout.get(&topic_hash).expect("Topic must exist") { + recipient_peers.insert(*peer); + } + } else { + // We have no fanout peers, select mesh_n of them and add them to the fanout + let mesh_n = self.config.mesh_n(); + let new_peers = + get_random_peers(&self.connected_peers, &topic_hash, mesh_n, { + |p| { + !self.explicit_peers.contains(p) + && !self + .score_below_threshold(p, |pst| pst.publish_threshold) + .0 + } + }); + // Add the new peers to the fanout and recipient peers + self.fanout.insert(topic_hash.clone(), new_peers.clone()); + for peer in new_peers { + tracing::debug!(%peer, "Peer added to fanout"); + recipient_peers.insert(peer); } - // We are publishing to fanout peers - update the time we published - self.fanout_last_pub - .insert(topic_hash.clone(), Instant::now()); } + // We are publishing to fanout peers - update the time we published + self.fanout_last_pub + .insert(topic_hash.clone(), Instant::now()); } + } - // Explicit peers - for peer in &self.explicit_peers { - if set.contains(peer) { - recipient_peers.insert(*peer); - } - } + // Explicit peers that are part of the topic + recipient_peers + .extend(peers_on_topic.filter(|peer_id| self.explicit_peers.contains(peer_id))); - // Floodsub peers - for (peer, connections) in &self.connected_peers { - if connections.kind == PeerKind::Floodsub - && !self - .score_below_threshold(peer, |ts| ts.publish_threshold) - .0 - { - recipient_peers.insert(*peer); - } + // Floodsub peers + for (peer, connections) in &self.connected_peers { + if connections.kind == PeerKind::Floodsub + && !self + .score_below_threshold(peer, |ts| ts.publish_threshold) + .0 + { + recipient_peers.insert(*peer); } } } - if recipient_peers.is_empty() { - return Err(PublishError::InsufficientPeers); - } - // If the message isn't a duplicate and we have sent it to some peers add it to the // duplicate cache and memcache. self.duplicate_cache.insert(msg_id.clone()); @@ -962,7 +975,6 @@ where if added_peers.len() < self.config.mesh_n() { // get the peers let new_peers = get_random_peers( - &self.topic_peers, &self.connected_peers, topic_hash, self.config.mesh_n() - added_peers.len(), @@ -1007,7 +1019,6 @@ where peer_id, vec![topic_hash], &self.mesh, - self.peer_topics.get(&peer_id), &mut self.events, &self.connected_peers, ); @@ -1054,7 +1065,6 @@ where // Select peers for peer exchange let peers = if do_px { get_random_peers( - &self.topic_peers, &self.connected_peers, topic_hash, self.config.prune_peers(), @@ -1105,7 +1115,6 @@ where peer, topic_hash, &self.mesh, - self.peer_topics.get(&peer), &mut self.events, &self.connected_peers, ); @@ -1116,7 +1125,7 @@ where /// Checks if the given peer is still connected and if not dials the peer again. fn check_explicit_peer_connection(&mut self, peer_id: &PeerId) { - if !self.peer_topics.contains_key(peer_id) { + if !self.connected_peers.contains_key(peer_id) { // Connect to peer tracing::debug!(peer=%peer_id, "Connecting to explicit peer"); self.events.push_back(ToSwarm::Dial { @@ -1297,7 +1306,7 @@ where for id in iwant_msgs { // If we have it and the IHAVE count is not above the threshold, - // foward the message. + // forward the message. if let Some((msg, count)) = self .mcache .get_with_iwant_counts(&id, peer_id) @@ -1327,17 +1336,19 @@ where let mut do_px = self.config.do_px(); + let Some(connected_peer) = self.connected_peers.get_mut(peer_id) else { + tracing::error!(peer_id = %peer_id, "Peer non-existent when handling graft"); + return; + }; + // For each topic, if a peer has grafted us, then we necessarily must be in their mesh // and they must be subscribed to the topic. Ensure we have recorded the mapping. for topic in &topics { - self.peer_topics - .entry(*peer_id) - .or_default() - .insert(topic.clone()); - self.topic_peers - .entry(topic.clone()) - .or_default() - .insert(*peer_id); + if connected_peer.topics.insert(topic.clone()) { + if let Some(m) = self.metrics.as_mut() { + m.inc_topic_peers(topic); + } + } } // we don't GRAFT to/from explicit peers; complain loudly if this happens @@ -1439,7 +1450,6 @@ where *peer_id, vec![&topic_hash], &self.mesh, - self.peer_topics.get(peer_id), &mut self.events, &self.connected_peers, ); @@ -1512,7 +1522,6 @@ where *peer_id, topic_hash, &self.mesh, - self.peer_topics.get(peer_id), &mut self.events, &self.connected_peers, ); @@ -1823,7 +1832,7 @@ where let mut unsubscribed_peers = Vec::new(); - let Some(subscribed_topics) = self.peer_topics.get_mut(propagation_source) else { + let Some(peer) = self.connected_peers.get_mut(propagation_source) else { tracing::error!( peer=%propagation_source, "Subscription by unknown peer" @@ -1839,7 +1848,7 @@ where let filtered_topics = match self .subscription_filter - .filter_incoming_subscriptions(subscriptions, subscribed_topics) + .filter_incoming_subscriptions(subscriptions, &peer.topics) { Ok(topics) => topics, Err(s) => { @@ -1855,29 +1864,24 @@ where for subscription in filtered_topics { // get the peers from the mapping, or insert empty lists if the topic doesn't exist let topic_hash = &subscription.topic_hash; - let peer_list = self.topic_peers.entry(topic_hash.clone()).or_default(); match subscription.action { SubscriptionAction::Subscribe => { - if peer_list.insert(*propagation_source) { + if peer.topics.insert(topic_hash.clone()) { tracing::debug!( peer=%propagation_source, topic=%topic_hash, "SUBSCRIPTION: Adding gossip peer to topic" ); - } - // add to the peer_topics mapping - subscribed_topics.insert(topic_hash.clone()); + if let Some(m) = self.metrics.as_mut() { + m.inc_topic_peers(topic_hash); + } + } // if the mesh needs peers add the peer to the mesh if !self.explicit_peers.contains(propagation_source) - && matches!( - self.connected_peers - .get(propagation_source) - .map(|v| &v.kind), - Some(PeerKind::Gossipsubv1_1) | Some(PeerKind::Gossipsub) - ) + && matches!(peer.kind, PeerKind::Gossipsubv1_1 | PeerKind::Gossipsub) && !Self::score_below_threshold_from_scores( &self.peer_score, propagation_source, @@ -1920,16 +1924,18 @@ where })); } SubscriptionAction::Unsubscribe => { - if peer_list.remove(propagation_source) { + if peer.topics.remove(topic_hash) { tracing::debug!( peer=%propagation_source, topic=%topic_hash, "SUBSCRIPTION: Removing gossip peer from topic" ); + + if let Some(m) = self.metrics.as_mut() { + m.dec_topic_peers(topic_hash); + } } - // remove topic from the peer_topics mapping - subscribed_topics.remove(topic_hash); unsubscribed_peers.push((*propagation_source, topic_hash.clone())); // generate an unsubscribe event to be polled application_event.push(ToSwarm::GenerateEvent(Event::Unsubscribed { @@ -1938,10 +1944,6 @@ where })); } } - - if let Some(m) = self.metrics.as_mut() { - m.set_topic_peers(topic_hash, peer_list.len()); - } } // remove unsubscribed peers from the mesh if it exists @@ -1956,7 +1958,6 @@ where *propagation_source, topics_joined, &self.mesh, - self.peer_topics.get(propagation_source), &mut self.events, &self.connected_peers, ); @@ -2037,7 +2038,6 @@ where for (topic_hash, peers) in self.mesh.iter_mut() { let explicit_peers = &self.explicit_peers; let backoffs = &self.backoffs; - let topic_peers = &self.topic_peers; let outbound_peers = &self.outbound_peers; // drop all peers with negative score, without PX @@ -2085,18 +2085,13 @@ where ); // not enough peers - get mesh_n - current_length more let desired_peers = self.config.mesh_n() - peers.len(); - let peer_list = get_random_peers( - topic_peers, - &self.connected_peers, - topic_hash, - desired_peers, - |peer| { + let peer_list = + get_random_peers(&self.connected_peers, topic_hash, desired_peers, |peer| { !peers.contains(peer) && !explicit_peers.contains(peer) && !backoffs.is_backoff_with_slack(topic_hash, peer) && *scores.get(peer).unwrap_or(&0.0) >= 0.0 - }, - ); + }); for peer in &peer_list { let current_topic = to_graft.entry(*peer).or_insert_with(Vec::new); current_topic.push(topic_hash.clone()); @@ -2152,10 +2147,9 @@ where if outbound <= self.config.mesh_outbound_min() { // do not remove anymore outbound peers continue; - } else { - // an outbound peer gets removed - outbound -= 1; } + // an outbound peer gets removed + outbound -= 1; } // remove the peer @@ -2178,19 +2172,14 @@ where // if we have not enough outbound peers, graft to some new outbound peers if outbound < self.config.mesh_outbound_min() { let needed = self.config.mesh_outbound_min() - outbound; - let peer_list = get_random_peers( - topic_peers, - &self.connected_peers, - topic_hash, - needed, - |peer| { + let peer_list = + get_random_peers(&self.connected_peers, topic_hash, needed, |peer| { !peers.contains(peer) && !explicit_peers.contains(peer) && !backoffs.is_backoff_with_slack(topic_hash, peer) && *scores.get(peer).unwrap_or(&0.0) >= 0.0 && outbound_peers.contains(peer) - }, - ); + }); for peer in &peer_list { let current_topic = to_graft.entry(*peer).or_insert_with(Vec::new); current_topic.push(topic_hash.clone()); @@ -2247,7 +2236,6 @@ where // GRAFT if median < thresholds.opportunistic_graft_threshold { let peer_list = get_random_peers( - topic_peers, &self.connected_peers, topic_hash, self.config.opportunistic_graft_peers(), @@ -2306,22 +2294,22 @@ where Some((_, thresholds, _, _)) => thresholds.publish_threshold, _ => 0.0, }; - for peer in peers.iter() { + for peer_id in peers.iter() { // is the peer still subscribed to the topic? - let peer_score = *scores.get(peer).unwrap_or(&0.0); - match self.peer_topics.get(peer) { - Some(topics) => { - if !topics.contains(topic_hash) || peer_score < publish_threshold { + let peer_score = *scores.get(peer_id).unwrap_or(&0.0); + match self.connected_peers.get(peer_id) { + Some(peer) => { + if !peer.topics.contains(topic_hash) || peer_score < publish_threshold { tracing::debug!( topic=%topic_hash, "HEARTBEAT: Peer removed from fanout for topic" ); - to_remove_peers.push(*peer); + to_remove_peers.push(*peer_id); } } None => { // remove if the peer has disconnected - to_remove_peers.push(*peer); + to_remove_peers.push(*peer_id); } } } @@ -2338,17 +2326,12 @@ where ); let needed_peers = self.config.mesh_n() - peers.len(); let explicit_peers = &self.explicit_peers; - let new_peers = get_random_peers( - &self.topic_peers, - &self.connected_peers, - topic_hash, - needed_peers, - |peer_id| { + let new_peers = + get_random_peers(&self.connected_peers, topic_hash, needed_peers, |peer_id| { !peers.contains(peer_id) && !explicit_peers.contains(peer_id) && *scores.get(peer_id).unwrap_or(&0.0) < publish_threshold - }, - ); + }); peers.extend(new_peers); } } @@ -2430,17 +2413,12 @@ where ) }; // get gossip_lazy random peers - let to_msg_peers = get_random_peers_dynamic( - &self.topic_peers, - &self.connected_peers, - topic_hash, - n_map, - |peer| { + let to_msg_peers = + get_random_peers_dynamic(&self.connected_peers, topic_hash, n_map, |peer| { !peers.contains(peer) && !self.explicit_peers.contains(peer) && !self.score_below_threshold(peer, |ts| ts.gossip_threshold).0 - }, - ); + }); tracing::debug!("Gossiping IHAVE to {} peers", to_msg_peers.len()); @@ -2490,7 +2468,6 @@ where peer, vec![topic], &self.mesh, - self.peer_topics.get(&peer), &mut self.events, &self.connected_peers, ); @@ -2541,7 +2518,6 @@ where *peer, topic_hash, &self.mesh, - self.peer_topics.get(peer), &mut self.events, &self.connected_peers, ); @@ -2552,6 +2528,7 @@ where /// Helper function which forwards a message to mesh\[topic\] peers. /// /// Returns true if at least one peer was messaged. + #[allow(clippy::unnecessary_wraps)] fn forward_msg( &mut self, msg_id: &MessageId, @@ -2574,11 +2551,11 @@ where // Add explicit peers for peer_id in &self.explicit_peers { - if let Some(topics) = self.peer_topics.get(peer_id) { + if let Some(peer) = self.connected_peers.get(peer_id) { if Some(peer_id) != propagation_source && !originating_peers.contains(peer_id) && Some(peer_id) != message.source.as_ref() - && topics.contains(&message.topic) + && peer.topics.contains(&message.topic) { recipient_peers.insert(*peer_id); } @@ -2787,6 +2764,7 @@ where .or_insert(PeerConnections { kind: PeerKind::Floodsub, connections: vec![], + topics: Default::default(), }) .connections .push(connection_id); @@ -2795,9 +2773,6 @@ where return; // Not our first connection to this peer, hence nothing to do. } - // Insert an empty set of the topics of this peer until known. - self.peer_topics.insert(peer_id, Default::default()); - if let Some((peer_score, ..)) = &mut self.peer_score { peer_score.add_peer(peer_id); } @@ -2840,28 +2815,26 @@ where if remaining_established != 0 { // Remove the connection from the list - if let Some(connections) = self.connected_peers.get_mut(&peer_id) { - let index = connections + if let Some(peer) = self.connected_peers.get_mut(&peer_id) { + let index = peer .connections .iter() .position(|v| v == &connection_id) .expect("Previously established connection to peer must be present"); - connections.connections.remove(index); + peer.connections.remove(index); // If there are more connections and this peer is in a mesh, inform the first connection // handler. - if !connections.connections.is_empty() { - if let Some(topics) = self.peer_topics.get(&peer_id) { - for topic in topics { - if let Some(mesh_peers) = self.mesh.get(topic) { - if mesh_peers.contains(&peer_id) { - self.events.push_back(ToSwarm::NotifyHandler { - peer_id, - event: HandlerIn::JoinedMesh, - handler: NotifyHandler::One(connections.connections[0]), - }); - break; - } + if !peer.connections.is_empty() { + for topic in &peer.topics { + if let Some(mesh_peers) = self.mesh.get(topic) { + if mesh_peers.contains(&peer_id) { + self.events.push_back(ToSwarm::NotifyHandler { + peer_id, + event: HandlerIn::JoinedMesh, + handler: NotifyHandler::One(peer.connections[0]), + }); + break; } } } @@ -2871,7 +2844,7 @@ where // remove from mesh, topic_peers, peer_topic and the fanout tracing::debug!(peer=%peer_id, "Peer disconnected"); { - let Some(topics) = self.peer_topics.get(&peer_id) else { + let Some(peer) = self.connected_peers.get(&peer_id) else { debug_assert!( self.blacklisted_peers.contains(&peer_id), "Disconnected node not in connected list" @@ -2880,7 +2853,7 @@ where }; // remove peer from all mappings - for topic in topics { + for topic in peer.topics.iter() { // check the mesh for the topic if let Some(mesh_peers) = self.mesh.get_mut(topic) { // check if the peer is in the mesh and remove it @@ -2892,24 +2865,8 @@ where }; } - // remove from topic_peers - if let Some(peer_list) = self.topic_peers.get_mut(topic) { - if !peer_list.remove(&peer_id) { - // debugging purposes - tracing::warn!( - peer=%peer_id, - "Disconnected node: peer not in topic_peers" - ); - } - if let Some(m) = self.metrics.as_mut() { - m.set_topic_peers(topic, peer_list.len()) - } - } else { - tracing::warn!( - peer=%peer_id, - topic=%topic, - "Disconnected node: peer with topic not in topic_peers" - ); + if let Some(m) = self.metrics.as_mut() { + m.dec_topic_peers(topic); } // remove from fanout @@ -2923,11 +2880,6 @@ where self.px_peers.remove(&peer_id); self.outbound_peers.remove(&peer_id); - // Remove peer from peer_topics and connected_peers - // NOTE: It is possible the peer has already been removed from all mappings if it does not - // support the protocol. - self.peer_topics.remove(&peer_id); - // If metrics are enabled, register the disconnection of a peer based on its protocol. if let Some(metrics) = self.metrics.as_mut() { let peer_kind = &self @@ -3011,6 +2963,7 @@ where _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(Handler::new(self.config.protocol_config())) } @@ -3186,7 +3139,6 @@ fn peer_added_to_mesh( peer_id: PeerId, new_topics: Vec<&TopicHash>, mesh: &HashMap>, - known_topics: Option<&BTreeSet>, events: &mut VecDeque>, connections: &HashMap, ) { @@ -3200,8 +3152,8 @@ fn peer_added_to_mesh( conn.connections[0] }; - if let Some(topics) = known_topics { - for topic in topics { + if let Some(peer) = connections.get(&peer_id) { + for topic in &peer.topics { if !new_topics.contains(&topic) { if let Some(mesh_peers) = mesh.get(topic) { if mesh_peers.contains(&peer_id) { @@ -3227,7 +3179,6 @@ fn peer_removed_from_mesh( peer_id: PeerId, old_topic: &TopicHash, mesh: &HashMap>, - known_topics: Option<&BTreeSet>, events: &mut VecDeque>, connections: &HashMap, ) { @@ -3239,8 +3190,8 @@ fn peer_removed_from_mesh( .first() .expect("There should be at least one connection to a peer."); - if let Some(topics) = known_topics { - for topic in topics { + if let Some(peer) = connections.get(&peer_id) { + for topic in &peer.topics { if topic != old_topic { if let Some(mesh_peers) = mesh.get(topic) { if mesh_peers.contains(&peer_id) { @@ -3263,28 +3214,19 @@ fn peer_removed_from_mesh( /// filtered by the function `f`. The number of peers to get equals the output of `n_map` /// that gets as input the number of filtered peers. fn get_random_peers_dynamic( - topic_peers: &HashMap>, connected_peers: &HashMap, topic_hash: &TopicHash, // maps the number of total peers to the number of selected peers n_map: impl Fn(usize) -> usize, mut f: impl FnMut(&PeerId) -> bool, ) -> BTreeSet { - let mut gossip_peers = match topic_peers.get(topic_hash) { - // if they exist, filter the peers by `f` - Some(peer_list) => peer_list - .iter() - .copied() - .filter(|p| { - f(p) && match connected_peers.get(p) { - Some(connections) if connections.kind == PeerKind::Gossipsub => true, - Some(connections) if connections.kind == PeerKind::Gossipsubv1_1 => true, - _ => false, - } - }) - .collect(), - None => Vec::new(), - }; + let mut gossip_peers = connected_peers + .iter() + .filter(|(_, p)| p.topics.contains(topic_hash)) + .filter(|(peer_id, _)| f(peer_id)) + .filter(|(_, p)| p.kind == PeerKind::Gossipsub || p.kind == PeerKind::Gossipsubv1_1) + .map(|(peer_id, _)| *peer_id) + .collect::>(); // if we have less than needed, return them let n = n_map(gossip_peers.len()); @@ -3305,13 +3247,12 @@ fn get_random_peers_dynamic( /// Helper function to get a set of `n` random gossipsub peers for a `topic_hash` /// filtered by the function `f`. fn get_random_peers( - topic_peers: &HashMap>, connected_peers: &HashMap, topic_hash: &TopicHash, n: usize, f: impl FnMut(&PeerId) -> bool, ) -> BTreeSet { - get_random_peers_dynamic(topic_peers, connected_peers, topic_hash, |_| n, f) + get_random_peers_dynamic(connected_peers, topic_hash, |_| n, f) } /// Validates the combination of signing, privacy and message validation to ensure the @@ -3351,8 +3292,6 @@ impl fmt::Debug for Behaviour @@ -211,6 +206,7 @@ where ConnectedPoint::Dialer { address, role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, } } else { ConnectedPoint::Listener { @@ -261,6 +257,7 @@ where let fake_endpoint = ConnectedPoint::Dialer { address: Multiaddr::empty(), role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, }; // this is not relevant // peer_connections.connections should never be empty. @@ -273,6 +270,7 @@ where connection_id, endpoint: &fake_endpoint, remaining_established: active_connections, + cause: None, })); } } @@ -396,7 +394,7 @@ fn test_subscribe() { .create_network(); assert!( - gs.mesh.get(&topic_hashes[0]).is_some(), + gs.mesh.contains_key(&topic_hashes[0]), "Subscribe should add a new entry to the mesh[topic] hashmap" ); @@ -442,11 +440,13 @@ fn test_unsubscribe() { for topic_hash in &topic_hashes { assert!( - gs.topic_peers.get(topic_hash).is_some(), + gs.connected_peers + .values() + .any(|p| p.topics.contains(topic_hash)), "Topic_peers contain a topic entry" ); assert!( - gs.mesh.get(topic_hash).is_some(), + gs.mesh.contains_key(topic_hash), "mesh should contain a topic entry" ); } @@ -479,7 +479,7 @@ fn test_unsubscribe() { // check we clean up internal structures for topic_hash in &topic_hashes { assert!( - gs.mesh.get(topic_hash).is_none(), + !gs.mesh.contains_key(topic_hash), "All topics should have been removed from the mesh" ); } @@ -566,6 +566,7 @@ fn test_join() { endpoint: &ConnectedPoint::Dialer { address: "/ip4/127.0.0.1".parse::().unwrap(), role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, }, failed_addresses: &[], other_established: 0, @@ -624,14 +625,17 @@ fn test_publish_without_flood_publishing() { .create_network(); assert!( - gs.mesh.get(&topic_hashes[0]).is_some(), + gs.mesh.contains_key(&topic_hashes[0]), "Subscribe should add a new entry to the mesh[topic] hashmap" ); // all peers should be subscribed to the topic assert_eq!( - gs.topic_peers.get(&topic_hashes[0]).map(|p| p.len()), - Some(20), + gs.connected_peers + .values() + .filter(|p| p.topics.contains(&topic_hashes[0])) + .count(), + 20, "Peers should be subscribed to the topic" ); @@ -670,8 +674,8 @@ fn test_publish_without_flood_publishing() { let config: Config = Config::default(); assert_eq!( publishes.len(), - config.mesh_n_low(), - "Should send a publish message to all known peers" + config.mesh_n(), + "Should send a publish message to at least mesh_n peers" ); assert!( @@ -703,7 +707,7 @@ fn test_fanout() { .create_network(); assert!( - gs.mesh.get(&topic_hashes[0]).is_some(), + gs.mesh.contains_key(&topic_hashes[0]), "Subscribe should add a new entry to the mesh[topic] hashmap" ); // Unsubscribe from topic @@ -810,9 +814,9 @@ fn test_inject_connected() { // should add the new peers to `peer_topics` with an empty vec as a gossipsub node for peer in peers { - let known_topics = gs.peer_topics.get(&peer).unwrap(); + let peer = gs.connected_peers.get(&peer).unwrap(); assert!( - known_topics == &topic_hashes.iter().cloned().collect(), + peer.topics == topic_hashes.iter().cloned().collect(), "The topics for each node should all topics" ); } @@ -861,24 +865,39 @@ fn test_handle_received_subscriptions() { // verify the result - let peer_topics = gs.peer_topics.get(&peers[0]).unwrap().clone(); + let peer = gs.connected_peers.get(&peers[0]).unwrap(); assert!( - peer_topics == topic_hashes.iter().take(3).cloned().collect(), + peer.topics + == topic_hashes + .iter() + .take(3) + .cloned() + .collect::>(), "First peer should be subscribed to three topics" ); - let peer_topics = gs.peer_topics.get(&peers[1]).unwrap().clone(); + let peer1 = gs.connected_peers.get(&peers[1]).unwrap(); assert!( - peer_topics == topic_hashes.iter().take(3).cloned().collect(), + peer1.topics + == topic_hashes + .iter() + .take(3) + .cloned() + .collect::>(), "Second peer should be subscribed to three topics" ); assert!( - gs.peer_topics.get(&unknown_peer).is_none(), + !gs.connected_peers.contains_key(&unknown_peer), "Unknown peer should not have been added" ); for topic_hash in topic_hashes[..3].iter() { - let topic_peers = gs.topic_peers.get(topic_hash).unwrap().clone(); + let topic_peers = gs + .connected_peers + .iter() + .filter(|(_, p)| p.topics.contains(topic_hash)) + .map(|(peer_id, _)| *peer_id) + .collect::>(); assert!( topic_peers == peers[..2].iter().cloned().collect(), "Two peers should be added to the first three topics" @@ -895,13 +914,21 @@ fn test_handle_received_subscriptions() { &peers[0], ); - let peer_topics = gs.peer_topics.get(&peers[0]).unwrap().clone(); - assert!( - peer_topics == topic_hashes[1..3].iter().cloned().collect(), + let peer = gs.connected_peers.get(&peers[0]).unwrap().clone(); + assert_eq!( + peer.topics, + topic_hashes[1..3].iter().cloned().collect::>(), "Peer should be subscribed to two topics" ); - let topic_peers = gs.topic_peers.get(&topic_hashes[0]).unwrap().clone(); // only gossipsub at the moment + // only gossipsub at the moment + let topic_peers = gs + .connected_peers + .iter() + .filter(|(_, p)| p.topics.contains(&topic_hashes[0])) + .map(|(peer_id, _)| *peer_id) + .collect::>(); + assert!( topic_peers == peers[1..2].iter().cloned().collect(), "Only the second peers should be in the first topic" @@ -925,9 +952,8 @@ fn test_get_random_peers() { for _ in 0..20 { peers.push(PeerId::random()) } - - gs.topic_peers - .insert(topic_hash.clone(), peers.iter().cloned().collect()); + let mut topics = BTreeSet::new(); + topics.insert(topic_hash.clone()); gs.connected_peers = peers .iter() @@ -937,52 +963,32 @@ fn test_get_random_peers() { PeerConnections { kind: PeerKind::Gossipsubv1_1, connections: vec![ConnectionId::new_unchecked(0)], + topics: topics.clone(), }, ) }) .collect(); - let random_peers = - get_random_peers(&gs.topic_peers, &gs.connected_peers, &topic_hash, 5, |_| { - true - }); + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 5, |_| true); assert_eq!(random_peers.len(), 5, "Expected 5 peers to be returned"); - let random_peers = get_random_peers( - &gs.topic_peers, - &gs.connected_peers, - &topic_hash, - 30, - |_| true, - ); + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 30, |_| true); assert!(random_peers.len() == 20, "Expected 20 peers to be returned"); assert!( random_peers == peers.iter().cloned().collect(), "Expected no shuffling" ); - let random_peers = get_random_peers( - &gs.topic_peers, - &gs.connected_peers, - &topic_hash, - 20, - |_| true, - ); + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 20, |_| true); assert!(random_peers.len() == 20, "Expected 20 peers to be returned"); assert!( random_peers == peers.iter().cloned().collect(), "Expected no shuffling" ); - let random_peers = - get_random_peers(&gs.topic_peers, &gs.connected_peers, &topic_hash, 0, |_| { - true - }); + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 0, |_| true); assert!(random_peers.is_empty(), "Expected 0 peers to be returned"); // test the filter - let random_peers = - get_random_peers(&gs.topic_peers, &gs.connected_peers, &topic_hash, 5, |_| { - false - }); + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 5, |_| false); assert!(random_peers.is_empty(), "Expected 0 peers to be returned"); - let random_peers = get_random_peers(&gs.topic_peers, &gs.connected_peers, &topic_hash, 10, { + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 10, { |peer| peers.contains(peer) }); assert!(random_peers.len() == 10, "Expected 10 peers to be returned"); @@ -1151,7 +1157,7 @@ fn test_handle_ihave_subscribed_and_msg_not_cached() { assert!( iwant_exists, - "Expected to send an IWANT control message for unkown message id" + "Expected to send an IWANT control message for unknown message id" ); } @@ -1272,7 +1278,7 @@ fn test_handle_graft_multiple_topics() { } assert!( - gs.mesh.get(&topic_hashes[2]).is_none(), + !gs.mesh.contains_key(&topic_hashes[2]), "Expected the second topic to not be in the mesh" ); } @@ -2397,7 +2403,7 @@ fn test_dont_graft_to_negative_scored_peers() { } ///Note that in this test also without a penalty the px would be ignored because of the -/// acceptPXThreshold, but the spec still explicitely states the rule that px from negative +/// acceptPXThreshold, but the spec still explicitly states the rule that px from negative /// peers should get ignored, therefore we test it here. #[test] fn test_ignore_px_from_negative_scored_peer() { @@ -4056,6 +4062,7 @@ fn test_scoring_p6() { endpoint: &ConnectedPoint::Dialer { address: addr.clone(), role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, }, failed_addresses: &[], other_established: 0, @@ -4077,6 +4084,7 @@ fn test_scoring_p6() { endpoint: &ConnectedPoint::Dialer { address: addr2.clone(), role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, }, failed_addresses: &[], other_established: 1, @@ -4107,6 +4115,7 @@ fn test_scoring_p6() { endpoint: &ConnectedPoint::Dialer { address: addr, role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, }, failed_addresses: &[], other_established: 2, @@ -4551,7 +4560,7 @@ fn test_limit_number_of_message_ids_inside_ihave() { //emit gossip gs.emit_gossip(); - // both peers should have gotten 100 random ihave messages, to asser the randomness, we + // both peers should have gotten 100 random ihave messages, to assert the randomness, we // assert that both have not gotten the same set of messages, but have an intersection // (which is the case with very high probability, the probabiltity of failure is < 10^-58). @@ -5100,7 +5109,7 @@ fn test_graft_without_subscribe() { .create_network(); assert!( - gs.mesh.get(&topic_hashes[0]).is_some(), + gs.mesh.contains_key(&topic_hashes[0]), "Subscribe should add a new entry to the mesh[topic] hashmap" ); diff --git a/protocols/gossipsub/src/config.rs b/protocols/gossipsub/src/config.rs index 7e79912cc4a..febe2514a30 100644 --- a/protocols/gossipsub/src/config.rs +++ b/protocols/gossipsub/src/config.rs @@ -262,9 +262,9 @@ impl Config { self.unsubscribe_backoff } - /// Number of heartbeat slots considered as slack for backoffs. This gurantees that we wait + /// Number of heartbeat slots considered as slack for backoffs. This guarantees that we wait /// at least backoff_slack heartbeats after a backoff is over before we try to graft. This - /// solves problems occuring through high latencies. In particular if + /// solves problems occurring through high latencies. In particular if /// `backoff_slack * heartbeat_interval` is longer than any latencies between processing /// prunes on our side and processing prunes on the receiving side this guarantees that we /// get not punished for too early grafting. The default is 1. @@ -292,7 +292,7 @@ impl Config { self.mesh_outbound_min } - /// Number of heartbeat ticks that specifcy the interval in which opportunistic grafting is + /// Number of heartbeat ticks that specify the interval in which opportunistic grafting is /// applied. Every `opportunistic_graft_ticks` we will attempt to select some high-scoring mesh /// peers to replace lower-scoring ones, if the median score of our mesh peers falls below a /// threshold (see ). @@ -660,9 +660,9 @@ impl ConfigBuilder { self } - /// Number of heartbeat slots considered as slack for backoffs. This gurantees that we wait + /// Number of heartbeat slots considered as slack for backoffs. This guarantees that we wait /// at least backoff_slack heartbeats after a backoff is over before we try to graft. This - /// solves problems occuring through high latencies. In particular if + /// solves problems occurring through high latencies. In particular if /// `backoff_slack * heartbeat_interval` is longer than any latencies between processing /// prunes on our side and processing prunes on the receiving side this guarantees that we /// get not punished for too early grafting. The default is 1. @@ -694,7 +694,7 @@ impl ConfigBuilder { self } - /// Number of heartbeat ticks that specifcy the interval in which opportunistic grafting is + /// Number of heartbeat ticks that specify the interval in which opportunistic grafting is /// applied. Every `opportunistic_graft_ticks` we will attempt to select some high-scoring mesh /// peers to replace lower-scoring ones, if the median score of our mesh peers falls below a /// threshold (see ). @@ -860,10 +860,8 @@ impl std::fmt::Debug for Config { mod test { use super::*; use crate::topic::IdentityHash; - use crate::types::PeerKind; use crate::Topic; use libp2p_core::UpgradeInfo; - use libp2p_swarm::StreamProtocol; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; diff --git a/protocols/gossipsub/src/gossip_promises.rs b/protocols/gossipsub/src/gossip_promises.rs index 9538622c0dc..bdf58b74fc2 100644 --- a/protocols/gossipsub/src/gossip_promises.rs +++ b/protocols/gossipsub/src/gossip_promises.rs @@ -21,9 +21,9 @@ use crate::peer_score::RejectReason; use crate::MessageId; use crate::ValidationError; -use instant::Instant; use libp2p_identity::PeerId; use std::collections::HashMap; +use web_time::Instant; /// Tracks recently sent `IWANT` messages and checks if peers respond to them. #[derive(Default)] diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index e91f81776e7..8e3b3a8b022 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -26,7 +26,6 @@ use asynchronous_codec::Framed; use futures::future::Either; use futures::prelude::*; use futures::StreamExt; -use instant::Instant; use libp2p_core::upgrade::DeniedUpgrade; use libp2p_swarm::handler::{ ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError, @@ -38,6 +37,7 @@ use std::{ pin::Pin, task::{Context, Poll}, }; +use web_time::Instant; /// The event emitted by the Handler. This informs the behaviour of various events created /// by the handler. @@ -493,6 +493,8 @@ impl ConnectionHandler for Handler { .. }) => match protocol { Either::Left(protocol) => handler.on_fully_negotiated_inbound(protocol), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right(v) => void::unreachable(v), }, ConnectionEvent::FullyNegotiatedOutbound(fully_negotiated_outbound) => { @@ -504,6 +506,8 @@ impl ConnectionHandler for Handler { }) => { tracing::debug!("Dial upgrade error: Protocol negotiation timeout"); } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::DialUpgradeError(DialUpgradeError { error: StreamUpgradeError::Apply(e), .. diff --git a/protocols/gossipsub/src/lib.rs b/protocols/gossipsub/src/lib.rs index 15db5eba21d..3db2fa7ce51 100644 --- a/protocols/gossipsub/src/lib.rs +++ b/protocols/gossipsub/src/lib.rs @@ -43,16 +43,16 @@ //! implementations, due to undefined elements in the current specification. //! //! - **Topics** - In gossipsub, topics configurable by the `hash_topics` configuration parameter. -//! Topics are of type [`TopicHash`]. The current go implementation uses raw utf-8 strings, and this -//! is default configuration in rust-libp2p. Topics can be hashed (SHA256 hashed then base64 -//! encoded) by setting the `hash_topics` configuration parameter to true. +//! Topics are of type [`TopicHash`]. The current go implementation uses raw utf-8 strings, and this +//! is default configuration in rust-libp2p. Topics can be hashed (SHA256 hashed then base64 +//! encoded) by setting the `hash_topics` configuration parameter to true. //! //! - **Sequence Numbers** - A message on the gossipsub network is identified by the source -//! [`PeerId`](libp2p_identity::PeerId) and a nonce (sequence number) of the message. The sequence numbers in -//! this implementation are sent as raw bytes across the wire. They are 64-bit big-endian unsigned -//! integers. When messages are signed, they are monotonically increasing integers starting from a -//! random value and wrapping around u64::MAX. When messages are unsigned, they are chosen at random. -//! NOTE: These numbers are sequential in the current go implementation. +//! [`PeerId`](libp2p_identity::PeerId) and a nonce (sequence number) of the message. The sequence numbers in +//! this implementation are sent as raw bytes across the wire. They are 64-bit big-endian unsigned +//! integers. When messages are signed, they are monotonically increasing integers starting from a +//! random value and wrapping around u64::MAX. When messages are unsigned, they are chosen at random. +//! NOTE: These numbers are sequential in the current go implementation. //! //! # Peer Discovery //! diff --git a/protocols/gossipsub/src/mcache.rs b/protocols/gossipsub/src/mcache.rs index ef4a93bc936..aa65e3b7f1d 100644 --- a/protocols/gossipsub/src/mcache.rs +++ b/protocols/gossipsub/src/mcache.rs @@ -221,9 +221,7 @@ impl MessageCache { #[cfg(test)] mod tests { use super::*; - use crate::types::RawMessage; - use crate::{IdentTopic as Topic, TopicHash}; - use libp2p_identity::PeerId; + use crate::IdentTopic as Topic; fn gen_testm(x: u64, topic: TopicHash) -> (MessageId, RawMessage) { let default_id = |message: &RawMessage| { diff --git a/protocols/gossipsub/src/metrics.rs b/protocols/gossipsub/src/metrics.rs index e044ca67e71..7d4acada3c7 100644 --- a/protocols/gossipsub/src/metrics.rs +++ b/protocols/gossipsub/src/metrics.rs @@ -355,12 +355,17 @@ impl Metrics { } } - /// Register how many peers do we known are subscribed to this topic. - pub(crate) fn set_topic_peers(&mut self, topic: &TopicHash, count: usize) { + /// Increase the number of peers that are subscribed to this topic. + pub(crate) fn inc_topic_peers(&mut self, topic: &TopicHash) { if self.register_topic(topic).is_ok() { - self.topic_peers_count - .get_or_create(topic) - .set(count as i64); + self.topic_peers_count.get_or_create(topic).inc(); + } + } + + /// Decrease the number of peers that are subscribed to this topic. + pub(crate) fn dec_topic_peers(&mut self, topic: &TopicHash) { + if self.register_topic(topic).is_ok() { + self.topic_peers_count.get_or_create(topic).dec(); } } diff --git a/protocols/gossipsub/src/peer_score.rs b/protocols/gossipsub/src/peer_score.rs index b1ea9bfae95..ac24fc91970 100644 --- a/protocols/gossipsub/src/peer_score.rs +++ b/protocols/gossipsub/src/peer_score.rs @@ -24,11 +24,11 @@ use crate::metrics::{Metrics, Penalty}; use crate::time_cache::TimeCache; use crate::{MessageId, TopicHash}; -use instant::Instant; use libp2p_identity::PeerId; use std::collections::{hash_map, HashMap, HashSet}; use std::net::IpAddr; use std::time::Duration; +use web_time::Instant; mod params; use crate::ValidationError; @@ -99,7 +99,7 @@ impl PeerStats { topic_hash: TopicHash, params: &PeerScoreParams, ) -> Option<&mut TopicStats> { - if params.topics.get(&topic_hash).is_some() { + if params.topics.contains_key(&topic_hash) { Some(self.topics.entry(topic_hash).or_default()) } else { self.topics.get_mut(&topic_hash) @@ -307,7 +307,7 @@ impl PeerScore { // P6: IP collocation factor for ip in peer_stats.known_ips.iter() { - if self.params.ip_colocation_factor_whitelist.get(ip).is_some() { + if self.params.ip_colocation_factor_whitelist.contains(ip) { continue; } @@ -680,7 +680,7 @@ impl PeerScore { ) { let record = self.deliveries.entry(msg_id.clone()).or_default(); - if record.peers.get(from).is_some() { + if record.peers.contains(from) { // we have already seen this duplicate! return; } diff --git a/protocols/gossipsub/src/protocol.rs b/protocols/gossipsub/src/protocol.rs index e9600a4d8d8..c13caae58b6 100644 --- a/protocols/gossipsub/src/protocol.rs +++ b/protocols/gossipsub/src/protocol.rs @@ -29,7 +29,6 @@ use crate::ValidationError; use asynchronous_codec::{Decoder, Encoder, Framed}; use byteorder::{BigEndian, ByteOrder}; use bytes::BytesMut; -use futures::future; use futures::prelude::*; use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; use libp2p_identity::{PeerId, PublicKey}; @@ -542,20 +541,6 @@ mod tests { struct TestKeypair(Keypair); impl Arbitrary for TestKeypair { - #[cfg(feature = "rsa")] - fn arbitrary(g: &mut Gen) -> Self { - let keypair = if bool::arbitrary(g) { - // Small enough to be inlined. - Keypair::generate_ed25519() - } else { - // Too large to be inlined. - let mut rsa_key = hex::decode("308204bd020100300d06092a864886f70d0101010500048204a7308204a30201000282010100ef930f41a71288b643c1cbecbf5f72ab53992249e2b00835bf07390b6745419f3848cbcc5b030faa127bc88cdcda1c1d6f3ff699f0524c15ab9d2c9d8015f5d4bd09881069aad4e9f91b8b0d2964d215cdbbae83ddd31a7622a8228acee07079f6e501aea95508fa26c6122816ef7b00ac526d422bd12aed347c37fff6c1c307f3ba57bb28a7f28609e0bdcc839da4eedca39f5d2fa855ba4b0f9c763e9764937db929a1839054642175312a3de2d3405c9d27bdf6505ef471ce85c5e015eee85bf7874b3d512f715de58d0794fd8afe021c197fbd385bb88a930342fac8da31c27166e2edab00fa55dc1c3814448ba38363077f4e8fe2bdea1c081f85f1aa6f02030100010282010028ff427a1aac1a470e7b4879601a6656193d3857ea79f33db74df61e14730e92bf9ffd78200efb0c40937c3356cbe049cd32e5f15be5c96d5febcaa9bd3484d7fded76a25062d282a3856a1b3b7d2c525cdd8434beae147628e21adf241dd64198d5819f310d033743915ba40ea0b6acdbd0533022ad6daa1ff42de51885f9e8bab2306c6ef1181902d1cd7709006eba1ab0587842b724e0519f295c24f6d848907f772ae9a0953fc931f4af16a07df450fb8bfa94572562437056613647818c238a6ff3f606cffa0533e4b8755da33418dfbc64a85110b1a036623c947400a536bb8df65e5ebe46f2dfd0cfc86e7aeeddd7574c253e8fbf755562b3669525d902818100f9fff30c6677b78dd31ec7a634361438457e80be7a7faf390903067ea8355faa78a1204a82b6e99cb7d9058d23c1ecf6cfe4a900137a00cecc0113fd68c5931602980267ea9a95d182d48ba0a6b4d5dd32fdac685cb2e5d8b42509b2eb59c9579ea6a67ccc7547427e2bd1fb1f23b0ccb4dd6ba7d206c8dd93253d70a451701302818100f5530dfef678d73ce6a401ae47043af10a2e3f224c71ae933035ecd68ccbc4df52d72bc6ca2b17e8faf3e548b483a2506c0369ab80df3b137b54d53fac98f95547c2bc245b416e650ce617e0d29db36066f1335a9ba02ad3e0edf9dc3d58fd835835042663edebce81803972696c789012847cb1f854ab2ac0a1bd3867ac7fb502818029c53010d456105f2bf52a9a8482bca2224a5eac74bf3cc1a4d5d291fafcdffd15a6a6448cce8efdd661f6617ca5fc37c8c885cc3374e109ac6049bcbf72b37eabf44602a2da2d4a1237fd145c863e6d75059976de762d9d258c42b0984e2a2befa01c95217c3ee9c736ff209c355466ff99375194eff943bc402ea1d172a1ed02818027175bf493bbbfb8719c12b47d967bf9eac061c90a5b5711172e9095c38bb8cc493c063abffe4bea110b0a2f22ac9311b3947ba31b7ef6bfecf8209eebd6d86c316a2366bbafda7279b2b47d5bb24b6202254f249205dcad347b574433f6593733b806f84316276c1990a016ce1bbdbe5f650325acc7791aefe515ecc60063bd02818100b6a2077f4adcf15a17092d9c4a346d6022ac48f3861b73cf714f84c440a07419a7ce75a73b9cbff4597c53c128bf81e87b272d70428a272d99f90cd9b9ea1033298e108f919c6477400145a102df3fb5601ffc4588203cf710002517bfa24e6ad32f4d09c6b1a995fa28a3104131bedd9072f3b4fb4a5c2056232643d310453f").unwrap(); - Keypair::rsa_from_pkcs8(&mut rsa_key).unwrap() - }; - TestKeypair(keypair) - } - - #[cfg(not(feature = "rsa"))] fn arbitrary(_g: &mut Gen) -> Self { // Small enough to be inlined. TestKeypair(Keypair::generate_ed25519()) diff --git a/protocols/gossipsub/src/subscription_filter.rs b/protocols/gossipsub/src/subscription_filter.rs index 09c323d7904..02bb9b4eab6 100644 --- a/protocols/gossipsub/src/subscription_filter.rs +++ b/protocols/gossipsub/src/subscription_filter.rs @@ -212,7 +212,6 @@ impl TopicSubscriptionFilter for RegexSubscriptionFilter { mod test { use super::*; use crate::types::SubscriptionAction::*; - use std::iter::FromIterator; #[test] fn test_filter_incoming_allow_all_with_duplicates() { diff --git a/protocols/gossipsub/src/time_cache.rs b/protocols/gossipsub/src/time_cache.rs index 89fd4afee09..a3e5c01ac4c 100644 --- a/protocols/gossipsub/src/time_cache.rs +++ b/protocols/gossipsub/src/time_cache.rs @@ -21,13 +21,13 @@ //! This implements a time-based LRU cache for checking gossipsub message duplicates. use fnv::FnvHashMap; -use instant::Instant; use std::collections::hash_map::{ self, Entry::{Occupied, Vacant}, }; use std::collections::VecDeque; use std::time::Duration; +use web_time::Instant; struct ExpiringElement { /// The element that expires diff --git a/protocols/gossipsub/src/transform.rs b/protocols/gossipsub/src/transform.rs index 6f57d9fc46b..4831f9781b0 100644 --- a/protocols/gossipsub/src/transform.rs +++ b/protocols/gossipsub/src/transform.rs @@ -28,6 +28,7 @@ use crate::{Message, RawMessage, TopicHash}; /// A general trait of transforming a [`RawMessage`] into a [`Message`]. The +/// /// [`RawMessage`] is obtained from the wire and the [`Message`] is used to /// calculate the [`crate::MessageId`] of the message and is what is sent to the application. /// diff --git a/protocols/gossipsub/src/types.rs b/protocols/gossipsub/src/types.rs index d1b92ff0ba8..a88f4822ac2 100644 --- a/protocols/gossipsub/src/types.rs +++ b/protocols/gossipsub/src/types.rs @@ -24,8 +24,8 @@ use libp2p_identity::PeerId; use libp2p_swarm::ConnectionId; use prometheus_client::encoding::EncodeLabelValue; use quick_protobuf::MessageWrite; -use std::fmt; use std::fmt::Debug; +use std::{collections::BTreeSet, fmt}; use crate::rpc_proto::proto; #[cfg(feature = "serde")] @@ -77,6 +77,8 @@ pub(crate) struct PeerConnections { pub(crate) kind: PeerKind, /// Its current connections. pub(crate) connections: Vec, + /// Subscribed topics. + pub(crate) topics: BTreeSet, } /// Describes the types of peers that can exist in the gossipsub context. diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index 83984448d07..c5778ff92ee 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -1,3 +1,18 @@ +## 0.45.1 + +- Add `hide_listen_addrs` option to prevent leaking (local) listen addresses. + See [PR 5507](https://github.com/libp2p/rust-libp2p/pull/5507). + +## 0.45.0 + +- Address translation is moved here from `libp2p-core`. + See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568) + + + +- Add `ConnectionId` in `Event`. + See [PR 4981](https://github.com/libp2p/rust-libp2p/pull/4981). + ## 0.44.2 - Emit `ToSwarm::NewExternalAddrOfPeer` for all external addresses of remote peers. diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index 340df99e4dc..c3fb585c99c 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -2,8 +2,8 @@ name = "libp2p-identify" edition = "2021" rust-version = { workspace = true } -description = "Nodes identifcation protocol for libp2p" -version = "0.44.2" +description = "Nodes identification protocol for libp2p" +version = "0.45.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -12,33 +12,31 @@ categories = ["network-programming", "asynchronous"] [dependencies] asynchronous-codec = { workspace = true } -futures = "0.3.30" +futures = { workspace = true } futures-timer = "3.0.3" futures-bounded = { workspace = true } libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true } -lru = "0.12.2" +lru = "0.12.3" quick-protobuf-codec = { workspace = true } quick-protobuf = "0.8" -smallvec = "1.13.1" +smallvec = "1.13.2" thiserror = "1.0" -tracing = "0.1.37" +tracing = { workspace = true } void = "1.0" -either = "1.9.0" +either = "1.12.0" [dev-dependencies] async-std = { version = "1.6.2", features = ["attributes"] } libp2p-swarm-test = { path = "../../swarm-test" } libp2p-swarm = { workspace = true, features = ["macros"] } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 43bddb52fe7..c8672674c1a 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -20,6 +20,8 @@ use crate::handler::{self, Handler, InEvent}; use crate::protocol::{Info, UpgradeError}; +use libp2p_core::multiaddr::Protocol; +use libp2p_core::transport::PortUse; use libp2p_core::{multiaddr, ConnectedPoint, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_identity::PublicKey; @@ -27,6 +29,7 @@ use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailu use libp2p_swarm::{ ConnectionDenied, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour, NotifyHandler, PeerAddresses, StreamUpgradeError, THandlerInEvent, ToSwarm, + _address_translation, }; use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent}; @@ -39,6 +42,50 @@ use std::{ time::Duration, }; +/// Whether an [`Multiaddr`] is a valid for the QUIC transport. +fn is_quic_addr(addr: &Multiaddr, v1: bool) -> bool { + use Protocol::*; + let mut iter = addr.iter(); + let Some(first) = iter.next() else { + return false; + }; + let Some(second) = iter.next() else { + return false; + }; + let Some(third) = iter.next() else { + return false; + }; + let fourth = iter.next(); + let fifth = iter.next(); + + matches!(first, Ip4(_) | Ip6(_) | Dns(_) | Dns4(_) | Dns6(_)) + && matches!(second, Udp(_)) + && if v1 { + matches!(third, QuicV1) + } else { + matches!(third, Quic) + } + && matches!(fourth, Some(P2p(_)) | None) + && fifth.is_none() +} + +fn is_tcp_addr(addr: &Multiaddr) -> bool { + use Protocol::*; + + let mut iter = addr.iter(); + + let first = match iter.next() { + None => return false, + Some(p) => p, + }; + let second = match iter.next() { + None => return false, + Some(p) => p, + }; + + matches!(first, Ip4(_) | Ip6(_) | Dns(_) | Dns4(_) | Dns6(_)) && matches!(second, Tcp(_)) +} + /// Network behaviour that automatically identifies nodes periodically, returns information /// about them, and answers identify queries from other nodes. /// @@ -52,6 +99,9 @@ pub struct Behaviour { /// The address a remote observed for us. our_observed_addresses: HashMap, + /// The outbound connections established without port reuse (require translation) + outbound_connections_with_ephemeral_port: HashSet, + /// Pending events to be emitted when polled. events: VecDeque>, /// The addresses of all peers that we have discovered. @@ -97,6 +147,12 @@ pub struct Config { /// /// Disabled by default. pub cache_size: usize, + + /// Whether to include our listen addresses in our responses. If enabled, + /// we will effectively only share our external addresses. + /// + /// Disabled by default. + pub hide_listen_addrs: bool, } impl Config { @@ -110,6 +166,7 @@ impl Config { interval: Duration::from_secs(5 * 60), push_listen_addr_updates: false, cache_size: 100, + hide_listen_addrs: false, } } @@ -139,6 +196,12 @@ impl Config { self.cache_size = cache_size; self } + + /// Configures whether we prevent sending out our listen addresses. + pub fn with_hide_listen_addrs(mut self, b: bool) -> Self { + self.hide_listen_addrs = b; + self + } } impl Behaviour { @@ -153,6 +216,7 @@ impl Behaviour { config, connected: HashMap::new(), our_observed_addresses: Default::default(), + outbound_connections_with_ephemeral_port: Default::default(), events: VecDeque::new(), discovered_peers, listen_addresses: Default::default(), @@ -207,11 +271,63 @@ impl Behaviour { } fn all_addresses(&self) -> HashSet { - self.listen_addresses - .iter() - .chain(self.external_addresses.iter()) - .cloned() - .collect() + let mut addrs = HashSet::from_iter(self.external_addresses.iter().cloned()); + if !self.config.hide_listen_addrs { + addrs.extend(self.listen_addresses.iter().cloned()); + }; + addrs + } + + fn emit_new_external_addr_candidate_event( + &mut self, + connection_id: ConnectionId, + observed: &Multiaddr, + ) { + if self + .outbound_connections_with_ephemeral_port + .contains(&connection_id) + { + // Apply address translation to the candidate address. + // For TCP without port-reuse, the observed address contains an ephemeral port which needs to be replaced by the port of a listen address. + let translated_addresses = { + let mut addrs: Vec<_> = self + .listen_addresses + .iter() + .filter_map(|server| { + if (is_tcp_addr(server) && is_tcp_addr(observed)) + || (is_quic_addr(server, true) && is_quic_addr(observed, true)) + || (is_quic_addr(server, false) && is_quic_addr(observed, false)) + { + _address_translation(server, observed) + } else { + None + } + }) + .collect(); + + // remove duplicates + addrs.sort_unstable(); + addrs.dedup(); + addrs + }; + + // If address translation yielded nothing, broadcast the original candidate address. + if translated_addresses.is_empty() { + self.events + .push_back(ToSwarm::NewExternalAddrCandidate(observed.clone())); + } else { + for addr in translated_addresses { + self.events + .push_back(ToSwarm::NewExternalAddrCandidate(addr)); + } + } + return; + } + + // outgoing connection dialed with port reuse + // incomming connection + self.events + .push_back(ToSwarm::NewExternalAddrCandidate(observed.clone())); } } @@ -239,11 +355,25 @@ impl NetworkBehaviour for Behaviour { fn handle_established_outbound_connection( &mut self, - _: ConnectionId, + connection_id: ConnectionId, peer: PeerId, addr: &Multiaddr, _: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied> { + // Contrary to inbound events, outbound events are full-p2p qualified + // so we remove /p2p/ in order to be homogeneous + // this will avoid Autonatv2 to probe twice the same address (fully-p2p-qualified + not fully-p2p-qualified) + let mut addr = addr.clone(); + if matches!(addr.iter().last(), Some(multiaddr::Protocol::P2p(_))) { + addr.pop(); + } + + if port_use == PortUse::New { + self.outbound_connections_with_ephemeral_port + .insert(connection_id); + } + Ok(Handler::new( self.config.interval, peer, @@ -258,7 +388,7 @@ impl NetworkBehaviour for Behaviour { fn on_connection_handler_event( &mut self, peer_id: PeerId, - id: ConnectionId, + connection_id: ConnectionId, event: THandlerOutEvent, ) { match event { @@ -270,6 +400,7 @@ impl NetworkBehaviour for Behaviour { let observed = info.observed_addr.clone(); self.events .push_back(ToSwarm::GenerateEvent(Event::Received { + connection_id, peer_id, info: info.clone(), })); @@ -285,11 +416,10 @@ impl NetworkBehaviour for Behaviour { } } - match self.our_observed_addresses.entry(id) { + match self.our_observed_addresses.entry(connection_id) { Entry::Vacant(not_yet_observed) => { not_yet_observed.insert(observed.clone()); - self.events - .push_back(ToSwarm::NewExternalAddrCandidate(observed)); + self.emit_new_external_addr_candidate_event(connection_id, &observed); } Entry::Occupied(already_observed) if already_observed.get() == &observed => { // No-op, we already observed this address. @@ -298,26 +428,33 @@ impl NetworkBehaviour for Behaviour { tracing::info!( old_address=%already_observed.get(), new_address=%observed, - "Our observed address on connection {id} changed", + "Our observed address on connection {connection_id} changed", ); *already_observed.get_mut() = observed.clone(); - self.events - .push_back(ToSwarm::NewExternalAddrCandidate(observed)); + self.emit_new_external_addr_candidate_event(connection_id, &observed); } } } handler::Event::Identification => { - self.events - .push_back(ToSwarm::GenerateEvent(Event::Sent { peer_id })); + self.events.push_back(ToSwarm::GenerateEvent(Event::Sent { + connection_id, + peer_id, + })); } handler::Event::IdentificationPushed(info) => { - self.events - .push_back(ToSwarm::GenerateEvent(Event::Pushed { peer_id, info })); + self.events.push_back(ToSwarm::GenerateEvent(Event::Pushed { + connection_id, + peer_id, + info, + })); } handler::Event::IdentificationError(error) => { - self.events - .push_back(ToSwarm::GenerateEvent(Event::Error { peer_id, error })); + self.events.push_back(ToSwarm::GenerateEvent(Event::Error { + connection_id, + peer_id, + error, + })); } } } @@ -394,6 +531,8 @@ impl NetworkBehaviour for Behaviour { } self.our_observed_addresses.remove(&connection_id); + self.outbound_connections_with_ephemeral_port + .remove(&connection_id); } FromSwarm::DialFailure(DialFailure { peer_id, error, .. }) => { if let (Some(peer_id), Some(cache), DialError::Transport(errors)) = @@ -415,6 +554,8 @@ impl NetworkBehaviour for Behaviour { pub enum Event { /// Identification information has been received from a peer. Received { + /// Identifier of the connection. + connection_id: ConnectionId, /// The peer that has been identified. peer_id: PeerId, /// The information provided by the peer. @@ -423,12 +564,16 @@ pub enum Event { /// Identification information of the local node has been sent to a peer in /// response to an identification request. Sent { + /// Identifier of the connection. + connection_id: ConnectionId, /// The peer that the information has been sent to. peer_id: PeerId, }, /// Identification information of the local node has been actively pushed to /// a peer. Pushed { + /// Identifier of the connection. + connection_id: ConnectionId, /// The peer that the information has been sent to. peer_id: PeerId, /// The full Info struct we pushed to the remote peer. Clients must @@ -437,6 +582,8 @@ pub enum Event { }, /// Error while attempting to identify the remote. Error { + /// Identifier of the connection. + connection_id: ConnectionId, /// The peer with whom the error originated. peer_id: PeerId, /// The error that occurred. @@ -444,6 +591,17 @@ pub enum Event { }, } +impl Event { + pub fn connection_id(&self) -> ConnectionId { + match self { + Event::Received { connection_id, .. } + | Event::Sent { connection_id, .. } + | Event::Pushed { connection_id, .. } + | Event::Error { connection_id, .. } => *connection_id, + } + } +} + /// If there is a given peer_id in the multiaddr, make sure it is the same as /// the given peer_id. If there is no peer_id for the peer in the mutiaddr, this returns true. fn multiaddr_matches_peer_id(addr: &Multiaddr, peer_id: &PeerId) -> bool { diff --git a/protocols/identify/src/lib.rs b/protocols/identify/src/lib.rs index 34928d5d7df..7d28e5b5cc7 100644 --- a/protocols/identify/src/lib.rs +++ b/protocols/identify/src/lib.rs @@ -29,9 +29,9 @@ //! # Important Discrepancies //! //! - **Using Identify with other protocols** Unlike some other libp2p implementations, -//! rust-libp2p does not treat Identify as a core protocol. This means that other protocols cannot -//! rely upon the existence of Identify, and need to be manually hooked up to Identify in order to -//! make use of its capabilities. +//! rust-libp2p does not treat Identify as a core protocol. This means that other protocols cannot +//! rely upon the existence of Identify, and need to be manually hooked up to Identify in order to +//! make use of its capabilities. //! //! # Usage //! diff --git a/protocols/identify/src/protocol.rs b/protocols/identify/src/protocol.rs index c6b22b00c0a..f4dfd544dd1 100644 --- a/protocols/identify/src/protocol.rs +++ b/protocols/identify/src/protocol.rs @@ -25,7 +25,6 @@ use libp2p_core::{multiaddr, Multiaddr}; use libp2p_identity as identity; use libp2p_identity::PublicKey; use libp2p_swarm::StreamProtocol; -use std::convert::TryFrom; use std::io; use thiserror::Error; diff --git a/protocols/identify/tests/smoke.rs b/protocols/identify/tests/smoke.rs index dd92d10979a..d624005408e 100644 --- a/protocols/identify/tests/smoke.rs +++ b/protocols/identify/tests/smoke.rs @@ -1,5 +1,4 @@ use futures::StreamExt; -use libp2p_core::multiaddr::Protocol; use libp2p_identify as identify; use libp2p_swarm::{Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; @@ -59,12 +58,7 @@ async fn periodic_identify() { assert_eq!(s1_info.protocol_version, "c"); assert_eq!(s1_info.agent_version, "d"); assert!(!s1_info.protocols.is_empty()); - assert_eq!( - s1_info.observed_addr, - swarm1_memory_listen - .clone() - .with(Protocol::P2p(swarm1_peer_id)) - ); + assert_eq!(s1_info.observed_addr, swarm1_memory_listen); assert!(s1_info.listen_addrs.contains(&swarm2_tcp_listen_addr)); assert!(s1_info.listen_addrs.contains(&swarm2_memory_listen)); @@ -228,6 +222,77 @@ async fn emits_unique_listen_addresses() { assert!(reported_addrs.contains(&(swarm2_peer_id, swarm2_tcp_listen_addr))); } +#[async_std::test] +async fn hides_listen_addresses() { + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); + + let mut swarm1 = Swarm::new_ephemeral(|identity| { + identify::Behaviour::new( + identify::Config::new("a".to_string(), identity.public()) + .with_agent_version("b".to_string()) + .with_interval(Duration::from_secs(1)) + .with_cache_size(10), + ) + }); + let mut swarm2 = Swarm::new_ephemeral(|identity| { + identify::Behaviour::new( + identify::Config::new("c".to_string(), identity.public()) + .with_agent_version("d".to_string()) + .with_hide_listen_addrs(true), + ) + }); + + let (_swarm2_mem_listen_addr, swarm2_tcp_listen_addr) = + swarm2.listen().with_tcp_addr_external().await; + let swarm2_peer_id = *swarm2.local_peer_id(); + swarm1.connect(&mut swarm2).await; + + async_std::task::spawn(swarm2.loop_on_next()); + + let swarm_events = futures::stream::poll_fn(|cx| swarm1.poll_next_unpin(cx)) + .take(8) + .collect::>() + .await; + + let infos = swarm_events + .iter() + .filter_map(|e| match e { + SwarmEvent::Behaviour(identify::Event::Received { info, .. }) => Some(info.clone()), + _ => None, + }) + .collect::>(); + + assert!( + infos.len() > 1, + "should exchange identify payload more than once" + ); + + let listen_addrs = infos + .iter() + .map(|i| i.listen_addrs.clone()) + .collect::>(); + + for addrs in listen_addrs { + assert_eq!(addrs.len(), 1); + assert!(addrs.contains(&swarm2_tcp_listen_addr)); + } + + let reported_addrs = swarm_events + .iter() + .filter_map(|e| match e { + SwarmEvent::NewExternalAddrOfPeer { peer_id, address } => { + Some((*peer_id, address.clone())) + } + _ => None, + }) + .collect::>(); + + assert_eq!(reported_addrs.len(), 1, "To have one TCP address of remote"); + assert!(reported_addrs.contains(&(swarm2_peer_id, swarm2_tcp_listen_addr))); +} + #[async_std::test] async fn identify_push() { let _ = tracing_subscriber::fmt() diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 0e781c18916..d0ab7986aad 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,10 +1,53 @@ -## 0.45.4 +## 0.47.0 +- Expose a kad query facility allowing specify num_results dynamicly. + See [PR 5555](https://github.com/libp2p/rust-libp2p/pull/5555). +- Add `mode` getter on `Behaviour`. + See [PR 5573](https://github.com/libp2p/rust-libp2p/pull/5573). + + +## 0.46.2 + +- Emit `ToSwarm::NewExternalAddrOfPeer`. + See [PR 5549](https://github.com/libp2p/rust-libp2p/pull/5549) + +## 0.46.1 + +- Use new provider record update strategy to prevent Sybil attack. + See [PR 5536](https://github.com/libp2p/rust-libp2p/pull/5536). + +## 0.46.0 + +- Included multiaddresses of found peers alongside peer IDs in `GetClosestPeers` query results. + See [PR 5475](https://github.com/libp2p/rust-libp2p/pull/5475) +- Changed `FIND_NODE` response: now includes a list of closest peers when querying the recipient peer ID. Previously, this request yielded an empty response. + See [PR 5270](https://github.com/libp2p/rust-libp2p/pull/5270). +- Update to DHT republish interval and expiration time defaults to 22h and 48h respectively, rationale in [libp2p/specs#451](https://github.com/libp2p/specs/pull/451). + See [PR 3230](https://github.com/libp2p/rust-libp2p/pull/3230). +- Use default dial conditions more consistently. + See [PR 4957](https://github.com/libp2p/rust-libp2p/pull/4957) +- QueryClose progress whenever closer in range, instead of having to be the closest. + See [PR 4934](https://github.com/libp2p/rust-libp2p/pull/4934). - Add periodic and automatic bootstrap. See [PR 4838](https://github.com/libp2p/rust-libp2p/pull/4838). - Make it mandatory to provide protocol names when creating a `kad::Config`. Deprecate `kad::Config::default()`, replaced by `kad::Config::new(StreamProtocol)`. See [PR 5122](https://github.com/libp2p/rust-libp2p/pull/5122). +- Compute `jobs_query_capacity` accurately. + See [PR 5148](https://github.com/libp2p/rust-libp2p/pull/5148). +- Derive `Copy` for `kbucket::key::Key`. + See [PR 5317](https://github.com/libp2p/rust-libp2p/pull/5317). +⁻ `KBucket` size can now be modified without changing the `K_VALUE`. + See [PR 5414](https://github.com/libp2p/rust-libp2p/pull/5414). +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + +- Correctly handle the `NoKnownPeers` error on automatic bootstrap. + See [PR 5349](https://github.com/libp2p/rust-libp2p/pull/5349). +- Improve automatic bootstrap triggering conditions: + trigger when the routing table is updated and we have less that `K_VALUE` peers in it, + trigger when a new listen address is discovered and we have no connected peers. + See [PR 5474](https://github.com/libp2p/rust-libp2p/pull/5474). ## 0.45.3 @@ -59,6 +102,7 @@ --> ## 0.44.5 + - Migrate to `quick-protobuf-codec` crate for codec logic. See [PR 4501]. @@ -184,7 +228,7 @@ - Instead of a single event `OutboundQueryCompleted`, there are now multiple events emitted, allowing the user to process them as they come in (via the new `OutboundQueryProgressed`). See `ProgressStep` to identify the final `OutboundQueryProgressed` of a single query. - To finish a query early, i.e. before the final `OutboundQueryProgressed` of the query, a caller needs to call `query.finish()`. - There is no more automatic caching of records. The user has to manually call `put_record_to` on the `QueryInfo::GetRecord.cache_candidates` to cache a record to a close peer that did not return the record on the foregone query. - See [PR 2712]. + See [PR 2712]. [PR 3085]: https://github.com/libp2p/rust-libp2p/pull/3085 [PR 3011]: https://github.com/libp2p/rust-libp2p/pull/3011 @@ -263,7 +307,7 @@ - Require owned key in `get_record()` method (see [PR 2477]). -- Merge NetworkBehaviour's inject_\* paired methods (see PR 2445). +- Merge NetworkBehaviour's inject\_\* paired methods (see PR 2445). [PR 2477]: https://github.com/libp2p/rust-libp2p/pull/2477 [PR 2445]: https://github.com/libp2p/rust-libp2p/pull/2445 diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index 3a91955cc06..5b95b8ac17d 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-kad" edition = "2021" rust-version = { workspace = true } description = "Kademlia protocol for libp2p" -version = "0.45.4" +version = "0.47.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -13,10 +13,10 @@ categories = ["network-programming", "asynchronous"] [dependencies] arrayvec = "0.7.4" bytes = "1" -either = "1.9" +either = "1.11" fnv = "1.0" asynchronous-codec = { workspace = true } -futures = "0.3.30" +futures = { workspace = true } libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } futures-bounded = { workspace = true } @@ -25,14 +25,14 @@ quick-protobuf-codec = { workspace = true } libp2p-identity = { workspace = true, features = ["rand"] } rand = "0.8" sha2 = "0.10.8" -smallvec = "1.13.1" +smallvec = "1.13.2" uint = "0.9" void = "1.0" futures-timer = "3.0.3" -instant = "0.1.12" +web-time = { workspace = true } serde = { version = "1.0", optional = true, features = ["derive"] } thiserror = "1" -tracing = "0.1.37" +tracing = { workspace = true } [dev-dependencies] async-std = { version = "1.12.0", features = ["attributes"] } @@ -43,7 +43,7 @@ libp2p-swarm = { path = "../../swarm", features = ["macros"] } libp2p-swarm-test = { path = "../../swarm-test" } libp2p-yamux = { workspace = true } quickcheck = { workspace = true } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [features] serde = ["dep:serde", "bytes/serde"] @@ -52,8 +52,6 @@ serde = ["dep:serde", "bytes/serde"] # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 31568b0f9a8..0b15e507ba4 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -23,9 +23,8 @@ mod test; use crate::addresses::Addresses; -use crate::bootstrap; use crate::handler::{Handler, HandlerEvent, HandlerIn, RequestId}; -use crate::kbucket::{self, Distance, KBucketsTable, NodeStatus}; +use crate::kbucket::{self, Distance, KBucketConfig, KBucketsTable, NodeStatus}; use crate::protocol::{ConnectionType, KadPeer, ProtocolConfig}; use crate::query::{Query, QueryConfig, QueryId, QueryPool, QueryPoolState}; use crate::record::{ @@ -33,11 +32,10 @@ use crate::record::{ store::{self, RecordStore}, ProviderRecord, Record, }; -use crate::K_VALUE; +use crate::{bootstrap, K_VALUE}; use crate::{jobs::*, protocol}; -use fnv::{FnvHashMap, FnvHashSet}; -use instant::Instant; -use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr}; +use fnv::FnvHashSet; +use libp2p_core::{transport::PortUse, ConnectedPoint, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::behaviour::{ AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm, @@ -48,7 +46,6 @@ use libp2p_swarm::{ ListenAddresses, NetworkBehaviour, NotifyHandler, StreamProtocol, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm, }; -use smallvec::SmallVec; use std::collections::{BTreeMap, HashMap, HashSet, VecDeque}; use std::fmt; use std::num::NonZeroUsize; @@ -57,6 +54,7 @@ use std::time::Duration; use std::vec; use thiserror::Error; use tracing::Level; +use web_time::Instant; pub use crate::query::QueryStats; @@ -76,7 +74,7 @@ pub struct Behaviour { record_filtering: StoreInserts, /// The currently active (i.e. in-progress) queries. - queries: QueryPool, + queries: QueryPool, /// The currently connected peers. /// @@ -174,7 +172,7 @@ pub enum StoreInserts { /// The configuration is consumed by [`Behaviour::new`]. #[derive(Debug, Clone)] pub struct Config { - kbucket_pending_timeout: Duration, + kbucket_config: KBucketConfig, query_config: QueryConfig, protocol_config: ProtocolConfig, record_ttl: Option, @@ -217,15 +215,15 @@ impl Config { /// Builds a new `Config` with the given protocol name. pub fn new(protocol_name: StreamProtocol) -> Self { Config { - kbucket_pending_timeout: Duration::from_secs(60), + kbucket_config: KBucketConfig::default(), query_config: QueryConfig::default(), protocol_config: ProtocolConfig::new(protocol_name), - record_ttl: Some(Duration::from_secs(36 * 60 * 60)), + record_ttl: Some(Duration::from_secs(48 * 60 * 60)), record_replication_interval: Some(Duration::from_secs(60 * 60)), - record_publication_interval: Some(Duration::from_secs(24 * 60 * 60)), + record_publication_interval: Some(Duration::from_secs(22 * 60 * 60)), record_filtering: StoreInserts::Unfiltered, provider_publication_interval: Some(Duration::from_secs(12 * 60 * 60)), - provider_record_ttl: Some(Duration::from_secs(24 * 60 * 60)), + provider_record_ttl: Some(Duration::from_secs(48 * 60 * 60)), kbucket_inserts: BucketInserts::OnConnected, caching: Caching::Enabled { max_peers: 1 }, periodic_bootstrap_interval: Some(Duration::from_secs(5 * 60)), @@ -270,7 +268,7 @@ impl Config { /// Sets the replication factor to use. /// /// The replication factor determines to how many closest peers - /// a record is replicated. The default is [`K_VALUE`]. + /// a record is replicated. The default is [`crate::K_VALUE`]. pub fn set_replication_factor(&mut self, replication_factor: NonZeroUsize) -> &mut Self { self.query_config.replication_factor = replication_factor; self @@ -426,10 +424,28 @@ impl Config { self } + /// Sets the configuration for the k-buckets. + /// + /// * Default to K_VALUE. + pub fn set_kbucket_size(&mut self, size: NonZeroUsize) -> &mut Self { + self.kbucket_config.set_bucket_size(size); + self + } + + /// Sets the timeout duration after creation of a pending entry after which + /// it becomes eligible for insertion into a full bucket, replacing the + /// least-recently (dis)connected node. + /// + /// * Default to `60` s. + pub fn set_kbucket_pending_timeout(&mut self, timeout: Duration) -> &mut Self { + self.kbucket_config.set_pending_timeout(timeout); + self + } + /// Sets the time to wait before calling [`Behaviour::bootstrap`] after a new peer is inserted in the routing table. /// This prevent cascading bootstrap requests when multiple peers are inserted into the routing table "at the same time". /// This also allows to wait a little bit for other potential peers to be inserted into the routing table before - /// triggering a bootstrap, giving more context to the future bootstrap request. + /// triggering a bootstrap, giving more context to the future bootstrap request. /// /// * Default to `500` ms. /// * Set to `Some(Duration::ZERO)` to never wait before triggering a bootstrap request when a new peer @@ -483,7 +499,7 @@ where Behaviour { store, caching: config.caching, - kbuckets: KBucketsTable::new(local_key, config.kbucket_pending_timeout), + kbuckets: KBucketsTable::new(local_key, config.kbucket_config), kbucket_inserts: config.kbucket_inserts, protocol_config: config.protocol_config, record_filtering: config.record_filtering, @@ -606,7 +622,8 @@ where }; match entry.insert(addresses.clone(), status) { kbucket::InsertResult::Inserted => { - self.bootstrap_status.on_new_peer_in_routing_table(); + self.bootstrap_on_low_peers(); + self.queued_events.push_back(ToSwarm::GenerateEvent( Event::RoutingUpdated { peer: *peer, @@ -628,9 +645,7 @@ where } kbucket::InsertResult::Pending { disconnected } => { self.queued_events.push_back(ToSwarm::Dial { - opts: DialOpts::peer_id(disconnected.into_preimage()) - .condition(dial_opts::PeerCondition::NotDialing) - .build(), + opts: DialOpts::peer_id(disconnected.into_preimage()).build(), }); RoutingUpdate::Pending } @@ -717,6 +732,31 @@ where /// The result of the query is delivered in a /// [`Event::OutboundQueryProgressed{QueryResult::GetClosestPeers}`]. pub fn get_closest_peers(&mut self, key: K) -> QueryId + where + K: Into> + Into> + Clone, + { + self.get_closest_peers_inner(key, None) + } + + /// Initiates an iterative query for the closest peers to the given key. + /// The expected responding peers is specified by `num_results` + /// Note that the result is capped after exceeds K_VALUE + /// + /// The result of the query is delivered in a + /// [`Event::OutboundQueryProgressed{QueryResult::GetClosestPeers}`]. + pub fn get_n_closest_peers(&mut self, key: K, num_results: NonZeroUsize) -> QueryId + where + K: Into> + Into> + Clone, + { + // The inner code never expect higher than K_VALUE results to be returned. + // And removing such cap will be tricky, + // since it would involve forging a new key and additional requests. + // Hence bound to K_VALUE here to set clear expectation and prevent unexpected behaviour. + let capped_num_results = std::cmp::min(num_results, K_VALUE); + self.get_closest_peers_inner(key, Some(capped_num_results)) + } + + fn get_closest_peers_inner(&mut self, key: K, num_results: Option) -> QueryId where K: Into> + Into> + Clone, { @@ -725,10 +765,10 @@ where let info = QueryInfo::GetClosestPeers { key, step: ProgressStep::first(), + num_results, }; let peer_keys: Vec> = self.kbuckets.closest_keys(&target).collect(); - let inner = QueryInner::new(info); - self.queries.add_iter_closest(target, peer_keys, inner) + self.queries.add_iter_closest(target, peer_keys, info) } /// Returns closest peers to the given key; takes peers from local routing table only. @@ -777,8 +817,7 @@ where } }; let peers = self.kbuckets.closest_keys(&target); - let inner = QueryInner::new(info); - let id = self.queries.add_iter_closest(target.clone(), peers, inner); + let id = self.queries.add_iter_closest(target.clone(), peers, info); // No queries were actually done for the results yet. let stats = QueryStats::empty(); @@ -834,8 +873,7 @@ where quorum, phase: PutRecordPhase::GetClosestPeers, }; - let inner = QueryInner::new(info); - Ok(self.queries.add_iter_closest(target.clone(), peers, inner)) + Ok(self.queries.add_iter_closest(target.clone(), peers, info)) } /// Stores a record at specific peers, without storing it locally. @@ -880,8 +918,7 @@ where get_closest_peers_stats: QueryStats::empty(), }, }; - let inner = QueryInner::new(info); - self.queries.add_fixed(peers, inner) + self.queries.add_fixed(peers, info) } /// Removes the record with the given key from _local_ storage, @@ -927,13 +964,13 @@ where /// > See [`Behaviour::add_address`]. /// /// > **Note**: Bootstrap does not require to be called manually. It is periodically - /// invoked at regular intervals based on the configured `periodic_bootstrap_interval` (see - /// [`Config::set_periodic_bootstrap_interval`] for details) and it is also automatically invoked - /// when a new peer is inserted in the routing table. - /// This parameter is used to call [`Behaviour::bootstrap`] periodically and automatically - /// to ensure a healthy routing table. + /// > invoked at regular intervals based on the configured `periodic_bootstrap_interval` (see + /// > [`Config::set_periodic_bootstrap_interval`] for details) and it is also automatically invoked + /// > when a new peer is inserted in the routing table. + /// > This parameter is used to call [`Behaviour::bootstrap`] periodically and automatically + /// > to ensure a healthy routing table. pub fn bootstrap(&mut self) -> Result { - let local_key = self.kbuckets.local_key().clone(); + let local_key = *self.kbuckets.local_key(); let info = QueryInfo::Bootstrap { peer: *local_key.preimage(), remaining: None, @@ -941,11 +978,11 @@ where }; let peers = self.kbuckets.closest_keys(&local_key).collect::>(); if peers.is_empty() { + self.bootstrap_status.reset_timers(); Err(NoKnownPeers()) } else { self.bootstrap_status.on_started(); - let inner = QueryInner::new(info); - Ok(self.queries.add_iter_closest(local_key, peers, inner)) + Ok(self.queries.add_iter_closest(local_key, peers, info)) } } @@ -990,8 +1027,7 @@ where key, phase: AddProviderPhase::GetClosestPeers, }; - let inner = QueryInner::new(info); - let id = self.queries.add_iter_closest(target.clone(), peers, inner); + let id = self.queries.add_iter_closest(target.clone(), peers, info); Ok(id) } @@ -1031,8 +1067,7 @@ where let target = kbucket::Key::new(key.clone()); let peers = self.kbuckets.closest_keys(&target); - let inner = QueryInner::new(info); - let id = self.queries.add_iter_closest(target.clone(), peers, inner); + let id = self.queries.add_iter_closest(target.clone(), peers, info); // No queries were actually done for the results yet. let stats = QueryStats::empty(); @@ -1076,6 +1111,11 @@ where } } + /// Get the [`Mode`] in which the DHT is currently operating. + pub fn mode(&self) -> Mode { + self.mode + } + fn reconfigure_mode(&mut self) { if self.connections.is_empty() { return; @@ -1166,7 +1206,7 @@ where "Peer reported by source in query" ); let addrs = peer.multiaddrs.iter().cloned().collect(); - query.inner.addresses.insert(peer.node_id, addrs); + query.peers.addresses.insert(peer.node_id, addrs); } query.on_success(source, others_iter.cloned().map(|kp| kp.node_id)) } @@ -1180,16 +1220,12 @@ where target: &kbucket::Key, source: &PeerId, ) -> Vec { - if target == self.kbuckets.local_key() { - Vec::new() - } else { - self.kbuckets - .closest(target) - .filter(|e| e.node.key.preimage() != source) - .take(self.queries.config().replication_factor.get()) - .map(KadPeer::from) - .collect() - } + self.kbuckets + .closest(target) + .filter(|e| e.node.key.preimage() != source) + .take(self.queries.config().replication_factor.get()) + .map(KadPeer::from) + .collect() } /// Collects all peers who are known to be providers of the value for a given `Multihash`. @@ -1259,8 +1295,7 @@ where }; let target = kbucket::Key::new(key); let peers = self.kbuckets.closest_keys(&target); - let inner = QueryInner::new(info); - self.queries.add_iter_closest(target.clone(), peers, inner); + self.queries.add_iter_closest(target.clone(), peers, info); } /// Starts an iterative `PUT_VALUE` query for the given record. @@ -1274,8 +1309,7 @@ where context, phase: PutRecordPhase::GetClosestPeers, }; - let inner = QueryInner::new(info); - self.queries.add_iter_closest(target.clone(), peers, inner); + self.queries.add_iter_closest(target.clone(), peers, info); } /// Updates the routing table with a new connection status and address of a peer. @@ -1340,7 +1374,8 @@ where let addresses = Addresses::new(a); match entry.insert(addresses.clone(), new_status) { kbucket::InsertResult::Inserted => { - self.bootstrap_status.on_new_peer_in_routing_table(); + self.bootstrap_on_low_peers(); + let event = Event::RoutingUpdated { peer, is_new_peer: true, @@ -1379,7 +1414,6 @@ where if !self.connected_peers.contains(disconnected.preimage()) { self.queued_events.push_back(ToSwarm::Dial { opts: DialOpts::peer_id(disconnected.into_preimage()) - .condition(dial_opts::PeerCondition::NotDialing) .build(), }) } @@ -1392,18 +1426,31 @@ where } } + /// A new peer has been inserted in the routing table but we check if the routing + /// table is currently small (less that `K_VALUE` peers are present) and only + /// trigger a bootstrap in that case + fn bootstrap_on_low_peers(&mut self) { + if self + .kbuckets() + .map(|kbucket| kbucket.num_entries()) + .sum::() + < K_VALUE.get() + { + self.bootstrap_status.trigger(); + } + } + /// Handles a finished (i.e. successful) query. - fn query_finished(&mut self, q: Query) -> Option { + fn query_finished(&mut self, q: Query) -> Option { let query_id = q.id(); tracing::trace!(query=?query_id, "Query finished"); - let result = q.into_result(); - match result.inner.info { + match q.info { QueryInfo::Bootstrap { peer, remaining, mut step, } => { - let local_key = self.kbuckets.local_key().clone(); + let local_key = *self.kbuckets.local_key(); let mut remaining = remaining.unwrap_or_else(|| { debug_assert_eq!(&peer, local_key.preimage()); // The lookup for the local key finished. To complete the bootstrap process, @@ -1451,9 +1498,8 @@ where step: step.next(), }; let peers = self.kbuckets.closest_keys(&target); - let inner = QueryInner::new(info); self.queries - .continue_iter_closest(query_id, target.clone(), peers, inner); + .continue_iter_closest(query_id, target, peers, info); } else { step.last = true; self.bootstrap_status.on_finish(); @@ -1461,7 +1507,7 @@ where Some(Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: q.stats, result: QueryResult::Bootstrap(Ok(BootstrapOk { peer, num_remaining, @@ -1470,15 +1516,15 @@ where }) } - QueryInfo::GetClosestPeers { key, mut step } => { + QueryInfo::GetClosestPeers { key, mut step, .. } => { step.last = true; Some(Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: q.stats, result: QueryResult::GetClosestPeers(Ok(GetClosestPeersOk { key, - peers: result.peers.collect(), + peers: q.peers.into_peerinfos_iter().collect(), })), step, }) @@ -1489,10 +1535,10 @@ where Some(Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: q.stats, result: QueryResult::GetProviders(Ok( GetProvidersOk::FinishedWithNoAdditionalRecord { - closest_peers: result.peers.collect(), + closest_peers: q.peers.into_peerids_iter().collect(), }, )), step, @@ -1506,16 +1552,17 @@ where } => { let provider_id = self.local_peer_id; let external_addresses = self.external_addresses.iter().cloned().collect(); - let inner = QueryInner::new(QueryInfo::AddProvider { + let info = QueryInfo::AddProvider { context, key, phase: AddProviderPhase::AddProvider { provider_id, external_addresses, - get_closest_peers_stats: result.stats, + get_closest_peers_stats: q.stats, }, - }); - self.queries.continue_fixed(query_id, result.peers, inner); + }; + self.queries + .continue_fixed(query_id, q.peers.into_peerids_iter(), info); None } @@ -1530,13 +1577,13 @@ where } => match context { AddProviderContext::Publish => Some(Event::OutboundQueryProgressed { id: query_id, - stats: get_closest_peers_stats.merge(result.stats), + stats: get_closest_peers_stats.merge(q.stats), result: QueryResult::StartProviding(Ok(AddProviderOk { key })), step: ProgressStep::first_and_last(), }), AddProviderContext::Republish => Some(Event::OutboundQueryProgressed { id: query_id, - stats: get_closest_peers_stats.merge(result.stats), + stats: get_closest_peers_stats.merge(q.stats), result: QueryResult::RepublishProvider(Ok(AddProviderOk { key })), step: ProgressStep::first_and_last(), }), @@ -1555,12 +1602,12 @@ where } else { Err(GetRecordError::NotFound { key, - closest_peers: result.peers.collect(), + closest_peers: q.peers.into_peerids_iter().collect(), }) }; Some(Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: q.stats, result: QueryResult::GetRecord(results), step, }) @@ -1578,11 +1625,11 @@ where quorum, phase: PutRecordPhase::PutRecord { success: vec![], - get_closest_peers_stats: result.stats, + get_closest_peers_stats: q.stats, }, }; - let inner = QueryInner::new(info); - self.queries.continue_fixed(query_id, result.peers, inner); + self.queries + .continue_fixed(query_id, q.peers.into_peerids_iter(), info); None } @@ -1611,14 +1658,14 @@ where PutRecordContext::Publish | PutRecordContext::Custom => { Some(Event::OutboundQueryProgressed { id: query_id, - stats: get_closest_peers_stats.merge(result.stats), + stats: get_closest_peers_stats.merge(q.stats), result: QueryResult::PutRecord(mk_result(record.key)), step: ProgressStep::first_and_last(), }) } PutRecordContext::Republish => Some(Event::OutboundQueryProgressed { id: query_id, - stats: get_closest_peers_stats.merge(result.stats), + stats: get_closest_peers_stats.merge(q.stats), result: QueryResult::RepublishRecord(mk_result(record.key)), step: ProgressStep::first_and_last(), }), @@ -1632,11 +1679,10 @@ where } /// Handles a query that timed out. - fn query_timeout(&mut self, query: Query) -> Option { + fn query_timeout(&mut self, query: Query) -> Option { let query_id = query.id(); tracing::trace!(query=?query_id, "Query timed out"); - let result = query.into_result(); - match result.inner.info { + match query.info { QueryInfo::Bootstrap { peer, mut remaining, @@ -1649,14 +1695,13 @@ where remaining.take().and_then(|mut r| Some((r.next()?, r))) { let info = QueryInfo::Bootstrap { - peer: target.clone().into_preimage(), + peer: target.into_preimage(), remaining: Some(remaining), step: step.next(), }; let peers = self.kbuckets.closest_keys(&target); - let inner = QueryInner::new(info); self.queries - .continue_iter_closest(query_id, target.clone(), peers, inner); + .continue_iter_closest(query_id, target, peers, info); } else { step.last = true; self.bootstrap_status.on_finish(); @@ -1664,7 +1709,7 @@ where Some(Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: query.stats, result: QueryResult::Bootstrap(Err(BootstrapError::Timeout { peer, num_remaining, @@ -1676,27 +1721,26 @@ where QueryInfo::AddProvider { context, key, .. } => Some(match context { AddProviderContext::Publish => Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: query.stats, result: QueryResult::StartProviding(Err(AddProviderError::Timeout { key })), step: ProgressStep::first_and_last(), }, AddProviderContext::Republish => Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: query.stats, result: QueryResult::RepublishProvider(Err(AddProviderError::Timeout { key })), step: ProgressStep::first_and_last(), }, }), - QueryInfo::GetClosestPeers { key, mut step } => { + QueryInfo::GetClosestPeers { key, mut step, .. } => { step.last = true; - Some(Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: query.stats, result: QueryResult::GetClosestPeers(Err(GetClosestPeersError::Timeout { key, - peers: result.peers.collect(), + peers: query.peers.into_peerinfos_iter().collect(), })), step, }) @@ -1720,14 +1764,14 @@ where PutRecordContext::Publish | PutRecordContext::Custom => { Some(Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: query.stats, result: QueryResult::PutRecord(err), step: ProgressStep::first_and_last(), }) } PutRecordContext::Republish => Some(Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: query.stats, result: QueryResult::RepublishRecord(err), step: ProgressStep::first_and_last(), }), @@ -1752,7 +1796,7 @@ where Some(Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: query.stats, result: QueryResult::GetRecord(Err(GetRecordError::Timeout { key })), step, }) @@ -1763,10 +1807,10 @@ where Some(Event::OutboundQueryProgressed { id: query_id, - stats: result.stats, + stats: query.stats, result: QueryResult::GetProviders(Err(GetProvidersError::Timeout { key, - closest_peers: result.peers.collect(), + closest_peers: query.peers.into_peerids_iter().collect(), })), step, }) @@ -1964,7 +2008,7 @@ where } for query in self.queries.iter_mut() { - if let Some(addrs) = query.inner.addresses.get_mut(&peer_id) { + if let Some(addrs) = query.peers.addresses.get_mut(&peer_id) { addrs.retain(|a| a != address); } } @@ -2036,10 +2080,10 @@ where // // Given two connected nodes: local node A and remote node B. Say node B // is not in node A's routing table. Additionally node B is part of the - // `QueryInner::addresses` list of an ongoing query on node A. Say Node + // `Query::addresses` list of an ongoing query on node A. Say Node // B triggers an address change and then disconnects. Later on the // earlier mentioned query on node A would like to connect to node B. - // Without replacing the address in the `QueryInner::addresses` set node + // Without replacing the address in the `Query::addresses` set node // A would attempt to dial the old and not the new address. // // While upholding correctness, iterating through all discovered @@ -2047,7 +2091,7 @@ where // large performance impact. If so, the code below might be worth // revisiting. for query in self.queries.iter_mut() { - if let Some(addrs) = query.inner.addresses.get_mut(&peer) { + if let Some(addrs) = query.peers.addresses.get_mut(&peer) { for addr in addrs.iter_mut() { if addr == old { *addr = new.clone(); @@ -2122,11 +2166,10 @@ where // Queue events for sending pending RPCs to the connected peer. // There can be only one pending RPC for a particular peer and query per definition. for (_peer_id, event) in self.queries.iter_mut().filter_map(|q| { - q.inner - .pending_rpcs + q.pending_rpcs .iter() .position(|(p, _)| p == &peer) - .map(|p| q.inner.pending_rpcs.remove(p)) + .map(|p| q.pending_rpcs.remove(p)) }) { handler.on_behaviour_event(event) } @@ -2174,10 +2217,12 @@ where peer: PeerId, addr: &Multiaddr, role_override: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied> { let connected_point = ConnectedPoint::Dialer { address: addr.clone(), role_override, + port_use, }; let mut handler = Handler::new( @@ -2203,7 +2248,7 @@ where Some(peer) => peer, }; - // We should order addresses from decreasing likelyhood of connectivity, so start with + // We should order addresses from decreasing likelihood of connectivity, so start with // the addresses of that peer in the k-buckets. let key = kbucket::Key::from(peer_id); let mut peer_addrs = @@ -2217,7 +2262,7 @@ where // We add to that a temporary list of addresses from the ongoing queries. for query in self.queries.iter() { - if let Some(addrs) = query.inner.addresses.get(&peer_id) { + if let Some(addrs) = query.peers.addresses.get(&peer_id) { peer_addrs.extend(addrs.iter().cloned()) } } @@ -2318,7 +2363,7 @@ where ref mut providers_found, ref mut step, .. - } = query.inner.info + } = query.info { *providers_found += provider_peers.len(); let providers = provider_peers.iter().map(|p| p.node_id).collect(); @@ -2410,7 +2455,7 @@ where ref mut step, ref mut found_a_record, cache_candidates, - } = &mut query.inner.info + } = &mut query.info { if let Some(record) = record { *found_a_record = true; @@ -2464,7 +2509,7 @@ where phase: PutRecordPhase::PutRecord { success, .. }, quorum, .. - } = &mut query.inner.info + } = &mut query.info { success.push(source); @@ -2502,14 +2547,14 @@ where // Run the periodic provider announcement job. if let Some(mut job) = self.add_provider_job.take() { let num = usize::min(JOBS_MAX_NEW_QUERIES, jobs_query_capacity); - for _ in 0..num { + for i in 0..num { if let Poll::Ready(r) = job.poll(cx, &mut self.store, now) { self.start_add_provider(r.key, AddProviderContext::Republish) } else { + jobs_query_capacity -= i; break; } } - jobs_query_capacity -= num; self.add_provider_job = Some(job); } @@ -2548,13 +2593,19 @@ where // Drain applied pending entries from the routing table. if let Some(entry) = self.kbuckets.take_applied_pending() { let kbucket::Node { key, value } = entry.inserted; + let peer_id = key.into_preimage(); + self.queued_events + .push_back(ToSwarm::NewExternalAddrOfPeer { + peer_id, + address: value.first().clone(), + }); let event = Event::RoutingUpdated { bucket_range: self .kbuckets .bucket(&key) .map(|b| b.range()) .expect("Self to never be applied from pending."), - peer: key.into_preimage(), + peer: peer_id, is_new_peer: true, addresses: value, old_peer: entry.evicted.map(|n| n.key.into_preimage()), @@ -2576,7 +2627,7 @@ where } } QueryPoolState::Waiting(Some((query, peer_id))) => { - let event = query.inner.info.to_request(query.id()); + let event = query.info.to_request(query.id()); // TODO: AddProvider requests yield no response, so the query completes // as soon as all requests have been sent. However, the handler should // better emit an event when the request has been sent (and report @@ -2585,7 +2636,7 @@ where if let QueryInfo::AddProvider { phase: AddProviderPhase::AddProvider { .. }, .. - } = &query.inner.info + } = &query.info { query.on_success(&peer_id, vec![]) } @@ -2597,11 +2648,9 @@ where handler: NotifyHandler::Any, }); } else if &peer_id != self.kbuckets.local_key().preimage() { - query.inner.pending_rpcs.push((peer_id, event)); + query.pending_rpcs.push((peer_id, event)); self.queued_events.push_back(ToSwarm::Dial { - opts: DialOpts::peer_id(peer_id) - .condition(dial_opts::PeerCondition::NotDialing) - .build(), + opts: DialOpts::peer_id(peer_id).build(), }); } } @@ -2637,11 +2686,24 @@ where } FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure), FromSwarm::AddressChange(address_change) => self.on_address_change(address_change), + FromSwarm::NewListenAddr(_) if self.connected_peers.is_empty() => { + // A new listen addr was just discovered and we have no connected peers, + // it can mean that our network interfaces were not up but they are now + // so it might be a good idea to trigger a bootstrap. + self.bootstrap_status.trigger(); + } _ => {} } } } +/// Peer Info combines a Peer ID with a set of multiaddrs that the peer is listening on. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PeerInfo { + pub peer_id: PeerId, + pub addrs: Vec, +} + /// A quorum w.r.t. the configured replication factor specifies the minimum /// number of distinct nodes that must be successfully contacted in order /// for a query to succeed. @@ -2986,14 +3048,14 @@ pub type GetClosestPeersResult = Result #[derive(Debug, Clone)] pub struct GetClosestPeersOk { pub key: Vec, - pub peers: Vec, + pub peers: Vec, } /// The error result of [`Behaviour::get_closest_peers`]. #[derive(Debug, Clone, Error)] pub enum GetClosestPeersError { #[error("the request timed out")] - Timeout { key: Vec, peers: Vec }, + Timeout { key: Vec, peers: Vec }, } impl GetClosestPeersError { @@ -3101,31 +3163,6 @@ impl From, Addresses>> for KadPeer { } } -////////////////////////////////////////////////////////////////////////////// -// Internal query state - -struct QueryInner { - /// The query-specific state. - info: QueryInfo, - /// Addresses of peers discovered during a query. - addresses: FnvHashMap>, - /// A map of pending requests to peers. - /// - /// A request is pending if the targeted peer is not currently connected - /// and these requests are sent as soon as a connection to the peer is established. - pending_rpcs: SmallVec<[(PeerId, HandlerIn); K_VALUE.get()]>, -} - -impl QueryInner { - fn new(info: QueryInfo) -> Self { - QueryInner { - info, - addresses: Default::default(), - pending_rpcs: SmallVec::default(), - } - } -} - /// The context of a [`QueryInfo::AddProvider`] query. #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum AddProviderContext { @@ -3175,6 +3212,8 @@ pub enum QueryInfo { key: Vec, /// Current index of events. step: ProgressStep, + /// If required, `num_results` specifies expected responding peers + num_results: Option, }, /// A (repeated) query initiated by [`Behaviour::get_providers`]. @@ -3311,7 +3350,7 @@ pub enum PutRecordPhase { /// A mutable reference to a running query. pub struct QueryMut<'a> { - query: &'a mut Query, + query: &'a mut Query, } impl<'a> QueryMut<'a> { @@ -3321,7 +3360,7 @@ impl<'a> QueryMut<'a> { /// Gets information about the type and state of the query. pub fn info(&self) -> &QueryInfo { - &self.query.inner.info + &self.query.info } /// Gets execution statistics about the query. @@ -3341,7 +3380,7 @@ impl<'a> QueryMut<'a> { /// An immutable reference to a running query. pub struct QueryRef<'a> { - query: &'a Query, + query: &'a Query, } impl<'a> QueryRef<'a> { @@ -3351,7 +3390,7 @@ impl<'a> QueryRef<'a> { /// Gets information about the type and state of the query. pub fn info(&self) -> &QueryInfo { - &self.query.inner.info + &self.query.info } /// Gets execution statistics about the query. diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index b879084c54f..7409168ac2a 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -22,32 +22,22 @@ use super::*; -use crate::kbucket::Distance; use crate::record::{store::MemoryStore, Key}; use crate::{K_VALUE, PROTOCOL_NAME, SHA_256_MH}; use futures::{executor::block_on, future::poll_fn, prelude::*}; use futures_timer::Delay; use libp2p_core::{ - connection::ConnectedPoint, - multiaddr::{multiaddr, Multiaddr, Protocol}, + multiaddr::{multiaddr, Protocol}, multihash::Multihash, transport::MemoryTransport, - upgrade, Endpoint, Transport, + upgrade, Transport, }; use libp2p_identity as identity; -use libp2p_identity::PeerId; use libp2p_noise as noise; -use libp2p_swarm::behaviour::ConnectionEstablished; -use libp2p_swarm::{self as swarm, ConnectionId, Swarm, SwarmEvent}; +use libp2p_swarm::{self as swarm, Swarm, SwarmEvent}; use libp2p_yamux as yamux; use quickcheck::*; use rand::{random, rngs::StdRng, thread_rng, Rng, SeedableRng}; -use std::{ - collections::{HashMap, HashSet}, - num::NonZeroUsize, - time::Duration, - u64, -}; type TestSwarm = Swarm>; @@ -273,7 +263,7 @@ fn query_iter() { match swarms[0].behaviour_mut().query(&qid) { Some(q) => match q.info() { - QueryInfo::GetClosestPeers { key, step } => { + QueryInfo::GetClosestPeers { key, step, .. } => { assert_eq!(&key[..], search_target.to_bytes().as_slice()); assert_eq!(usize::from(step.count), 1); } @@ -304,9 +294,11 @@ fn query_iter() { assert_eq!(&ok.key[..], search_target.to_bytes().as_slice()); assert_eq!(swarm_ids[i], expected_swarm_id); assert_eq!(swarm.behaviour_mut().queries.size(), 0); - assert!(expected_peer_ids.iter().all(|p| ok.peers.contains(p))); + let peer_ids = + ok.peers.into_iter().map(|p| p.peer_id).collect::>(); + assert!(expected_peer_ids.iter().all(|p| peer_ids.contains(p))); let key = kbucket::Key::new(ok.key); - assert_eq!(expected_distances, distances(&key, ok.peers)); + assert_eq!(expected_distances, distances(&key, peer_ids)); return Poll::Ready(()); } // Ignore any other event. @@ -418,7 +410,69 @@ fn unresponsive_not_returned_indirect() { }))) => { assert_eq!(&ok.key[..], search_target.to_bytes().as_slice()); assert_eq!(ok.peers.len(), 1); - assert_eq!(ok.peers[0], first_peer_id); + assert_eq!(ok.peers[0].peer_id, first_peer_id); + return Poll::Ready(()); + } + // Ignore any other event. + Poll::Ready(Some(_)) => (), + e @ Poll::Ready(_) => panic!("Unexpected return value: {e:?}"), + Poll::Pending => break, + } + } + } + + Poll::Pending + })) +} + +// Test the result of get_closest_peers with different num_results +// Note that the result is capped after exceeds K_VALUE +#[test] +fn get_closest_with_different_num_results() { + let k_value = K_VALUE.get(); + for replication_factor in [5, k_value / 2, k_value] { + for num_results in k_value / 2..k_value * 2 { + get_closest_with_different_num_results_inner(num_results, replication_factor) + } + } +} + +fn get_closest_with_different_num_results_inner(num_results: usize, replication_factor: usize) { + let k_value = K_VALUE.get(); + let num_of_nodes = 3 * k_value; + let mut cfg = Config::new(PROTOCOL_NAME); + cfg.set_replication_factor(NonZeroUsize::new(replication_factor).unwrap()); + let swarms = build_connected_nodes_with_config(num_of_nodes, replication_factor - 1, cfg); + + let mut swarms = swarms + .into_iter() + .map(|(_addr, swarm)| swarm) + .collect::>(); + + // Ask first to search a random value. + let search_target = PeerId::random(); + let Some(num_results_nonzero) = std::num::NonZeroUsize::new(num_results) else { + panic!("Unexpected NonZeroUsize val of {num_results}"); + }; + swarms[0] + .behaviour_mut() + .get_n_closest_peers(search_target, num_results_nonzero); + + block_on(poll_fn(move |ctx| { + for swarm in &mut swarms { + loop { + match swarm.poll_next_unpin(ctx) { + Poll::Ready(Some(SwarmEvent::Behaviour(Event::OutboundQueryProgressed { + result: QueryResult::GetClosestPeers(Ok(ok)), + .. + }))) => { + assert_eq!(&ok.key[..], search_target.to_bytes().as_slice()); + if num_results > k_value { + assert_eq!(ok.peers.len(), k_value, "Failed with replication_factor: {replication_factor}, num_results: {num_results}"); + } else { + assert_eq!(ok.peers.len(), num_results, "Failed with replication_factor: {replication_factor}, num_results: {num_results}"); + } + return Poll::Ready(()); } // Ignore any other event. @@ -1195,7 +1249,7 @@ fn disjoint_query_does_not_finish_before_all_paths_did() { .behaviour() .queries .iter() - .for_each(|q| match &q.inner.info { + .for_each(|q| match &q.info { QueryInfo::GetRecord { step, .. } => { assert_eq!(usize::from(step.count), 2); } @@ -1318,6 +1372,7 @@ fn network_behaviour_on_address_change() { let endpoint = ConnectedPoint::Dialer { address: old_address.clone(), role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, }; // Mimick a connection being established. @@ -1368,10 +1423,12 @@ fn network_behaviour_on_address_change() { old: &ConnectedPoint::Dialer { address: old_address, role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, }, new: &ConnectedPoint::Dialer { address: new_address.clone(), role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, }, })); diff --git a/protocols/kad/src/bootstrap.rs b/protocols/kad/src/bootstrap.rs index 58fa662d414..40acdfd88ee 100644 --- a/protocols/kad/src/bootstrap.rs +++ b/protocols/kad/src/bootstrap.rs @@ -44,7 +44,8 @@ impl Status { } } - pub(crate) fn on_new_peer_in_routing_table(&mut self) { + /// Trigger a bootstrap now or after the configured `automatic_throttle` if configured. + pub(crate) fn trigger(&mut self) { // Registering `self.throttle_timer` means scheduling a bootstrap. // A bootstrap will be triggered when `self.throttle_timer` finishes. // A `throttle_timer` is useful to not trigger a batch of bootstraps when a @@ -61,19 +62,23 @@ impl Status { } } + pub(crate) fn reset_timers(&mut self) { + // Canceling the `throttle_timer` if any and resetting the `delay` if any. + self.throttle_timer = None; + + if let Some((interval, delay)) = self.interval_and_delay.as_mut() { + delay.reset(*interval); + } + } + pub(crate) fn on_started(&mut self) { // No periodic or automatic bootstrap will be triggered as long as // `self.current_bootstrap_requests > 0` but the user could still manually // trigger a bootstrap. self.current_bootstrap_requests += 1; - // Canceling the `throttle_timer` if any since a bootstrap request is being triggered right now. - self.throttle_timer = None; - - // Resetting the `delay` if any since a bootstrap request is being triggered right now. - if let Some((interval, delay)) = self.interval_and_delay.as_mut() { - delay.reset(*interval); - } + // Resetting the Status timers since a bootstrap request is being triggered right now. + self.reset_timers(); } pub(crate) fn on_finish(&mut self) { @@ -171,7 +176,7 @@ impl futures::Future for ThrottleTimer { #[cfg(test)] mod tests { use super::*; - use instant::Instant; + use web_time::Instant; const MS_5: Duration = Duration::from_millis(5); const MS_100: Duration = Duration::from_millis(100); @@ -197,7 +202,7 @@ mod tests { "bootstrap to not be triggered immediately because periodic bootstrap is in ~1s" ); - status.on_new_peer_in_routing_table(); // Connected to a new peer though! + status.trigger(); // Connected to a new peer though! assert!( status.next().now_or_never().is_some(), "bootstrap to be triggered immediately because we connected to a new peer" @@ -222,7 +227,7 @@ mod tests { "bootstrap to not be triggered immediately because periodic bootstrap is in ~1s" ); - status.on_new_peer_in_routing_table(); // Connected to a new peer though! + status.trigger(); // Connected to a new peer though! assert!( status.next().now_or_never().is_none(), "bootstrap to not be triggered immediately because throttle is 5ms" @@ -243,7 +248,7 @@ mod tests { // User manually triggered a bootstrap do_bootstrap(&mut status); - status.on_new_peer_in_routing_table(); // Connected to a new peer though! + status.trigger(); // Connected to a new peer though! assert!( status.next().now_or_never().is_some(), @@ -256,7 +261,7 @@ mod tests { ) { let mut status = Status::new(Some(MS_100), Some(MS_5)); - status.on_new_peer_in_routing_table(); + status.trigger(); let start = Instant::now(); await_and_do_bootstrap(&mut status).await; @@ -276,7 +281,7 @@ mod tests { ) { let mut status = Status::new(None, Some(Duration::ZERO)); - status.on_new_peer_in_routing_table(); + status.trigger(); status.next().await; } @@ -300,10 +305,10 @@ mod tests { ) { let mut status = Status::new(None, Some(MS_100)); - status.on_new_peer_in_routing_table(); + status.trigger(); for _ in 0..10 { Delay::new(MS_100 / 2).await; - status.on_new_peer_in_routing_table(); // should reset throttle_timer + status.trigger(); // should reset throttle_timer } assert!( status.next().now_or_never().is_none(), diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index 5e7c2e21b8b..17c483da709 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -501,6 +501,8 @@ impl Handler { // is a `Void`. let protocol = match protocol { future::Either::Left(p) => p, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] future::Either::Right(p) => void::unreachable(p), }; diff --git a/protocols/kad/src/jobs.rs b/protocols/kad/src/jobs.rs index f1631ed6ad1..537f652b7a4 100644 --- a/protocols/kad/src/jobs.rs +++ b/protocols/kad/src/jobs.rs @@ -64,13 +64,13 @@ use crate::record::{self, store::RecordStore, ProviderRecord, Record}; use futures::prelude::*; use futures_timer::Delay; -use instant::Instant; use libp2p_identity::PeerId; use std::collections::HashSet; use std::pin::Pin; use std::task::{Context, Poll}; use std::time::Duration; use std::vec; +use web_time::Instant; /// The maximum number of queries towards which background jobs /// are allowed to start new queries on an invocation of diff --git a/protocols/kad/src/kbucket.rs b/protocols/kad/src/kbucket.rs index a0d272c31f1..28d7df03917 100644 --- a/protocols/kad/src/kbucket.rs +++ b/protocols/kad/src/kbucket.rs @@ -75,14 +75,49 @@ mod key; pub use bucket::NodeStatus; pub use entry::*; -use arrayvec::{self, ArrayVec}; use bucket::KBucket; use std::collections::VecDeque; -use std::time::{Duration, Instant}; +use std::num::NonZeroUsize; +use std::time::Duration; +use web_time::Instant; /// Maximum number of k-buckets. const NUM_BUCKETS: usize = 256; +/// The configuration for `KBucketsTable`. +#[derive(Debug, Clone, Copy)] +pub(crate) struct KBucketConfig { + /// Maximal number of nodes that a bucket can contain. + bucket_size: usize, + /// Specifies the duration after creation of a [`PendingEntry`] after which + /// it becomes eligible for insertion into a full bucket, replacing the + /// least-recently (dis)connected node. + pending_timeout: Duration, +} + +impl Default for KBucketConfig { + fn default() -> Self { + KBucketConfig { + bucket_size: K_VALUE.get(), + pending_timeout: Duration::from_secs(60), + } + } +} + +impl KBucketConfig { + /// Modifies the maximal number of nodes that a bucket can contain. + pub(crate) fn set_bucket_size(&mut self, bucket_size: NonZeroUsize) { + self.bucket_size = bucket_size.get(); + } + + /// Modifies the duration after creation of a [`PendingEntry`] after which + /// it becomes eligible for insertion into a full bucket, replacing the + /// least-recently (dis)connected node. + pub(crate) fn set_pending_timeout(&mut self, pending_timeout: Duration) { + self.pending_timeout = pending_timeout; + } +} + /// A `KBucketsTable` represents a Kademlia routing table. #[derive(Debug, Clone)] pub(crate) struct KBucketsTable { @@ -90,6 +125,8 @@ pub(crate) struct KBucketsTable { local_key: TKey, /// The buckets comprising the routing table. buckets: Vec>, + /// The maximal number of nodes that a bucket can contain. + bucket_size: usize, /// The list of evicted entries that have been replaced with pending /// entries since the last call to [`KBucketsTable::take_applied_pending`]. applied_pending: VecDeque>, @@ -150,17 +187,12 @@ where TVal: Clone, { /// Creates a new, empty Kademlia routing table with entries partitioned - /// into buckets as per the Kademlia protocol. - /// - /// The given `pending_timeout` specifies the duration after creation of - /// a [`PendingEntry`] after which it becomes eligible for insertion into - /// a full bucket, replacing the least-recently (dis)connected node. - pub(crate) fn new(local_key: TKey, pending_timeout: Duration) -> Self { + /// into buckets as per the Kademlia protocol using the provided config. + pub(crate) fn new(local_key: TKey, config: KBucketConfig) -> Self { KBucketsTable { local_key, - buckets: (0..NUM_BUCKETS) - .map(|_| KBucket::new(pending_timeout)) - .collect(), + buckets: (0..NUM_BUCKETS).map(|_| KBucket::new(config)).collect(), + bucket_size: config.bucket_size, applied_pending: VecDeque::new(), } } @@ -246,13 +278,16 @@ where T: AsRef, { let distance = self.local_key.as_ref().distance(target); + let bucket_size = self.bucket_size; ClosestIter { target, iter: None, table: self, buckets_iter: ClosestBucketsIter::new(distance), - fmap: |b: &KBucket| -> ArrayVec<_, { K_VALUE.get() }> { - b.iter().map(|(n, _)| n.key.clone()).collect() + fmap: move |b: &KBucket| -> Vec<_> { + let mut vec = Vec::with_capacity(bucket_size); + vec.extend(b.iter().map(|(n, _)| n.key.clone())); + vec }, } } @@ -268,13 +303,15 @@ where TVal: Clone, { let distance = self.local_key.as_ref().distance(target); + let bucket_size = self.bucket_size; ClosestIter { target, iter: None, table: self, buckets_iter: ClosestBucketsIter::new(distance), - fmap: |b: &KBucket<_, TVal>| -> ArrayVec<_, { K_VALUE.get() }> { + fmap: move |b: &KBucket<_, TVal>| -> Vec<_> { b.iter() + .take(bucket_size) .map(|(n, status)| EntryView { node: n.clone(), status, @@ -323,7 +360,7 @@ struct ClosestIter<'a, TTarget, TKey, TVal, TMap, TOut> { /// distance of the local key to the target. buckets_iter: ClosestBucketsIter, /// The iterator over the entries in the currently traversed bucket. - iter: Option>, + iter: Option>, /// The projection function / mapping applied on each bucket as /// it is encountered, producing the next `iter`ator. fmap: TMap, @@ -344,7 +381,7 @@ enum ClosestBucketsIterState { /// The starting state of the iterator yields the first bucket index and /// then transitions to `ZoomIn`. Start(BucketIndex), - /// The iterator "zooms in" to to yield the next bucket cotaining nodes that + /// The iterator "zooms in" to yield the next bucket containing nodes that /// are incrementally closer to the local node but further from the `target`. /// These buckets are identified by a `1` in the corresponding bit position /// of the distance bit string. When bucket `0` is reached, the iterator @@ -428,7 +465,7 @@ where TTarget: AsRef, TKey: Clone + AsRef, TVal: Clone, - TMap: Fn(&KBucket) -> ArrayVec, + TMap: Fn(&KBucket) -> Vec, TOut: AsRef, { type Item = TOut; @@ -534,19 +571,19 @@ mod tests { fn arbitrary(g: &mut Gen) -> TestTable { let local_key = Key::from(PeerId::random()); let timeout = Duration::from_secs(g.gen_range(1..360)); - let mut table = TestTable::new(local_key.clone().into(), timeout); + let mut config = KBucketConfig::default(); + config.set_pending_timeout(timeout); + let bucket_size = config.bucket_size; + let mut table = TestTable::new(local_key.into(), config); let mut num_total = g.gen_range(0..100); for (i, b) in &mut table.buckets.iter_mut().enumerate().rev() { let ix = BucketIndex(i); - let num = g.gen_range(0..usize::min(K_VALUE.get(), num_total) + 1); + let num = g.gen_range(0..usize::min(bucket_size, num_total) + 1); num_total -= num; for _ in 0..num { let distance = ix.rand_distance(&mut rand::thread_rng()); let key = local_key.for_distance(distance); - let node = Node { - key: key.clone(), - value: (), - }; + let node = Node { key, value: () }; let status = NodeStatus::arbitrary(g); match b.insert(node, status) { InsertResult::Inserted => {} @@ -562,7 +599,9 @@ mod tests { fn buckets_are_non_overlapping_and_exhaustive() { let local_key = Key::from(PeerId::random()); let timeout = Duration::from_secs(0); - let mut table = KBucketsTable::::new(local_key.into(), timeout); + let mut config = KBucketConfig::default(); + config.set_pending_timeout(timeout); + let mut table = KBucketsTable::::new(local_key.into(), config); let mut prev_max = U256::from(0); @@ -579,7 +618,9 @@ mod tests { fn bucket_contains_range() { fn prop(ix: u8) { let index = BucketIndex(ix as usize); - let mut bucket = KBucket::, ()>::new(Duration::from_secs(0)); + let mut config = KBucketConfig::default(); + config.set_pending_timeout(Duration::from_secs(0)); + let mut bucket = KBucket::, ()>::new(config); let bucket_ref = KBucketRef { index, bucket: &mut bucket, @@ -597,7 +638,7 @@ mod tests { assert!(!bucket_ref.contains(&Distance(min.0 - 1))); } - if max != Distance(U256::max_value()) { + if max != Distance(U256::MAX) { // ^ avoid overflow assert!(!bucket_ref.contains(&Distance(max.0 + 1))); } @@ -625,7 +666,7 @@ mod tests { let local_key = Key::from(PeerId::random()); let other_id = Key::from(PeerId::random()); - let mut table = KBucketsTable::<_, ()>::new(local_key, Duration::from_secs(5)); + let mut table = KBucketsTable::<_, ()>::new(local_key, KBucketConfig::default()); if let Some(Entry::Absent(entry)) = table.entry(&other_id) { match entry.insert((), NodeStatus::Connected) { InsertResult::Inserted => (), @@ -643,7 +684,7 @@ mod tests { #[test] fn entry_self() { let local_key = Key::from(PeerId::random()); - let mut table = KBucketsTable::<_, ()>::new(local_key.clone(), Duration::from_secs(5)); + let mut table = KBucketsTable::<_, ()>::new(local_key, KBucketConfig::default()); assert!(table.entry(&local_key).is_none()) } @@ -651,7 +692,7 @@ mod tests { #[test] fn closest() { let local_key = Key::from(PeerId::random()); - let mut table = KBucketsTable::<_, ()>::new(local_key, Duration::from_secs(5)); + let mut table = KBucketsTable::<_, ()>::new(local_key, KBucketConfig::default()); let mut count = 0; loop { if count == 100 { @@ -671,7 +712,7 @@ mod tests { let mut expected_keys: Vec<_> = table .buckets .iter() - .flat_map(|t| t.iter().map(|(n, _)| n.key.clone())) + .flat_map(|t| t.iter().map(|(n, _)| n.key)) .collect(); for _ in 0..10 { @@ -686,7 +727,9 @@ mod tests { #[test] fn applied_pending() { let local_key = Key::from(PeerId::random()); - let mut table = KBucketsTable::<_, ()>::new(local_key.clone(), Duration::from_millis(1)); + let mut config = KBucketConfig::default(); + config.set_pending_timeout(Duration::from_millis(1)); + let mut table = KBucketsTable::<_, ()>::new(local_key, config); let expected_applied; let full_bucket_index; loop { @@ -698,10 +741,7 @@ mod tests { match e.insert((), NodeStatus::Connected) { InsertResult::Pending { disconnected } => { expected_applied = AppliedPending { - inserted: Node { - key: key.clone(), - value: (), - }, + inserted: Node { key, value: () }, evicted: Some(Node { key: disconnected, value: (), diff --git a/protocols/kad/src/kbucket/bucket.rs b/protocols/kad/src/kbucket/bucket.rs index d70161919e1..1426017aa7a 100644 --- a/protocols/kad/src/kbucket/bucket.rs +++ b/protocols/kad/src/kbucket/bucket.rs @@ -88,15 +88,17 @@ pub struct Node { } /// The position of a node in a `KBucket`, i.e. a non-negative integer -/// in the range `[0, K_VALUE)`. +/// in the range `[0, bucket_size)`. #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub(crate) struct Position(usize); -/// A `KBucket` is a list of up to `K_VALUE` keys and associated values, +/// A `KBucket` is a list of up to `capacity` keys and associated values, /// ordered from least-recently connected to most-recently connected. #[derive(Debug, Clone)] pub(crate) struct KBucket { /// The nodes contained in the bucket. - nodes: ArrayVec, { K_VALUE.get() }>, + nodes: Vec>, + /// The maximal number of nodes that a bucket can contain. + capacity: usize, /// The position (index) in `nodes` that marks the first connected node. /// @@ -104,7 +106,7 @@ pub(crate) struct KBucket { /// most-recently connected, all entries above this index are also considered /// connected, i.e. the range `[0, first_connected_pos)` marks the sub-list of entries /// that are considered disconnected and the range - /// `[first_connected_pos, K_VALUE)` marks sub-list of entries that are + /// `[first_connected_pos, capacity)` marks sub-list of entries that are /// considered connected. /// /// `None` indicates that there are no connected entries in the bucket, i.e. @@ -156,18 +158,31 @@ pub(crate) struct AppliedPending { pub(crate) evicted: Option>, } +impl Default for KBucket { + fn default() -> Self { + KBucket { + nodes: Vec::with_capacity(K_VALUE.get()), + capacity: K_VALUE.get(), + first_connected_pos: None, + pending: None, + pending_timeout: Duration::from_secs(60), + } + } +} + impl KBucket where TKey: Clone + AsRef, TVal: Clone, { - /// Creates a new `KBucket` with the given timeout for pending entries. - pub(crate) fn new(pending_timeout: Duration) -> Self { + /// Creates a new `KBucket` with the given configuration. + pub(crate) fn new(config: KBucketConfig) -> Self { KBucket { - nodes: ArrayVec::new(), + nodes: Vec::with_capacity(config.bucket_size), + capacity: config.bucket_size, first_connected_pos: None, pending: None, - pending_timeout, + pending_timeout: config.pending_timeout, } } @@ -205,7 +220,7 @@ where pub(crate) fn apply_pending(&mut self) -> Option> { if let Some(pending) = self.pending.take() { if pending.replace <= Instant::now() { - if self.nodes.is_full() { + if self.nodes.len() >= self.capacity { if self.status(Position(0)) == NodeStatus::Connected { // The bucket is full with connected nodes. Drop the pending node. return None; @@ -316,7 +331,7 @@ where ) -> InsertResult { match status { NodeStatus::Connected => { - if self.nodes.is_full() { + if self.nodes.len() >= self.capacity { if self.first_connected_pos == Some(0) || self.pending.is_some() { return InsertResult::Full; } else { @@ -336,7 +351,7 @@ where InsertResult::Inserted } NodeStatus::Disconnected => { - if self.nodes.is_full() { + if self.nodes.len() >= self.capacity { return InsertResult::Full; } if let Some(ref mut p) = self.first_connected_pos { @@ -431,19 +446,17 @@ mod tests { use super::*; use libp2p_identity::PeerId; use quickcheck::*; - use std::collections::VecDeque; impl Arbitrary for KBucket, ()> { fn arbitrary(g: &mut Gen) -> KBucket, ()> { let timeout = Duration::from_secs(g.gen_range(1..g.size()) as u64); - let mut bucket = KBucket::, ()>::new(timeout); - let num_nodes = g.gen_range(1..K_VALUE.get() + 1); + let mut config = KBucketConfig::default(); + config.set_pending_timeout(timeout); + let mut bucket = KBucket::, ()>::new(config); + let num_nodes = g.gen_range(1..bucket.capacity + 1); for _ in 0..num_nodes { let key = Key::from(PeerId::random()); - let node = Node { - key: key.clone(), - value: (), - }; + let node = Node { key, value: () }; let status = NodeStatus::arbitrary(g); match bucket.insert(node, status) { InsertResult::Inserted => {} @@ -473,7 +486,7 @@ mod tests { // Fill a bucket with random nodes with the given status. fn fill_bucket(bucket: &mut KBucket, ()>, status: NodeStatus) { let num_entries_start = bucket.num_entries(); - for i in 0..K_VALUE.get() - num_entries_start { + for i in 0..bucket.capacity - num_entries_start { let key = Key::from(PeerId::random()); let node = Node { key, value: () }; assert_eq!(InsertResult::Inserted, bucket.insert(node, status)); @@ -484,7 +497,7 @@ mod tests { #[test] fn ordering() { fn prop(status: Vec) -> bool { - let mut bucket = KBucket::, ()>::new(Duration::from_secs(1)); + let mut bucket = KBucket::, ()>::default(); // The expected lists of connected and disconnected nodes. let mut connected = VecDeque::new(); @@ -493,11 +506,8 @@ mod tests { // Fill the bucket, thereby populating the expected lists in insertion order. for status in status { let key = Key::from(PeerId::random()); - let node = Node { - key: key.clone(), - value: (), - }; - let full = bucket.num_entries() == K_VALUE.get(); + let node = Node { key, value: () }; + let full = bucket.num_entries() == bucket.capacity; if let InsertResult::Inserted = bucket.insert(node, status) { let vec = match status { NodeStatus::Connected => &mut connected, @@ -506,15 +516,12 @@ mod tests { if full { vec.pop_front(); } - vec.push_back((status, key.clone())); + vec.push_back((status, key)); } } // Get all nodes from the bucket, together with their status. - let mut nodes = bucket - .iter() - .map(|(n, s)| (s, n.key.clone())) - .collect::>(); + let mut nodes = bucket.iter().map(|(n, s)| (s, n.key)).collect::>(); // Split the list of nodes at the first connected node. let first_connected_pos = nodes.iter().position(|(s, _)| *s == NodeStatus::Connected); @@ -532,7 +539,7 @@ mod tests { #[test] fn full_bucket() { - let mut bucket = KBucket::, ()>::new(Duration::from_secs(1)); + let mut bucket = KBucket::, ()>::default(); // Fill the bucket with disconnected nodes. fill_bucket(&mut bucket, NodeStatus::Disconnected); @@ -546,7 +553,7 @@ mod tests { } // One-by-one fill the bucket with connected nodes, replacing the disconnected ones. - for i in 0..K_VALUE.get() { + for i in 0..bucket.capacity { let (first, first_status) = bucket.iter().next().unwrap(); let first_disconnected = first.clone(); assert_eq!(first_status, NodeStatus::Disconnected); @@ -554,10 +561,7 @@ mod tests { // Add a connected node, which is expected to be pending, scheduled to // replace the first (i.e. least-recently connected) node. let key = Key::from(PeerId::random()); - let node = Node { - key: key.clone(), - value: (), - }; + let node = Node { key, value: () }; match bucket.insert(node.clone(), NodeStatus::Connected) { InsertResult::Pending { disconnected } => { assert_eq!(disconnected, first_disconnected.key) @@ -586,11 +590,11 @@ mod tests { ); assert_eq!(Some((&node, NodeStatus::Connected)), bucket.iter().last()); assert!(bucket.pending().is_none()); - assert_eq!(Some(K_VALUE.get() - (i + 1)), bucket.first_connected_pos); + assert_eq!(Some(bucket.capacity - (i + 1)), bucket.first_connected_pos); } assert!(bucket.pending().is_none()); - assert_eq!(K_VALUE.get(), bucket.num_entries()); + assert_eq!(bucket.capacity, bucket.num_entries()); // Trying to insert another connected node fails. let key = Key::from(PeerId::random()); @@ -603,17 +607,14 @@ mod tests { #[test] fn full_bucket_discard_pending() { - let mut bucket = KBucket::, ()>::new(Duration::from_secs(1)); + let mut bucket = KBucket::, ()>::default(); fill_bucket(&mut bucket, NodeStatus::Disconnected); let (first, _) = bucket.iter().next().unwrap(); let first_disconnected = first.clone(); // Add a connected pending node. let key = Key::from(PeerId::random()); - let node = Node { - key: key.clone(), - value: (), - }; + let node = Node { key, value: () }; if let InsertResult::Pending { disconnected } = bucket.insert(node, NodeStatus::Connected) { assert_eq!(&disconnected, &first_disconnected.key); } else { @@ -638,7 +639,7 @@ mod tests { bucket.first_connected_pos ); assert_eq!(1, bucket.num_connected()); - assert_eq!(K_VALUE.get() - 1, bucket.num_disconnected()); + assert_eq!(bucket.capacity - 1, bucket.num_disconnected()); } #[test] @@ -648,13 +649,10 @@ mod tests { // Capture position and key of the random node to update. let pos = pos.0 % num_nodes; - let key = bucket.nodes[pos].key.clone(); + let key = bucket.nodes[pos].key; // Record the (ordered) list of status of all nodes in the bucket. - let mut expected = bucket - .iter() - .map(|(n, s)| (n.key.clone(), s)) - .collect::>(); + let mut expected = bucket.iter().map(|(n, s)| (n.key, s)).collect::>(); // Update the node in the bucket. bucket.update(&key, status); @@ -666,14 +664,28 @@ mod tests { NodeStatus::Disconnected => bucket.first_connected_pos.unwrap_or(num_nodes) - 1, }; expected.remove(pos); - expected.insert(expected_pos, (key.clone(), status)); - let actual = bucket - .iter() - .map(|(n, s)| (n.key.clone(), s)) - .collect::>(); + expected.insert(expected_pos, (key, status)); + let actual = bucket.iter().map(|(n, s)| (n.key, s)).collect::>(); expected == actual } quickcheck(prop as fn(_, _, _) -> _); } + + #[test] + fn test_custom_bucket_size() { + let bucket_sizes: [NonZeroUsize; 4] = [ + NonZeroUsize::new(2).unwrap(), + NonZeroUsize::new(20).unwrap(), + NonZeroUsize::new(200).unwrap(), + NonZeroUsize::new(2000).unwrap(), + ]; + for &size in &bucket_sizes { + let mut config = KBucketConfig::default(); + config.set_bucket_size(size); + let mut bucket = KBucket::, ()>::new(config); + fill_bucket(&mut bucket, NodeStatus::Disconnected); + assert_eq!(size.get(), bucket.num_entries()); + } + } } diff --git a/protocols/kad/src/kbucket/entry.rs b/protocols/kad/src/kbucket/entry.rs index 02c90fdfcc7..808db08d858 100644 --- a/protocols/kad/src/kbucket/entry.rs +++ b/protocols/kad/src/kbucket/entry.rs @@ -18,10 +18,10 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -//! The `Entry` API for quering and modifying the entries of a `KBucketsTable` +//! The `Entry` API for querying and modifying the entries of a `KBucketsTable` //! representing the nodes participating in the Kademlia DHT. -pub(crate) use super::bucket::{AppliedPending, InsertResult, Node, NodeStatus, K_VALUE}; +pub(crate) use super::bucket::{AppliedPending, InsertResult, Node, K_VALUE}; pub use super::key::*; use super::*; diff --git a/protocols/kad/src/kbucket/key.rs b/protocols/kad/src/kbucket/key.rs index bc5d6a53750..f35849c6b26 100644 --- a/protocols/kad/src/kbucket/key.rs +++ b/protocols/kad/src/kbucket/key.rs @@ -39,7 +39,7 @@ construct_uint! { /// /// `Key`s have an XOR metric as defined in the Kademlia paper, i.e. the bitwise XOR of /// the hash digests, interpreted as an integer. See [`Key::distance`]. -#[derive(Clone, Debug)] +#[derive(Clone, Copy, Debug)] pub struct Key { preimage: T, bytes: KeyBytes, @@ -145,7 +145,7 @@ impl Hash for Key { } /// The raw bytes of a key in the DHT keyspace. -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(PartialEq, Eq, Clone, Copy, Debug)] pub struct KeyBytes(GenericArray); impl KeyBytes { diff --git a/protocols/kad/src/lib.rs b/protocols/kad/src/lib.rs index bc01b9fd3ce..681d135f79b 100644 --- a/protocols/kad/src/lib.rs +++ b/protocols/kad/src/lib.rs @@ -23,15 +23,15 @@ //! # Important Discrepancies //! //! - **Peer Discovery with Identify** In other libp2p implementations, the -//! [Identify](https://github.com/libp2p/specs/tree/master/identify) protocol might be seen as a core protocol. Rust-libp2p -//! tries to stay as generic as possible, and does not make this assumption. -//! This means that the Identify protocol must be manually hooked up to Kademlia through calls -//! to [`Behaviour::add_address`]. -//! If you choose not to use the Identify protocol, and do not provide an alternative peer -//! discovery mechanism, a Kademlia node will not discover nodes beyond the network's -//! [boot nodes](https://docs.libp2p.io/concepts/glossary/#boot-node). Without the Identify protocol, -//! existing nodes in the kademlia network cannot obtain the listen addresses -//! of nodes querying them, and thus will not be able to add them to their routing table. +//! [Identify](https://github.com/libp2p/specs/tree/master/identify) protocol might be seen as a core protocol. Rust-libp2p +//! tries to stay as generic as possible, and does not make this assumption. +//! This means that the Identify protocol must be manually hooked up to Kademlia through calls +//! to [`Behaviour::add_address`]. +//! If you choose not to use the Identify protocol, and do not provide an alternative peer +//! discovery mechanism, a Kademlia node will not discover nodes beyond the network's +//! [boot nodes](https://docs.libp2p.io/concepts/glossary/#boot-node). Without the Identify protocol, +//! existing nodes in the kademlia network cannot obtain the listen addresses +//! of nodes querying them, and thus will not be able to add them to their routing table. #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] @@ -59,9 +59,9 @@ pub use behaviour::{ AddProviderContext, AddProviderError, AddProviderOk, AddProviderPhase, AddProviderResult, BootstrapError, BootstrapOk, BootstrapResult, GetClosestPeersError, GetClosestPeersOk, GetClosestPeersResult, GetProvidersError, GetProvidersOk, GetProvidersResult, GetRecordError, - GetRecordOk, GetRecordResult, InboundRequest, Mode, NoKnownPeers, PeerRecord, PutRecordContext, - PutRecordError, PutRecordOk, PutRecordPhase, PutRecordResult, QueryInfo, QueryMut, QueryRef, - QueryResult, QueryStats, RoutingUpdate, + GetRecordOk, GetRecordResult, InboundRequest, Mode, NoKnownPeers, PeerInfo, PeerRecord, + PutRecordContext, PutRecordError, PutRecordOk, PutRecordPhase, PutRecordResult, QueryInfo, + QueryMut, QueryRef, QueryResult, QueryStats, RoutingUpdate, }; pub use behaviour::{ Behaviour, BucketInserts, Caching, Config, Event, ProgressStep, Quorum, StoreInserts, diff --git a/protocols/kad/src/protocol.rs b/protocols/kad/src/protocol.rs index 5abe2089852..9d2ef56f5d8 100644 --- a/protocols/kad/src/protocol.rs +++ b/protocols/kad/src/protocol.rs @@ -31,15 +31,15 @@ use crate::record::{self, Record}; use asynchronous_codec::{Decoder, Encoder, Framed}; use bytes::BytesMut; use futures::prelude::*; -use instant::Instant; use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; use libp2p_core::Multiaddr; use libp2p_identity::PeerId; use libp2p_swarm::StreamProtocol; use std::marker::PhantomData; -use std::{convert::TryFrom, time::Duration}; +use std::time::Duration; use std::{io, iter}; use tracing::debug; +use web_time::Instant; /// The protocol name used for negotiating with multistream-select. pub(crate) const DEFAULT_PROTO_NAME: StreamProtocol = StreamProtocol::new("/ipfs/kad/1.0.0"); diff --git a/protocols/kad/src/query.rs b/protocols/kad/src/query.rs index bb240d5864a..1a895d9627c 100644 --- a/protocols/kad/src/query.rs +++ b/protocols/kad/src/query.rs @@ -20,45 +20,49 @@ mod peers; +use libp2p_core::Multiaddr; use peers::closest::{ disjoint::ClosestDisjointPeersIter, ClosestPeersIter, ClosestPeersIterConfig, }; use peers::fixed::FixedPeersIter; use peers::PeersIterState; +use smallvec::SmallVec; +use crate::behaviour::PeerInfo; +use crate::handler::HandlerIn; use crate::kbucket::{Key, KeyBytes}; -use crate::{ALPHA_VALUE, K_VALUE}; +use crate::{QueryInfo, ALPHA_VALUE, K_VALUE}; use either::Either; use fnv::FnvHashMap; -use instant::Instant; use libp2p_identity::PeerId; use std::{num::NonZeroUsize, time::Duration}; +use web_time::Instant; /// A `QueryPool` provides an aggregate state machine for driving `Query`s to completion. /// /// Internally, a `Query` is in turn driven by an underlying `QueryPeerIter` /// that determines the peer selection strategy, i.e. the order in which the /// peers involved in the query should be contacted. -pub(crate) struct QueryPool { +pub(crate) struct QueryPool { next_id: usize, config: QueryConfig, - queries: FnvHashMap>, + queries: FnvHashMap, } /// The observable states emitted by [`QueryPool::poll`]. -pub(crate) enum QueryPoolState<'a, TInner> { +pub(crate) enum QueryPoolState<'a> { /// The pool is idle, i.e. there are no queries to process. Idle, /// At least one query is waiting for results. `Some(request)` indicates /// that a new request is now being waited on. - Waiting(Option<(&'a mut Query, PeerId)>), + Waiting(Option<(&'a mut Query, PeerId)>), /// A query has finished. - Finished(Query), + Finished(Query), /// A query has timed out. - Timeout(Query), + Timeout(Query), } -impl QueryPool { +impl QueryPool { /// Creates a new `QueryPool` with the given configuration. pub(crate) fn new(config: QueryConfig) -> Self { QueryPool { @@ -74,7 +78,7 @@ impl QueryPool { } /// Returns an iterator over the queries in the pool. - pub(crate) fn iter(&self) -> impl Iterator> { + pub(crate) fn iter(&self) -> impl Iterator { self.queries.values() } @@ -84,42 +88,42 @@ impl QueryPool { } /// Returns an iterator that allows modifying each query in the pool. - pub(crate) fn iter_mut(&mut self) -> impl Iterator> { + pub(crate) fn iter_mut(&mut self) -> impl Iterator { self.queries.values_mut() } /// Adds a query to the pool that contacts a fixed set of peers. - pub(crate) fn add_fixed(&mut self, peers: I, inner: TInner) -> QueryId + pub(crate) fn add_fixed(&mut self, peers: I, info: QueryInfo) -> QueryId where I: IntoIterator, { let id = self.next_query_id(); - self.continue_fixed(id, peers, inner); + self.continue_fixed(id, peers, info); id } /// Continues an earlier query with a fixed set of peers, reusing /// the given query ID, which must be from a query that finished /// earlier. - pub(crate) fn continue_fixed(&mut self, id: QueryId, peers: I, inner: TInner) + pub(crate) fn continue_fixed(&mut self, id: QueryId, peers: I, info: QueryInfo) where I: IntoIterator, { assert!(!self.queries.contains_key(&id)); let parallelism = self.config.replication_factor; let peer_iter = QueryPeerIter::Fixed(FixedPeersIter::new(peers, parallelism)); - let query = Query::new(id, peer_iter, inner); + let query = Query::new(id, peer_iter, info); self.queries.insert(id, query); } /// Adds a query to the pool that iterates towards the closest peers to the target. - pub(crate) fn add_iter_closest(&mut self, target: T, peers: I, inner: TInner) -> QueryId + pub(crate) fn add_iter_closest(&mut self, target: T, peers: I, info: QueryInfo) -> QueryId where T: Into + Clone, I: IntoIterator>, { let id = self.next_query_id(); - self.continue_iter_closest(id, target, peers, inner); + self.continue_iter_closest(id, target, peers, info); id } @@ -129,13 +133,21 @@ impl QueryPool { id: QueryId, target: T, peers: I, - inner: TInner, + info: QueryInfo, ) where T: Into + Clone, I: IntoIterator>, { + let num_results = match info { + QueryInfo::GetClosestPeers { + num_results: Some(val), + .. + } => val, + _ => self.config.replication_factor, + }; + let cfg = ClosestPeersIterConfig { - num_results: self.config.replication_factor, + num_results, parallelism: self.config.parallelism, ..ClosestPeersIterConfig::default() }; @@ -148,7 +160,7 @@ impl QueryPool { QueryPeerIter::Closest(ClosestPeersIter::with_config(cfg, target, peers)) }; - let query = Query::new(id, peer_iter, inner); + let query = Query::new(id, peer_iter, info); self.queries.insert(id, query); } @@ -159,17 +171,17 @@ impl QueryPool { } /// Returns a reference to a query with the given ID, if it is in the pool. - pub(crate) fn get(&self, id: &QueryId) -> Option<&Query> { + pub(crate) fn get(&self, id: &QueryId) -> Option<&Query> { self.queries.get(id) } /// Returns a mutablereference to a query with the given ID, if it is in the pool. - pub(crate) fn get_mut(&mut self, id: &QueryId) -> Option<&mut Query> { + pub(crate) fn get_mut(&mut self, id: &QueryId) -> Option<&mut Query> { self.queries.get_mut(id) } /// Polls the pool to advance the queries. - pub(crate) fn poll(&mut self, now: Instant) -> QueryPoolState<'_, TInner> { + pub(crate) fn poll(&mut self, now: Instant) -> QueryPoolState<'_> { let mut finished = None; let mut timeout = None; let mut waiting = None; @@ -264,15 +276,53 @@ impl Default for QueryConfig { } /// A query in a `QueryPool`. -pub(crate) struct Query { +pub(crate) struct Query { /// The unique ID of the query. id: QueryId, /// The peer iterator that drives the query state. - peer_iter: QueryPeerIter, + pub(crate) peers: QueryPeers, /// Execution statistics of the query. - stats: QueryStats, - /// The opaque inner query state. - pub(crate) inner: TInner, + pub(crate) stats: QueryStats, + /// The query-specific state. + pub(crate) info: QueryInfo, + /// A map of pending requests to peers. + /// + /// A request is pending if the targeted peer is not currently connected + /// and these requests are sent as soon as a connection to the peer is established. + pub(crate) pending_rpcs: SmallVec<[(PeerId, HandlerIn); K_VALUE.get()]>, +} + +/// The peer iterator that drives the query state, +pub(crate) struct QueryPeers { + /// Addresses of peers discovered during a query. + pub(crate) addresses: FnvHashMap>, + /// The peer iterator that drives the query state. + peer_iter: QueryPeerIter, +} + +impl QueryPeers { + /// Consumes the peers iterator, producing a final `Iterator` over the discovered `PeerId`s. + pub(crate) fn into_peerids_iter(self) -> impl Iterator { + match self.peer_iter { + QueryPeerIter::Closest(iter) => Either::Left(Either::Left(iter.into_result())), + QueryPeerIter::ClosestDisjoint(iter) => Either::Left(Either::Right(iter.into_result())), + QueryPeerIter::Fixed(iter) => Either::Right(iter.into_result()), + } + } + + /// Consumes the peers iterator, producing a final `Iterator` over the discovered `PeerId`s + /// with their matching `Multiaddr`s. + pub(crate) fn into_peerinfos_iter(mut self) -> impl Iterator { + match self.peer_iter { + QueryPeerIter::Closest(iter) => Either::Left(Either::Left(iter.into_result())), + QueryPeerIter::ClosestDisjoint(iter) => Either::Left(Either::Right(iter.into_result())), + QueryPeerIter::Fixed(iter) => Either::Right(iter.into_result()), + } + .map(move |peer_id| { + let addrs = self.addresses.remove(&peer_id).unwrap_or_default().to_vec(); + PeerInfo { peer_id, addrs } + }) + } } /// The peer selection strategies that can be used by queries. @@ -282,13 +332,17 @@ enum QueryPeerIter { Fixed(FixedPeersIter), } -impl Query { +impl Query { /// Creates a new query without starting it. - fn new(id: QueryId, peer_iter: QueryPeerIter, inner: TInner) -> Self { + fn new(id: QueryId, peer_iter: QueryPeerIter, info: QueryInfo) -> Self { Query { id, - inner, - peer_iter, + info, + peers: QueryPeers { + addresses: Default::default(), + peer_iter, + }, + pending_rpcs: SmallVec::default(), stats: QueryStats::empty(), } } @@ -305,7 +359,7 @@ impl Query { /// Informs the query that the attempt to contact `peer` failed. pub(crate) fn on_failure(&mut self, peer: &PeerId) { - let updated = match &mut self.peer_iter { + let updated = match &mut self.peers.peer_iter { QueryPeerIter::Closest(iter) => iter.on_failure(peer), QueryPeerIter::ClosestDisjoint(iter) => iter.on_failure(peer), QueryPeerIter::Fixed(iter) => iter.on_failure(peer), @@ -322,7 +376,7 @@ impl Query { where I: IntoIterator, { - let updated = match &mut self.peer_iter { + let updated = match &mut self.peers.peer_iter { QueryPeerIter::Closest(iter) => iter.on_success(peer, new_peers), QueryPeerIter::ClosestDisjoint(iter) => iter.on_success(peer, new_peers), QueryPeerIter::Fixed(iter) => iter.on_success(peer), @@ -334,7 +388,7 @@ impl Query { /// Advances the state of the underlying peer iterator. fn next(&mut self, now: Instant) -> PeersIterState<'_> { - let state = match &mut self.peer_iter { + let state = match &mut self.peers.peer_iter { QueryPeerIter::Closest(iter) => iter.next(now), QueryPeerIter::ClosestDisjoint(iter) => iter.next(now), QueryPeerIter::Fixed(iter) => iter.next(), @@ -368,7 +422,7 @@ impl Query { where I: IntoIterator, { - match &mut self.peer_iter { + match &mut self.peers.peer_iter { QueryPeerIter::Closest(iter) => { iter.finish(); true @@ -386,7 +440,7 @@ impl Query { /// A finished query immediately stops yielding new peers to contact and will be /// reported by [`QueryPool::poll`] via [`QueryPoolState::Finished`]. pub(crate) fn finish(&mut self) { - match &mut self.peer_iter { + match &mut self.peers.peer_iter { QueryPeerIter::Closest(iter) => iter.finish(), QueryPeerIter::ClosestDisjoint(iter) => iter.finish(), QueryPeerIter::Fixed(iter) => iter.finish(), @@ -398,36 +452,12 @@ impl Query { /// A finished query is eventually reported by `QueryPool::next()` and /// removed from the pool. pub(crate) fn is_finished(&self) -> bool { - match &self.peer_iter { + match &self.peers.peer_iter { QueryPeerIter::Closest(iter) => iter.is_finished(), QueryPeerIter::ClosestDisjoint(iter) => iter.is_finished(), QueryPeerIter::Fixed(iter) => iter.is_finished(), } } - - /// Consumes the query, producing the final `QueryResult`. - pub(crate) fn into_result(self) -> QueryResult> { - let peers = match self.peer_iter { - QueryPeerIter::Closest(iter) => Either::Left(Either::Left(iter.into_result())), - QueryPeerIter::ClosestDisjoint(iter) => Either::Left(Either::Right(iter.into_result())), - QueryPeerIter::Fixed(iter) => Either::Right(iter.into_result()), - }; - QueryResult { - peers, - inner: self.inner, - stats: self.stats, - } - } -} - -/// The result of a `Query`. -pub(crate) struct QueryResult { - /// The opaque inner query state. - pub(crate) inner: TInner, - /// The successfully contacted peers. - pub(crate) peers: TPeers, - /// The collected query statistics. - pub(crate) stats: QueryStats, } /// Execution statistics of a query. diff --git a/protocols/kad/src/query/peers/closest.rs b/protocols/kad/src/query/peers/closest.rs index dc913f1bbca..2505ee2e9b2 100644 --- a/protocols/kad/src/query/peers/closest.rs +++ b/protocols/kad/src/query/peers/closest.rs @@ -22,10 +22,9 @@ use super::*; use crate::kbucket::{Distance, Key, KeyBytes}; use crate::{ALPHA_VALUE, K_VALUE}; -use instant::Instant; -use libp2p_identity::PeerId; use std::collections::btree_map::{BTreeMap, Entry}; -use std::{iter::FromIterator, num::NonZeroUsize, time::Duration}; +use std::{num::NonZeroUsize, time::Duration}; +use web_time::Instant; pub(crate) mod disjoint; /// A peer iterator for a dynamically changing list of peers, sorted by increasing @@ -175,6 +174,20 @@ impl ClosestPeersIter { }, } + let mut cur_range = distance; + let num_results = self.config.num_results.get(); + // furthest_peer is the furthest peer in range among the closest_peers + let furthest_peer = self + .closest_peers + .iter() + .enumerate() + .nth(num_results - 1) + .map(|(_, peer)| peer) + .or_else(|| self.closest_peers.iter().last()); + if let Some((dist, _)) = furthest_peer { + cur_range = *dist; + } + // Incorporate the reported closer peers into the iterator. // // The iterator makes progress if: @@ -190,9 +203,16 @@ impl ClosestPeersIter { key, state: PeerState::NotContacted, }; - self.closest_peers.entry(distance).or_insert(peer); - progress = self.closest_peers.keys().next() == Some(&distance) || progress; + let is_first_insert = match self.closest_peers.entry(distance) { + Entry::Occupied(_) => false, + Entry::Vacant(entry) => { + entry.insert(peer); + true + } + }; + + progress = (is_first_insert && distance < cur_range) || progress; } // Update the iterator state. @@ -477,10 +497,9 @@ mod tests { use super::*; use crate::SHA_256_MH; use libp2p_core::multihash::Multihash; - use libp2p_identity::PeerId; use quickcheck::*; use rand::{rngs::StdRng, Rng, SeedableRng}; - use std::{iter, time::Duration}; + use std::iter; fn random_peers(n: usize, g: &mut R) -> Vec { (0..n) @@ -537,12 +556,12 @@ mod tests { #[test] fn new_iter() { fn prop(iter: ClosestPeersIter) { - let target = iter.target.clone(); + let target = iter.target; let (keys, states): (Vec<_>, Vec<_>) = iter .closest_peers .values() - .map(|e| (e.key.clone(), &e.state)) + .map(|e| (e.key, &e.state)) .unzip(); let none_contacted = states.iter().all(|s| matches!(s, PeerState::NotContacted)); @@ -576,12 +595,12 @@ mod tests { let mut expected = iter .closest_peers .values() - .map(|e| e.key.clone()) + .map(|e| e.key) .collect::>(); let num_known = expected.len(); let max_parallelism = usize::min(iter.config.parallelism.get(), num_known); - let target = iter.target.clone(); + let target = iter.target; let mut remaining; let mut num_failures = 0; @@ -648,7 +667,7 @@ mod tests { .values() .all(|e| !matches!(e.state, PeerState::NotContacted | PeerState::Waiting { .. })); - let target = iter.target.clone(); + let target = iter.target; let num_results = iter.config.num_results; let result = iter.into_result(); let closest = result.map(Key::from).collect::>(); @@ -725,7 +744,6 @@ mod tests { .next() .unwrap() .key - .clone() .into_preimage(); // Poll the iterator for the first peer to be in progress. diff --git a/protocols/kad/src/query/peers/closest/disjoint.rs b/protocols/kad/src/query/peers/closest/disjoint.rs index 68721f93d7c..cafe87b6ef4 100644 --- a/protocols/kad/src/query/peers/closest/disjoint.rs +++ b/protocols/kad/src/query/peers/closest/disjoint.rs @@ -19,9 +19,6 @@ // DEALINGS IN THE SOFTWARE. use super::*; -use crate::kbucket::{Key, KeyBytes}; -use instant::Instant; -use libp2p_identity::PeerId; use std::{ collections::HashMap, iter::{Cycle, Map, Peekable}, @@ -438,7 +435,7 @@ impl>> Iterator for ResultIter { mod tests { use super::*; - use crate::{K_VALUE, SHA_256_MH}; + use crate::SHA_256_MH; use libp2p_core::multihash::Multihash; use quickcheck::*; use std::collections::HashSet; @@ -463,7 +460,7 @@ mod tests { peers.into_iter() }); - ResultIter::new(target.clone(), iters) + ResultIter::new(target, iters) } fn shrink(&self) -> Box> { @@ -484,7 +481,7 @@ mod tests { .collect(); Box::new(ResultIterShrinker { - target: self.target.clone(), + target: self.target, peers, iters, }) @@ -514,7 +511,7 @@ mod tests { Some(iter.into_iter()) }); - Some(ResultIter::new(self.target.clone(), iters)) + Some(ResultIter::new(self.target, iters)) } } @@ -602,20 +599,6 @@ mod tests { } } - #[derive(Debug, Clone)] - struct PeerVec(Vec>); - - impl Arbitrary for PeerVec { - fn arbitrary(g: &mut Gen) -> Self { - PeerVec( - (0..g.gen_range(1..60u8)) - .map(|_| ArbitraryPeerId::arbitrary(g).0) - .map(Key::from) - .collect(), - ) - } - } - #[test] fn s_kademlia_disjoint_paths() { let now = Instant::now(); @@ -884,7 +867,7 @@ mod tests { let closest = drive_to_finish( PeerIterator::Closest(ClosestPeersIter::with_config( cfg.clone(), - target.clone(), + target, known_closest_peers.clone(), )), graph.clone(), @@ -894,7 +877,7 @@ mod tests { let disjoint = drive_to_finish( PeerIterator::Disjoint(ClosestDisjointPeersIter::with_config( cfg, - target.clone(), + target, known_closest_peers.clone(), )), graph, diff --git a/protocols/kad/src/query/peers/fixed.rs b/protocols/kad/src/query/peers/fixed.rs index 50a969380a3..b34f7516801 100644 --- a/protocols/kad/src/query/peers/fixed.rs +++ b/protocols/kad/src/query/peers/fixed.rs @@ -21,7 +21,6 @@ use super::*; use fnv::FnvHashMap; -use libp2p_identity::PeerId; use std::{collections::hash_map::Entry, num::NonZeroUsize, vec}; /// A peer iterator for a fixed set of peers. diff --git a/protocols/kad/src/record.rs b/protocols/kad/src/record.rs index 4eb8e861c6f..cb7c4b866fc 100644 --- a/protocols/kad/src/record.rs +++ b/protocols/kad/src/record.rs @@ -23,13 +23,13 @@ pub mod store; use bytes::Bytes; -use instant::Instant; use libp2p_core::{multihash::Multihash, Multiaddr}; use libp2p_identity::PeerId; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use std::borrow::Borrow; use std::hash::{Hash, Hasher}; +use web_time::Instant; /// The (opaque) key of a record. #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] diff --git a/protocols/kad/src/record/store/memory.rs b/protocols/kad/src/record/store/memory.rs index edeae188ac6..3fb6d2be3e8 100644 --- a/protocols/kad/src/record/store/memory.rs +++ b/protocols/kad/src/record/store/memory.rs @@ -21,9 +21,7 @@ use super::*; use crate::kbucket; -use libp2p_identity::PeerId; use smallvec::SmallVec; -use std::borrow::Cow; use std::collections::{hash_map, hash_set, HashMap, HashSet}; use std::iter; @@ -154,38 +152,31 @@ impl RecordStore for MemoryStore { } .or_insert_with(Default::default); - if let Some(i) = providers.iter().position(|p| p.provider == record.provider) { - // In-place update of an existing provider record. - providers.as_mut()[i] = record; - } else { - // It is a new provider record for that key. - let local_key = self.local_key.clone(); - let key = kbucket::Key::new(record.key.clone()); - let provider = kbucket::Key::from(record.provider); - if let Some(i) = providers.iter().position(|p| { - let pk = kbucket::Key::from(p.provider); - provider.distance(&key) < pk.distance(&key) - }) { - // Insert the new provider. - if local_key.preimage() == &record.provider { + for p in providers.iter_mut() { + if p.provider == record.provider { + // In-place update of an existing provider record. + if self.local_key.preimage() == &record.provider { + self.provided.remove(p); self.provided.insert(record.clone()); } - providers.insert(i, record); - // Remove the excess provider, if any. - if providers.len() > self.config.max_providers_per_key { - if let Some(p) = providers.pop() { - self.provided.remove(&p); - } - } - } else if providers.len() < self.config.max_providers_per_key { - // The distance of the new provider to the key is larger than - // the distance of any existing provider, but there is still room. - if local_key.preimage() == &record.provider { - self.provided.insert(record.clone()); - } - providers.push(record); + *p = record; + return Ok(()); } } + + // If the providers list is full, we ignore the new provider. + // This strategy can mitigate Sybil attacks, in which an attacker + // floods the network with fake provider records. + if providers.len() == self.config.max_providers_per_key { + return Ok(()); + } + + // Otherwise, insert the new provider record. + if self.local_key.preimage() == &record.provider { + self.provided.insert(record.clone()); + } + providers.push(record); + Ok(()) } @@ -204,7 +195,9 @@ impl RecordStore for MemoryStore { let providers = e.get_mut(); if let Some(i) = providers.iter().position(|p| &p.provider == provider) { let p = providers.remove(i); - self.provided.remove(&p); + if &p.provider == self.local_key.preimage() { + self.provided.remove(&p); + } } if providers.is_empty() { e.remove(); @@ -217,18 +210,12 @@ impl RecordStore for MemoryStore { mod tests { use super::*; use crate::SHA_256_MH; - use libp2p_core::multihash::Multihash; use quickcheck::*; use rand::Rng; fn random_multihash() -> Multihash<64> { Multihash::wrap(SHA_256_MH, &rand::thread_rng().gen::<[u8; 32]>()).unwrap() } - - fn distance(r: &ProviderRecord) -> kbucket::Distance { - kbucket::Key::new(r.key.clone()).distance(&kbucket::Key::from(r.provider)) - } - #[test] fn put_get_remove_record() { fn prop(r: Record) { @@ -253,30 +240,6 @@ mod tests { quickcheck(prop as fn(_)) } - #[test] - fn providers_ordered_by_distance_to_key() { - fn prop(providers: Vec>) -> bool { - let mut store = MemoryStore::new(PeerId::random()); - let key = Key::from(random_multihash()); - - let mut records = providers - .into_iter() - .map(|p| ProviderRecord::new(key.clone(), p.into_preimage(), Vec::new())) - .collect::>(); - - for r in &records { - assert!(store.add_provider(r.clone()).is_ok()); - } - - records.sort_by_key(distance); - records.truncate(store.config.max_providers_per_key); - - records == store.providers(&key).to_vec() - } - - quickcheck(prop as fn(_) -> _) - } - #[test] fn provided() { let id = PeerId::random(); @@ -305,6 +268,46 @@ mod tests { assert_eq!(vec![rec.clone()], store.providers(&rec.key).to_vec()); } + #[test] + fn update_provided() { + let prv = PeerId::random(); + let mut store = MemoryStore::new(prv); + let key = random_multihash(); + let mut rec = ProviderRecord::new(key, prv, Vec::new()); + assert!(store.add_provider(rec.clone()).is_ok()); + assert_eq!( + vec![Cow::Borrowed(&rec)], + store.provided().collect::>() + ); + rec.expires = Some(Instant::now()); + assert!(store.add_provider(rec.clone()).is_ok()); + assert_eq!( + vec![Cow::Borrowed(&rec)], + store.provided().collect::>() + ); + } + + #[test] + fn max_providers_per_key() { + let config = MemoryStoreConfig::default(); + let key = kbucket::Key::new(Key::from(random_multihash())); + + let mut store = MemoryStore::with_config(PeerId::random(), config.clone()); + let peers = (0..config.max_providers_per_key) + .map(|_| PeerId::random()) + .collect::>(); + for peer in peers { + let rec = ProviderRecord::new(key.preimage().clone(), peer, Vec::new()); + assert!(store.add_provider(rec).is_ok()); + } + + // The new provider cannot be added because the key is already saturated. + let peer = PeerId::random(); + let rec = ProviderRecord::new(key.preimage().clone(), peer, Vec::new()); + assert!(store.add_provider(rec.clone()).is_ok()); + assert!(!store.providers(&rec.key).contains(&rec)); + } + #[test] fn max_provided_keys() { let mut store = MemoryStore::new(PeerId::random()); diff --git a/protocols/kad/tests/client_mode.rs b/protocols/kad/tests/client_mode.rs index 6aceeb27263..2c8d11beac7 100644 --- a/protocols/kad/tests/client_mode.rs +++ b/protocols/kad/tests/client_mode.rs @@ -23,14 +23,21 @@ async fn server_gets_added_to_routing_table_by_client() { let server_peer_id = *server.local_peer_id(); async_std::task::spawn(server.loop_on_next()); - let peer = client + let external_event_peer = client + .wait(|e| match e { + SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id), + _ => None, + }) + .await; + let routing_updated_peer = client .wait(|e| match e { SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer), _ => None, }) .await; - assert_eq!(peer, server_peer_id); + assert_eq!(external_event_peer, server_peer_id); + assert_eq!(routing_updated_peer, server_peer_id); } #[async_std::test] @@ -126,6 +133,12 @@ async fn set_client_to_server_mode() { let server_peer_id = *server.local_peer_id(); + let peer_id = client + .wait(|e| match e { + SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id), + _ => None, + }) + .await; let client_event = client.wait(|e| match e { SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer), _ => None, @@ -138,6 +151,7 @@ async fn set_client_to_server_mode() { let (peer, info) = futures::future::join(client_event, server_event).await; assert_eq!(peer, server_peer_id); + assert_eq!(peer_id, server_peer_id); assert!(info .protocols .iter() diff --git a/protocols/mdns/CHANGELOG.md b/protocols/mdns/CHANGELOG.md index cfd02232b07..67b1d669f60 100644 --- a/protocols/mdns/CHANGELOG.md +++ b/protocols/mdns/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.46.0 + + +## 0.45.2 + +- Add `#[track_caller]` on all `spawn` wrappers. + See [PR 5465](https://github.com/libp2p/rust-libp2p/pull/5465). + ## 0.45.1 - Ensure `Multiaddr` handled and returned by `Behaviour` are `/p2p` terminated. diff --git a/protocols/mdns/Cargo.toml b/protocols/mdns/Cargo.toml index 0d03a839a6f..19ae5ce9f36 100644 --- a/protocols/mdns/Cargo.toml +++ b/protocols/mdns/Cargo.toml @@ -2,7 +2,7 @@ name = "libp2p-mdns" edition = "2021" rust-version = { workspace = true } -version = "0.45.1" +version = "0.46.0" description = "Implementation of the libp2p mDNS discovery method" authors = ["Parity Technologies "] license = "MIT" @@ -12,19 +12,19 @@ categories = ["network-programming", "asynchronous"] [dependencies] async-std = { version = "1.12.0", optional = true } -async-io = { version = "2.3.2", optional = true } -data-encoding = "2.5.0" -futures = "0.3.30" +async-io = { version = "2.3.3", optional = true } +data-encoding = "2.6.0" +futures = { workspace = true } if-watch = "3.2.0" libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true } rand = "0.8.3" -smallvec = "1.13.1" -socket2 = { version = "0.5.6", features = ["all"] } -tokio = { version = "1.36", default-features = false, features = ["net", "time"], optional = true} -tracing = "0.1.37" -hickory-proto = { version = "0.24.0", default-features = false, features = ["mdns"] } +smallvec = "1.13.2" +socket2 = { version = "0.5.7", features = ["all"] } +tokio = { workspace = true, default-features = false, features = ["net", "time"], optional = true} +tracing = { workspace = true } +hickory-proto = { version = "0.24.1", default-features = false, features = ["mdns"] } void = "1.0.2" [features] @@ -37,9 +37,9 @@ libp2p-noise = { workspace = true } libp2p-swarm = { workspace = true, features = ["tokio", "async-std"] } libp2p-tcp = { workspace = true, features = ["tokio", "async-io"] } libp2p-yamux = { workspace = true } -tokio = { version = "1.36", default-features = false, features = ["macros", "rt", "rt-multi-thread", "time"] } +tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "time"] } libp2p-swarm-test = { path = "../../swarm-test" } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [[test]] name = "use-async-std" @@ -54,8 +54,6 @@ required-features = ["tokio"] # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 4e3533f26ab..6355fbf4943 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -28,6 +28,7 @@ use crate::Config; use futures::channel::mpsc; use futures::{Stream, StreamExt}; use if_watch::IfEvent; +use libp2p_core::transport::PortUse; use libp2p_core::{Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::behaviour::FromSwarm; @@ -55,6 +56,7 @@ pub trait Provider: 'static { /// Create a new instance of the `IfWatcher` type. fn new_watcher() -> Result; + #[track_caller] fn spawn(task: impl Future + Send + 'static) -> Self::TaskHandle; } @@ -262,6 +264,7 @@ where _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(dummy::ConnectionHandler) } diff --git a/protocols/mdns/src/behaviour/iface/dns.rs b/protocols/mdns/src/behaviour/iface/dns.rs index 6cc5550dbe5..39dbf08c731 100644 --- a/protocols/mdns/src/behaviour/iface/dns.rs +++ b/protocols/mdns/src/behaviour/iface/dns.rs @@ -247,7 +247,7 @@ fn duration_to_secs(duration: Duration) -> u32 { let secs = duration .as_secs() .saturating_add(u64::from(duration.subsec_nanos() > 0)); - cmp::min(secs, From::from(u32::max_value())) as u32 + cmp::min(secs, From::from(u32::MAX)) as u32 } /// Appends a big-endian u32 to `out`. @@ -397,7 +397,6 @@ mod tests { use super::*; use hickory_proto::op::Message; use libp2p_identity as identity; - use std::time::Duration; #[test] fn build_query_correct() { diff --git a/protocols/mdns/src/behaviour/iface/query.rs b/protocols/mdns/src/behaviour/iface/query.rs index eeb699fca6b..70b84816d0f 100644 --- a/protocols/mdns/src/behaviour/iface/query.rs +++ b/protocols/mdns/src/behaviour/iface/query.rs @@ -24,10 +24,9 @@ use hickory_proto::{ op::Message, rr::{Name, RData}, }; -use libp2p_core::{ - address_translation, - multiaddr::{Multiaddr, Protocol}, -}; +use libp2p_core::multiaddr::{Multiaddr, Protocol}; +use libp2p_swarm::_address_translation; + use libp2p_identity::PeerId; use std::time::Instant; use std::{fmt, net::SocketAddr, str, time::Duration}; @@ -179,7 +178,7 @@ impl MdnsResponse { let new_expiration = now + peer.ttl(); peer.addresses().iter().filter_map(move |address| { - let new_addr = address_translation(address, &observed)?; + let new_addr = _address_translation(address, &observed)?; let new_addr = new_addr.with_p2p(*peer.id()).ok()?; Some((*peer.id(), new_addr, new_expiration)) diff --git a/protocols/mdns/src/behaviour/socket.rs b/protocols/mdns/src/behaviour/socket.rs index fa9e0fbaf1e..ebaad17e45f 100644 --- a/protocols/mdns/src/behaviour/socket.rs +++ b/protocols/mdns/src/behaviour/socket.rs @@ -20,7 +20,6 @@ use std::{ io::Error, - marker::Unpin, net::{SocketAddr, UdpSocket}, task::{Context, Poll}, }; diff --git a/protocols/mdns/src/behaviour/timer.rs b/protocols/mdns/src/behaviour/timer.rs index 29622be9a38..5e284654676 100644 --- a/protocols/mdns/src/behaviour/timer.rs +++ b/protocols/mdns/src/behaviour/timer.rs @@ -18,10 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use std::{ - marker::Unpin, - time::{Duration, Instant}, -}; +use std::time::{Duration, Instant}; /// Simple wrapper for the different type of timers #[derive(Debug)] @@ -101,7 +98,7 @@ pub(crate) mod tokio { // Taken from: https://docs.rs/async-io/1.7.0/src/async_io/lib.rs.html#91 let mut inner = time::interval_at( TokioInstant::from_std(instant), - Duration::new(std::u64::MAX, 1_000_000_000 - 1), + Duration::new(u64::MAX, 1_000_000_000 - 1), ); inner.set_missed_tick_behavior(MissedTickBehavior::Skip); Self { inner } @@ -128,7 +125,7 @@ pub(crate) mod tokio { } fn size_hint(&self) -> (usize, Option) { - (std::usize::MAX, None) + (usize::MAX, None) } } } diff --git a/protocols/perf/CHANGELOG.md b/protocols/perf/CHANGELOG.md index 4e448d7f44a..abeca9fad25 100644 --- a/protocols/perf/CHANGELOG.md +++ b/protocols/perf/CHANGELOG.md @@ -1,3 +1,13 @@ +## 0.4.0 + + +- Add ConnectionError to FromSwarm::ConnectionClosed. + See [PR 5485](https://github.com/libp2p/rust-libp2p/pull/5485). + +## 0.3.1 +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + ## 0.3.0 - Continuously measure on single connection (iperf-style). diff --git a/protocols/perf/Cargo.toml b/protocols/perf/Cargo.toml index 68b1088a42c..398bdce65ec 100644 --- a/protocols/perf/Cargo.toml +++ b/protocols/perf/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-perf" edition = "2021" rust-version = { workspace = true } description = "libp2p perf protocol implementation" -version = "0.3.0" +version = "0.4.0" authors = ["Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -12,11 +12,11 @@ categories = ["network-programming", "asynchronous"] [dependencies] anyhow = "1" -clap = { version = "4.4.16", features = ["derive"] } -futures = "0.3.30" +clap = { version = "4.5.6", features = ["derive"] } +futures = { workspace = true } futures-bounded = { workspace = true } futures-timer = "3.0" -instant = "0.1.12" +web-time = { workspace = true } libp2p = { workspace = true, features = ["tokio", "tcp", "quic", "tls", "yamux", "dns"] } libp2p-core = { workspace = true } libp2p-dns = { workspace = true, features = ["tokio"] } @@ -29,9 +29,9 @@ libp2p-yamux = { workspace = true } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" thiserror = "1.0" -tracing = "0.1.37" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } -tokio = { version = "1.36", default-features = false, features = ["macros", "rt", "rt-multi-thread"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } +tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread"] } void = "1" [dev-dependencies] @@ -42,8 +42,6 @@ libp2p-swarm-test = { path = "../../swarm-test" } # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/perf/Dockerfile b/protocols/perf/Dockerfile index 6523e3bede1..f68ea6ef211 100644 --- a/protocols/perf/Dockerfile +++ b/protocols/perf/Dockerfile @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM rust:1.67.0 as builder +FROM rust:1.81.0 as builder # Run with access to the target cache to speed up builds WORKDIR /workspace diff --git a/protocols/perf/src/bin/perf.rs b/protocols/perf/src/bin/perf.rs index 9ac8f0a6cde..9a4cfb8bcac 100644 --- a/protocols/perf/src/bin/perf.rs +++ b/protocols/perf/src/bin/perf.rs @@ -23,7 +23,6 @@ use std::{net::SocketAddr, str::FromStr}; use anyhow::{bail, Result}; use clap::Parser; use futures::StreamExt; -use instant::{Duration, Instant}; use libp2p::core::{multiaddr::Protocol, upgrade, Multiaddr}; use libp2p::identity::PeerId; use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent}; @@ -32,6 +31,7 @@ use libp2p_perf::{client, server}; use libp2p_perf::{Final, Intermediate, Run, RunParams, RunUpdate}; use serde::{Deserialize, Serialize}; use tracing_subscriber::EnvFilter; +use web_time::{Duration, Instant}; #[derive(Debug, Parser)] #[clap(name = "libp2p perf client")] diff --git a/protocols/perf/src/client/behaviour.rs b/protocols/perf/src/client/behaviour.rs index 880bcdd9c83..1b181557acc 100644 --- a/protocols/perf/src/client/behaviour.rs +++ b/protocols/perf/src/client/behaviour.rs @@ -25,7 +25,7 @@ use std::{ task::{Context, Poll}, }; -use libp2p_core::Multiaddr; +use libp2p_core::{transport::PortUse, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::{ derive_prelude::ConnectionEstablished, ConnectionClosed, ConnectionId, FromSwarm, @@ -92,6 +92,7 @@ impl NetworkBehaviour for Behaviour { _peer: PeerId, _addr: &Multiaddr, _role_override: libp2p_core::Endpoint, + _port_use: PortUse, ) -> Result, libp2p_swarm::ConnectionDenied> { Ok(Handler::default()) } @@ -116,6 +117,7 @@ impl NetworkBehaviour for Behaviour { connection_id: _, endpoint: _, remaining_established, + .. }) => { if remaining_established == 0 { assert!(self.connected.remove(&peer_id)); diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index 2a2c5499fc2..55fafad7fcc 100644 --- a/protocols/perf/src/client/handler.rs +++ b/protocols/perf/src/client/handler.rs @@ -112,6 +112,8 @@ impl ConnectionHandler for Handler { >, ) { match event { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, .. }) => void::unreachable(protocol), @@ -144,6 +146,8 @@ impl ConnectionHandler for Handler { result: Err(error.into()), })); } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info: (), error }) => { void::unreachable(error) } diff --git a/protocols/perf/src/lib.rs b/protocols/perf/src/lib.rs index f9db96aa9d9..be950ac87a2 100644 --- a/protocols/perf/src/lib.rs +++ b/protocols/perf/src/lib.rs @@ -26,8 +26,8 @@ use std::fmt::Display; -use instant::Duration; use libp2p_swarm::StreamProtocol; +use web_time::Duration; pub mod client; mod protocol; diff --git a/protocols/perf/src/protocol.rs b/protocols/perf/src/protocol.rs index d2d65b42303..f995bbe2d3b 100644 --- a/protocols/perf/src/protocol.rs +++ b/protocols/perf/src/protocol.rs @@ -19,8 +19,8 @@ // DEALINGS IN THE SOFTWARE. use futures_timer::Delay; -use instant::Instant; use std::time::Duration; +use web_time::Instant; use futures::{ future::{select, Either}, diff --git a/protocols/perf/src/server/behaviour.rs b/protocols/perf/src/server/behaviour.rs index da24d763606..5408029e85d 100644 --- a/protocols/perf/src/server/behaviour.rs +++ b/protocols/perf/src/server/behaviour.rs @@ -25,6 +25,7 @@ use std::{ task::{Context, Poll}, }; +use libp2p_core::transport::PortUse; use libp2p_identity::PeerId; use libp2p_swarm::{ ConnectionId, FromSwarm, NetworkBehaviour, THandlerInEvent, THandlerOutEvent, ToSwarm, @@ -71,6 +72,7 @@ impl NetworkBehaviour for Behaviour { _peer: PeerId, _addr: &libp2p_core::Multiaddr, _role_override: libp2p_core::Endpoint, + _port_use: PortUse, ) -> Result, libp2p_swarm::ConnectionDenied> { Ok(Handler::default()) } diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index ddfe8f881e5..4cb535a452c 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -73,6 +73,8 @@ impl ConnectionHandler for Handler { } fn on_behaviour_event(&mut self, v: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(v) } @@ -98,16 +100,22 @@ impl ConnectionHandler for Handler { tracing::warn!("Dropping inbound stream because we are at capacity"); } } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { info, .. }) => { void::unreachable(info) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::DialUpgradeError(DialUpgradeError { info, .. }) => { void::unreachable(info) } ConnectionEvent::AddressChange(_) | ConnectionEvent::LocalProtocolsChange(_) | ConnectionEvent::RemoteProtocolsChange(_) => {} + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info: (), error }) => { void::unreachable(error) } diff --git a/protocols/ping/CHANGELOG.md b/protocols/ping/CHANGELOG.md index 33e0139b996..c0a124333e9 100644 --- a/protocols/ping/CHANGELOG.md +++ b/protocols/ping/CHANGELOG.md @@ -1,3 +1,23 @@ +## 0.45.0 + + + +## 0.44.2 + +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + +- Fix panic in WASM caused by retrying on dial upgrade errors. + See [PR 5447](https://github.com/libp2p/rust-libp2p/pull/5447). + +## 0.44.1 + +- Impose `Sync` on `ping::Failure::Other`. + `ping::Event` can now be shared between threads. + See [PR 5250] + +[PR 5250]: https://github.com/libp2p/rust-libp2p/pull/5250 + ## 0.44.0 diff --git a/protocols/ping/Cargo.toml b/protocols/ping/Cargo.toml index 5f08ac6fbbd..66775d3ba8d 100644 --- a/protocols/ping/Cargo.toml +++ b/protocols/ping/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-ping" edition = "2021" rust-version = { workspace = true } description = "Ping protocol for libp2p" -version = "0.44.0" +version = "0.45.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -11,15 +11,15 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] -either = "1.9.0" -futures = "0.3.30" +either = "1.11.0" +futures = { workspace = true } futures-timer = "3.0.3" -instant = "0.1.12" +web-time = { workspace = true } libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true } rand = "0.8" -tracing = "0.1.37" +tracing = { workspace = true } void = "1.0" [dev-dependencies] @@ -27,14 +27,12 @@ async-std = "1.6.2" libp2p-swarm = { workspace = true, features = ["macros"] } libp2p-swarm-test = { path = "../../swarm-test" } quickcheck = { workspace = true } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 5e6fc2cd2cf..7b36b2d4b3d 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -95,12 +95,12 @@ pub enum Failure { Unsupported, /// The ping failed for reasons other than a timeout. Other { - error: Box, + error: Box, }, } impl Failure { - fn other(e: impl std::error::Error + Send + 'static) -> Self { + fn other(e: impl std::error::Error + Send + Sync + 'static) -> Self { Self::Other { error: Box::new(e) } } } @@ -184,6 +184,18 @@ impl Handler { ) { self.outbound = None; // Request a new substream on the next `poll`. + // Timer is already polled and expired before substream request is initiated + // and will be polled again later on in our `poll` because we reset `self.outbound`. + // + // `futures-timer` allows an expired timer to be polled again and returns + // immediately `Poll::Ready`. However in its WASM implementation there is + // a bug that causes the expired timer to panic. + // This is a workaround until a proper fix is merged and released. + // See libp2p/rust-libp2p#5447 for more info. + // + // TODO: remove when async-rs/futures-timer#74 gets merged. + self.interval.reset(Duration::new(0, 0)); + let error = match error { StreamUpgradeError::NegotiationFailed => { debug_assert_eq!(self.state, State::Active); @@ -198,6 +210,8 @@ impl Handler { "ping protocol negotiation timed out", )), }, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(e) => void::unreachable(e), StreamUpgradeError::Io(e) => Failure::Other { error: Box::new(e) }, }; diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index 5eaa6d4952a..82f240cab6b 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -51,6 +51,7 @@ mod handler; mod protocol; use handler::Handler; +use libp2p_core::transport::PortUse; use libp2p_core::{Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::{ @@ -124,6 +125,7 @@ impl NetworkBehaviour for Behaviour { _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(Handler::new(self.config.clone())) } diff --git a/protocols/ping/src/protocol.rs b/protocols/ping/src/protocol.rs index 28549e1c198..566e5e258e2 100644 --- a/protocols/ping/src/protocol.rs +++ b/protocols/ping/src/protocol.rs @@ -19,10 +19,10 @@ // DEALINGS IN THE SOFTWARE. use futures::prelude::*; -use instant::Instant; use libp2p_swarm::StreamProtocol; use rand::{distributions, prelude::*}; use std::{io, time::Duration}; +use web_time::Instant; pub const PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/ipfs/ping/1.0.0"); @@ -44,8 +44,7 @@ pub const PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/ipfs/ping/1.0.0" /// > Nagle's algorithm, delayed acks and similar configuration options /// > which can affect latencies especially on otherwise low-volume /// > connections. -#[derive(Default, Debug, Copy, Clone)] -pub(crate) struct Ping; + const PING_SIZE: usize = 32; /// Sends a ping and waits for the pong. @@ -87,10 +86,9 @@ mod tests { use futures::StreamExt; use libp2p_core::{ multiaddr::multiaddr, - transport::{memory::MemoryTransport, ListenerId, Transport}, + transport::{memory::MemoryTransport, DialOpts, ListenerId, PortUse, Transport}, + Endpoint, }; - use rand::{thread_rng, Rng}; - use std::time::Duration; #[test] fn ping_pong() { @@ -113,7 +111,13 @@ mod tests { async_std::task::block_on(async move { let c = MemoryTransport::new() - .dial(listener_addr) + .dial( + listener_addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) .unwrap() .await .unwrap(); diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index aaade5e48f9..fc71ccedad5 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -1,3 +1,23 @@ +## 0.18.0 + + + +## 0.17.3 +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). +- Fix manual closure of relayed listener. + See [PR 5491](https://github.com/libp2p/rust-libp2p/pull/5491) +- Add resource limits to `CircuitReq` to be set + See [PR 5493](https://github.com/libp2p/rust-libp2p/pull/5493) + + +## 0.17.2 + +- Fix support for unlimited relay connection according to spec. + See [PR 5244](https://github.com/libp2p/rust-libp2p/pull/5244). +- use `web_time` `Instant` and `SystemTime` versions for wasm support. + See [PR 5328](https://github.com/libp2p/rust-libp2p/pull/5328). + ## 0.17.1 - Automatically register relayed addresses as external addresses. diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index 94b9deb1a64..084fec07efd 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-relay" edition = "2021" rust-version = { workspace = true } description = "Communications relaying for libp2p" -version = "0.17.1" +version = "0.18.0" authors = ["Parity Technologies ", "Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -13,11 +13,11 @@ categories = ["network-programming", "asynchronous"] [dependencies] asynchronous-codec = { workspace = true } bytes = "1" -either = "1.9.0" -futures = "0.3.30" +either = "1.12.0" +futures = { workspace = true } futures-timer = "3" futures-bounded = { workspace = true } -instant = "0.1.12" +web-time = { workspace = true } libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true } @@ -26,7 +26,7 @@ quick-protobuf-codec = { workspace = true } rand = "0.8.4" static_assertions = "1" thiserror = "1.0" -tracing = "0.1.37" +tracing = { workspace = true } void = "1" [dev-dependencies] @@ -37,15 +37,13 @@ libp2p-swarm = { workspace = true, features = ["macros", "async-std"] } libp2p-swarm-test = { workspace = true } libp2p-yamux = { workspace = true } quickcheck = { workspace = true } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index df8443e8359..46419ae64e3 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -27,8 +27,8 @@ use crate::multiaddr_ext::MultiaddrExt; use crate::proto; use crate::protocol::{inbound_hop, outbound_stop}; use either::Either; -use instant::Instant; use libp2p_core::multiaddr::Protocol; +use libp2p_core::transport::PortUse; use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm}; @@ -41,6 +41,7 @@ use std::num::NonZeroU32; use std::ops::Add; use std::task::{Context, Poll}; use std::time::Duration; +use web_time::Instant; /// Configuration for the relay [`Behaviour`]. /// @@ -328,6 +329,7 @@ impl NetworkBehaviour for Behaviour { _: PeerId, addr: &Multiaddr, role_override: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied> { if addr.is_relayed() { // Deny all substreams on relayed connection. @@ -343,6 +345,7 @@ impl NetworkBehaviour for Behaviour { ConnectedPoint::Dialer { address: addr.clone(), role_override, + port_use, }, ))) } @@ -363,6 +366,8 @@ impl NetworkBehaviour for Behaviour { ) { let event = match event { Either::Left(e) => e, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right(v) => void::unreachable(v), }; diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 958c6a9b906..23e90f4b3f8 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -28,7 +28,6 @@ use futures::future::{BoxFuture, FutureExt, TryFutureExt}; use futures::io::AsyncWriteExt; use futures::stream::{FuturesUnordered, StreamExt}; use futures_timer::Delay; -use instant::Instant; use libp2p_core::upgrade::ReadyUpgrade; use libp2p_core::{ConnectedPoint, Multiaddr}; use libp2p_identity::PeerId; @@ -43,6 +42,7 @@ use std::collections::{HashMap, VecDeque}; use std::task::{Context, Poll}; use std::time::Duration; use std::{fmt, io}; +use web_time::Instant; const MAX_CONCURRENT_STREAMS_PER_CONNECTION: usize = 10; const STREAM_TIMEOUT: Duration = Duration::from_secs(60); @@ -449,6 +449,8 @@ impl Handler { StreamUpgradeError::Timeout => outbound_stop::Error::Io(io::ErrorKind::TimedOut.into()), StreamUpgradeError::NegotiationFailed => outbound_stop::Error::Unsupported, StreamUpgradeError::Io(e) => outbound_stop::Error::Io(e), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(v) => void::unreachable(v), }; diff --git a/protocols/relay/src/behaviour/rate_limiter.rs b/protocols/relay/src/behaviour/rate_limiter.rs index 9c4f67d04a8..45b701c1b50 100644 --- a/protocols/relay/src/behaviour/rate_limiter.rs +++ b/protocols/relay/src/behaviour/rate_limiter.rs @@ -18,15 +18,14 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use instant::Instant; use libp2p_core::multiaddr::{Multiaddr, Protocol}; use libp2p_identity::PeerId; use std::collections::{HashMap, VecDeque}; -use std::convert::TryInto; use std::hash::Hash; use std::net::IpAddr; use std::num::NonZeroU32; use std::time::Duration; +use web_time::Instant; /// Allows rate limiting access to some resource based on the [`PeerId`] and /// [`Multiaddr`] of a remote peer. @@ -173,7 +172,6 @@ impl GenericRateLimiter { mod tests { use super::*; use quickcheck::{QuickCheck, TestResult}; - use std::num::NonZeroU32; #[test] fn first() { diff --git a/protocols/relay/src/copy_future.rs b/protocols/relay/src/copy_future.rs index 755344cd698..c0039c29534 100644 --- a/protocols/relay/src/copy_future.rs +++ b/protocols/relay/src/copy_future.rs @@ -30,7 +30,6 @@ use futures::io::{AsyncBufRead, BufReader}; use futures::io::{AsyncRead, AsyncWrite}; use futures::ready; use futures_timer::Delay; -use std::convert::TryInto; use std::io; use std::pin::Pin; use std::task::{Context, Poll}; @@ -73,7 +72,7 @@ where let this = &mut *self; loop { - if this.bytes_sent > this.max_circuit_bytes { + if this.max_circuit_bytes > 0 && this.bytes_sent > this.max_circuit_bytes { return Poll::Ready(Err(io::Error::new( io::ErrorKind::Other, "Max circuit bytes reached.", @@ -164,12 +163,9 @@ fn forward_data( mod tests { use super::*; use futures::executor::block_on; - use futures::io::{AsyncRead, AsyncWrite, BufReader, BufWriter}; + use futures::io::BufWriter; use quickcheck::QuickCheck; use std::io::ErrorKind; - use std::pin::Pin; - use std::task::{Context, Poll}; - use std::time::Duration; #[test] fn quickcheck() { diff --git a/protocols/relay/src/priv_client.rs b/protocols/relay/src/priv_client.rs index e414852ef81..8bbc813ec4c 100644 --- a/protocols/relay/src/priv_client.rs +++ b/protocols/relay/src/priv_client.rs @@ -34,6 +34,7 @@ use futures::io::{AsyncRead, AsyncWrite}; use futures::ready; use futures::stream::StreamExt; use libp2p_core::multiaddr::Protocol; +use libp2p_core::transport::PortUse; use libp2p_core::{Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm}; @@ -178,6 +179,7 @@ impl NetworkBehaviour for Behaviour { peer: PeerId, addr: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { if addr.is_relayed() { return Ok(Either::Right(dummy::ConnectionHandler)); @@ -234,6 +236,8 @@ impl NetworkBehaviour for Behaviour { ) { let handler_event = match handler_event { Either::Left(e) => e, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right(v) => void::unreachable(v), }; diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index 662d63cc742..05fdd5673ae 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -445,6 +445,8 @@ impl ConnectionHandler for Handler { let _ = next.send(Ok(ev.protocol)); } } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(ev) => void::unreachable(ev.error), ConnectionEvent::DialUpgradeError(ev) => { if let Some(next) = self.pending_streams.pop_front() { @@ -583,6 +585,8 @@ fn into_reserve_error(e: StreamUpgradeError) -> outbound_hop::ReserveError StreamUpgradeError::Timeout => { outbound_hop::ReserveError::Io(io::ErrorKind::TimedOut.into()) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(never) => void::unreachable(never), StreamUpgradeError::NegotiationFailed => outbound_hop::ReserveError::Unsupported, StreamUpgradeError::Io(e) => outbound_hop::ReserveError::Io(e), @@ -594,6 +598,8 @@ fn into_connect_error(e: StreamUpgradeError) -> outbound_hop::ConnectError StreamUpgradeError::Timeout => { outbound_hop::ConnectError::Io(io::ErrorKind::TimedOut.into()) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(never) => void::unreachable(never), StreamUpgradeError::NegotiationFailed => outbound_hop::ConnectError::Unsupported, StreamUpgradeError::Io(e) => outbound_hop::ConnectError::Io(e), diff --git a/protocols/relay/src/priv_client/transport.rs b/protocols/relay/src/priv_client/transport.rs index 7147f0b5e55..ec1e8ca5fb8 100644 --- a/protocols/relay/src/priv_client/transport.rs +++ b/protocols/relay/src/priv_client/transport.rs @@ -27,16 +27,15 @@ use crate::RequestId; use futures::channel::mpsc; use futures::channel::oneshot; use futures::future::{ready, BoxFuture, FutureExt, Ready}; -use futures::ready; use futures::sink::SinkExt; use futures::stream::SelectAll; use futures::stream::{Stream, StreamExt}; use libp2p_core::multiaddr::{Multiaddr, Protocol}; -use libp2p_core::transport::{ListenerId, TransportError, TransportEvent}; +use libp2p_core::transport::{DialOpts, ListenerId, TransportError, TransportEvent}; use libp2p_identity::PeerId; use std::collections::VecDeque; use std::pin::Pin; -use std::task::{Context, Poll}; +use std::task::{Context, Poll, Waker}; use thiserror::Error; /// A [`Transport`] enabling client relay capabilities. @@ -50,7 +49,7 @@ use thiserror::Error; /// 1. Establish relayed connections by dialing `/p2p-circuit` addresses. /// /// ``` -/// # use libp2p_core::{Multiaddr, multiaddr::{Protocol}, Transport}; +/// # use libp2p_core::{Multiaddr, multiaddr::{Protocol}, Transport, transport::{DialOpts, PortUse}, connection::Endpoint}; /// # use libp2p_core::transport::memory::MemoryTransport; /// # use libp2p_core::transport::choice::OrTransport; /// # use libp2p_relay as relay; @@ -67,7 +66,10 @@ use thiserror::Error; /// .with(Protocol::P2p(relay_id.into())) // Relay peer id. /// .with(Protocol::P2pCircuit) // Signal to connect via relay and not directly. /// .with(Protocol::P2p(destination_id.into())); // Destination peer id. -/// transport.dial(dst_addr_via_relay).unwrap(); +/// transport.dial(dst_addr_via_relay, DialOpts { +/// port_use: PortUse::Reuse, +/// role: Endpoint::Dialer, +/// }).unwrap(); /// ``` /// /// 3. Listen for incoming relayed connections via specific relay. @@ -151,6 +153,7 @@ impl libp2p_core::Transport for Transport { queued_events: Default::default(), from_behaviour, is_closed: false, + waker: None, }; self.listeners.push(listener); Ok(()) @@ -165,7 +168,19 @@ impl libp2p_core::Transport for Transport { } } - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + dial_opts: DialOpts, + ) -> Result> { + if dial_opts.role.is_listener() { + // [`Endpoint::Listener`] is used for NAT and firewall + // traversal. One would coordinate such traversal via a previously + // established relayed connection, but never using a relayed connection + // itself. + return Err(TransportError::MultiaddrNotSupported(addr)); + } + let RelayedMultiaddr { relay_peer_id, relay_addr, @@ -198,24 +213,6 @@ impl libp2p_core::Transport for Transport { .boxed()) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> - where - Self: Sized, - { - // [`Transport::dial_as_listener`] is used for NAT and firewall - // traversal. One would coordinate such traversal via a previously - // established relayed connection, but never using a relayed connection - // itself. - Err(TransportError::MultiaddrNotSupported(addr)) - } - - fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option { - None - } - fn poll( mut self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -313,6 +310,7 @@ pub(crate) struct Listener { /// The listener can be closed either manually with [`Transport::remove_listener`](libp2p_core::Transport) or if /// the sender side of the `from_behaviour` channel is dropped. is_closed: bool, + waker: Option, } impl Listener { @@ -328,6 +326,10 @@ impl Listener { reason, }); self.is_closed = true; + + if let Some(waker) = self.waker.take() { + waker.wake(); + } } } @@ -337,18 +339,27 @@ impl Stream for Listener { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { loop { if let Some(event) = self.queued_events.pop_front() { + self.waker = None; return Poll::Ready(Some(event)); } if self.is_closed { // Terminate the stream if the listener closed and all remaining events have been reported. + self.waker = None; return Poll::Ready(None); } - let Some(msg) = ready!(self.from_behaviour.poll_next_unpin(cx)) else { - // Sender of `from_behaviour` has been dropped, signaling listener to close. - self.close(Ok(())); - continue; + let msg = match self.from_behaviour.poll_next_unpin(cx) { + Poll::Ready(Some(msg)) => msg, + Poll::Ready(None) => { + // Sender of `from_behaviour` has been dropped, signaling listener to close. + self.close(Ok(())); + continue; + } + Poll::Pending => { + self.waker = Some(cx.waker().clone()); + return Poll::Pending; + } }; match msg { diff --git a/protocols/relay/src/protocol/inbound_hop.rs b/protocols/relay/src/protocol/inbound_hop.rs index 41fe2675dce..401c6258176 100644 --- a/protocols/relay/src/protocol/inbound_hop.rs +++ b/protocols/relay/src/protocol/inbound_hop.rs @@ -18,7 +18,8 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use std::time::{Duration, SystemTime}; +use std::time::Duration; +use web_time::SystemTime; use asynchronous_codec::{Framed, FramedParts}; use bytes::Bytes; @@ -114,6 +115,8 @@ impl ReservationReq { pub struct CircuitReq { dst: PeerId, substream: Framed>, + max_circuit_duration: Duration, + max_circuit_bytes: u64, } impl CircuitReq { @@ -126,7 +129,15 @@ impl CircuitReq { type_pb: proto::HopMessageType::STATUS, peer: None, reservation: None, - limit: None, + limit: Some(proto::Limit { + duration: Some( + self.max_circuit_duration + .as_secs() + .try_into() + .expect("`max_circuit_duration` not to exceed `u32::MAX`."), + ), + data: Some(self.max_circuit_bytes), + }), status: Some(proto::Status::OK), }; @@ -203,7 +214,12 @@ pub(crate) async fn handle_inbound_request( let dst = peer_id_res.map_err(|_| Error::ParsePeerId)?; - Either::Right(CircuitReq { dst, substream }) + Either::Right(CircuitReq { + dst, + substream, + max_circuit_duration, + max_circuit_bytes, + }) } Type::STATUS => return Err(Error::UnexpectedTypeStatus), }; diff --git a/protocols/relay/src/protocol/outbound_hop.rs b/protocols/relay/src/protocol/outbound_hop.rs index 3ae824be167..b349f8848be 100644 --- a/protocols/relay/src/protocol/outbound_hop.rs +++ b/protocols/relay/src/protocol/outbound_hop.rs @@ -19,13 +19,14 @@ // DEALINGS IN THE SOFTWARE. use std::io; -use std::time::{Duration, SystemTime}; +use std::time::Duration; use asynchronous_codec::{Framed, FramedParts}; use bytes::Bytes; use futures::prelude::*; use futures_timer::Delay; use thiserror::Error; +use web_time::SystemTime; use libp2p_core::Multiaddr; use libp2p_identity::PeerId; diff --git a/protocols/rendezvous/CHANGELOG.md b/protocols/rendezvous/CHANGELOG.md index e60699da734..1ed9e5bc3b0 100644 --- a/protocols/rendezvous/CHANGELOG.md +++ b/protocols/rendezvous/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.15.0 + + + +## 0.14.1 +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + ## 0.14.0 diff --git a/protocols/rendezvous/Cargo.toml b/protocols/rendezvous/Cargo.toml index e588c6d7a6b..78a6a1a0a4c 100644 --- a/protocols/rendezvous/Cargo.toml +++ b/protocols/rendezvous/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-rendezvous" edition = "2021" rust-version = { workspace = true } description = "Rendezvous protocol for libp2p" -version = "0.14.0" +version = "0.15.0" authors = ["The COMIT guys "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -14,9 +14,9 @@ categories = ["network-programming", "asynchronous"] asynchronous-codec = { workspace = true } async-trait = "0.1" bimap = "0.6.3" -futures = { version = "0.3", default-features = false, features = ["std"] } +futures = { workspace = true, features = ["std"] } futures-timer = "3.0.3" -instant = "0.1.12" +web-time = { workspace = true } libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true } @@ -25,7 +25,7 @@ quick-protobuf = "0.8" quick-protobuf-codec = { workspace = true } rand = "0.8" thiserror = "1" -tracing = "0.1.37" +tracing = { workspace = true } void = "1" [dev-dependencies] @@ -37,15 +37,13 @@ libp2p-swarm-test = { path = "../../swarm-test" } libp2p-tcp = { workspace = true, features = ["tokio"] } libp2p-yamux = { workspace = true } rand = "0.8" -tokio = { version = "1.36", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs", "net" ] } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tokio = { workspace = true, features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs", "net" ] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index 92d7884758b..a794252ff0b 100644 --- a/protocols/rendezvous/src/client.rs +++ b/protocols/rendezvous/src/client.rs @@ -24,6 +24,7 @@ use futures::future::BoxFuture; use futures::future::FutureExt; use futures::stream::FuturesUnordered; use futures::stream::StreamExt; +use libp2p_core::transport::PortUse; use libp2p_core::{Endpoint, Multiaddr, PeerRecord}; use libp2p_identity::{Keypair, PeerId, SigningError}; use libp2p_request_response::{OutboundRequestId, ProtocolSupport}; @@ -208,9 +209,15 @@ impl NetworkBehaviour for Behaviour { peer: PeerId, addr: &Multiaddr, role_override: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied> { - self.inner - .handle_established_outbound_connection(connection_id, peer, addr, role_override) + self.inner.handle_established_outbound_connection( + connection_id, + peer, + addr, + role_override, + port_use, + ) } fn on_connection_handler_event( diff --git a/protocols/rendezvous/src/codec.rs b/protocols/rendezvous/src/codec.rs index 41432a91d8c..cad3688e00b 100644 --- a/protocols/rendezvous/src/codec.rs +++ b/protocols/rendezvous/src/codec.rs @@ -27,7 +27,6 @@ use libp2p_core::{peer_record, signed_envelope, PeerRecord, SignedEnvelope}; use libp2p_swarm::StreamProtocol; use quick_protobuf_codec::Codec as ProtobufCodec; use rand::RngCore; -use std::convert::{TryFrom, TryInto}; use std::{fmt, io}; pub type Ttl = u64; @@ -642,7 +641,6 @@ mod proto { #[cfg(test)] mod tests { use super::*; - use crate::Namespace; #[test] fn cookie_wire_encoding_roundtrip() { diff --git a/protocols/rendezvous/src/server.rs b/protocols/rendezvous/src/server.rs index 667c71e20e3..45a525d9573 100644 --- a/protocols/rendezvous/src/server.rs +++ b/protocols/rendezvous/src/server.rs @@ -24,6 +24,7 @@ use bimap::BiMap; use futures::future::BoxFuture; use futures::stream::FuturesUnordered; use futures::{FutureExt, StreamExt}; +use libp2p_core::transport::PortUse; use libp2p_core::{Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_request_response::ProtocolSupport; @@ -34,7 +35,6 @@ use libp2p_swarm::{ }; use std::collections::{HashMap, HashSet}; use std::iter; -use std::iter::FromIterator; use std::task::{ready, Context, Poll}; use std::time::Duration; @@ -140,9 +140,15 @@ impl NetworkBehaviour for Behaviour { peer: PeerId, addr: &Multiaddr, role_override: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied> { - self.inner - .handle_established_outbound_connection(connection_id, peer, addr, role_override) + self.inner.handle_established_outbound_connection( + connection_id, + peer, + addr, + role_override, + port_use, + ) } fn on_connection_handler_event( @@ -443,7 +449,7 @@ impl Registrations { match (discover_namespace.as_ref(), cookie_namespace) { // discover all namespace but cookie is specific to a namespace? => bad (None, Some(_)) => return Err(CookieNamespaceMismatch), - // discover for a namespace but cookie is for a different namesapce? => bad + // discover for a namespace but cookie is for a different namespace? => bad (Some(namespace), Some(cookie_namespace)) if namespace != cookie_namespace => { return Err(CookieNamespaceMismatch) } @@ -528,8 +534,7 @@ pub struct CookieNamespaceMismatch; #[cfg(test)] mod tests { - use instant::SystemTime; - use std::option::Option::None; + use web_time::SystemTime; use libp2p_core::PeerRecord; use libp2p_identity as identity; diff --git a/protocols/rendezvous/tests/rendezvous.rs b/protocols/rendezvous/tests/rendezvous.rs index c2de88fd615..d9200780ece 100644 --- a/protocols/rendezvous/tests/rendezvous.rs +++ b/protocols/rendezvous/tests/rendezvous.rs @@ -27,7 +27,6 @@ use libp2p_rendezvous as rendezvous; use libp2p_rendezvous::client::RegisterError; use libp2p_swarm::{DialError, Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; -use std::convert::TryInto; use std::time::Duration; use tracing_subscriber::EnvFilter; diff --git a/protocols/request-response/CHANGELOG.md b/protocols/request-response/CHANGELOG.md index 92417508786..db0d9126516 100644 --- a/protocols/request-response/CHANGELOG.md +++ b/protocols/request-response/CHANGELOG.md @@ -1,3 +1,20 @@ +## 0.27.0 + + + +## 0.26.4 + +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). + +## 0.26.3 + +- Report failure when streams are at capacity. + See [PR 5417](https://github.com/libp2p/rust-libp2p/pull/5417). + +- Report dial IO errors to the user. + See [PR 5429](https://github.com/libp2p/rust-libp2p/pull/5429). + ## 0.26.2 - Deprecate `Behaviour::add_address` in favor of `Swarm::add_peer_address`. diff --git a/protocols/request-response/Cargo.toml b/protocols/request-response/Cargo.toml index 4b4021285a4..c6b2eda348b 100644 --- a/protocols/request-response/Cargo.toml +++ b/protocols/request-response/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-request-response" edition = "2021" rust-version = { workspace = true } description = "Generic Request/Response Protocols" -version = "0.26.2" +version = "0.27.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -13,16 +13,16 @@ categories = ["network-programming", "asynchronous"] [dependencies] async-trait = "0.1" cbor4ii = { version = "0.3.2", features = ["serde1", "use_std"], optional = true } -futures = "0.3.30" -instant = "0.1.12" +futures = { workspace = true } +web-time = { workspace = true } libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true } rand = "0.8" serde = { version = "1.0", optional = true} -serde_json = { version = "1.0.114", optional = true } -smallvec = "1.13.1" -tracing = "0.1.37" +serde_json = { version = "1.0.117", optional = true } +smallvec = "1.13.2" +tracing = { workspace = true } void = "1.0.2" futures-timer = "3.0.3" futures-bounded = { workspace = true } @@ -32,7 +32,7 @@ json = ["dep:serde", "dep:serde_json", "libp2p-swarm/macros"] cbor = ["dep:serde", "dep:cbor4ii", "libp2p-swarm/macros"] [dev-dependencies] -anyhow = "1.0.80" +anyhow = "1.0.86" async-std = { version = "1.6.2", features = ["attributes"] } libp2p-noise = { workspace = true } libp2p-tcp = { workspace = true, features = ["async-io"] } @@ -41,14 +41,12 @@ rand = "0.8" libp2p-swarm-test = { path = "../../swarm-test" } futures_ringbuf = "0.4.0" serde = { version = "1.0", features = ["derive"] } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/protocols/request-response/src/cbor.rs b/protocols/request-response/src/cbor.rs index f371f6149dc..a27d069e758 100644 --- a/protocols/request-response/src/cbor.rs +++ b/protocols/request-response/src/cbor.rs @@ -47,7 +47,6 @@ mod codec { use async_trait::async_trait; use cbor4ii::core::error::DecodeError; use futures::prelude::*; - use futures::{AsyncRead, AsyncWrite}; use libp2p_swarm::StreamProtocol; use serde::{de::DeserializeOwned, Serialize}; use std::{collections::TryReserveError, convert::Infallible, io, marker::PhantomData}; @@ -144,6 +143,8 @@ mod codec { fn decode_into_io_error(err: cbor4ii::serde::DecodeError) -> io::Error { match err { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] cbor4ii::serde::DecodeError::Core(DecodeError::Read(e)) => { io::Error::new(io::ErrorKind::Other, e) } diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index 2d45e0d7dc3..0591b37dc30 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -159,6 +159,9 @@ where } }; + // Inbound connections are reported to the upper layer from within the above task, + // so by failing to schedule it, it means the upper layer will never know about the + // inbound request. Because of that we do not report any inbound failure. if self .worker_streams .try_push(RequestId::Inbound(request_id), recv.boxed()) @@ -204,7 +207,10 @@ where .try_push(RequestId::Outbound(request_id), send.boxed()) .is_err() { - tracing::warn!("Dropping outbound stream because we are at capacity") + self.pending_events.push_back(Event::OutboundStreamFailed { + request_id: message.request_id, + error: io::Error::new(io::ErrorKind::Other, "max sub-streams reached"), + }); } } @@ -234,13 +240,14 @@ where self.pending_events .push_back(Event::OutboundUnsupportedProtocols(message.request_id)); } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(e) => void::unreachable(e), StreamUpgradeError::Io(e) => { - tracing::debug!( - "outbound stream for request {} failed: {e}, retrying", - message.request_id - ); - self.requested_outbound.push_back(message); + self.pending_events.push_back(Event::OutboundStreamFailed { + request_id: message.request_id, + error: e, + }); } } } @@ -251,6 +258,8 @@ where ::InboundProtocol, >, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(error) } } @@ -479,6 +488,8 @@ where ConnectionEvent::DialUpgradeError(dial_upgrade_error) => { self.on_dial_upgrade_error(dial_upgrade_error) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => { self.on_listen_upgrade_error(listen_upgrade_error) } diff --git a/protocols/request-response/src/json.rs b/protocols/request-response/src/json.rs index 0b3d634573b..85e78e7ddda 100644 --- a/protocols/request-response/src/json.rs +++ b/protocols/request-response/src/json.rs @@ -45,7 +45,6 @@ pub type Behaviour = crate::Behaviour>; mod codec { use async_trait::async_trait; use futures::prelude::*; - use futures::{AsyncRead, AsyncWrite}; use libp2p_swarm::StreamProtocol; use serde::{de::DeserializeOwned, Serialize}; use std::{io, marker::PhantomData}; diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index 4362b3255ad..e627f5668ff 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -79,7 +79,7 @@ pub use handler::ProtocolSupport; use crate::handler::OutboundMessage; use futures::channel::oneshot; use handler::Handler; -use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr}; +use libp2p_core::{transport::PortUse, ConnectedPoint, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::{ behaviour::{AddressChange, ConnectionClosed, DialFailure, FromSwarm}, @@ -772,6 +772,7 @@ where peer: PeerId, remote_address: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { let mut handler = Handler::new( self.inbound_protocols.clone(), diff --git a/protocols/request-response/tests/error_reporting.rs b/protocols/request-response/tests/error_reporting.rs index 2dc82b2e0c5..19f323e169f 100644 --- a/protocols/request-response/tests/error_reporting.rs +++ b/protocols/request-response/tests/error_reporting.rs @@ -161,6 +161,58 @@ async fn report_outbound_timeout_on_read_response() { futures::future::select(server_task, client_task).await; } +#[async_std::test] +async fn report_outbound_failure_on_max_streams() { + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); + + // `swarm2` will be able to handle only 1 stream per time. + let swarm2_config = request_response::Config::default() + .with_request_timeout(Duration::from_millis(100)) + .with_max_concurrent_streams(1); + + let (peer1_id, mut swarm1) = new_swarm(); + let (peer2_id, mut swarm2) = new_swarm_with_config(swarm2_config); + + swarm1.listen().with_memory_addr_external().await; + swarm2.connect(&mut swarm1).await; + + let swarm1_task = async move { + let _req_id = swarm1 + .behaviour_mut() + .send_request(&peer2_id, Action::FailOnMaxStreams); + + // Keep the connection alive, otherwise swarm2 may receive `ConnectionClosed` instead. + wait_no_events(&mut swarm1).await; + }; + + // Expects OutboundFailure::Io failure. + let swarm2_task = async move { + let (peer, _inbound_req_id, action, _resp_channel) = + wait_request(&mut swarm2).await.unwrap(); + assert_eq!(peer, peer1_id); + assert_eq!(action, Action::FailOnMaxStreams); + + // A task for sending back a response is already scheduled so max concurrent + // streams is reached and no new tasks can be scheduled. + // + // We produce the failure by creating new request before we response. + let outbound_req_id = swarm2 + .behaviour_mut() + .send_request(&peer1_id, Action::FailOnMaxStreams); + + let (peer, req_id_done, error) = wait_outbound_failure(&mut swarm2).await.unwrap(); + assert_eq!(peer, peer1_id); + assert_eq!(req_id_done, outbound_req_id); + assert!(matches!(error, OutboundFailure::Io(_))); + }; + + let swarm1_task = pin!(swarm1_task); + let swarm2_task = pin!(swarm2_task); + futures::future::select(swarm1_task, swarm2_task).await; +} + #[async_std::test] async fn report_inbound_failure_on_read_request() { let _ = tracing_subscriber::fmt() @@ -332,6 +384,7 @@ enum Action { FailOnWriteRequest, FailOnWriteResponse, TimeoutOnWriteResponse, + FailOnMaxStreams, } impl From for u8 { @@ -343,6 +396,7 @@ impl From for u8 { Action::FailOnWriteRequest => 3, Action::FailOnWriteResponse => 4, Action::TimeoutOnWriteResponse => 5, + Action::FailOnMaxStreams => 6, } } } @@ -358,6 +412,7 @@ impl TryFrom for Action { 3 => Ok(Action::FailOnWriteRequest), 4 => Ok(Action::FailOnWriteResponse), 5 => Ok(Action::TimeoutOnWriteResponse), + 6 => Ok(Action::FailOnMaxStreams), _ => Err(io::Error::new(io::ErrorKind::Other, "invalid action")), } } @@ -468,11 +523,10 @@ impl Codec for TestCodec { } } -fn new_swarm_with_timeout( - timeout: Duration, +fn new_swarm_with_config( + cfg: request_response::Config, ) -> (PeerId, Swarm>) { let protocols = iter::once((StreamProtocol::new("/test/1"), ProtocolSupport::Full)); - let cfg = request_response::Config::default().with_request_timeout(timeout); let swarm = Swarm::new_ephemeral(|_| request_response::Behaviour::::new(protocols, cfg)); @@ -481,6 +535,13 @@ fn new_swarm_with_timeout( (peed_id, swarm) } +fn new_swarm_with_timeout( + timeout: Duration, +) -> (PeerId, Swarm>) { + let cfg = request_response::Config::default().with_request_timeout(timeout); + new_swarm_with_config(cfg) +} + fn new_swarm() -> (PeerId, Swarm>) { new_swarm_with_timeout(Duration::from_millis(100)) } diff --git a/protocols/request-response/tests/peer_address.rs b/protocols/request-response/tests/peer_address.rs index 2a120931dcd..0ed7ffe5551 100644 --- a/protocols/request-response/tests/peer_address.rs +++ b/protocols/request-response/tests/peer_address.rs @@ -8,6 +8,7 @@ use std::iter; use tracing_subscriber::EnvFilter; #[async_std::test] +#[cfg(feature = "cbor")] async fn dial_succeeds_after_adding_peers_address() { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) diff --git a/protocols/request-response/tests/ping.rs b/protocols/request-response/tests/ping.rs index b9e7878a78b..827afae249c 100644 --- a/protocols/request-response/tests/ping.rs +++ b/protocols/request-response/tests/ping.rs @@ -26,7 +26,7 @@ use libp2p_request_response as request_response; use libp2p_request_response::ProtocolSupport; use libp2p_swarm::{StreamProtocol, Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; -use rand::{self, Rng}; +use rand::Rng; use serde::{Deserialize, Serialize}; use std::{io, iter}; use tracing_subscriber::EnvFilter; diff --git a/protocols/stream/CHANGELOG.md b/protocols/stream/CHANGELOG.md index 1e3b85da0b9..2532970d3c6 100644 --- a/protocols/stream/CHANGELOG.md +++ b/protocols/stream/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.0-alpha + + + ## 0.1.0-alpha.1 - Implement Error for `OpenStreamError`. See [PR 5169](https://github.com/libp2p/rust-libp2p/pull/5169). diff --git a/protocols/stream/Cargo.toml b/protocols/stream/Cargo.toml index 6a39794c196..9aa9559a2d6 100644 --- a/protocols/stream/Cargo.toml +++ b/protocols/stream/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-stream" -version = "0.1.0-alpha.1" +version = "0.2.0-alpha" edition = "2021" rust-version.workspace = true description = "Generic stream protocols for libp2p" @@ -10,18 +10,18 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] -futures = "0.3.29" +futures = { workspace = true } libp2p-core = { workspace = true } libp2p-identity = { workspace = true, features = ["peerid"] } libp2p-swarm = { workspace = true } -tracing = "0.1.37" +tracing = { workspace = true } void = "1" rand = "0.8" [dev-dependencies] libp2p-swarm-test = { workspace = true } -tokio = { version = "1", features = ["full"] } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tokio = { workspace = true, features = ["full"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] workspace = true diff --git a/protocols/stream/src/behaviour.rs b/protocols/stream/src/behaviour.rs index e02aca884b7..07549ccef54 100644 --- a/protocols/stream/src/behaviour.rs +++ b/protocols/stream/src/behaviour.rs @@ -5,7 +5,7 @@ use std::{ }; use futures::{channel::mpsc, StreamExt}; -use libp2p_core::{Endpoint, Multiaddr}; +use libp2p_core::{transport::PortUse, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use libp2p_swarm::{ self as swarm, dial_opts::DialOpts, ConnectionDenied, ConnectionId, FromSwarm, @@ -82,6 +82,7 @@ impl NetworkBehaviour for Behaviour { peer: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(Handler::new( peer, diff --git a/protocols/stream/src/handler.rs b/protocols/stream/src/handler.rs index f63b93c1761..bf80e30c3c6 100644 --- a/protocols/stream/src/handler.rs +++ b/protocols/stream/src/handler.rs @@ -96,6 +96,8 @@ impl ConnectionHandler for Handler { } fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -143,6 +145,8 @@ impl ConnectionHandler for Handler { swarm::StreamUpgradeError::Timeout => { OpenStreamError::Io(io::Error::from(io::ErrorKind::TimedOut)) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] swarm::StreamUpgradeError::Apply(v) => void::unreachable(v), swarm::StreamUpgradeError::NegotiationFailed => { OpenStreamError::UnsupportedProtocol(p) diff --git a/protocols/upnp/CHANGELOG.md b/protocols/upnp/CHANGELOG.md index ba031264a0f..d9c24f8efcc 100644 --- a/protocols/upnp/CHANGELOG.md +++ b/protocols/upnp/CHANGELOG.md @@ -1,3 +1,16 @@ +## 0.3.1 +- update igd-next to 0.15.1. + See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX). + +## 0.3.0 + + + +## 0.2.2 +- Fix a panic caused when `upnp::Gateway` is dropped and its events queue receiver is no longer +available. + See [PR 5273](https://github.com/libp2p/rust-libp2p/pull/5273). + ## 0.2.1 - Fix a panic caused when dropping `upnp::Behaviour` such as when used together with `Toggle`. See [PR 5096](https://github.com/libp2p/rust-libp2p/pull/5096). diff --git a/protocols/upnp/Cargo.toml b/protocols/upnp/Cargo.toml index 3119804bf60..209733f53e6 100644 --- a/protocols/upnp/Cargo.toml +++ b/protocols/upnp/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-upnp" edition = "2021" rust-version = "1.60.0" description = "UPnP support for libp2p transports" -version = "0.2.1" +version = "0.3.1" license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" keywords = ["peer-to-peer", "libp2p", "networking"] @@ -11,13 +11,13 @@ categories = ["network-programming", "asynchronous"] publish = true [dependencies] -futures = "0.3.30" +futures = { workspace = true } futures-timer = "3.0.3" -igd-next = "0.14.3" +igd-next = "0.15.1" libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } -tokio = { version = "1.36", default-features = false, features = ["rt"], optional = true } -tracing = "0.1.37" +tokio = { workspace = true, default-features = false, features = ["rt"], optional = true } +tracing = { workspace = true } void = "1.0.2" [features] @@ -30,5 +30,3 @@ workspace = true # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] diff --git a/protocols/upnp/src/behaviour.rs b/protocols/upnp/src/behaviour.rs index a94ef9526dd..29a7fbf84a4 100644 --- a/protocols/upnp/src/behaviour.rs +++ b/protocols/upnp/src/behaviour.rs @@ -36,7 +36,11 @@ use crate::tokio::{is_addr_global, Gateway}; use futures::{channel::oneshot, Future, StreamExt}; use futures_timer::Delay; use igd_next::PortMappingProtocol; -use libp2p_core::{multiaddr, transport::ListenerId, Endpoint, Multiaddr}; +use libp2p_core::{ + multiaddr, + transport::{ListenerId, PortUse}, + Endpoint, Multiaddr, +}; use libp2p_swarm::{ derive_prelude::PeerId, dummy, ConnectionDenied, ConnectionId, ExpiredListenAddr, FromSwarm, NetworkBehaviour, NewListenAddr, ToSwarm, @@ -248,6 +252,7 @@ impl NetworkBehaviour for Behaviour { _peer: PeerId, _addr: &Multiaddr, _role_override: Endpoint, + _port_use: PortUse, ) -> Result, libp2p_swarm::ConnectionDenied> { Ok(dummy::ConnectionHandler) } diff --git a/protocols/upnp/src/tokio.rs b/protocols/upnp/src/tokio.rs index 9c8b2cafef9..b2cad6fa5a7 100644 --- a/protocols/upnp/src/tokio.rs +++ b/protocols/upnp/src/tokio.rs @@ -158,10 +158,10 @@ pub(crate) fn search_gateway() -> oneshot::Receiver"] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -14,17 +14,15 @@ categories = ["network-programming", "asynchronous"] proc-macro = true [dependencies] -heck = "0.4" +heck = "0.5" quote = "1.0" -syn = { version = "2.0.52", default-features = false, features = ["clone-impls", "derive", "parsing", "printing", "proc-macro"] } +syn = { version = "2.0.66", default-features = false, features = ["clone-impls", "derive", "parsing", "printing", "proc-macro"] } proc-macro2 = "1.0" # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 2e7daf7acc4..258c0b976c8 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -76,6 +76,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result syn::Result syn::Result Result<#t_handler, #connection_denied> { Ok(#handle_established_outbound_connection) } diff --git a/swarm-test/CHANGELOG.md b/swarm-test/CHANGELOG.md index 95223e60272..33eebb2412c 100644 --- a/swarm-test/CHANGELOG.md +++ b/swarm-test/CHANGELOG.md @@ -1,3 +1,14 @@ +## 0.4.1 + +- Add `tokio` runtime support and make `tokio` and `async-std` runtimes optional behind features. + See [PR 5551]. + +[PR 5551]: https://github.com/libp2p/rust-libp2p/pull/5551 + +## 0.4.0 + + + ## 0.3.0 diff --git a/swarm-test/Cargo.toml b/swarm-test/Cargo.toml index ab27e141a4b..fa51454dd58 100644 --- a/swarm-test/Cargo.toml +++ b/swarm-test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-swarm-test" -version = "0.3.0" +version = "0.4.1" edition = "2021" rust-version = { workspace = true } license = "MIT" @@ -12,17 +12,23 @@ categories = ["network-programming", "asynchronous"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -async-trait = "0.1.77" +async-trait = "0.1.80" libp2p-core = { workspace = true } libp2p-identity = { workspace = true, features = ["rand"] } libp2p-plaintext = { workspace = true } -libp2p-swarm = { workspace = true, features = ["async-std"] } -libp2p-tcp = { workspace = true, features = ["async-io"] } +libp2p-swarm = { workspace = true } +libp2p-tcp = { workspace = true } libp2p-yamux = { workspace = true } -futures = "0.3.30" +futures = { workspace = true } rand = "0.8.5" -tracing = "0.1.37" +tracing = { workspace = true } futures-timer = "3.0.3" +[features] +default = ["async-std"] + +async-std = ["libp2p-swarm/async-std", "libp2p-tcp/async-io"] +tokio = ["libp2p-swarm/tokio", "libp2p-tcp/tokio"] + [lints] workspace = true diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index 48f5bcbf4ef..bcab6e5b700 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -21,14 +21,10 @@ use async_trait::async_trait; use futures::future::{BoxFuture, Either}; use futures::{FutureExt, StreamExt}; -use libp2p_core::{ - multiaddr::Protocol, transport::MemoryTransport, upgrade::Version, Multiaddr, Transport, -}; -use libp2p_identity::{Keypair, PeerId}; -use libp2p_plaintext as plaintext; +use libp2p_core::{multiaddr::Protocol, Multiaddr}; +use libp2p_identity::PeerId; use libp2p_swarm::dial_opts::PeerCondition; -use libp2p_swarm::{self as swarm, dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmEvent}; -use libp2p_yamux as yamux; +use libp2p_swarm::{dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmEvent}; use std::fmt::Debug; use std::future::IntoFuture; use std::time::Duration; @@ -38,12 +34,23 @@ use std::time::Duration; pub trait SwarmExt { type NB: NetworkBehaviour; - /// Create a new [`Swarm`] with an ephemeral identity. + /// Create a new [`Swarm`] with an ephemeral identity and the `async-std` runtime. /// - /// The swarm will use a [`MemoryTransport`] together with a [`plaintext::Config`] authentication layer and - /// [`yamux::Config`] as the multiplexer. However, these details should not be relied upon by the test + /// The swarm will use a [`libp2p_core::transport::MemoryTransport`] together with a [`libp2p_plaintext::Config`] authentication layer and + /// [`libp2p_yamux::Config`] as the multiplexer. However, these details should not be relied upon by the test /// and may change at any time. - fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Self + #[cfg(feature = "async-std")] + fn new_ephemeral(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self + where + Self: Sized; + + /// Create a new [`Swarm`] with an ephemeral identity and the `tokio` runtime. + /// + /// The swarm will use a [`libp2p_core::transport::MemoryTransport`] together with a [`libp2p_plaintext::Config`] authentication layer and + /// [`libp2p_yamux::Config`] as the multiplexer. However, these details should not be relied upon by the test + /// and may change at any time. + #[cfg(feature = "tokio")] + fn new_ephemeral_tokio(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self where Self: Sized; @@ -200,18 +207,50 @@ where { type NB = B; - fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Self + #[cfg(feature = "async-std")] + fn new_ephemeral(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self where Self: Sized, { + use libp2p_core::{transport::MemoryTransport, upgrade::Version, Transport as _}; + use libp2p_identity::Keypair; + let identity = Keypair::generate_ed25519(); let peer_id = PeerId::from(identity.public()); let transport = MemoryTransport::default() .or_transport(libp2p_tcp::async_io::Transport::default()) .upgrade(Version::V1) - .authenticate(plaintext::Config::new(&identity)) - .multiplex(yamux::Config::default()) + .authenticate(libp2p_plaintext::Config::new(&identity)) + .multiplex(libp2p_yamux::Config::default()) + .timeout(Duration::from_secs(20)) + .boxed(); + + Swarm::new( + transport, + behaviour_fn(identity), + peer_id, + libp2p_swarm::Config::with_async_std_executor() + .with_idle_connection_timeout(Duration::from_secs(5)), // Some tests need connections to be kept alive beyond what the individual behaviour configures., + ) + } + + #[cfg(feature = "tokio")] + fn new_ephemeral_tokio(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self + where + Self: Sized, + { + use libp2p_core::{transport::MemoryTransport, upgrade::Version, Transport as _}; + use libp2p_identity::Keypair; + + let identity = Keypair::generate_ed25519(); + let peer_id = PeerId::from(identity.public()); + + let transport = MemoryTransport::default() + .or_transport(libp2p_tcp::tokio::Transport::default()) + .upgrade(Version::V1) + .authenticate(libp2p_plaintext::Config::new(&identity)) + .multiplex(libp2p_yamux::Config::default()) .timeout(Duration::from_secs(20)) .boxed(); @@ -219,7 +258,7 @@ where transport, behaviour_fn(identity), peer_id, - swarm::Config::with_async_std_executor() + libp2p_swarm::Config::with_tokio_executor() .with_idle_connection_timeout(Duration::from_secs(5)), // Some tests need connections to be kept alive beyond what the individual behaviour configures., ) } diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 3d8e3981c96..c5d10872d40 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,3 +1,36 @@ +## 0.45.2 + +- Don't report `NewExternalAddrCandidate` for confirmed external addresses. + See [PR 5582](https://github.com/libp2p/rust-libp2p/pull/5582). + +## 0.45.1 + +- Update `libp2p-swarm-derive` to version `0.35.0`, see [PR 5545] + +[PR 5545]: https://github.com/libp2p/rust-libp2p/pull/5545 + +## 0.45.0 + +- Implement refactored `Transport`. + See [PR 4568] +- Move `address_translation` into swarm and into `libp2p-identify`. + See [PR 4568] + +[PR 4568]: https://github.com/libp2p/rust-libp2p/pull/4568 + +## 0.44.3 +- Optimize internal connection `fn poll`. New implementation now scales much better with number of listen protocols active. + No changes to public API introduced. + See [PR 5026](https://github.com/libp2p/rust-libp2p/pull/5026) +- Add peer_id to `FromSwarm::ListenFailure`. + See [PR 4818](https://github.com/libp2p/rust-libp2p/pull/4818). +- Use `web-time` instead of `instant`. + See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). +- Add `#[track_caller]` on all `spawn` wrappers. + See [PR 5465](https://github.com/libp2p/rust-libp2p/pull/5465). +- Add ConnectionError to FromSwarm::ConnectionClosed. + See [PR 5485](https://github.com/libp2p/rust-libp2p/pull/5485). + ## 0.44.2 - Allow `NetworkBehaviour`s to share addresses of peers. @@ -605,7 +638,7 @@ ## 0.34.0 [2022-02-22] - Rename `ProtocolsHandler` to `ConnectionHandler`. Upgrade should be as simple as renaming all - occurences of `ProtocolsHandler` to `ConnectionHandler` with your favorite text manipulation tool + occurrences of `ProtocolsHandler` to `ConnectionHandler` with your favorite text manipulation tool across your codebase. See [PR 2527]. - Fold `libp2p-core`'s `Network` into `Swarm`. See [PR 2492]. diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index 3fdf06aa19f..cdee67f3fb3 100644 --- a/swarm/Cargo.toml +++ b/swarm/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-swarm" edition = "2021" rust-version = { workspace = true } description = "The libp2p swarm" -version = "0.44.2" +version = "0.45.2" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -11,27 +11,27 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] -either = "1.9.0" +either = "1.11.0" fnv = "1.0" -futures = "0.3.30" +futures = { workspace = true } futures-timer = "3.0.3" -getrandom = { version = "0.2.12", features = ["js"], optional = true } # Explicit dependency to be used in `wasm-bindgen` feature -instant = "0.1.12" +getrandom = { version = "0.2.15", features = ["js"], optional = true } # Explicit dependency to be used in `wasm-bindgen` feature +web-time = { workspace = true } libp2p-core = { workspace = true } libp2p-identity = { workspace = true } libp2p-swarm-derive = { workspace = true, optional = true } -lru = "0.12.2" +lru = "0.12.3" multistream-select = { workspace = true } once_cell = "1.19.0" rand = "0.8" -smallvec = "1.13.1" -tracing = "0.1.37" +smallvec = "1.13.2" +tracing = { workspace = true } void = "1" wasm-bindgen-futures = { version = "0.4.42", optional = true } [target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies] async-std = { version = "1.6.2", optional = true } -tokio = { version = "1.36", features = ["rt"], optional = true } +tokio = { workspace = true, features = ["rt"], optional = true } [features] macros = ["dep:libp2p-swarm-derive"] @@ -41,8 +41,8 @@ wasm-bindgen = ["dep:wasm-bindgen-futures", "dep:getrandom"] [dev-dependencies] async-std = { version = "1.6.2", features = ["attributes"] } -either = "1.9.0" -futures = "0.3.30" +either = "1.11.0" +futures = { workspace = true } libp2p-identify = { path = "../protocols/identify" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing. libp2p-identity = { workspace = true, features = ["ed25519"] } libp2p-kad = { path = "../protocols/kad" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing. @@ -52,11 +52,12 @@ libp2p-swarm-derive = { path = "../swarm-derive" } # Using `pat libp2p-swarm-test = { path = "../swarm-test" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing. libp2p-yamux = { path = "../muxers/yamux" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing. quickcheck = { workspace = true } +criterion = { version = "0.5", features = ["async_tokio"] } void = "1" once_cell = "1.19.0" -trybuild = "1.0.89" -tokio = { version = "1.36.0", features = ["time", "rt", "macros", "rt-multi-thread"] } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +trybuild = "1.0.95" +tokio = { workspace = true, features = ["time", "rt", "macros", "rt-multi-thread"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [[test]] name = "swarm_derive" @@ -66,8 +67,10 @@ required-features = ["macros"] # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] + +[[bench]] +name = "connection_handler" +harness = false [lints] workspace = true diff --git a/swarm/benches/connection_handler.rs b/swarm/benches/connection_handler.rs new file mode 100644 index 00000000000..09340421f83 --- /dev/null +++ b/swarm/benches/connection_handler.rs @@ -0,0 +1,360 @@ +use async_std::stream::StreamExt; +use criterion::{criterion_group, criterion_main, Criterion}; +use libp2p_core::{ + transport::MemoryTransport, InboundUpgrade, Multiaddr, OutboundUpgrade, Transport, UpgradeInfo, +}; +use libp2p_identity::PeerId; +use libp2p_swarm::{ConnectionHandler, NetworkBehaviour, StreamProtocol}; +use std::{convert::Infallible, sync::atomic::AtomicUsize}; +use web_time::Duration; + +macro_rules! gen_behaviour { + ($($name:ident {$($field:ident),*};)*) => {$( + #[derive(libp2p_swarm::NetworkBehaviour, Default)] + #[behaviour(prelude = "libp2p_swarm::derive_prelude")] + struct $name { + $($field: SpinningBehaviour,)* + } + + impl BigBehaviour for $name { + fn behaviours(&mut self) -> &mut [SpinningBehaviour] { + unsafe { + std::slice::from_raw_parts_mut( + self as *mut Self as *mut SpinningBehaviour, + std::mem::size_of::() / std::mem::size_of::(), + ) + } + } + } + )*}; +} + +macro_rules! benchmarks { + ($( + $group:ident::[$( + $beh:ident::bench() + .name($name:ident) + .poll_count($count:expr) + .protocols_per_behaviour($protocols:expr), + )+]; + )*) => { + + $( + $( + fn $name(c: &mut Criterion) { + <$beh>::run_bench(c, $protocols, $count, true); + } + )+ + + criterion_group!($group, $($name),*); + )* + + criterion_main!($($group),*); + }; +} + +// fans go brrr +gen_behaviour! { + SpinningBehaviour5 { a, b, c, d, e }; + SpinningBehaviour10 { a, b, c, d, e, f, g, h, i, j }; + SpinningBehaviour20 { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u }; +} + +benchmarks! { + singles::[ + SpinningBehaviour::bench().name(b).poll_count(1000).protocols_per_behaviour(10), + SpinningBehaviour::bench().name(c).poll_count(1000).protocols_per_behaviour(100), + SpinningBehaviour::bench().name(d).poll_count(1000).protocols_per_behaviour(1000), + ]; + big_5::[ + SpinningBehaviour5::bench().name(e).poll_count(1000).protocols_per_behaviour(2), + SpinningBehaviour5::bench().name(f).poll_count(1000).protocols_per_behaviour(20), + SpinningBehaviour5::bench().name(g).poll_count(1000).protocols_per_behaviour(200), + ]; + top_10::[ + SpinningBehaviour10::bench().name(h).poll_count(1000).protocols_per_behaviour(1), + SpinningBehaviour10::bench().name(i).poll_count(1000).protocols_per_behaviour(10), + SpinningBehaviour10::bench().name(j).poll_count(1000).protocols_per_behaviour(100), + ]; + lucky_20::[ + SpinningBehaviour20::bench().name(k).poll_count(500).protocols_per_behaviour(1), + SpinningBehaviour20::bench().name(l).poll_count(500).protocols_per_behaviour(10), + SpinningBehaviour20::bench().name(m).poll_count(500).protocols_per_behaviour(100), + ]; +} +//fn main() {} + +trait BigBehaviour: Sized { + fn behaviours(&mut self) -> &mut [SpinningBehaviour]; + + fn for_each_beh(&mut self, f: impl FnMut(&mut SpinningBehaviour)) { + self.behaviours().iter_mut().for_each(f); + } + + fn any_beh(&mut self, f: impl FnMut(&mut SpinningBehaviour) -> bool) -> bool { + self.behaviours().iter_mut().any(f) + } + + fn run_bench( + c: &mut Criterion, + protocols_per_behaviour: usize, + spam_count: usize, + static_protocols: bool, + ) where + Self: Default + NetworkBehaviour, + { + let name = format!( + "{}::bench().poll_count({}).protocols_per_behaviour({})", + std::any::type_name::(), + spam_count, + protocols_per_behaviour + ); + + let init = || { + let mut swarm_a = new_swarm(Self::default()); + let mut swarm_b = new_swarm(Self::default()); + + let behaviour_count = swarm_a.behaviours().len(); + let protocol_count = behaviour_count * protocols_per_behaviour; + let protocols = (0..protocol_count) + .map(|i| { + if static_protocols { + StreamProtocol::new(format!("/protocol/{i}").leak()) + } else { + StreamProtocol::try_from_owned(format!("/protocol/{i}")).unwrap() + } + }) + .collect::>() + .leak(); + + let mut protocol_chunks = protocols.chunks(protocols_per_behaviour); + swarm_a.for_each_beh(|b| b.protocols = protocol_chunks.next().unwrap()); + let mut protocol_chunks = protocols.chunks(protocols_per_behaviour); + swarm_b.for_each_beh(|b| b.protocols = protocol_chunks.next().unwrap()); + + swarm_a.for_each_beh(|b| b.iter_count = spam_count); + swarm_b.for_each_beh(|b| b.iter_count = 0); + + swarm_a.for_each_beh(|b| b.other_peer = Some(*swarm_b.local_peer_id())); + swarm_b.for_each_beh(|b| b.other_peer = Some(*swarm_a.local_peer_id())); + + static OFFSET: AtomicUsize = AtomicUsize::new(8000); + let offset = OFFSET.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + + swarm_a + .listen_on(format!("/memory/{offset}").parse().unwrap()) + .unwrap(); + swarm_b + .dial(format!("/memory/{offset}").parse::().unwrap()) + .unwrap(); + + (swarm_a, swarm_b) + }; + + c.bench_function(&name, |b| { + b.to_async(tokio::runtime::Builder::new_multi_thread().build().unwrap()) + .iter_batched( + init, + |(mut swarm_a, mut swarm_b)| async move { + while swarm_a.any_beh(|b| !b.finished) || swarm_b.any_beh(|b| !b.finished) { + futures::future::select(swarm_b.next(), swarm_a.next()).await; + } + }, + criterion::BatchSize::LargeInput, + ); + }); + } +} + +impl BigBehaviour for libp2p_swarm::Swarm { + fn behaviours(&mut self) -> &mut [SpinningBehaviour] { + self.behaviour_mut().behaviours() + } +} + +fn new_swarm(beh: T) -> libp2p_swarm::Swarm { + let keypair = libp2p_identity::Keypair::generate_ed25519(); + libp2p_swarm::Swarm::new( + MemoryTransport::default() + .upgrade(multistream_select::Version::V1) + .authenticate(libp2p_plaintext::Config::new(&keypair)) + .multiplex(libp2p_yamux::Config::default()) + .boxed(), + beh, + keypair.public().to_peer_id(), + libp2p_swarm::Config::without_executor().with_idle_connection_timeout(Duration::MAX), + ) +} + +/// Whole purpose of the behaviour is to rapidly call `poll` on the handler +/// configured amount of times and then emit event when finished. +#[derive(Default)] +struct SpinningBehaviour { + iter_count: usize, + protocols: &'static [StreamProtocol], + finished: bool, + emitted: bool, + other_peer: Option, +} + +#[derive(Debug)] +struct FinishedSpinning; + +impl NetworkBehaviour for SpinningBehaviour { + type ConnectionHandler = SpinningHandler; + type ToSwarm = FinishedSpinning; + + fn handle_established_inbound_connection( + &mut self, + _connection_id: libp2p_swarm::ConnectionId, + _peer: libp2p_identity::PeerId, + _local_addr: &libp2p_core::Multiaddr, + _remote_addr: &libp2p_core::Multiaddr, + ) -> Result, libp2p_swarm::ConnectionDenied> { + Ok(SpinningHandler { + iter_count: 0, + protocols: self.protocols, + }) + } + + fn handle_established_outbound_connection( + &mut self, + _connection_id: libp2p_swarm::ConnectionId, + _peer: libp2p_identity::PeerId, + _addr: &libp2p_core::Multiaddr, + _role_override: libp2p_core::Endpoint, + _port_use: libp2p_core::transport::PortUse, + ) -> Result, libp2p_swarm::ConnectionDenied> { + Ok(SpinningHandler { + iter_count: self.iter_count, + protocols: self.protocols, + }) + } + + fn on_swarm_event(&mut self, _: libp2p_swarm::FromSwarm) {} + + fn on_connection_handler_event( + &mut self, + _peer_id: libp2p_identity::PeerId, + _connection_id: libp2p_swarm::ConnectionId, + _event: libp2p_swarm::THandlerOutEvent, + ) { + self.finished = true; + } + + fn poll( + &mut self, + _: &mut std::task::Context<'_>, + ) -> std::task::Poll>> + { + if self.finished && !self.emitted { + self.emitted = true; + std::task::Poll::Ready(libp2p_swarm::ToSwarm::GenerateEvent(FinishedSpinning)) + } else { + std::task::Poll::Pending + } + } +} + +impl BigBehaviour for SpinningBehaviour { + fn behaviours(&mut self) -> &mut [SpinningBehaviour] { + std::slice::from_mut(self) + } +} + +struct SpinningHandler { + iter_count: usize, + protocols: &'static [StreamProtocol], +} + +impl ConnectionHandler for SpinningHandler { + type FromBehaviour = Infallible; + + type ToBehaviour = FinishedSpinning; + + type InboundProtocol = Upgrade; + + type OutboundProtocol = Upgrade; + + type InboundOpenInfo = (); + + type OutboundOpenInfo = (); + + fn listen_protocol( + &self, + ) -> libp2p_swarm::SubstreamProtocol { + libp2p_swarm::SubstreamProtocol::new(Upgrade(self.protocols), ()) + } + + fn poll( + &mut self, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll< + libp2p_swarm::ConnectionHandlerEvent< + Self::OutboundProtocol, + Self::OutboundOpenInfo, + Self::ToBehaviour, + >, + > { + if self.iter_count == usize::MAX { + return std::task::Poll::Pending; + } + + if self.iter_count != 0 { + self.iter_count -= 1; + cx.waker().wake_by_ref(); + return std::task::Poll::Pending; + } + + self.iter_count = usize::MAX; + std::task::Poll::Ready(libp2p_swarm::ConnectionHandlerEvent::NotifyBehaviour( + FinishedSpinning, + )) + } + + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { + match event {} + } + + fn on_connection_event( + &mut self, + _event: libp2p_swarm::handler::ConnectionEvent< + Self::InboundProtocol, + Self::OutboundProtocol, + Self::InboundOpenInfo, + Self::OutboundOpenInfo, + >, + ) { + } +} + +pub struct Upgrade(&'static [StreamProtocol]); + +impl UpgradeInfo for Upgrade { + type Info = &'static StreamProtocol; + type InfoIter = std::slice::Iter<'static, StreamProtocol>; + + fn protocol_info(&self) -> Self::InfoIter { + self.0.iter() + } +} + +impl OutboundUpgrade for Upgrade { + type Output = libp2p_swarm::Stream; + type Error = Infallible; + type Future = futures::future::Ready>; + + fn upgrade_outbound(self, s: libp2p_swarm::Stream, _: Self::Info) -> Self::Future { + futures::future::ready(Ok(s)) + } +} + +impl InboundUpgrade for Upgrade { + type Output = libp2p_swarm::Stream; + type Error = Infallible; + type Future = futures::future::Ready>; + + fn upgrade_inbound(self, s: libp2p_swarm::Stream, _: Self::Info) -> Self::Future { + futures::future::ready(Ok(s)) + } +} diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 5070871a4c1..35aed12fba5 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -32,10 +32,13 @@ use crate::connection::ConnectionId; use crate::dial_opts::DialOpts; use crate::listen_opts::ListenOpts; use crate::{ - ConnectionDenied, ConnectionHandler, DialError, ListenError, THandler, THandlerInEvent, - THandlerOutEvent, + ConnectionDenied, ConnectionError, ConnectionHandler, DialError, ListenError, THandler, + THandlerInEvent, THandlerOutEvent, +}; +use libp2p_core::{ + transport::{ListenerId, PortUse}, + ConnectedPoint, Endpoint, Multiaddr, }; -use libp2p_core::{transport::ListenerId, ConnectedPoint, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use std::{task::Context, task::Poll}; @@ -148,6 +151,9 @@ pub trait NetworkBehaviour: 'static { /// At this point, we have verified their [`PeerId`] and we know, which particular [`Multiaddr`] succeeded in the dial. /// In order to actually use this connection, this function must return a [`ConnectionHandler`]. /// Returning an error will immediately close the connection. + /// + /// Note when any composed behaviour returns an error the connection will be closed and a + /// [`FromSwarm::ListenFailure`] event will be emitted. fn handle_established_inbound_connection( &mut self, _connection_id: ConnectionId, @@ -184,12 +190,16 @@ pub trait NetworkBehaviour: 'static { /// At this point, we have verified their [`PeerId`] and we know, which particular [`Multiaddr`] succeeded in the dial. /// In order to actually use this connection, this function must return a [`ConnectionHandler`]. /// Returning an error will immediately close the connection. + /// + /// Note when any composed behaviour returns an error the connection will be closed and a + /// [`FromSwarm::DialFailure`] event will be emitted. fn handle_established_outbound_connection( &mut self, _connection_id: ConnectionId, peer: PeerId, addr: &Multiaddr, role_override: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied>; /// Informs the behaviour about an event from the [`Swarm`](crate::Swarm). @@ -269,7 +279,7 @@ pub enum ToSwarm { /// The emphasis on a **new** candidate is important. /// Protocols MUST take care to only emit a candidate once per "source". /// For example, the observed address of a TCP connection does not change throughout its lifetime. - /// Thus, only one candidate should be emitted per connection. + /// Thus, only one candidate should be emitted per connection. /// /// This makes the report frequency of an address a meaningful data-point for consumers of this event. /// This address will be shared with all [`NetworkBehaviour`]s via [`FromSwarm::NewExternalAddrCandidate`]. @@ -475,6 +485,7 @@ pub struct ConnectionClosed<'a> { pub peer_id: PeerId, pub connection_id: ConnectionId, pub endpoint: &'a ConnectedPoint, + pub cause: Option<&'a ConnectionError>, pub remaining_established: usize, } @@ -508,6 +519,7 @@ pub struct ListenFailure<'a> { pub send_back_addr: &'a Multiaddr, pub error: &'a ListenError, pub connection_id: ConnectionId, + pub peer_id: Option, } /// [`FromSwarm`] variant that informs the behaviour that a new listener was created. diff --git a/swarm/src/behaviour/either.rs b/swarm/src/behaviour/either.rs index 25da83fa11f..7a51303e74d 100644 --- a/swarm/src/behaviour/either.rs +++ b/swarm/src/behaviour/either.rs @@ -22,6 +22,7 @@ use crate::behaviour::{self, NetworkBehaviour, ToSwarm}; use crate::connection::ConnectionId; use crate::{ConnectionDenied, THandler, THandlerInEvent, THandlerOutEvent}; use either::Either; +use libp2p_core::transport::PortUse; use libp2p_core::{Endpoint, Multiaddr}; use libp2p_identity::PeerId; use std::{task::Context, task::Poll}; @@ -103,6 +104,7 @@ where peer: PeerId, addr: &Multiaddr, role_override: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied> { let handler = match self { Either::Left(inner) => Either::Left(inner.handle_established_outbound_connection( @@ -110,12 +112,14 @@ where peer, addr, role_override, + port_use, )?), Either::Right(inner) => Either::Right(inner.handle_established_outbound_connection( connection_id, peer, addr, role_override, + port_use, )?), }; diff --git a/swarm/src/behaviour/peer_addresses.rs b/swarm/src/behaviour/peer_addresses.rs index a011867dcdf..1eeead56ca1 100644 --- a/swarm/src/behaviour/peer_addresses.rs +++ b/swarm/src/behaviour/peer_addresses.rs @@ -101,7 +101,7 @@ mod tests { use super::*; use std::io; - use crate::{ConnectionId, DialError}; + use crate::ConnectionId; use libp2p_core::{ multiaddr::Protocol, transport::{memory::MemoryTransportError, TransportError}, diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index e81c5343701..5d72534c91e 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -30,6 +30,7 @@ use crate::{ }; use either::Either; use futures::future; +use libp2p_core::transport::PortUse; use libp2p_core::{upgrade::DeniedUpgrade, Endpoint, Multiaddr}; use libp2p_identity::PeerId; use std::{task::Context, task::Poll}; @@ -139,6 +140,7 @@ where peer: PeerId, addr: &Multiaddr, role_override: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied> { let inner = match self.inner.as_mut() { None => return Ok(ToggleConnectionHandler { inner: None }), @@ -150,6 +152,7 @@ where peer, addr, role_override, + port_use, )?; Ok(ToggleConnectionHandler { @@ -207,6 +210,8 @@ where ) { let out = match out { future::Either::Left(out) => out, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] future::Either::Right(v) => void::unreachable(v), }; @@ -248,6 +253,8 @@ where let err = match err { Either::Left(e) => e, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right(v) => void::unreachable(v), }; diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index 15c49bb7bd5..2f9afc38418 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -27,12 +27,12 @@ pub use error::ConnectionError; pub(crate) use error::{ PendingConnectionError, PendingInboundConnectionError, PendingOutboundConnectionError, }; +use libp2p_core::transport::PortUse; pub use supported_protocols::SupportedProtocols; use crate::handler::{ AddressChange, ConnectionEvent, ConnectionHandler, DialUpgradeError, FullyNegotiatedInbound, - FullyNegotiatedOutbound, ListenUpgradeError, ProtocolSupport, ProtocolsAdded, ProtocolsChange, - UpgradeInfoSend, + FullyNegotiatedOutbound, ListenUpgradeError, ProtocolSupport, ProtocolsChange, UpgradeInfoSend, }; use crate::stream::ActiveStreamCounter; use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend}; @@ -44,7 +44,6 @@ use futures::stream::FuturesUnordered; use futures::StreamExt; use futures::{stream, FutureExt}; use futures_timer::Delay; -use instant::Instant; use libp2p_core::connection::ConnectedPoint; use libp2p_core::multiaddr::Multiaddr; use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerEvent, StreamMuxerExt, SubstreamBox}; @@ -52,13 +51,14 @@ use libp2p_core::upgrade; use libp2p_core::upgrade::{NegotiationError, ProtocolError}; use libp2p_core::Endpoint; use libp2p_identity::PeerId; -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::fmt::{Display, Formatter}; use std::future::Future; use std::sync::atomic::{AtomicUsize, Ordering}; use std::task::Waker; use std::time::Duration; use std::{fmt, io, mem, pin::Pin, task::Context, task::Poll}; +use web_time::Instant; static NEXT_CONNECTION_ID: AtomicUsize = AtomicUsize::new(1); @@ -153,8 +153,11 @@ where SubstreamRequested, >, - local_supported_protocols: HashSet, + local_supported_protocols: + HashMap::Info>, bool>, remote_supported_protocols: HashSet, + protocol_buffer: Vec, + idle_timeout: Duration, stream_counter: ActiveStreamCounter, } @@ -187,11 +190,17 @@ where idle_timeout: Duration, ) -> Self { let initial_protocols = gather_supported_protocols(&handler); + let mut buffer = Vec::new(); + if !initial_protocols.is_empty() { handler.on_connection_event(ConnectionEvent::LocalProtocolsChange( - ProtocolsChange::Added(ProtocolsAdded::from_set(&initial_protocols)), + ProtocolsChange::from_initial_protocols( + initial_protocols.keys().map(|e| &e.0), + &mut buffer, + ), )); } + Connection { muxing: muxer, handler, @@ -203,6 +212,7 @@ where requested_substreams: Default::default(), local_supported_protocols: initial_protocols, remote_supported_protocols: Default::default(), + protocol_buffer: buffer, idle_timeout, stream_counter: ActiveStreamCounter::default(), } @@ -250,6 +260,7 @@ where substream_upgrade_protocol_override, local_supported_protocols: supported_protocols, remote_supported_protocols, + protocol_buffer, idle_timeout, stream_counter, .. @@ -287,25 +298,24 @@ where ProtocolSupport::Added(protocols), )) => { if let Some(added) = - ProtocolsChange::add(remote_supported_protocols, &protocols) + ProtocolsChange::add(remote_supported_protocols, protocols, protocol_buffer) { handler.on_connection_event(ConnectionEvent::RemoteProtocolsChange(added)); - remote_supported_protocols.extend(protocols); + remote_supported_protocols.extend(protocol_buffer.drain(..)); } - continue; } Poll::Ready(ConnectionHandlerEvent::ReportRemoteProtocols( ProtocolSupport::Removed(protocols), )) => { - if let Some(removed) = - ProtocolsChange::remove(remote_supported_protocols, &protocols) - { + if let Some(removed) = ProtocolsChange::remove( + remote_supported_protocols, + protocols, + protocol_buffer, + ) { handler .on_connection_event(ConnectionEvent::RemoteProtocolsChange(removed)); - remote_supported_protocols.retain(|p| !protocols.contains(p)); } - continue; } } @@ -373,7 +383,7 @@ where match shutdown { Shutdown::None => {} Shutdown::Asap => return Poll::Ready(Err(ConnectionError::KeepAliveTimeout)), - Shutdown::Later(delay, _) => match Future::poll(Pin::new(delay), cx) { + Shutdown::Later(delay) => match Future::poll(Pin::new(delay), cx) { Poll::Ready(_) => { return Poll::Ready(Err(ConnectionError::KeepAliveTimeout)) } @@ -431,16 +441,16 @@ where } } - let new_protocols = gather_supported_protocols(handler); - let changes = ProtocolsChange::from_full_sets(supported_protocols, &new_protocols); + let changes = ProtocolsChange::from_full_sets( + supported_protocols, + handler.listen_protocol().upgrade().protocol_info(), + protocol_buffer, + ); if !changes.is_empty() { for change in changes { handler.on_connection_event(ConnectionEvent::LocalProtocolsChange(change)); } - - *supported_protocols = new_protocols; - continue; // Go back to the top, handler can potentially make progress again. } @@ -454,12 +464,14 @@ where } } -fn gather_supported_protocols(handler: &impl ConnectionHandler) -> HashSet { +fn gather_supported_protocols( + handler: &C, +) -> HashMap::Info>, bool> { handler .listen_protocol() .upgrade() .protocol_info() - .filter_map(|i| StreamProtocol::try_from_owned(i.as_ref().to_owned()).ok()) + .map(|info| (AsStrHashEq(info), true)) .collect() } @@ -470,15 +482,12 @@ fn compute_new_shutdown( ) -> Option { match (current_shutdown, handler_keep_alive) { (_, false) if idle_timeout == Duration::ZERO => Some(Shutdown::Asap), - (Shutdown::Later(_, _), false) => None, // Do nothing, i.e. let the shutdown timer continue to tick. + (Shutdown::Later(_), false) => None, // Do nothing, i.e. let the shutdown timer continue to tick. (_, false) => { let now = Instant::now(); let safe_keep_alive = checked_add_fraction(now, idle_timeout); - Some(Shutdown::Later( - Delay::new(safe_keep_alive), - now + safe_keep_alive, - )) + Some(Shutdown::Later(Delay::new(safe_keep_alive))) } (_, true) => Some(Shutdown::None), } @@ -720,7 +729,7 @@ impl Future for SubstreamRequested { /// The options for a planned connection & handler shutdown. /// -/// A shutdown is planned anew based on the the return value of +/// A shutdown is planned anew based on the return value of /// [`ConnectionHandler::connection_keep_alive`] of the underlying handler /// after every invocation of [`ConnectionHandler::poll`]. /// @@ -734,7 +743,26 @@ enum Shutdown { /// A shut down is planned as soon as possible. Asap, /// A shut down is planned for when a `Delay` has elapsed. - Later(Delay, Instant), + Later(Delay), +} + +// Structure used to avoid allocations when storing the protocols in the `HashMap. +// Instead of allocating a new `String` for the key, +// we use `T::as_ref()` in `Hash`, `Eq` and `PartialEq` requirements. +pub(crate) struct AsStrHashEq(pub(crate) T); + +impl> Eq for AsStrHashEq {} + +impl> PartialEq for AsStrHashEq { + fn eq(&self, other: &Self) -> bool { + self.0.as_ref() == other.0.as_ref() + } +} + +impl> std::hash::Hash for AsStrHashEq { + fn hash(&self, state: &mut H) { + self.0.as_ref().hash(state) + } } #[cfg(test)] @@ -947,11 +975,10 @@ mod tests { let shutdown = match self.0 { Shutdown::None => Shutdown::None, Shutdown::Asap => Shutdown::Asap, - Shutdown::Later(_, instant) => Shutdown::Later( + Shutdown::Later(_) => Shutdown::Later( // compute_new_shutdown does not touch the delay. Delay does not // implement Clone. Thus use a placeholder delay. Delay::new(Duration::from_secs(1)), - instant, ), }; @@ -964,12 +991,7 @@ mod tests { let shutdown = match g.gen_range(1u8..4) { 1 => Shutdown::None, 2 => Shutdown::Asap, - 3 => Shutdown::Later( - Delay::new(Duration::from_secs(u32::arbitrary(g) as u64)), - Instant::now() - .checked_add(Duration::arbitrary(g)) - .unwrap_or(Instant::now()), - ), + 3 => Shutdown::Later(Delay::new(Duration::from_secs(u32::arbitrary(g) as u64))), _ => unreachable!(), }; @@ -1000,7 +1022,9 @@ mod tests { self: Pin<&mut Self>, _: &mut Context<'_>, ) -> Poll> { - Poll::Ready(Ok(PendingSubstream(Arc::downgrade(&self.counter)))) + Poll::Ready(Ok(PendingSubstream { + _weak: Arc::downgrade(&self.counter), + })) } fn poll_outbound( @@ -1055,7 +1079,9 @@ mod tests { } } - struct PendingSubstream(Weak<()>); + struct PendingSubstream { + _weak: Weak<()>, + } impl AsyncRead for PendingSubstream { fn poll_read( @@ -1163,10 +1189,14 @@ mod tests { >, ) { match event { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, .. }) => void::unreachable(protocol), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { protocol, .. @@ -1174,6 +1204,8 @@ mod tests { ConnectionEvent::DialUpgradeError(DialUpgradeError { error, .. }) => { self.error = Some(error) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::AddressChange(_) | ConnectionEvent::ListenUpgradeError(_) | ConnectionEvent::LocalProtocolsChange(_) @@ -1182,6 +1214,8 @@ mod tests { } fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -1257,6 +1291,8 @@ mod tests { } fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -1327,6 +1363,7 @@ enum PendingPoint { Dialer { /// Same as [`ConnectedPoint::Dialer`] `role_override`. role_override: Endpoint, + port_use: PortUse, }, /// The socket comes from a listener. Listener { @@ -1340,7 +1377,14 @@ enum PendingPoint { impl From for PendingPoint { fn from(endpoint: ConnectedPoint) -> Self { match endpoint { - ConnectedPoint::Dialer { role_override, .. } => PendingPoint::Dialer { role_override }, + ConnectedPoint::Dialer { + role_override, + port_use, + .. + } => PendingPoint::Dialer { + role_override, + port_use, + }, ConnectedPoint::Listener { local_addr, send_back_addr, diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index 9bcd1b446d3..07f6968dec9 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -37,12 +37,12 @@ use futures::{ ready, stream::FuturesUnordered, }; -use instant::{Duration, Instant}; use libp2p_core::connection::Endpoint; use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerExt}; +use libp2p_core::transport::PortUse; use std::task::Waker; use std::{ - collections::{hash_map, HashMap}, + collections::HashMap, fmt, num::{NonZeroU8, NonZeroUsize}, pin::Pin, @@ -51,6 +51,7 @@ use std::{ }; use tracing::Instrument; use void::Void; +use web_time::{Duration, Instant}; mod concurrent_dial; mod task; @@ -70,6 +71,7 @@ impl ExecSwitch { } } + #[track_caller] fn spawn(&mut self, task: impl Future + Send + 'static) { let task = task.boxed(); @@ -423,6 +425,7 @@ where >, peer: Option, role_override: Endpoint, + port_use: PortUse, dial_concurrency_factor_override: Option, connection_id: ConnectionId, ) { @@ -443,7 +446,10 @@ where .instrument(span), ); - let endpoint = PendingPoint::Dialer { role_override }; + let endpoint = PendingPoint::Dialer { + role_override, + port_use, + }; self.counters.inc_pending(&endpoint); self.pending.insert( @@ -649,10 +655,17 @@ where self.counters.dec_pending(&endpoint); let (endpoint, concurrent_dial_errors) = match (endpoint, outgoing) { - (PendingPoint::Dialer { role_override }, Some((address, errors))) => ( + ( + PendingPoint::Dialer { + role_override, + port_use, + }, + Some((address, errors)), + ) => ( ConnectedPoint::Dialer { address, role_override, + port_use, }, Some(errors), ), @@ -1029,28 +1042,3 @@ impl PoolConfig { self } } - -trait EntryExt<'a, K, V> { - fn expect_occupied(self, msg: &'static str) -> hash_map::OccupiedEntry<'a, K, V>; -} - -impl<'a, K: 'a, V: 'a> EntryExt<'a, K, V> for hash_map::Entry<'a, K, V> { - fn expect_occupied(self, msg: &'static str) -> hash_map::OccupiedEntry<'a, K, V> { - match self { - hash_map::Entry::Occupied(entry) => entry, - hash_map::Entry::Vacant(_) => panic!("{}", msg), - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use futures::future::Future; - - struct Dummy; - - impl Executor for Dummy { - fn exec(&self, _: Pin + Send>>) {} - } -} diff --git a/swarm/src/connection/pool/task.rs b/swarm/src/connection/pool/task.rs index 08674fd2ee5..13977a17b85 100644 --- a/swarm/src/connection/pool/task.rs +++ b/swarm/src/connection/pool/task.rs @@ -105,6 +105,8 @@ pub(crate) async fn new_for_pending_outgoing_connection( }) .await; } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Left((Ok(v), _)) => void::unreachable(v), Either::Right((Ok((address, output, errors)), _)) => { let _ = events @@ -143,6 +145,8 @@ pub(crate) async fn new_for_pending_incoming_connection( }) .await; } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Left((Ok(v), _)) => void::unreachable(v), Either::Right((Ok(output), _)) => { let _ = events diff --git a/swarm/src/connection/supported_protocols.rs b/swarm/src/connection/supported_protocols.rs index 0575046bb44..124ec93d669 100644 --- a/swarm/src/connection/supported_protocols.rs +++ b/swarm/src/connection/supported_protocols.rs @@ -40,7 +40,6 @@ impl SupportedProtocols { mod tests { use super::*; use crate::handler::{ProtocolsAdded, ProtocolsRemoved}; - use once_cell::sync::Lazy; #[test] fn protocols_change_added_returns_correct_changed_value() { @@ -70,19 +69,24 @@ mod tests { } fn add_foo() -> ProtocolsChange<'static> { - ProtocolsChange::Added(ProtocolsAdded::from_set(&FOO_PROTOCOLS)) + ProtocolsChange::Added(ProtocolsAdded { + protocols: FOO_PROTOCOLS.iter(), + }) } fn add_foo_bar() -> ProtocolsChange<'static> { - ProtocolsChange::Added(ProtocolsAdded::from_set(&FOO_BAR_PROTOCOLS)) + ProtocolsChange::Added(ProtocolsAdded { + protocols: FOO_BAR_PROTOCOLS.iter(), + }) } fn remove_foo() -> ProtocolsChange<'static> { - ProtocolsChange::Removed(ProtocolsRemoved::from_set(&FOO_PROTOCOLS)) + ProtocolsChange::Removed(ProtocolsRemoved { + protocols: FOO_PROTOCOLS.iter(), + }) } - static FOO_PROTOCOLS: Lazy> = - Lazy::new(|| HashSet::from([StreamProtocol::new("/foo")])); - static FOO_BAR_PROTOCOLS: Lazy> = - Lazy::new(|| HashSet::from([StreamProtocol::new("/foo"), StreamProtocol::new("/bar")])); + static FOO_PROTOCOLS: &[StreamProtocol] = &[StreamProtocol::new("/foo")]; + static FOO_BAR_PROTOCOLS: &[StreamProtocol] = + &[StreamProtocol::new("/foo"), StreamProtocol::new("/bar")]; } diff --git a/swarm/src/dial_opts.rs b/swarm/src/dial_opts.rs index 4442d913847..4f5b621327c 100644 --- a/swarm/src/dial_opts.rs +++ b/swarm/src/dial_opts.rs @@ -22,10 +22,38 @@ use crate::ConnectionId; use libp2p_core::connection::Endpoint; use libp2p_core::multiaddr::Protocol; +use libp2p_core::transport::PortUse; use libp2p_core::Multiaddr; use libp2p_identity::PeerId; use std::num::NonZeroU8; +macro_rules! fn_override_role { + () => { + /// Override role of local node on connection. I.e. execute the dial _as a + /// listener_. + /// + /// See + /// [`ConnectedPoint::Dialer`](libp2p_core::connection::ConnectedPoint::Dialer) + /// for details. + pub fn override_role(mut self) -> Self { + self.role_override = Endpoint::Listener; + self + } + }; +} + +macro_rules! fn_allocate_new_port { + () => { + /// Enforce the allocation of a new port. + /// Default behaviour is best effort reuse of existing ports. If there is no existing + /// fitting listener, a new port is allocated. + pub fn allocate_new_port(mut self) -> Self { + self.port_use = PortUse::New; + self + } + }; +} + /// Options to configure a dial to a known or unknown peer. /// /// Used in [`Swarm::dial`](crate::Swarm::dial) and @@ -45,6 +73,7 @@ pub struct DialOpts { role_override: Endpoint, dial_concurrency_factor_override: Option, connection_id: ConnectionId, + port_use: PortUse, } impl DialOpts { @@ -65,6 +94,7 @@ impl DialOpts { condition: Default::default(), role_override: Endpoint::Dialer, dial_concurrency_factor_override: Default::default(), + port_use: PortUse::Reuse, } } @@ -124,6 +154,10 @@ impl DialOpts { pub(crate) fn role_override(&self) -> Endpoint { self.role_override } + + pub(crate) fn port_use(&self) -> PortUse { + self.port_use + } } impl From for DialOpts { @@ -144,6 +178,7 @@ pub struct WithPeerId { condition: PeerCondition, role_override: Endpoint, dial_concurrency_factor_override: Option, + port_use: PortUse, } impl WithPeerId { @@ -169,19 +204,12 @@ impl WithPeerId { extend_addresses_through_behaviour: false, role_override: self.role_override, dial_concurrency_factor_override: self.dial_concurrency_factor_override, + port_use: self.port_use, } } - /// Override role of local node on connection. I.e. execute the dial _as a - /// listener_. - /// - /// See - /// [`ConnectedPoint::Dialer`](libp2p_core::connection::ConnectedPoint::Dialer) - /// for details. - pub fn override_role(mut self) -> Self { - self.role_override = Endpoint::Listener; - self - } + fn_override_role!(); + fn_allocate_new_port!(); /// Build the final [`DialOpts`]. pub fn build(self) -> DialOpts { @@ -193,6 +221,7 @@ impl WithPeerId { role_override: self.role_override, dial_concurrency_factor_override: self.dial_concurrency_factor_override, connection_id: ConnectionId::next(), + port_use: self.port_use, } } } @@ -205,6 +234,7 @@ pub struct WithPeerIdWithAddresses { extend_addresses_through_behaviour: bool, role_override: Endpoint, dial_concurrency_factor_override: Option, + port_use: PortUse, } impl WithPeerIdWithAddresses { @@ -221,16 +251,8 @@ impl WithPeerIdWithAddresses { self } - /// Override role of local node on connection. I.e. execute the dial _as a - /// listener_. - /// - /// See - /// [`ConnectedPoint::Dialer`](libp2p_core::connection::ConnectedPoint::Dialer) - /// for details. - pub fn override_role(mut self) -> Self { - self.role_override = Endpoint::Listener; - self - } + fn_override_role!(); + fn_allocate_new_port!(); /// Override /// Number of addresses concurrently dialed for a single outbound connection attempt. @@ -249,6 +271,7 @@ impl WithPeerIdWithAddresses { role_override: self.role_override, dial_concurrency_factor_override: self.dial_concurrency_factor_override, connection_id: ConnectionId::next(), + port_use: self.port_use, } } } @@ -262,6 +285,7 @@ impl WithoutPeerId { WithoutPeerIdWithAddress { address, role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, } } } @@ -270,19 +294,13 @@ impl WithoutPeerId { pub struct WithoutPeerIdWithAddress { address: Multiaddr, role_override: Endpoint, + port_use: PortUse, } impl WithoutPeerIdWithAddress { - /// Override role of local node on connection. I.e. execute the dial _as a - /// listener_. - /// - /// See - /// [`ConnectedPoint::Dialer`](libp2p_core::connection::ConnectedPoint::Dialer) - /// for details. - pub fn override_role(mut self) -> Self { - self.role_override = Endpoint::Listener; - self - } + fn_override_role!(); + fn_allocate_new_port!(); + /// Build the final [`DialOpts`]. pub fn build(self) -> DialOpts { DialOpts { @@ -293,6 +311,7 @@ impl WithoutPeerIdWithAddress { role_override: self.role_override, dial_concurrency_factor_override: None, connection_id: ConnectionId::next(), + port_use: self.port_use, } } } diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index 86df676443b..0bd8c06862d 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -7,6 +7,7 @@ use crate::{ ConnectionDenied, ConnectionHandlerEvent, StreamUpgradeError, SubstreamProtocol, THandler, THandlerInEvent, THandlerOutEvent, }; +use libp2p_core::transport::PortUse; use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::Endpoint; use libp2p_core::Multiaddr; @@ -37,6 +38,7 @@ impl NetworkBehaviour for Behaviour { _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(ConnectionHandler) } @@ -47,6 +49,8 @@ impl NetworkBehaviour for Behaviour { _: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -74,6 +78,8 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { } fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -96,19 +102,29 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { >, ) { match event { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, .. }) => void::unreachable(protocol), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { protocol, .. }) => void::unreachable(protocol), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::DialUpgradeError(DialUpgradeError { info: _, error }) => match error { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Timeout => unreachable!(), StreamUpgradeError::Apply(e) => void::unreachable(e), StreamUpgradeError::NegotiationFailed | StreamUpgradeError::Io(_) => { unreachable!("Denied upgrade does not support any protocols") } }, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::AddressChange(_) | ConnectionEvent::ListenUpgradeError(_) | ConnectionEvent::LocalProtocolsChange(_) diff --git a/swarm/src/executor.rs b/swarm/src/executor.rs index e949bf3cbde..a2abbbde6ef 100644 --- a/swarm/src/executor.rs +++ b/swarm/src/executor.rs @@ -11,6 +11,7 @@ use std::{future::Future, pin::Pin}; /// > about running `Future`s on a separate task. pub trait Executor { /// Run the given future in the background until it ends. + #[track_caller] fn exec(&self, future: Pin + Send>>); } diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 31d2c91e391..610b95b8cf1 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -46,22 +46,19 @@ mod one_shot; mod pending; mod select; +use crate::connection::AsStrHashEq; pub use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, SendWrapper, UpgradeInfoSend}; pub use map_in::MapInEvent; pub use map_out::MapOutEvent; pub use one_shot::{OneShotHandler, OneShotHandlerConfig}; pub use pending::PendingConnectionHandler; pub use select::ConnectionHandlerSelect; +use smallvec::SmallVec; use crate::StreamProtocol; -use ::either::Either; +use core::slice; use libp2p_core::Multiaddr; -use once_cell::sync::Lazy; -use smallvec::SmallVec; -use std::collections::hash_map::RandomState; -use std::collections::hash_set::{Difference, Intersection}; -use std::collections::HashSet; -use std::iter::Peekable; +use std::collections::{HashMap, HashSet}; use std::{error, fmt, io, task::Context, task::Poll, time::Duration}; /// A handler for a set of protocols used on a connection with a remote. @@ -137,6 +134,7 @@ pub trait ConnectionHandler: Send + 'static { /// /// - Protocols like [circuit-relay v2](https://github.com/libp2p/specs/blob/master/relay/circuit-v2.md) need to keep a connection alive beyond these circumstances and can thus override this method. /// - Protocols like [ping](https://github.com/libp2p/specs/blob/master/ping/ping.md) **don't** want to keep a connection alive despite an active streams. + /// /// In that case, protocol authors can use [`Stream::ignore_for_keep_alive`](crate::Stream::ignore_for_keep_alive) to opt-out a particular stream from the keep-alive algorithm. fn connection_keep_alive(&self) -> bool { false @@ -334,64 +332,124 @@ pub enum ProtocolsChange<'a> { } impl<'a> ProtocolsChange<'a> { + /// Compute the protocol change for the initial set of protocols. + pub(crate) fn from_initial_protocols<'b, T: AsRef + 'b>( + new_protocols: impl IntoIterator, + buffer: &'a mut Vec, + ) -> Self { + buffer.clear(); + buffer.extend( + new_protocols + .into_iter() + .filter_map(|i| StreamProtocol::try_from_owned(i.as_ref().to_owned()).ok()), + ); + + ProtocolsChange::Added(ProtocolsAdded { + protocols: buffer.iter(), + }) + } + /// Compute the [`ProtocolsChange`] that results from adding `to_add` to `existing_protocols`. /// /// Returns `None` if the change is a no-op, i.e. `to_add` is a subset of `existing_protocols`. pub(crate) fn add( - existing_protocols: &'a HashSet, - to_add: &'a HashSet, + existing_protocols: &HashSet, + to_add: HashSet, + buffer: &'a mut Vec, ) -> Option { - let mut actually_added_protocols = to_add.difference(existing_protocols).peekable(); - - actually_added_protocols.peek()?; + buffer.clear(); + buffer.extend( + to_add + .into_iter() + .filter(|i| !existing_protocols.contains(i)), + ); + + if buffer.is_empty() { + return None; + } - Some(ProtocolsChange::Added(ProtocolsAdded { - protocols: actually_added_protocols, + Some(Self::Added(ProtocolsAdded { + protocols: buffer.iter(), })) } - /// Compute the [`ProtocolsChange`] that results from removing `to_remove` from `existing_protocols`. + /// Compute the [`ProtocolsChange`] that results from removing `to_remove` from `existing_protocols`. Removes the protocols from `existing_protocols`. /// /// Returns `None` if the change is a no-op, i.e. none of the protocols in `to_remove` are in `existing_protocols`. pub(crate) fn remove( - existing_protocols: &'a HashSet, - to_remove: &'a HashSet, + existing_protocols: &mut HashSet, + to_remove: HashSet, + buffer: &'a mut Vec, ) -> Option { - let mut actually_removed_protocols = existing_protocols.intersection(to_remove).peekable(); - - actually_removed_protocols.peek()?; + buffer.clear(); + buffer.extend( + to_remove + .into_iter() + .filter_map(|i| existing_protocols.take(&i)), + ); + + if buffer.is_empty() { + return None; + } - Some(ProtocolsChange::Removed(ProtocolsRemoved { - protocols: Either::Right(actually_removed_protocols), + Some(Self::Removed(ProtocolsRemoved { + protocols: buffer.iter(), })) } /// Compute the [`ProtocolsChange`]s required to go from `existing_protocols` to `new_protocols`. - pub(crate) fn from_full_sets( - existing_protocols: &'a HashSet, - new_protocols: &'a HashSet, + pub(crate) fn from_full_sets>( + existing_protocols: &mut HashMap, bool>, + new_protocols: impl IntoIterator, + buffer: &'a mut Vec, ) -> SmallVec<[Self; 2]> { - if existing_protocols == new_protocols { + buffer.clear(); + + // Initially, set the boolean for all protocols to `false`, meaning "not visited". + for v in existing_protocols.values_mut() { + *v = false; + } + + let mut new_protocol_count = 0; // We can only iterate `new_protocols` once, so keep track of its length separately. + for new_protocol in new_protocols { + existing_protocols + .entry(AsStrHashEq(new_protocol)) + .and_modify(|v| *v = true) // Mark protocol as visited (i.e. we still support it) + .or_insert_with_key(|k| { + // Encountered a previously unsupported protocol, remember it in `buffer`. + buffer.extend(StreamProtocol::try_from_owned(k.0.as_ref().to_owned()).ok()); + true + }); + new_protocol_count += 1; + } + + if new_protocol_count == existing_protocols.len() && buffer.is_empty() { return SmallVec::new(); } - let mut changes = SmallVec::new(); + let num_new_protocols = buffer.len(); + // Drain all protocols that we haven't visited. + // For existing protocols that are not in `new_protocols`, the boolean will be false, meaning we need to remove it. + existing_protocols.retain(|p, &mut is_supported| { + if !is_supported { + buffer.extend(StreamProtocol::try_from_owned(p.0.as_ref().to_owned()).ok()); + } - let mut added_protocols = new_protocols.difference(existing_protocols).peekable(); - let mut removed_protocols = existing_protocols.difference(new_protocols).peekable(); + is_supported + }); - if added_protocols.peek().is_some() { + let (added, removed) = buffer.split_at(num_new_protocols); + let mut changes = SmallVec::new(); + if !added.is_empty() { changes.push(ProtocolsChange::Added(ProtocolsAdded { - protocols: added_protocols, + protocols: added.iter(), })); } - - if removed_protocols.peek().is_some() { + if !removed.is_empty() { changes.push(ProtocolsChange::Removed(ProtocolsRemoved { - protocols: Either::Left(removed_protocols), + protocols: removed.iter(), })); } - changes } } @@ -399,33 +457,13 @@ impl<'a> ProtocolsChange<'a> { /// An [`Iterator`] over all protocols that have been added. #[derive(Debug, Clone)] pub struct ProtocolsAdded<'a> { - protocols: Peekable>, -} - -impl<'a> ProtocolsAdded<'a> { - pub(crate) fn from_set(protocols: &'a HashSet) -> Self { - ProtocolsAdded { - protocols: protocols.difference(&EMPTY_HASHSET).peekable(), - } - } + pub(crate) protocols: slice::Iter<'a, StreamProtocol>, } /// An [`Iterator`] over all protocols that have been removed. #[derive(Debug, Clone)] pub struct ProtocolsRemoved<'a> { - protocols: Either< - Peekable>, - Peekable>, - >, -} - -impl<'a> ProtocolsRemoved<'a> { - #[cfg(test)] - pub(crate) fn from_set(protocols: &'a HashSet) -> Self { - ProtocolsRemoved { - protocols: Either::Left(protocols.difference(&EMPTY_HASHSET).peekable()), - } - } + pub(crate) protocols: slice::Iter<'a, StreamProtocol>, } impl<'a> Iterator for ProtocolsAdded<'a> { @@ -690,6 +728,169 @@ where } } -/// A statically declared, empty [`HashSet`] allows us to work around borrow-checker rules for -/// [`ProtocolsAdded::from_set`]. The lifetimes don't work unless we have a [`HashSet`] with a `'static' lifetime. -static EMPTY_HASHSET: Lazy> = Lazy::new(HashSet::new); +#[cfg(test)] +mod test { + use super::*; + + fn protocol_set_of(s: &'static str) -> HashSet { + s.split_whitespace() + .map(|p| StreamProtocol::try_from_owned(format!("/{p}")).unwrap()) + .collect() + } + + fn test_remove( + existing: &mut HashSet, + to_remove: HashSet, + ) -> HashSet { + ProtocolsChange::remove(existing, to_remove, &mut Vec::new()) + .into_iter() + .flat_map(|c| match c { + ProtocolsChange::Added(_) => panic!("unexpected added"), + ProtocolsChange::Removed(r) => r.cloned(), + }) + .collect::>() + } + + #[test] + fn test_protocol_remove_subset() { + let mut existing = protocol_set_of("a b c"); + let to_remove = protocol_set_of("a b"); + + let change = test_remove(&mut existing, to_remove); + + assert_eq!(existing, protocol_set_of("c")); + assert_eq!(change, protocol_set_of("a b")); + } + + #[test] + fn test_protocol_remove_all() { + let mut existing = protocol_set_of("a b c"); + let to_remove = protocol_set_of("a b c"); + + let change = test_remove(&mut existing, to_remove); + + assert_eq!(existing, protocol_set_of("")); + assert_eq!(change, protocol_set_of("a b c")); + } + + #[test] + fn test_protocol_remove_superset() { + let mut existing = protocol_set_of("a b c"); + let to_remove = protocol_set_of("a b c d"); + + let change = test_remove(&mut existing, to_remove); + + assert_eq!(existing, protocol_set_of("")); + assert_eq!(change, protocol_set_of("a b c")); + } + + #[test] + fn test_protocol_remove_none() { + let mut existing = protocol_set_of("a b c"); + let to_remove = protocol_set_of("d"); + + let change = test_remove(&mut existing, to_remove); + + assert_eq!(existing, protocol_set_of("a b c")); + assert_eq!(change, protocol_set_of("")); + } + + #[test] + fn test_protocol_remove_none_from_empty() { + let mut existing = protocol_set_of(""); + let to_remove = protocol_set_of("d"); + + let change = test_remove(&mut existing, to_remove); + + assert_eq!(existing, protocol_set_of("")); + assert_eq!(change, protocol_set_of("")); + } + + fn test_from_full_sets( + existing: HashSet, + new: HashSet, + ) -> [HashSet; 2] { + let mut buffer = Vec::new(); + let mut existing = existing + .iter() + .map(|p| (AsStrHashEq(p.as_ref()), true)) + .collect::>(); + + let changes = ProtocolsChange::from_full_sets( + &mut existing, + new.iter().map(AsRef::as_ref), + &mut buffer, + ); + + let mut added_changes = HashSet::new(); + let mut removed_changes = HashSet::new(); + + for change in changes { + match change { + ProtocolsChange::Added(a) => { + added_changes.extend(a.cloned()); + } + ProtocolsChange::Removed(r) => { + removed_changes.extend(r.cloned()); + } + } + } + + [removed_changes, added_changes] + } + + #[test] + fn test_from_full_stes_subset() { + let existing = protocol_set_of("a b c"); + let new = protocol_set_of("a b"); + + let [removed_changes, added_changes] = test_from_full_sets(existing, new); + + assert_eq!(added_changes, protocol_set_of("")); + assert_eq!(removed_changes, protocol_set_of("c")); + } + + #[test] + fn test_from_full_sets_superset() { + let existing = protocol_set_of("a b"); + let new = protocol_set_of("a b c"); + + let [removed_changes, added_changes] = test_from_full_sets(existing, new); + + assert_eq!(added_changes, protocol_set_of("c")); + assert_eq!(removed_changes, protocol_set_of("")); + } + + #[test] + fn test_from_full_sets_intersection() { + let existing = protocol_set_of("a b c"); + let new = protocol_set_of("b c d"); + + let [removed_changes, added_changes] = test_from_full_sets(existing, new); + + assert_eq!(added_changes, protocol_set_of("d")); + assert_eq!(removed_changes, protocol_set_of("a")); + } + + #[test] + fn test_from_full_sets_disjoint() { + let existing = protocol_set_of("a b c"); + let new = protocol_set_of("d e f"); + + let [removed_changes, added_changes] = test_from_full_sets(existing, new); + + assert_eq!(added_changes, protocol_set_of("d e f")); + assert_eq!(removed_changes, protocol_set_of("a b c")); + } + + #[test] + fn test_from_full_sets_empty() { + let existing = protocol_set_of(""); + let new = protocol_set_of(""); + + let [removed_changes, added_changes] = test_from_full_sets(existing, new); + + assert_eq!(added_changes, protocol_set_of("")); + assert_eq!(removed_changes, protocol_set_of("")); + } +} diff --git a/swarm/src/handler/multi.rs b/swarm/src/handler/multi.rs index 96a77626c01..5efcde5c2bb 100644 --- a/swarm/src/handler/multi.rs +++ b/swarm/src/handler/multi.rs @@ -35,7 +35,7 @@ use std::{ error, fmt::{self, Debug}, hash::Hash, - iter::{self, FromIterator}, + iter, task::{Context, Poll}, time::Duration, }; diff --git a/swarm/src/handler/one_shot.rs b/swarm/src/handler/one_shot.rs index b1fc41e9098..fc1074b31e4 100644 --- a/swarm/src/handler/one_shot.rs +++ b/swarm/src/handler/one_shot.rs @@ -232,6 +232,6 @@ mod tests { } })); - assert!(matches!(handler.connection_keep_alive(), false)); + assert!(!handler.connection_keep_alive()); } } diff --git a/swarm/src/handler/pending.rs b/swarm/src/handler/pending.rs index 23b9adcfd90..9601f5cf78b 100644 --- a/swarm/src/handler/pending.rs +++ b/swarm/src/handler/pending.rs @@ -52,6 +52,8 @@ impl ConnectionHandler for PendingConnectionHandler { } fn on_behaviour_event(&mut self, v: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(v) } @@ -74,9 +76,13 @@ impl ConnectionHandler for PendingConnectionHandler { >, ) { match event { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, .. }) => void::unreachable(protocol), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { protocol, info: _info, @@ -87,6 +93,8 @@ impl ConnectionHandler for PendingConnectionHandler { void::unreachable(_info); } } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::AddressChange(_) | ConnectionEvent::DialUpgradeError(_) | ConnectionEvent::ListenUpgradeError(_) diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 2f02e43348d..12280e99f07 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -68,6 +68,7 @@ pub mod dial_opts; pub mod dummy; pub mod handler; mod listen_opts; +mod translation; /// Bundles all symbols required for the [`libp2p_swarm_derive::NetworkBehaviour`] macro. #[doc(hidden)] @@ -99,7 +100,7 @@ pub mod derive_prelude { pub use crate::ToSwarm; pub use either::Either; pub use futures::prelude as futures; - pub use libp2p_core::transport::ListenerId; + pub use libp2p_core::transport::{ListenerId, PortUse}; pub use libp2p_core::ConnectedPoint; pub use libp2p_core::Endpoint; pub use libp2p_core::Multiaddr; @@ -134,24 +135,27 @@ use connection::{ }; use dial_opts::{DialOpts, PeerCondition}; use futures::{prelude::*, stream::FusedStream}; + use libp2p_core::{ connection::ConnectedPoint, muxing::StreamMuxerBox, transport::{self, ListenerId, TransportError, TransportEvent}, - Endpoint, Multiaddr, Transport, + Multiaddr, Transport, }; use libp2p_identity::PeerId; + use smallvec::SmallVec; use std::collections::{HashMap, HashSet, VecDeque}; use std::num::{NonZeroU32, NonZeroU8, NonZeroUsize}; use std::time::Duration; use std::{ - convert::TryFrom, error, fmt, io, pin::Pin, task::{Context, Poll}, }; use tracing::Instrument; +#[doc(hidden)] +pub use translation::_address_translation; /// Event generated by the [`NetworkBehaviour`] that the swarm will report back. type TBehaviourOutEvent = ::ToSwarm; @@ -529,18 +533,15 @@ where .into_iter() .map(|a| match peer_id.map_or(Ok(a.clone()), |p| a.with_p2p(p)) { Ok(address) => { - let (dial, span) = match dial_opts.role_override() { - Endpoint::Dialer => ( - self.transport.dial(address.clone()), - tracing::debug_span!(parent: tracing::Span::none(), "Transport::dial", %address), - ), - Endpoint::Listener => ( - self.transport.dial_as_listener(address.clone()), - tracing::debug_span!(parent: tracing::Span::none(), "Transport::dial_as_listener", %address), - ), - }; + let dial = self.transport.dial( + address.clone(), + transport::DialOpts { + role: dial_opts.role_override(), + port_use: dial_opts.port_use(), + }, + ); + let span = tracing::debug_span!(parent: tracing::Span::none(), "Transport::dial", %address); span.follows_from(tracing::Span::current()); - match dial { Ok(fut) => fut .map(|r| (address, r.map_err(TransportError::Other))) @@ -561,6 +562,7 @@ where dials, peer_id, dial_opts.role_override(), + dial_opts.port_use(), dial_opts.dial_concurrency_override(), connection_id, ); @@ -707,12 +709,14 @@ where ConnectedPoint::Dialer { address, role_override, + port_use, } => { match self.behaviour.handle_established_outbound_connection( id, peer_id, &address, role_override, + port_use, ) { Ok(handler) => handler, Err(cause) => { @@ -755,6 +759,7 @@ where send_back_addr: &send_back_addr, error: &listen_error, connection_id: id, + peer_id: Some(peer_id), }, )); @@ -868,6 +873,7 @@ where send_back_addr: &send_back_addr, error: &error, connection_id: id, + peer_id: None, })); self.pending_swarm_events .push_back(SwarmEvent::IncomingConnectionError { @@ -908,6 +914,7 @@ where peer_id, connection_id: id, endpoint: &endpoint, + cause: error.as_ref(), remaining_established: num_established as usize, })); self.pending_swarm_events @@ -971,6 +978,7 @@ where send_back_addr: &send_back_addr, error: &listen_error, connection_id, + peer_id: None, })); self.pending_swarm_events @@ -1132,39 +1140,13 @@ where self.pending_handler_event = Some((peer_id, handler, event)); } ToSwarm::NewExternalAddrCandidate(addr) => { - // Apply address translation to the candidate address. - // For TCP without port-reuse, the observed address contains an ephemeral port which needs to be replaced by the port of a listen address. - let translated_addresses = { - let mut addrs: Vec<_> = self - .listened_addrs - .values() - .flatten() - .filter_map(|server| self.transport.address_translation(server, &addr)) - .collect(); - - // remove duplicates - addrs.sort_unstable(); - addrs.dedup(); - addrs - }; - - // If address translation yielded nothing, broacast the original candidate address. - if translated_addresses.is_empty() { + if !self.confirmed_external_addr.contains(&addr) { self.behaviour .on_swarm_event(FromSwarm::NewExternalAddrCandidate( NewExternalAddrCandidate { addr: &addr }, )); self.pending_swarm_events .push_back(SwarmEvent::NewExternalAddrCandidate { address: addr }); - } else { - for addr in translated_addresses { - self.behaviour - .on_swarm_event(FromSwarm::NewExternalAddrCandidate( - NewExternalAddrCandidate { addr: &addr }, - )); - self.pending_swarm_events - .push_back(SwarmEvent::NewExternalAddrCandidate { address: addr }); - } } } ToSwarm::ExternalAddrConfirmed(addr) => { @@ -1414,6 +1396,14 @@ impl Config { } } + #[doc(hidden)] + /// Used on connection benchmarks. + pub fn without_executor() -> Self { + Self { + pool_config: PoolConfig::new(None), + } + } + /// Sets executor to the `wasm` executor. /// Background tasks will be executed by the browser on the next micro-tick. /// @@ -1527,9 +1517,7 @@ impl Config { #[derive(Debug)] pub enum DialError { /// The peer identity obtained on the connection matches the local peer. - LocalPeerId { - endpoint: ConnectedPoint, - }, + LocalPeerId { endpoint: ConnectedPoint }, /// No addresses have been provided by [`NetworkBehaviour::handle_pending_outbound_connection`] and [`DialOpts`]. NoAddresses, /// The provided [`dial_opts::PeerCondition`] evaluated to false and thus @@ -1542,9 +1530,10 @@ pub enum DialError { obtained: PeerId, endpoint: ConnectedPoint, }, - Denied { - cause: ConnectionDenied, - }, + /// One of the [`NetworkBehaviour`]s rejected the outbound connection + /// via [`NetworkBehaviour::handle_pending_outbound_connection`] or + /// [`NetworkBehaviour::handle_established_outbound_connection`]. + Denied { cause: ConnectionDenied }, /// An error occurred while negotiating the transport protocol(s) on a connection. Transport(Vec<(Multiaddr, TransportError)>), } @@ -1771,12 +1760,10 @@ impl NetworkInfo { #[cfg(test)] mod tests { use super::*; - use crate::dummy; use crate::test::{CallTraceBehaviour, MockBehaviour}; - use futures::future; use libp2p_core::multiaddr::multiaddr; use libp2p_core::transport::memory::MemoryTransportError; - use libp2p_core::transport::TransportEvent; + use libp2p_core::transport::{PortUse, TransportEvent}; use libp2p_core::Endpoint; use libp2p_core::{multiaddr, transport, upgrade}; use libp2p_identity as identity; @@ -1832,7 +1819,7 @@ mod tests { && swarm2.is_connected(swarm1.local_peer_id()) } - fn swarms_disconnected( + fn swarms_disconnected( swarm1: &Swarm>, swarm2: &Swarm>, ) -> bool @@ -2173,6 +2160,7 @@ mod tests { ConnectedPoint::Dialer { address: other_addr, role_override: Endpoint::Dialer, + port_use: PortUse::Reuse, } ); } diff --git a/swarm/src/test.rs b/swarm/src/test.rs index 547277550bb..a6cb7c4d4eb 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -26,6 +26,7 @@ use crate::{ ConnectionDenied, ConnectionHandler, ConnectionId, NetworkBehaviour, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm, }; +use libp2p_core::transport::PortUse; use libp2p_core::{multiaddr::Multiaddr, transport::ListenerId, ConnectedPoint, Endpoint}; use libp2p_identity::PeerId; use std::collections::HashMap; @@ -91,6 +92,7 @@ where _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result { Ok(self.handler_proto.clone()) } @@ -301,6 +303,7 @@ where connection_id, endpoint, remaining_established, + cause, }: ConnectionClosed, ) { let mut other_closed_connections = self @@ -350,6 +353,7 @@ where connection_id, endpoint, remaining_established, + cause, })); } } @@ -425,6 +429,7 @@ where peer: PeerId, addr: &Multiaddr, role_override: Endpoint, + port_use: PortUse, ) -> Result, ConnectionDenied> { self.handle_established_outbound_connection.push(( peer, @@ -432,8 +437,13 @@ where role_override, connection_id, )); - self.inner - .handle_established_outbound_connection(connection_id, peer, addr, role_override) + self.inner.handle_established_outbound_connection( + connection_id, + peer, + addr, + role_override, + port_use, + ) } fn on_swarm_event(&mut self, event: FromSwarm) { diff --git a/core/src/translation.rs b/swarm/src/translation.rs similarity index 94% rename from core/src/translation.rs rename to swarm/src/translation.rs index 70700ca6ae8..baa80c907b5 100644 --- a/core/src/translation.rs +++ b/swarm/src/translation.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use multiaddr::{Multiaddr, Protocol}; +use libp2p_core::{multiaddr::Protocol, Multiaddr}; /// Perform IP address translation. /// @@ -35,7 +35,8 @@ use multiaddr::{Multiaddr, Protocol}; /// address and vice versa. /// /// If the first [`Protocol`]s are not IP addresses, `None` is returned instead. -pub fn address_translation(original: &Multiaddr, observed: &Multiaddr) -> Option { +#[doc(hidden)] +pub fn _address_translation(original: &Multiaddr, observed: &Multiaddr) -> Option { original.replace(0, move |proto| match proto { Protocol::Ip4(_) | Protocol::Ip6(_) @@ -65,7 +66,7 @@ mod tests { expected: Multiaddr, } - let tests = vec![ + let tests = [ // Basic ipv4. Test { original: "/ip4/192.0.2.1/tcp/1".parse().unwrap(), @@ -106,7 +107,7 @@ mod tests { for test in tests.iter() { assert_eq!( - address_translation(&test.original, &test.observed), + _address_translation(&test.original, &test.observed), Some(test.expected.clone()) ); } diff --git a/swarm/src/upgrade.rs b/swarm/src/upgrade.rs index 53b627458c9..f6c6648a373 100644 --- a/swarm/src/upgrade.rs +++ b/swarm/src/upgrade.rs @@ -121,6 +121,7 @@ where } /// Wraps around a type that implements [`OutboundUpgradeSend`], [`InboundUpgradeSend`], or +/// /// both, and implements [`OutboundUpgrade`](upgrade::OutboundUpgrade) and/or /// [`InboundUpgrade`](upgrade::InboundUpgrade). /// diff --git a/swarm/tests/connection_close.rs b/swarm/tests/connection_close.rs index 4efe8d17e49..4d530f47684 100644 --- a/swarm/tests/connection_close.rs +++ b/swarm/tests/connection_close.rs @@ -1,3 +1,4 @@ +use libp2p_core::transport::PortUse; use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::{Endpoint, Multiaddr}; use libp2p_identity::PeerId; @@ -66,6 +67,7 @@ impl NetworkBehaviour for Behaviour { _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(HandlerWithState { precious_state: self.state, diff --git a/swarm/tests/listener.rs b/swarm/tests/listener.rs index 8d22acc90e2..160b1f5b064 100644 --- a/swarm/tests/listener.rs +++ b/swarm/tests/listener.rs @@ -3,7 +3,11 @@ use std::{ task::{Context, Poll}, }; -use libp2p_core::{multiaddr::Protocol, transport::ListenerId, Endpoint, Multiaddr}; +use libp2p_core::{ + multiaddr::Protocol, + transport::{ListenerId, PortUse}, + Endpoint, Multiaddr, +}; use libp2p_identity::PeerId; use libp2p_swarm::{ derive_prelude::NewListener, dummy, ConnectionDenied, ConnectionId, FromSwarm, ListenOpts, @@ -93,6 +97,7 @@ impl NetworkBehaviour for Behaviour { _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(dummy::ConnectionHandler) } diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index 707abb03d6e..667f68408cf 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -19,7 +19,7 @@ // DEALINGS IN THE SOFTWARE. use futures::StreamExt; -use libp2p_core::{Endpoint, Multiaddr}; +use libp2p_core::{transport::PortUse, Endpoint, Multiaddr}; use libp2p_identify as identify; use libp2p_ping as ping; use libp2p_swarm::{ @@ -133,19 +133,19 @@ fn custom_event() { #[allow(clippy::large_enum_variant)] enum MyEvent { - Ping(ping::Event), - Identify(identify::Event), + Ping, + Identify, } impl From for MyEvent { - fn from(event: ping::Event) -> Self { - MyEvent::Ping(event) + fn from(_event: ping::Event) -> Self { + MyEvent::Ping } } impl From for MyEvent { - fn from(event: identify::Event) -> Self { - MyEvent::Identify(event) + fn from(_event: identify::Event) -> Self { + MyEvent::Identify } } @@ -167,19 +167,19 @@ fn custom_event_mismatching_field_names() { #[allow(clippy::large_enum_variant)] enum MyEvent { - Ping(ping::Event), - Identify(identify::Event), + Ping, + Identify, } impl From for MyEvent { - fn from(event: ping::Event) -> Self { - MyEvent::Ping(event) + fn from(_event: ping::Event) -> Self { + MyEvent::Ping } } impl From for MyEvent { - fn from(event: identify::Event) -> Self { - MyEvent::Identify(event) + fn from(_event: identify::Event) -> Self { + MyEvent::Identify } } @@ -252,19 +252,19 @@ fn nested_derives_with_import() { fn custom_event_emit_event_through_poll() { #[allow(clippy::large_enum_variant)] enum BehaviourOutEvent { - Ping(ping::Event), - Identify(identify::Event), + Ping, + Identify, } impl From for BehaviourOutEvent { - fn from(event: ping::Event) -> Self { - BehaviourOutEvent::Ping(event) + fn from(_event: ping::Event) -> Self { + BehaviourOutEvent::Ping } } impl From for BehaviourOutEvent { - fn from(event: identify::Event) -> Self { - BehaviourOutEvent::Identify(event) + fn from(_event: identify::Event) -> Self { + BehaviourOutEvent::Identify } } @@ -293,8 +293,8 @@ fn custom_event_emit_event_through_poll() { // check that the event is bubbled up all the way to swarm loop { match _swarm.select_next_some().await { - SwarmEvent::Behaviour(BehaviourOutEvent::Ping(_)) => break, - SwarmEvent::Behaviour(BehaviourOutEvent::Identify(_)) => break, + SwarmEvent::Behaviour(BehaviourOutEvent::Ping) => break, + SwarmEvent::Behaviour(BehaviourOutEvent::Identify) => break, _ => {} } } @@ -404,6 +404,7 @@ fn with_generics_constrained() { _: libp2p_identity::PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(dummy::ConnectionHandler) } @@ -460,21 +461,20 @@ fn with_generics_constrained() { fn custom_event_with_either() { use either::Either; - #[allow(clippy::large_enum_variant)] enum BehaviourOutEvent { - Kad(libp2p_kad::Event), - PingOrIdentify(Either), + Kad, + PingOrIdentify, } impl From for BehaviourOutEvent { - fn from(event: libp2p_kad::Event) -> Self { - BehaviourOutEvent::Kad(event) + fn from(_event: libp2p_kad::Event) -> Self { + BehaviourOutEvent::Kad } } impl From> for BehaviourOutEvent { - fn from(event: Either) -> Self { - BehaviourOutEvent::PingOrIdentify(event) + fn from(_event: Either) -> Self { + BehaviourOutEvent::PingOrIdentify } } @@ -566,6 +566,7 @@ fn custom_out_event_no_type_parameters() { _: PeerId, _: &Multiaddr, _: Endpoint, + _: PortUse, ) -> Result, ConnectionDenied> { Ok(dummy::ConnectionHandler) } @@ -576,6 +577,8 @@ fn custom_out_event_no_type_parameters() { _connection: ConnectionId, message: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(message); } diff --git a/transports/dns/CHANGELOG.md b/transports/dns/CHANGELOG.md index 91cfbc00883..e4f951f157f 100644 --- a/transports/dns/CHANGELOG.md +++ b/transports/dns/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.42.0 + +- Implement refactored `Transport`. + See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568) + ## 0.41.1 - Add hidden API that removes unnecessary async for `async-std`. diff --git a/transports/dns/Cargo.toml b/transports/dns/Cargo.toml index f026aac5157..707b67fc935 100644 --- a/transports/dns/Cargo.toml +++ b/transports/dns/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-dns" edition = "2021" rust-version = { workspace = true } description = "DNS transport implementation for libp2p" -version = "0.41.1" +version = "0.42.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -12,20 +12,20 @@ categories = ["network-programming", "asynchronous"] [dependencies] async-std-resolver = { version = "0.24", optional = true } -async-trait = "0.1.77" -futures = "0.3.30" +async-trait = "0.1.80" +futures = { workspace = true } libp2p-core = { workspace = true } libp2p-identity = { workspace = true } -parking_lot = "0.12.0" -hickory-resolver = { version = "0.24.0", default-features = false, features = ["system-config"] } -smallvec = "1.13.1" -tracing = "0.1.37" +parking_lot = "0.12.3" +hickory-resolver = { version = "0.24.1", default-features = false, features = ["system-config"] } +smallvec = "1.13.2" +tracing = { workspace = true } [dev-dependencies] libp2p-identity = { workspace = true, features = ["rand"] } -tokio-crate = { package = "tokio", version = "1.0", default-features = false, features = ["rt", "time"] } +tokio = { workspace = true, features = ["rt", "time"] } async-std-crate = { package = "async-std", version = "1.6" } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [features] async-std = ["async-std-resolver"] @@ -35,8 +35,6 @@ tokio = ["hickory-resolver/tokio-runtime"] # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index be847e568ed..7d92cc8ecfc 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -48,6 +48,7 @@ //! any system APIs (like libc's `gethostbyname`). Again this is //! problematic on platforms like Android, where there's a lot of //! complexity hidden behind the system APIs. +//! //! If the implementation requires different characteristics, one should //! consider providing their own implementation of [`Transport`] or use //! platform specific APIs to extract the host's DNS configuration (if possible) @@ -148,16 +149,14 @@ pub mod tokio { use async_trait::async_trait; use futures::{future::BoxFuture, prelude::*}; use libp2p_core::{ - connection::Endpoint, multiaddr::{Multiaddr, Protocol}, - transport::{ListenerId, TransportError, TransportEvent}, + transport::{DialOpts, ListenerId, TransportError, TransportEvent}, }; use parking_lot::Mutex; use smallvec::SmallVec; use std::io; use std::net::{Ipv4Addr, Ipv6Addr}; use std::{ - convert::TryFrom, error, fmt, iter, ops::DerefMut, pin::Pin, @@ -231,19 +230,12 @@ where self.inner.lock().remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { - self.do_dial(addr, Endpoint::Dialer) - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + dial_opts: DialOpts, ) -> Result> { - self.do_dial(addr, Endpoint::Listener) - } - - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.inner.lock().address_translation(server, observed) + Ok(self.do_dial(addr, dial_opts)) } fn poll( @@ -269,17 +261,14 @@ where fn do_dial( &mut self, addr: Multiaddr, - role_override: Endpoint, - ) -> Result< - ::Dial, - TransportError<::Error>, - > { + dial_opts: DialOpts, + ) -> ::Dial { let resolver = self.resolver.clone(); let inner = self.inner.clone(); // Asynchronously resolve all DNS names in the address before proceeding // with dialing on the underlying transport. - Ok(async move { + async move { let mut last_err = None; let mut dns_lookups = 0; let mut dial_attempts = 0; @@ -358,10 +347,7 @@ where tracing::debug!(address=%addr, "Dialing address"); let transport = inner.clone(); - let dial = match role_override { - Endpoint::Dialer => transport.lock().dial(addr), - Endpoint::Listener => transport.lock().dial_as_listener(addr), - }; + let dial = transport.lock().dial(addr, dial_opts); let result = match dial { Ok(out) => { // We only count attempts that the inner transport @@ -405,7 +391,7 @@ where })) } .boxed() - .right_future()) + .right_future() } } @@ -631,8 +617,8 @@ mod tests { use futures::future::BoxFuture; use libp2p_core::{ multiaddr::{Multiaddr, Protocol}, - transport::{TransportError, TransportEvent}, - Transport, + transport::{PortUse, TransportError, TransportEvent}, + Endpoint, Transport, }; use libp2p_identity::PeerId; @@ -663,7 +649,11 @@ mod tests { false } - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + _: DialOpts, + ) -> Result> { // Check that all DNS components have been resolved, i.e. replaced. assert!(!addr.iter().any(|p| matches!( p, @@ -672,17 +662,6 @@ mod tests { Ok(Box::pin(future::ready(Ok(())))) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - self.dial(addr) - } - - fn address_translation(&self, _: &Multiaddr, _: &Multiaddr) -> Option { - None - } - fn poll( self: Pin<&mut Self>, _: &mut Context<'_>, @@ -698,30 +677,34 @@ mod tests { T::Dial: Send, R: Clone + Send + Sync + Resolver + 'static, { + let dial_opts = DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }; // Success due to existing A record for example.com. let _ = transport - .dial("/dns4/example.com/tcp/20000".parse().unwrap()) + .dial("/dns4/example.com/tcp/20000".parse().unwrap(), dial_opts) .unwrap() .await .unwrap(); // Success due to existing AAAA record for example.com. let _ = transport - .dial("/dns6/example.com/tcp/20000".parse().unwrap()) + .dial("/dns6/example.com/tcp/20000".parse().unwrap(), dial_opts) .unwrap() .await .unwrap(); // Success due to pass-through, i.e. nothing to resolve. let _ = transport - .dial("/ip4/1.2.3.4/tcp/20000".parse().unwrap()) + .dial("/ip4/1.2.3.4/tcp/20000".parse().unwrap(), dial_opts) .unwrap() .await .unwrap(); // Success due to the DNS TXT records at _dnsaddr.bootstrap.libp2p.io. let _ = transport - .dial("/dnsaddr/bootstrap.libp2p.io".parse().unwrap()) + .dial("/dnsaddr/bootstrap.libp2p.io".parse().unwrap(), dial_opts) .unwrap() .await .unwrap(); @@ -730,7 +713,7 @@ mod tests { // an entry with suffix `/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN`, // i.e. a bootnode with such a peer ID. let _ = transport - .dial("/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN".parse().unwrap()) + .dial("/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN".parse().unwrap(), dial_opts) .unwrap() .await .unwrap(); @@ -742,6 +725,7 @@ mod tests { format!("/dnsaddr/bootstrap.libp2p.io/p2p/{}", PeerId::random()) .parse() .unwrap(), + dial_opts, ) .unwrap() .await @@ -753,7 +737,10 @@ mod tests { // Failure due to no records. match transport - .dial("/dns4/example.invalid/tcp/20000".parse().unwrap()) + .dial( + "/dns4/example.invalid/tcp/20000".parse().unwrap(), + dial_opts, + ) .unwrap() .await { @@ -783,7 +770,7 @@ mod tests { // type record lookups may not work with the system DNS resolver. let config = ResolverConfig::quad9(); let opts = ResolverOpts::default(); - let rt = tokio_crate::runtime::Builder::new_current_thread() + let rt = ::tokio::runtime::Builder::new_current_thread() .enable_io() .enable_time() .build() diff --git a/transports/noise/CHANGELOG.md b/transports/noise/CHANGELOG.md index 78effb673d2..f599ae3533f 100644 --- a/transports/noise/CHANGELOG.md +++ b/transports/noise/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.45.0 + + + ## 0.44.0 - Migrate to `{In,Out}boundConnectionUpgrade` traits. diff --git a/transports/noise/Cargo.toml b/transports/noise/Cargo.toml index 83bb6de8c0e..7f8e9004cd0 100644 --- a/transports/noise/Cargo.toml +++ b/transports/noise/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-noise" edition = "2021" rust-version = { workspace = true } description = "Cryptographic handshake protocol using the noise framework." -version = "0.44.0" +version = "0.45.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -12,7 +12,7 @@ repository = "https://github.com/libp2p/rust-libp2p" asynchronous-codec = { workspace = true } bytes = "1" curve25519-dalek = "4.1.2" -futures = "0.3.30" +futures = { workspace = true } libp2p-core = { workspace = true } libp2p-identity = { workspace = true, features = ["ed25519"] } multiaddr = { workspace = true } @@ -22,8 +22,8 @@ quick-protobuf = "0.8" rand = "0.8.3" sha2 = "0.10.8" static_assertions = "1" -thiserror = "1.0.57" -tracing = "0.1.37" +thiserror = "1.0.61" +tracing = { workspace = true } x25519-dalek = "2" zeroize = "1" @@ -36,15 +36,13 @@ snow = { version = "0.9.5", features = ["default-resolver"], default-features = [dev-dependencies] futures_ringbuf = "0.4.0" quickcheck = { workspace = true } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } libp2p-identity = { workspace = true, features = ["rand"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/transports/noise/src/io/framed.rs b/transports/noise/src/io/framed.rs index 9ed6045cf38..17254efb0a9 100644 --- a/transports/noise/src/io/framed.rs +++ b/transports/noise/src/io/framed.rs @@ -197,7 +197,7 @@ fn decrypt( ciphertext: &mut BytesMut, decrypt_fn: impl FnOnce(&[u8], &mut [u8]) -> Result, ) -> io::Result> { - let Some(ciphertext) = decode_length_prefixed(ciphertext)? else { + let Some(ciphertext) = decode_length_prefixed(ciphertext) else { return Ok(None); }; @@ -223,9 +223,9 @@ fn encode_length_prefixed(src: &[u8], dst: &mut BytesMut) { dst.extend_from_slice(src); } -fn decode_length_prefixed(src: &mut BytesMut) -> Result, io::Error> { +fn decode_length_prefixed(src: &mut BytesMut) -> Option { if src.len() < size_of::() { - return Ok(None); + return None; } let mut len_bytes = [0u8; U16_LENGTH]; @@ -235,8 +235,8 @@ fn decode_length_prefixed(src: &mut BytesMut) -> Result, io::Error if src.len() - U16_LENGTH >= len { // Skip the length header we already read. src.advance(U16_LENGTH); - Ok(Some(src.split_to(len).freeze())) + Some(src.split_to(len).freeze()) } else { - Ok(None) + None } } diff --git a/transports/noise/src/io/handshake.rs b/transports/noise/src/io/handshake.rs index 7cc0f859e6e..5c1fa806b6d 100644 --- a/transports/noise/src/io/handshake.rs +++ b/transports/noise/src/io/handshake.rs @@ -248,7 +248,7 @@ where ..Default::default() }; - pb.identity_sig = state.identity.signature.clone(); + pb.identity_sig.clone_from(&state.identity.signature); // If this is the responder then send WebTransport certhashes to initiator, if any. if state.io.codec().is_responder() { diff --git a/transports/noise/src/protocol.rs b/transports/noise/src/protocol.rs index e37c55c7f10..29d0c81e2e4 100644 --- a/transports/noise/src/protocol.rs +++ b/transports/noise/src/protocol.rs @@ -292,8 +292,6 @@ impl snow::types::Dh for Keypair { #[cfg(test)] mod tests { use super::*; - use crate::protocol::PARAMS_XX; - use once_cell::sync::Lazy; #[test] fn handshake_hashes_disagree_if_prologue_differs() { diff --git a/transports/noise/tests/smoke.rs b/transports/noise/tests/smoke.rs index 7100e7c7a8d..62b5d41d6b9 100644 --- a/transports/noise/tests/smoke.rs +++ b/transports/noise/tests/smoke.rs @@ -25,7 +25,7 @@ use libp2p_core::upgrade::{InboundConnectionUpgrade, OutboundConnectionUpgrade}; use libp2p_identity as identity; use libp2p_noise as noise; use quickcheck::*; -use std::{convert::TryInto, io}; +use std::io; use tracing_subscriber::EnvFilter; #[allow(dead_code)] diff --git a/transports/plaintext/CHANGELOG.md b/transports/plaintext/CHANGELOG.md index 42b53d12a88..91860c76590 100644 --- a/transports/plaintext/CHANGELOG.md +++ b/transports/plaintext/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.42.0 + + + ## 0.41.0 - Migrate to `{In,Out}boundConnectionUpgrade` traits. diff --git a/transports/plaintext/Cargo.toml b/transports/plaintext/Cargo.toml index e3f1e280851..47a3191baa9 100644 --- a/transports/plaintext/Cargo.toml +++ b/transports/plaintext/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-plaintext" edition = "2021" rust-version = { workspace = true } description = "Plaintext encryption dummy protocol for libp2p" -version = "0.41.0" +version = "0.42.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -13,11 +13,11 @@ categories = ["network-programming", "asynchronous"] [dependencies] asynchronous-codec = { workspace = true } bytes = "1" -futures = "0.3.30" +futures = { workspace = true } libp2p-core = { workspace = true } libp2p-identity = { workspace = true } quick-protobuf = "0.8" -tracing = "0.1.37" +tracing = { workspace = true } quick-protobuf-codec = { workspace = true } [dev-dependencies] @@ -25,14 +25,12 @@ libp2p-identity = { workspace = true, features = ["ed25519", "rand"] } quickcheck = { workspace = true } rand = "0.8" futures_ringbuf = "0.4.0" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } -# Passing arguments to the docsrs builder in order to properly document cfg's. +# Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/transports/pnet/CHANGELOG.md b/transports/pnet/CHANGELOG.md index 1fbc2d08807..86d519e8640 100644 --- a/transports/pnet/CHANGELOG.md +++ b/transports/pnet/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.25.0 + + + ## 0.24.0 diff --git a/transports/pnet/Cargo.toml b/transports/pnet/Cargo.toml index bdefa76bbd1..db5c72fb7cc 100644 --- a/transports/pnet/Cargo.toml +++ b/transports/pnet/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-pnet" edition = "2021" rust-version = { workspace = true } description = "Private swarm support for libp2p" -version = "0.24.0" +version = "0.25.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -11,12 +11,12 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] -futures = "0.3.30" +futures = { workspace = true } salsa20 = "0.10" sha3 = "0.10" -tracing = "0.1.37" +tracing = { workspace = true } rand = "0.8" -pin-project = "1.1.4" +pin-project = "1.1.5" [dev-dependencies] libp2p-core = { workspace = true } @@ -27,14 +27,12 @@ libp2p-tcp = { workspace = true, features = ["tokio"] } libp2p-websocket = { workspace = true } libp2p-yamux = { workspace = true } quickcheck = { workspace = true } -tokio = { version = "1.36.0", features = ["full"] } +tokio = { workspace = true, features = ["full"] } -# Passing arguments to the docsrs builder in order to properly document cfg's. +# Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/transports/quic/CHANGELOG.md b/transports/quic/CHANGELOG.md index 3c34a1989f9..6fc64c5df36 100644 --- a/transports/quic/CHANGELOG.md +++ b/transports/quic/CHANGELOG.md @@ -1,3 +1,22 @@ +## 0.11.1 + +- Update `libp2p-tls` to version `0.5.0`, see [PR 5547] + +[PR 5547]: https://github.com/libp2p/rust-libp2p/pull/5547 + +## 0.11.0 + +- Implement refactored `Transport`. + See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568) + +## 0.10.3 + +- Update `quinn` to 0.11 and `libp2p-tls` to 0.4.0. + See [PR 5316](https://github.com/libp2p/rust-libp2p/pull/5316) + +- Allow configuring MTU discovery upper bound. + See [PR 5386](https://github.com/libp2p/rust-libp2p/pull/5386). + ## 0.10.2 - Change `max_idle_timeout`to 10s. diff --git a/transports/quic/Cargo.toml b/transports/quic/Cargo.toml index abfbf0742f4..42cc8e54edb 100644 --- a/transports/quic/Cargo.toml +++ b/transports/quic/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-quic" -version = "0.10.2" +version = "0.11.1" authors = ["Parity Technologies "] edition = "2021" rust-version = { workspace = true } @@ -10,22 +10,22 @@ license = "MIT" [dependencies] async-std = { version = "1.12.0", optional = true } -bytes = "1.5.0" -futures = "0.3.30" +bytes = "1.6.0" +futures = { workspace = true } futures-timer = "3.0.3" if-watch = "3.2.0" libp2p-core = { workspace = true } libp2p-tls = { workspace = true } libp2p-identity = { workspace = true } -parking_lot = "0.12.0" -quinn = { version = "0.10.2", default-features = false, features = ["tls-rustls", "futures-io"] } +parking_lot = "0.12.3" +quinn = { version = "0.11.2", default-features = false, features = ["rustls", "futures-io"] } rand = "0.8.5" -rustls = { version = "0.21.9", default-features = false } -thiserror = "1.0.57" -tokio = { version = "1.36.0", default-features = false, features = ["net", "rt", "time"], optional = true } -tracing = "0.1.37" -socket2 = "0.5.6" -ring = "0.16.20" +rustls = { version = "0.23.9", default-features = false } +thiserror = "1.0.61" +tokio = { workspace = true, default-features = false, features = ["net", "rt", "time"], optional = true } +tracing = { workspace = true } +socket2 = "0.5.7" +ring = { workspace = true } [features] tokio = ["dep:tokio", "if-watch/tokio", "quinn/runtime-tokio"] @@ -35,8 +35,6 @@ async-std = ["dep:async-std", "if-watch/smol", "quinn/runtime-async-std"] # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [dev-dependencies] async-std = { version = "1.12.0", features = ["attributes"] } @@ -46,8 +44,8 @@ libp2p-noise = { workspace = true } libp2p-tcp = { workspace = true, features = ["async-io"] } libp2p-yamux = { workspace = true } quickcheck = "1" -tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread", "time"] } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [[test]] name = "stream_compliance" diff --git a/transports/quic/src/config.rs b/transports/quic/src/config.rs index 540f13e726b..2456ed3e36f 100644 --- a/transports/quic/src/config.rs +++ b/transports/quic/src/config.rs @@ -18,7 +18,10 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use quinn::{MtuDiscoveryConfig, VarInt}; +use quinn::{ + crypto::rustls::{QuicClientConfig, QuicServerConfig}, + MtuDiscoveryConfig, VarInt, +}; use std::{sync::Arc, time::Duration}; /// Config for the transport. @@ -40,10 +43,10 @@ pub struct Config { /// concurrently by the remote peer. pub max_concurrent_stream_limit: u32, - /// Max unacknowledged data in bytes that may be send on a single stream. + /// Max unacknowledged data in bytes that may be sent on a single stream. pub max_stream_data: u32, - /// Max unacknowledged data in bytes that may be send in total on all streams + /// Max unacknowledged data in bytes that may be sent in total on all streams /// of a connection. pub max_connection_data: u32, @@ -58,9 +61,9 @@ pub struct Config { pub support_draft_29: bool, /// TLS client config for the inner [`quinn::ClientConfig`]. - client_tls_config: Arc, + client_tls_config: Arc, /// TLS server config for the inner [`quinn::ServerConfig`]. - server_tls_config: Arc, + server_tls_config: Arc, /// Libp2p identity of the node. keypair: libp2p_identity::Keypair, @@ -71,8 +74,13 @@ pub struct Config { impl Config { /// Creates a new configuration object with default values. pub fn new(keypair: &libp2p_identity::Keypair) -> Self { - let client_tls_config = Arc::new(libp2p_tls::make_client_config(keypair, None).unwrap()); - let server_tls_config = Arc::new(libp2p_tls::make_server_config(keypair).unwrap()); + let client_tls_config = Arc::new( + QuicClientConfig::try_from(libp2p_tls::make_client_config(keypair, None).unwrap()) + .unwrap(), + ); + let server_tls_config = Arc::new( + QuicServerConfig::try_from(libp2p_tls::make_server_config(keypair).unwrap()).unwrap(), + ); Self { client_tls_config, server_tls_config, @@ -90,6 +98,14 @@ impl Config { } } + /// Set the upper bound to the max UDP payload size that MTU discovery will search for. + pub fn mtu_upper_bound(mut self, value: u16) -> Self { + self.mtu_discovery_config + .get_or_insert_with(Default::default) + .upper_bound(value); + self + } + /// Disable MTU path discovery (it is enabled by default). pub fn disable_path_mtu_discovery(mut self) -> Self { self.mtu_discovery_config = None; diff --git a/transports/quic/src/connection/connecting.rs b/transports/quic/src/connection/connecting.rs index 141f0b5542e..f6e397b4d1e 100644 --- a/transports/quic/src/connection/connecting.rs +++ b/transports/quic/src/connection/connecting.rs @@ -28,6 +28,7 @@ use futures::{ }; use futures_timer::Delay; use libp2p_identity::PeerId; +use quinn::rustls::pki_types::CertificateDer; use std::{ pin::Pin, task::{Context, Poll}, @@ -55,7 +56,7 @@ impl Connecting { let identity = connection .peer_identity() .expect("connection got identity because it passed TLS handshake; qed"); - let certificates: Box> = + let certificates: Box> = identity.downcast().expect("we rely on rustls feature; qed"); let end_entity = certificates .first() diff --git a/transports/quic/src/connection/stream.rs b/transports/quic/src/connection/stream.rs index b0c505bf856..ed3a71ea496 100644 --- a/transports/quic/src/connection/stream.rs +++ b/transports/quic/src/connection/stream.rs @@ -67,7 +67,9 @@ impl AsyncWrite for Stream { cx: &mut Context, buf: &[u8], ) -> Poll> { - Pin::new(&mut self.send).poll_write(cx, buf) + Pin::new(&mut self.send) + .poll_write(cx, buf) + .map_err(Into::into) } fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { diff --git a/transports/quic/src/hole_punching.rs b/transports/quic/src/hole_punching.rs index 605799af5e1..a38d123a6a4 100644 --- a/transports/quic/src/hole_punching.rs +++ b/transports/quic/src/hole_punching.rs @@ -20,6 +20,8 @@ pub(crate) async fn hole_puncher( match futures::future::select(P::sleep(timeout_duration), punch_holes_future).await { Either::Left(_) => Error::HandshakeTimedOut, Either::Right((Err(hole_punch_err), _)) => hole_punch_err, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right((Ok(never), _)) => match never {}, } } diff --git a/transports/quic/src/transport.rs b/transports/quic/src/transport.rs index aea3c91093f..057d0f978d7 100644 --- a/transports/quic/src/transport.rs +++ b/transports/quic/src/transport.rs @@ -31,6 +31,8 @@ use futures::{prelude::*, stream::SelectAll}; use if_watch::IfEvent; +use libp2p_core::transport::{DialOpts, PortUse}; +use libp2p_core::Endpoint; use libp2p_core::{ multiaddr::{Multiaddr, Protocol}, transport::{ListenerId, TransportError, TransportEvent}, @@ -195,6 +197,21 @@ impl GenTransport

{ Ok(socket.into()) } + + fn bound_socket(&mut self, socket_addr: SocketAddr) -> Result { + let socket_family = socket_addr.ip().into(); + if let Some(waker) = self.waker.take() { + waker.wake(); + } + let listen_socket_addr = match socket_family { + SocketFamily::Ipv4 => SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0), + SocketFamily::Ipv6 => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0), + }; + let socket = UdpSocket::bind(listen_socket_addr)?; + let endpoint_config = self.quinn_config.endpoint_config.clone(); + let endpoint = Self::new_endpoint(endpoint_config, None, socket)?; + Ok(endpoint) + } } impl Transport for GenTransport

{ @@ -247,119 +264,110 @@ impl Transport for GenTransport

{ } } - fn address_translation(&self, listen: &Multiaddr, observed: &Multiaddr) -> Option { - if !is_quic_addr(listen, self.support_draft_29) - || !is_quic_addr(observed, self.support_draft_29) - { - return None; - } - Some(observed.clone()) - } - - fn dial(&mut self, addr: Multiaddr) -> Result> { - let (socket_addr, version, _peer_id) = self.remote_multiaddr_to_socketaddr(addr, true)?; - - let endpoint = match self.eligible_listener(&socket_addr) { - None => { - // No listener. Get or create an explicit dialer. - let socket_family = socket_addr.ip().into(); - let dialer = match self.dialer.entry(socket_family) { - Entry::Occupied(occupied) => occupied.get().clone(), - Entry::Vacant(vacant) => { - if let Some(waker) = self.waker.take() { - waker.wake(); - } - let listen_socket_addr = match socket_family { - SocketFamily::Ipv4 => SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0), - SocketFamily::Ipv6 => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0), - }; - let socket = - UdpSocket::bind(listen_socket_addr).map_err(Self::Error::from)?; - let endpoint_config = self.quinn_config.endpoint_config.clone(); - let endpoint = Self::new_endpoint(endpoint_config, None, socket)?; - - vacant.insert(endpoint.clone()); - endpoint - } - }; - dialer - } - Some(listener) => listener.endpoint.clone(), - }; - let handshake_timeout = self.handshake_timeout; - let mut client_config = self.quinn_config.client_config.clone(); - if version == ProtocolVersion::Draft29 { - client_config.version(0xff00_001d); - } - Ok(Box::pin(async move { - // This `"l"` seems necessary because an empty string is an invalid domain - // name. While we don't use domain names, the underlying rustls library - // is based upon the assumption that we do. - let connecting = endpoint - .connect_with(client_config, socket_addr, "l") - .map_err(ConnectError)?; - Connecting::new(connecting, handshake_timeout).await - })) - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + dial_opts: DialOpts, ) -> Result> { - let (socket_addr, _version, peer_id) = + let (socket_addr, version, peer_id) = self.remote_multiaddr_to_socketaddr(addr.clone(), true)?; - let peer_id = peer_id.ok_or(TransportError::MultiaddrNotSupported(addr.clone()))?; - - let socket = self - .eligible_listener(&socket_addr) - .ok_or(TransportError::Other( - Error::NoActiveListenerForDialAsListener, - ))? - .try_clone_socket() - .map_err(Self::Error::from)?; - - tracing::debug!("Preparing for hole-punch from {addr}"); - - let hole_puncher = hole_puncher::

(socket, socket_addr, self.handshake_timeout); - - let (sender, receiver) = oneshot::channel(); - - match self.hole_punch_attempts.entry(socket_addr) { - Entry::Occupied(mut sender_entry) => { - // Stale senders, i.e. from failed hole punches are not removed. - // Thus, we can just overwrite a stale sender. - if !sender_entry.get().is_canceled() { - return Err(TransportError::Other(Error::HolePunchInProgress( - socket_addr, - ))); + + match (dial_opts.role, dial_opts.port_use) { + (Endpoint::Dialer, _) | (Endpoint::Listener, PortUse::Reuse) => { + let endpoint = if let Some(listener) = dial_opts + .port_use + .eq(&PortUse::Reuse) + .then(|| self.eligible_listener(&socket_addr)) + .flatten() + { + listener.endpoint.clone() + } else { + let socket_family = socket_addr.ip().into(); + let dialer = if dial_opts.port_use == PortUse::Reuse { + if let Some(occupied) = self.dialer.get(&socket_family) { + occupied.clone() + } else { + let endpoint = self.bound_socket(socket_addr)?; + self.dialer.insert(socket_family, endpoint.clone()); + endpoint + } + } else { + self.bound_socket(socket_addr)? + }; + dialer + }; + let handshake_timeout = self.handshake_timeout; + let mut client_config = self.quinn_config.client_config.clone(); + if version == ProtocolVersion::Draft29 { + client_config.version(0xff00_001d); } - sender_entry.insert(sender); - } - Entry::Vacant(entry) => { - entry.insert(sender); + Ok(Box::pin(async move { + // This `"l"` seems necessary because an empty string is an invalid domain + // name. While we don't use domain names, the underlying rustls library + // is based upon the assumption that we do. + let connecting = endpoint + .connect_with(client_config, socket_addr, "l") + .map_err(ConnectError)?; + Connecting::new(connecting, handshake_timeout).await + })) } - }; + (Endpoint::Listener, _) => { + let peer_id = peer_id.ok_or(TransportError::MultiaddrNotSupported(addr.clone()))?; + + let socket = self + .eligible_listener(&socket_addr) + .ok_or(TransportError::Other( + Error::NoActiveListenerForDialAsListener, + ))? + .try_clone_socket() + .map_err(Self::Error::from)?; + + tracing::debug!("Preparing for hole-punch from {addr}"); + + let hole_puncher = hole_puncher::

(socket, socket_addr, self.handshake_timeout); + + let (sender, receiver) = oneshot::channel(); + + match self.hole_punch_attempts.entry(socket_addr) { + Entry::Occupied(mut sender_entry) => { + // Stale senders, i.e. from failed hole punches are not removed. + // Thus, we can just overwrite a stale sender. + if !sender_entry.get().is_canceled() { + return Err(TransportError::Other(Error::HolePunchInProgress( + socket_addr, + ))); + } + sender_entry.insert(sender); + } + Entry::Vacant(entry) => { + entry.insert(sender); + } + }; - Ok(Box::pin(async move { - futures::pin_mut!(hole_puncher); - match futures::future::select(receiver, hole_puncher).await { - Either::Left((message, _)) => { - let (inbound_peer_id, connection) = message - .expect("hole punch connection sender is never dropped before receiver") - .await?; - if inbound_peer_id != peer_id { - tracing::warn!( - peer=%peer_id, - inbound_peer=%inbound_peer_id, - socket_address=%socket_addr, - "expected inbound connection from socket_address to resolve to peer but got inbound peer" - ); + Ok(Box::pin(async move { + futures::pin_mut!(hole_puncher); + match futures::future::select(receiver, hole_puncher).await { + Either::Left((message, _)) => { + let (inbound_peer_id, connection) = message + .expect( + "hole punch connection sender is never dropped before receiver", + ) + .await?; + if inbound_peer_id != peer_id { + tracing::warn!( + peer=%peer_id, + inbound_peer=%inbound_peer_id, + socket_address=%socket_addr, + "expected inbound connection from socket_address to resolve to peer but got inbound peer" + ); + } + Ok((inbound_peer_id, connection)) + } + Either::Right((hole_punch_err, _)) => Err(hole_punch_err), } - Ok((inbound_peer_id, connection)) - } - Either::Right((hole_punch_err, _)) => Err(hole_punch_err), + })) } - })) + } } fn poll( @@ -425,7 +433,7 @@ struct Listener { socket: UdpSocket, /// A future to poll new incoming connections. - accept: BoxFuture<'static, Option>, + accept: BoxFuture<'static, Option>, /// Timeout for connection establishment on inbound connections. handshake_timeout: Duration, @@ -583,10 +591,20 @@ impl Stream for Listener

{ } match self.accept.poll_unpin(cx) { - Poll::Ready(Some(connecting)) => { + Poll::Ready(Some(incoming)) => { let endpoint = self.endpoint.clone(); self.accept = async move { endpoint.accept().await }.boxed(); + let connecting = match incoming.accept() { + Ok(connecting) => connecting, + Err(error) => { + return Poll::Ready(Some(TransportEvent::ListenerError { + listener_id: self.listener_id, + error: Error::Connection(crate::ConnectionError(error)), + })) + } + }; + let local_addr = socketaddr_to_multiaddr(&self.socket_addr(), self.version); let remote_addr = connecting.remote_address(); let send_back_addr = socketaddr_to_multiaddr(&remote_addr, self.version); @@ -712,33 +730,6 @@ fn multiaddr_to_socketaddr( } } -/// Whether an [`Multiaddr`] is a valid for the QUIC transport. -fn is_quic_addr(addr: &Multiaddr, support_draft_29: bool) -> bool { - use Protocol::*; - let mut iter = addr.iter(); - let Some(first) = iter.next() else { - return false; - }; - let Some(second) = iter.next() else { - return false; - }; - let Some(third) = iter.next() else { - return false; - }; - let fourth = iter.next(); - let fifth = iter.next(); - - matches!(first, Ip4(_) | Ip6(_) | Dns(_) | Dns4(_) | Dns6(_)) - && matches!(second, Udp(_)) - && if support_draft_29 { - matches!(third, QuicV1 | Quic) - } else { - matches!(third, QuicV1) - } - && matches!(fourth, Some(P2p(_)) | None) - && fifth.is_none() -} - /// Turns an IP address and port into the corresponding QUIC multiaddr. fn socketaddr_to_multiaddr(socket_addr: &SocketAddr, version: ProtocolVersion) -> Multiaddr { let quic_proto = match version { @@ -754,10 +745,8 @@ fn socketaddr_to_multiaddr(socket_addr: &SocketAddr, version: ProtocolVersion) - #[cfg(test)] #[cfg(any(feature = "async-std", feature = "tokio"))] mod tests { - use futures::future::poll_fn; - use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; - use super::*; + use futures::future::poll_fn; #[test] fn multiaddr_to_udp_conversion() { @@ -913,7 +902,13 @@ mod tests { let mut transport = crate::tokio::Transport::new(config); let _dial = transport - .dial("/ip4/123.45.67.8/udp/1234/quic-v1".parse().unwrap()) + .dial( + "/ip4/123.45.67.8/udp/1234/quic-v1".parse().unwrap(), + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) .unwrap(); assert!(transport.dialer.contains_key(&SocketFamily::Ipv4)); diff --git a/transports/quic/tests/smoke.rs b/transports/quic/tests/smoke.rs index 36fb72a5ee7..6a760f9997c 100644 --- a/transports/quic/tests/smoke.rs +++ b/transports/quic/tests/smoke.rs @@ -7,8 +7,9 @@ use futures::stream::StreamExt; use futures::{future, AsyncReadExt, AsyncWriteExt, FutureExt, SinkExt}; use futures_timer::Delay; use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerExt, SubstreamBox}; -use libp2p_core::transport::{Boxed, OrTransport, TransportEvent}; +use libp2p_core::transport::{Boxed, DialOpts, OrTransport, PortUse, TransportEvent}; use libp2p_core::transport::{ListenerId, TransportError}; +use libp2p_core::Endpoint; use libp2p_core::{multiaddr::Protocol, upgrade, Multiaddr, Transport}; use libp2p_identity::PeerId; use libp2p_noise as noise; @@ -90,6 +91,8 @@ async fn ipv4_dial_ipv6() { #[cfg(feature = "async-std")] #[async_std::test] async fn wrapped_with_delay() { + use libp2p_core::transport::DialOpts; + let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) .try_init(); @@ -114,18 +117,14 @@ async fn wrapped_with_delay() { self.0.lock().unwrap().remove_listener(id) } - fn address_translation( - &self, - listen: &Multiaddr, - observed: &Multiaddr, - ) -> Option { - self.0.lock().unwrap().address_translation(listen, observed) - } - /// Delayed dial, i.e. calling [`Transport::dial`] on the inner [`Transport`] not within the /// synchronous [`Transport::dial`] method, but within the [`Future`] returned by the outer /// [`Transport::dial`]. - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + dial_opts: DialOpts, + ) -> Result> { let t = self.0.clone(); Ok(async move { // Simulate DNS lookup. Giving the `Transport::poll` the chance to return @@ -133,24 +132,21 @@ async fn wrapped_with_delay() { // on the inner transport below. Delay::new(Duration::from_millis(100)).await; - let dial = t.lock().unwrap().dial(addr).map_err(|e| match e { - TransportError::MultiaddrNotSupported(_) => { - panic!() - } - TransportError::Other(e) => e, - })?; + let dial = t + .lock() + .unwrap() + .dial(addr, dial_opts) + .map_err(|e| match e { + TransportError::MultiaddrNotSupported(_) => { + panic!() + } + TransportError::Other(e) => e, + })?; dial.await } .boxed()) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - self.0.lock().unwrap().dial_as_listener(addr) - } - fn poll( self: Pin<&mut Self>, cx: &mut std::task::Context<'_>, @@ -183,7 +179,15 @@ async fn wrapped_with_delay() { // Note that the dial is spawned on a different task than the transport allowing the transport // task to poll the transport once and then suspend, waiting for the wakeup from the dial. let dial = async_std::task::spawn({ - let dial = b_transport.dial(a_addr).unwrap(); + let dial = b_transport + .dial( + a_addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap(); async { dial.await.unwrap().0 } }); async_std::task::spawn(async move { b_transport.next().await }); @@ -315,7 +319,13 @@ async fn draft_29_support() { let (_, mut c_transport) = create_transport::(|cfg| cfg.support_draft_29 = false); assert!(matches!( - c_transport.dial(a_quic_addr), + c_transport.dial( + a_quic_addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::New + } + ), Err(TransportError::MultiaddrNotSupported(_)) )); @@ -331,7 +341,15 @@ async fn draft_29_support() { )); let d_quic_v1_addr = start_listening(&mut d_transport, "/ip4/127.0.0.1/udp/0/quic-v1").await; let d_quic_addr_mapped = swap_protocol!(d_quic_v1_addr, QuicV1 => Quic); - let dial = b_transport.dial(d_quic_addr_mapped).unwrap(); + let dial = b_transport + .dial( + d_quic_addr_mapped, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap(); let drive_transports = poll_fn::<(), _>(|cx| { let _ = b_transport.poll_next_unpin(cx); let _ = d_transport.poll_next_unpin(cx); @@ -408,7 +426,7 @@ async fn write_after_peer_dropped_stream() { .try_init(); let (stream_a, mut stream_b) = build_streams::().await; drop(stream_a); - futures_timer::Delay::new(Duration::from_millis(1)).await; + futures_timer::Delay::new(Duration::from_millis(100)).await; let data = vec![0; 10]; stream_b.write_all(&data).await.expect("Write failed."); @@ -765,7 +783,20 @@ async fn dial( transport: &mut Boxed<(PeerId, StreamMuxerBox)>, addr: Multiaddr, ) -> io::Result<(PeerId, StreamMuxerBox)> { - match future::select(transport.dial(addr).unwrap(), transport.next()).await { + match future::select( + transport + .dial( + addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap(), + transport.next(), + ) + .await + { Either::Left((conn, _)) => conn, Either::Right((event, _)) => { panic!("Unexpected event: {event:?}") diff --git a/transports/quic/tests/stream_compliance.rs b/transports/quic/tests/stream_compliance.rs index 0eff0584588..b0536473215 100644 --- a/transports/quic/tests/stream_compliance.rs +++ b/transports/quic/tests/stream_compliance.rs @@ -1,7 +1,7 @@ use futures::channel::oneshot; use futures::StreamExt; -use libp2p_core::transport::ListenerId; -use libp2p_core::Transport; +use libp2p_core::transport::{DialOpts, ListenerId, PortUse}; +use libp2p_core::{Endpoint, Transport}; use libp2p_quic as quic; use std::time::Duration; @@ -47,7 +47,15 @@ async fn connected_peers() -> (quic::Connection, quic::Connection) { listener.next().await; } }); - let dial_fut = dialer.dial(listen_address).unwrap(); + let dial_fut = dialer + .dial( + listen_address, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap(); async_std::task::spawn(async move { let connection = dial_fut.await.unwrap().1; diff --git a/transports/tcp/CHANGELOG.md b/transports/tcp/CHANGELOG.md index 2bde64056cb..107d0d13ece 100644 --- a/transports/tcp/CHANGELOG.md +++ b/transports/tcp/CHANGELOG.md @@ -1,3 +1,17 @@ +## 0.42.0 + +- Implement refactored `Transport`. + See [PR 4568] +- Deprecate `port_reuse` setting, as this is now decided by the behaviour, not the transport. + See [PR 4568] + +[PR 4568]: https://github.com/libp2p/rust-libp2p/pull/4568 + +## 0.41.1 + +- Disable Nagle's algorithm (i.e. `TCP_NODELAY`) by default. + See [PR 4916](https://github.com/libp2p/rust-libp2p/pull/4916) + ## 0.41.0 diff --git a/transports/tcp/Cargo.toml b/transports/tcp/Cargo.toml index 6fd4aec73fe..03e7fac491c 100644 --- a/transports/tcp/Cargo.toml +++ b/transports/tcp/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-tcp" edition = "2021" rust-version = { workspace = true } description = "TCP/IP transport protocol for libp2p" -version = "0.41.0" +version = "0.42.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -11,16 +11,16 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] -async-io = { version = "2.3.2", optional = true } -futures = "0.3.30" +async-io = { version = "2.3.3", optional = true } +futures = { workspace = true } futures-timer = "3.0" if-watch = "3.2.0" -libc = "0.2.153" +libc = "0.2.155" libp2p-core = { workspace = true } libp2p-identity = { workspace = true } -socket2 = { version = "0.5.6", features = ["all"] } -tokio = { version = "1.36.0", default-features = false, features = ["net"], optional = true } -tracing = "0.1.37" +socket2 = { version = "0.5.7", features = ["all"] } +tokio = { workspace = true, default-features = false, features = ["net"], optional = true } +tracing = { workspace = true } [features] tokio = ["dep:tokio", "if-watch/tokio"] @@ -29,16 +29,14 @@ async-io = ["dep:async-io", "if-watch/smol"] [dev-dependencies] async-std = { version = "1.6.5", features = ["attributes"] } libp2p-identity = { workspace = true, features = ["rand"] } -tokio = { version = "1.36.0", default-features = false, features = ["full"] } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tokio = { workspace = true, features = ["full"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/transports/tcp/src/lib.rs b/transports/tcp/src/lib.rs index fbb7008aa5b..4c4fa7c6b84 100644 --- a/transports/tcp/src/lib.rs +++ b/transports/tcp/src/lib.rs @@ -36,17 +36,12 @@ pub use provider::async_io; #[cfg(feature = "tokio")] pub use provider::tokio; -use futures::{ - future::{self, Ready}, - prelude::*, - stream::SelectAll, -}; +use futures::{future::Ready, prelude::*, stream::SelectAll}; use futures_timer::Delay; use if_watch::IfEvent; use libp2p_core::{ - address_translation, multiaddr::{Multiaddr, Protocol}, - transport::{ListenerId, TransportError, TransportEvent}, + transport::{DialOpts, ListenerId, PortUse, TransportError, TransportEvent}, }; use provider::{Incoming, Provider}; use socket2::{Domain, Socket, Type}; @@ -69,27 +64,16 @@ pub struct Config { nodelay: Option, /// Size of the listen backlog for listen sockets. backlog: u32, - /// Whether port reuse should be enabled. - enable_port_reuse: bool, } type Port = u16; /// The configuration for port reuse of listening sockets. -#[derive(Debug, Clone)] -enum PortReuse { - /// Port reuse is disabled, i.e. ephemeral local ports are - /// used for outgoing TCP connections. - Disabled, - /// Port reuse when dialing is enabled, i.e. the local - /// address and port that a new socket for an outgoing - /// connection is bound to are chosen from an existing - /// listening socket, if available. - Enabled { - /// The addresses and ports of the listening sockets - /// registered as eligible for port reuse when dialing. - listen_addrs: Arc>>, - }, +#[derive(Debug, Clone, Default)] +struct PortReuse { + /// The addresses and ports of the listening sockets + /// registered as eligible for port reuse when dialing + listen_addrs: Arc>>, } impl PortReuse { @@ -97,26 +81,22 @@ impl PortReuse { /// /// Has no effect if port reuse is disabled. fn register(&mut self, ip: IpAddr, port: Port) { - if let PortReuse::Enabled { listen_addrs } = self { - tracing::trace!(%ip, %port, "Registering for port reuse"); - listen_addrs - .write() - .expect("`register()` and `unregister()` never panic while holding the lock") - .insert((ip, port)); - } + tracing::trace!(%ip, %port, "Registering for port reuse"); + self.listen_addrs + .write() + .expect("`register()` and `unregister()` never panic while holding the lock") + .insert((ip, port)); } /// Unregisters a socket address for port reuse. /// /// Has no effect if port reuse is disabled. fn unregister(&mut self, ip: IpAddr, port: Port) { - if let PortReuse::Enabled { listen_addrs } = self { - tracing::trace!(%ip, %port, "Unregistering for port reuse"); - listen_addrs - .write() - .expect("`register()` and `unregister()` never panic while holding the lock") - .remove(&(ip, port)); - } + tracing::trace!(%ip, %port, "Unregistering for port reuse"); + self.listen_addrs + .write() + .expect("`register()` and `unregister()` never panic while holding the lock") + .remove(&(ip, port)); } /// Selects a listening socket address suitable for use @@ -129,20 +109,17 @@ impl PortReuse { /// Returns `None` if port reuse is disabled or no suitable /// listening socket address is found. fn local_dial_addr(&self, remote_ip: &IpAddr) -> Option { - if let PortReuse::Enabled { listen_addrs } = self { - for (ip, port) in listen_addrs - .read() - .expect("`local_dial_addr` never panic while holding the lock") - .iter() - { - if ip.is_ipv4() == remote_ip.is_ipv4() - && ip.is_loopback() == remote_ip.is_loopback() - { - if remote_ip.is_ipv4() { - return Some(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), *port)); - } else { - return Some(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), *port)); - } + for (ip, port) in self + .listen_addrs + .read() + .expect("`local_dial_addr` never panic while holding the lock") + .iter() + { + if ip.is_ipv4() == remote_ip.is_ipv4() && ip.is_loopback() == remote_ip.is_loopback() { + if remote_ip.is_ipv4() { + return Some(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), *port)); + } else { + return Some(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), *port)); } } } @@ -165,9 +142,8 @@ impl Config { pub fn new() -> Self { Self { ttl: None, - nodelay: None, + nodelay: Some(false), // Disable Nagle's algorithm by default backlog: 1024, - enable_port_reuse: false, } } @@ -193,101 +169,55 @@ impl Config { /// reuse of listening ports for outgoing connections to /// enhance NAT traversal capabilities. /// - /// Please refer to e.g. [RFC 4787](https://tools.ietf.org/html/rfc4787) - /// section 4 and 5 for some of the NAT terminology used here. - /// - /// There are two main use-cases for port reuse among local - /// sockets: - /// - /// 1. Creating multiple listening sockets for the same address - /// and port to allow accepting connections on multiple threads - /// without having to synchronise access to a single listen socket. - /// - /// 2. Creating outgoing connections whose local socket is bound to - /// the same address and port as a listening socket. In the rare - /// case of simple NATs with both endpoint-independent mapping and - /// endpoint-independent filtering, this can on its own already - /// permit NAT traversal by other nodes sharing the observed - /// external address of the local node. For the common case of - /// NATs with address-dependent or address and port-dependent - /// filtering, port reuse for outgoing connections can facilitate - /// further TCP hole punching techniques for NATs that perform - /// endpoint-independent mapping. Port reuse cannot facilitate - /// NAT traversal in the presence of "symmetric" NATs that employ - /// both address/port-dependent mapping and filtering, unless - /// there is some means of port prediction. - /// - /// Both use-cases are enabled when port reuse is enabled, with port reuse - /// for outgoing connections (`2.` above) always being implied. - /// - /// > **Note**: Due to the identification of a TCP socket by a 4-tuple - /// > of source IP address, source port, destination IP address and - /// > destination port, with port reuse enabled there can be only - /// > a single outgoing connection to a particular address and port - /// > of a peer per local listening socket address. - /// - /// [`Transport`] keeps track of the listen socket addresses as they - /// are reported by polling it. It is possible to listen on multiple - /// addresses, enabling port reuse for each, knowing exactly which listen - /// address is reused when dialing with a specific [`Transport`], as in the - /// following example: - /// - /// ```no_run - /// # use futures::StreamExt; - /// # use libp2p_core::transport::{ListenerId, TransportEvent}; - /// # use libp2p_core::{Multiaddr, Transport}; - /// # use std::pin::Pin; - /// # #[cfg(not(feature = "async-io"))] - /// # fn main() {} - /// # - /// #[cfg(feature = "async-io")] - /// #[async_std::main] - /// async fn main() -> std::io::Result<()> { - /// - /// let listen_addr1: Multiaddr = "/ip4/127.0.0.1/tcp/9001".parse().unwrap(); - /// let listen_addr2: Multiaddr = "/ip4/127.0.0.1/tcp/9002".parse().unwrap(); + /// # Deprecation Notice /// - /// let mut tcp1 = libp2p_tcp::async_io::Transport::new(libp2p_tcp::Config::new().port_reuse(true)).boxed(); - /// tcp1.listen_on(ListenerId::next(), listen_addr1.clone()).expect("listener"); - /// match tcp1.select_next_some().await { - /// TransportEvent::NewAddress { listen_addr, .. } => { - /// println!("Listening on {:?}", listen_addr); - /// let mut stream = tcp1.dial(listen_addr2.clone()).unwrap().await?; - /// // `stream` has `listen_addr1` as its local socket address. - /// } - /// _ => {} - /// } + /// The new implementation works on a per-connaction basis, defined by the behaviour. This + /// removes the necessaity to configure the transport for port reuse, instead the behaviour + /// requiring this behaviour can decide whether to use port reuse or not. /// - /// let mut tcp2 = libp2p_tcp::async_io::Transport::new(libp2p_tcp::Config::new().port_reuse(true)).boxed(); - /// tcp2.listen_on(ListenerId::next(), listen_addr2).expect("listener"); - /// match tcp2.select_next_some().await { - /// TransportEvent::NewAddress { listen_addr, .. } => { - /// println!("Listening on {:?}", listen_addr); - /// let mut socket = tcp2.dial(listen_addr1).unwrap().await?; - /// // `stream` has `listen_addr2` as its local socket address. - /// } - /// _ => {} - /// } - /// Ok(()) - /// } - /// ``` + /// The API to configure port reuse is part of [`Transport`] and the option can be found in + /// [`libp2p_core::transport::DialOpts`]. /// - /// If a wildcard listen socket address is used to listen on any interface, - /// there can be multiple such addresses registered for port reuse. In this - /// case, one is chosen whose IP protocol version and loopback status is the - /// same as that of the remote address. Consequently, for maximum control of - /// the local listening addresses and ports that are used for outgoing - /// connections, a new [`Transport`] should be created for each listening - /// socket, avoiding the use of wildcard addresses which bind a socket to - /// all network interfaces. - /// - /// When this option is enabled on a unix system, the socket - /// option `SO_REUSEPORT` is set, if available, to permit - /// reuse of listening ports for multiple sockets. - pub fn port_reuse(mut self, port_reuse: bool) -> Self { - self.enable_port_reuse = port_reuse; + /// If [`PortUse::Reuse`] is enabled, the transport will try to reuse the local port of the + /// listener. If that's not possible, i.e. there is no listener or the transport doesn't allow + /// a direct control over ports, a new port (or the default behaviour) is used. If port reuse + /// is enabled for a connection, this option will be treated on a best-effor basis. + #[deprecated( + since = "0.42.0", + note = "This option does nothing now, since the port reuse policy is now decided on a per-connection basis by the behaviour. The function will be removed in a future release." + )] + pub fn port_reuse(self, _port_reuse: bool) -> Self { self } + + fn create_socket(&self, socket_addr: SocketAddr, port_use: PortUse) -> io::Result { + let socket = Socket::new( + Domain::for_address(socket_addr), + Type::STREAM, + Some(socket2::Protocol::TCP), + )?; + if socket_addr.is_ipv6() { + socket.set_only_v6(true)?; + } + if let Some(ttl) = self.ttl { + socket.set_ttl(ttl)?; + } + if let Some(nodelay) = self.nodelay { + socket.set_nodelay(nodelay)?; + } + socket.set_reuse_address(true)?; + #[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))] + if port_use == PortUse::Reuse { + socket.set_reuse_port(true)?; + } + + #[cfg(not(all(unix, not(any(target_os = "solaris", target_os = "illumos")))))] + let _ = port_use; // silence the unused warning on non-unix platforms (i.e. Windows) + + socket.set_nonblocking(true)?; + + Ok(socket) + } } impl Default for Config { @@ -332,49 +262,18 @@ where /// - [`tokio::Transport::new`] /// - [`async_io::Transport::new`] pub fn new(config: Config) -> Self { - let port_reuse = if config.enable_port_reuse { - PortReuse::Enabled { - listen_addrs: Arc::new(RwLock::new(HashSet::new())), - } - } else { - PortReuse::Disabled - }; Transport { config, - port_reuse, ..Default::default() } } - fn create_socket(&self, socket_addr: SocketAddr) -> io::Result { - let socket = Socket::new( - Domain::for_address(socket_addr), - Type::STREAM, - Some(socket2::Protocol::TCP), - )?; - if socket_addr.is_ipv6() { - socket.set_only_v6(true)?; - } - if let Some(ttl) = self.config.ttl { - socket.set_ttl(ttl)?; - } - if let Some(nodelay) = self.config.nodelay { - socket.set_nodelay(nodelay)?; - } - socket.set_reuse_address(true)?; - #[cfg(unix)] - if let PortReuse::Enabled { .. } = &self.port_reuse { - socket.set_reuse_port(true)?; - } - Ok(socket) - } - fn do_listen( &mut self, id: ListenerId, socket_addr: SocketAddr, ) -> io::Result> { - let socket = self.create_socket(socket_addr)?; + let socket = self.config.create_socket(socket_addr, PortUse::Reuse)?; socket.bind(&socket_addr.into())?; socket.listen(self.config.backlog as _)?; socket.set_nonblocking(true)?; @@ -408,17 +307,9 @@ where /// /// This transport will have port-reuse disabled. fn default() -> Self { - let config = Config::default(); - let port_reuse = if config.enable_port_reuse { - PortReuse::Enabled { - listen_addrs: Arc::new(RwLock::new(HashSet::new())), - } - } else { - PortReuse::Disabled - }; Transport { - port_reuse, - config, + port_reuse: PortReuse::default(), + config: Config::default(), listeners: SelectAll::new(), pending_events: VecDeque::new(), } @@ -460,7 +351,11 @@ where } } - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + opts: DialOpts, + ) -> Result> { let socket_addr = if let Ok(socket_addr) = multiaddr_to_socketaddr(addr.clone()) { if socket_addr.port() == 0 || socket_addr.ip().is_unspecified() { return Err(TransportError::MultiaddrNotSupported(addr)); @@ -472,26 +367,45 @@ where tracing::debug!(address=%socket_addr, "dialing address"); let socket = self - .create_socket(socket_addr) + .config + .create_socket(socket_addr, opts.port_use) .map_err(TransportError::Other)?; - if let Some(addr) = self.port_reuse.local_dial_addr(&socket_addr.ip()) { - tracing::trace!(address=%addr, "Binding dial socket to listen socket address"); - socket.bind(&addr.into()).map_err(TransportError::Other)?; - } + let bind_addr = match self.port_reuse.local_dial_addr(&socket_addr.ip()) { + Some(socket_addr) if opts.port_use == PortUse::Reuse => { + tracing::trace!(address=%addr, "Binding dial socket to listen socket address"); + Some(socket_addr) + } + _ => None, + }; - socket - .set_nonblocking(true) - .map_err(TransportError::Other)?; + let local_config = self.config.clone(); Ok(async move { + if let Some(bind_addr) = bind_addr { + socket.bind(&bind_addr.into())?; + } + // [`Transport::dial`] should do no work unless the returned [`Future`] is polled. Thus // do the `connect` call within the [`Future`]. - match socket.connect(&socket_addr.into()) { - Ok(()) => {} - Err(err) if err.raw_os_error() == Some(libc::EINPROGRESS) => {} - Err(err) if err.kind() == io::ErrorKind::WouldBlock => {} - Err(err) => return Err(err), + let socket = match (socket.connect(&socket_addr.into()), bind_addr) { + (Ok(()), _) => socket, + (Err(err), _) if err.raw_os_error() == Some(libc::EINPROGRESS) => socket, + (Err(err), _) if err.kind() == io::ErrorKind::WouldBlock => socket, + (Err(err), Some(bind_addr)) if err.kind() == io::ErrorKind::AddrNotAvailable => { + // The socket was bound to a local address that is no longer available. + // Retry without binding. + tracing::debug!(connect_addr = %socket_addr, ?bind_addr, "Failed to connect using existing socket because we already have a connection, re-dialing with new port"); + std::mem::drop(socket); + let socket = local_config.create_socket(socket_addr, PortUse::New)?; + match socket.connect(&socket_addr.into()) { + Ok(()) => socket, + Err(err) if err.raw_os_error() == Some(libc::EINPROGRESS) => socket, + Err(err) if err.kind() == io::ErrorKind::WouldBlock => socket, + Err(err) => return Err(err), + } + } + (Err(err), _) => return Err(err), }; let stream = T::new_stream(socket.into()).await?; @@ -500,40 +414,6 @@ where .boxed()) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - self.dial(addr) - } - - /// When port reuse is disabled and hence ephemeral local ports are - /// used for outgoing connections, the returned address is the - /// `observed` address with the port replaced by the port of the - /// `listen` address. - /// - /// If port reuse is enabled, `Some(observed)` is returned, as there - /// is a chance that the `observed` address _and_ port are reachable - /// for other peers if there is a NAT in the way that does endpoint- - /// independent filtering. Furthermore, even if that is not the case - /// and TCP hole punching techniques must be used for NAT traversal, - /// the `observed` address is still the one that a remote should connect - /// to for the purpose of the hole punching procedure, as it represents - /// the mapped IP and port of the NAT device in front of the local - /// node. - /// - /// `None` is returned if one of the given addresses is not a TCP/IP - /// address. - fn address_translation(&self, listen: &Multiaddr, observed: &Multiaddr) -> Option { - if !is_tcp_addr(listen) || !is_tcp_addr(observed) { - return None; - } - match &self.port_reuse { - PortReuse::Disabled => address_translation(listen, observed), - PortReuse::Enabled { .. } => Some(observed.clone()), - } - } - /// Poll all listeners. #[tracing::instrument(level = "trace", name = "Transport::poll", skip(self, cx))] fn poll( @@ -823,23 +703,6 @@ fn ip_to_multiaddr(ip: IpAddr, port: u16) -> Multiaddr { Multiaddr::empty().with(ip.into()).with(Protocol::Tcp(port)) } -fn is_tcp_addr(addr: &Multiaddr) -> bool { - use Protocol::*; - - let mut iter = addr.iter(); - - let first = match iter.next() { - None => return false, - Some(p) => p, - }; - let second = match iter.next() { - None => return false, - Some(p) => p, - }; - - matches!(first, Ip4(_) | Ip6(_) | Dns(_) | Dns4(_) | Dns6(_)) && matches!(second, Tcp(_)) -} - #[cfg(test)] mod tests { use super::*; @@ -847,8 +710,8 @@ mod tests { channel::{mpsc, oneshot}, future::poll_fn, }; + use libp2p_core::Endpoint; use libp2p_core::Transport as _; - use libp2p_identity::PeerId; #[test] fn multiaddr_to_tcp_conversion() { @@ -931,7 +794,17 @@ mod tests { let mut tcp = Transport::::default(); // Obtain a future socket through dialing - let mut socket = tcp.dial(addr.clone()).unwrap().await.unwrap(); + let mut socket = tcp + .dial( + addr.clone(), + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap() + .await + .unwrap(); socket.write_all(&[0x1, 0x2, 0x3]).await.unwrap(); let mut buf = [0u8; 3]; @@ -1007,7 +880,16 @@ mod tests { async fn dialer(mut ready_rx: mpsc::Receiver) { let dest_addr = ready_rx.next().await.unwrap(); let mut tcp = Transport::::default(); - tcp.dial(dest_addr).unwrap().await.unwrap(); + tcp.dial( + dest_addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::New, + }, + ) + .unwrap() + .await + .unwrap(); } fn test(addr: Multiaddr) { @@ -1087,7 +969,7 @@ mod tests { port_reuse_tx: oneshot::Sender>, ) { let dest_addr = ready_rx.next().await.unwrap(); - let mut tcp = Transport::::new(Config::new().port_reuse(true)); + let mut tcp = Transport::::new(Config::new()); tcp.listen_on(ListenerId::next(), addr).unwrap(); match poll_fn(|cx| Pin::new(&mut tcp).poll(cx)).await { TransportEvent::NewAddress { .. } => { @@ -1106,7 +988,17 @@ mod tests { .ok(); // Obtain a future socket through dialing - let mut socket = tcp.dial(dest_addr).unwrap().await.unwrap(); + let mut socket = tcp + .dial( + dest_addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap() + .await + .unwrap(); socket.write_all(&[0x1, 0x2, 0x3]).await.unwrap(); // socket.flush().await; let mut buf = [0u8; 3]; @@ -1157,7 +1049,7 @@ mod tests { .try_init(); async fn listen_twice(addr: Multiaddr) { - let mut tcp = Transport::::new(Config::new().port_reuse(true)); + let mut tcp = Transport::::new(Config::new()); tcp.listen_on(ListenerId::next(), addr).unwrap(); match poll_fn(|cx| Pin::new(&mut tcp).poll(cx)).await { TransportEvent::NewAddress { @@ -1266,55 +1158,6 @@ mod tests { test("/ip4/127.0.0.1/tcp/12345/tcp/12345".parse().unwrap()); } - #[cfg(feature = "async-io")] - #[test] - fn test_address_translation_async_io() { - test_address_translation::() - } - - #[cfg(feature = "tokio")] - #[test] - fn test_address_translation_tokio() { - test_address_translation::() - } - - fn test_address_translation() - where - T: Default + libp2p_core::Transport, - { - let transport = T::default(); - - let port = 42; - let tcp_listen_addr = Multiaddr::empty() - .with(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1))) - .with(Protocol::Tcp(port)); - let observed_ip = Ipv4Addr::new(123, 45, 67, 8); - let tcp_observed_addr = Multiaddr::empty() - .with(Protocol::Ip4(observed_ip)) - .with(Protocol::Tcp(1)) - .with(Protocol::P2p(PeerId::random())); - - let translated = transport - .address_translation(&tcp_listen_addr, &tcp_observed_addr) - .unwrap(); - let mut iter = translated.iter(); - assert_eq!(iter.next(), Some(Protocol::Ip4(observed_ip))); - assert_eq!(iter.next(), Some(Protocol::Tcp(port))); - assert_eq!(iter.next(), None); - - let quic_addr = Multiaddr::empty() - .with(Protocol::Ip4(Ipv4Addr::new(87, 65, 43, 21))) - .with(Protocol::Udp(1)) - .with(Protocol::QuicV1); - - assert!(transport - .address_translation(&tcp_listen_addr, &quic_addr) - .is_none()); - assert!(transport - .address_translation(&quic_addr, &tcp_observed_addr) - .is_none()); - } - #[test] fn test_remove_listener() { let _ = tracing_subscriber::fmt() @@ -1377,7 +1220,7 @@ mod tests { .build() .unwrap(); rt.block_on(async { - test::(); + test::(); }); } } diff --git a/transports/tcp/src/provider/tokio.rs b/transports/tcp/src/provider/tokio.rs index b991c6bdae1..ec2d098e3fb 100644 --- a/transports/tcp/src/provider/tokio.rs +++ b/transports/tcp/src/provider/tokio.rs @@ -24,7 +24,6 @@ use futures::{ future::{BoxFuture, FutureExt}, prelude::*, }; -use std::convert::TryFrom; use std::io; use std::net; use std::pin::Pin; diff --git a/transports/tls/CHANGELOG.md b/transports/tls/CHANGELOG.md index 83f72286559..e27b8b4cf1f 100644 --- a/transports/tls/CHANGELOG.md +++ b/transports/tls/CHANGELOG.md @@ -1,3 +1,16 @@ +## 0.5.0 + + + +## 0.4.1 + +- Fix a panic caused by `rustls` parsing the libp2p TLS extension. + See [PR 5498](https://github.com/libp2p/rust-libp2p/pull/5498). + +## 0.4.0 + +- Upgrade `rustls` to `0.23`. See [PR 5385](https://github.com/libp2p/rust-libp2p/pull/5385) + ## 0.3.0 - Migrate to `{In,Out}boundConnectionUpgrade` traits. diff --git a/transports/tls/Cargo.toml b/transports/tls/Cargo.toml index 0ca134d418b..c27e14bb537 100644 --- a/transports/tls/Cargo.toml +++ b/transports/tls/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-tls" -version = "0.3.0" +version = "0.5.0" edition = "2021" rust-version = { workspace = true } description = "TLS configuration based on libp2p TLS specs." @@ -9,22 +9,23 @@ license = "MIT" exclude = ["src/test_assets"] [dependencies] -futures = { version = "0.3.30", default-features = false } -futures-rustls = "0.24.0" +futures = { workspace = true } +futures-rustls = { workspace = true } libp2p-core = { workspace = true } libp2p-identity = { workspace = true } -rcgen = "0.11.3" -ring = "0.16.20" -thiserror = "1.0.57" +rcgen = { workspace = true } +ring = { workspace = true } +thiserror = "1.0.61" webpki = { version = "0.101.4", package = "rustls-webpki", features = ["std"] } -x509-parser = "0.15.1" +x509-parser = "0.16.0" yasna = "0.5.2" # Exposed dependencies. Breaking changes to these are breaking changes to us. [dependencies.rustls] -version = "0.21.9" +version = "0.23.9" default-features = false -features = ["dangerous_configuration"] # Must enable this to allow for custom verification code. +features = ["ring", "std"] # Must enable this to allow for custom verification code. + [dev-dependencies] hex = "0.4.3" @@ -33,14 +34,12 @@ libp2p-core = { workspace = true } libp2p-identity = { workspace = true, features = ["ed25519", "rsa", "secp256k1", "ecdsa", "rand"] } libp2p-swarm = { workspace = true, features = ["tokio"] } libp2p-yamux = { workspace = true } -tokio = { version = "1.36.0", features = ["full"] } +tokio = { workspace = true, features = ["full"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/transports/tls/src/certificate.rs b/transports/tls/src/certificate.rs index 801ba3fe3ce..65b373bcf9b 100644 --- a/transports/tls/src/certificate.rs +++ b/transports/tls/src/certificate.rs @@ -26,6 +26,8 @@ use libp2p_identity as identity; use libp2p_identity::PeerId; use x509_parser::{prelude::*, signature_algorithm::SignatureAlgorithm}; +use std::sync::Arc; + /// The libp2p Public Key Extension is a X.509 extension /// with the Object Identifier 1.3.6.1.4.1.53594.1.1, /// allocated by IANA to the libp2p project at Protocol Labs. @@ -42,18 +44,65 @@ const P2P_SIGNING_PREFIX: [u8; 21] = *b"libp2p-tls-handshake:"; // Similarly, hash functions with an output length less than 256 bits MUST NOT be used. static P2P_SIGNATURE_ALGORITHM: &rcgen::SignatureAlgorithm = &rcgen::PKCS_ECDSA_P256_SHA256; +#[derive(Debug)] +pub(crate) struct AlwaysResolvesCert(Arc); + +impl AlwaysResolvesCert { + pub(crate) fn new( + cert: rustls::pki_types::CertificateDer<'static>, + key: &rustls::pki_types::PrivateKeyDer<'_>, + ) -> Result { + let certified_key = rustls::sign::CertifiedKey::new( + vec![cert], + rustls::crypto::ring::sign::any_ecdsa_type(key)?, + ); + Ok(Self(Arc::new(certified_key))) + } +} + +impl rustls::client::ResolvesClientCert for AlwaysResolvesCert { + fn resolve( + &self, + _root_hint_subjects: &[&[u8]], + _sigschemes: &[rustls::SignatureScheme], + ) -> Option> { + Some(Arc::clone(&self.0)) + } + + fn has_certs(&self) -> bool { + true + } +} + +impl rustls::server::ResolvesServerCert for AlwaysResolvesCert { + fn resolve( + &self, + _client_hello: rustls::server::ClientHello<'_>, + ) -> Option> { + Some(Arc::clone(&self.0)) + } +} + /// Generates a self-signed TLS certificate that includes a libp2p-specific /// certificate extension containing the public key of the given keypair. pub fn generate( identity_keypair: &identity::Keypair, -) -> Result<(rustls::Certificate, rustls::PrivateKey), GenError> { +) -> Result< + ( + rustls::pki_types::CertificateDer<'static>, + rustls::pki_types::PrivateKeyDer<'static>, + ), + GenError, +> { // Keypair used to sign the certificate. // SHOULD NOT be related to the host's key. // Endpoints MAY generate a new key and certificate // for every connection attempt, or they MAY reuse the same key // and certificate for multiple connections. let certificate_keypair = rcgen::KeyPair::generate(P2P_SIGNATURE_ALGORITHM)?; - let rustls_key = rustls::PrivateKey(certificate_keypair.serialize_der()); + let rustls_key = rustls::pki_types::PrivateKeyDer::from( + rustls::pki_types::PrivatePkcs8KeyDer::from(certificate_keypair.serialize_der()), + ); let certificate = { let mut params = rcgen::CertificateParams::new(vec![]); @@ -67,7 +116,7 @@ pub fn generate( rcgen::Certificate::from_params(params)? }; - let rustls_certificate = rustls::Certificate(certificate.serialize_der()?); + let rustls_certificate = rustls::pki_types::CertificateDer::from(certificate.serialize_der()?); Ok((rustls_certificate, rustls_key)) } @@ -76,7 +125,9 @@ pub fn generate( /// /// For this to succeed, the certificate must contain the specified extension and the signature must /// match the embedded public key. -pub fn parse(certificate: &rustls::Certificate) -> Result, ParseError> { +pub fn parse<'a>( + certificate: &'a rustls::pki_types::CertificateDer<'a>, +) -> Result, ParseError> { let certificate = parse_unverified(certificate.as_ref())?; certificate.verify()?; @@ -481,7 +532,9 @@ mod tests { #[test] fn rsa_pss_sha384() { - let cert = rustls::Certificate(include_bytes!("./test_assets/rsa_pss_sha384.der").to_vec()); + let cert = rustls::pki_types::CertificateDer::from( + include_bytes!("./test_assets/rsa_pss_sha384.der").to_vec(), + ); let cert = parse(&cert).unwrap(); @@ -502,7 +555,7 @@ mod tests { #[test] fn can_parse_certificate_with_ed25519_keypair() { - let certificate = rustls::Certificate(hex!("308201773082011ea003020102020900f5bd0debaa597f52300a06082a8648ce3d04030230003020170d3735303130313030303030305a180f34303936303130313030303030305a30003059301306072a8648ce3d020106082a8648ce3d030107034200046bf9871220d71dcb3483ecdfcbfcc7c103f8509d0974b3c18ab1f1be1302d643103a08f7a7722c1b247ba3876fe2c59e26526f479d7718a85202ddbe47562358a37f307d307b060a2b0601040183a25a01010101ff046a30680424080112207fda21856709c5ae12fd6e8450623f15f11955d384212b89f56e7e136d2e17280440aaa6bffabe91b6f30c35e3aa4f94b1188fed96b0ffdd393f4c58c1c047854120e674ce64c788406d1c2c4b116581fd7411b309881c3c7f20b46e54c7e6fe7f0f300a06082a8648ce3d040302034700304402207d1a1dbd2bda235ff2ec87daf006f9b04ba076a5a5530180cd9c2e8f6399e09d0220458527178c7e77024601dbb1b256593e9b96d961b96349d1f560114f61a87595").to_vec()); + let certificate = rustls::pki_types::CertificateDer::from(hex!("308201773082011ea003020102020900f5bd0debaa597f52300a06082a8648ce3d04030230003020170d3735303130313030303030305a180f34303936303130313030303030305a30003059301306072a8648ce3d020106082a8648ce3d030107034200046bf9871220d71dcb3483ecdfcbfcc7c103f8509d0974b3c18ab1f1be1302d643103a08f7a7722c1b247ba3876fe2c59e26526f479d7718a85202ddbe47562358a37f307d307b060a2b0601040183a25a01010101ff046a30680424080112207fda21856709c5ae12fd6e8450623f15f11955d384212b89f56e7e136d2e17280440aaa6bffabe91b6f30c35e3aa4f94b1188fed96b0ffdd393f4c58c1c047854120e674ce64c788406d1c2c4b116581fd7411b309881c3c7f20b46e54c7e6fe7f0f300a06082a8648ce3d040302034700304402207d1a1dbd2bda235ff2ec87daf006f9b04ba076a5a5530180cd9c2e8f6399e09d0220458527178c7e77024601dbb1b256593e9b96d961b96349d1f560114f61a87595").to_vec()); let peer_id = parse(&certificate).unwrap().peer_id(); @@ -516,7 +569,7 @@ mod tests { #[test] fn fails_to_parse_bad_certificate_with_ed25519_keypair() { - let certificate = rustls::Certificate(hex!("308201773082011da003020102020830a73c5d896a1109300a06082a8648ce3d04030230003020170d3735303130313030303030305a180f34303936303130313030303030305a30003059301306072a8648ce3d020106082a8648ce3d03010703420004bbe62df9a7c1c46b7f1f21d556deec5382a36df146fb29c7f1240e60d7d5328570e3b71d99602b77a65c9b3655f62837f8d66b59f1763b8c9beba3be07778043a37f307d307b060a2b0601040183a25a01010101ff046a3068042408011220ec8094573afb9728088860864f7bcea2d4fd412fef09a8e2d24d482377c20db60440ecabae8354afa2f0af4b8d2ad871e865cb5a7c0c8d3dbdbf42de577f92461a0ebb0a28703e33581af7d2a4f2270fc37aec6261fcc95f8af08f3f4806581c730a300a06082a8648ce3d040302034800304502202dfb17a6fa0f94ee0e2e6a3b9fb6e986f311dee27392058016464bd130930a61022100ba4b937a11c8d3172b81e7cd04aedb79b978c4379c2b5b24d565dd5d67d3cb3c").to_vec()); + let certificate = rustls::pki_types::CertificateDer::from(hex!("308201773082011da003020102020830a73c5d896a1109300a06082a8648ce3d04030230003020170d3735303130313030303030305a180f34303936303130313030303030305a30003059301306072a8648ce3d020106082a8648ce3d03010703420004bbe62df9a7c1c46b7f1f21d556deec5382a36df146fb29c7f1240e60d7d5328570e3b71d99602b77a65c9b3655f62837f8d66b59f1763b8c9beba3be07778043a37f307d307b060a2b0601040183a25a01010101ff046a3068042408011220ec8094573afb9728088860864f7bcea2d4fd412fef09a8e2d24d482377c20db60440ecabae8354afa2f0af4b8d2ad871e865cb5a7c0c8d3dbdbf42de577f92461a0ebb0a28703e33581af7d2a4f2270fc37aec6261fcc95f8af08f3f4806581c730a300a06082a8648ce3d040302034800304502202dfb17a6fa0f94ee0e2e6a3b9fb6e986f311dee27392058016464bd130930a61022100ba4b937a11c8d3172b81e7cd04aedb79b978c4379c2b5b24d565dd5d67d3cb3c").to_vec()); let error = parse(&certificate).unwrap_err(); @@ -525,7 +578,7 @@ mod tests { #[test] fn can_parse_certificate_with_ecdsa_keypair() { - let certificate = rustls::Certificate(hex!("308201c030820166a003020102020900eaf419a6e3edb4a6300a06082a8648ce3d04030230003020170d3735303130313030303030305a180f34303936303130313030303030305a30003059301306072a8648ce3d020106082a8648ce3d030107034200048dbf1116c7c608d6d5292bd826c3feb53483a89fce434bf64538a359c8e07538ff71f6766239be6a146dcc1a5f3bb934bcd4ae2ae1d4da28ac68b4a20593f06ba381c63081c33081c0060a2b0601040183a25a01010101ff0481ae3081ab045f0803125b3059301306072a8648ce3d020106082a8648ce3d0301070342000484b93fa456a74bd0153919f036db7bc63c802f055bc7023395d0203de718ee0fc7b570b767cdd858aca6c7c4113ff002e78bd2138ac1a3b26dde3519e06979ad04483046022100bc84014cea5a41feabdf4c161096564b9ccf4b62fbef4fe1cd382c84e11101780221009204f086a84cb8ed8a9ddd7868dc90c792ee434adf62c66f99a08a5eba11615b300a06082a8648ce3d0403020348003045022054b437be9a2edf591312d68ff24bf91367ad4143f76cf80b5658f232ade820da022100e23b48de9df9c25d4c83ddddf75d2676f0b9318ee2a6c88a736d85eab94a912f").to_vec()); + let certificate = rustls::pki_types::CertificateDer::from(hex!("308201c030820166a003020102020900eaf419a6e3edb4a6300a06082a8648ce3d04030230003020170d3735303130313030303030305a180f34303936303130313030303030305a30003059301306072a8648ce3d020106082a8648ce3d030107034200048dbf1116c7c608d6d5292bd826c3feb53483a89fce434bf64538a359c8e07538ff71f6766239be6a146dcc1a5f3bb934bcd4ae2ae1d4da28ac68b4a20593f06ba381c63081c33081c0060a2b0601040183a25a01010101ff0481ae3081ab045f0803125b3059301306072a8648ce3d020106082a8648ce3d0301070342000484b93fa456a74bd0153919f036db7bc63c802f055bc7023395d0203de718ee0fc7b570b767cdd858aca6c7c4113ff002e78bd2138ac1a3b26dde3519e06979ad04483046022100bc84014cea5a41feabdf4c161096564b9ccf4b62fbef4fe1cd382c84e11101780221009204f086a84cb8ed8a9ddd7868dc90c792ee434adf62c66f99a08a5eba11615b300a06082a8648ce3d0403020348003045022054b437be9a2edf591312d68ff24bf91367ad4143f76cf80b5658f232ade820da022100e23b48de9df9c25d4c83ddddf75d2676f0b9318ee2a6c88a736d85eab94a912f").to_vec()); let peer_id = parse(&certificate).unwrap().peer_id(); @@ -539,7 +592,7 @@ mod tests { #[test] fn can_parse_certificate_with_secp256k1_keypair() { - let certificate = rustls::Certificate(hex!("3082018230820128a003020102020900f3b305f55622cfdf300a06082a8648ce3d04030230003020170d3735303130313030303030305a180f34303936303130313030303030305a30003059301306072a8648ce3d020106082a8648ce3d0301070342000458f7e9581748ff9bdd933b655cc0e5552a1248f840658cc221dec2186b5a2fe4641b86ab7590a3422cdbb1000cf97662f27e5910d7569f22feed8829c8b52e0fa38188308185308182060a2b0601040183a25a01010101ff0471306f042508021221026b053094d1112bce799dc8026040ae6d4eb574157929f1598172061f753d9b1b04463044022040712707e97794c478d93989aaa28ae1f71c03af524a8a4bd2d98424948a782302207b61b7f074b696a25fb9e0059141a811cccc4cc28042d9301b9b2a4015e87470300a06082a8648ce3d04030203480030450220143ae4d86fdc8675d2480bb6912eca5e39165df7f572d836aa2f2d6acfab13f8022100831d1979a98f0c4a6fb5069ca374de92f1a1205c962a6d90ad3d7554cb7d9df4").to_vec()); + let certificate = rustls::pki_types::CertificateDer::from(hex!("3082018230820128a003020102020900f3b305f55622cfdf300a06082a8648ce3d04030230003020170d3735303130313030303030305a180f34303936303130313030303030305a30003059301306072a8648ce3d020106082a8648ce3d0301070342000458f7e9581748ff9bdd933b655cc0e5552a1248f840658cc221dec2186b5a2fe4641b86ab7590a3422cdbb1000cf97662f27e5910d7569f22feed8829c8b52e0fa38188308185308182060a2b0601040183a25a01010101ff0471306f042508021221026b053094d1112bce799dc8026040ae6d4eb574157929f1598172061f753d9b1b04463044022040712707e97794c478d93989aaa28ae1f71c03af524a8a4bd2d98424948a782302207b61b7f074b696a25fb9e0059141a811cccc4cc28042d9301b9b2a4015e87470300a06082a8648ce3d04030203480030450220143ae4d86fdc8675d2480bb6912eca5e39165df7f572d836aa2f2d6acfab13f8022100831d1979a98f0c4a6fb5069ca374de92f1a1205c962a6d90ad3d7554cb7d9df4").to_vec()); let peer_id = parse(&certificate).unwrap().peer_id(); diff --git a/transports/tls/src/lib.rs b/transports/tls/src/lib.rs index 1edd83e9807..3aa66db12b3 100644 --- a/transports/tls/src/lib.rs +++ b/transports/tls/src/lib.rs @@ -29,6 +29,7 @@ pub mod certificate; mod upgrade; mod verifier; +use certificate::AlwaysResolvesCert; use libp2p_identity::Keypair; use libp2p_identity::PeerId; use std::sync::Arc; @@ -46,16 +47,22 @@ pub fn make_client_config( ) -> Result { let (certificate, private_key) = certificate::generate(keypair)?; - let mut crypto = rustls::ClientConfig::builder() - .with_cipher_suites(verifier::CIPHERSUITES) - .with_safe_default_kx_groups() + let mut provider = rustls::crypto::ring::default_provider(); + provider.cipher_suites = verifier::CIPHERSUITES.to_vec(); + + let cert_resolver = Arc::new( + AlwaysResolvesCert::new(certificate, &private_key) + .expect("Client cert key DER is valid; qed"), + ); + + let mut crypto = rustls::ClientConfig::builder_with_provider(provider.into()) .with_protocol_versions(verifier::PROTOCOL_VERSIONS) .expect("Cipher suites and kx groups are configured; qed") + .dangerous() .with_custom_certificate_verifier(Arc::new( verifier::Libp2pCertificateVerifier::with_remote_peer_id(remote_peer_id), )) - .with_client_auth_cert(vec![certificate], private_key) - .expect("Client cert key DER is valid; qed"); + .with_client_cert_resolver(cert_resolver); crypto.alpn_protocols = vec![P2P_ALPN.to_vec()]; Ok(crypto) @@ -67,14 +74,19 @@ pub fn make_server_config( ) -> Result { let (certificate, private_key) = certificate::generate(keypair)?; - let mut crypto = rustls::ServerConfig::builder() - .with_cipher_suites(verifier::CIPHERSUITES) - .with_safe_default_kx_groups() + let mut provider = rustls::crypto::ring::default_provider(); + provider.cipher_suites = verifier::CIPHERSUITES.to_vec(); + + let cert_resolver = Arc::new( + AlwaysResolvesCert::new(certificate, &private_key) + .expect("Server cert key DER is valid; qed"), + ); + + let mut crypto = rustls::ServerConfig::builder_with_provider(provider.into()) .with_protocol_versions(verifier::PROTOCOL_VERSIONS) .expect("Cipher suites and kx groups are configured; qed") .with_client_cert_verifier(Arc::new(verifier::Libp2pCertificateVerifier::new())) - .with_single_cert(vec![certificate], private_key) - .expect("Server cert key DER is valid; qed"); + .with_cert_resolver(cert_resolver); crypto.alpn_protocols = vec![P2P_ALPN.to_vec()]; Ok(crypto) diff --git a/transports/tls/src/upgrade.rs b/transports/tls/src/upgrade.rs index 84510b6bab0..1c61d265ea6 100644 --- a/transports/tls/src/upgrade.rs +++ b/transports/tls/src/upgrade.rs @@ -28,7 +28,8 @@ use libp2p_core::upgrade::{InboundConnectionUpgrade, OutboundConnectionUpgrade}; use libp2p_core::UpgradeInfo; use libp2p_identity as identity; use libp2p_identity::PeerId; -use rustls::{CommonState, ServerName}; +use rustls::{pki_types::ServerName, CommonState}; + use std::net::{IpAddr, Ipv4Addr}; use std::sync::Arc; @@ -103,7 +104,9 @@ where async move { // Spec: In order to keep this flexibility for future versions, clients that only support the version of the handshake defined in this document MUST NOT send any value in the Server Name Indication. // Setting `ServerName` to unspecified will disable the use of the SNI extension. - let name = ServerName::IpAddress(IpAddr::V4(Ipv4Addr::UNSPECIFIED)); + let name = ServerName::IpAddress(rustls::pki_types::IpAddr::from(IpAddr::V4( + Ipv4Addr::UNSPECIFIED, + ))); let stream = futures_rustls::TlsConnector::from(Arc::new(self.client)) .connect(name, socket) diff --git a/transports/tls/src/verifier.rs b/transports/tls/src/verifier.rs index 01fdb8fdf11..65636cbe708 100644 --- a/transports/tls/src/verifier.rs +++ b/transports/tls/src/verifier.rs @@ -26,12 +26,13 @@ use crate::certificate; use libp2p_identity::PeerId; use rustls::{ - cipher_suite::{ + client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier}, + crypto::ring::cipher_suite::{ TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256, }, - client::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier}, - server::{ClientCertVerified, ClientCertVerifier}, - Certificate, CertificateError, DigitallySignedStruct, DistinguishedName, SignatureScheme, + pki_types::CertificateDer, + server::danger::{ClientCertVerified, ClientCertVerifier}, + CertificateError, DigitallySignedStruct, DistinguishedName, OtherError, SignatureScheme, SupportedCipherSuite, SupportedProtocolVersion, }; use std::sync::Arc; @@ -56,6 +57,7 @@ pub(crate) static CIPHERSUITES: &[SupportedCipherSuite] = &[ /// Implementation of the `rustls` certificate verification traits for libp2p. /// /// Only TLS 1.3 is supported. TLS 1.2 should be disabled in the configuration of `rustls`. +#[derive(Debug)] pub(crate) struct Libp2pCertificateVerifier { /// The peer ID we intend to connect to remote_peer_id: Option, @@ -103,12 +105,11 @@ impl Libp2pCertificateVerifier { impl ServerCertVerifier for Libp2pCertificateVerifier { fn verify_server_cert( &self, - end_entity: &Certificate, - intermediates: &[Certificate], - _server_name: &rustls::ServerName, - _scts: &mut dyn Iterator, + end_entity: &CertificateDer, + intermediates: &[CertificateDer], + _server_name: &rustls::pki_types::ServerName, _ocsp_response: &[u8], - _now: std::time::SystemTime, + _now: rustls::pki_types::UnixTime, ) -> Result { let peer_id = verify_presented_certs(end_entity, intermediates)?; @@ -130,7 +131,7 @@ impl ServerCertVerifier for Libp2pCertificateVerifier { fn verify_tls12_signature( &self, _message: &[u8], - _cert: &Certificate, + _cert: &CertificateDer, _dss: &DigitallySignedStruct, ) -> Result { unreachable!("`PROTOCOL_VERSIONS` only allows TLS 1.3") @@ -139,7 +140,7 @@ impl ServerCertVerifier for Libp2pCertificateVerifier { fn verify_tls13_signature( &self, message: &[u8], - cert: &Certificate, + cert: &CertificateDer, dss: &DigitallySignedStruct, ) -> Result { verify_tls13_signature(cert, dss.scheme, message, dss.signature()) @@ -162,15 +163,15 @@ impl ClientCertVerifier for Libp2pCertificateVerifier { true } - fn client_auth_root_subjects(&self) -> &[DistinguishedName] { + fn root_hint_subjects(&self) -> &[DistinguishedName] { &[] } fn verify_client_cert( &self, - end_entity: &Certificate, - intermediates: &[Certificate], - _now: std::time::SystemTime, + end_entity: &CertificateDer, + intermediates: &[CertificateDer], + _now: rustls::pki_types::UnixTime, ) -> Result { verify_presented_certs(end_entity, intermediates)?; @@ -180,7 +181,7 @@ impl ClientCertVerifier for Libp2pCertificateVerifier { fn verify_tls12_signature( &self, _message: &[u8], - _cert: &Certificate, + _cert: &CertificateDer, _dss: &DigitallySignedStruct, ) -> Result { unreachable!("`PROTOCOL_VERSIONS` only allows TLS 1.3") @@ -189,7 +190,7 @@ impl ClientCertVerifier for Libp2pCertificateVerifier { fn verify_tls13_signature( &self, message: &[u8], - cert: &Certificate, + cert: &CertificateDer, dss: &DigitallySignedStruct, ) -> Result { verify_tls13_signature(cert, dss.scheme, message, dss.signature()) @@ -207,8 +208,8 @@ impl ClientCertVerifier for Libp2pCertificateVerifier { /// Endpoints MUST abort the connection attempt if more than one certificate is received, /// or if the certificate’s self-signature is not valid. fn verify_presented_certs( - end_entity: &Certificate, - intermediates: &[Certificate], + end_entity: &CertificateDer, + intermediates: &[CertificateDer], ) -> Result { if !intermediates.is_empty() { return Err(rustls::Error::General( @@ -222,7 +223,7 @@ fn verify_presented_certs( } fn verify_tls13_signature( - cert: &Certificate, + cert: &CertificateDer, signature_scheme: SignatureScheme, message: &[u8], signature: &[u8], @@ -237,7 +238,9 @@ impl From for rustls::Error { use webpki::Error::*; match e { BadDer => rustls::Error::InvalidCertificate(CertificateError::BadEncoding), - e => rustls::Error::InvalidCertificate(CertificateError::Other(Arc::new(e))), + e => { + rustls::Error::InvalidCertificate(CertificateError::Other(OtherError(Arc::new(e)))) + } } } } @@ -248,7 +251,9 @@ impl From for rustls::Error { InvalidSignatureForPublicKey => { rustls::Error::InvalidCertificate(CertificateError::BadSignature) } - other => rustls::Error::InvalidCertificate(CertificateError::Other(Arc::new(other))), + other => rustls::Error::InvalidCertificate(CertificateError::Other(OtherError( + Arc::new(other), + ))), } } } diff --git a/transports/uds/CHANGELOG.md b/transports/uds/CHANGELOG.md index aad61d21547..aa068fe3877 100644 --- a/transports/uds/CHANGELOG.md +++ b/transports/uds/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.41.0 + +- Implement refactored `Transport`. + See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568) + ## 0.40.0 diff --git a/transports/uds/Cargo.toml b/transports/uds/Cargo.toml index 7b01e0e38d5..f70ac388fa8 100644 --- a/transports/uds/Cargo.toml +++ b/transports/uds/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-uds" edition = "2021" rust-version = { workspace = true } description = "Unix domain sockets transport for libp2p" -version = "0.40.0" +version = "0.41.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -13,9 +13,9 @@ categories = ["network-programming", "asynchronous"] [dependencies] async-std = { version = "1.6.2", optional = true } libp2p-core = { workspace = true } -futures = "0.3.30" -tokio = { version = "1.36", default-features = false, features = ["net"], optional = true } -tracing = "0.1.37" +futures = { workspace = true } +tokio = { workspace = true, default-features = false, features = ["net"], optional = true } +tracing = { workspace = true } [dev-dependencies] tempfile = "3.10" @@ -24,8 +24,6 @@ tempfile = "3.10" # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/transports/uds/src/lib.rs b/transports/uds/src/lib.rs index 075cbadb80a..5c57e255b4d 100644 --- a/transports/uds/src/lib.rs +++ b/transports/uds/src/lib.rs @@ -46,7 +46,7 @@ use futures::{ use libp2p_core::transport::ListenerId; use libp2p_core::{ multiaddr::{Multiaddr, Protocol}, - transport::{TransportError, TransportEvent}, + transport::{DialOpts, TransportError, TransportEvent}, Transport, }; use std::collections::VecDeque; @@ -159,7 +159,7 @@ macro_rules! codegen { } } - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial(&mut self, addr: Multiaddr, _dial_opts: DialOpts) -> Result> { // TODO: Should we dial at all? if let Ok(path) = multiaddr_to_path(&addr) { tracing::debug!(address=%addr, "Dialing address"); @@ -169,21 +169,6 @@ macro_rules! codegen { } } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - self.dial(addr) - } - - fn address_translation( - &self, - _server: &Multiaddr, - _observed: &Multiaddr, - ) -> Option { - None - } - fn poll( mut self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -260,10 +245,10 @@ mod tests { use futures::{channel::oneshot, prelude::*}; use libp2p_core::{ multiaddr::{Multiaddr, Protocol}, - transport::ListenerId, - Transport, + transport::{DialOpts, ListenerId, PortUse}, + Endpoint, Transport, }; - use std::{self, borrow::Cow, path::Path}; + use std::{borrow::Cow, path::Path}; #[test] fn multiaddr_to_path_conversion() { @@ -318,7 +303,17 @@ mod tests { async_std::task::block_on(async move { let mut uds = UdsConfig::new(); let addr = rx.await.unwrap(); - let mut socket = uds.dial(addr).unwrap().await.unwrap(); + let mut socket = uds + .dial( + addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap() + .await + .unwrap(); let _ = socket.write(&[1, 2, 3]).await.unwrap(); }); } diff --git a/transports/webrtc-websys/CHANGELOG.md b/transports/webrtc-websys/CHANGELOG.md index 634120c53c3..5b8f2efb3b0 100644 --- a/transports/webrtc-websys/CHANGELOG.md +++ b/transports/webrtc-websys/CHANGELOG.md @@ -1,3 +1,13 @@ +## 0.4.0-alpha.2 + +- Bump version of web-sys and update `__Nonexhaustive` to `__Invalid`. + See [PR 5569](https://github.com/libp2p/rust-libp2p/pull/5569) + +## 0.4.0-alpha + +- Implement refactored `Transport`. + See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568) + ## 0.3.0-alpha - Bump version in order to publish a new version dependent on latest `libp2p-core`. diff --git a/transports/webrtc-websys/Cargo.toml b/transports/webrtc-websys/Cargo.toml index 3c212e195c1..453abe57f74 100644 --- a/transports/webrtc-websys/Cargo.toml +++ b/transports/webrtc-websys/Cargo.toml @@ -8,13 +8,13 @@ license = "MIT" name = "libp2p-webrtc-websys" repository = "https://github.com/libp2p/rust-libp2p" rust-version = { workspace = true } -version = "0.3.0-alpha" +version = "0.4.0-alpha.2" publish = true [dependencies] bytes = "1" -futures = "0.3" -getrandom = { version = "0.2.12", features = ["js"] } +futures = { workspace = true } +getrandom = { version = "0.2.15", features = ["js"] } hex = "0.4.3" js-sys = { version = "0.3" } libp2p-core = { workspace = true } @@ -22,10 +22,10 @@ libp2p-identity = { workspace = true } libp2p-webrtc-utils = { workspace = true } send_wrapper = { version = "0.6.0", features = ["futures"] } thiserror = "1" -tracing = "0.1.37" +tracing = { workspace = true } wasm-bindgen = { version = "0.2.90" } wasm-bindgen-futures = { version = "0.4.42" } -web-sys = { version = "0.3.68", features = ["Document", "Location", "MessageEvent", "Navigator", "RtcCertificate", "RtcConfiguration", "RtcDataChannel", "RtcDataChannelEvent", "RtcDataChannelInit", "RtcDataChannelState", "RtcDataChannelType", "RtcPeerConnection", "RtcSdpType", "RtcSessionDescription", "RtcSessionDescriptionInit", "Window"] } +web-sys = { version = "0.3.70", features = ["Document", "Location", "MessageEvent", "Navigator", "RtcCertificate", "RtcConfiguration", "RtcDataChannel", "RtcDataChannelEvent", "RtcDataChannelInit", "RtcDataChannelState", "RtcDataChannelType", "RtcPeerConnection", "RtcSdpType", "RtcSessionDescription", "RtcSessionDescriptionInit", "Window"] } [lints] workspace = true diff --git a/transports/webrtc-websys/src/connection.rs b/transports/webrtc-websys/src/connection.rs index b858237da63..d0c6ccd2238 100644 --- a/transports/webrtc-websys/src/connection.rs +++ b/transports/webrtc-websys/src/connection.rs @@ -186,11 +186,11 @@ impl RtcPeerConnection { let certificate = JsFuture::from(certificate_promise).await?; - let mut config = RtcConfiguration::default(); + let config = RtcConfiguration::default(); // wrap certificate in a js Array first before adding it to the config object let certificate_arr = js_sys::Array::new(); certificate_arr.push(&certificate); - config.certificates(&certificate_arr); + config.set_certificates(&certificate_arr); let inner = web_sys::RtcPeerConnection::new_with_configuration(&config)?; @@ -214,8 +214,9 @@ impl RtcPeerConnection { let dc = match negotiated { true => { - let mut options = RtcDataChannelInit::new(); - options.negotiated(true).id(0); // id is only ever set to zero when negotiated is true + let options = RtcDataChannelInit::new(); + options.set_negotiated(true); + options.set_id(0); // id is only ever set to zero when negotiated is true self.inner .create_data_channel_with_data_channel_dict(LABEL, &options) diff --git a/transports/webrtc-websys/src/sdp.rs b/transports/webrtc-websys/src/sdp.rs index 439182ea4db..9e63fd92462 100644 --- a/transports/webrtc-websys/src/sdp.rs +++ b/transports/webrtc-websys/src/sdp.rs @@ -8,8 +8,8 @@ pub(crate) fn answer( server_fingerprint: Fingerprint, client_ufrag: &str, ) -> RtcSessionDescriptionInit { - let mut answer_obj = RtcSessionDescriptionInit::new(RtcSdpType::Answer); - answer_obj.sdp(&libp2p_webrtc_utils::sdp::answer( + let answer_obj = RtcSessionDescriptionInit::new(RtcSdpType::Answer); + answer_obj.set_sdp(&libp2p_webrtc_utils::sdp::answer( addr, server_fingerprint, client_ufrag, @@ -48,8 +48,8 @@ pub(crate) fn offer(offer: String, client_ufrag: &str) -> RtcSessionDescriptionI tracing::trace!(offer=%munged_sdp_offer, "Created SDP offer"); - let mut offer_obj = RtcSessionDescriptionInit::new(RtcSdpType::Offer); - offer_obj.sdp(&munged_sdp_offer); + let offer_obj = RtcSessionDescriptionInit::new(RtcSdpType::Offer); + offer_obj.set_sdp(&munged_sdp_offer); offer_obj } diff --git a/transports/webrtc-websys/src/stream/poll_data_channel.rs b/transports/webrtc-websys/src/stream/poll_data_channel.rs index 0ee4f7920c9..3ec744342eb 100644 --- a/transports/webrtc-websys/src/stream/poll_data_channel.rs +++ b/transports/webrtc-websys/src/stream/poll_data_channel.rs @@ -10,10 +10,10 @@ use bytes::BytesMut; use futures::task::AtomicWaker; use futures::{AsyncRead, AsyncWrite}; use libp2p_webrtc_utils::MAX_MSG_LEN; -use wasm_bindgen::{prelude::*, JsCast}; +use wasm_bindgen::prelude::*; use web_sys::{Event, MessageEvent, RtcDataChannel, RtcDataChannelEvent, RtcDataChannelState}; -/// [`PollDataChannel`] is a wrapper around around [`RtcDataChannel`] which implements [`AsyncRead`] and [`AsyncWrite`]. +/// [`PollDataChannel`] is a wrapper around [`RtcDataChannel`] which implements [`AsyncRead`] and [`AsyncWrite`]. #[derive(Debug, Clone)] pub(crate) struct PollDataChannel { /// The [`RtcDataChannel`] being wrapped. @@ -143,7 +143,8 @@ impl PollDataChannel { RtcDataChannelState::Closing | RtcDataChannelState::Closed => { return Poll::Ready(Err(io::ErrorKind::BrokenPipe.into())) } - RtcDataChannelState::Open | RtcDataChannelState::__Nonexhaustive => {} + RtcDataChannelState::Open | RtcDataChannelState::__Invalid => {} + _ => {} } if self.overloaded.load(Ordering::SeqCst) { diff --git a/transports/webrtc-websys/src/transport.rs b/transports/webrtc-websys/src/transport.rs index ecf137eab8a..836acb0b9f6 100644 --- a/transports/webrtc-websys/src/transport.rs +++ b/transports/webrtc-websys/src/transport.rs @@ -4,6 +4,7 @@ use super::Error; use futures::future::FutureExt; use libp2p_core::multiaddr::Multiaddr; use libp2p_core::muxing::StreamMuxerBox; +use libp2p_core::transport::DialOpts; use libp2p_core::transport::{Boxed, ListenerId, Transport as _, TransportError, TransportEvent}; use libp2p_identity::{Keypair, PeerId}; use std::future::Future; @@ -62,7 +63,15 @@ impl libp2p_core::Transport for Transport { false } - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + dial_opts: DialOpts, + ) -> Result> { + if dial_opts.role.is_listener() { + return Err(TransportError::MultiaddrNotSupported(addr)); + } + if maybe_local_firefox() { return Err(TransportError::Other( "Firefox does not support WebRTC over localhost or 127.0.0.1" @@ -89,23 +98,12 @@ impl libp2p_core::Transport for Transport { .boxed()) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - Err(TransportError::MultiaddrNotSupported(addr)) - } - fn poll( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll> { Poll::Pending } - - fn address_translation(&self, _listen: &Multiaddr, _observed: &Multiaddr) -> Option { - None - } } /// Checks if local Firefox. diff --git a/transports/webrtc/CHANGELOG.md b/transports/webrtc/CHANGELOG.md index 930526d58d5..90d4ce83df3 100644 --- a/transports/webrtc/CHANGELOG.md +++ b/transports/webrtc/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.8.0-alpha + +- Implement refactored `Transport`. + See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568) + ## 0.7.1-alpha - Bump `libp2p-webrtc-utils` dependency to `0.2.0`. diff --git a/transports/webrtc/Cargo.toml b/transports/webrtc/Cargo.toml index 1bd1bd57a61..fc2748d93c3 100644 --- a/transports/webrtc/Cargo.toml +++ b/transports/webrtc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-webrtc" -version = "0.7.1-alpha" +version = "0.8.0-alpha" authors = ["Parity Technologies "] description = "WebRTC transport for libp2p" repository = "https://github.com/libp2p/rust-libp2p" @@ -13,7 +13,7 @@ categories = ["network-programming", "asynchronous"] [dependencies] async-trait = "0.1" bytes = "1" -futures = "0.3" +futures = { workspace = true } futures-timer = "3" hex = "0.4" if-watch = "3.2" @@ -23,14 +23,14 @@ libp2p-identity = { workspace = true } libp2p-webrtc-utils = { workspace = true } multihash = { workspace = true } rand = "0.8" -rcgen = "0.11.3" +rcgen = { workspace = true } serde = { version = "1.0", features = ["derive"] } -stun = "0.5" +stun = "0.6" thiserror = "1" tinytemplate = "1.2" -tokio = { version = "1.36", features = ["net"], optional = true } +tokio = { workspace = true, features = ["net"], optional = true } tokio-util = { version = "0.7", features = ["compat"], optional = true } -tracing = "0.1.37" +tracing = { workspace = true } webrtc = { version = "0.9.0", optional = true } [features] @@ -39,9 +39,9 @@ pem = ["webrtc?/pem"] [dev-dependencies] libp2p-identity = { workspace = true, features = ["rand"] } -tokio = { version = "1.36", features = ["full"] } +tokio = { workspace = true, features = ["full"] } quickcheck = "1.0.3" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [[test]] @@ -55,5 +55,3 @@ workspace = true # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] diff --git a/transports/webrtc/src/tokio/certificate.rs b/transports/webrtc/src/tokio/certificate.rs index 7c7c65f0447..81197af4132 100644 --- a/transports/webrtc/src/tokio/certificate.rs +++ b/transports/webrtc/src/tokio/certificate.rs @@ -32,6 +32,7 @@ impl Certificate { /// Generate new certificate. /// /// `_rng` argument is ignored for now. See . + #[allow(clippy::unnecessary_wraps)] pub fn generate(_rng: &mut R) -> Result where R: CryptoRng + Rng, diff --git a/transports/webrtc/src/tokio/sdp.rs b/transports/webrtc/src/tokio/sdp.rs index 8549a864dcc..4be4c19f188 100644 --- a/transports/webrtc/src/tokio/sdp.rs +++ b/transports/webrtc/src/tokio/sdp.rs @@ -98,7 +98,7 @@ pub(crate) fn offer(addr: SocketAddr, client_ufrag: &str) -> RTCSessionDescripti // // a=ice-options:ice2 // -// Indicates that we are complying with RFC8839 (as oppposed to the legacy RFC5245). +// Indicates that we are complying with RFC8839 (as opposed to the legacy RFC5245). // // a=ice-ufrag: // a=ice-pwd: diff --git a/transports/webrtc/src/tokio/transport.rs b/transports/webrtc/src/tokio/transport.rs index 02cfa6f7296..62049c8f59b 100644 --- a/transports/webrtc/src/tokio/transport.rs +++ b/transports/webrtc/src/tokio/transport.rs @@ -18,11 +18,11 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use futures::{future::BoxFuture, prelude::*, stream::SelectAll, stream::Stream}; +use futures::{future::BoxFuture, prelude::*, stream::SelectAll}; use if_watch::{tokio::IfWatcher, IfEvent}; use libp2p_core::{ multiaddr::{Multiaddr, Protocol}, - transport::{ListenerId, TransportError, TransportEvent}, + transport::{DialOpts, ListenerId, TransportError, TransportEvent}, }; use libp2p_identity as identity; use libp2p_identity::PeerId; @@ -118,7 +118,19 @@ impl libp2p_core::Transport for Transport { } } - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + dial_opts: DialOpts, + ) -> Result> { + if dial_opts.role.is_listener() { + // TODO: As the listener of a WebRTC hole punch, we need to send a random UDP packet to the + // `addr`. See DCUtR specification below. + // + // https://github.com/libp2p/specs/blob/master/relay/DCUtR.md#the-protocol + tracing::warn!("WebRTC hole punch is not yet supported"); + } + let (sock_addr, server_fingerprint) = libp2p_webrtc_utils::parse_webrtc_dial_addr(&addr) .ok_or_else(|| TransportError::MultiaddrNotSupported(addr.clone()))?; if sock_addr.port() == 0 || sock_addr.ip().is_unspecified() { @@ -150,21 +162,6 @@ impl libp2p_core::Transport for Transport { } .boxed()) } - - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - // TODO: As the listener of a WebRTC hole punch, we need to send a random UDP packet to the - // `addr`. See DCUtR specification below. - // - // https://github.com/libp2p/specs/blob/master/relay/DCUtR.md#the-protocol - self.dial(addr) - } - - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - libp2p_core::address_translation(server, observed) - } } /// A stream of incoming connections on one or more interfaces. @@ -431,9 +428,9 @@ fn parse_webrtc_listen_addr(addr: &Multiaddr) -> Option { mod tests { use super::*; use futures::future::poll_fn; - use libp2p_core::{multiaddr::Protocol, Transport as _}; + use libp2p_core::Transport as _; use rand::thread_rng; - use std::net::{IpAddr, Ipv6Addr}; + use std::net::Ipv6Addr; #[test] fn missing_webrtc_protocol() { diff --git a/transports/webrtc/tests/smoke.rs b/transports/webrtc/tests/smoke.rs index 76e168edfd6..d606d66c41f 100644 --- a/transports/webrtc/tests/smoke.rs +++ b/transports/webrtc/tests/smoke.rs @@ -23,8 +23,8 @@ use futures::future::{BoxFuture, Either}; use futures::stream::StreamExt; use futures::{future, ready, AsyncReadExt, AsyncWriteExt, FutureExt, SinkExt}; use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerExt}; -use libp2p_core::transport::{Boxed, ListenerId, TransportEvent}; -use libp2p_core::{Multiaddr, Transport}; +use libp2p_core::transport::{Boxed, DialOpts, ListenerId, PortUse, TransportEvent}; +use libp2p_core::{Endpoint, Multiaddr, Transport}; use libp2p_identity::PeerId; use libp2p_webrtc as webrtc; use rand::{thread_rng, RngCore}; @@ -322,7 +322,17 @@ struct Dial<'a> { impl<'a> Dial<'a> { fn new(dialer: &'a mut Boxed<(PeerId, StreamMuxerBox)>, addr: Multiaddr) -> Self { Self { - dial_task: dialer.dial(addr).unwrap().map(|r| r.unwrap()).boxed(), + dial_task: dialer + .dial( + addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap() + .map(|r| r.unwrap()) + .boxed(), dialer, } } diff --git a/transports/websocket-websys/CHANGELOG.md b/transports/websocket-websys/CHANGELOG.md index 3cfb1b2fbf9..d0aeb509823 100644 --- a/transports/websocket-websys/CHANGELOG.md +++ b/transports/websocket-websys/CHANGELOG.md @@ -1,3 +1,22 @@ +## 0.4.0 + +- Implement refactored `Transport`. + See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568) +- Add support for `/tls/ws` and keep `/wss` backward compatible. + See [PR 5523](https://github.com/libp2p/rust-libp2p/pull/5523). + +## 0.3.3 + +- Fix use-after-free handler invocation from JS side. + See [PR 5521](https://github.com/libp2p/rust-libp2p/pull/5521). + +## 0.3.2 + +- Change close code in drop implementation to `1000` given that in browsers only + the code `1000` and codes between `3000` and `4999` are allowed to be set by + userland code. + See [PR 5229](https://github.com/libp2p/rust-libp2p/pull/5229). + ## 0.3.1 - Add support for different WASM environments by introducing a `WebContext` that diff --git a/transports/websocket-websys/Cargo.toml b/transports/websocket-websys/Cargo.toml index 5855b582c80..32483f28c57 100644 --- a/transports/websocket-websys/Cargo.toml +++ b/transports/websocket-websys/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-websocket-websys" edition = "2021" rust-version = "1.60.0" description = "WebSocket for libp2p under WASM environment" -version = "0.3.1" +version = "0.4.0" authors = ["Vince Vasta "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -11,23 +11,21 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] -bytes = "1.4.0" -futures = "0.3.30" -js-sys = "0.3.67" +bytes = "1.6.0" +futures = { workspace = true } +js-sys = "0.3.69" libp2p-core = { workspace = true } -tracing = "0.1.37" -parking_lot = "0.12.1" +tracing = { workspace = true } +parking_lot = "0.12.3" send_wrapper = "0.6.0" -thiserror = "1.0.57" +thiserror = "1.0.61" wasm-bindgen = "0.2.90" -web-sys = { version = "0.3.68", features = ["BinaryType", "CloseEvent", "MessageEvent", "WebSocket", "Window", "WorkerGlobalScope"] } +web-sys = { version = "0.3.69", features = ["BinaryType", "CloseEvent", "MessageEvent", "WebSocket", "Window", "WorkerGlobalScope"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [dev-dependencies] libp2p-yamux = { workspace = true } diff --git a/transports/websocket-websys/src/lib.rs b/transports/websocket-websys/src/lib.rs index 5c1a6ebf1c4..f353d92b204 100644 --- a/transports/websocket-websys/src/lib.rs +++ b/transports/websocket-websys/src/lib.rs @@ -26,6 +26,7 @@ use bytes::BytesMut; use futures::task::AtomicWaker; use futures::{future::Ready, io, prelude::*}; use js_sys::Array; +use libp2p_core::transport::DialOpts; use libp2p_core::{ multiaddr::{Multiaddr, Protocol}, transport::{ListenerId, TransportError, TransportEvent}, @@ -36,7 +37,7 @@ use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Mutex; use std::{pin::Pin, task::Context, task::Poll}; -use wasm_bindgen::{prelude::*, JsCast}; +use wasm_bindgen::prelude::*; use web_sys::{CloseEvent, Event, MessageEvent, WebSocket}; use crate::web_context::WebContext; @@ -86,7 +87,15 @@ impl libp2p_core::Transport for Transport { false } - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + dial_opts: DialOpts, + ) -> Result> { + if dial_opts.role.is_listener() { + return Err(TransportError::MultiaddrNotSupported(addr)); + } + let url = extract_websocket_url(&addr) .ok_or_else(|| TransportError::MultiaddrNotSupported(addr))?; @@ -101,23 +110,12 @@ impl libp2p_core::Transport for Transport { .boxed()) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - Err(TransportError::MultiaddrNotSupported(addr)) - } - fn poll( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> std::task::Poll> { Poll::Pending } - - fn address_translation(&self, _listen: &Multiaddr, _observed: &Multiaddr) -> Option { - None - } } // Try to convert Multiaddr to a Websocket url. @@ -139,9 +137,10 @@ fn extract_websocket_url(addr: &Multiaddr) -> Option { _ => return None, }; - let (scheme, wspath) = match protocols.next() { - Some(Protocol::Ws(path)) => ("ws", path.into_owned()), - Some(Protocol::Wss(path)) => ("wss", path.into_owned()), + let (scheme, wspath) = match (protocols.next(), protocols.next()) { + (Some(Protocol::Tls), Some(Protocol::Ws(path))) => ("wss", path.into_owned()), + (Some(Protocol::Ws(path)), _) => ("ws", path.into_owned()), + (Some(Protocol::Wss(path)), _) => ("wss", path.into_owned()), _ => return None, }; @@ -434,13 +433,20 @@ impl AsyncWrite for Connection { impl Drop for Connection { fn drop(&mut self) { - const GO_AWAY_STATUS_CODE: u16 = 1001; // See https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1. + // Unset event listeners, as otherwise they will be called by JS after the handlers have already been dropped. + self.inner.socket.set_onclose(None); + self.inner.socket.set_onerror(None); + self.inner.socket.set_onopen(None); + self.inner.socket.set_onmessage(None); + + // In browsers, userland code is not allowed to use any other status code than 1000: https://websockets.spec.whatwg.org/#dom-websocket-close + const REGULAR_CLOSE: u16 = 1000; // See https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1. if let ReadyState::Connecting | ReadyState::Open = self.inner.ready_state() { let _ = self .inner .socket - .close_with_code_and_reason(GO_AWAY_STATUS_CODE, "connection dropped"); + .close_with_code_and_reason(REGULAR_CLOSE, "connection dropped"); } WebContext::new() @@ -448,3 +454,103 @@ impl Drop for Connection { .clear_interval_with_handle(self.inner.buffered_amount_low_interval); } } + +#[cfg(test)] +mod tests { + use super::*; + use libp2p_identity::PeerId; + + #[test] + fn extract_url() { + let peer_id = PeerId::random(); + + // Check `/tls/ws` + let addr = "/dns4/example.com/tcp/2222/tls/ws" + .parse::() + .unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "wss://example.com:2222/"); + + // Check `/tls/ws` with `/p2p` + let addr = format!("/dns4/example.com/tcp/2222/tls/ws/p2p/{peer_id}") + .parse() + .unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "wss://example.com:2222/"); + + // Check `/tls/ws` with `/ip4` + let addr = "/ip4/127.0.0.1/tcp/2222/tls/ws" + .parse::() + .unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "wss://127.0.0.1:2222/"); + + // Check `/tls/ws` with `/ip6` + let addr = "/ip6/::1/tcp/2222/tls/ws".parse::().unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "wss://[::1]:2222/"); + + // Check `/wss` + let addr = "/dns4/example.com/tcp/2222/wss" + .parse::() + .unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "wss://example.com:2222/"); + + // Check `/wss` with `/p2p` + let addr = format!("/dns4/example.com/tcp/2222/wss/p2p/{peer_id}") + .parse() + .unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "wss://example.com:2222/"); + + // Check `/wss` with `/ip4` + let addr = "/ip4/127.0.0.1/tcp/2222/wss".parse::().unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "wss://127.0.0.1:2222/"); + + // Check `/wss` with `/ip6` + let addr = "/ip6/::1/tcp/2222/wss".parse::().unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "wss://[::1]:2222/"); + + // Check `/ws` + let addr = "/dns4/example.com/tcp/2222/ws" + .parse::() + .unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "ws://example.com:2222/"); + + // Check `/ws` with `/p2p` + let addr = format!("/dns4/example.com/tcp/2222/ws/p2p/{peer_id}") + .parse() + .unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "ws://example.com:2222/"); + + // Check `/ws` with `/ip4` + let addr = "/ip4/127.0.0.1/tcp/2222/ws".parse::().unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "ws://127.0.0.1:2222/"); + + // Check `/ws` with `/ip6` + let addr = "/ip6/::1/tcp/2222/ws".parse::().unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "ws://[::1]:2222/"); + + // Check `/ws` with `/ip4` + let addr = "/ip4/127.0.0.1/tcp/2222/ws".parse::().unwrap(); + let url = extract_websocket_url(&addr).unwrap(); + assert_eq!(url, "ws://127.0.0.1:2222/"); + + // Check that `/tls/wss` is invalid + let addr = "/ip4/127.0.0.1/tcp/2222/tls/wss" + .parse::() + .unwrap(); + assert!(extract_websocket_url(&addr).is_none()); + + // Check non-ws address + let addr = "/ip4/127.0.0.1/tcp/2222".parse::().unwrap(); + assert!(extract_websocket_url(&addr).is_none()); + } +} diff --git a/transports/websocket-websys/src/web_context.rs b/transports/websocket-websys/src/web_context.rs index c514435d2bb..40efbb1d9b0 100644 --- a/transports/websocket-websys/src/web_context.rs +++ b/transports/websocket-websys/src/web_context.rs @@ -1,4 +1,7 @@ -use wasm_bindgen::{prelude::*, JsCast}; +// TODO: remove when https://github.com/rust-lang/rust-clippy/issues/12377 fix lands in stable clippy. +#![allow(clippy::empty_docs)] + +use wasm_bindgen::prelude::*; use web_sys::window; /// Web context that abstract the window vs web worker API diff --git a/transports/websocket/CHANGELOG.md b/transports/websocket/CHANGELOG.md index 192b1fa094e..cd079cfdd5a 100644 --- a/transports/websocket/CHANGELOG.md +++ b/transports/websocket/CHANGELOG.md @@ -1,3 +1,20 @@ +## 0.44.0 + +- Implement refactored `Transport`. + See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568) +- Allow wss connections on IP addresses. + See [PR 5525](https://github.com/libp2p/rust-libp2p/pull/5525). +- Add support for `/tls/ws` and keep `/wss` backward compatible. + See [PR 5523](https://github.com/libp2p/rust-libp2p/pull/5523). + +## 0.43.2 + +- fix: Avoid websocket panic on polling after errors. See [PR 5482]. + +[PR 5482]: https://github.com/libp2p/rust-libp2p/pull/5482 + +## 0.43.1 + ## 0.43.0 diff --git a/transports/websocket/Cargo.toml b/transports/websocket/Cargo.toml index 473aeb2c8ff..e08346da5ca 100644 --- a/transports/websocket/Cargo.toml +++ b/transports/websocket/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-websocket" edition = "2021" rust-version = { workspace = true } description = "WebSocket transport for libp2p" -version = "0.43.0" +version = "0.44.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -11,16 +11,17 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] -futures-rustls = "0.24.0" -either = "1.9.0" -futures = "0.3.30" +futures-rustls = { workspace = true, features = ["ring"] } +either = "1.12.0" +futures = { workspace = true } libp2p-core = { workspace = true } libp2p-identity = { workspace = true } -parking_lot = "0.12.0" -pin-project-lite = "0.2.13" +parking_lot = "0.12.3" +pin-project-lite = "0.2.14" rw-stream-sink = { workspace = true } soketto = "0.8.0" -tracing = "0.1.37" +tracing = { workspace = true } +thiserror = "1.0.61" url = "2.5" webpki-roots = "0.25" @@ -29,14 +30,12 @@ libp2p-tcp = { workspace = true, features = ["async-io"] } libp2p-dns = { workspace = true, features = ["async-std"] } libp2p-identity = { workspace = true, features = ["rand"] } async-std = { version = "1.6.5", features = ["attributes"] } -rcgen = "0.11.3" +rcgen = { workspace = true } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/transports/websocket/src/framed.rs b/transports/websocket/src/framed.rs index 3593e1eaff2..a547aea21ef 100644 --- a/transports/websocket/src/framed.rs +++ b/transports/websocket/src/framed.rs @@ -21,11 +21,11 @@ use crate::{error::Error, quicksink, tls}; use either::Either; use futures::{future::BoxFuture, prelude::*, ready, stream::BoxStream}; -use futures_rustls::{client, rustls, server}; +use futures_rustls::rustls::pki_types::ServerName; +use futures_rustls::{client, server}; use libp2p_core::{ - connection::Endpoint, multiaddr::{Multiaddr, Protocol}, - transport::{ListenerId, TransportError, TransportEvent}, + transport::{DialOpts, ListenerId, TransportError, TransportEvent}, Transport, }; use parking_lot::Mutex; @@ -33,8 +33,10 @@ use soketto::{ connection::{self, CloseReason}, handshake, }; +use std::borrow::Cow; +use std::net::IpAddr; use std::{collections::HashMap, ops::DerefMut, sync::Arc}; -use std::{convert::TryInto, fmt, io, mem, pin::Pin, task::Context, task::Poll}; +use std::{fmt, io, mem, pin::Pin, task::Context, task::Poll}; use url::Url; /// Max. number of payload bytes of a single frame. @@ -50,10 +52,7 @@ pub struct WsConfig { tls_config: tls::Config, max_redirects: u8, /// Websocket protocol of the inner listener. - /// - /// This is the suffix of the address provided in `listen_on`. - /// Can only be [`Protocol::Ws`] or [`Protocol::Wss`]. - listener_protos: HashMap>, + listener_protos: HashMap>, } impl WsConfig @@ -120,22 +119,19 @@ where id: ListenerId, addr: Multiaddr, ) -> Result<(), TransportError> { - let mut inner_addr = addr.clone(); - let proto = match inner_addr.pop() { - Some(p @ Protocol::Wss(_)) => { - if self.tls_config.server.is_some() { - p - } else { - tracing::debug!("/wss address but TLS server support is not configured"); - return Err(TransportError::MultiaddrNotSupported(addr)); - } - } - Some(p @ Protocol::Ws(_)) => p, - _ => { - tracing::debug!(address=%addr, "Address is not a websocket multiaddr"); - return Err(TransportError::MultiaddrNotSupported(addr)); - } - }; + let (inner_addr, proto) = parse_ws_listen_addr(&addr).ok_or_else(|| { + tracing::debug!(address=%addr, "Address is not a websocket multiaddr"); + TransportError::MultiaddrNotSupported(addr.clone()) + })?; + + if proto.use_tls() && self.tls_config.server.is_none() { + tracing::debug!( + "{} address but TLS server support is not configured", + proto.prefix() + ); + return Err(TransportError::MultiaddrNotSupported(addr)); + } + match self.transport.lock().listen_on(id, inner_addr) { Ok(()) => { self.listener_protos.insert(id, proto); @@ -149,19 +145,12 @@ where self.transport.lock().remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { - self.do_dial(addr, Endpoint::Dialer) - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + dial_opts: DialOpts, ) -> Result> { - self.do_dial(addr, Endpoint::Listener) - } - - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.transport.lock().address_translation(server, observed) + self.do_dial(addr, dial_opts) } fn poll( @@ -181,11 +170,10 @@ where mut listen_addr, } => { // Append the ws / wss protocol back to the inner address. - let proto = self - .listener_protos + self.listener_protos .get(&listener_id) - .expect("Protocol was inserted in Transport::listen_on."); - listen_addr.push(proto.clone()); + .expect("Protocol was inserted in Transport::listen_on.") + .append_on_addr(&mut listen_addr); tracing::debug!(address=%listen_addr, "Listening on address"); TransportEvent::NewAddress { listener_id, @@ -196,11 +184,10 @@ where listener_id, mut listen_addr, } => { - let proto = self - .listener_protos + self.listener_protos .get(&listener_id) - .expect("Protocol was inserted in Transport::listen_on."); - listen_addr.push(proto.clone()); + .expect("Protocol was inserted in Transport::listen_on.") + .append_on_addr(&mut listen_addr); TransportEvent::AddressExpired { listener_id, listen_addr, @@ -232,13 +219,9 @@ where .listener_protos .get(&listener_id) .expect("Protocol was inserted in Transport::listen_on."); - let use_tls = match proto { - Protocol::Wss(_) => true, - Protocol::Ws(_) => false, - _ => unreachable!("Map contains only ws and wss protocols."), - }; - local_addr.push(proto.clone()); - send_back_addr.push(proto.clone()); + let use_tls = proto.use_tls(); + proto.append_on_addr(&mut local_addr); + proto.append_on_addr(&mut send_back_addr); let upgrade = self.map_upgrade(upgrade, send_back_addr.clone(), use_tls); TransportEvent::Incoming { listener_id, @@ -263,7 +246,7 @@ where fn do_dial( &mut self, addr: Multiaddr, - role_override: Endpoint, + dial_opts: DialOpts, ) -> Result<::Dial, TransportError<::Error>> { let mut addr = match parse_ws_dial_addr(addr) { Ok(addr) => addr, @@ -282,8 +265,7 @@ where let future = async move { loop { - match Self::dial_once(transport.clone(), addr, tls_config.clone(), role_override) - .await + match Self::dial_once(transport.clone(), addr, tls_config.clone(), dial_opts).await { Ok(Either::Left(redirect)) => { if remaining_redirects == 0 { @@ -307,33 +289,29 @@ where transport: Arc>, addr: WsAddress, tls_config: tls::Config, - role_override: Endpoint, + dial_opts: DialOpts, ) -> Result>, Error> { tracing::trace!(address=?addr, "Dialing websocket address"); - let dial = match role_override { - Endpoint::Dialer => transport.lock().dial(addr.tcp_addr), - Endpoint::Listener => transport.lock().dial_as_listener(addr.tcp_addr), - } - .map_err(|e| match e { - TransportError::MultiaddrNotSupported(a) => Error::InvalidMultiaddr(a), - TransportError::Other(e) => Error::Transport(e), - })?; + let dial = transport + .lock() + .dial(addr.tcp_addr, dial_opts) + .map_err(|e| match e { + TransportError::MultiaddrNotSupported(a) => Error::InvalidMultiaddr(a), + TransportError::Other(e) => Error::Transport(e), + })?; let stream = dial.map_err(Error::Transport).await?; tracing::trace!(port=%addr.host_port, "TCP connection established"); let stream = if addr.use_tls { // begin TLS session - let dns_name = addr - .dns_name - .expect("for use_tls we have checked that dns_name is some"); - tracing::trace!(?dns_name, "Starting TLS handshake"); + tracing::trace!(?addr.server_name, "Starting TLS handshake"); let stream = tls_config .client - .connect(dns_name.clone(), stream) + .connect(addr.server_name.clone(), stream) .map_err(|e| { - tracing::debug!(?dns_name, "TLS handshake failed: {}", e); + tracing::debug!(?addr.server_name, "TLS handshake failed: {}", e); Error::Tls(tls::Error::from(e)) }) .await?; @@ -457,11 +435,53 @@ where } } +#[derive(Debug, PartialEq)] +pub(crate) enum WsListenProto<'a> { + Ws(Cow<'a, str>), + Wss(Cow<'a, str>), + TlsWs(Cow<'a, str>), +} + +impl<'a> WsListenProto<'a> { + pub(crate) fn append_on_addr(&self, addr: &mut Multiaddr) { + match self { + WsListenProto::Ws(path) => { + addr.push(Protocol::Ws(path.clone())); + } + // `/tls/ws` and `/wss` are equivalend, however we regenerate + // the one that user passed at `listen_on` for backward compatibility. + WsListenProto::Wss(path) => { + addr.push(Protocol::Wss(path.clone())); + } + WsListenProto::TlsWs(path) => { + addr.push(Protocol::Tls); + addr.push(Protocol::Ws(path.clone())); + } + } + } + + pub(crate) fn use_tls(&self) -> bool { + match self { + WsListenProto::Ws(_) => false, + WsListenProto::Wss(_) => true, + WsListenProto::TlsWs(_) => true, + } + } + + pub(crate) fn prefix(&self) -> &'static str { + match self { + WsListenProto::Ws(_) => "/ws", + WsListenProto::Wss(_) => "/wss", + WsListenProto::TlsWs(_) => "/tls/ws", + } + } +} + #[derive(Debug)] struct WsAddress { host_port: String, path: String, - dns_name: Option, + server_name: ServerName<'static>, use_tls: bool, tcp_addr: Multiaddr, } @@ -478,19 +498,21 @@ fn parse_ws_dial_addr(addr: Multiaddr) -> Result> { let mut protocols = addr.iter(); let mut ip = protocols.next(); let mut tcp = protocols.next(); - let (host_port, dns_name) = loop { + let (host_port, server_name) = loop { match (ip, tcp) { (Some(Protocol::Ip4(ip)), Some(Protocol::Tcp(port))) => { - break (format!("{ip}:{port}"), None) + let server_name = ServerName::IpAddress(IpAddr::V4(ip).into()); + break (format!("{ip}:{port}"), server_name); } (Some(Protocol::Ip6(ip)), Some(Protocol::Tcp(port))) => { - break (format!("{ip}:{port}"), None) + let server_name = ServerName::IpAddress(IpAddr::V6(ip).into()); + break (format!("[{ip}]:{port}"), server_name); } (Some(Protocol::Dns(h)), Some(Protocol::Tcp(port))) | (Some(Protocol::Dns4(h)), Some(Protocol::Tcp(port))) | (Some(Protocol::Dns6(h)), Some(Protocol::Tcp(port))) | (Some(Protocol::Dnsaddr(h)), Some(Protocol::Tcp(port))) => { - break (format!("{}:{}", &h, port), Some(tls::dns_name_ref(&h)?)) + break (format!("{h}:{port}"), tls::dns_name_ref(&h)?) } (Some(_), Some(p)) => { ip = Some(p); @@ -508,20 +530,21 @@ fn parse_ws_dial_addr(addr: Multiaddr) -> Result> { let (use_tls, path) = loop { match protocols.pop() { p @ Some(Protocol::P2p(_)) => p2p = p, - Some(Protocol::Ws(path)) => break (false, path.into_owned()), - Some(Protocol::Wss(path)) => { - if dns_name.is_none() { - tracing::debug!(addrress=%addr, "Missing DNS name in WSS address"); - return Err(Error::InvalidMultiaddr(addr)); + Some(Protocol::Ws(path)) => match protocols.pop() { + Some(Protocol::Tls) => break (true, path.into_owned()), + Some(p) => { + protocols.push(p); + break (false, path.into_owned()); } - break (true, path.into_owned()); - } + None => return Err(Error::InvalidMultiaddr(addr)), + }, + Some(Protocol::Wss(path)) => break (true, path.into_owned()), _ => return Err(Error::InvalidMultiaddr(addr)), } }; // The original address, stripped of the `/ws` and `/wss` protocols, - // makes up the the address for the inner TCP-based transport. + // makes up the address for the inner TCP-based transport. let tcp_addr = match p2p { Some(p) => protocols.with(p), None => protocols, @@ -529,13 +552,29 @@ fn parse_ws_dial_addr(addr: Multiaddr) -> Result> { Ok(WsAddress { host_port, - dns_name, + server_name, path, use_tls, tcp_addr, }) } +fn parse_ws_listen_addr(addr: &Multiaddr) -> Option<(Multiaddr, WsListenProto<'static>)> { + let mut inner_addr = addr.clone(); + + match inner_addr.pop()? { + Protocol::Wss(path) => Some((inner_addr, WsListenProto::Wss(path))), + Protocol::Ws(path) => match inner_addr.pop()? { + Protocol::Tls => Some((inner_addr, WsListenProto::TlsWs(path))), + p => { + inner_addr.push(p); + Some((inner_addr, WsListenProto::Ws(path))) + } + }, + _ => None, + } +} + // Given a location URL, build a new websocket [`Multiaddr`]. fn location_to_multiaddr(location: &str) -> Result> { match Url::parse(location) { @@ -552,7 +591,8 @@ fn location_to_multiaddr(location: &str) -> Result> { } let s = url.scheme(); if s.eq_ignore_ascii_case("https") | s.eq_ignore_ascii_case("wss") { - a.push(Protocol::Wss(url.path().into())) + a.push(Protocol::Tls); + a.push(Protocol::Ws(url.path().into())); } else if s.eq_ignore_ascii_case("http") | s.eq_ignore_ascii_case("ws") { a.push(Protocol::Ws(url.path().into())) } else { @@ -571,7 +611,7 @@ fn location_to_multiaddr(location: &str) -> Result> { /// The websocket connection. pub struct Connection { receiver: BoxStream<'static, Result>, - sender: Pin + Send>>, + sender: Pin> + Send>>, _marker: std::marker::PhantomData, } @@ -767,3 +807,194 @@ where .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) } } + +#[cfg(test)] +mod tests { + use super::*; + use libp2p_identity::PeerId; + use std::io; + + #[test] + fn listen_addr() { + let tcp_addr = "/ip4/0.0.0.0/tcp/2222".parse::().unwrap(); + + // Check `/tls/ws` + let addr = tcp_addr + .clone() + .with(Protocol::Tls) + .with(Protocol::Ws("/".into())); + let (inner_addr, proto) = parse_ws_listen_addr(&addr).unwrap(); + assert_eq!(&inner_addr, &tcp_addr); + assert_eq!(proto, WsListenProto::TlsWs("/".into())); + + let mut listen_addr = tcp_addr.clone(); + proto.append_on_addr(&mut listen_addr); + assert_eq!(listen_addr, addr); + + // Check `/wss` + let addr = tcp_addr.clone().with(Protocol::Wss("/".into())); + let (inner_addr, proto) = parse_ws_listen_addr(&addr).unwrap(); + assert_eq!(&inner_addr, &tcp_addr); + assert_eq!(proto, WsListenProto::Wss("/".into())); + + let mut listen_addr = tcp_addr.clone(); + proto.append_on_addr(&mut listen_addr); + assert_eq!(listen_addr, addr); + + // Check `/ws` + let addr = tcp_addr.clone().with(Protocol::Ws("/".into())); + let (inner_addr, proto) = parse_ws_listen_addr(&addr).unwrap(); + assert_eq!(&inner_addr, &tcp_addr); + assert_eq!(proto, WsListenProto::Ws("/".into())); + + let mut listen_addr = tcp_addr.clone(); + proto.append_on_addr(&mut listen_addr); + assert_eq!(listen_addr, addr); + } + + #[test] + fn dial_addr() { + let peer_id = PeerId::random(); + + // Check `/tls/ws` + let addr = "/dns4/example.com/tcp/2222/tls/ws" + .parse::() + .unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "example.com:2222"); + assert_eq!(info.path, "/"); + assert!(info.use_tls); + assert_eq!(info.server_name, "example.com".try_into().unwrap()); + assert_eq!(info.tcp_addr, "/dns4/example.com/tcp/2222".parse().unwrap()); + + // Check `/tls/ws` with `/p2p` + let addr = format!("/dns4/example.com/tcp/2222/tls/ws/p2p/{peer_id}") + .parse() + .unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "example.com:2222"); + assert_eq!(info.path, "/"); + assert!(info.use_tls); + assert_eq!(info.server_name, "example.com".try_into().unwrap()); + assert_eq!( + info.tcp_addr, + format!("/dns4/example.com/tcp/2222/p2p/{peer_id}") + .parse() + .unwrap() + ); + + // Check `/tls/ws` with `/ip4` + let addr = "/ip4/127.0.0.1/tcp/2222/tls/ws" + .parse::() + .unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "127.0.0.1:2222"); + assert_eq!(info.path, "/"); + assert!(info.use_tls); + assert_eq!(info.server_name, "127.0.0.1".try_into().unwrap()); + assert_eq!(info.tcp_addr, "/ip4/127.0.0.1/tcp/2222".parse().unwrap()); + + // Check `/tls/ws` with `/ip6` + let addr = "/ip6/::1/tcp/2222/tls/ws".parse::().unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "[::1]:2222"); + assert_eq!(info.path, "/"); + assert!(info.use_tls); + assert_eq!(info.server_name, "::1".try_into().unwrap()); + assert_eq!(info.tcp_addr, "/ip6/::1/tcp/2222".parse().unwrap()); + + // Check `/wss` + let addr = "/dns4/example.com/tcp/2222/wss" + .parse::() + .unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "example.com:2222"); + assert_eq!(info.path, "/"); + assert!(info.use_tls); + assert_eq!(info.server_name, "example.com".try_into().unwrap()); + assert_eq!(info.tcp_addr, "/dns4/example.com/tcp/2222".parse().unwrap()); + + // Check `/wss` with `/p2p` + let addr = format!("/dns4/example.com/tcp/2222/wss/p2p/{peer_id}") + .parse() + .unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "example.com:2222"); + assert_eq!(info.path, "/"); + assert!(info.use_tls); + assert_eq!(info.server_name, "example.com".try_into().unwrap()); + assert_eq!( + info.tcp_addr, + format!("/dns4/example.com/tcp/2222/p2p/{peer_id}") + .parse() + .unwrap() + ); + + // Check `/wss` with `/ip4` + let addr = "/ip4/127.0.0.1/tcp/2222/wss".parse::().unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "127.0.0.1:2222"); + assert_eq!(info.path, "/"); + assert!(info.use_tls); + assert_eq!(info.server_name, "127.0.0.1".try_into().unwrap()); + assert_eq!(info.tcp_addr, "/ip4/127.0.0.1/tcp/2222".parse().unwrap()); + + // Check `/wss` with `/ip6` + let addr = "/ip6/::1/tcp/2222/wss".parse::().unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "[::1]:2222"); + assert_eq!(info.path, "/"); + assert!(info.use_tls); + assert_eq!(info.server_name, "::1".try_into().unwrap()); + assert_eq!(info.tcp_addr, "/ip6/::1/tcp/2222".parse().unwrap()); + + // Check `/ws` + let addr = "/dns4/example.com/tcp/2222/ws" + .parse::() + .unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "example.com:2222"); + assert_eq!(info.path, "/"); + assert!(!info.use_tls); + assert_eq!(info.server_name, "example.com".try_into().unwrap()); + assert_eq!(info.tcp_addr, "/dns4/example.com/tcp/2222".parse().unwrap()); + + // Check `/ws` with `/p2p` + let addr = format!("/dns4/example.com/tcp/2222/ws/p2p/{peer_id}") + .parse() + .unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "example.com:2222"); + assert_eq!(info.path, "/"); + assert!(!info.use_tls); + assert_eq!(info.server_name, "example.com".try_into().unwrap()); + assert_eq!( + info.tcp_addr, + format!("/dns4/example.com/tcp/2222/p2p/{peer_id}") + .parse() + .unwrap() + ); + + // Check `/ws` with `/ip4` + let addr = "/ip4/127.0.0.1/tcp/2222/ws".parse::().unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "127.0.0.1:2222"); + assert_eq!(info.path, "/"); + assert!(!info.use_tls); + assert_eq!(info.server_name, "127.0.0.1".try_into().unwrap()); + assert_eq!(info.tcp_addr, "/ip4/127.0.0.1/tcp/2222".parse().unwrap()); + + // Check `/ws` with `/ip6` + let addr = "/ip6/::1/tcp/2222/ws".parse::().unwrap(); + let info = parse_ws_dial_addr::(addr).unwrap(); + assert_eq!(info.host_port, "[::1]:2222"); + assert_eq!(info.path, "/"); + assert!(!info.use_tls); + assert_eq!(info.server_name, "::1".try_into().unwrap()); + assert_eq!(info.tcp_addr, "/ip6/::1/tcp/2222".parse().unwrap()); + + // Check non-ws address + let addr = "/ip4/127.0.0.1/tcp/2222".parse::().unwrap(); + parse_ws_dial_addr::(addr).unwrap_err(); + } +} diff --git a/transports/websocket/src/lib.rs b/transports/websocket/src/lib.rs index e0b3d09ca25..cbc923613dd 100644 --- a/transports/websocket/src/lib.rs +++ b/transports/websocket/src/lib.rs @@ -33,7 +33,7 @@ use futures::{future::BoxFuture, prelude::*, ready}; use libp2p_core::{ connection::ConnectedPoint, multiaddr::Multiaddr, - transport::{map::MapFuture, ListenerId, TransportError, TransportEvent}, + transport::{map::MapFuture, DialOpts, ListenerId, TransportError, TransportEvent}, Transport, }; use rw_stream_sink::RwStreamSink; @@ -84,7 +84,7 @@ use std::{ /// let cert = websocket::tls::Certificate::new(rcgen_cert.serialize_der().unwrap()); /// transport.set_tls_config(websocket::tls::Config::new(priv_key, vec![cert]).unwrap()); /// -/// let id = transport.listen_on(ListenerId::next(), "/ip4/127.0.0.1/tcp/0/wss".parse().unwrap()).unwrap(); +/// let id = transport.listen_on(ListenerId::next(), "/ip4/127.0.0.1/tcp/0/tls/ws".parse().unwrap()).unwrap(); /// /// let addr = future::poll_fn(|cx| Pin::new(&mut transport).poll(cx)).await.into_new_address().unwrap(); /// println!("Listening on {addr}"); @@ -202,19 +202,12 @@ where self.transport.remove_listener(id) } - fn dial(&mut self, addr: Multiaddr) -> Result> { - self.transport.dial(addr) - } - - fn dial_as_listener( + fn dial( &mut self, addr: Multiaddr, + opts: DialOpts, ) -> Result> { - self.transport.dial_as_listener(addr) - } - - fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { - self.transport.address_translation(server, observed) + self.transport.dial(addr, opts) } fn poll( @@ -292,7 +285,11 @@ where mod tests { use super::WsConfig; use futures::prelude::*; - use libp2p_core::{multiaddr::Protocol, transport::ListenerId, Multiaddr, Transport}; + use libp2p_core::{ + multiaddr::Protocol, + transport::{DialOpts, ListenerId, PortUse}, + Endpoint, Multiaddr, Transport, + }; use libp2p_identity::PeerId; use libp2p_tcp as tcp; @@ -339,7 +336,13 @@ mod tests { let outbound = new_ws_config() .boxed() - .dial(addr.with(Protocol::P2p(PeerId::random()))) + .dial( + addr.with(Protocol::P2p(PeerId::random())), + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::New, + }, + ) .unwrap(); let (a, b) = futures::join!(inbound, outbound); diff --git a/transports/websocket/src/quicksink.rs b/transports/websocket/src/quicksink.rs index d9edb4dfe0d..4f620536ea1 100644 --- a/transports/websocket/src/quicksink.rs +++ b/transports/websocket/src/quicksink.rs @@ -30,14 +30,6 @@ // Ok::<_, io::Error>(stdout) // }); // ``` -// -// # Panics -// -// - If any of the [`Sink`] methods produce an error, the sink transitions -// to a failure state and none of its methods must be called afterwards or -// else a panic will occur. -// - If [`Sink::poll_close`] has been called, no other sink method must be -// called afterwards or else a panic will be caused. use futures::{ready, sink::Sink}; use pin_project_lite::pin_project; @@ -102,6 +94,15 @@ enum State { Failed, } +/// Errors the `Sink` may return. +#[derive(Debug, thiserror::Error)] +pub(crate) enum Error { + #[error("Error while sending over the sink, {0}")] + Send(E), + #[error("The Sink has closed")] + Closed, +} + pin_project! { /// `SinkImpl` implements the `Sink` trait. #[derive(Debug)] @@ -119,7 +120,7 @@ where F: FnMut(S, Action) -> T, T: Future>, { - type Error = E; + type Error = Error; fn poll_ready(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { let mut this = self.project(); @@ -135,7 +136,7 @@ where Err(e) => { this.future.set(None); *this.state = State::Failed; - Poll::Ready(Err(e)) + Poll::Ready(Err(Error::Send(e))) } } } @@ -143,20 +144,19 @@ where Ok(_) => { this.future.set(None); *this.state = State::Closed; - panic!("SinkImpl::poll_ready called on a closing sink.") + Poll::Ready(Err(Error::Closed)) } Err(e) => { this.future.set(None); *this.state = State::Failed; - Poll::Ready(Err(e)) + Poll::Ready(Err(Error::Send(e))) } }, State::Empty => { assert!(this.param.is_some()); Poll::Ready(Ok(())) } - State::Closed => panic!("SinkImpl::poll_ready called on a closed sink."), - State::Failed => panic!("SinkImpl::poll_ready called after error."), + State::Closed | State::Failed => Poll::Ready(Err(Error::Closed)), } } @@ -193,7 +193,7 @@ where Err(e) => { this.future.set(None); *this.state = State::Failed; - return Poll::Ready(Err(e)); + return Poll::Ready(Err(Error::Send(e))); } }, State::Flushing => { @@ -207,7 +207,7 @@ where Err(e) => { this.future.set(None); *this.state = State::Failed; - return Poll::Ready(Err(e)); + return Poll::Ready(Err(Error::Send(e))); } } } @@ -221,11 +221,10 @@ where Err(e) => { this.future.set(None); *this.state = State::Failed; - return Poll::Ready(Err(e)); + return Poll::Ready(Err(Error::Send(e))); } }, - State::Closed => return Poll::Ready(Ok(())), - State::Failed => panic!("SinkImpl::poll_flush called after error."), + State::Closed | State::Failed => return Poll::Ready(Err(Error::Closed)), } } } @@ -253,7 +252,7 @@ where Err(e) => { this.future.set(None); *this.state = State::Failed; - return Poll::Ready(Err(e)); + return Poll::Ready(Err(Error::Send(e))); } }, State::Flushing => { @@ -266,7 +265,7 @@ where Err(e) => { this.future.set(None); *this.state = State::Failed; - return Poll::Ready(Err(e)); + return Poll::Ready(Err(Error::Send(e))); } } } @@ -280,11 +279,11 @@ where Err(e) => { this.future.set(None); *this.state = State::Failed; - return Poll::Ready(Err(e)); + return Poll::Ready(Err(Error::Send(e))); } }, State::Closed => return Poll::Ready(Ok(())), - State::Failed => panic!("SinkImpl::poll_closed called after error."), + State::Failed => return Poll::Ready(Err(Error::Closed)), } } } @@ -294,7 +293,7 @@ where mod tests { use crate::quicksink::{make_sink, Action}; use async_std::{io, task}; - use futures::{channel::mpsc, prelude::*, stream}; + use futures::{channel::mpsc, prelude::*}; #[test] fn smoke_test() { @@ -347,4 +346,31 @@ mod tests { assert_eq!(&expected[..], &actual[..]) }); } + + #[test] + fn error_does_not_panic() { + task::block_on(async { + let sink = make_sink(io::stdout(), |mut _stdout, _action| async move { + Err(io::Error::new(io::ErrorKind::Other, "oh no")) + }); + + futures::pin_mut!(sink); + + let result = sink.send("hello").await; + match result { + Err(crate::quicksink::Error::Send(e)) => { + assert_eq!(e.kind(), io::ErrorKind::Other); + assert_eq!(e.to_string(), "oh no") + } + _ => panic!("unexpected result: {:?}", result), + }; + + // Call send again, expect not to panic. + let result = sink.send("hello").await; + match result { + Err(crate::quicksink::Error::Closed) => {} + _ => panic!("unexpected result: {:?}", result), + }; + }) + } } diff --git a/transports/websocket/src/tls.rs b/transports/websocket/src/tls.rs index 5bff818f34c..77090e21675 100644 --- a/transports/websocket/src/tls.rs +++ b/transports/websocket/src/tls.rs @@ -19,7 +19,6 @@ // DEALINGS IN THE SOFTWARE. use futures_rustls::{rustls, TlsAcceptor, TlsConnector}; -use std::convert::TryFrom; use std::{fmt, io, sync::Arc}; /// TLS configuration. @@ -36,24 +35,32 @@ impl fmt::Debug for Config { } /// Private key, DER-encoded ASN.1 in either PKCS#8 or PKCS#1 format. -#[derive(Clone)] -pub struct PrivateKey(rustls::PrivateKey); +pub struct PrivateKey(rustls::pki_types::PrivateKeyDer<'static>); impl PrivateKey { /// Assert the given bytes are DER-encoded ASN.1 in either PKCS#8 or PKCS#1 format. pub fn new(bytes: Vec) -> Self { - PrivateKey(rustls::PrivateKey(bytes)) + PrivateKey( + rustls::pki_types::PrivateKeyDer::try_from(bytes) + .expect("unknown or invalid key format"), + ) + } +} + +impl Clone for PrivateKey { + fn clone(&self) -> Self { + Self(self.0.clone_key()) } } /// Certificate, DER-encoded X.509 format. #[derive(Debug, Clone)] -pub struct Certificate(rustls::Certificate); +pub struct Certificate(rustls::pki_types::CertificateDer<'static>); impl Certificate { /// Assert the given bytes are in DER-encoded X.509 format. pub fn new(bytes: Vec) -> Self { - Certificate(rustls::Certificate(bytes)) + Certificate(rustls::pki_types::CertificateDer::from(bytes)) } } @@ -70,8 +77,10 @@ impl Config { /// Create a client-only configuration. pub fn client() -> Self { - let client = rustls::ClientConfig::builder() - .with_safe_defaults() + let provider = rustls::crypto::ring::default_provider(); + let client = rustls::ClientConfig::builder_with_provider(provider.into()) + .with_safe_default_protocol_versions() + .unwrap() .with_root_certificates(client_root_store()) .with_no_client_auth(); Config { @@ -92,12 +101,12 @@ impl Config { /// Setup the rustls client configuration. fn client_root_store() -> rustls::RootCertStore { let mut client_root_store = rustls::RootCertStore::empty(); - client_root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| { - rustls::OwnedTrustAnchor::from_subject_spki_name_constraints( - ta.subject, - ta.spki, - ta.name_constraints, - ) + client_root_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| { + rustls::pki_types::TrustAnchor { + subject: ta.subject.into(), + subject_public_key_info: ta.spki.into(), + name_constraints: ta.name_constraints.map(|v| v.into()), + } })); client_root_store } @@ -115,8 +124,10 @@ impl Builder { I: IntoIterator, { let certs = certs.into_iter().map(|c| c.0).collect(); - let server = rustls::ServerConfig::builder() - .with_safe_defaults() + let provider = rustls::crypto::ring::default_provider(); + let server = rustls::ServerConfig::builder_with_provider(provider.into()) + .with_safe_default_protocol_versions() + .unwrap() .with_no_client_auth() .with_single_cert(certs, key.0) .map_err(|e| Error::Tls(Box::new(e)))?; @@ -127,15 +138,17 @@ impl Builder { /// Add an additional trust anchor. pub fn add_trust(&mut self, cert: &Certificate) -> Result<&mut Self, Error> { self.client_root_store - .add(&cert.0) + .add(cert.0.to_owned()) .map_err(|e| Error::Tls(Box::new(e)))?; Ok(self) } /// Finish configuration. pub fn finish(self) -> Config { - let client = rustls::ClientConfig::builder() - .with_safe_defaults() + let provider = rustls::crypto::ring::default_provider(); + let client = rustls::ClientConfig::builder_with_provider(provider.into()) + .with_safe_default_protocol_versions() + .unwrap() .with_root_certificates(self.client_root_store) .with_no_client_auth(); @@ -146,8 +159,9 @@ impl Builder { } } -pub(crate) fn dns_name_ref(name: &str) -> Result { - rustls::ServerName::try_from(name).map_err(|_| Error::InvalidDnsName(name.into())) +pub(crate) fn dns_name_ref(name: &str) -> Result, Error> { + rustls::pki_types::ServerName::try_from(String::from(name)) + .map_err(|_| Error::InvalidDnsName(name.into())) } // Error ////////////////////////////////////////////////////////////////////////////////////////// diff --git a/transports/webtransport-websys/CHANGELOG.md b/transports/webtransport-websys/CHANGELOG.md index b368a943395..411117918bd 100644 --- a/transports/webtransport-websys/CHANGELOG.md +++ b/transports/webtransport-websys/CHANGELOG.md @@ -1,3 +1,18 @@ +## 0.4.0 + +- Implement refactored `Transport`. + See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568) +- Bump version of web-sys and wasm-bindgen. + See [PR 5569](https://github.com/libp2p/rust-libp2p/pull/5569) + +## 0.3.0 + +* Fix unhandled exceptions thrown when calling `Webtransport::close`. + See [PR 5390](https://github.com/libp2p/rust-libp2p/pull/5390). +* Change logs to debug level. + See [PR 5396](https://github.com/libp2p/rust-libp2p/pull/5396). + + ## 0.2.0 diff --git a/transports/webtransport-websys/Cargo.toml b/transports/webtransport-websys/Cargo.toml index f71b6db5d87..9541c49b737 100644 --- a/transports/webtransport-websys/Cargo.toml +++ b/transports/webtransport-websys/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-webtransport-websys" edition = "2021" rust-version = { workspace = true } description = "WebTransport for libp2p under WASM environment" -version = "0.2.0" +version = "0.4.0" authors = [ "Yiannis Marangos ", "oblique ", @@ -14,19 +14,19 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] -futures = "0.3.30" -js-sys = "0.3.67" +futures = { workspace = true } +js-sys = "0.3.70" libp2p-core = { workspace = true } libp2p-identity = { workspace = true } libp2p-noise = { workspace = true } multiaddr = { workspace = true } multihash = { workspace = true } send_wrapper = { version = "0.6.0", features = ["futures"] } -thiserror = "1.0.57" -tracing = "0.1.37" -wasm-bindgen = "0.2.90" -wasm-bindgen-futures = "0.4.42" -web-sys = { version = "0.3.68", features = [ +thiserror = "1.0.61" +tracing = { workspace = true } +wasm-bindgen = "0.2.93" +wasm-bindgen-futures = "0.4.43" +web-sys = { version = "0.3.70", features = [ "ReadableStreamDefaultReader", "WebTransport", "WebTransportBidirectionalStream", @@ -44,8 +44,6 @@ multibase = "0.9.1" # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] -rustc-args = ["--cfg", "docsrs"] [lints] workspace = true diff --git a/transports/webtransport-websys/src/connection.rs b/transports/webtransport-websys/src/connection.rs index 982f9e5a32c..956a66288af 100644 --- a/transports/webtransport-websys/src/connection.rs +++ b/transports/webtransport-websys/src/connection.rs @@ -47,6 +47,12 @@ impl Connection { let opts = endpoint.webtransport_opts(); WebTransport::new_with_options(&url, &opts).map_err(Error::from_js_value)? }; + // Create a promise that will resolve once session is closed. + // It will catch the errors that can eventually happen when + // `.close()` is called. Without it, there is no way of catching + // those from the `.close()` itself, resulting in `Uncaught in promise...` + // logs popping up. + detach_promise(session.closed()); let incoming_streams = session.incoming_bidirectional_streams(); let incoming_streams_reader = diff --git a/transports/webtransport-websys/src/transport.rs b/transports/webtransport-websys/src/transport.rs index cb556ffef99..6a9a9dad954 100644 --- a/transports/webtransport-websys/src/transport.rs +++ b/transports/webtransport-websys/src/transport.rs @@ -1,6 +1,8 @@ use futures::future::FutureExt; use libp2p_core::muxing::StreamMuxerBox; -use libp2p_core::transport::{Boxed, ListenerId, Transport as _, TransportError, TransportEvent}; +use libp2p_core::transport::{ + Boxed, DialOpts, ListenerId, Transport as _, TransportError, TransportEvent, +}; use libp2p_identity::{Keypair, PeerId}; use multiaddr::Multiaddr; use std::future::Future; @@ -62,10 +64,18 @@ impl libp2p_core::Transport for Transport { false } - fn dial(&mut self, addr: Multiaddr) -> Result> { + fn dial( + &mut self, + addr: Multiaddr, + dial_opts: DialOpts, + ) -> Result> { + if dial_opts.role.is_listener() { + return Err(TransportError::MultiaddrNotSupported(addr)); + } + let endpoint = Endpoint::from_multiaddr(&addr).map_err(|e| match e { e @ Error::InvalidMultiaddr(_) => { - tracing::warn!("{}", e); + tracing::debug!("{}", e); TransportError::MultiaddrNotSupported(addr) } e => TransportError::Other(e), @@ -83,21 +93,10 @@ impl libp2p_core::Transport for Transport { .boxed()) } - fn dial_as_listener( - &mut self, - addr: Multiaddr, - ) -> Result> { - Err(TransportError::MultiaddrNotSupported(addr)) - } - fn poll( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll> { Poll::Pending } - - fn address_translation(&self, _listen: &Multiaddr, _observed: &Multiaddr) -> Option { - None - } } diff --git a/wasm-tests/README.md b/wasm-tests/README.md index 1d0902b106c..2538e48a145 100644 --- a/wasm-tests/README.md +++ b/wasm-tests/README.md @@ -8,4 +8,4 @@ Before you run the tests you need to install the following: # Run tests -Just call `run-all.sh` or `run.sh` in the test directory you are interested. +Just call `run-all.sh` or `run.sh` in the test directory if you are interested. diff --git a/wasm-tests/webtransport-tests/Cargo.toml b/wasm-tests/webtransport-tests/Cargo.toml index b98e56440b1..d7db378ab1a 100644 --- a/wasm-tests/webtransport-tests/Cargo.toml +++ b/wasm-tests/webtransport-tests/Cargo.toml @@ -9,18 +9,18 @@ publish = false release = false [dependencies] -futures = "0.3.30" -getrandom = { version = "0.2.12", features = ["js"] } +futures = { workspace = true } +getrandom = { version = "0.2.15", features = ["js"] } libp2p-core = { workspace = true } libp2p-identity = { workspace = true, features = ["rand"] } libp2p-noise = { workspace = true } libp2p-webtransport-websys = { workspace = true } multiaddr = { workspace = true } multihash = { workspace = true } -wasm-bindgen = "0.2.90" -wasm-bindgen-futures = "0.4.42" -wasm-bindgen-test = "0.3.41" -web-sys = { version = "0.3.68", features = ["Response", "Window"] } +wasm-bindgen = "0.2.93" +wasm-bindgen-futures = "0.4.43" +wasm-bindgen-test = "0.3.43" +web-sys = { version = "0.3.70", features = ["Response", "Window"] } [lints] workspace = true diff --git a/wasm-tests/webtransport-tests/echo-server/Dockerfile b/wasm-tests/webtransport-tests/echo-server/Dockerfile index f498e2baa1b..2d973c49041 100644 --- a/wasm-tests/webtransport-tests/echo-server/Dockerfile +++ b/wasm-tests/webtransport-tests/echo-server/Dockerfile @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM docker.io/library/golang:1.20 AS builder +FROM docker.io/library/golang:1.22 AS builder WORKDIR /workspace ADD . . RUN CGO_ENABLED=0 go build . diff --git a/wasm-tests/webtransport-tests/echo-server/go.mod b/wasm-tests/webtransport-tests/echo-server/go.mod index 12804780d8b..e2e0c6591ba 100644 --- a/wasm-tests/webtransport-tests/echo-server/go.mod +++ b/wasm-tests/webtransport-tests/echo-server/go.mod @@ -1,64 +1,55 @@ module echo-server -go 1.20 +go 1.22 require ( - github.com/libp2p/go-libp2p v0.27.8 - github.com/multiformats/go-multiaddr v0.9.0 + github.com/libp2p/go-libp2p v0.33.1 + github.com/multiformats/go-multiaddr v0.12.2 + github.com/quic-go/quic-go v0.42.0 ) require ( - github.com/benbjohnson/clock v1.3.0 // indirect - github.com/beorn7/perks v1.0.1 // indirect - github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/benbjohnson/clock v1.3.5 // indirect github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect - github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect - github.com/flynn/noise v1.0.0 // indirect + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect + github.com/flynn/noise v1.1.0 // indirect github.com/francoispqt/gojay v1.2.13 // indirect github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect - github.com/golang/mock v1.6.0 // indirect - github.com/golang/protobuf v1.5.3 // indirect github.com/google/gopacket v1.1.19 // indirect - github.com/google/pprof v0.0.0-20230405160723-4a4c7d95572b // indirect + github.com/google/pprof v0.0.0-20240207164012-fb44976bdcd5 // indirect github.com/ipfs/go-cid v0.4.1 // indirect github.com/ipfs/go-log/v2 v2.5.1 // indirect github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect - github.com/klauspost/compress v1.16.4 // indirect - github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/klauspost/compress v1.17.6 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/libp2p/go-buffer-pool v0.1.0 // indirect github.com/libp2p/go-netroute v0.2.1 // indirect - github.com/mattn/go-isatty v0.0.18 // indirect - github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect - github.com/minio/sha256-simd v1.0.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/minio/sha256-simd v1.0.1 // indirect github.com/mr-tron/base58 v1.2.0 // indirect github.com/multiformats/go-base32 v0.1.0 // indirect github.com/multiformats/go-base36 v0.2.0 // indirect github.com/multiformats/go-multibase v0.2.0 // indirect - github.com/multiformats/go-multicodec v0.8.1 // indirect - github.com/multiformats/go-multihash v0.2.1 // indirect - github.com/multiformats/go-multistream v0.4.1 // indirect + github.com/multiformats/go-multicodec v0.9.0 // indirect + github.com/multiformats/go-multihash v0.2.3 // indirect + github.com/multiformats/go-multistream v0.5.0 // indirect github.com/multiformats/go-varint v0.0.7 // indirect - github.com/onsi/ginkgo/v2 v2.9.2 // indirect - github.com/prometheus/client_golang v1.14.0 // indirect - github.com/prometheus/client_model v0.3.0 // indirect - github.com/prometheus/common v0.42.0 // indirect - github.com/prometheus/procfs v0.9.0 // indirect + github.com/onsi/ginkgo/v2 v2.15.0 // indirect github.com/quic-go/qpack v0.4.0 // indirect - github.com/quic-go/qtls-go1-19 v0.3.3 // indirect - github.com/quic-go/qtls-go1-20 v0.2.3 // indirect - github.com/quic-go/quic-go v0.33.0 // indirect - github.com/quic-go/webtransport-go v0.5.2 // indirect + github.com/quic-go/webtransport-go v0.6.0 // indirect + github.com/rogpeppe/go-internal v1.10.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect - go.uber.org/atomic v1.10.0 // indirect + go.uber.org/mock v0.4.0 // indirect go.uber.org/multierr v1.11.0 // indirect - go.uber.org/zap v1.24.0 // indirect - golang.org/x/crypto v0.17.0 // indirect - golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect - golang.org/x/mod v0.10.0 // indirect - golang.org/x/net v0.10.0 // indirect - golang.org/x/sys v0.15.0 // indirect + go.uber.org/zap v1.27.0 // indirect + golang.org/x/crypto v0.21.0 // indirect + golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect + golang.org/x/mod v0.15.0 // indirect + golang.org/x/net v0.23.0 // indirect + golang.org/x/sys v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect - golang.org/x/tools v0.7.0 // indirect + golang.org/x/tools v0.18.0 // indirect google.golang.org/protobuf v1.33.0 // indirect - lukechampine.com/blake3 v1.1.7 // indirect + lukechampine.com/blake3 v1.2.1 // indirect ) diff --git a/wasm-tests/webtransport-tests/echo-server/go.sum b/wasm-tests/webtransport-tests/echo-server/go.sum index 61dfda6a67d..a9d53233159 100644 --- a/wasm-tests/webtransport-tests/echo-server/go.sum +++ b/wasm-tests/webtransport-tests/echo-server/go.sum @@ -10,36 +10,35 @@ git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGy github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= -github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= -github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o= +github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= -github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= -github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= -github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= -github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c h1:pFUpOrbxDR6AkioZ1ySsx5yxlDQZ8stG2b88gTPxgJU= github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c/go.mod h1:6UhI8N9EjYm1c2odKpFpAYeR8dsBeM7PtzQhRgxRr9U= -github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0= -github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4= -github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc= +github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y= +github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= -github.com/flynn/noise v1.0.0 h1:DlTHqmzmvcEiKj+4RYo/imoswx/4r6iBlCMfVtrMXpQ= -github.com/flynn/noise v1.0.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwUTag= +github.com/flynn/noise v1.1.0 h1:KjPQoQCEFdZDiP03phOvGi11+SVVhBG2wOWAorLsstg= +github.com/flynn/noise v1.1.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwUTag= github.com/francoispqt/gojay v1.2.13 h1:d2m3sFjloqoIUQU3TsHBgj6qg/BVGlTBeHDUmyJnXKk= github.com/francoispqt/gojay v1.2.13/go.mod h1:ehT5mTG4ua4581f1++1WLG0vPdaA9HaiDsoyrBGkyDY= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= -github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= +github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -47,26 +46,22 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= -github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= -github.com/google/pprof v0.0.0-20230405160723-4a4c7d95572b h1:Qcx5LM0fSiks9uCyFZwDBUasd3lxd1RM0GYpL+Li5o4= -github.com/google/pprof v0.0.0-20230405160723-4a4c7d95572b/go.mod h1:79YE0hCXdHag9sBkw2o+N/YnZtTkXi0UT9Nnixa5eYk= +github.com/google/pprof v0.0.0-20240207164012-fb44976bdcd5 h1:E/LAvt58di64hlYjx7AsNS6C/ysHWYo+2qPCZKTQhRo= +github.com/google/pprof v0.0.0-20240207164012-fb44976bdcd5/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik= github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY= github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= @@ -82,38 +77,38 @@ github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0 github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/klauspost/compress v1.16.4 h1:91KN02FnsOYhuunwU4ssRe8lc2JosWmizWa91B5v1PU= -github.com/klauspost/compress v1.16.4/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= -github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= +github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI= +github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6cdF0Y8= github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg= -github.com/libp2p/go-libp2p v0.27.8 h1:IX5x/4yKwyPQeVS2AXHZ3J4YATM9oHBGH1gBc23jBAI= -github.com/libp2p/go-libp2p v0.27.8/go.mod h1:eCFFtd0s5i/EVKR7+5Ki8bM7qwkNW3TPTTSSW9sz8NE= +github.com/libp2p/go-libp2p v0.33.1 h1:tvJl9b9M6nSLBtZSXSguq+/lRhRj2oLRkyhBmQNMFLA= +github.com/libp2p/go-libp2p v0.33.1/go.mod h1:zOUTMjG4I7TXwMndNyOBn/CNtVBLlvBlnxfi+8xzx+E= github.com/libp2p/go-msgio v0.3.0 h1:mf3Z8B1xcFN314sWX+2vOTShIE0Mmn2TXn3YCUQGNj0= +github.com/libp2p/go-msgio v0.3.0/go.mod h1:nyRM819GmVaF9LX3l03RMh10QdOroF++NBbxAb0mmDM= github.com/libp2p/go-netroute v0.2.1 h1:V8kVrpD8GK0Riv15/7VN6RbUQ3URNZVosw7H2v9tksU= github.com/libp2p/go-netroute v0.2.1/go.mod h1:hraioZr0fhBjG0ZRXJJ6Zj2IVEVNx6tDTFQfSmcq7mQ= -github.com/libp2p/go-yamux/v4 v4.0.0 h1:+Y80dV2Yx/kv7Y7JKu0LECyVdMXm1VUoko+VQ9rBfZQ= +github.com/libp2p/go-yamux/v4 v4.0.1 h1:FfDR4S1wj6Bw2Pqbc8Uz7pCxeRBPbwsBbEdfwiCypkQ= +github.com/libp2p/go-yamux/v4 v4.0.1/go.mod h1:NWjl8ZTLOGlozrXSOZ/HlfG++39iKNnM5wwmtQP1YB4= github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= -github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= -github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= -github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4= -github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g= -github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= +github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= +github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= @@ -122,50 +117,42 @@ github.com/multiformats/go-base32 v0.1.0 h1:pVx9xoSPqEIQG8o+UbAe7DNi51oej1NtK+aG github.com/multiformats/go-base32 v0.1.0/go.mod h1:Kj3tFY6zNr+ABYMqeUNeGvkIC/UYgtWibDcT0rExnbI= github.com/multiformats/go-base36 v0.2.0 h1:lFsAbNOGeKtuKozrtBsAkSVhv1p9D0/qedU9rQyccr0= github.com/multiformats/go-base36 v0.2.0/go.mod h1:qvnKE++v+2MWCfePClUEjE78Z7P2a1UV0xHgWc0hkp4= -github.com/multiformats/go-multiaddr v0.9.0 h1:3h4V1LHIk5w4hJHekMKWALPXErDfz/sggzwC/NcqbDQ= -github.com/multiformats/go-multiaddr v0.9.0/go.mod h1:mI67Lb1EeTOYb8GQfL/7wpIZwc46ElrvzhYnoJOmTT0= +github.com/multiformats/go-multiaddr v0.12.2 h1:9G9sTY/wCYajKa9lyfWPmpZAwe6oV+Wb1zcmMS1HG24= +github.com/multiformats/go-multiaddr v0.12.2/go.mod h1:GKyaTYjZRdcUhyOetrxTk9z0cW+jA/YrnqTOvKgi44M= github.com/multiformats/go-multibase v0.2.0 h1:isdYCVLvksgWlMW9OZRYJEa9pZETFivncJHmHnnd87g= github.com/multiformats/go-multibase v0.2.0/go.mod h1:bFBZX4lKCA/2lyOFSAoKH5SS6oPyjtnzK/XTFDPkNuk= -github.com/multiformats/go-multicodec v0.8.1 h1:ycepHwavHafh3grIbR1jIXnKCsFm0fqsfEOsJ8NtKE8= -github.com/multiformats/go-multicodec v0.8.1/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI16i14xuaojr/H7Ai54k= -github.com/multiformats/go-multihash v0.2.1 h1:aem8ZT0VA2nCHHk7bPJ1BjUbHNciqZC/d16Vve9l108= -github.com/multiformats/go-multihash v0.2.1/go.mod h1:WxoMcYG85AZVQUyRyo9s4wULvW5qrI9vb2Lt6evduFc= -github.com/multiformats/go-multistream v0.4.1 h1:rFy0Iiyn3YT0asivDUIR05leAdwZq3de4741sbiSdfo= -github.com/multiformats/go-multistream v0.4.1/go.mod h1:Mz5eykRVAjJWckE2U78c6xqdtyNUEhKSM0Lwar2p77Q= +github.com/multiformats/go-multicodec v0.9.0 h1:pb/dlPnzee/Sxv/j4PmkDRxCOi3hXTz3IbPKOXWJkmg= +github.com/multiformats/go-multicodec v0.9.0/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI16i14xuaojr/H7Ai54k= +github.com/multiformats/go-multihash v0.2.3 h1:7Lyc8XfX/IY2jWb/gI7JP+o7JEq9hOa7BFvVU9RSh+U= +github.com/multiformats/go-multihash v0.2.3/go.mod h1:dXgKXCXjBzdscBLk9JkjINiEsCKRVch90MdaGiKsvSM= +github.com/multiformats/go-multistream v0.5.0 h1:5htLSLl7lvJk3xx3qT/8Zm9J4K8vEOf/QGkvOGQAyiE= +github.com/multiformats/go-multistream v0.5.0/go.mod h1:n6tMZiwiP2wUsR8DgfDWw1dydlEqV3l6N3/GBsX6ILA= github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/nEGOHFS8= github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= -github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU= -github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts= -github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E= +github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY= +github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM= +github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8= +github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= -github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw= -github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= -github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4= -github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= -github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM= -github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc= github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= -github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= -github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo= github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A= -github.com/quic-go/qtls-go1-19 v0.3.3 h1:wznEHvJwd+2X3PqftRha0SUKmGsnb6dfArMhy9PeJVE= -github.com/quic-go/qtls-go1-19 v0.3.3/go.mod h1:ySOI96ew8lnoKPtSqx2BlI5wCpUVPT05RMAlajtnyOI= -github.com/quic-go/qtls-go1-20 v0.2.3 h1:m575dovXn1y2ATOb1XrRFcrv0F+EQmlowTkoraNkDPI= -github.com/quic-go/qtls-go1-20 v0.2.3/go.mod h1:JKtK6mjbAVcUTN/9jZpvLbGxvdWIKS8uT7EiStoU1SM= -github.com/quic-go/quic-go v0.33.0 h1:ItNoTDN/Fm/zBlq769lLJc8ECe9gYaW40veHCCco7y0= -github.com/quic-go/quic-go v0.33.0/go.mod h1:YMuhaAV9/jIu0XclDXwZPAsP/2Kgr5yMYhe9oxhhOFA= -github.com/quic-go/webtransport-go v0.5.2 h1:GA6Bl6oZY+g/flt00Pnu0XtivSD8vukOu3lYhJjnGEk= -github.com/quic-go/webtransport-go v0.5.2/go.mod h1:OhmmgJIzTTqXK5xvtuX0oBpLV2GkLWNDA+UeTGJXErU= +github.com/quic-go/quic-go v0.42.0 h1:uSfdap0eveIl8KXnipv9K7nlwZ5IqLlYOpJ58u5utpM= +github.com/quic-go/quic-go v0.42.0/go.mod h1:132kz4kL3F9vxhW3CtQJLDVwcFe5wdWeJXXijhsO57M= +github.com/quic-go/webtransport-go v0.6.0 h1:CvNsKqc4W2HljHJnoT+rMmbRJybShZ0YPFDD3NxaZLY= +github.com/quic-go/webtransport-go v0.6.0/go.mod h1:9KjU4AEBqEQidGHNDkZrb8CAa1abRaosM2yGOyiikEc= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= @@ -199,23 +186,25 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= -go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= +go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI= -go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= -go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE= golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= @@ -224,11 +213,11 @@ golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200602180216-279210d13fed/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= -golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= -golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug= -golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE= +golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -236,8 +225,8 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHl golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= -golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= +golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -251,8 +240,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -275,10 +264,10 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -287,6 +276,8 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181030000716-a0a13e073c7b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -295,13 +286,11 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3 golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= -golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= +golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ= +golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= @@ -319,8 +308,6 @@ google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmE google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio= google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -334,11 +321,12 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -lukechampine.com/blake3 v1.1.7 h1:GgRMhmdsuK8+ii6UZFDL8Nb+VyMwadAgcJyfYHxG6n0= -lukechampine.com/blake3 v1.1.7/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA= +lukechampine.com/blake3 v1.2.1 h1:YuqqRuaqsGV71BV/nm9xlI0MKUv4QC54jQnBChWbGnI= +lukechampine.com/blake3 v1.2.1/go.mod h1:0OFRp7fBtAylGVCO40o87sbupkyIGgbpv1+M1k1LM6k= sourcegraph.com/sourcegraph/go-diff v0.5.0/go.mod h1:kuch7UrkMzY0X+p9CRK03kfuPQ2zzQcaEFbx8wA8rck= sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0= diff --git a/wasm-tests/webtransport-tests/echo-server/main.go b/wasm-tests/webtransport-tests/echo-server/main.go index def4151bd1b..f5e36692bd0 100644 --- a/wasm-tests/webtransport-tests/echo-server/main.go +++ b/wasm-tests/webtransport-tests/echo-server/main.go @@ -12,6 +12,7 @@ import ( "github.com/libp2p/go-libp2p/core/transport" "github.com/libp2p/go-libp2p/p2p/transport/quicreuse" webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport" + "github.com/quic-go/quic-go" "github.com/multiformats/go-multiaddr" ) @@ -69,7 +70,7 @@ func main() { panic(err) } - connManager, err := quicreuse.NewConnManager([32]byte{}) + connManager, err := quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{}) if err != nil { panic(err) } diff --git a/wasm-tests/webtransport-tests/src/lib.rs b/wasm-tests/webtransport-tests/src/lib.rs index 1f420cd6671..938cdf0b3e1 100644 --- a/wasm-tests/webtransport-tests/src/lib.rs +++ b/wasm-tests/webtransport-tests/src/lib.rs @@ -1,7 +1,8 @@ use futures::channel::oneshot; use futures::{AsyncReadExt, AsyncWriteExt}; use getrandom::getrandom; -use libp2p_core::{StreamMuxer, Transport as _}; +use libp2p_core::transport::{DialOpts, PortUse}; +use libp2p_core::{Endpoint, StreamMuxer, Transport as _}; use libp2p_identity::{Keypair, PeerId}; use libp2p_noise as noise; use libp2p_webtransport_websys::{Config, Connection, Error, Stream, Transport}; @@ -263,7 +264,17 @@ async fn connect_without_peer_id() { addr.pop(); let mut transport = Transport::new(Config::new(&keypair)); - transport.dial(addr).unwrap().await.unwrap(); + transport + .dial( + addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap() + .await + .unwrap(); } #[wasm_bindgen_test] @@ -278,7 +289,17 @@ async fn error_on_unknown_peer_id() { addr.push(Protocol::P2p(PeerId::random())); let mut transport = Transport::new(Config::new(&keypair)); - let e = transport.dial(addr.clone()).unwrap().await.unwrap_err(); + let e = transport + .dial( + addr.clone(), + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap() + .await + .unwrap_err(); assert!(matches!(e, Error::UnknownRemotePeerId)); } @@ -297,7 +318,17 @@ async fn error_on_unknown_certhash() { addr.push(peer_id); let mut transport = Transport::new(Config::new(&keypair)); - let e = transport.dial(addr.clone()).unwrap().await.unwrap_err(); + let e = transport + .dial( + addr.clone(), + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap() + .await + .unwrap_err(); assert!(matches!( e, Error::Noise(noise::Error::UnknownWebTransportCerthashes(..)) @@ -310,7 +341,17 @@ async fn new_connection_to_echo_server() -> Connection { let mut transport = Transport::new(Config::new(&keypair)); - let (_peer_id, conn) = transport.dial(addr).unwrap().await.unwrap(); + let (_peer_id, conn) = transport + .dial( + addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap() + .await + .unwrap(); conn }